]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/linux-thread-db.c
* linux-thread-db.c: Update some FIXME comments.
[thirdparty/binutils-gdb.git] / gdb / linux-thread-db.c
1 /* libthread_db assisted debugging support, generic parts.
2
3 Copyright (C) 1999, 2000, 2001, 2003, 2004, 2005, 2006, 2007
4 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
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.
12
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.
17
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. */
22
23 #include "defs.h"
24
25 #include "gdb_assert.h"
26 #include <dlfcn.h>
27 #include "gdb_proc_service.h"
28 #include "gdb_thread_db.h"
29
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"
41
42 #include <signal.h>
43
44 #ifdef HAVE_GNU_LIBC_VERSION_H
45 #include <gnu/libc-version.h>
46 #endif
47
48 #ifndef LIBTHREAD_DB_SO
49 #define LIBTHREAD_DB_SO "libthread_db.so.1"
50 #endif
51
52 /* If we're running on GNU/Linux, we must explicitly attach to any new
53 threads. */
54
55 /* This module's target vector. */
56 static struct target_ops thread_db_ops;
57
58 /* The target vector that we call for things this module can't handle. */
59 static struct target_ops *target_beneath;
60
61 /* Pointer to the next function on the objfile event chain. */
62 static void (*target_new_objfile_chain) (struct objfile * objfile);
63
64 /* Non-zero if we're using this module's target vector. */
65 static int using_thread_db;
66
67 /* Non-zero if we have determined the signals used by the threads
68 library. */
69 static int thread_signals;
70 static sigset_t thread_stop_set;
71 static sigset_t thread_print_set;
72
73 /* Structure that identifies the child process for the
74 <proc_service.h> interface. */
75 static struct ps_prochandle proc_handle;
76
77 /* Connection to the libthread_db library. */
78 static td_thragent_t *thread_agent;
79
80 /* Pointers to the libthread_db functions. */
81
82 static td_err_e (*td_init_p) (void);
83
84 static td_err_e (*td_ta_new_p) (struct ps_prochandle * ps,
85 td_thragent_t **ta);
86 static td_err_e (*td_ta_map_id2thr_p) (const td_thragent_t *ta, thread_t pt,
87 td_thrhandle_t *__th);
88 static td_err_e (*td_ta_map_lwp2thr_p) (const td_thragent_t *ta,
89 lwpid_t lwpid, td_thrhandle_t *th);
90 static td_err_e (*td_ta_thr_iter_p) (const td_thragent_t *ta,
91 td_thr_iter_f *callback, void *cbdata_p,
92 td_thr_state_e state, int ti_pri,
93 sigset_t *ti_sigmask_p,
94 unsigned int ti_user_flags);
95 static td_err_e (*td_ta_event_addr_p) (const td_thragent_t *ta,
96 td_event_e event, td_notify_t *ptr);
97 static td_err_e (*td_ta_set_event_p) (const td_thragent_t *ta,
98 td_thr_events_t *event);
99 static td_err_e (*td_ta_event_getmsg_p) (const td_thragent_t *ta,
100 td_event_msg_t *msg);
101
102 static td_err_e (*td_thr_validate_p) (const td_thrhandle_t *th);
103 static td_err_e (*td_thr_get_info_p) (const td_thrhandle_t *th,
104 td_thrinfo_t *infop);
105 static td_err_e (*td_thr_event_enable_p) (const td_thrhandle_t *th,
106 int event);
107
108 static td_err_e (*td_thr_tls_get_addr_p) (const td_thrhandle_t *th,
109 void *map_address,
110 size_t offset, void **address);
111
112 /* Location of the thread creation event breakpoint. The code at this
113 location in the child process will be called by the pthread library
114 whenever a new thread is created. By setting a special breakpoint
115 at this location, GDB can detect when a new thread is created. We
116 obtain this location via the td_ta_event_addr call. */
117 static CORE_ADDR td_create_bp_addr;
118
119 /* Location of the thread death event breakpoint. */
120 static CORE_ADDR td_death_bp_addr;
121
122 /* Prototypes for local functions. */
123 static void thread_db_find_new_threads (void);
124 static void attach_thread (ptid_t ptid, const td_thrhandle_t *th_p,
125 const td_thrinfo_t *ti_p, int verbose);
126 static void detach_thread (ptid_t ptid, int verbose);
127 \f
128
129 /* Building process ids. */
130
131 #define GET_PID(ptid) ptid_get_pid (ptid)
132 #define GET_LWP(ptid) ptid_get_lwp (ptid)
133 #define GET_THREAD(ptid) ptid_get_tid (ptid)
134
135 #define is_lwp(ptid) (GET_LWP (ptid) != 0)
136 #define is_thread(ptid) (GET_THREAD (ptid) != 0)
137
138 #define BUILD_LWP(lwp, pid) ptid_build (pid, lwp, 0)
139 \f
140
141 /* Use "struct private_thread_info" to cache thread state. This is
142 a substantial optimization. */
143
144 struct private_thread_info
145 {
146 /* Flag set when we see a TD_DEATH event for this thread. */
147 unsigned int dying:1;
148
149 /* Cached thread state. */
150 unsigned int th_valid:1;
151 unsigned int ti_valid:1;
152
153 td_thrhandle_t th;
154 td_thrinfo_t ti;
155 };
156 \f
157
158 static char *
159 thread_db_err_str (td_err_e err)
160 {
161 static char buf[64];
162
163 switch (err)
164 {
165 case TD_OK:
166 return "generic 'call succeeded'";
167 case TD_ERR:
168 return "generic error";
169 case TD_NOTHR:
170 return "no thread to satisfy query";
171 case TD_NOSV:
172 return "no sync handle to satisfy query";
173 case TD_NOLWP:
174 return "no LWP to satisfy query";
175 case TD_BADPH:
176 return "invalid process handle";
177 case TD_BADTH:
178 return "invalid thread handle";
179 case TD_BADSH:
180 return "invalid synchronization handle";
181 case TD_BADTA:
182 return "invalid thread agent";
183 case TD_BADKEY:
184 return "invalid key";
185 case TD_NOMSG:
186 return "no event message for getmsg";
187 case TD_NOFPREGS:
188 return "FPU register set not available";
189 case TD_NOLIBTHREAD:
190 return "application not linked with libthread";
191 case TD_NOEVENT:
192 return "requested event is not supported";
193 case TD_NOCAPAB:
194 return "capability not available";
195 case TD_DBERR:
196 return "debugger service failed";
197 case TD_NOAPLIC:
198 return "operation not applicable to";
199 case TD_NOTSD:
200 return "no thread-specific data for this thread";
201 case TD_MALLOC:
202 return "malloc failed";
203 case TD_PARTIALREG:
204 return "only part of register set was written/read";
205 case TD_NOXREGS:
206 return "X register set not available for this thread";
207 #ifdef THREAD_DB_HAS_TD_NOTALLOC
208 case TD_NOTALLOC:
209 return "thread has not yet allocated TLS for given module";
210 #endif
211 #ifdef THREAD_DB_HAS_TD_VERSION
212 case TD_VERSION:
213 return "versions of libpthread and libthread_db do not match";
214 #endif
215 #ifdef THREAD_DB_HAS_TD_NOTLS
216 case TD_NOTLS:
217 return "there is no TLS segment in the given module";
218 #endif
219 default:
220 snprintf (buf, sizeof (buf), "unknown thread_db error '%d'", err);
221 return buf;
222 }
223 }
224 \f
225 /* A callback function for td_ta_thr_iter, which we use to map all
226 threads to LWPs.
227
228 THP is a handle to the current thread; if INFOP is not NULL, the
229 struct thread_info associated with this thread is returned in
230 *INFOP.
231
232 If the thread is a zombie, TD_THR_ZOMBIE is returned. Otherwise,
233 zero is returned to indicate success. */
234
235 static int
236 thread_get_info_callback (const td_thrhandle_t *thp, void *infop)
237 {
238 td_thrinfo_t ti;
239 td_err_e err;
240 struct thread_info *thread_info;
241 ptid_t thread_ptid;
242
243 err = td_thr_get_info_p (thp, &ti);
244 if (err != TD_OK)
245 error (_("thread_get_info_callback: cannot get thread info: %s"),
246 thread_db_err_str (err));
247
248 /* Fill the cache. */
249 thread_ptid = ptid_build (GET_PID (inferior_ptid), ti.ti_lid, ti.ti_tid);
250 thread_info = find_thread_pid (thread_ptid);
251
252 /* In the case of a zombie thread, don't continue. We don't want to
253 attach to it thinking it is a new thread. */
254 if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
255 {
256 if (infop != NULL)
257 *(struct thread_info **) infop = thread_info;
258 if (thread_info != NULL)
259 {
260 memcpy (&thread_info->private->th, thp, sizeof (*thp));
261 thread_info->private->th_valid = 1;
262 memcpy (&thread_info->private->ti, &ti, sizeof (ti));
263 thread_info->private->ti_valid = 1;
264 }
265 return TD_THR_ZOMBIE;
266 }
267
268 if (thread_info == NULL)
269 {
270 /* New thread. Attach to it now (why wait?). */
271 attach_thread (thread_ptid, thp, &ti, 1);
272 thread_info = find_thread_pid (thread_ptid);
273 gdb_assert (thread_info != NULL);
274 }
275
276 memcpy (&thread_info->private->th, thp, sizeof (*thp));
277 thread_info->private->th_valid = 1;
278 memcpy (&thread_info->private->ti, &ti, sizeof (ti));
279 thread_info->private->ti_valid = 1;
280
281 if (infop != NULL)
282 *(struct thread_info **) infop = thread_info;
283
284 return 0;
285 }
286
287 /* Accessor functions for the thread_db information, with caching. */
288
289 static void
290 thread_db_map_id2thr (struct thread_info *thread_info, int fatal)
291 {
292 td_err_e err;
293
294 if (thread_info->private->th_valid)
295 return;
296
297 err = td_ta_map_id2thr_p (thread_agent, GET_THREAD (thread_info->ptid),
298 &thread_info->private->th);
299 if (err != TD_OK)
300 {
301 if (fatal)
302 error (_("Cannot find thread %ld: %s"),
303 (long) GET_THREAD (thread_info->ptid),
304 thread_db_err_str (err));
305 }
306 else
307 thread_info->private->th_valid = 1;
308 }
309 \f
310 /* Convert between user-level thread ids and LWP ids. */
311
312 static ptid_t
313 thread_from_lwp (ptid_t ptid)
314 {
315 td_thrhandle_t th;
316 td_err_e err;
317 struct thread_info *thread_info;
318 ptid_t thread_ptid;
319
320 if (GET_LWP (ptid) == 0)
321 ptid = BUILD_LWP (GET_PID (ptid), GET_PID (ptid));
322
323 gdb_assert (is_lwp (ptid));
324
325 err = td_ta_map_lwp2thr_p (thread_agent, GET_LWP (ptid), &th);
326 if (err != TD_OK)
327 error (_("Cannot find user-level thread for LWP %ld: %s"),
328 GET_LWP (ptid), thread_db_err_str (err));
329
330 thread_info = NULL;
331
332 /* Fetch the thread info. If we get back TD_THR_ZOMBIE, then the
333 event thread has already died. If another gdb interface has called
334 thread_alive() previously, the thread won't be found on the thread list
335 anymore. In that case, we don't want to process this ptid anymore
336 to avoid the possibility of later treating it as a newly
337 discovered thread id that we should add to the list. Thus,
338 we return a -1 ptid which is also how the thread list marks a
339 dead thread. */
340 if (thread_get_info_callback (&th, &thread_info) == TD_THR_ZOMBIE
341 && thread_info == NULL)
342 return pid_to_ptid (-1);
343
344 gdb_assert (thread_info && thread_info->private->ti_valid);
345
346 return ptid_build (GET_PID (ptid), GET_LWP (ptid),
347 thread_info->private->ti.ti_tid);
348 }
349
350 static ptid_t
351 lwp_from_thread (ptid_t ptid)
352 {
353 return BUILD_LWP (GET_LWP (ptid), GET_PID (ptid));
354 }
355 \f
356
357 void
358 thread_db_init (struct target_ops *target)
359 {
360 target_beneath = target;
361 }
362
363 static void *
364 verbose_dlsym (void *handle, const char *name)
365 {
366 void *sym = dlsym (handle, name);
367 if (sym == NULL)
368 warning (_("Symbol \"%s\" not found in libthread_db: %s"), name, dlerror ());
369 return sym;
370 }
371
372 static int
373 thread_db_load (void)
374 {
375 void *handle;
376 td_err_e err;
377
378 handle = dlopen (LIBTHREAD_DB_SO, RTLD_NOW);
379 if (handle == NULL)
380 {
381 fprintf_filtered (gdb_stderr, "\n\ndlopen failed on '%s' - %s\n",
382 LIBTHREAD_DB_SO, dlerror ());
383 fprintf_filtered (gdb_stderr,
384 "GDB will not be able to debug pthreads.\n\n");
385 return 0;
386 }
387
388 /* Initialize pointers to the dynamic library functions we will use.
389 Essential functions first. */
390
391 td_init_p = verbose_dlsym (handle, "td_init");
392 if (td_init_p == NULL)
393 return 0;
394
395 td_ta_new_p = verbose_dlsym (handle, "td_ta_new");
396 if (td_ta_new_p == NULL)
397 return 0;
398
399 td_ta_map_id2thr_p = verbose_dlsym (handle, "td_ta_map_id2thr");
400 if (td_ta_map_id2thr_p == NULL)
401 return 0;
402
403 td_ta_map_lwp2thr_p = verbose_dlsym (handle, "td_ta_map_lwp2thr");
404 if (td_ta_map_lwp2thr_p == NULL)
405 return 0;
406
407 td_ta_thr_iter_p = verbose_dlsym (handle, "td_ta_thr_iter");
408 if (td_ta_thr_iter_p == NULL)
409 return 0;
410
411 td_thr_validate_p = verbose_dlsym (handle, "td_thr_validate");
412 if (td_thr_validate_p == NULL)
413 return 0;
414
415 td_thr_get_info_p = verbose_dlsym (handle, "td_thr_get_info");
416 if (td_thr_get_info_p == NULL)
417 return 0;
418
419 /* Initialize the library. */
420 err = td_init_p ();
421 if (err != TD_OK)
422 {
423 warning (_("Cannot initialize libthread_db: %s"), thread_db_err_str (err));
424 return 0;
425 }
426
427 /* These are not essential. */
428 td_ta_event_addr_p = dlsym (handle, "td_ta_event_addr");
429 td_ta_set_event_p = dlsym (handle, "td_ta_set_event");
430 td_ta_event_getmsg_p = dlsym (handle, "td_ta_event_getmsg");
431 td_thr_event_enable_p = dlsym (handle, "td_thr_event_enable");
432 td_thr_tls_get_addr_p = dlsym (handle, "td_thr_tls_get_addr");
433
434 return 1;
435 }
436
437 static td_err_e
438 enable_thread_event (td_thragent_t *thread_agent, int event, CORE_ADDR *bp)
439 {
440 td_notify_t notify;
441 td_err_e err;
442
443 /* Get the breakpoint address for thread EVENT. */
444 err = td_ta_event_addr_p (thread_agent, event, &notify);
445 if (err != TD_OK)
446 return err;
447
448 /* Set up the breakpoint. */
449 gdb_assert (exec_bfd);
450 (*bp) = (gdbarch_convert_from_func_ptr_addr
451 (current_gdbarch,
452 /* Do proper sign extension for the target. */
453 (bfd_get_sign_extend_vma (exec_bfd) > 0
454 ? (CORE_ADDR) (intptr_t) notify.u.bptaddr
455 : (CORE_ADDR) (uintptr_t) notify.u.bptaddr),
456 &current_target));
457 create_thread_event_breakpoint ((*bp));
458
459 return TD_OK;
460 }
461
462 static void
463 enable_thread_event_reporting (void)
464 {
465 td_thr_events_t events;
466 td_notify_t notify;
467 td_err_e err;
468 #ifdef HAVE_GNU_LIBC_VERSION_H
469 const char *libc_version;
470 int libc_major, libc_minor;
471 #endif
472
473 /* We cannot use the thread event reporting facility if these
474 functions aren't available. */
475 if (td_ta_event_addr_p == NULL || td_ta_set_event_p == NULL
476 || td_ta_event_getmsg_p == NULL || td_thr_event_enable_p == NULL)
477 return;
478
479 /* Set the process wide mask saying which events we're interested in. */
480 td_event_emptyset (&events);
481 td_event_addset (&events, TD_CREATE);
482
483 #ifdef HAVE_GNU_LIBC_VERSION_H
484 /* The event reporting facility is broken for TD_DEATH events in
485 glibc 2.1.3, so don't enable it we have glibc but a lower
486 version. */
487 libc_version = gnu_get_libc_version ();
488 if (sscanf (libc_version, "%d.%d", &libc_major, &libc_minor) == 2
489 && (libc_major > 2 || (libc_major == 2 && libc_minor > 1)))
490 #endif
491 td_event_addset (&events, TD_DEATH);
492
493 err = td_ta_set_event_p (thread_agent, &events);
494 if (err != TD_OK)
495 {
496 warning (_("Unable to set global thread event mask: %s"),
497 thread_db_err_str (err));
498 return;
499 }
500
501 /* Delete previous thread event breakpoints, if any. */
502 remove_thread_event_breakpoints ();
503 td_create_bp_addr = 0;
504 td_death_bp_addr = 0;
505
506 /* Set up the thread creation event. */
507 err = enable_thread_event (thread_agent, TD_CREATE, &td_create_bp_addr);
508 if (err != TD_OK)
509 {
510 warning (_("Unable to get location for thread creation breakpoint: %s"),
511 thread_db_err_str (err));
512 return;
513 }
514
515 /* Set up the thread death event. */
516 err = enable_thread_event (thread_agent, TD_DEATH, &td_death_bp_addr);
517 if (err != TD_OK)
518 {
519 warning (_("Unable to get location for thread death breakpoint: %s"),
520 thread_db_err_str (err));
521 return;
522 }
523 }
524
525 static void
526 disable_thread_event_reporting (void)
527 {
528 td_thr_events_t events;
529
530 /* Set the process wide mask saying we aren't interested in any
531 events anymore. */
532 td_event_emptyset (&events);
533 td_ta_set_event_p (thread_agent, &events);
534
535 /* Delete thread event breakpoints, if any. */
536 remove_thread_event_breakpoints ();
537 td_create_bp_addr = 0;
538 td_death_bp_addr = 0;
539 }
540
541 static void
542 check_thread_signals (void)
543 {
544 #ifdef GET_THREAD_SIGNALS
545 if (!thread_signals)
546 {
547 sigset_t mask;
548 int i;
549
550 GET_THREAD_SIGNALS (&mask);
551 sigemptyset (&thread_stop_set);
552 sigemptyset (&thread_print_set);
553
554 for (i = 1; i < NSIG; i++)
555 {
556 if (sigismember (&mask, i))
557 {
558 if (signal_stop_update (target_signal_from_host (i), 0))
559 sigaddset (&thread_stop_set, i);
560 if (signal_print_update (target_signal_from_host (i), 0))
561 sigaddset (&thread_print_set, i);
562 thread_signals = 1;
563 }
564 }
565 }
566 #endif
567 }
568
569 /* Check whether thread_db is usable. This function is called when
570 an inferior is created (or otherwise acquired, e.g. attached to)
571 and when new shared libraries are loaded into a running process. */
572
573 void
574 check_for_thread_db (void)
575 {
576 td_err_e err;
577 static int already_loaded;
578
579 /* Do nothing if we couldn't load libthread_db.so.1. */
580 if (td_ta_new_p == NULL)
581 return;
582
583 /* First time through, report that libthread_db was successfuly
584 loaded. Can't print this in in thread_db_load as, at that stage,
585 the interpreter and it's console haven't started. */
586
587 if (!already_loaded)
588 {
589 Dl_info info;
590 const char *library = NULL;
591 if (dladdr ((*td_ta_new_p), &info) != 0)
592 library = info.dli_fname;
593
594 /* Try dlinfo? */
595
596 if (library == NULL)
597 /* Paranoid - don't let a NULL path slip through. */
598 library = LIBTHREAD_DB_SO;
599
600 printf_unfiltered (_("Using host libthread_db library \"%s\".\n"),
601 library);
602 already_loaded = 1;
603 }
604
605 if (using_thread_db)
606 /* Nothing to do. The thread library was already detected and the
607 target vector was already activated. */
608 return;
609
610 /* Don't attempt to use thread_db on targets which can not run
611 (executables not running yet, core files) for now. */
612 if (!target_has_execution)
613 return;
614
615 /* Don't attempt to use thread_db for remote targets. */
616 if (!target_can_run (&current_target))
617 return;
618
619 /* Initialize the structure that identifies the child process. */
620 proc_handle.pid = GET_PID (inferior_ptid);
621
622 /* Now attempt to open a connection to the thread library. */
623 err = td_ta_new_p (&proc_handle, &thread_agent);
624 switch (err)
625 {
626 case TD_NOLIBTHREAD:
627 /* No thread library was detected. */
628 break;
629
630 case TD_OK:
631 printf_unfiltered (_("[Thread debugging using libthread_db enabled]\n"));
632
633 /* The thread library was detected. Activate the thread_db target. */
634 push_target (&thread_db_ops);
635 using_thread_db = 1;
636
637 enable_thread_event_reporting ();
638 thread_db_find_new_threads ();
639 break;
640
641 default:
642 warning (_("Cannot initialize thread debugging library: %s"),
643 thread_db_err_str (err));
644 break;
645 }
646 }
647
648 static void
649 thread_db_new_objfile (struct objfile *objfile)
650 {
651 if (objfile != NULL)
652 check_for_thread_db ();
653
654 if (target_new_objfile_chain)
655 target_new_objfile_chain (objfile);
656 }
657
658 /* Attach to a new thread. This function is called when we receive a
659 TD_CREATE event or when we iterate over all threads and find one
660 that wasn't already in our list. */
661
662 static void
663 attach_thread (ptid_t ptid, const td_thrhandle_t *th_p,
664 const td_thrinfo_t *ti_p, int verbose)
665 {
666 struct thread_info *tp;
667 td_err_e err;
668
669 /* If we're being called after a TD_CREATE event, we may already
670 know about this thread. There are two ways this can happen. We
671 may have iterated over all threads between the thread creation
672 and the TD_CREATE event, for instance when the user has issued
673 the `info threads' command before the SIGTRAP for hitting the
674 thread creation breakpoint was reported. Alternatively, the
675 thread may have exited and a new one been created with the same
676 thread ID. In the first case we don't need to do anything; in
677 the second case we should discard information about the dead
678 thread and attach to the new one. */
679 if (in_thread_list (ptid))
680 {
681 tp = find_thread_pid (ptid);
682 gdb_assert (tp != NULL);
683
684 if (!tp->private->dying)
685 return;
686
687 delete_thread (ptid);
688 }
689
690 check_thread_signals ();
691
692 if (ti_p->ti_state == TD_THR_UNKNOWN || ti_p->ti_state == TD_THR_ZOMBIE)
693 return; /* A zombie thread -- do not attach. */
694
695 /* Under GNU/Linux, we have to attach to each and every thread. */
696 if (lin_lwp_attach_lwp (BUILD_LWP (ti_p->ti_lid, GET_PID (ptid)), 0) < 0)
697 return;
698
699 /* Add the thread to GDB's thread list. */
700 tp = add_thread (ptid);
701 tp->private = xmalloc (sizeof (struct private_thread_info));
702 memset (tp->private, 0, sizeof (struct private_thread_info));
703
704 if (verbose)
705 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid));
706
707 /* Enable thread event reporting for this thread. */
708 err = td_thr_event_enable_p (th_p, 1);
709 if (err != TD_OK)
710 error (_("Cannot enable thread event reporting for %s: %s"),
711 target_pid_to_str (ptid), thread_db_err_str (err));
712 }
713
714 static void
715 thread_db_attach (char *args, int from_tty)
716 {
717 target_beneath->to_attach (args, from_tty);
718
719 /* Destroy thread info; it's no longer valid. */
720 init_thread_list ();
721
722 /* The child process is now the actual multi-threaded
723 program. Snatch its process ID... */
724 proc_handle.pid = GET_PID (inferior_ptid);
725
726 /* ...and perform the remaining initialization steps. */
727 enable_thread_event_reporting ();
728 thread_db_find_new_threads ();
729 }
730
731 static void
732 detach_thread (ptid_t ptid, int verbose)
733 {
734 struct thread_info *thread_info;
735
736 if (verbose)
737 printf_unfiltered (_("[%s exited]\n"), target_pid_to_str (ptid));
738
739 /* Don't delete the thread now, because it still reports as active
740 until it has executed a few instructions after the event
741 breakpoint - if we deleted it now, "info threads" would cause us
742 to re-attach to it. Just mark it as having had a TD_DEATH
743 event. This means that we won't delete it from our thread list
744 until we notice that it's dead (via prune_threads), or until
745 something re-uses its thread ID. */
746 thread_info = find_thread_pid (ptid);
747 gdb_assert (thread_info != NULL);
748 thread_info->private->dying = 1;
749 }
750
751 static void
752 thread_db_detach (char *args, int from_tty)
753 {
754 disable_thread_event_reporting ();
755
756 /* There's no need to save & restore inferior_ptid here, since the
757 inferior is supposed to be survive this function call. */
758 inferior_ptid = lwp_from_thread (inferior_ptid);
759
760 /* Forget about the child's process ID. We shouldn't need it
761 anymore. */
762 proc_handle.pid = 0;
763
764 target_beneath->to_detach (args, from_tty);
765 }
766
767 static int
768 clear_lwpid_callback (struct thread_info *thread, void *dummy)
769 {
770 /* If we know that our thread implementation is 1-to-1, we could save
771 a certain amount of information; it's not clear how much, so we
772 are always conservative. */
773
774 thread->private->th_valid = 0;
775 thread->private->ti_valid = 0;
776
777 return 0;
778 }
779
780 static void
781 thread_db_resume (ptid_t ptid, int step, enum target_signal signo)
782 {
783 struct cleanup *old_chain = save_inferior_ptid ();
784
785 if (GET_PID (ptid) == -1)
786 inferior_ptid = lwp_from_thread (inferior_ptid);
787 else if (is_thread (ptid))
788 ptid = lwp_from_thread (ptid);
789
790 /* Clear cached data which may not be valid after the resume. */
791 iterate_over_threads (clear_lwpid_callback, NULL);
792
793 target_beneath->to_resume (ptid, step, signo);
794
795 do_cleanups (old_chain);
796 }
797
798 /* Check if PID is currently stopped at the location of a thread event
799 breakpoint location. If it is, read the event message and act upon
800 the event. */
801
802 static void
803 check_event (ptid_t ptid)
804 {
805 td_event_msg_t msg;
806 td_thrinfo_t ti;
807 td_err_e err;
808 CORE_ADDR stop_pc;
809 int loop = 0;
810
811 /* Bail out early if we're not at a thread event breakpoint. */
812 stop_pc = read_pc_pid (ptid) - DECR_PC_AFTER_BREAK;
813 if (stop_pc != td_create_bp_addr && stop_pc != td_death_bp_addr)
814 return;
815
816 /* If we are at a create breakpoint, we do not know what new lwp
817 was created and cannot specifically locate the event message for it.
818 We have to call td_ta_event_getmsg() to get
819 the latest message. Since we have no way of correlating whether
820 the event message we get back corresponds to our breakpoint, we must
821 loop and read all event messages, processing them appropriately.
822 This guarantees we will process the correct message before continuing
823 from the breakpoint.
824
825 Currently, death events are not enabled. If they are enabled,
826 the death event can use the td_thr_event_getmsg() interface to
827 get the message specifically for that lwp and avoid looping
828 below. */
829
830 loop = 1;
831
832 do
833 {
834 err = td_ta_event_getmsg_p (thread_agent, &msg);
835 if (err != TD_OK)
836 {
837 if (err == TD_NOMSG)
838 return;
839
840 error (_("Cannot get thread event message: %s"),
841 thread_db_err_str (err));
842 }
843
844 err = td_thr_get_info_p (msg.th_p, &ti);
845 if (err != TD_OK)
846 error (_("Cannot get thread info: %s"), thread_db_err_str (err));
847
848 ptid = ptid_build (GET_PID (ptid), ti.ti_lid, ti.ti_tid);
849
850 switch (msg.event)
851 {
852 case TD_CREATE:
853 /* Call attach_thread whether or not we already know about a
854 thread with this thread ID. */
855 attach_thread (ptid, msg.th_p, &ti, 1);
856
857 break;
858
859 case TD_DEATH:
860
861 if (!in_thread_list (ptid))
862 error (_("Spurious thread death event."));
863
864 detach_thread (ptid, 1);
865
866 break;
867
868 default:
869 error (_("Spurious thread event."));
870 }
871 }
872 while (loop);
873 }
874
875 static ptid_t
876 thread_db_wait (ptid_t ptid, struct target_waitstatus *ourstatus)
877 {
878 extern ptid_t trap_ptid;
879
880 if (GET_PID (ptid) != -1 && is_thread (ptid))
881 ptid = lwp_from_thread (ptid);
882
883 ptid = target_beneath->to_wait (ptid, ourstatus);
884
885 if (proc_handle.pid == 0)
886 /* The current child process isn't the actual multi-threaded
887 program yet, so don't try to do any special thread-specific
888 post-processing and bail out early. */
889 return ptid;
890
891 if (ourstatus->kind == TARGET_WAITKIND_EXITED
892 || ourstatus->kind == TARGET_WAITKIND_SIGNALLED)
893 return pid_to_ptid (-1);
894
895 if (ourstatus->kind == TARGET_WAITKIND_EXECD)
896 {
897 remove_thread_event_breakpoints ();
898 unpush_target (&thread_db_ops);
899 using_thread_db = 0;
900
901 return pid_to_ptid (GET_PID (ptid));
902 }
903
904 if (ourstatus->kind == TARGET_WAITKIND_STOPPED
905 && ourstatus->value.sig == TARGET_SIGNAL_TRAP)
906 /* Check for a thread event. */
907 check_event (ptid);
908
909 if (!ptid_equal (trap_ptid, null_ptid))
910 trap_ptid = thread_from_lwp (trap_ptid);
911
912 /* Change the ptid back into the higher level PID + TID format.
913 If the thread is dead and no longer on the thread list, we will
914 get back a dead ptid. This can occur if the thread death event
915 gets postponed by other simultaneous events. In such a case,
916 we want to just ignore the event and continue on. */
917 ptid = thread_from_lwp (ptid);
918 if (GET_PID (ptid) == -1)
919 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
920
921 return ptid;
922 }
923
924 static void
925 thread_db_kill (void)
926 {
927 /* There's no need to save & restore inferior_ptid here, since the
928 inferior isn't supposed to survive this function call. */
929 inferior_ptid = lwp_from_thread (inferior_ptid);
930 target_beneath->to_kill ();
931 }
932
933 static void
934 thread_db_create_inferior (char *exec_file, char *allargs, char **env,
935 int from_tty)
936 {
937 unpush_target (&thread_db_ops);
938 using_thread_db = 0;
939 target_beneath->to_create_inferior (exec_file, allargs, env, from_tty);
940 }
941
942 static void
943 thread_db_post_startup_inferior (ptid_t ptid)
944 {
945 if (proc_handle.pid == 0)
946 {
947 /* The child process is now the actual multi-threaded
948 program. Snatch its process ID... */
949 proc_handle.pid = GET_PID (ptid);
950
951 /* ...and perform the remaining initialization steps. */
952 enable_thread_event_reporting ();
953 thread_db_find_new_threads ();
954 }
955 }
956
957 static void
958 thread_db_mourn_inferior (void)
959 {
960 /* Forget about the child's process ID. We shouldn't need it
961 anymore. */
962 proc_handle.pid = 0;
963
964 target_beneath->to_mourn_inferior ();
965
966 /* Delete the old thread event breakpoints. Do this after mourning
967 the inferior, so that we don't try to uninsert them. */
968 remove_thread_event_breakpoints ();
969
970 /* Detach thread_db target ops. */
971 unpush_target (&thread_db_ops);
972 using_thread_db = 0;
973 }
974
975 static int
976 find_new_threads_callback (const td_thrhandle_t *th_p, void *data)
977 {
978 td_thrinfo_t ti;
979 td_err_e err;
980 ptid_t ptid;
981
982 err = td_thr_get_info_p (th_p, &ti);
983 if (err != TD_OK)
984 error (_("find_new_threads_callback: cannot get thread info: %s"),
985 thread_db_err_str (err));
986
987 if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
988 return 0; /* A zombie -- ignore. */
989
990 ptid = ptid_build (GET_PID (inferior_ptid), ti.ti_lid, ti.ti_tid);
991
992 if (!in_thread_list (ptid))
993 attach_thread (ptid, th_p, &ti, 1);
994
995 return 0;
996 }
997
998 static void
999 thread_db_find_new_threads (void)
1000 {
1001 td_err_e err;
1002
1003 /* Iterate over all user-space threads to discover new threads. */
1004 err = td_ta_thr_iter_p (thread_agent, find_new_threads_callback, NULL,
1005 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
1006 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
1007 if (err != TD_OK)
1008 error (_("Cannot find new threads: %s"), thread_db_err_str (err));
1009 }
1010
1011 static char *
1012 thread_db_pid_to_str (ptid_t ptid)
1013 {
1014 if (is_thread (ptid))
1015 {
1016 static char buf[64];
1017 struct thread_info *thread_info;
1018
1019 thread_info = find_thread_pid (ptid);
1020 if (thread_info == NULL)
1021 snprintf (buf, sizeof (buf), "Thread %ld (LWP %ld) (Missing)",
1022 GET_THREAD (ptid), GET_LWP (ptid));
1023 else
1024 snprintf (buf, sizeof (buf), "Thread %ld (LWP %ld)",
1025 GET_THREAD (ptid), GET_LWP (ptid));
1026
1027 return buf;
1028 }
1029
1030 if (target_beneath->to_pid_to_str (ptid))
1031 return target_beneath->to_pid_to_str (ptid);
1032
1033 return normal_pid_to_str (ptid);
1034 }
1035
1036 /* Return a string describing the state of the thread specified by
1037 INFO. */
1038
1039 static char *
1040 thread_db_extra_thread_info (struct thread_info *info)
1041 {
1042 if (info->private->dying)
1043 return "Exiting";
1044
1045 return NULL;
1046 }
1047
1048 /* Get the address of the thread local variable in load module LM which
1049 is stored at OFFSET within the thread local storage for thread PTID. */
1050
1051 static CORE_ADDR
1052 thread_db_get_thread_local_address (ptid_t ptid,
1053 CORE_ADDR lm,
1054 CORE_ADDR offset)
1055 {
1056 if (is_thread (ptid))
1057 {
1058 td_err_e err;
1059 void *address;
1060 struct thread_info *thread_info;
1061
1062 /* glibc doesn't provide the needed interface. */
1063 if (!td_thr_tls_get_addr_p)
1064 throw_error (TLS_NO_LIBRARY_SUPPORT_ERROR,
1065 _("No TLS library support"));
1066
1067 /* Caller should have verified that lm != 0. */
1068 gdb_assert (lm != 0);
1069
1070 /* Get info about the thread. */
1071 thread_info = find_thread_pid (ptid);
1072 thread_db_map_id2thr (thread_info, 1);
1073
1074 /* Finally, get the address of the variable. */
1075 err = td_thr_tls_get_addr_p (&thread_info->private->th,
1076 (void *)(size_t) lm,
1077 offset, &address);
1078
1079 #ifdef THREAD_DB_HAS_TD_NOTALLOC
1080 /* The memory hasn't been allocated, yet. */
1081 if (err == TD_NOTALLOC)
1082 /* Now, if libthread_db provided the initialization image's
1083 address, we *could* try to build a non-lvalue value from
1084 the initialization image. */
1085 throw_error (TLS_NOT_ALLOCATED_YET_ERROR,
1086 _("TLS not allocated yet"));
1087 #endif
1088
1089 /* Something else went wrong. */
1090 if (err != TD_OK)
1091 throw_error (TLS_GENERIC_ERROR,
1092 (("%s")), thread_db_err_str (err));
1093
1094 /* Cast assuming host == target. Joy. */
1095 /* Do proper sign extension for the target. */
1096 gdb_assert (exec_bfd);
1097 return (bfd_get_sign_extend_vma (exec_bfd) > 0
1098 ? (CORE_ADDR) (intptr_t) address
1099 : (CORE_ADDR) (uintptr_t) address);
1100 }
1101
1102 if (target_beneath->to_get_thread_local_address)
1103 return target_beneath->to_get_thread_local_address (ptid, lm, offset);
1104 else
1105 throw_error (TLS_GENERIC_ERROR,
1106 _("TLS not supported on this target"));
1107 }
1108
1109 static void
1110 init_thread_db_ops (void)
1111 {
1112 thread_db_ops.to_shortname = "multi-thread";
1113 thread_db_ops.to_longname = "multi-threaded child process.";
1114 thread_db_ops.to_doc = "Threads and pthreads support.";
1115 thread_db_ops.to_attach = thread_db_attach;
1116 thread_db_ops.to_detach = thread_db_detach;
1117 thread_db_ops.to_resume = thread_db_resume;
1118 thread_db_ops.to_wait = thread_db_wait;
1119 thread_db_ops.to_kill = thread_db_kill;
1120 thread_db_ops.to_create_inferior = thread_db_create_inferior;
1121 thread_db_ops.to_post_startup_inferior = thread_db_post_startup_inferior;
1122 thread_db_ops.to_mourn_inferior = thread_db_mourn_inferior;
1123 thread_db_ops.to_find_new_threads = thread_db_find_new_threads;
1124 thread_db_ops.to_pid_to_str = thread_db_pid_to_str;
1125 thread_db_ops.to_stratum = thread_stratum;
1126 thread_db_ops.to_has_thread_control = tc_schedlock;
1127 thread_db_ops.to_get_thread_local_address
1128 = thread_db_get_thread_local_address;
1129 thread_db_ops.to_extra_thread_info = thread_db_extra_thread_info;
1130 thread_db_ops.to_magic = OPS_MAGIC;
1131 }
1132
1133 void
1134 _initialize_thread_db (void)
1135 {
1136 /* Only initialize the module if we can load libthread_db. */
1137 if (thread_db_load ())
1138 {
1139 init_thread_db_ops ();
1140 add_target (&thread_db_ops);
1141
1142 /* Add ourselves to objfile event chain. */
1143 target_new_objfile_chain = deprecated_target_new_objfile_hook;
1144 deprecated_target_new_objfile_hook = thread_db_new_objfile;
1145 }
1146 }