1 /* Process record and replay target for GDB, the GNU debugger.
3 Copyright (C) 2008, 2009 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"
37 /* This module implements "target record", also known as "process
38 record and replay". This target sits on top of a "normal" target
39 (a target that "has execution"), and provides a record and replay
40 functionality, including reverse debugging.
42 Target record has two modes: recording, and replaying.
44 In record mode, we intercept the to_resume and to_wait methods.
45 Whenever gdb resumes the target, we run the target in single step
46 mode, and we build up an execution log in which, for each executed
47 instruction, we record all changes in memory and register state.
48 This is invisible to the user, to whom it just looks like an
49 ordinary debugging session (except for performance degredation).
51 In replay mode, instead of actually letting the inferior run as a
52 process, we simulate its execution by playing back the recorded
53 execution log. For each instruction in the log, we simulate the
54 instruction's side effects by duplicating the changes that it would
55 have made on memory and registers. */
57 #define DEFAULT_RECORD_INSN_MAX_NUM 200000
59 #define RECORD_IS_REPLAY \
60 (record_list->next || execution_direction == EXEC_REVERSE)
62 #define RECORD_FILE_MAGIC netorder32(0x20091016)
64 /* These are the core structs of the process record functionality.
66 A record_entry is a record of the value change of a register
67 ("record_reg") or a part of memory ("record_mem"). And each
68 instruction must have a struct record_entry ("record_end") that
69 indicates that this is the last struct record_entry of this
72 Each struct record_entry is linked to "record_list" by "prev" and
75 struct record_mem_entry
79 /* Set this flag if target memory for this entry
80 can no longer be accessed. */
81 int mem_entry_not_accessible
;
85 gdb_byte buf
[sizeof (gdb_byte
*)];
89 struct record_reg_entry
96 gdb_byte buf
[2 * sizeof (gdb_byte
*)];
100 struct record_end_entry
102 enum target_signal sigval
;
113 /* This is the data structure that makes up the execution log.
115 The execution log consists of a single linked list of entries
116 of type "struct record_entry". It is doubly linked so that it
117 can be traversed in either direction.
119 The start of the list is anchored by a struct called
120 "record_first". The pointer "record_list" either points to the
121 last entry that was added to the list (in record mode), or to the
122 next entry in the list that will be executed (in replay mode).
124 Each list element (struct record_entry), in addition to next and
125 prev pointers, consists of a union of three entry types: mem, reg,
126 and end. A field called "type" determines which entry type is
127 represented by a given list element.
129 Each instruction that is added to the execution log is represented
130 by a variable number of list elements ('entries'). The instruction
131 will have one "reg" entry for each register that is changed by
132 executing the instruction (including the PC in every case). It
133 will also have one "mem" entry for each memory change. Finally,
134 each instruction will have an "end" entry that separates it from
135 the changes associated with the next instruction. */
139 struct record_entry
*prev
;
140 struct record_entry
*next
;
141 enum record_type type
;
145 struct record_reg_entry reg
;
147 struct record_mem_entry mem
;
149 struct record_end_entry end
;
153 /* This is the debug switch for process record. */
154 int record_debug
= 0;
156 struct record_core_buf_entry
158 struct record_core_buf_entry
*prev
;
159 struct target_section
*p
;
163 /* Record buf with core target. */
164 static gdb_byte
*record_core_regbuf
= NULL
;
165 static struct target_section
*record_core_start
;
166 static struct target_section
*record_core_end
;
167 static struct record_core_buf_entry
*record_core_buf_list
= NULL
;
169 /* The following variables are used for managing the linked list that
170 represents the execution log.
172 record_first is the anchor that holds down the beginning of the list.
174 record_list serves two functions:
175 1) In record mode, it anchors the end of the list.
176 2) In replay mode, it traverses the list and points to
177 the next instruction that must be emulated.
179 record_arch_list_head and record_arch_list_tail are used to manage
180 a separate list, which is used to build up the change elements of
181 the currently executing instruction during record mode. When this
182 instruction has been completely annotated in the "arch list", it
183 will be appended to the main execution log. */
185 static struct record_entry record_first
;
186 static struct record_entry
*record_list
= &record_first
;
187 static struct record_entry
*record_arch_list_head
= NULL
;
188 static struct record_entry
*record_arch_list_tail
= NULL
;
190 /* 1 ask user. 0 auto delete the last struct record_entry. */
191 static int record_stop_at_limit
= 1;
192 /* Maximum allowed number of insns in execution log. */
193 static unsigned int record_insn_max_num
= DEFAULT_RECORD_INSN_MAX_NUM
;
194 /* Actual count of insns presently in execution log. */
195 static int record_insn_num
= 0;
196 /* Count of insns logged so far (may be larger
197 than count of insns presently in execution log). */
198 static ULONGEST record_insn_count
;
200 /* The target_ops of process record. */
201 static struct target_ops record_ops
;
202 static struct target_ops record_core_ops
;
204 /* The beneath function pointers. */
205 static struct target_ops
*record_beneath_to_resume_ops
;
206 static void (*record_beneath_to_resume
) (struct target_ops
*, ptid_t
, int,
208 static struct target_ops
*record_beneath_to_wait_ops
;
209 static ptid_t (*record_beneath_to_wait
) (struct target_ops
*, ptid_t
,
210 struct target_waitstatus
*,
212 static struct target_ops
*record_beneath_to_store_registers_ops
;
213 static void (*record_beneath_to_store_registers
) (struct target_ops
*,
216 static struct target_ops
*record_beneath_to_xfer_partial_ops
;
217 static LONGEST (*record_beneath_to_xfer_partial
) (struct target_ops
*ops
,
218 enum target_object object
,
221 const gdb_byte
*writebuf
,
224 static int (*record_beneath_to_insert_breakpoint
) (struct gdbarch
*,
225 struct bp_target_info
*);
226 static int (*record_beneath_to_remove_breakpoint
) (struct gdbarch
*,
227 struct bp_target_info
*);
229 /* Alloc and free functions for record_reg, record_mem, and record_end
232 /* Alloc a record_reg record entry. */
234 static inline struct record_entry
*
235 record_reg_alloc (struct regcache
*regcache
, int regnum
)
237 struct record_entry
*rec
;
238 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
240 rec
= (struct record_entry
*) xcalloc (1, sizeof (struct record_entry
));
241 rec
->type
= record_reg
;
242 rec
->u
.reg
.num
= regnum
;
243 rec
->u
.reg
.len
= register_size (gdbarch
, regnum
);
244 if (rec
->u
.reg
.len
> sizeof (rec
->u
.reg
.u
.buf
))
245 rec
->u
.reg
.u
.ptr
= (gdb_byte
*) xmalloc (rec
->u
.reg
.len
);
250 /* Free a record_reg record entry. */
253 record_reg_release (struct record_entry
*rec
)
255 gdb_assert (rec
->type
== record_reg
);
256 if (rec
->u
.reg
.len
> sizeof (rec
->u
.reg
.u
.buf
))
257 xfree (rec
->u
.reg
.u
.ptr
);
261 /* Alloc a record_mem record entry. */
263 static inline struct record_entry
*
264 record_mem_alloc (CORE_ADDR addr
, int len
)
266 struct record_entry
*rec
;
268 rec
= (struct record_entry
*) xcalloc (1, sizeof (struct record_entry
));
269 rec
->type
= record_mem
;
270 rec
->u
.mem
.addr
= addr
;
271 rec
->u
.mem
.len
= len
;
272 if (rec
->u
.mem
.len
> sizeof (rec
->u
.mem
.u
.buf
))
273 rec
->u
.mem
.u
.ptr
= (gdb_byte
*) xmalloc (len
);
278 /* Free a record_mem record entry. */
281 record_mem_release (struct record_entry
*rec
)
283 gdb_assert (rec
->type
== record_mem
);
284 if (rec
->u
.mem
.len
> sizeof (rec
->u
.mem
.u
.buf
))
285 xfree (rec
->u
.mem
.u
.ptr
);
289 /* Alloc a record_end record entry. */
291 static inline struct record_entry
*
292 record_end_alloc (void)
294 struct record_entry
*rec
;
296 rec
= (struct record_entry
*) xcalloc (1, sizeof (struct record_entry
));
297 rec
->type
= record_end
;
302 /* Free a record_end record entry. */
305 record_end_release (struct record_entry
*rec
)
310 /* Free one record entry, any type.
311 Return entry->type, in case caller wants to know. */
313 static inline enum record_type
314 record_entry_release (struct record_entry
*rec
)
316 enum record_type type
= rec
->type
;
320 record_reg_release (rec
);
323 record_mem_release (rec
);
326 record_end_release (rec
);
332 /* Free all record entries in list pointed to by REC. */
335 record_list_release (struct record_entry
*rec
)
346 record_entry_release (rec
->next
);
349 if (rec
== &record_first
)
352 record_first
.next
= NULL
;
355 record_entry_release (rec
);
358 /* Free all record entries forward of the given list position. */
361 record_list_release_following (struct record_entry
*rec
)
363 struct record_entry
*tmp
= rec
->next
;
369 if (record_entry_release (tmp
) == record_end
)
378 /* Delete the first instruction from the beginning of the log, to make
379 room for adding a new instruction at the end of the log.
381 Note -- this function does not modify record_insn_num. */
384 record_list_release_first (void)
386 struct record_entry
*tmp
;
388 if (!record_first
.next
)
391 /* Loop until a record_end. */
394 /* Cut record_first.next out of the linked list. */
395 tmp
= record_first
.next
;
396 record_first
.next
= tmp
->next
;
397 tmp
->next
->prev
= &record_first
;
399 /* tmp is now isolated, and can be deleted. */
400 if (record_entry_release (tmp
) == record_end
)
401 break; /* End loop at first record_end. */
403 if (!record_first
.next
)
405 gdb_assert (record_insn_num
== 1);
406 break; /* End loop when list is empty. */
411 /* Add a struct record_entry to record_arch_list. */
414 record_arch_list_add (struct record_entry
*rec
)
416 if (record_debug
> 1)
417 fprintf_unfiltered (gdb_stdlog
,
418 "Process record: record_arch_list_add %s.\n",
419 host_address_to_string (rec
));
421 if (record_arch_list_tail
)
423 record_arch_list_tail
->next
= rec
;
424 rec
->prev
= record_arch_list_tail
;
425 record_arch_list_tail
= rec
;
429 record_arch_list_head
= rec
;
430 record_arch_list_tail
= rec
;
434 /* Return the value storage location of a record entry. */
435 static inline gdb_byte
*
436 record_get_loc (struct record_entry
*rec
)
440 if (rec
->u
.mem
.len
> sizeof (rec
->u
.mem
.u
.buf
))
441 return rec
->u
.mem
.u
.ptr
;
443 return rec
->u
.mem
.u
.buf
;
445 if (rec
->u
.reg
.len
> sizeof (rec
->u
.reg
.u
.buf
))
446 return rec
->u
.reg
.u
.ptr
;
448 return rec
->u
.reg
.u
.buf
;
456 /* Record the value of a register NUM to record_arch_list. */
459 record_arch_list_add_reg (struct regcache
*regcache
, int regnum
)
461 struct record_entry
*rec
;
463 if (record_debug
> 1)
464 fprintf_unfiltered (gdb_stdlog
,
465 "Process record: add register num = %d to "
469 rec
= record_reg_alloc (regcache
, regnum
);
471 regcache_raw_read (regcache
, regnum
, record_get_loc (rec
));
473 record_arch_list_add (rec
);
478 /* Record the value of a region of memory whose address is ADDR and
479 length is LEN to record_arch_list. */
482 record_arch_list_add_mem (CORE_ADDR addr
, int len
)
484 struct record_entry
*rec
;
486 if (record_debug
> 1)
487 fprintf_unfiltered (gdb_stdlog
,
488 "Process record: add mem addr = %s len = %d to "
490 paddress (target_gdbarch
, addr
), len
);
492 if (!addr
) /* FIXME: Why? Some arch must permit it... */
495 rec
= record_mem_alloc (addr
, len
);
497 if (target_read_memory (addr
, record_get_loc (rec
), len
))
500 fprintf_unfiltered (gdb_stdlog
,
501 "Process record: error reading memory at "
502 "addr = %s len = %d.\n",
503 paddress (target_gdbarch
, addr
), len
);
504 record_mem_release (rec
);
508 record_arch_list_add (rec
);
513 /* Add a record_end type struct record_entry to record_arch_list. */
516 record_arch_list_add_end (void)
518 struct record_entry
*rec
;
520 if (record_debug
> 1)
521 fprintf_unfiltered (gdb_stdlog
,
522 "Process record: add end to arch list.\n");
524 rec
= record_end_alloc ();
525 rec
->u
.end
.sigval
= TARGET_SIGNAL_0
;
526 rec
->u
.end
.insn_num
= ++record_insn_count
;
528 record_arch_list_add (rec
);
534 record_check_insn_num (int set_terminal
)
536 if (record_insn_max_num
)
538 gdb_assert (record_insn_num
<= record_insn_max_num
);
539 if (record_insn_num
== record_insn_max_num
)
541 /* Ask user what to do. */
542 if (record_stop_at_limit
)
546 target_terminal_ours ();
547 q
= yquery (_("Do you want to auto delete previous execution "
548 "log entries when record/replay buffer becomes "
549 "full (record stop-at-limit)?"));
551 target_terminal_inferior ();
553 record_stop_at_limit
= 0;
555 error (_("Process record: stopped by user."));
562 record_arch_list_cleanups (void *ignore
)
564 record_list_release (record_arch_list_tail
);
567 /* Before inferior step (when GDB record the running message, inferior
568 only can step), GDB will call this function to record the values to
569 record_list. This function will call gdbarch_process_record to
570 record the running message of inferior and set them to
571 record_arch_list, and add it to record_list. */
573 struct record_message_args
{
574 struct regcache
*regcache
;
575 enum target_signal signal
;
579 record_message (void *args
)
582 struct record_message_args
*myargs
= args
;
583 struct gdbarch
*gdbarch
= get_regcache_arch (myargs
->regcache
);
584 struct cleanup
*old_cleanups
= make_cleanup (record_arch_list_cleanups
, 0);
586 record_arch_list_head
= NULL
;
587 record_arch_list_tail
= NULL
;
589 /* Check record_insn_num. */
590 record_check_insn_num (1);
592 /* If gdb sends a signal value to target_resume,
593 save it in the 'end' field of the previous instruction.
595 Maybe process record should record what really happened,
596 rather than what gdb pretends has happened.
598 So if Linux delivered the signal to the child process during
599 the record mode, we will record it and deliver it again in
602 If user says "ignore this signal" during the record mode, then
603 it will be ignored again during the replay mode (no matter if
604 the user says something different, like "deliver this signal"
605 during the replay mode).
607 User should understand that nothing he does during the replay
608 mode will change the behavior of the child. If he tries,
609 then that is a user error.
611 But we should still deliver the signal to gdb during the replay,
612 if we delivered it during the recording. Therefore we should
613 record the signal during record_wait, not record_resume. */
614 if (record_list
!= &record_first
) /* FIXME better way to check */
616 gdb_assert (record_list
->type
== record_end
);
617 record_list
->u
.end
.sigval
= myargs
->signal
;
620 if (myargs
->signal
== TARGET_SIGNAL_0
621 || !gdbarch_process_record_signal_p (gdbarch
))
622 ret
= gdbarch_process_record (gdbarch
,
624 regcache_read_pc (myargs
->regcache
));
626 ret
= gdbarch_process_record_signal (gdbarch
,
631 error (_("Process record: inferior program stopped."));
633 error (_("Process record: failed to record execution log."));
635 discard_cleanups (old_cleanups
);
637 record_list
->next
= record_arch_list_head
;
638 record_arch_list_head
->prev
= record_list
;
639 record_list
= record_arch_list_tail
;
641 if (record_insn_num
== record_insn_max_num
&& record_insn_max_num
)
642 record_list_release_first ();
650 do_record_message (struct regcache
*regcache
,
651 enum target_signal signal
)
653 struct record_message_args args
;
655 args
.regcache
= regcache
;
656 args
.signal
= signal
;
657 return catch_errors (record_message
, &args
, NULL
, RETURN_MASK_ALL
);
660 /* Set to 1 if record_store_registers and record_xfer_partial
661 doesn't need record. */
663 static int record_gdb_operation_disable
= 0;
666 record_gdb_operation_disable_set (void)
668 struct cleanup
*old_cleanups
= NULL
;
671 make_cleanup_restore_integer (&record_gdb_operation_disable
);
672 record_gdb_operation_disable
= 1;
677 /* Execute one instruction from the record log. Each instruction in
678 the log will be represented by an arbitrary sequence of register
679 entries and memory entries, followed by an 'end' entry. */
682 record_exec_insn (struct regcache
*regcache
, struct gdbarch
*gdbarch
,
683 struct record_entry
*entry
)
687 case record_reg
: /* reg */
689 gdb_byte reg
[MAX_REGISTER_SIZE
];
691 if (record_debug
> 1)
692 fprintf_unfiltered (gdb_stdlog
,
693 "Process record: record_reg %s to "
694 "inferior num = %d.\n",
695 host_address_to_string (entry
),
698 regcache_cooked_read (regcache
, entry
->u
.reg
.num
, reg
);
699 regcache_cooked_write (regcache
, entry
->u
.reg
.num
,
700 record_get_loc (entry
));
701 memcpy (record_get_loc (entry
), reg
, entry
->u
.reg
.len
);
705 case record_mem
: /* mem */
707 /* Nothing to do if the entry is flagged not_accessible. */
708 if (!entry
->u
.mem
.mem_entry_not_accessible
)
710 gdb_byte
*mem
= alloca (entry
->u
.mem
.len
);
712 if (record_debug
> 1)
713 fprintf_unfiltered (gdb_stdlog
,
714 "Process record: record_mem %s to "
715 "inferior addr = %s len = %d.\n",
716 host_address_to_string (entry
),
717 paddress (gdbarch
, entry
->u
.mem
.addr
),
720 if (target_read_memory (entry
->u
.mem
.addr
, mem
, entry
->u
.mem
.len
))
722 entry
->u
.mem
.mem_entry_not_accessible
= 1;
724 warning ("Process record: error reading memory at "
725 "addr = %s len = %d.",
726 paddress (gdbarch
, entry
->u
.mem
.addr
),
731 if (target_write_memory (entry
->u
.mem
.addr
,
732 record_get_loc (entry
),
735 entry
->u
.mem
.mem_entry_not_accessible
= 1;
737 warning ("Process record: error writing memory at "
738 "addr = %s len = %d.",
739 paddress (gdbarch
, entry
->u
.mem
.addr
),
743 memcpy (record_get_loc (entry
), mem
, entry
->u
.mem
.len
);
751 static struct target_ops
*tmp_to_resume_ops
;
752 static void (*tmp_to_resume
) (struct target_ops
*, ptid_t
, int,
754 static struct target_ops
*tmp_to_wait_ops
;
755 static ptid_t (*tmp_to_wait
) (struct target_ops
*, ptid_t
,
756 struct target_waitstatus
*,
758 static struct target_ops
*tmp_to_store_registers_ops
;
759 static void (*tmp_to_store_registers
) (struct target_ops
*,
762 static struct target_ops
*tmp_to_xfer_partial_ops
;
763 static LONGEST (*tmp_to_xfer_partial
) (struct target_ops
*ops
,
764 enum target_object object
,
767 const gdb_byte
*writebuf
,
770 static int (*tmp_to_insert_breakpoint
) (struct gdbarch
*,
771 struct bp_target_info
*);
772 static int (*tmp_to_remove_breakpoint
) (struct gdbarch
*,
773 struct bp_target_info
*);
775 static void record_restore (void);
777 /* Open the process record target. */
780 record_core_open_1 (char *name
, int from_tty
)
782 struct regcache
*regcache
= get_current_regcache ();
783 int regnum
= gdbarch_num_regs (get_regcache_arch (regcache
));
786 /* Get record_core_regbuf. */
787 target_fetch_registers (regcache
, -1);
788 record_core_regbuf
= xmalloc (MAX_REGISTER_SIZE
* regnum
);
789 for (i
= 0; i
< regnum
; i
++)
790 regcache_raw_collect (regcache
, i
,
791 record_core_regbuf
+ MAX_REGISTER_SIZE
* i
);
793 /* Get record_core_start and record_core_end. */
794 if (build_section_table (core_bfd
, &record_core_start
, &record_core_end
))
796 xfree (record_core_regbuf
);
797 record_core_regbuf
= NULL
;
798 error (_("\"%s\": Can't find sections: %s"),
799 bfd_get_filename (core_bfd
), bfd_errmsg (bfd_get_error ()));
802 push_target (&record_core_ops
);
806 /* "to_open" target method for 'live' processes. */
809 record_open_1 (char *name
, int from_tty
)
811 struct target_ops
*t
;
814 fprintf_unfiltered (gdb_stdlog
, "Process record: record_open\n");
817 if (!target_has_execution
)
818 error (_("Process record: the program is not being run."));
820 error (_("Process record target can't debug inferior in non-stop mode "
822 if (target_async_permitted
)
823 error (_("Process record target can't debug inferior in asynchronous "
824 "mode (target-async)."));
826 if (!gdbarch_process_record_p (target_gdbarch
))
827 error (_("Process record: the current architecture doesn't support "
828 "record function."));
831 error (_("Could not find 'to_resume' method on the target stack."));
833 error (_("Could not find 'to_wait' method on the target stack."));
834 if (!tmp_to_store_registers
)
835 error (_("Could not find 'to_store_registers' method on the target stack."));
836 if (!tmp_to_insert_breakpoint
)
837 error (_("Could not find 'to_insert_breakpoint' method on the target stack."));
838 if (!tmp_to_remove_breakpoint
)
839 error (_("Could not find 'to_remove_breakpoint' method on the target stack."));
841 push_target (&record_ops
);
844 /* "to_open" target method. Open the process record target. */
847 record_open (char *name
, int from_tty
)
849 struct target_ops
*t
;
852 fprintf_unfiltered (gdb_stdlog
, "Process record: record_open\n");
854 /* Check if record target is already running. */
855 if (current_target
.to_stratum
== record_stratum
)
856 error (_("Process record target already running. Use \"record stop\" to "
857 "stop record target first."));
859 /* Reset the tmp beneath pointers. */
860 tmp_to_resume_ops
= NULL
;
861 tmp_to_resume
= NULL
;
862 tmp_to_wait_ops
= NULL
;
864 tmp_to_store_registers_ops
= NULL
;
865 tmp_to_store_registers
= NULL
;
866 tmp_to_xfer_partial_ops
= NULL
;
867 tmp_to_xfer_partial
= NULL
;
868 tmp_to_insert_breakpoint
= NULL
;
869 tmp_to_remove_breakpoint
= NULL
;
871 /* Set the beneath function pointers. */
872 for (t
= current_target
.beneath
; t
!= NULL
; t
= t
->beneath
)
876 tmp_to_resume
= t
->to_resume
;
877 tmp_to_resume_ops
= t
;
881 tmp_to_wait
= t
->to_wait
;
884 if (!tmp_to_store_registers
)
886 tmp_to_store_registers
= t
->to_store_registers
;
887 tmp_to_store_registers_ops
= t
;
889 if (!tmp_to_xfer_partial
)
891 tmp_to_xfer_partial
= t
->to_xfer_partial
;
892 tmp_to_xfer_partial_ops
= t
;
894 if (!tmp_to_insert_breakpoint
)
895 tmp_to_insert_breakpoint
= t
->to_insert_breakpoint
;
896 if (!tmp_to_remove_breakpoint
)
897 tmp_to_remove_breakpoint
= t
->to_remove_breakpoint
;
899 if (!tmp_to_xfer_partial
)
900 error (_("Could not find 'to_xfer_partial' method on the target stack."));
904 record_insn_count
= 0;
905 record_list
= &record_first
;
906 record_list
->next
= NULL
;
908 /* Set the tmp beneath pointers to beneath pointers. */
909 record_beneath_to_resume_ops
= tmp_to_resume_ops
;
910 record_beneath_to_resume
= tmp_to_resume
;
911 record_beneath_to_wait_ops
= tmp_to_wait_ops
;
912 record_beneath_to_wait
= tmp_to_wait
;
913 record_beneath_to_store_registers_ops
= tmp_to_store_registers_ops
;
914 record_beneath_to_store_registers
= tmp_to_store_registers
;
915 record_beneath_to_xfer_partial_ops
= tmp_to_xfer_partial_ops
;
916 record_beneath_to_xfer_partial
= tmp_to_xfer_partial
;
917 record_beneath_to_insert_breakpoint
= tmp_to_insert_breakpoint
;
918 record_beneath_to_remove_breakpoint
= tmp_to_remove_breakpoint
;
920 if (current_target
.to_stratum
== core_stratum
)
921 record_core_open_1 (name
, from_tty
);
923 record_open_1 (name
, from_tty
);
926 /* "to_close" target method. Close the process record target. */
929 record_close (int quitting
)
931 struct record_core_buf_entry
*entry
;
934 fprintf_unfiltered (gdb_stdlog
, "Process record: record_close\n");
936 record_list_release (record_list
);
938 /* Release record_core_regbuf. */
939 if (record_core_regbuf
)
941 xfree (record_core_regbuf
);
942 record_core_regbuf
= NULL
;
945 /* Release record_core_buf_list. */
946 if (record_core_buf_list
)
948 for (entry
= record_core_buf_list
->prev
; entry
; entry
= entry
->prev
)
950 xfree (record_core_buf_list
);
951 record_core_buf_list
= entry
;
953 record_core_buf_list
= NULL
;
957 static int record_resume_step
= 0;
958 static int record_resume_error
;
960 /* "to_resume" target method. Resume the process record target. */
963 record_resume (struct target_ops
*ops
, ptid_t ptid
, int step
,
964 enum target_signal signal
)
966 record_resume_step
= step
;
968 if (!RECORD_IS_REPLAY
)
970 if (do_record_message (get_current_regcache (), signal
))
972 record_resume_error
= 0;
976 record_resume_error
= 1;
979 record_beneath_to_resume (record_beneath_to_resume_ops
, ptid
, 1,
984 static int record_get_sig
= 0;
986 /* SIGINT signal handler, registered by "to_wait" method. */
989 record_sig_handler (int signo
)
992 fprintf_unfiltered (gdb_stdlog
, "Process record: get a signal\n");
994 /* It will break the running inferior in replay mode. */
995 record_resume_step
= 1;
997 /* It will let record_wait set inferior status to get the signal
1003 record_wait_cleanups (void *ignore
)
1005 if (execution_direction
== EXEC_REVERSE
)
1007 if (record_list
->next
)
1008 record_list
= record_list
->next
;
1011 record_list
= record_list
->prev
;
1014 /* "to_wait" target method for process record target.
1016 In record mode, the target is always run in singlestep mode
1017 (even when gdb says to continue). The to_wait method intercepts
1018 the stop events and determines which ones are to be passed on to
1019 gdb. Most stop events are just singlestep events that gdb is not
1020 to know about, so the to_wait method just records them and keeps
1023 In replay mode, this function emulates the recorded execution log,
1024 one instruction at a time (forward or backward), and determines
1028 record_wait (struct target_ops
*ops
,
1029 ptid_t ptid
, struct target_waitstatus
*status
,
1032 struct cleanup
*set_cleanups
= record_gdb_operation_disable_set ();
1035 fprintf_unfiltered (gdb_stdlog
,
1036 "Process record: record_wait "
1037 "record_resume_step = %d\n",
1038 record_resume_step
);
1040 if (!RECORD_IS_REPLAY
&& ops
!= &record_core_ops
)
1042 if (record_resume_error
)
1044 /* If record_resume get error, return directly. */
1045 status
->kind
= TARGET_WAITKIND_STOPPED
;
1046 status
->value
.sig
= TARGET_SIGNAL_ABRT
;
1047 return inferior_ptid
;
1050 if (record_resume_step
)
1052 /* This is a single step. */
1053 return record_beneath_to_wait (record_beneath_to_wait_ops
,
1054 ptid
, status
, options
);
1058 /* This is not a single step. */
1064 ret
= record_beneath_to_wait (record_beneath_to_wait_ops
,
1065 ptid
, status
, options
);
1067 /* Is this a SIGTRAP? */
1068 if (status
->kind
== TARGET_WAITKIND_STOPPED
1069 && status
->value
.sig
== TARGET_SIGNAL_TRAP
)
1071 struct regcache
*regcache
;
1073 /* Yes -- check if there is a breakpoint. */
1074 registers_changed ();
1075 regcache
= get_current_regcache ();
1076 tmp_pc
= regcache_read_pc (regcache
);
1077 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache
),
1080 /* There is a breakpoint. GDB will want to stop. */
1081 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1082 CORE_ADDR decr_pc_after_break
1083 = gdbarch_decr_pc_after_break (gdbarch
);
1084 if (decr_pc_after_break
)
1085 regcache_write_pc (regcache
,
1086 tmp_pc
+ decr_pc_after_break
);
1090 /* There is not a breakpoint, and gdb is not
1091 stepping, therefore gdb will not stop.
1092 Therefore we will not return to gdb.
1093 Record the insn and resume. */
1094 if (!do_record_message (regcache
, TARGET_SIGNAL_0
))
1097 record_beneath_to_resume (record_beneath_to_resume_ops
,
1104 /* The inferior is broken by a breakpoint or a signal. */
1113 struct regcache
*regcache
= get_current_regcache ();
1114 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1115 int continue_flag
= 1;
1116 int first_record_end
= 1;
1117 struct cleanup
*old_cleanups
= make_cleanup (record_wait_cleanups
, 0);
1120 status
->kind
= TARGET_WAITKIND_STOPPED
;
1122 /* Check breakpoint when forward execute. */
1123 if (execution_direction
== EXEC_FORWARD
)
1125 tmp_pc
= regcache_read_pc (regcache
);
1126 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache
),
1130 fprintf_unfiltered (gdb_stdlog
,
1131 "Process record: break at %s.\n",
1132 paddress (gdbarch
, tmp_pc
));
1133 if (gdbarch_decr_pc_after_break (gdbarch
)
1134 && !record_resume_step
)
1135 regcache_write_pc (regcache
,
1137 gdbarch_decr_pc_after_break (gdbarch
));
1143 signal (SIGINT
, record_sig_handler
);
1144 /* If GDB is in terminal_inferior mode, it will not get the signal.
1145 And in GDB replay mode, GDB doesn't need to be in terminal_inferior
1146 mode, because inferior will not executed.
1147 Then set it to terminal_ours to make GDB get the signal. */
1148 target_terminal_ours ();
1150 /* In EXEC_FORWARD mode, record_list points to the tail of prev
1152 if (execution_direction
== EXEC_FORWARD
&& record_list
->next
)
1153 record_list
= record_list
->next
;
1155 /* Loop over the record_list, looking for the next place to
1159 /* Check for beginning and end of log. */
1160 if (execution_direction
== EXEC_REVERSE
1161 && record_list
== &record_first
)
1163 /* Hit beginning of record log in reverse. */
1164 status
->kind
= TARGET_WAITKIND_NO_HISTORY
;
1167 if (execution_direction
!= EXEC_REVERSE
&& !record_list
->next
)
1169 /* Hit end of record log going forward. */
1170 status
->kind
= TARGET_WAITKIND_NO_HISTORY
;
1174 record_exec_insn (regcache
, gdbarch
, record_list
);
1176 if (record_list
->type
== record_end
)
1178 if (record_debug
> 1)
1179 fprintf_unfiltered (gdb_stdlog
,
1180 "Process record: record_end %s to "
1182 host_address_to_string (record_list
));
1184 if (first_record_end
&& execution_direction
== EXEC_REVERSE
)
1186 /* When reverse excute, the first record_end is the part of
1187 current instruction. */
1188 first_record_end
= 0;
1192 /* In EXEC_REVERSE mode, this is the record_end of prev
1194 In EXEC_FORWARD mode, this is the record_end of current
1197 if (record_resume_step
)
1199 if (record_debug
> 1)
1200 fprintf_unfiltered (gdb_stdlog
,
1201 "Process record: step.\n");
1205 /* check breakpoint */
1206 tmp_pc
= regcache_read_pc (regcache
);
1207 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache
),
1211 fprintf_unfiltered (gdb_stdlog
,
1212 "Process record: break "
1214 paddress (gdbarch
, tmp_pc
));
1215 if (gdbarch_decr_pc_after_break (gdbarch
)
1216 && execution_direction
== EXEC_FORWARD
1217 && !record_resume_step
)
1218 regcache_write_pc (regcache
,
1220 gdbarch_decr_pc_after_break (gdbarch
));
1223 /* Check target signal */
1224 if (record_list
->u
.end
.sigval
!= TARGET_SIGNAL_0
)
1225 /* FIXME: better way to check */
1232 if (execution_direction
== EXEC_REVERSE
)
1234 if (record_list
->prev
)
1235 record_list
= record_list
->prev
;
1239 if (record_list
->next
)
1240 record_list
= record_list
->next
;
1244 while (continue_flag
);
1246 signal (SIGINT
, handle_sigint
);
1250 status
->value
.sig
= TARGET_SIGNAL_INT
;
1251 else if (record_list
->u
.end
.sigval
!= TARGET_SIGNAL_0
)
1252 /* FIXME: better way to check */
1253 status
->value
.sig
= record_list
->u
.end
.sigval
;
1255 status
->value
.sig
= TARGET_SIGNAL_TRAP
;
1257 discard_cleanups (old_cleanups
);
1260 do_cleanups (set_cleanups
);
1261 return inferior_ptid
;
1264 /* "to_disconnect" method for process record target. */
1267 record_disconnect (struct target_ops
*target
, char *args
, int from_tty
)
1270 fprintf_unfiltered (gdb_stdlog
, "Process record: record_disconnect\n");
1272 unpush_target (&record_ops
);
1273 target_disconnect (args
, from_tty
);
1276 /* "to_detach" method for process record target. */
1279 record_detach (struct target_ops
*ops
, char *args
, int from_tty
)
1282 fprintf_unfiltered (gdb_stdlog
, "Process record: record_detach\n");
1284 unpush_target (&record_ops
);
1285 target_detach (args
, from_tty
);
1288 /* "to_mourn_inferior" method for process record target. */
1291 record_mourn_inferior (struct target_ops
*ops
)
1294 fprintf_unfiltered (gdb_stdlog
, "Process record: "
1295 "record_mourn_inferior\n");
1297 unpush_target (&record_ops
);
1298 target_mourn_inferior ();
1301 /* Close process record target before killing the inferior process. */
1304 record_kill (struct target_ops
*ops
)
1307 fprintf_unfiltered (gdb_stdlog
, "Process record: record_kill\n");
1309 unpush_target (&record_ops
);
1313 /* Record registers change (by user or by GDB) to list as an instruction. */
1316 record_registers_change (struct regcache
*regcache
, int regnum
)
1318 /* Check record_insn_num. */
1319 record_check_insn_num (0);
1321 record_arch_list_head
= NULL
;
1322 record_arch_list_tail
= NULL
;
1327 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
1329 if (record_arch_list_add_reg (regcache
, i
))
1331 record_list_release (record_arch_list_tail
);
1332 error (_("Process record: failed to record execution log."));
1338 if (record_arch_list_add_reg (regcache
, regnum
))
1340 record_list_release (record_arch_list_tail
);
1341 error (_("Process record: failed to record execution log."));
1344 if (record_arch_list_add_end ())
1346 record_list_release (record_arch_list_tail
);
1347 error (_("Process record: failed to record execution log."));
1349 record_list
->next
= record_arch_list_head
;
1350 record_arch_list_head
->prev
= record_list
;
1351 record_list
= record_arch_list_tail
;
1353 if (record_insn_num
== record_insn_max_num
&& record_insn_max_num
)
1354 record_list_release_first ();
1359 /* "to_store_registers" method for process record target. */
1362 record_store_registers (struct target_ops
*ops
, struct regcache
*regcache
,
1365 if (!record_gdb_operation_disable
)
1367 if (RECORD_IS_REPLAY
)
1371 /* Let user choose if he wants to write register or not. */
1374 query (_("Because GDB is in replay mode, changing the "
1375 "value of a register will make the execution "
1376 "log unusable from this point onward. "
1377 "Change all registers?"));
1380 query (_("Because GDB is in replay mode, changing the value "
1381 "of a register will make the execution log unusable "
1382 "from this point onward. Change register %s?"),
1383 gdbarch_register_name (get_regcache_arch (regcache
),
1388 /* Invalidate the value of regcache that was set in function
1389 "regcache_raw_write". */
1394 i
< gdbarch_num_regs (get_regcache_arch (regcache
));
1396 regcache_invalidate (regcache
, i
);
1399 regcache_invalidate (regcache
, regno
);
1401 error (_("Process record canceled the operation."));
1404 /* Destroy the record from here forward. */
1405 record_list_release_following (record_list
);
1408 record_registers_change (regcache
, regno
);
1410 record_beneath_to_store_registers (record_beneath_to_store_registers_ops
,
1414 /* "to_xfer_partial" method. Behavior is conditional on RECORD_IS_REPLAY.
1415 In replay mode, we cannot write memory unles we are willing to
1416 invalidate the record/replay log from this point forward. */
1419 record_xfer_partial (struct target_ops
*ops
, enum target_object object
,
1420 const char *annex
, gdb_byte
*readbuf
,
1421 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
1423 if (!record_gdb_operation_disable
1424 && (object
== TARGET_OBJECT_MEMORY
1425 || object
== TARGET_OBJECT_RAW_MEMORY
) && writebuf
)
1427 if (RECORD_IS_REPLAY
)
1429 /* Let user choose if he wants to write memory or not. */
1430 if (!query (_("Because GDB is in replay mode, writing to memory "
1431 "will make the execution log unusable from this "
1432 "point onward. Write memory at address %s?"),
1433 paddress (target_gdbarch
, offset
)))
1434 error (_("Process record canceled the operation."));
1436 /* Destroy the record from here forward. */
1437 record_list_release_following (record_list
);
1440 /* Check record_insn_num */
1441 record_check_insn_num (0);
1443 /* Record registers change to list as an instruction. */
1444 record_arch_list_head
= NULL
;
1445 record_arch_list_tail
= NULL
;
1446 if (record_arch_list_add_mem (offset
, len
))
1448 record_list_release (record_arch_list_tail
);
1450 fprintf_unfiltered (gdb_stdlog
,
1451 "Process record: failed to record "
1455 if (record_arch_list_add_end ())
1457 record_list_release (record_arch_list_tail
);
1459 fprintf_unfiltered (gdb_stdlog
,
1460 "Process record: failed to record "
1464 record_list
->next
= record_arch_list_head
;
1465 record_arch_list_head
->prev
= record_list
;
1466 record_list
= record_arch_list_tail
;
1468 if (record_insn_num
== record_insn_max_num
&& record_insn_max_num
)
1469 record_list_release_first ();
1474 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops
,
1475 object
, annex
, readbuf
, writebuf
,
1479 /* Behavior is conditional on RECORD_IS_REPLAY.
1480 We will not actually insert or remove breakpoints when replaying,
1481 nor when recording. */
1484 record_insert_breakpoint (struct gdbarch
*gdbarch
,
1485 struct bp_target_info
*bp_tgt
)
1487 if (!RECORD_IS_REPLAY
)
1489 struct cleanup
*old_cleanups
= record_gdb_operation_disable_set ();
1490 int ret
= record_beneath_to_insert_breakpoint (gdbarch
, bp_tgt
);
1492 do_cleanups (old_cleanups
);
1500 /* "to_remove_breakpoint" method for process record target. */
1503 record_remove_breakpoint (struct gdbarch
*gdbarch
,
1504 struct bp_target_info
*bp_tgt
)
1506 if (!RECORD_IS_REPLAY
)
1508 struct cleanup
*old_cleanups
= record_gdb_operation_disable_set ();
1509 int ret
= record_beneath_to_remove_breakpoint (gdbarch
, bp_tgt
);
1511 do_cleanups (old_cleanups
);
1519 /* "to_can_execute_reverse" method for process record target. */
1522 record_can_execute_reverse (void)
1528 init_record_ops (void)
1530 record_ops
.to_shortname
= "record";
1531 record_ops
.to_longname
= "Process record and replay target";
1533 "Log program while executing and replay execution from log.";
1534 record_ops
.to_open
= record_open
;
1535 record_ops
.to_close
= record_close
;
1536 record_ops
.to_resume
= record_resume
;
1537 record_ops
.to_wait
= record_wait
;
1538 record_ops
.to_disconnect
= record_disconnect
;
1539 record_ops
.to_detach
= record_detach
;
1540 record_ops
.to_mourn_inferior
= record_mourn_inferior
;
1541 record_ops
.to_kill
= record_kill
;
1542 record_ops
.to_create_inferior
= find_default_create_inferior
;
1543 record_ops
.to_store_registers
= record_store_registers
;
1544 record_ops
.to_xfer_partial
= record_xfer_partial
;
1545 record_ops
.to_insert_breakpoint
= record_insert_breakpoint
;
1546 record_ops
.to_remove_breakpoint
= record_remove_breakpoint
;
1547 record_ops
.to_can_execute_reverse
= record_can_execute_reverse
;
1548 record_ops
.to_stratum
= record_stratum
;
1549 record_ops
.to_magic
= OPS_MAGIC
;
1552 /* "to_resume" method for prec over corefile. */
1555 record_core_resume (struct target_ops
*ops
, ptid_t ptid
, int step
,
1556 enum target_signal signal
)
1558 record_resume_step
= step
;
1561 /* "to_kill" method for prec over corefile. */
1564 record_core_kill (struct target_ops
*ops
)
1567 fprintf_unfiltered (gdb_stdlog
, "Process record: record_core_kill\n");
1569 unpush_target (&record_core_ops
);
1572 /* "to_fetch_registers" method for prec over corefile. */
1575 record_core_fetch_registers (struct target_ops
*ops
,
1576 struct regcache
*regcache
,
1581 int num
= gdbarch_num_regs (get_regcache_arch (regcache
));
1584 for (i
= 0; i
< num
; i
++)
1585 regcache_raw_supply (regcache
, i
,
1586 record_core_regbuf
+ MAX_REGISTER_SIZE
* i
);
1589 regcache_raw_supply (regcache
, regno
,
1590 record_core_regbuf
+ MAX_REGISTER_SIZE
* regno
);
1593 /* "to_prepare_to_store" method for prec over corefile. */
1596 record_core_prepare_to_store (struct regcache
*regcache
)
1600 /* "to_store_registers" method for prec over corefile. */
1603 record_core_store_registers (struct target_ops
*ops
,
1604 struct regcache
*regcache
,
1607 if (record_gdb_operation_disable
)
1608 regcache_raw_collect (regcache
, regno
,
1609 record_core_regbuf
+ MAX_REGISTER_SIZE
* regno
);
1611 error (_("You can't do that without a process to debug."));
1614 /* "to_xfer_partial" method for prec over corefile. */
1617 record_core_xfer_partial (struct target_ops
*ops
, enum target_object object
,
1618 const char *annex
, gdb_byte
*readbuf
,
1619 const gdb_byte
*writebuf
, ULONGEST offset
,
1622 if (object
== TARGET_OBJECT_MEMORY
)
1624 if (record_gdb_operation_disable
|| !writebuf
)
1626 struct target_section
*p
;
1627 for (p
= record_core_start
; p
< record_core_end
; p
++)
1629 if (offset
>= p
->addr
)
1631 struct record_core_buf_entry
*entry
;
1632 ULONGEST sec_offset
;
1634 if (offset
>= p
->endaddr
)
1637 if (offset
+ len
> p
->endaddr
)
1638 len
= p
->endaddr
- offset
;
1640 sec_offset
= offset
- p
->addr
;
1642 /* Read readbuf or write writebuf p, offset, len. */
1644 if (p
->the_bfd_section
->flags
& SEC_CONSTRUCTOR
1645 || (p
->the_bfd_section
->flags
& SEC_HAS_CONTENTS
) == 0)
1648 memset (readbuf
, 0, len
);
1651 /* Get record_core_buf_entry. */
1652 for (entry
= record_core_buf_list
; entry
;
1653 entry
= entry
->prev
)
1660 /* Add a new entry. */
1662 = (struct record_core_buf_entry
*)
1664 (sizeof (struct record_core_buf_entry
));
1666 if (!bfd_malloc_and_get_section (p
->bfd
,
1673 entry
->prev
= record_core_buf_list
;
1674 record_core_buf_list
= entry
;
1677 memcpy (entry
->buf
+ sec_offset
, writebuf
,
1683 return record_beneath_to_xfer_partial
1684 (record_beneath_to_xfer_partial_ops
,
1685 object
, annex
, readbuf
, writebuf
,
1688 memcpy (readbuf
, entry
->buf
+ sec_offset
,
1699 error (_("You can't do that without a process to debug."));
1702 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops
,
1703 object
, annex
, readbuf
, writebuf
,
1707 /* "to_insert_breakpoint" method for prec over corefile. */
1710 record_core_insert_breakpoint (struct gdbarch
*gdbarch
,
1711 struct bp_target_info
*bp_tgt
)
1716 /* "to_remove_breakpoint" method for prec over corefile. */
1719 record_core_remove_breakpoint (struct gdbarch
*gdbarch
,
1720 struct bp_target_info
*bp_tgt
)
1725 /* "to_has_execution" method for prec over corefile. */
1728 record_core_has_execution (struct target_ops
*ops
)
1734 init_record_core_ops (void)
1736 record_core_ops
.to_shortname
= "record_core";
1737 record_core_ops
.to_longname
= "Process record and replay target";
1738 record_core_ops
.to_doc
=
1739 "Log program while executing and replay execution from log.";
1740 record_core_ops
.to_open
= record_open
;
1741 record_core_ops
.to_close
= record_close
;
1742 record_core_ops
.to_resume
= record_core_resume
;
1743 record_core_ops
.to_wait
= record_wait
;
1744 record_core_ops
.to_kill
= record_core_kill
;
1745 record_core_ops
.to_fetch_registers
= record_core_fetch_registers
;
1746 record_core_ops
.to_prepare_to_store
= record_core_prepare_to_store
;
1747 record_core_ops
.to_store_registers
= record_core_store_registers
;
1748 record_core_ops
.to_xfer_partial
= record_core_xfer_partial
;
1749 record_core_ops
.to_insert_breakpoint
= record_core_insert_breakpoint
;
1750 record_core_ops
.to_remove_breakpoint
= record_core_remove_breakpoint
;
1751 record_core_ops
.to_can_execute_reverse
= record_can_execute_reverse
;
1752 record_core_ops
.to_has_execution
= record_core_has_execution
;
1753 record_core_ops
.to_stratum
= record_stratum
;
1754 record_core_ops
.to_magic
= OPS_MAGIC
;
1757 /* Implement "show record debug" command. */
1760 show_record_debug (struct ui_file
*file
, int from_tty
,
1761 struct cmd_list_element
*c
, const char *value
)
1763 fprintf_filtered (file
, _("Debugging of process record target is %s.\n"),
1767 /* Alias for "target record". */
1770 cmd_record_start (char *args
, int from_tty
)
1772 execute_command ("target record", from_tty
);
1775 /* Truncate the record log from the present point
1776 of replay until the end. */
1779 cmd_record_delete (char *args
, int from_tty
)
1781 if (current_target
.to_stratum
== record_stratum
)
1783 if (RECORD_IS_REPLAY
)
1785 if (!from_tty
|| query (_("Delete the log from this point forward "
1786 "and begin to record the running message "
1788 record_list_release_following (record_list
);
1791 printf_unfiltered (_("Already at end of record list.\n"));
1795 printf_unfiltered (_("Process record is not started.\n"));
1798 /* Implement the "stoprecord" or "record stop" command. */
1801 cmd_record_stop (char *args
, int from_tty
)
1803 if (current_target
.to_stratum
== record_stratum
)
1805 unpush_target (&record_ops
);
1806 printf_unfiltered (_("Process record is stopped and all execution "
1807 "logs are deleted.\n"));
1810 printf_unfiltered (_("Process record is not started.\n"));
1813 /* Set upper limit of record log size. */
1816 set_record_insn_max_num (char *args
, int from_tty
, struct cmd_list_element
*c
)
1818 if (record_insn_num
> record_insn_max_num
&& record_insn_max_num
)
1820 /* Count down record_insn_num while releasing records from list. */
1821 while (record_insn_num
> record_insn_max_num
)
1823 record_list_release_first ();
1829 static struct cmd_list_element
*record_cmdlist
, *set_record_cmdlist
,
1830 *show_record_cmdlist
, *info_record_cmdlist
;
1833 set_record_command (char *args
, int from_tty
)
1835 printf_unfiltered (_("\
1836 \"set record\" must be followed by an apporpriate subcommand.\n"));
1837 help_list (set_record_cmdlist
, "set record ", all_commands
, gdb_stdout
);
1841 show_record_command (char *args
, int from_tty
)
1843 cmd_show_list (show_record_cmdlist
, from_tty
, "");
1846 /* Display some statistics about the execution log. */
1849 info_record_command (char *args
, int from_tty
)
1851 struct record_entry
*p
;
1853 if (current_target
.to_stratum
== record_stratum
)
1855 if (RECORD_IS_REPLAY
)
1856 printf_filtered (_("Replay mode:\n"));
1858 printf_filtered (_("Record mode:\n"));
1860 /* Find entry for first actual instruction in the log. */
1861 for (p
= record_first
.next
;
1862 p
!= NULL
&& p
->type
!= record_end
;
1866 /* Do we have a log at all? */
1867 if (p
!= NULL
&& p
->type
== record_end
)
1869 /* Display instruction number for first instruction in the log. */
1870 printf_filtered (_("Lowest recorded instruction number is %s.\n"),
1871 pulongest (p
->u
.end
.insn_num
));
1873 /* If in replay mode, display where we are in the log. */
1874 if (RECORD_IS_REPLAY
)
1875 printf_filtered (_("Current instruction number is %s.\n"),
1876 pulongest (record_list
->u
.end
.insn_num
));
1878 /* Display instruction number for last instruction in the log. */
1879 printf_filtered (_("Highest recorded instruction number is %s.\n"),
1880 pulongest (record_insn_count
));
1882 /* Display log count. */
1883 printf_filtered (_("Log contains %d instructions.\n"),
1888 printf_filtered (_("No instructions have been logged.\n"));
1893 printf_filtered (_("target record is not active.\n"));
1896 /* Display max log size. */
1897 printf_filtered (_("Max logged instructions is %d.\n"),
1898 record_insn_max_num
);
1901 /* Record log save-file format
1902 Version 1 (never released)
1905 4 bytes: magic number htonl(0x20090829).
1906 NOTE: be sure to change whenever this file format changes!
1910 1 byte: record type (record_end, see enum record_type).
1912 1 byte: record type (record_reg, see enum record_type).
1913 8 bytes: register id (network byte order).
1914 MAX_REGISTER_SIZE bytes: register value.
1916 1 byte: record type (record_mem, see enum record_type).
1917 8 bytes: memory length (network byte order).
1918 8 bytes: memory address (network byte order).
1919 n bytes: memory value (n == memory length).
1922 4 bytes: magic number netorder32(0x20091016).
1923 NOTE: be sure to change whenever this file format changes!
1927 1 byte: record type (record_end, see enum record_type).
1929 4 bytes: instruction count
1931 1 byte: record type (record_reg, see enum record_type).
1932 4 bytes: register id (network byte order).
1933 n bytes: register value (n == actual register size).
1934 (eg. 4 bytes for x86 general registers).
1936 1 byte: record type (record_mem, see enum record_type).
1937 4 bytes: memory length (network byte order).
1938 8 bytes: memory address (network byte order).
1939 n bytes: memory value (n == memory length).
1943 /* bfdcore_read -- read bytes from a core file section. */
1946 bfdcore_read (bfd
*obfd
, asection
*osec
, void *buf
, int len
, int *offset
)
1948 int ret
= bfd_get_section_contents (obfd
, osec
, buf
, *offset
, len
);
1953 error (_("Failed to read %d bytes from core file %s ('%s').\n"),
1954 len
, bfd_get_filename (obfd
),
1955 bfd_errmsg (bfd_get_error ()));
1958 static inline uint64_t
1959 netorder64 (uint64_t fromfile
)
1961 return (BYTE_ORDER
== BFD_ENDIAN_LITTLE
)
1962 ? bswap_64 (fromfile
)
1966 static inline uint32_t
1967 netorder32 (uint32_t fromfile
)
1969 return (BYTE_ORDER
== BFD_ENDIAN_LITTLE
)
1970 ? bswap_32 (fromfile
)
1974 static inline uint16_t
1975 netorder16 (uint16_t fromfile
)
1977 return (BYTE_ORDER
== BFD_ENDIAN_LITTLE
)
1978 ? bswap_16 (fromfile
)
1982 /* Restore the execution log from a core_bfd file. */
1984 record_restore (void)
1987 struct cleanup
*old_cleanups
;
1988 struct record_entry
*rec
;
1992 struct regcache
*regcache
;
1994 /* We restore the execution log from the open core bfd,
1996 if (core_bfd
== NULL
)
1999 /* "record_restore" can only be called when record list is empty. */
2000 gdb_assert (record_first
.next
== NULL
);
2003 printf_filtered ("Restoring recording from core file.\n");
2005 /* Now need to find our special note section. */
2006 osec
= bfd_get_section_by_name (core_bfd
, "null0");
2007 osec_size
= bfd_section_size (core_bfd
, osec
);
2009 printf_filtered ("Find precord section %s.\n",
2010 osec
? "succeeded" : "failed");
2014 printf_filtered ("%s", bfd_section_name (core_bfd
, osec
));
2016 /* Check the magic code. */
2017 bfdcore_read (core_bfd
, osec
, &magic
, sizeof (magic
), &bfd_offset
);
2018 if (magic
!= RECORD_FILE_MAGIC
)
2019 error (_("Version mis-match or file format error in core file %s."),
2020 bfd_get_filename (core_bfd
));
2023 Reading 4-byte magic cookie RECORD_FILE_MAGIC (0x%s)\n",
2024 phex_nz (netorder32 (magic
), 4));
2026 /* Restore the entries in recfd into record_arch_list_head and
2027 record_arch_list_tail. */
2028 record_arch_list_head
= NULL
;
2029 record_arch_list_tail
= NULL
;
2030 record_insn_num
= 0;
2031 old_cleanups
= make_cleanup (record_arch_list_cleanups
, 0);
2032 regcache
= get_current_regcache ();
2038 uint32_t regnum
, len
, signal
, count
;
2041 /* We are finished when offset reaches osec_size. */
2042 if (bfd_offset
>= osec_size
)
2044 bfdcore_read (core_bfd
, osec
, &tmpu8
, sizeof (tmpu8
), &bfd_offset
);
2048 case record_reg
: /* reg */
2049 /* Get register number to regnum. */
2050 bfdcore_read (core_bfd
, osec
, ®num
,
2051 sizeof (regnum
), &bfd_offset
);
2052 regnum
= netorder32 (regnum
);
2054 rec
= record_reg_alloc (regcache
, regnum
);
2057 bfdcore_read (core_bfd
, osec
, record_get_loc (rec
),
2058 rec
->u
.reg
.len
, &bfd_offset
);
2062 Reading register %d (1 plus %lu plus %d bytes)\n",
2064 (unsigned long) sizeof (regnum
),
2068 case record_mem
: /* mem */
2070 bfdcore_read (core_bfd
, osec
, &len
,
2071 sizeof (len
), &bfd_offset
);
2072 len
= netorder32 (len
);
2075 bfdcore_read (core_bfd
, osec
, &addr
,
2076 sizeof (addr
), &bfd_offset
);
2077 addr
= netorder64 (addr
);
2079 rec
= record_mem_alloc (addr
, len
);
2082 bfdcore_read (core_bfd
, osec
, record_get_loc (rec
),
2083 rec
->u
.mem
.len
, &bfd_offset
);
2087 Reading memory %s (1 plus %lu plus %lu plus %d bytes)\n",
2088 paddress (get_current_arch (),
2090 (unsigned long) sizeof (addr
),
2091 (unsigned long) sizeof (len
),
2095 case record_end
: /* end */
2096 rec
= record_end_alloc ();
2099 /* Get signal value. */
2100 bfdcore_read (core_bfd
, osec
, &signal
,
2101 sizeof (signal
), &bfd_offset
);
2102 signal
= netorder32 (signal
);
2103 rec
->u
.end
.sigval
= signal
;
2105 /* Get insn count. */
2106 bfdcore_read (core_bfd
, osec
, &count
,
2107 sizeof (count
), &bfd_offset
);
2108 count
= netorder32 (count
);
2109 rec
->u
.end
.insn_num
= count
;
2110 record_insn_count
= count
+ 1;
2113 Reading record_end (1 + %lu + %lu bytes), offset == %s\n",
2114 (unsigned long) sizeof (signal
),
2115 (unsigned long) sizeof (count
),
2116 paddress (get_current_arch (),
2121 error (_("Bad entry type in core file %s."),
2122 bfd_get_filename (core_bfd
));
2126 /* Add rec to record arch list. */
2127 record_arch_list_add (rec
);
2130 discard_cleanups (old_cleanups
);
2132 /* Add record_arch_list_head to the end of record list. */
2133 record_first
.next
= record_arch_list_head
;
2134 record_arch_list_head
->prev
= &record_first
;
2135 record_arch_list_tail
->next
= NULL
;
2136 record_list
= &record_first
;
2138 /* Update record_insn_max_num. */
2139 if (record_insn_num
> record_insn_max_num
)
2141 record_insn_max_num
= record_insn_num
;
2142 warning (_("Auto increase record/replay buffer limit to %d."),
2143 record_insn_max_num
);
2147 printf_filtered (_("Restored records from core file %s.\n"),
2148 bfd_get_filename (core_bfd
));
2150 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
2153 /* bfdcore_write -- write bytes into a core file section. */
2156 bfdcore_write (bfd
*obfd
, asection
*osec
, void *buf
, int len
, int *offset
)
2158 int ret
= bfd_set_section_contents (obfd
, osec
, buf
, *offset
, len
);
2163 error (_("Failed to write %d bytes to core file %s ('%s').\n"),
2164 len
, bfd_get_filename (obfd
),
2165 bfd_errmsg (bfd_get_error ()));
2168 /* Restore the execution log from a file. We use a modified elf
2169 corefile format, with an extra section for our data. */
2172 cmd_record_restore (char *args
, int from_tty
)
2174 core_file_command (args
, from_tty
);
2175 record_open (args
, from_tty
);
2179 record_save_cleanups (void *data
)
2182 char *pathname
= xstrdup (bfd_get_filename (obfd
));
2188 /* Save the execution log to a file. We use a modified elf corefile
2189 format, with an extra section for our data. */
2192 cmd_record_save (char *args
, int from_tty
)
2194 char *recfilename
, recfilename_buffer
[40];
2196 struct record_entry
*cur_record_list
;
2198 struct regcache
*regcache
;
2199 struct gdbarch
*gdbarch
;
2200 struct cleanup
*old_cleanups
;
2201 struct cleanup
*set_cleanups
;
2204 asection
*osec
= NULL
;
2207 if (strcmp (current_target
.to_shortname
, "record") != 0)
2208 error (_("This command can only be used with target 'record'.\n"
2209 "Use 'target record' first.\n"));
2215 /* Default recfile name is "gdb_record.PID". */
2216 snprintf (recfilename_buffer
, sizeof (recfilename_buffer
),
2217 "gdb_record.%d", PIDGET (inferior_ptid
));
2218 recfilename
= recfilename_buffer
;
2221 /* Open the save file. */
2223 printf_filtered ("Saving execution log to core file '%s'\n", recfilename
);
2225 /* Open the output file. */
2226 obfd
= create_gcore_bfd (recfilename
);
2227 old_cleanups
= make_cleanup (record_save_cleanups
, obfd
);
2229 /* Save the current record entry to "cur_record_list". */
2230 cur_record_list
= record_list
;
2232 /* Get the values of regcache and gdbarch. */
2233 regcache
= get_current_regcache ();
2234 gdbarch
= get_regcache_arch (regcache
);
2236 /* Disable the GDB operation record. */
2237 set_cleanups
= record_gdb_operation_disable_set ();
2239 /* Reverse execute to the begin of record list. */
2242 /* Check for beginning and end of log. */
2243 if (record_list
== &record_first
)
2246 record_exec_insn (regcache
, gdbarch
, record_list
);
2248 if (record_list
->prev
)
2249 record_list
= record_list
->prev
;
2252 /* Compute the size needed for the extra bfd section. */
2253 save_size
= 4; /* magic cookie */
2254 for (record_list
= record_first
.next
; record_list
;
2255 record_list
= record_list
->next
)
2256 switch (record_list
->type
)
2259 save_size
+= 1 + 4 + 4;
2262 save_size
+= 1 + 4 + record_list
->u
.reg
.len
;
2265 save_size
+= 1 + 4 + 8 + record_list
->u
.mem
.len
;
2269 /* Make the new bfd section. */
2270 osec
= bfd_make_section_anyway_with_flags (obfd
, "precord",
2274 error (_("Failed to create 'precord' section for corefile %s: %s"),
2276 bfd_errmsg (bfd_get_error ()));
2277 bfd_set_section_size (obfd
, osec
, save_size
);
2278 bfd_set_section_vma (obfd
, osec
, 0);
2279 bfd_set_section_alignment (obfd
, osec
, 0);
2280 bfd_section_lma (obfd
, osec
) = 0;
2282 /* Save corefile state. */
2283 write_gcore_file (obfd
);
2285 /* Write out the record log. */
2286 /* Write the magic code. */
2287 magic
= RECORD_FILE_MAGIC
;
2290 Writing 4-byte magic cookie RECORD_FILE_MAGIC (0x%s)\n",
2291 phex_nz (magic
, 4));
2292 bfdcore_write (obfd
, osec
, &magic
, sizeof (magic
), &bfd_offset
);
2294 /* Save the entries to recfd and forward execute to the end of
2296 record_list
= &record_first
;
2300 if (record_list
!= &record_first
)
2303 uint32_t regnum
, len
, signal
, count
;
2306 type
= record_list
->type
;
2307 bfdcore_write (obfd
, osec
, &type
, sizeof (type
), &bfd_offset
);
2309 switch (record_list
->type
)
2311 case record_reg
: /* reg */
2314 Writing register %d (1 plus %lu plus %d bytes)\n",
2315 record_list
->u
.reg
.num
,
2316 (unsigned long) sizeof (regnum
),
2317 record_list
->u
.reg
.len
);
2320 regnum
= netorder32 (record_list
->u
.reg
.num
);
2321 bfdcore_write (obfd
, osec
, ®num
,
2322 sizeof (regnum
), &bfd_offset
);
2325 bfdcore_write (obfd
, osec
, record_get_loc (record_list
),
2326 record_list
->u
.reg
.len
, &bfd_offset
);
2329 case record_mem
: /* mem */
2332 Writing memory %s (1 plus %lu plus %lu plus %d bytes)\n",
2334 record_list
->u
.mem
.addr
),
2335 (unsigned long) sizeof (addr
),
2336 (unsigned long) sizeof (len
),
2337 record_list
->u
.mem
.len
);
2340 len
= netorder32 (record_list
->u
.mem
.len
);
2341 bfdcore_write (obfd
, osec
, &len
, sizeof (len
), &bfd_offset
);
2343 /* Write memaddr. */
2344 addr
= netorder64 (record_list
->u
.mem
.addr
);
2345 bfdcore_write (obfd
, osec
, &addr
,
2346 sizeof (addr
), &bfd_offset
);
2349 bfdcore_write (obfd
, osec
, record_get_loc (record_list
),
2350 record_list
->u
.mem
.len
, &bfd_offset
);
2356 Writing record_end (1 + %lu + %lu bytes)\n",
2357 (unsigned long) sizeof (signal
),
2358 (unsigned long) sizeof (count
));
2359 /* Write signal value. */
2360 signal
= netorder32 (record_list
->u
.end
.sigval
);
2361 bfdcore_write (obfd
, osec
, &signal
,
2362 sizeof (signal
), &bfd_offset
);
2364 /* Write insn count. */
2365 count
= netorder32 (record_list
->u
.end
.insn_num
);
2366 bfdcore_write (obfd
, osec
, &count
,
2367 sizeof (count
), &bfd_offset
);
2372 /* Execute entry. */
2373 record_exec_insn (regcache
, gdbarch
, record_list
);
2375 if (record_list
->next
)
2376 record_list
= record_list
->next
;
2381 /* Reverse execute to cur_record_list. */
2384 /* Check for beginning and end of log. */
2385 if (record_list
== cur_record_list
)
2388 record_exec_insn (regcache
, gdbarch
, record_list
);
2390 if (record_list
->prev
)
2391 record_list
= record_list
->prev
;
2394 do_cleanups (set_cleanups
);
2396 discard_cleanups (old_cleanups
);
2399 printf_filtered (_("Saved core file %s with execution log.\n"),
2404 _initialize_record (void)
2406 struct cmd_list_element
*c
;
2408 /* Init record_first. */
2409 record_first
.prev
= NULL
;
2410 record_first
.next
= NULL
;
2411 record_first
.type
= record_end
;
2414 add_target (&record_ops
);
2415 init_record_core_ops ();
2416 add_target (&record_core_ops
);
2418 add_setshow_zinteger_cmd ("record", no_class
, &record_debug
,
2419 _("Set debugging of record/replay feature."),
2420 _("Show debugging of record/replay feature."),
2421 _("When enabled, debugging output for "
2422 "record/replay feature is displayed."),
2423 NULL
, show_record_debug
, &setdebuglist
,
2426 c
= add_prefix_cmd ("record", class_obscure
, cmd_record_start
,
2427 _("Abbreviated form of \"target record\" command."),
2428 &record_cmdlist
, "record ", 0, &cmdlist
);
2429 set_cmd_completer (c
, filename_completer
);
2431 add_com_alias ("rec", "record", class_obscure
, 1);
2432 add_prefix_cmd ("record", class_support
, set_record_command
,
2433 _("Set record options"), &set_record_cmdlist
,
2434 "set record ", 0, &setlist
);
2435 add_alias_cmd ("rec", "record", class_obscure
, 1, &setlist
);
2436 add_prefix_cmd ("record", class_support
, show_record_command
,
2437 _("Show record options"), &show_record_cmdlist
,
2438 "show record ", 0, &showlist
);
2439 add_alias_cmd ("rec", "record", class_obscure
, 1, &showlist
);
2440 add_prefix_cmd ("record", class_support
, info_record_command
,
2441 _("Info record options"), &info_record_cmdlist
,
2442 "info record ", 0, &infolist
);
2443 add_alias_cmd ("rec", "record", class_obscure
, 1, &infolist
);
2445 c
= add_cmd ("save", class_obscure
, cmd_record_save
,
2446 _("Save the execution log to a file.\n\
2447 Argument is optional filename.\n\
2448 Default filename is 'gdb_record.<process_id>'."),
2450 set_cmd_completer (c
, filename_completer
);
2452 c
= add_cmd ("restore", class_obscure
, cmd_record_restore
,
2453 _("Restore the execution log from a file.\n\
2454 Argument is filename. File must be created with 'record save'."),
2456 set_cmd_completer (c
, filename_completer
);
2458 add_cmd ("delete", class_obscure
, cmd_record_delete
,
2459 _("Delete the rest of execution log and start recording it anew."),
2461 add_alias_cmd ("d", "delete", class_obscure
, 1, &record_cmdlist
);
2462 add_alias_cmd ("del", "delete", class_obscure
, 1, &record_cmdlist
);
2464 add_cmd ("stop", class_obscure
, cmd_record_stop
,
2465 _("Stop the record/replay target."),
2467 add_alias_cmd ("s", "stop", class_obscure
, 1, &record_cmdlist
);
2469 /* Record instructions number limit command. */
2470 add_setshow_boolean_cmd ("stop-at-limit", no_class
,
2471 &record_stop_at_limit
, _("\
2472 Set whether record/replay stops when record/replay buffer becomes full."), _("\
2473 Show whether record/replay stops when record/replay buffer becomes full."), _("\
2475 When ON, if the record/replay buffer becomes full, ask user what to do.\n\
2476 When OFF, if the record/replay buffer becomes full,\n\
2477 delete the oldest recorded instruction to make room for each new one."),
2479 &set_record_cmdlist
, &show_record_cmdlist
);
2480 add_setshow_uinteger_cmd ("insn-number-max", no_class
,
2481 &record_insn_max_num
,
2482 _("Set record/replay buffer limit."),
2483 _("Show record/replay buffer limit."), _("\
2484 Set the maximum number of instructions to be stored in the\n\
2485 record/replay buffer. Zero means unlimited. Default is 200000."),
2486 set_record_insn_max_num
,
2487 NULL
, &set_record_cmdlist
, &show_record_cmdlist
);