thread-db.cc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. /* Thread management interface, for the remote server for GDB.
  2. Copyright (C) 2002-2022 Free Software Foundation, Inc.
  3. Contributed by MontaVista Software.
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include "server.h"
  16. #include "linux-low.h"
  17. #include "debug.h"
  18. #include "gdb_proc_service.h"
  19. #include "nat/gdb_thread_db.h"
  20. #include "gdbsupport/gdb_vecs.h"
  21. #include "nat/linux-procfs.h"
  22. #include "gdbsupport/scoped_restore.h"
  23. #ifndef USE_LIBTHREAD_DB_DIRECTLY
  24. #include <dlfcn.h>
  25. #endif
  26. #include <limits.h>
  27. #include <ctype.h>
  28. struct thread_db
  29. {
  30. /* Structure that identifies the child process for the
  31. <proc_service.h> interface. */
  32. struct ps_prochandle proc_handle;
  33. /* Connection to the libthread_db library. */
  34. td_thragent_t *thread_agent;
  35. /* If this flag has been set, we've already asked GDB for all
  36. symbols we might need; assume symbol cache misses are
  37. failures. */
  38. int all_symbols_looked_up;
  39. #ifndef USE_LIBTHREAD_DB_DIRECTLY
  40. /* Handle of the libthread_db from dlopen. */
  41. void *handle;
  42. #endif
  43. /* Addresses of libthread_db functions. */
  44. td_ta_new_ftype *td_ta_new_p;
  45. td_ta_map_lwp2thr_ftype *td_ta_map_lwp2thr_p;
  46. td_thr_get_info_ftype *td_thr_get_info_p;
  47. td_ta_thr_iter_ftype *td_ta_thr_iter_p;
  48. td_thr_tls_get_addr_ftype *td_thr_tls_get_addr_p;
  49. td_thr_tlsbase_ftype *td_thr_tlsbase_p;
  50. td_symbol_list_ftype *td_symbol_list_p;
  51. };
  52. static char *libthread_db_search_path;
  53. static int find_one_thread (ptid_t);
  54. static int find_new_threads_callback (const td_thrhandle_t *th_p, void *data);
  55. static const char *
  56. thread_db_err_str (td_err_e err)
  57. {
  58. static char buf[64];
  59. switch (err)
  60. {
  61. case TD_OK:
  62. return "generic 'call succeeded'";
  63. case TD_ERR:
  64. return "generic error";
  65. case TD_NOTHR:
  66. return "no thread to satisfy query";
  67. case TD_NOSV:
  68. return "no sync handle to satisfy query";
  69. case TD_NOLWP:
  70. return "no LWP to satisfy query";
  71. case TD_BADPH:
  72. return "invalid process handle";
  73. case TD_BADTH:
  74. return "invalid thread handle";
  75. case TD_BADSH:
  76. return "invalid synchronization handle";
  77. case TD_BADTA:
  78. return "invalid thread agent";
  79. case TD_BADKEY:
  80. return "invalid key";
  81. case TD_NOMSG:
  82. return "no event message for getmsg";
  83. case TD_NOFPREGS:
  84. return "FPU register set not available";
  85. case TD_NOLIBTHREAD:
  86. return "application not linked with libthread";
  87. case TD_NOEVENT:
  88. return "requested event is not supported";
  89. case TD_NOCAPAB:
  90. return "capability not available";
  91. case TD_DBERR:
  92. return "debugger service failed";
  93. case TD_NOAPLIC:
  94. return "operation not applicable to";
  95. case TD_NOTSD:
  96. return "no thread-specific data for this thread";
  97. case TD_MALLOC:
  98. return "malloc failed";
  99. case TD_PARTIALREG:
  100. return "only part of register set was written/read";
  101. case TD_NOXREGS:
  102. return "X register set not available for this thread";
  103. #ifdef HAVE_TD_VERSION
  104. case TD_VERSION:
  105. return "version mismatch between libthread_db and libpthread";
  106. #endif
  107. default:
  108. xsnprintf (buf, sizeof (buf), "unknown thread_db error '%d'", err);
  109. return buf;
  110. }
  111. }
  112. #if 0
  113. static char *
  114. thread_db_state_str (td_thr_state_e state)
  115. {
  116. static char buf[64];
  117. switch (state)
  118. {
  119. case TD_THR_STOPPED:
  120. return "stopped by debugger";
  121. case TD_THR_RUN:
  122. return "runnable";
  123. case TD_THR_ACTIVE:
  124. return "active";
  125. case TD_THR_ZOMBIE:
  126. return "zombie";
  127. case TD_THR_SLEEP:
  128. return "sleeping";
  129. case TD_THR_STOPPED_ASLEEP:
  130. return "stopped by debugger AND blocked";
  131. default:
  132. xsnprintf (buf, sizeof (buf), "unknown thread_db state %d", state);
  133. return buf;
  134. }
  135. }
  136. #endif
  137. /* Get thread info about PTID, accessing memory via the current
  138. thread. */
  139. static int
  140. find_one_thread (ptid_t ptid)
  141. {
  142. td_thrhandle_t th;
  143. td_thrinfo_t ti;
  144. td_err_e err;
  145. struct lwp_info *lwp;
  146. struct thread_db *thread_db = current_process ()->priv->thread_db;
  147. int lwpid = ptid.lwp ();
  148. thread_info *thread = find_thread_ptid (ptid);
  149. lwp = get_thread_lwp (thread);
  150. if (lwp->thread_known)
  151. return 1;
  152. /* Get information about this thread. */
  153. err = thread_db->td_ta_map_lwp2thr_p (thread_db->thread_agent, lwpid, &th);
  154. if (err != TD_OK)
  155. error ("Cannot get thread handle for LWP %d: %s",
  156. lwpid, thread_db_err_str (err));
  157. err = thread_db->td_thr_get_info_p (&th, &ti);
  158. if (err != TD_OK)
  159. error ("Cannot get thread info for LWP %d: %s",
  160. lwpid, thread_db_err_str (err));
  161. threads_debug_printf ("Found thread %ld (LWP %d)",
  162. (unsigned long) ti.ti_tid, ti.ti_lid);
  163. if (lwpid != ti.ti_lid)
  164. {
  165. warning ("PID mismatch! Expected %ld, got %ld",
  166. (long) lwpid, (long) ti.ti_lid);
  167. return 0;
  168. }
  169. /* If the new thread ID is zero, a final thread ID will be available
  170. later. Do not enable thread debugging yet. */
  171. if (ti.ti_tid == 0)
  172. return 0;
  173. lwp->thread_known = 1;
  174. lwp->th = th;
  175. lwp->thread_handle = ti.ti_tid;
  176. return 1;
  177. }
  178. /* Attach a thread. Return true on success. */
  179. static int
  180. attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p)
  181. {
  182. struct process_info *proc = current_process ();
  183. int pid = pid_of (proc);
  184. ptid_t ptid = ptid_t (pid, ti_p->ti_lid);
  185. struct lwp_info *lwp;
  186. int err;
  187. threads_debug_printf ("Attaching to thread %ld (LWP %d)",
  188. (unsigned long) ti_p->ti_tid, ti_p->ti_lid);
  189. err = the_linux_target->attach_lwp (ptid);
  190. if (err != 0)
  191. {
  192. std::string reason = linux_ptrace_attach_fail_reason_string (ptid, err);
  193. warning ("Could not attach to thread %ld (LWP %d): %s",
  194. (unsigned long) ti_p->ti_tid, ti_p->ti_lid, reason.c_str ());
  195. return 0;
  196. }
  197. lwp = find_lwp_pid (ptid);
  198. gdb_assert (lwp != NULL);
  199. lwp->thread_known = 1;
  200. lwp->th = *th_p;
  201. lwp->thread_handle = ti_p->ti_tid;
  202. return 1;
  203. }
  204. /* Attach thread if we haven't seen it yet.
  205. Increment *COUNTER if we have attached a new thread.
  206. Return false on failure. */
  207. static int
  208. maybe_attach_thread (const td_thrhandle_t *th_p, td_thrinfo_t *ti_p,
  209. int *counter)
  210. {
  211. struct lwp_info *lwp;
  212. lwp = find_lwp_pid (ptid_t (ti_p->ti_lid));
  213. if (lwp != NULL)
  214. return 1;
  215. if (!attach_thread (th_p, ti_p))
  216. return 0;
  217. if (counter != NULL)
  218. *counter += 1;
  219. return 1;
  220. }
  221. static int
  222. find_new_threads_callback (const td_thrhandle_t *th_p, void *data)
  223. {
  224. td_thrinfo_t ti;
  225. td_err_e err;
  226. struct thread_db *thread_db = current_process ()->priv->thread_db;
  227. err = thread_db->td_thr_get_info_p (th_p, &ti);
  228. if (err != TD_OK)
  229. error ("Cannot get thread info: %s", thread_db_err_str (err));
  230. if (ti.ti_lid == -1)
  231. {
  232. /* A thread with kernel thread ID -1 is either a thread that
  233. exited and was joined, or a thread that is being created but
  234. hasn't started yet, and that is reusing the tcb/stack of a
  235. thread that previously exited and was joined. (glibc marks
  236. terminated and joined threads with kernel thread ID -1. See
  237. glibc PR17707. */
  238. threads_debug_printf ("thread_db: skipping exited and "
  239. "joined thread (0x%lx)",
  240. (unsigned long) ti.ti_tid);
  241. return 0;
  242. }
  243. /* Check for zombies. */
  244. if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
  245. return 0;
  246. if (!maybe_attach_thread (th_p, &ti, (int *) data))
  247. {
  248. /* Terminate iteration early: we might be looking at stale data in
  249. the inferior. The thread_db_find_new_threads will retry. */
  250. return 1;
  251. }
  252. return 0;
  253. }
  254. static void
  255. thread_db_find_new_threads (void)
  256. {
  257. td_err_e err;
  258. ptid_t ptid = current_ptid;
  259. struct thread_db *thread_db = current_process ()->priv->thread_db;
  260. int loop, iteration;
  261. /* This function is only called when we first initialize thread_db.
  262. First locate the initial thread. If it is not ready for
  263. debugging yet, then stop. */
  264. if (find_one_thread (ptid) == 0)
  265. return;
  266. /* Require 4 successive iterations which do not find any new threads.
  267. The 4 is a heuristic: there is an inherent race here, and I have
  268. seen that 2 iterations in a row are not always sufficient to
  269. "capture" all threads. */
  270. for (loop = 0, iteration = 0; loop < 4; ++loop, ++iteration)
  271. {
  272. int new_thread_count = 0;
  273. /* Iterate over all user-space threads to discover new threads. */
  274. err = thread_db->td_ta_thr_iter_p (thread_db->thread_agent,
  275. find_new_threads_callback,
  276. &new_thread_count,
  277. TD_THR_ANY_STATE,
  278. TD_THR_LOWEST_PRIORITY,
  279. TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
  280. threads_debug_printf ("Found %d threads in iteration %d.",
  281. new_thread_count, iteration);
  282. if (new_thread_count != 0)
  283. {
  284. /* Found new threads. Restart iteration from beginning. */
  285. loop = -1;
  286. }
  287. }
  288. if (err != TD_OK)
  289. error ("Cannot find new threads: %s", thread_db_err_str (err));
  290. }
  291. /* Cache all future symbols that thread_db might request. We can not
  292. request symbols at arbitrary states in the remote protocol, only
  293. when the client tells us that new symbols are available. So when
  294. we load the thread library, make sure to check the entire list. */
  295. static void
  296. thread_db_look_up_symbols (void)
  297. {
  298. struct thread_db *thread_db = current_process ()->priv->thread_db;
  299. const char **sym_list;
  300. CORE_ADDR unused;
  301. for (sym_list = thread_db->td_symbol_list_p (); *sym_list; sym_list++)
  302. look_up_one_symbol (*sym_list, &unused, 1);
  303. /* We're not interested in any other libraries loaded after this
  304. point, only in symbols in libpthread.so. */
  305. thread_db->all_symbols_looked_up = 1;
  306. }
  307. int
  308. thread_db_look_up_one_symbol (const char *name, CORE_ADDR *addrp)
  309. {
  310. struct thread_db *thread_db = current_process ()->priv->thread_db;
  311. int may_ask_gdb = !thread_db->all_symbols_looked_up;
  312. /* If we've passed the call to thread_db_look_up_symbols, then
  313. anything not in the cache must not exist; we're not interested
  314. in any libraries loaded after that point, only in symbols in
  315. libpthread.so. It might not be an appropriate time to look
  316. up a symbol, e.g. while we're trying to fetch registers. */
  317. return look_up_one_symbol (name, addrp, may_ask_gdb);
  318. }
  319. int
  320. thread_db_get_tls_address (struct thread_info *thread, CORE_ADDR offset,
  321. CORE_ADDR load_module, CORE_ADDR *address)
  322. {
  323. psaddr_t addr;
  324. td_err_e err;
  325. struct lwp_info *lwp;
  326. struct process_info *proc;
  327. struct thread_db *thread_db;
  328. proc = get_thread_process (thread);
  329. thread_db = proc->priv->thread_db;
  330. /* If the thread layer is not (yet) initialized, fail. */
  331. if (thread_db == NULL || !thread_db->all_symbols_looked_up)
  332. return TD_ERR;
  333. /* If td_thr_tls_get_addr is missing rather do not expect td_thr_tlsbase
  334. could work. */
  335. if (thread_db->td_thr_tls_get_addr_p == NULL
  336. || (load_module == 0 && thread_db->td_thr_tlsbase_p == NULL))
  337. return -1;
  338. lwp = get_thread_lwp (thread);
  339. if (!lwp->thread_known)
  340. find_one_thread (thread->id);
  341. if (!lwp->thread_known)
  342. return TD_NOTHR;
  343. scoped_restore_current_thread restore_thread;
  344. switch_to_thread (thread);
  345. if (load_module != 0)
  346. {
  347. /* Note the cast through uintptr_t: this interface only works if
  348. a target address fits in a psaddr_t, which is a host pointer.
  349. So a 32-bit debugger can not access 64-bit TLS through this. */
  350. err = thread_db->td_thr_tls_get_addr_p (&lwp->th,
  351. (psaddr_t) (uintptr_t) load_module,
  352. offset, &addr);
  353. }
  354. else
  355. {
  356. /* This code path handles the case of -static -pthread executables:
  357. https://sourceware.org/ml/libc-help/2014-03/msg00024.html
  358. For older GNU libc r_debug.r_map is NULL. For GNU libc after
  359. PR libc/16831 due to GDB PR threads/16954 LOAD_MODULE is also NULL.
  360. The constant number 1 depends on GNU __libc_setup_tls
  361. initialization of l_tls_modid to 1. */
  362. err = thread_db->td_thr_tlsbase_p (&lwp->th, 1, &addr);
  363. addr = (char *) addr + offset;
  364. }
  365. if (err == TD_OK)
  366. {
  367. *address = (CORE_ADDR) (uintptr_t) addr;
  368. return 0;
  369. }
  370. else
  371. return err;
  372. }
  373. /* See linux-low.h. */
  374. bool
  375. thread_db_thread_handle (ptid_t ptid, gdb_byte **handle, int *handle_len)
  376. {
  377. struct thread_db *thread_db;
  378. struct lwp_info *lwp;
  379. thread_info *thread = find_thread_ptid (ptid);
  380. if (thread == NULL)
  381. return false;
  382. thread_db = get_thread_process (thread)->priv->thread_db;
  383. if (thread_db == NULL)
  384. return false;
  385. lwp = get_thread_lwp (thread);
  386. if (!lwp->thread_known && !find_one_thread (thread->id))
  387. return false;
  388. gdb_assert (lwp->thread_known);
  389. *handle = (gdb_byte *) &lwp->thread_handle;
  390. *handle_len = sizeof (lwp->thread_handle);
  391. return true;
  392. }
  393. #ifdef USE_LIBTHREAD_DB_DIRECTLY
  394. static int
  395. thread_db_load_search (void)
  396. {
  397. td_err_e err;
  398. struct thread_db *tdb;
  399. struct process_info *proc = current_process ();
  400. gdb_assert (proc->priv->thread_db == NULL);
  401. tdb = XCNEW (struct thread_db);
  402. proc->priv->thread_db = tdb;
  403. tdb->td_ta_new_p = &td_ta_new;
  404. /* Attempt to open a connection to the thread library. */
  405. err = tdb->td_ta_new_p (&tdb->proc_handle, &tdb->thread_agent);
  406. if (err != TD_OK)
  407. {
  408. threads_debug_printf ("td_ta_new(): %s", thread_db_err_str (err));
  409. free (tdb);
  410. proc->priv->thread_db = NULL;
  411. return 0;
  412. }
  413. tdb->td_ta_map_lwp2thr_p = &td_ta_map_lwp2thr;
  414. tdb->td_thr_get_info_p = &td_thr_get_info;
  415. tdb->td_ta_thr_iter_p = &td_ta_thr_iter;
  416. tdb->td_symbol_list_p = &td_symbol_list;
  417. /* These are not essential. */
  418. tdb->td_thr_tls_get_addr_p = &td_thr_tls_get_addr;
  419. tdb->td_thr_tlsbase_p = &td_thr_tlsbase;
  420. return 1;
  421. }
  422. #else
  423. static int
  424. try_thread_db_load_1 (void *handle)
  425. {
  426. td_err_e err;
  427. struct thread_db *tdb;
  428. struct process_info *proc = current_process ();
  429. gdb_assert (proc->priv->thread_db == NULL);
  430. tdb = XCNEW (struct thread_db);
  431. proc->priv->thread_db = tdb;
  432. tdb->handle = handle;
  433. /* Initialize pointers to the dynamic library functions we will use.
  434. Essential functions first. */
  435. #define CHK(required, a) \
  436. do \
  437. { \
  438. if ((a) == NULL) \
  439. { \
  440. threads_debug_printf ("dlsym: %s", dlerror ()); \
  441. if (required) \
  442. { \
  443. free (tdb); \
  444. proc->priv->thread_db = NULL; \
  445. return 0; \
  446. } \
  447. } \
  448. } \
  449. while (0)
  450. #define TDB_DLSYM(tdb, func) \
  451. tdb->func ## _p = (func ## _ftype *) dlsym (tdb->handle, #func)
  452. CHK (1, TDB_DLSYM (tdb, td_ta_new));
  453. /* Attempt to open a connection to the thread library. */
  454. err = tdb->td_ta_new_p (&tdb->proc_handle, &tdb->thread_agent);
  455. if (err != TD_OK)
  456. {
  457. threads_debug_printf ("td_ta_new(): %s", thread_db_err_str (err));
  458. free (tdb);
  459. proc->priv->thread_db = NULL;
  460. return 0;
  461. }
  462. CHK (1, TDB_DLSYM (tdb, td_ta_map_lwp2thr));
  463. CHK (1, TDB_DLSYM (tdb, td_thr_get_info));
  464. CHK (1, TDB_DLSYM (tdb, td_ta_thr_iter));
  465. CHK (1, TDB_DLSYM (tdb, td_symbol_list));
  466. /* These are not essential. */
  467. CHK (0, TDB_DLSYM (tdb, td_thr_tls_get_addr));
  468. CHK (0, TDB_DLSYM (tdb, td_thr_tlsbase));
  469. #undef CHK
  470. #undef TDB_DLSYM
  471. return 1;
  472. }
  473. #ifdef HAVE_DLADDR
  474. /* Lookup a library in which given symbol resides.
  475. Note: this is looking in the GDBSERVER process, not in the inferior.
  476. Returns library name, or NULL. */
  477. static const char *
  478. dladdr_to_soname (const void *addr)
  479. {
  480. Dl_info info;
  481. if (dladdr (addr, &info) != 0)
  482. return info.dli_fname;
  483. return NULL;
  484. }
  485. #endif
  486. static int
  487. try_thread_db_load (const char *library)
  488. {
  489. void *handle;
  490. threads_debug_printf ("Trying host libthread_db library: %s.",
  491. library);
  492. handle = dlopen (library, RTLD_NOW);
  493. if (handle == NULL)
  494. {
  495. threads_debug_printf ("dlopen failed: %s.", dlerror ());
  496. return 0;
  497. }
  498. #ifdef HAVE_DLADDR
  499. if (debug_threads && strchr (library, '/') == NULL)
  500. {
  501. void *td_init;
  502. td_init = dlsym (handle, "td_init");
  503. if (td_init != NULL)
  504. {
  505. const char *const libpath = dladdr_to_soname (td_init);
  506. if (libpath != NULL)
  507. threads_debug_printf ("Host %s resolved to: %s.", library, libpath);
  508. }
  509. }
  510. #endif
  511. if (try_thread_db_load_1 (handle))
  512. return 1;
  513. /* This library "refused" to work on current inferior. */
  514. dlclose (handle);
  515. return 0;
  516. }
  517. /* Handle $sdir in libthread-db-search-path.
  518. Look for libthread_db in the system dirs, or wherever a plain
  519. dlopen(file_without_path) will look.
  520. The result is true for success. */
  521. static int
  522. try_thread_db_load_from_sdir (void)
  523. {
  524. return try_thread_db_load (LIBTHREAD_DB_SO);
  525. }
  526. /* Try to load libthread_db from directory DIR of length DIR_LEN.
  527. The result is true for success. */
  528. static int
  529. try_thread_db_load_from_dir (const char *dir, size_t dir_len)
  530. {
  531. char path[PATH_MAX];
  532. if (dir_len + 1 + strlen (LIBTHREAD_DB_SO) + 1 > sizeof (path))
  533. {
  534. char *cp = (char *) xmalloc (dir_len + 1);
  535. memcpy (cp, dir, dir_len);
  536. cp[dir_len] = '\0';
  537. warning (_("libthread-db-search-path component too long,"
  538. " ignored: %s."), cp);
  539. free (cp);
  540. return 0;
  541. }
  542. memcpy (path, dir, dir_len);
  543. path[dir_len] = '/';
  544. strcpy (path + dir_len + 1, LIBTHREAD_DB_SO);
  545. return try_thread_db_load (path);
  546. }
  547. /* Search libthread_db_search_path for libthread_db which "agrees"
  548. to work on current inferior.
  549. The result is true for success. */
  550. static int
  551. thread_db_load_search (void)
  552. {
  553. int rc = 0;
  554. if (libthread_db_search_path == NULL)
  555. libthread_db_search_path = xstrdup (LIBTHREAD_DB_SEARCH_PATH);
  556. std::vector<gdb::unique_xmalloc_ptr<char>> dir_vec
  557. = dirnames_to_char_ptr_vec (libthread_db_search_path);
  558. for (const gdb::unique_xmalloc_ptr<char> &this_dir_up : dir_vec)
  559. {
  560. char *this_dir = this_dir_up.get ();
  561. const int pdir_len = sizeof ("$pdir") - 1;
  562. size_t this_dir_len;
  563. this_dir_len = strlen (this_dir);
  564. if (strncmp (this_dir, "$pdir", pdir_len) == 0
  565. && (this_dir[pdir_len] == '\0'
  566. || this_dir[pdir_len] == '/'))
  567. {
  568. /* We don't maintain a list of loaded libraries so we don't know
  569. where libpthread lives. We *could* fetch the info, but we don't
  570. do that yet. Ignore it. */
  571. }
  572. else if (strcmp (this_dir, "$sdir") == 0)
  573. {
  574. if (try_thread_db_load_from_sdir ())
  575. {
  576. rc = 1;
  577. break;
  578. }
  579. }
  580. else
  581. {
  582. if (try_thread_db_load_from_dir (this_dir, this_dir_len))
  583. {
  584. rc = 1;
  585. break;
  586. }
  587. }
  588. }
  589. threads_debug_printf ("thread_db_load_search returning %d", rc);
  590. return rc;
  591. }
  592. #endif /* USE_LIBTHREAD_DB_DIRECTLY */
  593. int
  594. thread_db_init (void)
  595. {
  596. struct process_info *proc = current_process ();
  597. /* FIXME drow/2004-10-16: This is the "overall process ID", which
  598. GNU/Linux calls tgid, "thread group ID". When we support
  599. attaching to threads, the original thread may not be the correct
  600. thread. We would have to get the process ID from /proc for NPTL.
  601. This isn't the only place in gdbserver that assumes that the first
  602. process in the list is the thread group leader. */
  603. if (thread_db_load_search ())
  604. {
  605. /* It's best to avoid td_ta_thr_iter if possible. That walks
  606. data structures in the inferior's address space that may be
  607. corrupted, or, if the target is running, the list may change
  608. while we walk it. In the latter case, it's possible that a
  609. thread exits just at the exact time that causes GDBserver to
  610. get stuck in an infinite loop. As the kernel supports clone
  611. events and /proc/PID/task/ exists, then we already know about
  612. all threads in the process. When we need info out of
  613. thread_db on a given thread (e.g., for TLS), we'll use
  614. find_one_thread then. That uses thread_db entry points that
  615. do not walk libpthread's thread list, so should be safe, as
  616. well as more efficient. */
  617. if (!linux_proc_task_list_dir_exists (pid_of (proc)))
  618. thread_db_find_new_threads ();
  619. thread_db_look_up_symbols ();
  620. return 1;
  621. }
  622. return 0;
  623. }
  624. /* Disconnect from libthread_db and free resources. */
  625. static void
  626. disable_thread_event_reporting (struct process_info *proc)
  627. {
  628. struct thread_db *thread_db = proc->priv->thread_db;
  629. if (thread_db)
  630. {
  631. td_err_e (*td_ta_clear_event_p) (const td_thragent_t *ta,
  632. td_thr_events_t *event);
  633. #ifndef USE_LIBTHREAD_DB_DIRECTLY
  634. td_ta_clear_event_p
  635. = (td_ta_clear_event_ftype *) dlsym (thread_db->handle,
  636. "td_ta_clear_event");
  637. #else
  638. td_ta_clear_event_p = &td_ta_clear_event;
  639. #endif
  640. if (td_ta_clear_event_p != NULL)
  641. {
  642. scoped_restore_current_thread restore_thread;
  643. td_thr_events_t events;
  644. switch_to_process (proc);
  645. /* Set the process wide mask saying we aren't interested
  646. in any events anymore. */
  647. td_event_fillset (&events);
  648. (*td_ta_clear_event_p) (thread_db->thread_agent, &events);
  649. }
  650. }
  651. }
  652. void
  653. thread_db_detach (struct process_info *proc)
  654. {
  655. struct thread_db *thread_db = proc->priv->thread_db;
  656. if (thread_db)
  657. {
  658. disable_thread_event_reporting (proc);
  659. }
  660. }
  661. /* Disconnect from libthread_db and free resources. */
  662. void
  663. thread_db_mourn (struct process_info *proc)
  664. {
  665. struct thread_db *thread_db = proc->priv->thread_db;
  666. if (thread_db)
  667. {
  668. td_ta_delete_ftype *td_ta_delete_p;
  669. #ifndef USE_LIBTHREAD_DB_DIRECTLY
  670. td_ta_delete_p = (td_ta_delete_ftype *) dlsym (thread_db->handle, "td_ta_delete");
  671. #else
  672. td_ta_delete_p = &td_ta_delete;
  673. #endif
  674. if (td_ta_delete_p != NULL)
  675. (*td_ta_delete_p) (thread_db->thread_agent);
  676. #ifndef USE_LIBTHREAD_DB_DIRECTLY
  677. dlclose (thread_db->handle);
  678. #endif /* USE_LIBTHREAD_DB_DIRECTLY */
  679. free (thread_db);
  680. proc->priv->thread_db = NULL;
  681. }
  682. }
  683. /* Handle "set libthread-db-search-path" monitor command and return 1.
  684. For any other command, return 0. */
  685. int
  686. thread_db_handle_monitor_command (char *mon)
  687. {
  688. const char *cmd = "set libthread-db-search-path";
  689. size_t cmd_len = strlen (cmd);
  690. if (strncmp (mon, cmd, cmd_len) == 0
  691. && (mon[cmd_len] == '\0'
  692. || mon[cmd_len] == ' '))
  693. {
  694. const char *cp = mon + cmd_len;
  695. if (libthread_db_search_path != NULL)
  696. free (libthread_db_search_path);
  697. /* Skip leading space (if any). */
  698. while (isspace (*cp))
  699. ++cp;
  700. if (*cp == '\0')
  701. cp = LIBTHREAD_DB_SEARCH_PATH;
  702. libthread_db_search_path = xstrdup (cp);
  703. monitor_output ("libthread-db-search-path set to `");
  704. monitor_output (libthread_db_search_path);
  705. monitor_output ("'\n");
  706. return 1;
  707. }
  708. /* Tell server.c to perform default processing. */
  709. return 0;
  710. }
  711. /* See linux-low.h. */
  712. void
  713. thread_db_notice_clone (struct thread_info *parent_thr, ptid_t child_ptid)
  714. {
  715. process_info *parent_proc = get_thread_process (parent_thr);
  716. struct thread_db *thread_db = parent_proc->priv->thread_db;
  717. /* If the thread layer isn't initialized, return. It may just
  718. be that the program uses clone, but does not use libthread_db. */
  719. if (thread_db == NULL || !thread_db->all_symbols_looked_up)
  720. return;
  721. /* find_one_thread calls into libthread_db which accesses memory via
  722. the current thread. Temporarily switch to a thread we know is
  723. stopped. */
  724. scoped_restore_current_thread restore_thread;
  725. switch_to_thread (parent_thr);
  726. if (!find_one_thread (child_ptid))
  727. warning ("Cannot find thread after clone.");
  728. }