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