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 gdb_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 = GDB_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 gdb_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 == GDB_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 gdb_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 gdb_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 static void record_init_record_breakpoints (void);
901 /* "to_open" target method. Open the process record target. */
904 record_open (char *name, int from_tty)
906 struct target_ops *t;
909 fprintf_unfiltered (gdb_stdlog, "Process record: record_open\n");
911 /* Check if record target is already running. */
912 if (current_target.to_stratum == record_stratum)
913 error (_("Process record target already running. Use \"record stop\" to "
914 "stop record target first."));
916 /* Reset the tmp beneath pointers. */
917 tmp_to_resume_ops = NULL;
918 tmp_to_resume = NULL;
919 tmp_to_wait_ops = NULL;
921 tmp_to_store_registers_ops = NULL;
922 tmp_to_store_registers = NULL;
923 tmp_to_xfer_partial_ops = NULL;
924 tmp_to_xfer_partial = NULL;
925 tmp_to_insert_breakpoint = NULL;
926 tmp_to_remove_breakpoint = NULL;
927 tmp_to_stopped_by_watchpoint = NULL;
928 tmp_to_stopped_data_address = NULL;
931 /* Set the beneath function pointers. */
932 for (t = current_target.beneath; t != NULL; t = t->beneath)
936 tmp_to_resume = t->to_resume;
937 tmp_to_resume_ops = t;
941 tmp_to_wait = t->to_wait;
944 if (!tmp_to_store_registers)
946 tmp_to_store_registers = t->to_store_registers;
947 tmp_to_store_registers_ops = t;
949 if (!tmp_to_xfer_partial)
951 tmp_to_xfer_partial = t->to_xfer_partial;
952 tmp_to_xfer_partial_ops = t;
954 if (!tmp_to_insert_breakpoint)
955 tmp_to_insert_breakpoint = t->to_insert_breakpoint;
956 if (!tmp_to_remove_breakpoint)
957 tmp_to_remove_breakpoint = t->to_remove_breakpoint;
958 if (!tmp_to_stopped_by_watchpoint)
959 tmp_to_stopped_by_watchpoint = t->to_stopped_by_watchpoint;
960 if (!tmp_to_stopped_data_address)
961 tmp_to_stopped_data_address = t->to_stopped_data_address;
963 tmp_to_async = t->to_async;
965 if (!tmp_to_xfer_partial)
966 error (_("Could not find 'to_xfer_partial' method on the target stack."));
970 record_insn_count = 0;
971 record_list = &record_first;
972 record_list->next = NULL;
974 /* Set the tmp beneath pointers to beneath pointers. */
975 record_beneath_to_resume_ops = tmp_to_resume_ops;
976 record_beneath_to_resume = tmp_to_resume;
977 record_beneath_to_wait_ops = tmp_to_wait_ops;
978 record_beneath_to_wait = tmp_to_wait;
979 record_beneath_to_store_registers_ops = tmp_to_store_registers_ops;
980 record_beneath_to_store_registers = tmp_to_store_registers;
981 record_beneath_to_xfer_partial_ops = tmp_to_xfer_partial_ops;
982 record_beneath_to_xfer_partial = tmp_to_xfer_partial;
983 record_beneath_to_insert_breakpoint = tmp_to_insert_breakpoint;
984 record_beneath_to_remove_breakpoint = tmp_to_remove_breakpoint;
985 record_beneath_to_stopped_by_watchpoint = tmp_to_stopped_by_watchpoint;
986 record_beneath_to_stopped_data_address = tmp_to_stopped_data_address;
987 record_beneath_to_async = tmp_to_async;
990 record_core_open_1 (name, from_tty);
992 record_open_1 (name, from_tty);
994 /* Register extra event sources in the event loop. */
995 record_async_inferior_event_token
996 = create_async_event_handler (record_async_inferior_event_handler,
999 record_init_record_breakpoints ();
1002 /* "to_close" target method. Close the process record target. */
1005 record_close (int quitting)
1007 struct record_core_buf_entry *entry;
1010 fprintf_unfiltered (gdb_stdlog, "Process record: record_close\n");
1012 record_list_release (record_list);
1014 /* Release record_core_regbuf. */
1015 if (record_core_regbuf)
1017 xfree (record_core_regbuf);
1018 record_core_regbuf = NULL;
1021 /* Release record_core_buf_list. */
1022 if (record_core_buf_list)
1024 for (entry = record_core_buf_list->prev; entry; entry = entry->prev)
1026 xfree (record_core_buf_list);
1027 record_core_buf_list = entry;
1029 record_core_buf_list = NULL;
1032 if (record_async_inferior_event_token)
1033 delete_async_event_handler (&record_async_inferior_event_token);
1036 static int record_resume_step = 0;
1038 /* True if we've been resumed, and so each record_wait call should
1039 advance execution. If this is false, record_wait will return a
1040 TARGET_WAITKIND_IGNORE. */
1041 static int record_resumed = 0;
1043 /* The execution direction of the last resume we got. This is
1044 necessary for async mode. Vis (order is not strictly accurate):
1046 1. user has the global execution direction set to forward
1047 2. user does a reverse-step command
1048 3. record_resume is called with global execution direction
1049 temporarily switched to reverse
1050 4. GDB's execution direction is reverted back to forward
1051 5. target record notifies event loop there's an event to handle
1052 6. infrun asks the target which direction was it going, and switches
1053 the global execution direction accordingly (to reverse)
1054 7. infrun polls an event out of the record target, and handles it
1055 8. GDB goes back to the event loop, and goto #4.
1057 static enum exec_direction_kind record_execution_dir = EXEC_FORWARD;
1059 /* "to_resume" target method. Resume the process record target. */
1062 record_resume (struct target_ops *ops, ptid_t ptid, int step,
1063 enum gdb_signal signal)
1065 record_resume_step = step;
1067 record_execution_dir = execution_direction;
1069 if (!RECORD_IS_REPLAY)
1071 struct gdbarch *gdbarch = target_thread_architecture (ptid);
1073 record_message (get_current_regcache (), signal);
1077 /* This is not hard single step. */
1078 if (!gdbarch_software_single_step_p (gdbarch))
1080 /* This is a normal continue. */
1085 /* This arch support soft sigle step. */
1086 if (single_step_breakpoints_inserted ())
1088 /* This is a soft single step. */
1089 record_resume_step = 1;
1093 /* This is a continue.
1094 Try to insert a soft single step breakpoint. */
1095 if (!gdbarch_software_single_step (gdbarch,
1096 get_current_frame ()))
1098 /* This system don't want use soft single step.
1099 Use hard sigle step. */
1106 record_beneath_to_resume (record_beneath_to_resume_ops,
1107 ptid, step, signal);
1110 /* We are about to start executing the inferior (or simulate it),
1111 let's register it with the event loop. */
1112 if (target_can_async_p ())
1114 target_async (inferior_event_handler, 0);
1115 /* Notify the event loop there's an event to wait for. We do
1116 most of the work in record_wait. */
1117 mark_async_event_handler (record_async_inferior_event_token);
1121 static int record_get_sig = 0;
1123 /* SIGINT signal handler, registered by "to_wait" method. */
1126 record_sig_handler (int signo)
1129 fprintf_unfiltered (gdb_stdlog, "Process record: get a signal\n");
1131 /* It will break the running inferior in replay mode. */
1132 record_resume_step = 1;
1134 /* It will let record_wait set inferior status to get the signal
1140 record_wait_cleanups (void *ignore)
1142 if (execution_direction == EXEC_REVERSE)
1144 if (record_list->next)
1145 record_list = record_list->next;
1148 record_list = record_list->prev;
1151 /* "to_wait" target method for process record target.
1153 In record mode, the target is always run in singlestep mode
1154 (even when gdb says to continue). The to_wait method intercepts
1155 the stop events and determines which ones are to be passed on to
1156 gdb. Most stop events are just singlestep events that gdb is not
1157 to know about, so the to_wait method just records them and keeps
1160 In replay mode, this function emulates the recorded execution log,
1161 one instruction at a time (forward or backward), and determines
1165 record_wait_1 (struct target_ops *ops,
1166 ptid_t ptid, struct target_waitstatus *status,
1169 struct cleanup *set_cleanups = record_gdb_operation_disable_set ();
1172 fprintf_unfiltered (gdb_stdlog,
1173 "Process record: record_wait "
1174 "record_resume_step = %d, record_resumed = %d, direction=%s\n",
1175 record_resume_step, record_resumed,
1176 record_execution_dir == EXEC_FORWARD ? "forward" : "reverse");
1178 if (!record_resumed)
1180 gdb_assert ((options & TARGET_WNOHANG) != 0);
1182 /* No interesting event. */
1183 status->kind = TARGET_WAITKIND_IGNORE;
1184 return minus_one_ptid;
1188 signal (SIGINT, record_sig_handler);
1190 if (!RECORD_IS_REPLAY && ops != &record_core_ops)
1192 if (record_resume_step)
1194 /* This is a single step. */
1195 return record_beneath_to_wait (record_beneath_to_wait_ops,
1196 ptid, status, options);
1200 /* This is not a single step. */
1203 struct gdbarch *gdbarch = target_thread_architecture (inferior_ptid);
1207 ret = record_beneath_to_wait (record_beneath_to_wait_ops,
1208 ptid, status, options);
1209 if (status->kind == TARGET_WAITKIND_IGNORE)
1212 fprintf_unfiltered (gdb_stdlog,
1213 "Process record: record_wait "
1214 "target beneath not done yet\n");
1218 if (single_step_breakpoints_inserted ())
1219 remove_single_step_breakpoints ();
1221 if (record_resume_step)
1224 /* Is this a SIGTRAP? */
1225 if (status->kind == TARGET_WAITKIND_STOPPED
1226 && status->value.sig == GDB_SIGNAL_TRAP)
1228 struct regcache *regcache;
1229 struct address_space *aspace;
1231 /* Yes -- this is likely our single-step finishing,
1232 but check if there's any reason the core would be
1233 interested in the event. */
1235 registers_changed ();
1236 regcache = get_current_regcache ();
1237 tmp_pc = regcache_read_pc (regcache);
1238 aspace = get_regcache_aspace (regcache);
1240 if (target_stopped_by_watchpoint ())
1242 /* Always interested in watchpoints. */
1244 else if (breakpoint_inserted_here_p (aspace, tmp_pc))
1246 /* There is a breakpoint here. Let the core
1248 if (software_breakpoint_inserted_here_p (aspace, tmp_pc))
1250 struct gdbarch *gdbarch
1251 = get_regcache_arch (regcache);
1252 CORE_ADDR decr_pc_after_break
1253 = gdbarch_decr_pc_after_break (gdbarch);
1254 if (decr_pc_after_break)
1255 regcache_write_pc (regcache,
1256 tmp_pc + decr_pc_after_break);
1261 /* This is a single-step trap. Record the
1262 insn and issue another step.
1263 FIXME: this part can be a random SIGTRAP too.
1264 But GDB cannot handle it. */
1267 if (!record_message_wrapper_safe (regcache,
1270 status->kind = TARGET_WAITKIND_STOPPED;
1271 status->value.sig = GDB_SIGNAL_0;
1275 if (gdbarch_software_single_step_p (gdbarch))
1277 /* Try to insert the software single step breakpoint.
1278 If insert success, set step to 0. */
1279 set_executing (inferior_ptid, 0);
1280 reinit_frame_cache ();
1281 if (gdbarch_software_single_step (gdbarch,
1282 get_current_frame ()))
1284 set_executing (inferior_ptid, 1);
1288 fprintf_unfiltered (gdb_stdlog,
1289 "Process record: record_wait "
1290 "issuing one more step in the target beneath\n");
1291 record_beneath_to_resume (record_beneath_to_resume_ops,
1298 /* The inferior is broken by a breakpoint or a signal. */
1307 struct regcache *regcache = get_current_regcache ();
1308 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1309 struct address_space *aspace = get_regcache_aspace (regcache);
1310 int continue_flag = 1;
1311 int first_record_end = 1;
1312 struct cleanup *old_cleanups = make_cleanup (record_wait_cleanups, 0);
1315 record_hw_watchpoint = 0;
1316 status->kind = TARGET_WAITKIND_STOPPED;
1318 /* Check breakpoint when forward execute. */
1319 if (execution_direction == EXEC_FORWARD)
1321 tmp_pc = regcache_read_pc (regcache);
1322 if (breakpoint_inserted_here_p (aspace, tmp_pc))
1324 int decr_pc_after_break = gdbarch_decr_pc_after_break (gdbarch);
1327 fprintf_unfiltered (gdb_stdlog,
1328 "Process record: break at %s.\n",
1329 paddress (gdbarch, tmp_pc));
1331 if (decr_pc_after_break
1332 && !record_resume_step
1333 && software_breakpoint_inserted_here_p (aspace, tmp_pc))
1334 regcache_write_pc (regcache,
1335 tmp_pc + decr_pc_after_break);
1340 /* If GDB is in terminal_inferior mode, it will not get the signal.
1341 And in GDB replay mode, GDB doesn't need to be in terminal_inferior
1342 mode, because inferior will not executed.
1343 Then set it to terminal_ours to make GDB get the signal. */
1344 target_terminal_ours ();
1346 /* In EXEC_FORWARD mode, record_list points to the tail of prev
1348 if (execution_direction == EXEC_FORWARD && record_list->next)
1349 record_list = record_list->next;
1351 /* Loop over the record_list, looking for the next place to
1355 /* Check for beginning and end of log. */
1356 if (execution_direction == EXEC_REVERSE
1357 && record_list == &record_first)
1359 /* Hit beginning of record log in reverse. */
1360 status->kind = TARGET_WAITKIND_NO_HISTORY;
1363 if (execution_direction != EXEC_REVERSE && !record_list->next)
1365 /* Hit end of record log going forward. */
1366 status->kind = TARGET_WAITKIND_NO_HISTORY;
1370 record_exec_insn (regcache, gdbarch, record_list);
1372 if (record_list->type == record_end)
1374 if (record_debug > 1)
1375 fprintf_unfiltered (gdb_stdlog,
1376 "Process record: record_end %s to "
1378 host_address_to_string (record_list));
1380 if (first_record_end && execution_direction == EXEC_REVERSE)
1382 /* When reverse excute, the first record_end is the part of
1383 current instruction. */
1384 first_record_end = 0;
1388 /* In EXEC_REVERSE mode, this is the record_end of prev
1390 In EXEC_FORWARD mode, this is the record_end of current
1393 if (record_resume_step)
1395 if (record_debug > 1)
1396 fprintf_unfiltered (gdb_stdlog,
1397 "Process record: step.\n");
1401 /* check breakpoint */
1402 tmp_pc = regcache_read_pc (regcache);
1403 if (breakpoint_inserted_here_p (aspace, tmp_pc))
1405 int decr_pc_after_break
1406 = gdbarch_decr_pc_after_break (gdbarch);
1409 fprintf_unfiltered (gdb_stdlog,
1410 "Process record: break "
1412 paddress (gdbarch, tmp_pc));
1413 if (decr_pc_after_break
1414 && execution_direction == EXEC_FORWARD
1415 && !record_resume_step
1416 && software_breakpoint_inserted_here_p (aspace,
1418 regcache_write_pc (regcache,
1419 tmp_pc + decr_pc_after_break);
1423 if (record_hw_watchpoint)
1426 fprintf_unfiltered (gdb_stdlog,
1427 "Process record: hit hw "
1431 /* Check target signal */
1432 if (record_list->u.end.sigval != GDB_SIGNAL_0)
1433 /* FIXME: better way to check */
1440 if (execution_direction == EXEC_REVERSE)
1442 if (record_list->prev)
1443 record_list = record_list->prev;
1447 if (record_list->next)
1448 record_list = record_list->next;
1452 while (continue_flag);
1456 status->value.sig = GDB_SIGNAL_INT;
1457 else if (record_list->u.end.sigval != GDB_SIGNAL_0)
1458 /* FIXME: better way to check */
1459 status->value.sig = record_list->u.end.sigval;
1461 status->value.sig = GDB_SIGNAL_TRAP;
1463 discard_cleanups (old_cleanups);
1466 signal (SIGINT, handle_sigint);
1468 do_cleanups (set_cleanups);
1469 return inferior_ptid;
1473 record_wait (struct target_ops *ops,
1474 ptid_t ptid, struct target_waitstatus *status,
1479 return_ptid = record_wait_1 (ops, ptid, status, options);
1480 if (status->kind != TARGET_WAITKIND_IGNORE)
1482 /* We're reporting a stop. Make sure any spurious
1483 target_wait(WNOHANG) doesn't advance the target until the
1484 core wants us resumed again. */
1491 record_stopped_by_watchpoint (void)
1493 if (RECORD_IS_REPLAY)
1494 return record_hw_watchpoint;
1496 return record_beneath_to_stopped_by_watchpoint ();
1500 record_stopped_data_address (struct target_ops *ops, CORE_ADDR *addr_p)
1502 if (RECORD_IS_REPLAY)
1505 return record_beneath_to_stopped_data_address (ops, addr_p);
1508 /* "to_disconnect" method for process record target. */
1511 record_disconnect (struct target_ops *target, char *args, int from_tty)
1514 fprintf_unfiltered (gdb_stdlog, "Process record: record_disconnect\n");
1516 unpush_target (&record_ops);
1517 target_disconnect (args, from_tty);
1520 /* "to_detach" method for process record target. */
1523 record_detach (struct target_ops *ops, char *args, int from_tty)
1526 fprintf_unfiltered (gdb_stdlog, "Process record: record_detach\n");
1528 unpush_target (&record_ops);
1529 target_detach (args, from_tty);
1532 /* "to_mourn_inferior" method for process record target. */
1535 record_mourn_inferior (struct target_ops *ops)
1538 fprintf_unfiltered (gdb_stdlog, "Process record: "
1539 "record_mourn_inferior\n");
1541 unpush_target (&record_ops);
1542 target_mourn_inferior ();
1545 /* Close process record target before killing the inferior process. */
1548 record_kill (struct target_ops *ops)
1551 fprintf_unfiltered (gdb_stdlog, "Process record: record_kill\n");
1553 unpush_target (&record_ops);
1557 /* Record registers change (by user or by GDB) to list as an instruction. */
1560 record_registers_change (struct regcache *regcache, int regnum)
1562 /* Check record_insn_num. */
1563 record_check_insn_num (0);
1565 record_arch_list_head = NULL;
1566 record_arch_list_tail = NULL;
1572 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
1574 if (record_arch_list_add_reg (regcache, i))
1576 record_list_release (record_arch_list_tail);
1577 error (_("Process record: failed to record execution log."));
1583 if (record_arch_list_add_reg (regcache, regnum))
1585 record_list_release (record_arch_list_tail);
1586 error (_("Process record: failed to record execution log."));
1589 if (record_arch_list_add_end ())
1591 record_list_release (record_arch_list_tail);
1592 error (_("Process record: failed to record execution log."));
1594 record_list->next = record_arch_list_head;
1595 record_arch_list_head->prev = record_list;
1596 record_list = record_arch_list_tail;
1598 if (record_insn_num == record_insn_max_num && record_insn_max_num)
1599 record_list_release_first ();
1604 /* "to_store_registers" method for process record target. */
1607 record_store_registers (struct target_ops *ops, struct regcache *regcache,
1610 if (!record_gdb_operation_disable)
1612 if (RECORD_IS_REPLAY)
1616 /* Let user choose if he wants to write register or not. */
1619 query (_("Because GDB is in replay mode, changing the "
1620 "value of a register will make the execution "
1621 "log unusable from this point onward. "
1622 "Change all registers?"));
1625 query (_("Because GDB is in replay mode, changing the value "
1626 "of a register will make the execution log unusable "
1627 "from this point onward. Change register %s?"),
1628 gdbarch_register_name (get_regcache_arch (regcache),
1633 /* Invalidate the value of regcache that was set in function
1634 "regcache_raw_write". */
1640 i < gdbarch_num_regs (get_regcache_arch (regcache));
1642 regcache_invalidate (regcache, i);
1645 regcache_invalidate (regcache, regno);
1647 error (_("Process record canceled the operation."));
1650 /* Destroy the record from here forward. */
1651 record_list_release_following (record_list);
1654 record_registers_change (regcache, regno);
1656 record_beneath_to_store_registers (record_beneath_to_store_registers_ops,
1660 /* "to_xfer_partial" method. Behavior is conditional on RECORD_IS_REPLAY.
1661 In replay mode, we cannot write memory unles we are willing to
1662 invalidate the record/replay log from this point forward. */
1665 record_xfer_partial (struct target_ops *ops, enum target_object object,
1666 const char *annex, gdb_byte *readbuf,
1667 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
1669 if (!record_gdb_operation_disable
1670 && (object == TARGET_OBJECT_MEMORY
1671 || object == TARGET_OBJECT_RAW_MEMORY) && writebuf)
1673 if (RECORD_IS_REPLAY)
1675 /* Let user choose if he wants to write memory or not. */
1676 if (!query (_("Because GDB is in replay mode, writing to memory "
1677 "will make the execution log unusable from this "
1678 "point onward. Write memory at address %s?"),
1679 paddress (target_gdbarch, offset)))
1680 error (_("Process record canceled the operation."));
1682 /* Destroy the record from here forward. */
1683 record_list_release_following (record_list);
1686 /* Check record_insn_num */
1687 record_check_insn_num (0);
1689 /* Record registers change to list as an instruction. */
1690 record_arch_list_head = NULL;
1691 record_arch_list_tail = NULL;
1692 if (record_arch_list_add_mem (offset, len))
1694 record_list_release (record_arch_list_tail);
1696 fprintf_unfiltered (gdb_stdlog,
1697 "Process record: failed to record "
1701 if (record_arch_list_add_end ())
1703 record_list_release (record_arch_list_tail);
1705 fprintf_unfiltered (gdb_stdlog,
1706 "Process record: failed to record "
1710 record_list->next = record_arch_list_head;
1711 record_arch_list_head->prev = record_list;
1712 record_list = record_arch_list_tail;
1714 if (record_insn_num == record_insn_max_num && record_insn_max_num)
1715 record_list_release_first ();
1720 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops,
1721 object, annex, readbuf, writebuf,
1725 /* This structure represents a breakpoint inserted while the record
1726 target is active. We use this to know when to install/remove
1727 breakpoints in/from the target beneath. For example, a breakpoint
1728 may be inserted while recording, but removed when not replaying nor
1729 recording. In that case, the breakpoint had not been inserted on
1730 the target beneath, so we should not try to remove it there. */
1732 struct record_breakpoint
1734 /* The address and address space the breakpoint was set at. */
1735 struct address_space *address_space;
1738 /* True when the breakpoint has been also installed in the target
1739 beneath. This will be false for breakpoints set during replay or
1741 int in_target_beneath;
1744 typedef struct record_breakpoint *record_breakpoint_p;
1745 DEF_VEC_P(record_breakpoint_p);
1747 /* The list of breakpoints inserted while the record target is
1749 VEC(record_breakpoint_p) *record_breakpoints = NULL;
1752 record_sync_record_breakpoints (struct bp_location *loc, void *data)
1754 if (loc->loc_type != bp_loc_software_breakpoint)
1759 struct record_breakpoint *bp = XNEW (struct record_breakpoint);
1761 bp->addr = loc->target_info.placed_address;
1762 bp->address_space = loc->target_info.placed_address_space;
1764 bp->in_target_beneath = 1;
1766 VEC_safe_push (record_breakpoint_p, record_breakpoints, bp);
1770 /* Sync existing breakpoints to record_breakpoints. */
1773 record_init_record_breakpoints (void)
1775 VEC_free (record_breakpoint_p, record_breakpoints);
1777 iterate_over_bp_locations (record_sync_record_breakpoints);
1780 /* Behavior is conditional on RECORD_IS_REPLAY. We will not actually
1781 insert or remove breakpoints in the real target when replaying, nor
1785 record_insert_breakpoint (struct gdbarch *gdbarch,
1786 struct bp_target_info *bp_tgt)
1788 struct record_breakpoint *bp;
1789 int in_target_beneath = 0;
1791 if (!RECORD_IS_REPLAY)
1793 /* When recording, we currently always single-step, so we don't
1794 really need to install regular breakpoints in the inferior.
1795 However, we do have to insert software single-step
1796 breakpoints, in case the target can't hardware step. To keep
1797 things single, we always insert. */
1798 struct cleanup *old_cleanups;
1801 old_cleanups = record_gdb_operation_disable_set ();
1802 ret = record_beneath_to_insert_breakpoint (gdbarch, bp_tgt);
1803 do_cleanups (old_cleanups);
1808 in_target_beneath = 1;
1811 bp = XNEW (struct record_breakpoint);
1812 bp->addr = bp_tgt->placed_address;
1813 bp->address_space = bp_tgt->placed_address_space;
1814 bp->in_target_beneath = in_target_beneath;
1815 VEC_safe_push (record_breakpoint_p, record_breakpoints, bp);
1819 /* "to_remove_breakpoint" method for process record target. */
1822 record_remove_breakpoint (struct gdbarch *gdbarch,
1823 struct bp_target_info *bp_tgt)
1825 struct record_breakpoint *bp;
1829 VEC_iterate (record_breakpoint_p, record_breakpoints, ix, bp);
1832 if (bp->addr == bp_tgt->placed_address
1833 && bp->address_space == bp_tgt->placed_address_space)
1835 if (bp->in_target_beneath)
1837 struct cleanup *old_cleanups;
1840 old_cleanups = record_gdb_operation_disable_set ();
1841 ret = record_beneath_to_remove_breakpoint (gdbarch, bp_tgt);
1842 do_cleanups (old_cleanups);
1848 VEC_unordered_remove (record_breakpoint_p, record_breakpoints, ix);
1853 gdb_assert_not_reached ("removing unknown breakpoint");
1856 /* "to_can_execute_reverse" method for process record target. */
1859 record_can_execute_reverse (void)
1864 /* "to_get_bookmark" method for process record and prec over core. */
1867 record_get_bookmark (char *args, int from_tty)
1869 gdb_byte *ret = NULL;
1871 /* Return stringified form of instruction count. */
1872 if (record_list && record_list->type == record_end)
1873 ret = xstrdup (pulongest (record_list->u.end.insn_num));
1878 fprintf_unfiltered (gdb_stdlog,
1879 "record_get_bookmark returns %s\n", ret);
1881 fprintf_unfiltered (gdb_stdlog,
1882 "record_get_bookmark returns NULL\n");
1887 /* The implementation of the command "record goto". */
1888 static void cmd_record_goto (char *, int);
1890 /* "to_goto_bookmark" method for process record and prec over core. */
1893 record_goto_bookmark (gdb_byte *bookmark, int from_tty)
1896 fprintf_unfiltered (gdb_stdlog,
1897 "record_goto_bookmark receives %s\n", bookmark);
1899 if (bookmark[0] == '\'' || bookmark[0] == '\"')
1901 if (bookmark[strlen (bookmark) - 1] != bookmark[0])
1902 error (_("Unbalanced quotes: %s"), bookmark);
1904 /* Strip trailing quote. */
1905 bookmark[strlen (bookmark) - 1] = '\0';
1906 /* Strip leading quote. */
1908 /* Pass along to cmd_record_goto. */
1911 cmd_record_goto ((char *) bookmark, from_tty);
1916 record_async (void (*callback) (enum inferior_event_type event_type,
1917 void *context), void *context)
1919 /* If we're on top of a line target (e.g., linux-nat, remote), then
1920 set it to async mode as well. Will be NULL if we're sitting on
1921 top of the core target, for "record restore". */
1922 if (record_beneath_to_async != NULL)
1923 record_beneath_to_async (callback, context);
1927 record_can_async_p (void)
1929 /* We only enable async when the user specifically asks for it. */
1930 return target_async_permitted;
1934 record_is_async_p (void)
1936 /* We only enable async when the user specifically asks for it. */
1937 return target_async_permitted;
1940 static enum exec_direction_kind
1941 record_execution_direction (void)
1943 return record_execution_dir;
1947 init_record_ops (void)
1949 record_ops.to_shortname = "record";
1950 record_ops.to_longname = "Process record and replay target";
1952 "Log program while executing and replay execution from log.";
1953 record_ops.to_open = record_open;
1954 record_ops.to_close = record_close;
1955 record_ops.to_resume = record_resume;
1956 record_ops.to_wait = record_wait;
1957 record_ops.to_disconnect = record_disconnect;
1958 record_ops.to_detach = record_detach;
1959 record_ops.to_mourn_inferior = record_mourn_inferior;
1960 record_ops.to_kill = record_kill;
1961 record_ops.to_create_inferior = find_default_create_inferior;
1962 record_ops.to_store_registers = record_store_registers;
1963 record_ops.to_xfer_partial = record_xfer_partial;
1964 record_ops.to_insert_breakpoint = record_insert_breakpoint;
1965 record_ops.to_remove_breakpoint = record_remove_breakpoint;
1966 record_ops.to_stopped_by_watchpoint = record_stopped_by_watchpoint;
1967 record_ops.to_stopped_data_address = record_stopped_data_address;
1968 record_ops.to_can_execute_reverse = record_can_execute_reverse;
1969 record_ops.to_stratum = record_stratum;
1970 /* Add bookmark target methods. */
1971 record_ops.to_get_bookmark = record_get_bookmark;
1972 record_ops.to_goto_bookmark = record_goto_bookmark;
1973 record_ops.to_async = record_async;
1974 record_ops.to_can_async_p = record_can_async_p;
1975 record_ops.to_is_async_p = record_is_async_p;
1976 record_ops.to_execution_direction = record_execution_direction;
1977 record_ops.to_magic = OPS_MAGIC;
1980 /* "to_resume" method for prec over corefile. */
1983 record_core_resume (struct target_ops *ops, ptid_t ptid, int step,
1984 enum gdb_signal signal)
1986 record_resume_step = step;
1988 record_execution_dir = execution_direction;
1990 /* We are about to start executing the inferior (or simulate it),
1991 let's register it with the event loop. */
1992 if (target_can_async_p ())
1994 target_async (inferior_event_handler, 0);
1996 /* Notify the event loop there's an event to wait for. */
1997 mark_async_event_handler (record_async_inferior_event_token);
2001 /* "to_kill" method for prec over corefile. */
2004 record_core_kill (struct target_ops *ops)
2007 fprintf_unfiltered (gdb_stdlog, "Process record: record_core_kill\n");
2009 unpush_target (&record_core_ops);
2012 /* "to_fetch_registers" method for prec over corefile. */
2015 record_core_fetch_registers (struct target_ops *ops,
2016 struct regcache *regcache,
2021 int num = gdbarch_num_regs (get_regcache_arch (regcache));
2024 for (i = 0; i < num; i ++)
2025 regcache_raw_supply (regcache, i,
2026 record_core_regbuf + MAX_REGISTER_SIZE * i);
2029 regcache_raw_supply (regcache, regno,
2030 record_core_regbuf + MAX_REGISTER_SIZE * regno);
2033 /* "to_prepare_to_store" method for prec over corefile. */
2036 record_core_prepare_to_store (struct regcache *regcache)
2040 /* "to_store_registers" method for prec over corefile. */
2043 record_core_store_registers (struct target_ops *ops,
2044 struct regcache *regcache,
2047 if (record_gdb_operation_disable)
2048 regcache_raw_collect (regcache, regno,
2049 record_core_regbuf + MAX_REGISTER_SIZE * regno);
2051 error (_("You can't do that without a process to debug."));
2054 /* "to_xfer_partial" method for prec over corefile. */
2057 record_core_xfer_partial (struct target_ops *ops, enum target_object object,
2058 const char *annex, gdb_byte *readbuf,
2059 const gdb_byte *writebuf, ULONGEST offset,
2062 if (object == TARGET_OBJECT_MEMORY)
2064 if (record_gdb_operation_disable || !writebuf)
2066 struct target_section *p;
2068 for (p = record_core_start; p < record_core_end; p++)
2070 if (offset >= p->addr)
2072 struct record_core_buf_entry *entry;
2073 ULONGEST sec_offset;
2075 if (offset >= p->endaddr)
2078 if (offset + len > p->endaddr)
2079 len = p->endaddr - offset;
2081 sec_offset = offset - p->addr;
2083 /* Read readbuf or write writebuf p, offset, len. */
2085 if (p->the_bfd_section->flags & SEC_CONSTRUCTOR
2086 || (p->the_bfd_section->flags & SEC_HAS_CONTENTS) == 0)
2089 memset (readbuf, 0, len);
2092 /* Get record_core_buf_entry. */
2093 for (entry = record_core_buf_list; entry;
2094 entry = entry->prev)
2101 /* Add a new entry. */
2102 entry = (struct record_core_buf_entry *)
2103 xmalloc (sizeof (struct record_core_buf_entry));
2105 if (!bfd_malloc_and_get_section (p->bfd,
2112 entry->prev = record_core_buf_list;
2113 record_core_buf_list = entry;
2116 memcpy (entry->buf + sec_offset, writebuf,
2122 return record_beneath_to_xfer_partial
2123 (record_beneath_to_xfer_partial_ops,
2124 object, annex, readbuf, writebuf,
2127 memcpy (readbuf, entry->buf + sec_offset,
2138 error (_("You can't do that without a process to debug."));
2141 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops,
2142 object, annex, readbuf, writebuf,
2146 /* "to_insert_breakpoint" method for prec over corefile. */
2149 record_core_insert_breakpoint (struct gdbarch *gdbarch,
2150 struct bp_target_info *bp_tgt)
2155 /* "to_remove_breakpoint" method for prec over corefile. */
2158 record_core_remove_breakpoint (struct gdbarch *gdbarch,
2159 struct bp_target_info *bp_tgt)
2164 /* "to_has_execution" method for prec over corefile. */
2167 record_core_has_execution (struct target_ops *ops, ptid_t the_ptid)
2173 init_record_core_ops (void)
2175 record_core_ops.to_shortname = "record-core";
2176 record_core_ops.to_longname = "Process record and replay target";
2177 record_core_ops.to_doc =
2178 "Log program while executing and replay execution from log.";
2179 record_core_ops.to_open = record_open;
2180 record_core_ops.to_close = record_close;
2181 record_core_ops.to_resume = record_core_resume;
2182 record_core_ops.to_wait = record_wait;
2183 record_core_ops.to_kill = record_core_kill;
2184 record_core_ops.to_fetch_registers = record_core_fetch_registers;
2185 record_core_ops.to_prepare_to_store = record_core_prepare_to_store;
2186 record_core_ops.to_store_registers = record_core_store_registers;
2187 record_core_ops.to_xfer_partial = record_core_xfer_partial;
2188 record_core_ops.to_insert_breakpoint = record_core_insert_breakpoint;
2189 record_core_ops.to_remove_breakpoint = record_core_remove_breakpoint;
2190 record_core_ops.to_stopped_by_watchpoint = record_stopped_by_watchpoint;
2191 record_core_ops.to_stopped_data_address = record_stopped_data_address;
2192 record_core_ops.to_can_execute_reverse = record_can_execute_reverse;
2193 record_core_ops.to_has_execution = record_core_has_execution;
2194 record_core_ops.to_stratum = record_stratum;
2195 /* Add bookmark target methods. */
2196 record_core_ops.to_get_bookmark = record_get_bookmark;
2197 record_core_ops.to_goto_bookmark = record_goto_bookmark;
2198 record_core_ops.to_async = record_async;
2199 record_core_ops.to_can_async_p = record_can_async_p;
2200 record_core_ops.to_is_async_p = record_is_async_p;
2201 record_core_ops.to_execution_direction = record_execution_direction;
2202 record_core_ops.to_magic = OPS_MAGIC;
2205 /* Implement "show record debug" command. */
2208 show_record_debug (struct ui_file *file, int from_tty,
2209 struct cmd_list_element *c, const char *value)
2211 fprintf_filtered (file, _("Debugging of process record target is %s.\n"),
2215 /* Alias for "target record". */
2218 cmd_record_start (char *args, int from_tty)
2220 execute_command ("target record", from_tty);
2223 /* Truncate the record log from the present point
2224 of replay until the end. */
2227 cmd_record_delete (char *args, int from_tty)
2229 if (current_target.to_stratum == record_stratum)
2231 if (RECORD_IS_REPLAY)
2233 if (!from_tty || query (_("Delete the log from this point forward "
2234 "and begin to record the running message "
2236 record_list_release_following (record_list);
2239 printf_unfiltered (_("Already at end of record list.\n"));
2243 printf_unfiltered (_("Process record is not started.\n"));
2246 /* Implement the "stoprecord" or "record stop" command. */
2249 cmd_record_stop (char *args, int from_tty)
2251 if (current_target.to_stratum == record_stratum)
2253 unpush_target (&record_ops);
2254 printf_unfiltered (_("Process record is stopped and all execution "
2255 "logs are deleted.\n"));
2258 printf_unfiltered (_("Process record is not started.\n"));
2261 /* Set upper limit of record log size. */
2264 set_record_insn_max_num (char *args, int from_tty, struct cmd_list_element *c)
2266 if (record_insn_num > record_insn_max_num && record_insn_max_num)
2268 /* Count down record_insn_num while releasing records from list. */
2269 while (record_insn_num > record_insn_max_num)
2271 record_list_release_first ();
2277 static struct cmd_list_element *record_cmdlist, *set_record_cmdlist,
2278 *show_record_cmdlist, *info_record_cmdlist;
2281 set_record_command (char *args, int from_tty)
2283 printf_unfiltered (_("\"set record\" must be followed "
2284 "by an apporpriate subcommand.\n"));
2285 help_list (set_record_cmdlist, "set record ", all_commands, gdb_stdout);
2289 show_record_command (char *args, int from_tty)
2291 cmd_show_list (show_record_cmdlist, from_tty, "");
2294 /* Display some statistics about the execution log. */
2297 info_record_command (char *args, int from_tty)
2299 struct record_entry *p;
2301 if (current_target.to_stratum == record_stratum)
2303 if (RECORD_IS_REPLAY)
2304 printf_filtered (_("Replay mode:\n"));
2306 printf_filtered (_("Record mode:\n"));
2308 /* Find entry for first actual instruction in the log. */
2309 for (p = record_first.next;
2310 p != NULL && p->type != record_end;
2314 /* Do we have a log at all? */
2315 if (p != NULL && p->type == record_end)
2317 /* Display instruction number for first instruction in the log. */
2318 printf_filtered (_("Lowest recorded instruction number is %s.\n"),
2319 pulongest (p->u.end.insn_num));
2321 /* If in replay mode, display where we are in the log. */
2322 if (RECORD_IS_REPLAY)
2323 printf_filtered (_("Current instruction number is %s.\n"),
2324 pulongest (record_list->u.end.insn_num));
2326 /* Display instruction number for last instruction in the log. */
2327 printf_filtered (_("Highest recorded instruction number is %s.\n"),
2328 pulongest (record_insn_count));
2330 /* Display log count. */
2331 printf_filtered (_("Log contains %d instructions.\n"),
2336 printf_filtered (_("No instructions have been logged.\n"));
2341 printf_filtered (_("target record is not active.\n"));
2344 /* Display max log size. */
2345 printf_filtered (_("Max logged instructions is %d.\n"),
2346 record_insn_max_num);
2349 /* Record log save-file format
2350 Version 1 (never released)
2353 4 bytes: magic number htonl(0x20090829).
2354 NOTE: be sure to change whenever this file format changes!
2358 1 byte: record type (record_end, see enum record_type).
2360 1 byte: record type (record_reg, see enum record_type).
2361 8 bytes: register id (network byte order).
2362 MAX_REGISTER_SIZE bytes: register value.
2364 1 byte: record type (record_mem, see enum record_type).
2365 8 bytes: memory length (network byte order).
2366 8 bytes: memory address (network byte order).
2367 n bytes: memory value (n == memory length).
2370 4 bytes: magic number netorder32(0x20091016).
2371 NOTE: be sure to change whenever this file format changes!
2375 1 byte: record type (record_end, see enum record_type).
2377 4 bytes: instruction count
2379 1 byte: record type (record_reg, see enum record_type).
2380 4 bytes: register id (network byte order).
2381 n bytes: register value (n == actual register size).
2382 (eg. 4 bytes for x86 general registers).
2384 1 byte: record type (record_mem, see enum record_type).
2385 4 bytes: memory length (network byte order).
2386 8 bytes: memory address (network byte order).
2387 n bytes: memory value (n == memory length).
2391 /* bfdcore_read -- read bytes from a core file section. */
2394 bfdcore_read (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2396 int ret = bfd_get_section_contents (obfd, osec, buf, *offset, len);
2401 error (_("Failed to read %d bytes from core file %s ('%s')."),
2402 len, bfd_get_filename (obfd),
2403 bfd_errmsg (bfd_get_error ()));
2406 static inline uint64_t
2407 netorder64 (uint64_t input)
2411 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2412 BFD_ENDIAN_BIG, input);
2416 static inline uint32_t
2417 netorder32 (uint32_t input)
2421 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2422 BFD_ENDIAN_BIG, input);
2426 static inline uint16_t
2427 netorder16 (uint16_t input)
2431 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2432 BFD_ENDIAN_BIG, input);
2436 /* Restore the execution log from a core_bfd file. */
2438 record_restore (void)
2441 struct cleanup *old_cleanups;
2442 struct record_entry *rec;
2446 struct regcache *regcache;
2448 /* We restore the execution log from the open core bfd,
2450 if (core_bfd == NULL)
2453 /* "record_restore" can only be called when record list is empty. */
2454 gdb_assert (record_first.next == NULL);
2457 fprintf_unfiltered (gdb_stdlog, "Restoring recording from core file.\n");
2459 /* Now need to find our special note section. */
2460 osec = bfd_get_section_by_name (core_bfd, "null0");
2462 fprintf_unfiltered (gdb_stdlog, "Find precord section %s.\n",
2463 osec ? "succeeded" : "failed");
2466 osec_size = bfd_section_size (core_bfd, osec);
2468 fprintf_unfiltered (gdb_stdlog, "%s", bfd_section_name (core_bfd, osec));
2470 /* Check the magic code. */
2471 bfdcore_read (core_bfd, osec, &magic, sizeof (magic), &bfd_offset);
2472 if (magic != RECORD_FILE_MAGIC)
2473 error (_("Version mis-match or file format error in core file %s."),
2474 bfd_get_filename (core_bfd));
2476 fprintf_unfiltered (gdb_stdlog,
2477 " Reading 4-byte magic cookie "
2478 "RECORD_FILE_MAGIC (0x%s)\n",
2479 phex_nz (netorder32 (magic), 4));
2481 /* Restore the entries in recfd into record_arch_list_head and
2482 record_arch_list_tail. */
2483 record_arch_list_head = NULL;
2484 record_arch_list_tail = NULL;
2485 record_insn_num = 0;
2486 old_cleanups = make_cleanup (record_arch_list_cleanups, 0);
2487 regcache = get_current_regcache ();
2492 uint32_t regnum, len, signal, count;
2495 /* We are finished when offset reaches osec_size. */
2496 if (bfd_offset >= osec_size)
2498 bfdcore_read (core_bfd, osec, &rectype, sizeof (rectype), &bfd_offset);
2502 case record_reg: /* reg */
2503 /* Get register number to regnum. */
2504 bfdcore_read (core_bfd, osec, ®num,
2505 sizeof (regnum), &bfd_offset);
2506 regnum = netorder32 (regnum);
2508 rec = record_reg_alloc (regcache, regnum);
2511 bfdcore_read (core_bfd, osec, record_get_loc (rec),
2512 rec->u.reg.len, &bfd_offset);
2515 fprintf_unfiltered (gdb_stdlog,
2516 " Reading register %d (1 "
2517 "plus %lu plus %d bytes)\n",
2519 (unsigned long) sizeof (regnum),
2523 case record_mem: /* mem */
2525 bfdcore_read (core_bfd, osec, &len,
2526 sizeof (len), &bfd_offset);
2527 len = netorder32 (len);
2530 bfdcore_read (core_bfd, osec, &addr,
2531 sizeof (addr), &bfd_offset);
2532 addr = netorder64 (addr);
2534 rec = record_mem_alloc (addr, len);
2537 bfdcore_read (core_bfd, osec, record_get_loc (rec),
2538 rec->u.mem.len, &bfd_offset);
2541 fprintf_unfiltered (gdb_stdlog,
2542 " Reading memory %s (1 plus "
2543 "%lu plus %lu plus %d bytes)\n",
2544 paddress (get_current_arch (),
2546 (unsigned long) sizeof (addr),
2547 (unsigned long) sizeof (len),
2551 case record_end: /* end */
2552 rec = record_end_alloc ();
2555 /* Get signal value. */
2556 bfdcore_read (core_bfd, osec, &signal,
2557 sizeof (signal), &bfd_offset);
2558 signal = netorder32 (signal);
2559 rec->u.end.sigval = signal;
2561 /* Get insn count. */
2562 bfdcore_read (core_bfd, osec, &count,
2563 sizeof (count), &bfd_offset);
2564 count = netorder32 (count);
2565 rec->u.end.insn_num = count;
2566 record_insn_count = count + 1;
2568 fprintf_unfiltered (gdb_stdlog,
2569 " Reading record_end (1 + "
2570 "%lu + %lu bytes), offset == %s\n",
2571 (unsigned long) sizeof (signal),
2572 (unsigned long) sizeof (count),
2573 paddress (get_current_arch (),
2578 error (_("Bad entry type in core file %s."),
2579 bfd_get_filename (core_bfd));
2583 /* Add rec to record arch list. */
2584 record_arch_list_add (rec);
2587 discard_cleanups (old_cleanups);
2589 /* Add record_arch_list_head to the end of record list. */
2590 record_first.next = record_arch_list_head;
2591 record_arch_list_head->prev = &record_first;
2592 record_arch_list_tail->next = NULL;
2593 record_list = &record_first;
2595 /* Update record_insn_max_num. */
2596 if (record_insn_num > record_insn_max_num)
2598 record_insn_max_num = record_insn_num;
2599 warning (_("Auto increase record/replay buffer limit to %d."),
2600 record_insn_max_num);
2604 printf_filtered (_("Restored records from core file %s.\n"),
2605 bfd_get_filename (core_bfd));
2607 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
2610 /* bfdcore_write -- write bytes into a core file section. */
2613 bfdcore_write (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2615 int ret = bfd_set_section_contents (obfd, osec, buf, *offset, len);
2620 error (_("Failed to write %d bytes to core file %s ('%s')."),
2621 len, bfd_get_filename (obfd),
2622 bfd_errmsg (bfd_get_error ()));
2625 /* Restore the execution log from a file. We use a modified elf
2626 corefile format, with an extra section for our data. */
2629 cmd_record_restore (char *args, int from_tty)
2631 core_file_command (args, from_tty);
2632 record_open (args, from_tty);
2636 record_save_cleanups (void *data)
2639 char *pathname = xstrdup (bfd_get_filename (obfd));
2646 /* Save the execution log to a file. We use a modified elf corefile
2647 format, with an extra section for our data. */
2650 cmd_record_save (char *args, int from_tty)
2652 char *recfilename, recfilename_buffer[40];
2653 struct record_entry *cur_record_list;
2655 struct regcache *regcache;
2656 struct gdbarch *gdbarch;
2657 struct cleanup *old_cleanups;
2658 struct cleanup *set_cleanups;
2661 asection *osec = NULL;
2664 if (strcmp (current_target.to_shortname, "record") != 0)
2665 error (_("This command can only be used with target 'record'.\n"
2666 "Use 'target record' first.\n"));
2672 /* Default recfile name is "gdb_record.PID". */
2673 snprintf (recfilename_buffer, sizeof (recfilename_buffer),
2674 "gdb_record.%d", PIDGET (inferior_ptid));
2675 recfilename = recfilename_buffer;
2678 /* Open the save file. */
2680 fprintf_unfiltered (gdb_stdlog, "Saving execution log to core file '%s'\n",
2683 /* Open the output file. */
2684 obfd = create_gcore_bfd (recfilename);
2685 old_cleanups = make_cleanup (record_save_cleanups, obfd);
2687 /* Save the current record entry to "cur_record_list". */
2688 cur_record_list = record_list;
2690 /* Get the values of regcache and gdbarch. */
2691 regcache = get_current_regcache ();
2692 gdbarch = get_regcache_arch (regcache);
2694 /* Disable the GDB operation record. */
2695 set_cleanups = record_gdb_operation_disable_set ();
2697 /* Reverse execute to the begin of record list. */
2700 /* Check for beginning and end of log. */
2701 if (record_list == &record_first)
2704 record_exec_insn (regcache, gdbarch, record_list);
2706 if (record_list->prev)
2707 record_list = record_list->prev;
2710 /* Compute the size needed for the extra bfd section. */
2711 save_size = 4; /* magic cookie */
2712 for (record_list = record_first.next; record_list;
2713 record_list = record_list->next)
2714 switch (record_list->type)
2717 save_size += 1 + 4 + 4;
2720 save_size += 1 + 4 + record_list->u.reg.len;
2723 save_size += 1 + 4 + 8 + record_list->u.mem.len;
2727 /* Make the new bfd section. */
2728 osec = bfd_make_section_anyway_with_flags (obfd, "precord",
2732 error (_("Failed to create 'precord' section for corefile %s: %s"),
2734 bfd_errmsg (bfd_get_error ()));
2735 bfd_set_section_size (obfd, osec, save_size);
2736 bfd_set_section_vma (obfd, osec, 0);
2737 bfd_set_section_alignment (obfd, osec, 0);
2738 bfd_section_lma (obfd, osec) = 0;
2740 /* Save corefile state. */
2741 write_gcore_file (obfd);
2743 /* Write out the record log. */
2744 /* Write the magic code. */
2745 magic = RECORD_FILE_MAGIC;
2747 fprintf_unfiltered (gdb_stdlog,
2748 " Writing 4-byte magic cookie "
2749 "RECORD_FILE_MAGIC (0x%s)\n",
2750 phex_nz (magic, 4));
2751 bfdcore_write (obfd, osec, &magic, sizeof (magic), &bfd_offset);
2753 /* Save the entries to recfd and forward execute to the end of
2755 record_list = &record_first;
2759 if (record_list != &record_first)
2762 uint32_t regnum, len, signal, count;
2765 type = record_list->type;
2766 bfdcore_write (obfd, osec, &type, sizeof (type), &bfd_offset);
2768 switch (record_list->type)
2770 case record_reg: /* reg */
2772 fprintf_unfiltered (gdb_stdlog,
2773 " Writing register %d (1 "
2774 "plus %lu plus %d bytes)\n",
2775 record_list->u.reg.num,
2776 (unsigned long) sizeof (regnum),
2777 record_list->u.reg.len);
2780 regnum = netorder32 (record_list->u.reg.num);
2781 bfdcore_write (obfd, osec, ®num,
2782 sizeof (regnum), &bfd_offset);
2785 bfdcore_write (obfd, osec, record_get_loc (record_list),
2786 record_list->u.reg.len, &bfd_offset);
2789 case record_mem: /* mem */
2791 fprintf_unfiltered (gdb_stdlog,
2792 " Writing memory %s (1 plus "
2793 "%lu plus %lu plus %d bytes)\n",
2795 record_list->u.mem.addr),
2796 (unsigned long) sizeof (addr),
2797 (unsigned long) sizeof (len),
2798 record_list->u.mem.len);
2801 len = netorder32 (record_list->u.mem.len);
2802 bfdcore_write (obfd, osec, &len, sizeof (len), &bfd_offset);
2804 /* Write memaddr. */
2805 addr = netorder64 (record_list->u.mem.addr);
2806 bfdcore_write (obfd, osec, &addr,
2807 sizeof (addr), &bfd_offset);
2810 bfdcore_write (obfd, osec, record_get_loc (record_list),
2811 record_list->u.mem.len, &bfd_offset);
2816 fprintf_unfiltered (gdb_stdlog,
2817 " Writing record_end (1 + "
2818 "%lu + %lu bytes)\n",
2819 (unsigned long) sizeof (signal),
2820 (unsigned long) sizeof (count));
2821 /* Write signal value. */
2822 signal = netorder32 (record_list->u.end.sigval);
2823 bfdcore_write (obfd, osec, &signal,
2824 sizeof (signal), &bfd_offset);
2826 /* Write insn count. */
2827 count = netorder32 (record_list->u.end.insn_num);
2828 bfdcore_write (obfd, osec, &count,
2829 sizeof (count), &bfd_offset);
2834 /* Execute entry. */
2835 record_exec_insn (regcache, gdbarch, record_list);
2837 if (record_list->next)
2838 record_list = record_list->next;
2843 /* Reverse execute to cur_record_list. */
2846 /* Check for beginning and end of log. */
2847 if (record_list == cur_record_list)
2850 record_exec_insn (regcache, gdbarch, record_list);
2852 if (record_list->prev)
2853 record_list = record_list->prev;
2856 do_cleanups (set_cleanups);
2858 discard_cleanups (old_cleanups);
2861 printf_filtered (_("Saved core file %s with execution log.\n"),
2865 /* record_goto_insn -- rewind the record log (forward or backward,
2866 depending on DIR) to the given entry, changing the program state
2870 record_goto_insn (struct record_entry *entry,
2871 enum exec_direction_kind dir)
2873 struct cleanup *set_cleanups = record_gdb_operation_disable_set ();
2874 struct regcache *regcache = get_current_regcache ();
2875 struct gdbarch *gdbarch = get_regcache_arch (regcache);
2877 /* Assume everything is valid: we will hit the entry,
2878 and we will not hit the end of the recording. */
2880 if (dir == EXEC_FORWARD)
2881 record_list = record_list->next;
2885 record_exec_insn (regcache, gdbarch, record_list);
2886 if (dir == EXEC_REVERSE)
2887 record_list = record_list->prev;
2889 record_list = record_list->next;
2890 } while (record_list != entry);
2891 do_cleanups (set_cleanups);
2894 /* "record goto" command. Argument is an instruction number,
2895 as given by "info record".
2897 Rewinds the recording (forward or backward) to the given instruction. */
2900 cmd_record_goto (char *arg, int from_tty)
2902 struct record_entry *p = NULL;
2903 ULONGEST target_insn = 0;
2905 if (arg == NULL || *arg == '\0')
2906 error (_("Command requires an argument (insn number to go to)."));
2908 if (strncmp (arg, "start", strlen ("start")) == 0
2909 || strncmp (arg, "begin", strlen ("begin")) == 0)
2911 /* Special case. Find first insn. */
2912 for (p = &record_first; p != NULL; p = p->next)
2913 if (p->type == record_end)
2916 target_insn = p->u.end.insn_num;
2918 else if (strncmp (arg, "end", strlen ("end")) == 0)
2920 /* Special case. Find last insn. */
2921 for (p = record_list; p->next != NULL; p = p->next)
2923 for (; p!= NULL; p = p->prev)
2924 if (p->type == record_end)
2927 target_insn = p->u.end.insn_num;
2931 /* General case. Find designated insn. */
2932 target_insn = parse_and_eval_long (arg);
2934 for (p = &record_first; p != NULL; p = p->next)
2935 if (p->type == record_end && p->u.end.insn_num == target_insn)
2940 error (_("Target insn '%s' not found."), arg);
2941 else if (p == record_list)
2942 error (_("Already at insn '%s'."), arg);
2943 else if (p->u.end.insn_num > record_list->u.end.insn_num)
2945 printf_filtered (_("Go forward to insn number %s\n"),
2946 pulongest (target_insn));
2947 record_goto_insn (p, EXEC_FORWARD);
2951 printf_filtered (_("Go backward to insn number %s\n"),
2952 pulongest (target_insn));
2953 record_goto_insn (p, EXEC_REVERSE);
2955 registers_changed ();
2956 reinit_frame_cache ();
2957 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
2960 /* Provide a prototype to silence -Wmissing-prototypes. */
2961 extern initialize_file_ftype _initialize_record;
2964 _initialize_record (void)
2966 struct cmd_list_element *c;
2968 /* Init record_first. */
2969 record_first.prev = NULL;
2970 record_first.next = NULL;
2971 record_first.type = record_end;
2974 add_target (&record_ops);
2975 init_record_core_ops ();
2976 add_target (&record_core_ops);
2978 add_setshow_zinteger_cmd ("record", no_class, &record_debug,
2979 _("Set debugging of record/replay feature."),
2980 _("Show debugging of record/replay feature."),
2981 _("When enabled, debugging output for "
2982 "record/replay feature is displayed."),
2983 NULL, show_record_debug, &setdebuglist,
2986 c = add_prefix_cmd ("record", class_obscure, cmd_record_start,
2987 _("Abbreviated form of \"target record\" command."),
2988 &record_cmdlist, "record ", 0, &cmdlist);
2989 set_cmd_completer (c, filename_completer);
2991 add_com_alias ("rec", "record", class_obscure, 1);
2992 add_prefix_cmd ("record", class_support, set_record_command,
2993 _("Set record options"), &set_record_cmdlist,
2994 "set record ", 0, &setlist);
2995 add_alias_cmd ("rec", "record", class_obscure, 1, &setlist);
2996 add_prefix_cmd ("record", class_support, show_record_command,
2997 _("Show record options"), &show_record_cmdlist,
2998 "show record ", 0, &showlist);
2999 add_alias_cmd ("rec", "record", class_obscure, 1, &showlist);
3000 add_prefix_cmd ("record", class_support, info_record_command,
3001 _("Info record options"), &info_record_cmdlist,
3002 "info record ", 0, &infolist);
3003 add_alias_cmd ("rec", "record", class_obscure, 1, &infolist);
3005 c = add_cmd ("save", class_obscure, cmd_record_save,
3006 _("Save the execution log to a file.\n\
3007 Argument is optional filename.\n\
3008 Default filename is 'gdb_record.<process_id>'."),
3010 set_cmd_completer (c, filename_completer);
3012 c = add_cmd ("restore", class_obscure, cmd_record_restore,
3013 _("Restore the execution log from a file.\n\
3014 Argument is filename. File must be created with 'record save'."),
3016 set_cmd_completer (c, filename_completer);
3018 add_cmd ("delete", class_obscure, cmd_record_delete,
3019 _("Delete the rest of execution log and start recording it anew."),
3021 add_alias_cmd ("d", "delete", class_obscure, 1, &record_cmdlist);
3022 add_alias_cmd ("del", "delete", class_obscure, 1, &record_cmdlist);
3024 add_cmd ("stop", class_obscure, cmd_record_stop,
3025 _("Stop the record/replay target."),
3027 add_alias_cmd ("s", "stop", class_obscure, 1, &record_cmdlist);
3029 /* Record instructions number limit command. */
3030 add_setshow_boolean_cmd ("stop-at-limit", no_class,
3031 &record_stop_at_limit, _("\
3032 Set whether record/replay stops when record/replay buffer becomes full."), _("\
3033 Show whether record/replay stops when record/replay buffer becomes full."),
3034 _("Default is ON.\n\
3035 When ON, if the record/replay buffer becomes full, ask user what to do.\n\
3036 When OFF, if the record/replay buffer becomes full,\n\
3037 delete the oldest recorded instruction to make room for each new one."),
3039 &set_record_cmdlist, &show_record_cmdlist);
3040 add_setshow_uinteger_cmd ("insn-number-max", no_class,
3041 &record_insn_max_num,
3042 _("Set record/replay buffer limit."),
3043 _("Show record/replay buffer limit."), _("\
3044 Set the maximum number of instructions to be stored in the\n\
3045 record/replay buffer. Zero means unlimited. Default is 200000."),
3046 set_record_insn_max_num,
3047 NULL, &set_record_cmdlist, &show_record_cmdlist);
3049 add_cmd ("goto", class_obscure, cmd_record_goto, _("\
3050 Restore the program to its state at instruction number N.\n\
3051 Argument is instruction number, as shown by 'info record'."),
3054 add_setshow_boolean_cmd ("memory-query", no_class,
3055 &record_memory_query, _("\
3056 Set whether query if PREC cannot record memory change of next instruction."),
3058 Show whether query if PREC cannot record memory change of next instruction."),
3061 When ON, query if PREC cannot record memory change of next instruction."),
3063 &set_record_cmdlist, &show_record_cmdlist);