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