1 /* BSD user-level threads support.
3 Copyright 2005 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
24 #include "gdbthread.h"
33 #include "gdb_assert.h"
34 #include "gdb_obstack.h"
36 #include "bsd-uthread.h"
38 /* HACK: Save the bsd_uthreads ops returned by bsd_uthread_target. */
39 static struct target_ops *bsd_uthread_ops_hack;
42 /* Architecture-specific operations. */
44 /* Per-architecture data key. */
45 static struct gdbarch_data *bsd_uthread_data;
47 struct bsd_uthread_ops
49 /* Supply registers for an inactive thread to a register cache. */
50 void (*supply_uthread)(struct regcache *, int, CORE_ADDR);
52 /* Collect registers for an inactive thread from a register cache. */
53 void (*collect_uthread)(const struct regcache *, int, CORE_ADDR);
57 bsd_uthread_init (struct obstack *obstack)
59 struct bsd_uthread_ops *ops;
61 ops = OBSTACK_ZALLOC (obstack, struct bsd_uthread_ops);
65 /* Set the function that supplies registers from an inactive thread
66 for architecture GDBARCH to SUPPLY_UTHREAD. */
69 bsd_uthread_set_supply_uthread (struct gdbarch *gdbarch,
70 void (*supply_uthread) (struct regcache *,
73 struct bsd_uthread_ops *ops = gdbarch_data (gdbarch, bsd_uthread_data);
74 ops->supply_uthread = supply_uthread;
77 /* Set the function that collects registers for an inactive thread for
78 architecture GDBARCH to SUPPLY_UTHREAD. */
81 bsd_uthread_set_collect_uthread (struct gdbarch *gdbarch,
82 void (*collect_uthread) (const struct regcache *,
85 struct bsd_uthread_ops *ops = gdbarch_data (gdbarch, bsd_uthread_data);
86 ops->collect_uthread = collect_uthread;
89 /* Magic number to help recognize a valid thread structure. */
90 #define BSD_UTHREAD_PTHREAD_MAGIC 0xd09ba115
92 /* Check whether the thread structure at ADDR is valid. */
95 bsd_uthread_check_magic (CORE_ADDR addr)
97 ULONGEST magic = read_memory_unsigned_integer (addr, 4);
99 if (magic != BSD_UTHREAD_PTHREAD_MAGIC)
104 #define BSD_UTHREAD_PS_RUNNING 0
105 #define BSD_UTHREAD_PS_DEAD 18
107 /* Address of the pointer to the the thread structure for the running
109 static CORE_ADDR bsd_uthread_thread_run_addr;
111 /* Address of the list of all threads. */
112 static CORE_ADDR bsd_uthread_thread_list_addr;
114 /* Offsets of various "interesting" bits in the thread structure. */
115 static int bsd_uthread_thread_state_offset = -1;
116 static int bsd_uthread_thread_next_offset = -1;
117 static int bsd_uthread_thread_ctx_offset;
119 /* Name of shared threads library. */
120 static const char *bsd_uthread_solib_name;
122 /* Non-zero if the thread startum implemented by this module is active. */
123 static int bsd_uthread_active;
126 bsd_uthread_lookup_address (const char *name, struct objfile *objfile)
128 struct minimal_symbol *sym;
130 sym = lookup_minimal_symbol (name, NULL, objfile);
132 return SYMBOL_VALUE_ADDRESS (sym);
138 bsd_uthread_lookup_offset (const char *name, struct objfile *objfile)
142 addr = bsd_uthread_lookup_address (name, objfile);
146 return read_memory_unsigned_integer (addr, 4);
149 /* If OBJFILE contains the symbols corresponding to one of the
150 supported user-level threads libraries, activate the thread stratum
151 implemented by this module. */
154 bsd_uthread_activate (struct objfile *objfile)
156 struct gdbarch *gdbarch = current_gdbarch;
157 struct bsd_uthread_ops *ops = gdbarch_data (gdbarch, bsd_uthread_data);
159 /* Skip if the thread stratum has already been activated. */
160 if (bsd_uthread_active)
163 /* There's no point in enabling this module if no
164 architecture-specific operations are provided. */
165 if (!ops->supply_uthread)
168 bsd_uthread_thread_run_addr =
169 bsd_uthread_lookup_address ("_thread_run", objfile);
170 if (bsd_uthread_thread_run_addr == 0)
173 bsd_uthread_thread_list_addr =
174 bsd_uthread_lookup_address ("_thread_list", objfile);
175 if (bsd_uthread_thread_list_addr == 0)
178 bsd_uthread_thread_state_offset =
179 bsd_uthread_lookup_offset ("_thread_state_offset", objfile);
180 if (bsd_uthread_thread_state_offset == 0)
183 bsd_uthread_thread_next_offset =
184 bsd_uthread_lookup_offset ("_thread_next_offset", objfile);
185 if (bsd_uthread_thread_next_offset == 0)
188 bsd_uthread_thread_ctx_offset =
189 bsd_uthread_lookup_offset ("_thread_ctx_offset", objfile);
191 push_target (bsd_uthread_ops_hack);
192 bsd_uthread_active = 1;
196 /* Deactivate the thread stratum implemented by this module. */
199 bsd_uthread_deactivate (void)
201 /* Skip if the thread stratum has already been deactivated. */
202 if (!bsd_uthread_active)
205 bsd_uthread_active = 0;
206 unpush_target (bsd_uthread_ops_hack);
208 bsd_uthread_thread_run_addr = 0;
209 bsd_uthread_thread_list_addr = 0;
210 bsd_uthread_thread_state_offset = 0;
211 bsd_uthread_thread_next_offset = 0;
212 bsd_uthread_thread_ctx_offset = 0;
213 bsd_uthread_solib_name = NULL;
217 bsd_uthread_inferior_created (struct target_ops *ops, int from_tty)
219 bsd_uthread_activate (NULL);
222 /* Likely candidates for the threads library. */
223 static const char *bsd_uthread_solib_names[] =
225 "/usr/lib/libc_r.so", /* FreeBSD */
226 "/usr/lib/libpthread.so", /* OpenBSD */
231 bsd_uthread_solib_loaded (struct so_list *so)
233 const char **names = bsd_uthread_solib_names;
235 for (names = bsd_uthread_solib_names; *names; names++)
237 if (strncmp (so->so_original_name, *names, strlen (*names)) == 0)
239 solib_read_symbols (so, so->from_tty);
241 if (bsd_uthread_activate (so->objfile))
243 bsd_uthread_solib_name == so->so_original_name;
251 bsd_uthread_solib_unloaded (struct so_list *so)
253 if (!bsd_uthread_solib_name)
256 if (strcmp (so->so_original_name, bsd_uthread_solib_name) == 0)
257 bsd_uthread_deactivate ();
261 bsd_uthread_mourn_inferior (void)
263 find_target_beneath (bsd_uthread_ops_hack)->to_mourn_inferior ();
264 bsd_uthread_deactivate ();
268 bsd_uthread_fetch_registers (int regnum)
270 struct gdbarch *gdbarch = current_gdbarch;
271 struct bsd_uthread_ops *ops = gdbarch_data (gdbarch, bsd_uthread_data);
272 CORE_ADDR addr = ptid_get_tid (inferior_ptid);
273 CORE_ADDR active_addr;
275 /* Always fetch the appropriate registers from the layer beneath. */
276 find_target_beneath (bsd_uthread_ops_hack)->to_fetch_registers (regnum);
278 /* FIXME: That might have gotten us more than we asked for. Make
279 sure we overwrite all relevant registers with values from the
280 thread structure. This can go once we fix the underlying target. */
283 active_addr = read_memory_typed_address (bsd_uthread_thread_run_addr,
284 builtin_type_void_data_ptr);
285 if (addr != 0 && addr != active_addr)
287 bsd_uthread_check_magic (addr);
288 ops->supply_uthread (current_regcache, regnum,
289 addr + bsd_uthread_thread_ctx_offset);
294 bsd_uthread_store_registers (int regnum)
296 struct gdbarch *gdbarch = current_gdbarch;
297 struct bsd_uthread_ops *ops = gdbarch_data (gdbarch, bsd_uthread_data);
298 CORE_ADDR addr = ptid_get_tid (inferior_ptid);
299 CORE_ADDR active_addr;
301 active_addr = read_memory_typed_address (bsd_uthread_thread_run_addr,
302 builtin_type_void_data_ptr);
303 if (addr != 0 && addr != active_addr)
305 bsd_uthread_check_magic (addr);
306 ops->collect_uthread (current_regcache, regnum,
307 addr + bsd_uthread_thread_ctx_offset);
311 /* Updating the thread that is currently running; pass the
312 request to the layer beneath. */
313 find_target_beneath (bsd_uthread_ops_hack)->to_store_registers (regnum);
317 /* FIXME: This function is only there because otherwise GDB tries to
318 invoke deprecate_xfer_memory. */
321 bsd_uthread_xfer_partial (struct target_ops *ops, enum target_object object,
322 const char *annex, void *readbuf,
323 const void *writebuf, ULONGEST offset, LONGEST len)
325 gdb_assert (ops->beneath->to_xfer_partial);
326 return ops->beneath->to_xfer_partial (ops->beneath, object, annex, readbuf,
327 writebuf, offset, len);
331 bsd_uthread_wait (ptid_t ptid, struct target_waitstatus *status)
335 /* Pass the request to the layer beneath. */
336 ptid = find_target_beneath (bsd_uthread_ops_hack)->to_wait (ptid, status);
338 /* Fetch the corresponding thread ID, and augment the returned
339 process ID with it. */
340 addr = read_memory_typed_address (bsd_uthread_thread_run_addr,
341 builtin_type_void_data_ptr);
346 /* FIXME: For executables linked statically with the threads
347 library, we end up here before the program has actually been
348 executed. In that case ADDR will be garbage since it has
349 been read from the wrong virtual memory image. */
350 if (target_read_memory (addr, buf, 4) == 0)
352 ULONGEST magic = extract_unsigned_integer (buf, 4);
353 if (magic == BSD_UTHREAD_PTHREAD_MAGIC)
354 ptid = ptid_build (ptid_get_pid (ptid), 0, addr);
358 /* HACK: Twiddle INFERIOR_PTID such that the initial thread of a
359 process isn't recognized as a new thread. */
360 if (ptid_get_tid (ptid) != 0 && !in_thread_list (ptid)
361 && ptid_get_tid (inferior_ptid) == 0)
364 inferior_ptid = ptid;
371 bsd_uthread_resume (ptid_t ptid, int step, enum target_signal sig)
373 /* Pass the request to the layer beneath. */
374 find_target_beneath (bsd_uthread_ops_hack)->to_resume (ptid, step, sig);
378 bsd_uthread_thread_alive (ptid_t ptid)
380 CORE_ADDR addr = ptid_get_tid (inferior_ptid);
384 int offset = bsd_uthread_thread_state_offset;
387 bsd_uthread_check_magic (addr);
389 state = read_memory_unsigned_integer (addr + offset, 4);
390 if (state == BSD_UTHREAD_PS_DEAD)
394 return find_target_beneath (bsd_uthread_ops_hack)->to_thread_alive (ptid);
398 bsd_uthread_find_new_threads (void)
400 pid_t pid = ptid_get_pid (inferior_ptid);
401 int offset = bsd_uthread_thread_next_offset;
404 addr = read_memory_typed_address (bsd_uthread_thread_list_addr,
405 builtin_type_void_data_ptr);
408 ptid_t ptid = ptid_build (pid, 0, addr);
410 if (!in_thread_list (ptid))
413 addr = read_memory_typed_address (addr + offset,
414 builtin_type_void_data_ptr);
418 /* Possible states a thread can be in. */
419 static char *bsd_uthread_state[] =
443 /* Return a string describing th state of the thread specified by
447 bsd_uthread_extra_thread_info (struct thread_info *info)
449 CORE_ADDR addr = ptid_get_tid (info->ptid);
453 int offset = bsd_uthread_thread_state_offset;
456 state = read_memory_unsigned_integer (addr + offset, 4);
457 if (state < ARRAY_SIZE (bsd_uthread_state))
458 return bsd_uthread_state[state];
465 bsd_uthread_pid_to_str (ptid_t ptid)
467 if (ptid_get_tid (ptid) != 0)
472 size = snprintf (buf, sizeof buf, "process %d, thread 0x%lx",
473 ptid_get_pid (ptid), ptid_get_tid (ptid));
474 gdb_assert (size < sizeof buf);
478 return normal_pid_to_str (ptid);
482 bsd_uthread_target (void)
484 struct target_ops *t = XZALLOC (struct target_ops);
486 t->to_shortname = "bsd-uthreads";
487 t->to_longname = "BSD user-level threads";
488 t->to_doc = "BSD user-level threads";
489 t->to_mourn_inferior = bsd_uthread_mourn_inferior;
490 t->to_fetch_registers = bsd_uthread_fetch_registers;
491 t->to_store_registers = bsd_uthread_store_registers;
492 t->to_xfer_partial = bsd_uthread_xfer_partial;
493 t->to_wait = bsd_uthread_wait;
494 t->to_resume = bsd_uthread_resume;
495 t->to_thread_alive = bsd_uthread_thread_alive;
496 t->to_find_new_threads = bsd_uthread_find_new_threads;
497 t->to_extra_thread_info = bsd_uthread_extra_thread_info;
498 t->to_pid_to_str = bsd_uthread_pid_to_str;
499 t->to_stratum = thread_stratum;
500 t->to_magic = OPS_MAGIC;
501 bsd_uthread_ops_hack = t;
507 _initialize_bsd_uthread (void)
509 add_target (bsd_uthread_target ());
511 bsd_uthread_data = gdbarch_data_register_pre_init (bsd_uthread_init);
513 observer_attach_inferior_created (bsd_uthread_inferior_created);
514 observer_attach_solib_loaded (bsd_uthread_solib_loaded);
515 observer_attach_solib_unloaded (bsd_uthread_solib_unloaded);