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