dummy-frame.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /* Code dealing with dummy stack frames, for GDB, the GNU debugger.
  2. Copyright (C) 1986-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 "dummy-frame.h"
  16. #include "regcache.h"
  17. #include "frame.h"
  18. #include "inferior.h"
  19. #include "frame-unwind.h"
  20. #include "command.h"
  21. #include "gdbcmd.h"
  22. #include "observable.h"
  23. #include "gdbthread.h"
  24. #include "infcall.h"
  25. #include "gdbarch.h"
  26. struct dummy_frame_id
  27. {
  28. /* This frame's ID. Must match the value returned by
  29. gdbarch_dummy_id. */
  30. struct frame_id id;
  31. /* The thread this dummy_frame relates to. */
  32. thread_info *thread;
  33. };
  34. /* Return whether dummy_frame_id *ID1 and *ID2 are equal. */
  35. static int
  36. dummy_frame_id_eq (struct dummy_frame_id *id1,
  37. struct dummy_frame_id *id2)
  38. {
  39. return frame_id_eq (id1->id, id2->id) && id1->thread == id2->thread;
  40. }
  41. /* List of dummy_frame destructors. */
  42. struct dummy_frame_dtor_list
  43. {
  44. /* Next element in the list or NULL if this is the last element. */
  45. struct dummy_frame_dtor_list *next;
  46. /* If non-NULL, a destructor that is run when this dummy frame is freed. */
  47. dummy_frame_dtor_ftype *dtor;
  48. /* Arbitrary data that is passed to DTOR. */
  49. void *dtor_data;
  50. };
  51. /* Dummy frame. This saves the processor state just prior to setting
  52. up the inferior function call. Older targets save the registers
  53. on the target stack (but that really slows down function calls). */
  54. struct dummy_frame
  55. {
  56. struct dummy_frame *next;
  57. /* An id represents a dummy frame. */
  58. struct dummy_frame_id id;
  59. /* The caller's state prior to the call. */
  60. struct infcall_suspend_state *caller_state;
  61. /* First element of destructors list or NULL if there are no
  62. destructors registered for this dummy_frame. */
  63. struct dummy_frame_dtor_list *dtor_list;
  64. };
  65. static struct dummy_frame *dummy_frame_stack = NULL;
  66. /* Push the caller's state, along with the dummy frame info, onto the
  67. dummy-frame stack. */
  68. void
  69. dummy_frame_push (struct infcall_suspend_state *caller_state,
  70. const frame_id *dummy_id, thread_info *thread)
  71. {
  72. struct dummy_frame *dummy_frame;
  73. dummy_frame = XCNEW (struct dummy_frame);
  74. dummy_frame->caller_state = caller_state;
  75. dummy_frame->id.id = (*dummy_id);
  76. dummy_frame->id.thread = thread;
  77. dummy_frame->next = dummy_frame_stack;
  78. dummy_frame_stack = dummy_frame;
  79. }
  80. /* Remove *DUMMY_PTR from the dummy frame stack. */
  81. static void
  82. remove_dummy_frame (struct dummy_frame **dummy_ptr)
  83. {
  84. struct dummy_frame *dummy = *dummy_ptr;
  85. while (dummy->dtor_list != NULL)
  86. {
  87. struct dummy_frame_dtor_list *list = dummy->dtor_list;
  88. dummy->dtor_list = list->next;
  89. list->dtor (list->dtor_data, 0);
  90. xfree (list);
  91. }
  92. *dummy_ptr = dummy->next;
  93. discard_infcall_suspend_state (dummy->caller_state);
  94. xfree (dummy);
  95. }
  96. /* Delete any breakpoint B which is a momentary breakpoint for return from
  97. inferior call matching DUMMY_VOIDP. */
  98. static bool
  99. pop_dummy_frame_bpt (struct breakpoint *b, struct dummy_frame *dummy)
  100. {
  101. if (b->thread == dummy->id.thread->global_num
  102. && b->disposition == disp_del && frame_id_eq (b->frame_id, dummy->id.id))
  103. {
  104. while (b->related_breakpoint != b)
  105. delete_breakpoint (b->related_breakpoint);
  106. delete_breakpoint (b);
  107. /* Stop the traversal. */
  108. return true;
  109. }
  110. /* Continue the traversal. */
  111. return false;
  112. }
  113. /* Pop *DUMMY_PTR, restoring program state to that before the
  114. frame was created. */
  115. static void
  116. pop_dummy_frame (struct dummy_frame **dummy_ptr)
  117. {
  118. struct dummy_frame *dummy = *dummy_ptr;
  119. gdb_assert (dummy->id.thread == inferior_thread ());
  120. while (dummy->dtor_list != NULL)
  121. {
  122. struct dummy_frame_dtor_list *list = dummy->dtor_list;
  123. dummy->dtor_list = list->next;
  124. list->dtor (list->dtor_data, 1);
  125. xfree (list);
  126. }
  127. restore_infcall_suspend_state (dummy->caller_state);
  128. for (breakpoint *bp : all_breakpoints_safe ())
  129. if (pop_dummy_frame_bpt (bp, dummy))
  130. break;
  131. /* restore_infcall_control_state frees inf_state,
  132. all that remains is to pop *dummy_ptr. */
  133. *dummy_ptr = dummy->next;
  134. xfree (dummy);
  135. /* We've made right mess of GDB's local state, just discard
  136. everything. */
  137. reinit_frame_cache ();
  138. }
  139. /* Look up DUMMY_ID.
  140. Return NULL if not found. */
  141. static struct dummy_frame **
  142. lookup_dummy_frame (struct dummy_frame_id *dummy_id)
  143. {
  144. struct dummy_frame **dp;
  145. for (dp = &dummy_frame_stack; *dp != NULL; dp = &(*dp)->next)
  146. {
  147. if (dummy_frame_id_eq (&(*dp)->id, dummy_id))
  148. return dp;
  149. }
  150. return NULL;
  151. }
  152. /* Find the dummy frame by DUMMY_ID and THREAD, and pop it, restoring
  153. program state to that before the frame was created.
  154. On return reinit_frame_cache has been called.
  155. If the frame isn't found, flag an internal error. */
  156. void
  157. dummy_frame_pop (frame_id dummy_id, thread_info *thread)
  158. {
  159. struct dummy_frame **dp;
  160. struct dummy_frame_id id = { dummy_id, thread };
  161. dp = lookup_dummy_frame (&id);
  162. gdb_assert (dp != NULL);
  163. pop_dummy_frame (dp);
  164. }
  165. /* Find the dummy frame by DUMMY_ID and PTID and drop it. Do nothing
  166. if it is not found. Do not restore its state into inferior, just
  167. free its memory. */
  168. void
  169. dummy_frame_discard (struct frame_id dummy_id, thread_info *thread)
  170. {
  171. struct dummy_frame **dp;
  172. struct dummy_frame_id id = { dummy_id, thread };
  173. dp = lookup_dummy_frame (&id);
  174. if (dp)
  175. remove_dummy_frame (dp);
  176. }
  177. /* See dummy-frame.h. */
  178. void
  179. register_dummy_frame_dtor (frame_id dummy_id, thread_info *thread,
  180. dummy_frame_dtor_ftype *dtor, void *dtor_data)
  181. {
  182. struct dummy_frame_id id = { dummy_id, thread };
  183. struct dummy_frame **dp, *d;
  184. struct dummy_frame_dtor_list *list;
  185. dp = lookup_dummy_frame (&id);
  186. gdb_assert (dp != NULL);
  187. d = *dp;
  188. list = XNEW (struct dummy_frame_dtor_list);
  189. list->next = d->dtor_list;
  190. d->dtor_list = list;
  191. list->dtor = dtor;
  192. list->dtor_data = dtor_data;
  193. }
  194. /* See dummy-frame.h. */
  195. int
  196. find_dummy_frame_dtor (dummy_frame_dtor_ftype *dtor, void *dtor_data)
  197. {
  198. struct dummy_frame *d;
  199. for (d = dummy_frame_stack; d != NULL; d = d->next)
  200. {
  201. struct dummy_frame_dtor_list *list;
  202. for (list = d->dtor_list; list != NULL; list = list->next)
  203. if (list->dtor == dtor && list->dtor_data == dtor_data)
  204. return 1;
  205. }
  206. return 0;
  207. }
  208. /* There may be stale dummy frames, perhaps left over from when an uncaught
  209. longjmp took us out of a function that was called by the debugger. Clean
  210. them up at least once whenever we start a new inferior. */
  211. static void
  212. cleanup_dummy_frames (inferior *inf)
  213. {
  214. while (dummy_frame_stack != NULL)
  215. remove_dummy_frame (&dummy_frame_stack);
  216. }
  217. /* Return the dummy frame cache, it contains both the ID, and a
  218. pointer to the regcache. */
  219. struct dummy_frame_cache
  220. {
  221. struct frame_id this_id;
  222. readonly_detached_regcache *prev_regcache;
  223. };
  224. static int
  225. dummy_frame_sniffer (const struct frame_unwind *self,
  226. struct frame_info *this_frame,
  227. void **this_prologue_cache)
  228. {
  229. /* When unwinding a normal frame, the stack structure is determined
  230. by analyzing the frame's function's code (be it using brute force
  231. prologue analysis, or the dwarf2 CFI). In the case of a dummy
  232. frame, that simply isn't possible. The PC is either the program
  233. entry point, or some random address on the stack. Trying to use
  234. that PC to apply standard frame ID unwind techniques is just
  235. asking for trouble. */
  236. /* Don't bother unless there is at least one dummy frame. */
  237. if (dummy_frame_stack != NULL)
  238. {
  239. struct dummy_frame *dummyframe;
  240. /* Use an architecture specific method to extract this frame's
  241. dummy ID, assuming it is a dummy frame. */
  242. struct frame_id this_id
  243. = gdbarch_dummy_id (get_frame_arch (this_frame), this_frame);
  244. struct dummy_frame_id dummy_id = { this_id, inferior_thread () };
  245. /* Use that ID to find the corresponding cache entry. */
  246. for (dummyframe = dummy_frame_stack;
  247. dummyframe != NULL;
  248. dummyframe = dummyframe->next)
  249. {
  250. if (dummy_frame_id_eq (&dummyframe->id, &dummy_id))
  251. {
  252. struct dummy_frame_cache *cache;
  253. cache = FRAME_OBSTACK_ZALLOC (struct dummy_frame_cache);
  254. cache->prev_regcache = get_infcall_suspend_state_regcache
  255. (dummyframe->caller_state);
  256. cache->this_id = this_id;
  257. (*this_prologue_cache) = cache;
  258. return 1;
  259. }
  260. }
  261. }
  262. return 0;
  263. }
  264. /* Given a call-dummy dummy-frame, return the registers. Here the
  265. register value is taken from the local copy of the register buffer. */
  266. static struct value *
  267. dummy_frame_prev_register (struct frame_info *this_frame,
  268. void **this_prologue_cache,
  269. int regnum)
  270. {
  271. struct dummy_frame_cache *cache
  272. = (struct dummy_frame_cache *) *this_prologue_cache;
  273. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  274. struct value *reg_val;
  275. /* The dummy-frame sniffer always fills in the cache. */
  276. gdb_assert (cache != NULL);
  277. /* Describe the register's location. Generic dummy frames always
  278. have the register value in an ``expression''. */
  279. reg_val = value_zero (register_type (gdbarch, regnum), not_lval);
  280. /* Use the regcache_cooked_read() method so that it, on the fly,
  281. constructs either a raw or pseudo register from the raw
  282. register cache. */
  283. cache->prev_regcache->cooked_read
  284. (regnum, value_contents_writeable (reg_val).data ());
  285. return reg_val;
  286. }
  287. /* Assuming that THIS_FRAME is a dummy, return its ID. That ID is
  288. determined by examining the NEXT frame's unwound registers using
  289. the method dummy_id(). As a side effect, THIS dummy frame's
  290. dummy cache is located and saved in THIS_PROLOGUE_CACHE. */
  291. static void
  292. dummy_frame_this_id (struct frame_info *this_frame,
  293. void **this_prologue_cache,
  294. struct frame_id *this_id)
  295. {
  296. /* The dummy-frame sniffer always fills in the cache. */
  297. struct dummy_frame_cache *cache
  298. = (struct dummy_frame_cache *) *this_prologue_cache;
  299. gdb_assert (cache != NULL);
  300. (*this_id) = cache->this_id;
  301. }
  302. const struct frame_unwind dummy_frame_unwind =
  303. {
  304. "dummy",
  305. DUMMY_FRAME,
  306. default_frame_unwind_stop_reason,
  307. dummy_frame_this_id,
  308. dummy_frame_prev_register,
  309. NULL,
  310. dummy_frame_sniffer,
  311. };
  312. /* See dummy-frame.h. */
  313. struct frame_id
  314. default_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  315. {
  316. CORE_ADDR sp, pc;
  317. sp = get_frame_sp (this_frame);
  318. pc = get_frame_pc (this_frame);
  319. return frame_id_build (sp, pc);
  320. }
  321. static void
  322. fprint_dummy_frames (struct ui_file *file)
  323. {
  324. struct dummy_frame *s;
  325. for (s = dummy_frame_stack; s != NULL; s = s->next)
  326. gdb_printf (file, "%s: id=%s, ptid=%s\n",
  327. host_address_to_string (s),
  328. s->id.id.to_string ().c_str (),
  329. s->id.thread->ptid.to_string ().c_str ());
  330. }
  331. static void
  332. maintenance_print_dummy_frames (const char *args, int from_tty)
  333. {
  334. if (args == NULL)
  335. fprint_dummy_frames (gdb_stdout);
  336. else
  337. {
  338. stdio_file file;
  339. if (!file.open (args, "w"))
  340. perror_with_name (_("maintenance print dummy-frames"));
  341. fprint_dummy_frames (&file);
  342. }
  343. }
  344. void _initialize_dummy_frame ();
  345. void
  346. _initialize_dummy_frame ()
  347. {
  348. add_cmd ("dummy-frames", class_maintenance, maintenance_print_dummy_frames,
  349. _("Print the contents of the internal dummy-frame stack."),
  350. &maintenanceprintlist);
  351. gdb::observers::inferior_created.attach (cleanup_dummy_frames, "dummy-frame");
  352. }