* target.h (target_ops): Removed to_core_file_to_sym_file vector
[deliverable/binutils-gdb.git] / gdb / thread-db.c
CommitLineData
fb0e1ba7 1/* libthread_db assisted debugging support, generic parts.
4e052eda 2 Copyright 1999, 2000, 2001 Free Software Foundation, Inc.
fb0e1ba7
MK
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21#include "defs.h"
22
23#include "gdb_assert.h"
24#include <dlfcn.h>
25#include "gdb_proc_service.h"
26#include "gdb_thread_db.h"
27
bda9cb72 28#include "bfd.h"
fb0e1ba7
MK
29#include "gdbthread.h"
30#include "inferior.h"
bda9cb72
MK
31#include "symfile.h"
32#include "objfiles.h"
fb0e1ba7 33#include "target.h"
4e052eda 34#include "regcache.h"
fb0e1ba7
MK
35
36#ifndef LIBTHREAD_DB_SO
37#define LIBTHREAD_DB_SO "libthread_db.so.1"
38#endif
39
40/* If we're running on Linux, we must explicitly attach to any new threads. */
41
42/* FIXME: There is certainly some room for improvements:
43 - Cache LWP ids.
44 - Bypass libthread_db when fetching or storing registers for
45 threads bound to a LWP. */
46
47/* This module's target vector. */
48static struct target_ops thread_db_ops;
49
50/* The target vector that we call for things this module can't handle. */
51static struct target_ops *target_beneath;
52
53/* Pointer to the next function on the objfile event chain. */
54static void (*target_new_objfile_chain) (struct objfile *objfile);
55
56/* Non-zero if we're using this module's target vector. */
57static int using_thread_db;
58
bda9cb72
MK
59/* Non-zero if we musn't deactivate this module's target vector. */
60static int keep_thread_db;
61
fb0e1ba7
MK
62/* Non-zero if we have determined the signals used by the threads
63 library. */
64static int thread_signals;
65static sigset_t thread_stop_set;
66static sigset_t thread_print_set;
67
68/* Structure that identifies the child process for the
69 <proc_service.h> interface. */
70static struct ps_prochandle proc_handle;
71
72/* Connection to the libthread_db library. */
73static td_thragent_t *thread_agent;
74
75/* Pointers to the libthread_db functions. */
76
77static td_err_e (*td_init_p) (void);
78
79static td_err_e (*td_ta_new_p) (struct ps_prochandle *ps, td_thragent_t **ta);
80static td_err_e (*td_ta_map_id2thr_p) (const td_thragent_t *ta, thread_t pt,
81 td_thrhandle_t *__th);
82static td_err_e (*td_ta_map_lwp2thr_p) (const td_thragent_t *ta, lwpid_t lwpid,
83 td_thrhandle_t *th);
84static td_err_e (*td_ta_thr_iter_p) (const td_thragent_t *ta,
85 td_thr_iter_f *callback,
86 void *cbdata_p, td_thr_state_e state,
87 int ti_pri, sigset_t *ti_sigmask_p,
88 unsigned int ti_user_flags);
89static td_err_e (*td_ta_event_addr_p) (const td_thragent_t *ta,
90 td_event_e event, td_notify_t *ptr);
91static td_err_e (*td_ta_set_event_p) (const td_thragent_t *ta,
92 td_thr_events_t *event);
93static td_err_e (*td_ta_event_getmsg_p) (const td_thragent_t *ta,
94 td_event_msg_t *msg);
95
96static td_err_e (*td_thr_validate_p) (const td_thrhandle_t *th);
97static td_err_e (*td_thr_get_info_p) (const td_thrhandle_t *th,
98 td_thrinfo_t *infop);
99static td_err_e (*td_thr_getfpregs_p) (const td_thrhandle_t *th,
100 gdb_prfpregset_t *regset);
101static td_err_e (*td_thr_getgregs_p) (const td_thrhandle_t *th,
102 prgregset_t gregs);
103static td_err_e (*td_thr_setfpregs_p) (const td_thrhandle_t *th,
104 const gdb_prfpregset_t *fpregs);
105static td_err_e (*td_thr_setgregs_p) (const td_thrhandle_t *th,
106 prgregset_t gregs);
107static td_err_e (*td_thr_event_enable_p) (const td_thrhandle_t *th, int event);
108
109/* Location of the thread creation event breakpoint. The code at this
110 location in the child process will be called by the pthread library
111 whenever a new thread is created. By setting a special breakpoint
112 at this location, GDB can detect when a new thread is created. We
113 obtain this location via the td_ta_event_addr call. */
114static CORE_ADDR td_create_bp_addr;
115
116/* Location of the thread death event breakpoint. */
117static CORE_ADDR td_death_bp_addr;
118
119/* Prototypes for local functions. */
bda9cb72 120static void thread_db_find_new_threads (void);
fb0e1ba7
MK
121\f
122
123/* Building process ids. */
124
125#ifndef TIDGET
126#define TIDGET(PID) (((PID) & 0x7fffffff) >> 16)
127#define PIDGET(PID) (((PID) & 0xffff))
128#define MERGEPID(PID, TID) (((PID) & 0xffff) | ((TID) << 16))
129#endif
130
131#define THREAD_FLAG 0x80000000
132
133#define is_lwp(pid) (((pid) & THREAD_FLAG) == 0 && TIDGET (pid))
134#define is_thread(pid) ((pid) & THREAD_FLAG)
135
136#define GET_PID(pid) PIDGET (pid)
137#define GET_LWP(pid) TIDGET (pid)
138#define GET_THREAD(pid) TIDGET (pid)
139
140#define BUILD_LWP(tid, pid) MERGEPID (pid, tid)
141#define BUILD_THREAD(tid, pid) (MERGEPID (pid, tid) | THREAD_FLAG)
142\f
143
144struct private_thread_info
145{
146 /* Cached LWP id. Must come first, see lin-lwp.c. */
147 lwpid_t lwpid;
148};
149
150\f
151/* Helper functions. */
152
153static void
154restore_inferior_pid (void *arg)
155{
156 int *saved_pid_ptr = arg;
157 inferior_pid = *saved_pid_ptr;
b8c9b27d 158 xfree (arg);
fb0e1ba7
MK
159}
160
161static struct cleanup *
162save_inferior_pid (void)
163{
164 int *saved_pid_ptr;
165
166 saved_pid_ptr = xmalloc (sizeof (int));
167 *saved_pid_ptr = inferior_pid;
168 return make_cleanup (restore_inferior_pid, saved_pid_ptr);
169}
170\f
171
172static char *
173thread_db_err_str (td_err_e err)
174{
175 static char buf[64];
176
177 switch (err)
178 {
179 case TD_OK:
180 return "generic 'call succeeded'";
181 case TD_ERR:
182 return "generic error";
183 case TD_NOTHR:
184 return "no thread to satisfy query";
185 case TD_NOSV:
186 return "no sync handle to satisfy query";
187 case TD_NOLWP:
188 return "no LWP to satisfy query";
189 case TD_BADPH:
190 return "invalid process handle";
191 case TD_BADTH:
192 return "invalid thread handle";
193 case TD_BADSH:
194 return "invalid synchronization handle";
195 case TD_BADTA:
196 return "invalid thread agent";
197 case TD_BADKEY:
198 return "invalid key";
199 case TD_NOMSG:
200 return "no event message for getmsg";
201 case TD_NOFPREGS:
202 return "FPU register set not available";
203 case TD_NOLIBTHREAD:
204 return "application not linked with libthread";
205 case TD_NOEVENT:
206 return "requested event is not supported";
207 case TD_NOCAPAB:
208 return "capability not available";
209 case TD_DBERR:
210 return "debugger service failed";
211 case TD_NOAPLIC:
212 return "operation not applicable to";
213 case TD_NOTSD:
214 return "no thread-specific data for this thread";
215 case TD_MALLOC:
216 return "malloc failed";
217 case TD_PARTIALREG:
218 return "only part of register set was written/read";
219 case TD_NOXREGS:
220 return "X register set not available for this thread";
221 default:
222 snprintf (buf, sizeof (buf), "unknown thread_db error '%d'", err);
223 return buf;
224 }
225}
226
227static char *
228thread_db_state_str (td_thr_state_e state)
229{
230 static char buf[64];
231
232 switch (state)
233 {
234 case TD_THR_STOPPED:
235 return "stopped by debugger";
236 case TD_THR_RUN:
237 return "runnable";
238 case TD_THR_ACTIVE:
239 return "active";
240 case TD_THR_ZOMBIE:
241 return "zombie";
242 case TD_THR_SLEEP:
243 return "sleeping";
244 case TD_THR_STOPPED_ASLEEP:
245 return "stopped by debugger AND blocked";
246 default:
247 snprintf (buf, sizeof (buf), "unknown thread_db state %d", state);
248 return buf;
249 }
250}
251\f
252
253/* Convert between user-level thread ids and LWP ids. */
254
255static int
256thread_from_lwp (int pid)
257{
258 td_thrinfo_t ti;
259 td_thrhandle_t th;
260 td_err_e err;
261
262 if (GET_LWP (pid) == 0)
263 pid = BUILD_LWP (pid, pid);
264
265 gdb_assert (is_lwp (pid));
266
267 err = td_ta_map_lwp2thr_p (thread_agent, GET_LWP (pid), &th);
268 if (err != TD_OK)
269 error ("Cannot find user-level thread for LWP %d: %s",
270 GET_LWP (pid), thread_db_err_str (err));
271
272 err = td_thr_get_info_p (&th, &ti);
273 if (err != TD_OK)
274 error ("Cannot get thread info: %s", thread_db_err_str (err));
275
276 return BUILD_THREAD (ti.ti_tid, GET_PID (pid));
277}
278
279static int
280lwp_from_thread (int pid)
281{
282 td_thrinfo_t ti;
283 td_thrhandle_t th;
284 td_err_e err;
285
286 if (! is_thread (pid))
287 return pid;
288
289 err = td_ta_map_id2thr_p (thread_agent, GET_THREAD (pid), &th);
290 if (err != TD_OK)
291 error ("Cannot find thread %ld: %s",
292 (long) GET_THREAD (pid), thread_db_err_str (err));
293
294 err = td_thr_get_info_p (&th, &ti);
295 if (err != TD_OK)
296 error ("Cannot get thread info: %s", thread_db_err_str (err));
297
298 return BUILD_LWP (ti.ti_lid, GET_PID (pid));
299}
300\f
301
302void
303thread_db_init (struct target_ops *target)
304{
305 target_beneath = target;
306}
307
308static int
309thread_db_load (void)
310{
311 void *handle;
312 td_err_e err;
313
314 handle = dlopen (LIBTHREAD_DB_SO, RTLD_NOW);
315 if (handle == NULL)
316 return 0;
317
318 /* Initialize pointers to the dynamic library functions we will use.
319 Essential functions first. */
320
321 td_init_p = dlsym (handle, "td_init");
322 if (td_init_p == NULL)
323 return 0;
324
325 td_ta_new_p = dlsym (handle, "td_ta_new");
326 if (td_ta_new_p == NULL)
327 return 0;
328
329 td_ta_map_id2thr_p = dlsym (handle, "td_ta_map_id2thr");
330 if (td_ta_map_id2thr_p == NULL)
331 return 0;
332
333 td_ta_map_lwp2thr_p = dlsym (handle, "td_ta_map_lwp2thr");
334 if (td_ta_map_lwp2thr_p == NULL)
335 return 0;
336
337 td_ta_thr_iter_p = dlsym (handle, "td_ta_thr_iter");
338 if (td_ta_thr_iter_p == NULL)
339 return 0;
340
341 td_thr_validate_p = dlsym (handle, "td_thr_validate");
342 if (td_thr_validate_p == NULL)
343 return 0;
344
345 td_thr_get_info_p = dlsym (handle, "td_thr_get_info");
346 if (td_thr_get_info_p == NULL)
347 return 0;
348
349 td_thr_getfpregs_p = dlsym (handle, "td_thr_getfpregs");
350 if (td_thr_getfpregs_p == NULL)
351 return 0;
352
353 td_thr_getgregs_p = dlsym (handle, "td_thr_getgregs");
354 if (td_thr_getgregs_p == NULL)
355 return 0;
356
357 td_thr_setfpregs_p = dlsym (handle, "td_thr_setfpregs");
358 if (td_thr_setfpregs_p == NULL)
359 return 0;
360
361 td_thr_setgregs_p = dlsym (handle, "td_thr_setgregs");
362 if (td_thr_setgregs_p == NULL)
363 return 0;
364
365 /* Initialize the library. */
366 err = td_init_p ();
367 if (err != TD_OK)
368 {
369 warning ("Cannot initialize libthread_db: %s", thread_db_err_str (err));
370 return 0;
371 }
372
373 /* These are not essential. */
374 td_ta_event_addr_p = dlsym (handle, "td_ta_event_addr");
375 td_ta_set_event_p = dlsym (handle, "td_ta_set_event");
376 td_ta_event_getmsg_p = dlsym (handle, "td_ta_event_getmsg");
377 td_thr_event_enable_p = dlsym (handle, "td_thr_event_enable");
378
379 return 1;
380}
381
382static void
383enable_thread_event_reporting (void)
384{
385 td_thr_events_t events;
386 td_notify_t notify;
387 td_err_e err;
388
389 /* We cannot use the thread event reporting facility if these
390 functions aren't available. */
391 if (td_ta_event_addr_p == NULL || td_ta_set_event_p == NULL
392 || td_ta_event_getmsg_p == NULL || td_thr_event_enable_p == NULL)
393 return;
394
395 /* Set the process wide mask saying which events we're interested in. */
396 td_event_emptyset (&events);
397 td_event_addset (&events, TD_CREATE);
398#if 0
399 /* FIXME: kettenis/2000-04-23: The event reporting facility is
400 broken for TD_DEATH events in glibc 2.1.3, so don't enable it for
401 now. */
402 td_event_addset (&events, TD_DEATH);
403#endif
404
405 err = td_ta_set_event_p (thread_agent, &events);
406 if (err != TD_OK)
407 {
408 warning ("Unable to set global thread event mask: %s",
409 thread_db_err_str (err));
410 return;
411 }
412
413 /* Delete previous thread event breakpoints, if any. */
414 remove_thread_event_breakpoints ();
415
416 /* Get address for thread creation breakpoint. */
417 err = td_ta_event_addr_p (thread_agent, TD_CREATE, &notify);
418 if (err != TD_OK)
419 {
420 warning ("Unable to get location for thread creation breakpoint: %s",
421 thread_db_err_str (err));
422 return;
423 }
424
425 /* Set up the breakpoint. */
426 td_create_bp_addr = (CORE_ADDR) notify.u.bptaddr;
427 create_thread_event_breakpoint (td_create_bp_addr);
428
429 /* Get address for thread death breakpoint. */
430 err = td_ta_event_addr_p (thread_agent, TD_DEATH, &notify);
431 if (err != TD_OK)
432 {
433 warning ("Unable to get location for thread creation breakpoint: %s",
434 thread_db_err_str (err));
435 return;
436 }
437
438 /* Set up the breakpoint. */
439 td_death_bp_addr = (CORE_ADDR) notify.u.bptaddr;
440 create_thread_event_breakpoint (td_death_bp_addr);
441}
442
443static void
444disable_thread_event_reporting (void)
445{
446 td_thr_events_t events;
447
448 /* Set the process wide mask saying we aren't interested in any
449 events anymore. */
450 td_event_emptyset (&events);
451 td_ta_set_event_p (thread_agent, &events);
452
453 /* Delete thread event breakpoints, if any. */
454 remove_thread_event_breakpoints ();
455 td_create_bp_addr = 0;
456 td_death_bp_addr = 0;
457}
458
459static void
460check_thread_signals (void)
461{
462#ifdef GET_THREAD_SIGNALS
463 if (! thread_signals)
464 {
465 sigset_t mask;
466 int i;
467
468 GET_THREAD_SIGNALS (&mask);
469 sigemptyset (&thread_stop_set);
470 sigemptyset (&thread_print_set);
471
472 for (i = 0; i < NSIG; i++)
473 {
474 if (sigismember (&mask, i))
475 {
476 if (signal_stop_update (target_signal_from_host (i), 0))
477 sigaddset (&thread_stop_set, i);
478 if (signal_print_update (target_signal_from_host (i), 0))
479 sigaddset (&thread_print_set, i);
480 thread_signals = 1;
481 }
482 }
483 }
484#endif
485}
486
487static void
488disable_thread_signals (void)
489{
490#ifdef GET_THREAD_SIGNALS
491 if (thread_signals)
492 {
493 int i;
494
495 for (i = 0; i < NSIG; i++)
496 {
497 if (sigismember (&thread_stop_set, i))
498 signal_stop_update (target_signal_from_host (i), 1);
499 if (sigismember (&thread_print_set, i))
500 signal_print_update (target_signal_from_host (i), 1);
501 }
502
503 thread_signals = 0;
504 }
505#endif
506}
507
508static void
bda9cb72 509deactivate_target (void)
fb0e1ba7 510{
bda9cb72
MK
511 /* Forget about the child's process ID. We shouldn't need it
512 anymore. */
513 proc_handle.pid = 0;
fb0e1ba7 514
bda9cb72
MK
515 if (! keep_thread_db)
516 {
517 using_thread_db = 0;
518 unpush_target (&thread_db_ops);
519 }
fb0e1ba7
MK
520}
521
522static void
523thread_db_new_objfile (struct objfile *objfile)
524{
525 td_err_e err;
526
bda9cb72
MK
527 if (objfile == NULL)
528 {
529 /* All symbols have been discarded. If the thread_db target is
530 active, deactivate it now, even if the application was linked
531 statically against the thread library. */
532 keep_thread_db = 0;
533 if (using_thread_db)
534 deactivate_target ();
535
536 goto quit;
537 }
538
fb0e1ba7
MK
539 if (using_thread_db)
540 /* Nothing to do. The thread library was already detected and the
541 target vector was already activated. */
542 goto quit;
543
bda9cb72
MK
544 /* Initialize the structure that identifies the child process. Note
545 that at this point there is no guarantee that we actually have a
546 child process. */
fb0e1ba7
MK
547 proc_handle.pid = GET_PID (inferior_pid);
548
bda9cb72 549 /* Now attempt to open a connection to the thread library. */
fb0e1ba7
MK
550 err = td_ta_new_p (&proc_handle, &thread_agent);
551 switch (err)
552 {
553 case TD_NOLIBTHREAD:
bda9cb72 554 /* No thread library was detected. */
fb0e1ba7
MK
555 break;
556
557 case TD_OK:
bda9cb72
MK
558 /* The thread library was detected. Activate the thread_db target. */
559 push_target (&thread_db_ops);
560 using_thread_db = 1;
561
562 /* If the thread library was detected in the main symbol file
563 itself, we assume that the program was statically linked
564 against the thread library and well have to keep this
565 module's target vector activated until forever... Well, at
566 least until all symbols have been discarded anyway (see
567 above). */
568 if (objfile == symfile_objfile)
569 {
570 gdb_assert (proc_handle.pid == 0);
571 keep_thread_db = 1;
572 }
573
574 /* We can only poke around if there actually is a child process.
575 If there is no child process alive, postpone the steps below
576 until one has been created. */
577 if (proc_handle.pid != 0)
578 {
579 enable_thread_event_reporting ();
580 thread_db_find_new_threads ();
581 }
fb0e1ba7
MK
582 break;
583
584 default:
585 warning ("Cannot initialize thread debugging library: %s",
586 thread_db_err_str (err));
587 break;
588 }
589
590 quit:
591 if (target_new_objfile_chain)
592 target_new_objfile_chain (objfile);
593}
594
595static void
596attach_thread (int pid, const td_thrhandle_t *th_p,
597 const td_thrinfo_t *ti_p, int verbose)
598{
599 struct thread_info *tp;
600 td_err_e err;
601
602 check_thread_signals ();
603
604 if (verbose)
605 printf_unfiltered ("[New %s]\n", target_pid_to_str (pid));
606
607 /* Add the thread to GDB's thread list. */
608 tp = add_thread (pid);
609 tp->private = xmalloc (sizeof (struct private_thread_info));
610 tp->private->lwpid = ti_p->ti_lid;
611
612 /* Under Linux, we have to attach to each and every thread. */
613#ifdef ATTACH_LWP
614 if (ti_p->ti_lid != GET_PID (pid))
615 ATTACH_LWP (BUILD_LWP (ti_p->ti_lid, GET_PID (pid)), 0);
616#endif
617
618 /* Enable thread event reporting for this thread. */
619 err = td_thr_event_enable_p (th_p, 1);
620 if (err != TD_OK)
621 error ("Cannot enable thread event reporting for %s: %s",
622 target_pid_to_str (pid), thread_db_err_str (err));
623}
624
625static void
626detach_thread (int pid, int verbose)
627{
628 if (verbose)
629 printf_unfiltered ("[%s exited]\n", target_pid_to_str (pid));
630}
631
632static void
633thread_db_detach (char *args, int from_tty)
634{
635 disable_thread_event_reporting ();
bda9cb72 636 deactivate_target ();
fb0e1ba7
MK
637
638 target_beneath->to_detach (args, from_tty);
639}
640
641static void
642thread_db_resume (int pid, int step, enum target_signal signo)
643{
644 struct cleanup *old_chain = save_inferior_pid ();
645
646 if (pid == -1)
647 inferior_pid = lwp_from_thread (inferior_pid);
648 else if (is_thread (pid))
649 pid = lwp_from_thread (pid);
650
651 target_beneath->to_resume (pid, step, signo);
652
653 do_cleanups (old_chain);
654}
655
656/* Check if PID is currently stopped at the location of a thread event
657 breakpoint location. If it is, read the event message and act upon
658 the event. */
659
660static void
661check_event (int pid)
662{
663 td_event_msg_t msg;
664 td_thrinfo_t ti;
665 td_err_e err;
666 CORE_ADDR stop_pc;
667
668 /* Bail out early if we're not at a thread event breakpoint. */
669 stop_pc = read_pc_pid (pid) - DECR_PC_AFTER_BREAK;
670 if (stop_pc != td_create_bp_addr && stop_pc != td_death_bp_addr)
671 return;
672
673 err = td_ta_event_getmsg_p (thread_agent, &msg);
674 if (err != TD_OK)
675 {
676 if (err == TD_NOMSG)
677 return;
678
679 error ("Cannot get thread event message: %s", thread_db_err_str (err));
680 }
681
682 err = td_thr_get_info_p (msg.th_p, &ti);
683 if (err != TD_OK)
684 error ("Cannot get thread info: %s", thread_db_err_str (err));
685
686 pid = BUILD_THREAD (ti.ti_tid, GET_PID (pid));
687
688 switch (msg.event)
689 {
690 case TD_CREATE:
691#if 0
692 /* FIXME: kettenis/2000-08-26: Since we use td_ta_event_getmsg,
693 there is no guarantee that the breakpoint will match the
694 event. Should we use td_thr_event_getmsg instead? */
695
696 if (stop_pc != td_create_bp_addr)
697 error ("Thread creation event doesn't match breakpoint.");
698#endif
699
95a85621
MK
700 /* We may already know about this thread, for instance when the
701 user has issued the `info threads' command before the SIGTRAP
702 for hitting the thread creation breakpoint was reported. */
703 if (! in_thread_list (pid))
704 attach_thread (pid, msg.th_p, &ti, 1);
fb0e1ba7
MK
705 return;
706
707 case TD_DEATH:
708#if 0
709 /* FIXME: See TD_CREATE. */
710
711 if (stop_pc != td_death_bp_addr)
712 error ("Thread death event doesn't match breakpoint.");
713#endif
714
715 if (! in_thread_list (pid))
716 error ("Spurious thread death event.");
717
718 detach_thread (pid, 1);
719 return;
720
721 default:
722 error ("Spurious thread event.");
723 }
724}
725
726static int
727thread_db_wait (int pid, struct target_waitstatus *ourstatus)
728{
729 extern int trap_pid;
730
731 if (pid != -1 && is_thread (pid))
732 pid = lwp_from_thread (pid);
733
734 pid = target_beneath->to_wait (pid, ourstatus);
735
bda9cb72
MK
736 if (proc_handle.pid == 0)
737 /* The current child process isn't the actual multi-threaded
738 program yet, so don't try to do any special thread-specific
739 post-processing and bail out early. */
740 return pid;
741
fb0e1ba7
MK
742 if (ourstatus->kind == TARGET_WAITKIND_EXITED)
743 return -1;
744
745 if (ourstatus->kind == TARGET_WAITKIND_STOPPED
746 && ourstatus->value.sig == TARGET_SIGNAL_TRAP)
747 /* Check for a thread event. */
748 check_event (pid);
749
750 if (trap_pid)
751 trap_pid = thread_from_lwp (trap_pid);
752
753 return thread_from_lwp (pid);
754}
755
756static int
757thread_db_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
e5da8f38 758 struct mem_attrib *attrib,
fb0e1ba7
MK
759 struct target_ops *target)
760{
761 struct cleanup *old_chain = save_inferior_pid ();
762 int xfer;
763
764 if (is_thread (inferior_pid))
765 {
766 /* FIXME: This seems to be necessary to make sure breakpoints
767 are removed. */
768 if (! target_thread_alive (inferior_pid))
769 inferior_pid = GET_PID (inferior_pid);
770 else
771 inferior_pid = lwp_from_thread (inferior_pid);
772 }
773
e5da8f38 774 xfer = target_beneath->to_xfer_memory (memaddr, myaddr, len, write, attrib, target);
fb0e1ba7
MK
775
776 do_cleanups (old_chain);
777 return xfer;
778}
779
780static void
781thread_db_fetch_registers (int regno)
782{
783 td_thrhandle_t th;
784 prgregset_t gregset;
785 gdb_prfpregset_t fpregset;
786 td_err_e err;
787
788 if (! is_thread (inferior_pid))
789 {
790 /* Pass the request to the target beneath us. */
791 target_beneath->to_fetch_registers (regno);
792 return;
793 }
794
795 err = td_ta_map_id2thr_p (thread_agent, GET_THREAD (inferior_pid), &th);
796 if (err != TD_OK)
797 error ("Cannot find thread %ld: %s",
798 (long) GET_THREAD (inferior_pid), thread_db_err_str (err));
799
800 err = td_thr_getgregs_p (&th, gregset);
801 if (err != TD_OK)
802 error ("Cannot fetch general-purpose registers for thread %ld: %s",
803 (long) GET_THREAD (inferior_pid), thread_db_err_str (err));
804
805 err = td_thr_getfpregs_p (&th, &fpregset);
806 if (err != TD_OK)
807 error ("Cannot get floating-point registers for thread %ld: %s",
808 (long) GET_THREAD (inferior_pid), thread_db_err_str (err));
809
810 /* Note that we must call supply_gregset after calling the thread_db
811 routines because the thread_db routines call ps_lgetgregs and
812 friends which clobber GDB's register cache. */
813 supply_gregset ((gdb_gregset_t *) gregset);
814 supply_fpregset (&fpregset);
815}
816
817static void
818thread_db_store_registers (int regno)
819{
820 td_thrhandle_t th;
821 prgregset_t gregset;
822 gdb_prfpregset_t fpregset;
823 td_err_e err;
824
825 if (! is_thread (inferior_pid))
826 {
827 /* Pass the request to the target beneath us. */
828 target_beneath->to_store_registers (regno);
829 return;
830 }
831
832 err = td_ta_map_id2thr_p (thread_agent, GET_THREAD (inferior_pid), &th);
833 if (err != TD_OK)
834 error ("Cannot find thread %ld: %s",
835 (long) GET_THREAD (inferior_pid), thread_db_err_str (err));
836
837 if (regno != -1)
838 {
839 char raw[MAX_REGISTER_RAW_SIZE];
840
841 read_register_gen (regno, raw);
842 thread_db_fetch_registers (-1);
843 supply_register (regno, raw);
844 }
845
846 fill_gregset ((gdb_gregset_t *) gregset, -1);
847 fill_fpregset (&fpregset, -1);
848
849 err = td_thr_setgregs_p (&th, gregset);
850 if (err != TD_OK)
851 error ("Cannot store general-purpose registers for thread %ld: %s",
852 (long) GET_THREAD (inferior_pid), thread_db_err_str (err));
853 err = td_thr_setfpregs_p (&th, &fpregset);
854 if (err != TD_OK)
855 error ("Cannot store floating-point registers for thread %ld: %s",
856 (long) GET_THREAD (inferior_pid), thread_db_err_str (err));
857}
858
859static void
860thread_db_kill (void)
861{
862 target_beneath->to_kill ();
863}
864
865static void
866thread_db_create_inferior (char *exec_file, char *allargs, char **env)
867{
bda9cb72
MK
868 target_beneath->to_create_inferior (exec_file, allargs, env);
869}
fb0e1ba7 870
bda9cb72
MK
871static void
872thread_db_post_startup_inferior (int pid)
873{
874 if (proc_handle.pid == 0)
875 {
876 /* The child process is now the actual multi-threaded
877 program. Snatch its process ID... */
878 proc_handle.pid = GET_PID (pid);
fb0e1ba7 879
bda9cb72
MK
880 /* ...and perform the remaining initialization steps. */
881 enable_thread_event_reporting ();
882 thread_db_find_new_threads();
883 }
fb0e1ba7
MK
884}
885
886static void
887thread_db_mourn_inferior (void)
888{
889 remove_thread_event_breakpoints ();
bda9cb72 890 deactivate_target ();
fb0e1ba7
MK
891
892 target_beneath->to_mourn_inferior ();
893}
894
895static int
896thread_db_thread_alive (int pid)
897{
898 if (is_thread (pid))
899 {
900 td_thrhandle_t th;
901 td_err_e err;
902
903 err = td_ta_map_id2thr_p (thread_agent, GET_THREAD (pid), &th);
904 if (err != TD_OK)
905 return 0;
906
907 err = td_thr_validate_p (&th);
908 if (err != TD_OK)
909 return 0;
910
911 return 1;
912 }
913
914 if (target_beneath->to_thread_alive)
915 return target_beneath->to_thread_alive (pid);
916
917 return 0;
918}
919
920static int
921find_new_threads_callback (const td_thrhandle_t *th_p, void *data)
922{
923 td_thrinfo_t ti;
924 td_err_e err;
925 int pid;
926
927 err = td_thr_get_info_p (th_p, &ti);
928 if (err != TD_OK)
929 error ("Cannot get thread info: %s", thread_db_err_str (err));
930
931 pid = BUILD_THREAD (ti.ti_tid, GET_PID (inferior_pid));
932
933 if (! in_thread_list (pid))
934 attach_thread (pid, th_p, &ti, 1);
935
936 return 0;
937}
938
939static void
940thread_db_find_new_threads (void)
941{
942 td_err_e err;
943
944 /* Iterate over all user-space threads to discover new threads. */
945 err = td_ta_thr_iter_p (thread_agent, find_new_threads_callback, NULL,
946 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
947 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
948 if (err != TD_OK)
949 error ("Cannot find new threads: %s", thread_db_err_str (err));
950}
951
952static char *
953thread_db_pid_to_str (int pid)
954{
955 if (is_thread (pid))
956 {
957 static char buf[64];
958 td_thrhandle_t th;
959 td_thrinfo_t ti;
960 td_err_e err;
961
962 err = td_ta_map_id2thr_p (thread_agent, GET_THREAD (pid), &th);
963 if (err != TD_OK)
964 error ("Cannot find thread %ld: %s",
965 (long) GET_THREAD (pid), thread_db_err_str (err));
966
967 err = td_thr_get_info_p (&th, &ti);
968 if (err != TD_OK)
969 error ("Cannot get thread info for thread %ld: %s",
970 (long) GET_THREAD (pid), thread_db_err_str (err));
971
972 if (ti.ti_state == TD_THR_ACTIVE && ti.ti_lid != 0)
973 {
974 snprintf (buf, sizeof (buf), "Thread %ld (LWP %d)",
975 (long) ti.ti_tid, ti.ti_lid);
976 }
977 else
978 {
979 snprintf (buf, sizeof (buf), "Thread %ld (%s)",
980 (long) ti.ti_tid, thread_db_state_str (ti.ti_state));
981 }
982
983 return buf;
984 }
985
986 if (target_beneath->to_pid_to_str (pid))
987 return target_beneath->to_pid_to_str (pid);
988
989 return normal_pid_to_str (pid);
990}
991
992static void
993init_thread_db_ops (void)
994{
995 thread_db_ops.to_shortname = "multi-thread";
996 thread_db_ops.to_longname = "multi-threaded child process.";
997 thread_db_ops.to_doc = "Threads and pthreads support.";
998 thread_db_ops.to_detach = thread_db_detach;
999 thread_db_ops.to_resume = thread_db_resume;
1000 thread_db_ops.to_wait = thread_db_wait;
1001 thread_db_ops.to_fetch_registers = thread_db_fetch_registers;
1002 thread_db_ops.to_store_registers = thread_db_store_registers;
1003 thread_db_ops.to_xfer_memory = thread_db_xfer_memory;
1004 thread_db_ops.to_kill = thread_db_kill;
1005 thread_db_ops.to_create_inferior = thread_db_create_inferior;
bda9cb72 1006 thread_db_ops.to_post_startup_inferior = thread_db_post_startup_inferior;
fb0e1ba7
MK
1007 thread_db_ops.to_mourn_inferior = thread_db_mourn_inferior;
1008 thread_db_ops.to_thread_alive = thread_db_thread_alive;
1009 thread_db_ops.to_find_new_threads = thread_db_find_new_threads;
1010 thread_db_ops.to_pid_to_str = thread_db_pid_to_str;
1011 thread_db_ops.to_stratum = thread_stratum;
1012 thread_db_ops.to_has_thread_control = tc_schedlock;
1013 thread_db_ops.to_magic = OPS_MAGIC;
1014}
1015
1016void
1017_initialize_thread_db (void)
1018{
1019 /* Only initialize the module if we can load libthread_db. */
1020 if (thread_db_load ())
1021 {
1022 init_thread_db_ops ();
1023 add_target (&thread_db_ops);
1024
1025 /* Add ourselves to objfile event chain. */
1026 target_new_objfile_chain = target_new_objfile_hook;
1027 target_new_objfile_hook = thread_db_new_objfile;
1028 }
1029}
This page took 0.091621 seconds and 4 git commands to generate.