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