bsd-uthread.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. /* BSD user-level threads support.
  2. Copyright (C) 2005-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "gdbcore.h"
  16. #include "gdbthread.h"
  17. #include "inferior.h"
  18. #include "objfiles.h"
  19. #include "observable.h"
  20. #include "regcache.h"
  21. #include "solib.h"
  22. #include "solist.h"
  23. #include "symfile.h"
  24. #include "target.h"
  25. #include "gdbsupport/gdb_obstack.h"
  26. #include "bsd-uthread.h"
  27. static const target_info bsd_uthread_target_info = {
  28. "bsd-uthreads",
  29. N_("BSD user-level threads"),
  30. N_("BSD user-level threads")
  31. };
  32. struct bsd_uthread_target final : public target_ops
  33. {
  34. const target_info &info () const override
  35. { return bsd_uthread_target_info; }
  36. strata stratum () const override { return thread_stratum; }
  37. void close () override;
  38. void mourn_inferior () override;
  39. void fetch_registers (struct regcache *, int) override;
  40. void store_registers (struct regcache *, int) override;
  41. ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override;
  42. void resume (ptid_t, int, enum gdb_signal) override;
  43. bool thread_alive (ptid_t ptid) override;
  44. void update_thread_list () override;
  45. const char *extra_thread_info (struct thread_info *) override;
  46. std::string pid_to_str (ptid_t) override;
  47. };
  48. static bsd_uthread_target bsd_uthread_ops;
  49. /* Architecture-specific operations. */
  50. /* Per-architecture data key. */
  51. static struct gdbarch_data *bsd_uthread_data;
  52. struct bsd_uthread_ops
  53. {
  54. /* Supply registers for an inactive thread to a register cache. */
  55. void (*supply_uthread)(struct regcache *, int, CORE_ADDR);
  56. /* Collect registers for an inactive thread from a register cache. */
  57. void (*collect_uthread)(const struct regcache *, int, CORE_ADDR);
  58. };
  59. static void *
  60. bsd_uthread_init (struct obstack *obstack)
  61. {
  62. struct bsd_uthread_ops *ops;
  63. ops = OBSTACK_ZALLOC (obstack, struct bsd_uthread_ops);
  64. return ops;
  65. }
  66. /* Set the function that supplies registers from an inactive thread
  67. for architecture GDBARCH to SUPPLY_UTHREAD. */
  68. void
  69. bsd_uthread_set_supply_uthread (struct gdbarch *gdbarch,
  70. void (*supply_uthread) (struct regcache *,
  71. int, CORE_ADDR))
  72. {
  73. struct bsd_uthread_ops *ops
  74. = (struct bsd_uthread_ops *) gdbarch_data (gdbarch, bsd_uthread_data);
  75. ops->supply_uthread = supply_uthread;
  76. }
  77. /* Set the function that collects registers for an inactive thread for
  78. architecture GDBARCH to SUPPLY_UTHREAD. */
  79. void
  80. bsd_uthread_set_collect_uthread (struct gdbarch *gdbarch,
  81. void (*collect_uthread) (const struct regcache *,
  82. int, CORE_ADDR))
  83. {
  84. struct bsd_uthread_ops *ops
  85. = (struct bsd_uthread_ops *) gdbarch_data (gdbarch, bsd_uthread_data);
  86. ops->collect_uthread = collect_uthread;
  87. }
  88. /* Magic number to help recognize a valid thread structure. */
  89. #define BSD_UTHREAD_PTHREAD_MAGIC 0xd09ba115
  90. /* Check whether the thread structure at ADDR is valid. */
  91. static void
  92. bsd_uthread_check_magic (CORE_ADDR addr)
  93. {
  94. enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  95. ULONGEST magic = read_memory_unsigned_integer (addr, 4, byte_order);
  96. if (magic != BSD_UTHREAD_PTHREAD_MAGIC)
  97. error (_("Bad magic"));
  98. }
  99. /* Thread states. */
  100. #define BSD_UTHREAD_PS_RUNNING 0
  101. #define BSD_UTHREAD_PS_DEAD 18
  102. /* Address of the pointer to the thread structure for the running
  103. thread. */
  104. static CORE_ADDR bsd_uthread_thread_run_addr;
  105. /* Address of the list of all threads. */
  106. static CORE_ADDR bsd_uthread_thread_list_addr;
  107. /* Offsets of various "interesting" bits in the thread structure. */
  108. static int bsd_uthread_thread_state_offset = -1;
  109. static int bsd_uthread_thread_next_offset = -1;
  110. static int bsd_uthread_thread_ctx_offset;
  111. /* Name of shared threads library. */
  112. static const char *bsd_uthread_solib_name;
  113. /* Non-zero if the thread startum implemented by this module is active. */
  114. static int bsd_uthread_active;
  115. static CORE_ADDR
  116. bsd_uthread_lookup_address (const char *name, struct objfile *objfile)
  117. {
  118. struct bound_minimal_symbol sym;
  119. sym = lookup_minimal_symbol (name, NULL, objfile);
  120. if (sym.minsym)
  121. return BMSYMBOL_VALUE_ADDRESS (sym);
  122. return 0;
  123. }
  124. static int
  125. bsd_uthread_lookup_offset (const char *name, struct objfile *objfile)
  126. {
  127. enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  128. CORE_ADDR addr;
  129. addr = bsd_uthread_lookup_address (name, objfile);
  130. if (addr == 0)
  131. return 0;
  132. return read_memory_unsigned_integer (addr, 4, byte_order);
  133. }
  134. static CORE_ADDR
  135. bsd_uthread_read_memory_address (CORE_ADDR addr)
  136. {
  137. struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
  138. return read_memory_typed_address (addr, ptr_type);
  139. }
  140. /* If OBJFILE contains the symbols corresponding to one of the
  141. supported user-level threads libraries, activate the thread stratum
  142. implemented by this module. */
  143. static int
  144. bsd_uthread_activate (struct objfile *objfile)
  145. {
  146. struct gdbarch *gdbarch = target_gdbarch ();
  147. struct bsd_uthread_ops *ops
  148. = (struct bsd_uthread_ops *) gdbarch_data (gdbarch, bsd_uthread_data);
  149. /* Skip if the thread stratum has already been activated. */
  150. if (bsd_uthread_active)
  151. return 0;
  152. /* There's no point in enabling this module if no
  153. architecture-specific operations are provided. */
  154. if (!ops->supply_uthread)
  155. return 0;
  156. bsd_uthread_thread_run_addr =
  157. bsd_uthread_lookup_address ("_thread_run", objfile);
  158. if (bsd_uthread_thread_run_addr == 0)
  159. return 0;
  160. bsd_uthread_thread_list_addr =
  161. bsd_uthread_lookup_address ("_thread_list", objfile);
  162. if (bsd_uthread_thread_list_addr == 0)
  163. return 0;
  164. bsd_uthread_thread_state_offset =
  165. bsd_uthread_lookup_offset ("_thread_state_offset", objfile);
  166. if (bsd_uthread_thread_state_offset == 0)
  167. return 0;
  168. bsd_uthread_thread_next_offset =
  169. bsd_uthread_lookup_offset ("_thread_next_offset", objfile);
  170. if (bsd_uthread_thread_next_offset == 0)
  171. return 0;
  172. bsd_uthread_thread_ctx_offset =
  173. bsd_uthread_lookup_offset ("_thread_ctx_offset", objfile);
  174. current_inferior ()->push_target (&bsd_uthread_ops);
  175. bsd_uthread_active = 1;
  176. return 1;
  177. }
  178. /* Cleanup due to deactivation. */
  179. void
  180. bsd_uthread_target::close ()
  181. {
  182. bsd_uthread_active = 0;
  183. bsd_uthread_thread_run_addr = 0;
  184. bsd_uthread_thread_list_addr = 0;
  185. bsd_uthread_thread_state_offset = 0;
  186. bsd_uthread_thread_next_offset = 0;
  187. bsd_uthread_thread_ctx_offset = 0;
  188. bsd_uthread_solib_name = NULL;
  189. }
  190. /* Deactivate the thread stratum implemented by this module. */
  191. static void
  192. bsd_uthread_deactivate (void)
  193. {
  194. /* Skip if the thread stratum has already been deactivated. */
  195. if (!bsd_uthread_active)
  196. return;
  197. current_inferior ()->unpush_target (&bsd_uthread_ops);
  198. }
  199. static void
  200. bsd_uthread_inferior_created (inferior *inf)
  201. {
  202. bsd_uthread_activate (NULL);
  203. }
  204. /* Likely candidates for the threads library. */
  205. static const char * const bsd_uthread_solib_names[] =
  206. {
  207. "/usr/lib/libc_r.so", /* FreeBSD */
  208. "/usr/lib/libpthread.so", /* OpenBSD */
  209. NULL
  210. };
  211. static void
  212. bsd_uthread_solib_loaded (struct so_list *so)
  213. {
  214. const char * const *names = bsd_uthread_solib_names;
  215. for (names = bsd_uthread_solib_names; *names; names++)
  216. {
  217. if (startswith (so->so_original_name, *names))
  218. {
  219. solib_read_symbols (so, 0);
  220. if (bsd_uthread_activate (so->objfile))
  221. {
  222. bsd_uthread_solib_name = so->so_original_name;
  223. return;
  224. }
  225. }
  226. }
  227. }
  228. static void
  229. bsd_uthread_solib_unloaded (struct so_list *so)
  230. {
  231. if (!bsd_uthread_solib_name)
  232. return;
  233. if (strcmp (so->so_original_name, bsd_uthread_solib_name) == 0)
  234. bsd_uthread_deactivate ();
  235. }
  236. void
  237. bsd_uthread_target::mourn_inferior ()
  238. {
  239. beneath ()->mourn_inferior ();
  240. bsd_uthread_deactivate ();
  241. }
  242. void
  243. bsd_uthread_target::fetch_registers (struct regcache *regcache, int regnum)
  244. {
  245. struct gdbarch *gdbarch = regcache->arch ();
  246. struct bsd_uthread_ops *uthread_ops
  247. = (struct bsd_uthread_ops *) gdbarch_data (gdbarch, bsd_uthread_data);
  248. ptid_t ptid = regcache->ptid ();
  249. CORE_ADDR addr = ptid.tid ();
  250. CORE_ADDR active_addr;
  251. scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
  252. /* We are doing operations (e.g. reading memory) that rely on
  253. inferior_ptid. */
  254. inferior_ptid = ptid;
  255. /* Always fetch the appropriate registers from the layer beneath. */
  256. beneath ()->fetch_registers (regcache, regnum);
  257. /* FIXME: That might have gotten us more than we asked for. Make
  258. sure we overwrite all relevant registers with values from the
  259. thread structure. This can go once we fix the underlying target. */
  260. regnum = -1;
  261. active_addr = bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr);
  262. if (addr != 0 && addr != active_addr)
  263. {
  264. bsd_uthread_check_magic (addr);
  265. uthread_ops->supply_uthread (regcache, regnum,
  266. addr + bsd_uthread_thread_ctx_offset);
  267. }
  268. }
  269. void
  270. bsd_uthread_target::store_registers (struct regcache *regcache, int regnum)
  271. {
  272. struct gdbarch *gdbarch = regcache->arch ();
  273. struct bsd_uthread_ops *uthread_ops
  274. = (struct bsd_uthread_ops *) gdbarch_data (gdbarch, bsd_uthread_data);
  275. ptid_t ptid = regcache->ptid ();
  276. CORE_ADDR addr = ptid.tid ();
  277. CORE_ADDR active_addr;
  278. scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
  279. /* We are doing operations (e.g. reading memory) that rely on
  280. inferior_ptid. */
  281. inferior_ptid = ptid;
  282. active_addr = bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr);
  283. if (addr != 0 && addr != active_addr)
  284. {
  285. bsd_uthread_check_magic (addr);
  286. uthread_ops->collect_uthread (regcache, regnum,
  287. addr + bsd_uthread_thread_ctx_offset);
  288. }
  289. else
  290. {
  291. /* Updating the thread that is currently running; pass the
  292. request to the layer beneath. */
  293. beneath ()->store_registers (regcache, regnum);
  294. }
  295. }
  296. ptid_t
  297. bsd_uthread_target::wait (ptid_t ptid, struct target_waitstatus *status,
  298. target_wait_flags options)
  299. {
  300. enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  301. CORE_ADDR addr;
  302. process_stratum_target *beneath
  303. = as_process_stratum_target (this->beneath ());
  304. /* Pass the request to the layer beneath. */
  305. ptid = beneath->wait (ptid, status, options);
  306. /* If the process is no longer alive, there's no point in figuring
  307. out the thread ID. It will fail anyway. */
  308. if (status->kind () == TARGET_WAITKIND_SIGNALLED
  309. || status->kind () == TARGET_WAITKIND_EXITED)
  310. return ptid;
  311. /* Fetch the corresponding thread ID, and augment the returned
  312. process ID with it. */
  313. addr = bsd_uthread_read_memory_address (bsd_uthread_thread_run_addr);
  314. if (addr != 0)
  315. {
  316. gdb_byte buf[4];
  317. /* FIXME: For executables linked statically with the threads
  318. library, we end up here before the program has actually been
  319. executed. In that case ADDR will be garbage since it has
  320. been read from the wrong virtual memory image. */
  321. if (target_read_memory (addr, buf, 4) == 0)
  322. {
  323. ULONGEST magic = extract_unsigned_integer (buf, 4, byte_order);
  324. if (magic == BSD_UTHREAD_PTHREAD_MAGIC)
  325. ptid = ptid_t (ptid.pid (), 0, addr);
  326. }
  327. }
  328. /* If INFERIOR_PTID doesn't have a tid member yet, and we now have a
  329. ptid with tid set, then ptid is still the initial thread of
  330. the process. Notify GDB core about it. */
  331. if (inferior_ptid.tid () == 0
  332. && ptid.tid () != 0 && !in_thread_list (beneath, ptid))
  333. thread_change_ptid (beneath, inferior_ptid, ptid);
  334. /* Don't let the core see a ptid without a corresponding thread. */
  335. thread_info *thread = find_thread_ptid (beneath, ptid);
  336. if (thread == NULL || thread->state == THREAD_EXITED)
  337. add_thread (beneath, ptid);
  338. return ptid;
  339. }
  340. void
  341. bsd_uthread_target::resume (ptid_t ptid, int step, enum gdb_signal sig)
  342. {
  343. /* Pass the request to the layer beneath. */
  344. beneath ()->resume (ptid, step, sig);
  345. }
  346. bool
  347. bsd_uthread_target::thread_alive (ptid_t ptid)
  348. {
  349. enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  350. CORE_ADDR addr = ptid.tid ();
  351. if (addr != 0)
  352. {
  353. int offset = bsd_uthread_thread_state_offset;
  354. ULONGEST state;
  355. bsd_uthread_check_magic (addr);
  356. state = read_memory_unsigned_integer (addr + offset, 4, byte_order);
  357. if (state == BSD_UTHREAD_PS_DEAD)
  358. return false;
  359. }
  360. return beneath ()->thread_alive (ptid);
  361. }
  362. void
  363. bsd_uthread_target::update_thread_list ()
  364. {
  365. pid_t pid = inferior_ptid.pid ();
  366. int offset = bsd_uthread_thread_next_offset;
  367. CORE_ADDR addr;
  368. prune_threads ();
  369. addr = bsd_uthread_read_memory_address (bsd_uthread_thread_list_addr);
  370. while (addr != 0)
  371. {
  372. ptid_t ptid = ptid_t (pid, 0, addr);
  373. process_stratum_target *proc_target
  374. = as_process_stratum_target (this->beneath ());
  375. thread_info *thread = find_thread_ptid (proc_target, ptid);
  376. if (thread == nullptr || thread->state == THREAD_EXITED)
  377. {
  378. /* If INFERIOR_PTID doesn't have a tid member yet, then ptid
  379. is still the initial thread of the process. Notify GDB
  380. core about it. */
  381. if (inferior_ptid.tid () == 0)
  382. thread_change_ptid (proc_target, inferior_ptid, ptid);
  383. else
  384. add_thread (proc_target, ptid);
  385. }
  386. addr = bsd_uthread_read_memory_address (addr + offset);
  387. }
  388. }
  389. /* Possible states a thread can be in. */
  390. static const char * const bsd_uthread_state[] =
  391. {
  392. "RUNNING",
  393. "SIGTHREAD",
  394. "MUTEX_WAIT",
  395. "COND_WAIT",
  396. "FDLR_WAIT",
  397. "FDLW_WAIT",
  398. "FDR_WAIT",
  399. "FDW_WAIT",
  400. "FILE_WAIT",
  401. "POLL_WAIT",
  402. "SELECT_WAIT",
  403. "SLEEP_WAIT",
  404. "WAIT_WAIT",
  405. "SIGSUSPEND",
  406. "SIGWAIT",
  407. "SPINBLOCK",
  408. "JOIN",
  409. "SUSPENDED",
  410. "DEAD",
  411. "DEADLOCK"
  412. };
  413. /* Return a string describing th state of the thread specified by
  414. INFO. */
  415. const char *
  416. bsd_uthread_target::extra_thread_info (thread_info *info)
  417. {
  418. enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  419. CORE_ADDR addr = info->ptid.tid ();
  420. if (addr != 0)
  421. {
  422. int offset = bsd_uthread_thread_state_offset;
  423. ULONGEST state;
  424. state = read_memory_unsigned_integer (addr + offset, 4, byte_order);
  425. if (state < ARRAY_SIZE (bsd_uthread_state))
  426. return bsd_uthread_state[state];
  427. }
  428. return NULL;
  429. }
  430. std::string
  431. bsd_uthread_target::pid_to_str (ptid_t ptid)
  432. {
  433. if (ptid.tid () != 0)
  434. return string_printf ("process %d, thread 0x%s",
  435. ptid.pid (),
  436. phex_nz (ptid.tid (), sizeof (ULONGEST)));
  437. return normal_pid_to_str (ptid);
  438. }
  439. void _initialize_bsd_uthread ();
  440. void
  441. _initialize_bsd_uthread ()
  442. {
  443. bsd_uthread_data = gdbarch_data_register_pre_init (bsd_uthread_init);
  444. gdb::observers::inferior_created.attach (bsd_uthread_inferior_created,
  445. "bsd-uthread");
  446. gdb::observers::solib_loaded.attach (bsd_uthread_solib_loaded,
  447. "bsd-uthread");
  448. gdb::observers::solib_unloaded.attach (bsd_uthread_solib_unloaded,
  449. "bsd-uthread");
  450. }