1 /* Thread management interface, for the remote server for GDB.
2 Copyright (C) 2002-2024 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/>. */
22 #include "linux-low.h"
25 #include "gdb_proc_service.h"
26 #include "nat/gdb_thread_db.h"
27 #include "gdbsupport/gdb_vecs.h"
28 #include "nat/linux-procfs.h"
29 #include "gdbsupport/scoped_restore.h"
31 #ifndef USE_LIBTHREAD_DB_DIRECTLY
39 /* Structure that identifies the child process for the
40 <proc_service.h> interface. */
41 struct ps_prochandle proc_handle
;
43 /* Connection to the libthread_db library. */
44 td_thragent_t
*thread_agent
;
46 /* If this flag has been set, we've already asked GDB for all
47 symbols we might need; assume symbol cache misses are
49 int all_symbols_looked_up
;
51 #ifndef USE_LIBTHREAD_DB_DIRECTLY
52 /* Handle of the libthread_db from dlopen. */
56 /* Addresses of libthread_db functions. */
57 td_ta_new_ftype
*td_ta_new_p
;
58 td_ta_map_lwp2thr_ftype
*td_ta_map_lwp2thr_p
;
59 td_thr_get_info_ftype
*td_thr_get_info_p
;
60 td_ta_thr_iter_ftype
*td_ta_thr_iter_p
;
61 td_thr_tls_get_addr_ftype
*td_thr_tls_get_addr_p
;
62 td_thr_tlsbase_ftype
*td_thr_tlsbase_p
;
63 td_symbol_list_ftype
*td_symbol_list_p
;
66 static char *libthread_db_search_path
;
68 static int find_one_thread (ptid_t
);
69 static int find_new_threads_callback (const td_thrhandle_t
*th_p
, void *data
);
72 thread_db_err_str (td_err_e err
)
79 return "generic 'call succeeded'";
81 return "generic error";
83 return "no thread to satisfy query";
85 return "no sync handle to satisfy query";
87 return "no LWP to satisfy query";
89 return "invalid process handle";
91 return "invalid thread handle";
93 return "invalid synchronization handle";
95 return "invalid thread agent";
99 return "no event message for getmsg";
101 return "FPU register set not available";
103 return "application not linked with libthread";
105 return "requested event is not supported";
107 return "capability not available";
109 return "debugger service failed";
111 return "operation not applicable to";
113 return "no thread-specific data for this thread";
115 return "malloc failed";
117 return "only part of register set was written/read";
119 return "X register set not available for this thread";
120 #ifdef HAVE_TD_VERSION
122 return "version mismatch between libthread_db and libpthread";
125 xsnprintf (buf
, sizeof (buf
), "unknown thread_db error '%d'", err
);
132 thread_db_state_str (td_thr_state_e state
)
139 return "stopped by debugger";
148 case TD_THR_STOPPED_ASLEEP
:
149 return "stopped by debugger AND blocked";
151 xsnprintf (buf
, sizeof (buf
), "unknown thread_db state %d", state
);
157 /* Get thread info about PTID. */
160 find_one_thread (ptid_t ptid
)
162 thread_info
*thread
= find_thread_ptid (ptid
);
163 lwp_info
*lwp
= get_thread_lwp (thread
);
164 if (lwp
->thread_known
)
167 /* Get information about this thread. libthread_db will need to read some
168 memory, which will be done on the current process, so make PTID's process
170 process_info
*proc
= find_process_pid (ptid
.pid ());
171 gdb_assert (proc
!= nullptr);
173 scoped_restore_current_thread restore_thread
;
174 switch_to_process (proc
);
176 thread_db
*thread_db
= proc
->priv
->thread_db
;
178 int lwpid
= ptid
.lwp ();
179 td_err_e err
= thread_db
->td_ta_map_lwp2thr_p (thread_db
->thread_agent
, lwpid
,
182 error ("Cannot get thread handle for LWP %d: %s",
183 lwpid
, thread_db_err_str (err
));
186 err
= thread_db
->td_thr_get_info_p (&th
, &ti
);
188 error ("Cannot get thread info for LWP %d: %s",
189 lwpid
, thread_db_err_str (err
));
191 threads_debug_printf ("Found thread %ld (LWP %d)",
192 (unsigned long) ti
.ti_tid
, ti
.ti_lid
);
194 if (lwpid
!= ti
.ti_lid
)
196 warning ("PID mismatch! Expected %ld, got %ld",
197 (long) lwpid
, (long) ti
.ti_lid
);
201 /* If the new thread ID is zero, a final thread ID will be available
202 later. Do not enable thread debugging yet. */
206 lwp
->thread_known
= 1;
208 lwp
->thread_handle
= ti
.ti_tid
;
213 /* Attach a thread. Return true on success. */
216 attach_thread (const td_thrhandle_t
*th_p
, td_thrinfo_t
*ti_p
)
218 struct process_info
*proc
= current_process ();
219 int pid
= pid_of (proc
);
220 ptid_t ptid
= ptid_t (pid
, ti_p
->ti_lid
);
221 struct lwp_info
*lwp
;
224 threads_debug_printf ("Attaching to thread %ld (LWP %d)",
225 (unsigned long) ti_p
->ti_tid
, ti_p
->ti_lid
);
226 err
= the_linux_target
->attach_lwp (ptid
);
229 std::string reason
= linux_ptrace_attach_fail_reason_string (ptid
, err
);
231 warning ("Could not attach to thread %ld (LWP %d): %s",
232 (unsigned long) ti_p
->ti_tid
, ti_p
->ti_lid
, reason
.c_str ());
237 lwp
= find_lwp_pid (ptid
);
238 gdb_assert (lwp
!= NULL
);
239 lwp
->thread_known
= 1;
241 lwp
->thread_handle
= ti_p
->ti_tid
;
246 /* Attach thread if we haven't seen it yet.
247 Increment *COUNTER if we have attached a new thread.
248 Return false on failure. */
251 maybe_attach_thread (const td_thrhandle_t
*th_p
, td_thrinfo_t
*ti_p
,
254 struct lwp_info
*lwp
;
256 lwp
= find_lwp_pid (ptid_t (ti_p
->ti_lid
));
260 if (!attach_thread (th_p
, ti_p
))
270 find_new_threads_callback (const td_thrhandle_t
*th_p
, void *data
)
274 struct thread_db
*thread_db
= current_process ()->priv
->thread_db
;
276 err
= thread_db
->td_thr_get_info_p (th_p
, &ti
);
278 error ("Cannot get thread info: %s", thread_db_err_str (err
));
282 /* A thread with kernel thread ID -1 is either a thread that
283 exited and was joined, or a thread that is being created but
284 hasn't started yet, and that is reusing the tcb/stack of a
285 thread that previously exited and was joined. (glibc marks
286 terminated and joined threads with kernel thread ID -1. See
288 threads_debug_printf ("thread_db: skipping exited and "
289 "joined thread (0x%lx)",
290 (unsigned long) ti
.ti_tid
);
294 /* Check for zombies. */
295 if (ti
.ti_state
== TD_THR_UNKNOWN
|| ti
.ti_state
== TD_THR_ZOMBIE
)
298 if (!maybe_attach_thread (th_p
, &ti
, (int *) data
))
300 /* Terminate iteration early: we might be looking at stale data in
301 the inferior. The thread_db_find_new_threads will retry. */
309 thread_db_find_new_threads (void)
312 ptid_t ptid
= current_ptid
;
313 struct thread_db
*thread_db
= current_process ()->priv
->thread_db
;
316 /* This function is only called when we first initialize thread_db.
317 First locate the initial thread. If it is not ready for
318 debugging yet, then stop. */
319 if (find_one_thread (ptid
) == 0)
322 /* Require 4 successive iterations which do not find any new threads.
323 The 4 is a heuristic: there is an inherent race here, and I have
324 seen that 2 iterations in a row are not always sufficient to
325 "capture" all threads. */
326 for (loop
= 0, iteration
= 0; loop
< 4; ++loop
, ++iteration
)
328 int new_thread_count
= 0;
330 /* Iterate over all user-space threads to discover new threads. */
331 err
= thread_db
->td_ta_thr_iter_p (thread_db
->thread_agent
,
332 find_new_threads_callback
,
335 TD_THR_LOWEST_PRIORITY
,
336 TD_SIGNO_MASK
, TD_THR_ANY_USER_FLAGS
);
337 threads_debug_printf ("Found %d threads in iteration %d.",
338 new_thread_count
, iteration
);
340 if (new_thread_count
!= 0)
342 /* Found new threads. Restart iteration from beginning. */
347 error ("Cannot find new threads: %s", thread_db_err_str (err
));
350 /* Cache all future symbols that thread_db might request. We can not
351 request symbols at arbitrary states in the remote protocol, only
352 when the client tells us that new symbols are available. So when
353 we load the thread library, make sure to check the entire list. */
356 thread_db_look_up_symbols (void)
358 struct thread_db
*thread_db
= current_process ()->priv
->thread_db
;
359 const char **sym_list
;
362 for (sym_list
= thread_db
->td_symbol_list_p (); *sym_list
; sym_list
++)
363 look_up_one_symbol (*sym_list
, &unused
, 1);
365 /* We're not interested in any other libraries loaded after this
366 point, only in symbols in libpthread.so. */
367 thread_db
->all_symbols_looked_up
= 1;
371 thread_db_look_up_one_symbol (const char *name
, CORE_ADDR
*addrp
)
373 struct thread_db
*thread_db
= current_process ()->priv
->thread_db
;
374 int may_ask_gdb
= !thread_db
->all_symbols_looked_up
;
376 /* If we've passed the call to thread_db_look_up_symbols, then
377 anything not in the cache must not exist; we're not interested
378 in any libraries loaded after that point, only in symbols in
379 libpthread.so. It might not be an appropriate time to look
380 up a symbol, e.g. while we're trying to fetch registers. */
381 return look_up_one_symbol (name
, addrp
, may_ask_gdb
);
385 thread_db_get_tls_address (struct thread_info
*thread
, CORE_ADDR offset
,
386 CORE_ADDR load_module
, CORE_ADDR
*address
)
390 struct lwp_info
*lwp
;
391 struct process_info
*proc
;
392 struct thread_db
*thread_db
;
394 proc
= get_thread_process (thread
);
395 thread_db
= proc
->priv
->thread_db
;
397 /* If the thread layer is not (yet) initialized, fail. */
398 if (thread_db
== NULL
|| !thread_db
->all_symbols_looked_up
)
401 /* If td_thr_tls_get_addr is missing rather do not expect td_thr_tlsbase
403 if (thread_db
->td_thr_tls_get_addr_p
== NULL
404 || (load_module
== 0 && thread_db
->td_thr_tlsbase_p
== NULL
))
407 lwp
= get_thread_lwp (thread
);
408 if (!lwp
->thread_known
)
409 find_one_thread (thread
->id
);
410 if (!lwp
->thread_known
)
413 scoped_restore_current_thread restore_thread
;
414 switch_to_thread (thread
);
416 if (load_module
!= 0)
418 /* Note the cast through uintptr_t: this interface only works if
419 a target address fits in a psaddr_t, which is a host pointer.
420 So a 32-bit debugger can not access 64-bit TLS through this. */
421 err
= thread_db
->td_thr_tls_get_addr_p (&lwp
->th
,
422 (psaddr_t
) (uintptr_t) load_module
,
427 /* This code path handles the case of -static -pthread executables:
428 https://sourceware.org/ml/libc-help/2014-03/msg00024.html
429 For older GNU libc r_debug.r_map is NULL. For GNU libc after
430 PR libc/16831 due to GDB PR threads/16954 LOAD_MODULE is also NULL.
431 The constant number 1 depends on GNU __libc_setup_tls
432 initialization of l_tls_modid to 1. */
433 err
= thread_db
->td_thr_tlsbase_p (&lwp
->th
, 1, &addr
);
434 addr
= (char *) addr
+ offset
;
439 *address
= (CORE_ADDR
) (uintptr_t) addr
;
446 /* See linux-low.h. */
449 thread_db_thread_handle (ptid_t ptid
, gdb_byte
**handle
, int *handle_len
)
451 struct thread_db
*thread_db
;
452 struct lwp_info
*lwp
;
453 thread_info
*thread
= find_thread_ptid (ptid
);
458 thread_db
= get_thread_process (thread
)->priv
->thread_db
;
460 if (thread_db
== NULL
)
463 lwp
= get_thread_lwp (thread
);
465 if (!lwp
->thread_known
&& !find_one_thread (thread
->id
))
468 gdb_assert (lwp
->thread_known
);
470 *handle
= (gdb_byte
*) &lwp
->thread_handle
;
471 *handle_len
= sizeof (lwp
->thread_handle
);
475 #ifdef USE_LIBTHREAD_DB_DIRECTLY
478 thread_db_load_search (void)
481 struct thread_db
*tdb
;
482 struct process_info
*proc
= current_process ();
484 gdb_assert (proc
->priv
->thread_db
== NULL
);
486 tdb
= XCNEW (struct thread_db
);
487 proc
->priv
->thread_db
= tdb
;
489 tdb
->td_ta_new_p
= &td_ta_new
;
491 /* Attempt to open a connection to the thread library. */
492 err
= tdb
->td_ta_new_p (&tdb
->proc_handle
, &tdb
->thread_agent
);
495 threads_debug_printf ("td_ta_new(): %s", thread_db_err_str (err
));
497 proc
->priv
->thread_db
= NULL
;
501 tdb
->td_ta_map_lwp2thr_p
= &td_ta_map_lwp2thr
;
502 tdb
->td_thr_get_info_p
= &td_thr_get_info
;
503 tdb
->td_ta_thr_iter_p
= &td_ta_thr_iter
;
504 tdb
->td_symbol_list_p
= &td_symbol_list
;
506 /* These are not essential. */
507 tdb
->td_thr_tls_get_addr_p
= &td_thr_tls_get_addr
;
508 tdb
->td_thr_tlsbase_p
= &td_thr_tlsbase
;
516 try_thread_db_load_1 (void *handle
)
519 struct thread_db
*tdb
;
520 struct process_info
*proc
= current_process ();
522 gdb_assert (proc
->priv
->thread_db
== NULL
);
524 tdb
= XCNEW (struct thread_db
);
525 proc
->priv
->thread_db
= tdb
;
527 tdb
->handle
= handle
;
529 /* Initialize pointers to the dynamic library functions we will use.
530 Essential functions first. */
532 #define CHK(required, a) \
537 threads_debug_printf ("dlsym: %s", dlerror ()); \
541 proc->priv->thread_db = NULL; \
548 #define TDB_DLSYM(tdb, func) \
549 tdb->func ## _p = (func ## _ftype *) dlsym (tdb->handle, #func)
551 CHK (1, TDB_DLSYM (tdb
, td_ta_new
));
553 /* Attempt to open a connection to the thread library. */
554 err
= tdb
->td_ta_new_p (&tdb
->proc_handle
, &tdb
->thread_agent
);
557 threads_debug_printf ("td_ta_new(): %s", thread_db_err_str (err
));
559 proc
->priv
->thread_db
= NULL
;
563 CHK (1, TDB_DLSYM (tdb
, td_ta_map_lwp2thr
));
564 CHK (1, TDB_DLSYM (tdb
, td_thr_get_info
));
565 CHK (1, TDB_DLSYM (tdb
, td_ta_thr_iter
));
566 CHK (1, TDB_DLSYM (tdb
, td_symbol_list
));
568 /* These are not essential. */
569 CHK (0, TDB_DLSYM (tdb
, td_thr_tls_get_addr
));
570 CHK (0, TDB_DLSYM (tdb
, td_thr_tlsbase
));
580 /* Lookup a library in which given symbol resides.
581 Note: this is looking in the GDBSERVER process, not in the inferior.
582 Returns library name, or NULL. */
585 dladdr_to_soname (const void *addr
)
589 if (dladdr (addr
, &info
) != 0)
590 return info
.dli_fname
;
597 try_thread_db_load (const char *library
)
601 threads_debug_printf ("Trying host libthread_db library: %s.",
603 handle
= dlopen (library
, RTLD_NOW
);
606 threads_debug_printf ("dlopen failed: %s.", dlerror ());
611 if (debug_threads
&& strchr (library
, '/') == NULL
)
615 td_init
= dlsym (handle
, "td_init");
618 const char *const libpath
= dladdr_to_soname (td_init
);
621 threads_debug_printf ("Host %s resolved to: %s.", library
, libpath
);
626 if (try_thread_db_load_1 (handle
))
629 /* This library "refused" to work on current inferior. */
634 /* Handle $sdir in libthread-db-search-path.
635 Look for libthread_db in the system dirs, or wherever a plain
636 dlopen(file_without_path) will look.
637 The result is true for success. */
640 try_thread_db_load_from_sdir (void)
642 return try_thread_db_load (LIBTHREAD_DB_SO
);
645 /* Try to load libthread_db from directory DIR of length DIR_LEN.
646 The result is true for success. */
649 try_thread_db_load_from_dir (const char *dir
, size_t dir_len
)
653 if (dir_len
+ 1 + strlen (LIBTHREAD_DB_SO
) + 1 > sizeof (path
))
655 char *cp
= (char *) xmalloc (dir_len
+ 1);
657 memcpy (cp
, dir
, dir_len
);
659 warning (_("libthread-db-search-path component too long,"
660 " ignored: %s."), cp
);
665 memcpy (path
, dir
, dir_len
);
667 strcpy (path
+ dir_len
+ 1, LIBTHREAD_DB_SO
);
668 return try_thread_db_load (path
);
671 /* Search libthread_db_search_path for libthread_db which "agrees"
672 to work on current inferior.
673 The result is true for success. */
676 thread_db_load_search (void)
680 if (libthread_db_search_path
== NULL
)
681 libthread_db_search_path
= xstrdup (LIBTHREAD_DB_SEARCH_PATH
);
683 std::vector
<gdb::unique_xmalloc_ptr
<char>> dir_vec
684 = dirnames_to_char_ptr_vec (libthread_db_search_path
);
686 for (const gdb::unique_xmalloc_ptr
<char> &this_dir_up
: dir_vec
)
688 char *this_dir
= this_dir_up
.get ();
689 const int pdir_len
= sizeof ("$pdir") - 1;
692 this_dir_len
= strlen (this_dir
);
694 if (strncmp (this_dir
, "$pdir", pdir_len
) == 0
695 && (this_dir
[pdir_len
] == '\0'
696 || this_dir
[pdir_len
] == '/'))
698 /* We don't maintain a list of loaded libraries so we don't know
699 where libpthread lives. We *could* fetch the info, but we don't
700 do that yet. Ignore it. */
702 else if (strcmp (this_dir
, "$sdir") == 0)
704 if (try_thread_db_load_from_sdir ())
712 if (try_thread_db_load_from_dir (this_dir
, this_dir_len
))
720 threads_debug_printf ("thread_db_load_search returning %d", rc
);
724 #endif /* USE_LIBTHREAD_DB_DIRECTLY */
727 thread_db_init (void)
729 struct process_info
*proc
= current_process ();
731 /* FIXME drow/2004-10-16: This is the "overall process ID", which
732 GNU/Linux calls tgid, "thread group ID". When we support
733 attaching to threads, the original thread may not be the correct
734 thread. We would have to get the process ID from /proc for NPTL.
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 if (thread_db_load_search ())
741 /* It's best to avoid td_ta_thr_iter if possible. That walks
742 data structures in the inferior's address space that may be
743 corrupted, or, if the target is running, the list may change
744 while we walk it. In the latter case, it's possible that a
745 thread exits just at the exact time that causes GDBserver to
746 get stuck in an infinite loop. As the kernel supports clone
747 events and /proc/PID/task/ exists, then we already know about
748 all threads in the process. When we need info out of
749 thread_db on a given thread (e.g., for TLS), we'll use
750 find_one_thread then. That uses thread_db entry points that
751 do not walk libpthread's thread list, so should be safe, as
752 well as more efficient. */
753 if (!linux_proc_task_list_dir_exists (pid_of (proc
)))
754 thread_db_find_new_threads ();
755 thread_db_look_up_symbols ();
762 /* Disconnect from libthread_db and free resources. */
765 disable_thread_event_reporting (struct process_info
*proc
)
767 struct thread_db
*thread_db
= proc
->priv
->thread_db
;
770 td_err_e (*td_ta_clear_event_p
) (const td_thragent_t
*ta
,
771 td_thr_events_t
*event
);
773 #ifndef USE_LIBTHREAD_DB_DIRECTLY
775 = (td_ta_clear_event_ftype
*) dlsym (thread_db
->handle
,
776 "td_ta_clear_event");
778 td_ta_clear_event_p
= &td_ta_clear_event
;
781 if (td_ta_clear_event_p
!= NULL
)
783 scoped_restore_current_thread restore_thread
;
784 td_thr_events_t events
;
786 switch_to_process (proc
);
788 /* Set the process wide mask saying we aren't interested
789 in any events anymore. */
790 td_event_fillset (&events
);
791 (*td_ta_clear_event_p
) (thread_db
->thread_agent
, &events
);
797 thread_db_detach (struct process_info
*proc
)
799 struct thread_db
*thread_db
= proc
->priv
->thread_db
;
803 disable_thread_event_reporting (proc
);
807 /* Disconnect from libthread_db and free resources. */
810 thread_db_mourn (struct process_info
*proc
)
812 struct thread_db
*thread_db
= proc
->priv
->thread_db
;
815 td_ta_delete_ftype
*td_ta_delete_p
;
817 #ifndef USE_LIBTHREAD_DB_DIRECTLY
818 td_ta_delete_p
= (td_ta_delete_ftype
*) dlsym (thread_db
->handle
, "td_ta_delete");
820 td_ta_delete_p
= &td_ta_delete
;
823 if (td_ta_delete_p
!= NULL
)
824 (*td_ta_delete_p
) (thread_db
->thread_agent
);
826 #ifndef USE_LIBTHREAD_DB_DIRECTLY
827 dlclose (thread_db
->handle
);
828 #endif /* USE_LIBTHREAD_DB_DIRECTLY */
831 proc
->priv
->thread_db
= NULL
;
835 /* Handle "set libthread-db-search-path" monitor command and return 1.
836 For any other command, return 0. */
839 thread_db_handle_monitor_command (char *mon
)
841 const char *cmd
= "set libthread-db-search-path";
842 size_t cmd_len
= strlen (cmd
);
844 if (strncmp (mon
, cmd
, cmd_len
) == 0
845 && (mon
[cmd_len
] == '\0'
846 || mon
[cmd_len
] == ' '))
848 const char *cp
= mon
+ cmd_len
;
850 if (libthread_db_search_path
!= NULL
)
851 free (libthread_db_search_path
);
853 /* Skip leading space (if any). */
854 while (isspace (*cp
))
858 cp
= LIBTHREAD_DB_SEARCH_PATH
;
859 libthread_db_search_path
= xstrdup (cp
);
861 monitor_output ("libthread-db-search-path set to `");
862 monitor_output (libthread_db_search_path
);
863 monitor_output ("'\n");
867 /* Tell server.c to perform default processing. */
871 /* See linux-low.h. */
874 thread_db_notice_clone (struct thread_info
*parent_thr
, ptid_t child_ptid
)
876 process_info
*parent_proc
= get_thread_process (parent_thr
);
877 struct thread_db
*thread_db
= parent_proc
->priv
->thread_db
;
879 /* If the thread layer isn't initialized, return. It may just
880 be that the program uses clone, but does not use libthread_db. */
881 if (thread_db
== NULL
|| !thread_db
->all_symbols_looked_up
)
884 /* find_one_thread calls into libthread_db which accesses memory via
885 the current thread. Temporarily switch to a thread we know is
887 scoped_restore_current_thread restore_thread
;
888 switch_to_thread (parent_thr
);
890 if (!find_one_thread (child_ptid
))
891 warning ("Cannot find thread after clone.");