1 /* Process record and replay target for GDB, the GNU debugger.
3 Copyright (C) 2013 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "gdbthread.h"
24 #include "event-top.h"
25 #include "exceptions.h"
26 #include "completer.h"
27 #include "arch-utils.h"
31 #include "record-full.h"
34 #include "event-loop.h"
41 /* This module implements "target record-full", also known as "process
42 record and replay". This target sits on top of a "normal" target
43 (a target that "has execution"), and provides a record and replay
44 functionality, including reverse debugging.
46 Target record has two modes: recording, and replaying.
48 In record mode, we intercept the to_resume and to_wait methods.
49 Whenever gdb resumes the target, we run the target in single step
50 mode, and we build up an execution log in which, for each executed
51 instruction, we record all changes in memory and register state.
52 This is invisible to the user, to whom it just looks like an
53 ordinary debugging session (except for performance degredation).
55 In replay mode, instead of actually letting the inferior run as a
56 process, we simulate its execution by playing back the recorded
57 execution log. For each instruction in the log, we simulate the
58 instruction's side effects by duplicating the changes that it would
59 have made on memory and registers. */
61 #define DEFAULT_RECORD_INSN_MAX_NUM 200000
63 #define RECORD_IS_REPLAY \
64 (record_list->next || execution_direction == EXEC_REVERSE)
66 #define RECORD_FILE_MAGIC netorder32(0x20091016)
68 /* These are the core structs of the process record functionality.
70 A record_entry is a record of the value change of a register
71 ("record_reg") or a part of memory ("record_mem"). And each
72 instruction must have a struct record_entry ("record_end") that
73 indicates that this is the last struct record_entry of this
76 Each struct record_entry is linked to "record_list" by "prev" and
79 struct record_mem_entry
83 /* Set this flag if target memory for this entry
84 can no longer be accessed. */
85 int mem_entry_not_accessible
;
89 gdb_byte buf
[sizeof (gdb_byte
*)];
93 struct record_reg_entry
100 gdb_byte buf
[2 * sizeof (gdb_byte
*)];
104 struct record_end_entry
106 enum gdb_signal sigval
;
117 /* This is the data structure that makes up the execution log.
119 The execution log consists of a single linked list of entries
120 of type "struct record_entry". It is doubly linked so that it
121 can be traversed in either direction.
123 The start of the list is anchored by a struct called
124 "record_first". The pointer "record_list" either points to the
125 last entry that was added to the list (in record mode), or to the
126 next entry in the list that will be executed (in replay mode).
128 Each list element (struct record_entry), in addition to next and
129 prev pointers, consists of a union of three entry types: mem, reg,
130 and end. A field called "type" determines which entry type is
131 represented by a given list element.
133 Each instruction that is added to the execution log is represented
134 by a variable number of list elements ('entries'). The instruction
135 will have one "reg" entry for each register that is changed by
136 executing the instruction (including the PC in every case). It
137 will also have one "mem" entry for each memory change. Finally,
138 each instruction will have an "end" entry that separates it from
139 the changes associated with the next instruction. */
143 struct record_entry
*prev
;
144 struct record_entry
*next
;
145 enum record_type type
;
149 struct record_reg_entry reg
;
151 struct record_mem_entry mem
;
153 struct record_end_entry end
;
157 /* If true, query if PREC cannot record memory
158 change of next instruction. */
159 int record_memory_query
= 0;
161 struct record_core_buf_entry
163 struct record_core_buf_entry
*prev
;
164 struct target_section
*p
;
168 /* Record buf with core target. */
169 static gdb_byte
*record_core_regbuf
= NULL
;
170 static struct target_section
*record_core_start
;
171 static struct target_section
*record_core_end
;
172 static struct record_core_buf_entry
*record_core_buf_list
= NULL
;
174 /* The following variables are used for managing the linked list that
175 represents the execution log.
177 record_first is the anchor that holds down the beginning of the list.
179 record_list serves two functions:
180 1) In record mode, it anchors the end of the list.
181 2) In replay mode, it traverses the list and points to
182 the next instruction that must be emulated.
184 record_arch_list_head and record_arch_list_tail are used to manage
185 a separate list, which is used to build up the change elements of
186 the currently executing instruction during record mode. When this
187 instruction has been completely annotated in the "arch list", it
188 will be appended to the main execution log. */
190 static struct record_entry record_first
;
191 static struct record_entry
*record_list
= &record_first
;
192 static struct record_entry
*record_arch_list_head
= NULL
;
193 static struct record_entry
*record_arch_list_tail
= NULL
;
195 /* 1 ask user. 0 auto delete the last struct record_entry. */
196 static int record_stop_at_limit
= 1;
197 /* Maximum allowed number of insns in execution log. */
198 static unsigned int record_insn_max_num
= DEFAULT_RECORD_INSN_MAX_NUM
;
199 /* Actual count of insns presently in execution log. */
200 static int record_insn_num
= 0;
201 /* Count of insns logged so far (may be larger
202 than count of insns presently in execution log). */
203 static ULONGEST record_insn_count
;
205 /* The target_ops of process record. */
206 static struct target_ops record_ops
;
207 static struct target_ops record_core_ops
;
209 /* Command lists for "set/show record full". */
210 static struct cmd_list_element
*set_record_full_cmdlist
;
211 static struct cmd_list_element
*show_record_full_cmdlist
;
213 /* Command list for "record full". */
214 static struct cmd_list_element
*record_full_cmdlist
;
216 /* The beneath function pointers. */
217 static struct target_ops
*record_beneath_to_resume_ops
;
218 static void (*record_beneath_to_resume
) (struct target_ops
*, ptid_t
, int,
220 static struct target_ops
*record_beneath_to_wait_ops
;
221 static ptid_t (*record_beneath_to_wait
) (struct target_ops
*, ptid_t
,
222 struct target_waitstatus
*,
224 static struct target_ops
*record_beneath_to_store_registers_ops
;
225 static void (*record_beneath_to_store_registers
) (struct target_ops
*,
228 static struct target_ops
*record_beneath_to_xfer_partial_ops
;
229 static LONGEST (*record_beneath_to_xfer_partial
) (struct target_ops
*ops
,
230 enum target_object object
,
233 const gdb_byte
*writebuf
,
236 static int (*record_beneath_to_insert_breakpoint
) (struct gdbarch
*,
237 struct bp_target_info
*);
238 static int (*record_beneath_to_remove_breakpoint
) (struct gdbarch
*,
239 struct bp_target_info
*);
240 static int (*record_beneath_to_stopped_by_watchpoint
) (void);
241 static int (*record_beneath_to_stopped_data_address
) (struct target_ops
*,
243 static void (*record_beneath_to_async
) (void (*) (enum inferior_event_type
, void *), void *);
245 static void record_goto_insn (struct record_entry
*entry
,
246 enum exec_direction_kind dir
);
247 static void record_save (char *recfilename
);
249 /* Alloc and free functions for record_reg, record_mem, and record_end
252 /* Alloc a record_reg record entry. */
254 static inline struct record_entry
*
255 record_reg_alloc (struct regcache
*regcache
, int regnum
)
257 struct record_entry
*rec
;
258 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
260 rec
= (struct record_entry
*) xcalloc (1, sizeof (struct record_entry
));
261 rec
->type
= record_reg
;
262 rec
->u
.reg
.num
= regnum
;
263 rec
->u
.reg
.len
= register_size (gdbarch
, regnum
);
264 if (rec
->u
.reg
.len
> sizeof (rec
->u
.reg
.u
.buf
))
265 rec
->u
.reg
.u
.ptr
= (gdb_byte
*) xmalloc (rec
->u
.reg
.len
);
270 /* Free a record_reg record entry. */
273 record_reg_release (struct record_entry
*rec
)
275 gdb_assert (rec
->type
== record_reg
);
276 if (rec
->u
.reg
.len
> sizeof (rec
->u
.reg
.u
.buf
))
277 xfree (rec
->u
.reg
.u
.ptr
);
281 /* Alloc a record_mem record entry. */
283 static inline struct record_entry
*
284 record_mem_alloc (CORE_ADDR addr
, int len
)
286 struct record_entry
*rec
;
288 rec
= (struct record_entry
*) xcalloc (1, sizeof (struct record_entry
));
289 rec
->type
= record_mem
;
290 rec
->u
.mem
.addr
= addr
;
291 rec
->u
.mem
.len
= len
;
292 if (rec
->u
.mem
.len
> sizeof (rec
->u
.mem
.u
.buf
))
293 rec
->u
.mem
.u
.ptr
= (gdb_byte
*) xmalloc (len
);
298 /* Free a record_mem record entry. */
301 record_mem_release (struct record_entry
*rec
)
303 gdb_assert (rec
->type
== record_mem
);
304 if (rec
->u
.mem
.len
> sizeof (rec
->u
.mem
.u
.buf
))
305 xfree (rec
->u
.mem
.u
.ptr
);
309 /* Alloc a record_end record entry. */
311 static inline struct record_entry
*
312 record_end_alloc (void)
314 struct record_entry
*rec
;
316 rec
= (struct record_entry
*) xcalloc (1, sizeof (struct record_entry
));
317 rec
->type
= record_end
;
322 /* Free a record_end record entry. */
325 record_end_release (struct record_entry
*rec
)
330 /* Free one record entry, any type.
331 Return entry->type, in case caller wants to know. */
333 static inline enum record_type
334 record_entry_release (struct record_entry
*rec
)
336 enum record_type type
= rec
->type
;
340 record_reg_release (rec
);
343 record_mem_release (rec
);
346 record_end_release (rec
);
352 /* Free all record entries in list pointed to by REC. */
355 record_list_release (struct record_entry
*rec
)
366 record_entry_release (rec
->next
);
369 if (rec
== &record_first
)
372 record_first
.next
= NULL
;
375 record_entry_release (rec
);
378 /* Free all record entries forward of the given list position. */
381 record_list_release_following (struct record_entry
*rec
)
383 struct record_entry
*tmp
= rec
->next
;
389 if (record_entry_release (tmp
) == record_end
)
398 /* Delete the first instruction from the beginning of the log, to make
399 room for adding a new instruction at the end of the log.
401 Note -- this function does not modify record_insn_num. */
404 record_list_release_first (void)
406 struct record_entry
*tmp
;
408 if (!record_first
.next
)
411 /* Loop until a record_end. */
414 /* Cut record_first.next out of the linked list. */
415 tmp
= record_first
.next
;
416 record_first
.next
= tmp
->next
;
417 tmp
->next
->prev
= &record_first
;
419 /* tmp is now isolated, and can be deleted. */
420 if (record_entry_release (tmp
) == record_end
)
421 break; /* End loop at first record_end. */
423 if (!record_first
.next
)
425 gdb_assert (record_insn_num
== 1);
426 break; /* End loop when list is empty. */
431 /* Add a struct record_entry to record_arch_list. */
434 record_arch_list_add (struct record_entry
*rec
)
436 if (record_debug
> 1)
437 fprintf_unfiltered (gdb_stdlog
,
438 "Process record: record_arch_list_add %s.\n",
439 host_address_to_string (rec
));
441 if (record_arch_list_tail
)
443 record_arch_list_tail
->next
= rec
;
444 rec
->prev
= record_arch_list_tail
;
445 record_arch_list_tail
= rec
;
449 record_arch_list_head
= rec
;
450 record_arch_list_tail
= rec
;
454 /* Return the value storage location of a record entry. */
455 static inline gdb_byte
*
456 record_get_loc (struct record_entry
*rec
)
460 if (rec
->u
.mem
.len
> sizeof (rec
->u
.mem
.u
.buf
))
461 return rec
->u
.mem
.u
.ptr
;
463 return rec
->u
.mem
.u
.buf
;
465 if (rec
->u
.reg
.len
> sizeof (rec
->u
.reg
.u
.buf
))
466 return rec
->u
.reg
.u
.ptr
;
468 return rec
->u
.reg
.u
.buf
;
471 gdb_assert_not_reached ("unexpected record_entry type");
476 /* Record the value of a register NUM to record_arch_list. */
479 record_arch_list_add_reg (struct regcache
*regcache
, int regnum
)
481 struct record_entry
*rec
;
483 if (record_debug
> 1)
484 fprintf_unfiltered (gdb_stdlog
,
485 "Process record: add register num = %d to "
489 rec
= record_reg_alloc (regcache
, regnum
);
491 regcache_raw_read (regcache
, regnum
, record_get_loc (rec
));
493 record_arch_list_add (rec
);
498 /* Record the value of a region of memory whose address is ADDR and
499 length is LEN to record_arch_list. */
502 record_arch_list_add_mem (CORE_ADDR addr
, int len
)
504 struct record_entry
*rec
;
506 if (record_debug
> 1)
507 fprintf_unfiltered (gdb_stdlog
,
508 "Process record: add mem addr = %s len = %d to "
510 paddress (target_gdbarch (), addr
), len
);
512 if (!addr
) /* FIXME: Why? Some arch must permit it... */
515 rec
= record_mem_alloc (addr
, len
);
517 if (record_read_memory (target_gdbarch (), addr
, record_get_loc (rec
), len
))
519 record_mem_release (rec
);
523 record_arch_list_add (rec
);
528 /* Add a record_end type struct record_entry to record_arch_list. */
531 record_arch_list_add_end (void)
533 struct record_entry
*rec
;
535 if (record_debug
> 1)
536 fprintf_unfiltered (gdb_stdlog
,
537 "Process record: add end to arch list.\n");
539 rec
= record_end_alloc ();
540 rec
->u
.end
.sigval
= GDB_SIGNAL_0
;
541 rec
->u
.end
.insn_num
= ++record_insn_count
;
543 record_arch_list_add (rec
);
549 record_check_insn_num (int set_terminal
)
551 if (record_insn_max_num
)
553 gdb_assert (record_insn_num
<= record_insn_max_num
);
554 if (record_insn_num
== record_insn_max_num
)
556 /* Ask user what to do. */
557 if (record_stop_at_limit
)
562 target_terminal_ours ();
563 q
= yquery (_("Do you want to auto delete previous execution "
564 "log entries when record/replay buffer becomes "
565 "full (record full stop-at-limit)?"));
567 target_terminal_inferior ();
569 record_stop_at_limit
= 0;
571 error (_("Process record: stopped by user."));
578 record_arch_list_cleanups (void *ignore
)
580 record_list_release (record_arch_list_tail
);
583 /* Before inferior step (when GDB record the running message, inferior
584 only can step), GDB will call this function to record the values to
585 record_list. This function will call gdbarch_process_record to
586 record the running message of inferior and set them to
587 record_arch_list, and add it to record_list. */
590 record_message (struct regcache
*regcache
, enum gdb_signal signal
)
593 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
594 struct cleanup
*old_cleanups
= make_cleanup (record_arch_list_cleanups
, 0);
596 record_arch_list_head
= NULL
;
597 record_arch_list_tail
= NULL
;
599 /* Check record_insn_num. */
600 record_check_insn_num (1);
602 /* If gdb sends a signal value to target_resume,
603 save it in the 'end' field of the previous instruction.
605 Maybe process record should record what really happened,
606 rather than what gdb pretends has happened.
608 So if Linux delivered the signal to the child process during
609 the record mode, we will record it and deliver it again in
612 If user says "ignore this signal" during the record mode, then
613 it will be ignored again during the replay mode (no matter if
614 the user says something different, like "deliver this signal"
615 during the replay mode).
617 User should understand that nothing he does during the replay
618 mode will change the behavior of the child. If he tries,
619 then that is a user error.
621 But we should still deliver the signal to gdb during the replay,
622 if we delivered it during the recording. Therefore we should
623 record the signal during record_wait, not record_resume. */
624 if (record_list
!= &record_first
) /* FIXME better way to check */
626 gdb_assert (record_list
->type
== record_end
);
627 record_list
->u
.end
.sigval
= signal
;
630 if (signal
== GDB_SIGNAL_0
631 || !gdbarch_process_record_signal_p (gdbarch
))
632 ret
= gdbarch_process_record (gdbarch
,
634 regcache_read_pc (regcache
));
636 ret
= gdbarch_process_record_signal (gdbarch
,
641 error (_("Process record: inferior program stopped."));
643 error (_("Process record: failed to record execution log."));
645 discard_cleanups (old_cleanups
);
647 record_list
->next
= record_arch_list_head
;
648 record_arch_list_head
->prev
= record_list
;
649 record_list
= record_arch_list_tail
;
651 if (record_insn_num
== record_insn_max_num
&& record_insn_max_num
)
652 record_list_release_first ();
659 struct record_message_args
{
660 struct regcache
*regcache
;
661 enum gdb_signal signal
;
665 record_message_wrapper (void *args
)
667 struct record_message_args
*record_args
= args
;
669 return record_message (record_args
->regcache
, record_args
->signal
);
673 record_message_wrapper_safe (struct regcache
*regcache
,
674 enum gdb_signal signal
)
676 struct record_message_args args
;
678 args
.regcache
= regcache
;
679 args
.signal
= signal
;
681 return catch_errors (record_message_wrapper
, &args
, NULL
, RETURN_MASK_ALL
);
684 /* Set to 1 if record_store_registers and record_xfer_partial
685 doesn't need record. */
687 static int record_gdb_operation_disable
= 0;
690 record_gdb_operation_disable_set (void)
692 struct cleanup
*old_cleanups
= NULL
;
695 make_cleanup_restore_integer (&record_gdb_operation_disable
);
696 record_gdb_operation_disable
= 1;
701 /* Flag set to TRUE for target_stopped_by_watchpoint. */
702 static int record_hw_watchpoint
= 0;
704 /* Execute one instruction from the record log. Each instruction in
705 the log will be represented by an arbitrary sequence of register
706 entries and memory entries, followed by an 'end' entry. */
709 record_exec_insn (struct regcache
*regcache
, struct gdbarch
*gdbarch
,
710 struct record_entry
*entry
)
714 case record_reg
: /* reg */
716 gdb_byte reg
[MAX_REGISTER_SIZE
];
718 if (record_debug
> 1)
719 fprintf_unfiltered (gdb_stdlog
,
720 "Process record: record_reg %s to "
721 "inferior num = %d.\n",
722 host_address_to_string (entry
),
725 regcache_cooked_read (regcache
, entry
->u
.reg
.num
, reg
);
726 regcache_cooked_write (regcache
, entry
->u
.reg
.num
,
727 record_get_loc (entry
));
728 memcpy (record_get_loc (entry
), reg
, entry
->u
.reg
.len
);
732 case record_mem
: /* mem */
734 /* Nothing to do if the entry is flagged not_accessible. */
735 if (!entry
->u
.mem
.mem_entry_not_accessible
)
737 gdb_byte
*mem
= alloca (entry
->u
.mem
.len
);
739 if (record_debug
> 1)
740 fprintf_unfiltered (gdb_stdlog
,
741 "Process record: record_mem %s to "
742 "inferior addr = %s len = %d.\n",
743 host_address_to_string (entry
),
744 paddress (gdbarch
, entry
->u
.mem
.addr
),
747 if (record_read_memory (gdbarch
,
748 entry
->u
.mem
.addr
, mem
, entry
->u
.mem
.len
))
749 entry
->u
.mem
.mem_entry_not_accessible
= 1;
752 if (target_write_memory (entry
->u
.mem
.addr
,
753 record_get_loc (entry
),
756 entry
->u
.mem
.mem_entry_not_accessible
= 1;
758 warning (_("Process record: error writing memory at "
759 "addr = %s len = %d."),
760 paddress (gdbarch
, entry
->u
.mem
.addr
),
765 memcpy (record_get_loc (entry
), mem
, entry
->u
.mem
.len
);
767 /* We've changed memory --- check if a hardware
768 watchpoint should trap. Note that this
769 presently assumes the target beneath supports
770 continuable watchpoints. On non-continuable
771 watchpoints target, we'll want to check this
772 _before_ actually doing the memory change, and
773 not doing the change at all if the watchpoint
775 if (hardware_watchpoint_inserted_in_range
776 (get_regcache_aspace (regcache
),
777 entry
->u
.mem
.addr
, entry
->u
.mem
.len
))
778 record_hw_watchpoint
= 1;
787 static struct target_ops
*tmp_to_resume_ops
;
788 static void (*tmp_to_resume
) (struct target_ops
*, ptid_t
, int,
790 static struct target_ops
*tmp_to_wait_ops
;
791 static ptid_t (*tmp_to_wait
) (struct target_ops
*, ptid_t
,
792 struct target_waitstatus
*,
794 static struct target_ops
*tmp_to_store_registers_ops
;
795 static void (*tmp_to_store_registers
) (struct target_ops
*,
798 static struct target_ops
*tmp_to_xfer_partial_ops
;
799 static LONGEST (*tmp_to_xfer_partial
) (struct target_ops
*ops
,
800 enum target_object object
,
803 const gdb_byte
*writebuf
,
806 static int (*tmp_to_insert_breakpoint
) (struct gdbarch
*,
807 struct bp_target_info
*);
808 static int (*tmp_to_remove_breakpoint
) (struct gdbarch
*,
809 struct bp_target_info
*);
810 static int (*tmp_to_stopped_by_watchpoint
) (void);
811 static int (*tmp_to_stopped_data_address
) (struct target_ops
*, CORE_ADDR
*);
812 static int (*tmp_to_stopped_data_address
) (struct target_ops
*, CORE_ADDR
*);
813 static void (*tmp_to_async
) (void (*) (enum inferior_event_type
, void *), void *);
815 static void record_restore (void);
817 /* Asynchronous signal handle registered as event loop source for when
818 we have pending events ready to be passed to the core. */
820 static struct async_event_handler
*record_async_inferior_event_token
;
823 record_async_inferior_event_handler (gdb_client_data data
)
825 inferior_event_handler (INF_REG_EVENT
, NULL
);
828 /* Open the process record target. */
831 record_core_open_1 (char *name
, int from_tty
)
833 struct regcache
*regcache
= get_current_regcache ();
834 int regnum
= gdbarch_num_regs (get_regcache_arch (regcache
));
837 /* Get record_core_regbuf. */
838 target_fetch_registers (regcache
, -1);
839 record_core_regbuf
= xmalloc (MAX_REGISTER_SIZE
* regnum
);
840 for (i
= 0; i
< regnum
; i
++)
841 regcache_raw_collect (regcache
, i
,
842 record_core_regbuf
+ MAX_REGISTER_SIZE
* i
);
844 /* Get record_core_start and record_core_end. */
845 if (build_section_table (core_bfd
, &record_core_start
, &record_core_end
))
847 xfree (record_core_regbuf
);
848 record_core_regbuf
= NULL
;
849 error (_("\"%s\": Can't find sections: %s"),
850 bfd_get_filename (core_bfd
), bfd_errmsg (bfd_get_error ()));
853 push_target (&record_core_ops
);
857 /* "to_open" target method for 'live' processes. */
860 record_open_1 (char *name
, int from_tty
)
863 fprintf_unfiltered (gdb_stdlog
, "Process record: record_open\n");
866 if (!target_has_execution
)
867 error (_("Process record: the program is not being run."));
869 error (_("Process record target can't debug inferior in non-stop mode "
872 if (!gdbarch_process_record_p (target_gdbarch ()))
873 error (_("Process record: the current architecture doesn't support "
874 "record function."));
877 error (_("Could not find 'to_resume' method on the target stack."));
879 error (_("Could not find 'to_wait' method on the target stack."));
880 if (!tmp_to_store_registers
)
881 error (_("Could not find 'to_store_registers' "
882 "method on the target stack."));
883 if (!tmp_to_insert_breakpoint
)
884 error (_("Could not find 'to_insert_breakpoint' "
885 "method on the target stack."));
886 if (!tmp_to_remove_breakpoint
)
887 error (_("Could not find 'to_remove_breakpoint' "
888 "method on the target stack."));
889 if (!tmp_to_stopped_by_watchpoint
)
890 error (_("Could not find 'to_stopped_by_watchpoint' "
891 "method on the target stack."));
892 if (!tmp_to_stopped_data_address
)
893 error (_("Could not find 'to_stopped_data_address' "
894 "method on the target stack."));
896 push_target (&record_ops
);
899 static void record_init_record_breakpoints (void);
901 /* "to_open" target method. Open the process record target. */
904 record_open (char *name
, int from_tty
)
906 struct target_ops
*t
;
909 fprintf_unfiltered (gdb_stdlog
, "Process record: record_open\n");
911 /* Check if record target is already running. */
912 if (current_target
.to_stratum
== record_stratum
)
913 error (_("Process record target already running. Use \"record stop\" to "
914 "stop record target first."));
916 /* Reset the tmp beneath pointers. */
917 tmp_to_resume_ops
= NULL
;
918 tmp_to_resume
= NULL
;
919 tmp_to_wait_ops
= NULL
;
921 tmp_to_store_registers_ops
= NULL
;
922 tmp_to_store_registers
= NULL
;
923 tmp_to_xfer_partial_ops
= NULL
;
924 tmp_to_xfer_partial
= NULL
;
925 tmp_to_insert_breakpoint
= NULL
;
926 tmp_to_remove_breakpoint
= NULL
;
927 tmp_to_stopped_by_watchpoint
= NULL
;
928 tmp_to_stopped_data_address
= NULL
;
931 /* Set the beneath function pointers. */
932 for (t
= current_target
.beneath
; t
!= NULL
; t
= t
->beneath
)
936 tmp_to_resume
= t
->to_resume
;
937 tmp_to_resume_ops
= t
;
941 tmp_to_wait
= t
->to_wait
;
944 if (!tmp_to_store_registers
)
946 tmp_to_store_registers
= t
->to_store_registers
;
947 tmp_to_store_registers_ops
= t
;
949 if (!tmp_to_xfer_partial
)
951 tmp_to_xfer_partial
= t
->to_xfer_partial
;
952 tmp_to_xfer_partial_ops
= t
;
954 if (!tmp_to_insert_breakpoint
)
955 tmp_to_insert_breakpoint
= t
->to_insert_breakpoint
;
956 if (!tmp_to_remove_breakpoint
)
957 tmp_to_remove_breakpoint
= t
->to_remove_breakpoint
;
958 if (!tmp_to_stopped_by_watchpoint
)
959 tmp_to_stopped_by_watchpoint
= t
->to_stopped_by_watchpoint
;
960 if (!tmp_to_stopped_data_address
)
961 tmp_to_stopped_data_address
= t
->to_stopped_data_address
;
963 tmp_to_async
= t
->to_async
;
965 if (!tmp_to_xfer_partial
)
966 error (_("Could not find 'to_xfer_partial' method on the target stack."));
970 record_insn_count
= 0;
971 record_list
= &record_first
;
972 record_list
->next
= NULL
;
974 /* Set the tmp beneath pointers to beneath pointers. */
975 record_beneath_to_resume_ops
= tmp_to_resume_ops
;
976 record_beneath_to_resume
= tmp_to_resume
;
977 record_beneath_to_wait_ops
= tmp_to_wait_ops
;
978 record_beneath_to_wait
= tmp_to_wait
;
979 record_beneath_to_store_registers_ops
= tmp_to_store_registers_ops
;
980 record_beneath_to_store_registers
= tmp_to_store_registers
;
981 record_beneath_to_xfer_partial_ops
= tmp_to_xfer_partial_ops
;
982 record_beneath_to_xfer_partial
= tmp_to_xfer_partial
;
983 record_beneath_to_insert_breakpoint
= tmp_to_insert_breakpoint
;
984 record_beneath_to_remove_breakpoint
= tmp_to_remove_breakpoint
;
985 record_beneath_to_stopped_by_watchpoint
= tmp_to_stopped_by_watchpoint
;
986 record_beneath_to_stopped_data_address
= tmp_to_stopped_data_address
;
987 record_beneath_to_async
= tmp_to_async
;
990 record_core_open_1 (name
, from_tty
);
992 record_open_1 (name
, from_tty
);
994 /* Register extra event sources in the event loop. */
995 record_async_inferior_event_token
996 = create_async_event_handler (record_async_inferior_event_handler
,
999 record_init_record_breakpoints ();
1001 observer_notify_record_changed (current_inferior (), 1);
1004 /* "to_close" target method. Close the process record target. */
1007 record_close (int quitting
)
1009 struct record_core_buf_entry
*entry
;
1012 fprintf_unfiltered (gdb_stdlog
, "Process record: record_close\n");
1014 record_list_release (record_list
);
1016 /* Release record_core_regbuf. */
1017 if (record_core_regbuf
)
1019 xfree (record_core_regbuf
);
1020 record_core_regbuf
= NULL
;
1023 /* Release record_core_buf_list. */
1024 if (record_core_buf_list
)
1026 for (entry
= record_core_buf_list
->prev
; entry
; entry
= entry
->prev
)
1028 xfree (record_core_buf_list
);
1029 record_core_buf_list
= entry
;
1031 record_core_buf_list
= NULL
;
1034 if (record_async_inferior_event_token
)
1035 delete_async_event_handler (&record_async_inferior_event_token
);
1038 static int record_resume_step
= 0;
1040 /* True if we've been resumed, and so each record_wait call should
1041 advance execution. If this is false, record_wait will return a
1042 TARGET_WAITKIND_IGNORE. */
1043 static int record_resumed
= 0;
1045 /* The execution direction of the last resume we got. This is
1046 necessary for async mode. Vis (order is not strictly accurate):
1048 1. user has the global execution direction set to forward
1049 2. user does a reverse-step command
1050 3. record_resume is called with global execution direction
1051 temporarily switched to reverse
1052 4. GDB's execution direction is reverted back to forward
1053 5. target record notifies event loop there's an event to handle
1054 6. infrun asks the target which direction was it going, and switches
1055 the global execution direction accordingly (to reverse)
1056 7. infrun polls an event out of the record target, and handles it
1057 8. GDB goes back to the event loop, and goto #4.
1059 static enum exec_direction_kind record_execution_dir
= EXEC_FORWARD
;
1061 /* "to_resume" target method. Resume the process record target. */
1064 record_resume (struct target_ops
*ops
, ptid_t ptid
, int step
,
1065 enum gdb_signal signal
)
1067 record_resume_step
= step
;
1069 record_execution_dir
= execution_direction
;
1071 if (!RECORD_IS_REPLAY
)
1073 struct gdbarch
*gdbarch
= target_thread_architecture (ptid
);
1075 record_message (get_current_regcache (), signal
);
1079 /* This is not hard single step. */
1080 if (!gdbarch_software_single_step_p (gdbarch
))
1082 /* This is a normal continue. */
1087 /* This arch support soft sigle step. */
1088 if (single_step_breakpoints_inserted ())
1090 /* This is a soft single step. */
1091 record_resume_step
= 1;
1095 /* This is a continue.
1096 Try to insert a soft single step breakpoint. */
1097 if (!gdbarch_software_single_step (gdbarch
,
1098 get_current_frame ()))
1100 /* This system don't want use soft single step.
1101 Use hard sigle step. */
1108 /* Make sure the target beneath reports all signals. */
1109 target_pass_signals (0, NULL
);
1111 record_beneath_to_resume (record_beneath_to_resume_ops
,
1112 ptid
, step
, signal
);
1115 /* We are about to start executing the inferior (or simulate it),
1116 let's register it with the event loop. */
1117 if (target_can_async_p ())
1119 target_async (inferior_event_handler
, 0);
1120 /* Notify the event loop there's an event to wait for. We do
1121 most of the work in record_wait. */
1122 mark_async_event_handler (record_async_inferior_event_token
);
1126 static int record_get_sig
= 0;
1128 /* SIGINT signal handler, registered by "to_wait" method. */
1131 record_sig_handler (int signo
)
1134 fprintf_unfiltered (gdb_stdlog
, "Process record: get a signal\n");
1136 /* It will break the running inferior in replay mode. */
1137 record_resume_step
= 1;
1139 /* It will let record_wait set inferior status to get the signal
1145 record_wait_cleanups (void *ignore
)
1147 if (execution_direction
== EXEC_REVERSE
)
1149 if (record_list
->next
)
1150 record_list
= record_list
->next
;
1153 record_list
= record_list
->prev
;
1156 /* "to_wait" target method for process record target.
1158 In record mode, the target is always run in singlestep mode
1159 (even when gdb says to continue). The to_wait method intercepts
1160 the stop events and determines which ones are to be passed on to
1161 gdb. Most stop events are just singlestep events that gdb is not
1162 to know about, so the to_wait method just records them and keeps
1165 In replay mode, this function emulates the recorded execution log,
1166 one instruction at a time (forward or backward), and determines
1170 record_wait_1 (struct target_ops
*ops
,
1171 ptid_t ptid
, struct target_waitstatus
*status
,
1174 struct cleanup
*set_cleanups
= record_gdb_operation_disable_set ();
1177 fprintf_unfiltered (gdb_stdlog
,
1178 "Process record: record_wait "
1179 "record_resume_step = %d, record_resumed = %d, direction=%s\n",
1180 record_resume_step
, record_resumed
,
1181 record_execution_dir
== EXEC_FORWARD
? "forward" : "reverse");
1183 if (!record_resumed
)
1185 gdb_assert ((options
& TARGET_WNOHANG
) != 0);
1187 /* No interesting event. */
1188 status
->kind
= TARGET_WAITKIND_IGNORE
;
1189 return minus_one_ptid
;
1193 signal (SIGINT
, record_sig_handler
);
1195 if (!RECORD_IS_REPLAY
&& ops
!= &record_core_ops
)
1197 if (record_resume_step
)
1199 /* This is a single step. */
1200 return record_beneath_to_wait (record_beneath_to_wait_ops
,
1201 ptid
, status
, options
);
1205 /* This is not a single step. */
1208 struct gdbarch
*gdbarch
= target_thread_architecture (inferior_ptid
);
1212 ret
= record_beneath_to_wait (record_beneath_to_wait_ops
,
1213 ptid
, status
, options
);
1214 if (status
->kind
== TARGET_WAITKIND_IGNORE
)
1217 fprintf_unfiltered (gdb_stdlog
,
1218 "Process record: record_wait "
1219 "target beneath not done yet\n");
1223 if (single_step_breakpoints_inserted ())
1224 remove_single_step_breakpoints ();
1226 if (record_resume_step
)
1229 /* Is this a SIGTRAP? */
1230 if (status
->kind
== TARGET_WAITKIND_STOPPED
1231 && status
->value
.sig
== GDB_SIGNAL_TRAP
)
1233 struct regcache
*regcache
;
1234 struct address_space
*aspace
;
1236 /* Yes -- this is likely our single-step finishing,
1237 but check if there's any reason the core would be
1238 interested in the event. */
1240 registers_changed ();
1241 regcache
= get_current_regcache ();
1242 tmp_pc
= regcache_read_pc (regcache
);
1243 aspace
= get_regcache_aspace (regcache
);
1245 if (target_stopped_by_watchpoint ())
1247 /* Always interested in watchpoints. */
1249 else if (breakpoint_inserted_here_p (aspace
, tmp_pc
))
1251 /* There is a breakpoint here. Let the core
1253 if (software_breakpoint_inserted_here_p (aspace
, tmp_pc
))
1255 struct gdbarch
*gdbarch
1256 = get_regcache_arch (regcache
);
1257 CORE_ADDR decr_pc_after_break
1258 = gdbarch_decr_pc_after_break (gdbarch
);
1259 if (decr_pc_after_break
)
1260 regcache_write_pc (regcache
,
1261 tmp_pc
+ decr_pc_after_break
);
1266 /* This is a single-step trap. Record the
1267 insn and issue another step.
1268 FIXME: this part can be a random SIGTRAP too.
1269 But GDB cannot handle it. */
1272 if (!record_message_wrapper_safe (regcache
,
1275 status
->kind
= TARGET_WAITKIND_STOPPED
;
1276 status
->value
.sig
= GDB_SIGNAL_0
;
1280 if (gdbarch_software_single_step_p (gdbarch
))
1282 /* Try to insert the software single step breakpoint.
1283 If insert success, set step to 0. */
1284 set_executing (inferior_ptid
, 0);
1285 reinit_frame_cache ();
1286 if (gdbarch_software_single_step (gdbarch
,
1287 get_current_frame ()))
1289 set_executing (inferior_ptid
, 1);
1293 fprintf_unfiltered (gdb_stdlog
,
1294 "Process record: record_wait "
1295 "issuing one more step in the target beneath\n");
1296 record_beneath_to_resume (record_beneath_to_resume_ops
,
1303 /* The inferior is broken by a breakpoint or a signal. */
1312 struct regcache
*regcache
= get_current_regcache ();
1313 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1314 struct address_space
*aspace
= get_regcache_aspace (regcache
);
1315 int continue_flag
= 1;
1316 int first_record_end
= 1;
1317 struct cleanup
*old_cleanups
= make_cleanup (record_wait_cleanups
, 0);
1320 record_hw_watchpoint
= 0;
1321 status
->kind
= TARGET_WAITKIND_STOPPED
;
1323 /* Check breakpoint when forward execute. */
1324 if (execution_direction
== EXEC_FORWARD
)
1326 tmp_pc
= regcache_read_pc (regcache
);
1327 if (breakpoint_inserted_here_p (aspace
, tmp_pc
))
1329 int decr_pc_after_break
= gdbarch_decr_pc_after_break (gdbarch
);
1332 fprintf_unfiltered (gdb_stdlog
,
1333 "Process record: break at %s.\n",
1334 paddress (gdbarch
, tmp_pc
));
1336 if (decr_pc_after_break
1337 && !record_resume_step
1338 && software_breakpoint_inserted_here_p (aspace
, tmp_pc
))
1339 regcache_write_pc (regcache
,
1340 tmp_pc
+ decr_pc_after_break
);
1345 /* If GDB is in terminal_inferior mode, it will not get the signal.
1346 And in GDB replay mode, GDB doesn't need to be in terminal_inferior
1347 mode, because inferior will not executed.
1348 Then set it to terminal_ours to make GDB get the signal. */
1349 target_terminal_ours ();
1351 /* In EXEC_FORWARD mode, record_list points to the tail of prev
1353 if (execution_direction
== EXEC_FORWARD
&& record_list
->next
)
1354 record_list
= record_list
->next
;
1356 /* Loop over the record_list, looking for the next place to
1360 /* Check for beginning and end of log. */
1361 if (execution_direction
== EXEC_REVERSE
1362 && record_list
== &record_first
)
1364 /* Hit beginning of record log in reverse. */
1365 status
->kind
= TARGET_WAITKIND_NO_HISTORY
;
1368 if (execution_direction
!= EXEC_REVERSE
&& !record_list
->next
)
1370 /* Hit end of record log going forward. */
1371 status
->kind
= TARGET_WAITKIND_NO_HISTORY
;
1375 record_exec_insn (regcache
, gdbarch
, record_list
);
1377 if (record_list
->type
== record_end
)
1379 if (record_debug
> 1)
1380 fprintf_unfiltered (gdb_stdlog
,
1381 "Process record: record_end %s to "
1383 host_address_to_string (record_list
));
1385 if (first_record_end
&& execution_direction
== EXEC_REVERSE
)
1387 /* When reverse excute, the first record_end is the part of
1388 current instruction. */
1389 first_record_end
= 0;
1393 /* In EXEC_REVERSE mode, this is the record_end of prev
1395 In EXEC_FORWARD mode, this is the record_end of current
1398 if (record_resume_step
)
1400 if (record_debug
> 1)
1401 fprintf_unfiltered (gdb_stdlog
,
1402 "Process record: step.\n");
1406 /* check breakpoint */
1407 tmp_pc
= regcache_read_pc (regcache
);
1408 if (breakpoint_inserted_here_p (aspace
, tmp_pc
))
1410 int decr_pc_after_break
1411 = gdbarch_decr_pc_after_break (gdbarch
);
1414 fprintf_unfiltered (gdb_stdlog
,
1415 "Process record: break "
1417 paddress (gdbarch
, tmp_pc
));
1418 if (decr_pc_after_break
1419 && execution_direction
== EXEC_FORWARD
1420 && !record_resume_step
1421 && software_breakpoint_inserted_here_p (aspace
,
1423 regcache_write_pc (regcache
,
1424 tmp_pc
+ decr_pc_after_break
);
1428 if (record_hw_watchpoint
)
1431 fprintf_unfiltered (gdb_stdlog
,
1432 "Process record: hit hw "
1436 /* Check target signal */
1437 if (record_list
->u
.end
.sigval
!= GDB_SIGNAL_0
)
1438 /* FIXME: better way to check */
1445 if (execution_direction
== EXEC_REVERSE
)
1447 if (record_list
->prev
)
1448 record_list
= record_list
->prev
;
1452 if (record_list
->next
)
1453 record_list
= record_list
->next
;
1457 while (continue_flag
);
1461 status
->value
.sig
= GDB_SIGNAL_INT
;
1462 else if (record_list
->u
.end
.sigval
!= GDB_SIGNAL_0
)
1463 /* FIXME: better way to check */
1464 status
->value
.sig
= record_list
->u
.end
.sigval
;
1466 status
->value
.sig
= GDB_SIGNAL_TRAP
;
1468 discard_cleanups (old_cleanups
);
1471 signal (SIGINT
, handle_sigint
);
1473 do_cleanups (set_cleanups
);
1474 return inferior_ptid
;
1478 record_wait (struct target_ops
*ops
,
1479 ptid_t ptid
, struct target_waitstatus
*status
,
1484 return_ptid
= record_wait_1 (ops
, ptid
, status
, options
);
1485 if (status
->kind
!= TARGET_WAITKIND_IGNORE
)
1487 /* We're reporting a stop. Make sure any spurious
1488 target_wait(WNOHANG) doesn't advance the target until the
1489 core wants us resumed again. */
1496 record_stopped_by_watchpoint (void)
1498 if (RECORD_IS_REPLAY
)
1499 return record_hw_watchpoint
;
1501 return record_beneath_to_stopped_by_watchpoint ();
1504 /* "to_disconnect" method for process record target. */
1507 record_disconnect (struct target_ops
*target
, char *args
, int from_tty
)
1510 fprintf_unfiltered (gdb_stdlog
, "Process record: record_disconnect\n");
1512 unpush_target (&record_ops
);
1513 target_disconnect (args
, from_tty
);
1516 /* "to_detach" method for process record target. */
1519 record_detach (struct target_ops
*ops
, char *args
, int from_tty
)
1522 fprintf_unfiltered (gdb_stdlog
, "Process record: record_detach\n");
1524 unpush_target (&record_ops
);
1525 target_detach (args
, from_tty
);
1528 /* "to_mourn_inferior" method for process record target. */
1531 record_mourn_inferior (struct target_ops
*ops
)
1534 fprintf_unfiltered (gdb_stdlog
, "Process record: "
1535 "record_mourn_inferior\n");
1537 unpush_target (&record_ops
);
1538 target_mourn_inferior ();
1541 /* Close process record target before killing the inferior process. */
1544 record_kill (struct target_ops
*ops
)
1547 fprintf_unfiltered (gdb_stdlog
, "Process record: record_kill\n");
1549 unpush_target (&record_ops
);
1554 record_stopped_data_address (struct target_ops
*ops
, CORE_ADDR
*addr_p
)
1556 if (RECORD_IS_REPLAY
)
1559 return record_beneath_to_stopped_data_address (ops
, addr_p
);
1562 /* Record registers change (by user or by GDB) to list as an instruction. */
1565 record_registers_change (struct regcache
*regcache
, int regnum
)
1567 /* Check record_insn_num. */
1568 record_check_insn_num (0);
1570 record_arch_list_head
= NULL
;
1571 record_arch_list_tail
= NULL
;
1577 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
1579 if (record_arch_list_add_reg (regcache
, i
))
1581 record_list_release (record_arch_list_tail
);
1582 error (_("Process record: failed to record execution log."));
1588 if (record_arch_list_add_reg (regcache
, regnum
))
1590 record_list_release (record_arch_list_tail
);
1591 error (_("Process record: failed to record execution log."));
1594 if (record_arch_list_add_end ())
1596 record_list_release (record_arch_list_tail
);
1597 error (_("Process record: failed to record execution log."));
1599 record_list
->next
= record_arch_list_head
;
1600 record_arch_list_head
->prev
= record_list
;
1601 record_list
= record_arch_list_tail
;
1603 if (record_insn_num
== record_insn_max_num
&& record_insn_max_num
)
1604 record_list_release_first ();
1609 /* "to_store_registers" method for process record target. */
1612 record_store_registers (struct target_ops
*ops
, struct regcache
*regcache
,
1615 if (!record_gdb_operation_disable
)
1617 if (RECORD_IS_REPLAY
)
1621 /* Let user choose if he wants to write register or not. */
1624 query (_("Because GDB is in replay mode, changing the "
1625 "value of a register will make the execution "
1626 "log unusable from this point onward. "
1627 "Change all registers?"));
1630 query (_("Because GDB is in replay mode, changing the value "
1631 "of a register will make the execution log unusable "
1632 "from this point onward. Change register %s?"),
1633 gdbarch_register_name (get_regcache_arch (regcache
),
1638 /* Invalidate the value of regcache that was set in function
1639 "regcache_raw_write". */
1645 i
< gdbarch_num_regs (get_regcache_arch (regcache
));
1647 regcache_invalidate (regcache
, i
);
1650 regcache_invalidate (regcache
, regno
);
1652 error (_("Process record canceled the operation."));
1655 /* Destroy the record from here forward. */
1656 record_list_release_following (record_list
);
1659 record_registers_change (regcache
, regno
);
1661 record_beneath_to_store_registers (record_beneath_to_store_registers_ops
,
1665 /* "to_xfer_partial" method. Behavior is conditional on RECORD_IS_REPLAY.
1666 In replay mode, we cannot write memory unles we are willing to
1667 invalidate the record/replay log from this point forward. */
1670 record_xfer_partial (struct target_ops
*ops
, enum target_object object
,
1671 const char *annex
, gdb_byte
*readbuf
,
1672 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
1674 if (!record_gdb_operation_disable
1675 && (object
== TARGET_OBJECT_MEMORY
1676 || object
== TARGET_OBJECT_RAW_MEMORY
) && writebuf
)
1678 if (RECORD_IS_REPLAY
)
1680 /* Let user choose if he wants to write memory or not. */
1681 if (!query (_("Because GDB is in replay mode, writing to memory "
1682 "will make the execution log unusable from this "
1683 "point onward. Write memory at address %s?"),
1684 paddress (target_gdbarch (), offset
)))
1685 error (_("Process record canceled the operation."));
1687 /* Destroy the record from here forward. */
1688 record_list_release_following (record_list
);
1691 /* Check record_insn_num */
1692 record_check_insn_num (0);
1694 /* Record registers change to list as an instruction. */
1695 record_arch_list_head
= NULL
;
1696 record_arch_list_tail
= NULL
;
1697 if (record_arch_list_add_mem (offset
, len
))
1699 record_list_release (record_arch_list_tail
);
1701 fprintf_unfiltered (gdb_stdlog
,
1702 "Process record: failed to record "
1706 if (record_arch_list_add_end ())
1708 record_list_release (record_arch_list_tail
);
1710 fprintf_unfiltered (gdb_stdlog
,
1711 "Process record: failed to record "
1715 record_list
->next
= record_arch_list_head
;
1716 record_arch_list_head
->prev
= record_list
;
1717 record_list
= record_arch_list_tail
;
1719 if (record_insn_num
== record_insn_max_num
&& record_insn_max_num
)
1720 record_list_release_first ();
1725 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops
,
1726 object
, annex
, readbuf
, writebuf
,
1730 /* This structure represents a breakpoint inserted while the record
1731 target is active. We use this to know when to install/remove
1732 breakpoints in/from the target beneath. For example, a breakpoint
1733 may be inserted while recording, but removed when not replaying nor
1734 recording. In that case, the breakpoint had not been inserted on
1735 the target beneath, so we should not try to remove it there. */
1737 struct record_breakpoint
1739 /* The address and address space the breakpoint was set at. */
1740 struct address_space
*address_space
;
1743 /* True when the breakpoint has been also installed in the target
1744 beneath. This will be false for breakpoints set during replay or
1746 int in_target_beneath
;
1749 typedef struct record_breakpoint
*record_breakpoint_p
;
1750 DEF_VEC_P(record_breakpoint_p
);
1752 /* The list of breakpoints inserted while the record target is
1754 VEC(record_breakpoint_p
) *record_breakpoints
= NULL
;
1757 record_sync_record_breakpoints (struct bp_location
*loc
, void *data
)
1759 if (loc
->loc_type
!= bp_loc_software_breakpoint
)
1764 struct record_breakpoint
*bp
= XNEW (struct record_breakpoint
);
1766 bp
->addr
= loc
->target_info
.placed_address
;
1767 bp
->address_space
= loc
->target_info
.placed_address_space
;
1769 bp
->in_target_beneath
= 1;
1771 VEC_safe_push (record_breakpoint_p
, record_breakpoints
, bp
);
1775 /* Sync existing breakpoints to record_breakpoints. */
1778 record_init_record_breakpoints (void)
1780 VEC_free (record_breakpoint_p
, record_breakpoints
);
1782 iterate_over_bp_locations (record_sync_record_breakpoints
);
1785 /* Behavior is conditional on RECORD_IS_REPLAY. We will not actually
1786 insert or remove breakpoints in the real target when replaying, nor
1790 record_insert_breakpoint (struct gdbarch
*gdbarch
,
1791 struct bp_target_info
*bp_tgt
)
1793 struct record_breakpoint
*bp
;
1794 int in_target_beneath
= 0;
1796 if (!RECORD_IS_REPLAY
)
1798 /* When recording, we currently always single-step, so we don't
1799 really need to install regular breakpoints in the inferior.
1800 However, we do have to insert software single-step
1801 breakpoints, in case the target can't hardware step. To keep
1802 things single, we always insert. */
1803 struct cleanup
*old_cleanups
;
1806 old_cleanups
= record_gdb_operation_disable_set ();
1807 ret
= record_beneath_to_insert_breakpoint (gdbarch
, bp_tgt
);
1808 do_cleanups (old_cleanups
);
1813 in_target_beneath
= 1;
1816 bp
= XNEW (struct record_breakpoint
);
1817 bp
->addr
= bp_tgt
->placed_address
;
1818 bp
->address_space
= bp_tgt
->placed_address_space
;
1819 bp
->in_target_beneath
= in_target_beneath
;
1820 VEC_safe_push (record_breakpoint_p
, record_breakpoints
, bp
);
1824 /* "to_remove_breakpoint" method for process record target. */
1827 record_remove_breakpoint (struct gdbarch
*gdbarch
,
1828 struct bp_target_info
*bp_tgt
)
1830 struct record_breakpoint
*bp
;
1834 VEC_iterate (record_breakpoint_p
, record_breakpoints
, ix
, bp
);
1837 if (bp
->addr
== bp_tgt
->placed_address
1838 && bp
->address_space
== bp_tgt
->placed_address_space
)
1840 if (bp
->in_target_beneath
)
1842 struct cleanup
*old_cleanups
;
1845 old_cleanups
= record_gdb_operation_disable_set ();
1846 ret
= record_beneath_to_remove_breakpoint (gdbarch
, bp_tgt
);
1847 do_cleanups (old_cleanups
);
1853 VEC_unordered_remove (record_breakpoint_p
, record_breakpoints
, ix
);
1858 gdb_assert_not_reached ("removing unknown breakpoint");
1861 /* "to_can_execute_reverse" method for process record target. */
1864 record_can_execute_reverse (void)
1869 /* "to_get_bookmark" method for process record and prec over core. */
1872 record_get_bookmark (char *args
, int from_tty
)
1874 gdb_byte
*ret
= NULL
;
1876 /* Return stringified form of instruction count. */
1877 if (record_list
&& record_list
->type
== record_end
)
1878 ret
= xstrdup (pulongest (record_list
->u
.end
.insn_num
));
1883 fprintf_unfiltered (gdb_stdlog
,
1884 "record_get_bookmark returns %s\n", ret
);
1886 fprintf_unfiltered (gdb_stdlog
,
1887 "record_get_bookmark returns NULL\n");
1892 /* "to_goto_bookmark" method for process record and prec over core. */
1895 record_goto_bookmark (gdb_byte
*bookmark
, int from_tty
)
1898 fprintf_unfiltered (gdb_stdlog
,
1899 "record_goto_bookmark receives %s\n", bookmark
);
1901 if (bookmark
[0] == '\'' || bookmark
[0] == '\"')
1903 if (bookmark
[strlen (bookmark
) - 1] != bookmark
[0])
1904 error (_("Unbalanced quotes: %s"), bookmark
);
1906 /* Strip trailing quote. */
1907 bookmark
[strlen (bookmark
) - 1] = '\0';
1908 /* Strip leading quote. */
1910 /* Pass along to cmd_record_goto. */
1913 cmd_record_goto ((char *) bookmark
, from_tty
);
1918 record_async (void (*callback
) (enum inferior_event_type event_type
,
1919 void *context
), void *context
)
1921 /* If we're on top of a line target (e.g., linux-nat, remote), then
1922 set it to async mode as well. Will be NULL if we're sitting on
1923 top of the core target, for "record restore". */
1924 if (record_beneath_to_async
!= NULL
)
1925 record_beneath_to_async (callback
, context
);
1929 record_can_async_p (void)
1931 /* We only enable async when the user specifically asks for it. */
1932 return target_async_permitted
;
1936 record_is_async_p (void)
1938 /* We only enable async when the user specifically asks for it. */
1939 return target_async_permitted
;
1942 static enum exec_direction_kind
1943 record_execution_direction (void)
1945 return record_execution_dir
;
1951 struct record_entry
*p
;
1953 if (RECORD_IS_REPLAY
)
1954 printf_filtered (_("Replay mode:\n"));
1956 printf_filtered (_("Record mode:\n"));
1958 /* Find entry for first actual instruction in the log. */
1959 for (p
= record_first
.next
;
1960 p
!= NULL
&& p
->type
!= record_end
;
1964 /* Do we have a log at all? */
1965 if (p
!= NULL
&& p
->type
== record_end
)
1967 /* Display instruction number for first instruction in the log. */
1968 printf_filtered (_("Lowest recorded instruction number is %s.\n"),
1969 pulongest (p
->u
.end
.insn_num
));
1971 /* If in replay mode, display where we are in the log. */
1972 if (RECORD_IS_REPLAY
)
1973 printf_filtered (_("Current instruction number is %s.\n"),
1974 pulongest (record_list
->u
.end
.insn_num
));
1976 /* Display instruction number for last instruction in the log. */
1977 printf_filtered (_("Highest recorded instruction number is %s.\n"),
1978 pulongest (record_insn_count
));
1980 /* Display log count. */
1981 printf_filtered (_("Log contains %d instructions.\n"),
1985 printf_filtered (_("No instructions have been logged.\n"));
1987 /* Display max log size. */
1988 printf_filtered (_("Max logged instructions is %d.\n"),
1989 record_insn_max_num
);
1992 /* The "to_record_delete" target method. */
1995 record_delete (void)
1997 record_list_release_following (record_list
);
2000 /* The "to_record_is_replaying" target method. */
2003 record_is_replaying (void)
2005 return RECORD_IS_REPLAY
;
2008 /* Go to a specific entry. */
2011 record_goto_entry (struct record_entry
*p
)
2014 error (_("Target insn not found."));
2015 else if (p
== record_list
)
2016 error (_("Already at target insn."));
2017 else if (p
->u
.end
.insn_num
> record_list
->u
.end
.insn_num
)
2019 printf_filtered (_("Go forward to insn number %s\n"),
2020 pulongest (p
->u
.end
.insn_num
));
2021 record_goto_insn (p
, EXEC_FORWARD
);
2025 printf_filtered (_("Go backward to insn number %s\n"),
2026 pulongest (p
->u
.end
.insn_num
));
2027 record_goto_insn (p
, EXEC_REVERSE
);
2030 registers_changed ();
2031 reinit_frame_cache ();
2032 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
2035 /* The "to_goto_record_begin" target method. */
2038 record_goto_begin (void)
2040 struct record_entry
*p
= NULL
;
2042 for (p
= &record_first
; p
!= NULL
; p
= p
->next
)
2043 if (p
->type
== record_end
)
2046 record_goto_entry (p
);
2049 /* The "to_goto_record_end" target method. */
2052 record_goto_end (void)
2054 struct record_entry
*p
= NULL
;
2056 for (p
= record_list
; p
->next
!= NULL
; p
= p
->next
)
2058 for (; p
!= NULL
; p
= p
->prev
)
2059 if (p
->type
== record_end
)
2062 record_goto_entry (p
);
2065 /* The "to_goto_record" target method. */
2068 record_goto (ULONGEST target_insn
)
2070 struct record_entry
*p
= NULL
;
2072 for (p
= &record_first
; p
!= NULL
; p
= p
->next
)
2073 if (p
->type
== record_end
&& p
->u
.end
.insn_num
== target_insn
)
2076 record_goto_entry (p
);
2080 init_record_ops (void)
2082 record_ops
.to_shortname
= "record-full";
2083 record_ops
.to_longname
= "Process record and replay target";
2085 "Log program while executing and replay execution from log.";
2086 record_ops
.to_open
= record_open
;
2087 record_ops
.to_close
= record_close
;
2088 record_ops
.to_resume
= record_resume
;
2089 record_ops
.to_wait
= record_wait
;
2090 record_ops
.to_disconnect
= record_disconnect
;
2091 record_ops
.to_detach
= record_detach
;
2092 record_ops
.to_mourn_inferior
= record_mourn_inferior
;
2093 record_ops
.to_kill
= record_kill
;
2094 record_ops
.to_create_inferior
= find_default_create_inferior
;
2095 record_ops
.to_store_registers
= record_store_registers
;
2096 record_ops
.to_xfer_partial
= record_xfer_partial
;
2097 record_ops
.to_insert_breakpoint
= record_insert_breakpoint
;
2098 record_ops
.to_remove_breakpoint
= record_remove_breakpoint
;
2099 record_ops
.to_stopped_by_watchpoint
= record_stopped_by_watchpoint
;
2100 record_ops
.to_stopped_data_address
= record_stopped_data_address
;
2101 record_ops
.to_can_execute_reverse
= record_can_execute_reverse
;
2102 record_ops
.to_stratum
= record_stratum
;
2103 /* Add bookmark target methods. */
2104 record_ops
.to_get_bookmark
= record_get_bookmark
;
2105 record_ops
.to_goto_bookmark
= record_goto_bookmark
;
2106 record_ops
.to_async
= record_async
;
2107 record_ops
.to_can_async_p
= record_can_async_p
;
2108 record_ops
.to_is_async_p
= record_is_async_p
;
2109 record_ops
.to_execution_direction
= record_execution_direction
;
2110 record_ops
.to_info_record
= record_info
;
2111 record_ops
.to_save_record
= record_save
;
2112 record_ops
.to_delete_record
= record_delete
;
2113 record_ops
.to_record_is_replaying
= record_is_replaying
;
2114 record_ops
.to_goto_record_begin
= record_goto_begin
;
2115 record_ops
.to_goto_record_end
= record_goto_end
;
2116 record_ops
.to_goto_record
= record_goto
;
2117 record_ops
.to_magic
= OPS_MAGIC
;
2120 /* "to_resume" method for prec over corefile. */
2123 record_core_resume (struct target_ops
*ops
, ptid_t ptid
, int step
,
2124 enum gdb_signal signal
)
2126 record_resume_step
= step
;
2128 record_execution_dir
= execution_direction
;
2130 /* We are about to start executing the inferior (or simulate it),
2131 let's register it with the event loop. */
2132 if (target_can_async_p ())
2134 target_async (inferior_event_handler
, 0);
2136 /* Notify the event loop there's an event to wait for. */
2137 mark_async_event_handler (record_async_inferior_event_token
);
2141 /* "to_kill" method for prec over corefile. */
2144 record_core_kill (struct target_ops
*ops
)
2147 fprintf_unfiltered (gdb_stdlog
, "Process record: record_core_kill\n");
2149 unpush_target (&record_core_ops
);
2152 /* "to_fetch_registers" method for prec over corefile. */
2155 record_core_fetch_registers (struct target_ops
*ops
,
2156 struct regcache
*regcache
,
2161 int num
= gdbarch_num_regs (get_regcache_arch (regcache
));
2164 for (i
= 0; i
< num
; i
++)
2165 regcache_raw_supply (regcache
, i
,
2166 record_core_regbuf
+ MAX_REGISTER_SIZE
* i
);
2169 regcache_raw_supply (regcache
, regno
,
2170 record_core_regbuf
+ MAX_REGISTER_SIZE
* regno
);
2173 /* "to_prepare_to_store" method for prec over corefile. */
2176 record_core_prepare_to_store (struct regcache
*regcache
)
2180 /* "to_store_registers" method for prec over corefile. */
2183 record_core_store_registers (struct target_ops
*ops
,
2184 struct regcache
*regcache
,
2187 if (record_gdb_operation_disable
)
2188 regcache_raw_collect (regcache
, regno
,
2189 record_core_regbuf
+ MAX_REGISTER_SIZE
* regno
);
2191 error (_("You can't do that without a process to debug."));
2194 /* "to_xfer_partial" method for prec over corefile. */
2197 record_core_xfer_partial (struct target_ops
*ops
, enum target_object object
,
2198 const char *annex
, gdb_byte
*readbuf
,
2199 const gdb_byte
*writebuf
, ULONGEST offset
,
2202 if (object
== TARGET_OBJECT_MEMORY
)
2204 if (record_gdb_operation_disable
|| !writebuf
)
2206 struct target_section
*p
;
2208 for (p
= record_core_start
; p
< record_core_end
; p
++)
2210 if (offset
>= p
->addr
)
2212 struct record_core_buf_entry
*entry
;
2213 ULONGEST sec_offset
;
2215 if (offset
>= p
->endaddr
)
2218 if (offset
+ len
> p
->endaddr
)
2219 len
= p
->endaddr
- offset
;
2221 sec_offset
= offset
- p
->addr
;
2223 /* Read readbuf or write writebuf p, offset, len. */
2225 if (p
->the_bfd_section
->flags
& SEC_CONSTRUCTOR
2226 || (p
->the_bfd_section
->flags
& SEC_HAS_CONTENTS
) == 0)
2229 memset (readbuf
, 0, len
);
2232 /* Get record_core_buf_entry. */
2233 for (entry
= record_core_buf_list
; entry
;
2234 entry
= entry
->prev
)
2241 /* Add a new entry. */
2242 entry
= (struct record_core_buf_entry
*)
2243 xmalloc (sizeof (struct record_core_buf_entry
));
2245 if (!bfd_malloc_and_get_section (p
->bfd
,
2252 entry
->prev
= record_core_buf_list
;
2253 record_core_buf_list
= entry
;
2256 memcpy (entry
->buf
+ sec_offset
, writebuf
,
2262 return record_beneath_to_xfer_partial
2263 (record_beneath_to_xfer_partial_ops
,
2264 object
, annex
, readbuf
, writebuf
,
2267 memcpy (readbuf
, entry
->buf
+ sec_offset
,
2278 error (_("You can't do that without a process to debug."));
2281 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops
,
2282 object
, annex
, readbuf
, writebuf
,
2286 /* "to_insert_breakpoint" method for prec over corefile. */
2289 record_core_insert_breakpoint (struct gdbarch
*gdbarch
,
2290 struct bp_target_info
*bp_tgt
)
2295 /* "to_remove_breakpoint" method for prec over corefile. */
2298 record_core_remove_breakpoint (struct gdbarch
*gdbarch
,
2299 struct bp_target_info
*bp_tgt
)
2304 /* "to_has_execution" method for prec over corefile. */
2307 record_core_has_execution (struct target_ops
*ops
, ptid_t the_ptid
)
2313 init_record_core_ops (void)
2315 record_core_ops
.to_shortname
= "record-core";
2316 record_core_ops
.to_longname
= "Process record and replay target";
2317 record_core_ops
.to_doc
=
2318 "Log program while executing and replay execution from log.";
2319 record_core_ops
.to_open
= record_open
;
2320 record_core_ops
.to_close
= record_close
;
2321 record_core_ops
.to_resume
= record_core_resume
;
2322 record_core_ops
.to_wait
= record_wait
;
2323 record_core_ops
.to_kill
= record_core_kill
;
2324 record_core_ops
.to_fetch_registers
= record_core_fetch_registers
;
2325 record_core_ops
.to_prepare_to_store
= record_core_prepare_to_store
;
2326 record_core_ops
.to_store_registers
= record_core_store_registers
;
2327 record_core_ops
.to_xfer_partial
= record_core_xfer_partial
;
2328 record_core_ops
.to_insert_breakpoint
= record_core_insert_breakpoint
;
2329 record_core_ops
.to_remove_breakpoint
= record_core_remove_breakpoint
;
2330 record_core_ops
.to_stopped_by_watchpoint
= record_stopped_by_watchpoint
;
2331 record_core_ops
.to_stopped_data_address
= record_stopped_data_address
;
2332 record_core_ops
.to_can_execute_reverse
= record_can_execute_reverse
;
2333 record_core_ops
.to_has_execution
= record_core_has_execution
;
2334 record_core_ops
.to_stratum
= record_stratum
;
2335 /* Add bookmark target methods. */
2336 record_core_ops
.to_get_bookmark
= record_get_bookmark
;
2337 record_core_ops
.to_goto_bookmark
= record_goto_bookmark
;
2338 record_core_ops
.to_async
= record_async
;
2339 record_core_ops
.to_can_async_p
= record_can_async_p
;
2340 record_core_ops
.to_is_async_p
= record_is_async_p
;
2341 record_core_ops
.to_execution_direction
= record_execution_direction
;
2342 record_core_ops
.to_info_record
= record_info
;
2343 record_core_ops
.to_delete_record
= record_delete
;
2344 record_core_ops
.to_record_is_replaying
= record_is_replaying
;
2345 record_core_ops
.to_goto_record_begin
= record_goto_begin
;
2346 record_core_ops
.to_goto_record_end
= record_goto_end
;
2347 record_core_ops
.to_goto_record
= record_goto
;
2348 record_core_ops
.to_magic
= OPS_MAGIC
;
2351 /* Record log save-file format
2352 Version 1 (never released)
2355 4 bytes: magic number htonl(0x20090829).
2356 NOTE: be sure to change whenever this file format changes!
2360 1 byte: record type (record_end, see enum record_type).
2362 1 byte: record type (record_reg, see enum record_type).
2363 8 bytes: register id (network byte order).
2364 MAX_REGISTER_SIZE bytes: register value.
2366 1 byte: record type (record_mem, see enum record_type).
2367 8 bytes: memory length (network byte order).
2368 8 bytes: memory address (network byte order).
2369 n bytes: memory value (n == memory length).
2372 4 bytes: magic number netorder32(0x20091016).
2373 NOTE: be sure to change whenever this file format changes!
2377 1 byte: record type (record_end, see enum record_type).
2379 4 bytes: instruction count
2381 1 byte: record type (record_reg, see enum record_type).
2382 4 bytes: register id (network byte order).
2383 n bytes: register value (n == actual register size).
2384 (eg. 4 bytes for x86 general registers).
2386 1 byte: record type (record_mem, see enum record_type).
2387 4 bytes: memory length (network byte order).
2388 8 bytes: memory address (network byte order).
2389 n bytes: memory value (n == memory length).
2393 /* bfdcore_read -- read bytes from a core file section. */
2396 bfdcore_read (bfd
*obfd
, asection
*osec
, void *buf
, int len
, int *offset
)
2398 int ret
= bfd_get_section_contents (obfd
, osec
, buf
, *offset
, len
);
2403 error (_("Failed to read %d bytes from core file %s ('%s')."),
2404 len
, bfd_get_filename (obfd
),
2405 bfd_errmsg (bfd_get_error ()));
2408 static inline uint64_t
2409 netorder64 (uint64_t input
)
2413 store_unsigned_integer ((gdb_byte
*) &ret
, sizeof (ret
),
2414 BFD_ENDIAN_BIG
, input
);
2418 static inline uint32_t
2419 netorder32 (uint32_t input
)
2423 store_unsigned_integer ((gdb_byte
*) &ret
, sizeof (ret
),
2424 BFD_ENDIAN_BIG
, input
);
2428 static inline uint16_t
2429 netorder16 (uint16_t input
)
2433 store_unsigned_integer ((gdb_byte
*) &ret
, sizeof (ret
),
2434 BFD_ENDIAN_BIG
, input
);
2438 /* Restore the execution log from a core_bfd file. */
2440 record_restore (void)
2443 struct cleanup
*old_cleanups
;
2444 struct record_entry
*rec
;
2448 struct regcache
*regcache
;
2450 /* We restore the execution log from the open core bfd,
2452 if (core_bfd
== NULL
)
2455 /* "record_restore" can only be called when record list is empty. */
2456 gdb_assert (record_first
.next
== NULL
);
2459 fprintf_unfiltered (gdb_stdlog
, "Restoring recording from core file.\n");
2461 /* Now need to find our special note section. */
2462 osec
= bfd_get_section_by_name (core_bfd
, "null0");
2464 fprintf_unfiltered (gdb_stdlog
, "Find precord section %s.\n",
2465 osec
? "succeeded" : "failed");
2468 osec_size
= bfd_section_size (core_bfd
, osec
);
2470 fprintf_unfiltered (gdb_stdlog
, "%s", bfd_section_name (core_bfd
, osec
));
2472 /* Check the magic code. */
2473 bfdcore_read (core_bfd
, osec
, &magic
, sizeof (magic
), &bfd_offset
);
2474 if (magic
!= RECORD_FILE_MAGIC
)
2475 error (_("Version mis-match or file format error in core file %s."),
2476 bfd_get_filename (core_bfd
));
2478 fprintf_unfiltered (gdb_stdlog
,
2479 " Reading 4-byte magic cookie "
2480 "RECORD_FILE_MAGIC (0x%s)\n",
2481 phex_nz (netorder32 (magic
), 4));
2483 /* Restore the entries in recfd into record_arch_list_head and
2484 record_arch_list_tail. */
2485 record_arch_list_head
= NULL
;
2486 record_arch_list_tail
= NULL
;
2487 record_insn_num
= 0;
2488 old_cleanups
= make_cleanup (record_arch_list_cleanups
, 0);
2489 regcache
= get_current_regcache ();
2494 uint32_t regnum
, len
, signal
, count
;
2497 /* We are finished when offset reaches osec_size. */
2498 if (bfd_offset
>= osec_size
)
2500 bfdcore_read (core_bfd
, osec
, &rectype
, sizeof (rectype
), &bfd_offset
);
2504 case record_reg
: /* reg */
2505 /* Get register number to regnum. */
2506 bfdcore_read (core_bfd
, osec
, ®num
,
2507 sizeof (regnum
), &bfd_offset
);
2508 regnum
= netorder32 (regnum
);
2510 rec
= record_reg_alloc (regcache
, regnum
);
2513 bfdcore_read (core_bfd
, osec
, record_get_loc (rec
),
2514 rec
->u
.reg
.len
, &bfd_offset
);
2517 fprintf_unfiltered (gdb_stdlog
,
2518 " Reading register %d (1 "
2519 "plus %lu plus %d bytes)\n",
2521 (unsigned long) sizeof (regnum
),
2525 case record_mem
: /* mem */
2527 bfdcore_read (core_bfd
, osec
, &len
,
2528 sizeof (len
), &bfd_offset
);
2529 len
= netorder32 (len
);
2532 bfdcore_read (core_bfd
, osec
, &addr
,
2533 sizeof (addr
), &bfd_offset
);
2534 addr
= netorder64 (addr
);
2536 rec
= record_mem_alloc (addr
, len
);
2539 bfdcore_read (core_bfd
, osec
, record_get_loc (rec
),
2540 rec
->u
.mem
.len
, &bfd_offset
);
2543 fprintf_unfiltered (gdb_stdlog
,
2544 " Reading memory %s (1 plus "
2545 "%lu plus %lu plus %d bytes)\n",
2546 paddress (get_current_arch (),
2548 (unsigned long) sizeof (addr
),
2549 (unsigned long) sizeof (len
),
2553 case record_end
: /* end */
2554 rec
= record_end_alloc ();
2557 /* Get signal value. */
2558 bfdcore_read (core_bfd
, osec
, &signal
,
2559 sizeof (signal
), &bfd_offset
);
2560 signal
= netorder32 (signal
);
2561 rec
->u
.end
.sigval
= signal
;
2563 /* Get insn count. */
2564 bfdcore_read (core_bfd
, osec
, &count
,
2565 sizeof (count
), &bfd_offset
);
2566 count
= netorder32 (count
);
2567 rec
->u
.end
.insn_num
= count
;
2568 record_insn_count
= count
+ 1;
2570 fprintf_unfiltered (gdb_stdlog
,
2571 " Reading record_end (1 + "
2572 "%lu + %lu bytes), offset == %s\n",
2573 (unsigned long) sizeof (signal
),
2574 (unsigned long) sizeof (count
),
2575 paddress (get_current_arch (),
2580 error (_("Bad entry type in core file %s."),
2581 bfd_get_filename (core_bfd
));
2585 /* Add rec to record arch list. */
2586 record_arch_list_add (rec
);
2589 discard_cleanups (old_cleanups
);
2591 /* Add record_arch_list_head to the end of record list. */
2592 record_first
.next
= record_arch_list_head
;
2593 record_arch_list_head
->prev
= &record_first
;
2594 record_arch_list_tail
->next
= NULL
;
2595 record_list
= &record_first
;
2597 /* Update record_insn_max_num. */
2598 if (record_insn_num
> record_insn_max_num
)
2600 record_insn_max_num
= record_insn_num
;
2601 warning (_("Auto increase record/replay buffer limit to %d."),
2602 record_insn_max_num
);
2606 printf_filtered (_("Restored records from core file %s.\n"),
2607 bfd_get_filename (core_bfd
));
2609 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
2612 /* bfdcore_write -- write bytes into a core file section. */
2615 bfdcore_write (bfd
*obfd
, asection
*osec
, void *buf
, int len
, int *offset
)
2617 int ret
= bfd_set_section_contents (obfd
, osec
, buf
, *offset
, len
);
2622 error (_("Failed to write %d bytes to core file %s ('%s')."),
2623 len
, bfd_get_filename (obfd
),
2624 bfd_errmsg (bfd_get_error ()));
2627 /* Restore the execution log from a file. We use a modified elf
2628 corefile format, with an extra section for our data. */
2631 cmd_record_restore (char *args
, int from_tty
)
2633 core_file_command (args
, from_tty
);
2634 record_open (args
, from_tty
);
2638 record_save_cleanups (void *data
)
2641 char *pathname
= xstrdup (bfd_get_filename (obfd
));
2643 gdb_bfd_unref (obfd
);
2648 /* Save the execution log to a file. We use a modified elf corefile
2649 format, with an extra section for our data. */
2652 record_save (char *recfilename
)
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 /* Open the save file. */
2667 fprintf_unfiltered (gdb_stdlog
, "Saving execution log to core file '%s'\n",
2670 /* Open the output file. */
2671 obfd
= create_gcore_bfd (recfilename
);
2672 old_cleanups
= make_cleanup (record_save_cleanups
, obfd
);
2674 /* Save the current record entry to "cur_record_list". */
2675 cur_record_list
= record_list
;
2677 /* Get the values of regcache and gdbarch. */
2678 regcache
= get_current_regcache ();
2679 gdbarch
= get_regcache_arch (regcache
);
2681 /* Disable the GDB operation record. */
2682 set_cleanups
= record_gdb_operation_disable_set ();
2684 /* Reverse execute to the begin of record list. */
2687 /* Check for beginning and end of log. */
2688 if (record_list
== &record_first
)
2691 record_exec_insn (regcache
, gdbarch
, record_list
);
2693 if (record_list
->prev
)
2694 record_list
= record_list
->prev
;
2697 /* Compute the size needed for the extra bfd section. */
2698 save_size
= 4; /* magic cookie */
2699 for (record_list
= record_first
.next
; record_list
;
2700 record_list
= record_list
->next
)
2701 switch (record_list
->type
)
2704 save_size
+= 1 + 4 + 4;
2707 save_size
+= 1 + 4 + record_list
->u
.reg
.len
;
2710 save_size
+= 1 + 4 + 8 + record_list
->u
.mem
.len
;
2714 /* Make the new bfd section. */
2715 osec
= bfd_make_section_anyway_with_flags (obfd
, "precord",
2719 error (_("Failed to create 'precord' section for corefile %s: %s"),
2721 bfd_errmsg (bfd_get_error ()));
2722 bfd_set_section_size (obfd
, osec
, save_size
);
2723 bfd_set_section_vma (obfd
, osec
, 0);
2724 bfd_set_section_alignment (obfd
, osec
, 0);
2725 bfd_section_lma (obfd
, osec
) = 0;
2727 /* Save corefile state. */
2728 write_gcore_file (obfd
);
2730 /* Write out the record log. */
2731 /* Write the magic code. */
2732 magic
= RECORD_FILE_MAGIC
;
2734 fprintf_unfiltered (gdb_stdlog
,
2735 " Writing 4-byte magic cookie "
2736 "RECORD_FILE_MAGIC (0x%s)\n",
2737 phex_nz (magic
, 4));
2738 bfdcore_write (obfd
, osec
, &magic
, sizeof (magic
), &bfd_offset
);
2740 /* Save the entries to recfd and forward execute to the end of
2742 record_list
= &record_first
;
2746 if (record_list
!= &record_first
)
2749 uint32_t regnum
, len
, signal
, count
;
2752 type
= record_list
->type
;
2753 bfdcore_write (obfd
, osec
, &type
, sizeof (type
), &bfd_offset
);
2755 switch (record_list
->type
)
2757 case record_reg
: /* reg */
2759 fprintf_unfiltered (gdb_stdlog
,
2760 " Writing register %d (1 "
2761 "plus %lu plus %d bytes)\n",
2762 record_list
->u
.reg
.num
,
2763 (unsigned long) sizeof (regnum
),
2764 record_list
->u
.reg
.len
);
2767 regnum
= netorder32 (record_list
->u
.reg
.num
);
2768 bfdcore_write (obfd
, osec
, ®num
,
2769 sizeof (regnum
), &bfd_offset
);
2772 bfdcore_write (obfd
, osec
, record_get_loc (record_list
),
2773 record_list
->u
.reg
.len
, &bfd_offset
);
2776 case record_mem
: /* mem */
2778 fprintf_unfiltered (gdb_stdlog
,
2779 " Writing memory %s (1 plus "
2780 "%lu plus %lu plus %d bytes)\n",
2782 record_list
->u
.mem
.addr
),
2783 (unsigned long) sizeof (addr
),
2784 (unsigned long) sizeof (len
),
2785 record_list
->u
.mem
.len
);
2788 len
= netorder32 (record_list
->u
.mem
.len
);
2789 bfdcore_write (obfd
, osec
, &len
, sizeof (len
), &bfd_offset
);
2791 /* Write memaddr. */
2792 addr
= netorder64 (record_list
->u
.mem
.addr
);
2793 bfdcore_write (obfd
, osec
, &addr
,
2794 sizeof (addr
), &bfd_offset
);
2797 bfdcore_write (obfd
, osec
, record_get_loc (record_list
),
2798 record_list
->u
.mem
.len
, &bfd_offset
);
2803 fprintf_unfiltered (gdb_stdlog
,
2804 " Writing record_end (1 + "
2805 "%lu + %lu bytes)\n",
2806 (unsigned long) sizeof (signal
),
2807 (unsigned long) sizeof (count
));
2808 /* Write signal value. */
2809 signal
= netorder32 (record_list
->u
.end
.sigval
);
2810 bfdcore_write (obfd
, osec
, &signal
,
2811 sizeof (signal
), &bfd_offset
);
2813 /* Write insn count. */
2814 count
= netorder32 (record_list
->u
.end
.insn_num
);
2815 bfdcore_write (obfd
, osec
, &count
,
2816 sizeof (count
), &bfd_offset
);
2821 /* Execute entry. */
2822 record_exec_insn (regcache
, gdbarch
, record_list
);
2824 if (record_list
->next
)
2825 record_list
= record_list
->next
;
2830 /* Reverse execute to cur_record_list. */
2833 /* Check for beginning and end of log. */
2834 if (record_list
== cur_record_list
)
2837 record_exec_insn (regcache
, gdbarch
, record_list
);
2839 if (record_list
->prev
)
2840 record_list
= record_list
->prev
;
2843 do_cleanups (set_cleanups
);
2844 gdb_bfd_unref (obfd
);
2845 discard_cleanups (old_cleanups
);
2848 printf_filtered (_("Saved core file %s with execution log.\n"),
2852 /* record_goto_insn -- rewind the record log (forward or backward,
2853 depending on DIR) to the given entry, changing the program state
2857 record_goto_insn (struct record_entry
*entry
,
2858 enum exec_direction_kind dir
)
2860 struct cleanup
*set_cleanups
= record_gdb_operation_disable_set ();
2861 struct regcache
*regcache
= get_current_regcache ();
2862 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
2864 /* Assume everything is valid: we will hit the entry,
2865 and we will not hit the end of the recording. */
2867 if (dir
== EXEC_FORWARD
)
2868 record_list
= record_list
->next
;
2872 record_exec_insn (regcache
, gdbarch
, record_list
);
2873 if (dir
== EXEC_REVERSE
)
2874 record_list
= record_list
->prev
;
2876 record_list
= record_list
->next
;
2877 } while (record_list
!= entry
);
2878 do_cleanups (set_cleanups
);
2881 /* Alias for "target record-full". */
2884 cmd_record_start (char *args
, int from_tty
)
2886 execute_command ("target record-full", from_tty
);
2890 set_record_insn_max_num (char *args
, int from_tty
, struct cmd_list_element
*c
)
2892 if (record_insn_num
> record_insn_max_num
&& record_insn_max_num
)
2894 /* Count down record_insn_num while releasing records from list. */
2895 while (record_insn_num
> record_insn_max_num
)
2897 record_list_release_first ();
2903 /* The "set record full" command. */
2906 set_record_full_command (char *args
, int from_tty
)
2908 printf_unfiltered (_("\"set record full\" must be followed "
2909 "by an apporpriate subcommand.\n"));
2910 help_list (set_record_full_cmdlist
, "set record full ", all_commands
,
2914 /* The "show record full" command. */
2917 show_record_full_command (char *args
, int from_tty
)
2919 cmd_show_list (show_record_full_cmdlist
, from_tty
, "");
2922 /* Provide a prototype to silence -Wmissing-prototypes. */
2923 extern initialize_file_ftype _initialize_record_full
;
2926 _initialize_record_full (void)
2928 struct cmd_list_element
*c
;
2930 /* Init record_first. */
2931 record_first
.prev
= NULL
;
2932 record_first
.next
= NULL
;
2933 record_first
.type
= record_end
;
2936 add_target (&record_ops
);
2937 add_deprecated_target_alias (&record_ops
, "record");
2938 init_record_core_ops ();
2939 add_target (&record_core_ops
);
2941 add_prefix_cmd ("full", class_obscure
, cmd_record_start
,
2942 _("Start full execution recording."), &record_full_cmdlist
,
2943 "record full ", 0, &record_cmdlist
);
2945 c
= add_cmd ("restore", class_obscure
, cmd_record_restore
,
2946 _("Restore the execution log from a file.\n\
2947 Argument is filename. File must be created with 'record save'."),
2948 &record_full_cmdlist
);
2949 set_cmd_completer (c
, filename_completer
);
2951 /* Deprecate the old version without "full" prefix. */
2952 c
= add_alias_cmd ("restore", "full restore", class_obscure
, 1,
2954 set_cmd_completer (c
, filename_completer
);
2955 deprecate_cmd (c
, "record full restore");
2957 add_prefix_cmd ("full", class_support
, set_record_full_command
,
2958 _("Set record options"), &set_record_full_cmdlist
,
2959 "set record full ", 0, &set_record_cmdlist
);
2961 add_prefix_cmd ("full", class_support
, show_record_full_command
,
2962 _("Show record options"), &show_record_full_cmdlist
,
2963 "show record full ", 0, &show_record_cmdlist
);
2965 /* Record instructions number limit command. */
2966 add_setshow_boolean_cmd ("stop-at-limit", no_class
,
2967 &record_stop_at_limit
, _("\
2968 Set whether record/replay stops when record/replay buffer becomes full."), _("\
2969 Show whether record/replay stops when record/replay buffer becomes full."),
2970 _("Default is ON.\n\
2971 When ON, if the record/replay buffer becomes full, ask user what to do.\n\
2972 When OFF, if the record/replay buffer becomes full,\n\
2973 delete the oldest recorded instruction to make room for each new one."),
2975 &set_record_full_cmdlist
, &show_record_full_cmdlist
);
2977 c
= add_alias_cmd ("stop-at-limit", "full stop-at-limit", no_class
, 1,
2978 &set_record_cmdlist
);
2979 deprecate_cmd (c
, "set record full stop-at-limit");
2981 c
= add_alias_cmd ("stop-at-limit", "full stop-at-limit", no_class
, 1,
2982 &show_record_cmdlist
);
2983 deprecate_cmd (c
, "show record full stop-at-limit");
2985 add_setshow_uinteger_cmd ("insn-number-max", no_class
, &record_insn_max_num
,
2986 _("Set record/replay buffer limit."),
2987 _("Show record/replay buffer limit."), _("\
2988 Set the maximum number of instructions to be stored in the\n\
2989 record/replay buffer. Zero means unlimited. Default is 200000."),
2990 set_record_insn_max_num
,
2991 NULL
, &set_record_full_cmdlist
,
2992 &show_record_full_cmdlist
);
2994 c
= add_alias_cmd ("insn-number-max", "full insn-number-max", no_class
, 1,
2995 &set_record_cmdlist
);
2996 deprecate_cmd (c
, "set record full insn-number-max");
2998 c
= add_alias_cmd ("insn-number-max", "full insn-number-max", no_class
, 1,
2999 &show_record_cmdlist
);
3000 deprecate_cmd (c
, "show record full insn-number-max");
3002 add_setshow_boolean_cmd ("memory-query", no_class
, &record_memory_query
, _("\
3003 Set whether query if PREC cannot record memory change of next instruction."),
3005 Show whether query if PREC cannot record memory change of next instruction."),
3008 When ON, query if PREC cannot record memory change of next instruction."),
3010 &set_record_full_cmdlist
, &show_record_full_cmdlist
);
3012 c
= add_alias_cmd ("memory-query", "full memory-query", no_class
, 1,
3013 &set_record_cmdlist
);
3014 deprecate_cmd (c
, "set record full memory-query");
3016 c
= add_alias_cmd ("memory-query", "full memory-query", no_class
, 1,
3017 &show_record_cmdlist
);
3018 deprecate_cmd (c
, "show record full memory-query");