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