ia64-libunwind-tdep.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. /* Frame unwinder for ia64 frames using the libunwind library.
  2. Copyright (C) 2003-2022 Free Software Foundation, Inc.
  3. Written by Jeff Johnston, contributed by Red Hat Inc.
  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 "defs.h"
  16. #include "inferior.h"
  17. #include "frame.h"
  18. #include "frame-base.h"
  19. #include "frame-unwind.h"
  20. #include "gdbcore.h"
  21. #include "gdbtypes.h"
  22. #include "symtab.h"
  23. #include "objfiles.h"
  24. #include "regcache.h"
  25. #include <dlfcn.h>
  26. #include "ia64-libunwind-tdep.h"
  27. #include "gdbsupport/preprocessor.h"
  28. /* IA-64 is the only target that currently uses ia64-libunwind-tdep.
  29. Note how UNW_TARGET, UNW_OBJ, etc. are compile time constants below.
  30. Those come from libunwind's headers, and are target dependent.
  31. Also, some of libunwind's typedefs are target dependent, as e.g.,
  32. unw_word_t. If some other target wants to use this, we will need
  33. to do some abstracting in order to make it possible to select which
  34. libunwind we're talking to at runtime (and have one per arch). */
  35. /* The following two macros are normally defined in <endian.h>.
  36. But systems such as ia64-hpux do not provide such header, so
  37. we just define them here if not already defined. */
  38. #ifndef __LITTLE_ENDIAN
  39. #define __LITTLE_ENDIAN 1234
  40. #endif
  41. #ifndef __BIG_ENDIAN
  42. #define __BIG_ENDIAN 4321
  43. #endif
  44. static int libunwind_initialized;
  45. static struct gdbarch_data *libunwind_descr_handle;
  46. /* Required function pointers from libunwind. */
  47. typedef int (unw_get_reg_p_ftype) (unw_cursor_t *, unw_regnum_t, unw_word_t *);
  48. static unw_get_reg_p_ftype *unw_get_reg_p;
  49. typedef int (unw_get_fpreg_p_ftype) (unw_cursor_t *, unw_regnum_t,
  50. unw_fpreg_t *);
  51. static unw_get_fpreg_p_ftype *unw_get_fpreg_p;
  52. typedef int (unw_get_saveloc_p_ftype) (unw_cursor_t *, unw_regnum_t,
  53. unw_save_loc_t *);
  54. static unw_get_saveloc_p_ftype *unw_get_saveloc_p;
  55. typedef int (unw_is_signal_frame_p_ftype) (unw_cursor_t *);
  56. static unw_is_signal_frame_p_ftype *unw_is_signal_frame_p;
  57. typedef int (unw_step_p_ftype) (unw_cursor_t *);
  58. static unw_step_p_ftype *unw_step_p;
  59. typedef int (unw_init_remote_p_ftype) (unw_cursor_t *, unw_addr_space_t,
  60. void *);
  61. static unw_init_remote_p_ftype *unw_init_remote_p;
  62. typedef unw_addr_space_t (unw_create_addr_space_p_ftype) (unw_accessors_t *,
  63. int);
  64. static unw_create_addr_space_p_ftype *unw_create_addr_space_p;
  65. typedef void (unw_destroy_addr_space_p_ftype) (unw_addr_space_t);
  66. static unw_destroy_addr_space_p_ftype *unw_destroy_addr_space_p;
  67. typedef int (unw_search_unwind_table_p_ftype) (unw_addr_space_t, unw_word_t,
  68. unw_dyn_info_t *,
  69. unw_proc_info_t *, int, void *);
  70. static unw_search_unwind_table_p_ftype *unw_search_unwind_table_p;
  71. typedef unw_word_t (unw_find_dyn_list_p_ftype) (unw_addr_space_t,
  72. unw_dyn_info_t *, void *);
  73. static unw_find_dyn_list_p_ftype *unw_find_dyn_list_p;
  74. struct libunwind_frame_cache
  75. {
  76. CORE_ADDR base;
  77. CORE_ADDR func_addr;
  78. unw_cursor_t cursor;
  79. unw_addr_space_t as;
  80. };
  81. /* We need to qualify the function names with a platform-specific prefix
  82. to match the names used by the libunwind library. The UNW_OBJ macro is
  83. provided by the libunwind.h header file. */
  84. #ifndef LIBUNWIND_SO
  85. /* Use the stable ABI major version number. `libunwind-ia64.so' is a link time
  86. only library, not a runtime one. */
  87. #define LIBUNWIND_SO "libunwind-" STRINGIFY(UNW_TARGET) ".so.8"
  88. /* Provide also compatibility with older .so. The two APIs are compatible, .8
  89. is only extended a bit, GDB does not use the extended API at all. */
  90. #define LIBUNWIND_SO_7 "libunwind-" STRINGIFY(UNW_TARGET) ".so.7"
  91. #endif
  92. static const char *get_reg_name = STRINGIFY(UNW_OBJ(get_reg));
  93. static const char *get_fpreg_name = STRINGIFY(UNW_OBJ(get_fpreg));
  94. static const char *get_saveloc_name = STRINGIFY(UNW_OBJ(get_save_loc));
  95. static const char *is_signal_frame_name = STRINGIFY(UNW_OBJ(is_signal_frame));
  96. static const char *step_name = STRINGIFY(UNW_OBJ(step));
  97. static const char *init_remote_name = STRINGIFY(UNW_OBJ(init_remote));
  98. static const char *create_addr_space_name
  99. = STRINGIFY(UNW_OBJ(create_addr_space));
  100. static const char *destroy_addr_space_name
  101. = STRINGIFY(UNW_OBJ(destroy_addr_space));
  102. static const char *search_unwind_table_name
  103. = STRINGIFY(UNW_OBJ(search_unwind_table));
  104. static const char *find_dyn_list_name = STRINGIFY(UNW_OBJ(find_dyn_list));
  105. static struct libunwind_descr *
  106. libunwind_descr (struct gdbarch *gdbarch)
  107. {
  108. return ((struct libunwind_descr *)
  109. gdbarch_data (gdbarch, libunwind_descr_handle));
  110. }
  111. static void *
  112. libunwind_descr_init (struct obstack *obstack)
  113. {
  114. struct libunwind_descr *descr
  115. = OBSTACK_ZALLOC (obstack, struct libunwind_descr);
  116. return descr;
  117. }
  118. void
  119. libunwind_frame_set_descr (struct gdbarch *gdbarch,
  120. struct libunwind_descr *descr)
  121. {
  122. struct libunwind_descr *arch_descr;
  123. gdb_assert (gdbarch != NULL);
  124. arch_descr = ((struct libunwind_descr *)
  125. gdbarch_data (gdbarch, libunwind_descr_handle));
  126. gdb_assert (arch_descr != NULL);
  127. /* Copy new descriptor info into arch descriptor. */
  128. arch_descr->gdb2uw = descr->gdb2uw;
  129. arch_descr->uw2gdb = descr->uw2gdb;
  130. arch_descr->is_fpreg = descr->is_fpreg;
  131. arch_descr->accessors = descr->accessors;
  132. arch_descr->special_accessors = descr->special_accessors;
  133. }
  134. static struct libunwind_frame_cache *
  135. libunwind_frame_cache (struct frame_info *this_frame, void **this_cache)
  136. {
  137. unw_accessors_t *acc;
  138. unw_addr_space_t as;
  139. unw_word_t fp;
  140. unw_regnum_t uw_sp_regnum;
  141. struct libunwind_frame_cache *cache;
  142. struct libunwind_descr *descr;
  143. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  144. int ret;
  145. if (*this_cache)
  146. return (struct libunwind_frame_cache *) *this_cache;
  147. /* Allocate a new cache. */
  148. cache = FRAME_OBSTACK_ZALLOC (struct libunwind_frame_cache);
  149. cache->func_addr = get_frame_func (this_frame);
  150. if (cache->func_addr == 0)
  151. /* This can happen when the frame corresponds to a function for which
  152. there is no debugging information nor any entry in the symbol table.
  153. This is probably a static function for which an entry in the symbol
  154. table was not created when the objfile got linked (observed in
  155. libpthread.so on ia64-hpux).
  156. The best we can do, in that case, is use the frame PC as the function
  157. address. We don't need to give up since we still have the unwind
  158. record to help us perform the unwinding. There is also another
  159. compelling to continue, because abandoning now means stopping
  160. the backtrace, which can never be helpful for the user. */
  161. cache->func_addr = get_frame_pc (this_frame);
  162. /* Get a libunwind cursor to the previous frame.
  163. We do this by initializing a cursor. Libunwind treats a new cursor
  164. as the top of stack and will get the current register set via the
  165. libunwind register accessor. Now, we provide the platform-specific
  166. accessors and we set up the register accessor to use the frame
  167. register unwinding interfaces so that we properly get the registers
  168. for the current frame rather than the top. We then use the unw_step
  169. function to move the libunwind cursor back one frame. We can later
  170. use this cursor to find previous registers via the unw_get_reg
  171. interface which will invoke libunwind's special logic. */
  172. descr = libunwind_descr (gdbarch);
  173. acc = (unw_accessors_t *) descr->accessors;
  174. as = unw_create_addr_space_p (acc,
  175. gdbarch_byte_order (gdbarch)
  176. == BFD_ENDIAN_BIG
  177. ? __BIG_ENDIAN
  178. : __LITTLE_ENDIAN);
  179. unw_init_remote_p (&cache->cursor, as, this_frame);
  180. if (unw_step_p (&cache->cursor) < 0)
  181. {
  182. unw_destroy_addr_space_p (as);
  183. return NULL;
  184. }
  185. /* To get base address, get sp from previous frame. */
  186. uw_sp_regnum = descr->gdb2uw (gdbarch_sp_regnum (gdbarch));
  187. ret = unw_get_reg_p (&cache->cursor, uw_sp_regnum, &fp);
  188. if (ret < 0)
  189. {
  190. unw_destroy_addr_space_p (as);
  191. error (_("Can't get libunwind sp register."));
  192. }
  193. cache->base = (CORE_ADDR)fp;
  194. cache->as = as;
  195. *this_cache = cache;
  196. return cache;
  197. }
  198. void
  199. libunwind_frame_dealloc_cache (struct frame_info *self, void *this_cache)
  200. {
  201. struct libunwind_frame_cache *cache
  202. = (struct libunwind_frame_cache *) this_cache;
  203. if (cache->as)
  204. unw_destroy_addr_space_p (cache->as);
  205. }
  206. unw_word_t
  207. libunwind_find_dyn_list (unw_addr_space_t as, unw_dyn_info_t *di, void *arg)
  208. {
  209. return unw_find_dyn_list_p (as, di, arg);
  210. }
  211. /* Verify if there is sufficient libunwind information for the frame to use
  212. libunwind frame unwinding. */
  213. int
  214. libunwind_frame_sniffer (const struct frame_unwind *self,
  215. struct frame_info *this_frame, void **this_cache)
  216. {
  217. unw_cursor_t cursor;
  218. unw_accessors_t *acc;
  219. unw_addr_space_t as;
  220. struct libunwind_descr *descr;
  221. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  222. int ret;
  223. /* To test for libunwind unwind support, initialize a cursor to
  224. the current frame and try to back up. We use this same method
  225. when setting up the frame cache (see libunwind_frame_cache()).
  226. If libunwind returns success for this operation, it means that
  227. it has found sufficient libunwind unwinding information to do so. */
  228. descr = libunwind_descr (gdbarch);
  229. acc = (unw_accessors_t *) descr->accessors;
  230. as = unw_create_addr_space_p (acc,
  231. gdbarch_byte_order (gdbarch)
  232. == BFD_ENDIAN_BIG
  233. ? __BIG_ENDIAN
  234. : __LITTLE_ENDIAN);
  235. ret = unw_init_remote_p (&cursor, as, this_frame);
  236. if (ret < 0)
  237. {
  238. unw_destroy_addr_space_p (as);
  239. return 0;
  240. }
  241. /* Check to see if we have libunwind info by checking if we are in a
  242. signal frame. If it doesn't return an error, we have libunwind info
  243. and can use libunwind. */
  244. ret = unw_is_signal_frame_p (&cursor);
  245. unw_destroy_addr_space_p (as);
  246. if (ret < 0)
  247. return 0;
  248. return 1;
  249. }
  250. void
  251. libunwind_frame_this_id (struct frame_info *this_frame, void **this_cache,
  252. struct frame_id *this_id)
  253. {
  254. struct libunwind_frame_cache *cache =
  255. libunwind_frame_cache (this_frame, this_cache);
  256. if (cache != NULL)
  257. (*this_id) = frame_id_build (cache->base, cache->func_addr);
  258. }
  259. struct value *
  260. libunwind_frame_prev_register (struct frame_info *this_frame,
  261. void **this_cache, int regnum)
  262. {
  263. struct libunwind_frame_cache *cache =
  264. libunwind_frame_cache (this_frame, this_cache);
  265. unw_save_loc_t sl;
  266. int ret;
  267. unw_word_t intval;
  268. unw_fpreg_t fpval;
  269. unw_regnum_t uw_regnum;
  270. struct libunwind_descr *descr;
  271. struct value *val = NULL;
  272. if (cache == NULL)
  273. return frame_unwind_got_constant (this_frame, regnum, 0);
  274. /* Convert from gdb register number to libunwind register number. */
  275. descr = libunwind_descr (get_frame_arch (this_frame));
  276. uw_regnum = descr->gdb2uw (regnum);
  277. gdb_assert (regnum >= 0);
  278. if (!target_has_registers ())
  279. error (_("No registers."));
  280. if (uw_regnum < 0)
  281. return frame_unwind_got_constant (this_frame, regnum, 0);
  282. if (unw_get_saveloc_p (&cache->cursor, uw_regnum, &sl) < 0)
  283. return frame_unwind_got_constant (this_frame, regnum, 0);
  284. switch (sl.type)
  285. {
  286. case UNW_SLT_MEMORY:
  287. val = frame_unwind_got_memory (this_frame, regnum, sl.u.addr);
  288. break;
  289. case UNW_SLT_REG:
  290. val = frame_unwind_got_register (this_frame, regnum,
  291. descr->uw2gdb (sl.u.regnum));
  292. break;
  293. case UNW_SLT_NONE:
  294. {
  295. /* The register is not stored at a specific memory address nor
  296. inside another register. So use libunwind to fetch the register
  297. value for us, and create a constant value with the result. */
  298. if (descr->is_fpreg (uw_regnum))
  299. {
  300. ret = unw_get_fpreg_p (&cache->cursor, uw_regnum, &fpval);
  301. if (ret < 0)
  302. return frame_unwind_got_constant (this_frame, regnum, 0);
  303. val = frame_unwind_got_bytes (this_frame, regnum,
  304. (gdb_byte *) &fpval);
  305. }
  306. else
  307. {
  308. ret = unw_get_reg_p (&cache->cursor, uw_regnum, &intval);
  309. if (ret < 0)
  310. return frame_unwind_got_constant (this_frame, regnum, 0);
  311. val = frame_unwind_got_constant (this_frame, regnum, intval);
  312. }
  313. break;
  314. }
  315. }
  316. return val;
  317. }
  318. /* The following is a glue routine to call the libunwind unwind table
  319. search function to get unwind information for a specified ip address. */
  320. int
  321. libunwind_search_unwind_table (void *as, long ip, void *di,
  322. void *pi, int need_unwind_info, void *args)
  323. {
  324. return unw_search_unwind_table_p (*(unw_addr_space_t *) as, (unw_word_t) ip,
  325. (unw_dyn_info_t *) di,
  326. (unw_proc_info_t *) pi, need_unwind_info,
  327. args);
  328. }
  329. /* Verify if we are in a sigtramp frame and we can use libunwind to unwind. */
  330. int
  331. libunwind_sigtramp_frame_sniffer (const struct frame_unwind *self,
  332. struct frame_info *this_frame,
  333. void **this_cache)
  334. {
  335. unw_cursor_t cursor;
  336. unw_accessors_t *acc;
  337. unw_addr_space_t as;
  338. struct libunwind_descr *descr;
  339. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  340. int ret;
  341. /* To test for libunwind unwind support, initialize a cursor to the
  342. current frame and try to back up. We use this same method when
  343. setting up the frame cache (see libunwind_frame_cache()). If
  344. libunwind returns success for this operation, it means that it
  345. has found sufficient libunwind unwinding information to do
  346. so. */
  347. descr = libunwind_descr (gdbarch);
  348. acc = (unw_accessors_t *) descr->accessors;
  349. as = unw_create_addr_space_p (acc,
  350. gdbarch_byte_order (gdbarch)
  351. == BFD_ENDIAN_BIG
  352. ? __BIG_ENDIAN
  353. : __LITTLE_ENDIAN);
  354. ret = unw_init_remote_p (&cursor, as, this_frame);
  355. if (ret < 0)
  356. {
  357. unw_destroy_addr_space_p (as);
  358. return 0;
  359. }
  360. /* Check to see if we are in a signal frame. */
  361. ret = unw_is_signal_frame_p (&cursor);
  362. unw_destroy_addr_space_p (as);
  363. if (ret > 0)
  364. return 1;
  365. return 0;
  366. }
  367. /* The following routine is for accessing special registers of the top frame.
  368. A special set of accessors must be given that work without frame info.
  369. This is used by ia64 to access the rse registers r32-r127. While they
  370. are usually located at BOF, this is not always true and only the libunwind
  371. info can decipher where they actually are. */
  372. int
  373. libunwind_get_reg_special (struct gdbarch *gdbarch, readable_regcache *regcache,
  374. int regnum, void *buf)
  375. {
  376. unw_cursor_t cursor;
  377. unw_accessors_t *acc;
  378. unw_addr_space_t as;
  379. struct libunwind_descr *descr;
  380. int ret;
  381. unw_regnum_t uw_regnum;
  382. unw_word_t intval;
  383. unw_fpreg_t fpval;
  384. void *ptr;
  385. descr = libunwind_descr (gdbarch);
  386. acc = (unw_accessors_t *) descr->special_accessors;
  387. as = unw_create_addr_space_p (acc,
  388. gdbarch_byte_order (gdbarch)
  389. == BFD_ENDIAN_BIG
  390. ? __BIG_ENDIAN
  391. : __LITTLE_ENDIAN);
  392. ret = unw_init_remote_p (&cursor, as, regcache);
  393. if (ret < 0)
  394. {
  395. unw_destroy_addr_space_p (as);
  396. return -1;
  397. }
  398. uw_regnum = descr->gdb2uw (regnum);
  399. if (descr->is_fpreg (uw_regnum))
  400. {
  401. ret = unw_get_fpreg_p (&cursor, uw_regnum, &fpval);
  402. ptr = &fpval;
  403. }
  404. else
  405. {
  406. ret = unw_get_reg_p (&cursor, uw_regnum, &intval);
  407. ptr = &intval;
  408. }
  409. unw_destroy_addr_space_p (as);
  410. if (ret < 0)
  411. return -1;
  412. if (buf)
  413. memcpy (buf, ptr, register_size (gdbarch, regnum));
  414. return 0;
  415. }
  416. static int
  417. libunwind_load (void)
  418. {
  419. void *handle;
  420. char *so_error = NULL;
  421. handle = dlopen (LIBUNWIND_SO, RTLD_NOW);
  422. if (handle == NULL)
  423. {
  424. so_error = xstrdup (dlerror ());
  425. #ifdef LIBUNWIND_SO_7
  426. handle = dlopen (LIBUNWIND_SO_7, RTLD_NOW);
  427. #endif /* LIBUNWIND_SO_7 */
  428. }
  429. if (handle == NULL)
  430. {
  431. gdb_printf (gdb_stderr, _("[GDB failed to load %s: %s]\n"),
  432. LIBUNWIND_SO, so_error);
  433. #ifdef LIBUNWIND_SO_7
  434. gdb_printf (gdb_stderr, _("[GDB failed to load %s: %s]\n"),
  435. LIBUNWIND_SO_7, dlerror ());
  436. #endif /* LIBUNWIND_SO_7 */
  437. }
  438. xfree (so_error);
  439. if (handle == NULL)
  440. return 0;
  441. /* Initialize pointers to the dynamic library functions we will use. */
  442. unw_get_reg_p = (unw_get_reg_p_ftype *) dlsym (handle, get_reg_name);
  443. if (unw_get_reg_p == NULL)
  444. return 0;
  445. unw_get_fpreg_p = (unw_get_fpreg_p_ftype *) dlsym (handle, get_fpreg_name);
  446. if (unw_get_fpreg_p == NULL)
  447. return 0;
  448. unw_get_saveloc_p
  449. = (unw_get_saveloc_p_ftype *) dlsym (handle, get_saveloc_name);
  450. if (unw_get_saveloc_p == NULL)
  451. return 0;
  452. unw_is_signal_frame_p
  453. = (unw_is_signal_frame_p_ftype *) dlsym (handle, is_signal_frame_name);
  454. if (unw_is_signal_frame_p == NULL)
  455. return 0;
  456. unw_step_p = (unw_step_p_ftype *) dlsym (handle, step_name);
  457. if (unw_step_p == NULL)
  458. return 0;
  459. unw_init_remote_p
  460. = (unw_init_remote_p_ftype *) dlsym (handle, init_remote_name);
  461. if (unw_init_remote_p == NULL)
  462. return 0;
  463. unw_create_addr_space_p
  464. = (unw_create_addr_space_p_ftype *) dlsym (handle, create_addr_space_name);
  465. if (unw_create_addr_space_p == NULL)
  466. return 0;
  467. unw_destroy_addr_space_p
  468. = (unw_destroy_addr_space_p_ftype *) dlsym (handle,
  469. destroy_addr_space_name);
  470. if (unw_destroy_addr_space_p == NULL)
  471. return 0;
  472. unw_search_unwind_table_p
  473. = (unw_search_unwind_table_p_ftype *) dlsym (handle,
  474. search_unwind_table_name);
  475. if (unw_search_unwind_table_p == NULL)
  476. return 0;
  477. unw_find_dyn_list_p
  478. = (unw_find_dyn_list_p_ftype *) dlsym (handle, find_dyn_list_name);
  479. if (unw_find_dyn_list_p == NULL)
  480. return 0;
  481. return 1;
  482. }
  483. int
  484. libunwind_is_initialized (void)
  485. {
  486. return libunwind_initialized;
  487. }
  488. void _initialize_libunwind_frame ();
  489. void
  490. _initialize_libunwind_frame ()
  491. {
  492. libunwind_descr_handle
  493. = gdbarch_data_register_pre_init (libunwind_descr_init);
  494. libunwind_initialized = libunwind_load ();
  495. }