1 /* Reverse execution and reverse debugging.
3 Copyright (C) 2006-2014 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "cli/cli-cmds.h"
25 #include "cli/cli-decode.h"
26 #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_REVERSE)
54 error (_("Already in reverse mode. Use '%s' or 'set exec-dir forward'."),
57 if (!target_can_execute_reverse)
58 error (_("Target %s does not support this command."), target_shortname);
60 reverse_command = xstrprintf ("%s %s", cmd, args ? args : "");
61 old_chain = make_cleanup (exec_direction_default, NULL);
62 make_cleanup (xfree, reverse_command);
63 execution_direction = EXEC_REVERSE;
64 execute_command (reverse_command, from_tty);
65 do_cleanups (old_chain);
69 reverse_step (char *args, int from_tty)
71 exec_reverse_once ("step", args, from_tty);
75 reverse_stepi (char *args, int from_tty)
77 exec_reverse_once ("stepi", args, from_tty);
81 reverse_next (char *args, int from_tty)
83 exec_reverse_once ("next", args, from_tty);
87 reverse_nexti (char *args, int from_tty)
89 exec_reverse_once ("nexti", args, from_tty);
93 reverse_continue (char *args, int from_tty)
95 exec_reverse_once ("continue", args, from_tty);
99 reverse_finish (char *args, int from_tty)
101 exec_reverse_once ("finish", args, from_tty);
104 /* Data structures for a bookmark list. */
107 struct bookmark *next;
110 struct symtab_and_line sal;
111 gdb_byte *opaque_data;
114 static struct bookmark *bookmark_chain;
115 static int bookmark_count;
117 #define ALL_BOOKMARKS(B) for ((B) = bookmark_chain; (B); (B) = (B)->next)
119 #define ALL_BOOKMARKS_SAFE(B,TMP) \
120 for ((B) = bookmark_chain; \
121 (B) ? ((TMP) = (B)->next, 1) : 0; \
124 /* save_bookmark_command -- implement "bookmark" command.
125 Call target method to get a bookmark identifier.
126 Insert bookmark identifier into list.
128 Identifier will be a malloc string (gdb_byte *).
129 Up to us to free it as required. */
132 save_bookmark_command (char *args, int from_tty)
134 /* Get target's idea of a bookmark. */
135 gdb_byte *bookmark_id = target_get_bookmark (args, from_tty);
136 struct bookmark *b, *b1;
137 struct gdbarch *gdbarch = get_regcache_arch (get_current_regcache ());
139 /* CR should not cause another identical bookmark. */
142 if (bookmark_id == NULL)
143 error (_("target_get_bookmark failed."));
145 /* Set up a bookmark struct. */
146 b = xcalloc (1, sizeof (struct bookmark));
147 b->number = ++bookmark_count;
149 b->pc = regcache_read_pc (get_current_regcache ());
150 b->sal = find_pc_line (b->pc, 0);
151 b->sal.pspace = get_frame_program_space (get_current_frame ());
152 b->opaque_data = bookmark_id;
155 /* Add this bookmark to the end of the chain, so that a list
156 of bookmarks will come out in order of increasing numbers. */
167 printf_filtered (_("Saved bookmark %d at %s\n"), b->number,
168 paddress (gdbarch, b->sal.pc));
171 /* Implement "delete bookmark" command. */
174 delete_one_bookmark (int num)
176 struct bookmark *b1, *b;
178 /* Find bookmark with corresponding number. */
180 if (b->number == num)
183 /* Special case, first item in list. */
184 if (b == bookmark_chain)
185 bookmark_chain = b->next;
187 /* Find bookmark preceding "marked" one, so we can unlink. */
193 /* Found designated bookmark. Unlink and delete. */
197 xfree (b->opaque_data);
199 return 1; /* success */
201 return 0; /* failure */
205 delete_all_bookmarks (void)
207 struct bookmark *b, *b1;
209 ALL_BOOKMARKS_SAFE (b, b1)
211 xfree (b->opaque_data);
214 bookmark_chain = NULL;
218 delete_bookmark_command (char *args, int from_tty)
221 struct get_number_or_range_state state;
223 if (bookmark_chain == NULL)
225 warning (_("No bookmarks."));
229 if (args == NULL || args[0] == '\0')
231 if (from_tty && !query (_("Delete all bookmarks? ")))
233 delete_all_bookmarks ();
237 init_number_or_range (&state, args);
238 while (!state.finished)
240 num = get_number_or_range (&state);
241 if (!delete_one_bookmark (num))
243 warning (_("No bookmark #%d."), num);
247 /* Implement "goto-bookmark" command. */
250 goto_bookmark_command (char *args, int from_tty)
256 if (args == NULL || args[0] == '\0')
257 error (_("Command requires an argument."));
259 if (strncmp (args, "start", strlen ("start")) == 0
260 || strncmp (args, "begin", strlen ("begin")) == 0
261 || strncmp (args, "end", strlen ("end")) == 0)
263 /* Special case. Give target opportunity to handle. */
264 target_goto_bookmark ((gdb_byte *) args, from_tty);
268 if (args[0] == '\'' || args[0] == '\"')
270 /* Special case -- quoted string. Pass on to target. */
271 if (args[strlen (args) - 1] != args[0])
272 error (_("Unbalanced quotes: %s"), args);
273 target_goto_bookmark ((gdb_byte *) args, from_tty);
277 /* General case. Bookmark identified by bookmark number. */
278 num = get_number (&args);
281 error (_("goto-bookmark: invalid bookmark number '%s'."), p);
284 if (b->number == num)
289 /* Found. Send to target method. */
290 target_goto_bookmark (b->opaque_data, from_tty);
294 error (_("goto-bookmark: no bookmark found for '%s'."), p);
298 bookmark_1 (int bnum)
300 struct gdbarch *gdbarch = get_regcache_arch (get_current_regcache ());
306 if (bnum == -1 || bnum == b->number)
308 printf_filtered (" %d %s '%s'\n",
310 paddress (gdbarch, b->pc),
316 if (bnum > 0 && matched == 0)
317 printf_filtered ("No bookmark #%d\n", bnum);
322 /* Implement "info bookmarks" command. */
325 bookmarks_info (char *args, int from_tty)
330 printf_filtered (_("No bookmarks.\n"));
331 else if (args == NULL || *args == '\0')
335 struct get_number_or_range_state state;
337 init_number_or_range (&state, args);
338 while (!state.finished)
340 bnum = get_number_or_range (&state);
347 /* Provide a prototype to silence -Wmissing-prototypes. */
348 extern initialize_file_ftype _initialize_reverse;
351 _initialize_reverse (void)
353 add_com ("reverse-step", class_run, reverse_step, _("\
354 Step program backward until it reaches the beginning of another source line.\n\
355 Argument N means do this N times (or till program stops for another reason).")
357 add_com_alias ("rs", "reverse-step", class_alias, 1);
359 add_com ("reverse-next", class_run, reverse_next, _("\
360 Step program backward, proceeding through subroutine calls.\n\
361 Like the \"reverse-step\" command as long as subroutine calls do not happen;\n\
362 when they do, the call is treated as one instruction.\n\
363 Argument N means do this N times (or till program stops for another reason).")
365 add_com_alias ("rn", "reverse-next", class_alias, 1);
367 add_com ("reverse-stepi", class_run, reverse_stepi, _("\
368 Step backward exactly one instruction.\n\
369 Argument N means do this N times (or till program stops for another reason).")
371 add_com_alias ("rsi", "reverse-stepi", class_alias, 0);
373 add_com ("reverse-nexti", class_run, reverse_nexti, _("\
374 Step backward one instruction, but proceed through called subroutines.\n\
375 Argument N means do this N times (or till program stops for another reason).")
377 add_com_alias ("rni", "reverse-nexti", class_alias, 0);
379 add_com ("reverse-continue", class_run, reverse_continue, _("\
380 Continue program being debugged but run it in reverse.\n\
381 If proceeding from breakpoint, a number N may be used as an argument,\n\
382 which means to set the ignore count of that breakpoint to N - 1 (so that\n\
383 the breakpoint won't break until the Nth time it is reached)."));
384 add_com_alias ("rc", "reverse-continue", class_alias, 0);
386 add_com ("reverse-finish", class_run, reverse_finish, _("\
387 Execute backward until just before selected stack frame is called."));
389 add_com ("bookmark", class_bookmark, save_bookmark_command, _("\
390 Set a bookmark in the program's execution history.\n\
391 A bookmark represents a point in the execution history \n\
392 that can be returned to at a later point in the debug session."));
393 add_info ("bookmarks", bookmarks_info, _("\
394 Status of user-settable bookmarks.\n\
395 Bookmarks are user-settable markers representing a point in the \n\
396 execution history that can be returned to later in the same debug \n\
398 add_cmd ("bookmark", class_bookmark, delete_bookmark_command, _("\
399 Delete a bookmark from the bookmark list.\n\
400 Argument is a bookmark number or numbers,\n\
401 or no argument to delete all bookmarks.\n"),
403 add_com ("goto-bookmark", class_bookmark, goto_bookmark_command, _("\
404 Go to an earlier-bookmarked point in the program's execution history.\n\
405 Argument is the bookmark number of a bookmark saved earlier by using \n\
406 the 'bookmark' command, or the special arguments:\n\
407 start (beginning of recording)\n\
408 end (end of recording)\n"));