1 /* libthread_db assisted debugging support, generic parts.
3 Copyright (C) 1999-2015 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "gdb_proc_service.h"
23 #include "nat/gdb_thread_db.h"
28 #include "gdbthread.h"
36 #include "solib-svr4.h"
39 #include "linux-nat.h"
40 #include "nat/linux-procfs.h"
41 #include "nat/linux-ptrace.h"
42 #include "nat/linux-osdata.h"
43 #include "auto-load.h"
44 #include "cli/cli-utils.h"
49 /* GNU/Linux libthread_db support.
51 libthread_db is a library, provided along with libpthread.so, which
52 exposes the internals of the thread library to a debugger. It
53 allows GDB to find existing threads, new threads as they are
54 created, thread IDs (usually, the result of pthread_self), and
55 thread-local variables.
57 The libthread_db interface originates on Solaris, where it is
58 both more powerful and more complicated. This implementation
59 only works for LinuxThreads and NPTL, the two glibc threading
60 libraries. It assumes that each thread is permanently assigned
61 to a single light-weight process (LWP).
63 libthread_db-specific information is stored in the "private" field
64 of struct thread_info. When the field is NULL we do not yet have
65 information about the new thread; this could be temporary (created,
66 but the thread library's data structures do not reflect it yet)
67 or permanent (created using clone instead of pthread_create).
69 Process IDs managed by linux-thread-db.c match those used by
70 linux-nat.c: a common PID for all processes, an LWP ID for each
71 thread, and no TID. We save the TID in private. Keeping it out
72 of the ptid_t prevents thread IDs changing when libpthread is
73 loaded or unloaded. */
75 static char *libthread_db_search_path
;
77 /* Set to non-zero if thread_db auto-loading is enabled
78 by the "set auto-load libthread-db" command. */
79 static int auto_load_thread_db
= 1;
81 /* Returns true if we need to use thread_db thread create/death event
82 breakpoints to learn about threads. */
85 thread_db_use_events (void)
87 /* Not necessary if the kernel supports clone events. */
88 return !linux_supports_traceclone ();
91 /* "show" command for the auto_load_thread_db configuration variable. */
94 show_auto_load_thread_db (struct ui_file
*file
, int from_tty
,
95 struct cmd_list_element
*c
, const char *value
)
97 fprintf_filtered (file
, _("Auto-loading of inferior specific libthread_db "
103 set_libthread_db_search_path (char *ignored
, int from_tty
,
104 struct cmd_list_element
*c
)
106 if (*libthread_db_search_path
== '\0')
108 xfree (libthread_db_search_path
);
109 libthread_db_search_path
= xstrdup (LIBTHREAD_DB_SEARCH_PATH
);
113 /* If non-zero, print details of libthread_db processing. */
115 static unsigned int libthread_db_debug
;
118 show_libthread_db_debug (struct ui_file
*file
, int from_tty
,
119 struct cmd_list_element
*c
, const char *value
)
121 fprintf_filtered (file
, _("libthread-db debugging is %s.\n"), value
);
124 /* If we're running on GNU/Linux, we must explicitly attach to any new
127 /* This module's target vector. */
128 static struct target_ops thread_db_ops
;
130 /* Non-zero if we have determined the signals used by the threads
132 static int thread_signals
;
133 static sigset_t thread_stop_set
;
134 static sigset_t thread_print_set
;
136 struct thread_db_info
138 struct thread_db_info
*next
;
140 /* Process id this object refers to. */
143 /* Handle from dlopen for libthread_db.so. */
146 /* Absolute pathname from gdb_realpath to disk file used for dlopen-ing
147 HANDLE. It may be NULL for system library. */
150 /* Structure that identifies the child process for the
151 <proc_service.h> interface. */
152 struct ps_prochandle proc_handle
;
154 /* Connection to the libthread_db library. */
155 td_thragent_t
*thread_agent
;
157 /* True if we need to apply the workaround for glibc/BZ5983. When
158 we catch a PTRACE_O_TRACEFORK, and go query the child's thread
159 list, nptl_db returns the parent's threads in addition to the new
160 (single) child thread. If this flag is set, we do extra work to
161 be able to ignore such stale entries. */
162 int need_stale_parent_threads_check
;
164 /* Location of the thread creation event breakpoint. The code at
165 this location in the child process will be called by the pthread
166 library whenever a new thread is created. By setting a special
167 breakpoint at this location, GDB can detect when a new thread is
168 created. We obtain this location via the td_ta_event_addr
170 CORE_ADDR td_create_bp_addr
;
172 /* Location of the thread death event breakpoint. */
173 CORE_ADDR td_death_bp_addr
;
175 /* Pointers to the libthread_db functions. */
177 td_err_e (*td_init_p
) (void);
179 td_err_e (*td_ta_new_p
) (struct ps_prochandle
* ps
,
181 td_err_e (*td_ta_map_id2thr_p
) (const td_thragent_t
*ta
, thread_t pt
,
182 td_thrhandle_t
*__th
);
183 td_err_e (*td_ta_map_lwp2thr_p
) (const td_thragent_t
*ta
,
184 lwpid_t lwpid
, td_thrhandle_t
*th
);
185 td_err_e (*td_ta_thr_iter_p
) (const td_thragent_t
*ta
,
186 td_thr_iter_f
*callback
, void *cbdata_p
,
187 td_thr_state_e state
, int ti_pri
,
188 sigset_t
*ti_sigmask_p
,
189 unsigned int ti_user_flags
);
190 td_err_e (*td_ta_event_addr_p
) (const td_thragent_t
*ta
,
191 td_event_e event
, td_notify_t
*ptr
);
192 td_err_e (*td_ta_set_event_p
) (const td_thragent_t
*ta
,
193 td_thr_events_t
*event
);
194 td_err_e (*td_ta_clear_event_p
) (const td_thragent_t
*ta
,
195 td_thr_events_t
*event
);
196 td_err_e (*td_ta_event_getmsg_p
) (const td_thragent_t
*ta
,
197 td_event_msg_t
*msg
);
199 td_err_e (*td_thr_validate_p
) (const td_thrhandle_t
*th
);
200 td_err_e (*td_thr_get_info_p
) (const td_thrhandle_t
*th
,
201 td_thrinfo_t
*infop
);
202 td_err_e (*td_thr_event_enable_p
) (const td_thrhandle_t
*th
,
205 td_err_e (*td_thr_tls_get_addr_p
) (const td_thrhandle_t
*th
,
206 psaddr_t map_address
,
207 size_t offset
, psaddr_t
*address
);
208 td_err_e (*td_thr_tlsbase_p
) (const td_thrhandle_t
*th
,
209 unsigned long int modid
,
213 /* List of known processes using thread_db, and the required
215 struct thread_db_info
*thread_db_list
;
217 static void thread_db_find_new_threads_1 (ptid_t ptid
);
218 static void thread_db_find_new_threads_2 (ptid_t ptid
, int until_no_new
);
220 /* Add the current inferior to the list of processes using libpthread.
221 Return a pointer to the newly allocated object that was added to
222 THREAD_DB_LIST. HANDLE is the handle returned by dlopen'ing
225 static struct thread_db_info
*
226 add_thread_db_info (void *handle
)
228 struct thread_db_info
*info
;
230 info
= xcalloc (1, sizeof (*info
));
231 info
->pid
= ptid_get_pid (inferior_ptid
);
232 info
->handle
= handle
;
234 /* The workaround works by reading from /proc/pid/status, so it is
235 disabled for core files. */
236 if (target_has_execution
)
237 info
->need_stale_parent_threads_check
= 1;
239 info
->next
= thread_db_list
;
240 thread_db_list
= info
;
245 /* Return the thread_db_info object representing the bookkeeping
246 related to process PID, if any; NULL otherwise. */
248 static struct thread_db_info
*
249 get_thread_db_info (int pid
)
251 struct thread_db_info
*info
;
253 for (info
= thread_db_list
; info
; info
= info
->next
)
254 if (pid
== info
->pid
)
260 /* When PID has exited or has been detached, we no longer want to keep
261 track of it as using libpthread. Call this function to discard
262 thread_db related info related to PID. Note that this closes
263 LIBTHREAD_DB_SO's dlopen'ed handle. */
266 delete_thread_db_info (int pid
)
268 struct thread_db_info
*info
, *info_prev
;
272 for (info
= thread_db_list
; info
; info_prev
= info
, info
= info
->next
)
273 if (pid
== info
->pid
)
279 if (info
->handle
!= NULL
)
280 dlclose (info
->handle
);
282 xfree (info
->filename
);
285 info_prev
->next
= info
->next
;
287 thread_db_list
= info
->next
;
292 /* Prototypes for local functions. */
293 static int attach_thread (ptid_t ptid
, const td_thrhandle_t
*th_p
,
294 const td_thrinfo_t
*ti_p
);
295 static void detach_thread (ptid_t ptid
);
298 /* Use "struct private_thread_info" to cache thread state. This is
299 a substantial optimization. */
301 struct private_thread_info
303 /* Flag set when we see a TD_DEATH event for this thread. */
304 unsigned int dying
:1;
306 /* Cached thread state. */
313 thread_db_err_str (td_err_e err
)
320 return "generic 'call succeeded'";
322 return "generic error";
324 return "no thread to satisfy query";
326 return "no sync handle to satisfy query";
328 return "no LWP to satisfy query";
330 return "invalid process handle";
332 return "invalid thread handle";
334 return "invalid synchronization handle";
336 return "invalid thread agent";
338 return "invalid key";
340 return "no event message for getmsg";
342 return "FPU register set not available";
344 return "application not linked with libthread";
346 return "requested event is not supported";
348 return "capability not available";
350 return "debugger service failed";
352 return "operation not applicable to";
354 return "no thread-specific data for this thread";
356 return "malloc failed";
358 return "only part of register set was written/read";
360 return "X register set not available for this thread";
361 #ifdef THREAD_DB_HAS_TD_NOTALLOC
363 return "thread has not yet allocated TLS for given module";
365 #ifdef THREAD_DB_HAS_TD_VERSION
367 return "versions of libpthread and libthread_db do not match";
369 #ifdef THREAD_DB_HAS_TD_NOTLS
371 return "there is no TLS segment in the given module";
374 snprintf (buf
, sizeof (buf
), "unknown thread_db error '%d'", err
);
379 /* Return 1 if any threads have been registered. There may be none if
380 the threading library is not fully initialized yet. */
383 have_threads_callback (struct thread_info
*thread
, void *args
)
385 int pid
= * (int *) args
;
387 if (ptid_get_pid (thread
->ptid
) != pid
)
390 return thread
->private != NULL
;
394 have_threads (ptid_t ptid
)
396 int pid
= ptid_get_pid (ptid
);
398 return iterate_over_threads (have_threads_callback
, &pid
) != NULL
;
401 struct thread_get_info_inout
403 struct thread_info
*thread_info
;
404 struct thread_db_info
*thread_db_info
;
407 /* A callback function for td_ta_thr_iter, which we use to map all
410 THP is a handle to the current thread; if INFOP is not NULL, the
411 struct thread_info associated with this thread is returned in
414 If the thread is a zombie, TD_THR_ZOMBIE is returned. Otherwise,
415 zero is returned to indicate success. */
418 thread_get_info_callback (const td_thrhandle_t
*thp
, void *argp
)
423 struct thread_get_info_inout
*inout
;
424 struct thread_db_info
*info
;
427 info
= inout
->thread_db_info
;
429 err
= info
->td_thr_get_info_p (thp
, &ti
);
431 error (_("thread_get_info_callback: cannot get thread info: %s"),
432 thread_db_err_str (err
));
434 /* Fill the cache. */
435 thread_ptid
= ptid_build (info
->pid
, ti
.ti_lid
, 0);
436 inout
->thread_info
= find_thread_ptid (thread_ptid
);
438 if (inout
->thread_info
== NULL
)
440 /* New thread. Attach to it now (why wait?). */
441 if (!have_threads (thread_ptid
))
442 thread_db_find_new_threads_1 (thread_ptid
);
444 attach_thread (thread_ptid
, thp
, &ti
);
445 inout
->thread_info
= find_thread_ptid (thread_ptid
);
446 gdb_assert (inout
->thread_info
!= NULL
);
452 /* Fetch the user-level thread id of PTID. */
455 thread_from_lwp (ptid_t ptid
)
459 struct thread_db_info
*info
;
460 struct thread_get_info_inout io
= {0};
462 /* Just in case td_ta_map_lwp2thr doesn't initialize it completely. */
465 /* This ptid comes from linux-nat.c, which should always fill in the
467 gdb_assert (ptid_get_lwp (ptid
) != 0);
469 info
= get_thread_db_info (ptid_get_pid (ptid
));
471 /* Access an lwp we know is stopped. */
472 info
->proc_handle
.ptid
= ptid
;
473 err
= info
->td_ta_map_lwp2thr_p (info
->thread_agent
, ptid_get_lwp (ptid
),
476 error (_("Cannot find user-level thread for LWP %ld: %s"),
477 ptid_get_lwp (ptid
), thread_db_err_str (err
));
479 /* Long-winded way of fetching the thread info. */
480 io
.thread_db_info
= info
;
481 io
.thread_info
= NULL
;
482 thread_get_info_callback (&th
, &io
);
486 /* Attach to lwp PTID, doing whatever else is required to have this
487 LWP under the debugger's control --- e.g., enabling event
488 reporting. Returns true on success. */
490 thread_db_attach_lwp (ptid_t ptid
)
495 struct thread_db_info
*info
;
497 info
= get_thread_db_info (ptid_get_pid (ptid
));
502 /* This ptid comes from linux-nat.c, which should always fill in the
504 gdb_assert (ptid_get_lwp (ptid
) != 0);
506 /* Access an lwp we know is stopped. */
507 info
->proc_handle
.ptid
= ptid
;
509 /* If we have only looked at the first thread before libpthread was
510 initialized, we may not know its thread ID yet. Make sure we do
511 before we add another thread to the list. */
512 if (!have_threads (ptid
))
513 thread_db_find_new_threads_1 (ptid
);
515 err
= info
->td_ta_map_lwp2thr_p (info
->thread_agent
, ptid_get_lwp (ptid
),
518 /* Cannot find user-level thread. */
521 err
= info
->td_thr_get_info_p (&th
, &ti
);
524 warning (_("Cannot get thread info: %s"), thread_db_err_str (err
));
528 attach_thread (ptid
, &th
, &ti
);
533 verbose_dlsym (void *handle
, const char *name
)
535 void *sym
= dlsym (handle
, name
);
537 warning (_("Symbol \"%s\" not found in libthread_db: %s"),
543 enable_thread_event (int event
, CORE_ADDR
*bp
)
547 struct thread_db_info
*info
;
549 info
= get_thread_db_info (ptid_get_pid (inferior_ptid
));
551 /* Access an lwp we know is stopped. */
552 info
->proc_handle
.ptid
= inferior_ptid
;
554 /* Get the breakpoint address for thread EVENT. */
555 err
= info
->td_ta_event_addr_p (info
->thread_agent
, event
, ¬ify
);
559 /* Set up the breakpoint. */
560 gdb_assert (exec_bfd
);
561 (*bp
) = (gdbarch_convert_from_func_ptr_addr
563 /* Do proper sign extension for the target. */
564 (bfd_get_sign_extend_vma (exec_bfd
) > 0
565 ? (CORE_ADDR
) (intptr_t) notify
.u
.bptaddr
566 : (CORE_ADDR
) (uintptr_t) notify
.u
.bptaddr
),
568 create_thread_event_breakpoint (target_gdbarch (), *bp
);
573 /* Verify inferior's '\0'-terminated symbol VER_SYMBOL starts with "%d.%d" and
574 return 1 if this version is lower (and not equal) to
575 VER_MAJOR_MIN.VER_MINOR_MIN. Return 0 in all other cases. */
578 inferior_has_bug (const char *ver_symbol
, int ver_major_min
, int ver_minor_min
)
580 struct bound_minimal_symbol version_msym
;
581 CORE_ADDR version_addr
;
583 int err
, got
, retval
= 0;
585 version_msym
= lookup_minimal_symbol (ver_symbol
, NULL
, NULL
);
586 if (version_msym
.minsym
== NULL
)
589 version_addr
= BMSYMBOL_VALUE_ADDRESS (version_msym
);
590 got
= target_read_string (version_addr
, &version
, 32, &err
);
591 if (err
== 0 && memchr (version
, 0, got
) == &version
[got
-1])
595 retval
= (sscanf (version
, "%d.%d", &major
, &minor
) == 2
596 && (major
< ver_major_min
597 || (major
== ver_major_min
&& minor
< ver_minor_min
)));
605 enable_thread_event_reporting (void)
607 td_thr_events_t events
;
609 struct thread_db_info
*info
;
611 info
= get_thread_db_info (ptid_get_pid (inferior_ptid
));
613 /* We cannot use the thread event reporting facility if these
614 functions aren't available. */
615 if (info
->td_ta_event_addr_p
== NULL
616 || info
->td_ta_set_event_p
== NULL
617 || info
->td_ta_event_getmsg_p
== NULL
618 || info
->td_thr_event_enable_p
== NULL
)
621 /* Set the process wide mask saying which events we're interested in. */
622 td_event_emptyset (&events
);
623 td_event_addset (&events
, TD_CREATE
);
625 /* There is a bug fixed between linuxthreads 2.1.3 and 2.2 by
626 commit 2e4581e4fba917f1779cd0a010a45698586c190a
627 * manager.c (pthread_exited): Correctly report event as TD_REAP
628 instead of TD_DEATH. Fix comments.
629 where event reporting facility is broken for TD_DEATH events,
630 so don't enable it if we have glibc but a lower version. */
631 if (!inferior_has_bug ("__linuxthreads_version", 2, 2))
632 td_event_addset (&events
, TD_DEATH
);
634 err
= info
->td_ta_set_event_p (info
->thread_agent
, &events
);
637 warning (_("Unable to set global thread event mask: %s"),
638 thread_db_err_str (err
));
642 /* Delete previous thread event breakpoints, if any. */
643 remove_thread_event_breakpoints ();
644 info
->td_create_bp_addr
= 0;
645 info
->td_death_bp_addr
= 0;
647 /* Set up the thread creation event. */
648 err
= enable_thread_event (TD_CREATE
, &info
->td_create_bp_addr
);
651 warning (_("Unable to get location for thread creation breakpoint: %s"),
652 thread_db_err_str (err
));
656 /* Set up the thread death event. */
657 err
= enable_thread_event (TD_DEATH
, &info
->td_death_bp_addr
);
660 warning (_("Unable to get location for thread death breakpoint: %s"),
661 thread_db_err_str (err
));
666 /* Similar as thread_db_find_new_threads_1, but try to silently ignore errors
669 Return 1 if the caller should abort libthread_db initialization. Return 0
673 thread_db_find_new_threads_silently (ptid_t ptid
)
675 volatile struct gdb_exception except
;
677 TRY_CATCH (except
, RETURN_MASK_ERROR
)
679 thread_db_find_new_threads_2 (ptid
, 1);
682 if (except
.reason
< 0)
684 if (libthread_db_debug
)
685 exception_fprintf (gdb_stdlog
, except
,
686 "Warning: thread_db_find_new_threads_silently: ");
688 /* There is a bug fixed between nptl 2.6.1 and 2.7 by
689 commit 7d9d8bd18906fdd17364f372b160d7ab896ce909
690 where calls to td_thr_get_info fail with TD_ERR for statically linked
691 executables if td_thr_get_info is called before glibc has initialized
694 If the nptl bug is NOT present in the inferior and still thread_db
695 reports an error return 1. It means the inferior has corrupted thread
696 list and GDB should fall back only to LWPs.
698 If the nptl bug is present in the inferior return 0 to silently ignore
699 such errors, and let gdb enumerate threads again later. In such case
700 GDB cannot properly display LWPs if the inferior thread list is
701 corrupted. For core files it does not apply, no 'later enumeration'
704 if (!target_has_execution
|| !inferior_has_bug ("nptl_version", 2, 7))
706 exception_fprintf (gdb_stderr
, except
,
707 _("Warning: couldn't activate thread debugging "
708 "using libthread_db: "));
715 /* Lookup a library in which given symbol resides.
716 Note: this is looking in GDB process, not in the inferior.
717 Returns library name, or NULL. */
720 dladdr_to_soname (const void *addr
)
724 if (dladdr (addr
, &info
) != 0)
725 return info
.dli_fname
;
729 /* Attempt to initialize dlopen()ed libthread_db, described by INFO.
731 Failure could happen if libthread_db does not have symbols we expect,
732 or when it refuses to work with the current inferior (e.g. due to
733 version mismatch between libthread_db and libpthread). */
736 try_thread_db_load_1 (struct thread_db_info
*info
)
740 /* Initialize pointers to the dynamic library functions we will use.
741 Essential functions first. */
743 info
->td_init_p
= verbose_dlsym (info
->handle
, "td_init");
744 if (info
->td_init_p
== NULL
)
747 err
= info
->td_init_p ();
750 warning (_("Cannot initialize libthread_db: %s"),
751 thread_db_err_str (err
));
755 info
->td_ta_new_p
= verbose_dlsym (info
->handle
, "td_ta_new");
756 if (info
->td_ta_new_p
== NULL
)
759 /* Initialize the structure that identifies the child process. */
760 info
->proc_handle
.ptid
= inferior_ptid
;
762 /* Now attempt to open a connection to the thread library. */
763 err
= info
->td_ta_new_p (&info
->proc_handle
, &info
->thread_agent
);
766 if (libthread_db_debug
)
767 fprintf_unfiltered (gdb_stdlog
, _("td_ta_new failed: %s\n"),
768 thread_db_err_str (err
));
773 #ifdef THREAD_DB_HAS_TD_VERSION
776 /* The errors above are not unexpected and silently ignored:
777 they just mean we haven't found correct version of
781 warning (_("td_ta_new failed: %s"), thread_db_err_str (err
));
786 info
->td_ta_map_id2thr_p
= verbose_dlsym (info
->handle
, "td_ta_map_id2thr");
787 if (info
->td_ta_map_id2thr_p
== NULL
)
790 info
->td_ta_map_lwp2thr_p
= verbose_dlsym (info
->handle
,
791 "td_ta_map_lwp2thr");
792 if (info
->td_ta_map_lwp2thr_p
== NULL
)
795 info
->td_ta_thr_iter_p
= verbose_dlsym (info
->handle
, "td_ta_thr_iter");
796 if (info
->td_ta_thr_iter_p
== NULL
)
799 info
->td_thr_validate_p
= verbose_dlsym (info
->handle
, "td_thr_validate");
800 if (info
->td_thr_validate_p
== NULL
)
803 info
->td_thr_get_info_p
= verbose_dlsym (info
->handle
, "td_thr_get_info");
804 if (info
->td_thr_get_info_p
== NULL
)
807 /* These are not essential. */
808 info
->td_ta_event_addr_p
= dlsym (info
->handle
, "td_ta_event_addr");
809 info
->td_ta_set_event_p
= dlsym (info
->handle
, "td_ta_set_event");
810 info
->td_ta_clear_event_p
= dlsym (info
->handle
, "td_ta_clear_event");
811 info
->td_ta_event_getmsg_p
= dlsym (info
->handle
, "td_ta_event_getmsg");
812 info
->td_thr_event_enable_p
= dlsym (info
->handle
, "td_thr_event_enable");
813 info
->td_thr_tls_get_addr_p
= dlsym (info
->handle
, "td_thr_tls_get_addr");
814 info
->td_thr_tlsbase_p
= dlsym (info
->handle
, "td_thr_tlsbase");
816 if (thread_db_find_new_threads_silently (inferior_ptid
) != 0)
818 /* Even if libthread_db initializes, if the thread list is
819 corrupted, we'd not manage to list any threads. Better reject this
820 thread_db, and fall back to at least listing LWPs. */
824 printf_unfiltered (_("[Thread debugging using libthread_db enabled]\n"));
826 if (*libthread_db_search_path
|| libthread_db_debug
)
828 struct ui_file
*file
;
831 library
= dladdr_to_soname (*info
->td_ta_new_p
);
833 library
= LIBTHREAD_DB_SO
;
835 /* If we'd print this to gdb_stdout when debug output is
836 disabled, still print it to gdb_stdout if debug output is
837 enabled. User visible output should not depend on debug
839 file
= *libthread_db_search_path
!= '\0' ? gdb_stdout
: gdb_stdlog
;
840 fprintf_unfiltered (file
, _("Using host libthread_db library \"%s\".\n"),
844 /* The thread library was detected. Activate the thread_db target
845 if this is the first process using it. */
846 if (thread_db_list
->next
== NULL
)
847 push_target (&thread_db_ops
);
849 /* Enable event reporting, but not when debugging a core file. */
850 if (target_has_execution
&& thread_db_use_events ())
851 enable_thread_event_reporting ();
856 /* Attempt to use LIBRARY as libthread_db. LIBRARY could be absolute,
857 relative, or just LIBTHREAD_DB. */
860 try_thread_db_load (const char *library
, int check_auto_load_safe
)
863 struct thread_db_info
*info
;
865 if (libthread_db_debug
)
866 fprintf_unfiltered (gdb_stdlog
,
867 _("Trying host libthread_db library: %s.\n"),
870 if (check_auto_load_safe
)
872 if (access (library
, R_OK
) != 0)
874 /* Do not print warnings by file_is_auto_load_safe if the library does
875 not exist at this place. */
876 if (libthread_db_debug
)
877 fprintf_unfiltered (gdb_stdlog
, _("open failed: %s.\n"),
878 safe_strerror (errno
));
882 if (!file_is_auto_load_safe (library
, _("auto-load: Loading libthread-db "
883 "library \"%s\" from explicit "
889 handle
= dlopen (library
, RTLD_NOW
);
892 if (libthread_db_debug
)
893 fprintf_unfiltered (gdb_stdlog
, _("dlopen failed: %s.\n"), dlerror ());
897 if (libthread_db_debug
&& strchr (library
, '/') == NULL
)
901 td_init
= dlsym (handle
, "td_init");
904 const char *const libpath
= dladdr_to_soname (td_init
);
907 fprintf_unfiltered (gdb_stdlog
, _("Host %s resolved to: %s.\n"),
912 info
= add_thread_db_info (handle
);
914 /* Do not save system library name, that one is always trusted. */
915 if (strchr (library
, '/') != NULL
)
916 info
->filename
= gdb_realpath (library
);
918 if (try_thread_db_load_1 (info
))
921 /* This library "refused" to work on current inferior. */
922 delete_thread_db_info (ptid_get_pid (inferior_ptid
));
926 /* Subroutine of try_thread_db_load_from_pdir to simplify it.
927 Try loading libthread_db in directory(OBJ)/SUBDIR.
928 SUBDIR may be NULL. It may also be something like "../lib64".
929 The result is true for success. */
932 try_thread_db_load_from_pdir_1 (struct objfile
*obj
, const char *subdir
)
934 struct cleanup
*cleanup
;
937 const char *obj_name
= objfile_name (obj
);
939 if (obj_name
[0] != '/')
941 warning (_("Expected absolute pathname for libpthread in the"
942 " inferior, but got %s."), obj_name
);
946 path
= xmalloc (strlen (obj_name
) + (subdir
? strlen (subdir
) + 1 : 0)
947 + 1 + strlen (LIBTHREAD_DB_SO
) + 1);
948 cleanup
= make_cleanup (xfree
, path
);
950 strcpy (path
, obj_name
);
951 cp
= strrchr (path
, '/');
952 /* This should at minimum hit the first character. */
953 gdb_assert (cp
!= NULL
);
960 strcat (cp
, LIBTHREAD_DB_SO
);
962 result
= try_thread_db_load (path
, 1);
964 do_cleanups (cleanup
);
968 /* Handle $pdir in libthread-db-search-path.
969 Look for libthread_db in directory(libpthread)/SUBDIR.
970 SUBDIR may be NULL. It may also be something like "../lib64".
971 The result is true for success. */
974 try_thread_db_load_from_pdir (const char *subdir
)
978 if (!auto_load_thread_db
)
982 if (libpthread_name_p (objfile_name (obj
)))
984 if (try_thread_db_load_from_pdir_1 (obj
, subdir
))
987 /* We may have found the separate-debug-info version of
988 libpthread, and it may live in a directory without a matching
990 if (obj
->separate_debug_objfile_backlink
!= NULL
)
991 return try_thread_db_load_from_pdir_1 (obj
->separate_debug_objfile_backlink
,
1000 /* Handle $sdir in libthread-db-search-path.
1001 Look for libthread_db in the system dirs, or wherever a plain
1002 dlopen(file_without_path) will look.
1003 The result is true for success. */
1006 try_thread_db_load_from_sdir (void)
1008 return try_thread_db_load (LIBTHREAD_DB_SO
, 0);
1011 /* Try to load libthread_db from directory DIR of length DIR_LEN.
1012 The result is true for success. */
1015 try_thread_db_load_from_dir (const char *dir
, size_t dir_len
)
1017 struct cleanup
*cleanup
;
1021 if (!auto_load_thread_db
)
1024 path
= xmalloc (dir_len
+ 1 + strlen (LIBTHREAD_DB_SO
) + 1);
1025 cleanup
= make_cleanup (xfree
, path
);
1027 memcpy (path
, dir
, dir_len
);
1028 path
[dir_len
] = '/';
1029 strcpy (path
+ dir_len
+ 1, LIBTHREAD_DB_SO
);
1031 result
= try_thread_db_load (path
, 1);
1033 do_cleanups (cleanup
);
1037 /* Search libthread_db_search_path for libthread_db which "agrees"
1038 to work on current inferior.
1039 The result is true for success. */
1042 thread_db_load_search (void)
1044 VEC (char_ptr
) *dir_vec
;
1045 struct cleanup
*cleanups
;
1049 dir_vec
= dirnames_to_char_ptr_vec (libthread_db_search_path
);
1050 cleanups
= make_cleanup_free_char_ptr_vec (dir_vec
);
1052 for (i
= 0; VEC_iterate (char_ptr
, dir_vec
, i
, this_dir
); ++i
)
1054 const int pdir_len
= sizeof ("$pdir") - 1;
1055 size_t this_dir_len
;
1057 this_dir_len
= strlen (this_dir
);
1059 if (strncmp (this_dir
, "$pdir", pdir_len
) == 0
1060 && (this_dir
[pdir_len
] == '\0'
1061 || this_dir
[pdir_len
] == '/'))
1063 char *subdir
= NULL
;
1064 struct cleanup
*free_subdir_cleanup
1065 = make_cleanup (null_cleanup
, NULL
);
1067 if (this_dir
[pdir_len
] == '/')
1069 subdir
= xmalloc (strlen (this_dir
));
1070 make_cleanup (xfree
, subdir
);
1071 strcpy (subdir
, this_dir
+ pdir_len
+ 1);
1073 rc
= try_thread_db_load_from_pdir (subdir
);
1074 do_cleanups (free_subdir_cleanup
);
1078 else if (strcmp (this_dir
, "$sdir") == 0)
1080 if (try_thread_db_load_from_sdir ())
1088 if (try_thread_db_load_from_dir (this_dir
, this_dir_len
))
1096 do_cleanups (cleanups
);
1097 if (libthread_db_debug
)
1098 fprintf_unfiltered (gdb_stdlog
,
1099 _("thread_db_load_search returning %d\n"), rc
);
1103 /* Return non-zero if the inferior has a libpthread. */
1106 has_libpthread (void)
1108 struct objfile
*obj
;
1111 if (libpthread_name_p (objfile_name (obj
)))
1117 /* Attempt to load and initialize libthread_db.
1118 Return 1 on success. */
1121 thread_db_load (void)
1123 struct thread_db_info
*info
;
1125 info
= get_thread_db_info (ptid_get_pid (inferior_ptid
));
1130 /* Don't attempt to use thread_db on executables not running
1132 if (!target_has_registers
)
1135 /* Don't attempt to use thread_db for remote targets. */
1136 if (!(target_can_run (¤t_target
) || core_bfd
))
1139 if (thread_db_load_search ())
1142 /* We couldn't find a libthread_db.
1143 If the inferior has a libpthread warn the user. */
1144 if (has_libpthread ())
1146 warning (_("Unable to find libthread_db matching inferior's thread"
1147 " library, thread debugging will not be available."));
1151 /* Either this executable isn't using libpthread at all, or it is
1152 statically linked. Since we can't easily distinguish these two cases,
1153 no warning is issued. */
1158 disable_thread_event_reporting (struct thread_db_info
*info
)
1160 if (info
->td_ta_clear_event_p
!= NULL
)
1162 td_thr_events_t events
;
1164 /* Set the process wide mask saying we aren't interested in any
1166 td_event_fillset (&events
);
1167 info
->td_ta_clear_event_p (info
->thread_agent
, &events
);
1170 info
->td_create_bp_addr
= 0;
1171 info
->td_death_bp_addr
= 0;
1175 check_thread_signals (void)
1177 if (!thread_signals
)
1182 lin_thread_get_thread_signals (&mask
);
1183 sigemptyset (&thread_stop_set
);
1184 sigemptyset (&thread_print_set
);
1186 for (i
= 1; i
< NSIG
; i
++)
1188 if (sigismember (&mask
, i
))
1190 if (signal_stop_update (gdb_signal_from_host (i
), 0))
1191 sigaddset (&thread_stop_set
, i
);
1192 if (signal_print_update (gdb_signal_from_host (i
), 0))
1193 sigaddset (&thread_print_set
, i
);
1200 /* Check whether thread_db is usable. This function is called when
1201 an inferior is created (or otherwise acquired, e.g. attached to)
1202 and when new shared libraries are loaded into a running process. */
1205 check_for_thread_db (void)
1207 /* Do nothing if we couldn't load libthread_db.so.1. */
1208 if (!thread_db_load ())
1212 /* This function is called via the new_objfile observer. */
1215 thread_db_new_objfile (struct objfile
*objfile
)
1217 /* This observer must always be called with inferior_ptid set
1221 /* libpthread with separate debug info has its debug info file already
1222 loaded (and notified without successful thread_db initialization)
1223 the time observer_notify_new_objfile is called for the library itself.
1224 Static executables have their separate debug info loaded already
1225 before the inferior has started. */
1226 && objfile
->separate_debug_objfile_backlink
== NULL
1227 /* Only check for thread_db if we loaded libpthread,
1228 or if this is the main symbol file.
1229 We need to check OBJF_MAINLINE to handle the case of debugging
1230 a statically linked executable AND the symbol file is specified AFTER
1231 the exec file is loaded (e.g., gdb -c core ; file foo).
1232 For dynamically linked executables, libpthread can be near the end
1233 of the list of shared libraries to load, and in an app of several
1234 thousand shared libraries, this can otherwise be painful. */
1235 && ((objfile
->flags
& OBJF_MAINLINE
) != 0
1236 || libpthread_name_p (objfile_name (objfile
))))
1237 check_for_thread_db ();
1241 check_pid_namespace_match (void)
1243 /* Check is only relevant for local targets targets. */
1244 if (target_can_run (¤t_target
))
1246 /* If the child is in a different PID namespace, its idea of its
1247 PID will differ from our idea of its PID. When we scan the
1248 child's thread list, we'll mistakenly think it has no threads
1249 since the thread PID fields won't match the PID we give to
1251 char *our_pid_ns
= linux_proc_pid_get_ns (getpid (), "pid");
1252 char *inferior_pid_ns
= linux_proc_pid_get_ns (
1253 ptid_get_pid (inferior_ptid
), "pid");
1255 if (our_pid_ns
!= NULL
&& inferior_pid_ns
!= NULL
1256 && strcmp (our_pid_ns
, inferior_pid_ns
) != 0)
1258 warning (_ ("Target and debugger are in different PID "
1259 "namespaces; thread lists and other data are "
1260 "likely unreliable"));
1264 xfree (inferior_pid_ns
);
1268 /* This function is called via the inferior_created observer.
1269 This handles the case of debugging statically linked executables. */
1272 thread_db_inferior_created (struct target_ops
*target
, int from_tty
)
1274 check_pid_namespace_match ();
1275 check_for_thread_db ();
1278 /* Update the thread's state (what's displayed in "info threads"),
1279 from libthread_db thread state information. */
1282 update_thread_state (struct private_thread_info
*private,
1283 const td_thrinfo_t
*ti_p
)
1285 private->dying
= (ti_p
->ti_state
== TD_THR_UNKNOWN
1286 || ti_p
->ti_state
== TD_THR_ZOMBIE
);
1289 /* Attach to a new thread. This function is called when we receive a
1290 TD_CREATE event or when we iterate over all threads and find one
1291 that wasn't already in our list. Returns true on success. */
1294 attach_thread (ptid_t ptid
, const td_thrhandle_t
*th_p
,
1295 const td_thrinfo_t
*ti_p
)
1297 struct private_thread_info
*private;
1298 struct thread_info
*tp
;
1300 struct thread_db_info
*info
;
1302 /* If we're being called after a TD_CREATE event, we may already
1303 know about this thread. There are two ways this can happen. We
1304 may have iterated over all threads between the thread creation
1305 and the TD_CREATE event, for instance when the user has issued
1306 the `info threads' command before the SIGTRAP for hitting the
1307 thread creation breakpoint was reported. Alternatively, the
1308 thread may have exited and a new one been created with the same
1309 thread ID. In the first case we don't need to do anything; in
1310 the second case we should discard information about the dead
1311 thread and attach to the new one. */
1312 tp
= find_thread_ptid (ptid
);
1315 /* If tp->private is NULL, then GDB is already attached to this
1316 thread, but we do not know anything about it. We can learn
1317 about it here. This can only happen if we have some other
1318 way besides libthread_db to notice new threads (i.e.
1319 PTRACE_EVENT_CLONE); assume the same mechanism notices thread
1320 exit, so this can not be a stale thread recreated with the
1322 if (tp
->private != NULL
)
1324 if (!tp
->private->dying
)
1327 delete_thread (ptid
);
1332 if (target_has_execution
)
1333 check_thread_signals ();
1335 /* Under GNU/Linux, we have to attach to each and every thread. */
1336 if (target_has_execution
1341 res
= lin_lwp_attach_lwp (ptid_build (ptid_get_pid (ptid
),
1345 /* Error, stop iterating. */
1350 /* Pretend this thread doesn't exist yet, and keep
1355 /* Otherwise, we sucessfully attached to the thread. */
1358 /* Construct the thread's private data. */
1359 private = xmalloc (sizeof (struct private_thread_info
));
1360 memset (private, 0, sizeof (struct private_thread_info
));
1362 /* A thread ID of zero may mean the thread library has not initialized
1363 yet. But we shouldn't even get here if that's the case. FIXME:
1364 if we change GDB to always have at least one thread in the thread
1365 list this will have to go somewhere else; maybe private == NULL
1366 until the thread_db target claims it. */
1367 gdb_assert (ti_p
->ti_tid
!= 0);
1368 private->th
= *th_p
;
1369 private->tid
= ti_p
->ti_tid
;
1370 update_thread_state (private, ti_p
);
1372 /* Add the thread to GDB's thread list. */
1374 add_thread_with_info (ptid
, private);
1376 tp
->private = private;
1378 info
= get_thread_db_info (ptid_get_pid (ptid
));
1380 /* Enable thread event reporting for this thread, except when
1381 debugging a core file. */
1382 if (target_has_execution
&& thread_db_use_events ())
1384 err
= info
->td_thr_event_enable_p (th_p
, 1);
1386 error (_("Cannot enable thread event reporting for %s: %s"),
1387 target_pid_to_str (ptid
), thread_db_err_str (err
));
1394 detach_thread (ptid_t ptid
)
1396 struct thread_info
*thread_info
;
1398 /* Don't delete the thread now, because it still reports as active
1399 until it has executed a few instructions after the event
1400 breakpoint - if we deleted it now, "info threads" would cause us
1401 to re-attach to it. Just mark it as having had a TD_DEATH
1402 event. This means that we won't delete it from our thread list
1403 until we notice that it's dead (via prune_threads), or until
1404 something re-uses its thread ID. We'll report the thread exit
1405 when the underlying LWP dies. */
1406 thread_info
= find_thread_ptid (ptid
);
1407 gdb_assert (thread_info
!= NULL
&& thread_info
->private != NULL
);
1408 thread_info
->private->dying
= 1;
1412 thread_db_detach (struct target_ops
*ops
, const char *args
, int from_tty
)
1414 struct target_ops
*target_beneath
= find_target_beneath (ops
);
1415 struct thread_db_info
*info
;
1417 info
= get_thread_db_info (ptid_get_pid (inferior_ptid
));
1421 if (target_has_execution
&& thread_db_use_events ())
1423 disable_thread_event_reporting (info
);
1425 /* Delete the old thread event breakpoints. Note that
1426 unlike when mourning, we can remove them here because
1427 there's still a live inferior to poke at. In any case,
1428 GDB will not try to insert anything in the inferior when
1429 removing a breakpoint. */
1430 remove_thread_event_breakpoints ();
1433 delete_thread_db_info (ptid_get_pid (inferior_ptid
));
1436 target_beneath
->to_detach (target_beneath
, args
, from_tty
);
1438 /* NOTE: From this point on, inferior_ptid is null_ptid. */
1440 /* If there are no more processes using libpthread, detach the
1441 thread_db target ops. */
1442 if (!thread_db_list
)
1443 unpush_target (&thread_db_ops
);
1446 /* Check if PID is currently stopped at the location of a thread event
1447 breakpoint location. If it is, read the event message and act upon
1451 check_event (ptid_t ptid
)
1453 struct regcache
*regcache
= get_thread_regcache (ptid
);
1454 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1460 struct thread_db_info
*info
;
1462 info
= get_thread_db_info (ptid_get_pid (ptid
));
1464 /* Bail out early if we're not at a thread event breakpoint. */
1465 stop_pc
= regcache_read_pc (regcache
)
1466 - target_decr_pc_after_break (gdbarch
);
1467 if (stop_pc
!= info
->td_create_bp_addr
1468 && stop_pc
!= info
->td_death_bp_addr
)
1471 /* Access an lwp we know is stopped. */
1472 info
->proc_handle
.ptid
= ptid
;
1474 /* If we have only looked at the first thread before libpthread was
1475 initialized, we may not know its thread ID yet. Make sure we do
1476 before we add another thread to the list. */
1477 if (!have_threads (ptid
))
1478 thread_db_find_new_threads_1 (ptid
);
1480 /* If we are at a create breakpoint, we do not know what new lwp
1481 was created and cannot specifically locate the event message for it.
1482 We have to call td_ta_event_getmsg() to get
1483 the latest message. Since we have no way of correlating whether
1484 the event message we get back corresponds to our breakpoint, we must
1485 loop and read all event messages, processing them appropriately.
1486 This guarantees we will process the correct message before continuing
1487 from the breakpoint.
1489 Currently, death events are not enabled. If they are enabled,
1490 the death event can use the td_thr_event_getmsg() interface to
1491 get the message specifically for that lwp and avoid looping
1498 err
= info
->td_ta_event_getmsg_p (info
->thread_agent
, &msg
);
1501 if (err
== TD_NOMSG
)
1504 error (_("Cannot get thread event message: %s"),
1505 thread_db_err_str (err
));
1508 err
= info
->td_thr_get_info_p (msg
.th_p
, &ti
);
1510 error (_("Cannot get thread info: %s"), thread_db_err_str (err
));
1512 ptid
= ptid_build (ptid_get_pid (ptid
), ti
.ti_lid
, 0);
1517 /* Call attach_thread whether or not we already know about a
1518 thread with this thread ID. */
1519 attach_thread (ptid
, msg
.th_p
, &ti
);
1525 if (!in_thread_list (ptid
))
1526 error (_("Spurious thread death event."));
1528 detach_thread (ptid
);
1533 error (_("Spurious thread event."));
1540 thread_db_wait (struct target_ops
*ops
,
1541 ptid_t ptid
, struct target_waitstatus
*ourstatus
,
1544 struct thread_db_info
*info
;
1545 struct target_ops
*beneath
= find_target_beneath (ops
);
1547 ptid
= beneath
->to_wait (beneath
, ptid
, ourstatus
, options
);
1549 if (ourstatus
->kind
== TARGET_WAITKIND_IGNORE
)
1552 if (ourstatus
->kind
== TARGET_WAITKIND_EXITED
1553 || ourstatus
->kind
== TARGET_WAITKIND_SIGNALLED
)
1556 info
= get_thread_db_info (ptid_get_pid (ptid
));
1558 /* If this process isn't using thread_db, we're done. */
1562 if (ourstatus
->kind
== TARGET_WAITKIND_EXECD
)
1564 /* New image, it may or may not end up using thread_db. Assume
1565 not unless we find otherwise. */
1566 delete_thread_db_info (ptid_get_pid (ptid
));
1567 if (!thread_db_list
)
1568 unpush_target (&thread_db_ops
);
1570 /* Thread event breakpoints are deleted by
1571 update_breakpoints_after_exec. */
1576 /* If we do not know about the main thread yet, this would be a good time to
1578 if (ourstatus
->kind
== TARGET_WAITKIND_STOPPED
&& !have_threads (ptid
))
1579 thread_db_find_new_threads_1 (ptid
);
1581 if (ourstatus
->kind
== TARGET_WAITKIND_STOPPED
1582 && ourstatus
->value
.sig
== GDB_SIGNAL_TRAP
)
1583 /* Check for a thread event. */
1586 if (have_threads (ptid
))
1588 /* Fill in the thread's user-level thread id. */
1589 thread_from_lwp (ptid
);
1596 thread_db_mourn_inferior (struct target_ops
*ops
)
1598 struct target_ops
*target_beneath
= find_target_beneath (ops
);
1600 delete_thread_db_info (ptid_get_pid (inferior_ptid
));
1602 target_beneath
->to_mourn_inferior (target_beneath
);
1604 /* Delete the old thread event breakpoints. Do this after mourning
1605 the inferior, so that we don't try to uninsert them. */
1606 remove_thread_event_breakpoints ();
1608 /* Detach thread_db target ops. */
1609 if (!thread_db_list
)
1610 unpush_target (ops
);
1613 struct callback_data
1615 struct thread_db_info
*info
;
1620 find_new_threads_callback (const td_thrhandle_t
*th_p
, void *data
)
1625 struct thread_info
*tp
;
1626 struct callback_data
*cb_data
= data
;
1627 struct thread_db_info
*info
= cb_data
->info
;
1629 err
= info
->td_thr_get_info_p (th_p
, &ti
);
1631 error (_("find_new_threads_callback: cannot get thread info: %s"),
1632 thread_db_err_str (err
));
1634 if (ti
.ti_lid
== -1)
1636 /* A thread with kernel thread ID -1 is either a thread that
1637 exited and was joined, or a thread that is being created but
1638 hasn't started yet, and that is reusing the tcb/stack of a
1639 thread that previously exited and was joined. (glibc marks
1640 terminated and joined threads with kernel thread ID -1. See
1642 if (libthread_db_debug
)
1643 fprintf_unfiltered (gdb_stdlog
,
1644 "thread_db: skipping exited and "
1645 "joined thread (0x%lx)\n", ti
.ti_tid
);
1651 /* A thread ID of zero means that this is the main thread, but
1652 glibc has not yet initialized thread-local storage and the
1653 pthread library. We do not know what the thread's TID will
1654 be yet. Just enable event reporting and otherwise ignore
1657 /* In that case, we're not stopped in a fork syscall and don't
1658 need this glibc bug workaround. */
1659 info
->need_stale_parent_threads_check
= 0;
1661 if (target_has_execution
&& thread_db_use_events ())
1663 err
= info
->td_thr_event_enable_p (th_p
, 1);
1665 error (_("Cannot enable thread event reporting for LWP %d: %s"),
1666 (int) ti
.ti_lid
, thread_db_err_str (err
));
1672 /* Ignore stale parent threads, caused by glibc/BZ5983. This is a
1673 bit expensive, as it needs to open /proc/pid/status, so try to
1674 avoid doing the work if we know we don't have to. */
1675 if (info
->need_stale_parent_threads_check
)
1677 int tgid
= linux_proc_get_tgid (ti
.ti_lid
);
1679 if (tgid
!= -1 && tgid
!= info
->pid
)
1683 ptid
= ptid_build (info
->pid
, ti
.ti_lid
, 0);
1684 tp
= find_thread_ptid (ptid
);
1685 if (tp
== NULL
|| tp
->private == NULL
)
1687 if (attach_thread (ptid
, th_p
, &ti
))
1688 cb_data
->new_threads
+= 1;
1690 /* Problem attaching this thread; perhaps it exited before we
1692 This could mean that the thread list inside glibc itself is in
1693 inconsistent state, and libthread_db could go on looping forever
1694 (observed with glibc-2.3.6). To prevent that, terminate
1695 iteration: thread_db_find_new_threads_2 will retry. */
1698 else if (target_has_execution
&& !thread_db_use_events ())
1700 /* Need to update this if not using the libthread_db events
1701 (particularly, the TD_DEATH event). */
1702 update_thread_state (tp
->private, &ti
);
1708 /* Helper for thread_db_find_new_threads_2.
1709 Returns number of new threads found. */
1712 find_new_threads_once (struct thread_db_info
*info
, int iteration
,
1715 volatile struct gdb_exception except
;
1716 struct callback_data data
;
1717 td_err_e err
= TD_ERR
;
1720 data
.new_threads
= 0;
1722 TRY_CATCH (except
, RETURN_MASK_ERROR
)
1724 /* Iterate over all user-space threads to discover new threads. */
1725 err
= info
->td_ta_thr_iter_p (info
->thread_agent
,
1726 find_new_threads_callback
,
1729 TD_THR_LOWEST_PRIORITY
,
1731 TD_THR_ANY_USER_FLAGS
);
1734 if (libthread_db_debug
)
1736 if (except
.reason
< 0)
1737 exception_fprintf (gdb_stdlog
, except
,
1738 "Warning: find_new_threads_once: ");
1740 fprintf_unfiltered (gdb_stdlog
,
1741 _("Found %d new threads in iteration %d.\n"),
1742 data
.new_threads
, iteration
);
1748 return data
.new_threads
;
1751 /* Search for new threads, accessing memory through stopped thread
1752 PTID. If UNTIL_NO_NEW is true, repeat searching until several
1753 searches in a row do not discover any new threads. */
1756 thread_db_find_new_threads_2 (ptid_t ptid
, int until_no_new
)
1758 td_err_e err
= TD_OK
;
1759 struct thread_db_info
*info
;
1762 info
= get_thread_db_info (ptid_get_pid (ptid
));
1764 /* Access an lwp we know is stopped. */
1765 info
->proc_handle
.ptid
= ptid
;
1769 /* Require 4 successive iterations which do not find any new threads.
1770 The 4 is a heuristic: there is an inherent race here, and I have
1771 seen that 2 iterations in a row are not always sufficient to
1772 "capture" all threads. */
1773 for (i
= 0, loop
= 0; loop
< 4 && err
== TD_OK
; ++i
, ++loop
)
1774 if (find_new_threads_once (info
, i
, &err
) != 0)
1776 /* Found some new threads. Restart the loop from beginning. */
1781 find_new_threads_once (info
, 0, &err
);
1784 error (_("Cannot find new threads: %s"), thread_db_err_str (err
));
1788 thread_db_find_new_threads_1 (ptid_t ptid
)
1790 thread_db_find_new_threads_2 (ptid
, 0);
1794 update_thread_core (struct lwp_info
*info
, void *closure
)
1796 info
->core
= linux_common_core_of_thread (info
->ptid
);
1801 thread_db_update_thread_list (struct target_ops
*ops
)
1803 struct thread_db_info
*info
;
1804 struct inferior
*inf
;
1810 struct thread_info
*thread
;
1815 info
= get_thread_db_info (inf
->pid
);
1819 thread
= any_live_thread_of_process (inf
->pid
);
1820 if (thread
== NULL
|| thread
->executing
)
1823 thread_db_find_new_threads_1 (thread
->ptid
);
1826 if (target_has_execution
)
1827 iterate_over_lwps (minus_one_ptid
/* iterate over all */,
1828 update_thread_core
, NULL
);
1832 thread_db_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
1834 struct thread_info
*thread_info
= find_thread_ptid (ptid
);
1835 struct target_ops
*beneath
;
1837 if (thread_info
!= NULL
&& thread_info
->private != NULL
)
1839 static char buf
[64];
1842 tid
= thread_info
->private->tid
;
1843 snprintf (buf
, sizeof (buf
), "Thread 0x%lx (LWP %ld)",
1844 tid
, ptid_get_lwp (ptid
));
1849 beneath
= find_target_beneath (ops
);
1850 return beneath
->to_pid_to_str (beneath
, ptid
);
1853 /* Return a string describing the state of the thread specified by
1857 thread_db_extra_thread_info (struct target_ops
*self
,
1858 struct thread_info
*info
)
1860 if (info
->private == NULL
)
1863 if (info
->private->dying
)
1869 /* Get the address of the thread local variable in load module LM which
1870 is stored at OFFSET within the thread local storage for thread PTID. */
1873 thread_db_get_thread_local_address (struct target_ops
*ops
,
1878 struct thread_info
*thread_info
;
1879 struct target_ops
*beneath
;
1881 /* If we have not discovered any threads yet, check now. */
1882 if (!have_threads (ptid
))
1883 thread_db_find_new_threads_1 (ptid
);
1885 /* Find the matching thread. */
1886 thread_info
= find_thread_ptid (ptid
);
1888 if (thread_info
!= NULL
&& thread_info
->private != NULL
)
1892 struct thread_db_info
*info
;
1894 info
= get_thread_db_info (ptid_get_pid (ptid
));
1896 /* Finally, get the address of the variable. */
1899 /* glibc doesn't provide the needed interface. */
1900 if (!info
->td_thr_tls_get_addr_p
)
1901 throw_error (TLS_NO_LIBRARY_SUPPORT_ERROR
,
1902 _("No TLS library support"));
1904 /* Note the cast through uintptr_t: this interface only works if
1905 a target address fits in a psaddr_t, which is a host pointer.
1906 So a 32-bit debugger can not access 64-bit TLS through this. */
1907 err
= info
->td_thr_tls_get_addr_p (&thread_info
->private->th
,
1908 (psaddr_t
)(uintptr_t) lm
,
1913 /* If glibc doesn't provide the needed interface throw an error
1914 that LM is zero - normally cases it should not be. */
1915 if (!info
->td_thr_tlsbase_p
)
1916 throw_error (TLS_LOAD_MODULE_NOT_FOUND_ERROR
,
1917 _("TLS load module not found"));
1919 /* This code path handles the case of -static -pthread executables:
1920 https://sourceware.org/ml/libc-help/2014-03/msg00024.html
1921 For older GNU libc r_debug.r_map is NULL. For GNU libc after
1922 PR libc/16831 due to GDB PR threads/16954 LOAD_MODULE is also NULL.
1923 The constant number 1 depends on GNU __libc_setup_tls
1924 initialization of l_tls_modid to 1. */
1925 err
= info
->td_thr_tlsbase_p (&thread_info
->private->th
,
1927 address
= (char *) address
+ offset
;
1930 #ifdef THREAD_DB_HAS_TD_NOTALLOC
1931 /* The memory hasn't been allocated, yet. */
1932 if (err
== TD_NOTALLOC
)
1933 /* Now, if libthread_db provided the initialization image's
1934 address, we *could* try to build a non-lvalue value from
1935 the initialization image. */
1936 throw_error (TLS_NOT_ALLOCATED_YET_ERROR
,
1937 _("TLS not allocated yet"));
1940 /* Something else went wrong. */
1942 throw_error (TLS_GENERIC_ERROR
,
1943 (("%s")), thread_db_err_str (err
));
1945 /* Cast assuming host == target. Joy. */
1946 /* Do proper sign extension for the target. */
1947 gdb_assert (exec_bfd
);
1948 return (bfd_get_sign_extend_vma (exec_bfd
) > 0
1949 ? (CORE_ADDR
) (intptr_t) address
1950 : (CORE_ADDR
) (uintptr_t) address
);
1953 beneath
= find_target_beneath (ops
);
1954 return beneath
->to_get_thread_local_address (beneath
, ptid
, lm
, offset
);
1957 /* Callback routine used to find a thread based on the TID part of
1961 thread_db_find_thread_from_tid (struct thread_info
*thread
, void *data
)
1963 long *tid
= (long *) data
;
1965 if (thread
->private->tid
== *tid
)
1971 /* Implement the to_get_ada_task_ptid target method for this target. */
1974 thread_db_get_ada_task_ptid (struct target_ops
*self
, long lwp
, long thread
)
1976 struct thread_info
*thread_info
;
1978 thread_db_find_new_threads_1 (inferior_ptid
);
1979 thread_info
= iterate_over_threads (thread_db_find_thread_from_tid
, &thread
);
1981 gdb_assert (thread_info
!= NULL
);
1983 return (thread_info
->ptid
);
1987 thread_db_resume (struct target_ops
*ops
,
1988 ptid_t ptid
, int step
, enum gdb_signal signo
)
1990 struct target_ops
*beneath
= find_target_beneath (ops
);
1991 struct thread_db_info
*info
;
1993 if (ptid_equal (ptid
, minus_one_ptid
))
1994 info
= get_thread_db_info (ptid_get_pid (inferior_ptid
));
1996 info
= get_thread_db_info (ptid_get_pid (ptid
));
1998 /* This workaround is only needed for child fork lwps stopped in a
1999 PTRACE_O_TRACEFORK event. When the inferior is resumed, the
2000 workaround can be disabled. */
2002 info
->need_stale_parent_threads_check
= 0;
2004 beneath
->to_resume (beneath
, ptid
, step
, signo
);
2007 /* qsort helper function for info_auto_load_libthread_db, sort the
2008 thread_db_info pointers primarily by their FILENAME and secondarily by their
2009 PID, both in ascending order. */
2012 info_auto_load_libthread_db_compare (const void *ap
, const void *bp
)
2014 struct thread_db_info
*a
= *(struct thread_db_info
**) ap
;
2015 struct thread_db_info
*b
= *(struct thread_db_info
**) bp
;
2018 retval
= strcmp (a
->filename
, b
->filename
);
2022 return (a
->pid
> b
->pid
) - (a
->pid
- b
->pid
);
2025 /* Implement 'info auto-load libthread-db'. */
2028 info_auto_load_libthread_db (char *args
, int from_tty
)
2030 struct ui_out
*uiout
= current_uiout
;
2031 const char *cs
= args
? args
: "";
2032 struct thread_db_info
*info
, **array
;
2033 unsigned info_count
, unique_filenames
;
2034 size_t max_filename_len
, max_pids_len
, pids_len
;
2035 struct cleanup
*back_to
;
2039 cs
= skip_spaces_const (cs
);
2041 error (_("'info auto-load libthread-db' does not accept any parameters"));
2044 for (info
= thread_db_list
; info
; info
= info
->next
)
2045 if (info
->filename
!= NULL
)
2048 array
= xmalloc (sizeof (*array
) * info_count
);
2049 back_to
= make_cleanup (xfree
, array
);
2052 for (info
= thread_db_list
; info
; info
= info
->next
)
2053 if (info
->filename
!= NULL
)
2054 array
[info_count
++] = info
;
2056 /* Sort ARRAY by filenames and PIDs. */
2058 qsort (array
, info_count
, sizeof (*array
),
2059 info_auto_load_libthread_db_compare
);
2061 /* Calculate the number of unique filenames (rows) and the maximum string
2062 length of PIDs list for the unique filenames (columns). */
2064 unique_filenames
= 0;
2065 max_filename_len
= 0;
2068 for (i
= 0; i
< info_count
; i
++)
2070 int pid
= array
[i
]->pid
;
2071 size_t this_pid_len
;
2073 for (this_pid_len
= 0; pid
!= 0; pid
/= 10)
2076 if (i
== 0 || strcmp (array
[i
- 1]->filename
, array
[i
]->filename
) != 0)
2079 max_filename_len
= max (max_filename_len
,
2080 strlen (array
[i
]->filename
));
2084 pids_len
-= strlen (", ");
2085 max_pids_len
= max (max_pids_len
, pids_len
);
2089 pids_len
+= this_pid_len
+ strlen (", ");
2093 pids_len
-= strlen (", ");
2094 max_pids_len
= max (max_pids_len
, pids_len
);
2097 /* Table header shifted right by preceding "libthread-db: " would not match
2099 if (info_count
> 0 && args
== auto_load_info_scripts_pattern_nl
)
2100 ui_out_text (uiout
, "\n");
2102 make_cleanup_ui_out_table_begin_end (uiout
, 2, unique_filenames
,
2103 "LinuxThreadDbTable");
2105 ui_out_table_header (uiout
, max_filename_len
, ui_left
, "filename",
2107 ui_out_table_header (uiout
, pids_len
, ui_left
, "PIDs", "Pids");
2108 ui_out_table_body (uiout
);
2110 pids
= xmalloc (max_pids_len
+ 1);
2111 make_cleanup (xfree
, pids
);
2113 /* Note I is incremented inside the cycle, not at its end. */
2114 for (i
= 0; i
< info_count
;)
2116 struct cleanup
*chain
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
2120 ui_out_field_string (uiout
, "filename", info
->filename
);
2123 while (i
< info_count
&& strcmp (info
->filename
, array
[i
]->filename
) == 0)
2125 if (pids_end
!= pids
)
2130 pids_end
+= xsnprintf (pids_end
, &pids
[max_pids_len
+ 1] - pids_end
,
2131 "%u", array
[i
]->pid
);
2132 gdb_assert (pids_end
< &pids
[max_pids_len
+ 1]);
2138 ui_out_field_string (uiout
, "pids", pids
);
2140 ui_out_text (uiout
, "\n");
2141 do_cleanups (chain
);
2144 do_cleanups (back_to
);
2146 if (info_count
== 0)
2147 ui_out_message (uiout
, 0, _("No auto-loaded libthread-db.\n"));
2151 init_thread_db_ops (void)
2153 thread_db_ops
.to_shortname
= "multi-thread";
2154 thread_db_ops
.to_longname
= "multi-threaded child process.";
2155 thread_db_ops
.to_doc
= "Threads and pthreads support.";
2156 thread_db_ops
.to_detach
= thread_db_detach
;
2157 thread_db_ops
.to_wait
= thread_db_wait
;
2158 thread_db_ops
.to_resume
= thread_db_resume
;
2159 thread_db_ops
.to_mourn_inferior
= thread_db_mourn_inferior
;
2160 thread_db_ops
.to_update_thread_list
= thread_db_update_thread_list
;
2161 thread_db_ops
.to_pid_to_str
= thread_db_pid_to_str
;
2162 thread_db_ops
.to_stratum
= thread_stratum
;
2163 thread_db_ops
.to_has_thread_control
= tc_schedlock
;
2164 thread_db_ops
.to_get_thread_local_address
2165 = thread_db_get_thread_local_address
;
2166 thread_db_ops
.to_extra_thread_info
= thread_db_extra_thread_info
;
2167 thread_db_ops
.to_get_ada_task_ptid
= thread_db_get_ada_task_ptid
;
2168 thread_db_ops
.to_magic
= OPS_MAGIC
;
2170 complete_target_initialization (&thread_db_ops
);
2173 /* Provide a prototype to silence -Wmissing-prototypes. */
2174 extern initialize_file_ftype _initialize_thread_db
;
2177 _initialize_thread_db (void)
2179 init_thread_db_ops ();
2181 /* Defer loading of libthread_db.so until inferior is running.
2182 This allows gdb to load correct libthread_db for a given
2183 executable -- there could be mutiple versions of glibc,
2184 compiled with LinuxThreads or NPTL, and until there is
2185 a running inferior, we can't tell which libthread_db is
2186 the correct one to load. */
2188 libthread_db_search_path
= xstrdup (LIBTHREAD_DB_SEARCH_PATH
);
2190 add_setshow_optional_filename_cmd ("libthread-db-search-path",
2192 &libthread_db_search_path
, _("\
2193 Set search path for libthread_db."), _("\
2194 Show the current search path or libthread_db."), _("\
2195 This path is used to search for libthread_db to be loaded into \
2197 Its value is a colon (':') separate list of directories to search.\n\
2198 Setting the search path to an empty list resets it to its default value."),
2199 set_libthread_db_search_path
,
2201 &setlist
, &showlist
);
2203 add_setshow_zuinteger_cmd ("libthread-db", class_maintenance
,
2204 &libthread_db_debug
, _("\
2205 Set libthread-db debugging."), _("\
2206 Show libthread-db debugging."), _("\
2207 When non-zero, libthread-db debugging is enabled."),
2209 show_libthread_db_debug
,
2210 &setdebuglist
, &showdebuglist
);
2212 add_setshow_boolean_cmd ("libthread-db", class_support
,
2213 &auto_load_thread_db
, _("\
2214 Enable or disable auto-loading of inferior specific libthread_db."), _("\
2215 Show whether auto-loading inferior specific libthread_db is enabled."), _("\
2216 If enabled, libthread_db will be searched in 'set libthread-db-search-path'\n\
2217 locations to load libthread_db compatible with the inferior.\n\
2218 Standard system libthread_db still gets loaded even with this option off.\n\
2219 This options has security implications for untrusted inferiors."),
2220 NULL
, show_auto_load_thread_db
,
2221 auto_load_set_cmdlist_get (),
2222 auto_load_show_cmdlist_get ());
2224 add_cmd ("libthread-db", class_info
, info_auto_load_libthread_db
,
2225 _("Print the list of loaded inferior specific libthread_db.\n\
2226 Usage: info auto-load libthread-db"),
2227 auto_load_info_cmdlist_get ());
2229 /* Add ourselves to objfile event chain. */
2230 observer_attach_new_objfile (thread_db_new_objfile
);
2232 /* Add ourselves to inferior_created event chain.
2233 This is needed to handle debugging statically linked programs where
2234 the new_objfile observer won't get called for libpthread. */
2235 observer_attach_inferior_created (thread_db_inferior_created
);