1 /* Process record and replay target for GDB, the GNU debugger.
3 Copyright (C) 2013-2014 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 unsigned 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 /* See record-full.h. */
214 record_full_is_used (void)
216 struct target_ops *t;
218 t = find_record_target ();
219 return (t == &record_full_ops
220 || t == &record_full_core_ops);
224 /* Command lists for "set/show record full". */
225 static struct cmd_list_element *set_record_full_cmdlist;
226 static struct cmd_list_element *show_record_full_cmdlist;
228 /* Command list for "record full". */
229 static struct cmd_list_element *record_full_cmdlist;
231 /* The beneath function pointers. */
232 static struct target_ops *record_full_beneath_to_resume_ops;
233 static void (*record_full_beneath_to_resume) (struct target_ops *, ptid_t, int,
235 static struct target_ops *record_full_beneath_to_wait_ops;
236 static ptid_t (*record_full_beneath_to_wait) (struct target_ops *, ptid_t,
237 struct target_waitstatus *,
239 static struct target_ops *record_full_beneath_to_store_registers_ops;
240 static void (*record_full_beneath_to_store_registers) (struct target_ops *,
243 static struct target_ops *record_full_beneath_to_xfer_partial_ops;
244 static target_xfer_partial_ftype *record_full_beneath_to_xfer_partial;
246 (*record_full_beneath_to_insert_breakpoint) (struct target_ops *,
248 struct bp_target_info *);
249 static struct target_ops *record_full_beneath_to_insert_breakpoint_ops;
251 (*record_full_beneath_to_remove_breakpoint) (struct target_ops *,
253 struct bp_target_info *);
254 static struct target_ops *record_full_beneath_to_remove_breakpoint_ops;
255 static int (*record_full_beneath_to_stopped_by_watchpoint) (void);
256 static int (*record_full_beneath_to_stopped_data_address) (struct target_ops *,
259 (*record_full_beneath_to_async) (void (*) (enum inferior_event_type, void *),
262 static void record_full_goto_insn (struct record_full_entry *entry,
263 enum exec_direction_kind dir);
264 static void record_full_save (const char *recfilename);
266 /* Alloc and free functions for record_full_reg, record_full_mem, and
267 record_full_end entries. */
269 /* Alloc a record_full_reg record entry. */
271 static inline struct record_full_entry *
272 record_full_reg_alloc (struct regcache *regcache, int regnum)
274 struct record_full_entry *rec;
275 struct gdbarch *gdbarch = get_regcache_arch (regcache);
277 rec = xcalloc (1, sizeof (struct record_full_entry));
278 rec->type = record_full_reg;
279 rec->u.reg.num = regnum;
280 rec->u.reg.len = register_size (gdbarch, regnum);
281 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
282 rec->u.reg.u.ptr = (gdb_byte *) xmalloc (rec->u.reg.len);
287 /* Free a record_full_reg record entry. */
290 record_full_reg_release (struct record_full_entry *rec)
292 gdb_assert (rec->type == record_full_reg);
293 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
294 xfree (rec->u.reg.u.ptr);
298 /* Alloc a record_full_mem record entry. */
300 static inline struct record_full_entry *
301 record_full_mem_alloc (CORE_ADDR addr, int len)
303 struct record_full_entry *rec;
305 rec = xcalloc (1, sizeof (struct record_full_entry));
306 rec->type = record_full_mem;
307 rec->u.mem.addr = addr;
308 rec->u.mem.len = len;
309 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
310 rec->u.mem.u.ptr = (gdb_byte *) xmalloc (len);
315 /* Free a record_full_mem record entry. */
318 record_full_mem_release (struct record_full_entry *rec)
320 gdb_assert (rec->type == record_full_mem);
321 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
322 xfree (rec->u.mem.u.ptr);
326 /* Alloc a record_full_end record entry. */
328 static inline struct record_full_entry *
329 record_full_end_alloc (void)
331 struct record_full_entry *rec;
333 rec = xcalloc (1, sizeof (struct record_full_entry));
334 rec->type = record_full_end;
339 /* Free a record_full_end record entry. */
342 record_full_end_release (struct record_full_entry *rec)
347 /* Free one record entry, any type.
348 Return entry->type, in case caller wants to know. */
350 static inline enum record_full_type
351 record_full_entry_release (struct record_full_entry *rec)
353 enum record_full_type type = rec->type;
356 case record_full_reg:
357 record_full_reg_release (rec);
359 case record_full_mem:
360 record_full_mem_release (rec);
362 case record_full_end:
363 record_full_end_release (rec);
369 /* Free all record entries in list pointed to by REC. */
372 record_full_list_release (struct record_full_entry *rec)
383 record_full_entry_release (rec->next);
386 if (rec == &record_full_first)
388 record_full_insn_num = 0;
389 record_full_first.next = NULL;
392 record_full_entry_release (rec);
395 /* Free all record entries forward of the given list position. */
398 record_full_list_release_following (struct record_full_entry *rec)
400 struct record_full_entry *tmp = rec->next;
406 if (record_full_entry_release (tmp) == record_full_end)
408 record_full_insn_num--;
409 record_full_insn_count--;
415 /* Delete the first instruction from the beginning of the log, to make
416 room for adding a new instruction at the end of the log.
418 Note -- this function does not modify record_full_insn_num. */
421 record_full_list_release_first (void)
423 struct record_full_entry *tmp;
425 if (!record_full_first.next)
428 /* Loop until a record_full_end. */
431 /* Cut record_full_first.next out of the linked list. */
432 tmp = record_full_first.next;
433 record_full_first.next = tmp->next;
434 tmp->next->prev = &record_full_first;
436 /* tmp is now isolated, and can be deleted. */
437 if (record_full_entry_release (tmp) == record_full_end)
438 break; /* End loop at first record_full_end. */
440 if (!record_full_first.next)
442 gdb_assert (record_full_insn_num == 1);
443 break; /* End loop when list is empty. */
448 /* Add a struct record_full_entry to record_full_arch_list. */
451 record_full_arch_list_add (struct record_full_entry *rec)
453 if (record_debug > 1)
454 fprintf_unfiltered (gdb_stdlog,
455 "Process record: record_full_arch_list_add %s.\n",
456 host_address_to_string (rec));
458 if (record_full_arch_list_tail)
460 record_full_arch_list_tail->next = rec;
461 rec->prev = record_full_arch_list_tail;
462 record_full_arch_list_tail = rec;
466 record_full_arch_list_head = rec;
467 record_full_arch_list_tail = rec;
471 /* Return the value storage location of a record entry. */
472 static inline gdb_byte *
473 record_full_get_loc (struct record_full_entry *rec)
476 case record_full_mem:
477 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
478 return rec->u.mem.u.ptr;
480 return rec->u.mem.u.buf;
481 case record_full_reg:
482 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
483 return rec->u.reg.u.ptr;
485 return rec->u.reg.u.buf;
486 case record_full_end:
488 gdb_assert_not_reached ("unexpected record_full_entry type");
493 /* Record the value of a register NUM to record_full_arch_list. */
496 record_full_arch_list_add_reg (struct regcache *regcache, int regnum)
498 struct record_full_entry *rec;
500 if (record_debug > 1)
501 fprintf_unfiltered (gdb_stdlog,
502 "Process record: add register num = %d to "
506 rec = record_full_reg_alloc (regcache, regnum);
508 regcache_raw_read (regcache, regnum, record_full_get_loc (rec));
510 record_full_arch_list_add (rec);
515 /* Record the value of a region of memory whose address is ADDR and
516 length is LEN to record_full_arch_list. */
519 record_full_arch_list_add_mem (CORE_ADDR addr, int len)
521 struct record_full_entry *rec;
523 if (record_debug > 1)
524 fprintf_unfiltered (gdb_stdlog,
525 "Process record: add mem addr = %s len = %d to "
527 paddress (target_gdbarch (), addr), len);
529 if (!addr) /* FIXME: Why? Some arch must permit it... */
532 rec = record_full_mem_alloc (addr, len);
534 if (record_read_memory (target_gdbarch (), addr,
535 record_full_get_loc (rec), len))
537 record_full_mem_release (rec);
541 record_full_arch_list_add (rec);
546 /* Add a record_full_end type struct record_full_entry to
547 record_full_arch_list. */
550 record_full_arch_list_add_end (void)
552 struct record_full_entry *rec;
554 if (record_debug > 1)
555 fprintf_unfiltered (gdb_stdlog,
556 "Process record: add end to arch list.\n");
558 rec = record_full_end_alloc ();
559 rec->u.end.sigval = GDB_SIGNAL_0;
560 rec->u.end.insn_num = ++record_full_insn_count;
562 record_full_arch_list_add (rec);
568 record_full_check_insn_num (int set_terminal)
570 if (record_full_insn_num == record_full_insn_max_num)
572 /* Ask user what to do. */
573 if (record_full_stop_at_limit)
578 target_terminal_ours ();
579 q = yquery (_("Do you want to auto delete previous execution "
580 "log entries when record/replay buffer becomes "
581 "full (record full stop-at-limit)?"));
583 target_terminal_inferior ();
585 record_full_stop_at_limit = 0;
587 error (_("Process record: stopped by user."));
593 record_full_arch_list_cleanups (void *ignore)
595 record_full_list_release (record_full_arch_list_tail);
598 /* Before inferior step (when GDB record the running message, inferior
599 only can step), GDB will call this function to record the values to
600 record_full_list. This function will call gdbarch_process_record to
601 record the running message of inferior and set them to
602 record_full_arch_list, and add it to record_full_list. */
605 record_full_message (struct regcache *regcache, enum gdb_signal signal)
608 struct gdbarch *gdbarch = get_regcache_arch (regcache);
609 struct cleanup *old_cleanups
610 = make_cleanup (record_full_arch_list_cleanups, 0);
612 record_full_arch_list_head = NULL;
613 record_full_arch_list_tail = NULL;
615 /* Check record_full_insn_num. */
616 record_full_check_insn_num (1);
618 /* If gdb sends a signal value to target_resume,
619 save it in the 'end' field of the previous instruction.
621 Maybe process record should record what really happened,
622 rather than what gdb pretends has happened.
624 So if Linux delivered the signal to the child process during
625 the record mode, we will record it and deliver it again in
628 If user says "ignore this signal" during the record mode, then
629 it will be ignored again during the replay mode (no matter if
630 the user says something different, like "deliver this signal"
631 during the replay mode).
633 User should understand that nothing he does during the replay
634 mode will change the behavior of the child. If he tries,
635 then that is a user error.
637 But we should still deliver the signal to gdb during the replay,
638 if we delivered it during the recording. Therefore we should
639 record the signal during record_full_wait, not
640 record_full_resume. */
641 if (record_full_list != &record_full_first) /* FIXME better way to check */
643 gdb_assert (record_full_list->type == record_full_end);
644 record_full_list->u.end.sigval = signal;
647 if (signal == GDB_SIGNAL_0
648 || !gdbarch_process_record_signal_p (gdbarch))
649 ret = gdbarch_process_record (gdbarch,
651 regcache_read_pc (regcache));
653 ret = gdbarch_process_record_signal (gdbarch,
658 error (_("Process record: inferior program stopped."));
660 error (_("Process record: failed to record execution log."));
662 discard_cleanups (old_cleanups);
664 record_full_list->next = record_full_arch_list_head;
665 record_full_arch_list_head->prev = record_full_list;
666 record_full_list = record_full_arch_list_tail;
668 if (record_full_insn_num == record_full_insn_max_num)
669 record_full_list_release_first ();
671 record_full_insn_num++;
676 struct record_full_message_args {
677 struct regcache *regcache;
678 enum gdb_signal signal;
682 record_full_message_wrapper (void *args)
684 struct record_full_message_args *record_full_args = args;
686 return record_full_message (record_full_args->regcache,
687 record_full_args->signal);
691 record_full_message_wrapper_safe (struct regcache *regcache,
692 enum gdb_signal signal)
694 struct record_full_message_args args;
696 args.regcache = regcache;
697 args.signal = signal;
699 return catch_errors (record_full_message_wrapper, &args, NULL,
703 /* Set to 1 if record_full_store_registers and record_full_xfer_partial
704 doesn't need record. */
706 static int record_full_gdb_operation_disable = 0;
709 record_full_gdb_operation_disable_set (void)
711 struct cleanup *old_cleanups = NULL;
714 make_cleanup_restore_integer (&record_full_gdb_operation_disable);
715 record_full_gdb_operation_disable = 1;
720 /* Flag set to TRUE for target_stopped_by_watchpoint. */
721 static int record_full_hw_watchpoint = 0;
723 /* Execute one instruction from the record log. Each instruction in
724 the log will be represented by an arbitrary sequence of register
725 entries and memory entries, followed by an 'end' entry. */
728 record_full_exec_insn (struct regcache *regcache,
729 struct gdbarch *gdbarch,
730 struct record_full_entry *entry)
734 case record_full_reg: /* reg */
736 gdb_byte reg[MAX_REGISTER_SIZE];
738 if (record_debug > 1)
739 fprintf_unfiltered (gdb_stdlog,
740 "Process record: record_full_reg %s to "
741 "inferior num = %d.\n",
742 host_address_to_string (entry),
745 regcache_cooked_read (regcache, entry->u.reg.num, reg);
746 regcache_cooked_write (regcache, entry->u.reg.num,
747 record_full_get_loc (entry));
748 memcpy (record_full_get_loc (entry), reg, entry->u.reg.len);
752 case record_full_mem: /* mem */
754 /* Nothing to do if the entry is flagged not_accessible. */
755 if (!entry->u.mem.mem_entry_not_accessible)
757 gdb_byte *mem = alloca (entry->u.mem.len);
759 if (record_debug > 1)
760 fprintf_unfiltered (gdb_stdlog,
761 "Process record: record_full_mem %s to "
762 "inferior addr = %s len = %d.\n",
763 host_address_to_string (entry),
764 paddress (gdbarch, entry->u.mem.addr),
767 if (record_read_memory (gdbarch,
768 entry->u.mem.addr, mem, entry->u.mem.len))
769 entry->u.mem.mem_entry_not_accessible = 1;
772 if (target_write_memory (entry->u.mem.addr,
773 record_full_get_loc (entry),
776 entry->u.mem.mem_entry_not_accessible = 1;
778 warning (_("Process record: error writing memory at "
779 "addr = %s len = %d."),
780 paddress (gdbarch, entry->u.mem.addr),
785 memcpy (record_full_get_loc (entry), mem,
788 /* We've changed memory --- check if a hardware
789 watchpoint should trap. Note that this
790 presently assumes the target beneath supports
791 continuable watchpoints. On non-continuable
792 watchpoints target, we'll want to check this
793 _before_ actually doing the memory change, and
794 not doing the change at all if the watchpoint
796 if (hardware_watchpoint_inserted_in_range
797 (get_regcache_aspace (regcache),
798 entry->u.mem.addr, entry->u.mem.len))
799 record_full_hw_watchpoint = 1;
808 static struct target_ops *tmp_to_resume_ops;
809 static void (*tmp_to_resume) (struct target_ops *, ptid_t, int,
811 static struct target_ops *tmp_to_wait_ops;
812 static ptid_t (*tmp_to_wait) (struct target_ops *, ptid_t,
813 struct target_waitstatus *,
815 static struct target_ops *tmp_to_store_registers_ops;
816 static void (*tmp_to_store_registers) (struct target_ops *,
819 static struct target_ops *tmp_to_xfer_partial_ops;
820 static target_xfer_partial_ftype *tmp_to_xfer_partial;
821 static int (*tmp_to_insert_breakpoint) (struct target_ops *, struct gdbarch *,
822 struct bp_target_info *);
823 static struct target_ops *tmp_to_insert_breakpoint_ops;
824 static int (*tmp_to_remove_breakpoint) (struct target_ops *, struct gdbarch *,
825 struct bp_target_info *);
826 static struct target_ops *tmp_to_remove_breakpoint_ops;
827 static int (*tmp_to_stopped_by_watchpoint) (void);
828 static int (*tmp_to_stopped_data_address) (struct target_ops *, CORE_ADDR *);
829 static int (*tmp_to_stopped_data_address) (struct target_ops *, CORE_ADDR *);
830 static void (*tmp_to_async) (void (*) (enum inferior_event_type, void *), void *);
832 static void record_full_restore (void);
834 /* Asynchronous signal handle registered as event loop source for when
835 we have pending events ready to be passed to the core. */
837 static struct async_event_handler *record_full_async_inferior_event_token;
840 record_full_async_inferior_event_handler (gdb_client_data data)
842 inferior_event_handler (INF_REG_EVENT, NULL);
845 /* Open the process record target. */
848 record_full_core_open_1 (char *name, int from_tty)
850 struct regcache *regcache = get_current_regcache ();
851 int regnum = gdbarch_num_regs (get_regcache_arch (regcache));
854 /* Get record_full_core_regbuf. */
855 target_fetch_registers (regcache, -1);
856 record_full_core_regbuf = xmalloc (MAX_REGISTER_SIZE * regnum);
857 for (i = 0; i < regnum; i ++)
858 regcache_raw_collect (regcache, i,
859 record_full_core_regbuf + MAX_REGISTER_SIZE * i);
861 /* Get record_full_core_start and record_full_core_end. */
862 if (build_section_table (core_bfd, &record_full_core_start,
863 &record_full_core_end))
865 xfree (record_full_core_regbuf);
866 record_full_core_regbuf = NULL;
867 error (_("\"%s\": Can't find sections: %s"),
868 bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ()));
871 push_target (&record_full_core_ops);
872 record_full_restore ();
875 /* "to_open" target method for 'live' processes. */
878 record_full_open_1 (char *name, int from_tty)
881 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_open\n");
884 if (!target_has_execution)
885 error (_("Process record: the program is not being run."));
887 error (_("Process record target can't debug inferior in non-stop mode "
890 if (!gdbarch_process_record_p (target_gdbarch ()))
891 error (_("Process record: the current architecture doesn't support "
892 "record function."));
895 error (_("Could not find 'to_resume' method on the target stack."));
897 error (_("Could not find 'to_wait' method on the target stack."));
898 if (!tmp_to_store_registers)
899 error (_("Could not find 'to_store_registers' "
900 "method on the target stack."));
901 if (!tmp_to_insert_breakpoint)
902 error (_("Could not find 'to_insert_breakpoint' "
903 "method on the target stack."));
904 if (!tmp_to_remove_breakpoint)
905 error (_("Could not find 'to_remove_breakpoint' "
906 "method on the target stack."));
907 if (!tmp_to_stopped_by_watchpoint)
908 error (_("Could not find 'to_stopped_by_watchpoint' "
909 "method on the target stack."));
910 if (!tmp_to_stopped_data_address)
911 error (_("Could not find 'to_stopped_data_address' "
912 "method on the target stack."));
914 push_target (&record_full_ops);
917 static void record_full_init_record_breakpoints (void);
919 /* "to_open" target method. Open the process record target. */
922 record_full_open (char *name, int from_tty)
924 struct target_ops *t;
927 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_open\n");
931 /* Reset the tmp beneath pointers. */
932 tmp_to_resume_ops = NULL;
933 tmp_to_resume = NULL;
934 tmp_to_wait_ops = NULL;
936 tmp_to_store_registers_ops = NULL;
937 tmp_to_store_registers = NULL;
938 tmp_to_xfer_partial_ops = NULL;
939 tmp_to_xfer_partial = NULL;
940 tmp_to_insert_breakpoint = NULL;
941 tmp_to_remove_breakpoint = NULL;
942 tmp_to_stopped_by_watchpoint = NULL;
943 tmp_to_stopped_data_address = NULL;
945 tmp_to_insert_breakpoint_ops = NULL;
946 tmp_to_remove_breakpoint_ops = 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)
973 tmp_to_insert_breakpoint = t->to_insert_breakpoint;
974 tmp_to_insert_breakpoint_ops = t;
976 if (!tmp_to_remove_breakpoint)
978 tmp_to_remove_breakpoint = t->to_remove_breakpoint;
979 tmp_to_remove_breakpoint_ops = t;
981 if (!tmp_to_stopped_by_watchpoint)
982 tmp_to_stopped_by_watchpoint = t->to_stopped_by_watchpoint;
983 if (!tmp_to_stopped_data_address)
984 tmp_to_stopped_data_address = t->to_stopped_data_address;
986 tmp_to_async = t->to_async;
988 if (!tmp_to_xfer_partial)
989 error (_("Could not find 'to_xfer_partial' method on the target stack."));
992 record_full_insn_num = 0;
993 record_full_insn_count = 0;
994 record_full_list = &record_full_first;
995 record_full_list->next = NULL;
997 /* Set the tmp beneath pointers to beneath pointers. */
998 record_full_beneath_to_resume_ops = tmp_to_resume_ops;
999 record_full_beneath_to_resume = tmp_to_resume;
1000 record_full_beneath_to_wait_ops = tmp_to_wait_ops;
1001 record_full_beneath_to_wait = tmp_to_wait;
1002 record_full_beneath_to_store_registers_ops = tmp_to_store_registers_ops;
1003 record_full_beneath_to_store_registers = tmp_to_store_registers;
1004 record_full_beneath_to_xfer_partial_ops = tmp_to_xfer_partial_ops;
1005 record_full_beneath_to_xfer_partial = tmp_to_xfer_partial;
1006 record_full_beneath_to_insert_breakpoint = tmp_to_insert_breakpoint;
1007 record_full_beneath_to_insert_breakpoint_ops = tmp_to_insert_breakpoint_ops;
1008 record_full_beneath_to_remove_breakpoint = tmp_to_remove_breakpoint;
1009 record_full_beneath_to_remove_breakpoint_ops = tmp_to_remove_breakpoint_ops;
1010 record_full_beneath_to_stopped_by_watchpoint = tmp_to_stopped_by_watchpoint;
1011 record_full_beneath_to_stopped_data_address = tmp_to_stopped_data_address;
1012 record_full_beneath_to_async = tmp_to_async;
1015 record_full_core_open_1 (name, from_tty);
1017 record_full_open_1 (name, from_tty);
1019 /* Register extra event sources in the event loop. */
1020 record_full_async_inferior_event_token
1021 = create_async_event_handler (record_full_async_inferior_event_handler,
1024 record_full_init_record_breakpoints ();
1026 observer_notify_record_changed (current_inferior (), 1);
1029 /* "to_close" target method. Close the process record target. */
1032 record_full_close (void)
1034 struct record_full_core_buf_entry *entry;
1037 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_close\n");
1039 record_full_list_release (record_full_list);
1041 /* Release record_full_core_regbuf. */
1042 if (record_full_core_regbuf)
1044 xfree (record_full_core_regbuf);
1045 record_full_core_regbuf = NULL;
1048 /* Release record_full_core_buf_list. */
1049 if (record_full_core_buf_list)
1051 for (entry = record_full_core_buf_list->prev; entry;
1052 entry = entry->prev)
1054 xfree (record_full_core_buf_list);
1055 record_full_core_buf_list = entry;
1057 record_full_core_buf_list = NULL;
1060 if (record_full_async_inferior_event_token)
1061 delete_async_event_handler (&record_full_async_inferior_event_token);
1064 static int record_full_resume_step = 0;
1066 /* True if we've been resumed, and so each record_full_wait call should
1067 advance execution. If this is false, record_full_wait will return a
1068 TARGET_WAITKIND_IGNORE. */
1069 static int record_full_resumed = 0;
1071 /* The execution direction of the last resume we got. This is
1072 necessary for async mode. Vis (order is not strictly accurate):
1074 1. user has the global execution direction set to forward
1075 2. user does a reverse-step command
1076 3. record_full_resume is called with global execution direction
1077 temporarily switched to reverse
1078 4. GDB's execution direction is reverted back to forward
1079 5. target record notifies event loop there's an event to handle
1080 6. infrun asks the target which direction was it going, and switches
1081 the global execution direction accordingly (to reverse)
1082 7. infrun polls an event out of the record target, and handles it
1083 8. GDB goes back to the event loop, and goto #4.
1085 static enum exec_direction_kind record_full_execution_dir = EXEC_FORWARD;
1087 /* "to_resume" target method. Resume the process record target. */
1090 record_full_resume (struct target_ops *ops, ptid_t ptid, int step,
1091 enum gdb_signal signal)
1093 record_full_resume_step = step;
1094 record_full_resumed = 1;
1095 record_full_execution_dir = execution_direction;
1097 if (!RECORD_FULL_IS_REPLAY)
1099 struct gdbarch *gdbarch = target_thread_architecture (ptid);
1101 record_full_message (get_current_regcache (), signal);
1105 /* This is not hard single step. */
1106 if (!gdbarch_software_single_step_p (gdbarch))
1108 /* This is a normal continue. */
1113 /* This arch support soft sigle step. */
1114 if (single_step_breakpoints_inserted ())
1116 /* This is a soft single step. */
1117 record_full_resume_step = 1;
1121 /* This is a continue.
1122 Try to insert a soft single step breakpoint. */
1123 if (!gdbarch_software_single_step (gdbarch,
1124 get_current_frame ()))
1126 /* This system don't want use soft single step.
1127 Use hard sigle step. */
1134 /* Make sure the target beneath reports all signals. */
1135 target_pass_signals (0, NULL);
1137 record_full_beneath_to_resume (record_full_beneath_to_resume_ops,
1138 ptid, step, signal);
1141 /* We are about to start executing the inferior (or simulate it),
1142 let's register it with the event loop. */
1143 if (target_can_async_p ())
1145 target_async (inferior_event_handler, 0);
1146 /* Notify the event loop there's an event to wait for. We do
1147 most of the work in record_full_wait. */
1148 mark_async_event_handler (record_full_async_inferior_event_token);
1152 static int record_full_get_sig = 0;
1154 /* SIGINT signal handler, registered by "to_wait" method. */
1157 record_full_sig_handler (int signo)
1160 fprintf_unfiltered (gdb_stdlog, "Process record: get a signal\n");
1162 /* It will break the running inferior in replay mode. */
1163 record_full_resume_step = 1;
1165 /* It will let record_full_wait set inferior status to get the signal
1167 record_full_get_sig = 1;
1171 record_full_wait_cleanups (void *ignore)
1173 if (execution_direction == EXEC_REVERSE)
1175 if (record_full_list->next)
1176 record_full_list = record_full_list->next;
1179 record_full_list = record_full_list->prev;
1182 /* "to_wait" target method for process record target.
1184 In record mode, the target is always run in singlestep mode
1185 (even when gdb says to continue). The to_wait method intercepts
1186 the stop events and determines which ones are to be passed on to
1187 gdb. Most stop events are just singlestep events that gdb is not
1188 to know about, so the to_wait method just records them and keeps
1191 In replay mode, this function emulates the recorded execution log,
1192 one instruction at a time (forward or backward), and determines
1196 record_full_wait_1 (struct target_ops *ops,
1197 ptid_t ptid, struct target_waitstatus *status,
1200 struct cleanup *set_cleanups = record_full_gdb_operation_disable_set ();
1203 fprintf_unfiltered (gdb_stdlog,
1204 "Process record: record_full_wait "
1205 "record_full_resume_step = %d, "
1206 "record_full_resumed = %d, direction=%s\n",
1207 record_full_resume_step, record_full_resumed,
1208 record_full_execution_dir == EXEC_FORWARD
1209 ? "forward" : "reverse");
1211 if (!record_full_resumed)
1213 gdb_assert ((options & TARGET_WNOHANG) != 0);
1215 /* No interesting event. */
1216 status->kind = TARGET_WAITKIND_IGNORE;
1217 return minus_one_ptid;
1220 record_full_get_sig = 0;
1221 signal (SIGINT, record_full_sig_handler);
1223 if (!RECORD_FULL_IS_REPLAY && ops != &record_full_core_ops)
1225 if (record_full_resume_step)
1227 /* This is a single step. */
1228 return record_full_beneath_to_wait (record_full_beneath_to_wait_ops,
1229 ptid, status, options);
1233 /* This is not a single step. */
1236 struct gdbarch *gdbarch = target_thread_architecture (inferior_ptid);
1240 ret = record_full_beneath_to_wait
1241 (record_full_beneath_to_wait_ops, ptid, status, options);
1242 if (status->kind == TARGET_WAITKIND_IGNORE)
1245 fprintf_unfiltered (gdb_stdlog,
1246 "Process record: record_full_wait "
1247 "target beneath not done yet\n");
1251 if (single_step_breakpoints_inserted ())
1252 remove_single_step_breakpoints ();
1254 if (record_full_resume_step)
1257 /* Is this a SIGTRAP? */
1258 if (status->kind == TARGET_WAITKIND_STOPPED
1259 && status->value.sig == GDB_SIGNAL_TRAP)
1261 struct regcache *regcache;
1262 struct address_space *aspace;
1264 /* Yes -- this is likely our single-step finishing,
1265 but check if there's any reason the core would be
1266 interested in the event. */
1268 registers_changed ();
1269 regcache = get_current_regcache ();
1270 tmp_pc = regcache_read_pc (regcache);
1271 aspace = get_regcache_aspace (regcache);
1273 if (target_stopped_by_watchpoint ())
1275 /* Always interested in watchpoints. */
1277 else if (breakpoint_inserted_here_p (aspace, tmp_pc))
1279 /* There is a breakpoint here. Let the core
1281 if (software_breakpoint_inserted_here_p (aspace, tmp_pc))
1283 struct gdbarch *gdbarch
1284 = get_regcache_arch (regcache);
1285 CORE_ADDR decr_pc_after_break
1286 = target_decr_pc_after_break (gdbarch);
1287 if (decr_pc_after_break)
1288 regcache_write_pc (regcache,
1289 tmp_pc + decr_pc_after_break);
1294 /* This is a single-step trap. Record the
1295 insn and issue another step.
1296 FIXME: this part can be a random SIGTRAP too.
1297 But GDB cannot handle it. */
1300 if (!record_full_message_wrapper_safe (regcache,
1303 status->kind = TARGET_WAITKIND_STOPPED;
1304 status->value.sig = GDB_SIGNAL_0;
1308 if (gdbarch_software_single_step_p (gdbarch))
1310 /* Try to insert the software single step breakpoint.
1311 If insert success, set step to 0. */
1312 set_executing (inferior_ptid, 0);
1313 reinit_frame_cache ();
1314 if (gdbarch_software_single_step (gdbarch,
1315 get_current_frame ()))
1317 set_executing (inferior_ptid, 1);
1321 fprintf_unfiltered (gdb_stdlog,
1322 "Process record: record_full_wait "
1323 "issuing one more step in the "
1324 "target beneath\n");
1325 record_full_beneath_to_resume
1326 (record_full_beneath_to_resume_ops, ptid, step,
1332 /* The inferior is broken by a breakpoint or a signal. */
1341 struct regcache *regcache = get_current_regcache ();
1342 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1343 struct address_space *aspace = get_regcache_aspace (regcache);
1344 int continue_flag = 1;
1345 int first_record_full_end = 1;
1346 struct cleanup *old_cleanups
1347 = make_cleanup (record_full_wait_cleanups, 0);
1350 record_full_hw_watchpoint = 0;
1351 status->kind = TARGET_WAITKIND_STOPPED;
1353 /* Check breakpoint when forward execute. */
1354 if (execution_direction == EXEC_FORWARD)
1356 tmp_pc = regcache_read_pc (regcache);
1357 if (breakpoint_inserted_here_p (aspace, tmp_pc))
1359 int decr_pc_after_break = target_decr_pc_after_break (gdbarch);
1362 fprintf_unfiltered (gdb_stdlog,
1363 "Process record: break at %s.\n",
1364 paddress (gdbarch, tmp_pc));
1366 if (decr_pc_after_break
1367 && !record_full_resume_step
1368 && software_breakpoint_inserted_here_p (aspace, tmp_pc))
1369 regcache_write_pc (regcache,
1370 tmp_pc + decr_pc_after_break);
1375 /* If GDB is in terminal_inferior mode, it will not get the signal.
1376 And in GDB replay mode, GDB doesn't need to be in terminal_inferior
1377 mode, because inferior will not executed.
1378 Then set it to terminal_ours to make GDB get the signal. */
1379 target_terminal_ours ();
1381 /* In EXEC_FORWARD mode, record_full_list points to the tail of prev
1383 if (execution_direction == EXEC_FORWARD && record_full_list->next)
1384 record_full_list = record_full_list->next;
1386 /* Loop over the record_full_list, looking for the next place to
1390 /* Check for beginning and end of log. */
1391 if (execution_direction == EXEC_REVERSE
1392 && record_full_list == &record_full_first)
1394 /* Hit beginning of record log in reverse. */
1395 status->kind = TARGET_WAITKIND_NO_HISTORY;
1398 if (execution_direction != EXEC_REVERSE && !record_full_list->next)
1400 /* Hit end of record log going forward. */
1401 status->kind = TARGET_WAITKIND_NO_HISTORY;
1405 record_full_exec_insn (regcache, gdbarch, record_full_list);
1407 if (record_full_list->type == record_full_end)
1409 if (record_debug > 1)
1410 fprintf_unfiltered (gdb_stdlog,
1411 "Process record: record_full_end %s to "
1413 host_address_to_string (record_full_list));
1415 if (first_record_full_end && execution_direction == EXEC_REVERSE)
1417 /* When reverse excute, the first record_full_end is the
1418 part of current instruction. */
1419 first_record_full_end = 0;
1423 /* In EXEC_REVERSE mode, this is the record_full_end of prev
1425 In EXEC_FORWARD mode, this is the record_full_end of
1426 current instruction. */
1428 if (record_full_resume_step)
1430 if (record_debug > 1)
1431 fprintf_unfiltered (gdb_stdlog,
1432 "Process record: step.\n");
1436 /* check breakpoint */
1437 tmp_pc = regcache_read_pc (regcache);
1438 if (breakpoint_inserted_here_p (aspace, tmp_pc))
1440 int decr_pc_after_break
1441 = target_decr_pc_after_break (gdbarch);
1444 fprintf_unfiltered (gdb_stdlog,
1445 "Process record: break "
1447 paddress (gdbarch, tmp_pc));
1448 if (decr_pc_after_break
1449 && execution_direction == EXEC_FORWARD
1450 && !record_full_resume_step
1451 && software_breakpoint_inserted_here_p (aspace,
1453 regcache_write_pc (regcache,
1454 tmp_pc + decr_pc_after_break);
1458 if (record_full_hw_watchpoint)
1461 fprintf_unfiltered (gdb_stdlog,
1462 "Process record: hit hw "
1466 /* Check target signal */
1467 if (record_full_list->u.end.sigval != GDB_SIGNAL_0)
1468 /* FIXME: better way to check */
1475 if (execution_direction == EXEC_REVERSE)
1477 if (record_full_list->prev)
1478 record_full_list = record_full_list->prev;
1482 if (record_full_list->next)
1483 record_full_list = record_full_list->next;
1487 while (continue_flag);
1490 if (record_full_get_sig)
1491 status->value.sig = GDB_SIGNAL_INT;
1492 else if (record_full_list->u.end.sigval != GDB_SIGNAL_0)
1493 /* FIXME: better way to check */
1494 status->value.sig = record_full_list->u.end.sigval;
1496 status->value.sig = GDB_SIGNAL_TRAP;
1498 discard_cleanups (old_cleanups);
1501 signal (SIGINT, handle_sigint);
1503 do_cleanups (set_cleanups);
1504 return inferior_ptid;
1508 record_full_wait (struct target_ops *ops,
1509 ptid_t ptid, struct target_waitstatus *status,
1514 return_ptid = record_full_wait_1 (ops, ptid, status, options);
1515 if (status->kind != TARGET_WAITKIND_IGNORE)
1517 /* We're reporting a stop. Make sure any spurious
1518 target_wait(WNOHANG) doesn't advance the target until the
1519 core wants us resumed again. */
1520 record_full_resumed = 0;
1526 record_full_stopped_by_watchpoint (void)
1528 if (RECORD_FULL_IS_REPLAY)
1529 return record_full_hw_watchpoint;
1531 return record_full_beneath_to_stopped_by_watchpoint ();
1535 record_full_stopped_data_address (struct target_ops *ops, CORE_ADDR *addr_p)
1537 if (RECORD_FULL_IS_REPLAY)
1540 return record_full_beneath_to_stopped_data_address (ops, addr_p);
1543 /* Record registers change (by user or by GDB) to list as an instruction. */
1546 record_full_registers_change (struct regcache *regcache, int regnum)
1548 /* Check record_full_insn_num. */
1549 record_full_check_insn_num (0);
1551 record_full_arch_list_head = NULL;
1552 record_full_arch_list_tail = NULL;
1558 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
1560 if (record_full_arch_list_add_reg (regcache, i))
1562 record_full_list_release (record_full_arch_list_tail);
1563 error (_("Process record: failed to record execution log."));
1569 if (record_full_arch_list_add_reg (regcache, regnum))
1571 record_full_list_release (record_full_arch_list_tail);
1572 error (_("Process record: failed to record execution log."));
1575 if (record_full_arch_list_add_end ())
1577 record_full_list_release (record_full_arch_list_tail);
1578 error (_("Process record: failed to record execution log."));
1580 record_full_list->next = record_full_arch_list_head;
1581 record_full_arch_list_head->prev = record_full_list;
1582 record_full_list = record_full_arch_list_tail;
1584 if (record_full_insn_num == record_full_insn_max_num)
1585 record_full_list_release_first ();
1587 record_full_insn_num++;
1590 /* "to_store_registers" method for process record target. */
1593 record_full_store_registers (struct target_ops *ops,
1594 struct regcache *regcache,
1597 if (!record_full_gdb_operation_disable)
1599 if (RECORD_FULL_IS_REPLAY)
1603 /* Let user choose if he wants to write register or not. */
1606 query (_("Because GDB is in replay mode, changing the "
1607 "value of a register will make the execution "
1608 "log unusable from this point onward. "
1609 "Change all registers?"));
1612 query (_("Because GDB is in replay mode, changing the value "
1613 "of a register will make the execution log unusable "
1614 "from this point onward. Change register %s?"),
1615 gdbarch_register_name (get_regcache_arch (regcache),
1620 /* Invalidate the value of regcache that was set in function
1621 "regcache_raw_write". */
1627 i < gdbarch_num_regs (get_regcache_arch (regcache));
1629 regcache_invalidate (regcache, i);
1632 regcache_invalidate (regcache, regno);
1634 error (_("Process record canceled the operation."));
1637 /* Destroy the record from here forward. */
1638 record_full_list_release_following (record_full_list);
1641 record_full_registers_change (regcache, regno);
1643 record_full_beneath_to_store_registers
1644 (record_full_beneath_to_store_registers_ops, regcache, regno);
1647 /* "to_xfer_partial" method. Behavior is conditional on
1648 RECORD_FULL_IS_REPLAY.
1649 In replay mode, we cannot write memory unles we are willing to
1650 invalidate the record/replay log from this point forward. */
1653 record_full_xfer_partial (struct target_ops *ops, enum target_object object,
1654 const char *annex, gdb_byte *readbuf,
1655 const gdb_byte *writebuf, ULONGEST offset,
1658 if (!record_full_gdb_operation_disable
1659 && (object == TARGET_OBJECT_MEMORY
1660 || object == TARGET_OBJECT_RAW_MEMORY) && writebuf)
1662 if (RECORD_FULL_IS_REPLAY)
1664 /* Let user choose if he wants to write memory or not. */
1665 if (!query (_("Because GDB is in replay mode, writing to memory "
1666 "will make the execution log unusable from this "
1667 "point onward. Write memory at address %s?"),
1668 paddress (target_gdbarch (), offset)))
1669 error (_("Process record canceled the operation."));
1671 /* Destroy the record from here forward. */
1672 record_full_list_release_following (record_full_list);
1675 /* Check record_full_insn_num */
1676 record_full_check_insn_num (0);
1678 /* Record registers change to list as an instruction. */
1679 record_full_arch_list_head = NULL;
1680 record_full_arch_list_tail = NULL;
1681 if (record_full_arch_list_add_mem (offset, len))
1683 record_full_list_release (record_full_arch_list_tail);
1685 fprintf_unfiltered (gdb_stdlog,
1686 "Process record: failed to record "
1690 if (record_full_arch_list_add_end ())
1692 record_full_list_release (record_full_arch_list_tail);
1694 fprintf_unfiltered (gdb_stdlog,
1695 "Process record: failed to record "
1699 record_full_list->next = record_full_arch_list_head;
1700 record_full_arch_list_head->prev = record_full_list;
1701 record_full_list = record_full_arch_list_tail;
1703 if (record_full_insn_num == record_full_insn_max_num)
1704 record_full_list_release_first ();
1706 record_full_insn_num++;
1709 return record_full_beneath_to_xfer_partial
1710 (record_full_beneath_to_xfer_partial_ops, object, annex,
1711 readbuf, writebuf, offset, 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 /* The address and address space the breakpoint was set at. */
1724 struct address_space *address_space;
1727 /* True when the breakpoint has been also installed in the target
1728 beneath. This will be false for breakpoints set during replay or
1730 int in_target_beneath;
1733 typedef struct record_full_breakpoint *record_full_breakpoint_p;
1734 DEF_VEC_P(record_full_breakpoint_p);
1736 /* The list of breakpoints inserted while the record target is
1738 VEC(record_full_breakpoint_p) *record_full_breakpoints = NULL;
1741 record_full_sync_record_breakpoints (struct bp_location *loc, void *data)
1743 if (loc->loc_type != bp_loc_software_breakpoint)
1748 struct record_full_breakpoint *bp = XNEW (struct record_full_breakpoint);
1750 bp->addr = loc->target_info.placed_address;
1751 bp->address_space = loc->target_info.placed_address_space;
1753 bp->in_target_beneath = 1;
1755 VEC_safe_push (record_full_breakpoint_p, record_full_breakpoints, bp);
1759 /* Sync existing breakpoints to record_full_breakpoints. */
1762 record_full_init_record_breakpoints (void)
1764 VEC_free (record_full_breakpoint_p, record_full_breakpoints);
1766 iterate_over_bp_locations (record_full_sync_record_breakpoints);
1769 /* Behavior is conditional on RECORD_FULL_IS_REPLAY. We will not actually
1770 insert or remove breakpoints in the real target when replaying, nor
1774 record_full_insert_breakpoint (struct target_ops *ops,
1775 struct gdbarch *gdbarch,
1776 struct bp_target_info *bp_tgt)
1778 struct record_full_breakpoint *bp;
1779 int in_target_beneath = 0;
1781 if (!RECORD_FULL_IS_REPLAY)
1783 /* When recording, we currently always single-step, so we don't
1784 really need to install regular breakpoints in the inferior.
1785 However, we do have to insert software single-step
1786 breakpoints, in case the target can't hardware step. To keep
1787 things single, we always insert. */
1788 struct cleanup *old_cleanups;
1791 old_cleanups = record_full_gdb_operation_disable_set ();
1792 ops = record_full_beneath_to_insert_breakpoint_ops;
1793 ret = record_full_beneath_to_insert_breakpoint (ops, gdbarch,
1795 do_cleanups (old_cleanups);
1800 in_target_beneath = 1;
1803 bp = XNEW (struct record_full_breakpoint);
1804 bp->addr = bp_tgt->placed_address;
1805 bp->address_space = bp_tgt->placed_address_space;
1806 bp->in_target_beneath = in_target_beneath;
1807 VEC_safe_push (record_full_breakpoint_p, record_full_breakpoints, bp);
1811 /* "to_remove_breakpoint" method for process record target. */
1814 record_full_remove_breakpoint (struct target_ops *ops,
1815 struct gdbarch *gdbarch,
1816 struct bp_target_info *bp_tgt)
1818 struct record_full_breakpoint *bp;
1822 VEC_iterate (record_full_breakpoint_p,
1823 record_full_breakpoints, ix, bp);
1826 if (bp->addr == bp_tgt->placed_address
1827 && bp->address_space == bp_tgt->placed_address_space)
1829 if (bp->in_target_beneath)
1831 struct cleanup *old_cleanups;
1834 old_cleanups = record_full_gdb_operation_disable_set ();
1835 ops = record_full_beneath_to_remove_breakpoint_ops;
1836 ret = record_full_beneath_to_remove_breakpoint (ops, gdbarch,
1838 do_cleanups (old_cleanups);
1844 VEC_unordered_remove (record_full_breakpoint_p,
1845 record_full_breakpoints, ix);
1850 gdb_assert_not_reached ("removing unknown breakpoint");
1853 /* "to_can_execute_reverse" method for process record target. */
1856 record_full_can_execute_reverse (void)
1861 /* "to_get_bookmark" method for process record and prec over core. */
1864 record_full_get_bookmark (char *args, int from_tty)
1868 /* Return stringified form of instruction count. */
1869 if (record_full_list && record_full_list->type == record_full_end)
1870 ret = xstrdup (pulongest (record_full_list->u.end.insn_num));
1875 fprintf_unfiltered (gdb_stdlog,
1876 "record_full_get_bookmark returns %s\n", ret);
1878 fprintf_unfiltered (gdb_stdlog,
1879 "record_full_get_bookmark returns NULL\n");
1881 return (gdb_byte *) ret;
1884 /* "to_goto_bookmark" method for process record and prec over core. */
1887 record_full_goto_bookmark (gdb_byte *raw_bookmark, int from_tty)
1889 char *bookmark = (char *) raw_bookmark;
1892 fprintf_unfiltered (gdb_stdlog,
1893 "record_full_goto_bookmark receives %s\n", bookmark);
1895 if (bookmark[0] == '\'' || bookmark[0] == '\"')
1897 if (bookmark[strlen (bookmark) - 1] != bookmark[0])
1898 error (_("Unbalanced quotes: %s"), bookmark);
1900 /* Strip trailing quote. */
1901 bookmark[strlen (bookmark) - 1] = '\0';
1902 /* Strip leading quote. */
1904 /* Pass along to cmd_record_full_goto. */
1907 cmd_record_goto (bookmark, from_tty);
1912 record_full_async (void (*callback) (enum inferior_event_type event_type,
1913 void *context), void *context)
1915 /* If we're on top of a line target (e.g., linux-nat, remote), then
1916 set it to async mode as well. Will be NULL if we're sitting on
1917 top of the core target, for "record restore". */
1918 if (record_full_beneath_to_async != NULL)
1919 record_full_beneath_to_async (callback, context);
1923 record_full_can_async_p (void)
1925 /* We only enable async when the user specifically asks for it. */
1926 return target_async_permitted;
1930 record_full_is_async_p (void)
1932 /* We only enable async when the user specifically asks for it. */
1933 return target_async_permitted;
1936 static enum exec_direction_kind
1937 record_full_execution_direction (void)
1939 return record_full_execution_dir;
1943 record_full_info (void)
1945 struct record_full_entry *p;
1947 if (RECORD_FULL_IS_REPLAY)
1948 printf_filtered (_("Replay mode:\n"));
1950 printf_filtered (_("Record mode:\n"));
1952 /* Find entry for first actual instruction in the log. */
1953 for (p = record_full_first.next;
1954 p != NULL && p->type != record_full_end;
1958 /* Do we have a log at all? */
1959 if (p != NULL && p->type == record_full_end)
1961 /* Display instruction number for first instruction in the log. */
1962 printf_filtered (_("Lowest recorded instruction number is %s.\n"),
1963 pulongest (p->u.end.insn_num));
1965 /* If in replay mode, display where we are in the log. */
1966 if (RECORD_FULL_IS_REPLAY)
1967 printf_filtered (_("Current instruction number is %s.\n"),
1968 pulongest (record_full_list->u.end.insn_num));
1970 /* Display instruction number for last instruction in the log. */
1971 printf_filtered (_("Highest recorded instruction number is %s.\n"),
1972 pulongest (record_full_insn_count));
1974 /* Display log count. */
1975 printf_filtered (_("Log contains %u instructions.\n"),
1976 record_full_insn_num);
1979 printf_filtered (_("No instructions have been logged.\n"));
1981 /* Display max log size. */
1982 printf_filtered (_("Max logged instructions is %u.\n"),
1983 record_full_insn_max_num);
1986 /* The "to_record_delete" target method. */
1989 record_full_delete (void)
1991 record_full_list_release_following (record_full_list);
1994 /* The "to_record_is_replaying" target method. */
1997 record_full_is_replaying (void)
1999 return RECORD_FULL_IS_REPLAY;
2002 /* Go to a specific entry. */
2005 record_full_goto_entry (struct record_full_entry *p)
2008 error (_("Target insn not found."));
2009 else if (p == record_full_list)
2010 error (_("Already at target insn."));
2011 else if (p->u.end.insn_num > record_full_list->u.end.insn_num)
2013 printf_filtered (_("Go forward to insn number %s\n"),
2014 pulongest (p->u.end.insn_num));
2015 record_full_goto_insn (p, EXEC_FORWARD);
2019 printf_filtered (_("Go backward to insn number %s\n"),
2020 pulongest (p->u.end.insn_num));
2021 record_full_goto_insn (p, EXEC_REVERSE);
2024 registers_changed ();
2025 reinit_frame_cache ();
2026 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
2029 /* The "to_goto_record_begin" target method. */
2032 record_full_goto_begin (void)
2034 struct record_full_entry *p = NULL;
2036 for (p = &record_full_first; p != NULL; p = p->next)
2037 if (p->type == record_full_end)
2040 record_full_goto_entry (p);
2043 /* The "to_goto_record_end" target method. */
2046 record_full_goto_end (void)
2048 struct record_full_entry *p = NULL;
2050 for (p = record_full_list; p->next != NULL; p = p->next)
2052 for (; p!= NULL; p = p->prev)
2053 if (p->type == record_full_end)
2056 record_full_goto_entry (p);
2059 /* The "to_goto_record" target method. */
2062 record_full_goto (ULONGEST target_insn)
2064 struct record_full_entry *p = NULL;
2066 for (p = &record_full_first; p != NULL; p = p->next)
2067 if (p->type == record_full_end && p->u.end.insn_num == target_insn)
2070 record_full_goto_entry (p);
2074 init_record_full_ops (void)
2076 record_full_ops.to_shortname = "record-full";
2077 record_full_ops.to_longname = "Process record and replay target";
2078 record_full_ops.to_doc =
2079 "Log program while executing and replay execution from log.";
2080 record_full_ops.to_open = record_full_open;
2081 record_full_ops.to_close = record_full_close;
2082 record_full_ops.to_resume = record_full_resume;
2083 record_full_ops.to_wait = record_full_wait;
2084 record_full_ops.to_disconnect = record_disconnect;
2085 record_full_ops.to_detach = record_detach;
2086 record_full_ops.to_mourn_inferior = record_mourn_inferior;
2087 record_full_ops.to_kill = record_kill;
2088 record_full_ops.to_create_inferior = find_default_create_inferior;
2089 record_full_ops.to_store_registers = record_full_store_registers;
2090 record_full_ops.to_xfer_partial = record_full_xfer_partial;
2091 record_full_ops.to_insert_breakpoint = record_full_insert_breakpoint;
2092 record_full_ops.to_remove_breakpoint = record_full_remove_breakpoint;
2093 record_full_ops.to_stopped_by_watchpoint = record_full_stopped_by_watchpoint;
2094 record_full_ops.to_stopped_data_address = record_full_stopped_data_address;
2095 record_full_ops.to_can_execute_reverse = record_full_can_execute_reverse;
2096 record_full_ops.to_stratum = record_stratum;
2097 /* Add bookmark target methods. */
2098 record_full_ops.to_get_bookmark = record_full_get_bookmark;
2099 record_full_ops.to_goto_bookmark = record_full_goto_bookmark;
2100 record_full_ops.to_async = record_full_async;
2101 record_full_ops.to_can_async_p = record_full_can_async_p;
2102 record_full_ops.to_is_async_p = record_full_is_async_p;
2103 record_full_ops.to_execution_direction = record_full_execution_direction;
2104 record_full_ops.to_info_record = record_full_info;
2105 record_full_ops.to_save_record = record_full_save;
2106 record_full_ops.to_delete_record = record_full_delete;
2107 record_full_ops.to_record_is_replaying = record_full_is_replaying;
2108 record_full_ops.to_goto_record_begin = record_full_goto_begin;
2109 record_full_ops.to_goto_record_end = record_full_goto_end;
2110 record_full_ops.to_goto_record = record_full_goto;
2111 record_full_ops.to_magic = OPS_MAGIC;
2114 /* "to_resume" method for prec over corefile. */
2117 record_full_core_resume (struct target_ops *ops, ptid_t ptid, int step,
2118 enum gdb_signal signal)
2120 record_full_resume_step = step;
2121 record_full_resumed = 1;
2122 record_full_execution_dir = execution_direction;
2124 /* We are about to start executing the inferior (or simulate it),
2125 let's register it with the event loop. */
2126 if (target_can_async_p ())
2128 target_async (inferior_event_handler, 0);
2130 /* Notify the event loop there's an event to wait for. */
2131 mark_async_event_handler (record_full_async_inferior_event_token);
2135 /* "to_kill" method for prec over corefile. */
2138 record_full_core_kill (struct target_ops *ops)
2141 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_core_kill\n");
2143 unpush_target (&record_full_core_ops);
2146 /* "to_fetch_registers" method for prec over corefile. */
2149 record_full_core_fetch_registers (struct target_ops *ops,
2150 struct regcache *regcache,
2155 int num = gdbarch_num_regs (get_regcache_arch (regcache));
2158 for (i = 0; i < num; i ++)
2159 regcache_raw_supply (regcache, i,
2160 record_full_core_regbuf + MAX_REGISTER_SIZE * i);
2163 regcache_raw_supply (regcache, regno,
2164 record_full_core_regbuf + MAX_REGISTER_SIZE * regno);
2167 /* "to_prepare_to_store" method for prec over corefile. */
2170 record_full_core_prepare_to_store (struct target_ops *self,
2171 struct regcache *regcache)
2175 /* "to_store_registers" method for prec over corefile. */
2178 record_full_core_store_registers (struct target_ops *ops,
2179 struct regcache *regcache,
2182 if (record_full_gdb_operation_disable)
2183 regcache_raw_collect (regcache, regno,
2184 record_full_core_regbuf + MAX_REGISTER_SIZE * regno);
2186 error (_("You can't do that without a process to debug."));
2189 /* "to_xfer_partial" method for prec over corefile. */
2192 record_full_core_xfer_partial (struct target_ops *ops,
2193 enum target_object object,
2194 const char *annex, gdb_byte *readbuf,
2195 const gdb_byte *writebuf, ULONGEST offset,
2198 if (object == TARGET_OBJECT_MEMORY)
2200 if (record_full_gdb_operation_disable || !writebuf)
2202 struct target_section *p;
2204 for (p = record_full_core_start; p < record_full_core_end; p++)
2206 if (offset >= p->addr)
2208 struct record_full_core_buf_entry *entry;
2209 ULONGEST sec_offset;
2211 if (offset >= p->endaddr)
2214 if (offset + len > p->endaddr)
2215 len = p->endaddr - offset;
2217 sec_offset = offset - p->addr;
2219 /* Read readbuf or write writebuf p, offset, len. */
2221 if (p->the_bfd_section->flags & SEC_CONSTRUCTOR
2222 || (p->the_bfd_section->flags & SEC_HAS_CONTENTS) == 0)
2225 memset (readbuf, 0, len);
2228 /* Get record_full_core_buf_entry. */
2229 for (entry = record_full_core_buf_list; entry;
2230 entry = entry->prev)
2237 /* Add a new entry. */
2238 entry = (struct record_full_core_buf_entry *)
2240 (sizeof (struct record_full_core_buf_entry));
2242 if (!bfd_malloc_and_get_section
2243 (p->the_bfd_section->owner,
2250 entry->prev = record_full_core_buf_list;
2251 record_full_core_buf_list = entry;
2254 memcpy (entry->buf + sec_offset, writebuf,
2260 return record_full_beneath_to_xfer_partial
2261 (record_full_beneath_to_xfer_partial_ops,
2262 object, annex, readbuf, writebuf,
2265 memcpy (readbuf, entry->buf + sec_offset,
2276 error (_("You can't do that without a process to debug."));
2279 return record_full_beneath_to_xfer_partial
2280 (record_full_beneath_to_xfer_partial_ops, object, annex,
2281 readbuf, writebuf, offset, len);
2284 /* "to_insert_breakpoint" method for prec over corefile. */
2287 record_full_core_insert_breakpoint (struct target_ops *ops,
2288 struct gdbarch *gdbarch,
2289 struct bp_target_info *bp_tgt)
2294 /* "to_remove_breakpoint" method for prec over corefile. */
2297 record_full_core_remove_breakpoint (struct target_ops *ops,
2298 struct gdbarch *gdbarch,
2299 struct bp_target_info *bp_tgt)
2304 /* "to_has_execution" method for prec over corefile. */
2307 record_full_core_has_execution (struct target_ops *ops, ptid_t the_ptid)
2313 init_record_full_core_ops (void)
2315 record_full_core_ops.to_shortname = "record-core";
2316 record_full_core_ops.to_longname = "Process record and replay target";
2317 record_full_core_ops.to_doc =
2318 "Log program while executing and replay execution from log.";
2319 record_full_core_ops.to_open = record_full_open;
2320 record_full_core_ops.to_close = record_full_close;
2321 record_full_core_ops.to_resume = record_full_core_resume;
2322 record_full_core_ops.to_wait = record_full_wait;
2323 record_full_core_ops.to_kill = record_full_core_kill;
2324 record_full_core_ops.to_fetch_registers = record_full_core_fetch_registers;
2325 record_full_core_ops.to_prepare_to_store = record_full_core_prepare_to_store;
2326 record_full_core_ops.to_store_registers = record_full_core_store_registers;
2327 record_full_core_ops.to_xfer_partial = record_full_core_xfer_partial;
2328 record_full_core_ops.to_insert_breakpoint
2329 = record_full_core_insert_breakpoint;
2330 record_full_core_ops.to_remove_breakpoint
2331 = record_full_core_remove_breakpoint;
2332 record_full_core_ops.to_stopped_by_watchpoint
2333 = record_full_stopped_by_watchpoint;
2334 record_full_core_ops.to_stopped_data_address
2335 = record_full_stopped_data_address;
2336 record_full_core_ops.to_can_execute_reverse
2337 = record_full_can_execute_reverse;
2338 record_full_core_ops.to_has_execution = record_full_core_has_execution;
2339 record_full_core_ops.to_stratum = record_stratum;
2340 /* Add bookmark target methods. */
2341 record_full_core_ops.to_get_bookmark = record_full_get_bookmark;
2342 record_full_core_ops.to_goto_bookmark = record_full_goto_bookmark;
2343 record_full_core_ops.to_async = record_full_async;
2344 record_full_core_ops.to_can_async_p = record_full_can_async_p;
2345 record_full_core_ops.to_is_async_p = record_full_is_async_p;
2346 record_full_core_ops.to_execution_direction
2347 = record_full_execution_direction;
2348 record_full_core_ops.to_info_record = record_full_info;
2349 record_full_core_ops.to_delete_record = record_full_delete;
2350 record_full_core_ops.to_record_is_replaying = record_full_is_replaying;
2351 record_full_core_ops.to_goto_record_begin = record_full_goto_begin;
2352 record_full_core_ops.to_goto_record_end = record_full_goto_end;
2353 record_full_core_ops.to_goto_record = record_full_goto;
2354 record_full_core_ops.to_magic = OPS_MAGIC;
2357 /* Record log save-file format
2358 Version 1 (never released)
2361 4 bytes: magic number htonl(0x20090829).
2362 NOTE: be sure to change whenever this file format changes!
2366 1 byte: record type (record_full_end, see enum record_full_type).
2368 1 byte: record type (record_full_reg, see enum record_full_type).
2369 8 bytes: register id (network byte order).
2370 MAX_REGISTER_SIZE bytes: register value.
2372 1 byte: record type (record_full_mem, see enum record_full_type).
2373 8 bytes: memory length (network byte order).
2374 8 bytes: memory address (network byte order).
2375 n bytes: memory value (n == memory length).
2378 4 bytes: magic number netorder32(0x20091016).
2379 NOTE: be sure to change whenever this file format changes!
2383 1 byte: record type (record_full_end, see enum record_full_type).
2385 4 bytes: instruction count
2387 1 byte: record type (record_full_reg, see enum record_full_type).
2388 4 bytes: register id (network byte order).
2389 n bytes: register value (n == actual register size).
2390 (eg. 4 bytes for x86 general registers).
2392 1 byte: record type (record_full_mem, see enum record_full_type).
2393 4 bytes: memory length (network byte order).
2394 8 bytes: memory address (network byte order).
2395 n bytes: memory value (n == memory length).
2399 /* bfdcore_read -- read bytes from a core file section. */
2402 bfdcore_read (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2404 int ret = bfd_get_section_contents (obfd, osec, buf, *offset, len);
2409 error (_("Failed to read %d bytes from core file %s ('%s')."),
2410 len, bfd_get_filename (obfd),
2411 bfd_errmsg (bfd_get_error ()));
2414 static inline uint64_t
2415 netorder64 (uint64_t input)
2419 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2420 BFD_ENDIAN_BIG, input);
2424 static inline uint32_t
2425 netorder32 (uint32_t input)
2429 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2430 BFD_ENDIAN_BIG, input);
2434 static inline uint16_t
2435 netorder16 (uint16_t input)
2439 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2440 BFD_ENDIAN_BIG, input);
2444 /* Restore the execution log from a core_bfd file. */
2446 record_full_restore (void)
2449 struct cleanup *old_cleanups;
2450 struct record_full_entry *rec;
2454 struct regcache *regcache;
2456 /* We restore the execution log from the open core bfd,
2458 if (core_bfd == NULL)
2461 /* "record_full_restore" can only be called when record list is empty. */
2462 gdb_assert (record_full_first.next == NULL);
2465 fprintf_unfiltered (gdb_stdlog, "Restoring recording from core file.\n");
2467 /* Now need to find our special note section. */
2468 osec = bfd_get_section_by_name (core_bfd, "null0");
2470 fprintf_unfiltered (gdb_stdlog, "Find precord section %s.\n",
2471 osec ? "succeeded" : "failed");
2474 osec_size = bfd_section_size (core_bfd, osec);
2476 fprintf_unfiltered (gdb_stdlog, "%s", bfd_section_name (core_bfd, osec));
2478 /* Check the magic code. */
2479 bfdcore_read (core_bfd, osec, &magic, sizeof (magic), &bfd_offset);
2480 if (magic != RECORD_FULL_FILE_MAGIC)
2481 error (_("Version mis-match or file format error in core file %s."),
2482 bfd_get_filename (core_bfd));
2484 fprintf_unfiltered (gdb_stdlog,
2485 " Reading 4-byte magic cookie "
2486 "RECORD_FULL_FILE_MAGIC (0x%s)\n",
2487 phex_nz (netorder32 (magic), 4));
2489 /* Restore the entries in recfd into record_full_arch_list_head and
2490 record_full_arch_list_tail. */
2491 record_full_arch_list_head = NULL;
2492 record_full_arch_list_tail = NULL;
2493 record_full_insn_num = 0;
2494 old_cleanups = make_cleanup (record_full_arch_list_cleanups, 0);
2495 regcache = get_current_regcache ();
2500 uint32_t regnum, len, signal, count;
2503 /* We are finished when offset reaches osec_size. */
2504 if (bfd_offset >= osec_size)
2506 bfdcore_read (core_bfd, osec, &rectype, sizeof (rectype), &bfd_offset);
2510 case record_full_reg: /* reg */
2511 /* Get register number to regnum. */
2512 bfdcore_read (core_bfd, osec, ®num,
2513 sizeof (regnum), &bfd_offset);
2514 regnum = netorder32 (regnum);
2516 rec = record_full_reg_alloc (regcache, regnum);
2519 bfdcore_read (core_bfd, osec, record_full_get_loc (rec),
2520 rec->u.reg.len, &bfd_offset);
2523 fprintf_unfiltered (gdb_stdlog,
2524 " Reading register %d (1 "
2525 "plus %lu plus %d bytes)\n",
2527 (unsigned long) sizeof (regnum),
2531 case record_full_mem: /* mem */
2533 bfdcore_read (core_bfd, osec, &len,
2534 sizeof (len), &bfd_offset);
2535 len = netorder32 (len);
2538 bfdcore_read (core_bfd, osec, &addr,
2539 sizeof (addr), &bfd_offset);
2540 addr = netorder64 (addr);
2542 rec = record_full_mem_alloc (addr, len);
2545 bfdcore_read (core_bfd, osec, record_full_get_loc (rec),
2546 rec->u.mem.len, &bfd_offset);
2549 fprintf_unfiltered (gdb_stdlog,
2550 " Reading memory %s (1 plus "
2551 "%lu plus %lu plus %d bytes)\n",
2552 paddress (get_current_arch (),
2554 (unsigned long) sizeof (addr),
2555 (unsigned long) sizeof (len),
2559 case record_full_end: /* end */
2560 rec = record_full_end_alloc ();
2561 record_full_insn_num ++;
2563 /* Get signal value. */
2564 bfdcore_read (core_bfd, osec, &signal,
2565 sizeof (signal), &bfd_offset);
2566 signal = netorder32 (signal);
2567 rec->u.end.sigval = signal;
2569 /* Get insn count. */
2570 bfdcore_read (core_bfd, osec, &count,
2571 sizeof (count), &bfd_offset);
2572 count = netorder32 (count);
2573 rec->u.end.insn_num = count;
2574 record_full_insn_count = count + 1;
2576 fprintf_unfiltered (gdb_stdlog,
2577 " Reading record_full_end (1 + "
2578 "%lu + %lu bytes), offset == %s\n",
2579 (unsigned long) sizeof (signal),
2580 (unsigned long) sizeof (count),
2581 paddress (get_current_arch (),
2586 error (_("Bad entry type in core file %s."),
2587 bfd_get_filename (core_bfd));
2591 /* Add rec to record arch list. */
2592 record_full_arch_list_add (rec);
2595 discard_cleanups (old_cleanups);
2597 /* Add record_full_arch_list_head to the end of record list. */
2598 record_full_first.next = record_full_arch_list_head;
2599 record_full_arch_list_head->prev = &record_full_first;
2600 record_full_arch_list_tail->next = NULL;
2601 record_full_list = &record_full_first;
2603 /* Update record_full_insn_max_num. */
2604 if (record_full_insn_num > record_full_insn_max_num)
2606 record_full_insn_max_num = record_full_insn_num;
2607 warning (_("Auto increase record/replay buffer limit to %u."),
2608 record_full_insn_max_num);
2612 printf_filtered (_("Restored records from core file %s.\n"),
2613 bfd_get_filename (core_bfd));
2615 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
2618 /* bfdcore_write -- write bytes into a core file section. */
2621 bfdcore_write (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2623 int ret = bfd_set_section_contents (obfd, osec, buf, *offset, len);
2628 error (_("Failed to write %d bytes to core file %s ('%s')."),
2629 len, bfd_get_filename (obfd),
2630 bfd_errmsg (bfd_get_error ()));
2633 /* Restore the execution log from a file. We use a modified elf
2634 corefile format, with an extra section for our data. */
2637 cmd_record_full_restore (char *args, int from_tty)
2639 core_file_command (args, from_tty);
2640 record_full_open (args, from_tty);
2644 record_full_save_cleanups (void *data)
2647 char *pathname = xstrdup (bfd_get_filename (obfd));
2649 gdb_bfd_unref (obfd);
2654 /* Save the execution log to a file. We use a modified elf corefile
2655 format, with an extra section for our data. */
2658 record_full_save (const char *recfilename)
2660 struct record_full_entry *cur_record_full_list;
2662 struct regcache *regcache;
2663 struct gdbarch *gdbarch;
2664 struct cleanup *old_cleanups;
2665 struct cleanup *set_cleanups;
2668 asection *osec = NULL;
2671 /* Open the save file. */
2673 fprintf_unfiltered (gdb_stdlog, "Saving execution log to core file '%s'\n",
2676 /* Open the output file. */
2677 obfd = create_gcore_bfd (recfilename);
2678 old_cleanups = make_cleanup (record_full_save_cleanups, obfd);
2680 /* Save the current record entry to "cur_record_full_list". */
2681 cur_record_full_list = record_full_list;
2683 /* Get the values of regcache and gdbarch. */
2684 regcache = get_current_regcache ();
2685 gdbarch = get_regcache_arch (regcache);
2687 /* Disable the GDB operation record. */
2688 set_cleanups = record_full_gdb_operation_disable_set ();
2690 /* Reverse execute to the begin of record list. */
2693 /* Check for beginning and end of log. */
2694 if (record_full_list == &record_full_first)
2697 record_full_exec_insn (regcache, gdbarch, record_full_list);
2699 if (record_full_list->prev)
2700 record_full_list = record_full_list->prev;
2703 /* Compute the size needed for the extra bfd section. */
2704 save_size = 4; /* magic cookie */
2705 for (record_full_list = record_full_first.next; record_full_list;
2706 record_full_list = record_full_list->next)
2707 switch (record_full_list->type)
2709 case record_full_end:
2710 save_size += 1 + 4 + 4;
2712 case record_full_reg:
2713 save_size += 1 + 4 + record_full_list->u.reg.len;
2715 case record_full_mem:
2716 save_size += 1 + 4 + 8 + record_full_list->u.mem.len;
2720 /* Make the new bfd section. */
2721 osec = bfd_make_section_anyway_with_flags (obfd, "precord",
2725 error (_("Failed to create 'precord' section for corefile %s: %s"),
2727 bfd_errmsg (bfd_get_error ()));
2728 bfd_set_section_size (obfd, osec, save_size);
2729 bfd_set_section_vma (obfd, osec, 0);
2730 bfd_set_section_alignment (obfd, osec, 0);
2731 bfd_section_lma (obfd, osec) = 0;
2733 /* Save corefile state. */
2734 write_gcore_file (obfd);
2736 /* Write out the record log. */
2737 /* Write the magic code. */
2738 magic = RECORD_FULL_FILE_MAGIC;
2740 fprintf_unfiltered (gdb_stdlog,
2741 " Writing 4-byte magic cookie "
2742 "RECORD_FULL_FILE_MAGIC (0x%s)\n",
2743 phex_nz (magic, 4));
2744 bfdcore_write (obfd, osec, &magic, sizeof (magic), &bfd_offset);
2746 /* Save the entries to recfd and forward execute to the end of
2748 record_full_list = &record_full_first;
2752 if (record_full_list != &record_full_first)
2755 uint32_t regnum, len, signal, count;
2758 type = record_full_list->type;
2759 bfdcore_write (obfd, osec, &type, sizeof (type), &bfd_offset);
2761 switch (record_full_list->type)
2763 case record_full_reg: /* reg */
2765 fprintf_unfiltered (gdb_stdlog,
2766 " Writing register %d (1 "
2767 "plus %lu plus %d bytes)\n",
2768 record_full_list->u.reg.num,
2769 (unsigned long) sizeof (regnum),
2770 record_full_list->u.reg.len);
2773 regnum = netorder32 (record_full_list->u.reg.num);
2774 bfdcore_write (obfd, osec, ®num,
2775 sizeof (regnum), &bfd_offset);
2778 bfdcore_write (obfd, osec,
2779 record_full_get_loc (record_full_list),
2780 record_full_list->u.reg.len, &bfd_offset);
2783 case record_full_mem: /* mem */
2785 fprintf_unfiltered (gdb_stdlog,
2786 " Writing memory %s (1 plus "
2787 "%lu plus %lu plus %d bytes)\n",
2789 record_full_list->u.mem.addr),
2790 (unsigned long) sizeof (addr),
2791 (unsigned long) sizeof (len),
2792 record_full_list->u.mem.len);
2795 len = netorder32 (record_full_list->u.mem.len);
2796 bfdcore_write (obfd, osec, &len, sizeof (len), &bfd_offset);
2798 /* Write memaddr. */
2799 addr = netorder64 (record_full_list->u.mem.addr);
2800 bfdcore_write (obfd, osec, &addr,
2801 sizeof (addr), &bfd_offset);
2804 bfdcore_write (obfd, osec,
2805 record_full_get_loc (record_full_list),
2806 record_full_list->u.mem.len, &bfd_offset);
2809 case record_full_end:
2811 fprintf_unfiltered (gdb_stdlog,
2812 " Writing record_full_end (1 + "
2813 "%lu + %lu bytes)\n",
2814 (unsigned long) sizeof (signal),
2815 (unsigned long) sizeof (count));
2816 /* Write signal value. */
2817 signal = netorder32 (record_full_list->u.end.sigval);
2818 bfdcore_write (obfd, osec, &signal,
2819 sizeof (signal), &bfd_offset);
2821 /* Write insn count. */
2822 count = netorder32 (record_full_list->u.end.insn_num);
2823 bfdcore_write (obfd, osec, &count,
2824 sizeof (count), &bfd_offset);
2829 /* Execute entry. */
2830 record_full_exec_insn (regcache, gdbarch, record_full_list);
2832 if (record_full_list->next)
2833 record_full_list = record_full_list->next;
2838 /* Reverse execute to cur_record_full_list. */
2841 /* Check for beginning and end of log. */
2842 if (record_full_list == cur_record_full_list)
2845 record_full_exec_insn (regcache, gdbarch, record_full_list);
2847 if (record_full_list->prev)
2848 record_full_list = record_full_list->prev;
2851 do_cleanups (set_cleanups);
2852 gdb_bfd_unref (obfd);
2853 discard_cleanups (old_cleanups);
2856 printf_filtered (_("Saved core file %s with execution log.\n"),
2860 /* record_full_goto_insn -- rewind the record log (forward or backward,
2861 depending on DIR) to the given entry, changing the program state
2865 record_full_goto_insn (struct record_full_entry *entry,
2866 enum exec_direction_kind dir)
2868 struct cleanup *set_cleanups = record_full_gdb_operation_disable_set ();
2869 struct regcache *regcache = get_current_regcache ();
2870 struct gdbarch *gdbarch = get_regcache_arch (regcache);
2872 /* Assume everything is valid: we will hit the entry,
2873 and we will not hit the end of the recording. */
2875 if (dir == EXEC_FORWARD)
2876 record_full_list = record_full_list->next;
2880 record_full_exec_insn (regcache, gdbarch, record_full_list);
2881 if (dir == EXEC_REVERSE)
2882 record_full_list = record_full_list->prev;
2884 record_full_list = record_full_list->next;
2885 } while (record_full_list != entry);
2886 do_cleanups (set_cleanups);
2889 /* Alias for "target record-full". */
2892 cmd_record_full_start (char *args, int from_tty)
2894 execute_command ("target record-full", from_tty);
2898 set_record_full_insn_max_num (char *args, int from_tty,
2899 struct cmd_list_element *c)
2901 if (record_full_insn_num > record_full_insn_max_num)
2903 /* Count down record_full_insn_num while releasing records from list. */
2904 while (record_full_insn_num > record_full_insn_max_num)
2906 record_full_list_release_first ();
2907 record_full_insn_num--;
2912 /* The "set record full" command. */
2915 set_record_full_command (char *args, int from_tty)
2917 printf_unfiltered (_("\"set record full\" must be followed "
2918 "by an apporpriate subcommand.\n"));
2919 help_list (set_record_full_cmdlist, "set record full ", all_commands,
2923 /* The "show record full" command. */
2926 show_record_full_command (char *args, int from_tty)
2928 cmd_show_list (show_record_full_cmdlist, from_tty, "");
2931 /* Provide a prototype to silence -Wmissing-prototypes. */
2932 extern initialize_file_ftype _initialize_record_full;
2935 _initialize_record_full (void)
2937 struct cmd_list_element *c;
2939 /* Init record_full_first. */
2940 record_full_first.prev = NULL;
2941 record_full_first.next = NULL;
2942 record_full_first.type = record_full_end;
2944 init_record_full_ops ();
2945 add_target (&record_full_ops);
2946 add_deprecated_target_alias (&record_full_ops, "record");
2947 init_record_full_core_ops ();
2948 add_target (&record_full_core_ops);
2950 add_prefix_cmd ("full", class_obscure, cmd_record_full_start,
2951 _("Start full execution recording."), &record_full_cmdlist,
2952 "record full ", 0, &record_cmdlist);
2954 c = add_cmd ("restore", class_obscure, cmd_record_full_restore,
2955 _("Restore the execution log from a file.\n\
2956 Argument is filename. File must be created with 'record save'."),
2957 &record_full_cmdlist);
2958 set_cmd_completer (c, filename_completer);
2960 /* Deprecate the old version without "full" prefix. */
2961 c = add_alias_cmd ("restore", "full restore", class_obscure, 1,
2963 set_cmd_completer (c, filename_completer);
2964 deprecate_cmd (c, "record full restore");
2966 add_prefix_cmd ("full", class_support, set_record_full_command,
2967 _("Set record options"), &set_record_full_cmdlist,
2968 "set record full ", 0, &set_record_cmdlist);
2970 add_prefix_cmd ("full", class_support, show_record_full_command,
2971 _("Show record options"), &show_record_full_cmdlist,
2972 "show record full ", 0, &show_record_cmdlist);
2974 /* Record instructions number limit command. */
2975 add_setshow_boolean_cmd ("stop-at-limit", no_class,
2976 &record_full_stop_at_limit, _("\
2977 Set whether record/replay stops when record/replay buffer becomes full."), _("\
2978 Show whether record/replay stops when record/replay buffer becomes full."),
2979 _("Default is ON.\n\
2980 When ON, if the record/replay buffer becomes full, ask user what to do.\n\
2981 When OFF, if the record/replay buffer becomes full,\n\
2982 delete the oldest recorded instruction to make room for each new one."),
2984 &set_record_full_cmdlist, &show_record_full_cmdlist);
2986 c = add_alias_cmd ("stop-at-limit", "full stop-at-limit", no_class, 1,
2987 &set_record_cmdlist);
2988 deprecate_cmd (c, "set record full stop-at-limit");
2990 c = add_alias_cmd ("stop-at-limit", "full stop-at-limit", no_class, 1,
2991 &show_record_cmdlist);
2992 deprecate_cmd (c, "show record full stop-at-limit");
2994 add_setshow_uinteger_cmd ("insn-number-max", no_class,
2995 &record_full_insn_max_num,
2996 _("Set record/replay buffer limit."),
2997 _("Show record/replay buffer limit."), _("\
2998 Set the maximum number of instructions to be stored in the\n\
2999 record/replay buffer. A value of either \"unlimited\" or zero means no\n\
3000 limit. Default is 200000."),
3001 set_record_full_insn_max_num,
3002 NULL, &set_record_full_cmdlist,
3003 &show_record_full_cmdlist);
3005 c = add_alias_cmd ("insn-number-max", "full insn-number-max", no_class, 1,
3006 &set_record_cmdlist);
3007 deprecate_cmd (c, "set record full insn-number-max");
3009 c = add_alias_cmd ("insn-number-max", "full insn-number-max", no_class, 1,
3010 &show_record_cmdlist);
3011 deprecate_cmd (c, "show record full insn-number-max");
3013 add_setshow_boolean_cmd ("memory-query", no_class,
3014 &record_full_memory_query, _("\
3015 Set whether query if PREC cannot record memory change of next instruction."),
3017 Show whether query if PREC cannot record memory change of next instruction."),
3020 When ON, query if PREC cannot record memory change of next instruction."),
3022 &set_record_full_cmdlist,
3023 &show_record_full_cmdlist);
3025 c = add_alias_cmd ("memory-query", "full memory-query", no_class, 1,
3026 &set_record_cmdlist);
3027 deprecate_cmd (c, "set record full memory-query");
3029 c = add_alias_cmd ("memory-query", "full memory-query", no_class, 1,
3030 &show_record_cmdlist);
3031 deprecate_cmd (c, "show record full memory-query");