1 /* BSD user-level threads support.
3 Copyright (C) 2005-2018 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"
25 #include "observable.h"
32 #include "gdb_obstack.h"
34 #include "bsd-uthread.h"
36 /* HACK: Save the bsd_uthreads ops returned by bsd_uthread_target. */
37 static struct target_ops *bsd_uthread_ops_hack;
40 /* Architecture-specific operations. */
42 /* Per-architecture data key. */
43 static struct gdbarch_data *bsd_uthread_data;
45 struct bsd_uthread_ops
47 /* Supply registers for an inactive thread to a register cache. */
48 void (*supply_uthread)(struct regcache *, int, CORE_ADDR);
50 /* Collect registers for an inactive thread from a register cache. */
51 void (*collect_uthread)(const struct regcache *, int, CORE_ADDR);
55 bsd_uthread_init (struct obstack *obstack)
57 struct bsd_uthread_ops *ops;
59 ops = OBSTACK_ZALLOC (obstack, struct bsd_uthread_ops);
63 /* Set the function that supplies registers from an inactive thread
64 for architecture GDBARCH to SUPPLY_UTHREAD. */
67 bsd_uthread_set_supply_uthread (struct gdbarch *gdbarch,
68 void (*supply_uthread) (struct regcache *,
71 struct bsd_uthread_ops *ops
72 = (struct bsd_uthread_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
86 = (struct bsd_uthread_ops *) gdbarch_data (gdbarch, bsd_uthread_data);
88 ops->collect_uthread = collect_uthread;
91 /* Magic number to help recognize a valid thread structure. */
92 #define BSD_UTHREAD_PTHREAD_MAGIC 0xd09ba115
94 /* Check whether the thread structure at ADDR is valid. */
97 bsd_uthread_check_magic (CORE_ADDR addr)
99 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
100 ULONGEST magic = read_memory_unsigned_integer (addr, 4, byte_order);
102 if (magic != BSD_UTHREAD_PTHREAD_MAGIC)
103 error (_("Bad magic"));
107 #define BSD_UTHREAD_PS_RUNNING 0
108 #define BSD_UTHREAD_PS_DEAD 18
110 /* Address of the pointer to the thread structure for the running
112 static CORE_ADDR bsd_uthread_thread_run_addr;
114 /* Address of the list of all threads. */
115 static CORE_ADDR bsd_uthread_thread_list_addr;
117 /* Offsets of various "interesting" bits in the thread structure. */
118 static int bsd_uthread_thread_state_offset = -1;
119 static int bsd_uthread_thread_next_offset = -1;
120 static int bsd_uthread_thread_ctx_offset;
122 /* Name of shared threads library. */
123 static const char *bsd_uthread_solib_name;
125 /* Non-zero if the thread startum implemented by this module is active. */
126 static int bsd_uthread_active;
129 bsd_uthread_lookup_address (const char *name, struct objfile *objfile)
131 struct bound_minimal_symbol sym;
133 sym = lookup_minimal_symbol (name, NULL, objfile);
135 return BMSYMBOL_VALUE_ADDRESS (sym);
141 bsd_uthread_lookup_offset (const char *name, struct objfile *objfile)
143 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
146 addr = bsd_uthread_lookup_address (name, objfile);
150 return read_memory_unsigned_integer (addr, 4, byte_order);
154 bsd_uthread_read_memory_address (CORE_ADDR addr)
156 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
157 return read_memory_typed_address (addr, ptr_type);
160 /* If OBJFILE contains the symbols corresponding to one of the
161 supported user-level threads libraries, activate the thread stratum
162 implemented by this module. */
165 bsd_uthread_activate (struct objfile *objfile)
167 struct gdbarch *gdbarch = target_gdbarch ();
168 struct bsd_uthread_ops *ops
169 = (struct bsd_uthread_ops *) gdbarch_data (gdbarch, bsd_uthread_data);
171 /* Skip if the thread stratum has already been activated. */
172 if (bsd_uthread_active)
175 /* There's no point in enabling this module if no
176 architecture-specific operations are provided. */
177 if (!ops->supply_uthread)
180 bsd_uthread_thread_run_addr =
181 bsd_uthread_lookup_address ("_thread_run", objfile);
182 if (bsd_uthread_thread_run_addr == 0)
185 bsd_uthread_thread_list_addr =
186 bsd_uthread_lookup_address ("_thread_list", objfile);
187 if (bsd_uthread_thread_list_addr == 0)
190 bsd_uthread_thread_state_offset =
191 bsd_uthread_lookup_offset ("_thread_state_offset", objfile);
192 if (bsd_uthread_thread_state_offset == 0)
195 bsd_uthread_thread_next_offset =
196 bsd_uthread_lookup_offset ("_thread_next_offset", objfile);
197 if (bsd_uthread_thread_next_offset == 0)
200 bsd_uthread_thread_ctx_offset =
201 bsd_uthread_lookup_offset ("_thread_ctx_offset", objfile);
203 push_target (bsd_uthread_ops_hack);
204 bsd_uthread_active = 1;
208 /* Cleanup due to deactivation. */
211 bsd_uthread_close (struct target_ops *self)
213 bsd_uthread_active = 0;
214 bsd_uthread_thread_run_addr = 0;
215 bsd_uthread_thread_list_addr = 0;
216 bsd_uthread_thread_state_offset = 0;
217 bsd_uthread_thread_next_offset = 0;
218 bsd_uthread_thread_ctx_offset = 0;
219 bsd_uthread_solib_name = NULL;
222 /* Deactivate the thread stratum implemented by this module. */
225 bsd_uthread_deactivate (void)
227 /* Skip if the thread stratum has already been deactivated. */
228 if (!bsd_uthread_active)
231 unpush_target (bsd_uthread_ops_hack);
235 bsd_uthread_inferior_created (struct target_ops *ops, int from_tty)
237 bsd_uthread_activate (NULL);
240 /* Likely candidates for the threads library. */
241 static const char *bsd_uthread_solib_names[] =
243 "/usr/lib/libc_r.so", /* FreeBSD */
244 "/usr/lib/libpthread.so", /* OpenBSD */
249 bsd_uthread_solib_loaded (struct so_list *so)
251 const char **names = bsd_uthread_solib_names;
253 for (names = bsd_uthread_solib_names; *names; names++)
255 if (startswith (so->so_original_name, *names))
257 solib_read_symbols (so, 0);
259 if (bsd_uthread_activate (so->objfile))
261 bsd_uthread_solib_name = so->so_original_name;
269 bsd_uthread_solib_unloaded (struct so_list *so)
271 if (!bsd_uthread_solib_name)
274 if (strcmp (so->so_original_name, bsd_uthread_solib_name) == 0)
275 bsd_uthread_deactivate ();
279 bsd_uthread_mourn_inferior (struct target_ops *ops)
281 struct target_ops *beneath = find_target_beneath (ops);
282 beneath->to_mourn_inferior (beneath);
283 bsd_uthread_deactivate ();
287 bsd_uthread_fetch_registers (struct target_ops *ops,
288 struct regcache *regcache, int regnum)
290 struct gdbarch *gdbarch = regcache->arch ();
291 struct bsd_uthread_ops *uthread_ops
292 = (struct bsd_uthread_ops *) gdbarch_data (gdbarch, bsd_uthread_data);
293 ptid_t ptid = regcache_get_ptid (regcache);
294 CORE_ADDR addr = ptid_get_tid (ptid);
295 struct target_ops *beneath = find_target_beneath (ops);
296 CORE_ADDR active_addr;
297 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
299 /* We are doing operations (e.g. reading memory) that rely on
301 inferior_ptid = ptid;
303 /* Always fetch the appropriate registers from the layer beneath. */
304 beneath->to_fetch_registers (beneath, regcache, regnum);
306 /* FIXME: That might have gotten us more than we asked for. Make
307 sure we overwrite all relevant registers with values from the
308 thread structure. This can go once we fix the underlying target. */
311 active_addr = bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr);
312 if (addr != 0 && addr != active_addr)
314 bsd_uthread_check_magic (addr);
315 uthread_ops->supply_uthread (regcache, regnum,
316 addr + bsd_uthread_thread_ctx_offset);
321 bsd_uthread_store_registers (struct target_ops *ops,
322 struct regcache *regcache, int regnum)
324 struct gdbarch *gdbarch = regcache->arch ();
325 struct bsd_uthread_ops *uthread_ops
326 = (struct bsd_uthread_ops *) gdbarch_data (gdbarch, bsd_uthread_data);
327 struct target_ops *beneath = find_target_beneath (ops);
328 ptid_t ptid = regcache_get_ptid (regcache);
329 CORE_ADDR addr = ptid_get_tid (ptid);
330 CORE_ADDR active_addr;
331 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
333 /* We are doing operations (e.g. reading memory) that rely on
335 inferior_ptid = ptid;
337 active_addr = bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr);
338 if (addr != 0 && addr != active_addr)
340 bsd_uthread_check_magic (addr);
341 uthread_ops->collect_uthread (regcache, regnum,
342 addr + bsd_uthread_thread_ctx_offset);
346 /* Updating the thread that is currently running; pass the
347 request to the layer beneath. */
348 beneath->to_store_registers (beneath, regcache, regnum);
353 bsd_uthread_wait (struct target_ops *ops,
354 ptid_t ptid, struct target_waitstatus *status, int options)
356 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
358 struct target_ops *beneath = find_target_beneath (ops);
360 /* Pass the request to the layer beneath. */
361 ptid = beneath->to_wait (beneath, ptid, status, options);
363 /* If the process is no longer alive, there's no point in figuring
364 out the thread ID. It will fail anyway. */
365 if (status->kind == TARGET_WAITKIND_SIGNALLED
366 || status->kind == TARGET_WAITKIND_EXITED)
369 /* Fetch the corresponding thread ID, and augment the returned
370 process ID with it. */
371 addr = bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr);
376 /* FIXME: For executables linked statically with the threads
377 library, we end up here before the program has actually been
378 executed. In that case ADDR will be garbage since it has
379 been read from the wrong virtual memory image. */
380 if (target_read_memory (addr, buf, 4) == 0)
382 ULONGEST magic = extract_unsigned_integer (buf, 4, byte_order);
383 if (magic == BSD_UTHREAD_PTHREAD_MAGIC)
384 ptid = ptid_build (ptid_get_pid (ptid), 0, addr);
388 /* If INFERIOR_PTID doesn't have a tid member yet, and we now have a
389 ptid with tid set, then ptid is still the initial thread of
390 the process. Notify GDB core about it. */
391 if (ptid_get_tid (inferior_ptid) == 0
392 && ptid_get_tid (ptid) != 0 && !in_thread_list (ptid))
393 thread_change_ptid (inferior_ptid, ptid);
395 /* Don't let the core see a ptid without a corresponding thread. */
396 if (!in_thread_list (ptid) || is_exited (ptid))
403 bsd_uthread_resume (struct target_ops *ops,
404 ptid_t ptid, int step, enum gdb_signal sig)
406 /* Pass the request to the layer beneath. */
407 struct target_ops *beneath = find_target_beneath (ops);
408 beneath->to_resume (beneath, ptid, step, sig);
412 bsd_uthread_thread_alive (struct target_ops *ops, ptid_t ptid)
414 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
415 struct target_ops *beneath = find_target_beneath (ops);
416 CORE_ADDR addr = ptid_get_tid (ptid);
420 int offset = bsd_uthread_thread_state_offset;
423 bsd_uthread_check_magic (addr);
425 state = read_memory_unsigned_integer (addr + offset, 4, byte_order);
426 if (state == BSD_UTHREAD_PS_DEAD)
430 return beneath->to_thread_alive (beneath, ptid);
434 bsd_uthread_update_thread_list (struct target_ops *ops)
436 pid_t pid = ptid_get_pid (inferior_ptid);
437 int offset = bsd_uthread_thread_next_offset;
442 addr = bsd_uthread_read_memory_address (bsd_uthread_thread_list_addr);
445 ptid_t ptid = ptid_build (pid, 0, addr);
447 if (!in_thread_list (ptid) || is_exited (ptid))
449 /* If INFERIOR_PTID doesn't have a tid member yet, then ptid
450 is still the initial thread of the process. Notify GDB
452 if (ptid_get_tid (inferior_ptid) == 0)
453 thread_change_ptid (inferior_ptid, ptid);
458 addr = bsd_uthread_read_memory_address (addr + offset);
462 /* Possible states a thread can be in. */
463 static const char *bsd_uthread_state[] =
487 /* Return a string describing th state of the thread specified by
491 bsd_uthread_extra_thread_info (struct target_ops *self,
492 struct thread_info *info)
494 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
495 CORE_ADDR addr = ptid_get_tid (info->ptid);
499 int offset = bsd_uthread_thread_state_offset;
502 state = read_memory_unsigned_integer (addr + offset, 4, byte_order);
503 if (state < ARRAY_SIZE (bsd_uthread_state))
504 return bsd_uthread_state[state];
511 bsd_uthread_pid_to_str (struct target_ops *ops, ptid_t ptid)
513 if (ptid_get_tid (ptid) != 0)
517 xsnprintf (buf, sizeof buf, "process %d, thread 0x%lx",
518 ptid_get_pid (ptid), ptid_get_tid (ptid));
522 return normal_pid_to_str (ptid);
525 static struct target_ops *
526 bsd_uthread_target (void)
528 struct target_ops *t = XCNEW (struct target_ops);
530 t->to_shortname = "bsd-uthreads";
531 t->to_longname = "BSD user-level threads";
532 t->to_doc = "BSD user-level threads";
533 t->to_close = bsd_uthread_close;
534 t->to_mourn_inferior = bsd_uthread_mourn_inferior;
535 t->to_fetch_registers = bsd_uthread_fetch_registers;
536 t->to_store_registers = bsd_uthread_store_registers;
537 t->to_wait = bsd_uthread_wait;
538 t->to_resume = bsd_uthread_resume;
539 t->to_thread_alive = bsd_uthread_thread_alive;
540 t->to_update_thread_list = bsd_uthread_update_thread_list;
541 t->to_extra_thread_info = bsd_uthread_extra_thread_info;
542 t->to_pid_to_str = bsd_uthread_pid_to_str;
543 t->to_stratum = thread_stratum;
544 t->to_magic = OPS_MAGIC;
545 bsd_uthread_ops_hack = t;
551 _initialize_bsd_uthread (void)
553 complete_target_initialization (bsd_uthread_target ());
555 bsd_uthread_data = gdbarch_data_register_pre_init (bsd_uthread_init);
557 gdb::observers::inferior_created.attach (bsd_uthread_inferior_created);
558 gdb::observers::solib_loaded.attach (bsd_uthread_solib_loaded);
559 gdb::observers::solib_unloaded.attach (bsd_uthread_solib_unloaded);