Identify verilog dump tests as such.
[deliverable/binutils-gdb.git] / gdb / bsd-uthread.c
CommitLineData
82f5c14f
MK
1/* BSD user-level threads support.
2
618f726f 3 Copyright (C) 2005-2016 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"
25#include "observer.h"
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
36/* HACK: Save the bsd_uthreads ops returned by bsd_uthread_target. */
37static struct target_ops *bsd_uthread_ops_hack;
38\f
39
40/* Architecture-specific operations. */
41
42/* Per-architecture data key. */
43static struct gdbarch_data *bsd_uthread_data;
44
45struct bsd_uthread_ops
46{
47 /* Supply registers for an inactive thread to a register cache. */
48 void (*supply_uthread)(struct regcache *, int, CORE_ADDR);
49
50 /* Collect registers for an inactive thread from a register cache. */
51 void (*collect_uthread)(const struct regcache *, int, CORE_ADDR);
52};
53
54static void *
55bsd_uthread_init (struct obstack *obstack)
56{
57 struct bsd_uthread_ops *ops;
58
59 ops = OBSTACK_ZALLOC (obstack, struct bsd_uthread_ops);
60 return ops;
61}
62
63/* Set the function that supplies registers from an inactive thread
64 for architecture GDBARCH to SUPPLY_UTHREAD. */
65
66void
67bsd_uthread_set_supply_uthread (struct gdbarch *gdbarch,
68 void (*supply_uthread) (struct regcache *,
69 int, CORE_ADDR))
70{
9a3c8263
SM
71 struct bsd_uthread_ops *ops
72 = (struct bsd_uthread_ops *) gdbarch_data (gdbarch, bsd_uthread_data);
73
82f5c14f
MK
74 ops->supply_uthread = supply_uthread;
75}
76
77/* Set the function that collects registers for an inactive thread for
78 architecture GDBARCH to SUPPLY_UTHREAD. */
79
80void
81bsd_uthread_set_collect_uthread (struct gdbarch *gdbarch,
82 void (*collect_uthread) (const struct regcache *,
83 int, CORE_ADDR))
84{
9a3c8263
SM
85 struct bsd_uthread_ops *ops
86 = (struct bsd_uthread_ops *) gdbarch_data (gdbarch, bsd_uthread_data);
87
82f5c14f
MK
88 ops->collect_uthread = collect_uthread;
89}
90
91/* Magic number to help recognize a valid thread structure. */
92#define BSD_UTHREAD_PTHREAD_MAGIC 0xd09ba115
93
94/* Check whether the thread structure at ADDR is valid. */
95
96static void
97bsd_uthread_check_magic (CORE_ADDR addr)
98{
f5656ead 99 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
e17a4113 100 ULONGEST magic = read_memory_unsigned_integer (addr, 4, byte_order);
82f5c14f
MK
101
102 if (magic != BSD_UTHREAD_PTHREAD_MAGIC)
8a3fe4f8 103 error (_("Bad magic"));
82f5c14f
MK
104}
105
106/* Thread states. */
107#define BSD_UTHREAD_PS_RUNNING 0
108#define BSD_UTHREAD_PS_DEAD 18
109
b021a221 110/* Address of the pointer to the thread structure for the running
82f5c14f
MK
111 thread. */
112static CORE_ADDR bsd_uthread_thread_run_addr;
113
114/* Address of the list of all threads. */
115static CORE_ADDR bsd_uthread_thread_list_addr;
116
117/* Offsets of various "interesting" bits in the thread structure. */
118static int bsd_uthread_thread_state_offset = -1;
119static int bsd_uthread_thread_next_offset = -1;
120static int bsd_uthread_thread_ctx_offset;
121
122/* Name of shared threads library. */
123static const char *bsd_uthread_solib_name;
124
125/* Non-zero if the thread startum implemented by this module is active. */
126static int bsd_uthread_active;
127
128static CORE_ADDR
129bsd_uthread_lookup_address (const char *name, struct objfile *objfile)
130{
3b7344d5 131 struct bound_minimal_symbol sym;
82f5c14f
MK
132
133 sym = lookup_minimal_symbol (name, NULL, objfile);
3b7344d5 134 if (sym.minsym)
77e371c0 135 return BMSYMBOL_VALUE_ADDRESS (sym);
82f5c14f
MK
136
137 return 0;
138}
139
140static int
141bsd_uthread_lookup_offset (const char *name, struct objfile *objfile)
142{
f5656ead 143 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
82f5c14f
MK
144 CORE_ADDR addr;
145
146 addr = bsd_uthread_lookup_address (name, objfile);
147 if (addr == 0)
148 return 0;
149
e17a4113 150 return read_memory_unsigned_integer (addr, 4, byte_order);
82f5c14f
MK
151}
152
ff7da468
UW
153static CORE_ADDR
154bsd_uthread_read_memory_address (CORE_ADDR addr)
155{
f5656ead 156 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
ff7da468
UW
157 return read_memory_typed_address (addr, ptr_type);
158}
159
82f5c14f
MK
160/* If OBJFILE contains the symbols corresponding to one of the
161 supported user-level threads libraries, activate the thread stratum
162 implemented by this module. */
163
164static int
165bsd_uthread_activate (struct objfile *objfile)
166{
f5656ead 167 struct gdbarch *gdbarch = target_gdbarch ();
9a3c8263
SM
168 struct bsd_uthread_ops *ops
169 = (struct bsd_uthread_ops *) gdbarch_data (gdbarch, bsd_uthread_data);
82f5c14f
MK
170
171 /* Skip if the thread stratum has already been activated. */
172 if (bsd_uthread_active)
173 return 0;
174
175 /* There's no point in enabling this module if no
176 architecture-specific operations are provided. */
177 if (!ops->supply_uthread)
178 return 0;
179
180 bsd_uthread_thread_run_addr =
181 bsd_uthread_lookup_address ("_thread_run", objfile);
182 if (bsd_uthread_thread_run_addr == 0)
183 return 0;
184
185 bsd_uthread_thread_list_addr =
186 bsd_uthread_lookup_address ("_thread_list", objfile);
187 if (bsd_uthread_thread_list_addr == 0)
188 return 0;
189
190 bsd_uthread_thread_state_offset =
191 bsd_uthread_lookup_offset ("_thread_state_offset", objfile);
192 if (bsd_uthread_thread_state_offset == 0)
193 return 0;
194
195 bsd_uthread_thread_next_offset =
196 bsd_uthread_lookup_offset ("_thread_next_offset", objfile);
197 if (bsd_uthread_thread_next_offset == 0)
198 return 0;
199
200 bsd_uthread_thread_ctx_offset =
201 bsd_uthread_lookup_offset ("_thread_ctx_offset", objfile);
202
203 push_target (bsd_uthread_ops_hack);
204 bsd_uthread_active = 1;
205 return 1;
206}
207
39540081 208/* Cleanup due to deactivation. */
82f5c14f
MK
209
210static void
de90e03d 211bsd_uthread_close (struct target_ops *self)
82f5c14f 212{
82f5c14f 213 bsd_uthread_active = 0;
82f5c14f
MK
214 bsd_uthread_thread_run_addr = 0;
215 bsd_uthread_thread_list_addr = 0;
216 bsd_uthread_thread_state_offset = 0;
217 bsd_uthread_thread_next_offset = 0;
218 bsd_uthread_thread_ctx_offset = 0;
219 bsd_uthread_solib_name = NULL;
220}
221
39540081
PA
222/* Deactivate the thread stratum implemented by this module. */
223
224static void
225bsd_uthread_deactivate (void)
226{
227 /* Skip if the thread stratum has already been deactivated. */
228 if (!bsd_uthread_active)
229 return;
230
231 unpush_target (bsd_uthread_ops_hack);
232}
233
63807e1d 234static void
82f5c14f
MK
235bsd_uthread_inferior_created (struct target_ops *ops, int from_tty)
236{
237 bsd_uthread_activate (NULL);
238}
239
240/* Likely candidates for the threads library. */
241static const char *bsd_uthread_solib_names[] =
242{
243 "/usr/lib/libc_r.so", /* FreeBSD */
244 "/usr/lib/libpthread.so", /* OpenBSD */
245 NULL
246};
247
63807e1d 248static void
82f5c14f
MK
249bsd_uthread_solib_loaded (struct so_list *so)
250{
251 const char **names = bsd_uthread_solib_names;
252
253 for (names = bsd_uthread_solib_names; *names; names++)
254 {
61012eef 255 if (startswith (so->so_original_name, *names))
82f5c14f 256 {
048d532d 257 solib_read_symbols (so, 0);
82f5c14f
MK
258
259 if (bsd_uthread_activate (so->objfile))
260 {
39540081 261 bsd_uthread_solib_name = so->so_original_name;
82f5c14f
MK
262 return;
263 }
264 }
265 }
266}
267
63807e1d 268static void
82f5c14f
MK
269bsd_uthread_solib_unloaded (struct so_list *so)
270{
271 if (!bsd_uthread_solib_name)
272 return;
273
274 if (strcmp (so->so_original_name, bsd_uthread_solib_name) == 0)
275 bsd_uthread_deactivate ();
276}
277
278static void
136d6dae 279bsd_uthread_mourn_inferior (struct target_ops *ops)
82f5c14f 280{
28439f5e 281 struct target_ops *beneath = find_target_beneath (ops);
136d6dae 282 beneath->to_mourn_inferior (beneath);
82f5c14f
MK
283 bsd_uthread_deactivate ();
284}
285
286static void
28439f5e
PA
287bsd_uthread_fetch_registers (struct target_ops *ops,
288 struct regcache *regcache, int regnum)
82f5c14f 289{
27524c05 290 struct gdbarch *gdbarch = get_regcache_arch (regcache);
9a3c8263
SM
291 struct bsd_uthread_ops *uthread_ops
292 = (struct bsd_uthread_ops *) gdbarch_data (gdbarch, bsd_uthread_data);
82f5c14f 293 CORE_ADDR addr = ptid_get_tid (inferior_ptid);
28439f5e 294 struct target_ops *beneath = find_target_beneath (ops);
82f5c14f
MK
295 CORE_ADDR active_addr;
296
297 /* Always fetch the appropriate registers from the layer beneath. */
28439f5e 298 beneath->to_fetch_registers (beneath, regcache, regnum);
82f5c14f
MK
299
300 /* FIXME: That might have gotten us more than we asked for. Make
301 sure we overwrite all relevant registers with values from the
302 thread structure. This can go once we fix the underlying target. */
303 regnum = -1;
304
ff7da468 305 active_addr = bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr);
82f5c14f
MK
306 if (addr != 0 && addr != active_addr)
307 {
308 bsd_uthread_check_magic (addr);
28439f5e
PA
309 uthread_ops->supply_uthread (regcache, regnum,
310 addr + bsd_uthread_thread_ctx_offset);
82f5c14f
MK
311 }
312}
313
314static void
28439f5e
PA
315bsd_uthread_store_registers (struct target_ops *ops,
316 struct regcache *regcache, int regnum)
82f5c14f 317{
27524c05 318 struct gdbarch *gdbarch = get_regcache_arch (regcache);
9a3c8263
SM
319 struct bsd_uthread_ops *uthread_ops
320 = (struct bsd_uthread_ops *) gdbarch_data (gdbarch, bsd_uthread_data);
28439f5e 321 struct target_ops *beneath = find_target_beneath (ops);
82f5c14f
MK
322 CORE_ADDR addr = ptid_get_tid (inferior_ptid);
323 CORE_ADDR active_addr;
324
ff7da468 325 active_addr = bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr);
82f5c14f
MK
326 if (addr != 0 && addr != active_addr)
327 {
328 bsd_uthread_check_magic (addr);
28439f5e
PA
329 uthread_ops->collect_uthread (regcache, regnum,
330 addr + bsd_uthread_thread_ctx_offset);
82f5c14f
MK
331 }
332 else
333 {
334 /* Updating the thread that is currently running; pass the
335 request to the layer beneath. */
28439f5e 336 beneath->to_store_registers (beneath, regcache, regnum);
82f5c14f
MK
337 }
338}
339
82f5c14f 340static ptid_t
117de6a9 341bsd_uthread_wait (struct target_ops *ops,
47608cb1 342 ptid_t ptid, struct target_waitstatus *status, int options)
82f5c14f 343{
f5656ead 344 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
82f5c14f 345 CORE_ADDR addr;
28439f5e 346 struct target_ops *beneath = find_target_beneath (ops);
82f5c14f
MK
347
348 /* Pass the request to the layer beneath. */
47608cb1 349 ptid = beneath->to_wait (beneath, ptid, status, options);
82f5c14f 350
a4e7b2e7
MK
351 /* If the process is no longer alive, there's no point in figuring
352 out the thread ID. It will fail anyway. */
353 if (status->kind == TARGET_WAITKIND_SIGNALLED
354 || status->kind == TARGET_WAITKIND_EXITED)
355 return ptid;
356
82f5c14f
MK
357 /* Fetch the corresponding thread ID, and augment the returned
358 process ID with it. */
ff7da468 359 addr = bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr);
82f5c14f
MK
360 if (addr != 0)
361 {
df80278b 362 gdb_byte buf[4];
82f5c14f
MK
363
364 /* FIXME: For executables linked statically with the threads
365 library, we end up here before the program has actually been
366 executed. In that case ADDR will be garbage since it has
367 been read from the wrong virtual memory image. */
368 if (target_read_memory (addr, buf, 4) == 0)
369 {
e17a4113 370 ULONGEST magic = extract_unsigned_integer (buf, 4, byte_order);
82f5c14f
MK
371 if (magic == BSD_UTHREAD_PTHREAD_MAGIC)
372 ptid = ptid_build (ptid_get_pid (ptid), 0, addr);
373 }
374 }
375
fb5e7258
PA
376 /* If INFERIOR_PTID doesn't have a tid member yet, and we now have a
377 ptid with tid set, then ptid is still the initial thread of
378 the process. Notify GDB core about it. */
379 if (ptid_get_tid (inferior_ptid) == 0
380 && ptid_get_tid (ptid) != 0 && !in_thread_list (ptid))
381 thread_change_ptid (inferior_ptid, ptid);
382
383 /* Don't let the core see a ptid without a corresponding thread. */
384 if (!in_thread_list (ptid) || is_exited (ptid))
385 add_thread (ptid);
82f5c14f
MK
386
387 return ptid;
388}
389
390static void
28439f5e 391bsd_uthread_resume (struct target_ops *ops,
2ea28649 392 ptid_t ptid, int step, enum gdb_signal sig)
82f5c14f
MK
393{
394 /* Pass the request to the layer beneath. */
28439f5e
PA
395 struct target_ops *beneath = find_target_beneath (ops);
396 beneath->to_resume (beneath, ptid, step, sig);
82f5c14f
MK
397}
398
399static int
28439f5e 400bsd_uthread_thread_alive (struct target_ops *ops, ptid_t ptid)
82f5c14f 401{
f5656ead 402 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
28439f5e 403 struct target_ops *beneath = find_target_beneath (ops);
82f5c14f
MK
404 CORE_ADDR addr = ptid_get_tid (inferior_ptid);
405
406 if (addr != 0)
407 {
408 int offset = bsd_uthread_thread_state_offset;
409 ULONGEST state;
410
411 bsd_uthread_check_magic (addr);
412
e17a4113 413 state = read_memory_unsigned_integer (addr + offset, 4, byte_order);
82f5c14f
MK
414 if (state == BSD_UTHREAD_PS_DEAD)
415 return 0;
416 }
417
28439f5e 418 return beneath->to_thread_alive (beneath, ptid);
82f5c14f
MK
419}
420
421static void
e8032dde 422bsd_uthread_update_thread_list (struct target_ops *ops)
82f5c14f
MK
423{
424 pid_t pid = ptid_get_pid (inferior_ptid);
425 int offset = bsd_uthread_thread_next_offset;
426 CORE_ADDR addr;
427
e8032dde
PA
428 prune_threads ();
429
ff7da468 430 addr = bsd_uthread_read_memory_address (bsd_uthread_thread_list_addr);
82f5c14f
MK
431 while (addr != 0)
432 {
433 ptid_t ptid = ptid_build (pid, 0, addr);
434
fb5e7258 435 if (!in_thread_list (ptid) || is_exited (ptid))
757f359d
PA
436 {
437 /* If INFERIOR_PTID doesn't have a tid member yet, then ptid
438 is still the initial thread of the process. Notify GDB
439 core about it. */
440 if (ptid_get_tid (inferior_ptid) == 0)
441 thread_change_ptid (inferior_ptid, ptid);
442 else
443 add_thread (ptid);
444 }
82f5c14f 445
ff7da468 446 addr = bsd_uthread_read_memory_address (addr + offset);
82f5c14f
MK
447 }
448}
449
450/* Possible states a thread can be in. */
451static char *bsd_uthread_state[] =
452{
453 "RUNNING",
454 "SIGTHREAD",
455 "MUTEX_WAIT",
456 "COND_WAIT",
457 "FDLR_WAIT",
458 "FDLW_WAIT",
459 "FDR_WAIT",
460 "FDW_WAIT",
461 "FILE_WAIT",
462 "POLL_WAIT",
463 "SELECT_WAIT",
464 "SLEEP_WAIT",
465 "WAIT_WAIT",
466 "SIGSUSPEND",
467 "SIGWAIT",
468 "SPINBLOCK",
469 "JOIN",
470 "SUSPENDED",
471 "DEAD",
472 "DEADLOCK"
473};
474
475/* Return a string describing th state of the thread specified by
476 INFO. */
477
478static char *
c15906d8
TT
479bsd_uthread_extra_thread_info (struct target_ops *self,
480 struct thread_info *info)
82f5c14f 481{
f5656ead 482 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
82f5c14f
MK
483 CORE_ADDR addr = ptid_get_tid (info->ptid);
484
485 if (addr != 0)
486 {
487 int offset = bsd_uthread_thread_state_offset;
488 ULONGEST state;
489
e17a4113 490 state = read_memory_unsigned_integer (addr + offset, 4, byte_order);
82f5c14f
MK
491 if (state < ARRAY_SIZE (bsd_uthread_state))
492 return bsd_uthread_state[state];
493 }
494
495 return NULL;
496}
497
498static char *
117de6a9 499bsd_uthread_pid_to_str (struct target_ops *ops, ptid_t ptid)
82f5c14f
MK
500{
501 if (ptid_get_tid (ptid) != 0)
502 {
503 static char buf[64];
82f5c14f 504
5fff8fc0
MK
505 xsnprintf (buf, sizeof buf, "process %d, thread 0x%lx",
506 ptid_get_pid (ptid), ptid_get_tid (ptid));
82f5c14f
MK
507 return buf;
508 }
509
510 return normal_pid_to_str (ptid);
511}
512
63807e1d 513static struct target_ops *
82f5c14f
MK
514bsd_uthread_target (void)
515{
41bf6aca 516 struct target_ops *t = XCNEW (struct target_ops);
82f5c14f
MK
517
518 t->to_shortname = "bsd-uthreads";
519 t->to_longname = "BSD user-level threads";
520 t->to_doc = "BSD user-level threads";
39540081 521 t->to_close = bsd_uthread_close;
82f5c14f
MK
522 t->to_mourn_inferior = bsd_uthread_mourn_inferior;
523 t->to_fetch_registers = bsd_uthread_fetch_registers;
524 t->to_store_registers = bsd_uthread_store_registers;
82f5c14f
MK
525 t->to_wait = bsd_uthread_wait;
526 t->to_resume = bsd_uthread_resume;
527 t->to_thread_alive = bsd_uthread_thread_alive;
e8032dde 528 t->to_update_thread_list = bsd_uthread_update_thread_list;
82f5c14f
MK
529 t->to_extra_thread_info = bsd_uthread_extra_thread_info;
530 t->to_pid_to_str = bsd_uthread_pid_to_str;
531 t->to_stratum = thread_stratum;
532 t->to_magic = OPS_MAGIC;
533 bsd_uthread_ops_hack = t;
534
535 return t;
536}
537
63807e1d
PA
538/* Provide a prototype to silence -Wmissing-prototypes. */
539extern initialize_file_ftype _initialize_bsd_uthread;
540
82f5c14f
MK
541void
542_initialize_bsd_uthread (void)
543{
12070676 544 complete_target_initialization (bsd_uthread_target ());
82f5c14f
MK
545
546 bsd_uthread_data = gdbarch_data_register_pre_init (bsd_uthread_init);
547
548 observer_attach_inferior_created (bsd_uthread_inferior_created);
549 observer_attach_solib_loaded (bsd_uthread_solib_loaded);
550 observer_attach_solib_unloaded (bsd_uthread_solib_unloaded);
551}
This page took 0.929125 seconds and 4 git commands to generate.