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 unsigned 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 *);
132 /* Alloc and free functions for record_reg, record_mem, and record_end
135 /* Alloc a record_reg record entry. */
137 static inline struct record_entry *
138 record_reg_alloc (struct regcache *regcache, int regnum)
140 struct record_entry *rec;
141 struct gdbarch *gdbarch = get_regcache_arch (regcache);
143 rec = (struct record_entry *) xcalloc (1, sizeof (struct record_entry));
144 rec->type = record_reg;
145 rec->u.reg.num = regnum;
146 rec->u.reg.val = (gdb_byte *) xmalloc (MAX_REGISTER_SIZE);
151 /* Free a record_reg record entry. */
154 record_reg_release (struct record_entry *rec)
156 gdb_assert (rec->type == record_reg);
157 xfree (rec->u.reg.val);
161 /* Alloc a record_mem record entry. */
163 static inline struct record_entry *
164 record_mem_alloc (CORE_ADDR addr, int len)
166 struct record_entry *rec;
168 rec = (struct record_entry *) xcalloc (1, sizeof (struct record_entry));
169 rec->type = record_mem;
170 rec->u.mem.addr = addr;
171 rec->u.mem.len = len;
172 rec->u.mem.val = (gdb_byte *) xmalloc (len);
177 /* Free a record_mem record entry. */
180 record_mem_release (struct record_entry *rec)
182 gdb_assert (rec->type == record_mem);
183 xfree (rec->u.mem.val);
187 /* Alloc a record_end record entry. */
189 static inline struct record_entry *
190 record_end_alloc (void)
192 struct record_entry *rec;
194 rec = (struct record_entry *) xcalloc (1, sizeof (struct record_entry));
195 rec->type = record_end;
200 /* Free a record_end record entry. */
203 record_end_release (struct record_entry *rec)
208 /* Free one record entry, any type.
209 Return entry->type, in case caller wants to know. */
211 static inline enum record_type
212 record_entry_release (struct record_entry *rec)
214 enum record_type type = rec->type;
218 record_reg_release (rec);
221 record_mem_release (rec);
224 record_end_release (rec);
230 /* Free all record entries in list pointed to by REC. */
233 record_list_release (struct record_entry *rec)
244 record_entry_release (rec->next);
247 if (rec == &record_first)
250 record_first.next = NULL;
253 record_entry_release (rec);
256 /* Free all record entries forward of the given list position. */
259 record_list_release_following (struct record_entry *rec)
261 struct record_entry *tmp = rec->next;
267 if (record_entry_release (tmp) == record_end)
273 /* Delete the first instruction from the beginning of the log, to make
274 room for adding a new instruction at the end of the log.
276 Note -- this function does not modify record_insn_num. */
279 record_list_release_first (void)
281 struct record_entry *tmp;
283 if (!record_first.next)
286 /* Loop until a record_end. */
289 /* Cut record_first.next out of the linked list. */
290 tmp = record_first.next;
291 record_first.next = tmp->next;
292 tmp->next->prev = &record_first;
294 /* tmp is now isolated, and can be deleted. */
295 if (record_entry_release (tmp) == record_end)
298 break; /* End loop at first record_end. */
301 if (!record_first.next)
303 gdb_assert (record_insn_num == 1);
304 break; /* End loop when list is empty. */
309 /* Add a struct record_entry to record_arch_list. */
312 record_arch_list_add (struct record_entry *rec)
314 if (record_debug > 1)
315 fprintf_unfiltered (gdb_stdlog,
316 "Process record: record_arch_list_add %s.\n",
317 host_address_to_string (rec));
319 if (record_arch_list_tail)
321 record_arch_list_tail->next = rec;
322 rec->prev = record_arch_list_tail;
323 record_arch_list_tail = rec;
327 record_arch_list_head = rec;
328 record_arch_list_tail = rec;
332 /* Record the value of a register REGNUM to record_arch_list. */
335 record_arch_list_add_reg (struct regcache *regcache, int regnum)
337 struct record_entry *rec;
339 if (record_debug > 1)
340 fprintf_unfiltered (gdb_stdlog,
341 "Process record: add register num = %d to "
345 rec = record_reg_alloc (regcache, regnum);
347 regcache_raw_read (regcache, regnum, rec->u.reg.val);
349 record_arch_list_add (rec);
354 /* Record the value of a region of memory whose address is ADDR and
355 length is LEN to record_arch_list. */
358 record_arch_list_add_mem (CORE_ADDR addr, int len)
360 struct record_entry *rec;
362 if (record_debug > 1)
363 fprintf_unfiltered (gdb_stdlog,
364 "Process record: add mem addr = %s len = %d to "
366 paddress (target_gdbarch, addr), len);
368 if (!addr) /* FIXME: Why? Some arch must permit it... */
371 rec = record_mem_alloc (addr, len);
373 if (target_read_memory (addr, rec->u.mem.val, len))
376 fprintf_unfiltered (gdb_stdlog,
377 "Process record: error reading memory at "
378 "addr = %s len = %d.\n",
379 paddress (target_gdbarch, addr), len);
380 record_mem_release (rec);
384 record_arch_list_add (rec);
389 /* Add a record_end type struct record_entry to record_arch_list. */
392 record_arch_list_add_end (void)
394 struct record_entry *rec;
396 if (record_debug > 1)
397 fprintf_unfiltered (gdb_stdlog,
398 "Process record: add end to arch list.\n");
400 rec = record_end_alloc ();
401 rec->u.end.sigval = TARGET_SIGNAL_0;
403 record_arch_list_add (rec);
409 record_check_insn_num (int set_terminal)
411 if (record_insn_max_num)
413 gdb_assert (record_insn_num <= record_insn_max_num);
414 if (record_insn_num == record_insn_max_num)
416 /* Ask user what to do. */
417 if (record_stop_at_limit)
421 target_terminal_ours ();
422 q = yquery (_("Do you want to auto delete previous execution "
423 "log entries when record/replay buffer becomes "
424 "full (record stop-at-limit)?"));
426 target_terminal_inferior ();
428 record_stop_at_limit = 0;
430 error (_("Process record: inferior program stopped."));
436 /* Before inferior step (when GDB record the running message, inferior
437 only can step), GDB will call this function to record the values to
438 record_list. This function will call gdbarch_process_record to
439 record the running message of inferior and set them to
440 record_arch_list, and add it to record_list. */
443 record_message_cleanups (void *ignore)
445 record_list_release (record_arch_list_tail);
448 struct record_message_args {
449 struct regcache *regcache;
450 enum target_signal signal;
454 record_message (void *args)
457 struct record_message_args *myargs = args;
458 struct gdbarch *gdbarch = get_regcache_arch (myargs->regcache);
459 struct cleanup *old_cleanups = make_cleanup (record_message_cleanups, 0);
461 record_arch_list_head = NULL;
462 record_arch_list_tail = NULL;
464 /* Check record_insn_num. */
465 record_check_insn_num (1);
467 /* If gdb sends a signal value to target_resume,
468 save it in the 'end' field of the previous instruction.
470 Maybe process record should record what really happened,
471 rather than what gdb pretends has happened.
473 So if Linux delivered the signal to the child process during
474 the record mode, we will record it and deliver it again in
477 If user says "ignore this signal" during the record mode, then
478 it will be ignored again during the replay mode (no matter if
479 the user says something different, like "deliver this signal"
480 during the replay mode).
482 User should understand that nothing he does during the replay
483 mode will change the behavior of the child. If he tries,
484 then that is a user error.
486 But we should still deliver the signal to gdb during the replay,
487 if we delivered it during the recording. Therefore we should
488 record the signal during record_wait, not record_resume. */
489 if (record_list != &record_first) /* FIXME better way to check */
491 gdb_assert (record_list->type == record_end);
492 record_list->u.end.sigval = myargs->signal;
495 if (myargs->signal == TARGET_SIGNAL_0
496 || !gdbarch_process_record_signal_p (gdbarch))
497 ret = gdbarch_process_record (gdbarch,
499 regcache_read_pc (myargs->regcache));
501 ret = gdbarch_process_record_signal (gdbarch,
506 error (_("Process record: inferior program stopped."));
508 error (_("Process record: failed to record execution log."));
510 discard_cleanups (old_cleanups);
512 record_list->next = record_arch_list_head;
513 record_arch_list_head->prev = record_list;
514 record_list = record_arch_list_tail;
516 if (record_insn_num == record_insn_max_num && record_insn_max_num)
517 record_list_release_first ();
525 do_record_message (struct regcache *regcache,
526 enum target_signal signal)
528 struct record_message_args args;
530 args.regcache = regcache;
531 args.signal = signal;
532 return catch_errors (record_message, &args, NULL, RETURN_MASK_ALL);
535 /* Set to 1 if record_store_registers and record_xfer_partial
536 doesn't need record. */
538 static int record_gdb_operation_disable = 0;
541 record_gdb_operation_disable_set (void)
543 struct cleanup *old_cleanups = NULL;
546 make_cleanup_restore_integer (&record_gdb_operation_disable);
547 record_gdb_operation_disable = 1;
553 record_open (char *name, int from_tty)
555 struct target_ops *t;
558 fprintf_unfiltered (gdb_stdlog, "Process record: record_open\n");
561 if (!target_has_execution)
562 error (_("Process record: the program is not being run."));
564 error (_("Process record target can't debug inferior in non-stop mode "
566 if (target_async_permitted)
567 error (_("Process record target can't debug inferior in asynchronous "
568 "mode (target-async)."));
570 if (!gdbarch_process_record_p (target_gdbarch))
571 error (_("Process record: the current architecture doesn't support "
572 "record function."));
574 /* Check if record target is already running. */
575 if (current_target.to_stratum == record_stratum)
576 error (_("Process record target already running. Use \"record stop\" to "
577 "stop record target first."));
579 /*Reset the beneath function pointers. */
580 record_beneath_to_resume = NULL;
581 record_beneath_to_wait = NULL;
582 record_beneath_to_store_registers = NULL;
583 record_beneath_to_xfer_partial = NULL;
584 record_beneath_to_insert_breakpoint = NULL;
585 record_beneath_to_remove_breakpoint = NULL;
587 /* Set the beneath function pointers. */
588 for (t = current_target.beneath; t != NULL; t = t->beneath)
590 if (!record_beneath_to_resume)
592 record_beneath_to_resume = t->to_resume;
593 record_beneath_to_resume_ops = t;
595 if (!record_beneath_to_wait)
597 record_beneath_to_wait = t->to_wait;
598 record_beneath_to_wait_ops = t;
600 if (!record_beneath_to_store_registers)
602 record_beneath_to_store_registers = t->to_store_registers;
603 record_beneath_to_store_registers_ops = t;
605 if (!record_beneath_to_xfer_partial)
607 record_beneath_to_xfer_partial = t->to_xfer_partial;
608 record_beneath_to_xfer_partial_ops = t;
610 if (!record_beneath_to_insert_breakpoint)
611 record_beneath_to_insert_breakpoint = t->to_insert_breakpoint;
612 if (!record_beneath_to_remove_breakpoint)
613 record_beneath_to_remove_breakpoint = t->to_remove_breakpoint;
615 if (!record_beneath_to_resume)
616 error (_("Process record can't get to_resume."));
617 if (!record_beneath_to_wait)
618 error (_("Process record can't get to_wait."));
619 if (!record_beneath_to_store_registers)
620 error (_("Process record can't get to_store_registers."));
621 if (!record_beneath_to_xfer_partial)
622 error (_("Process record can't get to_xfer_partial."));
623 if (!record_beneath_to_insert_breakpoint)
624 error (_("Process record can't get to_insert_breakpoint."));
625 if (!record_beneath_to_remove_breakpoint)
626 error (_("Process record can't get to_remove_breakpoint."));
628 push_target (&record_ops);
632 record_list = &record_first;
633 record_list->next = NULL;
637 record_close (int quitting)
640 fprintf_unfiltered (gdb_stdlog, "Process record: record_close\n");
642 record_list_release (record_list);
645 static int record_resume_step = 0;
646 static int record_resume_error;
649 record_resume (struct target_ops *ops, ptid_t ptid, int step,
650 enum target_signal signal)
652 record_resume_step = step;
654 if (!RECORD_IS_REPLAY)
656 if (do_record_message (get_current_regcache (), signal))
658 record_resume_error = 0;
662 record_resume_error = 1;
665 record_beneath_to_resume (record_beneath_to_resume_ops, ptid, 1,
670 static int record_get_sig = 0;
673 record_sig_handler (int signo)
676 fprintf_unfiltered (gdb_stdlog, "Process record: get a signal\n");
678 /* It will break the running inferior in replay mode. */
679 record_resume_step = 1;
681 /* It will let record_wait set inferior status to get the signal
687 record_wait_cleanups (void *ignore)
689 if (execution_direction == EXEC_REVERSE)
691 if (record_list->next)
692 record_list = record_list->next;
695 record_list = record_list->prev;
698 /* In replay mode, this function examines the recorded log and
699 determines where to stop. */
702 record_wait (struct target_ops *ops,
703 ptid_t ptid, struct target_waitstatus *status,
706 struct cleanup *set_cleanups = record_gdb_operation_disable_set ();
709 fprintf_unfiltered (gdb_stdlog,
710 "Process record: record_wait "
711 "record_resume_step = %d\n",
714 if (!RECORD_IS_REPLAY)
716 if (record_resume_error)
718 /* If record_resume get error, return directly. */
719 status->kind = TARGET_WAITKIND_STOPPED;
720 status->value.sig = TARGET_SIGNAL_ABRT;
721 return inferior_ptid;
724 if (record_resume_step)
726 /* This is a single step. */
727 return record_beneath_to_wait (record_beneath_to_wait_ops,
728 ptid, status, options);
732 /* This is not a single step. */
738 ret = record_beneath_to_wait (record_beneath_to_wait_ops,
739 ptid, status, options);
741 /* Is this a SIGTRAP? */
742 if (status->kind == TARGET_WAITKIND_STOPPED
743 && status->value.sig == TARGET_SIGNAL_TRAP)
745 /* Yes -- check if there is a breakpoint. */
746 registers_changed ();
747 tmp_pc = regcache_read_pc (get_current_regcache ());
748 if (breakpoint_inserted_here_p (tmp_pc))
750 /* There is a breakpoint. GDB will want to stop. */
751 CORE_ADDR decr_pc_after_break =
752 gdbarch_decr_pc_after_break
753 (get_regcache_arch (get_current_regcache ()));
754 if (decr_pc_after_break)
756 regcache_write_pc (get_thread_regcache (ret),
757 tmp_pc + decr_pc_after_break);
762 /* There is not a breakpoint, and gdb is not
763 stepping, therefore gdb will not stop.
764 Therefore we will not return to gdb.
765 Record the insn and resume. */
766 if (!do_record_message (get_current_regcache (),
771 record_beneath_to_resume (record_beneath_to_resume_ops,
778 /* The inferior is broken by a breakpoint or a signal. */
787 struct regcache *regcache = get_current_regcache ();
788 struct gdbarch *gdbarch = get_regcache_arch (regcache);
789 int continue_flag = 1;
790 int first_record_end = 1;
791 struct cleanup *old_cleanups = make_cleanup (record_wait_cleanups, 0);
794 status->kind = TARGET_WAITKIND_STOPPED;
796 /* Check breakpoint when forward execute. */
797 if (execution_direction == EXEC_FORWARD)
799 tmp_pc = regcache_read_pc (regcache);
800 if (breakpoint_inserted_here_p (tmp_pc))
803 fprintf_unfiltered (gdb_stdlog,
804 "Process record: break at %s.\n",
805 paddress (gdbarch, tmp_pc));
806 if (gdbarch_decr_pc_after_break (gdbarch)
807 && !record_resume_step)
808 regcache_write_pc (regcache,
810 gdbarch_decr_pc_after_break (gdbarch));
816 signal (SIGINT, record_sig_handler);
817 /* If GDB is in terminal_inferior mode, it will not get the signal.
818 And in GDB replay mode, GDB doesn't need to be in terminal_inferior
819 mode, because inferior will not executed.
820 Then set it to terminal_ours to make GDB get the signal. */
821 target_terminal_ours ();
823 /* In EXEC_FORWARD mode, record_list points to the tail of prev
825 if (execution_direction == EXEC_FORWARD && record_list->next)
826 record_list = record_list->next;
828 /* Loop over the record_list, looking for the next place to
832 /* Check for beginning and end of log. */
833 if (execution_direction == EXEC_REVERSE
834 && record_list == &record_first)
836 /* Hit beginning of record log in reverse. */
837 status->kind = TARGET_WAITKIND_NO_HISTORY;
840 if (execution_direction != EXEC_REVERSE && !record_list->next)
842 /* Hit end of record log going forward. */
843 status->kind = TARGET_WAITKIND_NO_HISTORY;
847 /* Set ptid, register and memory according to record_list. */
848 if (record_list->type == record_reg)
851 gdb_byte reg[MAX_REGISTER_SIZE];
852 if (record_debug > 1)
853 fprintf_unfiltered (gdb_stdlog,
854 "Process record: record_reg %s to "
855 "inferior num = %d.\n",
856 host_address_to_string (record_list),
857 record_list->u.reg.num);
858 regcache_cooked_read (regcache, record_list->u.reg.num, reg);
859 regcache_cooked_write (regcache, record_list->u.reg.num,
860 record_list->u.reg.val);
861 memcpy (record_list->u.reg.val, reg, MAX_REGISTER_SIZE);
863 else if (record_list->type == record_mem)
866 /* Nothing to do if the entry is flagged not_accessible. */
867 if (!record_list->u.mem.mem_entry_not_accessible)
869 gdb_byte *mem = alloca (record_list->u.mem.len);
870 if (record_debug > 1)
871 fprintf_unfiltered (gdb_stdlog,
872 "Process record: record_mem %s to "
873 "inferior addr = %s len = %d.\n",
874 host_address_to_string (record_list),
876 record_list->u.mem.addr),
877 record_list->u.mem.len);
879 if (target_read_memory (record_list->u.mem.addr, mem,
880 record_list->u.mem.len))
882 if (execution_direction != EXEC_REVERSE)
883 error (_("Process record: error reading memory at "
884 "addr = %s len = %d."),
885 paddress (gdbarch, record_list->u.mem.addr),
886 record_list->u.mem.len);
889 flag entry as not_accessible. */
890 record_list->u.mem.mem_entry_not_accessible = 1;
894 if (target_write_memory (record_list->u.mem.addr,
895 record_list->u.mem.val,
896 record_list->u.mem.len))
898 if (execution_direction != EXEC_REVERSE)
899 error (_("Process record: error writing memory at "
900 "addr = %s len = %d."),
901 paddress (gdbarch, record_list->u.mem.addr),
902 record_list->u.mem.len);
905 flag entry as not_accessible. */
906 record_list->u.mem.mem_entry_not_accessible = 1;
910 memcpy (record_list->u.mem.val, mem,
911 record_list->u.mem.len);
918 if (record_debug > 1)
919 fprintf_unfiltered (gdb_stdlog,
920 "Process record: record_end %s to "
922 host_address_to_string (record_list));
924 if (first_record_end && execution_direction == EXEC_REVERSE)
926 /* When reverse excute, the first record_end is the part of
927 current instruction. */
928 first_record_end = 0;
932 /* In EXEC_REVERSE mode, this is the record_end of prev
934 In EXEC_FORWARD mode, this is the record_end of current
937 if (record_resume_step)
939 if (record_debug > 1)
940 fprintf_unfiltered (gdb_stdlog,
941 "Process record: step.\n");
945 /* check breakpoint */
946 tmp_pc = regcache_read_pc (regcache);
947 if (breakpoint_inserted_here_p (tmp_pc))
950 fprintf_unfiltered (gdb_stdlog,
951 "Process record: break "
953 paddress (gdbarch, tmp_pc));
954 if (gdbarch_decr_pc_after_break (gdbarch)
955 && execution_direction == EXEC_FORWARD
956 && !record_resume_step)
957 regcache_write_pc (regcache,
959 gdbarch_decr_pc_after_break (gdbarch));
962 /* Check target signal */
963 if (record_list->u.end.sigval != TARGET_SIGNAL_0)
964 /* FIXME: better way to check */
971 if (execution_direction == EXEC_REVERSE)
973 if (record_list->prev)
974 record_list = record_list->prev;
978 if (record_list->next)
979 record_list = record_list->next;
983 while (continue_flag);
985 signal (SIGINT, handle_sigint);
989 status->value.sig = TARGET_SIGNAL_INT;
990 else if (record_list->u.end.sigval != TARGET_SIGNAL_0)
991 /* FIXME: better way to check */
992 status->value.sig = record_list->u.end.sigval;
994 status->value.sig = TARGET_SIGNAL_TRAP;
996 discard_cleanups (old_cleanups);
999 do_cleanups (set_cleanups);
1000 return inferior_ptid;
1004 record_disconnect (struct target_ops *target, char *args, int from_tty)
1007 fprintf_unfiltered (gdb_stdlog, "Process record: record_disconnect\n");
1009 unpush_target (&record_ops);
1010 target_disconnect (args, from_tty);
1014 record_detach (struct target_ops *ops, char *args, int from_tty)
1017 fprintf_unfiltered (gdb_stdlog, "Process record: record_detach\n");
1019 unpush_target (&record_ops);
1020 target_detach (args, from_tty);
1024 record_mourn_inferior (struct target_ops *ops)
1027 fprintf_unfiltered (gdb_stdlog, "Process record: "
1028 "record_mourn_inferior\n");
1030 unpush_target (&record_ops);
1031 target_mourn_inferior ();
1034 /* Close process record target before killing the inferior process. */
1037 record_kill (struct target_ops *ops)
1040 fprintf_unfiltered (gdb_stdlog, "Process record: record_kill\n");
1042 unpush_target (&record_ops);
1046 /* Record registers change (by user or by GDB) to list as an instruction. */
1049 record_registers_change (struct regcache *regcache, int regnum)
1051 /* Check record_insn_num. */
1052 record_check_insn_num (0);
1054 record_arch_list_head = NULL;
1055 record_arch_list_tail = NULL;
1060 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
1062 if (record_arch_list_add_reg (regcache, i))
1064 record_list_release (record_arch_list_tail);
1065 error (_("Process record: failed to record execution log."));
1071 if (record_arch_list_add_reg (regcache, regnum))
1073 record_list_release (record_arch_list_tail);
1074 error (_("Process record: failed to record execution log."));
1077 if (record_arch_list_add_end ())
1079 record_list_release (record_arch_list_tail);
1080 error (_("Process record: failed to record execution log."));
1082 record_list->next = record_arch_list_head;
1083 record_arch_list_head->prev = record_list;
1084 record_list = record_arch_list_tail;
1086 if (record_insn_num == record_insn_max_num && record_insn_max_num)
1087 record_list_release_first ();
1093 record_store_registers (struct target_ops *ops, struct regcache *regcache,
1096 if (!record_gdb_operation_disable)
1098 if (RECORD_IS_REPLAY)
1102 /* Let user choose if he wants to write register or not. */
1105 query (_("Because GDB is in replay mode, changing the "
1106 "value of a register will make the execution "
1107 "log unusable from this point onward. "
1108 "Change all registers?"));
1111 query (_("Because GDB is in replay mode, changing the value "
1112 "of a register will make the execution log unusable "
1113 "from this point onward. Change register %s?"),
1114 gdbarch_register_name (get_regcache_arch (regcache),
1119 /* Invalidate the value of regcache that was set in function
1120 "regcache_raw_write". */
1125 i < gdbarch_num_regs (get_regcache_arch (regcache));
1127 regcache_invalidate (regcache, i);
1130 regcache_invalidate (regcache, regno);
1132 error (_("Process record canceled the operation."));
1135 /* Destroy the record from here forward. */
1136 record_list_release_following (record_list);
1139 record_registers_change (regcache, regno);
1141 record_beneath_to_store_registers (record_beneath_to_store_registers_ops,
1145 /* Behavior is conditional on RECORD_IS_REPLAY.
1146 In replay mode, we cannot write memory unles we are willing to
1147 invalidate the record/replay log from this point forward. */
1150 record_xfer_partial (struct target_ops *ops, enum target_object object,
1151 const char *annex, gdb_byte *readbuf,
1152 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
1154 if (!record_gdb_operation_disable
1155 && (object == TARGET_OBJECT_MEMORY
1156 || object == TARGET_OBJECT_RAW_MEMORY) && writebuf)
1158 if (RECORD_IS_REPLAY)
1160 /* Let user choose if he wants to write memory or not. */
1161 if (!query (_("Because GDB is in replay mode, writing to memory "
1162 "will make the execution log unusable from this "
1163 "point onward. Write memory at address %s?"),
1164 paddress (target_gdbarch, offset)))
1165 error (_("Process record canceled the operation."));
1167 /* Destroy the record from here forward. */
1168 record_list_release_following (record_list);
1171 /* Check record_insn_num */
1172 record_check_insn_num (0);
1174 /* Record registers change to list as an instruction. */
1175 record_arch_list_head = NULL;
1176 record_arch_list_tail = NULL;
1177 if (record_arch_list_add_mem (offset, len))
1179 record_list_release (record_arch_list_tail);
1181 fprintf_unfiltered (gdb_stdlog,
1182 _("Process record: failed to record "
1186 if (record_arch_list_add_end ())
1188 record_list_release (record_arch_list_tail);
1190 fprintf_unfiltered (gdb_stdlog,
1191 _("Process record: failed to record "
1195 record_list->next = record_arch_list_head;
1196 record_arch_list_head->prev = record_list;
1197 record_list = record_arch_list_tail;
1199 if (record_insn_num == record_insn_max_num && record_insn_max_num)
1200 record_list_release_first ();
1205 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops,
1206 object, annex, readbuf, writebuf,
1210 /* Behavior is conditional on RECORD_IS_REPLAY.
1211 We will not actually insert or remove breakpoints when replaying,
1212 nor when recording. */
1215 record_insert_breakpoint (struct gdbarch *gdbarch,
1216 struct bp_target_info *bp_tgt)
1218 if (!RECORD_IS_REPLAY)
1220 struct cleanup *old_cleanups = record_gdb_operation_disable_set ();
1221 int ret = record_beneath_to_insert_breakpoint (gdbarch, bp_tgt);
1223 do_cleanups (old_cleanups);
1232 record_remove_breakpoint (struct gdbarch *gdbarch,
1233 struct bp_target_info *bp_tgt)
1235 if (!RECORD_IS_REPLAY)
1237 struct cleanup *old_cleanups = record_gdb_operation_disable_set ();
1238 int ret = record_beneath_to_remove_breakpoint (gdbarch, bp_tgt);
1240 do_cleanups (old_cleanups);
1249 record_can_execute_reverse (void)
1255 init_record_ops (void)
1257 record_ops.to_shortname = "record";
1258 record_ops.to_longname = "Process record and replay target";
1260 "Log program while executing and replay execution from log.";
1261 record_ops.to_open = record_open;
1262 record_ops.to_close = record_close;
1263 record_ops.to_resume = record_resume;
1264 record_ops.to_wait = record_wait;
1265 record_ops.to_disconnect = record_disconnect;
1266 record_ops.to_detach = record_detach;
1267 record_ops.to_mourn_inferior = record_mourn_inferior;
1268 record_ops.to_kill = record_kill;
1269 record_ops.to_create_inferior = find_default_create_inferior;
1270 record_ops.to_store_registers = record_store_registers;
1271 record_ops.to_xfer_partial = record_xfer_partial;
1272 record_ops.to_insert_breakpoint = record_insert_breakpoint;
1273 record_ops.to_remove_breakpoint = record_remove_breakpoint;
1274 record_ops.to_can_execute_reverse = record_can_execute_reverse;
1275 record_ops.to_stratum = record_stratum;
1276 record_ops.to_magic = OPS_MAGIC;
1280 show_record_debug (struct ui_file *file, int from_tty,
1281 struct cmd_list_element *c, const char *value)
1283 fprintf_filtered (file, _("Debugging of process record target is %s.\n"),
1287 /* Alias for "target record". */
1290 cmd_record_start (char *args, int from_tty)
1292 execute_command ("target record", from_tty);
1295 /* Truncate the record log from the present point
1296 of replay until the end. */
1299 cmd_record_delete (char *args, int from_tty)
1301 if (current_target.to_stratum == record_stratum)
1303 if (RECORD_IS_REPLAY)
1305 if (!from_tty || query (_("Delete the log from this point forward "
1306 "and begin to record the running message "
1308 record_list_release_following (record_list);
1311 printf_unfiltered (_("Already at end of record list.\n"));
1315 printf_unfiltered (_("Process record is not started.\n"));
1318 /* Implement the "stoprecord" command. */
1321 cmd_record_stop (char *args, int from_tty)
1323 if (current_target.to_stratum == record_stratum)
1325 unpush_target (&record_ops);
1326 printf_unfiltered (_("Process record is stoped and all execution "
1327 "log is deleted.\n"));
1330 printf_unfiltered (_("Process record is not started.\n"));
1333 /* Set upper limit of record log size. */
1336 set_record_insn_max_num (char *args, int from_tty, struct cmd_list_element *c)
1338 if (record_insn_num > record_insn_max_num && record_insn_max_num)
1340 /* Count down record_insn_num while releasing records from list. */
1341 while (record_insn_num > record_insn_max_num)
1343 record_list_release_first ();
1349 /* Print the current index into the record log (number of insns recorded
1353 show_record_insn_number (char *ignore, int from_tty)
1355 printf_unfiltered (_("Record instruction number is %d.\n"),
1359 static struct cmd_list_element *record_cmdlist, *set_record_cmdlist,
1360 *show_record_cmdlist, *info_record_cmdlist;
1363 set_record_command (char *args, int from_tty)
1365 printf_unfiltered (_("\
1366 \"set record\" must be followed by an apporpriate subcommand.\n"));
1367 help_list (set_record_cmdlist, "set record ", all_commands, gdb_stdout);
1371 show_record_command (char *args, int from_tty)
1373 cmd_show_list (show_record_cmdlist, from_tty, "");
1377 info_record_command (char *args, int from_tty)
1379 cmd_show_list (info_record_cmdlist, from_tty, "");
1383 _initialize_record (void)
1385 /* Init record_first. */
1386 record_first.prev = NULL;
1387 record_first.next = NULL;
1388 record_first.type = record_end;
1391 add_target (&record_ops);
1393 add_setshow_zinteger_cmd ("record", no_class, &record_debug,
1394 _("Set debugging of record/replay feature."),
1395 _("Show debugging of record/replay feature."),
1396 _("When enabled, debugging output for "
1397 "record/replay feature is displayed."),
1398 NULL, show_record_debug, &setdebuglist,
1401 add_prefix_cmd ("record", class_obscure, cmd_record_start,
1402 _("Abbreviated form of \"target record\" command."),
1403 &record_cmdlist, "record ", 0, &cmdlist);
1404 add_com_alias ("rec", "record", class_obscure, 1);
1405 add_prefix_cmd ("record", class_support, set_record_command,
1406 _("Set record options"), &set_record_cmdlist,
1407 "set record ", 0, &setlist);
1408 add_alias_cmd ("rec", "record", class_obscure, 1, &setlist);
1409 add_prefix_cmd ("record", class_support, show_record_command,
1410 _("Show record options"), &show_record_cmdlist,
1411 "show record ", 0, &showlist);
1412 add_alias_cmd ("rec", "record", class_obscure, 1, &showlist);
1413 add_prefix_cmd ("record", class_support, info_record_command,
1414 _("Info record options"), &info_record_cmdlist,
1415 "info record ", 0, &infolist);
1416 add_alias_cmd ("rec", "record", class_obscure, 1, &infolist);
1419 add_cmd ("delete", class_obscure, cmd_record_delete,
1420 _("Delete the rest of execution log and start recording it anew."),
1422 add_alias_cmd ("d", "delete", class_obscure, 1, &record_cmdlist);
1423 add_alias_cmd ("del", "delete", class_obscure, 1, &record_cmdlist);
1425 add_cmd ("stop", class_obscure, cmd_record_stop,
1426 _("Stop the record/replay target."),
1428 add_alias_cmd ("s", "stop", class_obscure, 1, &record_cmdlist);
1430 /* Record instructions number limit command. */
1431 add_setshow_boolean_cmd ("stop-at-limit", no_class,
1432 &record_stop_at_limit, _("\
1433 Set whether record/replay stops when record/replay buffer becomes full."), _("\
1434 Show whether record/replay stops when record/replay buffer becomes full."), _("\
1436 When ON, if the record/replay buffer becomes full, ask user what to do.\n\
1437 When OFF, if the record/replay buffer becomes full,\n\
1438 delete the oldest recorded instruction to make room for each new one."),
1440 &set_record_cmdlist, &show_record_cmdlist);
1441 add_setshow_uinteger_cmd ("insn-number-max", no_class,
1442 &record_insn_max_num,
1443 _("Set record/replay buffer limit."),
1444 _("Show record/replay buffer limit."), _("\
1445 Set the maximum number of instructions to be stored in the\n\
1446 record/replay buffer. Zero means unlimited. Default is 200000."),
1447 set_record_insn_max_num,
1448 NULL, &set_record_cmdlist, &show_record_cmdlist);
1449 add_cmd ("insn-number", class_obscure, show_record_insn_number,
1450 _("Show the current number of instructions in the "
1451 "record/replay buffer."), &info_record_cmdlist);