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 /* Yes -- check if there is a breakpoint. */
782 registers_changed ();
783 tmp_pc
= regcache_read_pc (get_current_regcache ());
784 if (breakpoint_inserted_here_p (tmp_pc
))
786 /* There is a breakpoint. GDB will want to stop. */
787 CORE_ADDR decr_pc_after_break
=
788 gdbarch_decr_pc_after_break
789 (get_regcache_arch (get_current_regcache ()));
790 if (decr_pc_after_break
)
792 regcache_write_pc (get_thread_regcache (ret
),
793 tmp_pc
+ decr_pc_after_break
);
798 /* There is not a breakpoint, and gdb is not
799 stepping, therefore gdb will not stop.
800 Therefore we will not return to gdb.
801 Record the insn and resume. */
802 if (!do_record_message (get_current_regcache (),
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 (tmp_pc
))
839 fprintf_unfiltered (gdb_stdlog
,
840 "Process record: break at %s.\n",
841 paddress (gdbarch
, tmp_pc
));
842 if (gdbarch_decr_pc_after_break (gdbarch
)
843 && !record_resume_step
)
844 regcache_write_pc (regcache
,
846 gdbarch_decr_pc_after_break (gdbarch
));
852 signal (SIGINT
, record_sig_handler
);
853 /* If GDB is in terminal_inferior mode, it will not get the signal.
854 And in GDB replay mode, GDB doesn't need to be in terminal_inferior
855 mode, because inferior will not executed.
856 Then set it to terminal_ours to make GDB get the signal. */
857 target_terminal_ours ();
859 /* In EXEC_FORWARD mode, record_list points to the tail of prev
861 if (execution_direction
== EXEC_FORWARD
&& record_list
->next
)
862 record_list
= record_list
->next
;
864 /* Loop over the record_list, looking for the next place to
868 /* Check for beginning and end of log. */
869 if (execution_direction
== EXEC_REVERSE
870 && record_list
== &record_first
)
872 /* Hit beginning of record log in reverse. */
873 status
->kind
= TARGET_WAITKIND_NO_HISTORY
;
876 if (execution_direction
!= EXEC_REVERSE
&& !record_list
->next
)
878 /* Hit end of record log going forward. */
879 status
->kind
= TARGET_WAITKIND_NO_HISTORY
;
883 /* Set ptid, register and memory according to record_list. */
884 if (record_list
->type
== record_reg
)
887 gdb_byte reg
[MAX_REGISTER_SIZE
];
888 if (record_debug
> 1)
889 fprintf_unfiltered (gdb_stdlog
,
890 "Process record: record_reg %s to "
891 "inferior num = %d.\n",
892 host_address_to_string (record_list
),
893 record_list
->u
.reg
.num
);
894 regcache_cooked_read (regcache
, record_list
->u
.reg
.num
, reg
);
895 regcache_cooked_write (regcache
, record_list
->u
.reg
.num
,
896 record_get_loc (record_list
));
897 memcpy (record_get_loc (record_list
), reg
,
898 record_list
->u
.reg
.len
);
900 else if (record_list
->type
== record_mem
)
903 /* Nothing to do if the entry is flagged not_accessible. */
904 if (!record_list
->u
.mem
.mem_entry_not_accessible
)
906 gdb_byte
*mem
= alloca (record_list
->u
.mem
.len
);
907 if (record_debug
> 1)
908 fprintf_unfiltered (gdb_stdlog
,
909 "Process record: record_mem %s to "
910 "inferior addr = %s len = %d.\n",
911 host_address_to_string (record_list
),
913 record_list
->u
.mem
.addr
),
914 record_list
->u
.mem
.len
);
916 if (target_read_memory (record_list
->u
.mem
.addr
, mem
,
917 record_list
->u
.mem
.len
))
919 if (execution_direction
!= EXEC_REVERSE
)
920 error (_("Process record: error reading memory at "
921 "addr = %s len = %d."),
922 paddress (gdbarch
, record_list
->u
.mem
.addr
),
923 record_list
->u
.mem
.len
);
926 flag entry as not_accessible. */
927 record_list
->u
.mem
.mem_entry_not_accessible
= 1;
931 if (target_write_memory (record_list
->u
.mem
.addr
,
932 record_get_loc (record_list
),
933 record_list
->u
.mem
.len
))
935 if (execution_direction
!= EXEC_REVERSE
)
936 error (_("Process record: error writing memory at "
937 "addr = %s len = %d."),
938 paddress (gdbarch
, record_list
->u
.mem
.addr
),
939 record_list
->u
.mem
.len
);
942 flag entry as not_accessible. */
943 record_list
->u
.mem
.mem_entry_not_accessible
= 1;
947 memcpy (record_get_loc (record_list
), mem
,
948 record_list
->u
.mem
.len
);
955 if (record_debug
> 1)
956 fprintf_unfiltered (gdb_stdlog
,
957 "Process record: record_end %s to "
959 host_address_to_string (record_list
));
961 if (first_record_end
&& execution_direction
== EXEC_REVERSE
)
963 /* When reverse excute, the first record_end is the part of
964 current instruction. */
965 first_record_end
= 0;
969 /* In EXEC_REVERSE mode, this is the record_end of prev
971 In EXEC_FORWARD mode, this is the record_end of current
974 if (record_resume_step
)
976 if (record_debug
> 1)
977 fprintf_unfiltered (gdb_stdlog
,
978 "Process record: step.\n");
982 /* check breakpoint */
983 tmp_pc
= regcache_read_pc (regcache
);
984 if (breakpoint_inserted_here_p (tmp_pc
))
987 fprintf_unfiltered (gdb_stdlog
,
988 "Process record: break "
990 paddress (gdbarch
, tmp_pc
));
991 if (gdbarch_decr_pc_after_break (gdbarch
)
992 && execution_direction
== EXEC_FORWARD
993 && !record_resume_step
)
994 regcache_write_pc (regcache
,
996 gdbarch_decr_pc_after_break (gdbarch
));
999 /* Check target signal */
1000 if (record_list
->u
.end
.sigval
!= TARGET_SIGNAL_0
)
1001 /* FIXME: better way to check */
1008 if (execution_direction
== EXEC_REVERSE
)
1010 if (record_list
->prev
)
1011 record_list
= record_list
->prev
;
1015 if (record_list
->next
)
1016 record_list
= record_list
->next
;
1020 while (continue_flag
);
1022 signal (SIGINT
, handle_sigint
);
1026 status
->value
.sig
= TARGET_SIGNAL_INT
;
1027 else if (record_list
->u
.end
.sigval
!= TARGET_SIGNAL_0
)
1028 /* FIXME: better way to check */
1029 status
->value
.sig
= record_list
->u
.end
.sigval
;
1031 status
->value
.sig
= TARGET_SIGNAL_TRAP
;
1033 discard_cleanups (old_cleanups
);
1036 do_cleanups (set_cleanups
);
1037 return inferior_ptid
;
1041 record_disconnect (struct target_ops
*target
, char *args
, int from_tty
)
1044 fprintf_unfiltered (gdb_stdlog
, "Process record: record_disconnect\n");
1046 unpush_target (&record_ops
);
1047 target_disconnect (args
, from_tty
);
1051 record_detach (struct target_ops
*ops
, char *args
, int from_tty
)
1054 fprintf_unfiltered (gdb_stdlog
, "Process record: record_detach\n");
1056 unpush_target (&record_ops
);
1057 target_detach (args
, from_tty
);
1061 record_mourn_inferior (struct target_ops
*ops
)
1064 fprintf_unfiltered (gdb_stdlog
, "Process record: "
1065 "record_mourn_inferior\n");
1067 unpush_target (&record_ops
);
1068 target_mourn_inferior ();
1071 /* Close process record target before killing the inferior process. */
1074 record_kill (struct target_ops
*ops
)
1077 fprintf_unfiltered (gdb_stdlog
, "Process record: record_kill\n");
1079 unpush_target (&record_ops
);
1083 /* Record registers change (by user or by GDB) to list as an instruction. */
1086 record_registers_change (struct regcache
*regcache
, int regnum
)
1088 /* Check record_insn_num. */
1089 record_check_insn_num (0);
1091 record_arch_list_head
= NULL
;
1092 record_arch_list_tail
= NULL
;
1097 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
1099 if (record_arch_list_add_reg (regcache
, i
))
1101 record_list_release (record_arch_list_tail
);
1102 error (_("Process record: failed to record execution log."));
1108 if (record_arch_list_add_reg (regcache
, regnum
))
1110 record_list_release (record_arch_list_tail
);
1111 error (_("Process record: failed to record execution log."));
1114 if (record_arch_list_add_end ())
1116 record_list_release (record_arch_list_tail
);
1117 error (_("Process record: failed to record execution log."));
1119 record_list
->next
= record_arch_list_head
;
1120 record_arch_list_head
->prev
= record_list
;
1121 record_list
= record_arch_list_tail
;
1123 if (record_insn_num
== record_insn_max_num
&& record_insn_max_num
)
1124 record_list_release_first ();
1130 record_store_registers (struct target_ops
*ops
, struct regcache
*regcache
,
1133 if (!record_gdb_operation_disable
)
1135 if (RECORD_IS_REPLAY
)
1139 /* Let user choose if he wants to write register or not. */
1142 query (_("Because GDB is in replay mode, changing the "
1143 "value of a register will make the execution "
1144 "log unusable from this point onward. "
1145 "Change all registers?"));
1148 query (_("Because GDB is in replay mode, changing the value "
1149 "of a register will make the execution log unusable "
1150 "from this point onward. Change register %s?"),
1151 gdbarch_register_name (get_regcache_arch (regcache
),
1156 /* Invalidate the value of regcache that was set in function
1157 "regcache_raw_write". */
1162 i
< gdbarch_num_regs (get_regcache_arch (regcache
));
1164 regcache_invalidate (regcache
, i
);
1167 regcache_invalidate (regcache
, regno
);
1169 error (_("Process record canceled the operation."));
1172 /* Destroy the record from here forward. */
1173 record_list_release_following (record_list
);
1176 record_registers_change (regcache
, regno
);
1178 record_beneath_to_store_registers (record_beneath_to_store_registers_ops
,
1182 /* Behavior is conditional on RECORD_IS_REPLAY.
1183 In replay mode, we cannot write memory unles we are willing to
1184 invalidate the record/replay log from this point forward. */
1187 record_xfer_partial (struct target_ops
*ops
, enum target_object object
,
1188 const char *annex
, gdb_byte
*readbuf
,
1189 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
1191 if (!record_gdb_operation_disable
1192 && (object
== TARGET_OBJECT_MEMORY
1193 || object
== TARGET_OBJECT_RAW_MEMORY
) && writebuf
)
1195 if (RECORD_IS_REPLAY
)
1197 /* Let user choose if he wants to write memory or not. */
1198 if (!query (_("Because GDB is in replay mode, writing to memory "
1199 "will make the execution log unusable from this "
1200 "point onward. Write memory at address %s?"),
1201 paddress (target_gdbarch
, offset
)))
1202 error (_("Process record canceled the operation."));
1204 /* Destroy the record from here forward. */
1205 record_list_release_following (record_list
);
1208 /* Check record_insn_num */
1209 record_check_insn_num (0);
1211 /* Record registers change to list as an instruction. */
1212 record_arch_list_head
= NULL
;
1213 record_arch_list_tail
= NULL
;
1214 if (record_arch_list_add_mem (offset
, len
))
1216 record_list_release (record_arch_list_tail
);
1218 fprintf_unfiltered (gdb_stdlog
,
1219 _("Process record: failed to record "
1223 if (record_arch_list_add_end ())
1225 record_list_release (record_arch_list_tail
);
1227 fprintf_unfiltered (gdb_stdlog
,
1228 _("Process record: failed to record "
1232 record_list
->next
= record_arch_list_head
;
1233 record_arch_list_head
->prev
= record_list
;
1234 record_list
= record_arch_list_tail
;
1236 if (record_insn_num
== record_insn_max_num
&& record_insn_max_num
)
1237 record_list_release_first ();
1242 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops
,
1243 object
, annex
, readbuf
, writebuf
,
1247 /* Behavior is conditional on RECORD_IS_REPLAY.
1248 We will not actually insert or remove breakpoints when replaying,
1249 nor when recording. */
1252 record_insert_breakpoint (struct gdbarch
*gdbarch
,
1253 struct bp_target_info
*bp_tgt
)
1255 if (!RECORD_IS_REPLAY
)
1257 struct cleanup
*old_cleanups
= record_gdb_operation_disable_set ();
1258 int ret
= record_beneath_to_insert_breakpoint (gdbarch
, bp_tgt
);
1260 do_cleanups (old_cleanups
);
1269 record_remove_breakpoint (struct gdbarch
*gdbarch
,
1270 struct bp_target_info
*bp_tgt
)
1272 if (!RECORD_IS_REPLAY
)
1274 struct cleanup
*old_cleanups
= record_gdb_operation_disable_set ();
1275 int ret
= record_beneath_to_remove_breakpoint (gdbarch
, bp_tgt
);
1277 do_cleanups (old_cleanups
);
1286 record_can_execute_reverse (void)
1292 init_record_ops (void)
1294 record_ops
.to_shortname
= "record";
1295 record_ops
.to_longname
= "Process record and replay target";
1297 "Log program while executing and replay execution from log.";
1298 record_ops
.to_open
= record_open
;
1299 record_ops
.to_close
= record_close
;
1300 record_ops
.to_resume
= record_resume
;
1301 record_ops
.to_wait
= record_wait
;
1302 record_ops
.to_disconnect
= record_disconnect
;
1303 record_ops
.to_detach
= record_detach
;
1304 record_ops
.to_mourn_inferior
= record_mourn_inferior
;
1305 record_ops
.to_kill
= record_kill
;
1306 record_ops
.to_create_inferior
= find_default_create_inferior
;
1307 record_ops
.to_store_registers
= record_store_registers
;
1308 record_ops
.to_xfer_partial
= record_xfer_partial
;
1309 record_ops
.to_insert_breakpoint
= record_insert_breakpoint
;
1310 record_ops
.to_remove_breakpoint
= record_remove_breakpoint
;
1311 record_ops
.to_can_execute_reverse
= record_can_execute_reverse
;
1312 record_ops
.to_stratum
= record_stratum
;
1313 record_ops
.to_magic
= OPS_MAGIC
;
1317 show_record_debug (struct ui_file
*file
, int from_tty
,
1318 struct cmd_list_element
*c
, const char *value
)
1320 fprintf_filtered (file
, _("Debugging of process record target is %s.\n"),
1324 /* Alias for "target record". */
1327 cmd_record_start (char *args
, int from_tty
)
1329 execute_command ("target record", from_tty
);
1332 /* Truncate the record log from the present point
1333 of replay until the end. */
1336 cmd_record_delete (char *args
, int from_tty
)
1338 if (current_target
.to_stratum
== record_stratum
)
1340 if (RECORD_IS_REPLAY
)
1342 if (!from_tty
|| query (_("Delete the log from this point forward "
1343 "and begin to record the running message "
1345 record_list_release_following (record_list
);
1348 printf_unfiltered (_("Already at end of record list.\n"));
1352 printf_unfiltered (_("Process record is not started.\n"));
1355 /* Implement the "stoprecord" command. */
1358 cmd_record_stop (char *args
, int from_tty
)
1360 if (current_target
.to_stratum
== record_stratum
)
1362 unpush_target (&record_ops
);
1363 printf_unfiltered (_("Process record is stoped and all execution "
1364 "log is deleted.\n"));
1367 printf_unfiltered (_("Process record is not started.\n"));
1370 /* Set upper limit of record log size. */
1373 set_record_insn_max_num (char *args
, int from_tty
, struct cmd_list_element
*c
)
1375 if (record_insn_num
> record_insn_max_num
&& record_insn_max_num
)
1377 /* Count down record_insn_num while releasing records from list. */
1378 while (record_insn_num
> record_insn_max_num
)
1380 record_list_release_first ();
1386 /* Print the current index into the record log (number of insns recorded
1390 show_record_insn_number (char *ignore
, int from_tty
)
1392 printf_unfiltered (_("Record instruction number is %d.\n"),
1396 static struct cmd_list_element
*record_cmdlist
, *set_record_cmdlist
,
1397 *show_record_cmdlist
, *info_record_cmdlist
;
1400 set_record_command (char *args
, int from_tty
)
1402 printf_unfiltered (_("\
1403 \"set record\" must be followed by an apporpriate subcommand.\n"));
1404 help_list (set_record_cmdlist
, "set record ", all_commands
, gdb_stdout
);
1408 show_record_command (char *args
, int from_tty
)
1410 cmd_show_list (show_record_cmdlist
, from_tty
, "");
1414 info_record_command (char *args
, int from_tty
)
1416 cmd_show_list (info_record_cmdlist
, from_tty
, "");
1420 _initialize_record (void)
1422 /* Init record_first. */
1423 record_first
.prev
= NULL
;
1424 record_first
.next
= NULL
;
1425 record_first
.type
= record_end
;
1428 add_target (&record_ops
);
1430 add_setshow_zinteger_cmd ("record", no_class
, &record_debug
,
1431 _("Set debugging of record/replay feature."),
1432 _("Show debugging of record/replay feature."),
1433 _("When enabled, debugging output for "
1434 "record/replay feature is displayed."),
1435 NULL
, show_record_debug
, &setdebuglist
,
1438 add_prefix_cmd ("record", class_obscure
, cmd_record_start
,
1439 _("Abbreviated form of \"target record\" command."),
1440 &record_cmdlist
, "record ", 0, &cmdlist
);
1441 add_com_alias ("rec", "record", class_obscure
, 1);
1442 add_prefix_cmd ("record", class_support
, set_record_command
,
1443 _("Set record options"), &set_record_cmdlist
,
1444 "set record ", 0, &setlist
);
1445 add_alias_cmd ("rec", "record", class_obscure
, 1, &setlist
);
1446 add_prefix_cmd ("record", class_support
, show_record_command
,
1447 _("Show record options"), &show_record_cmdlist
,
1448 "show record ", 0, &showlist
);
1449 add_alias_cmd ("rec", "record", class_obscure
, 1, &showlist
);
1450 add_prefix_cmd ("record", class_support
, info_record_command
,
1451 _("Info record options"), &info_record_cmdlist
,
1452 "info record ", 0, &infolist
);
1453 add_alias_cmd ("rec", "record", class_obscure
, 1, &infolist
);
1456 add_cmd ("delete", class_obscure
, cmd_record_delete
,
1457 _("Delete the rest of execution log and start recording it anew."),
1459 add_alias_cmd ("d", "delete", class_obscure
, 1, &record_cmdlist
);
1460 add_alias_cmd ("del", "delete", class_obscure
, 1, &record_cmdlist
);
1462 add_cmd ("stop", class_obscure
, cmd_record_stop
,
1463 _("Stop the record/replay target."),
1465 add_alias_cmd ("s", "stop", class_obscure
, 1, &record_cmdlist
);
1467 /* Record instructions number limit command. */
1468 add_setshow_boolean_cmd ("stop-at-limit", no_class
,
1469 &record_stop_at_limit
, _("\
1470 Set whether record/replay stops when record/replay buffer becomes full."), _("\
1471 Show whether record/replay stops when record/replay buffer becomes full."), _("\
1473 When ON, if the record/replay buffer becomes full, ask user what to do.\n\
1474 When OFF, if the record/replay buffer becomes full,\n\
1475 delete the oldest recorded instruction to make room for each new one."),
1477 &set_record_cmdlist
, &show_record_cmdlist
);
1478 add_setshow_uinteger_cmd ("insn-number-max", no_class
,
1479 &record_insn_max_num
,
1480 _("Set record/replay buffer limit."),
1481 _("Show record/replay buffer limit."), _("\
1482 Set the maximum number of instructions to be stored in the\n\
1483 record/replay buffer. Zero means unlimited. Default is 200000."),
1484 set_record_insn_max_num
,
1485 NULL
, &set_record_cmdlist
, &show_record_cmdlist
);
1486 add_cmd ("insn-number", class_obscure
, show_record_insn_number
,
1487 _("Show the current number of instructions in the "
1488 "record/replay buffer."), &info_record_cmdlist
);