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