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
;
104 /* This is the data structure that makes up the execution log.
106 The execution log consists of a single linked list of entries
107 of type "struct record_entry". It is doubly linked so that it
108 can be traversed in either direction.
110 The start of the list is anchored by a struct called
111 "record_first". The pointer "record_list" either points to the
112 last entry that was added to the list (in record mode), or to the
113 next entry in the list that will be executed (in replay mode).
115 Each list element (struct record_entry), in addition to next and
116 prev pointers, consists of a union of three entry types: mem, reg,
117 and end. A field called "type" determines which entry type is
118 represented by a given list element.
120 Each instruction that is added to the execution log is represented
121 by a variable number of list elements ('entries'). The instruction
122 will have one "reg" entry for each register that is changed by
123 executing the instruction (including the PC in every case). It
124 will also have one "mem" entry for each memory change. Finally,
125 each instruction will have an "end" entry that separates it from
126 the changes associated with the next instruction. */
130 struct record_entry
*prev
;
131 struct record_entry
*next
;
132 enum record_type type
;
136 struct record_reg_entry reg
;
138 struct record_mem_entry mem
;
140 struct record_end_entry end
;
144 /* This is the debug switch for process record. */
145 int record_debug
= 0;
147 /* The following variables are used for managing the linked list that
148 represents the execution log.
150 record_first is the anchor that holds down the beginning of the list.
152 record_list serves two functions:
153 1) In record mode, it anchors the end of the list.
154 2) In replay mode, it traverses the list and points to
155 the next instruction that must be emulated.
157 record_arch_list_head and record_arch_list_tail are used to manage
158 a separate list, which is used to build up the change elements of
159 the currently executing instruction during record mode. When this
160 instruction has been completely annotated in the "arch list", it
161 will be appended to the main execution log. */
163 static struct record_entry record_first
;
164 static struct record_entry
*record_list
= &record_first
;
165 static struct record_entry
*record_arch_list_head
= NULL
;
166 static struct record_entry
*record_arch_list_tail
= NULL
;
168 /* 1 ask user. 0 auto delete the last struct record_entry. */
169 static int record_stop_at_limit
= 1;
170 /* Maximum allowed number of insns in execution log. */
171 static unsigned int record_insn_max_num
= DEFAULT_RECORD_INSN_MAX_NUM
;
172 /* Actual count of insns presently in execution log. */
173 static int record_insn_num
= 0;
174 /* Count of insns logged so far (may be larger
175 than count of insns presently in execution log). */
176 static ULONGEST record_insn_count
;
178 /* The target_ops of process record. */
179 static struct target_ops record_ops
;
181 /* The beneath function pointers. */
182 static struct target_ops
*record_beneath_to_resume_ops
;
183 static void (*record_beneath_to_resume
) (struct target_ops
*, ptid_t
, int,
185 static struct target_ops
*record_beneath_to_wait_ops
;
186 static ptid_t (*record_beneath_to_wait
) (struct target_ops
*, ptid_t
,
187 struct target_waitstatus
*,
189 static struct target_ops
*record_beneath_to_store_registers_ops
;
190 static void (*record_beneath_to_store_registers
) (struct target_ops
*,
193 static struct target_ops
*record_beneath_to_xfer_partial_ops
;
194 static LONGEST (*record_beneath_to_xfer_partial
) (struct target_ops
*ops
,
195 enum target_object object
,
198 const gdb_byte
*writebuf
,
201 static int (*record_beneath_to_insert_breakpoint
) (struct gdbarch
*,
202 struct bp_target_info
*);
203 static int (*record_beneath_to_remove_breakpoint
) (struct gdbarch
*,
204 struct bp_target_info
*);
206 /* Alloc and free functions for record_reg, record_mem, and record_end
209 /* Alloc a record_reg record entry. */
211 static inline struct record_entry
*
212 record_reg_alloc (struct regcache
*regcache
, int regnum
)
214 struct record_entry
*rec
;
215 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
217 rec
= (struct record_entry
*) xcalloc (1, sizeof (struct record_entry
));
218 rec
->type
= record_reg
;
219 rec
->u
.reg
.num
= regnum
;
220 rec
->u
.reg
.len
= register_size (gdbarch
, regnum
);
221 if (rec
->u
.reg
.len
> sizeof (rec
->u
.reg
.u
.buf
))
222 rec
->u
.reg
.u
.ptr
= (gdb_byte
*) xmalloc (rec
->u
.reg
.len
);
227 /* Free a record_reg record entry. */
230 record_reg_release (struct record_entry
*rec
)
232 gdb_assert (rec
->type
== record_reg
);
233 if (rec
->u
.reg
.len
> sizeof (rec
->u
.reg
.u
.buf
))
234 xfree (rec
->u
.reg
.u
.ptr
);
238 /* Alloc a record_mem record entry. */
240 static inline struct record_entry
*
241 record_mem_alloc (CORE_ADDR addr
, int len
)
243 struct record_entry
*rec
;
245 rec
= (struct record_entry
*) xcalloc (1, sizeof (struct record_entry
));
246 rec
->type
= record_mem
;
247 rec
->u
.mem
.addr
= addr
;
248 rec
->u
.mem
.len
= len
;
249 if (rec
->u
.mem
.len
> sizeof (rec
->u
.mem
.u
.buf
))
250 rec
->u
.mem
.u
.ptr
= (gdb_byte
*) xmalloc (len
);
255 /* Free a record_mem record entry. */
258 record_mem_release (struct record_entry
*rec
)
260 gdb_assert (rec
->type
== record_mem
);
261 if (rec
->u
.mem
.len
> sizeof (rec
->u
.mem
.u
.buf
))
262 xfree (rec
->u
.mem
.u
.ptr
);
266 /* Alloc a record_end record entry. */
268 static inline struct record_entry
*
269 record_end_alloc (void)
271 struct record_entry
*rec
;
273 rec
= (struct record_entry
*) xcalloc (1, sizeof (struct record_entry
));
274 rec
->type
= record_end
;
279 /* Free a record_end record entry. */
282 record_end_release (struct record_entry
*rec
)
287 /* Free one record entry, any type.
288 Return entry->type, in case caller wants to know. */
290 static inline enum record_type
291 record_entry_release (struct record_entry
*rec
)
293 enum record_type type
= rec
->type
;
297 record_reg_release (rec
);
300 record_mem_release (rec
);
303 record_end_release (rec
);
309 /* Free all record entries in list pointed to by REC. */
312 record_list_release (struct record_entry
*rec
)
323 record_entry_release (rec
->next
);
326 if (rec
== &record_first
)
329 record_first
.next
= NULL
;
332 record_entry_release (rec
);
335 /* Free all record entries forward of the given list position. */
338 record_list_release_following (struct record_entry
*rec
)
340 struct record_entry
*tmp
= rec
->next
;
346 if (record_entry_release (tmp
) == record_end
)
355 /* Delete the first instruction from the beginning of the log, to make
356 room for adding a new instruction at the end of the log.
358 Note -- this function does not modify record_insn_num. */
361 record_list_release_first (void)
363 struct record_entry
*tmp
;
365 if (!record_first
.next
)
368 /* Loop until a record_end. */
371 /* Cut record_first.next out of the linked list. */
372 tmp
= record_first
.next
;
373 record_first
.next
= tmp
->next
;
374 tmp
->next
->prev
= &record_first
;
376 /* tmp is now isolated, and can be deleted. */
377 if (record_entry_release (tmp
) == record_end
)
378 break; /* End loop at first record_end. */
380 if (!record_first
.next
)
382 gdb_assert (record_insn_num
== 1);
383 break; /* End loop when list is empty. */
388 /* Add a struct record_entry to record_arch_list. */
391 record_arch_list_add (struct record_entry
*rec
)
393 if (record_debug
> 1)
394 fprintf_unfiltered (gdb_stdlog
,
395 "Process record: record_arch_list_add %s.\n",
396 host_address_to_string (rec
));
398 if (record_arch_list_tail
)
400 record_arch_list_tail
->next
= rec
;
401 rec
->prev
= record_arch_list_tail
;
402 record_arch_list_tail
= rec
;
406 record_arch_list_head
= rec
;
407 record_arch_list_tail
= rec
;
411 /* Return the value storage location of a record entry. */
412 static inline gdb_byte
*
413 record_get_loc (struct record_entry
*rec
)
417 if (rec
->u
.mem
.len
> sizeof (rec
->u
.mem
.u
.buf
))
418 return rec
->u
.mem
.u
.ptr
;
420 return rec
->u
.mem
.u
.buf
;
422 if (rec
->u
.reg
.len
> sizeof (rec
->u
.reg
.u
.buf
))
423 return rec
->u
.reg
.u
.ptr
;
425 return rec
->u
.reg
.u
.buf
;
433 /* Record the value of a register NUM to record_arch_list. */
436 record_arch_list_add_reg (struct regcache
*regcache
, int regnum
)
438 struct record_entry
*rec
;
440 if (record_debug
> 1)
441 fprintf_unfiltered (gdb_stdlog
,
442 "Process record: add register num = %d to "
446 rec
= record_reg_alloc (regcache
, regnum
);
448 regcache_raw_read (regcache
, regnum
, record_get_loc (rec
));
450 record_arch_list_add (rec
);
455 /* Record the value of a region of memory whose address is ADDR and
456 length is LEN to record_arch_list. */
459 record_arch_list_add_mem (CORE_ADDR addr
, int len
)
461 struct record_entry
*rec
;
463 if (record_debug
> 1)
464 fprintf_unfiltered (gdb_stdlog
,
465 "Process record: add mem addr = %s len = %d to "
467 paddress (target_gdbarch
, addr
), len
);
469 if (!addr
) /* FIXME: Why? Some arch must permit it... */
472 rec
= record_mem_alloc (addr
, len
);
474 if (target_read_memory (addr
, record_get_loc (rec
), len
))
477 fprintf_unfiltered (gdb_stdlog
,
478 "Process record: error reading memory at "
479 "addr = %s len = %d.\n",
480 paddress (target_gdbarch
, addr
), len
);
481 record_mem_release (rec
);
485 record_arch_list_add (rec
);
490 /* Add a record_end type struct record_entry to record_arch_list. */
493 record_arch_list_add_end (void)
495 struct record_entry
*rec
;
497 if (record_debug
> 1)
498 fprintf_unfiltered (gdb_stdlog
,
499 "Process record: add end to arch list.\n");
501 rec
= record_end_alloc ();
502 rec
->u
.end
.sigval
= TARGET_SIGNAL_0
;
503 rec
->u
.end
.insn_num
= ++record_insn_count
;
505 record_arch_list_add (rec
);
511 record_check_insn_num (int set_terminal
)
513 if (record_insn_max_num
)
515 gdb_assert (record_insn_num
<= record_insn_max_num
);
516 if (record_insn_num
== record_insn_max_num
)
518 /* Ask user what to do. */
519 if (record_stop_at_limit
)
523 target_terminal_ours ();
524 q
= yquery (_("Do you want to auto delete previous execution "
525 "log entries when record/replay buffer becomes "
526 "full (record stop-at-limit)?"));
528 target_terminal_inferior ();
530 record_stop_at_limit
= 0;
532 error (_("Process record: inferior program stopped."));
538 /* Before inferior step (when GDB record the running message, inferior
539 only can step), GDB will call this function to record the values to
540 record_list. This function will call gdbarch_process_record to
541 record the running message of inferior and set them to
542 record_arch_list, and add it to record_list. */
545 record_message_cleanups (void *ignore
)
547 record_list_release (record_arch_list_tail
);
550 struct record_message_args
{
551 struct regcache
*regcache
;
552 enum target_signal signal
;
556 record_message (void *args
)
559 struct record_message_args
*myargs
= args
;
560 struct gdbarch
*gdbarch
= get_regcache_arch (myargs
->regcache
);
561 struct cleanup
*old_cleanups
= make_cleanup (record_message_cleanups
, 0);
563 record_arch_list_head
= NULL
;
564 record_arch_list_tail
= NULL
;
566 /* Check record_insn_num. */
567 record_check_insn_num (1);
569 /* If gdb sends a signal value to target_resume,
570 save it in the 'end' field of the previous instruction.
572 Maybe process record should record what really happened,
573 rather than what gdb pretends has happened.
575 So if Linux delivered the signal to the child process during
576 the record mode, we will record it and deliver it again in
579 If user says "ignore this signal" during the record mode, then
580 it will be ignored again during the replay mode (no matter if
581 the user says something different, like "deliver this signal"
582 during the replay mode).
584 User should understand that nothing he does during the replay
585 mode will change the behavior of the child. If he tries,
586 then that is a user error.
588 But we should still deliver the signal to gdb during the replay,
589 if we delivered it during the recording. Therefore we should
590 record the signal during record_wait, not record_resume. */
591 if (record_list
!= &record_first
) /* FIXME better way to check */
593 gdb_assert (record_list
->type
== record_end
);
594 record_list
->u
.end
.sigval
= myargs
->signal
;
597 if (myargs
->signal
== TARGET_SIGNAL_0
598 || !gdbarch_process_record_signal_p (gdbarch
))
599 ret
= gdbarch_process_record (gdbarch
,
601 regcache_read_pc (myargs
->regcache
));
603 ret
= gdbarch_process_record_signal (gdbarch
,
608 error (_("Process record: inferior program stopped."));
610 error (_("Process record: failed to record execution log."));
612 discard_cleanups (old_cleanups
);
614 record_list
->next
= record_arch_list_head
;
615 record_arch_list_head
->prev
= record_list
;
616 record_list
= record_arch_list_tail
;
618 if (record_insn_num
== record_insn_max_num
&& record_insn_max_num
)
619 record_list_release_first ();
627 do_record_message (struct regcache
*regcache
,
628 enum target_signal signal
)
630 struct record_message_args args
;
632 args
.regcache
= regcache
;
633 args
.signal
= signal
;
634 return catch_errors (record_message
, &args
, NULL
, RETURN_MASK_ALL
);
637 /* Set to 1 if record_store_registers and record_xfer_partial
638 doesn't need record. */
640 static int record_gdb_operation_disable
= 0;
643 record_gdb_operation_disable_set (void)
645 struct cleanup
*old_cleanups
= NULL
;
648 make_cleanup_restore_integer (&record_gdb_operation_disable
);
649 record_gdb_operation_disable
= 1;
654 /* Execute one instruction from the record log. Each instruction in
655 the log will be represented by an arbitrary sequence of register
656 entries and memory entries, followed by an 'end' entry. */
659 record_exec_insn (struct regcache
*regcache
, struct gdbarch
*gdbarch
,
660 struct record_entry
*entry
)
664 case record_reg
: /* reg */
666 gdb_byte reg
[MAX_REGISTER_SIZE
];
668 if (record_debug
> 1)
669 fprintf_unfiltered (gdb_stdlog
,
670 "Process record: record_reg %s to "
671 "inferior num = %d.\n",
672 host_address_to_string (entry
),
675 regcache_cooked_read (regcache
, entry
->u
.reg
.num
, reg
);
676 regcache_cooked_write (regcache
, entry
->u
.reg
.num
,
677 record_get_loc (entry
));
678 memcpy (record_get_loc (entry
), reg
, entry
->u
.reg
.len
);
682 case record_mem
: /* mem */
684 /* Nothing to do if the entry is flagged not_accessible. */
685 if (!entry
->u
.mem
.mem_entry_not_accessible
)
687 gdb_byte
*mem
= alloca (entry
->u
.mem
.len
);
689 if (record_debug
> 1)
690 fprintf_unfiltered (gdb_stdlog
,
691 "Process record: record_mem %s to "
692 "inferior addr = %s len = %d.\n",
693 host_address_to_string (entry
),
694 paddress (gdbarch
, entry
->u
.mem
.addr
),
697 if (target_read_memory (entry
->u
.mem
.addr
, mem
, entry
->u
.mem
.len
))
699 entry
->u
.mem
.mem_entry_not_accessible
= 1;
701 warning (_("Process record: error reading memory at "
702 "addr = %s len = %d."),
703 paddress (gdbarch
, entry
->u
.mem
.addr
),
708 if (target_write_memory (entry
->u
.mem
.addr
,
709 record_get_loc (entry
),
712 entry
->u
.mem
.mem_entry_not_accessible
= 1;
714 warning (_("Process record: error writing memory at "
715 "addr = %s len = %d."),
716 paddress (gdbarch
, entry
->u
.mem
.addr
),
720 memcpy (record_get_loc (entry
), mem
, entry
->u
.mem
.len
);
728 /* "to_open" target method. Open the process record target. */
731 record_open (char *name
, int from_tty
)
733 struct target_ops
*t
;
736 fprintf_unfiltered (gdb_stdlog
, "Process record: record_open\n");
739 if (!target_has_execution
)
740 error (_("Process record: the program is not being run."));
742 error (_("Process record target can't debug inferior in non-stop mode "
744 if (target_async_permitted
)
745 error (_("Process record target can't debug inferior in asynchronous "
746 "mode (target-async)."));
748 if (!gdbarch_process_record_p (target_gdbarch
))
749 error (_("Process record: the current architecture doesn't support "
750 "record function."));
752 /* Check if record target is already running. */
753 if (current_target
.to_stratum
== record_stratum
)
754 error (_("Process record target already running. Use \"record stop\" to "
755 "stop record target first."));
757 /*Reset the beneath function pointers. */
758 record_beneath_to_resume
= NULL
;
759 record_beneath_to_wait
= NULL
;
760 record_beneath_to_store_registers
= NULL
;
761 record_beneath_to_xfer_partial
= NULL
;
762 record_beneath_to_insert_breakpoint
= NULL
;
763 record_beneath_to_remove_breakpoint
= NULL
;
765 /* Set the beneath function pointers. */
766 for (t
= current_target
.beneath
; t
!= NULL
; t
= t
->beneath
)
768 if (!record_beneath_to_resume
)
770 record_beneath_to_resume
= t
->to_resume
;
771 record_beneath_to_resume_ops
= t
;
773 if (!record_beneath_to_wait
)
775 record_beneath_to_wait
= t
->to_wait
;
776 record_beneath_to_wait_ops
= t
;
778 if (!record_beneath_to_store_registers
)
780 record_beneath_to_store_registers
= t
->to_store_registers
;
781 record_beneath_to_store_registers_ops
= t
;
783 if (!record_beneath_to_xfer_partial
)
785 record_beneath_to_xfer_partial
= t
->to_xfer_partial
;
786 record_beneath_to_xfer_partial_ops
= t
;
788 if (!record_beneath_to_insert_breakpoint
)
789 record_beneath_to_insert_breakpoint
= t
->to_insert_breakpoint
;
790 if (!record_beneath_to_remove_breakpoint
)
791 record_beneath_to_remove_breakpoint
= t
->to_remove_breakpoint
;
793 if (!record_beneath_to_resume
)
794 error (_("Process record can't get to_resume."));
795 if (!record_beneath_to_wait
)
796 error (_("Process record can't get to_wait."));
797 if (!record_beneath_to_store_registers
)
798 error (_("Process record can't get to_store_registers."));
799 if (!record_beneath_to_xfer_partial
)
800 error (_("Process record can't get to_xfer_partial."));
801 if (!record_beneath_to_insert_breakpoint
)
802 error (_("Process record can't get to_insert_breakpoint."));
803 if (!record_beneath_to_remove_breakpoint
)
804 error (_("Process record can't get to_remove_breakpoint."));
806 push_target (&record_ops
);
810 record_insn_count
= 0;
811 record_list
= &record_first
;
812 record_list
->next
= NULL
;
815 /* "to_close" target method. Close the process record target. */
818 record_close (int quitting
)
821 fprintf_unfiltered (gdb_stdlog
, "Process record: record_close\n");
823 record_list_release (record_list
);
826 static int record_resume_step
= 0;
827 static int record_resume_error
;
829 /* "to_resume" target method. Resume the process record target. */
832 record_resume (struct target_ops
*ops
, ptid_t ptid
, int step
,
833 enum target_signal signal
)
835 record_resume_step
= step
;
837 if (!RECORD_IS_REPLAY
)
839 if (do_record_message (get_current_regcache (), signal
))
841 record_resume_error
= 0;
845 record_resume_error
= 1;
848 record_beneath_to_resume (record_beneath_to_resume_ops
, ptid
, 1,
853 static int record_get_sig
= 0;
855 /* SIGINT signal handler, registered by "to_wait" method. */
858 record_sig_handler (int signo
)
861 fprintf_unfiltered (gdb_stdlog
, "Process record: get a signal\n");
863 /* It will break the running inferior in replay mode. */
864 record_resume_step
= 1;
866 /* It will let record_wait set inferior status to get the signal
872 record_wait_cleanups (void *ignore
)
874 if (execution_direction
== EXEC_REVERSE
)
876 if (record_list
->next
)
877 record_list
= record_list
->next
;
880 record_list
= record_list
->prev
;
883 /* "to_wait" target method for process record target.
885 In record mode, the target is always run in singlestep mode
886 (even when gdb says to continue). The to_wait method intercepts
887 the stop events and determines which ones are to be passed on to
888 gdb. Most stop events are just singlestep events that gdb is not
889 to know about, so the to_wait method just records them and keeps
892 In replay mode, this function emulates the recorded execution log,
893 one instruction at a time (forward or backward), and determines
897 record_wait (struct target_ops
*ops
,
898 ptid_t ptid
, struct target_waitstatus
*status
,
901 struct cleanup
*set_cleanups
= record_gdb_operation_disable_set ();
904 fprintf_unfiltered (gdb_stdlog
,
905 "Process record: record_wait "
906 "record_resume_step = %d\n",
909 if (!RECORD_IS_REPLAY
)
911 if (record_resume_error
)
913 /* If record_resume get error, return directly. */
914 status
->kind
= TARGET_WAITKIND_STOPPED
;
915 status
->value
.sig
= TARGET_SIGNAL_ABRT
;
916 return inferior_ptid
;
919 if (record_resume_step
)
921 /* This is a single step. */
922 return record_beneath_to_wait (record_beneath_to_wait_ops
,
923 ptid
, status
, options
);
927 /* This is not a single step. */
933 ret
= record_beneath_to_wait (record_beneath_to_wait_ops
,
934 ptid
, status
, options
);
936 /* Is this a SIGTRAP? */
937 if (status
->kind
== TARGET_WAITKIND_STOPPED
938 && status
->value
.sig
== TARGET_SIGNAL_TRAP
)
940 struct regcache
*regcache
;
942 /* Yes -- check if there is a breakpoint. */
943 registers_changed ();
944 regcache
= get_current_regcache ();
945 tmp_pc
= regcache_read_pc (regcache
);
946 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache
),
949 /* There is a breakpoint. GDB will want to stop. */
950 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
951 CORE_ADDR decr_pc_after_break
952 = gdbarch_decr_pc_after_break (gdbarch
);
953 if (decr_pc_after_break
)
954 regcache_write_pc (regcache
,
955 tmp_pc
+ decr_pc_after_break
);
959 /* There is not a breakpoint, and gdb is not
960 stepping, therefore gdb will not stop.
961 Therefore we will not return to gdb.
962 Record the insn and resume. */
963 if (!do_record_message (regcache
, TARGET_SIGNAL_0
))
966 record_beneath_to_resume (record_beneath_to_resume_ops
,
973 /* The inferior is broken by a breakpoint or a signal. */
982 struct regcache
*regcache
= get_current_regcache ();
983 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
984 int continue_flag
= 1;
985 int first_record_end
= 1;
986 struct cleanup
*old_cleanups
= make_cleanup (record_wait_cleanups
, 0);
989 status
->kind
= TARGET_WAITKIND_STOPPED
;
991 /* Check breakpoint when forward execute. */
992 if (execution_direction
== EXEC_FORWARD
)
994 tmp_pc
= regcache_read_pc (regcache
);
995 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache
),
999 fprintf_unfiltered (gdb_stdlog
,
1000 "Process record: break at %s.\n",
1001 paddress (gdbarch
, tmp_pc
));
1002 if (gdbarch_decr_pc_after_break (gdbarch
)
1003 && !record_resume_step
)
1004 regcache_write_pc (regcache
,
1006 gdbarch_decr_pc_after_break (gdbarch
));
1012 signal (SIGINT
, record_sig_handler
);
1013 /* If GDB is in terminal_inferior mode, it will not get the signal.
1014 And in GDB replay mode, GDB doesn't need to be in terminal_inferior
1015 mode, because inferior will not executed.
1016 Then set it to terminal_ours to make GDB get the signal. */
1017 target_terminal_ours ();
1019 /* In EXEC_FORWARD mode, record_list points to the tail of prev
1021 if (execution_direction
== EXEC_FORWARD
&& record_list
->next
)
1022 record_list
= record_list
->next
;
1024 /* Loop over the record_list, looking for the next place to
1028 /* Check for beginning and end of log. */
1029 if (execution_direction
== EXEC_REVERSE
1030 && record_list
== &record_first
)
1032 /* Hit beginning of record log in reverse. */
1033 status
->kind
= TARGET_WAITKIND_NO_HISTORY
;
1036 if (execution_direction
!= EXEC_REVERSE
&& !record_list
->next
)
1038 /* Hit end of record log going forward. */
1039 status
->kind
= TARGET_WAITKIND_NO_HISTORY
;
1043 record_exec_insn (regcache
, gdbarch
, record_list
);
1045 if (record_list
->type
== record_end
)
1047 if (record_debug
> 1)
1048 fprintf_unfiltered (gdb_stdlog
,
1049 "Process record: record_end %s to "
1051 host_address_to_string (record_list
));
1053 if (first_record_end
&& execution_direction
== EXEC_REVERSE
)
1055 /* When reverse excute, the first record_end is the part of
1056 current instruction. */
1057 first_record_end
= 0;
1061 /* In EXEC_REVERSE mode, this is the record_end of prev
1063 In EXEC_FORWARD mode, this is the record_end of current
1066 if (record_resume_step
)
1068 if (record_debug
> 1)
1069 fprintf_unfiltered (gdb_stdlog
,
1070 "Process record: step.\n");
1074 /* check breakpoint */
1075 tmp_pc
= regcache_read_pc (regcache
);
1076 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache
),
1080 fprintf_unfiltered (gdb_stdlog
,
1081 "Process record: break "
1083 paddress (gdbarch
, tmp_pc
));
1084 if (gdbarch_decr_pc_after_break (gdbarch
)
1085 && execution_direction
== EXEC_FORWARD
1086 && !record_resume_step
)
1087 regcache_write_pc (regcache
,
1089 gdbarch_decr_pc_after_break (gdbarch
));
1092 /* Check target signal */
1093 if (record_list
->u
.end
.sigval
!= TARGET_SIGNAL_0
)
1094 /* FIXME: better way to check */
1101 if (execution_direction
== EXEC_REVERSE
)
1103 if (record_list
->prev
)
1104 record_list
= record_list
->prev
;
1108 if (record_list
->next
)
1109 record_list
= record_list
->next
;
1113 while (continue_flag
);
1115 signal (SIGINT
, handle_sigint
);
1119 status
->value
.sig
= TARGET_SIGNAL_INT
;
1120 else if (record_list
->u
.end
.sigval
!= TARGET_SIGNAL_0
)
1121 /* FIXME: better way to check */
1122 status
->value
.sig
= record_list
->u
.end
.sigval
;
1124 status
->value
.sig
= TARGET_SIGNAL_TRAP
;
1126 discard_cleanups (old_cleanups
);
1129 do_cleanups (set_cleanups
);
1130 return inferior_ptid
;
1133 /* "to_disconnect" method for process record target. */
1136 record_disconnect (struct target_ops
*target
, char *args
, int from_tty
)
1139 fprintf_unfiltered (gdb_stdlog
, "Process record: record_disconnect\n");
1141 unpush_target (&record_ops
);
1142 target_disconnect (args
, from_tty
);
1145 /* "to_detach" method for process record target. */
1148 record_detach (struct target_ops
*ops
, char *args
, int from_tty
)
1151 fprintf_unfiltered (gdb_stdlog
, "Process record: record_detach\n");
1153 unpush_target (&record_ops
);
1154 target_detach (args
, from_tty
);
1157 /* "to_mourn_inferior" method for process record target. */
1160 record_mourn_inferior (struct target_ops
*ops
)
1163 fprintf_unfiltered (gdb_stdlog
, "Process record: "
1164 "record_mourn_inferior\n");
1166 unpush_target (&record_ops
);
1167 target_mourn_inferior ();
1170 /* Close process record target before killing the inferior process. */
1173 record_kill (struct target_ops
*ops
)
1176 fprintf_unfiltered (gdb_stdlog
, "Process record: record_kill\n");
1178 unpush_target (&record_ops
);
1182 /* Record registers change (by user or by GDB) to list as an instruction. */
1185 record_registers_change (struct regcache
*regcache
, int regnum
)
1187 /* Check record_insn_num. */
1188 record_check_insn_num (0);
1190 record_arch_list_head
= NULL
;
1191 record_arch_list_tail
= NULL
;
1196 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
1198 if (record_arch_list_add_reg (regcache
, i
))
1200 record_list_release (record_arch_list_tail
);
1201 error (_("Process record: failed to record execution log."));
1207 if (record_arch_list_add_reg (regcache
, regnum
))
1209 record_list_release (record_arch_list_tail
);
1210 error (_("Process record: failed to record execution log."));
1213 if (record_arch_list_add_end ())
1215 record_list_release (record_arch_list_tail
);
1216 error (_("Process record: failed to record execution log."));
1218 record_list
->next
= record_arch_list_head
;
1219 record_arch_list_head
->prev
= record_list
;
1220 record_list
= record_arch_list_tail
;
1222 if (record_insn_num
== record_insn_max_num
&& record_insn_max_num
)
1223 record_list_release_first ();
1228 /* "to_store_registers" method for process record target. */
1231 record_store_registers (struct target_ops
*ops
, struct regcache
*regcache
,
1234 if (!record_gdb_operation_disable
)
1236 if (RECORD_IS_REPLAY
)
1240 /* Let user choose if he wants to write register or not. */
1243 query (_("Because GDB is in replay mode, changing the "
1244 "value of a register will make the execution "
1245 "log unusable from this point onward. "
1246 "Change all registers?"));
1249 query (_("Because GDB is in replay mode, changing the value "
1250 "of a register will make the execution log unusable "
1251 "from this point onward. Change register %s?"),
1252 gdbarch_register_name (get_regcache_arch (regcache
),
1257 /* Invalidate the value of regcache that was set in function
1258 "regcache_raw_write". */
1263 i
< gdbarch_num_regs (get_regcache_arch (regcache
));
1265 regcache_invalidate (regcache
, i
);
1268 regcache_invalidate (regcache
, regno
);
1270 error (_("Process record canceled the operation."));
1273 /* Destroy the record from here forward. */
1274 record_list_release_following (record_list
);
1277 record_registers_change (regcache
, regno
);
1279 record_beneath_to_store_registers (record_beneath_to_store_registers_ops
,
1283 /* Behavior is conditional on RECORD_IS_REPLAY.
1284 In replay mode, we cannot write memory unles we are willing to
1285 invalidate the record/replay log from this point forward. */
1288 record_xfer_partial (struct target_ops
*ops
, enum target_object object
,
1289 const char *annex
, gdb_byte
*readbuf
,
1290 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
1292 if (!record_gdb_operation_disable
1293 && (object
== TARGET_OBJECT_MEMORY
1294 || object
== TARGET_OBJECT_RAW_MEMORY
) && writebuf
)
1296 if (RECORD_IS_REPLAY
)
1298 /* Let user choose if he wants to write memory or not. */
1299 if (!query (_("Because GDB is in replay mode, writing to memory "
1300 "will make the execution log unusable from this "
1301 "point onward. Write memory at address %s?"),
1302 paddress (target_gdbarch
, offset
)))
1303 error (_("Process record canceled the operation."));
1305 /* Destroy the record from here forward. */
1306 record_list_release_following (record_list
);
1309 /* Check record_insn_num */
1310 record_check_insn_num (0);
1312 /* Record registers change to list as an instruction. */
1313 record_arch_list_head
= NULL
;
1314 record_arch_list_tail
= NULL
;
1315 if (record_arch_list_add_mem (offset
, len
))
1317 record_list_release (record_arch_list_tail
);
1319 fprintf_unfiltered (gdb_stdlog
,
1320 _("Process record: failed to record "
1324 if (record_arch_list_add_end ())
1326 record_list_release (record_arch_list_tail
);
1328 fprintf_unfiltered (gdb_stdlog
,
1329 _("Process record: failed to record "
1333 record_list
->next
= record_arch_list_head
;
1334 record_arch_list_head
->prev
= record_list
;
1335 record_list
= record_arch_list_tail
;
1337 if (record_insn_num
== record_insn_max_num
&& record_insn_max_num
)
1338 record_list_release_first ();
1343 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops
,
1344 object
, annex
, readbuf
, writebuf
,
1348 /* Behavior is conditional on RECORD_IS_REPLAY.
1349 We will not actually insert or remove breakpoints when replaying,
1350 nor when recording. */
1353 record_insert_breakpoint (struct gdbarch
*gdbarch
,
1354 struct bp_target_info
*bp_tgt
)
1356 if (!RECORD_IS_REPLAY
)
1358 struct cleanup
*old_cleanups
= record_gdb_operation_disable_set ();
1359 int ret
= record_beneath_to_insert_breakpoint (gdbarch
, bp_tgt
);
1361 do_cleanups (old_cleanups
);
1369 /* "to_remove_breakpoint" method for process record target. */
1372 record_remove_breakpoint (struct gdbarch
*gdbarch
,
1373 struct bp_target_info
*bp_tgt
)
1375 if (!RECORD_IS_REPLAY
)
1377 struct cleanup
*old_cleanups
= record_gdb_operation_disable_set ();
1378 int ret
= record_beneath_to_remove_breakpoint (gdbarch
, bp_tgt
);
1380 do_cleanups (old_cleanups
);
1388 /* "to_can_execute_reverse" method for process record target. */
1390 record_can_execute_reverse (void)
1396 init_record_ops (void)
1398 record_ops
.to_shortname
= "record";
1399 record_ops
.to_longname
= "Process record and replay target";
1401 "Log program while executing and replay execution from log.";
1402 record_ops
.to_open
= record_open
;
1403 record_ops
.to_close
= record_close
;
1404 record_ops
.to_resume
= record_resume
;
1405 record_ops
.to_wait
= record_wait
;
1406 record_ops
.to_disconnect
= record_disconnect
;
1407 record_ops
.to_detach
= record_detach
;
1408 record_ops
.to_mourn_inferior
= record_mourn_inferior
;
1409 record_ops
.to_kill
= record_kill
;
1410 record_ops
.to_create_inferior
= find_default_create_inferior
;
1411 record_ops
.to_store_registers
= record_store_registers
;
1412 record_ops
.to_xfer_partial
= record_xfer_partial
;
1413 record_ops
.to_insert_breakpoint
= record_insert_breakpoint
;
1414 record_ops
.to_remove_breakpoint
= record_remove_breakpoint
;
1415 record_ops
.to_can_execute_reverse
= record_can_execute_reverse
;
1416 record_ops
.to_stratum
= record_stratum
;
1417 record_ops
.to_magic
= OPS_MAGIC
;
1420 /* Implement "show record debug" command. */
1423 show_record_debug (struct ui_file
*file
, int from_tty
,
1424 struct cmd_list_element
*c
, const char *value
)
1426 fprintf_filtered (file
, _("Debugging of process record target is %s.\n"),
1430 /* Alias for "target record". */
1433 cmd_record_start (char *args
, int from_tty
)
1435 execute_command ("target record", from_tty
);
1438 /* Truncate the record log from the present point
1439 of replay until the end. */
1442 cmd_record_delete (char *args
, int from_tty
)
1444 if (current_target
.to_stratum
== record_stratum
)
1446 if (RECORD_IS_REPLAY
)
1448 if (!from_tty
|| query (_("Delete the log from this point forward "
1449 "and begin to record the running message "
1451 record_list_release_following (record_list
);
1454 printf_unfiltered (_("Already at end of record list.\n"));
1458 printf_unfiltered (_("Process record is not started.\n"));
1461 /* Implement the "stoprecord" or "record stop" command. */
1464 cmd_record_stop (char *args
, int from_tty
)
1466 if (current_target
.to_stratum
== record_stratum
)
1468 unpush_target (&record_ops
);
1469 printf_unfiltered (_("Process record is stopped and all execution "
1470 "logs are deleted.\n"));
1473 printf_unfiltered (_("Process record is not started.\n"));
1476 /* Set upper limit of record log size. */
1479 set_record_insn_max_num (char *args
, int from_tty
, struct cmd_list_element
*c
)
1481 if (record_insn_num
> record_insn_max_num
&& record_insn_max_num
)
1483 /* Count down record_insn_num while releasing records from list. */
1484 while (record_insn_num
> record_insn_max_num
)
1486 record_list_release_first ();
1492 static struct cmd_list_element
*record_cmdlist
, *set_record_cmdlist
,
1493 *show_record_cmdlist
, *info_record_cmdlist
;
1496 set_record_command (char *args
, int from_tty
)
1498 printf_unfiltered (_("\
1499 \"set record\" must be followed by an apporpriate subcommand.\n"));
1500 help_list (set_record_cmdlist
, "set record ", all_commands
, gdb_stdout
);
1504 show_record_command (char *args
, int from_tty
)
1506 cmd_show_list (show_record_cmdlist
, from_tty
, "");
1509 /* Display some statistics about the execution log. */
1512 info_record_command (char *args
, int from_tty
)
1514 struct record_entry
*p
;
1516 if (current_target
.to_stratum
== record_stratum
)
1518 if (RECORD_IS_REPLAY
)
1519 printf_filtered (_("Replay mode:\n"));
1521 printf_filtered (_("Record mode:\n"));
1523 /* Find entry for first actual instruction in the log. */
1524 for (p
= record_first
.next
;
1525 p
!= NULL
&& p
->type
!= record_end
;
1529 /* Do we have a log at all? */
1530 if (p
!= NULL
&& p
->type
== record_end
)
1532 /* Display instruction number for first instruction in the log. */
1533 printf_filtered (_("Lowest recorded instruction number is %s.\n"),
1534 pulongest (p
->u
.end
.insn_num
));
1536 /* If in replay mode, display where we are in the log. */
1537 if (RECORD_IS_REPLAY
)
1538 printf_filtered (_("Current instruction number is %s.\n"),
1539 pulongest (record_list
->u
.end
.insn_num
));
1541 /* Display instruction number for last instruction in the log. */
1542 printf_filtered (_("Highest recorded instruction number is %s.\n"),
1543 pulongest (record_insn_count
));
1545 /* Display log count. */
1546 printf_filtered (_("Log contains %d instructions.\n"),
1551 printf_filtered (_("No instructions have been logged.\n"));
1556 printf_filtered (_("target record is not active.\n"));
1559 /* Display max log size. */
1560 printf_filtered (_("Max logged instructions is %d.\n"),
1561 record_insn_max_num
);
1565 _initialize_record (void)
1567 /* Init record_first. */
1568 record_first
.prev
= NULL
;
1569 record_first
.next
= NULL
;
1570 record_first
.type
= record_end
;
1573 add_target (&record_ops
);
1575 add_setshow_zinteger_cmd ("record", no_class
, &record_debug
,
1576 _("Set debugging of record/replay feature."),
1577 _("Show debugging of record/replay feature."),
1578 _("When enabled, debugging output for "
1579 "record/replay feature is displayed."),
1580 NULL
, show_record_debug
, &setdebuglist
,
1583 add_prefix_cmd ("record", class_obscure
, cmd_record_start
,
1584 _("Abbreviated form of \"target record\" command."),
1585 &record_cmdlist
, "record ", 0, &cmdlist
);
1586 add_com_alias ("rec", "record", class_obscure
, 1);
1587 add_prefix_cmd ("record", class_support
, set_record_command
,
1588 _("Set record options"), &set_record_cmdlist
,
1589 "set record ", 0, &setlist
);
1590 add_alias_cmd ("rec", "record", class_obscure
, 1, &setlist
);
1591 add_prefix_cmd ("record", class_support
, show_record_command
,
1592 _("Show record options"), &show_record_cmdlist
,
1593 "show record ", 0, &showlist
);
1594 add_alias_cmd ("rec", "record", class_obscure
, 1, &showlist
);
1595 add_prefix_cmd ("record", class_support
, info_record_command
,
1596 _("Info record options"), &info_record_cmdlist
,
1597 "info record ", 0, &infolist
);
1598 add_alias_cmd ("rec", "record", class_obscure
, 1, &infolist
);
1601 add_cmd ("delete", class_obscure
, cmd_record_delete
,
1602 _("Delete the rest of execution log and start recording it anew."),
1604 add_alias_cmd ("d", "delete", class_obscure
, 1, &record_cmdlist
);
1605 add_alias_cmd ("del", "delete", class_obscure
, 1, &record_cmdlist
);
1607 add_cmd ("stop", class_obscure
, cmd_record_stop
,
1608 _("Stop the record/replay target."),
1610 add_alias_cmd ("s", "stop", class_obscure
, 1, &record_cmdlist
);
1612 /* Record instructions number limit command. */
1613 add_setshow_boolean_cmd ("stop-at-limit", no_class
,
1614 &record_stop_at_limit
, _("\
1615 Set whether record/replay stops when record/replay buffer becomes full."), _("\
1616 Show whether record/replay stops when record/replay buffer becomes full."), _("\
1618 When ON, if the record/replay buffer becomes full, ask user what to do.\n\
1619 When OFF, if the record/replay buffer becomes full,\n\
1620 delete the oldest recorded instruction to make room for each new one."),
1622 &set_record_cmdlist
, &show_record_cmdlist
);
1623 add_setshow_uinteger_cmd ("insn-number-max", no_class
,
1624 &record_insn_max_num
,
1625 _("Set record/replay buffer limit."),
1626 _("Show record/replay buffer limit."), _("\
1627 Set the maximum number of instructions to be stored in the\n\
1628 record/replay buffer. Zero means unlimited. Default is 200000."),
1629 set_record_insn_max_num
,
1630 NULL
, &set_record_cmdlist
, &show_record_cmdlist
);