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_reg_entry
52 struct record_mem_entry
56 /* Set this flag if target memory for this entry
57 can no longer be accessed. */
58 int mem_entry_not_accessible
;
62 struct record_end_entry
64 enum target_signal sigval
;
76 struct record_entry
*prev
;
77 struct record_entry
*next
;
78 enum record_type type
;
82 struct record_reg_entry reg
;
84 struct record_mem_entry mem
;
86 struct record_end_entry end
;
90 /* This is the debug switch for process record. */
93 /* These list is for execution log. */
94 static struct record_entry record_first
;
95 static struct record_entry
*record_list
= &record_first
;
96 static struct record_entry
*record_arch_list_head
= NULL
;
97 static struct record_entry
*record_arch_list_tail
= NULL
;
99 /* 1 ask user. 0 auto delete the last struct record_entry. */
100 static int record_stop_at_limit
= 1;
101 static int record_insn_max_num
= DEFAULT_RECORD_INSN_MAX_NUM
;
102 static int record_insn_num
= 0;
104 /* The target_ops of process record. */
105 static struct target_ops record_ops
;
107 /* The beneath function pointers. */
108 static struct target_ops
*record_beneath_to_resume_ops
;
109 static void (*record_beneath_to_resume
) (struct target_ops
*, ptid_t
, int,
111 static struct target_ops
*record_beneath_to_wait_ops
;
112 static ptid_t (*record_beneath_to_wait
) (struct target_ops
*, ptid_t
,
113 struct target_waitstatus
*,
115 static struct target_ops
*record_beneath_to_store_registers_ops
;
116 static void (*record_beneath_to_store_registers
) (struct target_ops
*,
119 static struct target_ops
*record_beneath_to_xfer_partial_ops
;
120 static LONGEST (*record_beneath_to_xfer_partial
) (struct target_ops
*ops
,
121 enum target_object object
,
124 const gdb_byte
*writebuf
,
127 static int (*record_beneath_to_insert_breakpoint
) (struct gdbarch
*,
128 struct bp_target_info
*);
129 static int (*record_beneath_to_remove_breakpoint
) (struct gdbarch
*,
130 struct bp_target_info
*);
133 record_list_release (struct record_entry
*rec
)
135 struct record_entry
*tmp
;
149 if (tmp
->type
== record_reg
)
150 xfree (tmp
->u
.reg
.val
);
151 else if (tmp
->type
== record_mem
)
152 xfree (tmp
->u
.mem
.val
);
156 if (rec
!= &record_first
)
161 record_list_release_next (void)
163 struct record_entry
*rec
= record_list
;
164 struct record_entry
*tmp
= rec
->next
;
169 if (tmp
->type
== record_end
)
171 else if (tmp
->type
== record_reg
)
172 xfree (tmp
->u
.reg
.val
);
173 else if (tmp
->type
== record_mem
)
174 xfree (tmp
->u
.mem
.val
);
181 record_list_release_first (void)
183 struct record_entry
*tmp
= NULL
;
184 enum record_type type
;
186 if (!record_first
.next
)
191 type
= record_first
.next
->type
;
193 if (type
== record_reg
)
194 xfree (record_first
.next
->u
.reg
.val
);
195 else if (type
== record_mem
)
196 xfree (record_first
.next
->u
.mem
.val
);
197 tmp
= record_first
.next
;
198 record_first
.next
= tmp
->next
;
201 if (!record_first
.next
)
203 gdb_assert (record_insn_num
== 1);
207 record_first
.next
->prev
= &record_first
;
209 if (type
== record_end
)
216 /* Add a struct record_entry to record_arch_list. */
219 record_arch_list_add (struct record_entry
*rec
)
221 if (record_debug
> 1)
222 fprintf_unfiltered (gdb_stdlog
,
223 "Process record: record_arch_list_add %s.\n",
224 host_address_to_string (rec
));
226 if (record_arch_list_tail
)
228 record_arch_list_tail
->next
= rec
;
229 rec
->prev
= record_arch_list_tail
;
230 record_arch_list_tail
= rec
;
234 record_arch_list_head
= rec
;
235 record_arch_list_tail
= rec
;
239 /* Record the value of a register NUM to record_arch_list. */
242 record_arch_list_add_reg (struct regcache
*regcache
, int num
)
244 struct record_entry
*rec
;
246 if (record_debug
> 1)
247 fprintf_unfiltered (gdb_stdlog
,
248 "Process record: add register num = %d to "
252 rec
= (struct record_entry
*) xmalloc (sizeof (struct record_entry
));
253 rec
->u
.reg
.val
= (gdb_byte
*) xmalloc (MAX_REGISTER_SIZE
);
256 rec
->type
= record_reg
;
257 rec
->u
.reg
.num
= num
;
259 regcache_raw_read (regcache
, num
, rec
->u
.reg
.val
);
261 record_arch_list_add (rec
);
266 /* Record the value of a region of memory whose address is ADDR and
267 length is LEN to record_arch_list. */
270 record_arch_list_add_mem (CORE_ADDR addr
, int len
)
272 struct record_entry
*rec
;
274 if (record_debug
> 1)
275 fprintf_unfiltered (gdb_stdlog
,
276 "Process record: add mem addr = %s len = %d to "
278 paddress (target_gdbarch
, addr
), len
);
283 rec
= (struct record_entry
*) xmalloc (sizeof (struct record_entry
));
284 rec
->u
.mem
.val
= (gdb_byte
*) xmalloc (len
);
287 rec
->type
= record_mem
;
288 rec
->u
.mem
.addr
= addr
;
289 rec
->u
.mem
.len
= len
;
290 rec
->u
.mem
.mem_entry_not_accessible
= 0;
292 if (target_read_memory (addr
, rec
->u
.mem
.val
, len
))
295 fprintf_unfiltered (gdb_stdlog
,
296 "Process record: error reading memory at "
297 "addr = %s len = %d.\n",
298 paddress (target_gdbarch
, addr
), len
);
299 xfree (rec
->u
.mem
.val
);
304 record_arch_list_add (rec
);
309 /* Add a record_end type struct record_entry to record_arch_list. */
312 record_arch_list_add_end (void)
314 struct record_entry
*rec
;
316 if (record_debug
> 1)
317 fprintf_unfiltered (gdb_stdlog
,
318 "Process record: add end to arch list.\n");
320 rec
= (struct record_entry
*) xmalloc (sizeof (struct record_entry
));
323 rec
->type
= record_end
;
324 rec
->u
.end
.sigval
= TARGET_SIGNAL_0
;
326 record_arch_list_add (rec
);
332 record_check_insn_num (int set_terminal
)
334 if (record_insn_max_num
)
336 gdb_assert (record_insn_num
<= record_insn_max_num
);
337 if (record_insn_num
== record_insn_max_num
)
339 /* Ask user what to do. */
340 if (record_stop_at_limit
)
344 target_terminal_ours ();
345 q
= yquery (_("Do you want to auto delete previous execution "
346 "log entries when record/replay buffer becomes "
347 "full (record stop-at-limit)?"));
349 target_terminal_inferior ();
351 record_stop_at_limit
= 0;
353 error (_("Process record: inferior program stopped."));
359 /* Before inferior step (when GDB record the running message, inferior
360 only can step), GDB will call this function to record the values to
361 record_list. This function will call gdbarch_process_record to
362 record the running message of inferior and set them to
363 record_arch_list, and add it to record_list. */
366 record_message_cleanups (void *ignore
)
368 record_list_release (record_arch_list_tail
);
371 struct record_message_args
{
372 struct regcache
*regcache
;
373 enum target_signal signal
;
377 record_message (void *args
)
380 struct record_message_args
*myargs
= args
;
381 struct gdbarch
*gdbarch
= get_regcache_arch (myargs
->regcache
);
382 struct cleanup
*old_cleanups
= make_cleanup (record_message_cleanups
, 0);
384 record_arch_list_head
= NULL
;
385 record_arch_list_tail
= NULL
;
387 /* Check record_insn_num. */
388 record_check_insn_num (1);
390 /* If gdb sends a signal value to target_resume,
391 save it in the 'end' field of the previous instruction.
393 Maybe process record should record what really happened,
394 rather than what gdb pretends has happened.
396 So if Linux delivered the signal to the child process during
397 the record mode, we will record it and deliver it again in
400 If user says "ignore this signal" during the record mode, then
401 it will be ignored again during the replay mode (no matter if
402 the user says something different, like "deliver this signal"
403 during the replay mode).
405 User should understand that nothing he does during the replay
406 mode will change the behavior of the child. If he tries,
407 then that is a user error.
409 But we should still deliver the signal to gdb during the replay,
410 if we delivered it during the recording. Therefore we should
411 record the signal during record_wait, not record_resume. */
412 if (record_list
!= &record_first
) /* FIXME better way to check */
414 gdb_assert (record_list
->type
== record_end
);
415 record_list
->u
.end
.sigval
= myargs
->signal
;
418 if (myargs
->signal
== TARGET_SIGNAL_0
419 || !gdbarch_process_record_signal_p (gdbarch
))
420 ret
= gdbarch_process_record (gdbarch
,
422 regcache_read_pc (myargs
->regcache
));
424 ret
= gdbarch_process_record_signal (gdbarch
,
429 error (_("Process record: inferior program stopped."));
431 error (_("Process record: failed to record execution log."));
433 discard_cleanups (old_cleanups
);
435 record_list
->next
= record_arch_list_head
;
436 record_arch_list_head
->prev
= record_list
;
437 record_list
= record_arch_list_tail
;
439 if (record_insn_num
== record_insn_max_num
&& record_insn_max_num
)
440 record_list_release_first ();
448 do_record_message (struct regcache
*regcache
,
449 enum target_signal signal
)
451 struct record_message_args args
;
453 args
.regcache
= regcache
;
454 args
.signal
= signal
;
455 return catch_errors (record_message
, &args
, NULL
, RETURN_MASK_ALL
);
458 /* Set to 1 if record_store_registers and record_xfer_partial
459 doesn't need record. */
461 static int record_gdb_operation_disable
= 0;
464 record_gdb_operation_disable_set (void)
466 struct cleanup
*old_cleanups
= NULL
;
469 make_cleanup_restore_integer (&record_gdb_operation_disable
);
470 record_gdb_operation_disable
= 1;
476 record_open (char *name
, int from_tty
)
478 struct target_ops
*t
;
481 fprintf_unfiltered (gdb_stdlog
, "Process record: record_open\n");
484 if (!target_has_execution
)
485 error (_("Process record: the program is not being run."));
487 error (_("Process record target can't debug inferior in non-stop mode "
489 if (target_async_permitted
)
490 error (_("Process record target can't debug inferior in asynchronous "
491 "mode (target-async)."));
493 if (!gdbarch_process_record_p (target_gdbarch
))
494 error (_("Process record: the current architecture doesn't support "
495 "record function."));
497 /* Check if record target is already running. */
498 if (current_target
.to_stratum
== record_stratum
)
501 (_("Process record target already running, do you want to delete "
502 "the old record log?")))
506 /*Reset the beneath function pointers. */
507 record_beneath_to_resume
= NULL
;
508 record_beneath_to_wait
= NULL
;
509 record_beneath_to_store_registers
= NULL
;
510 record_beneath_to_xfer_partial
= NULL
;
511 record_beneath_to_insert_breakpoint
= NULL
;
512 record_beneath_to_remove_breakpoint
= NULL
;
514 /* Set the beneath function pointers. */
515 for (t
= current_target
.beneath
; t
!= NULL
; t
= t
->beneath
)
517 if (!record_beneath_to_resume
)
519 record_beneath_to_resume
= t
->to_resume
;
520 record_beneath_to_resume_ops
= t
;
522 if (!record_beneath_to_wait
)
524 record_beneath_to_wait
= t
->to_wait
;
525 record_beneath_to_wait_ops
= t
;
527 if (!record_beneath_to_store_registers
)
529 record_beneath_to_store_registers
= t
->to_store_registers
;
530 record_beneath_to_store_registers_ops
= t
;
532 if (!record_beneath_to_xfer_partial
)
534 record_beneath_to_xfer_partial
= t
->to_xfer_partial
;
535 record_beneath_to_xfer_partial_ops
= t
;
537 if (!record_beneath_to_insert_breakpoint
)
538 record_beneath_to_insert_breakpoint
= t
->to_insert_breakpoint
;
539 if (!record_beneath_to_remove_breakpoint
)
540 record_beneath_to_remove_breakpoint
= t
->to_remove_breakpoint
;
542 if (!record_beneath_to_resume
)
543 error (_("Process record can't get to_resume."));
544 if (!record_beneath_to_wait
)
545 error (_("Process record can't get to_wait."));
546 if (!record_beneath_to_store_registers
)
547 error (_("Process record can't get to_store_registers."));
548 if (!record_beneath_to_xfer_partial
)
549 error (_("Process record can't get to_xfer_partial."));
550 if (!record_beneath_to_insert_breakpoint
)
551 error (_("Process record can't get to_insert_breakpoint."));
552 if (!record_beneath_to_remove_breakpoint
)
553 error (_("Process record can't get to_remove_breakpoint."));
555 push_target (&record_ops
);
559 record_list
= &record_first
;
560 record_list
->next
= NULL
;
564 record_close (int quitting
)
567 fprintf_unfiltered (gdb_stdlog
, "Process record: record_close\n");
569 record_list_release (record_list
);
572 static int record_resume_step
= 0;
573 static int record_resume_error
;
576 record_resume (struct target_ops
*ops
, ptid_t ptid
, int step
,
577 enum target_signal signal
)
579 record_resume_step
= step
;
581 if (!RECORD_IS_REPLAY
)
583 if (do_record_message (get_current_regcache (), signal
))
585 record_resume_error
= 0;
589 record_resume_error
= 1;
592 record_beneath_to_resume (record_beneath_to_resume_ops
, ptid
, 1,
597 static int record_get_sig
= 0;
600 record_sig_handler (int signo
)
603 fprintf_unfiltered (gdb_stdlog
, "Process record: get a signal\n");
605 /* It will break the running inferior in replay mode. */
606 record_resume_step
= 1;
608 /* It will let record_wait set inferior status to get the signal
614 record_wait_cleanups (void *ignore
)
616 if (execution_direction
== EXEC_REVERSE
)
618 if (record_list
->next
)
619 record_list
= record_list
->next
;
622 record_list
= record_list
->prev
;
625 /* In replay mode, this function examines the recorded log and
626 determines where to stop. */
629 record_wait (struct target_ops
*ops
,
630 ptid_t ptid
, struct target_waitstatus
*status
,
633 struct cleanup
*set_cleanups
= record_gdb_operation_disable_set ();
636 fprintf_unfiltered (gdb_stdlog
,
637 "Process record: record_wait "
638 "record_resume_step = %d\n",
641 if (!RECORD_IS_REPLAY
)
643 if (record_resume_error
)
645 /* If record_resume get error, return directly. */
646 status
->kind
= TARGET_WAITKIND_STOPPED
;
647 status
->value
.sig
= TARGET_SIGNAL_ABRT
;
648 return inferior_ptid
;
651 if (record_resume_step
)
653 /* This is a single step. */
654 return record_beneath_to_wait (record_beneath_to_wait_ops
,
655 ptid
, status
, options
);
659 /* This is not a single step. */
665 ret
= record_beneath_to_wait (record_beneath_to_wait_ops
,
666 ptid
, status
, options
);
668 /* Is this a SIGTRAP? */
669 if (status
->kind
== TARGET_WAITKIND_STOPPED
670 && status
->value
.sig
== TARGET_SIGNAL_TRAP
)
672 /* Yes -- check if there is a breakpoint. */
673 registers_changed ();
674 tmp_pc
= regcache_read_pc (get_current_regcache ());
675 if (breakpoint_inserted_here_p (tmp_pc
))
677 /* There is a breakpoint. GDB will want to stop. */
678 CORE_ADDR decr_pc_after_break
=
679 gdbarch_decr_pc_after_break
680 (get_regcache_arch (get_current_regcache ()));
681 if (decr_pc_after_break
)
683 regcache_write_pc (get_thread_regcache (ret
),
684 tmp_pc
+ decr_pc_after_break
);
689 /* There is not a breakpoint, and gdb is not
690 stepping, therefore gdb will not stop.
691 Therefore we will not return to gdb.
692 Record the insn and resume. */
693 if (!do_record_message (get_current_regcache (),
698 record_beneath_to_resume (record_beneath_to_resume_ops
,
705 /* The inferior is broken by a breakpoint or a signal. */
714 struct regcache
*regcache
= get_current_regcache ();
715 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
716 int continue_flag
= 1;
717 int first_record_end
= 1;
718 struct cleanup
*old_cleanups
= make_cleanup (record_wait_cleanups
, 0);
721 status
->kind
= TARGET_WAITKIND_STOPPED
;
723 /* Check breakpoint when forward execute. */
724 if (execution_direction
== EXEC_FORWARD
)
726 tmp_pc
= regcache_read_pc (regcache
);
727 if (breakpoint_inserted_here_p (tmp_pc
))
730 fprintf_unfiltered (gdb_stdlog
,
731 "Process record: break at %s.\n",
732 paddress (gdbarch
, tmp_pc
));
733 if (gdbarch_decr_pc_after_break (gdbarch
)
734 && !record_resume_step
)
735 regcache_write_pc (regcache
,
737 gdbarch_decr_pc_after_break (gdbarch
));
743 signal (SIGINT
, record_sig_handler
);
744 /* If GDB is in terminal_inferior mode, it will not get the signal.
745 And in GDB replay mode, GDB doesn't need to be in terminal_inferior
746 mode, because inferior will not executed.
747 Then set it to terminal_ours to make GDB get the signal. */
748 target_terminal_ours ();
750 /* In EXEC_FORWARD mode, record_list points to the tail of prev
752 if (execution_direction
== EXEC_FORWARD
&& record_list
->next
)
753 record_list
= record_list
->next
;
755 /* Loop over the record_list, looking for the next place to
759 /* Check for beginning and end of log. */
760 if (execution_direction
== EXEC_REVERSE
761 && record_list
== &record_first
)
763 /* Hit beginning of record log in reverse. */
764 status
->kind
= TARGET_WAITKIND_NO_HISTORY
;
767 if (execution_direction
!= EXEC_REVERSE
&& !record_list
->next
)
769 /* Hit end of record log going forward. */
770 status
->kind
= TARGET_WAITKIND_NO_HISTORY
;
774 /* Set ptid, register and memory according to record_list. */
775 if (record_list
->type
== record_reg
)
778 gdb_byte reg
[MAX_REGISTER_SIZE
];
779 if (record_debug
> 1)
780 fprintf_unfiltered (gdb_stdlog
,
781 "Process record: record_reg %s to "
782 "inferior num = %d.\n",
783 host_address_to_string (record_list
),
784 record_list
->u
.reg
.num
);
785 regcache_cooked_read (regcache
, record_list
->u
.reg
.num
, reg
);
786 regcache_cooked_write (regcache
, record_list
->u
.reg
.num
,
787 record_list
->u
.reg
.val
);
788 memcpy (record_list
->u
.reg
.val
, reg
, MAX_REGISTER_SIZE
);
790 else if (record_list
->type
== record_mem
)
793 /* Nothing to do if the entry is flagged not_accessible. */
794 if (!record_list
->u
.mem
.mem_entry_not_accessible
)
796 gdb_byte
*mem
= alloca (record_list
->u
.mem
.len
);
797 if (record_debug
> 1)
798 fprintf_unfiltered (gdb_stdlog
,
799 "Process record: record_mem %s to "
800 "inferior addr = %s len = %d.\n",
801 host_address_to_string (record_list
),
803 record_list
->u
.mem
.addr
),
804 record_list
->u
.mem
.len
);
806 if (target_read_memory (record_list
->u
.mem
.addr
, mem
,
807 record_list
->u
.mem
.len
))
809 if (execution_direction
!= EXEC_REVERSE
)
810 error (_("Process record: error reading memory at "
811 "addr = %s len = %d."),
812 paddress (gdbarch
, record_list
->u
.mem
.addr
),
813 record_list
->u
.mem
.len
);
816 flag entry as not_accessible. */
817 record_list
->u
.mem
.mem_entry_not_accessible
= 1;
821 if (target_write_memory (record_list
->u
.mem
.addr
,
822 record_list
->u
.mem
.val
,
823 record_list
->u
.mem
.len
))
825 if (execution_direction
!= EXEC_REVERSE
)
826 error (_("Process record: error writing memory at "
827 "addr = %s len = %d."),
828 paddress (gdbarch
, record_list
->u
.mem
.addr
),
829 record_list
->u
.mem
.len
);
832 flag entry as not_accessible. */
833 record_list
->u
.mem
.mem_entry_not_accessible
= 1;
837 memcpy (record_list
->u
.mem
.val
, mem
,
838 record_list
->u
.mem
.len
);
845 if (record_debug
> 1)
846 fprintf_unfiltered (gdb_stdlog
,
847 "Process record: record_end %s to "
849 host_address_to_string (record_list
));
851 if (first_record_end
&& execution_direction
== EXEC_REVERSE
)
853 /* When reverse excute, the first record_end is the part of
854 current instruction. */
855 first_record_end
= 0;
859 /* In EXEC_REVERSE mode, this is the record_end of prev
861 In EXEC_FORWARD mode, this is the record_end of current
864 if (record_resume_step
)
866 if (record_debug
> 1)
867 fprintf_unfiltered (gdb_stdlog
,
868 "Process record: step.\n");
872 /* check breakpoint */
873 tmp_pc
= regcache_read_pc (regcache
);
874 if (breakpoint_inserted_here_p (tmp_pc
))
877 fprintf_unfiltered (gdb_stdlog
,
878 "Process record: break "
880 paddress (gdbarch
, tmp_pc
));
881 if (gdbarch_decr_pc_after_break (gdbarch
)
882 && execution_direction
== EXEC_FORWARD
883 && !record_resume_step
)
884 regcache_write_pc (regcache
,
886 gdbarch_decr_pc_after_break (gdbarch
));
889 /* Check target signal */
890 if (record_list
->u
.end
.sigval
!= TARGET_SIGNAL_0
)
891 /* FIXME: better way to check */
898 if (execution_direction
== EXEC_REVERSE
)
900 if (record_list
->prev
)
901 record_list
= record_list
->prev
;
905 if (record_list
->next
)
906 record_list
= record_list
->next
;
910 while (continue_flag
);
912 signal (SIGINT
, handle_sigint
);
916 status
->value
.sig
= TARGET_SIGNAL_INT
;
917 else if (record_list
->u
.end
.sigval
!= TARGET_SIGNAL_0
)
918 /* FIXME: better way to check */
919 status
->value
.sig
= record_list
->u
.end
.sigval
;
921 status
->value
.sig
= TARGET_SIGNAL_TRAP
;
923 discard_cleanups (old_cleanups
);
926 do_cleanups (set_cleanups
);
927 return inferior_ptid
;
931 record_disconnect (struct target_ops
*target
, char *args
, int from_tty
)
934 fprintf_unfiltered (gdb_stdlog
, "Process record: record_disconnect\n");
936 unpush_target (&record_ops
);
937 target_disconnect (args
, from_tty
);
941 record_detach (struct target_ops
*ops
, char *args
, int from_tty
)
944 fprintf_unfiltered (gdb_stdlog
, "Process record: record_detach\n");
946 unpush_target (&record_ops
);
947 target_detach (args
, from_tty
);
951 record_mourn_inferior (struct target_ops
*ops
)
954 fprintf_unfiltered (gdb_stdlog
, "Process record: "
955 "record_mourn_inferior\n");
957 unpush_target (&record_ops
);
958 target_mourn_inferior ();
961 /* Close process record target before killing the inferior process. */
964 record_kill (struct target_ops
*ops
)
967 fprintf_unfiltered (gdb_stdlog
, "Process record: record_kill\n");
969 unpush_target (&record_ops
);
973 /* Record registers change (by user or by GDB) to list as an instruction. */
976 record_registers_change (struct regcache
*regcache
, int regnum
)
978 /* Check record_insn_num. */
979 record_check_insn_num (0);
981 record_arch_list_head
= NULL
;
982 record_arch_list_tail
= NULL
;
987 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
989 if (record_arch_list_add_reg (regcache
, i
))
991 record_list_release (record_arch_list_tail
);
992 error (_("Process record: failed to record execution log."));
998 if (record_arch_list_add_reg (regcache
, regnum
))
1000 record_list_release (record_arch_list_tail
);
1001 error (_("Process record: failed to record execution log."));
1004 if (record_arch_list_add_end ())
1006 record_list_release (record_arch_list_tail
);
1007 error (_("Process record: failed to record execution log."));
1009 record_list
->next
= record_arch_list_head
;
1010 record_arch_list_head
->prev
= record_list
;
1011 record_list
= record_arch_list_tail
;
1013 if (record_insn_num
== record_insn_max_num
&& record_insn_max_num
)
1014 record_list_release_first ();
1020 record_store_registers (struct target_ops
*ops
, struct regcache
*regcache
,
1023 if (!record_gdb_operation_disable
)
1025 if (RECORD_IS_REPLAY
)
1029 /* Let user choose if he wants to write register or not. */
1032 nquery (_("Because GDB is in replay mode, changing the "
1033 "value of a register will make the execution "
1034 "log unusable from this point onward. "
1035 "Change all registers?"));
1038 nquery (_("Because GDB is in replay mode, changing the value "
1039 "of a register will make the execution log unusable "
1040 "from this point onward. Change register %s?"),
1041 gdbarch_register_name (get_regcache_arch (regcache
),
1046 /* Invalidate the value of regcache that was set in function
1047 "regcache_raw_write". */
1052 i
< gdbarch_num_regs (get_regcache_arch (regcache
));
1054 regcache_invalidate (regcache
, i
);
1057 regcache_invalidate (regcache
, regno
);
1059 error (_("Process record canceled the operation."));
1062 /* Destroy the record from here forward. */
1063 record_list_release_next ();
1066 record_registers_change (regcache
, regno
);
1068 record_beneath_to_store_registers (record_beneath_to_store_registers_ops
,
1072 /* Behavior is conditional on RECORD_IS_REPLAY.
1073 In replay mode, we cannot write memory unles we are willing to
1074 invalidate the record/replay log from this point forward. */
1077 record_xfer_partial (struct target_ops
*ops
, enum target_object object
,
1078 const char *annex
, gdb_byte
*readbuf
,
1079 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
1081 if (!record_gdb_operation_disable
1082 && (object
== TARGET_OBJECT_MEMORY
1083 || object
== TARGET_OBJECT_RAW_MEMORY
) && writebuf
)
1085 if (RECORD_IS_REPLAY
)
1087 /* Let user choose if he wants to write memory or not. */
1088 if (!nquery (_("Because GDB is in replay mode, writing to memory "
1089 "will make the execution log unusable from this "
1090 "point onward. Write memory at address %s?"),
1091 paddress (target_gdbarch
, offset
)))
1092 error (_("Process record canceled the operation."));
1094 /* Destroy the record from here forward. */
1095 record_list_release_next ();
1098 /* Check record_insn_num */
1099 record_check_insn_num (0);
1101 /* Record registers change to list as an instruction. */
1102 record_arch_list_head
= NULL
;
1103 record_arch_list_tail
= NULL
;
1104 if (record_arch_list_add_mem (offset
, len
))
1106 record_list_release (record_arch_list_tail
);
1108 fprintf_unfiltered (gdb_stdlog
,
1109 _("Process record: failed to record "
1113 if (record_arch_list_add_end ())
1115 record_list_release (record_arch_list_tail
);
1117 fprintf_unfiltered (gdb_stdlog
,
1118 _("Process record: failed to record "
1122 record_list
->next
= record_arch_list_head
;
1123 record_arch_list_head
->prev
= record_list
;
1124 record_list
= record_arch_list_tail
;
1126 if (record_insn_num
== record_insn_max_num
&& record_insn_max_num
)
1127 record_list_release_first ();
1132 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops
,
1133 object
, annex
, readbuf
, writebuf
,
1137 /* Behavior is conditional on RECORD_IS_REPLAY.
1138 We will not actually insert or remove breakpoints when replaying,
1139 nor when recording. */
1142 record_insert_breakpoint (struct gdbarch
*gdbarch
,
1143 struct bp_target_info
*bp_tgt
)
1145 if (!RECORD_IS_REPLAY
)
1147 struct cleanup
*old_cleanups
= record_gdb_operation_disable_set ();
1148 int ret
= record_beneath_to_insert_breakpoint (gdbarch
, bp_tgt
);
1150 do_cleanups (old_cleanups
);
1159 record_remove_breakpoint (struct gdbarch
*gdbarch
,
1160 struct bp_target_info
*bp_tgt
)
1162 if (!RECORD_IS_REPLAY
)
1164 struct cleanup
*old_cleanups
= record_gdb_operation_disable_set ();
1165 int ret
= record_beneath_to_remove_breakpoint (gdbarch
, bp_tgt
);
1167 do_cleanups (old_cleanups
);
1176 record_can_execute_reverse (void)
1182 init_record_ops (void)
1184 record_ops
.to_shortname
= "record";
1185 record_ops
.to_longname
= "Process record and replay target";
1187 "Log program while executing and replay execution from log.";
1188 record_ops
.to_open
= record_open
;
1189 record_ops
.to_close
= record_close
;
1190 record_ops
.to_resume
= record_resume
;
1191 record_ops
.to_wait
= record_wait
;
1192 record_ops
.to_disconnect
= record_disconnect
;
1193 record_ops
.to_detach
= record_detach
;
1194 record_ops
.to_mourn_inferior
= record_mourn_inferior
;
1195 record_ops
.to_kill
= record_kill
;
1196 record_ops
.to_create_inferior
= find_default_create_inferior
;
1197 record_ops
.to_store_registers
= record_store_registers
;
1198 record_ops
.to_xfer_partial
= record_xfer_partial
;
1199 record_ops
.to_insert_breakpoint
= record_insert_breakpoint
;
1200 record_ops
.to_remove_breakpoint
= record_remove_breakpoint
;
1201 record_ops
.to_can_execute_reverse
= record_can_execute_reverse
;
1202 record_ops
.to_stratum
= record_stratum
;
1203 record_ops
.to_magic
= OPS_MAGIC
;
1207 show_record_debug (struct ui_file
*file
, int from_tty
,
1208 struct cmd_list_element
*c
, const char *value
)
1210 fprintf_filtered (file
, _("Debugging of process record target is %s.\n"),
1214 /* Alias for "target record". */
1217 cmd_record_start (char *args
, int from_tty
)
1219 execute_command ("target record", from_tty
);
1222 /* Truncate the record log from the present point
1223 of replay until the end. */
1226 cmd_record_delete (char *args
, int from_tty
)
1228 if (current_target
.to_stratum
== record_stratum
)
1230 if (RECORD_IS_REPLAY
)
1232 if (!from_tty
|| query (_("Delete the log from this point forward "
1233 "and begin to record the running message "
1235 record_list_release_next ();
1238 printf_unfiltered (_("Already at end of record list.\n"));
1242 printf_unfiltered (_("Process record is not started.\n"));
1245 /* Implement the "stoprecord" command. */
1248 cmd_record_stop (char *args
, int from_tty
)
1250 if (current_target
.to_stratum
== record_stratum
)
1252 if (!record_list
|| !from_tty
|| query (_("Delete recorded log and "
1253 "stop recording?")))
1254 unpush_target (&record_ops
);
1257 printf_unfiltered (_("Process record is not started.\n"));
1260 /* Set upper limit of record log size. */
1263 set_record_insn_max_num (char *args
, int from_tty
, struct cmd_list_element
*c
)
1265 if (record_insn_num
> record_insn_max_num
&& record_insn_max_num
)
1267 printf_unfiltered (_("Record instructions number is bigger than "
1268 "record instructions max number. Auto delete "
1269 "the first ones?\n"));
1271 while (record_insn_num
> record_insn_max_num
)
1272 record_list_release_first ();
1276 /* Print the current index into the record log (number of insns recorded
1280 show_record_insn_number (char *ignore
, int from_tty
)
1282 printf_unfiltered (_("Record instruction number is %d.\n"),
1286 static struct cmd_list_element
*record_cmdlist
, *set_record_cmdlist
,
1287 *show_record_cmdlist
, *info_record_cmdlist
;
1290 set_record_command (char *args
, int from_tty
)
1292 printf_unfiltered (_("\
1293 \"set record\" must be followed by an apporpriate subcommand.\n"));
1294 help_list (set_record_cmdlist
, "set record ", all_commands
, gdb_stdout
);
1298 show_record_command (char *args
, int from_tty
)
1300 cmd_show_list (show_record_cmdlist
, from_tty
, "");
1304 info_record_command (char *args
, int from_tty
)
1306 cmd_show_list (info_record_cmdlist
, from_tty
, "");
1310 _initialize_record (void)
1312 /* Init record_first. */
1313 record_first
.prev
= NULL
;
1314 record_first
.next
= NULL
;
1315 record_first
.type
= record_end
;
1318 add_target (&record_ops
);
1320 add_setshow_zinteger_cmd ("record", no_class
, &record_debug
,
1321 _("Set debugging of record/replay feature."),
1322 _("Show debugging of record/replay feature."),
1323 _("When enabled, debugging output for "
1324 "record/replay feature is displayed."),
1325 NULL
, show_record_debug
, &setdebuglist
,
1328 add_prefix_cmd ("record", class_obscure
, cmd_record_start
,
1329 _("Abbreviated form of \"target record\" command."),
1330 &record_cmdlist
, "record ", 0, &cmdlist
);
1331 add_com_alias ("rec", "record", class_obscure
, 1);
1332 add_prefix_cmd ("record", class_support
, set_record_command
,
1333 _("Set record options"), &set_record_cmdlist
,
1334 "set record ", 0, &setlist
);
1335 add_alias_cmd ("rec", "record", class_obscure
, 1, &setlist
);
1336 add_prefix_cmd ("record", class_support
, show_record_command
,
1337 _("Show record options"), &show_record_cmdlist
,
1338 "show record ", 0, &showlist
);
1339 add_alias_cmd ("rec", "record", class_obscure
, 1, &showlist
);
1340 add_prefix_cmd ("record", class_support
, info_record_command
,
1341 _("Info record options"), &info_record_cmdlist
,
1342 "info record ", 0, &infolist
);
1343 add_alias_cmd ("rec", "record", class_obscure
, 1, &infolist
);
1346 add_cmd ("delete", class_obscure
, cmd_record_delete
,
1347 _("Delete the rest of execution log and start recording it anew."),
1349 add_alias_cmd ("d", "delete", class_obscure
, 1, &record_cmdlist
);
1350 add_alias_cmd ("del", "delete", class_obscure
, 1, &record_cmdlist
);
1352 add_cmd ("stop", class_obscure
, cmd_record_stop
,
1353 _("Stop the record/replay target."),
1355 add_alias_cmd ("s", "stop", class_obscure
, 1, &record_cmdlist
);
1357 /* Record instructions number limit command. */
1358 add_setshow_boolean_cmd ("stop-at-limit", no_class
,
1359 &record_stop_at_limit
, _("\
1360 Set whether record/replay stops when record/replay buffer becomes full."), _("\
1361 Show whether record/replay stops when record/replay buffer becomes full."), _("\
1363 When ON, if the record/replay buffer becomes full, ask user what to do.\n\
1364 When OFF, if the record/replay buffer becomes full,\n\
1365 delete the oldest recorded instruction to make room for each new one."),
1367 &set_record_cmdlist
, &show_record_cmdlist
);
1368 add_setshow_zinteger_cmd ("insn-number-max", no_class
,
1369 &record_insn_max_num
,
1370 _("Set record/replay buffer limit."),
1371 _("Show record/replay buffer limit."), _("\
1372 Set the maximum number of instructions to be stored in the\n\
1373 record/replay buffer. Zero means unlimited. Default is 200000."),
1374 set_record_insn_max_num
,
1375 NULL
, &set_record_cmdlist
, &show_record_cmdlist
);
1376 add_cmd ("insn-number", class_obscure
, show_record_insn_number
,
1377 _("Show the current number of instructions in the "
1378 "record/replay buffer."), &info_record_cmdlist
);