1 /* Process record and replay target for GDB, the GNU debugger.
3 Copyright (C) 2008-2013 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/>. */
22 #include "completer.h"
26 #include "common/common-utils.h"
28 /* This is the debug switch for process record. */
29 unsigned int record_debug
= 0;
31 struct cmd_list_element
*record_cmdlist
= NULL
;
32 struct cmd_list_element
*set_record_cmdlist
= NULL
;
33 struct cmd_list_element
*show_record_cmdlist
= NULL
;
34 struct cmd_list_element
*info_record_cmdlist
= NULL
;
36 #define DEBUG(msg, args...) \
38 fprintf_unfiltered (gdb_stdlog, "record: " msg "\n", ##args)
40 /* Find the record target in the target stack. */
42 static struct target_ops
*
43 find_record_target (void)
47 for (t
= current_target
.beneath
; t
!= NULL
; t
= t
->beneath
)
48 if (t
->to_stratum
== record_stratum
)
54 /* Check that recording is active. Throw an error, if it isn't. */
56 static struct target_ops
*
57 require_record_target (void)
61 t
= find_record_target ();
63 error (_("No record target is currently active.\n"
64 "Use one of the \"target record-<tab><tab>\" commands first."));
72 record_read_memory (struct gdbarch
*gdbarch
,
73 CORE_ADDR memaddr
, gdb_byte
*myaddr
,
76 int ret
= target_read_memory (memaddr
, myaddr
, len
);
79 DEBUG ("error reading memory at addr %s len = %ld.\n",
80 paddress (gdbarch
, memaddr
), (long) len
);
88 record_stop (struct target_ops
*t
)
90 DEBUG ("stop %s", t
->to_shortname
);
92 if (t
->to_stop_recording
!= NULL
)
93 t
->to_stop_recording ();
96 /* Unpush the record target. */
99 record_unpush (struct target_ops
*t
)
101 DEBUG ("unpush %s", t
->to_shortname
);
109 record_disconnect (struct target_ops
*t
, char *args
, int from_tty
)
111 gdb_assert (t
->to_stratum
== record_stratum
);
113 DEBUG ("disconnect %s", t
->to_shortname
);
118 target_disconnect (args
, from_tty
);
124 record_detach (struct target_ops
*t
, char *args
, int from_tty
)
126 gdb_assert (t
->to_stratum
== record_stratum
);
128 DEBUG ("detach %s", t
->to_shortname
);
133 target_detach (args
, from_tty
);
139 record_mourn_inferior (struct target_ops
*t
)
141 gdb_assert (t
->to_stratum
== record_stratum
);
143 DEBUG ("mourn inferior %s", t
->to_shortname
);
145 /* It is safer to not stop recording. Resources will be freed when
146 threads are discarded. */
149 target_mourn_inferior ();
155 record_kill (struct target_ops
*t
)
157 gdb_assert (t
->to_stratum
== record_stratum
);
159 DEBUG ("kill %s", t
->to_shortname
);
161 /* It is safer to not stop recording. Resources will be freed when
162 threads are discarded. */
168 /* Implement "show record debug" command. */
171 show_record_debug (struct ui_file
*file
, int from_tty
,
172 struct cmd_list_element
*c
, const char *value
)
174 fprintf_filtered (file
, _("Debugging of process record target is %s.\n"),
178 /* Alias for "target record". */
181 cmd_record_start (char *args
, int from_tty
)
183 execute_command ("target record-full", from_tty
);
186 /* Truncate the record log from the present point
187 of replay until the end. */
190 cmd_record_delete (char *args
, int from_tty
)
192 require_record_target ();
194 if (!target_record_is_replaying ())
196 printf_unfiltered (_("Already at end of record list.\n"));
200 if (!target_supports_delete_record ())
202 printf_unfiltered (_("The current record target does not support "
203 "this operation.\n"));
207 if (!from_tty
|| query (_("Delete the log from this point forward "
208 "and begin to record the running message "
210 target_delete_record ();
213 /* Implement the "stoprecord" or "record stop" command. */
216 cmd_record_stop (char *args
, int from_tty
)
218 struct target_ops
*t
;
220 t
= require_record_target ();
225 printf_unfiltered (_("Process record is stopped and all execution "
226 "logs are deleted.\n"));
228 observer_notify_record_changed (current_inferior (), 0);
231 /* The "set record" command. */
234 set_record_command (char *args
, int from_tty
)
236 printf_unfiltered (_("\"set record\" must be followed "
237 "by an apporpriate subcommand.\n"));
238 help_list (set_record_cmdlist
, "set record ", all_commands
, gdb_stdout
);
241 /* The "show record" command. */
244 show_record_command (char *args
, int from_tty
)
246 cmd_show_list (show_record_cmdlist
, from_tty
, "");
249 /* The "info record" command. */
252 info_record_command (char *args
, int from_tty
)
254 struct target_ops
*t
;
256 t
= find_record_target ();
259 printf_filtered (_("No record target is currently active.\n"));
263 printf_filtered (_("Active record target: %s\n"), t
->to_shortname
);
264 if (t
->to_info_record
!= NULL
)
265 t
->to_info_record ();
268 /* The "record save" command. */
271 cmd_record_save (char *args
, int from_tty
)
273 char *recfilename
, recfilename_buffer
[40];
275 require_record_target ();
277 if (args
!= NULL
&& *args
!= 0)
281 /* Default recfile name is "gdb_record.PID". */
282 xsnprintf (recfilename_buffer
, sizeof (recfilename_buffer
),
283 "gdb_record.%d", PIDGET (inferior_ptid
));
284 recfilename
= recfilename_buffer
;
287 target_save_record (recfilename
);
290 /* "record goto" command. Argument is an instruction number,
291 as given by "info record".
293 Rewinds the recording (forward or backward) to the given instruction. */
296 cmd_record_goto (char *arg
, int from_tty
)
298 require_record_target ();
300 if (arg
== NULL
|| *arg
== '\0')
301 error (_("Command requires an argument (insn number to go to)."));
303 if (strncmp (arg
, "start", strlen ("start")) == 0
304 || strncmp (arg
, "begin", strlen ("begin")) == 0)
305 target_goto_record_begin ();
306 else if (strncmp (arg
, "end", strlen ("end")) == 0)
307 target_goto_record_end ();
312 insn
= parse_and_eval_long (arg
);
313 target_goto_record (insn
);
317 /* Provide a prototype to silence -Wmissing-prototypes. */
318 extern initialize_file_ftype _initialize_record
;
321 _initialize_record (void)
323 struct cmd_list_element
*c
;
325 add_setshow_zuinteger_cmd ("record", no_class
, &record_debug
,
326 _("Set debugging of record/replay feature."),
327 _("Show debugging of record/replay feature."),
328 _("When enabled, debugging output for "
329 "record/replay feature is displayed."),
330 NULL
, show_record_debug
, &setdebuglist
,
333 c
= add_prefix_cmd ("record", class_obscure
, cmd_record_start
,
334 _("Start recording."),
335 &record_cmdlist
, "record ", 0, &cmdlist
);
336 set_cmd_completer (c
, filename_completer
);
338 add_com_alias ("rec", "record", class_obscure
, 1);
339 add_prefix_cmd ("record", class_support
, set_record_command
,
340 _("Set record options"), &set_record_cmdlist
,
341 "set record ", 0, &setlist
);
342 add_alias_cmd ("rec", "record", class_obscure
, 1, &setlist
);
343 add_prefix_cmd ("record", class_support
, show_record_command
,
344 _("Show record options"), &show_record_cmdlist
,
345 "show record ", 0, &showlist
);
346 add_alias_cmd ("rec", "record", class_obscure
, 1, &showlist
);
347 add_prefix_cmd ("record", class_support
, info_record_command
,
348 _("Info record options"), &info_record_cmdlist
,
349 "info record ", 0, &infolist
);
350 add_alias_cmd ("rec", "record", class_obscure
, 1, &infolist
);
352 c
= add_cmd ("save", class_obscure
, cmd_record_save
,
353 _("Save the execution log to a file.\n\
354 Argument is optional filename.\n\
355 Default filename is 'gdb_record.<process_id>'."),
357 set_cmd_completer (c
, filename_completer
);
359 add_cmd ("delete", class_obscure
, cmd_record_delete
,
360 _("Delete the rest of execution log and start recording it anew."),
362 add_alias_cmd ("d", "delete", class_obscure
, 1, &record_cmdlist
);
363 add_alias_cmd ("del", "delete", class_obscure
, 1, &record_cmdlist
);
365 add_cmd ("stop", class_obscure
, cmd_record_stop
,
366 _("Stop the record/replay target."),
368 add_alias_cmd ("s", "stop", class_obscure
, 1, &record_cmdlist
);
370 add_cmd ("goto", class_obscure
, cmd_record_goto
, _("\
371 Restore the program to its state at instruction number N.\n\
372 Argument is instruction number, as shown by 'info record'."),