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 /* Make sure the target beneath reports all signals. */
1108 target_pass_signals (0, NULL
);
1110 record_beneath_to_resume (record_beneath_to_resume_ops
,
1111 ptid
, step
, signal
);
1114 /* We are about to start executing the inferior (or simulate it),
1115 let's register it with the event loop. */
1116 if (target_can_async_p ())
1118 target_async (inferior_event_handler
, 0);
1119 /* Notify the event loop there's an event to wait for. We do
1120 most of the work in record_wait. */
1121 mark_async_event_handler (record_async_inferior_event_token
);
1125 static int record_get_sig
= 0;
1127 /* SIGINT signal handler, registered by "to_wait" method. */
1130 record_sig_handler (int signo
)
1133 fprintf_unfiltered (gdb_stdlog
, "Process record: get a signal\n");
1135 /* It will break the running inferior in replay mode. */
1136 record_resume_step
= 1;
1138 /* It will let record_wait set inferior status to get the signal
1144 record_wait_cleanups (void *ignore
)
1146 if (execution_direction
== EXEC_REVERSE
)
1148 if (record_list
->next
)
1149 record_list
= record_list
->next
;
1152 record_list
= record_list
->prev
;
1155 /* "to_wait" target method for process record target.
1157 In record mode, the target is always run in singlestep mode
1158 (even when gdb says to continue). The to_wait method intercepts
1159 the stop events and determines which ones are to be passed on to
1160 gdb. Most stop events are just singlestep events that gdb is not
1161 to know about, so the to_wait method just records them and keeps
1164 In replay mode, this function emulates the recorded execution log,
1165 one instruction at a time (forward or backward), and determines
1169 record_wait_1 (struct target_ops
*ops
,
1170 ptid_t ptid
, struct target_waitstatus
*status
,
1173 struct cleanup
*set_cleanups
= record_gdb_operation_disable_set ();
1176 fprintf_unfiltered (gdb_stdlog
,
1177 "Process record: record_wait "
1178 "record_resume_step = %d, record_resumed = %d, direction=%s\n",
1179 record_resume_step
, record_resumed
,
1180 record_execution_dir
== EXEC_FORWARD
? "forward" : "reverse");
1182 if (!record_resumed
)
1184 gdb_assert ((options
& TARGET_WNOHANG
) != 0);
1186 /* No interesting event. */
1187 status
->kind
= TARGET_WAITKIND_IGNORE
;
1188 return minus_one_ptid
;
1192 signal (SIGINT
, record_sig_handler
);
1194 if (!RECORD_IS_REPLAY
&& ops
!= &record_core_ops
)
1196 if (record_resume_step
)
1198 /* This is a single step. */
1199 return record_beneath_to_wait (record_beneath_to_wait_ops
,
1200 ptid
, status
, options
);
1204 /* This is not a single step. */
1207 struct gdbarch
*gdbarch
= target_thread_architecture (inferior_ptid
);
1211 ret
= record_beneath_to_wait (record_beneath_to_wait_ops
,
1212 ptid
, status
, options
);
1213 if (status
->kind
== TARGET_WAITKIND_IGNORE
)
1216 fprintf_unfiltered (gdb_stdlog
,
1217 "Process record: record_wait "
1218 "target beneath not done yet\n");
1222 if (single_step_breakpoints_inserted ())
1223 remove_single_step_breakpoints ();
1225 if (record_resume_step
)
1228 /* Is this a SIGTRAP? */
1229 if (status
->kind
== TARGET_WAITKIND_STOPPED
1230 && status
->value
.sig
== GDB_SIGNAL_TRAP
)
1232 struct regcache
*regcache
;
1233 struct address_space
*aspace
;
1235 /* Yes -- this is likely our single-step finishing,
1236 but check if there's any reason the core would be
1237 interested in the event. */
1239 registers_changed ();
1240 regcache
= get_current_regcache ();
1241 tmp_pc
= regcache_read_pc (regcache
);
1242 aspace
= get_regcache_aspace (regcache
);
1244 if (target_stopped_by_watchpoint ())
1246 /* Always interested in watchpoints. */
1248 else if (breakpoint_inserted_here_p (aspace
, tmp_pc
))
1250 /* There is a breakpoint here. Let the core
1252 if (software_breakpoint_inserted_here_p (aspace
, tmp_pc
))
1254 struct gdbarch
*gdbarch
1255 = get_regcache_arch (regcache
);
1256 CORE_ADDR decr_pc_after_break
1257 = gdbarch_decr_pc_after_break (gdbarch
);
1258 if (decr_pc_after_break
)
1259 regcache_write_pc (regcache
,
1260 tmp_pc
+ decr_pc_after_break
);
1265 /* This is a single-step trap. Record the
1266 insn and issue another step.
1267 FIXME: this part can be a random SIGTRAP too.
1268 But GDB cannot handle it. */
1271 if (!record_message_wrapper_safe (regcache
,
1274 status
->kind
= TARGET_WAITKIND_STOPPED
;
1275 status
->value
.sig
= GDB_SIGNAL_0
;
1279 if (gdbarch_software_single_step_p (gdbarch
))
1281 /* Try to insert the software single step breakpoint.
1282 If insert success, set step to 0. */
1283 set_executing (inferior_ptid
, 0);
1284 reinit_frame_cache ();
1285 if (gdbarch_software_single_step (gdbarch
,
1286 get_current_frame ()))
1288 set_executing (inferior_ptid
, 1);
1292 fprintf_unfiltered (gdb_stdlog
,
1293 "Process record: record_wait "
1294 "issuing one more step in the target beneath\n");
1295 record_beneath_to_resume (record_beneath_to_resume_ops
,
1302 /* The inferior is broken by a breakpoint or a signal. */
1311 struct regcache
*regcache
= get_current_regcache ();
1312 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1313 struct address_space
*aspace
= get_regcache_aspace (regcache
);
1314 int continue_flag
= 1;
1315 int first_record_end
= 1;
1316 struct cleanup
*old_cleanups
= make_cleanup (record_wait_cleanups
, 0);
1319 record_hw_watchpoint
= 0;
1320 status
->kind
= TARGET_WAITKIND_STOPPED
;
1322 /* Check breakpoint when forward execute. */
1323 if (execution_direction
== EXEC_FORWARD
)
1325 tmp_pc
= regcache_read_pc (regcache
);
1326 if (breakpoint_inserted_here_p (aspace
, tmp_pc
))
1328 int decr_pc_after_break
= gdbarch_decr_pc_after_break (gdbarch
);
1331 fprintf_unfiltered (gdb_stdlog
,
1332 "Process record: break at %s.\n",
1333 paddress (gdbarch
, tmp_pc
));
1335 if (decr_pc_after_break
1336 && !record_resume_step
1337 && software_breakpoint_inserted_here_p (aspace
, tmp_pc
))
1338 regcache_write_pc (regcache
,
1339 tmp_pc
+ decr_pc_after_break
);
1344 /* If GDB is in terminal_inferior mode, it will not get the signal.
1345 And in GDB replay mode, GDB doesn't need to be in terminal_inferior
1346 mode, because inferior will not executed.
1347 Then set it to terminal_ours to make GDB get the signal. */
1348 target_terminal_ours ();
1350 /* In EXEC_FORWARD mode, record_list points to the tail of prev
1352 if (execution_direction
== EXEC_FORWARD
&& record_list
->next
)
1353 record_list
= record_list
->next
;
1355 /* Loop over the record_list, looking for the next place to
1359 /* Check for beginning and end of log. */
1360 if (execution_direction
== EXEC_REVERSE
1361 && record_list
== &record_first
)
1363 /* Hit beginning of record log in reverse. */
1364 status
->kind
= TARGET_WAITKIND_NO_HISTORY
;
1367 if (execution_direction
!= EXEC_REVERSE
&& !record_list
->next
)
1369 /* Hit end of record log going forward. */
1370 status
->kind
= TARGET_WAITKIND_NO_HISTORY
;
1374 record_exec_insn (regcache
, gdbarch
, record_list
);
1376 if (record_list
->type
== record_end
)
1378 if (record_debug
> 1)
1379 fprintf_unfiltered (gdb_stdlog
,
1380 "Process record: record_end %s to "
1382 host_address_to_string (record_list
));
1384 if (first_record_end
&& execution_direction
== EXEC_REVERSE
)
1386 /* When reverse excute, the first record_end is the part of
1387 current instruction. */
1388 first_record_end
= 0;
1392 /* In EXEC_REVERSE mode, this is the record_end of prev
1394 In EXEC_FORWARD mode, this is the record_end of current
1397 if (record_resume_step
)
1399 if (record_debug
> 1)
1400 fprintf_unfiltered (gdb_stdlog
,
1401 "Process record: step.\n");
1405 /* check breakpoint */
1406 tmp_pc
= regcache_read_pc (regcache
);
1407 if (breakpoint_inserted_here_p (aspace
, tmp_pc
))
1409 int decr_pc_after_break
1410 = gdbarch_decr_pc_after_break (gdbarch
);
1413 fprintf_unfiltered (gdb_stdlog
,
1414 "Process record: break "
1416 paddress (gdbarch
, tmp_pc
));
1417 if (decr_pc_after_break
1418 && execution_direction
== EXEC_FORWARD
1419 && !record_resume_step
1420 && software_breakpoint_inserted_here_p (aspace
,
1422 regcache_write_pc (regcache
,
1423 tmp_pc
+ decr_pc_after_break
);
1427 if (record_hw_watchpoint
)
1430 fprintf_unfiltered (gdb_stdlog
,
1431 "Process record: hit hw "
1435 /* Check target signal */
1436 if (record_list
->u
.end
.sigval
!= GDB_SIGNAL_0
)
1437 /* FIXME: better way to check */
1444 if (execution_direction
== EXEC_REVERSE
)
1446 if (record_list
->prev
)
1447 record_list
= record_list
->prev
;
1451 if (record_list
->next
)
1452 record_list
= record_list
->next
;
1456 while (continue_flag
);
1460 status
->value
.sig
= GDB_SIGNAL_INT
;
1461 else if (record_list
->u
.end
.sigval
!= GDB_SIGNAL_0
)
1462 /* FIXME: better way to check */
1463 status
->value
.sig
= record_list
->u
.end
.sigval
;
1465 status
->value
.sig
= GDB_SIGNAL_TRAP
;
1467 discard_cleanups (old_cleanups
);
1470 signal (SIGINT
, handle_sigint
);
1472 do_cleanups (set_cleanups
);
1473 return inferior_ptid
;
1477 record_wait (struct target_ops
*ops
,
1478 ptid_t ptid
, struct target_waitstatus
*status
,
1483 return_ptid
= record_wait_1 (ops
, ptid
, status
, options
);
1484 if (status
->kind
!= TARGET_WAITKIND_IGNORE
)
1486 /* We're reporting a stop. Make sure any spurious
1487 target_wait(WNOHANG) doesn't advance the target until the
1488 core wants us resumed again. */
1495 record_stopped_by_watchpoint (void)
1497 if (RECORD_IS_REPLAY
)
1498 return record_hw_watchpoint
;
1500 return record_beneath_to_stopped_by_watchpoint ();
1504 record_stopped_data_address (struct target_ops
*ops
, CORE_ADDR
*addr_p
)
1506 if (RECORD_IS_REPLAY
)
1509 return record_beneath_to_stopped_data_address (ops
, addr_p
);
1512 /* "to_disconnect" method for process record target. */
1515 record_disconnect (struct target_ops
*target
, char *args
, int from_tty
)
1518 fprintf_unfiltered (gdb_stdlog
, "Process record: record_disconnect\n");
1520 unpush_target (&record_ops
);
1521 target_disconnect (args
, from_tty
);
1524 /* "to_detach" method for process record target. */
1527 record_detach (struct target_ops
*ops
, char *args
, int from_tty
)
1530 fprintf_unfiltered (gdb_stdlog
, "Process record: record_detach\n");
1532 unpush_target (&record_ops
);
1533 target_detach (args
, from_tty
);
1536 /* "to_mourn_inferior" method for process record target. */
1539 record_mourn_inferior (struct target_ops
*ops
)
1542 fprintf_unfiltered (gdb_stdlog
, "Process record: "
1543 "record_mourn_inferior\n");
1545 unpush_target (&record_ops
);
1546 target_mourn_inferior ();
1549 /* Close process record target before killing the inferior process. */
1552 record_kill (struct target_ops
*ops
)
1555 fprintf_unfiltered (gdb_stdlog
, "Process record: record_kill\n");
1557 unpush_target (&record_ops
);
1561 /* Record registers change (by user or by GDB) to list as an instruction. */
1564 record_registers_change (struct regcache
*regcache
, int regnum
)
1566 /* Check record_insn_num. */
1567 record_check_insn_num (0);
1569 record_arch_list_head
= NULL
;
1570 record_arch_list_tail
= NULL
;
1576 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
1578 if (record_arch_list_add_reg (regcache
, i
))
1580 record_list_release (record_arch_list_tail
);
1581 error (_("Process record: failed to record execution log."));
1587 if (record_arch_list_add_reg (regcache
, regnum
))
1589 record_list_release (record_arch_list_tail
);
1590 error (_("Process record: failed to record execution log."));
1593 if (record_arch_list_add_end ())
1595 record_list_release (record_arch_list_tail
);
1596 error (_("Process record: failed to record execution log."));
1598 record_list
->next
= record_arch_list_head
;
1599 record_arch_list_head
->prev
= record_list
;
1600 record_list
= record_arch_list_tail
;
1602 if (record_insn_num
== record_insn_max_num
&& record_insn_max_num
)
1603 record_list_release_first ();
1608 /* "to_store_registers" method for process record target. */
1611 record_store_registers (struct target_ops
*ops
, struct regcache
*regcache
,
1614 if (!record_gdb_operation_disable
)
1616 if (RECORD_IS_REPLAY
)
1620 /* Let user choose if he wants to write register or not. */
1623 query (_("Because GDB is in replay mode, changing the "
1624 "value of a register will make the execution "
1625 "log unusable from this point onward. "
1626 "Change all registers?"));
1629 query (_("Because GDB is in replay mode, changing the value "
1630 "of a register will make the execution log unusable "
1631 "from this point onward. Change register %s?"),
1632 gdbarch_register_name (get_regcache_arch (regcache
),
1637 /* Invalidate the value of regcache that was set in function
1638 "regcache_raw_write". */
1644 i
< gdbarch_num_regs (get_regcache_arch (regcache
));
1646 regcache_invalidate (regcache
, i
);
1649 regcache_invalidate (regcache
, regno
);
1651 error (_("Process record canceled the operation."));
1654 /* Destroy the record from here forward. */
1655 record_list_release_following (record_list
);
1658 record_registers_change (regcache
, regno
);
1660 record_beneath_to_store_registers (record_beneath_to_store_registers_ops
,
1664 /* "to_xfer_partial" method. Behavior is conditional on RECORD_IS_REPLAY.
1665 In replay mode, we cannot write memory unles we are willing to
1666 invalidate the record/replay log from this point forward. */
1669 record_xfer_partial (struct target_ops
*ops
, enum target_object object
,
1670 const char *annex
, gdb_byte
*readbuf
,
1671 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
1673 if (!record_gdb_operation_disable
1674 && (object
== TARGET_OBJECT_MEMORY
1675 || object
== TARGET_OBJECT_RAW_MEMORY
) && writebuf
)
1677 if (RECORD_IS_REPLAY
)
1679 /* Let user choose if he wants to write memory or not. */
1680 if (!query (_("Because GDB is in replay mode, writing to memory "
1681 "will make the execution log unusable from this "
1682 "point onward. Write memory at address %s?"),
1683 paddress (target_gdbarch
, offset
)))
1684 error (_("Process record canceled the operation."));
1686 /* Destroy the record from here forward. */
1687 record_list_release_following (record_list
);
1690 /* Check record_insn_num */
1691 record_check_insn_num (0);
1693 /* Record registers change to list as an instruction. */
1694 record_arch_list_head
= NULL
;
1695 record_arch_list_tail
= NULL
;
1696 if (record_arch_list_add_mem (offset
, len
))
1698 record_list_release (record_arch_list_tail
);
1700 fprintf_unfiltered (gdb_stdlog
,
1701 "Process record: failed to record "
1705 if (record_arch_list_add_end ())
1707 record_list_release (record_arch_list_tail
);
1709 fprintf_unfiltered (gdb_stdlog
,
1710 "Process record: failed to record "
1714 record_list
->next
= record_arch_list_head
;
1715 record_arch_list_head
->prev
= record_list
;
1716 record_list
= record_arch_list_tail
;
1718 if (record_insn_num
== record_insn_max_num
&& record_insn_max_num
)
1719 record_list_release_first ();
1724 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops
,
1725 object
, annex
, readbuf
, writebuf
,
1729 /* This structure represents a breakpoint inserted while the record
1730 target is active. We use this to know when to install/remove
1731 breakpoints in/from the target beneath. For example, a breakpoint
1732 may be inserted while recording, but removed when not replaying nor
1733 recording. In that case, the breakpoint had not been inserted on
1734 the target beneath, so we should not try to remove it there. */
1736 struct record_breakpoint
1738 /* The address and address space the breakpoint was set at. */
1739 struct address_space
*address_space
;
1742 /* True when the breakpoint has been also installed in the target
1743 beneath. This will be false for breakpoints set during replay or
1745 int in_target_beneath
;
1748 typedef struct record_breakpoint
*record_breakpoint_p
;
1749 DEF_VEC_P(record_breakpoint_p
);
1751 /* The list of breakpoints inserted while the record target is
1753 VEC(record_breakpoint_p
) *record_breakpoints
= NULL
;
1756 record_sync_record_breakpoints (struct bp_location
*loc
, void *data
)
1758 if (loc
->loc_type
!= bp_loc_software_breakpoint
)
1763 struct record_breakpoint
*bp
= XNEW (struct record_breakpoint
);
1765 bp
->addr
= loc
->target_info
.placed_address
;
1766 bp
->address_space
= loc
->target_info
.placed_address_space
;
1768 bp
->in_target_beneath
= 1;
1770 VEC_safe_push (record_breakpoint_p
, record_breakpoints
, bp
);
1774 /* Sync existing breakpoints to record_breakpoints. */
1777 record_init_record_breakpoints (void)
1779 VEC_free (record_breakpoint_p
, record_breakpoints
);
1781 iterate_over_bp_locations (record_sync_record_breakpoints
);
1784 /* Behavior is conditional on RECORD_IS_REPLAY. We will not actually
1785 insert or remove breakpoints in the real target when replaying, nor
1789 record_insert_breakpoint (struct gdbarch
*gdbarch
,
1790 struct bp_target_info
*bp_tgt
)
1792 struct record_breakpoint
*bp
;
1793 int in_target_beneath
= 0;
1795 if (!RECORD_IS_REPLAY
)
1797 /* When recording, we currently always single-step, so we don't
1798 really need to install regular breakpoints in the inferior.
1799 However, we do have to insert software single-step
1800 breakpoints, in case the target can't hardware step. To keep
1801 things single, we always insert. */
1802 struct cleanup
*old_cleanups
;
1805 old_cleanups
= record_gdb_operation_disable_set ();
1806 ret
= record_beneath_to_insert_breakpoint (gdbarch
, bp_tgt
);
1807 do_cleanups (old_cleanups
);
1812 in_target_beneath
= 1;
1815 bp
= XNEW (struct record_breakpoint
);
1816 bp
->addr
= bp_tgt
->placed_address
;
1817 bp
->address_space
= bp_tgt
->placed_address_space
;
1818 bp
->in_target_beneath
= in_target_beneath
;
1819 VEC_safe_push (record_breakpoint_p
, record_breakpoints
, bp
);
1823 /* "to_remove_breakpoint" method for process record target. */
1826 record_remove_breakpoint (struct gdbarch
*gdbarch
,
1827 struct bp_target_info
*bp_tgt
)
1829 struct record_breakpoint
*bp
;
1833 VEC_iterate (record_breakpoint_p
, record_breakpoints
, ix
, bp
);
1836 if (bp
->addr
== bp_tgt
->placed_address
1837 && bp
->address_space
== bp_tgt
->placed_address_space
)
1839 if (bp
->in_target_beneath
)
1841 struct cleanup
*old_cleanups
;
1844 old_cleanups
= record_gdb_operation_disable_set ();
1845 ret
= record_beneath_to_remove_breakpoint (gdbarch
, bp_tgt
);
1846 do_cleanups (old_cleanups
);
1852 VEC_unordered_remove (record_breakpoint_p
, record_breakpoints
, ix
);
1857 gdb_assert_not_reached ("removing unknown breakpoint");
1860 /* "to_can_execute_reverse" method for process record target. */
1863 record_can_execute_reverse (void)
1868 /* "to_get_bookmark" method for process record and prec over core. */
1871 record_get_bookmark (char *args
, int from_tty
)
1873 gdb_byte
*ret
= NULL
;
1875 /* Return stringified form of instruction count. */
1876 if (record_list
&& record_list
->type
== record_end
)
1877 ret
= xstrdup (pulongest (record_list
->u
.end
.insn_num
));
1882 fprintf_unfiltered (gdb_stdlog
,
1883 "record_get_bookmark returns %s\n", ret
);
1885 fprintf_unfiltered (gdb_stdlog
,
1886 "record_get_bookmark returns NULL\n");
1891 /* The implementation of the command "record goto". */
1892 static void cmd_record_goto (char *, int);
1894 /* "to_goto_bookmark" method for process record and prec over core. */
1897 record_goto_bookmark (gdb_byte
*bookmark
, int from_tty
)
1900 fprintf_unfiltered (gdb_stdlog
,
1901 "record_goto_bookmark receives %s\n", bookmark
);
1903 if (bookmark
[0] == '\'' || bookmark
[0] == '\"')
1905 if (bookmark
[strlen (bookmark
) - 1] != bookmark
[0])
1906 error (_("Unbalanced quotes: %s"), bookmark
);
1908 /* Strip trailing quote. */
1909 bookmark
[strlen (bookmark
) - 1] = '\0';
1910 /* Strip leading quote. */
1912 /* Pass along to cmd_record_goto. */
1915 cmd_record_goto ((char *) bookmark
, from_tty
);
1920 record_async (void (*callback
) (enum inferior_event_type event_type
,
1921 void *context
), void *context
)
1923 /* If we're on top of a line target (e.g., linux-nat, remote), then
1924 set it to async mode as well. Will be NULL if we're sitting on
1925 top of the core target, for "record restore". */
1926 if (record_beneath_to_async
!= NULL
)
1927 record_beneath_to_async (callback
, context
);
1931 record_can_async_p (void)
1933 /* We only enable async when the user specifically asks for it. */
1934 return target_async_permitted
;
1938 record_is_async_p (void)
1940 /* We only enable async when the user specifically asks for it. */
1941 return target_async_permitted
;
1944 static enum exec_direction_kind
1945 record_execution_direction (void)
1947 return record_execution_dir
;
1951 init_record_ops (void)
1953 record_ops
.to_shortname
= "record";
1954 record_ops
.to_longname
= "Process record and replay target";
1956 "Log program while executing and replay execution from log.";
1957 record_ops
.to_open
= record_open
;
1958 record_ops
.to_close
= record_close
;
1959 record_ops
.to_resume
= record_resume
;
1960 record_ops
.to_wait
= record_wait
;
1961 record_ops
.to_disconnect
= record_disconnect
;
1962 record_ops
.to_detach
= record_detach
;
1963 record_ops
.to_mourn_inferior
= record_mourn_inferior
;
1964 record_ops
.to_kill
= record_kill
;
1965 record_ops
.to_create_inferior
= find_default_create_inferior
;
1966 record_ops
.to_store_registers
= record_store_registers
;
1967 record_ops
.to_xfer_partial
= record_xfer_partial
;
1968 record_ops
.to_insert_breakpoint
= record_insert_breakpoint
;
1969 record_ops
.to_remove_breakpoint
= record_remove_breakpoint
;
1970 record_ops
.to_stopped_by_watchpoint
= record_stopped_by_watchpoint
;
1971 record_ops
.to_stopped_data_address
= record_stopped_data_address
;
1972 record_ops
.to_can_execute_reverse
= record_can_execute_reverse
;
1973 record_ops
.to_stratum
= record_stratum
;
1974 /* Add bookmark target methods. */
1975 record_ops
.to_get_bookmark
= record_get_bookmark
;
1976 record_ops
.to_goto_bookmark
= record_goto_bookmark
;
1977 record_ops
.to_async
= record_async
;
1978 record_ops
.to_can_async_p
= record_can_async_p
;
1979 record_ops
.to_is_async_p
= record_is_async_p
;
1980 record_ops
.to_execution_direction
= record_execution_direction
;
1981 record_ops
.to_magic
= OPS_MAGIC
;
1984 /* "to_resume" method for prec over corefile. */
1987 record_core_resume (struct target_ops
*ops
, ptid_t ptid
, int step
,
1988 enum gdb_signal signal
)
1990 record_resume_step
= step
;
1992 record_execution_dir
= execution_direction
;
1994 /* We are about to start executing the inferior (or simulate it),
1995 let's register it with the event loop. */
1996 if (target_can_async_p ())
1998 target_async (inferior_event_handler
, 0);
2000 /* Notify the event loop there's an event to wait for. */
2001 mark_async_event_handler (record_async_inferior_event_token
);
2005 /* "to_kill" method for prec over corefile. */
2008 record_core_kill (struct target_ops
*ops
)
2011 fprintf_unfiltered (gdb_stdlog
, "Process record: record_core_kill\n");
2013 unpush_target (&record_core_ops
);
2016 /* "to_fetch_registers" method for prec over corefile. */
2019 record_core_fetch_registers (struct target_ops
*ops
,
2020 struct regcache
*regcache
,
2025 int num
= gdbarch_num_regs (get_regcache_arch (regcache
));
2028 for (i
= 0; i
< num
; i
++)
2029 regcache_raw_supply (regcache
, i
,
2030 record_core_regbuf
+ MAX_REGISTER_SIZE
* i
);
2033 regcache_raw_supply (regcache
, regno
,
2034 record_core_regbuf
+ MAX_REGISTER_SIZE
* regno
);
2037 /* "to_prepare_to_store" method for prec over corefile. */
2040 record_core_prepare_to_store (struct regcache
*regcache
)
2044 /* "to_store_registers" method for prec over corefile. */
2047 record_core_store_registers (struct target_ops
*ops
,
2048 struct regcache
*regcache
,
2051 if (record_gdb_operation_disable
)
2052 regcache_raw_collect (regcache
, regno
,
2053 record_core_regbuf
+ MAX_REGISTER_SIZE
* regno
);
2055 error (_("You can't do that without a process to debug."));
2058 /* "to_xfer_partial" method for prec over corefile. */
2061 record_core_xfer_partial (struct target_ops
*ops
, enum target_object object
,
2062 const char *annex
, gdb_byte
*readbuf
,
2063 const gdb_byte
*writebuf
, ULONGEST offset
,
2066 if (object
== TARGET_OBJECT_MEMORY
)
2068 if (record_gdb_operation_disable
|| !writebuf
)
2070 struct target_section
*p
;
2072 for (p
= record_core_start
; p
< record_core_end
; p
++)
2074 if (offset
>= p
->addr
)
2076 struct record_core_buf_entry
*entry
;
2077 ULONGEST sec_offset
;
2079 if (offset
>= p
->endaddr
)
2082 if (offset
+ len
> p
->endaddr
)
2083 len
= p
->endaddr
- offset
;
2085 sec_offset
= offset
- p
->addr
;
2087 /* Read readbuf or write writebuf p, offset, len. */
2089 if (p
->the_bfd_section
->flags
& SEC_CONSTRUCTOR
2090 || (p
->the_bfd_section
->flags
& SEC_HAS_CONTENTS
) == 0)
2093 memset (readbuf
, 0, len
);
2096 /* Get record_core_buf_entry. */
2097 for (entry
= record_core_buf_list
; entry
;
2098 entry
= entry
->prev
)
2105 /* Add a new entry. */
2106 entry
= (struct record_core_buf_entry
*)
2107 xmalloc (sizeof (struct record_core_buf_entry
));
2109 if (!bfd_malloc_and_get_section (p
->bfd
,
2116 entry
->prev
= record_core_buf_list
;
2117 record_core_buf_list
= entry
;
2120 memcpy (entry
->buf
+ sec_offset
, writebuf
,
2126 return record_beneath_to_xfer_partial
2127 (record_beneath_to_xfer_partial_ops
,
2128 object
, annex
, readbuf
, writebuf
,
2131 memcpy (readbuf
, entry
->buf
+ sec_offset
,
2142 error (_("You can't do that without a process to debug."));
2145 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops
,
2146 object
, annex
, readbuf
, writebuf
,
2150 /* "to_insert_breakpoint" method for prec over corefile. */
2153 record_core_insert_breakpoint (struct gdbarch
*gdbarch
,
2154 struct bp_target_info
*bp_tgt
)
2159 /* "to_remove_breakpoint" method for prec over corefile. */
2162 record_core_remove_breakpoint (struct gdbarch
*gdbarch
,
2163 struct bp_target_info
*bp_tgt
)
2168 /* "to_has_execution" method for prec over corefile. */
2171 record_core_has_execution (struct target_ops
*ops
, ptid_t the_ptid
)
2177 init_record_core_ops (void)
2179 record_core_ops
.to_shortname
= "record-core";
2180 record_core_ops
.to_longname
= "Process record and replay target";
2181 record_core_ops
.to_doc
=
2182 "Log program while executing and replay execution from log.";
2183 record_core_ops
.to_open
= record_open
;
2184 record_core_ops
.to_close
= record_close
;
2185 record_core_ops
.to_resume
= record_core_resume
;
2186 record_core_ops
.to_wait
= record_wait
;
2187 record_core_ops
.to_kill
= record_core_kill
;
2188 record_core_ops
.to_fetch_registers
= record_core_fetch_registers
;
2189 record_core_ops
.to_prepare_to_store
= record_core_prepare_to_store
;
2190 record_core_ops
.to_store_registers
= record_core_store_registers
;
2191 record_core_ops
.to_xfer_partial
= record_core_xfer_partial
;
2192 record_core_ops
.to_insert_breakpoint
= record_core_insert_breakpoint
;
2193 record_core_ops
.to_remove_breakpoint
= record_core_remove_breakpoint
;
2194 record_core_ops
.to_stopped_by_watchpoint
= record_stopped_by_watchpoint
;
2195 record_core_ops
.to_stopped_data_address
= record_stopped_data_address
;
2196 record_core_ops
.to_can_execute_reverse
= record_can_execute_reverse
;
2197 record_core_ops
.to_has_execution
= record_core_has_execution
;
2198 record_core_ops
.to_stratum
= record_stratum
;
2199 /* Add bookmark target methods. */
2200 record_core_ops
.to_get_bookmark
= record_get_bookmark
;
2201 record_core_ops
.to_goto_bookmark
= record_goto_bookmark
;
2202 record_core_ops
.to_async
= record_async
;
2203 record_core_ops
.to_can_async_p
= record_can_async_p
;
2204 record_core_ops
.to_is_async_p
= record_is_async_p
;
2205 record_core_ops
.to_execution_direction
= record_execution_direction
;
2206 record_core_ops
.to_magic
= OPS_MAGIC
;
2209 /* Implement "show record debug" command. */
2212 show_record_debug (struct ui_file
*file
, int from_tty
,
2213 struct cmd_list_element
*c
, const char *value
)
2215 fprintf_filtered (file
, _("Debugging of process record target is %s.\n"),
2219 /* Alias for "target record". */
2222 cmd_record_start (char *args
, int from_tty
)
2224 execute_command ("target record", from_tty
);
2227 /* Truncate the record log from the present point
2228 of replay until the end. */
2231 cmd_record_delete (char *args
, int from_tty
)
2233 if (current_target
.to_stratum
== record_stratum
)
2235 if (RECORD_IS_REPLAY
)
2237 if (!from_tty
|| query (_("Delete the log from this point forward "
2238 "and begin to record the running message "
2240 record_list_release_following (record_list
);
2243 printf_unfiltered (_("Already at end of record list.\n"));
2247 printf_unfiltered (_("Process record is not started.\n"));
2250 /* Implement the "stoprecord" or "record stop" command. */
2253 cmd_record_stop (char *args
, int from_tty
)
2255 if (current_target
.to_stratum
== record_stratum
)
2257 unpush_target (&record_ops
);
2258 printf_unfiltered (_("Process record is stopped and all execution "
2259 "logs are deleted.\n"));
2262 printf_unfiltered (_("Process record is not started.\n"));
2265 /* Set upper limit of record log size. */
2268 set_record_insn_max_num (char *args
, int from_tty
, struct cmd_list_element
*c
)
2270 if (record_insn_num
> record_insn_max_num
&& record_insn_max_num
)
2272 /* Count down record_insn_num while releasing records from list. */
2273 while (record_insn_num
> record_insn_max_num
)
2275 record_list_release_first ();
2281 static struct cmd_list_element
*record_cmdlist
, *set_record_cmdlist
,
2282 *show_record_cmdlist
, *info_record_cmdlist
;
2285 set_record_command (char *args
, int from_tty
)
2287 printf_unfiltered (_("\"set record\" must be followed "
2288 "by an apporpriate subcommand.\n"));
2289 help_list (set_record_cmdlist
, "set record ", all_commands
, gdb_stdout
);
2293 show_record_command (char *args
, int from_tty
)
2295 cmd_show_list (show_record_cmdlist
, from_tty
, "");
2298 /* Display some statistics about the execution log. */
2301 info_record_command (char *args
, int from_tty
)
2303 struct record_entry
*p
;
2305 if (current_target
.to_stratum
== record_stratum
)
2307 if (RECORD_IS_REPLAY
)
2308 printf_filtered (_("Replay mode:\n"));
2310 printf_filtered (_("Record mode:\n"));
2312 /* Find entry for first actual instruction in the log. */
2313 for (p
= record_first
.next
;
2314 p
!= NULL
&& p
->type
!= record_end
;
2318 /* Do we have a log at all? */
2319 if (p
!= NULL
&& p
->type
== record_end
)
2321 /* Display instruction number for first instruction in the log. */
2322 printf_filtered (_("Lowest recorded instruction number is %s.\n"),
2323 pulongest (p
->u
.end
.insn_num
));
2325 /* If in replay mode, display where we are in the log. */
2326 if (RECORD_IS_REPLAY
)
2327 printf_filtered (_("Current instruction number is %s.\n"),
2328 pulongest (record_list
->u
.end
.insn_num
));
2330 /* Display instruction number for last instruction in the log. */
2331 printf_filtered (_("Highest recorded instruction number is %s.\n"),
2332 pulongest (record_insn_count
));
2334 /* Display log count. */
2335 printf_filtered (_("Log contains %d instructions.\n"),
2340 printf_filtered (_("No instructions have been logged.\n"));
2345 printf_filtered (_("target record is not active.\n"));
2348 /* Display max log size. */
2349 printf_filtered (_("Max logged instructions is %d.\n"),
2350 record_insn_max_num
);
2353 /* Record log save-file format
2354 Version 1 (never released)
2357 4 bytes: magic number htonl(0x20090829).
2358 NOTE: be sure to change whenever this file format changes!
2362 1 byte: record type (record_end, see enum record_type).
2364 1 byte: record type (record_reg, see enum record_type).
2365 8 bytes: register id (network byte order).
2366 MAX_REGISTER_SIZE bytes: register value.
2368 1 byte: record type (record_mem, see enum record_type).
2369 8 bytes: memory length (network byte order).
2370 8 bytes: memory address (network byte order).
2371 n bytes: memory value (n == memory length).
2374 4 bytes: magic number netorder32(0x20091016).
2375 NOTE: be sure to change whenever this file format changes!
2379 1 byte: record type (record_end, see enum record_type).
2381 4 bytes: instruction count
2383 1 byte: record type (record_reg, see enum record_type).
2384 4 bytes: register id (network byte order).
2385 n bytes: register value (n == actual register size).
2386 (eg. 4 bytes for x86 general registers).
2388 1 byte: record type (record_mem, see enum record_type).
2389 4 bytes: memory length (network byte order).
2390 8 bytes: memory address (network byte order).
2391 n bytes: memory value (n == memory length).
2395 /* bfdcore_read -- read bytes from a core file section. */
2398 bfdcore_read (bfd
*obfd
, asection
*osec
, void *buf
, int len
, int *offset
)
2400 int ret
= bfd_get_section_contents (obfd
, osec
, buf
, *offset
, len
);
2405 error (_("Failed to read %d bytes from core file %s ('%s')."),
2406 len
, bfd_get_filename (obfd
),
2407 bfd_errmsg (bfd_get_error ()));
2410 static inline uint64_t
2411 netorder64 (uint64_t input
)
2415 store_unsigned_integer ((gdb_byte
*) &ret
, sizeof (ret
),
2416 BFD_ENDIAN_BIG
, input
);
2420 static inline uint32_t
2421 netorder32 (uint32_t input
)
2425 store_unsigned_integer ((gdb_byte
*) &ret
, sizeof (ret
),
2426 BFD_ENDIAN_BIG
, input
);
2430 static inline uint16_t
2431 netorder16 (uint16_t input
)
2435 store_unsigned_integer ((gdb_byte
*) &ret
, sizeof (ret
),
2436 BFD_ENDIAN_BIG
, input
);
2440 /* Restore the execution log from a core_bfd file. */
2442 record_restore (void)
2445 struct cleanup
*old_cleanups
;
2446 struct record_entry
*rec
;
2450 struct regcache
*regcache
;
2452 /* We restore the execution log from the open core bfd,
2454 if (core_bfd
== NULL
)
2457 /* "record_restore" can only be called when record list is empty. */
2458 gdb_assert (record_first
.next
== NULL
);
2461 fprintf_unfiltered (gdb_stdlog
, "Restoring recording from core file.\n");
2463 /* Now need to find our special note section. */
2464 osec
= bfd_get_section_by_name (core_bfd
, "null0");
2466 fprintf_unfiltered (gdb_stdlog
, "Find precord section %s.\n",
2467 osec
? "succeeded" : "failed");
2470 osec_size
= bfd_section_size (core_bfd
, osec
);
2472 fprintf_unfiltered (gdb_stdlog
, "%s", bfd_section_name (core_bfd
, osec
));
2474 /* Check the magic code. */
2475 bfdcore_read (core_bfd
, osec
, &magic
, sizeof (magic
), &bfd_offset
);
2476 if (magic
!= RECORD_FILE_MAGIC
)
2477 error (_("Version mis-match or file format error in core file %s."),
2478 bfd_get_filename (core_bfd
));
2480 fprintf_unfiltered (gdb_stdlog
,
2481 " Reading 4-byte magic cookie "
2482 "RECORD_FILE_MAGIC (0x%s)\n",
2483 phex_nz (netorder32 (magic
), 4));
2485 /* Restore the entries in recfd into record_arch_list_head and
2486 record_arch_list_tail. */
2487 record_arch_list_head
= NULL
;
2488 record_arch_list_tail
= NULL
;
2489 record_insn_num
= 0;
2490 old_cleanups
= make_cleanup (record_arch_list_cleanups
, 0);
2491 regcache
= get_current_regcache ();
2496 uint32_t regnum
, len
, signal
, count
;
2499 /* We are finished when offset reaches osec_size. */
2500 if (bfd_offset
>= osec_size
)
2502 bfdcore_read (core_bfd
, osec
, &rectype
, sizeof (rectype
), &bfd_offset
);
2506 case record_reg
: /* reg */
2507 /* Get register number to regnum. */
2508 bfdcore_read (core_bfd
, osec
, ®num
,
2509 sizeof (regnum
), &bfd_offset
);
2510 regnum
= netorder32 (regnum
);
2512 rec
= record_reg_alloc (regcache
, regnum
);
2515 bfdcore_read (core_bfd
, osec
, record_get_loc (rec
),
2516 rec
->u
.reg
.len
, &bfd_offset
);
2519 fprintf_unfiltered (gdb_stdlog
,
2520 " Reading register %d (1 "
2521 "plus %lu plus %d bytes)\n",
2523 (unsigned long) sizeof (regnum
),
2527 case record_mem
: /* mem */
2529 bfdcore_read (core_bfd
, osec
, &len
,
2530 sizeof (len
), &bfd_offset
);
2531 len
= netorder32 (len
);
2534 bfdcore_read (core_bfd
, osec
, &addr
,
2535 sizeof (addr
), &bfd_offset
);
2536 addr
= netorder64 (addr
);
2538 rec
= record_mem_alloc (addr
, len
);
2541 bfdcore_read (core_bfd
, osec
, record_get_loc (rec
),
2542 rec
->u
.mem
.len
, &bfd_offset
);
2545 fprintf_unfiltered (gdb_stdlog
,
2546 " Reading memory %s (1 plus "
2547 "%lu plus %lu plus %d bytes)\n",
2548 paddress (get_current_arch (),
2550 (unsigned long) sizeof (addr
),
2551 (unsigned long) sizeof (len
),
2555 case record_end
: /* end */
2556 rec
= record_end_alloc ();
2559 /* Get signal value. */
2560 bfdcore_read (core_bfd
, osec
, &signal
,
2561 sizeof (signal
), &bfd_offset
);
2562 signal
= netorder32 (signal
);
2563 rec
->u
.end
.sigval
= signal
;
2565 /* Get insn count. */
2566 bfdcore_read (core_bfd
, osec
, &count
,
2567 sizeof (count
), &bfd_offset
);
2568 count
= netorder32 (count
);
2569 rec
->u
.end
.insn_num
= count
;
2570 record_insn_count
= count
+ 1;
2572 fprintf_unfiltered (gdb_stdlog
,
2573 " Reading record_end (1 + "
2574 "%lu + %lu bytes), offset == %s\n",
2575 (unsigned long) sizeof (signal
),
2576 (unsigned long) sizeof (count
),
2577 paddress (get_current_arch (),
2582 error (_("Bad entry type in core file %s."),
2583 bfd_get_filename (core_bfd
));
2587 /* Add rec to record arch list. */
2588 record_arch_list_add (rec
);
2591 discard_cleanups (old_cleanups
);
2593 /* Add record_arch_list_head to the end of record list. */
2594 record_first
.next
= record_arch_list_head
;
2595 record_arch_list_head
->prev
= &record_first
;
2596 record_arch_list_tail
->next
= NULL
;
2597 record_list
= &record_first
;
2599 /* Update record_insn_max_num. */
2600 if (record_insn_num
> record_insn_max_num
)
2602 record_insn_max_num
= record_insn_num
;
2603 warning (_("Auto increase record/replay buffer limit to %d."),
2604 record_insn_max_num
);
2608 printf_filtered (_("Restored records from core file %s.\n"),
2609 bfd_get_filename (core_bfd
));
2611 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
2614 /* bfdcore_write -- write bytes into a core file section. */
2617 bfdcore_write (bfd
*obfd
, asection
*osec
, void *buf
, int len
, int *offset
)
2619 int ret
= bfd_set_section_contents (obfd
, osec
, buf
, *offset
, len
);
2624 error (_("Failed to write %d bytes to core file %s ('%s')."),
2625 len
, bfd_get_filename (obfd
),
2626 bfd_errmsg (bfd_get_error ()));
2629 /* Restore the execution log from a file. We use a modified elf
2630 corefile format, with an extra section for our data. */
2633 cmd_record_restore (char *args
, int from_tty
)
2635 core_file_command (args
, from_tty
);
2636 record_open (args
, from_tty
);
2640 record_save_cleanups (void *data
)
2643 char *pathname
= xstrdup (bfd_get_filename (obfd
));
2645 gdb_bfd_unref (obfd
);
2650 /* Save the execution log to a file. We use a modified elf corefile
2651 format, with an extra section for our data. */
2654 cmd_record_save (char *args
, int from_tty
)
2656 char *recfilename
, recfilename_buffer
[40];
2657 struct record_entry
*cur_record_list
;
2659 struct regcache
*regcache
;
2660 struct gdbarch
*gdbarch
;
2661 struct cleanup
*old_cleanups
;
2662 struct cleanup
*set_cleanups
;
2665 asection
*osec
= NULL
;
2668 if (strcmp (current_target
.to_shortname
, "record") != 0)
2669 error (_("This command can only be used with target 'record'.\n"
2670 "Use 'target record' first.\n"));
2676 /* Default recfile name is "gdb_record.PID". */
2677 snprintf (recfilename_buffer
, sizeof (recfilename_buffer
),
2678 "gdb_record.%d", PIDGET (inferior_ptid
));
2679 recfilename
= recfilename_buffer
;
2682 /* Open the save file. */
2684 fprintf_unfiltered (gdb_stdlog
, "Saving execution log to core file '%s'\n",
2687 /* Open the output file. */
2688 obfd
= create_gcore_bfd (recfilename
);
2689 old_cleanups
= make_cleanup (record_save_cleanups
, obfd
);
2691 /* Save the current record entry to "cur_record_list". */
2692 cur_record_list
= record_list
;
2694 /* Get the values of regcache and gdbarch. */
2695 regcache
= get_current_regcache ();
2696 gdbarch
= get_regcache_arch (regcache
);
2698 /* Disable the GDB operation record. */
2699 set_cleanups
= record_gdb_operation_disable_set ();
2701 /* Reverse execute to the begin of record list. */
2704 /* Check for beginning and end of log. */
2705 if (record_list
== &record_first
)
2708 record_exec_insn (regcache
, gdbarch
, record_list
);
2710 if (record_list
->prev
)
2711 record_list
= record_list
->prev
;
2714 /* Compute the size needed for the extra bfd section. */
2715 save_size
= 4; /* magic cookie */
2716 for (record_list
= record_first
.next
; record_list
;
2717 record_list
= record_list
->next
)
2718 switch (record_list
->type
)
2721 save_size
+= 1 + 4 + 4;
2724 save_size
+= 1 + 4 + record_list
->u
.reg
.len
;
2727 save_size
+= 1 + 4 + 8 + record_list
->u
.mem
.len
;
2731 /* Make the new bfd section. */
2732 osec
= bfd_make_section_anyway_with_flags (obfd
, "precord",
2736 error (_("Failed to create 'precord' section for corefile %s: %s"),
2738 bfd_errmsg (bfd_get_error ()));
2739 bfd_set_section_size (obfd
, osec
, save_size
);
2740 bfd_set_section_vma (obfd
, osec
, 0);
2741 bfd_set_section_alignment (obfd
, osec
, 0);
2742 bfd_section_lma (obfd
, osec
) = 0;
2744 /* Save corefile state. */
2745 write_gcore_file (obfd
);
2747 /* Write out the record log. */
2748 /* Write the magic code. */
2749 magic
= RECORD_FILE_MAGIC
;
2751 fprintf_unfiltered (gdb_stdlog
,
2752 " Writing 4-byte magic cookie "
2753 "RECORD_FILE_MAGIC (0x%s)\n",
2754 phex_nz (magic
, 4));
2755 bfdcore_write (obfd
, osec
, &magic
, sizeof (magic
), &bfd_offset
);
2757 /* Save the entries to recfd and forward execute to the end of
2759 record_list
= &record_first
;
2763 if (record_list
!= &record_first
)
2766 uint32_t regnum
, len
, signal
, count
;
2769 type
= record_list
->type
;
2770 bfdcore_write (obfd
, osec
, &type
, sizeof (type
), &bfd_offset
);
2772 switch (record_list
->type
)
2774 case record_reg
: /* reg */
2776 fprintf_unfiltered (gdb_stdlog
,
2777 " Writing register %d (1 "
2778 "plus %lu plus %d bytes)\n",
2779 record_list
->u
.reg
.num
,
2780 (unsigned long) sizeof (regnum
),
2781 record_list
->u
.reg
.len
);
2784 regnum
= netorder32 (record_list
->u
.reg
.num
);
2785 bfdcore_write (obfd
, osec
, ®num
,
2786 sizeof (regnum
), &bfd_offset
);
2789 bfdcore_write (obfd
, osec
, record_get_loc (record_list
),
2790 record_list
->u
.reg
.len
, &bfd_offset
);
2793 case record_mem
: /* mem */
2795 fprintf_unfiltered (gdb_stdlog
,
2796 " Writing memory %s (1 plus "
2797 "%lu plus %lu plus %d bytes)\n",
2799 record_list
->u
.mem
.addr
),
2800 (unsigned long) sizeof (addr
),
2801 (unsigned long) sizeof (len
),
2802 record_list
->u
.mem
.len
);
2805 len
= netorder32 (record_list
->u
.mem
.len
);
2806 bfdcore_write (obfd
, osec
, &len
, sizeof (len
), &bfd_offset
);
2808 /* Write memaddr. */
2809 addr
= netorder64 (record_list
->u
.mem
.addr
);
2810 bfdcore_write (obfd
, osec
, &addr
,
2811 sizeof (addr
), &bfd_offset
);
2814 bfdcore_write (obfd
, osec
, record_get_loc (record_list
),
2815 record_list
->u
.mem
.len
, &bfd_offset
);
2820 fprintf_unfiltered (gdb_stdlog
,
2821 " Writing record_end (1 + "
2822 "%lu + %lu bytes)\n",
2823 (unsigned long) sizeof (signal
),
2824 (unsigned long) sizeof (count
));
2825 /* Write signal value. */
2826 signal
= netorder32 (record_list
->u
.end
.sigval
);
2827 bfdcore_write (obfd
, osec
, &signal
,
2828 sizeof (signal
), &bfd_offset
);
2830 /* Write insn count. */
2831 count
= netorder32 (record_list
->u
.end
.insn_num
);
2832 bfdcore_write (obfd
, osec
, &count
,
2833 sizeof (count
), &bfd_offset
);
2838 /* Execute entry. */
2839 record_exec_insn (regcache
, gdbarch
, record_list
);
2841 if (record_list
->next
)
2842 record_list
= record_list
->next
;
2847 /* Reverse execute to cur_record_list. */
2850 /* Check for beginning and end of log. */
2851 if (record_list
== cur_record_list
)
2854 record_exec_insn (regcache
, gdbarch
, record_list
);
2856 if (record_list
->prev
)
2857 record_list
= record_list
->prev
;
2860 do_cleanups (set_cleanups
);
2861 gdb_bfd_unref (obfd
);
2862 discard_cleanups (old_cleanups
);
2865 printf_filtered (_("Saved core file %s with execution log.\n"),
2869 /* record_goto_insn -- rewind the record log (forward or backward,
2870 depending on DIR) to the given entry, changing the program state
2874 record_goto_insn (struct record_entry
*entry
,
2875 enum exec_direction_kind dir
)
2877 struct cleanup
*set_cleanups
= record_gdb_operation_disable_set ();
2878 struct regcache
*regcache
= get_current_regcache ();
2879 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
2881 /* Assume everything is valid: we will hit the entry,
2882 and we will not hit the end of the recording. */
2884 if (dir
== EXEC_FORWARD
)
2885 record_list
= record_list
->next
;
2889 record_exec_insn (regcache
, gdbarch
, record_list
);
2890 if (dir
== EXEC_REVERSE
)
2891 record_list
= record_list
->prev
;
2893 record_list
= record_list
->next
;
2894 } while (record_list
!= entry
);
2895 do_cleanups (set_cleanups
);
2898 /* "record goto" command. Argument is an instruction number,
2899 as given by "info record".
2901 Rewinds the recording (forward or backward) to the given instruction. */
2904 cmd_record_goto (char *arg
, int from_tty
)
2906 struct record_entry
*p
= NULL
;
2907 ULONGEST target_insn
= 0;
2909 if (arg
== NULL
|| *arg
== '\0')
2910 error (_("Command requires an argument (insn number to go to)."));
2912 if (strncmp (arg
, "start", strlen ("start")) == 0
2913 || strncmp (arg
, "begin", strlen ("begin")) == 0)
2915 /* Special case. Find first insn. */
2916 for (p
= &record_first
; p
!= NULL
; p
= p
->next
)
2917 if (p
->type
== record_end
)
2920 target_insn
= p
->u
.end
.insn_num
;
2922 else if (strncmp (arg
, "end", strlen ("end")) == 0)
2924 /* Special case. Find last insn. */
2925 for (p
= record_list
; p
->next
!= NULL
; p
= p
->next
)
2927 for (; p
!= NULL
; p
= p
->prev
)
2928 if (p
->type
== record_end
)
2931 target_insn
= p
->u
.end
.insn_num
;
2935 /* General case. Find designated insn. */
2936 target_insn
= parse_and_eval_long (arg
);
2938 for (p
= &record_first
; p
!= NULL
; p
= p
->next
)
2939 if (p
->type
== record_end
&& p
->u
.end
.insn_num
== target_insn
)
2944 error (_("Target insn '%s' not found."), arg
);
2945 else if (p
== record_list
)
2946 error (_("Already at insn '%s'."), arg
);
2947 else if (p
->u
.end
.insn_num
> record_list
->u
.end
.insn_num
)
2949 printf_filtered (_("Go forward to insn number %s\n"),
2950 pulongest (target_insn
));
2951 record_goto_insn (p
, EXEC_FORWARD
);
2955 printf_filtered (_("Go backward to insn number %s\n"),
2956 pulongest (target_insn
));
2957 record_goto_insn (p
, EXEC_REVERSE
);
2959 registers_changed ();
2960 reinit_frame_cache ();
2961 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
2964 /* Provide a prototype to silence -Wmissing-prototypes. */
2965 extern initialize_file_ftype _initialize_record
;
2968 _initialize_record (void)
2970 struct cmd_list_element
*c
;
2972 /* Init record_first. */
2973 record_first
.prev
= NULL
;
2974 record_first
.next
= NULL
;
2975 record_first
.type
= record_end
;
2978 add_target (&record_ops
);
2979 init_record_core_ops ();
2980 add_target (&record_core_ops
);
2982 add_setshow_zinteger_cmd ("record", no_class
, &record_debug
,
2983 _("Set debugging of record/replay feature."),
2984 _("Show debugging of record/replay feature."),
2985 _("When enabled, debugging output for "
2986 "record/replay feature is displayed."),
2987 NULL
, show_record_debug
, &setdebuglist
,
2990 c
= add_prefix_cmd ("record", class_obscure
, cmd_record_start
,
2991 _("Abbreviated form of \"target record\" command."),
2992 &record_cmdlist
, "record ", 0, &cmdlist
);
2993 set_cmd_completer (c
, filename_completer
);
2995 add_com_alias ("rec", "record", class_obscure
, 1);
2996 add_prefix_cmd ("record", class_support
, set_record_command
,
2997 _("Set record options"), &set_record_cmdlist
,
2998 "set record ", 0, &setlist
);
2999 add_alias_cmd ("rec", "record", class_obscure
, 1, &setlist
);
3000 add_prefix_cmd ("record", class_support
, show_record_command
,
3001 _("Show record options"), &show_record_cmdlist
,
3002 "show record ", 0, &showlist
);
3003 add_alias_cmd ("rec", "record", class_obscure
, 1, &showlist
);
3004 add_prefix_cmd ("record", class_support
, info_record_command
,
3005 _("Info record options"), &info_record_cmdlist
,
3006 "info record ", 0, &infolist
);
3007 add_alias_cmd ("rec", "record", class_obscure
, 1, &infolist
);
3009 c
= add_cmd ("save", class_obscure
, cmd_record_save
,
3010 _("Save the execution log to a file.\n\
3011 Argument is optional filename.\n\
3012 Default filename is 'gdb_record.<process_id>'."),
3014 set_cmd_completer (c
, filename_completer
);
3016 c
= add_cmd ("restore", class_obscure
, cmd_record_restore
,
3017 _("Restore the execution log from a file.\n\
3018 Argument is filename. File must be created with 'record save'."),
3020 set_cmd_completer (c
, filename_completer
);
3022 add_cmd ("delete", class_obscure
, cmd_record_delete
,
3023 _("Delete the rest of execution log and start recording it anew."),
3025 add_alias_cmd ("d", "delete", class_obscure
, 1, &record_cmdlist
);
3026 add_alias_cmd ("del", "delete", class_obscure
, 1, &record_cmdlist
);
3028 add_cmd ("stop", class_obscure
, cmd_record_stop
,
3029 _("Stop the record/replay target."),
3031 add_alias_cmd ("s", "stop", class_obscure
, 1, &record_cmdlist
);
3033 /* Record instructions number limit command. */
3034 add_setshow_boolean_cmd ("stop-at-limit", no_class
,
3035 &record_stop_at_limit
, _("\
3036 Set whether record/replay stops when record/replay buffer becomes full."), _("\
3037 Show whether record/replay stops when record/replay buffer becomes full."),
3038 _("Default is ON.\n\
3039 When ON, if the record/replay buffer becomes full, ask user what to do.\n\
3040 When OFF, if the record/replay buffer becomes full,\n\
3041 delete the oldest recorded instruction to make room for each new one."),
3043 &set_record_cmdlist
, &show_record_cmdlist
);
3044 add_setshow_uinteger_cmd ("insn-number-max", no_class
,
3045 &record_insn_max_num
,
3046 _("Set record/replay buffer limit."),
3047 _("Show record/replay buffer limit."), _("\
3048 Set the maximum number of instructions to be stored in the\n\
3049 record/replay buffer. Zero means unlimited. Default is 200000."),
3050 set_record_insn_max_num
,
3051 NULL
, &set_record_cmdlist
, &show_record_cmdlist
);
3053 add_cmd ("goto", class_obscure
, cmd_record_goto
, _("\
3054 Restore the program to its state at instruction number N.\n\
3055 Argument is instruction number, as shown by 'info record'."),
3058 add_setshow_boolean_cmd ("memory-query", no_class
,
3059 &record_memory_query
, _("\
3060 Set whether query if PREC cannot record memory change of next instruction."),
3062 Show whether query if PREC cannot record memory change of next instruction."),
3065 When ON, query if PREC cannot record memory change of next instruction."),
3067 &set_record_cmdlist
, &show_record_cmdlist
);