Shadow SIM's debug_printf function
[deliverable/binutils-gdb.git] / gdb / gdbserver / thread-db.c
CommitLineData
0d62e5e8 1/* Thread management interface, for the remote server for GDB.
ecd75fc8 2 Copyright (C) 2002-2014 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
196 if (thread_db->td_ta_event_getmsg_p == NULL)
197 fatal ("unexpected 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. */
54a0b537
PA
214 lwp = get_thread_lwp (current_inferior);
215 if (lwp->thread_known == 0)
80894984 216 find_one_thread (current_inferior->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;
cdbfd419
PP
231 struct thread_db *thread_db = current_process ()->private->thread_db;
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;
cdbfd419 275 struct thread_db *thread_db = current_process ()->private->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,
343 linux_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;
7ae1a6a6 355 struct thread_db *thread_db = proc->private->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;
cdbfd419 394 struct thread_db *thread_db = current_process ()->private->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
400 /* Check for zombies. */
401 if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
402 return 0;
403
5f7d1694
PP
404 if (!maybe_attach_thread (th_p, &ti, (int *) data))
405 {
406 /* Terminate iteration early: we might be looking at stale data in
407 the inferior. The thread_db_find_new_threads will retry. */
408 return 1;
409 }
0d62e5e8
DJ
410
411 return 0;
412}
413
414static void
415thread_db_find_new_threads (void)
416{
417 td_err_e err;
fbd5db48 418 ptid_t ptid = current_ptid;
cdbfd419 419 struct thread_db *thread_db = current_process ()->private->thread_db;
5f7d1694 420 int loop, iteration;
0d62e5e8 421
ae13219e
DJ
422 /* This function is only called when we first initialize thread_db.
423 First locate the initial thread. If it is not ready for
424 debugging yet, then stop. */
95954743 425 if (find_one_thread (ptid) == 0)
ae13219e
DJ
426 return;
427
5f7d1694
PP
428 /* Require 4 successive iterations which do not find any new threads.
429 The 4 is a heuristic: there is an inherent race here, and I have
430 seen that 2 iterations in a row are not always sufficient to
431 "capture" all threads. */
432 for (loop = 0, iteration = 0; loop < 4; ++loop, ++iteration)
433 {
434 int new_thread_count = 0;
435
436 /* Iterate over all user-space threads to discover new threads. */
437 err = thread_db->td_ta_thr_iter_p (thread_db->thread_agent,
438 find_new_threads_callback,
439 &new_thread_count,
493e2a69
MS
440 TD_THR_ANY_STATE,
441 TD_THR_LOWEST_PRIORITY,
5f7d1694
PP
442 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
443 if (debug_threads)
87ce2a04
DE
444 debug_printf ("Found %d threads in iteration %d.\n",
445 new_thread_count, iteration);
5f7d1694
PP
446
447 if (new_thread_count != 0)
448 {
449 /* Found new threads. Restart iteration from beginning. */
450 loop = -1;
451 }
452 }
0d62e5e8
DJ
453 if (err != TD_OK)
454 error ("Cannot find new threads: %s", thread_db_err_str (err));
455}
456
fd500816
DJ
457/* Cache all future symbols that thread_db might request. We can not
458 request symbols at arbitrary states in the remote protocol, only
459 when the client tells us that new symbols are available. So when
460 we load the thread library, make sure to check the entire list. */
461
462static void
463thread_db_look_up_symbols (void)
464{
cdbfd419
PP
465 struct thread_db *thread_db = current_process ()->private->thread_db;
466 const char **sym_list;
fd500816
DJ
467 CORE_ADDR unused;
468
cdbfd419 469 for (sym_list = thread_db->td_symbol_list_p (); *sym_list; sym_list++)
9836d6ea
PA
470 look_up_one_symbol (*sym_list, &unused, 1);
471
472 /* We're not interested in any other libraries loaded after this
473 point, only in symbols in libpthread.so. */
474 thread_db->all_symbols_looked_up = 1;
475}
476
477int
478thread_db_look_up_one_symbol (const char *name, CORE_ADDR *addrp)
479{
480 struct thread_db *thread_db = current_process ()->private->thread_db;
481 int may_ask_gdb = !thread_db->all_symbols_looked_up;
482
483 /* If we've passed the call to thread_db_look_up_symbols, then
484 anything not in the cache must not exist; we're not interested
485 in any libraries loaded after that point, only in symbols in
486 libpthread.so. It might not be an appropriate time to look
487 up a symbol, e.g. while we're trying to fetch registers. */
488 return look_up_one_symbol (name, addrp, may_ask_gdb);
fd500816
DJ
489}
490
dae5f5cf
DJ
491int
492thread_db_get_tls_address (struct thread_info *thread, CORE_ADDR offset,
493 CORE_ADDR load_module, CORE_ADDR *address)
494{
dae5f5cf
DJ
495 psaddr_t addr;
496 td_err_e err;
54a0b537 497 struct lwp_info *lwp;
95954743 498 struct thread_info *saved_inferior;
cdbfd419
PP
499 struct process_info *proc;
500 struct thread_db *thread_db;
501
502 proc = get_thread_process (thread);
503 thread_db = proc->private->thread_db;
dae5f5cf 504
7fe519cb 505 /* If the thread layer is not (yet) initialized, fail. */
8a4ac37e 506 if (thread_db == NULL || !thread_db->all_symbols_looked_up)
7fe519cb
UW
507 return TD_ERR;
508
5876f503
JK
509 /* If td_thr_tls_get_addr is missing rather do not expect td_thr_tlsbase
510 could work. */
511 if (thread_db->td_thr_tls_get_addr_p == NULL
512 || (load_module == 0 && thread_db->td_thr_tlsbase_p == NULL))
cdbfd419
PP
513 return -1;
514
54a0b537
PA
515 lwp = get_thread_lwp (thread);
516 if (!lwp->thread_known)
80894984 517 find_one_thread (thread->entry.id);
54a0b537 518 if (!lwp->thread_known)
dae5f5cf
DJ
519 return TD_NOTHR;
520
95954743
PA
521 saved_inferior = current_inferior;
522 current_inferior = thread;
5876f503
JK
523
524 if (load_module != 0)
525 {
526 /* Note the cast through uintptr_t: this interface only works if
527 a target address fits in a psaddr_t, which is a host pointer.
528 So a 32-bit debugger can not access 64-bit TLS through this. */
529 err = thread_db->td_thr_tls_get_addr_p (&lwp->th,
530 (psaddr_t) (uintptr_t) load_module,
531 offset, &addr);
532 }
533 else
534 {
535 /* This code path handles the case of -static -pthread executables:
536 https://sourceware.org/ml/libc-help/2014-03/msg00024.html
537 For older GNU libc r_debug.r_map is NULL. For GNU libc after
538 PR libc/16831 due to GDB PR threads/16954 LOAD_MODULE is also NULL.
539 The constant number 1 depends on GNU __libc_setup_tls
540 initialization of l_tls_modid to 1. */
541 err = thread_db->td_thr_tlsbase_p (&lwp->th, 1, &addr);
542 addr = (char *) addr + offset;
543 }
544
95954743 545 current_inferior = saved_inferior;
dae5f5cf
DJ
546 if (err == TD_OK)
547 {
186947f7 548 *address = (CORE_ADDR) (uintptr_t) addr;
dae5f5cf
DJ
549 return 0;
550 }
551 else
552 return err;
cdbfd419
PP
553}
554
96f15937
PP
555#ifdef USE_LIBTHREAD_DB_DIRECTLY
556
557static int
558thread_db_load_search (void)
559{
560 td_err_e err;
9836d6ea 561 struct thread_db *tdb;
96f15937
PP
562 struct process_info *proc = current_process ();
563
564 if (proc->private->thread_db != NULL)
565 fatal ("unexpected: proc->private->thread_db != NULL");
566
9836d6ea
PA
567 tdb = xcalloc (1, sizeof (*tdb));
568 proc->private->thread_db = tdb;
f9e39928 569
9836d6ea 570 tdb->td_ta_new_p = &td_ta_new;
96f15937
PP
571
572 /* Attempt to open a connection to the thread library. */
9836d6ea 573 err = tdb->td_ta_new_p (&tdb->proc_handle, &tdb->thread_agent);
96f15937
PP
574 if (err != TD_OK)
575 {
576 if (debug_threads)
87ce2a04 577 debug_printf ("td_ta_new(): %s\n", thread_db_err_str (err));
9836d6ea
PA
578 free (tdb);
579 proc->private->thread_db = NULL;
96f15937
PP
580 return 0;
581 }
582
9836d6ea
PA
583 tdb->td_ta_map_lwp2thr_p = &td_ta_map_lwp2thr;
584 tdb->td_thr_get_info_p = &td_thr_get_info;
585 tdb->td_ta_thr_iter_p = &td_ta_thr_iter;
586 tdb->td_symbol_list_p = &td_symbol_list;
96f15937
PP
587
588 /* This is required only when thread_db_use_events is on. */
9836d6ea 589 tdb->td_thr_event_enable_p = &td_thr_event_enable;
96f15937
PP
590
591 /* These are not essential. */
9836d6ea
PA
592 tdb->td_ta_event_addr_p = &td_ta_event_addr;
593 tdb->td_ta_set_event_p = &td_ta_set_event;
594 tdb->td_ta_event_getmsg_p = &td_ta_event_getmsg;
595 tdb->td_thr_tls_get_addr_p = &td_thr_tls_get_addr;
5876f503 596 tdb->td_thr_tlsbase_p = &td_thr_tlsbase;
96f15937
PP
597
598 return 1;
599}
600
601#else
602
cdbfd419
PP
603static int
604try_thread_db_load_1 (void *handle)
605{
606 td_err_e err;
9836d6ea 607 struct thread_db *tdb;
cdbfd419
PP
608 struct process_info *proc = current_process ();
609
610 if (proc->private->thread_db != NULL)
611 fatal ("unexpected: proc->private->thread_db != NULL");
612
9836d6ea
PA
613 tdb = xcalloc (1, sizeof (*tdb));
614 proc->private->thread_db = tdb;
f9e39928 615
9836d6ea 616 tdb->handle = handle;
cdbfd419
PP
617
618 /* Initialize pointers to the dynamic library functions we will use.
619 Essential functions first. */
620
621#define CHK(required, a) \
622 do \
623 { \
624 if ((a) == NULL) \
625 { \
626 if (debug_threads) \
87ce2a04 627 debug_printf ("dlsym: %s\n", dlerror ()); \
cdbfd419 628 if (required) \
9836d6ea
PA
629 { \
630 free (tdb); \
631 proc->private->thread_db = NULL; \
632 return 0; \
633 } \
cdbfd419
PP
634 } \
635 } \
636 while (0)
637
9836d6ea 638 CHK (1, tdb->td_ta_new_p = dlsym (handle, "td_ta_new"));
cdbfd419
PP
639
640 /* Attempt to open a connection to the thread library. */
9836d6ea 641 err = tdb->td_ta_new_p (&tdb->proc_handle, &tdb->thread_agent);
cdbfd419
PP
642 if (err != TD_OK)
643 {
644 if (debug_threads)
87ce2a04 645 debug_printf ("td_ta_new(): %s\n", thread_db_err_str (err));
9836d6ea
PA
646 free (tdb);
647 proc->private->thread_db = NULL;
cdbfd419
PP
648 return 0;
649 }
650
9836d6ea
PA
651 CHK (1, tdb->td_ta_map_lwp2thr_p = dlsym (handle, "td_ta_map_lwp2thr"));
652 CHK (1, tdb->td_thr_get_info_p = dlsym (handle, "td_thr_get_info"));
653 CHK (1, tdb->td_ta_thr_iter_p = dlsym (handle, "td_ta_thr_iter"));
654 CHK (1, tdb->td_symbol_list_p = dlsym (handle, "td_symbol_list"));
cdbfd419
PP
655
656 /* This is required only when thread_db_use_events is on. */
657 CHK (thread_db_use_events,
9836d6ea 658 tdb->td_thr_event_enable_p = dlsym (handle, "td_thr_event_enable"));
cdbfd419
PP
659
660 /* These are not essential. */
9836d6ea
PA
661 CHK (0, tdb->td_ta_event_addr_p = dlsym (handle, "td_ta_event_addr"));
662 CHK (0, tdb->td_ta_set_event_p = dlsym (handle, "td_ta_set_event"));
663 CHK (0, tdb->td_ta_event_getmsg_p = dlsym (handle, "td_ta_event_getmsg"));
664 CHK (0, tdb->td_thr_tls_get_addr_p = dlsym (handle, "td_thr_tls_get_addr"));
5876f503 665 CHK (0, tdb->td_thr_tlsbase_p = dlsym (handle, "td_thr_tlsbase"));
cdbfd419
PP
666
667#undef CHK
668
cdbfd419
PP
669 return 1;
670}
671
10e86dd7
DE
672#ifdef HAVE_DLADDR
673
cdbfd419
PP
674/* Lookup a library in which given symbol resides.
675 Note: this is looking in the GDBSERVER process, not in the inferior.
676 Returns library name, or NULL. */
677
678static const char *
679dladdr_to_soname (const void *addr)
680{
681 Dl_info info;
682
683 if (dladdr (addr, &info) != 0)
684 return info.dli_fname;
685 return NULL;
686}
687
10e86dd7
DE
688#endif
689
cdbfd419
PP
690static int
691try_thread_db_load (const char *library)
692{
693 void *handle;
694
695 if (debug_threads)
87ce2a04
DE
696 debug_printf ("Trying host libthread_db library: %s.\n",
697 library);
cdbfd419
PP
698 handle = dlopen (library, RTLD_NOW);
699 if (handle == NULL)
700 {
701 if (debug_threads)
87ce2a04 702 debug_printf ("dlopen failed: %s.\n", dlerror ());
cdbfd419
PP
703 return 0;
704 }
705
10e86dd7 706#ifdef HAVE_DLADDR
cdbfd419
PP
707 if (debug_threads && strchr (library, '/') == NULL)
708 {
709 void *td_init;
710
711 td_init = dlsym (handle, "td_init");
712 if (td_init != NULL)
713 {
714 const char *const libpath = dladdr_to_soname (td_init);
715
716 if (libpath != NULL)
717 fprintf (stderr, "Host %s resolved to: %s.\n",
718 library, libpath);
719 }
720 }
10e86dd7 721#endif
cdbfd419
PP
722
723 if (try_thread_db_load_1 (handle))
724 return 1;
725
726 /* This library "refused" to work on current inferior. */
727 dlclose (handle);
728 return 0;
729}
730
98a5dd13
DE
731/* Handle $sdir in libthread-db-search-path.
732 Look for libthread_db in the system dirs, or wherever a plain
733 dlopen(file_without_path) will look.
734 The result is true for success. */
735
cdbfd419 736static int
98a5dd13
DE
737try_thread_db_load_from_sdir (void)
738{
739 return try_thread_db_load (LIBTHREAD_DB_SO);
740}
741
742/* Try to load libthread_db from directory DIR of length DIR_LEN.
743 The result is true for success. */
744
745static int
746try_thread_db_load_from_dir (const char *dir, size_t dir_len)
cdbfd419
PP
747{
748 char path[PATH_MAX];
98a5dd13
DE
749
750 if (dir_len + 1 + strlen (LIBTHREAD_DB_SO) + 1 > sizeof (path))
751 {
752 char *cp = xmalloc (dir_len + 1);
753
754 memcpy (cp, dir, dir_len);
755 cp[dir_len] = '\0';
756 warning (_("libthread-db-search-path component too long,"
757 " ignored: %s."), cp);
758 free (cp);
759 return 0;
760 }
761
762 memcpy (path, dir, dir_len);
763 path[dir_len] = '/';
764 strcpy (path + dir_len + 1, LIBTHREAD_DB_SO);
765 return try_thread_db_load (path);
766}
767
768/* Search libthread_db_search_path for libthread_db which "agrees"
769 to work on current inferior.
770 The result is true for success. */
771
772static int
773thread_db_load_search (void)
774{
e6712ff1
DE
775 VEC (char_ptr) *dir_vec;
776 char *this_dir;
777 int i, rc = 0;
cdbfd419
PP
778
779 if (libthread_db_search_path == NULL)
780 libthread_db_search_path = xstrdup (LIBTHREAD_DB_SEARCH_PATH);
781
e6712ff1
DE
782 dir_vec = dirnames_to_char_ptr_vec (libthread_db_search_path);
783
784 for (i = 0; VEC_iterate (char_ptr, dir_vec, i, this_dir); ++i)
cdbfd419 785 {
e6712ff1 786 const int pdir_len = sizeof ("$pdir") - 1;
98a5dd13
DE
787 size_t this_dir_len;
788
e6712ff1 789 this_dir_len = strlen (this_dir);
cdbfd419 790
e6712ff1
DE
791 if (strncmp (this_dir, "$pdir", pdir_len) == 0
792 && (this_dir[pdir_len] == '\0'
793 || this_dir[pdir_len] == '/'))
98a5dd13
DE
794 {
795 /* We don't maintain a list of loaded libraries so we don't know
796 where libpthread lives. We *could* fetch the info, but we don't
797 do that yet. Ignore it. */
798 }
e6712ff1 799 else if (strcmp (this_dir, "$sdir") == 0)
98a5dd13
DE
800 {
801 if (try_thread_db_load_from_sdir ())
cdbfd419 802 {
98a5dd13 803 rc = 1;
cdbfd419
PP
804 break;
805 }
cdbfd419 806 }
98a5dd13 807 else
cdbfd419 808 {
98a5dd13
DE
809 if (try_thread_db_load_from_dir (this_dir, this_dir_len))
810 {
811 rc = 1;
812 break;
813 }
cdbfd419
PP
814 }
815 }
cdbfd419 816
e6712ff1 817 free_char_ptr_vec (dir_vec);
cdbfd419 818 if (debug_threads)
87ce2a04 819 debug_printf ("thread_db_load_search returning %d\n", rc);
cdbfd419 820 return rc;
dae5f5cf
DJ
821}
822
96f15937
PP
823#endif /* USE_LIBTHREAD_DB_DIRECTLY */
824
0d62e5e8 825int
24a09b5f 826thread_db_init (int use_events)
0d62e5e8 827{
95954743 828 struct process_info *proc = current_process ();
0d62e5e8 829
fd500816
DJ
830 /* FIXME drow/2004-10-16: This is the "overall process ID", which
831 GNU/Linux calls tgid, "thread group ID". When we support
832 attaching to threads, the original thread may not be the correct
833 thread. We would have to get the process ID from /proc for NPTL.
834 For LinuxThreads we could do something similar: follow the chain
835 of parent processes until we find the highest one we're attached
836 to, and use its tgid.
837
838 This isn't the only place in gdbserver that assumes that the first
839 process in the list is the thread group leader. */
ea025f5f 840
24a09b5f
DJ
841 thread_db_use_events = use_events;
842
cdbfd419 843 if (thread_db_load_search ())
0d62e5e8 844 {
24a09b5f 845 if (use_events && thread_db_enable_reporting () == 0)
cdbfd419
PP
846 {
847 /* Keep trying; maybe event reporting will work later. */
8336d594 848 thread_db_mourn (proc);
cdbfd419
PP
849 return 0;
850 }
0d62e5e8 851 thread_db_find_new_threads ();
fd500816 852 thread_db_look_up_symbols ();
0d62e5e8 853 return 1;
cdbfd419 854 }
0d62e5e8 855
cdbfd419
PP
856 return 0;
857}
858
ca5c370d
PA
859static int
860any_thread_of (struct inferior_list_entry *entry, void *args)
861{
862 int *pid_p = args;
863
864 if (ptid_get_pid (entry->id) == *pid_p)
865 return 1;
866
867 return 0;
868}
869
f9e39928
PA
870static void
871switch_to_process (struct process_info *proc)
872{
873 int pid = pid_of (proc);
874
875 current_inferior =
876 (struct thread_info *) find_inferior (&all_threads,
877 any_thread_of, &pid);
878}
879
cdbfd419
PP
880/* Disconnect from libthread_db and free resources. */
881
8336d594
PA
882static void
883disable_thread_event_reporting (struct process_info *proc)
cdbfd419
PP
884{
885 struct thread_db *thread_db = proc->private->thread_db;
886 if (thread_db)
887 {
21e1bee4
PP
888 td_err_e (*td_ta_clear_event_p) (const td_thragent_t *ta,
889 td_thr_events_t *event);
890
fd7dd3e6 891#ifndef USE_LIBTHREAD_DB_DIRECTLY
21e1bee4 892 td_ta_clear_event_p = dlsym (thread_db->handle, "td_ta_clear_event");
fd7dd3e6 893#else
fd7dd3e6
PA
894 td_ta_clear_event_p = &td_ta_clear_event;
895#endif
896
8336d594 897 if (td_ta_clear_event_p != NULL)
21e1bee4 898 {
f9e39928 899 struct thread_info *saved_inferior = current_inferior;
21e1bee4 900 td_thr_events_t events;
8336d594 901
f9e39928 902 switch_to_process (proc);
21e1bee4 903
fd7dd3e6
PA
904 /* Set the process wide mask saying we aren't interested
905 in any events anymore. */
21e1bee4
PP
906 td_event_fillset (&events);
907 (*td_ta_clear_event_p) (thread_db->thread_agent, &events);
8336d594
PA
908
909 current_inferior = saved_inferior;
21e1bee4 910 }
8336d594
PA
911 }
912}
913
f9e39928
PA
914static void
915remove_thread_event_breakpoints (struct process_info *proc)
916{
917 struct thread_db *thread_db = proc->private->thread_db;
918
919 if (thread_db->td_create_bp != NULL)
920 {
921 struct thread_info *saved_inferior = current_inferior;
922
923 switch_to_process (proc);
924
925 delete_breakpoint (thread_db->td_create_bp);
926 thread_db->td_create_bp = NULL;
927
928 current_inferior = saved_inferior;
929 }
930}
931
8336d594
PA
932void
933thread_db_detach (struct process_info *proc)
934{
f9e39928
PA
935 struct thread_db *thread_db = proc->private->thread_db;
936
937 if (thread_db)
938 {
939 disable_thread_event_reporting (proc);
940 remove_thread_event_breakpoints (proc);
941 }
8336d594
PA
942}
943
944/* Disconnect from libthread_db and free resources. */
945
946void
947thread_db_mourn (struct process_info *proc)
948{
949 struct thread_db *thread_db = proc->private->thread_db;
950 if (thread_db)
951 {
952 td_err_e (*td_ta_delete_p) (td_thragent_t *);
953
954#ifndef USE_LIBTHREAD_DB_DIRECTLY
955 td_ta_delete_p = dlsym (thread_db->handle, "td_ta_delete");
956#else
957 td_ta_delete_p = &td_ta_delete;
958#endif
cdbfd419 959
cdbfd419
PP
960 if (td_ta_delete_p != NULL)
961 (*td_ta_delete_p) (thread_db->thread_agent);
962
fd7dd3e6 963#ifndef USE_LIBTHREAD_DB_DIRECTLY
cdbfd419 964 dlclose (thread_db->handle);
96f15937
PP
965#endif /* USE_LIBTHREAD_DB_DIRECTLY */
966
cdbfd419
PP
967 free (thread_db);
968 proc->private->thread_db = NULL;
969 }
970}
971
972/* Handle "set libthread-db-search-path" monitor command and return 1.
973 For any other command, return 0. */
974
975int
976thread_db_handle_monitor_command (char *mon)
977{
84e578fb
DE
978 const char *cmd = "set libthread-db-search-path";
979 size_t cmd_len = strlen (cmd);
980
981 if (strncmp (mon, cmd, cmd_len) == 0
982 && (mon[cmd_len] == '\0'
983 || mon[cmd_len] == ' '))
cdbfd419 984 {
84e578fb 985 const char *cp = mon + cmd_len;
cdbfd419
PP
986
987 if (libthread_db_search_path != NULL)
988 free (libthread_db_search_path);
989
990 /* Skip leading space (if any). */
991 while (isspace (*cp))
992 ++cp;
993
84e578fb
DE
994 if (*cp == '\0')
995 cp = LIBTHREAD_DB_SEARCH_PATH;
cdbfd419
PP
996 libthread_db_search_path = xstrdup (cp);
997
998 monitor_output ("libthread-db-search-path set to `");
999 monitor_output (libthread_db_search_path);
1000 monitor_output ("'\n");
1001 return 1;
0d62e5e8
DJ
1002 }
1003
cdbfd419 1004 /* Tell server.c to perform default processing. */
0d62e5e8
DJ
1005 return 0;
1006}
This page took 0.971008 seconds and 4 git commands to generate.