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 #define DEFAULT_RECORD_INSN_MAX_NUM 200000
32 #define RECORD_IS_REPLAY \
33 (record_list->next || execution_direction == EXEC_REVERSE)
35 /* These are the core structs of the process record functionality.
37 A record_entry is a record of the value change of a register
38 ("record_reg") or a part of memory ("record_mem"). And each
39 instruction must have a struct record_entry ("record_end") that
40 indicates that this is the last struct record_entry of this
43 Each struct record_entry is linked to "record_list" by "prev" and
46 struct record_mem_entry
50 /* Set this flag if target memory for this entry
51 can no longer be accessed. */
52 int mem_entry_not_accessible
;
56 gdb_byte buf
[sizeof (gdb_byte
*)];
60 struct record_reg_entry
67 gdb_byte buf
[2 * sizeof (gdb_byte
*)];
71 struct record_end_entry
73 enum target_signal sigval
;
85 struct record_entry
*prev
;
86 struct record_entry
*next
;
87 enum record_type type
;
91 struct record_reg_entry reg
;
93 struct record_mem_entry mem
;
95 struct record_end_entry end
;
99 /* This is the debug switch for process record. */
100 int record_debug
= 0;
102 /* These list is for execution log. */
103 static struct record_entry record_first
;
104 static struct record_entry
*record_list
= &record_first
;
105 static struct record_entry
*record_arch_list_head
= NULL
;
106 static struct record_entry
*record_arch_list_tail
= NULL
;
108 /* 1 ask user. 0 auto delete the last struct record_entry. */
109 static int record_stop_at_limit
= 1;
110 static unsigned int record_insn_max_num
= DEFAULT_RECORD_INSN_MAX_NUM
;
111 static int record_insn_num
= 0;
113 /* The target_ops of process record. */
114 static struct target_ops record_ops
;
116 /* The beneath function pointers. */
117 static struct target_ops
*record_beneath_to_resume_ops
;
118 static void (*record_beneath_to_resume
) (struct target_ops
*, ptid_t
, int,
120 static struct target_ops
*record_beneath_to_wait_ops
;
121 static ptid_t (*record_beneath_to_wait
) (struct target_ops
*, ptid_t
,
122 struct target_waitstatus
*,
124 static struct target_ops
*record_beneath_to_store_registers_ops
;
125 static void (*record_beneath_to_store_registers
) (struct target_ops
*,
128 static struct target_ops
*record_beneath_to_xfer_partial_ops
;
129 static LONGEST (*record_beneath_to_xfer_partial
) (struct target_ops
*ops
,
130 enum target_object object
,
133 const gdb_byte
*writebuf
,
136 static int (*record_beneath_to_insert_breakpoint
) (struct gdbarch
*,
137 struct bp_target_info
*);
138 static int (*record_beneath_to_remove_breakpoint
) (struct gdbarch
*,
139 struct bp_target_info
*);
141 /* Alloc and free functions for record_reg, record_mem, and record_end
144 /* Alloc a record_reg record entry. */
146 static inline struct record_entry
*
147 record_reg_alloc (struct regcache
*regcache
, int regnum
)
149 struct record_entry
*rec
;
150 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
152 rec
= (struct record_entry
*) xcalloc (1, sizeof (struct record_entry
));
153 rec
->type
= record_reg
;
154 rec
->u
.reg
.num
= regnum
;
155 rec
->u
.reg
.len
= register_size (gdbarch
, regnum
);
156 if (rec
->u
.reg
.len
> sizeof (rec
->u
.reg
.u
.buf
))
157 rec
->u
.reg
.u
.ptr
= (gdb_byte
*) xmalloc (rec
->u
.reg
.len
);
162 /* Free a record_reg record entry. */
165 record_reg_release (struct record_entry
*rec
)
167 gdb_assert (rec
->type
== record_reg
);
168 if (rec
->u
.reg
.len
> sizeof (rec
->u
.reg
.u
.buf
))
169 xfree (rec
->u
.reg
.u
.ptr
);
173 /* Alloc a record_mem record entry. */
175 static inline struct record_entry
*
176 record_mem_alloc (CORE_ADDR addr
, int len
)
178 struct record_entry
*rec
;
180 rec
= (struct record_entry
*) xcalloc (1, sizeof (struct record_entry
));
181 rec
->type
= record_mem
;
182 rec
->u
.mem
.addr
= addr
;
183 rec
->u
.mem
.len
= len
;
184 if (rec
->u
.mem
.len
> sizeof (rec
->u
.mem
.u
.buf
))
185 rec
->u
.mem
.u
.ptr
= (gdb_byte
*) xmalloc (len
);
190 /* Free a record_mem record entry. */
193 record_mem_release (struct record_entry
*rec
)
195 gdb_assert (rec
->type
== record_mem
);
196 if (rec
->u
.mem
.len
> sizeof (rec
->u
.mem
.u
.buf
))
197 xfree (rec
->u
.mem
.u
.ptr
);
201 /* Alloc a record_end record entry. */
203 static inline struct record_entry
*
204 record_end_alloc (void)
206 struct record_entry
*rec
;
208 rec
= (struct record_entry
*) xcalloc (1, sizeof (struct record_entry
));
209 rec
->type
= record_end
;
214 /* Free a record_end record entry. */
217 record_end_release (struct record_entry
*rec
)
222 /* Free one record entry, any type.
223 Return entry->type, in case caller wants to know. */
225 static inline enum record_type
226 record_entry_release (struct record_entry
*rec
)
228 enum record_type type
= rec
->type
;
232 record_reg_release (rec
);
235 record_mem_release (rec
);
238 record_end_release (rec
);
244 /* Free all record entries in list pointed to by REC. */
247 record_list_release (struct record_entry
*rec
)
258 record_entry_release (rec
->next
);
261 if (rec
== &record_first
)
264 record_first
.next
= NULL
;
267 record_entry_release (rec
);
270 /* Free all record entries forward of the given list position. */
273 record_list_release_following (struct record_entry
*rec
)
275 struct record_entry
*tmp
= rec
->next
;
281 if (record_entry_release (tmp
) == record_end
)
287 /* Delete the first instruction from the beginning of the log, to make
288 room for adding a new instruction at the end of the log.
290 Note -- this function does not modify record_insn_num. */
293 record_list_release_first (void)
295 struct record_entry
*tmp
;
297 if (!record_first
.next
)
300 /* Loop until a record_end. */
303 /* Cut record_first.next out of the linked list. */
304 tmp
= record_first
.next
;
305 record_first
.next
= tmp
->next
;
306 tmp
->next
->prev
= &record_first
;
308 /* tmp is now isolated, and can be deleted. */
309 if (record_entry_release (tmp
) == record_end
)
312 break; /* End loop at first record_end. */
315 if (!record_first
.next
)
317 gdb_assert (record_insn_num
== 1);
318 break; /* End loop when list is empty. */
323 /* Add a struct record_entry to record_arch_list. */
326 record_arch_list_add (struct record_entry
*rec
)
328 if (record_debug
> 1)
329 fprintf_unfiltered (gdb_stdlog
,
330 "Process record: record_arch_list_add %s.\n",
331 host_address_to_string (rec
));
333 if (record_arch_list_tail
)
335 record_arch_list_tail
->next
= rec
;
336 rec
->prev
= record_arch_list_tail
;
337 record_arch_list_tail
= rec
;
341 record_arch_list_head
= rec
;
342 record_arch_list_tail
= rec
;
346 /* Return the value storage location of a record entry. */
347 static inline gdb_byte
*
348 record_get_loc (struct record_entry
*rec
)
352 if (rec
->u
.mem
.len
> sizeof (rec
->u
.mem
.u
.buf
))
353 return rec
->u
.mem
.u
.ptr
;
355 return rec
->u
.mem
.u
.buf
;
357 if (rec
->u
.reg
.len
> sizeof (rec
->u
.reg
.u
.buf
))
358 return rec
->u
.reg
.u
.ptr
;
360 return rec
->u
.reg
.u
.buf
;
368 /* Record the value of a register NUM to record_arch_list. */
371 record_arch_list_add_reg (struct regcache
*regcache
, int regnum
)
373 struct record_entry
*rec
;
375 if (record_debug
> 1)
376 fprintf_unfiltered (gdb_stdlog
,
377 "Process record: add register num = %d to "
381 rec
= record_reg_alloc (regcache
, regnum
);
383 regcache_raw_read (regcache
, regnum
, record_get_loc (rec
));
385 record_arch_list_add (rec
);
390 /* Record the value of a region of memory whose address is ADDR and
391 length is LEN to record_arch_list. */
394 record_arch_list_add_mem (CORE_ADDR addr
, int len
)
396 struct record_entry
*rec
;
398 if (record_debug
> 1)
399 fprintf_unfiltered (gdb_stdlog
,
400 "Process record: add mem addr = %s len = %d to "
402 paddress (target_gdbarch
, addr
), len
);
404 if (!addr
) /* FIXME: Why? Some arch must permit it... */
407 rec
= record_mem_alloc (addr
, len
);
409 if (target_read_memory (addr
, record_get_loc (rec
), len
))
412 fprintf_unfiltered (gdb_stdlog
,
413 "Process record: error reading memory at "
414 "addr = %s len = %d.\n",
415 paddress (target_gdbarch
, addr
), len
);
416 record_mem_release (rec
);
420 record_arch_list_add (rec
);
425 /* Add a record_end type struct record_entry to record_arch_list. */
428 record_arch_list_add_end (void)
430 struct record_entry
*rec
;
432 if (record_debug
> 1)
433 fprintf_unfiltered (gdb_stdlog
,
434 "Process record: add end to arch list.\n");
436 rec
= record_end_alloc ();
437 rec
->u
.end
.sigval
= TARGET_SIGNAL_0
;
439 record_arch_list_add (rec
);
445 record_check_insn_num (int set_terminal
)
447 if (record_insn_max_num
)
449 gdb_assert (record_insn_num
<= record_insn_max_num
);
450 if (record_insn_num
== record_insn_max_num
)
452 /* Ask user what to do. */
453 if (record_stop_at_limit
)
457 target_terminal_ours ();
458 q
= yquery (_("Do you want to auto delete previous execution "
459 "log entries when record/replay buffer becomes "
460 "full (record stop-at-limit)?"));
462 target_terminal_inferior ();
464 record_stop_at_limit
= 0;
466 error (_("Process record: inferior program stopped."));
472 /* Before inferior step (when GDB record the running message, inferior
473 only can step), GDB will call this function to record the values to
474 record_list. This function will call gdbarch_process_record to
475 record the running message of inferior and set them to
476 record_arch_list, and add it to record_list. */
479 record_message_cleanups (void *ignore
)
481 record_list_release (record_arch_list_tail
);
484 struct record_message_args
{
485 struct regcache
*regcache
;
486 enum target_signal signal
;
490 record_message (void *args
)
493 struct record_message_args
*myargs
= args
;
494 struct gdbarch
*gdbarch
= get_regcache_arch (myargs
->regcache
);
495 struct cleanup
*old_cleanups
= make_cleanup (record_message_cleanups
, 0);
497 record_arch_list_head
= NULL
;
498 record_arch_list_tail
= NULL
;
500 /* Check record_insn_num. */
501 record_check_insn_num (1);
503 /* If gdb sends a signal value to target_resume,
504 save it in the 'end' field of the previous instruction.
506 Maybe process record should record what really happened,
507 rather than what gdb pretends has happened.
509 So if Linux delivered the signal to the child process during
510 the record mode, we will record it and deliver it again in
513 If user says "ignore this signal" during the record mode, then
514 it will be ignored again during the replay mode (no matter if
515 the user says something different, like "deliver this signal"
516 during the replay mode).
518 User should understand that nothing he does during the replay
519 mode will change the behavior of the child. If he tries,
520 then that is a user error.
522 But we should still deliver the signal to gdb during the replay,
523 if we delivered it during the recording. Therefore we should
524 record the signal during record_wait, not record_resume. */
525 if (record_list
!= &record_first
) /* FIXME better way to check */
527 gdb_assert (record_list
->type
== record_end
);
528 record_list
->u
.end
.sigval
= myargs
->signal
;
531 if (myargs
->signal
== TARGET_SIGNAL_0
532 || !gdbarch_process_record_signal_p (gdbarch
))
533 ret
= gdbarch_process_record (gdbarch
,
535 regcache_read_pc (myargs
->regcache
));
537 ret
= gdbarch_process_record_signal (gdbarch
,
542 error (_("Process record: inferior program stopped."));
544 error (_("Process record: failed to record execution log."));
546 discard_cleanups (old_cleanups
);
548 record_list
->next
= record_arch_list_head
;
549 record_arch_list_head
->prev
= record_list
;
550 record_list
= record_arch_list_tail
;
552 if (record_insn_num
== record_insn_max_num
&& record_insn_max_num
)
553 record_list_release_first ();
561 do_record_message (struct regcache
*regcache
,
562 enum target_signal signal
)
564 struct record_message_args args
;
566 args
.regcache
= regcache
;
567 args
.signal
= signal
;
568 return catch_errors (record_message
, &args
, NULL
, RETURN_MASK_ALL
);
571 /* Set to 1 if record_store_registers and record_xfer_partial
572 doesn't need record. */
574 static int record_gdb_operation_disable
= 0;
577 record_gdb_operation_disable_set (void)
579 struct cleanup
*old_cleanups
= NULL
;
582 make_cleanup_restore_integer (&record_gdb_operation_disable
);
583 record_gdb_operation_disable
= 1;
589 record_open (char *name
, int from_tty
)
591 struct target_ops
*t
;
594 fprintf_unfiltered (gdb_stdlog
, "Process record: record_open\n");
597 if (!target_has_execution
)
598 error (_("Process record: the program is not being run."));
600 error (_("Process record target can't debug inferior in non-stop mode "
602 if (target_async_permitted
)
603 error (_("Process record target can't debug inferior in asynchronous "
604 "mode (target-async)."));
606 if (!gdbarch_process_record_p (target_gdbarch
))
607 error (_("Process record: the current architecture doesn't support "
608 "record function."));
610 /* Check if record target is already running. */
611 if (current_target
.to_stratum
== record_stratum
)
612 error (_("Process record target already running. Use \"record stop\" to "
613 "stop record target first."));
615 /*Reset the beneath function pointers. */
616 record_beneath_to_resume
= NULL
;
617 record_beneath_to_wait
= NULL
;
618 record_beneath_to_store_registers
= NULL
;
619 record_beneath_to_xfer_partial
= NULL
;
620 record_beneath_to_insert_breakpoint
= NULL
;
621 record_beneath_to_remove_breakpoint
= NULL
;
623 /* Set the beneath function pointers. */
624 for (t
= current_target
.beneath
; t
!= NULL
; t
= t
->beneath
)
626 if (!record_beneath_to_resume
)
628 record_beneath_to_resume
= t
->to_resume
;
629 record_beneath_to_resume_ops
= t
;
631 if (!record_beneath_to_wait
)
633 record_beneath_to_wait
= t
->to_wait
;
634 record_beneath_to_wait_ops
= t
;
636 if (!record_beneath_to_store_registers
)
638 record_beneath_to_store_registers
= t
->to_store_registers
;
639 record_beneath_to_store_registers_ops
= t
;
641 if (!record_beneath_to_xfer_partial
)
643 record_beneath_to_xfer_partial
= t
->to_xfer_partial
;
644 record_beneath_to_xfer_partial_ops
= t
;
646 if (!record_beneath_to_insert_breakpoint
)
647 record_beneath_to_insert_breakpoint
= t
->to_insert_breakpoint
;
648 if (!record_beneath_to_remove_breakpoint
)
649 record_beneath_to_remove_breakpoint
= t
->to_remove_breakpoint
;
651 if (!record_beneath_to_resume
)
652 error (_("Process record can't get to_resume."));
653 if (!record_beneath_to_wait
)
654 error (_("Process record can't get to_wait."));
655 if (!record_beneath_to_store_registers
)
656 error (_("Process record can't get to_store_registers."));
657 if (!record_beneath_to_xfer_partial
)
658 error (_("Process record can't get to_xfer_partial."));
659 if (!record_beneath_to_insert_breakpoint
)
660 error (_("Process record can't get to_insert_breakpoint."));
661 if (!record_beneath_to_remove_breakpoint
)
662 error (_("Process record can't get to_remove_breakpoint."));
664 push_target (&record_ops
);
668 record_list
= &record_first
;
669 record_list
->next
= NULL
;
673 record_close (int quitting
)
676 fprintf_unfiltered (gdb_stdlog
, "Process record: record_close\n");
678 record_list_release (record_list
);
681 static int record_resume_step
= 0;
682 static int record_resume_error
;
685 record_resume (struct target_ops
*ops
, ptid_t ptid
, int step
,
686 enum target_signal signal
)
688 record_resume_step
= step
;
690 if (!RECORD_IS_REPLAY
)
692 if (do_record_message (get_current_regcache (), signal
))
694 record_resume_error
= 0;
698 record_resume_error
= 1;
701 record_beneath_to_resume (record_beneath_to_resume_ops
, ptid
, 1,
706 static int record_get_sig
= 0;
709 record_sig_handler (int signo
)
712 fprintf_unfiltered (gdb_stdlog
, "Process record: get a signal\n");
714 /* It will break the running inferior in replay mode. */
715 record_resume_step
= 1;
717 /* It will let record_wait set inferior status to get the signal
723 record_wait_cleanups (void *ignore
)
725 if (execution_direction
== EXEC_REVERSE
)
727 if (record_list
->next
)
728 record_list
= record_list
->next
;
731 record_list
= record_list
->prev
;
734 /* In replay mode, this function examines the recorded log and
735 determines where to stop. */
738 record_wait (struct target_ops
*ops
,
739 ptid_t ptid
, struct target_waitstatus
*status
,
742 struct cleanup
*set_cleanups
= record_gdb_operation_disable_set ();
745 fprintf_unfiltered (gdb_stdlog
,
746 "Process record: record_wait "
747 "record_resume_step = %d\n",
750 if (!RECORD_IS_REPLAY
)
752 if (record_resume_error
)
754 /* If record_resume get error, return directly. */
755 status
->kind
= TARGET_WAITKIND_STOPPED
;
756 status
->value
.sig
= TARGET_SIGNAL_ABRT
;
757 return inferior_ptid
;
760 if (record_resume_step
)
762 /* This is a single step. */
763 return record_beneath_to_wait (record_beneath_to_wait_ops
,
764 ptid
, status
, options
);
768 /* This is not a single step. */
774 ret
= record_beneath_to_wait (record_beneath_to_wait_ops
,
775 ptid
, status
, options
);
777 /* Is this a SIGTRAP? */
778 if (status
->kind
== TARGET_WAITKIND_STOPPED
779 && status
->value
.sig
== TARGET_SIGNAL_TRAP
)
781 struct regcache
*regcache
;
783 /* Yes -- check if there is a breakpoint. */
784 registers_changed ();
785 regcache
= get_current_regcache ();
786 tmp_pc
= regcache_read_pc (regcache
);
787 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache
),
790 /* There is a breakpoint. GDB will want to stop. */
791 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
792 CORE_ADDR decr_pc_after_break
793 = gdbarch_decr_pc_after_break (gdbarch
);
794 if (decr_pc_after_break
)
795 regcache_write_pc (regcache
,
796 tmp_pc
+ decr_pc_after_break
);
800 /* There is not a breakpoint, and gdb is not
801 stepping, therefore gdb will not stop.
802 Therefore we will not return to gdb.
803 Record the insn and resume. */
804 if (!do_record_message (regcache
, TARGET_SIGNAL_0
))
807 record_beneath_to_resume (record_beneath_to_resume_ops
,
814 /* The inferior is broken by a breakpoint or a signal. */
823 struct regcache
*regcache
= get_current_regcache ();
824 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
825 int continue_flag
= 1;
826 int first_record_end
= 1;
827 struct cleanup
*old_cleanups
= make_cleanup (record_wait_cleanups
, 0);
830 status
->kind
= TARGET_WAITKIND_STOPPED
;
832 /* Check breakpoint when forward execute. */
833 if (execution_direction
== EXEC_FORWARD
)
835 tmp_pc
= regcache_read_pc (regcache
);
836 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache
),
840 fprintf_unfiltered (gdb_stdlog
,
841 "Process record: break at %s.\n",
842 paddress (gdbarch
, tmp_pc
));
843 if (gdbarch_decr_pc_after_break (gdbarch
)
844 && !record_resume_step
)
845 regcache_write_pc (regcache
,
847 gdbarch_decr_pc_after_break (gdbarch
));
853 signal (SIGINT
, record_sig_handler
);
854 /* If GDB is in terminal_inferior mode, it will not get the signal.
855 And in GDB replay mode, GDB doesn't need to be in terminal_inferior
856 mode, because inferior will not executed.
857 Then set it to terminal_ours to make GDB get the signal. */
858 target_terminal_ours ();
860 /* In EXEC_FORWARD mode, record_list points to the tail of prev
862 if (execution_direction
== EXEC_FORWARD
&& record_list
->next
)
863 record_list
= record_list
->next
;
865 /* Loop over the record_list, looking for the next place to
869 /* Check for beginning and end of log. */
870 if (execution_direction
== EXEC_REVERSE
871 && record_list
== &record_first
)
873 /* Hit beginning of record log in reverse. */
874 status
->kind
= TARGET_WAITKIND_NO_HISTORY
;
877 if (execution_direction
!= EXEC_REVERSE
&& !record_list
->next
)
879 /* Hit end of record log going forward. */
880 status
->kind
= TARGET_WAITKIND_NO_HISTORY
;
884 /* Set ptid, register and memory according to record_list. */
885 if (record_list
->type
== record_reg
)
888 gdb_byte reg
[MAX_REGISTER_SIZE
];
889 if (record_debug
> 1)
890 fprintf_unfiltered (gdb_stdlog
,
891 "Process record: record_reg %s to "
892 "inferior num = %d.\n",
893 host_address_to_string (record_list
),
894 record_list
->u
.reg
.num
);
895 regcache_cooked_read (regcache
, record_list
->u
.reg
.num
, reg
);
896 regcache_cooked_write (regcache
, record_list
->u
.reg
.num
,
897 record_get_loc (record_list
));
898 memcpy (record_get_loc (record_list
), reg
,
899 record_list
->u
.reg
.len
);
901 else if (record_list
->type
== record_mem
)
904 /* Nothing to do if the entry is flagged not_accessible. */
905 if (!record_list
->u
.mem
.mem_entry_not_accessible
)
907 gdb_byte
*mem
= alloca (record_list
->u
.mem
.len
);
908 if (record_debug
> 1)
909 fprintf_unfiltered (gdb_stdlog
,
910 "Process record: record_mem %s to "
911 "inferior addr = %s len = %d.\n",
912 host_address_to_string (record_list
),
914 record_list
->u
.mem
.addr
),
915 record_list
->u
.mem
.len
);
917 if (target_read_memory (record_list
->u
.mem
.addr
, mem
,
918 record_list
->u
.mem
.len
))
920 if (execution_direction
!= EXEC_REVERSE
)
921 error (_("Process record: error reading memory at "
922 "addr = %s len = %d."),
923 paddress (gdbarch
, record_list
->u
.mem
.addr
),
924 record_list
->u
.mem
.len
);
927 flag entry as not_accessible. */
928 record_list
->u
.mem
.mem_entry_not_accessible
= 1;
932 if (target_write_memory (record_list
->u
.mem
.addr
,
933 record_get_loc (record_list
),
934 record_list
->u
.mem
.len
))
936 if (execution_direction
!= EXEC_REVERSE
)
937 error (_("Process record: error writing memory at "
938 "addr = %s len = %d."),
939 paddress (gdbarch
, record_list
->u
.mem
.addr
),
940 record_list
->u
.mem
.len
);
943 flag entry as not_accessible. */
944 record_list
->u
.mem
.mem_entry_not_accessible
= 1;
948 memcpy (record_get_loc (record_list
), mem
,
949 record_list
->u
.mem
.len
);
956 if (record_debug
> 1)
957 fprintf_unfiltered (gdb_stdlog
,
958 "Process record: record_end %s to "
960 host_address_to_string (record_list
));
962 if (first_record_end
&& execution_direction
== EXEC_REVERSE
)
964 /* When reverse excute, the first record_end is the part of
965 current instruction. */
966 first_record_end
= 0;
970 /* In EXEC_REVERSE mode, this is the record_end of prev
972 In EXEC_FORWARD mode, this is the record_end of current
975 if (record_resume_step
)
977 if (record_debug
> 1)
978 fprintf_unfiltered (gdb_stdlog
,
979 "Process record: step.\n");
983 /* check breakpoint */
984 tmp_pc
= regcache_read_pc (regcache
);
985 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache
),
989 fprintf_unfiltered (gdb_stdlog
,
990 "Process record: break "
992 paddress (gdbarch
, tmp_pc
));
993 if (gdbarch_decr_pc_after_break (gdbarch
)
994 && execution_direction
== EXEC_FORWARD
995 && !record_resume_step
)
996 regcache_write_pc (regcache
,
998 gdbarch_decr_pc_after_break (gdbarch
));
1001 /* Check target signal */
1002 if (record_list
->u
.end
.sigval
!= TARGET_SIGNAL_0
)
1003 /* FIXME: better way to check */
1010 if (execution_direction
== EXEC_REVERSE
)
1012 if (record_list
->prev
)
1013 record_list
= record_list
->prev
;
1017 if (record_list
->next
)
1018 record_list
= record_list
->next
;
1022 while (continue_flag
);
1024 signal (SIGINT
, handle_sigint
);
1028 status
->value
.sig
= TARGET_SIGNAL_INT
;
1029 else if (record_list
->u
.end
.sigval
!= TARGET_SIGNAL_0
)
1030 /* FIXME: better way to check */
1031 status
->value
.sig
= record_list
->u
.end
.sigval
;
1033 status
->value
.sig
= TARGET_SIGNAL_TRAP
;
1035 discard_cleanups (old_cleanups
);
1038 do_cleanups (set_cleanups
);
1039 return inferior_ptid
;
1043 record_disconnect (struct target_ops
*target
, char *args
, int from_tty
)
1046 fprintf_unfiltered (gdb_stdlog
, "Process record: record_disconnect\n");
1048 unpush_target (&record_ops
);
1049 target_disconnect (args
, from_tty
);
1053 record_detach (struct target_ops
*ops
, char *args
, int from_tty
)
1056 fprintf_unfiltered (gdb_stdlog
, "Process record: record_detach\n");
1058 unpush_target (&record_ops
);
1059 target_detach (args
, from_tty
);
1063 record_mourn_inferior (struct target_ops
*ops
)
1066 fprintf_unfiltered (gdb_stdlog
, "Process record: "
1067 "record_mourn_inferior\n");
1069 unpush_target (&record_ops
);
1070 target_mourn_inferior ();
1073 /* Close process record target before killing the inferior process. */
1076 record_kill (struct target_ops
*ops
)
1079 fprintf_unfiltered (gdb_stdlog
, "Process record: record_kill\n");
1081 unpush_target (&record_ops
);
1085 /* Record registers change (by user or by GDB) to list as an instruction. */
1088 record_registers_change (struct regcache
*regcache
, int regnum
)
1090 /* Check record_insn_num. */
1091 record_check_insn_num (0);
1093 record_arch_list_head
= NULL
;
1094 record_arch_list_tail
= NULL
;
1099 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
1101 if (record_arch_list_add_reg (regcache
, i
))
1103 record_list_release (record_arch_list_tail
);
1104 error (_("Process record: failed to record execution log."));
1110 if (record_arch_list_add_reg (regcache
, regnum
))
1112 record_list_release (record_arch_list_tail
);
1113 error (_("Process record: failed to record execution log."));
1116 if (record_arch_list_add_end ())
1118 record_list_release (record_arch_list_tail
);
1119 error (_("Process record: failed to record execution log."));
1121 record_list
->next
= record_arch_list_head
;
1122 record_arch_list_head
->prev
= record_list
;
1123 record_list
= record_arch_list_tail
;
1125 if (record_insn_num
== record_insn_max_num
&& record_insn_max_num
)
1126 record_list_release_first ();
1132 record_store_registers (struct target_ops
*ops
, struct regcache
*regcache
,
1135 if (!record_gdb_operation_disable
)
1137 if (RECORD_IS_REPLAY
)
1141 /* Let user choose if he wants to write register or not. */
1144 query (_("Because GDB is in replay mode, changing the "
1145 "value of a register will make the execution "
1146 "log unusable from this point onward. "
1147 "Change all registers?"));
1150 query (_("Because GDB is in replay mode, changing the value "
1151 "of a register will make the execution log unusable "
1152 "from this point onward. Change register %s?"),
1153 gdbarch_register_name (get_regcache_arch (regcache
),
1158 /* Invalidate the value of regcache that was set in function
1159 "regcache_raw_write". */
1164 i
< gdbarch_num_regs (get_regcache_arch (regcache
));
1166 regcache_invalidate (regcache
, i
);
1169 regcache_invalidate (regcache
, regno
);
1171 error (_("Process record canceled the operation."));
1174 /* Destroy the record from here forward. */
1175 record_list_release_following (record_list
);
1178 record_registers_change (regcache
, regno
);
1180 record_beneath_to_store_registers (record_beneath_to_store_registers_ops
,
1184 /* Behavior is conditional on RECORD_IS_REPLAY.
1185 In replay mode, we cannot write memory unles we are willing to
1186 invalidate the record/replay log from this point forward. */
1189 record_xfer_partial (struct target_ops
*ops
, enum target_object object
,
1190 const char *annex
, gdb_byte
*readbuf
,
1191 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
1193 if (!record_gdb_operation_disable
1194 && (object
== TARGET_OBJECT_MEMORY
1195 || object
== TARGET_OBJECT_RAW_MEMORY
) && writebuf
)
1197 if (RECORD_IS_REPLAY
)
1199 /* Let user choose if he wants to write memory or not. */
1200 if (!query (_("Because GDB is in replay mode, writing to memory "
1201 "will make the execution log unusable from this "
1202 "point onward. Write memory at address %s?"),
1203 paddress (target_gdbarch
, offset
)))
1204 error (_("Process record canceled the operation."));
1206 /* Destroy the record from here forward. */
1207 record_list_release_following (record_list
);
1210 /* Check record_insn_num */
1211 record_check_insn_num (0);
1213 /* Record registers change to list as an instruction. */
1214 record_arch_list_head
= NULL
;
1215 record_arch_list_tail
= NULL
;
1216 if (record_arch_list_add_mem (offset
, len
))
1218 record_list_release (record_arch_list_tail
);
1220 fprintf_unfiltered (gdb_stdlog
,
1221 _("Process record: failed to record "
1225 if (record_arch_list_add_end ())
1227 record_list_release (record_arch_list_tail
);
1229 fprintf_unfiltered (gdb_stdlog
,
1230 _("Process record: failed to record "
1234 record_list
->next
= record_arch_list_head
;
1235 record_arch_list_head
->prev
= record_list
;
1236 record_list
= record_arch_list_tail
;
1238 if (record_insn_num
== record_insn_max_num
&& record_insn_max_num
)
1239 record_list_release_first ();
1244 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops
,
1245 object
, annex
, readbuf
, writebuf
,
1249 /* Behavior is conditional on RECORD_IS_REPLAY.
1250 We will not actually insert or remove breakpoints when replaying,
1251 nor when recording. */
1254 record_insert_breakpoint (struct gdbarch
*gdbarch
,
1255 struct bp_target_info
*bp_tgt
)
1257 if (!RECORD_IS_REPLAY
)
1259 struct cleanup
*old_cleanups
= record_gdb_operation_disable_set ();
1260 int ret
= record_beneath_to_insert_breakpoint (gdbarch
, bp_tgt
);
1262 do_cleanups (old_cleanups
);
1271 record_remove_breakpoint (struct gdbarch
*gdbarch
,
1272 struct bp_target_info
*bp_tgt
)
1274 if (!RECORD_IS_REPLAY
)
1276 struct cleanup
*old_cleanups
= record_gdb_operation_disable_set ();
1277 int ret
= record_beneath_to_remove_breakpoint (gdbarch
, bp_tgt
);
1279 do_cleanups (old_cleanups
);
1288 record_can_execute_reverse (void)
1294 init_record_ops (void)
1296 record_ops
.to_shortname
= "record";
1297 record_ops
.to_longname
= "Process record and replay target";
1299 "Log program while executing and replay execution from log.";
1300 record_ops
.to_open
= record_open
;
1301 record_ops
.to_close
= record_close
;
1302 record_ops
.to_resume
= record_resume
;
1303 record_ops
.to_wait
= record_wait
;
1304 record_ops
.to_disconnect
= record_disconnect
;
1305 record_ops
.to_detach
= record_detach
;
1306 record_ops
.to_mourn_inferior
= record_mourn_inferior
;
1307 record_ops
.to_kill
= record_kill
;
1308 record_ops
.to_create_inferior
= find_default_create_inferior
;
1309 record_ops
.to_store_registers
= record_store_registers
;
1310 record_ops
.to_xfer_partial
= record_xfer_partial
;
1311 record_ops
.to_insert_breakpoint
= record_insert_breakpoint
;
1312 record_ops
.to_remove_breakpoint
= record_remove_breakpoint
;
1313 record_ops
.to_can_execute_reverse
= record_can_execute_reverse
;
1314 record_ops
.to_stratum
= record_stratum
;
1315 record_ops
.to_magic
= OPS_MAGIC
;
1319 show_record_debug (struct ui_file
*file
, int from_tty
,
1320 struct cmd_list_element
*c
, const char *value
)
1322 fprintf_filtered (file
, _("Debugging of process record target is %s.\n"),
1326 /* Alias for "target record". */
1329 cmd_record_start (char *args
, int from_tty
)
1331 execute_command ("target record", from_tty
);
1334 /* Truncate the record log from the present point
1335 of replay until the end. */
1338 cmd_record_delete (char *args
, int from_tty
)
1340 if (current_target
.to_stratum
== record_stratum
)
1342 if (RECORD_IS_REPLAY
)
1344 if (!from_tty
|| query (_("Delete the log from this point forward "
1345 "and begin to record the running message "
1347 record_list_release_following (record_list
);
1350 printf_unfiltered (_("Already at end of record list.\n"));
1354 printf_unfiltered (_("Process record is not started.\n"));
1357 /* Implement the "stoprecord" command. */
1360 cmd_record_stop (char *args
, int from_tty
)
1362 if (current_target
.to_stratum
== record_stratum
)
1364 unpush_target (&record_ops
);
1365 printf_unfiltered (_("Process record is stoped and all execution "
1366 "log is deleted.\n"));
1369 printf_unfiltered (_("Process record is not started.\n"));
1372 /* Set upper limit of record log size. */
1375 set_record_insn_max_num (char *args
, int from_tty
, struct cmd_list_element
*c
)
1377 if (record_insn_num
> record_insn_max_num
&& record_insn_max_num
)
1379 /* Count down record_insn_num while releasing records from list. */
1380 while (record_insn_num
> record_insn_max_num
)
1382 record_list_release_first ();
1388 /* Print the current index into the record log (number of insns recorded
1392 show_record_insn_number (char *ignore
, int from_tty
)
1394 printf_unfiltered (_("Record instruction number is %d.\n"),
1398 static struct cmd_list_element
*record_cmdlist
, *set_record_cmdlist
,
1399 *show_record_cmdlist
, *info_record_cmdlist
;
1402 set_record_command (char *args
, int from_tty
)
1404 printf_unfiltered (_("\
1405 \"set record\" must be followed by an apporpriate subcommand.\n"));
1406 help_list (set_record_cmdlist
, "set record ", all_commands
, gdb_stdout
);
1410 show_record_command (char *args
, int from_tty
)
1412 cmd_show_list (show_record_cmdlist
, from_tty
, "");
1416 info_record_command (char *args
, int from_tty
)
1418 cmd_show_list (info_record_cmdlist
, from_tty
, "");
1422 _initialize_record (void)
1424 /* Init record_first. */
1425 record_first
.prev
= NULL
;
1426 record_first
.next
= NULL
;
1427 record_first
.type
= record_end
;
1430 add_target (&record_ops
);
1432 add_setshow_zinteger_cmd ("record", no_class
, &record_debug
,
1433 _("Set debugging of record/replay feature."),
1434 _("Show debugging of record/replay feature."),
1435 _("When enabled, debugging output for "
1436 "record/replay feature is displayed."),
1437 NULL
, show_record_debug
, &setdebuglist
,
1440 add_prefix_cmd ("record", class_obscure
, cmd_record_start
,
1441 _("Abbreviated form of \"target record\" command."),
1442 &record_cmdlist
, "record ", 0, &cmdlist
);
1443 add_com_alias ("rec", "record", class_obscure
, 1);
1444 add_prefix_cmd ("record", class_support
, set_record_command
,
1445 _("Set record options"), &set_record_cmdlist
,
1446 "set record ", 0, &setlist
);
1447 add_alias_cmd ("rec", "record", class_obscure
, 1, &setlist
);
1448 add_prefix_cmd ("record", class_support
, show_record_command
,
1449 _("Show record options"), &show_record_cmdlist
,
1450 "show record ", 0, &showlist
);
1451 add_alias_cmd ("rec", "record", class_obscure
, 1, &showlist
);
1452 add_prefix_cmd ("record", class_support
, info_record_command
,
1453 _("Info record options"), &info_record_cmdlist
,
1454 "info record ", 0, &infolist
);
1455 add_alias_cmd ("rec", "record", class_obscure
, 1, &infolist
);
1458 add_cmd ("delete", class_obscure
, cmd_record_delete
,
1459 _("Delete the rest of execution log and start recording it anew."),
1461 add_alias_cmd ("d", "delete", class_obscure
, 1, &record_cmdlist
);
1462 add_alias_cmd ("del", "delete", class_obscure
, 1, &record_cmdlist
);
1464 add_cmd ("stop", class_obscure
, cmd_record_stop
,
1465 _("Stop the record/replay target."),
1467 add_alias_cmd ("s", "stop", class_obscure
, 1, &record_cmdlist
);
1469 /* Record instructions number limit command. */
1470 add_setshow_boolean_cmd ("stop-at-limit", no_class
,
1471 &record_stop_at_limit
, _("\
1472 Set whether record/replay stops when record/replay buffer becomes full."), _("\
1473 Show whether record/replay stops when record/replay buffer becomes full."), _("\
1475 When ON, if the record/replay buffer becomes full, ask user what to do.\n\
1476 When OFF, if the record/replay buffer becomes full,\n\
1477 delete the oldest recorded instruction to make room for each new one."),
1479 &set_record_cmdlist
, &show_record_cmdlist
);
1480 add_setshow_uinteger_cmd ("insn-number-max", no_class
,
1481 &record_insn_max_num
,
1482 _("Set record/replay buffer limit."),
1483 _("Show record/replay buffer limit."), _("\
1484 Set the maximum number of instructions to be stored in the\n\
1485 record/replay buffer. Zero means unlimited. Default is 200000."),
1486 set_record_insn_max_num
,
1487 NULL
, &set_record_cmdlist
, &show_record_cmdlist
);
1488 add_cmd ("insn-number", class_obscure
, show_record_insn_number
,
1489 _("Show the current number of instructions in the "
1490 "record/replay buffer."), &info_record_cmdlist
);