1 /* Thread management interface, for the remote server for GDB.
2 Copyright (C) 2002-2017 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_map_lwp2thr_ftype
*td_ta_map_lwp2thr_p
;
60 td_thr_get_info_ftype
*td_thr_get_info_p
;
61 td_ta_thr_iter_ftype
*td_ta_thr_iter_p
;
62 td_thr_tls_get_addr_ftype
*td_thr_tls_get_addr_p
;
63 td_thr_tlsbase_ftype
*td_thr_tlsbase_p
;
64 td_symbol_list_ftype
*td_symbol_list_p
;
67 static char *libthread_db_search_path
;
69 static int find_one_thread (ptid_t
);
70 static int find_new_threads_callback (const td_thrhandle_t
*th_p
, void *data
);
73 thread_db_err_str (td_err_e err
)
80 return "generic 'call succeeded'";
82 return "generic error";
84 return "no thread to satisfy query";
86 return "no sync handle to satisfy query";
88 return "no LWP to satisfy query";
90 return "invalid process handle";
92 return "invalid thread handle";
94 return "invalid synchronization handle";
96 return "invalid thread agent";
100 return "no event message for getmsg";
102 return "FPU register set not available";
104 return "application not linked with libthread";
106 return "requested event is not supported";
108 return "capability not available";
110 return "debugger service failed";
112 return "operation not applicable to";
114 return "no thread-specific data for this thread";
116 return "malloc failed";
118 return "only part of register set was written/read";
120 return "X register set not available for this thread";
121 #ifdef HAVE_TD_VERSION
123 return "version mismatch between libthread_db and libpthread";
126 xsnprintf (buf
, sizeof (buf
), "unknown thread_db error '%d'", err
);
133 thread_db_state_str (td_thr_state_e state
)
140 return "stopped by debugger";
149 case TD_THR_STOPPED_ASLEEP
:
150 return "stopped by debugger AND blocked";
152 xsnprintf (buf
, sizeof (buf
), "unknown thread_db state %d", state
);
159 find_one_thread (ptid_t ptid
)
164 struct thread_info
*inferior
;
165 struct lwp_info
*lwp
;
166 struct thread_db
*thread_db
= current_process ()->priv
->thread_db
;
167 int lwpid
= ptid_get_lwp (ptid
);
169 inferior
= (struct thread_info
*) find_inferior_id (&all_threads
, ptid
);
170 lwp
= get_thread_lwp (inferior
);
171 if (lwp
->thread_known
)
174 /* Get information about this thread. */
175 err
= thread_db
->td_ta_map_lwp2thr_p (thread_db
->thread_agent
, lwpid
, &th
);
177 error ("Cannot get thread handle for LWP %d: %s",
178 lwpid
, thread_db_err_str (err
));
180 err
= thread_db
->td_thr_get_info_p (&th
, &ti
);
182 error ("Cannot get thread info for LWP %d: %s",
183 lwpid
, thread_db_err_str (err
));
186 debug_printf ("Found thread %ld (LWP %d)\n",
187 (unsigned long) ti
.ti_tid
, ti
.ti_lid
);
189 if (lwpid
!= ti
.ti_lid
)
191 warning ("PID mismatch! Expected %ld, got %ld",
192 (long) lwpid
, (long) ti
.ti_lid
);
196 /* If the new thread ID is zero, a final thread ID will be available
197 later. Do not enable thread debugging yet. */
201 lwp
->thread_known
= 1;
207 /* Attach a thread. Return true on success. */
210 attach_thread (const td_thrhandle_t
*th_p
, td_thrinfo_t
*ti_p
)
212 struct process_info
*proc
= current_process ();
213 int pid
= pid_of (proc
);
214 ptid_t ptid
= ptid_build (pid
, ti_p
->ti_lid
, 0);
215 struct lwp_info
*lwp
;
219 debug_printf ("Attaching to thread %ld (LWP %d)\n",
220 (unsigned long) ti_p
->ti_tid
, ti_p
->ti_lid
);
221 err
= linux_attach_lwp (ptid
);
224 warning ("Could not attach to thread %ld (LWP %d): %s\n",
225 (unsigned long) ti_p
->ti_tid
, ti_p
->ti_lid
,
226 linux_ptrace_attach_fail_reason_string (ptid
, err
));
230 lwp
= find_lwp_pid (ptid
);
231 gdb_assert (lwp
!= NULL
);
232 lwp
->thread_known
= 1;
238 /* Attach thread if we haven't seen it yet.
239 Increment *COUNTER if we have attached a new thread.
240 Return false on failure. */
243 maybe_attach_thread (const td_thrhandle_t
*th_p
, td_thrinfo_t
*ti_p
,
246 struct lwp_info
*lwp
;
248 lwp
= find_lwp_pid (pid_to_ptid (ti_p
->ti_lid
));
252 if (!attach_thread (th_p
, ti_p
))
262 find_new_threads_callback (const td_thrhandle_t
*th_p
, void *data
)
266 struct thread_db
*thread_db
= current_process ()->priv
->thread_db
;
268 err
= thread_db
->td_thr_get_info_p (th_p
, &ti
);
270 error ("Cannot get thread info: %s", thread_db_err_str (err
));
274 /* A thread with kernel thread ID -1 is either a thread that
275 exited and was joined, or a thread that is being created but
276 hasn't started yet, and that is reusing the tcb/stack of a
277 thread that previously exited and was joined. (glibc marks
278 terminated and joined threads with kernel thread ID -1. See
281 debug_printf ("thread_db: skipping exited and "
282 "joined thread (0x%lx)\n",
283 (unsigned long) ti
.ti_tid
);
287 /* Check for zombies. */
288 if (ti
.ti_state
== TD_THR_UNKNOWN
|| ti
.ti_state
== TD_THR_ZOMBIE
)
291 if (!maybe_attach_thread (th_p
, &ti
, (int *) data
))
293 /* Terminate iteration early: we might be looking at stale data in
294 the inferior. The thread_db_find_new_threads will retry. */
302 thread_db_find_new_threads (void)
305 ptid_t ptid
= current_ptid
;
306 struct thread_db
*thread_db
= current_process ()->priv
->thread_db
;
309 /* This function is only called when we first initialize thread_db.
310 First locate the initial thread. If it is not ready for
311 debugging yet, then stop. */
312 if (find_one_thread (ptid
) == 0)
315 /* Require 4 successive iterations which do not find any new threads.
316 The 4 is a heuristic: there is an inherent race here, and I have
317 seen that 2 iterations in a row are not always sufficient to
318 "capture" all threads. */
319 for (loop
= 0, iteration
= 0; loop
< 4; ++loop
, ++iteration
)
321 int new_thread_count
= 0;
323 /* Iterate over all user-space threads to discover new threads. */
324 err
= thread_db
->td_ta_thr_iter_p (thread_db
->thread_agent
,
325 find_new_threads_callback
,
328 TD_THR_LOWEST_PRIORITY
,
329 TD_SIGNO_MASK
, TD_THR_ANY_USER_FLAGS
);
331 debug_printf ("Found %d threads in iteration %d.\n",
332 new_thread_count
, iteration
);
334 if (new_thread_count
!= 0)
336 /* Found new threads. Restart iteration from beginning. */
341 error ("Cannot find new threads: %s", thread_db_err_str (err
));
344 /* Cache all future symbols that thread_db might request. We can not
345 request symbols at arbitrary states in the remote protocol, only
346 when the client tells us that new symbols are available. So when
347 we load the thread library, make sure to check the entire list. */
350 thread_db_look_up_symbols (void)
352 struct thread_db
*thread_db
= current_process ()->priv
->thread_db
;
353 const char **sym_list
;
356 for (sym_list
= thread_db
->td_symbol_list_p (); *sym_list
; sym_list
++)
357 look_up_one_symbol (*sym_list
, &unused
, 1);
359 /* We're not interested in any other libraries loaded after this
360 point, only in symbols in libpthread.so. */
361 thread_db
->all_symbols_looked_up
= 1;
365 thread_db_look_up_one_symbol (const char *name
, CORE_ADDR
*addrp
)
367 struct thread_db
*thread_db
= current_process ()->priv
->thread_db
;
368 int may_ask_gdb
= !thread_db
->all_symbols_looked_up
;
370 /* If we've passed the call to thread_db_look_up_symbols, then
371 anything not in the cache must not exist; we're not interested
372 in any libraries loaded after that point, only in symbols in
373 libpthread.so. It might not be an appropriate time to look
374 up a symbol, e.g. while we're trying to fetch registers. */
375 return look_up_one_symbol (name
, addrp
, may_ask_gdb
);
379 thread_db_get_tls_address (struct thread_info
*thread
, CORE_ADDR offset
,
380 CORE_ADDR load_module
, CORE_ADDR
*address
)
384 struct lwp_info
*lwp
;
385 struct thread_info
*saved_thread
;
386 struct process_info
*proc
;
387 struct thread_db
*thread_db
;
389 proc
= get_thread_process (thread
);
390 thread_db
= proc
->priv
->thread_db
;
392 /* If the thread layer is not (yet) initialized, fail. */
393 if (thread_db
== NULL
|| !thread_db
->all_symbols_looked_up
)
396 /* If td_thr_tls_get_addr is missing rather do not expect td_thr_tlsbase
398 if (thread_db
->td_thr_tls_get_addr_p
== NULL
399 || (load_module
== 0 && thread_db
->td_thr_tlsbase_p
== NULL
))
402 lwp
= get_thread_lwp (thread
);
403 if (!lwp
->thread_known
)
404 find_one_thread (thread
->entry
.id
);
405 if (!lwp
->thread_known
)
408 saved_thread
= current_thread
;
409 current_thread
= thread
;
411 if (load_module
!= 0)
413 /* Note the cast through uintptr_t: this interface only works if
414 a target address fits in a psaddr_t, which is a host pointer.
415 So a 32-bit debugger can not access 64-bit TLS through this. */
416 err
= thread_db
->td_thr_tls_get_addr_p (&lwp
->th
,
417 (psaddr_t
) (uintptr_t) load_module
,
422 /* This code path handles the case of -static -pthread executables:
423 https://sourceware.org/ml/libc-help/2014-03/msg00024.html
424 For older GNU libc r_debug.r_map is NULL. For GNU libc after
425 PR libc/16831 due to GDB PR threads/16954 LOAD_MODULE is also NULL.
426 The constant number 1 depends on GNU __libc_setup_tls
427 initialization of l_tls_modid to 1. */
428 err
= thread_db
->td_thr_tlsbase_p (&lwp
->th
, 1, &addr
);
429 addr
= (char *) addr
+ offset
;
432 current_thread
= saved_thread
;
435 *address
= (CORE_ADDR
) (uintptr_t) addr
;
442 #ifdef USE_LIBTHREAD_DB_DIRECTLY
445 thread_db_load_search (void)
448 struct thread_db
*tdb
;
449 struct process_info
*proc
= current_process ();
451 gdb_assert (proc
->priv
->thread_db
== NULL
);
453 tdb
= XCNEW (struct thread_db
);
454 proc
->priv
->thread_db
= tdb
;
456 tdb
->td_ta_new_p
= &td_ta_new
;
458 /* Attempt to open a connection to the thread library. */
459 err
= tdb
->td_ta_new_p (&tdb
->proc_handle
, &tdb
->thread_agent
);
463 debug_printf ("td_ta_new(): %s\n", thread_db_err_str (err
));
465 proc
->priv
->thread_db
= NULL
;
469 tdb
->td_ta_map_lwp2thr_p
= &td_ta_map_lwp2thr
;
470 tdb
->td_thr_get_info_p
= &td_thr_get_info
;
471 tdb
->td_ta_thr_iter_p
= &td_ta_thr_iter
;
472 tdb
->td_symbol_list_p
= &td_symbol_list
;
474 /* These are not essential. */
475 tdb
->td_thr_tls_get_addr_p
= &td_thr_tls_get_addr
;
476 tdb
->td_thr_tlsbase_p
= &td_thr_tlsbase
;
484 try_thread_db_load_1 (void *handle
)
487 struct thread_db
*tdb
;
488 struct process_info
*proc
= current_process ();
490 gdb_assert (proc
->priv
->thread_db
== NULL
);
492 tdb
= XCNEW (struct thread_db
);
493 proc
->priv
->thread_db
= tdb
;
495 tdb
->handle
= handle
;
497 /* Initialize pointers to the dynamic library functions we will use.
498 Essential functions first. */
500 #define CHK(required, a) \
506 debug_printf ("dlsym: %s\n", dlerror ()); \
510 proc->priv->thread_db = NULL; \
517 #define TDB_DLSYM(tdb, func) \
518 tdb->func ## _p = (func ## _ftype *) dlsym (tdb->handle, #func)
520 CHK (1, TDB_DLSYM (tdb
, td_ta_new
));
522 /* Attempt to open a connection to the thread library. */
523 err
= tdb
->td_ta_new_p (&tdb
->proc_handle
, &tdb
->thread_agent
);
527 debug_printf ("td_ta_new(): %s\n", thread_db_err_str (err
));
529 proc
->priv
->thread_db
= NULL
;
533 CHK (1, TDB_DLSYM (tdb
, td_ta_map_lwp2thr
));
534 CHK (1, TDB_DLSYM (tdb
, td_thr_get_info
));
535 CHK (1, TDB_DLSYM (tdb
, td_ta_thr_iter
));
536 CHK (1, TDB_DLSYM (tdb
, td_symbol_list
));
538 /* These are not essential. */
539 CHK (0, TDB_DLSYM (tdb
, td_thr_tls_get_addr
));
540 CHK (0, TDB_DLSYM (tdb
, td_thr_tlsbase
));
550 /* Lookup a library in which given symbol resides.
551 Note: this is looking in the GDBSERVER process, not in the inferior.
552 Returns library name, or NULL. */
555 dladdr_to_soname (const void *addr
)
559 if (dladdr (addr
, &info
) != 0)
560 return info
.dli_fname
;
567 try_thread_db_load (const char *library
)
572 debug_printf ("Trying host libthread_db library: %s.\n",
574 handle
= dlopen (library
, RTLD_NOW
);
578 debug_printf ("dlopen failed: %s.\n", dlerror ());
583 if (debug_threads
&& strchr (library
, '/') == NULL
)
587 td_init
= dlsym (handle
, "td_init");
590 const char *const libpath
= dladdr_to_soname (td_init
);
593 debug_printf ("Host %s resolved to: %s.\n", library
, libpath
);
598 if (try_thread_db_load_1 (handle
))
601 /* This library "refused" to work on current inferior. */
606 /* Handle $sdir in libthread-db-search-path.
607 Look for libthread_db in the system dirs, or wherever a plain
608 dlopen(file_without_path) will look.
609 The result is true for success. */
612 try_thread_db_load_from_sdir (void)
614 return try_thread_db_load (LIBTHREAD_DB_SO
);
617 /* Try to load libthread_db from directory DIR of length DIR_LEN.
618 The result is true for success. */
621 try_thread_db_load_from_dir (const char *dir
, size_t dir_len
)
625 if (dir_len
+ 1 + strlen (LIBTHREAD_DB_SO
) + 1 > sizeof (path
))
627 char *cp
= (char *) xmalloc (dir_len
+ 1);
629 memcpy (cp
, dir
, dir_len
);
631 warning (_("libthread-db-search-path component too long,"
632 " ignored: %s."), cp
);
637 memcpy (path
, dir
, dir_len
);
639 strcpy (path
+ dir_len
+ 1, LIBTHREAD_DB_SO
);
640 return try_thread_db_load (path
);
643 /* Search libthread_db_search_path for libthread_db which "agrees"
644 to work on current inferior.
645 The result is true for success. */
648 thread_db_load_search (void)
650 VEC (char_ptr
) *dir_vec
;
654 if (libthread_db_search_path
== NULL
)
655 libthread_db_search_path
= xstrdup (LIBTHREAD_DB_SEARCH_PATH
);
657 dir_vec
= dirnames_to_char_ptr_vec (libthread_db_search_path
);
659 for (i
= 0; VEC_iterate (char_ptr
, dir_vec
, i
, this_dir
); ++i
)
661 const int pdir_len
= sizeof ("$pdir") - 1;
664 this_dir_len
= strlen (this_dir
);
666 if (strncmp (this_dir
, "$pdir", pdir_len
) == 0
667 && (this_dir
[pdir_len
] == '\0'
668 || this_dir
[pdir_len
] == '/'))
670 /* We don't maintain a list of loaded libraries so we don't know
671 where libpthread lives. We *could* fetch the info, but we don't
672 do that yet. Ignore it. */
674 else if (strcmp (this_dir
, "$sdir") == 0)
676 if (try_thread_db_load_from_sdir ())
684 if (try_thread_db_load_from_dir (this_dir
, this_dir_len
))
692 free_char_ptr_vec (dir_vec
);
694 debug_printf ("thread_db_load_search returning %d\n", rc
);
698 #endif /* USE_LIBTHREAD_DB_DIRECTLY */
701 thread_db_init (void)
703 struct process_info
*proc
= current_process ();
705 /* FIXME drow/2004-10-16: This is the "overall process ID", which
706 GNU/Linux calls tgid, "thread group ID". When we support
707 attaching to threads, the original thread may not be the correct
708 thread. We would have to get the process ID from /proc for NPTL.
710 This isn't the only place in gdbserver that assumes that the first
711 process in the list is the thread group leader. */
713 if (thread_db_load_search ())
715 /* It's best to avoid td_ta_thr_iter if possible. That walks
716 data structures in the inferior's address space that may be
717 corrupted, or, if the target is running, the list may change
718 while we walk it. In the latter case, it's possible that a
719 thread exits just at the exact time that causes GDBserver to
720 get stuck in an infinite loop. As the kernel supports clone
721 events and /proc/PID/task/ exists, then we already know about
722 all threads in the process. When we need info out of
723 thread_db on a given thread (e.g., for TLS), we'll use
724 find_one_thread then. That uses thread_db entry points that
725 do not walk libpthread's thread list, so should be safe, as
726 well as more efficient. */
727 if (!linux_proc_task_list_dir_exists (pid_of (proc
)))
728 thread_db_find_new_threads ();
729 thread_db_look_up_symbols ();
737 any_thread_of (struct inferior_list_entry
*entry
, void *args
)
739 int *pid_p
= (int *) args
;
741 if (ptid_get_pid (entry
->id
) == *pid_p
)
748 switch_to_process (struct process_info
*proc
)
750 int pid
= pid_of (proc
);
753 (struct thread_info
*) find_inferior (&all_threads
,
754 any_thread_of
, &pid
);
757 /* Disconnect from libthread_db and free resources. */
760 disable_thread_event_reporting (struct process_info
*proc
)
762 struct thread_db
*thread_db
= proc
->priv
->thread_db
;
765 td_err_e (*td_ta_clear_event_p
) (const td_thragent_t
*ta
,
766 td_thr_events_t
*event
);
768 #ifndef USE_LIBTHREAD_DB_DIRECTLY
770 = (td_ta_clear_event_ftype
*) dlsym (thread_db
->handle
,
771 "td_ta_clear_event");
773 td_ta_clear_event_p
= &td_ta_clear_event
;
776 if (td_ta_clear_event_p
!= NULL
)
778 struct thread_info
*saved_thread
= current_thread
;
779 td_thr_events_t events
;
781 switch_to_process (proc
);
783 /* Set the process wide mask saying we aren't interested
784 in any events anymore. */
785 td_event_fillset (&events
);
786 (*td_ta_clear_event_p
) (thread_db
->thread_agent
, &events
);
788 current_thread
= saved_thread
;
794 thread_db_detach (struct process_info
*proc
)
796 struct thread_db
*thread_db
= proc
->priv
->thread_db
;
800 disable_thread_event_reporting (proc
);
804 /* Disconnect from libthread_db and free resources. */
807 thread_db_mourn (struct process_info
*proc
)
809 struct thread_db
*thread_db
= proc
->priv
->thread_db
;
812 td_ta_delete_ftype
*td_ta_delete_p
;
814 #ifndef USE_LIBTHREAD_DB_DIRECTLY
815 td_ta_delete_p
= (td_ta_delete_ftype
*) dlsym (thread_db
->handle
, "td_ta_delete");
817 td_ta_delete_p
= &td_ta_delete
;
820 if (td_ta_delete_p
!= NULL
)
821 (*td_ta_delete_p
) (thread_db
->thread_agent
);
823 #ifndef USE_LIBTHREAD_DB_DIRECTLY
824 dlclose (thread_db
->handle
);
825 #endif /* USE_LIBTHREAD_DB_DIRECTLY */
828 proc
->priv
->thread_db
= NULL
;
832 /* Handle "set libthread-db-search-path" monitor command and return 1.
833 For any other command, return 0. */
836 thread_db_handle_monitor_command (char *mon
)
838 const char *cmd
= "set libthread-db-search-path";
839 size_t cmd_len
= strlen (cmd
);
841 if (strncmp (mon
, cmd
, cmd_len
) == 0
842 && (mon
[cmd_len
] == '\0'
843 || mon
[cmd_len
] == ' '))
845 const char *cp
= mon
+ cmd_len
;
847 if (libthread_db_search_path
!= NULL
)
848 free (libthread_db_search_path
);
850 /* Skip leading space (if any). */
851 while (isspace (*cp
))
855 cp
= LIBTHREAD_DB_SEARCH_PATH
;
856 libthread_db_search_path
= xstrdup (cp
);
858 monitor_output ("libthread-db-search-path set to `");
859 monitor_output (libthread_db_search_path
);
860 monitor_output ("'\n");
864 /* Tell server.c to perform default processing. */