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 static const target_info bsd_uthread_target_info = {
38 N_("BSD user-level threads"),
39 N_("BSD user-level threads")
42 struct bsd_uthread_target final : public target_ops
45 { to_stratum = thread_stratum; }
47 const target_info &info () const override
48 { return bsd_uthread_target_info; }
50 void close () override;
52 void mourn_inferior () override;
54 void fetch_registers (struct regcache *, int) override;
55 void store_registers (struct regcache *, int) override;
57 ptid_t wait (ptid_t, struct target_waitstatus *, int) override;
58 void resume (ptid_t, int, enum gdb_signal) override;
60 bool thread_alive (ptid_t ptid) override;
62 void update_thread_list () override;
64 const char *extra_thread_info (struct thread_info *) override;
66 const char *pid_to_str (ptid_t) override;
69 static bsd_uthread_target bsd_uthread_ops;
72 /* Architecture-specific operations. */
74 /* Per-architecture data key. */
75 static struct gdbarch_data *bsd_uthread_data;
77 struct bsd_uthread_ops
79 /* Supply registers for an inactive thread to a register cache. */
80 void (*supply_uthread)(struct regcache *, int, CORE_ADDR);
82 /* Collect registers for an inactive thread from a register cache. */
83 void (*collect_uthread)(const struct regcache *, int, CORE_ADDR);
87 bsd_uthread_init (struct obstack *obstack)
89 struct bsd_uthread_ops *ops;
91 ops = OBSTACK_ZALLOC (obstack, struct bsd_uthread_ops);
95 /* Set the function that supplies registers from an inactive thread
96 for architecture GDBARCH to SUPPLY_UTHREAD. */
99 bsd_uthread_set_supply_uthread (struct gdbarch *gdbarch,
100 void (*supply_uthread) (struct regcache *,
103 struct bsd_uthread_ops *ops
104 = (struct bsd_uthread_ops *) gdbarch_data (gdbarch, bsd_uthread_data);
106 ops->supply_uthread = supply_uthread;
109 /* Set the function that collects registers for an inactive thread for
110 architecture GDBARCH to SUPPLY_UTHREAD. */
113 bsd_uthread_set_collect_uthread (struct gdbarch *gdbarch,
114 void (*collect_uthread) (const struct regcache *,
117 struct bsd_uthread_ops *ops
118 = (struct bsd_uthread_ops *) gdbarch_data (gdbarch, bsd_uthread_data);
120 ops->collect_uthread = collect_uthread;
123 /* Magic number to help recognize a valid thread structure. */
124 #define BSD_UTHREAD_PTHREAD_MAGIC 0xd09ba115
126 /* Check whether the thread structure at ADDR is valid. */
129 bsd_uthread_check_magic (CORE_ADDR addr)
131 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
132 ULONGEST magic = read_memory_unsigned_integer (addr, 4, byte_order);
134 if (magic != BSD_UTHREAD_PTHREAD_MAGIC)
135 error (_("Bad magic"));
139 #define BSD_UTHREAD_PS_RUNNING 0
140 #define BSD_UTHREAD_PS_DEAD 18
142 /* Address of the pointer to the thread structure for the running
144 static CORE_ADDR bsd_uthread_thread_run_addr;
146 /* Address of the list of all threads. */
147 static CORE_ADDR bsd_uthread_thread_list_addr;
149 /* Offsets of various "interesting" bits in the thread structure. */
150 static int bsd_uthread_thread_state_offset = -1;
151 static int bsd_uthread_thread_next_offset = -1;
152 static int bsd_uthread_thread_ctx_offset;
154 /* Name of shared threads library. */
155 static const char *bsd_uthread_solib_name;
157 /* Non-zero if the thread startum implemented by this module is active. */
158 static int bsd_uthread_active;
161 bsd_uthread_lookup_address (const char *name, struct objfile *objfile)
163 struct bound_minimal_symbol sym;
165 sym = lookup_minimal_symbol (name, NULL, objfile);
167 return BMSYMBOL_VALUE_ADDRESS (sym);
173 bsd_uthread_lookup_offset (const char *name, struct objfile *objfile)
175 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
178 addr = bsd_uthread_lookup_address (name, objfile);
182 return read_memory_unsigned_integer (addr, 4, byte_order);
186 bsd_uthread_read_memory_address (CORE_ADDR addr)
188 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
189 return read_memory_typed_address (addr, ptr_type);
192 /* If OBJFILE contains the symbols corresponding to one of the
193 supported user-level threads libraries, activate the thread stratum
194 implemented by this module. */
197 bsd_uthread_activate (struct objfile *objfile)
199 struct gdbarch *gdbarch = target_gdbarch ();
200 struct bsd_uthread_ops *ops
201 = (struct bsd_uthread_ops *) gdbarch_data (gdbarch, bsd_uthread_data);
203 /* Skip if the thread stratum has already been activated. */
204 if (bsd_uthread_active)
207 /* There's no point in enabling this module if no
208 architecture-specific operations are provided. */
209 if (!ops->supply_uthread)
212 bsd_uthread_thread_run_addr =
213 bsd_uthread_lookup_address ("_thread_run", objfile);
214 if (bsd_uthread_thread_run_addr == 0)
217 bsd_uthread_thread_list_addr =
218 bsd_uthread_lookup_address ("_thread_list", objfile);
219 if (bsd_uthread_thread_list_addr == 0)
222 bsd_uthread_thread_state_offset =
223 bsd_uthread_lookup_offset ("_thread_state_offset", objfile);
224 if (bsd_uthread_thread_state_offset == 0)
227 bsd_uthread_thread_next_offset =
228 bsd_uthread_lookup_offset ("_thread_next_offset", objfile);
229 if (bsd_uthread_thread_next_offset == 0)
232 bsd_uthread_thread_ctx_offset =
233 bsd_uthread_lookup_offset ("_thread_ctx_offset", objfile);
235 push_target (&bsd_uthread_ops);
236 bsd_uthread_active = 1;
240 /* Cleanup due to deactivation. */
243 bsd_uthread_target::close ()
245 bsd_uthread_active = 0;
246 bsd_uthread_thread_run_addr = 0;
247 bsd_uthread_thread_list_addr = 0;
248 bsd_uthread_thread_state_offset = 0;
249 bsd_uthread_thread_next_offset = 0;
250 bsd_uthread_thread_ctx_offset = 0;
251 bsd_uthread_solib_name = NULL;
254 /* Deactivate the thread stratum implemented by this module. */
257 bsd_uthread_deactivate (void)
259 /* Skip if the thread stratum has already been deactivated. */
260 if (!bsd_uthread_active)
263 unpush_target (&bsd_uthread_ops);
267 bsd_uthread_inferior_created (struct target_ops *ops, int from_tty)
269 bsd_uthread_activate (NULL);
272 /* Likely candidates for the threads library. */
273 static const char *bsd_uthread_solib_names[] =
275 "/usr/lib/libc_r.so", /* FreeBSD */
276 "/usr/lib/libpthread.so", /* OpenBSD */
281 bsd_uthread_solib_loaded (struct so_list *so)
283 const char **names = bsd_uthread_solib_names;
285 for (names = bsd_uthread_solib_names; *names; names++)
287 if (startswith (so->so_original_name, *names))
289 solib_read_symbols (so, 0);
291 if (bsd_uthread_activate (so->objfile))
293 bsd_uthread_solib_name = so->so_original_name;
301 bsd_uthread_solib_unloaded (struct so_list *so)
303 if (!bsd_uthread_solib_name)
306 if (strcmp (so->so_original_name, bsd_uthread_solib_name) == 0)
307 bsd_uthread_deactivate ();
311 bsd_uthread_target::mourn_inferior ()
313 beneath ()->mourn_inferior ();
314 bsd_uthread_deactivate ();
318 bsd_uthread_target::fetch_registers (struct regcache *regcache, int regnum)
320 struct gdbarch *gdbarch = regcache->arch ();
321 struct bsd_uthread_ops *uthread_ops
322 = (struct bsd_uthread_ops *) gdbarch_data (gdbarch, bsd_uthread_data);
323 ptid_t ptid = regcache->ptid ();
324 CORE_ADDR addr = ptid_get_tid (ptid);
325 CORE_ADDR active_addr;
326 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
328 /* We are doing operations (e.g. reading memory) that rely on
330 inferior_ptid = ptid;
332 /* Always fetch the appropriate registers from the layer beneath. */
333 beneath ()->fetch_registers (regcache, regnum);
335 /* FIXME: That might have gotten us more than we asked for. Make
336 sure we overwrite all relevant registers with values from the
337 thread structure. This can go once we fix the underlying target. */
340 active_addr = bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr);
341 if (addr != 0 && addr != active_addr)
343 bsd_uthread_check_magic (addr);
344 uthread_ops->supply_uthread (regcache, regnum,
345 addr + bsd_uthread_thread_ctx_offset);
350 bsd_uthread_target::store_registers (struct regcache *regcache, int regnum)
352 struct gdbarch *gdbarch = regcache->arch ();
353 struct bsd_uthread_ops *uthread_ops
354 = (struct bsd_uthread_ops *) gdbarch_data (gdbarch, bsd_uthread_data);
355 ptid_t ptid = regcache->ptid ();
356 CORE_ADDR addr = ptid_get_tid (ptid);
357 CORE_ADDR active_addr;
358 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
360 /* We are doing operations (e.g. reading memory) that rely on
362 inferior_ptid = ptid;
364 active_addr = bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr);
365 if (addr != 0 && addr != active_addr)
367 bsd_uthread_check_magic (addr);
368 uthread_ops->collect_uthread (regcache, regnum,
369 addr + bsd_uthread_thread_ctx_offset);
373 /* Updating the thread that is currently running; pass the
374 request to the layer beneath. */
375 beneath ()->store_registers (regcache, regnum);
380 bsd_uthread_target::wait (ptid_t ptid, struct target_waitstatus *status,
383 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
386 /* Pass the request to the layer beneath. */
387 ptid = beneath ()->wait (ptid, status, options);
389 /* If the process is no longer alive, there's no point in figuring
390 out the thread ID. It will fail anyway. */
391 if (status->kind == TARGET_WAITKIND_SIGNALLED
392 || status->kind == TARGET_WAITKIND_EXITED)
395 /* Fetch the corresponding thread ID, and augment the returned
396 process ID with it. */
397 addr = bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr);
402 /* FIXME: For executables linked statically with the threads
403 library, we end up here before the program has actually been
404 executed. In that case ADDR will be garbage since it has
405 been read from the wrong virtual memory image. */
406 if (target_read_memory (addr, buf, 4) == 0)
408 ULONGEST magic = extract_unsigned_integer (buf, 4, byte_order);
409 if (magic == BSD_UTHREAD_PTHREAD_MAGIC)
410 ptid = ptid_t (ptid.pid (), 0, addr);
414 /* If INFERIOR_PTID doesn't have a tid member yet, and we now have a
415 ptid with tid set, then ptid is still the initial thread of
416 the process. Notify GDB core about it. */
417 if (ptid_get_tid (inferior_ptid) == 0
418 && ptid_get_tid (ptid) != 0 && !in_thread_list (ptid))
419 thread_change_ptid (inferior_ptid, ptid);
421 /* Don't let the core see a ptid without a corresponding thread. */
422 thread_info *thread = find_thread_ptid (ptid);
423 if (thread == NULL || thread->state == THREAD_EXITED)
430 bsd_uthread_target::resume (ptid_t ptid, int step, enum gdb_signal sig)
432 /* Pass the request to the layer beneath. */
433 beneath ()->resume (ptid, step, sig);
437 bsd_uthread_target::thread_alive (ptid_t ptid)
439 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
440 CORE_ADDR addr = ptid_get_tid (ptid);
444 int offset = bsd_uthread_thread_state_offset;
447 bsd_uthread_check_magic (addr);
449 state = read_memory_unsigned_integer (addr + offset, 4, byte_order);
450 if (state == BSD_UTHREAD_PS_DEAD)
454 return beneath ()->thread_alive (ptid);
458 bsd_uthread_target::update_thread_list ()
460 pid_t pid = inferior_ptid.pid ();
461 int offset = bsd_uthread_thread_next_offset;
466 addr = bsd_uthread_read_memory_address (bsd_uthread_thread_list_addr);
469 ptid_t ptid = ptid_t (pid, 0, addr);
471 thread_info *thread = find_thread_ptid (ptid);
472 if (thread == nullptr || thread->state == THREAD_EXITED)
474 /* If INFERIOR_PTID doesn't have a tid member yet, then ptid
475 is still the initial thread of the process. Notify GDB
477 if (ptid_get_tid (inferior_ptid) == 0)
478 thread_change_ptid (inferior_ptid, ptid);
483 addr = bsd_uthread_read_memory_address (addr + offset);
487 /* Possible states a thread can be in. */
488 static const char *bsd_uthread_state[] =
512 /* Return a string describing th state of the thread specified by
516 bsd_uthread_target::extra_thread_info (thread_info *info)
518 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
519 CORE_ADDR addr = ptid_get_tid (info->ptid);
523 int offset = bsd_uthread_thread_state_offset;
526 state = read_memory_unsigned_integer (addr + offset, 4, byte_order);
527 if (state < ARRAY_SIZE (bsd_uthread_state))
528 return bsd_uthread_state[state];
535 bsd_uthread_target::pid_to_str (ptid_t ptid)
537 if (ptid_get_tid (ptid) != 0)
541 xsnprintf (buf, sizeof buf, "process %d, thread 0x%lx",
542 ptid.pid (), ptid_get_tid (ptid));
546 return normal_pid_to_str (ptid);
550 _initialize_bsd_uthread (void)
552 bsd_uthread_data = gdbarch_data_register_pre_init (bsd_uthread_init);
554 gdb::observers::inferior_created.attach (bsd_uthread_inferior_created);
555 gdb::observers::solib_loaded.attach (bsd_uthread_solib_loaded);
556 gdb::observers::solib_unloaded.attach (bsd_uthread_solib_unloaded);