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