1 /* Process record and replay target for GDB, the GNU debugger.
3 Copyright (C) 2008, 2009 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "gdbthread.h"
24 #include "event-top.h"
25 #include "exceptions.h"
30 /* This module implements "target record", also known as "process
31 record and replay". This target sits on top of a "normal" target
32 (a target that "has execution"), and provides a record and replay
33 functionality, including reverse debugging.
35 Target record has two modes: recording, and replaying.
37 In record mode, we intercept the to_resume and to_wait methods.
38 Whenever gdb resumes the target, we run the target in single step
39 mode, and we build up an execution log in which, for each executed
40 instruction, we record all changes in memory and register state.
41 This is invisible to the user, to whom it just looks like an
42 ordinary debugging session (except for performance degredation).
44 In replay mode, instead of actually letting the inferior run as a
45 process, we simulate its execution by playing back the recorded
46 execution log. For each instruction in the log, we simulate the
47 instruction's side effects by duplicating the changes that it would
48 have made on memory and registers. */
50 #define DEFAULT_RECORD_INSN_MAX_NUM 200000
52 #define RECORD_IS_REPLAY \
53 (record_list->next || execution_direction == EXEC_REVERSE)
55 /* These are the core structs of the process record functionality.
57 A record_entry is a record of the value change of a register
58 ("record_reg") or a part of memory ("record_mem"). And each
59 instruction must have a struct record_entry ("record_end") that
60 indicates that this is the last struct record_entry of this
63 Each struct record_entry is linked to "record_list" by "prev" and
66 struct record_mem_entry
70 /* Set this flag if target memory for this entry
71 can no longer be accessed. */
72 int mem_entry_not_accessible
;
76 gdb_byte buf
[sizeof (gdb_byte
*)];
80 struct record_reg_entry
87 gdb_byte buf
[2 * sizeof (gdb_byte
*)];
91 struct record_end_entry
93 enum target_signal sigval
;
103 /* This is the data structure that makes up the execution log.
105 The execution log consists of a single linked list of entries
106 of type "struct record_entry". It is doubly linked so that it
107 can be traversed in either direction.
109 The start of the list is anchored by a struct called
110 "record_first". The pointer "record_list" either points to the
111 last entry that was added to the list (in record mode), or to the
112 next entry in the list that will be executed (in replay mode).
114 Each list element (struct record_entry), in addition to next and
115 prev pointers, consists of a union of three entry types: mem, reg,
116 and end. A field called "type" determines which entry type is
117 represented by a given list element.
119 Each instruction that is added to the execution log is represented
120 by a variable number of list elements ('entries'). The instruction
121 will have one "reg" entry for each register that is changed by
122 executing the instruction (including the PC in every case). It
123 will also have one "mem" entry for each memory change. Finally,
124 each instruction will have an "end" entry that separates it from
125 the changes associated with the next instruction. */
129 struct record_entry
*prev
;
130 struct record_entry
*next
;
131 enum record_type type
;
135 struct record_reg_entry reg
;
137 struct record_mem_entry mem
;
139 struct record_end_entry end
;
143 /* This is the debug switch for process record. */
144 int record_debug
= 0;
146 /* The following variables are used for managing the linked list that
147 represents the execution log.
149 record_first is the anchor that holds down the beginning of the list.
151 record_list serves two functions:
152 1) In record mode, it anchors the end of the list.
153 2) In replay mode, it traverses the list and points to
154 the next instruction that must be emulated.
156 record_arch_list_head and record_arch_list_tail are used to manage
157 a separate list, which is used to build up the change elements of
158 the currently executing instruction during record mode. When this
159 instruction has been completely annotated in the "arch list", it
160 will be appended to the main execution log. */
162 static struct record_entry record_first
;
163 static struct record_entry
*record_list
= &record_first
;
164 static struct record_entry
*record_arch_list_head
= NULL
;
165 static struct record_entry
*record_arch_list_tail
= NULL
;
167 /* 1 ask user. 0 auto delete the last struct record_entry. */
168 static int record_stop_at_limit
= 1;
169 static unsigned int record_insn_max_num
= DEFAULT_RECORD_INSN_MAX_NUM
;
170 static int record_insn_num
= 0;
172 /* The target_ops of process record. */
173 static struct target_ops record_ops
;
175 /* The beneath function pointers. */
176 static struct target_ops
*record_beneath_to_resume_ops
;
177 static void (*record_beneath_to_resume
) (struct target_ops
*, ptid_t
, int,
179 static struct target_ops
*record_beneath_to_wait_ops
;
180 static ptid_t (*record_beneath_to_wait
) (struct target_ops
*, ptid_t
,
181 struct target_waitstatus
*,
183 static struct target_ops
*record_beneath_to_store_registers_ops
;
184 static void (*record_beneath_to_store_registers
) (struct target_ops
*,
187 static struct target_ops
*record_beneath_to_xfer_partial_ops
;
188 static LONGEST (*record_beneath_to_xfer_partial
) (struct target_ops
*ops
,
189 enum target_object object
,
192 const gdb_byte
*writebuf
,
195 static int (*record_beneath_to_insert_breakpoint
) (struct gdbarch
*,
196 struct bp_target_info
*);
197 static int (*record_beneath_to_remove_breakpoint
) (struct gdbarch
*,
198 struct bp_target_info
*);
200 /* Alloc and free functions for record_reg, record_mem, and record_end
203 /* Alloc a record_reg record entry. */
205 static inline struct record_entry
*
206 record_reg_alloc (struct regcache
*regcache
, int regnum
)
208 struct record_entry
*rec
;
209 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
211 rec
= (struct record_entry
*) xcalloc (1, sizeof (struct record_entry
));
212 rec
->type
= record_reg
;
213 rec
->u
.reg
.num
= regnum
;
214 rec
->u
.reg
.len
= register_size (gdbarch
, regnum
);
215 if (rec
->u
.reg
.len
> sizeof (rec
->u
.reg
.u
.buf
))
216 rec
->u
.reg
.u
.ptr
= (gdb_byte
*) xmalloc (rec
->u
.reg
.len
);
221 /* Free a record_reg record entry. */
224 record_reg_release (struct record_entry
*rec
)
226 gdb_assert (rec
->type
== record_reg
);
227 if (rec
->u
.reg
.len
> sizeof (rec
->u
.reg
.u
.buf
))
228 xfree (rec
->u
.reg
.u
.ptr
);
232 /* Alloc a record_mem record entry. */
234 static inline struct record_entry
*
235 record_mem_alloc (CORE_ADDR addr
, int len
)
237 struct record_entry
*rec
;
239 rec
= (struct record_entry
*) xcalloc (1, sizeof (struct record_entry
));
240 rec
->type
= record_mem
;
241 rec
->u
.mem
.addr
= addr
;
242 rec
->u
.mem
.len
= len
;
243 if (rec
->u
.mem
.len
> sizeof (rec
->u
.mem
.u
.buf
))
244 rec
->u
.mem
.u
.ptr
= (gdb_byte
*) xmalloc (len
);
249 /* Free a record_mem record entry. */
252 record_mem_release (struct record_entry
*rec
)
254 gdb_assert (rec
->type
== record_mem
);
255 if (rec
->u
.mem
.len
> sizeof (rec
->u
.mem
.u
.buf
))
256 xfree (rec
->u
.mem
.u
.ptr
);
260 /* Alloc a record_end record entry. */
262 static inline struct record_entry
*
263 record_end_alloc (void)
265 struct record_entry
*rec
;
267 rec
= (struct record_entry
*) xcalloc (1, sizeof (struct record_entry
));
268 rec
->type
= record_end
;
273 /* Free a record_end record entry. */
276 record_end_release (struct record_entry
*rec
)
281 /* Free one record entry, any type.
282 Return entry->type, in case caller wants to know. */
284 static inline enum record_type
285 record_entry_release (struct record_entry
*rec
)
287 enum record_type type
= rec
->type
;
291 record_reg_release (rec
);
294 record_mem_release (rec
);
297 record_end_release (rec
);
303 /* Free all record entries in list pointed to by REC. */
306 record_list_release (struct record_entry
*rec
)
317 record_entry_release (rec
->next
);
320 if (rec
== &record_first
)
323 record_first
.next
= NULL
;
326 record_entry_release (rec
);
329 /* Free all record entries forward of the given list position. */
332 record_list_release_following (struct record_entry
*rec
)
334 struct record_entry
*tmp
= rec
->next
;
340 if (record_entry_release (tmp
) == record_end
)
346 /* Delete the first instruction from the beginning of the log, to make
347 room for adding a new instruction at the end of the log.
349 Note -- this function does not modify record_insn_num. */
352 record_list_release_first (void)
354 struct record_entry
*tmp
;
356 if (!record_first
.next
)
359 /* Loop until a record_end. */
362 /* Cut record_first.next out of the linked list. */
363 tmp
= record_first
.next
;
364 record_first
.next
= tmp
->next
;
365 tmp
->next
->prev
= &record_first
;
367 /* tmp is now isolated, and can be deleted. */
368 if (record_entry_release (tmp
) == record_end
)
371 break; /* End loop at first record_end. */
374 if (!record_first
.next
)
376 gdb_assert (record_insn_num
== 1);
377 break; /* End loop when list is empty. */
382 /* Add a struct record_entry to record_arch_list. */
385 record_arch_list_add (struct record_entry
*rec
)
387 if (record_debug
> 1)
388 fprintf_unfiltered (gdb_stdlog
,
389 "Process record: record_arch_list_add %s.\n",
390 host_address_to_string (rec
));
392 if (record_arch_list_tail
)
394 record_arch_list_tail
->next
= rec
;
395 rec
->prev
= record_arch_list_tail
;
396 record_arch_list_tail
= rec
;
400 record_arch_list_head
= rec
;
401 record_arch_list_tail
= rec
;
405 /* Return the value storage location of a record entry. */
406 static inline gdb_byte
*
407 record_get_loc (struct record_entry
*rec
)
411 if (rec
->u
.mem
.len
> sizeof (rec
->u
.mem
.u
.buf
))
412 return rec
->u
.mem
.u
.ptr
;
414 return rec
->u
.mem
.u
.buf
;
416 if (rec
->u
.reg
.len
> sizeof (rec
->u
.reg
.u
.buf
))
417 return rec
->u
.reg
.u
.ptr
;
419 return rec
->u
.reg
.u
.buf
;
427 /* Record the value of a register NUM to record_arch_list. */
430 record_arch_list_add_reg (struct regcache
*regcache
, int regnum
)
432 struct record_entry
*rec
;
434 if (record_debug
> 1)
435 fprintf_unfiltered (gdb_stdlog
,
436 "Process record: add register num = %d to "
440 rec
= record_reg_alloc (regcache
, regnum
);
442 regcache_raw_read (regcache
, regnum
, record_get_loc (rec
));
444 record_arch_list_add (rec
);
449 /* Record the value of a region of memory whose address is ADDR and
450 length is LEN to record_arch_list. */
453 record_arch_list_add_mem (CORE_ADDR addr
, int len
)
455 struct record_entry
*rec
;
457 if (record_debug
> 1)
458 fprintf_unfiltered (gdb_stdlog
,
459 "Process record: add mem addr = %s len = %d to "
461 paddress (target_gdbarch
, addr
), len
);
463 if (!addr
) /* FIXME: Why? Some arch must permit it... */
466 rec
= record_mem_alloc (addr
, len
);
468 if (target_read_memory (addr
, record_get_loc (rec
), len
))
471 fprintf_unfiltered (gdb_stdlog
,
472 "Process record: error reading memory at "
473 "addr = %s len = %d.\n",
474 paddress (target_gdbarch
, addr
), len
);
475 record_mem_release (rec
);
479 record_arch_list_add (rec
);
484 /* Add a record_end type struct record_entry to record_arch_list. */
487 record_arch_list_add_end (void)
489 struct record_entry
*rec
;
491 if (record_debug
> 1)
492 fprintf_unfiltered (gdb_stdlog
,
493 "Process record: add end to arch list.\n");
495 rec
= record_end_alloc ();
496 rec
->u
.end
.sigval
= TARGET_SIGNAL_0
;
498 record_arch_list_add (rec
);
504 record_check_insn_num (int set_terminal
)
506 if (record_insn_max_num
)
508 gdb_assert (record_insn_num
<= record_insn_max_num
);
509 if (record_insn_num
== record_insn_max_num
)
511 /* Ask user what to do. */
512 if (record_stop_at_limit
)
516 target_terminal_ours ();
517 q
= yquery (_("Do you want to auto delete previous execution "
518 "log entries when record/replay buffer becomes "
519 "full (record stop-at-limit)?"));
521 target_terminal_inferior ();
523 record_stop_at_limit
= 0;
525 error (_("Process record: inferior program stopped."));
531 /* Before inferior step (when GDB record the running message, inferior
532 only can step), GDB will call this function to record the values to
533 record_list. This function will call gdbarch_process_record to
534 record the running message of inferior and set them to
535 record_arch_list, and add it to record_list. */
538 record_message_cleanups (void *ignore
)
540 record_list_release (record_arch_list_tail
);
543 struct record_message_args
{
544 struct regcache
*regcache
;
545 enum target_signal signal
;
549 record_message (void *args
)
552 struct record_message_args
*myargs
= args
;
553 struct gdbarch
*gdbarch
= get_regcache_arch (myargs
->regcache
);
554 struct cleanup
*old_cleanups
= make_cleanup (record_message_cleanups
, 0);
556 record_arch_list_head
= NULL
;
557 record_arch_list_tail
= NULL
;
559 /* Check record_insn_num. */
560 record_check_insn_num (1);
562 /* If gdb sends a signal value to target_resume,
563 save it in the 'end' field of the previous instruction.
565 Maybe process record should record what really happened,
566 rather than what gdb pretends has happened.
568 So if Linux delivered the signal to the child process during
569 the record mode, we will record it and deliver it again in
572 If user says "ignore this signal" during the record mode, then
573 it will be ignored again during the replay mode (no matter if
574 the user says something different, like "deliver this signal"
575 during the replay mode).
577 User should understand that nothing he does during the replay
578 mode will change the behavior of the child. If he tries,
579 then that is a user error.
581 But we should still deliver the signal to gdb during the replay,
582 if we delivered it during the recording. Therefore we should
583 record the signal during record_wait, not record_resume. */
584 if (record_list
!= &record_first
) /* FIXME better way to check */
586 gdb_assert (record_list
->type
== record_end
);
587 record_list
->u
.end
.sigval
= myargs
->signal
;
590 if (myargs
->signal
== TARGET_SIGNAL_0
591 || !gdbarch_process_record_signal_p (gdbarch
))
592 ret
= gdbarch_process_record (gdbarch
,
594 regcache_read_pc (myargs
->regcache
));
596 ret
= gdbarch_process_record_signal (gdbarch
,
601 error (_("Process record: inferior program stopped."));
603 error (_("Process record: failed to record execution log."));
605 discard_cleanups (old_cleanups
);
607 record_list
->next
= record_arch_list_head
;
608 record_arch_list_head
->prev
= record_list
;
609 record_list
= record_arch_list_tail
;
611 if (record_insn_num
== record_insn_max_num
&& record_insn_max_num
)
612 record_list_release_first ();
620 do_record_message (struct regcache
*regcache
,
621 enum target_signal signal
)
623 struct record_message_args args
;
625 args
.regcache
= regcache
;
626 args
.signal
= signal
;
627 return catch_errors (record_message
, &args
, NULL
, RETURN_MASK_ALL
);
630 /* Set to 1 if record_store_registers and record_xfer_partial
631 doesn't need record. */
633 static int record_gdb_operation_disable
= 0;
636 record_gdb_operation_disable_set (void)
638 struct cleanup
*old_cleanups
= NULL
;
641 make_cleanup_restore_integer (&record_gdb_operation_disable
);
642 record_gdb_operation_disable
= 1;
647 /* Execute one instruction from the record log. Each instruction in
648 the log will be represented by an arbitrary sequence of register
649 entries and memory entries, followed by an 'end' entry. */
652 record_exec_insn (struct regcache
*regcache
, struct gdbarch
*gdbarch
,
653 struct record_entry
*entry
)
657 case record_reg
: /* reg */
659 gdb_byte reg
[MAX_REGISTER_SIZE
];
661 if (record_debug
> 1)
662 fprintf_unfiltered (gdb_stdlog
,
663 "Process record: record_reg %s to "
664 "inferior num = %d.\n",
665 host_address_to_string (entry
),
668 regcache_cooked_read (regcache
, entry
->u
.reg
.num
, reg
);
669 regcache_cooked_write (regcache
, entry
->u
.reg
.num
,
670 record_get_loc (entry
));
671 memcpy (record_get_loc (entry
), reg
, entry
->u
.reg
.len
);
675 case record_mem
: /* mem */
677 /* Nothing to do if the entry is flagged not_accessible. */
678 if (!entry
->u
.mem
.mem_entry_not_accessible
)
680 gdb_byte
*mem
= alloca (entry
->u
.mem
.len
);
682 if (record_debug
> 1)
683 fprintf_unfiltered (gdb_stdlog
,
684 "Process record: record_mem %s to "
685 "inferior addr = %s len = %d.\n",
686 host_address_to_string (entry
),
687 paddress (gdbarch
, entry
->u
.mem
.addr
),
690 if (target_read_memory (entry
->u
.mem
.addr
, mem
, entry
->u
.mem
.len
))
692 entry
->u
.mem
.mem_entry_not_accessible
= 1;
694 warning (_("Process record: error reading memory at "
695 "addr = %s len = %d."),
696 paddress (gdbarch
, entry
->u
.mem
.addr
),
701 if (target_write_memory (entry
->u
.mem
.addr
,
702 record_get_loc (entry
),
705 entry
->u
.mem
.mem_entry_not_accessible
= 1;
707 warning (_("Process record: error writing memory at "
708 "addr = %s len = %d."),
709 paddress (gdbarch
, entry
->u
.mem
.addr
),
713 memcpy (record_get_loc (entry
), mem
, entry
->u
.mem
.len
);
721 /* "to_open" target method. Open the process record target. */
724 record_open (char *name
, int from_tty
)
726 struct target_ops
*t
;
729 fprintf_unfiltered (gdb_stdlog
, "Process record: record_open\n");
732 if (!target_has_execution
)
733 error (_("Process record: the program is not being run."));
735 error (_("Process record target can't debug inferior in non-stop mode "
737 if (target_async_permitted
)
738 error (_("Process record target can't debug inferior in asynchronous "
739 "mode (target-async)."));
741 if (!gdbarch_process_record_p (target_gdbarch
))
742 error (_("Process record: the current architecture doesn't support "
743 "record function."));
745 /* Check if record target is already running. */
746 if (current_target
.to_stratum
== record_stratum
)
747 error (_("Process record target already running. Use \"record stop\" to "
748 "stop record target first."));
750 /*Reset the beneath function pointers. */
751 record_beneath_to_resume
= NULL
;
752 record_beneath_to_wait
= NULL
;
753 record_beneath_to_store_registers
= NULL
;
754 record_beneath_to_xfer_partial
= NULL
;
755 record_beneath_to_insert_breakpoint
= NULL
;
756 record_beneath_to_remove_breakpoint
= NULL
;
758 /* Set the beneath function pointers. */
759 for (t
= current_target
.beneath
; t
!= NULL
; t
= t
->beneath
)
761 if (!record_beneath_to_resume
)
763 record_beneath_to_resume
= t
->to_resume
;
764 record_beneath_to_resume_ops
= t
;
766 if (!record_beneath_to_wait
)
768 record_beneath_to_wait
= t
->to_wait
;
769 record_beneath_to_wait_ops
= t
;
771 if (!record_beneath_to_store_registers
)
773 record_beneath_to_store_registers
= t
->to_store_registers
;
774 record_beneath_to_store_registers_ops
= t
;
776 if (!record_beneath_to_xfer_partial
)
778 record_beneath_to_xfer_partial
= t
->to_xfer_partial
;
779 record_beneath_to_xfer_partial_ops
= t
;
781 if (!record_beneath_to_insert_breakpoint
)
782 record_beneath_to_insert_breakpoint
= t
->to_insert_breakpoint
;
783 if (!record_beneath_to_remove_breakpoint
)
784 record_beneath_to_remove_breakpoint
= t
->to_remove_breakpoint
;
786 if (!record_beneath_to_resume
)
787 error (_("Process record can't get to_resume."));
788 if (!record_beneath_to_wait
)
789 error (_("Process record can't get to_wait."));
790 if (!record_beneath_to_store_registers
)
791 error (_("Process record can't get to_store_registers."));
792 if (!record_beneath_to_xfer_partial
)
793 error (_("Process record can't get to_xfer_partial."));
794 if (!record_beneath_to_insert_breakpoint
)
795 error (_("Process record can't get to_insert_breakpoint."));
796 if (!record_beneath_to_remove_breakpoint
)
797 error (_("Process record can't get to_remove_breakpoint."));
799 push_target (&record_ops
);
803 record_list
= &record_first
;
804 record_list
->next
= NULL
;
807 /* "to_close" target method. Close the process record target. */
810 record_close (int quitting
)
813 fprintf_unfiltered (gdb_stdlog
, "Process record: record_close\n");
815 record_list_release (record_list
);
818 static int record_resume_step
= 0;
819 static int record_resume_error
;
821 /* "to_resume" target method. Resume the process record target. */
824 record_resume (struct target_ops
*ops
, ptid_t ptid
, int step
,
825 enum target_signal signal
)
827 record_resume_step
= step
;
829 if (!RECORD_IS_REPLAY
)
831 if (do_record_message (get_current_regcache (), signal
))
833 record_resume_error
= 0;
837 record_resume_error
= 1;
840 record_beneath_to_resume (record_beneath_to_resume_ops
, ptid
, 1,
845 static int record_get_sig
= 0;
847 /* SIGINT signal handler, registered by "to_wait" method. */
850 record_sig_handler (int signo
)
853 fprintf_unfiltered (gdb_stdlog
, "Process record: get a signal\n");
855 /* It will break the running inferior in replay mode. */
856 record_resume_step
= 1;
858 /* It will let record_wait set inferior status to get the signal
864 record_wait_cleanups (void *ignore
)
866 if (execution_direction
== EXEC_REVERSE
)
868 if (record_list
->next
)
869 record_list
= record_list
->next
;
872 record_list
= record_list
->prev
;
875 /* "to_wait" target method for process record target.
877 In record mode, the target is always run in singlestep mode
878 (even when gdb says to continue). The to_wait method intercepts
879 the stop events and determines which ones are to be passed on to
880 gdb. Most stop events are just singlestep events that gdb is not
881 to know about, so the to_wait method just records them and keeps
884 In replay mode, this function emulates the recorded execution log,
885 one instruction at a time (forward or backward), and determines
889 record_wait (struct target_ops
*ops
,
890 ptid_t ptid
, struct target_waitstatus
*status
,
893 struct cleanup
*set_cleanups
= record_gdb_operation_disable_set ();
896 fprintf_unfiltered (gdb_stdlog
,
897 "Process record: record_wait "
898 "record_resume_step = %d\n",
901 if (!RECORD_IS_REPLAY
)
903 if (record_resume_error
)
905 /* If record_resume get error, return directly. */
906 status
->kind
= TARGET_WAITKIND_STOPPED
;
907 status
->value
.sig
= TARGET_SIGNAL_ABRT
;
908 return inferior_ptid
;
911 if (record_resume_step
)
913 /* This is a single step. */
914 return record_beneath_to_wait (record_beneath_to_wait_ops
,
915 ptid
, status
, options
);
919 /* This is not a single step. */
925 ret
= record_beneath_to_wait (record_beneath_to_wait_ops
,
926 ptid
, status
, options
);
928 /* Is this a SIGTRAP? */
929 if (status
->kind
== TARGET_WAITKIND_STOPPED
930 && status
->value
.sig
== TARGET_SIGNAL_TRAP
)
932 struct regcache
*regcache
;
934 /* Yes -- check if there is a breakpoint. */
935 registers_changed ();
936 regcache
= get_current_regcache ();
937 tmp_pc
= regcache_read_pc (regcache
);
938 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache
),
941 /* There is a breakpoint. GDB will want to stop. */
942 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
943 CORE_ADDR decr_pc_after_break
944 = gdbarch_decr_pc_after_break (gdbarch
);
945 if (decr_pc_after_break
)
946 regcache_write_pc (regcache
,
947 tmp_pc
+ decr_pc_after_break
);
951 /* There is not a breakpoint, and gdb is not
952 stepping, therefore gdb will not stop.
953 Therefore we will not return to gdb.
954 Record the insn and resume. */
955 if (!do_record_message (regcache
, TARGET_SIGNAL_0
))
958 record_beneath_to_resume (record_beneath_to_resume_ops
,
965 /* The inferior is broken by a breakpoint or a signal. */
974 struct regcache
*regcache
= get_current_regcache ();
975 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
976 int continue_flag
= 1;
977 int first_record_end
= 1;
978 struct cleanup
*old_cleanups
= make_cleanup (record_wait_cleanups
, 0);
981 status
->kind
= TARGET_WAITKIND_STOPPED
;
983 /* Check breakpoint when forward execute. */
984 if (execution_direction
== EXEC_FORWARD
)
986 tmp_pc
= regcache_read_pc (regcache
);
987 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache
),
991 fprintf_unfiltered (gdb_stdlog
,
992 "Process record: break at %s.\n",
993 paddress (gdbarch
, tmp_pc
));
994 if (gdbarch_decr_pc_after_break (gdbarch
)
995 && !record_resume_step
)
996 regcache_write_pc (regcache
,
998 gdbarch_decr_pc_after_break (gdbarch
));
1004 signal (SIGINT
, record_sig_handler
);
1005 /* If GDB is in terminal_inferior mode, it will not get the signal.
1006 And in GDB replay mode, GDB doesn't need to be in terminal_inferior
1007 mode, because inferior will not executed.
1008 Then set it to terminal_ours to make GDB get the signal. */
1009 target_terminal_ours ();
1011 /* In EXEC_FORWARD mode, record_list points to the tail of prev
1013 if (execution_direction
== EXEC_FORWARD
&& record_list
->next
)
1014 record_list
= record_list
->next
;
1016 /* Loop over the record_list, looking for the next place to
1020 /* Check for beginning and end of log. */
1021 if (execution_direction
== EXEC_REVERSE
1022 && record_list
== &record_first
)
1024 /* Hit beginning of record log in reverse. */
1025 status
->kind
= TARGET_WAITKIND_NO_HISTORY
;
1028 if (execution_direction
!= EXEC_REVERSE
&& !record_list
->next
)
1030 /* Hit end of record log going forward. */
1031 status
->kind
= TARGET_WAITKIND_NO_HISTORY
;
1035 record_exec_insn (regcache
, gdbarch
, record_list
);
1037 if (record_list
->type
== record_end
)
1039 if (record_debug
> 1)
1040 fprintf_unfiltered (gdb_stdlog
,
1041 "Process record: record_end %s to "
1043 host_address_to_string (record_list
));
1045 if (first_record_end
&& execution_direction
== EXEC_REVERSE
)
1047 /* When reverse excute, the first record_end is the part of
1048 current instruction. */
1049 first_record_end
= 0;
1053 /* In EXEC_REVERSE mode, this is the record_end of prev
1055 In EXEC_FORWARD mode, this is the record_end of current
1058 if (record_resume_step
)
1060 if (record_debug
> 1)
1061 fprintf_unfiltered (gdb_stdlog
,
1062 "Process record: step.\n");
1066 /* check breakpoint */
1067 tmp_pc
= regcache_read_pc (regcache
);
1068 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache
),
1072 fprintf_unfiltered (gdb_stdlog
,
1073 "Process record: break "
1075 paddress (gdbarch
, tmp_pc
));
1076 if (gdbarch_decr_pc_after_break (gdbarch
)
1077 && execution_direction
== EXEC_FORWARD
1078 && !record_resume_step
)
1079 regcache_write_pc (regcache
,
1081 gdbarch_decr_pc_after_break (gdbarch
));
1084 /* Check target signal */
1085 if (record_list
->u
.end
.sigval
!= TARGET_SIGNAL_0
)
1086 /* FIXME: better way to check */
1093 if (execution_direction
== EXEC_REVERSE
)
1095 if (record_list
->prev
)
1096 record_list
= record_list
->prev
;
1100 if (record_list
->next
)
1101 record_list
= record_list
->next
;
1105 while (continue_flag
);
1107 signal (SIGINT
, handle_sigint
);
1111 status
->value
.sig
= TARGET_SIGNAL_INT
;
1112 else if (record_list
->u
.end
.sigval
!= TARGET_SIGNAL_0
)
1113 /* FIXME: better way to check */
1114 status
->value
.sig
= record_list
->u
.end
.sigval
;
1116 status
->value
.sig
= TARGET_SIGNAL_TRAP
;
1118 discard_cleanups (old_cleanups
);
1121 do_cleanups (set_cleanups
);
1122 return inferior_ptid
;
1125 /* "to_disconnect" method for process record target. */
1128 record_disconnect (struct target_ops
*target
, char *args
, int from_tty
)
1131 fprintf_unfiltered (gdb_stdlog
, "Process record: record_disconnect\n");
1133 unpush_target (&record_ops
);
1134 target_disconnect (args
, from_tty
);
1137 /* "to_detach" method for process record target. */
1140 record_detach (struct target_ops
*ops
, char *args
, int from_tty
)
1143 fprintf_unfiltered (gdb_stdlog
, "Process record: record_detach\n");
1145 unpush_target (&record_ops
);
1146 target_detach (args
, from_tty
);
1149 /* "to_mourn_inferior" method for process record target. */
1152 record_mourn_inferior (struct target_ops
*ops
)
1155 fprintf_unfiltered (gdb_stdlog
, "Process record: "
1156 "record_mourn_inferior\n");
1158 unpush_target (&record_ops
);
1159 target_mourn_inferior ();
1162 /* Close process record target before killing the inferior process. */
1165 record_kill (struct target_ops
*ops
)
1168 fprintf_unfiltered (gdb_stdlog
, "Process record: record_kill\n");
1170 unpush_target (&record_ops
);
1174 /* Record registers change (by user or by GDB) to list as an instruction. */
1177 record_registers_change (struct regcache
*regcache
, int regnum
)
1179 /* Check record_insn_num. */
1180 record_check_insn_num (0);
1182 record_arch_list_head
= NULL
;
1183 record_arch_list_tail
= NULL
;
1188 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
1190 if (record_arch_list_add_reg (regcache
, i
))
1192 record_list_release (record_arch_list_tail
);
1193 error (_("Process record: failed to record execution log."));
1199 if (record_arch_list_add_reg (regcache
, regnum
))
1201 record_list_release (record_arch_list_tail
);
1202 error (_("Process record: failed to record execution log."));
1205 if (record_arch_list_add_end ())
1207 record_list_release (record_arch_list_tail
);
1208 error (_("Process record: failed to record execution log."));
1210 record_list
->next
= record_arch_list_head
;
1211 record_arch_list_head
->prev
= record_list
;
1212 record_list
= record_arch_list_tail
;
1214 if (record_insn_num
== record_insn_max_num
&& record_insn_max_num
)
1215 record_list_release_first ();
1220 /* "to_store_registers" method for process record target. */
1223 record_store_registers (struct target_ops
*ops
, struct regcache
*regcache
,
1226 if (!record_gdb_operation_disable
)
1228 if (RECORD_IS_REPLAY
)
1232 /* Let user choose if he wants to write register or not. */
1235 query (_("Because GDB is in replay mode, changing the "
1236 "value of a register will make the execution "
1237 "log unusable from this point onward. "
1238 "Change all registers?"));
1241 query (_("Because GDB is in replay mode, changing the value "
1242 "of a register will make the execution log unusable "
1243 "from this point onward. Change register %s?"),
1244 gdbarch_register_name (get_regcache_arch (regcache
),
1249 /* Invalidate the value of regcache that was set in function
1250 "regcache_raw_write". */
1255 i
< gdbarch_num_regs (get_regcache_arch (regcache
));
1257 regcache_invalidate (regcache
, i
);
1260 regcache_invalidate (regcache
, regno
);
1262 error (_("Process record canceled the operation."));
1265 /* Destroy the record from here forward. */
1266 record_list_release_following (record_list
);
1269 record_registers_change (regcache
, regno
);
1271 record_beneath_to_store_registers (record_beneath_to_store_registers_ops
,
1275 /* Behavior is conditional on RECORD_IS_REPLAY.
1276 In replay mode, we cannot write memory unles we are willing to
1277 invalidate the record/replay log from this point forward. */
1280 record_xfer_partial (struct target_ops
*ops
, enum target_object object
,
1281 const char *annex
, gdb_byte
*readbuf
,
1282 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
1284 if (!record_gdb_operation_disable
1285 && (object
== TARGET_OBJECT_MEMORY
1286 || object
== TARGET_OBJECT_RAW_MEMORY
) && writebuf
)
1288 if (RECORD_IS_REPLAY
)
1290 /* Let user choose if he wants to write memory or not. */
1291 if (!query (_("Because GDB is in replay mode, writing to memory "
1292 "will make the execution log unusable from this "
1293 "point onward. Write memory at address %s?"),
1294 paddress (target_gdbarch
, offset
)))
1295 error (_("Process record canceled the operation."));
1297 /* Destroy the record from here forward. */
1298 record_list_release_following (record_list
);
1301 /* Check record_insn_num */
1302 record_check_insn_num (0);
1304 /* Record registers change to list as an instruction. */
1305 record_arch_list_head
= NULL
;
1306 record_arch_list_tail
= NULL
;
1307 if (record_arch_list_add_mem (offset
, len
))
1309 record_list_release (record_arch_list_tail
);
1311 fprintf_unfiltered (gdb_stdlog
,
1312 _("Process record: failed to record "
1316 if (record_arch_list_add_end ())
1318 record_list_release (record_arch_list_tail
);
1320 fprintf_unfiltered (gdb_stdlog
,
1321 _("Process record: failed to record "
1325 record_list
->next
= record_arch_list_head
;
1326 record_arch_list_head
->prev
= record_list
;
1327 record_list
= record_arch_list_tail
;
1329 if (record_insn_num
== record_insn_max_num
&& record_insn_max_num
)
1330 record_list_release_first ();
1335 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops
,
1336 object
, annex
, readbuf
, writebuf
,
1340 /* Behavior is conditional on RECORD_IS_REPLAY.
1341 We will not actually insert or remove breakpoints when replaying,
1342 nor when recording. */
1345 record_insert_breakpoint (struct gdbarch
*gdbarch
,
1346 struct bp_target_info
*bp_tgt
)
1348 if (!RECORD_IS_REPLAY
)
1350 struct cleanup
*old_cleanups
= record_gdb_operation_disable_set ();
1351 int ret
= record_beneath_to_insert_breakpoint (gdbarch
, bp_tgt
);
1353 do_cleanups (old_cleanups
);
1361 /* "to_remove_breakpoint" method for process record target. */
1364 record_remove_breakpoint (struct gdbarch
*gdbarch
,
1365 struct bp_target_info
*bp_tgt
)
1367 if (!RECORD_IS_REPLAY
)
1369 struct cleanup
*old_cleanups
= record_gdb_operation_disable_set ();
1370 int ret
= record_beneath_to_remove_breakpoint (gdbarch
, bp_tgt
);
1372 do_cleanups (old_cleanups
);
1380 /* "to_can_execute_reverse" method for process record target. */
1382 record_can_execute_reverse (void)
1388 init_record_ops (void)
1390 record_ops
.to_shortname
= "record";
1391 record_ops
.to_longname
= "Process record and replay target";
1393 "Log program while executing and replay execution from log.";
1394 record_ops
.to_open
= record_open
;
1395 record_ops
.to_close
= record_close
;
1396 record_ops
.to_resume
= record_resume
;
1397 record_ops
.to_wait
= record_wait
;
1398 record_ops
.to_disconnect
= record_disconnect
;
1399 record_ops
.to_detach
= record_detach
;
1400 record_ops
.to_mourn_inferior
= record_mourn_inferior
;
1401 record_ops
.to_kill
= record_kill
;
1402 record_ops
.to_create_inferior
= find_default_create_inferior
;
1403 record_ops
.to_store_registers
= record_store_registers
;
1404 record_ops
.to_xfer_partial
= record_xfer_partial
;
1405 record_ops
.to_insert_breakpoint
= record_insert_breakpoint
;
1406 record_ops
.to_remove_breakpoint
= record_remove_breakpoint
;
1407 record_ops
.to_can_execute_reverse
= record_can_execute_reverse
;
1408 record_ops
.to_stratum
= record_stratum
;
1409 record_ops
.to_magic
= OPS_MAGIC
;
1412 /* Implement "show record debug" command. */
1415 show_record_debug (struct ui_file
*file
, int from_tty
,
1416 struct cmd_list_element
*c
, const char *value
)
1418 fprintf_filtered (file
, _("Debugging of process record target is %s.\n"),
1422 /* Alias for "target record". */
1425 cmd_record_start (char *args
, int from_tty
)
1427 execute_command ("target record", from_tty
);
1430 /* Truncate the record log from the present point
1431 of replay until the end. */
1434 cmd_record_delete (char *args
, int from_tty
)
1436 if (current_target
.to_stratum
== record_stratum
)
1438 if (RECORD_IS_REPLAY
)
1440 if (!from_tty
|| query (_("Delete the log from this point forward "
1441 "and begin to record the running message "
1443 record_list_release_following (record_list
);
1446 printf_unfiltered (_("Already at end of record list.\n"));
1450 printf_unfiltered (_("Process record is not started.\n"));
1453 /* Implement the "stoprecord" or "record stop" command. */
1456 cmd_record_stop (char *args
, int from_tty
)
1458 if (current_target
.to_stratum
== record_stratum
)
1460 unpush_target (&record_ops
);
1461 printf_unfiltered (_("Process record is stoped and all execution "
1462 "log is deleted.\n"));
1465 printf_unfiltered (_("Process record is not started.\n"));
1468 /* Set upper limit of record log size. */
1471 set_record_insn_max_num (char *args
, int from_tty
, struct cmd_list_element
*c
)
1473 if (record_insn_num
> record_insn_max_num
&& record_insn_max_num
)
1475 /* Count down record_insn_num while releasing records from list. */
1476 while (record_insn_num
> record_insn_max_num
)
1478 record_list_release_first ();
1484 /* Print the current index into the record log (number of insns recorded
1488 show_record_insn_number (char *ignore
, int from_tty
)
1490 printf_unfiltered (_("Record instruction number is %d.\n"),
1494 static struct cmd_list_element
*record_cmdlist
, *set_record_cmdlist
,
1495 *show_record_cmdlist
, *info_record_cmdlist
;
1498 set_record_command (char *args
, int from_tty
)
1500 printf_unfiltered (_("\
1501 \"set record\" must be followed by an apporpriate subcommand.\n"));
1502 help_list (set_record_cmdlist
, "set record ", all_commands
, gdb_stdout
);
1506 show_record_command (char *args
, int from_tty
)
1508 cmd_show_list (show_record_cmdlist
, from_tty
, "");
1512 info_record_command (char *args
, int from_tty
)
1514 cmd_show_list (info_record_cmdlist
, from_tty
, "");
1518 _initialize_record (void)
1520 /* Init record_first. */
1521 record_first
.prev
= NULL
;
1522 record_first
.next
= NULL
;
1523 record_first
.type
= record_end
;
1526 add_target (&record_ops
);
1528 add_setshow_zinteger_cmd ("record", no_class
, &record_debug
,
1529 _("Set debugging of record/replay feature."),
1530 _("Show debugging of record/replay feature."),
1531 _("When enabled, debugging output for "
1532 "record/replay feature is displayed."),
1533 NULL
, show_record_debug
, &setdebuglist
,
1536 add_prefix_cmd ("record", class_obscure
, cmd_record_start
,
1537 _("Abbreviated form of \"target record\" command."),
1538 &record_cmdlist
, "record ", 0, &cmdlist
);
1539 add_com_alias ("rec", "record", class_obscure
, 1);
1540 add_prefix_cmd ("record", class_support
, set_record_command
,
1541 _("Set record options"), &set_record_cmdlist
,
1542 "set record ", 0, &setlist
);
1543 add_alias_cmd ("rec", "record", class_obscure
, 1, &setlist
);
1544 add_prefix_cmd ("record", class_support
, show_record_command
,
1545 _("Show record options"), &show_record_cmdlist
,
1546 "show record ", 0, &showlist
);
1547 add_alias_cmd ("rec", "record", class_obscure
, 1, &showlist
);
1548 add_prefix_cmd ("record", class_support
, info_record_command
,
1549 _("Info record options"), &info_record_cmdlist
,
1550 "info record ", 0, &infolist
);
1551 add_alias_cmd ("rec", "record", class_obscure
, 1, &infolist
);
1554 add_cmd ("delete", class_obscure
, cmd_record_delete
,
1555 _("Delete the rest of execution log and start recording it anew."),
1557 add_alias_cmd ("d", "delete", class_obscure
, 1, &record_cmdlist
);
1558 add_alias_cmd ("del", "delete", class_obscure
, 1, &record_cmdlist
);
1560 add_cmd ("stop", class_obscure
, cmd_record_stop
,
1561 _("Stop the record/replay target."),
1563 add_alias_cmd ("s", "stop", class_obscure
, 1, &record_cmdlist
);
1565 /* Record instructions number limit command. */
1566 add_setshow_boolean_cmd ("stop-at-limit", no_class
,
1567 &record_stop_at_limit
, _("\
1568 Set whether record/replay stops when record/replay buffer becomes full."), _("\
1569 Show whether record/replay stops when record/replay buffer becomes full."), _("\
1571 When ON, if the record/replay buffer becomes full, ask user what to do.\n\
1572 When OFF, if the record/replay buffer becomes full,\n\
1573 delete the oldest recorded instruction to make room for each new one."),
1575 &set_record_cmdlist
, &show_record_cmdlist
);
1576 add_setshow_uinteger_cmd ("insn-number-max", no_class
,
1577 &record_insn_max_num
,
1578 _("Set record/replay buffer limit."),
1579 _("Show record/replay buffer limit."), _("\
1580 Set the maximum number of instructions to be stored in the\n\
1581 record/replay buffer. Zero means unlimited. Default is 200000."),
1582 set_record_insn_max_num
,
1583 NULL
, &set_record_cmdlist
, &show_record_cmdlist
);
1584 add_cmd ("insn-number", class_obscure
, show_record_insn_number
,
1585 _("Show the current number of instructions in the "
1586 "record/replay buffer."), &info_record_cmdlist
);