Regenerate AArch64 opcodes files
[binutils-gdb.git] / gdb / bsd-uthread.c
blob10e2ca5939e7a1b353ecd82528d108e11a743969
1 /* BSD user-level threads support.
3 Copyright (C) 2005-2024 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/>. */
20 #include "defs.h"
21 #include "gdbcore.h"
22 #include "gdbthread.h"
23 #include "inferior.h"
24 #include "objfiles.h"
25 #include "observable.h"
26 #include "regcache.h"
27 #include "solib.h"
28 #include "solist.h"
29 #include "symfile.h"
30 #include "target.h"
32 #include "gdbsupport/gdb_obstack.h"
34 #include "bsd-uthread.h"
36 static const target_info bsd_uthread_target_info = {
37 "bsd-uthreads",
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 *, target_wait_flags) 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 struct bsd_uthread_ops
75 /* Supply registers for an inactive thread to a register cache. */
76 void (*supply_uthread)(struct regcache *, int, CORE_ADDR) = nullptr;
78 /* Collect registers for an inactive thread from a register cache. */
79 void (*collect_uthread)(const struct regcache *, int, CORE_ADDR) = nullptr;
82 /* Per-architecture data key. */
83 static const registry<gdbarch>::key<struct bsd_uthread_ops> bsd_uthread_data;
85 static struct bsd_uthread_ops *
86 get_bsd_uthread (struct gdbarch *gdbarch)
88 struct bsd_uthread_ops *ops = bsd_uthread_data.get (gdbarch);
89 if (ops == nullptr)
90 ops = bsd_uthread_data.emplace (gdbarch);
91 return ops;
94 /* Set the function that supplies registers from an inactive thread
95 for architecture GDBARCH to SUPPLY_UTHREAD. */
97 void
98 bsd_uthread_set_supply_uthread (struct gdbarch *gdbarch,
99 void (*supply_uthread) (struct regcache *,
100 int, CORE_ADDR))
102 struct bsd_uthread_ops *ops = get_bsd_uthread (gdbarch);
104 ops->supply_uthread = supply_uthread;
107 /* Set the function that collects registers for an inactive thread for
108 architecture GDBARCH to SUPPLY_UTHREAD. */
110 void
111 bsd_uthread_set_collect_uthread (struct gdbarch *gdbarch,
112 void (*collect_uthread) (const struct regcache *,
113 int, CORE_ADDR))
115 struct bsd_uthread_ops *ops = get_bsd_uthread (gdbarch);
117 ops->collect_uthread = collect_uthread;
120 /* Magic number to help recognize a valid thread structure. */
121 #define BSD_UTHREAD_PTHREAD_MAGIC 0xd09ba115
123 /* Check whether the thread structure at ADDR is valid. */
125 static void
126 bsd_uthread_check_magic (CORE_ADDR addr)
128 bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
129 ULONGEST magic = read_memory_unsigned_integer (addr, 4, byte_order);
131 if (magic != BSD_UTHREAD_PTHREAD_MAGIC)
132 error (_("Bad magic"));
135 /* Thread states. */
136 #define BSD_UTHREAD_PS_RUNNING 0
137 #define BSD_UTHREAD_PS_DEAD 18
139 /* Address of the pointer to the thread structure for the running
140 thread. */
141 static CORE_ADDR bsd_uthread_thread_run_addr;
143 /* Address of the list of all threads. */
144 static CORE_ADDR bsd_uthread_thread_list_addr;
146 /* Offsets of various "interesting" bits in the thread structure. */
147 static int bsd_uthread_thread_state_offset = -1;
148 static int bsd_uthread_thread_next_offset = -1;
149 static int bsd_uthread_thread_ctx_offset;
151 /* Name of shared threads library. */
152 static std::string bsd_uthread_solib_name;
154 /* Non-zero if the thread stratum implemented by this module is active. */
155 static int bsd_uthread_active;
157 static CORE_ADDR
158 bsd_uthread_lookup_address (const char *name, struct objfile *objfile)
160 struct bound_minimal_symbol sym;
162 sym = lookup_minimal_symbol (name, NULL, objfile);
163 if (sym.minsym)
164 return sym.value_address ();
166 return 0;
169 static int
170 bsd_uthread_lookup_offset (const char *name, struct objfile *objfile)
172 bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
173 CORE_ADDR addr;
175 addr = bsd_uthread_lookup_address (name, objfile);
176 if (addr == 0)
177 return 0;
179 return read_memory_unsigned_integer (addr, 4, byte_order);
182 static CORE_ADDR
183 bsd_uthread_read_memory_address (CORE_ADDR addr)
185 type *ptr_type
186 = builtin_type (current_inferior ()->arch ())->builtin_data_ptr;
187 return read_memory_typed_address (addr, ptr_type);
190 /* If OBJFILE contains the symbols corresponding to one of the
191 supported user-level threads libraries, activate the thread stratum
192 implemented by this module. */
194 static int
195 bsd_uthread_activate (struct objfile *objfile)
197 gdbarch *gdbarch = current_inferior ()->arch ();
198 struct bsd_uthread_ops *ops = get_bsd_uthread (gdbarch);
200 /* Skip if the thread stratum has already been activated. */
201 if (bsd_uthread_active)
202 return 0;
204 /* There's no point in enabling this module if no
205 architecture-specific operations are provided. */
206 if (!ops->supply_uthread)
207 return 0;
209 bsd_uthread_thread_run_addr =
210 bsd_uthread_lookup_address ("_thread_run", objfile);
211 if (bsd_uthread_thread_run_addr == 0)
212 return 0;
214 bsd_uthread_thread_list_addr =
215 bsd_uthread_lookup_address ("_thread_list", objfile);
216 if (bsd_uthread_thread_list_addr == 0)
217 return 0;
219 bsd_uthread_thread_state_offset =
220 bsd_uthread_lookup_offset ("_thread_state_offset", objfile);
221 if (bsd_uthread_thread_state_offset == 0)
222 return 0;
224 bsd_uthread_thread_next_offset =
225 bsd_uthread_lookup_offset ("_thread_next_offset", objfile);
226 if (bsd_uthread_thread_next_offset == 0)
227 return 0;
229 bsd_uthread_thread_ctx_offset =
230 bsd_uthread_lookup_offset ("_thread_ctx_offset", objfile);
232 current_inferior ()->push_target (&bsd_uthread_ops);
233 bsd_uthread_active = 1;
234 return 1;
237 /* Cleanup due to deactivation. */
239 void
240 bsd_uthread_target::close ()
242 bsd_uthread_active = 0;
243 bsd_uthread_thread_run_addr = 0;
244 bsd_uthread_thread_list_addr = 0;
245 bsd_uthread_thread_state_offset = 0;
246 bsd_uthread_thread_next_offset = 0;
247 bsd_uthread_thread_ctx_offset = 0;
248 bsd_uthread_solib_name.clear ();
251 /* Deactivate the thread stratum implemented by this module. */
253 static void
254 bsd_uthread_deactivate (void)
256 /* Skip if the thread stratum has already been deactivated. */
257 if (!bsd_uthread_active)
258 return;
260 current_inferior ()->unpush_target (&bsd_uthread_ops);
263 static void
264 bsd_uthread_inferior_created (inferior *inf)
266 bsd_uthread_activate (NULL);
269 /* Likely candidates for the threads library. */
270 static const char * const bsd_uthread_solib_names[] =
272 "/usr/lib/libc_r.so", /* FreeBSD */
273 "/usr/lib/libpthread.so", /* OpenBSD */
274 NULL
277 static void
278 bsd_uthread_solib_loaded (solib &so)
280 const char * const *names = bsd_uthread_solib_names;
282 for (names = bsd_uthread_solib_names; *names; names++)
284 if (startswith (so.so_original_name, *names))
286 solib_read_symbols (so, 0);
288 if (bsd_uthread_activate (so.objfile))
290 bsd_uthread_solib_name = so.so_original_name;
291 return;
297 static void
298 bsd_uthread_solib_unloaded (program_space *pspace, const solib &so)
300 if (bsd_uthread_solib_name.empty ())
301 return;
303 if (so.so_original_name == bsd_uthread_solib_name)
304 bsd_uthread_deactivate ();
307 void
308 bsd_uthread_target::mourn_inferior ()
310 beneath ()->mourn_inferior ();
311 bsd_uthread_deactivate ();
314 void
315 bsd_uthread_target::fetch_registers (struct regcache *regcache, int regnum)
317 struct gdbarch *gdbarch = regcache->arch ();
318 struct bsd_uthread_ops *uthread_ops = get_bsd_uthread (gdbarch);
319 ptid_t ptid = regcache->ptid ();
320 CORE_ADDR addr = ptid.tid ();
321 CORE_ADDR active_addr;
322 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
324 /* We are doing operations (e.g. reading memory) that rely on
325 inferior_ptid. */
326 inferior_ptid = ptid;
328 /* Always fetch the appropriate registers from the layer beneath. */
329 beneath ()->fetch_registers (regcache, regnum);
331 /* FIXME: That might have gotten us more than we asked for. Make
332 sure we overwrite all relevant registers with values from the
333 thread structure. This can go once we fix the underlying target. */
334 regnum = -1;
336 active_addr = bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr);
337 if (addr != 0 && addr != active_addr)
339 bsd_uthread_check_magic (addr);
340 uthread_ops->supply_uthread (regcache, regnum,
341 addr + bsd_uthread_thread_ctx_offset);
345 void
346 bsd_uthread_target::store_registers (struct regcache *regcache, int regnum)
348 struct gdbarch *gdbarch = regcache->arch ();
349 struct bsd_uthread_ops *uthread_ops = get_bsd_uthread (gdbarch);
350 ptid_t ptid = regcache->ptid ();
351 CORE_ADDR addr = ptid.tid ();
352 CORE_ADDR active_addr;
353 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
355 /* We are doing operations (e.g. reading memory) that rely on
356 inferior_ptid. */
357 inferior_ptid = ptid;
359 active_addr = bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr);
360 if (addr != 0 && addr != active_addr)
362 bsd_uthread_check_magic (addr);
363 uthread_ops->collect_uthread (regcache, regnum,
364 addr + bsd_uthread_thread_ctx_offset);
366 else
368 /* Updating the thread that is currently running; pass the
369 request to the layer beneath. */
370 beneath ()->store_registers (regcache, regnum);
374 ptid_t
375 bsd_uthread_target::wait (ptid_t ptid, struct target_waitstatus *status,
376 target_wait_flags options)
378 bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
379 CORE_ADDR addr;
380 process_stratum_target *beneath
381 = as_process_stratum_target (this->beneath ());
383 /* Pass the request to the layer beneath. */
384 ptid = beneath->wait (ptid, status, options);
386 /* If the process is no longer alive, there's no point in figuring
387 out the thread ID. It will fail anyway. */
388 if (status->kind () == TARGET_WAITKIND_SIGNALLED
389 || status->kind () == TARGET_WAITKIND_EXITED)
390 return ptid;
392 /* Fetch the corresponding thread ID, and augment the returned
393 process ID with it. */
394 addr = bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr);
395 if (addr != 0)
397 gdb_byte buf[4];
399 /* FIXME: For executables linked statically with the threads
400 library, we end up here before the program has actually been
401 executed. In that case ADDR will be garbage since it has
402 been read from the wrong virtual memory image. */
403 if (target_read_memory (addr, buf, 4) == 0)
405 ULONGEST magic = extract_unsigned_integer (buf, 4, byte_order);
406 if (magic == BSD_UTHREAD_PTHREAD_MAGIC)
407 ptid = ptid_t (ptid.pid (), 0, addr);
411 /* If INFERIOR_PTID doesn't have a tid member yet, and we now have a
412 ptid with tid set, then ptid is still the initial thread of
413 the process. Notify GDB core about it. */
414 if (inferior_ptid.tid () == 0
415 && ptid.tid () != 0 && !in_thread_list (beneath, ptid))
416 thread_change_ptid (beneath, inferior_ptid, ptid);
418 /* Don't let the core see a ptid without a corresponding thread. */
419 thread_info *thread = beneath->find_thread (ptid);
420 if (thread == NULL || thread->state == THREAD_EXITED)
421 add_thread (beneath, ptid);
423 return ptid;
426 void
427 bsd_uthread_target::resume (ptid_t ptid, int step, enum gdb_signal sig)
429 /* Pass the request to the layer beneath. */
430 beneath ()->resume (ptid, step, sig);
433 bool
434 bsd_uthread_target::thread_alive (ptid_t ptid)
436 bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
437 CORE_ADDR addr = ptid.tid ();
439 if (addr != 0)
441 int offset = bsd_uthread_thread_state_offset;
442 ULONGEST state;
444 bsd_uthread_check_magic (addr);
446 state = read_memory_unsigned_integer (addr + offset, 4, byte_order);
447 if (state == BSD_UTHREAD_PS_DEAD)
448 return false;
451 return beneath ()->thread_alive (ptid);
454 void
455 bsd_uthread_target::update_thread_list ()
457 pid_t pid = inferior_ptid.pid ();
458 int offset = bsd_uthread_thread_next_offset;
459 CORE_ADDR addr;
461 prune_threads ();
463 addr = bsd_uthread_read_memory_address (bsd_uthread_thread_list_addr);
464 while (addr != 0)
466 ptid_t ptid = ptid_t (pid, 0, addr);
468 process_stratum_target *proc_target
469 = as_process_stratum_target (this->beneath ());
470 thread_info *thread = proc_target->find_thread (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
475 core about it. */
476 if (inferior_ptid.tid () == 0)
477 thread_change_ptid (proc_target, inferior_ptid, ptid);
478 else
479 add_thread (proc_target, ptid);
482 addr = bsd_uthread_read_memory_address (addr + offset);
486 /* Possible states a thread can be in. */
487 static const char * const bsd_uthread_state[] =
489 "RUNNING",
490 "SIGTHREAD",
491 "MUTEX_WAIT",
492 "COND_WAIT",
493 "FDLR_WAIT",
494 "FDLW_WAIT",
495 "FDR_WAIT",
496 "FDW_WAIT",
497 "FILE_WAIT",
498 "POLL_WAIT",
499 "SELECT_WAIT",
500 "SLEEP_WAIT",
501 "WAIT_WAIT",
502 "SIGSUSPEND",
503 "SIGWAIT",
504 "SPINBLOCK",
505 "JOIN",
506 "SUSPENDED",
507 "DEAD",
508 "DEADLOCK"
511 /* Return a string describing th state of the thread specified by
512 INFO. */
514 const char *
515 bsd_uthread_target::extra_thread_info (thread_info *info)
517 bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
518 CORE_ADDR addr = info->ptid.tid ();
520 if (addr != 0)
522 int offset = bsd_uthread_thread_state_offset;
523 ULONGEST state;
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];
530 return NULL;
533 std::string
534 bsd_uthread_target::pid_to_str (ptid_t ptid)
536 if (ptid.tid () != 0)
537 return string_printf ("process %d, thread 0x%s",
538 ptid.pid (),
539 phex_nz (ptid.tid (), sizeof (ULONGEST)));
541 return normal_pid_to_str (ptid);
544 void _initialize_bsd_uthread ();
545 void
546 _initialize_bsd_uthread ()
548 gdb::observers::inferior_created.attach (bsd_uthread_inferior_created,
549 "bsd-uthread");
550 gdb::observers::solib_loaded.attach (bsd_uthread_solib_loaded,
551 "bsd-uthread");
552 gdb::observers::solib_unloaded.attach (bsd_uthread_solib_unloaded,
553 "bsd-uthread");