Regenerate AArch64 opcodes files
[binutils-gdb.git] / gdb / record.c
blob25901d2deefdba7d57c77b7937586423482fd137
1 /* Process record and replay target for GDB, the GNU debugger.
3 Copyright (C) 2008-2024 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"
29 #include "interps.h"
31 #include <ctype.h>
33 /* This is the debug switch for process record. */
34 unsigned int record_debug = 0;
36 /* The number of instructions to print in "record instruction-history". */
37 static unsigned int record_insn_history_size = 10;
39 /* The variable registered as control variable in the "record
40 instruction-history" command. Necessary for extra input
41 validation. */
42 static unsigned int record_insn_history_size_setshow_var;
44 /* The number of functions to print in "record function-call-history". */
45 static unsigned int record_call_history_size = 10;
47 /* The variable registered as control variable in the "record
48 call-history" command. Necessary for extra input validation. */
49 static unsigned int record_call_history_size_setshow_var;
51 struct cmd_list_element *record_cmdlist = NULL;
52 static struct cmd_list_element *record_goto_cmdlist = NULL;
53 struct cmd_list_element *set_record_cmdlist = NULL;
54 struct cmd_list_element *show_record_cmdlist = NULL;
55 struct cmd_list_element *info_record_cmdlist = NULL;
57 #define DEBUG(msg, args...) \
58 if (record_debug) \
59 gdb_printf (gdb_stdlog, "record: " msg "\n", ##args)
61 /* See record.h. */
63 struct target_ops *
64 find_record_target (void)
66 return find_target_at (record_stratum);
69 /* Check that recording is active. Throw an error, if it isn't. */
71 static struct target_ops *
72 require_record_target (void)
74 struct target_ops *t;
76 t = find_record_target ();
77 if (t == NULL)
78 error (_("No recording is currently active.\n"
79 "Use the \"record full\" or \"record btrace\" command first."));
81 return t;
84 /* See record.h. */
86 void
87 record_preopen (void)
89 /* Check if a record target is already running. */
90 if (find_record_target () != NULL)
91 error (_("The process is already being recorded. Use \"record stop\" to "
92 "stop recording first."));
95 /* See record.h. */
97 void
98 record_start (const char *method, const char *format, int from_tty)
100 if (method == NULL)
102 if (format == NULL)
103 execute_command_to_string ("record", from_tty, false);
104 else
105 error (_("Invalid format."));
107 else if (strcmp (method, "full") == 0)
109 if (format == NULL)
110 execute_command_to_string ("record full", from_tty, false);
111 else
112 error (_("Invalid format."));
114 else if (strcmp (method, "btrace") == 0)
116 if (format == NULL)
117 execute_command_to_string ("record btrace", from_tty, false);
118 else if (strcmp (format, "bts") == 0)
119 execute_command_to_string ("record btrace bts", from_tty, false);
120 else if (strcmp (format, "pt") == 0)
121 execute_command_to_string ("record btrace pt", from_tty, false);
122 else
123 error (_("Invalid format."));
125 else
126 error (_("Invalid method."));
129 /* See record.h. */
131 void
132 record_stop (int from_tty)
134 execute_command_to_string ("record stop", from_tty, false);
137 /* See record.h. */
140 record_read_memory (struct gdbarch *gdbarch,
141 CORE_ADDR memaddr, gdb_byte *myaddr,
142 ssize_t len)
144 int ret = target_read_memory (memaddr, myaddr, len);
146 if (ret != 0)
147 DEBUG ("error reading memory at addr %s len = %ld.\n",
148 paddress (gdbarch, memaddr), (long) len);
150 return ret;
153 /* Stop recording. */
155 static void
156 record_stop (struct target_ops *t)
158 DEBUG ("stop %s", t->shortname ());
160 t->stop_recording ();
163 /* Unpush the record target. */
165 static void
166 record_unpush (struct target_ops *t)
168 DEBUG ("unpush %s", t->shortname ());
170 current_inferior ()->unpush_target (t);
173 /* See record.h. */
175 void
176 record_disconnect (struct target_ops *t, const char *args, int from_tty)
178 gdb_assert (t->stratum () == record_stratum);
180 DEBUG ("disconnect %s", t->shortname ());
182 record_stop (t);
183 record_unpush (t);
185 target_disconnect (args, from_tty);
188 /* See record.h. */
190 void
191 record_detach (struct target_ops *t, inferior *inf, int from_tty)
193 gdb_assert (t->stratum () == record_stratum);
195 DEBUG ("detach %s", t->shortname ());
197 record_stop (t);
198 record_unpush (t);
200 target_detach (inf, from_tty);
203 /* See record.h. */
205 void
206 record_mourn_inferior (struct target_ops *t)
208 gdb_assert (t->stratum () == record_stratum);
210 DEBUG ("mourn inferior %s", t->shortname ());
212 /* It is safer to not stop recording. Resources will be freed when
213 threads are discarded. */
214 record_unpush (t);
216 target_mourn_inferior (inferior_ptid);
219 /* See record.h. */
221 void
222 record_kill (struct target_ops *t)
224 gdb_assert (t->stratum () == record_stratum);
226 DEBUG ("kill %s", t->shortname ());
228 /* It is safer to not stop recording. Resources will be freed when
229 threads are discarded. */
230 record_unpush (t);
232 target_kill ();
235 /* See record.h. */
238 record_check_stopped_by_breakpoint (const address_space *aspace,
239 CORE_ADDR pc,
240 enum target_stop_reason *reason)
242 if (breakpoint_inserted_here_p (aspace, pc))
244 if (hardware_breakpoint_inserted_here_p (aspace, pc))
245 *reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
246 else
247 *reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
248 return 1;
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 interps_notify_record_changed (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 'b':
498 modifiers |= DISASSEMBLY_RAW_BYTES;
499 break;
500 case 'f':
501 modifiers |= DISASSEMBLY_OMIT_FNAME;
502 break;
503 case 'p':
504 modifiers |= DISASSEMBLY_OMIT_PC;
505 break;
506 default:
507 error (_("Invalid modifier: %c."), *args);
511 args = skip_spaces (args);
514 /* Update the argument string. */
515 *arg = args;
517 return modifiers;
520 /* The "set record instruction-history-size / set record
521 function-call-history-size" commands are unsigned, with UINT_MAX
522 meaning unlimited. The target interfaces works with signed int
523 though, to indicate direction, so map "unlimited" to INT_MAX, which
524 is about the same as unlimited in practice. If the user does have
525 a log that huge, she can fetch it in chunks across several requests,
526 but she'll likely have other problems first... */
528 static int
529 command_size_to_target_size (unsigned int size)
531 gdb_assert (size <= INT_MAX || size == UINT_MAX);
533 if (size == UINT_MAX)
534 return INT_MAX;
535 else
536 return size;
539 /* The "record instruction-history" command. */
541 static void
542 cmd_record_insn_history (const char *arg, int from_tty)
544 require_record_target ();
546 gdb_disassembly_flags flags = get_insn_history_modifiers (&arg);
548 int size = command_size_to_target_size (record_insn_history_size);
550 if (arg == NULL || *arg == 0 || strcmp (arg, "+") == 0)
551 target_insn_history (size, flags);
552 else if (strcmp (arg, "-") == 0)
553 target_insn_history (-size, flags);
554 else
556 ULONGEST begin, end;
558 begin = get_insn_number (&arg);
560 if (*arg == ',')
562 arg = skip_spaces (++arg);
564 if (*arg == '+')
566 arg += 1;
567 size = get_context_size (&arg);
569 no_chunk (arg);
571 target_insn_history_from (begin, size, flags);
573 else if (*arg == '-')
575 arg += 1;
576 size = get_context_size (&arg);
578 no_chunk (arg);
580 target_insn_history_from (begin, -size, flags);
582 else
584 end = get_insn_number (&arg);
586 no_chunk (arg);
588 target_insn_history_range (begin, end, flags);
591 else
593 no_chunk (arg);
595 target_insn_history_from (begin, size, flags);
598 dont_repeat ();
602 /* Read function-call-history modifiers from an argument string. */
604 static record_print_flags
605 get_call_history_modifiers (const char **arg)
607 record_print_flags modifiers = 0;
608 const char *args = *arg;
610 if (args == NULL)
611 return modifiers;
613 while (*args == '/')
615 ++args;
617 if (*args == '\0')
618 error (_("Missing modifier."));
620 for (; *args; ++args)
622 if (isspace (*args))
623 break;
625 if (*args == '/')
626 continue;
628 switch (*args)
630 case 'l':
631 modifiers |= RECORD_PRINT_SRC_LINE;
632 break;
633 case 'i':
634 modifiers |= RECORD_PRINT_INSN_RANGE;
635 break;
636 case 'c':
637 modifiers |= RECORD_PRINT_INDENT_CALLS;
638 break;
639 default:
640 error (_("Invalid modifier: %c."), *args);
644 args = skip_spaces (args);
647 /* Update the argument string. */
648 *arg = args;
650 return modifiers;
653 /* The "record function-call-history" command. */
655 static void
656 cmd_record_call_history (const char *arg, int from_tty)
658 require_record_target ();
660 record_print_flags flags = get_call_history_modifiers (&arg);
662 int size = command_size_to_target_size (record_call_history_size);
664 if (arg == NULL || *arg == 0 || strcmp (arg, "+") == 0)
665 target_call_history (size, flags);
666 else if (strcmp (arg, "-") == 0)
667 target_call_history (-size, flags);
668 else
670 ULONGEST begin, end;
672 begin = get_insn_number (&arg);
674 if (*arg == ',')
676 arg = skip_spaces (++arg);
678 if (*arg == '+')
680 arg += 1;
681 size = get_context_size (&arg);
683 no_chunk (arg);
685 target_call_history_from (begin, size, flags);
687 else if (*arg == '-')
689 arg += 1;
690 size = get_context_size (&arg);
692 no_chunk (arg);
694 target_call_history_from (begin, -size, flags);
696 else
698 end = get_insn_number (&arg);
700 no_chunk (arg);
702 target_call_history_range (begin, end, flags);
705 else
707 no_chunk (arg);
709 target_call_history_from (begin, size, flags);
712 dont_repeat ();
716 /* Helper for "set record instruction-history-size" and "set record
717 function-call-history-size" input validation. COMMAND_VAR is the
718 variable registered in the command as control variable. *SETTING
719 is the real setting the command allows changing. */
721 static void
722 validate_history_size (unsigned int *command_var, unsigned int *setting)
724 if (*command_var != UINT_MAX && *command_var > INT_MAX)
726 unsigned int new_value = *command_var;
728 /* Restore previous value. */
729 *command_var = *setting;
730 error (_("integer %u out of range"), new_value);
733 /* Commit new value. */
734 *setting = *command_var;
737 /* Called by do_setshow_command. We only want values in the
738 [0..INT_MAX] range, while the command's machinery accepts
739 [0..UINT_MAX]. See command_size_to_target_size. */
741 static void
742 set_record_insn_history_size (const char *args, int from_tty,
743 struct cmd_list_element *c)
745 validate_history_size (&record_insn_history_size_setshow_var,
746 &record_insn_history_size);
749 /* Called by do_setshow_command. We only want values in the
750 [0..INT_MAX] range, while the command's machinery accepts
751 [0..UINT_MAX]. See command_size_to_target_size. */
753 static void
754 set_record_call_history_size (const char *args, int from_tty,
755 struct cmd_list_element *c)
757 validate_history_size (&record_call_history_size_setshow_var,
758 &record_call_history_size);
761 void _initialize_record ();
762 void
763 _initialize_record ()
765 struct cmd_list_element *c;
767 add_setshow_zuinteger_cmd ("record", no_class, &record_debug,
768 _("Set debugging of record/replay feature."),
769 _("Show debugging of record/replay feature."),
770 _("When enabled, debugging output for "
771 "record/replay feature is displayed."),
772 NULL, show_record_debug, &setdebuglist,
773 &showdebuglist);
775 add_setshow_uinteger_cmd ("instruction-history-size", no_class,
776 &record_insn_history_size_setshow_var, _("\
777 Set number of instructions to print in \"record instruction-history\"."), _("\
778 Show number of instructions to print in \"record instruction-history\"."), _("\
779 A size of \"unlimited\" means unlimited instructions. The default is 10."),
780 set_record_insn_history_size, NULL,
781 &set_record_cmdlist, &show_record_cmdlist);
783 add_setshow_uinteger_cmd ("function-call-history-size", no_class,
784 &record_call_history_size_setshow_var, _("\
785 Set number of function to print in \"record function-call-history\"."), _("\
786 Show number of functions to print in \"record function-call-history\"."), _("\
787 A size of \"unlimited\" means unlimited lines. The default is 10."),
788 set_record_call_history_size, NULL,
789 &set_record_cmdlist, &show_record_cmdlist);
791 cmd_list_element *record_cmd
792 = add_prefix_cmd ("record", class_obscure, cmd_record_start,
793 _("Start recording."),
794 &record_cmdlist, 0, &cmdlist);
795 set_cmd_completer (record_cmd, filename_completer);
797 add_com_alias ("rec", record_cmd, class_obscure, 1);
799 set_show_commands setshow_record_cmds
800 = add_setshow_prefix_cmd ("record", class_support,
801 _("Set record options."),
802 _("Show record options."),
803 &set_record_cmdlist, &show_record_cmdlist,
804 &setlist, &showlist);
807 add_alias_cmd ("rec", setshow_record_cmds.set, class_obscure, 1, &setlist);
808 add_alias_cmd ("rec", setshow_record_cmds.show, class_obscure, 1, &showlist);
810 cmd_list_element *info_record_cmd
811 = add_prefix_cmd ("record", class_support, info_record_command,
812 _("Info record options."), &info_record_cmdlist,
813 0, &infolist);
814 add_alias_cmd ("rec", info_record_cmd, class_obscure, 1, &infolist);
816 c = add_cmd ("save", class_obscure, cmd_record_save,
817 _("Save the execution log to a file.\n\
818 Usage: record save [FILENAME]\n\
819 Default filename is 'gdb_record.PROCESS_ID'."),
820 &record_cmdlist);
821 set_cmd_completer (c, filename_completer);
823 cmd_list_element *record_delete_cmd
824 = add_cmd ("delete", class_obscure, cmd_record_delete,
825 _("Delete the rest of execution log and start recording it \
826 anew."),
827 &record_cmdlist);
828 add_alias_cmd ("d", record_delete_cmd, class_obscure, 1, &record_cmdlist);
829 add_alias_cmd ("del", record_delete_cmd, class_obscure, 1, &record_cmdlist);
831 cmd_list_element *record_stop_cmd
832 = add_cmd ("stop", class_obscure, cmd_record_stop,
833 _("Stop the record/replay target."),
834 &record_cmdlist);
835 add_alias_cmd ("s", record_stop_cmd, class_obscure, 1, &record_cmdlist);
837 add_prefix_cmd ("goto", class_obscure, cmd_record_goto, _("\
838 Restore the program to its state at instruction number N.\n\
839 Argument is instruction number, as shown by 'info record'."),
840 &record_goto_cmdlist, 1, &record_cmdlist);
842 cmd_list_element *record_goto_begin_cmd
843 = add_cmd ("begin", class_obscure, cmd_record_goto_begin,
844 _("Go to the beginning of the execution log."),
845 &record_goto_cmdlist);
846 add_alias_cmd ("start", record_goto_begin_cmd, class_obscure, 1,
847 &record_goto_cmdlist);
849 add_cmd ("end", class_obscure, cmd_record_goto_end,
850 _("Go to the end of the execution log."),
851 &record_goto_cmdlist);
853 add_cmd ("instruction-history", class_obscure, cmd_record_insn_history, _("\
854 Print disassembled instructions stored in the execution log.\n\
855 With a /m or /s modifier, source lines are included (if available).\n\
856 With a /r modifier, raw instructions in hex are included.\n\
857 With a /f modifier, function names are omitted.\n\
858 With a /p modifier, current position markers are omitted.\n\
859 With no argument, disassembles ten more instructions after the previous \
860 disassembly.\n\
861 \"record instruction-history -\" disassembles ten instructions before a \
862 previous disassembly.\n\
863 One argument specifies an instruction number as shown by 'info record', and \
864 ten instructions are disassembled after that instruction.\n\
865 Two arguments with comma between them specify starting and ending instruction \
866 numbers to disassemble.\n\
867 If the second argument is preceded by '+' or '-', it specifies the distance \
868 from the first argument.\n\
869 The number of instructions to disassemble can be defined with \"set record \
870 instruction-history-size\"."),
871 &record_cmdlist);
873 add_cmd ("function-call-history", class_obscure, cmd_record_call_history, _("\
874 Prints the execution history at function granularity.\n\
875 It prints one line for each sequence of instructions that belong to the same \
876 function.\n\
877 Without modifiers, it prints the function name.\n\
878 With a /l modifier, the source file and line number range is included.\n\
879 With a /i modifier, the instruction number range is included.\n\
880 With a /c modifier, the output is indented based on the call stack depth.\n\
881 With no argument, prints ten more lines after the previous ten-line print.\n\
882 \"record function-call-history -\" prints ten lines before a previous ten-line \
883 print.\n\
884 One argument specifies a function number as shown by 'info record', and \
885 ten lines are printed after that function.\n\
886 Two arguments with comma between them specify a range of functions to print.\n\
887 If the second argument is preceded by '+' or '-', it specifies the distance \
888 from the first argument.\n\
889 The number of functions to print can be defined with \"set record \
890 function-call-history-size\"."),
891 &record_cmdlist);
893 /* Sync command control variables. */
894 record_insn_history_size_setshow_var = record_insn_history_size;
895 record_call_history_size_setshow_var = record_call_history_size;