S390: Add target descriptions for vector register sets
[binutils-gdb.git] / gdb / record.c
blob57851ecdfeade666fae7d52931b2eec346852251
1 /* Process record and replay target for GDB, the GNU debugger.
3 Copyright (C) 2008-2015 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 /* See record.h. */
62 struct target_ops *
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)
73 struct target_ops *t;
75 t = find_record_target ();
76 if (t == NULL)
77 error (_("No record target is currently active.\n"
78 "Use one of the \"target record-<tab><tab>\" commands first."));
80 return t;
83 /* See record.h. */
85 void
86 record_preopen (void)
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."));
94 /* See record.h. */
96 int
97 record_read_memory (struct gdbarch *gdbarch,
98 CORE_ADDR memaddr, gdb_byte *myaddr,
99 ssize_t len)
101 int ret = target_read_memory (memaddr, myaddr, len);
103 if (ret != 0)
104 DEBUG ("error reading memory at addr %s len = %ld.\n",
105 paddress (gdbarch, memaddr), (long) len);
107 return ret;
110 /* Stop recording. */
112 static void
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. */
122 static void
123 record_unpush (struct target_ops *t)
125 DEBUG ("unpush %s", t->to_shortname);
127 unpush_target (t);
130 /* See record.h. */
132 void
133 record_disconnect (struct target_ops *t, const char *args, int from_tty)
135 gdb_assert (t->to_stratum == record_stratum);
137 DEBUG ("disconnect %s", t->to_shortname);
139 record_stop (t);
140 record_unpush (t);
142 target_disconnect (args, from_tty);
145 /* See record.h. */
147 void
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);
154 record_stop (t);
155 record_unpush (t);
157 target_detach (args, from_tty);
160 /* See record.h. */
162 void
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. */
171 record_unpush (t);
173 target_mourn_inferior ();
176 /* See record.h. */
178 void
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. */
187 record_unpush (t);
189 target_kill ();
192 /* Implement "show record debug" command. */
194 static void
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"),
199 value);
202 /* Alias for "target record". */
204 static void
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. */
213 static void
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"));
221 return;
224 if (!target_supports_delete_record ())
226 printf_unfiltered (_("The current record target does not support "
227 "this operation.\n"));
228 return;
231 if (!from_tty || query (_("Delete the log from this point forward "
232 "and begin to record the running message "
233 "at current PC?")))
234 target_delete_record ();
237 /* Implement the "stoprecord" or "record stop" command. */
239 static void
240 cmd_record_stop (char *args, int from_tty)
242 struct target_ops *t;
244 t = require_record_target ();
246 record_stop (t);
247 record_unpush (t);
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. */
257 static void
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. */
267 static void
268 show_record_command (char *args, int from_tty)
270 cmd_show_list (show_record_cmdlist, from_tty, "");
273 /* The "info record" command. */
275 static void
276 info_record_command (char *args, int from_tty)
278 struct target_ops *t;
280 t = find_record_target ();
281 if (t == NULL)
283 printf_filtered (_("No record target is currently active.\n"));
284 return;
287 printf_filtered (_("Active record target: %s\n"), t->to_shortname);
288 t->to_info_record (t);
291 /* The "record save" command. */
293 static void
294 cmd_record_save (char *args, int from_tty)
296 char *recfilename, recfilename_buffer[40];
298 require_record_target ();
300 if (args != NULL && *args != 0)
301 recfilename = args;
302 else
304 /* Default recfile name is "gdb_record.PID". */
305 xsnprintf (recfilename_buffer, sizeof (recfilename_buffer),
306 "gdb_record.%d", ptid_get_pid (inferior_ptid));
307 recfilename = recfilename_buffer;
310 target_save_record (recfilename);
313 /* See record.h. */
315 void
316 record_goto (const char *arg)
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 /* "record goto" command. Argument is an instruction number,
330 as given by "info record".
332 Rewinds the recording (forward or backward) to the given instruction. */
334 static void
335 cmd_record_goto (char *arg, int from_tty)
337 record_goto (arg);
340 /* The "record goto begin" command. */
342 static void
343 cmd_record_goto_begin (char *arg, int from_tty)
345 if (arg != NULL && *arg != '\0')
346 error (_("Junk after argument: %s."), arg);
348 require_record_target ();
349 target_goto_record_begin ();
352 /* The "record goto end" command. */
354 static void
355 cmd_record_goto_end (char *arg, int from_tty)
357 if (arg != NULL && *arg != '\0')
358 error (_("Junk after argument: %s."), arg);
360 require_record_target ();
361 target_goto_record_end ();
364 /* Read an instruction number from an argument string. */
366 static ULONGEST
367 get_insn_number (char **arg)
369 ULONGEST number;
370 const char *begin, *end, *pos;
372 begin = *arg;
373 pos = skip_spaces_const (begin);
375 if (!isdigit (*pos))
376 error (_("Expected positive number, got: %s."), pos);
378 number = strtoulst (pos, &end, 10);
380 *arg += (end - begin);
382 return number;
385 /* Read a context size from an argument string. */
387 static int
388 get_context_size (char **arg)
390 char *pos;
391 int number;
393 pos = skip_spaces (*arg);
395 if (!isdigit (*pos))
396 error (_("Expected positive number, got: %s."), pos);
398 return strtol (pos, arg, 10);
401 /* Complain about junk at the end of an argument string. */
403 static void
404 no_chunk (char *arg)
406 if (*arg != 0)
407 error (_("Junk after argument: %s."), arg);
410 /* Read instruction-history modifiers from an argument string. */
412 static int
413 get_insn_history_modifiers (char **arg)
415 int modifiers;
416 char *args;
418 modifiers = 0;
419 args = *arg;
421 if (args == NULL)
422 return modifiers;
424 while (*args == '/')
426 ++args;
428 if (*args == '\0')
429 error (_("Missing modifier."));
431 for (; *args; ++args)
433 if (isspace (*args))
434 break;
436 if (*args == '/')
437 continue;
439 switch (*args)
441 case 'm':
442 modifiers |= DISASSEMBLY_SOURCE;
443 modifiers |= DISASSEMBLY_FILENAME;
444 break;
445 case 'r':
446 modifiers |= DISASSEMBLY_RAW_INSN;
447 break;
448 case 'f':
449 modifiers |= DISASSEMBLY_OMIT_FNAME;
450 break;
451 case 'p':
452 modifiers |= DISASSEMBLY_OMIT_PC;
453 break;
454 default:
455 error (_("Invalid modifier: %c."), *args);
459 args = skip_spaces (args);
462 /* Update the argument string. */
463 *arg = args;
465 return modifiers;
468 /* The "set record instruction-history-size / set record
469 function-call-history-size" commands are unsigned, with UINT_MAX
470 meaning unlimited. The target interfaces works with signed int
471 though, to indicate direction, so map "unlimited" to INT_MAX, which
472 is about the same as unlimited in practice. If the user does have
473 a log that huge, she can fetch it in chunks across several requests,
474 but she'll likely have other problems first... */
476 static int
477 command_size_to_target_size (unsigned int size)
479 gdb_assert (size <= INT_MAX || size == UINT_MAX);
481 if (size == UINT_MAX)
482 return INT_MAX;
483 else
484 return size;
487 /* The "record instruction-history" command. */
489 static void
490 cmd_record_insn_history (char *arg, int from_tty)
492 int flags, size;
494 require_record_target ();
496 flags = get_insn_history_modifiers (&arg);
498 size = command_size_to_target_size (record_insn_history_size);
500 if (arg == NULL || *arg == 0 || strcmp (arg, "+") == 0)
501 target_insn_history (size, flags);
502 else if (strcmp (arg, "-") == 0)
503 target_insn_history (-size, flags);
504 else
506 ULONGEST begin, end;
508 begin = get_insn_number (&arg);
510 if (*arg == ',')
512 arg = skip_spaces (++arg);
514 if (*arg == '+')
516 arg += 1;
517 size = get_context_size (&arg);
519 no_chunk (arg);
521 target_insn_history_from (begin, size, flags);
523 else if (*arg == '-')
525 arg += 1;
526 size = get_context_size (&arg);
528 no_chunk (arg);
530 target_insn_history_from (begin, -size, flags);
532 else
534 end = get_insn_number (&arg);
536 no_chunk (arg);
538 target_insn_history_range (begin, end, flags);
541 else
543 no_chunk (arg);
545 target_insn_history_from (begin, size, flags);
548 dont_repeat ();
552 /* Read function-call-history modifiers from an argument string. */
554 static int
555 get_call_history_modifiers (char **arg)
557 int modifiers;
558 char *args;
560 modifiers = 0;
561 args = *arg;
563 if (args == NULL)
564 return modifiers;
566 while (*args == '/')
568 ++args;
570 if (*args == '\0')
571 error (_("Missing modifier."));
573 for (; *args; ++args)
575 if (isspace (*args))
576 break;
578 if (*args == '/')
579 continue;
581 switch (*args)
583 case 'l':
584 modifiers |= RECORD_PRINT_SRC_LINE;
585 break;
586 case 'i':
587 modifiers |= RECORD_PRINT_INSN_RANGE;
588 break;
589 case 'c':
590 modifiers |= RECORD_PRINT_INDENT_CALLS;
591 break;
592 default:
593 error (_("Invalid modifier: %c."), *args);
597 args = skip_spaces (args);
600 /* Update the argument string. */
601 *arg = args;
603 return modifiers;
606 /* The "record function-call-history" command. */
608 static void
609 cmd_record_call_history (char *arg, int from_tty)
611 int flags, size;
613 require_record_target ();
615 flags = get_call_history_modifiers (&arg);
617 size = command_size_to_target_size (record_call_history_size);
619 if (arg == NULL || *arg == 0 || strcmp (arg, "+") == 0)
620 target_call_history (size, flags);
621 else if (strcmp (arg, "-") == 0)
622 target_call_history (-size, flags);
623 else
625 ULONGEST begin, end;
627 begin = get_insn_number (&arg);
629 if (*arg == ',')
631 arg = skip_spaces (++arg);
633 if (*arg == '+')
635 arg += 1;
636 size = get_context_size (&arg);
638 no_chunk (arg);
640 target_call_history_from (begin, size, flags);
642 else if (*arg == '-')
644 arg += 1;
645 size = get_context_size (&arg);
647 no_chunk (arg);
649 target_call_history_from (begin, -size, flags);
651 else
653 end = get_insn_number (&arg);
655 no_chunk (arg);
657 target_call_history_range (begin, end, flags);
660 else
662 no_chunk (arg);
664 target_call_history_from (begin, size, flags);
667 dont_repeat ();
671 /* Helper for "set record instruction-history-size" and "set record
672 function-call-history-size" input validation. COMMAND_VAR is the
673 variable registered in the command as control variable. *SETTING
674 is the real setting the command allows changing. */
676 static void
677 validate_history_size (unsigned int *command_var, unsigned int *setting)
679 if (*command_var != UINT_MAX && *command_var > INT_MAX)
681 unsigned int new_value = *command_var;
683 /* Restore previous value. */
684 *command_var = *setting;
685 error (_("integer %u out of range"), new_value);
688 /* Commit new value. */
689 *setting = *command_var;
692 /* Called by do_setshow_command. We only want values in the
693 [0..INT_MAX] range, while the command's machinery accepts
694 [0..UINT_MAX]. See command_size_to_target_size. */
696 static void
697 set_record_insn_history_size (char *args, int from_tty,
698 struct cmd_list_element *c)
700 validate_history_size (&record_insn_history_size_setshow_var,
701 &record_insn_history_size);
704 /* Called by do_setshow_command. We only want values in the
705 [0..INT_MAX] range, while the command's machinery accepts
706 [0..UINT_MAX]. See command_size_to_target_size. */
708 static void
709 set_record_call_history_size (char *args, int from_tty,
710 struct cmd_list_element *c)
712 validate_history_size (&record_call_history_size_setshow_var,
713 &record_call_history_size);
716 /* Provide a prototype to silence -Wmissing-prototypes. */
717 extern initialize_file_ftype _initialize_record;
719 void
720 _initialize_record (void)
722 struct cmd_list_element *c;
724 add_setshow_zuinteger_cmd ("record", no_class, &record_debug,
725 _("Set debugging of record/replay feature."),
726 _("Show debugging of record/replay feature."),
727 _("When enabled, debugging output for "
728 "record/replay feature is displayed."),
729 NULL, show_record_debug, &setdebuglist,
730 &showdebuglist);
732 add_setshow_uinteger_cmd ("instruction-history-size", no_class,
733 &record_insn_history_size_setshow_var, _("\
734 Set number of instructions to print in \"record instruction-history\"."), _("\
735 Show number of instructions to print in \"record instruction-history\"."), _("\
736 A size of \"unlimited\" means unlimited instructions. The default is 10."),
737 set_record_insn_history_size, NULL,
738 &set_record_cmdlist, &show_record_cmdlist);
740 add_setshow_uinteger_cmd ("function-call-history-size", no_class,
741 &record_call_history_size_setshow_var, _("\
742 Set number of function to print in \"record function-call-history\"."), _("\
743 Show number of functions to print in \"record function-call-history\"."), _("\
744 A size of \"unlimited\" means unlimited lines. The default is 10."),
745 set_record_call_history_size, NULL,
746 &set_record_cmdlist, &show_record_cmdlist);
748 c = add_prefix_cmd ("record", class_obscure, cmd_record_start,
749 _("Start recording."),
750 &record_cmdlist, "record ", 0, &cmdlist);
751 set_cmd_completer (c, filename_completer);
753 add_com_alias ("rec", "record", class_obscure, 1);
754 add_prefix_cmd ("record", class_support, set_record_command,
755 _("Set record options"), &set_record_cmdlist,
756 "set record ", 0, &setlist);
757 add_alias_cmd ("rec", "record", class_obscure, 1, &setlist);
758 add_prefix_cmd ("record", class_support, show_record_command,
759 _("Show record options"), &show_record_cmdlist,
760 "show record ", 0, &showlist);
761 add_alias_cmd ("rec", "record", class_obscure, 1, &showlist);
762 add_prefix_cmd ("record", class_support, info_record_command,
763 _("Info record options"), &info_record_cmdlist,
764 "info record ", 0, &infolist);
765 add_alias_cmd ("rec", "record", class_obscure, 1, &infolist);
767 c = add_cmd ("save", class_obscure, cmd_record_save,
768 _("Save the execution log to a file.\n\
769 Argument is optional filename.\n\
770 Default filename is 'gdb_record.<process_id>'."),
771 &record_cmdlist);
772 set_cmd_completer (c, filename_completer);
774 add_cmd ("delete", class_obscure, cmd_record_delete,
775 _("Delete the rest of execution log and start recording it anew."),
776 &record_cmdlist);
777 add_alias_cmd ("d", "delete", class_obscure, 1, &record_cmdlist);
778 add_alias_cmd ("del", "delete", class_obscure, 1, &record_cmdlist);
780 add_cmd ("stop", class_obscure, cmd_record_stop,
781 _("Stop the record/replay target."),
782 &record_cmdlist);
783 add_alias_cmd ("s", "stop", class_obscure, 1, &record_cmdlist);
785 add_prefix_cmd ("goto", class_obscure, cmd_record_goto, _("\
786 Restore the program to its state at instruction number N.\n\
787 Argument is instruction number, as shown by 'info record'."),
788 &record_goto_cmdlist, "record goto ", 1, &record_cmdlist);
790 add_cmd ("begin", class_obscure, cmd_record_goto_begin,
791 _("Go to the beginning of the execution log."),
792 &record_goto_cmdlist);
793 add_alias_cmd ("start", "begin", class_obscure, 1, &record_goto_cmdlist);
795 add_cmd ("end", class_obscure, cmd_record_goto_end,
796 _("Go to the end of the execution log."),
797 &record_goto_cmdlist);
799 add_cmd ("instruction-history", class_obscure, cmd_record_insn_history, _("\
800 Print disassembled instructions stored in the execution log.\n\
801 With a /m modifier, source lines are included (if available).\n\
802 With a /r modifier, raw instructions in hex are included.\n\
803 With a /f modifier, function names are omitted.\n\
804 With a /p modifier, current position markers are omitted.\n\
805 With no argument, disassembles ten more instructions after the previous \
806 disassembly.\n\
807 \"record instruction-history -\" disassembles ten instructions before a \
808 previous disassembly.\n\
809 One argument specifies an instruction number as shown by 'info record', and \
810 ten instructions are disassembled after that instruction.\n\
811 Two arguments with comma between them specify starting and ending instruction \
812 numbers to disassemble.\n\
813 If the second argument is preceded by '+' or '-', it specifies the distance \
814 from the first argument.\n\
815 The number of instructions to disassemble can be defined with \"set record \
816 instruction-history-size\"."),
817 &record_cmdlist);
819 add_cmd ("function-call-history", class_obscure, cmd_record_call_history, _("\
820 Prints the execution history at function granularity.\n\
821 It prints one line for each sequence of instructions that belong to the same \
822 function.\n\
823 Without modifiers, it prints the function name.\n\
824 With a /l modifier, the source file and line number range is included.\n\
825 With a /i modifier, the instruction number range is included.\n\
826 With a /c modifier, the output is indented based on the call stack depth.\n\
827 With no argument, prints ten more lines after the previous ten-line print.\n\
828 \"record function-call-history -\" prints ten lines before a previous ten-line \
829 print.\n\
830 One argument specifies a function number as shown by 'info record', and \
831 ten lines are printed after that function.\n\
832 Two arguments with comma between them specify a range of functions to print.\n\
833 If the second argument is preceded by '+' or '-', it specifies the distance \
834 from the first argument.\n\
835 The number of functions to print can be defined with \"set record \
836 function-call-history-size\"."),
837 &record_cmdlist);
839 /* Sync command control variables. */
840 record_insn_history_size_setshow_var = record_insn_history_size;
841 record_call_history_size_setshow_var = record_call_history_size;