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