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