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 /* Location of the thread creation event breakpoint. The code at
108 this location in the child process will be called by the pthread
109 library whenever a new thread is created. By setting a special
110 breakpoint at this location, GDB can detect when a new thread is
111 created. We obtain this location via the td_ta_event_addr
113 CORE_ADDR td_create_bp_addr
;
115 /* Location of the thread death event breakpoint. */
116 CORE_ADDR td_death_bp_addr
;
118 /* Pointers to the libthread_db functions. */
120 td_err_e (*td_init_p
) (void);
122 td_err_e (*td_ta_new_p
) (struct ps_prochandle
* ps
,
124 td_err_e (*td_ta_map_id2thr_p
) (const td_thragent_t
*ta
, thread_t pt
,
125 td_thrhandle_t
*__th
);
126 td_err_e (*td_ta_map_lwp2thr_p
) (const td_thragent_t
*ta
,
127 lwpid_t lwpid
, td_thrhandle_t
*th
);
128 td_err_e (*td_ta_thr_iter_p
) (const td_thragent_t
*ta
,
129 td_thr_iter_f
*callback
, void *cbdata_p
,
130 td_thr_state_e state
, int ti_pri
,
131 sigset_t
*ti_sigmask_p
,
132 unsigned int ti_user_flags
);
133 td_err_e (*td_ta_event_addr_p
) (const td_thragent_t
*ta
,
134 td_event_e event
, td_notify_t
*ptr
);
135 td_err_e (*td_ta_set_event_p
) (const td_thragent_t
*ta
,
136 td_thr_events_t
*event
);
137 td_err_e (*td_ta_event_getmsg_p
) (const td_thragent_t
*ta
,
138 td_event_msg_t
*msg
);
140 td_err_e (*td_thr_validate_p
) (const td_thrhandle_t
*th
);
141 td_err_e (*td_thr_get_info_p
) (const td_thrhandle_t
*th
,
142 td_thrinfo_t
*infop
);
143 td_err_e (*td_thr_event_enable_p
) (const td_thrhandle_t
*th
,
146 td_err_e (*td_thr_tls_get_addr_p
) (const td_thrhandle_t
*th
,
148 size_t offset
, void **address
);
151 /* List of known processes using thread_db, and the required
153 struct thread_db_info
*thread_db_list
;
155 static void thread_db_find_new_threads_1 (ptid_t ptid
);
157 /* Add the current inferior to the list of processes using libpthread.
158 Return a pointer to the newly allocated object that was added to
159 THREAD_DB_LIST. HANDLE is the handle returned by dlopen'ing
162 static struct thread_db_info
*
163 add_thread_db_info (void *handle
)
166 struct thread_db_info
*info
;
168 info
= xcalloc (1, sizeof (*info
));
169 info
->pid
= ptid_get_pid (inferior_ptid
);
170 info
->handle
= handle
;
172 info
->next
= thread_db_list
;
173 thread_db_list
= info
;
178 /* Return the thread_db_info object representing the bookkeeping
179 related to process PID, if any; NULL otherwise. */
181 static struct thread_db_info
*
182 get_thread_db_info (int pid
)
184 struct thread_db_info
*info
;
186 for (info
= thread_db_list
; info
; info
= info
->next
)
187 if (pid
== info
->pid
)
193 /* When PID has exited or has been detached, we no longer want to keep
194 track of it as using libpthread. Call this function to discard
195 thread_db related info related to PID. Note that this closes
196 LIBTHREAD_DB_SO's dlopen'ed handle. */
199 delete_thread_db_info (int pid
)
201 struct thread_db_info
*info
, *info_prev
;
205 for (info
= thread_db_list
; info
; info_prev
= info
, info
= info
->next
)
206 if (pid
== info
->pid
)
212 if (info
->handle
!= NULL
)
213 dlclose (info
->handle
);
216 info_prev
->next
= info
->next
;
218 thread_db_list
= info
->next
;
223 /* Prototypes for local functions. */
224 static void attach_thread (ptid_t ptid
, const td_thrhandle_t
*th_p
,
225 const td_thrinfo_t
*ti_p
);
226 static void detach_thread (ptid_t ptid
);
229 /* Use "struct private_thread_info" to cache thread state. This is
230 a substantial optimization. */
232 struct private_thread_info
234 /* Flag set when we see a TD_DEATH event for this thread. */
235 unsigned int dying
:1;
237 /* Cached thread state. */
244 thread_db_err_str (td_err_e err
)
251 return "generic 'call succeeded'";
253 return "generic error";
255 return "no thread to satisfy query";
257 return "no sync handle to satisfy query";
259 return "no LWP to satisfy query";
261 return "invalid process handle";
263 return "invalid thread handle";
265 return "invalid synchronization handle";
267 return "invalid thread agent";
269 return "invalid key";
271 return "no event message for getmsg";
273 return "FPU register set not available";
275 return "application not linked with libthread";
277 return "requested event is not supported";
279 return "capability not available";
281 return "debugger service failed";
283 return "operation not applicable to";
285 return "no thread-specific data for this thread";
287 return "malloc failed";
289 return "only part of register set was written/read";
291 return "X register set not available for this thread";
292 #ifdef THREAD_DB_HAS_TD_NOTALLOC
294 return "thread has not yet allocated TLS for given module";
296 #ifdef THREAD_DB_HAS_TD_VERSION
298 return "versions of libpthread and libthread_db do not match";
300 #ifdef THREAD_DB_HAS_TD_NOTLS
302 return "there is no TLS segment in the given module";
305 snprintf (buf
, sizeof (buf
), "unknown thread_db error '%d'", err
);
310 /* Return 1 if any threads have been registered. There may be none if
311 the threading library is not fully initialized yet. */
314 have_threads_callback (struct thread_info
*thread
, void *args
)
316 int pid
= * (int *) args
;
317 if (ptid_get_pid (thread
->ptid
) != pid
)
320 return thread
->private != NULL
;
324 have_threads (ptid_t ptid
)
326 int pid
= ptid_get_pid (ptid
);
328 return iterate_over_threads (have_threads_callback
, &pid
) != NULL
;
331 struct thread_get_info_inout
333 struct thread_info
*thread_info
;
334 struct thread_db_info
*thread_db_info
;
337 /* A callback function for td_ta_thr_iter, which we use to map all
340 THP is a handle to the current thread; if INFOP is not NULL, the
341 struct thread_info associated with this thread is returned in
344 If the thread is a zombie, TD_THR_ZOMBIE is returned. Otherwise,
345 zero is returned to indicate success. */
348 thread_get_info_callback (const td_thrhandle_t
*thp
, void *argp
)
353 struct thread_get_info_inout
*inout
;
354 struct thread_db_info
*info
;
357 info
= inout
->thread_db_info
;
359 err
= info
->td_thr_get_info_p (thp
, &ti
);
361 error (_("thread_get_info_callback: cannot get thread info: %s"),
362 thread_db_err_str (err
));
364 /* Fill the cache. */
365 thread_ptid
= ptid_build (info
->pid
, ti
.ti_lid
, 0);
366 inout
->thread_info
= find_thread_pid (thread_ptid
);
368 /* In the case of a zombie thread, don't continue. We don't want to
369 attach to it thinking it is a new thread. */
370 if (ti
.ti_state
== TD_THR_UNKNOWN
|| ti
.ti_state
== TD_THR_ZOMBIE
)
371 return TD_THR_ZOMBIE
;
373 if (inout
->thread_info
== NULL
)
375 /* New thread. Attach to it now (why wait?). */
376 if (!have_threads (thread_ptid
))
377 thread_db_find_new_threads_1 (thread_ptid
);
379 attach_thread (thread_ptid
, thp
, &ti
);
380 inout
->thread_info
= find_thread_pid (thread_ptid
);
381 gdb_assert (inout
->thread_info
!= NULL
);
387 /* Convert between user-level thread ids and LWP ids. */
390 thread_from_lwp (ptid_t ptid
)
395 struct thread_db_info
*info
;
396 struct thread_get_info_inout io
= {0};
398 /* This ptid comes from linux-nat.c, which should always fill in the
400 gdb_assert (GET_LWP (ptid
) != 0);
402 info
= get_thread_db_info (GET_PID (ptid
));
404 /* Access an lwp we know is stopped. */
405 info
->proc_handle
.ptid
= ptid
;
406 err
= info
->td_ta_map_lwp2thr_p (info
->thread_agent
, GET_LWP (ptid
), &th
);
408 error (_("Cannot find user-level thread for LWP %ld: %s"),
409 GET_LWP (ptid
), thread_db_err_str (err
));
411 /* Fetch the thread info. If we get back TD_THR_ZOMBIE, then the
412 event thread has already died. If another gdb interface has called
413 thread_alive() previously, the thread won't be found on the thread list
414 anymore. In that case, we don't want to process this ptid anymore
415 to avoid the possibility of later treating it as a newly
416 discovered thread id that we should add to the list. Thus,
417 we return a -1 ptid which is also how the thread list marks a
419 io
.thread_db_info
= info
;
420 io
.thread_info
= NULL
;
421 if (thread_get_info_callback (&th
, &io
) == TD_THR_ZOMBIE
422 && io
.thread_info
== NULL
)
423 return minus_one_ptid
;
425 gdb_assert (ptid_get_tid (ptid
) == 0);
430 /* Attach to lwp PTID, doing whatever else is required to have this
431 LWP under the debugger's control --- e.g., enabling event
432 reporting. Returns true on success. */
434 thread_db_attach_lwp (ptid_t ptid
)
439 struct thread_db_info
*info
;
441 info
= get_thread_db_info (GET_PID (ptid
));
446 /* This ptid comes from linux-nat.c, which should always fill in the
448 gdb_assert (GET_LWP (ptid
) != 0);
450 /* Access an lwp we know is stopped. */
451 info
->proc_handle
.ptid
= ptid
;
453 /* If we have only looked at the first thread before libpthread was
454 initialized, we may not know its thread ID yet. Make sure we do
455 before we add another thread to the list. */
456 if (!have_threads (ptid
))
457 thread_db_find_new_threads_1 (ptid
);
459 err
= info
->td_ta_map_lwp2thr_p (info
->thread_agent
, GET_LWP (ptid
), &th
);
461 /* Cannot find user-level thread. */
464 err
= info
->td_thr_get_info_p (&th
, &ti
);
467 warning (_("Cannot get thread info: %s"), thread_db_err_str (err
));
471 attach_thread (ptid
, &th
, &ti
);
476 verbose_dlsym (void *handle
, const char *name
)
478 void *sym
= dlsym (handle
, name
);
480 warning (_("Symbol \"%s\" not found in libthread_db: %s"), name
, dlerror ());
485 enable_thread_event (int event
, CORE_ADDR
*bp
)
489 struct thread_db_info
*info
;
491 info
= get_thread_db_info (GET_PID (inferior_ptid
));
493 /* Access an lwp we know is stopped. */
494 info
->proc_handle
.ptid
= inferior_ptid
;
496 /* Get the breakpoint address for thread EVENT. */
497 err
= info
->td_ta_event_addr_p (info
->thread_agent
, event
, ¬ify
);
501 /* Set up the breakpoint. */
502 gdb_assert (exec_bfd
);
503 (*bp
) = (gdbarch_convert_from_func_ptr_addr
505 /* Do proper sign extension for the target. */
506 (bfd_get_sign_extend_vma (exec_bfd
) > 0
507 ? (CORE_ADDR
) (intptr_t) notify
.u
.bptaddr
508 : (CORE_ADDR
) (uintptr_t) notify
.u
.bptaddr
),
510 create_thread_event_breakpoint ((*bp
));
516 enable_thread_event_reporting (void)
518 td_thr_events_t events
;
521 #ifdef HAVE_GNU_LIBC_VERSION_H
522 const char *libc_version
;
523 int libc_major
, libc_minor
;
525 struct thread_db_info
*info
;
527 info
= get_thread_db_info (GET_PID (inferior_ptid
));
529 /* We cannot use the thread event reporting facility if these
530 functions aren't available. */
531 if (info
->td_ta_event_addr_p
== NULL
532 || info
->td_ta_set_event_p
== NULL
533 || info
->td_ta_event_getmsg_p
== NULL
534 || info
->td_thr_event_enable_p
== NULL
)
537 /* Set the process wide mask saying which events we're interested in. */
538 td_event_emptyset (&events
);
539 td_event_addset (&events
, TD_CREATE
);
541 #ifdef HAVE_GNU_LIBC_VERSION_H
542 /* The event reporting facility is broken for TD_DEATH events in
543 glibc 2.1.3, so don't enable it if we have glibc but a lower
545 libc_version
= gnu_get_libc_version ();
546 if (sscanf (libc_version
, "%d.%d", &libc_major
, &libc_minor
) == 2
547 && (libc_major
> 2 || (libc_major
== 2 && libc_minor
> 1)))
549 td_event_addset (&events
, TD_DEATH
);
551 err
= info
->td_ta_set_event_p (info
->thread_agent
, &events
);
554 warning (_("Unable to set global thread event mask: %s"),
555 thread_db_err_str (err
));
559 /* Delete previous thread event breakpoints, if any. */
560 remove_thread_event_breakpoints ();
561 info
->td_create_bp_addr
= 0;
562 info
->td_death_bp_addr
= 0;
564 /* Set up the thread creation event. */
565 err
= enable_thread_event (TD_CREATE
, &info
->td_create_bp_addr
);
568 warning (_("Unable to get location for thread creation breakpoint: %s"),
569 thread_db_err_str (err
));
573 /* Set up the thread death event. */
574 err
= enable_thread_event (TD_DEATH
, &info
->td_death_bp_addr
);
577 warning (_("Unable to get location for thread death breakpoint: %s"),
578 thread_db_err_str (err
));
583 /* Lookup a library in which given symbol resides.
584 Note: this is looking in GDB process, not in the inferior.
585 Returns library name, or NULL. */
588 dladdr_to_soname (const void *addr
)
592 if (dladdr (addr
, &info
) != 0)
593 return info
.dli_fname
;
597 /* Attempt to initialize dlopen()ed libthread_db, described by HANDLE.
599 Failure could happen if libthread_db does not have symbols we expect,
600 or when it refuses to work with the current inferior (e.g. due to
601 version mismatch between libthread_db and libpthread). */
604 try_thread_db_load_1 (struct thread_db_info
*info
)
608 /* Initialize pointers to the dynamic library functions we will use.
609 Essential functions first. */
611 info
->td_init_p
= verbose_dlsym (info
->handle
, "td_init");
612 if (info
->td_init_p
== NULL
)
615 err
= info
->td_init_p ();
618 warning (_("Cannot initialize libthread_db: %s"), thread_db_err_str (err
));
622 info
->td_ta_new_p
= verbose_dlsym (info
->handle
, "td_ta_new");
623 if (info
->td_ta_new_p
== NULL
)
626 /* Initialize the structure that identifies the child process. */
627 info
->proc_handle
.ptid
= inferior_ptid
;
629 /* Now attempt to open a connection to the thread library. */
630 err
= info
->td_ta_new_p (&info
->proc_handle
, &info
->thread_agent
);
634 printf_unfiltered (_("td_ta_new failed: %s\n"),
635 thread_db_err_str (err
));
640 #ifdef THREAD_DB_HAS_TD_VERSION
643 /* The errors above are not unexpected and silently ignored:
644 they just mean we haven't found correct version of
648 warning (_("td_ta_new failed: %s"), thread_db_err_str (err
));
653 info
->td_ta_map_id2thr_p
= verbose_dlsym (info
->handle
, "td_ta_map_id2thr");
654 if (info
->td_ta_map_id2thr_p
== NULL
)
657 info
->td_ta_map_lwp2thr_p
= verbose_dlsym (info
->handle
, "td_ta_map_lwp2thr");
658 if (info
->td_ta_map_lwp2thr_p
== NULL
)
661 info
->td_ta_thr_iter_p
= verbose_dlsym (info
->handle
, "td_ta_thr_iter");
662 if (info
->td_ta_thr_iter_p
== NULL
)
665 info
->td_thr_validate_p
= verbose_dlsym (info
->handle
, "td_thr_validate");
666 if (info
->td_thr_validate_p
== NULL
)
669 info
->td_thr_get_info_p
= verbose_dlsym (info
->handle
, "td_thr_get_info");
670 if (info
->td_thr_get_info_p
== NULL
)
673 /* These are not essential. */
674 info
->td_ta_event_addr_p
= dlsym (info
->handle
, "td_ta_event_addr");
675 info
->td_ta_set_event_p
= dlsym (info
->handle
, "td_ta_set_event");
676 info
->td_ta_event_getmsg_p
= dlsym (info
->handle
, "td_ta_event_getmsg");
677 info
->td_thr_event_enable_p
= dlsym (info
->handle
, "td_thr_event_enable");
678 info
->td_thr_tls_get_addr_p
= dlsym (info
->handle
, "td_thr_tls_get_addr");
680 printf_unfiltered (_("[Thread debugging using libthread_db enabled]\n"));
682 if (info_verbose
|| *libthread_db_search_path
)
686 library
= dladdr_to_soname (*info
->td_ta_new_p
);
688 library
= LIBTHREAD_DB_SO
;
690 printf_unfiltered (_("Using host libthread_db library \"%s\".\n"),
694 /* The thread library was detected. Activate the thread_db target
695 if this is the first process using it. */
696 if (thread_db_list
->next
== NULL
)
697 push_target (&thread_db_ops
);
699 enable_thread_event_reporting ();
700 thread_db_find_new_threads_1 (inferior_ptid
);
704 /* Attempt to use LIBRARY as libthread_db. LIBRARY could be absolute,
705 relative, or just LIBTHREAD_DB. */
708 try_thread_db_load (const char *library
)
711 struct thread_db_info
*info
;
714 printf_unfiltered (_("Trying host libthread_db library: %s.\n"),
716 handle
= dlopen (library
, RTLD_NOW
);
720 printf_unfiltered (_("dlopen failed: %s.\n"), dlerror ());
724 if (info_verbose
&& strchr (library
, '/') == NULL
)
728 td_init
= dlsym (handle
, "td_init");
731 const char *const libpath
= dladdr_to_soname (td_init
);
734 printf_unfiltered (_("Host %s resolved to: %s.\n"),
739 info
= add_thread_db_info (handle
);
741 if (try_thread_db_load_1 (info
))
744 /* This library "refused" to work on current inferior. */
745 delete_thread_db_info (GET_PID (inferior_ptid
));
750 /* Search libthread_db_search_path for libthread_db which "agrees"
751 to work on current inferior. */
754 thread_db_load_search (void)
757 const char *search_path
= libthread_db_search_path
;
762 const char *end
= strchr (search_path
, ':');
765 size_t len
= end
- search_path
;
766 if (len
+ 1 + strlen (LIBTHREAD_DB_SO
) + 1 > sizeof (path
))
768 char *cp
= xmalloc (len
+ 1);
769 memcpy (cp
, search_path
, len
);
771 warning (_("libthread_db_search_path component too long,"
772 " ignored: %s."), cp
);
774 search_path
+= len
+ 1;
777 memcpy (path
, search_path
, len
);
779 search_path
+= len
+ 1;
783 size_t len
= strlen (search_path
);
785 if (len
+ 1 + strlen (LIBTHREAD_DB_SO
) + 1 > sizeof (path
))
787 warning (_("libthread_db_search_path component too long,"
788 " ignored: %s."), search_path
);
791 memcpy (path
, search_path
, len
+ 1);
795 strcat (path
, LIBTHREAD_DB_SO
);
796 if (try_thread_db_load (path
))
803 rc
= try_thread_db_load (LIBTHREAD_DB_SO
);
807 /* Attempt to load and initialize libthread_db.
812 thread_db_load (void)
815 struct thread_db_info
*info
;
817 info
= get_thread_db_info (GET_PID (inferior_ptid
));
822 /* Don't attempt to use thread_db on targets which can not run
823 (executables not running yet, core files) for now. */
824 if (!target_has_execution
)
827 /* Don't attempt to use thread_db for remote targets. */
828 if (!target_can_run (¤t_target
))
831 if (thread_db_load_search ())
834 /* None of the libthread_db's on our search path, not the system default
835 ones worked. If the executable is dynamically linked against
836 libpthread, try loading libthread_db from the same directory. */
839 if (libpthread_name_p (obj
->name
))
841 char path
[PATH_MAX
], *cp
;
843 gdb_assert (strlen (obj
->name
) < sizeof (path
));
844 strcpy (path
, obj
->name
);
845 cp
= strrchr (path
, '/');
849 warning (_("Expected absolute pathname for libpthread in the"
850 " inferior, but got %s."), path
);
852 else if (cp
+ 1 + strlen (LIBTHREAD_DB_SO
) + 1 > path
+ sizeof (path
))
854 warning (_("Unexpected: path to libpthread in the inferior is"
855 " too long: %s"), path
);
859 strcpy (cp
+ 1, LIBTHREAD_DB_SO
);
860 if (try_thread_db_load (path
))
863 warning (_("Unable to find libthread_db matching inferior's thread"
864 " library, thread debugging will not be available."));
867 /* Either this executable isn't using libpthread at all, or it is
868 statically linked. Since we can't easily distinguish these two cases,
869 no warning is issued. */
874 disable_thread_event_reporting (void)
876 td_thr_events_t events
;
877 struct thread_db_info
*info
;
879 info
= get_thread_db_info (GET_PID (inferior_ptid
));
881 /* Set the process wide mask saying we aren't interested in any
883 td_event_emptyset (&events
);
884 info
->td_ta_set_event_p (info
->thread_agent
, &events
);
886 /* Delete thread event breakpoints, if any. */
887 remove_thread_event_breakpoints ();
888 info
->td_create_bp_addr
= 0;
889 info
->td_death_bp_addr
= 0;
893 check_thread_signals (void)
895 #ifdef GET_THREAD_SIGNALS
901 GET_THREAD_SIGNALS (&mask
);
902 sigemptyset (&thread_stop_set
);
903 sigemptyset (&thread_print_set
);
905 for (i
= 1; i
< NSIG
; i
++)
907 if (sigismember (&mask
, i
))
909 if (signal_stop_update (target_signal_from_host (i
), 0))
910 sigaddset (&thread_stop_set
, i
);
911 if (signal_print_update (target_signal_from_host (i
), 0))
912 sigaddset (&thread_print_set
, i
);
920 /* Check whether thread_db is usable. This function is called when
921 an inferior is created (or otherwise acquired, e.g. attached to)
922 and when new shared libraries are loaded into a running process. */
925 check_for_thread_db (void)
928 static void *last_loaded
;
930 /* Do nothing if we couldn't load libthread_db.so.1. */
931 if (!thread_db_load ())
936 thread_db_new_objfile (struct objfile
*objfile
)
938 /* This observer must always be called with inferior_ptid set
942 check_for_thread_db ();
945 /* Attach to a new thread. This function is called when we receive a
946 TD_CREATE event or when we iterate over all threads and find one
947 that wasn't already in our list. */
950 attach_thread (ptid_t ptid
, const td_thrhandle_t
*th_p
,
951 const td_thrinfo_t
*ti_p
)
953 struct private_thread_info
*private;
954 struct thread_info
*tp
= NULL
;
956 struct thread_db_info
*info
;
958 /* If we're being called after a TD_CREATE event, we may already
959 know about this thread. There are two ways this can happen. We
960 may have iterated over all threads between the thread creation
961 and the TD_CREATE event, for instance when the user has issued
962 the `info threads' command before the SIGTRAP for hitting the
963 thread creation breakpoint was reported. Alternatively, the
964 thread may have exited and a new one been created with the same
965 thread ID. In the first case we don't need to do anything; in
966 the second case we should discard information about the dead
967 thread and attach to the new one. */
968 if (in_thread_list (ptid
))
970 tp
= find_thread_pid (ptid
);
971 gdb_assert (tp
!= NULL
);
973 /* If tp->private is NULL, then GDB is already attached to this
974 thread, but we do not know anything about it. We can learn
975 about it here. This can only happen if we have some other
976 way besides libthread_db to notice new threads (i.e.
977 PTRACE_EVENT_CLONE); assume the same mechanism notices thread
978 exit, so this can not be a stale thread recreated with the
980 if (tp
->private != NULL
)
982 if (!tp
->private->dying
)
985 delete_thread (ptid
);
990 check_thread_signals ();
992 if (ti_p
->ti_state
== TD_THR_UNKNOWN
|| ti_p
->ti_state
== TD_THR_ZOMBIE
)
993 return; /* A zombie thread -- do not attach. */
995 /* Under GNU/Linux, we have to attach to each and every thread. */
997 && lin_lwp_attach_lwp (BUILD_LWP (ti_p
->ti_lid
, GET_PID (ptid
))) < 0)
1000 /* Construct the thread's private data. */
1001 private = xmalloc (sizeof (struct private_thread_info
));
1002 memset (private, 0, sizeof (struct private_thread_info
));
1004 /* A thread ID of zero may mean the thread library has not initialized
1005 yet. But we shouldn't even get here if that's the case. FIXME:
1006 if we change GDB to always have at least one thread in the thread
1007 list this will have to go somewhere else; maybe private == NULL
1008 until the thread_db target claims it. */
1009 gdb_assert (ti_p
->ti_tid
!= 0);
1010 private->th
= *th_p
;
1011 private->tid
= ti_p
->ti_tid
;
1013 /* Add the thread to GDB's thread list. */
1015 tp
= add_thread_with_info (ptid
, private);
1017 tp
->private = private;
1019 info
= get_thread_db_info (GET_PID (ptid
));
1021 /* Enable thread event reporting for this thread. */
1022 err
= info
->td_thr_event_enable_p (th_p
, 1);
1024 error (_("Cannot enable thread event reporting for %s: %s"),
1025 target_pid_to_str (ptid
), thread_db_err_str (err
));
1029 detach_thread (ptid_t ptid
)
1031 struct thread_info
*thread_info
;
1033 /* Don't delete the thread now, because it still reports as active
1034 until it has executed a few instructions after the event
1035 breakpoint - if we deleted it now, "info threads" would cause us
1036 to re-attach to it. Just mark it as having had a TD_DEATH
1037 event. This means that we won't delete it from our thread list
1038 until we notice that it's dead (via prune_threads), or until
1039 something re-uses its thread ID. We'll report the thread exit
1040 when the underlying LWP dies. */
1041 thread_info
= find_thread_pid (ptid
);
1042 gdb_assert (thread_info
!= NULL
&& thread_info
->private != NULL
);
1043 thread_info
->private->dying
= 1;
1047 thread_db_detach (struct target_ops
*ops
, char *args
, int from_tty
)
1049 struct target_ops
*target_beneath
= find_target_beneath (ops
);
1050 struct thread_db_info
*info
;
1052 info
= get_thread_db_info (GET_PID (inferior_ptid
));
1056 disable_thread_event_reporting ();
1058 /* Delete the old thread event breakpoints. Note that unlike
1059 when mourning, we can remove them here because there's still
1060 a live inferior to poke at. In any case, GDB will not try to
1061 insert anything in the inferior when removing a
1063 remove_thread_event_breakpoints ();
1065 delete_thread_db_info (GET_PID (inferior_ptid
));
1068 target_beneath
->to_detach (target_beneath
, args
, from_tty
);
1070 /* NOTE: From this point on, inferior_ptid is null_ptid. */
1072 /* If there are no more processes using libpthread, detach the
1073 thread_db target ops. */
1074 if (!thread_db_list
)
1075 unpush_target (&thread_db_ops
);
1078 /* Check if PID is currently stopped at the location of a thread event
1079 breakpoint location. If it is, read the event message and act upon
1083 check_event (ptid_t ptid
)
1085 struct regcache
*regcache
= get_thread_regcache (ptid
);
1086 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1092 struct thread_db_info
*info
;
1094 info
= get_thread_db_info (GET_PID (ptid
));
1096 /* Bail out early if we're not at a thread event breakpoint. */
1097 stop_pc
= regcache_read_pc (regcache
)
1098 - gdbarch_decr_pc_after_break (gdbarch
);
1099 if (stop_pc
!= info
->td_create_bp_addr
1100 && stop_pc
!= info
->td_death_bp_addr
)
1103 /* Access an lwp we know is stopped. */
1104 info
->proc_handle
.ptid
= ptid
;
1106 /* If we have only looked at the first thread before libpthread was
1107 initialized, we may not know its thread ID yet. Make sure we do
1108 before we add another thread to the list. */
1109 if (!have_threads (ptid
))
1110 thread_db_find_new_threads_1 (ptid
);
1112 /* If we are at a create breakpoint, we do not know what new lwp
1113 was created and cannot specifically locate the event message for it.
1114 We have to call td_ta_event_getmsg() to get
1115 the latest message. Since we have no way of correlating whether
1116 the event message we get back corresponds to our breakpoint, we must
1117 loop and read all event messages, processing them appropriately.
1118 This guarantees we will process the correct message before continuing
1119 from the breakpoint.
1121 Currently, death events are not enabled. If they are enabled,
1122 the death event can use the td_thr_event_getmsg() interface to
1123 get the message specifically for that lwp and avoid looping
1130 err
= info
->td_ta_event_getmsg_p (info
->thread_agent
, &msg
);
1133 if (err
== TD_NOMSG
)
1136 error (_("Cannot get thread event message: %s"),
1137 thread_db_err_str (err
));
1140 err
= info
->td_thr_get_info_p (msg
.th_p
, &ti
);
1142 error (_("Cannot get thread info: %s"), thread_db_err_str (err
));
1144 ptid
= ptid_build (GET_PID (ptid
), ti
.ti_lid
, 0);
1149 /* Call attach_thread whether or not we already know about a
1150 thread with this thread ID. */
1151 attach_thread (ptid
, msg
.th_p
, &ti
);
1157 if (!in_thread_list (ptid
))
1158 error (_("Spurious thread death event."));
1160 detach_thread (ptid
);
1165 error (_("Spurious thread event."));
1172 thread_db_wait (struct target_ops
*ops
,
1173 ptid_t ptid
, struct target_waitstatus
*ourstatus
)
1175 struct thread_db_info
*info
;
1176 struct target_ops
*beneath
= find_target_beneath (ops
);
1178 ptid
= beneath
->to_wait (beneath
, ptid
, ourstatus
);
1180 if (ourstatus
->kind
== TARGET_WAITKIND_IGNORE
)
1183 if (ourstatus
->kind
== TARGET_WAITKIND_EXITED
1184 || ourstatus
->kind
== TARGET_WAITKIND_SIGNALLED
)
1187 info
= get_thread_db_info (GET_PID (ptid
));
1189 /* If this process isn't using thread_db, we're done. */
1193 if (ourstatus
->kind
== TARGET_WAITKIND_EXECD
)
1195 /* Breakpoints have already been marked non-inserted by the
1196 layer below. We're safe in knowing that removing them will
1197 not write the shadows of the old image into the new
1199 remove_thread_event_breakpoints ();
1201 /* New image, it may or may not end up using thread_db. Assume
1202 not unless we find otherwise. */
1203 delete_thread_db_info (GET_PID (ptid
));
1204 if (!thread_db_list
)
1205 unpush_target (&thread_db_ops
);
1210 /* If we do not know about the main thread yet, this would be a good time to
1212 if (ourstatus
->kind
== TARGET_WAITKIND_STOPPED
&& !have_threads (ptid
))
1213 thread_db_find_new_threads_1 (ptid
);
1215 if (ourstatus
->kind
== TARGET_WAITKIND_STOPPED
1216 && ourstatus
->value
.sig
== TARGET_SIGNAL_TRAP
)
1217 /* Check for a thread event. */
1220 if (have_threads (ptid
))
1222 /* Change ptids back into the higher level PID + TID format. If
1223 the thread is dead and no longer on the thread list, we will
1224 get back a dead ptid. This can occur if the thread death
1225 event gets postponed by other simultaneous events. In such a
1226 case, we want to just ignore the event and continue on. */
1228 ptid
= thread_from_lwp (ptid
);
1229 if (GET_PID (ptid
) == -1)
1230 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
1237 thread_db_mourn_inferior (struct target_ops
*ops
)
1239 struct target_ops
*target_beneath
= find_target_beneath (ops
);
1241 delete_thread_db_info (GET_PID (inferior_ptid
));
1243 /* Delete the old thread event breakpoints. Mark breakpoints out,
1244 so that we don't try to un-insert them. */
1245 mark_breakpoints_out ();
1246 remove_thread_event_breakpoints ();
1248 target_beneath
->to_mourn_inferior (target_beneath
);
1250 /* Detach thread_db target ops. */
1251 if (!thread_db_list
)
1252 unpush_target (ops
);
1256 find_new_threads_callback (const td_thrhandle_t
*th_p
, void *data
)
1261 struct thread_info
*tp
;
1262 struct thread_db_info
*info
= data
;
1264 err
= info
->td_thr_get_info_p (th_p
, &ti
);
1266 error (_("find_new_threads_callback: cannot get thread info: %s"),
1267 thread_db_err_str (err
));
1269 if (ti
.ti_state
== TD_THR_UNKNOWN
|| ti
.ti_state
== TD_THR_ZOMBIE
)
1270 return 0; /* A zombie -- ignore. */
1272 ptid
= ptid_build (info
->pid
, ti
.ti_lid
, 0);
1276 /* A thread ID of zero means that this is the main thread, but
1277 glibc has not yet initialized thread-local storage and the
1278 pthread library. We do not know what the thread's TID will
1279 be yet. Just enable event reporting and otherwise ignore
1282 err
= info
->td_thr_event_enable_p (th_p
, 1);
1284 error (_("Cannot enable thread event reporting for %s: %s"),
1285 target_pid_to_str (ptid
), thread_db_err_str (err
));
1290 tp
= find_thread_pid (ptid
);
1291 if (tp
== NULL
|| tp
->private == NULL
)
1292 attach_thread (ptid
, th_p
, &ti
);
1297 /* Search for new threads, accessing memory through stopped thread
1301 thread_db_find_new_threads_1 (ptid_t ptid
)
1304 struct lwp_info
*lp
;
1305 struct thread_db_info
*info
;
1306 int pid
= ptid_get_pid (ptid
);
1308 /* In linux, we can only read memory through a stopped lwp. */
1310 if (lp
->stopped
&& ptid_get_pid (lp
->ptid
) == pid
)
1314 /* There is no stopped thread. Bail out. */
1317 info
= get_thread_db_info (GET_PID (ptid
));
1319 /* Access an lwp we know is stopped. */
1320 info
->proc_handle
.ptid
= ptid
;
1321 /* Iterate over all user-space threads to discover new threads. */
1322 err
= info
->td_ta_thr_iter_p (info
->thread_agent
, find_new_threads_callback
,
1323 info
, TD_THR_ANY_STATE
, TD_THR_LOWEST_PRIORITY
,
1324 TD_SIGNO_MASK
, TD_THR_ANY_USER_FLAGS
);
1326 error (_("Cannot find new threads: %s"), thread_db_err_str (err
));
1330 thread_db_find_new_threads (struct target_ops
*ops
)
1332 struct thread_db_info
*info
;
1334 info
= get_thread_db_info (GET_PID (inferior_ptid
));
1339 thread_db_find_new_threads_1 (inferior_ptid
);
1343 thread_db_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
1345 struct thread_info
*thread_info
= find_thread_pid (ptid
);
1346 struct target_ops
*beneath
;
1348 if (thread_info
!= NULL
&& thread_info
->private != NULL
)
1350 static char buf
[64];
1353 tid
= thread_info
->private->tid
;
1354 snprintf (buf
, sizeof (buf
), "Thread 0x%lx (LWP %ld)",
1355 tid
, GET_LWP (ptid
));
1360 beneath
= find_target_beneath (ops
);
1361 if (beneath
->to_pid_to_str (beneath
, ptid
))
1362 return beneath
->to_pid_to_str (beneath
, ptid
);
1364 return normal_pid_to_str (ptid
);
1367 /* Return a string describing the state of the thread specified by
1371 thread_db_extra_thread_info (struct thread_info
*info
)
1373 if (info
->private == NULL
)
1376 if (info
->private->dying
)
1382 /* Get the address of the thread local variable in load module LM which
1383 is stored at OFFSET within the thread local storage for thread PTID. */
1386 thread_db_get_thread_local_address (struct target_ops
*ops
,
1391 struct thread_info
*thread_info
;
1392 struct target_ops
*beneath
;
1394 /* If we have not discovered any threads yet, check now. */
1395 if (!have_threads (ptid
))
1396 thread_db_find_new_threads_1 (ptid
);
1398 /* Find the matching thread. */
1399 thread_info
= find_thread_pid (ptid
);
1401 if (thread_info
!= NULL
&& thread_info
->private != NULL
)
1405 struct thread_db_info
*info
;
1407 info
= get_thread_db_info (GET_PID (ptid
));
1409 /* glibc doesn't provide the needed interface. */
1410 if (!info
->td_thr_tls_get_addr_p
)
1411 throw_error (TLS_NO_LIBRARY_SUPPORT_ERROR
,
1412 _("No TLS library support"));
1414 /* Caller should have verified that lm != 0. */
1415 gdb_assert (lm
!= 0);
1417 /* Finally, get the address of the variable. */
1418 err
= info
->td_thr_tls_get_addr_p (&thread_info
->private->th
,
1419 (void *)(size_t) lm
,
1422 #ifdef THREAD_DB_HAS_TD_NOTALLOC
1423 /* The memory hasn't been allocated, yet. */
1424 if (err
== TD_NOTALLOC
)
1425 /* Now, if libthread_db provided the initialization image's
1426 address, we *could* try to build a non-lvalue value from
1427 the initialization image. */
1428 throw_error (TLS_NOT_ALLOCATED_YET_ERROR
,
1429 _("TLS not allocated yet"));
1432 /* Something else went wrong. */
1434 throw_error (TLS_GENERIC_ERROR
,
1435 (("%s")), thread_db_err_str (err
));
1437 /* Cast assuming host == target. Joy. */
1438 /* Do proper sign extension for the target. */
1439 gdb_assert (exec_bfd
);
1440 return (bfd_get_sign_extend_vma (exec_bfd
) > 0
1441 ? (CORE_ADDR
) (intptr_t) address
1442 : (CORE_ADDR
) (uintptr_t) address
);
1445 beneath
= find_target_beneath (ops
);
1446 if (beneath
->to_get_thread_local_address
)
1447 return beneath
->to_get_thread_local_address (beneath
, ptid
, lm
, offset
);
1449 throw_error (TLS_GENERIC_ERROR
,
1450 _("TLS not supported on this target"));
1453 /* Callback routine used to find a thread based on the TID part of
1457 thread_db_find_thread_from_tid (struct thread_info
*thread
, void *data
)
1459 long *tid
= (long *) data
;
1461 if (thread
->private->tid
== *tid
)
1467 /* Implement the to_get_ada_task_ptid target method for this target. */
1470 thread_db_get_ada_task_ptid (long lwp
, long thread
)
1472 struct thread_info
*thread_info
;
1474 thread_db_find_new_threads_1 (inferior_ptid
);
1475 thread_info
= iterate_over_threads (thread_db_find_thread_from_tid
, &thread
);
1477 gdb_assert (thread_info
!= NULL
);
1479 return (thread_info
->ptid
);
1483 init_thread_db_ops (void)
1485 thread_db_ops
.to_shortname
= "multi-thread";
1486 thread_db_ops
.to_longname
= "multi-threaded child process.";
1487 thread_db_ops
.to_doc
= "Threads and pthreads support.";
1488 thread_db_ops
.to_detach
= thread_db_detach
;
1489 thread_db_ops
.to_wait
= thread_db_wait
;
1490 thread_db_ops
.to_mourn_inferior
= thread_db_mourn_inferior
;
1491 thread_db_ops
.to_find_new_threads
= thread_db_find_new_threads
;
1492 thread_db_ops
.to_pid_to_str
= thread_db_pid_to_str
;
1493 thread_db_ops
.to_stratum
= thread_stratum
;
1494 thread_db_ops
.to_has_thread_control
= tc_schedlock
;
1495 thread_db_ops
.to_get_thread_local_address
1496 = thread_db_get_thread_local_address
;
1497 thread_db_ops
.to_extra_thread_info
= thread_db_extra_thread_info
;
1498 thread_db_ops
.to_get_ada_task_ptid
= thread_db_get_ada_task_ptid
;
1499 thread_db_ops
.to_magic
= OPS_MAGIC
;
1502 /* Provide a prototype to silence -Wmissing-prototypes. */
1503 extern initialize_file_ftype _initialize_thread_db
;
1506 _initialize_thread_db (void)
1508 init_thread_db_ops ();
1509 add_target (&thread_db_ops
);
1511 /* Defer loading of libthread_db.so until inferior is running.
1512 This allows gdb to load correct libthread_db for a given
1513 executable -- there could be mutiple versions of glibc,
1514 compiled with LinuxThreads or NPTL, and until there is
1515 a running inferior, we can't tell which libthread_db is
1516 the correct one to load. */
1518 libthread_db_search_path
= xstrdup (LIBTHREAD_DB_SEARCH_PATH
);
1520 add_setshow_optional_filename_cmd ("libthread-db-search-path",
1522 &libthread_db_search_path
, _("\
1523 Set search path for libthread_db."), _("\
1524 Show the current search path or libthread_db."), _("\
1525 This path is used to search for libthread_db to be loaded into \
1529 &setlist
, &showlist
);
1530 /* Add ourselves to objfile event chain. */
1531 observer_attach_new_objfile (thread_db_new_objfile
);