frame-unwind.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /* Definitions for frame unwinder, for GDB, the GNU debugger.
  2. Copyright (C) 2003-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 "frame.h"
  16. #include "frame-unwind.h"
  17. #include "dummy-frame.h"
  18. #include "inline-frame.h"
  19. #include "value.h"
  20. #include "regcache.h"
  21. #include "gdbsupport/gdb_obstack.h"
  22. #include "target.h"
  23. #include "gdbarch.h"
  24. #include "dwarf2/frame-tailcall.h"
  25. static struct gdbarch_data *frame_unwind_data;
  26. struct frame_unwind_table_entry
  27. {
  28. const struct frame_unwind *unwinder;
  29. struct frame_unwind_table_entry *next;
  30. };
  31. struct frame_unwind_table
  32. {
  33. struct frame_unwind_table_entry *list;
  34. /* The head of the OSABI part of the search list. */
  35. struct frame_unwind_table_entry **osabi_head;
  36. };
  37. /* A helper function to add an unwinder to a list. LINK says where to
  38. install the new unwinder. The new link is returned. */
  39. static struct frame_unwind_table_entry **
  40. add_unwinder (struct obstack *obstack, const struct frame_unwind *unwinder,
  41. struct frame_unwind_table_entry **link)
  42. {
  43. *link = OBSTACK_ZALLOC (obstack, struct frame_unwind_table_entry);
  44. (*link)->unwinder = unwinder;
  45. return &(*link)->next;
  46. }
  47. static void *
  48. frame_unwind_init (struct obstack *obstack)
  49. {
  50. struct frame_unwind_table *table
  51. = OBSTACK_ZALLOC (obstack, struct frame_unwind_table);
  52. /* Start the table out with a few default sniffers. OSABI code
  53. can't override this. */
  54. struct frame_unwind_table_entry **link = &table->list;
  55. link = add_unwinder (obstack, &dummy_frame_unwind, link);
  56. /* The DWARF tailcall sniffer must come before the inline sniffer.
  57. Otherwise, we can end up in a situation where a DWARF frame finds
  58. tailcall information, but then the inline sniffer claims a frame
  59. before the tailcall sniffer, resulting in confusion. This is
  60. safe to do always because the tailcall sniffer can only ever be
  61. activated if the newer frame was created using the DWARF
  62. unwinder, and it also found tailcall information. */
  63. link = add_unwinder (obstack, &dwarf2_tailcall_frame_unwind, link);
  64. link = add_unwinder (obstack, &inline_frame_unwind, link);
  65. /* The insertion point for OSABI sniffers. */
  66. table->osabi_head = link;
  67. return table;
  68. }
  69. void
  70. frame_unwind_prepend_unwinder (struct gdbarch *gdbarch,
  71. const struct frame_unwind *unwinder)
  72. {
  73. struct frame_unwind_table *table
  74. = (struct frame_unwind_table *) gdbarch_data (gdbarch, frame_unwind_data);
  75. struct frame_unwind_table_entry *entry;
  76. /* Insert the new entry at the start of the list. */
  77. entry = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct frame_unwind_table_entry);
  78. entry->unwinder = unwinder;
  79. entry->next = (*table->osabi_head);
  80. (*table->osabi_head) = entry;
  81. }
  82. void
  83. frame_unwind_append_unwinder (struct gdbarch *gdbarch,
  84. const struct frame_unwind *unwinder)
  85. {
  86. struct frame_unwind_table *table
  87. = (struct frame_unwind_table *) gdbarch_data (gdbarch, frame_unwind_data);
  88. struct frame_unwind_table_entry **ip;
  89. /* Find the end of the list and insert the new entry there. */
  90. for (ip = table->osabi_head; (*ip) != NULL; ip = &(*ip)->next);
  91. (*ip) = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct frame_unwind_table_entry);
  92. (*ip)->unwinder = unwinder;
  93. }
  94. /* Call SNIFFER from UNWINDER. If it succeeded set UNWINDER for
  95. THIS_FRAME and return 1. Otherwise the function keeps THIS_FRAME
  96. unchanged and returns 0. */
  97. static int
  98. frame_unwind_try_unwinder (struct frame_info *this_frame, void **this_cache,
  99. const struct frame_unwind *unwinder)
  100. {
  101. int res = 0;
  102. unsigned int entry_generation = get_frame_cache_generation ();
  103. frame_prepare_for_sniffer (this_frame, unwinder);
  104. try
  105. {
  106. frame_debug_printf ("trying unwinder \"%s\"", unwinder->name);
  107. res = unwinder->sniffer (unwinder, this_frame, this_cache);
  108. }
  109. catch (const gdb_exception &ex)
  110. {
  111. frame_debug_printf ("caught exception: %s", ex.message->c_str ());
  112. /* Catch all exceptions, caused by either interrupt or error.
  113. Reset *THIS_CACHE, unless something reinitialized the frame
  114. cache meanwhile, in which case THIS_FRAME/THIS_CACHE are now
  115. dangling. */
  116. if (get_frame_cache_generation () == entry_generation)
  117. {
  118. *this_cache = NULL;
  119. frame_cleanup_after_sniffer (this_frame);
  120. }
  121. if (ex.error == NOT_AVAILABLE_ERROR)
  122. {
  123. /* This usually means that not even the PC is available,
  124. thus most unwinders aren't able to determine if they're
  125. the best fit. Keep trying. Fallback prologue unwinders
  126. should always accept the frame. */
  127. return 0;
  128. }
  129. throw;
  130. }
  131. if (res)
  132. {
  133. frame_debug_printf ("yes");
  134. return 1;
  135. }
  136. else
  137. {
  138. frame_debug_printf ("no");
  139. /* Don't set *THIS_CACHE to NULL here, because sniffer has to do
  140. so. */
  141. frame_cleanup_after_sniffer (this_frame);
  142. return 0;
  143. }
  144. gdb_assert_not_reached ("frame_unwind_try_unwinder");
  145. }
  146. /* Iterate through sniffers for THIS_FRAME frame until one returns with an
  147. unwinder implementation. THIS_FRAME->UNWIND must be NULL, it will get set
  148. by this function. Possibly initialize THIS_CACHE. */
  149. void
  150. frame_unwind_find_by_frame (struct frame_info *this_frame, void **this_cache)
  151. {
  152. FRAME_SCOPED_DEBUG_ENTER_EXIT;
  153. frame_debug_printf ("this_frame=%d", frame_relative_level (this_frame));
  154. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  155. struct frame_unwind_table *table
  156. = (struct frame_unwind_table *) gdbarch_data (gdbarch, frame_unwind_data);
  157. struct frame_unwind_table_entry *entry;
  158. const struct frame_unwind *unwinder_from_target;
  159. unwinder_from_target = target_get_unwinder ();
  160. if (unwinder_from_target != NULL
  161. && frame_unwind_try_unwinder (this_frame, this_cache,
  162. unwinder_from_target))
  163. return;
  164. unwinder_from_target = target_get_tailcall_unwinder ();
  165. if (unwinder_from_target != NULL
  166. && frame_unwind_try_unwinder (this_frame, this_cache,
  167. unwinder_from_target))
  168. return;
  169. for (entry = table->list; entry != NULL; entry = entry->next)
  170. if (frame_unwind_try_unwinder (this_frame, this_cache, entry->unwinder))
  171. return;
  172. internal_error (__FILE__, __LINE__, _("frame_unwind_find_by_frame failed"));
  173. }
  174. /* A default frame sniffer which always accepts the frame. Used by
  175. fallback prologue unwinders. */
  176. int
  177. default_frame_sniffer (const struct frame_unwind *self,
  178. struct frame_info *this_frame,
  179. void **this_prologue_cache)
  180. {
  181. return 1;
  182. }
  183. /* The default frame unwinder stop_reason callback. */
  184. enum unwind_stop_reason
  185. default_frame_unwind_stop_reason (struct frame_info *this_frame,
  186. void **this_cache)
  187. {
  188. struct frame_id this_id = get_frame_id (this_frame);
  189. if (frame_id_eq (this_id, outer_frame_id))
  190. return UNWIND_OUTERMOST;
  191. else
  192. return UNWIND_NO_REASON;
  193. }
  194. /* See frame-unwind.h. */
  195. CORE_ADDR
  196. default_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
  197. {
  198. int pc_regnum = gdbarch_pc_regnum (gdbarch);
  199. CORE_ADDR pc = frame_unwind_register_unsigned (next_frame, pc_regnum);
  200. pc = gdbarch_addr_bits_remove (gdbarch, pc);
  201. return pc;
  202. }
  203. /* See frame-unwind.h. */
  204. CORE_ADDR
  205. default_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
  206. {
  207. int sp_regnum = gdbarch_sp_regnum (gdbarch);
  208. return frame_unwind_register_unsigned (next_frame, sp_regnum);
  209. }
  210. /* Helper functions for value-based register unwinding. These return
  211. a (possibly lazy) value of the appropriate type. */
  212. /* Return a value which indicates that FRAME did not save REGNUM. */
  213. struct value *
  214. frame_unwind_got_optimized (struct frame_info *frame, int regnum)
  215. {
  216. struct gdbarch *gdbarch = frame_unwind_arch (frame);
  217. struct type *type = register_type (gdbarch, regnum);
  218. return allocate_optimized_out_value (type);
  219. }
  220. /* Return a value which indicates that FRAME copied REGNUM into
  221. register NEW_REGNUM. */
  222. struct value *
  223. frame_unwind_got_register (struct frame_info *frame,
  224. int regnum, int new_regnum)
  225. {
  226. return value_of_register_lazy (frame, new_regnum);
  227. }
  228. /* Return a value which indicates that FRAME saved REGNUM in memory at
  229. ADDR. */
  230. struct value *
  231. frame_unwind_got_memory (struct frame_info *frame, int regnum, CORE_ADDR addr)
  232. {
  233. struct gdbarch *gdbarch = frame_unwind_arch (frame);
  234. struct value *v = value_at_lazy (register_type (gdbarch, regnum), addr);
  235. set_value_stack (v, 1);
  236. return v;
  237. }
  238. /* Return a value which indicates that FRAME's saved version of
  239. REGNUM has a known constant (computed) value of VAL. */
  240. struct value *
  241. frame_unwind_got_constant (struct frame_info *frame, int regnum,
  242. ULONGEST val)
  243. {
  244. struct gdbarch *gdbarch = frame_unwind_arch (frame);
  245. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  246. struct value *reg_val;
  247. reg_val = value_zero (register_type (gdbarch, regnum), not_lval);
  248. store_unsigned_integer (value_contents_writeable (reg_val).data (),
  249. register_size (gdbarch, regnum), byte_order, val);
  250. return reg_val;
  251. }
  252. struct value *
  253. frame_unwind_got_bytes (struct frame_info *frame, int regnum, const gdb_byte *buf)
  254. {
  255. struct gdbarch *gdbarch = frame_unwind_arch (frame);
  256. struct value *reg_val;
  257. reg_val = value_zero (register_type (gdbarch, regnum), not_lval);
  258. memcpy (value_contents_raw (reg_val).data (), buf,
  259. register_size (gdbarch, regnum));
  260. return reg_val;
  261. }
  262. /* Return a value which indicates that FRAME's saved version of REGNUM
  263. has a known constant (computed) value of ADDR. Convert the
  264. CORE_ADDR to a target address if necessary. */
  265. struct value *
  266. frame_unwind_got_address (struct frame_info *frame, int regnum,
  267. CORE_ADDR addr)
  268. {
  269. struct gdbarch *gdbarch = frame_unwind_arch (frame);
  270. struct value *reg_val;
  271. reg_val = value_zero (register_type (gdbarch, regnum), not_lval);
  272. pack_long (value_contents_writeable (reg_val).data (),
  273. register_type (gdbarch, regnum), addr);
  274. return reg_val;
  275. }
  276. void _initialize_frame_unwind ();
  277. void
  278. _initialize_frame_unwind ()
  279. {
  280. frame_unwind_data = gdbarch_data_register_pre_init (frame_unwind_init);
  281. }