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