1 /* Reverse execution and reverse debugging.
3 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "gdb_string.h"
25 #include "cli/cli-cmds.h"
26 #include "cli/cli-decode.h"
31 reverse-step, reverse-next etc. */
34 exec_direction_default (void *notused)
36 /* Return execution direction to default state. */
37 execution_direction = EXEC_FORWARD;
40 /* exec_reverse_once -- accepts an arbitrary gdb command (string),
41 and executes it with exec-direction set to 'reverse'.
43 Used to implement reverse-next etc. commands. */
46 exec_reverse_once (char *cmd, char *args, int from_tty)
48 char *reverse_command;
49 enum exec_direction_kind dir = execution_direction;
50 struct cleanup *old_chain;
52 if (dir == EXEC_ERROR)
53 error (_("Target %s does not support this command."), target_shortname);
55 if (dir == EXEC_REVERSE)
56 error (_("Already in reverse mode. Use '%s' or 'set exec-dir forward'."),
59 if (!target_can_execute_reverse)
60 error (_("Target %s does not support this command."), target_shortname);
62 reverse_command = xstrprintf ("%s %s", cmd, args ? args : "");
63 old_chain = make_cleanup (exec_direction_default, NULL);
64 make_cleanup (xfree, reverse_command);
65 execution_direction = EXEC_REVERSE;
66 execute_command (reverse_command, from_tty);
67 do_cleanups (old_chain);
71 reverse_step (char *args, int from_tty)
73 exec_reverse_once ("step", args, from_tty);
77 reverse_stepi (char *args, int from_tty)
79 exec_reverse_once ("stepi", args, from_tty);
83 reverse_next (char *args, int from_tty)
85 exec_reverse_once ("next", args, from_tty);
89 reverse_nexti (char *args, int from_tty)
91 exec_reverse_once ("nexti", args, from_tty);
95 reverse_continue (char *args, int from_tty)
97 exec_reverse_once ("continue", args, from_tty);
101 reverse_finish (char *args, int from_tty)
103 exec_reverse_once ("finish", args, from_tty);
106 /* Data structures for a bookmark list. */
109 struct bookmark *next;
112 struct symtab_and_line sal;
113 gdb_byte *opaque_data;
116 static struct bookmark *bookmark_chain;
117 static int bookmark_count;
119 #define ALL_BOOKMARKS(B) for ((B) = bookmark_chain; (B); (B) = (B)->next)
121 #define ALL_BOOKMARKS_SAFE(B,TMP) \
122 for ((B) = bookmark_chain; \
123 (B) ? ((TMP) = (B)->next, 1) : 0; \
126 /* save_bookmark_command -- implement "bookmark" command.
127 Call target method to get a bookmark identifier.
128 Insert bookmark identifier into list.
130 Identifier will be a malloc string (gdb_byte *).
131 Up to us to free it as required. */
134 save_bookmark_command (char *args, int from_tty)
136 /* Get target's idea of a bookmark. */
137 gdb_byte *bookmark_id = target_get_bookmark (args, from_tty);
138 struct bookmark *b, *b1;
139 struct gdbarch *gdbarch = get_regcache_arch (get_current_regcache ());
141 /* CR should not cause another identical bookmark. */
144 if (bookmark_id == NULL)
145 error (_("target_get_bookmark failed."));
147 /* Set up a bookmark struct. */
148 b = xcalloc (1, sizeof (struct bookmark));
149 b->number = ++bookmark_count;
151 b->pc = regcache_read_pc (get_current_regcache ());
152 b->sal = find_pc_line (b->pc, 0);
153 b->sal.pspace = get_frame_program_space (get_current_frame ());
154 b->opaque_data = bookmark_id;
157 /* Add this bookmark to the end of the chain, so that a list
158 of bookmarks will come out in order of increasing numbers. */
169 printf_filtered (_("Saved bookmark %d at %s\n"), b->number,
170 paddress (gdbarch, b->sal.pc));
173 /* Implement "delete bookmark" command. */
176 delete_one_bookmark (struct bookmark *b)
180 /* Special case, first item in list. */
181 if (b == bookmark_chain)
182 bookmark_chain = b->next;
184 /* Find bookmark preceeding "marked" one, so we can unlink. */
190 /* Found designated bookmark. Unlink and delete. */
194 xfree (b->opaque_data);
196 return 1; /* success */
198 return 0; /* failure */
202 delete_all_bookmarks (void)
204 struct bookmark *b, *b1;
206 ALL_BOOKMARKS_SAFE (b, b1)
208 xfree (b->opaque_data);
211 bookmark_chain = NULL;
215 delete_bookmark_command (char *args, int from_tty)
220 if (bookmark_chain == NULL)
222 warning (_("No bookmarks."));
226 if (args == NULL || args[0] == '\0')
228 if (from_tty && !query (_("Delete all bookmarks? ")))
230 delete_all_bookmarks ();
234 num = strtoul (args, NULL, 0);
235 /* Find bookmark with corresponding number. */
237 if (b->number == num)
240 if (!delete_one_bookmark (b))
242 error (_("delete bookmark: no bookmark found for '%s'."), args);
245 /* Implement "goto-bookmark" command. */
248 goto_bookmark_command (char *args, int from_tty)
253 if (args == NULL || args[0] == '\0')
254 error (_("Command requires an argument."));
256 if (strncmp (args, "start", strlen ("start")) == 0
257 || strncmp (args, "begin", strlen ("begin")) == 0
258 || strncmp (args, "end", strlen ("end")) == 0)
260 /* Special case. Give target opportunity to handle. */
261 target_goto_bookmark (args, from_tty);
265 if (args[0] == '\'' || args[0] == '\"')
267 /* Special case -- quoted string. Pass on to target. */
268 if (args[strlen (args) - 1] != args[0])
269 error (_("Unbalanced quotes: %s"), args);
270 target_goto_bookmark (args, from_tty);
274 /* General case. Bookmark identified by bookmark number. */
275 num = strtoul (args, NULL, 0);
277 if (b->number == num)
282 /* Found. Send to target method. */
283 target_goto_bookmark (b->opaque_data, from_tty);
287 error (_("goto-bookmark: no bookmark found for '%s'."), args);
290 /* Implement "info bookmarks" command. */
293 bookmarks_info (char *args, int from_tty)
297 struct gdbarch *gdbarch;
300 bnum = parse_and_eval_long (args);
304 printf_filtered (_("No bookmarks.\n"));
308 gdbarch = get_regcache_arch (get_current_regcache ());
309 printf_filtered (_("Bookmark Address Opaque\n"));
310 printf_filtered (_(" ID Data\n"));
313 printf_filtered (" %d %s '%s'\n",
315 paddress (gdbarch, b->pc),
320 /* Provide a prototype to silence -Wmissing-prototypes. */
321 extern initialize_file_ftype _initialize_reverse;
324 _initialize_reverse (void)
326 add_com ("reverse-step", class_run, reverse_step, _("\
327 Step program backward until it reaches the beginning of another source line.\n\
328 Argument N means do this N times (or till program stops for another reason).")
330 add_com_alias ("rs", "reverse-step", class_alias, 1);
332 add_com ("reverse-next", class_run, reverse_next, _("\
333 Step program backward, proceeding through subroutine calls.\n\
334 Like the \"reverse-step\" command as long as subroutine calls do not happen;\n\
335 when they do, the call is treated as one instruction.\n\
336 Argument N means do this N times (or till program stops for another reason).")
338 add_com_alias ("rn", "reverse-next", class_alias, 1);
340 add_com ("reverse-stepi", class_run, reverse_stepi, _("\
341 Step backward exactly one instruction.\n\
342 Argument N means do this N times (or till program stops for another reason).")
344 add_com_alias ("rsi", "reverse-stepi", class_alias, 0);
346 add_com ("reverse-nexti", class_run, reverse_nexti, _("\
347 Step backward one instruction, but proceed through called subroutines.\n\
348 Argument N means do this N times (or till program stops for another reason).")
350 add_com_alias ("rni", "reverse-nexti", class_alias, 0);
352 add_com ("reverse-continue", class_run, reverse_continue, _("\
353 Continue program being debugged but run it in reverse.\n\
354 If proceeding from breakpoint, a number N may be used as an argument,\n\
355 which means to set the ignore count of that breakpoint to N - 1 (so that\n\
356 the breakpoint won't break until the Nth time it is reached)."));
357 add_com_alias ("rc", "reverse-continue", class_alias, 0);
359 add_com ("reverse-finish", class_run, reverse_finish, _("\
360 Execute backward until just before selected stack frame is called."));
362 add_com ("bookmark", class_bookmark, save_bookmark_command, _("\
363 Set a bookmark in the program's execution history.\n\
364 A bookmark represents a point in the execution history \n\
365 that can be returned to at a later point in the debug session."));
366 add_info ("bookmarks", bookmarks_info, _("\
367 Status of user-settable bookmarks.\n\
368 Bookmarks are user-settable markers representing a point in the \n\
369 execution history that can be returned to later in the same debug \n\
371 add_cmd ("bookmark", class_bookmark, delete_bookmark_command, _("\
372 Delete a bookmark from the bookmark list.\n\
373 Argument is a bookmark number, or no argument to delete all bookmarks.\n"),
375 add_com ("goto-bookmark", class_bookmark, goto_bookmark_command, _("\
376 Go to an earlier-bookmarked point in the program's execution history.\n\
377 Argument is the bookmark number of a bookmark saved earlier by using \n\
378 the 'bookmark' command, or the special arguments:\n\
379 start (beginning of recording)\n\
380 end (end of recording)\n"));