2009-10-29 Paul Pluzhnikov <ppluzhnikov@google.com>
[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,
74 void *map_address,
75 size_t offset, void **address);
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
599/* Lookup a library in which given symbol resides.
600 Note: this is looking in the GDBSERVER process, not in the inferior.
601 Returns library name, or NULL. */
602
603static const char *
604dladdr_to_soname (const void *addr)
605{
606 Dl_info info;
607
608 if (dladdr (addr, &info) != 0)
609 return info.dli_fname;
610 return NULL;
611}
612
613static int
614try_thread_db_load (const char *library)
615{
616 void *handle;
617
618 if (debug_threads)
619 fprintf (stderr, "Trying host libthread_db library: %s.\n",
620 library);
621 handle = dlopen (library, RTLD_NOW);
622 if (handle == NULL)
623 {
624 if (debug_threads)
625 fprintf (stderr, "dlopen failed: %s.\n", dlerror ());
626 return 0;
627 }
628
629 if (debug_threads && strchr (library, '/') == NULL)
630 {
631 void *td_init;
632
633 td_init = dlsym (handle, "td_init");
634 if (td_init != NULL)
635 {
636 const char *const libpath = dladdr_to_soname (td_init);
637
638 if (libpath != NULL)
639 fprintf (stderr, "Host %s resolved to: %s.\n",
640 library, libpath);
641 }
642 }
643
644 if (try_thread_db_load_1 (handle))
645 return 1;
646
647 /* This library "refused" to work on current inferior. */
648 dlclose (handle);
649 return 0;
650}
651
652static int
653thread_db_load_search (void)
654{
655 char path[PATH_MAX];
656 const char *search_path;
657 int rc = 0;
658
659 if (libthread_db_search_path == NULL)
660 libthread_db_search_path = xstrdup (LIBTHREAD_DB_SEARCH_PATH);
661
662 search_path = libthread_db_search_path;
663 while (*search_path)
664 {
665 const char *end = strchr (search_path, ':');
666 if (end)
667 {
668 size_t len = end - search_path;
669 if (len + 1 + strlen (LIBTHREAD_DB_SO) + 1 > sizeof (path))
670 {
671 char *cp = xmalloc (len + 1);
672 memcpy (cp, search_path, len);
673 cp[len] = '\0';
674 warning ("libthread_db_search_path component too long, "
675 "ignored: %s.", cp);
676 free (cp);
677 search_path += len + 1;
678 continue;
679 }
680 memcpy (path, search_path, len);
681 path[len] = '\0';
682 search_path += len + 1;
683 }
684 else
685 {
686 size_t len = strlen (search_path);
687
688 if (len + 1 + strlen (LIBTHREAD_DB_SO) + 1 > sizeof (path))
689 {
690 warning ("libthread_db_search_path component too long,"
691 " ignored: %s.", search_path);
692 break;
693 }
694 memcpy (path, search_path, len + 1);
695 search_path += len;
696 }
697 strcat (path, "/");
698 strcat (path, LIBTHREAD_DB_SO);
699 if (debug_threads)
700 fprintf (stderr, "thread_db_load_search trying %s\n", path);
701 if (try_thread_db_load (path))
702 {
703 rc = 1;
704 break;
705 }
706 }
707 if (rc == 0)
708 rc = try_thread_db_load (LIBTHREAD_DB_SO);
709
710 if (debug_threads)
711 fprintf (stderr, "thread_db_load_search returning %d\n", rc);
712 return rc;
dae5f5cf
DJ
713}
714
96f15937
PP
715#endif /* USE_LIBTHREAD_DB_DIRECTLY */
716
0d62e5e8 717int
24a09b5f 718thread_db_init (int use_events)
0d62e5e8 719{
95954743 720 struct process_info *proc = current_process ();
0d62e5e8 721
fd500816
DJ
722 /* FIXME drow/2004-10-16: This is the "overall process ID", which
723 GNU/Linux calls tgid, "thread group ID". When we support
724 attaching to threads, the original thread may not be the correct
725 thread. We would have to get the process ID from /proc for NPTL.
726 For LinuxThreads we could do something similar: follow the chain
727 of parent processes until we find the highest one we're attached
728 to, and use its tgid.
729
730 This isn't the only place in gdbserver that assumes that the first
731 process in the list is the thread group leader. */
ea025f5f 732
24a09b5f
DJ
733 thread_db_use_events = use_events;
734
cdbfd419 735 if (thread_db_load_search ())
0d62e5e8 736 {
24a09b5f 737 if (use_events && thread_db_enable_reporting () == 0)
cdbfd419
PP
738 {
739 /* Keep trying; maybe event reporting will work later. */
740 thread_db_free (proc);
741 return 0;
742 }
0d62e5e8 743 thread_db_find_new_threads ();
fd500816 744 thread_db_look_up_symbols ();
95954743 745 proc->all_symbols_looked_up = 1;
0d62e5e8 746 return 1;
cdbfd419 747 }
0d62e5e8 748
cdbfd419
PP
749 return 0;
750}
751
752/* Disconnect from libthread_db and free resources. */
753
754void
755thread_db_free (struct process_info *proc)
756{
757 struct thread_db *thread_db = proc->private->thread_db;
758 if (thread_db)
759 {
96f15937 760#ifndef USE_LIBTHREAD_DB_DIRECTLY
cdbfd419
PP
761 td_err_e (*td_ta_delete_p) (td_thragent_t *);
762
763 td_ta_delete_p = dlsym (thread_db->handle, "td_ta_delete");
764 if (td_ta_delete_p != NULL)
765 (*td_ta_delete_p) (thread_db->thread_agent);
766
767 dlclose (thread_db->handle);
96f15937
PP
768#else
769 td_ta_delete (thread_db->thread_agent);
770#endif /* USE_LIBTHREAD_DB_DIRECTLY */
771
cdbfd419
PP
772 free (thread_db);
773 proc->private->thread_db = NULL;
774 }
775}
776
777/* Handle "set libthread-db-search-path" monitor command and return 1.
778 For any other command, return 0. */
779
780int
781thread_db_handle_monitor_command (char *mon)
782{
783 if (strncmp (mon, "set libthread-db-search-path ", 29) == 0)
784 {
785 const char *cp = mon + 29;
786
787 if (libthread_db_search_path != NULL)
788 free (libthread_db_search_path);
789
790 /* Skip leading space (if any). */
791 while (isspace (*cp))
792 ++cp;
793
794 libthread_db_search_path = xstrdup (cp);
795
796 monitor_output ("libthread-db-search-path set to `");
797 monitor_output (libthread_db_search_path);
798 monitor_output ("'\n");
799 return 1;
0d62e5e8
DJ
800 }
801
cdbfd419 802 /* Tell server.c to perform default processing. */
0d62e5e8
DJ
803 return 0;
804}
This page took 0.991246 seconds and 4 git commands to generate.