1 /* BSD user-level threads support.
3 Copyright (C) 2005-2014 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 3 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, see <http://www.gnu.org/licenses/>. */
22 #include "gdbthread.h"
32 #include "gdb_assert.h"
33 #include "gdb_obstack.h"
35 #include "bsd-uthread.h"
37 /* HACK: Save the bsd_uthreads ops returned by bsd_uthread_target. */
38 static struct target_ops *bsd_uthread_ops_hack;
41 /* Architecture-specific operations. */
43 /* Per-architecture data key. */
44 static struct gdbarch_data *bsd_uthread_data;
46 struct bsd_uthread_ops
48 /* Supply registers for an inactive thread to a register cache. */
49 void (*supply_uthread)(struct regcache *, int, CORE_ADDR);
51 /* Collect registers for an inactive thread from a register cache. */
52 void (*collect_uthread)(const struct regcache *, int, CORE_ADDR);
56 bsd_uthread_init (struct obstack *obstack)
58 struct bsd_uthread_ops *ops;
60 ops = OBSTACK_ZALLOC (obstack, struct bsd_uthread_ops);
64 /* Set the function that supplies registers from an inactive thread
65 for architecture GDBARCH to SUPPLY_UTHREAD. */
68 bsd_uthread_set_supply_uthread (struct gdbarch *gdbarch,
69 void (*supply_uthread) (struct regcache *,
72 struct bsd_uthread_ops *ops = gdbarch_data (gdbarch, bsd_uthread_data);
73 ops->supply_uthread = supply_uthread;
76 /* Set the function that collects registers for an inactive thread for
77 architecture GDBARCH to SUPPLY_UTHREAD. */
80 bsd_uthread_set_collect_uthread (struct gdbarch *gdbarch,
81 void (*collect_uthread) (const struct regcache *,
84 struct bsd_uthread_ops *ops = gdbarch_data (gdbarch, bsd_uthread_data);
85 ops->collect_uthread = collect_uthread;
88 /* Magic number to help recognize a valid thread structure. */
89 #define BSD_UTHREAD_PTHREAD_MAGIC 0xd09ba115
91 /* Check whether the thread structure at ADDR is valid. */
94 bsd_uthread_check_magic (CORE_ADDR addr)
96 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
97 ULONGEST magic = read_memory_unsigned_integer (addr, 4, byte_order);
99 if (magic != BSD_UTHREAD_PTHREAD_MAGIC)
100 error (_("Bad magic"));
104 #define BSD_UTHREAD_PS_RUNNING 0
105 #define BSD_UTHREAD_PS_DEAD 18
107 /* Address of the pointer to 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 bound_minimal_symbol sym;
130 sym = lookup_minimal_symbol (name, NULL, objfile);
132 return BMSYMBOL_VALUE_ADDRESS (sym);
138 bsd_uthread_lookup_offset (const char *name, struct objfile *objfile)
140 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
143 addr = bsd_uthread_lookup_address (name, objfile);
147 return read_memory_unsigned_integer (addr, 4, byte_order);
151 bsd_uthread_read_memory_address (CORE_ADDR addr)
153 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
154 return read_memory_typed_address (addr, ptr_type);
157 /* If OBJFILE contains the symbols corresponding to one of the
158 supported user-level threads libraries, activate the thread stratum
159 implemented by this module. */
162 bsd_uthread_activate (struct objfile *objfile)
164 struct gdbarch *gdbarch = target_gdbarch ();
165 struct bsd_uthread_ops *ops = gdbarch_data (gdbarch, bsd_uthread_data);
167 /* Skip if the thread stratum has already been activated. */
168 if (bsd_uthread_active)
171 /* There's no point in enabling this module if no
172 architecture-specific operations are provided. */
173 if (!ops->supply_uthread)
176 bsd_uthread_thread_run_addr =
177 bsd_uthread_lookup_address ("_thread_run", objfile);
178 if (bsd_uthread_thread_run_addr == 0)
181 bsd_uthread_thread_list_addr =
182 bsd_uthread_lookup_address ("_thread_list", objfile);
183 if (bsd_uthread_thread_list_addr == 0)
186 bsd_uthread_thread_state_offset =
187 bsd_uthread_lookup_offset ("_thread_state_offset", objfile);
188 if (bsd_uthread_thread_state_offset == 0)
191 bsd_uthread_thread_next_offset =
192 bsd_uthread_lookup_offset ("_thread_next_offset", objfile);
193 if (bsd_uthread_thread_next_offset == 0)
196 bsd_uthread_thread_ctx_offset =
197 bsd_uthread_lookup_offset ("_thread_ctx_offset", objfile);
199 push_target (bsd_uthread_ops_hack);
200 bsd_uthread_active = 1;
204 /* Cleanup due to deactivation. */
207 bsd_uthread_close (struct target_ops *self)
209 bsd_uthread_active = 0;
210 bsd_uthread_thread_run_addr = 0;
211 bsd_uthread_thread_list_addr = 0;
212 bsd_uthread_thread_state_offset = 0;
213 bsd_uthread_thread_next_offset = 0;
214 bsd_uthread_thread_ctx_offset = 0;
215 bsd_uthread_solib_name = NULL;
218 /* Deactivate the thread stratum implemented by this module. */
221 bsd_uthread_deactivate (void)
223 /* Skip if the thread stratum has already been deactivated. */
224 if (!bsd_uthread_active)
227 unpush_target (bsd_uthread_ops_hack);
231 bsd_uthread_inferior_created (struct target_ops *ops, int from_tty)
233 bsd_uthread_activate (NULL);
236 /* Likely candidates for the threads library. */
237 static const char *bsd_uthread_solib_names[] =
239 "/usr/lib/libc_r.so", /* FreeBSD */
240 "/usr/lib/libpthread.so", /* OpenBSD */
245 bsd_uthread_solib_loaded (struct so_list *so)
247 const char **names = bsd_uthread_solib_names;
249 for (names = bsd_uthread_solib_names; *names; names++)
251 if (strncmp (so->so_original_name, *names, strlen (*names)) == 0)
253 solib_read_symbols (so, 0);
255 if (bsd_uthread_activate (so->objfile))
257 bsd_uthread_solib_name = so->so_original_name;
265 bsd_uthread_solib_unloaded (struct so_list *so)
267 if (!bsd_uthread_solib_name)
270 if (strcmp (so->so_original_name, bsd_uthread_solib_name) == 0)
271 bsd_uthread_deactivate ();
275 bsd_uthread_mourn_inferior (struct target_ops *ops)
277 struct target_ops *beneath = find_target_beneath (ops);
278 beneath->to_mourn_inferior (beneath);
279 bsd_uthread_deactivate ();
283 bsd_uthread_fetch_registers (struct target_ops *ops,
284 struct regcache *regcache, int regnum)
286 struct gdbarch *gdbarch = get_regcache_arch (regcache);
287 struct bsd_uthread_ops *uthread_ops = gdbarch_data (gdbarch, bsd_uthread_data);
288 CORE_ADDR addr = ptid_get_tid (inferior_ptid);
289 struct target_ops *beneath = find_target_beneath (ops);
290 CORE_ADDR active_addr;
292 /* Always fetch the appropriate registers from the layer beneath. */
293 beneath->to_fetch_registers (beneath, regcache, regnum);
295 /* FIXME: That might have gotten us more than we asked for. Make
296 sure we overwrite all relevant registers with values from the
297 thread structure. This can go once we fix the underlying target. */
300 active_addr = bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr);
301 if (addr != 0 && addr != active_addr)
303 bsd_uthread_check_magic (addr);
304 uthread_ops->supply_uthread (regcache, regnum,
305 addr + bsd_uthread_thread_ctx_offset);
310 bsd_uthread_store_registers (struct target_ops *ops,
311 struct regcache *regcache, int regnum)
313 struct gdbarch *gdbarch = get_regcache_arch (regcache);
314 struct bsd_uthread_ops *uthread_ops = gdbarch_data (gdbarch, bsd_uthread_data);
315 struct target_ops *beneath = find_target_beneath (ops);
316 CORE_ADDR addr = ptid_get_tid (inferior_ptid);
317 CORE_ADDR active_addr;
319 active_addr = bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr);
320 if (addr != 0 && addr != active_addr)
322 bsd_uthread_check_magic (addr);
323 uthread_ops->collect_uthread (regcache, regnum,
324 addr + bsd_uthread_thread_ctx_offset);
328 /* Updating the thread that is currently running; pass the
329 request to the layer beneath. */
330 beneath->to_store_registers (beneath, regcache, regnum);
335 bsd_uthread_wait (struct target_ops *ops,
336 ptid_t ptid, struct target_waitstatus *status, int options)
338 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
340 struct target_ops *beneath = find_target_beneath (ops);
342 /* Pass the request to the layer beneath. */
343 ptid = beneath->to_wait (beneath, ptid, status, options);
345 /* If the process is no longer alive, there's no point in figuring
346 out the thread ID. It will fail anyway. */
347 if (status->kind == TARGET_WAITKIND_SIGNALLED
348 || status->kind == TARGET_WAITKIND_EXITED)
351 /* Fetch the corresponding thread ID, and augment the returned
352 process ID with it. */
353 addr = bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr);
358 /* FIXME: For executables linked statically with the threads
359 library, we end up here before the program has actually been
360 executed. In that case ADDR will be garbage since it has
361 been read from the wrong virtual memory image. */
362 if (target_read_memory (addr, buf, 4) == 0)
364 ULONGEST magic = extract_unsigned_integer (buf, 4, byte_order);
365 if (magic == BSD_UTHREAD_PTHREAD_MAGIC)
366 ptid = ptid_build (ptid_get_pid (ptid), 0, addr);
370 /* If INFERIOR_PTID doesn't have a tid member yet, and we now have a
371 ptid with tid set, then ptid is still the initial thread of
372 the process. Notify GDB core about it. */
373 if (ptid_get_tid (inferior_ptid) == 0
374 && ptid_get_tid (ptid) != 0 && !in_thread_list (ptid))
375 thread_change_ptid (inferior_ptid, ptid);
377 /* Don't let the core see a ptid without a corresponding thread. */
378 if (!in_thread_list (ptid) || is_exited (ptid))
385 bsd_uthread_resume (struct target_ops *ops,
386 ptid_t ptid, int step, enum gdb_signal sig)
388 /* Pass the request to the layer beneath. */
389 struct target_ops *beneath = find_target_beneath (ops);
390 beneath->to_resume (beneath, ptid, step, sig);
394 bsd_uthread_thread_alive (struct target_ops *ops, ptid_t ptid)
396 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
397 struct target_ops *beneath = find_target_beneath (ops);
398 CORE_ADDR addr = ptid_get_tid (inferior_ptid);
402 int offset = bsd_uthread_thread_state_offset;
405 bsd_uthread_check_magic (addr);
407 state = read_memory_unsigned_integer (addr + offset, 4, byte_order);
408 if (state == BSD_UTHREAD_PS_DEAD)
412 return beneath->to_thread_alive (beneath, ptid);
416 bsd_uthread_find_new_threads (struct target_ops *ops)
418 pid_t pid = ptid_get_pid (inferior_ptid);
419 int offset = bsd_uthread_thread_next_offset;
422 addr = bsd_uthread_read_memory_address (bsd_uthread_thread_list_addr);
425 ptid_t ptid = ptid_build (pid, 0, addr);
427 if (!in_thread_list (ptid) || is_exited (ptid))
429 /* If INFERIOR_PTID doesn't have a tid member yet, then ptid
430 is still the initial thread of the process. Notify GDB
432 if (ptid_get_tid (inferior_ptid) == 0)
433 thread_change_ptid (inferior_ptid, ptid);
438 addr = bsd_uthread_read_memory_address (addr + offset);
442 /* Possible states a thread can be in. */
443 static char *bsd_uthread_state[] =
467 /* Return a string describing th state of the thread specified by
471 bsd_uthread_extra_thread_info (struct target_ops *self,
472 struct thread_info *info)
474 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
475 CORE_ADDR addr = ptid_get_tid (info->ptid);
479 int offset = bsd_uthread_thread_state_offset;
482 state = read_memory_unsigned_integer (addr + offset, 4, byte_order);
483 if (state < ARRAY_SIZE (bsd_uthread_state))
484 return bsd_uthread_state[state];
491 bsd_uthread_pid_to_str (struct target_ops *ops, ptid_t ptid)
493 if (ptid_get_tid (ptid) != 0)
497 xsnprintf (buf, sizeof buf, "process %d, thread 0x%lx",
498 ptid_get_pid (ptid), ptid_get_tid (ptid));
502 return normal_pid_to_str (ptid);
505 static struct target_ops *
506 bsd_uthread_target (void)
508 struct target_ops *t = XCNEW (struct target_ops);
510 t->to_shortname = "bsd-uthreads";
511 t->to_longname = "BSD user-level threads";
512 t->to_doc = "BSD user-level threads";
513 t->to_close = bsd_uthread_close;
514 t->to_mourn_inferior = bsd_uthread_mourn_inferior;
515 t->to_fetch_registers = bsd_uthread_fetch_registers;
516 t->to_store_registers = bsd_uthread_store_registers;
517 t->to_wait = bsd_uthread_wait;
518 t->to_resume = bsd_uthread_resume;
519 t->to_thread_alive = bsd_uthread_thread_alive;
520 t->to_find_new_threads = bsd_uthread_find_new_threads;
521 t->to_extra_thread_info = bsd_uthread_extra_thread_info;
522 t->to_pid_to_str = bsd_uthread_pid_to_str;
523 t->to_stratum = thread_stratum;
524 t->to_magic = OPS_MAGIC;
525 bsd_uthread_ops_hack = t;
530 /* Provide a prototype to silence -Wmissing-prototypes. */
531 extern initialize_file_ftype _initialize_bsd_uthread;
534 _initialize_bsd_uthread (void)
536 complete_target_initialization (bsd_uthread_target ());
538 bsd_uthread_data = gdbarch_data_register_pre_init (bsd_uthread_init);
540 observer_attach_inferior_created (bsd_uthread_inferior_created);
541 observer_attach_solib_loaded (bsd_uthread_solib_loaded);
542 observer_attach_solib_unloaded (bsd_uthread_solib_unloaded);