*** empty log message ***
[deliverable/binutils-gdb.git] / gdb / gdbserver / thread-db.c
CommitLineData
0d62e5e8 1/* Thread management interface, for the remote server for GDB.
0fb0cc75 2 Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008, 2009
9b254dd1 3 Free Software Foundation, Inc.
0d62e5e8
DJ
4
5 Contributed by MontaVista Software.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
0d62e5e8
DJ
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
0d62e5e8
DJ
21
22#include "server.h"
23
24#include "linux-low.h"
25
26extern int debug_threads;
27
24a09b5f
DJ
28static int thread_db_use_events;
29
0050a760 30#include "gdb_proc_service.h"
cdbfd419 31#include "../gdb_thread_db.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
96f15937 50#ifndef USE_LIBTHREAD_DB_DIRECTLY
cdbfd419
PP
51 /* Handle of the libthread_db from dlopen. */
52 void *handle;
96f15937 53#endif
cdbfd419
PP
54
55 /* Addresses of libthread_db functions. */
56 td_err_e (*td_ta_new_p) (struct ps_prochandle * ps, td_thragent_t **ta);
57 td_err_e (*td_ta_event_getmsg_p) (const td_thragent_t *ta,
58 td_event_msg_t *msg);
59 td_err_e (*td_ta_set_event_p) (const td_thragent_t *ta,
60 td_thr_events_t *event);
61 td_err_e (*td_ta_event_addr_p) (const td_thragent_t *ta,
62 td_event_e event, td_notify_t *ptr);
63 td_err_e (*td_ta_map_lwp2thr_p) (const td_thragent_t *ta, lwpid_t lwpid,
64 td_thrhandle_t *th);
65 td_err_e (*td_thr_get_info_p) (const td_thrhandle_t *th,
66 td_thrinfo_t *infop);
67 td_err_e (*td_thr_event_enable_p) (const td_thrhandle_t *th, int event);
68 td_err_e (*td_ta_thr_iter_p) (const td_thragent_t *ta,
69 td_thr_iter_f *callback, void *cbdata_p,
70 td_thr_state_e state, int ti_pri,
71 sigset_t *ti_sigmask_p,
72 unsigned int ti_user_flags);
73 td_err_e (*td_thr_tls_get_addr_p) (const td_thrhandle_t *th,
00f515da
DE
74 psaddr_t map_address,
75 size_t offset, psaddr_t *address);
cdbfd419
PP
76 const char ** (*td_symbol_list_p) (void);
77};
78
79static char *libthread_db_search_path;
186947f7 80
95954743 81static int find_one_thread (ptid_t);
0d62e5e8
DJ
82static int find_new_threads_callback (const td_thrhandle_t *th_p, void *data);
83
54363045 84static const char *
0d62e5e8
DJ
85thread_db_err_str (td_err_e err)
86{
87 static char buf[64];
88
89 switch (err)
90 {
91 case TD_OK:
92 return "generic 'call succeeded'";
93 case TD_ERR:
94 return "generic error";
95 case TD_NOTHR:
96 return "no thread to satisfy query";
97 case TD_NOSV:
98 return "no sync handle to satisfy query";
99 case TD_NOLWP:
100 return "no LWP to satisfy query";
101 case TD_BADPH:
102 return "invalid process handle";
103 case TD_BADTH:
104 return "invalid thread handle";
105 case TD_BADSH:
106 return "invalid synchronization handle";
107 case TD_BADTA:
108 return "invalid thread agent";
109 case TD_BADKEY:
110 return "invalid key";
111 case TD_NOMSG:
112 return "no event message for getmsg";
113 case TD_NOFPREGS:
114 return "FPU register set not available";
115 case TD_NOLIBTHREAD:
116 return "application not linked with libthread";
117 case TD_NOEVENT:
118 return "requested event is not supported";
119 case TD_NOCAPAB:
120 return "capability not available";
121 case TD_DBERR:
122 return "debugger service failed";
123 case TD_NOAPLIC:
124 return "operation not applicable to";
125 case TD_NOTSD:
126 return "no thread-specific data for this thread";
127 case TD_MALLOC:
128 return "malloc failed";
129 case TD_PARTIALREG:
130 return "only part of register set was written/read";
131 case TD_NOXREGS:
132 return "X register set not available for this thread";
3db0444b
DJ
133#ifdef HAVE_TD_VERSION
134 case TD_VERSION:
135 return "version mismatch between libthread_db and libpthread";
136#endif
0d62e5e8
DJ
137 default:
138 snprintf (buf, sizeof (buf), "unknown thread_db error '%d'", err);
139 return buf;
140 }
141}
142
143#if 0
144static char *
145thread_db_state_str (td_thr_state_e state)
146{
147 static char buf[64];
148
149 switch (state)
150 {
151 case TD_THR_STOPPED:
152 return "stopped by debugger";
153 case TD_THR_RUN:
154 return "runnable";
155 case TD_THR_ACTIVE:
156 return "active";
157 case TD_THR_ZOMBIE:
158 return "zombie";
159 case TD_THR_SLEEP:
160 return "sleeping";
161 case TD_THR_STOPPED_ASLEEP:
162 return "stopped by debugger AND blocked";
163 default:
164 snprintf (buf, sizeof (buf), "unknown thread_db state %d", state);
165 return buf;
166 }
167}
168#endif
169
b65d95c5 170static int
0d62e5e8
DJ
171thread_db_create_event (CORE_ADDR where)
172{
173 td_event_msg_t msg;
174 td_err_e err;
54a0b537 175 struct lwp_info *lwp;
cdbfd419
PP
176 struct thread_db *thread_db = current_process ()->private->thread_db;
177
178 if (thread_db->td_ta_event_getmsg_p == NULL)
179 fatal ("unexpected thread_db->td_ta_event_getmsg_p == NULL");
0d62e5e8
DJ
180
181 if (debug_threads)
182 fprintf (stderr, "Thread creation event.\n");
183
0d62e5e8
DJ
184 /* FIXME: This assumes we don't get another event.
185 In the LinuxThreads implementation, this is safe,
186 because all events come from the manager thread
187 (except for its own creation, of course). */
cdbfd419 188 err = thread_db->td_ta_event_getmsg_p (thread_db->thread_agent, &msg);
0d62e5e8
DJ
189 if (err != TD_OK)
190 fprintf (stderr, "thread getmsg err: %s\n",
191 thread_db_err_str (err));
192
4105de34
DJ
193 /* If we do not know about the main thread yet, this would be a good time to
194 find it. We need to do this to pick up the main thread before any newly
195 created threads. */
54a0b537
PA
196 lwp = get_thread_lwp (current_inferior);
197 if (lwp->thread_known == 0)
95954743 198 find_one_thread (lwp->head.id);
4105de34 199
0d62e5e8
DJ
200 /* msg.event == TD_EVENT_CREATE */
201
202 find_new_threads_callback (msg.th_p, NULL);
b65d95c5
DJ
203
204 return 0;
0d62e5e8
DJ
205}
206
0d62e5e8
DJ
207static int
208thread_db_enable_reporting ()
209{
210 td_thr_events_t events;
211 td_notify_t notify;
212 td_err_e err;
cdbfd419
PP
213 struct thread_db *thread_db = current_process ()->private->thread_db;
214
215 if (thread_db->td_ta_set_event_p == NULL
216 || thread_db->td_ta_event_addr_p == NULL
217 || thread_db->td_ta_event_getmsg_p == NULL)
218 /* This libthread_db is missing required support. */
219 return 0;
0d62e5e8
DJ
220
221 /* Set the process wide mask saying which events we're interested in. */
222 td_event_emptyset (&events);
223 td_event_addset (&events, TD_CREATE);
224
cdbfd419 225 err = thread_db->td_ta_set_event_p (thread_db->thread_agent, &events);
0d62e5e8
DJ
226 if (err != TD_OK)
227 {
228 warning ("Unable to set global thread event mask: %s",
1b3f6016 229 thread_db_err_str (err));
0d62e5e8
DJ
230 return 0;
231 }
232
233 /* Get address for thread creation breakpoint. */
cdbfd419
PP
234 err = thread_db->td_ta_event_addr_p (thread_db->thread_agent, TD_CREATE,
235 &notify);
0d62e5e8
DJ
236 if (err != TD_OK)
237 {
238 warning ("Unable to get location for thread creation breakpoint: %s",
239 thread_db_err_str (err));
240 return 0;
241 }
242 set_breakpoint_at ((CORE_ADDR) (unsigned long) notify.u.bptaddr,
243 thread_db_create_event);
244
0d62e5e8
DJ
245 return 1;
246}
247
ae13219e 248static int
95954743 249find_one_thread (ptid_t ptid)
0d62e5e8 250{
ae13219e
DJ
251 td_thrhandle_t th;
252 td_thrinfo_t ti;
0d62e5e8
DJ
253 td_err_e err;
254 struct thread_info *inferior;
54a0b537 255 struct lwp_info *lwp;
cdbfd419 256 struct thread_db *thread_db = current_process ()->private->thread_db;
95954743 257 int lwpid = ptid_get_lwp (ptid);
0d62e5e8 258
95954743 259 inferior = (struct thread_info *) find_inferior_id (&all_threads, ptid);
54a0b537
PA
260 lwp = get_thread_lwp (inferior);
261 if (lwp->thread_known)
ae13219e
DJ
262 return 1;
263
24a09b5f 264 /* Get information about this thread. */
cdbfd419 265 err = thread_db->td_ta_map_lwp2thr_p (thread_db->thread_agent, lwpid, &th);
ae13219e 266 if (err != TD_OK)
24a09b5f
DJ
267 error ("Cannot get thread handle for LWP %d: %s",
268 lwpid, thread_db_err_str (err));
ae13219e 269
cdbfd419 270 err = thread_db->td_thr_get_info_p (&th, &ti);
ae13219e 271 if (err != TD_OK)
24a09b5f
DJ
272 error ("Cannot get thread info for LWP %d: %s",
273 lwpid, thread_db_err_str (err));
ae13219e
DJ
274
275 if (debug_threads)
24a09b5f 276 fprintf (stderr, "Found thread %ld (LWP %d)\n",
ae13219e
DJ
277 ti.ti_tid, ti.ti_lid);
278
95954743 279 if (lwpid != ti.ti_lid)
24a09b5f
DJ
280 {
281 warning ("PID mismatch! Expected %ld, got %ld",
95954743 282 (long) lwpid, (long) ti.ti_lid);
24a09b5f
DJ
283 return 0;
284 }
ae13219e 285
24a09b5f 286 if (thread_db_use_events)
0d62e5e8 287 {
cdbfd419 288 err = thread_db->td_thr_event_enable_p (&th, 1);
ae13219e
DJ
289 if (err != TD_OK)
290 error ("Cannot enable thread event reporting for %d: %s",
291 ti.ti_lid, thread_db_err_str (err));
0d62e5e8 292 }
ae13219e 293
24a09b5f
DJ
294 /* If the new thread ID is zero, a final thread ID will be available
295 later. Do not enable thread debugging yet. */
296 if (ti.ti_tid == 0)
297 return 0;
ae13219e 298
54a0b537
PA
299 lwp->thread_known = 1;
300 lwp->th = th;
ae13219e 301
ae13219e
DJ
302 return 1;
303}
304
5f7d1694
PP
305/* Attach a thread. Return true on success. */
306
307static int
308attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p)
ae13219e 309{
54a0b537 310 struct lwp_info *lwp;
ae13219e 311
0d62e5e8
DJ
312 if (debug_threads)
313 fprintf (stderr, "Attaching to thread %ld (LWP %d)\n",
314 ti_p->ti_tid, ti_p->ti_lid);
24a09b5f 315 linux_attach_lwp (ti_p->ti_lid);
95954743
PA
316 lwp = find_lwp_pid (pid_to_ptid (ti_p->ti_lid));
317 if (lwp == NULL)
0d62e5e8
DJ
318 {
319 warning ("Could not attach to thread %ld (LWP %d)\n",
320 ti_p->ti_tid, ti_p->ti_lid);
5f7d1694 321 return 0;
0d62e5e8
DJ
322 }
323
54a0b537
PA
324 lwp->thread_known = 1;
325 lwp->th = *th_p;
24a09b5f
DJ
326
327 if (thread_db_use_events)
328 {
5f7d1694 329 td_err_e err;
cdbfd419 330 struct thread_db *thread_db = current_process ()->private->thread_db;
5f7d1694 331
cdbfd419 332 err = thread_db->td_thr_event_enable_p (th_p, 1);
24a09b5f
DJ
333 if (err != TD_OK)
334 error ("Cannot enable thread event reporting for %d: %s",
335 ti_p->ti_lid, thread_db_err_str (err));
336 }
5f7d1694
PP
337
338 return 1;
339}
340
341/* Attach thread if we haven't seen it yet.
342 Increment *COUNTER if we have attached a new thread.
343 Return false on failure. */
344
345static int
346maybe_attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p,
347 int *counter)
348{
349 struct lwp_info *lwp;
350
351 lwp = find_lwp_pid (pid_to_ptid (ti_p->ti_lid));
352 if (lwp != NULL)
353 return 1;
354
355 if (!attach_thread (th_p, ti_p))
356 return 0;
357
358 if (counter != NULL)
359 *counter += 1;
360
361 return 1;
0d62e5e8
DJ
362}
363
364static int
365find_new_threads_callback (const td_thrhandle_t *th_p, void *data)
366{
367 td_thrinfo_t ti;
368 td_err_e err;
cdbfd419 369 struct thread_db *thread_db = current_process ()->private->thread_db;
0d62e5e8 370
cdbfd419 371 err = thread_db->td_thr_get_info_p (th_p, &ti);
0d62e5e8
DJ
372 if (err != TD_OK)
373 error ("Cannot get thread info: %s", thread_db_err_str (err));
374
375 /* Check for zombies. */
376 if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
377 return 0;
378
5f7d1694
PP
379 if (!maybe_attach_thread (th_p, &ti, (int *) data))
380 {
381 /* Terminate iteration early: we might be looking at stale data in
382 the inferior. The thread_db_find_new_threads will retry. */
383 return 1;
384 }
0d62e5e8
DJ
385
386 return 0;
387}
388
389static void
390thread_db_find_new_threads (void)
391{
392 td_err_e err;
95954743 393 ptid_t ptid = ((struct inferior_list_entry *) current_inferior)->id;
cdbfd419 394 struct thread_db *thread_db = current_process ()->private->thread_db;
5f7d1694 395 int loop, iteration;
0d62e5e8 396
ae13219e
DJ
397 /* This function is only called when we first initialize thread_db.
398 First locate the initial thread. If it is not ready for
399 debugging yet, then stop. */
95954743 400 if (find_one_thread (ptid) == 0)
ae13219e
DJ
401 return;
402
5f7d1694
PP
403 /* Require 4 successive iterations which do not find any new threads.
404 The 4 is a heuristic: there is an inherent race here, and I have
405 seen that 2 iterations in a row are not always sufficient to
406 "capture" all threads. */
407 for (loop = 0, iteration = 0; loop < 4; ++loop, ++iteration)
408 {
409 int new_thread_count = 0;
410
411 /* Iterate over all user-space threads to discover new threads. */
412 err = thread_db->td_ta_thr_iter_p (thread_db->thread_agent,
413 find_new_threads_callback,
414 &new_thread_count,
415 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
416 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
417 if (debug_threads)
418 fprintf (stderr, "Found %d threads in iteration %d.\n",
419 new_thread_count, iteration);
420
421 if (new_thread_count != 0)
422 {
423 /* Found new threads. Restart iteration from beginning. */
424 loop = -1;
425 }
426 }
0d62e5e8
DJ
427 if (err != TD_OK)
428 error ("Cannot find new threads: %s", thread_db_err_str (err));
429}
430
fd500816
DJ
431/* Cache all future symbols that thread_db might request. We can not
432 request symbols at arbitrary states in the remote protocol, only
433 when the client tells us that new symbols are available. So when
434 we load the thread library, make sure to check the entire list. */
435
436static void
437thread_db_look_up_symbols (void)
438{
cdbfd419
PP
439 struct thread_db *thread_db = current_process ()->private->thread_db;
440 const char **sym_list;
fd500816
DJ
441 CORE_ADDR unused;
442
cdbfd419 443 for (sym_list = thread_db->td_symbol_list_p (); *sym_list; sym_list++)
fd500816
DJ
444 look_up_one_symbol (*sym_list, &unused);
445}
446
dae5f5cf
DJ
447int
448thread_db_get_tls_address (struct thread_info *thread, CORE_ADDR offset,
449 CORE_ADDR load_module, CORE_ADDR *address)
450{
dae5f5cf
DJ
451 psaddr_t addr;
452 td_err_e err;
54a0b537 453 struct lwp_info *lwp;
95954743 454 struct thread_info *saved_inferior;
cdbfd419
PP
455 struct process_info *proc;
456 struct thread_db *thread_db;
457
458 proc = get_thread_process (thread);
459 thread_db = proc->private->thread_db;
dae5f5cf 460
7fe519cb 461 /* If the thread layer is not (yet) initialized, fail. */
cdbfd419 462 if (!proc->all_symbols_looked_up)
7fe519cb
UW
463 return TD_ERR;
464
cdbfd419
PP
465 if (thread_db->td_thr_tls_get_addr_p == NULL)
466 return -1;
467
54a0b537
PA
468 lwp = get_thread_lwp (thread);
469 if (!lwp->thread_known)
95954743 470 find_one_thread (lwp->head.id);
54a0b537 471 if (!lwp->thread_known)
dae5f5cf
DJ
472 return TD_NOTHR;
473
95954743
PA
474 saved_inferior = current_inferior;
475 current_inferior = thread;
186947f7
DJ
476 /* Note the cast through uintptr_t: this interface only works if
477 a target address fits in a psaddr_t, which is a host pointer.
478 So a 32-bit debugger can not access 64-bit TLS through this. */
cdbfd419
PP
479 err = thread_db->td_thr_tls_get_addr_p (&lwp->th,
480 (psaddr_t) (uintptr_t) load_module,
481 offset, &addr);
95954743 482 current_inferior = saved_inferior;
dae5f5cf
DJ
483 if (err == TD_OK)
484 {
186947f7 485 *address = (CORE_ADDR) (uintptr_t) addr;
dae5f5cf
DJ
486 return 0;
487 }
488 else
489 return err;
cdbfd419
PP
490}
491
96f15937
PP
492#ifdef USE_LIBTHREAD_DB_DIRECTLY
493
494static int
495thread_db_load_search (void)
496{
497 td_err_e err;
498 struct thread_db tdb;
499 struct process_info *proc = current_process ();
500
501 if (proc->private->thread_db != NULL)
502 fatal ("unexpected: proc->private->thread_db != NULL");
503
504 tdb.td_ta_new_p = &td_ta_new;
505
506 /* Attempt to open a connection to the thread library. */
507 err = tdb.td_ta_new_p (&tdb.proc_handle, &tdb.thread_agent);
508 if (err != TD_OK)
509 {
510 if (debug_threads)
511 fprintf (stderr, "td_ta_new(): %s\n", thread_db_err_str (err));
512 return 0;
513 }
514
515 tdb.td_ta_map_lwp2thr_p = &td_ta_map_lwp2thr;
516 tdb.td_thr_get_info_p = &td_thr_get_info;
517 tdb.td_ta_thr_iter_p = &td_ta_thr_iter;
518 tdb.td_symbol_list_p = &td_symbol_list;
519
520 /* This is required only when thread_db_use_events is on. */
521 tdb.td_thr_event_enable_p = &td_thr_event_enable;
522
523 /* These are not essential. */
524 tdb.td_ta_event_addr_p = &td_ta_event_addr;
525 tdb.td_ta_set_event_p = &td_ta_set_event;
526 tdb.td_ta_event_getmsg_p = &td_ta_event_getmsg;
527 tdb.td_thr_tls_get_addr_p = &td_thr_tls_get_addr;
528
529 proc->private->thread_db = xmalloc (sizeof (tdb));
530 memcpy (proc->private->thread_db, &tdb, sizeof (tdb));
531
532 return 1;
533}
534
535#else
536
cdbfd419
PP
537static int
538try_thread_db_load_1 (void *handle)
539{
540 td_err_e err;
541 struct thread_db tdb;
542 struct process_info *proc = current_process ();
543
544 if (proc->private->thread_db != NULL)
545 fatal ("unexpected: proc->private->thread_db != NULL");
546
547 tdb.handle = handle;
548
549 /* Initialize pointers to the dynamic library functions we will use.
550 Essential functions first. */
551
552#define CHK(required, a) \
553 do \
554 { \
555 if ((a) == NULL) \
556 { \
557 if (debug_threads) \
558 fprintf (stderr, "dlsym: %s\n", dlerror ()); \
559 if (required) \
560 return 0; \
561 } \
562 } \
563 while (0)
564
565 CHK (1, tdb.td_ta_new_p = dlsym (handle, "td_ta_new"));
566
567 /* Attempt to open a connection to the thread library. */
568 err = tdb.td_ta_new_p (&tdb.proc_handle, &tdb.thread_agent);
569 if (err != TD_OK)
570 {
571 if (debug_threads)
572 fprintf (stderr, "td_ta_new(): %s\n", thread_db_err_str (err));
573 return 0;
574 }
575
576 CHK (1, tdb.td_ta_map_lwp2thr_p = dlsym (handle, "td_ta_map_lwp2thr"));
577 CHK (1, tdb.td_thr_get_info_p = dlsym (handle, "td_thr_get_info"));
578 CHK (1, tdb.td_ta_thr_iter_p = dlsym (handle, "td_ta_thr_iter"));
579 CHK (1, tdb.td_symbol_list_p = dlsym (handle, "td_symbol_list"));
580
581 /* This is required only when thread_db_use_events is on. */
582 CHK (thread_db_use_events,
583 tdb.td_thr_event_enable_p = dlsym (handle, "td_thr_event_enable"));
584
585 /* These are not essential. */
586 CHK (0, tdb.td_ta_event_addr_p = dlsym (handle, "td_ta_event_addr"));
587 CHK (0, tdb.td_ta_set_event_p = dlsym (handle, "td_ta_set_event"));
588 CHK (0, tdb.td_ta_event_getmsg_p = dlsym (handle, "td_ta_event_getmsg"));
589 CHK (0, tdb.td_thr_tls_get_addr_p = dlsym (handle, "td_thr_tls_get_addr"));
590
591#undef CHK
592
593 proc->private->thread_db = xmalloc (sizeof (tdb));
594 memcpy (proc->private->thread_db, &tdb, sizeof (tdb));
595
596 return 1;
597}
598
10e86dd7
DE
599#ifdef HAVE_DLADDR
600
cdbfd419
PP
601/* Lookup a library in which given symbol resides.
602 Note: this is looking in the GDBSERVER process, not in the inferior.
603 Returns library name, or NULL. */
604
605static const char *
606dladdr_to_soname (const void *addr)
607{
608 Dl_info info;
609
610 if (dladdr (addr, &info) != 0)
611 return info.dli_fname;
612 return NULL;
613}
614
10e86dd7
DE
615#endif
616
cdbfd419
PP
617static int
618try_thread_db_load (const char *library)
619{
620 void *handle;
621
622 if (debug_threads)
623 fprintf (stderr, "Trying host libthread_db library: %s.\n",
624 library);
625 handle = dlopen (library, RTLD_NOW);
626 if (handle == NULL)
627 {
628 if (debug_threads)
629 fprintf (stderr, "dlopen failed: %s.\n", dlerror ());
630 return 0;
631 }
632
10e86dd7 633#ifdef HAVE_DLADDR
cdbfd419
PP
634 if (debug_threads && strchr (library, '/') == NULL)
635 {
636 void *td_init;
637
638 td_init = dlsym (handle, "td_init");
639 if (td_init != NULL)
640 {
641 const char *const libpath = dladdr_to_soname (td_init);
642
643 if (libpath != NULL)
644 fprintf (stderr, "Host %s resolved to: %s.\n",
645 library, libpath);
646 }
647 }
10e86dd7 648#endif
cdbfd419
PP
649
650 if (try_thread_db_load_1 (handle))
651 return 1;
652
653 /* This library "refused" to work on current inferior. */
654 dlclose (handle);
655 return 0;
656}
657
658static int
659thread_db_load_search (void)
660{
661 char path[PATH_MAX];
662 const char *search_path;
663 int rc = 0;
664
665 if (libthread_db_search_path == NULL)
666 libthread_db_search_path = xstrdup (LIBTHREAD_DB_SEARCH_PATH);
667
668 search_path = libthread_db_search_path;
669 while (*search_path)
670 {
671 const char *end = strchr (search_path, ':');
672 if (end)
673 {
674 size_t len = end - search_path;
675 if (len + 1 + strlen (LIBTHREAD_DB_SO) + 1 > sizeof (path))
676 {
677 char *cp = xmalloc (len + 1);
678 memcpy (cp, search_path, len);
679 cp[len] = '\0';
680 warning ("libthread_db_search_path component too long, "
681 "ignored: %s.", cp);
682 free (cp);
683 search_path += len + 1;
684 continue;
685 }
686 memcpy (path, search_path, len);
687 path[len] = '\0';
688 search_path += len + 1;
689 }
690 else
691 {
692 size_t len = strlen (search_path);
693
694 if (len + 1 + strlen (LIBTHREAD_DB_SO) + 1 > sizeof (path))
695 {
696 warning ("libthread_db_search_path component too long,"
697 " ignored: %s.", search_path);
698 break;
699 }
700 memcpy (path, search_path, len + 1);
701 search_path += len;
702 }
703 strcat (path, "/");
704 strcat (path, LIBTHREAD_DB_SO);
705 if (debug_threads)
706 fprintf (stderr, "thread_db_load_search trying %s\n", path);
707 if (try_thread_db_load (path))
708 {
709 rc = 1;
710 break;
711 }
712 }
713 if (rc == 0)
714 rc = try_thread_db_load (LIBTHREAD_DB_SO);
715
716 if (debug_threads)
717 fprintf (stderr, "thread_db_load_search returning %d\n", rc);
718 return rc;
dae5f5cf
DJ
719}
720
96f15937
PP
721#endif /* USE_LIBTHREAD_DB_DIRECTLY */
722
0d62e5e8 723int
24a09b5f 724thread_db_init (int use_events)
0d62e5e8 725{
95954743 726 struct process_info *proc = current_process ();
0d62e5e8 727
fd500816
DJ
728 /* FIXME drow/2004-10-16: This is the "overall process ID", which
729 GNU/Linux calls tgid, "thread group ID". When we support
730 attaching to threads, the original thread may not be the correct
731 thread. We would have to get the process ID from /proc for NPTL.
732 For LinuxThreads we could do something similar: follow the chain
733 of parent processes until we find the highest one we're attached
734 to, and use its tgid.
735
736 This isn't the only place in gdbserver that assumes that the first
737 process in the list is the thread group leader. */
ea025f5f 738
24a09b5f
DJ
739 thread_db_use_events = use_events;
740
cdbfd419 741 if (thread_db_load_search ())
0d62e5e8 742 {
24a09b5f 743 if (use_events && thread_db_enable_reporting () == 0)
cdbfd419
PP
744 {
745 /* Keep trying; maybe event reporting will work later. */
fd7dd3e6 746 thread_db_free (proc, 0);
cdbfd419
PP
747 return 0;
748 }
0d62e5e8 749 thread_db_find_new_threads ();
fd500816 750 thread_db_look_up_symbols ();
95954743 751 proc->all_symbols_looked_up = 1;
0d62e5e8 752 return 1;
cdbfd419 753 }
0d62e5e8 754
cdbfd419
PP
755 return 0;
756}
757
758/* Disconnect from libthread_db and free resources. */
759
760void
fd7dd3e6 761thread_db_free (struct process_info *proc, int detaching)
cdbfd419
PP
762{
763 struct thread_db *thread_db = proc->private->thread_db;
764 if (thread_db)
765 {
766 td_err_e (*td_ta_delete_p) (td_thragent_t *);
21e1bee4
PP
767 td_err_e (*td_ta_clear_event_p) (const td_thragent_t *ta,
768 td_thr_events_t *event);
769
fd7dd3e6 770#ifndef USE_LIBTHREAD_DB_DIRECTLY
21e1bee4 771 td_ta_clear_event_p = dlsym (thread_db->handle, "td_ta_clear_event");
fd7dd3e6
PA
772 td_ta_delete_p = dlsym (thread_db->handle, "td_ta_delete");
773#else
774 td_ta_delete_p = &td_ta_delete;
775 td_ta_clear_event_p = &td_ta_clear_event;
776#endif
777
778 if (detaching && td_ta_clear_event_p != NULL)
21e1bee4
PP
779 {
780 td_thr_events_t events;
781
fd7dd3e6
PA
782 /* Set the process wide mask saying we aren't interested
783 in any events anymore. */
21e1bee4
PP
784 td_event_fillset (&events);
785 (*td_ta_clear_event_p) (thread_db->thread_agent, &events);
786 }
cdbfd419 787
cdbfd419
PP
788 if (td_ta_delete_p != NULL)
789 (*td_ta_delete_p) (thread_db->thread_agent);
790
fd7dd3e6 791#ifndef USE_LIBTHREAD_DB_DIRECTLY
cdbfd419 792 dlclose (thread_db->handle);
96f15937
PP
793#endif /* USE_LIBTHREAD_DB_DIRECTLY */
794
cdbfd419
PP
795 free (thread_db);
796 proc->private->thread_db = NULL;
797 }
798}
799
800/* Handle "set libthread-db-search-path" monitor command and return 1.
801 For any other command, return 0. */
802
803int
804thread_db_handle_monitor_command (char *mon)
805{
806 if (strncmp (mon, "set libthread-db-search-path ", 29) == 0)
807 {
808 const char *cp = mon + 29;
809
810 if (libthread_db_search_path != NULL)
811 free (libthread_db_search_path);
812
813 /* Skip leading space (if any). */
814 while (isspace (*cp))
815 ++cp;
816
817 libthread_db_search_path = xstrdup (cp);
818
819 monitor_output ("libthread-db-search-path set to `");
820 monitor_output (libthread_db_search_path);
821 monitor_output ("'\n");
822 return 1;
0d62e5e8
DJ
823 }
824
cdbfd419 825 /* Tell server.c to perform default processing. */
0d62e5e8
DJ
826 return 0;
827}
This page took 0.562841 seconds and 4 git commands to generate.