1 /* libthread_db assisted debugging support, generic parts.
3 Copyright (C) 1999, 2000, 2001, 2003, 2004, 2005, 2006, 2007
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 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 /* If we're running on GNU/Linux, we must explicitly attach to any new
54 /* This module's target vector. */
55 static struct target_ops thread_db_ops
;
57 /* The target vector that we call for things this module can't handle. */
58 static struct target_ops
*target_beneath
;
60 /* Non-zero if we're using this module's target vector. */
61 static int using_thread_db
;
63 /* Non-zero if we have determined the signals used by the threads
65 static int thread_signals
;
66 static sigset_t thread_stop_set
;
67 static sigset_t thread_print_set
;
69 /* Structure that identifies the child process for the
70 <proc_service.h> interface. */
71 static struct ps_prochandle proc_handle
;
73 /* Connection to the libthread_db library. */
74 static td_thragent_t
*thread_agent
;
76 /* Pointers to the libthread_db functions. */
78 static td_err_e (*td_init_p
) (void);
80 static td_err_e (*td_ta_new_p
) (struct ps_prochandle
* ps
,
82 static td_err_e (*td_ta_map_id2thr_p
) (const td_thragent_t
*ta
, thread_t pt
,
83 td_thrhandle_t
*__th
);
84 static td_err_e (*td_ta_map_lwp2thr_p
) (const td_thragent_t
*ta
,
85 lwpid_t lwpid
, td_thrhandle_t
*th
);
86 static td_err_e (*td_ta_thr_iter_p
) (const td_thragent_t
*ta
,
87 td_thr_iter_f
*callback
, void *cbdata_p
,
88 td_thr_state_e state
, int ti_pri
,
89 sigset_t
*ti_sigmask_p
,
90 unsigned int ti_user_flags
);
91 static td_err_e (*td_ta_event_addr_p
) (const td_thragent_t
*ta
,
92 td_event_e event
, td_notify_t
*ptr
);
93 static td_err_e (*td_ta_set_event_p
) (const td_thragent_t
*ta
,
94 td_thr_events_t
*event
);
95 static td_err_e (*td_ta_event_getmsg_p
) (const td_thragent_t
*ta
,
98 static td_err_e (*td_thr_validate_p
) (const td_thrhandle_t
*th
);
99 static td_err_e (*td_thr_get_info_p
) (const td_thrhandle_t
*th
,
100 td_thrinfo_t
*infop
);
101 static td_err_e (*td_thr_event_enable_p
) (const td_thrhandle_t
*th
,
104 static td_err_e (*td_thr_tls_get_addr_p
) (const td_thrhandle_t
*th
,
106 size_t offset
, void **address
);
108 /* Location of the thread creation event breakpoint. The code at this
109 location in the child process will be called by the pthread library
110 whenever a new thread is created. By setting a special breakpoint
111 at this location, GDB can detect when a new thread is created. We
112 obtain this location via the td_ta_event_addr call. */
113 static CORE_ADDR td_create_bp_addr
;
115 /* Location of the thread death event breakpoint. */
116 static CORE_ADDR td_death_bp_addr
;
118 /* Prototypes for local functions. */
119 static void thread_db_find_new_threads (void);
120 static void attach_thread (ptid_t ptid
, const td_thrhandle_t
*th_p
,
121 const td_thrinfo_t
*ti_p
, int verbose
);
122 static void detach_thread (ptid_t ptid
, int verbose
);
125 /* Building process ids. */
127 #define GET_PID(ptid) ptid_get_pid (ptid)
128 #define GET_LWP(ptid) ptid_get_lwp (ptid)
129 #define GET_THREAD(ptid) ptid_get_tid (ptid)
131 #define is_lwp(ptid) (GET_LWP (ptid) != 0)
132 #define is_thread(ptid) (GET_THREAD (ptid) != 0)
134 #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
137 /* Use "struct private_thread_info" to cache thread state. This is
138 a substantial optimization. */
140 struct private_thread_info
142 /* Flag set when we see a TD_DEATH event for this thread. */
143 unsigned int dying
:1;
145 /* Cached thread state. */
146 unsigned int th_valid
:1;
147 unsigned int ti_valid
:1;
155 thread_db_err_str (td_err_e err
)
162 return "generic 'call succeeded'";
164 return "generic error";
166 return "no thread to satisfy query";
168 return "no sync handle to satisfy query";
170 return "no LWP to satisfy query";
172 return "invalid process handle";
174 return "invalid thread handle";
176 return "invalid synchronization handle";
178 return "invalid thread agent";
180 return "invalid key";
182 return "no event message for getmsg";
184 return "FPU register set not available";
186 return "application not linked with libthread";
188 return "requested event is not supported";
190 return "capability not available";
192 return "debugger service failed";
194 return "operation not applicable to";
196 return "no thread-specific data for this thread";
198 return "malloc failed";
200 return "only part of register set was written/read";
202 return "X register set not available for this thread";
203 #ifdef THREAD_DB_HAS_TD_NOTALLOC
205 return "thread has not yet allocated TLS for given module";
207 #ifdef THREAD_DB_HAS_TD_VERSION
209 return "versions of libpthread and libthread_db do not match";
211 #ifdef THREAD_DB_HAS_TD_NOTLS
213 return "there is no TLS segment in the given module";
216 snprintf (buf
, sizeof (buf
), "unknown thread_db error '%d'", err
);
221 /* Return 1 if any threads have been registered. There may be none if
222 the threading library is not fully initialized yet. */
225 have_threads_callback (struct thread_info
*thread
, void *dummy
)
233 return iterate_over_threads (have_threads_callback
, NULL
) != NULL
;
236 /* A callback function for td_ta_thr_iter, which we use to map all
239 THP is a handle to the current thread; if INFOP is not NULL, the
240 struct thread_info associated with this thread is returned in
243 If the thread is a zombie, TD_THR_ZOMBIE is returned. Otherwise,
244 zero is returned to indicate success. */
247 thread_get_info_callback (const td_thrhandle_t
*thp
, void *infop
)
251 struct thread_info
*thread_info
;
254 err
= td_thr_get_info_p (thp
, &ti
);
256 error (_("thread_get_info_callback: cannot get thread info: %s"),
257 thread_db_err_str (err
));
259 /* Fill the cache. */
260 thread_ptid
= ptid_build (GET_PID (inferior_ptid
), ti
.ti_lid
, ti
.ti_tid
);
261 thread_info
= find_thread_pid (thread_ptid
);
263 /* In the case of a zombie thread, don't continue. We don't want to
264 attach to it thinking it is a new thread. */
265 if (ti
.ti_state
== TD_THR_UNKNOWN
|| ti
.ti_state
== TD_THR_ZOMBIE
)
268 *(struct thread_info
**) infop
= thread_info
;
269 if (thread_info
!= NULL
)
271 memcpy (&thread_info
->private->th
, thp
, sizeof (*thp
));
272 thread_info
->private->th_valid
= 1;
273 memcpy (&thread_info
->private->ti
, &ti
, sizeof (ti
));
274 thread_info
->private->ti_valid
= 1;
276 return TD_THR_ZOMBIE
;
279 if (thread_info
== NULL
)
281 /* New thread. Attach to it now (why wait?). */
282 attach_thread (thread_ptid
, thp
, &ti
, 1);
283 thread_info
= find_thread_pid (thread_ptid
);
284 gdb_assert (thread_info
!= NULL
);
287 memcpy (&thread_info
->private->th
, thp
, sizeof (*thp
));
288 thread_info
->private->th_valid
= 1;
289 memcpy (&thread_info
->private->ti
, &ti
, sizeof (ti
));
290 thread_info
->private->ti_valid
= 1;
293 *(struct thread_info
**) infop
= thread_info
;
298 /* Accessor functions for the thread_db information, with caching. */
301 thread_db_map_id2thr (struct thread_info
*thread_info
, int fatal
)
305 if (thread_info
->private->th_valid
)
308 err
= td_ta_map_id2thr_p (thread_agent
, GET_THREAD (thread_info
->ptid
),
309 &thread_info
->private->th
);
313 error (_("Cannot find thread %ld: %s"),
314 (long) GET_THREAD (thread_info
->ptid
),
315 thread_db_err_str (err
));
318 thread_info
->private->th_valid
= 1;
321 /* Convert between user-level thread ids and LWP ids. */
324 thread_from_lwp (ptid_t ptid
)
328 struct thread_info
*thread_info
;
331 if (GET_LWP (ptid
) == 0)
332 ptid
= BUILD_LWP (GET_PID (ptid
), GET_PID (ptid
));
334 gdb_assert (is_lwp (ptid
));
336 err
= td_ta_map_lwp2thr_p (thread_agent
, GET_LWP (ptid
), &th
);
338 error (_("Cannot find user-level thread for LWP %ld: %s"),
339 GET_LWP (ptid
), thread_db_err_str (err
));
343 /* Fetch the thread info. If we get back TD_THR_ZOMBIE, then the
344 event thread has already died. If another gdb interface has called
345 thread_alive() previously, the thread won't be found on the thread list
346 anymore. In that case, we don't want to process this ptid anymore
347 to avoid the possibility of later treating it as a newly
348 discovered thread id that we should add to the list. Thus,
349 we return a -1 ptid which is also how the thread list marks a
351 if (thread_get_info_callback (&th
, &thread_info
) == TD_THR_ZOMBIE
352 && thread_info
== NULL
)
353 return pid_to_ptid (-1);
355 gdb_assert (thread_info
&& thread_info
->private->ti_valid
);
357 return ptid_build (GET_PID (ptid
), GET_LWP (ptid
),
358 thread_info
->private->ti
.ti_tid
);
362 lwp_from_thread (ptid_t ptid
)
364 return BUILD_LWP (GET_LWP (ptid
), GET_PID (ptid
));
369 thread_db_init (struct target_ops
*target
)
371 target_beneath
= target
;
375 verbose_dlsym (void *handle
, const char *name
)
377 void *sym
= dlsym (handle
, name
);
379 warning (_("Symbol \"%s\" not found in libthread_db: %s"), name
, dlerror ());
384 thread_db_load (void)
389 handle
= dlopen (LIBTHREAD_DB_SO
, RTLD_NOW
);
392 fprintf_filtered (gdb_stderr
, "\n\ndlopen failed on '%s' - %s\n",
393 LIBTHREAD_DB_SO
, dlerror ());
394 fprintf_filtered (gdb_stderr
,
395 "GDB will not be able to debug pthreads.\n\n");
399 /* Initialize pointers to the dynamic library functions we will use.
400 Essential functions first. */
402 td_init_p
= verbose_dlsym (handle
, "td_init");
403 if (td_init_p
== NULL
)
406 td_ta_new_p
= verbose_dlsym (handle
, "td_ta_new");
407 if (td_ta_new_p
== NULL
)
410 td_ta_map_id2thr_p
= verbose_dlsym (handle
, "td_ta_map_id2thr");
411 if (td_ta_map_id2thr_p
== NULL
)
414 td_ta_map_lwp2thr_p
= verbose_dlsym (handle
, "td_ta_map_lwp2thr");
415 if (td_ta_map_lwp2thr_p
== NULL
)
418 td_ta_thr_iter_p
= verbose_dlsym (handle
, "td_ta_thr_iter");
419 if (td_ta_thr_iter_p
== NULL
)
422 td_thr_validate_p
= verbose_dlsym (handle
, "td_thr_validate");
423 if (td_thr_validate_p
== NULL
)
426 td_thr_get_info_p
= verbose_dlsym (handle
, "td_thr_get_info");
427 if (td_thr_get_info_p
== NULL
)
430 /* Initialize the library. */
434 warning (_("Cannot initialize libthread_db: %s"), thread_db_err_str (err
));
438 /* These are not essential. */
439 td_ta_event_addr_p
= dlsym (handle
, "td_ta_event_addr");
440 td_ta_set_event_p
= dlsym (handle
, "td_ta_set_event");
441 td_ta_event_getmsg_p
= dlsym (handle
, "td_ta_event_getmsg");
442 td_thr_event_enable_p
= dlsym (handle
, "td_thr_event_enable");
443 td_thr_tls_get_addr_p
= dlsym (handle
, "td_thr_tls_get_addr");
449 enable_thread_event (td_thragent_t
*thread_agent
, int event
, CORE_ADDR
*bp
)
454 /* Get the breakpoint address for thread EVENT. */
455 err
= td_ta_event_addr_p (thread_agent
, event
, ¬ify
);
459 /* Set up the breakpoint. */
460 gdb_assert (exec_bfd
);
461 (*bp
) = (gdbarch_convert_from_func_ptr_addr
463 /* Do proper sign extension for the target. */
464 (bfd_get_sign_extend_vma (exec_bfd
) > 0
465 ? (CORE_ADDR
) (intptr_t) notify
.u
.bptaddr
466 : (CORE_ADDR
) (uintptr_t) notify
.u
.bptaddr
),
468 create_thread_event_breakpoint ((*bp
));
474 enable_thread_event_reporting (void)
476 td_thr_events_t events
;
479 #ifdef HAVE_GNU_LIBC_VERSION_H
480 const char *libc_version
;
481 int libc_major
, libc_minor
;
484 /* We cannot use the thread event reporting facility if these
485 functions aren't available. */
486 if (td_ta_event_addr_p
== NULL
|| td_ta_set_event_p
== NULL
487 || td_ta_event_getmsg_p
== NULL
|| td_thr_event_enable_p
== NULL
)
490 /* Set the process wide mask saying which events we're interested in. */
491 td_event_emptyset (&events
);
492 td_event_addset (&events
, TD_CREATE
);
494 #ifdef HAVE_GNU_LIBC_VERSION_H
495 /* The event reporting facility is broken for TD_DEATH events in
496 glibc 2.1.3, so don't enable it if we have glibc but a lower
498 libc_version
= gnu_get_libc_version ();
499 if (sscanf (libc_version
, "%d.%d", &libc_major
, &libc_minor
) == 2
500 && (libc_major
> 2 || (libc_major
== 2 && libc_minor
> 1)))
502 td_event_addset (&events
, TD_DEATH
);
504 err
= td_ta_set_event_p (thread_agent
, &events
);
507 warning (_("Unable to set global thread event mask: %s"),
508 thread_db_err_str (err
));
512 /* Delete previous thread event breakpoints, if any. */
513 remove_thread_event_breakpoints ();
514 td_create_bp_addr
= 0;
515 td_death_bp_addr
= 0;
517 /* Set up the thread creation event. */
518 err
= enable_thread_event (thread_agent
, TD_CREATE
, &td_create_bp_addr
);
521 warning (_("Unable to get location for thread creation breakpoint: %s"),
522 thread_db_err_str (err
));
526 /* Set up the thread death event. */
527 err
= enable_thread_event (thread_agent
, TD_DEATH
, &td_death_bp_addr
);
530 warning (_("Unable to get location for thread death breakpoint: %s"),
531 thread_db_err_str (err
));
537 disable_thread_event_reporting (void)
539 td_thr_events_t events
;
541 /* Set the process wide mask saying we aren't interested in any
543 td_event_emptyset (&events
);
544 td_ta_set_event_p (thread_agent
, &events
);
546 /* Delete thread event breakpoints, if any. */
547 remove_thread_event_breakpoints ();
548 td_create_bp_addr
= 0;
549 td_death_bp_addr
= 0;
553 check_thread_signals (void)
555 #ifdef GET_THREAD_SIGNALS
561 GET_THREAD_SIGNALS (&mask
);
562 sigemptyset (&thread_stop_set
);
563 sigemptyset (&thread_print_set
);
565 for (i
= 1; i
< NSIG
; i
++)
567 if (sigismember (&mask
, i
))
569 if (signal_stop_update (target_signal_from_host (i
), 0))
570 sigaddset (&thread_stop_set
, i
);
571 if (signal_print_update (target_signal_from_host (i
), 0))
572 sigaddset (&thread_print_set
, i
);
580 /* Check whether thread_db is usable. This function is called when
581 an inferior is created (or otherwise acquired, e.g. attached to)
582 and when new shared libraries are loaded into a running process. */
585 check_for_thread_db (void)
588 static int already_loaded
;
590 /* Do nothing if we couldn't load libthread_db.so.1. */
591 if (td_ta_new_p
== NULL
)
594 /* First time through, report that libthread_db was successfuly
595 loaded. Can't print this in in thread_db_load as, at that stage,
596 the interpreter and it's console haven't started. */
601 const char *library
= NULL
;
602 if (dladdr ((*td_ta_new_p
), &info
) != 0)
603 library
= info
.dli_fname
;
608 /* Paranoid - don't let a NULL path slip through. */
609 library
= LIBTHREAD_DB_SO
;
611 printf_unfiltered (_("Using host libthread_db library \"%s\".\n"),
617 /* Nothing to do. The thread library was already detected and the
618 target vector was already activated. */
621 /* Don't attempt to use thread_db on targets which can not run
622 (executables not running yet, core files) for now. */
623 if (!target_has_execution
)
626 /* Don't attempt to use thread_db for remote targets. */
627 if (!target_can_run (¤t_target
))
630 /* Initialize the structure that identifies the child process. */
631 proc_handle
.pid
= GET_PID (inferior_ptid
);
633 /* Now attempt to open a connection to the thread library. */
634 err
= td_ta_new_p (&proc_handle
, &thread_agent
);
638 /* No thread library was detected. */
642 printf_unfiltered (_("[Thread debugging using libthread_db enabled]\n"));
644 /* The thread library was detected. Activate the thread_db target. */
645 push_target (&thread_db_ops
);
648 enable_thread_event_reporting ();
649 thread_db_find_new_threads ();
653 warning (_("Cannot initialize thread debugging library: %s"),
654 thread_db_err_str (err
));
660 thread_db_new_objfile (struct objfile
*objfile
)
663 check_for_thread_db ();
666 /* Attach to a new thread. This function is called when we receive a
667 TD_CREATE event or when we iterate over all threads and find one
668 that wasn't already in our list. */
671 attach_thread (ptid_t ptid
, const td_thrhandle_t
*th_p
,
672 const td_thrinfo_t
*ti_p
, int verbose
)
674 struct thread_info
*tp
;
677 /* If we're being called after a TD_CREATE event, we may already
678 know about this thread. There are two ways this can happen. We
679 may have iterated over all threads between the thread creation
680 and the TD_CREATE event, for instance when the user has issued
681 the `info threads' command before the SIGTRAP for hitting the
682 thread creation breakpoint was reported. Alternatively, the
683 thread may have exited and a new one been created with the same
684 thread ID. In the first case we don't need to do anything; in
685 the second case we should discard information about the dead
686 thread and attach to the new one. */
687 if (in_thread_list (ptid
))
689 tp
= find_thread_pid (ptid
);
690 gdb_assert (tp
!= NULL
);
692 if (!tp
->private->dying
)
695 delete_thread (ptid
);
698 check_thread_signals ();
700 if (ti_p
->ti_state
== TD_THR_UNKNOWN
|| ti_p
->ti_state
== TD_THR_ZOMBIE
)
701 return; /* A zombie thread -- do not attach. */
703 /* Under GNU/Linux, we have to attach to each and every thread. */
704 if (lin_lwp_attach_lwp (BUILD_LWP (ti_p
->ti_lid
, GET_PID (ptid
)), 0) < 0)
707 /* Add the thread to GDB's thread list. */
708 tp
= add_thread (ptid
);
709 tp
->private = xmalloc (sizeof (struct private_thread_info
));
710 memset (tp
->private, 0, sizeof (struct private_thread_info
));
713 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid
));
715 /* Enable thread event reporting for this thread. */
716 err
= td_thr_event_enable_p (th_p
, 1);
718 error (_("Cannot enable thread event reporting for %s: %s"),
719 target_pid_to_str (ptid
), thread_db_err_str (err
));
723 detach_thread (ptid_t ptid
, int verbose
)
725 struct thread_info
*thread_info
;
728 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (ptid
));
730 /* Don't delete the thread now, because it still reports as active
731 until it has executed a few instructions after the event
732 breakpoint - if we deleted it now, "info threads" would cause us
733 to re-attach to it. Just mark it as having had a TD_DEATH
734 event. This means that we won't delete it from our thread list
735 until we notice that it's dead (via prune_threads), or until
736 something re-uses its thread ID. */
737 thread_info
= find_thread_pid (ptid
);
738 gdb_assert (thread_info
!= NULL
);
739 thread_info
->private->dying
= 1;
743 thread_db_detach (char *args
, int from_tty
)
745 disable_thread_event_reporting ();
747 /* There's no need to save & restore inferior_ptid here, since the
748 inferior is not supposed to survive this function call. */
749 inferior_ptid
= lwp_from_thread (inferior_ptid
);
751 target_beneath
->to_detach (args
, from_tty
);
753 /* Should this be done by detach_command? */
754 target_mourn_inferior ();
758 clear_lwpid_callback (struct thread_info
*thread
, void *dummy
)
760 /* If we know that our thread implementation is 1-to-1, we could save
761 a certain amount of information; it's not clear how much, so we
762 are always conservative. */
764 thread
->private->th_valid
= 0;
765 thread
->private->ti_valid
= 0;
771 thread_db_resume (ptid_t ptid
, int step
, enum target_signal signo
)
773 struct cleanup
*old_chain
= save_inferior_ptid ();
775 if (GET_PID (ptid
) == -1)
776 inferior_ptid
= lwp_from_thread (inferior_ptid
);
777 else if (is_thread (ptid
))
778 ptid
= lwp_from_thread (ptid
);
780 /* Clear cached data which may not be valid after the resume. */
781 iterate_over_threads (clear_lwpid_callback
, NULL
);
783 target_beneath
->to_resume (ptid
, step
, signo
);
785 do_cleanups (old_chain
);
788 /* Check if PID is currently stopped at the location of a thread event
789 breakpoint location. If it is, read the event message and act upon
793 check_event (ptid_t ptid
)
801 /* Bail out early if we're not at a thread event breakpoint. */
802 stop_pc
= read_pc_pid (ptid
) - gdbarch_decr_pc_after_break (current_gdbarch
);
803 if (stop_pc
!= td_create_bp_addr
&& stop_pc
!= td_death_bp_addr
)
806 /* If we are at a create breakpoint, we do not know what new lwp
807 was created and cannot specifically locate the event message for it.
808 We have to call td_ta_event_getmsg() to get
809 the latest message. Since we have no way of correlating whether
810 the event message we get back corresponds to our breakpoint, we must
811 loop and read all event messages, processing them appropriately.
812 This guarantees we will process the correct message before continuing
815 Currently, death events are not enabled. If they are enabled,
816 the death event can use the td_thr_event_getmsg() interface to
817 get the message specifically for that lwp and avoid looping
824 err
= td_ta_event_getmsg_p (thread_agent
, &msg
);
830 error (_("Cannot get thread event message: %s"),
831 thread_db_err_str (err
));
834 err
= td_thr_get_info_p (msg
.th_p
, &ti
);
836 error (_("Cannot get thread info: %s"), thread_db_err_str (err
));
838 ptid
= ptid_build (GET_PID (ptid
), ti
.ti_lid
, ti
.ti_tid
);
843 /* Call attach_thread whether or not we already know about a
844 thread with this thread ID. */
845 attach_thread (ptid
, msg
.th_p
, &ti
, 1);
851 if (!in_thread_list (ptid
))
852 error (_("Spurious thread death event."));
854 detach_thread (ptid
, 1);
859 error (_("Spurious thread event."));
866 thread_db_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
868 extern ptid_t trap_ptid
;
870 if (GET_PID (ptid
) != -1 && is_thread (ptid
))
871 ptid
= lwp_from_thread (ptid
);
873 ptid
= target_beneath
->to_wait (ptid
, ourstatus
);
875 if (ourstatus
->kind
== TARGET_WAITKIND_EXITED
876 || ourstatus
->kind
== TARGET_WAITKIND_SIGNALLED
)
877 return pid_to_ptid (-1);
879 if (ourstatus
->kind
== TARGET_WAITKIND_EXECD
)
881 remove_thread_event_breakpoints ();
882 unpush_target (&thread_db_ops
);
885 return pid_to_ptid (GET_PID (ptid
));
888 /* If we do not know about the main thread yet, this would be a good time to
890 if (ourstatus
->kind
== TARGET_WAITKIND_STOPPED
&& !have_threads ())
891 thread_db_find_new_threads ();
893 if (ourstatus
->kind
== TARGET_WAITKIND_STOPPED
894 && ourstatus
->value
.sig
== TARGET_SIGNAL_TRAP
)
895 /* Check for a thread event. */
900 /* Change ptids back into the higher level PID + TID format. If
901 the thread is dead and no longer on the thread list, we will
902 get back a dead ptid. This can occur if the thread death
903 event gets postponed by other simultaneous events. In such a
904 case, we want to just ignore the event and continue on. */
906 if (!ptid_equal (trap_ptid
, null_ptid
))
907 trap_ptid
= thread_from_lwp (trap_ptid
);
909 ptid
= thread_from_lwp (ptid
);
910 if (GET_PID (ptid
) == -1)
911 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
918 thread_db_kill (void)
920 /* There's no need to save & restore inferior_ptid here, since the
921 inferior isn't supposed to survive this function call. */
922 inferior_ptid
= lwp_from_thread (inferior_ptid
);
923 target_beneath
->to_kill ();
927 thread_db_mourn_inferior (void)
929 /* Forget about the child's process ID. We shouldn't need it
933 target_beneath
->to_mourn_inferior ();
935 /* Delete the old thread event breakpoints. Do this after mourning
936 the inferior, so that we don't try to uninsert them. */
937 remove_thread_event_breakpoints ();
939 /* Detach thread_db target ops. */
940 unpush_target (&thread_db_ops
);
945 find_new_threads_callback (const td_thrhandle_t
*th_p
, void *data
)
951 err
= td_thr_get_info_p (th_p
, &ti
);
953 error (_("find_new_threads_callback: cannot get thread info: %s"),
954 thread_db_err_str (err
));
956 if (ti
.ti_state
== TD_THR_UNKNOWN
|| ti
.ti_state
== TD_THR_ZOMBIE
)
957 return 0; /* A zombie -- ignore. */
959 ptid
= ptid_build (GET_PID (inferior_ptid
), ti
.ti_lid
, ti
.ti_tid
);
963 /* A thread ID of zero means that this is the main thread, but
964 glibc has not yet initialized thread-local storage and the
965 pthread library. We do not know what the thread's TID will
966 be yet. Just enable event reporting and otherwise ignore
969 err
= td_thr_event_enable_p (th_p
, 1);
971 error (_("Cannot enable thread event reporting for %s: %s"),
972 target_pid_to_str (ptid
), thread_db_err_str (err
));
977 if (!in_thread_list (ptid
))
978 attach_thread (ptid
, th_p
, &ti
, 1);
984 thread_db_find_new_threads (void)
988 /* Iterate over all user-space threads to discover new threads. */
989 err
= td_ta_thr_iter_p (thread_agent
, find_new_threads_callback
, NULL
,
990 TD_THR_ANY_STATE
, TD_THR_LOWEST_PRIORITY
,
991 TD_SIGNO_MASK
, TD_THR_ANY_USER_FLAGS
);
993 error (_("Cannot find new threads: %s"), thread_db_err_str (err
));
997 thread_db_pid_to_str (ptid_t ptid
)
999 if (is_thread (ptid
))
1001 static char buf
[64];
1002 struct thread_info
*thread_info
;
1004 thread_info
= find_thread_pid (ptid
);
1005 if (thread_info
== NULL
)
1006 snprintf (buf
, sizeof (buf
), "Thread 0x%lx (LWP %ld) (Missing)",
1007 GET_THREAD (ptid
), GET_LWP (ptid
));
1009 snprintf (buf
, sizeof (buf
), "Thread 0x%lx (LWP %ld)",
1010 GET_THREAD (ptid
), GET_LWP (ptid
));
1015 if (target_beneath
->to_pid_to_str (ptid
))
1016 return target_beneath
->to_pid_to_str (ptid
);
1018 return normal_pid_to_str (ptid
);
1021 /* Return a string describing the state of the thread specified by
1025 thread_db_extra_thread_info (struct thread_info
*info
)
1027 if (info
->private->dying
)
1033 /* Return 1 if this thread has the same LWP as the passed PTID. */
1036 same_ptid_callback (struct thread_info
*thread
, void *arg
)
1038 ptid_t
*ptid_p
= arg
;
1040 return GET_LWP (thread
->ptid
) == GET_LWP (*ptid_p
);
1043 /* Get the address of the thread local variable in load module LM which
1044 is stored at OFFSET within the thread local storage for thread PTID. */
1047 thread_db_get_thread_local_address (ptid_t ptid
,
1051 /* If we have not discovered any threads yet, check now. */
1052 if (!is_thread (ptid
) && !have_threads ())
1053 thread_db_find_new_threads ();
1055 /* Try to find a matching thread if we still have the LWP ID instead
1056 of the thread ID. */
1057 if (!is_thread (ptid
))
1059 struct thread_info
*thread
;
1061 thread
= iterate_over_threads (same_ptid_callback
, &ptid
);
1063 ptid
= thread
->ptid
;
1066 if (is_thread (ptid
))
1070 struct thread_info
*thread_info
;
1072 /* glibc doesn't provide the needed interface. */
1073 if (!td_thr_tls_get_addr_p
)
1074 throw_error (TLS_NO_LIBRARY_SUPPORT_ERROR
,
1075 _("No TLS library support"));
1077 /* Caller should have verified that lm != 0. */
1078 gdb_assert (lm
!= 0);
1080 /* Get info about the thread. */
1081 thread_info
= find_thread_pid (ptid
);
1082 gdb_assert (thread_info
);
1083 thread_db_map_id2thr (thread_info
, 1);
1085 /* Finally, get the address of the variable. */
1086 err
= td_thr_tls_get_addr_p (&thread_info
->private->th
,
1087 (void *)(size_t) lm
,
1090 #ifdef THREAD_DB_HAS_TD_NOTALLOC
1091 /* The memory hasn't been allocated, yet. */
1092 if (err
== TD_NOTALLOC
)
1093 /* Now, if libthread_db provided the initialization image's
1094 address, we *could* try to build a non-lvalue value from
1095 the initialization image. */
1096 throw_error (TLS_NOT_ALLOCATED_YET_ERROR
,
1097 _("TLS not allocated yet"));
1100 /* Something else went wrong. */
1102 throw_error (TLS_GENERIC_ERROR
,
1103 (("%s")), thread_db_err_str (err
));
1105 /* Cast assuming host == target. Joy. */
1106 /* Do proper sign extension for the target. */
1107 gdb_assert (exec_bfd
);
1108 return (bfd_get_sign_extend_vma (exec_bfd
) > 0
1109 ? (CORE_ADDR
) (intptr_t) address
1110 : (CORE_ADDR
) (uintptr_t) address
);
1113 if (target_beneath
->to_get_thread_local_address
)
1114 return target_beneath
->to_get_thread_local_address (ptid
, lm
, offset
);
1116 throw_error (TLS_GENERIC_ERROR
,
1117 _("TLS not supported on this target"));
1121 init_thread_db_ops (void)
1123 thread_db_ops
.to_shortname
= "multi-thread";
1124 thread_db_ops
.to_longname
= "multi-threaded child process.";
1125 thread_db_ops
.to_doc
= "Threads and pthreads support.";
1126 thread_db_ops
.to_detach
= thread_db_detach
;
1127 thread_db_ops
.to_resume
= thread_db_resume
;
1128 thread_db_ops
.to_wait
= thread_db_wait
;
1129 thread_db_ops
.to_kill
= thread_db_kill
;
1130 thread_db_ops
.to_mourn_inferior
= thread_db_mourn_inferior
;
1131 thread_db_ops
.to_find_new_threads
= thread_db_find_new_threads
;
1132 thread_db_ops
.to_pid_to_str
= thread_db_pid_to_str
;
1133 thread_db_ops
.to_stratum
= thread_stratum
;
1134 thread_db_ops
.to_has_thread_control
= tc_schedlock
;
1135 thread_db_ops
.to_get_thread_local_address
1136 = thread_db_get_thread_local_address
;
1137 thread_db_ops
.to_extra_thread_info
= thread_db_extra_thread_info
;
1138 thread_db_ops
.to_magic
= OPS_MAGIC
;
1142 _initialize_thread_db (void)
1144 /* Only initialize the module if we can load libthread_db. */
1145 if (thread_db_load ())
1147 init_thread_db_ops ();
1148 add_target (&thread_db_ops
);
1150 /* Add ourselves to objfile event chain. */
1151 observer_attach_new_objfile (thread_db_new_objfile
);