s390: Fix build when using EXEEXT_FOR_BUILD
[binutils-gdb.git] / gdb / record-full.c
blob2fc9e433ca85644e56adccc394460a73e5364ec2
1 /* Process record and replay target for GDB, the GNU debugger.
3 Copyright (C) 2013-2023 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "defs.h"
21 #include "gdbcmd.h"
22 #include "regcache.h"
23 #include "gdbthread.h"
24 #include "inferior.h"
25 #include "event-top.h"
26 #include "completer.h"
27 #include "arch-utils.h"
28 #include "gdbcore.h"
29 #include "exec.h"
30 #include "record.h"
31 #include "record-full.h"
32 #include "elf-bfd.h"
33 #include "gcore.h"
34 #include "gdbsupport/event-loop.h"
35 #include "inf-loop.h"
36 #include "gdb_bfd.h"
37 #include "observable.h"
38 #include "infrun.h"
39 #include "gdbsupport/gdb_unlinker.h"
40 #include "gdbsupport/byte-vector.h"
41 #include "async-event.h"
42 #include "valprint.h"
43 #include "interps.h"
45 #include <signal.h>
47 /* This module implements "target record-full", also known as "process
48 record and replay". This target sits on top of a "normal" target
49 (a target that "has execution"), and provides a record and replay
50 functionality, including reverse debugging.
52 Target record has two modes: recording, and replaying.
54 In record mode, we intercept the resume and wait methods.
55 Whenever gdb resumes the target, we run the target in single step
56 mode, and we build up an execution log in which, for each executed
57 instruction, we record all changes in memory and register state.
58 This is invisible to the user, to whom it just looks like an
59 ordinary debugging session (except for performance degradation).
61 In replay mode, instead of actually letting the inferior run as a
62 process, we simulate its execution by playing back the recorded
63 execution log. For each instruction in the log, we simulate the
64 instruction's side effects by duplicating the changes that it would
65 have made on memory and registers. */
67 #define DEFAULT_RECORD_FULL_INSN_MAX_NUM 200000
69 #define RECORD_FULL_IS_REPLAY \
70 (record_full_list->next || ::execution_direction == EXEC_REVERSE)
72 #define RECORD_FULL_FILE_MAGIC netorder32(0x20091016)
74 /* These are the core structs of the process record functionality.
76 A record_full_entry is a record of the value change of a register
77 ("record_full_reg") or a part of memory ("record_full_mem"). And each
78 instruction must have a struct record_full_entry ("record_full_end")
79 that indicates that this is the last struct record_full_entry of this
80 instruction.
82 Each struct record_full_entry is linked to "record_full_list" by "prev"
83 and "next" pointers. */
85 struct record_full_mem_entry
87 CORE_ADDR addr;
88 int len;
89 /* Set this flag if target memory for this entry
90 can no longer be accessed. */
91 int mem_entry_not_accessible;
92 union
94 gdb_byte *ptr;
95 gdb_byte buf[sizeof (gdb_byte *)];
96 } u;
99 struct record_full_reg_entry
101 unsigned short num;
102 unsigned short len;
103 union
105 gdb_byte *ptr;
106 gdb_byte buf[2 * sizeof (gdb_byte *)];
107 } u;
110 struct record_full_end_entry
112 enum gdb_signal sigval;
113 ULONGEST insn_num;
116 enum record_full_type
118 record_full_end = 0,
119 record_full_reg,
120 record_full_mem
123 /* This is the data structure that makes up the execution log.
125 The execution log consists of a single linked list of entries
126 of type "struct record_full_entry". It is doubly linked so that it
127 can be traversed in either direction.
129 The start of the list is anchored by a struct called
130 "record_full_first". The pointer "record_full_list" either points
131 to the last entry that was added to the list (in record mode), or to
132 the next entry in the list that will be executed (in replay mode).
134 Each list element (struct record_full_entry), in addition to next
135 and prev pointers, consists of a union of three entry types: mem,
136 reg, and end. A field called "type" determines which entry type is
137 represented by a given list element.
139 Each instruction that is added to the execution log is represented
140 by a variable number of list elements ('entries'). The instruction
141 will have one "reg" entry for each register that is changed by
142 executing the instruction (including the PC in every case). It
143 will also have one "mem" entry for each memory change. Finally,
144 each instruction will have an "end" entry that separates it from
145 the changes associated with the next instruction. */
147 struct record_full_entry
149 struct record_full_entry *prev;
150 struct record_full_entry *next;
151 enum record_full_type type;
152 union
154 /* reg */
155 struct record_full_reg_entry reg;
156 /* mem */
157 struct record_full_mem_entry mem;
158 /* end */
159 struct record_full_end_entry end;
160 } u;
163 /* If true, query if PREC cannot record memory
164 change of next instruction. */
165 bool record_full_memory_query = false;
167 struct record_full_core_buf_entry
169 struct record_full_core_buf_entry *prev;
170 struct target_section *p;
171 bfd_byte *buf;
174 /* Record buf with core target. */
175 static detached_regcache *record_full_core_regbuf = NULL;
176 static std::vector<target_section> record_full_core_sections;
177 static struct record_full_core_buf_entry *record_full_core_buf_list = NULL;
179 /* The following variables are used for managing the linked list that
180 represents the execution log.
182 record_full_first is the anchor that holds down the beginning of
183 the list.
185 record_full_list serves two functions:
186 1) In record mode, it anchors the end of the list.
187 2) In replay mode, it traverses the list and points to
188 the next instruction that must be emulated.
190 record_full_arch_list_head and record_full_arch_list_tail are used
191 to manage a separate list, which is used to build up the change
192 elements of the currently executing instruction during record mode.
193 When this instruction has been completely annotated in the "arch
194 list", it will be appended to the main execution log. */
196 static struct record_full_entry record_full_first;
197 static struct record_full_entry *record_full_list = &record_full_first;
198 static struct record_full_entry *record_full_arch_list_head = NULL;
199 static struct record_full_entry *record_full_arch_list_tail = NULL;
201 /* true ask user. false auto delete the last struct record_full_entry. */
202 static bool record_full_stop_at_limit = true;
203 /* Maximum allowed number of insns in execution log. */
204 static unsigned int record_full_insn_max_num
205 = DEFAULT_RECORD_FULL_INSN_MAX_NUM;
206 /* Actual count of insns presently in execution log. */
207 static unsigned int record_full_insn_num = 0;
208 /* Count of insns logged so far (may be larger
209 than count of insns presently in execution log). */
210 static ULONGEST record_full_insn_count;
212 static const char record_longname[]
213 = N_("Process record and replay target");
214 static const char record_doc[]
215 = N_("Log program while executing and replay execution from log.");
217 /* Base class implementing functionality common to both the
218 "record-full" and "record-core" targets. */
220 class record_full_base_target : public target_ops
222 public:
223 const target_info &info () const override = 0;
225 strata stratum () const override { return record_stratum; }
227 void close () override;
228 void async (bool) override;
229 ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override;
230 bool stopped_by_watchpoint () override;
231 bool stopped_data_address (CORE_ADDR *) override;
233 bool stopped_by_sw_breakpoint () override;
234 bool supports_stopped_by_sw_breakpoint () override;
236 bool stopped_by_hw_breakpoint () override;
237 bool supports_stopped_by_hw_breakpoint () override;
239 bool can_execute_reverse () override;
241 /* Add bookmark target methods. */
242 gdb_byte *get_bookmark (const char *, int) override;
243 void goto_bookmark (const gdb_byte *, int) override;
244 enum exec_direction_kind execution_direction () override;
245 enum record_method record_method (ptid_t ptid) override;
246 void info_record () override;
247 void save_record (const char *filename) override;
248 bool supports_delete_record () override;
249 void delete_record () override;
250 bool record_is_replaying (ptid_t ptid) override;
251 bool record_will_replay (ptid_t ptid, int dir) override;
252 void record_stop_replaying () override;
253 void goto_record_begin () override;
254 void goto_record_end () override;
255 void goto_record (ULONGEST insn) override;
258 /* The "record-full" target. */
260 static const target_info record_full_target_info = {
261 "record-full",
262 record_longname,
263 record_doc,
266 class record_full_target final : public record_full_base_target
268 public:
269 const target_info &info () const override
270 { return record_full_target_info; }
272 void resume (ptid_t, int, enum gdb_signal) override;
273 void disconnect (const char *, int) override;
274 void detach (inferior *, int) override;
275 void mourn_inferior () override;
276 void kill () override;
277 void store_registers (struct regcache *, int) override;
278 enum target_xfer_status xfer_partial (enum target_object object,
279 const char *annex,
280 gdb_byte *readbuf,
281 const gdb_byte *writebuf,
282 ULONGEST offset, ULONGEST len,
283 ULONGEST *xfered_len) override;
284 int insert_breakpoint (struct gdbarch *,
285 struct bp_target_info *) override;
286 int remove_breakpoint (struct gdbarch *,
287 struct bp_target_info *,
288 enum remove_bp_reason) override;
291 /* The "record-core" target. */
293 static const target_info record_full_core_target_info = {
294 "record-core",
295 record_longname,
296 record_doc,
299 class record_full_core_target final : public record_full_base_target
301 public:
302 const target_info &info () const override
303 { return record_full_core_target_info; }
305 void resume (ptid_t, int, enum gdb_signal) override;
306 void disconnect (const char *, int) override;
307 void kill () override;
308 void fetch_registers (struct regcache *regcache, int regno) override;
309 void prepare_to_store (struct regcache *regcache) override;
310 void store_registers (struct regcache *, int) override;
311 enum target_xfer_status xfer_partial (enum target_object object,
312 const char *annex,
313 gdb_byte *readbuf,
314 const gdb_byte *writebuf,
315 ULONGEST offset, ULONGEST len,
316 ULONGEST *xfered_len) override;
317 int insert_breakpoint (struct gdbarch *,
318 struct bp_target_info *) override;
319 int remove_breakpoint (struct gdbarch *,
320 struct bp_target_info *,
321 enum remove_bp_reason) override;
323 bool has_execution (inferior *inf) override;
326 static record_full_target record_full_ops;
327 static record_full_core_target record_full_core_ops;
329 void
330 record_full_target::detach (inferior *inf, int from_tty)
332 record_detach (this, inf, from_tty);
335 void
336 record_full_target::disconnect (const char *args, int from_tty)
338 record_disconnect (this, args, from_tty);
341 void
342 record_full_core_target::disconnect (const char *args, int from_tty)
344 record_disconnect (this, args, from_tty);
347 void
348 record_full_target::mourn_inferior ()
350 record_mourn_inferior (this);
353 void
354 record_full_target::kill ()
356 record_kill (this);
359 /* See record-full.h. */
362 record_full_is_used (void)
364 struct target_ops *t;
366 t = find_record_target ();
367 return (t == &record_full_ops
368 || t == &record_full_core_ops);
372 /* Command lists for "set/show record full". */
373 static struct cmd_list_element *set_record_full_cmdlist;
374 static struct cmd_list_element *show_record_full_cmdlist;
376 /* Command list for "record full". */
377 static struct cmd_list_element *record_full_cmdlist;
379 static void record_full_goto_insn (struct record_full_entry *entry,
380 enum exec_direction_kind dir);
382 /* Alloc and free functions for record_full_reg, record_full_mem, and
383 record_full_end entries. */
385 /* Alloc a record_full_reg record entry. */
387 static inline struct record_full_entry *
388 record_full_reg_alloc (struct regcache *regcache, int regnum)
390 struct record_full_entry *rec;
391 struct gdbarch *gdbarch = regcache->arch ();
393 rec = XCNEW (struct record_full_entry);
394 rec->type = record_full_reg;
395 rec->u.reg.num = regnum;
396 rec->u.reg.len = register_size (gdbarch, regnum);
397 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
398 rec->u.reg.u.ptr = (gdb_byte *) xmalloc (rec->u.reg.len);
400 return rec;
403 /* Free a record_full_reg record entry. */
405 static inline void
406 record_full_reg_release (struct record_full_entry *rec)
408 gdb_assert (rec->type == record_full_reg);
409 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
410 xfree (rec->u.reg.u.ptr);
411 xfree (rec);
414 /* Alloc a record_full_mem record entry. */
416 static inline struct record_full_entry *
417 record_full_mem_alloc (CORE_ADDR addr, int len)
419 struct record_full_entry *rec;
421 rec = XCNEW (struct record_full_entry);
422 rec->type = record_full_mem;
423 rec->u.mem.addr = addr;
424 rec->u.mem.len = len;
425 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
426 rec->u.mem.u.ptr = (gdb_byte *) xmalloc (len);
428 return rec;
431 /* Free a record_full_mem record entry. */
433 static inline void
434 record_full_mem_release (struct record_full_entry *rec)
436 gdb_assert (rec->type == record_full_mem);
437 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
438 xfree (rec->u.mem.u.ptr);
439 xfree (rec);
442 /* Alloc a record_full_end record entry. */
444 static inline struct record_full_entry *
445 record_full_end_alloc (void)
447 struct record_full_entry *rec;
449 rec = XCNEW (struct record_full_entry);
450 rec->type = record_full_end;
452 return rec;
455 /* Free a record_full_end record entry. */
457 static inline void
458 record_full_end_release (struct record_full_entry *rec)
460 xfree (rec);
463 /* Free one record entry, any type.
464 Return entry->type, in case caller wants to know. */
466 static inline enum record_full_type
467 record_full_entry_release (struct record_full_entry *rec)
469 enum record_full_type type = rec->type;
471 switch (type) {
472 case record_full_reg:
473 record_full_reg_release (rec);
474 break;
475 case record_full_mem:
476 record_full_mem_release (rec);
477 break;
478 case record_full_end:
479 record_full_end_release (rec);
480 break;
482 return type;
485 /* Free all record entries in list pointed to by REC. */
487 static void
488 record_full_list_release (struct record_full_entry *rec)
490 if (!rec)
491 return;
493 while (rec->next)
494 rec = rec->next;
496 while (rec->prev)
498 rec = rec->prev;
499 record_full_entry_release (rec->next);
502 if (rec == &record_full_first)
504 record_full_insn_num = 0;
505 record_full_first.next = NULL;
507 else
508 record_full_entry_release (rec);
511 /* Free all record entries forward of the given list position. */
513 static void
514 record_full_list_release_following (struct record_full_entry *rec)
516 struct record_full_entry *tmp = rec->next;
518 rec->next = NULL;
519 while (tmp)
521 rec = tmp->next;
522 if (record_full_entry_release (tmp) == record_full_end)
524 record_full_insn_num--;
525 record_full_insn_count--;
527 tmp = rec;
531 /* Delete the first instruction from the beginning of the log, to make
532 room for adding a new instruction at the end of the log.
534 Note -- this function does not modify record_full_insn_num. */
536 static void
537 record_full_list_release_first (void)
539 struct record_full_entry *tmp;
541 if (!record_full_first.next)
542 return;
544 /* Loop until a record_full_end. */
545 while (1)
547 /* Cut record_full_first.next out of the linked list. */
548 tmp = record_full_first.next;
549 record_full_first.next = tmp->next;
550 tmp->next->prev = &record_full_first;
552 /* tmp is now isolated, and can be deleted. */
553 if (record_full_entry_release (tmp) == record_full_end)
554 break; /* End loop at first record_full_end. */
556 if (!record_full_first.next)
558 gdb_assert (record_full_insn_num == 1);
559 break; /* End loop when list is empty. */
564 /* Add a struct record_full_entry to record_full_arch_list. */
566 static void
567 record_full_arch_list_add (struct record_full_entry *rec)
569 if (record_debug > 1)
570 gdb_printf (gdb_stdlog,
571 "Process record: record_full_arch_list_add %s.\n",
572 host_address_to_string (rec));
574 if (record_full_arch_list_tail)
576 record_full_arch_list_tail->next = rec;
577 rec->prev = record_full_arch_list_tail;
578 record_full_arch_list_tail = rec;
580 else
582 record_full_arch_list_head = rec;
583 record_full_arch_list_tail = rec;
587 /* Return the value storage location of a record entry. */
588 static inline gdb_byte *
589 record_full_get_loc (struct record_full_entry *rec)
591 switch (rec->type) {
592 case record_full_mem:
593 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
594 return rec->u.mem.u.ptr;
595 else
596 return rec->u.mem.u.buf;
597 case record_full_reg:
598 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
599 return rec->u.reg.u.ptr;
600 else
601 return rec->u.reg.u.buf;
602 case record_full_end:
603 default:
604 gdb_assert_not_reached ("unexpected record_full_entry type");
605 return NULL;
609 /* Record the value of a register NUM to record_full_arch_list. */
612 record_full_arch_list_add_reg (struct regcache *regcache, int regnum)
614 struct record_full_entry *rec;
616 if (record_debug > 1)
617 gdb_printf (gdb_stdlog,
618 "Process record: add register num = %d to "
619 "record list.\n",
620 regnum);
622 rec = record_full_reg_alloc (regcache, regnum);
624 regcache->raw_read (regnum, record_full_get_loc (rec));
626 record_full_arch_list_add (rec);
628 return 0;
631 /* Record the value of a region of memory whose address is ADDR and
632 length is LEN to record_full_arch_list. */
635 record_full_arch_list_add_mem (CORE_ADDR addr, int len)
637 struct record_full_entry *rec;
639 if (record_debug > 1)
640 gdb_printf (gdb_stdlog,
641 "Process record: add mem addr = %s len = %d to "
642 "record list.\n",
643 paddress (current_inferior ()->arch (), addr), len);
645 if (!addr) /* FIXME: Why? Some arch must permit it... */
646 return 0;
648 rec = record_full_mem_alloc (addr, len);
650 if (record_read_memory (current_inferior ()->arch (), addr,
651 record_full_get_loc (rec), len))
653 record_full_mem_release (rec);
654 return -1;
657 record_full_arch_list_add (rec);
659 return 0;
662 /* Add a record_full_end type struct record_full_entry to
663 record_full_arch_list. */
666 record_full_arch_list_add_end (void)
668 struct record_full_entry *rec;
670 if (record_debug > 1)
671 gdb_printf (gdb_stdlog,
672 "Process record: add end to arch list.\n");
674 rec = record_full_end_alloc ();
675 rec->u.end.sigval = GDB_SIGNAL_0;
676 rec->u.end.insn_num = ++record_full_insn_count;
678 record_full_arch_list_add (rec);
680 return 0;
683 static void
684 record_full_check_insn_num (void)
686 if (record_full_insn_num == record_full_insn_max_num)
688 /* Ask user what to do. */
689 if (record_full_stop_at_limit)
691 if (!yquery (_("Do you want to auto delete previous execution "
692 "log entries when record/replay buffer becomes "
693 "full (record full stop-at-limit)?")))
694 error (_("Process record: stopped by user."));
695 record_full_stop_at_limit = 0;
700 /* Before inferior step (when GDB record the running message, inferior
701 only can step), GDB will call this function to record the values to
702 record_full_list. This function will call gdbarch_process_record to
703 record the running message of inferior and set them to
704 record_full_arch_list, and add it to record_full_list. */
706 static void
707 record_full_message (struct regcache *regcache, enum gdb_signal signal)
709 int ret;
710 struct gdbarch *gdbarch = regcache->arch ();
714 record_full_arch_list_head = NULL;
715 record_full_arch_list_tail = NULL;
717 /* Check record_full_insn_num. */
718 record_full_check_insn_num ();
720 /* If gdb sends a signal value to target_resume,
721 save it in the 'end' field of the previous instruction.
723 Maybe process record should record what really happened,
724 rather than what gdb pretends has happened.
726 So if Linux delivered the signal to the child process during
727 the record mode, we will record it and deliver it again in
728 the replay mode.
730 If user says "ignore this signal" during the record mode, then
731 it will be ignored again during the replay mode (no matter if
732 the user says something different, like "deliver this signal"
733 during the replay mode).
735 User should understand that nothing he does during the replay
736 mode will change the behavior of the child. If he tries,
737 then that is a user error.
739 But we should still deliver the signal to gdb during the replay,
740 if we delivered it during the recording. Therefore we should
741 record the signal during record_full_wait, not
742 record_full_resume. */
743 if (record_full_list != &record_full_first) /* FIXME better way
744 to check */
746 gdb_assert (record_full_list->type == record_full_end);
747 record_full_list->u.end.sigval = signal;
750 if (signal == GDB_SIGNAL_0
751 || !gdbarch_process_record_signal_p (gdbarch))
752 ret = gdbarch_process_record (gdbarch,
753 regcache,
754 regcache_read_pc (regcache));
755 else
756 ret = gdbarch_process_record_signal (gdbarch,
757 regcache,
758 signal);
760 if (ret > 0)
761 error (_("Process record: inferior program stopped."));
762 if (ret < 0)
763 error (_("Process record: failed to record execution log."));
765 catch (const gdb_exception &ex)
767 record_full_list_release (record_full_arch_list_tail);
768 throw;
771 record_full_list->next = record_full_arch_list_head;
772 record_full_arch_list_head->prev = record_full_list;
773 record_full_list = record_full_arch_list_tail;
775 if (record_full_insn_num == record_full_insn_max_num)
776 record_full_list_release_first ();
777 else
778 record_full_insn_num++;
781 static bool
782 record_full_message_wrapper_safe (struct regcache *regcache,
783 enum gdb_signal signal)
787 record_full_message (regcache, signal);
789 catch (const gdb_exception_error &ex)
791 exception_print (gdb_stderr, ex);
792 return false;
795 return true;
798 /* Set to 1 if record_full_store_registers and record_full_xfer_partial
799 doesn't need record. */
801 static int record_full_gdb_operation_disable = 0;
803 scoped_restore_tmpl<int>
804 record_full_gdb_operation_disable_set (void)
806 return make_scoped_restore (&record_full_gdb_operation_disable, 1);
809 /* Flag set to TRUE for target_stopped_by_watchpoint. */
810 static enum target_stop_reason record_full_stop_reason
811 = TARGET_STOPPED_BY_NO_REASON;
813 /* Execute one instruction from the record log. Each instruction in
814 the log will be represented by an arbitrary sequence of register
815 entries and memory entries, followed by an 'end' entry. */
817 static inline void
818 record_full_exec_insn (struct regcache *regcache,
819 struct gdbarch *gdbarch,
820 struct record_full_entry *entry)
822 switch (entry->type)
824 case record_full_reg: /* reg */
826 gdb::byte_vector reg (entry->u.reg.len);
828 if (record_debug > 1)
829 gdb_printf (gdb_stdlog,
830 "Process record: record_full_reg %s to "
831 "inferior num = %d.\n",
832 host_address_to_string (entry),
833 entry->u.reg.num);
835 regcache->cooked_read (entry->u.reg.num, reg.data ());
836 regcache->cooked_write (entry->u.reg.num, record_full_get_loc (entry));
837 memcpy (record_full_get_loc (entry), reg.data (), entry->u.reg.len);
839 break;
841 case record_full_mem: /* mem */
843 /* Nothing to do if the entry is flagged not_accessible. */
844 if (!entry->u.mem.mem_entry_not_accessible)
846 gdb::byte_vector mem (entry->u.mem.len);
848 if (record_debug > 1)
849 gdb_printf (gdb_stdlog,
850 "Process record: record_full_mem %s to "
851 "inferior addr = %s len = %d.\n",
852 host_address_to_string (entry),
853 paddress (gdbarch, entry->u.mem.addr),
854 entry->u.mem.len);
856 if (record_read_memory (gdbarch,
857 entry->u.mem.addr, mem.data (),
858 entry->u.mem.len))
859 entry->u.mem.mem_entry_not_accessible = 1;
860 else
862 if (target_write_memory (entry->u.mem.addr,
863 record_full_get_loc (entry),
864 entry->u.mem.len))
866 entry->u.mem.mem_entry_not_accessible = 1;
867 if (record_debug)
868 warning (_("Process record: error writing memory at "
869 "addr = %s len = %d."),
870 paddress (gdbarch, entry->u.mem.addr),
871 entry->u.mem.len);
873 else
875 memcpy (record_full_get_loc (entry), mem.data (),
876 entry->u.mem.len);
878 /* We've changed memory --- check if a hardware
879 watchpoint should trap. Note that this
880 presently assumes the target beneath supports
881 continuable watchpoints. On non-continuable
882 watchpoints target, we'll want to check this
883 _before_ actually doing the memory change, and
884 not doing the change at all if the watchpoint
885 traps. */
886 if (hardware_watchpoint_inserted_in_range
887 (current_inferior ()->aspace.get (),
888 entry->u.mem.addr, entry->u.mem.len))
889 record_full_stop_reason = TARGET_STOPPED_BY_WATCHPOINT;
894 break;
898 static void record_full_restore (void);
900 /* Asynchronous signal handle registered as event loop source for when
901 we have pending events ready to be passed to the core. */
903 static struct async_event_handler *record_full_async_inferior_event_token;
905 static void
906 record_full_async_inferior_event_handler (gdb_client_data data)
908 inferior_event_handler (INF_REG_EVENT);
911 /* Open the process record target for 'core' files. */
913 static void
914 record_full_core_open_1 (const char *name, int from_tty)
916 regcache *regcache = get_thread_regcache (inferior_thread ());
917 int regnum = gdbarch_num_regs (regcache->arch ());
918 int i;
920 /* Get record_full_core_regbuf. */
921 target_fetch_registers (regcache, -1);
922 record_full_core_regbuf = new detached_regcache (regcache->arch (), false);
924 for (i = 0; i < regnum; i ++)
925 record_full_core_regbuf->raw_supply (i, *regcache);
927 record_full_core_sections = build_section_table (core_bfd);
929 current_inferior ()->push_target (&record_full_core_ops);
930 record_full_restore ();
933 /* Open the process record target for 'live' processes. */
935 static void
936 record_full_open_1 (const char *name, int from_tty)
938 if (record_debug)
939 gdb_printf (gdb_stdlog, "Process record: record_full_open_1\n");
941 /* check exec */
942 if (!target_has_execution ())
943 error (_("Process record: the program is not being run."));
944 if (non_stop)
945 error (_("Process record target can't debug inferior in non-stop mode "
946 "(non-stop)."));
948 if (!gdbarch_process_record_p (current_inferior ()->arch ()))
949 error (_("Process record: the current architecture doesn't support "
950 "record function."));
952 current_inferior ()->push_target (&record_full_ops);
955 static void record_full_init_record_breakpoints (void);
957 /* Open the process record target. */
959 static void
960 record_full_open (const char *name, int from_tty)
962 if (record_debug)
963 gdb_printf (gdb_stdlog, "Process record: record_full_open\n");
965 record_preopen ();
967 /* Reset */
968 record_full_insn_num = 0;
969 record_full_insn_count = 0;
970 record_full_list = &record_full_first;
971 record_full_list->next = NULL;
973 if (core_bfd)
974 record_full_core_open_1 (name, from_tty);
975 else
976 record_full_open_1 (name, from_tty);
978 /* Register extra event sources in the event loop. */
979 record_full_async_inferior_event_token
980 = create_async_event_handler (record_full_async_inferior_event_handler,
981 NULL, "record-full");
983 record_full_init_record_breakpoints ();
985 interps_notify_record_changed (current_inferior (), 1, "full", NULL);
988 /* "close" target method. Close the process record target. */
990 void
991 record_full_base_target::close ()
993 struct record_full_core_buf_entry *entry;
995 if (record_debug)
996 gdb_printf (gdb_stdlog, "Process record: record_full_close\n");
998 record_full_list_release (record_full_list);
1000 /* Release record_full_core_regbuf. */
1001 if (record_full_core_regbuf)
1003 delete record_full_core_regbuf;
1004 record_full_core_regbuf = NULL;
1007 /* Release record_full_core_buf_list. */
1008 while (record_full_core_buf_list)
1010 entry = record_full_core_buf_list;
1011 record_full_core_buf_list = record_full_core_buf_list->prev;
1012 xfree (entry);
1015 if (record_full_async_inferior_event_token)
1016 delete_async_event_handler (&record_full_async_inferior_event_token);
1019 /* "async" target method. */
1021 void
1022 record_full_base_target::async (bool enable)
1024 if (enable)
1025 mark_async_event_handler (record_full_async_inferior_event_token);
1026 else
1027 clear_async_event_handler (record_full_async_inferior_event_token);
1029 beneath ()->async (enable);
1032 /* The PTID and STEP arguments last passed to
1033 record_full_target::resume. */
1034 static ptid_t record_full_resume_ptid = null_ptid;
1035 static int record_full_resume_step = 0;
1037 /* True if we've been resumed, and so each record_full_wait call should
1038 advance execution. If this is false, record_full_wait will return a
1039 TARGET_WAITKIND_IGNORE. */
1040 static int record_full_resumed = 0;
1042 /* The execution direction of the last resume we got. This is
1043 necessary for async mode. Vis (order is not strictly accurate):
1045 1. user has the global execution direction set to forward
1046 2. user does a reverse-step command
1047 3. record_full_resume is called with global execution direction
1048 temporarily switched to reverse
1049 4. GDB's execution direction is reverted back to forward
1050 5. target record notifies event loop there's an event to handle
1051 6. infrun asks the target which direction was it going, and switches
1052 the global execution direction accordingly (to reverse)
1053 7. infrun polls an event out of the record target, and handles it
1054 8. GDB goes back to the event loop, and goto #4.
1056 static enum exec_direction_kind record_full_execution_dir = EXEC_FORWARD;
1058 /* "resume" target method. Resume the process record target. */
1060 void
1061 record_full_target::resume (ptid_t ptid, int step, enum gdb_signal signal)
1063 record_full_resume_ptid = inferior_ptid;
1064 record_full_resume_step = step;
1065 record_full_resumed = 1;
1066 record_full_execution_dir = ::execution_direction;
1068 if (!RECORD_FULL_IS_REPLAY)
1070 struct gdbarch *gdbarch = target_thread_architecture (ptid);
1072 record_full_message (get_thread_regcache (inferior_thread ()), signal);
1074 if (!step)
1076 /* This is not hard single step. */
1077 if (!gdbarch_software_single_step_p (gdbarch))
1079 /* This is a normal continue. */
1080 step = 1;
1082 else
1084 /* This arch supports soft single step. */
1085 if (thread_has_single_step_breakpoints_set (inferior_thread ()))
1087 /* This is a soft single step. */
1088 record_full_resume_step = 1;
1090 else
1091 step = !insert_single_step_breakpoints (gdbarch);
1095 /* Make sure the target beneath reports all signals. */
1096 target_pass_signals ({});
1098 /* Disable range-stepping, forcing the process target to report stops for
1099 all executed instructions, so we can record them all. */
1100 process_stratum_target *proc_target
1101 = current_inferior ()->process_target ();
1102 for (thread_info *thread : all_non_exited_threads (proc_target, ptid))
1103 thread->control.may_range_step = 0;
1105 this->beneath ()->resume (ptid, step, signal);
1109 static int record_full_get_sig = 0;
1111 /* SIGINT signal handler, registered by "wait" method. */
1113 static void
1114 record_full_sig_handler (int signo)
1116 if (record_debug)
1117 gdb_printf (gdb_stdlog, "Process record: get a signal\n");
1119 /* It will break the running inferior in replay mode. */
1120 record_full_resume_step = 1;
1122 /* It will let record_full_wait set inferior status to get the signal
1123 SIGINT. */
1124 record_full_get_sig = 1;
1127 /* "wait" target method for process record target.
1129 In record mode, the target is always run in singlestep mode
1130 (even when gdb says to continue). The wait method intercepts
1131 the stop events and determines which ones are to be passed on to
1132 gdb. Most stop events are just singlestep events that gdb is not
1133 to know about, so the wait method just records them and keeps
1134 singlestepping.
1136 In replay mode, this function emulates the recorded execution log,
1137 one instruction at a time (forward or backward), and determines
1138 where to stop. */
1140 static ptid_t
1141 record_full_wait_1 (struct target_ops *ops,
1142 ptid_t ptid, struct target_waitstatus *status,
1143 target_wait_flags options)
1145 scoped_restore restore_operation_disable
1146 = record_full_gdb_operation_disable_set ();
1148 if (record_debug)
1149 gdb_printf (gdb_stdlog,
1150 "Process record: record_full_wait "
1151 "record_full_resume_step = %d, "
1152 "record_full_resumed = %d, direction=%s\n",
1153 record_full_resume_step, record_full_resumed,
1154 record_full_execution_dir == EXEC_FORWARD
1155 ? "forward" : "reverse");
1157 if (!record_full_resumed)
1159 gdb_assert ((options & TARGET_WNOHANG) != 0);
1161 /* No interesting event. */
1162 status->set_ignore ();
1163 return minus_one_ptid;
1166 record_full_get_sig = 0;
1167 signal (SIGINT, record_full_sig_handler);
1169 record_full_stop_reason = TARGET_STOPPED_BY_NO_REASON;
1171 if (!RECORD_FULL_IS_REPLAY && ops != &record_full_core_ops)
1173 if (record_full_resume_step)
1175 /* This is a single step. */
1176 return ops->beneath ()->wait (ptid, status, options);
1178 else
1180 /* This is not a single step. */
1181 ptid_t ret;
1182 CORE_ADDR tmp_pc;
1183 struct gdbarch *gdbarch
1184 = target_thread_architecture (record_full_resume_ptid);
1186 while (1)
1188 ret = ops->beneath ()->wait (ptid, status, options);
1189 if (status->kind () == TARGET_WAITKIND_IGNORE)
1191 if (record_debug)
1192 gdb_printf (gdb_stdlog,
1193 "Process record: record_full_wait "
1194 "target beneath not done yet\n");
1195 return ret;
1198 for (thread_info *tp : all_non_exited_threads ())
1199 delete_single_step_breakpoints (tp);
1201 if (record_full_resume_step)
1202 return ret;
1204 /* Is this a SIGTRAP? */
1205 if (status->kind () == TARGET_WAITKIND_STOPPED
1206 && status->sig () == GDB_SIGNAL_TRAP)
1208 struct regcache *regcache;
1209 enum target_stop_reason *stop_reason_p
1210 = &record_full_stop_reason;
1212 /* Yes -- this is likely our single-step finishing,
1213 but check if there's any reason the core would be
1214 interested in the event. */
1216 registers_changed ();
1217 switch_to_thread (current_inferior ()->process_target (),
1218 ret);
1219 regcache = get_thread_regcache (inferior_thread ());
1220 tmp_pc = regcache_read_pc (regcache);
1221 const address_space *aspace
1222 = current_inferior ()->aspace.get ();
1224 if (target_stopped_by_watchpoint ())
1226 /* Always interested in watchpoints. */
1228 else if (record_check_stopped_by_breakpoint (aspace, tmp_pc,
1229 stop_reason_p))
1231 /* There is a breakpoint here. Let the core
1232 handle it. */
1234 else
1236 /* This is a single-step trap. Record the
1237 insn and issue another step.
1238 FIXME: this part can be a random SIGTRAP too.
1239 But GDB cannot handle it. */
1240 int step = 1;
1242 if (!record_full_message_wrapper_safe (regcache,
1243 GDB_SIGNAL_0))
1245 status->set_stopped (GDB_SIGNAL_0);
1246 break;
1249 process_stratum_target *proc_target
1250 = current_inferior ()->process_target ();
1252 if (gdbarch_software_single_step_p (gdbarch))
1254 /* Try to insert the software single step breakpoint.
1255 If insert success, set step to 0. */
1256 set_executing (proc_target, inferior_ptid, false);
1257 SCOPE_EXIT
1259 set_executing (proc_target, inferior_ptid, true);
1262 reinit_frame_cache ();
1263 step = !insert_single_step_breakpoints (gdbarch);
1266 if (record_debug)
1267 gdb_printf (gdb_stdlog,
1268 "Process record: record_full_wait "
1269 "issuing one more step in the "
1270 "target beneath\n");
1271 ops->beneath ()->resume (ptid, step, GDB_SIGNAL_0);
1272 proc_target->commit_resumed_state = true;
1273 proc_target->commit_resumed ();
1274 proc_target->commit_resumed_state = false;
1275 continue;
1279 /* The inferior is broken by a breakpoint or a signal. */
1280 break;
1283 return ret;
1286 else
1288 switch_to_thread (current_inferior ()->process_target (),
1289 record_full_resume_ptid);
1290 regcache *regcache = get_thread_regcache (inferior_thread ());
1291 struct gdbarch *gdbarch = regcache->arch ();
1292 const address_space *aspace = current_inferior ()->aspace.get ();
1293 int continue_flag = 1;
1294 int first_record_full_end = 1;
1298 CORE_ADDR tmp_pc;
1300 record_full_stop_reason = TARGET_STOPPED_BY_NO_REASON;
1301 status->set_stopped (GDB_SIGNAL_0);
1303 /* Check breakpoint when forward execute. */
1304 if (execution_direction == EXEC_FORWARD)
1306 tmp_pc = regcache_read_pc (regcache);
1307 if (record_check_stopped_by_breakpoint (aspace, tmp_pc,
1308 &record_full_stop_reason))
1310 if (record_debug)
1311 gdb_printf (gdb_stdlog,
1312 "Process record: break at %s.\n",
1313 paddress (gdbarch, tmp_pc));
1314 goto replay_out;
1318 /* If GDB is in terminal_inferior mode, it will not get the
1319 signal. And in GDB replay mode, GDB doesn't need to be
1320 in terminal_inferior mode, because inferior will not
1321 executed. Then set it to terminal_ours to make GDB get
1322 the signal. */
1323 target_terminal::ours ();
1325 /* In EXEC_FORWARD mode, record_full_list points to the tail of prev
1326 instruction. */
1327 if (execution_direction == EXEC_FORWARD && record_full_list->next)
1328 record_full_list = record_full_list->next;
1330 /* Loop over the record_full_list, looking for the next place to
1331 stop. */
1334 /* Check for beginning and end of log. */
1335 if (execution_direction == EXEC_REVERSE
1336 && record_full_list == &record_full_first)
1338 /* Hit beginning of record log in reverse. */
1339 status->set_no_history ();
1340 break;
1342 if (execution_direction != EXEC_REVERSE
1343 && !record_full_list->next)
1345 /* Hit end of record log going forward. */
1346 status->set_no_history ();
1347 break;
1350 record_full_exec_insn (regcache, gdbarch, record_full_list);
1352 if (record_full_list->type == record_full_end)
1354 if (record_debug > 1)
1355 gdb_printf
1356 (gdb_stdlog,
1357 "Process record: record_full_end %s to "
1358 "inferior.\n",
1359 host_address_to_string (record_full_list));
1361 if (first_record_full_end
1362 && execution_direction == EXEC_REVERSE)
1364 /* When reverse execute, the first
1365 record_full_end is the part of current
1366 instruction. */
1367 first_record_full_end = 0;
1369 else
1371 /* In EXEC_REVERSE mode, this is the
1372 record_full_end of prev instruction. In
1373 EXEC_FORWARD mode, this is the
1374 record_full_end of current instruction. */
1375 /* step */
1376 if (record_full_resume_step)
1378 if (record_debug > 1)
1379 gdb_printf (gdb_stdlog,
1380 "Process record: step.\n");
1381 continue_flag = 0;
1384 /* check breakpoint */
1385 tmp_pc = regcache_read_pc (regcache);
1386 if (record_check_stopped_by_breakpoint
1387 (aspace, tmp_pc, &record_full_stop_reason))
1389 if (record_debug)
1390 gdb_printf (gdb_stdlog,
1391 "Process record: break "
1392 "at %s.\n",
1393 paddress (gdbarch, tmp_pc));
1395 continue_flag = 0;
1398 if (record_full_stop_reason
1399 == TARGET_STOPPED_BY_WATCHPOINT)
1401 if (record_debug)
1402 gdb_printf (gdb_stdlog,
1403 "Process record: hit hw "
1404 "watchpoint.\n");
1405 continue_flag = 0;
1407 /* Check target signal */
1408 if (record_full_list->u.end.sigval != GDB_SIGNAL_0)
1409 /* FIXME: better way to check */
1410 continue_flag = 0;
1414 if (continue_flag)
1416 if (execution_direction == EXEC_REVERSE)
1418 if (record_full_list->prev)
1419 record_full_list = record_full_list->prev;
1421 else
1423 if (record_full_list->next)
1424 record_full_list = record_full_list->next;
1428 while (continue_flag);
1430 replay_out:
1431 if (status->kind () == TARGET_WAITKIND_STOPPED)
1433 if (record_full_get_sig)
1434 status->set_stopped (GDB_SIGNAL_INT);
1435 else if (record_full_list->u.end.sigval != GDB_SIGNAL_0)
1436 /* FIXME: better way to check */
1437 status->set_stopped (record_full_list->u.end.sigval);
1438 else
1439 status->set_stopped (GDB_SIGNAL_TRAP);
1442 catch (const gdb_exception &ex)
1444 if (execution_direction == EXEC_REVERSE)
1446 if (record_full_list->next)
1447 record_full_list = record_full_list->next;
1449 else
1450 record_full_list = record_full_list->prev;
1452 throw;
1456 signal (SIGINT, handle_sigint);
1458 return inferior_ptid;
1461 ptid_t
1462 record_full_base_target::wait (ptid_t ptid, struct target_waitstatus *status,
1463 target_wait_flags options)
1465 ptid_t return_ptid;
1467 clear_async_event_handler (record_full_async_inferior_event_token);
1469 return_ptid = record_full_wait_1 (this, ptid, status, options);
1470 if (status->kind () != TARGET_WAITKIND_IGNORE)
1472 /* We're reporting a stop. Make sure any spurious
1473 target_wait(WNOHANG) doesn't advance the target until the
1474 core wants us resumed again. */
1475 record_full_resumed = 0;
1477 return return_ptid;
1480 bool
1481 record_full_base_target::stopped_by_watchpoint ()
1483 if (RECORD_FULL_IS_REPLAY)
1484 return record_full_stop_reason == TARGET_STOPPED_BY_WATCHPOINT;
1485 else
1486 return beneath ()->stopped_by_watchpoint ();
1489 bool
1490 record_full_base_target::stopped_data_address (CORE_ADDR *addr_p)
1492 if (RECORD_FULL_IS_REPLAY)
1493 return false;
1494 else
1495 return this->beneath ()->stopped_data_address (addr_p);
1498 /* The stopped_by_sw_breakpoint method of target record-full. */
1500 bool
1501 record_full_base_target::stopped_by_sw_breakpoint ()
1503 return record_full_stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT;
1506 /* The supports_stopped_by_sw_breakpoint method of target
1507 record-full. */
1509 bool
1510 record_full_base_target::supports_stopped_by_sw_breakpoint ()
1512 return true;
1515 /* The stopped_by_hw_breakpoint method of target record-full. */
1517 bool
1518 record_full_base_target::stopped_by_hw_breakpoint ()
1520 return record_full_stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT;
1523 /* The supports_stopped_by_sw_breakpoint method of target
1524 record-full. */
1526 bool
1527 record_full_base_target::supports_stopped_by_hw_breakpoint ()
1529 return true;
1532 /* Record registers change (by user or by GDB) to list as an instruction. */
1534 static void
1535 record_full_registers_change (struct regcache *regcache, int regnum)
1537 /* Check record_full_insn_num. */
1538 record_full_check_insn_num ();
1540 record_full_arch_list_head = NULL;
1541 record_full_arch_list_tail = NULL;
1543 if (regnum < 0)
1545 int i;
1547 for (i = 0; i < gdbarch_num_regs (regcache->arch ()); i++)
1549 if (record_full_arch_list_add_reg (regcache, i))
1551 record_full_list_release (record_full_arch_list_tail);
1552 error (_("Process record: failed to record execution log."));
1556 else
1558 if (record_full_arch_list_add_reg (regcache, regnum))
1560 record_full_list_release (record_full_arch_list_tail);
1561 error (_("Process record: failed to record execution log."));
1564 if (record_full_arch_list_add_end ())
1566 record_full_list_release (record_full_arch_list_tail);
1567 error (_("Process record: failed to record execution log."));
1569 record_full_list->next = record_full_arch_list_head;
1570 record_full_arch_list_head->prev = record_full_list;
1571 record_full_list = record_full_arch_list_tail;
1573 if (record_full_insn_num == record_full_insn_max_num)
1574 record_full_list_release_first ();
1575 else
1576 record_full_insn_num++;
1579 /* "store_registers" method for process record target. */
1581 void
1582 record_full_target::store_registers (struct regcache *regcache, int regno)
1584 if (!record_full_gdb_operation_disable)
1586 if (RECORD_FULL_IS_REPLAY)
1588 int n;
1590 /* Let user choose if he wants to write register or not. */
1591 if (regno < 0)
1593 query (_("Because GDB is in replay mode, changing the "
1594 "value of a register will make the execution "
1595 "log unusable from this point onward. "
1596 "Change all registers?"));
1597 else
1599 query (_("Because GDB is in replay mode, changing the value "
1600 "of a register will make the execution log unusable "
1601 "from this point onward. Change register %s?"),
1602 gdbarch_register_name (regcache->arch (),
1603 regno));
1605 if (!n)
1607 /* Invalidate the value of regcache that was set in function
1608 "regcache_raw_write". */
1609 if (regno < 0)
1611 int i;
1613 for (i = 0;
1614 i < gdbarch_num_regs (regcache->arch ());
1615 i++)
1616 regcache->invalidate (i);
1618 else
1619 regcache->invalidate (regno);
1621 error (_("Process record canceled the operation."));
1624 /* Destroy the record from here forward. */
1625 record_full_list_release_following (record_full_list);
1628 record_full_registers_change (regcache, regno);
1630 this->beneath ()->store_registers (regcache, regno);
1633 /* "xfer_partial" method. Behavior is conditional on
1634 RECORD_FULL_IS_REPLAY.
1635 In replay mode, we cannot write memory unles we are willing to
1636 invalidate the record/replay log from this point forward. */
1638 enum target_xfer_status
1639 record_full_target::xfer_partial (enum target_object object,
1640 const char *annex, gdb_byte *readbuf,
1641 const gdb_byte *writebuf, ULONGEST offset,
1642 ULONGEST len, ULONGEST *xfered_len)
1644 if (!record_full_gdb_operation_disable
1645 && (object == TARGET_OBJECT_MEMORY
1646 || object == TARGET_OBJECT_RAW_MEMORY) && writebuf)
1648 if (RECORD_FULL_IS_REPLAY)
1650 /* Let user choose if he wants to write memory or not. */
1651 if (!query (_("Because GDB is in replay mode, writing to memory "
1652 "will make the execution log unusable from this "
1653 "point onward. Write memory at address %s?"),
1654 paddress (current_inferior ()->arch (), offset)))
1655 error (_("Process record canceled the operation."));
1657 /* Destroy the record from here forward. */
1658 record_full_list_release_following (record_full_list);
1661 /* Check record_full_insn_num */
1662 record_full_check_insn_num ();
1664 /* Record registers change to list as an instruction. */
1665 record_full_arch_list_head = NULL;
1666 record_full_arch_list_tail = NULL;
1667 if (record_full_arch_list_add_mem (offset, len))
1669 record_full_list_release (record_full_arch_list_tail);
1670 if (record_debug)
1671 gdb_printf (gdb_stdlog,
1672 "Process record: failed to record "
1673 "execution log.");
1674 return TARGET_XFER_E_IO;
1676 if (record_full_arch_list_add_end ())
1678 record_full_list_release (record_full_arch_list_tail);
1679 if (record_debug)
1680 gdb_printf (gdb_stdlog,
1681 "Process record: failed to record "
1682 "execution log.");
1683 return TARGET_XFER_E_IO;
1685 record_full_list->next = record_full_arch_list_head;
1686 record_full_arch_list_head->prev = record_full_list;
1687 record_full_list = record_full_arch_list_tail;
1689 if (record_full_insn_num == record_full_insn_max_num)
1690 record_full_list_release_first ();
1691 else
1692 record_full_insn_num++;
1695 return this->beneath ()->xfer_partial (object, annex, readbuf, writebuf,
1696 offset, len, xfered_len);
1699 /* This structure represents a breakpoint inserted while the record
1700 target is active. We use this to know when to install/remove
1701 breakpoints in/from the target beneath. For example, a breakpoint
1702 may be inserted while recording, but removed when not replaying nor
1703 recording. In that case, the breakpoint had not been inserted on
1704 the target beneath, so we should not try to remove it there. */
1706 struct record_full_breakpoint
1708 record_full_breakpoint (struct address_space *address_space_,
1709 CORE_ADDR addr_,
1710 bool in_target_beneath_)
1711 : address_space (address_space_),
1712 addr (addr_),
1713 in_target_beneath (in_target_beneath_)
1717 /* The address and address space the breakpoint was set at. */
1718 struct address_space *address_space;
1719 CORE_ADDR addr;
1721 /* True when the breakpoint has been also installed in the target
1722 beneath. This will be false for breakpoints set during replay or
1723 when recording. */
1724 bool in_target_beneath;
1727 /* The list of breakpoints inserted while the record target is
1728 active. */
1729 static std::vector<record_full_breakpoint> record_full_breakpoints;
1731 /* Sync existing breakpoints to record_full_breakpoints. */
1733 static void
1734 record_full_init_record_breakpoints (void)
1736 record_full_breakpoints.clear ();
1738 for (bp_location *loc : all_bp_locations ())
1740 if (loc->loc_type != bp_loc_software_breakpoint)
1741 continue;
1743 if (loc->inserted)
1744 record_full_breakpoints.emplace_back
1745 (loc->target_info.placed_address_space,
1746 loc->target_info.placed_address, 1);
1750 /* Behavior is conditional on RECORD_FULL_IS_REPLAY. We will not actually
1751 insert or remove breakpoints in the real target when replaying, nor
1752 when recording. */
1755 record_full_target::insert_breakpoint (struct gdbarch *gdbarch,
1756 struct bp_target_info *bp_tgt)
1758 bool in_target_beneath = false;
1760 if (!RECORD_FULL_IS_REPLAY)
1762 /* When recording, we currently always single-step, so we don't
1763 really need to install regular breakpoints in the inferior.
1764 However, we do have to insert software single-step
1765 breakpoints, in case the target can't hardware step. To keep
1766 things simple, we always insert. */
1768 scoped_restore restore_operation_disable
1769 = record_full_gdb_operation_disable_set ();
1771 int ret = this->beneath ()->insert_breakpoint (gdbarch, bp_tgt);
1772 if (ret != 0)
1773 return ret;
1775 in_target_beneath = true;
1778 /* Use the existing entries if found in order to avoid duplication
1779 in record_full_breakpoints. */
1781 for (const record_full_breakpoint &bp : record_full_breakpoints)
1783 if (bp.addr == bp_tgt->placed_address
1784 && bp.address_space == bp_tgt->placed_address_space)
1786 gdb_assert (bp.in_target_beneath == in_target_beneath);
1787 return 0;
1791 record_full_breakpoints.emplace_back (bp_tgt->placed_address_space,
1792 bp_tgt->placed_address,
1793 in_target_beneath);
1794 return 0;
1797 /* "remove_breakpoint" method for process record target. */
1800 record_full_target::remove_breakpoint (struct gdbarch *gdbarch,
1801 struct bp_target_info *bp_tgt,
1802 enum remove_bp_reason reason)
1804 for (auto iter = record_full_breakpoints.begin ();
1805 iter != record_full_breakpoints.end ();
1806 ++iter)
1808 struct record_full_breakpoint &bp = *iter;
1810 if (bp.addr == bp_tgt->placed_address
1811 && bp.address_space == bp_tgt->placed_address_space)
1813 if (bp.in_target_beneath)
1815 scoped_restore restore_operation_disable
1816 = record_full_gdb_operation_disable_set ();
1818 int ret = this->beneath ()->remove_breakpoint (gdbarch, bp_tgt,
1819 reason);
1820 if (ret != 0)
1821 return ret;
1824 if (reason == REMOVE_BREAKPOINT)
1825 unordered_remove (record_full_breakpoints, iter);
1826 return 0;
1830 gdb_assert_not_reached ("removing unknown breakpoint");
1833 /* "can_execute_reverse" method for process record target. */
1835 bool
1836 record_full_base_target::can_execute_reverse ()
1838 return true;
1841 /* "get_bookmark" method for process record and prec over core. */
1843 gdb_byte *
1844 record_full_base_target::get_bookmark (const char *args, int from_tty)
1846 char *ret = NULL;
1848 /* Return stringified form of instruction count. */
1849 if (record_full_list && record_full_list->type == record_full_end)
1850 ret = xstrdup (pulongest (record_full_list->u.end.insn_num));
1852 if (record_debug)
1854 if (ret)
1855 gdb_printf (gdb_stdlog,
1856 "record_full_get_bookmark returns %s\n", ret);
1857 else
1858 gdb_printf (gdb_stdlog,
1859 "record_full_get_bookmark returns NULL\n");
1861 return (gdb_byte *) ret;
1864 /* "goto_bookmark" method for process record and prec over core. */
1866 void
1867 record_full_base_target::goto_bookmark (const gdb_byte *raw_bookmark,
1868 int from_tty)
1870 const char *bookmark = (const char *) raw_bookmark;
1872 if (record_debug)
1873 gdb_printf (gdb_stdlog,
1874 "record_full_goto_bookmark receives %s\n", bookmark);
1876 std::string name_holder;
1877 if (bookmark[0] == '\'' || bookmark[0] == '\"')
1879 if (bookmark[strlen (bookmark) - 1] != bookmark[0])
1880 error (_("Unbalanced quotes: %s"), bookmark);
1882 name_holder = std::string (bookmark + 1, strlen (bookmark) - 2);
1883 bookmark = name_holder.c_str ();
1886 record_goto (bookmark);
1889 enum exec_direction_kind
1890 record_full_base_target::execution_direction ()
1892 return record_full_execution_dir;
1895 /* The record_method method of target record-full. */
1897 enum record_method
1898 record_full_base_target::record_method (ptid_t ptid)
1900 return RECORD_METHOD_FULL;
1903 void
1904 record_full_base_target::info_record ()
1906 struct record_full_entry *p;
1908 if (RECORD_FULL_IS_REPLAY)
1909 gdb_printf (_("Replay mode:\n"));
1910 else
1911 gdb_printf (_("Record mode:\n"));
1913 /* Find entry for first actual instruction in the log. */
1914 for (p = record_full_first.next;
1915 p != NULL && p->type != record_full_end;
1916 p = p->next)
1919 /* Do we have a log at all? */
1920 if (p != NULL && p->type == record_full_end)
1922 /* Display instruction number for first instruction in the log. */
1923 gdb_printf (_("Lowest recorded instruction number is %s.\n"),
1924 pulongest (p->u.end.insn_num));
1926 /* If in replay mode, display where we are in the log. */
1927 if (RECORD_FULL_IS_REPLAY)
1928 gdb_printf (_("Current instruction number is %s.\n"),
1929 pulongest (record_full_list->u.end.insn_num));
1931 /* Display instruction number for last instruction in the log. */
1932 gdb_printf (_("Highest recorded instruction number is %s.\n"),
1933 pulongest (record_full_insn_count));
1935 /* Display log count. */
1936 gdb_printf (_("Log contains %u instructions.\n"),
1937 record_full_insn_num);
1939 else
1940 gdb_printf (_("No instructions have been logged.\n"));
1942 /* Display max log size. */
1943 gdb_printf (_("Max logged instructions is %u.\n"),
1944 record_full_insn_max_num);
1947 bool
1948 record_full_base_target::supports_delete_record ()
1950 return true;
1953 /* The "delete_record" target method. */
1955 void
1956 record_full_base_target::delete_record ()
1958 record_full_list_release_following (record_full_list);
1961 /* The "record_is_replaying" target method. */
1963 bool
1964 record_full_base_target::record_is_replaying (ptid_t ptid)
1966 return RECORD_FULL_IS_REPLAY;
1969 /* The "record_will_replay" target method. */
1971 bool
1972 record_full_base_target::record_will_replay (ptid_t ptid, int dir)
1974 /* We can currently only record when executing forwards. Should we be able
1975 to record when executing backwards on targets that support reverse
1976 execution, this needs to be changed. */
1978 return RECORD_FULL_IS_REPLAY || dir == EXEC_REVERSE;
1981 /* Go to a specific entry. */
1983 static void
1984 record_full_goto_entry (struct record_full_entry *p)
1986 if (p == NULL)
1987 error (_("Target insn not found."));
1988 else if (p == record_full_list)
1989 error (_("Already at target insn."));
1990 else if (p->u.end.insn_num > record_full_list->u.end.insn_num)
1992 gdb_printf (_("Go forward to insn number %s\n"),
1993 pulongest (p->u.end.insn_num));
1994 record_full_goto_insn (p, EXEC_FORWARD);
1996 else
1998 gdb_printf (_("Go backward to insn number %s\n"),
1999 pulongest (p->u.end.insn_num));
2000 record_full_goto_insn (p, EXEC_REVERSE);
2003 registers_changed ();
2004 reinit_frame_cache ();
2006 thread_info *thr = inferior_thread ();
2007 thr->set_stop_pc (regcache_read_pc (get_thread_regcache (thr)));
2008 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
2011 /* The "goto_record_begin" target method. */
2013 void
2014 record_full_base_target::goto_record_begin ()
2016 struct record_full_entry *p = NULL;
2018 for (p = &record_full_first; p != NULL; p = p->next)
2019 if (p->type == record_full_end)
2020 break;
2022 record_full_goto_entry (p);
2025 /* The "goto_record_end" target method. */
2027 void
2028 record_full_base_target::goto_record_end ()
2030 struct record_full_entry *p = NULL;
2032 for (p = record_full_list; p->next != NULL; p = p->next)
2034 for (; p!= NULL; p = p->prev)
2035 if (p->type == record_full_end)
2036 break;
2038 record_full_goto_entry (p);
2041 /* The "goto_record" target method. */
2043 void
2044 record_full_base_target::goto_record (ULONGEST target_insn)
2046 struct record_full_entry *p = NULL;
2048 for (p = &record_full_first; p != NULL; p = p->next)
2049 if (p->type == record_full_end && p->u.end.insn_num == target_insn)
2050 break;
2052 record_full_goto_entry (p);
2055 /* The "record_stop_replaying" target method. */
2057 void
2058 record_full_base_target::record_stop_replaying ()
2060 goto_record_end ();
2063 /* "resume" method for prec over corefile. */
2065 void
2066 record_full_core_target::resume (ptid_t ptid, int step,
2067 enum gdb_signal signal)
2069 record_full_resume_step = step;
2070 record_full_resumed = 1;
2071 record_full_execution_dir = ::execution_direction;
2074 /* "kill" method for prec over corefile. */
2076 void
2077 record_full_core_target::kill ()
2079 if (record_debug)
2080 gdb_printf (gdb_stdlog, "Process record: record_full_core_kill\n");
2082 current_inferior ()->unpush_target (this);
2085 /* "fetch_registers" method for prec over corefile. */
2087 void
2088 record_full_core_target::fetch_registers (struct regcache *regcache,
2089 int regno)
2091 if (regno < 0)
2093 int num = gdbarch_num_regs (regcache->arch ());
2094 int i;
2096 for (i = 0; i < num; i ++)
2097 regcache->raw_supply (i, *record_full_core_regbuf);
2099 else
2100 regcache->raw_supply (regno, *record_full_core_regbuf);
2103 /* "prepare_to_store" method for prec over corefile. */
2105 void
2106 record_full_core_target::prepare_to_store (struct regcache *regcache)
2110 /* "store_registers" method for prec over corefile. */
2112 void
2113 record_full_core_target::store_registers (struct regcache *regcache,
2114 int regno)
2116 if (record_full_gdb_operation_disable)
2117 record_full_core_regbuf->raw_supply (regno, *regcache);
2118 else
2119 error (_("You can't do that without a process to debug."));
2122 /* "xfer_partial" method for prec over corefile. */
2124 enum target_xfer_status
2125 record_full_core_target::xfer_partial (enum target_object object,
2126 const char *annex, gdb_byte *readbuf,
2127 const gdb_byte *writebuf, ULONGEST offset,
2128 ULONGEST len, ULONGEST *xfered_len)
2130 if (object == TARGET_OBJECT_MEMORY)
2132 if (record_full_gdb_operation_disable || !writebuf)
2134 for (target_section &p : record_full_core_sections)
2136 if (offset >= p.addr)
2138 struct record_full_core_buf_entry *entry;
2139 ULONGEST sec_offset;
2141 if (offset >= p.endaddr)
2142 continue;
2144 if (offset + len > p.endaddr)
2145 len = p.endaddr - offset;
2147 sec_offset = offset - p.addr;
2149 /* Read readbuf or write writebuf p, offset, len. */
2150 /* Check flags. */
2151 if (p.the_bfd_section->flags & SEC_CONSTRUCTOR
2152 || (p.the_bfd_section->flags & SEC_HAS_CONTENTS) == 0)
2154 if (readbuf)
2155 memset (readbuf, 0, len);
2157 *xfered_len = len;
2158 return TARGET_XFER_OK;
2160 /* Get record_full_core_buf_entry. */
2161 for (entry = record_full_core_buf_list; entry;
2162 entry = entry->prev)
2163 if (entry->p == &p)
2164 break;
2165 if (writebuf)
2167 if (!entry)
2169 /* Add a new entry. */
2170 entry = XNEW (struct record_full_core_buf_entry);
2171 entry->p = &p;
2172 if (!bfd_malloc_and_get_section
2173 (p.the_bfd_section->owner,
2174 p.the_bfd_section,
2175 &entry->buf))
2177 xfree (entry);
2178 return TARGET_XFER_EOF;
2180 entry->prev = record_full_core_buf_list;
2181 record_full_core_buf_list = entry;
2184 memcpy (entry->buf + sec_offset, writebuf,
2185 (size_t) len);
2187 else
2189 if (!entry)
2190 return this->beneath ()->xfer_partial (object, annex,
2191 readbuf, writebuf,
2192 offset, len,
2193 xfered_len);
2195 memcpy (readbuf, entry->buf + sec_offset,
2196 (size_t) len);
2199 *xfered_len = len;
2200 return TARGET_XFER_OK;
2204 return TARGET_XFER_E_IO;
2206 else
2207 error (_("You can't do that without a process to debug."));
2210 return this->beneath ()->xfer_partial (object, annex,
2211 readbuf, writebuf, offset, len,
2212 xfered_len);
2215 /* "insert_breakpoint" method for prec over corefile. */
2218 record_full_core_target::insert_breakpoint (struct gdbarch *gdbarch,
2219 struct bp_target_info *bp_tgt)
2221 return 0;
2224 /* "remove_breakpoint" method for prec over corefile. */
2227 record_full_core_target::remove_breakpoint (struct gdbarch *gdbarch,
2228 struct bp_target_info *bp_tgt,
2229 enum remove_bp_reason reason)
2231 return 0;
2234 /* "has_execution" method for prec over corefile. */
2236 bool
2237 record_full_core_target::has_execution (inferior *inf)
2239 return true;
2242 /* Record log save-file format
2243 Version 1 (never released)
2245 Header:
2246 4 bytes: magic number htonl(0x20090829).
2247 NOTE: be sure to change whenever this file format changes!
2249 Records:
2250 record_full_end:
2251 1 byte: record type (record_full_end, see enum record_full_type).
2252 record_full_reg:
2253 1 byte: record type (record_full_reg, see enum record_full_type).
2254 8 bytes: register id (network byte order).
2255 MAX_REGISTER_SIZE bytes: register value.
2256 record_full_mem:
2257 1 byte: record type (record_full_mem, see enum record_full_type).
2258 8 bytes: memory length (network byte order).
2259 8 bytes: memory address (network byte order).
2260 n bytes: memory value (n == memory length).
2262 Version 2
2263 4 bytes: magic number netorder32(0x20091016).
2264 NOTE: be sure to change whenever this file format changes!
2266 Records:
2267 record_full_end:
2268 1 byte: record type (record_full_end, see enum record_full_type).
2269 4 bytes: signal
2270 4 bytes: instruction count
2271 record_full_reg:
2272 1 byte: record type (record_full_reg, see enum record_full_type).
2273 4 bytes: register id (network byte order).
2274 n bytes: register value (n == actual register size).
2275 (eg. 4 bytes for x86 general registers).
2276 record_full_mem:
2277 1 byte: record type (record_full_mem, see enum record_full_type).
2278 4 bytes: memory length (network byte order).
2279 8 bytes: memory address (network byte order).
2280 n bytes: memory value (n == memory length).
2284 /* bfdcore_read -- read bytes from a core file section. */
2286 static inline void
2287 bfdcore_read (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2289 int ret = bfd_get_section_contents (obfd, osec, buf, *offset, len);
2291 if (ret)
2292 *offset += len;
2293 else
2294 error (_("Failed to read %d bytes from core file %s ('%s')."),
2295 len, bfd_get_filename (obfd),
2296 bfd_errmsg (bfd_get_error ()));
2299 static inline uint64_t
2300 netorder64 (uint64_t input)
2302 uint64_t ret;
2304 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2305 BFD_ENDIAN_BIG, input);
2306 return ret;
2309 static inline uint32_t
2310 netorder32 (uint32_t input)
2312 uint32_t ret;
2314 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2315 BFD_ENDIAN_BIG, input);
2316 return ret;
2319 /* Restore the execution log from a core_bfd file. */
2320 static void
2321 record_full_restore (void)
2323 uint32_t magic;
2324 struct record_full_entry *rec;
2325 asection *osec;
2326 uint32_t osec_size;
2327 int bfd_offset = 0;
2329 /* We restore the execution log from the open core bfd,
2330 if there is one. */
2331 if (core_bfd == NULL)
2332 return;
2334 /* "record_full_restore" can only be called when record list is empty. */
2335 gdb_assert (record_full_first.next == NULL);
2337 if (record_debug)
2338 gdb_printf (gdb_stdlog, "Restoring recording from core file.\n");
2340 /* Now need to find our special note section. */
2341 osec = bfd_get_section_by_name (core_bfd, "null0");
2342 if (record_debug)
2343 gdb_printf (gdb_stdlog, "Find precord section %s.\n",
2344 osec ? "succeeded" : "failed");
2345 if (osec == NULL)
2346 return;
2347 osec_size = bfd_section_size (osec);
2348 if (record_debug)
2349 gdb_printf (gdb_stdlog, "%s", bfd_section_name (osec));
2351 /* Check the magic code. */
2352 bfdcore_read (core_bfd, osec, &magic, sizeof (magic), &bfd_offset);
2353 if (magic != RECORD_FULL_FILE_MAGIC)
2354 error (_("Version mis-match or file format error in core file %s."),
2355 bfd_get_filename (core_bfd));
2356 if (record_debug)
2357 gdb_printf (gdb_stdlog,
2358 " Reading 4-byte magic cookie "
2359 "RECORD_FULL_FILE_MAGIC (0x%s)\n",
2360 phex_nz (netorder32 (magic), 4));
2362 /* Restore the entries in recfd into record_full_arch_list_head and
2363 record_full_arch_list_tail. */
2364 record_full_arch_list_head = NULL;
2365 record_full_arch_list_tail = NULL;
2366 record_full_insn_num = 0;
2370 regcache *regcache = get_thread_regcache (inferior_thread ());
2372 while (1)
2374 uint8_t rectype;
2375 uint32_t regnum, len, signal, count;
2376 uint64_t addr;
2378 /* We are finished when offset reaches osec_size. */
2379 if (bfd_offset >= osec_size)
2380 break;
2381 bfdcore_read (core_bfd, osec, &rectype, sizeof (rectype), &bfd_offset);
2383 switch (rectype)
2385 case record_full_reg: /* reg */
2386 /* Get register number to regnum. */
2387 bfdcore_read (core_bfd, osec, &regnum,
2388 sizeof (regnum), &bfd_offset);
2389 regnum = netorder32 (regnum);
2391 rec = record_full_reg_alloc (regcache, regnum);
2393 /* Get val. */
2394 bfdcore_read (core_bfd, osec, record_full_get_loc (rec),
2395 rec->u.reg.len, &bfd_offset);
2397 if (record_debug)
2398 gdb_printf (gdb_stdlog,
2399 " Reading register %d (1 "
2400 "plus %lu plus %d bytes)\n",
2401 rec->u.reg.num,
2402 (unsigned long) sizeof (regnum),
2403 rec->u.reg.len);
2404 break;
2406 case record_full_mem: /* mem */
2407 /* Get len. */
2408 bfdcore_read (core_bfd, osec, &len,
2409 sizeof (len), &bfd_offset);
2410 len = netorder32 (len);
2412 /* Get addr. */
2413 bfdcore_read (core_bfd, osec, &addr,
2414 sizeof (addr), &bfd_offset);
2415 addr = netorder64 (addr);
2417 rec = record_full_mem_alloc (addr, len);
2419 /* Get val. */
2420 bfdcore_read (core_bfd, osec, record_full_get_loc (rec),
2421 rec->u.mem.len, &bfd_offset);
2423 if (record_debug)
2424 gdb_printf (gdb_stdlog,
2425 " Reading memory %s (1 plus "
2426 "%lu plus %lu plus %d bytes)\n",
2427 paddress (get_current_arch (),
2428 rec->u.mem.addr),
2429 (unsigned long) sizeof (addr),
2430 (unsigned long) sizeof (len),
2431 rec->u.mem.len);
2432 break;
2434 case record_full_end: /* end */
2435 rec = record_full_end_alloc ();
2436 record_full_insn_num ++;
2438 /* Get signal value. */
2439 bfdcore_read (core_bfd, osec, &signal,
2440 sizeof (signal), &bfd_offset);
2441 signal = netorder32 (signal);
2442 rec->u.end.sigval = (enum gdb_signal) signal;
2444 /* Get insn count. */
2445 bfdcore_read (core_bfd, osec, &count,
2446 sizeof (count), &bfd_offset);
2447 count = netorder32 (count);
2448 rec->u.end.insn_num = count;
2449 record_full_insn_count = count + 1;
2450 if (record_debug)
2451 gdb_printf (gdb_stdlog,
2452 " Reading record_full_end (1 + "
2453 "%lu + %lu bytes), offset == %s\n",
2454 (unsigned long) sizeof (signal),
2455 (unsigned long) sizeof (count),
2456 paddress (get_current_arch (),
2457 bfd_offset));
2458 break;
2460 default:
2461 error (_("Bad entry type in core file %s."),
2462 bfd_get_filename (core_bfd));
2463 break;
2466 /* Add rec to record arch list. */
2467 record_full_arch_list_add (rec);
2470 catch (const gdb_exception &ex)
2472 record_full_list_release (record_full_arch_list_tail);
2473 throw;
2476 /* Add record_full_arch_list_head to the end of record list. */
2477 record_full_first.next = record_full_arch_list_head;
2478 record_full_arch_list_head->prev = &record_full_first;
2479 record_full_arch_list_tail->next = NULL;
2480 record_full_list = &record_full_first;
2482 /* Update record_full_insn_max_num. */
2483 if (record_full_insn_num > record_full_insn_max_num)
2485 record_full_insn_max_num = record_full_insn_num;
2486 warning (_("Auto increase record/replay buffer limit to %u."),
2487 record_full_insn_max_num);
2490 /* Succeeded. */
2491 gdb_printf (_("Restored records from core file %s.\n"),
2492 bfd_get_filename (core_bfd));
2494 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
2497 /* bfdcore_write -- write bytes into a core file section. */
2499 static inline void
2500 bfdcore_write (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2502 int ret = bfd_set_section_contents (obfd, osec, buf, *offset, len);
2504 if (ret)
2505 *offset += len;
2506 else
2507 error (_("Failed to write %d bytes to core file %s ('%s')."),
2508 len, bfd_get_filename (obfd),
2509 bfd_errmsg (bfd_get_error ()));
2512 /* Restore the execution log from a file. We use a modified elf
2513 corefile format, with an extra section for our data. */
2515 static void
2516 cmd_record_full_restore (const char *args, int from_tty)
2518 core_file_command (args, from_tty);
2519 record_full_open (args, from_tty);
2522 /* Save the execution log to a file. We use a modified elf corefile
2523 format, with an extra section for our data. */
2525 void
2526 record_full_base_target::save_record (const char *recfilename)
2528 struct record_full_entry *cur_record_full_list;
2529 uint32_t magic;
2530 struct gdbarch *gdbarch;
2531 int save_size = 0;
2532 asection *osec = NULL;
2533 int bfd_offset = 0;
2535 /* Open the save file. */
2536 if (record_debug)
2537 gdb_printf (gdb_stdlog, "Saving execution log to core file '%s'\n",
2538 recfilename);
2540 /* Open the output file. */
2541 gdb_bfd_ref_ptr obfd (create_gcore_bfd (recfilename));
2543 /* Arrange to remove the output file on failure. */
2544 gdb::unlinker unlink_file (recfilename);
2546 /* Save the current record entry to "cur_record_full_list". */
2547 cur_record_full_list = record_full_list;
2549 /* Get the values of regcache and gdbarch. */
2550 regcache *regcache = get_thread_regcache (inferior_thread ());
2551 gdbarch = regcache->arch ();
2553 /* Disable the GDB operation record. */
2554 scoped_restore restore_operation_disable
2555 = record_full_gdb_operation_disable_set ();
2557 /* Reverse execute to the begin of record list. */
2558 while (1)
2560 /* Check for beginning and end of log. */
2561 if (record_full_list == &record_full_first)
2562 break;
2564 record_full_exec_insn (regcache, gdbarch, record_full_list);
2566 if (record_full_list->prev)
2567 record_full_list = record_full_list->prev;
2570 /* Compute the size needed for the extra bfd section. */
2571 save_size = 4; /* magic cookie */
2572 for (record_full_list = record_full_first.next; record_full_list;
2573 record_full_list = record_full_list->next)
2574 switch (record_full_list->type)
2576 case record_full_end:
2577 save_size += 1 + 4 + 4;
2578 break;
2579 case record_full_reg:
2580 save_size += 1 + 4 + record_full_list->u.reg.len;
2581 break;
2582 case record_full_mem:
2583 save_size += 1 + 4 + 8 + record_full_list->u.mem.len;
2584 break;
2587 /* Make the new bfd section. */
2588 osec = bfd_make_section_anyway_with_flags (obfd.get (), "precord",
2589 SEC_HAS_CONTENTS
2590 | SEC_READONLY);
2591 if (osec == NULL)
2592 error (_("Failed to create 'precord' section for corefile %s: %s"),
2593 recfilename,
2594 bfd_errmsg (bfd_get_error ()));
2595 bfd_set_section_size (osec, save_size);
2596 bfd_set_section_vma (osec, 0);
2597 bfd_set_section_alignment (osec, 0);
2599 /* Save corefile state. */
2600 write_gcore_file (obfd.get ());
2602 /* Write out the record log. */
2603 /* Write the magic code. */
2604 magic = RECORD_FULL_FILE_MAGIC;
2605 if (record_debug)
2606 gdb_printf (gdb_stdlog,
2607 " Writing 4-byte magic cookie "
2608 "RECORD_FULL_FILE_MAGIC (0x%s)\n",
2609 phex_nz (magic, 4));
2610 bfdcore_write (obfd.get (), osec, &magic, sizeof (magic), &bfd_offset);
2612 /* Save the entries to recfd and forward execute to the end of
2613 record list. */
2614 record_full_list = &record_full_first;
2615 while (1)
2617 /* Save entry. */
2618 if (record_full_list != &record_full_first)
2620 uint8_t type;
2621 uint32_t regnum, len, signal, count;
2622 uint64_t addr;
2624 type = record_full_list->type;
2625 bfdcore_write (obfd.get (), osec, &type, sizeof (type), &bfd_offset);
2627 switch (record_full_list->type)
2629 case record_full_reg: /* reg */
2630 if (record_debug)
2631 gdb_printf (gdb_stdlog,
2632 " Writing register %d (1 "
2633 "plus %lu plus %d bytes)\n",
2634 record_full_list->u.reg.num,
2635 (unsigned long) sizeof (regnum),
2636 record_full_list->u.reg.len);
2638 /* Write regnum. */
2639 regnum = netorder32 (record_full_list->u.reg.num);
2640 bfdcore_write (obfd.get (), osec, &regnum,
2641 sizeof (regnum), &bfd_offset);
2643 /* Write regval. */
2644 bfdcore_write (obfd.get (), osec,
2645 record_full_get_loc (record_full_list),
2646 record_full_list->u.reg.len, &bfd_offset);
2647 break;
2649 case record_full_mem: /* mem */
2650 if (record_debug)
2651 gdb_printf (gdb_stdlog,
2652 " Writing memory %s (1 plus "
2653 "%lu plus %lu plus %d bytes)\n",
2654 paddress (gdbarch,
2655 record_full_list->u.mem.addr),
2656 (unsigned long) sizeof (addr),
2657 (unsigned long) sizeof (len),
2658 record_full_list->u.mem.len);
2660 /* Write memlen. */
2661 len = netorder32 (record_full_list->u.mem.len);
2662 bfdcore_write (obfd.get (), osec, &len, sizeof (len),
2663 &bfd_offset);
2665 /* Write memaddr. */
2666 addr = netorder64 (record_full_list->u.mem.addr);
2667 bfdcore_write (obfd.get (), osec, &addr,
2668 sizeof (addr), &bfd_offset);
2670 /* Write memval. */
2671 bfdcore_write (obfd.get (), osec,
2672 record_full_get_loc (record_full_list),
2673 record_full_list->u.mem.len, &bfd_offset);
2674 break;
2676 case record_full_end:
2677 if (record_debug)
2678 gdb_printf (gdb_stdlog,
2679 " Writing record_full_end (1 + "
2680 "%lu + %lu bytes)\n",
2681 (unsigned long) sizeof (signal),
2682 (unsigned long) sizeof (count));
2683 /* Write signal value. */
2684 signal = netorder32 (record_full_list->u.end.sigval);
2685 bfdcore_write (obfd.get (), osec, &signal,
2686 sizeof (signal), &bfd_offset);
2688 /* Write insn count. */
2689 count = netorder32 (record_full_list->u.end.insn_num);
2690 bfdcore_write (obfd.get (), osec, &count,
2691 sizeof (count), &bfd_offset);
2692 break;
2696 /* Execute entry. */
2697 record_full_exec_insn (regcache, gdbarch, record_full_list);
2699 if (record_full_list->next)
2700 record_full_list = record_full_list->next;
2701 else
2702 break;
2705 /* Reverse execute to cur_record_full_list. */
2706 while (1)
2708 /* Check for beginning and end of log. */
2709 if (record_full_list == cur_record_full_list)
2710 break;
2712 record_full_exec_insn (regcache, gdbarch, record_full_list);
2714 if (record_full_list->prev)
2715 record_full_list = record_full_list->prev;
2718 unlink_file.keep ();
2720 /* Succeeded. */
2721 gdb_printf (_("Saved core file %s with execution log.\n"),
2722 recfilename);
2725 /* record_full_goto_insn -- rewind the record log (forward or backward,
2726 depending on DIR) to the given entry, changing the program state
2727 correspondingly. */
2729 static void
2730 record_full_goto_insn (struct record_full_entry *entry,
2731 enum exec_direction_kind dir)
2733 scoped_restore restore_operation_disable
2734 = record_full_gdb_operation_disable_set ();
2735 regcache *regcache = get_thread_regcache (inferior_thread ());
2736 struct gdbarch *gdbarch = regcache->arch ();
2738 /* Assume everything is valid: we will hit the entry,
2739 and we will not hit the end of the recording. */
2741 if (dir == EXEC_FORWARD)
2742 record_full_list = record_full_list->next;
2746 record_full_exec_insn (regcache, gdbarch, record_full_list);
2747 if (dir == EXEC_REVERSE)
2748 record_full_list = record_full_list->prev;
2749 else
2750 record_full_list = record_full_list->next;
2751 } while (record_full_list != entry);
2754 /* Alias for "target record-full". */
2756 static void
2757 cmd_record_full_start (const char *args, int from_tty)
2759 execute_command ("target record-full", from_tty);
2762 static void
2763 set_record_full_insn_max_num (const char *args, int from_tty,
2764 struct cmd_list_element *c)
2766 if (record_full_insn_num > record_full_insn_max_num)
2768 /* Count down record_full_insn_num while releasing records from list. */
2769 while (record_full_insn_num > record_full_insn_max_num)
2771 record_full_list_release_first ();
2772 record_full_insn_num--;
2777 /* Implement the 'maintenance print record-instruction' command. */
2779 static void
2780 maintenance_print_record_instruction (const char *args, int from_tty)
2782 struct record_full_entry *to_print = record_full_list;
2784 if (args != nullptr)
2786 int offset = value_as_long (parse_and_eval (args));
2787 if (offset > 0)
2789 /* Move forward OFFSET instructions. We know we found the
2790 end of an instruction when to_print->type is record_full_end. */
2791 while (to_print->next != nullptr && offset > 0)
2793 to_print = to_print->next;
2794 if (to_print->type == record_full_end)
2795 offset--;
2797 if (offset != 0)
2798 error (_("Not enough recorded history"));
2800 else
2802 while (to_print->prev != nullptr && offset < 0)
2804 to_print = to_print->prev;
2805 if (to_print->type == record_full_end)
2806 offset++;
2808 if (offset != 0)
2809 error (_("Not enough recorded history"));
2812 gdb_assert (to_print != nullptr);
2814 gdbarch *arch = current_inferior ()->arch ();
2816 /* Go back to the start of the instruction. */
2817 while (to_print->prev != nullptr && to_print->prev->type != record_full_end)
2818 to_print = to_print->prev;
2820 /* if we're in the first record, there are no actual instructions
2821 recorded. Warn the user and leave. */
2822 if (to_print == &record_full_first)
2823 error (_("Not enough recorded history"));
2825 while (to_print->type != record_full_end)
2827 switch (to_print->type)
2829 case record_full_reg:
2831 type *regtype = gdbarch_register_type (arch, to_print->u.reg.num);
2832 value *val
2833 = value_from_contents (regtype,
2834 record_full_get_loc (to_print));
2835 gdb_printf ("Register %s changed: ",
2836 gdbarch_register_name (arch, to_print->u.reg.num));
2837 struct value_print_options opts;
2838 get_user_print_options (&opts);
2839 opts.raw = true;
2840 value_print (val, gdb_stdout, &opts);
2841 gdb_printf ("\n");
2842 break;
2844 case record_full_mem:
2846 gdb_byte *b = record_full_get_loc (to_print);
2847 gdb_printf ("%d bytes of memory at address %s changed from:",
2848 to_print->u.mem.len,
2849 print_core_address (arch, to_print->u.mem.addr));
2850 for (int i = 0; i < to_print->u.mem.len; i++)
2851 gdb_printf (" %02x", b[i]);
2852 gdb_printf ("\n");
2853 break;
2856 to_print = to_print->next;
2860 void _initialize_record_full ();
2861 void
2862 _initialize_record_full ()
2864 struct cmd_list_element *c;
2866 /* Init record_full_first. */
2867 record_full_first.prev = NULL;
2868 record_full_first.next = NULL;
2869 record_full_first.type = record_full_end;
2871 add_target (record_full_target_info, record_full_open);
2872 add_deprecated_target_alias (record_full_target_info, "record");
2873 add_target (record_full_core_target_info, record_full_open);
2875 add_prefix_cmd ("full", class_obscure, cmd_record_full_start,
2876 _("Start full execution recording."), &record_full_cmdlist,
2877 0, &record_cmdlist);
2879 cmd_list_element *record_full_restore_cmd
2880 = add_cmd ("restore", class_obscure, cmd_record_full_restore,
2881 _("Restore the execution log from a file.\n\
2882 Argument is filename. File must be created with 'record save'."),
2883 &record_full_cmdlist);
2884 set_cmd_completer (record_full_restore_cmd, filename_completer);
2886 /* Deprecate the old version without "full" prefix. */
2887 c = add_alias_cmd ("restore", record_full_restore_cmd, class_obscure, 1,
2888 &record_cmdlist);
2889 set_cmd_completer (c, filename_completer);
2890 deprecate_cmd (c, "record full restore");
2892 add_setshow_prefix_cmd ("full", class_support,
2893 _("Set record options."),
2894 _("Show record options."),
2895 &set_record_full_cmdlist,
2896 &show_record_full_cmdlist,
2897 &set_record_cmdlist,
2898 &show_record_cmdlist);
2900 /* Record instructions number limit command. */
2901 set_show_commands set_record_full_stop_at_limit_cmds
2902 = add_setshow_boolean_cmd ("stop-at-limit", no_class,
2903 &record_full_stop_at_limit, _("\
2904 Set whether record/replay stops when record/replay buffer becomes full."), _("\
2905 Show whether record/replay stops when record/replay buffer becomes full."),
2906 _("Default is ON.\n\
2907 When ON, if the record/replay buffer becomes full, ask user what to do.\n\
2908 When OFF, if the record/replay buffer becomes full,\n\
2909 delete the oldest recorded instruction to make room for each new one."),
2910 NULL, NULL,
2911 &set_record_full_cmdlist,
2912 &show_record_full_cmdlist);
2914 c = add_alias_cmd ("stop-at-limit",
2915 set_record_full_stop_at_limit_cmds.set, no_class, 1,
2916 &set_record_cmdlist);
2917 deprecate_cmd (c, "set record full stop-at-limit");
2919 c = add_alias_cmd ("stop-at-limit",
2920 set_record_full_stop_at_limit_cmds.show, no_class, 1,
2921 &show_record_cmdlist);
2922 deprecate_cmd (c, "show record full stop-at-limit");
2924 set_show_commands record_full_insn_number_max_cmds
2925 = add_setshow_uinteger_cmd ("insn-number-max", no_class,
2926 &record_full_insn_max_num,
2927 _("Set record/replay buffer limit."),
2928 _("Show record/replay buffer limit."), _("\
2929 Set the maximum number of instructions to be stored in the\n\
2930 record/replay buffer. A value of either \"unlimited\" or zero means no\n\
2931 limit. Default is 200000."),
2932 set_record_full_insn_max_num,
2933 NULL, &set_record_full_cmdlist,
2934 &show_record_full_cmdlist);
2936 c = add_alias_cmd ("insn-number-max", record_full_insn_number_max_cmds.set,
2937 no_class, 1, &set_record_cmdlist);
2938 deprecate_cmd (c, "set record full insn-number-max");
2940 c = add_alias_cmd ("insn-number-max", record_full_insn_number_max_cmds.show,
2941 no_class, 1, &show_record_cmdlist);
2942 deprecate_cmd (c, "show record full insn-number-max");
2944 set_show_commands record_full_memory_query_cmds
2945 = add_setshow_boolean_cmd ("memory-query", no_class,
2946 &record_full_memory_query, _("\
2947 Set whether query if PREC cannot record memory change of next instruction."),
2948 _("\
2949 Show whether query if PREC cannot record memory change of next instruction."),
2950 _("\
2951 Default is OFF.\n\
2952 When ON, query if PREC cannot record memory change of next instruction."),
2953 NULL, NULL,
2954 &set_record_full_cmdlist,
2955 &show_record_full_cmdlist);
2957 c = add_alias_cmd ("memory-query", record_full_memory_query_cmds.set,
2958 no_class, 1, &set_record_cmdlist);
2959 deprecate_cmd (c, "set record full memory-query");
2961 c = add_alias_cmd ("memory-query", record_full_memory_query_cmds.show,
2962 no_class, 1,&show_record_cmdlist);
2963 deprecate_cmd (c, "show record full memory-query");
2965 add_cmd ("record-instruction", class_maintenance,
2966 maintenance_print_record_instruction,
2967 _("\
2968 Print a recorded instruction.\n\
2969 If no argument is provided, print the last instruction recorded.\n\
2970 If a negative argument is given, prints how the nth previous \
2971 instruction will be undone.\n\
2972 If a positive argument is given, prints \
2973 how the nth following instruction will be redone."), &maintenanceprintlist);