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 /* The number of functions to print in "record function-call-history". */
39 static unsigned int record_call_history_size
= 10;
41 struct cmd_list_element
*record_cmdlist
= NULL
;
42 struct cmd_list_element
*set_record_cmdlist
= NULL
;
43 struct cmd_list_element
*show_record_cmdlist
= NULL
;
44 struct cmd_list_element
*info_record_cmdlist
= NULL
;
46 #define DEBUG(msg, args...) \
48 fprintf_unfiltered (gdb_stdlog, "record: " msg "\n", ##args)
50 /* Find the record target in the target stack. */
52 static struct target_ops
*
53 find_record_target (void)
57 for (t
= current_target
.beneath
; t
!= NULL
; t
= t
->beneath
)
58 if (t
->to_stratum
== record_stratum
)
64 /* Check that recording is active. Throw an error, if it isn't. */
66 static struct target_ops
*
67 require_record_target (void)
71 t
= find_record_target ();
73 error (_("No record target is currently active.\n"
74 "Use one of the \"target record-<tab><tab>\" commands first."));
82 record_read_memory (struct gdbarch
*gdbarch
,
83 CORE_ADDR memaddr
, gdb_byte
*myaddr
,
86 int ret
= target_read_memory (memaddr
, myaddr
, len
);
89 DEBUG ("error reading memory at addr %s len = %ld.\n",
90 paddress (gdbarch
, memaddr
), (long) len
);
98 record_stop (struct target_ops
*t
)
100 DEBUG ("stop %s", t
->to_shortname
);
102 if (t
->to_stop_recording
!= NULL
)
103 t
->to_stop_recording ();
106 /* Unpush the record target. */
109 record_unpush (struct target_ops
*t
)
111 DEBUG ("unpush %s", t
->to_shortname
);
119 record_disconnect (struct target_ops
*t
, char *args
, int from_tty
)
121 gdb_assert (t
->to_stratum
== record_stratum
);
123 DEBUG ("disconnect %s", t
->to_shortname
);
128 target_disconnect (args
, from_tty
);
134 record_detach (struct target_ops
*t
, char *args
, int from_tty
)
136 gdb_assert (t
->to_stratum
== record_stratum
);
138 DEBUG ("detach %s", t
->to_shortname
);
143 target_detach (args
, from_tty
);
149 record_mourn_inferior (struct target_ops
*t
)
151 gdb_assert (t
->to_stratum
== record_stratum
);
153 DEBUG ("mourn inferior %s", t
->to_shortname
);
155 /* It is safer to not stop recording. Resources will be freed when
156 threads are discarded. */
159 target_mourn_inferior ();
165 record_kill (struct target_ops
*t
)
167 gdb_assert (t
->to_stratum
== record_stratum
);
169 DEBUG ("kill %s", t
->to_shortname
);
171 /* It is safer to not stop recording. Resources will be freed when
172 threads are discarded. */
178 /* Implement "show record debug" command. */
181 show_record_debug (struct ui_file
*file
, int from_tty
,
182 struct cmd_list_element
*c
, const char *value
)
184 fprintf_filtered (file
, _("Debugging of process record target is %s.\n"),
188 /* Alias for "target record". */
191 cmd_record_start (char *args
, int from_tty
)
193 execute_command ("target record-full", from_tty
);
196 /* Truncate the record log from the present point
197 of replay until the end. */
200 cmd_record_delete (char *args
, int from_tty
)
202 require_record_target ();
204 if (!target_record_is_replaying ())
206 printf_unfiltered (_("Already at end of record list.\n"));
210 if (!target_supports_delete_record ())
212 printf_unfiltered (_("The current record target does not support "
213 "this operation.\n"));
217 if (!from_tty
|| query (_("Delete the log from this point forward "
218 "and begin to record the running message "
220 target_delete_record ();
223 /* Implement the "stoprecord" or "record stop" command. */
226 cmd_record_stop (char *args
, int from_tty
)
228 struct target_ops
*t
;
230 t
= require_record_target ();
235 printf_unfiltered (_("Process record is stopped and all execution "
236 "logs are deleted.\n"));
238 observer_notify_record_changed (current_inferior (), 0);
241 /* The "set record" command. */
244 set_record_command (char *args
, int from_tty
)
246 printf_unfiltered (_("\"set record\" must be followed "
247 "by an apporpriate subcommand.\n"));
248 help_list (set_record_cmdlist
, "set record ", all_commands
, gdb_stdout
);
251 /* The "show record" command. */
254 show_record_command (char *args
, int from_tty
)
256 cmd_show_list (show_record_cmdlist
, from_tty
, "");
259 /* The "info record" command. */
262 info_record_command (char *args
, int from_tty
)
264 struct target_ops
*t
;
266 t
= find_record_target ();
269 printf_filtered (_("No record target is currently active.\n"));
273 printf_filtered (_("Active record target: %s\n"), t
->to_shortname
);
274 if (t
->to_info_record
!= NULL
)
275 t
->to_info_record ();
278 /* The "record save" command. */
281 cmd_record_save (char *args
, int from_tty
)
283 char *recfilename
, recfilename_buffer
[40];
285 require_record_target ();
287 if (args
!= NULL
&& *args
!= 0)
291 /* Default recfile name is "gdb_record.PID". */
292 xsnprintf (recfilename_buffer
, sizeof (recfilename_buffer
),
293 "gdb_record.%d", PIDGET (inferior_ptid
));
294 recfilename
= recfilename_buffer
;
297 target_save_record (recfilename
);
300 /* "record goto" command. Argument is an instruction number,
301 as given by "info record".
303 Rewinds the recording (forward or backward) to the given instruction. */
306 cmd_record_goto (char *arg
, int from_tty
)
308 require_record_target ();
310 if (arg
== NULL
|| *arg
== '\0')
311 error (_("Command requires an argument (insn number to go to)."));
313 if (strncmp (arg
, "start", strlen ("start")) == 0
314 || strncmp (arg
, "begin", strlen ("begin")) == 0)
315 target_goto_record_begin ();
316 else if (strncmp (arg
, "end", strlen ("end")) == 0)
317 target_goto_record_end ();
322 insn
= parse_and_eval_long (arg
);
323 target_goto_record (insn
);
327 /* Read an instruction number from an argument string. */
330 get_insn_number (char **arg
)
333 const char *begin
, *end
, *pos
;
336 pos
= skip_spaces_const (begin
);
339 error (_("Expected positive number, got: %s."), pos
);
341 number
= strtoulst (pos
, &end
, 10);
343 *arg
+= (end
- begin
);
348 /* Read a context size from an argument string. */
351 get_context_size (char **arg
)
356 pos
= skip_spaces (*arg
);
359 error (_("Expected positive number, got: %s."), pos
);
361 return strtol (pos
, arg
, 10);
364 /* Complain about junk at the end of an argument string. */
370 error (_("Junk after argument: %s."), arg
);
373 /* Read instruction-history modifiers from an argument string. */
376 get_insn_history_modifiers (char **arg
)
392 error (_("Missing modifier."));
394 for (; *args
; ++args
)
405 modifiers
|= DISASSEMBLY_SOURCE
;
406 modifiers
|= DISASSEMBLY_FILENAME
;
409 modifiers
|= DISASSEMBLY_RAW_INSN
;
412 modifiers
|= DISASSEMBLY_OMIT_FNAME
;
415 modifiers
|= DISASSEMBLY_OMIT_PC
;
418 error (_("Invalid modifier: %c."), *args
);
422 args
= skip_spaces (args
);
425 /* Update the argument string. */
431 /* The "record instruction-history" command. */
434 cmd_record_insn_history (char *arg
, int from_tty
)
438 require_record_target ();
440 flags
= get_insn_history_modifiers (&arg
);
442 /* We use a signed size to also indicate the direction. Make sure that
443 unlimited remains unlimited. */
444 size
= (int) record_insn_history_size
;
448 if (arg
== NULL
|| *arg
== 0 || strcmp (arg
, "+") == 0)
449 target_insn_history (size
, flags
);
450 else if (strcmp (arg
, "-") == 0)
451 target_insn_history (-size
, flags
);
456 begin
= get_insn_number (&arg
);
460 arg
= skip_spaces (++arg
);
465 size
= get_context_size (&arg
);
469 target_insn_history_from (begin
, size
, flags
);
471 else if (*arg
== '-')
474 size
= get_context_size (&arg
);
478 target_insn_history_from (begin
, -size
, flags
);
482 end
= get_insn_number (&arg
);
486 target_insn_history_range (begin
, end
, flags
);
493 target_insn_history_from (begin
, size
, flags
);
500 /* Read function-call-history modifiers from an argument string. */
503 get_call_history_modifiers (char **arg
)
519 error (_("Missing modifier."));
521 for (; *args
; ++args
)
532 modifiers
|= record_print_src_line
;
535 modifiers
|= record_print_insn_range
;
538 error (_("Invalid modifier: %c."), *args
);
542 args
= skip_spaces (args
);
545 /* Update the argument string. */
551 /* The "record function-call-history" command. */
554 cmd_record_call_history (char *arg
, int from_tty
)
558 require_record_target ();
560 flags
= get_call_history_modifiers (&arg
);
562 /* We use a signed size to also indicate the direction. Make sure that
563 unlimited remains unlimited. */
564 size
= (int) record_call_history_size
;
568 if (arg
== NULL
|| *arg
== 0 || strcmp (arg
, "+") == 0)
569 target_call_history (size
, flags
);
570 else if (strcmp (arg
, "-") == 0)
571 target_call_history (-size
, flags
);
576 begin
= get_insn_number (&arg
);
580 arg
= skip_spaces (++arg
);
585 size
= get_context_size (&arg
);
589 target_call_history_from (begin
, size
, flags
);
591 else if (*arg
== '-')
594 size
= get_context_size (&arg
);
598 target_call_history_from (begin
, -size
, flags
);
602 end
= get_insn_number (&arg
);
606 target_call_history_range (begin
, end
, flags
);
613 target_call_history_from (begin
, size
, flags
);
620 /* Provide a prototype to silence -Wmissing-prototypes. */
621 extern initialize_file_ftype _initialize_record
;
624 _initialize_record (void)
626 struct cmd_list_element
*c
;
628 add_setshow_zuinteger_cmd ("record", no_class
, &record_debug
,
629 _("Set debugging of record/replay feature."),
630 _("Show debugging of record/replay feature."),
631 _("When enabled, debugging output for "
632 "record/replay feature is displayed."),
633 NULL
, show_record_debug
, &setdebuglist
,
636 add_setshow_uinteger_cmd ("instruction-history-size", no_class
,
637 &record_insn_history_size
, _("\
638 Set number of instructions to print in \"record instruction-history\"."), _("\
639 Show number of instructions to print in \"record instruction-history\"."),
640 NULL
, NULL
, NULL
, &set_record_cmdlist
,
641 &show_record_cmdlist
);
643 add_setshow_uinteger_cmd ("function-call-history-size", no_class
,
644 &record_call_history_size
, _("\
645 Set number of function to print in \"record function-call-history\"."), _("\
646 Show number of functions to print in \"record function-call-history\"."),
647 NULL
, NULL
, NULL
, &set_record_cmdlist
,
648 &show_record_cmdlist
);
650 c
= add_prefix_cmd ("record", class_obscure
, cmd_record_start
,
651 _("Start recording."),
652 &record_cmdlist
, "record ", 0, &cmdlist
);
653 set_cmd_completer (c
, filename_completer
);
655 add_com_alias ("rec", "record", class_obscure
, 1);
656 add_prefix_cmd ("record", class_support
, set_record_command
,
657 _("Set record options"), &set_record_cmdlist
,
658 "set record ", 0, &setlist
);
659 add_alias_cmd ("rec", "record", class_obscure
, 1, &setlist
);
660 add_prefix_cmd ("record", class_support
, show_record_command
,
661 _("Show record options"), &show_record_cmdlist
,
662 "show record ", 0, &showlist
);
663 add_alias_cmd ("rec", "record", class_obscure
, 1, &showlist
);
664 add_prefix_cmd ("record", class_support
, info_record_command
,
665 _("Info record options"), &info_record_cmdlist
,
666 "info record ", 0, &infolist
);
667 add_alias_cmd ("rec", "record", class_obscure
, 1, &infolist
);
669 c
= add_cmd ("save", class_obscure
, cmd_record_save
,
670 _("Save the execution log to a file.\n\
671 Argument is optional filename.\n\
672 Default filename is 'gdb_record.<process_id>'."),
674 set_cmd_completer (c
, filename_completer
);
676 add_cmd ("delete", class_obscure
, cmd_record_delete
,
677 _("Delete the rest of execution log and start recording it anew."),
679 add_alias_cmd ("d", "delete", class_obscure
, 1, &record_cmdlist
);
680 add_alias_cmd ("del", "delete", class_obscure
, 1, &record_cmdlist
);
682 add_cmd ("stop", class_obscure
, cmd_record_stop
,
683 _("Stop the record/replay target."),
685 add_alias_cmd ("s", "stop", class_obscure
, 1, &record_cmdlist
);
687 add_cmd ("goto", class_obscure
, cmd_record_goto
, _("\
688 Restore the program to its state at instruction number N.\n\
689 Argument is instruction number, as shown by 'info record'."),
692 add_cmd ("instruction-history", class_obscure
, cmd_record_insn_history
, _("\
693 Print disassembled instructions stored in the execution log.\n\
694 With a /m modifier, source lines are included (if available).\n\
695 With a /r modifier, raw instructions in hex are included.\n\
696 With a /f modifier, function names are omitted.\n\
697 With a /p modifier, current position markers are omitted.\n\
698 With no argument, disassembles ten more instructions after the previous \
700 \"record instruction-history -\" disassembles ten instructions before a \
701 previous disassembly.\n\
702 One argument specifies an instruction number as shown by 'info record', and \
703 ten instructions are disassembled after that instruction.\n\
704 Two arguments with comma between them specify starting and ending instruction \
705 numbers to disassemble.\n\
706 If the second argument is preceded by '+' or '-', it specifies the distance \
707 from the first argument.\n\
708 The number of instructions to disassemble can be defined with \"set record \
709 instruction-history-size\"."),
712 add_cmd ("function-call-history", class_obscure
, cmd_record_call_history
, _("\
713 Prints the execution history at function granularity.\n\
714 It prints one line for each sequence of instructions that belong to the same \
716 Without modifiers, it prints the function name.\n\
717 With a /l modifier, the source file and line number range is included.\n\
718 With a /i modifier, the instruction number range is included.\n\
719 With no argument, prints ten more lines after the previous ten-line print.\n\
720 \"record function-call-history -\" prints ten lines before a previous ten-line \
722 One argument specifies a function number as shown by 'info record', and \
723 ten lines are printed after that function.\n\
724 Two arguments with comma between them specify a range of functions to print.\n\
725 If the second argument is preceded by '+' or '-', it specifies the distance \
726 from the first argument.\n\
727 The number of functions to print can be defined with \"set record \
728 function-call-history-size\"."),