1 /* libthread_db assisted debugging support, generic parts.
3 Copyright 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
5 This file is part of GDB.
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
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
24 #include "gdb_assert.h"
26 #include "gdb_proc_service.h"
27 #include "gdb_thread_db.h"
30 #include "gdbthread.h"
36 #include "solib-svr4.h"
38 #ifndef LIBTHREAD_DB_SO
39 #define LIBTHREAD_DB_SO "libthread_db.so.1"
42 /* If we're running on GNU/Linux, we must explicitly attach to any new
45 /* FIXME: There is certainly some room for improvements:
47 - Bypass libthread_db when fetching or storing registers for
48 threads bound to a LWP. */
50 /* This module's target vector. */
51 static struct target_ops thread_db_ops
;
53 /* The target vector that we call for things this module can't handle. */
54 static struct target_ops
*target_beneath
;
56 /* Pointer to the next function on the objfile event chain. */
57 static void (*target_new_objfile_chain
) (struct objfile
*objfile
);
59 /* Non-zero if we're using this module's target vector. */
60 static int using_thread_db
;
62 /* Non-zero if we have to keep this module's target vector active
64 static int keep_thread_db
;
66 /* Non-zero if we have determined the signals used by the threads
68 static int thread_signals
;
69 static sigset_t thread_stop_set
;
70 static sigset_t thread_print_set
;
72 /* Structure that identifies the child process for the
73 <proc_service.h> interface. */
74 static struct ps_prochandle proc_handle
;
76 /* Connection to the libthread_db library. */
77 static td_thragent_t
*thread_agent
;
79 /* Pointers to the libthread_db functions. */
81 static td_err_e (*td_init_p
) (void);
83 static td_err_e (*td_ta_new_p
) (struct ps_prochandle
*ps
, td_thragent_t
**ta
);
84 static td_err_e (*td_ta_map_id2thr_p
) (const td_thragent_t
*ta
, thread_t pt
,
85 td_thrhandle_t
*__th
);
86 static td_err_e (*td_ta_map_lwp2thr_p
) (const td_thragent_t
*ta
, lwpid_t lwpid
,
88 static td_err_e (*td_ta_thr_iter_p
) (const td_thragent_t
*ta
,
89 td_thr_iter_f
*callback
,
90 void *cbdata_p
, td_thr_state_e state
,
91 int ti_pri
, sigset_t
*ti_sigmask_p
,
92 unsigned int ti_user_flags
);
93 static td_err_e (*td_ta_event_addr_p
) (const td_thragent_t
*ta
,
94 td_event_e event
, td_notify_t
*ptr
);
95 static td_err_e (*td_ta_set_event_p
) (const td_thragent_t
*ta
,
96 td_thr_events_t
*event
);
97 static td_err_e (*td_ta_event_getmsg_p
) (const td_thragent_t
*ta
,
100 static td_err_e (*td_thr_validate_p
) (const td_thrhandle_t
*th
);
101 static td_err_e (*td_thr_get_info_p
) (const td_thrhandle_t
*th
,
102 td_thrinfo_t
*infop
);
103 static td_err_e (*td_thr_getfpregs_p
) (const td_thrhandle_t
*th
,
104 gdb_prfpregset_t
*regset
);
105 static td_err_e (*td_thr_getgregs_p
) (const td_thrhandle_t
*th
,
107 static td_err_e (*td_thr_setfpregs_p
) (const td_thrhandle_t
*th
,
108 const gdb_prfpregset_t
*fpregs
);
109 static td_err_e (*td_thr_setgregs_p
) (const td_thrhandle_t
*th
,
111 static td_err_e (*td_thr_event_enable_p
) (const td_thrhandle_t
*th
, int event
);
113 static td_err_e (*td_thr_tls_get_addr_p
) (const td_thrhandle_t
*th
,
118 /* Location of the thread creation event breakpoint. The code at this
119 location in the child process will be called by the pthread library
120 whenever a new thread is created. By setting a special breakpoint
121 at this location, GDB can detect when a new thread is created. We
122 obtain this location via the td_ta_event_addr call. */
123 static CORE_ADDR td_create_bp_addr
;
125 /* Location of the thread death event breakpoint. */
126 static CORE_ADDR td_death_bp_addr
;
128 /* Prototypes for local functions. */
129 static void thread_db_find_new_threads (void);
130 static void attach_thread (ptid_t ptid
, const td_thrhandle_t
*th_p
,
131 const td_thrinfo_t
*ti_p
, int verbose
);
134 /* Building process ids. */
136 #define GET_PID(ptid) ptid_get_pid (ptid)
137 #define GET_LWP(ptid) ptid_get_lwp (ptid)
138 #define GET_THREAD(ptid) ptid_get_tid (ptid)
140 #define is_lwp(ptid) (GET_LWP (ptid) != 0)
141 #define is_thread(ptid) (GET_THREAD (ptid) != 0)
143 #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
144 #define BUILD_THREAD(tid, pid) ptid_build (pid, 0, tid)
147 /* Use "struct private_thread_info" to cache thread state. This is
148 a substantial optimization. */
150 struct private_thread_info
152 /* Cached thread state. */
153 unsigned int th_valid
: 1;
154 unsigned int ti_valid
: 1;
162 thread_db_err_str (td_err_e err
)
169 return "generic 'call succeeded'";
171 return "generic error";
173 return "no thread to satisfy query";
175 return "no sync handle to satisfy query";
177 return "no LWP to satisfy query";
179 return "invalid process handle";
181 return "invalid thread handle";
183 return "invalid synchronization handle";
185 return "invalid thread agent";
187 return "invalid key";
189 return "no event message for getmsg";
191 return "FPU register set not available";
193 return "application not linked with libthread";
195 return "requested event is not supported";
197 return "capability not available";
199 return "debugger service failed";
201 return "operation not applicable to";
203 return "no thread-specific data for this thread";
205 return "malloc failed";
207 return "only part of register set was written/read";
209 return "X register set not available for this thread";
211 snprintf (buf
, sizeof (buf
), "unknown thread_db error '%d'", err
);
217 thread_db_state_str (td_thr_state_e state
)
224 return "stopped by debugger";
233 case TD_THR_STOPPED_ASLEEP
:
234 return "stopped by debugger AND blocked";
236 snprintf (buf
, sizeof (buf
), "unknown thread_db state %d", state
);
241 /* A callback function for td_ta_thr_iter, which we use to map all
244 THP is a handle to the current thread; if INFOP is not NULL, the
245 struct thread_info associated with this thread is returned in
249 thread_get_info_callback (const td_thrhandle_t
*thp
, void *infop
)
253 struct thread_info
*thread_info
;
256 err
= td_thr_get_info_p (thp
, &ti
);
258 error ("thread_get_info_callback: cannot get thread info: %s",
259 thread_db_err_str (err
));
261 /* Fill the cache. */
262 thread_ptid
= BUILD_THREAD (ti
.ti_tid
, GET_PID (inferior_ptid
));
263 thread_info
= find_thread_pid (thread_ptid
);
265 if (thread_info
== NULL
)
267 /* New thread. Attach to it now (why wait?). */
268 attach_thread (thread_ptid
, thp
, &ti
, 1);
269 thread_info
= find_thread_pid (thread_ptid
);
270 gdb_assert (thread_info
!= NULL
);
273 memcpy (&thread_info
->private->th
, thp
, sizeof (*thp
));
274 thread_info
->private->th_valid
= 1;
275 memcpy (&thread_info
->private->ti
, &ti
, sizeof (ti
));
276 thread_info
->private->ti_valid
= 1;
279 *(struct thread_info
**) infop
= thread_info
;
284 /* Accessor functions for the thread_db information, with caching. */
287 thread_db_map_id2thr (struct thread_info
*thread_info
, int fatal
)
291 if (thread_info
->private->th_valid
)
294 err
= td_ta_map_id2thr_p (thread_agent
, GET_THREAD (thread_info
->ptid
),
295 &thread_info
->private->th
);
299 error ("Cannot find thread %ld: %s",
300 (long) GET_THREAD (thread_info
->ptid
), thread_db_err_str (err
));
303 thread_info
->private->th_valid
= 1;
306 static td_thrinfo_t
*
307 thread_db_get_info (struct thread_info
*thread_info
)
311 if (thread_info
->private->ti_valid
)
312 return &thread_info
->private->ti
;
314 if (! thread_info
->private->th_valid
)
315 thread_db_map_id2thr (thread_info
, 1);
317 err
= td_thr_get_info_p (&thread_info
->private->th
, &thread_info
->private->ti
);
319 error ("thread_db_get_info: cannot get thread info: %s",
320 thread_db_err_str (err
));
322 thread_info
->private->ti_valid
= 1;
323 return &thread_info
->private->ti
;
326 /* Convert between user-level thread ids and LWP ids. */
329 thread_from_lwp (ptid_t ptid
)
333 struct thread_info
*thread_info
;
336 if (GET_LWP (ptid
) == 0)
337 ptid
= BUILD_LWP (GET_PID (ptid
), GET_PID (ptid
));
339 gdb_assert (is_lwp (ptid
));
341 err
= td_ta_map_lwp2thr_p (thread_agent
, GET_LWP (ptid
), &th
);
343 error ("Cannot find user-level thread for LWP %ld: %s",
344 GET_LWP (ptid
), thread_db_err_str (err
));
347 thread_get_info_callback (&th
, &thread_info
);
348 gdb_assert (thread_info
&& thread_info
->private->ti_valid
);
350 return BUILD_THREAD (thread_info
->private->ti
.ti_tid
, GET_PID (ptid
));
354 lwp_from_thread (ptid_t ptid
)
356 struct thread_info
*thread_info
;
359 if (!is_thread (ptid
))
362 thread_info
= find_thread_pid (ptid
);
363 thread_db_get_info (thread_info
);
365 return BUILD_LWP (thread_info
->private->ti
.ti_lid
, GET_PID (ptid
));
370 thread_db_init (struct target_ops
*target
)
372 target_beneath
= target
;
376 thread_db_load (void)
381 handle
= dlopen (LIBTHREAD_DB_SO
, RTLD_NOW
);
384 fprintf_filtered (gdb_stderr
, "\n\ndlopen failed on '%s' - %s\n",
385 LIBTHREAD_DB_SO
, dlerror ());
386 fprintf_filtered (gdb_stderr
,
387 "GDB will not be able to debug pthreads.\n\n");
391 /* Initialize pointers to the dynamic library functions we will use.
392 Essential functions first. */
394 td_init_p
= dlsym (handle
, "td_init");
395 if (td_init_p
== NULL
)
398 td_ta_new_p
= dlsym (handle
, "td_ta_new");
399 if (td_ta_new_p
== NULL
)
402 td_ta_map_id2thr_p
= dlsym (handle
, "td_ta_map_id2thr");
403 if (td_ta_map_id2thr_p
== NULL
)
406 td_ta_map_lwp2thr_p
= dlsym (handle
, "td_ta_map_lwp2thr");
407 if (td_ta_map_lwp2thr_p
== NULL
)
410 td_ta_thr_iter_p
= dlsym (handle
, "td_ta_thr_iter");
411 if (td_ta_thr_iter_p
== NULL
)
414 td_thr_validate_p
= dlsym (handle
, "td_thr_validate");
415 if (td_thr_validate_p
== NULL
)
418 td_thr_get_info_p
= dlsym (handle
, "td_thr_get_info");
419 if (td_thr_get_info_p
== NULL
)
422 td_thr_getfpregs_p
= dlsym (handle
, "td_thr_getfpregs");
423 if (td_thr_getfpregs_p
== NULL
)
426 td_thr_getgregs_p
= dlsym (handle
, "td_thr_getgregs");
427 if (td_thr_getgregs_p
== NULL
)
430 td_thr_setfpregs_p
= dlsym (handle
, "td_thr_setfpregs");
431 if (td_thr_setfpregs_p
== NULL
)
434 td_thr_setgregs_p
= dlsym (handle
, "td_thr_setgregs");
435 if (td_thr_setgregs_p
== NULL
)
438 /* Initialize the library. */
442 warning ("Cannot initialize libthread_db: %s", thread_db_err_str (err
));
446 /* These are not essential. */
447 td_ta_event_addr_p
= dlsym (handle
, "td_ta_event_addr");
448 td_ta_set_event_p
= dlsym (handle
, "td_ta_set_event");
449 td_ta_event_getmsg_p
= dlsym (handle
, "td_ta_event_getmsg");
450 td_thr_event_enable_p
= dlsym (handle
, "td_thr_event_enable");
451 td_thr_tls_get_addr_p
= dlsym (handle
, "td_thr_tls_get_addr");
457 enable_thread_event_reporting (void)
459 td_thr_events_t events
;
463 /* We cannot use the thread event reporting facility if these
464 functions aren't available. */
465 if (td_ta_event_addr_p
== NULL
|| td_ta_set_event_p
== NULL
466 || td_ta_event_getmsg_p
== NULL
|| td_thr_event_enable_p
== NULL
)
469 /* Set the process wide mask saying which events we're interested in. */
470 td_event_emptyset (&events
);
471 td_event_addset (&events
, TD_CREATE
);
473 /* FIXME: kettenis/2000-04-23: The event reporting facility is
474 broken for TD_DEATH events in glibc 2.1.3, so don't enable it for
476 td_event_addset (&events
, TD_DEATH
);
479 err
= td_ta_set_event_p (thread_agent
, &events
);
482 warning ("Unable to set global thread event mask: %s",
483 thread_db_err_str (err
));
487 /* Delete previous thread event breakpoints, if any. */
488 remove_thread_event_breakpoints ();
490 /* Get address for thread creation breakpoint. */
491 err
= td_ta_event_addr_p (thread_agent
, TD_CREATE
, ¬ify
);
494 warning ("Unable to get location for thread creation breakpoint: %s",
495 thread_db_err_str (err
));
499 /* Set up the breakpoint. */
500 td_create_bp_addr
= (CORE_ADDR
) notify
.u
.bptaddr
;
501 create_thread_event_breakpoint (td_create_bp_addr
);
503 /* Get address for thread death breakpoint. */
504 err
= td_ta_event_addr_p (thread_agent
, TD_DEATH
, ¬ify
);
507 warning ("Unable to get location for thread death breakpoint: %s",
508 thread_db_err_str (err
));
512 /* Set up the breakpoint. */
513 td_death_bp_addr
= (CORE_ADDR
) notify
.u
.bptaddr
;
514 create_thread_event_breakpoint (td_death_bp_addr
);
518 disable_thread_event_reporting (void)
520 td_thr_events_t events
;
522 /* Set the process wide mask saying we aren't interested in any
524 td_event_emptyset (&events
);
525 td_ta_set_event_p (thread_agent
, &events
);
527 /* Delete thread event breakpoints, if any. */
528 remove_thread_event_breakpoints ();
529 td_create_bp_addr
= 0;
530 td_death_bp_addr
= 0;
534 check_thread_signals (void)
536 #ifdef GET_THREAD_SIGNALS
542 GET_THREAD_SIGNALS (&mask
);
543 sigemptyset (&thread_stop_set
);
544 sigemptyset (&thread_print_set
);
546 for (i
= 1; i
< NSIG
; i
++)
548 if (sigismember (&mask
, i
))
550 if (signal_stop_update (target_signal_from_host (i
), 0))
551 sigaddset (&thread_stop_set
, i
);
552 if (signal_print_update (target_signal_from_host (i
), 0))
553 sigaddset (&thread_print_set
, i
);
562 disable_thread_signals (void)
564 #ifdef GET_THREAD_SIGNALS
569 for (i
= 1; i
< NSIG
; i
++)
571 if (sigismember (&thread_stop_set
, i
))
572 signal_stop_update (target_signal_from_host (i
), 1);
573 if (sigismember (&thread_print_set
, i
))
574 signal_print_update (target_signal_from_host (i
), 1);
583 thread_db_new_objfile (struct objfile
*objfile
)
587 /* Don't attempt to use thread_db on targets which can not run
589 if (objfile
== NULL
|| !target_has_execution
)
591 /* All symbols have been discarded. If the thread_db target is
592 active, deactivate it now. */
595 gdb_assert (proc_handle
.pid
== 0);
596 unpush_target (&thread_db_ops
);
606 /* Nothing to do. The thread library was already detected and the
607 target vector was already activated. */
610 /* Initialize the structure that identifies the child process. Note
611 that at this point there is no guarantee that we actually have a
613 proc_handle
.pid
= GET_PID (inferior_ptid
);
615 /* Now attempt to open a connection to the thread library. */
616 err
= td_ta_new_p (&proc_handle
, &thread_agent
);
620 /* No thread library was detected. */
624 /* The thread library was detected. Activate the thread_db target. */
625 push_target (&thread_db_ops
);
628 /* If the thread library was detected in the main symbol file
629 itself, we assume that the program was statically linked
630 against the thread library and well have to keep this
631 module's target vector activated until forever... Well, at
632 least until all symbols have been discarded anyway (see
634 if (objfile
== symfile_objfile
)
636 gdb_assert (proc_handle
.pid
== 0);
640 /* We can only poke around if there actually is a child process.
641 If there is no child process alive, postpone the steps below
642 until one has been created. */
643 if (proc_handle
.pid
!= 0)
645 enable_thread_event_reporting ();
646 thread_db_find_new_threads ();
651 warning ("Cannot initialize thread debugging library: %s",
652 thread_db_err_str (err
));
657 if (target_new_objfile_chain
)
658 target_new_objfile_chain (objfile
);
662 attach_thread (ptid_t ptid
, const td_thrhandle_t
*th_p
,
663 const td_thrinfo_t
*ti_p
, int verbose
)
665 struct thread_info
*tp
;
668 check_thread_signals ();
670 /* Add the thread to GDB's thread list. */
671 tp
= add_thread (ptid
);
672 tp
->private = xmalloc (sizeof (struct private_thread_info
));
673 memset (tp
->private, 0, sizeof (struct private_thread_info
));
676 printf_unfiltered ("[New %s]\n", target_pid_to_str (ptid
));
678 if (ti_p
->ti_state
== TD_THR_UNKNOWN
|| ti_p
->ti_state
== TD_THR_ZOMBIE
)
679 return; /* A zombie thread -- do not attach. */
681 /* Under GNU/Linux, we have to attach to each and every thread. */
683 ATTACH_LWP (BUILD_LWP (ti_p
->ti_lid
, GET_PID (ptid
)), 0);
686 /* Enable thread event reporting for this thread. */
687 err
= td_thr_event_enable_p (th_p
, 1);
689 error ("Cannot enable thread event reporting for %s: %s",
690 target_pid_to_str (ptid
), thread_db_err_str (err
));
694 thread_db_attach (char *args
, int from_tty
)
696 target_beneath
->to_attach (args
, from_tty
);
698 /* Destroy thread info; it's no longer valid. */
701 /* The child process is now the actual multi-threaded
702 program. Snatch its process ID... */
703 proc_handle
.pid
= GET_PID (inferior_ptid
);
705 /* ...and perform the remaining initialization steps. */
706 enable_thread_event_reporting ();
707 thread_db_find_new_threads();
711 detach_thread (ptid_t ptid
, int verbose
)
714 printf_unfiltered ("[%s exited]\n", target_pid_to_str (ptid
));
718 thread_db_detach (char *args
, int from_tty
)
720 disable_thread_event_reporting ();
722 /* There's no need to save & restore inferior_ptid here, since the
723 inferior is supposed to be survive this function call. */
724 inferior_ptid
= lwp_from_thread (inferior_ptid
);
726 /* Forget about the child's process ID. We shouldn't need it
730 target_beneath
->to_detach (args
, from_tty
);
734 clear_lwpid_callback (struct thread_info
*thread
, void *dummy
)
736 /* If we know that our thread implementation is 1-to-1, we could save
737 a certain amount of information; it's not clear how much, so we
738 are always conservative. */
740 thread
->private->th_valid
= 0;
741 thread
->private->ti_valid
= 0;
747 thread_db_resume (ptid_t ptid
, int step
, enum target_signal signo
)
749 struct cleanup
*old_chain
= save_inferior_ptid ();
751 if (GET_PID (ptid
) == -1)
752 inferior_ptid
= lwp_from_thread (inferior_ptid
);
753 else if (is_thread (ptid
))
754 ptid
= lwp_from_thread (ptid
);
756 /* Clear cached data which may not be valid after the resume. */
757 iterate_over_threads (clear_lwpid_callback
, NULL
);
759 target_beneath
->to_resume (ptid
, step
, signo
);
761 do_cleanups (old_chain
);
764 /* Check if PID is currently stopped at the location of a thread event
765 breakpoint location. If it is, read the event message and act upon
769 check_event (ptid_t ptid
)
776 /* Bail out early if we're not at a thread event breakpoint. */
777 stop_pc
= read_pc_pid (ptid
) - DECR_PC_AFTER_BREAK
;
778 if (stop_pc
!= td_create_bp_addr
&& stop_pc
!= td_death_bp_addr
)
781 err
= td_ta_event_getmsg_p (thread_agent
, &msg
);
787 error ("Cannot get thread event message: %s", thread_db_err_str (err
));
790 err
= td_thr_get_info_p (msg
.th_p
, &ti
);
792 error ("check_event: cannot get thread info: %s",
793 thread_db_err_str (err
));
795 ptid
= BUILD_THREAD (ti
.ti_tid
, GET_PID (ptid
));
801 /* FIXME: kettenis/2000-08-26: Since we use td_ta_event_getmsg,
802 there is no guarantee that the breakpoint will match the
803 event. Should we use td_thr_event_getmsg instead? */
805 if (stop_pc
!= td_create_bp_addr
)
806 error ("Thread creation event doesn't match breakpoint.");
809 /* We may already know about this thread, for instance when the
810 user has issued the `info threads' command before the SIGTRAP
811 for hitting the thread creation breakpoint was reported. */
812 if (!in_thread_list (ptid
))
813 attach_thread (ptid
, msg
.th_p
, &ti
, 1);
818 /* FIXME: See TD_CREATE. */
820 if (stop_pc
!= td_death_bp_addr
)
821 error ("Thread death event doesn't match breakpoint.");
824 if (!in_thread_list (ptid
))
825 error ("Spurious thread death event.");
827 detach_thread (ptid
, 1);
831 error ("Spurious thread event.");
836 thread_db_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
838 extern ptid_t trap_ptid
;
840 if (GET_PID (ptid
) != -1 && is_thread (ptid
))
841 ptid
= lwp_from_thread (ptid
);
843 ptid
= target_beneath
->to_wait (ptid
, ourstatus
);
845 if (proc_handle
.pid
== 0)
846 /* The current child process isn't the actual multi-threaded
847 program yet, so don't try to do any special thread-specific
848 post-processing and bail out early. */
851 if (ourstatus
->kind
== TARGET_WAITKIND_EXITED
)
852 return pid_to_ptid (-1);
854 if (ourstatus
->kind
== TARGET_WAITKIND_STOPPED
855 && ourstatus
->value
.sig
== TARGET_SIGNAL_TRAP
)
856 /* Check for a thread event. */
859 if (!ptid_equal (trap_ptid
, null_ptid
))
860 trap_ptid
= thread_from_lwp (trap_ptid
);
862 return thread_from_lwp (ptid
);
866 thread_db_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int write
,
867 struct mem_attrib
*attrib
,
868 struct target_ops
*target
)
870 struct cleanup
*old_chain
= save_inferior_ptid ();
873 if (is_thread (inferior_ptid
))
875 /* FIXME: This seems to be necessary to make sure breakpoints
877 if (!target_thread_alive (inferior_ptid
))
878 inferior_ptid
= pid_to_ptid (GET_PID (inferior_ptid
));
880 inferior_ptid
= lwp_from_thread (inferior_ptid
);
883 xfer
= target_beneath
->to_xfer_memory (memaddr
, myaddr
, len
, write
, attrib
, target
);
885 do_cleanups (old_chain
);
890 thread_db_fetch_registers (int regno
)
892 struct thread_info
*thread_info
;
894 gdb_prfpregset_t fpregset
;
897 if (!is_thread (inferior_ptid
))
899 /* Pass the request to the target beneath us. */
900 target_beneath
->to_fetch_registers (regno
);
904 thread_info
= find_thread_pid (inferior_ptid
);
905 thread_db_map_id2thr (thread_info
, 1);
907 err
= td_thr_getgregs_p (&thread_info
->private->th
, gregset
);
909 error ("Cannot fetch general-purpose registers for thread %ld: %s",
910 (long) GET_THREAD (inferior_ptid
), thread_db_err_str (err
));
912 err
= td_thr_getfpregs_p (&thread_info
->private->th
, &fpregset
);
914 error ("Cannot get floating-point registers for thread %ld: %s",
915 (long) GET_THREAD (inferior_ptid
), thread_db_err_str (err
));
917 /* Note that we must call supply_gregset after calling the thread_db
918 routines because the thread_db routines call ps_lgetgregs and
919 friends which clobber GDB's register cache. */
920 supply_gregset ((gdb_gregset_t
*) gregset
);
921 supply_fpregset (&fpregset
);
925 thread_db_store_registers (int regno
)
928 gdb_prfpregset_t fpregset
;
930 struct thread_info
*thread_info
;
932 if (!is_thread (inferior_ptid
))
934 /* Pass the request to the target beneath us. */
935 target_beneath
->to_store_registers (regno
);
939 thread_info
= find_thread_pid (inferior_ptid
);
940 thread_db_map_id2thr (thread_info
, 1);
944 char *raw
= alloca (max_register_size (current_gdbarch
));
946 deprecated_read_register_gen (regno
, raw
);
947 thread_db_fetch_registers (-1);
948 supply_register (regno
, raw
);
951 fill_gregset ((gdb_gregset_t
*) gregset
, -1);
952 fill_fpregset (&fpregset
, -1);
954 err
= td_thr_setgregs_p (&thread_info
->private->th
, gregset
);
956 error ("Cannot store general-purpose registers for thread %ld: %s",
957 (long) GET_THREAD (inferior_ptid
), thread_db_err_str (err
));
958 err
= td_thr_setfpregs_p (&thread_info
->private->th
, &fpregset
);
960 error ("Cannot store floating-point registers for thread %ld: %s",
961 (long) GET_THREAD (inferior_ptid
), thread_db_err_str (err
));
965 thread_db_kill (void)
967 /* There's no need to save & restore inferior_ptid here, since the
968 inferior isn't supposed to survive this function call. */
969 inferior_ptid
= lwp_from_thread (inferior_ptid
);
970 target_beneath
->to_kill ();
974 thread_db_create_inferior (char *exec_file
, char *allargs
, char **env
)
978 unpush_target (&thread_db_ops
);
982 target_beneath
->to_create_inferior (exec_file
, allargs
, env
);
986 thread_db_post_startup_inferior (ptid_t ptid
)
988 if (proc_handle
.pid
== 0)
990 /* The child process is now the actual multi-threaded
991 program. Snatch its process ID... */
992 proc_handle
.pid
= GET_PID (ptid
);
994 /* ...and perform the remaining initialization steps. */
995 enable_thread_event_reporting ();
996 thread_db_find_new_threads ();
1001 thread_db_mourn_inferior (void)
1003 remove_thread_event_breakpoints ();
1005 /* Forget about the child's process ID. We shouldn't need it
1007 proc_handle
.pid
= 0;
1009 target_beneath
->to_mourn_inferior ();
1013 thread_db_thread_alive (ptid_t ptid
)
1018 if (is_thread (ptid
))
1020 struct thread_info
*thread_info
;
1021 thread_info
= find_thread_pid (ptid
);
1023 thread_db_map_id2thr (thread_info
, 0);
1024 if (! thread_info
->private->th_valid
)
1027 err
= td_thr_validate_p (&thread_info
->private->th
);
1031 if (! thread_info
->private->ti_valid
)
1033 err
= td_thr_get_info_p (&thread_info
->private->th
, &thread_info
->private->ti
);
1036 thread_info
->private->ti_valid
= 1;
1039 if (thread_info
->private->ti
.ti_state
== TD_THR_UNKNOWN
1040 || thread_info
->private->ti
.ti_state
== TD_THR_ZOMBIE
)
1041 return 0; /* A zombie thread. */
1046 if (target_beneath
->to_thread_alive
)
1047 return target_beneath
->to_thread_alive (ptid
);
1053 find_new_threads_callback (const td_thrhandle_t
*th_p
, void *data
)
1059 err
= td_thr_get_info_p (th_p
, &ti
);
1061 error ("find_new_threads_callback: cannot get thread info: %s",
1062 thread_db_err_str (err
));
1064 if (ti
.ti_state
== TD_THR_UNKNOWN
|| ti
.ti_state
== TD_THR_ZOMBIE
)
1065 return 0; /* A zombie -- ignore. */
1067 ptid
= BUILD_THREAD (ti
.ti_tid
, GET_PID (inferior_ptid
));
1069 if (!in_thread_list (ptid
))
1070 attach_thread (ptid
, th_p
, &ti
, 1);
1076 thread_db_find_new_threads (void)
1080 /* Iterate over all user-space threads to discover new threads. */
1081 err
= td_ta_thr_iter_p (thread_agent
, find_new_threads_callback
, NULL
,
1082 TD_THR_ANY_STATE
, TD_THR_LOWEST_PRIORITY
,
1083 TD_SIGNO_MASK
, TD_THR_ANY_USER_FLAGS
);
1085 error ("Cannot find new threads: %s", thread_db_err_str (err
));
1089 thread_db_pid_to_str (ptid_t ptid
)
1091 if (is_thread (ptid
))
1093 static char buf
[64];
1096 struct thread_info
*thread_info
;
1098 thread_info
= find_thread_pid (ptid
);
1099 thread_db_map_id2thr (thread_info
, 0);
1100 if (! thread_info
->private->th_valid
)
1102 snprintf (buf
, sizeof (buf
), "Thread %ld (Missing)", GET_THREAD (ptid
));
1106 ti_p
= thread_db_get_info (thread_info
);
1108 if (ti_p
->ti_state
== TD_THR_ACTIVE
&& ti_p
->ti_lid
!= 0)
1110 snprintf (buf
, sizeof (buf
), "Thread %ld (LWP %d)",
1111 (long) ti_p
->ti_tid
, ti_p
->ti_lid
);
1115 snprintf (buf
, sizeof (buf
), "Thread %ld (%s)",
1116 (long) ti_p
->ti_tid
, thread_db_state_str (ti_p
->ti_state
));
1122 if (target_beneath
->to_pid_to_str (ptid
))
1123 return target_beneath
->to_pid_to_str (ptid
);
1125 return normal_pid_to_str (ptid
);
1128 /* Get the address of the thread local variable in OBJFILE which is
1129 stored at OFFSET within the thread local storage for thread PTID. */
1132 thread_db_get_thread_local_address (ptid_t ptid
, struct objfile
*objfile
,
1135 if (is_thread (ptid
))
1137 int objfile_is_library
= (objfile
->flags
& OBJF_SHARED
);
1141 struct thread_info
*thread_info
;
1143 /* glibc doesn't provide the needed interface. */
1144 if (! td_thr_tls_get_addr_p
)
1145 error ("Cannot find thread-local variables in this thread library.");
1147 /* Get the address of the link map for this objfile. */
1148 lm
= svr4_fetch_objfile_link_map (objfile
);
1150 /* Whoops, we couldn't find one. Bail out. */
1153 if (objfile_is_library
)
1154 error ("Cannot find shared library `%s' link_map in dynamic"
1155 " linker's module list", objfile
->name
);
1157 error ("Cannot find executable file `%s' link_map in dynamic"
1158 " linker's module list", objfile
->name
);
1161 /* Get info about the thread. */
1162 thread_info
= find_thread_pid (ptid
);
1163 thread_db_map_id2thr (thread_info
, 1);
1165 /* Finally, get the address of the variable. */
1166 err
= td_thr_tls_get_addr_p (&thread_info
->private->th
, (void *) lm
,
1169 #ifdef THREAD_DB_HAS_TD_NOTALLOC
1170 /* The memory hasn't been allocated, yet. */
1171 if (err
== TD_NOTALLOC
)
1173 /* Now, if libthread_db provided the initialization image's
1174 address, we *could* try to build a non-lvalue value from
1175 the initialization image. */
1176 if (objfile_is_library
)
1177 error ("The inferior has not yet allocated storage for"
1178 " thread-local variables in\n"
1179 "the shared library `%s'\n"
1180 "for the thread %ld",
1181 objfile
->name
, (long) GET_THREAD (ptid
));
1183 error ("The inferior has not yet allocated storage for"
1184 " thread-local variables in\n"
1185 "the executable `%s'\n"
1186 "for the thread %ld",
1187 objfile
->name
, (long) GET_THREAD (ptid
));
1191 /* Something else went wrong. */
1194 if (objfile_is_library
)
1195 error ("Cannot find thread-local storage for thread %ld, "
1196 "shared library %s:\n%s",
1197 (long) GET_THREAD (ptid
),
1199 thread_db_err_str (err
));
1201 error ("Cannot find thread-local storage for thread %ld, "
1202 "executable file %s:\n%s",
1203 (long) GET_THREAD (ptid
),
1205 thread_db_err_str (err
));
1208 /* Cast assuming host == target. Joy. */
1209 return (CORE_ADDR
) address
;
1212 if (target_beneath
->to_get_thread_local_address
)
1213 return target_beneath
->to_get_thread_local_address (ptid
, objfile
, offset
);
1215 error ("Cannot find thread-local values on this target.");
1219 init_thread_db_ops (void)
1221 thread_db_ops
.to_shortname
= "multi-thread";
1222 thread_db_ops
.to_longname
= "multi-threaded child process.";
1223 thread_db_ops
.to_doc
= "Threads and pthreads support.";
1224 thread_db_ops
.to_attach
= thread_db_attach
;
1225 thread_db_ops
.to_detach
= thread_db_detach
;
1226 thread_db_ops
.to_resume
= thread_db_resume
;
1227 thread_db_ops
.to_wait
= thread_db_wait
;
1228 thread_db_ops
.to_fetch_registers
= thread_db_fetch_registers
;
1229 thread_db_ops
.to_store_registers
= thread_db_store_registers
;
1230 thread_db_ops
.to_xfer_memory
= thread_db_xfer_memory
;
1231 thread_db_ops
.to_kill
= thread_db_kill
;
1232 thread_db_ops
.to_create_inferior
= thread_db_create_inferior
;
1233 thread_db_ops
.to_post_startup_inferior
= thread_db_post_startup_inferior
;
1234 thread_db_ops
.to_mourn_inferior
= thread_db_mourn_inferior
;
1235 thread_db_ops
.to_thread_alive
= thread_db_thread_alive
;
1236 thread_db_ops
.to_find_new_threads
= thread_db_find_new_threads
;
1237 thread_db_ops
.to_pid_to_str
= thread_db_pid_to_str
;
1238 thread_db_ops
.to_stratum
= thread_stratum
;
1239 thread_db_ops
.to_has_thread_control
= tc_schedlock
;
1240 thread_db_ops
.to_get_thread_local_address
1241 = thread_db_get_thread_local_address
;
1242 thread_db_ops
.to_magic
= OPS_MAGIC
;
1246 _initialize_thread_db (void)
1248 /* Only initialize the module if we can load libthread_db. */
1249 if (thread_db_load ())
1251 init_thread_db_ops ();
1252 add_target (&thread_db_ops
);
1254 /* Add ourselves to objfile event chain. */
1255 target_new_objfile_chain
= target_new_objfile_hook
;
1256 target_new_objfile_hook
= thread_db_new_objfile
;