1 /* Thread management interface, for the remote server for GDB.
2 Copyright (C) 2002-2015 Free Software Foundation, Inc.
4 Contributed by MontaVista Software.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "linux-low.h"
25 extern int debug_threads
;
27 #include "gdb_proc_service.h"
28 #include "nat/gdb_thread_db.h"
30 #include "nat/linux-procfs.h"
32 #ifndef USE_LIBTHREAD_DB_DIRECTLY
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 /* If this flag has been set, we've already asked GDB for all
48 symbols we might need; assume symbol cache misses are
50 int all_symbols_looked_up
;
52 #ifndef USE_LIBTHREAD_DB_DIRECTLY
53 /* Handle of the libthread_db from dlopen. */
57 /* Addresses of libthread_db functions. */
58 td_ta_new_ftype
*td_ta_new_p
;
59 td_ta_event_getmsg_ftype
* td_ta_event_getmsg_p
;
60 td_ta_set_event_ftype
*td_ta_set_event_p
;
61 td_ta_event_addr_ftype
*td_ta_event_addr_p
;
62 td_ta_map_lwp2thr_ftype
*td_ta_map_lwp2thr_p
;
63 td_thr_get_info_ftype
*td_thr_get_info_p
;
64 td_ta_thr_iter_ftype
*td_ta_thr_iter_p
;
65 td_thr_tls_get_addr_ftype
*td_thr_tls_get_addr_p
;
66 td_thr_tlsbase_ftype
*td_thr_tlsbase_p
;
67 td_symbol_list_ftype
*td_symbol_list_p
;
70 static char *libthread_db_search_path
;
72 static int find_one_thread (ptid_t
);
73 static int find_new_threads_callback (const td_thrhandle_t
*th_p
, void *data
);
76 thread_db_err_str (td_err_e err
)
83 return "generic 'call succeeded'";
85 return "generic error";
87 return "no thread to satisfy query";
89 return "no sync handle to satisfy query";
91 return "no LWP to satisfy query";
93 return "invalid process handle";
95 return "invalid thread handle";
97 return "invalid synchronization handle";
99 return "invalid thread agent";
101 return "invalid key";
103 return "no event message for getmsg";
105 return "FPU register set not available";
107 return "application not linked with libthread";
109 return "requested event is not supported";
111 return "capability not available";
113 return "debugger service failed";
115 return "operation not applicable to";
117 return "no thread-specific data for this thread";
119 return "malloc failed";
121 return "only part of register set was written/read";
123 return "X register set not available for this thread";
124 #ifdef HAVE_TD_VERSION
126 return "version mismatch between libthread_db and libpthread";
129 xsnprintf (buf
, sizeof (buf
), "unknown thread_db error '%d'", err
);
136 thread_db_state_str (td_thr_state_e state
)
143 return "stopped by debugger";
152 case TD_THR_STOPPED_ASLEEP
:
153 return "stopped by debugger AND blocked";
155 xsnprintf (buf
, sizeof (buf
), "unknown thread_db state %d", state
);
162 find_one_thread (ptid_t ptid
)
167 struct thread_info
*inferior
;
168 struct lwp_info
*lwp
;
169 struct thread_db
*thread_db
= current_process ()->priv
->thread_db
;
170 int lwpid
= ptid_get_lwp (ptid
);
172 inferior
= (struct thread_info
*) find_inferior_id (&all_threads
, ptid
);
173 lwp
= get_thread_lwp (inferior
);
174 if (lwp
->thread_known
)
177 /* Get information about this thread. */
178 err
= thread_db
->td_ta_map_lwp2thr_p (thread_db
->thread_agent
, lwpid
, &th
);
180 error ("Cannot get thread handle for LWP %d: %s",
181 lwpid
, thread_db_err_str (err
));
183 err
= thread_db
->td_thr_get_info_p (&th
, &ti
);
185 error ("Cannot get thread info for LWP %d: %s",
186 lwpid
, thread_db_err_str (err
));
189 debug_printf ("Found thread %ld (LWP %d)\n",
190 (unsigned long) ti
.ti_tid
, ti
.ti_lid
);
192 if (lwpid
!= ti
.ti_lid
)
194 warning ("PID mismatch! Expected %ld, got %ld",
195 (long) lwpid
, (long) ti
.ti_lid
);
199 /* If the new thread ID is zero, a final thread ID will be available
200 later. Do not enable thread debugging yet. */
204 lwp
->thread_known
= 1;
210 /* Attach a thread. Return true on success. */
213 attach_thread (const td_thrhandle_t
*th_p
, td_thrinfo_t
*ti_p
)
215 struct process_info
*proc
= current_process ();
216 int pid
= pid_of (proc
);
217 ptid_t ptid
= ptid_build (pid
, ti_p
->ti_lid
, 0);
218 struct lwp_info
*lwp
;
222 debug_printf ("Attaching to thread %ld (LWP %d)\n",
223 (unsigned long) ti_p
->ti_tid
, ti_p
->ti_lid
);
224 err
= linux_attach_lwp (ptid
);
227 warning ("Could not attach to thread %ld (LWP %d): %s\n",
228 (unsigned long) ti_p
->ti_tid
, ti_p
->ti_lid
,
229 linux_ptrace_attach_fail_reason_string (ptid
, err
));
233 lwp
= find_lwp_pid (ptid
);
234 gdb_assert (lwp
!= NULL
);
235 lwp
->thread_known
= 1;
241 /* Attach thread if we haven't seen it yet.
242 Increment *COUNTER if we have attached a new thread.
243 Return false on failure. */
246 maybe_attach_thread (const td_thrhandle_t
*th_p
, td_thrinfo_t
*ti_p
,
249 struct lwp_info
*lwp
;
251 lwp
= find_lwp_pid (pid_to_ptid (ti_p
->ti_lid
));
255 if (!attach_thread (th_p
, ti_p
))
265 find_new_threads_callback (const td_thrhandle_t
*th_p
, void *data
)
269 struct thread_db
*thread_db
= current_process ()->priv
->thread_db
;
271 err
= thread_db
->td_thr_get_info_p (th_p
, &ti
);
273 error ("Cannot get thread info: %s", thread_db_err_str (err
));
277 /* A thread with kernel thread ID -1 is either a thread that
278 exited and was joined, or a thread that is being created but
279 hasn't started yet, and that is reusing the tcb/stack of a
280 thread that previously exited and was joined. (glibc marks
281 terminated and joined threads with kernel thread ID -1. See
284 debug_printf ("thread_db: skipping exited and "
285 "joined thread (0x%lx)\n",
286 (unsigned long) ti
.ti_tid
);
290 /* Check for zombies. */
291 if (ti
.ti_state
== TD_THR_UNKNOWN
|| ti
.ti_state
== TD_THR_ZOMBIE
)
294 if (!maybe_attach_thread (th_p
, &ti
, (int *) data
))
296 /* Terminate iteration early: we might be looking at stale data in
297 the inferior. The thread_db_find_new_threads will retry. */
305 thread_db_find_new_threads (void)
308 ptid_t ptid
= current_ptid
;
309 struct thread_db
*thread_db
= current_process ()->priv
->thread_db
;
312 /* This function is only called when we first initialize thread_db.
313 First locate the initial thread. If it is not ready for
314 debugging yet, then stop. */
315 if (find_one_thread (ptid
) == 0)
318 /* Require 4 successive iterations which do not find any new threads.
319 The 4 is a heuristic: there is an inherent race here, and I have
320 seen that 2 iterations in a row are not always sufficient to
321 "capture" all threads. */
322 for (loop
= 0, iteration
= 0; loop
< 4; ++loop
, ++iteration
)
324 int new_thread_count
= 0;
326 /* Iterate over all user-space threads to discover new threads. */
327 err
= thread_db
->td_ta_thr_iter_p (thread_db
->thread_agent
,
328 find_new_threads_callback
,
331 TD_THR_LOWEST_PRIORITY
,
332 TD_SIGNO_MASK
, TD_THR_ANY_USER_FLAGS
);
334 debug_printf ("Found %d threads in iteration %d.\n",
335 new_thread_count
, iteration
);
337 if (new_thread_count
!= 0)
339 /* Found new threads. Restart iteration from beginning. */
344 error ("Cannot find new threads: %s", thread_db_err_str (err
));
347 /* Cache all future symbols that thread_db might request. We can not
348 request symbols at arbitrary states in the remote protocol, only
349 when the client tells us that new symbols are available. So when
350 we load the thread library, make sure to check the entire list. */
353 thread_db_look_up_symbols (void)
355 struct thread_db
*thread_db
= current_process ()->priv
->thread_db
;
356 const char **sym_list
;
359 for (sym_list
= thread_db
->td_symbol_list_p (); *sym_list
; sym_list
++)
360 look_up_one_symbol (*sym_list
, &unused
, 1);
362 /* We're not interested in any other libraries loaded after this
363 point, only in symbols in libpthread.so. */
364 thread_db
->all_symbols_looked_up
= 1;
368 thread_db_look_up_one_symbol (const char *name
, CORE_ADDR
*addrp
)
370 struct thread_db
*thread_db
= current_process ()->priv
->thread_db
;
371 int may_ask_gdb
= !thread_db
->all_symbols_looked_up
;
373 /* If we've passed the call to thread_db_look_up_symbols, then
374 anything not in the cache must not exist; we're not interested
375 in any libraries loaded after that point, only in symbols in
376 libpthread.so. It might not be an appropriate time to look
377 up a symbol, e.g. while we're trying to fetch registers. */
378 return look_up_one_symbol (name
, addrp
, may_ask_gdb
);
382 thread_db_get_tls_address (struct thread_info
*thread
, CORE_ADDR offset
,
383 CORE_ADDR load_module
, CORE_ADDR
*address
)
387 struct lwp_info
*lwp
;
388 struct thread_info
*saved_thread
;
389 struct process_info
*proc
;
390 struct thread_db
*thread_db
;
392 proc
= get_thread_process (thread
);
393 thread_db
= proc
->priv
->thread_db
;
395 /* If the thread layer is not (yet) initialized, fail. */
396 if (thread_db
== NULL
|| !thread_db
->all_symbols_looked_up
)
399 /* If td_thr_tls_get_addr is missing rather do not expect td_thr_tlsbase
401 if (thread_db
->td_thr_tls_get_addr_p
== NULL
402 || (load_module
== 0 && thread_db
->td_thr_tlsbase_p
== NULL
))
405 lwp
= get_thread_lwp (thread
);
406 if (!lwp
->thread_known
)
407 find_one_thread (thread
->entry
.id
);
408 if (!lwp
->thread_known
)
411 saved_thread
= current_thread
;
412 current_thread
= thread
;
414 if (load_module
!= 0)
416 /* Note the cast through uintptr_t: this interface only works if
417 a target address fits in a psaddr_t, which is a host pointer.
418 So a 32-bit debugger can not access 64-bit TLS through this. */
419 err
= thread_db
->td_thr_tls_get_addr_p (&lwp
->th
,
420 (psaddr_t
) (uintptr_t) load_module
,
425 /* This code path handles the case of -static -pthread executables:
426 https://sourceware.org/ml/libc-help/2014-03/msg00024.html
427 For older GNU libc r_debug.r_map is NULL. For GNU libc after
428 PR libc/16831 due to GDB PR threads/16954 LOAD_MODULE is also NULL.
429 The constant number 1 depends on GNU __libc_setup_tls
430 initialization of l_tls_modid to 1. */
431 err
= thread_db
->td_thr_tlsbase_p (&lwp
->th
, 1, &addr
);
432 addr
= (char *) addr
+ offset
;
435 current_thread
= saved_thread
;
438 *address
= (CORE_ADDR
) (uintptr_t) addr
;
445 #ifdef USE_LIBTHREAD_DB_DIRECTLY
448 thread_db_load_search (void)
451 struct thread_db
*tdb
;
452 struct process_info
*proc
= current_process ();
454 gdb_assert (proc
->priv
->thread_db
== NULL
);
456 tdb
= XCNEW (struct thread_db
);
457 proc
->priv
->thread_db
= tdb
;
459 tdb
->td_ta_new_p
= &td_ta_new
;
461 /* Attempt to open a connection to the thread library. */
462 err
= tdb
->td_ta_new_p (&tdb
->proc_handle
, &tdb
->thread_agent
);
466 debug_printf ("td_ta_new(): %s\n", thread_db_err_str (err
));
468 proc
->priv
->thread_db
= NULL
;
472 tdb
->td_ta_map_lwp2thr_p
= &td_ta_map_lwp2thr
;
473 tdb
->td_thr_get_info_p
= &td_thr_get_info
;
474 tdb
->td_ta_thr_iter_p
= &td_ta_thr_iter
;
475 tdb
->td_symbol_list_p
= &td_symbol_list
;
477 /* These are not essential. */
478 tdb
->td_ta_event_addr_p
= &td_ta_event_addr
;
479 tdb
->td_ta_set_event_p
= &td_ta_set_event
;
480 tdb
->td_ta_event_getmsg_p
= &td_ta_event_getmsg
;
481 tdb
->td_thr_tls_get_addr_p
= &td_thr_tls_get_addr
;
482 tdb
->td_thr_tlsbase_p
= &td_thr_tlsbase
;
490 try_thread_db_load_1 (void *handle
)
493 struct thread_db
*tdb
;
494 struct process_info
*proc
= current_process ();
496 gdb_assert (proc
->priv
->thread_db
== NULL
);
498 tdb
= XCNEW (struct thread_db
);
499 proc
->priv
->thread_db
= tdb
;
501 tdb
->handle
= handle
;
503 /* Initialize pointers to the dynamic library functions we will use.
504 Essential functions first. */
506 #define CHK(required, a) \
512 debug_printf ("dlsym: %s\n", dlerror ()); \
516 proc->priv->thread_db = NULL; \
523 #define TDB_DLSYM(tdb, func) \
524 tdb->func ## _p = (func ## _ftype *) dlsym (tdb->handle, #func)
526 CHK (1, TDB_DLSYM (tdb
, td_ta_new
));
528 /* Attempt to open a connection to the thread library. */
529 err
= tdb
->td_ta_new_p (&tdb
->proc_handle
, &tdb
->thread_agent
);
533 debug_printf ("td_ta_new(): %s\n", thread_db_err_str (err
));
535 proc
->priv
->thread_db
= NULL
;
539 CHK (1, TDB_DLSYM (tdb
, td_ta_map_lwp2thr
));
540 CHK (1, TDB_DLSYM (tdb
, td_thr_get_info
));
541 CHK (1, TDB_DLSYM (tdb
, td_ta_thr_iter
));
542 CHK (1, TDB_DLSYM (tdb
, td_symbol_list
));
544 /* These are not essential. */
545 CHK (0, TDB_DLSYM (tdb
, td_ta_event_addr
));
546 CHK (0, TDB_DLSYM (tdb
, td_ta_set_event
));
547 CHK (0, TDB_DLSYM (tdb
, td_ta_event_getmsg
));
548 CHK (0, TDB_DLSYM (tdb
, td_thr_tls_get_addr
));
549 CHK (0, TDB_DLSYM (tdb
, td_thr_tlsbase
));
559 /* Lookup a library in which given symbol resides.
560 Note: this is looking in the GDBSERVER process, not in the inferior.
561 Returns library name, or NULL. */
564 dladdr_to_soname (const void *addr
)
568 if (dladdr (addr
, &info
) != 0)
569 return info
.dli_fname
;
576 try_thread_db_load (const char *library
)
581 debug_printf ("Trying host libthread_db library: %s.\n",
583 handle
= dlopen (library
, RTLD_NOW
);
587 debug_printf ("dlopen failed: %s.\n", dlerror ());
592 if (debug_threads
&& strchr (library
, '/') == NULL
)
596 td_init
= dlsym (handle
, "td_init");
599 const char *const libpath
= dladdr_to_soname (td_init
);
602 fprintf (stderr
, "Host %s resolved to: %s.\n",
608 if (try_thread_db_load_1 (handle
))
611 /* This library "refused" to work on current inferior. */
616 /* Handle $sdir in libthread-db-search-path.
617 Look for libthread_db in the system dirs, or wherever a plain
618 dlopen(file_without_path) will look.
619 The result is true for success. */
622 try_thread_db_load_from_sdir (void)
624 return try_thread_db_load (LIBTHREAD_DB_SO
);
627 /* Try to load libthread_db from directory DIR of length DIR_LEN.
628 The result is true for success. */
631 try_thread_db_load_from_dir (const char *dir
, size_t dir_len
)
635 if (dir_len
+ 1 + strlen (LIBTHREAD_DB_SO
) + 1 > sizeof (path
))
637 char *cp
= (char *) xmalloc (dir_len
+ 1);
639 memcpy (cp
, dir
, dir_len
);
641 warning (_("libthread-db-search-path component too long,"
642 " ignored: %s."), cp
);
647 memcpy (path
, dir
, dir_len
);
649 strcpy (path
+ dir_len
+ 1, LIBTHREAD_DB_SO
);
650 return try_thread_db_load (path
);
653 /* Search libthread_db_search_path for libthread_db which "agrees"
654 to work on current inferior.
655 The result is true for success. */
658 thread_db_load_search (void)
660 VEC (char_ptr
) *dir_vec
;
664 if (libthread_db_search_path
== NULL
)
665 libthread_db_search_path
= xstrdup (LIBTHREAD_DB_SEARCH_PATH
);
667 dir_vec
= dirnames_to_char_ptr_vec (libthread_db_search_path
);
669 for (i
= 0; VEC_iterate (char_ptr
, dir_vec
, i
, this_dir
); ++i
)
671 const int pdir_len
= sizeof ("$pdir") - 1;
674 this_dir_len
= strlen (this_dir
);
676 if (strncmp (this_dir
, "$pdir", pdir_len
) == 0
677 && (this_dir
[pdir_len
] == '\0'
678 || this_dir
[pdir_len
] == '/'))
680 /* We don't maintain a list of loaded libraries so we don't know
681 where libpthread lives. We *could* fetch the info, but we don't
682 do that yet. Ignore it. */
684 else if (strcmp (this_dir
, "$sdir") == 0)
686 if (try_thread_db_load_from_sdir ())
694 if (try_thread_db_load_from_dir (this_dir
, this_dir_len
))
702 free_char_ptr_vec (dir_vec
);
704 debug_printf ("thread_db_load_search returning %d\n", rc
);
708 #endif /* USE_LIBTHREAD_DB_DIRECTLY */
711 thread_db_init (void)
713 struct process_info
*proc
= current_process ();
715 /* FIXME drow/2004-10-16: This is the "overall process ID", which
716 GNU/Linux calls tgid, "thread group ID". When we support
717 attaching to threads, the original thread may not be the correct
718 thread. We would have to get the process ID from /proc for NPTL.
719 For LinuxThreads we could do something similar: follow the chain
720 of parent processes until we find the highest one we're attached
721 to, and use its tgid.
723 This isn't the only place in gdbserver that assumes that the first
724 process in the list is the thread group leader. */
726 if (thread_db_load_search ())
728 /* It's best to avoid td_ta_thr_iter if possible. That walks
729 data structures in the inferior's address space that may be
730 corrupted, or, if the target is running, the list may change
731 while we walk it. In the latter case, it's possible that a
732 thread exits just at the exact time that causes GDBserver to
733 get stuck in an infinite loop. As the kernel supports clone
734 events and /proc/PID/task/ exists, then we already know about
735 all threads in the process. When we need info out of
736 thread_db on a given thread (e.g., for TLS), we'll use
737 find_one_thread then. That uses thread_db entry points that
738 do not walk libpthread's thread list, so should be safe, as
739 well as more efficient. */
740 if (!linux_proc_task_list_dir_exists (pid_of (proc
)))
741 thread_db_find_new_threads ();
742 thread_db_look_up_symbols ();
750 any_thread_of (struct inferior_list_entry
*entry
, void *args
)
752 int *pid_p
= (int *) args
;
754 if (ptid_get_pid (entry
->id
) == *pid_p
)
761 switch_to_process (struct process_info
*proc
)
763 int pid
= pid_of (proc
);
766 (struct thread_info
*) find_inferior (&all_threads
,
767 any_thread_of
, &pid
);
770 /* Disconnect from libthread_db and free resources. */
773 disable_thread_event_reporting (struct process_info
*proc
)
775 struct thread_db
*thread_db
= proc
->priv
->thread_db
;
778 td_err_e (*td_ta_clear_event_p
) (const td_thragent_t
*ta
,
779 td_thr_events_t
*event
);
781 #ifndef USE_LIBTHREAD_DB_DIRECTLY
783 = (td_ta_clear_event_ftype
*) dlsym (thread_db
->handle
,
784 "td_ta_clear_event");
786 td_ta_clear_event_p
= &td_ta_clear_event
;
789 if (td_ta_clear_event_p
!= NULL
)
791 struct thread_info
*saved_thread
= current_thread
;
792 td_thr_events_t events
;
794 switch_to_process (proc
);
796 /* Set the process wide mask saying we aren't interested
797 in any events anymore. */
798 td_event_fillset (&events
);
799 (*td_ta_clear_event_p
) (thread_db
->thread_agent
, &events
);
801 current_thread
= saved_thread
;
807 thread_db_detach (struct process_info
*proc
)
809 struct thread_db
*thread_db
= proc
->priv
->thread_db
;
813 disable_thread_event_reporting (proc
);
817 /* Disconnect from libthread_db and free resources. */
820 thread_db_mourn (struct process_info
*proc
)
822 struct thread_db
*thread_db
= proc
->priv
->thread_db
;
825 td_ta_delete_ftype
*td_ta_delete_p
;
827 #ifndef USE_LIBTHREAD_DB_DIRECTLY
828 td_ta_delete_p
= (td_ta_delete_ftype
*) dlsym (thread_db
->handle
, "td_ta_delete");
830 td_ta_delete_p
= &td_ta_delete
;
833 if (td_ta_delete_p
!= NULL
)
834 (*td_ta_delete_p
) (thread_db
->thread_agent
);
836 #ifndef USE_LIBTHREAD_DB_DIRECTLY
837 dlclose (thread_db
->handle
);
838 #endif /* USE_LIBTHREAD_DB_DIRECTLY */
841 proc
->priv
->thread_db
= NULL
;
845 /* Handle "set libthread-db-search-path" monitor command and return 1.
846 For any other command, return 0. */
849 thread_db_handle_monitor_command (char *mon
)
851 const char *cmd
= "set libthread-db-search-path";
852 size_t cmd_len
= strlen (cmd
);
854 if (strncmp (mon
, cmd
, cmd_len
) == 0
855 && (mon
[cmd_len
] == '\0'
856 || mon
[cmd_len
] == ' '))
858 const char *cp
= mon
+ cmd_len
;
860 if (libthread_db_search_path
!= NULL
)
861 free (libthread_db_search_path
);
863 /* Skip leading space (if any). */
864 while (isspace (*cp
))
868 cp
= LIBTHREAD_DB_SEARCH_PATH
;
869 libthread_db_search_path
= xstrdup (cp
);
871 monitor_output ("libthread-db-search-path set to `");
872 monitor_output (libthread_db_search_path
);
873 monitor_output ("'\n");
877 /* Tell server.c to perform default processing. */