1 /* libthread_db assisted debugging support, generic parts.
3 Copyright (C) 1999, 2000, 2001, 2003, 2004, 2005, 2006
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"
42 #ifdef HAVE_GNU_LIBC_VERSION_H
43 #include <gnu/libc-version.h>
46 #ifndef LIBTHREAD_DB_SO
47 #define LIBTHREAD_DB_SO "libthread_db.so.1"
50 /* If we're running on GNU/Linux, we must explicitly attach to any new
53 /* FIXME: There is certainly some room for improvements:
55 - Bypass libthread_db when fetching or storing registers for
56 threads bound to a LWP. */
58 /* This module's target vector. */
59 static struct target_ops thread_db_ops
;
61 /* The target vector that we call for things this module can't handle. */
62 static struct target_ops
*target_beneath
;
64 /* Pointer to the next function on the objfile event chain. */
65 static void (*target_new_objfile_chain
) (struct objfile
* objfile
);
67 /* Non-zero if we're using this module's target vector. */
68 static int using_thread_db
;
70 /* Non-zero if we have determined the signals used by the threads
72 static int thread_signals
;
73 static sigset_t thread_stop_set
;
74 static sigset_t thread_print_set
;
76 /* Structure that identifies the child process for the
77 <proc_service.h> interface. */
78 static struct ps_prochandle proc_handle
;
80 /* Connection to the libthread_db library. */
81 static td_thragent_t
*thread_agent
;
83 /* Pointers to the libthread_db functions. */
85 static td_err_e (*td_init_p
) (void);
87 static td_err_e (*td_ta_new_p
) (struct ps_prochandle
* ps
,
89 static td_err_e (*td_ta_map_id2thr_p
) (const td_thragent_t
*ta
, thread_t pt
,
90 td_thrhandle_t
*__th
);
91 static td_err_e (*td_ta_map_lwp2thr_p
) (const td_thragent_t
*ta
,
92 lwpid_t lwpid
, td_thrhandle_t
*th
);
93 static td_err_e (*td_ta_thr_iter_p
) (const td_thragent_t
*ta
,
94 td_thr_iter_f
*callback
, void *cbdata_p
,
95 td_thr_state_e state
, int ti_pri
,
96 sigset_t
*ti_sigmask_p
,
97 unsigned int ti_user_flags
);
98 static td_err_e (*td_ta_event_addr_p
) (const td_thragent_t
*ta
,
99 td_event_e event
, td_notify_t
*ptr
);
100 static td_err_e (*td_ta_set_event_p
) (const td_thragent_t
*ta
,
101 td_thr_events_t
*event
);
102 static td_err_e (*td_ta_event_getmsg_p
) (const td_thragent_t
*ta
,
103 td_event_msg_t
*msg
);
105 static td_err_e (*td_thr_validate_p
) (const td_thrhandle_t
*th
);
106 static td_err_e (*td_thr_get_info_p
) (const td_thrhandle_t
*th
,
107 td_thrinfo_t
*infop
);
108 static td_err_e (*td_thr_event_enable_p
) (const td_thrhandle_t
*th
,
111 static td_err_e (*td_thr_tls_get_addr_p
) (const td_thrhandle_t
*th
,
113 size_t offset
, void **address
);
115 /* Location of the thread creation event breakpoint. The code at this
116 location in the child process will be called by the pthread library
117 whenever a new thread is created. By setting a special breakpoint
118 at this location, GDB can detect when a new thread is created. We
119 obtain this location via the td_ta_event_addr call. */
120 static CORE_ADDR td_create_bp_addr
;
122 /* Location of the thread death event breakpoint. */
123 static CORE_ADDR td_death_bp_addr
;
125 /* Prototypes for local functions. */
126 static void thread_db_find_new_threads (void);
127 static void attach_thread (ptid_t ptid
, const td_thrhandle_t
*th_p
,
128 const td_thrinfo_t
*ti_p
, int verbose
);
129 static void detach_thread (ptid_t ptid
, int verbose
);
132 /* Building process ids. */
134 #define GET_PID(ptid) ptid_get_pid (ptid)
135 #define GET_LWP(ptid) ptid_get_lwp (ptid)
136 #define GET_THREAD(ptid) ptid_get_tid (ptid)
138 #define is_lwp(ptid) (GET_LWP (ptid) != 0)
139 #define is_thread(ptid) (GET_THREAD (ptid) != 0)
141 #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
144 /* Use "struct private_thread_info" to cache thread state. This is
145 a substantial optimization. */
147 struct private_thread_info
149 /* Flag set when we see a TD_DEATH event for this thread. */
150 unsigned int dying
:1;
152 /* Cached thread state. */
153 unsigned int th_valid
:1;
154 unsigned int ti_valid
:1;
162 thread_db_err_str (td_err_e err
)
169 return "generic 'call succeeded'";
171 return "generic error";
173 return "no thread to satisfy query";
175 return "no sync handle to satisfy query";
177 return "no LWP to satisfy query";
179 return "invalid process handle";
181 return "invalid thread handle";
183 return "invalid synchronization handle";
185 return "invalid thread agent";
187 return "invalid key";
189 return "no event message for getmsg";
191 return "FPU register set not available";
193 return "application not linked with libthread";
195 return "requested event is not supported";
197 return "capability not available";
199 return "debugger service failed";
201 return "operation not applicable to";
203 return "no thread-specific data for this thread";
205 return "malloc failed";
207 return "only part of register set was written/read";
209 return "X register set not available for this thread";
211 snprintf (buf
, sizeof (buf
), "unknown thread_db error '%d'", err
);
216 /* A callback function for td_ta_thr_iter, which we use to map all
219 THP is a handle to the current thread; if INFOP is not NULL, the
220 struct thread_info associated with this thread is returned in
223 If the thread is a zombie, TD_THR_ZOMBIE is returned. Otherwise,
224 zero is returned to indicate success. */
227 thread_get_info_callback (const td_thrhandle_t
*thp
, void *infop
)
231 struct thread_info
*thread_info
;
234 err
= td_thr_get_info_p (thp
, &ti
);
236 error (_("thread_get_info_callback: cannot get thread info: %s"),
237 thread_db_err_str (err
));
239 /* Fill the cache. */
240 thread_ptid
= ptid_build (GET_PID (inferior_ptid
), ti
.ti_lid
, ti
.ti_tid
);
241 thread_info
= find_thread_pid (thread_ptid
);
243 /* In the case of a zombie thread, don't continue. We don't want to
244 attach to it thinking it is a new thread. */
245 if (ti
.ti_state
== TD_THR_UNKNOWN
|| ti
.ti_state
== TD_THR_ZOMBIE
)
248 *(struct thread_info
**) infop
= thread_info
;
249 if (thread_info
!= NULL
)
251 memcpy (&thread_info
->private->th
, thp
, sizeof (*thp
));
252 thread_info
->private->th_valid
= 1;
253 memcpy (&thread_info
->private->ti
, &ti
, sizeof (ti
));
254 thread_info
->private->ti_valid
= 1;
256 return TD_THR_ZOMBIE
;
259 if (thread_info
== NULL
)
261 /* New thread. Attach to it now (why wait?). */
262 attach_thread (thread_ptid
, thp
, &ti
, 1);
263 thread_info
= find_thread_pid (thread_ptid
);
264 gdb_assert (thread_info
!= NULL
);
267 memcpy (&thread_info
->private->th
, thp
, sizeof (*thp
));
268 thread_info
->private->th_valid
= 1;
269 memcpy (&thread_info
->private->ti
, &ti
, sizeof (ti
));
270 thread_info
->private->ti_valid
= 1;
273 *(struct thread_info
**) infop
= thread_info
;
278 /* Accessor functions for the thread_db information, with caching. */
281 thread_db_map_id2thr (struct thread_info
*thread_info
, int fatal
)
285 if (thread_info
->private->th_valid
)
288 err
= td_ta_map_id2thr_p (thread_agent
, GET_THREAD (thread_info
->ptid
),
289 &thread_info
->private->th
);
293 error (_("Cannot find thread %ld: %s"),
294 (long) GET_THREAD (thread_info
->ptid
),
295 thread_db_err_str (err
));
298 thread_info
->private->th_valid
= 1;
301 /* Convert between user-level thread ids and LWP ids. */
304 thread_from_lwp (ptid_t ptid
)
308 struct thread_info
*thread_info
;
311 if (GET_LWP (ptid
) == 0)
312 ptid
= BUILD_LWP (GET_PID (ptid
), GET_PID (ptid
));
314 gdb_assert (is_lwp (ptid
));
316 err
= td_ta_map_lwp2thr_p (thread_agent
, GET_LWP (ptid
), &th
);
318 error (_("Cannot find user-level thread for LWP %ld: %s"),
319 GET_LWP (ptid
), thread_db_err_str (err
));
323 /* Fetch the thread info. If we get back TD_THR_ZOMBIE, then the
324 event thread has already died. If another gdb interface has called
325 thread_alive() previously, the thread won't be found on the thread list
326 anymore. In that case, we don't want to process this ptid anymore
327 to avoid the possibility of later treating it as a newly
328 discovered thread id that we should add to the list. Thus,
329 we return a -1 ptid which is also how the thread list marks a
331 if (thread_get_info_callback (&th
, &thread_info
) == TD_THR_ZOMBIE
332 && thread_info
== NULL
)
333 return pid_to_ptid (-1);
335 gdb_assert (thread_info
&& thread_info
->private->ti_valid
);
337 return ptid_build (GET_PID (ptid
), GET_LWP (ptid
),
338 thread_info
->private->ti
.ti_tid
);
342 lwp_from_thread (ptid_t ptid
)
344 return BUILD_LWP (GET_LWP (ptid
), GET_PID (ptid
));
349 thread_db_init (struct target_ops
*target
)
351 target_beneath
= target
;
355 verbose_dlsym (void *handle
, const char *name
)
357 void *sym
= dlsym (handle
, name
);
359 warning (_("Symbol \"%s\" not found in libthread_db: %s"), name
, dlerror ());
364 thread_db_load (void)
369 handle
= dlopen (LIBTHREAD_DB_SO
, RTLD_NOW
);
372 fprintf_filtered (gdb_stderr
, "\n\ndlopen failed on '%s' - %s\n",
373 LIBTHREAD_DB_SO
, dlerror ());
374 fprintf_filtered (gdb_stderr
,
375 "GDB will not be able to debug pthreads.\n\n");
379 /* Initialize pointers to the dynamic library functions we will use.
380 Essential functions first. */
382 td_init_p
= verbose_dlsym (handle
, "td_init");
383 if (td_init_p
== NULL
)
386 td_ta_new_p
= verbose_dlsym (handle
, "td_ta_new");
387 if (td_ta_new_p
== NULL
)
390 td_ta_map_id2thr_p
= verbose_dlsym (handle
, "td_ta_map_id2thr");
391 if (td_ta_map_id2thr_p
== NULL
)
394 td_ta_map_lwp2thr_p
= verbose_dlsym (handle
, "td_ta_map_lwp2thr");
395 if (td_ta_map_lwp2thr_p
== NULL
)
398 td_ta_thr_iter_p
= verbose_dlsym (handle
, "td_ta_thr_iter");
399 if (td_ta_thr_iter_p
== NULL
)
402 td_thr_validate_p
= verbose_dlsym (handle
, "td_thr_validate");
403 if (td_thr_validate_p
== NULL
)
406 td_thr_get_info_p
= verbose_dlsym (handle
, "td_thr_get_info");
407 if (td_thr_get_info_p
== NULL
)
410 /* Initialize the library. */
414 warning (_("Cannot initialize libthread_db: %s"), thread_db_err_str (err
));
418 /* These are not essential. */
419 td_ta_event_addr_p
= dlsym (handle
, "td_ta_event_addr");
420 td_ta_set_event_p
= dlsym (handle
, "td_ta_set_event");
421 td_ta_event_getmsg_p
= dlsym (handle
, "td_ta_event_getmsg");
422 td_thr_event_enable_p
= dlsym (handle
, "td_thr_event_enable");
423 td_thr_tls_get_addr_p
= dlsym (handle
, "td_thr_tls_get_addr");
429 enable_thread_event (td_thragent_t
*thread_agent
, int event
, CORE_ADDR
*bp
)
434 /* Get the breakpoint address for thread EVENT. */
435 err
= td_ta_event_addr_p (thread_agent
, event
, ¬ify
);
439 /* Set up the breakpoint. */
440 gdb_assert (exec_bfd
);
441 (*bp
) = (gdbarch_convert_from_func_ptr_addr
443 /* Do proper sign extension for the target. */
444 (bfd_get_sign_extend_vma (exec_bfd
) > 0
445 ? (CORE_ADDR
) (intptr_t) notify
.u
.bptaddr
446 : (CORE_ADDR
) (uintptr_t) notify
.u
.bptaddr
),
448 create_thread_event_breakpoint ((*bp
));
454 enable_thread_event_reporting (void)
456 td_thr_events_t events
;
459 #ifdef HAVE_GNU_LIBC_VERSION_H
460 const char *libc_version
;
461 int libc_major
, libc_minor
;
464 /* We cannot use the thread event reporting facility if these
465 functions aren't available. */
466 if (td_ta_event_addr_p
== NULL
|| td_ta_set_event_p
== NULL
467 || td_ta_event_getmsg_p
== NULL
|| td_thr_event_enable_p
== NULL
)
470 /* Set the process wide mask saying which events we're interested in. */
471 td_event_emptyset (&events
);
472 td_event_addset (&events
, TD_CREATE
);
474 #ifdef HAVE_GNU_LIBC_VERSION_H
475 /* FIXME: kettenis/2000-04-23: The event reporting facility is
476 broken for TD_DEATH events in glibc 2.1.3, so don't enable it for
478 libc_version
= gnu_get_libc_version ();
479 if (sscanf (libc_version
, "%d.%d", &libc_major
, &libc_minor
) == 2
480 && (libc_major
> 2 || (libc_major
== 2 && libc_minor
> 1)))
482 td_event_addset (&events
, TD_DEATH
);
484 err
= td_ta_set_event_p (thread_agent
, &events
);
487 warning (_("Unable to set global thread event mask: %s"),
488 thread_db_err_str (err
));
492 /* Delete previous thread event breakpoints, if any. */
493 remove_thread_event_breakpoints ();
494 td_create_bp_addr
= 0;
495 td_death_bp_addr
= 0;
497 /* Set up the thread creation event. */
498 err
= enable_thread_event (thread_agent
, TD_CREATE
, &td_create_bp_addr
);
501 warning (_("Unable to get location for thread creation breakpoint: %s"),
502 thread_db_err_str (err
));
506 /* Set up the thread death event. */
507 err
= enable_thread_event (thread_agent
, TD_DEATH
, &td_death_bp_addr
);
510 warning (_("Unable to get location for thread death breakpoint: %s"),
511 thread_db_err_str (err
));
517 disable_thread_event_reporting (void)
519 td_thr_events_t events
;
521 /* Set the process wide mask saying we aren't interested in any
523 td_event_emptyset (&events
);
524 td_ta_set_event_p (thread_agent
, &events
);
526 /* Delete thread event breakpoints, if any. */
527 remove_thread_event_breakpoints ();
528 td_create_bp_addr
= 0;
529 td_death_bp_addr
= 0;
533 check_thread_signals (void)
535 #ifdef GET_THREAD_SIGNALS
541 GET_THREAD_SIGNALS (&mask
);
542 sigemptyset (&thread_stop_set
);
543 sigemptyset (&thread_print_set
);
545 for (i
= 1; i
< NSIG
; i
++)
547 if (sigismember (&mask
, i
))
549 if (signal_stop_update (target_signal_from_host (i
), 0))
550 sigaddset (&thread_stop_set
, i
);
551 if (signal_print_update (target_signal_from_host (i
), 0))
552 sigaddset (&thread_print_set
, i
);
560 /* Check whether thread_db is usable. This function is called when
561 an inferior is created (or otherwise acquired, e.g. attached to)
562 and when new shared libraries are loaded into a running process. */
565 check_for_thread_db (void)
568 static int already_loaded
;
570 /* First time through, report that libthread_db was successfuly
571 loaded. Can't print this in in thread_db_load as, at that stage,
572 the interpreter and it's console haven't started. */
577 const char *library
= NULL
;
578 if (dladdr ((*td_ta_new_p
), &info
) != 0)
579 library
= info
.dli_fname
;
584 /* Paranoid - don't let a NULL path slip through. */
585 library
= LIBTHREAD_DB_SO
;
587 printf_unfiltered (_("Using host libthread_db library \"%s\".\n"),
593 /* Nothing to do. The thread library was already detected and the
594 target vector was already activated. */
597 /* Don't attempt to use thread_db on targets which can not run
598 (executables not running yet, core files) for now. */
599 if (!target_has_execution
)
602 /* Don't attempt to use thread_db for remote targets. */
603 if (!target_can_run (¤t_target
))
606 /* Initialize the structure that identifies the child process. */
607 proc_handle
.pid
= GET_PID (inferior_ptid
);
609 /* Now attempt to open a connection to the thread library. */
610 err
= td_ta_new_p (&proc_handle
, &thread_agent
);
614 /* No thread library was detected. */
618 printf_unfiltered (_("[Thread debugging using libthread_db enabled]\n"));
620 /* The thread library was detected. Activate the thread_db target. */
621 push_target (&thread_db_ops
);
624 enable_thread_event_reporting ();
625 thread_db_find_new_threads ();
629 warning (_("Cannot initialize thread debugging library: %s"),
630 thread_db_err_str (err
));
636 thread_db_new_objfile (struct objfile
*objfile
)
639 check_for_thread_db ();
641 if (target_new_objfile_chain
)
642 target_new_objfile_chain (objfile
);
645 /* Attach to a new thread. This function is called when we receive a
646 TD_CREATE event or when we iterate over all threads and find one
647 that wasn't already in our list. */
650 attach_thread (ptid_t ptid
, const td_thrhandle_t
*th_p
,
651 const td_thrinfo_t
*ti_p
, int verbose
)
653 struct thread_info
*tp
;
656 /* If we're being called after a TD_CREATE event, we may already
657 know about this thread. There are two ways this can happen. We
658 may have iterated over all threads between the thread creation
659 and the TD_CREATE event, for instance when the user has issued
660 the `info threads' command before the SIGTRAP for hitting the
661 thread creation breakpoint was reported. Alternatively, the
662 thread may have exited and a new one been created with the same
663 thread ID. In the first case we don't need to do anything; in
664 the second case we should discard information about the dead
665 thread and attach to the new one. */
666 if (in_thread_list (ptid
))
668 tp
= find_thread_pid (ptid
);
669 gdb_assert (tp
!= NULL
);
671 if (!tp
->private->dying
)
674 delete_thread (ptid
);
677 check_thread_signals ();
679 /* Add the thread to GDB's thread list. */
680 tp
= add_thread (ptid
);
681 tp
->private = xmalloc (sizeof (struct private_thread_info
));
682 memset (tp
->private, 0, sizeof (struct private_thread_info
));
685 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid
));
687 if (ti_p
->ti_state
== TD_THR_UNKNOWN
|| ti_p
->ti_state
== TD_THR_ZOMBIE
)
688 return; /* A zombie thread -- do not attach. */
690 /* Under GNU/Linux, we have to attach to each and every thread. */
692 ATTACH_LWP (BUILD_LWP (ti_p
->ti_lid
, GET_PID (ptid
)), 0);
695 /* Enable thread event reporting for this thread. */
696 err
= td_thr_event_enable_p (th_p
, 1);
698 error (_("Cannot enable thread event reporting for %s: %s"),
699 target_pid_to_str (ptid
), thread_db_err_str (err
));
703 thread_db_attach (char *args
, int from_tty
)
705 target_beneath
->to_attach (args
, from_tty
);
707 /* Destroy thread info; it's no longer valid. */
710 /* The child process is now the actual multi-threaded
711 program. Snatch its process ID... */
712 proc_handle
.pid
= GET_PID (inferior_ptid
);
714 /* ...and perform the remaining initialization steps. */
715 enable_thread_event_reporting ();
716 thread_db_find_new_threads ();
720 detach_thread (ptid_t ptid
, int verbose
)
722 struct thread_info
*thread_info
;
725 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (ptid
));
727 /* Don't delete the thread now, because it still reports as active
728 until it has executed a few instructions after the event
729 breakpoint - if we deleted it now, "info threads" would cause us
730 to re-attach to it. Just mark it as having had a TD_DEATH
731 event. This means that we won't delete it from our thread list
732 until we notice that it's dead (via prune_threads), or until
733 something re-uses its thread ID. */
734 thread_info
= find_thread_pid (ptid
);
735 gdb_assert (thread_info
!= NULL
);
736 thread_info
->private->dying
= 1;
740 thread_db_detach (char *args
, int from_tty
)
742 disable_thread_event_reporting ();
744 /* There's no need to save & restore inferior_ptid here, since the
745 inferior is supposed to be survive this function call. */
746 inferior_ptid
= lwp_from_thread (inferior_ptid
);
748 /* Forget about the child's process ID. We shouldn't need it
752 target_beneath
->to_detach (args
, from_tty
);
756 clear_lwpid_callback (struct thread_info
*thread
, void *dummy
)
758 /* If we know that our thread implementation is 1-to-1, we could save
759 a certain amount of information; it's not clear how much, so we
760 are always conservative. */
762 thread
->private->th_valid
= 0;
763 thread
->private->ti_valid
= 0;
769 thread_db_resume (ptid_t ptid
, int step
, enum target_signal signo
)
771 struct cleanup
*old_chain
= save_inferior_ptid ();
773 if (GET_PID (ptid
) == -1)
774 inferior_ptid
= lwp_from_thread (inferior_ptid
);
775 else if (is_thread (ptid
))
776 ptid
= lwp_from_thread (ptid
);
778 /* Clear cached data which may not be valid after the resume. */
779 iterate_over_threads (clear_lwpid_callback
, NULL
);
781 target_beneath
->to_resume (ptid
, step
, signo
);
783 do_cleanups (old_chain
);
786 /* Check if PID is currently stopped at the location of a thread event
787 breakpoint location. If it is, read the event message and act upon
791 check_event (ptid_t ptid
)
799 /* Bail out early if we're not at a thread event breakpoint. */
800 stop_pc
= read_pc_pid (ptid
) - DECR_PC_AFTER_BREAK
;
801 if (stop_pc
!= td_create_bp_addr
&& stop_pc
!= td_death_bp_addr
)
804 /* If we are at a create breakpoint, we do not know what new lwp
805 was created and cannot specifically locate the event message for it.
806 We have to call td_ta_event_getmsg() to get
807 the latest message. Since we have no way of correlating whether
808 the event message we get back corresponds to our breakpoint, we must
809 loop and read all event messages, processing them appropriately.
810 This guarantees we will process the correct message before continuing
813 Currently, death events are not enabled. If they are enabled,
814 the death event can use the td_thr_event_getmsg() interface to
815 get the message specifically for that lwp and avoid looping
822 err
= td_ta_event_getmsg_p (thread_agent
, &msg
);
828 error (_("Cannot get thread event message: %s"),
829 thread_db_err_str (err
));
832 err
= td_thr_get_info_p (msg
.th_p
, &ti
);
834 error (_("Cannot get thread info: %s"), thread_db_err_str (err
));
836 ptid
= ptid_build (GET_PID (ptid
), ti
.ti_lid
, ti
.ti_tid
);
841 /* Call attach_thread whether or not we already know about a
842 thread with this thread ID. */
843 attach_thread (ptid
, msg
.th_p
, &ti
, 1);
849 if (!in_thread_list (ptid
))
850 error (_("Spurious thread death event."));
852 detach_thread (ptid
, 1);
857 error (_("Spurious thread event."));
864 thread_db_wait (ptid_t ptid
, struct target_waitstatus
*ourstatus
)
866 extern ptid_t trap_ptid
;
868 if (GET_PID (ptid
) != -1 && is_thread (ptid
))
869 ptid
= lwp_from_thread (ptid
);
871 ptid
= target_beneath
->to_wait (ptid
, ourstatus
);
873 if (proc_handle
.pid
== 0)
874 /* The current child process isn't the actual multi-threaded
875 program yet, so don't try to do any special thread-specific
876 post-processing and bail out early. */
879 if (ourstatus
->kind
== TARGET_WAITKIND_EXITED
880 || ourstatus
->kind
== TARGET_WAITKIND_SIGNALLED
)
881 return pid_to_ptid (-1);
883 if (ourstatus
->kind
== TARGET_WAITKIND_EXECD
)
885 remove_thread_event_breakpoints ();
886 unpush_target (&thread_db_ops
);
889 return pid_to_ptid (GET_PID (ptid
));
892 if (ourstatus
->kind
== TARGET_WAITKIND_STOPPED
893 && ourstatus
->value
.sig
== TARGET_SIGNAL_TRAP
)
894 /* Check for a thread event. */
897 if (!ptid_equal (trap_ptid
, null_ptid
))
898 trap_ptid
= thread_from_lwp (trap_ptid
);
900 /* Change the ptid back into the higher level PID + TID format.
901 If 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 event
903 gets postponed by other simultaneous events. In such a case,
904 we want to just ignore the event and continue on. */
905 ptid
= thread_from_lwp (ptid
);
906 if (GET_PID (ptid
) == -1)
907 ourstatus
->kind
= TARGET_WAITKIND_SPURIOUS
;
913 thread_db_xfer_partial (struct target_ops
*ops
, enum target_object object
,
914 const char *annex
, gdb_byte
*readbuf
,
915 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
917 struct cleanup
*old_chain
= save_inferior_ptid ();
920 if (is_thread (inferior_ptid
))
922 /* FIXME: This seems to be necessary to make sure breakpoints
924 if (!target_thread_alive (inferior_ptid
))
925 inferior_ptid
= pid_to_ptid (GET_PID (inferior_ptid
));
927 inferior_ptid
= lwp_from_thread (inferior_ptid
);
930 xfer
= target_beneath
->to_xfer_partial (ops
, object
, annex
,
931 readbuf
, writebuf
, offset
, len
);
933 do_cleanups (old_chain
);
938 thread_db_kill (void)
940 /* There's no need to save & restore inferior_ptid here, since the
941 inferior isn't supposed to survive this function call. */
942 inferior_ptid
= lwp_from_thread (inferior_ptid
);
943 target_beneath
->to_kill ();
947 thread_db_create_inferior (char *exec_file
, char *allargs
, char **env
,
950 unpush_target (&thread_db_ops
);
952 target_beneath
->to_create_inferior (exec_file
, allargs
, env
, from_tty
);
956 thread_db_post_startup_inferior (ptid_t ptid
)
958 if (proc_handle
.pid
== 0)
960 /* The child process is now the actual multi-threaded
961 program. Snatch its process ID... */
962 proc_handle
.pid
= GET_PID (ptid
);
964 /* ...and perform the remaining initialization steps. */
965 enable_thread_event_reporting ();
966 thread_db_find_new_threads ();
971 thread_db_mourn_inferior (void)
973 /* Forget about the child's process ID. We shouldn't need it
977 target_beneath
->to_mourn_inferior ();
979 /* Delete the old thread event breakpoints. Do this after mourning
980 the inferior, so that we don't try to uninsert them. */
981 remove_thread_event_breakpoints ();
983 /* Detach thread_db target ops. */
984 unpush_target (&thread_db_ops
);
989 find_new_threads_callback (const td_thrhandle_t
*th_p
, void *data
)
995 err
= td_thr_get_info_p (th_p
, &ti
);
997 error (_("find_new_threads_callback: cannot get thread info: %s"),
998 thread_db_err_str (err
));
1000 if (ti
.ti_state
== TD_THR_UNKNOWN
|| ti
.ti_state
== TD_THR_ZOMBIE
)
1001 return 0; /* A zombie -- ignore. */
1003 ptid
= ptid_build (GET_PID (inferior_ptid
), ti
.ti_lid
, ti
.ti_tid
);
1005 if (!in_thread_list (ptid
))
1006 attach_thread (ptid
, th_p
, &ti
, 1);
1012 thread_db_find_new_threads (void)
1016 /* Iterate over all user-space threads to discover new threads. */
1017 err
= td_ta_thr_iter_p (thread_agent
, find_new_threads_callback
, NULL
,
1018 TD_THR_ANY_STATE
, TD_THR_LOWEST_PRIORITY
,
1019 TD_SIGNO_MASK
, TD_THR_ANY_USER_FLAGS
);
1021 error (_("Cannot find new threads: %s"), thread_db_err_str (err
));
1025 thread_db_pid_to_str (ptid_t ptid
)
1027 if (is_thread (ptid
))
1029 static char buf
[64];
1030 struct thread_info
*thread_info
;
1032 thread_info
= find_thread_pid (ptid
);
1033 if (thread_info
== NULL
)
1034 snprintf (buf
, sizeof (buf
), "Thread %ld (LWP %ld) (Missing)",
1035 GET_THREAD (ptid
), GET_LWP (ptid
));
1037 snprintf (buf
, sizeof (buf
), "Thread %ld (LWP %ld)",
1038 GET_THREAD (ptid
), GET_LWP (ptid
));
1043 if (target_beneath
->to_pid_to_str (ptid
))
1044 return target_beneath
->to_pid_to_str (ptid
);
1046 return normal_pid_to_str (ptid
);
1049 /* Return a string describing the state of the thread specified by
1053 thread_db_extra_thread_info (struct thread_info
*info
)
1055 if (info
->private->dying
)
1061 /* Get the address of the thread local variable in load module LM which
1062 is stored at OFFSET within the thread local storage for thread PTID. */
1065 thread_db_get_thread_local_address (ptid_t ptid
,
1069 if (is_thread (ptid
))
1073 struct thread_info
*thread_info
;
1075 /* glibc doesn't provide the needed interface. */
1076 if (!td_thr_tls_get_addr_p
)
1077 throw_error (TLS_NO_LIBRARY_SUPPORT_ERROR
,
1078 _("No TLS library support"));
1080 /* Caller should have verified that lm != 0. */
1081 gdb_assert (lm
!= 0);
1083 /* Get info about the thread. */
1084 thread_info
= find_thread_pid (ptid
);
1085 thread_db_map_id2thr (thread_info
, 1);
1087 /* Finally, get the address of the variable. */
1088 err
= td_thr_tls_get_addr_p (&thread_info
->private->th
,
1089 (void *)(size_t) lm
,
1092 #ifdef THREAD_DB_HAS_TD_NOTALLOC
1093 /* The memory hasn't been allocated, yet. */
1094 if (err
== TD_NOTALLOC
)
1095 /* Now, if libthread_db provided the initialization image's
1096 address, we *could* try to build a non-lvalue value from
1097 the initialization image. */
1098 throw_error (TLS_NOT_ALLOCATED_YET_ERROR
,
1099 _("TLS not allocated yet"));
1102 /* Something else went wrong. */
1104 throw_error (TLS_GENERIC_ERROR
,
1105 (("%s")), thread_db_err_str (err
));
1107 /* Cast assuming host == target. Joy. */
1108 /* Do proper sign extension for the target. */
1109 gdb_assert (exec_bfd
);
1110 return (bfd_get_sign_extend_vma (exec_bfd
) > 0
1111 ? (CORE_ADDR
) (intptr_t) address
1112 : (CORE_ADDR
) (uintptr_t) address
);
1115 if (target_beneath
->to_get_thread_local_address
)
1116 return target_beneath
->to_get_thread_local_address (ptid
, lm
, offset
);
1118 throw_error (TLS_GENERIC_ERROR
,
1119 _("TLS not supported on this target"));
1123 init_thread_db_ops (void)
1125 thread_db_ops
.to_shortname
= "multi-thread";
1126 thread_db_ops
.to_longname
= "multi-threaded child process.";
1127 thread_db_ops
.to_doc
= "Threads and pthreads support.";
1128 thread_db_ops
.to_attach
= thread_db_attach
;
1129 thread_db_ops
.to_detach
= thread_db_detach
;
1130 thread_db_ops
.to_resume
= thread_db_resume
;
1131 thread_db_ops
.to_wait
= thread_db_wait
;
1132 thread_db_ops
.to_xfer_partial
= thread_db_xfer_partial
;
1133 thread_db_ops
.to_kill
= thread_db_kill
;
1134 thread_db_ops
.to_create_inferior
= thread_db_create_inferior
;
1135 thread_db_ops
.to_post_startup_inferior
= thread_db_post_startup_inferior
;
1136 thread_db_ops
.to_mourn_inferior
= thread_db_mourn_inferior
;
1137 thread_db_ops
.to_find_new_threads
= thread_db_find_new_threads
;
1138 thread_db_ops
.to_pid_to_str
= thread_db_pid_to_str
;
1139 thread_db_ops
.to_stratum
= thread_stratum
;
1140 thread_db_ops
.to_has_thread_control
= tc_schedlock
;
1141 thread_db_ops
.to_get_thread_local_address
1142 = thread_db_get_thread_local_address
;
1143 thread_db_ops
.to_extra_thread_info
= thread_db_extra_thread_info
;
1144 thread_db_ops
.to_magic
= OPS_MAGIC
;
1148 _initialize_thread_db (void)
1150 /* Only initialize the module if we can load libthread_db. */
1151 if (thread_db_load ())
1153 init_thread_db_ops ();
1154 add_target (&thread_db_ops
);
1156 /* Add ourselves to objfile event chain. */
1157 target_new_objfile_chain
= deprecated_target_new_objfile_hook
;
1158 deprecated_target_new_objfile_hook
= thread_db_new_objfile
;