binutils/
[binutils-gdb.git] / gdb / record.c
blob07b1b9722108f4b919e95afb8590a4448e785725
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/>. */
20 #include "defs.h"
21 #include "gdbcmd.h"
22 #include "completer.h"
23 #include "record.h"
24 #include "observer.h"
25 #include "inferior.h"
26 #include "common/common-utils.h"
27 #include "cli/cli-utils.h"
28 #include "disasm.h"
30 #include <ctype.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
40 validation. */
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...) \
57 if (record_debug) \
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)
65 struct target_ops *t;
67 for (t = current_target.beneath; t != NULL; t = t->beneath)
68 if (t->to_stratum == record_stratum)
69 return t;
71 return NULL;
74 /* Check that recording is active. Throw an error, if it isn't. */
76 static struct target_ops *
77 require_record_target (void)
79 struct target_ops *t;
81 t = find_record_target ();
82 if (t == NULL)
83 error (_("No record target is currently active.\n"
84 "Use one of the \"target record-<tab><tab>\" commands first."));
86 return t;
89 /* See record.h. */
91 int
92 record_read_memory (struct gdbarch *gdbarch,
93 CORE_ADDR memaddr, gdb_byte *myaddr,
94 ssize_t len)
96 int ret = target_read_memory (memaddr, myaddr, len);
98 if (ret != 0)
99 DEBUG ("error reading memory at addr %s len = %ld.\n",
100 paddress (gdbarch, memaddr), (long) len);
102 return ret;
105 /* Stop recording. */
107 static void
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. */
118 static void
119 record_unpush (struct target_ops *t)
121 DEBUG ("unpush %s", t->to_shortname);
123 unpush_target (t);
126 /* See record.h. */
128 void
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);
135 record_stop (t);
136 record_unpush (t);
138 target_disconnect (args, from_tty);
141 /* See record.h. */
143 void
144 record_detach (struct target_ops *t, char *args, int from_tty)
146 gdb_assert (t->to_stratum == record_stratum);
148 DEBUG ("detach %s", t->to_shortname);
150 record_stop (t);
151 record_unpush (t);
153 target_detach (args, from_tty);
156 /* See record.h. */
158 void
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. */
167 record_unpush (t);
169 target_mourn_inferior ();
172 /* See record.h. */
174 void
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. */
183 record_unpush (t);
185 target_kill ();
188 /* Implement "show record debug" command. */
190 static void
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"),
195 value);
198 /* Alias for "target record". */
200 static void
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. */
209 static void
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"));
217 return;
220 if (!target_supports_delete_record ())
222 printf_unfiltered (_("The current record target does not support "
223 "this operation.\n"));
224 return;
227 if (!from_tty || query (_("Delete the log from this point forward "
228 "and begin to record the running message "
229 "at current PC?")))
230 target_delete_record ();
233 /* Implement the "stoprecord" or "record stop" command. */
235 static void
236 cmd_record_stop (char *args, int from_tty)
238 struct target_ops *t;
240 t = require_record_target ();
242 record_stop (t);
243 record_unpush (t);
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. */
253 static void
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. */
263 static void
264 show_record_command (char *args, int from_tty)
266 cmd_show_list (show_record_cmdlist, from_tty, "");
269 /* The "info record" command. */
271 static void
272 info_record_command (char *args, int from_tty)
274 struct target_ops *t;
276 t = find_record_target ();
277 if (t == NULL)
279 printf_filtered (_("No record target is currently active.\n"));
280 return;
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. */
290 static void
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)
298 recfilename = args;
299 else
301 /* Default recfile name is "gdb_record.PID". */
302 xsnprintf (recfilename_buffer, sizeof (recfilename_buffer),
303 "gdb_record.%d", PIDGET (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. */
315 void
316 cmd_record_goto (char *arg, int from_tty)
318 ULONGEST insn;
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. */
331 static void
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. */
343 static void
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. */
355 static ULONGEST
356 get_insn_number (char **arg)
358 ULONGEST number;
359 const char *begin, *end, *pos;
361 begin = *arg;
362 pos = skip_spaces_const (begin);
364 if (!isdigit (*pos))
365 error (_("Expected positive number, got: %s."), pos);
367 number = strtoulst (pos, &end, 10);
369 *arg += (end - begin);
371 return number;
374 /* Read a context size from an argument string. */
376 static int
377 get_context_size (char **arg)
379 char *pos;
380 int number;
382 pos = skip_spaces (*arg);
384 if (!isdigit (*pos))
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. */
392 static void
393 no_chunk (char *arg)
395 if (*arg != 0)
396 error (_("Junk after argument: %s."), arg);
399 /* Read instruction-history modifiers from an argument string. */
401 static int
402 get_insn_history_modifiers (char **arg)
404 int modifiers;
405 char *args;
407 modifiers = 0;
408 args = *arg;
410 if (args == NULL)
411 return modifiers;
413 while (*args == '/')
415 ++args;
417 if (*args == '\0')
418 error (_("Missing modifier."));
420 for (; *args; ++args)
422 if (isspace (*args))
423 break;
425 if (*args == '/')
426 continue;
428 switch (*args)
430 case 'm':
431 modifiers |= DISASSEMBLY_SOURCE;
432 modifiers |= DISASSEMBLY_FILENAME;
433 break;
434 case 'r':
435 modifiers |= DISASSEMBLY_RAW_INSN;
436 break;
437 case 'f':
438 modifiers |= DISASSEMBLY_OMIT_FNAME;
439 break;
440 case 'p':
441 modifiers |= DISASSEMBLY_OMIT_PC;
442 break;
443 default:
444 error (_("Invalid modifier: %c."), *args);
448 args = skip_spaces (args);
451 /* Update the argument string. */
452 *arg = args;
454 return modifiers;
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... */
465 static int
466 command_size_to_target_size (unsigned int size)
468 gdb_assert (size <= INT_MAX || size == UINT_MAX);
470 if (size == UINT_MAX)
471 return INT_MAX;
472 else
473 return size;
476 /* The "record instruction-history" command. */
478 static void
479 cmd_record_insn_history (char *arg, int from_tty)
481 int flags, size;
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);
493 else
495 ULONGEST begin, end;
497 begin = get_insn_number (&arg);
499 if (*arg == ',')
501 arg = skip_spaces (++arg);
503 if (*arg == '+')
505 arg += 1;
506 size = get_context_size (&arg);
508 no_chunk (arg);
510 target_insn_history_from (begin, size, flags);
512 else if (*arg == '-')
514 arg += 1;
515 size = get_context_size (&arg);
517 no_chunk (arg);
519 target_insn_history_from (begin, -size, flags);
521 else
523 end = get_insn_number (&arg);
525 no_chunk (arg);
527 target_insn_history_range (begin, end, flags);
530 else
532 no_chunk (arg);
534 target_insn_history_from (begin, size, flags);
537 dont_repeat ();
541 /* Read function-call-history modifiers from an argument string. */
543 static int
544 get_call_history_modifiers (char **arg)
546 int modifiers;
547 char *args;
549 modifiers = 0;
550 args = *arg;
552 if (args == NULL)
553 return modifiers;
555 while (*args == '/')
557 ++args;
559 if (*args == '\0')
560 error (_("Missing modifier."));
562 for (; *args; ++args)
564 if (isspace (*args))
565 break;
567 if (*args == '/')
568 continue;
570 switch (*args)
572 case 'l':
573 modifiers |= RECORD_PRINT_SRC_LINE;
574 break;
575 case 'i':
576 modifiers |= RECORD_PRINT_INSN_RANGE;
577 break;
578 default:
579 error (_("Invalid modifier: %c."), *args);
583 args = skip_spaces (args);
586 /* Update the argument string. */
587 *arg = args;
589 return modifiers;
592 /* The "record function-call-history" command. */
594 static void
595 cmd_record_call_history (char *arg, int from_tty)
597 int flags, size;
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);
609 else
611 ULONGEST begin, end;
613 begin = get_insn_number (&arg);
615 if (*arg == ',')
617 arg = skip_spaces (++arg);
619 if (*arg == '+')
621 arg += 1;
622 size = get_context_size (&arg);
624 no_chunk (arg);
626 target_call_history_from (begin, size, flags);
628 else if (*arg == '-')
630 arg += 1;
631 size = get_context_size (&arg);
633 no_chunk (arg);
635 target_call_history_from (begin, -size, flags);
637 else
639 end = get_insn_number (&arg);
641 no_chunk (arg);
643 target_call_history_range (begin, end, flags);
646 else
648 no_chunk (arg);
650 target_call_history_from (begin, size, flags);
653 dont_repeat ();
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. */
662 static void
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. */
682 static void
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. */
694 static void
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;
705 void
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,
716 &showdebuglist);
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>'."),
757 &record_cmdlist);
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."),
762 &record_cmdlist);
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."),
768 &record_cmdlist);
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 \
792 disassembly.\n\
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\"."),
803 &record_cmdlist);
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 \
808 function.\n\
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 \
814 print.\n\
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\"."),
822 &record_cmdlist);
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;