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 2 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, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
25 #include "gdb_assert.h"
27 #include "gdb_proc_service.h"
28 #include "gdb_thread_db.h"
31 #include "exceptions.h"
32 #include "gdbthread.h"
38 #include "solib-svr4.h"
40 #include "linux-nat.h"
44 #ifdef HAVE_GNU_LIBC_VERSION_H
45 #include <gnu/libc-version.h>
48 #ifndef LIBTHREAD_DB_SO
49 #define LIBTHREAD_DB_SO "libthread_db.so.1"
52 /* If we're running on GNU/Linux, we must explicitly attach to any new
55 /* FIXME: There is certainly some room for improvements:
57 - Bypass libthread_db when fetching or storing registers for
58 threads bound to a LWP. */
60 /* This module's target vector. */
61 static struct target_ops thread_db_ops
;
63 /* The target vector that we call for things this module can't handle. */
64 static struct target_ops
*target_beneath
;
66 /* Pointer to the next function on the objfile event chain. */
67 static void (*target_new_objfile_chain
) (struct objfile
* objfile
);
69 /* Non-zero if we're using this module's target vector. */
70 static int using_thread_db
;
72 /* Non-zero if we have determined the signals used by the threads
74 static int thread_signals
;
75 static sigset_t thread_stop_set
;
76 static sigset_t thread_print_set
;
78 /* Structure that identifies the child process for the
79 <proc_service.h> interface. */
80 static struct ps_prochandle proc_handle
;
82 /* Connection to the libthread_db library. */
83 static td_thragent_t
*thread_agent
;
85 /* Pointers to the libthread_db functions. */
87 static td_err_e (*td_init_p
) (void);
89 static td_err_e (*td_ta_new_p
) (struct ps_prochandle
* ps
,
91 static td_err_e (*td_ta_map_id2thr_p
) (const td_thragent_t
*ta
, thread_t pt
,
92 td_thrhandle_t
*__th
);
93 static td_err_e (*td_ta_map_lwp2thr_p
) (const td_thragent_t
*ta
,
94 lwpid_t lwpid
, td_thrhandle_t
*th
);
95 static td_err_e (*td_ta_thr_iter_p
) (const td_thragent_t
*ta
,
96 td_thr_iter_f
*callback
, void *cbdata_p
,
97 td_thr_state_e state
, int ti_pri
,
98 sigset_t
*ti_sigmask_p
,
99 unsigned int ti_user_flags
);
100 static td_err_e (*td_ta_event_addr_p
) (const td_thragent_t
*ta
,
101 td_event_e event
, td_notify_t
*ptr
);
102 static td_err_e (*td_ta_set_event_p
) (const td_thragent_t
*ta
,
103 td_thr_events_t
*event
);
104 static td_err_e (*td_ta_event_getmsg_p
) (const td_thragent_t
*ta
,
105 td_event_msg_t
*msg
);
107 static td_err_e (*td_thr_validate_p
) (const td_thrhandle_t
*th
);
108 static td_err_e (*td_thr_get_info_p
) (const td_thrhandle_t
*th
,
109 td_thrinfo_t
*infop
);
110 static td_err_e (*td_thr_event_enable_p
) (const td_thrhandle_t
*th
,
113 static td_err_e (*td_thr_tls_get_addr_p
) (const td_thrhandle_t
*th
,
115 size_t offset
, void **address
);
117 /* Location of the thread creation event breakpoint. The code at this
118 location in the child process will be called by the pthread library
119 whenever a new thread is created. By setting a special breakpoint
120 at this location, GDB can detect when a new thread is created. We
121 obtain this location via the td_ta_event_addr call. */
122 static CORE_ADDR td_create_bp_addr
;
124 /* Location of the thread death event breakpoint. */
125 static CORE_ADDR td_death_bp_addr
;
127 /* Prototypes for local functions. */
128 static void thread_db_find_new_threads (void);
129 static void attach_thread (ptid_t ptid
, const td_thrhandle_t
*th_p
,
130 const td_thrinfo_t
*ti_p
, int verbose
);
131 static void detach_thread (ptid_t ptid
, int verbose
);
134 /* Building process ids. */
136 #define GET_PID(ptid) ptid_get_pid (ptid)
137 #define GET_LWP(ptid) ptid_get_lwp (ptid)
138 #define GET_THREAD(ptid) ptid_get_tid (ptid)
140 #define is_lwp(ptid) (GET_LWP (ptid) != 0)
141 #define is_thread(ptid) (GET_THREAD (ptid) != 0)
143 #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
146 /* Use "struct private_thread_info" to cache thread state. This is
147 a substantial optimization. */
149 struct private_thread_info
151 /* Flag set when we see a TD_DEATH event for this thread. */
152 unsigned int dying
:1;
154 /* Cached thread state. */
155 unsigned int th_valid
:1;
156 unsigned int ti_valid
:1;
164 thread_db_err_str (td_err_e err
)
171 return "generic 'call succeeded'";
173 return "generic error";
175 return "no thread to satisfy query";
177 return "no sync handle to satisfy query";
179 return "no LWP to satisfy query";
181 return "invalid process handle";
183 return "invalid thread handle";
185 return "invalid synchronization handle";
187 return "invalid thread agent";
189 return "invalid key";
191 return "no event message for getmsg";
193 return "FPU register set not available";
195 return "application not linked with libthread";
197 return "requested event is not supported";
199 return "capability not available";
201 return "debugger service failed";
203 return "operation not applicable to";
205 return "no thread-specific data for this thread";
207 return "malloc failed";
209 return "only part of register set was written/read";
211 return "X register set not available for this thread";
212 #ifdef THREAD_DB_HAS_TD_NOTALLOC
214 return "thread has not yet allocated TLS for given module";
216 #ifdef THREAD_DB_HAS_TD_VERSION
218 return "versions of libpthread and libthread_db do not match";
220 #ifdef THREAD_DB_HAS_TD_NOTLS
222 return "there is no TLS segment in the given module";
225 snprintf (buf
, sizeof (buf
), "unknown thread_db error '%d'", err
);
230 /* A callback function for td_ta_thr_iter, which we use to map all
233 THP is a handle to the current thread; if INFOP is not NULL, the
234 struct thread_info associated with this thread is returned in
237 If the thread is a zombie, TD_THR_ZOMBIE is returned. Otherwise,
238 zero is returned to indicate success. */
241 thread_get_info_callback (const td_thrhandle_t
*thp
, void *infop
)
245 struct thread_info
*thread_info
;
248 err
= td_thr_get_info_p (thp
, &ti
);
250 error (_("thread_get_info_callback: cannot get thread info: %s"),
251 thread_db_err_str (err
));
253 /* Fill the cache. */
254 thread_ptid
= ptid_build (GET_PID (inferior_ptid
), ti
.ti_lid
, ti
.ti_tid
);
255 thread_info
= find_thread_pid (thread_ptid
);
257 /* In the case of a zombie thread, don't continue. We don't want to
258 attach to it thinking it is a new thread. */
259 if (ti
.ti_state
== TD_THR_UNKNOWN
|| ti
.ti_state
== TD_THR_ZOMBIE
)
262 *(struct thread_info
**) infop
= thread_info
;
263 if (thread_info
!= NULL
)
265 memcpy (&thread_info
->private->th
, thp
, sizeof (*thp
));
266 thread_info
->private->th_valid
= 1;
267 memcpy (&thread_info
->private->ti
, &ti
, sizeof (ti
));
268 thread_info
->private->ti_valid
= 1;
270 return TD_THR_ZOMBIE
;
273 if (thread_info
== NULL
)
275 /* New thread. Attach to it now (why wait?). */
276 attach_thread (thread_ptid
, thp
, &ti
, 1);
277 thread_info
= find_thread_pid (thread_ptid
);
278 gdb_assert (thread_info
!= NULL
);
281 memcpy (&thread_info
->private->th
, thp
, sizeof (*thp
));
282 thread_info
->private->th_valid
= 1;
283 memcpy (&thread_info
->private->ti
, &ti
, sizeof (ti
));
284 thread_info
->private->ti_valid
= 1;
287 *(struct thread_info
**) infop
= thread_info
;
292 /* Accessor functions for the thread_db information, with caching. */
295 thread_db_map_id2thr (struct thread_info
*thread_info
, int fatal
)
299 if (thread_info
->private->th_valid
)
302 err
= td_ta_map_id2thr_p (thread_agent
, GET_THREAD (thread_info
->ptid
),
303 &thread_info
->private->th
);
307 error (_("Cannot find thread %ld: %s"),
308 (long) GET_THREAD (thread_info
->ptid
),
309 thread_db_err_str (err
));
312 thread_info
->private->th_valid
= 1;
315 /* Convert between user-level thread ids and LWP ids. */
318 thread_from_lwp (ptid_t ptid
)
322 struct thread_info
*thread_info
;
325 if (GET_LWP (ptid
) == 0)
326 ptid
= BUILD_LWP (GET_PID (ptid
), GET_PID (ptid
));
328 gdb_assert (is_lwp (ptid
));
330 err
= td_ta_map_lwp2thr_p (thread_agent
, GET_LWP (ptid
), &th
);
332 error (_("Cannot find user-level thread for LWP %ld: %s"),
333 GET_LWP (ptid
), thread_db_err_str (err
));
337 /* Fetch the thread info. If we get back TD_THR_ZOMBIE, then the
338 event thread has already died. If another gdb interface has called
339 thread_alive() previously, the thread won't be found on the thread list
340 anymore. In that case, we don't want to process this ptid anymore
341 to avoid the possibility of later treating it as a newly
342 discovered thread id that we should add to the list. Thus,
343 we return a -1 ptid which is also how the thread list marks a
345 if (thread_get_info_callback (&th
, &thread_info
) == TD_THR_ZOMBIE
346 && thread_info
== NULL
)
347 return pid_to_ptid (-1);
349 gdb_assert (thread_info
&& thread_info
->private->ti_valid
);
351 return ptid_build (GET_PID (ptid
), GET_LWP (ptid
),
352 thread_info
->private->ti
.ti_tid
);
356 lwp_from_thread (ptid_t ptid
)
358 return BUILD_LWP (GET_LWP (ptid
), GET_PID (ptid
));
363 thread_db_init (struct target_ops
*target
)
365 target_beneath
= target
;
369 verbose_dlsym (void *handle
, const char *name
)
371 void *sym
= dlsym (handle
, name
);
373 warning (_("Symbol \"%s\" not found in libthread_db: %s"), name
, dlerror ());
378 thread_db_load (void)
383 handle
= dlopen (LIBTHREAD_DB_SO
, RTLD_NOW
);
386 fprintf_filtered (gdb_stderr
, "\n\ndlopen failed on '%s' - %s\n",
387 LIBTHREAD_DB_SO
, dlerror ());
388 fprintf_filtered (gdb_stderr
,
389 "GDB will not be able to debug pthreads.\n\n");
393 /* Initialize pointers to the dynamic library functions we will use.
394 Essential functions first. */
396 td_init_p
= verbose_dlsym (handle
, "td_init");
397 if (td_init_p
== NULL
)
400 td_ta_new_p
= verbose_dlsym (handle
, "td_ta_new");
401 if (td_ta_new_p
== NULL
)
404 td_ta_map_id2thr_p
= verbose_dlsym (handle
, "td_ta_map_id2thr");
405 if (td_ta_map_id2thr_p
== NULL
)
408 td_ta_map_lwp2thr_p
= verbose_dlsym (handle
, "td_ta_map_lwp2thr");
409 if (td_ta_map_lwp2thr_p
== NULL
)
412 td_ta_thr_iter_p
= verbose_dlsym (handle
, "td_ta_thr_iter");
413 if (td_ta_thr_iter_p
== NULL
)
416 td_thr_validate_p
= verbose_dlsym (handle
, "td_thr_validate");
417 if (td_thr_validate_p
== NULL
)
420 td_thr_get_info_p
= verbose_dlsym (handle
, "td_thr_get_info");
421 if (td_thr_get_info_p
== NULL
)
424 /* Initialize the library. */
428 warning (_("Cannot initialize libthread_db: %s"), thread_db_err_str (err
));
432 /* These are not essential. */
433 td_ta_event_addr_p
= dlsym (handle
, "td_ta_event_addr");
434 td_ta_set_event_p
= dlsym (handle
, "td_ta_set_event");
435 td_ta_event_getmsg_p
= dlsym (handle
, "td_ta_event_getmsg");
436 td_thr_event_enable_p
= dlsym (handle
, "td_thr_event_enable");
437 td_thr_tls_get_addr_p
= dlsym (handle
, "td_thr_tls_get_addr");
443 enable_thread_event (td_thragent_t
*thread_agent
, int event
, CORE_ADDR
*bp
)
448 /* Get the breakpoint address for thread EVENT. */
449 err
= td_ta_event_addr_p (thread_agent
, event
, ¬ify
);
453 /* Set up the breakpoint. */
454 gdb_assert (exec_bfd
);
455 (*bp
) = (gdbarch_convert_from_func_ptr_addr
457 /* Do proper sign extension for the target. */
458 (bfd_get_sign_extend_vma (exec_bfd
) > 0
459 ? (CORE_ADDR
) (intptr_t) notify
.u
.bptaddr
460 : (CORE_ADDR
) (uintptr_t) notify
.u
.bptaddr
),
462 create_thread_event_breakpoint ((*bp
));
468 enable_thread_event_reporting (void)
470 td_thr_events_t events
;
473 #ifdef HAVE_GNU_LIBC_VERSION_H
474 const char *libc_version
;
475 int libc_major
, libc_minor
;
478 /* We cannot use the thread event reporting facility if these
479 functions aren't available. */
480 if (td_ta_event_addr_p
== NULL
|| td_ta_set_event_p
== NULL
481 || td_ta_event_getmsg_p
== NULL
|| td_thr_event_enable_p
== NULL
)
484 /* Set the process wide mask saying which events we're interested in. */
485 td_event_emptyset (&events
);
486 td_event_addset (&events
, TD_CREATE
);
488 #ifdef HAVE_GNU_LIBC_VERSION_H
489 /* FIXME: kettenis/2000-04-23: The event reporting facility is
490 broken for TD_DEATH events in glibc 2.1.3, so don't enable it for
492 libc_version
= gnu_get_libc_version ();
493 if (sscanf (libc_version
, "%d.%d", &libc_major
, &libc_minor
) == 2
494 && (libc_major
> 2 || (libc_major
== 2 && libc_minor
> 1)))
496 td_event_addset (&events
, TD_DEATH
);
498 err
= td_ta_set_event_p (thread_agent
, &events
);
501 warning (_("Unable to set global thread event mask: %s"),
502 thread_db_err_str (err
));
506 /* Delete previous thread event breakpoints, if any. */
507 remove_thread_event_breakpoints ();
508 td_create_bp_addr
= 0;
509 td_death_bp_addr
= 0;
511 /* Set up the thread creation event. */
512 err
= enable_thread_event (thread_agent
, TD_CREATE
, &td_create_bp_addr
);
515 warning (_("Unable to get location for thread creation breakpoint: %s"),
516 thread_db_err_str (err
));
520 /* Set up the thread death event. */
521 err
= enable_thread_event (thread_agent
, TD_DEATH
, &td_death_bp_addr
);
524 warning (_("Unable to get location for thread death breakpoint: %s"),
525 thread_db_err_str (err
));
531 disable_thread_event_reporting (void)
533 td_thr_events_t events
;
535 /* Set the process wide mask saying we aren't interested in any
537 td_event_emptyset (&events
);
538 td_ta_set_event_p (thread_agent
, &events
);
540 /* Delete thread event breakpoints, if any. */
541 remove_thread_event_breakpoints ();
542 td_create_bp_addr
= 0;
543 td_death_bp_addr
= 0;
547 check_thread_signals (void)
549 #ifdef GET_THREAD_SIGNALS
555 GET_THREAD_SIGNALS (&mask
);
556 sigemptyset (&thread_stop_set
);
557 sigemptyset (&thread_print_set
);
559 for (i
= 1; i
< NSIG
; i
++)
561 if (sigismember (&mask
, i
))
563 if (signal_stop_update (target_signal_from_host (i
), 0))
564 sigaddset (&thread_stop_set
, i
);
565 if (signal_print_update (target_signal_from_host (i
), 0))
566 sigaddset (&thread_print_set
, i
);
574 /* Check whether thread_db is usable. This function is called when
575 an inferior is created (or otherwise acquired, e.g. attached to)
576 and when new shared libraries are loaded into a running process. */
579 check_for_thread_db (void)
582 static int already_loaded
;
584 /* Do nothing if we couldn't load libthread_db.so.1. */
585 if (td_ta_new_p
== NULL
)
588 /* First time through, report that libthread_db was successfuly
589 loaded. Can't print this in in thread_db_load as, at that stage,
590 the interpreter and it's console haven't started. */
595 const char *library
= NULL
;
596 if (dladdr ((*td_ta_new_p
), &info
) != 0)
597 library
= info
.dli_fname
;
602 /* Paranoid - don't let a NULL path slip through. */
603 library
= LIBTHREAD_DB_SO
;
605 printf_unfiltered (_("Using host libthread_db library \"%s\".\n"),
611 /* Nothing to do. The thread library was already detected and the
612 target vector was already activated. */
615 /* Don't attempt to use thread_db on targets which can not run
616 (executables not running yet, core files) for now. */
617 if (!target_has_execution
)
620 /* Don't attempt to use thread_db for remote targets. */
621 if (!target_can_run (¤t_target
))
624 /* Initialize the structure that identifies the child process. */
625 proc_handle
.pid
= GET_PID (inferior_ptid
);
627 /* Now attempt to open a connection to the thread library. */
628 err
= td_ta_new_p (&proc_handle
, &thread_agent
);
632 /* No thread library was detected. */
636 printf_unfiltered (_("[Thread debugging using libthread_db enabled]\n"));
638 /* The thread library was detected. Activate the thread_db target. */
639 push_target (&thread_db_ops
);
642 enable_thread_event_reporting ();
643 thread_db_find_new_threads ();
647 warning (_("Cannot initialize thread debugging library: %s"),
648 thread_db_err_str (err
));
654 thread_db_new_objfile (struct objfile
*objfile
)
657 check_for_thread_db ();
659 if (target_new_objfile_chain
)
660 target_new_objfile_chain (objfile
);
663 /* Attach to a new thread. This function is called when we receive a
664 TD_CREATE event or when we iterate over all threads and find one
665 that wasn't already in our list. */
668 attach_thread (ptid_t ptid
, const td_thrhandle_t
*th_p
,
669 const td_thrinfo_t
*ti_p
, int verbose
)
671 struct thread_info
*tp
;
674 /* If we're being called after a TD_CREATE event, we may already
675 know about this thread. There are two ways this can happen. We
676 may have iterated over all threads between the thread creation
677 and the TD_CREATE event, for instance when the user has issued
678 the `info threads' command before the SIGTRAP for hitting the
679 thread creation breakpoint was reported. Alternatively, the
680 thread may have exited and a new one been created with the same
681 thread ID. In the first case we don't need to do anything; in
682 the second case we should discard information about the dead
683 thread and attach to the new one. */
684 if (in_thread_list (ptid
))
686 tp
= find_thread_pid (ptid
);
687 gdb_assert (tp
!= NULL
);
689 if (!tp
->private->dying
)
692 delete_thread (ptid
);
695 check_thread_signals ();
697 if (ti_p
->ti_state
== TD_THR_UNKNOWN
|| ti_p
->ti_state
== TD_THR_ZOMBIE
)
698 return; /* A zombie thread -- do not attach. */
700 /* Under GNU/Linux, we have to attach to each and every thread. */
701 if (lin_lwp_attach_lwp (BUILD_LWP (ti_p
->ti_lid
, GET_PID (ptid
)), 0) < 0)
704 /* Add the thread to GDB's thread list. */
705 tp
= add_thread (ptid
);
706 tp
->private = xmalloc (sizeof (struct private_thread_info
));
707 memset (tp
->private, 0, sizeof (struct private_thread_info
));
710 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid
));
712 /* Enable thread event reporting for this thread. */
713 err
= td_thr_event_enable_p (th_p
, 1);
715 error (_("Cannot enable thread event reporting for %s: %s"),
716 target_pid_to_str (ptid
), thread_db_err_str (err
));
720 thread_db_attach (char *args
, int from_tty
)
722 target_beneath
->to_attach (args
, from_tty
);
724 /* Destroy thread info; it's no longer valid. */
727 /* The child process is now the actual multi-threaded
728 program. Snatch its process ID... */
729 proc_handle
.pid
= GET_PID (inferior_ptid
);
731 /* ...and perform the remaining initialization steps. */
732 enable_thread_event_reporting ();
733 thread_db_find_new_threads ();
737 detach_thread (ptid_t ptid
, int verbose
)
739 struct thread_info
*thread_info
;
742 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (ptid
));
744 /* Don't delete the thread now, because it still reports as active
745 until it has executed a few instructions after the event
746 breakpoint - if we deleted it now, "info threads" would cause us
747 to re-attach to it. Just mark it as having had a TD_DEATH
748 event. This means that we won't delete it from our thread list
749 until we notice that it's dead (via prune_threads), or until
750 something re-uses its thread ID. */
751 thread_info
= find_thread_pid (ptid
);
752 gdb_assert (thread_info
!= NULL
);
753 thread_info
->private->dying
= 1;
757 thread_db_detach (char *args
, int from_tty
)
759 disable_thread_event_reporting ();
761 /* There's no need to save & restore inferior_ptid here, since the
762 inferior is supposed to be survive this function call. */
763 inferior_ptid
= lwp_from_thread (inferior_ptid
);
765 /* Forget about the child's process ID. We shouldn't need it
769 target_beneath
->to_detach (args
, from_tty
);
773 clear_lwpid_callback (struct thread_info
*thread
, void *dummy
)
775 /* If we know that our thread implementation is 1-to-1, we could save
776 a certain amount of information; it's not clear how much, so we
777 are always conservative. */
779 thread
->private->th_valid
= 0;
780 thread
->private->ti_valid
= 0;
786 thread_db_resume (ptid_t ptid
, int step
, enum target_signal signo
)
788 struct cleanup
*old_chain
= save_inferior_ptid ();
790 if (GET_PID (ptid
) == -1)
791 inferior_ptid
= lwp_from_thread (inferior_ptid
);
792 else if (is_thread (ptid
))
793 ptid
= lwp_from_thread (ptid
);
795 /* Clear cached data which may not be valid after the resume. */
796 iterate_over_threads (clear_lwpid_callback
, NULL
);
798 target_beneath
->to_resume (ptid
, step
, signo
);
800 do_cleanups (old_chain
);
803 /* Check if PID is currently stopped at the location of a thread event
804 breakpoint location. If it is, read the event message and act upon
808 check_event (ptid_t ptid
)
816 /* Bail out early if we're not at a thread event breakpoint. */
817 stop_pc
= read_pc_pid (ptid
) - DECR_PC_AFTER_BREAK
;
818 if (stop_pc
!= td_create_bp_addr
&& stop_pc
!= td_death_bp_addr
)
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
, ti
.ti_tid
);
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
, 1);
866 if (!in_thread_list (ptid
))
867 error (_("Spurious thread death event."));
869 detach_thread (ptid
, 1);
874 error (_("Spurious thread event."));
881 thread_db_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
883 extern ptid_t trap_ptid
;
885 if (GET_PID (ptid
) != -1 && is_thread (ptid
))
886 ptid
= lwp_from_thread (ptid
);
888 ptid
= target_beneath
->to_wait (ptid
, ourstatus
);
890 if (proc_handle
.pid
== 0)
891 /* The current child process isn't the actual multi-threaded
892 program yet, so don't try to do any special thread-specific
893 post-processing and bail out early. */
896 if (ourstatus
->kind
== TARGET_WAITKIND_EXITED
897 || ourstatus
->kind
== TARGET_WAITKIND_SIGNALLED
)
898 return pid_to_ptid (-1);
900 if (ourstatus
->kind
== TARGET_WAITKIND_EXECD
)
902 remove_thread_event_breakpoints ();
903 unpush_target (&thread_db_ops
);
906 return pid_to_ptid (GET_PID (ptid
));
909 if (ourstatus
->kind
== TARGET_WAITKIND_STOPPED
910 && ourstatus
->value
.sig
== TARGET_SIGNAL_TRAP
)
911 /* Check for a thread event. */
914 if (!ptid_equal (trap_ptid
, null_ptid
))
915 trap_ptid
= thread_from_lwp (trap_ptid
);
917 /* Change the ptid back into the higher level PID + TID format.
918 If the thread is dead and no longer on the thread list, we will
919 get back a dead ptid. This can occur if the thread death event
920 gets postponed by other simultaneous events. In such a case,
921 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
;
930 thread_db_xfer_partial (struct target_ops
*ops
, enum target_object object
,
931 const char *annex
, gdb_byte
*readbuf
,
932 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
934 struct cleanup
*old_chain
= save_inferior_ptid ();
937 if (is_thread (inferior_ptid
))
939 /* FIXME: This seems to be necessary to make sure breakpoints
941 if (!target_thread_alive (inferior_ptid
))
942 inferior_ptid
= pid_to_ptid (GET_PID (inferior_ptid
));
944 inferior_ptid
= lwp_from_thread (inferior_ptid
);
947 xfer
= target_beneath
->to_xfer_partial (ops
, object
, annex
,
948 readbuf
, writebuf
, offset
, len
);
950 do_cleanups (old_chain
);
955 thread_db_kill (void)
957 /* There's no need to save & restore inferior_ptid here, since the
958 inferior isn't supposed to survive this function call. */
959 inferior_ptid
= lwp_from_thread (inferior_ptid
);
960 target_beneath
->to_kill ();
964 thread_db_create_inferior (char *exec_file
, char *allargs
, char **env
,
967 unpush_target (&thread_db_ops
);
969 target_beneath
->to_create_inferior (exec_file
, allargs
, env
, from_tty
);
973 thread_db_post_startup_inferior (ptid_t ptid
)
975 if (proc_handle
.pid
== 0)
977 /* The child process is now the actual multi-threaded
978 program. Snatch its process ID... */
979 proc_handle
.pid
= GET_PID (ptid
);
981 /* ...and perform the remaining initialization steps. */
982 enable_thread_event_reporting ();
983 thread_db_find_new_threads ();
988 thread_db_mourn_inferior (void)
990 /* Forget about the child's process ID. We shouldn't need it
994 target_beneath
->to_mourn_inferior ();
996 /* Delete the old thread event breakpoints. Do this after mourning
997 the inferior, so that we don't try to uninsert them. */
998 remove_thread_event_breakpoints ();
1000 /* Detach thread_db target ops. */
1001 unpush_target (&thread_db_ops
);
1002 using_thread_db
= 0;
1006 find_new_threads_callback (const td_thrhandle_t
*th_p
, void *data
)
1012 err
= td_thr_get_info_p (th_p
, &ti
);
1014 error (_("find_new_threads_callback: cannot get thread info: %s"),
1015 thread_db_err_str (err
));
1017 if (ti
.ti_state
== TD_THR_UNKNOWN
|| ti
.ti_state
== TD_THR_ZOMBIE
)
1018 return 0; /* A zombie -- ignore. */
1020 ptid
= ptid_build (GET_PID (inferior_ptid
), ti
.ti_lid
, ti
.ti_tid
);
1022 if (!in_thread_list (ptid
))
1023 attach_thread (ptid
, th_p
, &ti
, 1);
1029 thread_db_find_new_threads (void)
1033 /* Iterate over all user-space threads to discover new threads. */
1034 err
= td_ta_thr_iter_p (thread_agent
, find_new_threads_callback
, NULL
,
1035 TD_THR_ANY_STATE
, TD_THR_LOWEST_PRIORITY
,
1036 TD_SIGNO_MASK
, TD_THR_ANY_USER_FLAGS
);
1038 error (_("Cannot find new threads: %s"), thread_db_err_str (err
));
1042 thread_db_pid_to_str (ptid_t ptid
)
1044 if (is_thread (ptid
))
1046 static char buf
[64];
1047 struct thread_info
*thread_info
;
1049 thread_info
= find_thread_pid (ptid
);
1050 if (thread_info
== NULL
)
1051 snprintf (buf
, sizeof (buf
), "Thread %ld (LWP %ld) (Missing)",
1052 GET_THREAD (ptid
), GET_LWP (ptid
));
1054 snprintf (buf
, sizeof (buf
), "Thread %ld (LWP %ld)",
1055 GET_THREAD (ptid
), GET_LWP (ptid
));
1060 if (target_beneath
->to_pid_to_str (ptid
))
1061 return target_beneath
->to_pid_to_str (ptid
);
1063 return normal_pid_to_str (ptid
);
1066 /* Return a string describing the state of the thread specified by
1070 thread_db_extra_thread_info (struct thread_info
*info
)
1072 if (info
->private->dying
)
1078 /* Get the address of the thread local variable in load module LM which
1079 is stored at OFFSET within the thread local storage for thread PTID. */
1082 thread_db_get_thread_local_address (ptid_t ptid
,
1086 if (is_thread (ptid
))
1090 struct thread_info
*thread_info
;
1092 /* glibc doesn't provide the needed interface. */
1093 if (!td_thr_tls_get_addr_p
)
1094 throw_error (TLS_NO_LIBRARY_SUPPORT_ERROR
,
1095 _("No TLS library support"));
1097 /* Caller should have verified that lm != 0. */
1098 gdb_assert (lm
!= 0);
1100 /* Get info about the thread. */
1101 thread_info
= find_thread_pid (ptid
);
1102 thread_db_map_id2thr (thread_info
, 1);
1104 /* Finally, get the address of the variable. */
1105 err
= td_thr_tls_get_addr_p (&thread_info
->private->th
,
1106 (void *)(size_t) lm
,
1109 #ifdef THREAD_DB_HAS_TD_NOTALLOC
1110 /* The memory hasn't been allocated, yet. */
1111 if (err
== TD_NOTALLOC
)
1112 /* Now, if libthread_db provided the initialization image's
1113 address, we *could* try to build a non-lvalue value from
1114 the initialization image. */
1115 throw_error (TLS_NOT_ALLOCATED_YET_ERROR
,
1116 _("TLS not allocated yet"));
1119 /* Something else went wrong. */
1121 throw_error (TLS_GENERIC_ERROR
,
1122 (("%s")), thread_db_err_str (err
));
1124 /* Cast assuming host == target. Joy. */
1125 /* Do proper sign extension for the target. */
1126 gdb_assert (exec_bfd
);
1127 return (bfd_get_sign_extend_vma (exec_bfd
) > 0
1128 ? (CORE_ADDR
) (intptr_t) address
1129 : (CORE_ADDR
) (uintptr_t) address
);
1132 if (target_beneath
->to_get_thread_local_address
)
1133 return target_beneath
->to_get_thread_local_address (ptid
, lm
, offset
);
1135 throw_error (TLS_GENERIC_ERROR
,
1136 _("TLS not supported on this target"));
1140 init_thread_db_ops (void)
1142 thread_db_ops
.to_shortname
= "multi-thread";
1143 thread_db_ops
.to_longname
= "multi-threaded child process.";
1144 thread_db_ops
.to_doc
= "Threads and pthreads support.";
1145 thread_db_ops
.to_attach
= thread_db_attach
;
1146 thread_db_ops
.to_detach
= thread_db_detach
;
1147 thread_db_ops
.to_resume
= thread_db_resume
;
1148 thread_db_ops
.to_wait
= thread_db_wait
;
1149 thread_db_ops
.to_xfer_partial
= thread_db_xfer_partial
;
1150 thread_db_ops
.to_kill
= thread_db_kill
;
1151 thread_db_ops
.to_create_inferior
= thread_db_create_inferior
;
1152 thread_db_ops
.to_post_startup_inferior
= thread_db_post_startup_inferior
;
1153 thread_db_ops
.to_mourn_inferior
= thread_db_mourn_inferior
;
1154 thread_db_ops
.to_find_new_threads
= thread_db_find_new_threads
;
1155 thread_db_ops
.to_pid_to_str
= thread_db_pid_to_str
;
1156 thread_db_ops
.to_stratum
= thread_stratum
;
1157 thread_db_ops
.to_has_thread_control
= tc_schedlock
;
1158 thread_db_ops
.to_get_thread_local_address
1159 = thread_db_get_thread_local_address
;
1160 thread_db_ops
.to_extra_thread_info
= thread_db_extra_thread_info
;
1161 thread_db_ops
.to_magic
= OPS_MAGIC
;
1165 _initialize_thread_db (void)
1167 /* Only initialize the module if we can load libthread_db. */
1168 if (thread_db_load ())
1170 init_thread_db_ops ();
1171 add_target (&thread_db_ops
);
1173 /* Add ourselves to objfile event chain. */
1174 target_new_objfile_chain
= deprecated_target_new_objfile_hook
;
1175 deprecated_target_new_objfile_hook
= thread_db_new_objfile
;