1 /* Thread management interface, for the remote server for GDB.
2 Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008, 2009, 2010
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 /* 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
;
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
,
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
,
75 td_err_e (*td_thr_get_info_p
) (const td_thrhandle_t
*th
,
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
,
85 size_t offset
, psaddr_t
*address
);
86 const char ** (*td_symbol_list_p
) (void);
89 static char *libthread_db_search_path
;
91 static int find_one_thread (ptid_t
);
92 static int find_new_threads_callback (const td_thrhandle_t
*th_p
, void *data
);
95 thread_db_err_str (td_err_e err
)
102 return "generic 'call succeeded'";
104 return "generic error";
106 return "no thread to satisfy query";
108 return "no sync handle to satisfy query";
110 return "no LWP to satisfy query";
112 return "invalid process handle";
114 return "invalid thread handle";
116 return "invalid synchronization handle";
118 return "invalid thread agent";
120 return "invalid key";
122 return "no event message for getmsg";
124 return "FPU register set not available";
126 return "application not linked with libthread";
128 return "requested event is not supported";
130 return "capability not available";
132 return "debugger service failed";
134 return "operation not applicable to";
136 return "no thread-specific data for this thread";
138 return "malloc failed";
140 return "only part of register set was written/read";
142 return "X register set not available for this thread";
143 #ifdef HAVE_TD_VERSION
145 return "version mismatch between libthread_db and libpthread";
148 snprintf (buf
, sizeof (buf
), "unknown thread_db error '%d'", err
);
155 thread_db_state_str (td_thr_state_e state
)
162 return "stopped by debugger";
171 case TD_THR_STOPPED_ASLEEP
:
172 return "stopped by debugger AND blocked";
174 snprintf (buf
, sizeof (buf
), "unknown thread_db state %d", state
);
181 thread_db_create_event (CORE_ADDR where
)
185 struct lwp_info
*lwp
;
186 struct thread_db
*thread_db
= current_process ()->private->thread_db
;
188 if (thread_db
->td_ta_event_getmsg_p
== NULL
)
189 fatal ("unexpected thread_db->td_ta_event_getmsg_p == NULL");
192 fprintf (stderr
, "Thread creation event.\n");
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). */
198 err
= thread_db
->td_ta_event_getmsg_p (thread_db
->thread_agent
, &msg
);
200 fprintf (stderr
, "thread getmsg err: %s\n",
201 thread_db_err_str (err
));
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
206 lwp
= get_thread_lwp (current_inferior
);
207 if (lwp
->thread_known
== 0)
208 find_one_thread (lwp
->head
.id
);
210 /* msg.event == TD_EVENT_CREATE */
212 find_new_threads_callback (msg
.th_p
, NULL
);
218 thread_db_enable_reporting (void)
220 td_thr_events_t events
;
223 struct thread_db
*thread_db
= current_process ()->private->thread_db
;
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. */
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
);
235 err
= thread_db
->td_ta_set_event_p (thread_db
->thread_agent
, &events
);
238 warning ("Unable to set global thread event mask: %s",
239 thread_db_err_str (err
));
243 /* Get address for thread creation breakpoint. */
244 err
= thread_db
->td_ta_event_addr_p (thread_db
->thread_agent
, TD_CREATE
,
248 warning ("Unable to get location for thread creation breakpoint: %s",
249 thread_db_err_str (err
));
252 thread_db
->td_create_bp
253 = set_breakpoint_at ((CORE_ADDR
) (unsigned long) notify
.u
.bptaddr
,
254 thread_db_create_event
);
260 find_one_thread (ptid_t ptid
)
265 struct thread_info
*inferior
;
266 struct lwp_info
*lwp
;
267 struct thread_db
*thread_db
= current_process ()->private->thread_db
;
268 int lwpid
= ptid_get_lwp (ptid
);
270 inferior
= (struct thread_info
*) find_inferior_id (&all_threads
, ptid
);
271 lwp
= get_thread_lwp (inferior
);
272 if (lwp
->thread_known
)
275 /* Get information about this thread. */
276 err
= thread_db
->td_ta_map_lwp2thr_p (thread_db
->thread_agent
, lwpid
, &th
);
278 error ("Cannot get thread handle for LWP %d: %s",
279 lwpid
, thread_db_err_str (err
));
281 err
= thread_db
->td_thr_get_info_p (&th
, &ti
);
283 error ("Cannot get thread info for LWP %d: %s",
284 lwpid
, thread_db_err_str (err
));
287 fprintf (stderr
, "Found thread %ld (LWP %d)\n",
288 ti
.ti_tid
, ti
.ti_lid
);
290 if (lwpid
!= ti
.ti_lid
)
292 warning ("PID mismatch! Expected %ld, got %ld",
293 (long) lwpid
, (long) ti
.ti_lid
);
297 if (thread_db_use_events
)
299 err
= thread_db
->td_thr_event_enable_p (&th
, 1);
301 error ("Cannot enable thread event reporting for %d: %s",
302 ti
.ti_lid
, thread_db_err_str (err
));
305 /* If the new thread ID is zero, a final thread ID will be available
306 later. Do not enable thread debugging yet. */
310 lwp
->thread_known
= 1;
316 /* Attach a thread. Return true on success. */
319 attach_thread (const td_thrhandle_t
*th_p
, td_thrinfo_t
*ti_p
)
321 struct lwp_info
*lwp
;
324 fprintf (stderr
, "Attaching to thread %ld (LWP %d)\n",
325 ti_p
->ti_tid
, ti_p
->ti_lid
);
326 linux_attach_lwp (ti_p
->ti_lid
);
327 lwp
= find_lwp_pid (pid_to_ptid (ti_p
->ti_lid
));
330 warning ("Could not attach to thread %ld (LWP %d)\n",
331 ti_p
->ti_tid
, ti_p
->ti_lid
);
335 lwp
->thread_known
= 1;
338 if (thread_db_use_events
)
341 struct thread_db
*thread_db
= current_process ()->private->thread_db
;
343 err
= thread_db
->td_thr_event_enable_p (th_p
, 1);
345 error ("Cannot enable thread event reporting for %d: %s",
346 ti_p
->ti_lid
, thread_db_err_str (err
));
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. */
357 maybe_attach_thread (const td_thrhandle_t
*th_p
, td_thrinfo_t
*ti_p
,
360 struct lwp_info
*lwp
;
362 lwp
= find_lwp_pid (pid_to_ptid (ti_p
->ti_lid
));
366 if (!attach_thread (th_p
, ti_p
))
376 find_new_threads_callback (const td_thrhandle_t
*th_p
, void *data
)
380 struct thread_db
*thread_db
= current_process ()->private->thread_db
;
382 err
= thread_db
->td_thr_get_info_p (th_p
, &ti
);
384 error ("Cannot get thread info: %s", thread_db_err_str (err
));
386 /* Check for zombies. */
387 if (ti
.ti_state
== TD_THR_UNKNOWN
|| ti
.ti_state
== TD_THR_ZOMBIE
)
390 if (!maybe_attach_thread (th_p
, &ti
, (int *) data
))
392 /* Terminate iteration early: we might be looking at stale data in
393 the inferior. The thread_db_find_new_threads will retry. */
401 thread_db_find_new_threads (void)
404 ptid_t ptid
= ((struct inferior_list_entry
*) current_inferior
)->id
;
405 struct thread_db
*thread_db
= current_process ()->private->thread_db
;
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. */
411 if (find_one_thread (ptid
) == 0)
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
)
420 int new_thread_count
= 0;
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
,
426 TD_THR_ANY_STATE
, TD_THR_LOWEST_PRIORITY
,
427 TD_SIGNO_MASK
, TD_THR_ANY_USER_FLAGS
);
429 fprintf (stderr
, "Found %d threads in iteration %d.\n",
430 new_thread_count
, iteration
);
432 if (new_thread_count
!= 0)
434 /* Found new threads. Restart iteration from beginning. */
439 error ("Cannot find new threads: %s", thread_db_err_str (err
));
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. */
448 thread_db_look_up_symbols (void)
450 struct thread_db
*thread_db
= current_process ()->private->thread_db
;
451 const char **sym_list
;
454 for (sym_list
= thread_db
->td_symbol_list_p (); *sym_list
; sym_list
++)
455 look_up_one_symbol (*sym_list
, &unused
);
459 thread_db_get_tls_address (struct thread_info
*thread
, CORE_ADDR offset
,
460 CORE_ADDR load_module
, CORE_ADDR
*address
)
464 struct lwp_info
*lwp
;
465 struct thread_info
*saved_inferior
;
466 struct process_info
*proc
;
467 struct thread_db
*thread_db
;
469 proc
= get_thread_process (thread
);
470 thread_db
= proc
->private->thread_db
;
472 /* If the thread layer is not (yet) initialized, fail. */
473 if (!proc
->all_symbols_looked_up
)
476 if (thread_db
->td_thr_tls_get_addr_p
== NULL
)
479 lwp
= get_thread_lwp (thread
);
480 if (!lwp
->thread_known
)
481 find_one_thread (lwp
->head
.id
);
482 if (!lwp
->thread_known
)
485 saved_inferior
= current_inferior
;
486 current_inferior
= thread
;
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. */
490 err
= thread_db
->td_thr_tls_get_addr_p (&lwp
->th
,
491 (psaddr_t
) (uintptr_t) load_module
,
493 current_inferior
= saved_inferior
;
496 *address
= (CORE_ADDR
) (uintptr_t) addr
;
503 #ifdef USE_LIBTHREAD_DB_DIRECTLY
506 thread_db_load_search (void)
509 struct thread_db tdb
;
510 struct process_info
*proc
= current_process ();
512 if (proc
->private->thread_db
!= NULL
)
513 fatal ("unexpected: proc->private->thread_db != NULL");
515 memset (&tdb
, 0, sizeof (tdb
));
517 tdb
.td_ta_new_p
= &td_ta_new
;
519 /* Attempt to open a connection to the thread library. */
520 err
= tdb
.td_ta_new_p (&tdb
.proc_handle
, &tdb
.thread_agent
);
524 fprintf (stderr
, "td_ta_new(): %s\n", thread_db_err_str (err
));
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
;
533 /* This is required only when thread_db_use_events is on. */
534 tdb
.td_thr_event_enable_p
= &td_thr_event_enable
;
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
;
542 proc
->private->thread_db
= xmalloc (sizeof (tdb
));
543 memcpy (proc
->private->thread_db
, &tdb
, sizeof (tdb
));
551 try_thread_db_load_1 (void *handle
)
554 struct thread_db tdb
;
555 struct process_info
*proc
= current_process ();
557 if (proc
->private->thread_db
!= NULL
)
558 fatal ("unexpected: proc->private->thread_db != NULL");
560 memset (&tdb
, 0, sizeof (tdb
));
564 /* Initialize pointers to the dynamic library functions we will use.
565 Essential functions first. */
567 #define CHK(required, a) \
573 fprintf (stderr, "dlsym: %s\n", dlerror ()); \
580 CHK (1, tdb
.td_ta_new_p
= dlsym (handle
, "td_ta_new"));
582 /* Attempt to open a connection to the thread library. */
583 err
= tdb
.td_ta_new_p (&tdb
.proc_handle
, &tdb
.thread_agent
);
587 fprintf (stderr
, "td_ta_new(): %s\n", thread_db_err_str (err
));
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"));
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"));
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"));
608 proc
->private->thread_db
= xmalloc (sizeof (tdb
));
609 memcpy (proc
->private->thread_db
, &tdb
, sizeof (tdb
));
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. */
621 dladdr_to_soname (const void *addr
)
625 if (dladdr (addr
, &info
) != 0)
626 return info
.dli_fname
;
633 try_thread_db_load (const char *library
)
638 fprintf (stderr
, "Trying host libthread_db library: %s.\n",
640 handle
= dlopen (library
, RTLD_NOW
);
644 fprintf (stderr
, "dlopen failed: %s.\n", dlerror ());
649 if (debug_threads
&& strchr (library
, '/') == NULL
)
653 td_init
= dlsym (handle
, "td_init");
656 const char *const libpath
= dladdr_to_soname (td_init
);
659 fprintf (stderr
, "Host %s resolved to: %s.\n",
665 if (try_thread_db_load_1 (handle
))
668 /* This library "refused" to work on current inferior. */
674 thread_db_load_search (void)
677 const char *search_path
;
680 if (libthread_db_search_path
== NULL
)
681 libthread_db_search_path
= xstrdup (LIBTHREAD_DB_SEARCH_PATH
);
683 search_path
= libthread_db_search_path
;
686 const char *end
= strchr (search_path
, ':');
689 size_t len
= end
- search_path
;
690 if (len
+ 1 + strlen (LIBTHREAD_DB_SO
) + 1 > sizeof (path
))
692 char *cp
= xmalloc (len
+ 1);
693 memcpy (cp
, search_path
, len
);
695 warning ("libthread_db_search_path component too long, "
698 search_path
+= len
+ 1;
701 memcpy (path
, search_path
, len
);
703 search_path
+= len
+ 1;
707 size_t len
= strlen (search_path
);
709 if (len
+ 1 + strlen (LIBTHREAD_DB_SO
) + 1 > sizeof (path
))
711 warning ("libthread_db_search_path component too long,"
712 " ignored: %s.", search_path
);
715 memcpy (path
, search_path
, len
+ 1);
719 strcat (path
, LIBTHREAD_DB_SO
);
721 fprintf (stderr
, "thread_db_load_search trying %s\n", path
);
722 if (try_thread_db_load (path
))
729 rc
= try_thread_db_load (LIBTHREAD_DB_SO
);
732 fprintf (stderr
, "thread_db_load_search returning %d\n", rc
);
736 #endif /* USE_LIBTHREAD_DB_DIRECTLY */
739 thread_db_init (int use_events
)
741 struct process_info
*proc
= current_process ();
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.
751 This isn't the only place in gdbserver that assumes that the first
752 process in the list is the thread group leader. */
754 thread_db_use_events
= use_events
;
756 if (thread_db_load_search ())
758 if (use_events
&& thread_db_enable_reporting () == 0)
760 /* Keep trying; maybe event reporting will work later. */
761 thread_db_mourn (proc
);
764 thread_db_find_new_threads ();
765 thread_db_look_up_symbols ();
766 proc
->all_symbols_looked_up
= 1;
774 any_thread_of (struct inferior_list_entry
*entry
, void *args
)
778 if (ptid_get_pid (entry
->id
) == *pid_p
)
785 switch_to_process (struct process_info
*proc
)
787 int pid
= pid_of (proc
);
790 (struct thread_info
*) find_inferior (&all_threads
,
791 any_thread_of
, &pid
);
794 /* Disconnect from libthread_db and free resources. */
797 disable_thread_event_reporting (struct process_info
*proc
)
799 struct thread_db
*thread_db
= proc
->private->thread_db
;
802 td_err_e (*td_ta_clear_event_p
) (const td_thragent_t
*ta
,
803 td_thr_events_t
*event
);
805 #ifndef USE_LIBTHREAD_DB_DIRECTLY
806 td_ta_clear_event_p
= dlsym (thread_db
->handle
, "td_ta_clear_event");
808 td_ta_clear_event_p
= &td_ta_clear_event
;
811 if (td_ta_clear_event_p
!= NULL
)
813 struct thread_info
*saved_inferior
= current_inferior
;
814 td_thr_events_t events
;
816 switch_to_process (proc
);
818 /* Set the process wide mask saying we aren't interested
819 in any events anymore. */
820 td_event_fillset (&events
);
821 (*td_ta_clear_event_p
) (thread_db
->thread_agent
, &events
);
823 current_inferior
= saved_inferior
;
829 remove_thread_event_breakpoints (struct process_info
*proc
)
831 struct thread_db
*thread_db
= proc
->private->thread_db
;
833 if (thread_db
->td_create_bp
!= NULL
)
835 struct thread_info
*saved_inferior
= current_inferior
;
837 switch_to_process (proc
);
839 delete_breakpoint (thread_db
->td_create_bp
);
840 thread_db
->td_create_bp
= NULL
;
842 current_inferior
= saved_inferior
;
847 thread_db_detach (struct process_info
*proc
)
849 struct thread_db
*thread_db
= proc
->private->thread_db
;
853 disable_thread_event_reporting (proc
);
854 remove_thread_event_breakpoints (proc
);
858 /* Disconnect from libthread_db and free resources. */
861 thread_db_mourn (struct process_info
*proc
)
863 struct thread_db
*thread_db
= proc
->private->thread_db
;
866 td_err_e (*td_ta_delete_p
) (td_thragent_t
*);
868 #ifndef USE_LIBTHREAD_DB_DIRECTLY
869 td_ta_delete_p
= dlsym (thread_db
->handle
, "td_ta_delete");
871 td_ta_delete_p
= &td_ta_delete
;
874 if (td_ta_delete_p
!= NULL
)
875 (*td_ta_delete_p
) (thread_db
->thread_agent
);
877 #ifndef USE_LIBTHREAD_DB_DIRECTLY
878 dlclose (thread_db
->handle
);
879 #endif /* USE_LIBTHREAD_DB_DIRECTLY */
882 proc
->private->thread_db
= NULL
;
886 /* Handle "set libthread-db-search-path" monitor command and return 1.
887 For any other command, return 0. */
890 thread_db_handle_monitor_command (char *mon
)
892 if (strncmp (mon
, "set libthread-db-search-path ", 29) == 0)
894 const char *cp
= mon
+ 29;
896 if (libthread_db_search_path
!= NULL
)
897 free (libthread_db_search_path
);
899 /* Skip leading space (if any). */
900 while (isspace (*cp
))
903 libthread_db_search_path
= xstrdup (cp
);
905 monitor_output ("libthread-db-search-path set to `");
906 monitor_output (libthread_db_search_path
);
907 monitor_output ("'\n");
911 /* Tell server.c to perform default processing. */