]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/gdbserver/thread-db.c
Remove ptid_get_lwp
[thirdparty/binutils-gdb.git] / gdb / gdbserver / thread-db.c
CommitLineData
0d62e5e8 1/* Thread management interface, for the remote server for GDB.
e2882c85 2 Copyright (C) 2002-2018 Free Software Foundation, Inc.
0d62e5e8
DJ
3
4 Contributed by MontaVista Software.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
0d62e5e8
DJ
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
0d62e5e8
DJ
20
21#include "server.h"
22
23#include "linux-low.h"
24
25extern int debug_threads;
26
0050a760 27#include "gdb_proc_service.h"
125f8a3d 28#include "nat/gdb_thread_db.h"
e6712ff1 29#include "gdb_vecs.h"
2db9a427 30#include "nat/linux-procfs.h"
94c207e0 31#include "common/scoped_restore.h"
0d62e5e8 32
96f15937 33#ifndef USE_LIBTHREAD_DB_DIRECTLY
cdbfd419 34#include <dlfcn.h>
96f15937 35#endif
cdbfd419
PP
36#include <limits.h>
37#include <ctype.h>
38
39struct thread_db
40{
41 /* Structure that identifies the child process for the
42 <proc_service.h> interface. */
43 struct ps_prochandle proc_handle;
44
45 /* Connection to the libthread_db library. */
46 td_thragent_t *thread_agent;
47
9836d6ea
PA
48 /* If this flag has been set, we've already asked GDB for all
49 symbols we might need; assume symbol cache misses are
50 failures. */
51 int all_symbols_looked_up;
52
96f15937 53#ifndef USE_LIBTHREAD_DB_DIRECTLY
cdbfd419
PP
54 /* Handle of the libthread_db from dlopen. */
55 void *handle;
96f15937 56#endif
cdbfd419
PP
57
58 /* Addresses of libthread_db functions. */
96e9210f 59 td_ta_new_ftype *td_ta_new_p;
96e9210f
PA
60 td_ta_map_lwp2thr_ftype *td_ta_map_lwp2thr_p;
61 td_thr_get_info_ftype *td_thr_get_info_p;
96e9210f
PA
62 td_ta_thr_iter_ftype *td_ta_thr_iter_p;
63 td_thr_tls_get_addr_ftype *td_thr_tls_get_addr_p;
64 td_thr_tlsbase_ftype *td_thr_tlsbase_p;
65 td_symbol_list_ftype *td_symbol_list_p;
cdbfd419
PP
66};
67
68static char *libthread_db_search_path;
186947f7 69
95954743 70static int find_one_thread (ptid_t);
0d62e5e8
DJ
71static int find_new_threads_callback (const td_thrhandle_t *th_p, void *data);
72
54363045 73static const char *
0d62e5e8
DJ
74thread_db_err_str (td_err_e err)
75{
76 static char buf[64];
77
78 switch (err)
79 {
80 case TD_OK:
81 return "generic 'call succeeded'";
82 case TD_ERR:
83 return "generic error";
84 case TD_NOTHR:
85 return "no thread to satisfy query";
86 case TD_NOSV:
87 return "no sync handle to satisfy query";
88 case TD_NOLWP:
89 return "no LWP to satisfy query";
90 case TD_BADPH:
91 return "invalid process handle";
92 case TD_BADTH:
93 return "invalid thread handle";
94 case TD_BADSH:
95 return "invalid synchronization handle";
96 case TD_BADTA:
97 return "invalid thread agent";
98 case TD_BADKEY:
99 return "invalid key";
100 case TD_NOMSG:
101 return "no event message for getmsg";
102 case TD_NOFPREGS:
103 return "FPU register set not available";
104 case TD_NOLIBTHREAD:
105 return "application not linked with libthread";
106 case TD_NOEVENT:
107 return "requested event is not supported";
108 case TD_NOCAPAB:
109 return "capability not available";
110 case TD_DBERR:
111 return "debugger service failed";
112 case TD_NOAPLIC:
113 return "operation not applicable to";
114 case TD_NOTSD:
115 return "no thread-specific data for this thread";
116 case TD_MALLOC:
117 return "malloc failed";
118 case TD_PARTIALREG:
119 return "only part of register set was written/read";
120 case TD_NOXREGS:
121 return "X register set not available for this thread";
3db0444b
DJ
122#ifdef HAVE_TD_VERSION
123 case TD_VERSION:
124 return "version mismatch between libthread_db and libpthread";
125#endif
0d62e5e8 126 default:
6cebaf6e 127 xsnprintf (buf, sizeof (buf), "unknown thread_db error '%d'", err);
0d62e5e8
DJ
128 return buf;
129 }
130}
131
132#if 0
133static char *
134thread_db_state_str (td_thr_state_e state)
135{
136 static char buf[64];
137
138 switch (state)
139 {
140 case TD_THR_STOPPED:
141 return "stopped by debugger";
142 case TD_THR_RUN:
143 return "runnable";
144 case TD_THR_ACTIVE:
145 return "active";
146 case TD_THR_ZOMBIE:
147 return "zombie";
148 case TD_THR_SLEEP:
149 return "sleeping";
150 case TD_THR_STOPPED_ASLEEP:
151 return "stopped by debugger AND blocked";
152 default:
6cebaf6e 153 xsnprintf (buf, sizeof (buf), "unknown thread_db state %d", state);
0d62e5e8
DJ
154 return buf;
155 }
156}
157#endif
158
94c207e0
PA
159/* Get thread info about PTID, accessing memory via the current
160 thread. */
161
ae13219e 162static int
95954743 163find_one_thread (ptid_t ptid)
0d62e5e8 164{
ae13219e
DJ
165 td_thrhandle_t th;
166 td_thrinfo_t ti;
0d62e5e8 167 td_err_e err;
54a0b537 168 struct lwp_info *lwp;
fe978cb0 169 struct thread_db *thread_db = current_process ()->priv->thread_db;
e38504b3 170 int lwpid = ptid.lwp ();
0d62e5e8 171
8dc7b443
SM
172 thread_info *thread = find_thread_ptid (ptid);
173 lwp = get_thread_lwp (thread);
54a0b537 174 if (lwp->thread_known)
ae13219e
DJ
175 return 1;
176
24a09b5f 177 /* Get information about this thread. */
cdbfd419 178 err = thread_db->td_ta_map_lwp2thr_p (thread_db->thread_agent, lwpid, &th);
ae13219e 179 if (err != TD_OK)
24a09b5f
DJ
180 error ("Cannot get thread handle for LWP %d: %s",
181 lwpid, thread_db_err_str (err));
ae13219e 182
cdbfd419 183 err = thread_db->td_thr_get_info_p (&th, &ti);
ae13219e 184 if (err != TD_OK)
24a09b5f
DJ
185 error ("Cannot get thread info for LWP %d: %s",
186 lwpid, thread_db_err_str (err));
ae13219e
DJ
187
188 if (debug_threads)
87ce2a04 189 debug_printf ("Found thread %ld (LWP %d)\n",
d41401ac 190 (unsigned long) ti.ti_tid, ti.ti_lid);
ae13219e 191
95954743 192 if (lwpid != ti.ti_lid)
24a09b5f
DJ
193 {
194 warning ("PID mismatch! Expected %ld, got %ld",
95954743 195 (long) lwpid, (long) ti.ti_lid);
24a09b5f
DJ
196 return 0;
197 }
ae13219e 198
24a09b5f
DJ
199 /* If the new thread ID is zero, a final thread ID will be available
200 later. Do not enable thread debugging yet. */
201 if (ti.ti_tid == 0)
202 return 0;
ae13219e 203
54a0b537
PA
204 lwp->thread_known = 1;
205 lwp->th = th;
f6327dcb 206 lwp->thread_handle = ti.ti_tid;
ae13219e 207
ae13219e
DJ
208 return 1;
209}
210
5f7d1694
PP
211/* Attach a thread. Return true on success. */
212
213static int
214attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p)
ae13219e 215{
7ae1a6a6
PA
216 struct process_info *proc = current_process ();
217 int pid = pid_of (proc);
fd79271b 218 ptid_t ptid = ptid_t (pid, ti_p->ti_lid, 0);
54a0b537 219 struct lwp_info *lwp;
7ae1a6a6 220 int err;
ae13219e 221
0d62e5e8 222 if (debug_threads)
87ce2a04 223 debug_printf ("Attaching to thread %ld (LWP %d)\n",
d41401ac 224 (unsigned long) ti_p->ti_tid, ti_p->ti_lid);
7ae1a6a6
PA
225 err = linux_attach_lwp (ptid);
226 if (err != 0)
0d62e5e8 227 {
4d9b86e1
SM
228 std::string reason = linux_ptrace_attach_fail_reason_string (ptid, err);
229
7ae1a6a6 230 warning ("Could not attach to thread %ld (LWP %d): %s\n",
4d9b86e1
SM
231 (unsigned long) ti_p->ti_tid, ti_p->ti_lid, reason.c_str ());
232
5f7d1694 233 return 0;
0d62e5e8
DJ
234 }
235
7ae1a6a6
PA
236 lwp = find_lwp_pid (ptid);
237 gdb_assert (lwp != NULL);
54a0b537
PA
238 lwp->thread_known = 1;
239 lwp->th = *th_p;
f6327dcb 240 lwp->thread_handle = ti_p->ti_tid;
24a09b5f 241
5f7d1694
PP
242 return 1;
243}
244
245/* Attach thread if we haven't seen it yet.
246 Increment *COUNTER if we have attached a new thread.
247 Return false on failure. */
248
249static int
250maybe_attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p,
251 int *counter)
252{
253 struct lwp_info *lwp;
254
f2907e49 255 lwp = find_lwp_pid (ptid_t (ti_p->ti_lid));
5f7d1694
PP
256 if (lwp != NULL)
257 return 1;
258
259 if (!attach_thread (th_p, ti_p))
260 return 0;
261
262 if (counter != NULL)
263 *counter += 1;
264
265 return 1;
0d62e5e8
DJ
266}
267
268static int
269find_new_threads_callback (const td_thrhandle_t *th_p, void *data)
270{
271 td_thrinfo_t ti;
272 td_err_e err;
fe978cb0 273 struct thread_db *thread_db = current_process ()->priv->thread_db;
0d62e5e8 274
cdbfd419 275 err = thread_db->td_thr_get_info_p (th_p, &ti);
0d62e5e8
DJ
276 if (err != TD_OK)
277 error ("Cannot get thread info: %s", thread_db_err_str (err));
278
a33e3959
PA
279 if (ti.ti_lid == -1)
280 {
281 /* A thread with kernel thread ID -1 is either a thread that
282 exited and was joined, or a thread that is being created but
283 hasn't started yet, and that is reusing the tcb/stack of a
284 thread that previously exited and was joined. (glibc marks
285 terminated and joined threads with kernel thread ID -1. See
286 glibc PR17707. */
d6c146e9
PA
287 if (debug_threads)
288 debug_printf ("thread_db: skipping exited and "
d41401ac
DE
289 "joined thread (0x%lx)\n",
290 (unsigned long) ti.ti_tid);
a33e3959
PA
291 return 0;
292 }
293
0d62e5e8
DJ
294 /* Check for zombies. */
295 if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
296 return 0;
297
5f7d1694
PP
298 if (!maybe_attach_thread (th_p, &ti, (int *) data))
299 {
300 /* Terminate iteration early: we might be looking at stale data in
301 the inferior. The thread_db_find_new_threads will retry. */
302 return 1;
303 }
0d62e5e8
DJ
304
305 return 0;
306}
307
308static void
309thread_db_find_new_threads (void)
310{
311 td_err_e err;
fbd5db48 312 ptid_t ptid = current_ptid;
fe978cb0 313 struct thread_db *thread_db = current_process ()->priv->thread_db;
5f7d1694 314 int loop, iteration;
0d62e5e8 315
ae13219e
DJ
316 /* This function is only called when we first initialize thread_db.
317 First locate the initial thread. If it is not ready for
318 debugging yet, then stop. */
95954743 319 if (find_one_thread (ptid) == 0)
ae13219e
DJ
320 return;
321
5f7d1694
PP
322 /* Require 4 successive iterations which do not find any new threads.
323 The 4 is a heuristic: there is an inherent race here, and I have
324 seen that 2 iterations in a row are not always sufficient to
325 "capture" all threads. */
326 for (loop = 0, iteration = 0; loop < 4; ++loop, ++iteration)
327 {
328 int new_thread_count = 0;
329
330 /* Iterate over all user-space threads to discover new threads. */
331 err = thread_db->td_ta_thr_iter_p (thread_db->thread_agent,
332 find_new_threads_callback,
333 &new_thread_count,
493e2a69
MS
334 TD_THR_ANY_STATE,
335 TD_THR_LOWEST_PRIORITY,
5f7d1694
PP
336 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
337 if (debug_threads)
87ce2a04
DE
338 debug_printf ("Found %d threads in iteration %d.\n",
339 new_thread_count, iteration);
5f7d1694
PP
340
341 if (new_thread_count != 0)
342 {
343 /* Found new threads. Restart iteration from beginning. */
344 loop = -1;
345 }
346 }
0d62e5e8
DJ
347 if (err != TD_OK)
348 error ("Cannot find new threads: %s", thread_db_err_str (err));
349}
350
fd500816
DJ
351/* Cache all future symbols that thread_db might request. We can not
352 request symbols at arbitrary states in the remote protocol, only
353 when the client tells us that new symbols are available. So when
354 we load the thread library, make sure to check the entire list. */
355
356static void
357thread_db_look_up_symbols (void)
358{
fe978cb0 359 struct thread_db *thread_db = current_process ()->priv->thread_db;
cdbfd419 360 const char **sym_list;
fd500816
DJ
361 CORE_ADDR unused;
362
cdbfd419 363 for (sym_list = thread_db->td_symbol_list_p (); *sym_list; sym_list++)
9836d6ea
PA
364 look_up_one_symbol (*sym_list, &unused, 1);
365
366 /* We're not interested in any other libraries loaded after this
367 point, only in symbols in libpthread.so. */
368 thread_db->all_symbols_looked_up = 1;
369}
370
371int
372thread_db_look_up_one_symbol (const char *name, CORE_ADDR *addrp)
373{
fe978cb0 374 struct thread_db *thread_db = current_process ()->priv->thread_db;
9836d6ea
PA
375 int may_ask_gdb = !thread_db->all_symbols_looked_up;
376
377 /* If we've passed the call to thread_db_look_up_symbols, then
378 anything not in the cache must not exist; we're not interested
379 in any libraries loaded after that point, only in symbols in
380 libpthread.so. It might not be an appropriate time to look
381 up a symbol, e.g. while we're trying to fetch registers. */
382 return look_up_one_symbol (name, addrp, may_ask_gdb);
fd500816
DJ
383}
384
dae5f5cf
DJ
385int
386thread_db_get_tls_address (struct thread_info *thread, CORE_ADDR offset,
387 CORE_ADDR load_module, CORE_ADDR *address)
388{
dae5f5cf
DJ
389 psaddr_t addr;
390 td_err_e err;
54a0b537 391 struct lwp_info *lwp;
0bfdf32f 392 struct thread_info *saved_thread;
cdbfd419
PP
393 struct process_info *proc;
394 struct thread_db *thread_db;
395
396 proc = get_thread_process (thread);
fe978cb0 397 thread_db = proc->priv->thread_db;
dae5f5cf 398
7fe519cb 399 /* If the thread layer is not (yet) initialized, fail. */
8a4ac37e 400 if (thread_db == NULL || !thread_db->all_symbols_looked_up)
7fe519cb
UW
401 return TD_ERR;
402
5876f503
JK
403 /* If td_thr_tls_get_addr is missing rather do not expect td_thr_tlsbase
404 could work. */
405 if (thread_db->td_thr_tls_get_addr_p == NULL
406 || (load_module == 0 && thread_db->td_thr_tlsbase_p == NULL))
cdbfd419
PP
407 return -1;
408
54a0b537
PA
409 lwp = get_thread_lwp (thread);
410 if (!lwp->thread_known)
9c80ecd6 411 find_one_thread (thread->id);
54a0b537 412 if (!lwp->thread_known)
dae5f5cf
DJ
413 return TD_NOTHR;
414
0bfdf32f
GB
415 saved_thread = current_thread;
416 current_thread = thread;
5876f503
JK
417
418 if (load_module != 0)
419 {
420 /* Note the cast through uintptr_t: this interface only works if
421 a target address fits in a psaddr_t, which is a host pointer.
422 So a 32-bit debugger can not access 64-bit TLS through this. */
423 err = thread_db->td_thr_tls_get_addr_p (&lwp->th,
424 (psaddr_t) (uintptr_t) load_module,
425 offset, &addr);
426 }
427 else
428 {
429 /* This code path handles the case of -static -pthread executables:
430 https://sourceware.org/ml/libc-help/2014-03/msg00024.html
431 For older GNU libc r_debug.r_map is NULL. For GNU libc after
432 PR libc/16831 due to GDB PR threads/16954 LOAD_MODULE is also NULL.
433 The constant number 1 depends on GNU __libc_setup_tls
434 initialization of l_tls_modid to 1. */
435 err = thread_db->td_thr_tlsbase_p (&lwp->th, 1, &addr);
436 addr = (char *) addr + offset;
437 }
438
0bfdf32f 439 current_thread = saved_thread;
dae5f5cf
DJ
440 if (err == TD_OK)
441 {
186947f7 442 *address = (CORE_ADDR) (uintptr_t) addr;
dae5f5cf
DJ
443 return 0;
444 }
445 else
446 return err;
cdbfd419
PP
447}
448
f6327dcb
KB
449/* See linux-low.h. */
450
451bool
452thread_db_thread_handle (ptid_t ptid, gdb_byte **handle, int *handle_len)
453{
454 struct thread_db *thread_db;
455 struct lwp_info *lwp;
8dc7b443 456 thread_info *thread = find_thread_ptid (ptid);
f6327dcb
KB
457
458 if (thread == NULL)
459 return false;
460
461 thread_db = get_thread_process (thread)->priv->thread_db;
462
463 if (thread_db == NULL)
464 return false;
465
466 lwp = get_thread_lwp (thread);
467
9c80ecd6 468 if (!lwp->thread_known && !find_one_thread (thread->id))
f6327dcb
KB
469 return false;
470
471 gdb_assert (lwp->thread_known);
472
473 *handle = (gdb_byte *) &lwp->thread_handle;
474 *handle_len = sizeof (lwp->thread_handle);
475 return true;
476}
477
96f15937
PP
478#ifdef USE_LIBTHREAD_DB_DIRECTLY
479
480static int
481thread_db_load_search (void)
482{
483 td_err_e err;
9836d6ea 484 struct thread_db *tdb;
96f15937
PP
485 struct process_info *proc = current_process ();
486
fe978cb0 487 gdb_assert (proc->priv->thread_db == NULL);
96f15937 488
8d749320 489 tdb = XCNEW (struct thread_db);
fe978cb0 490 proc->priv->thread_db = tdb;
f9e39928 491
9836d6ea 492 tdb->td_ta_new_p = &td_ta_new;
96f15937
PP
493
494 /* Attempt to open a connection to the thread library. */
9836d6ea 495 err = tdb->td_ta_new_p (&tdb->proc_handle, &tdb->thread_agent);
96f15937
PP
496 if (err != TD_OK)
497 {
498 if (debug_threads)
87ce2a04 499 debug_printf ("td_ta_new(): %s\n", thread_db_err_str (err));
9836d6ea 500 free (tdb);
fe978cb0 501 proc->priv->thread_db = NULL;
96f15937
PP
502 return 0;
503 }
504
9836d6ea
PA
505 tdb->td_ta_map_lwp2thr_p = &td_ta_map_lwp2thr;
506 tdb->td_thr_get_info_p = &td_thr_get_info;
507 tdb->td_ta_thr_iter_p = &td_ta_thr_iter;
508 tdb->td_symbol_list_p = &td_symbol_list;
96f15937 509
96f15937 510 /* These are not essential. */
9836d6ea 511 tdb->td_thr_tls_get_addr_p = &td_thr_tls_get_addr;
5876f503 512 tdb->td_thr_tlsbase_p = &td_thr_tlsbase;
96f15937
PP
513
514 return 1;
515}
516
517#else
518
cdbfd419
PP
519static int
520try_thread_db_load_1 (void *handle)
521{
522 td_err_e err;
9836d6ea 523 struct thread_db *tdb;
cdbfd419
PP
524 struct process_info *proc = current_process ();
525
fe978cb0 526 gdb_assert (proc->priv->thread_db == NULL);
cdbfd419 527
8d749320 528 tdb = XCNEW (struct thread_db);
fe978cb0 529 proc->priv->thread_db = tdb;
f9e39928 530
9836d6ea 531 tdb->handle = handle;
cdbfd419
PP
532
533 /* Initialize pointers to the dynamic library functions we will use.
534 Essential functions first. */
535
536#define CHK(required, a) \
537 do \
538 { \
539 if ((a) == NULL) \
540 { \
541 if (debug_threads) \
87ce2a04 542 debug_printf ("dlsym: %s\n", dlerror ()); \
cdbfd419 543 if (required) \
9836d6ea
PA
544 { \
545 free (tdb); \
fe978cb0 546 proc->priv->thread_db = NULL; \
9836d6ea
PA
547 return 0; \
548 } \
cdbfd419
PP
549 } \
550 } \
551 while (0)
552
96e9210f
PA
553#define TDB_DLSYM(tdb, func) \
554 tdb->func ## _p = (func ## _ftype *) dlsym (tdb->handle, #func)
555
556 CHK (1, TDB_DLSYM (tdb, td_ta_new));
cdbfd419
PP
557
558 /* Attempt to open a connection to the thread library. */
9836d6ea 559 err = tdb->td_ta_new_p (&tdb->proc_handle, &tdb->thread_agent);
cdbfd419
PP
560 if (err != TD_OK)
561 {
562 if (debug_threads)
87ce2a04 563 debug_printf ("td_ta_new(): %s\n", thread_db_err_str (err));
9836d6ea 564 free (tdb);
fe978cb0 565 proc->priv->thread_db = NULL;
cdbfd419
PP
566 return 0;
567 }
568
96e9210f
PA
569 CHK (1, TDB_DLSYM (tdb, td_ta_map_lwp2thr));
570 CHK (1, TDB_DLSYM (tdb, td_thr_get_info));
571 CHK (1, TDB_DLSYM (tdb, td_ta_thr_iter));
572 CHK (1, TDB_DLSYM (tdb, td_symbol_list));
cdbfd419 573
cdbfd419 574 /* These are not essential. */
96e9210f
PA
575 CHK (0, TDB_DLSYM (tdb, td_thr_tls_get_addr));
576 CHK (0, TDB_DLSYM (tdb, td_thr_tlsbase));
cdbfd419
PP
577
578#undef CHK
96e9210f 579#undef TDB_DLSYM
cdbfd419 580
cdbfd419
PP
581 return 1;
582}
583
10e86dd7
DE
584#ifdef HAVE_DLADDR
585
cdbfd419
PP
586/* Lookup a library in which given symbol resides.
587 Note: this is looking in the GDBSERVER process, not in the inferior.
588 Returns library name, or NULL. */
589
590static const char *
591dladdr_to_soname (const void *addr)
592{
593 Dl_info info;
594
595 if (dladdr (addr, &info) != 0)
596 return info.dli_fname;
597 return NULL;
598}
599
10e86dd7
DE
600#endif
601
cdbfd419
PP
602static int
603try_thread_db_load (const char *library)
604{
605 void *handle;
606
607 if (debug_threads)
87ce2a04
DE
608 debug_printf ("Trying host libthread_db library: %s.\n",
609 library);
cdbfd419
PP
610 handle = dlopen (library, RTLD_NOW);
611 if (handle == NULL)
612 {
613 if (debug_threads)
87ce2a04 614 debug_printf ("dlopen failed: %s.\n", dlerror ());
cdbfd419
PP
615 return 0;
616 }
617
10e86dd7 618#ifdef HAVE_DLADDR
cdbfd419
PP
619 if (debug_threads && strchr (library, '/') == NULL)
620 {
621 void *td_init;
622
623 td_init = dlsym (handle, "td_init");
624 if (td_init != NULL)
625 {
626 const char *const libpath = dladdr_to_soname (td_init);
627
628 if (libpath != NULL)
4eefa7bc 629 debug_printf ("Host %s resolved to: %s.\n", library, libpath);
cdbfd419
PP
630 }
631 }
10e86dd7 632#endif
cdbfd419
PP
633
634 if (try_thread_db_load_1 (handle))
635 return 1;
636
637 /* This library "refused" to work on current inferior. */
638 dlclose (handle);
639 return 0;
640}
641
98a5dd13
DE
642/* Handle $sdir in libthread-db-search-path.
643 Look for libthread_db in the system dirs, or wherever a plain
644 dlopen(file_without_path) will look.
645 The result is true for success. */
646
cdbfd419 647static int
98a5dd13
DE
648try_thread_db_load_from_sdir (void)
649{
650 return try_thread_db_load (LIBTHREAD_DB_SO);
651}
652
653/* Try to load libthread_db from directory DIR of length DIR_LEN.
654 The result is true for success. */
655
656static int
657try_thread_db_load_from_dir (const char *dir, size_t dir_len)
cdbfd419
PP
658{
659 char path[PATH_MAX];
98a5dd13
DE
660
661 if (dir_len + 1 + strlen (LIBTHREAD_DB_SO) + 1 > sizeof (path))
662 {
224c3ddb 663 char *cp = (char *) xmalloc (dir_len + 1);
98a5dd13
DE
664
665 memcpy (cp, dir, dir_len);
666 cp[dir_len] = '\0';
667 warning (_("libthread-db-search-path component too long,"
668 " ignored: %s."), cp);
669 free (cp);
670 return 0;
671 }
672
673 memcpy (path, dir, dir_len);
674 path[dir_len] = '/';
675 strcpy (path + dir_len + 1, LIBTHREAD_DB_SO);
676 return try_thread_db_load (path);
677}
678
679/* Search libthread_db_search_path for libthread_db which "agrees"
680 to work on current inferior.
681 The result is true for success. */
682
683static int
684thread_db_load_search (void)
685{
e80aaf61 686 int rc = 0;
cdbfd419
PP
687
688 if (libthread_db_search_path == NULL)
689 libthread_db_search_path = xstrdup (LIBTHREAD_DB_SEARCH_PATH);
690
e80aaf61
SM
691 std::vector<gdb::unique_xmalloc_ptr<char>> dir_vec
692 = dirnames_to_char_ptr_vec (libthread_db_search_path);
e6712ff1 693
e80aaf61 694 for (const gdb::unique_xmalloc_ptr<char> &this_dir_up : dir_vec)
cdbfd419 695 {
e80aaf61 696 char *this_dir = this_dir_up.get ();
e6712ff1 697 const int pdir_len = sizeof ("$pdir") - 1;
98a5dd13
DE
698 size_t this_dir_len;
699
e6712ff1 700 this_dir_len = strlen (this_dir);
cdbfd419 701
e6712ff1
DE
702 if (strncmp (this_dir, "$pdir", pdir_len) == 0
703 && (this_dir[pdir_len] == '\0'
704 || this_dir[pdir_len] == '/'))
98a5dd13
DE
705 {
706 /* We don't maintain a list of loaded libraries so we don't know
707 where libpthread lives. We *could* fetch the info, but we don't
708 do that yet. Ignore it. */
709 }
e6712ff1 710 else if (strcmp (this_dir, "$sdir") == 0)
98a5dd13
DE
711 {
712 if (try_thread_db_load_from_sdir ())
cdbfd419 713 {
98a5dd13 714 rc = 1;
cdbfd419
PP
715 break;
716 }
cdbfd419 717 }
98a5dd13 718 else
cdbfd419 719 {
98a5dd13
DE
720 if (try_thread_db_load_from_dir (this_dir, this_dir_len))
721 {
722 rc = 1;
723 break;
724 }
cdbfd419
PP
725 }
726 }
cdbfd419
PP
727
728 if (debug_threads)
87ce2a04 729 debug_printf ("thread_db_load_search returning %d\n", rc);
cdbfd419 730 return rc;
dae5f5cf
DJ
731}
732
96f15937
PP
733#endif /* USE_LIBTHREAD_DB_DIRECTLY */
734
0d62e5e8 735int
9b4c5f87 736thread_db_init (void)
0d62e5e8 737{
95954743 738 struct process_info *proc = current_process ();
0d62e5e8 739
fd500816
DJ
740 /* FIXME drow/2004-10-16: This is the "overall process ID", which
741 GNU/Linux calls tgid, "thread group ID". When we support
742 attaching to threads, the original thread may not be the correct
743 thread. We would have to get the process ID from /proc for NPTL.
fd500816
DJ
744
745 This isn't the only place in gdbserver that assumes that the first
746 process in the list is the thread group leader. */
ea025f5f 747
cdbfd419 748 if (thread_db_load_search ())
0d62e5e8 749 {
2db9a427
PA
750 /* It's best to avoid td_ta_thr_iter if possible. That walks
751 data structures in the inferior's address space that may be
752 corrupted, or, if the target is running, the list may change
753 while we walk it. In the latter case, it's possible that a
754 thread exits just at the exact time that causes GDBserver to
9b4c5f87
AT
755 get stuck in an infinite loop. As the kernel supports clone
756 events and /proc/PID/task/ exists, then we already know about
2db9a427
PA
757 all threads in the process. When we need info out of
758 thread_db on a given thread (e.g., for TLS), we'll use
759 find_one_thread then. That uses thread_db entry points that
760 do not walk libpthread's thread list, so should be safe, as
761 well as more efficient. */
9b4c5f87 762 if (!linux_proc_task_list_dir_exists (pid_of (proc)))
2db9a427 763 thread_db_find_new_threads ();
fd500816 764 thread_db_look_up_symbols ();
0d62e5e8 765 return 1;
cdbfd419 766 }
0d62e5e8 767
cdbfd419
PP
768 return 0;
769}
770
f9e39928
PA
771static void
772switch_to_process (struct process_info *proc)
773{
774 int pid = pid_of (proc);
775
785922a5 776 current_thread = find_any_thread_of_pid (pid);
f9e39928
PA
777}
778
cdbfd419
PP
779/* Disconnect from libthread_db and free resources. */
780
8336d594
PA
781static void
782disable_thread_event_reporting (struct process_info *proc)
cdbfd419 783{
fe978cb0 784 struct thread_db *thread_db = proc->priv->thread_db;
cdbfd419
PP
785 if (thread_db)
786 {
21e1bee4
PP
787 td_err_e (*td_ta_clear_event_p) (const td_thragent_t *ta,
788 td_thr_events_t *event);
789
fd7dd3e6 790#ifndef USE_LIBTHREAD_DB_DIRECTLY
96e9210f
PA
791 td_ta_clear_event_p
792 = (td_ta_clear_event_ftype *) dlsym (thread_db->handle,
793 "td_ta_clear_event");
fd7dd3e6 794#else
fd7dd3e6
PA
795 td_ta_clear_event_p = &td_ta_clear_event;
796#endif
797
8336d594 798 if (td_ta_clear_event_p != NULL)
21e1bee4 799 {
0bfdf32f 800 struct thread_info *saved_thread = current_thread;
21e1bee4 801 td_thr_events_t events;
8336d594 802
f9e39928 803 switch_to_process (proc);
21e1bee4 804
fd7dd3e6
PA
805 /* Set the process wide mask saying we aren't interested
806 in any events anymore. */
21e1bee4
PP
807 td_event_fillset (&events);
808 (*td_ta_clear_event_p) (thread_db->thread_agent, &events);
8336d594 809
0bfdf32f 810 current_thread = saved_thread;
21e1bee4 811 }
8336d594
PA
812 }
813}
814
815void
816thread_db_detach (struct process_info *proc)
817{
fe978cb0 818 struct thread_db *thread_db = proc->priv->thread_db;
f9e39928
PA
819
820 if (thread_db)
821 {
822 disable_thread_event_reporting (proc);
f9e39928 823 }
8336d594
PA
824}
825
826/* Disconnect from libthread_db and free resources. */
827
828void
829thread_db_mourn (struct process_info *proc)
830{
fe978cb0 831 struct thread_db *thread_db = proc->priv->thread_db;
8336d594
PA
832 if (thread_db)
833 {
96e9210f 834 td_ta_delete_ftype *td_ta_delete_p;
8336d594
PA
835
836#ifndef USE_LIBTHREAD_DB_DIRECTLY
96e9210f 837 td_ta_delete_p = (td_ta_delete_ftype *) dlsym (thread_db->handle, "td_ta_delete");
8336d594
PA
838#else
839 td_ta_delete_p = &td_ta_delete;
840#endif
cdbfd419 841
cdbfd419
PP
842 if (td_ta_delete_p != NULL)
843 (*td_ta_delete_p) (thread_db->thread_agent);
844
fd7dd3e6 845#ifndef USE_LIBTHREAD_DB_DIRECTLY
cdbfd419 846 dlclose (thread_db->handle);
96f15937
PP
847#endif /* USE_LIBTHREAD_DB_DIRECTLY */
848
cdbfd419 849 free (thread_db);
fe978cb0 850 proc->priv->thread_db = NULL;
cdbfd419
PP
851 }
852}
853
854/* Handle "set libthread-db-search-path" monitor command and return 1.
855 For any other command, return 0. */
856
857int
858thread_db_handle_monitor_command (char *mon)
859{
84e578fb
DE
860 const char *cmd = "set libthread-db-search-path";
861 size_t cmd_len = strlen (cmd);
862
863 if (strncmp (mon, cmd, cmd_len) == 0
864 && (mon[cmd_len] == '\0'
865 || mon[cmd_len] == ' '))
cdbfd419 866 {
84e578fb 867 const char *cp = mon + cmd_len;
cdbfd419
PP
868
869 if (libthread_db_search_path != NULL)
870 free (libthread_db_search_path);
871
872 /* Skip leading space (if any). */
873 while (isspace (*cp))
874 ++cp;
875
84e578fb
DE
876 if (*cp == '\0')
877 cp = LIBTHREAD_DB_SEARCH_PATH;
cdbfd419
PP
878 libthread_db_search_path = xstrdup (cp);
879
880 monitor_output ("libthread-db-search-path set to `");
881 monitor_output (libthread_db_search_path);
882 monitor_output ("'\n");
883 return 1;
0d62e5e8
DJ
884 }
885
cdbfd419 886 /* Tell server.c to perform default processing. */
0d62e5e8
DJ
887 return 0;
888}
86299109
KB
889
890/* See linux-low.h. */
891
892void
94c207e0 893thread_db_notice_clone (struct thread_info *parent_thr, ptid_t child_ptid)
86299109 894{
94c207e0
PA
895 process_info *parent_proc = get_thread_process (parent_thr);
896 struct thread_db *thread_db = parent_proc->priv->thread_db;
86299109
KB
897
898 /* If the thread layer isn't initialized, return. It may just
899 be that the program uses clone, but does not use libthread_db. */
900 if (thread_db == NULL || !thread_db->all_symbols_looked_up)
901 return;
902
94c207e0
PA
903 /* find_one_thread calls into libthread_db which accesses memory via
904 the current thread. Temporarily switch to a thread we know is
905 stopped. */
906 scoped_restore restore_current_thread
907 = make_scoped_restore (&current_thread, parent_thr);
908
909 if (!find_one_thread (child_ptid))
86299109
KB
910 warning ("Cannot find thread after clone.\n");
911}