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"
27 #include "cli/cli-utils.h"
32 reverse-step, reverse-next etc. */
35 exec_direction_default (void *notused)
37 /* Return execution direction to default state. */
38 execution_direction = EXEC_FORWARD;
41 /* exec_reverse_once -- accepts an arbitrary gdb command (string),
42 and executes it with exec-direction set to 'reverse'.
44 Used to implement reverse-next etc. commands. */
47 exec_reverse_once (char *cmd, char *args, int from_tty)
49 char *reverse_command;
50 enum exec_direction_kind dir = execution_direction;
51 struct cleanup *old_chain;
53 if (dir == EXEC_ERROR)
54 error (_("Target %s does not support this command."), target_shortname);
56 if (dir == EXEC_REVERSE)
57 error (_("Already in reverse mode. Use '%s' or 'set exec-dir forward'."),
60 if (!target_can_execute_reverse)
61 error (_("Target %s does not support this command."), target_shortname);
63 reverse_command = xstrprintf ("%s %s", cmd, args ? args : "");
64 old_chain = make_cleanup (exec_direction_default, NULL);
65 make_cleanup (xfree, reverse_command);
66 execution_direction = EXEC_REVERSE;
67 execute_command (reverse_command, from_tty);
68 do_cleanups (old_chain);
72 reverse_step (char *args, int from_tty)
74 exec_reverse_once ("step", args, from_tty);
78 reverse_stepi (char *args, int from_tty)
80 exec_reverse_once ("stepi", args, from_tty);
84 reverse_next (char *args, int from_tty)
86 exec_reverse_once ("next", args, from_tty);
90 reverse_nexti (char *args, int from_tty)
92 exec_reverse_once ("nexti", args, from_tty);
96 reverse_continue (char *args, int from_tty)
98 exec_reverse_once ("continue", args, from_tty);
102 reverse_finish (char *args, int from_tty)
104 exec_reverse_once ("finish", args, from_tty);
107 /* Data structures for a bookmark list. */
110 struct bookmark *next;
113 struct symtab_and_line sal;
114 gdb_byte *opaque_data;
117 static struct bookmark *bookmark_chain;
118 static int bookmark_count;
120 #define ALL_BOOKMARKS(B) for ((B) = bookmark_chain; (B); (B) = (B)->next)
122 #define ALL_BOOKMARKS_SAFE(B,TMP) \
123 for ((B) = bookmark_chain; \
124 (B) ? ((TMP) = (B)->next, 1) : 0; \
127 /* save_bookmark_command -- implement "bookmark" command.
128 Call target method to get a bookmark identifier.
129 Insert bookmark identifier into list.
131 Identifier will be a malloc string (gdb_byte *).
132 Up to us to free it as required. */
135 save_bookmark_command (char *args, int from_tty)
137 /* Get target's idea of a bookmark. */
138 gdb_byte *bookmark_id = target_get_bookmark (args, from_tty);
139 struct bookmark *b, *b1;
140 struct gdbarch *gdbarch = get_regcache_arch (get_current_regcache ());
142 /* CR should not cause another identical bookmark. */
145 if (bookmark_id == NULL)
146 error (_("target_get_bookmark failed."));
148 /* Set up a bookmark struct. */
149 b = xcalloc (1, sizeof (struct bookmark));
150 b->number = ++bookmark_count;
152 b->pc = regcache_read_pc (get_current_regcache ());
153 b->sal = find_pc_line (b->pc, 0);
154 b->sal.pspace = get_frame_program_space (get_current_frame ());
155 b->opaque_data = bookmark_id;
158 /* Add this bookmark to the end of the chain, so that a list
159 of bookmarks will come out in order of increasing numbers. */
170 printf_filtered (_("Saved bookmark %d at %s\n"), b->number,
171 paddress (gdbarch, b->sal.pc));
174 /* Implement "delete bookmark" command. */
177 delete_one_bookmark (int num)
179 struct bookmark *b1, *b;
181 /* Find bookmark with corresponding number. */
183 if (b->number == num)
186 /* Special case, first item in list. */
187 if (b == bookmark_chain)
188 bookmark_chain = b->next;
190 /* Find bookmark preceeding "marked" one, so we can unlink. */
196 /* Found designated bookmark. Unlink and delete. */
200 xfree (b->opaque_data);
202 return 1; /* success */
204 return 0; /* failure */
208 delete_all_bookmarks (void)
210 struct bookmark *b, *b1;
212 ALL_BOOKMARKS_SAFE (b, b1)
214 xfree (b->opaque_data);
217 bookmark_chain = NULL;
221 delete_bookmark_command (char *args, int from_tty)
225 struct get_number_or_range_state state;
227 if (bookmark_chain == NULL)
229 warning (_("No bookmarks."));
233 if (args == NULL || args[0] == '\0')
235 if (from_tty && !query (_("Delete all bookmarks? ")))
237 delete_all_bookmarks ();
241 init_number_or_range (&state, args);
242 while (!state.finished)
244 num = get_number_or_range (&state);
245 if (!delete_one_bookmark (num))
247 warning (_("No bookmark #%d."), num);
251 /* Implement "goto-bookmark" command. */
254 goto_bookmark_command (char *args, int from_tty)
259 if (args == NULL || args[0] == '\0')
260 error (_("Command requires an argument."));
262 if (strncmp (args, "start", strlen ("start")) == 0
263 || strncmp (args, "begin", strlen ("begin")) == 0
264 || strncmp (args, "end", strlen ("end")) == 0)
266 /* Special case. Give target opportunity to handle. */
267 target_goto_bookmark (args, from_tty);
271 if (args[0] == '\'' || args[0] == '\"')
273 /* Special case -- quoted string. Pass on to target. */
274 if (args[strlen (args) - 1] != args[0])
275 error (_("Unbalanced quotes: %s"), args);
276 target_goto_bookmark (args, from_tty);
280 /* General case. Bookmark identified by bookmark number. */
281 num = get_number (&args);
283 if (b->number == num)
288 /* Found. Send to target method. */
289 target_goto_bookmark (b->opaque_data, from_tty);
293 error (_("goto-bookmark: no bookmark found for '%s'."), args);
297 bookmark_1 (int bnum)
299 struct gdbarch *gdbarch = get_regcache_arch (get_current_regcache ());
305 if (bnum == -1 || bnum == b->number)
307 printf_filtered (" %d %s '%s'\n",
309 paddress (gdbarch, b->pc),
315 if (bnum > 0 && matched == 0)
316 printf_filtered ("No bookmark #%d\n", bnum);
321 /* Implement "info bookmarks" command. */
324 bookmarks_info (char *args, int from_tty)
329 printf_filtered (_("No bookmarks.\n"));
330 else if (args == NULL || *args == '\0')
334 struct get_number_or_range_state state;
336 init_number_or_range (&state, args);
337 while (!state.finished)
339 bnum = get_number_or_range (&state);
346 /* Provide a prototype to silence -Wmissing-prototypes. */
347 extern initialize_file_ftype _initialize_reverse;
350 _initialize_reverse (void)
352 add_com ("reverse-step", class_run, reverse_step, _("\
353 Step program backward until it reaches the beginning of another source line.\n\
354 Argument N means do this N times (or till program stops for another reason).")
356 add_com_alias ("rs", "reverse-step", class_alias, 1);
358 add_com ("reverse-next", class_run, reverse_next, _("\
359 Step program backward, proceeding through subroutine calls.\n\
360 Like the \"reverse-step\" command as long as subroutine calls do not happen;\n\
361 when they do, the call is treated as one instruction.\n\
362 Argument N means do this N times (or till program stops for another reason).")
364 add_com_alias ("rn", "reverse-next", class_alias, 1);
366 add_com ("reverse-stepi", class_run, reverse_stepi, _("\
367 Step backward exactly one instruction.\n\
368 Argument N means do this N times (or till program stops for another reason).")
370 add_com_alias ("rsi", "reverse-stepi", class_alias, 0);
372 add_com ("reverse-nexti", class_run, reverse_nexti, _("\
373 Step backward one instruction, but proceed through called subroutines.\n\
374 Argument N means do this N times (or till program stops for another reason).")
376 add_com_alias ("rni", "reverse-nexti", class_alias, 0);
378 add_com ("reverse-continue", class_run, reverse_continue, _("\
379 Continue program being debugged but run it in reverse.\n\
380 If proceeding from breakpoint, a number N may be used as an argument,\n\
381 which means to set the ignore count of that breakpoint to N - 1 (so that\n\
382 the breakpoint won't break until the Nth time it is reached)."));
383 add_com_alias ("rc", "reverse-continue", class_alias, 0);
385 add_com ("reverse-finish", class_run, reverse_finish, _("\
386 Execute backward until just before selected stack frame is called."));
388 add_com ("bookmark", class_bookmark, save_bookmark_command, _("\
389 Set a bookmark in the program's execution history.\n\
390 A bookmark represents a point in the execution history \n\
391 that can be returned to at a later point in the debug session."));
392 add_info ("bookmarks", bookmarks_info, _("\
393 Status of user-settable bookmarks.\n\
394 Bookmarks are user-settable markers representing a point in the \n\
395 execution history that can be returned to later in the same debug \n\
397 add_cmd ("bookmark", class_bookmark, delete_bookmark_command, _("\
398 Delete a bookmark from the bookmark list.\n\
399 Argument is a bookmark number or numbers,\n\
400 or no argument to delete all bookmarks.\n"),
402 add_com ("goto-bookmark", class_bookmark, goto_bookmark_command, _("\
403 Go to an earlier-bookmarked point in the program's execution history.\n\
404 Argument is the bookmark number of a bookmark saved earlier by using \n\
405 the 'bookmark' command, or the special arguments:\n\
406 start (beginning of recording)\n\
407 end (end of recording)\n"));