1 /* Process record and replay target for GDB, the GNU debugger.
3 Copyright (C) 2013 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/>. */
23 #include "gdbthread.h"
24 #include "event-top.h"
25 #include "exceptions.h"
26 #include "completer.h"
27 #include "arch-utils.h"
31 #include "record-full.h"
34 #include "event-loop.h"
41 /* This module implements "target record-full", also known as "process
42 record and replay". This target sits on top of a "normal" target
43 (a target that "has execution"), and provides a record and replay
44 functionality, including reverse debugging.
46 Target record has two modes: recording, and replaying.
48 In record mode, we intercept the to_resume and to_wait methods.
49 Whenever gdb resumes the target, we run the target in single step
50 mode, and we build up an execution log in which, for each executed
51 instruction, we record all changes in memory and register state.
52 This is invisible to the user, to whom it just looks like an
53 ordinary debugging session (except for performance degredation).
55 In replay mode, instead of actually letting the inferior run as a
56 process, we simulate its execution by playing back the recorded
57 execution log. For each instruction in the log, we simulate the
58 instruction's side effects by duplicating the changes that it would
59 have made on memory and registers. */
61 #define DEFAULT_RECORD_FULL_INSN_MAX_NUM 200000
63 #define RECORD_FULL_IS_REPLAY \
64 (record_full_list->next || execution_direction == EXEC_REVERSE)
66 #define RECORD_FULL_FILE_MAGIC netorder32(0x20091016)
68 /* These are the core structs of the process record functionality.
70 A record_full_entry is a record of the value change of a register
71 ("record_full_reg") or a part of memory ("record_full_mem"). And each
72 instruction must have a struct record_full_entry ("record_full_end")
73 that indicates that this is the last struct record_full_entry of this
76 Each struct record_full_entry is linked to "record_full_list" by "prev"
77 and "next" pointers. */
79 struct record_full_mem_entry
83 /* Set this flag if target memory for this entry
84 can no longer be accessed. */
85 int mem_entry_not_accessible;
89 gdb_byte buf[sizeof (gdb_byte *)];
93 struct record_full_reg_entry
100 gdb_byte buf[2 * sizeof (gdb_byte *)];
104 struct record_full_end_entry
106 enum gdb_signal sigval;
110 enum record_full_type
117 /* This is the data structure that makes up the execution log.
119 The execution log consists of a single linked list of entries
120 of type "struct record_full_entry". It is doubly linked so that it
121 can be traversed in either direction.
123 The start of the list is anchored by a struct called
124 "record_full_first". The pointer "record_full_list" either points
125 to the last entry that was added to the list (in record mode), or to
126 the next entry in the list that will be executed (in replay mode).
128 Each list element (struct record_full_entry), in addition to next
129 and prev pointers, consists of a union of three entry types: mem,
130 reg, and end. A field called "type" determines which entry type is
131 represented by a given list element.
133 Each instruction that is added to the execution log is represented
134 by a variable number of list elements ('entries'). The instruction
135 will have one "reg" entry for each register that is changed by
136 executing the instruction (including the PC in every case). It
137 will also have one "mem" entry for each memory change. Finally,
138 each instruction will have an "end" entry that separates it from
139 the changes associated with the next instruction. */
141 struct record_full_entry
143 struct record_full_entry *prev;
144 struct record_full_entry *next;
145 enum record_full_type type;
149 struct record_full_reg_entry reg;
151 struct record_full_mem_entry mem;
153 struct record_full_end_entry end;
157 /* If true, query if PREC cannot record memory
158 change of next instruction. */
159 int record_full_memory_query = 0;
161 struct record_full_core_buf_entry
163 struct record_full_core_buf_entry *prev;
164 struct target_section *p;
168 /* Record buf with core target. */
169 static gdb_byte *record_full_core_regbuf = NULL;
170 static struct target_section *record_full_core_start;
171 static struct target_section *record_full_core_end;
172 static struct record_full_core_buf_entry *record_full_core_buf_list = NULL;
174 /* The following variables are used for managing the linked list that
175 represents the execution log.
177 record_full_first is the anchor that holds down the beginning of
180 record_full_list serves two functions:
181 1) In record mode, it anchors the end of the list.
182 2) In replay mode, it traverses the list and points to
183 the next instruction that must be emulated.
185 record_full_arch_list_head and record_full_arch_list_tail are used
186 to manage a separate list, which is used to build up the change
187 elements of the currently executing instruction during record mode.
188 When this instruction has been completely annotated in the "arch
189 list", it will be appended to the main execution log. */
191 static struct record_full_entry record_full_first;
192 static struct record_full_entry *record_full_list = &record_full_first;
193 static struct record_full_entry *record_full_arch_list_head = NULL;
194 static struct record_full_entry *record_full_arch_list_tail = NULL;
196 /* 1 ask user. 0 auto delete the last struct record_full_entry. */
197 static int record_full_stop_at_limit = 1;
198 /* Maximum allowed number of insns in execution log. */
199 static unsigned int record_full_insn_max_num
200 = DEFAULT_RECORD_FULL_INSN_MAX_NUM;
201 /* Actual count of insns presently in execution log. */
202 static int record_full_insn_num = 0;
203 /* Count of insns logged so far (may be larger
204 than count of insns presently in execution log). */
205 static ULONGEST record_full_insn_count;
207 /* The target_ops of process record. */
208 static struct target_ops record_full_ops;
209 static struct target_ops record_full_core_ops;
211 /* Command lists for "set/show record full". */
212 static struct cmd_list_element *set_record_full_cmdlist;
213 static struct cmd_list_element *show_record_full_cmdlist;
215 /* Command list for "record full". */
216 static struct cmd_list_element *record_full_cmdlist;
218 /* The beneath function pointers. */
219 static struct target_ops *record_full_beneath_to_resume_ops;
220 static void (*record_full_beneath_to_resume) (struct target_ops *, ptid_t, int,
222 static struct target_ops *record_full_beneath_to_wait_ops;
223 static ptid_t (*record_full_beneath_to_wait) (struct target_ops *, ptid_t,
224 struct target_waitstatus *,
226 static struct target_ops *record_full_beneath_to_store_registers_ops;
227 static void (*record_full_beneath_to_store_registers) (struct target_ops *,
230 static struct target_ops *record_full_beneath_to_xfer_partial_ops;
232 (*record_full_beneath_to_xfer_partial) (struct target_ops *ops,
233 enum target_object object,
236 const gdb_byte *writebuf,
240 (*record_full_beneath_to_insert_breakpoint) (struct gdbarch *,
241 struct bp_target_info *);
243 (*record_full_beneath_to_remove_breakpoint) (struct gdbarch *,
244 struct bp_target_info *);
245 static int (*record_full_beneath_to_stopped_by_watchpoint) (void);
246 static int (*record_full_beneath_to_stopped_data_address) (struct target_ops *,
249 (*record_full_beneath_to_async) (void (*) (enum inferior_event_type, void *),
252 static void record_full_goto_insn (struct record_full_entry *entry,
253 enum exec_direction_kind dir);
254 static void record_full_save (char *recfilename);
256 /* Alloc and free functions for record_full_reg, record_full_mem, and
257 record_full_end entries. */
259 /* Alloc a record_full_reg record entry. */
261 static inline struct record_full_entry *
262 record_full_reg_alloc (struct regcache *regcache, int regnum)
264 struct record_full_entry *rec;
265 struct gdbarch *gdbarch = get_regcache_arch (regcache);
267 rec = xcalloc (1, sizeof (struct record_full_entry));
268 rec->type = record_full_reg;
269 rec->u.reg.num = regnum;
270 rec->u.reg.len = register_size (gdbarch, regnum);
271 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
272 rec->u.reg.u.ptr = (gdb_byte *) xmalloc (rec->u.reg.len);
277 /* Free a record_full_reg record entry. */
280 record_full_reg_release (struct record_full_entry *rec)
282 gdb_assert (rec->type == record_full_reg);
283 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
284 xfree (rec->u.reg.u.ptr);
288 /* Alloc a record_full_mem record entry. */
290 static inline struct record_full_entry *
291 record_full_mem_alloc (CORE_ADDR addr, int len)
293 struct record_full_entry *rec;
295 rec = xcalloc (1, sizeof (struct record_full_entry));
296 rec->type = record_full_mem;
297 rec->u.mem.addr = addr;
298 rec->u.mem.len = len;
299 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
300 rec->u.mem.u.ptr = (gdb_byte *) xmalloc (len);
305 /* Free a record_full_mem record entry. */
308 record_full_mem_release (struct record_full_entry *rec)
310 gdb_assert (rec->type == record_full_mem);
311 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
312 xfree (rec->u.mem.u.ptr);
316 /* Alloc a record_full_end record entry. */
318 static inline struct record_full_entry *
319 record_full_end_alloc (void)
321 struct record_full_entry *rec;
323 rec = xcalloc (1, sizeof (struct record_full_entry));
324 rec->type = record_full_end;
329 /* Free a record_full_end record entry. */
332 record_full_end_release (struct record_full_entry *rec)
337 /* Free one record entry, any type.
338 Return entry->type, in case caller wants to know. */
340 static inline enum record_full_type
341 record_full_entry_release (struct record_full_entry *rec)
343 enum record_full_type type = rec->type;
346 case record_full_reg:
347 record_full_reg_release (rec);
349 case record_full_mem:
350 record_full_mem_release (rec);
352 case record_full_end:
353 record_full_end_release (rec);
359 /* Free all record entries in list pointed to by REC. */
362 record_full_list_release (struct record_full_entry *rec)
373 record_full_entry_release (rec->next);
376 if (rec == &record_full_first)
378 record_full_insn_num = 0;
379 record_full_first.next = NULL;
382 record_full_entry_release (rec);
385 /* Free all record entries forward of the given list position. */
388 record_full_list_release_following (struct record_full_entry *rec)
390 struct record_full_entry *tmp = rec->next;
396 if (record_full_entry_release (tmp) == record_full_end)
398 record_full_insn_num--;
399 record_full_insn_count--;
405 /* Delete the first instruction from the beginning of the log, to make
406 room for adding a new instruction at the end of the log.
408 Note -- this function does not modify record_full_insn_num. */
411 record_full_list_release_first (void)
413 struct record_full_entry *tmp;
415 if (!record_full_first.next)
418 /* Loop until a record_full_end. */
421 /* Cut record_full_first.next out of the linked list. */
422 tmp = record_full_first.next;
423 record_full_first.next = tmp->next;
424 tmp->next->prev = &record_full_first;
426 /* tmp is now isolated, and can be deleted. */
427 if (record_full_entry_release (tmp) == record_full_end)
428 break; /* End loop at first record_full_end. */
430 if (!record_full_first.next)
432 gdb_assert (record_full_insn_num == 1);
433 break; /* End loop when list is empty. */
438 /* Add a struct record_full_entry to record_full_arch_list. */
441 record_full_arch_list_add (struct record_full_entry *rec)
443 if (record_debug > 1)
444 fprintf_unfiltered (gdb_stdlog,
445 "Process record: record_full_arch_list_add %s.\n",
446 host_address_to_string (rec));
448 if (record_full_arch_list_tail)
450 record_full_arch_list_tail->next = rec;
451 rec->prev = record_full_arch_list_tail;
452 record_full_arch_list_tail = rec;
456 record_full_arch_list_head = rec;
457 record_full_arch_list_tail = rec;
461 /* Return the value storage location of a record entry. */
462 static inline gdb_byte *
463 record_full_get_loc (struct record_full_entry *rec)
466 case record_full_mem:
467 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
468 return rec->u.mem.u.ptr;
470 return rec->u.mem.u.buf;
471 case record_full_reg:
472 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
473 return rec->u.reg.u.ptr;
475 return rec->u.reg.u.buf;
476 case record_full_end:
478 gdb_assert_not_reached ("unexpected record_full_entry type");
483 /* Record the value of a register NUM to record_full_arch_list. */
486 record_full_arch_list_add_reg (struct regcache *regcache, int regnum)
488 struct record_full_entry *rec;
490 if (record_debug > 1)
491 fprintf_unfiltered (gdb_stdlog,
492 "Process record: add register num = %d to "
496 rec = record_full_reg_alloc (regcache, regnum);
498 regcache_raw_read (regcache, regnum, record_full_get_loc (rec));
500 record_full_arch_list_add (rec);
505 /* Record the value of a region of memory whose address is ADDR and
506 length is LEN to record_full_arch_list. */
509 record_full_arch_list_add_mem (CORE_ADDR addr, int len)
511 struct record_full_entry *rec;
513 if (record_debug > 1)
514 fprintf_unfiltered (gdb_stdlog,
515 "Process record: add mem addr = %s len = %d to "
517 paddress (target_gdbarch (), addr), len);
519 if (!addr) /* FIXME: Why? Some arch must permit it... */
522 rec = record_full_mem_alloc (addr, len);
524 if (record_read_memory (target_gdbarch (), addr,
525 record_full_get_loc (rec), len))
527 record_full_mem_release (rec);
531 record_full_arch_list_add (rec);
536 /* Add a record_full_end type struct record_full_entry to
537 record_full_arch_list. */
540 record_full_arch_list_add_end (void)
542 struct record_full_entry *rec;
544 if (record_debug > 1)
545 fprintf_unfiltered (gdb_stdlog,
546 "Process record: add end to arch list.\n");
548 rec = record_full_end_alloc ();
549 rec->u.end.sigval = GDB_SIGNAL_0;
550 rec->u.end.insn_num = ++record_full_insn_count;
552 record_full_arch_list_add (rec);
558 record_full_check_insn_num (int set_terminal)
560 if (record_full_insn_max_num)
562 gdb_assert (record_full_insn_num <= record_full_insn_max_num);
563 if (record_full_insn_num == record_full_insn_max_num)
565 /* Ask user what to do. */
566 if (record_full_stop_at_limit)
571 target_terminal_ours ();
572 q = yquery (_("Do you want to auto delete previous execution "
573 "log entries when record/replay buffer becomes "
574 "full (record full stop-at-limit)?"));
576 target_terminal_inferior ();
578 record_full_stop_at_limit = 0;
580 error (_("Process record: stopped by user."));
587 record_full_arch_list_cleanups (void *ignore)
589 record_full_list_release (record_full_arch_list_tail);
592 /* Before inferior step (when GDB record the running message, inferior
593 only can step), GDB will call this function to record the values to
594 record_full_list. This function will call gdbarch_process_record to
595 record the running message of inferior and set them to
596 record_full_arch_list, and add it to record_full_list. */
599 record_full_message (struct regcache *regcache, enum gdb_signal signal)
602 struct gdbarch *gdbarch = get_regcache_arch (regcache);
603 struct cleanup *old_cleanups
604 = make_cleanup (record_full_arch_list_cleanups, 0);
606 record_full_arch_list_head = NULL;
607 record_full_arch_list_tail = NULL;
609 /* Check record_full_insn_num. */
610 record_full_check_insn_num (1);
612 /* If gdb sends a signal value to target_resume,
613 save it in the 'end' field of the previous instruction.
615 Maybe process record should record what really happened,
616 rather than what gdb pretends has happened.
618 So if Linux delivered the signal to the child process during
619 the record mode, we will record it and deliver it again in
622 If user says "ignore this signal" during the record mode, then
623 it will be ignored again during the replay mode (no matter if
624 the user says something different, like "deliver this signal"
625 during the replay mode).
627 User should understand that nothing he does during the replay
628 mode will change the behavior of the child. If he tries,
629 then that is a user error.
631 But we should still deliver the signal to gdb during the replay,
632 if we delivered it during the recording. Therefore we should
633 record the signal during record_full_wait, not
634 record_full_resume. */
635 if (record_full_list != &record_full_first) /* FIXME better way to check */
637 gdb_assert (record_full_list->type == record_full_end);
638 record_full_list->u.end.sigval = signal;
641 if (signal == GDB_SIGNAL_0
642 || !gdbarch_process_record_signal_p (gdbarch))
643 ret = gdbarch_process_record (gdbarch,
645 regcache_read_pc (regcache));
647 ret = gdbarch_process_record_signal (gdbarch,
652 error (_("Process record: inferior program stopped."));
654 error (_("Process record: failed to record execution log."));
656 discard_cleanups (old_cleanups);
658 record_full_list->next = record_full_arch_list_head;
659 record_full_arch_list_head->prev = record_full_list;
660 record_full_list = record_full_arch_list_tail;
662 if (record_full_insn_num == record_full_insn_max_num
663 && record_full_insn_max_num)
664 record_full_list_release_first ();
666 record_full_insn_num++;
671 struct record_full_message_args {
672 struct regcache *regcache;
673 enum gdb_signal signal;
677 record_full_message_wrapper (void *args)
679 struct record_full_message_args *record_full_args = args;
681 return record_full_message (record_full_args->regcache,
682 record_full_args->signal);
686 record_full_message_wrapper_safe (struct regcache *regcache,
687 enum gdb_signal signal)
689 struct record_full_message_args args;
691 args.regcache = regcache;
692 args.signal = signal;
694 return catch_errors (record_full_message_wrapper, &args, NULL,
698 /* Set to 1 if record_full_store_registers and record_full_xfer_partial
699 doesn't need record. */
701 static int record_full_gdb_operation_disable = 0;
704 record_full_gdb_operation_disable_set (void)
706 struct cleanup *old_cleanups = NULL;
709 make_cleanup_restore_integer (&record_full_gdb_operation_disable);
710 record_full_gdb_operation_disable = 1;
715 /* Flag set to TRUE for target_stopped_by_watchpoint. */
716 static int record_full_hw_watchpoint = 0;
718 /* Execute one instruction from the record log. Each instruction in
719 the log will be represented by an arbitrary sequence of register
720 entries and memory entries, followed by an 'end' entry. */
723 record_full_exec_insn (struct regcache *regcache,
724 struct gdbarch *gdbarch,
725 struct record_full_entry *entry)
729 case record_full_reg: /* reg */
731 gdb_byte reg[MAX_REGISTER_SIZE];
733 if (record_debug > 1)
734 fprintf_unfiltered (gdb_stdlog,
735 "Process record: record_full_reg %s to "
736 "inferior num = %d.\n",
737 host_address_to_string (entry),
740 regcache_cooked_read (regcache, entry->u.reg.num, reg);
741 regcache_cooked_write (regcache, entry->u.reg.num,
742 record_full_get_loc (entry));
743 memcpy (record_full_get_loc (entry), reg, entry->u.reg.len);
747 case record_full_mem: /* mem */
749 /* Nothing to do if the entry is flagged not_accessible. */
750 if (!entry->u.mem.mem_entry_not_accessible)
752 gdb_byte *mem = alloca (entry->u.mem.len);
754 if (record_debug > 1)
755 fprintf_unfiltered (gdb_stdlog,
756 "Process record: record_full_mem %s to "
757 "inferior addr = %s len = %d.\n",
758 host_address_to_string (entry),
759 paddress (gdbarch, entry->u.mem.addr),
762 if (record_read_memory (gdbarch,
763 entry->u.mem.addr, mem, entry->u.mem.len))
764 entry->u.mem.mem_entry_not_accessible = 1;
767 if (target_write_memory (entry->u.mem.addr,
768 record_full_get_loc (entry),
771 entry->u.mem.mem_entry_not_accessible = 1;
773 warning (_("Process record: error writing memory at "
774 "addr = %s len = %d."),
775 paddress (gdbarch, entry->u.mem.addr),
780 memcpy (record_full_get_loc (entry), mem,
783 /* We've changed memory --- check if a hardware
784 watchpoint should trap. Note that this
785 presently assumes the target beneath supports
786 continuable watchpoints. On non-continuable
787 watchpoints target, we'll want to check this
788 _before_ actually doing the memory change, and
789 not doing the change at all if the watchpoint
791 if (hardware_watchpoint_inserted_in_range
792 (get_regcache_aspace (regcache),
793 entry->u.mem.addr, entry->u.mem.len))
794 record_full_hw_watchpoint = 1;
803 static struct target_ops *tmp_to_resume_ops;
804 static void (*tmp_to_resume) (struct target_ops *, ptid_t, int,
806 static struct target_ops *tmp_to_wait_ops;
807 static ptid_t (*tmp_to_wait) (struct target_ops *, ptid_t,
808 struct target_waitstatus *,
810 static struct target_ops *tmp_to_store_registers_ops;
811 static void (*tmp_to_store_registers) (struct target_ops *,
814 static struct target_ops *tmp_to_xfer_partial_ops;
815 static LONGEST (*tmp_to_xfer_partial) (struct target_ops *ops,
816 enum target_object object,
819 const gdb_byte *writebuf,
822 static int (*tmp_to_insert_breakpoint) (struct gdbarch *,
823 struct bp_target_info *);
824 static int (*tmp_to_remove_breakpoint) (struct gdbarch *,
825 struct bp_target_info *);
826 static int (*tmp_to_stopped_by_watchpoint) (void);
827 static int (*tmp_to_stopped_data_address) (struct target_ops *, CORE_ADDR *);
828 static int (*tmp_to_stopped_data_address) (struct target_ops *, CORE_ADDR *);
829 static void (*tmp_to_async) (void (*) (enum inferior_event_type, void *), void *);
831 static void record_full_restore (void);
833 /* Asynchronous signal handle registered as event loop source for when
834 we have pending events ready to be passed to the core. */
836 static struct async_event_handler *record_full_async_inferior_event_token;
839 record_full_async_inferior_event_handler (gdb_client_data data)
841 inferior_event_handler (INF_REG_EVENT, NULL);
844 /* Open the process record target. */
847 record_full_core_open_1 (char *name, int from_tty)
849 struct regcache *regcache = get_current_regcache ();
850 int regnum = gdbarch_num_regs (get_regcache_arch (regcache));
853 /* Get record_full_core_regbuf. */
854 target_fetch_registers (regcache, -1);
855 record_full_core_regbuf = xmalloc (MAX_REGISTER_SIZE * regnum);
856 for (i = 0; i < regnum; i ++)
857 regcache_raw_collect (regcache, i,
858 record_full_core_regbuf + MAX_REGISTER_SIZE * i);
860 /* Get record_full_core_start and record_full_core_end. */
861 if (build_section_table (core_bfd, &record_full_core_start,
862 &record_full_core_end))
864 xfree (record_full_core_regbuf);
865 record_full_core_regbuf = NULL;
866 error (_("\"%s\": Can't find sections: %s"),
867 bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ()));
870 push_target (&record_full_core_ops);
871 record_full_restore ();
874 /* "to_open" target method for 'live' processes. */
877 record_full_open_1 (char *name, int from_tty)
880 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_open\n");
883 if (!target_has_execution)
884 error (_("Process record: the program is not being run."));
886 error (_("Process record target can't debug inferior in non-stop mode "
889 if (!gdbarch_process_record_p (target_gdbarch ()))
890 error (_("Process record: the current architecture doesn't support "
891 "record function."));
894 error (_("Could not find 'to_resume' method on the target stack."));
896 error (_("Could not find 'to_wait' method on the target stack."));
897 if (!tmp_to_store_registers)
898 error (_("Could not find 'to_store_registers' "
899 "method on the target stack."));
900 if (!tmp_to_insert_breakpoint)
901 error (_("Could not find 'to_insert_breakpoint' "
902 "method on the target stack."));
903 if (!tmp_to_remove_breakpoint)
904 error (_("Could not find 'to_remove_breakpoint' "
905 "method on the target stack."));
906 if (!tmp_to_stopped_by_watchpoint)
907 error (_("Could not find 'to_stopped_by_watchpoint' "
908 "method on the target stack."));
909 if (!tmp_to_stopped_data_address)
910 error (_("Could not find 'to_stopped_data_address' "
911 "method on the target stack."));
913 push_target (&record_full_ops);
916 static void record_full_init_record_breakpoints (void);
918 /* "to_open" target method. Open the process record target. */
921 record_full_open (char *name, int from_tty)
923 struct target_ops *t;
926 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_open\n");
928 /* Check if record target is already running. */
929 if (current_target.to_stratum == record_stratum)
930 error (_("Process record target already running. Use \"record stop\" to "
931 "stop record target first."));
933 /* Reset the tmp beneath pointers. */
934 tmp_to_resume_ops = NULL;
935 tmp_to_resume = NULL;
936 tmp_to_wait_ops = NULL;
938 tmp_to_store_registers_ops = NULL;
939 tmp_to_store_registers = NULL;
940 tmp_to_xfer_partial_ops = NULL;
941 tmp_to_xfer_partial = NULL;
942 tmp_to_insert_breakpoint = NULL;
943 tmp_to_remove_breakpoint = NULL;
944 tmp_to_stopped_by_watchpoint = NULL;
945 tmp_to_stopped_data_address = NULL;
948 /* Set the beneath function pointers. */
949 for (t = current_target.beneath; t != NULL; t = t->beneath)
953 tmp_to_resume = t->to_resume;
954 tmp_to_resume_ops = t;
958 tmp_to_wait = t->to_wait;
961 if (!tmp_to_store_registers)
963 tmp_to_store_registers = t->to_store_registers;
964 tmp_to_store_registers_ops = t;
966 if (!tmp_to_xfer_partial)
968 tmp_to_xfer_partial = t->to_xfer_partial;
969 tmp_to_xfer_partial_ops = t;
971 if (!tmp_to_insert_breakpoint)
972 tmp_to_insert_breakpoint = t->to_insert_breakpoint;
973 if (!tmp_to_remove_breakpoint)
974 tmp_to_remove_breakpoint = t->to_remove_breakpoint;
975 if (!tmp_to_stopped_by_watchpoint)
976 tmp_to_stopped_by_watchpoint = t->to_stopped_by_watchpoint;
977 if (!tmp_to_stopped_data_address)
978 tmp_to_stopped_data_address = t->to_stopped_data_address;
980 tmp_to_async = t->to_async;
982 if (!tmp_to_xfer_partial)
983 error (_("Could not find 'to_xfer_partial' method on the target stack."));
986 record_full_insn_num = 0;
987 record_full_insn_count = 0;
988 record_full_list = &record_full_first;
989 record_full_list->next = NULL;
991 /* Set the tmp beneath pointers to beneath pointers. */
992 record_full_beneath_to_resume_ops = tmp_to_resume_ops;
993 record_full_beneath_to_resume = tmp_to_resume;
994 record_full_beneath_to_wait_ops = tmp_to_wait_ops;
995 record_full_beneath_to_wait = tmp_to_wait;
996 record_full_beneath_to_store_registers_ops = tmp_to_store_registers_ops;
997 record_full_beneath_to_store_registers = tmp_to_store_registers;
998 record_full_beneath_to_xfer_partial_ops = tmp_to_xfer_partial_ops;
999 record_full_beneath_to_xfer_partial = tmp_to_xfer_partial;
1000 record_full_beneath_to_insert_breakpoint = tmp_to_insert_breakpoint;
1001 record_full_beneath_to_remove_breakpoint = tmp_to_remove_breakpoint;
1002 record_full_beneath_to_stopped_by_watchpoint = tmp_to_stopped_by_watchpoint;
1003 record_full_beneath_to_stopped_data_address = tmp_to_stopped_data_address;
1004 record_full_beneath_to_async = tmp_to_async;
1007 record_full_core_open_1 (name, from_tty);
1009 record_full_open_1 (name, from_tty);
1011 /* Register extra event sources in the event loop. */
1012 record_full_async_inferior_event_token
1013 = create_async_event_handler (record_full_async_inferior_event_handler,
1016 record_full_init_record_breakpoints ();
1018 observer_notify_record_changed (current_inferior (), 1);
1021 /* "to_close" target method. Close the process record target. */
1024 record_full_close (int quitting)
1026 struct record_full_core_buf_entry *entry;
1029 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_close\n");
1031 record_full_list_release (record_full_list);
1033 /* Release record_full_core_regbuf. */
1034 if (record_full_core_regbuf)
1036 xfree (record_full_core_regbuf);
1037 record_full_core_regbuf = NULL;
1040 /* Release record_full_core_buf_list. */
1041 if (record_full_core_buf_list)
1043 for (entry = record_full_core_buf_list->prev; entry;
1044 entry = entry->prev)
1046 xfree (record_full_core_buf_list);
1047 record_full_core_buf_list = entry;
1049 record_full_core_buf_list = NULL;
1052 if (record_full_async_inferior_event_token)
1053 delete_async_event_handler (&record_full_async_inferior_event_token);
1056 static int record_full_resume_step = 0;
1058 /* True if we've been resumed, and so each record_full_wait call should
1059 advance execution. If this is false, record_full_wait will return a
1060 TARGET_WAITKIND_IGNORE. */
1061 static int record_full_resumed = 0;
1063 /* The execution direction of the last resume we got. This is
1064 necessary for async mode. Vis (order is not strictly accurate):
1066 1. user has the global execution direction set to forward
1067 2. user does a reverse-step command
1068 3. record_full_resume is called with global execution direction
1069 temporarily switched to reverse
1070 4. GDB's execution direction is reverted back to forward
1071 5. target record notifies event loop there's an event to handle
1072 6. infrun asks the target which direction was it going, and switches
1073 the global execution direction accordingly (to reverse)
1074 7. infrun polls an event out of the record target, and handles it
1075 8. GDB goes back to the event loop, and goto #4.
1077 static enum exec_direction_kind record_full_execution_dir = EXEC_FORWARD;
1079 /* "to_resume" target method. Resume the process record target. */
1082 record_full_resume (struct target_ops *ops, ptid_t ptid, int step,
1083 enum gdb_signal signal)
1085 record_full_resume_step = step;
1086 record_full_resumed = 1;
1087 record_full_execution_dir = execution_direction;
1089 if (!RECORD_FULL_IS_REPLAY)
1091 struct gdbarch *gdbarch = target_thread_architecture (ptid);
1093 record_full_message (get_current_regcache (), signal);
1097 /* This is not hard single step. */
1098 if (!gdbarch_software_single_step_p (gdbarch))
1100 /* This is a normal continue. */
1105 /* This arch support soft sigle step. */
1106 if (single_step_breakpoints_inserted ())
1108 /* This is a soft single step. */
1109 record_full_resume_step = 1;
1113 /* This is a continue.
1114 Try to insert a soft single step breakpoint. */
1115 if (!gdbarch_software_single_step (gdbarch,
1116 get_current_frame ()))
1118 /* This system don't want use soft single step.
1119 Use hard sigle step. */
1126 /* Make sure the target beneath reports all signals. */
1127 target_pass_signals (0, NULL);
1129 record_full_beneath_to_resume (record_full_beneath_to_resume_ops,
1130 ptid, step, signal);
1133 /* We are about to start executing the inferior (or simulate it),
1134 let's register it with the event loop. */
1135 if (target_can_async_p ())
1137 target_async (inferior_event_handler, 0);
1138 /* Notify the event loop there's an event to wait for. We do
1139 most of the work in record_full_wait. */
1140 mark_async_event_handler (record_full_async_inferior_event_token);
1144 static int record_full_get_sig = 0;
1146 /* SIGINT signal handler, registered by "to_wait" method. */
1149 record_full_sig_handler (int signo)
1152 fprintf_unfiltered (gdb_stdlog, "Process record: get a signal\n");
1154 /* It will break the running inferior in replay mode. */
1155 record_full_resume_step = 1;
1157 /* It will let record_full_wait set inferior status to get the signal
1159 record_full_get_sig = 1;
1163 record_full_wait_cleanups (void *ignore)
1165 if (execution_direction == EXEC_REVERSE)
1167 if (record_full_list->next)
1168 record_full_list = record_full_list->next;
1171 record_full_list = record_full_list->prev;
1174 /* "to_wait" target method for process record target.
1176 In record mode, the target is always run in singlestep mode
1177 (even when gdb says to continue). The to_wait method intercepts
1178 the stop events and determines which ones are to be passed on to
1179 gdb. Most stop events are just singlestep events that gdb is not
1180 to know about, so the to_wait method just records them and keeps
1183 In replay mode, this function emulates the recorded execution log,
1184 one instruction at a time (forward or backward), and determines
1188 record_full_wait_1 (struct target_ops *ops,
1189 ptid_t ptid, struct target_waitstatus *status,
1192 struct cleanup *set_cleanups = record_full_gdb_operation_disable_set ();
1195 fprintf_unfiltered (gdb_stdlog,
1196 "Process record: record_full_wait "
1197 "record_full_resume_step = %d, "
1198 "record_full_resumed = %d, direction=%s\n",
1199 record_full_resume_step, record_full_resumed,
1200 record_full_execution_dir == EXEC_FORWARD
1201 ? "forward" : "reverse");
1203 if (!record_full_resumed)
1205 gdb_assert ((options & TARGET_WNOHANG) != 0);
1207 /* No interesting event. */
1208 status->kind = TARGET_WAITKIND_IGNORE;
1209 return minus_one_ptid;
1212 record_full_get_sig = 0;
1213 signal (SIGINT, record_full_sig_handler);
1215 if (!RECORD_FULL_IS_REPLAY && ops != &record_full_core_ops)
1217 if (record_full_resume_step)
1219 /* This is a single step. */
1220 return record_full_beneath_to_wait (record_full_beneath_to_wait_ops,
1221 ptid, status, options);
1225 /* This is not a single step. */
1228 struct gdbarch *gdbarch = target_thread_architecture (inferior_ptid);
1232 ret = record_full_beneath_to_wait
1233 (record_full_beneath_to_wait_ops, ptid, status, options);
1234 if (status->kind == TARGET_WAITKIND_IGNORE)
1237 fprintf_unfiltered (gdb_stdlog,
1238 "Process record: record_full_wait "
1239 "target beneath not done yet\n");
1243 if (single_step_breakpoints_inserted ())
1244 remove_single_step_breakpoints ();
1246 if (record_full_resume_step)
1249 /* Is this a SIGTRAP? */
1250 if (status->kind == TARGET_WAITKIND_STOPPED
1251 && status->value.sig == GDB_SIGNAL_TRAP)
1253 struct regcache *regcache;
1254 struct address_space *aspace;
1256 /* Yes -- this is likely our single-step finishing,
1257 but check if there's any reason the core would be
1258 interested in the event. */
1260 registers_changed ();
1261 regcache = get_current_regcache ();
1262 tmp_pc = regcache_read_pc (regcache);
1263 aspace = get_regcache_aspace (regcache);
1265 if (target_stopped_by_watchpoint ())
1267 /* Always interested in watchpoints. */
1269 else if (breakpoint_inserted_here_p (aspace, tmp_pc))
1271 /* There is a breakpoint here. Let the core
1273 if (software_breakpoint_inserted_here_p (aspace, tmp_pc))
1275 struct gdbarch *gdbarch
1276 = get_regcache_arch (regcache);
1277 CORE_ADDR decr_pc_after_break
1278 = gdbarch_decr_pc_after_break (gdbarch);
1279 if (decr_pc_after_break)
1280 regcache_write_pc (regcache,
1281 tmp_pc + decr_pc_after_break);
1286 /* This is a single-step trap. Record the
1287 insn and issue another step.
1288 FIXME: this part can be a random SIGTRAP too.
1289 But GDB cannot handle it. */
1292 if (!record_full_message_wrapper_safe (regcache,
1295 status->kind = TARGET_WAITKIND_STOPPED;
1296 status->value.sig = GDB_SIGNAL_0;
1300 if (gdbarch_software_single_step_p (gdbarch))
1302 /* Try to insert the software single step breakpoint.
1303 If insert success, set step to 0. */
1304 set_executing (inferior_ptid, 0);
1305 reinit_frame_cache ();
1306 if (gdbarch_software_single_step (gdbarch,
1307 get_current_frame ()))
1309 set_executing (inferior_ptid, 1);
1313 fprintf_unfiltered (gdb_stdlog,
1314 "Process record: record_full_wait "
1315 "issuing one more step in the "
1316 "target beneath\n");
1317 record_full_beneath_to_resume
1318 (record_full_beneath_to_resume_ops, ptid, step,
1324 /* The inferior is broken by a breakpoint or a signal. */
1333 struct regcache *regcache = get_current_regcache ();
1334 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1335 struct address_space *aspace = get_regcache_aspace (regcache);
1336 int continue_flag = 1;
1337 int first_record_full_end = 1;
1338 struct cleanup *old_cleanups
1339 = make_cleanup (record_full_wait_cleanups, 0);
1342 record_full_hw_watchpoint = 0;
1343 status->kind = TARGET_WAITKIND_STOPPED;
1345 /* Check breakpoint when forward execute. */
1346 if (execution_direction == EXEC_FORWARD)
1348 tmp_pc = regcache_read_pc (regcache);
1349 if (breakpoint_inserted_here_p (aspace, tmp_pc))
1351 int decr_pc_after_break = gdbarch_decr_pc_after_break (gdbarch);
1354 fprintf_unfiltered (gdb_stdlog,
1355 "Process record: break at %s.\n",
1356 paddress (gdbarch, tmp_pc));
1358 if (decr_pc_after_break
1359 && !record_full_resume_step
1360 && software_breakpoint_inserted_here_p (aspace, tmp_pc))
1361 regcache_write_pc (regcache,
1362 tmp_pc + decr_pc_after_break);
1367 /* If GDB is in terminal_inferior mode, it will not get the signal.
1368 And in GDB replay mode, GDB doesn't need to be in terminal_inferior
1369 mode, because inferior will not executed.
1370 Then set it to terminal_ours to make GDB get the signal. */
1371 target_terminal_ours ();
1373 /* In EXEC_FORWARD mode, record_full_list points to the tail of prev
1375 if (execution_direction == EXEC_FORWARD && record_full_list->next)
1376 record_full_list = record_full_list->next;
1378 /* Loop over the record_full_list, looking for the next place to
1382 /* Check for beginning and end of log. */
1383 if (execution_direction == EXEC_REVERSE
1384 && record_full_list == &record_full_first)
1386 /* Hit beginning of record log in reverse. */
1387 status->kind = TARGET_WAITKIND_NO_HISTORY;
1390 if (execution_direction != EXEC_REVERSE && !record_full_list->next)
1392 /* Hit end of record log going forward. */
1393 status->kind = TARGET_WAITKIND_NO_HISTORY;
1397 record_full_exec_insn (regcache, gdbarch, record_full_list);
1399 if (record_full_list->type == record_full_end)
1401 if (record_debug > 1)
1402 fprintf_unfiltered (gdb_stdlog,
1403 "Process record: record_full_end %s to "
1405 host_address_to_string (record_full_list));
1407 if (first_record_full_end && execution_direction == EXEC_REVERSE)
1409 /* When reverse excute, the first record_full_end is the
1410 part of current instruction. */
1411 first_record_full_end = 0;
1415 /* In EXEC_REVERSE mode, this is the record_full_end of prev
1417 In EXEC_FORWARD mode, this is the record_full_end of
1418 current instruction. */
1420 if (record_full_resume_step)
1422 if (record_debug > 1)
1423 fprintf_unfiltered (gdb_stdlog,
1424 "Process record: step.\n");
1428 /* check breakpoint */
1429 tmp_pc = regcache_read_pc (regcache);
1430 if (breakpoint_inserted_here_p (aspace, tmp_pc))
1432 int decr_pc_after_break
1433 = gdbarch_decr_pc_after_break (gdbarch);
1436 fprintf_unfiltered (gdb_stdlog,
1437 "Process record: break "
1439 paddress (gdbarch, tmp_pc));
1440 if (decr_pc_after_break
1441 && execution_direction == EXEC_FORWARD
1442 && !record_full_resume_step
1443 && software_breakpoint_inserted_here_p (aspace,
1445 regcache_write_pc (regcache,
1446 tmp_pc + decr_pc_after_break);
1450 if (record_full_hw_watchpoint)
1453 fprintf_unfiltered (gdb_stdlog,
1454 "Process record: hit hw "
1458 /* Check target signal */
1459 if (record_full_list->u.end.sigval != GDB_SIGNAL_0)
1460 /* FIXME: better way to check */
1467 if (execution_direction == EXEC_REVERSE)
1469 if (record_full_list->prev)
1470 record_full_list = record_full_list->prev;
1474 if (record_full_list->next)
1475 record_full_list = record_full_list->next;
1479 while (continue_flag);
1482 if (record_full_get_sig)
1483 status->value.sig = GDB_SIGNAL_INT;
1484 else if (record_full_list->u.end.sigval != GDB_SIGNAL_0)
1485 /* FIXME: better way to check */
1486 status->value.sig = record_full_list->u.end.sigval;
1488 status->value.sig = GDB_SIGNAL_TRAP;
1490 discard_cleanups (old_cleanups);
1493 signal (SIGINT, handle_sigint);
1495 do_cleanups (set_cleanups);
1496 return inferior_ptid;
1500 record_full_wait (struct target_ops *ops,
1501 ptid_t ptid, struct target_waitstatus *status,
1506 return_ptid = record_full_wait_1 (ops, ptid, status, options);
1507 if (status->kind != TARGET_WAITKIND_IGNORE)
1509 /* We're reporting a stop. Make sure any spurious
1510 target_wait(WNOHANG) doesn't advance the target until the
1511 core wants us resumed again. */
1512 record_full_resumed = 0;
1518 record_full_stopped_by_watchpoint (void)
1520 if (RECORD_FULL_IS_REPLAY)
1521 return record_full_hw_watchpoint;
1523 return record_full_beneath_to_stopped_by_watchpoint ();
1526 /* "to_disconnect" method for process record target. */
1529 record_full_disconnect (struct target_ops *target, char *args, int from_tty)
1532 fprintf_unfiltered (gdb_stdlog,
1533 "Process record: record_full_disconnect\n");
1535 unpush_target (&record_full_ops);
1536 target_disconnect (args, from_tty);
1539 /* "to_detach" method for process record target. */
1542 record_full_detach (struct target_ops *ops, char *args, int from_tty)
1545 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_detach\n");
1547 unpush_target (&record_full_ops);
1548 target_detach (args, from_tty);
1551 /* "to_mourn_inferior" method for process record target. */
1554 record_full_mourn_inferior (struct target_ops *ops)
1557 fprintf_unfiltered (gdb_stdlog, "Process record: "
1558 "record_full_mourn_inferior\n");
1560 unpush_target (&record_full_ops);
1561 target_mourn_inferior ();
1564 /* Close process record target before killing the inferior process. */
1567 record_full_kill (struct target_ops *ops)
1570 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_kill\n");
1572 unpush_target (&record_full_ops);
1577 record_full_stopped_data_address (struct target_ops *ops, CORE_ADDR *addr_p)
1579 if (RECORD_FULL_IS_REPLAY)
1582 return record_full_beneath_to_stopped_data_address (ops, addr_p);
1585 /* Record registers change (by user or by GDB) to list as an instruction. */
1588 record_full_registers_change (struct regcache *regcache, int regnum)
1590 /* Check record_full_insn_num. */
1591 record_full_check_insn_num (0);
1593 record_full_arch_list_head = NULL;
1594 record_full_arch_list_tail = NULL;
1600 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
1602 if (record_full_arch_list_add_reg (regcache, i))
1604 record_full_list_release (record_full_arch_list_tail);
1605 error (_("Process record: failed to record execution log."));
1611 if (record_full_arch_list_add_reg (regcache, regnum))
1613 record_full_list_release (record_full_arch_list_tail);
1614 error (_("Process record: failed to record execution log."));
1617 if (record_full_arch_list_add_end ())
1619 record_full_list_release (record_full_arch_list_tail);
1620 error (_("Process record: failed to record execution log."));
1622 record_full_list->next = record_full_arch_list_head;
1623 record_full_arch_list_head->prev = record_full_list;
1624 record_full_list = record_full_arch_list_tail;
1626 if (record_full_insn_num == record_full_insn_max_num
1627 && record_full_insn_max_num)
1628 record_full_list_release_first ();
1630 record_full_insn_num++;
1633 /* "to_store_registers" method for process record target. */
1636 record_full_store_registers (struct target_ops *ops,
1637 struct regcache *regcache,
1640 if (!record_full_gdb_operation_disable)
1642 if (RECORD_FULL_IS_REPLAY)
1646 /* Let user choose if he wants to write register or not. */
1649 query (_("Because GDB is in replay mode, changing the "
1650 "value of a register will make the execution "
1651 "log unusable from this point onward. "
1652 "Change all registers?"));
1655 query (_("Because GDB is in replay mode, changing the value "
1656 "of a register will make the execution log unusable "
1657 "from this point onward. Change register %s?"),
1658 gdbarch_register_name (get_regcache_arch (regcache),
1663 /* Invalidate the value of regcache that was set in function
1664 "regcache_raw_write". */
1670 i < gdbarch_num_regs (get_regcache_arch (regcache));
1672 regcache_invalidate (regcache, i);
1675 regcache_invalidate (regcache, regno);
1677 error (_("Process record canceled the operation."));
1680 /* Destroy the record from here forward. */
1681 record_full_list_release_following (record_full_list);
1684 record_full_registers_change (regcache, regno);
1686 record_full_beneath_to_store_registers
1687 (record_full_beneath_to_store_registers_ops, regcache, regno);
1690 /* "to_xfer_partial" method. Behavior is conditional on
1691 RECORD_FULL_IS_REPLAY.
1692 In replay mode, we cannot write memory unles we are willing to
1693 invalidate the record/replay log from this point forward. */
1696 record_full_xfer_partial (struct target_ops *ops, enum target_object object,
1697 const char *annex, gdb_byte *readbuf,
1698 const gdb_byte *writebuf, ULONGEST offset,
1701 if (!record_full_gdb_operation_disable
1702 && (object == TARGET_OBJECT_MEMORY
1703 || object == TARGET_OBJECT_RAW_MEMORY) && writebuf)
1705 if (RECORD_FULL_IS_REPLAY)
1707 /* Let user choose if he wants to write memory or not. */
1708 if (!query (_("Because GDB is in replay mode, writing to memory "
1709 "will make the execution log unusable from this "
1710 "point onward. Write memory at address %s?"),
1711 paddress (target_gdbarch (), offset)))
1712 error (_("Process record canceled the operation."));
1714 /* Destroy the record from here forward. */
1715 record_full_list_release_following (record_full_list);
1718 /* Check record_full_insn_num */
1719 record_full_check_insn_num (0);
1721 /* Record registers change to list as an instruction. */
1722 record_full_arch_list_head = NULL;
1723 record_full_arch_list_tail = NULL;
1724 if (record_full_arch_list_add_mem (offset, len))
1726 record_full_list_release (record_full_arch_list_tail);
1728 fprintf_unfiltered (gdb_stdlog,
1729 "Process record: failed to record "
1733 if (record_full_arch_list_add_end ())
1735 record_full_list_release (record_full_arch_list_tail);
1737 fprintf_unfiltered (gdb_stdlog,
1738 "Process record: failed to record "
1742 record_full_list->next = record_full_arch_list_head;
1743 record_full_arch_list_head->prev = record_full_list;
1744 record_full_list = record_full_arch_list_tail;
1746 if (record_full_insn_num == record_full_insn_max_num
1747 && record_full_insn_max_num)
1748 record_full_list_release_first ();
1750 record_full_insn_num++;
1753 return record_full_beneath_to_xfer_partial
1754 (record_full_beneath_to_xfer_partial_ops, object, annex,
1755 readbuf, writebuf, offset, len);
1758 /* This structure represents a breakpoint inserted while the record
1759 target is active. We use this to know when to install/remove
1760 breakpoints in/from the target beneath. For example, a breakpoint
1761 may be inserted while recording, but removed when not replaying nor
1762 recording. In that case, the breakpoint had not been inserted on
1763 the target beneath, so we should not try to remove it there. */
1765 struct record_full_breakpoint
1767 /* The address and address space the breakpoint was set at. */
1768 struct address_space *address_space;
1771 /* True when the breakpoint has been also installed in the target
1772 beneath. This will be false for breakpoints set during replay or
1774 int in_target_beneath;
1777 typedef struct record_full_breakpoint *record_full_breakpoint_p;
1778 DEF_VEC_P(record_full_breakpoint_p);
1780 /* The list of breakpoints inserted while the record target is
1782 VEC(record_full_breakpoint_p) *record_full_breakpoints = NULL;
1785 record_full_sync_record_breakpoints (struct bp_location *loc, void *data)
1787 if (loc->loc_type != bp_loc_software_breakpoint)
1792 struct record_full_breakpoint *bp = XNEW (struct record_full_breakpoint);
1794 bp->addr = loc->target_info.placed_address;
1795 bp->address_space = loc->target_info.placed_address_space;
1797 bp->in_target_beneath = 1;
1799 VEC_safe_push (record_full_breakpoint_p, record_full_breakpoints, bp);
1803 /* Sync existing breakpoints to record_full_breakpoints. */
1806 record_full_init_record_breakpoints (void)
1808 VEC_free (record_full_breakpoint_p, record_full_breakpoints);
1810 iterate_over_bp_locations (record_full_sync_record_breakpoints);
1813 /* Behavior is conditional on RECORD_FULL_IS_REPLAY. We will not actually
1814 insert or remove breakpoints in the real target when replaying, nor
1818 record_full_insert_breakpoint (struct gdbarch *gdbarch,
1819 struct bp_target_info *bp_tgt)
1821 struct record_full_breakpoint *bp;
1822 int in_target_beneath = 0;
1824 if (!RECORD_FULL_IS_REPLAY)
1826 /* When recording, we currently always single-step, so we don't
1827 really need to install regular breakpoints in the inferior.
1828 However, we do have to insert software single-step
1829 breakpoints, in case the target can't hardware step. To keep
1830 things single, we always insert. */
1831 struct cleanup *old_cleanups;
1834 old_cleanups = record_full_gdb_operation_disable_set ();
1835 ret = record_full_beneath_to_insert_breakpoint (gdbarch, bp_tgt);
1836 do_cleanups (old_cleanups);
1841 in_target_beneath = 1;
1844 bp = XNEW (struct record_full_breakpoint);
1845 bp->addr = bp_tgt->placed_address;
1846 bp->address_space = bp_tgt->placed_address_space;
1847 bp->in_target_beneath = in_target_beneath;
1848 VEC_safe_push (record_full_breakpoint_p, record_full_breakpoints, bp);
1852 /* "to_remove_breakpoint" method for process record target. */
1855 record_full_remove_breakpoint (struct gdbarch *gdbarch,
1856 struct bp_target_info *bp_tgt)
1858 struct record_full_breakpoint *bp;
1862 VEC_iterate (record_full_breakpoint_p,
1863 record_full_breakpoints, ix, bp);
1866 if (bp->addr == bp_tgt->placed_address
1867 && bp->address_space == bp_tgt->placed_address_space)
1869 if (bp->in_target_beneath)
1871 struct cleanup *old_cleanups;
1874 old_cleanups = record_full_gdb_operation_disable_set ();
1875 ret = record_full_beneath_to_remove_breakpoint (gdbarch, bp_tgt);
1876 do_cleanups (old_cleanups);
1882 VEC_unordered_remove (record_full_breakpoint_p,
1883 record_full_breakpoints, ix);
1888 gdb_assert_not_reached ("removing unknown breakpoint");
1891 /* "to_can_execute_reverse" method for process record target. */
1894 record_full_can_execute_reverse (void)
1899 /* "to_get_bookmark" method for process record and prec over core. */
1902 record_full_get_bookmark (char *args, int from_tty)
1904 gdb_byte *ret = NULL;
1906 /* Return stringified form of instruction count. */
1907 if (record_full_list && record_full_list->type == record_full_end)
1908 ret = xstrdup (pulongest (record_full_list->u.end.insn_num));
1913 fprintf_unfiltered (gdb_stdlog,
1914 "record_full_get_bookmark returns %s\n", ret);
1916 fprintf_unfiltered (gdb_stdlog,
1917 "record_full_get_bookmark returns NULL\n");
1922 /* "to_goto_bookmark" method for process record and prec over core. */
1925 record_full_goto_bookmark (gdb_byte *bookmark, int from_tty)
1928 fprintf_unfiltered (gdb_stdlog,
1929 "record_full_goto_bookmark receives %s\n", bookmark);
1931 if (bookmark[0] == '\'' || bookmark[0] == '\"')
1933 if (bookmark[strlen (bookmark) - 1] != bookmark[0])
1934 error (_("Unbalanced quotes: %s"), bookmark);
1936 /* Strip trailing quote. */
1937 bookmark[strlen (bookmark) - 1] = '\0';
1938 /* Strip leading quote. */
1940 /* Pass along to cmd_record_full_goto. */
1943 cmd_record_goto ((char *) bookmark, from_tty);
1948 record_full_async (void (*callback) (enum inferior_event_type event_type,
1949 void *context), void *context)
1951 /* If we're on top of a line target (e.g., linux-nat, remote), then
1952 set it to async mode as well. Will be NULL if we're sitting on
1953 top of the core target, for "record restore". */
1954 if (record_full_beneath_to_async != NULL)
1955 record_full_beneath_to_async (callback, context);
1959 record_full_can_async_p (void)
1961 /* We only enable async when the user specifically asks for it. */
1962 return target_async_permitted;
1966 record_full_is_async_p (void)
1968 /* We only enable async when the user specifically asks for it. */
1969 return target_async_permitted;
1972 static enum exec_direction_kind
1973 record_full_execution_direction (void)
1975 return record_full_execution_dir;
1979 record_full_info (void)
1981 struct record_full_entry *p;
1983 if (RECORD_FULL_IS_REPLAY)
1984 printf_filtered (_("Replay mode:\n"));
1986 printf_filtered (_("Record mode:\n"));
1988 /* Find entry for first actual instruction in the log. */
1989 for (p = record_full_first.next;
1990 p != NULL && p->type != record_full_end;
1994 /* Do we have a log at all? */
1995 if (p != NULL && p->type == record_full_end)
1997 /* Display instruction number for first instruction in the log. */
1998 printf_filtered (_("Lowest recorded instruction number is %s.\n"),
1999 pulongest (p->u.end.insn_num));
2001 /* If in replay mode, display where we are in the log. */
2002 if (RECORD_FULL_IS_REPLAY)
2003 printf_filtered (_("Current instruction number is %s.\n"),
2004 pulongest (record_full_list->u.end.insn_num));
2006 /* Display instruction number for last instruction in the log. */
2007 printf_filtered (_("Highest recorded instruction number is %s.\n"),
2008 pulongest (record_full_insn_count));
2010 /* Display log count. */
2011 printf_filtered (_("Log contains %d instructions.\n"),
2012 record_full_insn_num);
2015 printf_filtered (_("No instructions have been logged.\n"));
2017 /* Display max log size. */
2018 printf_filtered (_("Max logged instructions is %d.\n"),
2019 record_full_insn_max_num);
2022 /* The "to_record_delete" target method. */
2025 record_full_delete (void)
2027 record_full_list_release_following (record_full_list);
2030 /* The "to_record_is_replaying" target method. */
2033 record_full_is_replaying (void)
2035 return RECORD_FULL_IS_REPLAY;
2038 /* Go to a specific entry. */
2041 record_full_goto_entry (struct record_full_entry *p)
2044 error (_("Target insn not found."));
2045 else if (p == record_full_list)
2046 error (_("Already at target insn."));
2047 else if (p->u.end.insn_num > record_full_list->u.end.insn_num)
2049 printf_filtered (_("Go forward to insn number %s\n"),
2050 pulongest (p->u.end.insn_num));
2051 record_full_goto_insn (p, EXEC_FORWARD);
2055 printf_filtered (_("Go backward to insn number %s\n"),
2056 pulongest (p->u.end.insn_num));
2057 record_full_goto_insn (p, EXEC_REVERSE);
2060 registers_changed ();
2061 reinit_frame_cache ();
2062 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
2065 /* The "to_goto_record_begin" target method. */
2068 record_full_goto_begin (void)
2070 struct record_full_entry *p = NULL;
2072 for (p = &record_full_first; p != NULL; p = p->next)
2073 if (p->type == record_full_end)
2076 record_full_goto_entry (p);
2079 /* The "to_goto_record_end" target method. */
2082 record_full_goto_end (void)
2084 struct record_full_entry *p = NULL;
2086 for (p = record_full_list; p->next != NULL; p = p->next)
2088 for (; p!= NULL; p = p->prev)
2089 if (p->type == record_full_end)
2092 record_full_goto_entry (p);
2095 /* The "to_goto_record" target method. */
2098 record_full_goto (ULONGEST target_insn)
2100 struct record_full_entry *p = NULL;
2102 for (p = &record_full_first; p != NULL; p = p->next)
2103 if (p->type == record_full_end && p->u.end.insn_num == target_insn)
2106 record_full_goto_entry (p);
2110 init_record_full_ops (void)
2112 record_full_ops.to_shortname = "record-full";
2113 record_full_ops.to_longname = "Process record and replay target";
2114 record_full_ops.to_doc =
2115 "Log program while executing and replay execution from log.";
2116 record_full_ops.to_open = record_full_open;
2117 record_full_ops.to_close = record_full_close;
2118 record_full_ops.to_resume = record_full_resume;
2119 record_full_ops.to_wait = record_full_wait;
2120 record_full_ops.to_disconnect = record_full_disconnect;
2121 record_full_ops.to_detach = record_full_detach;
2122 record_full_ops.to_mourn_inferior = record_full_mourn_inferior;
2123 record_full_ops.to_kill = record_full_kill;
2124 record_full_ops.to_create_inferior = find_default_create_inferior;
2125 record_full_ops.to_store_registers = record_full_store_registers;
2126 record_full_ops.to_xfer_partial = record_full_xfer_partial;
2127 record_full_ops.to_insert_breakpoint = record_full_insert_breakpoint;
2128 record_full_ops.to_remove_breakpoint = record_full_remove_breakpoint;
2129 record_full_ops.to_stopped_by_watchpoint = record_full_stopped_by_watchpoint;
2130 record_full_ops.to_stopped_data_address = record_full_stopped_data_address;
2131 record_full_ops.to_can_execute_reverse = record_full_can_execute_reverse;
2132 record_full_ops.to_stratum = record_stratum;
2133 /* Add bookmark target methods. */
2134 record_full_ops.to_get_bookmark = record_full_get_bookmark;
2135 record_full_ops.to_goto_bookmark = record_full_goto_bookmark;
2136 record_full_ops.to_async = record_full_async;
2137 record_full_ops.to_can_async_p = record_full_can_async_p;
2138 record_full_ops.to_is_async_p = record_full_is_async_p;
2139 record_full_ops.to_execution_direction = record_full_execution_direction;
2140 record_full_ops.to_info_record = record_full_info;
2141 record_full_ops.to_save_record = record_full_save;
2142 record_full_ops.to_delete_record = record_full_delete;
2143 record_full_ops.to_record_is_replaying = record_full_is_replaying;
2144 record_full_ops.to_goto_record_begin = record_full_goto_begin;
2145 record_full_ops.to_goto_record_end = record_full_goto_end;
2146 record_full_ops.to_goto_record = record_full_goto;
2147 record_full_ops.to_magic = OPS_MAGIC;
2150 /* "to_resume" method for prec over corefile. */
2153 record_full_core_resume (struct target_ops *ops, ptid_t ptid, int step,
2154 enum gdb_signal signal)
2156 record_full_resume_step = step;
2157 record_full_resumed = 1;
2158 record_full_execution_dir = execution_direction;
2160 /* We are about to start executing the inferior (or simulate it),
2161 let's register it with the event loop. */
2162 if (target_can_async_p ())
2164 target_async (inferior_event_handler, 0);
2166 /* Notify the event loop there's an event to wait for. */
2167 mark_async_event_handler (record_full_async_inferior_event_token);
2171 /* "to_kill" method for prec over corefile. */
2174 record_full_core_kill (struct target_ops *ops)
2177 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_core_kill\n");
2179 unpush_target (&record_full_core_ops);
2182 /* "to_fetch_registers" method for prec over corefile. */
2185 record_full_core_fetch_registers (struct target_ops *ops,
2186 struct regcache *regcache,
2191 int num = gdbarch_num_regs (get_regcache_arch (regcache));
2194 for (i = 0; i < num; i ++)
2195 regcache_raw_supply (regcache, i,
2196 record_full_core_regbuf + MAX_REGISTER_SIZE * i);
2199 regcache_raw_supply (regcache, regno,
2200 record_full_core_regbuf + MAX_REGISTER_SIZE * regno);
2203 /* "to_prepare_to_store" method for prec over corefile. */
2206 record_full_core_prepare_to_store (struct regcache *regcache)
2210 /* "to_store_registers" method for prec over corefile. */
2213 record_full_core_store_registers (struct target_ops *ops,
2214 struct regcache *regcache,
2217 if (record_full_gdb_operation_disable)
2218 regcache_raw_collect (regcache, regno,
2219 record_full_core_regbuf + MAX_REGISTER_SIZE * regno);
2221 error (_("You can't do that without a process to debug."));
2224 /* "to_xfer_partial" method for prec over corefile. */
2227 record_full_core_xfer_partial (struct target_ops *ops,
2228 enum target_object object,
2229 const char *annex, gdb_byte *readbuf,
2230 const gdb_byte *writebuf, ULONGEST offset,
2233 if (object == TARGET_OBJECT_MEMORY)
2235 if (record_full_gdb_operation_disable || !writebuf)
2237 struct target_section *p;
2239 for (p = record_full_core_start; p < record_full_core_end; p++)
2241 if (offset >= p->addr)
2243 struct record_full_core_buf_entry *entry;
2244 ULONGEST sec_offset;
2246 if (offset >= p->endaddr)
2249 if (offset + len > p->endaddr)
2250 len = p->endaddr - offset;
2252 sec_offset = offset - p->addr;
2254 /* Read readbuf or write writebuf p, offset, len. */
2256 if (p->the_bfd_section->flags & SEC_CONSTRUCTOR
2257 || (p->the_bfd_section->flags & SEC_HAS_CONTENTS) == 0)
2260 memset (readbuf, 0, len);
2263 /* Get record_full_core_buf_entry. */
2264 for (entry = record_full_core_buf_list; entry;
2265 entry = entry->prev)
2272 /* Add a new entry. */
2273 entry = (struct record_full_core_buf_entry *)
2275 (sizeof (struct record_full_core_buf_entry));
2277 if (!bfd_malloc_and_get_section (p->bfd,
2284 entry->prev = record_full_core_buf_list;
2285 record_full_core_buf_list = entry;
2288 memcpy (entry->buf + sec_offset, writebuf,
2294 return record_full_beneath_to_xfer_partial
2295 (record_full_beneath_to_xfer_partial_ops,
2296 object, annex, readbuf, writebuf,
2299 memcpy (readbuf, entry->buf + sec_offset,
2310 error (_("You can't do that without a process to debug."));
2313 return record_full_beneath_to_xfer_partial
2314 (record_full_beneath_to_xfer_partial_ops, object, annex,
2315 readbuf, writebuf, offset, len);
2318 /* "to_insert_breakpoint" method for prec over corefile. */
2321 record_full_core_insert_breakpoint (struct gdbarch *gdbarch,
2322 struct bp_target_info *bp_tgt)
2327 /* "to_remove_breakpoint" method for prec over corefile. */
2330 record_full_core_remove_breakpoint (struct gdbarch *gdbarch,
2331 struct bp_target_info *bp_tgt)
2336 /* "to_has_execution" method for prec over corefile. */
2339 record_full_core_has_execution (struct target_ops *ops, ptid_t the_ptid)
2345 init_record_full_core_ops (void)
2347 record_full_core_ops.to_shortname = "record-core";
2348 record_full_core_ops.to_longname = "Process record and replay target";
2349 record_full_core_ops.to_doc =
2350 "Log program while executing and replay execution from log.";
2351 record_full_core_ops.to_open = record_full_open;
2352 record_full_core_ops.to_close = record_full_close;
2353 record_full_core_ops.to_resume = record_full_core_resume;
2354 record_full_core_ops.to_wait = record_full_wait;
2355 record_full_core_ops.to_kill = record_full_core_kill;
2356 record_full_core_ops.to_fetch_registers = record_full_core_fetch_registers;
2357 record_full_core_ops.to_prepare_to_store = record_full_core_prepare_to_store;
2358 record_full_core_ops.to_store_registers = record_full_core_store_registers;
2359 record_full_core_ops.to_xfer_partial = record_full_core_xfer_partial;
2360 record_full_core_ops.to_insert_breakpoint
2361 = record_full_core_insert_breakpoint;
2362 record_full_core_ops.to_remove_breakpoint
2363 = record_full_core_remove_breakpoint;
2364 record_full_core_ops.to_stopped_by_watchpoint
2365 = record_full_stopped_by_watchpoint;
2366 record_full_core_ops.to_stopped_data_address
2367 = record_full_stopped_data_address;
2368 record_full_core_ops.to_can_execute_reverse
2369 = record_full_can_execute_reverse;
2370 record_full_core_ops.to_has_execution = record_full_core_has_execution;
2371 record_full_core_ops.to_stratum = record_stratum;
2372 /* Add bookmark target methods. */
2373 record_full_core_ops.to_get_bookmark = record_full_get_bookmark;
2374 record_full_core_ops.to_goto_bookmark = record_full_goto_bookmark;
2375 record_full_core_ops.to_async = record_full_async;
2376 record_full_core_ops.to_can_async_p = record_full_can_async_p;
2377 record_full_core_ops.to_is_async_p = record_full_is_async_p;
2378 record_full_core_ops.to_execution_direction
2379 = record_full_execution_direction;
2380 record_full_core_ops.to_info_record = record_full_info;
2381 record_full_core_ops.to_delete_record = record_full_delete;
2382 record_full_core_ops.to_record_is_replaying = record_full_is_replaying;
2383 record_full_core_ops.to_goto_record_begin = record_full_goto_begin;
2384 record_full_core_ops.to_goto_record_end = record_full_goto_end;
2385 record_full_core_ops.to_goto_record = record_full_goto;
2386 record_full_core_ops.to_magic = OPS_MAGIC;
2389 /* Record log save-file format
2390 Version 1 (never released)
2393 4 bytes: magic number htonl(0x20090829).
2394 NOTE: be sure to change whenever this file format changes!
2398 1 byte: record type (record_full_end, see enum record_full_type).
2400 1 byte: record type (record_full_reg, see enum record_full_type).
2401 8 bytes: register id (network byte order).
2402 MAX_REGISTER_SIZE bytes: register value.
2404 1 byte: record type (record_full_mem, see enum record_full_type).
2405 8 bytes: memory length (network byte order).
2406 8 bytes: memory address (network byte order).
2407 n bytes: memory value (n == memory length).
2410 4 bytes: magic number netorder32(0x20091016).
2411 NOTE: be sure to change whenever this file format changes!
2415 1 byte: record type (record_full_end, see enum record_full_type).
2417 4 bytes: instruction count
2419 1 byte: record type (record_full_reg, see enum record_full_type).
2420 4 bytes: register id (network byte order).
2421 n bytes: register value (n == actual register size).
2422 (eg. 4 bytes for x86 general registers).
2424 1 byte: record type (record_full_mem, see enum record_full_type).
2425 4 bytes: memory length (network byte order).
2426 8 bytes: memory address (network byte order).
2427 n bytes: memory value (n == memory length).
2431 /* bfdcore_read -- read bytes from a core file section. */
2434 bfdcore_read (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2436 int ret = bfd_get_section_contents (obfd, osec, buf, *offset, len);
2441 error (_("Failed to read %d bytes from core file %s ('%s')."),
2442 len, bfd_get_filename (obfd),
2443 bfd_errmsg (bfd_get_error ()));
2446 static inline uint64_t
2447 netorder64 (uint64_t input)
2451 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2452 BFD_ENDIAN_BIG, input);
2456 static inline uint32_t
2457 netorder32 (uint32_t input)
2461 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2462 BFD_ENDIAN_BIG, input);
2466 static inline uint16_t
2467 netorder16 (uint16_t input)
2471 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2472 BFD_ENDIAN_BIG, input);
2476 /* Restore the execution log from a core_bfd file. */
2478 record_full_restore (void)
2481 struct cleanup *old_cleanups;
2482 struct record_full_entry *rec;
2486 struct regcache *regcache;
2488 /* We restore the execution log from the open core bfd,
2490 if (core_bfd == NULL)
2493 /* "record_full_restore" can only be called when record list is empty. */
2494 gdb_assert (record_full_first.next == NULL);
2497 fprintf_unfiltered (gdb_stdlog, "Restoring recording from core file.\n");
2499 /* Now need to find our special note section. */
2500 osec = bfd_get_section_by_name (core_bfd, "null0");
2502 fprintf_unfiltered (gdb_stdlog, "Find precord section %s.\n",
2503 osec ? "succeeded" : "failed");
2506 osec_size = bfd_section_size (core_bfd, osec);
2508 fprintf_unfiltered (gdb_stdlog, "%s", bfd_section_name (core_bfd, osec));
2510 /* Check the magic code. */
2511 bfdcore_read (core_bfd, osec, &magic, sizeof (magic), &bfd_offset);
2512 if (magic != RECORD_FULL_FILE_MAGIC)
2513 error (_("Version mis-match or file format error in core file %s."),
2514 bfd_get_filename (core_bfd));
2516 fprintf_unfiltered (gdb_stdlog,
2517 " Reading 4-byte magic cookie "
2518 "RECORD_FULL_FILE_MAGIC (0x%s)\n",
2519 phex_nz (netorder32 (magic), 4));
2521 /* Restore the entries in recfd into record_full_arch_list_head and
2522 record_full_arch_list_tail. */
2523 record_full_arch_list_head = NULL;
2524 record_full_arch_list_tail = NULL;
2525 record_full_insn_num = 0;
2526 old_cleanups = make_cleanup (record_full_arch_list_cleanups, 0);
2527 regcache = get_current_regcache ();
2532 uint32_t regnum, len, signal, count;
2535 /* We are finished when offset reaches osec_size. */
2536 if (bfd_offset >= osec_size)
2538 bfdcore_read (core_bfd, osec, &rectype, sizeof (rectype), &bfd_offset);
2542 case record_full_reg: /* reg */
2543 /* Get register number to regnum. */
2544 bfdcore_read (core_bfd, osec, ®num,
2545 sizeof (regnum), &bfd_offset);
2546 regnum = netorder32 (regnum);
2548 rec = record_full_reg_alloc (regcache, regnum);
2551 bfdcore_read (core_bfd, osec, record_full_get_loc (rec),
2552 rec->u.reg.len, &bfd_offset);
2555 fprintf_unfiltered (gdb_stdlog,
2556 " Reading register %d (1 "
2557 "plus %lu plus %d bytes)\n",
2559 (unsigned long) sizeof (regnum),
2563 case record_full_mem: /* mem */
2565 bfdcore_read (core_bfd, osec, &len,
2566 sizeof (len), &bfd_offset);
2567 len = netorder32 (len);
2570 bfdcore_read (core_bfd, osec, &addr,
2571 sizeof (addr), &bfd_offset);
2572 addr = netorder64 (addr);
2574 rec = record_full_mem_alloc (addr, len);
2577 bfdcore_read (core_bfd, osec, record_full_get_loc (rec),
2578 rec->u.mem.len, &bfd_offset);
2581 fprintf_unfiltered (gdb_stdlog,
2582 " Reading memory %s (1 plus "
2583 "%lu plus %lu plus %d bytes)\n",
2584 paddress (get_current_arch (),
2586 (unsigned long) sizeof (addr),
2587 (unsigned long) sizeof (len),
2591 case record_full_end: /* end */
2592 rec = record_full_end_alloc ();
2593 record_full_insn_num ++;
2595 /* Get signal value. */
2596 bfdcore_read (core_bfd, osec, &signal,
2597 sizeof (signal), &bfd_offset);
2598 signal = netorder32 (signal);
2599 rec->u.end.sigval = signal;
2601 /* Get insn count. */
2602 bfdcore_read (core_bfd, osec, &count,
2603 sizeof (count), &bfd_offset);
2604 count = netorder32 (count);
2605 rec->u.end.insn_num = count;
2606 record_full_insn_count = count + 1;
2608 fprintf_unfiltered (gdb_stdlog,
2609 " Reading record_full_end (1 + "
2610 "%lu + %lu bytes), offset == %s\n",
2611 (unsigned long) sizeof (signal),
2612 (unsigned long) sizeof (count),
2613 paddress (get_current_arch (),
2618 error (_("Bad entry type in core file %s."),
2619 bfd_get_filename (core_bfd));
2623 /* Add rec to record arch list. */
2624 record_full_arch_list_add (rec);
2627 discard_cleanups (old_cleanups);
2629 /* Add record_full_arch_list_head to the end of record list. */
2630 record_full_first.next = record_full_arch_list_head;
2631 record_full_arch_list_head->prev = &record_full_first;
2632 record_full_arch_list_tail->next = NULL;
2633 record_full_list = &record_full_first;
2635 /* Update record_full_insn_max_num. */
2636 if (record_full_insn_num > record_full_insn_max_num)
2638 record_full_insn_max_num = record_full_insn_num;
2639 warning (_("Auto increase record/replay buffer limit to %d."),
2640 record_full_insn_max_num);
2644 printf_filtered (_("Restored records from core file %s.\n"),
2645 bfd_get_filename (core_bfd));
2647 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
2650 /* bfdcore_write -- write bytes into a core file section. */
2653 bfdcore_write (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2655 int ret = bfd_set_section_contents (obfd, osec, buf, *offset, len);
2660 error (_("Failed to write %d bytes to core file %s ('%s')."),
2661 len, bfd_get_filename (obfd),
2662 bfd_errmsg (bfd_get_error ()));
2665 /* Restore the execution log from a file. We use a modified elf
2666 corefile format, with an extra section for our data. */
2669 cmd_record_full_restore (char *args, int from_tty)
2671 core_file_command (args, from_tty);
2672 record_full_open (args, from_tty);
2676 record_full_save_cleanups (void *data)
2679 char *pathname = xstrdup (bfd_get_filename (obfd));
2681 gdb_bfd_unref (obfd);
2686 /* Save the execution log to a file. We use a modified elf corefile
2687 format, with an extra section for our data. */
2690 record_full_save (char *recfilename)
2692 struct record_full_entry *cur_record_full_list;
2694 struct regcache *regcache;
2695 struct gdbarch *gdbarch;
2696 struct cleanup *old_cleanups;
2697 struct cleanup *set_cleanups;
2700 asection *osec = NULL;
2703 /* Open the save file. */
2705 fprintf_unfiltered (gdb_stdlog, "Saving execution log to core file '%s'\n",
2708 /* Open the output file. */
2709 obfd = create_gcore_bfd (recfilename);
2710 old_cleanups = make_cleanup (record_full_save_cleanups, obfd);
2712 /* Save the current record entry to "cur_record_full_list". */
2713 cur_record_full_list = record_full_list;
2715 /* Get the values of regcache and gdbarch. */
2716 regcache = get_current_regcache ();
2717 gdbarch = get_regcache_arch (regcache);
2719 /* Disable the GDB operation record. */
2720 set_cleanups = record_full_gdb_operation_disable_set ();
2722 /* Reverse execute to the begin of record list. */
2725 /* Check for beginning and end of log. */
2726 if (record_full_list == &record_full_first)
2729 record_full_exec_insn (regcache, gdbarch, record_full_list);
2731 if (record_full_list->prev)
2732 record_full_list = record_full_list->prev;
2735 /* Compute the size needed for the extra bfd section. */
2736 save_size = 4; /* magic cookie */
2737 for (record_full_list = record_full_first.next; record_full_list;
2738 record_full_list = record_full_list->next)
2739 switch (record_full_list->type)
2741 case record_full_end:
2742 save_size += 1 + 4 + 4;
2744 case record_full_reg:
2745 save_size += 1 + 4 + record_full_list->u.reg.len;
2747 case record_full_mem:
2748 save_size += 1 + 4 + 8 + record_full_list->u.mem.len;
2752 /* Make the new bfd section. */
2753 osec = bfd_make_section_anyway_with_flags (obfd, "precord",
2757 error (_("Failed to create 'precord' section for corefile %s: %s"),
2759 bfd_errmsg (bfd_get_error ()));
2760 bfd_set_section_size (obfd, osec, save_size);
2761 bfd_set_section_vma (obfd, osec, 0);
2762 bfd_set_section_alignment (obfd, osec, 0);
2763 bfd_section_lma (obfd, osec) = 0;
2765 /* Save corefile state. */
2766 write_gcore_file (obfd);
2768 /* Write out the record log. */
2769 /* Write the magic code. */
2770 magic = RECORD_FULL_FILE_MAGIC;
2772 fprintf_unfiltered (gdb_stdlog,
2773 " Writing 4-byte magic cookie "
2774 "RECORD_FULL_FILE_MAGIC (0x%s)\n",
2775 phex_nz (magic, 4));
2776 bfdcore_write (obfd, osec, &magic, sizeof (magic), &bfd_offset);
2778 /* Save the entries to recfd and forward execute to the end of
2780 record_full_list = &record_full_first;
2784 if (record_full_list != &record_full_first)
2787 uint32_t regnum, len, signal, count;
2790 type = record_full_list->type;
2791 bfdcore_write (obfd, osec, &type, sizeof (type), &bfd_offset);
2793 switch (record_full_list->type)
2795 case record_full_reg: /* reg */
2797 fprintf_unfiltered (gdb_stdlog,
2798 " Writing register %d (1 "
2799 "plus %lu plus %d bytes)\n",
2800 record_full_list->u.reg.num,
2801 (unsigned long) sizeof (regnum),
2802 record_full_list->u.reg.len);
2805 regnum = netorder32 (record_full_list->u.reg.num);
2806 bfdcore_write (obfd, osec, ®num,
2807 sizeof (regnum), &bfd_offset);
2810 bfdcore_write (obfd, osec,
2811 record_full_get_loc (record_full_list),
2812 record_full_list->u.reg.len, &bfd_offset);
2815 case record_full_mem: /* mem */
2817 fprintf_unfiltered (gdb_stdlog,
2818 " Writing memory %s (1 plus "
2819 "%lu plus %lu plus %d bytes)\n",
2821 record_full_list->u.mem.addr),
2822 (unsigned long) sizeof (addr),
2823 (unsigned long) sizeof (len),
2824 record_full_list->u.mem.len);
2827 len = netorder32 (record_full_list->u.mem.len);
2828 bfdcore_write (obfd, osec, &len, sizeof (len), &bfd_offset);
2830 /* Write memaddr. */
2831 addr = netorder64 (record_full_list->u.mem.addr);
2832 bfdcore_write (obfd, osec, &addr,
2833 sizeof (addr), &bfd_offset);
2836 bfdcore_write (obfd, osec,
2837 record_full_get_loc (record_full_list),
2838 record_full_list->u.mem.len, &bfd_offset);
2841 case record_full_end:
2843 fprintf_unfiltered (gdb_stdlog,
2844 " Writing record_full_end (1 + "
2845 "%lu + %lu bytes)\n",
2846 (unsigned long) sizeof (signal),
2847 (unsigned long) sizeof (count));
2848 /* Write signal value. */
2849 signal = netorder32 (record_full_list->u.end.sigval);
2850 bfdcore_write (obfd, osec, &signal,
2851 sizeof (signal), &bfd_offset);
2853 /* Write insn count. */
2854 count = netorder32 (record_full_list->u.end.insn_num);
2855 bfdcore_write (obfd, osec, &count,
2856 sizeof (count), &bfd_offset);
2861 /* Execute entry. */
2862 record_full_exec_insn (regcache, gdbarch, record_full_list);
2864 if (record_full_list->next)
2865 record_full_list = record_full_list->next;
2870 /* Reverse execute to cur_record_full_list. */
2873 /* Check for beginning and end of log. */
2874 if (record_full_list == cur_record_full_list)
2877 record_full_exec_insn (regcache, gdbarch, record_full_list);
2879 if (record_full_list->prev)
2880 record_full_list = record_full_list->prev;
2883 do_cleanups (set_cleanups);
2884 gdb_bfd_unref (obfd);
2885 discard_cleanups (old_cleanups);
2888 printf_filtered (_("Saved core file %s with execution log.\n"),
2892 /* record_full_goto_insn -- rewind the record log (forward or backward,
2893 depending on DIR) to the given entry, changing the program state
2897 record_full_goto_insn (struct record_full_entry *entry,
2898 enum exec_direction_kind dir)
2900 struct cleanup *set_cleanups = record_full_gdb_operation_disable_set ();
2901 struct regcache *regcache = get_current_regcache ();
2902 struct gdbarch *gdbarch = get_regcache_arch (regcache);
2904 /* Assume everything is valid: we will hit the entry,
2905 and we will not hit the end of the recording. */
2907 if (dir == EXEC_FORWARD)
2908 record_full_list = record_full_list->next;
2912 record_full_exec_insn (regcache, gdbarch, record_full_list);
2913 if (dir == EXEC_REVERSE)
2914 record_full_list = record_full_list->prev;
2916 record_full_list = record_full_list->next;
2917 } while (record_full_list != entry);
2918 do_cleanups (set_cleanups);
2921 /* Alias for "target record-full". */
2924 cmd_record_full_start (char *args, int from_tty)
2926 execute_command ("target record-full", from_tty);
2930 set_record_full_insn_max_num (char *args, int from_tty,
2931 struct cmd_list_element *c)
2933 if (record_full_insn_num > record_full_insn_max_num
2934 && record_full_insn_max_num)
2936 /* Count down record_full_insn_num while releasing records from list. */
2937 while (record_full_insn_num > record_full_insn_max_num)
2939 record_full_list_release_first ();
2940 record_full_insn_num--;
2945 /* The "set record full" command. */
2948 set_record_full_command (char *args, int from_tty)
2950 printf_unfiltered (_("\"set record full\" must be followed "
2951 "by an apporpriate subcommand.\n"));
2952 help_list (set_record_full_cmdlist, "set record full ", all_commands,
2956 /* The "show record full" command. */
2959 show_record_full_command (char *args, int from_tty)
2961 cmd_show_list (show_record_full_cmdlist, from_tty, "");
2964 /* Provide a prototype to silence -Wmissing-prototypes. */
2965 extern initialize_file_ftype _initialize_record_full;
2968 _initialize_record_full (void)
2970 struct cmd_list_element *c;
2972 /* Init record_full_first. */
2973 record_full_first.prev = NULL;
2974 record_full_first.next = NULL;
2975 record_full_first.type = record_full_end;
2977 init_record_full_ops ();
2978 add_target (&record_full_ops);
2979 add_deprecated_target_alias (&record_full_ops, "record");
2980 init_record_full_core_ops ();
2981 add_target (&record_full_core_ops);
2983 add_prefix_cmd ("full", class_obscure, cmd_record_full_start,
2984 _("Start full execution recording."), &record_full_cmdlist,
2985 "record full ", 0, &record_cmdlist);
2987 c = add_cmd ("restore", class_obscure, cmd_record_full_restore,
2988 _("Restore the execution log from a file.\n\
2989 Argument is filename. File must be created with 'record save'."),
2990 &record_full_cmdlist);
2991 set_cmd_completer (c, filename_completer);
2993 /* Deprecate the old version without "full" prefix. */
2994 c = add_alias_cmd ("restore", "full restore", class_obscure, 1,
2996 set_cmd_completer (c, filename_completer);
2997 deprecate_cmd (c, "record full restore");
2999 add_prefix_cmd ("full", class_support, set_record_full_command,
3000 _("Set record options"), &set_record_full_cmdlist,
3001 "set record full ", 0, &set_record_cmdlist);
3003 add_prefix_cmd ("full", class_support, show_record_full_command,
3004 _("Show record options"), &show_record_full_cmdlist,
3005 "show record full ", 0, &show_record_cmdlist);
3007 /* Record instructions number limit command. */
3008 add_setshow_boolean_cmd ("stop-at-limit", no_class,
3009 &record_full_stop_at_limit, _("\
3010 Set whether record/replay stops when record/replay buffer becomes full."), _("\
3011 Show whether record/replay stops when record/replay buffer becomes full."),
3012 _("Default is ON.\n\
3013 When ON, if the record/replay buffer becomes full, ask user what to do.\n\
3014 When OFF, if the record/replay buffer becomes full,\n\
3015 delete the oldest recorded instruction to make room for each new one."),
3017 &set_record_full_cmdlist, &show_record_full_cmdlist);
3019 c = add_alias_cmd ("stop-at-limit", "full stop-at-limit", no_class, 1,
3020 &set_record_cmdlist);
3021 deprecate_cmd (c, "set record full stop-at-limit");
3023 c = add_alias_cmd ("stop-at-limit", "full stop-at-limit", no_class, 1,
3024 &show_record_cmdlist);
3025 deprecate_cmd (c, "show record full stop-at-limit");
3027 add_setshow_uinteger_cmd ("insn-number-max", no_class,
3028 &record_full_insn_max_num,
3029 _("Set record/replay buffer limit."),
3030 _("Show record/replay buffer limit."), _("\
3031 Set the maximum number of instructions to be stored in the\n\
3032 record/replay buffer. Zero means unlimited. Default is 200000."),
3033 set_record_full_insn_max_num,
3034 NULL, &set_record_full_cmdlist,
3035 &show_record_full_cmdlist);
3037 c = add_alias_cmd ("insn-number-max", "full insn-number-max", no_class, 1,
3038 &set_record_cmdlist);
3039 deprecate_cmd (c, "set record full insn-number-max");
3041 c = add_alias_cmd ("insn-number-max", "full insn-number-max", no_class, 1,
3042 &show_record_cmdlist);
3043 deprecate_cmd (c, "show record full insn-number-max");
3045 add_setshow_boolean_cmd ("memory-query", no_class,
3046 &record_full_memory_query, _("\
3047 Set whether query if PREC cannot record memory change of next instruction."),
3049 Show whether query if PREC cannot record memory change of next instruction."),
3052 When ON, query if PREC cannot record memory change of next instruction."),
3054 &set_record_full_cmdlist,
3055 &show_record_full_cmdlist);
3057 c = add_alias_cmd ("memory-query", "full memory-query", no_class, 1,
3058 &set_record_cmdlist);
3059 deprecate_cmd (c, "set record full memory-query");
3061 c = add_alias_cmd ("memory-query", "full memory-query", no_class, 1,
3062 &show_record_cmdlist);
3063 deprecate_cmd (c, "show record full memory-query");