Automatic date update in version.in
[binutils-gdb.git] / gdb / record.c
blob17a5df262bdad8799cd1bc526f2aeda502b19c81
1 /* Process record and replay target for GDB, the GNU debugger.
3 Copyright (C) 2008-2022 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 "observable.h"
25 #include "inferior.h"
26 #include "gdbsupport/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 static 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 gdb_printf (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 recording is currently active.\n"
78 "Use the \"record full\" or \"record btrace\" command 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 void
97 record_start (const char *method, const char *format, int from_tty)
99 if (method == NULL)
101 if (format == NULL)
102 execute_command_to_string ("record", from_tty, false);
103 else
104 error (_("Invalid format."));
106 else if (strcmp (method, "full") == 0)
108 if (format == NULL)
109 execute_command_to_string ("record full", from_tty, false);
110 else
111 error (_("Invalid format."));
113 else if (strcmp (method, "btrace") == 0)
115 if (format == NULL)
116 execute_command_to_string ("record btrace", from_tty, false);
117 else if (strcmp (format, "bts") == 0)
118 execute_command_to_string ("record btrace bts", from_tty, false);
119 else if (strcmp (format, "pt") == 0)
120 execute_command_to_string ("record btrace pt", from_tty, false);
121 else
122 error (_("Invalid format."));
124 else
125 error (_("Invalid method."));
128 /* See record.h. */
130 void
131 record_stop (int from_tty)
133 execute_command_to_string ("record stop", from_tty, false);
136 /* See record.h. */
139 record_read_memory (struct gdbarch *gdbarch,
140 CORE_ADDR memaddr, gdb_byte *myaddr,
141 ssize_t len)
143 int ret = target_read_memory (memaddr, myaddr, len);
145 if (ret != 0)
146 DEBUG ("error reading memory at addr %s len = %ld.\n",
147 paddress (gdbarch, memaddr), (long) len);
149 return ret;
152 /* Stop recording. */
154 static void
155 record_stop (struct target_ops *t)
157 DEBUG ("stop %s", t->shortname ());
159 t->stop_recording ();
162 /* Unpush the record target. */
164 static void
165 record_unpush (struct target_ops *t)
167 DEBUG ("unpush %s", t->shortname ());
169 current_inferior ()->unpush_target (t);
172 /* See record.h. */
174 void
175 record_disconnect (struct target_ops *t, const char *args, int from_tty)
177 gdb_assert (t->stratum () == record_stratum);
179 DEBUG ("disconnect %s", t->shortname ());
181 record_stop (t);
182 record_unpush (t);
184 target_disconnect (args, from_tty);
187 /* See record.h. */
189 void
190 record_detach (struct target_ops *t, inferior *inf, int from_tty)
192 gdb_assert (t->stratum () == record_stratum);
194 DEBUG ("detach %s", t->shortname ());
196 record_stop (t);
197 record_unpush (t);
199 target_detach (inf, from_tty);
202 /* See record.h. */
204 void
205 record_mourn_inferior (struct target_ops *t)
207 gdb_assert (t->stratum () == record_stratum);
209 DEBUG ("mourn inferior %s", t->shortname ());
211 /* It is safer to not stop recording. Resources will be freed when
212 threads are discarded. */
213 record_unpush (t);
215 target_mourn_inferior (inferior_ptid);
218 /* See record.h. */
220 void
221 record_kill (struct target_ops *t)
223 gdb_assert (t->stratum () == record_stratum);
225 DEBUG ("kill %s", t->shortname ());
227 /* It is safer to not stop recording. Resources will be freed when
228 threads are discarded. */
229 record_unpush (t);
231 target_kill ();
234 /* See record.h. */
237 record_check_stopped_by_breakpoint (const address_space *aspace,
238 CORE_ADDR pc,
239 enum target_stop_reason *reason)
241 if (breakpoint_inserted_here_p (aspace, pc))
243 if (hardware_breakpoint_inserted_here_p (aspace, pc))
244 *reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
245 else
246 *reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
247 return 1;
250 *reason = TARGET_STOPPED_BY_NO_REASON;
251 return 0;
254 /* Implement "show record debug" command. */
256 static void
257 show_record_debug (struct ui_file *file, int from_tty,
258 struct cmd_list_element *c, const char *value)
260 gdb_printf (file, _("Debugging of process record target is %s.\n"),
261 value);
264 /* Alias for "target record". */
266 static void
267 cmd_record_start (const char *args, int from_tty)
269 execute_command ("target record-full", from_tty);
272 /* Truncate the record log from the present point
273 of replay until the end. */
275 static void
276 cmd_record_delete (const char *args, int from_tty)
278 require_record_target ();
280 if (!target_record_is_replaying (inferior_ptid))
282 gdb_printf (_("Already at end of record list.\n"));
283 return;
286 if (!target_supports_delete_record ())
288 gdb_printf (_("The current record target does not support "
289 "this operation.\n"));
290 return;
293 if (!from_tty || query (_("Delete the log from this point forward "
294 "and begin to record the running message "
295 "at current PC?")))
296 target_delete_record ();
299 /* Implement the "stoprecord" or "record stop" command. */
301 static void
302 cmd_record_stop (const char *args, int from_tty)
304 struct target_ops *t;
306 t = require_record_target ();
308 record_stop (t);
309 record_unpush (t);
311 gdb_printf (_("Process record is stopped and all execution "
312 "logs are deleted.\n"));
314 gdb::observers::record_changed.notify (current_inferior (), 0, NULL, NULL);
318 /* The "info record" command. */
320 static void
321 info_record_command (const char *args, int from_tty)
323 struct target_ops *t;
325 t = find_record_target ();
326 if (t == NULL)
328 gdb_printf (_("No recording is currently active.\n"));
329 return;
332 gdb_printf (_("Active record target: %s\n"), t->shortname ());
333 t->info_record ();
336 /* The "record save" command. */
338 static void
339 cmd_record_save (const char *args, int from_tty)
341 const char *recfilename;
342 char recfilename_buffer[40];
344 require_record_target ();
346 if (args != NULL && *args != 0)
347 recfilename = args;
348 else
350 /* Default recfile name is "gdb_record.PID". */
351 xsnprintf (recfilename_buffer, sizeof (recfilename_buffer),
352 "gdb_record.%d", inferior_ptid.pid ());
353 recfilename = recfilename_buffer;
356 target_save_record (recfilename);
359 /* See record.h. */
361 void
362 record_goto (const char *arg)
364 ULONGEST insn;
366 if (arg == NULL || *arg == '\0')
367 error (_("Command requires an argument (insn number to go to)."));
369 insn = parse_and_eval_long (arg);
371 require_record_target ();
372 target_goto_record (insn);
375 /* "record goto" command. Argument is an instruction number,
376 as given by "info record".
378 Rewinds the recording (forward or backward) to the given instruction. */
380 static void
381 cmd_record_goto (const char *arg, int from_tty)
383 record_goto (arg);
386 /* The "record goto begin" command. */
388 static void
389 cmd_record_goto_begin (const char *arg, int from_tty)
391 if (arg != NULL && *arg != '\0')
392 error (_("Junk after argument: %s."), arg);
394 require_record_target ();
395 target_goto_record_begin ();
398 /* The "record goto end" command. */
400 static void
401 cmd_record_goto_end (const char *arg, int from_tty)
403 if (arg != NULL && *arg != '\0')
404 error (_("Junk after argument: %s."), arg);
406 require_record_target ();
407 target_goto_record_end ();
410 /* Read an instruction number from an argument string. */
412 static ULONGEST
413 get_insn_number (const char **arg)
415 ULONGEST number;
416 const char *begin, *end, *pos;
418 begin = *arg;
419 pos = skip_spaces (begin);
421 if (!isdigit (*pos))
422 error (_("Expected positive number, got: %s."), pos);
424 number = strtoulst (pos, &end, 10);
426 *arg += (end - begin);
428 return number;
431 /* Read a context size from an argument string. */
433 static int
434 get_context_size (const char **arg)
436 const char *pos;
437 char *end;
439 pos = skip_spaces (*arg);
441 if (!isdigit (*pos))
442 error (_("Expected positive number, got: %s."), pos);
444 long result = strtol (pos, &end, 10);
445 *arg = end;
446 return result;
449 /* Complain about junk at the end of an argument string. */
451 static void
452 no_chunk (const char *arg)
454 if (*arg != 0)
455 error (_("Junk after argument: %s."), arg);
458 /* Read instruction-history modifiers from an argument string. */
460 static gdb_disassembly_flags
461 get_insn_history_modifiers (const char **arg)
463 gdb_disassembly_flags modifiers;
464 const char *args;
466 modifiers = 0;
467 args = *arg;
469 if (args == NULL)
470 return modifiers;
472 while (*args == '/')
474 ++args;
476 if (*args == '\0')
477 error (_("Missing modifier."));
479 for (; *args; ++args)
481 if (isspace (*args))
482 break;
484 if (*args == '/')
485 continue;
487 switch (*args)
489 case 'm':
490 case 's':
491 modifiers |= DISASSEMBLY_SOURCE;
492 modifiers |= DISASSEMBLY_FILENAME;
493 break;
494 case 'r':
495 modifiers |= DISASSEMBLY_RAW_INSN;
496 break;
497 case 'f':
498 modifiers |= DISASSEMBLY_OMIT_FNAME;
499 break;
500 case 'p':
501 modifiers |= DISASSEMBLY_OMIT_PC;
502 break;
503 default:
504 error (_("Invalid modifier: %c."), *args);
508 args = skip_spaces (args);
511 /* Update the argument string. */
512 *arg = args;
514 return modifiers;
517 /* The "set record instruction-history-size / set record
518 function-call-history-size" commands are unsigned, with UINT_MAX
519 meaning unlimited. The target interfaces works with signed int
520 though, to indicate direction, so map "unlimited" to INT_MAX, which
521 is about the same as unlimited in practice. If the user does have
522 a log that huge, she can fetch it in chunks across several requests,
523 but she'll likely have other problems first... */
525 static int
526 command_size_to_target_size (unsigned int size)
528 gdb_assert (size <= INT_MAX || size == UINT_MAX);
530 if (size == UINT_MAX)
531 return INT_MAX;
532 else
533 return size;
536 /* The "record instruction-history" command. */
538 static void
539 cmd_record_insn_history (const char *arg, int from_tty)
541 require_record_target ();
543 gdb_disassembly_flags flags = get_insn_history_modifiers (&arg);
545 int size = command_size_to_target_size (record_insn_history_size);
547 if (arg == NULL || *arg == 0 || strcmp (arg, "+") == 0)
548 target_insn_history (size, flags);
549 else if (strcmp (arg, "-") == 0)
550 target_insn_history (-size, flags);
551 else
553 ULONGEST begin, end;
555 begin = get_insn_number (&arg);
557 if (*arg == ',')
559 arg = skip_spaces (++arg);
561 if (*arg == '+')
563 arg += 1;
564 size = get_context_size (&arg);
566 no_chunk (arg);
568 target_insn_history_from (begin, size, flags);
570 else if (*arg == '-')
572 arg += 1;
573 size = get_context_size (&arg);
575 no_chunk (arg);
577 target_insn_history_from (begin, -size, flags);
579 else
581 end = get_insn_number (&arg);
583 no_chunk (arg);
585 target_insn_history_range (begin, end, flags);
588 else
590 no_chunk (arg);
592 target_insn_history_from (begin, size, flags);
595 dont_repeat ();
599 /* Read function-call-history modifiers from an argument string. */
601 static record_print_flags
602 get_call_history_modifiers (const char **arg)
604 record_print_flags modifiers = 0;
605 const char *args = *arg;
607 if (args == NULL)
608 return modifiers;
610 while (*args == '/')
612 ++args;
614 if (*args == '\0')
615 error (_("Missing modifier."));
617 for (; *args; ++args)
619 if (isspace (*args))
620 break;
622 if (*args == '/')
623 continue;
625 switch (*args)
627 case 'l':
628 modifiers |= RECORD_PRINT_SRC_LINE;
629 break;
630 case 'i':
631 modifiers |= RECORD_PRINT_INSN_RANGE;
632 break;
633 case 'c':
634 modifiers |= RECORD_PRINT_INDENT_CALLS;
635 break;
636 default:
637 error (_("Invalid modifier: %c."), *args);
641 args = skip_spaces (args);
644 /* Update the argument string. */
645 *arg = args;
647 return modifiers;
650 /* The "record function-call-history" command. */
652 static void
653 cmd_record_call_history (const char *arg, int from_tty)
655 require_record_target ();
657 record_print_flags flags = get_call_history_modifiers (&arg);
659 int size = command_size_to_target_size (record_call_history_size);
661 if (arg == NULL || *arg == 0 || strcmp (arg, "+") == 0)
662 target_call_history (size, flags);
663 else if (strcmp (arg, "-") == 0)
664 target_call_history (-size, flags);
665 else
667 ULONGEST begin, end;
669 begin = get_insn_number (&arg);
671 if (*arg == ',')
673 arg = skip_spaces (++arg);
675 if (*arg == '+')
677 arg += 1;
678 size = get_context_size (&arg);
680 no_chunk (arg);
682 target_call_history_from (begin, size, flags);
684 else if (*arg == '-')
686 arg += 1;
687 size = get_context_size (&arg);
689 no_chunk (arg);
691 target_call_history_from (begin, -size, flags);
693 else
695 end = get_insn_number (&arg);
697 no_chunk (arg);
699 target_call_history_range (begin, end, flags);
702 else
704 no_chunk (arg);
706 target_call_history_from (begin, size, flags);
709 dont_repeat ();
713 /* Helper for "set record instruction-history-size" and "set record
714 function-call-history-size" input validation. COMMAND_VAR is the
715 variable registered in the command as control variable. *SETTING
716 is the real setting the command allows changing. */
718 static void
719 validate_history_size (unsigned int *command_var, unsigned int *setting)
721 if (*command_var != UINT_MAX && *command_var > INT_MAX)
723 unsigned int new_value = *command_var;
725 /* Restore previous value. */
726 *command_var = *setting;
727 error (_("integer %u out of range"), new_value);
730 /* Commit new value. */
731 *setting = *command_var;
734 /* Called by do_setshow_command. We only want values in the
735 [0..INT_MAX] range, while the command's machinery accepts
736 [0..UINT_MAX]. See command_size_to_target_size. */
738 static void
739 set_record_insn_history_size (const char *args, int from_tty,
740 struct cmd_list_element *c)
742 validate_history_size (&record_insn_history_size_setshow_var,
743 &record_insn_history_size);
746 /* Called by do_setshow_command. We only want values in the
747 [0..INT_MAX] range, while the command's machinery accepts
748 [0..UINT_MAX]. See command_size_to_target_size. */
750 static void
751 set_record_call_history_size (const char *args, int from_tty,
752 struct cmd_list_element *c)
754 validate_history_size (&record_call_history_size_setshow_var,
755 &record_call_history_size);
758 void _initialize_record ();
759 void
760 _initialize_record ()
762 struct cmd_list_element *c;
764 add_setshow_zuinteger_cmd ("record", no_class, &record_debug,
765 _("Set debugging of record/replay feature."),
766 _("Show debugging of record/replay feature."),
767 _("When enabled, debugging output for "
768 "record/replay feature is displayed."),
769 NULL, show_record_debug, &setdebuglist,
770 &showdebuglist);
772 add_setshow_uinteger_cmd ("instruction-history-size", no_class,
773 &record_insn_history_size_setshow_var, _("\
774 Set number of instructions to print in \"record instruction-history\"."), _("\
775 Show number of instructions to print in \"record instruction-history\"."), _("\
776 A size of \"unlimited\" means unlimited instructions. The default is 10."),
777 set_record_insn_history_size, NULL,
778 &set_record_cmdlist, &show_record_cmdlist);
780 add_setshow_uinteger_cmd ("function-call-history-size", no_class,
781 &record_call_history_size_setshow_var, _("\
782 Set number of function to print in \"record function-call-history\"."), _("\
783 Show number of functions to print in \"record function-call-history\"."), _("\
784 A size of \"unlimited\" means unlimited lines. The default is 10."),
785 set_record_call_history_size, NULL,
786 &set_record_cmdlist, &show_record_cmdlist);
788 cmd_list_element *record_cmd
789 = add_prefix_cmd ("record", class_obscure, cmd_record_start,
790 _("Start recording."),
791 &record_cmdlist, 0, &cmdlist);
792 set_cmd_completer (record_cmd, filename_completer);
794 add_com_alias ("rec", record_cmd, class_obscure, 1);
796 set_show_commands setshow_record_cmds
797 = add_setshow_prefix_cmd ("record", class_support,
798 _("Set record options."),
799 _("Show record options."),
800 &set_record_cmdlist, &show_record_cmdlist,
801 &setlist, &showlist);
804 add_alias_cmd ("rec", setshow_record_cmds.set, class_obscure, 1, &setlist);
805 add_alias_cmd ("rec", setshow_record_cmds.show, class_obscure, 1, &showlist);
807 cmd_list_element *info_record_cmd
808 = add_prefix_cmd ("record", class_support, info_record_command,
809 _("Info record options."), &info_record_cmdlist,
810 0, &infolist);
811 add_alias_cmd ("rec", info_record_cmd, class_obscure, 1, &infolist);
813 c = add_cmd ("save", class_obscure, cmd_record_save,
814 _("Save the execution log to a file.\n\
815 Usage: record save [FILENAME]\n\
816 Default filename is 'gdb_record.PROCESS_ID'."),
817 &record_cmdlist);
818 set_cmd_completer (c, filename_completer);
820 cmd_list_element *record_delete_cmd
821 = add_cmd ("delete", class_obscure, cmd_record_delete,
822 _("Delete the rest of execution log and start recording it \
823 anew."),
824 &record_cmdlist);
825 add_alias_cmd ("d", record_delete_cmd, class_obscure, 1, &record_cmdlist);
826 add_alias_cmd ("del", record_delete_cmd, class_obscure, 1, &record_cmdlist);
828 cmd_list_element *record_stop_cmd
829 = add_cmd ("stop", class_obscure, cmd_record_stop,
830 _("Stop the record/replay target."),
831 &record_cmdlist);
832 add_alias_cmd ("s", record_stop_cmd, class_obscure, 1, &record_cmdlist);
834 add_prefix_cmd ("goto", class_obscure, cmd_record_goto, _("\
835 Restore the program to its state at instruction number N.\n\
836 Argument is instruction number, as shown by 'info record'."),
837 &record_goto_cmdlist, 1, &record_cmdlist);
839 cmd_list_element *record_goto_begin_cmd
840 = add_cmd ("begin", class_obscure, cmd_record_goto_begin,
841 _("Go to the beginning of the execution log."),
842 &record_goto_cmdlist);
843 add_alias_cmd ("start", record_goto_begin_cmd, class_obscure, 1,
844 &record_goto_cmdlist);
846 add_cmd ("end", class_obscure, cmd_record_goto_end,
847 _("Go to the end of the execution log."),
848 &record_goto_cmdlist);
850 add_cmd ("instruction-history", class_obscure, cmd_record_insn_history, _("\
851 Print disassembled instructions stored in the execution log.\n\
852 With a /m or /s modifier, source lines are included (if available).\n\
853 With a /r modifier, raw instructions in hex are included.\n\
854 With a /f modifier, function names are omitted.\n\
855 With a /p modifier, current position markers are omitted.\n\
856 With no argument, disassembles ten more instructions after the previous \
857 disassembly.\n\
858 \"record instruction-history -\" disassembles ten instructions before a \
859 previous disassembly.\n\
860 One argument specifies an instruction number as shown by 'info record', and \
861 ten instructions are disassembled after that instruction.\n\
862 Two arguments with comma between them specify starting and ending instruction \
863 numbers to disassemble.\n\
864 If the second argument is preceded by '+' or '-', it specifies the distance \
865 from the first argument.\n\
866 The number of instructions to disassemble can be defined with \"set record \
867 instruction-history-size\"."),
868 &record_cmdlist);
870 add_cmd ("function-call-history", class_obscure, cmd_record_call_history, _("\
871 Prints the execution history at function granularity.\n\
872 It prints one line for each sequence of instructions that belong to the same \
873 function.\n\
874 Without modifiers, it prints the function name.\n\
875 With a /l modifier, the source file and line number range is included.\n\
876 With a /i modifier, the instruction number range is included.\n\
877 With a /c modifier, the output is indented based on the call stack depth.\n\
878 With no argument, prints ten more lines after the previous ten-line print.\n\
879 \"record function-call-history -\" prints ten lines before a previous ten-line \
880 print.\n\
881 One argument specifies a function number as shown by 'info record', and \
882 ten lines are printed after that function.\n\
883 Two arguments with comma between them specify a range of functions to print.\n\
884 If the second argument is preceded by '+' or '-', it specifies the distance \
885 from the first argument.\n\
886 The number of functions to print can be defined with \"set record \
887 function-call-history-size\"."),
888 &record_cmdlist);
890 /* Sync command control variables. */
891 record_insn_history_size_setshow_var = record_insn_history_size;
892 record_call_history_size_setshow_var = record_call_history_size;