1 /* libthread_db assisted debugging support, generic parts.
2 Copyright 1999, 2000, 2001 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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. */
23 #include "gdb_assert.h"
25 #include "gdb_proc_service.h"
26 #include "gdb_thread_db.h"
29 #include "gdbthread.h"
36 #ifndef LIBTHREAD_DB_SO
37 #define LIBTHREAD_DB_SO "libthread_db.so.1"
40 /* If we're running on Linux, we must explicitly attach to any new threads. */
42 /* FIXME: There is certainly some room for improvements:
44 - Bypass libthread_db when fetching or storing registers for
45 threads bound to a LWP. */
47 /* This module's target vector. */
48 static struct target_ops thread_db_ops
;
50 /* The target vector that we call for things this module can't handle. */
51 static struct target_ops
*target_beneath
;
53 /* Pointer to the next function on the objfile event chain. */
54 static void (*target_new_objfile_chain
) (struct objfile
*objfile
);
56 /* Non-zero if we're using this module's target vector. */
57 static int using_thread_db
;
59 /* Non-zero if we have to keep this module's target vector active
61 static int keep_thread_db
;
63 /* Non-zero if we have determined the signals used by the threads
65 static int thread_signals
;
66 static sigset_t thread_stop_set
;
67 static sigset_t thread_print_set
;
69 /* Structure that identifies the child process for the
70 <proc_service.h> interface. */
71 static struct ps_prochandle proc_handle
;
73 /* Connection to the libthread_db library. */
74 static td_thragent_t
*thread_agent
;
76 /* Pointers to the libthread_db functions. */
78 static td_err_e (*td_init_p
) (void);
80 static td_err_e (*td_ta_new_p
) (struct ps_prochandle
*ps
, td_thragent_t
**ta
);
81 static td_err_e (*td_ta_map_id2thr_p
) (const td_thragent_t
*ta
, thread_t pt
,
82 td_thrhandle_t
*__th
);
83 static td_err_e (*td_ta_map_lwp2thr_p
) (const td_thragent_t
*ta
, lwpid_t lwpid
,
85 static td_err_e (*td_ta_thr_iter_p
) (const td_thragent_t
*ta
,
86 td_thr_iter_f
*callback
,
87 void *cbdata_p
, td_thr_state_e state
,
88 int ti_pri
, sigset_t
*ti_sigmask_p
,
89 unsigned int ti_user_flags
);
90 static td_err_e (*td_ta_event_addr_p
) (const td_thragent_t
*ta
,
91 td_event_e event
, td_notify_t
*ptr
);
92 static td_err_e (*td_ta_set_event_p
) (const td_thragent_t
*ta
,
93 td_thr_events_t
*event
);
94 static td_err_e (*td_ta_event_getmsg_p
) (const td_thragent_t
*ta
,
97 static td_err_e (*td_thr_validate_p
) (const td_thrhandle_t
*th
);
98 static td_err_e (*td_thr_get_info_p
) (const td_thrhandle_t
*th
,
100 static td_err_e (*td_thr_getfpregs_p
) (const td_thrhandle_t
*th
,
101 gdb_prfpregset_t
*regset
);
102 static td_err_e (*td_thr_getgregs_p
) (const td_thrhandle_t
*th
,
104 static td_err_e (*td_thr_setfpregs_p
) (const td_thrhandle_t
*th
,
105 const gdb_prfpregset_t
*fpregs
);
106 static td_err_e (*td_thr_setgregs_p
) (const td_thrhandle_t
*th
,
108 static td_err_e (*td_thr_event_enable_p
) (const td_thrhandle_t
*th
, int event
);
110 /* Location of the thread creation event breakpoint. The code at this
111 location in the child process will be called by the pthread library
112 whenever a new thread is created. By setting a special breakpoint
113 at this location, GDB can detect when a new thread is created. We
114 obtain this location via the td_ta_event_addr call. */
115 static CORE_ADDR td_create_bp_addr
;
117 /* Location of the thread death event breakpoint. */
118 static CORE_ADDR td_death_bp_addr
;
120 /* Prototypes for local functions. */
121 static void thread_db_find_new_threads (void);
124 /* Building process ids. */
126 #define GET_PID(ptid) ptid_get_pid (ptid)
127 #define GET_LWP(ptid) ptid_get_lwp (ptid)
128 #define GET_THREAD(ptid) ptid_get_tid (ptid)
130 #define is_lwp(ptid) (GET_LWP (ptid) != 0)
131 #define is_thread(ptid) (GET_THREAD (ptid) != 0)
133 #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
134 #define BUILD_THREAD(tid, pid) ptid_build (pid, 0, tid)
137 struct private_thread_info
139 /* Cached LWP id. Must come first, see lin-lwp.c. */
145 thread_db_err_str (td_err_e err
)
152 return "generic 'call succeeded'";
154 return "generic error";
156 return "no thread to satisfy query";
158 return "no sync handle to satisfy query";
160 return "no LWP to satisfy query";
162 return "invalid process handle";
164 return "invalid thread handle";
166 return "invalid synchronization handle";
168 return "invalid thread agent";
170 return "invalid key";
172 return "no event message for getmsg";
174 return "FPU register set not available";
176 return "application not linked with libthread";
178 return "requested event is not supported";
180 return "capability not available";
182 return "debugger service failed";
184 return "operation not applicable to";
186 return "no thread-specific data for this thread";
188 return "malloc failed";
190 return "only part of register set was written/read";
192 return "X register set not available for this thread";
194 snprintf (buf
, sizeof (buf
), "unknown thread_db error '%d'", err
);
200 thread_db_state_str (td_thr_state_e state
)
207 return "stopped by debugger";
216 case TD_THR_STOPPED_ASLEEP
:
217 return "stopped by debugger AND blocked";
219 snprintf (buf
, sizeof (buf
), "unknown thread_db state %d", state
);
225 /* Convert between user-level thread ids and LWP ids. */
228 thread_from_lwp (ptid_t ptid
)
234 if (GET_LWP (ptid
) == 0)
235 ptid
= BUILD_LWP (GET_PID (ptid
), GET_PID (ptid
));
237 gdb_assert (is_lwp (ptid
));
239 err
= td_ta_map_lwp2thr_p (thread_agent
, GET_LWP (ptid
), &th
);
241 error ("Cannot find user-level thread for LWP %d: %s",
242 GET_LWP (ptid
), thread_db_err_str (err
));
244 err
= td_thr_get_info_p (&th
, &ti
);
246 error ("Cannot get thread info: %s", thread_db_err_str (err
));
248 return BUILD_THREAD (ti
.ti_tid
, GET_PID (ptid
));
252 lwp_from_thread (ptid_t ptid
)
258 if (!is_thread (ptid
))
261 err
= td_ta_map_id2thr_p (thread_agent
, GET_THREAD (ptid
), &th
);
263 error ("Cannot find thread %ld: %s",
264 (long) GET_THREAD (ptid
), thread_db_err_str (err
));
266 err
= td_thr_get_info_p (&th
, &ti
);
268 error ("Cannot get thread info: %s", thread_db_err_str (err
));
270 return BUILD_LWP (ti
.ti_lid
, GET_PID (ptid
));
275 thread_db_init (struct target_ops
*target
)
277 target_beneath
= target
;
281 thread_db_load (void)
286 handle
= dlopen (LIBTHREAD_DB_SO
, RTLD_NOW
);
290 /* Initialize pointers to the dynamic library functions we will use.
291 Essential functions first. */
293 td_init_p
= dlsym (handle
, "td_init");
294 if (td_init_p
== NULL
)
297 td_ta_new_p
= dlsym (handle
, "td_ta_new");
298 if (td_ta_new_p
== NULL
)
301 td_ta_map_id2thr_p
= dlsym (handle
, "td_ta_map_id2thr");
302 if (td_ta_map_id2thr_p
== NULL
)
305 td_ta_map_lwp2thr_p
= dlsym (handle
, "td_ta_map_lwp2thr");
306 if (td_ta_map_lwp2thr_p
== NULL
)
309 td_ta_thr_iter_p
= dlsym (handle
, "td_ta_thr_iter");
310 if (td_ta_thr_iter_p
== NULL
)
313 td_thr_validate_p
= dlsym (handle
, "td_thr_validate");
314 if (td_thr_validate_p
== NULL
)
317 td_thr_get_info_p
= dlsym (handle
, "td_thr_get_info");
318 if (td_thr_get_info_p
== NULL
)
321 td_thr_getfpregs_p
= dlsym (handle
, "td_thr_getfpregs");
322 if (td_thr_getfpregs_p
== NULL
)
325 td_thr_getgregs_p
= dlsym (handle
, "td_thr_getgregs");
326 if (td_thr_getgregs_p
== NULL
)
329 td_thr_setfpregs_p
= dlsym (handle
, "td_thr_setfpregs");
330 if (td_thr_setfpregs_p
== NULL
)
333 td_thr_setgregs_p
= dlsym (handle
, "td_thr_setgregs");
334 if (td_thr_setgregs_p
== NULL
)
337 /* Initialize the library. */
341 warning ("Cannot initialize libthread_db: %s", thread_db_err_str (err
));
345 /* These are not essential. */
346 td_ta_event_addr_p
= dlsym (handle
, "td_ta_event_addr");
347 td_ta_set_event_p
= dlsym (handle
, "td_ta_set_event");
348 td_ta_event_getmsg_p
= dlsym (handle
, "td_ta_event_getmsg");
349 td_thr_event_enable_p
= dlsym (handle
, "td_thr_event_enable");
355 enable_thread_event_reporting (void)
357 td_thr_events_t events
;
361 /* We cannot use the thread event reporting facility if these
362 functions aren't available. */
363 if (td_ta_event_addr_p
== NULL
|| td_ta_set_event_p
== NULL
364 || td_ta_event_getmsg_p
== NULL
|| td_thr_event_enable_p
== NULL
)
367 /* Set the process wide mask saying which events we're interested in. */
368 td_event_emptyset (&events
);
369 td_event_addset (&events
, TD_CREATE
);
371 /* FIXME: kettenis/2000-04-23: The event reporting facility is
372 broken for TD_DEATH events in glibc 2.1.3, so don't enable it for
374 td_event_addset (&events
, TD_DEATH
);
377 err
= td_ta_set_event_p (thread_agent
, &events
);
380 warning ("Unable to set global thread event mask: %s",
381 thread_db_err_str (err
));
385 /* Delete previous thread event breakpoints, if any. */
386 remove_thread_event_breakpoints ();
388 /* Get address for thread creation breakpoint. */
389 err
= td_ta_event_addr_p (thread_agent
, TD_CREATE
, ¬ify
);
392 warning ("Unable to get location for thread creation breakpoint: %s",
393 thread_db_err_str (err
));
397 /* Set up the breakpoint. */
398 td_create_bp_addr
= (CORE_ADDR
) notify
.u
.bptaddr
;
399 create_thread_event_breakpoint (td_create_bp_addr
);
401 /* Get address for thread death breakpoint. */
402 err
= td_ta_event_addr_p (thread_agent
, TD_DEATH
, ¬ify
);
405 warning ("Unable to get location for thread death breakpoint: %s",
406 thread_db_err_str (err
));
410 /* Set up the breakpoint. */
411 td_death_bp_addr
= (CORE_ADDR
) notify
.u
.bptaddr
;
412 create_thread_event_breakpoint (td_death_bp_addr
);
416 disable_thread_event_reporting (void)
418 td_thr_events_t events
;
420 /* Set the process wide mask saying we aren't interested in any
422 td_event_emptyset (&events
);
423 td_ta_set_event_p (thread_agent
, &events
);
425 /* Delete thread event breakpoints, if any. */
426 remove_thread_event_breakpoints ();
427 td_create_bp_addr
= 0;
428 td_death_bp_addr
= 0;
432 check_thread_signals (void)
434 #ifdef GET_THREAD_SIGNALS
440 GET_THREAD_SIGNALS (&mask
);
441 sigemptyset (&thread_stop_set
);
442 sigemptyset (&thread_print_set
);
444 for (i
= 1; i
< NSIG
; i
++)
446 if (sigismember (&mask
, i
))
448 if (signal_stop_update (target_signal_from_host (i
), 0))
449 sigaddset (&thread_stop_set
, i
);
450 if (signal_print_update (target_signal_from_host (i
), 0))
451 sigaddset (&thread_print_set
, i
);
460 disable_thread_signals (void)
462 #ifdef GET_THREAD_SIGNALS
467 for (i
= 1; i
< NSIG
; i
++)
469 if (sigismember (&thread_stop_set
, i
))
470 signal_stop_update (target_signal_from_host (i
), 1);
471 if (sigismember (&thread_print_set
, i
))
472 signal_print_update (target_signal_from_host (i
), 1);
481 thread_db_new_objfile (struct objfile
*objfile
)
487 /* All symbols have been discarded. If the thread_db target is
488 active, deactivate it now. */
491 gdb_assert (proc_handle
.pid
== 0);
492 unpush_target (&thread_db_ops
);
502 /* Nothing to do. The thread library was already detected and the
503 target vector was already activated. */
506 /* Initialize the structure that identifies the child process. Note
507 that at this point there is no guarantee that we actually have a
509 proc_handle
.pid
= GET_PID (inferior_ptid
);
511 /* Now attempt to open a connection to the thread library. */
512 err
= td_ta_new_p (&proc_handle
, &thread_agent
);
516 /* No thread library was detected. */
520 /* The thread library was detected. Activate the thread_db target. */
521 push_target (&thread_db_ops
);
524 /* If the thread library was detected in the main symbol file
525 itself, we assume that the program was statically linked
526 against the thread library and well have to keep this
527 module's target vector activated until forever... Well, at
528 least until all symbols have been discarded anyway (see
530 if (objfile
== symfile_objfile
)
532 gdb_assert (proc_handle
.pid
== 0);
536 /* We can only poke around if there actually is a child process.
537 If there is no child process alive, postpone the steps below
538 until one has been created. */
539 if (proc_handle
.pid
!= 0)
541 enable_thread_event_reporting ();
542 thread_db_find_new_threads ();
547 warning ("Cannot initialize thread debugging library: %s",
548 thread_db_err_str (err
));
553 if (target_new_objfile_chain
)
554 target_new_objfile_chain (objfile
);
558 attach_thread (ptid_t ptid
, const td_thrhandle_t
*th_p
,
559 const td_thrinfo_t
*ti_p
, int verbose
)
561 struct thread_info
*tp
;
564 check_thread_signals ();
567 printf_unfiltered ("[New %s]\n", target_pid_to_str (ptid
));
569 /* Add the thread to GDB's thread list. */
570 tp
= add_thread (ptid
);
571 tp
->private = xmalloc (sizeof (struct private_thread_info
));
572 tp
->private->lwpid
= ti_p
->ti_lid
;
574 if (ti_p
->ti_state
== TD_THR_UNKNOWN
|| ti_p
->ti_state
== TD_THR_ZOMBIE
)
575 return; /* A zombie thread -- do not attach. */
577 /* Under Linux, we have to attach to each and every thread. */
579 ATTACH_LWP (BUILD_LWP (ti_p
->ti_lid
, GET_PID (ptid
)), 0);
582 /* Enable thread event reporting for this thread. */
583 err
= td_thr_event_enable_p (th_p
, 1);
585 error ("Cannot enable thread event reporting for %s: %s",
586 target_pid_to_str (ptid
), thread_db_err_str (err
));
590 thread_db_attach (char *args
, int from_tty
)
592 target_beneath
->to_attach (args
, from_tty
);
594 /* Destroy thread info; it's no longer valid. */
597 /* The child process is now the actual multi-threaded
598 program. Snatch its process ID... */
599 proc_handle
.pid
= GET_PID (inferior_ptid
);
601 /* ...and perform the remaining initialization steps. */
602 enable_thread_event_reporting ();
603 thread_db_find_new_threads();
607 detach_thread (ptid_t ptid
, int verbose
)
610 printf_unfiltered ("[%s exited]\n", target_pid_to_str (ptid
));
614 thread_db_detach (char *args
, int from_tty
)
616 disable_thread_event_reporting ();
618 /* There's no need to save & restore inferior_ptid here, since the
619 inferior is supposed to be survive this function call. */
620 inferior_ptid
= lwp_from_thread (inferior_ptid
);
622 /* Forget about the child's process ID. We shouldn't need it
626 target_beneath
->to_detach (args
, from_tty
);
630 thread_db_resume (ptid_t ptid
, int step
, enum target_signal signo
)
632 struct cleanup
*old_chain
= save_inferior_ptid ();
634 if (GET_PID (ptid
) == -1)
635 inferior_ptid
= lwp_from_thread (inferior_ptid
);
636 else if (is_thread (ptid
))
637 ptid
= lwp_from_thread (ptid
);
639 target_beneath
->to_resume (ptid
, step
, signo
);
641 do_cleanups (old_chain
);
644 /* Check if PID is currently stopped at the location of a thread event
645 breakpoint location. If it is, read the event message and act upon
649 check_event (ptid_t ptid
)
656 /* Bail out early if we're not at a thread event breakpoint. */
657 stop_pc
= read_pc_pid (ptid
) - DECR_PC_AFTER_BREAK
;
658 if (stop_pc
!= td_create_bp_addr
&& stop_pc
!= td_death_bp_addr
)
661 err
= td_ta_event_getmsg_p (thread_agent
, &msg
);
667 error ("Cannot get thread event message: %s", thread_db_err_str (err
));
670 err
= td_thr_get_info_p (msg
.th_p
, &ti
);
672 error ("Cannot get thread info: %s", thread_db_err_str (err
));
674 ptid
= BUILD_THREAD (ti
.ti_tid
, GET_PID (ptid
));
680 /* FIXME: kettenis/2000-08-26: Since we use td_ta_event_getmsg,
681 there is no guarantee that the breakpoint will match the
682 event. Should we use td_thr_event_getmsg instead? */
684 if (stop_pc
!= td_create_bp_addr
)
685 error ("Thread creation event doesn't match breakpoint.");
688 /* We may already know about this thread, for instance when the
689 user has issued the `info threads' command before the SIGTRAP
690 for hitting the thread creation breakpoint was reported. */
691 if (!in_thread_list (ptid
))
692 attach_thread (ptid
, msg
.th_p
, &ti
, 1);
697 /* FIXME: See TD_CREATE. */
699 if (stop_pc
!= td_death_bp_addr
)
700 error ("Thread death event doesn't match breakpoint.");
703 if (!in_thread_list (ptid
))
704 error ("Spurious thread death event.");
706 detach_thread (ptid
, 1);
710 error ("Spurious thread event.");
715 thread_db_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
717 extern ptid_t trap_ptid
;
719 if (GET_PID (ptid
) != -1 && is_thread (ptid
))
720 ptid
= lwp_from_thread (ptid
);
722 ptid
= target_beneath
->to_wait (ptid
, ourstatus
);
724 if (proc_handle
.pid
== 0)
725 /* The current child process isn't the actual multi-threaded
726 program yet, so don't try to do any special thread-specific
727 post-processing and bail out early. */
730 if (ourstatus
->kind
== TARGET_WAITKIND_EXITED
)
731 return pid_to_ptid (-1);
733 if (ourstatus
->kind
== TARGET_WAITKIND_STOPPED
734 && ourstatus
->value
.sig
== TARGET_SIGNAL_TRAP
)
735 /* Check for a thread event. */
738 if (!ptid_equal (trap_ptid
, null_ptid
))
739 trap_ptid
= thread_from_lwp (trap_ptid
);
741 return thread_from_lwp (ptid
);
745 thread_db_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int write
,
746 struct mem_attrib
*attrib
,
747 struct target_ops
*target
)
749 struct cleanup
*old_chain
= save_inferior_ptid ();
752 if (is_thread (inferior_ptid
))
754 /* FIXME: This seems to be necessary to make sure breakpoints
756 if (!target_thread_alive (inferior_ptid
))
757 inferior_ptid
= pid_to_ptid (GET_PID (inferior_ptid
));
759 inferior_ptid
= lwp_from_thread (inferior_ptid
);
762 xfer
= target_beneath
->to_xfer_memory (memaddr
, myaddr
, len
, write
, attrib
, target
);
764 do_cleanups (old_chain
);
769 thread_db_fetch_registers (int regno
)
773 gdb_prfpregset_t fpregset
;
776 if (!is_thread (inferior_ptid
))
778 /* Pass the request to the target beneath us. */
779 target_beneath
->to_fetch_registers (regno
);
783 err
= td_ta_map_id2thr_p (thread_agent
, GET_THREAD (inferior_ptid
), &th
);
785 error ("Cannot find thread %ld: %s",
786 (long) GET_THREAD (inferior_ptid
), thread_db_err_str (err
));
788 err
= td_thr_getgregs_p (&th
, gregset
);
790 error ("Cannot fetch general-purpose registers for thread %ld: %s",
791 (long) GET_THREAD (inferior_ptid
), thread_db_err_str (err
));
793 err
= td_thr_getfpregs_p (&th
, &fpregset
);
795 error ("Cannot get floating-point registers for thread %ld: %s",
796 (long) GET_THREAD (inferior_ptid
), thread_db_err_str (err
));
798 /* Note that we must call supply_gregset after calling the thread_db
799 routines because the thread_db routines call ps_lgetgregs and
800 friends which clobber GDB's register cache. */
801 supply_gregset ((gdb_gregset_t
*) gregset
);
802 supply_fpregset (&fpregset
);
806 thread_db_store_registers (int regno
)
810 gdb_prfpregset_t fpregset
;
813 if (!is_thread (inferior_ptid
))
815 /* Pass the request to the target beneath us. */
816 target_beneath
->to_store_registers (regno
);
820 err
= td_ta_map_id2thr_p (thread_agent
, GET_THREAD (inferior_ptid
), &th
);
822 error ("Cannot find thread %ld: %s",
823 (long) GET_THREAD (inferior_ptid
), thread_db_err_str (err
));
827 char raw
[MAX_REGISTER_RAW_SIZE
];
829 read_register_gen (regno
, raw
);
830 thread_db_fetch_registers (-1);
831 supply_register (regno
, raw
);
834 fill_gregset ((gdb_gregset_t
*) gregset
, -1);
835 fill_fpregset (&fpregset
, -1);
837 err
= td_thr_setgregs_p (&th
, gregset
);
839 error ("Cannot store general-purpose registers for thread %ld: %s",
840 (long) GET_THREAD (inferior_ptid
), thread_db_err_str (err
));
841 err
= td_thr_setfpregs_p (&th
, &fpregset
);
843 error ("Cannot store floating-point registers for thread %ld: %s",
844 (long) GET_THREAD (inferior_ptid
), thread_db_err_str (err
));
848 thread_db_kill (void)
850 /* There's no need to save & restore inferior_ptid here, since the
851 inferior isn't supposed to survive this function call. */
852 inferior_ptid
= lwp_from_thread (inferior_ptid
);
853 target_beneath
->to_kill ();
857 thread_db_create_inferior (char *exec_file
, char *allargs
, char **env
)
861 unpush_target (&thread_db_ops
);
865 target_beneath
->to_create_inferior (exec_file
, allargs
, env
);
869 thread_db_post_startup_inferior (ptid_t ptid
)
871 if (proc_handle
.pid
== 0)
873 /* The child process is now the actual multi-threaded
874 program. Snatch its process ID... */
875 proc_handle
.pid
= GET_PID (ptid
);
877 /* ...and perform the remaining initialization steps. */
878 enable_thread_event_reporting ();
879 thread_db_find_new_threads ();
884 thread_db_mourn_inferior (void)
886 remove_thread_event_breakpoints ();
888 /* Forget about the child's process ID. We shouldn't need it
892 target_beneath
->to_mourn_inferior ();
896 thread_db_thread_alive (ptid_t ptid
)
902 if (is_thread (ptid
))
904 err
= td_ta_map_id2thr_p (thread_agent
, GET_THREAD (ptid
), &th
);
908 err
= td_thr_validate_p (&th
);
912 err
= td_thr_get_info_p (&th
, &ti
);
916 if (ti
.ti_state
== TD_THR_UNKNOWN
|| ti
.ti_state
== TD_THR_ZOMBIE
)
917 return 0; /* A zombie thread. */
922 if (target_beneath
->to_thread_alive
)
923 return target_beneath
->to_thread_alive (ptid
);
929 find_new_threads_callback (const td_thrhandle_t
*th_p
, void *data
)
935 err
= td_thr_get_info_p (th_p
, &ti
);
937 error ("Cannot get thread info: %s", thread_db_err_str (err
));
939 if (ti
.ti_state
== TD_THR_UNKNOWN
|| ti
.ti_state
== TD_THR_ZOMBIE
)
940 return 0; /* A zombie -- ignore. */
942 ptid
= BUILD_THREAD (ti
.ti_tid
, GET_PID (inferior_ptid
));
944 if (!in_thread_list (ptid
))
945 attach_thread (ptid
, th_p
, &ti
, 1);
951 thread_db_find_new_threads (void)
955 /* Iterate over all user-space threads to discover new threads. */
956 err
= td_ta_thr_iter_p (thread_agent
, find_new_threads_callback
, NULL
,
957 TD_THR_ANY_STATE
, TD_THR_LOWEST_PRIORITY
,
958 TD_SIGNO_MASK
, TD_THR_ANY_USER_FLAGS
);
960 error ("Cannot find new threads: %s", thread_db_err_str (err
));
964 thread_db_pid_to_str (ptid_t ptid
)
966 if (is_thread (ptid
))
973 err
= td_ta_map_id2thr_p (thread_agent
, GET_THREAD (ptid
), &th
);
975 error ("Cannot find thread %ld: %s",
976 (long) GET_THREAD (ptid
), thread_db_err_str (err
));
978 err
= td_thr_get_info_p (&th
, &ti
);
980 error ("Cannot get thread info for thread %ld: %s",
981 (long) GET_THREAD (ptid
), thread_db_err_str (err
));
983 if (ti
.ti_state
== TD_THR_ACTIVE
&& ti
.ti_lid
!= 0)
985 snprintf (buf
, sizeof (buf
), "Thread %ld (LWP %d)",
986 (long) ti
.ti_tid
, ti
.ti_lid
);
990 snprintf (buf
, sizeof (buf
), "Thread %ld (%s)",
991 (long) ti
.ti_tid
, thread_db_state_str (ti
.ti_state
));
997 if (target_beneath
->to_pid_to_str (ptid
))
998 return target_beneath
->to_pid_to_str (ptid
);
1000 return normal_pid_to_str (ptid
);
1004 init_thread_db_ops (void)
1006 thread_db_ops
.to_shortname
= "multi-thread";
1007 thread_db_ops
.to_longname
= "multi-threaded child process.";
1008 thread_db_ops
.to_doc
= "Threads and pthreads support.";
1009 thread_db_ops
.to_attach
= thread_db_attach
;
1010 thread_db_ops
.to_detach
= thread_db_detach
;
1011 thread_db_ops
.to_resume
= thread_db_resume
;
1012 thread_db_ops
.to_wait
= thread_db_wait
;
1013 thread_db_ops
.to_fetch_registers
= thread_db_fetch_registers
;
1014 thread_db_ops
.to_store_registers
= thread_db_store_registers
;
1015 thread_db_ops
.to_xfer_memory
= thread_db_xfer_memory
;
1016 thread_db_ops
.to_kill
= thread_db_kill
;
1017 thread_db_ops
.to_create_inferior
= thread_db_create_inferior
;
1018 thread_db_ops
.to_post_startup_inferior
= thread_db_post_startup_inferior
;
1019 thread_db_ops
.to_mourn_inferior
= thread_db_mourn_inferior
;
1020 thread_db_ops
.to_thread_alive
= thread_db_thread_alive
;
1021 thread_db_ops
.to_find_new_threads
= thread_db_find_new_threads
;
1022 thread_db_ops
.to_pid_to_str
= thread_db_pid_to_str
;
1023 thread_db_ops
.to_stratum
= thread_stratum
;
1024 thread_db_ops
.to_has_thread_control
= tc_schedlock
;
1025 thread_db_ops
.to_magic
= OPS_MAGIC
;
1029 _initialize_thread_db (void)
1031 /* Only initialize the module if we can load libthread_db. */
1032 if (thread_db_load ())
1034 init_thread_db_ops ();
1035 add_target (&thread_db_ops
);
1037 /* Add ourselves to objfile event chain. */
1038 target_new_objfile_chain
= target_new_objfile_hook
;
1039 target_new_objfile_hook
= thread_db_new_objfile
;