Automatic date update in version.in
[binutils-gdb.git] / gdb / record-full.c
blob22a513ac79abfcf2d86d05b193a87085a71c4df8
1 /* Process record and replay target for GDB, the GNU debugger.
3 Copyright (C) 2013-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 "exceptions.h"
21 #include "extract-store-integer.h"
22 #include "cli/cli-cmds.h"
23 #include "regcache.h"
24 #include "gdbthread.h"
25 #include "inferior.h"
26 #include "event-top.h"
27 #include "completer.h"
28 #include "arch-utils.h"
29 #include "gdbcore.h"
30 #include "exec.h"
31 #include "record.h"
32 #include "record-full.h"
33 #include "elf-bfd.h"
34 #include "gcore.h"
35 #include "gdbsupport/event-loop.h"
36 #include "inf-loop.h"
37 #include "gdb_bfd.h"
38 #include "observable.h"
39 #include "infrun.h"
40 #include "gdbsupport/gdb_unlinker.h"
41 #include "gdbsupport/byte-vector.h"
42 #include "async-event.h"
43 #include "top.h"
44 #include "valprint.h"
45 #include "interps.h"
47 #include <signal.h>
49 /* This module implements "target record-full", also known as "process
50 record and replay". This target sits on top of a "normal" target
51 (a target that "has execution"), and provides a record and replay
52 functionality, including reverse debugging.
54 Target record has two modes: recording, and replaying.
56 In record mode, we intercept the resume and wait methods.
57 Whenever gdb resumes the target, we run the target in single step
58 mode, and we build up an execution log in which, for each executed
59 instruction, we record all changes in memory and register state.
60 This is invisible to the user, to whom it just looks like an
61 ordinary debugging session (except for performance degradation).
63 In replay mode, instead of actually letting the inferior run as a
64 process, we simulate its execution by playing back the recorded
65 execution log. For each instruction in the log, we simulate the
66 instruction's side effects by duplicating the changes that it would
67 have made on memory and registers. */
69 #define DEFAULT_RECORD_FULL_INSN_MAX_NUM 200000
71 #define RECORD_FULL_IS_REPLAY \
72 (record_full_list->next || ::execution_direction == EXEC_REVERSE)
74 #define RECORD_FULL_FILE_MAGIC netorder32(0x20091016)
76 /* These are the core structs of the process record functionality.
78 A record_full_entry is a record of the value change of a register
79 ("record_full_reg") or a part of memory ("record_full_mem"). And each
80 instruction must have a struct record_full_entry ("record_full_end")
81 that indicates that this is the last struct record_full_entry of this
82 instruction.
84 Each struct record_full_entry is linked to "record_full_list" by "prev"
85 and "next" pointers. */
87 struct record_full_mem_entry
89 CORE_ADDR addr;
90 int len;
91 /* Set this flag if target memory for this entry
92 can no longer be accessed. */
93 int mem_entry_not_accessible;
94 union
96 gdb_byte *ptr;
97 gdb_byte buf[sizeof (gdb_byte *)];
98 } u;
101 struct record_full_reg_entry
103 unsigned short num;
104 unsigned short len;
105 union
107 gdb_byte *ptr;
108 gdb_byte buf[2 * sizeof (gdb_byte *)];
109 } u;
112 struct record_full_end_entry
114 enum gdb_signal sigval;
115 ULONGEST insn_num;
118 enum record_full_type
120 record_full_end = 0,
121 record_full_reg,
122 record_full_mem
125 /* This is the data structure that makes up the execution log.
127 The execution log consists of a single linked list of entries
128 of type "struct record_full_entry". It is doubly linked so that it
129 can be traversed in either direction.
131 The start of the list is anchored by a struct called
132 "record_full_first". The pointer "record_full_list" either points
133 to the last entry that was added to the list (in record mode), or to
134 the next entry in the list that will be executed (in replay mode).
136 Each list element (struct record_full_entry), in addition to next
137 and prev pointers, consists of a union of three entry types: mem,
138 reg, and end. A field called "type" determines which entry type is
139 represented by a given list element.
141 Each instruction that is added to the execution log is represented
142 by a variable number of list elements ('entries'). The instruction
143 will have one "reg" entry for each register that is changed by
144 executing the instruction (including the PC in every case). It
145 will also have one "mem" entry for each memory change. Finally,
146 each instruction will have an "end" entry that separates it from
147 the changes associated with the next instruction. */
149 struct record_full_entry
151 struct record_full_entry *prev;
152 struct record_full_entry *next;
153 enum record_full_type type;
154 union
156 /* reg */
157 struct record_full_reg_entry reg;
158 /* mem */
159 struct record_full_mem_entry mem;
160 /* end */
161 struct record_full_end_entry end;
162 } u;
165 /* If true, query if PREC cannot record memory
166 change of next instruction. */
167 bool record_full_memory_query = false;
169 struct record_full_core_buf_entry
171 struct record_full_core_buf_entry *prev;
172 struct target_section *p;
173 bfd_byte *buf;
176 /* Record buf with core target. */
177 static detached_regcache *record_full_core_regbuf = NULL;
178 static std::vector<target_section> record_full_core_sections;
179 static struct record_full_core_buf_entry *record_full_core_buf_list = NULL;
181 /* The following variables are used for managing the linked list that
182 represents the execution log.
184 record_full_first is the anchor that holds down the beginning of
185 the list.
187 record_full_list serves two functions:
188 1) In record mode, it anchors the end of the list.
189 2) In replay mode, it traverses the list and points to
190 the next instruction that must be emulated.
192 record_full_arch_list_head and record_full_arch_list_tail are used
193 to manage a separate list, which is used to build up the change
194 elements of the currently executing instruction during record mode.
195 When this instruction has been completely annotated in the "arch
196 list", it will be appended to the main execution log. */
198 static struct record_full_entry record_full_first;
199 static struct record_full_entry *record_full_list = &record_full_first;
200 static struct record_full_entry *record_full_arch_list_head = NULL;
201 static struct record_full_entry *record_full_arch_list_tail = NULL;
203 /* true ask user. false auto delete the last struct record_full_entry. */
204 static bool record_full_stop_at_limit = true;
205 /* Maximum allowed number of insns in execution log. */
206 static unsigned int record_full_insn_max_num
207 = DEFAULT_RECORD_FULL_INSN_MAX_NUM;
208 /* Actual count of insns presently in execution log. */
209 static unsigned int record_full_insn_num = 0;
210 /* Count of insns logged so far (may be larger
211 than count of insns presently in execution log). */
212 static ULONGEST record_full_insn_count;
214 static const char record_longname[]
215 = N_("Process record and replay target");
216 static const char record_doc[]
217 = N_("Log program while executing and replay execution from log.");
219 /* Base class implementing functionality common to both the
220 "record-full" and "record-core" targets. */
222 class record_full_base_target : public target_ops
224 public:
225 const target_info &info () const override = 0;
227 strata stratum () const override { return record_stratum; }
229 void close () override;
230 void async (bool) override;
231 ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override;
232 bool stopped_by_watchpoint () override;
233 bool stopped_data_address (CORE_ADDR *) override;
235 bool stopped_by_sw_breakpoint () override;
236 bool supports_stopped_by_sw_breakpoint () override;
238 bool stopped_by_hw_breakpoint () override;
239 bool supports_stopped_by_hw_breakpoint () override;
241 bool can_execute_reverse () override;
243 /* Add bookmark target methods. */
244 gdb_byte *get_bookmark (const char *, int) override;
245 void goto_bookmark (const gdb_byte *, int) override;
246 enum exec_direction_kind execution_direction () override;
247 enum record_method record_method (ptid_t ptid) override;
248 void info_record () override;
249 void save_record (const char *filename) override;
250 bool supports_delete_record () override;
251 void delete_record () override;
252 bool record_is_replaying (ptid_t ptid) override;
253 bool record_will_replay (ptid_t ptid, int dir) override;
254 void record_stop_replaying () override;
255 void goto_record_begin () override;
256 void goto_record_end () override;
257 void goto_record (ULONGEST insn) override;
260 /* The "record-full" target. */
262 static const target_info record_full_target_info = {
263 "record-full",
264 record_longname,
265 record_doc,
268 class record_full_target final : public record_full_base_target
270 public:
271 const target_info &info () const override
272 { return record_full_target_info; }
274 void resume (ptid_t, int, enum gdb_signal) override;
275 void disconnect (const char *, int) override;
276 void detach (inferior *, int) override;
277 void mourn_inferior () override;
278 void kill () override;
279 void store_registers (struct regcache *, int) override;
280 enum target_xfer_status xfer_partial (enum target_object object,
281 const char *annex,
282 gdb_byte *readbuf,
283 const gdb_byte *writebuf,
284 ULONGEST offset, ULONGEST len,
285 ULONGEST *xfered_len) override;
286 int insert_breakpoint (struct gdbarch *,
287 struct bp_target_info *) override;
288 int remove_breakpoint (struct gdbarch *,
289 struct bp_target_info *,
290 enum remove_bp_reason) override;
293 /* The "record-core" target. */
295 static const target_info record_full_core_target_info = {
296 "record-core",
297 record_longname,
298 record_doc,
301 class record_full_core_target final : public record_full_base_target
303 public:
304 const target_info &info () const override
305 { return record_full_core_target_info; }
307 void resume (ptid_t, int, enum gdb_signal) override;
308 void disconnect (const char *, int) override;
309 void kill () override;
310 void fetch_registers (struct regcache *regcache, int regno) override;
311 void prepare_to_store (struct regcache *regcache) override;
312 void store_registers (struct regcache *, int) override;
313 enum target_xfer_status xfer_partial (enum target_object object,
314 const char *annex,
315 gdb_byte *readbuf,
316 const gdb_byte *writebuf,
317 ULONGEST offset, ULONGEST len,
318 ULONGEST *xfered_len) override;
319 int insert_breakpoint (struct gdbarch *,
320 struct bp_target_info *) override;
321 int remove_breakpoint (struct gdbarch *,
322 struct bp_target_info *,
323 enum remove_bp_reason) override;
325 bool has_execution (inferior *inf) override;
328 static record_full_target record_full_ops;
329 static record_full_core_target record_full_core_ops;
331 void
332 record_full_target::detach (inferior *inf, int from_tty)
334 record_detach (this, inf, from_tty);
337 void
338 record_full_target::disconnect (const char *args, int from_tty)
340 record_disconnect (this, args, from_tty);
343 void
344 record_full_core_target::disconnect (const char *args, int from_tty)
346 record_disconnect (this, args, from_tty);
349 void
350 record_full_target::mourn_inferior ()
352 record_mourn_inferior (this);
355 void
356 record_full_target::kill ()
358 record_kill (this);
361 /* See record-full.h. */
364 record_full_is_used (void)
366 struct target_ops *t;
368 t = find_record_target ();
369 return (t == &record_full_ops
370 || t == &record_full_core_ops);
373 /* see record-full.h. */
374 bool
375 record_full_is_replaying ()
377 auto target = dynamic_cast<record_full_target *>
378 (current_inferior ()->target_at (record_stratum));
379 return target != nullptr && RECORD_FULL_IS_REPLAY;
383 /* Command lists for "set/show record full". */
384 static struct cmd_list_element *set_record_full_cmdlist;
385 static struct cmd_list_element *show_record_full_cmdlist;
387 /* Command list for "record full". */
388 static struct cmd_list_element *record_full_cmdlist;
390 static void record_full_goto_insn (struct record_full_entry *entry,
391 enum exec_direction_kind dir);
393 /* Alloc and free functions for record_full_reg, record_full_mem, and
394 record_full_end entries. */
396 /* Alloc a record_full_reg record entry. */
398 static inline struct record_full_entry *
399 record_full_reg_alloc (struct regcache *regcache, int regnum)
401 struct record_full_entry *rec;
402 struct gdbarch *gdbarch = regcache->arch ();
404 rec = XCNEW (struct record_full_entry);
405 rec->type = record_full_reg;
406 rec->u.reg.num = regnum;
407 rec->u.reg.len = register_size (gdbarch, regnum);
408 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
409 rec->u.reg.u.ptr = (gdb_byte *) xmalloc (rec->u.reg.len);
411 return rec;
414 /* Free a record_full_reg record entry. */
416 static inline void
417 record_full_reg_release (struct record_full_entry *rec)
419 gdb_assert (rec->type == record_full_reg);
420 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
421 xfree (rec->u.reg.u.ptr);
422 xfree (rec);
425 /* Alloc a record_full_mem record entry. */
427 static inline struct record_full_entry *
428 record_full_mem_alloc (CORE_ADDR addr, int len)
430 struct record_full_entry *rec;
432 rec = XCNEW (struct record_full_entry);
433 rec->type = record_full_mem;
434 rec->u.mem.addr = addr;
435 rec->u.mem.len = len;
436 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
437 rec->u.mem.u.ptr = (gdb_byte *) xmalloc (len);
439 return rec;
442 /* Free a record_full_mem record entry. */
444 static inline void
445 record_full_mem_release (struct record_full_entry *rec)
447 gdb_assert (rec->type == record_full_mem);
448 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
449 xfree (rec->u.mem.u.ptr);
450 xfree (rec);
453 /* Alloc a record_full_end record entry. */
455 static inline struct record_full_entry *
456 record_full_end_alloc (void)
458 struct record_full_entry *rec;
460 rec = XCNEW (struct record_full_entry);
461 rec->type = record_full_end;
463 return rec;
466 /* Free a record_full_end record entry. */
468 static inline void
469 record_full_end_release (struct record_full_entry *rec)
471 xfree (rec);
474 /* Free one record entry, any type.
475 Return entry->type, in case caller wants to know. */
477 static inline enum record_full_type
478 record_full_entry_release (struct record_full_entry *rec)
480 enum record_full_type type = rec->type;
482 switch (type) {
483 case record_full_reg:
484 record_full_reg_release (rec);
485 break;
486 case record_full_mem:
487 record_full_mem_release (rec);
488 break;
489 case record_full_end:
490 record_full_end_release (rec);
491 break;
493 return type;
496 /* Free all record entries in list pointed to by REC. */
498 static void
499 record_full_list_release (struct record_full_entry *rec)
501 if (!rec)
502 return;
504 while (rec->next)
505 rec = rec->next;
507 while (rec->prev)
509 rec = rec->prev;
510 record_full_entry_release (rec->next);
513 if (rec == &record_full_first)
515 record_full_insn_num = 0;
516 record_full_first.next = NULL;
518 else
519 record_full_entry_release (rec);
522 /* Free all record entries forward of the given list position. */
524 static void
525 record_full_list_release_following (struct record_full_entry *rec)
527 struct record_full_entry *tmp = rec->next;
529 rec->next = NULL;
530 while (tmp)
532 rec = tmp->next;
533 if (record_full_entry_release (tmp) == record_full_end)
535 record_full_insn_num--;
536 record_full_insn_count--;
538 tmp = rec;
542 /* Delete the first instruction from the beginning of the log, to make
543 room for adding a new instruction at the end of the log.
545 Note -- this function does not modify record_full_insn_num. */
547 static void
548 record_full_list_release_first (void)
550 struct record_full_entry *tmp;
552 if (!record_full_first.next)
553 return;
555 /* Loop until a record_full_end. */
556 while (1)
558 /* Cut record_full_first.next out of the linked list. */
559 tmp = record_full_first.next;
560 record_full_first.next = tmp->next;
561 tmp->next->prev = &record_full_first;
563 /* tmp is now isolated, and can be deleted. */
564 if (record_full_entry_release (tmp) == record_full_end)
565 break; /* End loop at first record_full_end. */
567 if (!record_full_first.next)
569 gdb_assert (record_full_insn_num == 1);
570 break; /* End loop when list is empty. */
575 /* Add a struct record_full_entry to record_full_arch_list. */
577 static void
578 record_full_arch_list_add (struct record_full_entry *rec)
580 if (record_debug > 1)
581 gdb_printf (gdb_stdlog,
582 "Process record: record_full_arch_list_add %s.\n",
583 host_address_to_string (rec));
585 if (record_full_arch_list_tail)
587 record_full_arch_list_tail->next = rec;
588 rec->prev = record_full_arch_list_tail;
589 record_full_arch_list_tail = rec;
591 else
593 record_full_arch_list_head = rec;
594 record_full_arch_list_tail = rec;
598 /* Return the value storage location of a record entry. */
599 static inline gdb_byte *
600 record_full_get_loc (struct record_full_entry *rec)
602 switch (rec->type) {
603 case record_full_mem:
604 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
605 return rec->u.mem.u.ptr;
606 else
607 return rec->u.mem.u.buf;
608 case record_full_reg:
609 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
610 return rec->u.reg.u.ptr;
611 else
612 return rec->u.reg.u.buf;
613 case record_full_end:
614 default:
615 gdb_assert_not_reached ("unexpected record_full_entry type");
616 return NULL;
620 /* Record the value of a register NUM to record_full_arch_list. */
623 record_full_arch_list_add_reg (struct regcache *regcache, int regnum)
625 struct record_full_entry *rec;
627 if (record_debug > 1)
628 gdb_printf (gdb_stdlog,
629 "Process record: add register num = %d to "
630 "record list.\n",
631 regnum);
633 rec = record_full_reg_alloc (regcache, regnum);
635 regcache->cooked_read (regnum, record_full_get_loc (rec));
637 record_full_arch_list_add (rec);
639 return 0;
642 /* Record the value of a region of memory whose address is ADDR and
643 length is LEN to record_full_arch_list. */
646 record_full_arch_list_add_mem (CORE_ADDR addr, int len)
648 struct record_full_entry *rec;
650 if (record_debug > 1)
651 gdb_printf (gdb_stdlog,
652 "Process record: add mem addr = %s len = %d to "
653 "record list.\n",
654 paddress (current_inferior ()->arch (), addr), len);
656 if (!addr) /* FIXME: Why? Some arch must permit it... */
657 return 0;
659 rec = record_full_mem_alloc (addr, len);
661 if (record_read_memory (current_inferior ()->arch (), addr,
662 record_full_get_loc (rec), len))
664 record_full_mem_release (rec);
665 return -1;
668 record_full_arch_list_add (rec);
670 return 0;
673 /* Add a record_full_end type struct record_full_entry to
674 record_full_arch_list. */
677 record_full_arch_list_add_end (void)
679 struct record_full_entry *rec;
681 if (record_debug > 1)
682 gdb_printf (gdb_stdlog,
683 "Process record: add end to arch list.\n");
685 rec = record_full_end_alloc ();
686 rec->u.end.sigval = GDB_SIGNAL_0;
687 rec->u.end.insn_num = ++record_full_insn_count;
689 record_full_arch_list_add (rec);
691 return 0;
694 static void
695 record_full_check_insn_num (void)
697 if (record_full_insn_num == record_full_insn_max_num)
699 /* Ask user what to do. */
700 if (record_full_stop_at_limit)
702 if (!yquery (_("Do you want to auto delete previous execution "
703 "log entries when record/replay buffer becomes "
704 "full (record full stop-at-limit)?")))
705 error (_("Process record: stopped by user."));
706 record_full_stop_at_limit = 0;
711 /* Before inferior step (when GDB record the running message, inferior
712 only can step), GDB will call this function to record the values to
713 record_full_list. This function will call gdbarch_process_record to
714 record the running message of inferior and set them to
715 record_full_arch_list, and add it to record_full_list. */
717 static void
718 record_full_message (struct regcache *regcache, enum gdb_signal signal)
720 int ret;
721 struct gdbarch *gdbarch = regcache->arch ();
725 record_full_arch_list_head = NULL;
726 record_full_arch_list_tail = NULL;
728 /* Check record_full_insn_num. */
729 record_full_check_insn_num ();
731 /* If gdb sends a signal value to target_resume,
732 save it in the 'end' field of the previous instruction.
734 Maybe process record should record what really happened,
735 rather than what gdb pretends has happened.
737 So if Linux delivered the signal to the child process during
738 the record mode, we will record it and deliver it again in
739 the replay mode.
741 If user says "ignore this signal" during the record mode, then
742 it will be ignored again during the replay mode (no matter if
743 the user says something different, like "deliver this signal"
744 during the replay mode).
746 User should understand that nothing he does during the replay
747 mode will change the behavior of the child. If he tries,
748 then that is a user error.
750 But we should still deliver the signal to gdb during the replay,
751 if we delivered it during the recording. Therefore we should
752 record the signal during record_full_wait, not
753 record_full_resume. */
754 if (record_full_list != &record_full_first) /* FIXME better way
755 to check */
757 gdb_assert (record_full_list->type == record_full_end);
758 record_full_list->u.end.sigval = signal;
761 if (signal == GDB_SIGNAL_0
762 || !gdbarch_process_record_signal_p (gdbarch))
763 ret = gdbarch_process_record (gdbarch,
764 regcache,
765 regcache_read_pc (regcache));
766 else
767 ret = gdbarch_process_record_signal (gdbarch,
768 regcache,
769 signal);
771 if (ret > 0)
772 error (_("Process record: inferior program stopped."));
773 if (ret < 0)
774 error (_("Process record: failed to record execution log."));
776 catch (const gdb_exception &ex)
778 record_full_list_release (record_full_arch_list_tail);
779 throw;
782 record_full_list->next = record_full_arch_list_head;
783 record_full_arch_list_head->prev = record_full_list;
784 record_full_list = record_full_arch_list_tail;
786 if (record_full_insn_num == record_full_insn_max_num)
787 record_full_list_release_first ();
788 else
789 record_full_insn_num++;
792 static bool
793 record_full_message_wrapper_safe (struct regcache *regcache,
794 enum gdb_signal signal)
798 record_full_message (regcache, signal);
800 catch (const gdb_exception_error &ex)
802 exception_print (gdb_stderr, ex);
803 return false;
806 return true;
809 /* Set to 1 if record_full_store_registers and record_full_xfer_partial
810 doesn't need record. */
812 static int record_full_gdb_operation_disable = 0;
814 scoped_restore_tmpl<int>
815 record_full_gdb_operation_disable_set (void)
817 return make_scoped_restore (&record_full_gdb_operation_disable, 1);
820 /* Flag set to TRUE for target_stopped_by_watchpoint. */
821 static enum target_stop_reason record_full_stop_reason
822 = TARGET_STOPPED_BY_NO_REASON;
824 /* Execute one instruction from the record log. Each instruction in
825 the log will be represented by an arbitrary sequence of register
826 entries and memory entries, followed by an 'end' entry. */
828 static inline void
829 record_full_exec_insn (struct regcache *regcache,
830 struct gdbarch *gdbarch,
831 struct record_full_entry *entry)
833 switch (entry->type)
835 case record_full_reg: /* reg */
837 gdb::byte_vector reg (entry->u.reg.len);
839 if (record_debug > 1)
840 gdb_printf (gdb_stdlog,
841 "Process record: record_full_reg %s to "
842 "inferior num = %d.\n",
843 host_address_to_string (entry),
844 entry->u.reg.num);
846 regcache->cooked_read (entry->u.reg.num, reg.data ());
847 regcache->cooked_write (entry->u.reg.num, record_full_get_loc (entry));
848 memcpy (record_full_get_loc (entry), reg.data (), entry->u.reg.len);
850 break;
852 case record_full_mem: /* mem */
854 /* Nothing to do if the entry is flagged not_accessible. */
855 if (!entry->u.mem.mem_entry_not_accessible)
857 gdb::byte_vector mem (entry->u.mem.len);
859 if (record_debug > 1)
860 gdb_printf (gdb_stdlog,
861 "Process record: record_full_mem %s to "
862 "inferior addr = %s len = %d.\n",
863 host_address_to_string (entry),
864 paddress (gdbarch, entry->u.mem.addr),
865 entry->u.mem.len);
867 if (record_read_memory (gdbarch,
868 entry->u.mem.addr, mem.data (),
869 entry->u.mem.len))
870 entry->u.mem.mem_entry_not_accessible = 1;
871 else
873 if (target_write_memory (entry->u.mem.addr,
874 record_full_get_loc (entry),
875 entry->u.mem.len))
877 entry->u.mem.mem_entry_not_accessible = 1;
878 if (record_debug)
879 warning (_("Process record: error writing memory at "
880 "addr = %s len = %d."),
881 paddress (gdbarch, entry->u.mem.addr),
882 entry->u.mem.len);
884 else
886 memcpy (record_full_get_loc (entry), mem.data (),
887 entry->u.mem.len);
889 /* We've changed memory --- check if a hardware
890 watchpoint should trap. Note that this
891 presently assumes the target beneath supports
892 continuable watchpoints. On non-continuable
893 watchpoints target, we'll want to check this
894 _before_ actually doing the memory change, and
895 not doing the change at all if the watchpoint
896 traps. */
897 if (hardware_watchpoint_inserted_in_range
898 (current_inferior ()->aspace.get (),
899 entry->u.mem.addr, entry->u.mem.len))
900 record_full_stop_reason = TARGET_STOPPED_BY_WATCHPOINT;
905 break;
909 static void record_full_restore (void);
911 /* Asynchronous signal handle registered as event loop source for when
912 we have pending events ready to be passed to the core. */
914 static struct async_event_handler *record_full_async_inferior_event_token;
916 static void
917 record_full_async_inferior_event_handler (gdb_client_data data)
919 inferior_event_handler (INF_REG_EVENT);
922 /* Open the process record target for 'core' files. */
924 static void
925 record_full_core_open_1 ()
927 regcache *regcache = get_thread_regcache (inferior_thread ());
928 int regnum = gdbarch_num_regs (regcache->arch ());
929 int i;
931 /* Get record_full_core_regbuf. */
932 target_fetch_registers (regcache, -1);
933 record_full_core_regbuf = new detached_regcache (regcache->arch (), false);
935 for (i = 0; i < regnum; i ++)
936 record_full_core_regbuf->raw_supply (i, *regcache);
938 record_full_core_sections
939 = build_section_table (current_program_space->core_bfd ());
941 current_inferior ()->push_target (&record_full_core_ops);
942 record_full_restore ();
945 /* Open the process record target for 'live' processes. */
947 static void
948 record_full_open_1 ()
950 if (record_debug)
951 gdb_printf (gdb_stdlog, "Process record: record_full_open_1\n");
953 /* check exec */
954 if (!target_has_execution ())
955 error (_("Process record: the program is not being run."));
956 if (non_stop)
957 error (_("Process record target can't debug inferior in non-stop mode "
958 "(non-stop)."));
960 if (!gdbarch_process_record_p (current_inferior ()->arch ()))
961 error (_("Process record: the current architecture doesn't support "
962 "record function."));
964 current_inferior ()->push_target (&record_full_ops);
967 static void record_full_init_record_breakpoints (void);
969 /* Open the process record target. */
971 static void
972 record_full_open (const char *args, int from_tty)
974 if (record_debug)
975 gdb_printf (gdb_stdlog, "Process record: record_full_open\n");
977 if (args != nullptr)
978 error (_("Trailing junk: '%s'"), args);
980 record_preopen ();
982 /* Reset */
983 record_full_insn_num = 0;
984 record_full_insn_count = 0;
985 record_full_list = &record_full_first;
986 record_full_list->next = NULL;
988 if (current_program_space->core_bfd ())
989 record_full_core_open_1 ();
990 else
991 record_full_open_1 ();
993 /* Register extra event sources in the event loop. */
994 record_full_async_inferior_event_token
995 = create_async_event_handler (record_full_async_inferior_event_handler,
996 NULL, "record-full");
998 record_full_init_record_breakpoints ();
1000 interps_notify_record_changed (current_inferior (), 1, "full", NULL);
1003 /* "close" target method. Close the process record target. */
1005 void
1006 record_full_base_target::close ()
1008 struct record_full_core_buf_entry *entry;
1010 if (record_debug)
1011 gdb_printf (gdb_stdlog, "Process record: record_full_close\n");
1013 record_full_list_release (record_full_list);
1015 /* Release record_full_core_regbuf. */
1016 if (record_full_core_regbuf)
1018 delete record_full_core_regbuf;
1019 record_full_core_regbuf = NULL;
1022 /* Release record_full_core_buf_list. */
1023 while (record_full_core_buf_list)
1025 entry = record_full_core_buf_list;
1026 record_full_core_buf_list = record_full_core_buf_list->prev;
1027 xfree (entry);
1030 if (record_full_async_inferior_event_token)
1031 delete_async_event_handler (&record_full_async_inferior_event_token);
1034 /* "async" target method. */
1036 void
1037 record_full_base_target::async (bool enable)
1039 if (enable)
1040 mark_async_event_handler (record_full_async_inferior_event_token);
1041 else
1042 clear_async_event_handler (record_full_async_inferior_event_token);
1044 beneath ()->async (enable);
1047 /* The PTID and STEP arguments last passed to
1048 record_full_target::resume. */
1049 static ptid_t record_full_resume_ptid = null_ptid;
1050 static int record_full_resume_step = 0;
1052 /* True if we've been resumed, and so each record_full_wait call should
1053 advance execution. If this is false, record_full_wait will return a
1054 TARGET_WAITKIND_IGNORE. */
1055 static int record_full_resumed = 0;
1057 /* The execution direction of the last resume we got. This is
1058 necessary for async mode. Vis (order is not strictly accurate):
1060 1. user has the global execution direction set to forward
1061 2. user does a reverse-step command
1062 3. record_full_resume is called with global execution direction
1063 temporarily switched to reverse
1064 4. GDB's execution direction is reverted back to forward
1065 5. target record notifies event loop there's an event to handle
1066 6. infrun asks the target which direction was it going, and switches
1067 the global execution direction accordingly (to reverse)
1068 7. infrun polls an event out of the record target, and handles it
1069 8. GDB goes back to the event loop, and goto #4.
1071 static enum exec_direction_kind record_full_execution_dir = EXEC_FORWARD;
1073 /* "resume" target method. Resume the process record target. */
1075 void
1076 record_full_target::resume (ptid_t ptid, int step, enum gdb_signal signal)
1078 record_full_resume_ptid = inferior_ptid;
1079 record_full_resume_step = step;
1080 record_full_resumed = 1;
1081 record_full_execution_dir = ::execution_direction;
1083 if (!RECORD_FULL_IS_REPLAY)
1085 struct gdbarch *gdbarch = target_thread_architecture (ptid);
1087 record_full_message (get_thread_regcache (inferior_thread ()), signal);
1089 if (!step)
1091 /* This is not hard single step. */
1092 if (!gdbarch_software_single_step_p (gdbarch))
1094 /* This is a normal continue. */
1095 step = 1;
1097 else
1099 /* This arch supports soft single step. */
1100 if (thread_has_single_step_breakpoints_set (inferior_thread ()))
1102 /* This is a soft single step. */
1103 record_full_resume_step = 1;
1105 else
1106 step = !insert_single_step_breakpoints (gdbarch);
1110 /* Make sure the target beneath reports all signals. */
1111 target_pass_signals ({});
1113 /* Disable range-stepping, forcing the process target to report stops for
1114 all executed instructions, so we can record them all. */
1115 process_stratum_target *proc_target
1116 = current_inferior ()->process_target ();
1117 for (thread_info *thread : all_non_exited_threads (proc_target, ptid))
1118 thread->control.may_range_step = 0;
1120 this->beneath ()->resume (ptid, step, signal);
1124 static int record_full_get_sig = 0;
1126 /* SIGINT signal handler, registered by "wait" method. */
1128 static void
1129 record_full_sig_handler (int signo)
1131 if (record_debug)
1132 gdb_printf (gdb_stdlog, "Process record: get a signal\n");
1134 /* It will break the running inferior in replay mode. */
1135 record_full_resume_step = 1;
1137 /* It will let record_full_wait set inferior status to get the signal
1138 SIGINT. */
1139 record_full_get_sig = 1;
1142 /* "wait" target method for process record target.
1144 In record mode, the target is always run in singlestep mode
1145 (even when gdb says to continue). The wait method intercepts
1146 the stop events and determines which ones are to be passed on to
1147 gdb. Most stop events are just singlestep events that gdb is not
1148 to know about, so the wait method just records them and keeps
1149 singlestepping.
1151 In replay mode, this function emulates the recorded execution log,
1152 one instruction at a time (forward or backward), and determines
1153 where to stop. */
1155 static ptid_t
1156 record_full_wait_1 (struct target_ops *ops,
1157 ptid_t ptid, struct target_waitstatus *status,
1158 target_wait_flags options)
1160 scoped_restore restore_operation_disable
1161 = record_full_gdb_operation_disable_set ();
1163 if (record_debug)
1164 gdb_printf (gdb_stdlog,
1165 "Process record: record_full_wait "
1166 "record_full_resume_step = %d, "
1167 "record_full_resumed = %d, direction=%s\n",
1168 record_full_resume_step, record_full_resumed,
1169 record_full_execution_dir == EXEC_FORWARD
1170 ? "forward" : "reverse");
1172 if (!record_full_resumed)
1174 gdb_assert ((options & TARGET_WNOHANG) != 0);
1176 /* No interesting event. */
1177 status->set_ignore ();
1178 return minus_one_ptid;
1181 record_full_get_sig = 0;
1182 signal (SIGINT, record_full_sig_handler);
1184 record_full_stop_reason = TARGET_STOPPED_BY_NO_REASON;
1186 if (!RECORD_FULL_IS_REPLAY && ops != &record_full_core_ops)
1188 if (record_full_resume_step)
1190 /* This is a single step. */
1191 return ops->beneath ()->wait (ptid, status, options);
1193 else
1195 /* This is not a single step. */
1196 ptid_t ret;
1197 CORE_ADDR tmp_pc;
1198 struct gdbarch *gdbarch
1199 = target_thread_architecture (record_full_resume_ptid);
1201 while (1)
1203 ret = ops->beneath ()->wait (ptid, status, options);
1204 if (status->kind () == TARGET_WAITKIND_IGNORE)
1206 if (record_debug)
1207 gdb_printf (gdb_stdlog,
1208 "Process record: record_full_wait "
1209 "target beneath not done yet\n");
1210 return ret;
1213 for (thread_info *tp : all_non_exited_threads ())
1214 delete_single_step_breakpoints (tp);
1216 if (record_full_resume_step)
1217 return ret;
1219 /* Is this a SIGTRAP? */
1220 if (status->kind () == TARGET_WAITKIND_STOPPED
1221 && status->sig () == GDB_SIGNAL_TRAP)
1223 struct regcache *regcache;
1224 enum target_stop_reason *stop_reason_p
1225 = &record_full_stop_reason;
1227 /* Yes -- this is likely our single-step finishing,
1228 but check if there's any reason the core would be
1229 interested in the event. */
1231 registers_changed ();
1232 switch_to_thread (current_inferior ()->process_target (),
1233 ret);
1234 regcache = get_thread_regcache (inferior_thread ());
1235 tmp_pc = regcache_read_pc (regcache);
1236 const address_space *aspace
1237 = current_inferior ()->aspace.get ();
1239 if (target_stopped_by_watchpoint ())
1241 /* Always interested in watchpoints. */
1243 else if (record_check_stopped_by_breakpoint (aspace, tmp_pc,
1244 stop_reason_p))
1246 /* There is a breakpoint here. Let the core
1247 handle it. */
1249 else
1251 /* This is a single-step trap. Record the
1252 insn and issue another step.
1253 FIXME: this part can be a random SIGTRAP too.
1254 But GDB cannot handle it. */
1255 int step = 1;
1257 if (!record_full_message_wrapper_safe (regcache,
1258 GDB_SIGNAL_0))
1260 status->set_stopped (GDB_SIGNAL_0);
1261 break;
1264 process_stratum_target *proc_target
1265 = current_inferior ()->process_target ();
1267 if (gdbarch_software_single_step_p (gdbarch))
1269 /* Try to insert the software single step breakpoint.
1270 If insert success, set step to 0. */
1271 set_executing (proc_target, inferior_ptid, false);
1272 SCOPE_EXIT
1274 set_executing (proc_target, inferior_ptid, true);
1277 reinit_frame_cache ();
1278 step = !insert_single_step_breakpoints (gdbarch);
1281 if (record_debug)
1282 gdb_printf (gdb_stdlog,
1283 "Process record: record_full_wait "
1284 "issuing one more step in the "
1285 "target beneath\n");
1286 ops->beneath ()->resume (ptid, step, GDB_SIGNAL_0);
1287 proc_target->commit_resumed_state = true;
1288 proc_target->commit_resumed ();
1289 proc_target->commit_resumed_state = false;
1290 continue;
1294 /* The inferior is broken by a breakpoint or a signal. */
1295 break;
1298 return ret;
1301 else
1303 switch_to_thread (current_inferior ()->process_target (),
1304 record_full_resume_ptid);
1305 regcache *regcache = get_thread_regcache (inferior_thread ());
1306 struct gdbarch *gdbarch = regcache->arch ();
1307 const address_space *aspace = current_inferior ()->aspace.get ();
1308 int continue_flag = 1;
1309 int first_record_full_end = 1;
1313 CORE_ADDR tmp_pc;
1315 record_full_stop_reason = TARGET_STOPPED_BY_NO_REASON;
1316 status->set_stopped (GDB_SIGNAL_0);
1318 /* Check breakpoint when forward execute. */
1319 if (execution_direction == EXEC_FORWARD)
1321 tmp_pc = regcache_read_pc (regcache);
1322 if (record_check_stopped_by_breakpoint (aspace, tmp_pc,
1323 &record_full_stop_reason))
1325 if (record_debug)
1326 gdb_printf (gdb_stdlog,
1327 "Process record: break at %s.\n",
1328 paddress (gdbarch, tmp_pc));
1329 goto replay_out;
1333 /* If GDB is in terminal_inferior mode, it will not get the
1334 signal. And in GDB replay mode, GDB doesn't need to be
1335 in terminal_inferior mode, because inferior will not
1336 executed. Then set it to terminal_ours to make GDB get
1337 the signal. */
1338 target_terminal::ours ();
1340 /* In EXEC_FORWARD mode, record_full_list points to the tail of prev
1341 instruction. */
1342 if (execution_direction == EXEC_FORWARD && record_full_list->next)
1343 record_full_list = record_full_list->next;
1345 /* Loop over the record_full_list, looking for the next place to
1346 stop. */
1349 /* Check for beginning and end of log. */
1350 if (execution_direction == EXEC_REVERSE
1351 && record_full_list == &record_full_first)
1353 /* Hit beginning of record log in reverse. */
1354 status->set_no_history ();
1355 break;
1357 if (execution_direction != EXEC_REVERSE
1358 && !record_full_list->next)
1360 /* Hit end of record log going forward. */
1361 status->set_no_history ();
1362 break;
1365 record_full_exec_insn (regcache, gdbarch, record_full_list);
1367 if (record_full_list->type == record_full_end)
1369 if (record_debug > 1)
1370 gdb_printf
1371 (gdb_stdlog,
1372 "Process record: record_full_end %s to "
1373 "inferior.\n",
1374 host_address_to_string (record_full_list));
1376 if (first_record_full_end
1377 && execution_direction == EXEC_REVERSE)
1379 /* When reverse execute, the first
1380 record_full_end is the part of current
1381 instruction. */
1382 first_record_full_end = 0;
1384 else
1386 /* In EXEC_REVERSE mode, this is the
1387 record_full_end of prev instruction. In
1388 EXEC_FORWARD mode, this is the
1389 record_full_end of current instruction. */
1390 /* step */
1391 if (record_full_resume_step)
1393 if (record_debug > 1)
1394 gdb_printf (gdb_stdlog,
1395 "Process record: step.\n");
1396 continue_flag = 0;
1399 /* check breakpoint */
1400 tmp_pc = regcache_read_pc (regcache);
1401 if (record_check_stopped_by_breakpoint
1402 (aspace, tmp_pc, &record_full_stop_reason))
1404 if (record_debug)
1405 gdb_printf (gdb_stdlog,
1406 "Process record: break "
1407 "at %s.\n",
1408 paddress (gdbarch, tmp_pc));
1410 continue_flag = 0;
1413 if (record_full_stop_reason
1414 == TARGET_STOPPED_BY_WATCHPOINT)
1416 if (record_debug)
1417 gdb_printf (gdb_stdlog,
1418 "Process record: hit hw "
1419 "watchpoint.\n");
1420 continue_flag = 0;
1422 /* Check target signal */
1423 if (record_full_list->u.end.sigval != GDB_SIGNAL_0)
1424 /* FIXME: better way to check */
1425 continue_flag = 0;
1429 if (continue_flag)
1431 if (execution_direction == EXEC_REVERSE)
1433 if (record_full_list->prev)
1434 record_full_list = record_full_list->prev;
1436 else
1438 if (record_full_list->next)
1439 record_full_list = record_full_list->next;
1443 while (continue_flag);
1445 replay_out:
1446 if (status->kind () == TARGET_WAITKIND_STOPPED)
1448 if (record_full_get_sig)
1449 status->set_stopped (GDB_SIGNAL_INT);
1450 else if (record_full_list->u.end.sigval != GDB_SIGNAL_0)
1451 /* FIXME: better way to check */
1452 status->set_stopped (record_full_list->u.end.sigval);
1453 else
1454 status->set_stopped (GDB_SIGNAL_TRAP);
1457 catch (const gdb_exception &ex)
1459 if (execution_direction == EXEC_REVERSE)
1461 if (record_full_list->next)
1462 record_full_list = record_full_list->next;
1464 else
1465 record_full_list = record_full_list->prev;
1467 throw;
1471 signal (SIGINT, handle_sigint);
1473 return inferior_ptid;
1476 ptid_t
1477 record_full_base_target::wait (ptid_t ptid, struct target_waitstatus *status,
1478 target_wait_flags options)
1480 ptid_t return_ptid;
1482 clear_async_event_handler (record_full_async_inferior_event_token);
1484 return_ptid = record_full_wait_1 (this, ptid, status, options);
1485 if (status->kind () != TARGET_WAITKIND_IGNORE)
1487 /* We're reporting a stop. Make sure any spurious
1488 target_wait(WNOHANG) doesn't advance the target until the
1489 core wants us resumed again. */
1490 record_full_resumed = 0;
1492 return return_ptid;
1495 bool
1496 record_full_base_target::stopped_by_watchpoint ()
1498 if (RECORD_FULL_IS_REPLAY)
1499 return record_full_stop_reason == TARGET_STOPPED_BY_WATCHPOINT;
1500 else
1501 return beneath ()->stopped_by_watchpoint ();
1504 bool
1505 record_full_base_target::stopped_data_address (CORE_ADDR *addr_p)
1507 if (RECORD_FULL_IS_REPLAY)
1508 return false;
1509 else
1510 return this->beneath ()->stopped_data_address (addr_p);
1513 /* The stopped_by_sw_breakpoint method of target record-full. */
1515 bool
1516 record_full_base_target::stopped_by_sw_breakpoint ()
1518 return record_full_stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT;
1521 /* The supports_stopped_by_sw_breakpoint method of target
1522 record-full. */
1524 bool
1525 record_full_base_target::supports_stopped_by_sw_breakpoint ()
1527 return true;
1530 /* The stopped_by_hw_breakpoint method of target record-full. */
1532 bool
1533 record_full_base_target::stopped_by_hw_breakpoint ()
1535 return record_full_stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT;
1538 /* The supports_stopped_by_sw_breakpoint method of target
1539 record-full. */
1541 bool
1542 record_full_base_target::supports_stopped_by_hw_breakpoint ()
1544 return true;
1547 /* Record registers change (by user or by GDB) to list as an instruction. */
1549 static void
1550 record_full_registers_change (struct regcache *regcache, int regnum)
1552 /* Check record_full_insn_num. */
1553 record_full_check_insn_num ();
1555 record_full_arch_list_head = NULL;
1556 record_full_arch_list_tail = NULL;
1558 if (regnum < 0)
1560 int i;
1562 for (i = 0; i < gdbarch_num_regs (regcache->arch ()); i++)
1564 if (record_full_arch_list_add_reg (regcache, i))
1566 record_full_list_release (record_full_arch_list_tail);
1567 error (_("Process record: failed to record execution log."));
1571 else
1573 if (record_full_arch_list_add_reg (regcache, regnum))
1575 record_full_list_release (record_full_arch_list_tail);
1576 error (_("Process record: failed to record execution log."));
1579 if (record_full_arch_list_add_end ())
1581 record_full_list_release (record_full_arch_list_tail);
1582 error (_("Process record: failed to record execution log."));
1584 record_full_list->next = record_full_arch_list_head;
1585 record_full_arch_list_head->prev = record_full_list;
1586 record_full_list = record_full_arch_list_tail;
1588 if (record_full_insn_num == record_full_insn_max_num)
1589 record_full_list_release_first ();
1590 else
1591 record_full_insn_num++;
1594 /* "store_registers" method for process record target. */
1596 void
1597 record_full_target::store_registers (struct regcache *regcache, int regno)
1599 if (!record_full_gdb_operation_disable)
1601 if (RECORD_FULL_IS_REPLAY)
1603 int n;
1605 /* Let user choose if he wants to write register or not. */
1606 if (regno < 0)
1608 query (_("Because GDB is in replay mode, changing the "
1609 "value of a register will make the execution "
1610 "log unusable from this point onward. "
1611 "Change all registers?"));
1612 else
1614 query (_("Because GDB is in replay mode, changing the value "
1615 "of a register will make the execution log unusable "
1616 "from this point onward. Change register %s?"),
1617 gdbarch_register_name (regcache->arch (),
1618 regno));
1620 if (!n)
1622 /* Invalidate the value of regcache that was set in function
1623 "regcache_raw_write". */
1624 if (regno < 0)
1626 int i;
1628 for (i = 0;
1629 i < gdbarch_num_regs (regcache->arch ());
1630 i++)
1631 regcache->invalidate (i);
1633 else
1634 regcache->invalidate (regno);
1636 error (_("Process record canceled the operation."));
1639 /* Destroy the record from here forward. */
1640 record_full_list_release_following (record_full_list);
1643 record_full_registers_change (regcache, regno);
1645 this->beneath ()->store_registers (regcache, regno);
1648 /* "xfer_partial" method. Behavior is conditional on
1649 RECORD_FULL_IS_REPLAY.
1650 In replay mode, we cannot write memory unles we are willing to
1651 invalidate the record/replay log from this point forward. */
1653 enum target_xfer_status
1654 record_full_target::xfer_partial (enum target_object object,
1655 const char *annex, gdb_byte *readbuf,
1656 const gdb_byte *writebuf, ULONGEST offset,
1657 ULONGEST len, ULONGEST *xfered_len)
1659 if (!record_full_gdb_operation_disable
1660 && (object == TARGET_OBJECT_MEMORY
1661 || object == TARGET_OBJECT_RAW_MEMORY) && writebuf)
1663 if (RECORD_FULL_IS_REPLAY)
1665 /* Let user choose if he wants to write memory or not. */
1666 if (!query (_("Because GDB is in replay mode, writing to memory "
1667 "will make the execution log unusable from this "
1668 "point onward. Write memory at address %s?"),
1669 paddress (current_inferior ()->arch (), offset)))
1670 error (_("Process record canceled the operation."));
1672 /* Destroy the record from here forward. */
1673 record_full_list_release_following (record_full_list);
1676 /* Check record_full_insn_num */
1677 record_full_check_insn_num ();
1679 /* Record registers change to list as an instruction. */
1680 record_full_arch_list_head = NULL;
1681 record_full_arch_list_tail = NULL;
1682 if (record_full_arch_list_add_mem (offset, len))
1684 record_full_list_release (record_full_arch_list_tail);
1685 if (record_debug)
1686 gdb_printf (gdb_stdlog,
1687 "Process record: failed to record "
1688 "execution log.");
1689 return TARGET_XFER_E_IO;
1691 if (record_full_arch_list_add_end ())
1693 record_full_list_release (record_full_arch_list_tail);
1694 if (record_debug)
1695 gdb_printf (gdb_stdlog,
1696 "Process record: failed to record "
1697 "execution log.");
1698 return TARGET_XFER_E_IO;
1700 record_full_list->next = record_full_arch_list_head;
1701 record_full_arch_list_head->prev = record_full_list;
1702 record_full_list = record_full_arch_list_tail;
1704 if (record_full_insn_num == record_full_insn_max_num)
1705 record_full_list_release_first ();
1706 else
1707 record_full_insn_num++;
1710 return this->beneath ()->xfer_partial (object, annex, readbuf, writebuf,
1711 offset, len, xfered_len);
1714 /* This structure represents a breakpoint inserted while the record
1715 target is active. We use this to know when to install/remove
1716 breakpoints in/from the target beneath. For example, a breakpoint
1717 may be inserted while recording, but removed when not replaying nor
1718 recording. In that case, the breakpoint had not been inserted on
1719 the target beneath, so we should not try to remove it there. */
1721 struct record_full_breakpoint
1723 record_full_breakpoint (struct address_space *address_space_,
1724 CORE_ADDR addr_,
1725 bool in_target_beneath_)
1726 : address_space (address_space_),
1727 addr (addr_),
1728 in_target_beneath (in_target_beneath_)
1732 /* The address and address space the breakpoint was set at. */
1733 struct address_space *address_space;
1734 CORE_ADDR addr;
1736 /* True when the breakpoint has been also installed in the target
1737 beneath. This will be false for breakpoints set during replay or
1738 when recording. */
1739 bool in_target_beneath;
1742 /* The list of breakpoints inserted while the record target is
1743 active. */
1744 static std::vector<record_full_breakpoint> record_full_breakpoints;
1746 /* Sync existing breakpoints to record_full_breakpoints. */
1748 static void
1749 record_full_init_record_breakpoints (void)
1751 record_full_breakpoints.clear ();
1753 for (bp_location *loc : all_bp_locations ())
1755 if (loc->loc_type != bp_loc_software_breakpoint)
1756 continue;
1758 if (loc->inserted)
1759 record_full_breakpoints.emplace_back
1760 (loc->target_info.placed_address_space,
1761 loc->target_info.placed_address, 1);
1765 /* Behavior is conditional on RECORD_FULL_IS_REPLAY. We will not actually
1766 insert or remove breakpoints in the real target when replaying, nor
1767 when recording. */
1770 record_full_target::insert_breakpoint (struct gdbarch *gdbarch,
1771 struct bp_target_info *bp_tgt)
1773 bool in_target_beneath = false;
1775 if (!RECORD_FULL_IS_REPLAY)
1777 /* When recording, we currently always single-step, so we don't
1778 really need to install regular breakpoints in the inferior.
1779 However, we do have to insert software single-step
1780 breakpoints, in case the target can't hardware step. To keep
1781 things simple, we always insert. */
1783 scoped_restore restore_operation_disable
1784 = record_full_gdb_operation_disable_set ();
1786 int ret = this->beneath ()->insert_breakpoint (gdbarch, bp_tgt);
1787 if (ret != 0)
1788 return ret;
1790 in_target_beneath = true;
1793 /* Use the existing entries if found in order to avoid duplication
1794 in record_full_breakpoints. */
1796 for (const record_full_breakpoint &bp : record_full_breakpoints)
1798 if (bp.addr == bp_tgt->placed_address
1799 && bp.address_space == bp_tgt->placed_address_space)
1801 gdb_assert (bp.in_target_beneath == in_target_beneath);
1802 return 0;
1806 record_full_breakpoints.emplace_back (bp_tgt->placed_address_space,
1807 bp_tgt->placed_address,
1808 in_target_beneath);
1809 return 0;
1812 /* "remove_breakpoint" method for process record target. */
1815 record_full_target::remove_breakpoint (struct gdbarch *gdbarch,
1816 struct bp_target_info *bp_tgt,
1817 enum remove_bp_reason reason)
1819 for (auto iter = record_full_breakpoints.begin ();
1820 iter != record_full_breakpoints.end ();
1821 ++iter)
1823 struct record_full_breakpoint &bp = *iter;
1825 if (bp.addr == bp_tgt->placed_address
1826 && bp.address_space == bp_tgt->placed_address_space)
1828 if (bp.in_target_beneath)
1830 scoped_restore restore_operation_disable
1831 = record_full_gdb_operation_disable_set ();
1833 int ret = this->beneath ()->remove_breakpoint (gdbarch, bp_tgt,
1834 reason);
1835 if (ret != 0)
1836 return ret;
1839 if (reason == REMOVE_BREAKPOINT)
1840 unordered_remove (record_full_breakpoints, iter);
1841 return 0;
1845 gdb_assert_not_reached ("removing unknown breakpoint");
1848 /* "can_execute_reverse" method for process record target. */
1850 bool
1851 record_full_base_target::can_execute_reverse ()
1853 return true;
1856 /* "get_bookmark" method for process record and prec over core. */
1858 gdb_byte *
1859 record_full_base_target::get_bookmark (const char *args, int from_tty)
1861 char *ret = NULL;
1863 /* Return stringified form of instruction count. */
1864 if (record_full_list && record_full_list->type == record_full_end)
1865 ret = xstrdup (pulongest (record_full_list->u.end.insn_num));
1867 if (record_debug)
1869 if (ret)
1870 gdb_printf (gdb_stdlog,
1871 "record_full_get_bookmark returns %s\n", ret);
1872 else
1873 gdb_printf (gdb_stdlog,
1874 "record_full_get_bookmark returns NULL\n");
1876 return (gdb_byte *) ret;
1879 /* "goto_bookmark" method for process record and prec over core. */
1881 void
1882 record_full_base_target::goto_bookmark (const gdb_byte *raw_bookmark,
1883 int from_tty)
1885 const char *bookmark = (const char *) raw_bookmark;
1887 if (record_debug)
1888 gdb_printf (gdb_stdlog,
1889 "record_full_goto_bookmark receives %s\n", bookmark);
1891 std::string name_holder;
1892 if (bookmark[0] == '\'' || bookmark[0] == '\"')
1894 if (bookmark[strlen (bookmark) - 1] != bookmark[0])
1895 error (_("Unbalanced quotes: %s"), bookmark);
1897 name_holder = std::string (bookmark + 1, strlen (bookmark) - 2);
1898 bookmark = name_holder.c_str ();
1901 record_goto (bookmark);
1904 enum exec_direction_kind
1905 record_full_base_target::execution_direction ()
1907 return record_full_execution_dir;
1910 /* The record_method method of target record-full. */
1912 enum record_method
1913 record_full_base_target::record_method (ptid_t ptid)
1915 return RECORD_METHOD_FULL;
1918 void
1919 record_full_base_target::info_record ()
1921 struct record_full_entry *p;
1923 if (RECORD_FULL_IS_REPLAY)
1924 gdb_printf (_("Replay mode:\n"));
1925 else
1926 gdb_printf (_("Record mode:\n"));
1928 /* Find entry for first actual instruction in the log. */
1929 for (p = record_full_first.next;
1930 p != NULL && p->type != record_full_end;
1931 p = p->next)
1934 /* Do we have a log at all? */
1935 if (p != NULL && p->type == record_full_end)
1937 /* Display instruction number for first instruction in the log. */
1938 gdb_printf (_("Lowest recorded instruction number is %s.\n"),
1939 pulongest (p->u.end.insn_num));
1941 /* If in replay mode, display where we are in the log. */
1942 if (RECORD_FULL_IS_REPLAY)
1943 gdb_printf (_("Current instruction number is %s.\n"),
1944 pulongest (record_full_list->u.end.insn_num));
1946 /* Display instruction number for last instruction in the log. */
1947 gdb_printf (_("Highest recorded instruction number is %s.\n"),
1948 pulongest (record_full_insn_count));
1950 /* Display log count. */
1951 gdb_printf (_("Log contains %u instructions.\n"),
1952 record_full_insn_num);
1954 else
1955 gdb_printf (_("No instructions have been logged.\n"));
1957 /* Display max log size. */
1958 gdb_printf (_("Max logged instructions is %u.\n"),
1959 record_full_insn_max_num);
1962 bool
1963 record_full_base_target::supports_delete_record ()
1965 return true;
1968 /* The "delete_record" target method. */
1970 void
1971 record_full_base_target::delete_record ()
1973 record_full_list_release_following (record_full_list);
1976 /* The "record_is_replaying" target method. */
1978 bool
1979 record_full_base_target::record_is_replaying (ptid_t ptid)
1981 return RECORD_FULL_IS_REPLAY;
1984 /* The "record_will_replay" target method. */
1986 bool
1987 record_full_base_target::record_will_replay (ptid_t ptid, int dir)
1989 /* We can currently only record when executing forwards. Should we be able
1990 to record when executing backwards on targets that support reverse
1991 execution, this needs to be changed. */
1993 return RECORD_FULL_IS_REPLAY || dir == EXEC_REVERSE;
1996 /* Go to a specific entry. */
1998 static void
1999 record_full_goto_entry (struct record_full_entry *p)
2001 if (p == NULL)
2002 error (_("Target insn not found."));
2003 else if (p == record_full_list)
2004 error (_("Already at target insn."));
2005 else if (p->u.end.insn_num > record_full_list->u.end.insn_num)
2007 gdb_printf (_("Go forward to insn number %s\n"),
2008 pulongest (p->u.end.insn_num));
2009 record_full_goto_insn (p, EXEC_FORWARD);
2011 else
2013 gdb_printf (_("Go backward to insn number %s\n"),
2014 pulongest (p->u.end.insn_num));
2015 record_full_goto_insn (p, EXEC_REVERSE);
2018 registers_changed ();
2019 reinit_frame_cache ();
2021 thread_info *thr = inferior_thread ();
2022 thr->set_stop_pc (regcache_read_pc (get_thread_regcache (thr)));
2023 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
2026 /* The "goto_record_begin" target method. */
2028 void
2029 record_full_base_target::goto_record_begin ()
2031 struct record_full_entry *p = NULL;
2033 for (p = &record_full_first; p != NULL; p = p->next)
2034 if (p->type == record_full_end)
2035 break;
2037 record_full_goto_entry (p);
2040 /* The "goto_record_end" target method. */
2042 void
2043 record_full_base_target::goto_record_end ()
2045 struct record_full_entry *p = NULL;
2047 for (p = record_full_list; p->next != NULL; p = p->next)
2049 for (; p!= NULL; p = p->prev)
2050 if (p->type == record_full_end)
2051 break;
2053 record_full_goto_entry (p);
2056 /* The "goto_record" target method. */
2058 void
2059 record_full_base_target::goto_record (ULONGEST target_insn)
2061 struct record_full_entry *p = NULL;
2063 for (p = &record_full_first; p != NULL; p = p->next)
2064 if (p->type == record_full_end && p->u.end.insn_num == target_insn)
2065 break;
2067 record_full_goto_entry (p);
2070 /* The "record_stop_replaying" target method. */
2072 void
2073 record_full_base_target::record_stop_replaying ()
2075 goto_record_end ();
2078 /* "resume" method for prec over corefile. */
2080 void
2081 record_full_core_target::resume (ptid_t ptid, int step,
2082 enum gdb_signal signal)
2084 record_full_resume_step = step;
2085 record_full_resume_ptid = ptid;
2086 record_full_resumed = 1;
2087 record_full_execution_dir = ::execution_direction;
2090 /* "kill" method for prec over corefile. */
2092 void
2093 record_full_core_target::kill ()
2095 if (record_debug)
2096 gdb_printf (gdb_stdlog, "Process record: record_full_core_kill\n");
2098 current_inferior ()->unpush_target (this);
2101 /* "fetch_registers" method for prec over corefile. */
2103 void
2104 record_full_core_target::fetch_registers (struct regcache *regcache,
2105 int regno)
2107 if (regno < 0)
2109 int num = gdbarch_num_regs (regcache->arch ());
2110 int i;
2112 for (i = 0; i < num; i ++)
2113 regcache->raw_supply (i, *record_full_core_regbuf);
2115 else
2116 regcache->raw_supply (regno, *record_full_core_regbuf);
2119 /* "prepare_to_store" method for prec over corefile. */
2121 void
2122 record_full_core_target::prepare_to_store (struct regcache *regcache)
2126 /* "store_registers" method for prec over corefile. */
2128 void
2129 record_full_core_target::store_registers (struct regcache *regcache,
2130 int regno)
2132 if (record_full_gdb_operation_disable)
2133 record_full_core_regbuf->raw_supply (regno, *regcache);
2134 else
2135 error (_("You can't do that without a process to debug."));
2138 /* "xfer_partial" method for prec over corefile. */
2140 enum target_xfer_status
2141 record_full_core_target::xfer_partial (enum target_object object,
2142 const char *annex, gdb_byte *readbuf,
2143 const gdb_byte *writebuf, ULONGEST offset,
2144 ULONGEST len, ULONGEST *xfered_len)
2146 if (object == TARGET_OBJECT_MEMORY)
2148 if (record_full_gdb_operation_disable || !writebuf)
2150 for (target_section &p : record_full_core_sections)
2152 if (offset >= p.addr)
2154 struct record_full_core_buf_entry *entry;
2155 ULONGEST sec_offset;
2157 if (offset >= p.endaddr)
2158 continue;
2160 if (offset + len > p.endaddr)
2161 len = p.endaddr - offset;
2163 sec_offset = offset - p.addr;
2165 /* Read readbuf or write writebuf p, offset, len. */
2166 /* Check flags. */
2167 if (p.the_bfd_section->flags & SEC_CONSTRUCTOR
2168 || (p.the_bfd_section->flags & SEC_HAS_CONTENTS) == 0)
2170 if (readbuf)
2171 memset (readbuf, 0, len);
2173 *xfered_len = len;
2174 return TARGET_XFER_OK;
2176 /* Get record_full_core_buf_entry. */
2177 for (entry = record_full_core_buf_list; entry;
2178 entry = entry->prev)
2179 if (entry->p == &p)
2180 break;
2181 if (writebuf)
2183 if (!entry)
2185 /* Add a new entry. */
2186 entry = XNEW (struct record_full_core_buf_entry);
2187 entry->p = &p;
2188 if (!bfd_malloc_and_get_section
2189 (p.the_bfd_section->owner,
2190 p.the_bfd_section,
2191 &entry->buf))
2193 xfree (entry);
2194 return TARGET_XFER_EOF;
2196 entry->prev = record_full_core_buf_list;
2197 record_full_core_buf_list = entry;
2200 memcpy (entry->buf + sec_offset, writebuf,
2201 (size_t) len);
2203 else
2205 if (!entry)
2206 return this->beneath ()->xfer_partial (object, annex,
2207 readbuf, writebuf,
2208 offset, len,
2209 xfered_len);
2211 memcpy (readbuf, entry->buf + sec_offset,
2212 (size_t) len);
2215 *xfered_len = len;
2216 return TARGET_XFER_OK;
2220 return TARGET_XFER_E_IO;
2222 else
2223 error (_("You can't do that without a process to debug."));
2226 return this->beneath ()->xfer_partial (object, annex,
2227 readbuf, writebuf, offset, len,
2228 xfered_len);
2231 /* "insert_breakpoint" method for prec over corefile. */
2234 record_full_core_target::insert_breakpoint (struct gdbarch *gdbarch,
2235 struct bp_target_info *bp_tgt)
2237 return 0;
2240 /* "remove_breakpoint" method for prec over corefile. */
2243 record_full_core_target::remove_breakpoint (struct gdbarch *gdbarch,
2244 struct bp_target_info *bp_tgt,
2245 enum remove_bp_reason reason)
2247 return 0;
2250 /* "has_execution" method for prec over corefile. */
2252 bool
2253 record_full_core_target::has_execution (inferior *inf)
2255 return true;
2258 /* Record log save-file format
2259 Version 1 (never released)
2261 Header:
2262 4 bytes: magic number htonl(0x20090829).
2263 NOTE: be sure to change whenever this file format changes!
2265 Records:
2266 record_full_end:
2267 1 byte: record type (record_full_end, see enum record_full_type).
2268 record_full_reg:
2269 1 byte: record type (record_full_reg, see enum record_full_type).
2270 8 bytes: register id (network byte order).
2271 MAX_REGISTER_SIZE bytes: register value.
2272 record_full_mem:
2273 1 byte: record type (record_full_mem, see enum record_full_type).
2274 8 bytes: memory length (network byte order).
2275 8 bytes: memory address (network byte order).
2276 n bytes: memory value (n == memory length).
2278 Version 2
2279 4 bytes: magic number netorder32(0x20091016).
2280 NOTE: be sure to change whenever this file format changes!
2282 Records:
2283 record_full_end:
2284 1 byte: record type (record_full_end, see enum record_full_type).
2285 4 bytes: signal
2286 4 bytes: instruction count
2287 record_full_reg:
2288 1 byte: record type (record_full_reg, see enum record_full_type).
2289 4 bytes: register id (network byte order).
2290 n bytes: register value (n == actual register size).
2291 (eg. 4 bytes for x86 general registers).
2292 record_full_mem:
2293 1 byte: record type (record_full_mem, see enum record_full_type).
2294 4 bytes: memory length (network byte order).
2295 8 bytes: memory address (network byte order).
2296 n bytes: memory value (n == memory length).
2300 /* bfdcore_read -- read bytes from a core file section. */
2302 static inline void
2303 bfdcore_read (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2305 int ret = bfd_get_section_contents (obfd, osec, buf, *offset, len);
2307 if (ret)
2308 *offset += len;
2309 else
2310 error (_("Failed to read %d bytes from core file %s ('%s')."),
2311 len, bfd_get_filename (obfd),
2312 bfd_errmsg (bfd_get_error ()));
2315 static inline uint64_t
2316 netorder64 (uint64_t input)
2318 uint64_t ret;
2320 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2321 BFD_ENDIAN_BIG, input);
2322 return ret;
2325 static inline uint32_t
2326 netorder32 (uint32_t input)
2328 uint32_t ret;
2330 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2331 BFD_ENDIAN_BIG, input);
2332 return ret;
2335 /* Restore the execution log from a core_bfd file. */
2336 static void
2337 record_full_restore (void)
2339 uint32_t magic;
2340 struct record_full_entry *rec;
2341 asection *osec;
2342 uint32_t osec_size;
2343 int bfd_offset = 0;
2345 /* We restore the execution log from the open core bfd,
2346 if there is one. */
2347 if (current_program_space->core_bfd () == nullptr)
2348 return;
2350 /* "record_full_restore" can only be called when record list is empty. */
2351 gdb_assert (record_full_first.next == NULL);
2353 if (record_debug)
2354 gdb_printf (gdb_stdlog, "Restoring recording from core file.\n");
2356 /* Now need to find our special note section. */
2357 osec = bfd_get_section_by_name (current_program_space->core_bfd (), "null0");
2358 if (record_debug)
2359 gdb_printf (gdb_stdlog, "Find precord section %s.\n",
2360 osec ? "succeeded" : "failed");
2361 if (osec == NULL)
2362 return;
2363 osec_size = bfd_section_size (osec);
2364 if (record_debug)
2365 gdb_printf (gdb_stdlog, "%s", bfd_section_name (osec));
2367 /* Check the magic code. */
2368 bfdcore_read (current_program_space->core_bfd (), osec, &magic,
2369 sizeof (magic), &bfd_offset);
2370 if (magic != RECORD_FULL_FILE_MAGIC)
2371 error (_("Version mis-match or file format error in core file %s."),
2372 bfd_get_filename (current_program_space->core_bfd ()));
2373 if (record_debug)
2374 gdb_printf (gdb_stdlog,
2375 " Reading 4-byte magic cookie "
2376 "RECORD_FULL_FILE_MAGIC (0x%s)\n",
2377 phex_nz (netorder32 (magic), 4));
2379 /* Restore the entries in recfd into record_full_arch_list_head and
2380 record_full_arch_list_tail. */
2381 record_full_arch_list_head = NULL;
2382 record_full_arch_list_tail = NULL;
2383 record_full_insn_num = 0;
2387 regcache *regcache = get_thread_regcache (inferior_thread ());
2389 while (1)
2391 uint8_t rectype;
2392 uint32_t regnum, len, signal, count;
2393 uint64_t addr;
2395 /* We are finished when offset reaches osec_size. */
2396 if (bfd_offset >= osec_size)
2397 break;
2398 bfdcore_read (current_program_space->core_bfd (), osec, &rectype,
2399 sizeof (rectype), &bfd_offset);
2401 switch (rectype)
2403 case record_full_reg: /* reg */
2404 /* Get register number to regnum. */
2405 bfdcore_read (current_program_space->core_bfd (), osec, &regnum,
2406 sizeof (regnum), &bfd_offset);
2407 regnum = netorder32 (regnum);
2409 rec = record_full_reg_alloc (regcache, regnum);
2411 /* Get val. */
2412 bfdcore_read (current_program_space->core_bfd (), osec,
2413 record_full_get_loc (rec), rec->u.reg.len,
2414 &bfd_offset);
2416 if (record_debug)
2417 gdb_printf (gdb_stdlog,
2418 " Reading register %d (1 "
2419 "plus %lu plus %d bytes)\n",
2420 rec->u.reg.num,
2421 (unsigned long) sizeof (regnum),
2422 rec->u.reg.len);
2423 break;
2425 case record_full_mem: /* mem */
2426 /* Get len. */
2427 bfdcore_read (current_program_space->core_bfd (), osec, &len,
2428 sizeof (len), &bfd_offset);
2429 len = netorder32 (len);
2431 /* Get addr. */
2432 bfdcore_read (current_program_space->core_bfd (), osec, &addr,
2433 sizeof (addr), &bfd_offset);
2434 addr = netorder64 (addr);
2436 rec = record_full_mem_alloc (addr, len);
2438 /* Get val. */
2439 bfdcore_read (current_program_space->core_bfd (), osec,
2440 record_full_get_loc (rec), rec->u.mem.len,
2441 &bfd_offset);
2443 if (record_debug)
2444 gdb_printf (gdb_stdlog,
2445 " Reading memory %s (1 plus "
2446 "%lu plus %lu plus %d bytes)\n",
2447 paddress (get_current_arch (),
2448 rec->u.mem.addr),
2449 (unsigned long) sizeof (addr),
2450 (unsigned long) sizeof (len),
2451 rec->u.mem.len);
2452 break;
2454 case record_full_end: /* end */
2455 rec = record_full_end_alloc ();
2456 record_full_insn_num ++;
2458 /* Get signal value. */
2459 bfdcore_read (current_program_space->core_bfd (), osec, &signal,
2460 sizeof (signal), &bfd_offset);
2461 signal = netorder32 (signal);
2462 rec->u.end.sigval = (enum gdb_signal) signal;
2464 /* Get insn count. */
2465 bfdcore_read (current_program_space->core_bfd (), osec, &count,
2466 sizeof (count), &bfd_offset);
2467 count = netorder32 (count);
2468 rec->u.end.insn_num = count;
2469 record_full_insn_count = count + 1;
2470 if (record_debug)
2471 gdb_printf (gdb_stdlog,
2472 " Reading record_full_end (1 + "
2473 "%lu + %lu bytes), offset == %s\n",
2474 (unsigned long) sizeof (signal),
2475 (unsigned long) sizeof (count),
2476 paddress (get_current_arch (),
2477 bfd_offset));
2478 break;
2480 default:
2481 error (_("Bad entry type in core file %s."),
2482 bfd_get_filename (current_program_space->core_bfd ()));
2483 break;
2486 /* Add rec to record arch list. */
2487 record_full_arch_list_add (rec);
2490 catch (const gdb_exception &ex)
2492 record_full_list_release (record_full_arch_list_tail);
2493 throw;
2496 /* Add record_full_arch_list_head to the end of record list. */
2497 record_full_first.next = record_full_arch_list_head;
2498 record_full_arch_list_head->prev = &record_full_first;
2499 record_full_arch_list_tail->next = NULL;
2500 record_full_list = &record_full_first;
2502 /* Update record_full_insn_max_num. */
2503 if (record_full_insn_num > record_full_insn_max_num)
2505 record_full_insn_max_num = record_full_insn_num;
2506 warning (_("Auto increase record/replay buffer limit to %u."),
2507 record_full_insn_max_num);
2510 /* Succeeded. */
2511 gdb_printf (_("Restored records from core file %s.\n"),
2512 bfd_get_filename (current_program_space->core_bfd ()));
2514 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
2517 /* bfdcore_write -- write bytes into a core file section. */
2519 static inline void
2520 bfdcore_write (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2522 int ret = bfd_set_section_contents (obfd, osec, buf, *offset, len);
2524 if (ret)
2525 *offset += len;
2526 else
2527 error (_("Failed to write %d bytes to core file %s ('%s')."),
2528 len, bfd_get_filename (obfd),
2529 bfd_errmsg (bfd_get_error ()));
2532 /* Restore the execution log from a file. We use a modified elf
2533 corefile format, with an extra section for our data. */
2535 static void
2536 cmd_record_full_restore (const char *args, int from_tty)
2538 core_file_command (args, from_tty);
2539 record_full_open (nullptr, from_tty);
2542 /* Save the execution log to a file. We use a modified elf corefile
2543 format, with an extra section for our data. */
2545 void
2546 record_full_base_target::save_record (const char *recfilename)
2548 struct record_full_entry *cur_record_full_list;
2549 uint32_t magic;
2550 struct gdbarch *gdbarch;
2551 int save_size = 0;
2552 asection *osec = NULL;
2553 int bfd_offset = 0;
2555 /* Open the save file. */
2556 if (record_debug)
2557 gdb_printf (gdb_stdlog, "Saving execution log to core file '%s'\n",
2558 recfilename);
2560 /* Open the output file. */
2561 gdb_bfd_ref_ptr obfd (create_gcore_bfd (recfilename));
2563 /* Arrange to remove the output file on failure. */
2564 gdb::unlinker unlink_file (recfilename);
2566 /* Save the current record entry to "cur_record_full_list". */
2567 cur_record_full_list = record_full_list;
2569 /* Get the values of regcache and gdbarch. */
2570 regcache *regcache = get_thread_regcache (inferior_thread ());
2571 gdbarch = regcache->arch ();
2573 /* Disable the GDB operation record. */
2574 scoped_restore restore_operation_disable
2575 = record_full_gdb_operation_disable_set ();
2577 /* Reverse execute to the begin of record list. */
2578 while (1)
2580 /* Check for beginning and end of log. */
2581 if (record_full_list == &record_full_first)
2582 break;
2584 record_full_exec_insn (regcache, gdbarch, record_full_list);
2586 if (record_full_list->prev)
2587 record_full_list = record_full_list->prev;
2590 /* Compute the size needed for the extra bfd section. */
2591 save_size = 4; /* magic cookie */
2592 for (record_full_list = record_full_first.next; record_full_list;
2593 record_full_list = record_full_list->next)
2594 switch (record_full_list->type)
2596 case record_full_end:
2597 save_size += 1 + 4 + 4;
2598 break;
2599 case record_full_reg:
2600 save_size += 1 + 4 + record_full_list->u.reg.len;
2601 break;
2602 case record_full_mem:
2603 save_size += 1 + 4 + 8 + record_full_list->u.mem.len;
2604 break;
2607 /* Make the new bfd section. */
2608 osec = bfd_make_section_anyway_with_flags (obfd.get (), "precord",
2609 SEC_HAS_CONTENTS
2610 | SEC_READONLY);
2611 if (osec == NULL)
2612 error (_("Failed to create 'precord' section for corefile %s: %s"),
2613 recfilename,
2614 bfd_errmsg (bfd_get_error ()));
2615 bfd_set_section_size (osec, save_size);
2616 bfd_set_section_vma (osec, 0);
2617 bfd_set_section_alignment (osec, 0);
2619 /* Save corefile state. */
2620 write_gcore_file (obfd.get ());
2622 /* Write out the record log. */
2623 /* Write the magic code. */
2624 magic = RECORD_FULL_FILE_MAGIC;
2625 if (record_debug)
2626 gdb_printf (gdb_stdlog,
2627 " Writing 4-byte magic cookie "
2628 "RECORD_FULL_FILE_MAGIC (0x%s)\n",
2629 phex_nz (magic, 4));
2630 bfdcore_write (obfd.get (), osec, &magic, sizeof (magic), &bfd_offset);
2632 /* Save the entries to recfd and forward execute to the end of
2633 record list. */
2634 record_full_list = &record_full_first;
2635 while (1)
2637 /* Save entry. */
2638 if (record_full_list != &record_full_first)
2640 uint8_t type;
2641 uint32_t regnum, len, signal, count;
2642 uint64_t addr;
2644 type = record_full_list->type;
2645 bfdcore_write (obfd.get (), osec, &type, sizeof (type), &bfd_offset);
2647 switch (record_full_list->type)
2649 case record_full_reg: /* reg */
2650 if (record_debug)
2651 gdb_printf (gdb_stdlog,
2652 " Writing register %d (1 "
2653 "plus %lu plus %d bytes)\n",
2654 record_full_list->u.reg.num,
2655 (unsigned long) sizeof (regnum),
2656 record_full_list->u.reg.len);
2658 /* Write regnum. */
2659 regnum = netorder32 (record_full_list->u.reg.num);
2660 bfdcore_write (obfd.get (), osec, &regnum,
2661 sizeof (regnum), &bfd_offset);
2663 /* Write regval. */
2664 bfdcore_write (obfd.get (), osec,
2665 record_full_get_loc (record_full_list),
2666 record_full_list->u.reg.len, &bfd_offset);
2667 break;
2669 case record_full_mem: /* mem */
2670 if (record_debug)
2671 gdb_printf (gdb_stdlog,
2672 " Writing memory %s (1 plus "
2673 "%lu plus %lu plus %d bytes)\n",
2674 paddress (gdbarch,
2675 record_full_list->u.mem.addr),
2676 (unsigned long) sizeof (addr),
2677 (unsigned long) sizeof (len),
2678 record_full_list->u.mem.len);
2680 /* Write memlen. */
2681 len = netorder32 (record_full_list->u.mem.len);
2682 bfdcore_write (obfd.get (), osec, &len, sizeof (len),
2683 &bfd_offset);
2685 /* Write memaddr. */
2686 addr = netorder64 (record_full_list->u.mem.addr);
2687 bfdcore_write (obfd.get (), osec, &addr,
2688 sizeof (addr), &bfd_offset);
2690 /* Write memval. */
2691 bfdcore_write (obfd.get (), osec,
2692 record_full_get_loc (record_full_list),
2693 record_full_list->u.mem.len, &bfd_offset);
2694 break;
2696 case record_full_end:
2697 if (record_debug)
2698 gdb_printf (gdb_stdlog,
2699 " Writing record_full_end (1 + "
2700 "%lu + %lu bytes)\n",
2701 (unsigned long) sizeof (signal),
2702 (unsigned long) sizeof (count));
2703 /* Write signal value. */
2704 signal = netorder32 (record_full_list->u.end.sigval);
2705 bfdcore_write (obfd.get (), osec, &signal,
2706 sizeof (signal), &bfd_offset);
2708 /* Write insn count. */
2709 count = netorder32 (record_full_list->u.end.insn_num);
2710 bfdcore_write (obfd.get (), osec, &count,
2711 sizeof (count), &bfd_offset);
2712 break;
2716 /* Execute entry. */
2717 record_full_exec_insn (regcache, gdbarch, record_full_list);
2719 if (record_full_list->next)
2720 record_full_list = record_full_list->next;
2721 else
2722 break;
2725 /* Reverse execute to cur_record_full_list. */
2726 while (1)
2728 /* Check for beginning and end of log. */
2729 if (record_full_list == cur_record_full_list)
2730 break;
2732 record_full_exec_insn (regcache, gdbarch, record_full_list);
2734 if (record_full_list->prev)
2735 record_full_list = record_full_list->prev;
2738 unlink_file.keep ();
2740 /* Succeeded. */
2741 gdb_printf (_("Saved core file %s with execution log.\n"),
2742 recfilename);
2745 /* record_full_goto_insn -- rewind the record log (forward or backward,
2746 depending on DIR) to the given entry, changing the program state
2747 correspondingly. */
2749 static void
2750 record_full_goto_insn (struct record_full_entry *entry,
2751 enum exec_direction_kind dir)
2753 scoped_restore restore_operation_disable
2754 = record_full_gdb_operation_disable_set ();
2755 regcache *regcache = get_thread_regcache (inferior_thread ());
2756 struct gdbarch *gdbarch = regcache->arch ();
2758 /* Assume everything is valid: we will hit the entry,
2759 and we will not hit the end of the recording. */
2761 if (dir == EXEC_FORWARD)
2762 record_full_list = record_full_list->next;
2766 record_full_exec_insn (regcache, gdbarch, record_full_list);
2767 if (dir == EXEC_REVERSE)
2768 record_full_list = record_full_list->prev;
2769 else
2770 record_full_list = record_full_list->next;
2771 } while (record_full_list != entry);
2774 /* Alias for "target record-full". */
2776 static void
2777 cmd_record_full_start (const char *args, int from_tty)
2779 execute_command ("target record-full", from_tty);
2782 static void
2783 set_record_full_insn_max_num (const char *args, int from_tty,
2784 struct cmd_list_element *c)
2786 if (record_full_insn_num > record_full_insn_max_num)
2788 /* Count down record_full_insn_num while releasing records from list. */
2789 while (record_full_insn_num > record_full_insn_max_num)
2791 record_full_list_release_first ();
2792 record_full_insn_num--;
2797 /* Implement the 'maintenance print record-instruction' command. */
2799 static void
2800 maintenance_print_record_instruction (const char *args, int from_tty)
2802 struct record_full_entry *to_print = record_full_list;
2804 if (args != nullptr)
2806 int offset = value_as_long (parse_and_eval (args));
2807 if (offset > 0)
2809 /* Move forward OFFSET instructions. We know we found the
2810 end of an instruction when to_print->type is record_full_end. */
2811 while (to_print->next != nullptr && offset > 0)
2813 to_print = to_print->next;
2814 if (to_print->type == record_full_end)
2815 offset--;
2817 if (offset != 0)
2818 error (_("Not enough recorded history"));
2820 else
2822 while (to_print->prev != nullptr && offset < 0)
2824 to_print = to_print->prev;
2825 if (to_print->type == record_full_end)
2826 offset++;
2828 if (offset != 0)
2829 error (_("Not enough recorded history"));
2832 gdb_assert (to_print != nullptr);
2834 gdbarch *arch = current_inferior ()->arch ();
2836 /* Go back to the start of the instruction. */
2837 while (to_print->prev != nullptr && to_print->prev->type != record_full_end)
2838 to_print = to_print->prev;
2840 /* if we're in the first record, there are no actual instructions
2841 recorded. Warn the user and leave. */
2842 if (to_print == &record_full_first)
2843 error (_("Not enough recorded history"));
2845 while (to_print->type != record_full_end)
2847 switch (to_print->type)
2849 case record_full_reg:
2851 type *regtype = gdbarch_register_type (arch, to_print->u.reg.num);
2852 value *val
2853 = value_from_contents (regtype,
2854 record_full_get_loc (to_print));
2855 gdb_printf ("Register %s changed: ",
2856 gdbarch_register_name (arch, to_print->u.reg.num));
2857 struct value_print_options opts;
2858 get_user_print_options (&opts);
2859 opts.raw = true;
2860 value_print (val, gdb_stdout, &opts);
2861 gdb_printf ("\n");
2862 break;
2864 case record_full_mem:
2866 gdb_byte *b = record_full_get_loc (to_print);
2867 gdb_printf ("%d bytes of memory at address %s changed from:",
2868 to_print->u.mem.len,
2869 print_core_address (arch, to_print->u.mem.addr));
2870 for (int i = 0; i < to_print->u.mem.len; i++)
2871 gdb_printf (" %02x", b[i]);
2872 gdb_printf ("\n");
2873 break;
2876 to_print = to_print->next;
2880 void _initialize_record_full ();
2881 void
2882 _initialize_record_full ()
2884 struct cmd_list_element *c;
2886 /* Init record_full_first. */
2887 record_full_first.prev = NULL;
2888 record_full_first.next = NULL;
2889 record_full_first.type = record_full_end;
2891 add_target (record_full_target_info, record_full_open);
2892 add_deprecated_target_alias (record_full_target_info, "record");
2893 add_target (record_full_core_target_info, record_full_open);
2895 add_prefix_cmd ("full", class_obscure, cmd_record_full_start,
2896 _("Start full execution recording."), &record_full_cmdlist,
2897 0, &record_cmdlist);
2899 cmd_list_element *record_full_restore_cmd
2900 = add_cmd ("restore", class_obscure, cmd_record_full_restore,
2901 _("Restore the execution log from a file.\n\
2902 Argument is filename. File must be created with 'record save'."),
2903 &record_full_cmdlist);
2904 set_cmd_completer (record_full_restore_cmd, deprecated_filename_completer);
2906 /* Deprecate the old version without "full" prefix. */
2907 c = add_alias_cmd ("restore", record_full_restore_cmd, class_obscure, 1,
2908 &record_cmdlist);
2909 set_cmd_completer (c, deprecated_filename_completer);
2910 deprecate_cmd (c, "record full restore");
2912 add_setshow_prefix_cmd ("full", class_support,
2913 _("Set record options."),
2914 _("Show record options."),
2915 &set_record_full_cmdlist,
2916 &show_record_full_cmdlist,
2917 &set_record_cmdlist,
2918 &show_record_cmdlist);
2920 /* Record instructions number limit command. */
2921 set_show_commands set_record_full_stop_at_limit_cmds
2922 = add_setshow_boolean_cmd ("stop-at-limit", no_class,
2923 &record_full_stop_at_limit, _("\
2924 Set whether record/replay stops when record/replay buffer becomes full."), _("\
2925 Show whether record/replay stops when record/replay buffer becomes full."),
2926 _("Default is ON.\n\
2927 When ON, if the record/replay buffer becomes full, ask user what to do.\n\
2928 When OFF, if the record/replay buffer becomes full,\n\
2929 delete the oldest recorded instruction to make room for each new one."),
2930 NULL, NULL,
2931 &set_record_full_cmdlist,
2932 &show_record_full_cmdlist);
2934 c = add_alias_cmd ("stop-at-limit",
2935 set_record_full_stop_at_limit_cmds.set, no_class, 1,
2936 &set_record_cmdlist);
2937 deprecate_cmd (c, "set record full stop-at-limit");
2939 c = add_alias_cmd ("stop-at-limit",
2940 set_record_full_stop_at_limit_cmds.show, no_class, 1,
2941 &show_record_cmdlist);
2942 deprecate_cmd (c, "show record full stop-at-limit");
2944 set_show_commands record_full_insn_number_max_cmds
2945 = add_setshow_uinteger_cmd ("insn-number-max", no_class,
2946 &record_full_insn_max_num,
2947 _("Set record/replay buffer limit."),
2948 _("Show record/replay buffer limit."), _("\
2949 Set the maximum number of instructions to be stored in the\n\
2950 record/replay buffer. A value of either \"unlimited\" or zero means no\n\
2951 limit. Default is 200000."),
2952 set_record_full_insn_max_num,
2953 NULL, &set_record_full_cmdlist,
2954 &show_record_full_cmdlist);
2956 c = add_alias_cmd ("insn-number-max", record_full_insn_number_max_cmds.set,
2957 no_class, 1, &set_record_cmdlist);
2958 deprecate_cmd (c, "set record full insn-number-max");
2960 c = add_alias_cmd ("insn-number-max", record_full_insn_number_max_cmds.show,
2961 no_class, 1, &show_record_cmdlist);
2962 deprecate_cmd (c, "show record full insn-number-max");
2964 set_show_commands record_full_memory_query_cmds
2965 = add_setshow_boolean_cmd ("memory-query", no_class,
2966 &record_full_memory_query, _("\
2967 Set whether query if PREC cannot record memory change of next instruction."),
2968 _("\
2969 Show whether query if PREC cannot record memory change of next instruction."),
2970 _("\
2971 Default is OFF.\n\
2972 When ON, query if PREC cannot record memory change of next instruction."),
2973 NULL, NULL,
2974 &set_record_full_cmdlist,
2975 &show_record_full_cmdlist);
2977 c = add_alias_cmd ("memory-query", record_full_memory_query_cmds.set,
2978 no_class, 1, &set_record_cmdlist);
2979 deprecate_cmd (c, "set record full memory-query");
2981 c = add_alias_cmd ("memory-query", record_full_memory_query_cmds.show,
2982 no_class, 1,&show_record_cmdlist);
2983 deprecate_cmd (c, "show record full memory-query");
2985 add_cmd ("record-instruction", class_maintenance,
2986 maintenance_print_record_instruction,
2987 _("\
2988 Print a recorded instruction.\n\
2989 If no argument is provided, print the last instruction recorded.\n\
2990 If a negative argument is given, prints how the nth previous \
2991 instruction will be undone.\n\
2992 If a positive argument is given, prints \
2993 how the nth following instruction will be redone."), &maintenanceprintlist);