1 /* libthread_db assisted debugging support, generic parts.
3 Copyright (C) 1999, 2000, 2001, 2003, 2004, 2005, 2006, 2007
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
25 #include "gdb_assert.h"
27 #include "gdb_proc_service.h"
28 #include "gdb_thread_db.h"
31 #include "exceptions.h"
32 #include "gdbthread.h"
38 #include "solib-svr4.h"
41 #include "linux-nat.h"
45 #ifdef HAVE_GNU_LIBC_VERSION_H
46 #include <gnu/libc-version.h>
49 #ifndef LIBTHREAD_DB_SO
50 #define LIBTHREAD_DB_SO "libthread_db.so.1"
53 /* If we're running on GNU/Linux, we must explicitly attach to any new
56 /* This module's target vector. */
57 static struct target_ops thread_db_ops;
59 /* The target vector that we call for things this module can't handle. */
60 static struct target_ops *target_beneath;
62 /* Non-zero if we're using this module's target vector. */
63 static int using_thread_db;
65 /* Non-zero if we have determined the signals used by the threads
67 static int thread_signals;
68 static sigset_t thread_stop_set;
69 static sigset_t thread_print_set;
71 /* Structure that identifies the child process for the
72 <proc_service.h> interface. */
73 static struct ps_prochandle proc_handle;
75 /* Connection to the libthread_db library. */
76 static td_thragent_t *thread_agent;
78 /* Pointers to the libthread_db functions. */
80 static td_err_e (*td_init_p) (void);
82 static td_err_e (*td_ta_new_p) (struct ps_prochandle * ps,
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,
87 lwpid_t lwpid, td_thrhandle_t *th);
88 static td_err_e (*td_ta_thr_iter_p) (const td_thragent_t *ta,
89 td_thr_iter_f *callback, void *cbdata_p,
90 td_thr_state_e state, int ti_pri,
91 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_event_enable_p) (const td_thrhandle_t *th,
106 static td_err_e (*td_thr_tls_get_addr_p) (const td_thrhandle_t *th,
108 size_t offset, void **address);
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);
122 static void attach_thread (ptid_t ptid, const td_thrhandle_t *th_p,
123 const td_thrinfo_t *ti_p, int verbose);
124 static void detach_thread (ptid_t ptid, int verbose);
127 /* Building process ids. */
129 #define GET_PID(ptid) ptid_get_pid (ptid)
130 #define GET_LWP(ptid) ptid_get_lwp (ptid)
131 #define GET_THREAD(ptid) ptid_get_tid (ptid)
133 #define is_lwp(ptid) (GET_LWP (ptid) != 0)
134 #define is_thread(ptid) (GET_THREAD (ptid) != 0)
136 #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
139 /* Use "struct private_thread_info" to cache thread state. This is
140 a substantial optimization. */
142 struct private_thread_info
144 /* Flag set when we see a TD_DEATH event for this thread. */
145 unsigned int dying:1;
147 /* Cached thread state. */
148 unsigned int th_valid:1;
149 unsigned int ti_valid:1;
157 thread_db_err_str (td_err_e err)
164 return "generic 'call succeeded'";
166 return "generic error";
168 return "no thread to satisfy query";
170 return "no sync handle to satisfy query";
172 return "no LWP to satisfy query";
174 return "invalid process handle";
176 return "invalid thread handle";
178 return "invalid synchronization handle";
180 return "invalid thread agent";
182 return "invalid key";
184 return "no event message for getmsg";
186 return "FPU register set not available";
188 return "application not linked with libthread";
190 return "requested event is not supported";
192 return "capability not available";
194 return "debugger service failed";
196 return "operation not applicable to";
198 return "no thread-specific data for this thread";
200 return "malloc failed";
202 return "only part of register set was written/read";
204 return "X register set not available for this thread";
205 #ifdef THREAD_DB_HAS_TD_NOTALLOC
207 return "thread has not yet allocated TLS for given module";
209 #ifdef THREAD_DB_HAS_TD_VERSION
211 return "versions of libpthread and libthread_db do not match";
213 #ifdef THREAD_DB_HAS_TD_NOTLS
215 return "there is no TLS segment in the given module";
218 snprintf (buf, sizeof (buf), "unknown thread_db error '%d'", err);
223 /* Return 1 if any threads have been registered. There may be none if
224 the threading library is not fully initialized yet. */
227 have_threads_callback (struct thread_info *thread, void *dummy)
235 return iterate_over_threads (have_threads_callback, NULL) != NULL;
238 /* A callback function for td_ta_thr_iter, which we use to map all
241 THP is a handle to the current thread; if INFOP is not NULL, the
242 struct thread_info associated with this thread is returned in
245 If the thread is a zombie, TD_THR_ZOMBIE is returned. Otherwise,
246 zero is returned to indicate success. */
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 = ptid_build (GET_PID (inferior_ptid), ti.ti_lid, ti.ti_tid);
263 thread_info = find_thread_pid (thread_ptid);
265 /* In the case of a zombie thread, don't continue. We don't want to
266 attach to it thinking it is a new thread. */
267 if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
270 *(struct thread_info **) infop = thread_info;
271 if (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;
278 return TD_THR_ZOMBIE;
281 if (thread_info == NULL)
283 /* New thread. Attach to it now (why wait?). */
284 attach_thread (thread_ptid, thp, &ti, 1);
285 thread_info = find_thread_pid (thread_ptid);
286 gdb_assert (thread_info != NULL);
289 memcpy (&thread_info->private->th, thp, sizeof (*thp));
290 thread_info->private->th_valid = 1;
291 memcpy (&thread_info->private->ti, &ti, sizeof (ti));
292 thread_info->private->ti_valid = 1;
295 *(struct thread_info **) infop = thread_info;
300 /* Accessor functions for the thread_db information, with caching. */
303 thread_db_map_id2thr (struct thread_info *thread_info, int fatal)
307 if (thread_info->private->th_valid)
310 err = td_ta_map_id2thr_p (thread_agent, GET_THREAD (thread_info->ptid),
311 &thread_info->private->th);
315 error (_("Cannot find thread %ld: %s"),
316 (long) GET_THREAD (thread_info->ptid),
317 thread_db_err_str (err));
320 thread_info->private->th_valid = 1;
323 /* Convert between user-level thread ids and LWP ids. */
326 thread_from_lwp (ptid_t ptid)
330 struct thread_info *thread_info;
333 if (GET_LWP (ptid) == 0)
334 ptid = BUILD_LWP (GET_PID (ptid), GET_PID (ptid));
336 gdb_assert (is_lwp (ptid));
338 err = td_ta_map_lwp2thr_p (thread_agent, GET_LWP (ptid), &th);
340 error (_("Cannot find user-level thread for LWP %ld: %s"),
341 GET_LWP (ptid), thread_db_err_str (err));
345 /* Fetch the thread info. If we get back TD_THR_ZOMBIE, then the
346 event thread has already died. If another gdb interface has called
347 thread_alive() previously, the thread won't be found on the thread list
348 anymore. In that case, we don't want to process this ptid anymore
349 to avoid the possibility of later treating it as a newly
350 discovered thread id that we should add to the list. Thus,
351 we return a -1 ptid which is also how the thread list marks a
353 if (thread_get_info_callback (&th, &thread_info) == TD_THR_ZOMBIE
354 && thread_info == NULL)
355 return pid_to_ptid (-1);
357 gdb_assert (thread_info && thread_info->private->ti_valid);
359 return ptid_build (GET_PID (ptid), GET_LWP (ptid),
360 thread_info->private->ti.ti_tid);
364 lwp_from_thread (ptid_t ptid)
366 return BUILD_LWP (GET_LWP (ptid), GET_PID (ptid));
371 thread_db_init (struct target_ops *target)
373 target_beneath = target;
377 verbose_dlsym (void *handle, const char *name)
379 void *sym = dlsym (handle, name);
381 warning (_("Symbol \"%s\" not found in libthread_db: %s"), name, dlerror ());
386 thread_db_load (void)
391 handle = dlopen (LIBTHREAD_DB_SO, RTLD_NOW);
394 fprintf_filtered (gdb_stderr, "\n\ndlopen failed on '%s' - %s\n",
395 LIBTHREAD_DB_SO, dlerror ());
396 fprintf_filtered (gdb_stderr,
397 "GDB will not be able to debug pthreads.\n\n");
401 /* Initialize pointers to the dynamic library functions we will use.
402 Essential functions first. */
404 td_init_p = verbose_dlsym (handle, "td_init");
405 if (td_init_p == NULL)
408 td_ta_new_p = verbose_dlsym (handle, "td_ta_new");
409 if (td_ta_new_p == NULL)
412 td_ta_map_id2thr_p = verbose_dlsym (handle, "td_ta_map_id2thr");
413 if (td_ta_map_id2thr_p == NULL)
416 td_ta_map_lwp2thr_p = verbose_dlsym (handle, "td_ta_map_lwp2thr");
417 if (td_ta_map_lwp2thr_p == NULL)
420 td_ta_thr_iter_p = verbose_dlsym (handle, "td_ta_thr_iter");
421 if (td_ta_thr_iter_p == NULL)
424 td_thr_validate_p = verbose_dlsym (handle, "td_thr_validate");
425 if (td_thr_validate_p == NULL)
428 td_thr_get_info_p = verbose_dlsym (handle, "td_thr_get_info");
429 if (td_thr_get_info_p == NULL)
432 /* Initialize the library. */
436 warning (_("Cannot initialize libthread_db: %s"), thread_db_err_str (err));
440 /* These are not essential. */
441 td_ta_event_addr_p = dlsym (handle, "td_ta_event_addr");
442 td_ta_set_event_p = dlsym (handle, "td_ta_set_event");
443 td_ta_event_getmsg_p = dlsym (handle, "td_ta_event_getmsg");
444 td_thr_event_enable_p = dlsym (handle, "td_thr_event_enable");
445 td_thr_tls_get_addr_p = dlsym (handle, "td_thr_tls_get_addr");
451 enable_thread_event (td_thragent_t *thread_agent, int event, CORE_ADDR *bp)
456 /* Get the breakpoint address for thread EVENT. */
457 err = td_ta_event_addr_p (thread_agent, event, ¬ify);
461 /* Set up the breakpoint. */
462 gdb_assert (exec_bfd);
463 (*bp) = (gdbarch_convert_from_func_ptr_addr
465 /* Do proper sign extension for the target. */
466 (bfd_get_sign_extend_vma (exec_bfd) > 0
467 ? (CORE_ADDR) (intptr_t) notify.u.bptaddr
468 : (CORE_ADDR) (uintptr_t) notify.u.bptaddr),
470 create_thread_event_breakpoint ((*bp));
476 enable_thread_event_reporting (void)
478 td_thr_events_t events;
481 #ifdef HAVE_GNU_LIBC_VERSION_H
482 const char *libc_version;
483 int libc_major, libc_minor;
486 /* We cannot use the thread event reporting facility if these
487 functions aren't available. */
488 if (td_ta_event_addr_p == NULL || td_ta_set_event_p == NULL
489 || td_ta_event_getmsg_p == NULL || td_thr_event_enable_p == NULL)
492 /* Set the process wide mask saying which events we're interested in. */
493 td_event_emptyset (&events);
494 td_event_addset (&events, TD_CREATE);
496 #ifdef HAVE_GNU_LIBC_VERSION_H
497 /* The event reporting facility is broken for TD_DEATH events in
498 glibc 2.1.3, so don't enable it if we have glibc but a lower
500 libc_version = gnu_get_libc_version ();
501 if (sscanf (libc_version, "%d.%d", &libc_major, &libc_minor) == 2
502 && (libc_major > 2 || (libc_major == 2 && libc_minor > 1)))
504 td_event_addset (&events, TD_DEATH);
506 err = td_ta_set_event_p (thread_agent, &events);
509 warning (_("Unable to set global thread event mask: %s"),
510 thread_db_err_str (err));
514 /* Delete previous thread event breakpoints, if any. */
515 remove_thread_event_breakpoints ();
516 td_create_bp_addr = 0;
517 td_death_bp_addr = 0;
519 /* Set up the thread creation event. */
520 err = enable_thread_event (thread_agent, TD_CREATE, &td_create_bp_addr);
523 warning (_("Unable to get location for thread creation breakpoint: %s"),
524 thread_db_err_str (err));
528 /* Set up the thread death event. */
529 err = enable_thread_event (thread_agent, TD_DEATH, &td_death_bp_addr);
532 warning (_("Unable to get location for thread death breakpoint: %s"),
533 thread_db_err_str (err));
539 disable_thread_event_reporting (void)
541 td_thr_events_t events;
543 /* Set the process wide mask saying we aren't interested in any
545 td_event_emptyset (&events);
546 td_ta_set_event_p (thread_agent, &events);
548 /* Delete thread event breakpoints, if any. */
549 remove_thread_event_breakpoints ();
550 td_create_bp_addr = 0;
551 td_death_bp_addr = 0;
555 check_thread_signals (void)
557 #ifdef GET_THREAD_SIGNALS
563 GET_THREAD_SIGNALS (&mask);
564 sigemptyset (&thread_stop_set);
565 sigemptyset (&thread_print_set);
567 for (i = 1; i < NSIG; i++)
569 if (sigismember (&mask, i))
571 if (signal_stop_update (target_signal_from_host (i), 0))
572 sigaddset (&thread_stop_set, i);
573 if (signal_print_update (target_signal_from_host (i), 0))
574 sigaddset (&thread_print_set, i);
582 /* Check whether thread_db is usable. This function is called when
583 an inferior is created (or otherwise acquired, e.g. attached to)
584 and when new shared libraries are loaded into a running process. */
587 check_for_thread_db (void)
590 static int already_loaded;
592 /* Do nothing if we couldn't load libthread_db.so.1. */
593 if (td_ta_new_p == NULL)
596 /* First time through, report that libthread_db was successfuly
597 loaded. Can't print this in in thread_db_load as, at that stage,
598 the interpreter and it's console haven't started. */
603 const char *library = NULL;
604 if (dladdr ((*td_ta_new_p), &info) != 0)
605 library = info.dli_fname;
610 /* Paranoid - don't let a NULL path slip through. */
611 library = LIBTHREAD_DB_SO;
613 printf_unfiltered (_("Using host libthread_db library \"%s\".\n"),
619 /* Nothing to do. The thread library was already detected and the
620 target vector was already activated. */
623 /* Don't attempt to use thread_db on targets which can not run
624 (executables not running yet, core files) for now. */
625 if (!target_has_execution)
628 /* Don't attempt to use thread_db for remote targets. */
629 if (!target_can_run (¤t_target))
632 /* Initialize the structure that identifies the child process. */
633 proc_handle.pid = GET_PID (inferior_ptid);
635 /* Now attempt to open a connection to the thread library. */
636 err = td_ta_new_p (&proc_handle, &thread_agent);
640 /* No thread library was detected. */
644 printf_unfiltered (_("[Thread debugging using libthread_db enabled]\n"));
646 /* The thread library was detected. Activate the thread_db target. */
647 push_target (&thread_db_ops);
650 enable_thread_event_reporting ();
651 thread_db_find_new_threads ();
655 warning (_("Cannot initialize thread debugging library: %s"),
656 thread_db_err_str (err));
662 thread_db_new_objfile (struct objfile *objfile)
665 check_for_thread_db ();
668 /* Attach to a new thread. This function is called when we receive a
669 TD_CREATE event or when we iterate over all threads and find one
670 that wasn't already in our list. */
673 attach_thread (ptid_t ptid, const td_thrhandle_t *th_p,
674 const td_thrinfo_t *ti_p, int verbose)
676 struct thread_info *tp;
679 /* If we're being called after a TD_CREATE event, we may already
680 know about this thread. There are two ways this can happen. We
681 may have iterated over all threads between the thread creation
682 and the TD_CREATE event, for instance when the user has issued
683 the `info threads' command before the SIGTRAP for hitting the
684 thread creation breakpoint was reported. Alternatively, the
685 thread may have exited and a new one been created with the same
686 thread ID. In the first case we don't need to do anything; in
687 the second case we should discard information about the dead
688 thread and attach to the new one. */
689 if (in_thread_list (ptid))
691 tp = find_thread_pid (ptid);
692 gdb_assert (tp != NULL);
694 if (!tp->private->dying)
697 delete_thread (ptid);
700 check_thread_signals ();
702 if (ti_p->ti_state == TD_THR_UNKNOWN || ti_p->ti_state == TD_THR_ZOMBIE)
703 return; /* A zombie thread -- do not attach. */
705 /* Under GNU/Linux, we have to attach to each and every thread. */
706 if (lin_lwp_attach_lwp (BUILD_LWP (ti_p->ti_lid, GET_PID (ptid)), 0) < 0)
709 /* Add the thread to GDB's thread list. */
710 tp = add_thread (ptid);
711 tp->private = xmalloc (sizeof (struct private_thread_info));
712 memset (tp->private, 0, sizeof (struct private_thread_info));
715 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid));
717 /* Enable thread event reporting for this thread. */
718 err = td_thr_event_enable_p (th_p, 1);
720 error (_("Cannot enable thread event reporting for %s: %s"),
721 target_pid_to_str (ptid), thread_db_err_str (err));
725 detach_thread (ptid_t ptid, int verbose)
727 struct thread_info *thread_info;
730 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (ptid));
732 /* Don't delete the thread now, because it still reports as active
733 until it has executed a few instructions after the event
734 breakpoint - if we deleted it now, "info threads" would cause us
735 to re-attach to it. Just mark it as having had a TD_DEATH
736 event. This means that we won't delete it from our thread list
737 until we notice that it's dead (via prune_threads), or until
738 something re-uses its thread ID. */
739 thread_info = find_thread_pid (ptid);
740 gdb_assert (thread_info != NULL);
741 thread_info->private->dying = 1;
745 thread_db_detach (char *args, int from_tty)
747 disable_thread_event_reporting ();
749 /* There's no need to save & restore inferior_ptid here, since the
750 inferior is not supposed to survive this function call. */
751 inferior_ptid = lwp_from_thread (inferior_ptid);
753 target_beneath->to_detach (args, from_tty);
755 /* Should this be done by detach_command? */
756 target_mourn_inferior ();
760 clear_lwpid_callback (struct thread_info *thread, void *dummy)
762 /* If we know that our thread implementation is 1-to-1, we could save
763 a certain amount of information; it's not clear how much, so we
764 are always conservative. */
766 thread->private->th_valid = 0;
767 thread->private->ti_valid = 0;
773 thread_db_resume (ptid_t ptid, int step, enum target_signal signo)
775 struct cleanup *old_chain = save_inferior_ptid ();
777 if (GET_PID (ptid) == -1)
778 inferior_ptid = lwp_from_thread (inferior_ptid);
779 else if (is_thread (ptid))
780 ptid = lwp_from_thread (ptid);
782 /* Clear cached data which may not be valid after the resume. */
783 iterate_over_threads (clear_lwpid_callback, NULL);
785 target_beneath->to_resume (ptid, step, signo);
787 do_cleanups (old_chain);
790 /* Check if PID is currently stopped at the location of a thread event
791 breakpoint location. If it is, read the event message and act upon
795 check_event (ptid_t ptid)
803 /* Bail out early if we're not at a thread event breakpoint. */
804 stop_pc = read_pc_pid (ptid) - gdbarch_decr_pc_after_break (current_gdbarch);
805 if (stop_pc != td_create_bp_addr && stop_pc != td_death_bp_addr)
808 /* If we are at a create breakpoint, we do not know what new lwp
809 was created and cannot specifically locate the event message for it.
810 We have to call td_ta_event_getmsg() to get
811 the latest message. Since we have no way of correlating whether
812 the event message we get back corresponds to our breakpoint, we must
813 loop and read all event messages, processing them appropriately.
814 This guarantees we will process the correct message before continuing
817 Currently, death events are not enabled. If they are enabled,
818 the death event can use the td_thr_event_getmsg() interface to
819 get the message specifically for that lwp and avoid looping
826 err = td_ta_event_getmsg_p (thread_agent, &msg);
832 error (_("Cannot get thread event message: %s"),
833 thread_db_err_str (err));
836 err = td_thr_get_info_p (msg.th_p, &ti);
838 error (_("Cannot get thread info: %s"), thread_db_err_str (err));
840 ptid = ptid_build (GET_PID (ptid), ti.ti_lid, ti.ti_tid);
845 /* Call attach_thread whether or not we already know about a
846 thread with this thread ID. */
847 attach_thread (ptid, msg.th_p, &ti, 1);
853 if (!in_thread_list (ptid))
854 error (_("Spurious thread death event."));
856 detach_thread (ptid, 1);
861 error (_("Spurious thread event."));
868 thread_db_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
870 extern ptid_t trap_ptid;
872 if (GET_PID (ptid) != -1 && is_thread (ptid))
873 ptid = lwp_from_thread (ptid);
875 ptid = target_beneath->to_wait (ptid, ourstatus);
877 if (ourstatus->kind == TARGET_WAITKIND_EXITED
878 || ourstatus->kind == TARGET_WAITKIND_SIGNALLED)
879 return pid_to_ptid (-1);
881 if (ourstatus->kind == TARGET_WAITKIND_EXECD)
883 remove_thread_event_breakpoints ();
884 unpush_target (&thread_db_ops);
887 return pid_to_ptid (GET_PID (ptid));
890 /* If we do not know about the main thread yet, this would be a good time to
892 if (ourstatus->kind == TARGET_WAITKIND_STOPPED && !have_threads ())
893 thread_db_find_new_threads ();
895 if (ourstatus->kind == TARGET_WAITKIND_STOPPED
896 && ourstatus->value.sig == TARGET_SIGNAL_TRAP)
897 /* Check for a thread event. */
902 /* Change ptids back into the higher level PID + TID format. If
903 the thread is dead and no longer on the thread list, we will
904 get back a dead ptid. This can occur if the thread death
905 event gets postponed by other simultaneous events. In such a
906 case, we want to just ignore the event and continue on. */
908 if (!ptid_equal (trap_ptid, null_ptid))
909 trap_ptid = thread_from_lwp (trap_ptid);
911 ptid = thread_from_lwp (ptid);
912 if (GET_PID (ptid) == -1)
913 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
920 thread_db_kill (void)
922 /* There's no need to save & restore inferior_ptid here, since the
923 inferior isn't supposed to survive this function call. */
924 inferior_ptid = lwp_from_thread (inferior_ptid);
925 target_beneath->to_kill ();
929 thread_db_mourn_inferior (void)
931 /* Forget about the child's process ID. We shouldn't need it
935 target_beneath->to_mourn_inferior ();
937 /* Delete the old thread event breakpoints. Do this after mourning
938 the inferior, so that we don't try to uninsert them. */
939 remove_thread_event_breakpoints ();
941 /* Detach thread_db target ops. */
942 unpush_target (&thread_db_ops);
947 find_new_threads_callback (const td_thrhandle_t *th_p, void *data)
953 err = td_thr_get_info_p (th_p, &ti);
955 error (_("find_new_threads_callback: cannot get thread info: %s"),
956 thread_db_err_str (err));
958 if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
959 return 0; /* A zombie -- ignore. */
961 ptid = ptid_build (GET_PID (inferior_ptid), ti.ti_lid, ti.ti_tid);
965 /* A thread ID of zero means that this is the main thread, but
966 glibc has not yet initialized thread-local storage and the
967 pthread library. We do not know what the thread's TID will
968 be yet. Just enable event reporting and otherwise ignore
971 err = td_thr_event_enable_p (th_p, 1);
973 error (_("Cannot enable thread event reporting for %s: %s"),
974 target_pid_to_str (ptid), thread_db_err_str (err));
979 if (!in_thread_list (ptid))
980 attach_thread (ptid, th_p, &ti, 1);
986 thread_db_find_new_threads (void)
990 /* Iterate over all user-space threads to discover new threads. */
991 err = td_ta_thr_iter_p (thread_agent, find_new_threads_callback, NULL,
992 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
993 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
995 error (_("Cannot find new threads: %s"), thread_db_err_str (err));
999 thread_db_pid_to_str (ptid_t ptid)
1001 if (is_thread (ptid))
1003 static char buf[64];
1004 struct thread_info *thread_info;
1006 thread_info = find_thread_pid (ptid);
1007 if (thread_info == NULL)
1008 snprintf (buf, sizeof (buf), "Thread 0x%lx (LWP %ld) (Missing)",
1009 GET_THREAD (ptid), GET_LWP (ptid));
1011 snprintf (buf, sizeof (buf), "Thread 0x%lx (LWP %ld)",
1012 GET_THREAD (ptid), GET_LWP (ptid));
1017 if (target_beneath->to_pid_to_str (ptid))
1018 return target_beneath->to_pid_to_str (ptid);
1020 return normal_pid_to_str (ptid);
1023 /* Return a string describing the state of the thread specified by
1027 thread_db_extra_thread_info (struct thread_info *info)
1029 if (info->private->dying)
1035 /* Return 1 if this thread has the same LWP as the passed PTID. */
1038 same_ptid_callback (struct thread_info *thread, void *arg)
1040 ptid_t *ptid_p = arg;
1042 return GET_LWP (thread->ptid) == GET_LWP (*ptid_p);
1045 /* Get the address of the thread local variable in load module LM which
1046 is stored at OFFSET within the thread local storage for thread PTID. */
1049 thread_db_get_thread_local_address (ptid_t ptid,
1053 /* If we have not discovered any threads yet, check now. */
1054 if (!is_thread (ptid) && !have_threads ())
1055 thread_db_find_new_threads ();
1057 /* Try to find a matching thread if we still have the LWP ID instead
1058 of the thread ID. */
1059 if (!is_thread (ptid))
1061 struct thread_info *thread;
1063 thread = iterate_over_threads (same_ptid_callback, &ptid);
1065 ptid = thread->ptid;
1068 if (is_thread (ptid))
1072 struct thread_info *thread_info;
1074 /* glibc doesn't provide the needed interface. */
1075 if (!td_thr_tls_get_addr_p)
1076 throw_error (TLS_NO_LIBRARY_SUPPORT_ERROR,
1077 _("No TLS library support"));
1079 /* Caller should have verified that lm != 0. */
1080 gdb_assert (lm != 0);
1082 /* Get info about the thread. */
1083 thread_info = find_thread_pid (ptid);
1084 thread_db_map_id2thr (thread_info, 1);
1086 /* Finally, get the address of the variable. */
1087 err = td_thr_tls_get_addr_p (&thread_info->private->th,
1088 (void *)(size_t) lm,
1091 #ifdef THREAD_DB_HAS_TD_NOTALLOC
1092 /* The memory hasn't been allocated, yet. */
1093 if (err == TD_NOTALLOC)
1094 /* Now, if libthread_db provided the initialization image's
1095 address, we *could* try to build a non-lvalue value from
1096 the initialization image. */
1097 throw_error (TLS_NOT_ALLOCATED_YET_ERROR,
1098 _("TLS not allocated yet"));
1101 /* Something else went wrong. */
1103 throw_error (TLS_GENERIC_ERROR,
1104 (("%s")), thread_db_err_str (err));
1106 /* Cast assuming host == target. Joy. */
1107 /* Do proper sign extension for the target. */
1108 gdb_assert (exec_bfd);
1109 return (bfd_get_sign_extend_vma (exec_bfd) > 0
1110 ? (CORE_ADDR) (intptr_t) address
1111 : (CORE_ADDR) (uintptr_t) address);
1114 if (target_beneath->to_get_thread_local_address)
1115 return target_beneath->to_get_thread_local_address (ptid, lm, offset);
1117 throw_error (TLS_GENERIC_ERROR,
1118 _("TLS not supported on this target"));
1122 init_thread_db_ops (void)
1124 thread_db_ops.to_shortname = "multi-thread";
1125 thread_db_ops.to_longname = "multi-threaded child process.";
1126 thread_db_ops.to_doc = "Threads and pthreads support.";
1127 thread_db_ops.to_detach = thread_db_detach;
1128 thread_db_ops.to_resume = thread_db_resume;
1129 thread_db_ops.to_wait = thread_db_wait;
1130 thread_db_ops.to_kill = thread_db_kill;
1131 thread_db_ops.to_mourn_inferior = thread_db_mourn_inferior;
1132 thread_db_ops.to_find_new_threads = thread_db_find_new_threads;
1133 thread_db_ops.to_pid_to_str = thread_db_pid_to_str;
1134 thread_db_ops.to_stratum = thread_stratum;
1135 thread_db_ops.to_has_thread_control = tc_schedlock;
1136 thread_db_ops.to_get_thread_local_address
1137 = thread_db_get_thread_local_address;
1138 thread_db_ops.to_extra_thread_info = thread_db_extra_thread_info;
1139 thread_db_ops.to_magic = OPS_MAGIC;
1143 _initialize_thread_db (void)
1145 /* Only initialize the module if we can load libthread_db. */
1146 if (thread_db_load ())
1148 init_thread_db_ops ();
1149 add_target (&thread_db_ops);
1151 /* Add ourselves to objfile event chain. */
1152 observer_attach_new_objfile (thread_db_new_objfile);