More updated translations
[binutils-gdb.git] / gdb / record-full.c
blob6d056514e8b1622bbddcbadb6a2c0d02beb3a821
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 "gdbsupport/gdb_vecs.h"
24 #include "regcache.h"
25 #include "gdbthread.h"
26 #include "inferior.h"
27 #include "event-top.h"
28 #include "completer.h"
29 #include "arch-utils.h"
30 #include "gdbcore.h"
31 #include "exec.h"
32 #include "record.h"
33 #include "record-full.h"
34 #include "elf-bfd.h"
35 #include "gcore.h"
36 #include "gdbsupport/event-loop.h"
37 #include "inf-loop.h"
38 #include "gdb_bfd.h"
39 #include "observable.h"
40 #include "infrun.h"
41 #include "gdbsupport/gdb_unlinker.h"
42 #include "gdbsupport/byte-vector.h"
43 #include "gdbsupport/scoped_signal_handler.h"
44 #include "async-event.h"
45 #include "top.h"
46 #include "valprint.h"
47 #include "interps.h"
49 #include <signal.h>
51 /* This module implements "target record-full", also known as "process
52 record and replay". This target sits on top of a "normal" target
53 (a target that "has execution"), and provides a record and replay
54 functionality, including reverse debugging.
56 Target record has two modes: recording, and replaying.
58 In record mode, we intercept the resume and wait methods.
59 Whenever gdb resumes the target, we run the target in single step
60 mode, and we build up an execution log in which, for each executed
61 instruction, we record all changes in memory and register state.
62 This is invisible to the user, to whom it just looks like an
63 ordinary debugging session (except for performance degradation).
65 In replay mode, instead of actually letting the inferior run as a
66 process, we simulate its execution by playing back the recorded
67 execution log. For each instruction in the log, we simulate the
68 instruction's side effects by duplicating the changes that it would
69 have made on memory and registers. */
71 #define DEFAULT_RECORD_FULL_INSN_MAX_NUM 200000
73 #define RECORD_FULL_IS_REPLAY \
74 (record_full_list->next || ::execution_direction == EXEC_REVERSE)
76 #define RECORD_FULL_FILE_MAGIC netorder32(0x20091016)
78 /* These are the core structs of the process record functionality.
80 A record_full_entry is a record of the value change of a register
81 ("record_full_reg") or a part of memory ("record_full_mem"). And each
82 instruction must have a struct record_full_entry ("record_full_end")
83 that indicates that this is the last struct record_full_entry of this
84 instruction.
86 Each struct record_full_entry is linked to "record_full_list" by "prev"
87 and "next" pointers. */
89 struct record_full_mem_entry
91 CORE_ADDR addr;
92 int len;
93 /* Set this flag if target memory for this entry
94 can no longer be accessed. */
95 int mem_entry_not_accessible;
96 union
98 gdb_byte *ptr;
99 gdb_byte buf[sizeof (gdb_byte *)];
100 } u;
103 struct record_full_reg_entry
105 unsigned short num;
106 unsigned short len;
107 union
109 gdb_byte *ptr;
110 gdb_byte buf[2 * sizeof (gdb_byte *)];
111 } u;
114 struct record_full_end_entry
116 enum gdb_signal sigval;
117 ULONGEST insn_num;
120 enum record_full_type
122 record_full_end = 0,
123 record_full_reg,
124 record_full_mem
127 /* This is the data structure that makes up the execution log.
129 The execution log consists of a single linked list of entries
130 of type "struct record_full_entry". It is doubly linked so that it
131 can be traversed in either direction.
133 The start of the list is anchored by a struct called
134 "record_full_first". The pointer "record_full_list" either points
135 to the last entry that was added to the list (in record mode), or to
136 the next entry in the list that will be executed (in replay mode).
138 Each list element (struct record_full_entry), in addition to next
139 and prev pointers, consists of a union of three entry types: mem,
140 reg, and end. A field called "type" determines which entry type is
141 represented by a given list element.
143 Each instruction that is added to the execution log is represented
144 by a variable number of list elements ('entries'). The instruction
145 will have one "reg" entry for each register that is changed by
146 executing the instruction (including the PC in every case). It
147 will also have one "mem" entry for each memory change. Finally,
148 each instruction will have an "end" entry that separates it from
149 the changes associated with the next instruction. */
151 struct record_full_entry
153 struct record_full_entry *prev;
154 struct record_full_entry *next;
155 enum record_full_type type;
156 union
158 /* reg */
159 struct record_full_reg_entry reg;
160 /* mem */
161 struct record_full_mem_entry mem;
162 /* end */
163 struct record_full_end_entry end;
164 } u;
167 /* If true, query if PREC cannot record memory
168 change of next instruction. */
169 bool record_full_memory_query = false;
171 struct record_full_core_buf_entry
173 struct record_full_core_buf_entry *prev;
174 struct target_section *p;
175 bfd_byte *buf;
178 /* Record buf with core target. */
179 static detached_regcache *record_full_core_regbuf = NULL;
180 static std::vector<target_section> record_full_core_sections;
181 static struct record_full_core_buf_entry *record_full_core_buf_list = NULL;
183 /* The following variables are used for managing the linked list that
184 represents the execution log.
186 record_full_first is the anchor that holds down the beginning of
187 the list.
189 record_full_list serves two functions:
190 1) In record mode, it anchors the end of the list.
191 2) In replay mode, it traverses the list and points to
192 the next instruction that must be emulated.
194 record_full_arch_list_head and record_full_arch_list_tail are used
195 to manage a separate list, which is used to build up the change
196 elements of the currently executing instruction during record mode.
197 When this instruction has been completely annotated in the "arch
198 list", it will be appended to the main execution log. */
200 static struct record_full_entry record_full_first;
201 static struct record_full_entry *record_full_list = &record_full_first;
202 static struct record_full_entry *record_full_arch_list_head = NULL;
203 static struct record_full_entry *record_full_arch_list_tail = NULL;
205 /* true ask user. false auto delete the last struct record_full_entry. */
206 static bool record_full_stop_at_limit = true;
207 /* Maximum allowed number of insns in execution log. */
208 static unsigned int record_full_insn_max_num
209 = DEFAULT_RECORD_FULL_INSN_MAX_NUM;
210 /* Actual count of insns presently in execution log. */
211 static unsigned int record_full_insn_num = 0;
212 /* Count of insns logged so far (may be larger
213 than count of insns presently in execution log). */
214 static ULONGEST record_full_insn_count;
216 static const char record_longname[]
217 = N_("Process record and replay target");
218 static const char record_doc[]
219 = N_("Log program while executing and replay execution from log.");
221 /* Base class implementing functionality common to both the
222 "record-full" and "record-core" targets. */
224 class record_full_base_target : public target_ops
226 public:
227 const target_info &info () const override = 0;
229 strata stratum () const override { return record_stratum; }
231 void close () override;
232 void async (bool) override;
233 ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override;
234 bool stopped_by_watchpoint () override;
235 bool stopped_data_address (CORE_ADDR *) override;
237 bool stopped_by_sw_breakpoint () override;
238 bool supports_stopped_by_sw_breakpoint () override;
240 bool stopped_by_hw_breakpoint () override;
241 bool supports_stopped_by_hw_breakpoint () override;
243 bool can_execute_reverse () override;
245 /* Add bookmark target methods. */
246 gdb_byte *get_bookmark (const char *, int) override;
247 void goto_bookmark (const gdb_byte *, int) override;
248 enum exec_direction_kind execution_direction () override;
249 enum record_method record_method (ptid_t ptid) override;
250 void info_record () override;
251 void save_record (const char *filename) override;
252 bool supports_delete_record () override;
253 void delete_record () override;
254 bool record_is_replaying (ptid_t ptid) override;
255 bool record_will_replay (ptid_t ptid, int dir) override;
256 void record_stop_replaying () override;
257 void goto_record_begin () override;
258 void goto_record_end () override;
259 void goto_record (ULONGEST insn) override;
262 /* The "record-full" target. */
264 static const target_info record_full_target_info = {
265 "record-full",
266 record_longname,
267 record_doc,
270 class record_full_target final : public record_full_base_target
272 public:
273 const target_info &info () const override
274 { return record_full_target_info; }
276 void resume (ptid_t, int, enum gdb_signal) override;
277 void disconnect (const char *, int) override;
278 void detach (inferior *, int) override;
279 void mourn_inferior () override;
280 void kill () override;
281 void store_registers (struct regcache *, int) override;
282 enum target_xfer_status xfer_partial (enum target_object object,
283 const char *annex,
284 gdb_byte *readbuf,
285 const gdb_byte *writebuf,
286 ULONGEST offset, ULONGEST len,
287 ULONGEST *xfered_len) override;
288 int insert_breakpoint (struct gdbarch *,
289 struct bp_target_info *) override;
290 int remove_breakpoint (struct gdbarch *,
291 struct bp_target_info *,
292 enum remove_bp_reason) override;
295 /* The "record-core" target. */
297 static const target_info record_full_core_target_info = {
298 "record-core",
299 record_longname,
300 record_doc,
303 class record_full_core_target final : public record_full_base_target
305 public:
306 const target_info &info () const override
307 { return record_full_core_target_info; }
309 void resume (ptid_t, int, enum gdb_signal) override;
310 void disconnect (const char *, int) override;
311 void kill () override;
312 void fetch_registers (struct regcache *regcache, int regno) override;
313 void prepare_to_store (struct regcache *regcache) override;
314 void store_registers (struct regcache *, int) override;
315 enum target_xfer_status xfer_partial (enum target_object object,
316 const char *annex,
317 gdb_byte *readbuf,
318 const gdb_byte *writebuf,
319 ULONGEST offset, ULONGEST len,
320 ULONGEST *xfered_len) override;
321 int insert_breakpoint (struct gdbarch *,
322 struct bp_target_info *) override;
323 int remove_breakpoint (struct gdbarch *,
324 struct bp_target_info *,
325 enum remove_bp_reason) override;
327 bool has_execution (inferior *inf) override;
330 static record_full_target record_full_ops;
331 static record_full_core_target record_full_core_ops;
333 void
334 record_full_target::detach (inferior *inf, int from_tty)
336 record_detach (this, inf, from_tty);
339 void
340 record_full_target::disconnect (const char *args, int from_tty)
342 record_disconnect (this, args, from_tty);
345 void
346 record_full_core_target::disconnect (const char *args, int from_tty)
348 record_disconnect (this, args, from_tty);
351 void
352 record_full_target::mourn_inferior ()
354 record_mourn_inferior (this);
357 void
358 record_full_target::kill ()
360 record_kill (this);
363 /* See record-full.h. */
366 record_full_is_used (void)
368 struct target_ops *t;
370 t = find_record_target ();
371 return (t == &record_full_ops
372 || t == &record_full_core_ops);
375 /* see record-full.h. */
376 bool
377 record_full_is_replaying ()
379 auto target = dynamic_cast<record_full_target *>
380 (current_inferior ()->target_at (record_stratum));
381 return target != nullptr && RECORD_FULL_IS_REPLAY;
385 /* Command lists for "set/show record full". */
386 static struct cmd_list_element *set_record_full_cmdlist;
387 static struct cmd_list_element *show_record_full_cmdlist;
389 /* Command list for "record full". */
390 static struct cmd_list_element *record_full_cmdlist;
392 static void record_full_goto_insn (struct record_full_entry *entry,
393 enum exec_direction_kind dir);
395 /* Alloc and free functions for record_full_reg, record_full_mem, and
396 record_full_end entries. */
398 /* Alloc a record_full_reg record entry. */
400 static inline struct record_full_entry *
401 record_full_reg_alloc (struct regcache *regcache, int regnum)
403 struct record_full_entry *rec;
404 struct gdbarch *gdbarch = regcache->arch ();
406 rec = XCNEW (struct record_full_entry);
407 rec->type = record_full_reg;
408 rec->u.reg.num = regnum;
409 rec->u.reg.len = register_size (gdbarch, regnum);
410 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
411 rec->u.reg.u.ptr = (gdb_byte *) xmalloc (rec->u.reg.len);
413 return rec;
416 /* Free a record_full_reg record entry. */
418 static inline void
419 record_full_reg_release (struct record_full_entry *rec)
421 gdb_assert (rec->type == record_full_reg);
422 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
423 xfree (rec->u.reg.u.ptr);
424 xfree (rec);
427 /* Alloc a record_full_mem record entry. */
429 static inline struct record_full_entry *
430 record_full_mem_alloc (CORE_ADDR addr, int len)
432 struct record_full_entry *rec;
434 rec = XCNEW (struct record_full_entry);
435 rec->type = record_full_mem;
436 rec->u.mem.addr = addr;
437 rec->u.mem.len = len;
438 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
439 rec->u.mem.u.ptr = (gdb_byte *) xmalloc (len);
441 return rec;
444 /* Free a record_full_mem record entry. */
446 static inline void
447 record_full_mem_release (struct record_full_entry *rec)
449 gdb_assert (rec->type == record_full_mem);
450 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
451 xfree (rec->u.mem.u.ptr);
452 xfree (rec);
455 /* Alloc a record_full_end record entry. */
457 static inline struct record_full_entry *
458 record_full_end_alloc (void)
460 struct record_full_entry *rec;
462 rec = XCNEW (struct record_full_entry);
463 rec->type = record_full_end;
465 return rec;
468 /* Free a record_full_end record entry. */
470 static inline void
471 record_full_end_release (struct record_full_entry *rec)
473 xfree (rec);
476 /* Free one record entry, any type.
477 Return entry->type, in case caller wants to know. */
479 static inline enum record_full_type
480 record_full_entry_release (struct record_full_entry *rec)
482 enum record_full_type type = rec->type;
484 switch (type) {
485 case record_full_reg:
486 record_full_reg_release (rec);
487 break;
488 case record_full_mem:
489 record_full_mem_release (rec);
490 break;
491 case record_full_end:
492 record_full_end_release (rec);
493 break;
495 return type;
498 /* Free all record entries in list pointed to by REC. */
500 static void
501 record_full_list_release (struct record_full_entry *rec)
503 if (!rec)
504 return;
506 while (rec->next)
507 rec = rec->next;
509 while (rec->prev)
511 rec = rec->prev;
512 record_full_entry_release (rec->next);
515 if (rec == &record_full_first)
517 record_full_insn_num = 0;
518 record_full_first.next = NULL;
520 else
521 record_full_entry_release (rec);
524 /* Free all record entries forward of the given list position. */
526 static void
527 record_full_list_release_following (struct record_full_entry *rec)
529 struct record_full_entry *tmp = rec->next;
531 rec->next = NULL;
532 while (tmp)
534 rec = tmp->next;
535 if (record_full_entry_release (tmp) == record_full_end)
537 record_full_insn_num--;
538 record_full_insn_count--;
540 tmp = rec;
544 /* Delete the first instruction from the beginning of the log, to make
545 room for adding a new instruction at the end of the log.
547 Note -- this function does not modify record_full_insn_num. */
549 static void
550 record_full_list_release_first (void)
552 struct record_full_entry *tmp;
554 if (!record_full_first.next)
555 return;
557 /* Loop until a record_full_end. */
558 while (1)
560 /* Cut record_full_first.next out of the linked list. */
561 tmp = record_full_first.next;
562 record_full_first.next = tmp->next;
563 tmp->next->prev = &record_full_first;
565 /* tmp is now isolated, and can be deleted. */
566 if (record_full_entry_release (tmp) == record_full_end)
567 break; /* End loop at first record_full_end. */
569 if (!record_full_first.next)
571 gdb_assert (record_full_insn_num == 1);
572 break; /* End loop when list is empty. */
577 /* Add a struct record_full_entry to record_full_arch_list. */
579 static void
580 record_full_arch_list_add (struct record_full_entry *rec)
582 if (record_debug > 1)
583 gdb_printf (gdb_stdlog,
584 "Process record: record_full_arch_list_add %s.\n",
585 host_address_to_string (rec));
587 if (record_full_arch_list_tail)
589 record_full_arch_list_tail->next = rec;
590 rec->prev = record_full_arch_list_tail;
591 record_full_arch_list_tail = rec;
593 else
595 record_full_arch_list_head = rec;
596 record_full_arch_list_tail = rec;
600 /* Return the value storage location of a record entry. */
601 static inline gdb_byte *
602 record_full_get_loc (struct record_full_entry *rec)
604 switch (rec->type) {
605 case record_full_mem:
606 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
607 return rec->u.mem.u.ptr;
608 else
609 return rec->u.mem.u.buf;
610 case record_full_reg:
611 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
612 return rec->u.reg.u.ptr;
613 else
614 return rec->u.reg.u.buf;
615 case record_full_end:
616 default:
617 gdb_assert_not_reached ("unexpected record_full_entry type");
618 return NULL;
622 /* Record the value of a register NUM to record_full_arch_list. */
625 record_full_arch_list_add_reg (struct regcache *regcache, int regnum)
627 struct record_full_entry *rec;
629 if (record_debug > 1)
630 gdb_printf (gdb_stdlog,
631 "Process record: add register num = %d to "
632 "record list.\n",
633 regnum);
635 rec = record_full_reg_alloc (regcache, regnum);
637 regcache->cooked_read (regnum, record_full_get_loc (rec));
639 record_full_arch_list_add (rec);
641 return 0;
644 /* Record the value of a region of memory whose address is ADDR and
645 length is LEN to record_full_arch_list. */
648 record_full_arch_list_add_mem (CORE_ADDR addr, int len)
650 struct record_full_entry *rec;
652 if (record_debug > 1)
653 gdb_printf (gdb_stdlog,
654 "Process record: add mem addr = %s len = %d to "
655 "record list.\n",
656 paddress (current_inferior ()->arch (), addr), len);
658 if (!addr) /* FIXME: Why? Some arch must permit it... */
659 return 0;
661 rec = record_full_mem_alloc (addr, len);
663 if (record_read_memory (current_inferior ()->arch (), addr,
664 record_full_get_loc (rec), len))
666 record_full_mem_release (rec);
667 return -1;
670 record_full_arch_list_add (rec);
672 return 0;
675 /* Add a record_full_end type struct record_full_entry to
676 record_full_arch_list. */
679 record_full_arch_list_add_end (void)
681 struct record_full_entry *rec;
683 if (record_debug > 1)
684 gdb_printf (gdb_stdlog,
685 "Process record: add end to arch list.\n");
687 rec = record_full_end_alloc ();
688 rec->u.end.sigval = GDB_SIGNAL_0;
689 rec->u.end.insn_num = ++record_full_insn_count;
691 record_full_arch_list_add (rec);
693 return 0;
696 static void
697 record_full_check_insn_num (void)
699 if (record_full_insn_num == record_full_insn_max_num)
701 /* Ask user what to do. */
702 if (record_full_stop_at_limit)
704 if (!yquery (_("Do you want to auto delete previous execution "
705 "log entries when record/replay buffer becomes "
706 "full (record full stop-at-limit)?")))
707 error (_("Process record: stopped by user."));
708 record_full_stop_at_limit = 0;
713 /* Before inferior step (when GDB record the running message, inferior
714 only can step), GDB will call this function to record the values to
715 record_full_list. This function will call gdbarch_process_record to
716 record the running message of inferior and set them to
717 record_full_arch_list, and add it to record_full_list. */
719 static void
720 record_full_message (struct regcache *regcache, enum gdb_signal signal)
722 int ret;
723 struct gdbarch *gdbarch = regcache->arch ();
727 record_full_arch_list_head = NULL;
728 record_full_arch_list_tail = NULL;
730 /* Check record_full_insn_num. */
731 record_full_check_insn_num ();
733 /* If gdb sends a signal value to target_resume,
734 save it in the 'end' field of the previous instruction.
736 Maybe process record should record what really happened,
737 rather than what gdb pretends has happened.
739 So if Linux delivered the signal to the child process during
740 the record mode, we will record it and deliver it again in
741 the replay mode.
743 If user says "ignore this signal" during the record mode, then
744 it will be ignored again during the replay mode (no matter if
745 the user says something different, like "deliver this signal"
746 during the replay mode).
748 User should understand that nothing he does during the replay
749 mode will change the behavior of the child. If he tries,
750 then that is a user error.
752 But we should still deliver the signal to gdb during the replay,
753 if we delivered it during the recording. Therefore we should
754 record the signal during record_full_wait, not
755 record_full_resume. */
756 if (record_full_list != &record_full_first) /* FIXME better way
757 to check */
759 gdb_assert (record_full_list->type == record_full_end);
760 record_full_list->u.end.sigval = signal;
763 if (signal == GDB_SIGNAL_0
764 || !gdbarch_process_record_signal_p (gdbarch))
765 ret = gdbarch_process_record (gdbarch,
766 regcache,
767 regcache_read_pc (regcache));
768 else
769 ret = gdbarch_process_record_signal (gdbarch,
770 regcache,
771 signal);
773 if (ret > 0)
774 error (_("Process record: inferior program stopped."));
775 if (ret < 0)
776 error (_("Process record: failed to record execution log."));
778 catch (const gdb_exception &ex)
780 record_full_list_release (record_full_arch_list_tail);
781 throw;
784 record_full_list->next = record_full_arch_list_head;
785 record_full_arch_list_head->prev = record_full_list;
786 record_full_list = record_full_arch_list_tail;
788 if (record_full_insn_num == record_full_insn_max_num)
789 record_full_list_release_first ();
790 else
791 record_full_insn_num++;
794 static bool
795 record_full_message_wrapper_safe (struct regcache *regcache,
796 enum gdb_signal signal)
800 record_full_message (regcache, signal);
802 catch (const gdb_exception_error &ex)
804 exception_print (gdb_stderr, ex);
805 return false;
808 return true;
811 /* Set to 1 if record_full_store_registers and record_full_xfer_partial
812 doesn't need record. */
814 static int record_full_gdb_operation_disable = 0;
816 scoped_restore_tmpl<int>
817 record_full_gdb_operation_disable_set (void)
819 return make_scoped_restore (&record_full_gdb_operation_disable, 1);
822 /* Flag set to TRUE for target_stopped_by_watchpoint. */
823 static enum target_stop_reason record_full_stop_reason
824 = TARGET_STOPPED_BY_NO_REASON;
826 /* Execute one instruction from the record log. Each instruction in
827 the log will be represented by an arbitrary sequence of register
828 entries and memory entries, followed by an 'end' entry. */
830 static inline void
831 record_full_exec_insn (struct regcache *regcache,
832 struct gdbarch *gdbarch,
833 struct record_full_entry *entry)
835 switch (entry->type)
837 case record_full_reg: /* reg */
839 gdb::byte_vector reg (entry->u.reg.len);
841 if (record_debug > 1)
842 gdb_printf (gdb_stdlog,
843 "Process record: record_full_reg %s to "
844 "inferior num = %d.\n",
845 host_address_to_string (entry),
846 entry->u.reg.num);
848 regcache->cooked_read (entry->u.reg.num, reg.data ());
849 regcache->cooked_write (entry->u.reg.num, record_full_get_loc (entry));
850 memcpy (record_full_get_loc (entry), reg.data (), entry->u.reg.len);
852 break;
854 case record_full_mem: /* mem */
856 /* Nothing to do if the entry is flagged not_accessible. */
857 if (!entry->u.mem.mem_entry_not_accessible)
859 gdb::byte_vector mem (entry->u.mem.len);
861 if (record_debug > 1)
862 gdb_printf (gdb_stdlog,
863 "Process record: record_full_mem %s to "
864 "inferior addr = %s len = %d.\n",
865 host_address_to_string (entry),
866 paddress (gdbarch, entry->u.mem.addr),
867 entry->u.mem.len);
869 if (record_read_memory (gdbarch,
870 entry->u.mem.addr, mem.data (),
871 entry->u.mem.len))
872 entry->u.mem.mem_entry_not_accessible = 1;
873 else
875 if (target_write_memory (entry->u.mem.addr,
876 record_full_get_loc (entry),
877 entry->u.mem.len))
879 entry->u.mem.mem_entry_not_accessible = 1;
880 if (record_debug)
881 warning (_("Process record: error writing memory at "
882 "addr = %s len = %d."),
883 paddress (gdbarch, entry->u.mem.addr),
884 entry->u.mem.len);
886 else
888 memcpy (record_full_get_loc (entry), mem.data (),
889 entry->u.mem.len);
891 /* We've changed memory --- check if a hardware
892 watchpoint should trap. Note that this
893 presently assumes the target beneath supports
894 continuable watchpoints. On non-continuable
895 watchpoints target, we'll want to check this
896 _before_ actually doing the memory change, and
897 not doing the change at all if the watchpoint
898 traps. */
899 if (hardware_watchpoint_inserted_in_range
900 (current_inferior ()->aspace.get (),
901 entry->u.mem.addr, entry->u.mem.len))
902 record_full_stop_reason = TARGET_STOPPED_BY_WATCHPOINT;
907 break;
911 static void record_full_restore (void);
913 /* Asynchronous signal handle registered as event loop source for when
914 we have pending events ready to be passed to the core. */
916 static struct async_event_handler *record_full_async_inferior_event_token;
918 static void
919 record_full_async_inferior_event_handler (gdb_client_data data)
921 inferior_event_handler (INF_REG_EVENT);
924 /* Open the process record target for 'core' files. */
926 static void
927 record_full_core_open_1 ()
929 regcache *regcache = get_thread_regcache (inferior_thread ());
930 int regnum = gdbarch_num_regs (regcache->arch ());
931 int i;
933 /* Get record_full_core_regbuf. */
934 target_fetch_registers (regcache, -1);
935 record_full_core_regbuf = new detached_regcache (regcache->arch (), false);
937 for (i = 0; i < regnum; i ++)
938 record_full_core_regbuf->raw_supply (i, *regcache);
940 record_full_core_sections
941 = build_section_table (current_program_space->core_bfd ());
943 current_inferior ()->push_target (&record_full_core_ops);
944 record_full_restore ();
947 /* Open the process record target for 'live' processes. */
949 static void
950 record_full_open_1 ()
952 if (record_debug)
953 gdb_printf (gdb_stdlog, "Process record: record_full_open_1\n");
955 /* check exec */
956 if (!target_has_execution ())
957 error (_("Process record: the program is not being run."));
958 if (non_stop)
959 error (_("Process record target can't debug inferior in non-stop mode "
960 "(non-stop)."));
962 if (!gdbarch_process_record_p (current_inferior ()->arch ()))
963 error (_("Process record: the current architecture doesn't support "
964 "record function."));
966 current_inferior ()->push_target (&record_full_ops);
969 static void record_full_init_record_breakpoints (void);
971 /* Open the process record target. */
973 static void
974 record_full_open (const char *args, int from_tty)
976 if (record_debug)
977 gdb_printf (gdb_stdlog, "Process record: record_full_open\n");
979 if (args != nullptr)
980 error (_("Trailing junk: '%s'"), args);
982 record_preopen ();
984 /* Reset */
985 record_full_insn_num = 0;
986 record_full_insn_count = 0;
987 record_full_list = &record_full_first;
988 record_full_list->next = NULL;
990 if (current_program_space->core_bfd ())
991 record_full_core_open_1 ();
992 else
993 record_full_open_1 ();
995 /* Register extra event sources in the event loop. */
996 record_full_async_inferior_event_token
997 = create_async_event_handler (record_full_async_inferior_event_handler,
998 NULL, "record-full");
1000 record_full_init_record_breakpoints ();
1002 interps_notify_record_changed (current_inferior (), 1, "full", NULL);
1005 /* "close" target method. Close the process record target. */
1007 void
1008 record_full_base_target::close ()
1010 struct record_full_core_buf_entry *entry;
1012 if (record_debug)
1013 gdb_printf (gdb_stdlog, "Process record: record_full_close\n");
1015 record_full_list_release (record_full_list);
1017 /* Release record_full_core_regbuf. */
1018 if (record_full_core_regbuf)
1020 delete record_full_core_regbuf;
1021 record_full_core_regbuf = NULL;
1024 /* Release record_full_core_buf_list. */
1025 while (record_full_core_buf_list)
1027 entry = record_full_core_buf_list;
1028 record_full_core_buf_list = record_full_core_buf_list->prev;
1029 xfree (entry);
1032 if (record_full_async_inferior_event_token)
1033 delete_async_event_handler (&record_full_async_inferior_event_token);
1036 /* "async" target method. */
1038 void
1039 record_full_base_target::async (bool enable)
1041 if (enable)
1042 mark_async_event_handler (record_full_async_inferior_event_token);
1043 else
1044 clear_async_event_handler (record_full_async_inferior_event_token);
1046 beneath ()->async (enable);
1049 /* The PTID and STEP arguments last passed to
1050 record_full_target::resume. */
1051 static ptid_t record_full_resume_ptid = null_ptid;
1052 static int record_full_resume_step = 0;
1054 /* True if we've been resumed, and so each record_full_wait call should
1055 advance execution. If this is false, record_full_wait will return a
1056 TARGET_WAITKIND_IGNORE. */
1057 static int record_full_resumed = 0;
1059 /* The execution direction of the last resume we got. This is
1060 necessary for async mode. Vis (order is not strictly accurate):
1062 1. user has the global execution direction set to forward
1063 2. user does a reverse-step command
1064 3. record_full_resume is called with global execution direction
1065 temporarily switched to reverse
1066 4. GDB's execution direction is reverted back to forward
1067 5. target record notifies event loop there's an event to handle
1068 6. infrun asks the target which direction was it going, and switches
1069 the global execution direction accordingly (to reverse)
1070 7. infrun polls an event out of the record target, and handles it
1071 8. GDB goes back to the event loop, and goto #4.
1073 static enum exec_direction_kind record_full_execution_dir = EXEC_FORWARD;
1075 /* "resume" target method. Resume the process record target. */
1077 void
1078 record_full_target::resume (ptid_t ptid, int step, enum gdb_signal signal)
1080 record_full_resume_ptid = inferior_ptid;
1081 record_full_resume_step = step;
1082 record_full_resumed = 1;
1083 record_full_execution_dir = ::execution_direction;
1085 if (!RECORD_FULL_IS_REPLAY)
1087 struct gdbarch *gdbarch = target_thread_architecture (ptid);
1089 record_full_message (get_thread_regcache (inferior_thread ()), signal);
1091 if (!step)
1093 /* This is not hard single step. */
1094 if (!gdbarch_software_single_step_p (gdbarch))
1096 /* This is a normal continue. */
1097 step = 1;
1099 else
1101 /* This arch supports soft single step. */
1102 if (thread_has_single_step_breakpoints_set (inferior_thread ()))
1104 /* This is a soft single step. */
1105 record_full_resume_step = 1;
1107 else
1108 step = !insert_single_step_breakpoints (gdbarch);
1112 /* Make sure the target beneath reports all signals. */
1113 target_pass_signals ({});
1115 /* Disable range-stepping, forcing the process target to report stops for
1116 all executed instructions, so we can record them all. */
1117 process_stratum_target *proc_target
1118 = current_inferior ()->process_target ();
1119 for (thread_info *thread : all_non_exited_threads (proc_target, ptid))
1120 thread->control.may_range_step = 0;
1122 this->beneath ()->resume (ptid, step, signal);
1126 static int record_full_get_sig = 0;
1128 /* SIGINT signal handler, registered by "wait" method. */
1130 static void
1131 record_full_sig_handler (int signo)
1133 if (record_debug)
1134 gdb_printf (gdb_stdlog, "Process record: get a signal\n");
1136 /* It will break the running inferior in replay mode. */
1137 record_full_resume_step = 1;
1139 /* It will let record_full_wait set inferior status to get the signal
1140 SIGINT. */
1141 record_full_get_sig = 1;
1144 /* "wait" target method for process record target.
1146 In record mode, the target is always run in singlestep mode
1147 (even when gdb says to continue). The wait method intercepts
1148 the stop events and determines which ones are to be passed on to
1149 gdb. Most stop events are just singlestep events that gdb is not
1150 to know about, so the wait method just records them and keeps
1151 singlestepping.
1153 In replay mode, this function emulates the recorded execution log,
1154 one instruction at a time (forward or backward), and determines
1155 where to stop. */
1157 static ptid_t
1158 record_full_wait_1 (struct target_ops *ops,
1159 ptid_t ptid, struct target_waitstatus *status,
1160 target_wait_flags options)
1162 scoped_restore restore_operation_disable
1163 = record_full_gdb_operation_disable_set ();
1164 scoped_signal_handler<SIGINT> interrupt_handler (record_full_sig_handler);
1166 if (record_debug)
1167 gdb_printf (gdb_stdlog,
1168 "Process record: record_full_wait "
1169 "record_full_resume_step = %d, "
1170 "record_full_resumed = %d, direction=%s\n",
1171 record_full_resume_step, record_full_resumed,
1172 record_full_execution_dir == EXEC_FORWARD
1173 ? "forward" : "reverse");
1175 if (!record_full_resumed)
1177 gdb_assert ((options & TARGET_WNOHANG) != 0);
1179 /* No interesting event. */
1180 status->set_ignore ();
1181 return minus_one_ptid;
1184 record_full_get_sig = 0;
1186 record_full_stop_reason = TARGET_STOPPED_BY_NO_REASON;
1188 if (!RECORD_FULL_IS_REPLAY && ops != &record_full_core_ops)
1190 if (record_full_resume_step)
1192 /* This is a single step. */
1193 return ops->beneath ()->wait (ptid, status, options);
1195 else
1197 /* This is not a single step. */
1198 ptid_t ret;
1199 CORE_ADDR tmp_pc;
1200 struct gdbarch *gdbarch
1201 = target_thread_architecture (record_full_resume_ptid);
1203 while (1)
1205 ret = ops->beneath ()->wait (ptid, status, options);
1206 if (status->kind () == TARGET_WAITKIND_IGNORE)
1208 if (record_debug)
1209 gdb_printf (gdb_stdlog,
1210 "Process record: record_full_wait "
1211 "target beneath not done yet\n");
1212 return ret;
1215 for (thread_info *tp : all_non_exited_threads ())
1216 delete_single_step_breakpoints (tp);
1218 if (record_full_resume_step)
1219 return ret;
1221 /* Is this a SIGTRAP? */
1222 if (status->kind () == TARGET_WAITKIND_STOPPED
1223 && status->sig () == GDB_SIGNAL_TRAP)
1225 struct regcache *regcache;
1226 enum target_stop_reason *stop_reason_p
1227 = &record_full_stop_reason;
1229 /* Yes -- this is likely our single-step finishing,
1230 but check if there's any reason the core would be
1231 interested in the event. */
1233 registers_changed ();
1234 switch_to_thread (current_inferior ()->process_target (),
1235 ret);
1236 regcache = get_thread_regcache (inferior_thread ());
1237 tmp_pc = regcache_read_pc (regcache);
1238 const address_space *aspace
1239 = current_inferior ()->aspace.get ();
1241 if (target_stopped_by_watchpoint ())
1243 /* Always interested in watchpoints. */
1245 else if (record_check_stopped_by_breakpoint (aspace, tmp_pc,
1246 stop_reason_p))
1248 /* There is a breakpoint here. Let the core
1249 handle it. */
1251 else
1253 /* This is a single-step trap. Record the
1254 insn and issue another step.
1255 FIXME: this part can be a random SIGTRAP too.
1256 But GDB cannot handle it. */
1257 int step = 1;
1259 if (!record_full_message_wrapper_safe (regcache,
1260 GDB_SIGNAL_0))
1262 status->set_stopped (GDB_SIGNAL_0);
1263 break;
1266 process_stratum_target *proc_target
1267 = current_inferior ()->process_target ();
1269 if (gdbarch_software_single_step_p (gdbarch))
1271 /* Try to insert the software single step breakpoint.
1272 If insert success, set step to 0. */
1273 set_executing (proc_target, inferior_ptid, false);
1274 SCOPE_EXIT
1276 set_executing (proc_target, inferior_ptid, true);
1279 reinit_frame_cache ();
1280 step = !insert_single_step_breakpoints (gdbarch);
1283 if (record_debug)
1284 gdb_printf (gdb_stdlog,
1285 "Process record: record_full_wait "
1286 "issuing one more step in the "
1287 "target beneath\n");
1288 ops->beneath ()->resume (ptid, step, GDB_SIGNAL_0);
1289 proc_target->commit_resumed_state = true;
1290 proc_target->commit_resumed ();
1291 proc_target->commit_resumed_state = false;
1292 continue;
1296 /* The inferior is broken by a breakpoint or a signal. */
1297 break;
1300 return ret;
1303 else
1305 switch_to_thread (current_inferior ()->process_target (),
1306 record_full_resume_ptid);
1307 regcache *regcache = get_thread_regcache (inferior_thread ());
1308 struct gdbarch *gdbarch = regcache->arch ();
1309 const address_space *aspace = current_inferior ()->aspace.get ();
1310 int continue_flag = 1;
1311 int first_record_full_end = 1;
1315 CORE_ADDR tmp_pc;
1317 record_full_stop_reason = TARGET_STOPPED_BY_NO_REASON;
1318 status->set_stopped (GDB_SIGNAL_0);
1320 /* Check breakpoint when forward execute. */
1321 if (execution_direction == EXEC_FORWARD)
1323 tmp_pc = regcache_read_pc (regcache);
1324 if (record_check_stopped_by_breakpoint (aspace, tmp_pc,
1325 &record_full_stop_reason))
1327 if (record_debug)
1328 gdb_printf (gdb_stdlog,
1329 "Process record: break at %s.\n",
1330 paddress (gdbarch, tmp_pc));
1331 goto replay_out;
1335 /* If GDB is in terminal_inferior mode, it will not get the
1336 signal. And in GDB replay mode, GDB doesn't need to be
1337 in terminal_inferior mode, because inferior will not
1338 executed. Then set it to terminal_ours to make GDB get
1339 the signal. */
1340 target_terminal::ours ();
1342 /* In EXEC_FORWARD mode, record_full_list points to the tail of prev
1343 instruction. */
1344 if (execution_direction == EXEC_FORWARD && record_full_list->next)
1345 record_full_list = record_full_list->next;
1347 /* Loop over the record_full_list, looking for the next place to
1348 stop. */
1351 /* Check for beginning and end of log. */
1352 if (execution_direction == EXEC_REVERSE
1353 && record_full_list == &record_full_first)
1355 /* Hit beginning of record log in reverse. */
1356 status->set_no_history ();
1357 break;
1359 if (execution_direction != EXEC_REVERSE
1360 && !record_full_list->next)
1362 /* Hit end of record log going forward. */
1363 status->set_no_history ();
1364 break;
1367 record_full_exec_insn (regcache, gdbarch, record_full_list);
1369 if (record_full_list->type == record_full_end)
1371 if (record_debug > 1)
1372 gdb_printf
1373 (gdb_stdlog,
1374 "Process record: record_full_end %s to "
1375 "inferior.\n",
1376 host_address_to_string (record_full_list));
1378 if (first_record_full_end
1379 && execution_direction == EXEC_REVERSE)
1381 /* When reverse execute, the first
1382 record_full_end is the part of current
1383 instruction. */
1384 first_record_full_end = 0;
1386 else
1388 /* In EXEC_REVERSE mode, this is the
1389 record_full_end of prev instruction. In
1390 EXEC_FORWARD mode, this is the
1391 record_full_end of current instruction. */
1392 /* step */
1393 if (record_full_resume_step)
1395 if (record_debug > 1)
1396 gdb_printf (gdb_stdlog,
1397 "Process record: step.\n");
1398 continue_flag = 0;
1401 /* check breakpoint */
1402 tmp_pc = regcache_read_pc (regcache);
1403 if (record_check_stopped_by_breakpoint
1404 (aspace, tmp_pc, &record_full_stop_reason))
1406 if (record_debug)
1407 gdb_printf (gdb_stdlog,
1408 "Process record: break "
1409 "at %s.\n",
1410 paddress (gdbarch, tmp_pc));
1412 continue_flag = 0;
1415 if (record_full_stop_reason
1416 == TARGET_STOPPED_BY_WATCHPOINT)
1418 if (record_debug)
1419 gdb_printf (gdb_stdlog,
1420 "Process record: hit hw "
1421 "watchpoint.\n");
1422 continue_flag = 0;
1424 /* Check target signal */
1425 if (record_full_list->u.end.sigval != GDB_SIGNAL_0)
1426 /* FIXME: better way to check */
1427 continue_flag = 0;
1431 if (continue_flag)
1433 if (execution_direction == EXEC_REVERSE)
1435 if (record_full_list->prev)
1436 record_full_list = record_full_list->prev;
1438 else
1440 if (record_full_list->next)
1441 record_full_list = record_full_list->next;
1445 while (continue_flag);
1447 replay_out:
1448 if (status->kind () == TARGET_WAITKIND_STOPPED)
1450 if (record_full_get_sig)
1451 status->set_stopped (GDB_SIGNAL_INT);
1452 else if (record_full_list->u.end.sigval != GDB_SIGNAL_0)
1453 /* FIXME: better way to check */
1454 status->set_stopped (record_full_list->u.end.sigval);
1455 else
1456 status->set_stopped (GDB_SIGNAL_TRAP);
1459 catch (const gdb_exception &ex)
1461 if (execution_direction == EXEC_REVERSE)
1463 if (record_full_list->next)
1464 record_full_list = record_full_list->next;
1466 else
1467 record_full_list = record_full_list->prev;
1469 throw;
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\n\
2991 instruction will be undone.\n\
2992 If a positive argument is given, prints\n\
2993 how the nth following instruction will be redone."), &maintenanceprintlist);