1 /* libthread_db assisted debugging support, generic parts.
3 Copyright 1999, 2000, 2001, 2003, 2004 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 #ifdef HAVE_GNU_LIBC_VERSION_H
39 #include <gnu/libc-version.h>
42 #ifndef LIBTHREAD_DB_SO
43 #define LIBTHREAD_DB_SO "libthread_db.so.1"
46 /* If we're running on GNU/Linux, we must explicitly attach to any new
49 /* FIXME: There is certainly some room for improvements:
51 - Bypass libthread_db when fetching or storing registers for
52 threads bound to a LWP. */
54 /* This module's target vector. */
55 static struct target_ops thread_db_ops;
57 /* The target vector that we call for things this module can't handle. */
58 static struct target_ops *target_beneath;
60 /* Pointer to the next function on the objfile event chain. */
61 static void (*target_new_objfile_chain) (struct objfile * objfile);
63 /* Non-zero if we're using this module's target vector. */
64 static int using_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,
85 static td_err_e (*td_ta_map_id2thr_p) (const td_thragent_t *ta, thread_t pt,
86 td_thrhandle_t *__th);
87 static td_err_e (*td_ta_map_lwp2thr_p) (const td_thragent_t *ta,
88 lwpid_t lwpid, td_thrhandle_t *th);
89 static td_err_e (*td_ta_thr_iter_p) (const td_thragent_t *ta,
90 td_thr_iter_f *callback, void *cbdata_p,
91 td_thr_state_e state, int ti_pri,
92 sigset_t *ti_sigmask_p,
93 unsigned int ti_user_flags);
94 static td_err_e (*td_ta_event_addr_p) (const td_thragent_t *ta,
95 td_event_e event, td_notify_t *ptr);
96 static td_err_e (*td_ta_set_event_p) (const td_thragent_t *ta,
97 td_thr_events_t *event);
98 static td_err_e (*td_ta_event_getmsg_p) (const td_thragent_t *ta,
101 static td_err_e (*td_thr_validate_p) (const td_thrhandle_t *th);
102 static td_err_e (*td_thr_get_info_p) (const td_thrhandle_t *th,
103 td_thrinfo_t *infop);
104 static td_err_e (*td_thr_getfpregs_p) (const td_thrhandle_t *th,
105 gdb_prfpregset_t *regset);
106 static td_err_e (*td_thr_getgregs_p) (const td_thrhandle_t *th,
108 static td_err_e (*td_thr_setfpregs_p) (const td_thrhandle_t *th,
109 const gdb_prfpregset_t *fpregs);
110 static td_err_e (*td_thr_setgregs_p) (const td_thrhandle_t *th,
112 static td_err_e (*td_thr_event_enable_p) (const td_thrhandle_t *th,
115 static td_err_e (*td_thr_tls_get_addr_p) (const td_thrhandle_t *th,
117 size_t offset, void **address);
119 /* Location of the thread creation event breakpoint. The code at this
120 location in the child process will be called by the pthread library
121 whenever a new thread is created. By setting a special breakpoint
122 at this location, GDB can detect when a new thread is created. We
123 obtain this location via the td_ta_event_addr call. */
124 static CORE_ADDR td_create_bp_addr;
126 /* Location of the thread death event breakpoint. */
127 static CORE_ADDR td_death_bp_addr;
129 /* Prototypes for local functions. */
130 static void thread_db_find_new_threads (void);
131 static void attach_thread (ptid_t ptid, const td_thrhandle_t *th_p,
132 const td_thrinfo_t *ti_p, int verbose);
133 static void detach_thread (ptid_t ptid, int verbose);
136 /* Building process ids. */
138 #define GET_PID(ptid) ptid_get_pid (ptid)
139 #define GET_LWP(ptid) ptid_get_lwp (ptid)
140 #define GET_THREAD(ptid) ptid_get_tid (ptid)
142 #define is_lwp(ptid) (GET_LWP (ptid) != 0)
143 #define is_thread(ptid) (GET_THREAD (ptid) != 0)
145 #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
148 /* Use "struct private_thread_info" to cache thread state. This is
149 a substantial optimization. */
151 struct private_thread_info
153 /* Flag set when we see a TD_DEATH event for this thread. */
154 unsigned int dying:1;
156 /* Cached thread state. */
157 unsigned int th_valid:1;
158 unsigned int ti_valid:1;
166 thread_db_err_str (td_err_e err)
173 return "generic 'call succeeded'";
175 return "generic error";
177 return "no thread to satisfy query";
179 return "no sync handle to satisfy query";
181 return "no LWP to satisfy query";
183 return "invalid process handle";
185 return "invalid thread handle";
187 return "invalid synchronization handle";
189 return "invalid thread agent";
191 return "invalid key";
193 return "no event message for getmsg";
195 return "FPU register set not available";
197 return "application not linked with libthread";
199 return "requested event is not supported";
201 return "capability not available";
203 return "debugger service failed";
205 return "operation not applicable to";
207 return "no thread-specific data for this thread";
209 return "malloc failed";
211 return "only part of register set was written/read";
213 return "X register set not available for this thread";
215 snprintf (buf, sizeof (buf), "unknown thread_db error '%d'", err);
221 thread_db_state_str (td_thr_state_e state)
228 return "stopped by debugger";
237 case TD_THR_STOPPED_ASLEEP:
238 return "stopped by debugger AND blocked";
240 snprintf (buf, sizeof (buf), "unknown thread_db state %d", state);
245 /* A callback function for td_ta_thr_iter, which we use to map all
248 THP is a handle to the current thread; if INFOP is not NULL, the
249 struct thread_info associated with this thread is returned in
252 If the thread is a zombie, TD_THR_ZOMBIE is returned. Otherwise,
253 zero is returned to indicate success. */
256 thread_get_info_callback (const td_thrhandle_t *thp, void *infop)
260 struct thread_info *thread_info;
263 err = td_thr_get_info_p (thp, &ti);
265 error (_("thread_get_info_callback: cannot get thread info: %s"),
266 thread_db_err_str (err));
268 /* Fill the cache. */
269 thread_ptid = ptid_build (GET_PID (inferior_ptid), ti.ti_lid, ti.ti_tid);
270 thread_info = find_thread_pid (thread_ptid);
272 /* In the case of a zombie thread, don't continue. We don't want to
273 attach to it thinking it is a new thread. */
274 if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
277 *(struct thread_info **) infop = thread_info;
278 if (thread_info != NULL)
280 memcpy (&thread_info->private->th, thp, sizeof (*thp));
281 thread_info->private->th_valid = 1;
282 memcpy (&thread_info->private->ti, &ti, sizeof (ti));
283 thread_info->private->ti_valid = 1;
285 return TD_THR_ZOMBIE;
288 if (thread_info == NULL)
290 /* New thread. Attach to it now (why wait?). */
291 attach_thread (thread_ptid, thp, &ti, 1);
292 thread_info = find_thread_pid (thread_ptid);
293 gdb_assert (thread_info != NULL);
296 memcpy (&thread_info->private->th, thp, sizeof (*thp));
297 thread_info->private->th_valid = 1;
298 memcpy (&thread_info->private->ti, &ti, sizeof (ti));
299 thread_info->private->ti_valid = 1;
302 *(struct thread_info **) infop = thread_info;
307 /* Accessor functions for the thread_db information, with caching. */
310 thread_db_map_id2thr (struct thread_info *thread_info, int fatal)
314 if (thread_info->private->th_valid)
317 err = td_ta_map_id2thr_p (thread_agent, GET_THREAD (thread_info->ptid),
318 &thread_info->private->th);
322 error (_("Cannot find thread %ld: %s"),
323 (long) GET_THREAD (thread_info->ptid),
324 thread_db_err_str (err));
327 thread_info->private->th_valid = 1;
330 static td_thrinfo_t *
331 thread_db_get_info (struct thread_info *thread_info)
335 if (thread_info->private->ti_valid)
336 return &thread_info->private->ti;
338 if (!thread_info->private->th_valid)
339 thread_db_map_id2thr (thread_info, 1);
342 td_thr_get_info_p (&thread_info->private->th, &thread_info->private->ti);
344 error (_("thread_db_get_info: cannot get thread info: %s"),
345 thread_db_err_str (err));
347 thread_info->private->ti_valid = 1;
348 return &thread_info->private->ti;
351 /* Convert between user-level thread ids and LWP ids. */
354 thread_from_lwp (ptid_t ptid)
358 struct thread_info *thread_info;
361 if (GET_LWP (ptid) == 0)
362 ptid = BUILD_LWP (GET_PID (ptid), GET_PID (ptid));
364 gdb_assert (is_lwp (ptid));
366 err = td_ta_map_lwp2thr_p (thread_agent, GET_LWP (ptid), &th);
368 error (_("Cannot find user-level thread for LWP %ld: %s"),
369 GET_LWP (ptid), thread_db_err_str (err));
373 /* Fetch the thread info. If we get back TD_THR_ZOMBIE, then the
374 event thread has already died. If another gdb interface has called
375 thread_alive() previously, the thread won't be found on the thread list
376 anymore. In that case, we don't want to process this ptid anymore
377 to avoid the possibility of later treating it as a newly
378 discovered thread id that we should add to the list. Thus,
379 we return a -1 ptid which is also how the thread list marks a
381 if (thread_get_info_callback (&th, &thread_info) == TD_THR_ZOMBIE
382 && thread_info == NULL)
383 return pid_to_ptid (-1);
385 gdb_assert (thread_info && thread_info->private->ti_valid);
387 return ptid_build (GET_PID (ptid), GET_LWP (ptid),
388 thread_info->private->ti.ti_tid);
392 lwp_from_thread (ptid_t ptid)
394 return BUILD_LWP (GET_LWP (ptid), GET_PID (ptid));
399 thread_db_init (struct target_ops *target)
401 target_beneath = target;
405 verbose_dlsym (void *handle, const char *name)
407 void *sym = dlsym (handle, name);
409 warning (_("Symbol \"%s\" not found in libthread_db: %s"), name, dlerror ());
414 thread_db_load (void)
419 handle = dlopen (LIBTHREAD_DB_SO, RTLD_NOW);
422 fprintf_filtered (gdb_stderr, "\n\ndlopen failed on '%s' - %s\n",
423 LIBTHREAD_DB_SO, dlerror ());
424 fprintf_filtered (gdb_stderr,
425 "GDB will not be able to debug pthreads.\n\n");
429 /* Initialize pointers to the dynamic library functions we will use.
430 Essential functions first. */
432 td_init_p = verbose_dlsym (handle, "td_init");
433 if (td_init_p == NULL)
436 td_ta_new_p = verbose_dlsym (handle, "td_ta_new");
437 if (td_ta_new_p == NULL)
440 td_ta_map_id2thr_p = verbose_dlsym (handle, "td_ta_map_id2thr");
441 if (td_ta_map_id2thr_p == NULL)
444 td_ta_map_lwp2thr_p = verbose_dlsym (handle, "td_ta_map_lwp2thr");
445 if (td_ta_map_lwp2thr_p == NULL)
448 td_ta_thr_iter_p = verbose_dlsym (handle, "td_ta_thr_iter");
449 if (td_ta_thr_iter_p == NULL)
452 td_thr_validate_p = verbose_dlsym (handle, "td_thr_validate");
453 if (td_thr_validate_p == NULL)
456 td_thr_get_info_p = verbose_dlsym (handle, "td_thr_get_info");
457 if (td_thr_get_info_p == NULL)
460 td_thr_getfpregs_p = verbose_dlsym (handle, "td_thr_getfpregs");
461 if (td_thr_getfpregs_p == NULL)
464 td_thr_getgregs_p = verbose_dlsym (handle, "td_thr_getgregs");
465 if (td_thr_getgregs_p == NULL)
468 td_thr_setfpregs_p = verbose_dlsym (handle, "td_thr_setfpregs");
469 if (td_thr_setfpregs_p == NULL)
472 td_thr_setgregs_p = verbose_dlsym (handle, "td_thr_setgregs");
473 if (td_thr_setgregs_p == NULL)
476 /* Initialize the library. */
480 warning (_("Cannot initialize libthread_db: %s"), thread_db_err_str (err));
484 /* These are not essential. */
485 td_ta_event_addr_p = dlsym (handle, "td_ta_event_addr");
486 td_ta_set_event_p = dlsym (handle, "td_ta_set_event");
487 td_ta_event_getmsg_p = dlsym (handle, "td_ta_event_getmsg");
488 td_thr_event_enable_p = dlsym (handle, "td_thr_event_enable");
489 td_thr_tls_get_addr_p = dlsym (handle, "td_thr_tls_get_addr");
495 enable_thread_event (td_thragent_t *thread_agent, int event, CORE_ADDR *bp)
500 /* Get the breakpoint address for thread EVENT. */
501 err = td_ta_event_addr_p (thread_agent, event, ¬ify);
505 /* Set up the breakpoint. */
506 (*bp) = gdbarch_convert_from_func_ptr_addr (current_gdbarch,
507 (CORE_ADDR) notify.u.bptaddr,
509 create_thread_event_breakpoint ((*bp));
515 enable_thread_event_reporting (void)
517 td_thr_events_t events;
520 #ifdef HAVE_GNU_LIBC_VERSION_H
521 const char *libc_version;
522 int libc_major, libc_minor;
525 /* We cannot use the thread event reporting facility if these
526 functions aren't available. */
527 if (td_ta_event_addr_p == NULL || td_ta_set_event_p == NULL
528 || td_ta_event_getmsg_p == NULL || td_thr_event_enable_p == NULL)
531 /* Set the process wide mask saying which events we're interested in. */
532 td_event_emptyset (&events);
533 td_event_addset (&events, TD_CREATE);
535 #ifdef HAVE_GNU_LIBC_VERSION_H
536 /* FIXME: kettenis/2000-04-23: The event reporting facility is
537 broken for TD_DEATH events in glibc 2.1.3, so don't enable it for
539 libc_version = gnu_get_libc_version ();
540 if (sscanf (libc_version, "%d.%d", &libc_major, &libc_minor) == 2
541 && (libc_major > 2 || (libc_major == 2 && libc_minor > 1)))
543 td_event_addset (&events, TD_DEATH);
545 err = td_ta_set_event_p (thread_agent, &events);
548 warning (_("Unable to set global thread event mask: %s"),
549 thread_db_err_str (err));
553 /* Delete previous thread event breakpoints, if any. */
554 remove_thread_event_breakpoints ();
555 td_create_bp_addr = 0;
556 td_death_bp_addr = 0;
558 /* Set up the thread creation event. */
559 err = enable_thread_event (thread_agent, TD_CREATE, &td_create_bp_addr);
562 warning (_("Unable to get location for thread creation breakpoint: %s"),
563 thread_db_err_str (err));
567 /* Set up the thread death event. */
568 err = enable_thread_event (thread_agent, TD_DEATH, &td_death_bp_addr);
571 warning (_("Unable to get location for thread death breakpoint: %s"),
572 thread_db_err_str (err));
578 disable_thread_event_reporting (void)
580 td_thr_events_t events;
582 /* Set the process wide mask saying we aren't interested in any
584 td_event_emptyset (&events);
585 td_ta_set_event_p (thread_agent, &events);
587 /* Delete thread event breakpoints, if any. */
588 remove_thread_event_breakpoints ();
589 td_create_bp_addr = 0;
590 td_death_bp_addr = 0;
594 check_thread_signals (void)
596 #ifdef GET_THREAD_SIGNALS
602 GET_THREAD_SIGNALS (&mask);
603 sigemptyset (&thread_stop_set);
604 sigemptyset (&thread_print_set);
606 for (i = 1; i < NSIG; i++)
608 if (sigismember (&mask, i))
610 if (signal_stop_update (target_signal_from_host (i), 0))
611 sigaddset (&thread_stop_set, i);
612 if (signal_print_update (target_signal_from_host (i), 0))
613 sigaddset (&thread_print_set, i);
622 thread_db_new_objfile (struct objfile *objfile)
626 /* First time through, report that libthread_db was successfuly
627 loaded. Can't print this in in thread_db_load as, at that stage,
628 the interpreter and it's console haven't started. The real
629 problem here is that libthread_db is loaded too early - it should
630 only be loaded when there is a program to debug. */
636 const char *library = NULL;
638 if (dladdr ((*td_ta_new_p), &info) != 0)
639 library = info.dli_fname;
642 /* Paranoid - don't let a NULL path slip through. */
643 library = LIBTHREAD_DB_SO;
644 printf_unfiltered (_("Using host libthread_db library \"%s\".\n"),
650 /* Don't attempt to use thread_db on targets which can not run
652 if (objfile == NULL || !target_has_execution)
654 /* All symbols have been discarded. If the thread_db target is
655 active, deactivate it now. */
658 gdb_assert (proc_handle.pid == 0);
659 unpush_target (&thread_db_ops);
667 /* Nothing to do. The thread library was already detected and the
668 target vector was already activated. */
671 /* Initialize the structure that identifies the child process. Note
672 that at this point there is no guarantee that we actually have a
674 proc_handle.pid = GET_PID (inferior_ptid);
676 /* Now attempt to open a connection to the thread library. */
677 err = td_ta_new_p (&proc_handle, &thread_agent);
681 /* No thread library was detected. */
685 printf_unfiltered (_("[Thread debugging using libthread_db enabled]\n"));
687 /* The thread library was detected. Activate the thread_db target. */
688 push_target (&thread_db_ops);
691 enable_thread_event_reporting ();
692 thread_db_find_new_threads ();
696 warning (_("Cannot initialize thread debugging library: %s"),
697 thread_db_err_str (err));
702 if (target_new_objfile_chain)
703 target_new_objfile_chain (objfile);
706 /* Attach to a new thread. This function is called when we receive a
707 TD_CREATE event or when we iterate over all threads and find one
708 that wasn't already in our list. */
711 attach_thread (ptid_t ptid, const td_thrhandle_t *th_p,
712 const td_thrinfo_t *ti_p, int verbose)
714 struct thread_info *tp;
717 /* If we're being called after a TD_CREATE event, we may already
718 know about this thread. There are two ways this can happen. We
719 may have iterated over all threads between the thread creation
720 and the TD_CREATE event, for instance when the user has issued
721 the `info threads' command before the SIGTRAP for hitting the
722 thread creation breakpoint was reported. Alternatively, the
723 thread may have exited and a new one been created with the same
724 thread ID. In the first case we don't need to do anything; in
725 the second case we should discard information about the dead
726 thread and attach to the new one. */
727 if (in_thread_list (ptid))
729 tp = find_thread_pid (ptid);
730 gdb_assert (tp != NULL);
732 if (!tp->private->dying)
735 delete_thread (ptid);
738 check_thread_signals ();
740 /* Add the thread to GDB's thread list. */
741 tp = add_thread (ptid);
742 tp->private = xmalloc (sizeof (struct private_thread_info));
743 memset (tp->private, 0, sizeof (struct private_thread_info));
746 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid));
748 if (ti_p->ti_state == TD_THR_UNKNOWN || ti_p->ti_state == TD_THR_ZOMBIE)
749 return; /* A zombie thread -- do not attach. */
751 /* Under GNU/Linux, we have to attach to each and every thread. */
753 ATTACH_LWP (BUILD_LWP (ti_p->ti_lid, GET_PID (ptid)), 0);
756 /* Enable thread event reporting for this thread. */
757 err = td_thr_event_enable_p (th_p, 1);
759 error (_("Cannot enable thread event reporting for %s: %s"),
760 target_pid_to_str (ptid), thread_db_err_str (err));
764 thread_db_attach (char *args, int from_tty)
766 target_beneath->to_attach (args, from_tty);
768 /* Destroy thread info; it's no longer valid. */
771 /* The child process is now the actual multi-threaded
772 program. Snatch its process ID... */
773 proc_handle.pid = GET_PID (inferior_ptid);
775 /* ...and perform the remaining initialization steps. */
776 enable_thread_event_reporting ();
777 thread_db_find_new_threads ();
781 detach_thread (ptid_t ptid, int verbose)
783 struct thread_info *thread_info;
786 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (ptid));
788 /* Don't delete the thread now, because it still reports as active
789 until it has executed a few instructions after the event
790 breakpoint - if we deleted it now, "info threads" would cause us
791 to re-attach to it. Just mark it as having had a TD_DEATH
792 event. This means that we won't delete it from our thread list
793 until we notice that it's dead (via prune_threads), or until
794 something re-uses its thread ID. */
795 thread_info = find_thread_pid (ptid);
796 gdb_assert (thread_info != NULL);
797 thread_info->private->dying = 1;
801 thread_db_detach (char *args, int from_tty)
803 disable_thread_event_reporting ();
805 /* There's no need to save & restore inferior_ptid here, since the
806 inferior is supposed to be survive this function call. */
807 inferior_ptid = lwp_from_thread (inferior_ptid);
809 /* Forget about the child's process ID. We shouldn't need it
813 target_beneath->to_detach (args, from_tty);
817 clear_lwpid_callback (struct thread_info *thread, void *dummy)
819 /* If we know that our thread implementation is 1-to-1, we could save
820 a certain amount of information; it's not clear how much, so we
821 are always conservative. */
823 thread->private->th_valid = 0;
824 thread->private->ti_valid = 0;
830 thread_db_resume (ptid_t ptid, int step, enum target_signal signo)
832 struct cleanup *old_chain = save_inferior_ptid ();
834 if (GET_PID (ptid) == -1)
835 inferior_ptid = lwp_from_thread (inferior_ptid);
836 else if (is_thread (ptid))
837 ptid = lwp_from_thread (ptid);
839 /* Clear cached data which may not be valid after the resume. */
840 iterate_over_threads (clear_lwpid_callback, NULL);
842 target_beneath->to_resume (ptid, step, signo);
844 do_cleanups (old_chain);
847 /* Check if PID is currently stopped at the location of a thread event
848 breakpoint location. If it is, read the event message and act upon
852 check_event (ptid_t ptid)
860 /* Bail out early if we're not at a thread event breakpoint. */
861 stop_pc = read_pc_pid (ptid) - DECR_PC_AFTER_BREAK;
862 if (stop_pc != td_create_bp_addr && stop_pc != td_death_bp_addr)
865 /* If we are at a create breakpoint, we do not know what new lwp
866 was created and cannot specifically locate the event message for it.
867 We have to call td_ta_event_getmsg() to get
868 the latest message. Since we have no way of correlating whether
869 the event message we get back corresponds to our breakpoint, we must
870 loop and read all event messages, processing them appropriately.
871 This guarantees we will process the correct message before continuing
874 Currently, death events are not enabled. If they are enabled,
875 the death event can use the td_thr_event_getmsg() interface to
876 get the message specifically for that lwp and avoid looping
883 err = td_ta_event_getmsg_p (thread_agent, &msg);
889 error (_("Cannot get thread event message: %s"),
890 thread_db_err_str (err));
893 err = td_thr_get_info_p (msg.th_p, &ti);
895 error (_("Cannot get thread info: %s"), thread_db_err_str (err));
897 ptid = ptid_build (GET_PID (ptid), ti.ti_lid, ti.ti_tid);
902 /* Call attach_thread whether or not we already know about a
903 thread with this thread ID. */
904 attach_thread (ptid, msg.th_p, &ti, 1);
910 if (!in_thread_list (ptid))
911 error (_("Spurious thread death event."));
913 detach_thread (ptid, 1);
918 error (_("Spurious thread event."));
925 thread_db_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
927 extern ptid_t trap_ptid;
929 if (GET_PID (ptid) != -1 && is_thread (ptid))
930 ptid = lwp_from_thread (ptid);
932 ptid = target_beneath->to_wait (ptid, ourstatus);
934 if (proc_handle.pid == 0)
935 /* The current child process isn't the actual multi-threaded
936 program yet, so don't try to do any special thread-specific
937 post-processing and bail out early. */
940 if (ourstatus->kind == TARGET_WAITKIND_EXITED)
941 return pid_to_ptid (-1);
943 if (ourstatus->kind == TARGET_WAITKIND_STOPPED
944 && ourstatus->value.sig == TARGET_SIGNAL_TRAP)
945 /* Check for a thread event. */
948 if (!ptid_equal (trap_ptid, null_ptid))
949 trap_ptid = thread_from_lwp (trap_ptid);
951 /* Change the ptid back into the higher level PID + TID format.
952 If the thread is dead and no longer on the thread list, we will
953 get back a dead ptid. This can occur if the thread death event
954 gets postponed by other simultaneous events. In such a case,
955 we want to just ignore the event and continue on. */
956 ptid = thread_from_lwp (ptid);
957 if (GET_PID (ptid) == -1)
958 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
964 thread_db_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
965 struct mem_attrib *attrib, struct target_ops *target)
967 struct cleanup *old_chain = save_inferior_ptid ();
970 if (is_thread (inferior_ptid))
972 /* FIXME: This seems to be necessary to make sure breakpoints
974 if (!target_thread_alive (inferior_ptid))
975 inferior_ptid = pid_to_ptid (GET_PID (inferior_ptid));
977 inferior_ptid = lwp_from_thread (inferior_ptid);
981 target_beneath->deprecated_xfer_memory (memaddr, myaddr, len, write,
984 do_cleanups (old_chain);
989 thread_db_fetch_registers (int regno)
991 struct thread_info *thread_info;
993 gdb_prfpregset_t fpregset;
996 if (!is_thread (inferior_ptid))
998 /* Pass the request to the target beneath us. */
999 target_beneath->to_fetch_registers (regno);
1003 thread_info = find_thread_pid (inferior_ptid);
1004 thread_db_map_id2thr (thread_info, 1);
1006 err = td_thr_getgregs_p (&thread_info->private->th, gregset);
1008 error (_("Cannot fetch general-purpose registers for thread %ld: %s"),
1009 (long) GET_THREAD (inferior_ptid), thread_db_err_str (err));
1011 err = td_thr_getfpregs_p (&thread_info->private->th, &fpregset);
1013 error (_("Cannot get floating-point registers for thread %ld: %s"),
1014 (long) GET_THREAD (inferior_ptid), thread_db_err_str (err));
1016 /* Note that we must call supply_gregset after calling the thread_db
1017 routines because the thread_db routines call ps_lgetgregs and
1018 friends which clobber GDB's register cache. */
1019 supply_gregset ((gdb_gregset_t *) gregset);
1020 supply_fpregset (&fpregset);
1024 thread_db_store_registers (int regno)
1026 prgregset_t gregset;
1027 gdb_prfpregset_t fpregset;
1029 struct thread_info *thread_info;
1031 if (!is_thread (inferior_ptid))
1033 /* Pass the request to the target beneath us. */
1034 target_beneath->to_store_registers (regno);
1038 thread_info = find_thread_pid (inferior_ptid);
1039 thread_db_map_id2thr (thread_info, 1);
1043 char raw[MAX_REGISTER_SIZE];
1045 deprecated_read_register_gen (regno, raw);
1046 thread_db_fetch_registers (-1);
1047 regcache_raw_supply (current_regcache, regno, raw);
1050 fill_gregset ((gdb_gregset_t *) gregset, -1);
1051 fill_fpregset (&fpregset, -1);
1053 err = td_thr_setgregs_p (&thread_info->private->th, gregset);
1055 error (_("Cannot store general-purpose registers for thread %ld: %s"),
1056 (long) GET_THREAD (inferior_ptid), thread_db_err_str (err));
1057 err = td_thr_setfpregs_p (&thread_info->private->th, &fpregset);
1059 error (_("Cannot store floating-point registers for thread %ld: %s"),
1060 (long) GET_THREAD (inferior_ptid), thread_db_err_str (err));
1064 thread_db_kill (void)
1066 /* There's no need to save & restore inferior_ptid here, since the
1067 inferior isn't supposed to survive this function call. */
1068 inferior_ptid = lwp_from_thread (inferior_ptid);
1069 target_beneath->to_kill ();
1073 thread_db_create_inferior (char *exec_file, char *allargs, char **env,
1076 unpush_target (&thread_db_ops);
1077 using_thread_db = 0;
1078 target_beneath->to_create_inferior (exec_file, allargs, env, from_tty);
1082 thread_db_post_startup_inferior (ptid_t ptid)
1084 if (proc_handle.pid == 0)
1086 /* The child process is now the actual multi-threaded
1087 program. Snatch its process ID... */
1088 proc_handle.pid = GET_PID (ptid);
1090 /* ...and perform the remaining initialization steps. */
1091 enable_thread_event_reporting ();
1092 thread_db_find_new_threads ();
1097 thread_db_mourn_inferior (void)
1099 remove_thread_event_breakpoints ();
1101 /* Forget about the child's process ID. We shouldn't need it
1103 proc_handle.pid = 0;
1105 target_beneath->to_mourn_inferior ();
1107 /* Detach thread_db target ops. */
1108 unpush_target (&thread_db_ops);
1109 using_thread_db = 0;
1113 thread_db_thread_alive (ptid_t ptid)
1118 if (is_thread (ptid))
1120 struct thread_info *thread_info;
1121 thread_info = find_thread_pid (ptid);
1123 thread_db_map_id2thr (thread_info, 0);
1124 if (!thread_info->private->th_valid)
1127 err = td_thr_validate_p (&thread_info->private->th);
1131 if (!thread_info->private->ti_valid)
1134 td_thr_get_info_p (&thread_info->private->th,
1135 &thread_info->private->ti);
1138 thread_info->private->ti_valid = 1;
1141 if (thread_info->private->ti.ti_state == TD_THR_UNKNOWN
1142 || thread_info->private->ti.ti_state == TD_THR_ZOMBIE)
1143 return 0; /* A zombie thread. */
1148 if (target_beneath->to_thread_alive)
1149 return target_beneath->to_thread_alive (ptid);
1155 find_new_threads_callback (const td_thrhandle_t *th_p, void *data)
1161 err = td_thr_get_info_p (th_p, &ti);
1163 error (_("find_new_threads_callback: cannot get thread info: %s"),
1164 thread_db_err_str (err));
1166 if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
1167 return 0; /* A zombie -- ignore. */
1169 ptid = ptid_build (GET_PID (inferior_ptid), ti.ti_lid, ti.ti_tid);
1171 if (!in_thread_list (ptid))
1172 attach_thread (ptid, th_p, &ti, 1);
1178 thread_db_find_new_threads (void)
1182 /* Iterate over all user-space threads to discover new threads. */
1183 err = td_ta_thr_iter_p (thread_agent, find_new_threads_callback, NULL,
1184 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
1185 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
1187 error (_("Cannot find new threads: %s"), thread_db_err_str (err));
1191 thread_db_pid_to_str (ptid_t ptid)
1193 if (is_thread (ptid))
1195 static char buf[64];
1198 struct thread_info *thread_info;
1200 thread_info = find_thread_pid (ptid);
1201 thread_db_map_id2thr (thread_info, 0);
1202 if (!thread_info->private->th_valid)
1204 snprintf (buf, sizeof (buf), "Thread %ld (Missing)",
1209 ti_p = thread_db_get_info (thread_info);
1211 if (ti_p->ti_state == TD_THR_ACTIVE && ti_p->ti_lid != 0)
1213 snprintf (buf, sizeof (buf), "Thread %ld (LWP %d)",
1214 (long) ti_p->ti_tid, ti_p->ti_lid);
1218 snprintf (buf, sizeof (buf), "Thread %ld (%s)",
1219 (long) ti_p->ti_tid,
1220 thread_db_state_str (ti_p->ti_state));
1226 if (target_beneath->to_pid_to_str (ptid))
1227 return target_beneath->to_pid_to_str (ptid);
1229 return normal_pid_to_str (ptid);
1232 /* Get the address of the thread local variable in OBJFILE which is
1233 stored at OFFSET within the thread local storage for thread PTID. */
1236 thread_db_get_thread_local_address (ptid_t ptid, struct objfile *objfile,
1239 if (is_thread (ptid))
1241 int objfile_is_library = (objfile->flags & OBJF_SHARED);
1245 struct thread_info *thread_info;
1247 /* glibc doesn't provide the needed interface. */
1248 if (!td_thr_tls_get_addr_p)
1249 error (_("Cannot find thread-local variables in this thread library."));
1251 /* Get the address of the link map for this objfile. */
1252 lm = svr4_fetch_objfile_link_map (objfile);
1254 /* Whoops, we couldn't find one. Bail out. */
1257 if (objfile_is_library)
1258 error (_("Cannot find shared library `%s' link_map in dynamic"
1259 " linker's module list"), objfile->name);
1261 error (_("Cannot find executable file `%s' link_map in dynamic"
1262 " linker's module list"), objfile->name);
1265 /* Get info about the thread. */
1266 thread_info = find_thread_pid (ptid);
1267 thread_db_map_id2thr (thread_info, 1);
1269 /* Finally, get the address of the variable. */
1270 err = td_thr_tls_get_addr_p (&thread_info->private->th, (void *) lm,
1273 #ifdef THREAD_DB_HAS_TD_NOTALLOC
1274 /* The memory hasn't been allocated, yet. */
1275 if (err == TD_NOTALLOC)
1277 /* Now, if libthread_db provided the initialization image's
1278 address, we *could* try to build a non-lvalue value from
1279 the initialization image. */
1280 if (objfile_is_library)
1281 error (_("The inferior has not yet allocated storage for"
1282 " thread-local variables in\n"
1283 "the shared library `%s'\n"
1284 "for the thread %ld"),
1285 objfile->name, (long) GET_THREAD (ptid));
1287 error (_("The inferior has not yet allocated storage for"
1288 " thread-local variables in\n"
1289 "the executable `%s'\n"
1290 "for the thread %ld"),
1291 objfile->name, (long) GET_THREAD (ptid));
1295 /* Something else went wrong. */
1298 if (objfile_is_library)
1299 error (_("Cannot find thread-local storage for thread %ld, "
1300 "shared library %s:\n%s"),
1301 (long) GET_THREAD (ptid),
1302 objfile->name, thread_db_err_str (err));
1304 error (_("Cannot find thread-local storage for thread %ld, "
1305 "executable file %s:\n%s"),
1306 (long) GET_THREAD (ptid),
1307 objfile->name, thread_db_err_str (err));
1310 /* Cast assuming host == target. Joy. */
1311 return (CORE_ADDR) address;
1314 if (target_beneath->to_get_thread_local_address)
1315 return target_beneath->to_get_thread_local_address (ptid, objfile,
1318 error (_("Cannot find thread-local values on this target."));
1322 init_thread_db_ops (void)
1324 thread_db_ops.to_shortname = "multi-thread";
1325 thread_db_ops.to_longname = "multi-threaded child process.";
1326 thread_db_ops.to_doc = "Threads and pthreads support.";
1327 thread_db_ops.to_attach = thread_db_attach;
1328 thread_db_ops.to_detach = thread_db_detach;
1329 thread_db_ops.to_resume = thread_db_resume;
1330 thread_db_ops.to_wait = thread_db_wait;
1331 thread_db_ops.to_fetch_registers = thread_db_fetch_registers;
1332 thread_db_ops.to_store_registers = thread_db_store_registers;
1333 thread_db_ops.deprecated_xfer_memory = thread_db_xfer_memory;
1334 thread_db_ops.to_kill = thread_db_kill;
1335 thread_db_ops.to_create_inferior = thread_db_create_inferior;
1336 thread_db_ops.to_post_startup_inferior = thread_db_post_startup_inferior;
1337 thread_db_ops.to_mourn_inferior = thread_db_mourn_inferior;
1338 thread_db_ops.to_thread_alive = thread_db_thread_alive;
1339 thread_db_ops.to_find_new_threads = thread_db_find_new_threads;
1340 thread_db_ops.to_pid_to_str = thread_db_pid_to_str;
1341 thread_db_ops.to_stratum = thread_stratum;
1342 thread_db_ops.to_has_thread_control = tc_schedlock;
1343 thread_db_ops.to_get_thread_local_address
1344 = thread_db_get_thread_local_address;
1345 thread_db_ops.to_magic = OPS_MAGIC;
1349 _initialize_thread_db (void)
1351 /* Only initialize the module if we can load libthread_db. */
1352 if (thread_db_load ())
1354 init_thread_db_ops ();
1355 add_target (&thread_db_ops);
1357 /* Add ourselves to objfile event chain. */
1358 target_new_objfile_chain = deprecated_target_new_objfile_hook;
1359 deprecated_target_new_objfile_hook = thread_db_new_objfile;