1 /* BSD user-level threads support.
3 Copyright (C) 2005-2019 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
44 const target_info &info () const override
45 { return bsd_uthread_target_info; }
47 strata stratum () const override { return thread_stratum; }
49 void close () override;
51 void mourn_inferior () override;
53 void fetch_registers (struct regcache *, int) override;
54 void store_registers (struct regcache *, int) override;
56 ptid_t wait (ptid_t, struct target_waitstatus *, int) override;
57 void resume (ptid_t, int, enum gdb_signal) override;
59 bool thread_alive (ptid_t ptid) override;
61 void update_thread_list () override;
63 const char *extra_thread_info (struct thread_info *) override;
65 std::string pid_to_str (ptid_t) override;
68 static bsd_uthread_target bsd_uthread_ops;
71 /* Architecture-specific operations. */
73 /* Per-architecture data key. */
74 static struct gdbarch_data *bsd_uthread_data;
76 struct bsd_uthread_ops
78 /* Supply registers for an inactive thread to a register cache. */
79 void (*supply_uthread)(struct regcache *, int, CORE_ADDR);
81 /* Collect registers for an inactive thread from a register cache. */
82 void (*collect_uthread)(const struct regcache *, int, CORE_ADDR);
86 bsd_uthread_init (struct obstack *obstack)
88 struct bsd_uthread_ops *ops;
90 ops = OBSTACK_ZALLOC (obstack, struct bsd_uthread_ops);
94 /* Set the function that supplies registers from an inactive thread
95 for architecture GDBARCH to SUPPLY_UTHREAD. */
98 bsd_uthread_set_supply_uthread (struct gdbarch *gdbarch,
99 void (*supply_uthread) (struct regcache *,
102 struct bsd_uthread_ops *ops
103 = (struct bsd_uthread_ops *) gdbarch_data (gdbarch, bsd_uthread_data);
105 ops->supply_uthread = supply_uthread;
108 /* Set the function that collects registers for an inactive thread for
109 architecture GDBARCH to SUPPLY_UTHREAD. */
112 bsd_uthread_set_collect_uthread (struct gdbarch *gdbarch,
113 void (*collect_uthread) (const struct regcache *,
116 struct bsd_uthread_ops *ops
117 = (struct bsd_uthread_ops *) gdbarch_data (gdbarch, bsd_uthread_data);
119 ops->collect_uthread = collect_uthread;
122 /* Magic number to help recognize a valid thread structure. */
123 #define BSD_UTHREAD_PTHREAD_MAGIC 0xd09ba115
125 /* Check whether the thread structure at ADDR is valid. */
128 bsd_uthread_check_magic (CORE_ADDR addr)
130 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
131 ULONGEST magic = read_memory_unsigned_integer (addr, 4, byte_order);
133 if (magic != BSD_UTHREAD_PTHREAD_MAGIC)
134 error (_("Bad magic"));
138 #define BSD_UTHREAD_PS_RUNNING 0
139 #define BSD_UTHREAD_PS_DEAD 18
141 /* Address of the pointer to the thread structure for the running
143 static CORE_ADDR bsd_uthread_thread_run_addr;
145 /* Address of the list of all threads. */
146 static CORE_ADDR bsd_uthread_thread_list_addr;
148 /* Offsets of various "interesting" bits in the thread structure. */
149 static int bsd_uthread_thread_state_offset = -1;
150 static int bsd_uthread_thread_next_offset = -1;
151 static int bsd_uthread_thread_ctx_offset;
153 /* Name of shared threads library. */
154 static const char *bsd_uthread_solib_name;
156 /* Non-zero if the thread startum implemented by this module is active. */
157 static int bsd_uthread_active;
160 bsd_uthread_lookup_address (const char *name, struct objfile *objfile)
162 struct bound_minimal_symbol sym;
164 sym = lookup_minimal_symbol (name, NULL, objfile);
166 return BMSYMBOL_VALUE_ADDRESS (sym);
172 bsd_uthread_lookup_offset (const char *name, struct objfile *objfile)
174 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
177 addr = bsd_uthread_lookup_address (name, objfile);
181 return read_memory_unsigned_integer (addr, 4, byte_order);
185 bsd_uthread_read_memory_address (CORE_ADDR addr)
187 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
188 return read_memory_typed_address (addr, ptr_type);
191 /* If OBJFILE contains the symbols corresponding to one of the
192 supported user-level threads libraries, activate the thread stratum
193 implemented by this module. */
196 bsd_uthread_activate (struct objfile *objfile)
198 struct gdbarch *gdbarch = target_gdbarch ();
199 struct bsd_uthread_ops *ops
200 = (struct bsd_uthread_ops *) gdbarch_data (gdbarch, bsd_uthread_data);
202 /* Skip if the thread stratum has already been activated. */
203 if (bsd_uthread_active)
206 /* There's no point in enabling this module if no
207 architecture-specific operations are provided. */
208 if (!ops->supply_uthread)
211 bsd_uthread_thread_run_addr =
212 bsd_uthread_lookup_address ("_thread_run", objfile);
213 if (bsd_uthread_thread_run_addr == 0)
216 bsd_uthread_thread_list_addr =
217 bsd_uthread_lookup_address ("_thread_list", objfile);
218 if (bsd_uthread_thread_list_addr == 0)
221 bsd_uthread_thread_state_offset =
222 bsd_uthread_lookup_offset ("_thread_state_offset", objfile);
223 if (bsd_uthread_thread_state_offset == 0)
226 bsd_uthread_thread_next_offset =
227 bsd_uthread_lookup_offset ("_thread_next_offset", objfile);
228 if (bsd_uthread_thread_next_offset == 0)
231 bsd_uthread_thread_ctx_offset =
232 bsd_uthread_lookup_offset ("_thread_ctx_offset", objfile);
234 push_target (&bsd_uthread_ops);
235 bsd_uthread_active = 1;
239 /* Cleanup due to deactivation. */
242 bsd_uthread_target::close ()
244 bsd_uthread_active = 0;
245 bsd_uthread_thread_run_addr = 0;
246 bsd_uthread_thread_list_addr = 0;
247 bsd_uthread_thread_state_offset = 0;
248 bsd_uthread_thread_next_offset = 0;
249 bsd_uthread_thread_ctx_offset = 0;
250 bsd_uthread_solib_name = NULL;
253 /* Deactivate the thread stratum implemented by this module. */
256 bsd_uthread_deactivate (void)
258 /* Skip if the thread stratum has already been deactivated. */
259 if (!bsd_uthread_active)
262 unpush_target (&bsd_uthread_ops);
266 bsd_uthread_inferior_created (struct target_ops *ops, int from_tty)
268 bsd_uthread_activate (NULL);
271 /* Likely candidates for the threads library. */
272 static const char *bsd_uthread_solib_names[] =
274 "/usr/lib/libc_r.so", /* FreeBSD */
275 "/usr/lib/libpthread.so", /* OpenBSD */
280 bsd_uthread_solib_loaded (struct so_list *so)
282 const char **names = bsd_uthread_solib_names;
284 for (names = bsd_uthread_solib_names; *names; names++)
286 if (startswith (so->so_original_name, *names))
288 solib_read_symbols (so, 0);
290 if (bsd_uthread_activate (so->objfile))
292 bsd_uthread_solib_name = so->so_original_name;
300 bsd_uthread_solib_unloaded (struct so_list *so)
302 if (!bsd_uthread_solib_name)
305 if (strcmp (so->so_original_name, bsd_uthread_solib_name) == 0)
306 bsd_uthread_deactivate ();
310 bsd_uthread_target::mourn_inferior ()
312 beneath ()->mourn_inferior ();
313 bsd_uthread_deactivate ();
317 bsd_uthread_target::fetch_registers (struct regcache *regcache, int regnum)
319 struct gdbarch *gdbarch = regcache->arch ();
320 struct bsd_uthread_ops *uthread_ops
321 = (struct bsd_uthread_ops *) gdbarch_data (gdbarch, bsd_uthread_data);
322 ptid_t ptid = regcache->ptid ();
323 CORE_ADDR addr = ptid.tid ();
324 CORE_ADDR active_addr;
325 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
327 /* We are doing operations (e.g. reading memory) that rely on
329 inferior_ptid = ptid;
331 /* Always fetch the appropriate registers from the layer beneath. */
332 beneath ()->fetch_registers (regcache, regnum);
334 /* FIXME: That might have gotten us more than we asked for. Make
335 sure we overwrite all relevant registers with values from the
336 thread structure. This can go once we fix the underlying target. */
339 active_addr = bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr);
340 if (addr != 0 && addr != active_addr)
342 bsd_uthread_check_magic (addr);
343 uthread_ops->supply_uthread (regcache, regnum,
344 addr + bsd_uthread_thread_ctx_offset);
349 bsd_uthread_target::store_registers (struct regcache *regcache, int regnum)
351 struct gdbarch *gdbarch = regcache->arch ();
352 struct bsd_uthread_ops *uthread_ops
353 = (struct bsd_uthread_ops *) gdbarch_data (gdbarch, bsd_uthread_data);
354 ptid_t ptid = regcache->ptid ();
355 CORE_ADDR addr = ptid.tid ();
356 CORE_ADDR active_addr;
357 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
359 /* We are doing operations (e.g. reading memory) that rely on
361 inferior_ptid = ptid;
363 active_addr = bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr);
364 if (addr != 0 && addr != active_addr)
366 bsd_uthread_check_magic (addr);
367 uthread_ops->collect_uthread (regcache, regnum,
368 addr + bsd_uthread_thread_ctx_offset);
372 /* Updating the thread that is currently running; pass the
373 request to the layer beneath. */
374 beneath ()->store_registers (regcache, regnum);
379 bsd_uthread_target::wait (ptid_t ptid, struct target_waitstatus *status,
382 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
385 /* Pass the request to the layer beneath. */
386 ptid = beneath ()->wait (ptid, status, options);
388 /* If the process is no longer alive, there's no point in figuring
389 out the thread ID. It will fail anyway. */
390 if (status->kind == TARGET_WAITKIND_SIGNALLED
391 || status->kind == TARGET_WAITKIND_EXITED)
394 /* Fetch the corresponding thread ID, and augment the returned
395 process ID with it. */
396 addr = bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr);
401 /* FIXME: For executables linked statically with the threads
402 library, we end up here before the program has actually been
403 executed. In that case ADDR will be garbage since it has
404 been read from the wrong virtual memory image. */
405 if (target_read_memory (addr, buf, 4) == 0)
407 ULONGEST magic = extract_unsigned_integer (buf, 4, byte_order);
408 if (magic == BSD_UTHREAD_PTHREAD_MAGIC)
409 ptid = ptid_t (ptid.pid (), 0, addr);
413 /* If INFERIOR_PTID doesn't have a tid member yet, and we now have a
414 ptid with tid set, then ptid is still the initial thread of
415 the process. Notify GDB core about it. */
416 if (inferior_ptid.tid () == 0
417 && ptid.tid () != 0 && !in_thread_list (ptid))
418 thread_change_ptid (inferior_ptid, ptid);
420 /* Don't let the core see a ptid without a corresponding thread. */
421 thread_info *thread = find_thread_ptid (ptid);
422 if (thread == NULL || thread->state == THREAD_EXITED)
429 bsd_uthread_target::resume (ptid_t ptid, int step, enum gdb_signal sig)
431 /* Pass the request to the layer beneath. */
432 beneath ()->resume (ptid, step, sig);
436 bsd_uthread_target::thread_alive (ptid_t ptid)
438 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
439 CORE_ADDR addr = ptid.tid ();
443 int offset = bsd_uthread_thread_state_offset;
446 bsd_uthread_check_magic (addr);
448 state = read_memory_unsigned_integer (addr + offset, 4, byte_order);
449 if (state == BSD_UTHREAD_PS_DEAD)
453 return beneath ()->thread_alive (ptid);
457 bsd_uthread_target::update_thread_list ()
459 pid_t pid = inferior_ptid.pid ();
460 int offset = bsd_uthread_thread_next_offset;
465 addr = bsd_uthread_read_memory_address (bsd_uthread_thread_list_addr);
468 ptid_t ptid = ptid_t (pid, 0, addr);
470 thread_info *thread = find_thread_ptid (ptid);
471 if (thread == nullptr || thread->state == THREAD_EXITED)
473 /* If INFERIOR_PTID doesn't have a tid member yet, then ptid
474 is still the initial thread of the process. Notify GDB
476 if (inferior_ptid.tid () == 0)
477 thread_change_ptid (inferior_ptid, ptid);
482 addr = bsd_uthread_read_memory_address (addr + offset);
486 /* Possible states a thread can be in. */
487 static const char *bsd_uthread_state[] =
511 /* Return a string describing th state of the thread specified by
515 bsd_uthread_target::extra_thread_info (thread_info *info)
517 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
518 CORE_ADDR addr = info->ptid.tid ();
522 int offset = bsd_uthread_thread_state_offset;
525 state = read_memory_unsigned_integer (addr + offset, 4, byte_order);
526 if (state < ARRAY_SIZE (bsd_uthread_state))
527 return bsd_uthread_state[state];
534 bsd_uthread_target::pid_to_str (ptid_t ptid)
536 if (ptid.tid () != 0)
537 return string_printf ("process %d, thread 0x%lx",
538 ptid.pid (), ptid.tid ());
540 return normal_pid_to_str (ptid);
544 _initialize_bsd_uthread (void)
546 bsd_uthread_data = gdbarch_data_register_pre_init (bsd_uthread_init);
548 gdb::observers::inferior_created.attach (bsd_uthread_inferior_created);
549 gdb::observers::solib_loaded.attach (bsd_uthread_solib_loaded);
550 gdb::observers::solib_unloaded.attach (bsd_uthread_solib_unloaded);