[gdb/testsuite] Fix gdb.threads/threadcrash.exp with glibc debuginfo
[binutils-gdb.git] / gdb / record.c
blob5b1093dd12ee3247a9044f8d2da61fc88d86d265
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 "gdbcmd.h"
21 #include "completer.h"
22 #include "record.h"
23 #include "observable.h"
24 #include "inferior.h"
25 #include "gdbsupport/common-utils.h"
26 #include "cli/cli-utils.h"
27 #include "disasm.h"
28 #include "interps.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 return 0;
253 /* Implement "show record debug" command. */
255 static void
256 show_record_debug (struct ui_file *file, int from_tty,
257 struct cmd_list_element *c, const char *value)
259 gdb_printf (file, _("Debugging of process record target is %s.\n"),
260 value);
263 /* Alias for "target record-full". */
265 static void
266 cmd_record_start (const char *args, int from_tty)
268 /* As 'record' is a prefix command then if the user types 'record blah'
269 GDB will search for the 'blah' sub-command and either run that instead
270 of calling this function, or throw an error if 'blah' doesn't exist.
271 As a result, we only get here if no args are given. */
272 gdb_assert (args == nullptr);
273 execute_command ("target record-full", from_tty);
276 /* Truncate the record log from the present point
277 of replay until the end. */
279 static void
280 cmd_record_delete (const char *args, int from_tty)
282 require_record_target ();
284 if (!target_record_is_replaying (inferior_ptid))
286 gdb_printf (_("Already at end of record list.\n"));
287 return;
290 if (!target_supports_delete_record ())
292 gdb_printf (_("The current record target does not support "
293 "this operation.\n"));
294 return;
297 if (!from_tty || query (_("Delete the log from this point forward "
298 "and begin to record the running message "
299 "at current PC?")))
300 target_delete_record ();
303 /* Implement the "stoprecord" or "record stop" command. */
305 static void
306 cmd_record_stop (const char *args, int from_tty)
308 struct target_ops *t;
310 t = require_record_target ();
312 record_stop (t);
313 record_unpush (t);
315 gdb_printf (_("Process record is stopped and all execution "
316 "logs are deleted.\n"));
318 interps_notify_record_changed (current_inferior (), 0, NULL, NULL);
322 /* The "info record" command. */
324 static void
325 info_record_command (const char *args, int from_tty)
327 struct target_ops *t;
329 t = find_record_target ();
330 if (t == NULL)
332 gdb_printf (_("No recording is currently active.\n"));
333 return;
336 gdb_printf (_("Active record target: %s\n"), t->shortname ());
337 t->info_record ();
340 /* The "record save" command. */
342 static void
343 cmd_record_save (const char *args, int from_tty)
345 const char *recfilename;
346 char recfilename_buffer[40];
348 require_record_target ();
350 if (args != NULL && *args != 0)
351 recfilename = args;
352 else
354 /* Default recfile name is "gdb_record.PID". */
355 xsnprintf (recfilename_buffer, sizeof (recfilename_buffer),
356 "gdb_record.%d", inferior_ptid.pid ());
357 recfilename = recfilename_buffer;
360 target_save_record (recfilename);
363 /* See record.h. */
365 void
366 record_goto (const char *arg)
368 ULONGEST insn;
370 if (arg == NULL || *arg == '\0')
371 error (_("Command requires an argument (insn number to go to)."));
373 insn = parse_and_eval_long (arg);
375 require_record_target ();
376 target_goto_record (insn);
379 /* "record goto" command. Argument is an instruction number,
380 as given by "info record".
382 Rewinds the recording (forward or backward) to the given instruction. */
384 static void
385 cmd_record_goto (const char *arg, int from_tty)
387 record_goto (arg);
390 /* The "record goto begin" command. */
392 static void
393 cmd_record_goto_begin (const char *arg, int from_tty)
395 if (arg != NULL && *arg != '\0')
396 error (_("Junk after argument: %s."), arg);
398 require_record_target ();
399 target_goto_record_begin ();
402 /* The "record goto end" command. */
404 static void
405 cmd_record_goto_end (const char *arg, int from_tty)
407 if (arg != NULL && *arg != '\0')
408 error (_("Junk after argument: %s."), arg);
410 require_record_target ();
411 target_goto_record_end ();
414 /* Read an instruction number from an argument string. */
416 static ULONGEST
417 get_insn_number (const char **arg)
419 ULONGEST number;
420 const char *begin, *end, *pos;
422 begin = *arg;
423 pos = skip_spaces (begin);
425 if (!isdigit (*pos))
426 error (_("Expected positive number, got: %s."), pos);
428 number = strtoulst (pos, &end, 10);
430 *arg += (end - begin);
432 return number;
435 /* Read a context size from an argument string. */
437 static int
438 get_context_size (const char **arg)
440 const char *pos;
441 char *end;
443 pos = skip_spaces (*arg);
445 if (!isdigit (*pos))
446 error (_("Expected positive number, got: %s."), pos);
448 long result = strtol (pos, &end, 10);
449 *arg = end;
450 return result;
453 /* Complain about junk at the end of an argument string. */
455 static void
456 no_chunk (const char *arg)
458 if (*arg != 0)
459 error (_("Junk after argument: %s."), arg);
462 /* Read instruction-history modifiers from an argument string. */
464 static gdb_disassembly_flags
465 get_insn_history_modifiers (const char **arg)
467 gdb_disassembly_flags modifiers;
468 const char *args;
470 modifiers = 0;
471 args = *arg;
473 if (args == NULL)
474 return modifiers;
476 while (*args == '/')
478 ++args;
480 if (*args == '\0')
481 error (_("Missing modifier."));
483 for (; *args; ++args)
485 if (isspace (*args))
486 break;
488 if (*args == '/')
489 continue;
491 switch (*args)
493 case 'm':
494 case 's':
495 modifiers |= DISASSEMBLY_SOURCE;
496 modifiers |= DISASSEMBLY_FILENAME;
497 break;
498 case 'r':
499 modifiers |= DISASSEMBLY_RAW_INSN;
500 break;
501 case 'b':
502 modifiers |= DISASSEMBLY_RAW_BYTES;
503 break;
504 case 'f':
505 modifiers |= DISASSEMBLY_OMIT_FNAME;
506 break;
507 case 'p':
508 modifiers |= DISASSEMBLY_OMIT_PC;
509 break;
510 default:
511 error (_("Invalid modifier: %c."), *args);
515 args = skip_spaces (args);
518 /* Update the argument string. */
519 *arg = args;
521 return modifiers;
524 /* The "set record instruction-history-size / set record
525 function-call-history-size" commands are unsigned, with UINT_MAX
526 meaning unlimited. The target interfaces works with signed int
527 though, to indicate direction, so map "unlimited" to INT_MAX, which
528 is about the same as unlimited in practice. If the user does have
529 a log that huge, she can fetch it in chunks across several requests,
530 but she'll likely have other problems first... */
532 static int
533 command_size_to_target_size (unsigned int size)
535 gdb_assert (size <= INT_MAX || size == UINT_MAX);
537 if (size == UINT_MAX)
538 return INT_MAX;
539 else
540 return size;
543 /* The "record instruction-history" command. */
545 static void
546 cmd_record_insn_history (const char *arg, int from_tty)
548 require_record_target ();
550 gdb_disassembly_flags flags = get_insn_history_modifiers (&arg);
552 int size = command_size_to_target_size (record_insn_history_size);
554 if (arg == NULL || *arg == 0 || strcmp (arg, "+") == 0)
555 target_insn_history (size, flags);
556 else if (strcmp (arg, "-") == 0)
557 target_insn_history (-size, flags);
558 else
560 ULONGEST begin, end;
562 begin = get_insn_number (&arg);
564 if (*arg == ',')
566 arg = skip_spaces (++arg);
568 if (*arg == '+')
570 arg += 1;
571 size = get_context_size (&arg);
573 no_chunk (arg);
575 target_insn_history_from (begin, size, flags);
577 else if (*arg == '-')
579 arg += 1;
580 size = get_context_size (&arg);
582 no_chunk (arg);
584 target_insn_history_from (begin, -size, flags);
586 else
588 end = get_insn_number (&arg);
590 no_chunk (arg);
592 target_insn_history_range (begin, end, flags);
595 else
597 no_chunk (arg);
599 target_insn_history_from (begin, size, flags);
602 dont_repeat ();
606 /* Read function-call-history modifiers from an argument string. */
608 static record_print_flags
609 get_call_history_modifiers (const char **arg)
611 record_print_flags modifiers = 0;
612 const char *args = *arg;
614 if (args == NULL)
615 return modifiers;
617 while (*args == '/')
619 ++args;
621 if (*args == '\0')
622 error (_("Missing modifier."));
624 for (; *args; ++args)
626 if (isspace (*args))
627 break;
629 if (*args == '/')
630 continue;
632 switch (*args)
634 case 'l':
635 modifiers |= RECORD_PRINT_SRC_LINE;
636 break;
637 case 'i':
638 modifiers |= RECORD_PRINT_INSN_RANGE;
639 break;
640 case 'c':
641 modifiers |= RECORD_PRINT_INDENT_CALLS;
642 break;
643 default:
644 error (_("Invalid modifier: %c."), *args);
648 args = skip_spaces (args);
651 /* Update the argument string. */
652 *arg = args;
654 return modifiers;
657 /* The "record function-call-history" command. */
659 static void
660 cmd_record_call_history (const char *arg, int from_tty)
662 require_record_target ();
664 record_print_flags flags = get_call_history_modifiers (&arg);
666 int size = command_size_to_target_size (record_call_history_size);
668 if (arg == NULL || *arg == 0 || strcmp (arg, "+") == 0)
669 target_call_history (size, flags);
670 else if (strcmp (arg, "-") == 0)
671 target_call_history (-size, flags);
672 else
674 ULONGEST begin, end;
676 begin = get_insn_number (&arg);
678 if (*arg == ',')
680 arg = skip_spaces (++arg);
682 if (*arg == '+')
684 arg += 1;
685 size = get_context_size (&arg);
687 no_chunk (arg);
689 target_call_history_from (begin, size, flags);
691 else if (*arg == '-')
693 arg += 1;
694 size = get_context_size (&arg);
696 no_chunk (arg);
698 target_call_history_from (begin, -size, flags);
700 else
702 end = get_insn_number (&arg);
704 no_chunk (arg);
706 target_call_history_range (begin, end, flags);
709 else
711 no_chunk (arg);
713 target_call_history_from (begin, size, flags);
716 dont_repeat ();
720 /* Helper for "set record instruction-history-size" and "set record
721 function-call-history-size" input validation. COMMAND_VAR is the
722 variable registered in the command as control variable. *SETTING
723 is the real setting the command allows changing. */
725 static void
726 validate_history_size (unsigned int *command_var, unsigned int *setting)
728 if (*command_var != UINT_MAX && *command_var > INT_MAX)
730 unsigned int new_value = *command_var;
732 /* Restore previous value. */
733 *command_var = *setting;
734 error (_("integer %u out of range"), new_value);
737 /* Commit new value. */
738 *setting = *command_var;
741 /* Called by do_setshow_command. We only want values in the
742 [0..INT_MAX] range, while the command's machinery accepts
743 [0..UINT_MAX]. See command_size_to_target_size. */
745 static void
746 set_record_insn_history_size (const char *args, int from_tty,
747 struct cmd_list_element *c)
749 validate_history_size (&record_insn_history_size_setshow_var,
750 &record_insn_history_size);
753 /* Called by do_setshow_command. We only want values in the
754 [0..INT_MAX] range, while the command's machinery accepts
755 [0..UINT_MAX]. See command_size_to_target_size. */
757 static void
758 set_record_call_history_size (const char *args, int from_tty,
759 struct cmd_list_element *c)
761 validate_history_size (&record_call_history_size_setshow_var,
762 &record_call_history_size);
765 void _initialize_record ();
766 void
767 _initialize_record ()
769 struct cmd_list_element *c;
771 add_setshow_zuinteger_cmd ("record", no_class, &record_debug,
772 _("Set debugging of record/replay feature."),
773 _("Show debugging of record/replay feature."),
774 _("When enabled, debugging output for "
775 "record/replay feature is displayed."),
776 NULL, show_record_debug, &setdebuglist,
777 &showdebuglist);
779 add_setshow_uinteger_cmd ("instruction-history-size", no_class,
780 &record_insn_history_size_setshow_var, _("\
781 Set number of instructions to print in \"record instruction-history\"."), _("\
782 Show number of instructions to print in \"record instruction-history\"."), _("\
783 A size of \"unlimited\" means unlimited instructions. The default is 10."),
784 set_record_insn_history_size, NULL,
785 &set_record_cmdlist, &show_record_cmdlist);
787 add_setshow_uinteger_cmd ("function-call-history-size", no_class,
788 &record_call_history_size_setshow_var, _("\
789 Set number of function to print in \"record function-call-history\"."), _("\
790 Show number of functions to print in \"record function-call-history\"."), _("\
791 A size of \"unlimited\" means unlimited lines. The default is 10."),
792 set_record_call_history_size, NULL,
793 &set_record_cmdlist, &show_record_cmdlist);
795 cmd_list_element *record_cmd
796 = add_prefix_cmd ("record", class_obscure, cmd_record_start,
797 _("Start recording."),
798 &record_cmdlist, 0, &cmdlist);
799 add_com_alias ("rec", record_cmd, class_obscure, 1);
801 set_show_commands setshow_record_cmds
802 = add_setshow_prefix_cmd ("record", class_support,
803 _("Set record options."),
804 _("Show record options."),
805 &set_record_cmdlist, &show_record_cmdlist,
806 &setlist, &showlist);
809 add_alias_cmd ("rec", setshow_record_cmds.set, class_obscure, 1, &setlist);
810 add_alias_cmd ("rec", setshow_record_cmds.show, class_obscure, 1, &showlist);
812 cmd_list_element *info_record_cmd
813 = add_prefix_cmd ("record", class_support, info_record_command,
814 _("Info record options."), &info_record_cmdlist,
815 0, &infolist);
816 add_alias_cmd ("rec", info_record_cmd, class_obscure, 1, &infolist);
818 c = add_cmd ("save", class_obscure, cmd_record_save,
819 _("Save the execution log to a file.\n\
820 Usage: record save [FILENAME]\n\
821 Default filename is 'gdb_record.PROCESS_ID'."),
822 &record_cmdlist);
823 set_cmd_completer (c, filename_completer);
825 cmd_list_element *record_delete_cmd
826 = add_cmd ("delete", class_obscure, cmd_record_delete,
827 _("Delete the rest of execution log and start recording it \
828 anew."),
829 &record_cmdlist);
830 add_alias_cmd ("d", record_delete_cmd, class_obscure, 1, &record_cmdlist);
831 add_alias_cmd ("del", record_delete_cmd, class_obscure, 1, &record_cmdlist);
833 cmd_list_element *record_stop_cmd
834 = add_cmd ("stop", class_obscure, cmd_record_stop,
835 _("Stop the record/replay target."),
836 &record_cmdlist);
837 add_alias_cmd ("s", record_stop_cmd, class_obscure, 1, &record_cmdlist);
839 add_prefix_cmd ("goto", class_obscure, cmd_record_goto, _("\
840 Restore the program to its state at instruction number N.\n\
841 Argument is instruction number, as shown by 'info record'."),
842 &record_goto_cmdlist, 1, &record_cmdlist);
844 cmd_list_element *record_goto_begin_cmd
845 = add_cmd ("begin", class_obscure, cmd_record_goto_begin,
846 _("Go to the beginning of the execution log."),
847 &record_goto_cmdlist);
848 add_alias_cmd ("start", record_goto_begin_cmd, class_obscure, 1,
849 &record_goto_cmdlist);
851 add_cmd ("end", class_obscure, cmd_record_goto_end,
852 _("Go to the end of the execution log."),
853 &record_goto_cmdlist);
855 add_cmd ("instruction-history", class_obscure, cmd_record_insn_history, _("\
856 Print disassembled instructions stored in the execution log.\n\
857 With a /m or /s modifier, source lines are included (if available).\n\
858 With a /r modifier, raw instructions in hex are included.\n\
859 With a /f modifier, function names are omitted.\n\
860 With a /p modifier, current position markers are omitted.\n\
861 With no argument, disassembles ten more instructions after the previous \
862 disassembly.\n\
863 \"record instruction-history -\" disassembles ten instructions before a \
864 previous disassembly.\n\
865 One argument specifies an instruction number as shown by 'info record', and \
866 ten instructions are disassembled after that instruction.\n\
867 Two arguments with comma between them specify starting and ending instruction \
868 numbers to disassemble.\n\
869 If the second argument is preceded by '+' or '-', it specifies the distance \
870 from the first argument.\n\
871 The number of instructions to disassemble can be defined with \"set record \
872 instruction-history-size\"."),
873 &record_cmdlist);
875 add_cmd ("function-call-history", class_obscure, cmd_record_call_history, _("\
876 Prints the execution history at function granularity.\n\
877 It prints one line for each sequence of instructions that belong to the same \
878 function.\n\
879 Without modifiers, it prints the function name.\n\
880 With a /l modifier, the source file and line number range is included.\n\
881 With a /i modifier, the instruction number range is included.\n\
882 With a /c modifier, the output is indented based on the call stack depth.\n\
883 With no argument, prints ten more lines after the previous ten-line print.\n\
884 \"record function-call-history -\" prints ten lines before a previous ten-line \
885 print.\n\
886 One argument specifies a function number as shown by 'info record', and \
887 ten lines are printed after that function.\n\
888 Two arguments with comma between them specify a range of functions to print.\n\
889 If the second argument is preceded by '+' or '-', it specifies the distance \
890 from the first argument.\n\
891 The number of functions to print can be defined with \"set record \
892 function-call-history-size\"."),
893 &record_cmdlist);
895 /* Sync command control variables. */
896 record_insn_history_size_setshow_var = record_insn_history_size;
897 record_call_history_size_setshow_var = record_call_history_size;