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 ((char *) "record", from_tty);
104 error (_("Invalid format."));
106 else if (strcmp (method, "full") == 0)
109 execute_command_to_string ((char *) "record full", from_tty);
111 error (_("Invalid format."));
113 else if (strcmp (method, "btrace") == 0)
116 execute_command_to_string ((char *) "record btrace", from_tty);
117 else if (strcmp (format, "bts") == 0)
118 execute_command_to_string ((char *) "record btrace bts", from_tty);
119 else if (strcmp (format, "pt") == 0)
120 execute_command_to_string ((char *) "record btrace pt", from_tty);
122 error (_("Invalid format."));
125 error (_("Invalid method."));
131 record_stop (int from_tty)
133 execute_command_to_string ((char *) "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 (struct address_space *aspace, CORE_ADDR pc,
238 enum target_stop_reason *reason)
240 if (breakpoint_inserted_here_p (aspace, pc))
242 if (hardware_breakpoint_inserted_here_p (aspace, pc))
243 *reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
245 *reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
249 *reason = TARGET_STOPPED_BY_NO_REASON;
253 /* Implement "show record debug" command. */
256 show_record_debug (struct ui_file *file, int from_tty,
257 struct cmd_list_element *c, const char *value)
259 fprintf_filtered (file, _("Debugging of process record target is %s.\n"),
263 /* Alias for "target record". */
266 cmd_record_start (char *args, int from_tty)
268 execute_command ((char *) "target record-full", from_tty);
271 /* Truncate the record log from the present point
272 of replay until the end. */
275 cmd_record_delete (char *args, int from_tty)
277 require_record_target ();
279 if (!target_record_is_replaying (inferior_ptid))
281 printf_unfiltered (_("Already at end of record list.\n"));
285 if (!target_supports_delete_record ())
287 printf_unfiltered (_("The current record target does not support "
288 "this operation.\n"));
292 if (!from_tty || query (_("Delete the log from this point forward "
293 "and begin to record the running message "
295 target_delete_record ();
298 /* Implement the "stoprecord" or "record stop" command. */
301 cmd_record_stop (char *args, int from_tty)
303 struct target_ops *t;
305 t = require_record_target ();
310 printf_unfiltered (_("Process record is stopped and all execution "
311 "logs are deleted.\n"));
313 observer_notify_record_changed (current_inferior (), 0, NULL, NULL);
316 /* The "set record" command. */
319 set_record_command (char *args, int from_tty)
321 printf_unfiltered (_("\"set record\" must be followed "
322 "by an apporpriate subcommand.\n"));
323 help_list (set_record_cmdlist, "set record ", all_commands, gdb_stdout);
326 /* The "show record" command. */
329 show_record_command (char *args, int from_tty)
331 cmd_show_list (show_record_cmdlist, from_tty, "");
334 /* The "info record" command. */
337 info_record_command (char *args, int from_tty)
339 struct target_ops *t;
341 t = find_record_target ();
344 printf_filtered (_("No record target is currently active.\n"));
348 printf_filtered (_("Active record target: %s\n"), t->to_shortname);
349 t->to_info_record (t);
352 /* The "record save" command. */
355 cmd_record_save (char *args, int from_tty)
357 char *recfilename, recfilename_buffer[40];
359 require_record_target ();
361 if (args != NULL && *args != 0)
365 /* Default recfile name is "gdb_record.PID". */
366 xsnprintf (recfilename_buffer, sizeof (recfilename_buffer),
367 "gdb_record.%d", ptid_get_pid (inferior_ptid));
368 recfilename = recfilename_buffer;
371 target_save_record (recfilename);
377 record_goto (const char *arg)
381 if (arg == NULL || *arg == '\0')
382 error (_("Command requires an argument (insn number to go to)."));
384 insn = parse_and_eval_long (arg);
386 require_record_target ();
387 target_goto_record (insn);
390 /* "record goto" command. Argument is an instruction number,
391 as given by "info record".
393 Rewinds the recording (forward or backward) to the given instruction. */
396 cmd_record_goto (char *arg, int from_tty)
401 /* The "record goto begin" command. */
404 cmd_record_goto_begin (char *arg, int from_tty)
406 if (arg != NULL && *arg != '\0')
407 error (_("Junk after argument: %s."), arg);
409 require_record_target ();
410 target_goto_record_begin ();
413 /* The "record goto end" command. */
416 cmd_record_goto_end (char *arg, int from_tty)
418 if (arg != NULL && *arg != '\0')
419 error (_("Junk after argument: %s."), arg);
421 require_record_target ();
422 target_goto_record_end ();
425 /* Read an instruction number from an argument string. */
428 get_insn_number (char **arg)
431 const char *begin, *end, *pos;
434 pos = skip_spaces (begin);
437 error (_("Expected positive number, got: %s."), pos);
439 number = strtoulst (pos, &end, 10);
441 *arg += (end - begin);
446 /* Read a context size from an argument string. */
449 get_context_size (char **arg)
454 pos = skip_spaces (*arg);
457 error (_("Expected positive number, got: %s."), pos);
459 return strtol (pos, arg, 10);
462 /* Complain about junk at the end of an argument string. */
468 error (_("Junk after argument: %s."), arg);
471 /* Read instruction-history modifiers from an argument string. */
473 static gdb_disassembly_flags
474 get_insn_history_modifiers (char **arg)
476 gdb_disassembly_flags modifiers;
490 error (_("Missing modifier."));
492 for (; *args; ++args)
504 modifiers |= DISASSEMBLY_SOURCE;
505 modifiers |= DISASSEMBLY_FILENAME;
508 modifiers |= DISASSEMBLY_RAW_INSN;
511 modifiers |= DISASSEMBLY_OMIT_FNAME;
514 modifiers |= DISASSEMBLY_OMIT_PC;
517 error (_("Invalid modifier: %c."), *args);
521 args = skip_spaces (args);
524 /* Update the argument string. */
530 /* The "set record instruction-history-size / set record
531 function-call-history-size" commands are unsigned, with UINT_MAX
532 meaning unlimited. The target interfaces works with signed int
533 though, to indicate direction, so map "unlimited" to INT_MAX, which
534 is about the same as unlimited in practice. If the user does have
535 a log that huge, she can fetch it in chunks across several requests,
536 but she'll likely have other problems first... */
539 command_size_to_target_size (unsigned int size)
541 gdb_assert (size <= INT_MAX || size == UINT_MAX);
543 if (size == UINT_MAX)
549 /* The "record instruction-history" command. */
552 cmd_record_insn_history (char *arg, int from_tty)
554 require_record_target ();
556 gdb_disassembly_flags flags = get_insn_history_modifiers (&arg);
558 int size = command_size_to_target_size (record_insn_history_size);
560 if (arg == NULL || *arg == 0 || strcmp (arg, "+") == 0)
561 target_insn_history (size, flags);
562 else if (strcmp (arg, "-") == 0)
563 target_insn_history (-size, flags);
568 begin = get_insn_number (&arg);
572 arg = skip_spaces (++arg);
577 size = get_context_size (&arg);
581 target_insn_history_from (begin, size, flags);
583 else if (*arg == '-')
586 size = get_context_size (&arg);
590 target_insn_history_from (begin, -size, flags);
594 end = get_insn_number (&arg);
598 target_insn_history_range (begin, end, flags);
605 target_insn_history_from (begin, size, flags);
612 /* Read function-call-history modifiers from an argument string. */
615 get_call_history_modifiers (char **arg)
631 error (_("Missing modifier."));
633 for (; *args; ++args)
644 modifiers |= RECORD_PRINT_SRC_LINE;
647 modifiers |= RECORD_PRINT_INSN_RANGE;
650 modifiers |= RECORD_PRINT_INDENT_CALLS;
653 error (_("Invalid modifier: %c."), *args);
657 args = skip_spaces (args);
660 /* Update the argument string. */
666 /* The "record function-call-history" command. */
669 cmd_record_call_history (char *arg, int from_tty)
673 require_record_target ();
675 flags = get_call_history_modifiers (&arg);
677 size = command_size_to_target_size (record_call_history_size);
679 if (arg == NULL || *arg == 0 || strcmp (arg, "+") == 0)
680 target_call_history (size, flags);
681 else if (strcmp (arg, "-") == 0)
682 target_call_history (-size, flags);
687 begin = get_insn_number (&arg);
691 arg = skip_spaces (++arg);
696 size = get_context_size (&arg);
700 target_call_history_from (begin, size, flags);
702 else if (*arg == '-')
705 size = get_context_size (&arg);
709 target_call_history_from (begin, -size, flags);
713 end = get_insn_number (&arg);
717 target_call_history_range (begin, end, flags);
724 target_call_history_from (begin, size, flags);
731 /* Helper for "set record instruction-history-size" and "set record
732 function-call-history-size" input validation. COMMAND_VAR is the
733 variable registered in the command as control variable. *SETTING
734 is the real setting the command allows changing. */
737 validate_history_size (unsigned int *command_var, unsigned int *setting)
739 if (*command_var != UINT_MAX && *command_var > INT_MAX)
741 unsigned int new_value = *command_var;
743 /* Restore previous value. */
744 *command_var = *setting;
745 error (_("integer %u out of range"), new_value);
748 /* Commit new value. */
749 *setting = *command_var;
752 /* Called by do_setshow_command. We only want values in the
753 [0..INT_MAX] range, while the command's machinery accepts
754 [0..UINT_MAX]. See command_size_to_target_size. */
757 set_record_insn_history_size (char *args, int from_tty,
758 struct cmd_list_element *c)
760 validate_history_size (&record_insn_history_size_setshow_var,
761 &record_insn_history_size);
764 /* Called by do_setshow_command. We only want values in the
765 [0..INT_MAX] range, while the command's machinery accepts
766 [0..UINT_MAX]. See command_size_to_target_size. */
769 set_record_call_history_size (char *args, int from_tty,
770 struct cmd_list_element *c)
772 validate_history_size (&record_call_history_size_setshow_var,
773 &record_call_history_size);
777 _initialize_record (void)
779 struct cmd_list_element *c;
781 add_setshow_zuinteger_cmd ("record", no_class, &record_debug,
782 _("Set debugging of record/replay feature."),
783 _("Show debugging of record/replay feature."),
784 _("When enabled, debugging output for "
785 "record/replay feature is displayed."),
786 NULL, show_record_debug, &setdebuglist,
789 add_setshow_uinteger_cmd ("instruction-history-size", no_class,
790 &record_insn_history_size_setshow_var, _("\
791 Set number of instructions to print in \"record instruction-history\"."), _("\
792 Show number of instructions to print in \"record instruction-history\"."), _("\
793 A size of \"unlimited\" means unlimited instructions. The default is 10."),
794 set_record_insn_history_size, NULL,
795 &set_record_cmdlist, &show_record_cmdlist);
797 add_setshow_uinteger_cmd ("function-call-history-size", no_class,
798 &record_call_history_size_setshow_var, _("\
799 Set number of function to print in \"record function-call-history\"."), _("\
800 Show number of functions to print in \"record function-call-history\"."), _("\
801 A size of \"unlimited\" means unlimited lines. The default is 10."),
802 set_record_call_history_size, NULL,
803 &set_record_cmdlist, &show_record_cmdlist);
805 c = add_prefix_cmd ("record", class_obscure, cmd_record_start,
806 _("Start recording."),
807 &record_cmdlist, "record ", 0, &cmdlist);
808 set_cmd_completer (c, filename_completer);
810 add_com_alias ("rec", "record", class_obscure, 1);
811 add_prefix_cmd ("record", class_support, set_record_command,
812 _("Set record options"), &set_record_cmdlist,
813 "set record ", 0, &setlist);
814 add_alias_cmd ("rec", "record", class_obscure, 1, &setlist);
815 add_prefix_cmd ("record", class_support, show_record_command,
816 _("Show record options"), &show_record_cmdlist,
817 "show record ", 0, &showlist);
818 add_alias_cmd ("rec", "record", class_obscure, 1, &showlist);
819 add_prefix_cmd ("record", class_support, info_record_command,
820 _("Info record options"), &info_record_cmdlist,
821 "info record ", 0, &infolist);
822 add_alias_cmd ("rec", "record", class_obscure, 1, &infolist);
824 c = add_cmd ("save", class_obscure, cmd_record_save,
825 _("Save the execution log to a file.\n\
826 Argument is optional filename.\n\
827 Default filename is 'gdb_record.<process_id>'."),
829 set_cmd_completer (c, filename_completer);
831 add_cmd ("delete", class_obscure, cmd_record_delete,
832 _("Delete the rest of execution log and start recording it anew."),
834 add_alias_cmd ("d", "delete", class_obscure, 1, &record_cmdlist);
835 add_alias_cmd ("del", "delete", class_obscure, 1, &record_cmdlist);
837 add_cmd ("stop", class_obscure, cmd_record_stop,
838 _("Stop the record/replay target."),
840 add_alias_cmd ("s", "stop", class_obscure, 1, &record_cmdlist);
842 add_prefix_cmd ("goto", class_obscure, cmd_record_goto, _("\
843 Restore the program to its state at instruction number N.\n\
844 Argument is instruction number, as shown by 'info record'."),
845 &record_goto_cmdlist, "record goto ", 1, &record_cmdlist);
847 add_cmd ("begin", class_obscure, cmd_record_goto_begin,
848 _("Go to the beginning of the execution log."),
849 &record_goto_cmdlist);
850 add_alias_cmd ("start", "begin", class_obscure, 1, &record_goto_cmdlist);
852 add_cmd ("end", class_obscure, cmd_record_goto_end,
853 _("Go to the end of the execution log."),
854 &record_goto_cmdlist);
856 add_cmd ("instruction-history", class_obscure, cmd_record_insn_history, _("\
857 Print disassembled instructions stored in the execution log.\n\
858 With a /m or /s modifier, source lines are included (if available).\n\
859 With a /r modifier, raw instructions in hex are included.\n\
860 With a /f modifier, function names are omitted.\n\
861 With a /p modifier, current position markers are omitted.\n\
862 With no argument, disassembles ten more instructions after the previous \
864 \"record instruction-history -\" disassembles ten instructions before a \
865 previous disassembly.\n\
866 One argument specifies an instruction number as shown by 'info record', and \
867 ten instructions are disassembled after that instruction.\n\
868 Two arguments with comma between them specify starting and ending instruction \
869 numbers to disassemble.\n\
870 If the second argument is preceded by '+' or '-', it specifies the distance \
871 from the first argument.\n\
872 The number of instructions to disassemble can be defined with \"set record \
873 instruction-history-size\"."),
876 add_cmd ("function-call-history", class_obscure, cmd_record_call_history, _("\
877 Prints the execution history at function granularity.\n\
878 It prints one line for each sequence of instructions that belong to the same \
880 Without modifiers, it prints the function name.\n\
881 With a /l modifier, the source file and line number range is included.\n\
882 With a /i modifier, the instruction number range is included.\n\
883 With a /c modifier, the output is indented based on the call stack depth.\n\
884 With no argument, prints ten more lines after the previous ten-line print.\n\
885 \"record function-call-history -\" prints ten lines before a previous ten-line \
887 One argument specifies a function number as shown by 'info record', and \
888 ten lines are printed after that function.\n\
889 Two arguments with comma between them specify a range of functions to print.\n\
890 If the second argument is preceded by '+' or '-', it specifies the distance \
891 from the first argument.\n\
892 The number of functions to print can be defined with \"set record \
893 function-call-history-size\"."),
896 /* Sync command control variables. */
897 record_insn_history_size_setshow_var = record_insn_history_size;
898 record_call_history_size_setshow_var = record_call_history_size;