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 (int num
)
178 struct bookmark
*b1
, *b
;
180 /* Find bookmark with corresponding number. */
182 if (b
->number
== num
)
185 /* Special case, first item in list. */
186 if (b
== bookmark_chain
)
187 bookmark_chain
= b
->next
;
189 /* Find bookmark preceeding "marked" one, so we can unlink. */
195 /* Found designated bookmark. Unlink and delete. */
199 xfree (b
->opaque_data
);
201 return 1; /* success */
203 return 0; /* failure */
207 delete_all_bookmarks (void)
209 struct bookmark
*b
, *b1
;
211 ALL_BOOKMARKS_SAFE (b
, b1
)
213 xfree (b
->opaque_data
);
216 bookmark_chain
= NULL
;
220 delete_bookmark_command (char *args
, int from_tty
)
225 if (bookmark_chain
== NULL
)
227 warning (_("No bookmarks."));
231 if (args
== NULL
|| args
[0] == '\0')
233 if (from_tty
&& !query (_("Delete all bookmarks? ")))
235 delete_all_bookmarks ();
239 while (args
!= NULL
&& *args
!= '\0')
241 num
= get_number_or_range (&args
);
242 if (!delete_one_bookmark (num
))
244 warning (_("No bookmark #%d."), num
);
248 /* Implement "goto-bookmark" command. */
251 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 (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 (args
, from_tty
);
277 /* General case. Bookmark identified by bookmark number. */
278 num
= get_number (&args
);
280 if (b
->number
== num
)
285 /* Found. Send to target method. */
286 target_goto_bookmark (b
->opaque_data
, from_tty
);
290 error (_("goto-bookmark: no bookmark found for '%s'."), args
);
294 bookmark_1 (int bnum
)
296 struct gdbarch
*gdbarch
= get_regcache_arch (get_current_regcache ());
302 if (bnum
== -1 || bnum
== b
->number
)
304 printf_filtered (" %d %s '%s'\n",
306 paddress (gdbarch
, b
->pc
),
312 if (bnum
> 0 && matched
== 0)
313 printf_filtered ("No bookmark #%d\n", bnum
);
318 /* Implement "info bookmarks" command. */
321 bookmarks_info (char *args
, int from_tty
)
326 printf_filtered (_("No bookmarks.\n"));
327 else if (args
== NULL
|| *args
== '\0')
330 while (args
!= NULL
&& *args
!= '\0')
332 bnum
= get_number_or_range (&args
);
338 /* Provide a prototype to silence -Wmissing-prototypes. */
339 extern initialize_file_ftype _initialize_reverse
;
342 _initialize_reverse (void)
344 add_com ("reverse-step", class_run
, reverse_step
, _("\
345 Step program backward until it reaches the beginning of another source line.\n\
346 Argument N means do this N times (or till program stops for another reason).")
348 add_com_alias ("rs", "reverse-step", class_alias
, 1);
350 add_com ("reverse-next", class_run
, reverse_next
, _("\
351 Step program backward, proceeding through subroutine calls.\n\
352 Like the \"reverse-step\" command as long as subroutine calls do not happen;\n\
353 when they do, the call is treated as one instruction.\n\
354 Argument N means do this N times (or till program stops for another reason).")
356 add_com_alias ("rn", "reverse-next", class_alias
, 1);
358 add_com ("reverse-stepi", class_run
, reverse_stepi
, _("\
359 Step backward exactly one instruction.\n\
360 Argument N means do this N times (or till program stops for another reason).")
362 add_com_alias ("rsi", "reverse-stepi", class_alias
, 0);
364 add_com ("reverse-nexti", class_run
, reverse_nexti
, _("\
365 Step backward one instruction, but proceed through called subroutines.\n\
366 Argument N means do this N times (or till program stops for another reason).")
368 add_com_alias ("rni", "reverse-nexti", class_alias
, 0);
370 add_com ("reverse-continue", class_run
, reverse_continue
, _("\
371 Continue program being debugged but run it in reverse.\n\
372 If proceeding from breakpoint, a number N may be used as an argument,\n\
373 which means to set the ignore count of that breakpoint to N - 1 (so that\n\
374 the breakpoint won't break until the Nth time it is reached)."));
375 add_com_alias ("rc", "reverse-continue", class_alias
, 0);
377 add_com ("reverse-finish", class_run
, reverse_finish
, _("\
378 Execute backward until just before selected stack frame is called."));
380 add_com ("bookmark", class_bookmark
, save_bookmark_command
, _("\
381 Set a bookmark in the program's execution history.\n\
382 A bookmark represents a point in the execution history \n\
383 that can be returned to at a later point in the debug session."));
384 add_info ("bookmarks", bookmarks_info
, _("\
385 Status of user-settable bookmarks.\n\
386 Bookmarks are user-settable markers representing a point in the \n\
387 execution history that can be returned to later in the same debug \n\
389 add_cmd ("bookmark", class_bookmark
, delete_bookmark_command
, _("\
390 Delete a bookmark from the bookmark list.\n\
391 Argument is a bookmark number or numbers,\n\
392 or no argument to delete all bookmarks.\n"),
394 add_com ("goto-bookmark", class_bookmark
, goto_bookmark_command
, _("\
395 Go to an earlier-bookmarked point in the program's execution history.\n\
396 Argument is the bookmark number of a bookmark saved earlier by using \n\
397 the 'bookmark' command, or the special arguments:\n\
398 start (beginning of recording)\n\
399 end (end of recording)\n"));