No empty .Rs/.Re
[netbsd-mini2440.git] / gnu / dist / gdb6 / gdb / linux-thread-db.c
blob8c081df268434e2d06c59b19a6e578159bc8210b
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. */
23 #include "defs.h"
25 #include "gdb_assert.h"
26 #include <dlfcn.h>
27 #include "gdb_proc_service.h"
28 #include "gdb_thread_db.h"
30 #include "bfd.h"
31 #include "exceptions.h"
32 #include "gdbthread.h"
33 #include "inferior.h"
34 #include "symfile.h"
35 #include "objfiles.h"
36 #include "target.h"
37 #include "regcache.h"
38 #include "solib-svr4.h"
39 #include "gdbcore.h"
40 #include "linux-nat.h"
42 #ifdef HAVE_GNU_LIBC_VERSION_H
43 #include <gnu/libc-version.h>
44 #endif
46 #ifndef LIBTHREAD_DB_SO
47 #define LIBTHREAD_DB_SO "libthread_db.so.1"
48 #endif
50 /* If we're running on GNU/Linux, we must explicitly attach to any new
51 threads. */
53 /* FIXME: There is certainly some room for improvements:
54 - Cache LWP ids.
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
71 library. */
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,
88 td_thragent_t **ta);
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_getfpregs_p) (const td_thrhandle_t *th,
109 gdb_prfpregset_t *regset);
110 static td_err_e (*td_thr_getgregs_p) (const td_thrhandle_t *th,
111 prgregset_t gregs);
112 static td_err_e (*td_thr_setfpregs_p) (const td_thrhandle_t *th,
113 const gdb_prfpregset_t *fpregs);
114 static td_err_e (*td_thr_setgregs_p) (const td_thrhandle_t *th,
115 prgregset_t gregs);
116 static td_err_e (*td_thr_event_enable_p) (const td_thrhandle_t *th,
117 int event);
119 static td_err_e (*td_thr_tls_get_addr_p) (const td_thrhandle_t *th,
120 void *map_address,
121 size_t offset, void **address);
123 /* Location of the thread creation event breakpoint. The code at this
124 location in the child process will be called by the pthread library
125 whenever a new thread is created. By setting a special breakpoint
126 at this location, GDB can detect when a new thread is created. We
127 obtain this location via the td_ta_event_addr call. */
128 static CORE_ADDR td_create_bp_addr;
130 /* Location of the thread death event breakpoint. */
131 static CORE_ADDR td_death_bp_addr;
133 /* Prototypes for local functions. */
134 static void thread_db_find_new_threads (void);
135 static void attach_thread (ptid_t ptid, const td_thrhandle_t *th_p,
136 const td_thrinfo_t *ti_p, int verbose);
137 static void detach_thread (ptid_t ptid, int verbose);
140 /* Building process ids. */
142 #define GET_PID(ptid) ptid_get_pid (ptid)
143 #define GET_LWP(ptid) ptid_get_lwp (ptid)
144 #define GET_THREAD(ptid) ptid_get_tid (ptid)
146 #define is_lwp(ptid) (GET_LWP (ptid) != 0)
147 #define is_thread(ptid) (GET_THREAD (ptid) != 0)
149 #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
152 /* Use "struct private_thread_info" to cache thread state. This is
153 a substantial optimization. */
155 struct private_thread_info
157 /* Flag set when we see a TD_DEATH event for this thread. */
158 unsigned int dying:1;
160 /* Cached thread state. */
161 unsigned int th_valid:1;
162 unsigned int ti_valid:1;
164 td_thrhandle_t th;
165 td_thrinfo_t ti;
169 static char *
170 thread_db_err_str (td_err_e err)
172 static char buf[64];
174 switch (err)
176 case TD_OK:
177 return "generic 'call succeeded'";
178 case TD_ERR:
179 return "generic error";
180 case TD_NOTHR:
181 return "no thread to satisfy query";
182 case TD_NOSV:
183 return "no sync handle to satisfy query";
184 case TD_NOLWP:
185 return "no LWP to satisfy query";
186 case TD_BADPH:
187 return "invalid process handle";
188 case TD_BADTH:
189 return "invalid thread handle";
190 case TD_BADSH:
191 return "invalid synchronization handle";
192 case TD_BADTA:
193 return "invalid thread agent";
194 case TD_BADKEY:
195 return "invalid key";
196 case TD_NOMSG:
197 return "no event message for getmsg";
198 case TD_NOFPREGS:
199 return "FPU register set not available";
200 case TD_NOLIBTHREAD:
201 return "application not linked with libthread";
202 case TD_NOEVENT:
203 return "requested event is not supported";
204 case TD_NOCAPAB:
205 return "capability not available";
206 case TD_DBERR:
207 return "debugger service failed";
208 case TD_NOAPLIC:
209 return "operation not applicable to";
210 case TD_NOTSD:
211 return "no thread-specific data for this thread";
212 case TD_MALLOC:
213 return "malloc failed";
214 case TD_PARTIALREG:
215 return "only part of register set was written/read";
216 case TD_NOXREGS:
217 return "X register set not available for this thread";
218 default:
219 snprintf (buf, sizeof (buf), "unknown thread_db error '%d'", err);
220 return buf;
224 static char *
225 thread_db_state_str (td_thr_state_e state)
227 static char buf[64];
229 switch (state)
231 case TD_THR_STOPPED:
232 return "stopped by debugger";
233 case TD_THR_RUN:
234 return "runnable";
235 case TD_THR_ACTIVE:
236 return "active";
237 case TD_THR_ZOMBIE:
238 return "zombie";
239 case TD_THR_SLEEP:
240 return "sleeping";
241 case TD_THR_STOPPED_ASLEEP:
242 return "stopped by debugger AND blocked";
243 default:
244 snprintf (buf, sizeof (buf), "unknown thread_db state %d", state);
245 return buf;
249 /* A callback function for td_ta_thr_iter, which we use to map all
250 threads to LWPs.
252 THP is a handle to the current thread; if INFOP is not NULL, the
253 struct thread_info associated with this thread is returned in
254 *INFOP.
256 If the thread is a zombie, TD_THR_ZOMBIE is returned. Otherwise,
257 zero is returned to indicate success. */
259 static int
260 thread_get_info_callback (const td_thrhandle_t *thp, void *infop)
262 td_thrinfo_t ti;
263 td_err_e err;
264 struct thread_info *thread_info;
265 ptid_t thread_ptid;
267 err = td_thr_get_info_p (thp, &ti);
268 if (err != TD_OK)
269 error (_("thread_get_info_callback: cannot get thread info: %s"),
270 thread_db_err_str (err));
272 /* Fill the cache. */
273 thread_ptid = ptid_build (GET_PID (inferior_ptid), ti.ti_lid, ti.ti_tid);
274 thread_info = find_thread_pid (thread_ptid);
276 /* In the case of a zombie thread, don't continue. We don't want to
277 attach to it thinking it is a new thread. */
278 if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
280 if (infop != NULL)
281 *(struct thread_info **) infop = thread_info;
282 if (thread_info != NULL)
284 memcpy (&thread_info->private->th, thp, sizeof (*thp));
285 thread_info->private->th_valid = 1;
286 memcpy (&thread_info->private->ti, &ti, sizeof (ti));
287 thread_info->private->ti_valid = 1;
289 return TD_THR_ZOMBIE;
292 if (thread_info == NULL)
294 /* New thread. Attach to it now (why wait?). */
295 attach_thread (thread_ptid, thp, &ti, 1);
296 thread_info = find_thread_pid (thread_ptid);
297 gdb_assert (thread_info != NULL);
300 memcpy (&thread_info->private->th, thp, sizeof (*thp));
301 thread_info->private->th_valid = 1;
302 memcpy (&thread_info->private->ti, &ti, sizeof (ti));
303 thread_info->private->ti_valid = 1;
305 if (infop != NULL)
306 *(struct thread_info **) infop = thread_info;
308 return 0;
311 /* Accessor functions for the thread_db information, with caching. */
313 static void
314 thread_db_map_id2thr (struct thread_info *thread_info, int fatal)
316 td_err_e err;
318 if (thread_info->private->th_valid)
319 return;
321 err = td_ta_map_id2thr_p (thread_agent, GET_THREAD (thread_info->ptid),
322 &thread_info->private->th);
323 if (err != TD_OK)
325 if (fatal)
326 error (_("Cannot find thread %ld: %s"),
327 (long) GET_THREAD (thread_info->ptid),
328 thread_db_err_str (err));
330 else
331 thread_info->private->th_valid = 1;
334 static td_thrinfo_t *
335 thread_db_get_info (struct thread_info *thread_info)
337 td_err_e err;
339 if (thread_info->private->ti_valid)
340 return &thread_info->private->ti;
342 if (!thread_info->private->th_valid)
343 thread_db_map_id2thr (thread_info, 1);
345 err =
346 td_thr_get_info_p (&thread_info->private->th, &thread_info->private->ti);
347 if (err != TD_OK)
348 error (_("thread_db_get_info: cannot get thread info: %s"),
349 thread_db_err_str (err));
351 thread_info->private->ti_valid = 1;
352 return &thread_info->private->ti;
355 /* Convert between user-level thread ids and LWP ids. */
357 static ptid_t
358 thread_from_lwp (ptid_t ptid)
360 td_thrhandle_t th;
361 td_err_e err;
362 struct thread_info *thread_info;
363 ptid_t thread_ptid;
365 if (GET_LWP (ptid) == 0)
366 ptid = BUILD_LWP (GET_PID (ptid), GET_PID (ptid));
368 gdb_assert (is_lwp (ptid));
370 err = td_ta_map_lwp2thr_p (thread_agent, GET_LWP (ptid), &th);
371 if (err != TD_OK)
372 error (_("Cannot find user-level thread for LWP %ld: %s"),
373 GET_LWP (ptid), thread_db_err_str (err));
375 thread_info = NULL;
377 /* Fetch the thread info. If we get back TD_THR_ZOMBIE, then the
378 event thread has already died. If another gdb interface has called
379 thread_alive() previously, the thread won't be found on the thread list
380 anymore. In that case, we don't want to process this ptid anymore
381 to avoid the possibility of later treating it as a newly
382 discovered thread id that we should add to the list. Thus,
383 we return a -1 ptid which is also how the thread list marks a
384 dead thread. */
385 if (thread_get_info_callback (&th, &thread_info) == TD_THR_ZOMBIE
386 && thread_info == NULL)
387 return pid_to_ptid (-1);
389 gdb_assert (thread_info && thread_info->private->ti_valid);
391 return ptid_build (GET_PID (ptid), GET_LWP (ptid),
392 thread_info->private->ti.ti_tid);
395 static ptid_t
396 lwp_from_thread (ptid_t ptid)
398 return BUILD_LWP (GET_LWP (ptid), GET_PID (ptid));
402 void
403 thread_db_init (struct target_ops *target)
405 target_beneath = target;
408 static void *
409 verbose_dlsym (void *handle, const char *name)
411 void *sym = dlsym (handle, name);
412 if (sym == NULL)
413 warning (_("Symbol \"%s\" not found in libthread_db: %s"), name, dlerror ());
414 return sym;
417 static int
418 thread_db_load (void)
420 void *handle;
421 td_err_e err;
423 handle = dlopen (LIBTHREAD_DB_SO, RTLD_NOW);
424 if (handle == NULL)
426 fprintf_filtered (gdb_stderr, "\n\ndlopen failed on '%s' - %s\n",
427 LIBTHREAD_DB_SO, dlerror ());
428 fprintf_filtered (gdb_stderr,
429 "GDB will not be able to debug pthreads.\n\n");
430 return 0;
433 /* Initialize pointers to the dynamic library functions we will use.
434 Essential functions first. */
436 td_init_p = verbose_dlsym (handle, "td_init");
437 if (td_init_p == NULL)
438 return 0;
440 td_ta_new_p = verbose_dlsym (handle, "td_ta_new");
441 if (td_ta_new_p == NULL)
442 return 0;
444 td_ta_map_id2thr_p = verbose_dlsym (handle, "td_ta_map_id2thr");
445 if (td_ta_map_id2thr_p == NULL)
446 return 0;
448 td_ta_map_lwp2thr_p = verbose_dlsym (handle, "td_ta_map_lwp2thr");
449 if (td_ta_map_lwp2thr_p == NULL)
450 return 0;
452 td_ta_thr_iter_p = verbose_dlsym (handle, "td_ta_thr_iter");
453 if (td_ta_thr_iter_p == NULL)
454 return 0;
456 td_thr_validate_p = verbose_dlsym (handle, "td_thr_validate");
457 if (td_thr_validate_p == NULL)
458 return 0;
460 td_thr_get_info_p = verbose_dlsym (handle, "td_thr_get_info");
461 if (td_thr_get_info_p == NULL)
462 return 0;
464 td_thr_getfpregs_p = verbose_dlsym (handle, "td_thr_getfpregs");
465 if (td_thr_getfpregs_p == NULL)
466 return 0;
468 td_thr_getgregs_p = verbose_dlsym (handle, "td_thr_getgregs");
469 if (td_thr_getgregs_p == NULL)
470 return 0;
472 td_thr_setfpregs_p = verbose_dlsym (handle, "td_thr_setfpregs");
473 if (td_thr_setfpregs_p == NULL)
474 return 0;
476 td_thr_setgregs_p = verbose_dlsym (handle, "td_thr_setgregs");
477 if (td_thr_setgregs_p == NULL)
478 return 0;
480 /* Initialize the library. */
481 err = td_init_p ();
482 if (err != TD_OK)
484 warning (_("Cannot initialize libthread_db: %s"), thread_db_err_str (err));
485 return 0;
488 /* These are not essential. */
489 td_ta_event_addr_p = dlsym (handle, "td_ta_event_addr");
490 td_ta_set_event_p = dlsym (handle, "td_ta_set_event");
491 td_ta_event_getmsg_p = dlsym (handle, "td_ta_event_getmsg");
492 td_thr_event_enable_p = dlsym (handle, "td_thr_event_enable");
493 td_thr_tls_get_addr_p = dlsym (handle, "td_thr_tls_get_addr");
495 return 1;
498 static td_err_e
499 enable_thread_event (td_thragent_t *thread_agent, int event, CORE_ADDR *bp)
501 td_notify_t notify;
502 td_err_e err;
504 /* Get the breakpoint address for thread EVENT. */
505 err = td_ta_event_addr_p (thread_agent, event, &notify);
506 if (err != TD_OK)
507 return err;
509 /* Set up the breakpoint. */
510 gdb_assert (exec_bfd);
511 (*bp) = (gdbarch_convert_from_func_ptr_addr
512 (current_gdbarch,
513 /* Do proper sign extension for the target. */
514 (bfd_get_sign_extend_vma (exec_bfd) > 0
515 ? (CORE_ADDR) (intptr_t) notify.u.bptaddr
516 : (CORE_ADDR) (uintptr_t) notify.u.bptaddr),
517 &current_target));
518 create_thread_event_breakpoint ((*bp));
520 return TD_OK;
523 static void
524 enable_thread_event_reporting (void)
526 td_thr_events_t events;
527 td_notify_t notify;
528 td_err_e err;
529 #ifdef HAVE_GNU_LIBC_VERSION_H
530 const char *libc_version;
531 int libc_major, libc_minor;
532 #endif
534 /* We cannot use the thread event reporting facility if these
535 functions aren't available. */
536 if (td_ta_event_addr_p == NULL || td_ta_set_event_p == NULL
537 || td_ta_event_getmsg_p == NULL || td_thr_event_enable_p == NULL)
538 return;
540 /* Set the process wide mask saying which events we're interested in. */
541 td_event_emptyset (&events);
542 td_event_addset (&events, TD_CREATE);
544 #ifdef HAVE_GNU_LIBC_VERSION_H
545 /* FIXME: kettenis/2000-04-23: The event reporting facility is
546 broken for TD_DEATH events in glibc 2.1.3, so don't enable it for
547 now. */
548 libc_version = gnu_get_libc_version ();
549 if (sscanf (libc_version, "%d.%d", &libc_major, &libc_minor) == 2
550 && (libc_major > 2 || (libc_major == 2 && libc_minor > 1)))
551 #endif
552 td_event_addset (&events, TD_DEATH);
554 err = td_ta_set_event_p (thread_agent, &events);
555 if (err != TD_OK)
557 warning (_("Unable to set global thread event mask: %s"),
558 thread_db_err_str (err));
559 return;
562 /* Delete previous thread event breakpoints, if any. */
563 remove_thread_event_breakpoints ();
564 td_create_bp_addr = 0;
565 td_death_bp_addr = 0;
567 /* Set up the thread creation event. */
568 err = enable_thread_event (thread_agent, TD_CREATE, &td_create_bp_addr);
569 if (err != TD_OK)
571 warning (_("Unable to get location for thread creation breakpoint: %s"),
572 thread_db_err_str (err));
573 return;
576 /* Set up the thread death event. */
577 err = enable_thread_event (thread_agent, TD_DEATH, &td_death_bp_addr);
578 if (err != TD_OK)
580 warning (_("Unable to get location for thread death breakpoint: %s"),
581 thread_db_err_str (err));
582 return;
586 static void
587 disable_thread_event_reporting (void)
589 td_thr_events_t events;
591 /* Set the process wide mask saying we aren't interested in any
592 events anymore. */
593 td_event_emptyset (&events);
594 td_ta_set_event_p (thread_agent, &events);
596 /* Delete thread event breakpoints, if any. */
597 remove_thread_event_breakpoints ();
598 td_create_bp_addr = 0;
599 td_death_bp_addr = 0;
602 static void
603 check_thread_signals (void)
605 #ifdef GET_THREAD_SIGNALS
606 if (!thread_signals)
608 sigset_t mask;
609 int i;
611 GET_THREAD_SIGNALS (&mask);
612 sigemptyset (&thread_stop_set);
613 sigemptyset (&thread_print_set);
615 for (i = 1; i < NSIG; i++)
617 if (sigismember (&mask, i))
619 if (signal_stop_update (target_signal_from_host (i), 0))
620 sigaddset (&thread_stop_set, i);
621 if (signal_print_update (target_signal_from_host (i), 0))
622 sigaddset (&thread_print_set, i);
623 thread_signals = 1;
627 #endif
630 /* Check whether thread_db is usable. This function is called when
631 an inferior is created (or otherwise acquired, e.g. attached to)
632 and when new shared libraries are loaded into a running process. */
634 void
635 check_for_thread_db (void)
637 td_err_e err;
638 static int already_loaded;
640 /* First time through, report that libthread_db was successfuly
641 loaded. Can't print this in in thread_db_load as, at that stage,
642 the interpreter and it's console haven't started. */
644 if (!already_loaded)
646 Dl_info info;
647 const char *library = NULL;
648 if (dladdr ((*td_ta_new_p), &info) != 0)
649 library = info.dli_fname;
651 /* Try dlinfo? */
653 if (library == NULL)
654 /* Paranoid - don't let a NULL path slip through. */
655 library = LIBTHREAD_DB_SO;
657 printf_unfiltered (_("Using host libthread_db library \"%s\".\n"),
658 library);
659 already_loaded = 1;
662 if (using_thread_db)
663 /* Nothing to do. The thread library was already detected and the
664 target vector was already activated. */
665 return;
667 /* Don't attempt to use thread_db on targets which can not run
668 (executables not running yet, core files) for now. */
669 if (!target_has_execution)
670 return;
672 /* Initialize the structure that identifies the child process. */
673 proc_handle.pid = GET_PID (inferior_ptid);
675 /* Now attempt to open a connection to the thread library. */
676 err = td_ta_new_p (&proc_handle, &thread_agent);
677 switch (err)
679 case TD_NOLIBTHREAD:
680 /* No thread library was detected. */
681 break;
683 case TD_OK:
684 printf_unfiltered (_("[Thread debugging using libthread_db enabled]\n"));
686 /* The thread library was detected. Activate the thread_db target. */
687 push_target (&thread_db_ops);
688 using_thread_db = 1;
690 enable_thread_event_reporting ();
691 thread_db_find_new_threads ();
692 break;
694 default:
695 warning (_("Cannot initialize thread debugging library: %s"),
696 thread_db_err_str (err));
697 break;
701 static void
702 thread_db_new_objfile (struct objfile *objfile)
704 if (objfile != NULL)
705 check_for_thread_db ();
707 if (target_new_objfile_chain)
708 target_new_objfile_chain (objfile);
711 /* Attach to a new thread. This function is called when we receive a
712 TD_CREATE event or when we iterate over all threads and find one
713 that wasn't already in our list. */
715 static void
716 attach_thread (ptid_t ptid, const td_thrhandle_t *th_p,
717 const td_thrinfo_t *ti_p, int verbose)
719 struct thread_info *tp;
720 td_err_e err;
722 /* If we're being called after a TD_CREATE event, we may already
723 know about this thread. There are two ways this can happen. We
724 may have iterated over all threads between the thread creation
725 and the TD_CREATE event, for instance when the user has issued
726 the `info threads' command before the SIGTRAP for hitting the
727 thread creation breakpoint was reported. Alternatively, the
728 thread may have exited and a new one been created with the same
729 thread ID. In the first case we don't need to do anything; in
730 the second case we should discard information about the dead
731 thread and attach to the new one. */
732 if (in_thread_list (ptid))
734 tp = find_thread_pid (ptid);
735 gdb_assert (tp != NULL);
737 if (!tp->private->dying)
738 return;
740 delete_thread (ptid);
743 check_thread_signals ();
745 /* Add the thread to GDB's thread list. */
746 tp = add_thread (ptid);
747 tp->private = xmalloc (sizeof (struct private_thread_info));
748 memset (tp->private, 0, sizeof (struct private_thread_info));
750 if (verbose)
751 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid));
753 if (ti_p->ti_state == TD_THR_UNKNOWN || ti_p->ti_state == TD_THR_ZOMBIE)
754 return; /* A zombie thread -- do not attach. */
756 /* Under GNU/Linux, we have to attach to each and every thread. */
757 #ifdef ATTACH_LWP
758 ATTACH_LWP (BUILD_LWP (ti_p->ti_lid, GET_PID (ptid)), 0);
759 #endif
761 /* Enable thread event reporting for this thread. */
762 err = td_thr_event_enable_p (th_p, 1);
763 if (err != TD_OK)
764 error (_("Cannot enable thread event reporting for %s: %s"),
765 target_pid_to_str (ptid), thread_db_err_str (err));
768 static void
769 thread_db_attach (char *args, int from_tty)
771 target_beneath->to_attach (args, from_tty);
773 /* Destroy thread info; it's no longer valid. */
774 init_thread_list ();
776 /* The child process is now the actual multi-threaded
777 program. Snatch its process ID... */
778 proc_handle.pid = GET_PID (inferior_ptid);
780 /* ...and perform the remaining initialization steps. */
781 enable_thread_event_reporting ();
782 thread_db_find_new_threads ();
785 static void
786 detach_thread (ptid_t ptid, int verbose)
788 struct thread_info *thread_info;
790 if (verbose)
791 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (ptid));
793 /* Don't delete the thread now, because it still reports as active
794 until it has executed a few instructions after the event
795 breakpoint - if we deleted it now, "info threads" would cause us
796 to re-attach to it. Just mark it as having had a TD_DEATH
797 event. This means that we won't delete it from our thread list
798 until we notice that it's dead (via prune_threads), or until
799 something re-uses its thread ID. */
800 thread_info = find_thread_pid (ptid);
801 gdb_assert (thread_info != NULL);
802 thread_info->private->dying = 1;
805 static void
806 thread_db_detach (char *args, int from_tty)
808 disable_thread_event_reporting ();
810 /* There's no need to save & restore inferior_ptid here, since the
811 inferior is supposed to be survive this function call. */
812 inferior_ptid = lwp_from_thread (inferior_ptid);
814 /* Forget about the child's process ID. We shouldn't need it
815 anymore. */
816 proc_handle.pid = 0;
818 target_beneath->to_detach (args, from_tty);
821 static int
822 clear_lwpid_callback (struct thread_info *thread, void *dummy)
824 /* If we know that our thread implementation is 1-to-1, we could save
825 a certain amount of information; it's not clear how much, so we
826 are always conservative. */
828 thread->private->th_valid = 0;
829 thread->private->ti_valid = 0;
831 return 0;
834 static void
835 thread_db_resume (ptid_t ptid, int step, enum target_signal signo)
837 struct cleanup *old_chain = save_inferior_ptid ();
839 if (GET_PID (ptid) == -1)
840 inferior_ptid = lwp_from_thread (inferior_ptid);
841 else if (is_thread (ptid))
842 ptid = lwp_from_thread (ptid);
844 /* Clear cached data which may not be valid after the resume. */
845 iterate_over_threads (clear_lwpid_callback, NULL);
847 target_beneath->to_resume (ptid, step, signo);
849 do_cleanups (old_chain);
852 /* Check if PID is currently stopped at the location of a thread event
853 breakpoint location. If it is, read the event message and act upon
854 the event. */
856 static void
857 check_event (ptid_t ptid)
859 td_event_msg_t msg;
860 td_thrinfo_t ti;
861 td_err_e err;
862 CORE_ADDR stop_pc;
863 int loop = 0;
865 /* Bail out early if we're not at a thread event breakpoint. */
866 stop_pc = read_pc_pid (ptid) - DECR_PC_AFTER_BREAK;
867 if (stop_pc != td_create_bp_addr && stop_pc != td_death_bp_addr)
868 return;
870 /* If we are at a create breakpoint, we do not know what new lwp
871 was created and cannot specifically locate the event message for it.
872 We have to call td_ta_event_getmsg() to get
873 the latest message. Since we have no way of correlating whether
874 the event message we get back corresponds to our breakpoint, we must
875 loop and read all event messages, processing them appropriately.
876 This guarantees we will process the correct message before continuing
877 from the breakpoint.
879 Currently, death events are not enabled. If they are enabled,
880 the death event can use the td_thr_event_getmsg() interface to
881 get the message specifically for that lwp and avoid looping
882 below. */
884 loop = 1;
888 err = td_ta_event_getmsg_p (thread_agent, &msg);
889 if (err != TD_OK)
891 if (err == TD_NOMSG)
892 return;
894 error (_("Cannot get thread event message: %s"),
895 thread_db_err_str (err));
898 err = td_thr_get_info_p (msg.th_p, &ti);
899 if (err != TD_OK)
900 error (_("Cannot get thread info: %s"), thread_db_err_str (err));
902 ptid = ptid_build (GET_PID (ptid), ti.ti_lid, ti.ti_tid);
904 switch (msg.event)
906 case TD_CREATE:
907 /* Call attach_thread whether or not we already know about a
908 thread with this thread ID. */
909 attach_thread (ptid, msg.th_p, &ti, 1);
911 break;
913 case TD_DEATH:
915 if (!in_thread_list (ptid))
916 error (_("Spurious thread death event."));
918 detach_thread (ptid, 1);
920 break;
922 default:
923 error (_("Spurious thread event."));
926 while (loop);
929 static ptid_t
930 thread_db_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
932 extern ptid_t trap_ptid;
934 if (GET_PID (ptid) != -1 && is_thread (ptid))
935 ptid = lwp_from_thread (ptid);
937 ptid = target_beneath->to_wait (ptid, ourstatus);
939 if (proc_handle.pid == 0)
940 /* The current child process isn't the actual multi-threaded
941 program yet, so don't try to do any special thread-specific
942 post-processing and bail out early. */
943 return ptid;
945 if (ourstatus->kind == TARGET_WAITKIND_EXITED)
946 return pid_to_ptid (-1);
948 if (ourstatus->kind == TARGET_WAITKIND_STOPPED
949 && ourstatus->value.sig == TARGET_SIGNAL_TRAP)
950 /* Check for a thread event. */
951 check_event (ptid);
953 if (!ptid_equal (trap_ptid, null_ptid))
954 trap_ptid = thread_from_lwp (trap_ptid);
956 /* Change the ptid back into the higher level PID + TID format.
957 If the thread is dead and no longer on the thread list, we will
958 get back a dead ptid. This can occur if the thread death event
959 gets postponed by other simultaneous events. In such a case,
960 we want to just ignore the event and continue on. */
961 ptid = thread_from_lwp (ptid);
962 if (GET_PID (ptid) == -1)
963 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
965 return ptid;
968 static LONGEST
969 thread_db_xfer_partial (struct target_ops *ops, enum target_object object,
970 const char *annex, gdb_byte *readbuf,
971 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
973 struct cleanup *old_chain = save_inferior_ptid ();
974 LONGEST xfer;
976 if (is_thread (inferior_ptid))
978 /* FIXME: This seems to be necessary to make sure breakpoints
979 are removed. */
980 if (!target_thread_alive (inferior_ptid))
981 inferior_ptid = pid_to_ptid (GET_PID (inferior_ptid));
982 else
983 inferior_ptid = lwp_from_thread (inferior_ptid);
986 xfer = target_beneath->to_xfer_partial (ops, object, annex,
987 readbuf, writebuf, offset, len);
989 do_cleanups (old_chain);
990 return xfer;
993 static void
994 thread_db_fetch_registers (int regno)
996 struct thread_info *thread_info;
997 prgregset_t gregset;
998 gdb_prfpregset_t fpregset;
999 td_err_e err;
1001 if (!is_thread (inferior_ptid))
1003 /* Pass the request to the target beneath us. */
1004 target_beneath->to_fetch_registers (regno);
1005 return;
1008 thread_info = find_thread_pid (inferior_ptid);
1009 thread_db_map_id2thr (thread_info, 1);
1011 err = td_thr_getgregs_p (&thread_info->private->th, gregset);
1012 if (err != TD_OK)
1013 error (_("Cannot fetch general-purpose registers for thread %ld: %s"),
1014 (long) GET_THREAD (inferior_ptid), thread_db_err_str (err));
1016 err = td_thr_getfpregs_p (&thread_info->private->th, &fpregset);
1017 if (err != TD_OK)
1018 error (_("Cannot get floating-point registers for thread %ld: %s"),
1019 (long) GET_THREAD (inferior_ptid), thread_db_err_str (err));
1021 /* Note that we must call supply_gregset after calling the thread_db
1022 routines because the thread_db routines call ps_lgetgregs and
1023 friends which clobber GDB's register cache. */
1024 supply_gregset ((gdb_gregset_t *) gregset);
1025 supply_fpregset (&fpregset);
1028 static void
1029 thread_db_store_registers (int regno)
1031 prgregset_t gregset;
1032 gdb_prfpregset_t fpregset;
1033 td_err_e err;
1034 struct thread_info *thread_info;
1036 if (!is_thread (inferior_ptid))
1038 /* Pass the request to the target beneath us. */
1039 target_beneath->to_store_registers (regno);
1040 return;
1043 thread_info = find_thread_pid (inferior_ptid);
1044 thread_db_map_id2thr (thread_info, 1);
1046 if (regno != -1)
1048 gdb_byte raw[MAX_REGISTER_SIZE];
1050 regcache_raw_collect (current_regcache, regno, raw);
1051 thread_db_fetch_registers (-1);
1052 regcache_raw_supply (current_regcache, regno, raw);
1055 fill_gregset ((gdb_gregset_t *) gregset, -1);
1056 fill_fpregset (&fpregset, -1);
1058 err = td_thr_setgregs_p (&thread_info->private->th, gregset);
1059 if (err != TD_OK)
1060 error (_("Cannot store general-purpose registers for thread %ld: %s"),
1061 (long) GET_THREAD (inferior_ptid), thread_db_err_str (err));
1062 err = td_thr_setfpregs_p (&thread_info->private->th, &fpregset);
1063 if (err != TD_OK)
1064 error (_("Cannot store floating-point registers for thread %ld: %s"),
1065 (long) GET_THREAD (inferior_ptid), thread_db_err_str (err));
1068 static void
1069 thread_db_kill (void)
1071 /* There's no need to save & restore inferior_ptid here, since the
1072 inferior isn't supposed to survive this function call. */
1073 inferior_ptid = lwp_from_thread (inferior_ptid);
1074 target_beneath->to_kill ();
1077 static void
1078 thread_db_create_inferior (char *exec_file, char *allargs, char **env,
1079 int from_tty)
1081 unpush_target (&thread_db_ops);
1082 using_thread_db = 0;
1083 target_beneath->to_create_inferior (exec_file, allargs, env, from_tty);
1086 static void
1087 thread_db_post_startup_inferior (ptid_t ptid)
1089 if (proc_handle.pid == 0)
1091 /* The child process is now the actual multi-threaded
1092 program. Snatch its process ID... */
1093 proc_handle.pid = GET_PID (ptid);
1095 /* ...and perform the remaining initialization steps. */
1096 enable_thread_event_reporting ();
1097 thread_db_find_new_threads ();
1101 static void
1102 thread_db_mourn_inferior (void)
1104 /* Forget about the child's process ID. We shouldn't need it
1105 anymore. */
1106 proc_handle.pid = 0;
1108 target_beneath->to_mourn_inferior ();
1110 /* Delete the old thread event breakpoints. Do this after mourning
1111 the inferior, so that we don't try to uninsert them. */
1112 remove_thread_event_breakpoints ();
1114 /* Detach thread_db target ops. */
1115 unpush_target (&thread_db_ops);
1116 using_thread_db = 0;
1119 static int
1120 thread_db_thread_alive (ptid_t ptid)
1122 td_thrhandle_t th;
1123 td_err_e err;
1125 if (is_thread (ptid))
1127 struct thread_info *thread_info;
1128 thread_info = find_thread_pid (ptid);
1130 thread_db_map_id2thr (thread_info, 0);
1131 if (!thread_info->private->th_valid)
1132 return 0;
1134 err = td_thr_validate_p (&thread_info->private->th);
1135 if (err != TD_OK)
1136 return 0;
1138 if (!thread_info->private->ti_valid)
1140 err =
1141 td_thr_get_info_p (&thread_info->private->th,
1142 &thread_info->private->ti);
1143 if (err != TD_OK)
1144 return 0;
1145 thread_info->private->ti_valid = 1;
1148 if (thread_info->private->ti.ti_state == TD_THR_UNKNOWN
1149 || thread_info->private->ti.ti_state == TD_THR_ZOMBIE)
1150 return 0; /* A zombie thread. */
1152 return 1;
1155 if (target_beneath->to_thread_alive)
1156 return target_beneath->to_thread_alive (ptid);
1158 return 0;
1161 static int
1162 find_new_threads_callback (const td_thrhandle_t *th_p, void *data)
1164 td_thrinfo_t ti;
1165 td_err_e err;
1166 ptid_t ptid;
1168 err = td_thr_get_info_p (th_p, &ti);
1169 if (err != TD_OK)
1170 error (_("find_new_threads_callback: cannot get thread info: %s"),
1171 thread_db_err_str (err));
1173 if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
1174 return 0; /* A zombie -- ignore. */
1176 ptid = ptid_build (GET_PID (inferior_ptid), ti.ti_lid, ti.ti_tid);
1178 if (!in_thread_list (ptid))
1179 attach_thread (ptid, th_p, &ti, 1);
1181 return 0;
1184 static void
1185 thread_db_find_new_threads (void)
1187 td_err_e err;
1189 /* Iterate over all user-space threads to discover new threads. */
1190 err = td_ta_thr_iter_p (thread_agent, find_new_threads_callback, NULL,
1191 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
1192 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
1193 if (err != TD_OK)
1194 error (_("Cannot find new threads: %s"), thread_db_err_str (err));
1197 static char *
1198 thread_db_pid_to_str (ptid_t ptid)
1200 if (is_thread (ptid))
1202 static char buf[64];
1203 td_thrinfo_t *ti_p;
1204 td_err_e err;
1205 struct thread_info *thread_info;
1207 thread_info = find_thread_pid (ptid);
1208 thread_db_map_id2thr (thread_info, 0);
1209 if (!thread_info->private->th_valid)
1211 snprintf (buf, sizeof (buf), "Thread %ld (Missing)",
1212 GET_THREAD (ptid));
1213 return buf;
1216 ti_p = thread_db_get_info (thread_info);
1218 if (ti_p->ti_state == TD_THR_ACTIVE && ti_p->ti_lid != 0)
1220 snprintf (buf, sizeof (buf), "Thread %ld (LWP %d)",
1221 (long) ti_p->ti_tid, ti_p->ti_lid);
1223 else
1225 snprintf (buf, sizeof (buf), "Thread %ld (%s)",
1226 (long) ti_p->ti_tid,
1227 thread_db_state_str (ti_p->ti_state));
1230 return buf;
1233 if (target_beneath->to_pid_to_str (ptid))
1234 return target_beneath->to_pid_to_str (ptid);
1236 return normal_pid_to_str (ptid);
1239 /* Get the address of the thread local variable in load module LM which
1240 is stored at OFFSET within the thread local storage for thread PTID. */
1242 static CORE_ADDR
1243 thread_db_get_thread_local_address (ptid_t ptid,
1244 CORE_ADDR lm,
1245 CORE_ADDR offset)
1247 if (is_thread (ptid))
1249 td_err_e err;
1250 void *address;
1251 struct thread_info *thread_info;
1253 /* glibc doesn't provide the needed interface. */
1254 if (!td_thr_tls_get_addr_p)
1255 throw_error (TLS_NO_LIBRARY_SUPPORT_ERROR,
1256 _("No TLS library support"));
1258 /* Caller should have verified that lm != 0. */
1259 gdb_assert (lm != 0);
1261 /* Get info about the thread. */
1262 thread_info = find_thread_pid (ptid);
1263 thread_db_map_id2thr (thread_info, 1);
1265 /* Finally, get the address of the variable. */
1266 err = td_thr_tls_get_addr_p (&thread_info->private->th, (void *) lm,
1267 offset, &address);
1269 #ifdef THREAD_DB_HAS_TD_NOTALLOC
1270 /* The memory hasn't been allocated, yet. */
1271 if (err == TD_NOTALLOC)
1272 /* Now, if libthread_db provided the initialization image's
1273 address, we *could* try to build a non-lvalue value from
1274 the initialization image. */
1275 throw_error (TLS_NOT_ALLOCATED_YET_ERROR,
1276 _("TLS not allocated yet"));
1277 #endif
1279 /* Something else went wrong. */
1280 if (err != TD_OK)
1281 throw_error (TLS_GENERIC_ERROR,
1282 (("%s")), thread_db_err_str (err));
1284 /* Cast assuming host == target. Joy. */
1285 /* Do proper sign extension for the target. */
1286 gdb_assert (exec_bfd);
1287 return (bfd_get_sign_extend_vma (exec_bfd) > 0
1288 ? (CORE_ADDR) (intptr_t) address
1289 : (CORE_ADDR) (uintptr_t) address);
1292 if (target_beneath->to_get_thread_local_address)
1293 return target_beneath->to_get_thread_local_address (ptid, lm, offset);
1294 else
1295 throw_error (TLS_GENERIC_ERROR,
1296 _("TLS not supported on this target"));
1299 static void
1300 init_thread_db_ops (void)
1302 thread_db_ops.to_shortname = "multi-thread";
1303 thread_db_ops.to_longname = "multi-threaded child process.";
1304 thread_db_ops.to_doc = "Threads and pthreads support.";
1305 thread_db_ops.to_attach = thread_db_attach;
1306 thread_db_ops.to_detach = thread_db_detach;
1307 thread_db_ops.to_resume = thread_db_resume;
1308 thread_db_ops.to_wait = thread_db_wait;
1309 thread_db_ops.to_fetch_registers = thread_db_fetch_registers;
1310 thread_db_ops.to_store_registers = thread_db_store_registers;
1311 thread_db_ops.to_xfer_partial = thread_db_xfer_partial;
1312 thread_db_ops.to_kill = thread_db_kill;
1313 thread_db_ops.to_create_inferior = thread_db_create_inferior;
1314 thread_db_ops.to_post_startup_inferior = thread_db_post_startup_inferior;
1315 thread_db_ops.to_mourn_inferior = thread_db_mourn_inferior;
1316 thread_db_ops.to_thread_alive = thread_db_thread_alive;
1317 thread_db_ops.to_find_new_threads = thread_db_find_new_threads;
1318 thread_db_ops.to_pid_to_str = thread_db_pid_to_str;
1319 thread_db_ops.to_stratum = thread_stratum;
1320 thread_db_ops.to_has_thread_control = tc_schedlock;
1321 thread_db_ops.to_get_thread_local_address
1322 = thread_db_get_thread_local_address;
1323 thread_db_ops.to_magic = OPS_MAGIC;
1326 void
1327 _initialize_thread_db (void)
1329 /* Only initialize the module if we can load libthread_db. */
1330 if (thread_db_load ())
1332 init_thread_db_ops ();
1333 add_target (&thread_db_ops);
1335 /* Add ourselves to objfile event chain. */
1336 target_new_objfile_chain = deprecated_target_new_objfile_hook;
1337 deprecated_target_new_objfile_hook = thread_db_new_objfile;