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"
40 #include "linux-nat.h"
44 #ifdef HAVE_GNU_LIBC_VERSION_H
45 #include <gnu/libc-version.h>
48 #ifndef LIBTHREAD_DB_SO
49 #define LIBTHREAD_DB_SO "libthread_db.so.1"
52 /* If we're running on GNU/Linux, we must explicitly attach to any new
55 /* This module's target vector. */
56 static struct target_ops thread_db_ops;
58 /* The target vector that we call for things this module can't handle. */
59 static struct target_ops *target_beneath;
61 /* Pointer to the next function on the objfile event chain. */
62 static void (*target_new_objfile_chain) (struct objfile * objfile);
64 /* Non-zero if we're using this module's target vector. */
65 static int using_thread_db;
67 /* Non-zero if we have determined the signals used by the threads
69 static int thread_signals;
70 static sigset_t thread_stop_set;
71 static sigset_t thread_print_set;
73 /* Structure that identifies the child process for the
74 <proc_service.h> interface. */
75 static struct ps_prochandle proc_handle;
77 /* Connection to the libthread_db library. */
78 static td_thragent_t *thread_agent;
80 /* Pointers to the libthread_db functions. */
82 static td_err_e (*td_init_p) (void);
84 static td_err_e (*td_ta_new_p) (struct ps_prochandle * ps,
86 static td_err_e (*td_ta_map_id2thr_p) (const td_thragent_t *ta, thread_t pt,
87 td_thrhandle_t *__th);
88 static td_err_e (*td_ta_map_lwp2thr_p) (const td_thragent_t *ta,
89 lwpid_t lwpid, td_thrhandle_t *th);
90 static td_err_e (*td_ta_thr_iter_p) (const td_thragent_t *ta,
91 td_thr_iter_f *callback, void *cbdata_p,
92 td_thr_state_e state, int ti_pri,
93 sigset_t *ti_sigmask_p,
94 unsigned int ti_user_flags);
95 static td_err_e (*td_ta_event_addr_p) (const td_thragent_t *ta,
96 td_event_e event, td_notify_t *ptr);
97 static td_err_e (*td_ta_set_event_p) (const td_thragent_t *ta,
98 td_thr_events_t *event);
99 static td_err_e (*td_ta_event_getmsg_p) (const td_thragent_t *ta,
100 td_event_msg_t *msg);
102 static td_err_e (*td_thr_validate_p) (const td_thrhandle_t *th);
103 static td_err_e (*td_thr_get_info_p) (const td_thrhandle_t *th,
104 td_thrinfo_t *infop);
105 static td_err_e (*td_thr_event_enable_p) (const td_thrhandle_t *th,
108 static td_err_e (*td_thr_tls_get_addr_p) (const td_thrhandle_t *th,
110 size_t offset, void **address);
112 /* Location of the thread creation event breakpoint. The code at this
113 location in the child process will be called by the pthread library
114 whenever a new thread is created. By setting a special breakpoint
115 at this location, GDB can detect when a new thread is created. We
116 obtain this location via the td_ta_event_addr call. */
117 static CORE_ADDR td_create_bp_addr;
119 /* Location of the thread death event breakpoint. */
120 static CORE_ADDR td_death_bp_addr;
122 /* Prototypes for local functions. */
123 static void thread_db_find_new_threads (void);
124 static void attach_thread (ptid_t ptid, const td_thrhandle_t *th_p,
125 const td_thrinfo_t *ti_p, int verbose);
126 static void detach_thread (ptid_t ptid, int verbose);
129 /* Building process ids. */
131 #define GET_PID(ptid) ptid_get_pid (ptid)
132 #define GET_LWP(ptid) ptid_get_lwp (ptid)
133 #define GET_THREAD(ptid) ptid_get_tid (ptid)
135 #define is_lwp(ptid) (GET_LWP (ptid) != 0)
136 #define is_thread(ptid) (GET_THREAD (ptid) != 0)
138 #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
141 /* Use "struct private_thread_info" to cache thread state. This is
142 a substantial optimization. */
144 struct private_thread_info
146 /* Flag set when we see a TD_DEATH event for this thread. */
147 unsigned int dying:1;
149 /* Cached thread state. */
150 unsigned int th_valid:1;
151 unsigned int ti_valid:1;
159 thread_db_err_str (td_err_e err)
166 return "generic 'call succeeded'";
168 return "generic error";
170 return "no thread to satisfy query";
172 return "no sync handle to satisfy query";
174 return "no LWP to satisfy query";
176 return "invalid process handle";
178 return "invalid thread handle";
180 return "invalid synchronization handle";
182 return "invalid thread agent";
184 return "invalid key";
186 return "no event message for getmsg";
188 return "FPU register set not available";
190 return "application not linked with libthread";
192 return "requested event is not supported";
194 return "capability not available";
196 return "debugger service failed";
198 return "operation not applicable to";
200 return "no thread-specific data for this thread";
202 return "malloc failed";
204 return "only part of register set was written/read";
206 return "X register set not available for this thread";
207 #ifdef THREAD_DB_HAS_TD_NOTALLOC
209 return "thread has not yet allocated TLS for given module";
211 #ifdef THREAD_DB_HAS_TD_VERSION
213 return "versions of libpthread and libthread_db do not match";
215 #ifdef THREAD_DB_HAS_TD_NOTLS
217 return "there is no TLS segment in the given module";
220 snprintf (buf, sizeof (buf), "unknown thread_db error '%d'", err);
225 /* A callback function for td_ta_thr_iter, which we use to map all
228 THP is a handle to the current thread; if INFOP is not NULL, the
229 struct thread_info associated with this thread is returned in
232 If the thread is a zombie, TD_THR_ZOMBIE is returned. Otherwise,
233 zero is returned to indicate success. */
236 thread_get_info_callback (const td_thrhandle_t *thp, void *infop)
240 struct thread_info *thread_info;
243 err = td_thr_get_info_p (thp, &ti);
245 error (_("thread_get_info_callback: cannot get thread info: %s"),
246 thread_db_err_str (err));
248 /* Fill the cache. */
249 thread_ptid = ptid_build (GET_PID (inferior_ptid), ti.ti_lid, ti.ti_tid);
250 thread_info = find_thread_pid (thread_ptid);
252 /* In the case of a zombie thread, don't continue. We don't want to
253 attach to it thinking it is a new thread. */
254 if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
257 *(struct thread_info **) infop = thread_info;
258 if (thread_info != NULL)
260 memcpy (&thread_info->private->th, thp, sizeof (*thp));
261 thread_info->private->th_valid = 1;
262 memcpy (&thread_info->private->ti, &ti, sizeof (ti));
263 thread_info->private->ti_valid = 1;
265 return TD_THR_ZOMBIE;
268 if (thread_info == NULL)
270 /* New thread. Attach to it now (why wait?). */
271 attach_thread (thread_ptid, thp, &ti, 1);
272 thread_info = find_thread_pid (thread_ptid);
273 gdb_assert (thread_info != NULL);
276 memcpy (&thread_info->private->th, thp, sizeof (*thp));
277 thread_info->private->th_valid = 1;
278 memcpy (&thread_info->private->ti, &ti, sizeof (ti));
279 thread_info->private->ti_valid = 1;
282 *(struct thread_info **) infop = thread_info;
287 /* Accessor functions for the thread_db information, with caching. */
290 thread_db_map_id2thr (struct thread_info *thread_info, int fatal)
294 if (thread_info->private->th_valid)
297 err = td_ta_map_id2thr_p (thread_agent, GET_THREAD (thread_info->ptid),
298 &thread_info->private->th);
302 error (_("Cannot find thread %ld: %s"),
303 (long) GET_THREAD (thread_info->ptid),
304 thread_db_err_str (err));
307 thread_info->private->th_valid = 1;
310 /* Convert between user-level thread ids and LWP ids. */
313 thread_from_lwp (ptid_t ptid)
317 struct thread_info *thread_info;
320 if (GET_LWP (ptid) == 0)
321 ptid = BUILD_LWP (GET_PID (ptid), GET_PID (ptid));
323 gdb_assert (is_lwp (ptid));
325 err = td_ta_map_lwp2thr_p (thread_agent, GET_LWP (ptid), &th);
327 error (_("Cannot find user-level thread for LWP %ld: %s"),
328 GET_LWP (ptid), thread_db_err_str (err));
332 /* Fetch the thread info. If we get back TD_THR_ZOMBIE, then the
333 event thread has already died. If another gdb interface has called
334 thread_alive() previously, the thread won't be found on the thread list
335 anymore. In that case, we don't want to process this ptid anymore
336 to avoid the possibility of later treating it as a newly
337 discovered thread id that we should add to the list. Thus,
338 we return a -1 ptid which is also how the thread list marks a
340 if (thread_get_info_callback (&th, &thread_info) == TD_THR_ZOMBIE
341 && thread_info == NULL)
342 return pid_to_ptid (-1);
344 gdb_assert (thread_info && thread_info->private->ti_valid);
346 return ptid_build (GET_PID (ptid), GET_LWP (ptid),
347 thread_info->private->ti.ti_tid);
351 lwp_from_thread (ptid_t ptid)
353 return BUILD_LWP (GET_LWP (ptid), GET_PID (ptid));
358 thread_db_init (struct target_ops *target)
360 target_beneath = target;
364 verbose_dlsym (void *handle, const char *name)
366 void *sym = dlsym (handle, name);
368 warning (_("Symbol \"%s\" not found in libthread_db: %s"), name, dlerror ());
373 thread_db_load (void)
378 handle = dlopen (LIBTHREAD_DB_SO, RTLD_NOW);
381 fprintf_filtered (gdb_stderr, "\n\ndlopen failed on '%s' - %s\n",
382 LIBTHREAD_DB_SO, dlerror ());
383 fprintf_filtered (gdb_stderr,
384 "GDB will not be able to debug pthreads.\n\n");
388 /* Initialize pointers to the dynamic library functions we will use.
389 Essential functions first. */
391 td_init_p = verbose_dlsym (handle, "td_init");
392 if (td_init_p == NULL)
395 td_ta_new_p = verbose_dlsym (handle, "td_ta_new");
396 if (td_ta_new_p == NULL)
399 td_ta_map_id2thr_p = verbose_dlsym (handle, "td_ta_map_id2thr");
400 if (td_ta_map_id2thr_p == NULL)
403 td_ta_map_lwp2thr_p = verbose_dlsym (handle, "td_ta_map_lwp2thr");
404 if (td_ta_map_lwp2thr_p == NULL)
407 td_ta_thr_iter_p = verbose_dlsym (handle, "td_ta_thr_iter");
408 if (td_ta_thr_iter_p == NULL)
411 td_thr_validate_p = verbose_dlsym (handle, "td_thr_validate");
412 if (td_thr_validate_p == NULL)
415 td_thr_get_info_p = verbose_dlsym (handle, "td_thr_get_info");
416 if (td_thr_get_info_p == NULL)
419 /* Initialize the library. */
423 warning (_("Cannot initialize libthread_db: %s"), thread_db_err_str (err));
427 /* These are not essential. */
428 td_ta_event_addr_p = dlsym (handle, "td_ta_event_addr");
429 td_ta_set_event_p = dlsym (handle, "td_ta_set_event");
430 td_ta_event_getmsg_p = dlsym (handle, "td_ta_event_getmsg");
431 td_thr_event_enable_p = dlsym (handle, "td_thr_event_enable");
432 td_thr_tls_get_addr_p = dlsym (handle, "td_thr_tls_get_addr");
438 enable_thread_event (td_thragent_t *thread_agent, int event, CORE_ADDR *bp)
443 /* Get the breakpoint address for thread EVENT. */
444 err = td_ta_event_addr_p (thread_agent, event, ¬ify);
448 /* Set up the breakpoint. */
449 gdb_assert (exec_bfd);
450 (*bp) = (gdbarch_convert_from_func_ptr_addr
452 /* Do proper sign extension for the target. */
453 (bfd_get_sign_extend_vma (exec_bfd) > 0
454 ? (CORE_ADDR) (intptr_t) notify.u.bptaddr
455 : (CORE_ADDR) (uintptr_t) notify.u.bptaddr),
457 create_thread_event_breakpoint ((*bp));
463 enable_thread_event_reporting (void)
465 td_thr_events_t events;
468 #ifdef HAVE_GNU_LIBC_VERSION_H
469 const char *libc_version;
470 int libc_major, libc_minor;
473 /* We cannot use the thread event reporting facility if these
474 functions aren't available. */
475 if (td_ta_event_addr_p == NULL || td_ta_set_event_p == NULL
476 || td_ta_event_getmsg_p == NULL || td_thr_event_enable_p == NULL)
479 /* Set the process wide mask saying which events we're interested in. */
480 td_event_emptyset (&events);
481 td_event_addset (&events, TD_CREATE);
483 #ifdef HAVE_GNU_LIBC_VERSION_H
484 /* The event reporting facility is broken for TD_DEATH events in
485 glibc 2.1.3, so don't enable it if we have glibc but a lower
487 libc_version = gnu_get_libc_version ();
488 if (sscanf (libc_version, "%d.%d", &libc_major, &libc_minor) == 2
489 && (libc_major > 2 || (libc_major == 2 && libc_minor > 1)))
491 td_event_addset (&events, TD_DEATH);
493 err = td_ta_set_event_p (thread_agent, &events);
496 warning (_("Unable to set global thread event mask: %s"),
497 thread_db_err_str (err));
501 /* Delete previous thread event breakpoints, if any. */
502 remove_thread_event_breakpoints ();
503 td_create_bp_addr = 0;
504 td_death_bp_addr = 0;
506 /* Set up the thread creation event. */
507 err = enable_thread_event (thread_agent, TD_CREATE, &td_create_bp_addr);
510 warning (_("Unable to get location for thread creation breakpoint: %s"),
511 thread_db_err_str (err));
515 /* Set up the thread death event. */
516 err = enable_thread_event (thread_agent, TD_DEATH, &td_death_bp_addr);
519 warning (_("Unable to get location for thread death breakpoint: %s"),
520 thread_db_err_str (err));
526 disable_thread_event_reporting (void)
528 td_thr_events_t events;
530 /* Set the process wide mask saying we aren't interested in any
532 td_event_emptyset (&events);
533 td_ta_set_event_p (thread_agent, &events);
535 /* Delete thread event breakpoints, if any. */
536 remove_thread_event_breakpoints ();
537 td_create_bp_addr = 0;
538 td_death_bp_addr = 0;
542 check_thread_signals (void)
544 #ifdef GET_THREAD_SIGNALS
550 GET_THREAD_SIGNALS (&mask);
551 sigemptyset (&thread_stop_set);
552 sigemptyset (&thread_print_set);
554 for (i = 1; i < NSIG; i++)
556 if (sigismember (&mask, i))
558 if (signal_stop_update (target_signal_from_host (i), 0))
559 sigaddset (&thread_stop_set, i);
560 if (signal_print_update (target_signal_from_host (i), 0))
561 sigaddset (&thread_print_set, i);
569 /* Check whether thread_db is usable. This function is called when
570 an inferior is created (or otherwise acquired, e.g. attached to)
571 and when new shared libraries are loaded into a running process. */
574 check_for_thread_db (void)
577 static int already_loaded;
579 /* Do nothing if we couldn't load libthread_db.so.1. */
580 if (td_ta_new_p == NULL)
583 /* First time through, report that libthread_db was successfuly
584 loaded. Can't print this in in thread_db_load as, at that stage,
585 the interpreter and it's console haven't started. */
590 const char *library = NULL;
591 if (dladdr ((*td_ta_new_p), &info) != 0)
592 library = info.dli_fname;
597 /* Paranoid - don't let a NULL path slip through. */
598 library = LIBTHREAD_DB_SO;
600 printf_unfiltered (_("Using host libthread_db library \"%s\".\n"),
606 /* Nothing to do. The thread library was already detected and the
607 target vector was already activated. */
610 /* Don't attempt to use thread_db on targets which can not run
611 (executables not running yet, core files) for now. */
612 if (!target_has_execution)
615 /* Don't attempt to use thread_db for remote targets. */
616 if (!target_can_run (¤t_target))
619 /* Initialize the structure that identifies the child process. */
620 proc_handle.pid = GET_PID (inferior_ptid);
622 /* Now attempt to open a connection to the thread library. */
623 err = td_ta_new_p (&proc_handle, &thread_agent);
627 /* No thread library was detected. */
631 printf_unfiltered (_("[Thread debugging using libthread_db enabled]\n"));
633 /* The thread library was detected. Activate the thread_db target. */
634 push_target (&thread_db_ops);
637 enable_thread_event_reporting ();
638 thread_db_find_new_threads ();
642 warning (_("Cannot initialize thread debugging library: %s"),
643 thread_db_err_str (err));
649 thread_db_new_objfile (struct objfile *objfile)
652 check_for_thread_db ();
654 if (target_new_objfile_chain)
655 target_new_objfile_chain (objfile);
658 /* Attach to a new thread. This function is called when we receive a
659 TD_CREATE event or when we iterate over all threads and find one
660 that wasn't already in our list. */
663 attach_thread (ptid_t ptid, const td_thrhandle_t *th_p,
664 const td_thrinfo_t *ti_p, int verbose)
666 struct thread_info *tp;
669 /* If we're being called after a TD_CREATE event, we may already
670 know about this thread. There are two ways this can happen. We
671 may have iterated over all threads between the thread creation
672 and the TD_CREATE event, for instance when the user has issued
673 the `info threads' command before the SIGTRAP for hitting the
674 thread creation breakpoint was reported. Alternatively, the
675 thread may have exited and a new one been created with the same
676 thread ID. In the first case we don't need to do anything; in
677 the second case we should discard information about the dead
678 thread and attach to the new one. */
679 if (in_thread_list (ptid))
681 tp = find_thread_pid (ptid);
682 gdb_assert (tp != NULL);
684 if (!tp->private->dying)
687 delete_thread (ptid);
690 check_thread_signals ();
692 if (ti_p->ti_state == TD_THR_UNKNOWN || ti_p->ti_state == TD_THR_ZOMBIE)
693 return; /* A zombie thread -- do not attach. */
695 /* Under GNU/Linux, we have to attach to each and every thread. */
696 if (lin_lwp_attach_lwp (BUILD_LWP (ti_p->ti_lid, GET_PID (ptid)), 0) < 0)
699 /* Add the thread to GDB's thread list. */
700 tp = add_thread (ptid);
701 tp->private = xmalloc (sizeof (struct private_thread_info));
702 memset (tp->private, 0, sizeof (struct private_thread_info));
705 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid));
707 /* Enable thread event reporting for this thread. */
708 err = td_thr_event_enable_p (th_p, 1);
710 error (_("Cannot enable thread event reporting for %s: %s"),
711 target_pid_to_str (ptid), thread_db_err_str (err));
715 thread_db_attach (char *args, int from_tty)
717 target_beneath->to_attach (args, from_tty);
719 /* Destroy thread info; it's no longer valid. */
722 /* The child process is now the actual multi-threaded
723 program. Snatch its process ID... */
724 proc_handle.pid = GET_PID (inferior_ptid);
726 /* ...and perform the remaining initialization steps. */
727 enable_thread_event_reporting ();
728 thread_db_find_new_threads ();
732 detach_thread (ptid_t ptid, int verbose)
734 struct thread_info *thread_info;
737 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (ptid));
739 /* Don't delete the thread now, because it still reports as active
740 until it has executed a few instructions after the event
741 breakpoint - if we deleted it now, "info threads" would cause us
742 to re-attach to it. Just mark it as having had a TD_DEATH
743 event. This means that we won't delete it from our thread list
744 until we notice that it's dead (via prune_threads), or until
745 something re-uses its thread ID. */
746 thread_info = find_thread_pid (ptid);
747 gdb_assert (thread_info != NULL);
748 thread_info->private->dying = 1;
752 thread_db_detach (char *args, int from_tty)
754 disable_thread_event_reporting ();
756 /* There's no need to save & restore inferior_ptid here, since the
757 inferior is supposed to be survive this function call. */
758 inferior_ptid = lwp_from_thread (inferior_ptid);
760 /* Forget about the child's process ID. We shouldn't need it
764 target_beneath->to_detach (args, from_tty);
768 clear_lwpid_callback (struct thread_info *thread, void *dummy)
770 /* If we know that our thread implementation is 1-to-1, we could save
771 a certain amount of information; it's not clear how much, so we
772 are always conservative. */
774 thread->private->th_valid = 0;
775 thread->private->ti_valid = 0;
781 thread_db_resume (ptid_t ptid, int step, enum target_signal signo)
783 struct cleanup *old_chain = save_inferior_ptid ();
785 if (GET_PID (ptid) == -1)
786 inferior_ptid = lwp_from_thread (inferior_ptid);
787 else if (is_thread (ptid))
788 ptid = lwp_from_thread (ptid);
790 /* Clear cached data which may not be valid after the resume. */
791 iterate_over_threads (clear_lwpid_callback, NULL);
793 target_beneath->to_resume (ptid, step, signo);
795 do_cleanups (old_chain);
798 /* Check if PID is currently stopped at the location of a thread event
799 breakpoint location. If it is, read the event message and act upon
803 check_event (ptid_t ptid)
811 /* Bail out early if we're not at a thread event breakpoint. */
812 stop_pc = read_pc_pid (ptid) - DECR_PC_AFTER_BREAK;
813 if (stop_pc != td_create_bp_addr && stop_pc != td_death_bp_addr)
816 /* If we are at a create breakpoint, we do not know what new lwp
817 was created and cannot specifically locate the event message for it.
818 We have to call td_ta_event_getmsg() to get
819 the latest message. Since we have no way of correlating whether
820 the event message we get back corresponds to our breakpoint, we must
821 loop and read all event messages, processing them appropriately.
822 This guarantees we will process the correct message before continuing
825 Currently, death events are not enabled. If they are enabled,
826 the death event can use the td_thr_event_getmsg() interface to
827 get the message specifically for that lwp and avoid looping
834 err = td_ta_event_getmsg_p (thread_agent, &msg);
840 error (_("Cannot get thread event message: %s"),
841 thread_db_err_str (err));
844 err = td_thr_get_info_p (msg.th_p, &ti);
846 error (_("Cannot get thread info: %s"), thread_db_err_str (err));
848 ptid = ptid_build (GET_PID (ptid), ti.ti_lid, ti.ti_tid);
853 /* Call attach_thread whether or not we already know about a
854 thread with this thread ID. */
855 attach_thread (ptid, msg.th_p, &ti, 1);
861 if (!in_thread_list (ptid))
862 error (_("Spurious thread death event."));
864 detach_thread (ptid, 1);
869 error (_("Spurious thread event."));
876 thread_db_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
878 extern ptid_t trap_ptid;
880 if (GET_PID (ptid) != -1 && is_thread (ptid))
881 ptid = lwp_from_thread (ptid);
883 ptid = target_beneath->to_wait (ptid, ourstatus);
885 if (proc_handle.pid == 0)
886 /* The current child process isn't the actual multi-threaded
887 program yet, so don't try to do any special thread-specific
888 post-processing and bail out early. */
891 if (ourstatus->kind == TARGET_WAITKIND_EXITED
892 || ourstatus->kind == TARGET_WAITKIND_SIGNALLED)
893 return pid_to_ptid (-1);
895 if (ourstatus->kind == TARGET_WAITKIND_EXECD)
897 remove_thread_event_breakpoints ();
898 unpush_target (&thread_db_ops);
901 return pid_to_ptid (GET_PID (ptid));
904 if (ourstatus->kind == TARGET_WAITKIND_STOPPED
905 && ourstatus->value.sig == TARGET_SIGNAL_TRAP)
906 /* Check for a thread event. */
909 if (!ptid_equal (trap_ptid, null_ptid))
910 trap_ptid = thread_from_lwp (trap_ptid);
912 /* Change the ptid back into the higher level PID + TID format.
913 If the thread is dead and no longer on the thread list, we will
914 get back a dead ptid. This can occur if the thread death event
915 gets postponed by other simultaneous events. In such a case,
916 we want to just ignore the event and continue on. */
917 ptid = thread_from_lwp (ptid);
918 if (GET_PID (ptid) == -1)
919 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
925 thread_db_kill (void)
927 /* There's no need to save & restore inferior_ptid here, since the
928 inferior isn't supposed to survive this function call. */
929 inferior_ptid = lwp_from_thread (inferior_ptid);
930 target_beneath->to_kill ();
934 thread_db_create_inferior (char *exec_file, char *allargs, char **env,
937 unpush_target (&thread_db_ops);
939 target_beneath->to_create_inferior (exec_file, allargs, env, from_tty);
943 thread_db_post_startup_inferior (ptid_t ptid)
945 if (proc_handle.pid == 0)
947 /* The child process is now the actual multi-threaded
948 program. Snatch its process ID... */
949 proc_handle.pid = GET_PID (ptid);
951 /* ...and perform the remaining initialization steps. */
952 enable_thread_event_reporting ();
953 thread_db_find_new_threads ();
958 thread_db_mourn_inferior (void)
960 /* Forget about the child's process ID. We shouldn't need it
964 target_beneath->to_mourn_inferior ();
966 /* Delete the old thread event breakpoints. Do this after mourning
967 the inferior, so that we don't try to uninsert them. */
968 remove_thread_event_breakpoints ();
970 /* Detach thread_db target ops. */
971 unpush_target (&thread_db_ops);
976 find_new_threads_callback (const td_thrhandle_t *th_p, void *data)
982 err = td_thr_get_info_p (th_p, &ti);
984 error (_("find_new_threads_callback: cannot get thread info: %s"),
985 thread_db_err_str (err));
987 if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
988 return 0; /* A zombie -- ignore. */
990 ptid = ptid_build (GET_PID (inferior_ptid), ti.ti_lid, ti.ti_tid);
992 if (!in_thread_list (ptid))
993 attach_thread (ptid, th_p, &ti, 1);
999 thread_db_find_new_threads (void)
1003 /* Iterate over all user-space threads to discover new threads. */
1004 err = td_ta_thr_iter_p (thread_agent, find_new_threads_callback, NULL,
1005 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
1006 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
1008 error (_("Cannot find new threads: %s"), thread_db_err_str (err));
1012 thread_db_pid_to_str (ptid_t ptid)
1014 if (is_thread (ptid))
1016 static char buf[64];
1017 struct thread_info *thread_info;
1019 thread_info = find_thread_pid (ptid);
1020 if (thread_info == NULL)
1021 snprintf (buf, sizeof (buf), "Thread %ld (LWP %ld) (Missing)",
1022 GET_THREAD (ptid), GET_LWP (ptid));
1024 snprintf (buf, sizeof (buf), "Thread %ld (LWP %ld)",
1025 GET_THREAD (ptid), GET_LWP (ptid));
1030 if (target_beneath->to_pid_to_str (ptid))
1031 return target_beneath->to_pid_to_str (ptid);
1033 return normal_pid_to_str (ptid);
1036 /* Return a string describing the state of the thread specified by
1040 thread_db_extra_thread_info (struct thread_info *info)
1042 if (info->private->dying)
1048 /* Get the address of the thread local variable in load module LM which
1049 is stored at OFFSET within the thread local storage for thread PTID. */
1052 thread_db_get_thread_local_address (ptid_t ptid,
1056 if (is_thread (ptid))
1060 struct thread_info *thread_info;
1062 /* glibc doesn't provide the needed interface. */
1063 if (!td_thr_tls_get_addr_p)
1064 throw_error (TLS_NO_LIBRARY_SUPPORT_ERROR,
1065 _("No TLS library support"));
1067 /* Caller should have verified that lm != 0. */
1068 gdb_assert (lm != 0);
1070 /* Get info about the thread. */
1071 thread_info = find_thread_pid (ptid);
1072 thread_db_map_id2thr (thread_info, 1);
1074 /* Finally, get the address of the variable. */
1075 err = td_thr_tls_get_addr_p (&thread_info->private->th,
1076 (void *)(size_t) lm,
1079 #ifdef THREAD_DB_HAS_TD_NOTALLOC
1080 /* The memory hasn't been allocated, yet. */
1081 if (err == TD_NOTALLOC)
1082 /* Now, if libthread_db provided the initialization image's
1083 address, we *could* try to build a non-lvalue value from
1084 the initialization image. */
1085 throw_error (TLS_NOT_ALLOCATED_YET_ERROR,
1086 _("TLS not allocated yet"));
1089 /* Something else went wrong. */
1091 throw_error (TLS_GENERIC_ERROR,
1092 (("%s")), thread_db_err_str (err));
1094 /* Cast assuming host == target. Joy. */
1095 /* Do proper sign extension for the target. */
1096 gdb_assert (exec_bfd);
1097 return (bfd_get_sign_extend_vma (exec_bfd) > 0
1098 ? (CORE_ADDR) (intptr_t) address
1099 : (CORE_ADDR) (uintptr_t) address);
1102 if (target_beneath->to_get_thread_local_address)
1103 return target_beneath->to_get_thread_local_address (ptid, lm, offset);
1105 throw_error (TLS_GENERIC_ERROR,
1106 _("TLS not supported on this target"));
1110 init_thread_db_ops (void)
1112 thread_db_ops.to_shortname = "multi-thread";
1113 thread_db_ops.to_longname = "multi-threaded child process.";
1114 thread_db_ops.to_doc = "Threads and pthreads support.";
1115 thread_db_ops.to_attach = thread_db_attach;
1116 thread_db_ops.to_detach = thread_db_detach;
1117 thread_db_ops.to_resume = thread_db_resume;
1118 thread_db_ops.to_wait = thread_db_wait;
1119 thread_db_ops.to_kill = thread_db_kill;
1120 thread_db_ops.to_create_inferior = thread_db_create_inferior;
1121 thread_db_ops.to_post_startup_inferior = thread_db_post_startup_inferior;
1122 thread_db_ops.to_mourn_inferior = thread_db_mourn_inferior;
1123 thread_db_ops.to_find_new_threads = thread_db_find_new_threads;
1124 thread_db_ops.to_pid_to_str = thread_db_pid_to_str;
1125 thread_db_ops.to_stratum = thread_stratum;
1126 thread_db_ops.to_has_thread_control = tc_schedlock;
1127 thread_db_ops.to_get_thread_local_address
1128 = thread_db_get_thread_local_address;
1129 thread_db_ops.to_extra_thread_info = thread_db_extra_thread_info;
1130 thread_db_ops.to_magic = OPS_MAGIC;
1134 _initialize_thread_db (void)
1136 /* Only initialize the module if we can load libthread_db. */
1137 if (thread_db_load ())
1139 init_thread_db_ops ();
1140 add_target (&thread_db_ops);
1142 /* Add ourselves to objfile event chain. */
1143 target_new_objfile_chain = deprecated_target_new_objfile_hook;
1144 deprecated_target_new_objfile_hook = thread_db_new_objfile;