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