]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/bsd-uthread.c
Remove pid_to_ptid
[thirdparty/binutils-gdb.git] / gdb / bsd-uthread.c
CommitLineData
82f5c14f
MK
1/* BSD user-level threads support.
2
e2882c85 3 Copyright (C) 2005-2018 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
MK
19
20#include "defs.h"
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 "solist.h"
29#include "symfile.h"
30#include "target.h"
31
82f5c14f
MK
32#include "gdb_obstack.h"
33
34#include "bsd-uthread.h"
35
d9f719f1
PA
36static const target_info bsd_uthread_target_info = {
37 "bsd-uthreads",
38 N_("BSD user-level threads"),
39 N_("BSD user-level threads")
40};
41
f6ac5f3d
PA
42struct bsd_uthread_target final : public target_ops
43{
44 bsd_uthread_target ()
45 { to_stratum = thread_stratum; }
46
d9f719f1
PA
47 const target_info &info () const override
48 { return bsd_uthread_target_info; }
f6ac5f3d
PA
49
50 void close () override;
51
52 void mourn_inferior () override;
53
54 void fetch_registers (struct regcache *, int) override;
55 void store_registers (struct regcache *, int) override;
56
57 ptid_t wait (ptid_t, struct target_waitstatus *, int) override;
58 void resume (ptid_t, int, enum gdb_signal) override;
59
57810aa7 60 bool thread_alive (ptid_t ptid) override;
f6ac5f3d
PA
61
62 void update_thread_list () override;
63
64 const char *extra_thread_info (struct thread_info *) override;
65
66 const char *pid_to_str (ptid_t) override;
67};
68
69static bsd_uthread_target bsd_uthread_ops;
82f5c14f
MK
70\f
71
72/* Architecture-specific operations. */
73
74/* Per-architecture data key. */
75static struct gdbarch_data *bsd_uthread_data;
76
77struct bsd_uthread_ops
78{
79 /* Supply registers for an inactive thread to a register cache. */
80 void (*supply_uthread)(struct regcache *, int, CORE_ADDR);
81
82 /* Collect registers for an inactive thread from a register cache. */
83 void (*collect_uthread)(const struct regcache *, int, CORE_ADDR);
84};
85
86static void *
87bsd_uthread_init (struct obstack *obstack)
88{
89 struct bsd_uthread_ops *ops;
90
91 ops = OBSTACK_ZALLOC (obstack, struct bsd_uthread_ops);
92 return ops;
93}
94
95/* Set the function that supplies registers from an inactive thread
96 for architecture GDBARCH to SUPPLY_UTHREAD. */
97
98void
99bsd_uthread_set_supply_uthread (struct gdbarch *gdbarch,
100 void (*supply_uthread) (struct regcache *,
101 int, CORE_ADDR))
102{
9a3c8263
SM
103 struct bsd_uthread_ops *ops
104 = (struct bsd_uthread_ops *) gdbarch_data (gdbarch, bsd_uthread_data);
105
82f5c14f
MK
106 ops->supply_uthread = supply_uthread;
107}
108
109/* Set the function that collects registers for an inactive thread for
110 architecture GDBARCH to SUPPLY_UTHREAD. */
111
112void
113bsd_uthread_set_collect_uthread (struct gdbarch *gdbarch,
114 void (*collect_uthread) (const struct regcache *,
115 int, CORE_ADDR))
116{
9a3c8263
SM
117 struct bsd_uthread_ops *ops
118 = (struct bsd_uthread_ops *) gdbarch_data (gdbarch, bsd_uthread_data);
119
82f5c14f
MK
120 ops->collect_uthread = collect_uthread;
121}
122
123/* Magic number to help recognize a valid thread structure. */
124#define BSD_UTHREAD_PTHREAD_MAGIC 0xd09ba115
125
126/* Check whether the thread structure at ADDR is valid. */
127
128static void
129bsd_uthread_check_magic (CORE_ADDR addr)
130{
f5656ead 131 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
e17a4113 132 ULONGEST magic = read_memory_unsigned_integer (addr, 4, byte_order);
82f5c14f
MK
133
134 if (magic != BSD_UTHREAD_PTHREAD_MAGIC)
8a3fe4f8 135 error (_("Bad magic"));
82f5c14f
MK
136}
137
138/* Thread states. */
139#define BSD_UTHREAD_PS_RUNNING 0
140#define BSD_UTHREAD_PS_DEAD 18
141
b021a221 142/* Address of the pointer to the thread structure for the running
82f5c14f
MK
143 thread. */
144static CORE_ADDR bsd_uthread_thread_run_addr;
145
146/* Address of the list of all threads. */
147static CORE_ADDR bsd_uthread_thread_list_addr;
148
149/* Offsets of various "interesting" bits in the thread structure. */
150static int bsd_uthread_thread_state_offset = -1;
151static int bsd_uthread_thread_next_offset = -1;
152static int bsd_uthread_thread_ctx_offset;
153
154/* Name of shared threads library. */
155static const char *bsd_uthread_solib_name;
156
157/* Non-zero if the thread startum implemented by this module is active. */
158static int bsd_uthread_active;
159
160static CORE_ADDR
161bsd_uthread_lookup_address (const char *name, struct objfile *objfile)
162{
3b7344d5 163 struct bound_minimal_symbol sym;
82f5c14f
MK
164
165 sym = lookup_minimal_symbol (name, NULL, objfile);
3b7344d5 166 if (sym.minsym)
77e371c0 167 return BMSYMBOL_VALUE_ADDRESS (sym);
82f5c14f
MK
168
169 return 0;
170}
171
172static int
173bsd_uthread_lookup_offset (const char *name, struct objfile *objfile)
174{
f5656ead 175 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
82f5c14f
MK
176 CORE_ADDR addr;
177
178 addr = bsd_uthread_lookup_address (name, objfile);
179 if (addr == 0)
180 return 0;
181
e17a4113 182 return read_memory_unsigned_integer (addr, 4, byte_order);
82f5c14f
MK
183}
184
ff7da468
UW
185static CORE_ADDR
186bsd_uthread_read_memory_address (CORE_ADDR addr)
187{
f5656ead 188 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
ff7da468
UW
189 return read_memory_typed_address (addr, ptr_type);
190}
191
82f5c14f
MK
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. */
195
196static int
197bsd_uthread_activate (struct objfile *objfile)
198{
f5656ead 199 struct gdbarch *gdbarch = target_gdbarch ();
9a3c8263
SM
200 struct bsd_uthread_ops *ops
201 = (struct bsd_uthread_ops *) gdbarch_data (gdbarch, bsd_uthread_data);
82f5c14f
MK
202
203 /* Skip if the thread stratum has already been activated. */
204 if (bsd_uthread_active)
205 return 0;
206
207 /* There's no point in enabling this module if no
208 architecture-specific operations are provided. */
209 if (!ops->supply_uthread)
210 return 0;
211
212 bsd_uthread_thread_run_addr =
213 bsd_uthread_lookup_address ("_thread_run", objfile);
214 if (bsd_uthread_thread_run_addr == 0)
215 return 0;
216
217 bsd_uthread_thread_list_addr =
218 bsd_uthread_lookup_address ("_thread_list", objfile);
219 if (bsd_uthread_thread_list_addr == 0)
220 return 0;
221
222 bsd_uthread_thread_state_offset =
223 bsd_uthread_lookup_offset ("_thread_state_offset", objfile);
224 if (bsd_uthread_thread_state_offset == 0)
225 return 0;
226
227 bsd_uthread_thread_next_offset =
228 bsd_uthread_lookup_offset ("_thread_next_offset", objfile);
229 if (bsd_uthread_thread_next_offset == 0)
230 return 0;
231
232 bsd_uthread_thread_ctx_offset =
233 bsd_uthread_lookup_offset ("_thread_ctx_offset", objfile);
234
f6ac5f3d 235 push_target (&bsd_uthread_ops);
82f5c14f
MK
236 bsd_uthread_active = 1;
237 return 1;
238}
239
39540081 240/* Cleanup due to deactivation. */
82f5c14f 241
f6ac5f3d
PA
242void
243bsd_uthread_target::close ()
82f5c14f 244{
82f5c14f 245 bsd_uthread_active = 0;
82f5c14f
MK
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;
252}
253
39540081
PA
254/* Deactivate the thread stratum implemented by this module. */
255
256static void
257bsd_uthread_deactivate (void)
258{
259 /* Skip if the thread stratum has already been deactivated. */
260 if (!bsd_uthread_active)
261 return;
262
f6ac5f3d 263 unpush_target (&bsd_uthread_ops);
39540081
PA
264}
265
63807e1d 266static void
82f5c14f
MK
267bsd_uthread_inferior_created (struct target_ops *ops, int from_tty)
268{
269 bsd_uthread_activate (NULL);
270}
271
272/* Likely candidates for the threads library. */
273static const char *bsd_uthread_solib_names[] =
274{
275 "/usr/lib/libc_r.so", /* FreeBSD */
276 "/usr/lib/libpthread.so", /* OpenBSD */
277 NULL
278};
279
63807e1d 280static void
82f5c14f
MK
281bsd_uthread_solib_loaded (struct so_list *so)
282{
283 const char **names = bsd_uthread_solib_names;
284
285 for (names = bsd_uthread_solib_names; *names; names++)
286 {
61012eef 287 if (startswith (so->so_original_name, *names))
82f5c14f 288 {
048d532d 289 solib_read_symbols (so, 0);
82f5c14f
MK
290
291 if (bsd_uthread_activate (so->objfile))
292 {
39540081 293 bsd_uthread_solib_name = so->so_original_name;
82f5c14f
MK
294 return;
295 }
296 }
297 }
298}
299
63807e1d 300static void
82f5c14f
MK
301bsd_uthread_solib_unloaded (struct so_list *so)
302{
303 if (!bsd_uthread_solib_name)
304 return;
305
306 if (strcmp (so->so_original_name, bsd_uthread_solib_name) == 0)
307 bsd_uthread_deactivate ();
308}
309
f6ac5f3d
PA
310void
311bsd_uthread_target::mourn_inferior ()
82f5c14f 312{
d6ca69cd 313 beneath ()->mourn_inferior ();
82f5c14f
MK
314 bsd_uthread_deactivate ();
315}
316
f6ac5f3d
PA
317void
318bsd_uthread_target::fetch_registers (struct regcache *regcache, int regnum)
82f5c14f 319{
ac7936df 320 struct gdbarch *gdbarch = regcache->arch ();
9a3c8263
SM
321 struct bsd_uthread_ops *uthread_ops
322 = (struct bsd_uthread_ops *) gdbarch_data (gdbarch, bsd_uthread_data);
222312d3 323 ptid_t ptid = regcache->ptid ();
317cd492 324 CORE_ADDR addr = ptid_get_tid (ptid);
82f5c14f 325 CORE_ADDR active_addr;
2989a365 326 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
317cd492
SM
327
328 /* We are doing operations (e.g. reading memory) that rely on
329 inferior_ptid. */
330 inferior_ptid = ptid;
82f5c14f
MK
331
332 /* Always fetch the appropriate registers from the layer beneath. */
d6ca69cd 333 beneath ()->fetch_registers (regcache, regnum);
82f5c14f
MK
334
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. */
338 regnum = -1;
339
ff7da468 340 active_addr = bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr);
82f5c14f
MK
341 if (addr != 0 && addr != active_addr)
342 {
343 bsd_uthread_check_magic (addr);
28439f5e
PA
344 uthread_ops->supply_uthread (regcache, regnum,
345 addr + bsd_uthread_thread_ctx_offset);
82f5c14f
MK
346 }
347}
348
f6ac5f3d
PA
349void
350bsd_uthread_target::store_registers (struct regcache *regcache, int regnum)
82f5c14f 351{
ac7936df 352 struct gdbarch *gdbarch = regcache->arch ();
9a3c8263
SM
353 struct bsd_uthread_ops *uthread_ops
354 = (struct bsd_uthread_ops *) gdbarch_data (gdbarch, bsd_uthread_data);
222312d3 355 ptid_t ptid = regcache->ptid ();
317cd492 356 CORE_ADDR addr = ptid_get_tid (ptid);
82f5c14f 357 CORE_ADDR active_addr;
2989a365 358 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
317cd492
SM
359
360 /* We are doing operations (e.g. reading memory) that rely on
361 inferior_ptid. */
362 inferior_ptid = ptid;
82f5c14f 363
ff7da468 364 active_addr = bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr);
82f5c14f
MK
365 if (addr != 0 && addr != active_addr)
366 {
367 bsd_uthread_check_magic (addr);
28439f5e
PA
368 uthread_ops->collect_uthread (regcache, regnum,
369 addr + bsd_uthread_thread_ctx_offset);
82f5c14f
MK
370 }
371 else
372 {
373 /* Updating the thread that is currently running; pass the
374 request to the layer beneath. */
d6ca69cd 375 beneath ()->store_registers (regcache, regnum);
82f5c14f
MK
376 }
377}
378
f6ac5f3d
PA
379ptid_t
380bsd_uthread_target::wait (ptid_t ptid, struct target_waitstatus *status,
381 int options)
82f5c14f 382{
f5656ead 383 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
82f5c14f
MK
384 CORE_ADDR addr;
385
386 /* Pass the request to the layer beneath. */
d6ca69cd 387 ptid = beneath ()->wait (ptid, status, options);
82f5c14f 388
a4e7b2e7
MK
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)
393 return ptid;
394
82f5c14f
MK
395 /* Fetch the corresponding thread ID, and augment the returned
396 process ID with it. */
ff7da468 397 addr = bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr);
82f5c14f
MK
398 if (addr != 0)
399 {
df80278b 400 gdb_byte buf[4];
82f5c14f
MK
401
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)
407 {
e17a4113 408 ULONGEST magic = extract_unsigned_integer (buf, 4, byte_order);
82f5c14f 409 if (magic == BSD_UTHREAD_PTHREAD_MAGIC)
fd79271b 410 ptid = ptid_t (ptid_get_pid (ptid), 0, addr);
82f5c14f
MK
411 }
412 }
413
fb5e7258
PA
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);
420
421 /* Don't let the core see a ptid without a corresponding thread. */
00431a78
PA
422 thread_info *thread = find_thread_ptid (ptid);
423 if (thread == NULL || thread->state == THREAD_EXITED)
fb5e7258 424 add_thread (ptid);
82f5c14f
MK
425
426 return ptid;
427}
428
f6ac5f3d
PA
429void
430bsd_uthread_target::resume (ptid_t ptid, int step, enum gdb_signal sig)
82f5c14f
MK
431{
432 /* Pass the request to the layer beneath. */
d6ca69cd 433 beneath ()->resume (ptid, step, sig);
82f5c14f
MK
434}
435
57810aa7 436bool
f6ac5f3d 437bsd_uthread_target::thread_alive (ptid_t ptid)
82f5c14f 438{
f5656ead 439 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
8eaf5320 440 CORE_ADDR addr = ptid_get_tid (ptid);
82f5c14f
MK
441
442 if (addr != 0)
443 {
444 int offset = bsd_uthread_thread_state_offset;
445 ULONGEST state;
446
447 bsd_uthread_check_magic (addr);
448
e17a4113 449 state = read_memory_unsigned_integer (addr + offset, 4, byte_order);
82f5c14f 450 if (state == BSD_UTHREAD_PS_DEAD)
57810aa7 451 return false;
82f5c14f
MK
452 }
453
d6ca69cd 454 return beneath ()->thread_alive (ptid);
82f5c14f
MK
455}
456
f6ac5f3d
PA
457void
458bsd_uthread_target::update_thread_list ()
82f5c14f
MK
459{
460 pid_t pid = ptid_get_pid (inferior_ptid);
461 int offset = bsd_uthread_thread_next_offset;
462 CORE_ADDR addr;
463
e8032dde
PA
464 prune_threads ();
465
ff7da468 466 addr = bsd_uthread_read_memory_address (bsd_uthread_thread_list_addr);
82f5c14f
MK
467 while (addr != 0)
468 {
fd79271b 469 ptid_t ptid = ptid_t (pid, 0, addr);
82f5c14f 470
00431a78
PA
471 thread_info *thread = find_thread_ptid (ptid);
472 if (thread == nullptr || thread->state == THREAD_EXITED)
757f359d
PA
473 {
474 /* If INFERIOR_PTID doesn't have a tid member yet, then ptid
475 is still the initial thread of the process. Notify GDB
476 core about it. */
477 if (ptid_get_tid (inferior_ptid) == 0)
478 thread_change_ptid (inferior_ptid, ptid);
479 else
480 add_thread (ptid);
481 }
82f5c14f 482
ff7da468 483 addr = bsd_uthread_read_memory_address (addr + offset);
82f5c14f
MK
484 }
485}
486
487/* Possible states a thread can be in. */
a121b7c1 488static const char *bsd_uthread_state[] =
82f5c14f
MK
489{
490 "RUNNING",
491 "SIGTHREAD",
492 "MUTEX_WAIT",
493 "COND_WAIT",
494 "FDLR_WAIT",
495 "FDLW_WAIT",
496 "FDR_WAIT",
497 "FDW_WAIT",
498 "FILE_WAIT",
499 "POLL_WAIT",
500 "SELECT_WAIT",
501 "SLEEP_WAIT",
502 "WAIT_WAIT",
503 "SIGSUSPEND",
504 "SIGWAIT",
505 "SPINBLOCK",
506 "JOIN",
507 "SUSPENDED",
508 "DEAD",
509 "DEADLOCK"
510};
511
512/* Return a string describing th state of the thread specified by
513 INFO. */
514
f6ac5f3d
PA
515const char *
516bsd_uthread_target::extra_thread_info (thread_info *info)
82f5c14f 517{
f5656ead 518 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
82f5c14f
MK
519 CORE_ADDR addr = ptid_get_tid (info->ptid);
520
521 if (addr != 0)
522 {
523 int offset = bsd_uthread_thread_state_offset;
524 ULONGEST state;
525
e17a4113 526 state = read_memory_unsigned_integer (addr + offset, 4, byte_order);
82f5c14f
MK
527 if (state < ARRAY_SIZE (bsd_uthread_state))
528 return bsd_uthread_state[state];
529 }
530
531 return NULL;
532}
533
f6ac5f3d
PA
534const char *
535bsd_uthread_target::pid_to_str (ptid_t ptid)
82f5c14f
MK
536{
537 if (ptid_get_tid (ptid) != 0)
538 {
539 static char buf[64];
82f5c14f 540
5fff8fc0
MK
541 xsnprintf (buf, sizeof buf, "process %d, thread 0x%lx",
542 ptid_get_pid (ptid), ptid_get_tid (ptid));
82f5c14f
MK
543 return buf;
544 }
545
546 return normal_pid_to_str (ptid);
547}
548
82f5c14f
MK
549void
550_initialize_bsd_uthread (void)
551{
82f5c14f
MK
552 bsd_uthread_data = gdbarch_data_register_pre_init (bsd_uthread_init);
553
76727919
TT
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);
82f5c14f 557}