]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/gdbserver/thread-db.c
Update year range in copyright notice of all files owned by the GDB project.
[thirdparty/binutils-gdb.git] / gdb / gdbserver / thread-db.c
1 /* Thread management interface, for the remote server for GDB.
2 Copyright (C) 2002-2015 Free Software Foundation, Inc.
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
10 the Free Software Foundation; either version 3 of the License, or
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
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include "server.h"
22
23 #include "linux-low.h"
24
25 extern int debug_threads;
26
27 static int thread_db_use_events;
28
29 #include "gdb_proc_service.h"
30 #include "nat/gdb_thread_db.h"
31 #include "gdb_vecs.h"
32
33 #ifndef USE_LIBTHREAD_DB_DIRECTLY
34 #include <dlfcn.h>
35 #endif
36
37 #include <stdint.h>
38 #include <limits.h>
39 #include <ctype.h>
40
41 struct 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
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
55 #ifndef USE_LIBTHREAD_DB_DIRECTLY
56 /* Handle of the libthread_db from dlopen. */
57 void *handle;
58 #endif
59
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
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,
89 psaddr_t map_address,
90 size_t offset, psaddr_t *address);
91 td_err_e (*td_thr_tlsbase_p) (const td_thrhandle_t *th,
92 unsigned long int modid,
93 psaddr_t *base);
94 const char ** (*td_symbol_list_p) (void);
95 };
96
97 static char *libthread_db_search_path;
98
99 static int find_one_thread (ptid_t);
100 static int find_new_threads_callback (const td_thrhandle_t *th_p, void *data);
101
102 static const char *
103 thread_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";
151 #ifdef HAVE_TD_VERSION
152 case TD_VERSION:
153 return "version mismatch between libthread_db and libpthread";
154 #endif
155 default:
156 xsnprintf (buf, sizeof (buf), "unknown thread_db error '%d'", err);
157 return buf;
158 }
159 }
160
161 #if 0
162 static char *
163 thread_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:
182 xsnprintf (buf, sizeof (buf), "unknown thread_db state %d", state);
183 return buf;
184 }
185 }
186 #endif
187
188 static int
189 thread_db_create_event (CORE_ADDR where)
190 {
191 td_event_msg_t msg;
192 td_err_e err;
193 struct lwp_info *lwp;
194 struct thread_db *thread_db = current_process ()->private->thread_db;
195
196 gdb_assert (thread_db->td_ta_event_getmsg_p != NULL);
197
198 if (debug_threads)
199 debug_printf ("Thread creation event.\n");
200
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). */
205 err = thread_db->td_ta_event_getmsg_p (thread_db->thread_agent, &msg);
206 if (err != TD_OK)
207 fprintf (stderr, "thread getmsg err: %s\n",
208 thread_db_err_str (err));
209
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. */
213 lwp = get_thread_lwp (current_thread);
214 if (lwp->thread_known == 0)
215 find_one_thread (current_thread->entry.id);
216
217 /* msg.event == TD_EVENT_CREATE */
218
219 find_new_threads_callback (msg.th_p, NULL);
220
221 return 0;
222 }
223
224 static int
225 thread_db_enable_reporting (void)
226 {
227 td_thr_events_t events;
228 td_notify_t notify;
229 td_err_e err;
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;
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
242 err = thread_db->td_ta_set_event_p (thread_db->thread_agent, &events);
243 if (err != TD_OK)
244 {
245 warning ("Unable to set global thread event mask: %s",
246 thread_db_err_str (err));
247 return 0;
248 }
249
250 /* Get address for thread creation breakpoint. */
251 err = thread_db->td_ta_event_addr_p (thread_db->thread_agent, TD_CREATE,
252 &notify);
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 }
259 thread_db->td_create_bp
260 = set_breakpoint_at ((CORE_ADDR) (unsigned long) notify.u.bptaddr,
261 thread_db_create_event);
262
263 return 1;
264 }
265
266 static int
267 find_one_thread (ptid_t ptid)
268 {
269 td_thrhandle_t th;
270 td_thrinfo_t ti;
271 td_err_e err;
272 struct thread_info *inferior;
273 struct lwp_info *lwp;
274 struct thread_db *thread_db = current_process ()->private->thread_db;
275 int lwpid = ptid_get_lwp (ptid);
276
277 inferior = (struct thread_info *) find_inferior_id (&all_threads, ptid);
278 lwp = get_thread_lwp (inferior);
279 if (lwp->thread_known)
280 return 1;
281
282 /* Get information about this thread. */
283 err = thread_db->td_ta_map_lwp2thr_p (thread_db->thread_agent, lwpid, &th);
284 if (err != TD_OK)
285 error ("Cannot get thread handle for LWP %d: %s",
286 lwpid, thread_db_err_str (err));
287
288 err = thread_db->td_thr_get_info_p (&th, &ti);
289 if (err != TD_OK)
290 error ("Cannot get thread info for LWP %d: %s",
291 lwpid, thread_db_err_str (err));
292
293 if (debug_threads)
294 debug_printf ("Found thread %ld (LWP %d)\n",
295 ti.ti_tid, ti.ti_lid);
296
297 if (lwpid != ti.ti_lid)
298 {
299 warning ("PID mismatch! Expected %ld, got %ld",
300 (long) lwpid, (long) ti.ti_lid);
301 return 0;
302 }
303
304 if (thread_db_use_events)
305 {
306 err = thread_db->td_thr_event_enable_p (&th, 1);
307 if (err != TD_OK)
308 error ("Cannot enable thread event reporting for %d: %s",
309 ti.ti_lid, thread_db_err_str (err));
310 }
311
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;
316
317 lwp->thread_known = 1;
318 lwp->th = th;
319
320 return 1;
321 }
322
323 /* Attach a thread. Return true on success. */
324
325 static int
326 attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p)
327 {
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);
331 struct lwp_info *lwp;
332 int err;
333
334 if (debug_threads)
335 debug_printf ("Attaching to thread %ld (LWP %d)\n",
336 ti_p->ti_tid, ti_p->ti_lid);
337 err = linux_attach_lwp (ptid);
338 if (err != 0)
339 {
340 warning ("Could not attach to thread %ld (LWP %d): %s\n",
341 ti_p->ti_tid, ti_p->ti_lid,
342 linux_attach_fail_reason_string (ptid, err));
343 return 0;
344 }
345
346 lwp = find_lwp_pid (ptid);
347 gdb_assert (lwp != NULL);
348 lwp->thread_known = 1;
349 lwp->th = *th_p;
350
351 if (thread_db_use_events)
352 {
353 td_err_e err;
354 struct thread_db *thread_db = proc->private->thread_db;
355
356 err = thread_db->td_thr_event_enable_p (th_p, 1);
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 }
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
369 static int
370 maybe_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;
386 }
387
388 static int
389 find_new_threads_callback (const td_thrhandle_t *th_p, void *data)
390 {
391 td_thrinfo_t ti;
392 td_err_e err;
393 struct thread_db *thread_db = current_process ()->private->thread_db;
394
395 err = thread_db->td_thr_get_info_p (th_p, &ti);
396 if (err != TD_OK)
397 error ("Cannot get thread info: %s", thread_db_err_str (err));
398
399 /* Check for zombies. */
400 if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
401 return 0;
402
403 if (!maybe_attach_thread (th_p, &ti, (int *) data))
404 {
405 /* Terminate iteration early: we might be looking at stale data in
406 the inferior. The thread_db_find_new_threads will retry. */
407 return 1;
408 }
409
410 return 0;
411 }
412
413 static void
414 thread_db_find_new_threads (void)
415 {
416 td_err_e err;
417 ptid_t ptid = current_ptid;
418 struct thread_db *thread_db = current_process ()->private->thread_db;
419 int loop, iteration;
420
421 /* This function is only called when we first initialize thread_db.
422 First locate the initial thread. If it is not ready for
423 debugging yet, then stop. */
424 if (find_one_thread (ptid) == 0)
425 return;
426
427 /* Require 4 successive iterations which do not find any new threads.
428 The 4 is a heuristic: there is an inherent race here, and I have
429 seen that 2 iterations in a row are not always sufficient to
430 "capture" all threads. */
431 for (loop = 0, iteration = 0; loop < 4; ++loop, ++iteration)
432 {
433 int new_thread_count = 0;
434
435 /* Iterate over all user-space threads to discover new threads. */
436 err = thread_db->td_ta_thr_iter_p (thread_db->thread_agent,
437 find_new_threads_callback,
438 &new_thread_count,
439 TD_THR_ANY_STATE,
440 TD_THR_LOWEST_PRIORITY,
441 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
442 if (debug_threads)
443 debug_printf ("Found %d threads in iteration %d.\n",
444 new_thread_count, iteration);
445
446 if (new_thread_count != 0)
447 {
448 /* Found new threads. Restart iteration from beginning. */
449 loop = -1;
450 }
451 }
452 if (err != TD_OK)
453 error ("Cannot find new threads: %s", thread_db_err_str (err));
454 }
455
456 /* Cache all future symbols that thread_db might request. We can not
457 request symbols at arbitrary states in the remote protocol, only
458 when the client tells us that new symbols are available. So when
459 we load the thread library, make sure to check the entire list. */
460
461 static void
462 thread_db_look_up_symbols (void)
463 {
464 struct thread_db *thread_db = current_process ()->private->thread_db;
465 const char **sym_list;
466 CORE_ADDR unused;
467
468 for (sym_list = thread_db->td_symbol_list_p (); *sym_list; sym_list++)
469 look_up_one_symbol (*sym_list, &unused, 1);
470
471 /* We're not interested in any other libraries loaded after this
472 point, only in symbols in libpthread.so. */
473 thread_db->all_symbols_looked_up = 1;
474 }
475
476 int
477 thread_db_look_up_one_symbol (const char *name, CORE_ADDR *addrp)
478 {
479 struct thread_db *thread_db = current_process ()->private->thread_db;
480 int may_ask_gdb = !thread_db->all_symbols_looked_up;
481
482 /* If we've passed the call to thread_db_look_up_symbols, then
483 anything not in the cache must not exist; we're not interested
484 in any libraries loaded after that point, only in symbols in
485 libpthread.so. It might not be an appropriate time to look
486 up a symbol, e.g. while we're trying to fetch registers. */
487 return look_up_one_symbol (name, addrp, may_ask_gdb);
488 }
489
490 int
491 thread_db_get_tls_address (struct thread_info *thread, CORE_ADDR offset,
492 CORE_ADDR load_module, CORE_ADDR *address)
493 {
494 psaddr_t addr;
495 td_err_e err;
496 struct lwp_info *lwp;
497 struct thread_info *saved_thread;
498 struct process_info *proc;
499 struct thread_db *thread_db;
500
501 proc = get_thread_process (thread);
502 thread_db = proc->private->thread_db;
503
504 /* If the thread layer is not (yet) initialized, fail. */
505 if (thread_db == NULL || !thread_db->all_symbols_looked_up)
506 return TD_ERR;
507
508 /* If td_thr_tls_get_addr is missing rather do not expect td_thr_tlsbase
509 could work. */
510 if (thread_db->td_thr_tls_get_addr_p == NULL
511 || (load_module == 0 && thread_db->td_thr_tlsbase_p == NULL))
512 return -1;
513
514 lwp = get_thread_lwp (thread);
515 if (!lwp->thread_known)
516 find_one_thread (thread->entry.id);
517 if (!lwp->thread_known)
518 return TD_NOTHR;
519
520 saved_thread = current_thread;
521 current_thread = thread;
522
523 if (load_module != 0)
524 {
525 /* Note the cast through uintptr_t: this interface only works if
526 a target address fits in a psaddr_t, which is a host pointer.
527 So a 32-bit debugger can not access 64-bit TLS through this. */
528 err = thread_db->td_thr_tls_get_addr_p (&lwp->th,
529 (psaddr_t) (uintptr_t) load_module,
530 offset, &addr);
531 }
532 else
533 {
534 /* This code path handles the case of -static -pthread executables:
535 https://sourceware.org/ml/libc-help/2014-03/msg00024.html
536 For older GNU libc r_debug.r_map is NULL. For GNU libc after
537 PR libc/16831 due to GDB PR threads/16954 LOAD_MODULE is also NULL.
538 The constant number 1 depends on GNU __libc_setup_tls
539 initialization of l_tls_modid to 1. */
540 err = thread_db->td_thr_tlsbase_p (&lwp->th, 1, &addr);
541 addr = (char *) addr + offset;
542 }
543
544 current_thread = saved_thread;
545 if (err == TD_OK)
546 {
547 *address = (CORE_ADDR) (uintptr_t) addr;
548 return 0;
549 }
550 else
551 return err;
552 }
553
554 #ifdef USE_LIBTHREAD_DB_DIRECTLY
555
556 static int
557 thread_db_load_search (void)
558 {
559 td_err_e err;
560 struct thread_db *tdb;
561 struct process_info *proc = current_process ();
562
563 gdb_assert (proc->private->thread_db == NULL);
564
565 tdb = xcalloc (1, sizeof (*tdb));
566 proc->private->thread_db = tdb;
567
568 tdb->td_ta_new_p = &td_ta_new;
569
570 /* Attempt to open a connection to the thread library. */
571 err = tdb->td_ta_new_p (&tdb->proc_handle, &tdb->thread_agent);
572 if (err != TD_OK)
573 {
574 if (debug_threads)
575 debug_printf ("td_ta_new(): %s\n", thread_db_err_str (err));
576 free (tdb);
577 proc->private->thread_db = NULL;
578 return 0;
579 }
580
581 tdb->td_ta_map_lwp2thr_p = &td_ta_map_lwp2thr;
582 tdb->td_thr_get_info_p = &td_thr_get_info;
583 tdb->td_ta_thr_iter_p = &td_ta_thr_iter;
584 tdb->td_symbol_list_p = &td_symbol_list;
585
586 /* This is required only when thread_db_use_events is on. */
587 tdb->td_thr_event_enable_p = &td_thr_event_enable;
588
589 /* These are not essential. */
590 tdb->td_ta_event_addr_p = &td_ta_event_addr;
591 tdb->td_ta_set_event_p = &td_ta_set_event;
592 tdb->td_ta_event_getmsg_p = &td_ta_event_getmsg;
593 tdb->td_thr_tls_get_addr_p = &td_thr_tls_get_addr;
594 tdb->td_thr_tlsbase_p = &td_thr_tlsbase;
595
596 return 1;
597 }
598
599 #else
600
601 static int
602 try_thread_db_load_1 (void *handle)
603 {
604 td_err_e err;
605 struct thread_db *tdb;
606 struct process_info *proc = current_process ();
607
608 gdb_assert (proc->private->thread_db == NULL);
609
610 tdb = xcalloc (1, sizeof (*tdb));
611 proc->private->thread_db = tdb;
612
613 tdb->handle = handle;
614
615 /* Initialize pointers to the dynamic library functions we will use.
616 Essential functions first. */
617
618 #define CHK(required, a) \
619 do \
620 { \
621 if ((a) == NULL) \
622 { \
623 if (debug_threads) \
624 debug_printf ("dlsym: %s\n", dlerror ()); \
625 if (required) \
626 { \
627 free (tdb); \
628 proc->private->thread_db = NULL; \
629 return 0; \
630 } \
631 } \
632 } \
633 while (0)
634
635 CHK (1, tdb->td_ta_new_p = dlsym (handle, "td_ta_new"));
636
637 /* Attempt to open a connection to the thread library. */
638 err = tdb->td_ta_new_p (&tdb->proc_handle, &tdb->thread_agent);
639 if (err != TD_OK)
640 {
641 if (debug_threads)
642 debug_printf ("td_ta_new(): %s\n", thread_db_err_str (err));
643 free (tdb);
644 proc->private->thread_db = NULL;
645 return 0;
646 }
647
648 CHK (1, tdb->td_ta_map_lwp2thr_p = dlsym (handle, "td_ta_map_lwp2thr"));
649 CHK (1, tdb->td_thr_get_info_p = dlsym (handle, "td_thr_get_info"));
650 CHK (1, tdb->td_ta_thr_iter_p = dlsym (handle, "td_ta_thr_iter"));
651 CHK (1, tdb->td_symbol_list_p = dlsym (handle, "td_symbol_list"));
652
653 /* This is required only when thread_db_use_events is on. */
654 CHK (thread_db_use_events,
655 tdb->td_thr_event_enable_p = dlsym (handle, "td_thr_event_enable"));
656
657 /* These are not essential. */
658 CHK (0, tdb->td_ta_event_addr_p = dlsym (handle, "td_ta_event_addr"));
659 CHK (0, tdb->td_ta_set_event_p = dlsym (handle, "td_ta_set_event"));
660 CHK (0, tdb->td_ta_event_getmsg_p = dlsym (handle, "td_ta_event_getmsg"));
661 CHK (0, tdb->td_thr_tls_get_addr_p = dlsym (handle, "td_thr_tls_get_addr"));
662 CHK (0, tdb->td_thr_tlsbase_p = dlsym (handle, "td_thr_tlsbase"));
663
664 #undef CHK
665
666 return 1;
667 }
668
669 #ifdef HAVE_DLADDR
670
671 /* Lookup a library in which given symbol resides.
672 Note: this is looking in the GDBSERVER process, not in the inferior.
673 Returns library name, or NULL. */
674
675 static const char *
676 dladdr_to_soname (const void *addr)
677 {
678 Dl_info info;
679
680 if (dladdr (addr, &info) != 0)
681 return info.dli_fname;
682 return NULL;
683 }
684
685 #endif
686
687 static int
688 try_thread_db_load (const char *library)
689 {
690 void *handle;
691
692 if (debug_threads)
693 debug_printf ("Trying host libthread_db library: %s.\n",
694 library);
695 handle = dlopen (library, RTLD_NOW);
696 if (handle == NULL)
697 {
698 if (debug_threads)
699 debug_printf ("dlopen failed: %s.\n", dlerror ());
700 return 0;
701 }
702
703 #ifdef HAVE_DLADDR
704 if (debug_threads && strchr (library, '/') == NULL)
705 {
706 void *td_init;
707
708 td_init = dlsym (handle, "td_init");
709 if (td_init != NULL)
710 {
711 const char *const libpath = dladdr_to_soname (td_init);
712
713 if (libpath != NULL)
714 fprintf (stderr, "Host %s resolved to: %s.\n",
715 library, libpath);
716 }
717 }
718 #endif
719
720 if (try_thread_db_load_1 (handle))
721 return 1;
722
723 /* This library "refused" to work on current inferior. */
724 dlclose (handle);
725 return 0;
726 }
727
728 /* Handle $sdir in libthread-db-search-path.
729 Look for libthread_db in the system dirs, or wherever a plain
730 dlopen(file_without_path) will look.
731 The result is true for success. */
732
733 static int
734 try_thread_db_load_from_sdir (void)
735 {
736 return try_thread_db_load (LIBTHREAD_DB_SO);
737 }
738
739 /* Try to load libthread_db from directory DIR of length DIR_LEN.
740 The result is true for success. */
741
742 static int
743 try_thread_db_load_from_dir (const char *dir, size_t dir_len)
744 {
745 char path[PATH_MAX];
746
747 if (dir_len + 1 + strlen (LIBTHREAD_DB_SO) + 1 > sizeof (path))
748 {
749 char *cp = xmalloc (dir_len + 1);
750
751 memcpy (cp, dir, dir_len);
752 cp[dir_len] = '\0';
753 warning (_("libthread-db-search-path component too long,"
754 " ignored: %s."), cp);
755 free (cp);
756 return 0;
757 }
758
759 memcpy (path, dir, dir_len);
760 path[dir_len] = '/';
761 strcpy (path + dir_len + 1, LIBTHREAD_DB_SO);
762 return try_thread_db_load (path);
763 }
764
765 /* Search libthread_db_search_path for libthread_db which "agrees"
766 to work on current inferior.
767 The result is true for success. */
768
769 static int
770 thread_db_load_search (void)
771 {
772 VEC (char_ptr) *dir_vec;
773 char *this_dir;
774 int i, rc = 0;
775
776 if (libthread_db_search_path == NULL)
777 libthread_db_search_path = xstrdup (LIBTHREAD_DB_SEARCH_PATH);
778
779 dir_vec = dirnames_to_char_ptr_vec (libthread_db_search_path);
780
781 for (i = 0; VEC_iterate (char_ptr, dir_vec, i, this_dir); ++i)
782 {
783 const int pdir_len = sizeof ("$pdir") - 1;
784 size_t this_dir_len;
785
786 this_dir_len = strlen (this_dir);
787
788 if (strncmp (this_dir, "$pdir", pdir_len) == 0
789 && (this_dir[pdir_len] == '\0'
790 || this_dir[pdir_len] == '/'))
791 {
792 /* We don't maintain a list of loaded libraries so we don't know
793 where libpthread lives. We *could* fetch the info, but we don't
794 do that yet. Ignore it. */
795 }
796 else if (strcmp (this_dir, "$sdir") == 0)
797 {
798 if (try_thread_db_load_from_sdir ())
799 {
800 rc = 1;
801 break;
802 }
803 }
804 else
805 {
806 if (try_thread_db_load_from_dir (this_dir, this_dir_len))
807 {
808 rc = 1;
809 break;
810 }
811 }
812 }
813
814 free_char_ptr_vec (dir_vec);
815 if (debug_threads)
816 debug_printf ("thread_db_load_search returning %d\n", rc);
817 return rc;
818 }
819
820 #endif /* USE_LIBTHREAD_DB_DIRECTLY */
821
822 int
823 thread_db_init (int use_events)
824 {
825 struct process_info *proc = current_process ();
826
827 /* FIXME drow/2004-10-16: This is the "overall process ID", which
828 GNU/Linux calls tgid, "thread group ID". When we support
829 attaching to threads, the original thread may not be the correct
830 thread. We would have to get the process ID from /proc for NPTL.
831 For LinuxThreads we could do something similar: follow the chain
832 of parent processes until we find the highest one we're attached
833 to, and use its tgid.
834
835 This isn't the only place in gdbserver that assumes that the first
836 process in the list is the thread group leader. */
837
838 thread_db_use_events = use_events;
839
840 if (thread_db_load_search ())
841 {
842 if (use_events && thread_db_enable_reporting () == 0)
843 {
844 /* Keep trying; maybe event reporting will work later. */
845 thread_db_mourn (proc);
846 return 0;
847 }
848 thread_db_find_new_threads ();
849 thread_db_look_up_symbols ();
850 return 1;
851 }
852
853 return 0;
854 }
855
856 static int
857 any_thread_of (struct inferior_list_entry *entry, void *args)
858 {
859 int *pid_p = args;
860
861 if (ptid_get_pid (entry->id) == *pid_p)
862 return 1;
863
864 return 0;
865 }
866
867 static void
868 switch_to_process (struct process_info *proc)
869 {
870 int pid = pid_of (proc);
871
872 current_thread =
873 (struct thread_info *) find_inferior (&all_threads,
874 any_thread_of, &pid);
875 }
876
877 /* Disconnect from libthread_db and free resources. */
878
879 static void
880 disable_thread_event_reporting (struct process_info *proc)
881 {
882 struct thread_db *thread_db = proc->private->thread_db;
883 if (thread_db)
884 {
885 td_err_e (*td_ta_clear_event_p) (const td_thragent_t *ta,
886 td_thr_events_t *event);
887
888 #ifndef USE_LIBTHREAD_DB_DIRECTLY
889 td_ta_clear_event_p = dlsym (thread_db->handle, "td_ta_clear_event");
890 #else
891 td_ta_clear_event_p = &td_ta_clear_event;
892 #endif
893
894 if (td_ta_clear_event_p != NULL)
895 {
896 struct thread_info *saved_thread = current_thread;
897 td_thr_events_t events;
898
899 switch_to_process (proc);
900
901 /* Set the process wide mask saying we aren't interested
902 in any events anymore. */
903 td_event_fillset (&events);
904 (*td_ta_clear_event_p) (thread_db->thread_agent, &events);
905
906 current_thread = saved_thread;
907 }
908 }
909 }
910
911 static void
912 remove_thread_event_breakpoints (struct process_info *proc)
913 {
914 struct thread_db *thread_db = proc->private->thread_db;
915
916 if (thread_db->td_create_bp != NULL)
917 {
918 struct thread_info *saved_thread = current_thread;
919
920 switch_to_process (proc);
921
922 delete_breakpoint (thread_db->td_create_bp);
923 thread_db->td_create_bp = NULL;
924
925 current_thread = saved_thread;
926 }
927 }
928
929 void
930 thread_db_detach (struct process_info *proc)
931 {
932 struct thread_db *thread_db = proc->private->thread_db;
933
934 if (thread_db)
935 {
936 disable_thread_event_reporting (proc);
937 remove_thread_event_breakpoints (proc);
938 }
939 }
940
941 /* Disconnect from libthread_db and free resources. */
942
943 void
944 thread_db_mourn (struct process_info *proc)
945 {
946 struct thread_db *thread_db = proc->private->thread_db;
947 if (thread_db)
948 {
949 td_err_e (*td_ta_delete_p) (td_thragent_t *);
950
951 #ifndef USE_LIBTHREAD_DB_DIRECTLY
952 td_ta_delete_p = dlsym (thread_db->handle, "td_ta_delete");
953 #else
954 td_ta_delete_p = &td_ta_delete;
955 #endif
956
957 if (td_ta_delete_p != NULL)
958 (*td_ta_delete_p) (thread_db->thread_agent);
959
960 #ifndef USE_LIBTHREAD_DB_DIRECTLY
961 dlclose (thread_db->handle);
962 #endif /* USE_LIBTHREAD_DB_DIRECTLY */
963
964 free (thread_db);
965 proc->private->thread_db = NULL;
966 }
967 }
968
969 /* Handle "set libthread-db-search-path" monitor command and return 1.
970 For any other command, return 0. */
971
972 int
973 thread_db_handle_monitor_command (char *mon)
974 {
975 const char *cmd = "set libthread-db-search-path";
976 size_t cmd_len = strlen (cmd);
977
978 if (strncmp (mon, cmd, cmd_len) == 0
979 && (mon[cmd_len] == '\0'
980 || mon[cmd_len] == ' '))
981 {
982 const char *cp = mon + cmd_len;
983
984 if (libthread_db_search_path != NULL)
985 free (libthread_db_search_path);
986
987 /* Skip leading space (if any). */
988 while (isspace (*cp))
989 ++cp;
990
991 if (*cp == '\0')
992 cp = LIBTHREAD_DB_SEARCH_PATH;
993 libthread_db_search_path = xstrdup (cp);
994
995 monitor_output ("libthread-db-search-path set to `");
996 monitor_output (libthread_db_search_path);
997 monitor_output ("'\n");
998 return 1;
999 }
1000
1001 /* Tell server.c to perform default processing. */
1002 return 0;
1003 }