1 /* Process record and replay target for GDB, the GNU debugger.
3 Copyright (C) 2008-2016 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 variable registered as control variable in the "record
39 instruction-history" command. Necessary for extra input
41 static unsigned int record_insn_history_size_setshow_var
;
43 /* The number of functions to print in "record function-call-history". */
44 static unsigned int record_call_history_size
= 10;
46 /* The variable registered as control variable in the "record
47 call-history" command. Necessary for extra input validation. */
48 static unsigned int record_call_history_size_setshow_var
;
50 struct cmd_list_element
*record_cmdlist
= NULL
;
51 struct cmd_list_element
*record_goto_cmdlist
= NULL
;
52 struct cmd_list_element
*set_record_cmdlist
= NULL
;
53 struct cmd_list_element
*show_record_cmdlist
= NULL
;
54 struct cmd_list_element
*info_record_cmdlist
= NULL
;
56 #define DEBUG(msg, args...) \
58 fprintf_unfiltered (gdb_stdlog, "record: " msg "\n", ##args)
63 find_record_target (void)
65 return find_target_at (record_stratum
);
68 /* Check that recording is active. Throw an error, if it isn't. */
70 static struct target_ops
*
71 require_record_target (void)
75 t
= find_record_target ();
77 error (_("No record target is currently active.\n"
78 "Use one of the \"target record-<tab><tab>\" commands first."));
88 /* Check if a record target is already running. */
89 if (find_record_target () != NULL
)
90 error (_("The process is already being recorded. Use \"record stop\" to "
91 "stop recording first."));
97 record_read_memory (struct gdbarch
*gdbarch
,
98 CORE_ADDR memaddr
, gdb_byte
*myaddr
,
101 int ret
= target_read_memory (memaddr
, myaddr
, len
);
104 DEBUG ("error reading memory at addr %s len = %ld.\n",
105 paddress (gdbarch
, memaddr
), (long) len
);
110 /* Stop recording. */
113 record_stop (struct target_ops
*t
)
115 DEBUG ("stop %s", t
->to_shortname
);
117 t
->to_stop_recording (t
);
120 /* Unpush the record target. */
123 record_unpush (struct target_ops
*t
)
125 DEBUG ("unpush %s", t
->to_shortname
);
133 record_disconnect (struct target_ops
*t
, const char *args
, int from_tty
)
135 gdb_assert (t
->to_stratum
== record_stratum
);
137 DEBUG ("disconnect %s", t
->to_shortname
);
142 target_disconnect (args
, from_tty
);
148 record_detach (struct target_ops
*t
, const char *args
, int from_tty
)
150 gdb_assert (t
->to_stratum
== record_stratum
);
152 DEBUG ("detach %s", t
->to_shortname
);
157 target_detach (args
, from_tty
);
163 record_mourn_inferior (struct target_ops
*t
)
165 gdb_assert (t
->to_stratum
== record_stratum
);
167 DEBUG ("mourn inferior %s", t
->to_shortname
);
169 /* It is safer to not stop recording. Resources will be freed when
170 threads are discarded. */
173 target_mourn_inferior (inferior_ptid
);
179 record_kill (struct target_ops
*t
)
181 gdb_assert (t
->to_stratum
== record_stratum
);
183 DEBUG ("kill %s", t
->to_shortname
);
185 /* It is safer to not stop recording. Resources will be freed when
186 threads are discarded. */
195 record_check_stopped_by_breakpoint (struct address_space
*aspace
, CORE_ADDR pc
,
196 enum target_stop_reason
*reason
)
198 if (breakpoint_inserted_here_p (aspace
, pc
))
200 if (hardware_breakpoint_inserted_here_p (aspace
, pc
))
201 *reason
= TARGET_STOPPED_BY_HW_BREAKPOINT
;
203 *reason
= TARGET_STOPPED_BY_SW_BREAKPOINT
;
207 *reason
= TARGET_STOPPED_BY_NO_REASON
;
211 /* Implement "show record debug" command. */
214 show_record_debug (struct ui_file
*file
, int from_tty
,
215 struct cmd_list_element
*c
, const char *value
)
217 fprintf_filtered (file
, _("Debugging of process record target is %s.\n"),
221 /* Alias for "target record". */
224 cmd_record_start (char *args
, int from_tty
)
226 execute_command ("target record-full", from_tty
);
229 /* Truncate the record log from the present point
230 of replay until the end. */
233 cmd_record_delete (char *args
, int from_tty
)
235 require_record_target ();
237 if (!target_record_is_replaying (inferior_ptid
))
239 printf_unfiltered (_("Already at end of record list.\n"));
243 if (!target_supports_delete_record ())
245 printf_unfiltered (_("The current record target does not support "
246 "this operation.\n"));
250 if (!from_tty
|| query (_("Delete the log from this point forward "
251 "and begin to record the running message "
253 target_delete_record ();
256 /* Implement the "stoprecord" or "record stop" command. */
259 cmd_record_stop (char *args
, int from_tty
)
261 struct target_ops
*t
;
263 t
= require_record_target ();
268 printf_unfiltered (_("Process record is stopped and all execution "
269 "logs are deleted.\n"));
271 observer_notify_record_changed (current_inferior (), 0, NULL
, NULL
);
274 /* The "set record" command. */
277 set_record_command (char *args
, int from_tty
)
279 printf_unfiltered (_("\"set record\" must be followed "
280 "by an apporpriate subcommand.\n"));
281 help_list (set_record_cmdlist
, "set record ", all_commands
, gdb_stdout
);
284 /* The "show record" command. */
287 show_record_command (char *args
, int from_tty
)
289 cmd_show_list (show_record_cmdlist
, from_tty
, "");
292 /* The "info record" command. */
295 info_record_command (char *args
, int from_tty
)
297 struct target_ops
*t
;
299 t
= find_record_target ();
302 printf_filtered (_("No record target is currently active.\n"));
306 printf_filtered (_("Active record target: %s\n"), t
->to_shortname
);
307 t
->to_info_record (t
);
310 /* The "record save" command. */
313 cmd_record_save (char *args
, int from_tty
)
315 char *recfilename
, recfilename_buffer
[40];
317 require_record_target ();
319 if (args
!= NULL
&& *args
!= 0)
323 /* Default recfile name is "gdb_record.PID". */
324 xsnprintf (recfilename_buffer
, sizeof (recfilename_buffer
),
325 "gdb_record.%d", ptid_get_pid (inferior_ptid
));
326 recfilename
= recfilename_buffer
;
329 target_save_record (recfilename
);
335 record_goto (const char *arg
)
339 if (arg
== NULL
|| *arg
== '\0')
340 error (_("Command requires an argument (insn number to go to)."));
342 insn
= parse_and_eval_long (arg
);
344 require_record_target ();
345 target_goto_record (insn
);
348 /* "record goto" command. Argument is an instruction number,
349 as given by "info record".
351 Rewinds the recording (forward or backward) to the given instruction. */
354 cmd_record_goto (char *arg
, int from_tty
)
359 /* The "record goto begin" command. */
362 cmd_record_goto_begin (char *arg
, int from_tty
)
364 if (arg
!= NULL
&& *arg
!= '\0')
365 error (_("Junk after argument: %s."), arg
);
367 require_record_target ();
368 target_goto_record_begin ();
371 /* The "record goto end" command. */
374 cmd_record_goto_end (char *arg
, int from_tty
)
376 if (arg
!= NULL
&& *arg
!= '\0')
377 error (_("Junk after argument: %s."), arg
);
379 require_record_target ();
380 target_goto_record_end ();
383 /* Read an instruction number from an argument string. */
386 get_insn_number (char **arg
)
389 const char *begin
, *end
, *pos
;
392 pos
= skip_spaces_const (begin
);
395 error (_("Expected positive number, got: %s."), pos
);
397 number
= strtoulst (pos
, &end
, 10);
399 *arg
+= (end
- begin
);
404 /* Read a context size from an argument string. */
407 get_context_size (char **arg
)
412 pos
= skip_spaces (*arg
);
415 error (_("Expected positive number, got: %s."), pos
);
417 return strtol (pos
, arg
, 10);
420 /* Complain about junk at the end of an argument string. */
426 error (_("Junk after argument: %s."), arg
);
429 /* Read instruction-history modifiers from an argument string. */
432 get_insn_history_modifiers (char **arg
)
448 error (_("Missing modifier."));
450 for (; *args
; ++args
)
462 modifiers
|= DISASSEMBLY_SOURCE
;
463 modifiers
|= DISASSEMBLY_FILENAME
;
466 modifiers
|= DISASSEMBLY_RAW_INSN
;
469 modifiers
|= DISASSEMBLY_OMIT_FNAME
;
472 modifiers
|= DISASSEMBLY_OMIT_PC
;
475 error (_("Invalid modifier: %c."), *args
);
479 args
= skip_spaces (args
);
482 /* Update the argument string. */
488 /* The "set record instruction-history-size / set record
489 function-call-history-size" commands are unsigned, with UINT_MAX
490 meaning unlimited. The target interfaces works with signed int
491 though, to indicate direction, so map "unlimited" to INT_MAX, which
492 is about the same as unlimited in practice. If the user does have
493 a log that huge, she can fetch it in chunks across several requests,
494 but she'll likely have other problems first... */
497 command_size_to_target_size (unsigned int size
)
499 gdb_assert (size
<= INT_MAX
|| size
== UINT_MAX
);
501 if (size
== UINT_MAX
)
507 /* The "record instruction-history" command. */
510 cmd_record_insn_history (char *arg
, int from_tty
)
514 require_record_target ();
516 flags
= get_insn_history_modifiers (&arg
);
518 size
= command_size_to_target_size (record_insn_history_size
);
520 if (arg
== NULL
|| *arg
== 0 || strcmp (arg
, "+") == 0)
521 target_insn_history (size
, flags
);
522 else if (strcmp (arg
, "-") == 0)
523 target_insn_history (-size
, flags
);
528 begin
= get_insn_number (&arg
);
532 arg
= skip_spaces (++arg
);
537 size
= get_context_size (&arg
);
541 target_insn_history_from (begin
, size
, flags
);
543 else if (*arg
== '-')
546 size
= get_context_size (&arg
);
550 target_insn_history_from (begin
, -size
, flags
);
554 end
= get_insn_number (&arg
);
558 target_insn_history_range (begin
, end
, flags
);
565 target_insn_history_from (begin
, size
, flags
);
572 /* Read function-call-history modifiers from an argument string. */
575 get_call_history_modifiers (char **arg
)
591 error (_("Missing modifier."));
593 for (; *args
; ++args
)
604 modifiers
|= RECORD_PRINT_SRC_LINE
;
607 modifiers
|= RECORD_PRINT_INSN_RANGE
;
610 modifiers
|= RECORD_PRINT_INDENT_CALLS
;
613 error (_("Invalid modifier: %c."), *args
);
617 args
= skip_spaces (args
);
620 /* Update the argument string. */
626 /* The "record function-call-history" command. */
629 cmd_record_call_history (char *arg
, int from_tty
)
633 require_record_target ();
635 flags
= get_call_history_modifiers (&arg
);
637 size
= command_size_to_target_size (record_call_history_size
);
639 if (arg
== NULL
|| *arg
== 0 || strcmp (arg
, "+") == 0)
640 target_call_history (size
, flags
);
641 else if (strcmp (arg
, "-") == 0)
642 target_call_history (-size
, flags
);
647 begin
= get_insn_number (&arg
);
651 arg
= skip_spaces (++arg
);
656 size
= get_context_size (&arg
);
660 target_call_history_from (begin
, size
, flags
);
662 else if (*arg
== '-')
665 size
= get_context_size (&arg
);
669 target_call_history_from (begin
, -size
, flags
);
673 end
= get_insn_number (&arg
);
677 target_call_history_range (begin
, end
, flags
);
684 target_call_history_from (begin
, size
, flags
);
691 /* Helper for "set record instruction-history-size" and "set record
692 function-call-history-size" input validation. COMMAND_VAR is the
693 variable registered in the command as control variable. *SETTING
694 is the real setting the command allows changing. */
697 validate_history_size (unsigned int *command_var
, unsigned int *setting
)
699 if (*command_var
!= UINT_MAX
&& *command_var
> INT_MAX
)
701 unsigned int new_value
= *command_var
;
703 /* Restore previous value. */
704 *command_var
= *setting
;
705 error (_("integer %u out of range"), new_value
);
708 /* Commit new value. */
709 *setting
= *command_var
;
712 /* Called by do_setshow_command. We only want values in the
713 [0..INT_MAX] range, while the command's machinery accepts
714 [0..UINT_MAX]. See command_size_to_target_size. */
717 set_record_insn_history_size (char *args
, int from_tty
,
718 struct cmd_list_element
*c
)
720 validate_history_size (&record_insn_history_size_setshow_var
,
721 &record_insn_history_size
);
724 /* Called by do_setshow_command. We only want values in the
725 [0..INT_MAX] range, while the command's machinery accepts
726 [0..UINT_MAX]. See command_size_to_target_size. */
729 set_record_call_history_size (char *args
, int from_tty
,
730 struct cmd_list_element
*c
)
732 validate_history_size (&record_call_history_size_setshow_var
,
733 &record_call_history_size
);
736 /* Provide a prototype to silence -Wmissing-prototypes. */
737 extern initialize_file_ftype _initialize_record
;
740 _initialize_record (void)
742 struct cmd_list_element
*c
;
744 add_setshow_zuinteger_cmd ("record", no_class
, &record_debug
,
745 _("Set debugging of record/replay feature."),
746 _("Show debugging of record/replay feature."),
747 _("When enabled, debugging output for "
748 "record/replay feature is displayed."),
749 NULL
, show_record_debug
, &setdebuglist
,
752 add_setshow_uinteger_cmd ("instruction-history-size", no_class
,
753 &record_insn_history_size_setshow_var
, _("\
754 Set number of instructions to print in \"record instruction-history\"."), _("\
755 Show number of instructions to print in \"record instruction-history\"."), _("\
756 A size of \"unlimited\" means unlimited instructions. The default is 10."),
757 set_record_insn_history_size
, NULL
,
758 &set_record_cmdlist
, &show_record_cmdlist
);
760 add_setshow_uinteger_cmd ("function-call-history-size", no_class
,
761 &record_call_history_size_setshow_var
, _("\
762 Set number of function to print in \"record function-call-history\"."), _("\
763 Show number of functions to print in \"record function-call-history\"."), _("\
764 A size of \"unlimited\" means unlimited lines. The default is 10."),
765 set_record_call_history_size
, NULL
,
766 &set_record_cmdlist
, &show_record_cmdlist
);
768 c
= add_prefix_cmd ("record", class_obscure
, cmd_record_start
,
769 _("Start recording."),
770 &record_cmdlist
, "record ", 0, &cmdlist
);
771 set_cmd_completer (c
, filename_completer
);
773 add_com_alias ("rec", "record", class_obscure
, 1);
774 add_prefix_cmd ("record", class_support
, set_record_command
,
775 _("Set record options"), &set_record_cmdlist
,
776 "set record ", 0, &setlist
);
777 add_alias_cmd ("rec", "record", class_obscure
, 1, &setlist
);
778 add_prefix_cmd ("record", class_support
, show_record_command
,
779 _("Show record options"), &show_record_cmdlist
,
780 "show record ", 0, &showlist
);
781 add_alias_cmd ("rec", "record", class_obscure
, 1, &showlist
);
782 add_prefix_cmd ("record", class_support
, info_record_command
,
783 _("Info record options"), &info_record_cmdlist
,
784 "info record ", 0, &infolist
);
785 add_alias_cmd ("rec", "record", class_obscure
, 1, &infolist
);
787 c
= add_cmd ("save", class_obscure
, cmd_record_save
,
788 _("Save the execution log to a file.\n\
789 Argument is optional filename.\n\
790 Default filename is 'gdb_record.<process_id>'."),
792 set_cmd_completer (c
, filename_completer
);
794 add_cmd ("delete", class_obscure
, cmd_record_delete
,
795 _("Delete the rest of execution log and start recording it anew."),
797 add_alias_cmd ("d", "delete", class_obscure
, 1, &record_cmdlist
);
798 add_alias_cmd ("del", "delete", class_obscure
, 1, &record_cmdlist
);
800 add_cmd ("stop", class_obscure
, cmd_record_stop
,
801 _("Stop the record/replay target."),
803 add_alias_cmd ("s", "stop", class_obscure
, 1, &record_cmdlist
);
805 add_prefix_cmd ("goto", class_obscure
, cmd_record_goto
, _("\
806 Restore the program to its state at instruction number N.\n\
807 Argument is instruction number, as shown by 'info record'."),
808 &record_goto_cmdlist
, "record goto ", 1, &record_cmdlist
);
810 add_cmd ("begin", class_obscure
, cmd_record_goto_begin
,
811 _("Go to the beginning of the execution log."),
812 &record_goto_cmdlist
);
813 add_alias_cmd ("start", "begin", class_obscure
, 1, &record_goto_cmdlist
);
815 add_cmd ("end", class_obscure
, cmd_record_goto_end
,
816 _("Go to the end of the execution log."),
817 &record_goto_cmdlist
);
819 add_cmd ("instruction-history", class_obscure
, cmd_record_insn_history
, _("\
820 Print disassembled instructions stored in the execution log.\n\
821 With a /m or /s modifier, source lines are included (if available).\n\
822 With a /r modifier, raw instructions in hex are included.\n\
823 With a /f modifier, function names are omitted.\n\
824 With a /p modifier, current position markers are omitted.\n\
825 With no argument, disassembles ten more instructions after the previous \
827 \"record instruction-history -\" disassembles ten instructions before a \
828 previous disassembly.\n\
829 One argument specifies an instruction number as shown by 'info record', and \
830 ten instructions are disassembled after that instruction.\n\
831 Two arguments with comma between them specify starting and ending instruction \
832 numbers to disassemble.\n\
833 If the second argument is preceded by '+' or '-', it specifies the distance \
834 from the first argument.\n\
835 The number of instructions to disassemble can be defined with \"set record \
836 instruction-history-size\"."),
839 add_cmd ("function-call-history", class_obscure
, cmd_record_call_history
, _("\
840 Prints the execution history at function granularity.\n\
841 It prints one line for each sequence of instructions that belong to the same \
843 Without modifiers, it prints the function name.\n\
844 With a /l modifier, the source file and line number range is included.\n\
845 With a /i modifier, the instruction number range is included.\n\
846 With a /c modifier, the output is indented based on the call stack depth.\n\
847 With no argument, prints ten more lines after the previous ten-line print.\n\
848 \"record function-call-history -\" prints ten lines before a previous ten-line \
850 One argument specifies a function number as shown by 'info record', and \
851 ten lines are printed after that function.\n\
852 Two arguments with comma between them specify a range of functions to print.\n\
853 If the second argument is preceded by '+' or '-', it specifies the distance \
854 from the first argument.\n\
855 The number of functions to print can be defined with \"set record \
856 function-call-history-size\"."),
859 /* Sync command control variables. */
860 record_insn_history_size_setshow_var
= record_insn_history_size
;
861 record_call_history_size_setshow_var
= record_call_history_size
;