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.
5 Contributed by MontaVista Software.
7 This file is part of GDB.
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.
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.
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/>. */
24 #include "linux-low.h"
26 extern int debug_threads;
28 static int thread_db_use_events;
30 #include "gdb_proc_service.h"
31 #include "../gdb_thread_db.h"
33 #ifndef USE_LIBTHREAD_DB_DIRECTLY
43 /* Structure that identifies the child process for the
44 <proc_service.h> interface. */
45 struct ps_prochandle proc_handle;
47 /* Connection to the libthread_db library. */
48 td_thragent_t *thread_agent;
50 #ifndef USE_LIBTHREAD_DB_DIRECTLY
51 /* Handle of the libthread_db from dlopen. */
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,
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,
65 td_err_e (*td_thr_get_info_p) (const td_thrhandle_t *th,
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,
75 size_t offset, psaddr_t *address);
76 const char ** (*td_symbol_list_p) (void);
79 static char *libthread_db_search_path;
81 static int find_one_thread (ptid_t);
82 static int find_new_threads_callback (const td_thrhandle_t *th_p, void *data);
85 thread_db_err_str (td_err_e err)
92 return "generic 'call succeeded'";
94 return "generic error";
96 return "no thread to satisfy query";
98 return "no sync handle to satisfy query";
100 return "no LWP to satisfy query";
102 return "invalid process handle";
104 return "invalid thread handle";
106 return "invalid synchronization handle";
108 return "invalid thread agent";
110 return "invalid key";
112 return "no event message for getmsg";
114 return "FPU register set not available";
116 return "application not linked with libthread";
118 return "requested event is not supported";
120 return "capability not available";
122 return "debugger service failed";
124 return "operation not applicable to";
126 return "no thread-specific data for this thread";
128 return "malloc failed";
130 return "only part of register set was written/read";
132 return "X register set not available for this thread";
133 #ifdef HAVE_TD_VERSION
135 return "version mismatch between libthread_db and libpthread";
138 snprintf (buf, sizeof (buf), "unknown thread_db error '%d'", err);
145 thread_db_state_str (td_thr_state_e state)
152 return "stopped by debugger";
161 case TD_THR_STOPPED_ASLEEP:
162 return "stopped by debugger AND blocked";
164 snprintf (buf, sizeof (buf), "unknown thread_db state %d", state);
171 thread_db_create_event (CORE_ADDR where)
175 struct lwp_info *lwp;
176 struct thread_db *thread_db = current_process ()->private->thread_db;
178 if (thread_db->td_ta_event_getmsg_p == NULL)
179 fatal ("unexpected thread_db->td_ta_event_getmsg_p == NULL");
182 fprintf (stderr, "Thread creation event.\n");
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). */
188 err = thread_db->td_ta_event_getmsg_p (thread_db->thread_agent, &msg);
190 fprintf (stderr, "thread getmsg err: %s\n",
191 thread_db_err_str (err));
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
196 lwp = get_thread_lwp (current_inferior);
197 if (lwp->thread_known == 0)
198 find_one_thread (lwp->head.id);
200 /* msg.event == TD_EVENT_CREATE */
202 find_new_threads_callback (msg.th_p, NULL);
208 thread_db_enable_reporting ()
210 td_thr_events_t events;
213 struct thread_db *thread_db = current_process ()->private->thread_db;
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. */
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);
225 err = thread_db->td_ta_set_event_p (thread_db->thread_agent, &events);
228 warning ("Unable to set global thread event mask: %s",
229 thread_db_err_str (err));
233 /* Get address for thread creation breakpoint. */
234 err = thread_db->td_ta_event_addr_p (thread_db->thread_agent, TD_CREATE,
238 warning ("Unable to get location for thread creation breakpoint: %s",
239 thread_db_err_str (err));
242 set_breakpoint_at ((CORE_ADDR) (unsigned long) notify.u.bptaddr,
243 thread_db_create_event);
249 find_one_thread (ptid_t ptid)
254 struct thread_info *inferior;
255 struct lwp_info *lwp;
256 struct thread_db *thread_db = current_process ()->private->thread_db;
257 int lwpid = ptid_get_lwp (ptid);
259 inferior = (struct thread_info *) find_inferior_id (&all_threads, ptid);
260 lwp = get_thread_lwp (inferior);
261 if (lwp->thread_known)
264 /* Get information about this thread. */
265 err = thread_db->td_ta_map_lwp2thr_p (thread_db->thread_agent, lwpid, &th);
267 error ("Cannot get thread handle for LWP %d: %s",
268 lwpid, thread_db_err_str (err));
270 err = thread_db->td_thr_get_info_p (&th, &ti);
272 error ("Cannot get thread info for LWP %d: %s",
273 lwpid, thread_db_err_str (err));
276 fprintf (stderr, "Found thread %ld (LWP %d)\n",
277 ti.ti_tid, ti.ti_lid);
279 if (lwpid != ti.ti_lid)
281 warning ("PID mismatch! Expected %ld, got %ld",
282 (long) lwpid, (long) ti.ti_lid);
286 if (thread_db_use_events)
288 err = thread_db->td_thr_event_enable_p (&th, 1);
290 error ("Cannot enable thread event reporting for %d: %s",
291 ti.ti_lid, thread_db_err_str (err));
294 /* If the new thread ID is zero, a final thread ID will be available
295 later. Do not enable thread debugging yet. */
299 lwp->thread_known = 1;
305 /* Attach a thread. Return true on success. */
308 attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p)
310 struct lwp_info *lwp;
313 fprintf (stderr, "Attaching to thread %ld (LWP %d)\n",
314 ti_p->ti_tid, ti_p->ti_lid);
315 linux_attach_lwp (ti_p->ti_lid);
316 lwp = find_lwp_pid (pid_to_ptid (ti_p->ti_lid));
319 warning ("Could not attach to thread %ld (LWP %d)\n",
320 ti_p->ti_tid, ti_p->ti_lid);
324 lwp->thread_known = 1;
327 if (thread_db_use_events)
330 struct thread_db *thread_db = current_process ()->private->thread_db;
332 err = thread_db->td_thr_event_enable_p (th_p, 1);
334 error ("Cannot enable thread event reporting for %d: %s",
335 ti_p->ti_lid, thread_db_err_str (err));
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. */
346 maybe_attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p,
349 struct lwp_info *lwp;
351 lwp = find_lwp_pid (pid_to_ptid (ti_p->ti_lid));
355 if (!attach_thread (th_p, ti_p))
365 find_new_threads_callback (const td_thrhandle_t *th_p, void *data)
369 struct thread_db *thread_db = current_process ()->private->thread_db;
371 err = thread_db->td_thr_get_info_p (th_p, &ti);
373 error ("Cannot get thread info: %s", thread_db_err_str (err));
375 /* Check for zombies. */
376 if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
379 if (!maybe_attach_thread (th_p, &ti, (int *) data))
381 /* Terminate iteration early: we might be looking at stale data in
382 the inferior. The thread_db_find_new_threads will retry. */
390 thread_db_find_new_threads (void)
393 ptid_t ptid = ((struct inferior_list_entry *) current_inferior)->id;
394 struct thread_db *thread_db = current_process ()->private->thread_db;
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. */
400 if (find_one_thread (ptid) == 0)
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)
409 int new_thread_count = 0;
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,
415 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
416 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
418 fprintf (stderr, "Found %d threads in iteration %d.\n",
419 new_thread_count, iteration);
421 if (new_thread_count != 0)
423 /* Found new threads. Restart iteration from beginning. */
428 error ("Cannot find new threads: %s", thread_db_err_str (err));
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. */
437 thread_db_look_up_symbols (void)
439 struct thread_db *thread_db = current_process ()->private->thread_db;
440 const char **sym_list;
443 for (sym_list = thread_db->td_symbol_list_p (); *sym_list; sym_list++)
444 look_up_one_symbol (*sym_list, &unused);
448 thread_db_get_tls_address (struct thread_info *thread, CORE_ADDR offset,
449 CORE_ADDR load_module, CORE_ADDR *address)
453 struct lwp_info *lwp;
454 struct thread_info *saved_inferior;
455 struct process_info *proc;
456 struct thread_db *thread_db;
458 proc = get_thread_process (thread);
459 thread_db = proc->private->thread_db;
461 /* If the thread layer is not (yet) initialized, fail. */
462 if (!proc->all_symbols_looked_up)
465 if (thread_db->td_thr_tls_get_addr_p == NULL)
468 lwp = get_thread_lwp (thread);
469 if (!lwp->thread_known)
470 find_one_thread (lwp->head.id);
471 if (!lwp->thread_known)
474 saved_inferior = current_inferior;
475 current_inferior = thread;
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. */
479 err = thread_db->td_thr_tls_get_addr_p (&lwp->th,
480 (psaddr_t) (uintptr_t) load_module,
482 current_inferior = saved_inferior;
485 *address = (CORE_ADDR) (uintptr_t) addr;
492 #ifdef USE_LIBTHREAD_DB_DIRECTLY
495 thread_db_load_search (void)
498 struct thread_db tdb;
499 struct process_info *proc = current_process ();
501 if (proc->private->thread_db != NULL)
502 fatal ("unexpected: proc->private->thread_db != NULL");
504 tdb.td_ta_new_p = &td_ta_new;
506 /* Attempt to open a connection to the thread library. */
507 err = tdb.td_ta_new_p (&tdb.proc_handle, &tdb.thread_agent);
511 fprintf (stderr, "td_ta_new(): %s\n", thread_db_err_str (err));
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;
520 /* This is required only when thread_db_use_events is on. */
521 tdb.td_thr_event_enable_p = &td_thr_event_enable;
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;
529 proc->private->thread_db = xmalloc (sizeof (tdb));
530 memcpy (proc->private->thread_db, &tdb, sizeof (tdb));
538 try_thread_db_load_1 (void *handle)
541 struct thread_db tdb;
542 struct process_info *proc = current_process ();
544 if (proc->private->thread_db != NULL)
545 fatal ("unexpected: proc->private->thread_db != NULL");
549 /* Initialize pointers to the dynamic library functions we will use.
550 Essential functions first. */
552 #define CHK(required, a) \
558 fprintf (stderr, "dlsym: %s\n", dlerror ()); \
565 CHK (1, tdb.td_ta_new_p = dlsym (handle, "td_ta_new"));
567 /* Attempt to open a connection to the thread library. */
568 err = tdb.td_ta_new_p (&tdb.proc_handle, &tdb.thread_agent);
572 fprintf (stderr, "td_ta_new(): %s\n", thread_db_err_str (err));
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"));
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"));
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"));
593 proc->private->thread_db = xmalloc (sizeof (tdb));
594 memcpy (proc->private->thread_db, &tdb, sizeof (tdb));
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. */
606 dladdr_to_soname (const void *addr)
610 if (dladdr (addr, &info) != 0)
611 return info.dli_fname;
618 try_thread_db_load (const char *library)
623 fprintf (stderr, "Trying host libthread_db library: %s.\n",
625 handle = dlopen (library, RTLD_NOW);
629 fprintf (stderr, "dlopen failed: %s.\n", dlerror ());
634 if (debug_threads && strchr (library, '/') == NULL)
638 td_init = dlsym (handle, "td_init");
641 const char *const libpath = dladdr_to_soname (td_init);
644 fprintf (stderr, "Host %s resolved to: %s.\n",
650 if (try_thread_db_load_1 (handle))
653 /* This library "refused" to work on current inferior. */
659 thread_db_load_search (void)
662 const char *search_path;
665 if (libthread_db_search_path == NULL)
666 libthread_db_search_path = xstrdup (LIBTHREAD_DB_SEARCH_PATH);
668 search_path = libthread_db_search_path;
671 const char *end = strchr (search_path, ':');
674 size_t len = end - search_path;
675 if (len + 1 + strlen (LIBTHREAD_DB_SO) + 1 > sizeof (path))
677 char *cp = xmalloc (len + 1);
678 memcpy (cp, search_path, len);
680 warning ("libthread_db_search_path component too long, "
683 search_path += len + 1;
686 memcpy (path, search_path, len);
688 search_path += len + 1;
692 size_t len = strlen (search_path);
694 if (len + 1 + strlen (LIBTHREAD_DB_SO) + 1 > sizeof (path))
696 warning ("libthread_db_search_path component too long,"
697 " ignored: %s.", search_path);
700 memcpy (path, search_path, len + 1);
704 strcat (path, LIBTHREAD_DB_SO);
706 fprintf (stderr, "thread_db_load_search trying %s\n", path);
707 if (try_thread_db_load (path))
714 rc = try_thread_db_load (LIBTHREAD_DB_SO);
717 fprintf (stderr, "thread_db_load_search returning %d\n", rc);
721 #endif /* USE_LIBTHREAD_DB_DIRECTLY */
724 thread_db_init (int use_events)
726 struct process_info *proc = current_process ();
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.
736 This isn't the only place in gdbserver that assumes that the first
737 process in the list is the thread group leader. */
739 thread_db_use_events = use_events;
741 if (thread_db_load_search ())
743 if (use_events && thread_db_enable_reporting () == 0)
745 /* Keep trying; maybe event reporting will work later. */
746 thread_db_free (proc, 0);
749 thread_db_find_new_threads ();
750 thread_db_look_up_symbols ();
751 proc->all_symbols_looked_up = 1;
758 /* Disconnect from libthread_db and free resources. */
761 thread_db_free (struct process_info *proc, int detaching)
763 struct thread_db *thread_db = proc->private->thread_db;
766 td_err_e (*td_ta_delete_p) (td_thragent_t *);
767 td_err_e (*td_ta_clear_event_p) (const td_thragent_t *ta,
768 td_thr_events_t *event);
770 #ifndef USE_LIBTHREAD_DB_DIRECTLY
771 td_ta_clear_event_p = dlsym (thread_db->handle, "td_ta_clear_event");
772 td_ta_delete_p = dlsym (thread_db->handle, "td_ta_delete");
774 td_ta_delete_p = &td_ta_delete;
775 td_ta_clear_event_p = &td_ta_clear_event;
778 if (detaching && td_ta_clear_event_p != NULL)
780 td_thr_events_t events;
782 /* Set the process wide mask saying we aren't interested
783 in any events anymore. */
784 td_event_fillset (&events);
785 (*td_ta_clear_event_p) (thread_db->thread_agent, &events);
788 if (td_ta_delete_p != NULL)
789 (*td_ta_delete_p) (thread_db->thread_agent);
791 #ifndef USE_LIBTHREAD_DB_DIRECTLY
792 dlclose (thread_db->handle);
793 #endif /* USE_LIBTHREAD_DB_DIRECTLY */
796 proc->private->thread_db = NULL;
800 /* Handle "set libthread-db-search-path" monitor command and return 1.
801 For any other command, return 0. */
804 thread_db_handle_monitor_command (char *mon)
806 if (strncmp (mon, "set libthread-db-search-path ", 29) == 0)
808 const char *cp = mon + 29;
810 if (libthread_db_search_path != NULL)
811 free (libthread_db_search_path);
813 /* Skip leading space (if any). */
814 while (isspace (*cp))
817 libthread_db_search_path = xstrdup (cp);
819 monitor_output ("libthread-db-search-path set to `");
820 monitor_output (libthread_db_search_path);
821 monitor_output ("'\n");
825 /* Tell server.c to perform default processing. */