]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/bsd-uthread.c
Automatic date update in version.in
[thirdparty/binutils-gdb.git] / gdb / bsd-uthread.c
CommitLineData
82f5c14f
MK
1/* BSD user-level threads support.
2
d01e8234 3 Copyright (C) 2005-2025 Free Software Foundation, Inc.
82f5c14f
MK
4
5 This file is part of GDB.
6
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
82f5c14f
MK
10 (at your option) any later version.
11
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.
16
17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
82f5c14f 19
ec452525 20#include "extract-store-integer.h"
82f5c14f
MK
21#include "gdbcore.h"
22#include "gdbthread.h"
23#include "inferior.h"
24#include "objfiles.h"
76727919 25#include "observable.h"
82f5c14f 26#include "regcache.h"
5ebc08b0 27#include "solib.h"
82f5c14f
MK
28#include "symfile.h"
29#include "target.h"
30
bf31fd38 31#include "gdbsupport/gdb_obstack.h"
4de283e4
TT
32
33#include "bsd-uthread.h"
34
d9f719f1
PA
35static const target_info bsd_uthread_target_info = {
36 "bsd-uthreads",
37 N_("BSD user-level threads"),
38 N_("BSD user-level threads")
39};
40
f6ac5f3d
PA
41struct bsd_uthread_target final : public target_ops
42{
d9f719f1
PA
43 const target_info &info () const override
44 { return bsd_uthread_target_info; }
f6ac5f3d 45
66b4deae
PA
46 strata stratum () const override { return thread_stratum; }
47
f6ac5f3d
PA
48 void close () override;
49
50 void mourn_inferior () override;
51
52 void fetch_registers (struct regcache *, int) override;
53 void store_registers (struct regcache *, int) override;
54
b60cea74 55 ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override;
f6ac5f3d
PA
56 void resume (ptid_t, int, enum gdb_signal) override;
57
57810aa7 58 bool thread_alive (ptid_t ptid) override;
f6ac5f3d
PA
59
60 void update_thread_list () override;
61
62 const char *extra_thread_info (struct thread_info *) override;
63
a068643d 64 std::string pid_to_str (ptid_t) override;
f6ac5f3d
PA
65};
66
67static bsd_uthread_target bsd_uthread_ops;
82f5c14f
MK
68\f
69
70/* Architecture-specific operations. */
71
82f5c14f
MK
72struct bsd_uthread_ops
73{
74 /* Supply registers for an inactive thread to a register cache. */
cb275538 75 void (*supply_uthread)(struct regcache *, int, CORE_ADDR) = nullptr;
82f5c14f
MK
76
77 /* Collect registers for an inactive thread from a register cache. */
cb275538 78 void (*collect_uthread)(const struct regcache *, int, CORE_ADDR) = nullptr;
82f5c14f
MK
79};
80
cb275538
TT
81/* Per-architecture data key. */
82static const registry<gdbarch>::key<struct bsd_uthread_ops> bsd_uthread_data;
82f5c14f 83
cb275538
TT
84static struct bsd_uthread_ops *
85get_bsd_uthread (struct gdbarch *gdbarch)
86{
87 struct bsd_uthread_ops *ops = bsd_uthread_data.get (gdbarch);
88 if (ops == nullptr)
89 ops = bsd_uthread_data.emplace (gdbarch);
82f5c14f
MK
90 return ops;
91}
92
93/* Set the function that supplies registers from an inactive thread
94 for architecture GDBARCH to SUPPLY_UTHREAD. */
95
96void
97bsd_uthread_set_supply_uthread (struct gdbarch *gdbarch,
98 void (*supply_uthread) (struct regcache *,
99 int, CORE_ADDR))
100{
cb275538 101 struct bsd_uthread_ops *ops = get_bsd_uthread (gdbarch);
9a3c8263 102
82f5c14f
MK
103 ops->supply_uthread = supply_uthread;
104}
105
106/* Set the function that collects registers for an inactive thread for
107 architecture GDBARCH to SUPPLY_UTHREAD. */
108
109void
110bsd_uthread_set_collect_uthread (struct gdbarch *gdbarch,
111 void (*collect_uthread) (const struct regcache *,
112 int, CORE_ADDR))
113{
cb275538 114 struct bsd_uthread_ops *ops = get_bsd_uthread (gdbarch);
9a3c8263 115
82f5c14f
MK
116 ops->collect_uthread = collect_uthread;
117}
118
119/* Magic number to help recognize a valid thread structure. */
120#define BSD_UTHREAD_PTHREAD_MAGIC 0xd09ba115
121
122/* Check whether the thread structure at ADDR is valid. */
123
124static void
125bsd_uthread_check_magic (CORE_ADDR addr)
126{
99d9c3b9 127 bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
e17a4113 128 ULONGEST magic = read_memory_unsigned_integer (addr, 4, byte_order);
82f5c14f
MK
129
130 if (magic != BSD_UTHREAD_PTHREAD_MAGIC)
8a3fe4f8 131 error (_("Bad magic"));
82f5c14f
MK
132}
133
134/* Thread states. */
135#define BSD_UTHREAD_PS_RUNNING 0
136#define BSD_UTHREAD_PS_DEAD 18
137
b021a221 138/* Address of the pointer to the thread structure for the running
82f5c14f
MK
139 thread. */
140static CORE_ADDR bsd_uthread_thread_run_addr;
141
142/* Address of the list of all threads. */
143static CORE_ADDR bsd_uthread_thread_list_addr;
144
145/* Offsets of various "interesting" bits in the thread structure. */
146static int bsd_uthread_thread_state_offset = -1;
147static int bsd_uthread_thread_next_offset = -1;
148static int bsd_uthread_thread_ctx_offset;
149
150/* Name of shared threads library. */
98107b0b 151static std::string bsd_uthread_solib_name;
82f5c14f 152
33b5899f 153/* Non-zero if the thread stratum implemented by this module is active. */
82f5c14f
MK
154static int bsd_uthread_active;
155
156static CORE_ADDR
157bsd_uthread_lookup_address (const char *name, struct objfile *objfile)
158{
4144d36a
SM
159 bound_minimal_symbol sym
160 = lookup_minimal_symbol (current_program_space, name, objfile);
3b7344d5 161 if (sym.minsym)
4aeddc50 162 return sym.value_address ();
82f5c14f
MK
163
164 return 0;
165}
166
167static int
168bsd_uthread_lookup_offset (const char *name, struct objfile *objfile)
169{
99d9c3b9 170 bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
82f5c14f
MK
171 CORE_ADDR addr;
172
173 addr = bsd_uthread_lookup_address (name, objfile);
174 if (addr == 0)
175 return 0;
176
e17a4113 177 return read_memory_unsigned_integer (addr, 4, byte_order);
82f5c14f
MK
178}
179
ff7da468
UW
180static CORE_ADDR
181bsd_uthread_read_memory_address (CORE_ADDR addr)
182{
99d9c3b9
SM
183 type *ptr_type
184 = builtin_type (current_inferior ()->arch ())->builtin_data_ptr;
ff7da468
UW
185 return read_memory_typed_address (addr, ptr_type);
186}
187
82f5c14f
MK
188/* If OBJFILE contains the symbols corresponding to one of the
189 supported user-level threads libraries, activate the thread stratum
190 implemented by this module. */
191
192static int
193bsd_uthread_activate (struct objfile *objfile)
194{
99d9c3b9 195 gdbarch *gdbarch = current_inferior ()->arch ();
cb275538 196 struct bsd_uthread_ops *ops = get_bsd_uthread (gdbarch);
82f5c14f
MK
197
198 /* Skip if the thread stratum has already been activated. */
199 if (bsd_uthread_active)
200 return 0;
201
202 /* There's no point in enabling this module if no
203 architecture-specific operations are provided. */
204 if (!ops->supply_uthread)
205 return 0;
206
207 bsd_uthread_thread_run_addr =
208 bsd_uthread_lookup_address ("_thread_run", objfile);
209 if (bsd_uthread_thread_run_addr == 0)
210 return 0;
211
212 bsd_uthread_thread_list_addr =
213 bsd_uthread_lookup_address ("_thread_list", objfile);
214 if (bsd_uthread_thread_list_addr == 0)
215 return 0;
216
217 bsd_uthread_thread_state_offset =
218 bsd_uthread_lookup_offset ("_thread_state_offset", objfile);
219 if (bsd_uthread_thread_state_offset == 0)
220 return 0;
221
222 bsd_uthread_thread_next_offset =
223 bsd_uthread_lookup_offset ("_thread_next_offset", objfile);
224 if (bsd_uthread_thread_next_offset == 0)
225 return 0;
226
227 bsd_uthread_thread_ctx_offset =
228 bsd_uthread_lookup_offset ("_thread_ctx_offset", objfile);
229
02980c56 230 current_inferior ()->push_target (&bsd_uthread_ops);
82f5c14f
MK
231 bsd_uthread_active = 1;
232 return 1;
233}
234
39540081 235/* Cleanup due to deactivation. */
82f5c14f 236
f6ac5f3d
PA
237void
238bsd_uthread_target::close ()
82f5c14f 239{
82f5c14f 240 bsd_uthread_active = 0;
82f5c14f
MK
241 bsd_uthread_thread_run_addr = 0;
242 bsd_uthread_thread_list_addr = 0;
243 bsd_uthread_thread_state_offset = 0;
244 bsd_uthread_thread_next_offset = 0;
245 bsd_uthread_thread_ctx_offset = 0;
98107b0b 246 bsd_uthread_solib_name.clear ();
82f5c14f
MK
247}
248
39540081
PA
249/* Deactivate the thread stratum implemented by this module. */
250
251static void
252bsd_uthread_deactivate (void)
253{
254 /* Skip if the thread stratum has already been deactivated. */
255 if (!bsd_uthread_active)
256 return;
257
fadf6add 258 current_inferior ()->unpush_target (&bsd_uthread_ops);
39540081
PA
259}
260
63807e1d 261static void
a0ff652f 262bsd_uthread_inferior_created (inferior *inf)
82f5c14f
MK
263{
264 bsd_uthread_activate (NULL);
265}
266
267/* Likely candidates for the threads library. */
27087b7f 268static const char * const bsd_uthread_solib_names[] =
82f5c14f
MK
269{
270 "/usr/lib/libc_r.so", /* FreeBSD */
271 "/usr/lib/libpthread.so", /* OpenBSD */
272 NULL
273};
274
63807e1d 275static void
7b323785 276bsd_uthread_solib_loaded (solib &so)
82f5c14f 277{
27087b7f 278 const char * const *names = bsd_uthread_solib_names;
82f5c14f
MK
279
280 for (names = bsd_uthread_solib_names; *names; names++)
281 {
6896e625 282 if (startswith (so.original_name, *names))
82f5c14f 283 {
048d532d 284 solib_read_symbols (so, 0);
82f5c14f 285
bb86ab83 286 if (bsd_uthread_activate (so.objfile))
82f5c14f 287 {
6896e625 288 bsd_uthread_solib_name = so.original_name;
82f5c14f
MK
289 return;
290 }
291 }
292 }
293}
294
63807e1d 295static void
9da3b735 296bsd_uthread_solib_unloaded (program_space *pspace, const solib &so,
4bc72a45 297 bool still_in_use, bool /* silent */)
82f5c14f 298{
9da3b735 299 if (bsd_uthread_solib_name.empty () || still_in_use)
82f5c14f
MK
300 return;
301
6896e625 302 if (so.original_name == bsd_uthread_solib_name)
82f5c14f
MK
303 bsd_uthread_deactivate ();
304}
305
f6ac5f3d
PA
306void
307bsd_uthread_target::mourn_inferior ()
82f5c14f 308{
d6ca69cd 309 beneath ()->mourn_inferior ();
82f5c14f
MK
310 bsd_uthread_deactivate ();
311}
312
f6ac5f3d
PA
313void
314bsd_uthread_target::fetch_registers (struct regcache *regcache, int regnum)
82f5c14f 315{
ac7936df 316 struct gdbarch *gdbarch = regcache->arch ();
cb275538 317 struct bsd_uthread_ops *uthread_ops = get_bsd_uthread (gdbarch);
222312d3 318 ptid_t ptid = regcache->ptid ();
cc6bcb54 319 CORE_ADDR addr = ptid.tid ();
82f5c14f 320 CORE_ADDR active_addr;
2989a365 321 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
317cd492
SM
322
323 /* We are doing operations (e.g. reading memory) that rely on
324 inferior_ptid. */
325 inferior_ptid = ptid;
82f5c14f
MK
326
327 /* Always fetch the appropriate registers from the layer beneath. */
d6ca69cd 328 beneath ()->fetch_registers (regcache, regnum);
82f5c14f
MK
329
330 /* FIXME: That might have gotten us more than we asked for. Make
331 sure we overwrite all relevant registers with values from the
332 thread structure. This can go once we fix the underlying target. */
333 regnum = -1;
334
ff7da468 335 active_addr = bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr);
82f5c14f
MK
336 if (addr != 0 && addr != active_addr)
337 {
338 bsd_uthread_check_magic (addr);
28439f5e
PA
339 uthread_ops->supply_uthread (regcache, regnum,
340 addr + bsd_uthread_thread_ctx_offset);
82f5c14f
MK
341 }
342}
343
f6ac5f3d
PA
344void
345bsd_uthread_target::store_registers (struct regcache *regcache, int regnum)
82f5c14f 346{
ac7936df 347 struct gdbarch *gdbarch = regcache->arch ();
cb275538 348 struct bsd_uthread_ops *uthread_ops = get_bsd_uthread (gdbarch);
222312d3 349 ptid_t ptid = regcache->ptid ();
cc6bcb54 350 CORE_ADDR addr = ptid.tid ();
82f5c14f 351 CORE_ADDR active_addr;
2989a365 352 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
317cd492
SM
353
354 /* We are doing operations (e.g. reading memory) that rely on
355 inferior_ptid. */
356 inferior_ptid = ptid;
82f5c14f 357
ff7da468 358 active_addr = bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr);
82f5c14f
MK
359 if (addr != 0 && addr != active_addr)
360 {
361 bsd_uthread_check_magic (addr);
28439f5e
PA
362 uthread_ops->collect_uthread (regcache, regnum,
363 addr + bsd_uthread_thread_ctx_offset);
82f5c14f
MK
364 }
365 else
366 {
367 /* Updating the thread that is currently running; pass the
dda83cd7 368 request to the layer beneath. */
d6ca69cd 369 beneath ()->store_registers (regcache, regnum);
82f5c14f
MK
370 }
371}
372
f6ac5f3d
PA
373ptid_t
374bsd_uthread_target::wait (ptid_t ptid, struct target_waitstatus *status,
b60cea74 375 target_wait_flags options)
82f5c14f 376{
99d9c3b9 377 bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
82f5c14f 378 CORE_ADDR addr;
5b6d1e4f
PA
379 process_stratum_target *beneath
380 = as_process_stratum_target (this->beneath ());
82f5c14f
MK
381
382 /* Pass the request to the layer beneath. */
5b6d1e4f 383 ptid = beneath->wait (ptid, status, options);
82f5c14f 384
a4e7b2e7
MK
385 /* If the process is no longer alive, there's no point in figuring
386 out the thread ID. It will fail anyway. */
183be222
SM
387 if (status->kind () == TARGET_WAITKIND_SIGNALLED
388 || status->kind () == TARGET_WAITKIND_EXITED)
a4e7b2e7
MK
389 return ptid;
390
82f5c14f
MK
391 /* Fetch the corresponding thread ID, and augment the returned
392 process ID with it. */
ff7da468 393 addr = bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr);
82f5c14f
MK
394 if (addr != 0)
395 {
df80278b 396 gdb_byte buf[4];
82f5c14f
MK
397
398 /* FIXME: For executables linked statically with the threads
dda83cd7
SM
399 library, we end up here before the program has actually been
400 executed. In that case ADDR will be garbage since it has
401 been read from the wrong virtual memory image. */
82f5c14f
MK
402 if (target_read_memory (addr, buf, 4) == 0)
403 {
e17a4113 404 ULONGEST magic = extract_unsigned_integer (buf, 4, byte_order);
82f5c14f 405 if (magic == BSD_UTHREAD_PTHREAD_MAGIC)
e99b03dc 406 ptid = ptid_t (ptid.pid (), 0, addr);
82f5c14f
MK
407 }
408 }
409
fb5e7258
PA
410 /* If INFERIOR_PTID doesn't have a tid member yet, and we now have a
411 ptid with tid set, then ptid is still the initial thread of
412 the process. Notify GDB core about it. */
cc6bcb54 413 if (inferior_ptid.tid () == 0
5b6d1e4f
PA
414 && ptid.tid () != 0 && !in_thread_list (beneath, ptid))
415 thread_change_ptid (beneath, inferior_ptid, ptid);
fb5e7258
PA
416
417 /* Don't let the core see a ptid without a corresponding thread. */
9213a6d7 418 thread_info *thread = beneath->find_thread (ptid);
00431a78 419 if (thread == NULL || thread->state == THREAD_EXITED)
5b6d1e4f 420 add_thread (beneath, ptid);
82f5c14f
MK
421
422 return ptid;
423}
424
f6ac5f3d
PA
425void
426bsd_uthread_target::resume (ptid_t ptid, int step, enum gdb_signal sig)
82f5c14f
MK
427{
428 /* Pass the request to the layer beneath. */
d6ca69cd 429 beneath ()->resume (ptid, step, sig);
82f5c14f
MK
430}
431
57810aa7 432bool
f6ac5f3d 433bsd_uthread_target::thread_alive (ptid_t ptid)
82f5c14f 434{
99d9c3b9 435 bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
cc6bcb54 436 CORE_ADDR addr = ptid.tid ();
82f5c14f
MK
437
438 if (addr != 0)
439 {
440 int offset = bsd_uthread_thread_state_offset;
441 ULONGEST state;
442
443 bsd_uthread_check_magic (addr);
444
e17a4113 445 state = read_memory_unsigned_integer (addr + offset, 4, byte_order);
82f5c14f 446 if (state == BSD_UTHREAD_PS_DEAD)
57810aa7 447 return false;
82f5c14f
MK
448 }
449
d6ca69cd 450 return beneath ()->thread_alive (ptid);
82f5c14f
MK
451}
452
f6ac5f3d
PA
453void
454bsd_uthread_target::update_thread_list ()
82f5c14f 455{
e99b03dc 456 pid_t pid = inferior_ptid.pid ();
82f5c14f
MK
457 int offset = bsd_uthread_thread_next_offset;
458 CORE_ADDR addr;
459
e8032dde
PA
460 prune_threads ();
461
ff7da468 462 addr = bsd_uthread_read_memory_address (bsd_uthread_thread_list_addr);
82f5c14f
MK
463 while (addr != 0)
464 {
fd79271b 465 ptid_t ptid = ptid_t (pid, 0, addr);
82f5c14f 466
5b6d1e4f
PA
467 process_stratum_target *proc_target
468 = as_process_stratum_target (this->beneath ());
9213a6d7 469 thread_info *thread = proc_target->find_thread (ptid);
00431a78 470 if (thread == nullptr || thread->state == THREAD_EXITED)
757f359d
PA
471 {
472 /* If INFERIOR_PTID doesn't have a tid member yet, then ptid
473 is still the initial thread of the process. Notify GDB
474 core about it. */
cc6bcb54 475 if (inferior_ptid.tid () == 0)
5b6d1e4f 476 thread_change_ptid (proc_target, inferior_ptid, ptid);
757f359d 477 else
5b6d1e4f 478 add_thread (proc_target, ptid);
757f359d 479 }
82f5c14f 480
ff7da468 481 addr = bsd_uthread_read_memory_address (addr + offset);
82f5c14f
MK
482 }
483}
484
485/* Possible states a thread can be in. */
27087b7f 486static const char * const bsd_uthread_state[] =
82f5c14f
MK
487{
488 "RUNNING",
489 "SIGTHREAD",
490 "MUTEX_WAIT",
491 "COND_WAIT",
492 "FDLR_WAIT",
493 "FDLW_WAIT",
494 "FDR_WAIT",
495 "FDW_WAIT",
496 "FILE_WAIT",
497 "POLL_WAIT",
498 "SELECT_WAIT",
499 "SLEEP_WAIT",
500 "WAIT_WAIT",
501 "SIGSUSPEND",
502 "SIGWAIT",
503 "SPINBLOCK",
504 "JOIN",
505 "SUSPENDED",
506 "DEAD",
507 "DEADLOCK"
508};
509
510/* Return a string describing th state of the thread specified by
511 INFO. */
512
f6ac5f3d
PA
513const char *
514bsd_uthread_target::extra_thread_info (thread_info *info)
82f5c14f 515{
99d9c3b9 516 bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
cc6bcb54 517 CORE_ADDR addr = info->ptid.tid ();
82f5c14f
MK
518
519 if (addr != 0)
520 {
521 int offset = bsd_uthread_thread_state_offset;
522 ULONGEST state;
523
e17a4113 524 state = read_memory_unsigned_integer (addr + offset, 4, byte_order);
82f5c14f
MK
525 if (state < ARRAY_SIZE (bsd_uthread_state))
526 return bsd_uthread_state[state];
527 }
528
529 return NULL;
530}
531
a068643d 532std::string
f6ac5f3d 533bsd_uthread_target::pid_to_str (ptid_t ptid)
82f5c14f 534{
cc6bcb54 535 if (ptid.tid () != 0)
96bbe3ef
TT
536 return string_printf ("process %d, thread 0x%s",
537 ptid.pid (),
9c1f84c9 538 phex_nz (ptid.tid ()));
82f5c14f
MK
539
540 return normal_pid_to_str (ptid);
541}
542
5fe70629 543INIT_GDB_FILE (bsd_uthread)
82f5c14f 544{
c90e7d63
SM
545 gdb::observers::inferior_created.attach (bsd_uthread_inferior_created,
546 "bsd-uthread");
547 gdb::observers::solib_loaded.attach (bsd_uthread_solib_loaded,
548 "bsd-uthread");
549 gdb::observers::solib_unloaded.attach (bsd_uthread_solib_unloaded,
550 "bsd-uthread");
82f5c14f 551}