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 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, 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 ();
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. */
192 /* Implement "show record debug" command. */
195 show_record_debug (struct ui_file *file, int from_tty,
196 struct cmd_list_element *c, const char *value)
198 fprintf_filtered (file, _("Debugging of process record target is %s.\n"),
202 /* Alias for "target record". */
205 cmd_record_start (char *args, int from_tty)
207 execute_command ("target record-full", from_tty);
210 /* Truncate the record log from the present point
211 of replay until the end. */
214 cmd_record_delete (char *args, int from_tty)
216 require_record_target ();
218 if (!target_record_is_replaying ())
220 printf_unfiltered (_("Already at end of record list.\n"));
224 if (!target_supports_delete_record ())
226 printf_unfiltered (_("The current record target does not support "
227 "this operation.\n"));
231 if (!from_tty || query (_("Delete the log from this point forward "
232 "and begin to record the running message "
234 target_delete_record ();
237 /* Implement the "stoprecord" or "record stop" command. */
240 cmd_record_stop (char *args, int from_tty)
242 struct target_ops *t;
244 t = require_record_target ();
249 printf_unfiltered (_("Process record is stopped and all execution "
250 "logs are deleted.\n"));
252 observer_notify_record_changed (current_inferior (), 0);
255 /* The "set record" command. */
258 set_record_command (char *args, int from_tty)
260 printf_unfiltered (_("\"set record\" must be followed "
261 "by an apporpriate subcommand.\n"));
262 help_list (set_record_cmdlist, "set record ", all_commands, gdb_stdout);
265 /* The "show record" command. */
268 show_record_command (char *args, int from_tty)
270 cmd_show_list (show_record_cmdlist, from_tty, "");
273 /* The "info record" command. */
276 info_record_command (char *args, int from_tty)
278 struct target_ops *t;
280 t = find_record_target ();
283 printf_filtered (_("No record target is currently active.\n"));
287 printf_filtered (_("Active record target: %s\n"), t->to_shortname);
288 if (t->to_info_record != NULL)
289 t->to_info_record (t);
292 /* The "record save" command. */
295 cmd_record_save (char *args, int from_tty)
297 char *recfilename, recfilename_buffer[40];
299 require_record_target ();
301 if (args != NULL && *args != 0)
305 /* Default recfile name is "gdb_record.PID". */
306 xsnprintf (recfilename_buffer, sizeof (recfilename_buffer),
307 "gdb_record.%d", ptid_get_pid (inferior_ptid));
308 recfilename = recfilename_buffer;
311 target_save_record (recfilename);
314 /* "record goto" command. Argument is an instruction number,
315 as given by "info record".
317 Rewinds the recording (forward or backward) to the given instruction. */
320 cmd_record_goto (char *arg, int from_tty)
324 if (arg == NULL || *arg == '\0')
325 error (_("Command requires an argument (insn number to go to)."));
327 insn = parse_and_eval_long (arg);
329 require_record_target ();
330 target_goto_record (insn);
333 /* The "record goto begin" command. */
336 cmd_record_goto_begin (char *arg, int from_tty)
338 if (arg != NULL && *arg != '\0')
339 error (_("Junk after argument: %s."), arg);
341 require_record_target ();
342 target_goto_record_begin ();
345 /* The "record goto end" command. */
348 cmd_record_goto_end (char *arg, int from_tty)
350 if (arg != NULL && *arg != '\0')
351 error (_("Junk after argument: %s."), arg);
353 require_record_target ();
354 target_goto_record_end ();
357 /* Read an instruction number from an argument string. */
360 get_insn_number (char **arg)
363 const char *begin, *end, *pos;
366 pos = skip_spaces_const (begin);
369 error (_("Expected positive number, got: %s."), pos);
371 number = strtoulst (pos, &end, 10);
373 *arg += (end - begin);
378 /* Read a context size from an argument string. */
381 get_context_size (char **arg)
386 pos = skip_spaces (*arg);
389 error (_("Expected positive number, got: %s."), pos);
391 return strtol (pos, arg, 10);
394 /* Complain about junk at the end of an argument string. */
400 error (_("Junk after argument: %s."), arg);
403 /* Read instruction-history modifiers from an argument string. */
406 get_insn_history_modifiers (char **arg)
422 error (_("Missing modifier."));
424 for (; *args; ++args)
435 modifiers |= DISASSEMBLY_SOURCE;
436 modifiers |= DISASSEMBLY_FILENAME;
439 modifiers |= DISASSEMBLY_RAW_INSN;
442 modifiers |= DISASSEMBLY_OMIT_FNAME;
445 modifiers |= DISASSEMBLY_OMIT_PC;
448 error (_("Invalid modifier: %c."), *args);
452 args = skip_spaces (args);
455 /* Update the argument string. */
461 /* The "set record instruction-history-size / set record
462 function-call-history-size" commands are unsigned, with UINT_MAX
463 meaning unlimited. The target interfaces works with signed int
464 though, to indicate direction, so map "unlimited" to INT_MAX, which
465 is about the same as unlimited in practice. If the user does have
466 a log that huge, she can fetch it in chunks across several requests,
467 but she'll likely have other problems first... */
470 command_size_to_target_size (unsigned int size)
472 gdb_assert (size <= INT_MAX || size == UINT_MAX);
474 if (size == UINT_MAX)
480 /* The "record instruction-history" command. */
483 cmd_record_insn_history (char *arg, int from_tty)
487 require_record_target ();
489 flags = get_insn_history_modifiers (&arg);
491 size = command_size_to_target_size (record_insn_history_size);
493 if (arg == NULL || *arg == 0 || strcmp (arg, "+") == 0)
494 target_insn_history (size, flags);
495 else if (strcmp (arg, "-") == 0)
496 target_insn_history (-size, flags);
501 begin = get_insn_number (&arg);
505 arg = skip_spaces (++arg);
510 size = get_context_size (&arg);
514 target_insn_history_from (begin, size, flags);
516 else if (*arg == '-')
519 size = get_context_size (&arg);
523 target_insn_history_from (begin, -size, flags);
527 end = get_insn_number (&arg);
531 target_insn_history_range (begin, end, flags);
538 target_insn_history_from (begin, size, flags);
545 /* Read function-call-history modifiers from an argument string. */
548 get_call_history_modifiers (char **arg)
564 error (_("Missing modifier."));
566 for (; *args; ++args)
577 modifiers |= RECORD_PRINT_SRC_LINE;
580 modifiers |= RECORD_PRINT_INSN_RANGE;
583 modifiers |= RECORD_PRINT_INDENT_CALLS;
586 error (_("Invalid modifier: %c."), *args);
590 args = skip_spaces (args);
593 /* Update the argument string. */
599 /* The "record function-call-history" command. */
602 cmd_record_call_history (char *arg, int from_tty)
606 require_record_target ();
608 flags = get_call_history_modifiers (&arg);
610 size = command_size_to_target_size (record_call_history_size);
612 if (arg == NULL || *arg == 0 || strcmp (arg, "+") == 0)
613 target_call_history (size, flags);
614 else if (strcmp (arg, "-") == 0)
615 target_call_history (-size, flags);
620 begin = get_insn_number (&arg);
624 arg = skip_spaces (++arg);
629 size = get_context_size (&arg);
633 target_call_history_from (begin, size, flags);
635 else if (*arg == '-')
638 size = get_context_size (&arg);
642 target_call_history_from (begin, -size, flags);
646 end = get_insn_number (&arg);
650 target_call_history_range (begin, end, flags);
657 target_call_history_from (begin, size, flags);
664 /* Helper for "set record instruction-history-size" and "set record
665 function-call-history-size" input validation. COMMAND_VAR is the
666 variable registered in the command as control variable. *SETTING
667 is the real setting the command allows changing. */
670 validate_history_size (unsigned int *command_var, unsigned int *setting)
672 if (*command_var != UINT_MAX && *command_var > INT_MAX)
674 unsigned int new_value = *command_var;
676 /* Restore previous value. */
677 *command_var = *setting;
678 error (_("integer %u out of range"), new_value);
681 /* Commit new value. */
682 *setting = *command_var;
685 /* Called by do_setshow_command. We only want values in the
686 [0..INT_MAX] range, while the command's machinery accepts
687 [0..UINT_MAX]. See command_size_to_target_size. */
690 set_record_insn_history_size (char *args, int from_tty,
691 struct cmd_list_element *c)
693 validate_history_size (&record_insn_history_size_setshow_var,
694 &record_insn_history_size);
697 /* Called by do_setshow_command. We only want values in the
698 [0..INT_MAX] range, while the command's machinery accepts
699 [0..UINT_MAX]. See command_size_to_target_size. */
702 set_record_call_history_size (char *args, int from_tty,
703 struct cmd_list_element *c)
705 validate_history_size (&record_call_history_size_setshow_var,
706 &record_call_history_size);
709 /* Provide a prototype to silence -Wmissing-prototypes. */
710 extern initialize_file_ftype _initialize_record;
713 _initialize_record (void)
715 struct cmd_list_element *c;
717 add_setshow_zuinteger_cmd ("record", no_class, &record_debug,
718 _("Set debugging of record/replay feature."),
719 _("Show debugging of record/replay feature."),
720 _("When enabled, debugging output for "
721 "record/replay feature is displayed."),
722 NULL, show_record_debug, &setdebuglist,
725 add_setshow_uinteger_cmd ("instruction-history-size", no_class,
726 &record_insn_history_size_setshow_var, _("\
727 Set number of instructions to print in \"record instruction-history\"."), _("\
728 Show number of instructions to print in \"record instruction-history\"."), _("\
729 A size of \"unlimited\" means unlimited instructions. The default is 10."),
730 set_record_insn_history_size, NULL,
731 &set_record_cmdlist, &show_record_cmdlist);
733 add_setshow_uinteger_cmd ("function-call-history-size", no_class,
734 &record_call_history_size_setshow_var, _("\
735 Set number of function to print in \"record function-call-history\"."), _("\
736 Show number of functions to print in \"record function-call-history\"."), _("\
737 A size of \"unlimited\" means unlimited lines. The default is 10."),
738 set_record_call_history_size, NULL,
739 &set_record_cmdlist, &show_record_cmdlist);
741 c = add_prefix_cmd ("record", class_obscure, cmd_record_start,
742 _("Start recording."),
743 &record_cmdlist, "record ", 0, &cmdlist);
744 set_cmd_completer (c, filename_completer);
746 add_com_alias ("rec", "record", class_obscure, 1);
747 add_prefix_cmd ("record", class_support, set_record_command,
748 _("Set record options"), &set_record_cmdlist,
749 "set record ", 0, &setlist);
750 add_alias_cmd ("rec", "record", class_obscure, 1, &setlist);
751 add_prefix_cmd ("record", class_support, show_record_command,
752 _("Show record options"), &show_record_cmdlist,
753 "show record ", 0, &showlist);
754 add_alias_cmd ("rec", "record", class_obscure, 1, &showlist);
755 add_prefix_cmd ("record", class_support, info_record_command,
756 _("Info record options"), &info_record_cmdlist,
757 "info record ", 0, &infolist);
758 add_alias_cmd ("rec", "record", class_obscure, 1, &infolist);
760 c = add_cmd ("save", class_obscure, cmd_record_save,
761 _("Save the execution log to a file.\n\
762 Argument is optional filename.\n\
763 Default filename is 'gdb_record.<process_id>'."),
765 set_cmd_completer (c, filename_completer);
767 add_cmd ("delete", class_obscure, cmd_record_delete,
768 _("Delete the rest of execution log and start recording it anew."),
770 add_alias_cmd ("d", "delete", class_obscure, 1, &record_cmdlist);
771 add_alias_cmd ("del", "delete", class_obscure, 1, &record_cmdlist);
773 add_cmd ("stop", class_obscure, cmd_record_stop,
774 _("Stop the record/replay target."),
776 add_alias_cmd ("s", "stop", class_obscure, 1, &record_cmdlist);
778 add_prefix_cmd ("goto", class_obscure, cmd_record_goto, _("\
779 Restore the program to its state at instruction number N.\n\
780 Argument is instruction number, as shown by 'info record'."),
781 &record_goto_cmdlist, "record goto ", 1, &record_cmdlist);
783 add_cmd ("begin", class_obscure, cmd_record_goto_begin,
784 _("Go to the beginning of the execution log."),
785 &record_goto_cmdlist);
786 add_alias_cmd ("start", "begin", class_obscure, 1, &record_goto_cmdlist);
788 add_cmd ("end", class_obscure, cmd_record_goto_end,
789 _("Go to the end of the execution log."),
790 &record_goto_cmdlist);
792 add_cmd ("instruction-history", class_obscure, cmd_record_insn_history, _("\
793 Print disassembled instructions stored in the execution log.\n\
794 With a /m modifier, source lines are included (if available).\n\
795 With a /r modifier, raw instructions in hex are included.\n\
796 With a /f modifier, function names are omitted.\n\
797 With a /p modifier, current position markers are omitted.\n\
798 With no argument, disassembles ten more instructions after the previous \
800 \"record instruction-history -\" disassembles ten instructions before a \
801 previous disassembly.\n\
802 One argument specifies an instruction number as shown by 'info record', and \
803 ten instructions are disassembled after that instruction.\n\
804 Two arguments with comma between them specify starting and ending instruction \
805 numbers to disassemble.\n\
806 If the second argument is preceded by '+' or '-', it specifies the distance \
807 from the first argument.\n\
808 The number of instructions to disassemble can be defined with \"set record \
809 instruction-history-size\"."),
812 add_cmd ("function-call-history", class_obscure, cmd_record_call_history, _("\
813 Prints the execution history at function granularity.\n\
814 It prints one line for each sequence of instructions that belong to the same \
816 Without modifiers, it prints the function name.\n\
817 With a /l modifier, the source file and line number range is included.\n\
818 With a /i modifier, the instruction number range is included.\n\
819 With a /c modifier, the output is indented based on the call stack depth.\n\
820 With no argument, prints ten more lines after the previous ten-line print.\n\
821 \"record function-call-history -\" prints ten lines before a previous ten-line \
823 One argument specifies a function number as shown by 'info record', and \
824 ten lines are printed after that function.\n\
825 Two arguments with comma between them specify a range of functions to print.\n\
826 If the second argument is preceded by '+' or '-', it specifies the distance \
827 from the first argument.\n\
828 The number of functions to print can be defined with \"set record \
829 function-call-history-size\"."),
832 /* Sync command control variables. */
833 record_insn_history_size_setshow_var = record_insn_history_size;
834 record_call_history_size_setshow_var = record_call_history_size;