New Georgian translation for the ld sub-directory
[binutils-gdb.git] / gdb / record.c
blobf7c82665b0586d78fa6744a9b80bd59bb8db3302
1 /* Process record and replay target for GDB, the GNU debugger.
3 Copyright (C) 2008-2023 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 *reason = TARGET_STOPPED_BY_NO_REASON;
252 return 0;
255 /* Implement "show record debug" command. */
257 static void
258 show_record_debug (struct ui_file *file, int from_tty,
259 struct cmd_list_element *c, const char *value)
261 gdb_printf (file, _("Debugging of process record target is %s.\n"),
262 value);
265 /* Alias for "target record". */
267 static void
268 cmd_record_start (const char *args, int from_tty)
270 execute_command ("target record-full", from_tty);
273 /* Truncate the record log from the present point
274 of replay until the end. */
276 static void
277 cmd_record_delete (const char *args, int from_tty)
279 require_record_target ();
281 if (!target_record_is_replaying (inferior_ptid))
283 gdb_printf (_("Already at end of record list.\n"));
284 return;
287 if (!target_supports_delete_record ())
289 gdb_printf (_("The current record target does not support "
290 "this operation.\n"));
291 return;
294 if (!from_tty || query (_("Delete the log from this point forward "
295 "and begin to record the running message "
296 "at current PC?")))
297 target_delete_record ();
300 /* Implement the "stoprecord" or "record stop" command. */
302 static void
303 cmd_record_stop (const char *args, int from_tty)
305 struct target_ops *t;
307 t = require_record_target ();
309 record_stop (t);
310 record_unpush (t);
312 gdb_printf (_("Process record is stopped and all execution "
313 "logs are deleted.\n"));
315 interps_notify_record_changed (current_inferior (), 0, NULL, NULL);
319 /* The "info record" command. */
321 static void
322 info_record_command (const char *args, int from_tty)
324 struct target_ops *t;
326 t = find_record_target ();
327 if (t == NULL)
329 gdb_printf (_("No recording is currently active.\n"));
330 return;
333 gdb_printf (_("Active record target: %s\n"), t->shortname ());
334 t->info_record ();
337 /* The "record save" command. */
339 static void
340 cmd_record_save (const char *args, int from_tty)
342 const char *recfilename;
343 char recfilename_buffer[40];
345 require_record_target ();
347 if (args != NULL && *args != 0)
348 recfilename = args;
349 else
351 /* Default recfile name is "gdb_record.PID". */
352 xsnprintf (recfilename_buffer, sizeof (recfilename_buffer),
353 "gdb_record.%d", inferior_ptid.pid ());
354 recfilename = recfilename_buffer;
357 target_save_record (recfilename);
360 /* See record.h. */
362 void
363 record_goto (const char *arg)
365 ULONGEST insn;
367 if (arg == NULL || *arg == '\0')
368 error (_("Command requires an argument (insn number to go to)."));
370 insn = parse_and_eval_long (arg);
372 require_record_target ();
373 target_goto_record (insn);
376 /* "record goto" command. Argument is an instruction number,
377 as given by "info record".
379 Rewinds the recording (forward or backward) to the given instruction. */
381 static void
382 cmd_record_goto (const char *arg, int from_tty)
384 record_goto (arg);
387 /* The "record goto begin" command. */
389 static void
390 cmd_record_goto_begin (const char *arg, int from_tty)
392 if (arg != NULL && *arg != '\0')
393 error (_("Junk after argument: %s."), arg);
395 require_record_target ();
396 target_goto_record_begin ();
399 /* The "record goto end" command. */
401 static void
402 cmd_record_goto_end (const char *arg, int from_tty)
404 if (arg != NULL && *arg != '\0')
405 error (_("Junk after argument: %s."), arg);
407 require_record_target ();
408 target_goto_record_end ();
411 /* Read an instruction number from an argument string. */
413 static ULONGEST
414 get_insn_number (const char **arg)
416 ULONGEST number;
417 const char *begin, *end, *pos;
419 begin = *arg;
420 pos = skip_spaces (begin);
422 if (!isdigit (*pos))
423 error (_("Expected positive number, got: %s."), pos);
425 number = strtoulst (pos, &end, 10);
427 *arg += (end - begin);
429 return number;
432 /* Read a context size from an argument string. */
434 static int
435 get_context_size (const char **arg)
437 const char *pos;
438 char *end;
440 pos = skip_spaces (*arg);
442 if (!isdigit (*pos))
443 error (_("Expected positive number, got: %s."), pos);
445 long result = strtol (pos, &end, 10);
446 *arg = end;
447 return result;
450 /* Complain about junk at the end of an argument string. */
452 static void
453 no_chunk (const char *arg)
455 if (*arg != 0)
456 error (_("Junk after argument: %s."), arg);
459 /* Read instruction-history modifiers from an argument string. */
461 static gdb_disassembly_flags
462 get_insn_history_modifiers (const char **arg)
464 gdb_disassembly_flags modifiers;
465 const char *args;
467 modifiers = 0;
468 args = *arg;
470 if (args == NULL)
471 return modifiers;
473 while (*args == '/')
475 ++args;
477 if (*args == '\0')
478 error (_("Missing modifier."));
480 for (; *args; ++args)
482 if (isspace (*args))
483 break;
485 if (*args == '/')
486 continue;
488 switch (*args)
490 case 'm':
491 case 's':
492 modifiers |= DISASSEMBLY_SOURCE;
493 modifiers |= DISASSEMBLY_FILENAME;
494 break;
495 case 'r':
496 modifiers |= DISASSEMBLY_RAW_INSN;
497 break;
498 case 'b':
499 modifiers |= DISASSEMBLY_RAW_BYTES;
500 break;
501 case 'f':
502 modifiers |= DISASSEMBLY_OMIT_FNAME;
503 break;
504 case 'p':
505 modifiers |= DISASSEMBLY_OMIT_PC;
506 break;
507 default:
508 error (_("Invalid modifier: %c."), *args);
512 args = skip_spaces (args);
515 /* Update the argument string. */
516 *arg = args;
518 return modifiers;
521 /* The "set record instruction-history-size / set record
522 function-call-history-size" commands are unsigned, with UINT_MAX
523 meaning unlimited. The target interfaces works with signed int
524 though, to indicate direction, so map "unlimited" to INT_MAX, which
525 is about the same as unlimited in practice. If the user does have
526 a log that huge, she can fetch it in chunks across several requests,
527 but she'll likely have other problems first... */
529 static int
530 command_size_to_target_size (unsigned int size)
532 gdb_assert (size <= INT_MAX || size == UINT_MAX);
534 if (size == UINT_MAX)
535 return INT_MAX;
536 else
537 return size;
540 /* The "record instruction-history" command. */
542 static void
543 cmd_record_insn_history (const char *arg, int from_tty)
545 require_record_target ();
547 gdb_disassembly_flags flags = get_insn_history_modifiers (&arg);
549 int size = command_size_to_target_size (record_insn_history_size);
551 if (arg == NULL || *arg == 0 || strcmp (arg, "+") == 0)
552 target_insn_history (size, flags);
553 else if (strcmp (arg, "-") == 0)
554 target_insn_history (-size, flags);
555 else
557 ULONGEST begin, end;
559 begin = get_insn_number (&arg);
561 if (*arg == ',')
563 arg = skip_spaces (++arg);
565 if (*arg == '+')
567 arg += 1;
568 size = get_context_size (&arg);
570 no_chunk (arg);
572 target_insn_history_from (begin, size, flags);
574 else if (*arg == '-')
576 arg += 1;
577 size = get_context_size (&arg);
579 no_chunk (arg);
581 target_insn_history_from (begin, -size, flags);
583 else
585 end = get_insn_number (&arg);
587 no_chunk (arg);
589 target_insn_history_range (begin, end, flags);
592 else
594 no_chunk (arg);
596 target_insn_history_from (begin, size, flags);
599 dont_repeat ();
603 /* Read function-call-history modifiers from an argument string. */
605 static record_print_flags
606 get_call_history_modifiers (const char **arg)
608 record_print_flags modifiers = 0;
609 const char *args = *arg;
611 if (args == NULL)
612 return modifiers;
614 while (*args == '/')
616 ++args;
618 if (*args == '\0')
619 error (_("Missing modifier."));
621 for (; *args; ++args)
623 if (isspace (*args))
624 break;
626 if (*args == '/')
627 continue;
629 switch (*args)
631 case 'l':
632 modifiers |= RECORD_PRINT_SRC_LINE;
633 break;
634 case 'i':
635 modifiers |= RECORD_PRINT_INSN_RANGE;
636 break;
637 case 'c':
638 modifiers |= RECORD_PRINT_INDENT_CALLS;
639 break;
640 default:
641 error (_("Invalid modifier: %c."), *args);
645 args = skip_spaces (args);
648 /* Update the argument string. */
649 *arg = args;
651 return modifiers;
654 /* The "record function-call-history" command. */
656 static void
657 cmd_record_call_history (const char *arg, int from_tty)
659 require_record_target ();
661 record_print_flags flags = get_call_history_modifiers (&arg);
663 int size = command_size_to_target_size (record_call_history_size);
665 if (arg == NULL || *arg == 0 || strcmp (arg, "+") == 0)
666 target_call_history (size, flags);
667 else if (strcmp (arg, "-") == 0)
668 target_call_history (-size, flags);
669 else
671 ULONGEST begin, end;
673 begin = get_insn_number (&arg);
675 if (*arg == ',')
677 arg = skip_spaces (++arg);
679 if (*arg == '+')
681 arg += 1;
682 size = get_context_size (&arg);
684 no_chunk (arg);
686 target_call_history_from (begin, size, flags);
688 else if (*arg == '-')
690 arg += 1;
691 size = get_context_size (&arg);
693 no_chunk (arg);
695 target_call_history_from (begin, -size, flags);
697 else
699 end = get_insn_number (&arg);
701 no_chunk (arg);
703 target_call_history_range (begin, end, flags);
706 else
708 no_chunk (arg);
710 target_call_history_from (begin, size, flags);
713 dont_repeat ();
717 /* Helper for "set record instruction-history-size" and "set record
718 function-call-history-size" input validation. COMMAND_VAR is the
719 variable registered in the command as control variable. *SETTING
720 is the real setting the command allows changing. */
722 static void
723 validate_history_size (unsigned int *command_var, unsigned int *setting)
725 if (*command_var != UINT_MAX && *command_var > INT_MAX)
727 unsigned int new_value = *command_var;
729 /* Restore previous value. */
730 *command_var = *setting;
731 error (_("integer %u out of range"), new_value);
734 /* Commit new value. */
735 *setting = *command_var;
738 /* Called by do_setshow_command. We only want values in the
739 [0..INT_MAX] range, while the command's machinery accepts
740 [0..UINT_MAX]. See command_size_to_target_size. */
742 static void
743 set_record_insn_history_size (const char *args, int from_tty,
744 struct cmd_list_element *c)
746 validate_history_size (&record_insn_history_size_setshow_var,
747 &record_insn_history_size);
750 /* Called by do_setshow_command. We only want values in the
751 [0..INT_MAX] range, while the command's machinery accepts
752 [0..UINT_MAX]. See command_size_to_target_size. */
754 static void
755 set_record_call_history_size (const char *args, int from_tty,
756 struct cmd_list_element *c)
758 validate_history_size (&record_call_history_size_setshow_var,
759 &record_call_history_size);
762 void _initialize_record ();
763 void
764 _initialize_record ()
766 struct cmd_list_element *c;
768 add_setshow_zuinteger_cmd ("record", no_class, &record_debug,
769 _("Set debugging of record/replay feature."),
770 _("Show debugging of record/replay feature."),
771 _("When enabled, debugging output for "
772 "record/replay feature is displayed."),
773 NULL, show_record_debug, &setdebuglist,
774 &showdebuglist);
776 add_setshow_uinteger_cmd ("instruction-history-size", no_class,
777 &record_insn_history_size_setshow_var, _("\
778 Set number of instructions to print in \"record instruction-history\"."), _("\
779 Show number of instructions to print in \"record instruction-history\"."), _("\
780 A size of \"unlimited\" means unlimited instructions. The default is 10."),
781 set_record_insn_history_size, NULL,
782 &set_record_cmdlist, &show_record_cmdlist);
784 add_setshow_uinteger_cmd ("function-call-history-size", no_class,
785 &record_call_history_size_setshow_var, _("\
786 Set number of function to print in \"record function-call-history\"."), _("\
787 Show number of functions to print in \"record function-call-history\"."), _("\
788 A size of \"unlimited\" means unlimited lines. The default is 10."),
789 set_record_call_history_size, NULL,
790 &set_record_cmdlist, &show_record_cmdlist);
792 cmd_list_element *record_cmd
793 = add_prefix_cmd ("record", class_obscure, cmd_record_start,
794 _("Start recording."),
795 &record_cmdlist, 0, &cmdlist);
796 set_cmd_completer (record_cmd, filename_completer);
798 add_com_alias ("rec", record_cmd, class_obscure, 1);
800 set_show_commands setshow_record_cmds
801 = add_setshow_prefix_cmd ("record", class_support,
802 _("Set record options."),
803 _("Show record options."),
804 &set_record_cmdlist, &show_record_cmdlist,
805 &setlist, &showlist);
808 add_alias_cmd ("rec", setshow_record_cmds.set, class_obscure, 1, &setlist);
809 add_alias_cmd ("rec", setshow_record_cmds.show, class_obscure, 1, &showlist);
811 cmd_list_element *info_record_cmd
812 = add_prefix_cmd ("record", class_support, info_record_command,
813 _("Info record options."), &info_record_cmdlist,
814 0, &infolist);
815 add_alias_cmd ("rec", info_record_cmd, class_obscure, 1, &infolist);
817 c = add_cmd ("save", class_obscure, cmd_record_save,
818 _("Save the execution log to a file.\n\
819 Usage: record save [FILENAME]\n\
820 Default filename is 'gdb_record.PROCESS_ID'."),
821 &record_cmdlist);
822 set_cmd_completer (c, filename_completer);
824 cmd_list_element *record_delete_cmd
825 = add_cmd ("delete", class_obscure, cmd_record_delete,
826 _("Delete the rest of execution log and start recording it \
827 anew."),
828 &record_cmdlist);
829 add_alias_cmd ("d", record_delete_cmd, class_obscure, 1, &record_cmdlist);
830 add_alias_cmd ("del", record_delete_cmd, class_obscure, 1, &record_cmdlist);
832 cmd_list_element *record_stop_cmd
833 = add_cmd ("stop", class_obscure, cmd_record_stop,
834 _("Stop the record/replay target."),
835 &record_cmdlist);
836 add_alias_cmd ("s", record_stop_cmd, class_obscure, 1, &record_cmdlist);
838 add_prefix_cmd ("goto", class_obscure, cmd_record_goto, _("\
839 Restore the program to its state at instruction number N.\n\
840 Argument is instruction number, as shown by 'info record'."),
841 &record_goto_cmdlist, 1, &record_cmdlist);
843 cmd_list_element *record_goto_begin_cmd
844 = add_cmd ("begin", class_obscure, cmd_record_goto_begin,
845 _("Go to the beginning of the execution log."),
846 &record_goto_cmdlist);
847 add_alias_cmd ("start", record_goto_begin_cmd, class_obscure, 1,
848 &record_goto_cmdlist);
850 add_cmd ("end", class_obscure, cmd_record_goto_end,
851 _("Go to the end of the execution log."),
852 &record_goto_cmdlist);
854 add_cmd ("instruction-history", class_obscure, cmd_record_insn_history, _("\
855 Print disassembled instructions stored in the execution log.\n\
856 With a /m or /s modifier, source lines are included (if available).\n\
857 With a /r modifier, raw instructions in hex are included.\n\
858 With a /f modifier, function names are omitted.\n\
859 With a /p modifier, current position markers are omitted.\n\
860 With no argument, disassembles ten more instructions after the previous \
861 disassembly.\n\
862 \"record instruction-history -\" disassembles ten instructions before a \
863 previous disassembly.\n\
864 One argument specifies an instruction number as shown by 'info record', and \
865 ten instructions are disassembled after that instruction.\n\
866 Two arguments with comma between them specify starting and ending instruction \
867 numbers to disassemble.\n\
868 If the second argument is preceded by '+' or '-', it specifies the distance \
869 from the first argument.\n\
870 The number of instructions to disassemble can be defined with \"set record \
871 instruction-history-size\"."),
872 &record_cmdlist);
874 add_cmd ("function-call-history", class_obscure, cmd_record_call_history, _("\
875 Prints the execution history at function granularity.\n\
876 It prints one line for each sequence of instructions that belong to the same \
877 function.\n\
878 Without modifiers, it prints the function name.\n\
879 With a /l modifier, the source file and line number range is included.\n\
880 With a /i modifier, the instruction number range is included.\n\
881 With a /c modifier, the output is indented based on the call stack depth.\n\
882 With no argument, prints ten more lines after the previous ten-line print.\n\
883 \"record function-call-history -\" prints ten lines before a previous ten-line \
884 print.\n\
885 One argument specifies a function number as shown by 'info record', and \
886 ten lines are printed after that function.\n\
887 Two arguments with comma between them specify a range of functions to print.\n\
888 If the second argument is preceded by '+' or '-', it specifies the distance \
889 from the first argument.\n\
890 The number of functions to print can be defined with \"set record \
891 function-call-history-size\"."),
892 &record_cmdlist);
894 /* Sync command control variables. */
895 record_insn_history_size_setshow_var = record_insn_history_size;
896 record_call_history_size_setshow_var = record_call_history_size;