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
*set_record_cmdlist
= NULL
;
52 struct cmd_list_element
*show_record_cmdlist
= NULL
;
53 struct cmd_list_element
*info_record_cmdlist
= NULL
;
55 #define DEBUG(msg, args...) \
57 fprintf_unfiltered (gdb_stdlog, "record: " msg "\n", ##args)
59 /* Find the record target in the target stack. */
61 static struct target_ops
*
62 find_record_target (void)
66 for (t
= current_target
.beneath
; t
!= NULL
; t
= t
->beneath
)
67 if (t
->to_stratum
== record_stratum
)
73 /* Check that recording is active. Throw an error, if it isn't. */
75 static struct target_ops
*
76 require_record_target (void)
80 t
= find_record_target ();
82 error (_("No record target is currently active.\n"
83 "Use one of the \"target record-<tab><tab>\" commands first."));
91 record_read_memory (struct gdbarch
*gdbarch
,
92 CORE_ADDR memaddr
, gdb_byte
*myaddr
,
95 int ret
= target_read_memory (memaddr
, myaddr
, len
);
98 DEBUG ("error reading memory at addr %s len = %ld.\n",
99 paddress (gdbarch
, memaddr
), (long) len
);
104 /* Stop recording. */
107 record_stop (struct target_ops
*t
)
109 DEBUG ("stop %s", t
->to_shortname
);
111 if (t
->to_stop_recording
!= NULL
)
112 t
->to_stop_recording ();
115 /* Unpush the record target. */
118 record_unpush (struct target_ops
*t
)
120 DEBUG ("unpush %s", t
->to_shortname
);
128 record_disconnect (struct target_ops
*t
, char *args
, int from_tty
)
130 gdb_assert (t
->to_stratum
== record_stratum
);
132 DEBUG ("disconnect %s", t
->to_shortname
);
137 target_disconnect (args
, from_tty
);
143 record_detach (struct target_ops
*t
, char *args
, int from_tty
)
145 gdb_assert (t
->to_stratum
== record_stratum
);
147 DEBUG ("detach %s", t
->to_shortname
);
152 target_detach (args
, from_tty
);
158 record_mourn_inferior (struct target_ops
*t
)
160 gdb_assert (t
->to_stratum
== record_stratum
);
162 DEBUG ("mourn inferior %s", t
->to_shortname
);
164 /* It is safer to not stop recording. Resources will be freed when
165 threads are discarded. */
168 target_mourn_inferior ();
174 record_kill (struct target_ops
*t
)
176 gdb_assert (t
->to_stratum
== record_stratum
);
178 DEBUG ("kill %s", t
->to_shortname
);
180 /* It is safer to not stop recording. Resources will be freed when
181 threads are discarded. */
187 /* Implement "show record debug" command. */
190 show_record_debug (struct ui_file
*file
, int from_tty
,
191 struct cmd_list_element
*c
, const char *value
)
193 fprintf_filtered (file
, _("Debugging of process record target is %s.\n"),
197 /* Alias for "target record". */
200 cmd_record_start (char *args
, int from_tty
)
202 execute_command ("target record-full", from_tty
);
205 /* Truncate the record log from the present point
206 of replay until the end. */
209 cmd_record_delete (char *args
, int from_tty
)
211 require_record_target ();
213 if (!target_record_is_replaying ())
215 printf_unfiltered (_("Already at end of record list.\n"));
219 if (!target_supports_delete_record ())
221 printf_unfiltered (_("The current record target does not support "
222 "this operation.\n"));
226 if (!from_tty
|| query (_("Delete the log from this point forward "
227 "and begin to record the running message "
229 target_delete_record ();
232 /* Implement the "stoprecord" or "record stop" command. */
235 cmd_record_stop (char *args
, int from_tty
)
237 struct target_ops
*t
;
239 t
= require_record_target ();
244 printf_unfiltered (_("Process record is stopped and all execution "
245 "logs are deleted.\n"));
247 observer_notify_record_changed (current_inferior (), 0);
250 /* The "set record" command. */
253 set_record_command (char *args
, int from_tty
)
255 printf_unfiltered (_("\"set record\" must be followed "
256 "by an apporpriate subcommand.\n"));
257 help_list (set_record_cmdlist
, "set record ", all_commands
, gdb_stdout
);
260 /* The "show record" command. */
263 show_record_command (char *args
, int from_tty
)
265 cmd_show_list (show_record_cmdlist
, from_tty
, "");
268 /* The "info record" command. */
271 info_record_command (char *args
, int from_tty
)
273 struct target_ops
*t
;
275 t
= find_record_target ();
278 printf_filtered (_("No record target is currently active.\n"));
282 printf_filtered (_("Active record target: %s\n"), t
->to_shortname
);
283 if (t
->to_info_record
!= NULL
)
284 t
->to_info_record ();
287 /* The "record save" command. */
290 cmd_record_save (char *args
, int from_tty
)
292 char *recfilename
, recfilename_buffer
[40];
294 require_record_target ();
296 if (args
!= NULL
&& *args
!= 0)
300 /* Default recfile name is "gdb_record.PID". */
301 xsnprintf (recfilename_buffer
, sizeof (recfilename_buffer
),
302 "gdb_record.%d", PIDGET (inferior_ptid
));
303 recfilename
= recfilename_buffer
;
306 target_save_record (recfilename
);
309 /* "record goto" command. Argument is an instruction number,
310 as given by "info record".
312 Rewinds the recording (forward or backward) to the given instruction. */
315 cmd_record_goto (char *arg
, int from_tty
)
317 require_record_target ();
319 if (arg
== NULL
|| *arg
== '\0')
320 error (_("Command requires an argument (insn number to go to)."));
322 if (strncmp (arg
, "start", strlen ("start")) == 0
323 || strncmp (arg
, "begin", strlen ("begin")) == 0)
324 target_goto_record_begin ();
325 else if (strncmp (arg
, "end", strlen ("end")) == 0)
326 target_goto_record_end ();
331 insn
= parse_and_eval_long (arg
);
332 target_goto_record (insn
);
336 /* Read an instruction number from an argument string. */
339 get_insn_number (char **arg
)
342 const char *begin
, *end
, *pos
;
345 pos
= skip_spaces_const (begin
);
348 error (_("Expected positive number, got: %s."), pos
);
350 number
= strtoulst (pos
, &end
, 10);
352 *arg
+= (end
- begin
);
357 /* Read a context size from an argument string. */
360 get_context_size (char **arg
)
365 pos
= skip_spaces (*arg
);
368 error (_("Expected positive number, got: %s."), pos
);
370 return strtol (pos
, arg
, 10);
373 /* Complain about junk at the end of an argument string. */
379 error (_("Junk after argument: %s."), arg
);
382 /* Read instruction-history modifiers from an argument string. */
385 get_insn_history_modifiers (char **arg
)
401 error (_("Missing modifier."));
403 for (; *args
; ++args
)
414 modifiers
|= DISASSEMBLY_SOURCE
;
415 modifiers
|= DISASSEMBLY_FILENAME
;
418 modifiers
|= DISASSEMBLY_RAW_INSN
;
421 modifiers
|= DISASSEMBLY_OMIT_FNAME
;
424 modifiers
|= DISASSEMBLY_OMIT_PC
;
427 error (_("Invalid modifier: %c."), *args
);
431 args
= skip_spaces (args
);
434 /* Update the argument string. */
440 /* The "set record instruction-history-size / set record
441 function-call-history-size" commands are unsigned, with UINT_MAX
442 meaning unlimited. The target interfaces works with signed int
443 though, to indicate direction, so map "unlimited" to INT_MAX, which
444 is about the same as unlimited in practice. If the user does have
445 a log that huge, she can fetch it in chunks across several requests,
446 but she'll likely have other problems first... */
449 command_size_to_target_size (unsigned int size
)
451 gdb_assert (size
<= INT_MAX
|| size
== UINT_MAX
);
453 if (size
== UINT_MAX
)
459 /* The "record instruction-history" command. */
462 cmd_record_insn_history (char *arg
, int from_tty
)
466 require_record_target ();
468 flags
= get_insn_history_modifiers (&arg
);
470 size
= command_size_to_target_size (record_insn_history_size
);
472 if (arg
== NULL
|| *arg
== 0 || strcmp (arg
, "+") == 0)
473 target_insn_history (size
, flags
);
474 else if (strcmp (arg
, "-") == 0)
475 target_insn_history (-size
, flags
);
480 begin
= get_insn_number (&arg
);
484 arg
= skip_spaces (++arg
);
489 size
= get_context_size (&arg
);
493 target_insn_history_from (begin
, size
, flags
);
495 else if (*arg
== '-')
498 size
= get_context_size (&arg
);
502 target_insn_history_from (begin
, -size
, flags
);
506 end
= get_insn_number (&arg
);
510 target_insn_history_range (begin
, end
, flags
);
517 target_insn_history_from (begin
, size
, flags
);
524 /* Read function-call-history modifiers from an argument string. */
527 get_call_history_modifiers (char **arg
)
543 error (_("Missing modifier."));
545 for (; *args
; ++args
)
556 modifiers
|= record_print_src_line
;
559 modifiers
|= record_print_insn_range
;
562 error (_("Invalid modifier: %c."), *args
);
566 args
= skip_spaces (args
);
569 /* Update the argument string. */
575 /* The "record function-call-history" command. */
578 cmd_record_call_history (char *arg
, int from_tty
)
582 require_record_target ();
584 flags
= get_call_history_modifiers (&arg
);
586 size
= command_size_to_target_size (record_call_history_size
);
588 if (arg
== NULL
|| *arg
== 0 || strcmp (arg
, "+") == 0)
589 target_call_history (size
, flags
);
590 else if (strcmp (arg
, "-") == 0)
591 target_call_history (-size
, flags
);
596 begin
= get_insn_number (&arg
);
600 arg
= skip_spaces (++arg
);
605 size
= get_context_size (&arg
);
609 target_call_history_from (begin
, size
, flags
);
611 else if (*arg
== '-')
614 size
= get_context_size (&arg
);
618 target_call_history_from (begin
, -size
, flags
);
622 end
= get_insn_number (&arg
);
626 target_call_history_range (begin
, end
, flags
);
633 target_call_history_from (begin
, size
, flags
);
640 /* Helper for "set record instruction-history-size" and "set record
641 function-call-history-size" input validation. COMMAND_VAR is the
642 variable registered in the command as control variable. *SETTING
643 is the real setting the command allows changing. */
646 validate_history_size (unsigned int *command_var
, int *setting
)
648 if (*command_var
!= UINT_MAX
&& *command_var
> INT_MAX
)
650 unsigned int new_value
= *command_var
;
652 /* Restore previous value. */
653 *command_var
= *setting
;
654 error (_("integer %u out of range"), new_value
);
657 /* Commit new value. */
658 *setting
= *command_var
;
661 /* Called by do_setshow_command. We only want values in the
662 [0..INT_MAX] range, while the command's machinery accepts
663 [0..UINT_MAX]. See command_size_to_target_size. */
666 set_record_insn_history_size (char *args
, int from_tty
,
667 struct cmd_list_element
*c
)
669 validate_history_size (&record_insn_history_size_setshow_var
,
670 &record_insn_history_size
);
673 /* Called by do_setshow_command. We only want values in the
674 [0..INT_MAX] range, while the command's machinery accepts
675 [0..UINT_MAX]. See command_size_to_target_size. */
678 set_record_call_history_size (char *args
, int from_tty
,
679 struct cmd_list_element
*c
)
681 validate_history_size (&record_call_history_size_setshow_var
,
682 &record_call_history_size
);
685 /* Provide a prototype to silence -Wmissing-prototypes. */
686 extern initialize_file_ftype _initialize_record
;
689 _initialize_record (void)
691 struct cmd_list_element
*c
;
693 add_setshow_zuinteger_cmd ("record", no_class
, &record_debug
,
694 _("Set debugging of record/replay feature."),
695 _("Show debugging of record/replay feature."),
696 _("When enabled, debugging output for "
697 "record/replay feature is displayed."),
698 NULL
, show_record_debug
, &setdebuglist
,
701 add_setshow_uinteger_cmd ("instruction-history-size", no_class
,
702 &record_insn_history_size_setshow_var
, _("\
703 Set number of instructions to print in \"record instruction-history\"."), _("\
704 Show number of instructions to print in \"record instruction-history\"."), _("\
705 A size of \"unlimited\" means unlimited instructions. The default is 10."),
706 set_record_insn_history_size
, NULL
,
707 &set_record_cmdlist
, &show_record_cmdlist
);
709 add_setshow_uinteger_cmd ("function-call-history-size", no_class
,
710 &record_call_history_size_setshow_var
, _("\
711 Set number of function to print in \"record function-call-history\"."), _("\
712 Show number of functions to print in \"record function-call-history\"."), _("\
713 A size of \"unlimited\" means unlimited lines. The default is 10."),
714 set_record_call_history_size
, NULL
,
715 &set_record_cmdlist
, &show_record_cmdlist
);
717 c
= add_prefix_cmd ("record", class_obscure
, cmd_record_start
,
718 _("Start recording."),
719 &record_cmdlist
, "record ", 0, &cmdlist
);
720 set_cmd_completer (c
, filename_completer
);
722 add_com_alias ("rec", "record", class_obscure
, 1);
723 add_prefix_cmd ("record", class_support
, set_record_command
,
724 _("Set record options"), &set_record_cmdlist
,
725 "set record ", 0, &setlist
);
726 add_alias_cmd ("rec", "record", class_obscure
, 1, &setlist
);
727 add_prefix_cmd ("record", class_support
, show_record_command
,
728 _("Show record options"), &show_record_cmdlist
,
729 "show record ", 0, &showlist
);
730 add_alias_cmd ("rec", "record", class_obscure
, 1, &showlist
);
731 add_prefix_cmd ("record", class_support
, info_record_command
,
732 _("Info record options"), &info_record_cmdlist
,
733 "info record ", 0, &infolist
);
734 add_alias_cmd ("rec", "record", class_obscure
, 1, &infolist
);
736 c
= add_cmd ("save", class_obscure
, cmd_record_save
,
737 _("Save the execution log to a file.\n\
738 Argument is optional filename.\n\
739 Default filename is 'gdb_record.<process_id>'."),
741 set_cmd_completer (c
, filename_completer
);
743 add_cmd ("delete", class_obscure
, cmd_record_delete
,
744 _("Delete the rest of execution log and start recording it anew."),
746 add_alias_cmd ("d", "delete", class_obscure
, 1, &record_cmdlist
);
747 add_alias_cmd ("del", "delete", class_obscure
, 1, &record_cmdlist
);
749 add_cmd ("stop", class_obscure
, cmd_record_stop
,
750 _("Stop the record/replay target."),
752 add_alias_cmd ("s", "stop", class_obscure
, 1, &record_cmdlist
);
754 add_cmd ("goto", class_obscure
, cmd_record_goto
, _("\
755 Restore the program to its state at instruction number N.\n\
756 Argument is instruction number, as shown by 'info record'."),
759 add_cmd ("instruction-history", class_obscure
, cmd_record_insn_history
, _("\
760 Print disassembled instructions stored in the execution log.\n\
761 With a /m modifier, source lines are included (if available).\n\
762 With a /r modifier, raw instructions in hex are included.\n\
763 With a /f modifier, function names are omitted.\n\
764 With a /p modifier, current position markers are omitted.\n\
765 With no argument, disassembles ten more instructions after the previous \
767 \"record instruction-history -\" disassembles ten instructions before a \
768 previous disassembly.\n\
769 One argument specifies an instruction number as shown by 'info record', and \
770 ten instructions are disassembled after that instruction.\n\
771 Two arguments with comma between them specify starting and ending instruction \
772 numbers to disassemble.\n\
773 If the second argument is preceded by '+' or '-', it specifies the distance \
774 from the first argument.\n\
775 The number of instructions to disassemble can be defined with \"set record \
776 instruction-history-size\"."),
779 add_cmd ("function-call-history", class_obscure
, cmd_record_call_history
, _("\
780 Prints the execution history at function granularity.\n\
781 It prints one line for each sequence of instructions that belong to the same \
783 Without modifiers, it prints the function name.\n\
784 With a /l modifier, the source file and line number range is included.\n\
785 With a /i modifier, the instruction number range is included.\n\
786 With no argument, prints ten more lines after the previous ten-line print.\n\
787 \"record function-call-history -\" prints ten lines before a previous ten-line \
789 One argument specifies a function number as shown by 'info record', and \
790 ten lines are printed after that function.\n\
791 Two arguments with comma between them specify a range of functions to print.\n\
792 If the second argument is preceded by '+' or '-', it specifies the distance \
793 from the first argument.\n\
794 The number of functions to print can be defined with \"set record \
795 function-call-history-size\"."),
798 /* Sync command control variables. */
799 record_insn_history_size_setshow_var
= record_insn_history_size
;
800 record_call_history_size_setshow_var
= record_call_history_size
;