1 /* Process record and replay target for GDB, the GNU debugger.
3 Copyright (C) 2008-2014 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 if (t
->to_stop_recording
!= NULL
)
118 t
->to_stop_recording (t
);
121 /* Unpush the record target. */
124 record_unpush (struct target_ops
*t
)
126 DEBUG ("unpush %s", t
->to_shortname
);
134 record_disconnect (struct target_ops
*t
, char *args
, int from_tty
)
136 gdb_assert (t
->to_stratum
== record_stratum
);
138 DEBUG ("disconnect %s", t
->to_shortname
);
143 target_disconnect (args
, from_tty
);
149 record_detach (struct target_ops
*t
, const char *args
, int from_tty
)
151 gdb_assert (t
->to_stratum
== record_stratum
);
153 DEBUG ("detach %s", t
->to_shortname
);
158 target_detach (args
, from_tty
);
164 record_mourn_inferior (struct target_ops
*t
)
166 gdb_assert (t
->to_stratum
== record_stratum
);
168 DEBUG ("mourn inferior %s", t
->to_shortname
);
170 /* It is safer to not stop recording. Resources will be freed when
171 threads are discarded. */
174 target_mourn_inferior ();
180 record_kill (struct target_ops
*t
)
182 gdb_assert (t
->to_stratum
== record_stratum
);
184 DEBUG ("kill %s", t
->to_shortname
);
186 /* It is safer to not stop recording. Resources will be freed when
187 threads are discarded. */
193 /* Implement "show record debug" command. */
196 show_record_debug (struct ui_file
*file
, int from_tty
,
197 struct cmd_list_element
*c
, const char *value
)
199 fprintf_filtered (file
, _("Debugging of process record target is %s.\n"),
203 /* Alias for "target record". */
206 cmd_record_start (char *args
, int from_tty
)
208 execute_command ("target record-full", from_tty
);
211 /* Truncate the record log from the present point
212 of replay until the end. */
215 cmd_record_delete (char *args
, int from_tty
)
217 require_record_target ();
219 if (!target_record_is_replaying ())
221 printf_unfiltered (_("Already at end of record list.\n"));
225 if (!target_supports_delete_record ())
227 printf_unfiltered (_("The current record target does not support "
228 "this operation.\n"));
232 if (!from_tty
|| query (_("Delete the log from this point forward "
233 "and begin to record the running message "
235 target_delete_record ();
238 /* Implement the "stoprecord" or "record stop" command. */
241 cmd_record_stop (char *args
, int from_tty
)
243 struct target_ops
*t
;
245 t
= require_record_target ();
250 printf_unfiltered (_("Process record is stopped and all execution "
251 "logs are deleted.\n"));
253 observer_notify_record_changed (current_inferior (), 0);
256 /* The "set record" command. */
259 set_record_command (char *args
, int from_tty
)
261 printf_unfiltered (_("\"set record\" must be followed "
262 "by an apporpriate subcommand.\n"));
263 help_list (set_record_cmdlist
, "set record ", all_commands
, gdb_stdout
);
266 /* The "show record" command. */
269 show_record_command (char *args
, int from_tty
)
271 cmd_show_list (show_record_cmdlist
, from_tty
, "");
274 /* The "info record" command. */
277 info_record_command (char *args
, int from_tty
)
279 struct target_ops
*t
;
281 t
= find_record_target ();
284 printf_filtered (_("No record target is currently active.\n"));
288 printf_filtered (_("Active record target: %s\n"), t
->to_shortname
);
289 if (t
->to_info_record
!= NULL
)
290 t
->to_info_record ();
293 /* The "record save" command. */
296 cmd_record_save (char *args
, int from_tty
)
298 char *recfilename
, recfilename_buffer
[40];
300 require_record_target ();
302 if (args
!= NULL
&& *args
!= 0)
306 /* Default recfile name is "gdb_record.PID". */
307 xsnprintf (recfilename_buffer
, sizeof (recfilename_buffer
),
308 "gdb_record.%d", ptid_get_pid (inferior_ptid
));
309 recfilename
= recfilename_buffer
;
312 target_save_record (recfilename
);
315 /* "record goto" command. Argument is an instruction number,
316 as given by "info record".
318 Rewinds the recording (forward or backward) to the given instruction. */
321 cmd_record_goto (char *arg
, int from_tty
)
325 if (arg
== NULL
|| *arg
== '\0')
326 error (_("Command requires an argument (insn number to go to)."));
328 insn
= parse_and_eval_long (arg
);
330 require_record_target ();
331 target_goto_record (insn
);
334 /* The "record goto begin" command. */
337 cmd_record_goto_begin (char *arg
, int from_tty
)
339 if (arg
!= NULL
&& *arg
!= '\0')
340 error (_("Junk after argument: %s."), arg
);
342 require_record_target ();
343 target_goto_record_begin ();
346 /* The "record goto end" command. */
349 cmd_record_goto_end (char *arg
, int from_tty
)
351 if (arg
!= NULL
&& *arg
!= '\0')
352 error (_("Junk after argument: %s."), arg
);
354 require_record_target ();
355 target_goto_record_end ();
358 /* Read an instruction number from an argument string. */
361 get_insn_number (char **arg
)
364 const char *begin
, *end
, *pos
;
367 pos
= skip_spaces_const (begin
);
370 error (_("Expected positive number, got: %s."), pos
);
372 number
= strtoulst (pos
, &end
, 10);
374 *arg
+= (end
- begin
);
379 /* Read a context size from an argument string. */
382 get_context_size (char **arg
)
387 pos
= skip_spaces (*arg
);
390 error (_("Expected positive number, got: %s."), pos
);
392 return strtol (pos
, arg
, 10);
395 /* Complain about junk at the end of an argument string. */
401 error (_("Junk after argument: %s."), arg
);
404 /* Read instruction-history modifiers from an argument string. */
407 get_insn_history_modifiers (char **arg
)
423 error (_("Missing modifier."));
425 for (; *args
; ++args
)
436 modifiers
|= DISASSEMBLY_SOURCE
;
437 modifiers
|= DISASSEMBLY_FILENAME
;
440 modifiers
|= DISASSEMBLY_RAW_INSN
;
443 modifiers
|= DISASSEMBLY_OMIT_FNAME
;
446 modifiers
|= DISASSEMBLY_OMIT_PC
;
449 error (_("Invalid modifier: %c."), *args
);
453 args
= skip_spaces (args
);
456 /* Update the argument string. */
462 /* The "set record instruction-history-size / set record
463 function-call-history-size" commands are unsigned, with UINT_MAX
464 meaning unlimited. The target interfaces works with signed int
465 though, to indicate direction, so map "unlimited" to INT_MAX, which
466 is about the same as unlimited in practice. If the user does have
467 a log that huge, she can fetch it in chunks across several requests,
468 but she'll likely have other problems first... */
471 command_size_to_target_size (unsigned int size
)
473 gdb_assert (size
<= INT_MAX
|| size
== UINT_MAX
);
475 if (size
== UINT_MAX
)
481 /* The "record instruction-history" command. */
484 cmd_record_insn_history (char *arg
, int from_tty
)
488 require_record_target ();
490 flags
= get_insn_history_modifiers (&arg
);
492 size
= command_size_to_target_size (record_insn_history_size
);
494 if (arg
== NULL
|| *arg
== 0 || strcmp (arg
, "+") == 0)
495 target_insn_history (size
, flags
);
496 else if (strcmp (arg
, "-") == 0)
497 target_insn_history (-size
, flags
);
502 begin
= get_insn_number (&arg
);
506 arg
= skip_spaces (++arg
);
511 size
= get_context_size (&arg
);
515 target_insn_history_from (begin
, size
, flags
);
517 else if (*arg
== '-')
520 size
= get_context_size (&arg
);
524 target_insn_history_from (begin
, -size
, flags
);
528 end
= get_insn_number (&arg
);
532 target_insn_history_range (begin
, end
, flags
);
539 target_insn_history_from (begin
, size
, flags
);
546 /* Read function-call-history modifiers from an argument string. */
549 get_call_history_modifiers (char **arg
)
565 error (_("Missing modifier."));
567 for (; *args
; ++args
)
578 modifiers
|= RECORD_PRINT_SRC_LINE
;
581 modifiers
|= RECORD_PRINT_INSN_RANGE
;
584 modifiers
|= RECORD_PRINT_INDENT_CALLS
;
587 error (_("Invalid modifier: %c."), *args
);
591 args
= skip_spaces (args
);
594 /* Update the argument string. */
600 /* The "record function-call-history" command. */
603 cmd_record_call_history (char *arg
, int from_tty
)
607 require_record_target ();
609 flags
= get_call_history_modifiers (&arg
);
611 size
= command_size_to_target_size (record_call_history_size
);
613 if (arg
== NULL
|| *arg
== 0 || strcmp (arg
, "+") == 0)
614 target_call_history (size
, flags
);
615 else if (strcmp (arg
, "-") == 0)
616 target_call_history (-size
, flags
);
621 begin
= get_insn_number (&arg
);
625 arg
= skip_spaces (++arg
);
630 size
= get_context_size (&arg
);
634 target_call_history_from (begin
, size
, flags
);
636 else if (*arg
== '-')
639 size
= get_context_size (&arg
);
643 target_call_history_from (begin
, -size
, flags
);
647 end
= get_insn_number (&arg
);
651 target_call_history_range (begin
, end
, flags
);
658 target_call_history_from (begin
, size
, flags
);
665 /* Helper for "set record instruction-history-size" and "set record
666 function-call-history-size" input validation. COMMAND_VAR is the
667 variable registered in the command as control variable. *SETTING
668 is the real setting the command allows changing. */
671 validate_history_size (unsigned int *command_var
, unsigned int *setting
)
673 if (*command_var
!= UINT_MAX
&& *command_var
> INT_MAX
)
675 unsigned int new_value
= *command_var
;
677 /* Restore previous value. */
678 *command_var
= *setting
;
679 error (_("integer %u out of range"), new_value
);
682 /* Commit new value. */
683 *setting
= *command_var
;
686 /* Called by do_setshow_command. We only want values in the
687 [0..INT_MAX] range, while the command's machinery accepts
688 [0..UINT_MAX]. See command_size_to_target_size. */
691 set_record_insn_history_size (char *args
, int from_tty
,
692 struct cmd_list_element
*c
)
694 validate_history_size (&record_insn_history_size_setshow_var
,
695 &record_insn_history_size
);
698 /* Called by do_setshow_command. We only want values in the
699 [0..INT_MAX] range, while the command's machinery accepts
700 [0..UINT_MAX]. See command_size_to_target_size. */
703 set_record_call_history_size (char *args
, int from_tty
,
704 struct cmd_list_element
*c
)
706 validate_history_size (&record_call_history_size_setshow_var
,
707 &record_call_history_size
);
710 /* Provide a prototype to silence -Wmissing-prototypes. */
711 extern initialize_file_ftype _initialize_record
;
714 _initialize_record (void)
716 struct cmd_list_element
*c
;
718 add_setshow_zuinteger_cmd ("record", no_class
, &record_debug
,
719 _("Set debugging of record/replay feature."),
720 _("Show debugging of record/replay feature."),
721 _("When enabled, debugging output for "
722 "record/replay feature is displayed."),
723 NULL
, show_record_debug
, &setdebuglist
,
726 add_setshow_uinteger_cmd ("instruction-history-size", no_class
,
727 &record_insn_history_size_setshow_var
, _("\
728 Set number of instructions to print in \"record instruction-history\"."), _("\
729 Show number of instructions to print in \"record instruction-history\"."), _("\
730 A size of \"unlimited\" means unlimited instructions. The default is 10."),
731 set_record_insn_history_size
, NULL
,
732 &set_record_cmdlist
, &show_record_cmdlist
);
734 add_setshow_uinteger_cmd ("function-call-history-size", no_class
,
735 &record_call_history_size_setshow_var
, _("\
736 Set number of function to print in \"record function-call-history\"."), _("\
737 Show number of functions to print in \"record function-call-history\"."), _("\
738 A size of \"unlimited\" means unlimited lines. The default is 10."),
739 set_record_call_history_size
, NULL
,
740 &set_record_cmdlist
, &show_record_cmdlist
);
742 c
= add_prefix_cmd ("record", class_obscure
, cmd_record_start
,
743 _("Start recording."),
744 &record_cmdlist
, "record ", 0, &cmdlist
);
745 set_cmd_completer (c
, filename_completer
);
747 add_com_alias ("rec", "record", class_obscure
, 1);
748 add_prefix_cmd ("record", class_support
, set_record_command
,
749 _("Set record options"), &set_record_cmdlist
,
750 "set record ", 0, &setlist
);
751 add_alias_cmd ("rec", "record", class_obscure
, 1, &setlist
);
752 add_prefix_cmd ("record", class_support
, show_record_command
,
753 _("Show record options"), &show_record_cmdlist
,
754 "show record ", 0, &showlist
);
755 add_alias_cmd ("rec", "record", class_obscure
, 1, &showlist
);
756 add_prefix_cmd ("record", class_support
, info_record_command
,
757 _("Info record options"), &info_record_cmdlist
,
758 "info record ", 0, &infolist
);
759 add_alias_cmd ("rec", "record", class_obscure
, 1, &infolist
);
761 c
= add_cmd ("save", class_obscure
, cmd_record_save
,
762 _("Save the execution log to a file.\n\
763 Argument is optional filename.\n\
764 Default filename is 'gdb_record.<process_id>'."),
766 set_cmd_completer (c
, filename_completer
);
768 add_cmd ("delete", class_obscure
, cmd_record_delete
,
769 _("Delete the rest of execution log and start recording it anew."),
771 add_alias_cmd ("d", "delete", class_obscure
, 1, &record_cmdlist
);
772 add_alias_cmd ("del", "delete", class_obscure
, 1, &record_cmdlist
);
774 add_cmd ("stop", class_obscure
, cmd_record_stop
,
775 _("Stop the record/replay target."),
777 add_alias_cmd ("s", "stop", class_obscure
, 1, &record_cmdlist
);
779 add_prefix_cmd ("goto", class_obscure
, cmd_record_goto
, _("\
780 Restore the program to its state at instruction number N.\n\
781 Argument is instruction number, as shown by 'info record'."),
782 &record_goto_cmdlist
, "record goto ", 1, &record_cmdlist
);
784 add_cmd ("begin", class_obscure
, cmd_record_goto_begin
,
785 _("Go to the beginning of the execution log."),
786 &record_goto_cmdlist
);
787 add_alias_cmd ("start", "begin", class_obscure
, 1, &record_goto_cmdlist
);
789 add_cmd ("end", class_obscure
, cmd_record_goto_end
,
790 _("Go to the end of the execution log."),
791 &record_goto_cmdlist
);
793 add_cmd ("instruction-history", class_obscure
, cmd_record_insn_history
, _("\
794 Print disassembled instructions stored in the execution log.\n\
795 With a /m modifier, source lines are included (if available).\n\
796 With a /r modifier, raw instructions in hex are included.\n\
797 With a /f modifier, function names are omitted.\n\
798 With a /p modifier, current position markers are omitted.\n\
799 With no argument, disassembles ten more instructions after the previous \
801 \"record instruction-history -\" disassembles ten instructions before a \
802 previous disassembly.\n\
803 One argument specifies an instruction number as shown by 'info record', and \
804 ten instructions are disassembled after that instruction.\n\
805 Two arguments with comma between them specify starting and ending instruction \
806 numbers to disassemble.\n\
807 If the second argument is preceded by '+' or '-', it specifies the distance \
808 from the first argument.\n\
809 The number of instructions to disassemble can be defined with \"set record \
810 instruction-history-size\"."),
813 add_cmd ("function-call-history", class_obscure
, cmd_record_call_history
, _("\
814 Prints the execution history at function granularity.\n\
815 It prints one line for each sequence of instructions that belong to the same \
817 Without modifiers, it prints the function name.\n\
818 With a /l modifier, the source file and line number range is included.\n\
819 With a /i modifier, the instruction number range is included.\n\
820 With a /c modifier, the output is indented based on the call stack depth.\n\
821 With no argument, prints ten more lines after the previous ten-line print.\n\
822 \"record function-call-history -\" prints ten lines before a previous ten-line \
824 One argument specifies a function number as shown by 'info record', and \
825 ten lines are printed after that function.\n\
826 Two arguments with comma between them specify a range of functions to print.\n\
827 If the second argument is preceded by '+' or '-', it specifies the distance \
828 from the first argument.\n\
829 The number of functions to print can be defined with \"set record \
830 function-call-history-size\"."),
833 /* Sync command control variables. */
834 record_insn_history_size_setshow_var
= record_insn_history_size
;
835 record_call_history_size_setshow_var
= record_call_history_size
;