1 /* Process record and replay target for GDB, the GNU debugger.
3 Copyright (C) 2008-2012 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"
33 #include "event-loop.h"
39 /* This module implements "target record", also known as "process
40 record and replay". This target sits on top of a "normal" target
41 (a target that "has execution"), and provides a record and replay
42 functionality, including reverse debugging.
44 Target record has two modes: recording, and replaying.
46 In record mode, we intercept the to_resume and to_wait methods.
47 Whenever gdb resumes the target, we run the target in single step
48 mode, and we build up an execution log in which, for each executed
49 instruction, we record all changes in memory and register state.
50 This is invisible to the user, to whom it just looks like an
51 ordinary debugging session (except for performance degredation).
53 In replay mode, instead of actually letting the inferior run as a
54 process, we simulate its execution by playing back the recorded
55 execution log. For each instruction in the log, we simulate the
56 instruction's side effects by duplicating the changes that it would
57 have made on memory and registers. */
59 #define DEFAULT_RECORD_INSN_MAX_NUM 200000
61 #define RECORD_IS_REPLAY \
62 (record_list->next || execution_direction == EXEC_REVERSE)
64 #define RECORD_FILE_MAGIC netorder32(0x20091016)
66 /* These are the core structs of the process record functionality.
68 A record_entry is a record of the value change of a register
69 ("record_reg") or a part of memory ("record_mem"). And each
70 instruction must have a struct record_entry ("record_end") that
71 indicates that this is the last struct record_entry of this
74 Each struct record_entry is linked to "record_list" by "prev" and
77 struct record_mem_entry
81 /* Set this flag if target memory for this entry
82 can no longer be accessed. */
83 int mem_entry_not_accessible
;
87 gdb_byte buf
[sizeof (gdb_byte
*)];
91 struct record_reg_entry
98 gdb_byte buf
[2 * sizeof (gdb_byte
*)];
102 struct record_end_entry
104 enum gdb_signal sigval
;
115 /* This is the data structure that makes up the execution log.
117 The execution log consists of a single linked list of entries
118 of type "struct record_entry". It is doubly linked so that it
119 can be traversed in either direction.
121 The start of the list is anchored by a struct called
122 "record_first". The pointer "record_list" either points to the
123 last entry that was added to the list (in record mode), or to the
124 next entry in the list that will be executed (in replay mode).
126 Each list element (struct record_entry), in addition to next and
127 prev pointers, consists of a union of three entry types: mem, reg,
128 and end. A field called "type" determines which entry type is
129 represented by a given list element.
131 Each instruction that is added to the execution log is represented
132 by a variable number of list elements ('entries'). The instruction
133 will have one "reg" entry for each register that is changed by
134 executing the instruction (including the PC in every case). It
135 will also have one "mem" entry for each memory change. Finally,
136 each instruction will have an "end" entry that separates it from
137 the changes associated with the next instruction. */
141 struct record_entry
*prev
;
142 struct record_entry
*next
;
143 enum record_type type
;
147 struct record_reg_entry reg
;
149 struct record_mem_entry mem
;
151 struct record_end_entry end
;
155 /* This is the debug switch for process record. */
156 int record_debug
= 0;
158 /* If true, query if PREC cannot record memory
159 change of next instruction. */
160 int record_memory_query
= 0;
162 struct record_core_buf_entry
164 struct record_core_buf_entry
*prev
;
165 struct target_section
*p
;
169 /* Record buf with core target. */
170 static gdb_byte
*record_core_regbuf
= NULL
;
171 static struct target_section
*record_core_start
;
172 static struct target_section
*record_core_end
;
173 static struct record_core_buf_entry
*record_core_buf_list
= NULL
;
175 /* The following variables are used for managing the linked list that
176 represents the execution log.
178 record_first is the anchor that holds down the beginning of the list.
180 record_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_arch_list_head and record_arch_list_tail are used to manage
186 a separate list, which is used to build up the change elements of
187 the currently executing instruction during record mode. When this
188 instruction has been completely annotated in the "arch list", it
189 will be appended to the main execution log. */
191 static struct record_entry record_first
;
192 static struct record_entry
*record_list
= &record_first
;
193 static struct record_entry
*record_arch_list_head
= NULL
;
194 static struct record_entry
*record_arch_list_tail
= NULL
;
196 /* 1 ask user. 0 auto delete the last struct record_entry. */
197 static int record_stop_at_limit
= 1;
198 /* Maximum allowed number of insns in execution log. */
199 static unsigned int record_insn_max_num
= DEFAULT_RECORD_INSN_MAX_NUM
;
200 /* Actual count of insns presently in execution log. */
201 static int record_insn_num
= 0;
202 /* Count of insns logged so far (may be larger
203 than count of insns presently in execution log). */
204 static ULONGEST record_insn_count
;
206 /* The target_ops of process record. */
207 static struct target_ops record_ops
;
208 static struct target_ops record_core_ops
;
210 /* The beneath function pointers. */
211 static struct target_ops
*record_beneath_to_resume_ops
;
212 static void (*record_beneath_to_resume
) (struct target_ops
*, ptid_t
, int,
214 static struct target_ops
*record_beneath_to_wait_ops
;
215 static ptid_t (*record_beneath_to_wait
) (struct target_ops
*, ptid_t
,
216 struct target_waitstatus
*,
218 static struct target_ops
*record_beneath_to_store_registers_ops
;
219 static void (*record_beneath_to_store_registers
) (struct target_ops
*,
222 static struct target_ops
*record_beneath_to_xfer_partial_ops
;
223 static LONGEST (*record_beneath_to_xfer_partial
) (struct target_ops
*ops
,
224 enum target_object object
,
227 const gdb_byte
*writebuf
,
230 static int (*record_beneath_to_insert_breakpoint
) (struct gdbarch
*,
231 struct bp_target_info
*);
232 static int (*record_beneath_to_remove_breakpoint
) (struct gdbarch
*,
233 struct bp_target_info
*);
234 static int (*record_beneath_to_stopped_by_watchpoint
) (void);
235 static int (*record_beneath_to_stopped_data_address
) (struct target_ops
*,
237 static void (*record_beneath_to_async
) (void (*) (enum inferior_event_type
, void *), void *);
239 /* Alloc and free functions for record_reg, record_mem, and record_end
242 /* Alloc a record_reg record entry. */
244 static inline struct record_entry
*
245 record_reg_alloc (struct regcache
*regcache
, int regnum
)
247 struct record_entry
*rec
;
248 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
250 rec
= (struct record_entry
*) xcalloc (1, sizeof (struct record_entry
));
251 rec
->type
= record_reg
;
252 rec
->u
.reg
.num
= regnum
;
253 rec
->u
.reg
.len
= register_size (gdbarch
, regnum
);
254 if (rec
->u
.reg
.len
> sizeof (rec
->u
.reg
.u
.buf
))
255 rec
->u
.reg
.u
.ptr
= (gdb_byte
*) xmalloc (rec
->u
.reg
.len
);
260 /* Free a record_reg record entry. */
263 record_reg_release (struct record_entry
*rec
)
265 gdb_assert (rec
->type
== record_reg
);
266 if (rec
->u
.reg
.len
> sizeof (rec
->u
.reg
.u
.buf
))
267 xfree (rec
->u
.reg
.u
.ptr
);
271 /* Alloc a record_mem record entry. */
273 static inline struct record_entry
*
274 record_mem_alloc (CORE_ADDR addr
, int len
)
276 struct record_entry
*rec
;
278 rec
= (struct record_entry
*) xcalloc (1, sizeof (struct record_entry
));
279 rec
->type
= record_mem
;
280 rec
->u
.mem
.addr
= addr
;
281 rec
->u
.mem
.len
= len
;
282 if (rec
->u
.mem
.len
> sizeof (rec
->u
.mem
.u
.buf
))
283 rec
->u
.mem
.u
.ptr
= (gdb_byte
*) xmalloc (len
);
288 /* Free a record_mem record entry. */
291 record_mem_release (struct record_entry
*rec
)
293 gdb_assert (rec
->type
== record_mem
);
294 if (rec
->u
.mem
.len
> sizeof (rec
->u
.mem
.u
.buf
))
295 xfree (rec
->u
.mem
.u
.ptr
);
299 /* Alloc a record_end record entry. */
301 static inline struct record_entry
*
302 record_end_alloc (void)
304 struct record_entry
*rec
;
306 rec
= (struct record_entry
*) xcalloc (1, sizeof (struct record_entry
));
307 rec
->type
= record_end
;
312 /* Free a record_end record entry. */
315 record_end_release (struct record_entry
*rec
)
320 /* Free one record entry, any type.
321 Return entry->type, in case caller wants to know. */
323 static inline enum record_type
324 record_entry_release (struct record_entry
*rec
)
326 enum record_type type
= rec
->type
;
330 record_reg_release (rec
);
333 record_mem_release (rec
);
336 record_end_release (rec
);
342 /* Free all record entries in list pointed to by REC. */
345 record_list_release (struct record_entry
*rec
)
356 record_entry_release (rec
->next
);
359 if (rec
== &record_first
)
362 record_first
.next
= NULL
;
365 record_entry_release (rec
);
368 /* Free all record entries forward of the given list position. */
371 record_list_release_following (struct record_entry
*rec
)
373 struct record_entry
*tmp
= rec
->next
;
379 if (record_entry_release (tmp
) == record_end
)
388 /* Delete the first instruction from the beginning of the log, to make
389 room for adding a new instruction at the end of the log.
391 Note -- this function does not modify record_insn_num. */
394 record_list_release_first (void)
396 struct record_entry
*tmp
;
398 if (!record_first
.next
)
401 /* Loop until a record_end. */
404 /* Cut record_first.next out of the linked list. */
405 tmp
= record_first
.next
;
406 record_first
.next
= tmp
->next
;
407 tmp
->next
->prev
= &record_first
;
409 /* tmp is now isolated, and can be deleted. */
410 if (record_entry_release (tmp
) == record_end
)
411 break; /* End loop at first record_end. */
413 if (!record_first
.next
)
415 gdb_assert (record_insn_num
== 1);
416 break; /* End loop when list is empty. */
421 /* Add a struct record_entry to record_arch_list. */
424 record_arch_list_add (struct record_entry
*rec
)
426 if (record_debug
> 1)
427 fprintf_unfiltered (gdb_stdlog
,
428 "Process record: record_arch_list_add %s.\n",
429 host_address_to_string (rec
));
431 if (record_arch_list_tail
)
433 record_arch_list_tail
->next
= rec
;
434 rec
->prev
= record_arch_list_tail
;
435 record_arch_list_tail
= rec
;
439 record_arch_list_head
= rec
;
440 record_arch_list_tail
= rec
;
444 /* Return the value storage location of a record entry. */
445 static inline gdb_byte
*
446 record_get_loc (struct record_entry
*rec
)
450 if (rec
->u
.mem
.len
> sizeof (rec
->u
.mem
.u
.buf
))
451 return rec
->u
.mem
.u
.ptr
;
453 return rec
->u
.mem
.u
.buf
;
455 if (rec
->u
.reg
.len
> sizeof (rec
->u
.reg
.u
.buf
))
456 return rec
->u
.reg
.u
.ptr
;
458 return rec
->u
.reg
.u
.buf
;
461 gdb_assert_not_reached ("unexpected record_entry type");
466 /* Record the value of a register NUM to record_arch_list. */
469 record_arch_list_add_reg (struct regcache
*regcache
, int regnum
)
471 struct record_entry
*rec
;
473 if (record_debug
> 1)
474 fprintf_unfiltered (gdb_stdlog
,
475 "Process record: add register num = %d to "
479 rec
= record_reg_alloc (regcache
, regnum
);
481 regcache_raw_read (regcache
, regnum
, record_get_loc (rec
));
483 record_arch_list_add (rec
);
489 record_read_memory (struct gdbarch
*gdbarch
,
490 CORE_ADDR memaddr
, gdb_byte
*myaddr
,
493 int ret
= target_read_memory (memaddr
, myaddr
, len
);
495 if (ret
&& record_debug
)
496 printf_unfiltered (_("Process record: error reading memory "
497 "at addr %s len = %ld.\n"),
498 paddress (gdbarch
, memaddr
), (long) len
);
502 /* Record the value of a region of memory whose address is ADDR and
503 length is LEN to record_arch_list. */
506 record_arch_list_add_mem (CORE_ADDR addr
, int len
)
508 struct record_entry
*rec
;
510 if (record_debug
> 1)
511 fprintf_unfiltered (gdb_stdlog
,
512 "Process record: add mem addr = %s len = %d to "
514 paddress (target_gdbarch
, addr
), len
);
516 if (!addr
) /* FIXME: Why? Some arch must permit it... */
519 rec
= record_mem_alloc (addr
, len
);
521 if (record_read_memory (target_gdbarch
, addr
, record_get_loc (rec
), len
))
523 record_mem_release (rec
);
527 record_arch_list_add (rec
);
532 /* Add a record_end type struct record_entry to record_arch_list. */
535 record_arch_list_add_end (void)
537 struct record_entry
*rec
;
539 if (record_debug
> 1)
540 fprintf_unfiltered (gdb_stdlog
,
541 "Process record: add end to arch list.\n");
543 rec
= record_end_alloc ();
544 rec
->u
.end
.sigval
= GDB_SIGNAL_0
;
545 rec
->u
.end
.insn_num
= ++record_insn_count
;
547 record_arch_list_add (rec
);
553 record_check_insn_num (int set_terminal
)
555 if (record_insn_max_num
)
557 gdb_assert (record_insn_num
<= record_insn_max_num
);
558 if (record_insn_num
== record_insn_max_num
)
560 /* Ask user what to do. */
561 if (record_stop_at_limit
)
566 target_terminal_ours ();
567 q
= yquery (_("Do you want to auto delete previous execution "
568 "log entries when record/replay buffer becomes "
569 "full (record stop-at-limit)?"));
571 target_terminal_inferior ();
573 record_stop_at_limit
= 0;
575 error (_("Process record: stopped by user."));
582 record_arch_list_cleanups (void *ignore
)
584 record_list_release (record_arch_list_tail
);
587 /* Before inferior step (when GDB record the running message, inferior
588 only can step), GDB will call this function to record the values to
589 record_list. This function will call gdbarch_process_record to
590 record the running message of inferior and set them to
591 record_arch_list, and add it to record_list. */
594 record_message (struct regcache
*regcache
, enum gdb_signal signal
)
597 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
598 struct cleanup
*old_cleanups
= make_cleanup (record_arch_list_cleanups
, 0);
600 record_arch_list_head
= NULL
;
601 record_arch_list_tail
= NULL
;
603 /* Check record_insn_num. */
604 record_check_insn_num (1);
606 /* If gdb sends a signal value to target_resume,
607 save it in the 'end' field of the previous instruction.
609 Maybe process record should record what really happened,
610 rather than what gdb pretends has happened.
612 So if Linux delivered the signal to the child process during
613 the record mode, we will record it and deliver it again in
616 If user says "ignore this signal" during the record mode, then
617 it will be ignored again during the replay mode (no matter if
618 the user says something different, like "deliver this signal"
619 during the replay mode).
621 User should understand that nothing he does during the replay
622 mode will change the behavior of the child. If he tries,
623 then that is a user error.
625 But we should still deliver the signal to gdb during the replay,
626 if we delivered it during the recording. Therefore we should
627 record the signal during record_wait, not record_resume. */
628 if (record_list
!= &record_first
) /* FIXME better way to check */
630 gdb_assert (record_list
->type
== record_end
);
631 record_list
->u
.end
.sigval
= signal
;
634 if (signal
== GDB_SIGNAL_0
635 || !gdbarch_process_record_signal_p (gdbarch
))
636 ret
= gdbarch_process_record (gdbarch
,
638 regcache_read_pc (regcache
));
640 ret
= gdbarch_process_record_signal (gdbarch
,
645 error (_("Process record: inferior program stopped."));
647 error (_("Process record: failed to record execution log."));
649 discard_cleanups (old_cleanups
);
651 record_list
->next
= record_arch_list_head
;
652 record_arch_list_head
->prev
= record_list
;
653 record_list
= record_arch_list_tail
;
655 if (record_insn_num
== record_insn_max_num
&& record_insn_max_num
)
656 record_list_release_first ();
663 struct record_message_args
{
664 struct regcache
*regcache
;
665 enum gdb_signal signal
;
669 record_message_wrapper (void *args
)
671 struct record_message_args
*record_args
= args
;
673 return record_message (record_args
->regcache
, record_args
->signal
);
677 record_message_wrapper_safe (struct regcache
*regcache
,
678 enum gdb_signal signal
)
680 struct record_message_args args
;
682 args
.regcache
= regcache
;
683 args
.signal
= signal
;
685 return catch_errors (record_message_wrapper
, &args
, NULL
, RETURN_MASK_ALL
);
688 /* Set to 1 if record_store_registers and record_xfer_partial
689 doesn't need record. */
691 static int record_gdb_operation_disable
= 0;
694 record_gdb_operation_disable_set (void)
696 struct cleanup
*old_cleanups
= NULL
;
699 make_cleanup_restore_integer (&record_gdb_operation_disable
);
700 record_gdb_operation_disable
= 1;
705 /* Flag set to TRUE for target_stopped_by_watchpoint. */
706 static int record_hw_watchpoint
= 0;
708 /* Execute one instruction from the record log. Each instruction in
709 the log will be represented by an arbitrary sequence of register
710 entries and memory entries, followed by an 'end' entry. */
713 record_exec_insn (struct regcache
*regcache
, struct gdbarch
*gdbarch
,
714 struct record_entry
*entry
)
718 case record_reg
: /* reg */
720 gdb_byte reg
[MAX_REGISTER_SIZE
];
722 if (record_debug
> 1)
723 fprintf_unfiltered (gdb_stdlog
,
724 "Process record: record_reg %s to "
725 "inferior num = %d.\n",
726 host_address_to_string (entry
),
729 regcache_cooked_read (regcache
, entry
->u
.reg
.num
, reg
);
730 regcache_cooked_write (regcache
, entry
->u
.reg
.num
,
731 record_get_loc (entry
));
732 memcpy (record_get_loc (entry
), reg
, entry
->u
.reg
.len
);
736 case record_mem
: /* mem */
738 /* Nothing to do if the entry is flagged not_accessible. */
739 if (!entry
->u
.mem
.mem_entry_not_accessible
)
741 gdb_byte
*mem
= alloca (entry
->u
.mem
.len
);
743 if (record_debug
> 1)
744 fprintf_unfiltered (gdb_stdlog
,
745 "Process record: record_mem %s to "
746 "inferior addr = %s len = %d.\n",
747 host_address_to_string (entry
),
748 paddress (gdbarch
, entry
->u
.mem
.addr
),
751 if (record_read_memory (gdbarch
,
752 entry
->u
.mem
.addr
, mem
, entry
->u
.mem
.len
))
753 entry
->u
.mem
.mem_entry_not_accessible
= 1;
756 if (target_write_memory (entry
->u
.mem
.addr
,
757 record_get_loc (entry
),
760 entry
->u
.mem
.mem_entry_not_accessible
= 1;
762 warning (_("Process record: error writing memory at "
763 "addr = %s len = %d."),
764 paddress (gdbarch
, entry
->u
.mem
.addr
),
769 memcpy (record_get_loc (entry
), mem
, entry
->u
.mem
.len
);
771 /* We've changed memory --- check if a hardware
772 watchpoint should trap. Note that this
773 presently assumes the target beneath supports
774 continuable watchpoints. On non-continuable
775 watchpoints target, we'll want to check this
776 _before_ actually doing the memory change, and
777 not doing the change at all if the watchpoint
779 if (hardware_watchpoint_inserted_in_range
780 (get_regcache_aspace (regcache
),
781 entry
->u
.mem
.addr
, entry
->u
.mem
.len
))
782 record_hw_watchpoint
= 1;
791 static struct target_ops
*tmp_to_resume_ops
;
792 static void (*tmp_to_resume
) (struct target_ops
*, ptid_t
, int,
794 static struct target_ops
*tmp_to_wait_ops
;
795 static ptid_t (*tmp_to_wait
) (struct target_ops
*, ptid_t
,
796 struct target_waitstatus
*,
798 static struct target_ops
*tmp_to_store_registers_ops
;
799 static void (*tmp_to_store_registers
) (struct target_ops
*,
802 static struct target_ops
*tmp_to_xfer_partial_ops
;
803 static LONGEST (*tmp_to_xfer_partial
) (struct target_ops
*ops
,
804 enum target_object object
,
807 const gdb_byte
*writebuf
,
810 static int (*tmp_to_insert_breakpoint
) (struct gdbarch
*,
811 struct bp_target_info
*);
812 static int (*tmp_to_remove_breakpoint
) (struct gdbarch
*,
813 struct bp_target_info
*);
814 static int (*tmp_to_stopped_by_watchpoint
) (void);
815 static int (*tmp_to_stopped_data_address
) (struct target_ops
*, CORE_ADDR
*);
816 static int (*tmp_to_stopped_data_address
) (struct target_ops
*, CORE_ADDR
*);
817 static void (*tmp_to_async
) (void (*) (enum inferior_event_type
, void *), void *);
819 static void record_restore (void);
821 /* Asynchronous signal handle registered as event loop source for when
822 we have pending events ready to be passed to the core. */
824 static struct async_event_handler
*record_async_inferior_event_token
;
827 record_async_inferior_event_handler (gdb_client_data data
)
829 inferior_event_handler (INF_REG_EVENT
, NULL
);
832 /* Open the process record target. */
835 record_core_open_1 (char *name
, int from_tty
)
837 struct regcache
*regcache
= get_current_regcache ();
838 int regnum
= gdbarch_num_regs (get_regcache_arch (regcache
));
841 /* Get record_core_regbuf. */
842 target_fetch_registers (regcache
, -1);
843 record_core_regbuf
= xmalloc (MAX_REGISTER_SIZE
* regnum
);
844 for (i
= 0; i
< regnum
; i
++)
845 regcache_raw_collect (regcache
, i
,
846 record_core_regbuf
+ MAX_REGISTER_SIZE
* i
);
848 /* Get record_core_start and record_core_end. */
849 if (build_section_table (core_bfd
, &record_core_start
, &record_core_end
))
851 xfree (record_core_regbuf
);
852 record_core_regbuf
= NULL
;
853 error (_("\"%s\": Can't find sections: %s"),
854 bfd_get_filename (core_bfd
), bfd_errmsg (bfd_get_error ()));
857 push_target (&record_core_ops
);
861 /* "to_open" target method for 'live' processes. */
864 record_open_1 (char *name
, int from_tty
)
867 fprintf_unfiltered (gdb_stdlog
, "Process record: record_open\n");
870 if (!target_has_execution
)
871 error (_("Process record: the program is not being run."));
873 error (_("Process record target can't debug inferior in non-stop mode "
876 if (!gdbarch_process_record_p (target_gdbarch
))
877 error (_("Process record: the current architecture doesn't support "
878 "record function."));
881 error (_("Could not find 'to_resume' method on the target stack."));
883 error (_("Could not find 'to_wait' method on the target stack."));
884 if (!tmp_to_store_registers
)
885 error (_("Could not find 'to_store_registers' "
886 "method on the target stack."));
887 if (!tmp_to_insert_breakpoint
)
888 error (_("Could not find 'to_insert_breakpoint' "
889 "method on the target stack."));
890 if (!tmp_to_remove_breakpoint
)
891 error (_("Could not find 'to_remove_breakpoint' "
892 "method on the target stack."));
893 if (!tmp_to_stopped_by_watchpoint
)
894 error (_("Could not find 'to_stopped_by_watchpoint' "
895 "method on the target stack."));
896 if (!tmp_to_stopped_data_address
)
897 error (_("Could not find 'to_stopped_data_address' "
898 "method on the target stack."));
900 push_target (&record_ops
);
903 static void record_init_record_breakpoints (void);
905 /* "to_open" target method. Open the process record target. */
908 record_open (char *name
, int from_tty
)
910 struct target_ops
*t
;
913 fprintf_unfiltered (gdb_stdlog
, "Process record: record_open\n");
915 /* Check if record target is already running. */
916 if (current_target
.to_stratum
== record_stratum
)
917 error (_("Process record target already running. Use \"record stop\" to "
918 "stop record target first."));
920 /* Reset the tmp beneath pointers. */
921 tmp_to_resume_ops
= NULL
;
922 tmp_to_resume
= NULL
;
923 tmp_to_wait_ops
= NULL
;
925 tmp_to_store_registers_ops
= NULL
;
926 tmp_to_store_registers
= NULL
;
927 tmp_to_xfer_partial_ops
= NULL
;
928 tmp_to_xfer_partial
= NULL
;
929 tmp_to_insert_breakpoint
= NULL
;
930 tmp_to_remove_breakpoint
= NULL
;
931 tmp_to_stopped_by_watchpoint
= NULL
;
932 tmp_to_stopped_data_address
= NULL
;
935 /* Set the beneath function pointers. */
936 for (t
= current_target
.beneath
; t
!= NULL
; t
= t
->beneath
)
940 tmp_to_resume
= t
->to_resume
;
941 tmp_to_resume_ops
= t
;
945 tmp_to_wait
= t
->to_wait
;
948 if (!tmp_to_store_registers
)
950 tmp_to_store_registers
= t
->to_store_registers
;
951 tmp_to_store_registers_ops
= t
;
953 if (!tmp_to_xfer_partial
)
955 tmp_to_xfer_partial
= t
->to_xfer_partial
;
956 tmp_to_xfer_partial_ops
= t
;
958 if (!tmp_to_insert_breakpoint
)
959 tmp_to_insert_breakpoint
= t
->to_insert_breakpoint
;
960 if (!tmp_to_remove_breakpoint
)
961 tmp_to_remove_breakpoint
= t
->to_remove_breakpoint
;
962 if (!tmp_to_stopped_by_watchpoint
)
963 tmp_to_stopped_by_watchpoint
= t
->to_stopped_by_watchpoint
;
964 if (!tmp_to_stopped_data_address
)
965 tmp_to_stopped_data_address
= t
->to_stopped_data_address
;
967 tmp_to_async
= t
->to_async
;
969 if (!tmp_to_xfer_partial
)
970 error (_("Could not find 'to_xfer_partial' method on the target stack."));
974 record_insn_count
= 0;
975 record_list
= &record_first
;
976 record_list
->next
= NULL
;
978 /* Set the tmp beneath pointers to beneath pointers. */
979 record_beneath_to_resume_ops
= tmp_to_resume_ops
;
980 record_beneath_to_resume
= tmp_to_resume
;
981 record_beneath_to_wait_ops
= tmp_to_wait_ops
;
982 record_beneath_to_wait
= tmp_to_wait
;
983 record_beneath_to_store_registers_ops
= tmp_to_store_registers_ops
;
984 record_beneath_to_store_registers
= tmp_to_store_registers
;
985 record_beneath_to_xfer_partial_ops
= tmp_to_xfer_partial_ops
;
986 record_beneath_to_xfer_partial
= tmp_to_xfer_partial
;
987 record_beneath_to_insert_breakpoint
= tmp_to_insert_breakpoint
;
988 record_beneath_to_remove_breakpoint
= tmp_to_remove_breakpoint
;
989 record_beneath_to_stopped_by_watchpoint
= tmp_to_stopped_by_watchpoint
;
990 record_beneath_to_stopped_data_address
= tmp_to_stopped_data_address
;
991 record_beneath_to_async
= tmp_to_async
;
994 record_core_open_1 (name
, from_tty
);
996 record_open_1 (name
, from_tty
);
998 /* Register extra event sources in the event loop. */
999 record_async_inferior_event_token
1000 = create_async_event_handler (record_async_inferior_event_handler
,
1003 record_init_record_breakpoints ();
1006 /* "to_close" target method. Close the process record target. */
1009 record_close (int quitting
)
1011 struct record_core_buf_entry
*entry
;
1014 fprintf_unfiltered (gdb_stdlog
, "Process record: record_close\n");
1016 record_list_release (record_list
);
1018 /* Release record_core_regbuf. */
1019 if (record_core_regbuf
)
1021 xfree (record_core_regbuf
);
1022 record_core_regbuf
= NULL
;
1025 /* Release record_core_buf_list. */
1026 if (record_core_buf_list
)
1028 for (entry
= record_core_buf_list
->prev
; entry
; entry
= entry
->prev
)
1030 xfree (record_core_buf_list
);
1031 record_core_buf_list
= entry
;
1033 record_core_buf_list
= NULL
;
1036 if (record_async_inferior_event_token
)
1037 delete_async_event_handler (&record_async_inferior_event_token
);
1040 static int record_resume_step
= 0;
1042 /* True if we've been resumed, and so each record_wait call should
1043 advance execution. If this is false, record_wait will return a
1044 TARGET_WAITKIND_IGNORE. */
1045 static int record_resumed
= 0;
1047 /* The execution direction of the last resume we got. This is
1048 necessary for async mode. Vis (order is not strictly accurate):
1050 1. user has the global execution direction set to forward
1051 2. user does a reverse-step command
1052 3. record_resume is called with global execution direction
1053 temporarily switched to reverse
1054 4. GDB's execution direction is reverted back to forward
1055 5. target record notifies event loop there's an event to handle
1056 6. infrun asks the target which direction was it going, and switches
1057 the global execution direction accordingly (to reverse)
1058 7. infrun polls an event out of the record target, and handles it
1059 8. GDB goes back to the event loop, and goto #4.
1061 static enum exec_direction_kind record_execution_dir
= EXEC_FORWARD
;
1063 /* "to_resume" target method. Resume the process record target. */
1066 record_resume (struct target_ops
*ops
, ptid_t ptid
, int step
,
1067 enum gdb_signal signal
)
1069 record_resume_step
= step
;
1071 record_execution_dir
= execution_direction
;
1073 if (!RECORD_IS_REPLAY
)
1075 struct gdbarch
*gdbarch
= target_thread_architecture (ptid
);
1077 record_message (get_current_regcache (), signal
);
1081 /* This is not hard single step. */
1082 if (!gdbarch_software_single_step_p (gdbarch
))
1084 /* This is a normal continue. */
1089 /* This arch support soft sigle step. */
1090 if (single_step_breakpoints_inserted ())
1092 /* This is a soft single step. */
1093 record_resume_step
= 1;
1097 /* This is a continue.
1098 Try to insert a soft single step breakpoint. */
1099 if (!gdbarch_software_single_step (gdbarch
,
1100 get_current_frame ()))
1102 /* This system don't want use soft single step.
1103 Use hard sigle step. */
1110 /* Make sure the target beneath reports all signals. */
1111 target_pass_signals (0, NULL
);
1113 record_beneath_to_resume (record_beneath_to_resume_ops
,
1114 ptid
, step
, signal
);
1117 /* We are about to start executing the inferior (or simulate it),
1118 let's register it with the event loop. */
1119 if (target_can_async_p ())
1121 target_async (inferior_event_handler
, 0);
1122 /* Notify the event loop there's an event to wait for. We do
1123 most of the work in record_wait. */
1124 mark_async_event_handler (record_async_inferior_event_token
);
1128 static int record_get_sig
= 0;
1130 /* SIGINT signal handler, registered by "to_wait" method. */
1133 record_sig_handler (int signo
)
1136 fprintf_unfiltered (gdb_stdlog
, "Process record: get a signal\n");
1138 /* It will break the running inferior in replay mode. */
1139 record_resume_step
= 1;
1141 /* It will let record_wait set inferior status to get the signal
1147 record_wait_cleanups (void *ignore
)
1149 if (execution_direction
== EXEC_REVERSE
)
1151 if (record_list
->next
)
1152 record_list
= record_list
->next
;
1155 record_list
= record_list
->prev
;
1158 /* "to_wait" target method for process record target.
1160 In record mode, the target is always run in singlestep mode
1161 (even when gdb says to continue). The to_wait method intercepts
1162 the stop events and determines which ones are to be passed on to
1163 gdb. Most stop events are just singlestep events that gdb is not
1164 to know about, so the to_wait method just records them and keeps
1167 In replay mode, this function emulates the recorded execution log,
1168 one instruction at a time (forward or backward), and determines
1172 record_wait_1 (struct target_ops
*ops
,
1173 ptid_t ptid
, struct target_waitstatus
*status
,
1176 struct cleanup
*set_cleanups
= record_gdb_operation_disable_set ();
1179 fprintf_unfiltered (gdb_stdlog
,
1180 "Process record: record_wait "
1181 "record_resume_step = %d, record_resumed = %d, direction=%s\n",
1182 record_resume_step
, record_resumed
,
1183 record_execution_dir
== EXEC_FORWARD
? "forward" : "reverse");
1185 if (!record_resumed
)
1187 gdb_assert ((options
& TARGET_WNOHANG
) != 0);
1189 /* No interesting event. */
1190 status
->kind
= TARGET_WAITKIND_IGNORE
;
1191 return minus_one_ptid
;
1195 signal (SIGINT
, record_sig_handler
);
1197 if (!RECORD_IS_REPLAY
&& ops
!= &record_core_ops
)
1199 if (record_resume_step
)
1201 /* This is a single step. */
1202 return record_beneath_to_wait (record_beneath_to_wait_ops
,
1203 ptid
, status
, options
);
1207 /* This is not a single step. */
1210 struct gdbarch
*gdbarch
= target_thread_architecture (inferior_ptid
);
1214 ret
= record_beneath_to_wait (record_beneath_to_wait_ops
,
1215 ptid
, status
, options
);
1216 if (status
->kind
== TARGET_WAITKIND_IGNORE
)
1219 fprintf_unfiltered (gdb_stdlog
,
1220 "Process record: record_wait "
1221 "target beneath not done yet\n");
1225 if (single_step_breakpoints_inserted ())
1226 remove_single_step_breakpoints ();
1228 if (record_resume_step
)
1231 /* Is this a SIGTRAP? */
1232 if (status
->kind
== TARGET_WAITKIND_STOPPED
1233 && status
->value
.sig
== GDB_SIGNAL_TRAP
)
1235 struct regcache
*regcache
;
1236 struct address_space
*aspace
;
1238 /* Yes -- this is likely our single-step finishing,
1239 but check if there's any reason the core would be
1240 interested in the event. */
1242 registers_changed ();
1243 regcache
= get_current_regcache ();
1244 tmp_pc
= regcache_read_pc (regcache
);
1245 aspace
= get_regcache_aspace (regcache
);
1247 if (target_stopped_by_watchpoint ())
1249 /* Always interested in watchpoints. */
1251 else if (breakpoint_inserted_here_p (aspace
, tmp_pc
))
1253 /* There is a breakpoint here. Let the core
1255 if (software_breakpoint_inserted_here_p (aspace
, tmp_pc
))
1257 struct gdbarch
*gdbarch
1258 = get_regcache_arch (regcache
);
1259 CORE_ADDR decr_pc_after_break
1260 = gdbarch_decr_pc_after_break (gdbarch
);
1261 if (decr_pc_after_break
)
1262 regcache_write_pc (regcache
,
1263 tmp_pc
+ decr_pc_after_break
);
1268 /* This is a single-step trap. Record the
1269 insn and issue another step.
1270 FIXME: this part can be a random SIGTRAP too.
1271 But GDB cannot handle it. */
1274 if (!record_message_wrapper_safe (regcache
,
1277 status
->kind
= TARGET_WAITKIND_STOPPED
;
1278 status
->value
.sig
= GDB_SIGNAL_0
;
1282 if (gdbarch_software_single_step_p (gdbarch
))
1284 /* Try to insert the software single step breakpoint.
1285 If insert success, set step to 0. */
1286 set_executing (inferior_ptid
, 0);
1287 reinit_frame_cache ();
1288 if (gdbarch_software_single_step (gdbarch
,
1289 get_current_frame ()))
1291 set_executing (inferior_ptid
, 1);
1295 fprintf_unfiltered (gdb_stdlog
,
1296 "Process record: record_wait "
1297 "issuing one more step in the target beneath\n");
1298 record_beneath_to_resume (record_beneath_to_resume_ops
,
1305 /* The inferior is broken by a breakpoint or a signal. */
1314 struct regcache
*regcache
= get_current_regcache ();
1315 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1316 struct address_space
*aspace
= get_regcache_aspace (regcache
);
1317 int continue_flag
= 1;
1318 int first_record_end
= 1;
1319 struct cleanup
*old_cleanups
= make_cleanup (record_wait_cleanups
, 0);
1322 record_hw_watchpoint
= 0;
1323 status
->kind
= TARGET_WAITKIND_STOPPED
;
1325 /* Check breakpoint when forward execute. */
1326 if (execution_direction
== EXEC_FORWARD
)
1328 tmp_pc
= regcache_read_pc (regcache
);
1329 if (breakpoint_inserted_here_p (aspace
, tmp_pc
))
1331 int decr_pc_after_break
= gdbarch_decr_pc_after_break (gdbarch
);
1334 fprintf_unfiltered (gdb_stdlog
,
1335 "Process record: break at %s.\n",
1336 paddress (gdbarch
, tmp_pc
));
1338 if (decr_pc_after_break
1339 && !record_resume_step
1340 && software_breakpoint_inserted_here_p (aspace
, tmp_pc
))
1341 regcache_write_pc (regcache
,
1342 tmp_pc
+ decr_pc_after_break
);
1347 /* If GDB is in terminal_inferior mode, it will not get the signal.
1348 And in GDB replay mode, GDB doesn't need to be in terminal_inferior
1349 mode, because inferior will not executed.
1350 Then set it to terminal_ours to make GDB get the signal. */
1351 target_terminal_ours ();
1353 /* In EXEC_FORWARD mode, record_list points to the tail of prev
1355 if (execution_direction
== EXEC_FORWARD
&& record_list
->next
)
1356 record_list
= record_list
->next
;
1358 /* Loop over the record_list, looking for the next place to
1362 /* Check for beginning and end of log. */
1363 if (execution_direction
== EXEC_REVERSE
1364 && record_list
== &record_first
)
1366 /* Hit beginning of record log in reverse. */
1367 status
->kind
= TARGET_WAITKIND_NO_HISTORY
;
1370 if (execution_direction
!= EXEC_REVERSE
&& !record_list
->next
)
1372 /* Hit end of record log going forward. */
1373 status
->kind
= TARGET_WAITKIND_NO_HISTORY
;
1377 record_exec_insn (regcache
, gdbarch
, record_list
);
1379 if (record_list
->type
== record_end
)
1381 if (record_debug
> 1)
1382 fprintf_unfiltered (gdb_stdlog
,
1383 "Process record: record_end %s to "
1385 host_address_to_string (record_list
));
1387 if (first_record_end
&& execution_direction
== EXEC_REVERSE
)
1389 /* When reverse excute, the first record_end is the part of
1390 current instruction. */
1391 first_record_end
= 0;
1395 /* In EXEC_REVERSE mode, this is the record_end of prev
1397 In EXEC_FORWARD mode, this is the record_end of current
1400 if (record_resume_step
)
1402 if (record_debug
> 1)
1403 fprintf_unfiltered (gdb_stdlog
,
1404 "Process record: step.\n");
1408 /* check breakpoint */
1409 tmp_pc
= regcache_read_pc (regcache
);
1410 if (breakpoint_inserted_here_p (aspace
, tmp_pc
))
1412 int decr_pc_after_break
1413 = gdbarch_decr_pc_after_break (gdbarch
);
1416 fprintf_unfiltered (gdb_stdlog
,
1417 "Process record: break "
1419 paddress (gdbarch
, tmp_pc
));
1420 if (decr_pc_after_break
1421 && execution_direction
== EXEC_FORWARD
1422 && !record_resume_step
1423 && software_breakpoint_inserted_here_p (aspace
,
1425 regcache_write_pc (regcache
,
1426 tmp_pc
+ decr_pc_after_break
);
1430 if (record_hw_watchpoint
)
1433 fprintf_unfiltered (gdb_stdlog
,
1434 "Process record: hit hw "
1438 /* Check target signal */
1439 if (record_list
->u
.end
.sigval
!= GDB_SIGNAL_0
)
1440 /* FIXME: better way to check */
1447 if (execution_direction
== EXEC_REVERSE
)
1449 if (record_list
->prev
)
1450 record_list
= record_list
->prev
;
1454 if (record_list
->next
)
1455 record_list
= record_list
->next
;
1459 while (continue_flag
);
1463 status
->value
.sig
= GDB_SIGNAL_INT
;
1464 else if (record_list
->u
.end
.sigval
!= GDB_SIGNAL_0
)
1465 /* FIXME: better way to check */
1466 status
->value
.sig
= record_list
->u
.end
.sigval
;
1468 status
->value
.sig
= GDB_SIGNAL_TRAP
;
1470 discard_cleanups (old_cleanups
);
1473 signal (SIGINT
, handle_sigint
);
1475 do_cleanups (set_cleanups
);
1476 return inferior_ptid
;
1480 record_wait (struct target_ops
*ops
,
1481 ptid_t ptid
, struct target_waitstatus
*status
,
1486 return_ptid
= record_wait_1 (ops
, ptid
, status
, options
);
1487 if (status
->kind
!= TARGET_WAITKIND_IGNORE
)
1489 /* We're reporting a stop. Make sure any spurious
1490 target_wait(WNOHANG) doesn't advance the target until the
1491 core wants us resumed again. */
1498 record_stopped_by_watchpoint (void)
1500 if (RECORD_IS_REPLAY
)
1501 return record_hw_watchpoint
;
1503 return record_beneath_to_stopped_by_watchpoint ();
1507 record_stopped_data_address (struct target_ops
*ops
, CORE_ADDR
*addr_p
)
1509 if (RECORD_IS_REPLAY
)
1512 return record_beneath_to_stopped_data_address (ops
, addr_p
);
1515 /* "to_disconnect" method for process record target. */
1518 record_disconnect (struct target_ops
*target
, char *args
, int from_tty
)
1521 fprintf_unfiltered (gdb_stdlog
, "Process record: record_disconnect\n");
1523 unpush_target (&record_ops
);
1524 target_disconnect (args
, from_tty
);
1527 /* "to_detach" method for process record target. */
1530 record_detach (struct target_ops
*ops
, char *args
, int from_tty
)
1533 fprintf_unfiltered (gdb_stdlog
, "Process record: record_detach\n");
1535 unpush_target (&record_ops
);
1536 target_detach (args
, from_tty
);
1539 /* "to_mourn_inferior" method for process record target. */
1542 record_mourn_inferior (struct target_ops
*ops
)
1545 fprintf_unfiltered (gdb_stdlog
, "Process record: "
1546 "record_mourn_inferior\n");
1548 unpush_target (&record_ops
);
1549 target_mourn_inferior ();
1552 /* Close process record target before killing the inferior process. */
1555 record_kill (struct target_ops
*ops
)
1558 fprintf_unfiltered (gdb_stdlog
, "Process record: record_kill\n");
1560 unpush_target (&record_ops
);
1564 /* Record registers change (by user or by GDB) to list as an instruction. */
1567 record_registers_change (struct regcache
*regcache
, int regnum
)
1569 /* Check record_insn_num. */
1570 record_check_insn_num (0);
1572 record_arch_list_head
= NULL
;
1573 record_arch_list_tail
= NULL
;
1579 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
1581 if (record_arch_list_add_reg (regcache
, i
))
1583 record_list_release (record_arch_list_tail
);
1584 error (_("Process record: failed to record execution log."));
1590 if (record_arch_list_add_reg (regcache
, regnum
))
1592 record_list_release (record_arch_list_tail
);
1593 error (_("Process record: failed to record execution log."));
1596 if (record_arch_list_add_end ())
1598 record_list_release (record_arch_list_tail
);
1599 error (_("Process record: failed to record execution log."));
1601 record_list
->next
= record_arch_list_head
;
1602 record_arch_list_head
->prev
= record_list
;
1603 record_list
= record_arch_list_tail
;
1605 if (record_insn_num
== record_insn_max_num
&& record_insn_max_num
)
1606 record_list_release_first ();
1611 /* "to_store_registers" method for process record target. */
1614 record_store_registers (struct target_ops
*ops
, struct regcache
*regcache
,
1617 if (!record_gdb_operation_disable
)
1619 if (RECORD_IS_REPLAY
)
1623 /* Let user choose if he wants to write register or not. */
1626 query (_("Because GDB is in replay mode, changing the "
1627 "value of a register will make the execution "
1628 "log unusable from this point onward. "
1629 "Change all registers?"));
1632 query (_("Because GDB is in replay mode, changing the value "
1633 "of a register will make the execution log unusable "
1634 "from this point onward. Change register %s?"),
1635 gdbarch_register_name (get_regcache_arch (regcache
),
1640 /* Invalidate the value of regcache that was set in function
1641 "regcache_raw_write". */
1647 i
< gdbarch_num_regs (get_regcache_arch (regcache
));
1649 regcache_invalidate (regcache
, i
);
1652 regcache_invalidate (regcache
, regno
);
1654 error (_("Process record canceled the operation."));
1657 /* Destroy the record from here forward. */
1658 record_list_release_following (record_list
);
1661 record_registers_change (regcache
, regno
);
1663 record_beneath_to_store_registers (record_beneath_to_store_registers_ops
,
1667 /* "to_xfer_partial" method. Behavior is conditional on RECORD_IS_REPLAY.
1668 In replay mode, we cannot write memory unles we are willing to
1669 invalidate the record/replay log from this point forward. */
1672 record_xfer_partial (struct target_ops
*ops
, enum target_object object
,
1673 const char *annex
, gdb_byte
*readbuf
,
1674 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
1676 if (!record_gdb_operation_disable
1677 && (object
== TARGET_OBJECT_MEMORY
1678 || object
== TARGET_OBJECT_RAW_MEMORY
) && writebuf
)
1680 if (RECORD_IS_REPLAY
)
1682 /* Let user choose if he wants to write memory or not. */
1683 if (!query (_("Because GDB is in replay mode, writing to memory "
1684 "will make the execution log unusable from this "
1685 "point onward. Write memory at address %s?"),
1686 paddress (target_gdbarch
, offset
)))
1687 error (_("Process record canceled the operation."));
1689 /* Destroy the record from here forward. */
1690 record_list_release_following (record_list
);
1693 /* Check record_insn_num */
1694 record_check_insn_num (0);
1696 /* Record registers change to list as an instruction. */
1697 record_arch_list_head
= NULL
;
1698 record_arch_list_tail
= NULL
;
1699 if (record_arch_list_add_mem (offset
, len
))
1701 record_list_release (record_arch_list_tail
);
1703 fprintf_unfiltered (gdb_stdlog
,
1704 "Process record: failed to record "
1708 if (record_arch_list_add_end ())
1710 record_list_release (record_arch_list_tail
);
1712 fprintf_unfiltered (gdb_stdlog
,
1713 "Process record: failed to record "
1717 record_list
->next
= record_arch_list_head
;
1718 record_arch_list_head
->prev
= record_list
;
1719 record_list
= record_arch_list_tail
;
1721 if (record_insn_num
== record_insn_max_num
&& record_insn_max_num
)
1722 record_list_release_first ();
1727 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops
,
1728 object
, annex
, readbuf
, writebuf
,
1732 /* This structure represents a breakpoint inserted while the record
1733 target is active. We use this to know when to install/remove
1734 breakpoints in/from the target beneath. For example, a breakpoint
1735 may be inserted while recording, but removed when not replaying nor
1736 recording. In that case, the breakpoint had not been inserted on
1737 the target beneath, so we should not try to remove it there. */
1739 struct record_breakpoint
1741 /* The address and address space the breakpoint was set at. */
1742 struct address_space
*address_space
;
1745 /* True when the breakpoint has been also installed in the target
1746 beneath. This will be false for breakpoints set during replay or
1748 int in_target_beneath
;
1751 typedef struct record_breakpoint
*record_breakpoint_p
;
1752 DEF_VEC_P(record_breakpoint_p
);
1754 /* The list of breakpoints inserted while the record target is
1756 VEC(record_breakpoint_p
) *record_breakpoints
= NULL
;
1759 record_sync_record_breakpoints (struct bp_location
*loc
, void *data
)
1761 if (loc
->loc_type
!= bp_loc_software_breakpoint
)
1766 struct record_breakpoint
*bp
= XNEW (struct record_breakpoint
);
1768 bp
->addr
= loc
->target_info
.placed_address
;
1769 bp
->address_space
= loc
->target_info
.placed_address_space
;
1771 bp
->in_target_beneath
= 1;
1773 VEC_safe_push (record_breakpoint_p
, record_breakpoints
, bp
);
1777 /* Sync existing breakpoints to record_breakpoints. */
1780 record_init_record_breakpoints (void)
1782 VEC_free (record_breakpoint_p
, record_breakpoints
);
1784 iterate_over_bp_locations (record_sync_record_breakpoints
);
1787 /* Behavior is conditional on RECORD_IS_REPLAY. We will not actually
1788 insert or remove breakpoints in the real target when replaying, nor
1792 record_insert_breakpoint (struct gdbarch
*gdbarch
,
1793 struct bp_target_info
*bp_tgt
)
1795 struct record_breakpoint
*bp
;
1796 int in_target_beneath
= 0;
1798 if (!RECORD_IS_REPLAY
)
1800 /* When recording, we currently always single-step, so we don't
1801 really need to install regular breakpoints in the inferior.
1802 However, we do have to insert software single-step
1803 breakpoints, in case the target can't hardware step. To keep
1804 things single, we always insert. */
1805 struct cleanup
*old_cleanups
;
1808 old_cleanups
= record_gdb_operation_disable_set ();
1809 ret
= record_beneath_to_insert_breakpoint (gdbarch
, bp_tgt
);
1810 do_cleanups (old_cleanups
);
1815 in_target_beneath
= 1;
1818 bp
= XNEW (struct record_breakpoint
);
1819 bp
->addr
= bp_tgt
->placed_address
;
1820 bp
->address_space
= bp_tgt
->placed_address_space
;
1821 bp
->in_target_beneath
= in_target_beneath
;
1822 VEC_safe_push (record_breakpoint_p
, record_breakpoints
, bp
);
1826 /* "to_remove_breakpoint" method for process record target. */
1829 record_remove_breakpoint (struct gdbarch
*gdbarch
,
1830 struct bp_target_info
*bp_tgt
)
1832 struct record_breakpoint
*bp
;
1836 VEC_iterate (record_breakpoint_p
, record_breakpoints
, ix
, bp
);
1839 if (bp
->addr
== bp_tgt
->placed_address
1840 && bp
->address_space
== bp_tgt
->placed_address_space
)
1842 if (bp
->in_target_beneath
)
1844 struct cleanup
*old_cleanups
;
1847 old_cleanups
= record_gdb_operation_disable_set ();
1848 ret
= record_beneath_to_remove_breakpoint (gdbarch
, bp_tgt
);
1849 do_cleanups (old_cleanups
);
1855 VEC_unordered_remove (record_breakpoint_p
, record_breakpoints
, ix
);
1860 gdb_assert_not_reached ("removing unknown breakpoint");
1863 /* "to_can_execute_reverse" method for process record target. */
1866 record_can_execute_reverse (void)
1871 /* "to_get_bookmark" method for process record and prec over core. */
1874 record_get_bookmark (char *args
, int from_tty
)
1876 gdb_byte
*ret
= NULL
;
1878 /* Return stringified form of instruction count. */
1879 if (record_list
&& record_list
->type
== record_end
)
1880 ret
= xstrdup (pulongest (record_list
->u
.end
.insn_num
));
1885 fprintf_unfiltered (gdb_stdlog
,
1886 "record_get_bookmark returns %s\n", ret
);
1888 fprintf_unfiltered (gdb_stdlog
,
1889 "record_get_bookmark returns NULL\n");
1894 /* The implementation of the command "record goto". */
1895 static void cmd_record_goto (char *, int);
1897 /* "to_goto_bookmark" method for process record and prec over core. */
1900 record_goto_bookmark (gdb_byte
*bookmark
, int from_tty
)
1903 fprintf_unfiltered (gdb_stdlog
,
1904 "record_goto_bookmark receives %s\n", bookmark
);
1906 if (bookmark
[0] == '\'' || bookmark
[0] == '\"')
1908 if (bookmark
[strlen (bookmark
) - 1] != bookmark
[0])
1909 error (_("Unbalanced quotes: %s"), bookmark
);
1911 /* Strip trailing quote. */
1912 bookmark
[strlen (bookmark
) - 1] = '\0';
1913 /* Strip leading quote. */
1915 /* Pass along to cmd_record_goto. */
1918 cmd_record_goto ((char *) bookmark
, from_tty
);
1923 record_async (void (*callback
) (enum inferior_event_type event_type
,
1924 void *context
), void *context
)
1926 /* If we're on top of a line target (e.g., linux-nat, remote), then
1927 set it to async mode as well. Will be NULL if we're sitting on
1928 top of the core target, for "record restore". */
1929 if (record_beneath_to_async
!= NULL
)
1930 record_beneath_to_async (callback
, context
);
1934 record_can_async_p (void)
1936 /* We only enable async when the user specifically asks for it. */
1937 return target_async_permitted
;
1941 record_is_async_p (void)
1943 /* We only enable async when the user specifically asks for it. */
1944 return target_async_permitted
;
1947 static enum exec_direction_kind
1948 record_execution_direction (void)
1950 return record_execution_dir
;
1954 init_record_ops (void)
1956 record_ops
.to_shortname
= "record";
1957 record_ops
.to_longname
= "Process record and replay target";
1959 "Log program while executing and replay execution from log.";
1960 record_ops
.to_open
= record_open
;
1961 record_ops
.to_close
= record_close
;
1962 record_ops
.to_resume
= record_resume
;
1963 record_ops
.to_wait
= record_wait
;
1964 record_ops
.to_disconnect
= record_disconnect
;
1965 record_ops
.to_detach
= record_detach
;
1966 record_ops
.to_mourn_inferior
= record_mourn_inferior
;
1967 record_ops
.to_kill
= record_kill
;
1968 record_ops
.to_create_inferior
= find_default_create_inferior
;
1969 record_ops
.to_store_registers
= record_store_registers
;
1970 record_ops
.to_xfer_partial
= record_xfer_partial
;
1971 record_ops
.to_insert_breakpoint
= record_insert_breakpoint
;
1972 record_ops
.to_remove_breakpoint
= record_remove_breakpoint
;
1973 record_ops
.to_stopped_by_watchpoint
= record_stopped_by_watchpoint
;
1974 record_ops
.to_stopped_data_address
= record_stopped_data_address
;
1975 record_ops
.to_can_execute_reverse
= record_can_execute_reverse
;
1976 record_ops
.to_stratum
= record_stratum
;
1977 /* Add bookmark target methods. */
1978 record_ops
.to_get_bookmark
= record_get_bookmark
;
1979 record_ops
.to_goto_bookmark
= record_goto_bookmark
;
1980 record_ops
.to_async
= record_async
;
1981 record_ops
.to_can_async_p
= record_can_async_p
;
1982 record_ops
.to_is_async_p
= record_is_async_p
;
1983 record_ops
.to_execution_direction
= record_execution_direction
;
1984 record_ops
.to_magic
= OPS_MAGIC
;
1987 /* "to_resume" method for prec over corefile. */
1990 record_core_resume (struct target_ops
*ops
, ptid_t ptid
, int step
,
1991 enum gdb_signal signal
)
1993 record_resume_step
= step
;
1995 record_execution_dir
= execution_direction
;
1997 /* We are about to start executing the inferior (or simulate it),
1998 let's register it with the event loop. */
1999 if (target_can_async_p ())
2001 target_async (inferior_event_handler
, 0);
2003 /* Notify the event loop there's an event to wait for. */
2004 mark_async_event_handler (record_async_inferior_event_token
);
2008 /* "to_kill" method for prec over corefile. */
2011 record_core_kill (struct target_ops
*ops
)
2014 fprintf_unfiltered (gdb_stdlog
, "Process record: record_core_kill\n");
2016 unpush_target (&record_core_ops
);
2019 /* "to_fetch_registers" method for prec over corefile. */
2022 record_core_fetch_registers (struct target_ops
*ops
,
2023 struct regcache
*regcache
,
2028 int num
= gdbarch_num_regs (get_regcache_arch (regcache
));
2031 for (i
= 0; i
< num
; i
++)
2032 regcache_raw_supply (regcache
, i
,
2033 record_core_regbuf
+ MAX_REGISTER_SIZE
* i
);
2036 regcache_raw_supply (regcache
, regno
,
2037 record_core_regbuf
+ MAX_REGISTER_SIZE
* regno
);
2040 /* "to_prepare_to_store" method for prec over corefile. */
2043 record_core_prepare_to_store (struct regcache
*regcache
)
2047 /* "to_store_registers" method for prec over corefile. */
2050 record_core_store_registers (struct target_ops
*ops
,
2051 struct regcache
*regcache
,
2054 if (record_gdb_operation_disable
)
2055 regcache_raw_collect (regcache
, regno
,
2056 record_core_regbuf
+ MAX_REGISTER_SIZE
* regno
);
2058 error (_("You can't do that without a process to debug."));
2061 /* "to_xfer_partial" method for prec over corefile. */
2064 record_core_xfer_partial (struct target_ops
*ops
, enum target_object object
,
2065 const char *annex
, gdb_byte
*readbuf
,
2066 const gdb_byte
*writebuf
, ULONGEST offset
,
2069 if (object
== TARGET_OBJECT_MEMORY
)
2071 if (record_gdb_operation_disable
|| !writebuf
)
2073 struct target_section
*p
;
2075 for (p
= record_core_start
; p
< record_core_end
; p
++)
2077 if (offset
>= p
->addr
)
2079 struct record_core_buf_entry
*entry
;
2080 ULONGEST sec_offset
;
2082 if (offset
>= p
->endaddr
)
2085 if (offset
+ len
> p
->endaddr
)
2086 len
= p
->endaddr
- offset
;
2088 sec_offset
= offset
- p
->addr
;
2090 /* Read readbuf or write writebuf p, offset, len. */
2092 if (p
->the_bfd_section
->flags
& SEC_CONSTRUCTOR
2093 || (p
->the_bfd_section
->flags
& SEC_HAS_CONTENTS
) == 0)
2096 memset (readbuf
, 0, len
);
2099 /* Get record_core_buf_entry. */
2100 for (entry
= record_core_buf_list
; entry
;
2101 entry
= entry
->prev
)
2108 /* Add a new entry. */
2109 entry
= (struct record_core_buf_entry
*)
2110 xmalloc (sizeof (struct record_core_buf_entry
));
2112 if (!bfd_malloc_and_get_section (p
->bfd
,
2119 entry
->prev
= record_core_buf_list
;
2120 record_core_buf_list
= entry
;
2123 memcpy (entry
->buf
+ sec_offset
, writebuf
,
2129 return record_beneath_to_xfer_partial
2130 (record_beneath_to_xfer_partial_ops
,
2131 object
, annex
, readbuf
, writebuf
,
2134 memcpy (readbuf
, entry
->buf
+ sec_offset
,
2145 error (_("You can't do that without a process to debug."));
2148 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops
,
2149 object
, annex
, readbuf
, writebuf
,
2153 /* "to_insert_breakpoint" method for prec over corefile. */
2156 record_core_insert_breakpoint (struct gdbarch
*gdbarch
,
2157 struct bp_target_info
*bp_tgt
)
2162 /* "to_remove_breakpoint" method for prec over corefile. */
2165 record_core_remove_breakpoint (struct gdbarch
*gdbarch
,
2166 struct bp_target_info
*bp_tgt
)
2171 /* "to_has_execution" method for prec over corefile. */
2174 record_core_has_execution (struct target_ops
*ops
, ptid_t the_ptid
)
2180 init_record_core_ops (void)
2182 record_core_ops
.to_shortname
= "record-core";
2183 record_core_ops
.to_longname
= "Process record and replay target";
2184 record_core_ops
.to_doc
=
2185 "Log program while executing and replay execution from log.";
2186 record_core_ops
.to_open
= record_open
;
2187 record_core_ops
.to_close
= record_close
;
2188 record_core_ops
.to_resume
= record_core_resume
;
2189 record_core_ops
.to_wait
= record_wait
;
2190 record_core_ops
.to_kill
= record_core_kill
;
2191 record_core_ops
.to_fetch_registers
= record_core_fetch_registers
;
2192 record_core_ops
.to_prepare_to_store
= record_core_prepare_to_store
;
2193 record_core_ops
.to_store_registers
= record_core_store_registers
;
2194 record_core_ops
.to_xfer_partial
= record_core_xfer_partial
;
2195 record_core_ops
.to_insert_breakpoint
= record_core_insert_breakpoint
;
2196 record_core_ops
.to_remove_breakpoint
= record_core_remove_breakpoint
;
2197 record_core_ops
.to_stopped_by_watchpoint
= record_stopped_by_watchpoint
;
2198 record_core_ops
.to_stopped_data_address
= record_stopped_data_address
;
2199 record_core_ops
.to_can_execute_reverse
= record_can_execute_reverse
;
2200 record_core_ops
.to_has_execution
= record_core_has_execution
;
2201 record_core_ops
.to_stratum
= record_stratum
;
2202 /* Add bookmark target methods. */
2203 record_core_ops
.to_get_bookmark
= record_get_bookmark
;
2204 record_core_ops
.to_goto_bookmark
= record_goto_bookmark
;
2205 record_core_ops
.to_async
= record_async
;
2206 record_core_ops
.to_can_async_p
= record_can_async_p
;
2207 record_core_ops
.to_is_async_p
= record_is_async_p
;
2208 record_core_ops
.to_execution_direction
= record_execution_direction
;
2209 record_core_ops
.to_magic
= OPS_MAGIC
;
2212 /* Implement "show record debug" command. */
2215 show_record_debug (struct ui_file
*file
, int from_tty
,
2216 struct cmd_list_element
*c
, const char *value
)
2218 fprintf_filtered (file
, _("Debugging of process record target is %s.\n"),
2222 /* Alias for "target record". */
2225 cmd_record_start (char *args
, int from_tty
)
2227 execute_command ("target record", from_tty
);
2230 /* Truncate the record log from the present point
2231 of replay until the end. */
2234 cmd_record_delete (char *args
, int from_tty
)
2236 if (current_target
.to_stratum
== record_stratum
)
2238 if (RECORD_IS_REPLAY
)
2240 if (!from_tty
|| query (_("Delete the log from this point forward "
2241 "and begin to record the running message "
2243 record_list_release_following (record_list
);
2246 printf_unfiltered (_("Already at end of record list.\n"));
2250 printf_unfiltered (_("Process record is not started.\n"));
2253 /* Implement the "stoprecord" or "record stop" command. */
2256 cmd_record_stop (char *args
, int from_tty
)
2258 if (current_target
.to_stratum
== record_stratum
)
2260 unpush_target (&record_ops
);
2261 printf_unfiltered (_("Process record is stopped and all execution "
2262 "logs are deleted.\n"));
2265 printf_unfiltered (_("Process record is not started.\n"));
2268 /* Set upper limit of record log size. */
2271 set_record_insn_max_num (char *args
, int from_tty
, struct cmd_list_element
*c
)
2273 if (record_insn_num
> record_insn_max_num
&& record_insn_max_num
)
2275 /* Count down record_insn_num while releasing records from list. */
2276 while (record_insn_num
> record_insn_max_num
)
2278 record_list_release_first ();
2284 static struct cmd_list_element
*record_cmdlist
, *set_record_cmdlist
,
2285 *show_record_cmdlist
, *info_record_cmdlist
;
2288 set_record_command (char *args
, int from_tty
)
2290 printf_unfiltered (_("\"set record\" must be followed "
2291 "by an apporpriate subcommand.\n"));
2292 help_list (set_record_cmdlist
, "set record ", all_commands
, gdb_stdout
);
2296 show_record_command (char *args
, int from_tty
)
2298 cmd_show_list (show_record_cmdlist
, from_tty
, "");
2301 /* Display some statistics about the execution log. */
2304 info_record_command (char *args
, int from_tty
)
2306 struct record_entry
*p
;
2308 if (current_target
.to_stratum
== record_stratum
)
2310 if (RECORD_IS_REPLAY
)
2311 printf_filtered (_("Replay mode:\n"));
2313 printf_filtered (_("Record mode:\n"));
2315 /* Find entry for first actual instruction in the log. */
2316 for (p
= record_first
.next
;
2317 p
!= NULL
&& p
->type
!= record_end
;
2321 /* Do we have a log at all? */
2322 if (p
!= NULL
&& p
->type
== record_end
)
2324 /* Display instruction number for first instruction in the log. */
2325 printf_filtered (_("Lowest recorded instruction number is %s.\n"),
2326 pulongest (p
->u
.end
.insn_num
));
2328 /* If in replay mode, display where we are in the log. */
2329 if (RECORD_IS_REPLAY
)
2330 printf_filtered (_("Current instruction number is %s.\n"),
2331 pulongest (record_list
->u
.end
.insn_num
));
2333 /* Display instruction number for last instruction in the log. */
2334 printf_filtered (_("Highest recorded instruction number is %s.\n"),
2335 pulongest (record_insn_count
));
2337 /* Display log count. */
2338 printf_filtered (_("Log contains %d instructions.\n"),
2343 printf_filtered (_("No instructions have been logged.\n"));
2348 printf_filtered (_("target record is not active.\n"));
2351 /* Display max log size. */
2352 printf_filtered (_("Max logged instructions is %d.\n"),
2353 record_insn_max_num
);
2356 /* Record log save-file format
2357 Version 1 (never released)
2360 4 bytes: magic number htonl(0x20090829).
2361 NOTE: be sure to change whenever this file format changes!
2365 1 byte: record type (record_end, see enum record_type).
2367 1 byte: record type (record_reg, see enum record_type).
2368 8 bytes: register id (network byte order).
2369 MAX_REGISTER_SIZE bytes: register value.
2371 1 byte: record type (record_mem, see enum record_type).
2372 8 bytes: memory length (network byte order).
2373 8 bytes: memory address (network byte order).
2374 n bytes: memory value (n == memory length).
2377 4 bytes: magic number netorder32(0x20091016).
2378 NOTE: be sure to change whenever this file format changes!
2382 1 byte: record type (record_end, see enum record_type).
2384 4 bytes: instruction count
2386 1 byte: record type (record_reg, see enum record_type).
2387 4 bytes: register id (network byte order).
2388 n bytes: register value (n == actual register size).
2389 (eg. 4 bytes for x86 general registers).
2391 1 byte: record type (record_mem, see enum record_type).
2392 4 bytes: memory length (network byte order).
2393 8 bytes: memory address (network byte order).
2394 n bytes: memory value (n == memory length).
2398 /* bfdcore_read -- read bytes from a core file section. */
2401 bfdcore_read (bfd
*obfd
, asection
*osec
, void *buf
, int len
, int *offset
)
2403 int ret
= bfd_get_section_contents (obfd
, osec
, buf
, *offset
, len
);
2408 error (_("Failed to read %d bytes from core file %s ('%s')."),
2409 len
, bfd_get_filename (obfd
),
2410 bfd_errmsg (bfd_get_error ()));
2413 static inline uint64_t
2414 netorder64 (uint64_t input
)
2418 store_unsigned_integer ((gdb_byte
*) &ret
, sizeof (ret
),
2419 BFD_ENDIAN_BIG
, input
);
2423 static inline uint32_t
2424 netorder32 (uint32_t input
)
2428 store_unsigned_integer ((gdb_byte
*) &ret
, sizeof (ret
),
2429 BFD_ENDIAN_BIG
, input
);
2433 static inline uint16_t
2434 netorder16 (uint16_t input
)
2438 store_unsigned_integer ((gdb_byte
*) &ret
, sizeof (ret
),
2439 BFD_ENDIAN_BIG
, input
);
2443 /* Restore the execution log from a core_bfd file. */
2445 record_restore (void)
2448 struct cleanup
*old_cleanups
;
2449 struct record_entry
*rec
;
2453 struct regcache
*regcache
;
2455 /* We restore the execution log from the open core bfd,
2457 if (core_bfd
== NULL
)
2460 /* "record_restore" can only be called when record list is empty. */
2461 gdb_assert (record_first
.next
== NULL
);
2464 fprintf_unfiltered (gdb_stdlog
, "Restoring recording from core file.\n");
2466 /* Now need to find our special note section. */
2467 osec
= bfd_get_section_by_name (core_bfd
, "null0");
2469 fprintf_unfiltered (gdb_stdlog
, "Find precord section %s.\n",
2470 osec
? "succeeded" : "failed");
2473 osec_size
= bfd_section_size (core_bfd
, osec
);
2475 fprintf_unfiltered (gdb_stdlog
, "%s", bfd_section_name (core_bfd
, osec
));
2477 /* Check the magic code. */
2478 bfdcore_read (core_bfd
, osec
, &magic
, sizeof (magic
), &bfd_offset
);
2479 if (magic
!= RECORD_FILE_MAGIC
)
2480 error (_("Version mis-match or file format error in core file %s."),
2481 bfd_get_filename (core_bfd
));
2483 fprintf_unfiltered (gdb_stdlog
,
2484 " Reading 4-byte magic cookie "
2485 "RECORD_FILE_MAGIC (0x%s)\n",
2486 phex_nz (netorder32 (magic
), 4));
2488 /* Restore the entries in recfd into record_arch_list_head and
2489 record_arch_list_tail. */
2490 record_arch_list_head
= NULL
;
2491 record_arch_list_tail
= NULL
;
2492 record_insn_num
= 0;
2493 old_cleanups
= make_cleanup (record_arch_list_cleanups
, 0);
2494 regcache
= get_current_regcache ();
2499 uint32_t regnum
, len
, signal
, count
;
2502 /* We are finished when offset reaches osec_size. */
2503 if (bfd_offset
>= osec_size
)
2505 bfdcore_read (core_bfd
, osec
, &rectype
, sizeof (rectype
), &bfd_offset
);
2509 case record_reg
: /* reg */
2510 /* Get register number to regnum. */
2511 bfdcore_read (core_bfd
, osec
, ®num
,
2512 sizeof (regnum
), &bfd_offset
);
2513 regnum
= netorder32 (regnum
);
2515 rec
= record_reg_alloc (regcache
, regnum
);
2518 bfdcore_read (core_bfd
, osec
, record_get_loc (rec
),
2519 rec
->u
.reg
.len
, &bfd_offset
);
2522 fprintf_unfiltered (gdb_stdlog
,
2523 " Reading register %d (1 "
2524 "plus %lu plus %d bytes)\n",
2526 (unsigned long) sizeof (regnum
),
2530 case record_mem
: /* mem */
2532 bfdcore_read (core_bfd
, osec
, &len
,
2533 sizeof (len
), &bfd_offset
);
2534 len
= netorder32 (len
);
2537 bfdcore_read (core_bfd
, osec
, &addr
,
2538 sizeof (addr
), &bfd_offset
);
2539 addr
= netorder64 (addr
);
2541 rec
= record_mem_alloc (addr
, len
);
2544 bfdcore_read (core_bfd
, osec
, record_get_loc (rec
),
2545 rec
->u
.mem
.len
, &bfd_offset
);
2548 fprintf_unfiltered (gdb_stdlog
,
2549 " Reading memory %s (1 plus "
2550 "%lu plus %lu plus %d bytes)\n",
2551 paddress (get_current_arch (),
2553 (unsigned long) sizeof (addr
),
2554 (unsigned long) sizeof (len
),
2558 case record_end
: /* end */
2559 rec
= record_end_alloc ();
2562 /* Get signal value. */
2563 bfdcore_read (core_bfd
, osec
, &signal
,
2564 sizeof (signal
), &bfd_offset
);
2565 signal
= netorder32 (signal
);
2566 rec
->u
.end
.sigval
= signal
;
2568 /* Get insn count. */
2569 bfdcore_read (core_bfd
, osec
, &count
,
2570 sizeof (count
), &bfd_offset
);
2571 count
= netorder32 (count
);
2572 rec
->u
.end
.insn_num
= count
;
2573 record_insn_count
= count
+ 1;
2575 fprintf_unfiltered (gdb_stdlog
,
2576 " Reading record_end (1 + "
2577 "%lu + %lu bytes), offset == %s\n",
2578 (unsigned long) sizeof (signal
),
2579 (unsigned long) sizeof (count
),
2580 paddress (get_current_arch (),
2585 error (_("Bad entry type in core file %s."),
2586 bfd_get_filename (core_bfd
));
2590 /* Add rec to record arch list. */
2591 record_arch_list_add (rec
);
2594 discard_cleanups (old_cleanups
);
2596 /* Add record_arch_list_head to the end of record list. */
2597 record_first
.next
= record_arch_list_head
;
2598 record_arch_list_head
->prev
= &record_first
;
2599 record_arch_list_tail
->next
= NULL
;
2600 record_list
= &record_first
;
2602 /* Update record_insn_max_num. */
2603 if (record_insn_num
> record_insn_max_num
)
2605 record_insn_max_num
= record_insn_num
;
2606 warning (_("Auto increase record/replay buffer limit to %d."),
2607 record_insn_max_num
);
2611 printf_filtered (_("Restored records from core file %s.\n"),
2612 bfd_get_filename (core_bfd
));
2614 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
2617 /* bfdcore_write -- write bytes into a core file section. */
2620 bfdcore_write (bfd
*obfd
, asection
*osec
, void *buf
, int len
, int *offset
)
2622 int ret
= bfd_set_section_contents (obfd
, osec
, buf
, *offset
, len
);
2627 error (_("Failed to write %d bytes to core file %s ('%s')."),
2628 len
, bfd_get_filename (obfd
),
2629 bfd_errmsg (bfd_get_error ()));
2632 /* Restore the execution log from a file. We use a modified elf
2633 corefile format, with an extra section for our data. */
2636 cmd_record_restore (char *args
, int from_tty
)
2638 core_file_command (args
, from_tty
);
2639 record_open (args
, from_tty
);
2643 record_save_cleanups (void *data
)
2646 char *pathname
= xstrdup (bfd_get_filename (obfd
));
2648 gdb_bfd_unref (obfd
);
2653 /* Save the execution log to a file. We use a modified elf corefile
2654 format, with an extra section for our data. */
2657 cmd_record_save (char *args
, int from_tty
)
2659 char *recfilename
, recfilename_buffer
[40];
2660 struct record_entry
*cur_record_list
;
2662 struct regcache
*regcache
;
2663 struct gdbarch
*gdbarch
;
2664 struct cleanup
*old_cleanups
;
2665 struct cleanup
*set_cleanups
;
2668 asection
*osec
= NULL
;
2671 if (strcmp (current_target
.to_shortname
, "record") != 0)
2672 error (_("This command can only be used with target 'record'.\n"
2673 "Use 'target record' first.\n"));
2679 /* Default recfile name is "gdb_record.PID". */
2680 snprintf (recfilename_buffer
, sizeof (recfilename_buffer
),
2681 "gdb_record.%d", PIDGET (inferior_ptid
));
2682 recfilename
= recfilename_buffer
;
2685 /* Open the save file. */
2687 fprintf_unfiltered (gdb_stdlog
, "Saving execution log to core file '%s'\n",
2690 /* Open the output file. */
2691 obfd
= create_gcore_bfd (recfilename
);
2692 old_cleanups
= make_cleanup (record_save_cleanups
, obfd
);
2694 /* Save the current record entry to "cur_record_list". */
2695 cur_record_list
= record_list
;
2697 /* Get the values of regcache and gdbarch. */
2698 regcache
= get_current_regcache ();
2699 gdbarch
= get_regcache_arch (regcache
);
2701 /* Disable the GDB operation record. */
2702 set_cleanups
= record_gdb_operation_disable_set ();
2704 /* Reverse execute to the begin of record list. */
2707 /* Check for beginning and end of log. */
2708 if (record_list
== &record_first
)
2711 record_exec_insn (regcache
, gdbarch
, record_list
);
2713 if (record_list
->prev
)
2714 record_list
= record_list
->prev
;
2717 /* Compute the size needed for the extra bfd section. */
2718 save_size
= 4; /* magic cookie */
2719 for (record_list
= record_first
.next
; record_list
;
2720 record_list
= record_list
->next
)
2721 switch (record_list
->type
)
2724 save_size
+= 1 + 4 + 4;
2727 save_size
+= 1 + 4 + record_list
->u
.reg
.len
;
2730 save_size
+= 1 + 4 + 8 + record_list
->u
.mem
.len
;
2734 /* Make the new bfd section. */
2735 osec
= bfd_make_section_anyway_with_flags (obfd
, "precord",
2739 error (_("Failed to create 'precord' section for corefile %s: %s"),
2741 bfd_errmsg (bfd_get_error ()));
2742 bfd_set_section_size (obfd
, osec
, save_size
);
2743 bfd_set_section_vma (obfd
, osec
, 0);
2744 bfd_set_section_alignment (obfd
, osec
, 0);
2745 bfd_section_lma (obfd
, osec
) = 0;
2747 /* Save corefile state. */
2748 write_gcore_file (obfd
);
2750 /* Write out the record log. */
2751 /* Write the magic code. */
2752 magic
= RECORD_FILE_MAGIC
;
2754 fprintf_unfiltered (gdb_stdlog
,
2755 " Writing 4-byte magic cookie "
2756 "RECORD_FILE_MAGIC (0x%s)\n",
2757 phex_nz (magic
, 4));
2758 bfdcore_write (obfd
, osec
, &magic
, sizeof (magic
), &bfd_offset
);
2760 /* Save the entries to recfd and forward execute to the end of
2762 record_list
= &record_first
;
2766 if (record_list
!= &record_first
)
2769 uint32_t regnum
, len
, signal
, count
;
2772 type
= record_list
->type
;
2773 bfdcore_write (obfd
, osec
, &type
, sizeof (type
), &bfd_offset
);
2775 switch (record_list
->type
)
2777 case record_reg
: /* reg */
2779 fprintf_unfiltered (gdb_stdlog
,
2780 " Writing register %d (1 "
2781 "plus %lu plus %d bytes)\n",
2782 record_list
->u
.reg
.num
,
2783 (unsigned long) sizeof (regnum
),
2784 record_list
->u
.reg
.len
);
2787 regnum
= netorder32 (record_list
->u
.reg
.num
);
2788 bfdcore_write (obfd
, osec
, ®num
,
2789 sizeof (regnum
), &bfd_offset
);
2792 bfdcore_write (obfd
, osec
, record_get_loc (record_list
),
2793 record_list
->u
.reg
.len
, &bfd_offset
);
2796 case record_mem
: /* mem */
2798 fprintf_unfiltered (gdb_stdlog
,
2799 " Writing memory %s (1 plus "
2800 "%lu plus %lu plus %d bytes)\n",
2802 record_list
->u
.mem
.addr
),
2803 (unsigned long) sizeof (addr
),
2804 (unsigned long) sizeof (len
),
2805 record_list
->u
.mem
.len
);
2808 len
= netorder32 (record_list
->u
.mem
.len
);
2809 bfdcore_write (obfd
, osec
, &len
, sizeof (len
), &bfd_offset
);
2811 /* Write memaddr. */
2812 addr
= netorder64 (record_list
->u
.mem
.addr
);
2813 bfdcore_write (obfd
, osec
, &addr
,
2814 sizeof (addr
), &bfd_offset
);
2817 bfdcore_write (obfd
, osec
, record_get_loc (record_list
),
2818 record_list
->u
.mem
.len
, &bfd_offset
);
2823 fprintf_unfiltered (gdb_stdlog
,
2824 " Writing record_end (1 + "
2825 "%lu + %lu bytes)\n",
2826 (unsigned long) sizeof (signal
),
2827 (unsigned long) sizeof (count
));
2828 /* Write signal value. */
2829 signal
= netorder32 (record_list
->u
.end
.sigval
);
2830 bfdcore_write (obfd
, osec
, &signal
,
2831 sizeof (signal
), &bfd_offset
);
2833 /* Write insn count. */
2834 count
= netorder32 (record_list
->u
.end
.insn_num
);
2835 bfdcore_write (obfd
, osec
, &count
,
2836 sizeof (count
), &bfd_offset
);
2841 /* Execute entry. */
2842 record_exec_insn (regcache
, gdbarch
, record_list
);
2844 if (record_list
->next
)
2845 record_list
= record_list
->next
;
2850 /* Reverse execute to cur_record_list. */
2853 /* Check for beginning and end of log. */
2854 if (record_list
== cur_record_list
)
2857 record_exec_insn (regcache
, gdbarch
, record_list
);
2859 if (record_list
->prev
)
2860 record_list
= record_list
->prev
;
2863 do_cleanups (set_cleanups
);
2864 gdb_bfd_unref (obfd
);
2865 discard_cleanups (old_cleanups
);
2868 printf_filtered (_("Saved core file %s with execution log.\n"),
2872 /* record_goto_insn -- rewind the record log (forward or backward,
2873 depending on DIR) to the given entry, changing the program state
2877 record_goto_insn (struct record_entry
*entry
,
2878 enum exec_direction_kind dir
)
2880 struct cleanup
*set_cleanups
= record_gdb_operation_disable_set ();
2881 struct regcache
*regcache
= get_current_regcache ();
2882 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
2884 /* Assume everything is valid: we will hit the entry,
2885 and we will not hit the end of the recording. */
2887 if (dir
== EXEC_FORWARD
)
2888 record_list
= record_list
->next
;
2892 record_exec_insn (regcache
, gdbarch
, record_list
);
2893 if (dir
== EXEC_REVERSE
)
2894 record_list
= record_list
->prev
;
2896 record_list
= record_list
->next
;
2897 } while (record_list
!= entry
);
2898 do_cleanups (set_cleanups
);
2901 /* "record goto" command. Argument is an instruction number,
2902 as given by "info record".
2904 Rewinds the recording (forward or backward) to the given instruction. */
2907 cmd_record_goto (char *arg
, int from_tty
)
2909 struct record_entry
*p
= NULL
;
2910 ULONGEST target_insn
= 0;
2912 if (arg
== NULL
|| *arg
== '\0')
2913 error (_("Command requires an argument (insn number to go to)."));
2915 if (strncmp (arg
, "start", strlen ("start")) == 0
2916 || strncmp (arg
, "begin", strlen ("begin")) == 0)
2918 /* Special case. Find first insn. */
2919 for (p
= &record_first
; p
!= NULL
; p
= p
->next
)
2920 if (p
->type
== record_end
)
2923 target_insn
= p
->u
.end
.insn_num
;
2925 else if (strncmp (arg
, "end", strlen ("end")) == 0)
2927 /* Special case. Find last insn. */
2928 for (p
= record_list
; p
->next
!= NULL
; p
= p
->next
)
2930 for (; p
!= NULL
; p
= p
->prev
)
2931 if (p
->type
== record_end
)
2934 target_insn
= p
->u
.end
.insn_num
;
2938 /* General case. Find designated insn. */
2939 target_insn
= parse_and_eval_long (arg
);
2941 for (p
= &record_first
; p
!= NULL
; p
= p
->next
)
2942 if (p
->type
== record_end
&& p
->u
.end
.insn_num
== target_insn
)
2947 error (_("Target insn '%s' not found."), arg
);
2948 else if (p
== record_list
)
2949 error (_("Already at insn '%s'."), arg
);
2950 else if (p
->u
.end
.insn_num
> record_list
->u
.end
.insn_num
)
2952 printf_filtered (_("Go forward to insn number %s\n"),
2953 pulongest (target_insn
));
2954 record_goto_insn (p
, EXEC_FORWARD
);
2958 printf_filtered (_("Go backward to insn number %s\n"),
2959 pulongest (target_insn
));
2960 record_goto_insn (p
, EXEC_REVERSE
);
2962 registers_changed ();
2963 reinit_frame_cache ();
2964 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
2967 /* Provide a prototype to silence -Wmissing-prototypes. */
2968 extern initialize_file_ftype _initialize_record
;
2971 _initialize_record (void)
2973 struct cmd_list_element
*c
;
2975 /* Init record_first. */
2976 record_first
.prev
= NULL
;
2977 record_first
.next
= NULL
;
2978 record_first
.type
= record_end
;
2981 add_target (&record_ops
);
2982 init_record_core_ops ();
2983 add_target (&record_core_ops
);
2985 add_setshow_zinteger_cmd ("record", no_class
, &record_debug
,
2986 _("Set debugging of record/replay feature."),
2987 _("Show debugging of record/replay feature."),
2988 _("When enabled, debugging output for "
2989 "record/replay feature is displayed."),
2990 NULL
, show_record_debug
, &setdebuglist
,
2993 c
= add_prefix_cmd ("record", class_obscure
, cmd_record_start
,
2994 _("Abbreviated form of \"target record\" command."),
2995 &record_cmdlist
, "record ", 0, &cmdlist
);
2996 set_cmd_completer (c
, filename_completer
);
2998 add_com_alias ("rec", "record", class_obscure
, 1);
2999 add_prefix_cmd ("record", class_support
, set_record_command
,
3000 _("Set record options"), &set_record_cmdlist
,
3001 "set record ", 0, &setlist
);
3002 add_alias_cmd ("rec", "record", class_obscure
, 1, &setlist
);
3003 add_prefix_cmd ("record", class_support
, show_record_command
,
3004 _("Show record options"), &show_record_cmdlist
,
3005 "show record ", 0, &showlist
);
3006 add_alias_cmd ("rec", "record", class_obscure
, 1, &showlist
);
3007 add_prefix_cmd ("record", class_support
, info_record_command
,
3008 _("Info record options"), &info_record_cmdlist
,
3009 "info record ", 0, &infolist
);
3010 add_alias_cmd ("rec", "record", class_obscure
, 1, &infolist
);
3012 c
= add_cmd ("save", class_obscure
, cmd_record_save
,
3013 _("Save the execution log to a file.\n\
3014 Argument is optional filename.\n\
3015 Default filename is 'gdb_record.<process_id>'."),
3017 set_cmd_completer (c
, filename_completer
);
3019 c
= add_cmd ("restore", class_obscure
, cmd_record_restore
,
3020 _("Restore the execution log from a file.\n\
3021 Argument is filename. File must be created with 'record save'."),
3023 set_cmd_completer (c
, filename_completer
);
3025 add_cmd ("delete", class_obscure
, cmd_record_delete
,
3026 _("Delete the rest of execution log and start recording it anew."),
3028 add_alias_cmd ("d", "delete", class_obscure
, 1, &record_cmdlist
);
3029 add_alias_cmd ("del", "delete", class_obscure
, 1, &record_cmdlist
);
3031 add_cmd ("stop", class_obscure
, cmd_record_stop
,
3032 _("Stop the record/replay target."),
3034 add_alias_cmd ("s", "stop", class_obscure
, 1, &record_cmdlist
);
3036 /* Record instructions number limit command. */
3037 add_setshow_boolean_cmd ("stop-at-limit", no_class
,
3038 &record_stop_at_limit
, _("\
3039 Set whether record/replay stops when record/replay buffer becomes full."), _("\
3040 Show whether record/replay stops when record/replay buffer becomes full."),
3041 _("Default is ON.\n\
3042 When ON, if the record/replay buffer becomes full, ask user what to do.\n\
3043 When OFF, if the record/replay buffer becomes full,\n\
3044 delete the oldest recorded instruction to make room for each new one."),
3046 &set_record_cmdlist
, &show_record_cmdlist
);
3047 add_setshow_uinteger_cmd ("insn-number-max", no_class
,
3048 &record_insn_max_num
,
3049 _("Set record/replay buffer limit."),
3050 _("Show record/replay buffer limit."), _("\
3051 Set the maximum number of instructions to be stored in the\n\
3052 record/replay buffer. Zero means unlimited. Default is 200000."),
3053 set_record_insn_max_num
,
3054 NULL
, &set_record_cmdlist
, &show_record_cmdlist
);
3056 add_cmd ("goto", class_obscure
, cmd_record_goto
, _("\
3057 Restore the program to its state at instruction number N.\n\
3058 Argument is instruction number, as shown by 'info record'."),
3061 add_setshow_boolean_cmd ("memory-query", no_class
,
3062 &record_memory_query
, _("\
3063 Set whether query if PREC cannot record memory change of next instruction."),
3065 Show whether query if PREC cannot record memory change of next instruction."),
3068 When ON, query if PREC cannot record memory change of next instruction."),
3070 &set_record_cmdlist
, &show_record_cmdlist
);