1 /* Process record and replay target for GDB, the GNU debugger.
3 Copyright (C) 2008-2013 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)
60 /* Find the record target in the target stack. */
62 static struct target_ops *
63 find_record_target (void)
67 for (t = current_target.beneath; t != NULL; t = t->beneath)
68 if (t->to_stratum == record_stratum)
74 /* Check that recording is active. Throw an error, if it isn't. */
76 static struct target_ops *
77 require_record_target (void)
81 t = find_record_target ();
83 error (_("No record target is currently active.\n"
84 "Use one of the \"target record-<tab><tab>\" commands first."));
92 record_read_memory (struct gdbarch *gdbarch,
93 CORE_ADDR memaddr, gdb_byte *myaddr,
96 int ret = target_read_memory (memaddr, myaddr, len);
99 DEBUG ("error reading memory at addr %s len = %ld.\n",
100 paddress (gdbarch, memaddr), (long) len);
105 /* Stop recording. */
108 record_stop (struct target_ops *t)
110 DEBUG ("stop %s", t->to_shortname);
112 if (t->to_stop_recording != NULL)
113 t->to_stop_recording ();
116 /* Unpush the record target. */
119 record_unpush (struct target_ops *t)
121 DEBUG ("unpush %s", t->to_shortname);
129 record_disconnect (struct target_ops *t, char *args, int from_tty)
131 gdb_assert (t->to_stratum == record_stratum);
133 DEBUG ("disconnect %s", t->to_shortname);
138 target_disconnect (args, from_tty);
144 record_detach (struct target_ops *t, const char *args, int from_tty)
146 gdb_assert (t->to_stratum == record_stratum);
148 DEBUG ("detach %s", t->to_shortname);
153 target_detach (args, from_tty);
159 record_mourn_inferior (struct target_ops *t)
161 gdb_assert (t->to_stratum == record_stratum);
163 DEBUG ("mourn inferior %s", t->to_shortname);
165 /* It is safer to not stop recording. Resources will be freed when
166 threads are discarded. */
169 target_mourn_inferior ();
175 record_kill (struct target_ops *t)
177 gdb_assert (t->to_stratum == record_stratum);
179 DEBUG ("kill %s", t->to_shortname);
181 /* It is safer to not stop recording. Resources will be freed when
182 threads are discarded. */
188 /* Implement "show record debug" command. */
191 show_record_debug (struct ui_file *file, int from_tty,
192 struct cmd_list_element *c, const char *value)
194 fprintf_filtered (file, _("Debugging of process record target is %s.\n"),
198 /* Alias for "target record". */
201 cmd_record_start (char *args, int from_tty)
203 execute_command ("target record-full", from_tty);
206 /* Truncate the record log from the present point
207 of replay until the end. */
210 cmd_record_delete (char *args, int from_tty)
212 require_record_target ();
214 if (!target_record_is_replaying ())
216 printf_unfiltered (_("Already at end of record list.\n"));
220 if (!target_supports_delete_record ())
222 printf_unfiltered (_("The current record target does not support "
223 "this operation.\n"));
227 if (!from_tty || query (_("Delete the log from this point forward "
228 "and begin to record the running message "
230 target_delete_record ();
233 /* Implement the "stoprecord" or "record stop" command. */
236 cmd_record_stop (char *args, int from_tty)
238 struct target_ops *t;
240 t = require_record_target ();
245 printf_unfiltered (_("Process record is stopped and all execution "
246 "logs are deleted.\n"));
248 observer_notify_record_changed (current_inferior (), 0);
251 /* The "set record" command. */
254 set_record_command (char *args, int from_tty)
256 printf_unfiltered (_("\"set record\" must be followed "
257 "by an apporpriate subcommand.\n"));
258 help_list (set_record_cmdlist, "set record ", all_commands, gdb_stdout);
261 /* The "show record" command. */
264 show_record_command (char *args, int from_tty)
266 cmd_show_list (show_record_cmdlist, from_tty, "");
269 /* The "info record" command. */
272 info_record_command (char *args, int from_tty)
274 struct target_ops *t;
276 t = find_record_target ();
279 printf_filtered (_("No record target is currently active.\n"));
283 printf_filtered (_("Active record target: %s\n"), t->to_shortname);
284 if (t->to_info_record != NULL)
285 t->to_info_record ();
288 /* The "record save" command. */
291 cmd_record_save (char *args, int from_tty)
293 char *recfilename, recfilename_buffer[40];
295 require_record_target ();
297 if (args != NULL && *args != 0)
301 /* Default recfile name is "gdb_record.PID". */
302 xsnprintf (recfilename_buffer, sizeof (recfilename_buffer),
303 "gdb_record.%d", ptid_get_pid (inferior_ptid));
304 recfilename = recfilename_buffer;
307 target_save_record (recfilename);
310 /* "record goto" command. Argument is an instruction number,
311 as given by "info record".
313 Rewinds the recording (forward or backward) to the given instruction. */
316 cmd_record_goto (char *arg, int from_tty)
320 if (arg == NULL || *arg == '\0')
321 error (_("Command requires an argument (insn number to go to)."));
323 insn = parse_and_eval_long (arg);
325 require_record_target ();
326 target_goto_record (insn);
329 /* The "record goto begin" command. */
332 cmd_record_goto_begin (char *arg, int from_tty)
334 if (arg != NULL && *arg != '\0')
335 error (_("Junk after argument: %s."), arg);
337 require_record_target ();
338 target_goto_record_begin ();
341 /* The "record goto end" command. */
344 cmd_record_goto_end (char *arg, int from_tty)
346 if (arg != NULL && *arg != '\0')
347 error (_("Junk after argument: %s."), arg);
349 require_record_target ();
350 target_goto_record_end ();
353 /* Read an instruction number from an argument string. */
356 get_insn_number (char **arg)
359 const char *begin, *end, *pos;
362 pos = skip_spaces_const (begin);
365 error (_("Expected positive number, got: %s."), pos);
367 number = strtoulst (pos, &end, 10);
369 *arg += (end - begin);
374 /* Read a context size from an argument string. */
377 get_context_size (char **arg)
382 pos = skip_spaces (*arg);
385 error (_("Expected positive number, got: %s."), pos);
387 return strtol (pos, arg, 10);
390 /* Complain about junk at the end of an argument string. */
396 error (_("Junk after argument: %s."), arg);
399 /* Read instruction-history modifiers from an argument string. */
402 get_insn_history_modifiers (char **arg)
418 error (_("Missing modifier."));
420 for (; *args; ++args)
431 modifiers |= DISASSEMBLY_SOURCE;
432 modifiers |= DISASSEMBLY_FILENAME;
435 modifiers |= DISASSEMBLY_RAW_INSN;
438 modifiers |= DISASSEMBLY_OMIT_FNAME;
441 modifiers |= DISASSEMBLY_OMIT_PC;
444 error (_("Invalid modifier: %c."), *args);
448 args = skip_spaces (args);
451 /* Update the argument string. */
457 /* The "set record instruction-history-size / set record
458 function-call-history-size" commands are unsigned, with UINT_MAX
459 meaning unlimited. The target interfaces works with signed int
460 though, to indicate direction, so map "unlimited" to INT_MAX, which
461 is about the same as unlimited in practice. If the user does have
462 a log that huge, she can fetch it in chunks across several requests,
463 but she'll likely have other problems first... */
466 command_size_to_target_size (unsigned int size)
468 gdb_assert (size <= INT_MAX || size == UINT_MAX);
470 if (size == UINT_MAX)
476 /* The "record instruction-history" command. */
479 cmd_record_insn_history (char *arg, int from_tty)
483 require_record_target ();
485 flags = get_insn_history_modifiers (&arg);
487 size = command_size_to_target_size (record_insn_history_size);
489 if (arg == NULL || *arg == 0 || strcmp (arg, "+") == 0)
490 target_insn_history (size, flags);
491 else if (strcmp (arg, "-") == 0)
492 target_insn_history (-size, flags);
497 begin = get_insn_number (&arg);
501 arg = skip_spaces (++arg);
506 size = get_context_size (&arg);
510 target_insn_history_from (begin, size, flags);
512 else if (*arg == '-')
515 size = get_context_size (&arg);
519 target_insn_history_from (begin, -size, flags);
523 end = get_insn_number (&arg);
527 target_insn_history_range (begin, end, flags);
534 target_insn_history_from (begin, size, flags);
541 /* Read function-call-history modifiers from an argument string. */
544 get_call_history_modifiers (char **arg)
560 error (_("Missing modifier."));
562 for (; *args; ++args)
573 modifiers |= RECORD_PRINT_SRC_LINE;
576 modifiers |= RECORD_PRINT_INSN_RANGE;
579 error (_("Invalid modifier: %c."), *args);
583 args = skip_spaces (args);
586 /* Update the argument string. */
592 /* The "record function-call-history" command. */
595 cmd_record_call_history (char *arg, int from_tty)
599 require_record_target ();
601 flags = get_call_history_modifiers (&arg);
603 size = command_size_to_target_size (record_call_history_size);
605 if (arg == NULL || *arg == 0 || strcmp (arg, "+") == 0)
606 target_call_history (size, flags);
607 else if (strcmp (arg, "-") == 0)
608 target_call_history (-size, flags);
613 begin = get_insn_number (&arg);
617 arg = skip_spaces (++arg);
622 size = get_context_size (&arg);
626 target_call_history_from (begin, size, flags);
628 else if (*arg == '-')
631 size = get_context_size (&arg);
635 target_call_history_from (begin, -size, flags);
639 end = get_insn_number (&arg);
643 target_call_history_range (begin, end, flags);
650 target_call_history_from (begin, size, flags);
657 /* Helper for "set record instruction-history-size" and "set record
658 function-call-history-size" input validation. COMMAND_VAR is the
659 variable registered in the command as control variable. *SETTING
660 is the real setting the command allows changing. */
663 validate_history_size (unsigned int *command_var, unsigned int *setting)
665 if (*command_var != UINT_MAX && *command_var > INT_MAX)
667 unsigned int new_value = *command_var;
669 /* Restore previous value. */
670 *command_var = *setting;
671 error (_("integer %u out of range"), new_value);
674 /* Commit new value. */
675 *setting = *command_var;
678 /* Called by do_setshow_command. We only want values in the
679 [0..INT_MAX] range, while the command's machinery accepts
680 [0..UINT_MAX]. See command_size_to_target_size. */
683 set_record_insn_history_size (char *args, int from_tty,
684 struct cmd_list_element *c)
686 validate_history_size (&record_insn_history_size_setshow_var,
687 &record_insn_history_size);
690 /* Called by do_setshow_command. We only want values in the
691 [0..INT_MAX] range, while the command's machinery accepts
692 [0..UINT_MAX]. See command_size_to_target_size. */
695 set_record_call_history_size (char *args, int from_tty,
696 struct cmd_list_element *c)
698 validate_history_size (&record_call_history_size_setshow_var,
699 &record_call_history_size);
702 /* Provide a prototype to silence -Wmissing-prototypes. */
703 extern initialize_file_ftype _initialize_record;
706 _initialize_record (void)
708 struct cmd_list_element *c;
710 add_setshow_zuinteger_cmd ("record", no_class, &record_debug,
711 _("Set debugging of record/replay feature."),
712 _("Show debugging of record/replay feature."),
713 _("When enabled, debugging output for "
714 "record/replay feature is displayed."),
715 NULL, show_record_debug, &setdebuglist,
718 add_setshow_uinteger_cmd ("instruction-history-size", no_class,
719 &record_insn_history_size_setshow_var, _("\
720 Set number of instructions to print in \"record instruction-history\"."), _("\
721 Show number of instructions to print in \"record instruction-history\"."), _("\
722 A size of \"unlimited\" means unlimited instructions. The default is 10."),
723 set_record_insn_history_size, NULL,
724 &set_record_cmdlist, &show_record_cmdlist);
726 add_setshow_uinteger_cmd ("function-call-history-size", no_class,
727 &record_call_history_size_setshow_var, _("\
728 Set number of function to print in \"record function-call-history\"."), _("\
729 Show number of functions to print in \"record function-call-history\"."), _("\
730 A size of \"unlimited\" means unlimited lines. The default is 10."),
731 set_record_call_history_size, NULL,
732 &set_record_cmdlist, &show_record_cmdlist);
734 c = add_prefix_cmd ("record", class_obscure, cmd_record_start,
735 _("Start recording."),
736 &record_cmdlist, "record ", 0, &cmdlist);
737 set_cmd_completer (c, filename_completer);
739 add_com_alias ("rec", "record", class_obscure, 1);
740 add_prefix_cmd ("record", class_support, set_record_command,
741 _("Set record options"), &set_record_cmdlist,
742 "set record ", 0, &setlist);
743 add_alias_cmd ("rec", "record", class_obscure, 1, &setlist);
744 add_prefix_cmd ("record", class_support, show_record_command,
745 _("Show record options"), &show_record_cmdlist,
746 "show record ", 0, &showlist);
747 add_alias_cmd ("rec", "record", class_obscure, 1, &showlist);
748 add_prefix_cmd ("record", class_support, info_record_command,
749 _("Info record options"), &info_record_cmdlist,
750 "info record ", 0, &infolist);
751 add_alias_cmd ("rec", "record", class_obscure, 1, &infolist);
753 c = add_cmd ("save", class_obscure, cmd_record_save,
754 _("Save the execution log to a file.\n\
755 Argument is optional filename.\n\
756 Default filename is 'gdb_record.<process_id>'."),
758 set_cmd_completer (c, filename_completer);
760 add_cmd ("delete", class_obscure, cmd_record_delete,
761 _("Delete the rest of execution log and start recording it anew."),
763 add_alias_cmd ("d", "delete", class_obscure, 1, &record_cmdlist);
764 add_alias_cmd ("del", "delete", class_obscure, 1, &record_cmdlist);
766 add_cmd ("stop", class_obscure, cmd_record_stop,
767 _("Stop the record/replay target."),
769 add_alias_cmd ("s", "stop", class_obscure, 1, &record_cmdlist);
771 add_prefix_cmd ("goto", class_obscure, cmd_record_goto, _("\
772 Restore the program to its state at instruction number N.\n\
773 Argument is instruction number, as shown by 'info record'."),
774 &record_goto_cmdlist, "record goto ", 1, &record_cmdlist);
776 add_cmd ("begin", class_obscure, cmd_record_goto_begin,
777 _("Go to the beginning of the execution log."),
778 &record_goto_cmdlist);
779 add_alias_cmd ("start", "begin", class_obscure, 1, &record_goto_cmdlist);
781 add_cmd ("end", class_obscure, cmd_record_goto_end,
782 _("Go to the end of the execution log."),
783 &record_goto_cmdlist);
785 add_cmd ("instruction-history", class_obscure, cmd_record_insn_history, _("\
786 Print disassembled instructions stored in the execution log.\n\
787 With a /m modifier, source lines are included (if available).\n\
788 With a /r modifier, raw instructions in hex are included.\n\
789 With a /f modifier, function names are omitted.\n\
790 With a /p modifier, current position markers are omitted.\n\
791 With no argument, disassembles ten more instructions after the previous \
793 \"record instruction-history -\" disassembles ten instructions before a \
794 previous disassembly.\n\
795 One argument specifies an instruction number as shown by 'info record', and \
796 ten instructions are disassembled after that instruction.\n\
797 Two arguments with comma between them specify starting and ending instruction \
798 numbers to disassemble.\n\
799 If the second argument is preceded by '+' or '-', it specifies the distance \
800 from the first argument.\n\
801 The number of instructions to disassemble can be defined with \"set record \
802 instruction-history-size\"."),
805 add_cmd ("function-call-history", class_obscure, cmd_record_call_history, _("\
806 Prints the execution history at function granularity.\n\
807 It prints one line for each sequence of instructions that belong to the same \
809 Without modifiers, it prints the function name.\n\
810 With a /l modifier, the source file and line number range is included.\n\
811 With a /i modifier, the instruction number range is included.\n\
812 With no argument, prints ten more lines after the previous ten-line print.\n\
813 \"record function-call-history -\" prints ten lines before a previous ten-line \
815 One argument specifies a function number as shown by 'info record', and \
816 ten lines are printed after that function.\n\
817 Two arguments with comma between them specify a range of functions to print.\n\
818 If the second argument is preceded by '+' or '-', it specifies the distance \
819 from the first argument.\n\
820 The number of functions to print can be defined with \"set record \
821 function-call-history-size\"."),
824 /* Sync command control variables. */
825 record_insn_history_size_setshow_var = record_insn_history_size;
826 record_call_history_size_setshow_var = record_call_history_size;