reverse.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /* Reverse execution and reverse debugging.
  2. Copyright (C) 2006-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 "target.h"
  16. #include "top.h"
  17. #include "cli/cli-cmds.h"
  18. #include "cli/cli-decode.h"
  19. #include "cli/cli-utils.h"
  20. #include "inferior.h"
  21. #include "infrun.h"
  22. #include "regcache.h"
  23. /* User interface:
  24. reverse-step, reverse-next etc. */
  25. /* exec_reverse_once -- accepts an arbitrary gdb command (string),
  26. and executes it with exec-direction set to 'reverse'.
  27. Used to implement reverse-next etc. commands. */
  28. static void
  29. exec_reverse_once (const char *cmd, const char *args, int from_tty)
  30. {
  31. enum exec_direction_kind dir = execution_direction;
  32. if (dir == EXEC_REVERSE)
  33. error (_("Already in reverse mode. Use '%s' or 'set exec-dir forward'."),
  34. cmd);
  35. if (!target_can_execute_reverse ())
  36. error (_("Target %s does not support this command."), target_shortname ());
  37. std::string reverse_command = string_printf ("%s %s", cmd, args ? args : "");
  38. scoped_restore restore_exec_dir
  39. = make_scoped_restore (&execution_direction, EXEC_REVERSE);
  40. execute_command (reverse_command.c_str (), from_tty);
  41. }
  42. static void
  43. reverse_step (const char *args, int from_tty)
  44. {
  45. exec_reverse_once ("step", args, from_tty);
  46. }
  47. static void
  48. reverse_stepi (const char *args, int from_tty)
  49. {
  50. exec_reverse_once ("stepi", args, from_tty);
  51. }
  52. static void
  53. reverse_next (const char *args, int from_tty)
  54. {
  55. exec_reverse_once ("next", args, from_tty);
  56. }
  57. static void
  58. reverse_nexti (const char *args, int from_tty)
  59. {
  60. exec_reverse_once ("nexti", args, from_tty);
  61. }
  62. static void
  63. reverse_continue (const char *args, int from_tty)
  64. {
  65. exec_reverse_once ("continue", args, from_tty);
  66. }
  67. static void
  68. reverse_finish (const char *args, int from_tty)
  69. {
  70. exec_reverse_once ("finish", args, from_tty);
  71. }
  72. /* Data structures for a bookmark list. */
  73. struct bookmark {
  74. struct bookmark *next;
  75. int number;
  76. CORE_ADDR pc;
  77. struct symtab_and_line sal;
  78. gdb_byte *opaque_data;
  79. };
  80. static struct bookmark *bookmark_chain;
  81. static int bookmark_count;
  82. #define ALL_BOOKMARKS(B) for ((B) = bookmark_chain; (B); (B) = (B)->next)
  83. #define ALL_BOOKMARKS_SAFE(B,TMP) \
  84. for ((B) = bookmark_chain; \
  85. (B) ? ((TMP) = (B)->next, 1) : 0; \
  86. (B) = (TMP))
  87. /* save_bookmark_command -- implement "bookmark" command.
  88. Call target method to get a bookmark identifier.
  89. Insert bookmark identifier into list.
  90. Identifier will be a malloc string (gdb_byte *).
  91. Up to us to free it as required. */
  92. static void
  93. save_bookmark_command (const char *args, int from_tty)
  94. {
  95. /* Get target's idea of a bookmark. */
  96. gdb_byte *bookmark_id = target_get_bookmark (args, from_tty);
  97. struct gdbarch *gdbarch = get_current_regcache ()->arch ();
  98. /* CR should not cause another identical bookmark. */
  99. dont_repeat ();
  100. if (bookmark_id == NULL)
  101. error (_("target_get_bookmark failed."));
  102. /* Set up a bookmark struct. */
  103. bookmark *b = new bookmark ();
  104. b->number = ++bookmark_count;
  105. b->pc = regcache_read_pc (get_current_regcache ());
  106. b->sal = find_pc_line (b->pc, 0);
  107. b->sal.pspace = get_frame_program_space (get_current_frame ());
  108. b->opaque_data = bookmark_id;
  109. b->next = NULL;
  110. /* Add this bookmark to the end of the chain, so that a list
  111. of bookmarks will come out in order of increasing numbers. */
  112. bookmark *b1 = bookmark_chain;
  113. if (b1 == 0)
  114. bookmark_chain = b;
  115. else
  116. {
  117. while (b1->next)
  118. b1 = b1->next;
  119. b1->next = b;
  120. }
  121. gdb_printf (_("Saved bookmark %d at %s\n"), b->number,
  122. paddress (gdbarch, b->sal.pc));
  123. }
  124. /* Implement "delete bookmark" command. */
  125. static int
  126. delete_one_bookmark (int num)
  127. {
  128. struct bookmark *b1, *b;
  129. /* Find bookmark with corresponding number. */
  130. ALL_BOOKMARKS (b)
  131. if (b->number == num)
  132. break;
  133. /* Special case, first item in list. */
  134. if (b == bookmark_chain)
  135. bookmark_chain = b->next;
  136. /* Find bookmark preceding "marked" one, so we can unlink. */
  137. if (b)
  138. {
  139. ALL_BOOKMARKS (b1)
  140. if (b1->next == b)
  141. {
  142. /* Found designated bookmark. Unlink and delete. */
  143. b1->next = b->next;
  144. break;
  145. }
  146. xfree (b->opaque_data);
  147. delete b;
  148. return 1; /* success */
  149. }
  150. return 0; /* failure */
  151. }
  152. static void
  153. delete_all_bookmarks (void)
  154. {
  155. struct bookmark *b, *b1;
  156. ALL_BOOKMARKS_SAFE (b, b1)
  157. {
  158. xfree (b->opaque_data);
  159. xfree (b);
  160. }
  161. bookmark_chain = NULL;
  162. }
  163. static void
  164. delete_bookmark_command (const char *args, int from_tty)
  165. {
  166. if (bookmark_chain == NULL)
  167. {
  168. warning (_("No bookmarks."));
  169. return;
  170. }
  171. if (args == NULL || args[0] == '\0')
  172. {
  173. if (from_tty && !query (_("Delete all bookmarks? ")))
  174. return;
  175. delete_all_bookmarks ();
  176. return;
  177. }
  178. number_or_range_parser parser (args);
  179. while (!parser.finished ())
  180. {
  181. int num = parser.get_number ();
  182. if (!delete_one_bookmark (num))
  183. /* Not found. */
  184. warning (_("No bookmark #%d."), num);
  185. }
  186. }
  187. /* Implement "goto-bookmark" command. */
  188. static void
  189. goto_bookmark_command (const char *args, int from_tty)
  190. {
  191. struct bookmark *b;
  192. unsigned long num;
  193. const char *p = args;
  194. if (args == NULL || args[0] == '\0')
  195. error (_("Command requires an argument."));
  196. if (startswith (args, "start")
  197. || startswith (args, "begin")
  198. || startswith (args, "end"))
  199. {
  200. /* Special case. Give target opportunity to handle. */
  201. target_goto_bookmark ((gdb_byte *) args, from_tty);
  202. return;
  203. }
  204. if (args[0] == '\'' || args[0] == '\"')
  205. {
  206. /* Special case -- quoted string. Pass on to target. */
  207. if (args[strlen (args) - 1] != args[0])
  208. error (_("Unbalanced quotes: %s"), args);
  209. target_goto_bookmark ((gdb_byte *) args, from_tty);
  210. return;
  211. }
  212. /* General case. Bookmark identified by bookmark number. */
  213. num = get_number (&args);
  214. if (num == 0)
  215. error (_("goto-bookmark: invalid bookmark number '%s'."), p);
  216. ALL_BOOKMARKS (b)
  217. if (b->number == num)
  218. break;
  219. if (b)
  220. {
  221. /* Found. Send to target method. */
  222. target_goto_bookmark (b->opaque_data, from_tty);
  223. return;
  224. }
  225. /* Not found. */
  226. error (_("goto-bookmark: no bookmark found for '%s'."), p);
  227. }
  228. static int
  229. bookmark_1 (int bnum)
  230. {
  231. struct gdbarch *gdbarch = get_current_regcache ()->arch ();
  232. struct bookmark *b;
  233. int matched = 0;
  234. ALL_BOOKMARKS (b)
  235. {
  236. if (bnum == -1 || bnum == b->number)
  237. {
  238. gdb_printf (" %d %s '%s'\n",
  239. b->number,
  240. paddress (gdbarch, b->pc),
  241. b->opaque_data);
  242. matched++;
  243. }
  244. }
  245. if (bnum > 0 && matched == 0)
  246. gdb_printf ("No bookmark #%d\n", bnum);
  247. return matched;
  248. }
  249. /* Implement "info bookmarks" command. */
  250. static void
  251. info_bookmarks_command (const char *args, int from_tty)
  252. {
  253. if (!bookmark_chain)
  254. gdb_printf (_("No bookmarks.\n"));
  255. else if (args == NULL || *args == '\0')
  256. bookmark_1 (-1);
  257. else
  258. {
  259. number_or_range_parser parser (args);
  260. while (!parser.finished ())
  261. {
  262. int bnum = parser.get_number ();
  263. bookmark_1 (bnum);
  264. }
  265. }
  266. }
  267. void _initialize_reverse ();
  268. void
  269. _initialize_reverse ()
  270. {
  271. cmd_list_element *reverse_step_cmd
  272. = add_com ("reverse-step", class_run, reverse_step, _("\
  273. Step program backward until it reaches the beginning of another source line.\n\
  274. Argument N means do this N times (or till program stops for another reason)."));
  275. add_com_alias ("rs", reverse_step_cmd, class_run, 1);
  276. cmd_list_element *reverse_next_cmd
  277. = add_com ("reverse-next", class_run, reverse_next, _("\
  278. Step program backward, proceeding through subroutine calls.\n\
  279. Like the \"reverse-step\" command as long as subroutine calls do not happen;\n\
  280. when they do, the call is treated as one instruction.\n\
  281. Argument N means do this N times (or till program stops for another reason)."));
  282. add_com_alias ("rn", reverse_next_cmd, class_run, 1);
  283. cmd_list_element *reverse_stepi_cmd
  284. = add_com ("reverse-stepi", class_run, reverse_stepi, _("\
  285. Step backward exactly one instruction.\n\
  286. Argument N means do this N times (or till program stops for another reason)."));
  287. add_com_alias ("rsi", reverse_stepi_cmd, class_run, 0);
  288. cmd_list_element *reverse_nexti_cmd
  289. = add_com ("reverse-nexti", class_run, reverse_nexti, _("\
  290. Step backward one instruction, but proceed through called subroutines.\n\
  291. Argument N means do this N times (or till program stops for another reason)."));
  292. add_com_alias ("rni", reverse_nexti_cmd, class_run, 0);
  293. cmd_list_element *reverse_continue_cmd
  294. = add_com ("reverse-continue", class_run, reverse_continue, _("\
  295. Continue program being debugged but run it in reverse.\n\
  296. If proceeding from breakpoint, a number N may be used as an argument,\n\
  297. which means to set the ignore count of that breakpoint to N - 1 (so that\n\
  298. the breakpoint won't break until the Nth time it is reached)."));
  299. add_com_alias ("rc", reverse_continue_cmd, class_run, 0);
  300. add_com ("reverse-finish", class_run, reverse_finish, _("\
  301. Execute backward until just before selected stack frame is called."));
  302. add_com ("bookmark", class_bookmark, save_bookmark_command, _("\
  303. Set a bookmark in the program's execution history.\n\
  304. A bookmark represents a point in the execution history \n\
  305. that can be returned to at a later point in the debug session."));
  306. add_info ("bookmarks", info_bookmarks_command, _("\
  307. Status of user-settable bookmarks.\n\
  308. Bookmarks are user-settable markers representing a point in the \n\
  309. execution history that can be returned to later in the same debug \n\
  310. session."));
  311. add_cmd ("bookmark", class_bookmark, delete_bookmark_command, _("\
  312. Delete a bookmark from the bookmark list.\n\
  313. Argument is a bookmark number or numbers,\n\
  314. or no argument to delete all bookmarks."),
  315. &deletelist);
  316. add_com ("goto-bookmark", class_bookmark, goto_bookmark_command, _("\
  317. Go to an earlier-bookmarked point in the program's execution history.\n\
  318. Argument is the bookmark number of a bookmark saved earlier by using \n\
  319. the 'bookmark' command, or the special arguments:\n\
  320. start (beginning of recording)\n\
  321. end (end of recording)"));
  322. }