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