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