18dffb485Schristos /* Thread management interface, for the remote server for GDB. 2*2be465b0Schristos Copyright (C) 2002-2024 Free Software Foundation, Inc. 38dffb485Schristos 48dffb485Schristos Contributed by MontaVista Software. 58dffb485Schristos 68dffb485Schristos This file is part of GDB. 78dffb485Schristos 88dffb485Schristos This program is free software; you can redistribute it and/or modify 98dffb485Schristos it under the terms of the GNU General Public License as published by 108dffb485Schristos the Free Software Foundation; either version 3 of the License, or 118dffb485Schristos (at your option) any later version. 128dffb485Schristos 138dffb485Schristos This program is distributed in the hope that it will be useful, 148dffb485Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of 158dffb485Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 168dffb485Schristos GNU General Public License for more details. 178dffb485Schristos 188dffb485Schristos You should have received a copy of the GNU General Public License 198dffb485Schristos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 208dffb485Schristos 218dffb485Schristos 228dffb485Schristos #include "linux-low.h" 238dffb485Schristos 248dffb485Schristos #include "debug.h" 258dffb485Schristos #include "gdb_proc_service.h" 268dffb485Schristos #include "nat/gdb_thread_db.h" 278dffb485Schristos #include "gdbsupport/gdb_vecs.h" 288dffb485Schristos #include "nat/linux-procfs.h" 298dffb485Schristos #include "gdbsupport/scoped_restore.h" 308dffb485Schristos 318dffb485Schristos #ifndef USE_LIBTHREAD_DB_DIRECTLY 328dffb485Schristos #include <dlfcn.h> 338dffb485Schristos #endif 348dffb485Schristos #include <limits.h> 358dffb485Schristos #include <ctype.h> 368dffb485Schristos 378dffb485Schristos struct thread_db 388dffb485Schristos { 398dffb485Schristos /* Structure that identifies the child process for the 408dffb485Schristos <proc_service.h> interface. */ 418dffb485Schristos struct ps_prochandle proc_handle; 428dffb485Schristos 438dffb485Schristos /* Connection to the libthread_db library. */ 448dffb485Schristos td_thragent_t *thread_agent; 458dffb485Schristos 468dffb485Schristos /* If this flag has been set, we've already asked GDB for all 478dffb485Schristos symbols we might need; assume symbol cache misses are 488dffb485Schristos failures. */ 498dffb485Schristos int all_symbols_looked_up; 508dffb485Schristos 518dffb485Schristos #ifndef USE_LIBTHREAD_DB_DIRECTLY 528dffb485Schristos /* Handle of the libthread_db from dlopen. */ 538dffb485Schristos void *handle; 548dffb485Schristos #endif 558dffb485Schristos 568dffb485Schristos /* Addresses of libthread_db functions. */ 578dffb485Schristos td_ta_new_ftype *td_ta_new_p; 588dffb485Schristos td_ta_map_lwp2thr_ftype *td_ta_map_lwp2thr_p; 598dffb485Schristos td_thr_get_info_ftype *td_thr_get_info_p; 608dffb485Schristos td_ta_thr_iter_ftype *td_ta_thr_iter_p; 618dffb485Schristos td_thr_tls_get_addr_ftype *td_thr_tls_get_addr_p; 628dffb485Schristos td_thr_tlsbase_ftype *td_thr_tlsbase_p; 638dffb485Schristos td_symbol_list_ftype *td_symbol_list_p; 648dffb485Schristos }; 658dffb485Schristos 668dffb485Schristos static char *libthread_db_search_path; 678dffb485Schristos 688dffb485Schristos static int find_one_thread (ptid_t); 698dffb485Schristos static int find_new_threads_callback (const td_thrhandle_t *th_p, void *data); 708dffb485Schristos 718dffb485Schristos static const char * 728dffb485Schristos thread_db_err_str (td_err_e err) 738dffb485Schristos { 748dffb485Schristos static char buf[64]; 758dffb485Schristos 768dffb485Schristos switch (err) 778dffb485Schristos { 788dffb485Schristos case TD_OK: 798dffb485Schristos return "generic 'call succeeded'"; 808dffb485Schristos case TD_ERR: 818dffb485Schristos return "generic error"; 828dffb485Schristos case TD_NOTHR: 838dffb485Schristos return "no thread to satisfy query"; 848dffb485Schristos case TD_NOSV: 858dffb485Schristos return "no sync handle to satisfy query"; 868dffb485Schristos case TD_NOLWP: 878dffb485Schristos return "no LWP to satisfy query"; 888dffb485Schristos case TD_BADPH: 898dffb485Schristos return "invalid process handle"; 908dffb485Schristos case TD_BADTH: 918dffb485Schristos return "invalid thread handle"; 928dffb485Schristos case TD_BADSH: 938dffb485Schristos return "invalid synchronization handle"; 948dffb485Schristos case TD_BADTA: 958dffb485Schristos return "invalid thread agent"; 968dffb485Schristos case TD_BADKEY: 978dffb485Schristos return "invalid key"; 988dffb485Schristos case TD_NOMSG: 998dffb485Schristos return "no event message for getmsg"; 1008dffb485Schristos case TD_NOFPREGS: 1018dffb485Schristos return "FPU register set not available"; 1028dffb485Schristos case TD_NOLIBTHREAD: 1038dffb485Schristos return "application not linked with libthread"; 1048dffb485Schristos case TD_NOEVENT: 1058dffb485Schristos return "requested event is not supported"; 1068dffb485Schristos case TD_NOCAPAB: 1078dffb485Schristos return "capability not available"; 1088dffb485Schristos case TD_DBERR: 1098dffb485Schristos return "debugger service failed"; 1108dffb485Schristos case TD_NOAPLIC: 1118dffb485Schristos return "operation not applicable to"; 1128dffb485Schristos case TD_NOTSD: 1138dffb485Schristos return "no thread-specific data for this thread"; 1148dffb485Schristos case TD_MALLOC: 1158dffb485Schristos return "malloc failed"; 1168dffb485Schristos case TD_PARTIALREG: 1178dffb485Schristos return "only part of register set was written/read"; 1188dffb485Schristos case TD_NOXREGS: 1198dffb485Schristos return "X register set not available for this thread"; 1208dffb485Schristos #ifdef HAVE_TD_VERSION 1218dffb485Schristos case TD_VERSION: 1228dffb485Schristos return "version mismatch between libthread_db and libpthread"; 1238dffb485Schristos #endif 1248dffb485Schristos default: 1258dffb485Schristos xsnprintf (buf, sizeof (buf), "unknown thread_db error '%d'", err); 1268dffb485Schristos return buf; 1278dffb485Schristos } 1288dffb485Schristos } 1298dffb485Schristos 1308dffb485Schristos #if 0 1318dffb485Schristos static char * 1328dffb485Schristos thread_db_state_str (td_thr_state_e state) 1338dffb485Schristos { 1348dffb485Schristos static char buf[64]; 1358dffb485Schristos 1368dffb485Schristos switch (state) 1378dffb485Schristos { 1388dffb485Schristos case TD_THR_STOPPED: 1398dffb485Schristos return "stopped by debugger"; 1408dffb485Schristos case TD_THR_RUN: 1418dffb485Schristos return "runnable"; 1428dffb485Schristos case TD_THR_ACTIVE: 1438dffb485Schristos return "active"; 1448dffb485Schristos case TD_THR_ZOMBIE: 1458dffb485Schristos return "zombie"; 1468dffb485Schristos case TD_THR_SLEEP: 1478dffb485Schristos return "sleeping"; 1488dffb485Schristos case TD_THR_STOPPED_ASLEEP: 1498dffb485Schristos return "stopped by debugger AND blocked"; 1508dffb485Schristos default: 1518dffb485Schristos xsnprintf (buf, sizeof (buf), "unknown thread_db state %d", state); 1528dffb485Schristos return buf; 1538dffb485Schristos } 1548dffb485Schristos } 1558dffb485Schristos #endif 1568dffb485Schristos 1574b169a6bSchristos /* Get thread info about PTID. */ 1588dffb485Schristos 1598dffb485Schristos static int 1608dffb485Schristos find_one_thread (ptid_t ptid) 1618dffb485Schristos { 1628dffb485Schristos thread_info *thread = find_thread_ptid (ptid); 1634b169a6bSchristos lwp_info *lwp = get_thread_lwp (thread); 1648dffb485Schristos if (lwp->thread_known) 1658dffb485Schristos return 1; 1668dffb485Schristos 1674b169a6bSchristos /* Get information about this thread. libthread_db will need to read some 1684b169a6bSchristos memory, which will be done on the current process, so make PTID's process 1694b169a6bSchristos the current one. */ 1704b169a6bSchristos process_info *proc = find_process_pid (ptid.pid ()); 1714b169a6bSchristos gdb_assert (proc != nullptr); 1724b169a6bSchristos 1734b169a6bSchristos scoped_restore_current_thread restore_thread; 1744b169a6bSchristos switch_to_process (proc); 1754b169a6bSchristos 1764b169a6bSchristos thread_db *thread_db = proc->priv->thread_db; 1774b169a6bSchristos td_thrhandle_t th; 1784b169a6bSchristos int lwpid = ptid.lwp (); 1794b169a6bSchristos td_err_e err = thread_db->td_ta_map_lwp2thr_p (thread_db->thread_agent, lwpid, 1804b169a6bSchristos &th); 1818dffb485Schristos if (err != TD_OK) 1828dffb485Schristos error ("Cannot get thread handle for LWP %d: %s", 1838dffb485Schristos lwpid, thread_db_err_str (err)); 1848dffb485Schristos 1854b169a6bSchristos td_thrinfo_t ti; 1868dffb485Schristos err = thread_db->td_thr_get_info_p (&th, &ti); 1878dffb485Schristos if (err != TD_OK) 1888dffb485Schristos error ("Cannot get thread info for LWP %d: %s", 1898dffb485Schristos lwpid, thread_db_err_str (err)); 1908dffb485Schristos 1914b169a6bSchristos threads_debug_printf ("Found thread %ld (LWP %d)", 1928dffb485Schristos (unsigned long) ti.ti_tid, ti.ti_lid); 1938dffb485Schristos 1948dffb485Schristos if (lwpid != ti.ti_lid) 1958dffb485Schristos { 1968dffb485Schristos warning ("PID mismatch! Expected %ld, got %ld", 1978dffb485Schristos (long) lwpid, (long) ti.ti_lid); 1988dffb485Schristos return 0; 1998dffb485Schristos } 2008dffb485Schristos 2018dffb485Schristos /* If the new thread ID is zero, a final thread ID will be available 2028dffb485Schristos later. Do not enable thread debugging yet. */ 2038dffb485Schristos if (ti.ti_tid == 0) 2048dffb485Schristos return 0; 2058dffb485Schristos 2068dffb485Schristos lwp->thread_known = 1; 2078dffb485Schristos lwp->th = th; 2088dffb485Schristos lwp->thread_handle = ti.ti_tid; 2098dffb485Schristos 2108dffb485Schristos return 1; 2118dffb485Schristos } 2128dffb485Schristos 2138dffb485Schristos /* Attach a thread. Return true on success. */ 2148dffb485Schristos 2158dffb485Schristos static int 2168dffb485Schristos attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p) 2178dffb485Schristos { 2188dffb485Schristos struct process_info *proc = current_process (); 2198dffb485Schristos int pid = pid_of (proc); 2204b169a6bSchristos ptid_t ptid = ptid_t (pid, ti_p->ti_lid); 2218dffb485Schristos struct lwp_info *lwp; 2228dffb485Schristos int err; 2238dffb485Schristos 2244b169a6bSchristos threads_debug_printf ("Attaching to thread %ld (LWP %d)", 2258dffb485Schristos (unsigned long) ti_p->ti_tid, ti_p->ti_lid); 2268dffb485Schristos err = the_linux_target->attach_lwp (ptid); 2278dffb485Schristos if (err != 0) 2288dffb485Schristos { 2298dffb485Schristos std::string reason = linux_ptrace_attach_fail_reason_string (ptid, err); 2308dffb485Schristos 2318dffb485Schristos warning ("Could not attach to thread %ld (LWP %d): %s", 2328dffb485Schristos (unsigned long) ti_p->ti_tid, ti_p->ti_lid, reason.c_str ()); 2338dffb485Schristos 2348dffb485Schristos return 0; 2358dffb485Schristos } 2368dffb485Schristos 2378dffb485Schristos lwp = find_lwp_pid (ptid); 2388dffb485Schristos gdb_assert (lwp != NULL); 2398dffb485Schristos lwp->thread_known = 1; 2408dffb485Schristos lwp->th = *th_p; 2418dffb485Schristos lwp->thread_handle = ti_p->ti_tid; 2428dffb485Schristos 2438dffb485Schristos return 1; 2448dffb485Schristos } 2458dffb485Schristos 2468dffb485Schristos /* Attach thread if we haven't seen it yet. 2478dffb485Schristos Increment *COUNTER if we have attached a new thread. 2488dffb485Schristos Return false on failure. */ 2498dffb485Schristos 2508dffb485Schristos static int 2518dffb485Schristos maybe_attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p, 2528dffb485Schristos int *counter) 2538dffb485Schristos { 2548dffb485Schristos struct lwp_info *lwp; 2558dffb485Schristos 2568dffb485Schristos lwp = find_lwp_pid (ptid_t (ti_p->ti_lid)); 2578dffb485Schristos if (lwp != NULL) 2588dffb485Schristos return 1; 2598dffb485Schristos 2608dffb485Schristos if (!attach_thread (th_p, ti_p)) 2618dffb485Schristos return 0; 2628dffb485Schristos 2638dffb485Schristos if (counter != NULL) 2648dffb485Schristos *counter += 1; 2658dffb485Schristos 2668dffb485Schristos return 1; 2678dffb485Schristos } 2688dffb485Schristos 2698dffb485Schristos static int 2708dffb485Schristos find_new_threads_callback (const td_thrhandle_t *th_p, void *data) 2718dffb485Schristos { 2728dffb485Schristos td_thrinfo_t ti; 2738dffb485Schristos td_err_e err; 2748dffb485Schristos struct thread_db *thread_db = current_process ()->priv->thread_db; 2758dffb485Schristos 2768dffb485Schristos err = thread_db->td_thr_get_info_p (th_p, &ti); 2778dffb485Schristos if (err != TD_OK) 2788dffb485Schristos error ("Cannot get thread info: %s", thread_db_err_str (err)); 2798dffb485Schristos 2808dffb485Schristos if (ti.ti_lid == -1) 2818dffb485Schristos { 2828dffb485Schristos /* A thread with kernel thread ID -1 is either a thread that 2838dffb485Schristos exited and was joined, or a thread that is being created but 2848dffb485Schristos hasn't started yet, and that is reusing the tcb/stack of a 2858dffb485Schristos thread that previously exited and was joined. (glibc marks 2868dffb485Schristos terminated and joined threads with kernel thread ID -1. See 2878dffb485Schristos glibc PR17707. */ 2884b169a6bSchristos threads_debug_printf ("thread_db: skipping exited and " 2894b169a6bSchristos "joined thread (0x%lx)", 2908dffb485Schristos (unsigned long) ti.ti_tid); 2918dffb485Schristos return 0; 2928dffb485Schristos } 2938dffb485Schristos 2948dffb485Schristos /* Check for zombies. */ 2958dffb485Schristos if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE) 2968dffb485Schristos return 0; 2978dffb485Schristos 2988dffb485Schristos if (!maybe_attach_thread (th_p, &ti, (int *) data)) 2998dffb485Schristos { 3008dffb485Schristos /* Terminate iteration early: we might be looking at stale data in 3018dffb485Schristos the inferior. The thread_db_find_new_threads will retry. */ 3028dffb485Schristos return 1; 3038dffb485Schristos } 3048dffb485Schristos 3058dffb485Schristos return 0; 3068dffb485Schristos } 3078dffb485Schristos 3088dffb485Schristos static void 3098dffb485Schristos thread_db_find_new_threads (void) 3108dffb485Schristos { 3118dffb485Schristos td_err_e err; 3128dffb485Schristos ptid_t ptid = current_ptid; 3138dffb485Schristos struct thread_db *thread_db = current_process ()->priv->thread_db; 3148dffb485Schristos int loop, iteration; 3158dffb485Schristos 3168dffb485Schristos /* This function is only called when we first initialize thread_db. 3178dffb485Schristos First locate the initial thread. If it is not ready for 3188dffb485Schristos debugging yet, then stop. */ 3198dffb485Schristos if (find_one_thread (ptid) == 0) 3208dffb485Schristos return; 3218dffb485Schristos 3228dffb485Schristos /* Require 4 successive iterations which do not find any new threads. 3238dffb485Schristos The 4 is a heuristic: there is an inherent race here, and I have 3248dffb485Schristos seen that 2 iterations in a row are not always sufficient to 3258dffb485Schristos "capture" all threads. */ 3268dffb485Schristos for (loop = 0, iteration = 0; loop < 4; ++loop, ++iteration) 3278dffb485Schristos { 3288dffb485Schristos int new_thread_count = 0; 3298dffb485Schristos 3308dffb485Schristos /* Iterate over all user-space threads to discover new threads. */ 3318dffb485Schristos err = thread_db->td_ta_thr_iter_p (thread_db->thread_agent, 3328dffb485Schristos find_new_threads_callback, 3338dffb485Schristos &new_thread_count, 3348dffb485Schristos TD_THR_ANY_STATE, 3358dffb485Schristos TD_THR_LOWEST_PRIORITY, 3368dffb485Schristos TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS); 3374b169a6bSchristos threads_debug_printf ("Found %d threads in iteration %d.", 3388dffb485Schristos new_thread_count, iteration); 3398dffb485Schristos 3408dffb485Schristos if (new_thread_count != 0) 3418dffb485Schristos { 3428dffb485Schristos /* Found new threads. Restart iteration from beginning. */ 3438dffb485Schristos loop = -1; 3448dffb485Schristos } 3458dffb485Schristos } 3468dffb485Schristos if (err != TD_OK) 3478dffb485Schristos error ("Cannot find new threads: %s", thread_db_err_str (err)); 3488dffb485Schristos } 3498dffb485Schristos 3508dffb485Schristos /* Cache all future symbols that thread_db might request. We can not 3518dffb485Schristos request symbols at arbitrary states in the remote protocol, only 3528dffb485Schristos when the client tells us that new symbols are available. So when 3538dffb485Schristos we load the thread library, make sure to check the entire list. */ 3548dffb485Schristos 3558dffb485Schristos static void 3568dffb485Schristos thread_db_look_up_symbols (void) 3578dffb485Schristos { 3588dffb485Schristos struct thread_db *thread_db = current_process ()->priv->thread_db; 3598dffb485Schristos const char **sym_list; 3608dffb485Schristos CORE_ADDR unused; 3618dffb485Schristos 3628dffb485Schristos for (sym_list = thread_db->td_symbol_list_p (); *sym_list; sym_list++) 3638dffb485Schristos look_up_one_symbol (*sym_list, &unused, 1); 3648dffb485Schristos 3658dffb485Schristos /* We're not interested in any other libraries loaded after this 3668dffb485Schristos point, only in symbols in libpthread.so. */ 3678dffb485Schristos thread_db->all_symbols_looked_up = 1; 3688dffb485Schristos } 3698dffb485Schristos 3708dffb485Schristos int 3718dffb485Schristos thread_db_look_up_one_symbol (const char *name, CORE_ADDR *addrp) 3728dffb485Schristos { 3738dffb485Schristos struct thread_db *thread_db = current_process ()->priv->thread_db; 3748dffb485Schristos int may_ask_gdb = !thread_db->all_symbols_looked_up; 3758dffb485Schristos 3768dffb485Schristos /* If we've passed the call to thread_db_look_up_symbols, then 3778dffb485Schristos anything not in the cache must not exist; we're not interested 3788dffb485Schristos in any libraries loaded after that point, only in symbols in 3798dffb485Schristos libpthread.so. It might not be an appropriate time to look 3808dffb485Schristos up a symbol, e.g. while we're trying to fetch registers. */ 3818dffb485Schristos return look_up_one_symbol (name, addrp, may_ask_gdb); 3828dffb485Schristos } 3838dffb485Schristos 3848dffb485Schristos int 3858dffb485Schristos thread_db_get_tls_address (struct thread_info *thread, CORE_ADDR offset, 3868dffb485Schristos CORE_ADDR load_module, CORE_ADDR *address) 3878dffb485Schristos { 3888dffb485Schristos psaddr_t addr; 3898dffb485Schristos td_err_e err; 3908dffb485Schristos struct lwp_info *lwp; 3918dffb485Schristos struct process_info *proc; 3928dffb485Schristos struct thread_db *thread_db; 3938dffb485Schristos 3948dffb485Schristos proc = get_thread_process (thread); 3958dffb485Schristos thread_db = proc->priv->thread_db; 3968dffb485Schristos 3978dffb485Schristos /* If the thread layer is not (yet) initialized, fail. */ 3988dffb485Schristos if (thread_db == NULL || !thread_db->all_symbols_looked_up) 3998dffb485Schristos return TD_ERR; 4008dffb485Schristos 4018dffb485Schristos /* If td_thr_tls_get_addr is missing rather do not expect td_thr_tlsbase 4028dffb485Schristos could work. */ 4038dffb485Schristos if (thread_db->td_thr_tls_get_addr_p == NULL 4048dffb485Schristos || (load_module == 0 && thread_db->td_thr_tlsbase_p == NULL)) 4058dffb485Schristos return -1; 4068dffb485Schristos 4078dffb485Schristos lwp = get_thread_lwp (thread); 4088dffb485Schristos if (!lwp->thread_known) 4098dffb485Schristos find_one_thread (thread->id); 4108dffb485Schristos if (!lwp->thread_known) 4118dffb485Schristos return TD_NOTHR; 4128dffb485Schristos 4134b169a6bSchristos scoped_restore_current_thread restore_thread; 4144b169a6bSchristos switch_to_thread (thread); 4158dffb485Schristos 4168dffb485Schristos if (load_module != 0) 4178dffb485Schristos { 4188dffb485Schristos /* Note the cast through uintptr_t: this interface only works if 4198dffb485Schristos a target address fits in a psaddr_t, which is a host pointer. 4208dffb485Schristos So a 32-bit debugger can not access 64-bit TLS through this. */ 4218dffb485Schristos err = thread_db->td_thr_tls_get_addr_p (&lwp->th, 4228dffb485Schristos (psaddr_t) (uintptr_t) load_module, 4238dffb485Schristos offset, &addr); 4248dffb485Schristos } 4258dffb485Schristos else 4268dffb485Schristos { 4278dffb485Schristos /* This code path handles the case of -static -pthread executables: 4288dffb485Schristos https://sourceware.org/ml/libc-help/2014-03/msg00024.html 4298dffb485Schristos For older GNU libc r_debug.r_map is NULL. For GNU libc after 4308dffb485Schristos PR libc/16831 due to GDB PR threads/16954 LOAD_MODULE is also NULL. 4318dffb485Schristos The constant number 1 depends on GNU __libc_setup_tls 4328dffb485Schristos initialization of l_tls_modid to 1. */ 4338dffb485Schristos err = thread_db->td_thr_tlsbase_p (&lwp->th, 1, &addr); 4348dffb485Schristos addr = (char *) addr + offset; 4358dffb485Schristos } 4368dffb485Schristos 4378dffb485Schristos if (err == TD_OK) 4388dffb485Schristos { 4398dffb485Schristos *address = (CORE_ADDR) (uintptr_t) addr; 4408dffb485Schristos return 0; 4418dffb485Schristos } 4428dffb485Schristos else 4438dffb485Schristos return err; 4448dffb485Schristos } 4458dffb485Schristos 4468dffb485Schristos /* See linux-low.h. */ 4478dffb485Schristos 4488dffb485Schristos bool 4498dffb485Schristos thread_db_thread_handle (ptid_t ptid, gdb_byte **handle, int *handle_len) 4508dffb485Schristos { 4518dffb485Schristos struct thread_db *thread_db; 4528dffb485Schristos struct lwp_info *lwp; 4538dffb485Schristos thread_info *thread = find_thread_ptid (ptid); 4548dffb485Schristos 4558dffb485Schristos if (thread == NULL) 4568dffb485Schristos return false; 4578dffb485Schristos 4588dffb485Schristos thread_db = get_thread_process (thread)->priv->thread_db; 4598dffb485Schristos 4608dffb485Schristos if (thread_db == NULL) 4618dffb485Schristos return false; 4628dffb485Schristos 4638dffb485Schristos lwp = get_thread_lwp (thread); 4648dffb485Schristos 4658dffb485Schristos if (!lwp->thread_known && !find_one_thread (thread->id)) 4668dffb485Schristos return false; 4678dffb485Schristos 4688dffb485Schristos gdb_assert (lwp->thread_known); 4698dffb485Schristos 4708dffb485Schristos *handle = (gdb_byte *) &lwp->thread_handle; 4718dffb485Schristos *handle_len = sizeof (lwp->thread_handle); 4728dffb485Schristos return true; 4738dffb485Schristos } 4748dffb485Schristos 4758dffb485Schristos #ifdef USE_LIBTHREAD_DB_DIRECTLY 4768dffb485Schristos 4778dffb485Schristos static int 4788dffb485Schristos thread_db_load_search (void) 4798dffb485Schristos { 4808dffb485Schristos td_err_e err; 4818dffb485Schristos struct thread_db *tdb; 4828dffb485Schristos struct process_info *proc = current_process (); 4838dffb485Schristos 4848dffb485Schristos gdb_assert (proc->priv->thread_db == NULL); 4858dffb485Schristos 4868dffb485Schristos tdb = XCNEW (struct thread_db); 4878dffb485Schristos proc->priv->thread_db = tdb; 4888dffb485Schristos 4898dffb485Schristos tdb->td_ta_new_p = &td_ta_new; 4908dffb485Schristos 4918dffb485Schristos /* Attempt to open a connection to the thread library. */ 4928dffb485Schristos err = tdb->td_ta_new_p (&tdb->proc_handle, &tdb->thread_agent); 4938dffb485Schristos if (err != TD_OK) 4948dffb485Schristos { 4954b169a6bSchristos threads_debug_printf ("td_ta_new(): %s", thread_db_err_str (err)); 4968dffb485Schristos free (tdb); 4978dffb485Schristos proc->priv->thread_db = NULL; 4988dffb485Schristos return 0; 4998dffb485Schristos } 5008dffb485Schristos 5018dffb485Schristos tdb->td_ta_map_lwp2thr_p = &td_ta_map_lwp2thr; 5028dffb485Schristos tdb->td_thr_get_info_p = &td_thr_get_info; 5038dffb485Schristos tdb->td_ta_thr_iter_p = &td_ta_thr_iter; 5048dffb485Schristos tdb->td_symbol_list_p = &td_symbol_list; 5058dffb485Schristos 5068dffb485Schristos /* These are not essential. */ 5078dffb485Schristos tdb->td_thr_tls_get_addr_p = &td_thr_tls_get_addr; 5088dffb485Schristos tdb->td_thr_tlsbase_p = &td_thr_tlsbase; 5098dffb485Schristos 5108dffb485Schristos return 1; 5118dffb485Schristos } 5128dffb485Schristos 5138dffb485Schristos #else 5148dffb485Schristos 5158dffb485Schristos static int 5168dffb485Schristos try_thread_db_load_1 (void *handle) 5178dffb485Schristos { 5188dffb485Schristos td_err_e err; 5198dffb485Schristos struct thread_db *tdb; 5208dffb485Schristos struct process_info *proc = current_process (); 5218dffb485Schristos 5228dffb485Schristos gdb_assert (proc->priv->thread_db == NULL); 5238dffb485Schristos 5248dffb485Schristos tdb = XCNEW (struct thread_db); 5258dffb485Schristos proc->priv->thread_db = tdb; 5268dffb485Schristos 5278dffb485Schristos tdb->handle = handle; 5288dffb485Schristos 5298dffb485Schristos /* Initialize pointers to the dynamic library functions we will use. 5308dffb485Schristos Essential functions first. */ 5318dffb485Schristos 5328dffb485Schristos #define CHK(required, a) \ 5338dffb485Schristos do \ 5348dffb485Schristos { \ 5358dffb485Schristos if ((a) == NULL) \ 5368dffb485Schristos { \ 5374b169a6bSchristos threads_debug_printf ("dlsym: %s", dlerror ()); \ 5388dffb485Schristos if (required) \ 5398dffb485Schristos { \ 5408dffb485Schristos free (tdb); \ 5418dffb485Schristos proc->priv->thread_db = NULL; \ 5428dffb485Schristos return 0; \ 5438dffb485Schristos } \ 5448dffb485Schristos } \ 5458dffb485Schristos } \ 5468dffb485Schristos while (0) 5478dffb485Schristos 5488dffb485Schristos #define TDB_DLSYM(tdb, func) \ 5498dffb485Schristos tdb->func ## _p = (func ## _ftype *) dlsym (tdb->handle, #func) 5508dffb485Schristos 5518dffb485Schristos CHK (1, TDB_DLSYM (tdb, td_ta_new)); 5528dffb485Schristos 5538dffb485Schristos /* Attempt to open a connection to the thread library. */ 5548dffb485Schristos err = tdb->td_ta_new_p (&tdb->proc_handle, &tdb->thread_agent); 5558dffb485Schristos if (err != TD_OK) 5568dffb485Schristos { 5574b169a6bSchristos threads_debug_printf ("td_ta_new(): %s", thread_db_err_str (err)); 5588dffb485Schristos free (tdb); 5598dffb485Schristos proc->priv->thread_db = NULL; 5608dffb485Schristos return 0; 5618dffb485Schristos } 5628dffb485Schristos 5638dffb485Schristos CHK (1, TDB_DLSYM (tdb, td_ta_map_lwp2thr)); 5648dffb485Schristos CHK (1, TDB_DLSYM (tdb, td_thr_get_info)); 5658dffb485Schristos CHK (1, TDB_DLSYM (tdb, td_ta_thr_iter)); 5668dffb485Schristos CHK (1, TDB_DLSYM (tdb, td_symbol_list)); 5678dffb485Schristos 5688dffb485Schristos /* These are not essential. */ 5698dffb485Schristos CHK (0, TDB_DLSYM (tdb, td_thr_tls_get_addr)); 5708dffb485Schristos CHK (0, TDB_DLSYM (tdb, td_thr_tlsbase)); 5718dffb485Schristos 5728dffb485Schristos #undef CHK 5738dffb485Schristos #undef TDB_DLSYM 5748dffb485Schristos 5758dffb485Schristos return 1; 5768dffb485Schristos } 5778dffb485Schristos 5788dffb485Schristos #ifdef HAVE_DLADDR 5798dffb485Schristos 5808dffb485Schristos /* Lookup a library in which given symbol resides. 5818dffb485Schristos Note: this is looking in the GDBSERVER process, not in the inferior. 5828dffb485Schristos Returns library name, or NULL. */ 5838dffb485Schristos 5848dffb485Schristos static const char * 5858dffb485Schristos dladdr_to_soname (const void *addr) 5868dffb485Schristos { 5878dffb485Schristos Dl_info info; 5888dffb485Schristos 5898dffb485Schristos if (dladdr (addr, &info) != 0) 5908dffb485Schristos return info.dli_fname; 5918dffb485Schristos return NULL; 5928dffb485Schristos } 5938dffb485Schristos 5948dffb485Schristos #endif 5958dffb485Schristos 5968dffb485Schristos static int 5978dffb485Schristos try_thread_db_load (const char *library) 5988dffb485Schristos { 5998dffb485Schristos void *handle; 6008dffb485Schristos 6014b169a6bSchristos threads_debug_printf ("Trying host libthread_db library: %s.", 6028dffb485Schristos library); 6038dffb485Schristos handle = dlopen (library, RTLD_NOW); 6048dffb485Schristos if (handle == NULL) 6058dffb485Schristos { 6064b169a6bSchristos threads_debug_printf ("dlopen failed: %s.", dlerror ()); 6078dffb485Schristos return 0; 6088dffb485Schristos } 6098dffb485Schristos 6108dffb485Schristos #ifdef HAVE_DLADDR 6118dffb485Schristos if (debug_threads && strchr (library, '/') == NULL) 6128dffb485Schristos { 6138dffb485Schristos void *td_init; 6148dffb485Schristos 6158dffb485Schristos td_init = dlsym (handle, "td_init"); 6168dffb485Schristos if (td_init != NULL) 6178dffb485Schristos { 6188dffb485Schristos const char *const libpath = dladdr_to_soname (td_init); 6198dffb485Schristos 6208dffb485Schristos if (libpath != NULL) 6214b169a6bSchristos threads_debug_printf ("Host %s resolved to: %s.", library, libpath); 6228dffb485Schristos } 6238dffb485Schristos } 6248dffb485Schristos #endif 6258dffb485Schristos 6268dffb485Schristos if (try_thread_db_load_1 (handle)) 6278dffb485Schristos return 1; 6288dffb485Schristos 6298dffb485Schristos /* This library "refused" to work on current inferior. */ 6308dffb485Schristos dlclose (handle); 6318dffb485Schristos return 0; 6328dffb485Schristos } 6338dffb485Schristos 6348dffb485Schristos /* Handle $sdir in libthread-db-search-path. 6358dffb485Schristos Look for libthread_db in the system dirs, or wherever a plain 6368dffb485Schristos dlopen(file_without_path) will look. 6378dffb485Schristos The result is true for success. */ 6388dffb485Schristos 6398dffb485Schristos static int 6408dffb485Schristos try_thread_db_load_from_sdir (void) 6418dffb485Schristos { 6428dffb485Schristos return try_thread_db_load (LIBTHREAD_DB_SO); 6438dffb485Schristos } 6448dffb485Schristos 6458dffb485Schristos /* Try to load libthread_db from directory DIR of length DIR_LEN. 6468dffb485Schristos The result is true for success. */ 6478dffb485Schristos 6488dffb485Schristos static int 6498dffb485Schristos try_thread_db_load_from_dir (const char *dir, size_t dir_len) 6508dffb485Schristos { 6518dffb485Schristos char path[PATH_MAX]; 6528dffb485Schristos 6538dffb485Schristos if (dir_len + 1 + strlen (LIBTHREAD_DB_SO) + 1 > sizeof (path)) 6548dffb485Schristos { 6558dffb485Schristos char *cp = (char *) xmalloc (dir_len + 1); 6568dffb485Schristos 6578dffb485Schristos memcpy (cp, dir, dir_len); 6588dffb485Schristos cp[dir_len] = '\0'; 6598dffb485Schristos warning (_("libthread-db-search-path component too long," 6608dffb485Schristos " ignored: %s."), cp); 6618dffb485Schristos free (cp); 6628dffb485Schristos return 0; 6638dffb485Schristos } 6648dffb485Schristos 6658dffb485Schristos memcpy (path, dir, dir_len); 6668dffb485Schristos path[dir_len] = '/'; 6678dffb485Schristos strcpy (path + dir_len + 1, LIBTHREAD_DB_SO); 6688dffb485Schristos return try_thread_db_load (path); 6698dffb485Schristos } 6708dffb485Schristos 6718dffb485Schristos /* Search libthread_db_search_path for libthread_db which "agrees" 6728dffb485Schristos to work on current inferior. 6738dffb485Schristos The result is true for success. */ 6748dffb485Schristos 6758dffb485Schristos static int 6768dffb485Schristos thread_db_load_search (void) 6778dffb485Schristos { 6788dffb485Schristos int rc = 0; 6798dffb485Schristos 6808dffb485Schristos if (libthread_db_search_path == NULL) 6818dffb485Schristos libthread_db_search_path = xstrdup (LIBTHREAD_DB_SEARCH_PATH); 6828dffb485Schristos 6838dffb485Schristos std::vector<gdb::unique_xmalloc_ptr<char>> dir_vec 6848dffb485Schristos = dirnames_to_char_ptr_vec (libthread_db_search_path); 6858dffb485Schristos 6868dffb485Schristos for (const gdb::unique_xmalloc_ptr<char> &this_dir_up : dir_vec) 6878dffb485Schristos { 6888dffb485Schristos char *this_dir = this_dir_up.get (); 6898dffb485Schristos const int pdir_len = sizeof ("$pdir") - 1; 6908dffb485Schristos size_t this_dir_len; 6918dffb485Schristos 6928dffb485Schristos this_dir_len = strlen (this_dir); 6938dffb485Schristos 6948dffb485Schristos if (strncmp (this_dir, "$pdir", pdir_len) == 0 6958dffb485Schristos && (this_dir[pdir_len] == '\0' 6968dffb485Schristos || this_dir[pdir_len] == '/')) 6978dffb485Schristos { 6988dffb485Schristos /* We don't maintain a list of loaded libraries so we don't know 6998dffb485Schristos where libpthread lives. We *could* fetch the info, but we don't 7008dffb485Schristos do that yet. Ignore it. */ 7018dffb485Schristos } 7028dffb485Schristos else if (strcmp (this_dir, "$sdir") == 0) 7038dffb485Schristos { 7048dffb485Schristos if (try_thread_db_load_from_sdir ()) 7058dffb485Schristos { 7068dffb485Schristos rc = 1; 7078dffb485Schristos break; 7088dffb485Schristos } 7098dffb485Schristos } 7108dffb485Schristos else 7118dffb485Schristos { 7128dffb485Schristos if (try_thread_db_load_from_dir (this_dir, this_dir_len)) 7138dffb485Schristos { 7148dffb485Schristos rc = 1; 7158dffb485Schristos break; 7168dffb485Schristos } 7178dffb485Schristos } 7188dffb485Schristos } 7198dffb485Schristos 7204b169a6bSchristos threads_debug_printf ("thread_db_load_search returning %d", rc); 7218dffb485Schristos return rc; 7228dffb485Schristos } 7238dffb485Schristos 7248dffb485Schristos #endif /* USE_LIBTHREAD_DB_DIRECTLY */ 7258dffb485Schristos 7268dffb485Schristos int 7278dffb485Schristos thread_db_init (void) 7288dffb485Schristos { 7298dffb485Schristos struct process_info *proc = current_process (); 7308dffb485Schristos 7318dffb485Schristos /* FIXME drow/2004-10-16: This is the "overall process ID", which 7328dffb485Schristos GNU/Linux calls tgid, "thread group ID". When we support 7338dffb485Schristos attaching to threads, the original thread may not be the correct 7348dffb485Schristos thread. We would have to get the process ID from /proc for NPTL. 7358dffb485Schristos 7368dffb485Schristos This isn't the only place in gdbserver that assumes that the first 7378dffb485Schristos process in the list is the thread group leader. */ 7388dffb485Schristos 7398dffb485Schristos if (thread_db_load_search ()) 7408dffb485Schristos { 7418dffb485Schristos /* It's best to avoid td_ta_thr_iter if possible. That walks 7428dffb485Schristos data structures in the inferior's address space that may be 7438dffb485Schristos corrupted, or, if the target is running, the list may change 7448dffb485Schristos while we walk it. In the latter case, it's possible that a 7458dffb485Schristos thread exits just at the exact time that causes GDBserver to 7468dffb485Schristos get stuck in an infinite loop. As the kernel supports clone 7478dffb485Schristos events and /proc/PID/task/ exists, then we already know about 7488dffb485Schristos all threads in the process. When we need info out of 7498dffb485Schristos thread_db on a given thread (e.g., for TLS), we'll use 7508dffb485Schristos find_one_thread then. That uses thread_db entry points that 7518dffb485Schristos do not walk libpthread's thread list, so should be safe, as 7528dffb485Schristos well as more efficient. */ 7538dffb485Schristos if (!linux_proc_task_list_dir_exists (pid_of (proc))) 7548dffb485Schristos thread_db_find_new_threads (); 7558dffb485Schristos thread_db_look_up_symbols (); 7568dffb485Schristos return 1; 7578dffb485Schristos } 7588dffb485Schristos 7598dffb485Schristos return 0; 7608dffb485Schristos } 7618dffb485Schristos 7628dffb485Schristos /* Disconnect from libthread_db and free resources. */ 7638dffb485Schristos 7648dffb485Schristos static void 7658dffb485Schristos disable_thread_event_reporting (struct process_info *proc) 7668dffb485Schristos { 7678dffb485Schristos struct thread_db *thread_db = proc->priv->thread_db; 7688dffb485Schristos if (thread_db) 7698dffb485Schristos { 7708dffb485Schristos td_err_e (*td_ta_clear_event_p) (const td_thragent_t *ta, 7718dffb485Schristos td_thr_events_t *event); 7728dffb485Schristos 7738dffb485Schristos #ifndef USE_LIBTHREAD_DB_DIRECTLY 7748dffb485Schristos td_ta_clear_event_p 7758dffb485Schristos = (td_ta_clear_event_ftype *) dlsym (thread_db->handle, 7768dffb485Schristos "td_ta_clear_event"); 7778dffb485Schristos #else 7788dffb485Schristos td_ta_clear_event_p = &td_ta_clear_event; 7798dffb485Schristos #endif 7808dffb485Schristos 7818dffb485Schristos if (td_ta_clear_event_p != NULL) 7828dffb485Schristos { 7834b169a6bSchristos scoped_restore_current_thread restore_thread; 7848dffb485Schristos td_thr_events_t events; 7858dffb485Schristos 7868dffb485Schristos switch_to_process (proc); 7878dffb485Schristos 7888dffb485Schristos /* Set the process wide mask saying we aren't interested 7898dffb485Schristos in any events anymore. */ 7908dffb485Schristos td_event_fillset (&events); 7918dffb485Schristos (*td_ta_clear_event_p) (thread_db->thread_agent, &events); 7928dffb485Schristos } 7938dffb485Schristos } 7948dffb485Schristos } 7958dffb485Schristos 7968dffb485Schristos void 7978dffb485Schristos thread_db_detach (struct process_info *proc) 7988dffb485Schristos { 7998dffb485Schristos struct thread_db *thread_db = proc->priv->thread_db; 8008dffb485Schristos 8018dffb485Schristos if (thread_db) 8028dffb485Schristos { 8038dffb485Schristos disable_thread_event_reporting (proc); 8048dffb485Schristos } 8058dffb485Schristos } 8068dffb485Schristos 8078dffb485Schristos /* Disconnect from libthread_db and free resources. */ 8088dffb485Schristos 8098dffb485Schristos void 8108dffb485Schristos thread_db_mourn (struct process_info *proc) 8118dffb485Schristos { 8128dffb485Schristos struct thread_db *thread_db = proc->priv->thread_db; 8138dffb485Schristos if (thread_db) 8148dffb485Schristos { 8158dffb485Schristos td_ta_delete_ftype *td_ta_delete_p; 8168dffb485Schristos 8178dffb485Schristos #ifndef USE_LIBTHREAD_DB_DIRECTLY 8188dffb485Schristos td_ta_delete_p = (td_ta_delete_ftype *) dlsym (thread_db->handle, "td_ta_delete"); 8198dffb485Schristos #else 8208dffb485Schristos td_ta_delete_p = &td_ta_delete; 8218dffb485Schristos #endif 8228dffb485Schristos 8238dffb485Schristos if (td_ta_delete_p != NULL) 8248dffb485Schristos (*td_ta_delete_p) (thread_db->thread_agent); 8258dffb485Schristos 8268dffb485Schristos #ifndef USE_LIBTHREAD_DB_DIRECTLY 8278dffb485Schristos dlclose (thread_db->handle); 8288dffb485Schristos #endif /* USE_LIBTHREAD_DB_DIRECTLY */ 8298dffb485Schristos 8308dffb485Schristos free (thread_db); 8318dffb485Schristos proc->priv->thread_db = NULL; 8328dffb485Schristos } 8338dffb485Schristos } 8348dffb485Schristos 8358dffb485Schristos /* Handle "set libthread-db-search-path" monitor command and return 1. 8368dffb485Schristos For any other command, return 0. */ 8378dffb485Schristos 8388dffb485Schristos int 8398dffb485Schristos thread_db_handle_monitor_command (char *mon) 8408dffb485Schristos { 8418dffb485Schristos const char *cmd = "set libthread-db-search-path"; 8428dffb485Schristos size_t cmd_len = strlen (cmd); 8438dffb485Schristos 8448dffb485Schristos if (strncmp (mon, cmd, cmd_len) == 0 8458dffb485Schristos && (mon[cmd_len] == '\0' 8468dffb485Schristos || mon[cmd_len] == ' ')) 8478dffb485Schristos { 8488dffb485Schristos const char *cp = mon + cmd_len; 8498dffb485Schristos 8508dffb485Schristos if (libthread_db_search_path != NULL) 8518dffb485Schristos free (libthread_db_search_path); 8528dffb485Schristos 8538dffb485Schristos /* Skip leading space (if any). */ 8548dffb485Schristos while (isspace (*cp)) 8558dffb485Schristos ++cp; 8568dffb485Schristos 8578dffb485Schristos if (*cp == '\0') 8588dffb485Schristos cp = LIBTHREAD_DB_SEARCH_PATH; 8598dffb485Schristos libthread_db_search_path = xstrdup (cp); 8608dffb485Schristos 8618dffb485Schristos monitor_output ("libthread-db-search-path set to `"); 8628dffb485Schristos monitor_output (libthread_db_search_path); 8638dffb485Schristos monitor_output ("'\n"); 8648dffb485Schristos return 1; 8658dffb485Schristos } 8668dffb485Schristos 8678dffb485Schristos /* Tell server.c to perform default processing. */ 8688dffb485Schristos return 0; 8698dffb485Schristos } 8708dffb485Schristos 8718dffb485Schristos /* See linux-low.h. */ 8728dffb485Schristos 8738dffb485Schristos void 8748dffb485Schristos thread_db_notice_clone (struct thread_info *parent_thr, ptid_t child_ptid) 8758dffb485Schristos { 8768dffb485Schristos process_info *parent_proc = get_thread_process (parent_thr); 8778dffb485Schristos struct thread_db *thread_db = parent_proc->priv->thread_db; 8788dffb485Schristos 8798dffb485Schristos /* If the thread layer isn't initialized, return. It may just 8808dffb485Schristos be that the program uses clone, but does not use libthread_db. */ 8818dffb485Schristos if (thread_db == NULL || !thread_db->all_symbols_looked_up) 8828dffb485Schristos return; 8838dffb485Schristos 8848dffb485Schristos /* find_one_thread calls into libthread_db which accesses memory via 8858dffb485Schristos the current thread. Temporarily switch to a thread we know is 8868dffb485Schristos stopped. */ 8874b169a6bSchristos scoped_restore_current_thread restore_thread; 8884b169a6bSchristos switch_to_thread (parent_thr); 8898dffb485Schristos 8908dffb485Schristos if (!find_one_thread (child_ptid)) 8918dffb485Schristos warning ("Cannot find thread after clone."); 8928dffb485Schristos } 893