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"
40 /* Structure that identifies the child process for the
41 <proc_service.h> interface. */
42 struct ps_prochandle proc_handle
;
44 /* Connection to the libthread_db library. */
45 td_thragent_t
*thread_agent
;
47 /* Handle of the libthread_db from dlopen. */
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
,
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
,
60 td_err_e (*td_thr_get_info_p
) (const td_thrhandle_t
*th
,
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
,
70 size_t offset
, void **address
);
71 const char ** (*td_symbol_list_p
) (void);
74 static char *libthread_db_search_path
;
76 static int find_one_thread (ptid_t
);
77 static int find_new_threads_callback (const td_thrhandle_t
*th_p
, void *data
);
80 thread_db_err_str (td_err_e err
)
87 return "generic 'call succeeded'";
89 return "generic error";
91 return "no thread to satisfy query";
93 return "no sync handle to satisfy query";
95 return "no LWP to satisfy query";
97 return "invalid process handle";
99 return "invalid thread handle";
101 return "invalid synchronization handle";
103 return "invalid thread agent";
105 return "invalid key";
107 return "no event message for getmsg";
109 return "FPU register set not available";
111 return "application not linked with libthread";
113 return "requested event is not supported";
115 return "capability not available";
117 return "debugger service failed";
119 return "operation not applicable to";
121 return "no thread-specific data for this thread";
123 return "malloc failed";
125 return "only part of register set was written/read";
127 return "X register set not available for this thread";
128 #ifdef HAVE_TD_VERSION
130 return "version mismatch between libthread_db and libpthread";
133 snprintf (buf
, sizeof (buf
), "unknown thread_db error '%d'", err
);
140 thread_db_state_str (td_thr_state_e state
)
147 return "stopped by debugger";
156 case TD_THR_STOPPED_ASLEEP
:
157 return "stopped by debugger AND blocked";
159 snprintf (buf
, sizeof (buf
), "unknown thread_db state %d", state
);
166 thread_db_create_event (CORE_ADDR where
)
170 struct lwp_info
*lwp
;
171 struct thread_db
*thread_db
= current_process ()->private->thread_db
;
173 if (thread_db
->td_ta_event_getmsg_p
== NULL
)
174 fatal ("unexpected thread_db->td_ta_event_getmsg_p == NULL");
177 fprintf (stderr
, "Thread creation event.\n");
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
);
185 fprintf (stderr
, "thread getmsg err: %s\n",
186 thread_db_err_str (err
));
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
191 lwp
= get_thread_lwp (current_inferior
);
192 if (lwp
->thread_known
== 0)
193 find_one_thread (lwp
->head
.id
);
195 /* msg.event == TD_EVENT_CREATE */
197 find_new_threads_callback (msg
.th_p
, NULL
);
203 thread_db_enable_reporting ()
205 td_thr_events_t events
;
208 struct thread_db
*thread_db
= current_process ()->private->thread_db
;
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. */
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
);
220 err
= thread_db
->td_ta_set_event_p (thread_db
->thread_agent
, &events
);
223 warning ("Unable to set global thread event mask: %s",
224 thread_db_err_str (err
));
228 /* Get address for thread creation breakpoint. */
229 err
= thread_db
->td_ta_event_addr_p (thread_db
->thread_agent
, TD_CREATE
,
233 warning ("Unable to get location for thread creation breakpoint: %s",
234 thread_db_err_str (err
));
237 set_breakpoint_at ((CORE_ADDR
) (unsigned long) notify
.u
.bptaddr
,
238 thread_db_create_event
);
244 find_one_thread (ptid_t ptid
)
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
);
254 inferior
= (struct thread_info
*) find_inferior_id (&all_threads
, ptid
);
255 lwp
= get_thread_lwp (inferior
);
256 if (lwp
->thread_known
)
259 /* Get information about this thread. */
260 err
= thread_db
->td_ta_map_lwp2thr_p (thread_db
->thread_agent
, lwpid
, &th
);
262 error ("Cannot get thread handle for LWP %d: %s",
263 lwpid
, thread_db_err_str (err
));
265 err
= thread_db
->td_thr_get_info_p (&th
, &ti
);
267 error ("Cannot get thread info for LWP %d: %s",
268 lwpid
, thread_db_err_str (err
));
271 fprintf (stderr
, "Found thread %ld (LWP %d)\n",
272 ti
.ti_tid
, ti
.ti_lid
);
274 if (lwpid
!= ti
.ti_lid
)
276 warning ("PID mismatch! Expected %ld, got %ld",
277 (long) lwpid
, (long) ti
.ti_lid
);
281 if (thread_db_use_events
)
283 err
= thread_db
->td_thr_event_enable_p (&th
, 1);
285 error ("Cannot enable thread event reporting for %d: %s",
286 ti
.ti_lid
, thread_db_err_str (err
));
289 /* If the new thread ID is zero, a final thread ID will be available
290 later. Do not enable thread debugging yet. */
294 lwp
->thread_known
= 1;
301 maybe_attach_thread (const td_thrhandle_t
*th_p
, td_thrinfo_t
*ti_p
)
304 struct lwp_info
*lwp
;
306 lwp
= find_lwp_pid (pid_to_ptid (ti_p
->ti_lid
));
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
));
317 warning ("Could not attach to thread %ld (LWP %d)\n",
318 ti_p
->ti_tid
, ti_p
->ti_lid
);
322 lwp
->thread_known
= 1;
325 if (thread_db_use_events
)
327 struct thread_db
*thread_db
= current_process ()->private->thread_db
;
328 err
= thread_db
->td_thr_event_enable_p (th_p
, 1);
330 error ("Cannot enable thread event reporting for %d: %s",
331 ti_p
->ti_lid
, thread_db_err_str (err
));
336 find_new_threads_callback (const td_thrhandle_t
*th_p
, void *data
)
340 struct thread_db
*thread_db
= current_process ()->private->thread_db
;
342 err
= thread_db
->td_thr_get_info_p (th_p
, &ti
);
344 error ("Cannot get thread info: %s", thread_db_err_str (err
));
346 /* Check for zombies. */
347 if (ti
.ti_state
== TD_THR_UNKNOWN
|| ti
.ti_state
== TD_THR_ZOMBIE
)
350 maybe_attach_thread (th_p
, &ti
);
356 thread_db_find_new_threads (void)
359 ptid_t ptid
= ((struct inferior_list_entry
*) current_inferior
)->id
;
360 struct thread_db
*thread_db
= current_process ()->private->thread_db
;
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)
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
);
374 error ("Cannot find new threads: %s", thread_db_err_str (err
));
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. */
383 thread_db_look_up_symbols (void)
385 struct thread_db
*thread_db
= current_process ()->private->thread_db
;
386 const char **sym_list
;
389 for (sym_list
= thread_db
->td_symbol_list_p (); *sym_list
; sym_list
++)
390 look_up_one_symbol (*sym_list
, &unused
);
394 thread_db_get_tls_address (struct thread_info
*thread
, CORE_ADDR offset
,
395 CORE_ADDR load_module
, CORE_ADDR
*address
)
399 struct lwp_info
*lwp
;
400 struct thread_info
*saved_inferior
;
401 struct process_info
*proc
;
402 struct thread_db
*thread_db
;
404 proc
= get_thread_process (thread
);
405 thread_db
= proc
->private->thread_db
;
407 /* If the thread layer is not (yet) initialized, fail. */
408 if (!proc
->all_symbols_looked_up
)
411 if (thread_db
->td_thr_tls_get_addr_p
== NULL
)
414 lwp
= get_thread_lwp (thread
);
415 if (!lwp
->thread_known
)
416 find_one_thread (lwp
->head
.id
);
417 if (!lwp
->thread_known
)
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
,
428 current_inferior
= saved_inferior
;
431 *address
= (CORE_ADDR
) (uintptr_t) addr
;
439 try_thread_db_load_1 (void *handle
)
442 struct thread_db tdb
;
443 struct process_info
*proc
= current_process ();
445 if (proc
->private->thread_db
!= NULL
)
446 fatal ("unexpected: proc->private->thread_db != NULL");
450 /* Initialize pointers to the dynamic library functions we will use.
451 Essential functions first. */
453 #define CHK(required, a) \
459 fprintf (stderr, "dlsym: %s\n", dlerror ()); \
466 CHK (1, tdb
.td_ta_new_p
= dlsym (handle
, "td_ta_new"));
468 /* Attempt to open a connection to the thread library. */
469 err
= tdb
.td_ta_new_p (&tdb
.proc_handle
, &tdb
.thread_agent
);
473 fprintf (stderr
, "td_ta_new(): %s\n", thread_db_err_str (err
));
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"));
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"));
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"));
494 proc
->private->thread_db
= xmalloc (sizeof (tdb
));
495 memcpy (proc
->private->thread_db
, &tdb
, sizeof (tdb
));
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. */
505 dladdr_to_soname (const void *addr
)
509 if (dladdr (addr
, &info
) != 0)
510 return info
.dli_fname
;
515 try_thread_db_load (const char *library
)
520 fprintf (stderr
, "Trying host libthread_db library: %s.\n",
522 handle
= dlopen (library
, RTLD_NOW
);
526 fprintf (stderr
, "dlopen failed: %s.\n", dlerror ());
530 if (debug_threads
&& strchr (library
, '/') == NULL
)
534 td_init
= dlsym (handle
, "td_init");
537 const char *const libpath
= dladdr_to_soname (td_init
);
540 fprintf (stderr
, "Host %s resolved to: %s.\n",
545 if (try_thread_db_load_1 (handle
))
548 /* This library "refused" to work on current inferior. */
554 thread_db_load_search (void)
557 const char *search_path
;
560 if (libthread_db_search_path
== NULL
)
561 libthread_db_search_path
= xstrdup (LIBTHREAD_DB_SEARCH_PATH
);
563 search_path
= libthread_db_search_path
;
566 const char *end
= strchr (search_path
, ':');
569 size_t len
= end
- search_path
;
570 if (len
+ 1 + strlen (LIBTHREAD_DB_SO
) + 1 > sizeof (path
))
572 char *cp
= xmalloc (len
+ 1);
573 memcpy (cp
, search_path
, len
);
575 warning ("libthread_db_search_path component too long, "
578 search_path
+= len
+ 1;
581 memcpy (path
, search_path
, len
);
583 search_path
+= len
+ 1;
587 size_t len
= strlen (search_path
);
589 if (len
+ 1 + strlen (LIBTHREAD_DB_SO
) + 1 > sizeof (path
))
591 warning ("libthread_db_search_path component too long,"
592 " ignored: %s.", search_path
);
595 memcpy (path
, search_path
, len
+ 1);
599 strcat (path
, LIBTHREAD_DB_SO
);
601 fprintf (stderr
, "thread_db_load_search trying %s\n", path
);
602 if (try_thread_db_load (path
))
609 rc
= try_thread_db_load (LIBTHREAD_DB_SO
);
612 fprintf (stderr
, "thread_db_load_search returning %d\n", rc
);
617 thread_db_init (int use_events
)
619 struct process_info
*proc
= current_process ();
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.
629 This isn't the only place in gdbserver that assumes that the first
630 process in the list is the thread group leader. */
632 thread_db_use_events
= use_events
;
634 if (thread_db_load_search ())
636 if (use_events
&& thread_db_enable_reporting () == 0)
638 /* Keep trying; maybe event reporting will work later. */
639 thread_db_free (proc
);
642 thread_db_find_new_threads ();
643 thread_db_look_up_symbols ();
644 proc
->all_symbols_looked_up
= 1;
651 /* Disconnect from libthread_db and free resources. */
654 thread_db_free (struct process_info
*proc
)
656 struct thread_db
*thread_db
= proc
->private->thread_db
;
659 td_err_e (*td_ta_delete_p
) (td_thragent_t
*);
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
);
665 dlclose (thread_db
->handle
);
667 proc
->private->thread_db
= NULL
;
671 /* Handle "set libthread-db-search-path" monitor command and return 1.
672 For any other command, return 0. */
675 thread_db_handle_monitor_command (char *mon
)
677 if (strncmp (mon
, "set libthread-db-search-path ", 29) == 0)
679 const char *cp
= mon
+ 29;
681 if (libthread_db_search_path
!= NULL
)
682 free (libthread_db_search_path
);
684 /* Skip leading space (if any). */
685 while (isspace (*cp
))
688 libthread_db_search_path
= xstrdup (cp
);
690 monitor_output ("libthread-db-search-path set to `");
691 monitor_output (libthread_db_search_path
);
692 monitor_output ("'\n");
696 /* Tell server.c to perform default processing. */