1 /* Process record and replay target for GDB, the GNU debugger.
3 Copyright (C) 2008-2012 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"
26 #include "completer.h"
27 #include "arch-utils.h"
33 #include "event-loop.h"
38 /* This module implements "target record", also known as "process
39 record and replay". This target sits on top of a "normal" target
40 (a target that "has execution"), and provides a record and replay
41 functionality, including reverse debugging.
43 Target record has two modes: recording, and replaying.
45 In record mode, we intercept the to_resume and to_wait methods.
46 Whenever gdb resumes the target, we run the target in single step
47 mode, and we build up an execution log in which, for each executed
48 instruction, we record all changes in memory and register state.
49 This is invisible to the user, to whom it just looks like an
50 ordinary debugging session (except for performance degredation).
52 In replay mode, instead of actually letting the inferior run as a
53 process, we simulate its execution by playing back the recorded
54 execution log. For each instruction in the log, we simulate the
55 instruction's side effects by duplicating the changes that it would
56 have made on memory and registers. */
58 #define DEFAULT_RECORD_INSN_MAX_NUM 200000
60 #define RECORD_IS_REPLAY \
61 (record_list->next || execution_direction == EXEC_REVERSE)
63 #define RECORD_FILE_MAGIC netorder32(0x20091016)
65 /* These are the core structs of the process record functionality.
67 A record_entry is a record of the value change of a register
68 ("record_reg") or a part of memory ("record_mem"). And each
69 instruction must have a struct record_entry ("record_end") that
70 indicates that this is the last struct record_entry of this
73 Each struct record_entry is linked to "record_list" by "prev" and
76 struct record_mem_entry
80 /* Set this flag if target memory for this entry
81 can no longer be accessed. */
82 int mem_entry_not_accessible;
86 gdb_byte buf[sizeof (gdb_byte *)];
90 struct record_reg_entry
97 gdb_byte buf[2 * sizeof (gdb_byte *)];
101 struct record_end_entry
103 enum target_signal sigval;
114 /* This is the data structure that makes up the execution log.
116 The execution log consists of a single linked list of entries
117 of type "struct record_entry". It is doubly linked so that it
118 can be traversed in either direction.
120 The start of the list is anchored by a struct called
121 "record_first". The pointer "record_list" either points to the
122 last entry that was added to the list (in record mode), or to the
123 next entry in the list that will be executed (in replay mode).
125 Each list element (struct record_entry), in addition to next and
126 prev pointers, consists of a union of three entry types: mem, reg,
127 and end. A field called "type" determines which entry type is
128 represented by a given list element.
130 Each instruction that is added to the execution log is represented
131 by a variable number of list elements ('entries'). The instruction
132 will have one "reg" entry for each register that is changed by
133 executing the instruction (including the PC in every case). It
134 will also have one "mem" entry for each memory change. Finally,
135 each instruction will have an "end" entry that separates it from
136 the changes associated with the next instruction. */
140 struct record_entry *prev;
141 struct record_entry *next;
142 enum record_type type;
146 struct record_reg_entry reg;
148 struct record_mem_entry mem;
150 struct record_end_entry end;
154 /* This is the debug switch for process record. */
155 int record_debug = 0;
157 /* If true, query if PREC cannot record memory
158 change of next instruction. */
159 int record_memory_query = 0;
161 struct record_core_buf_entry
163 struct record_core_buf_entry *prev;
164 struct target_section *p;
168 /* Record buf with core target. */
169 static gdb_byte *record_core_regbuf = NULL;
170 static struct target_section *record_core_start;
171 static struct target_section *record_core_end;
172 static struct record_core_buf_entry *record_core_buf_list = NULL;
174 /* The following variables are used for managing the linked list that
175 represents the execution log.
177 record_first is the anchor that holds down the beginning of the list.
179 record_list serves two functions:
180 1) In record mode, it anchors the end of the list.
181 2) In replay mode, it traverses the list and points to
182 the next instruction that must be emulated.
184 record_arch_list_head and record_arch_list_tail are used to manage
185 a separate list, which is used to build up the change elements of
186 the currently executing instruction during record mode. When this
187 instruction has been completely annotated in the "arch list", it
188 will be appended to the main execution log. */
190 static struct record_entry record_first;
191 static struct record_entry *record_list = &record_first;
192 static struct record_entry *record_arch_list_head = NULL;
193 static struct record_entry *record_arch_list_tail = NULL;
195 /* 1 ask user. 0 auto delete the last struct record_entry. */
196 static int record_stop_at_limit = 1;
197 /* Maximum allowed number of insns in execution log. */
198 static unsigned int record_insn_max_num = DEFAULT_RECORD_INSN_MAX_NUM;
199 /* Actual count of insns presently in execution log. */
200 static int record_insn_num = 0;
201 /* Count of insns logged so far (may be larger
202 than count of insns presently in execution log). */
203 static ULONGEST record_insn_count;
205 /* The target_ops of process record. */
206 static struct target_ops record_ops;
207 static struct target_ops record_core_ops;
209 /* The beneath function pointers. */
210 static struct target_ops *record_beneath_to_resume_ops;
211 static void (*record_beneath_to_resume) (struct target_ops *, ptid_t, int,
213 static struct target_ops *record_beneath_to_wait_ops;
214 static ptid_t (*record_beneath_to_wait) (struct target_ops *, ptid_t,
215 struct target_waitstatus *,
217 static struct target_ops *record_beneath_to_store_registers_ops;
218 static void (*record_beneath_to_store_registers) (struct target_ops *,
221 static struct target_ops *record_beneath_to_xfer_partial_ops;
222 static LONGEST (*record_beneath_to_xfer_partial) (struct target_ops *ops,
223 enum target_object object,
226 const gdb_byte *writebuf,
229 static int (*record_beneath_to_insert_breakpoint) (struct gdbarch *,
230 struct bp_target_info *);
231 static int (*record_beneath_to_remove_breakpoint) (struct gdbarch *,
232 struct bp_target_info *);
233 static int (*record_beneath_to_stopped_by_watchpoint) (void);
234 static int (*record_beneath_to_stopped_data_address) (struct target_ops *,
236 static void (*record_beneath_to_async) (void (*) (enum inferior_event_type, void *), void *);
238 /* Alloc and free functions for record_reg, record_mem, and record_end
241 /* Alloc a record_reg record entry. */
243 static inline struct record_entry *
244 record_reg_alloc (struct regcache *regcache, int regnum)
246 struct record_entry *rec;
247 struct gdbarch *gdbarch = get_regcache_arch (regcache);
249 rec = (struct record_entry *) xcalloc (1, sizeof (struct record_entry));
250 rec->type = record_reg;
251 rec->u.reg.num = regnum;
252 rec->u.reg.len = register_size (gdbarch, regnum);
253 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
254 rec->u.reg.u.ptr = (gdb_byte *) xmalloc (rec->u.reg.len);
259 /* Free a record_reg record entry. */
262 record_reg_release (struct record_entry *rec)
264 gdb_assert (rec->type == record_reg);
265 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
266 xfree (rec->u.reg.u.ptr);
270 /* Alloc a record_mem record entry. */
272 static inline struct record_entry *
273 record_mem_alloc (CORE_ADDR addr, int len)
275 struct record_entry *rec;
277 rec = (struct record_entry *) xcalloc (1, sizeof (struct record_entry));
278 rec->type = record_mem;
279 rec->u.mem.addr = addr;
280 rec->u.mem.len = len;
281 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
282 rec->u.mem.u.ptr = (gdb_byte *) xmalloc (len);
287 /* Free a record_mem record entry. */
290 record_mem_release (struct record_entry *rec)
292 gdb_assert (rec->type == record_mem);
293 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
294 xfree (rec->u.mem.u.ptr);
298 /* Alloc a record_end record entry. */
300 static inline struct record_entry *
301 record_end_alloc (void)
303 struct record_entry *rec;
305 rec = (struct record_entry *) xcalloc (1, sizeof (struct record_entry));
306 rec->type = record_end;
311 /* Free a record_end record entry. */
314 record_end_release (struct record_entry *rec)
319 /* Free one record entry, any type.
320 Return entry->type, in case caller wants to know. */
322 static inline enum record_type
323 record_entry_release (struct record_entry *rec)
325 enum record_type type = rec->type;
329 record_reg_release (rec);
332 record_mem_release (rec);
335 record_end_release (rec);
341 /* Free all record entries in list pointed to by REC. */
344 record_list_release (struct record_entry *rec)
355 record_entry_release (rec->next);
358 if (rec == &record_first)
361 record_first.next = NULL;
364 record_entry_release (rec);
367 /* Free all record entries forward of the given list position. */
370 record_list_release_following (struct record_entry *rec)
372 struct record_entry *tmp = rec->next;
378 if (record_entry_release (tmp) == record_end)
387 /* Delete the first instruction from the beginning of the log, to make
388 room for adding a new instruction at the end of the log.
390 Note -- this function does not modify record_insn_num. */
393 record_list_release_first (void)
395 struct record_entry *tmp;
397 if (!record_first.next)
400 /* Loop until a record_end. */
403 /* Cut record_first.next out of the linked list. */
404 tmp = record_first.next;
405 record_first.next = tmp->next;
406 tmp->next->prev = &record_first;
408 /* tmp is now isolated, and can be deleted. */
409 if (record_entry_release (tmp) == record_end)
410 break; /* End loop at first record_end. */
412 if (!record_first.next)
414 gdb_assert (record_insn_num == 1);
415 break; /* End loop when list is empty. */
420 /* Add a struct record_entry to record_arch_list. */
423 record_arch_list_add (struct record_entry *rec)
425 if (record_debug > 1)
426 fprintf_unfiltered (gdb_stdlog,
427 "Process record: record_arch_list_add %s.\n",
428 host_address_to_string (rec));
430 if (record_arch_list_tail)
432 record_arch_list_tail->next = rec;
433 rec->prev = record_arch_list_tail;
434 record_arch_list_tail = rec;
438 record_arch_list_head = rec;
439 record_arch_list_tail = rec;
443 /* Return the value storage location of a record entry. */
444 static inline gdb_byte *
445 record_get_loc (struct record_entry *rec)
449 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
450 return rec->u.mem.u.ptr;
452 return rec->u.mem.u.buf;
454 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
455 return rec->u.reg.u.ptr;
457 return rec->u.reg.u.buf;
460 gdb_assert_not_reached ("unexpected record_entry type");
465 /* Record the value of a register NUM to record_arch_list. */
468 record_arch_list_add_reg (struct regcache *regcache, int regnum)
470 struct record_entry *rec;
472 if (record_debug > 1)
473 fprintf_unfiltered (gdb_stdlog,
474 "Process record: add register num = %d to "
478 rec = record_reg_alloc (regcache, regnum);
480 regcache_raw_read (regcache, regnum, record_get_loc (rec));
482 record_arch_list_add (rec);
487 /* Record the value of a region of memory whose address is ADDR and
488 length is LEN to record_arch_list. */
491 record_arch_list_add_mem (CORE_ADDR addr, int len)
493 struct record_entry *rec;
495 if (record_debug > 1)
496 fprintf_unfiltered (gdb_stdlog,
497 "Process record: add mem addr = %s len = %d to "
499 paddress (target_gdbarch, addr), len);
501 if (!addr) /* FIXME: Why? Some arch must permit it... */
504 rec = record_mem_alloc (addr, len);
506 if (target_read_memory (addr, record_get_loc (rec), len))
509 fprintf_unfiltered (gdb_stdlog,
510 "Process record: error reading memory at "
511 "addr = %s len = %d.\n",
512 paddress (target_gdbarch, addr), len);
513 record_mem_release (rec);
517 record_arch_list_add (rec);
522 /* Add a record_end type struct record_entry to record_arch_list. */
525 record_arch_list_add_end (void)
527 struct record_entry *rec;
529 if (record_debug > 1)
530 fprintf_unfiltered (gdb_stdlog,
531 "Process record: add end to arch list.\n");
533 rec = record_end_alloc ();
534 rec->u.end.sigval = TARGET_SIGNAL_0;
535 rec->u.end.insn_num = ++record_insn_count;
537 record_arch_list_add (rec);
543 record_check_insn_num (int set_terminal)
545 if (record_insn_max_num)
547 gdb_assert (record_insn_num <= record_insn_max_num);
548 if (record_insn_num == record_insn_max_num)
550 /* Ask user what to do. */
551 if (record_stop_at_limit)
556 target_terminal_ours ();
557 q = yquery (_("Do you want to auto delete previous execution "
558 "log entries when record/replay buffer becomes "
559 "full (record stop-at-limit)?"));
561 target_terminal_inferior ();
563 record_stop_at_limit = 0;
565 error (_("Process record: stopped by user."));
572 record_arch_list_cleanups (void *ignore)
574 record_list_release (record_arch_list_tail);
577 /* Before inferior step (when GDB record the running message, inferior
578 only can step), GDB will call this function to record the values to
579 record_list. This function will call gdbarch_process_record to
580 record the running message of inferior and set them to
581 record_arch_list, and add it to record_list. */
584 record_message (struct regcache *regcache, enum target_signal signal)
587 struct gdbarch *gdbarch = get_regcache_arch (regcache);
588 struct cleanup *old_cleanups = make_cleanup (record_arch_list_cleanups, 0);
590 record_arch_list_head = NULL;
591 record_arch_list_tail = NULL;
593 /* Check record_insn_num. */
594 record_check_insn_num (1);
596 /* If gdb sends a signal value to target_resume,
597 save it in the 'end' field of the previous instruction.
599 Maybe process record should record what really happened,
600 rather than what gdb pretends has happened.
602 So if Linux delivered the signal to the child process during
603 the record mode, we will record it and deliver it again in
606 If user says "ignore this signal" during the record mode, then
607 it will be ignored again during the replay mode (no matter if
608 the user says something different, like "deliver this signal"
609 during the replay mode).
611 User should understand that nothing he does during the replay
612 mode will change the behavior of the child. If he tries,
613 then that is a user error.
615 But we should still deliver the signal to gdb during the replay,
616 if we delivered it during the recording. Therefore we should
617 record the signal during record_wait, not record_resume. */
618 if (record_list != &record_first) /* FIXME better way to check */
620 gdb_assert (record_list->type == record_end);
621 record_list->u.end.sigval = signal;
624 if (signal == TARGET_SIGNAL_0
625 || !gdbarch_process_record_signal_p (gdbarch))
626 ret = gdbarch_process_record (gdbarch,
628 regcache_read_pc (regcache));
630 ret = gdbarch_process_record_signal (gdbarch,
635 error (_("Process record: inferior program stopped."));
637 error (_("Process record: failed to record execution log."));
639 discard_cleanups (old_cleanups);
641 record_list->next = record_arch_list_head;
642 record_arch_list_head->prev = record_list;
643 record_list = record_arch_list_tail;
645 if (record_insn_num == record_insn_max_num && record_insn_max_num)
646 record_list_release_first ();
653 struct record_message_args {
654 struct regcache *regcache;
655 enum target_signal signal;
659 record_message_wrapper (void *args)
661 struct record_message_args *record_args = args;
663 return record_message (record_args->regcache, record_args->signal);
667 record_message_wrapper_safe (struct regcache *regcache,
668 enum target_signal signal)
670 struct record_message_args args;
672 args.regcache = regcache;
673 args.signal = signal;
675 return catch_errors (record_message_wrapper, &args, NULL, RETURN_MASK_ALL);
678 /* Set to 1 if record_store_registers and record_xfer_partial
679 doesn't need record. */
681 static int record_gdb_operation_disable = 0;
684 record_gdb_operation_disable_set (void)
686 struct cleanup *old_cleanups = NULL;
689 make_cleanup_restore_integer (&record_gdb_operation_disable);
690 record_gdb_operation_disable = 1;
695 /* Flag set to TRUE for target_stopped_by_watchpoint. */
696 static int record_hw_watchpoint = 0;
698 /* Execute one instruction from the record log. Each instruction in
699 the log will be represented by an arbitrary sequence of register
700 entries and memory entries, followed by an 'end' entry. */
703 record_exec_insn (struct regcache *regcache, struct gdbarch *gdbarch,
704 struct record_entry *entry)
708 case record_reg: /* reg */
710 gdb_byte reg[MAX_REGISTER_SIZE];
712 if (record_debug > 1)
713 fprintf_unfiltered (gdb_stdlog,
714 "Process record: record_reg %s to "
715 "inferior num = %d.\n",
716 host_address_to_string (entry),
719 regcache_cooked_read (regcache, entry->u.reg.num, reg);
720 regcache_cooked_write (regcache, entry->u.reg.num,
721 record_get_loc (entry));
722 memcpy (record_get_loc (entry), reg, entry->u.reg.len);
726 case record_mem: /* mem */
728 /* Nothing to do if the entry is flagged not_accessible. */
729 if (!entry->u.mem.mem_entry_not_accessible)
731 gdb_byte *mem = alloca (entry->u.mem.len);
733 if (record_debug > 1)
734 fprintf_unfiltered (gdb_stdlog,
735 "Process record: record_mem %s to "
736 "inferior addr = %s len = %d.\n",
737 host_address_to_string (entry),
738 paddress (gdbarch, entry->u.mem.addr),
741 if (target_read_memory (entry->u.mem.addr, mem, entry->u.mem.len))
743 entry->u.mem.mem_entry_not_accessible = 1;
745 warning (_("Process record: error reading memory at "
746 "addr = %s len = %d."),
747 paddress (gdbarch, entry->u.mem.addr),
752 if (target_write_memory (entry->u.mem.addr,
753 record_get_loc (entry),
756 entry->u.mem.mem_entry_not_accessible = 1;
758 warning (_("Process record: error writing memory at "
759 "addr = %s len = %d."),
760 paddress (gdbarch, entry->u.mem.addr),
765 memcpy (record_get_loc (entry), mem, entry->u.mem.len);
767 /* We've changed memory --- check if a hardware
768 watchpoint should trap. Note that this
769 presently assumes the target beneath supports
770 continuable watchpoints. On non-continuable
771 watchpoints target, we'll want to check this
772 _before_ actually doing the memory change, and
773 not doing the change at all if the watchpoint
775 if (hardware_watchpoint_inserted_in_range
776 (get_regcache_aspace (regcache),
777 entry->u.mem.addr, entry->u.mem.len))
778 record_hw_watchpoint = 1;
787 static struct target_ops *tmp_to_resume_ops;
788 static void (*tmp_to_resume) (struct target_ops *, ptid_t, int,
790 static struct target_ops *tmp_to_wait_ops;
791 static ptid_t (*tmp_to_wait) (struct target_ops *, ptid_t,
792 struct target_waitstatus *,
794 static struct target_ops *tmp_to_store_registers_ops;
795 static void (*tmp_to_store_registers) (struct target_ops *,
798 static struct target_ops *tmp_to_xfer_partial_ops;
799 static LONGEST (*tmp_to_xfer_partial) (struct target_ops *ops,
800 enum target_object object,
803 const gdb_byte *writebuf,
806 static int (*tmp_to_insert_breakpoint) (struct gdbarch *,
807 struct bp_target_info *);
808 static int (*tmp_to_remove_breakpoint) (struct gdbarch *,
809 struct bp_target_info *);
810 static int (*tmp_to_stopped_by_watchpoint) (void);
811 static int (*tmp_to_stopped_data_address) (struct target_ops *, CORE_ADDR *);
812 static int (*tmp_to_stopped_data_address) (struct target_ops *, CORE_ADDR *);
813 static void (*tmp_to_async) (void (*) (enum inferior_event_type, void *), void *);
815 static void record_restore (void);
817 /* Asynchronous signal handle registered as event loop source for when
818 we have pending events ready to be passed to the core. */
820 static struct async_event_handler *record_async_inferior_event_token;
823 record_async_inferior_event_handler (gdb_client_data data)
825 inferior_event_handler (INF_REG_EVENT, NULL);
828 /* Open the process record target. */
831 record_core_open_1 (char *name, int from_tty)
833 struct regcache *regcache = get_current_regcache ();
834 int regnum = gdbarch_num_regs (get_regcache_arch (regcache));
837 /* Get record_core_regbuf. */
838 target_fetch_registers (regcache, -1);
839 record_core_regbuf = xmalloc (MAX_REGISTER_SIZE * regnum);
840 for (i = 0; i < regnum; i ++)
841 regcache_raw_collect (regcache, i,
842 record_core_regbuf + MAX_REGISTER_SIZE * i);
844 /* Get record_core_start and record_core_end. */
845 if (build_section_table (core_bfd, &record_core_start, &record_core_end))
847 xfree (record_core_regbuf);
848 record_core_regbuf = NULL;
849 error (_("\"%s\": Can't find sections: %s"),
850 bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ()));
853 push_target (&record_core_ops);
857 /* "to_open" target method for 'live' processes. */
860 record_open_1 (char *name, int from_tty)
863 fprintf_unfiltered (gdb_stdlog, "Process record: record_open\n");
866 if (!target_has_execution)
867 error (_("Process record: the program is not being run."));
869 error (_("Process record target can't debug inferior in non-stop mode "
872 if (!gdbarch_process_record_p (target_gdbarch))
873 error (_("Process record: the current architecture doesn't support "
874 "record function."));
877 error (_("Could not find 'to_resume' method on the target stack."));
879 error (_("Could not find 'to_wait' method on the target stack."));
880 if (!tmp_to_store_registers)
881 error (_("Could not find 'to_store_registers' "
882 "method on the target stack."));
883 if (!tmp_to_insert_breakpoint)
884 error (_("Could not find 'to_insert_breakpoint' "
885 "method on the target stack."));
886 if (!tmp_to_remove_breakpoint)
887 error (_("Could not find 'to_remove_breakpoint' "
888 "method on the target stack."));
889 if (!tmp_to_stopped_by_watchpoint)
890 error (_("Could not find 'to_stopped_by_watchpoint' "
891 "method on the target stack."));
892 if (!tmp_to_stopped_data_address)
893 error (_("Could not find 'to_stopped_data_address' "
894 "method on the target stack."));
896 push_target (&record_ops);
899 /* "to_open" target method. Open the process record target. */
902 record_open (char *name, int from_tty)
904 struct target_ops *t;
907 fprintf_unfiltered (gdb_stdlog, "Process record: record_open\n");
909 /* Check if record target is already running. */
910 if (current_target.to_stratum == record_stratum)
911 error (_("Process record target already running. Use \"record stop\" to "
912 "stop record target first."));
914 /* Reset the tmp beneath pointers. */
915 tmp_to_resume_ops = NULL;
916 tmp_to_resume = NULL;
917 tmp_to_wait_ops = NULL;
919 tmp_to_store_registers_ops = NULL;
920 tmp_to_store_registers = NULL;
921 tmp_to_xfer_partial_ops = NULL;
922 tmp_to_xfer_partial = NULL;
923 tmp_to_insert_breakpoint = NULL;
924 tmp_to_remove_breakpoint = NULL;
925 tmp_to_stopped_by_watchpoint = NULL;
926 tmp_to_stopped_data_address = NULL;
929 /* Set the beneath function pointers. */
930 for (t = current_target.beneath; t != NULL; t = t->beneath)
934 tmp_to_resume = t->to_resume;
935 tmp_to_resume_ops = t;
939 tmp_to_wait = t->to_wait;
942 if (!tmp_to_store_registers)
944 tmp_to_store_registers = t->to_store_registers;
945 tmp_to_store_registers_ops = t;
947 if (!tmp_to_xfer_partial)
949 tmp_to_xfer_partial = t->to_xfer_partial;
950 tmp_to_xfer_partial_ops = t;
952 if (!tmp_to_insert_breakpoint)
953 tmp_to_insert_breakpoint = t->to_insert_breakpoint;
954 if (!tmp_to_remove_breakpoint)
955 tmp_to_remove_breakpoint = t->to_remove_breakpoint;
956 if (!tmp_to_stopped_by_watchpoint)
957 tmp_to_stopped_by_watchpoint = t->to_stopped_by_watchpoint;
958 if (!tmp_to_stopped_data_address)
959 tmp_to_stopped_data_address = t->to_stopped_data_address;
961 tmp_to_async = t->to_async;
963 if (!tmp_to_xfer_partial)
964 error (_("Could not find 'to_xfer_partial' method on the target stack."));
968 record_insn_count = 0;
969 record_list = &record_first;
970 record_list->next = NULL;
972 /* Set the tmp beneath pointers to beneath pointers. */
973 record_beneath_to_resume_ops = tmp_to_resume_ops;
974 record_beneath_to_resume = tmp_to_resume;
975 record_beneath_to_wait_ops = tmp_to_wait_ops;
976 record_beneath_to_wait = tmp_to_wait;
977 record_beneath_to_store_registers_ops = tmp_to_store_registers_ops;
978 record_beneath_to_store_registers = tmp_to_store_registers;
979 record_beneath_to_xfer_partial_ops = tmp_to_xfer_partial_ops;
980 record_beneath_to_xfer_partial = tmp_to_xfer_partial;
981 record_beneath_to_insert_breakpoint = tmp_to_insert_breakpoint;
982 record_beneath_to_remove_breakpoint = tmp_to_remove_breakpoint;
983 record_beneath_to_stopped_by_watchpoint = tmp_to_stopped_by_watchpoint;
984 record_beneath_to_stopped_data_address = tmp_to_stopped_data_address;
985 record_beneath_to_async = tmp_to_async;
988 record_core_open_1 (name, from_tty);
990 record_open_1 (name, from_tty);
992 /* Register extra event sources in the event loop. */
993 record_async_inferior_event_token
994 = create_async_event_handler (record_async_inferior_event_handler,
998 /* "to_close" target method. Close the process record target. */
1001 record_close (int quitting)
1003 struct record_core_buf_entry *entry;
1006 fprintf_unfiltered (gdb_stdlog, "Process record: record_close\n");
1008 record_list_release (record_list);
1010 /* Release record_core_regbuf. */
1011 if (record_core_regbuf)
1013 xfree (record_core_regbuf);
1014 record_core_regbuf = NULL;
1017 /* Release record_core_buf_list. */
1018 if (record_core_buf_list)
1020 for (entry = record_core_buf_list->prev; entry; entry = entry->prev)
1022 xfree (record_core_buf_list);
1023 record_core_buf_list = entry;
1025 record_core_buf_list = NULL;
1028 if (record_async_inferior_event_token)
1029 delete_async_event_handler (&record_async_inferior_event_token);
1032 static int record_resume_step = 0;
1034 /* True if we've been resumed, and so each record_wait call should
1035 advance execution. If this is false, record_wait will return a
1036 TARGET_WAITKIND_IGNORE. */
1037 static int record_resumed = 0;
1039 /* The execution direction of the last resume we got. This is
1040 necessary for async mode. Vis (order is not strictly accurate):
1042 1. user has the global execution direction set to forward
1043 2. user does a reverse-step command
1044 3. record_resume is called with global execution direction
1045 temporarily switched to reverse
1046 4. GDB's execution direction is reverted back to forward
1047 5. target record notifies event loop there's an event to handle
1048 6. infrun asks the target which direction was it going, and switches
1049 the global execution direction accordingly (to reverse)
1050 7. infrun polls an event out of the record target, and handles it
1051 8. GDB goes back to the event loop, and goto #4.
1053 static enum exec_direction_kind record_execution_dir = EXEC_FORWARD;
1055 /* "to_resume" target method. Resume the process record target. */
1058 record_resume (struct target_ops *ops, ptid_t ptid, int step,
1059 enum target_signal signal)
1061 record_resume_step = step;
1063 record_execution_dir = execution_direction;
1065 if (!RECORD_IS_REPLAY)
1067 struct gdbarch *gdbarch = target_thread_architecture (ptid);
1069 record_message (get_current_regcache (), signal);
1073 /* This is not hard single step. */
1074 if (!gdbarch_software_single_step_p (gdbarch))
1076 /* This is a normal continue. */
1081 /* This arch support soft sigle step. */
1082 if (single_step_breakpoints_inserted ())
1084 /* This is a soft single step. */
1085 record_resume_step = 1;
1089 /* This is a continue.
1090 Try to insert a soft single step breakpoint. */
1091 if (!gdbarch_software_single_step (gdbarch,
1092 get_current_frame ()))
1094 /* This system don't want use soft single step.
1095 Use hard sigle step. */
1102 record_beneath_to_resume (record_beneath_to_resume_ops,
1103 ptid, step, signal);
1106 /* We are about to start executing the inferior (or simulate it),
1107 let's register it with the event loop. */
1108 if (target_can_async_p ())
1110 target_async (inferior_event_handler, 0);
1111 /* Notify the event loop there's an event to wait for. We do
1112 most of the work in record_wait. */
1113 mark_async_event_handler (record_async_inferior_event_token);
1117 static int record_get_sig = 0;
1119 /* SIGINT signal handler, registered by "to_wait" method. */
1122 record_sig_handler (int signo)
1125 fprintf_unfiltered (gdb_stdlog, "Process record: get a signal\n");
1127 /* It will break the running inferior in replay mode. */
1128 record_resume_step = 1;
1130 /* It will let record_wait set inferior status to get the signal
1136 record_wait_cleanups (void *ignore)
1138 if (execution_direction == EXEC_REVERSE)
1140 if (record_list->next)
1141 record_list = record_list->next;
1144 record_list = record_list->prev;
1147 /* "to_wait" target method for process record target.
1149 In record mode, the target is always run in singlestep mode
1150 (even when gdb says to continue). The to_wait method intercepts
1151 the stop events and determines which ones are to be passed on to
1152 gdb. Most stop events are just singlestep events that gdb is not
1153 to know about, so the to_wait method just records them and keeps
1156 In replay mode, this function emulates the recorded execution log,
1157 one instruction at a time (forward or backward), and determines
1161 record_wait_1 (struct target_ops *ops,
1162 ptid_t ptid, struct target_waitstatus *status,
1165 struct cleanup *set_cleanups = record_gdb_operation_disable_set ();
1168 fprintf_unfiltered (gdb_stdlog,
1169 "Process record: record_wait "
1170 "record_resume_step = %d, record_resumed = %d, direction=%s\n",
1171 record_resume_step, record_resumed,
1172 record_execution_dir == EXEC_FORWARD ? "forward" : "reverse");
1174 if (!record_resumed)
1176 gdb_assert ((options & TARGET_WNOHANG) != 0);
1178 /* No interesting event. */
1179 status->kind = TARGET_WAITKIND_IGNORE;
1180 return minus_one_ptid;
1184 signal (SIGINT, record_sig_handler);
1186 if (!RECORD_IS_REPLAY && ops != &record_core_ops)
1188 if (record_resume_step)
1190 /* This is a single step. */
1191 return record_beneath_to_wait (record_beneath_to_wait_ops,
1192 ptid, status, options);
1196 /* This is not a single step. */
1199 struct gdbarch *gdbarch = target_thread_architecture (inferior_ptid);
1203 ret = record_beneath_to_wait (record_beneath_to_wait_ops,
1204 ptid, status, options);
1205 if (status->kind == TARGET_WAITKIND_IGNORE)
1208 fprintf_unfiltered (gdb_stdlog,
1209 "Process record: record_wait "
1210 "target beneath not done yet\n");
1214 if (single_step_breakpoints_inserted ())
1215 remove_single_step_breakpoints ();
1217 if (record_resume_step)
1220 /* Is this a SIGTRAP? */
1221 if (status->kind == TARGET_WAITKIND_STOPPED
1222 && status->value.sig == TARGET_SIGNAL_TRAP)
1224 struct regcache *regcache;
1225 struct address_space *aspace;
1227 /* Yes -- this is likely our single-step finishing,
1228 but check if there's any reason the core would be
1229 interested in the event. */
1231 registers_changed ();
1232 regcache = get_current_regcache ();
1233 tmp_pc = regcache_read_pc (regcache);
1234 aspace = get_regcache_aspace (regcache);
1236 if (target_stopped_by_watchpoint ())
1238 /* Always interested in watchpoints. */
1240 else if (breakpoint_inserted_here_p (aspace, tmp_pc))
1242 /* There is a breakpoint here. Let the core
1244 if (software_breakpoint_inserted_here_p (aspace, tmp_pc))
1246 struct gdbarch *gdbarch
1247 = get_regcache_arch (regcache);
1248 CORE_ADDR decr_pc_after_break
1249 = gdbarch_decr_pc_after_break (gdbarch);
1250 if (decr_pc_after_break)
1251 regcache_write_pc (regcache,
1252 tmp_pc + decr_pc_after_break);
1257 /* This is a single-step trap. Record the
1258 insn and issue another step.
1259 FIXME: this part can be a random SIGTRAP too.
1260 But GDB cannot handle it. */
1263 if (!record_message_wrapper_safe (regcache,
1266 status->kind = TARGET_WAITKIND_STOPPED;
1267 status->value.sig = TARGET_SIGNAL_0;
1271 if (gdbarch_software_single_step_p (gdbarch))
1273 /* Try to insert the software single step breakpoint.
1274 If insert success, set step to 0. */
1275 set_executing (inferior_ptid, 0);
1276 reinit_frame_cache ();
1277 if (gdbarch_software_single_step (gdbarch,
1278 get_current_frame ()))
1280 set_executing (inferior_ptid, 1);
1284 fprintf_unfiltered (gdb_stdlog,
1285 "Process record: record_wait "
1286 "issuing one more step in the target beneath\n");
1287 record_beneath_to_resume (record_beneath_to_resume_ops,
1294 /* The inferior is broken by a breakpoint or a signal. */
1303 struct regcache *regcache = get_current_regcache ();
1304 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1305 struct address_space *aspace = get_regcache_aspace (regcache);
1306 int continue_flag = 1;
1307 int first_record_end = 1;
1308 struct cleanup *old_cleanups = make_cleanup (record_wait_cleanups, 0);
1311 record_hw_watchpoint = 0;
1312 status->kind = TARGET_WAITKIND_STOPPED;
1314 /* Check breakpoint when forward execute. */
1315 if (execution_direction == EXEC_FORWARD)
1317 tmp_pc = regcache_read_pc (regcache);
1318 if (breakpoint_inserted_here_p (aspace, tmp_pc))
1320 int decr_pc_after_break = gdbarch_decr_pc_after_break (gdbarch);
1323 fprintf_unfiltered (gdb_stdlog,
1324 "Process record: break at %s.\n",
1325 paddress (gdbarch, tmp_pc));
1327 if (decr_pc_after_break
1328 && !record_resume_step
1329 && software_breakpoint_inserted_here_p (aspace, tmp_pc))
1330 regcache_write_pc (regcache,
1331 tmp_pc + decr_pc_after_break);
1336 /* If GDB is in terminal_inferior mode, it will not get the signal.
1337 And in GDB replay mode, GDB doesn't need to be in terminal_inferior
1338 mode, because inferior will not executed.
1339 Then set it to terminal_ours to make GDB get the signal. */
1340 target_terminal_ours ();
1342 /* In EXEC_FORWARD mode, record_list points to the tail of prev
1344 if (execution_direction == EXEC_FORWARD && record_list->next)
1345 record_list = record_list->next;
1347 /* Loop over the record_list, looking for the next place to
1351 /* Check for beginning and end of log. */
1352 if (execution_direction == EXEC_REVERSE
1353 && record_list == &record_first)
1355 /* Hit beginning of record log in reverse. */
1356 status->kind = TARGET_WAITKIND_NO_HISTORY;
1359 if (execution_direction != EXEC_REVERSE && !record_list->next)
1361 /* Hit end of record log going forward. */
1362 status->kind = TARGET_WAITKIND_NO_HISTORY;
1366 record_exec_insn (regcache, gdbarch, record_list);
1368 if (record_list->type == record_end)
1370 if (record_debug > 1)
1371 fprintf_unfiltered (gdb_stdlog,
1372 "Process record: record_end %s to "
1374 host_address_to_string (record_list));
1376 if (first_record_end && execution_direction == EXEC_REVERSE)
1378 /* When reverse excute, the first record_end is the part of
1379 current instruction. */
1380 first_record_end = 0;
1384 /* In EXEC_REVERSE mode, this is the record_end of prev
1386 In EXEC_FORWARD mode, this is the record_end of current
1389 if (record_resume_step)
1391 if (record_debug > 1)
1392 fprintf_unfiltered (gdb_stdlog,
1393 "Process record: step.\n");
1397 /* check breakpoint */
1398 tmp_pc = regcache_read_pc (regcache);
1399 if (breakpoint_inserted_here_p (aspace, tmp_pc))
1401 int decr_pc_after_break
1402 = gdbarch_decr_pc_after_break (gdbarch);
1405 fprintf_unfiltered (gdb_stdlog,
1406 "Process record: break "
1408 paddress (gdbarch, tmp_pc));
1409 if (decr_pc_after_break
1410 && execution_direction == EXEC_FORWARD
1411 && !record_resume_step
1412 && software_breakpoint_inserted_here_p (aspace,
1414 regcache_write_pc (regcache,
1415 tmp_pc + decr_pc_after_break);
1419 if (record_hw_watchpoint)
1422 fprintf_unfiltered (gdb_stdlog,
1423 "Process record: hit hw "
1427 /* Check target signal */
1428 if (record_list->u.end.sigval != TARGET_SIGNAL_0)
1429 /* FIXME: better way to check */
1436 if (execution_direction == EXEC_REVERSE)
1438 if (record_list->prev)
1439 record_list = record_list->prev;
1443 if (record_list->next)
1444 record_list = record_list->next;
1448 while (continue_flag);
1452 status->value.sig = TARGET_SIGNAL_INT;
1453 else if (record_list->u.end.sigval != TARGET_SIGNAL_0)
1454 /* FIXME: better way to check */
1455 status->value.sig = record_list->u.end.sigval;
1457 status->value.sig = TARGET_SIGNAL_TRAP;
1459 discard_cleanups (old_cleanups);
1462 signal (SIGINT, handle_sigint);
1464 do_cleanups (set_cleanups);
1465 return inferior_ptid;
1469 record_wait (struct target_ops *ops,
1470 ptid_t ptid, struct target_waitstatus *status,
1475 return_ptid = record_wait_1 (ops, ptid, status, options);
1476 if (status->kind != TARGET_WAITKIND_IGNORE)
1478 /* We're reporting a stop. Make sure any spurious
1479 target_wait(WNOHANG) doesn't advance the target until the
1480 core wants us resumed again. */
1487 record_stopped_by_watchpoint (void)
1489 if (RECORD_IS_REPLAY)
1490 return record_hw_watchpoint;
1492 return record_beneath_to_stopped_by_watchpoint ();
1496 record_stopped_data_address (struct target_ops *ops, CORE_ADDR *addr_p)
1498 if (RECORD_IS_REPLAY)
1501 return record_beneath_to_stopped_data_address (ops, addr_p);
1504 /* "to_disconnect" method for process record target. */
1507 record_disconnect (struct target_ops *target, char *args, int from_tty)
1510 fprintf_unfiltered (gdb_stdlog, "Process record: record_disconnect\n");
1512 unpush_target (&record_ops);
1513 target_disconnect (args, from_tty);
1516 /* "to_detach" method for process record target. */
1519 record_detach (struct target_ops *ops, char *args, int from_tty)
1522 fprintf_unfiltered (gdb_stdlog, "Process record: record_detach\n");
1524 unpush_target (&record_ops);
1525 target_detach (args, from_tty);
1528 /* "to_mourn_inferior" method for process record target. */
1531 record_mourn_inferior (struct target_ops *ops)
1534 fprintf_unfiltered (gdb_stdlog, "Process record: "
1535 "record_mourn_inferior\n");
1537 unpush_target (&record_ops);
1538 target_mourn_inferior ();
1541 /* Close process record target before killing the inferior process. */
1544 record_kill (struct target_ops *ops)
1547 fprintf_unfiltered (gdb_stdlog, "Process record: record_kill\n");
1549 unpush_target (&record_ops);
1553 /* Record registers change (by user or by GDB) to list as an instruction. */
1556 record_registers_change (struct regcache *regcache, int regnum)
1558 /* Check record_insn_num. */
1559 record_check_insn_num (0);
1561 record_arch_list_head = NULL;
1562 record_arch_list_tail = NULL;
1568 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
1570 if (record_arch_list_add_reg (regcache, i))
1572 record_list_release (record_arch_list_tail);
1573 error (_("Process record: failed to record execution log."));
1579 if (record_arch_list_add_reg (regcache, regnum))
1581 record_list_release (record_arch_list_tail);
1582 error (_("Process record: failed to record execution log."));
1585 if (record_arch_list_add_end ())
1587 record_list_release (record_arch_list_tail);
1588 error (_("Process record: failed to record execution log."));
1590 record_list->next = record_arch_list_head;
1591 record_arch_list_head->prev = record_list;
1592 record_list = record_arch_list_tail;
1594 if (record_insn_num == record_insn_max_num && record_insn_max_num)
1595 record_list_release_first ();
1600 /* "to_store_registers" method for process record target. */
1603 record_store_registers (struct target_ops *ops, struct regcache *regcache,
1606 if (!record_gdb_operation_disable)
1608 if (RECORD_IS_REPLAY)
1612 /* Let user choose if he wants to write register or not. */
1615 query (_("Because GDB is in replay mode, changing the "
1616 "value of a register will make the execution "
1617 "log unusable from this point onward. "
1618 "Change all registers?"));
1621 query (_("Because GDB is in replay mode, changing the value "
1622 "of a register will make the execution log unusable "
1623 "from this point onward. Change register %s?"),
1624 gdbarch_register_name (get_regcache_arch (regcache),
1629 /* Invalidate the value of regcache that was set in function
1630 "regcache_raw_write". */
1636 i < gdbarch_num_regs (get_regcache_arch (regcache));
1638 regcache_invalidate (regcache, i);
1641 regcache_invalidate (regcache, regno);
1643 error (_("Process record canceled the operation."));
1646 /* Destroy the record from here forward. */
1647 record_list_release_following (record_list);
1650 record_registers_change (regcache, regno);
1652 record_beneath_to_store_registers (record_beneath_to_store_registers_ops,
1656 /* "to_xfer_partial" method. Behavior is conditional on RECORD_IS_REPLAY.
1657 In replay mode, we cannot write memory unles we are willing to
1658 invalidate the record/replay log from this point forward. */
1661 record_xfer_partial (struct target_ops *ops, enum target_object object,
1662 const char *annex, gdb_byte *readbuf,
1663 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
1665 if (!record_gdb_operation_disable
1666 && (object == TARGET_OBJECT_MEMORY
1667 || object == TARGET_OBJECT_RAW_MEMORY) && writebuf)
1669 if (RECORD_IS_REPLAY)
1671 /* Let user choose if he wants to write memory or not. */
1672 if (!query (_("Because GDB is in replay mode, writing to memory "
1673 "will make the execution log unusable from this "
1674 "point onward. Write memory at address %s?"),
1675 paddress (target_gdbarch, offset)))
1676 error (_("Process record canceled the operation."));
1678 /* Destroy the record from here forward. */
1679 record_list_release_following (record_list);
1682 /* Check record_insn_num */
1683 record_check_insn_num (0);
1685 /* Record registers change to list as an instruction. */
1686 record_arch_list_head = NULL;
1687 record_arch_list_tail = NULL;
1688 if (record_arch_list_add_mem (offset, len))
1690 record_list_release (record_arch_list_tail);
1692 fprintf_unfiltered (gdb_stdlog,
1693 "Process record: failed to record "
1697 if (record_arch_list_add_end ())
1699 record_list_release (record_arch_list_tail);
1701 fprintf_unfiltered (gdb_stdlog,
1702 "Process record: failed to record "
1706 record_list->next = record_arch_list_head;
1707 record_arch_list_head->prev = record_list;
1708 record_list = record_arch_list_tail;
1710 if (record_insn_num == record_insn_max_num && record_insn_max_num)
1711 record_list_release_first ();
1716 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops,
1717 object, annex, readbuf, writebuf,
1721 /* Behavior is conditional on RECORD_IS_REPLAY.
1722 We will not actually insert or remove breakpoints when replaying,
1723 nor when recording. */
1726 record_insert_breakpoint (struct gdbarch *gdbarch,
1727 struct bp_target_info *bp_tgt)
1729 if (!RECORD_IS_REPLAY)
1731 struct cleanup *old_cleanups = record_gdb_operation_disable_set ();
1732 int ret = record_beneath_to_insert_breakpoint (gdbarch, bp_tgt);
1734 do_cleanups (old_cleanups);
1742 /* "to_remove_breakpoint" method for process record target. */
1745 record_remove_breakpoint (struct gdbarch *gdbarch,
1746 struct bp_target_info *bp_tgt)
1748 if (!RECORD_IS_REPLAY)
1750 struct cleanup *old_cleanups = record_gdb_operation_disable_set ();
1751 int ret = record_beneath_to_remove_breakpoint (gdbarch, bp_tgt);
1753 do_cleanups (old_cleanups);
1761 /* "to_can_execute_reverse" method for process record target. */
1764 record_can_execute_reverse (void)
1769 /* "to_get_bookmark" method for process record and prec over core. */
1772 record_get_bookmark (char *args, int from_tty)
1774 gdb_byte *ret = NULL;
1776 /* Return stringified form of instruction count. */
1777 if (record_list && record_list->type == record_end)
1778 ret = xstrdup (pulongest (record_list->u.end.insn_num));
1783 fprintf_unfiltered (gdb_stdlog,
1784 "record_get_bookmark returns %s\n", ret);
1786 fprintf_unfiltered (gdb_stdlog,
1787 "record_get_bookmark returns NULL\n");
1792 /* The implementation of the command "record goto". */
1793 static void cmd_record_goto (char *, int);
1795 /* "to_goto_bookmark" method for process record and prec over core. */
1798 record_goto_bookmark (gdb_byte *bookmark, int from_tty)
1801 fprintf_unfiltered (gdb_stdlog,
1802 "record_goto_bookmark receives %s\n", bookmark);
1804 if (bookmark[0] == '\'' || bookmark[0] == '\"')
1806 if (bookmark[strlen (bookmark) - 1] != bookmark[0])
1807 error (_("Unbalanced quotes: %s"), bookmark);
1809 /* Strip trailing quote. */
1810 bookmark[strlen (bookmark) - 1] = '\0';
1811 /* Strip leading quote. */
1813 /* Pass along to cmd_record_goto. */
1816 cmd_record_goto ((char *) bookmark, from_tty);
1821 record_async (void (*callback) (enum inferior_event_type event_type,
1822 void *context), void *context)
1824 /* If we're on top of a line target (e.g., linux-nat, remote), then
1825 set it to async mode as well. Will be NULL if we're sitting on
1826 top of the core target, for "record restore". */
1827 if (record_beneath_to_async != NULL)
1828 record_beneath_to_async (callback, context);
1832 record_can_async_p (void)
1834 /* We only enable async when the user specifically asks for it. */
1835 return target_async_permitted;
1839 record_is_async_p (void)
1841 /* We only enable async when the user specifically asks for it. */
1842 return target_async_permitted;
1845 static enum exec_direction_kind
1846 record_execution_direction (void)
1848 return record_execution_dir;
1852 init_record_ops (void)
1854 record_ops.to_shortname = "record";
1855 record_ops.to_longname = "Process record and replay target";
1857 "Log program while executing and replay execution from log.";
1858 record_ops.to_open = record_open;
1859 record_ops.to_close = record_close;
1860 record_ops.to_resume = record_resume;
1861 record_ops.to_wait = record_wait;
1862 record_ops.to_disconnect = record_disconnect;
1863 record_ops.to_detach = record_detach;
1864 record_ops.to_mourn_inferior = record_mourn_inferior;
1865 record_ops.to_kill = record_kill;
1866 record_ops.to_create_inferior = find_default_create_inferior;
1867 record_ops.to_store_registers = record_store_registers;
1868 record_ops.to_xfer_partial = record_xfer_partial;
1869 record_ops.to_insert_breakpoint = record_insert_breakpoint;
1870 record_ops.to_remove_breakpoint = record_remove_breakpoint;
1871 record_ops.to_stopped_by_watchpoint = record_stopped_by_watchpoint;
1872 record_ops.to_stopped_data_address = record_stopped_data_address;
1873 record_ops.to_can_execute_reverse = record_can_execute_reverse;
1874 record_ops.to_stratum = record_stratum;
1875 /* Add bookmark target methods. */
1876 record_ops.to_get_bookmark = record_get_bookmark;
1877 record_ops.to_goto_bookmark = record_goto_bookmark;
1878 record_ops.to_async = record_async;
1879 record_ops.to_can_async_p = record_can_async_p;
1880 record_ops.to_is_async_p = record_is_async_p;
1881 record_ops.to_execution_direction = record_execution_direction;
1882 record_ops.to_magic = OPS_MAGIC;
1885 /* "to_resume" method for prec over corefile. */
1888 record_core_resume (struct target_ops *ops, ptid_t ptid, int step,
1889 enum target_signal signal)
1891 record_resume_step = step;
1893 record_execution_dir = execution_direction;
1895 /* We are about to start executing the inferior (or simulate it),
1896 let's register it with the event loop. */
1897 if (target_can_async_p ())
1899 target_async (inferior_event_handler, 0);
1901 /* Notify the event loop there's an event to wait for. */
1902 mark_async_event_handler (record_async_inferior_event_token);
1906 /* "to_kill" method for prec over corefile. */
1909 record_core_kill (struct target_ops *ops)
1912 fprintf_unfiltered (gdb_stdlog, "Process record: record_core_kill\n");
1914 unpush_target (&record_core_ops);
1917 /* "to_fetch_registers" method for prec over corefile. */
1920 record_core_fetch_registers (struct target_ops *ops,
1921 struct regcache *regcache,
1926 int num = gdbarch_num_regs (get_regcache_arch (regcache));
1929 for (i = 0; i < num; i ++)
1930 regcache_raw_supply (regcache, i,
1931 record_core_regbuf + MAX_REGISTER_SIZE * i);
1934 regcache_raw_supply (regcache, regno,
1935 record_core_regbuf + MAX_REGISTER_SIZE * regno);
1938 /* "to_prepare_to_store" method for prec over corefile. */
1941 record_core_prepare_to_store (struct regcache *regcache)
1945 /* "to_store_registers" method for prec over corefile. */
1948 record_core_store_registers (struct target_ops *ops,
1949 struct regcache *regcache,
1952 if (record_gdb_operation_disable)
1953 regcache_raw_collect (regcache, regno,
1954 record_core_regbuf + MAX_REGISTER_SIZE * regno);
1956 error (_("You can't do that without a process to debug."));
1959 /* "to_xfer_partial" method for prec over corefile. */
1962 record_core_xfer_partial (struct target_ops *ops, enum target_object object,
1963 const char *annex, gdb_byte *readbuf,
1964 const gdb_byte *writebuf, ULONGEST offset,
1967 if (object == TARGET_OBJECT_MEMORY)
1969 if (record_gdb_operation_disable || !writebuf)
1971 struct target_section *p;
1973 for (p = record_core_start; p < record_core_end; p++)
1975 if (offset >= p->addr)
1977 struct record_core_buf_entry *entry;
1978 ULONGEST sec_offset;
1980 if (offset >= p->endaddr)
1983 if (offset + len > p->endaddr)
1984 len = p->endaddr - offset;
1986 sec_offset = offset - p->addr;
1988 /* Read readbuf or write writebuf p, offset, len. */
1990 if (p->the_bfd_section->flags & SEC_CONSTRUCTOR
1991 || (p->the_bfd_section->flags & SEC_HAS_CONTENTS) == 0)
1994 memset (readbuf, 0, len);
1997 /* Get record_core_buf_entry. */
1998 for (entry = record_core_buf_list; entry;
1999 entry = entry->prev)
2006 /* Add a new entry. */
2007 entry = (struct record_core_buf_entry *)
2008 xmalloc (sizeof (struct record_core_buf_entry));
2010 if (!bfd_malloc_and_get_section (p->bfd,
2017 entry->prev = record_core_buf_list;
2018 record_core_buf_list = entry;
2021 memcpy (entry->buf + sec_offset, writebuf,
2027 return record_beneath_to_xfer_partial
2028 (record_beneath_to_xfer_partial_ops,
2029 object, annex, readbuf, writebuf,
2032 memcpy (readbuf, entry->buf + sec_offset,
2043 error (_("You can't do that without a process to debug."));
2046 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops,
2047 object, annex, readbuf, writebuf,
2051 /* "to_insert_breakpoint" method for prec over corefile. */
2054 record_core_insert_breakpoint (struct gdbarch *gdbarch,
2055 struct bp_target_info *bp_tgt)
2060 /* "to_remove_breakpoint" method for prec over corefile. */
2063 record_core_remove_breakpoint (struct gdbarch *gdbarch,
2064 struct bp_target_info *bp_tgt)
2069 /* "to_has_execution" method for prec over corefile. */
2072 record_core_has_execution (struct target_ops *ops, ptid_t the_ptid)
2078 init_record_core_ops (void)
2080 record_core_ops.to_shortname = "record-core";
2081 record_core_ops.to_longname = "Process record and replay target";
2082 record_core_ops.to_doc =
2083 "Log program while executing and replay execution from log.";
2084 record_core_ops.to_open = record_open;
2085 record_core_ops.to_close = record_close;
2086 record_core_ops.to_resume = record_core_resume;
2087 record_core_ops.to_wait = record_wait;
2088 record_core_ops.to_kill = record_core_kill;
2089 record_core_ops.to_fetch_registers = record_core_fetch_registers;
2090 record_core_ops.to_prepare_to_store = record_core_prepare_to_store;
2091 record_core_ops.to_store_registers = record_core_store_registers;
2092 record_core_ops.to_xfer_partial = record_core_xfer_partial;
2093 record_core_ops.to_insert_breakpoint = record_core_insert_breakpoint;
2094 record_core_ops.to_remove_breakpoint = record_core_remove_breakpoint;
2095 record_core_ops.to_stopped_by_watchpoint = record_stopped_by_watchpoint;
2096 record_core_ops.to_stopped_data_address = record_stopped_data_address;
2097 record_core_ops.to_can_execute_reverse = record_can_execute_reverse;
2098 record_core_ops.to_has_execution = record_core_has_execution;
2099 record_core_ops.to_stratum = record_stratum;
2100 /* Add bookmark target methods. */
2101 record_core_ops.to_get_bookmark = record_get_bookmark;
2102 record_core_ops.to_goto_bookmark = record_goto_bookmark;
2103 record_core_ops.to_async = record_async;
2104 record_core_ops.to_can_async_p = record_can_async_p;
2105 record_core_ops.to_is_async_p = record_is_async_p;
2106 record_core_ops.to_execution_direction = record_execution_direction;
2107 record_core_ops.to_magic = OPS_MAGIC;
2110 /* Implement "show record debug" command. */
2113 show_record_debug (struct ui_file *file, int from_tty,
2114 struct cmd_list_element *c, const char *value)
2116 fprintf_filtered (file, _("Debugging of process record target is %s.\n"),
2120 /* Alias for "target record". */
2123 cmd_record_start (char *args, int from_tty)
2125 execute_command ("target record", from_tty);
2128 /* Truncate the record log from the present point
2129 of replay until the end. */
2132 cmd_record_delete (char *args, int from_tty)
2134 if (current_target.to_stratum == record_stratum)
2136 if (RECORD_IS_REPLAY)
2138 if (!from_tty || query (_("Delete the log from this point forward "
2139 "and begin to record the running message "
2141 record_list_release_following (record_list);
2144 printf_unfiltered (_("Already at end of record list.\n"));
2148 printf_unfiltered (_("Process record is not started.\n"));
2151 /* Implement the "stoprecord" or "record stop" command. */
2154 cmd_record_stop (char *args, int from_tty)
2156 if (current_target.to_stratum == record_stratum)
2158 unpush_target (&record_ops);
2159 printf_unfiltered (_("Process record is stopped and all execution "
2160 "logs are deleted.\n"));
2163 printf_unfiltered (_("Process record is not started.\n"));
2166 /* Set upper limit of record log size. */
2169 set_record_insn_max_num (char *args, int from_tty, struct cmd_list_element *c)
2171 if (record_insn_num > record_insn_max_num && record_insn_max_num)
2173 /* Count down record_insn_num while releasing records from list. */
2174 while (record_insn_num > record_insn_max_num)
2176 record_list_release_first ();
2182 static struct cmd_list_element *record_cmdlist, *set_record_cmdlist,
2183 *show_record_cmdlist, *info_record_cmdlist;
2186 set_record_command (char *args, int from_tty)
2188 printf_unfiltered (_("\"set record\" must be followed "
2189 "by an apporpriate subcommand.\n"));
2190 help_list (set_record_cmdlist, "set record ", all_commands, gdb_stdout);
2194 show_record_command (char *args, int from_tty)
2196 cmd_show_list (show_record_cmdlist, from_tty, "");
2199 /* Display some statistics about the execution log. */
2202 info_record_command (char *args, int from_tty)
2204 struct record_entry *p;
2206 if (current_target.to_stratum == record_stratum)
2208 if (RECORD_IS_REPLAY)
2209 printf_filtered (_("Replay mode:\n"));
2211 printf_filtered (_("Record mode:\n"));
2213 /* Find entry for first actual instruction in the log. */
2214 for (p = record_first.next;
2215 p != NULL && p->type != record_end;
2219 /* Do we have a log at all? */
2220 if (p != NULL && p->type == record_end)
2222 /* Display instruction number for first instruction in the log. */
2223 printf_filtered (_("Lowest recorded instruction number is %s.\n"),
2224 pulongest (p->u.end.insn_num));
2226 /* If in replay mode, display where we are in the log. */
2227 if (RECORD_IS_REPLAY)
2228 printf_filtered (_("Current instruction number is %s.\n"),
2229 pulongest (record_list->u.end.insn_num));
2231 /* Display instruction number for last instruction in the log. */
2232 printf_filtered (_("Highest recorded instruction number is %s.\n"),
2233 pulongest (record_insn_count));
2235 /* Display log count. */
2236 printf_filtered (_("Log contains %d instructions.\n"),
2241 printf_filtered (_("No instructions have been logged.\n"));
2246 printf_filtered (_("target record is not active.\n"));
2249 /* Display max log size. */
2250 printf_filtered (_("Max logged instructions is %d.\n"),
2251 record_insn_max_num);
2254 /* Record log save-file format
2255 Version 1 (never released)
2258 4 bytes: magic number htonl(0x20090829).
2259 NOTE: be sure to change whenever this file format changes!
2263 1 byte: record type (record_end, see enum record_type).
2265 1 byte: record type (record_reg, see enum record_type).
2266 8 bytes: register id (network byte order).
2267 MAX_REGISTER_SIZE bytes: register value.
2269 1 byte: record type (record_mem, see enum record_type).
2270 8 bytes: memory length (network byte order).
2271 8 bytes: memory address (network byte order).
2272 n bytes: memory value (n == memory length).
2275 4 bytes: magic number netorder32(0x20091016).
2276 NOTE: be sure to change whenever this file format changes!
2280 1 byte: record type (record_end, see enum record_type).
2282 4 bytes: instruction count
2284 1 byte: record type (record_reg, see enum record_type).
2285 4 bytes: register id (network byte order).
2286 n bytes: register value (n == actual register size).
2287 (eg. 4 bytes for x86 general registers).
2289 1 byte: record type (record_mem, see enum record_type).
2290 4 bytes: memory length (network byte order).
2291 8 bytes: memory address (network byte order).
2292 n bytes: memory value (n == memory length).
2296 /* bfdcore_read -- read bytes from a core file section. */
2299 bfdcore_read (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2301 int ret = bfd_get_section_contents (obfd, osec, buf, *offset, len);
2306 error (_("Failed to read %d bytes from core file %s ('%s')."),
2307 len, bfd_get_filename (obfd),
2308 bfd_errmsg (bfd_get_error ()));
2311 static inline uint64_t
2312 netorder64 (uint64_t input)
2316 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2317 BFD_ENDIAN_BIG, input);
2321 static inline uint32_t
2322 netorder32 (uint32_t input)
2326 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2327 BFD_ENDIAN_BIG, input);
2331 static inline uint16_t
2332 netorder16 (uint16_t input)
2336 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2337 BFD_ENDIAN_BIG, input);
2341 /* Restore the execution log from a core_bfd file. */
2343 record_restore (void)
2346 struct cleanup *old_cleanups;
2347 struct record_entry *rec;
2351 struct regcache *regcache;
2353 /* We restore the execution log from the open core bfd,
2355 if (core_bfd == NULL)
2358 /* "record_restore" can only be called when record list is empty. */
2359 gdb_assert (record_first.next == NULL);
2362 fprintf_unfiltered (gdb_stdlog, "Restoring recording from core file.\n");
2364 /* Now need to find our special note section. */
2365 osec = bfd_get_section_by_name (core_bfd, "null0");
2367 fprintf_unfiltered (gdb_stdlog, "Find precord section %s.\n",
2368 osec ? "succeeded" : "failed");
2371 osec_size = bfd_section_size (core_bfd, osec);
2373 fprintf_unfiltered (gdb_stdlog, "%s", bfd_section_name (core_bfd, osec));
2375 /* Check the magic code. */
2376 bfdcore_read (core_bfd, osec, &magic, sizeof (magic), &bfd_offset);
2377 if (magic != RECORD_FILE_MAGIC)
2378 error (_("Version mis-match or file format error in core file %s."),
2379 bfd_get_filename (core_bfd));
2381 fprintf_unfiltered (gdb_stdlog,
2382 " Reading 4-byte magic cookie "
2383 "RECORD_FILE_MAGIC (0x%s)\n",
2384 phex_nz (netorder32 (magic), 4));
2386 /* Restore the entries in recfd into record_arch_list_head and
2387 record_arch_list_tail. */
2388 record_arch_list_head = NULL;
2389 record_arch_list_tail = NULL;
2390 record_insn_num = 0;
2391 old_cleanups = make_cleanup (record_arch_list_cleanups, 0);
2392 regcache = get_current_regcache ();
2397 uint32_t regnum, len, signal, count;
2400 /* We are finished when offset reaches osec_size. */
2401 if (bfd_offset >= osec_size)
2403 bfdcore_read (core_bfd, osec, &rectype, sizeof (rectype), &bfd_offset);
2407 case record_reg: /* reg */
2408 /* Get register number to regnum. */
2409 bfdcore_read (core_bfd, osec, ®num,
2410 sizeof (regnum), &bfd_offset);
2411 regnum = netorder32 (regnum);
2413 rec = record_reg_alloc (regcache, regnum);
2416 bfdcore_read (core_bfd, osec, record_get_loc (rec),
2417 rec->u.reg.len, &bfd_offset);
2420 fprintf_unfiltered (gdb_stdlog,
2421 " Reading register %d (1 "
2422 "plus %lu plus %d bytes)\n",
2424 (unsigned long) sizeof (regnum),
2428 case record_mem: /* mem */
2430 bfdcore_read (core_bfd, osec, &len,
2431 sizeof (len), &bfd_offset);
2432 len = netorder32 (len);
2435 bfdcore_read (core_bfd, osec, &addr,
2436 sizeof (addr), &bfd_offset);
2437 addr = netorder64 (addr);
2439 rec = record_mem_alloc (addr, len);
2442 bfdcore_read (core_bfd, osec, record_get_loc (rec),
2443 rec->u.mem.len, &bfd_offset);
2446 fprintf_unfiltered (gdb_stdlog,
2447 " Reading memory %s (1 plus "
2448 "%lu plus %lu plus %d bytes)\n",
2449 paddress (get_current_arch (),
2451 (unsigned long) sizeof (addr),
2452 (unsigned long) sizeof (len),
2456 case record_end: /* end */
2457 rec = record_end_alloc ();
2460 /* Get signal value. */
2461 bfdcore_read (core_bfd, osec, &signal,
2462 sizeof (signal), &bfd_offset);
2463 signal = netorder32 (signal);
2464 rec->u.end.sigval = signal;
2466 /* Get insn count. */
2467 bfdcore_read (core_bfd, osec, &count,
2468 sizeof (count), &bfd_offset);
2469 count = netorder32 (count);
2470 rec->u.end.insn_num = count;
2471 record_insn_count = count + 1;
2473 fprintf_unfiltered (gdb_stdlog,
2474 " Reading record_end (1 + "
2475 "%lu + %lu bytes), offset == %s\n",
2476 (unsigned long) sizeof (signal),
2477 (unsigned long) sizeof (count),
2478 paddress (get_current_arch (),
2483 error (_("Bad entry type in core file %s."),
2484 bfd_get_filename (core_bfd));
2488 /* Add rec to record arch list. */
2489 record_arch_list_add (rec);
2492 discard_cleanups (old_cleanups);
2494 /* Add record_arch_list_head to the end of record list. */
2495 record_first.next = record_arch_list_head;
2496 record_arch_list_head->prev = &record_first;
2497 record_arch_list_tail->next = NULL;
2498 record_list = &record_first;
2500 /* Update record_insn_max_num. */
2501 if (record_insn_num > record_insn_max_num)
2503 record_insn_max_num = record_insn_num;
2504 warning (_("Auto increase record/replay buffer limit to %d."),
2505 record_insn_max_num);
2509 printf_filtered (_("Restored records from core file %s.\n"),
2510 bfd_get_filename (core_bfd));
2512 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
2515 /* bfdcore_write -- write bytes into a core file section. */
2518 bfdcore_write (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2520 int ret = bfd_set_section_contents (obfd, osec, buf, *offset, len);
2525 error (_("Failed to write %d bytes to core file %s ('%s')."),
2526 len, bfd_get_filename (obfd),
2527 bfd_errmsg (bfd_get_error ()));
2530 /* Restore the execution log from a file. We use a modified elf
2531 corefile format, with an extra section for our data. */
2534 cmd_record_restore (char *args, int from_tty)
2536 core_file_command (args, from_tty);
2537 record_open (args, from_tty);
2541 record_save_cleanups (void *data)
2544 char *pathname = xstrdup (bfd_get_filename (obfd));
2551 /* Save the execution log to a file. We use a modified elf corefile
2552 format, with an extra section for our data. */
2555 cmd_record_save (char *args, int from_tty)
2557 char *recfilename, recfilename_buffer[40];
2558 struct record_entry *cur_record_list;
2560 struct regcache *regcache;
2561 struct gdbarch *gdbarch;
2562 struct cleanup *old_cleanups;
2563 struct cleanup *set_cleanups;
2566 asection *osec = NULL;
2569 if (strcmp (current_target.to_shortname, "record") != 0)
2570 error (_("This command can only be used with target 'record'.\n"
2571 "Use 'target record' first.\n"));
2577 /* Default recfile name is "gdb_record.PID". */
2578 snprintf (recfilename_buffer, sizeof (recfilename_buffer),
2579 "gdb_record.%d", PIDGET (inferior_ptid));
2580 recfilename = recfilename_buffer;
2583 /* Open the save file. */
2585 fprintf_unfiltered (gdb_stdlog, "Saving execution log to core file '%s'\n",
2588 /* Open the output file. */
2589 obfd = create_gcore_bfd (recfilename);
2590 old_cleanups = make_cleanup (record_save_cleanups, obfd);
2592 /* Save the current record entry to "cur_record_list". */
2593 cur_record_list = record_list;
2595 /* Get the values of regcache and gdbarch. */
2596 regcache = get_current_regcache ();
2597 gdbarch = get_regcache_arch (regcache);
2599 /* Disable the GDB operation record. */
2600 set_cleanups = record_gdb_operation_disable_set ();
2602 /* Reverse execute to the begin of record list. */
2605 /* Check for beginning and end of log. */
2606 if (record_list == &record_first)
2609 record_exec_insn (regcache, gdbarch, record_list);
2611 if (record_list->prev)
2612 record_list = record_list->prev;
2615 /* Compute the size needed for the extra bfd section. */
2616 save_size = 4; /* magic cookie */
2617 for (record_list = record_first.next; record_list;
2618 record_list = record_list->next)
2619 switch (record_list->type)
2622 save_size += 1 + 4 + 4;
2625 save_size += 1 + 4 + record_list->u.reg.len;
2628 save_size += 1 + 4 + 8 + record_list->u.mem.len;
2632 /* Make the new bfd section. */
2633 osec = bfd_make_section_anyway_with_flags (obfd, "precord",
2637 error (_("Failed to create 'precord' section for corefile %s: %s"),
2639 bfd_errmsg (bfd_get_error ()));
2640 bfd_set_section_size (obfd, osec, save_size);
2641 bfd_set_section_vma (obfd, osec, 0);
2642 bfd_set_section_alignment (obfd, osec, 0);
2643 bfd_section_lma (obfd, osec) = 0;
2645 /* Save corefile state. */
2646 write_gcore_file (obfd);
2648 /* Write out the record log. */
2649 /* Write the magic code. */
2650 magic = RECORD_FILE_MAGIC;
2652 fprintf_unfiltered (gdb_stdlog,
2653 " Writing 4-byte magic cookie "
2654 "RECORD_FILE_MAGIC (0x%s)\n",
2655 phex_nz (magic, 4));
2656 bfdcore_write (obfd, osec, &magic, sizeof (magic), &bfd_offset);
2658 /* Save the entries to recfd and forward execute to the end of
2660 record_list = &record_first;
2664 if (record_list != &record_first)
2667 uint32_t regnum, len, signal, count;
2670 type = record_list->type;
2671 bfdcore_write (obfd, osec, &type, sizeof (type), &bfd_offset);
2673 switch (record_list->type)
2675 case record_reg: /* reg */
2677 fprintf_unfiltered (gdb_stdlog,
2678 " Writing register %d (1 "
2679 "plus %lu plus %d bytes)\n",
2680 record_list->u.reg.num,
2681 (unsigned long) sizeof (regnum),
2682 record_list->u.reg.len);
2685 regnum = netorder32 (record_list->u.reg.num);
2686 bfdcore_write (obfd, osec, ®num,
2687 sizeof (regnum), &bfd_offset);
2690 bfdcore_write (obfd, osec, record_get_loc (record_list),
2691 record_list->u.reg.len, &bfd_offset);
2694 case record_mem: /* mem */
2696 fprintf_unfiltered (gdb_stdlog,
2697 " Writing memory %s (1 plus "
2698 "%lu plus %lu plus %d bytes)\n",
2700 record_list->u.mem.addr),
2701 (unsigned long) sizeof (addr),
2702 (unsigned long) sizeof (len),
2703 record_list->u.mem.len);
2706 len = netorder32 (record_list->u.mem.len);
2707 bfdcore_write (obfd, osec, &len, sizeof (len), &bfd_offset);
2709 /* Write memaddr. */
2710 addr = netorder64 (record_list->u.mem.addr);
2711 bfdcore_write (obfd, osec, &addr,
2712 sizeof (addr), &bfd_offset);
2715 bfdcore_write (obfd, osec, record_get_loc (record_list),
2716 record_list->u.mem.len, &bfd_offset);
2721 fprintf_unfiltered (gdb_stdlog,
2722 " Writing record_end (1 + "
2723 "%lu + %lu bytes)\n",
2724 (unsigned long) sizeof (signal),
2725 (unsigned long) sizeof (count));
2726 /* Write signal value. */
2727 signal = netorder32 (record_list->u.end.sigval);
2728 bfdcore_write (obfd, osec, &signal,
2729 sizeof (signal), &bfd_offset);
2731 /* Write insn count. */
2732 count = netorder32 (record_list->u.end.insn_num);
2733 bfdcore_write (obfd, osec, &count,
2734 sizeof (count), &bfd_offset);
2739 /* Execute entry. */
2740 record_exec_insn (regcache, gdbarch, record_list);
2742 if (record_list->next)
2743 record_list = record_list->next;
2748 /* Reverse execute to cur_record_list. */
2751 /* Check for beginning and end of log. */
2752 if (record_list == cur_record_list)
2755 record_exec_insn (regcache, gdbarch, record_list);
2757 if (record_list->prev)
2758 record_list = record_list->prev;
2761 do_cleanups (set_cleanups);
2763 discard_cleanups (old_cleanups);
2766 printf_filtered (_("Saved core file %s with execution log.\n"),
2770 /* record_goto_insn -- rewind the record log (forward or backward,
2771 depending on DIR) to the given entry, changing the program state
2775 record_goto_insn (struct record_entry *entry,
2776 enum exec_direction_kind dir)
2778 struct cleanup *set_cleanups = record_gdb_operation_disable_set ();
2779 struct regcache *regcache = get_current_regcache ();
2780 struct gdbarch *gdbarch = get_regcache_arch (regcache);
2782 /* Assume everything is valid: we will hit the entry,
2783 and we will not hit the end of the recording. */
2785 if (dir == EXEC_FORWARD)
2786 record_list = record_list->next;
2790 record_exec_insn (regcache, gdbarch, record_list);
2791 if (dir == EXEC_REVERSE)
2792 record_list = record_list->prev;
2794 record_list = record_list->next;
2795 } while (record_list != entry);
2796 do_cleanups (set_cleanups);
2799 /* "record goto" command. Argument is an instruction number,
2800 as given by "info record".
2802 Rewinds the recording (forward or backward) to the given instruction. */
2805 cmd_record_goto (char *arg, int from_tty)
2807 struct record_entry *p = NULL;
2808 ULONGEST target_insn = 0;
2810 if (arg == NULL || *arg == '\0')
2811 error (_("Command requires an argument (insn number to go to)."));
2813 if (strncmp (arg, "start", strlen ("start")) == 0
2814 || strncmp (arg, "begin", strlen ("begin")) == 0)
2816 /* Special case. Find first insn. */
2817 for (p = &record_first; p != NULL; p = p->next)
2818 if (p->type == record_end)
2821 target_insn = p->u.end.insn_num;
2823 else if (strncmp (arg, "end", strlen ("end")) == 0)
2825 /* Special case. Find last insn. */
2826 for (p = record_list; p->next != NULL; p = p->next)
2828 for (; p!= NULL; p = p->prev)
2829 if (p->type == record_end)
2832 target_insn = p->u.end.insn_num;
2836 /* General case. Find designated insn. */
2837 target_insn = parse_and_eval_long (arg);
2839 for (p = &record_first; p != NULL; p = p->next)
2840 if (p->type == record_end && p->u.end.insn_num == target_insn)
2845 error (_("Target insn '%s' not found."), arg);
2846 else if (p == record_list)
2847 error (_("Already at insn '%s'."), arg);
2848 else if (p->u.end.insn_num > record_list->u.end.insn_num)
2850 printf_filtered (_("Go forward to insn number %s\n"),
2851 pulongest (target_insn));
2852 record_goto_insn (p, EXEC_FORWARD);
2856 printf_filtered (_("Go backward to insn number %s\n"),
2857 pulongest (target_insn));
2858 record_goto_insn (p, EXEC_REVERSE);
2860 registers_changed ();
2861 reinit_frame_cache ();
2862 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
2866 _initialize_record (void)
2868 struct cmd_list_element *c;
2870 /* Init record_first. */
2871 record_first.prev = NULL;
2872 record_first.next = NULL;
2873 record_first.type = record_end;
2876 add_target (&record_ops);
2877 init_record_core_ops ();
2878 add_target (&record_core_ops);
2880 add_setshow_zinteger_cmd ("record", no_class, &record_debug,
2881 _("Set debugging of record/replay feature."),
2882 _("Show debugging of record/replay feature."),
2883 _("When enabled, debugging output for "
2884 "record/replay feature is displayed."),
2885 NULL, show_record_debug, &setdebuglist,
2888 c = add_prefix_cmd ("record", class_obscure, cmd_record_start,
2889 _("Abbreviated form of \"target record\" command."),
2890 &record_cmdlist, "record ", 0, &cmdlist);
2891 set_cmd_completer (c, filename_completer);
2893 add_com_alias ("rec", "record", class_obscure, 1);
2894 add_prefix_cmd ("record", class_support, set_record_command,
2895 _("Set record options"), &set_record_cmdlist,
2896 "set record ", 0, &setlist);
2897 add_alias_cmd ("rec", "record", class_obscure, 1, &setlist);
2898 add_prefix_cmd ("record", class_support, show_record_command,
2899 _("Show record options"), &show_record_cmdlist,
2900 "show record ", 0, &showlist);
2901 add_alias_cmd ("rec", "record", class_obscure, 1, &showlist);
2902 add_prefix_cmd ("record", class_support, info_record_command,
2903 _("Info record options"), &info_record_cmdlist,
2904 "info record ", 0, &infolist);
2905 add_alias_cmd ("rec", "record", class_obscure, 1, &infolist);
2907 c = add_cmd ("save", class_obscure, cmd_record_save,
2908 _("Save the execution log to a file.\n\
2909 Argument is optional filename.\n\
2910 Default filename is 'gdb_record.<process_id>'."),
2912 set_cmd_completer (c, filename_completer);
2914 c = add_cmd ("restore", class_obscure, cmd_record_restore,
2915 _("Restore the execution log from a file.\n\
2916 Argument is filename. File must be created with 'record save'."),
2918 set_cmd_completer (c, filename_completer);
2920 add_cmd ("delete", class_obscure, cmd_record_delete,
2921 _("Delete the rest of execution log and start recording it anew."),
2923 add_alias_cmd ("d", "delete", class_obscure, 1, &record_cmdlist);
2924 add_alias_cmd ("del", "delete", class_obscure, 1, &record_cmdlist);
2926 add_cmd ("stop", class_obscure, cmd_record_stop,
2927 _("Stop the record/replay target."),
2929 add_alias_cmd ("s", "stop", class_obscure, 1, &record_cmdlist);
2931 /* Record instructions number limit command. */
2932 add_setshow_boolean_cmd ("stop-at-limit", no_class,
2933 &record_stop_at_limit, _("\
2934 Set whether record/replay stops when record/replay buffer becomes full."), _("\
2935 Show whether record/replay stops when record/replay buffer becomes full."),
2936 _("Default is ON.\n\
2937 When ON, if the record/replay buffer becomes full, ask user what to do.\n\
2938 When OFF, if the record/replay buffer becomes full,\n\
2939 delete the oldest recorded instruction to make room for each new one."),
2941 &set_record_cmdlist, &show_record_cmdlist);
2942 add_setshow_uinteger_cmd ("insn-number-max", no_class,
2943 &record_insn_max_num,
2944 _("Set record/replay buffer limit."),
2945 _("Show record/replay buffer limit."), _("\
2946 Set the maximum number of instructions to be stored in the\n\
2947 record/replay buffer. Zero means unlimited. Default is 200000."),
2948 set_record_insn_max_num,
2949 NULL, &set_record_cmdlist, &show_record_cmdlist);
2951 add_cmd ("goto", class_obscure, cmd_record_goto, _("\
2952 Restore the program to its state at instruction number N.\n\
2953 Argument is instruction number, as shown by 'info record'."),
2956 add_setshow_boolean_cmd ("memory-query", no_class,
2957 &record_memory_query, _("\
2958 Set whether query if PREC cannot record memory change of next instruction."),
2960 Show whether query if PREC cannot record memory change of next instruction."),
2963 When ON, query if PREC cannot record memory change of next instruction."),
2965 &set_record_cmdlist, &show_record_cmdlist);