1 /* Thread management interface, for the remote server for GDB.
2 Copyright (C) 2002-2019 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"
29 #include "common/gdb_vecs.h"
30 #include "nat/linux-procfs.h"
31 #include "common/scoped_restore.h"
33 #ifndef USE_LIBTHREAD_DB_DIRECTLY
41 /* Structure that identifies the child process for the
42 <proc_service.h> interface. */
43 struct ps_prochandle proc_handle
;
45 /* Connection to the libthread_db library. */
46 td_thragent_t
*thread_agent
;
48 /* If this flag has been set, we've already asked GDB for all
49 symbols we might need; assume symbol cache misses are
51 int all_symbols_looked_up
;
53 #ifndef USE_LIBTHREAD_DB_DIRECTLY
54 /* Handle of the libthread_db from dlopen. */
58 /* Addresses of libthread_db functions. */
59 td_ta_new_ftype
*td_ta_new_p
;
60 td_ta_map_lwp2thr_ftype
*td_ta_map_lwp2thr_p
;
61 td_thr_get_info_ftype
*td_thr_get_info_p
;
62 td_ta_thr_iter_ftype
*td_ta_thr_iter_p
;
63 td_thr_tls_get_addr_ftype
*td_thr_tls_get_addr_p
;
64 td_thr_tlsbase_ftype
*td_thr_tlsbase_p
;
65 td_symbol_list_ftype
*td_symbol_list_p
;
68 static char *libthread_db_search_path
;
70 static int find_one_thread (ptid_t
);
71 static int find_new_threads_callback (const td_thrhandle_t
*th_p
, void *data
);
74 thread_db_err_str (td_err_e err
)
81 return "generic 'call succeeded'";
83 return "generic error";
85 return "no thread to satisfy query";
87 return "no sync handle to satisfy query";
89 return "no LWP to satisfy query";
91 return "invalid process handle";
93 return "invalid thread handle";
95 return "invalid synchronization handle";
97 return "invalid thread agent";
101 return "no event message for getmsg";
103 return "FPU register set not available";
105 return "application not linked with libthread";
107 return "requested event is not supported";
109 return "capability not available";
111 return "debugger service failed";
113 return "operation not applicable to";
115 return "no thread-specific data for this thread";
117 return "malloc failed";
119 return "only part of register set was written/read";
121 return "X register set not available for this thread";
122 #ifdef HAVE_TD_VERSION
124 return "version mismatch between libthread_db and libpthread";
127 xsnprintf (buf
, sizeof (buf
), "unknown thread_db error '%d'", err
);
134 thread_db_state_str (td_thr_state_e state
)
141 return "stopped by debugger";
150 case TD_THR_STOPPED_ASLEEP
:
151 return "stopped by debugger AND blocked";
153 xsnprintf (buf
, sizeof (buf
), "unknown thread_db state %d", state
);
159 /* Get thread info about PTID, accessing memory via the current
163 find_one_thread (ptid_t ptid
)
168 struct lwp_info
*lwp
;
169 struct thread_db
*thread_db
= current_process ()->priv
->thread_db
;
170 int lwpid
= ptid
.lwp ();
172 thread_info
*thread
= find_thread_ptid (ptid
);
173 lwp
= get_thread_lwp (thread
);
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;
206 lwp
->thread_handle
= ti
.ti_tid
;
211 /* Attach a thread. Return true on success. */
214 attach_thread (const td_thrhandle_t
*th_p
, td_thrinfo_t
*ti_p
)
216 struct process_info
*proc
= current_process ();
217 int pid
= pid_of (proc
);
218 ptid_t ptid
= ptid_t (pid
, ti_p
->ti_lid
, 0);
219 struct lwp_info
*lwp
;
223 debug_printf ("Attaching to thread %ld (LWP %d)\n",
224 (unsigned long) ti_p
->ti_tid
, ti_p
->ti_lid
);
225 err
= linux_attach_lwp (ptid
);
228 std::string reason
= linux_ptrace_attach_fail_reason_string (ptid
, err
);
230 warning ("Could not attach to thread %ld (LWP %d): %s\n",
231 (unsigned long) ti_p
->ti_tid
, ti_p
->ti_lid
, reason
.c_str ());
236 lwp
= find_lwp_pid (ptid
);
237 gdb_assert (lwp
!= NULL
);
238 lwp
->thread_known
= 1;
240 lwp
->thread_handle
= ti_p
->ti_tid
;
245 /* Attach thread if we haven't seen it yet.
246 Increment *COUNTER if we have attached a new thread.
247 Return false on failure. */
250 maybe_attach_thread (const td_thrhandle_t
*th_p
, td_thrinfo_t
*ti_p
,
253 struct lwp_info
*lwp
;
255 lwp
= find_lwp_pid (ptid_t (ti_p
->ti_lid
));
259 if (!attach_thread (th_p
, ti_p
))
269 find_new_threads_callback (const td_thrhandle_t
*th_p
, void *data
)
273 struct thread_db
*thread_db
= current_process ()->priv
->thread_db
;
275 err
= thread_db
->td_thr_get_info_p (th_p
, &ti
);
277 error ("Cannot get thread info: %s", thread_db_err_str (err
));
281 /* A thread with kernel thread ID -1 is either a thread that
282 exited and was joined, or a thread that is being created but
283 hasn't started yet, and that is reusing the tcb/stack of a
284 thread that previously exited and was joined. (glibc marks
285 terminated and joined threads with kernel thread ID -1. See
288 debug_printf ("thread_db: skipping exited and "
289 "joined thread (0x%lx)\n",
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
);
338 debug_printf ("Found %d threads in iteration %d.\n",
339 new_thread_count
, iteration
);
341 if (new_thread_count
!= 0)
343 /* Found new threads. Restart iteration from beginning. */
348 error ("Cannot find new threads: %s", thread_db_err_str (err
));
351 /* Cache all future symbols that thread_db might request. We can not
352 request symbols at arbitrary states in the remote protocol, only
353 when the client tells us that new symbols are available. So when
354 we load the thread library, make sure to check the entire list. */
357 thread_db_look_up_symbols (void)
359 struct thread_db
*thread_db
= current_process ()->priv
->thread_db
;
360 const char **sym_list
;
363 for (sym_list
= thread_db
->td_symbol_list_p (); *sym_list
; sym_list
++)
364 look_up_one_symbol (*sym_list
, &unused
, 1);
366 /* We're not interested in any other libraries loaded after this
367 point, only in symbols in libpthread.so. */
368 thread_db
->all_symbols_looked_up
= 1;
372 thread_db_look_up_one_symbol (const char *name
, CORE_ADDR
*addrp
)
374 struct thread_db
*thread_db
= current_process ()->priv
->thread_db
;
375 int may_ask_gdb
= !thread_db
->all_symbols_looked_up
;
377 /* If we've passed the call to thread_db_look_up_symbols, then
378 anything not in the cache must not exist; we're not interested
379 in any libraries loaded after that point, only in symbols in
380 libpthread.so. It might not be an appropriate time to look
381 up a symbol, e.g. while we're trying to fetch registers. */
382 return look_up_one_symbol (name
, addrp
, may_ask_gdb
);
386 thread_db_get_tls_address (struct thread_info
*thread
, CORE_ADDR offset
,
387 CORE_ADDR load_module
, CORE_ADDR
*address
)
391 struct lwp_info
*lwp
;
392 struct thread_info
*saved_thread
;
393 struct process_info
*proc
;
394 struct thread_db
*thread_db
;
396 proc
= get_thread_process (thread
);
397 thread_db
= proc
->priv
->thread_db
;
399 /* If the thread layer is not (yet) initialized, fail. */
400 if (thread_db
== NULL
|| !thread_db
->all_symbols_looked_up
)
403 /* If td_thr_tls_get_addr is missing rather do not expect td_thr_tlsbase
405 if (thread_db
->td_thr_tls_get_addr_p
== NULL
406 || (load_module
== 0 && thread_db
->td_thr_tlsbase_p
== NULL
))
409 lwp
= get_thread_lwp (thread
);
410 if (!lwp
->thread_known
)
411 find_one_thread (thread
->id
);
412 if (!lwp
->thread_known
)
415 saved_thread
= current_thread
;
416 current_thread
= thread
;
418 if (load_module
!= 0)
420 /* Note the cast through uintptr_t: this interface only works if
421 a target address fits in a psaddr_t, which is a host pointer.
422 So a 32-bit debugger can not access 64-bit TLS through this. */
423 err
= thread_db
->td_thr_tls_get_addr_p (&lwp
->th
,
424 (psaddr_t
) (uintptr_t) load_module
,
429 /* This code path handles the case of -static -pthread executables:
430 https://sourceware.org/ml/libc-help/2014-03/msg00024.html
431 For older GNU libc r_debug.r_map is NULL. For GNU libc after
432 PR libc/16831 due to GDB PR threads/16954 LOAD_MODULE is also NULL.
433 The constant number 1 depends on GNU __libc_setup_tls
434 initialization of l_tls_modid to 1. */
435 err
= thread_db
->td_thr_tlsbase_p (&lwp
->th
, 1, &addr
);
436 addr
= (char *) addr
+ offset
;
439 current_thread
= saved_thread
;
442 *address
= (CORE_ADDR
) (uintptr_t) addr
;
449 /* See linux-low.h. */
452 thread_db_thread_handle (ptid_t ptid
, gdb_byte
**handle
, int *handle_len
)
454 struct thread_db
*thread_db
;
455 struct lwp_info
*lwp
;
456 thread_info
*thread
= find_thread_ptid (ptid
);
461 thread_db
= get_thread_process (thread
)->priv
->thread_db
;
463 if (thread_db
== NULL
)
466 lwp
= get_thread_lwp (thread
);
468 if (!lwp
->thread_known
&& !find_one_thread (thread
->id
))
471 gdb_assert (lwp
->thread_known
);
473 *handle
= (gdb_byte
*) &lwp
->thread_handle
;
474 *handle_len
= sizeof (lwp
->thread_handle
);
478 #ifdef USE_LIBTHREAD_DB_DIRECTLY
481 thread_db_load_search (void)
484 struct thread_db
*tdb
;
485 struct process_info
*proc
= current_process ();
487 gdb_assert (proc
->priv
->thread_db
== NULL
);
489 tdb
= XCNEW (struct thread_db
);
490 proc
->priv
->thread_db
= tdb
;
492 tdb
->td_ta_new_p
= &td_ta_new
;
494 /* Attempt to open a connection to the thread library. */
495 err
= tdb
->td_ta_new_p (&tdb
->proc_handle
, &tdb
->thread_agent
);
499 debug_printf ("td_ta_new(): %s\n", thread_db_err_str (err
));
501 proc
->priv
->thread_db
= NULL
;
505 tdb
->td_ta_map_lwp2thr_p
= &td_ta_map_lwp2thr
;
506 tdb
->td_thr_get_info_p
= &td_thr_get_info
;
507 tdb
->td_ta_thr_iter_p
= &td_ta_thr_iter
;
508 tdb
->td_symbol_list_p
= &td_symbol_list
;
510 /* These are not essential. */
511 tdb
->td_thr_tls_get_addr_p
= &td_thr_tls_get_addr
;
512 tdb
->td_thr_tlsbase_p
= &td_thr_tlsbase
;
520 try_thread_db_load_1 (void *handle
)
523 struct thread_db
*tdb
;
524 struct process_info
*proc
= current_process ();
526 gdb_assert (proc
->priv
->thread_db
== NULL
);
528 tdb
= XCNEW (struct thread_db
);
529 proc
->priv
->thread_db
= tdb
;
531 tdb
->handle
= handle
;
533 /* Initialize pointers to the dynamic library functions we will use.
534 Essential functions first. */
536 #define CHK(required, a) \
542 debug_printf ("dlsym: %s\n", dlerror ()); \
546 proc->priv->thread_db = NULL; \
553 #define TDB_DLSYM(tdb, func) \
554 tdb->func ## _p = (func ## _ftype *) dlsym (tdb->handle, #func)
556 CHK (1, TDB_DLSYM (tdb
, td_ta_new
));
558 /* Attempt to open a connection to the thread library. */
559 err
= tdb
->td_ta_new_p (&tdb
->proc_handle
, &tdb
->thread_agent
);
563 debug_printf ("td_ta_new(): %s\n", thread_db_err_str (err
));
565 proc
->priv
->thread_db
= NULL
;
569 CHK (1, TDB_DLSYM (tdb
, td_ta_map_lwp2thr
));
570 CHK (1, TDB_DLSYM (tdb
, td_thr_get_info
));
571 CHK (1, TDB_DLSYM (tdb
, td_ta_thr_iter
));
572 CHK (1, TDB_DLSYM (tdb
, td_symbol_list
));
574 /* These are not essential. */
575 CHK (0, TDB_DLSYM (tdb
, td_thr_tls_get_addr
));
576 CHK (0, TDB_DLSYM (tdb
, td_thr_tlsbase
));
586 /* Lookup a library in which given symbol resides.
587 Note: this is looking in the GDBSERVER process, not in the inferior.
588 Returns library name, or NULL. */
591 dladdr_to_soname (const void *addr
)
595 if (dladdr (addr
, &info
) != 0)
596 return info
.dli_fname
;
603 try_thread_db_load (const char *library
)
608 debug_printf ("Trying host libthread_db library: %s.\n",
610 handle
= dlopen (library
, RTLD_NOW
);
614 debug_printf ("dlopen failed: %s.\n", dlerror ());
619 if (debug_threads
&& strchr (library
, '/') == NULL
)
623 td_init
= dlsym (handle
, "td_init");
626 const char *const libpath
= dladdr_to_soname (td_init
);
629 debug_printf ("Host %s resolved to: %s.\n", library
, libpath
);
634 if (try_thread_db_load_1 (handle
))
637 /* This library "refused" to work on current inferior. */
642 /* Handle $sdir in libthread-db-search-path.
643 Look for libthread_db in the system dirs, or wherever a plain
644 dlopen(file_without_path) will look.
645 The result is true for success. */
648 try_thread_db_load_from_sdir (void)
650 return try_thread_db_load (LIBTHREAD_DB_SO
);
653 /* Try to load libthread_db from directory DIR of length DIR_LEN.
654 The result is true for success. */
657 try_thread_db_load_from_dir (const char *dir
, size_t dir_len
)
661 if (dir_len
+ 1 + strlen (LIBTHREAD_DB_SO
) + 1 > sizeof (path
))
663 char *cp
= (char *) xmalloc (dir_len
+ 1);
665 memcpy (cp
, dir
, dir_len
);
667 warning (_("libthread-db-search-path component too long,"
668 " ignored: %s."), cp
);
673 memcpy (path
, dir
, dir_len
);
675 strcpy (path
+ dir_len
+ 1, LIBTHREAD_DB_SO
);
676 return try_thread_db_load (path
);
679 /* Search libthread_db_search_path for libthread_db which "agrees"
680 to work on current inferior.
681 The result is true for success. */
684 thread_db_load_search (void)
688 if (libthread_db_search_path
== NULL
)
689 libthread_db_search_path
= xstrdup (LIBTHREAD_DB_SEARCH_PATH
);
691 std::vector
<gdb::unique_xmalloc_ptr
<char>> dir_vec
692 = dirnames_to_char_ptr_vec (libthread_db_search_path
);
694 for (const gdb::unique_xmalloc_ptr
<char> &this_dir_up
: dir_vec
)
696 char *this_dir
= this_dir_up
.get ();
697 const int pdir_len
= sizeof ("$pdir") - 1;
700 this_dir_len
= strlen (this_dir
);
702 if (strncmp (this_dir
, "$pdir", pdir_len
) == 0
703 && (this_dir
[pdir_len
] == '\0'
704 || this_dir
[pdir_len
] == '/'))
706 /* We don't maintain a list of loaded libraries so we don't know
707 where libpthread lives. We *could* fetch the info, but we don't
708 do that yet. Ignore it. */
710 else if (strcmp (this_dir
, "$sdir") == 0)
712 if (try_thread_db_load_from_sdir ())
720 if (try_thread_db_load_from_dir (this_dir
, this_dir_len
))
729 debug_printf ("thread_db_load_search returning %d\n", rc
);
733 #endif /* USE_LIBTHREAD_DB_DIRECTLY */
736 thread_db_init (void)
738 struct process_info
*proc
= current_process ();
740 /* FIXME drow/2004-10-16: This is the "overall process ID", which
741 GNU/Linux calls tgid, "thread group ID". When we support
742 attaching to threads, the original thread may not be the correct
743 thread. We would have to get the process ID from /proc for NPTL.
745 This isn't the only place in gdbserver that assumes that the first
746 process in the list is the thread group leader. */
748 if (thread_db_load_search ())
750 /* It's best to avoid td_ta_thr_iter if possible. That walks
751 data structures in the inferior's address space that may be
752 corrupted, or, if the target is running, the list may change
753 while we walk it. In the latter case, it's possible that a
754 thread exits just at the exact time that causes GDBserver to
755 get stuck in an infinite loop. As the kernel supports clone
756 events and /proc/PID/task/ exists, then we already know about
757 all threads in the process. When we need info out of
758 thread_db on a given thread (e.g., for TLS), we'll use
759 find_one_thread then. That uses thread_db entry points that
760 do not walk libpthread's thread list, so should be safe, as
761 well as more efficient. */
762 if (!linux_proc_task_list_dir_exists (pid_of (proc
)))
763 thread_db_find_new_threads ();
764 thread_db_look_up_symbols ();
772 switch_to_process (struct process_info
*proc
)
774 int pid
= pid_of (proc
);
776 current_thread
= find_any_thread_of_pid (pid
);
779 /* Disconnect from libthread_db and free resources. */
782 disable_thread_event_reporting (struct process_info
*proc
)
784 struct thread_db
*thread_db
= proc
->priv
->thread_db
;
787 td_err_e (*td_ta_clear_event_p
) (const td_thragent_t
*ta
,
788 td_thr_events_t
*event
);
790 #ifndef USE_LIBTHREAD_DB_DIRECTLY
792 = (td_ta_clear_event_ftype
*) dlsym (thread_db
->handle
,
793 "td_ta_clear_event");
795 td_ta_clear_event_p
= &td_ta_clear_event
;
798 if (td_ta_clear_event_p
!= NULL
)
800 struct thread_info
*saved_thread
= current_thread
;
801 td_thr_events_t events
;
803 switch_to_process (proc
);
805 /* Set the process wide mask saying we aren't interested
806 in any events anymore. */
807 td_event_fillset (&events
);
808 (*td_ta_clear_event_p
) (thread_db
->thread_agent
, &events
);
810 current_thread
= saved_thread
;
816 thread_db_detach (struct process_info
*proc
)
818 struct thread_db
*thread_db
= proc
->priv
->thread_db
;
822 disable_thread_event_reporting (proc
);
826 /* Disconnect from libthread_db and free resources. */
829 thread_db_mourn (struct process_info
*proc
)
831 struct thread_db
*thread_db
= proc
->priv
->thread_db
;
834 td_ta_delete_ftype
*td_ta_delete_p
;
836 #ifndef USE_LIBTHREAD_DB_DIRECTLY
837 td_ta_delete_p
= (td_ta_delete_ftype
*) dlsym (thread_db
->handle
, "td_ta_delete");
839 td_ta_delete_p
= &td_ta_delete
;
842 if (td_ta_delete_p
!= NULL
)
843 (*td_ta_delete_p
) (thread_db
->thread_agent
);
845 #ifndef USE_LIBTHREAD_DB_DIRECTLY
846 dlclose (thread_db
->handle
);
847 #endif /* USE_LIBTHREAD_DB_DIRECTLY */
850 proc
->priv
->thread_db
= NULL
;
854 /* Handle "set libthread-db-search-path" monitor command and return 1.
855 For any other command, return 0. */
858 thread_db_handle_monitor_command (char *mon
)
860 const char *cmd
= "set libthread-db-search-path";
861 size_t cmd_len
= strlen (cmd
);
863 if (strncmp (mon
, cmd
, cmd_len
) == 0
864 && (mon
[cmd_len
] == '\0'
865 || mon
[cmd_len
] == ' '))
867 const char *cp
= mon
+ cmd_len
;
869 if (libthread_db_search_path
!= NULL
)
870 free (libthread_db_search_path
);
872 /* Skip leading space (if any). */
873 while (isspace (*cp
))
877 cp
= LIBTHREAD_DB_SEARCH_PATH
;
878 libthread_db_search_path
= xstrdup (cp
);
880 monitor_output ("libthread-db-search-path set to `");
881 monitor_output (libthread_db_search_path
);
882 monitor_output ("'\n");
886 /* Tell server.c to perform default processing. */
890 /* See linux-low.h. */
893 thread_db_notice_clone (struct thread_info
*parent_thr
, ptid_t child_ptid
)
895 process_info
*parent_proc
= get_thread_process (parent_thr
);
896 struct thread_db
*thread_db
= parent_proc
->priv
->thread_db
;
898 /* If the thread layer isn't initialized, return. It may just
899 be that the program uses clone, but does not use libthread_db. */
900 if (thread_db
== NULL
|| !thread_db
->all_symbols_looked_up
)
903 /* find_one_thread calls into libthread_db which accesses memory via
904 the current thread. Temporarily switch to a thread we know is
906 scoped_restore restore_current_thread
907 = make_scoped_restore (¤t_thread
, parent_thr
);
909 if (!find_one_thread (child_ptid
))
910 warning ("Cannot find thread after clone.\n");