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 /* A callback function for td_ta_thr_iter, which we use to map all
226 THP is a handle to the current thread; if INFOP is not NULL, the
227 struct thread_info associated with this thread is returned in
230 If the thread is a zombie, TD_THR_ZOMBIE is returned. Otherwise,
231 zero is returned to indicate success. */
234 thread_get_info_callback (const td_thrhandle_t *thp, void *infop)
238 struct thread_info *thread_info;
241 err = td_thr_get_info_p (thp, &ti);
243 error (_("thread_get_info_callback: cannot get thread info: %s"),
244 thread_db_err_str (err));
246 /* Fill the cache. */
247 thread_ptid = ptid_build (GET_PID (inferior_ptid), ti.ti_lid, ti.ti_tid);
248 thread_info = find_thread_pid (thread_ptid);
250 /* In the case of a zombie thread, don't continue. We don't want to
251 attach to it thinking it is a new thread. */
252 if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
255 *(struct thread_info **) infop = thread_info;
256 if (thread_info != NULL)
258 memcpy (&thread_info->private->th, thp, sizeof (*thp));
259 thread_info->private->th_valid = 1;
260 memcpy (&thread_info->private->ti, &ti, sizeof (ti));
261 thread_info->private->ti_valid = 1;
263 return TD_THR_ZOMBIE;
266 if (thread_info == NULL)
268 /* New thread. Attach to it now (why wait?). */
269 attach_thread (thread_ptid, thp, &ti, 1);
270 thread_info = find_thread_pid (thread_ptid);
271 gdb_assert (thread_info != NULL);
274 memcpy (&thread_info->private->th, thp, sizeof (*thp));
275 thread_info->private->th_valid = 1;
276 memcpy (&thread_info->private->ti, &ti, sizeof (ti));
277 thread_info->private->ti_valid = 1;
280 *(struct thread_info **) infop = thread_info;
285 /* Accessor functions for the thread_db information, with caching. */
288 thread_db_map_id2thr (struct thread_info *thread_info, int fatal)
292 if (thread_info->private->th_valid)
295 err = td_ta_map_id2thr_p (thread_agent, GET_THREAD (thread_info->ptid),
296 &thread_info->private->th);
300 error (_("Cannot find thread %ld: %s"),
301 (long) GET_THREAD (thread_info->ptid),
302 thread_db_err_str (err));
305 thread_info->private->th_valid = 1;
308 /* Convert between user-level thread ids and LWP ids. */
311 thread_from_lwp (ptid_t ptid)
315 struct thread_info *thread_info;
318 if (GET_LWP (ptid) == 0)
319 ptid = BUILD_LWP (GET_PID (ptid), GET_PID (ptid));
321 gdb_assert (is_lwp (ptid));
323 err = td_ta_map_lwp2thr_p (thread_agent, GET_LWP (ptid), &th);
325 error (_("Cannot find user-level thread for LWP %ld: %s"),
326 GET_LWP (ptid), thread_db_err_str (err));
330 /* Fetch the thread info. If we get back TD_THR_ZOMBIE, then the
331 event thread has already died. If another gdb interface has called
332 thread_alive() previously, the thread won't be found on the thread list
333 anymore. In that case, we don't want to process this ptid anymore
334 to avoid the possibility of later treating it as a newly
335 discovered thread id that we should add to the list. Thus,
336 we return a -1 ptid which is also how the thread list marks a
338 if (thread_get_info_callback (&th, &thread_info) == TD_THR_ZOMBIE
339 && thread_info == NULL)
340 return pid_to_ptid (-1);
342 gdb_assert (thread_info && thread_info->private->ti_valid);
344 return ptid_build (GET_PID (ptid), GET_LWP (ptid),
345 thread_info->private->ti.ti_tid);
349 lwp_from_thread (ptid_t ptid)
351 return BUILD_LWP (GET_LWP (ptid), GET_PID (ptid));
356 thread_db_init (struct target_ops *target)
358 target_beneath = target;
362 verbose_dlsym (void *handle, const char *name)
364 void *sym = dlsym (handle, name);
366 warning (_("Symbol \"%s\" not found in libthread_db: %s"), name, dlerror ());
371 thread_db_load (void)
376 handle = dlopen (LIBTHREAD_DB_SO, RTLD_NOW);
379 fprintf_filtered (gdb_stderr, "\n\ndlopen failed on '%s' - %s\n",
380 LIBTHREAD_DB_SO, dlerror ());
381 fprintf_filtered (gdb_stderr,
382 "GDB will not be able to debug pthreads.\n\n");
386 /* Initialize pointers to the dynamic library functions we will use.
387 Essential functions first. */
389 td_init_p = verbose_dlsym (handle, "td_init");
390 if (td_init_p == NULL)
393 td_ta_new_p = verbose_dlsym (handle, "td_ta_new");
394 if (td_ta_new_p == NULL)
397 td_ta_map_id2thr_p = verbose_dlsym (handle, "td_ta_map_id2thr");
398 if (td_ta_map_id2thr_p == NULL)
401 td_ta_map_lwp2thr_p = verbose_dlsym (handle, "td_ta_map_lwp2thr");
402 if (td_ta_map_lwp2thr_p == NULL)
405 td_ta_thr_iter_p = verbose_dlsym (handle, "td_ta_thr_iter");
406 if (td_ta_thr_iter_p == NULL)
409 td_thr_validate_p = verbose_dlsym (handle, "td_thr_validate");
410 if (td_thr_validate_p == NULL)
413 td_thr_get_info_p = verbose_dlsym (handle, "td_thr_get_info");
414 if (td_thr_get_info_p == NULL)
417 /* Initialize the library. */
421 warning (_("Cannot initialize libthread_db: %s"), thread_db_err_str (err));
425 /* These are not essential. */
426 td_ta_event_addr_p = dlsym (handle, "td_ta_event_addr");
427 td_ta_set_event_p = dlsym (handle, "td_ta_set_event");
428 td_ta_event_getmsg_p = dlsym (handle, "td_ta_event_getmsg");
429 td_thr_event_enable_p = dlsym (handle, "td_thr_event_enable");
430 td_thr_tls_get_addr_p = dlsym (handle, "td_thr_tls_get_addr");
436 enable_thread_event (td_thragent_t *thread_agent, int event, CORE_ADDR *bp)
441 /* Get the breakpoint address for thread EVENT. */
442 err = td_ta_event_addr_p (thread_agent, event, ¬ify);
446 /* Set up the breakpoint. */
447 gdb_assert (exec_bfd);
448 (*bp) = (gdbarch_convert_from_func_ptr_addr
450 /* Do proper sign extension for the target. */
451 (bfd_get_sign_extend_vma (exec_bfd) > 0
452 ? (CORE_ADDR) (intptr_t) notify.u.bptaddr
453 : (CORE_ADDR) (uintptr_t) notify.u.bptaddr),
455 create_thread_event_breakpoint ((*bp));
461 enable_thread_event_reporting (void)
463 td_thr_events_t events;
466 #ifdef HAVE_GNU_LIBC_VERSION_H
467 const char *libc_version;
468 int libc_major, libc_minor;
471 /* We cannot use the thread event reporting facility if these
472 functions aren't available. */
473 if (td_ta_event_addr_p == NULL || td_ta_set_event_p == NULL
474 || td_ta_event_getmsg_p == NULL || td_thr_event_enable_p == NULL)
477 /* Set the process wide mask saying which events we're interested in. */
478 td_event_emptyset (&events);
479 td_event_addset (&events, TD_CREATE);
481 #ifdef HAVE_GNU_LIBC_VERSION_H
482 /* The event reporting facility is broken for TD_DEATH events in
483 glibc 2.1.3, so don't enable it if we have glibc but a lower
485 libc_version = gnu_get_libc_version ();
486 if (sscanf (libc_version, "%d.%d", &libc_major, &libc_minor) == 2
487 && (libc_major > 2 || (libc_major == 2 && libc_minor > 1)))
489 td_event_addset (&events, TD_DEATH);
491 err = td_ta_set_event_p (thread_agent, &events);
494 warning (_("Unable to set global thread event mask: %s"),
495 thread_db_err_str (err));
499 /* Delete previous thread event breakpoints, if any. */
500 remove_thread_event_breakpoints ();
501 td_create_bp_addr = 0;
502 td_death_bp_addr = 0;
504 /* Set up the thread creation event. */
505 err = enable_thread_event (thread_agent, TD_CREATE, &td_create_bp_addr);
508 warning (_("Unable to get location for thread creation breakpoint: %s"),
509 thread_db_err_str (err));
513 /* Set up the thread death event. */
514 err = enable_thread_event (thread_agent, TD_DEATH, &td_death_bp_addr);
517 warning (_("Unable to get location for thread death breakpoint: %s"),
518 thread_db_err_str (err));
524 disable_thread_event_reporting (void)
526 td_thr_events_t events;
528 /* Set the process wide mask saying we aren't interested in any
530 td_event_emptyset (&events);
531 td_ta_set_event_p (thread_agent, &events);
533 /* Delete thread event breakpoints, if any. */
534 remove_thread_event_breakpoints ();
535 td_create_bp_addr = 0;
536 td_death_bp_addr = 0;
540 check_thread_signals (void)
542 #ifdef GET_THREAD_SIGNALS
548 GET_THREAD_SIGNALS (&mask);
549 sigemptyset (&thread_stop_set);
550 sigemptyset (&thread_print_set);
552 for (i = 1; i < NSIG; i++)
554 if (sigismember (&mask, i))
556 if (signal_stop_update (target_signal_from_host (i), 0))
557 sigaddset (&thread_stop_set, i);
558 if (signal_print_update (target_signal_from_host (i), 0))
559 sigaddset (&thread_print_set, i);
567 /* Check whether thread_db is usable. This function is called when
568 an inferior is created (or otherwise acquired, e.g. attached to)
569 and when new shared libraries are loaded into a running process. */
572 check_for_thread_db (void)
575 static int already_loaded;
577 /* Do nothing if we couldn't load libthread_db.so.1. */
578 if (td_ta_new_p == NULL)
581 /* First time through, report that libthread_db was successfuly
582 loaded. Can't print this in in thread_db_load as, at that stage,
583 the interpreter and it's console haven't started. */
588 const char *library = NULL;
589 if (dladdr ((*td_ta_new_p), &info) != 0)
590 library = info.dli_fname;
595 /* Paranoid - don't let a NULL path slip through. */
596 library = LIBTHREAD_DB_SO;
598 printf_unfiltered (_("Using host libthread_db library \"%s\".\n"),
604 /* Nothing to do. The thread library was already detected and the
605 target vector was already activated. */
608 /* Don't attempt to use thread_db on targets which can not run
609 (executables not running yet, core files) for now. */
610 if (!target_has_execution)
613 /* Don't attempt to use thread_db for remote targets. */
614 if (!target_can_run (¤t_target))
617 /* Initialize the structure that identifies the child process. */
618 proc_handle.pid = GET_PID (inferior_ptid);
620 /* Now attempt to open a connection to the thread library. */
621 err = td_ta_new_p (&proc_handle, &thread_agent);
625 /* No thread library was detected. */
629 printf_unfiltered (_("[Thread debugging using libthread_db enabled]\n"));
631 /* The thread library was detected. Activate the thread_db target. */
632 push_target (&thread_db_ops);
635 enable_thread_event_reporting ();
636 thread_db_find_new_threads ();
640 warning (_("Cannot initialize thread debugging library: %s"),
641 thread_db_err_str (err));
647 thread_db_new_objfile (struct objfile *objfile)
650 check_for_thread_db ();
653 /* Attach to a new thread. This function is called when we receive a
654 TD_CREATE event or when we iterate over all threads and find one
655 that wasn't already in our list. */
658 attach_thread (ptid_t ptid, const td_thrhandle_t *th_p,
659 const td_thrinfo_t *ti_p, int verbose)
661 struct thread_info *tp;
664 /* If we're being called after a TD_CREATE event, we may already
665 know about this thread. There are two ways this can happen. We
666 may have iterated over all threads between the thread creation
667 and the TD_CREATE event, for instance when the user has issued
668 the `info threads' command before the SIGTRAP for hitting the
669 thread creation breakpoint was reported. Alternatively, the
670 thread may have exited and a new one been created with the same
671 thread ID. In the first case we don't need to do anything; in
672 the second case we should discard information about the dead
673 thread and attach to the new one. */
674 if (in_thread_list (ptid))
676 tp = find_thread_pid (ptid);
677 gdb_assert (tp != NULL);
679 if (!tp->private->dying)
682 delete_thread (ptid);
685 check_thread_signals ();
687 if (ti_p->ti_state == TD_THR_UNKNOWN || ti_p->ti_state == TD_THR_ZOMBIE)
688 return; /* A zombie thread -- do not attach. */
690 /* Under GNU/Linux, we have to attach to each and every thread. */
691 if (lin_lwp_attach_lwp (BUILD_LWP (ti_p->ti_lid, GET_PID (ptid)), 0) < 0)
694 /* Add the thread to GDB's thread list. */
695 tp = add_thread (ptid);
696 tp->private = xmalloc (sizeof (struct private_thread_info));
697 memset (tp->private, 0, sizeof (struct private_thread_info));
700 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid));
702 /* Enable thread event reporting for this thread. */
703 err = td_thr_event_enable_p (th_p, 1);
705 error (_("Cannot enable thread event reporting for %s: %s"),
706 target_pid_to_str (ptid), thread_db_err_str (err));
710 thread_db_attach (char *args, int from_tty)
712 target_beneath->to_attach (args, from_tty);
714 /* Destroy thread info; it's no longer valid. */
717 /* The child process is now the actual multi-threaded
718 program. Snatch its process ID... */
719 proc_handle.pid = GET_PID (inferior_ptid);
721 /* ...and perform the remaining initialization steps. */
722 enable_thread_event_reporting ();
723 thread_db_find_new_threads ();
727 detach_thread (ptid_t ptid, int verbose)
729 struct thread_info *thread_info;
732 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (ptid));
734 /* Don't delete the thread now, because it still reports as active
735 until it has executed a few instructions after the event
736 breakpoint - if we deleted it now, "info threads" would cause us
737 to re-attach to it. Just mark it as having had a TD_DEATH
738 event. This means that we won't delete it from our thread list
739 until we notice that it's dead (via prune_threads), or until
740 something re-uses its thread ID. */
741 thread_info = find_thread_pid (ptid);
742 gdb_assert (thread_info != NULL);
743 thread_info->private->dying = 1;
747 thread_db_detach (char *args, int from_tty)
749 disable_thread_event_reporting ();
751 /* There's no need to save & restore inferior_ptid here, since the
752 inferior is supposed to be survive this function call. */
753 inferior_ptid = lwp_from_thread (inferior_ptid);
755 /* Forget about the child's process ID. We shouldn't need it
759 target_beneath->to_detach (args, from_tty);
763 clear_lwpid_callback (struct thread_info *thread, void *dummy)
765 /* If we know that our thread implementation is 1-to-1, we could save
766 a certain amount of information; it's not clear how much, so we
767 are always conservative. */
769 thread->private->th_valid = 0;
770 thread->private->ti_valid = 0;
776 thread_db_resume (ptid_t ptid, int step, enum target_signal signo)
778 struct cleanup *old_chain = save_inferior_ptid ();
780 if (GET_PID (ptid) == -1)
781 inferior_ptid = lwp_from_thread (inferior_ptid);
782 else if (is_thread (ptid))
783 ptid = lwp_from_thread (ptid);
785 /* Clear cached data which may not be valid after the resume. */
786 iterate_over_threads (clear_lwpid_callback, NULL);
788 target_beneath->to_resume (ptid, step, signo);
790 do_cleanups (old_chain);
793 /* Check if PID is currently stopped at the location of a thread event
794 breakpoint location. If it is, read the event message and act upon
798 check_event (ptid_t ptid)
806 /* Bail out early if we're not at a thread event breakpoint. */
807 stop_pc = read_pc_pid (ptid) - gdbarch_decr_pc_after_break (current_gdbarch);
808 if (stop_pc != td_create_bp_addr && stop_pc != td_death_bp_addr)
811 /* If we are at a create breakpoint, we do not know what new lwp
812 was created and cannot specifically locate the event message for it.
813 We have to call td_ta_event_getmsg() to get
814 the latest message. Since we have no way of correlating whether
815 the event message we get back corresponds to our breakpoint, we must
816 loop and read all event messages, processing them appropriately.
817 This guarantees we will process the correct message before continuing
820 Currently, death events are not enabled. If they are enabled,
821 the death event can use the td_thr_event_getmsg() interface to
822 get the message specifically for that lwp and avoid looping
829 err = td_ta_event_getmsg_p (thread_agent, &msg);
835 error (_("Cannot get thread event message: %s"),
836 thread_db_err_str (err));
839 err = td_thr_get_info_p (msg.th_p, &ti);
841 error (_("Cannot get thread info: %s"), thread_db_err_str (err));
843 ptid = ptid_build (GET_PID (ptid), ti.ti_lid, ti.ti_tid);
848 /* Call attach_thread whether or not we already know about a
849 thread with this thread ID. */
850 attach_thread (ptid, msg.th_p, &ti, 1);
856 if (!in_thread_list (ptid))
857 error (_("Spurious thread death event."));
859 detach_thread (ptid, 1);
864 error (_("Spurious thread event."));
871 thread_db_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
873 extern ptid_t trap_ptid;
875 if (GET_PID (ptid) != -1 && is_thread (ptid))
876 ptid = lwp_from_thread (ptid);
878 ptid = target_beneath->to_wait (ptid, ourstatus);
880 if (proc_handle.pid == 0)
881 /* The current child process isn't the actual multi-threaded
882 program yet, so don't try to do any special thread-specific
883 post-processing and bail out early. */
886 if (ourstatus->kind == TARGET_WAITKIND_EXITED
887 || ourstatus->kind == TARGET_WAITKIND_SIGNALLED)
888 return pid_to_ptid (-1);
890 if (ourstatus->kind == TARGET_WAITKIND_EXECD)
892 remove_thread_event_breakpoints ();
893 unpush_target (&thread_db_ops);
896 return pid_to_ptid (GET_PID (ptid));
899 if (ourstatus->kind == TARGET_WAITKIND_STOPPED
900 && ourstatus->value.sig == TARGET_SIGNAL_TRAP)
901 /* Check for a thread event. */
904 if (!ptid_equal (trap_ptid, null_ptid))
905 trap_ptid = thread_from_lwp (trap_ptid);
907 /* Change the ptid back into the higher level PID + TID format.
908 If the thread is dead and no longer on the thread list, we will
909 get back a dead ptid. This can occur if the thread death event
910 gets postponed by other simultaneous events. In such a case,
911 we want to just ignore the event and continue on. */
912 ptid = thread_from_lwp (ptid);
913 if (GET_PID (ptid) == -1)
914 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_create_inferior (char *exec_file, char *allargs, char **env,
932 unpush_target (&thread_db_ops);
934 target_beneath->to_create_inferior (exec_file, allargs, env, from_tty);
938 thread_db_post_startup_inferior (ptid_t ptid)
940 if (proc_handle.pid == 0)
942 /* The child process is now the actual multi-threaded
943 program. Snatch its process ID... */
944 proc_handle.pid = GET_PID (ptid);
946 /* ...and perform the remaining initialization steps. */
947 enable_thread_event_reporting ();
948 thread_db_find_new_threads ();
953 thread_db_mourn_inferior (void)
955 /* Forget about the child's process ID. We shouldn't need it
959 target_beneath->to_mourn_inferior ();
961 /* Delete the old thread event breakpoints. Do this after mourning
962 the inferior, so that we don't try to uninsert them. */
963 remove_thread_event_breakpoints ();
965 /* Detach thread_db target ops. */
966 unpush_target (&thread_db_ops);
971 find_new_threads_callback (const td_thrhandle_t *th_p, void *data)
977 err = td_thr_get_info_p (th_p, &ti);
979 error (_("find_new_threads_callback: cannot get thread info: %s"),
980 thread_db_err_str (err));
982 if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
983 return 0; /* A zombie -- ignore. */
985 ptid = ptid_build (GET_PID (inferior_ptid), ti.ti_lid, ti.ti_tid);
987 if (!in_thread_list (ptid))
988 attach_thread (ptid, th_p, &ti, 1);
994 thread_db_find_new_threads (void)
998 /* Iterate over all user-space threads to discover new threads. */
999 err = td_ta_thr_iter_p (thread_agent, find_new_threads_callback, NULL,
1000 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
1001 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
1003 error (_("Cannot find new threads: %s"), thread_db_err_str (err));
1007 thread_db_pid_to_str (ptid_t ptid)
1009 if (is_thread (ptid))
1011 static char buf[64];
1012 struct thread_info *thread_info;
1014 thread_info = find_thread_pid (ptid);
1015 if (thread_info == NULL)
1016 snprintf (buf, sizeof (buf), "Thread 0x%lx (LWP %ld) (Missing)",
1017 GET_THREAD (ptid), GET_LWP (ptid));
1019 snprintf (buf, sizeof (buf), "Thread 0x%lx (LWP %ld)",
1020 GET_THREAD (ptid), GET_LWP (ptid));
1025 if (target_beneath->to_pid_to_str (ptid))
1026 return target_beneath->to_pid_to_str (ptid);
1028 return normal_pid_to_str (ptid);
1031 /* Return a string describing the state of the thread specified by
1035 thread_db_extra_thread_info (struct thread_info *info)
1037 if (info->private->dying)
1043 /* Get the address of the thread local variable in load module LM which
1044 is stored at OFFSET within the thread local storage for thread PTID. */
1047 thread_db_get_thread_local_address (ptid_t ptid,
1051 if (is_thread (ptid))
1055 struct thread_info *thread_info;
1057 /* glibc doesn't provide the needed interface. */
1058 if (!td_thr_tls_get_addr_p)
1059 throw_error (TLS_NO_LIBRARY_SUPPORT_ERROR,
1060 _("No TLS library support"));
1062 /* Caller should have verified that lm != 0. */
1063 gdb_assert (lm != 0);
1065 /* Get info about the thread. */
1066 thread_info = find_thread_pid (ptid);
1067 thread_db_map_id2thr (thread_info, 1);
1069 /* Finally, get the address of the variable. */
1070 err = td_thr_tls_get_addr_p (&thread_info->private->th,
1071 (void *)(size_t) lm,
1074 #ifdef THREAD_DB_HAS_TD_NOTALLOC
1075 /* The memory hasn't been allocated, yet. */
1076 if (err == TD_NOTALLOC)
1077 /* Now, if libthread_db provided the initialization image's
1078 address, we *could* try to build a non-lvalue value from
1079 the initialization image. */
1080 throw_error (TLS_NOT_ALLOCATED_YET_ERROR,
1081 _("TLS not allocated yet"));
1084 /* Something else went wrong. */
1086 throw_error (TLS_GENERIC_ERROR,
1087 (("%s")), thread_db_err_str (err));
1089 /* Cast assuming host == target. Joy. */
1090 /* Do proper sign extension for the target. */
1091 gdb_assert (exec_bfd);
1092 return (bfd_get_sign_extend_vma (exec_bfd) > 0
1093 ? (CORE_ADDR) (intptr_t) address
1094 : (CORE_ADDR) (uintptr_t) address);
1097 if (target_beneath->to_get_thread_local_address)
1098 return target_beneath->to_get_thread_local_address (ptid, lm, offset);
1100 throw_error (TLS_GENERIC_ERROR,
1101 _("TLS not supported on this target"));
1105 init_thread_db_ops (void)
1107 thread_db_ops.to_shortname = "multi-thread";
1108 thread_db_ops.to_longname = "multi-threaded child process.";
1109 thread_db_ops.to_doc = "Threads and pthreads support.";
1110 thread_db_ops.to_attach = thread_db_attach;
1111 thread_db_ops.to_detach = thread_db_detach;
1112 thread_db_ops.to_resume = thread_db_resume;
1113 thread_db_ops.to_wait = thread_db_wait;
1114 thread_db_ops.to_kill = thread_db_kill;
1115 thread_db_ops.to_create_inferior = thread_db_create_inferior;
1116 thread_db_ops.to_post_startup_inferior = thread_db_post_startup_inferior;
1117 thread_db_ops.to_mourn_inferior = thread_db_mourn_inferior;
1118 thread_db_ops.to_find_new_threads = thread_db_find_new_threads;
1119 thread_db_ops.to_pid_to_str = thread_db_pid_to_str;
1120 thread_db_ops.to_stratum = thread_stratum;
1121 thread_db_ops.to_has_thread_control = tc_schedlock;
1122 thread_db_ops.to_get_thread_local_address
1123 = thread_db_get_thread_local_address;
1124 thread_db_ops.to_extra_thread_info = thread_db_extra_thread_info;
1125 thread_db_ops.to_magic = OPS_MAGIC;
1129 _initialize_thread_db (void)
1131 /* Only initialize the module if we can load libthread_db. */
1132 if (thread_db_load ())
1134 init_thread_db_ops ();
1135 add_target (&thread_db_ops);
1137 /* Add ourselves to objfile event chain. */
1138 observer_attach_new_objfile (thread_db_new_objfile);