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"
27 #include "cli/cli-utils.h"
32 /* This is the debug switch for process record. */
33 unsigned int record_debug
= 0;
35 /* The number of instructions to print in "record instruction-history". */
36 static unsigned int record_insn_history_size
= 10;
38 struct cmd_list_element
*record_cmdlist
= NULL
;
39 struct cmd_list_element
*set_record_cmdlist
= NULL
;
40 struct cmd_list_element
*show_record_cmdlist
= NULL
;
41 struct cmd_list_element
*info_record_cmdlist
= NULL
;
43 #define DEBUG(msg, args...) \
45 fprintf_unfiltered (gdb_stdlog, "record: " msg "\n", ##args)
47 /* Find the record target in the target stack. */
49 static struct target_ops
*
50 find_record_target (void)
54 for (t
= current_target
.beneath
; t
!= NULL
; t
= t
->beneath
)
55 if (t
->to_stratum
== record_stratum
)
61 /* Check that recording is active. Throw an error, if it isn't. */
63 static struct target_ops
*
64 require_record_target (void)
68 t
= find_record_target ();
70 error (_("No record target is currently active.\n"
71 "Use one of the \"target record-<tab><tab>\" commands first."));
79 record_read_memory (struct gdbarch
*gdbarch
,
80 CORE_ADDR memaddr
, gdb_byte
*myaddr
,
83 int ret
= target_read_memory (memaddr
, myaddr
, len
);
86 DEBUG ("error reading memory at addr %s len = %ld.\n",
87 paddress (gdbarch
, memaddr
), (long) len
);
95 record_stop (struct target_ops
*t
)
97 DEBUG ("stop %s", t
->to_shortname
);
99 if (t
->to_stop_recording
!= NULL
)
100 t
->to_stop_recording ();
103 /* Unpush the record target. */
106 record_unpush (struct target_ops
*t
)
108 DEBUG ("unpush %s", t
->to_shortname
);
116 record_disconnect (struct target_ops
*t
, char *args
, int from_tty
)
118 gdb_assert (t
->to_stratum
== record_stratum
);
120 DEBUG ("disconnect %s", t
->to_shortname
);
125 target_disconnect (args
, from_tty
);
131 record_detach (struct target_ops
*t
, char *args
, int from_tty
)
133 gdb_assert (t
->to_stratum
== record_stratum
);
135 DEBUG ("detach %s", t
->to_shortname
);
140 target_detach (args
, from_tty
);
146 record_mourn_inferior (struct target_ops
*t
)
148 gdb_assert (t
->to_stratum
== record_stratum
);
150 DEBUG ("mourn inferior %s", t
->to_shortname
);
152 /* It is safer to not stop recording. Resources will be freed when
153 threads are discarded. */
156 target_mourn_inferior ();
162 record_kill (struct target_ops
*t
)
164 gdb_assert (t
->to_stratum
== record_stratum
);
166 DEBUG ("kill %s", t
->to_shortname
);
168 /* It is safer to not stop recording. Resources will be freed when
169 threads are discarded. */
175 /* Implement "show record debug" command. */
178 show_record_debug (struct ui_file
*file
, int from_tty
,
179 struct cmd_list_element
*c
, const char *value
)
181 fprintf_filtered (file
, _("Debugging of process record target is %s.\n"),
185 /* Alias for "target record". */
188 cmd_record_start (char *args
, int from_tty
)
190 execute_command ("target record-full", from_tty
);
193 /* Truncate the record log from the present point
194 of replay until the end. */
197 cmd_record_delete (char *args
, int from_tty
)
199 require_record_target ();
201 if (!target_record_is_replaying ())
203 printf_unfiltered (_("Already at end of record list.\n"));
207 if (!target_supports_delete_record ())
209 printf_unfiltered (_("The current record target does not support "
210 "this operation.\n"));
214 if (!from_tty
|| query (_("Delete the log from this point forward "
215 "and begin to record the running message "
217 target_delete_record ();
220 /* Implement the "stoprecord" or "record stop" command. */
223 cmd_record_stop (char *args
, int from_tty
)
225 struct target_ops
*t
;
227 t
= require_record_target ();
232 printf_unfiltered (_("Process record is stopped and all execution "
233 "logs are deleted.\n"));
235 observer_notify_record_changed (current_inferior (), 0);
238 /* The "set record" command. */
241 set_record_command (char *args
, int from_tty
)
243 printf_unfiltered (_("\"set record\" must be followed "
244 "by an apporpriate subcommand.\n"));
245 help_list (set_record_cmdlist
, "set record ", all_commands
, gdb_stdout
);
248 /* The "show record" command. */
251 show_record_command (char *args
, int from_tty
)
253 cmd_show_list (show_record_cmdlist
, from_tty
, "");
256 /* The "info record" command. */
259 info_record_command (char *args
, int from_tty
)
261 struct target_ops
*t
;
263 t
= find_record_target ();
266 printf_filtered (_("No record target is currently active.\n"));
270 printf_filtered (_("Active record target: %s\n"), t
->to_shortname
);
271 if (t
->to_info_record
!= NULL
)
272 t
->to_info_record ();
275 /* The "record save" command. */
278 cmd_record_save (char *args
, int from_tty
)
280 char *recfilename
, recfilename_buffer
[40];
282 require_record_target ();
284 if (args
!= NULL
&& *args
!= 0)
288 /* Default recfile name is "gdb_record.PID". */
289 xsnprintf (recfilename_buffer
, sizeof (recfilename_buffer
),
290 "gdb_record.%d", PIDGET (inferior_ptid
));
291 recfilename
= recfilename_buffer
;
294 target_save_record (recfilename
);
297 /* "record goto" command. Argument is an instruction number,
298 as given by "info record".
300 Rewinds the recording (forward or backward) to the given instruction. */
303 cmd_record_goto (char *arg
, int from_tty
)
305 require_record_target ();
307 if (arg
== NULL
|| *arg
== '\0')
308 error (_("Command requires an argument (insn number to go to)."));
310 if (strncmp (arg
, "start", strlen ("start")) == 0
311 || strncmp (arg
, "begin", strlen ("begin")) == 0)
312 target_goto_record_begin ();
313 else if (strncmp (arg
, "end", strlen ("end")) == 0)
314 target_goto_record_end ();
319 insn
= parse_and_eval_long (arg
);
320 target_goto_record (insn
);
324 /* Read an instruction number from an argument string. */
327 get_insn_number (char **arg
)
330 const char *begin
, *end
, *pos
;
333 pos
= skip_spaces_const (begin
);
336 error (_("Expected positive number, got: %s."), pos
);
338 number
= strtoulst (pos
, &end
, 10);
340 *arg
+= (end
- begin
);
345 /* Read a context size from an argument string. */
348 get_context_size (char **arg
)
353 pos
= skip_spaces (*arg
);
356 error (_("Expected positive number, got: %s."), pos
);
358 return strtol (pos
, arg
, 10);
361 /* Complain about junk at the end of an argument string. */
367 error (_("Junk after argument: %s."), arg
);
370 /* Read instruction-history modifiers from an argument string. */
373 get_insn_history_modifiers (char **arg
)
389 error (_("Missing modifier."));
391 for (; *args
; ++args
)
402 modifiers
|= DISASSEMBLY_SOURCE
;
403 modifiers
|= DISASSEMBLY_FILENAME
;
406 modifiers
|= DISASSEMBLY_RAW_INSN
;
409 modifiers
|= DISASSEMBLY_OMIT_FNAME
;
412 error (_("Invalid modifier: %c."), *args
);
416 args
= skip_spaces (args
);
419 /* Update the argument string. */
425 /* The "record instruction-history" command. */
428 cmd_record_insn_history (char *arg
, int from_tty
)
432 require_record_target ();
434 flags
= get_insn_history_modifiers (&arg
);
436 /* We use a signed size to also indicate the direction. Make sure that
437 unlimited remains unlimited. */
438 size
= (int) record_insn_history_size
;
442 if (arg
== NULL
|| *arg
== 0 || strcmp (arg
, "+") == 0)
443 target_insn_history (size
, flags
);
444 else if (strcmp (arg
, "-") == 0)
445 target_insn_history (-size
, flags
);
450 begin
= get_insn_number (&arg
);
454 arg
= skip_spaces (++arg
);
459 size
= get_context_size (&arg
);
463 target_insn_history_from (begin
, size
, flags
);
465 else if (*arg
== '-')
468 size
= get_context_size (&arg
);
472 target_insn_history_from (begin
, -size
, flags
);
476 end
= get_insn_number (&arg
);
480 target_insn_history_range (begin
, end
, flags
);
487 target_insn_history_from (begin
, size
, flags
);
494 /* Provide a prototype to silence -Wmissing-prototypes. */
495 extern initialize_file_ftype _initialize_record
;
498 _initialize_record (void)
500 struct cmd_list_element
*c
;
502 add_setshow_zuinteger_cmd ("record", no_class
, &record_debug
,
503 _("Set debugging of record/replay feature."),
504 _("Show debugging of record/replay feature."),
505 _("When enabled, debugging output for "
506 "record/replay feature is displayed."),
507 NULL
, show_record_debug
, &setdebuglist
,
510 add_setshow_uinteger_cmd ("instruction-history-size", no_class
,
511 &record_insn_history_size
, _("\
512 Set number of instructions to print in \"record instruction-history\"."), _("\
513 Show number of instructions to print in \"record instruction-history\"."),
514 NULL
, NULL
, NULL
, &set_record_cmdlist
,
515 &show_record_cmdlist
);
517 c
= add_prefix_cmd ("record", class_obscure
, cmd_record_start
,
518 _("Start recording."),
519 &record_cmdlist
, "record ", 0, &cmdlist
);
520 set_cmd_completer (c
, filename_completer
);
522 add_com_alias ("rec", "record", class_obscure
, 1);
523 add_prefix_cmd ("record", class_support
, set_record_command
,
524 _("Set record options"), &set_record_cmdlist
,
525 "set record ", 0, &setlist
);
526 add_alias_cmd ("rec", "record", class_obscure
, 1, &setlist
);
527 add_prefix_cmd ("record", class_support
, show_record_command
,
528 _("Show record options"), &show_record_cmdlist
,
529 "show record ", 0, &showlist
);
530 add_alias_cmd ("rec", "record", class_obscure
, 1, &showlist
);
531 add_prefix_cmd ("record", class_support
, info_record_command
,
532 _("Info record options"), &info_record_cmdlist
,
533 "info record ", 0, &infolist
);
534 add_alias_cmd ("rec", "record", class_obscure
, 1, &infolist
);
536 c
= add_cmd ("save", class_obscure
, cmd_record_save
,
537 _("Save the execution log to a file.\n\
538 Argument is optional filename.\n\
539 Default filename is 'gdb_record.<process_id>'."),
541 set_cmd_completer (c
, filename_completer
);
543 add_cmd ("delete", class_obscure
, cmd_record_delete
,
544 _("Delete the rest of execution log and start recording it anew."),
546 add_alias_cmd ("d", "delete", class_obscure
, 1, &record_cmdlist
);
547 add_alias_cmd ("del", "delete", class_obscure
, 1, &record_cmdlist
);
549 add_cmd ("stop", class_obscure
, cmd_record_stop
,
550 _("Stop the record/replay target."),
552 add_alias_cmd ("s", "stop", class_obscure
, 1, &record_cmdlist
);
554 add_cmd ("goto", class_obscure
, cmd_record_goto
, _("\
555 Restore the program to its state at instruction number N.\n\
556 Argument is instruction number, as shown by 'info record'."),
559 add_cmd ("instruction-history", class_obscure
, cmd_record_insn_history
, _("\
560 Print disassembled instructions stored in the execution log.\n\
561 With a /m modifier, source lines are included (if available).\n\
562 With a /r modifier, raw instructions in hex are included.\n\
563 With a /f modifier, function names are omitted.\n\
564 With no argument, disassembles ten more instructions after the previous \
566 \"record instruction-history -\" disassembles ten instructions before a \
567 previous disassembly.\n\
568 One argument specifies an instruction number as shown by 'info record', and \
569 ten instructions are disassembled after that instruction.\n\
570 Two arguments with comma between them specify starting and ending instruction \
571 numbers to disassemble.\n\
572 If the second argument is preceded by '+' or '-', it specifies the distance \
573 from the first argument.\n\
574 The number of instructions to disassemble can be defined with \"set record \
575 instruction-history-size\"."),