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