]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/linux-thread-db.c
gdb/
[thirdparty/binutils-gdb.git] / gdb / linux-thread-db.c
1 /* libthread_db assisted debugging support, generic parts.
2
3 Copyright (C) 1999, 2000, 2001, 2003, 2004, 2005, 2006, 2007, 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 "command.h"
30 #include "exceptions.h"
31 #include "gdbcmd.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.h"
39 #include "solib-svr4.h"
40 #include "gdbcore.h"
41 #include "observer.h"
42 #include "linux-nat.h"
43
44 #include <signal.h>
45
46 #ifdef HAVE_GNU_LIBC_VERSION_H
47 #include <gnu/libc-version.h>
48 #endif
49
50 /* GNU/Linux libthread_db support.
51
52 libthread_db is a library, provided along with libpthread.so, which
53 exposes the internals of the thread library to a debugger. It
54 allows GDB to find existing threads, new threads as they are
55 created, thread IDs (usually, the result of pthread_self), and
56 thread-local variables.
57
58 The libthread_db interface originates on Solaris, where it is
59 both more powerful and more complicated. This implementation
60 only works for LinuxThreads and NPTL, the two glibc threading
61 libraries. It assumes that each thread is permanently assigned
62 to a single light-weight process (LWP).
63
64 libthread_db-specific information is stored in the "private" field
65 of struct thread_info. When the field is NULL we do not yet have
66 information about the new thread; this could be temporary (created,
67 but the thread library's data structures do not reflect it yet)
68 or permanent (created using clone instead of pthread_create).
69
70 Process IDs managed by linux-thread-db.c match those used by
71 linux-nat.c: a common PID for all processes, an LWP ID for each
72 thread, and no TID. We save the TID in private. Keeping it out
73 of the ptid_t prevents thread IDs changing when libpthread is
74 loaded or unloaded. */
75
76 static char *libthread_db_search_path;
77
78 /* If we're running on GNU/Linux, we must explicitly attach to any new
79 threads. */
80
81 /* This module's target vector. */
82 static struct target_ops thread_db_ops;
83
84 /* Non-zero if we have determined the signals used by the threads
85 library. */
86 static int thread_signals;
87 static sigset_t thread_stop_set;
88 static sigset_t thread_print_set;
89
90 struct thread_db_info
91 {
92 struct thread_db_info *next;
93
94 /* Process id this object refers to. */
95 int pid;
96
97 /* Handle from dlopen for libthread_db.so. */
98 void *handle;
99
100 /* Structure that identifies the child process for the
101 <proc_service.h> interface. */
102 struct ps_prochandle proc_handle;
103
104 /* Connection to the libthread_db library. */
105 td_thragent_t *thread_agent;
106
107 /* True if we need to apply the workaround for glibc/BZ5983. When
108 we catch a PTRACE_O_TRACEFORK, and go query the child's thread
109 list, nptl_db returns the parent's threads in addition to the new
110 (single) child thread. If this flag is set, we do extra work to
111 be able to ignore such stale entries. */
112 int need_stale_parent_threads_check;
113
114 /* Location of the thread creation event breakpoint. The code at
115 this location in the child process will be called by the pthread
116 library whenever a new thread is created. By setting a special
117 breakpoint at this location, GDB can detect when a new thread is
118 created. We obtain this location via the td_ta_event_addr
119 call. */
120 CORE_ADDR td_create_bp_addr;
121
122 /* Location of the thread death event breakpoint. */
123 CORE_ADDR td_death_bp_addr;
124
125 /* Pointers to the libthread_db functions. */
126
127 td_err_e (*td_init_p) (void);
128
129 td_err_e (*td_ta_new_p) (struct ps_prochandle * ps,
130 td_thragent_t **ta);
131 td_err_e (*td_ta_map_id2thr_p) (const td_thragent_t *ta, thread_t pt,
132 td_thrhandle_t *__th);
133 td_err_e (*td_ta_map_lwp2thr_p) (const td_thragent_t *ta,
134 lwpid_t lwpid, td_thrhandle_t *th);
135 td_err_e (*td_ta_thr_iter_p) (const td_thragent_t *ta,
136 td_thr_iter_f *callback, void *cbdata_p,
137 td_thr_state_e state, int ti_pri,
138 sigset_t *ti_sigmask_p,
139 unsigned int ti_user_flags);
140 td_err_e (*td_ta_event_addr_p) (const td_thragent_t *ta,
141 td_event_e event, td_notify_t *ptr);
142 td_err_e (*td_ta_set_event_p) (const td_thragent_t *ta,
143 td_thr_events_t *event);
144 td_err_e (*td_ta_clear_event_p) (const td_thragent_t *ta,
145 td_thr_events_t *event);
146 td_err_e (*td_ta_event_getmsg_p) (const td_thragent_t *ta,
147 td_event_msg_t *msg);
148
149 td_err_e (*td_thr_validate_p) (const td_thrhandle_t *th);
150 td_err_e (*td_thr_get_info_p) (const td_thrhandle_t *th,
151 td_thrinfo_t *infop);
152 td_err_e (*td_thr_event_enable_p) (const td_thrhandle_t *th,
153 int event);
154
155 td_err_e (*td_thr_tls_get_addr_p) (const td_thrhandle_t *th,
156 psaddr_t map_address,
157 size_t offset, psaddr_t *address);
158 };
159
160 /* List of known processes using thread_db, and the required
161 bookkeeping. */
162 struct thread_db_info *thread_db_list;
163
164 static void thread_db_find_new_threads_1 (ptid_t ptid);
165 static void thread_db_find_new_threads_2 (ptid_t ptid, int until_no_new);
166
167 /* Add the current inferior to the list of processes using libpthread.
168 Return a pointer to the newly allocated object that was added to
169 THREAD_DB_LIST. HANDLE is the handle returned by dlopen'ing
170 LIBTHREAD_DB_SO. */
171
172 static struct thread_db_info *
173 add_thread_db_info (void *handle)
174 {
175 int pid;
176 struct thread_db_info *info;
177
178 info = xcalloc (1, sizeof (*info));
179 info->pid = ptid_get_pid (inferior_ptid);
180 info->handle = handle;
181 info->need_stale_parent_threads_check = 1;
182
183 info->next = thread_db_list;
184 thread_db_list = info;
185
186 return info;
187 }
188
189 /* Return the thread_db_info object representing the bookkeeping
190 related to process PID, if any; NULL otherwise. */
191
192 static struct thread_db_info *
193 get_thread_db_info (int pid)
194 {
195 struct thread_db_info *info;
196
197 for (info = thread_db_list; info; info = info->next)
198 if (pid == info->pid)
199 return info;
200
201 return NULL;
202 }
203
204 /* When PID has exited or has been detached, we no longer want to keep
205 track of it as using libpthread. Call this function to discard
206 thread_db related info related to PID. Note that this closes
207 LIBTHREAD_DB_SO's dlopen'ed handle. */
208
209 static void
210 delete_thread_db_info (int pid)
211 {
212 struct thread_db_info *info, *info_prev;
213
214 info_prev = NULL;
215
216 for (info = thread_db_list; info; info_prev = info, info = info->next)
217 if (pid == info->pid)
218 break;
219
220 if (info == NULL)
221 return;
222
223 if (info->handle != NULL)
224 dlclose (info->handle);
225
226 if (info_prev)
227 info_prev->next = info->next;
228 else
229 thread_db_list = info->next;
230
231 xfree (info);
232 }
233
234 /* Prototypes for local functions. */
235 static int attach_thread (ptid_t ptid, const td_thrhandle_t *th_p,
236 const td_thrinfo_t *ti_p);
237 static void detach_thread (ptid_t ptid);
238 \f
239
240 /* Use "struct private_thread_info" to cache thread state. This is
241 a substantial optimization. */
242
243 struct private_thread_info
244 {
245 /* Flag set when we see a TD_DEATH event for this thread. */
246 unsigned int dying:1;
247
248 /* Cached thread state. */
249 td_thrhandle_t th;
250 thread_t tid;
251 };
252 \f
253
254 static char *
255 thread_db_err_str (td_err_e err)
256 {
257 static char buf[64];
258
259 switch (err)
260 {
261 case TD_OK:
262 return "generic 'call succeeded'";
263 case TD_ERR:
264 return "generic error";
265 case TD_NOTHR:
266 return "no thread to satisfy query";
267 case TD_NOSV:
268 return "no sync handle to satisfy query";
269 case TD_NOLWP:
270 return "no LWP to satisfy query";
271 case TD_BADPH:
272 return "invalid process handle";
273 case TD_BADTH:
274 return "invalid thread handle";
275 case TD_BADSH:
276 return "invalid synchronization handle";
277 case TD_BADTA:
278 return "invalid thread agent";
279 case TD_BADKEY:
280 return "invalid key";
281 case TD_NOMSG:
282 return "no event message for getmsg";
283 case TD_NOFPREGS:
284 return "FPU register set not available";
285 case TD_NOLIBTHREAD:
286 return "application not linked with libthread";
287 case TD_NOEVENT:
288 return "requested event is not supported";
289 case TD_NOCAPAB:
290 return "capability not available";
291 case TD_DBERR:
292 return "debugger service failed";
293 case TD_NOAPLIC:
294 return "operation not applicable to";
295 case TD_NOTSD:
296 return "no thread-specific data for this thread";
297 case TD_MALLOC:
298 return "malloc failed";
299 case TD_PARTIALREG:
300 return "only part of register set was written/read";
301 case TD_NOXREGS:
302 return "X register set not available for this thread";
303 #ifdef THREAD_DB_HAS_TD_NOTALLOC
304 case TD_NOTALLOC:
305 return "thread has not yet allocated TLS for given module";
306 #endif
307 #ifdef THREAD_DB_HAS_TD_VERSION
308 case TD_VERSION:
309 return "versions of libpthread and libthread_db do not match";
310 #endif
311 #ifdef THREAD_DB_HAS_TD_NOTLS
312 case TD_NOTLS:
313 return "there is no TLS segment in the given module";
314 #endif
315 default:
316 snprintf (buf, sizeof (buf), "unknown thread_db error '%d'", err);
317 return buf;
318 }
319 }
320 \f
321 /* Return 1 if any threads have been registered. There may be none if
322 the threading library is not fully initialized yet. */
323
324 static int
325 have_threads_callback (struct thread_info *thread, void *args)
326 {
327 int pid = * (int *) args;
328 if (ptid_get_pid (thread->ptid) != pid)
329 return 0;
330
331 return thread->private != NULL;
332 }
333
334 static int
335 have_threads (ptid_t ptid)
336 {
337 int pid = ptid_get_pid (ptid);
338
339 return iterate_over_threads (have_threads_callback, &pid) != NULL;
340 }
341
342 struct thread_get_info_inout
343 {
344 struct thread_info *thread_info;
345 struct thread_db_info *thread_db_info;
346 };
347
348 /* A callback function for td_ta_thr_iter, which we use to map all
349 threads to LWPs.
350
351 THP is a handle to the current thread; if INFOP is not NULL, the
352 struct thread_info associated with this thread is returned in
353 *INFOP.
354
355 If the thread is a zombie, TD_THR_ZOMBIE is returned. Otherwise,
356 zero is returned to indicate success. */
357
358 static int
359 thread_get_info_callback (const td_thrhandle_t *thp, void *argp)
360 {
361 td_thrinfo_t ti;
362 td_err_e err;
363 ptid_t thread_ptid;
364 struct thread_get_info_inout *inout;
365 struct thread_db_info *info;
366
367 inout = argp;
368 info = inout->thread_db_info;
369
370 err = info->td_thr_get_info_p (thp, &ti);
371 if (err != TD_OK)
372 error (_("thread_get_info_callback: cannot get thread info: %s"),
373 thread_db_err_str (err));
374
375 /* Fill the cache. */
376 thread_ptid = ptid_build (info->pid, ti.ti_lid, 0);
377 inout->thread_info = find_thread_ptid (thread_ptid);
378
379 /* In the case of a zombie thread, don't continue. We don't want to
380 attach to it thinking it is a new thread. */
381 if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
382 return TD_THR_ZOMBIE;
383
384 if (inout->thread_info == NULL)
385 {
386 /* New thread. Attach to it now (why wait?). */
387 if (!have_threads (thread_ptid))
388 thread_db_find_new_threads_1 (thread_ptid);
389 else
390 attach_thread (thread_ptid, thp, &ti);
391 inout->thread_info = find_thread_ptid (thread_ptid);
392 gdb_assert (inout->thread_info != NULL);
393 }
394
395 return 0;
396 }
397 \f
398 /* Convert between user-level thread ids and LWP ids. */
399
400 static ptid_t
401 thread_from_lwp (ptid_t ptid)
402 {
403 td_thrhandle_t th;
404 td_err_e err;
405 ptid_t thread_ptid;
406 struct thread_db_info *info;
407 struct thread_get_info_inout io = {0};
408
409 /* This ptid comes from linux-nat.c, which should always fill in the
410 LWP. */
411 gdb_assert (GET_LWP (ptid) != 0);
412
413 info = get_thread_db_info (GET_PID (ptid));
414
415 /* Access an lwp we know is stopped. */
416 info->proc_handle.ptid = ptid;
417 err = info->td_ta_map_lwp2thr_p (info->thread_agent, GET_LWP (ptid), &th);
418 if (err != TD_OK)
419 error (_("Cannot find user-level thread for LWP %ld: %s"),
420 GET_LWP (ptid), thread_db_err_str (err));
421
422 /* Fetch the thread info. If we get back TD_THR_ZOMBIE, then the
423 event thread has already died. If another gdb interface has called
424 thread_alive() previously, the thread won't be found on the thread list
425 anymore. In that case, we don't want to process this ptid anymore
426 to avoid the possibility of later treating it as a newly
427 discovered thread id that we should add to the list. Thus,
428 we return a -1 ptid which is also how the thread list marks a
429 dead thread. */
430 io.thread_db_info = info;
431 io.thread_info = NULL;
432 if (thread_get_info_callback (&th, &io) == TD_THR_ZOMBIE
433 && io.thread_info == NULL)
434 return minus_one_ptid;
435
436 gdb_assert (ptid_get_tid (ptid) == 0);
437 return ptid;
438 }
439 \f
440
441 /* Attach to lwp PTID, doing whatever else is required to have this
442 LWP under the debugger's control --- e.g., enabling event
443 reporting. Returns true on success. */
444 int
445 thread_db_attach_lwp (ptid_t ptid)
446 {
447 td_thrhandle_t th;
448 td_thrinfo_t ti;
449 td_err_e err;
450 struct thread_db_info *info;
451
452 info = get_thread_db_info (GET_PID (ptid));
453
454 if (info == NULL)
455 return 0;
456
457 /* This ptid comes from linux-nat.c, which should always fill in the
458 LWP. */
459 gdb_assert (GET_LWP (ptid) != 0);
460
461 /* Access an lwp we know is stopped. */
462 info->proc_handle.ptid = ptid;
463
464 /* If we have only looked at the first thread before libpthread was
465 initialized, we may not know its thread ID yet. Make sure we do
466 before we add another thread to the list. */
467 if (!have_threads (ptid))
468 thread_db_find_new_threads_1 (ptid);
469
470 err = info->td_ta_map_lwp2thr_p (info->thread_agent, GET_LWP (ptid), &th);
471 if (err != TD_OK)
472 /* Cannot find user-level thread. */
473 return 0;
474
475 err = info->td_thr_get_info_p (&th, &ti);
476 if (err != TD_OK)
477 {
478 warning (_("Cannot get thread info: %s"), thread_db_err_str (err));
479 return 0;
480 }
481
482 attach_thread (ptid, &th, &ti);
483 return 1;
484 }
485
486 static void *
487 verbose_dlsym (void *handle, const char *name)
488 {
489 void *sym = dlsym (handle, name);
490 if (sym == NULL)
491 warning (_("Symbol \"%s\" not found in libthread_db: %s"), name, dlerror ());
492 return sym;
493 }
494
495 static td_err_e
496 enable_thread_event (int event, CORE_ADDR *bp)
497 {
498 td_notify_t notify;
499 td_err_e err;
500 struct thread_db_info *info;
501
502 info = get_thread_db_info (GET_PID (inferior_ptid));
503
504 /* Access an lwp we know is stopped. */
505 info->proc_handle.ptid = inferior_ptid;
506
507 /* Get the breakpoint address for thread EVENT. */
508 err = info->td_ta_event_addr_p (info->thread_agent, event, &notify);
509 if (err != TD_OK)
510 return err;
511
512 /* Set up the breakpoint. */
513 gdb_assert (exec_bfd);
514 (*bp) = (gdbarch_convert_from_func_ptr_addr
515 (target_gdbarch,
516 /* Do proper sign extension for the target. */
517 (bfd_get_sign_extend_vma (exec_bfd) > 0
518 ? (CORE_ADDR) (intptr_t) notify.u.bptaddr
519 : (CORE_ADDR) (uintptr_t) notify.u.bptaddr),
520 &current_target));
521 create_thread_event_breakpoint (target_gdbarch, *bp);
522
523 return TD_OK;
524 }
525
526 static void
527 enable_thread_event_reporting (void)
528 {
529 td_thr_events_t events;
530 td_notify_t notify;
531 td_err_e err;
532 #ifdef HAVE_GNU_LIBC_VERSION_H
533 const char *libc_version;
534 int libc_major, libc_minor;
535 #endif
536 struct thread_db_info *info;
537
538 info = get_thread_db_info (GET_PID (inferior_ptid));
539
540 /* We cannot use the thread event reporting facility if these
541 functions aren't available. */
542 if (info->td_ta_event_addr_p == NULL
543 || info->td_ta_set_event_p == NULL
544 || info->td_ta_event_getmsg_p == NULL
545 || info->td_thr_event_enable_p == NULL)
546 return;
547
548 /* Set the process wide mask saying which events we're interested in. */
549 td_event_emptyset (&events);
550 td_event_addset (&events, TD_CREATE);
551
552 #ifdef HAVE_GNU_LIBC_VERSION_H
553 /* The event reporting facility is broken for TD_DEATH events in
554 glibc 2.1.3, so don't enable it if we have glibc but a lower
555 version. */
556 libc_version = gnu_get_libc_version ();
557 if (sscanf (libc_version, "%d.%d", &libc_major, &libc_minor) == 2
558 && (libc_major > 2 || (libc_major == 2 && libc_minor > 1)))
559 #endif
560 td_event_addset (&events, TD_DEATH);
561
562 err = info->td_ta_set_event_p (info->thread_agent, &events);
563 if (err != TD_OK)
564 {
565 warning (_("Unable to set global thread event mask: %s"),
566 thread_db_err_str (err));
567 return;
568 }
569
570 /* Delete previous thread event breakpoints, if any. */
571 remove_thread_event_breakpoints ();
572 info->td_create_bp_addr = 0;
573 info->td_death_bp_addr = 0;
574
575 /* Set up the thread creation event. */
576 err = enable_thread_event (TD_CREATE, &info->td_create_bp_addr);
577 if (err != TD_OK)
578 {
579 warning (_("Unable to get location for thread creation breakpoint: %s"),
580 thread_db_err_str (err));
581 return;
582 }
583
584 /* Set up the thread death event. */
585 err = enable_thread_event (TD_DEATH, &info->td_death_bp_addr);
586 if (err != TD_OK)
587 {
588 warning (_("Unable to get location for thread death breakpoint: %s"),
589 thread_db_err_str (err));
590 return;
591 }
592 }
593
594 /* Same as thread_db_find_new_threads_1, but silently ignore errors. */
595
596 static void
597 thread_db_find_new_threads_silently (ptid_t ptid)
598 {
599 volatile struct gdb_exception except;
600
601 TRY_CATCH (except, RETURN_MASK_ERROR)
602 {
603 thread_db_find_new_threads_2 (ptid, 1);
604 }
605
606 if (except.reason < 0 && info_verbose)
607 {
608 exception_fprintf (gdb_stderr, except,
609 "Warning: thread_db_find_new_threads_silently: ");
610 }
611 }
612
613 /* Lookup a library in which given symbol resides.
614 Note: this is looking in GDB process, not in the inferior.
615 Returns library name, or NULL. */
616
617 static const char *
618 dladdr_to_soname (const void *addr)
619 {
620 Dl_info info;
621
622 if (dladdr (addr, &info) != 0)
623 return info.dli_fname;
624 return NULL;
625 }
626
627 /* Attempt to initialize dlopen()ed libthread_db, described by HANDLE.
628 Return 1 on success.
629 Failure could happen if libthread_db does not have symbols we expect,
630 or when it refuses to work with the current inferior (e.g. due to
631 version mismatch between libthread_db and libpthread). */
632
633 static int
634 try_thread_db_load_1 (struct thread_db_info *info)
635 {
636 td_err_e err;
637
638 /* Initialize pointers to the dynamic library functions we will use.
639 Essential functions first. */
640
641 info->td_init_p = verbose_dlsym (info->handle, "td_init");
642 if (info->td_init_p == NULL)
643 return 0;
644
645 err = info->td_init_p ();
646 if (err != TD_OK)
647 {
648 warning (_("Cannot initialize libthread_db: %s"), thread_db_err_str (err));
649 return 0;
650 }
651
652 info->td_ta_new_p = verbose_dlsym (info->handle, "td_ta_new");
653 if (info->td_ta_new_p == NULL)
654 return 0;
655
656 /* Initialize the structure that identifies the child process. */
657 info->proc_handle.ptid = inferior_ptid;
658
659 /* Now attempt to open a connection to the thread library. */
660 err = info->td_ta_new_p (&info->proc_handle, &info->thread_agent);
661 if (err != TD_OK)
662 {
663 if (info_verbose)
664 printf_unfiltered (_("td_ta_new failed: %s\n"),
665 thread_db_err_str (err));
666 else
667 switch (err)
668 {
669 case TD_NOLIBTHREAD:
670 #ifdef THREAD_DB_HAS_TD_VERSION
671 case TD_VERSION:
672 #endif
673 /* The errors above are not unexpected and silently ignored:
674 they just mean we haven't found correct version of
675 libthread_db yet. */
676 break;
677 default:
678 warning (_("td_ta_new failed: %s"), thread_db_err_str (err));
679 }
680 return 0;
681 }
682
683 info->td_ta_map_id2thr_p = verbose_dlsym (info->handle, "td_ta_map_id2thr");
684 if (info->td_ta_map_id2thr_p == NULL)
685 return 0;
686
687 info->td_ta_map_lwp2thr_p = verbose_dlsym (info->handle, "td_ta_map_lwp2thr");
688 if (info->td_ta_map_lwp2thr_p == NULL)
689 return 0;
690
691 info->td_ta_thr_iter_p = verbose_dlsym (info->handle, "td_ta_thr_iter");
692 if (info->td_ta_thr_iter_p == NULL)
693 return 0;
694
695 info->td_thr_validate_p = verbose_dlsym (info->handle, "td_thr_validate");
696 if (info->td_thr_validate_p == NULL)
697 return 0;
698
699 info->td_thr_get_info_p = verbose_dlsym (info->handle, "td_thr_get_info");
700 if (info->td_thr_get_info_p == NULL)
701 return 0;
702
703 /* These are not essential. */
704 info->td_ta_event_addr_p = dlsym (info->handle, "td_ta_event_addr");
705 info->td_ta_set_event_p = dlsym (info->handle, "td_ta_set_event");
706 info->td_ta_clear_event_p = dlsym (info->handle, "td_ta_clear_event");
707 info->td_ta_event_getmsg_p = dlsym (info->handle, "td_ta_event_getmsg");
708 info->td_thr_event_enable_p = dlsym (info->handle, "td_thr_event_enable");
709 info->td_thr_tls_get_addr_p = dlsym (info->handle, "td_thr_tls_get_addr");
710
711 printf_unfiltered (_("[Thread debugging using libthread_db enabled]\n"));
712
713 if (info_verbose || *libthread_db_search_path)
714 {
715 const char *library;
716
717 library = dladdr_to_soname (*info->td_ta_new_p);
718 if (library == NULL)
719 library = LIBTHREAD_DB_SO;
720
721 printf_unfiltered (_("Using host libthread_db library \"%s\".\n"),
722 library);
723 }
724
725 /* The thread library was detected. Activate the thread_db target
726 if this is the first process using it. */
727 if (thread_db_list->next == NULL)
728 push_target (&thread_db_ops);
729
730 enable_thread_event_reporting ();
731
732 /* There appears to be a bug in glibc-2.3.6: calls to td_thr_get_info fail
733 with TD_ERR for statically linked executables if td_thr_get_info is
734 called before glibc has initialized itself. Silently ignore such
735 errors, and let gdb enumerate threads again later. */
736 thread_db_find_new_threads_silently (inferior_ptid);
737
738 return 1;
739 }
740
741 /* Attempt to use LIBRARY as libthread_db. LIBRARY could be absolute,
742 relative, or just LIBTHREAD_DB. */
743
744 static int
745 try_thread_db_load (const char *library)
746 {
747 void *handle;
748 struct thread_db_info *info;
749
750 if (info_verbose)
751 printf_unfiltered (_("Trying host libthread_db library: %s.\n"),
752 library);
753 handle = dlopen (library, RTLD_NOW);
754 if (handle == NULL)
755 {
756 if (info_verbose)
757 printf_unfiltered (_("dlopen failed: %s.\n"), dlerror ());
758 return 0;
759 }
760
761 if (info_verbose && strchr (library, '/') == NULL)
762 {
763 void *td_init;
764
765 td_init = dlsym (handle, "td_init");
766 if (td_init != NULL)
767 {
768 const char *const libpath = dladdr_to_soname (td_init);
769
770 if (libpath != NULL)
771 printf_unfiltered (_("Host %s resolved to: %s.\n"),
772 library, libpath);
773 }
774 }
775
776 info = add_thread_db_info (handle);
777
778 if (try_thread_db_load_1 (info))
779 return 1;
780
781 /* This library "refused" to work on current inferior. */
782 delete_thread_db_info (GET_PID (inferior_ptid));
783 return 0;
784 }
785
786
787 /* Search libthread_db_search_path for libthread_db which "agrees"
788 to work on current inferior. */
789
790 static int
791 thread_db_load_search (void)
792 {
793 char path[PATH_MAX];
794 const char *search_path = libthread_db_search_path;
795 int rc = 0;
796
797 while (*search_path)
798 {
799 const char *end = strchr (search_path, ':');
800 if (end)
801 {
802 size_t len = end - search_path;
803 if (len + 1 + strlen (LIBTHREAD_DB_SO) + 1 > sizeof (path))
804 {
805 char *cp = xmalloc (len + 1);
806 memcpy (cp, search_path, len);
807 cp[len] = '\0';
808 warning (_("libthread_db_search_path component too long,"
809 " ignored: %s."), cp);
810 xfree (cp);
811 search_path += len + 1;
812 continue;
813 }
814 memcpy (path, search_path, len);
815 path[len] = '\0';
816 search_path += len + 1;
817 }
818 else
819 {
820 size_t len = strlen (search_path);
821
822 if (len + 1 + strlen (LIBTHREAD_DB_SO) + 1 > sizeof (path))
823 {
824 warning (_("libthread_db_search_path component too long,"
825 " ignored: %s."), search_path);
826 break;
827 }
828 memcpy (path, search_path, len + 1);
829 search_path += len;
830 }
831 strcat (path, "/");
832 strcat (path, LIBTHREAD_DB_SO);
833 if (try_thread_db_load (path))
834 {
835 rc = 1;
836 break;
837 }
838 }
839 if (rc == 0)
840 rc = try_thread_db_load (LIBTHREAD_DB_SO);
841 return rc;
842 }
843
844 /* Attempt to load and initialize libthread_db.
845 Return 1 on success.
846 */
847
848 static int
849 thread_db_load (void)
850 {
851 struct objfile *obj;
852 struct thread_db_info *info;
853
854 info = get_thread_db_info (GET_PID (inferior_ptid));
855
856 if (info != NULL)
857 return 1;
858
859 /* Don't attempt to use thread_db on targets which can not run
860 (executables not running yet, core files) for now. */
861 if (!target_has_execution)
862 return 0;
863
864 /* Don't attempt to use thread_db for remote targets. */
865 if (!target_can_run (&current_target))
866 return 0;
867
868 if (thread_db_load_search ())
869 return 1;
870
871 /* None of the libthread_db's on our search path, not the system default
872 ones worked. If the executable is dynamically linked against
873 libpthread, try loading libthread_db from the same directory. */
874
875 ALL_OBJFILES (obj)
876 if (libpthread_name_p (obj->name))
877 {
878 char path[PATH_MAX], *cp;
879
880 gdb_assert (strlen (obj->name) < sizeof (path));
881 strcpy (path, obj->name);
882 cp = strrchr (path, '/');
883
884 if (cp == NULL)
885 {
886 warning (_("Expected absolute pathname for libpthread in the"
887 " inferior, but got %s."), path);
888 }
889 else if (cp + 1 + strlen (LIBTHREAD_DB_SO) + 1 > path + sizeof (path))
890 {
891 warning (_("Unexpected: path to libpthread in the inferior is"
892 " too long: %s"), path);
893 }
894 else
895 {
896 strcpy (cp + 1, LIBTHREAD_DB_SO);
897 if (try_thread_db_load (path))
898 return 1;
899 }
900 warning (_("Unable to find libthread_db matching inferior's thread"
901 " library, thread debugging will not be available."));
902 return 0;
903 }
904 /* Either this executable isn't using libpthread at all, or it is
905 statically linked. Since we can't easily distinguish these two cases,
906 no warning is issued. */
907 return 0;
908 }
909
910 static void
911 disable_thread_event_reporting (struct thread_db_info *info)
912 {
913 if (info->td_ta_clear_event_p != NULL)
914 {
915 td_thr_events_t events;
916
917 /* Set the process wide mask saying we aren't interested in any
918 events anymore. */
919 td_event_fillset (&events);
920 info->td_ta_clear_event_p (info->thread_agent, &events);
921 }
922
923 info->td_create_bp_addr = 0;
924 info->td_death_bp_addr = 0;
925 }
926
927 static void
928 check_thread_signals (void)
929 {
930 #ifdef GET_THREAD_SIGNALS
931 if (!thread_signals)
932 {
933 sigset_t mask;
934 int i;
935
936 GET_THREAD_SIGNALS (&mask);
937 sigemptyset (&thread_stop_set);
938 sigemptyset (&thread_print_set);
939
940 for (i = 1; i < NSIG; i++)
941 {
942 if (sigismember (&mask, i))
943 {
944 if (signal_stop_update (target_signal_from_host (i), 0))
945 sigaddset (&thread_stop_set, i);
946 if (signal_print_update (target_signal_from_host (i), 0))
947 sigaddset (&thread_print_set, i);
948 thread_signals = 1;
949 }
950 }
951 }
952 #endif
953 }
954
955 /* Check whether thread_db is usable. This function is called when
956 an inferior is created (or otherwise acquired, e.g. attached to)
957 and when new shared libraries are loaded into a running process. */
958
959 void
960 check_for_thread_db (void)
961 {
962 td_err_e err;
963 static void *last_loaded;
964
965 /* Do nothing if we couldn't load libthread_db.so.1. */
966 if (!thread_db_load ())
967 return;
968 }
969
970 static void
971 thread_db_new_objfile (struct objfile *objfile)
972 {
973 /* This observer must always be called with inferior_ptid set
974 correctly. */
975
976 if (objfile != NULL)
977 check_for_thread_db ();
978 }
979
980 /* Attach to a new thread. This function is called when we receive a
981 TD_CREATE event or when we iterate over all threads and find one
982 that wasn't already in our list. Returns true on success. */
983
984 static int
985 attach_thread (ptid_t ptid, const td_thrhandle_t *th_p,
986 const td_thrinfo_t *ti_p)
987 {
988 struct private_thread_info *private;
989 struct thread_info *tp = NULL;
990 td_err_e err;
991 struct thread_db_info *info;
992
993 /* If we're being called after a TD_CREATE event, we may already
994 know about this thread. There are two ways this can happen. We
995 may have iterated over all threads between the thread creation
996 and the TD_CREATE event, for instance when the user has issued
997 the `info threads' command before the SIGTRAP for hitting the
998 thread creation breakpoint was reported. Alternatively, the
999 thread may have exited and a new one been created with the same
1000 thread ID. In the first case we don't need to do anything; in
1001 the second case we should discard information about the dead
1002 thread and attach to the new one. */
1003 if (in_thread_list (ptid))
1004 {
1005 tp = find_thread_ptid (ptid);
1006 gdb_assert (tp != NULL);
1007
1008 /* If tp->private is NULL, then GDB is already attached to this
1009 thread, but we do not know anything about it. We can learn
1010 about it here. This can only happen if we have some other
1011 way besides libthread_db to notice new threads (i.e.
1012 PTRACE_EVENT_CLONE); assume the same mechanism notices thread
1013 exit, so this can not be a stale thread recreated with the
1014 same ID. */
1015 if (tp->private != NULL)
1016 {
1017 if (!tp->private->dying)
1018 return 0;
1019
1020 delete_thread (ptid);
1021 tp = NULL;
1022 }
1023 }
1024
1025 check_thread_signals ();
1026
1027 if (ti_p->ti_state == TD_THR_UNKNOWN || ti_p->ti_state == TD_THR_ZOMBIE)
1028 return 0; /* A zombie thread -- do not attach. */
1029
1030 /* Under GNU/Linux, we have to attach to each and every thread. */
1031 if (tp == NULL
1032 && lin_lwp_attach_lwp (BUILD_LWP (ti_p->ti_lid, GET_PID (ptid))) < 0)
1033 return 0;
1034
1035 /* Construct the thread's private data. */
1036 private = xmalloc (sizeof (struct private_thread_info));
1037 memset (private, 0, sizeof (struct private_thread_info));
1038
1039 /* A thread ID of zero may mean the thread library has not initialized
1040 yet. But we shouldn't even get here if that's the case. FIXME:
1041 if we change GDB to always have at least one thread in the thread
1042 list this will have to go somewhere else; maybe private == NULL
1043 until the thread_db target claims it. */
1044 gdb_assert (ti_p->ti_tid != 0);
1045 private->th = *th_p;
1046 private->tid = ti_p->ti_tid;
1047
1048 /* Add the thread to GDB's thread list. */
1049 if (tp == NULL)
1050 tp = add_thread_with_info (ptid, private);
1051 else
1052 tp->private = private;
1053
1054 info = get_thread_db_info (GET_PID (ptid));
1055
1056 /* Enable thread event reporting for this thread. */
1057 err = info->td_thr_event_enable_p (th_p, 1);
1058 if (err != TD_OK)
1059 error (_("Cannot enable thread event reporting for %s: %s"),
1060 target_pid_to_str (ptid), thread_db_err_str (err));
1061 return 1;
1062 }
1063
1064 static void
1065 detach_thread (ptid_t ptid)
1066 {
1067 struct thread_info *thread_info;
1068
1069 /* Don't delete the thread now, because it still reports as active
1070 until it has executed a few instructions after the event
1071 breakpoint - if we deleted it now, "info threads" would cause us
1072 to re-attach to it. Just mark it as having had a TD_DEATH
1073 event. This means that we won't delete it from our thread list
1074 until we notice that it's dead (via prune_threads), or until
1075 something re-uses its thread ID. We'll report the thread exit
1076 when the underlying LWP dies. */
1077 thread_info = find_thread_ptid (ptid);
1078 gdb_assert (thread_info != NULL && thread_info->private != NULL);
1079 thread_info->private->dying = 1;
1080 }
1081
1082 static void
1083 thread_db_detach (struct target_ops *ops, char *args, int from_tty)
1084 {
1085 struct target_ops *target_beneath = find_target_beneath (ops);
1086 struct thread_db_info *info;
1087
1088 info = get_thread_db_info (GET_PID (inferior_ptid));
1089
1090 if (info)
1091 {
1092 disable_thread_event_reporting (info);
1093
1094 /* Delete the old thread event breakpoints. Note that unlike
1095 when mourning, we can remove them here because there's still
1096 a live inferior to poke at. In any case, GDB will not try to
1097 insert anything in the inferior when removing a
1098 breakpoint. */
1099 remove_thread_event_breakpoints ();
1100
1101 delete_thread_db_info (GET_PID (inferior_ptid));
1102 }
1103
1104 target_beneath->to_detach (target_beneath, args, from_tty);
1105
1106 /* NOTE: From this point on, inferior_ptid is null_ptid. */
1107
1108 /* If there are no more processes using libpthread, detach the
1109 thread_db target ops. */
1110 if (!thread_db_list)
1111 unpush_target (&thread_db_ops);
1112 }
1113
1114 /* Check if PID is currently stopped at the location of a thread event
1115 breakpoint location. If it is, read the event message and act upon
1116 the event. */
1117
1118 static void
1119 check_event (ptid_t ptid)
1120 {
1121 struct regcache *regcache = get_thread_regcache (ptid);
1122 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1123 td_event_msg_t msg;
1124 td_thrinfo_t ti;
1125 td_err_e err;
1126 CORE_ADDR stop_pc;
1127 int loop = 0;
1128 struct thread_db_info *info;
1129
1130 info = get_thread_db_info (GET_PID (ptid));
1131
1132 /* Bail out early if we're not at a thread event breakpoint. */
1133 stop_pc = regcache_read_pc (regcache)
1134 - gdbarch_decr_pc_after_break (gdbarch);
1135 if (stop_pc != info->td_create_bp_addr
1136 && stop_pc != info->td_death_bp_addr)
1137 return;
1138
1139 /* Access an lwp we know is stopped. */
1140 info->proc_handle.ptid = ptid;
1141
1142 /* If we have only looked at the first thread before libpthread was
1143 initialized, we may not know its thread ID yet. Make sure we do
1144 before we add another thread to the list. */
1145 if (!have_threads (ptid))
1146 thread_db_find_new_threads_1 (ptid);
1147
1148 /* If we are at a create breakpoint, we do not know what new lwp
1149 was created and cannot specifically locate the event message for it.
1150 We have to call td_ta_event_getmsg() to get
1151 the latest message. Since we have no way of correlating whether
1152 the event message we get back corresponds to our breakpoint, we must
1153 loop and read all event messages, processing them appropriately.
1154 This guarantees we will process the correct message before continuing
1155 from the breakpoint.
1156
1157 Currently, death events are not enabled. If they are enabled,
1158 the death event can use the td_thr_event_getmsg() interface to
1159 get the message specifically for that lwp and avoid looping
1160 below. */
1161
1162 loop = 1;
1163
1164 do
1165 {
1166 err = info->td_ta_event_getmsg_p (info->thread_agent, &msg);
1167 if (err != TD_OK)
1168 {
1169 if (err == TD_NOMSG)
1170 return;
1171
1172 error (_("Cannot get thread event message: %s"),
1173 thread_db_err_str (err));
1174 }
1175
1176 err = info->td_thr_get_info_p (msg.th_p, &ti);
1177 if (err != TD_OK)
1178 error (_("Cannot get thread info: %s"), thread_db_err_str (err));
1179
1180 ptid = ptid_build (GET_PID (ptid), ti.ti_lid, 0);
1181
1182 switch (msg.event)
1183 {
1184 case TD_CREATE:
1185 /* Call attach_thread whether or not we already know about a
1186 thread with this thread ID. */
1187 attach_thread (ptid, msg.th_p, &ti);
1188
1189 break;
1190
1191 case TD_DEATH:
1192
1193 if (!in_thread_list (ptid))
1194 error (_("Spurious thread death event."));
1195
1196 detach_thread (ptid);
1197
1198 break;
1199
1200 default:
1201 error (_("Spurious thread event."));
1202 }
1203 }
1204 while (loop);
1205 }
1206
1207 static ptid_t
1208 thread_db_wait (struct target_ops *ops,
1209 ptid_t ptid, struct target_waitstatus *ourstatus,
1210 int options)
1211 {
1212 struct thread_db_info *info;
1213 struct target_ops *beneath = find_target_beneath (ops);
1214
1215 ptid = beneath->to_wait (beneath, ptid, ourstatus, options);
1216
1217 if (ourstatus->kind == TARGET_WAITKIND_IGNORE)
1218 return ptid;
1219
1220 if (ourstatus->kind == TARGET_WAITKIND_EXITED
1221 || ourstatus->kind == TARGET_WAITKIND_SIGNALLED)
1222 return ptid;
1223
1224 info = get_thread_db_info (GET_PID (ptid));
1225
1226 /* If this process isn't using thread_db, we're done. */
1227 if (info == NULL)
1228 return ptid;
1229
1230 if (ourstatus->kind == TARGET_WAITKIND_EXECD)
1231 {
1232 /* New image, it may or may not end up using thread_db. Assume
1233 not unless we find otherwise. */
1234 delete_thread_db_info (GET_PID (ptid));
1235 if (!thread_db_list)
1236 unpush_target (&thread_db_ops);
1237
1238 /* Thread event breakpoints are deleted by
1239 update_breakpoints_after_exec. */
1240
1241 return ptid;
1242 }
1243
1244 /* If we do not know about the main thread yet, this would be a good time to
1245 find it. */
1246 if (ourstatus->kind == TARGET_WAITKIND_STOPPED && !have_threads (ptid))
1247 thread_db_find_new_threads_1 (ptid);
1248
1249 if (ourstatus->kind == TARGET_WAITKIND_STOPPED
1250 && ourstatus->value.sig == TARGET_SIGNAL_TRAP)
1251 /* Check for a thread event. */
1252 check_event (ptid);
1253
1254 if (have_threads (ptid))
1255 {
1256 /* Change ptids back into the higher level PID + TID format. If
1257 the thread is dead and no longer on the thread list, we will
1258 get back a dead ptid. This can occur if the thread death
1259 event gets postponed by other simultaneous events. In such a
1260 case, we want to just ignore the event and continue on. */
1261
1262 ptid = thread_from_lwp (ptid);
1263 if (GET_PID (ptid) == -1)
1264 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
1265 }
1266
1267 return ptid;
1268 }
1269
1270 static void
1271 thread_db_mourn_inferior (struct target_ops *ops)
1272 {
1273 struct target_ops *target_beneath = find_target_beneath (ops);
1274
1275 delete_thread_db_info (GET_PID (inferior_ptid));
1276
1277 target_beneath->to_mourn_inferior (target_beneath);
1278
1279 /* Delete the old thread event breakpoints. Do this after mourning
1280 the inferior, so that we don't try to uninsert them. */
1281 remove_thread_event_breakpoints ();
1282
1283 /* Detach thread_db target ops. */
1284 if (!thread_db_list)
1285 unpush_target (ops);
1286 }
1287
1288 struct callback_data
1289 {
1290 struct thread_db_info *info;
1291 int new_threads;
1292 };
1293
1294 static int
1295 find_new_threads_callback (const td_thrhandle_t *th_p, void *data)
1296 {
1297 td_thrinfo_t ti;
1298 td_err_e err;
1299 ptid_t ptid;
1300 struct thread_info *tp;
1301 struct callback_data *cb_data = data;
1302 struct thread_db_info *info = cb_data->info;
1303
1304 err = info->td_thr_get_info_p (th_p, &ti);
1305 if (err != TD_OK)
1306 error (_("find_new_threads_callback: cannot get thread info: %s"),
1307 thread_db_err_str (err));
1308
1309 if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
1310 return 0; /* A zombie -- ignore. */
1311
1312 if (ti.ti_tid == 0)
1313 {
1314 /* A thread ID of zero means that this is the main thread, but
1315 glibc has not yet initialized thread-local storage and the
1316 pthread library. We do not know what the thread's TID will
1317 be yet. Just enable event reporting and otherwise ignore
1318 it. */
1319
1320 /* In that case, we're not stopped in a fork syscall and don't
1321 need this glibc bug workaround. */
1322 info->need_stale_parent_threads_check = 0;
1323
1324 err = info->td_thr_event_enable_p (th_p, 1);
1325 if (err != TD_OK)
1326 error (_("Cannot enable thread event reporting for LWP %d: %s"),
1327 (int) ti.ti_lid, thread_db_err_str (err));
1328
1329 return 0;
1330 }
1331
1332 /* Ignore stale parent threads, caused by glibc/BZ5983. This is a
1333 bit expensive, as it needs to open /proc/pid/status, so try to
1334 avoid doing the work if we know we don't have to. */
1335 if (info->need_stale_parent_threads_check)
1336 {
1337 int tgid = linux_proc_get_tgid (ti.ti_lid);
1338 if (tgid != -1 && tgid != info->pid)
1339 return 0;
1340 }
1341
1342 ptid = ptid_build (info->pid, ti.ti_lid, 0);
1343 tp = find_thread_ptid (ptid);
1344 if (tp == NULL || tp->private == NULL)
1345 {
1346 if (attach_thread (ptid, th_p, &ti))
1347 cb_data->new_threads += 1;
1348 else
1349 /* Problem attaching this thread; perhaps it exited before we
1350 could attach it?
1351 This could mean that the thread list inside glibc itself is in
1352 inconsistent state, and libthread_db could go on looping forever
1353 (observed with glibc-2.3.6). To prevent that, terminate
1354 iteration: thread_db_find_new_threads_2 will retry. */
1355 return 1;
1356 }
1357
1358 return 0;
1359 }
1360
1361 /* Helper for thread_db_find_new_threads_2.
1362 Returns number of new threads found. */
1363
1364 static int
1365 find_new_threads_once (struct thread_db_info *info, int iteration,
1366 td_err_e *errp)
1367 {
1368 volatile struct gdb_exception except;
1369 struct callback_data data;
1370 td_err_e err = TD_ERR;
1371
1372 data.info = info;
1373 data.new_threads = 0;
1374
1375 TRY_CATCH (except, RETURN_MASK_ERROR)
1376 {
1377 /* Iterate over all user-space threads to discover new threads. */
1378 err = info->td_ta_thr_iter_p (info->thread_agent,
1379 find_new_threads_callback,
1380 &data,
1381 TD_THR_ANY_STATE,
1382 TD_THR_LOWEST_PRIORITY,
1383 TD_SIGNO_MASK,
1384 TD_THR_ANY_USER_FLAGS);
1385 }
1386
1387 if (info_verbose)
1388 {
1389 if (except.reason < 0)
1390 exception_fprintf (gdb_stderr, except,
1391 "Warning: find_new_threads_once: ");
1392
1393 printf_filtered (_("Found %d new threads in iteration %d.\n"),
1394 data.new_threads, iteration);
1395 }
1396
1397 if (errp != NULL)
1398 *errp = err;
1399
1400 return data.new_threads;
1401 }
1402
1403 /* Search for new threads, accessing memory through stopped thread
1404 PTID. If UNTIL_NO_NEW is true, repeat searching until several
1405 searches in a row do not discover any new threads. */
1406
1407 static void
1408 thread_db_find_new_threads_2 (ptid_t ptid, int until_no_new)
1409 {
1410 td_err_e err;
1411 struct lwp_info *lp;
1412 struct thread_db_info *info;
1413 int pid = ptid_get_pid (ptid);
1414 int i, loop;
1415
1416 /* In linux, we can only read memory through a stopped lwp. */
1417 ALL_LWPS (lp, ptid)
1418 if (lp->stopped && ptid_get_pid (lp->ptid) == pid)
1419 break;
1420
1421 if (!lp)
1422 /* There is no stopped thread. Bail out. */
1423 return;
1424
1425 info = get_thread_db_info (GET_PID (ptid));
1426
1427 /* Access an lwp we know is stopped. */
1428 info->proc_handle.ptid = ptid;
1429
1430 if (until_no_new)
1431 {
1432 /* Require 4 successive iterations which do not find any new threads.
1433 The 4 is a heuristic: there is an inherent race here, and I have
1434 seen that 2 iterations in a row are not always sufficient to
1435 "capture" all threads. */
1436 for (i = 0, loop = 0; loop < 4; ++i, ++loop)
1437 if (find_new_threads_once (info, i, NULL) != 0)
1438 /* Found some new threads. Restart the loop from beginning. */
1439 loop = -1;
1440 }
1441 else
1442 {
1443 td_err_e err;
1444
1445 find_new_threads_once (info, 0, &err);
1446 if (err != TD_OK)
1447 error (_("Cannot find new threads: %s"), thread_db_err_str (err));
1448 }
1449 }
1450
1451 static void
1452 thread_db_find_new_threads_1 (ptid_t ptid)
1453 {
1454 thread_db_find_new_threads_2 (ptid, 0);
1455 }
1456
1457
1458 static void
1459 thread_db_find_new_threads (struct target_ops *ops)
1460 {
1461 struct thread_db_info *info;
1462
1463 info = get_thread_db_info (GET_PID (inferior_ptid));
1464
1465 if (info == NULL)
1466 return;
1467
1468 thread_db_find_new_threads_1 (inferior_ptid);
1469 }
1470
1471 static char *
1472 thread_db_pid_to_str (struct target_ops *ops, ptid_t ptid)
1473 {
1474 struct thread_info *thread_info = find_thread_ptid (ptid);
1475 struct target_ops *beneath;
1476
1477 if (thread_info != NULL && thread_info->private != NULL)
1478 {
1479 static char buf[64];
1480 thread_t tid;
1481
1482 tid = thread_info->private->tid;
1483 snprintf (buf, sizeof (buf), "Thread 0x%lx (LWP %ld)",
1484 tid, GET_LWP (ptid));
1485
1486 return buf;
1487 }
1488
1489 beneath = find_target_beneath (ops);
1490 if (beneath->to_pid_to_str (beneath, ptid))
1491 return beneath->to_pid_to_str (beneath, ptid);
1492
1493 return normal_pid_to_str (ptid);
1494 }
1495
1496 /* Return a string describing the state of the thread specified by
1497 INFO. */
1498
1499 static char *
1500 thread_db_extra_thread_info (struct thread_info *info)
1501 {
1502 if (info->private == NULL)
1503 return NULL;
1504
1505 if (info->private->dying)
1506 return "Exiting";
1507
1508 return NULL;
1509 }
1510
1511 /* Get the address of the thread local variable in load module LM which
1512 is stored at OFFSET within the thread local storage for thread PTID. */
1513
1514 static CORE_ADDR
1515 thread_db_get_thread_local_address (struct target_ops *ops,
1516 ptid_t ptid,
1517 CORE_ADDR lm,
1518 CORE_ADDR offset)
1519 {
1520 struct thread_info *thread_info;
1521 struct target_ops *beneath;
1522
1523 /* If we have not discovered any threads yet, check now. */
1524 if (!have_threads (ptid))
1525 thread_db_find_new_threads_1 (ptid);
1526
1527 /* Find the matching thread. */
1528 thread_info = find_thread_ptid (ptid);
1529
1530 if (thread_info != NULL && thread_info->private != NULL)
1531 {
1532 td_err_e err;
1533 psaddr_t address;
1534 struct thread_db_info *info;
1535
1536 info = get_thread_db_info (GET_PID (ptid));
1537
1538 /* glibc doesn't provide the needed interface. */
1539 if (!info->td_thr_tls_get_addr_p)
1540 throw_error (TLS_NO_LIBRARY_SUPPORT_ERROR,
1541 _("No TLS library support"));
1542
1543 /* Caller should have verified that lm != 0. */
1544 gdb_assert (lm != 0);
1545
1546 /* Finally, get the address of the variable. */
1547 /* Note the cast through uintptr_t: this interface only works if
1548 a target address fits in a psaddr_t, which is a host pointer.
1549 So a 32-bit debugger can not access 64-bit TLS through this. */
1550 err = info->td_thr_tls_get_addr_p (&thread_info->private->th,
1551 (psaddr_t)(uintptr_t) lm,
1552 offset, &address);
1553
1554 #ifdef THREAD_DB_HAS_TD_NOTALLOC
1555 /* The memory hasn't been allocated, yet. */
1556 if (err == TD_NOTALLOC)
1557 /* Now, if libthread_db provided the initialization image's
1558 address, we *could* try to build a non-lvalue value from
1559 the initialization image. */
1560 throw_error (TLS_NOT_ALLOCATED_YET_ERROR,
1561 _("TLS not allocated yet"));
1562 #endif
1563
1564 /* Something else went wrong. */
1565 if (err != TD_OK)
1566 throw_error (TLS_GENERIC_ERROR,
1567 (("%s")), thread_db_err_str (err));
1568
1569 /* Cast assuming host == target. Joy. */
1570 /* Do proper sign extension for the target. */
1571 gdb_assert (exec_bfd);
1572 return (bfd_get_sign_extend_vma (exec_bfd) > 0
1573 ? (CORE_ADDR) (intptr_t) address
1574 : (CORE_ADDR) (uintptr_t) address);
1575 }
1576
1577 beneath = find_target_beneath (ops);
1578 if (beneath->to_get_thread_local_address)
1579 return beneath->to_get_thread_local_address (beneath, ptid, lm, offset);
1580 else
1581 throw_error (TLS_GENERIC_ERROR,
1582 _("TLS not supported on this target"));
1583 }
1584
1585 /* Callback routine used to find a thread based on the TID part of
1586 its PTID. */
1587
1588 static int
1589 thread_db_find_thread_from_tid (struct thread_info *thread, void *data)
1590 {
1591 long *tid = (long *) data;
1592
1593 if (thread->private->tid == *tid)
1594 return 1;
1595
1596 return 0;
1597 }
1598
1599 /* Implement the to_get_ada_task_ptid target method for this target. */
1600
1601 static ptid_t
1602 thread_db_get_ada_task_ptid (long lwp, long thread)
1603 {
1604 struct thread_info *thread_info;
1605
1606 thread_db_find_new_threads_1 (inferior_ptid);
1607 thread_info = iterate_over_threads (thread_db_find_thread_from_tid, &thread);
1608
1609 gdb_assert (thread_info != NULL);
1610
1611 return (thread_info->ptid);
1612 }
1613
1614 static void
1615 thread_db_resume (struct target_ops *ops,
1616 ptid_t ptid, int step, enum target_signal signo)
1617 {
1618 struct target_ops *beneath = find_target_beneath (ops);
1619 struct thread_db_info *info;
1620
1621 if (ptid_equal (ptid, minus_one_ptid))
1622 info = get_thread_db_info (GET_PID (inferior_ptid));
1623 else
1624 info = get_thread_db_info (GET_PID (ptid));
1625
1626 /* This workaround is only needed for child fork lwps stopped in a
1627 PTRACE_O_TRACEFORK event. When the inferior is resumed, the
1628 workaround can be disabled. */
1629 if (info)
1630 info->need_stale_parent_threads_check = 0;
1631
1632 beneath->to_resume (beneath, ptid, step, signo);
1633 }
1634
1635 static void
1636 init_thread_db_ops (void)
1637 {
1638 thread_db_ops.to_shortname = "multi-thread";
1639 thread_db_ops.to_longname = "multi-threaded child process.";
1640 thread_db_ops.to_doc = "Threads and pthreads support.";
1641 thread_db_ops.to_detach = thread_db_detach;
1642 thread_db_ops.to_wait = thread_db_wait;
1643 thread_db_ops.to_resume = thread_db_resume;
1644 thread_db_ops.to_mourn_inferior = thread_db_mourn_inferior;
1645 thread_db_ops.to_find_new_threads = thread_db_find_new_threads;
1646 thread_db_ops.to_pid_to_str = thread_db_pid_to_str;
1647 thread_db_ops.to_stratum = thread_stratum;
1648 thread_db_ops.to_has_thread_control = tc_schedlock;
1649 thread_db_ops.to_get_thread_local_address
1650 = thread_db_get_thread_local_address;
1651 thread_db_ops.to_extra_thread_info = thread_db_extra_thread_info;
1652 thread_db_ops.to_get_ada_task_ptid = thread_db_get_ada_task_ptid;
1653 thread_db_ops.to_magic = OPS_MAGIC;
1654 }
1655
1656 /* Provide a prototype to silence -Wmissing-prototypes. */
1657 extern initialize_file_ftype _initialize_thread_db;
1658
1659 void
1660 _initialize_thread_db (void)
1661 {
1662 init_thread_db_ops ();
1663 add_target (&thread_db_ops);
1664
1665 /* Defer loading of libthread_db.so until inferior is running.
1666 This allows gdb to load correct libthread_db for a given
1667 executable -- there could be mutiple versions of glibc,
1668 compiled with LinuxThreads or NPTL, and until there is
1669 a running inferior, we can't tell which libthread_db is
1670 the correct one to load. */
1671
1672 libthread_db_search_path = xstrdup (LIBTHREAD_DB_SEARCH_PATH);
1673
1674 add_setshow_optional_filename_cmd ("libthread-db-search-path",
1675 class_support,
1676 &libthread_db_search_path, _("\
1677 Set search path for libthread_db."), _("\
1678 Show the current search path or libthread_db."), _("\
1679 This path is used to search for libthread_db to be loaded into \
1680 gdb itself."),
1681 NULL,
1682 NULL,
1683 &setlist, &showlist);
1684 /* Add ourselves to objfile event chain. */
1685 observer_attach_new_objfile (thread_db_new_objfile);
1686 }