1 /* libthread_db assisted debugging support, generic parts.
3 Copyright (C) 1999, 2000, 2001, 2003, 2004, 2005, 2006, 2007, 2008, 2009
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 3 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, see <http://www.gnu.org/licenses/>. */
23 #include "gdb_assert.h"
25 #include "gdb_proc_service.h"
26 #include "gdb_thread_db.h"
30 #include "exceptions.h"
32 #include "gdbthread.h"
39 #include "solib-svr4.h"
42 #include "linux-nat.h"
46 #ifdef HAVE_GNU_LIBC_VERSION_H
47 #include <gnu/libc-version.h>
50 /* GNU/Linux libthread_db support.
52 libthread_db is a library, provided along with libpthread.so, which
53 exposes the internals of the thread library to a debugger. It
54 allows GDB to find existing threads, new threads as they are
55 created, thread IDs (usually, the result of pthread_self), and
56 thread-local variables.
58 The libthread_db interface originates on Solaris, where it is
59 both more powerful and more complicated. This implementation
60 only works for LinuxThreads and NPTL, the two glibc threading
61 libraries. It assumes that each thread is permanently assigned
62 to a single light-weight process (LWP).
64 libthread_db-specific information is stored in the "private" field
65 of struct thread_info. When the field is NULL we do not yet have
66 information about the new thread; this could be temporary (created,
67 but the thread library's data structures do not reflect it yet)
68 or permanent (created using clone instead of pthread_create).
70 Process IDs managed by linux-thread-db.c match those used by
71 linux-nat.c: a common PID for all processes, an LWP ID for each
72 thread, and no TID. We save the TID in private. Keeping it out
73 of the ptid_t prevents thread IDs changing when libpthread is
74 loaded or unloaded. */
76 static char *libthread_db_search_path;
78 /* If we're running on GNU/Linux, we must explicitly attach to any new
81 /* This module's target vector. */
82 static struct target_ops thread_db_ops;
84 /* Non-zero if we have determined the signals used by the threads
86 static int thread_signals;
87 static sigset_t thread_stop_set;
88 static sigset_t thread_print_set;
92 struct thread_db_info *next;
94 /* Process id this object refers to. */
97 /* Handle from dlopen for libthread_db.so. */
100 /* Structure that identifies the child process for the
101 <proc_service.h> interface. */
102 struct ps_prochandle proc_handle;
104 /* Connection to the libthread_db library. */
105 td_thragent_t *thread_agent;
107 /* True if we need to apply the workaround for glibc/BZ5983. When
108 we catch a PTRACE_O_TRACEFORK, and go query the child's thread
109 list, nptl_db returns the parent's threads in addition to the new
110 (single) child thread. If this flag is set, we do extra work to
111 be able to ignore such stale entries. */
112 int need_stale_parent_threads_check;
114 /* Location of the thread creation event breakpoint. The code at
115 this location in the child process will be called by the pthread
116 library whenever a new thread is created. By setting a special
117 breakpoint at this location, GDB can detect when a new thread is
118 created. We obtain this location via the td_ta_event_addr
120 CORE_ADDR td_create_bp_addr;
122 /* Location of the thread death event breakpoint. */
123 CORE_ADDR td_death_bp_addr;
125 /* Pointers to the libthread_db functions. */
127 td_err_e (*td_init_p) (void);
129 td_err_e (*td_ta_new_p) (struct ps_prochandle * ps,
131 td_err_e (*td_ta_map_id2thr_p) (const td_thragent_t *ta, thread_t pt,
132 td_thrhandle_t *__th);
133 td_err_e (*td_ta_map_lwp2thr_p) (const td_thragent_t *ta,
134 lwpid_t lwpid, td_thrhandle_t *th);
135 td_err_e (*td_ta_thr_iter_p) (const td_thragent_t *ta,
136 td_thr_iter_f *callback, void *cbdata_p,
137 td_thr_state_e state, int ti_pri,
138 sigset_t *ti_sigmask_p,
139 unsigned int ti_user_flags);
140 td_err_e (*td_ta_event_addr_p) (const td_thragent_t *ta,
141 td_event_e event, td_notify_t *ptr);
142 td_err_e (*td_ta_set_event_p) (const td_thragent_t *ta,
143 td_thr_events_t *event);
144 td_err_e (*td_ta_clear_event_p) (const td_thragent_t *ta,
145 td_thr_events_t *event);
146 td_err_e (*td_ta_event_getmsg_p) (const td_thragent_t *ta,
147 td_event_msg_t *msg);
149 td_err_e (*td_thr_validate_p) (const td_thrhandle_t *th);
150 td_err_e (*td_thr_get_info_p) (const td_thrhandle_t *th,
151 td_thrinfo_t *infop);
152 td_err_e (*td_thr_event_enable_p) (const td_thrhandle_t *th,
155 td_err_e (*td_thr_tls_get_addr_p) (const td_thrhandle_t *th,
156 psaddr_t map_address,
157 size_t offset, psaddr_t *address);
160 /* List of known processes using thread_db, and the required
162 struct thread_db_info *thread_db_list;
164 static void thread_db_find_new_threads_1 (ptid_t ptid);
165 static void thread_db_find_new_threads_2 (ptid_t ptid, int until_no_new);
167 /* Add the current inferior to the list of processes using libpthread.
168 Return a pointer to the newly allocated object that was added to
169 THREAD_DB_LIST. HANDLE is the handle returned by dlopen'ing
172 static struct thread_db_info *
173 add_thread_db_info (void *handle)
176 struct thread_db_info *info;
178 info = xcalloc (1, sizeof (*info));
179 info->pid = ptid_get_pid (inferior_ptid);
180 info->handle = handle;
181 info->need_stale_parent_threads_check = 1;
183 info->next = thread_db_list;
184 thread_db_list = info;
189 /* Return the thread_db_info object representing the bookkeeping
190 related to process PID, if any; NULL otherwise. */
192 static struct thread_db_info *
193 get_thread_db_info (int pid)
195 struct thread_db_info *info;
197 for (info = thread_db_list; info; info = info->next)
198 if (pid == info->pid)
204 /* When PID has exited or has been detached, we no longer want to keep
205 track of it as using libpthread. Call this function to discard
206 thread_db related info related to PID. Note that this closes
207 LIBTHREAD_DB_SO's dlopen'ed handle. */
210 delete_thread_db_info (int pid)
212 struct thread_db_info *info, *info_prev;
216 for (info = thread_db_list; info; info_prev = info, info = info->next)
217 if (pid == info->pid)
223 if (info->handle != NULL)
224 dlclose (info->handle);
227 info_prev->next = info->next;
229 thread_db_list = info->next;
234 /* Prototypes for local functions. */
235 static int attach_thread (ptid_t ptid, const td_thrhandle_t *th_p,
236 const td_thrinfo_t *ti_p);
237 static void detach_thread (ptid_t ptid);
240 /* Use "struct private_thread_info" to cache thread state. This is
241 a substantial optimization. */
243 struct private_thread_info
245 /* Flag set when we see a TD_DEATH event for this thread. */
246 unsigned int dying:1;
248 /* Cached thread state. */
255 thread_db_err_str (td_err_e err)
262 return "generic 'call succeeded'";
264 return "generic error";
266 return "no thread to satisfy query";
268 return "no sync handle to satisfy query";
270 return "no LWP to satisfy query";
272 return "invalid process handle";
274 return "invalid thread handle";
276 return "invalid synchronization handle";
278 return "invalid thread agent";
280 return "invalid key";
282 return "no event message for getmsg";
284 return "FPU register set not available";
286 return "application not linked with libthread";
288 return "requested event is not supported";
290 return "capability not available";
292 return "debugger service failed";
294 return "operation not applicable to";
296 return "no thread-specific data for this thread";
298 return "malloc failed";
300 return "only part of register set was written/read";
302 return "X register set not available for this thread";
303 #ifdef THREAD_DB_HAS_TD_NOTALLOC
305 return "thread has not yet allocated TLS for given module";
307 #ifdef THREAD_DB_HAS_TD_VERSION
309 return "versions of libpthread and libthread_db do not match";
311 #ifdef THREAD_DB_HAS_TD_NOTLS
313 return "there is no TLS segment in the given module";
316 snprintf (buf, sizeof (buf), "unknown thread_db error '%d'", err);
321 /* Return 1 if any threads have been registered. There may be none if
322 the threading library is not fully initialized yet. */
325 have_threads_callback (struct thread_info *thread, void *args)
327 int pid = * (int *) args;
328 if (ptid_get_pid (thread->ptid) != pid)
331 return thread->private != NULL;
335 have_threads (ptid_t ptid)
337 int pid = ptid_get_pid (ptid);
339 return iterate_over_threads (have_threads_callback, &pid) != NULL;
342 struct thread_get_info_inout
344 struct thread_info *thread_info;
345 struct thread_db_info *thread_db_info;
348 /* A callback function for td_ta_thr_iter, which we use to map all
351 THP is a handle to the current thread; if INFOP is not NULL, the
352 struct thread_info associated with this thread is returned in
355 If the thread is a zombie, TD_THR_ZOMBIE is returned. Otherwise,
356 zero is returned to indicate success. */
359 thread_get_info_callback (const td_thrhandle_t *thp, void *argp)
364 struct thread_get_info_inout *inout;
365 struct thread_db_info *info;
368 info = inout->thread_db_info;
370 err = info->td_thr_get_info_p (thp, &ti);
372 error (_("thread_get_info_callback: cannot get thread info: %s"),
373 thread_db_err_str (err));
375 /* Fill the cache. */
376 thread_ptid = ptid_build (info->pid, ti.ti_lid, 0);
377 inout->thread_info = find_thread_ptid (thread_ptid);
379 /* In the case of a zombie thread, don't continue. We don't want to
380 attach to it thinking it is a new thread. */
381 if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
382 return TD_THR_ZOMBIE;
384 if (inout->thread_info == NULL)
386 /* New thread. Attach to it now (why wait?). */
387 if (!have_threads (thread_ptid))
388 thread_db_find_new_threads_1 (thread_ptid);
390 attach_thread (thread_ptid, thp, &ti);
391 inout->thread_info = find_thread_ptid (thread_ptid);
392 gdb_assert (inout->thread_info != NULL);
398 /* Convert between user-level thread ids and LWP ids. */
401 thread_from_lwp (ptid_t ptid)
406 struct thread_db_info *info;
407 struct thread_get_info_inout io = {0};
409 /* This ptid comes from linux-nat.c, which should always fill in the
411 gdb_assert (GET_LWP (ptid) != 0);
413 info = get_thread_db_info (GET_PID (ptid));
415 /* Access an lwp we know is stopped. */
416 info->proc_handle.ptid = ptid;
417 err = info->td_ta_map_lwp2thr_p (info->thread_agent, GET_LWP (ptid), &th);
419 error (_("Cannot find user-level thread for LWP %ld: %s"),
420 GET_LWP (ptid), thread_db_err_str (err));
422 /* Fetch the thread info. If we get back TD_THR_ZOMBIE, then the
423 event thread has already died. If another gdb interface has called
424 thread_alive() previously, the thread won't be found on the thread list
425 anymore. In that case, we don't want to process this ptid anymore
426 to avoid the possibility of later treating it as a newly
427 discovered thread id that we should add to the list. Thus,
428 we return a -1 ptid which is also how the thread list marks a
430 io.thread_db_info = info;
431 io.thread_info = NULL;
432 if (thread_get_info_callback (&th, &io) == TD_THR_ZOMBIE
433 && io.thread_info == NULL)
434 return minus_one_ptid;
436 gdb_assert (ptid_get_tid (ptid) == 0);
441 /* Attach to lwp PTID, doing whatever else is required to have this
442 LWP under the debugger's control --- e.g., enabling event
443 reporting. Returns true on success. */
445 thread_db_attach_lwp (ptid_t ptid)
450 struct thread_db_info *info;
452 info = get_thread_db_info (GET_PID (ptid));
457 /* This ptid comes from linux-nat.c, which should always fill in the
459 gdb_assert (GET_LWP (ptid) != 0);
461 /* Access an lwp we know is stopped. */
462 info->proc_handle.ptid = ptid;
464 /* If we have only looked at the first thread before libpthread was
465 initialized, we may not know its thread ID yet. Make sure we do
466 before we add another thread to the list. */
467 if (!have_threads (ptid))
468 thread_db_find_new_threads_1 (ptid);
470 err = info->td_ta_map_lwp2thr_p (info->thread_agent, GET_LWP (ptid), &th);
472 /* Cannot find user-level thread. */
475 err = info->td_thr_get_info_p (&th, &ti);
478 warning (_("Cannot get thread info: %s"), thread_db_err_str (err));
482 attach_thread (ptid, &th, &ti);
487 verbose_dlsym (void *handle, const char *name)
489 void *sym = dlsym (handle, name);
491 warning (_("Symbol \"%s\" not found in libthread_db: %s"), name, dlerror ());
496 enable_thread_event (int event, CORE_ADDR *bp)
500 struct thread_db_info *info;
502 info = get_thread_db_info (GET_PID (inferior_ptid));
504 /* Access an lwp we know is stopped. */
505 info->proc_handle.ptid = inferior_ptid;
507 /* Get the breakpoint address for thread EVENT. */
508 err = info->td_ta_event_addr_p (info->thread_agent, event, ¬ify);
512 /* Set up the breakpoint. */
513 gdb_assert (exec_bfd);
514 (*bp) = (gdbarch_convert_from_func_ptr_addr
516 /* Do proper sign extension for the target. */
517 (bfd_get_sign_extend_vma (exec_bfd) > 0
518 ? (CORE_ADDR) (intptr_t) notify.u.bptaddr
519 : (CORE_ADDR) (uintptr_t) notify.u.bptaddr),
521 create_thread_event_breakpoint (target_gdbarch, *bp);
527 enable_thread_event_reporting (void)
529 td_thr_events_t events;
532 #ifdef HAVE_GNU_LIBC_VERSION_H
533 const char *libc_version;
534 int libc_major, libc_minor;
536 struct thread_db_info *info;
538 info = get_thread_db_info (GET_PID (inferior_ptid));
540 /* We cannot use the thread event reporting facility if these
541 functions aren't available. */
542 if (info->td_ta_event_addr_p == NULL
543 || info->td_ta_set_event_p == NULL
544 || info->td_ta_event_getmsg_p == NULL
545 || info->td_thr_event_enable_p == NULL)
548 /* Set the process wide mask saying which events we're interested in. */
549 td_event_emptyset (&events);
550 td_event_addset (&events, TD_CREATE);
552 #ifdef HAVE_GNU_LIBC_VERSION_H
553 /* The event reporting facility is broken for TD_DEATH events in
554 glibc 2.1.3, so don't enable it if we have glibc but a lower
556 libc_version = gnu_get_libc_version ();
557 if (sscanf (libc_version, "%d.%d", &libc_major, &libc_minor) == 2
558 && (libc_major > 2 || (libc_major == 2 && libc_minor > 1)))
560 td_event_addset (&events, TD_DEATH);
562 err = info->td_ta_set_event_p (info->thread_agent, &events);
565 warning (_("Unable to set global thread event mask: %s"),
566 thread_db_err_str (err));
570 /* Delete previous thread event breakpoints, if any. */
571 remove_thread_event_breakpoints ();
572 info->td_create_bp_addr = 0;
573 info->td_death_bp_addr = 0;
575 /* Set up the thread creation event. */
576 err = enable_thread_event (TD_CREATE, &info->td_create_bp_addr);
579 warning (_("Unable to get location for thread creation breakpoint: %s"),
580 thread_db_err_str (err));
584 /* Set up the thread death event. */
585 err = enable_thread_event (TD_DEATH, &info->td_death_bp_addr);
588 warning (_("Unable to get location for thread death breakpoint: %s"),
589 thread_db_err_str (err));
594 /* Same as thread_db_find_new_threads_1, but silently ignore errors. */
597 thread_db_find_new_threads_silently (ptid_t ptid)
599 volatile struct gdb_exception except;
601 TRY_CATCH (except, RETURN_MASK_ERROR)
603 thread_db_find_new_threads_2 (ptid, 1);
606 if (except.reason < 0 && info_verbose)
608 exception_fprintf (gdb_stderr, except,
609 "Warning: thread_db_find_new_threads_silently: ");
613 /* Lookup a library in which given symbol resides.
614 Note: this is looking in GDB process, not in the inferior.
615 Returns library name, or NULL. */
618 dladdr_to_soname (const void *addr)
622 if (dladdr (addr, &info) != 0)
623 return info.dli_fname;
627 /* Attempt to initialize dlopen()ed libthread_db, described by HANDLE.
629 Failure could happen if libthread_db does not have symbols we expect,
630 or when it refuses to work with the current inferior (e.g. due to
631 version mismatch between libthread_db and libpthread). */
634 try_thread_db_load_1 (struct thread_db_info *info)
638 /* Initialize pointers to the dynamic library functions we will use.
639 Essential functions first. */
641 info->td_init_p = verbose_dlsym (info->handle, "td_init");
642 if (info->td_init_p == NULL)
645 err = info->td_init_p ();
648 warning (_("Cannot initialize libthread_db: %s"), thread_db_err_str (err));
652 info->td_ta_new_p = verbose_dlsym (info->handle, "td_ta_new");
653 if (info->td_ta_new_p == NULL)
656 /* Initialize the structure that identifies the child process. */
657 info->proc_handle.ptid = inferior_ptid;
659 /* Now attempt to open a connection to the thread library. */
660 err = info->td_ta_new_p (&info->proc_handle, &info->thread_agent);
664 printf_unfiltered (_("td_ta_new failed: %s\n"),
665 thread_db_err_str (err));
670 #ifdef THREAD_DB_HAS_TD_VERSION
673 /* The errors above are not unexpected and silently ignored:
674 they just mean we haven't found correct version of
678 warning (_("td_ta_new failed: %s"), thread_db_err_str (err));
683 info->td_ta_map_id2thr_p = verbose_dlsym (info->handle, "td_ta_map_id2thr");
684 if (info->td_ta_map_id2thr_p == NULL)
687 info->td_ta_map_lwp2thr_p = verbose_dlsym (info->handle, "td_ta_map_lwp2thr");
688 if (info->td_ta_map_lwp2thr_p == NULL)
691 info->td_ta_thr_iter_p = verbose_dlsym (info->handle, "td_ta_thr_iter");
692 if (info->td_ta_thr_iter_p == NULL)
695 info->td_thr_validate_p = verbose_dlsym (info->handle, "td_thr_validate");
696 if (info->td_thr_validate_p == NULL)
699 info->td_thr_get_info_p = verbose_dlsym (info->handle, "td_thr_get_info");
700 if (info->td_thr_get_info_p == NULL)
703 /* These are not essential. */
704 info->td_ta_event_addr_p = dlsym (info->handle, "td_ta_event_addr");
705 info->td_ta_set_event_p = dlsym (info->handle, "td_ta_set_event");
706 info->td_ta_clear_event_p = dlsym (info->handle, "td_ta_clear_event");
707 info->td_ta_event_getmsg_p = dlsym (info->handle, "td_ta_event_getmsg");
708 info->td_thr_event_enable_p = dlsym (info->handle, "td_thr_event_enable");
709 info->td_thr_tls_get_addr_p = dlsym (info->handle, "td_thr_tls_get_addr");
711 printf_unfiltered (_("[Thread debugging using libthread_db enabled]\n"));
713 if (info_verbose || *libthread_db_search_path)
717 library = dladdr_to_soname (*info->td_ta_new_p);
719 library = LIBTHREAD_DB_SO;
721 printf_unfiltered (_("Using host libthread_db library \"%s\".\n"),
725 /* The thread library was detected. Activate the thread_db target
726 if this is the first process using it. */
727 if (thread_db_list->next == NULL)
728 push_target (&thread_db_ops);
730 enable_thread_event_reporting ();
732 /* There appears to be a bug in glibc-2.3.6: calls to td_thr_get_info fail
733 with TD_ERR for statically linked executables if td_thr_get_info is
734 called before glibc has initialized itself. Silently ignore such
735 errors, and let gdb enumerate threads again later. */
736 thread_db_find_new_threads_silently (inferior_ptid);
741 /* Attempt to use LIBRARY as libthread_db. LIBRARY could be absolute,
742 relative, or just LIBTHREAD_DB. */
745 try_thread_db_load (const char *library)
748 struct thread_db_info *info;
751 printf_unfiltered (_("Trying host libthread_db library: %s.\n"),
753 handle = dlopen (library, RTLD_NOW);
757 printf_unfiltered (_("dlopen failed: %s.\n"), dlerror ());
761 if (info_verbose && strchr (library, '/') == NULL)
765 td_init = dlsym (handle, "td_init");
768 const char *const libpath = dladdr_to_soname (td_init);
771 printf_unfiltered (_("Host %s resolved to: %s.\n"),
776 info = add_thread_db_info (handle);
778 if (try_thread_db_load_1 (info))
781 /* This library "refused" to work on current inferior. */
782 delete_thread_db_info (GET_PID (inferior_ptid));
787 /* Search libthread_db_search_path for libthread_db which "agrees"
788 to work on current inferior. */
791 thread_db_load_search (void)
794 const char *search_path = libthread_db_search_path;
799 const char *end = strchr (search_path, ':');
802 size_t len = end - search_path;
803 if (len + 1 + strlen (LIBTHREAD_DB_SO) + 1 > sizeof (path))
805 char *cp = xmalloc (len + 1);
806 memcpy (cp, search_path, len);
808 warning (_("libthread_db_search_path component too long,"
809 " ignored: %s."), cp);
811 search_path += len + 1;
814 memcpy (path, search_path, len);
816 search_path += len + 1;
820 size_t len = strlen (search_path);
822 if (len + 1 + strlen (LIBTHREAD_DB_SO) + 1 > sizeof (path))
824 warning (_("libthread_db_search_path component too long,"
825 " ignored: %s."), search_path);
828 memcpy (path, search_path, len + 1);
832 strcat (path, LIBTHREAD_DB_SO);
833 if (try_thread_db_load (path))
840 rc = try_thread_db_load (LIBTHREAD_DB_SO);
844 /* Attempt to load and initialize libthread_db.
849 thread_db_load (void)
852 struct thread_db_info *info;
854 info = get_thread_db_info (GET_PID (inferior_ptid));
859 /* Don't attempt to use thread_db on targets which can not run
860 (executables not running yet, core files) for now. */
861 if (!target_has_execution)
864 /* Don't attempt to use thread_db for remote targets. */
865 if (!target_can_run (¤t_target))
868 if (thread_db_load_search ())
871 /* None of the libthread_db's on our search path, not the system default
872 ones worked. If the executable is dynamically linked against
873 libpthread, try loading libthread_db from the same directory. */
876 if (libpthread_name_p (obj->name))
878 char path[PATH_MAX], *cp;
880 gdb_assert (strlen (obj->name) < sizeof (path));
881 strcpy (path, obj->name);
882 cp = strrchr (path, '/');
886 warning (_("Expected absolute pathname for libpthread in the"
887 " inferior, but got %s."), path);
889 else if (cp + 1 + strlen (LIBTHREAD_DB_SO) + 1 > path + sizeof (path))
891 warning (_("Unexpected: path to libpthread in the inferior is"
892 " too long: %s"), path);
896 strcpy (cp + 1, LIBTHREAD_DB_SO);
897 if (try_thread_db_load (path))
900 warning (_("Unable to find libthread_db matching inferior's thread"
901 " library, thread debugging will not be available."));
904 /* Either this executable isn't using libpthread at all, or it is
905 statically linked. Since we can't easily distinguish these two cases,
906 no warning is issued. */
911 disable_thread_event_reporting (struct thread_db_info *info)
913 if (info->td_ta_clear_event_p != NULL)
915 td_thr_events_t events;
917 /* Set the process wide mask saying we aren't interested in any
919 td_event_fillset (&events);
920 info->td_ta_clear_event_p (info->thread_agent, &events);
923 info->td_create_bp_addr = 0;
924 info->td_death_bp_addr = 0;
928 check_thread_signals (void)
930 #ifdef GET_THREAD_SIGNALS
936 GET_THREAD_SIGNALS (&mask);
937 sigemptyset (&thread_stop_set);
938 sigemptyset (&thread_print_set);
940 for (i = 1; i < NSIG; i++)
942 if (sigismember (&mask, i))
944 if (signal_stop_update (target_signal_from_host (i), 0))
945 sigaddset (&thread_stop_set, i);
946 if (signal_print_update (target_signal_from_host (i), 0))
947 sigaddset (&thread_print_set, i);
955 /* Check whether thread_db is usable. This function is called when
956 an inferior is created (or otherwise acquired, e.g. attached to)
957 and when new shared libraries are loaded into a running process. */
960 check_for_thread_db (void)
963 static void *last_loaded;
965 /* Do nothing if we couldn't load libthread_db.so.1. */
966 if (!thread_db_load ())
971 thread_db_new_objfile (struct objfile *objfile)
973 /* This observer must always be called with inferior_ptid set
977 check_for_thread_db ();
980 /* Attach to a new thread. This function is called when we receive a
981 TD_CREATE event or when we iterate over all threads and find one
982 that wasn't already in our list. Returns true on success. */
985 attach_thread (ptid_t ptid, const td_thrhandle_t *th_p,
986 const td_thrinfo_t *ti_p)
988 struct private_thread_info *private;
989 struct thread_info *tp = NULL;
991 struct thread_db_info *info;
993 /* If we're being called after a TD_CREATE event, we may already
994 know about this thread. There are two ways this can happen. We
995 may have iterated over all threads between the thread creation
996 and the TD_CREATE event, for instance when the user has issued
997 the `info threads' command before the SIGTRAP for hitting the
998 thread creation breakpoint was reported. Alternatively, the
999 thread may have exited and a new one been created with the same
1000 thread ID. In the first case we don't need to do anything; in
1001 the second case we should discard information about the dead
1002 thread and attach to the new one. */
1003 if (in_thread_list (ptid))
1005 tp = find_thread_ptid (ptid);
1006 gdb_assert (tp != NULL);
1008 /* If tp->private is NULL, then GDB is already attached to this
1009 thread, but we do not know anything about it. We can learn
1010 about it here. This can only happen if we have some other
1011 way besides libthread_db to notice new threads (i.e.
1012 PTRACE_EVENT_CLONE); assume the same mechanism notices thread
1013 exit, so this can not be a stale thread recreated with the
1015 if (tp->private != NULL)
1017 if (!tp->private->dying)
1020 delete_thread (ptid);
1025 check_thread_signals ();
1027 if (ti_p->ti_state == TD_THR_UNKNOWN || ti_p->ti_state == TD_THR_ZOMBIE)
1028 return 0; /* A zombie thread -- do not attach. */
1030 /* Under GNU/Linux, we have to attach to each and every thread. */
1032 && lin_lwp_attach_lwp (BUILD_LWP (ti_p->ti_lid, GET_PID (ptid))) < 0)
1035 /* Construct the thread's private data. */
1036 private = xmalloc (sizeof (struct private_thread_info));
1037 memset (private, 0, sizeof (struct private_thread_info));
1039 /* A thread ID of zero may mean the thread library has not initialized
1040 yet. But we shouldn't even get here if that's the case. FIXME:
1041 if we change GDB to always have at least one thread in the thread
1042 list this will have to go somewhere else; maybe private == NULL
1043 until the thread_db target claims it. */
1044 gdb_assert (ti_p->ti_tid != 0);
1045 private->th = *th_p;
1046 private->tid = ti_p->ti_tid;
1048 /* Add the thread to GDB's thread list. */
1050 tp = add_thread_with_info (ptid, private);
1052 tp->private = private;
1054 info = get_thread_db_info (GET_PID (ptid));
1056 /* Enable thread event reporting for this thread. */
1057 err = info->td_thr_event_enable_p (th_p, 1);
1059 error (_("Cannot enable thread event reporting for %s: %s"),
1060 target_pid_to_str (ptid), thread_db_err_str (err));
1065 detach_thread (ptid_t ptid)
1067 struct thread_info *thread_info;
1069 /* Don't delete the thread now, because it still reports as active
1070 until it has executed a few instructions after the event
1071 breakpoint - if we deleted it now, "info threads" would cause us
1072 to re-attach to it. Just mark it as having had a TD_DEATH
1073 event. This means that we won't delete it from our thread list
1074 until we notice that it's dead (via prune_threads), or until
1075 something re-uses its thread ID. We'll report the thread exit
1076 when the underlying LWP dies. */
1077 thread_info = find_thread_ptid (ptid);
1078 gdb_assert (thread_info != NULL && thread_info->private != NULL);
1079 thread_info->private->dying = 1;
1083 thread_db_detach (struct target_ops *ops, char *args, int from_tty)
1085 struct target_ops *target_beneath = find_target_beneath (ops);
1086 struct thread_db_info *info;
1088 info = get_thread_db_info (GET_PID (inferior_ptid));
1092 disable_thread_event_reporting (info);
1094 /* Delete the old thread event breakpoints. Note that unlike
1095 when mourning, we can remove them here because there's still
1096 a live inferior to poke at. In any case, GDB will not try to
1097 insert anything in the inferior when removing a
1099 remove_thread_event_breakpoints ();
1101 delete_thread_db_info (GET_PID (inferior_ptid));
1104 target_beneath->to_detach (target_beneath, args, from_tty);
1106 /* NOTE: From this point on, inferior_ptid is null_ptid. */
1108 /* If there are no more processes using libpthread, detach the
1109 thread_db target ops. */
1110 if (!thread_db_list)
1111 unpush_target (&thread_db_ops);
1114 /* Check if PID is currently stopped at the location of a thread event
1115 breakpoint location. If it is, read the event message and act upon
1119 check_event (ptid_t ptid)
1121 struct regcache *regcache = get_thread_regcache (ptid);
1122 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1128 struct thread_db_info *info;
1130 info = get_thread_db_info (GET_PID (ptid));
1132 /* Bail out early if we're not at a thread event breakpoint. */
1133 stop_pc = regcache_read_pc (regcache)
1134 - gdbarch_decr_pc_after_break (gdbarch);
1135 if (stop_pc != info->td_create_bp_addr
1136 && stop_pc != info->td_death_bp_addr)
1139 /* Access an lwp we know is stopped. */
1140 info->proc_handle.ptid = ptid;
1142 /* If we have only looked at the first thread before libpthread was
1143 initialized, we may not know its thread ID yet. Make sure we do
1144 before we add another thread to the list. */
1145 if (!have_threads (ptid))
1146 thread_db_find_new_threads_1 (ptid);
1148 /* If we are at a create breakpoint, we do not know what new lwp
1149 was created and cannot specifically locate the event message for it.
1150 We have to call td_ta_event_getmsg() to get
1151 the latest message. Since we have no way of correlating whether
1152 the event message we get back corresponds to our breakpoint, we must
1153 loop and read all event messages, processing them appropriately.
1154 This guarantees we will process the correct message before continuing
1155 from the breakpoint.
1157 Currently, death events are not enabled. If they are enabled,
1158 the death event can use the td_thr_event_getmsg() interface to
1159 get the message specifically for that lwp and avoid looping
1166 err = info->td_ta_event_getmsg_p (info->thread_agent, &msg);
1169 if (err == TD_NOMSG)
1172 error (_("Cannot get thread event message: %s"),
1173 thread_db_err_str (err));
1176 err = info->td_thr_get_info_p (msg.th_p, &ti);
1178 error (_("Cannot get thread info: %s"), thread_db_err_str (err));
1180 ptid = ptid_build (GET_PID (ptid), ti.ti_lid, 0);
1185 /* Call attach_thread whether or not we already know about a
1186 thread with this thread ID. */
1187 attach_thread (ptid, msg.th_p, &ti);
1193 if (!in_thread_list (ptid))
1194 error (_("Spurious thread death event."));
1196 detach_thread (ptid);
1201 error (_("Spurious thread event."));
1208 thread_db_wait (struct target_ops *ops,
1209 ptid_t ptid, struct target_waitstatus *ourstatus,
1212 struct thread_db_info *info;
1213 struct target_ops *beneath = find_target_beneath (ops);
1215 ptid = beneath->to_wait (beneath, ptid, ourstatus, options);
1217 if (ourstatus->kind == TARGET_WAITKIND_IGNORE)
1220 if (ourstatus->kind == TARGET_WAITKIND_EXITED
1221 || ourstatus->kind == TARGET_WAITKIND_SIGNALLED)
1224 info = get_thread_db_info (GET_PID (ptid));
1226 /* If this process isn't using thread_db, we're done. */
1230 if (ourstatus->kind == TARGET_WAITKIND_EXECD)
1232 /* New image, it may or may not end up using thread_db. Assume
1233 not unless we find otherwise. */
1234 delete_thread_db_info (GET_PID (ptid));
1235 if (!thread_db_list)
1236 unpush_target (&thread_db_ops);
1238 /* Thread event breakpoints are deleted by
1239 update_breakpoints_after_exec. */
1244 /* If we do not know about the main thread yet, this would be a good time to
1246 if (ourstatus->kind == TARGET_WAITKIND_STOPPED && !have_threads (ptid))
1247 thread_db_find_new_threads_1 (ptid);
1249 if (ourstatus->kind == TARGET_WAITKIND_STOPPED
1250 && ourstatus->value.sig == TARGET_SIGNAL_TRAP)
1251 /* Check for a thread event. */
1254 if (have_threads (ptid))
1256 /* Change ptids back into the higher level PID + TID format. If
1257 the thread is dead and no longer on the thread list, we will
1258 get back a dead ptid. This can occur if the thread death
1259 event gets postponed by other simultaneous events. In such a
1260 case, we want to just ignore the event and continue on. */
1262 ptid = thread_from_lwp (ptid);
1263 if (GET_PID (ptid) == -1)
1264 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
1271 thread_db_mourn_inferior (struct target_ops *ops)
1273 struct target_ops *target_beneath = find_target_beneath (ops);
1275 delete_thread_db_info (GET_PID (inferior_ptid));
1277 target_beneath->to_mourn_inferior (target_beneath);
1279 /* Delete the old thread event breakpoints. Do this after mourning
1280 the inferior, so that we don't try to uninsert them. */
1281 remove_thread_event_breakpoints ();
1283 /* Detach thread_db target ops. */
1284 if (!thread_db_list)
1285 unpush_target (ops);
1288 struct callback_data
1290 struct thread_db_info *info;
1295 find_new_threads_callback (const td_thrhandle_t *th_p, void *data)
1300 struct thread_info *tp;
1301 struct callback_data *cb_data = data;
1302 struct thread_db_info *info = cb_data->info;
1304 err = info->td_thr_get_info_p (th_p, &ti);
1306 error (_("find_new_threads_callback: cannot get thread info: %s"),
1307 thread_db_err_str (err));
1309 if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
1310 return 0; /* A zombie -- ignore. */
1314 /* A thread ID of zero means that this is the main thread, but
1315 glibc has not yet initialized thread-local storage and the
1316 pthread library. We do not know what the thread's TID will
1317 be yet. Just enable event reporting and otherwise ignore
1320 /* In that case, we're not stopped in a fork syscall and don't
1321 need this glibc bug workaround. */
1322 info->need_stale_parent_threads_check = 0;
1324 err = info->td_thr_event_enable_p (th_p, 1);
1326 error (_("Cannot enable thread event reporting for LWP %d: %s"),
1327 (int) ti.ti_lid, thread_db_err_str (err));
1332 /* Ignore stale parent threads, caused by glibc/BZ5983. This is a
1333 bit expensive, as it needs to open /proc/pid/status, so try to
1334 avoid doing the work if we know we don't have to. */
1335 if (info->need_stale_parent_threads_check)
1337 int tgid = linux_proc_get_tgid (ti.ti_lid);
1338 if (tgid != -1 && tgid != info->pid)
1342 ptid = ptid_build (info->pid, ti.ti_lid, 0);
1343 tp = find_thread_ptid (ptid);
1344 if (tp == NULL || tp->private == NULL)
1346 if (attach_thread (ptid, th_p, &ti))
1347 cb_data->new_threads += 1;
1349 /* Problem attaching this thread; perhaps it exited before we
1351 This could mean that the thread list inside glibc itself is in
1352 inconsistent state, and libthread_db could go on looping forever
1353 (observed with glibc-2.3.6). To prevent that, terminate
1354 iteration: thread_db_find_new_threads_2 will retry. */
1361 /* Helper for thread_db_find_new_threads_2.
1362 Returns number of new threads found. */
1365 find_new_threads_once (struct thread_db_info *info, int iteration,
1368 volatile struct gdb_exception except;
1369 struct callback_data data;
1370 td_err_e err = TD_ERR;
1373 data.new_threads = 0;
1375 TRY_CATCH (except, RETURN_MASK_ERROR)
1377 /* Iterate over all user-space threads to discover new threads. */
1378 err = info->td_ta_thr_iter_p (info->thread_agent,
1379 find_new_threads_callback,
1382 TD_THR_LOWEST_PRIORITY,
1384 TD_THR_ANY_USER_FLAGS);
1389 if (except.reason < 0)
1390 exception_fprintf (gdb_stderr, except,
1391 "Warning: find_new_threads_once: ");
1393 printf_filtered (_("Found %d new threads in iteration %d.\n"),
1394 data.new_threads, iteration);
1400 return data.new_threads;
1403 /* Search for new threads, accessing memory through stopped thread
1404 PTID. If UNTIL_NO_NEW is true, repeat searching until several
1405 searches in a row do not discover any new threads. */
1408 thread_db_find_new_threads_2 (ptid_t ptid, int until_no_new)
1411 struct lwp_info *lp;
1412 struct thread_db_info *info;
1413 int pid = ptid_get_pid (ptid);
1416 /* In linux, we can only read memory through a stopped lwp. */
1418 if (lp->stopped && ptid_get_pid (lp->ptid) == pid)
1422 /* There is no stopped thread. Bail out. */
1425 info = get_thread_db_info (GET_PID (ptid));
1427 /* Access an lwp we know is stopped. */
1428 info->proc_handle.ptid = ptid;
1432 /* Require 4 successive iterations which do not find any new threads.
1433 The 4 is a heuristic: there is an inherent race here, and I have
1434 seen that 2 iterations in a row are not always sufficient to
1435 "capture" all threads. */
1436 for (i = 0, loop = 0; loop < 4; ++i, ++loop)
1437 if (find_new_threads_once (info, i, NULL) != 0)
1438 /* Found some new threads. Restart the loop from beginning. */
1445 find_new_threads_once (info, 0, &err);
1447 error (_("Cannot find new threads: %s"), thread_db_err_str (err));
1452 thread_db_find_new_threads_1 (ptid_t ptid)
1454 thread_db_find_new_threads_2 (ptid, 0);
1459 thread_db_find_new_threads (struct target_ops *ops)
1461 struct thread_db_info *info;
1463 info = get_thread_db_info (GET_PID (inferior_ptid));
1468 thread_db_find_new_threads_1 (inferior_ptid);
1472 thread_db_pid_to_str (struct target_ops *ops, ptid_t ptid)
1474 struct thread_info *thread_info = find_thread_ptid (ptid);
1475 struct target_ops *beneath;
1477 if (thread_info != NULL && thread_info->private != NULL)
1479 static char buf[64];
1482 tid = thread_info->private->tid;
1483 snprintf (buf, sizeof (buf), "Thread 0x%lx (LWP %ld)",
1484 tid, GET_LWP (ptid));
1489 beneath = find_target_beneath (ops);
1490 if (beneath->to_pid_to_str (beneath, ptid))
1491 return beneath->to_pid_to_str (beneath, ptid);
1493 return normal_pid_to_str (ptid);
1496 /* Return a string describing the state of the thread specified by
1500 thread_db_extra_thread_info (struct thread_info *info)
1502 if (info->private == NULL)
1505 if (info->private->dying)
1511 /* Get the address of the thread local variable in load module LM which
1512 is stored at OFFSET within the thread local storage for thread PTID. */
1515 thread_db_get_thread_local_address (struct target_ops *ops,
1520 struct thread_info *thread_info;
1521 struct target_ops *beneath;
1523 /* If we have not discovered any threads yet, check now. */
1524 if (!have_threads (ptid))
1525 thread_db_find_new_threads_1 (ptid);
1527 /* Find the matching thread. */
1528 thread_info = find_thread_ptid (ptid);
1530 if (thread_info != NULL && thread_info->private != NULL)
1534 struct thread_db_info *info;
1536 info = get_thread_db_info (GET_PID (ptid));
1538 /* glibc doesn't provide the needed interface. */
1539 if (!info->td_thr_tls_get_addr_p)
1540 throw_error (TLS_NO_LIBRARY_SUPPORT_ERROR,
1541 _("No TLS library support"));
1543 /* Caller should have verified that lm != 0. */
1544 gdb_assert (lm != 0);
1546 /* Finally, get the address of the variable. */
1547 /* Note the cast through uintptr_t: this interface only works if
1548 a target address fits in a psaddr_t, which is a host pointer.
1549 So a 32-bit debugger can not access 64-bit TLS through this. */
1550 err = info->td_thr_tls_get_addr_p (&thread_info->private->th,
1551 (psaddr_t)(uintptr_t) lm,
1554 #ifdef THREAD_DB_HAS_TD_NOTALLOC
1555 /* The memory hasn't been allocated, yet. */
1556 if (err == TD_NOTALLOC)
1557 /* Now, if libthread_db provided the initialization image's
1558 address, we *could* try to build a non-lvalue value from
1559 the initialization image. */
1560 throw_error (TLS_NOT_ALLOCATED_YET_ERROR,
1561 _("TLS not allocated yet"));
1564 /* Something else went wrong. */
1566 throw_error (TLS_GENERIC_ERROR,
1567 (("%s")), thread_db_err_str (err));
1569 /* Cast assuming host == target. Joy. */
1570 /* Do proper sign extension for the target. */
1571 gdb_assert (exec_bfd);
1572 return (bfd_get_sign_extend_vma (exec_bfd) > 0
1573 ? (CORE_ADDR) (intptr_t) address
1574 : (CORE_ADDR) (uintptr_t) address);
1577 beneath = find_target_beneath (ops);
1578 if (beneath->to_get_thread_local_address)
1579 return beneath->to_get_thread_local_address (beneath, ptid, lm, offset);
1581 throw_error (TLS_GENERIC_ERROR,
1582 _("TLS not supported on this target"));
1585 /* Callback routine used to find a thread based on the TID part of
1589 thread_db_find_thread_from_tid (struct thread_info *thread, void *data)
1591 long *tid = (long *) data;
1593 if (thread->private->tid == *tid)
1599 /* Implement the to_get_ada_task_ptid target method for this target. */
1602 thread_db_get_ada_task_ptid (long lwp, long thread)
1604 struct thread_info *thread_info;
1606 thread_db_find_new_threads_1 (inferior_ptid);
1607 thread_info = iterate_over_threads (thread_db_find_thread_from_tid, &thread);
1609 gdb_assert (thread_info != NULL);
1611 return (thread_info->ptid);
1615 thread_db_resume (struct target_ops *ops,
1616 ptid_t ptid, int step, enum target_signal signo)
1618 struct target_ops *beneath = find_target_beneath (ops);
1619 struct thread_db_info *info;
1621 if (ptid_equal (ptid, minus_one_ptid))
1622 info = get_thread_db_info (GET_PID (inferior_ptid));
1624 info = get_thread_db_info (GET_PID (ptid));
1626 /* This workaround is only needed for child fork lwps stopped in a
1627 PTRACE_O_TRACEFORK event. When the inferior is resumed, the
1628 workaround can be disabled. */
1630 info->need_stale_parent_threads_check = 0;
1632 beneath->to_resume (beneath, ptid, step, signo);
1636 init_thread_db_ops (void)
1638 thread_db_ops.to_shortname = "multi-thread";
1639 thread_db_ops.to_longname = "multi-threaded child process.";
1640 thread_db_ops.to_doc = "Threads and pthreads support.";
1641 thread_db_ops.to_detach = thread_db_detach;
1642 thread_db_ops.to_wait = thread_db_wait;
1643 thread_db_ops.to_resume = thread_db_resume;
1644 thread_db_ops.to_mourn_inferior = thread_db_mourn_inferior;
1645 thread_db_ops.to_find_new_threads = thread_db_find_new_threads;
1646 thread_db_ops.to_pid_to_str = thread_db_pid_to_str;
1647 thread_db_ops.to_stratum = thread_stratum;
1648 thread_db_ops.to_has_thread_control = tc_schedlock;
1649 thread_db_ops.to_get_thread_local_address
1650 = thread_db_get_thread_local_address;
1651 thread_db_ops.to_extra_thread_info = thread_db_extra_thread_info;
1652 thread_db_ops.to_get_ada_task_ptid = thread_db_get_ada_task_ptid;
1653 thread_db_ops.to_magic = OPS_MAGIC;
1656 /* Provide a prototype to silence -Wmissing-prototypes. */
1657 extern initialize_file_ftype _initialize_thread_db;
1660 _initialize_thread_db (void)
1662 init_thread_db_ops ();
1663 add_target (&thread_db_ops);
1665 /* Defer loading of libthread_db.so until inferior is running.
1666 This allows gdb to load correct libthread_db for a given
1667 executable -- there could be mutiple versions of glibc,
1668 compiled with LinuxThreads or NPTL, and until there is
1669 a running inferior, we can't tell which libthread_db is
1670 the correct one to load. */
1672 libthread_db_search_path = xstrdup (LIBTHREAD_DB_SEARCH_PATH);
1674 add_setshow_optional_filename_cmd ("libthread-db-search-path",
1676 &libthread_db_search_path, _("\
1677 Set search path for libthread_db."), _("\
1678 Show the current search path or libthread_db."), _("\
1679 This path is used to search for libthread_db to be loaded into \
1683 &setlist, &showlist);
1684 /* Add ourselves to objfile event chain. */
1685 observer_attach_new_objfile (thread_db_new_objfile);