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"
29 #include "exceptions.h"
30 #include "gdbthread.h"
36 #include "solib-svr4.h"
39 #include "linux-nat.h"
43 #ifdef HAVE_GNU_LIBC_VERSION_H
44 #include <gnu/libc-version.h>
47 #ifndef LIBTHREAD_DB_SO
48 #define LIBTHREAD_DB_SO "libthread_db.so.1"
51 /* GNU/Linux libthread_db support.
53 libthread_db is a library, provided along with libpthread.so, which
54 exposes the internals of the thread library to a debugger. It
55 allows GDB to find existing threads, new threads as they are
56 created, thread IDs (usually, the result of pthread_self), and
57 thread-local variables.
59 The libthread_db interface originates on Solaris, where it is
60 both more powerful and more complicated. This implementation
61 only works for LinuxThreads and NPTL, the two glibc threading
62 libraries. It assumes that each thread is permanently assigned
63 to a single light-weight process (LWP).
65 libthread_db-specific information is stored in the "private" field
66 of struct thread_info. When the field is NULL we do not yet have
67 information about the new thread; this could be temporary (created,
68 but the thread library's data structures do not reflect it yet)
69 or permanent (created using clone instead of pthread_create).
71 Process IDs managed by linux-thread-db.c match those used by
72 linux-nat.c: a common PID for all processes, an LWP ID for each
73 thread, and no TID. We save the TID in private. Keeping it out
74 of the ptid_t prevents thread IDs changing when libpthread is
75 loaded or unloaded. */
77 /* If we're running on GNU/Linux, we must explicitly attach to any new
80 /* This module's target vector. */
81 static struct target_ops thread_db_ops
;
83 /* Non-zero if we're using this module's target vector. */
84 static int using_thread_db
;
86 /* Non-zero if we have determined the signals used by the threads
88 static int thread_signals
;
89 static sigset_t thread_stop_set
;
90 static sigset_t thread_print_set
;
92 /* Structure that identifies the child process for the
93 <proc_service.h> interface. */
94 static struct ps_prochandle proc_handle
;
96 /* Connection to the libthread_db library. */
97 static td_thragent_t
*thread_agent
;
99 /* Pointers to the libthread_db functions. */
101 static td_err_e (*td_init_p
) (void);
103 static td_err_e (*td_ta_new_p
) (struct ps_prochandle
* ps
,
105 static td_err_e (*td_ta_map_id2thr_p
) (const td_thragent_t
*ta
, thread_t pt
,
106 td_thrhandle_t
*__th
);
107 static td_err_e (*td_ta_map_lwp2thr_p
) (const td_thragent_t
*ta
,
108 lwpid_t lwpid
, td_thrhandle_t
*th
);
109 static td_err_e (*td_ta_thr_iter_p
) (const td_thragent_t
*ta
,
110 td_thr_iter_f
*callback
, void *cbdata_p
,
111 td_thr_state_e state
, int ti_pri
,
112 sigset_t
*ti_sigmask_p
,
113 unsigned int ti_user_flags
);
114 static td_err_e (*td_ta_event_addr_p
) (const td_thragent_t
*ta
,
115 td_event_e event
, td_notify_t
*ptr
);
116 static td_err_e (*td_ta_set_event_p
) (const td_thragent_t
*ta
,
117 td_thr_events_t
*event
);
118 static td_err_e (*td_ta_event_getmsg_p
) (const td_thragent_t
*ta
,
119 td_event_msg_t
*msg
);
121 static td_err_e (*td_thr_validate_p
) (const td_thrhandle_t
*th
);
122 static td_err_e (*td_thr_get_info_p
) (const td_thrhandle_t
*th
,
123 td_thrinfo_t
*infop
);
124 static td_err_e (*td_thr_event_enable_p
) (const td_thrhandle_t
*th
,
127 static td_err_e (*td_thr_tls_get_addr_p
) (const td_thrhandle_t
*th
,
129 size_t offset
, void **address
);
131 /* Location of the thread creation event breakpoint. The code at this
132 location in the child process will be called by the pthread library
133 whenever a new thread is created. By setting a special breakpoint
134 at this location, GDB can detect when a new thread is created. We
135 obtain this location via the td_ta_event_addr call. */
136 static CORE_ADDR td_create_bp_addr
;
138 /* Location of the thread death event breakpoint. */
139 static CORE_ADDR td_death_bp_addr
;
141 /* Prototypes for local functions. */
142 static void thread_db_find_new_threads_1 (void);
143 static void attach_thread (ptid_t ptid
, const td_thrhandle_t
*th_p
,
144 const td_thrinfo_t
*ti_p
);
145 static void detach_thread (ptid_t ptid
);
148 /* Use "struct private_thread_info" to cache thread state. This is
149 a substantial optimization. */
151 struct private_thread_info
153 /* Flag set when we see a TD_DEATH event for this thread. */
154 unsigned int dying
:1;
156 /* Cached thread state. */
163 thread_db_err_str (td_err_e err
)
170 return "generic 'call succeeded'";
172 return "generic error";
174 return "no thread to satisfy query";
176 return "no sync handle to satisfy query";
178 return "no LWP to satisfy query";
180 return "invalid process handle";
182 return "invalid thread handle";
184 return "invalid synchronization handle";
186 return "invalid thread agent";
188 return "invalid key";
190 return "no event message for getmsg";
192 return "FPU register set not available";
194 return "application not linked with libthread";
196 return "requested event is not supported";
198 return "capability not available";
200 return "debugger service failed";
202 return "operation not applicable to";
204 return "no thread-specific data for this thread";
206 return "malloc failed";
208 return "only part of register set was written/read";
210 return "X register set not available for this thread";
211 #ifdef THREAD_DB_HAS_TD_NOTALLOC
213 return "thread has not yet allocated TLS for given module";
215 #ifdef THREAD_DB_HAS_TD_VERSION
217 return "versions of libpthread and libthread_db do not match";
219 #ifdef THREAD_DB_HAS_TD_NOTLS
221 return "there is no TLS segment in the given module";
224 snprintf (buf
, sizeof (buf
), "unknown thread_db error '%d'", err
);
229 /* Return 1 if any threads have been registered. There may be none if
230 the threading library is not fully initialized yet. */
233 have_threads_callback (struct thread_info
*thread
, void *dummy
)
235 return thread
->private != NULL
;
241 return iterate_over_threads (have_threads_callback
, NULL
) != NULL
;
244 /* A callback function for td_ta_thr_iter, which we use to map all
247 THP is a handle to the current thread; if INFOP is not NULL, the
248 struct thread_info associated with this thread is returned in
251 If the thread is a zombie, TD_THR_ZOMBIE is returned. Otherwise,
252 zero is returned to indicate success. */
255 thread_get_info_callback (const td_thrhandle_t
*thp
, void *infop
)
259 struct thread_info
*thread_info
;
262 err
= td_thr_get_info_p (thp
, &ti
);
264 error (_("thread_get_info_callback: cannot get thread info: %s"),
265 thread_db_err_str (err
));
267 /* Fill the cache. */
268 thread_ptid
= ptid_build (GET_PID (proc_handle
.ptid
), ti
.ti_lid
, 0);
269 thread_info
= find_thread_pid (thread_ptid
);
271 /* In the case of a zombie thread, don't continue. We don't want to
272 attach to it thinking it is a new thread. */
273 if (ti
.ti_state
== TD_THR_UNKNOWN
|| ti
.ti_state
== TD_THR_ZOMBIE
)
276 *(struct thread_info
**) infop
= thread_info
;
277 return TD_THR_ZOMBIE
;
280 if (thread_info
== NULL
)
282 /* New thread. Attach to it now (why wait?). */
283 if (!have_threads ())
284 thread_db_find_new_threads_1 ();
286 attach_thread (thread_ptid
, thp
, &ti
);
287 thread_info
= find_thread_pid (thread_ptid
);
288 gdb_assert (thread_info
!= NULL
);
292 *(struct thread_info
**) infop
= thread_info
;
297 /* Convert between user-level thread ids and LWP ids. */
300 thread_from_lwp (ptid_t ptid
)
304 struct thread_info
*thread_info
;
307 /* This ptid comes from linux-nat.c, which should always fill in the
309 gdb_assert (GET_LWP (ptid
) != 0);
311 /* Access an lwp we know is stopped. */
312 proc_handle
.ptid
= ptid
;
313 err
= td_ta_map_lwp2thr_p (thread_agent
, GET_LWP (ptid
), &th
);
315 error (_("Cannot find user-level thread for LWP %ld: %s"),
316 GET_LWP (ptid
), thread_db_err_str (err
));
320 /* Fetch the thread info. If we get back TD_THR_ZOMBIE, then the
321 event thread has already died. If another gdb interface has called
322 thread_alive() previously, the thread won't be found on the thread list
323 anymore. In that case, we don't want to process this ptid anymore
324 to avoid the possibility of later treating it as a newly
325 discovered thread id that we should add to the list. Thus,
326 we return a -1 ptid which is also how the thread list marks a
328 if (thread_get_info_callback (&th
, &thread_info
) == TD_THR_ZOMBIE
329 && thread_info
== NULL
)
330 return pid_to_ptid (-1);
332 gdb_assert (ptid_get_tid (ptid
) == 0);
337 /* Attach to lwp PTID, doing whatever else is required to have this
338 LWP under the debugger's control --- e.g., enabling event
339 reporting. Returns true on success. */
341 thread_db_attach_lwp (ptid_t ptid
)
347 if (!using_thread_db
)
350 /* This ptid comes from linux-nat.c, which should always fill in the
352 gdb_assert (GET_LWP (ptid
) != 0);
354 /* Access an lwp we know is stopped. */
355 proc_handle
.ptid
= ptid
;
357 /* If we have only looked at the first thread before libpthread was
358 initialized, we may not know its thread ID yet. Make sure we do
359 before we add another thread to the list. */
360 if (!have_threads ())
361 thread_db_find_new_threads_1 ();
363 err
= td_ta_map_lwp2thr_p (thread_agent
, GET_LWP (ptid
), &th
);
365 /* Cannot find user-level thread. */
368 err
= td_thr_get_info_p (&th
, &ti
);
371 warning (_("Cannot get thread info: %s"), thread_db_err_str (err
));
375 attach_thread (ptid
, &th
, &ti
);
380 verbose_dlsym (void *handle
, const char *name
)
382 void *sym
= dlsym (handle
, name
);
384 warning (_("Symbol \"%s\" not found in libthread_db: %s"), name
, dlerror ());
389 thread_db_load (void)
394 handle
= dlopen (LIBTHREAD_DB_SO
, RTLD_NOW
);
397 fprintf_filtered (gdb_stderr
, "\n\ndlopen failed on '%s' - %s\n",
398 LIBTHREAD_DB_SO
, dlerror ());
399 fprintf_filtered (gdb_stderr
,
400 "GDB will not be able to debug pthreads.\n\n");
404 /* Initialize pointers to the dynamic library functions we will use.
405 Essential functions first. */
407 td_init_p
= verbose_dlsym (handle
, "td_init");
408 if (td_init_p
== NULL
)
411 td_ta_new_p
= verbose_dlsym (handle
, "td_ta_new");
412 if (td_ta_new_p
== NULL
)
415 td_ta_map_id2thr_p
= verbose_dlsym (handle
, "td_ta_map_id2thr");
416 if (td_ta_map_id2thr_p
== NULL
)
419 td_ta_map_lwp2thr_p
= verbose_dlsym (handle
, "td_ta_map_lwp2thr");
420 if (td_ta_map_lwp2thr_p
== NULL
)
423 td_ta_thr_iter_p
= verbose_dlsym (handle
, "td_ta_thr_iter");
424 if (td_ta_thr_iter_p
== NULL
)
427 td_thr_validate_p
= verbose_dlsym (handle
, "td_thr_validate");
428 if (td_thr_validate_p
== NULL
)
431 td_thr_get_info_p
= verbose_dlsym (handle
, "td_thr_get_info");
432 if (td_thr_get_info_p
== NULL
)
435 /* Initialize the library. */
439 warning (_("Cannot initialize libthread_db: %s"), thread_db_err_str (err
));
443 /* These are not essential. */
444 td_ta_event_addr_p
= dlsym (handle
, "td_ta_event_addr");
445 td_ta_set_event_p
= dlsym (handle
, "td_ta_set_event");
446 td_ta_event_getmsg_p
= dlsym (handle
, "td_ta_event_getmsg");
447 td_thr_event_enable_p
= dlsym (handle
, "td_thr_event_enable");
448 td_thr_tls_get_addr_p
= dlsym (handle
, "td_thr_tls_get_addr");
454 enable_thread_event (td_thragent_t
*thread_agent
, int event
, CORE_ADDR
*bp
)
459 /* Access an lwp we know is stopped. */
460 proc_handle
.ptid
= inferior_ptid
;
462 /* Get the breakpoint address for thread EVENT. */
463 err
= td_ta_event_addr_p (thread_agent
, event
, ¬ify
);
467 /* Set up the breakpoint. */
468 gdb_assert (exec_bfd
);
469 (*bp
) = (gdbarch_convert_from_func_ptr_addr
471 /* Do proper sign extension for the target. */
472 (bfd_get_sign_extend_vma (exec_bfd
) > 0
473 ? (CORE_ADDR
) (intptr_t) notify
.u
.bptaddr
474 : (CORE_ADDR
) (uintptr_t) notify
.u
.bptaddr
),
476 create_thread_event_breakpoint ((*bp
));
482 enable_thread_event_reporting (void)
484 td_thr_events_t events
;
487 #ifdef HAVE_GNU_LIBC_VERSION_H
488 const char *libc_version
;
489 int libc_major
, libc_minor
;
492 /* We cannot use the thread event reporting facility if these
493 functions aren't available. */
494 if (td_ta_event_addr_p
== NULL
|| td_ta_set_event_p
== NULL
495 || td_ta_event_getmsg_p
== NULL
|| td_thr_event_enable_p
== NULL
)
498 /* Set the process wide mask saying which events we're interested in. */
499 td_event_emptyset (&events
);
500 td_event_addset (&events
, TD_CREATE
);
502 #ifdef HAVE_GNU_LIBC_VERSION_H
503 /* The event reporting facility is broken for TD_DEATH events in
504 glibc 2.1.3, so don't enable it if we have glibc but a lower
506 libc_version
= gnu_get_libc_version ();
507 if (sscanf (libc_version
, "%d.%d", &libc_major
, &libc_minor
) == 2
508 && (libc_major
> 2 || (libc_major
== 2 && libc_minor
> 1)))
510 td_event_addset (&events
, TD_DEATH
);
512 err
= td_ta_set_event_p (thread_agent
, &events
);
515 warning (_("Unable to set global thread event mask: %s"),
516 thread_db_err_str (err
));
520 /* Delete previous thread event breakpoints, if any. */
521 remove_thread_event_breakpoints ();
522 td_create_bp_addr
= 0;
523 td_death_bp_addr
= 0;
525 /* Set up the thread creation event. */
526 err
= enable_thread_event (thread_agent
, TD_CREATE
, &td_create_bp_addr
);
529 warning (_("Unable to get location for thread creation breakpoint: %s"),
530 thread_db_err_str (err
));
534 /* Set up the thread death event. */
535 err
= enable_thread_event (thread_agent
, TD_DEATH
, &td_death_bp_addr
);
538 warning (_("Unable to get location for thread death breakpoint: %s"),
539 thread_db_err_str (err
));
545 disable_thread_event_reporting (void)
547 td_thr_events_t events
;
549 /* Set the process wide mask saying we aren't interested in any
551 td_event_emptyset (&events
);
552 td_ta_set_event_p (thread_agent
, &events
);
554 /* Delete thread event breakpoints, if any. */
555 remove_thread_event_breakpoints ();
556 td_create_bp_addr
= 0;
557 td_death_bp_addr
= 0;
561 check_thread_signals (void)
563 #ifdef GET_THREAD_SIGNALS
569 GET_THREAD_SIGNALS (&mask
);
570 sigemptyset (&thread_stop_set
);
571 sigemptyset (&thread_print_set
);
573 for (i
= 1; i
< NSIG
; i
++)
575 if (sigismember (&mask
, i
))
577 if (signal_stop_update (target_signal_from_host (i
), 0))
578 sigaddset (&thread_stop_set
, i
);
579 if (signal_print_update (target_signal_from_host (i
), 0))
580 sigaddset (&thread_print_set
, i
);
588 /* Check whether thread_db is usable. This function is called when
589 an inferior is created (or otherwise acquired, e.g. attached to)
590 and when new shared libraries are loaded into a running process. */
593 check_for_thread_db (void)
596 static int already_loaded
;
598 /* Do nothing if we couldn't load libthread_db.so.1. */
599 if (td_ta_new_p
== NULL
)
602 /* First time through, report that libthread_db was successfuly
603 loaded. Can't print this in in thread_db_load as, at that stage,
604 the interpreter and it's console haven't started. */
609 const char *library
= NULL
;
610 if (dladdr ((*td_ta_new_p
), &info
) != 0)
611 library
= info
.dli_fname
;
616 /* Paranoid - don't let a NULL path slip through. */
617 library
= LIBTHREAD_DB_SO
;
620 printf_unfiltered (_("Using host libthread_db library \"%s\".\n"),
626 /* Nothing to do. The thread library was already detected and the
627 target vector was already activated. */
630 /* Don't attempt to use thread_db on targets which can not run
631 (executables not running yet, core files) for now. */
632 if (!target_has_execution
)
635 /* Don't attempt to use thread_db for remote targets. */
636 if (!target_can_run (¤t_target
))
639 /* Initialize the structure that identifies the child process. */
640 proc_handle
.ptid
= inferior_ptid
;
642 /* Now attempt to open a connection to the thread library. */
643 err
= td_ta_new_p (&proc_handle
, &thread_agent
);
647 /* No thread library was detected. */
651 printf_unfiltered (_("[Thread debugging using libthread_db enabled]\n"));
653 /* The thread library was detected. Activate the thread_db target. */
654 push_target (&thread_db_ops
);
657 enable_thread_event_reporting ();
658 thread_db_find_new_threads_1 ();
662 warning (_("Cannot initialize thread debugging library: %s"),
663 thread_db_err_str (err
));
669 thread_db_new_objfile (struct objfile
*objfile
)
672 check_for_thread_db ();
675 /* Attach to a new thread. This function is called when we receive a
676 TD_CREATE event or when we iterate over all threads and find one
677 that wasn't already in our list. */
680 attach_thread (ptid_t ptid
, const td_thrhandle_t
*th_p
,
681 const td_thrinfo_t
*ti_p
)
683 struct private_thread_info
*private;
684 struct thread_info
*tp
= NULL
;
687 /* If we're being called after a TD_CREATE event, we may already
688 know about this thread. There are two ways this can happen. We
689 may have iterated over all threads between the thread creation
690 and the TD_CREATE event, for instance when the user has issued
691 the `info threads' command before the SIGTRAP for hitting the
692 thread creation breakpoint was reported. Alternatively, the
693 thread may have exited and a new one been created with the same
694 thread ID. In the first case we don't need to do anything; in
695 the second case we should discard information about the dead
696 thread and attach to the new one. */
697 if (in_thread_list (ptid
))
699 tp
= find_thread_pid (ptid
);
700 gdb_assert (tp
!= NULL
);
702 /* If tp->private is NULL, then GDB is already attached to this
703 thread, but we do not know anything about it. We can learn
704 about it here. This can only happen if we have some other
705 way besides libthread_db to notice new threads (i.e.
706 PTRACE_EVENT_CLONE); assume the same mechanism notices thread
707 exit, so this can not be a stale thread recreated with the
709 if (tp
->private != NULL
)
711 if (!tp
->private->dying
)
714 delete_thread (ptid
);
719 check_thread_signals ();
721 if (ti_p
->ti_state
== TD_THR_UNKNOWN
|| ti_p
->ti_state
== TD_THR_ZOMBIE
)
722 return; /* A zombie thread -- do not attach. */
724 /* Under GNU/Linux, we have to attach to each and every thread. */
726 && lin_lwp_attach_lwp (BUILD_LWP (ti_p
->ti_lid
, GET_PID (ptid
))) < 0)
729 /* Construct the thread's private data. */
730 private = xmalloc (sizeof (struct private_thread_info
));
731 memset (private, 0, sizeof (struct private_thread_info
));
733 /* A thread ID of zero may mean the thread library has not initialized
734 yet. But we shouldn't even get here if that's the case. FIXME:
735 if we change GDB to always have at least one thread in the thread
736 list this will have to go somewhere else; maybe private == NULL
737 until the thread_db target claims it. */
738 gdb_assert (ti_p
->ti_tid
!= 0);
740 private->tid
= ti_p
->ti_tid
;
742 /* Add the thread to GDB's thread list. */
744 tp
= add_thread_with_info (ptid
, private);
746 tp
->private = private;
748 /* Enable thread event reporting for this thread. */
749 err
= td_thr_event_enable_p (th_p
, 1);
751 error (_("Cannot enable thread event reporting for %s: %s"),
752 target_pid_to_str (ptid
), thread_db_err_str (err
));
756 detach_thread (ptid_t ptid
)
758 struct thread_info
*thread_info
;
760 /* Don't delete the thread now, because it still reports as active
761 until it has executed a few instructions after the event
762 breakpoint - if we deleted it now, "info threads" would cause us
763 to re-attach to it. Just mark it as having had a TD_DEATH
764 event. This means that we won't delete it from our thread list
765 until we notice that it's dead (via prune_threads), or until
766 something re-uses its thread ID. We'll report the thread exit
767 when the underlying LWP dies. */
768 thread_info
= find_thread_pid (ptid
);
769 gdb_assert (thread_info
!= NULL
&& thread_info
->private != NULL
);
770 thread_info
->private->dying
= 1;
774 thread_db_detach (struct target_ops
*ops
, char *args
, int from_tty
)
776 struct target_ops
*target_beneath
= find_target_beneath (ops
);
778 disable_thread_event_reporting ();
780 /* Forget about the child's process ID. We shouldn't need it
782 proc_handle
.ptid
= null_ptid
;
784 /* Detach thread_db target ops. */
785 unpush_target (&thread_db_ops
);
788 target_beneath
->to_detach (target_beneath
, args
, from_tty
);
791 /* Check if PID is currently stopped at the location of a thread event
792 breakpoint location. If it is, read the event message and act upon
796 check_event (ptid_t ptid
)
798 struct regcache
*regcache
= get_thread_regcache (ptid
);
799 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
806 /* Bail out early if we're not at a thread event breakpoint. */
807 stop_pc
= regcache_read_pc (regcache
)
808 - gdbarch_decr_pc_after_break (gdbarch
);
809 if (stop_pc
!= td_create_bp_addr
&& stop_pc
!= td_death_bp_addr
)
812 /* Access an lwp we know is stopped. */
813 proc_handle
.ptid
= ptid
;
815 /* If we have only looked at the first thread before libpthread was
816 initialized, we may not know its thread ID yet. Make sure we do
817 before we add another thread to the list. */
818 if (!have_threads ())
819 thread_db_find_new_threads_1 ();
821 /* If we are at a create breakpoint, we do not know what new lwp
822 was created and cannot specifically locate the event message for it.
823 We have to call td_ta_event_getmsg() to get
824 the latest message. Since we have no way of correlating whether
825 the event message we get back corresponds to our breakpoint, we must
826 loop and read all event messages, processing them appropriately.
827 This guarantees we will process the correct message before continuing
830 Currently, death events are not enabled. If they are enabled,
831 the death event can use the td_thr_event_getmsg() interface to
832 get the message specifically for that lwp and avoid looping
839 err
= td_ta_event_getmsg_p (thread_agent
, &msg
);
845 error (_("Cannot get thread event message: %s"),
846 thread_db_err_str (err
));
849 err
= td_thr_get_info_p (msg
.th_p
, &ti
);
851 error (_("Cannot get thread info: %s"), thread_db_err_str (err
));
853 ptid
= ptid_build (GET_PID (ptid
), ti
.ti_lid
, 0);
858 /* Call attach_thread whether or not we already know about a
859 thread with this thread ID. */
860 attach_thread (ptid
, msg
.th_p
, &ti
);
866 if (!in_thread_list (ptid
))
867 error (_("Spurious thread death event."));
869 detach_thread (ptid
);
874 error (_("Spurious thread event."));
881 thread_db_wait (struct target_ops
*ops
,
882 ptid_t ptid
, struct target_waitstatus
*ourstatus
)
884 struct target_ops
*beneath
= find_target_beneath (ops
);
886 ptid
= beneath
->to_wait (beneath
, ptid
, ourstatus
);
888 if (ourstatus
->kind
== TARGET_WAITKIND_IGNORE
)
891 if (ourstatus
->kind
== TARGET_WAITKIND_EXITED
892 || ourstatus
->kind
== TARGET_WAITKIND_SIGNALLED
)
895 if (ourstatus
->kind
== TARGET_WAITKIND_EXECD
)
897 remove_thread_event_breakpoints ();
898 unpush_target (&thread_db_ops
);
904 /* If we do not know about the main thread yet, this would be a good time to
906 if (ourstatus
->kind
== TARGET_WAITKIND_STOPPED
&& !have_threads ())
907 thread_db_find_new_threads_1 ();
909 if (ourstatus
->kind
== TARGET_WAITKIND_STOPPED
910 && ourstatus
->value
.sig
== TARGET_SIGNAL_TRAP
)
911 /* Check for a thread event. */
916 /* Change ptids back into the higher level PID + TID format. If
917 the thread is dead and no longer on the thread list, we will
918 get back a dead ptid. This can occur if the thread death
919 event gets postponed by other simultaneous events. In such a
920 case, we want to just ignore the event and continue on. */
922 ptid
= thread_from_lwp (ptid
);
923 if (GET_PID (ptid
) == -1)
924 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
931 thread_db_mourn_inferior (struct target_ops
*ops
)
933 struct target_ops
*target_beneath
= find_target_beneath (ops
);
935 /* Forget about the child's process ID. We shouldn't need it
937 proc_handle
.ptid
= null_ptid
;
939 target_beneath
->to_mourn_inferior (target_beneath
);
941 /* Delete the old thread event breakpoints. Do this after mourning
942 the inferior, so that we don't try to uninsert them. */
943 remove_thread_event_breakpoints ();
945 /* Detach thread_db target ops. */
951 find_new_threads_callback (const td_thrhandle_t
*th_p
, void *data
)
956 struct thread_info
*tp
;
958 err
= td_thr_get_info_p (th_p
, &ti
);
960 error (_("find_new_threads_callback: cannot get thread info: %s"),
961 thread_db_err_str (err
));
963 if (ti
.ti_state
== TD_THR_UNKNOWN
|| ti
.ti_state
== TD_THR_ZOMBIE
)
964 return 0; /* A zombie -- ignore. */
966 ptid
= ptid_build (GET_PID (proc_handle
.ptid
), ti
.ti_lid
, 0);
970 /* A thread ID of zero means that this is the main thread, but
971 glibc has not yet initialized thread-local storage and the
972 pthread library. We do not know what the thread's TID will
973 be yet. Just enable event reporting and otherwise ignore
976 err
= td_thr_event_enable_p (th_p
, 1);
978 error (_("Cannot enable thread event reporting for %s: %s"),
979 target_pid_to_str (ptid
), thread_db_err_str (err
));
984 tp
= find_thread_pid (ptid
);
985 if (tp
== NULL
|| tp
->private == NULL
)
986 attach_thread (ptid
, th_p
, &ti
);
991 /* Search for new threads, accessing memory through stopped thread
995 thread_db_find_new_threads_1 (void)
1001 /* In linux, we can only read memory through a stopped lwp. */
1007 /* There is no stopped thread. Bail out. */
1010 /* Access an lwp we know is stopped. */
1011 proc_handle
.ptid
= ptid
;
1012 /* Iterate over all user-space threads to discover new threads. */
1013 err
= td_ta_thr_iter_p (thread_agent
, find_new_threads_callback
, NULL
,
1014 TD_THR_ANY_STATE
, TD_THR_LOWEST_PRIORITY
,
1015 TD_SIGNO_MASK
, TD_THR_ANY_USER_FLAGS
);
1017 error (_("Cannot find new threads: %s"), thread_db_err_str (err
));
1021 thread_db_find_new_threads (struct target_ops
*ops
)
1023 thread_db_find_new_threads_1 ();
1027 thread_db_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
1029 struct thread_info
*thread_info
= find_thread_pid (ptid
);
1030 struct target_ops
*beneath
;
1032 if (thread_info
!= NULL
&& thread_info
->private != NULL
)
1034 static char buf
[64];
1037 tid
= thread_info
->private->tid
;
1038 thread_info
= find_thread_pid (ptid
);
1039 snprintf (buf
, sizeof (buf
), "Thread 0x%lx (LWP %ld)",
1040 tid
, GET_LWP (ptid
));
1045 beneath
= find_target_beneath (ops
);
1046 if (beneath
->to_pid_to_str (beneath
, ptid
))
1047 return beneath
->to_pid_to_str (beneath
, ptid
);
1049 return normal_pid_to_str (ptid
);
1052 /* Return a string describing the state of the thread specified by
1056 thread_db_extra_thread_info (struct thread_info
*info
)
1058 if (info
->private == NULL
)
1061 if (info
->private->dying
)
1067 /* Get the address of the thread local variable in load module LM which
1068 is stored at OFFSET within the thread local storage for thread PTID. */
1071 thread_db_get_thread_local_address (struct target_ops
*ops
,
1076 struct thread_info
*thread_info
;
1077 struct target_ops
*beneath
;
1079 /* If we have not discovered any threads yet, check now. */
1080 if (!have_threads ())
1081 thread_db_find_new_threads_1 ();
1083 /* Find the matching thread. */
1084 thread_info
= find_thread_pid (ptid
);
1086 if (thread_info
!= NULL
&& thread_info
->private != NULL
)
1091 /* glibc doesn't provide the needed interface. */
1092 if (!td_thr_tls_get_addr_p
)
1093 throw_error (TLS_NO_LIBRARY_SUPPORT_ERROR
,
1094 _("No TLS library support"));
1096 /* Caller should have verified that lm != 0. */
1097 gdb_assert (lm
!= 0);
1099 /* Finally, get the address of the variable. */
1100 err
= td_thr_tls_get_addr_p (&thread_info
->private->th
,
1101 (void *)(size_t) lm
,
1104 #ifdef THREAD_DB_HAS_TD_NOTALLOC
1105 /* The memory hasn't been allocated, yet. */
1106 if (err
== TD_NOTALLOC
)
1107 /* Now, if libthread_db provided the initialization image's
1108 address, we *could* try to build a non-lvalue value from
1109 the initialization image. */
1110 throw_error (TLS_NOT_ALLOCATED_YET_ERROR
,
1111 _("TLS not allocated yet"));
1114 /* Something else went wrong. */
1116 throw_error (TLS_GENERIC_ERROR
,
1117 (("%s")), thread_db_err_str (err
));
1119 /* Cast assuming host == target. Joy. */
1120 /* Do proper sign extension for the target. */
1121 gdb_assert (exec_bfd
);
1122 return (bfd_get_sign_extend_vma (exec_bfd
) > 0
1123 ? (CORE_ADDR
) (intptr_t) address
1124 : (CORE_ADDR
) (uintptr_t) address
);
1127 beneath
= find_target_beneath (ops
);
1128 if (beneath
->to_get_thread_local_address
)
1129 return beneath
->to_get_thread_local_address (beneath
, ptid
, lm
, offset
);
1131 throw_error (TLS_GENERIC_ERROR
,
1132 _("TLS not supported on this target"));
1135 /* Callback routine used to find a thread based on the TID part of
1139 thread_db_find_thread_from_tid (struct thread_info
*thread
, void *data
)
1141 long *tid
= (long *) data
;
1143 if (thread
->private->tid
== *tid
)
1149 /* Implement the to_get_ada_task_ptid target method for this target. */
1152 thread_db_get_ada_task_ptid (long lwp
, long thread
)
1154 struct thread_info
*thread_info
;
1156 thread_db_find_new_threads_1 ();
1157 thread_info
= iterate_over_threads (thread_db_find_thread_from_tid
, &thread
);
1159 gdb_assert (thread_info
!= NULL
);
1161 return (thread_info
->ptid
);
1165 init_thread_db_ops (void)
1167 thread_db_ops
.to_shortname
= "multi-thread";
1168 thread_db_ops
.to_longname
= "multi-threaded child process.";
1169 thread_db_ops
.to_doc
= "Threads and pthreads support.";
1170 thread_db_ops
.to_detach
= thread_db_detach
;
1171 thread_db_ops
.to_wait
= thread_db_wait
;
1172 thread_db_ops
.to_mourn_inferior
= thread_db_mourn_inferior
;
1173 thread_db_ops
.to_find_new_threads
= thread_db_find_new_threads
;
1174 thread_db_ops
.to_pid_to_str
= thread_db_pid_to_str
;
1175 thread_db_ops
.to_stratum
= thread_stratum
;
1176 thread_db_ops
.to_has_thread_control
= tc_schedlock
;
1177 thread_db_ops
.to_get_thread_local_address
1178 = thread_db_get_thread_local_address
;
1179 thread_db_ops
.to_extra_thread_info
= thread_db_extra_thread_info
;
1180 thread_db_ops
.to_get_ada_task_ptid
= thread_db_get_ada_task_ptid
;
1181 thread_db_ops
.to_magic
= OPS_MAGIC
;
1184 /* Provide a prototype to silence -Wmissing-prototypes. */
1185 extern initialize_file_ftype _initialize_thread_db
;
1188 _initialize_thread_db (void)
1190 /* Only initialize the module if we can load libthread_db. */
1191 if (thread_db_load ())
1193 init_thread_db_ops ();
1194 add_target (&thread_db_ops
);
1196 /* Add ourselves to objfile event chain. */
1197 observer_attach_new_objfile (thread_db_new_objfile
);