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
);
488 /* Record the value of a region of memory whose address is ADDR and
489 length is LEN to record_arch_list. */
492 record_arch_list_add_mem (CORE_ADDR addr
, int len
)
494 struct record_entry
*rec
;
496 if (record_debug
> 1)
497 fprintf_unfiltered (gdb_stdlog
,
498 "Process record: add mem addr = %s len = %d to "
500 paddress (target_gdbarch
, addr
), len
);
502 if (!addr
) /* FIXME: Why? Some arch must permit it... */
505 rec
= record_mem_alloc (addr
, len
);
507 if (target_read_memory (addr
, record_get_loc (rec
), len
))
510 fprintf_unfiltered (gdb_stdlog
,
511 "Process record: error reading memory at "
512 "addr = %s len = %d.\n",
513 paddress (target_gdbarch
, addr
), len
);
514 record_mem_release (rec
);
518 record_arch_list_add (rec
);
523 /* Add a record_end type struct record_entry to record_arch_list. */
526 record_arch_list_add_end (void)
528 struct record_entry
*rec
;
530 if (record_debug
> 1)
531 fprintf_unfiltered (gdb_stdlog
,
532 "Process record: add end to arch list.\n");
534 rec
= record_end_alloc ();
535 rec
->u
.end
.sigval
= GDB_SIGNAL_0
;
536 rec
->u
.end
.insn_num
= ++record_insn_count
;
538 record_arch_list_add (rec
);
544 record_check_insn_num (int set_terminal
)
546 if (record_insn_max_num
)
548 gdb_assert (record_insn_num
<= record_insn_max_num
);
549 if (record_insn_num
== record_insn_max_num
)
551 /* Ask user what to do. */
552 if (record_stop_at_limit
)
557 target_terminal_ours ();
558 q
= yquery (_("Do you want to auto delete previous execution "
559 "log entries when record/replay buffer becomes "
560 "full (record stop-at-limit)?"));
562 target_terminal_inferior ();
564 record_stop_at_limit
= 0;
566 error (_("Process record: stopped by user."));
573 record_arch_list_cleanups (void *ignore
)
575 record_list_release (record_arch_list_tail
);
578 /* Before inferior step (when GDB record the running message, inferior
579 only can step), GDB will call this function to record the values to
580 record_list. This function will call gdbarch_process_record to
581 record the running message of inferior and set them to
582 record_arch_list, and add it to record_list. */
585 record_message (struct regcache
*regcache
, enum gdb_signal signal
)
588 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
589 struct cleanup
*old_cleanups
= make_cleanup (record_arch_list_cleanups
, 0);
591 record_arch_list_head
= NULL
;
592 record_arch_list_tail
= NULL
;
594 /* Check record_insn_num. */
595 record_check_insn_num (1);
597 /* If gdb sends a signal value to target_resume,
598 save it in the 'end' field of the previous instruction.
600 Maybe process record should record what really happened,
601 rather than what gdb pretends has happened.
603 So if Linux delivered the signal to the child process during
604 the record mode, we will record it and deliver it again in
607 If user says "ignore this signal" during the record mode, then
608 it will be ignored again during the replay mode (no matter if
609 the user says something different, like "deliver this signal"
610 during the replay mode).
612 User should understand that nothing he does during the replay
613 mode will change the behavior of the child. If he tries,
614 then that is a user error.
616 But we should still deliver the signal to gdb during the replay,
617 if we delivered it during the recording. Therefore we should
618 record the signal during record_wait, not record_resume. */
619 if (record_list
!= &record_first
) /* FIXME better way to check */
621 gdb_assert (record_list
->type
== record_end
);
622 record_list
->u
.end
.sigval
= signal
;
625 if (signal
== GDB_SIGNAL_0
626 || !gdbarch_process_record_signal_p (gdbarch
))
627 ret
= gdbarch_process_record (gdbarch
,
629 regcache_read_pc (regcache
));
631 ret
= gdbarch_process_record_signal (gdbarch
,
636 error (_("Process record: inferior program stopped."));
638 error (_("Process record: failed to record execution log."));
640 discard_cleanups (old_cleanups
);
642 record_list
->next
= record_arch_list_head
;
643 record_arch_list_head
->prev
= record_list
;
644 record_list
= record_arch_list_tail
;
646 if (record_insn_num
== record_insn_max_num
&& record_insn_max_num
)
647 record_list_release_first ();
654 struct record_message_args
{
655 struct regcache
*regcache
;
656 enum gdb_signal signal
;
660 record_message_wrapper (void *args
)
662 struct record_message_args
*record_args
= args
;
664 return record_message (record_args
->regcache
, record_args
->signal
);
668 record_message_wrapper_safe (struct regcache
*regcache
,
669 enum gdb_signal signal
)
671 struct record_message_args args
;
673 args
.regcache
= regcache
;
674 args
.signal
= signal
;
676 return catch_errors (record_message_wrapper
, &args
, NULL
, RETURN_MASK_ALL
);
679 /* Set to 1 if record_store_registers and record_xfer_partial
680 doesn't need record. */
682 static int record_gdb_operation_disable
= 0;
685 record_gdb_operation_disable_set (void)
687 struct cleanup
*old_cleanups
= NULL
;
690 make_cleanup_restore_integer (&record_gdb_operation_disable
);
691 record_gdb_operation_disable
= 1;
696 /* Flag set to TRUE for target_stopped_by_watchpoint. */
697 static int record_hw_watchpoint
= 0;
699 /* Execute one instruction from the record log. Each instruction in
700 the log will be represented by an arbitrary sequence of register
701 entries and memory entries, followed by an 'end' entry. */
704 record_exec_insn (struct regcache
*regcache
, struct gdbarch
*gdbarch
,
705 struct record_entry
*entry
)
709 case record_reg
: /* reg */
711 gdb_byte reg
[MAX_REGISTER_SIZE
];
713 if (record_debug
> 1)
714 fprintf_unfiltered (gdb_stdlog
,
715 "Process record: record_reg %s to "
716 "inferior num = %d.\n",
717 host_address_to_string (entry
),
720 regcache_cooked_read (regcache
, entry
->u
.reg
.num
, reg
);
721 regcache_cooked_write (regcache
, entry
->u
.reg
.num
,
722 record_get_loc (entry
));
723 memcpy (record_get_loc (entry
), reg
, entry
->u
.reg
.len
);
727 case record_mem
: /* mem */
729 /* Nothing to do if the entry is flagged not_accessible. */
730 if (!entry
->u
.mem
.mem_entry_not_accessible
)
732 gdb_byte
*mem
= alloca (entry
->u
.mem
.len
);
734 if (record_debug
> 1)
735 fprintf_unfiltered (gdb_stdlog
,
736 "Process record: record_mem %s to "
737 "inferior addr = %s len = %d.\n",
738 host_address_to_string (entry
),
739 paddress (gdbarch
, entry
->u
.mem
.addr
),
742 if (target_read_memory (entry
->u
.mem
.addr
, mem
, entry
->u
.mem
.len
))
744 entry
->u
.mem
.mem_entry_not_accessible
= 1;
746 warning (_("Process record: error reading memory at "
747 "addr = %s len = %d."),
748 paddress (gdbarch
, entry
->u
.mem
.addr
),
753 if (target_write_memory (entry
->u
.mem
.addr
,
754 record_get_loc (entry
),
757 entry
->u
.mem
.mem_entry_not_accessible
= 1;
759 warning (_("Process record: error writing memory at "
760 "addr = %s len = %d."),
761 paddress (gdbarch
, entry
->u
.mem
.addr
),
766 memcpy (record_get_loc (entry
), mem
, entry
->u
.mem
.len
);
768 /* We've changed memory --- check if a hardware
769 watchpoint should trap. Note that this
770 presently assumes the target beneath supports
771 continuable watchpoints. On non-continuable
772 watchpoints target, we'll want to check this
773 _before_ actually doing the memory change, and
774 not doing the change at all if the watchpoint
776 if (hardware_watchpoint_inserted_in_range
777 (get_regcache_aspace (regcache
),
778 entry
->u
.mem
.addr
, entry
->u
.mem
.len
))
779 record_hw_watchpoint
= 1;
788 static struct target_ops
*tmp_to_resume_ops
;
789 static void (*tmp_to_resume
) (struct target_ops
*, ptid_t
, int,
791 static struct target_ops
*tmp_to_wait_ops
;
792 static ptid_t (*tmp_to_wait
) (struct target_ops
*, ptid_t
,
793 struct target_waitstatus
*,
795 static struct target_ops
*tmp_to_store_registers_ops
;
796 static void (*tmp_to_store_registers
) (struct target_ops
*,
799 static struct target_ops
*tmp_to_xfer_partial_ops
;
800 static LONGEST (*tmp_to_xfer_partial
) (struct target_ops
*ops
,
801 enum target_object object
,
804 const gdb_byte
*writebuf
,
807 static int (*tmp_to_insert_breakpoint
) (struct gdbarch
*,
808 struct bp_target_info
*);
809 static int (*tmp_to_remove_breakpoint
) (struct gdbarch
*,
810 struct bp_target_info
*);
811 static int (*tmp_to_stopped_by_watchpoint
) (void);
812 static int (*tmp_to_stopped_data_address
) (struct target_ops
*, CORE_ADDR
*);
813 static int (*tmp_to_stopped_data_address
) (struct target_ops
*, CORE_ADDR
*);
814 static void (*tmp_to_async
) (void (*) (enum inferior_event_type
, void *), void *);
816 static void record_restore (void);
818 /* Asynchronous signal handle registered as event loop source for when
819 we have pending events ready to be passed to the core. */
821 static struct async_event_handler
*record_async_inferior_event_token
;
824 record_async_inferior_event_handler (gdb_client_data data
)
826 inferior_event_handler (INF_REG_EVENT
, NULL
);
829 /* Open the process record target. */
832 record_core_open_1 (char *name
, int from_tty
)
834 struct regcache
*regcache
= get_current_regcache ();
835 int regnum
= gdbarch_num_regs (get_regcache_arch (regcache
));
838 /* Get record_core_regbuf. */
839 target_fetch_registers (regcache
, -1);
840 record_core_regbuf
= xmalloc (MAX_REGISTER_SIZE
* regnum
);
841 for (i
= 0; i
< regnum
; i
++)
842 regcache_raw_collect (regcache
, i
,
843 record_core_regbuf
+ MAX_REGISTER_SIZE
* i
);
845 /* Get record_core_start and record_core_end. */
846 if (build_section_table (core_bfd
, &record_core_start
, &record_core_end
))
848 xfree (record_core_regbuf
);
849 record_core_regbuf
= NULL
;
850 error (_("\"%s\": Can't find sections: %s"),
851 bfd_get_filename (core_bfd
), bfd_errmsg (bfd_get_error ()));
854 push_target (&record_core_ops
);
858 /* "to_open" target method for 'live' processes. */
861 record_open_1 (char *name
, int from_tty
)
864 fprintf_unfiltered (gdb_stdlog
, "Process record: record_open\n");
867 if (!target_has_execution
)
868 error (_("Process record: the program is not being run."));
870 error (_("Process record target can't debug inferior in non-stop mode "
873 if (!gdbarch_process_record_p (target_gdbarch
))
874 error (_("Process record: the current architecture doesn't support "
875 "record function."));
878 error (_("Could not find 'to_resume' method on the target stack."));
880 error (_("Could not find 'to_wait' method on the target stack."));
881 if (!tmp_to_store_registers
)
882 error (_("Could not find 'to_store_registers' "
883 "method on the target stack."));
884 if (!tmp_to_insert_breakpoint
)
885 error (_("Could not find 'to_insert_breakpoint' "
886 "method on the target stack."));
887 if (!tmp_to_remove_breakpoint
)
888 error (_("Could not find 'to_remove_breakpoint' "
889 "method on the target stack."));
890 if (!tmp_to_stopped_by_watchpoint
)
891 error (_("Could not find 'to_stopped_by_watchpoint' "
892 "method on the target stack."));
893 if (!tmp_to_stopped_data_address
)
894 error (_("Could not find 'to_stopped_data_address' "
895 "method on the target stack."));
897 push_target (&record_ops
);
900 static void record_init_record_breakpoints (void);
902 /* "to_open" target method. Open the process record target. */
905 record_open (char *name
, int from_tty
)
907 struct target_ops
*t
;
910 fprintf_unfiltered (gdb_stdlog
, "Process record: record_open\n");
912 /* Check if record target is already running. */
913 if (current_target
.to_stratum
== record_stratum
)
914 error (_("Process record target already running. Use \"record stop\" to "
915 "stop record target first."));
917 /* Reset the tmp beneath pointers. */
918 tmp_to_resume_ops
= NULL
;
919 tmp_to_resume
= NULL
;
920 tmp_to_wait_ops
= NULL
;
922 tmp_to_store_registers_ops
= NULL
;
923 tmp_to_store_registers
= NULL
;
924 tmp_to_xfer_partial_ops
= NULL
;
925 tmp_to_xfer_partial
= NULL
;
926 tmp_to_insert_breakpoint
= NULL
;
927 tmp_to_remove_breakpoint
= NULL
;
928 tmp_to_stopped_by_watchpoint
= NULL
;
929 tmp_to_stopped_data_address
= NULL
;
932 /* Set the beneath function pointers. */
933 for (t
= current_target
.beneath
; t
!= NULL
; t
= t
->beneath
)
937 tmp_to_resume
= t
->to_resume
;
938 tmp_to_resume_ops
= t
;
942 tmp_to_wait
= t
->to_wait
;
945 if (!tmp_to_store_registers
)
947 tmp_to_store_registers
= t
->to_store_registers
;
948 tmp_to_store_registers_ops
= t
;
950 if (!tmp_to_xfer_partial
)
952 tmp_to_xfer_partial
= t
->to_xfer_partial
;
953 tmp_to_xfer_partial_ops
= t
;
955 if (!tmp_to_insert_breakpoint
)
956 tmp_to_insert_breakpoint
= t
->to_insert_breakpoint
;
957 if (!tmp_to_remove_breakpoint
)
958 tmp_to_remove_breakpoint
= t
->to_remove_breakpoint
;
959 if (!tmp_to_stopped_by_watchpoint
)
960 tmp_to_stopped_by_watchpoint
= t
->to_stopped_by_watchpoint
;
961 if (!tmp_to_stopped_data_address
)
962 tmp_to_stopped_data_address
= t
->to_stopped_data_address
;
964 tmp_to_async
= t
->to_async
;
966 if (!tmp_to_xfer_partial
)
967 error (_("Could not find 'to_xfer_partial' method on the target stack."));
971 record_insn_count
= 0;
972 record_list
= &record_first
;
973 record_list
->next
= NULL
;
975 /* Set the tmp beneath pointers to beneath pointers. */
976 record_beneath_to_resume_ops
= tmp_to_resume_ops
;
977 record_beneath_to_resume
= tmp_to_resume
;
978 record_beneath_to_wait_ops
= tmp_to_wait_ops
;
979 record_beneath_to_wait
= tmp_to_wait
;
980 record_beneath_to_store_registers_ops
= tmp_to_store_registers_ops
;
981 record_beneath_to_store_registers
= tmp_to_store_registers
;
982 record_beneath_to_xfer_partial_ops
= tmp_to_xfer_partial_ops
;
983 record_beneath_to_xfer_partial
= tmp_to_xfer_partial
;
984 record_beneath_to_insert_breakpoint
= tmp_to_insert_breakpoint
;
985 record_beneath_to_remove_breakpoint
= tmp_to_remove_breakpoint
;
986 record_beneath_to_stopped_by_watchpoint
= tmp_to_stopped_by_watchpoint
;
987 record_beneath_to_stopped_data_address
= tmp_to_stopped_data_address
;
988 record_beneath_to_async
= tmp_to_async
;
991 record_core_open_1 (name
, from_tty
);
993 record_open_1 (name
, from_tty
);
995 /* Register extra event sources in the event loop. */
996 record_async_inferior_event_token
997 = create_async_event_handler (record_async_inferior_event_handler
,
1000 record_init_record_breakpoints ();
1003 /* "to_close" target method. Close the process record target. */
1006 record_close (int quitting
)
1008 struct record_core_buf_entry
*entry
;
1011 fprintf_unfiltered (gdb_stdlog
, "Process record: record_close\n");
1013 record_list_release (record_list
);
1015 /* Release record_core_regbuf. */
1016 if (record_core_regbuf
)
1018 xfree (record_core_regbuf
);
1019 record_core_regbuf
= NULL
;
1022 /* Release record_core_buf_list. */
1023 if (record_core_buf_list
)
1025 for (entry
= record_core_buf_list
->prev
; entry
; entry
= entry
->prev
)
1027 xfree (record_core_buf_list
);
1028 record_core_buf_list
= entry
;
1030 record_core_buf_list
= NULL
;
1033 if (record_async_inferior_event_token
)
1034 delete_async_event_handler (&record_async_inferior_event_token
);
1037 static int record_resume_step
= 0;
1039 /* True if we've been resumed, and so each record_wait call should
1040 advance execution. If this is false, record_wait will return a
1041 TARGET_WAITKIND_IGNORE. */
1042 static int record_resumed
= 0;
1044 /* The execution direction of the last resume we got. This is
1045 necessary for async mode. Vis (order is not strictly accurate):
1047 1. user has the global execution direction set to forward
1048 2. user does a reverse-step command
1049 3. record_resume is called with global execution direction
1050 temporarily switched to reverse
1051 4. GDB's execution direction is reverted back to forward
1052 5. target record notifies event loop there's an event to handle
1053 6. infrun asks the target which direction was it going, and switches
1054 the global execution direction accordingly (to reverse)
1055 7. infrun polls an event out of the record target, and handles it
1056 8. GDB goes back to the event loop, and goto #4.
1058 static enum exec_direction_kind record_execution_dir
= EXEC_FORWARD
;
1060 /* "to_resume" target method. Resume the process record target. */
1063 record_resume (struct target_ops
*ops
, ptid_t ptid
, int step
,
1064 enum gdb_signal signal
)
1066 record_resume_step
= step
;
1068 record_execution_dir
= execution_direction
;
1070 if (!RECORD_IS_REPLAY
)
1072 struct gdbarch
*gdbarch
= target_thread_architecture (ptid
);
1074 record_message (get_current_regcache (), signal
);
1078 /* This is not hard single step. */
1079 if (!gdbarch_software_single_step_p (gdbarch
))
1081 /* This is a normal continue. */
1086 /* This arch support soft sigle step. */
1087 if (single_step_breakpoints_inserted ())
1089 /* This is a soft single step. */
1090 record_resume_step
= 1;
1094 /* This is a continue.
1095 Try to insert a soft single step breakpoint. */
1096 if (!gdbarch_software_single_step (gdbarch
,
1097 get_current_frame ()))
1099 /* This system don't want use soft single step.
1100 Use hard sigle step. */
1107 record_beneath_to_resume (record_beneath_to_resume_ops
,
1108 ptid
, step
, signal
);
1111 /* We are about to start executing the inferior (or simulate it),
1112 let's register it with the event loop. */
1113 if (target_can_async_p ())
1115 target_async (inferior_event_handler
, 0);
1116 /* Notify the event loop there's an event to wait for. We do
1117 most of the work in record_wait. */
1118 mark_async_event_handler (record_async_inferior_event_token
);
1122 static int record_get_sig
= 0;
1124 /* SIGINT signal handler, registered by "to_wait" method. */
1127 record_sig_handler (int signo
)
1130 fprintf_unfiltered (gdb_stdlog
, "Process record: get a signal\n");
1132 /* It will break the running inferior in replay mode. */
1133 record_resume_step
= 1;
1135 /* It will let record_wait set inferior status to get the signal
1141 record_wait_cleanups (void *ignore
)
1143 if (execution_direction
== EXEC_REVERSE
)
1145 if (record_list
->next
)
1146 record_list
= record_list
->next
;
1149 record_list
= record_list
->prev
;
1152 /* "to_wait" target method for process record target.
1154 In record mode, the target is always run in singlestep mode
1155 (even when gdb says to continue). The to_wait method intercepts
1156 the stop events and determines which ones are to be passed on to
1157 gdb. Most stop events are just singlestep events that gdb is not
1158 to know about, so the to_wait method just records them and keeps
1161 In replay mode, this function emulates the recorded execution log,
1162 one instruction at a time (forward or backward), and determines
1166 record_wait_1 (struct target_ops
*ops
,
1167 ptid_t ptid
, struct target_waitstatus
*status
,
1170 struct cleanup
*set_cleanups
= record_gdb_operation_disable_set ();
1173 fprintf_unfiltered (gdb_stdlog
,
1174 "Process record: record_wait "
1175 "record_resume_step = %d, record_resumed = %d, direction=%s\n",
1176 record_resume_step
, record_resumed
,
1177 record_execution_dir
== EXEC_FORWARD
? "forward" : "reverse");
1179 if (!record_resumed
)
1181 gdb_assert ((options
& TARGET_WNOHANG
) != 0);
1183 /* No interesting event. */
1184 status
->kind
= TARGET_WAITKIND_IGNORE
;
1185 return minus_one_ptid
;
1189 signal (SIGINT
, record_sig_handler
);
1191 if (!RECORD_IS_REPLAY
&& ops
!= &record_core_ops
)
1193 if (record_resume_step
)
1195 /* This is a single step. */
1196 return record_beneath_to_wait (record_beneath_to_wait_ops
,
1197 ptid
, status
, options
);
1201 /* This is not a single step. */
1204 struct gdbarch
*gdbarch
= target_thread_architecture (inferior_ptid
);
1208 ret
= record_beneath_to_wait (record_beneath_to_wait_ops
,
1209 ptid
, status
, options
);
1210 if (status
->kind
== TARGET_WAITKIND_IGNORE
)
1213 fprintf_unfiltered (gdb_stdlog
,
1214 "Process record: record_wait "
1215 "target beneath not done yet\n");
1219 if (single_step_breakpoints_inserted ())
1220 remove_single_step_breakpoints ();
1222 if (record_resume_step
)
1225 /* Is this a SIGTRAP? */
1226 if (status
->kind
== TARGET_WAITKIND_STOPPED
1227 && status
->value
.sig
== GDB_SIGNAL_TRAP
)
1229 struct regcache
*regcache
;
1230 struct address_space
*aspace
;
1232 /* Yes -- this is likely our single-step finishing,
1233 but check if there's any reason the core would be
1234 interested in the event. */
1236 registers_changed ();
1237 regcache
= get_current_regcache ();
1238 tmp_pc
= regcache_read_pc (regcache
);
1239 aspace
= get_regcache_aspace (regcache
);
1241 if (target_stopped_by_watchpoint ())
1243 /* Always interested in watchpoints. */
1245 else if (breakpoint_inserted_here_p (aspace
, tmp_pc
))
1247 /* There is a breakpoint here. Let the core
1249 if (software_breakpoint_inserted_here_p (aspace
, tmp_pc
))
1251 struct gdbarch
*gdbarch
1252 = get_regcache_arch (regcache
);
1253 CORE_ADDR decr_pc_after_break
1254 = gdbarch_decr_pc_after_break (gdbarch
);
1255 if (decr_pc_after_break
)
1256 regcache_write_pc (regcache
,
1257 tmp_pc
+ decr_pc_after_break
);
1262 /* This is a single-step trap. Record the
1263 insn and issue another step.
1264 FIXME: this part can be a random SIGTRAP too.
1265 But GDB cannot handle it. */
1268 if (!record_message_wrapper_safe (regcache
,
1271 status
->kind
= TARGET_WAITKIND_STOPPED
;
1272 status
->value
.sig
= GDB_SIGNAL_0
;
1276 if (gdbarch_software_single_step_p (gdbarch
))
1278 /* Try to insert the software single step breakpoint.
1279 If insert success, set step to 0. */
1280 set_executing (inferior_ptid
, 0);
1281 reinit_frame_cache ();
1282 if (gdbarch_software_single_step (gdbarch
,
1283 get_current_frame ()))
1285 set_executing (inferior_ptid
, 1);
1289 fprintf_unfiltered (gdb_stdlog
,
1290 "Process record: record_wait "
1291 "issuing one more step in the target beneath\n");
1292 record_beneath_to_resume (record_beneath_to_resume_ops
,
1299 /* The inferior is broken by a breakpoint or a signal. */
1308 struct regcache
*regcache
= get_current_regcache ();
1309 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1310 struct address_space
*aspace
= get_regcache_aspace (regcache
);
1311 int continue_flag
= 1;
1312 int first_record_end
= 1;
1313 struct cleanup
*old_cleanups
= make_cleanup (record_wait_cleanups
, 0);
1316 record_hw_watchpoint
= 0;
1317 status
->kind
= TARGET_WAITKIND_STOPPED
;
1319 /* Check breakpoint when forward execute. */
1320 if (execution_direction
== EXEC_FORWARD
)
1322 tmp_pc
= regcache_read_pc (regcache
);
1323 if (breakpoint_inserted_here_p (aspace
, tmp_pc
))
1325 int decr_pc_after_break
= gdbarch_decr_pc_after_break (gdbarch
);
1328 fprintf_unfiltered (gdb_stdlog
,
1329 "Process record: break at %s.\n",
1330 paddress (gdbarch
, tmp_pc
));
1332 if (decr_pc_after_break
1333 && !record_resume_step
1334 && software_breakpoint_inserted_here_p (aspace
, tmp_pc
))
1335 regcache_write_pc (regcache
,
1336 tmp_pc
+ decr_pc_after_break
);
1341 /* If GDB is in terminal_inferior mode, it will not get the signal.
1342 And in GDB replay mode, GDB doesn't need to be in terminal_inferior
1343 mode, because inferior will not executed.
1344 Then set it to terminal_ours to make GDB get the signal. */
1345 target_terminal_ours ();
1347 /* In EXEC_FORWARD mode, record_list points to the tail of prev
1349 if (execution_direction
== EXEC_FORWARD
&& record_list
->next
)
1350 record_list
= record_list
->next
;
1352 /* Loop over the record_list, looking for the next place to
1356 /* Check for beginning and end of log. */
1357 if (execution_direction
== EXEC_REVERSE
1358 && record_list
== &record_first
)
1360 /* Hit beginning of record log in reverse. */
1361 status
->kind
= TARGET_WAITKIND_NO_HISTORY
;
1364 if (execution_direction
!= EXEC_REVERSE
&& !record_list
->next
)
1366 /* Hit end of record log going forward. */
1367 status
->kind
= TARGET_WAITKIND_NO_HISTORY
;
1371 record_exec_insn (regcache
, gdbarch
, record_list
);
1373 if (record_list
->type
== record_end
)
1375 if (record_debug
> 1)
1376 fprintf_unfiltered (gdb_stdlog
,
1377 "Process record: record_end %s to "
1379 host_address_to_string (record_list
));
1381 if (first_record_end
&& execution_direction
== EXEC_REVERSE
)
1383 /* When reverse excute, the first record_end is the part of
1384 current instruction. */
1385 first_record_end
= 0;
1389 /* In EXEC_REVERSE mode, this is the record_end of prev
1391 In EXEC_FORWARD mode, this is the record_end of current
1394 if (record_resume_step
)
1396 if (record_debug
> 1)
1397 fprintf_unfiltered (gdb_stdlog
,
1398 "Process record: step.\n");
1402 /* check breakpoint */
1403 tmp_pc
= regcache_read_pc (regcache
);
1404 if (breakpoint_inserted_here_p (aspace
, tmp_pc
))
1406 int decr_pc_after_break
1407 = gdbarch_decr_pc_after_break (gdbarch
);
1410 fprintf_unfiltered (gdb_stdlog
,
1411 "Process record: break "
1413 paddress (gdbarch
, tmp_pc
));
1414 if (decr_pc_after_break
1415 && execution_direction
== EXEC_FORWARD
1416 && !record_resume_step
1417 && software_breakpoint_inserted_here_p (aspace
,
1419 regcache_write_pc (regcache
,
1420 tmp_pc
+ decr_pc_after_break
);
1424 if (record_hw_watchpoint
)
1427 fprintf_unfiltered (gdb_stdlog
,
1428 "Process record: hit hw "
1432 /* Check target signal */
1433 if (record_list
->u
.end
.sigval
!= GDB_SIGNAL_0
)
1434 /* FIXME: better way to check */
1441 if (execution_direction
== EXEC_REVERSE
)
1443 if (record_list
->prev
)
1444 record_list
= record_list
->prev
;
1448 if (record_list
->next
)
1449 record_list
= record_list
->next
;
1453 while (continue_flag
);
1457 status
->value
.sig
= GDB_SIGNAL_INT
;
1458 else if (record_list
->u
.end
.sigval
!= GDB_SIGNAL_0
)
1459 /* FIXME: better way to check */
1460 status
->value
.sig
= record_list
->u
.end
.sigval
;
1462 status
->value
.sig
= GDB_SIGNAL_TRAP
;
1464 discard_cleanups (old_cleanups
);
1467 signal (SIGINT
, handle_sigint
);
1469 do_cleanups (set_cleanups
);
1470 return inferior_ptid
;
1474 record_wait (struct target_ops
*ops
,
1475 ptid_t ptid
, struct target_waitstatus
*status
,
1480 return_ptid
= record_wait_1 (ops
, ptid
, status
, options
);
1481 if (status
->kind
!= TARGET_WAITKIND_IGNORE
)
1483 /* We're reporting a stop. Make sure any spurious
1484 target_wait(WNOHANG) doesn't advance the target until the
1485 core wants us resumed again. */
1492 record_stopped_by_watchpoint (void)
1494 if (RECORD_IS_REPLAY
)
1495 return record_hw_watchpoint
;
1497 return record_beneath_to_stopped_by_watchpoint ();
1501 record_stopped_data_address (struct target_ops
*ops
, CORE_ADDR
*addr_p
)
1503 if (RECORD_IS_REPLAY
)
1506 return record_beneath_to_stopped_data_address (ops
, addr_p
);
1509 /* "to_disconnect" method for process record target. */
1512 record_disconnect (struct target_ops
*target
, char *args
, int from_tty
)
1515 fprintf_unfiltered (gdb_stdlog
, "Process record: record_disconnect\n");
1517 unpush_target (&record_ops
);
1518 target_disconnect (args
, from_tty
);
1521 /* "to_detach" method for process record target. */
1524 record_detach (struct target_ops
*ops
, char *args
, int from_tty
)
1527 fprintf_unfiltered (gdb_stdlog
, "Process record: record_detach\n");
1529 unpush_target (&record_ops
);
1530 target_detach (args
, from_tty
);
1533 /* "to_mourn_inferior" method for process record target. */
1536 record_mourn_inferior (struct target_ops
*ops
)
1539 fprintf_unfiltered (gdb_stdlog
, "Process record: "
1540 "record_mourn_inferior\n");
1542 unpush_target (&record_ops
);
1543 target_mourn_inferior ();
1546 /* Close process record target before killing the inferior process. */
1549 record_kill (struct target_ops
*ops
)
1552 fprintf_unfiltered (gdb_stdlog
, "Process record: record_kill\n");
1554 unpush_target (&record_ops
);
1558 /* Record registers change (by user or by GDB) to list as an instruction. */
1561 record_registers_change (struct regcache
*regcache
, int regnum
)
1563 /* Check record_insn_num. */
1564 record_check_insn_num (0);
1566 record_arch_list_head
= NULL
;
1567 record_arch_list_tail
= NULL
;
1573 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
1575 if (record_arch_list_add_reg (regcache
, i
))
1577 record_list_release (record_arch_list_tail
);
1578 error (_("Process record: failed to record execution log."));
1584 if (record_arch_list_add_reg (regcache
, regnum
))
1586 record_list_release (record_arch_list_tail
);
1587 error (_("Process record: failed to record execution log."));
1590 if (record_arch_list_add_end ())
1592 record_list_release (record_arch_list_tail
);
1593 error (_("Process record: failed to record execution log."));
1595 record_list
->next
= record_arch_list_head
;
1596 record_arch_list_head
->prev
= record_list
;
1597 record_list
= record_arch_list_tail
;
1599 if (record_insn_num
== record_insn_max_num
&& record_insn_max_num
)
1600 record_list_release_first ();
1605 /* "to_store_registers" method for process record target. */
1608 record_store_registers (struct target_ops
*ops
, struct regcache
*regcache
,
1611 if (!record_gdb_operation_disable
)
1613 if (RECORD_IS_REPLAY
)
1617 /* Let user choose if he wants to write register or not. */
1620 query (_("Because GDB is in replay mode, changing the "
1621 "value of a register will make the execution "
1622 "log unusable from this point onward. "
1623 "Change all registers?"));
1626 query (_("Because GDB is in replay mode, changing the value "
1627 "of a register will make the execution log unusable "
1628 "from this point onward. Change register %s?"),
1629 gdbarch_register_name (get_regcache_arch (regcache
),
1634 /* Invalidate the value of regcache that was set in function
1635 "regcache_raw_write". */
1641 i
< gdbarch_num_regs (get_regcache_arch (regcache
));
1643 regcache_invalidate (regcache
, i
);
1646 regcache_invalidate (regcache
, regno
);
1648 error (_("Process record canceled the operation."));
1651 /* Destroy the record from here forward. */
1652 record_list_release_following (record_list
);
1655 record_registers_change (regcache
, regno
);
1657 record_beneath_to_store_registers (record_beneath_to_store_registers_ops
,
1661 /* "to_xfer_partial" method. Behavior is conditional on RECORD_IS_REPLAY.
1662 In replay mode, we cannot write memory unles we are willing to
1663 invalidate the record/replay log from this point forward. */
1666 record_xfer_partial (struct target_ops
*ops
, enum target_object object
,
1667 const char *annex
, gdb_byte
*readbuf
,
1668 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
1670 if (!record_gdb_operation_disable
1671 && (object
== TARGET_OBJECT_MEMORY
1672 || object
== TARGET_OBJECT_RAW_MEMORY
) && writebuf
)
1674 if (RECORD_IS_REPLAY
)
1676 /* Let user choose if he wants to write memory or not. */
1677 if (!query (_("Because GDB is in replay mode, writing to memory "
1678 "will make the execution log unusable from this "
1679 "point onward. Write memory at address %s?"),
1680 paddress (target_gdbarch
, offset
)))
1681 error (_("Process record canceled the operation."));
1683 /* Destroy the record from here forward. */
1684 record_list_release_following (record_list
);
1687 /* Check record_insn_num */
1688 record_check_insn_num (0);
1690 /* Record registers change to list as an instruction. */
1691 record_arch_list_head
= NULL
;
1692 record_arch_list_tail
= NULL
;
1693 if (record_arch_list_add_mem (offset
, len
))
1695 record_list_release (record_arch_list_tail
);
1697 fprintf_unfiltered (gdb_stdlog
,
1698 "Process record: failed to record "
1702 if (record_arch_list_add_end ())
1704 record_list_release (record_arch_list_tail
);
1706 fprintf_unfiltered (gdb_stdlog
,
1707 "Process record: failed to record "
1711 record_list
->next
= record_arch_list_head
;
1712 record_arch_list_head
->prev
= record_list
;
1713 record_list
= record_arch_list_tail
;
1715 if (record_insn_num
== record_insn_max_num
&& record_insn_max_num
)
1716 record_list_release_first ();
1721 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops
,
1722 object
, annex
, readbuf
, writebuf
,
1726 /* This structure represents a breakpoint inserted while the record
1727 target is active. We use this to know when to install/remove
1728 breakpoints in/from the target beneath. For example, a breakpoint
1729 may be inserted while recording, but removed when not replaying nor
1730 recording. In that case, the breakpoint had not been inserted on
1731 the target beneath, so we should not try to remove it there. */
1733 struct record_breakpoint
1735 /* The address and address space the breakpoint was set at. */
1736 struct address_space
*address_space
;
1739 /* True when the breakpoint has been also installed in the target
1740 beneath. This will be false for breakpoints set during replay or
1742 int in_target_beneath
;
1745 typedef struct record_breakpoint
*record_breakpoint_p
;
1746 DEF_VEC_P(record_breakpoint_p
);
1748 /* The list of breakpoints inserted while the record target is
1750 VEC(record_breakpoint_p
) *record_breakpoints
= NULL
;
1753 record_sync_record_breakpoints (struct bp_location
*loc
, void *data
)
1755 if (loc
->loc_type
!= bp_loc_software_breakpoint
)
1760 struct record_breakpoint
*bp
= XNEW (struct record_breakpoint
);
1762 bp
->addr
= loc
->target_info
.placed_address
;
1763 bp
->address_space
= loc
->target_info
.placed_address_space
;
1765 bp
->in_target_beneath
= 1;
1767 VEC_safe_push (record_breakpoint_p
, record_breakpoints
, bp
);
1771 /* Sync existing breakpoints to record_breakpoints. */
1774 record_init_record_breakpoints (void)
1776 VEC_free (record_breakpoint_p
, record_breakpoints
);
1778 iterate_over_bp_locations (record_sync_record_breakpoints
);
1781 /* Behavior is conditional on RECORD_IS_REPLAY. We will not actually
1782 insert or remove breakpoints in the real target when replaying, nor
1786 record_insert_breakpoint (struct gdbarch
*gdbarch
,
1787 struct bp_target_info
*bp_tgt
)
1789 struct record_breakpoint
*bp
;
1790 int in_target_beneath
= 0;
1792 if (!RECORD_IS_REPLAY
)
1794 /* When recording, we currently always single-step, so we don't
1795 really need to install regular breakpoints in the inferior.
1796 However, we do have to insert software single-step
1797 breakpoints, in case the target can't hardware step. To keep
1798 things single, we always insert. */
1799 struct cleanup
*old_cleanups
;
1802 old_cleanups
= record_gdb_operation_disable_set ();
1803 ret
= record_beneath_to_insert_breakpoint (gdbarch
, bp_tgt
);
1804 do_cleanups (old_cleanups
);
1809 in_target_beneath
= 1;
1812 bp
= XNEW (struct record_breakpoint
);
1813 bp
->addr
= bp_tgt
->placed_address
;
1814 bp
->address_space
= bp_tgt
->placed_address_space
;
1815 bp
->in_target_beneath
= in_target_beneath
;
1816 VEC_safe_push (record_breakpoint_p
, record_breakpoints
, bp
);
1820 /* "to_remove_breakpoint" method for process record target. */
1823 record_remove_breakpoint (struct gdbarch
*gdbarch
,
1824 struct bp_target_info
*bp_tgt
)
1826 struct record_breakpoint
*bp
;
1830 VEC_iterate (record_breakpoint_p
, record_breakpoints
, ix
, bp
);
1833 if (bp
->addr
== bp_tgt
->placed_address
1834 && bp
->address_space
== bp_tgt
->placed_address_space
)
1836 if (bp
->in_target_beneath
)
1838 struct cleanup
*old_cleanups
;
1841 old_cleanups
= record_gdb_operation_disable_set ();
1842 ret
= record_beneath_to_remove_breakpoint (gdbarch
, bp_tgt
);
1843 do_cleanups (old_cleanups
);
1849 VEC_unordered_remove (record_breakpoint_p
, record_breakpoints
, ix
);
1854 gdb_assert_not_reached ("removing unknown breakpoint");
1857 /* "to_can_execute_reverse" method for process record target. */
1860 record_can_execute_reverse (void)
1865 /* "to_get_bookmark" method for process record and prec over core. */
1868 record_get_bookmark (char *args
, int from_tty
)
1870 gdb_byte
*ret
= NULL
;
1872 /* Return stringified form of instruction count. */
1873 if (record_list
&& record_list
->type
== record_end
)
1874 ret
= xstrdup (pulongest (record_list
->u
.end
.insn_num
));
1879 fprintf_unfiltered (gdb_stdlog
,
1880 "record_get_bookmark returns %s\n", ret
);
1882 fprintf_unfiltered (gdb_stdlog
,
1883 "record_get_bookmark returns NULL\n");
1888 /* The implementation of the command "record goto". */
1889 static void cmd_record_goto (char *, int);
1891 /* "to_goto_bookmark" method for process record and prec over core. */
1894 record_goto_bookmark (gdb_byte
*bookmark
, int from_tty
)
1897 fprintf_unfiltered (gdb_stdlog
,
1898 "record_goto_bookmark receives %s\n", bookmark
);
1900 if (bookmark
[0] == '\'' || bookmark
[0] == '\"')
1902 if (bookmark
[strlen (bookmark
) - 1] != bookmark
[0])
1903 error (_("Unbalanced quotes: %s"), bookmark
);
1905 /* Strip trailing quote. */
1906 bookmark
[strlen (bookmark
) - 1] = '\0';
1907 /* Strip leading quote. */
1909 /* Pass along to cmd_record_goto. */
1912 cmd_record_goto ((char *) bookmark
, from_tty
);
1917 record_async (void (*callback
) (enum inferior_event_type event_type
,
1918 void *context
), void *context
)
1920 /* If we're on top of a line target (e.g., linux-nat, remote), then
1921 set it to async mode as well. Will be NULL if we're sitting on
1922 top of the core target, for "record restore". */
1923 if (record_beneath_to_async
!= NULL
)
1924 record_beneath_to_async (callback
, context
);
1928 record_can_async_p (void)
1930 /* We only enable async when the user specifically asks for it. */
1931 return target_async_permitted
;
1935 record_is_async_p (void)
1937 /* We only enable async when the user specifically asks for it. */
1938 return target_async_permitted
;
1941 static enum exec_direction_kind
1942 record_execution_direction (void)
1944 return record_execution_dir
;
1948 init_record_ops (void)
1950 record_ops
.to_shortname
= "record";
1951 record_ops
.to_longname
= "Process record and replay target";
1953 "Log program while executing and replay execution from log.";
1954 record_ops
.to_open
= record_open
;
1955 record_ops
.to_close
= record_close
;
1956 record_ops
.to_resume
= record_resume
;
1957 record_ops
.to_wait
= record_wait
;
1958 record_ops
.to_disconnect
= record_disconnect
;
1959 record_ops
.to_detach
= record_detach
;
1960 record_ops
.to_mourn_inferior
= record_mourn_inferior
;
1961 record_ops
.to_kill
= record_kill
;
1962 record_ops
.to_create_inferior
= find_default_create_inferior
;
1963 record_ops
.to_store_registers
= record_store_registers
;
1964 record_ops
.to_xfer_partial
= record_xfer_partial
;
1965 record_ops
.to_insert_breakpoint
= record_insert_breakpoint
;
1966 record_ops
.to_remove_breakpoint
= record_remove_breakpoint
;
1967 record_ops
.to_stopped_by_watchpoint
= record_stopped_by_watchpoint
;
1968 record_ops
.to_stopped_data_address
= record_stopped_data_address
;
1969 record_ops
.to_can_execute_reverse
= record_can_execute_reverse
;
1970 record_ops
.to_stratum
= record_stratum
;
1971 /* Add bookmark target methods. */
1972 record_ops
.to_get_bookmark
= record_get_bookmark
;
1973 record_ops
.to_goto_bookmark
= record_goto_bookmark
;
1974 record_ops
.to_async
= record_async
;
1975 record_ops
.to_can_async_p
= record_can_async_p
;
1976 record_ops
.to_is_async_p
= record_is_async_p
;
1977 record_ops
.to_execution_direction
= record_execution_direction
;
1978 record_ops
.to_magic
= OPS_MAGIC
;
1981 /* "to_resume" method for prec over corefile. */
1984 record_core_resume (struct target_ops
*ops
, ptid_t ptid
, int step
,
1985 enum gdb_signal signal
)
1987 record_resume_step
= step
;
1989 record_execution_dir
= execution_direction
;
1991 /* We are about to start executing the inferior (or simulate it),
1992 let's register it with the event loop. */
1993 if (target_can_async_p ())
1995 target_async (inferior_event_handler
, 0);
1997 /* Notify the event loop there's an event to wait for. */
1998 mark_async_event_handler (record_async_inferior_event_token
);
2002 /* "to_kill" method for prec over corefile. */
2005 record_core_kill (struct target_ops
*ops
)
2008 fprintf_unfiltered (gdb_stdlog
, "Process record: record_core_kill\n");
2010 unpush_target (&record_core_ops
);
2013 /* "to_fetch_registers" method for prec over corefile. */
2016 record_core_fetch_registers (struct target_ops
*ops
,
2017 struct regcache
*regcache
,
2022 int num
= gdbarch_num_regs (get_regcache_arch (regcache
));
2025 for (i
= 0; i
< num
; i
++)
2026 regcache_raw_supply (regcache
, i
,
2027 record_core_regbuf
+ MAX_REGISTER_SIZE
* i
);
2030 regcache_raw_supply (regcache
, regno
,
2031 record_core_regbuf
+ MAX_REGISTER_SIZE
* regno
);
2034 /* "to_prepare_to_store" method for prec over corefile. */
2037 record_core_prepare_to_store (struct regcache
*regcache
)
2041 /* "to_store_registers" method for prec over corefile. */
2044 record_core_store_registers (struct target_ops
*ops
,
2045 struct regcache
*regcache
,
2048 if (record_gdb_operation_disable
)
2049 regcache_raw_collect (regcache
, regno
,
2050 record_core_regbuf
+ MAX_REGISTER_SIZE
* regno
);
2052 error (_("You can't do that without a process to debug."));
2055 /* "to_xfer_partial" method for prec over corefile. */
2058 record_core_xfer_partial (struct target_ops
*ops
, enum target_object object
,
2059 const char *annex
, gdb_byte
*readbuf
,
2060 const gdb_byte
*writebuf
, ULONGEST offset
,
2063 if (object
== TARGET_OBJECT_MEMORY
)
2065 if (record_gdb_operation_disable
|| !writebuf
)
2067 struct target_section
*p
;
2069 for (p
= record_core_start
; p
< record_core_end
; p
++)
2071 if (offset
>= p
->addr
)
2073 struct record_core_buf_entry
*entry
;
2074 ULONGEST sec_offset
;
2076 if (offset
>= p
->endaddr
)
2079 if (offset
+ len
> p
->endaddr
)
2080 len
= p
->endaddr
- offset
;
2082 sec_offset
= offset
- p
->addr
;
2084 /* Read readbuf or write writebuf p, offset, len. */
2086 if (p
->the_bfd_section
->flags
& SEC_CONSTRUCTOR
2087 || (p
->the_bfd_section
->flags
& SEC_HAS_CONTENTS
) == 0)
2090 memset (readbuf
, 0, len
);
2093 /* Get record_core_buf_entry. */
2094 for (entry
= record_core_buf_list
; entry
;
2095 entry
= entry
->prev
)
2102 /* Add a new entry. */
2103 entry
= (struct record_core_buf_entry
*)
2104 xmalloc (sizeof (struct record_core_buf_entry
));
2106 if (!bfd_malloc_and_get_section (p
->bfd
,
2113 entry
->prev
= record_core_buf_list
;
2114 record_core_buf_list
= entry
;
2117 memcpy (entry
->buf
+ sec_offset
, writebuf
,
2123 return record_beneath_to_xfer_partial
2124 (record_beneath_to_xfer_partial_ops
,
2125 object
, annex
, readbuf
, writebuf
,
2128 memcpy (readbuf
, entry
->buf
+ sec_offset
,
2139 error (_("You can't do that without a process to debug."));
2142 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops
,
2143 object
, annex
, readbuf
, writebuf
,
2147 /* "to_insert_breakpoint" method for prec over corefile. */
2150 record_core_insert_breakpoint (struct gdbarch
*gdbarch
,
2151 struct bp_target_info
*bp_tgt
)
2156 /* "to_remove_breakpoint" method for prec over corefile. */
2159 record_core_remove_breakpoint (struct gdbarch
*gdbarch
,
2160 struct bp_target_info
*bp_tgt
)
2165 /* "to_has_execution" method for prec over corefile. */
2168 record_core_has_execution (struct target_ops
*ops
, ptid_t the_ptid
)
2174 init_record_core_ops (void)
2176 record_core_ops
.to_shortname
= "record-core";
2177 record_core_ops
.to_longname
= "Process record and replay target";
2178 record_core_ops
.to_doc
=
2179 "Log program while executing and replay execution from log.";
2180 record_core_ops
.to_open
= record_open
;
2181 record_core_ops
.to_close
= record_close
;
2182 record_core_ops
.to_resume
= record_core_resume
;
2183 record_core_ops
.to_wait
= record_wait
;
2184 record_core_ops
.to_kill
= record_core_kill
;
2185 record_core_ops
.to_fetch_registers
= record_core_fetch_registers
;
2186 record_core_ops
.to_prepare_to_store
= record_core_prepare_to_store
;
2187 record_core_ops
.to_store_registers
= record_core_store_registers
;
2188 record_core_ops
.to_xfer_partial
= record_core_xfer_partial
;
2189 record_core_ops
.to_insert_breakpoint
= record_core_insert_breakpoint
;
2190 record_core_ops
.to_remove_breakpoint
= record_core_remove_breakpoint
;
2191 record_core_ops
.to_stopped_by_watchpoint
= record_stopped_by_watchpoint
;
2192 record_core_ops
.to_stopped_data_address
= record_stopped_data_address
;
2193 record_core_ops
.to_can_execute_reverse
= record_can_execute_reverse
;
2194 record_core_ops
.to_has_execution
= record_core_has_execution
;
2195 record_core_ops
.to_stratum
= record_stratum
;
2196 /* Add bookmark target methods. */
2197 record_core_ops
.to_get_bookmark
= record_get_bookmark
;
2198 record_core_ops
.to_goto_bookmark
= record_goto_bookmark
;
2199 record_core_ops
.to_async
= record_async
;
2200 record_core_ops
.to_can_async_p
= record_can_async_p
;
2201 record_core_ops
.to_is_async_p
= record_is_async_p
;
2202 record_core_ops
.to_execution_direction
= record_execution_direction
;
2203 record_core_ops
.to_magic
= OPS_MAGIC
;
2206 /* Implement "show record debug" command. */
2209 show_record_debug (struct ui_file
*file
, int from_tty
,
2210 struct cmd_list_element
*c
, const char *value
)
2212 fprintf_filtered (file
, _("Debugging of process record target is %s.\n"),
2216 /* Alias for "target record". */
2219 cmd_record_start (char *args
, int from_tty
)
2221 execute_command ("target record", from_tty
);
2224 /* Truncate the record log from the present point
2225 of replay until the end. */
2228 cmd_record_delete (char *args
, int from_tty
)
2230 if (current_target
.to_stratum
== record_stratum
)
2232 if (RECORD_IS_REPLAY
)
2234 if (!from_tty
|| query (_("Delete the log from this point forward "
2235 "and begin to record the running message "
2237 record_list_release_following (record_list
);
2240 printf_unfiltered (_("Already at end of record list.\n"));
2244 printf_unfiltered (_("Process record is not started.\n"));
2247 /* Implement the "stoprecord" or "record stop" command. */
2250 cmd_record_stop (char *args
, int from_tty
)
2252 if (current_target
.to_stratum
== record_stratum
)
2254 unpush_target (&record_ops
);
2255 printf_unfiltered (_("Process record is stopped and all execution "
2256 "logs are deleted.\n"));
2259 printf_unfiltered (_("Process record is not started.\n"));
2262 /* Set upper limit of record log size. */
2265 set_record_insn_max_num (char *args
, int from_tty
, struct cmd_list_element
*c
)
2267 if (record_insn_num
> record_insn_max_num
&& record_insn_max_num
)
2269 /* Count down record_insn_num while releasing records from list. */
2270 while (record_insn_num
> record_insn_max_num
)
2272 record_list_release_first ();
2278 static struct cmd_list_element
*record_cmdlist
, *set_record_cmdlist
,
2279 *show_record_cmdlist
, *info_record_cmdlist
;
2282 set_record_command (char *args
, int from_tty
)
2284 printf_unfiltered (_("\"set record\" must be followed "
2285 "by an apporpriate subcommand.\n"));
2286 help_list (set_record_cmdlist
, "set record ", all_commands
, gdb_stdout
);
2290 show_record_command (char *args
, int from_tty
)
2292 cmd_show_list (show_record_cmdlist
, from_tty
, "");
2295 /* Display some statistics about the execution log. */
2298 info_record_command (char *args
, int from_tty
)
2300 struct record_entry
*p
;
2302 if (current_target
.to_stratum
== record_stratum
)
2304 if (RECORD_IS_REPLAY
)
2305 printf_filtered (_("Replay mode:\n"));
2307 printf_filtered (_("Record mode:\n"));
2309 /* Find entry for first actual instruction in the log. */
2310 for (p
= record_first
.next
;
2311 p
!= NULL
&& p
->type
!= record_end
;
2315 /* Do we have a log at all? */
2316 if (p
!= NULL
&& p
->type
== record_end
)
2318 /* Display instruction number for first instruction in the log. */
2319 printf_filtered (_("Lowest recorded instruction number is %s.\n"),
2320 pulongest (p
->u
.end
.insn_num
));
2322 /* If in replay mode, display where we are in the log. */
2323 if (RECORD_IS_REPLAY
)
2324 printf_filtered (_("Current instruction number is %s.\n"),
2325 pulongest (record_list
->u
.end
.insn_num
));
2327 /* Display instruction number for last instruction in the log. */
2328 printf_filtered (_("Highest recorded instruction number is %s.\n"),
2329 pulongest (record_insn_count
));
2331 /* Display log count. */
2332 printf_filtered (_("Log contains %d instructions.\n"),
2337 printf_filtered (_("No instructions have been logged.\n"));
2342 printf_filtered (_("target record is not active.\n"));
2345 /* Display max log size. */
2346 printf_filtered (_("Max logged instructions is %d.\n"),
2347 record_insn_max_num
);
2350 /* Record log save-file format
2351 Version 1 (never released)
2354 4 bytes: magic number htonl(0x20090829).
2355 NOTE: be sure to change whenever this file format changes!
2359 1 byte: record type (record_end, see enum record_type).
2361 1 byte: record type (record_reg, see enum record_type).
2362 8 bytes: register id (network byte order).
2363 MAX_REGISTER_SIZE bytes: register value.
2365 1 byte: record type (record_mem, see enum record_type).
2366 8 bytes: memory length (network byte order).
2367 8 bytes: memory address (network byte order).
2368 n bytes: memory value (n == memory length).
2371 4 bytes: magic number netorder32(0x20091016).
2372 NOTE: be sure to change whenever this file format changes!
2376 1 byte: record type (record_end, see enum record_type).
2378 4 bytes: instruction count
2380 1 byte: record type (record_reg, see enum record_type).
2381 4 bytes: register id (network byte order).
2382 n bytes: register value (n == actual register size).
2383 (eg. 4 bytes for x86 general registers).
2385 1 byte: record type (record_mem, see enum record_type).
2386 4 bytes: memory length (network byte order).
2387 8 bytes: memory address (network byte order).
2388 n bytes: memory value (n == memory length).
2392 /* bfdcore_read -- read bytes from a core file section. */
2395 bfdcore_read (bfd
*obfd
, asection
*osec
, void *buf
, int len
, int *offset
)
2397 int ret
= bfd_get_section_contents (obfd
, osec
, buf
, *offset
, len
);
2402 error (_("Failed to read %d bytes from core file %s ('%s')."),
2403 len
, bfd_get_filename (obfd
),
2404 bfd_errmsg (bfd_get_error ()));
2407 static inline uint64_t
2408 netorder64 (uint64_t input
)
2412 store_unsigned_integer ((gdb_byte
*) &ret
, sizeof (ret
),
2413 BFD_ENDIAN_BIG
, input
);
2417 static inline uint32_t
2418 netorder32 (uint32_t input
)
2422 store_unsigned_integer ((gdb_byte
*) &ret
, sizeof (ret
),
2423 BFD_ENDIAN_BIG
, input
);
2427 static inline uint16_t
2428 netorder16 (uint16_t input
)
2432 store_unsigned_integer ((gdb_byte
*) &ret
, sizeof (ret
),
2433 BFD_ENDIAN_BIG
, input
);
2437 /* Restore the execution log from a core_bfd file. */
2439 record_restore (void)
2442 struct cleanup
*old_cleanups
;
2443 struct record_entry
*rec
;
2447 struct regcache
*regcache
;
2449 /* We restore the execution log from the open core bfd,
2451 if (core_bfd
== NULL
)
2454 /* "record_restore" can only be called when record list is empty. */
2455 gdb_assert (record_first
.next
== NULL
);
2458 fprintf_unfiltered (gdb_stdlog
, "Restoring recording from core file.\n");
2460 /* Now need to find our special note section. */
2461 osec
= bfd_get_section_by_name (core_bfd
, "null0");
2463 fprintf_unfiltered (gdb_stdlog
, "Find precord section %s.\n",
2464 osec
? "succeeded" : "failed");
2467 osec_size
= bfd_section_size (core_bfd
, osec
);
2469 fprintf_unfiltered (gdb_stdlog
, "%s", bfd_section_name (core_bfd
, osec
));
2471 /* Check the magic code. */
2472 bfdcore_read (core_bfd
, osec
, &magic
, sizeof (magic
), &bfd_offset
);
2473 if (magic
!= RECORD_FILE_MAGIC
)
2474 error (_("Version mis-match or file format error in core file %s."),
2475 bfd_get_filename (core_bfd
));
2477 fprintf_unfiltered (gdb_stdlog
,
2478 " Reading 4-byte magic cookie "
2479 "RECORD_FILE_MAGIC (0x%s)\n",
2480 phex_nz (netorder32 (magic
), 4));
2482 /* Restore the entries in recfd into record_arch_list_head and
2483 record_arch_list_tail. */
2484 record_arch_list_head
= NULL
;
2485 record_arch_list_tail
= NULL
;
2486 record_insn_num
= 0;
2487 old_cleanups
= make_cleanup (record_arch_list_cleanups
, 0);
2488 regcache
= get_current_regcache ();
2493 uint32_t regnum
, len
, signal
, count
;
2496 /* We are finished when offset reaches osec_size. */
2497 if (bfd_offset
>= osec_size
)
2499 bfdcore_read (core_bfd
, osec
, &rectype
, sizeof (rectype
), &bfd_offset
);
2503 case record_reg
: /* reg */
2504 /* Get register number to regnum. */
2505 bfdcore_read (core_bfd
, osec
, ®num
,
2506 sizeof (regnum
), &bfd_offset
);
2507 regnum
= netorder32 (regnum
);
2509 rec
= record_reg_alloc (regcache
, regnum
);
2512 bfdcore_read (core_bfd
, osec
, record_get_loc (rec
),
2513 rec
->u
.reg
.len
, &bfd_offset
);
2516 fprintf_unfiltered (gdb_stdlog
,
2517 " Reading register %d (1 "
2518 "plus %lu plus %d bytes)\n",
2520 (unsigned long) sizeof (regnum
),
2524 case record_mem
: /* mem */
2526 bfdcore_read (core_bfd
, osec
, &len
,
2527 sizeof (len
), &bfd_offset
);
2528 len
= netorder32 (len
);
2531 bfdcore_read (core_bfd
, osec
, &addr
,
2532 sizeof (addr
), &bfd_offset
);
2533 addr
= netorder64 (addr
);
2535 rec
= record_mem_alloc (addr
, len
);
2538 bfdcore_read (core_bfd
, osec
, record_get_loc (rec
),
2539 rec
->u
.mem
.len
, &bfd_offset
);
2542 fprintf_unfiltered (gdb_stdlog
,
2543 " Reading memory %s (1 plus "
2544 "%lu plus %lu plus %d bytes)\n",
2545 paddress (get_current_arch (),
2547 (unsigned long) sizeof (addr
),
2548 (unsigned long) sizeof (len
),
2552 case record_end
: /* end */
2553 rec
= record_end_alloc ();
2556 /* Get signal value. */
2557 bfdcore_read (core_bfd
, osec
, &signal
,
2558 sizeof (signal
), &bfd_offset
);
2559 signal
= netorder32 (signal
);
2560 rec
->u
.end
.sigval
= signal
;
2562 /* Get insn count. */
2563 bfdcore_read (core_bfd
, osec
, &count
,
2564 sizeof (count
), &bfd_offset
);
2565 count
= netorder32 (count
);
2566 rec
->u
.end
.insn_num
= count
;
2567 record_insn_count
= count
+ 1;
2569 fprintf_unfiltered (gdb_stdlog
,
2570 " Reading record_end (1 + "
2571 "%lu + %lu bytes), offset == %s\n",
2572 (unsigned long) sizeof (signal
),
2573 (unsigned long) sizeof (count
),
2574 paddress (get_current_arch (),
2579 error (_("Bad entry type in core file %s."),
2580 bfd_get_filename (core_bfd
));
2584 /* Add rec to record arch list. */
2585 record_arch_list_add (rec
);
2588 discard_cleanups (old_cleanups
);
2590 /* Add record_arch_list_head to the end of record list. */
2591 record_first
.next
= record_arch_list_head
;
2592 record_arch_list_head
->prev
= &record_first
;
2593 record_arch_list_tail
->next
= NULL
;
2594 record_list
= &record_first
;
2596 /* Update record_insn_max_num. */
2597 if (record_insn_num
> record_insn_max_num
)
2599 record_insn_max_num
= record_insn_num
;
2600 warning (_("Auto increase record/replay buffer limit to %d."),
2601 record_insn_max_num
);
2605 printf_filtered (_("Restored records from core file %s.\n"),
2606 bfd_get_filename (core_bfd
));
2608 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
2611 /* bfdcore_write -- write bytes into a core file section. */
2614 bfdcore_write (bfd
*obfd
, asection
*osec
, void *buf
, int len
, int *offset
)
2616 int ret
= bfd_set_section_contents (obfd
, osec
, buf
, *offset
, len
);
2621 error (_("Failed to write %d bytes to core file %s ('%s')."),
2622 len
, bfd_get_filename (obfd
),
2623 bfd_errmsg (bfd_get_error ()));
2626 /* Restore the execution log from a file. We use a modified elf
2627 corefile format, with an extra section for our data. */
2630 cmd_record_restore (char *args
, int from_tty
)
2632 core_file_command (args
, from_tty
);
2633 record_open (args
, from_tty
);
2637 record_save_cleanups (void *data
)
2640 char *pathname
= xstrdup (bfd_get_filename (obfd
));
2642 gdb_bfd_unref (obfd
);
2647 /* Save the execution log to a file. We use a modified elf corefile
2648 format, with an extra section for our data. */
2651 cmd_record_save (char *args
, int from_tty
)
2653 char *recfilename
, recfilename_buffer
[40];
2654 struct record_entry
*cur_record_list
;
2656 struct regcache
*regcache
;
2657 struct gdbarch
*gdbarch
;
2658 struct cleanup
*old_cleanups
;
2659 struct cleanup
*set_cleanups
;
2662 asection
*osec
= NULL
;
2665 if (strcmp (current_target
.to_shortname
, "record") != 0)
2666 error (_("This command can only be used with target 'record'.\n"
2667 "Use 'target record' first.\n"));
2673 /* Default recfile name is "gdb_record.PID". */
2674 snprintf (recfilename_buffer
, sizeof (recfilename_buffer
),
2675 "gdb_record.%d", PIDGET (inferior_ptid
));
2676 recfilename
= recfilename_buffer
;
2679 /* Open the save file. */
2681 fprintf_unfiltered (gdb_stdlog
, "Saving execution log to core file '%s'\n",
2684 /* Open the output file. */
2685 obfd
= create_gcore_bfd (recfilename
);
2686 old_cleanups
= make_cleanup (record_save_cleanups
, obfd
);
2688 /* Save the current record entry to "cur_record_list". */
2689 cur_record_list
= record_list
;
2691 /* Get the values of regcache and gdbarch. */
2692 regcache
= get_current_regcache ();
2693 gdbarch
= get_regcache_arch (regcache
);
2695 /* Disable the GDB operation record. */
2696 set_cleanups
= record_gdb_operation_disable_set ();
2698 /* Reverse execute to the begin of record list. */
2701 /* Check for beginning and end of log. */
2702 if (record_list
== &record_first
)
2705 record_exec_insn (regcache
, gdbarch
, record_list
);
2707 if (record_list
->prev
)
2708 record_list
= record_list
->prev
;
2711 /* Compute the size needed for the extra bfd section. */
2712 save_size
= 4; /* magic cookie */
2713 for (record_list
= record_first
.next
; record_list
;
2714 record_list
= record_list
->next
)
2715 switch (record_list
->type
)
2718 save_size
+= 1 + 4 + 4;
2721 save_size
+= 1 + 4 + record_list
->u
.reg
.len
;
2724 save_size
+= 1 + 4 + 8 + record_list
->u
.mem
.len
;
2728 /* Make the new bfd section. */
2729 osec
= bfd_make_section_anyway_with_flags (obfd
, "precord",
2733 error (_("Failed to create 'precord' section for corefile %s: %s"),
2735 bfd_errmsg (bfd_get_error ()));
2736 bfd_set_section_size (obfd
, osec
, save_size
);
2737 bfd_set_section_vma (obfd
, osec
, 0);
2738 bfd_set_section_alignment (obfd
, osec
, 0);
2739 bfd_section_lma (obfd
, osec
) = 0;
2741 /* Save corefile state. */
2742 write_gcore_file (obfd
);
2744 /* Write out the record log. */
2745 /* Write the magic code. */
2746 magic
= RECORD_FILE_MAGIC
;
2748 fprintf_unfiltered (gdb_stdlog
,
2749 " Writing 4-byte magic cookie "
2750 "RECORD_FILE_MAGIC (0x%s)\n",
2751 phex_nz (magic
, 4));
2752 bfdcore_write (obfd
, osec
, &magic
, sizeof (magic
), &bfd_offset
);
2754 /* Save the entries to recfd and forward execute to the end of
2756 record_list
= &record_first
;
2760 if (record_list
!= &record_first
)
2763 uint32_t regnum
, len
, signal
, count
;
2766 type
= record_list
->type
;
2767 bfdcore_write (obfd
, osec
, &type
, sizeof (type
), &bfd_offset
);
2769 switch (record_list
->type
)
2771 case record_reg
: /* reg */
2773 fprintf_unfiltered (gdb_stdlog
,
2774 " Writing register %d (1 "
2775 "plus %lu plus %d bytes)\n",
2776 record_list
->u
.reg
.num
,
2777 (unsigned long) sizeof (regnum
),
2778 record_list
->u
.reg
.len
);
2781 regnum
= netorder32 (record_list
->u
.reg
.num
);
2782 bfdcore_write (obfd
, osec
, ®num
,
2783 sizeof (regnum
), &bfd_offset
);
2786 bfdcore_write (obfd
, osec
, record_get_loc (record_list
),
2787 record_list
->u
.reg
.len
, &bfd_offset
);
2790 case record_mem
: /* mem */
2792 fprintf_unfiltered (gdb_stdlog
,
2793 " Writing memory %s (1 plus "
2794 "%lu plus %lu plus %d bytes)\n",
2796 record_list
->u
.mem
.addr
),
2797 (unsigned long) sizeof (addr
),
2798 (unsigned long) sizeof (len
),
2799 record_list
->u
.mem
.len
);
2802 len
= netorder32 (record_list
->u
.mem
.len
);
2803 bfdcore_write (obfd
, osec
, &len
, sizeof (len
), &bfd_offset
);
2805 /* Write memaddr. */
2806 addr
= netorder64 (record_list
->u
.mem
.addr
);
2807 bfdcore_write (obfd
, osec
, &addr
,
2808 sizeof (addr
), &bfd_offset
);
2811 bfdcore_write (obfd
, osec
, record_get_loc (record_list
),
2812 record_list
->u
.mem
.len
, &bfd_offset
);
2817 fprintf_unfiltered (gdb_stdlog
,
2818 " Writing record_end (1 + "
2819 "%lu + %lu bytes)\n",
2820 (unsigned long) sizeof (signal
),
2821 (unsigned long) sizeof (count
));
2822 /* Write signal value. */
2823 signal
= netorder32 (record_list
->u
.end
.sigval
);
2824 bfdcore_write (obfd
, osec
, &signal
,
2825 sizeof (signal
), &bfd_offset
);
2827 /* Write insn count. */
2828 count
= netorder32 (record_list
->u
.end
.insn_num
);
2829 bfdcore_write (obfd
, osec
, &count
,
2830 sizeof (count
), &bfd_offset
);
2835 /* Execute entry. */
2836 record_exec_insn (regcache
, gdbarch
, record_list
);
2838 if (record_list
->next
)
2839 record_list
= record_list
->next
;
2844 /* Reverse execute to cur_record_list. */
2847 /* Check for beginning and end of log. */
2848 if (record_list
== cur_record_list
)
2851 record_exec_insn (regcache
, gdbarch
, record_list
);
2853 if (record_list
->prev
)
2854 record_list
= record_list
->prev
;
2857 do_cleanups (set_cleanups
);
2858 gdb_bfd_unref (obfd
);
2859 discard_cleanups (old_cleanups
);
2862 printf_filtered (_("Saved core file %s with execution log.\n"),
2866 /* record_goto_insn -- rewind the record log (forward or backward,
2867 depending on DIR) to the given entry, changing the program state
2871 record_goto_insn (struct record_entry
*entry
,
2872 enum exec_direction_kind dir
)
2874 struct cleanup
*set_cleanups
= record_gdb_operation_disable_set ();
2875 struct regcache
*regcache
= get_current_regcache ();
2876 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
2878 /* Assume everything is valid: we will hit the entry,
2879 and we will not hit the end of the recording. */
2881 if (dir
== EXEC_FORWARD
)
2882 record_list
= record_list
->next
;
2886 record_exec_insn (regcache
, gdbarch
, record_list
);
2887 if (dir
== EXEC_REVERSE
)
2888 record_list
= record_list
->prev
;
2890 record_list
= record_list
->next
;
2891 } while (record_list
!= entry
);
2892 do_cleanups (set_cleanups
);
2895 /* "record goto" command. Argument is an instruction number,
2896 as given by "info record".
2898 Rewinds the recording (forward or backward) to the given instruction. */
2901 cmd_record_goto (char *arg
, int from_tty
)
2903 struct record_entry
*p
= NULL
;
2904 ULONGEST target_insn
= 0;
2906 if (arg
== NULL
|| *arg
== '\0')
2907 error (_("Command requires an argument (insn number to go to)."));
2909 if (strncmp (arg
, "start", strlen ("start")) == 0
2910 || strncmp (arg
, "begin", strlen ("begin")) == 0)
2912 /* Special case. Find first insn. */
2913 for (p
= &record_first
; p
!= NULL
; p
= p
->next
)
2914 if (p
->type
== record_end
)
2917 target_insn
= p
->u
.end
.insn_num
;
2919 else if (strncmp (arg
, "end", strlen ("end")) == 0)
2921 /* Special case. Find last insn. */
2922 for (p
= record_list
; p
->next
!= NULL
; p
= p
->next
)
2924 for (; p
!= NULL
; p
= p
->prev
)
2925 if (p
->type
== record_end
)
2928 target_insn
= p
->u
.end
.insn_num
;
2932 /* General case. Find designated insn. */
2933 target_insn
= parse_and_eval_long (arg
);
2935 for (p
= &record_first
; p
!= NULL
; p
= p
->next
)
2936 if (p
->type
== record_end
&& p
->u
.end
.insn_num
== target_insn
)
2941 error (_("Target insn '%s' not found."), arg
);
2942 else if (p
== record_list
)
2943 error (_("Already at insn '%s'."), arg
);
2944 else if (p
->u
.end
.insn_num
> record_list
->u
.end
.insn_num
)
2946 printf_filtered (_("Go forward to insn number %s\n"),
2947 pulongest (target_insn
));
2948 record_goto_insn (p
, EXEC_FORWARD
);
2952 printf_filtered (_("Go backward to insn number %s\n"),
2953 pulongest (target_insn
));
2954 record_goto_insn (p
, EXEC_REVERSE
);
2956 registers_changed ();
2957 reinit_frame_cache ();
2958 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
2961 /* Provide a prototype to silence -Wmissing-prototypes. */
2962 extern initialize_file_ftype _initialize_record
;
2965 _initialize_record (void)
2967 struct cmd_list_element
*c
;
2969 /* Init record_first. */
2970 record_first
.prev
= NULL
;
2971 record_first
.next
= NULL
;
2972 record_first
.type
= record_end
;
2975 add_target (&record_ops
);
2976 init_record_core_ops ();
2977 add_target (&record_core_ops
);
2979 add_setshow_zinteger_cmd ("record", no_class
, &record_debug
,
2980 _("Set debugging of record/replay feature."),
2981 _("Show debugging of record/replay feature."),
2982 _("When enabled, debugging output for "
2983 "record/replay feature is displayed."),
2984 NULL
, show_record_debug
, &setdebuglist
,
2987 c
= add_prefix_cmd ("record", class_obscure
, cmd_record_start
,
2988 _("Abbreviated form of \"target record\" command."),
2989 &record_cmdlist
, "record ", 0, &cmdlist
);
2990 set_cmd_completer (c
, filename_completer
);
2992 add_com_alias ("rec", "record", class_obscure
, 1);
2993 add_prefix_cmd ("record", class_support
, set_record_command
,
2994 _("Set record options"), &set_record_cmdlist
,
2995 "set record ", 0, &setlist
);
2996 add_alias_cmd ("rec", "record", class_obscure
, 1, &setlist
);
2997 add_prefix_cmd ("record", class_support
, show_record_command
,
2998 _("Show record options"), &show_record_cmdlist
,
2999 "show record ", 0, &showlist
);
3000 add_alias_cmd ("rec", "record", class_obscure
, 1, &showlist
);
3001 add_prefix_cmd ("record", class_support
, info_record_command
,
3002 _("Info record options"), &info_record_cmdlist
,
3003 "info record ", 0, &infolist
);
3004 add_alias_cmd ("rec", "record", class_obscure
, 1, &infolist
);
3006 c
= add_cmd ("save", class_obscure
, cmd_record_save
,
3007 _("Save the execution log to a file.\n\
3008 Argument is optional filename.\n\
3009 Default filename is 'gdb_record.<process_id>'."),
3011 set_cmd_completer (c
, filename_completer
);
3013 c
= add_cmd ("restore", class_obscure
, cmd_record_restore
,
3014 _("Restore the execution log from a file.\n\
3015 Argument is filename. File must be created with 'record save'."),
3017 set_cmd_completer (c
, filename_completer
);
3019 add_cmd ("delete", class_obscure
, cmd_record_delete
,
3020 _("Delete the rest of execution log and start recording it anew."),
3022 add_alias_cmd ("d", "delete", class_obscure
, 1, &record_cmdlist
);
3023 add_alias_cmd ("del", "delete", class_obscure
, 1, &record_cmdlist
);
3025 add_cmd ("stop", class_obscure
, cmd_record_stop
,
3026 _("Stop the record/replay target."),
3028 add_alias_cmd ("s", "stop", class_obscure
, 1, &record_cmdlist
);
3030 /* Record instructions number limit command. */
3031 add_setshow_boolean_cmd ("stop-at-limit", no_class
,
3032 &record_stop_at_limit
, _("\
3033 Set whether record/replay stops when record/replay buffer becomes full."), _("\
3034 Show whether record/replay stops when record/replay buffer becomes full."),
3035 _("Default is ON.\n\
3036 When ON, if the record/replay buffer becomes full, ask user what to do.\n\
3037 When OFF, if the record/replay buffer becomes full,\n\
3038 delete the oldest recorded instruction to make room for each new one."),
3040 &set_record_cmdlist
, &show_record_cmdlist
);
3041 add_setshow_uinteger_cmd ("insn-number-max", no_class
,
3042 &record_insn_max_num
,
3043 _("Set record/replay buffer limit."),
3044 _("Show record/replay buffer limit."), _("\
3045 Set the maximum number of instructions to be stored in the\n\
3046 record/replay buffer. Zero means unlimited. Default is 200000."),
3047 set_record_insn_max_num
,
3048 NULL
, &set_record_cmdlist
, &show_record_cmdlist
);
3050 add_cmd ("goto", class_obscure
, cmd_record_goto
, _("\
3051 Restore the program to its state at instruction number N.\n\
3052 Argument is instruction number, as shown by 'info record'."),
3055 add_setshow_boolean_cmd ("memory-query", no_class
,
3056 &record_memory_query
, _("\
3057 Set whether query if PREC cannot record memory change of next instruction."),
3059 Show whether query if PREC cannot record memory change of next instruction."),
3062 When ON, query if PREC cannot record memory change of next instruction."),
3064 &set_record_cmdlist
, &show_record_cmdlist
);