]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/linux-thread-db.c
PR18006: internal error if threaded program calls clone(CLONE_VM)
[thirdparty/binutils-gdb.git] / gdb / linux-thread-db.c
1 /* libthread_db assisted debugging support, generic parts.
2
3 Copyright (C) 1999-2015 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include <dlfcn.h>
22 #include "gdb_proc_service.h"
23 #include "nat/gdb_thread_db.h"
24 #include "gdb_vecs.h"
25 #include "bfd.h"
26 #include "command.h"
27 #include "gdbcmd.h"
28 #include "gdbthread.h"
29 #include "inferior.h"
30 #include "infrun.h"
31 #include "symfile.h"
32 #include "objfiles.h"
33 #include "target.h"
34 #include "regcache.h"
35 #include "solib.h"
36 #include "solib-svr4.h"
37 #include "gdbcore.h"
38 #include "observer.h"
39 #include "linux-nat.h"
40 #include "nat/linux-procfs.h"
41 #include "nat/linux-ptrace.h"
42 #include "nat/linux-osdata.h"
43 #include "auto-load.h"
44 #include "cli/cli-utils.h"
45
46 #include <signal.h>
47 #include <ctype.h>
48
49 /* GNU/Linux libthread_db support.
50
51 libthread_db is a library, provided along with libpthread.so, which
52 exposes the internals of the thread library to a debugger. It
53 allows GDB to find existing threads, new threads as they are
54 created, thread IDs (usually, the result of pthread_self), and
55 thread-local variables.
56
57 The libthread_db interface originates on Solaris, where it is
58 both more powerful and more complicated. This implementation
59 only works for LinuxThreads and NPTL, the two glibc threading
60 libraries. It assumes that each thread is permanently assigned
61 to a single light-weight process (LWP).
62
63 libthread_db-specific information is stored in the "private" field
64 of struct thread_info. When the field is NULL we do not yet have
65 information about the new thread; this could be temporary (created,
66 but the thread library's data structures do not reflect it yet)
67 or permanent (created using clone instead of pthread_create).
68
69 Process IDs managed by linux-thread-db.c match those used by
70 linux-nat.c: a common PID for all processes, an LWP ID for each
71 thread, and no TID. We save the TID in private. Keeping it out
72 of the ptid_t prevents thread IDs changing when libpthread is
73 loaded or unloaded. */
74
75 static char *libthread_db_search_path;
76
77 /* Set to non-zero if thread_db auto-loading is enabled
78 by the "set auto-load libthread-db" command. */
79 static int auto_load_thread_db = 1;
80
81 /* Returns true if we need to use thread_db thread create/death event
82 breakpoints to learn about threads. */
83
84 static int
85 thread_db_use_events (void)
86 {
87 /* Not necessary if the kernel supports clone events. */
88 return !linux_supports_traceclone ();
89 }
90
91 /* "show" command for the auto_load_thread_db configuration variable. */
92
93 static void
94 show_auto_load_thread_db (struct ui_file *file, int from_tty,
95 struct cmd_list_element *c, const char *value)
96 {
97 fprintf_filtered (file, _("Auto-loading of inferior specific libthread_db "
98 "is %s.\n"),
99 value);
100 }
101
102 static void
103 set_libthread_db_search_path (char *ignored, int from_tty,
104 struct cmd_list_element *c)
105 {
106 if (*libthread_db_search_path == '\0')
107 {
108 xfree (libthread_db_search_path);
109 libthread_db_search_path = xstrdup (LIBTHREAD_DB_SEARCH_PATH);
110 }
111 }
112
113 /* If non-zero, print details of libthread_db processing. */
114
115 static unsigned int libthread_db_debug;
116
117 static void
118 show_libthread_db_debug (struct ui_file *file, int from_tty,
119 struct cmd_list_element *c, const char *value)
120 {
121 fprintf_filtered (file, _("libthread-db debugging is %s.\n"), value);
122 }
123
124 /* If we're running on GNU/Linux, we must explicitly attach to any new
125 threads. */
126
127 /* This module's target vector. */
128 static struct target_ops thread_db_ops;
129
130 /* Non-zero if we have determined the signals used by the threads
131 library. */
132 static int thread_signals;
133 static sigset_t thread_stop_set;
134 static sigset_t thread_print_set;
135
136 struct thread_db_info
137 {
138 struct thread_db_info *next;
139
140 /* Process id this object refers to. */
141 int pid;
142
143 /* Handle from dlopen for libthread_db.so. */
144 void *handle;
145
146 /* Absolute pathname from gdb_realpath to disk file used for dlopen-ing
147 HANDLE. It may be NULL for system library. */
148 char *filename;
149
150 /* Structure that identifies the child process for the
151 <proc_service.h> interface. */
152 struct ps_prochandle proc_handle;
153
154 /* Connection to the libthread_db library. */
155 td_thragent_t *thread_agent;
156
157 /* True if we need to apply the workaround for glibc/BZ5983. When
158 we catch a PTRACE_O_TRACEFORK, and go query the child's thread
159 list, nptl_db returns the parent's threads in addition to the new
160 (single) child thread. If this flag is set, we do extra work to
161 be able to ignore such stale entries. */
162 int need_stale_parent_threads_check;
163
164 /* Location of the thread creation event breakpoint. The code at
165 this location in the child process will be called by the pthread
166 library whenever a new thread is created. By setting a special
167 breakpoint at this location, GDB can detect when a new thread is
168 created. We obtain this location via the td_ta_event_addr
169 call. */
170 CORE_ADDR td_create_bp_addr;
171
172 /* Location of the thread death event breakpoint. */
173 CORE_ADDR td_death_bp_addr;
174
175 /* Pointers to the libthread_db functions. */
176
177 td_err_e (*td_init_p) (void);
178
179 td_err_e (*td_ta_new_p) (struct ps_prochandle * ps,
180 td_thragent_t **ta);
181 td_err_e (*td_ta_map_id2thr_p) (const td_thragent_t *ta, thread_t pt,
182 td_thrhandle_t *__th);
183 td_err_e (*td_ta_map_lwp2thr_p) (const td_thragent_t *ta,
184 lwpid_t lwpid, td_thrhandle_t *th);
185 td_err_e (*td_ta_thr_iter_p) (const td_thragent_t *ta,
186 td_thr_iter_f *callback, void *cbdata_p,
187 td_thr_state_e state, int ti_pri,
188 sigset_t *ti_sigmask_p,
189 unsigned int ti_user_flags);
190 td_err_e (*td_ta_event_addr_p) (const td_thragent_t *ta,
191 td_event_e event, td_notify_t *ptr);
192 td_err_e (*td_ta_set_event_p) (const td_thragent_t *ta,
193 td_thr_events_t *event);
194 td_err_e (*td_ta_clear_event_p) (const td_thragent_t *ta,
195 td_thr_events_t *event);
196 td_err_e (*td_ta_event_getmsg_p) (const td_thragent_t *ta,
197 td_event_msg_t *msg);
198
199 td_err_e (*td_thr_validate_p) (const td_thrhandle_t *th);
200 td_err_e (*td_thr_get_info_p) (const td_thrhandle_t *th,
201 td_thrinfo_t *infop);
202 td_err_e (*td_thr_event_enable_p) (const td_thrhandle_t *th,
203 int event);
204
205 td_err_e (*td_thr_tls_get_addr_p) (const td_thrhandle_t *th,
206 psaddr_t map_address,
207 size_t offset, psaddr_t *address);
208 td_err_e (*td_thr_tlsbase_p) (const td_thrhandle_t *th,
209 unsigned long int modid,
210 psaddr_t *base);
211 };
212
213 /* List of known processes using thread_db, and the required
214 bookkeeping. */
215 struct thread_db_info *thread_db_list;
216
217 static void thread_db_find_new_threads_1 (ptid_t ptid);
218 static void thread_db_find_new_threads_2 (ptid_t ptid, int until_no_new);
219
220 /* Add the current inferior to the list of processes using libpthread.
221 Return a pointer to the newly allocated object that was added to
222 THREAD_DB_LIST. HANDLE is the handle returned by dlopen'ing
223 LIBTHREAD_DB_SO. */
224
225 static struct thread_db_info *
226 add_thread_db_info (void *handle)
227 {
228 struct thread_db_info *info;
229
230 info = xcalloc (1, sizeof (*info));
231 info->pid = ptid_get_pid (inferior_ptid);
232 info->handle = handle;
233
234 /* The workaround works by reading from /proc/pid/status, so it is
235 disabled for core files. */
236 if (target_has_execution)
237 info->need_stale_parent_threads_check = 1;
238
239 info->next = thread_db_list;
240 thread_db_list = info;
241
242 return info;
243 }
244
245 /* Return the thread_db_info object representing the bookkeeping
246 related to process PID, if any; NULL otherwise. */
247
248 static struct thread_db_info *
249 get_thread_db_info (int pid)
250 {
251 struct thread_db_info *info;
252
253 for (info = thread_db_list; info; info = info->next)
254 if (pid == info->pid)
255 return info;
256
257 return NULL;
258 }
259
260 /* When PID has exited or has been detached, we no longer want to keep
261 track of it as using libpthread. Call this function to discard
262 thread_db related info related to PID. Note that this closes
263 LIBTHREAD_DB_SO's dlopen'ed handle. */
264
265 static void
266 delete_thread_db_info (int pid)
267 {
268 struct thread_db_info *info, *info_prev;
269
270 info_prev = NULL;
271
272 for (info = thread_db_list; info; info_prev = info, info = info->next)
273 if (pid == info->pid)
274 break;
275
276 if (info == NULL)
277 return;
278
279 if (info->handle != NULL)
280 dlclose (info->handle);
281
282 xfree (info->filename);
283
284 if (info_prev)
285 info_prev->next = info->next;
286 else
287 thread_db_list = info->next;
288
289 xfree (info);
290 }
291
292 /* Prototypes for local functions. */
293 static int attach_thread (ptid_t ptid, const td_thrhandle_t *th_p,
294 const td_thrinfo_t *ti_p);
295 static void detach_thread (ptid_t ptid);
296 \f
297
298 /* Use "struct private_thread_info" to cache thread state. This is
299 a substantial optimization. */
300
301 struct private_thread_info
302 {
303 /* Flag set when we see a TD_DEATH event for this thread. */
304 unsigned int dying:1;
305
306 /* Cached thread state. */
307 td_thrhandle_t th;
308 thread_t tid;
309 };
310 \f
311
312 static char *
313 thread_db_err_str (td_err_e err)
314 {
315 static char buf[64];
316
317 switch (err)
318 {
319 case TD_OK:
320 return "generic 'call succeeded'";
321 case TD_ERR:
322 return "generic error";
323 case TD_NOTHR:
324 return "no thread to satisfy query";
325 case TD_NOSV:
326 return "no sync handle to satisfy query";
327 case TD_NOLWP:
328 return "no LWP to satisfy query";
329 case TD_BADPH:
330 return "invalid process handle";
331 case TD_BADTH:
332 return "invalid thread handle";
333 case TD_BADSH:
334 return "invalid synchronization handle";
335 case TD_BADTA:
336 return "invalid thread agent";
337 case TD_BADKEY:
338 return "invalid key";
339 case TD_NOMSG:
340 return "no event message for getmsg";
341 case TD_NOFPREGS:
342 return "FPU register set not available";
343 case TD_NOLIBTHREAD:
344 return "application not linked with libthread";
345 case TD_NOEVENT:
346 return "requested event is not supported";
347 case TD_NOCAPAB:
348 return "capability not available";
349 case TD_DBERR:
350 return "debugger service failed";
351 case TD_NOAPLIC:
352 return "operation not applicable to";
353 case TD_NOTSD:
354 return "no thread-specific data for this thread";
355 case TD_MALLOC:
356 return "malloc failed";
357 case TD_PARTIALREG:
358 return "only part of register set was written/read";
359 case TD_NOXREGS:
360 return "X register set not available for this thread";
361 #ifdef THREAD_DB_HAS_TD_NOTALLOC
362 case TD_NOTALLOC:
363 return "thread has not yet allocated TLS for given module";
364 #endif
365 #ifdef THREAD_DB_HAS_TD_VERSION
366 case TD_VERSION:
367 return "versions of libpthread and libthread_db do not match";
368 #endif
369 #ifdef THREAD_DB_HAS_TD_NOTLS
370 case TD_NOTLS:
371 return "there is no TLS segment in the given module";
372 #endif
373 default:
374 snprintf (buf, sizeof (buf), "unknown thread_db error '%d'", err);
375 return buf;
376 }
377 }
378 \f
379 /* Return 1 if any threads have been registered. There may be none if
380 the threading library is not fully initialized yet. */
381
382 static int
383 have_threads_callback (struct thread_info *thread, void *args)
384 {
385 int pid = * (int *) args;
386
387 if (ptid_get_pid (thread->ptid) != pid)
388 return 0;
389
390 return thread->private != NULL;
391 }
392
393 static int
394 have_threads (ptid_t ptid)
395 {
396 int pid = ptid_get_pid (ptid);
397
398 return iterate_over_threads (have_threads_callback, &pid) != NULL;
399 }
400
401 struct thread_get_info_inout
402 {
403 struct thread_info *thread_info;
404 struct thread_db_info *thread_db_info;
405 };
406
407 /* A callback function for td_ta_thr_iter, which we use to map all
408 threads to LWPs.
409
410 THP is a handle to the current thread; if INFOP is not NULL, the
411 struct thread_info associated with this thread is returned in
412 *INFOP.
413
414 If the thread is a zombie, TD_THR_ZOMBIE is returned. Otherwise,
415 zero is returned to indicate success. */
416
417 static int
418 thread_get_info_callback (const td_thrhandle_t *thp, void *argp)
419 {
420 td_thrinfo_t ti;
421 td_err_e err;
422 ptid_t thread_ptid;
423 struct thread_get_info_inout *inout;
424 struct thread_db_info *info;
425
426 inout = argp;
427 info = inout->thread_db_info;
428
429 err = info->td_thr_get_info_p (thp, &ti);
430 if (err != TD_OK)
431 error (_("thread_get_info_callback: cannot get thread info: %s"),
432 thread_db_err_str (err));
433
434 if (ti.ti_lid == -1)
435 {
436 /* We'll get this if a threaded program has a thread call clone
437 with CLONE_VM. `clone' sets the pthread LID of the new LWP
438 to -1, which ends up clearing the parent thread's LID. */
439 return 0;
440 }
441
442 /* Fill the cache. */
443 thread_ptid = ptid_build (info->pid, ti.ti_lid, 0);
444 inout->thread_info = find_thread_ptid (thread_ptid);
445
446 if (inout->thread_info == NULL)
447 {
448 /* New thread. Attach to it now (why wait?). */
449 if (!have_threads (thread_ptid))
450 thread_db_find_new_threads_1 (thread_ptid);
451 else
452 attach_thread (thread_ptid, thp, &ti);
453 inout->thread_info = find_thread_ptid (thread_ptid);
454 gdb_assert (inout->thread_info != NULL);
455 }
456
457 return 0;
458 }
459 \f
460 /* Fetch the user-level thread id of PTID. */
461
462 static void
463 thread_from_lwp (ptid_t ptid)
464 {
465 td_thrhandle_t th;
466 td_err_e err;
467 struct thread_db_info *info;
468 struct thread_get_info_inout io = {0};
469
470 /* Just in case td_ta_map_lwp2thr doesn't initialize it completely. */
471 th.th_unique = 0;
472
473 /* This ptid comes from linux-nat.c, which should always fill in the
474 LWP. */
475 gdb_assert (ptid_get_lwp (ptid) != 0);
476
477 info = get_thread_db_info (ptid_get_pid (ptid));
478
479 /* Access an lwp we know is stopped. */
480 info->proc_handle.ptid = ptid;
481 err = info->td_ta_map_lwp2thr_p (info->thread_agent, ptid_get_lwp (ptid),
482 &th);
483 if (err != TD_OK)
484 error (_("Cannot find user-level thread for LWP %ld: %s"),
485 ptid_get_lwp (ptid), thread_db_err_str (err));
486
487 /* Long-winded way of fetching the thread info. */
488 io.thread_db_info = info;
489 io.thread_info = NULL;
490 thread_get_info_callback (&th, &io);
491 }
492 \f
493
494 /* Attach to lwp PTID, doing whatever else is required to have this
495 LWP under the debugger's control --- e.g., enabling event
496 reporting. Returns true on success. */
497 int
498 thread_db_attach_lwp (ptid_t ptid)
499 {
500 td_thrhandle_t th;
501 td_thrinfo_t ti;
502 td_err_e err;
503 struct thread_db_info *info;
504
505 info = get_thread_db_info (ptid_get_pid (ptid));
506
507 if (info == NULL)
508 return 0;
509
510 /* This ptid comes from linux-nat.c, which should always fill in the
511 LWP. */
512 gdb_assert (ptid_get_lwp (ptid) != 0);
513
514 /* Access an lwp we know is stopped. */
515 info->proc_handle.ptid = ptid;
516
517 /* If we have only looked at the first thread before libpthread was
518 initialized, we may not know its thread ID yet. Make sure we do
519 before we add another thread to the list. */
520 if (!have_threads (ptid))
521 thread_db_find_new_threads_1 (ptid);
522
523 err = info->td_ta_map_lwp2thr_p (info->thread_agent, ptid_get_lwp (ptid),
524 &th);
525 if (err != TD_OK)
526 /* Cannot find user-level thread. */
527 return 0;
528
529 err = info->td_thr_get_info_p (&th, &ti);
530 if (err != TD_OK)
531 {
532 warning (_("Cannot get thread info: %s"), thread_db_err_str (err));
533 return 0;
534 }
535
536 attach_thread (ptid, &th, &ti);
537 return 1;
538 }
539
540 static void *
541 verbose_dlsym (void *handle, const char *name)
542 {
543 void *sym = dlsym (handle, name);
544 if (sym == NULL)
545 warning (_("Symbol \"%s\" not found in libthread_db: %s"),
546 name, dlerror ());
547 return sym;
548 }
549
550 static td_err_e
551 enable_thread_event (int event, CORE_ADDR *bp)
552 {
553 td_notify_t notify;
554 td_err_e err;
555 struct thread_db_info *info;
556
557 info = get_thread_db_info (ptid_get_pid (inferior_ptid));
558
559 /* Access an lwp we know is stopped. */
560 info->proc_handle.ptid = inferior_ptid;
561
562 /* Get the breakpoint address for thread EVENT. */
563 err = info->td_ta_event_addr_p (info->thread_agent, event, &notify);
564 if (err != TD_OK)
565 return err;
566
567 /* Set up the breakpoint. */
568 gdb_assert (exec_bfd);
569 (*bp) = (gdbarch_convert_from_func_ptr_addr
570 (target_gdbarch (),
571 /* Do proper sign extension for the target. */
572 (bfd_get_sign_extend_vma (exec_bfd) > 0
573 ? (CORE_ADDR) (intptr_t) notify.u.bptaddr
574 : (CORE_ADDR) (uintptr_t) notify.u.bptaddr),
575 &current_target));
576 create_thread_event_breakpoint (target_gdbarch (), *bp);
577
578 return TD_OK;
579 }
580
581 /* Verify inferior's '\0'-terminated symbol VER_SYMBOL starts with "%d.%d" and
582 return 1 if this version is lower (and not equal) to
583 VER_MAJOR_MIN.VER_MINOR_MIN. Return 0 in all other cases. */
584
585 static int
586 inferior_has_bug (const char *ver_symbol, int ver_major_min, int ver_minor_min)
587 {
588 struct bound_minimal_symbol version_msym;
589 CORE_ADDR version_addr;
590 char *version;
591 int err, got, retval = 0;
592
593 version_msym = lookup_minimal_symbol (ver_symbol, NULL, NULL);
594 if (version_msym.minsym == NULL)
595 return 0;
596
597 version_addr = BMSYMBOL_VALUE_ADDRESS (version_msym);
598 got = target_read_string (version_addr, &version, 32, &err);
599 if (err == 0 && memchr (version, 0, got) == &version[got -1])
600 {
601 int major, minor;
602
603 retval = (sscanf (version, "%d.%d", &major, &minor) == 2
604 && (major < ver_major_min
605 || (major == ver_major_min && minor < ver_minor_min)));
606 }
607 xfree (version);
608
609 return retval;
610 }
611
612 static void
613 enable_thread_event_reporting (void)
614 {
615 td_thr_events_t events;
616 td_err_e err;
617 struct thread_db_info *info;
618
619 info = get_thread_db_info (ptid_get_pid (inferior_ptid));
620
621 /* We cannot use the thread event reporting facility if these
622 functions aren't available. */
623 if (info->td_ta_event_addr_p == NULL
624 || info->td_ta_set_event_p == NULL
625 || info->td_ta_event_getmsg_p == NULL
626 || info->td_thr_event_enable_p == NULL)
627 return;
628
629 /* Set the process wide mask saying which events we're interested in. */
630 td_event_emptyset (&events);
631 td_event_addset (&events, TD_CREATE);
632
633 /* There is a bug fixed between linuxthreads 2.1.3 and 2.2 by
634 commit 2e4581e4fba917f1779cd0a010a45698586c190a
635 * manager.c (pthread_exited): Correctly report event as TD_REAP
636 instead of TD_DEATH. Fix comments.
637 where event reporting facility is broken for TD_DEATH events,
638 so don't enable it if we have glibc but a lower version. */
639 if (!inferior_has_bug ("__linuxthreads_version", 2, 2))
640 td_event_addset (&events, TD_DEATH);
641
642 err = info->td_ta_set_event_p (info->thread_agent, &events);
643 if (err != TD_OK)
644 {
645 warning (_("Unable to set global thread event mask: %s"),
646 thread_db_err_str (err));
647 return;
648 }
649
650 /* Delete previous thread event breakpoints, if any. */
651 remove_thread_event_breakpoints ();
652 info->td_create_bp_addr = 0;
653 info->td_death_bp_addr = 0;
654
655 /* Set up the thread creation event. */
656 err = enable_thread_event (TD_CREATE, &info->td_create_bp_addr);
657 if (err != TD_OK)
658 {
659 warning (_("Unable to get location for thread creation breakpoint: %s"),
660 thread_db_err_str (err));
661 return;
662 }
663
664 /* Set up the thread death event. */
665 err = enable_thread_event (TD_DEATH, &info->td_death_bp_addr);
666 if (err != TD_OK)
667 {
668 warning (_("Unable to get location for thread death breakpoint: %s"),
669 thread_db_err_str (err));
670 return;
671 }
672 }
673
674 /* Similar as thread_db_find_new_threads_1, but try to silently ignore errors
675 if appropriate.
676
677 Return 1 if the caller should abort libthread_db initialization. Return 0
678 otherwise. */
679
680 static int
681 thread_db_find_new_threads_silently (ptid_t ptid)
682 {
683 volatile struct gdb_exception except;
684
685 TRY_CATCH (except, RETURN_MASK_ERROR)
686 {
687 thread_db_find_new_threads_2 (ptid, 1);
688 }
689
690 if (except.reason < 0)
691 {
692 if (libthread_db_debug)
693 exception_fprintf (gdb_stdlog, except,
694 "Warning: thread_db_find_new_threads_silently: ");
695
696 /* There is a bug fixed between nptl 2.6.1 and 2.7 by
697 commit 7d9d8bd18906fdd17364f372b160d7ab896ce909
698 where calls to td_thr_get_info fail with TD_ERR for statically linked
699 executables if td_thr_get_info is called before glibc has initialized
700 itself.
701
702 If the nptl bug is NOT present in the inferior and still thread_db
703 reports an error return 1. It means the inferior has corrupted thread
704 list and GDB should fall back only to LWPs.
705
706 If the nptl bug is present in the inferior return 0 to silently ignore
707 such errors, and let gdb enumerate threads again later. In such case
708 GDB cannot properly display LWPs if the inferior thread list is
709 corrupted. For core files it does not apply, no 'later enumeration'
710 is possible. */
711
712 if (!target_has_execution || !inferior_has_bug ("nptl_version", 2, 7))
713 {
714 exception_fprintf (gdb_stderr, except,
715 _("Warning: couldn't activate thread debugging "
716 "using libthread_db: "));
717 return 1;
718 }
719 }
720 return 0;
721 }
722
723 /* Lookup a library in which given symbol resides.
724 Note: this is looking in GDB process, not in the inferior.
725 Returns library name, or NULL. */
726
727 static const char *
728 dladdr_to_soname (const void *addr)
729 {
730 Dl_info info;
731
732 if (dladdr (addr, &info) != 0)
733 return info.dli_fname;
734 return NULL;
735 }
736
737 /* Attempt to initialize dlopen()ed libthread_db, described by INFO.
738 Return 1 on success.
739 Failure could happen if libthread_db does not have symbols we expect,
740 or when it refuses to work with the current inferior (e.g. due to
741 version mismatch between libthread_db and libpthread). */
742
743 static int
744 try_thread_db_load_1 (struct thread_db_info *info)
745 {
746 td_err_e err;
747
748 /* Initialize pointers to the dynamic library functions we will use.
749 Essential functions first. */
750
751 info->td_init_p = verbose_dlsym (info->handle, "td_init");
752 if (info->td_init_p == NULL)
753 return 0;
754
755 err = info->td_init_p ();
756 if (err != TD_OK)
757 {
758 warning (_("Cannot initialize libthread_db: %s"),
759 thread_db_err_str (err));
760 return 0;
761 }
762
763 info->td_ta_new_p = verbose_dlsym (info->handle, "td_ta_new");
764 if (info->td_ta_new_p == NULL)
765 return 0;
766
767 /* Initialize the structure that identifies the child process. */
768 info->proc_handle.ptid = inferior_ptid;
769
770 /* Now attempt to open a connection to the thread library. */
771 err = info->td_ta_new_p (&info->proc_handle, &info->thread_agent);
772 if (err != TD_OK)
773 {
774 if (libthread_db_debug)
775 fprintf_unfiltered (gdb_stdlog, _("td_ta_new failed: %s\n"),
776 thread_db_err_str (err));
777 else
778 switch (err)
779 {
780 case TD_NOLIBTHREAD:
781 #ifdef THREAD_DB_HAS_TD_VERSION
782 case TD_VERSION:
783 #endif
784 /* The errors above are not unexpected and silently ignored:
785 they just mean we haven't found correct version of
786 libthread_db yet. */
787 break;
788 default:
789 warning (_("td_ta_new failed: %s"), thread_db_err_str (err));
790 }
791 return 0;
792 }
793
794 info->td_ta_map_id2thr_p = verbose_dlsym (info->handle, "td_ta_map_id2thr");
795 if (info->td_ta_map_id2thr_p == NULL)
796 return 0;
797
798 info->td_ta_map_lwp2thr_p = verbose_dlsym (info->handle,
799 "td_ta_map_lwp2thr");
800 if (info->td_ta_map_lwp2thr_p == NULL)
801 return 0;
802
803 info->td_ta_thr_iter_p = verbose_dlsym (info->handle, "td_ta_thr_iter");
804 if (info->td_ta_thr_iter_p == NULL)
805 return 0;
806
807 info->td_thr_validate_p = verbose_dlsym (info->handle, "td_thr_validate");
808 if (info->td_thr_validate_p == NULL)
809 return 0;
810
811 info->td_thr_get_info_p = verbose_dlsym (info->handle, "td_thr_get_info");
812 if (info->td_thr_get_info_p == NULL)
813 return 0;
814
815 /* These are not essential. */
816 info->td_ta_event_addr_p = dlsym (info->handle, "td_ta_event_addr");
817 info->td_ta_set_event_p = dlsym (info->handle, "td_ta_set_event");
818 info->td_ta_clear_event_p = dlsym (info->handle, "td_ta_clear_event");
819 info->td_ta_event_getmsg_p = dlsym (info->handle, "td_ta_event_getmsg");
820 info->td_thr_event_enable_p = dlsym (info->handle, "td_thr_event_enable");
821 info->td_thr_tls_get_addr_p = dlsym (info->handle, "td_thr_tls_get_addr");
822 info->td_thr_tlsbase_p = dlsym (info->handle, "td_thr_tlsbase");
823
824 if (thread_db_find_new_threads_silently (inferior_ptid) != 0)
825 {
826 /* Even if libthread_db initializes, if the thread list is
827 corrupted, we'd not manage to list any threads. Better reject this
828 thread_db, and fall back to at least listing LWPs. */
829 return 0;
830 }
831
832 printf_unfiltered (_("[Thread debugging using libthread_db enabled]\n"));
833
834 if (*libthread_db_search_path || libthread_db_debug)
835 {
836 struct ui_file *file;
837 const char *library;
838
839 library = dladdr_to_soname (*info->td_ta_new_p);
840 if (library == NULL)
841 library = LIBTHREAD_DB_SO;
842
843 /* If we'd print this to gdb_stdout when debug output is
844 disabled, still print it to gdb_stdout if debug output is
845 enabled. User visible output should not depend on debug
846 settings. */
847 file = *libthread_db_search_path != '\0' ? gdb_stdout : gdb_stdlog;
848 fprintf_unfiltered (file, _("Using host libthread_db library \"%s\".\n"),
849 library);
850 }
851
852 /* The thread library was detected. Activate the thread_db target
853 if this is the first process using it. */
854 if (thread_db_list->next == NULL)
855 push_target (&thread_db_ops);
856
857 /* Enable event reporting, but not when debugging a core file. */
858 if (target_has_execution && thread_db_use_events ())
859 enable_thread_event_reporting ();
860
861 return 1;
862 }
863
864 /* Attempt to use LIBRARY as libthread_db. LIBRARY could be absolute,
865 relative, or just LIBTHREAD_DB. */
866
867 static int
868 try_thread_db_load (const char *library, int check_auto_load_safe)
869 {
870 void *handle;
871 struct thread_db_info *info;
872
873 if (libthread_db_debug)
874 fprintf_unfiltered (gdb_stdlog,
875 _("Trying host libthread_db library: %s.\n"),
876 library);
877
878 if (check_auto_load_safe)
879 {
880 if (access (library, R_OK) != 0)
881 {
882 /* Do not print warnings by file_is_auto_load_safe if the library does
883 not exist at this place. */
884 if (libthread_db_debug)
885 fprintf_unfiltered (gdb_stdlog, _("open failed: %s.\n"),
886 safe_strerror (errno));
887 return 0;
888 }
889
890 if (!file_is_auto_load_safe (library, _("auto-load: Loading libthread-db "
891 "library \"%s\" from explicit "
892 "directory.\n"),
893 library))
894 return 0;
895 }
896
897 handle = dlopen (library, RTLD_NOW);
898 if (handle == NULL)
899 {
900 if (libthread_db_debug)
901 fprintf_unfiltered (gdb_stdlog, _("dlopen failed: %s.\n"), dlerror ());
902 return 0;
903 }
904
905 if (libthread_db_debug && strchr (library, '/') == NULL)
906 {
907 void *td_init;
908
909 td_init = dlsym (handle, "td_init");
910 if (td_init != NULL)
911 {
912 const char *const libpath = dladdr_to_soname (td_init);
913
914 if (libpath != NULL)
915 fprintf_unfiltered (gdb_stdlog, _("Host %s resolved to: %s.\n"),
916 library, libpath);
917 }
918 }
919
920 info = add_thread_db_info (handle);
921
922 /* Do not save system library name, that one is always trusted. */
923 if (strchr (library, '/') != NULL)
924 info->filename = gdb_realpath (library);
925
926 if (try_thread_db_load_1 (info))
927 return 1;
928
929 /* This library "refused" to work on current inferior. */
930 delete_thread_db_info (ptid_get_pid (inferior_ptid));
931 return 0;
932 }
933
934 /* Subroutine of try_thread_db_load_from_pdir to simplify it.
935 Try loading libthread_db in directory(OBJ)/SUBDIR.
936 SUBDIR may be NULL. It may also be something like "../lib64".
937 The result is true for success. */
938
939 static int
940 try_thread_db_load_from_pdir_1 (struct objfile *obj, const char *subdir)
941 {
942 struct cleanup *cleanup;
943 char *path, *cp;
944 int result;
945 const char *obj_name = objfile_name (obj);
946
947 if (obj_name[0] != '/')
948 {
949 warning (_("Expected absolute pathname for libpthread in the"
950 " inferior, but got %s."), obj_name);
951 return 0;
952 }
953
954 path = xmalloc (strlen (obj_name) + (subdir ? strlen (subdir) + 1 : 0)
955 + 1 + strlen (LIBTHREAD_DB_SO) + 1);
956 cleanup = make_cleanup (xfree, path);
957
958 strcpy (path, obj_name);
959 cp = strrchr (path, '/');
960 /* This should at minimum hit the first character. */
961 gdb_assert (cp != NULL);
962 cp[1] = '\0';
963 if (subdir != NULL)
964 {
965 strcat (cp, subdir);
966 strcat (cp, "/");
967 }
968 strcat (cp, LIBTHREAD_DB_SO);
969
970 result = try_thread_db_load (path, 1);
971
972 do_cleanups (cleanup);
973 return result;
974 }
975
976 /* Handle $pdir in libthread-db-search-path.
977 Look for libthread_db in directory(libpthread)/SUBDIR.
978 SUBDIR may be NULL. It may also be something like "../lib64".
979 The result is true for success. */
980
981 static int
982 try_thread_db_load_from_pdir (const char *subdir)
983 {
984 struct objfile *obj;
985
986 if (!auto_load_thread_db)
987 return 0;
988
989 ALL_OBJFILES (obj)
990 if (libpthread_name_p (objfile_name (obj)))
991 {
992 if (try_thread_db_load_from_pdir_1 (obj, subdir))
993 return 1;
994
995 /* We may have found the separate-debug-info version of
996 libpthread, and it may live in a directory without a matching
997 libthread_db. */
998 if (obj->separate_debug_objfile_backlink != NULL)
999 return try_thread_db_load_from_pdir_1 (obj->separate_debug_objfile_backlink,
1000 subdir);
1001
1002 return 0;
1003 }
1004
1005 return 0;
1006 }
1007
1008 /* Handle $sdir in libthread-db-search-path.
1009 Look for libthread_db in the system dirs, or wherever a plain
1010 dlopen(file_without_path) will look.
1011 The result is true for success. */
1012
1013 static int
1014 try_thread_db_load_from_sdir (void)
1015 {
1016 return try_thread_db_load (LIBTHREAD_DB_SO, 0);
1017 }
1018
1019 /* Try to load libthread_db from directory DIR of length DIR_LEN.
1020 The result is true for success. */
1021
1022 static int
1023 try_thread_db_load_from_dir (const char *dir, size_t dir_len)
1024 {
1025 struct cleanup *cleanup;
1026 char *path;
1027 int result;
1028
1029 if (!auto_load_thread_db)
1030 return 0;
1031
1032 path = xmalloc (dir_len + 1 + strlen (LIBTHREAD_DB_SO) + 1);
1033 cleanup = make_cleanup (xfree, path);
1034
1035 memcpy (path, dir, dir_len);
1036 path[dir_len] = '/';
1037 strcpy (path + dir_len + 1, LIBTHREAD_DB_SO);
1038
1039 result = try_thread_db_load (path, 1);
1040
1041 do_cleanups (cleanup);
1042 return result;
1043 }
1044
1045 /* Search libthread_db_search_path for libthread_db which "agrees"
1046 to work on current inferior.
1047 The result is true for success. */
1048
1049 static int
1050 thread_db_load_search (void)
1051 {
1052 VEC (char_ptr) *dir_vec;
1053 struct cleanup *cleanups;
1054 char *this_dir;
1055 int i, rc = 0;
1056
1057 dir_vec = dirnames_to_char_ptr_vec (libthread_db_search_path);
1058 cleanups = make_cleanup_free_char_ptr_vec (dir_vec);
1059
1060 for (i = 0; VEC_iterate (char_ptr, dir_vec, i, this_dir); ++i)
1061 {
1062 const int pdir_len = sizeof ("$pdir") - 1;
1063 size_t this_dir_len;
1064
1065 this_dir_len = strlen (this_dir);
1066
1067 if (strncmp (this_dir, "$pdir", pdir_len) == 0
1068 && (this_dir[pdir_len] == '\0'
1069 || this_dir[pdir_len] == '/'))
1070 {
1071 char *subdir = NULL;
1072 struct cleanup *free_subdir_cleanup
1073 = make_cleanup (null_cleanup, NULL);
1074
1075 if (this_dir[pdir_len] == '/')
1076 {
1077 subdir = xmalloc (strlen (this_dir));
1078 make_cleanup (xfree, subdir);
1079 strcpy (subdir, this_dir + pdir_len + 1);
1080 }
1081 rc = try_thread_db_load_from_pdir (subdir);
1082 do_cleanups (free_subdir_cleanup);
1083 if (rc)
1084 break;
1085 }
1086 else if (strcmp (this_dir, "$sdir") == 0)
1087 {
1088 if (try_thread_db_load_from_sdir ())
1089 {
1090 rc = 1;
1091 break;
1092 }
1093 }
1094 else
1095 {
1096 if (try_thread_db_load_from_dir (this_dir, this_dir_len))
1097 {
1098 rc = 1;
1099 break;
1100 }
1101 }
1102 }
1103
1104 do_cleanups (cleanups);
1105 if (libthread_db_debug)
1106 fprintf_unfiltered (gdb_stdlog,
1107 _("thread_db_load_search returning %d\n"), rc);
1108 return rc;
1109 }
1110
1111 /* Return non-zero if the inferior has a libpthread. */
1112
1113 static int
1114 has_libpthread (void)
1115 {
1116 struct objfile *obj;
1117
1118 ALL_OBJFILES (obj)
1119 if (libpthread_name_p (objfile_name (obj)))
1120 return 1;
1121
1122 return 0;
1123 }
1124
1125 /* Attempt to load and initialize libthread_db.
1126 Return 1 on success. */
1127
1128 static int
1129 thread_db_load (void)
1130 {
1131 struct thread_db_info *info;
1132
1133 info = get_thread_db_info (ptid_get_pid (inferior_ptid));
1134
1135 if (info != NULL)
1136 return 1;
1137
1138 /* Don't attempt to use thread_db on executables not running
1139 yet. */
1140 if (!target_has_registers)
1141 return 0;
1142
1143 /* Don't attempt to use thread_db for remote targets. */
1144 if (!(target_can_run (&current_target) || core_bfd))
1145 return 0;
1146
1147 if (thread_db_load_search ())
1148 return 1;
1149
1150 /* We couldn't find a libthread_db.
1151 If the inferior has a libpthread warn the user. */
1152 if (has_libpthread ())
1153 {
1154 warning (_("Unable to find libthread_db matching inferior's thread"
1155 " library, thread debugging will not be available."));
1156 return 0;
1157 }
1158
1159 /* Either this executable isn't using libpthread at all, or it is
1160 statically linked. Since we can't easily distinguish these two cases,
1161 no warning is issued. */
1162 return 0;
1163 }
1164
1165 static void
1166 disable_thread_event_reporting (struct thread_db_info *info)
1167 {
1168 if (info->td_ta_clear_event_p != NULL)
1169 {
1170 td_thr_events_t events;
1171
1172 /* Set the process wide mask saying we aren't interested in any
1173 events anymore. */
1174 td_event_fillset (&events);
1175 info->td_ta_clear_event_p (info->thread_agent, &events);
1176 }
1177
1178 info->td_create_bp_addr = 0;
1179 info->td_death_bp_addr = 0;
1180 }
1181
1182 static void
1183 check_thread_signals (void)
1184 {
1185 if (!thread_signals)
1186 {
1187 sigset_t mask;
1188 int i;
1189
1190 lin_thread_get_thread_signals (&mask);
1191 sigemptyset (&thread_stop_set);
1192 sigemptyset (&thread_print_set);
1193
1194 for (i = 1; i < NSIG; i++)
1195 {
1196 if (sigismember (&mask, i))
1197 {
1198 if (signal_stop_update (gdb_signal_from_host (i), 0))
1199 sigaddset (&thread_stop_set, i);
1200 if (signal_print_update (gdb_signal_from_host (i), 0))
1201 sigaddset (&thread_print_set, i);
1202 thread_signals = 1;
1203 }
1204 }
1205 }
1206 }
1207
1208 /* Check whether thread_db is usable. This function is called when
1209 an inferior is created (or otherwise acquired, e.g. attached to)
1210 and when new shared libraries are loaded into a running process. */
1211
1212 void
1213 check_for_thread_db (void)
1214 {
1215 /* Do nothing if we couldn't load libthread_db.so.1. */
1216 if (!thread_db_load ())
1217 return;
1218 }
1219
1220 /* This function is called via the new_objfile observer. */
1221
1222 static void
1223 thread_db_new_objfile (struct objfile *objfile)
1224 {
1225 /* This observer must always be called with inferior_ptid set
1226 correctly. */
1227
1228 if (objfile != NULL
1229 /* libpthread with separate debug info has its debug info file already
1230 loaded (and notified without successful thread_db initialization)
1231 the time observer_notify_new_objfile is called for the library itself.
1232 Static executables have their separate debug info loaded already
1233 before the inferior has started. */
1234 && objfile->separate_debug_objfile_backlink == NULL
1235 /* Only check for thread_db if we loaded libpthread,
1236 or if this is the main symbol file.
1237 We need to check OBJF_MAINLINE to handle the case of debugging
1238 a statically linked executable AND the symbol file is specified AFTER
1239 the exec file is loaded (e.g., gdb -c core ; file foo).
1240 For dynamically linked executables, libpthread can be near the end
1241 of the list of shared libraries to load, and in an app of several
1242 thousand shared libraries, this can otherwise be painful. */
1243 && ((objfile->flags & OBJF_MAINLINE) != 0
1244 || libpthread_name_p (objfile_name (objfile))))
1245 check_for_thread_db ();
1246 }
1247
1248 static void
1249 check_pid_namespace_match (void)
1250 {
1251 /* Check is only relevant for local targets targets. */
1252 if (target_can_run (&current_target))
1253 {
1254 /* If the child is in a different PID namespace, its idea of its
1255 PID will differ from our idea of its PID. When we scan the
1256 child's thread list, we'll mistakenly think it has no threads
1257 since the thread PID fields won't match the PID we give to
1258 libthread_db. */
1259 char *our_pid_ns = linux_proc_pid_get_ns (getpid (), "pid");
1260 char *inferior_pid_ns = linux_proc_pid_get_ns (
1261 ptid_get_pid (inferior_ptid), "pid");
1262
1263 if (our_pid_ns != NULL && inferior_pid_ns != NULL
1264 && strcmp (our_pid_ns, inferior_pid_ns) != 0)
1265 {
1266 warning (_ ("Target and debugger are in different PID "
1267 "namespaces; thread lists and other data are "
1268 "likely unreliable"));
1269 }
1270
1271 xfree (our_pid_ns);
1272 xfree (inferior_pid_ns);
1273 }
1274 }
1275
1276 /* This function is called via the inferior_created observer.
1277 This handles the case of debugging statically linked executables. */
1278
1279 static void
1280 thread_db_inferior_created (struct target_ops *target, int from_tty)
1281 {
1282 check_pid_namespace_match ();
1283 check_for_thread_db ();
1284 }
1285
1286 /* Update the thread's state (what's displayed in "info threads"),
1287 from libthread_db thread state information. */
1288
1289 static void
1290 update_thread_state (struct private_thread_info *private,
1291 const td_thrinfo_t *ti_p)
1292 {
1293 private->dying = (ti_p->ti_state == TD_THR_UNKNOWN
1294 || ti_p->ti_state == TD_THR_ZOMBIE);
1295 }
1296
1297 /* Attach to a new thread. This function is called when we receive a
1298 TD_CREATE event or when we iterate over all threads and find one
1299 that wasn't already in our list. Returns true on success. */
1300
1301 static int
1302 attach_thread (ptid_t ptid, const td_thrhandle_t *th_p,
1303 const td_thrinfo_t *ti_p)
1304 {
1305 struct private_thread_info *private;
1306 struct thread_info *tp;
1307 td_err_e err;
1308 struct thread_db_info *info;
1309
1310 /* If we're being called after a TD_CREATE event, we may already
1311 know about this thread. There are two ways this can happen. We
1312 may have iterated over all threads between the thread creation
1313 and the TD_CREATE event, for instance when the user has issued
1314 the `info threads' command before the SIGTRAP for hitting the
1315 thread creation breakpoint was reported. Alternatively, the
1316 thread may have exited and a new one been created with the same
1317 thread ID. In the first case we don't need to do anything; in
1318 the second case we should discard information about the dead
1319 thread and attach to the new one. */
1320 tp = find_thread_ptid (ptid);
1321 if (tp != NULL)
1322 {
1323 /* If tp->private is NULL, then GDB is already attached to this
1324 thread, but we do not know anything about it. We can learn
1325 about it here. This can only happen if we have some other
1326 way besides libthread_db to notice new threads (i.e.
1327 PTRACE_EVENT_CLONE); assume the same mechanism notices thread
1328 exit, so this can not be a stale thread recreated with the
1329 same ID. */
1330 if (tp->private != NULL)
1331 {
1332 if (!tp->private->dying)
1333 return 0;
1334
1335 delete_thread (ptid);
1336 tp = NULL;
1337 }
1338 }
1339
1340 if (target_has_execution)
1341 check_thread_signals ();
1342
1343 /* Under GNU/Linux, we have to attach to each and every thread. */
1344 if (target_has_execution
1345 && tp == NULL)
1346 {
1347 int res;
1348
1349 res = lin_lwp_attach_lwp (ptid_build (ptid_get_pid (ptid),
1350 ti_p->ti_lid, 0));
1351 if (res < 0)
1352 {
1353 /* Error, stop iterating. */
1354 return 0;
1355 }
1356 else if (res > 0)
1357 {
1358 /* Pretend this thread doesn't exist yet, and keep
1359 iterating. */
1360 return 1;
1361 }
1362
1363 /* Otherwise, we sucessfully attached to the thread. */
1364 }
1365
1366 /* Construct the thread's private data. */
1367 private = xmalloc (sizeof (struct private_thread_info));
1368 memset (private, 0, sizeof (struct private_thread_info));
1369
1370 /* A thread ID of zero may mean the thread library has not initialized
1371 yet. But we shouldn't even get here if that's the case. FIXME:
1372 if we change GDB to always have at least one thread in the thread
1373 list this will have to go somewhere else; maybe private == NULL
1374 until the thread_db target claims it. */
1375 gdb_assert (ti_p->ti_tid != 0);
1376 private->th = *th_p;
1377 private->tid = ti_p->ti_tid;
1378 update_thread_state (private, ti_p);
1379
1380 /* Add the thread to GDB's thread list. */
1381 if (tp == NULL)
1382 add_thread_with_info (ptid, private);
1383 else
1384 tp->private = private;
1385
1386 info = get_thread_db_info (ptid_get_pid (ptid));
1387
1388 /* Enable thread event reporting for this thread, except when
1389 debugging a core file. */
1390 if (target_has_execution && thread_db_use_events ())
1391 {
1392 err = info->td_thr_event_enable_p (th_p, 1);
1393 if (err != TD_OK)
1394 error (_("Cannot enable thread event reporting for %s: %s"),
1395 target_pid_to_str (ptid), thread_db_err_str (err));
1396 }
1397
1398 return 1;
1399 }
1400
1401 static void
1402 detach_thread (ptid_t ptid)
1403 {
1404 struct thread_info *thread_info;
1405
1406 /* Don't delete the thread now, because it still reports as active
1407 until it has executed a few instructions after the event
1408 breakpoint - if we deleted it now, "info threads" would cause us
1409 to re-attach to it. Just mark it as having had a TD_DEATH
1410 event. This means that we won't delete it from our thread list
1411 until we notice that it's dead (via prune_threads), or until
1412 something re-uses its thread ID. We'll report the thread exit
1413 when the underlying LWP dies. */
1414 thread_info = find_thread_ptid (ptid);
1415 gdb_assert (thread_info != NULL && thread_info->private != NULL);
1416 thread_info->private->dying = 1;
1417 }
1418
1419 static void
1420 thread_db_detach (struct target_ops *ops, const char *args, int from_tty)
1421 {
1422 struct target_ops *target_beneath = find_target_beneath (ops);
1423 struct thread_db_info *info;
1424
1425 info = get_thread_db_info (ptid_get_pid (inferior_ptid));
1426
1427 if (info)
1428 {
1429 if (target_has_execution && thread_db_use_events ())
1430 {
1431 disable_thread_event_reporting (info);
1432
1433 /* Delete the old thread event breakpoints. Note that
1434 unlike when mourning, we can remove them here because
1435 there's still a live inferior to poke at. In any case,
1436 GDB will not try to insert anything in the inferior when
1437 removing a breakpoint. */
1438 remove_thread_event_breakpoints ();
1439 }
1440
1441 delete_thread_db_info (ptid_get_pid (inferior_ptid));
1442 }
1443
1444 target_beneath->to_detach (target_beneath, args, from_tty);
1445
1446 /* NOTE: From this point on, inferior_ptid is null_ptid. */
1447
1448 /* If there are no more processes using libpthread, detach the
1449 thread_db target ops. */
1450 if (!thread_db_list)
1451 unpush_target (&thread_db_ops);
1452 }
1453
1454 /* Check if PID is currently stopped at the location of a thread event
1455 breakpoint location. If it is, read the event message and act upon
1456 the event. */
1457
1458 static void
1459 check_event (ptid_t ptid)
1460 {
1461 struct regcache *regcache = get_thread_regcache (ptid);
1462 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1463 td_event_msg_t msg;
1464 td_thrinfo_t ti;
1465 td_err_e err;
1466 CORE_ADDR stop_pc;
1467 int loop = 0;
1468 struct thread_db_info *info;
1469
1470 info = get_thread_db_info (ptid_get_pid (ptid));
1471
1472 /* Bail out early if we're not at a thread event breakpoint. */
1473 stop_pc = regcache_read_pc (regcache)
1474 - target_decr_pc_after_break (gdbarch);
1475 if (stop_pc != info->td_create_bp_addr
1476 && stop_pc != info->td_death_bp_addr)
1477 return;
1478
1479 /* Access an lwp we know is stopped. */
1480 info->proc_handle.ptid = ptid;
1481
1482 /* If we have only looked at the first thread before libpthread was
1483 initialized, we may not know its thread ID yet. Make sure we do
1484 before we add another thread to the list. */
1485 if (!have_threads (ptid))
1486 thread_db_find_new_threads_1 (ptid);
1487
1488 /* If we are at a create breakpoint, we do not know what new lwp
1489 was created and cannot specifically locate the event message for it.
1490 We have to call td_ta_event_getmsg() to get
1491 the latest message. Since we have no way of correlating whether
1492 the event message we get back corresponds to our breakpoint, we must
1493 loop and read all event messages, processing them appropriately.
1494 This guarantees we will process the correct message before continuing
1495 from the breakpoint.
1496
1497 Currently, death events are not enabled. If they are enabled,
1498 the death event can use the td_thr_event_getmsg() interface to
1499 get the message specifically for that lwp and avoid looping
1500 below. */
1501
1502 loop = 1;
1503
1504 do
1505 {
1506 err = info->td_ta_event_getmsg_p (info->thread_agent, &msg);
1507 if (err != TD_OK)
1508 {
1509 if (err == TD_NOMSG)
1510 return;
1511
1512 error (_("Cannot get thread event message: %s"),
1513 thread_db_err_str (err));
1514 }
1515
1516 err = info->td_thr_get_info_p (msg.th_p, &ti);
1517 if (err != TD_OK)
1518 error (_("Cannot get thread info: %s"), thread_db_err_str (err));
1519
1520 ptid = ptid_build (ptid_get_pid (ptid), ti.ti_lid, 0);
1521
1522 switch (msg.event)
1523 {
1524 case TD_CREATE:
1525 /* Call attach_thread whether or not we already know about a
1526 thread with this thread ID. */
1527 attach_thread (ptid, msg.th_p, &ti);
1528
1529 break;
1530
1531 case TD_DEATH:
1532
1533 if (!in_thread_list (ptid))
1534 error (_("Spurious thread death event."));
1535
1536 detach_thread (ptid);
1537
1538 break;
1539
1540 default:
1541 error (_("Spurious thread event."));
1542 }
1543 }
1544 while (loop);
1545 }
1546
1547 static ptid_t
1548 thread_db_wait (struct target_ops *ops,
1549 ptid_t ptid, struct target_waitstatus *ourstatus,
1550 int options)
1551 {
1552 struct thread_db_info *info;
1553 struct target_ops *beneath = find_target_beneath (ops);
1554
1555 ptid = beneath->to_wait (beneath, ptid, ourstatus, options);
1556
1557 if (ourstatus->kind == TARGET_WAITKIND_IGNORE)
1558 return ptid;
1559
1560 if (ourstatus->kind == TARGET_WAITKIND_EXITED
1561 || ourstatus->kind == TARGET_WAITKIND_SIGNALLED)
1562 return ptid;
1563
1564 info = get_thread_db_info (ptid_get_pid (ptid));
1565
1566 /* If this process isn't using thread_db, we're done. */
1567 if (info == NULL)
1568 return ptid;
1569
1570 if (ourstatus->kind == TARGET_WAITKIND_EXECD)
1571 {
1572 /* New image, it may or may not end up using thread_db. Assume
1573 not unless we find otherwise. */
1574 delete_thread_db_info (ptid_get_pid (ptid));
1575 if (!thread_db_list)
1576 unpush_target (&thread_db_ops);
1577
1578 /* Thread event breakpoints are deleted by
1579 update_breakpoints_after_exec. */
1580
1581 return ptid;
1582 }
1583
1584 /* If we do not know about the main thread yet, this would be a good time to
1585 find it. */
1586 if (ourstatus->kind == TARGET_WAITKIND_STOPPED && !have_threads (ptid))
1587 thread_db_find_new_threads_1 (ptid);
1588
1589 if (ourstatus->kind == TARGET_WAITKIND_STOPPED
1590 && ourstatus->value.sig == GDB_SIGNAL_TRAP)
1591 /* Check for a thread event. */
1592 check_event (ptid);
1593
1594 if (have_threads (ptid))
1595 {
1596 /* Fill in the thread's user-level thread id. */
1597 thread_from_lwp (ptid);
1598 }
1599
1600 return ptid;
1601 }
1602
1603 static void
1604 thread_db_mourn_inferior (struct target_ops *ops)
1605 {
1606 struct target_ops *target_beneath = find_target_beneath (ops);
1607
1608 delete_thread_db_info (ptid_get_pid (inferior_ptid));
1609
1610 target_beneath->to_mourn_inferior (target_beneath);
1611
1612 /* Delete the old thread event breakpoints. Do this after mourning
1613 the inferior, so that we don't try to uninsert them. */
1614 remove_thread_event_breakpoints ();
1615
1616 /* Detach thread_db target ops. */
1617 if (!thread_db_list)
1618 unpush_target (ops);
1619 }
1620
1621 struct callback_data
1622 {
1623 struct thread_db_info *info;
1624 int new_threads;
1625 };
1626
1627 static int
1628 find_new_threads_callback (const td_thrhandle_t *th_p, void *data)
1629 {
1630 td_thrinfo_t ti;
1631 td_err_e err;
1632 ptid_t ptid;
1633 struct thread_info *tp;
1634 struct callback_data *cb_data = data;
1635 struct thread_db_info *info = cb_data->info;
1636
1637 err = info->td_thr_get_info_p (th_p, &ti);
1638 if (err != TD_OK)
1639 error (_("find_new_threads_callback: cannot get thread info: %s"),
1640 thread_db_err_str (err));
1641
1642 if (ti.ti_lid == -1)
1643 {
1644 /* A thread with kernel thread ID -1 is either a thread that
1645 exited and was joined, or a thread that is being created but
1646 hasn't started yet, and that is reusing the tcb/stack of a
1647 thread that previously exited and was joined. (glibc marks
1648 terminated and joined threads with kernel thread ID -1. See
1649 glibc PR17707. */
1650 if (libthread_db_debug)
1651 fprintf_unfiltered (gdb_stdlog,
1652 "thread_db: skipping exited and "
1653 "joined thread (0x%lx)\n", ti.ti_tid);
1654 return 0;
1655 }
1656
1657 if (ti.ti_tid == 0)
1658 {
1659 /* A thread ID of zero means that this is the main thread, but
1660 glibc has not yet initialized thread-local storage and the
1661 pthread library. We do not know what the thread's TID will
1662 be yet. Just enable event reporting and otherwise ignore
1663 it. */
1664
1665 /* In that case, we're not stopped in a fork syscall and don't
1666 need this glibc bug workaround. */
1667 info->need_stale_parent_threads_check = 0;
1668
1669 if (target_has_execution && thread_db_use_events ())
1670 {
1671 err = info->td_thr_event_enable_p (th_p, 1);
1672 if (err != TD_OK)
1673 error (_("Cannot enable thread event reporting for LWP %d: %s"),
1674 (int) ti.ti_lid, thread_db_err_str (err));
1675 }
1676
1677 return 0;
1678 }
1679
1680 /* Ignore stale parent threads, caused by glibc/BZ5983. This is a
1681 bit expensive, as it needs to open /proc/pid/status, so try to
1682 avoid doing the work if we know we don't have to. */
1683 if (info->need_stale_parent_threads_check)
1684 {
1685 int tgid = linux_proc_get_tgid (ti.ti_lid);
1686
1687 if (tgid != -1 && tgid != info->pid)
1688 return 0;
1689 }
1690
1691 ptid = ptid_build (info->pid, ti.ti_lid, 0);
1692 tp = find_thread_ptid (ptid);
1693 if (tp == NULL || tp->private == NULL)
1694 {
1695 if (attach_thread (ptid, th_p, &ti))
1696 cb_data->new_threads += 1;
1697 else
1698 /* Problem attaching this thread; perhaps it exited before we
1699 could attach it?
1700 This could mean that the thread list inside glibc itself is in
1701 inconsistent state, and libthread_db could go on looping forever
1702 (observed with glibc-2.3.6). To prevent that, terminate
1703 iteration: thread_db_find_new_threads_2 will retry. */
1704 return 1;
1705 }
1706 else if (target_has_execution && !thread_db_use_events ())
1707 {
1708 /* Need to update this if not using the libthread_db events
1709 (particularly, the TD_DEATH event). */
1710 update_thread_state (tp->private, &ti);
1711 }
1712
1713 return 0;
1714 }
1715
1716 /* Helper for thread_db_find_new_threads_2.
1717 Returns number of new threads found. */
1718
1719 static int
1720 find_new_threads_once (struct thread_db_info *info, int iteration,
1721 td_err_e *errp)
1722 {
1723 volatile struct gdb_exception except;
1724 struct callback_data data;
1725 td_err_e err = TD_ERR;
1726
1727 data.info = info;
1728 data.new_threads = 0;
1729
1730 TRY_CATCH (except, RETURN_MASK_ERROR)
1731 {
1732 /* Iterate over all user-space threads to discover new threads. */
1733 err = info->td_ta_thr_iter_p (info->thread_agent,
1734 find_new_threads_callback,
1735 &data,
1736 TD_THR_ANY_STATE,
1737 TD_THR_LOWEST_PRIORITY,
1738 TD_SIGNO_MASK,
1739 TD_THR_ANY_USER_FLAGS);
1740 }
1741
1742 if (libthread_db_debug)
1743 {
1744 if (except.reason < 0)
1745 exception_fprintf (gdb_stdlog, except,
1746 "Warning: find_new_threads_once: ");
1747
1748 fprintf_unfiltered (gdb_stdlog,
1749 _("Found %d new threads in iteration %d.\n"),
1750 data.new_threads, iteration);
1751 }
1752
1753 if (errp != NULL)
1754 *errp = err;
1755
1756 return data.new_threads;
1757 }
1758
1759 /* Search for new threads, accessing memory through stopped thread
1760 PTID. If UNTIL_NO_NEW is true, repeat searching until several
1761 searches in a row do not discover any new threads. */
1762
1763 static void
1764 thread_db_find_new_threads_2 (ptid_t ptid, int until_no_new)
1765 {
1766 td_err_e err = TD_OK;
1767 struct thread_db_info *info;
1768 int i, loop;
1769
1770 info = get_thread_db_info (ptid_get_pid (ptid));
1771
1772 /* Access an lwp we know is stopped. */
1773 info->proc_handle.ptid = ptid;
1774
1775 if (until_no_new)
1776 {
1777 /* Require 4 successive iterations which do not find any new threads.
1778 The 4 is a heuristic: there is an inherent race here, and I have
1779 seen that 2 iterations in a row are not always sufficient to
1780 "capture" all threads. */
1781 for (i = 0, loop = 0; loop < 4 && err == TD_OK; ++i, ++loop)
1782 if (find_new_threads_once (info, i, &err) != 0)
1783 {
1784 /* Found some new threads. Restart the loop from beginning. */
1785 loop = -1;
1786 }
1787 }
1788 else
1789 find_new_threads_once (info, 0, &err);
1790
1791 if (err != TD_OK)
1792 error (_("Cannot find new threads: %s"), thread_db_err_str (err));
1793 }
1794
1795 static void
1796 thread_db_find_new_threads_1 (ptid_t ptid)
1797 {
1798 thread_db_find_new_threads_2 (ptid, 0);
1799 }
1800
1801 static int
1802 update_thread_core (struct lwp_info *info, void *closure)
1803 {
1804 info->core = linux_common_core_of_thread (info->ptid);
1805 return 0;
1806 }
1807
1808 static void
1809 thread_db_update_thread_list (struct target_ops *ops)
1810 {
1811 struct thread_db_info *info;
1812 struct inferior *inf;
1813
1814 prune_threads ();
1815
1816 ALL_INFERIORS (inf)
1817 {
1818 struct thread_info *thread;
1819
1820 if (inf->pid == 0)
1821 continue;
1822
1823 info = get_thread_db_info (inf->pid);
1824 if (info == NULL)
1825 continue;
1826
1827 thread = any_live_thread_of_process (inf->pid);
1828 if (thread == NULL || thread->executing)
1829 continue;
1830
1831 thread_db_find_new_threads_1 (thread->ptid);
1832 }
1833
1834 if (target_has_execution)
1835 iterate_over_lwps (minus_one_ptid /* iterate over all */,
1836 update_thread_core, NULL);
1837 }
1838
1839 static char *
1840 thread_db_pid_to_str (struct target_ops *ops, ptid_t ptid)
1841 {
1842 struct thread_info *thread_info = find_thread_ptid (ptid);
1843 struct target_ops *beneath;
1844
1845 if (thread_info != NULL && thread_info->private != NULL)
1846 {
1847 static char buf[64];
1848 thread_t tid;
1849
1850 tid = thread_info->private->tid;
1851 snprintf (buf, sizeof (buf), "Thread 0x%lx (LWP %ld)",
1852 tid, ptid_get_lwp (ptid));
1853
1854 return buf;
1855 }
1856
1857 beneath = find_target_beneath (ops);
1858 return beneath->to_pid_to_str (beneath, ptid);
1859 }
1860
1861 /* Return a string describing the state of the thread specified by
1862 INFO. */
1863
1864 static char *
1865 thread_db_extra_thread_info (struct target_ops *self,
1866 struct thread_info *info)
1867 {
1868 if (info->private == NULL)
1869 return NULL;
1870
1871 if (info->private->dying)
1872 return "Exiting";
1873
1874 return NULL;
1875 }
1876
1877 /* Get the address of the thread local variable in load module LM which
1878 is stored at OFFSET within the thread local storage for thread PTID. */
1879
1880 static CORE_ADDR
1881 thread_db_get_thread_local_address (struct target_ops *ops,
1882 ptid_t ptid,
1883 CORE_ADDR lm,
1884 CORE_ADDR offset)
1885 {
1886 struct thread_info *thread_info;
1887 struct target_ops *beneath;
1888
1889 /* If we have not discovered any threads yet, check now. */
1890 if (!have_threads (ptid))
1891 thread_db_find_new_threads_1 (ptid);
1892
1893 /* Find the matching thread. */
1894 thread_info = find_thread_ptid (ptid);
1895
1896 if (thread_info != NULL && thread_info->private != NULL)
1897 {
1898 td_err_e err;
1899 psaddr_t address;
1900 struct thread_db_info *info;
1901
1902 info = get_thread_db_info (ptid_get_pid (ptid));
1903
1904 /* Finally, get the address of the variable. */
1905 if (lm != 0)
1906 {
1907 /* glibc doesn't provide the needed interface. */
1908 if (!info->td_thr_tls_get_addr_p)
1909 throw_error (TLS_NO_LIBRARY_SUPPORT_ERROR,
1910 _("No TLS library support"));
1911
1912 /* Note the cast through uintptr_t: this interface only works if
1913 a target address fits in a psaddr_t, which is a host pointer.
1914 So a 32-bit debugger can not access 64-bit TLS through this. */
1915 err = info->td_thr_tls_get_addr_p (&thread_info->private->th,
1916 (psaddr_t)(uintptr_t) lm,
1917 offset, &address);
1918 }
1919 else
1920 {
1921 /* If glibc doesn't provide the needed interface throw an error
1922 that LM is zero - normally cases it should not be. */
1923 if (!info->td_thr_tlsbase_p)
1924 throw_error (TLS_LOAD_MODULE_NOT_FOUND_ERROR,
1925 _("TLS load module not found"));
1926
1927 /* This code path handles the case of -static -pthread executables:
1928 https://sourceware.org/ml/libc-help/2014-03/msg00024.html
1929 For older GNU libc r_debug.r_map is NULL. For GNU libc after
1930 PR libc/16831 due to GDB PR threads/16954 LOAD_MODULE is also NULL.
1931 The constant number 1 depends on GNU __libc_setup_tls
1932 initialization of l_tls_modid to 1. */
1933 err = info->td_thr_tlsbase_p (&thread_info->private->th,
1934 1, &address);
1935 address = (char *) address + offset;
1936 }
1937
1938 #ifdef THREAD_DB_HAS_TD_NOTALLOC
1939 /* The memory hasn't been allocated, yet. */
1940 if (err == TD_NOTALLOC)
1941 /* Now, if libthread_db provided the initialization image's
1942 address, we *could* try to build a non-lvalue value from
1943 the initialization image. */
1944 throw_error (TLS_NOT_ALLOCATED_YET_ERROR,
1945 _("TLS not allocated yet"));
1946 #endif
1947
1948 /* Something else went wrong. */
1949 if (err != TD_OK)
1950 throw_error (TLS_GENERIC_ERROR,
1951 (("%s")), thread_db_err_str (err));
1952
1953 /* Cast assuming host == target. Joy. */
1954 /* Do proper sign extension for the target. */
1955 gdb_assert (exec_bfd);
1956 return (bfd_get_sign_extend_vma (exec_bfd) > 0
1957 ? (CORE_ADDR) (intptr_t) address
1958 : (CORE_ADDR) (uintptr_t) address);
1959 }
1960
1961 beneath = find_target_beneath (ops);
1962 return beneath->to_get_thread_local_address (beneath, ptid, lm, offset);
1963 }
1964
1965 /* Callback routine used to find a thread based on the TID part of
1966 its PTID. */
1967
1968 static int
1969 thread_db_find_thread_from_tid (struct thread_info *thread, void *data)
1970 {
1971 long *tid = (long *) data;
1972
1973 if (thread->private->tid == *tid)
1974 return 1;
1975
1976 return 0;
1977 }
1978
1979 /* Implement the to_get_ada_task_ptid target method for this target. */
1980
1981 static ptid_t
1982 thread_db_get_ada_task_ptid (struct target_ops *self, long lwp, long thread)
1983 {
1984 struct thread_info *thread_info;
1985
1986 thread_db_find_new_threads_1 (inferior_ptid);
1987 thread_info = iterate_over_threads (thread_db_find_thread_from_tid, &thread);
1988
1989 gdb_assert (thread_info != NULL);
1990
1991 return (thread_info->ptid);
1992 }
1993
1994 static void
1995 thread_db_resume (struct target_ops *ops,
1996 ptid_t ptid, int step, enum gdb_signal signo)
1997 {
1998 struct target_ops *beneath = find_target_beneath (ops);
1999 struct thread_db_info *info;
2000
2001 if (ptid_equal (ptid, minus_one_ptid))
2002 info = get_thread_db_info (ptid_get_pid (inferior_ptid));
2003 else
2004 info = get_thread_db_info (ptid_get_pid (ptid));
2005
2006 /* This workaround is only needed for child fork lwps stopped in a
2007 PTRACE_O_TRACEFORK event. When the inferior is resumed, the
2008 workaround can be disabled. */
2009 if (info)
2010 info->need_stale_parent_threads_check = 0;
2011
2012 beneath->to_resume (beneath, ptid, step, signo);
2013 }
2014
2015 /* qsort helper function for info_auto_load_libthread_db, sort the
2016 thread_db_info pointers primarily by their FILENAME and secondarily by their
2017 PID, both in ascending order. */
2018
2019 static int
2020 info_auto_load_libthread_db_compare (const void *ap, const void *bp)
2021 {
2022 struct thread_db_info *a = *(struct thread_db_info **) ap;
2023 struct thread_db_info *b = *(struct thread_db_info **) bp;
2024 int retval;
2025
2026 retval = strcmp (a->filename, b->filename);
2027 if (retval)
2028 return retval;
2029
2030 return (a->pid > b->pid) - (a->pid - b->pid);
2031 }
2032
2033 /* Implement 'info auto-load libthread-db'. */
2034
2035 static void
2036 info_auto_load_libthread_db (char *args, int from_tty)
2037 {
2038 struct ui_out *uiout = current_uiout;
2039 const char *cs = args ? args : "";
2040 struct thread_db_info *info, **array;
2041 unsigned info_count, unique_filenames;
2042 size_t max_filename_len, max_pids_len, pids_len;
2043 struct cleanup *back_to;
2044 char *pids;
2045 int i;
2046
2047 cs = skip_spaces_const (cs);
2048 if (*cs)
2049 error (_("'info auto-load libthread-db' does not accept any parameters"));
2050
2051 info_count = 0;
2052 for (info = thread_db_list; info; info = info->next)
2053 if (info->filename != NULL)
2054 info_count++;
2055
2056 array = xmalloc (sizeof (*array) * info_count);
2057 back_to = make_cleanup (xfree, array);
2058
2059 info_count = 0;
2060 for (info = thread_db_list; info; info = info->next)
2061 if (info->filename != NULL)
2062 array[info_count++] = info;
2063
2064 /* Sort ARRAY by filenames and PIDs. */
2065
2066 qsort (array, info_count, sizeof (*array),
2067 info_auto_load_libthread_db_compare);
2068
2069 /* Calculate the number of unique filenames (rows) and the maximum string
2070 length of PIDs list for the unique filenames (columns). */
2071
2072 unique_filenames = 0;
2073 max_filename_len = 0;
2074 max_pids_len = 0;
2075 pids_len = 0;
2076 for (i = 0; i < info_count; i++)
2077 {
2078 int pid = array[i]->pid;
2079 size_t this_pid_len;
2080
2081 for (this_pid_len = 0; pid != 0; pid /= 10)
2082 this_pid_len++;
2083
2084 if (i == 0 || strcmp (array[i - 1]->filename, array[i]->filename) != 0)
2085 {
2086 unique_filenames++;
2087 max_filename_len = max (max_filename_len,
2088 strlen (array[i]->filename));
2089
2090 if (i > 0)
2091 {
2092 pids_len -= strlen (", ");
2093 max_pids_len = max (max_pids_len, pids_len);
2094 }
2095 pids_len = 0;
2096 }
2097 pids_len += this_pid_len + strlen (", ");
2098 }
2099 if (i)
2100 {
2101 pids_len -= strlen (", ");
2102 max_pids_len = max (max_pids_len, pids_len);
2103 }
2104
2105 /* Table header shifted right by preceding "libthread-db: " would not match
2106 its columns. */
2107 if (info_count > 0 && args == auto_load_info_scripts_pattern_nl)
2108 ui_out_text (uiout, "\n");
2109
2110 make_cleanup_ui_out_table_begin_end (uiout, 2, unique_filenames,
2111 "LinuxThreadDbTable");
2112
2113 ui_out_table_header (uiout, max_filename_len, ui_left, "filename",
2114 "Filename");
2115 ui_out_table_header (uiout, pids_len, ui_left, "PIDs", "Pids");
2116 ui_out_table_body (uiout);
2117
2118 pids = xmalloc (max_pids_len + 1);
2119 make_cleanup (xfree, pids);
2120
2121 /* Note I is incremented inside the cycle, not at its end. */
2122 for (i = 0; i < info_count;)
2123 {
2124 struct cleanup *chain = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
2125 char *pids_end;
2126
2127 info = array[i];
2128 ui_out_field_string (uiout, "filename", info->filename);
2129 pids_end = pids;
2130
2131 while (i < info_count && strcmp (info->filename, array[i]->filename) == 0)
2132 {
2133 if (pids_end != pids)
2134 {
2135 *pids_end++ = ',';
2136 *pids_end++ = ' ';
2137 }
2138 pids_end += xsnprintf (pids_end, &pids[max_pids_len + 1] - pids_end,
2139 "%u", array[i]->pid);
2140 gdb_assert (pids_end < &pids[max_pids_len + 1]);
2141
2142 i++;
2143 }
2144 *pids_end = '\0';
2145
2146 ui_out_field_string (uiout, "pids", pids);
2147
2148 ui_out_text (uiout, "\n");
2149 do_cleanups (chain);
2150 }
2151
2152 do_cleanups (back_to);
2153
2154 if (info_count == 0)
2155 ui_out_message (uiout, 0, _("No auto-loaded libthread-db.\n"));
2156 }
2157
2158 static void
2159 init_thread_db_ops (void)
2160 {
2161 thread_db_ops.to_shortname = "multi-thread";
2162 thread_db_ops.to_longname = "multi-threaded child process.";
2163 thread_db_ops.to_doc = "Threads and pthreads support.";
2164 thread_db_ops.to_detach = thread_db_detach;
2165 thread_db_ops.to_wait = thread_db_wait;
2166 thread_db_ops.to_resume = thread_db_resume;
2167 thread_db_ops.to_mourn_inferior = thread_db_mourn_inferior;
2168 thread_db_ops.to_update_thread_list = thread_db_update_thread_list;
2169 thread_db_ops.to_pid_to_str = thread_db_pid_to_str;
2170 thread_db_ops.to_stratum = thread_stratum;
2171 thread_db_ops.to_has_thread_control = tc_schedlock;
2172 thread_db_ops.to_get_thread_local_address
2173 = thread_db_get_thread_local_address;
2174 thread_db_ops.to_extra_thread_info = thread_db_extra_thread_info;
2175 thread_db_ops.to_get_ada_task_ptid = thread_db_get_ada_task_ptid;
2176 thread_db_ops.to_magic = OPS_MAGIC;
2177
2178 complete_target_initialization (&thread_db_ops);
2179 }
2180
2181 /* Provide a prototype to silence -Wmissing-prototypes. */
2182 extern initialize_file_ftype _initialize_thread_db;
2183
2184 void
2185 _initialize_thread_db (void)
2186 {
2187 init_thread_db_ops ();
2188
2189 /* Defer loading of libthread_db.so until inferior is running.
2190 This allows gdb to load correct libthread_db for a given
2191 executable -- there could be mutiple versions of glibc,
2192 compiled with LinuxThreads or NPTL, and until there is
2193 a running inferior, we can't tell which libthread_db is
2194 the correct one to load. */
2195
2196 libthread_db_search_path = xstrdup (LIBTHREAD_DB_SEARCH_PATH);
2197
2198 add_setshow_optional_filename_cmd ("libthread-db-search-path",
2199 class_support,
2200 &libthread_db_search_path, _("\
2201 Set search path for libthread_db."), _("\
2202 Show the current search path or libthread_db."), _("\
2203 This path is used to search for libthread_db to be loaded into \
2204 gdb itself.\n\
2205 Its value is a colon (':') separate list of directories to search.\n\
2206 Setting the search path to an empty list resets it to its default value."),
2207 set_libthread_db_search_path,
2208 NULL,
2209 &setlist, &showlist);
2210
2211 add_setshow_zuinteger_cmd ("libthread-db", class_maintenance,
2212 &libthread_db_debug, _("\
2213 Set libthread-db debugging."), _("\
2214 Show libthread-db debugging."), _("\
2215 When non-zero, libthread-db debugging is enabled."),
2216 NULL,
2217 show_libthread_db_debug,
2218 &setdebuglist, &showdebuglist);
2219
2220 add_setshow_boolean_cmd ("libthread-db", class_support,
2221 &auto_load_thread_db, _("\
2222 Enable or disable auto-loading of inferior specific libthread_db."), _("\
2223 Show whether auto-loading inferior specific libthread_db is enabled."), _("\
2224 If enabled, libthread_db will be searched in 'set libthread-db-search-path'\n\
2225 locations to load libthread_db compatible with the inferior.\n\
2226 Standard system libthread_db still gets loaded even with this option off.\n\
2227 This options has security implications for untrusted inferiors."),
2228 NULL, show_auto_load_thread_db,
2229 auto_load_set_cmdlist_get (),
2230 auto_load_show_cmdlist_get ());
2231
2232 add_cmd ("libthread-db", class_info, info_auto_load_libthread_db,
2233 _("Print the list of loaded inferior specific libthread_db.\n\
2234 Usage: info auto-load libthread-db"),
2235 auto_load_info_cmdlist_get ());
2236
2237 /* Add ourselves to objfile event chain. */
2238 observer_attach_new_objfile (thread_db_new_objfile);
2239
2240 /* Add ourselves to inferior_created event chain.
2241 This is needed to handle debugging statically linked programs where
2242 the new_objfile observer won't get called for libpthread. */
2243 observer_attach_inferior_created (thread_db_inferior_created);
2244 }