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