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