1 /* Process record and replay target for GDB, the GNU debugger.
3 Copyright (C) 2008-2017 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_start (const char *method, const char *format, int from_tty)
102 execute_command_to_string ("record", from_tty);
104 error (_("Invalid format."));
106 else if (strcmp (method, "full") == 0)
109 execute_command_to_string ("record full", from_tty);
111 error (_("Invalid format."));
113 else if (strcmp (method, "btrace") == 0)
116 execute_command_to_string ("record btrace", from_tty);
117 else if (strcmp (format, "bts") == 0)
118 execute_command_to_string ("record btrace bts", from_tty);
119 else if (strcmp (format, "pt") == 0)
120 execute_command_to_string ("record btrace pt", from_tty);
122 error (_("Invalid format."));
125 error (_("Invalid method."));
131 record_stop (int from_tty)
133 execute_command_to_string ("record stop", from_tty);
139 record_read_memory (struct gdbarch *gdbarch,
140 CORE_ADDR memaddr, gdb_byte *myaddr,
143 int ret = target_read_memory (memaddr, myaddr, len);
146 DEBUG ("error reading memory at addr %s len = %ld.\n",
147 paddress (gdbarch, memaddr), (long) len);
152 /* Stop recording. */
155 record_stop (struct target_ops *t)
157 DEBUG ("stop %s", t->to_shortname);
159 t->to_stop_recording (t);
162 /* Unpush the record target. */
165 record_unpush (struct target_ops *t)
167 DEBUG ("unpush %s", t->to_shortname);
175 record_disconnect (struct target_ops *t, const char *args, int from_tty)
177 gdb_assert (t->to_stratum == record_stratum);
179 DEBUG ("disconnect %s", t->to_shortname);
184 target_disconnect (args, from_tty);
190 record_detach (struct target_ops *t, const char *args, int from_tty)
192 gdb_assert (t->to_stratum == record_stratum);
194 DEBUG ("detach %s", t->to_shortname);
199 target_detach (args, from_tty);
205 record_mourn_inferior (struct target_ops *t)
207 gdb_assert (t->to_stratum == record_stratum);
209 DEBUG ("mourn inferior %s", t->to_shortname);
211 /* It is safer to not stop recording. Resources will be freed when
212 threads are discarded. */
215 target_mourn_inferior (inferior_ptid);
221 record_kill (struct target_ops *t)
223 gdb_assert (t->to_stratum == record_stratum);
225 DEBUG ("kill %s", t->to_shortname);
227 /* It is safer to not stop recording. Resources will be freed when
228 threads are discarded. */
237 record_check_stopped_by_breakpoint (const address_space *aspace,
239 enum target_stop_reason *reason)
241 if (breakpoint_inserted_here_p (aspace, pc))
243 if (hardware_breakpoint_inserted_here_p (aspace, pc))
244 *reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
246 *reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
250 *reason = TARGET_STOPPED_BY_NO_REASON;
254 /* Implement "show record debug" command. */
257 show_record_debug (struct ui_file *file, int from_tty,
258 struct cmd_list_element *c, const char *value)
260 fprintf_filtered (file, _("Debugging of process record target is %s.\n"),
264 /* Alias for "target record". */
267 cmd_record_start (const char *args, int from_tty)
269 execute_command ("target record-full", from_tty);
272 /* Truncate the record log from the present point
273 of replay until the end. */
276 cmd_record_delete (const char *args, int from_tty)
278 require_record_target ();
280 if (!target_record_is_replaying (inferior_ptid))
282 printf_unfiltered (_("Already at end of record list.\n"));
286 if (!target_supports_delete_record ())
288 printf_unfiltered (_("The current record target does not support "
289 "this operation.\n"));
293 if (!from_tty || query (_("Delete the log from this point forward "
294 "and begin to record the running message "
296 target_delete_record ();
299 /* Implement the "stoprecord" or "record stop" command. */
302 cmd_record_stop (const char *args, int from_tty)
304 struct target_ops *t;
306 t = require_record_target ();
311 printf_unfiltered (_("Process record is stopped and all execution "
312 "logs are deleted.\n"));
314 observer_notify_record_changed (current_inferior (), 0, NULL, NULL);
317 /* The "set record" command. */
320 set_record_command (const char *args, int from_tty)
322 printf_unfiltered (_("\"set record\" must be followed "
323 "by an apporpriate subcommand.\n"));
324 help_list (set_record_cmdlist, "set record ", all_commands, gdb_stdout);
327 /* The "show record" command. */
330 show_record_command (const char *args, int from_tty)
332 cmd_show_list (show_record_cmdlist, from_tty, "");
335 /* The "info record" command. */
338 info_record_command (const char *args, int from_tty)
340 struct target_ops *t;
342 t = find_record_target ();
345 printf_filtered (_("No record target is currently active.\n"));
349 printf_filtered (_("Active record target: %s\n"), t->to_shortname);
350 t->to_info_record (t);
353 /* The "record save" command. */
356 cmd_record_save (const char *args, int from_tty)
358 const char *recfilename;
359 char recfilename_buffer[40];
361 require_record_target ();
363 if (args != NULL && *args != 0)
367 /* Default recfile name is "gdb_record.PID". */
368 xsnprintf (recfilename_buffer, sizeof (recfilename_buffer),
369 "gdb_record.%d", ptid_get_pid (inferior_ptid));
370 recfilename = recfilename_buffer;
373 target_save_record (recfilename);
379 record_goto (const char *arg)
383 if (arg == NULL || *arg == '\0')
384 error (_("Command requires an argument (insn number to go to)."));
386 insn = parse_and_eval_long (arg);
388 require_record_target ();
389 target_goto_record (insn);
392 /* "record goto" command. Argument is an instruction number,
393 as given by "info record".
395 Rewinds the recording (forward or backward) to the given instruction. */
398 cmd_record_goto (const char *arg, int from_tty)
403 /* The "record goto begin" command. */
406 cmd_record_goto_begin (const char *arg, int from_tty)
408 if (arg != NULL && *arg != '\0')
409 error (_("Junk after argument: %s."), arg);
411 require_record_target ();
412 target_goto_record_begin ();
415 /* The "record goto end" command. */
418 cmd_record_goto_end (const char *arg, int from_tty)
420 if (arg != NULL && *arg != '\0')
421 error (_("Junk after argument: %s."), arg);
423 require_record_target ();
424 target_goto_record_end ();
427 /* Read an instruction number from an argument string. */
430 get_insn_number (const char **arg)
433 const char *begin, *end, *pos;
436 pos = skip_spaces (begin);
439 error (_("Expected positive number, got: %s."), pos);
441 number = strtoulst (pos, &end, 10);
443 *arg += (end - begin);
448 /* Read a context size from an argument string. */
451 get_context_size (const char **arg)
457 pos = skip_spaces (*arg);
460 error (_("Expected positive number, got: %s."), pos);
462 long result = strtol (pos, &end, 10);
467 /* Complain about junk at the end of an argument string. */
470 no_chunk (const char *arg)
473 error (_("Junk after argument: %s."), arg);
476 /* Read instruction-history modifiers from an argument string. */
478 static gdb_disassembly_flags
479 get_insn_history_modifiers (const char **arg)
481 gdb_disassembly_flags modifiers;
495 error (_("Missing modifier."));
497 for (; *args; ++args)
509 modifiers |= DISASSEMBLY_SOURCE;
510 modifiers |= DISASSEMBLY_FILENAME;
513 modifiers |= DISASSEMBLY_RAW_INSN;
516 modifiers |= DISASSEMBLY_OMIT_FNAME;
519 modifiers |= DISASSEMBLY_OMIT_PC;
522 error (_("Invalid modifier: %c."), *args);
526 args = skip_spaces (args);
529 /* Update the argument string. */
535 /* The "set record instruction-history-size / set record
536 function-call-history-size" commands are unsigned, with UINT_MAX
537 meaning unlimited. The target interfaces works with signed int
538 though, to indicate direction, so map "unlimited" to INT_MAX, which
539 is about the same as unlimited in practice. If the user does have
540 a log that huge, she can fetch it in chunks across several requests,
541 but she'll likely have other problems first... */
544 command_size_to_target_size (unsigned int size)
546 gdb_assert (size <= INT_MAX || size == UINT_MAX);
548 if (size == UINT_MAX)
554 /* The "record instruction-history" command. */
557 cmd_record_insn_history (const char *arg, int from_tty)
559 require_record_target ();
561 gdb_disassembly_flags flags = get_insn_history_modifiers (&arg);
563 int size = command_size_to_target_size (record_insn_history_size);
565 if (arg == NULL || *arg == 0 || strcmp (arg, "+") == 0)
566 target_insn_history (size, flags);
567 else if (strcmp (arg, "-") == 0)
568 target_insn_history (-size, flags);
573 begin = get_insn_number (&arg);
577 arg = skip_spaces (++arg);
582 size = get_context_size (&arg);
586 target_insn_history_from (begin, size, flags);
588 else if (*arg == '-')
591 size = get_context_size (&arg);
595 target_insn_history_from (begin, -size, flags);
599 end = get_insn_number (&arg);
603 target_insn_history_range (begin, end, flags);
610 target_insn_history_from (begin, size, flags);
617 /* Read function-call-history modifiers from an argument string. */
620 get_call_history_modifiers (const char **arg)
636 error (_("Missing modifier."));
638 for (; *args; ++args)
649 modifiers |= RECORD_PRINT_SRC_LINE;
652 modifiers |= RECORD_PRINT_INSN_RANGE;
655 modifiers |= RECORD_PRINT_INDENT_CALLS;
658 error (_("Invalid modifier: %c."), *args);
662 args = skip_spaces (args);
665 /* Update the argument string. */
671 /* The "record function-call-history" command. */
674 cmd_record_call_history (const char *arg, int from_tty)
678 require_record_target ();
680 flags = get_call_history_modifiers (&arg);
682 size = command_size_to_target_size (record_call_history_size);
684 if (arg == NULL || *arg == 0 || strcmp (arg, "+") == 0)
685 target_call_history (size, flags);
686 else if (strcmp (arg, "-") == 0)
687 target_call_history (-size, flags);
692 begin = get_insn_number (&arg);
696 arg = skip_spaces (++arg);
701 size = get_context_size (&arg);
705 target_call_history_from (begin, size, flags);
707 else if (*arg == '-')
710 size = get_context_size (&arg);
714 target_call_history_from (begin, -size, flags);
718 end = get_insn_number (&arg);
722 target_call_history_range (begin, end, flags);
729 target_call_history_from (begin, size, flags);
736 /* Helper for "set record instruction-history-size" and "set record
737 function-call-history-size" input validation. COMMAND_VAR is the
738 variable registered in the command as control variable. *SETTING
739 is the real setting the command allows changing. */
742 validate_history_size (unsigned int *command_var, unsigned int *setting)
744 if (*command_var != UINT_MAX && *command_var > INT_MAX)
746 unsigned int new_value = *command_var;
748 /* Restore previous value. */
749 *command_var = *setting;
750 error (_("integer %u out of range"), new_value);
753 /* Commit new value. */
754 *setting = *command_var;
757 /* Called by do_setshow_command. We only want values in the
758 [0..INT_MAX] range, while the command's machinery accepts
759 [0..UINT_MAX]. See command_size_to_target_size. */
762 set_record_insn_history_size (const char *args, int from_tty,
763 struct cmd_list_element *c)
765 validate_history_size (&record_insn_history_size_setshow_var,
766 &record_insn_history_size);
769 /* Called by do_setshow_command. We only want values in the
770 [0..INT_MAX] range, while the command's machinery accepts
771 [0..UINT_MAX]. See command_size_to_target_size. */
774 set_record_call_history_size (const char *args, int from_tty,
775 struct cmd_list_element *c)
777 validate_history_size (&record_call_history_size_setshow_var,
778 &record_call_history_size);
782 _initialize_record (void)
784 struct cmd_list_element *c;
786 add_setshow_zuinteger_cmd ("record", no_class, &record_debug,
787 _("Set debugging of record/replay feature."),
788 _("Show debugging of record/replay feature."),
789 _("When enabled, debugging output for "
790 "record/replay feature is displayed."),
791 NULL, show_record_debug, &setdebuglist,
794 add_setshow_uinteger_cmd ("instruction-history-size", no_class,
795 &record_insn_history_size_setshow_var, _("\
796 Set number of instructions to print in \"record instruction-history\"."), _("\
797 Show number of instructions to print in \"record instruction-history\"."), _("\
798 A size of \"unlimited\" means unlimited instructions. The default is 10."),
799 set_record_insn_history_size, NULL,
800 &set_record_cmdlist, &show_record_cmdlist);
802 add_setshow_uinteger_cmd ("function-call-history-size", no_class,
803 &record_call_history_size_setshow_var, _("\
804 Set number of function to print in \"record function-call-history\"."), _("\
805 Show number of functions to print in \"record function-call-history\"."), _("\
806 A size of \"unlimited\" means unlimited lines. The default is 10."),
807 set_record_call_history_size, NULL,
808 &set_record_cmdlist, &show_record_cmdlist);
810 c = add_prefix_cmd ("record", class_obscure, cmd_record_start,
811 _("Start recording."),
812 &record_cmdlist, "record ", 0, &cmdlist);
813 set_cmd_completer (c, filename_completer);
815 add_com_alias ("rec", "record", class_obscure, 1);
816 add_prefix_cmd ("record", class_support, set_record_command,
817 _("Set record options"), &set_record_cmdlist,
818 "set record ", 0, &setlist);
819 add_alias_cmd ("rec", "record", class_obscure, 1, &setlist);
820 add_prefix_cmd ("record", class_support, show_record_command,
821 _("Show record options"), &show_record_cmdlist,
822 "show record ", 0, &showlist);
823 add_alias_cmd ("rec", "record", class_obscure, 1, &showlist);
824 add_prefix_cmd ("record", class_support, info_record_command,
825 _("Info record options"), &info_record_cmdlist,
826 "info record ", 0, &infolist);
827 add_alias_cmd ("rec", "record", class_obscure, 1, &infolist);
829 c = add_cmd ("save", class_obscure, cmd_record_save,
830 _("Save the execution log to a file.\n\
831 Argument is optional filename.\n\
832 Default filename is 'gdb_record.<process_id>'."),
834 set_cmd_completer (c, filename_completer);
836 add_cmd ("delete", class_obscure, cmd_record_delete,
837 _("Delete the rest of execution log and start recording it anew."),
839 add_alias_cmd ("d", "delete", class_obscure, 1, &record_cmdlist);
840 add_alias_cmd ("del", "delete", class_obscure, 1, &record_cmdlist);
842 add_cmd ("stop", class_obscure, cmd_record_stop,
843 _("Stop the record/replay target."),
845 add_alias_cmd ("s", "stop", class_obscure, 1, &record_cmdlist);
847 add_prefix_cmd ("goto", class_obscure, cmd_record_goto, _("\
848 Restore the program to its state at instruction number N.\n\
849 Argument is instruction number, as shown by 'info record'."),
850 &record_goto_cmdlist, "record goto ", 1, &record_cmdlist);
852 add_cmd ("begin", class_obscure, cmd_record_goto_begin,
853 _("Go to the beginning of the execution log."),
854 &record_goto_cmdlist);
855 add_alias_cmd ("start", "begin", class_obscure, 1, &record_goto_cmdlist);
857 add_cmd ("end", class_obscure, cmd_record_goto_end,
858 _("Go to the end of the execution log."),
859 &record_goto_cmdlist);
861 add_cmd ("instruction-history", class_obscure, cmd_record_insn_history, _("\
862 Print disassembled instructions stored in the execution log.\n\
863 With a /m or /s modifier, source lines are included (if available).\n\
864 With a /r modifier, raw instructions in hex are included.\n\
865 With a /f modifier, function names are omitted.\n\
866 With a /p modifier, current position markers are omitted.\n\
867 With no argument, disassembles ten more instructions after the previous \
869 \"record instruction-history -\" disassembles ten instructions before a \
870 previous disassembly.\n\
871 One argument specifies an instruction number as shown by 'info record', and \
872 ten instructions are disassembled after that instruction.\n\
873 Two arguments with comma between them specify starting and ending instruction \
874 numbers to disassemble.\n\
875 If the second argument is preceded by '+' or '-', it specifies the distance \
876 from the first argument.\n\
877 The number of instructions to disassemble can be defined with \"set record \
878 instruction-history-size\"."),
881 add_cmd ("function-call-history", class_obscure, cmd_record_call_history, _("\
882 Prints the execution history at function granularity.\n\
883 It prints one line for each sequence of instructions that belong to the same \
885 Without modifiers, it prints the function name.\n\
886 With a /l modifier, the source file and line number range is included.\n\
887 With a /i modifier, the instruction number range is included.\n\
888 With a /c modifier, the output is indented based on the call stack depth.\n\
889 With no argument, prints ten more lines after the previous ten-line print.\n\
890 \"record function-call-history -\" prints ten lines before a previous ten-line \
892 One argument specifies a function number as shown by 'info record', and \
893 ten lines are printed after that function.\n\
894 Two arguments with comma between them specify a range of functions to print.\n\
895 If the second argument is preceded by '+' or '-', it specifies the distance \
896 from the first argument.\n\
897 The number of functions to print can be defined with \"set record \
898 function-call-history-size\"."),
901 /* Sync command control variables. */
902 record_insn_history_size_setshow_var = record_insn_history_size;
903 record_call_history_size_setshow_var = record_call_history_size;