1 /* Process record and replay target for GDB, the GNU debugger.
3 Copyright (C) 2008, 2009 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "gdbthread.h"
24 #include "event-top.h"
25 #include "exceptions.h"
26 #include "completer.h"
27 #include "arch-utils.h"
37 /* This module implements "target record", also known as "process
38 record and replay". This target sits on top of a "normal" target
39 (a target that "has execution"), and provides a record and replay
40 functionality, including reverse debugging.
42 Target record has two modes: recording, and replaying.
44 In record mode, we intercept the to_resume and to_wait methods.
45 Whenever gdb resumes the target, we run the target in single step
46 mode, and we build up an execution log in which, for each executed
47 instruction, we record all changes in memory and register state.
48 This is invisible to the user, to whom it just looks like an
49 ordinary debugging session (except for performance degredation).
51 In replay mode, instead of actually letting the inferior run as a
52 process, we simulate its execution by playing back the recorded
53 execution log. For each instruction in the log, we simulate the
54 instruction's side effects by duplicating the changes that it would
55 have made on memory and registers. */
57 #define DEFAULT_RECORD_INSN_MAX_NUM 200000
59 #define RECORD_IS_REPLAY \
60 (record_list->next || execution_direction == EXEC_REVERSE)
62 #define RECORD_FILE_MAGIC netorder32(0x20091016)
64 /* These are the core structs of the process record functionality.
66 A record_entry is a record of the value change of a register
67 ("record_reg") or a part of memory ("record_mem"). And each
68 instruction must have a struct record_entry ("record_end") that
69 indicates that this is the last struct record_entry of this
72 Each struct record_entry is linked to "record_list" by "prev" and
75 struct record_mem_entry
79 /* Set this flag if target memory for this entry
80 can no longer be accessed. */
81 int mem_entry_not_accessible;
85 gdb_byte buf[sizeof (gdb_byte *)];
89 struct record_reg_entry
96 gdb_byte buf[2 * sizeof (gdb_byte *)];
100 struct record_end_entry
102 enum target_signal sigval;
113 /* This is the data structure that makes up the execution log.
115 The execution log consists of a single linked list of entries
116 of type "struct record_entry". It is doubly linked so that it
117 can be traversed in either direction.
119 The start of the list is anchored by a struct called
120 "record_first". The pointer "record_list" either points to the
121 last entry that was added to the list (in record mode), or to the
122 next entry in the list that will be executed (in replay mode).
124 Each list element (struct record_entry), in addition to next and
125 prev pointers, consists of a union of three entry types: mem, reg,
126 and end. A field called "type" determines which entry type is
127 represented by a given list element.
129 Each instruction that is added to the execution log is represented
130 by a variable number of list elements ('entries'). The instruction
131 will have one "reg" entry for each register that is changed by
132 executing the instruction (including the PC in every case). It
133 will also have one "mem" entry for each memory change. Finally,
134 each instruction will have an "end" entry that separates it from
135 the changes associated with the next instruction. */
139 struct record_entry *prev;
140 struct record_entry *next;
141 enum record_type type;
145 struct record_reg_entry reg;
147 struct record_mem_entry mem;
149 struct record_end_entry end;
153 /* This is the debug switch for process record. */
154 int record_debug = 0;
156 struct record_core_buf_entry
158 struct record_core_buf_entry *prev;
159 struct target_section *p;
163 /* Record buf with core target. */
164 static gdb_byte *record_core_regbuf = NULL;
165 static struct target_section *record_core_start;
166 static struct target_section *record_core_end;
167 static struct record_core_buf_entry *record_core_buf_list = NULL;
169 /* The following variables are used for managing the linked list that
170 represents the execution log.
172 record_first is the anchor that holds down the beginning of the list.
174 record_list serves two functions:
175 1) In record mode, it anchors the end of the list.
176 2) In replay mode, it traverses the list and points to
177 the next instruction that must be emulated.
179 record_arch_list_head and record_arch_list_tail are used to manage
180 a separate list, which is used to build up the change elements of
181 the currently executing instruction during record mode. When this
182 instruction has been completely annotated in the "arch list", it
183 will be appended to the main execution log. */
185 static struct record_entry record_first;
186 static struct record_entry *record_list = &record_first;
187 static struct record_entry *record_arch_list_head = NULL;
188 static struct record_entry *record_arch_list_tail = NULL;
190 /* 1 ask user. 0 auto delete the last struct record_entry. */
191 static int record_stop_at_limit = 1;
192 /* Maximum allowed number of insns in execution log. */
193 static unsigned int record_insn_max_num = DEFAULT_RECORD_INSN_MAX_NUM;
194 /* Actual count of insns presently in execution log. */
195 static int record_insn_num = 0;
196 /* Count of insns logged so far (may be larger
197 than count of insns presently in execution log). */
198 static ULONGEST record_insn_count;
200 /* The target_ops of process record. */
201 static struct target_ops record_ops;
202 static struct target_ops record_core_ops;
204 /* The beneath function pointers. */
205 static struct target_ops *record_beneath_to_resume_ops;
206 static void (*record_beneath_to_resume) (struct target_ops *, ptid_t, int,
208 static struct target_ops *record_beneath_to_wait_ops;
209 static ptid_t (*record_beneath_to_wait) (struct target_ops *, ptid_t,
210 struct target_waitstatus *,
212 static struct target_ops *record_beneath_to_store_registers_ops;
213 static void (*record_beneath_to_store_registers) (struct target_ops *,
216 static struct target_ops *record_beneath_to_xfer_partial_ops;
217 static LONGEST (*record_beneath_to_xfer_partial) (struct target_ops *ops,
218 enum target_object object,
221 const gdb_byte *writebuf,
224 static int (*record_beneath_to_insert_breakpoint) (struct gdbarch *,
225 struct bp_target_info *);
226 static int (*record_beneath_to_remove_breakpoint) (struct gdbarch *,
227 struct bp_target_info *);
229 /* Alloc and free functions for record_reg, record_mem, and record_end
232 /* Alloc a record_reg record entry. */
234 static inline struct record_entry *
235 record_reg_alloc (struct regcache *regcache, int regnum)
237 struct record_entry *rec;
238 struct gdbarch *gdbarch = get_regcache_arch (regcache);
240 rec = (struct record_entry *) xcalloc (1, sizeof (struct record_entry));
241 rec->type = record_reg;
242 rec->u.reg.num = regnum;
243 rec->u.reg.len = register_size (gdbarch, regnum);
244 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
245 rec->u.reg.u.ptr = (gdb_byte *) xmalloc (rec->u.reg.len);
250 /* Free a record_reg record entry. */
253 record_reg_release (struct record_entry *rec)
255 gdb_assert (rec->type == record_reg);
256 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
257 xfree (rec->u.reg.u.ptr);
261 /* Alloc a record_mem record entry. */
263 static inline struct record_entry *
264 record_mem_alloc (CORE_ADDR addr, int len)
266 struct record_entry *rec;
268 rec = (struct record_entry *) xcalloc (1, sizeof (struct record_entry));
269 rec->type = record_mem;
270 rec->u.mem.addr = addr;
271 rec->u.mem.len = len;
272 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
273 rec->u.mem.u.ptr = (gdb_byte *) xmalloc (len);
278 /* Free a record_mem record entry. */
281 record_mem_release (struct record_entry *rec)
283 gdb_assert (rec->type == record_mem);
284 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
285 xfree (rec->u.mem.u.ptr);
289 /* Alloc a record_end record entry. */
291 static inline struct record_entry *
292 record_end_alloc (void)
294 struct record_entry *rec;
296 rec = (struct record_entry *) xcalloc (1, sizeof (struct record_entry));
297 rec->type = record_end;
302 /* Free a record_end record entry. */
305 record_end_release (struct record_entry *rec)
310 /* Free one record entry, any type.
311 Return entry->type, in case caller wants to know. */
313 static inline enum record_type
314 record_entry_release (struct record_entry *rec)
316 enum record_type type = rec->type;
320 record_reg_release (rec);
323 record_mem_release (rec);
326 record_end_release (rec);
332 /* Free all record entries in list pointed to by REC. */
335 record_list_release (struct record_entry *rec)
346 record_entry_release (rec->next);
349 if (rec == &record_first)
352 record_first.next = NULL;
355 record_entry_release (rec);
358 /* Free all record entries forward of the given list position. */
361 record_list_release_following (struct record_entry *rec)
363 struct record_entry *tmp = rec->next;
369 if (record_entry_release (tmp) == record_end)
378 /* Delete the first instruction from the beginning of the log, to make
379 room for adding a new instruction at the end of the log.
381 Note -- this function does not modify record_insn_num. */
384 record_list_release_first (void)
386 struct record_entry *tmp;
388 if (!record_first.next)
391 /* Loop until a record_end. */
394 /* Cut record_first.next out of the linked list. */
395 tmp = record_first.next;
396 record_first.next = tmp->next;
397 tmp->next->prev = &record_first;
399 /* tmp is now isolated, and can be deleted. */
400 if (record_entry_release (tmp) == record_end)
401 break; /* End loop at first record_end. */
403 if (!record_first.next)
405 gdb_assert (record_insn_num == 1);
406 break; /* End loop when list is empty. */
411 /* Add a struct record_entry to record_arch_list. */
414 record_arch_list_add (struct record_entry *rec)
416 if (record_debug > 1)
417 fprintf_unfiltered (gdb_stdlog,
418 "Process record: record_arch_list_add %s.\n",
419 host_address_to_string (rec));
421 if (record_arch_list_tail)
423 record_arch_list_tail->next = rec;
424 rec->prev = record_arch_list_tail;
425 record_arch_list_tail = rec;
429 record_arch_list_head = rec;
430 record_arch_list_tail = rec;
434 /* Return the value storage location of a record entry. */
435 static inline gdb_byte *
436 record_get_loc (struct record_entry *rec)
440 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
441 return rec->u.mem.u.ptr;
443 return rec->u.mem.u.buf;
445 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
446 return rec->u.reg.u.ptr;
448 return rec->u.reg.u.buf;
456 /* Record the value of a register NUM to record_arch_list. */
459 record_arch_list_add_reg (struct regcache *regcache, int regnum)
461 struct record_entry *rec;
463 if (record_debug > 1)
464 fprintf_unfiltered (gdb_stdlog,
465 "Process record: add register num = %d to "
469 rec = record_reg_alloc (regcache, regnum);
471 regcache_raw_read (regcache, regnum, record_get_loc (rec));
473 record_arch_list_add (rec);
478 /* Record the value of a region of memory whose address is ADDR and
479 length is LEN to record_arch_list. */
482 record_arch_list_add_mem (CORE_ADDR addr, int len)
484 struct record_entry *rec;
486 if (record_debug > 1)
487 fprintf_unfiltered (gdb_stdlog,
488 "Process record: add mem addr = %s len = %d to "
490 paddress (target_gdbarch, addr), len);
492 if (!addr) /* FIXME: Why? Some arch must permit it... */
495 rec = record_mem_alloc (addr, len);
497 if (target_read_memory (addr, record_get_loc (rec), len))
500 fprintf_unfiltered (gdb_stdlog,
501 "Process record: error reading memory at "
502 "addr = %s len = %d.\n",
503 paddress (target_gdbarch, addr), len);
504 record_mem_release (rec);
508 record_arch_list_add (rec);
513 /* Add a record_end type struct record_entry to record_arch_list. */
516 record_arch_list_add_end (void)
518 struct record_entry *rec;
520 if (record_debug > 1)
521 fprintf_unfiltered (gdb_stdlog,
522 "Process record: add end to arch list.\n");
524 rec = record_end_alloc ();
525 rec->u.end.sigval = TARGET_SIGNAL_0;
526 rec->u.end.insn_num = ++record_insn_count;
528 record_arch_list_add (rec);
534 record_check_insn_num (int set_terminal)
536 if (record_insn_max_num)
538 gdb_assert (record_insn_num <= record_insn_max_num);
539 if (record_insn_num == record_insn_max_num)
541 /* Ask user what to do. */
542 if (record_stop_at_limit)
546 target_terminal_ours ();
547 q = yquery (_("Do you want to auto delete previous execution "
548 "log entries when record/replay buffer becomes "
549 "full (record stop-at-limit)?"));
551 target_terminal_inferior ();
553 record_stop_at_limit = 0;
555 error (_("Process record: stopped by user."));
562 record_arch_list_cleanups (void *ignore)
564 record_list_release (record_arch_list_tail);
567 /* Before inferior step (when GDB record the running message, inferior
568 only can step), GDB will call this function to record the values to
569 record_list. This function will call gdbarch_process_record to
570 record the running message of inferior and set them to
571 record_arch_list, and add it to record_list. */
573 struct record_message_args {
574 struct regcache *regcache;
575 enum target_signal signal;
579 record_message (void *args)
582 struct record_message_args *myargs = args;
583 struct gdbarch *gdbarch = get_regcache_arch (myargs->regcache);
584 struct cleanup *old_cleanups = make_cleanup (record_arch_list_cleanups, 0);
586 record_arch_list_head = NULL;
587 record_arch_list_tail = NULL;
589 /* Check record_insn_num. */
590 record_check_insn_num (1);
592 /* If gdb sends a signal value to target_resume,
593 save it in the 'end' field of the previous instruction.
595 Maybe process record should record what really happened,
596 rather than what gdb pretends has happened.
598 So if Linux delivered the signal to the child process during
599 the record mode, we will record it and deliver it again in
602 If user says "ignore this signal" during the record mode, then
603 it will be ignored again during the replay mode (no matter if
604 the user says something different, like "deliver this signal"
605 during the replay mode).
607 User should understand that nothing he does during the replay
608 mode will change the behavior of the child. If he tries,
609 then that is a user error.
611 But we should still deliver the signal to gdb during the replay,
612 if we delivered it during the recording. Therefore we should
613 record the signal during record_wait, not record_resume. */
614 if (record_list != &record_first) /* FIXME better way to check */
616 gdb_assert (record_list->type == record_end);
617 record_list->u.end.sigval = myargs->signal;
620 if (myargs->signal == TARGET_SIGNAL_0
621 || !gdbarch_process_record_signal_p (gdbarch))
622 ret = gdbarch_process_record (gdbarch,
624 regcache_read_pc (myargs->regcache));
626 ret = gdbarch_process_record_signal (gdbarch,
631 error (_("Process record: inferior program stopped."));
633 error (_("Process record: failed to record execution log."));
635 discard_cleanups (old_cleanups);
637 record_list->next = record_arch_list_head;
638 record_arch_list_head->prev = record_list;
639 record_list = record_arch_list_tail;
641 if (record_insn_num == record_insn_max_num && record_insn_max_num)
642 record_list_release_first ();
650 do_record_message (struct regcache *regcache,
651 enum target_signal signal)
653 struct record_message_args args;
655 args.regcache = regcache;
656 args.signal = signal;
657 return catch_errors (record_message, &args, NULL, RETURN_MASK_ALL);
660 /* Set to 1 if record_store_registers and record_xfer_partial
661 doesn't need record. */
663 static int record_gdb_operation_disable = 0;
666 record_gdb_operation_disable_set (void)
668 struct cleanup *old_cleanups = NULL;
671 make_cleanup_restore_integer (&record_gdb_operation_disable);
672 record_gdb_operation_disable = 1;
677 /* Execute one instruction from the record log. Each instruction in
678 the log will be represented by an arbitrary sequence of register
679 entries and memory entries, followed by an 'end' entry. */
682 record_exec_insn (struct regcache *regcache, struct gdbarch *gdbarch,
683 struct record_entry *entry)
687 case record_reg: /* reg */
689 gdb_byte reg[MAX_REGISTER_SIZE];
691 if (record_debug > 1)
692 fprintf_unfiltered (gdb_stdlog,
693 "Process record: record_reg %s to "
694 "inferior num = %d.\n",
695 host_address_to_string (entry),
698 regcache_cooked_read (regcache, entry->u.reg.num, reg);
699 regcache_cooked_write (regcache, entry->u.reg.num,
700 record_get_loc (entry));
701 memcpy (record_get_loc (entry), reg, entry->u.reg.len);
705 case record_mem: /* mem */
707 /* Nothing to do if the entry is flagged not_accessible. */
708 if (!entry->u.mem.mem_entry_not_accessible)
710 gdb_byte *mem = alloca (entry->u.mem.len);
712 if (record_debug > 1)
713 fprintf_unfiltered (gdb_stdlog,
714 "Process record: record_mem %s to "
715 "inferior addr = %s len = %d.\n",
716 host_address_to_string (entry),
717 paddress (gdbarch, entry->u.mem.addr),
720 if (target_read_memory (entry->u.mem.addr, mem, entry->u.mem.len))
722 entry->u.mem.mem_entry_not_accessible = 1;
724 warning ("Process record: error reading memory at "
725 "addr = %s len = %d.",
726 paddress (gdbarch, entry->u.mem.addr),
731 if (target_write_memory (entry->u.mem.addr,
732 record_get_loc (entry),
735 entry->u.mem.mem_entry_not_accessible = 1;
737 warning ("Process record: error writing memory at "
738 "addr = %s len = %d.",
739 paddress (gdbarch, entry->u.mem.addr),
743 memcpy (record_get_loc (entry), mem, entry->u.mem.len);
751 static struct target_ops *tmp_to_resume_ops;
752 static void (*tmp_to_resume) (struct target_ops *, ptid_t, int,
754 static struct target_ops *tmp_to_wait_ops;
755 static ptid_t (*tmp_to_wait) (struct target_ops *, ptid_t,
756 struct target_waitstatus *,
758 static struct target_ops *tmp_to_store_registers_ops;
759 static void (*tmp_to_store_registers) (struct target_ops *,
762 static struct target_ops *tmp_to_xfer_partial_ops;
763 static LONGEST (*tmp_to_xfer_partial) (struct target_ops *ops,
764 enum target_object object,
767 const gdb_byte *writebuf,
770 static int (*tmp_to_insert_breakpoint) (struct gdbarch *,
771 struct bp_target_info *);
772 static int (*tmp_to_remove_breakpoint) (struct gdbarch *,
773 struct bp_target_info *);
775 static void record_restore (void);
777 /* Open the process record target. */
780 record_core_open_1 (char *name, int from_tty)
782 struct regcache *regcache = get_current_regcache ();
783 int regnum = gdbarch_num_regs (get_regcache_arch (regcache));
786 /* Get record_core_regbuf. */
787 target_fetch_registers (regcache, -1);
788 record_core_regbuf = xmalloc (MAX_REGISTER_SIZE * regnum);
789 for (i = 0; i < regnum; i ++)
790 regcache_raw_collect (regcache, i,
791 record_core_regbuf + MAX_REGISTER_SIZE * i);
793 /* Get record_core_start and record_core_end. */
794 if (build_section_table (core_bfd, &record_core_start, &record_core_end))
796 xfree (record_core_regbuf);
797 record_core_regbuf = NULL;
798 error (_("\"%s\": Can't find sections: %s"),
799 bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ()));
802 push_target (&record_core_ops);
806 /* "to_open" target method for 'live' processes. */
809 record_open_1 (char *name, int from_tty)
811 struct target_ops *t;
814 fprintf_unfiltered (gdb_stdlog, "Process record: record_open\n");
817 if (!target_has_execution)
818 error (_("Process record: the program is not being run."));
820 error (_("Process record target can't debug inferior in non-stop mode "
822 if (target_async_permitted)
823 error (_("Process record target can't debug inferior in asynchronous "
824 "mode (target-async)."));
826 if (!gdbarch_process_record_p (target_gdbarch))
827 error (_("Process record: the current architecture doesn't support "
828 "record function."));
831 error (_("Could not find 'to_resume' method on the target stack."));
833 error (_("Could not find 'to_wait' method on the target stack."));
834 if (!tmp_to_store_registers)
835 error (_("Could not find 'to_store_registers' method on the target stack."));
836 if (!tmp_to_insert_breakpoint)
837 error (_("Could not find 'to_insert_breakpoint' method on the target stack."));
838 if (!tmp_to_remove_breakpoint)
839 error (_("Could not find 'to_remove_breakpoint' method on the target stack."));
841 push_target (&record_ops);
844 /* "to_open" target method. Open the process record target. */
847 record_open (char *name, int from_tty)
849 struct target_ops *t;
852 fprintf_unfiltered (gdb_stdlog, "Process record: record_open\n");
854 /* Check if record target is already running. */
855 if (current_target.to_stratum == record_stratum)
856 error (_("Process record target already running. Use \"record stop\" to "
857 "stop record target first."));
859 /* Reset the tmp beneath pointers. */
860 tmp_to_resume_ops = NULL;
861 tmp_to_resume = NULL;
862 tmp_to_wait_ops = NULL;
864 tmp_to_store_registers_ops = NULL;
865 tmp_to_store_registers = NULL;
866 tmp_to_xfer_partial_ops = NULL;
867 tmp_to_xfer_partial = NULL;
868 tmp_to_insert_breakpoint = NULL;
869 tmp_to_remove_breakpoint = NULL;
871 /* Set the beneath function pointers. */
872 for (t = current_target.beneath; t != NULL; t = t->beneath)
876 tmp_to_resume = t->to_resume;
877 tmp_to_resume_ops = t;
881 tmp_to_wait = t->to_wait;
884 if (!tmp_to_store_registers)
886 tmp_to_store_registers = t->to_store_registers;
887 tmp_to_store_registers_ops = t;
889 if (!tmp_to_xfer_partial)
891 tmp_to_xfer_partial = t->to_xfer_partial;
892 tmp_to_xfer_partial_ops = t;
894 if (!tmp_to_insert_breakpoint)
895 tmp_to_insert_breakpoint = t->to_insert_breakpoint;
896 if (!tmp_to_remove_breakpoint)
897 tmp_to_remove_breakpoint = t->to_remove_breakpoint;
899 if (!tmp_to_xfer_partial)
900 error (_("Could not find 'to_xfer_partial' method on the target stack."));
904 record_insn_count = 0;
905 record_list = &record_first;
906 record_list->next = NULL;
908 /* Set the tmp beneath pointers to beneath pointers. */
909 record_beneath_to_resume_ops = tmp_to_resume_ops;
910 record_beneath_to_resume = tmp_to_resume;
911 record_beneath_to_wait_ops = tmp_to_wait_ops;
912 record_beneath_to_wait = tmp_to_wait;
913 record_beneath_to_store_registers_ops = tmp_to_store_registers_ops;
914 record_beneath_to_store_registers = tmp_to_store_registers;
915 record_beneath_to_xfer_partial_ops = tmp_to_xfer_partial_ops;
916 record_beneath_to_xfer_partial = tmp_to_xfer_partial;
917 record_beneath_to_insert_breakpoint = tmp_to_insert_breakpoint;
918 record_beneath_to_remove_breakpoint = tmp_to_remove_breakpoint;
920 if (current_target.to_stratum == core_stratum)
921 record_core_open_1 (name, from_tty);
923 record_open_1 (name, from_tty);
926 /* "to_close" target method. Close the process record target. */
929 record_close (int quitting)
931 struct record_core_buf_entry *entry;
934 fprintf_unfiltered (gdb_stdlog, "Process record: record_close\n");
936 record_list_release (record_list);
938 /* Release record_core_regbuf. */
939 if (record_core_regbuf)
941 xfree (record_core_regbuf);
942 record_core_regbuf = NULL;
945 /* Release record_core_buf_list. */
946 if (record_core_buf_list)
948 for (entry = record_core_buf_list->prev; entry; entry = entry->prev)
950 xfree (record_core_buf_list);
951 record_core_buf_list = entry;
953 record_core_buf_list = NULL;
957 static int record_resume_step = 0;
958 static int record_resume_error;
960 /* "to_resume" target method. Resume the process record target. */
963 record_resume (struct target_ops *ops, ptid_t ptid, int step,
964 enum target_signal signal)
966 record_resume_step = step;
968 if (!RECORD_IS_REPLAY)
970 if (do_record_message (get_current_regcache (), signal))
972 record_resume_error = 0;
976 record_resume_error = 1;
979 record_beneath_to_resume (record_beneath_to_resume_ops, ptid, 1,
984 static int record_get_sig = 0;
986 /* SIGINT signal handler, registered by "to_wait" method. */
989 record_sig_handler (int signo)
992 fprintf_unfiltered (gdb_stdlog, "Process record: get a signal\n");
994 /* It will break the running inferior in replay mode. */
995 record_resume_step = 1;
997 /* It will let record_wait set inferior status to get the signal
1003 record_wait_cleanups (void *ignore)
1005 if (execution_direction == EXEC_REVERSE)
1007 if (record_list->next)
1008 record_list = record_list->next;
1011 record_list = record_list->prev;
1014 /* "to_wait" target method for process record target.
1016 In record mode, the target is always run in singlestep mode
1017 (even when gdb says to continue). The to_wait method intercepts
1018 the stop events and determines which ones are to be passed on to
1019 gdb. Most stop events are just singlestep events that gdb is not
1020 to know about, so the to_wait method just records them and keeps
1023 In replay mode, this function emulates the recorded execution log,
1024 one instruction at a time (forward or backward), and determines
1028 record_wait (struct target_ops *ops,
1029 ptid_t ptid, struct target_waitstatus *status,
1032 struct cleanup *set_cleanups = record_gdb_operation_disable_set ();
1035 fprintf_unfiltered (gdb_stdlog,
1036 "Process record: record_wait "
1037 "record_resume_step = %d\n",
1038 record_resume_step);
1040 if (!RECORD_IS_REPLAY && ops != &record_core_ops)
1042 if (record_resume_error)
1044 /* If record_resume get error, return directly. */
1045 status->kind = TARGET_WAITKIND_STOPPED;
1046 status->value.sig = TARGET_SIGNAL_ABRT;
1047 return inferior_ptid;
1050 if (record_resume_step)
1052 /* This is a single step. */
1053 return record_beneath_to_wait (record_beneath_to_wait_ops,
1054 ptid, status, options);
1058 /* This is not a single step. */
1064 ret = record_beneath_to_wait (record_beneath_to_wait_ops,
1065 ptid, status, options);
1067 /* Is this a SIGTRAP? */
1068 if (status->kind == TARGET_WAITKIND_STOPPED
1069 && status->value.sig == TARGET_SIGNAL_TRAP)
1071 struct regcache *regcache;
1073 /* Yes -- check if there is a breakpoint. */
1074 registers_changed ();
1075 regcache = get_current_regcache ();
1076 tmp_pc = regcache_read_pc (regcache);
1077 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache),
1080 /* There is a breakpoint. GDB will want to stop. */
1081 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1082 CORE_ADDR decr_pc_after_break
1083 = gdbarch_decr_pc_after_break (gdbarch);
1084 if (decr_pc_after_break)
1085 regcache_write_pc (regcache,
1086 tmp_pc + decr_pc_after_break);
1090 /* There is not a breakpoint, and gdb is not
1091 stepping, therefore gdb will not stop.
1092 Therefore we will not return to gdb.
1093 Record the insn and resume. */
1094 if (!do_record_message (regcache, TARGET_SIGNAL_0))
1097 record_beneath_to_resume (record_beneath_to_resume_ops,
1104 /* The inferior is broken by a breakpoint or a signal. */
1113 struct regcache *regcache = get_current_regcache ();
1114 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1115 int continue_flag = 1;
1116 int first_record_end = 1;
1117 struct cleanup *old_cleanups = make_cleanup (record_wait_cleanups, 0);
1120 status->kind = TARGET_WAITKIND_STOPPED;
1122 /* Check breakpoint when forward execute. */
1123 if (execution_direction == EXEC_FORWARD)
1125 tmp_pc = regcache_read_pc (regcache);
1126 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache),
1130 fprintf_unfiltered (gdb_stdlog,
1131 "Process record: break at %s.\n",
1132 paddress (gdbarch, tmp_pc));
1133 if (gdbarch_decr_pc_after_break (gdbarch)
1134 && !record_resume_step)
1135 regcache_write_pc (regcache,
1137 gdbarch_decr_pc_after_break (gdbarch));
1143 signal (SIGINT, record_sig_handler);
1144 /* If GDB is in terminal_inferior mode, it will not get the signal.
1145 And in GDB replay mode, GDB doesn't need to be in terminal_inferior
1146 mode, because inferior will not executed.
1147 Then set it to terminal_ours to make GDB get the signal. */
1148 target_terminal_ours ();
1150 /* In EXEC_FORWARD mode, record_list points to the tail of prev
1152 if (execution_direction == EXEC_FORWARD && record_list->next)
1153 record_list = record_list->next;
1155 /* Loop over the record_list, looking for the next place to
1159 /* Check for beginning and end of log. */
1160 if (execution_direction == EXEC_REVERSE
1161 && record_list == &record_first)
1163 /* Hit beginning of record log in reverse. */
1164 status->kind = TARGET_WAITKIND_NO_HISTORY;
1167 if (execution_direction != EXEC_REVERSE && !record_list->next)
1169 /* Hit end of record log going forward. */
1170 status->kind = TARGET_WAITKIND_NO_HISTORY;
1174 record_exec_insn (regcache, gdbarch, record_list);
1176 if (record_list->type == record_end)
1178 if (record_debug > 1)
1179 fprintf_unfiltered (gdb_stdlog,
1180 "Process record: record_end %s to "
1182 host_address_to_string (record_list));
1184 if (first_record_end && execution_direction == EXEC_REVERSE)
1186 /* When reverse excute, the first record_end is the part of
1187 current instruction. */
1188 first_record_end = 0;
1192 /* In EXEC_REVERSE mode, this is the record_end of prev
1194 In EXEC_FORWARD mode, this is the record_end of current
1197 if (record_resume_step)
1199 if (record_debug > 1)
1200 fprintf_unfiltered (gdb_stdlog,
1201 "Process record: step.\n");
1205 /* check breakpoint */
1206 tmp_pc = regcache_read_pc (regcache);
1207 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache),
1211 fprintf_unfiltered (gdb_stdlog,
1212 "Process record: break "
1214 paddress (gdbarch, tmp_pc));
1215 if (gdbarch_decr_pc_after_break (gdbarch)
1216 && execution_direction == EXEC_FORWARD
1217 && !record_resume_step)
1218 regcache_write_pc (regcache,
1220 gdbarch_decr_pc_after_break (gdbarch));
1223 /* Check target signal */
1224 if (record_list->u.end.sigval != TARGET_SIGNAL_0)
1225 /* FIXME: better way to check */
1232 if (execution_direction == EXEC_REVERSE)
1234 if (record_list->prev)
1235 record_list = record_list->prev;
1239 if (record_list->next)
1240 record_list = record_list->next;
1244 while (continue_flag);
1246 signal (SIGINT, handle_sigint);
1250 status->value.sig = TARGET_SIGNAL_INT;
1251 else if (record_list->u.end.sigval != TARGET_SIGNAL_0)
1252 /* FIXME: better way to check */
1253 status->value.sig = record_list->u.end.sigval;
1255 status->value.sig = TARGET_SIGNAL_TRAP;
1257 discard_cleanups (old_cleanups);
1260 do_cleanups (set_cleanups);
1261 return inferior_ptid;
1264 /* "to_disconnect" method for process record target. */
1267 record_disconnect (struct target_ops *target, char *args, int from_tty)
1270 fprintf_unfiltered (gdb_stdlog, "Process record: record_disconnect\n");
1272 unpush_target (&record_ops);
1273 target_disconnect (args, from_tty);
1276 /* "to_detach" method for process record target. */
1279 record_detach (struct target_ops *ops, char *args, int from_tty)
1282 fprintf_unfiltered (gdb_stdlog, "Process record: record_detach\n");
1284 unpush_target (&record_ops);
1285 target_detach (args, from_tty);
1288 /* "to_mourn_inferior" method for process record target. */
1291 record_mourn_inferior (struct target_ops *ops)
1294 fprintf_unfiltered (gdb_stdlog, "Process record: "
1295 "record_mourn_inferior\n");
1297 unpush_target (&record_ops);
1298 target_mourn_inferior ();
1301 /* Close process record target before killing the inferior process. */
1304 record_kill (struct target_ops *ops)
1307 fprintf_unfiltered (gdb_stdlog, "Process record: record_kill\n");
1309 unpush_target (&record_ops);
1313 /* Record registers change (by user or by GDB) to list as an instruction. */
1316 record_registers_change (struct regcache *regcache, int regnum)
1318 /* Check record_insn_num. */
1319 record_check_insn_num (0);
1321 record_arch_list_head = NULL;
1322 record_arch_list_tail = NULL;
1327 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
1329 if (record_arch_list_add_reg (regcache, i))
1331 record_list_release (record_arch_list_tail);
1332 error (_("Process record: failed to record execution log."));
1338 if (record_arch_list_add_reg (regcache, regnum))
1340 record_list_release (record_arch_list_tail);
1341 error (_("Process record: failed to record execution log."));
1344 if (record_arch_list_add_end ())
1346 record_list_release (record_arch_list_tail);
1347 error (_("Process record: failed to record execution log."));
1349 record_list->next = record_arch_list_head;
1350 record_arch_list_head->prev = record_list;
1351 record_list = record_arch_list_tail;
1353 if (record_insn_num == record_insn_max_num && record_insn_max_num)
1354 record_list_release_first ();
1359 /* "to_store_registers" method for process record target. */
1362 record_store_registers (struct target_ops *ops, struct regcache *regcache,
1365 if (!record_gdb_operation_disable)
1367 if (RECORD_IS_REPLAY)
1371 /* Let user choose if he wants to write register or not. */
1374 query (_("Because GDB is in replay mode, changing the "
1375 "value of a register will make the execution "
1376 "log unusable from this point onward. "
1377 "Change all registers?"));
1380 query (_("Because GDB is in replay mode, changing the value "
1381 "of a register will make the execution log unusable "
1382 "from this point onward. Change register %s?"),
1383 gdbarch_register_name (get_regcache_arch (regcache),
1388 /* Invalidate the value of regcache that was set in function
1389 "regcache_raw_write". */
1394 i < gdbarch_num_regs (get_regcache_arch (regcache));
1396 regcache_invalidate (regcache, i);
1399 regcache_invalidate (regcache, regno);
1401 error (_("Process record canceled the operation."));
1404 /* Destroy the record from here forward. */
1405 record_list_release_following (record_list);
1408 record_registers_change (regcache, regno);
1410 record_beneath_to_store_registers (record_beneath_to_store_registers_ops,
1414 /* "to_xfer_partial" method. Behavior is conditional on RECORD_IS_REPLAY.
1415 In replay mode, we cannot write memory unles we are willing to
1416 invalidate the record/replay log from this point forward. */
1419 record_xfer_partial (struct target_ops *ops, enum target_object object,
1420 const char *annex, gdb_byte *readbuf,
1421 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
1423 if (!record_gdb_operation_disable
1424 && (object == TARGET_OBJECT_MEMORY
1425 || object == TARGET_OBJECT_RAW_MEMORY) && writebuf)
1427 if (RECORD_IS_REPLAY)
1429 /* Let user choose if he wants to write memory or not. */
1430 if (!query (_("Because GDB is in replay mode, writing to memory "
1431 "will make the execution log unusable from this "
1432 "point onward. Write memory at address %s?"),
1433 paddress (target_gdbarch, offset)))
1434 error (_("Process record canceled the operation."));
1436 /* Destroy the record from here forward. */
1437 record_list_release_following (record_list);
1440 /* Check record_insn_num */
1441 record_check_insn_num (0);
1443 /* Record registers change to list as an instruction. */
1444 record_arch_list_head = NULL;
1445 record_arch_list_tail = NULL;
1446 if (record_arch_list_add_mem (offset, len))
1448 record_list_release (record_arch_list_tail);
1450 fprintf_unfiltered (gdb_stdlog,
1451 "Process record: failed to record "
1455 if (record_arch_list_add_end ())
1457 record_list_release (record_arch_list_tail);
1459 fprintf_unfiltered (gdb_stdlog,
1460 "Process record: failed to record "
1464 record_list->next = record_arch_list_head;
1465 record_arch_list_head->prev = record_list;
1466 record_list = record_arch_list_tail;
1468 if (record_insn_num == record_insn_max_num && record_insn_max_num)
1469 record_list_release_first ();
1474 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops,
1475 object, annex, readbuf, writebuf,
1479 /* Behavior is conditional on RECORD_IS_REPLAY.
1480 We will not actually insert or remove breakpoints when replaying,
1481 nor when recording. */
1484 record_insert_breakpoint (struct gdbarch *gdbarch,
1485 struct bp_target_info *bp_tgt)
1487 if (!RECORD_IS_REPLAY)
1489 struct cleanup *old_cleanups = record_gdb_operation_disable_set ();
1490 int ret = record_beneath_to_insert_breakpoint (gdbarch, bp_tgt);
1492 do_cleanups (old_cleanups);
1500 /* "to_remove_breakpoint" method for process record target. */
1503 record_remove_breakpoint (struct gdbarch *gdbarch,
1504 struct bp_target_info *bp_tgt)
1506 if (!RECORD_IS_REPLAY)
1508 struct cleanup *old_cleanups = record_gdb_operation_disable_set ();
1509 int ret = record_beneath_to_remove_breakpoint (gdbarch, bp_tgt);
1511 do_cleanups (old_cleanups);
1519 /* "to_can_execute_reverse" method for process record target. */
1522 record_can_execute_reverse (void)
1528 init_record_ops (void)
1530 record_ops.to_shortname = "record";
1531 record_ops.to_longname = "Process record and replay target";
1533 "Log program while executing and replay execution from log.";
1534 record_ops.to_open = record_open;
1535 record_ops.to_close = record_close;
1536 record_ops.to_resume = record_resume;
1537 record_ops.to_wait = record_wait;
1538 record_ops.to_disconnect = record_disconnect;
1539 record_ops.to_detach = record_detach;
1540 record_ops.to_mourn_inferior = record_mourn_inferior;
1541 record_ops.to_kill = record_kill;
1542 record_ops.to_create_inferior = find_default_create_inferior;
1543 record_ops.to_store_registers = record_store_registers;
1544 record_ops.to_xfer_partial = record_xfer_partial;
1545 record_ops.to_insert_breakpoint = record_insert_breakpoint;
1546 record_ops.to_remove_breakpoint = record_remove_breakpoint;
1547 record_ops.to_can_execute_reverse = record_can_execute_reverse;
1548 record_ops.to_stratum = record_stratum;
1549 record_ops.to_magic = OPS_MAGIC;
1552 /* "to_resume" method for prec over corefile. */
1555 record_core_resume (struct target_ops *ops, ptid_t ptid, int step,
1556 enum target_signal signal)
1558 record_resume_step = step;
1561 /* "to_kill" method for prec over corefile. */
1564 record_core_kill (struct target_ops *ops)
1567 fprintf_unfiltered (gdb_stdlog, "Process record: record_core_kill\n");
1569 unpush_target (&record_core_ops);
1572 /* "to_fetch_registers" method for prec over corefile. */
1575 record_core_fetch_registers (struct target_ops *ops,
1576 struct regcache *regcache,
1581 int num = gdbarch_num_regs (get_regcache_arch (regcache));
1584 for (i = 0; i < num; i ++)
1585 regcache_raw_supply (regcache, i,
1586 record_core_regbuf + MAX_REGISTER_SIZE * i);
1589 regcache_raw_supply (regcache, regno,
1590 record_core_regbuf + MAX_REGISTER_SIZE * regno);
1593 /* "to_prepare_to_store" method for prec over corefile. */
1596 record_core_prepare_to_store (struct regcache *regcache)
1600 /* "to_store_registers" method for prec over corefile. */
1603 record_core_store_registers (struct target_ops *ops,
1604 struct regcache *regcache,
1607 if (record_gdb_operation_disable)
1608 regcache_raw_collect (regcache, regno,
1609 record_core_regbuf + MAX_REGISTER_SIZE * regno);
1611 error (_("You can't do that without a process to debug."));
1614 /* "to_xfer_partial" method for prec over corefile. */
1617 record_core_xfer_partial (struct target_ops *ops, enum target_object object,
1618 const char *annex, gdb_byte *readbuf,
1619 const gdb_byte *writebuf, ULONGEST offset,
1622 if (object == TARGET_OBJECT_MEMORY)
1624 if (record_gdb_operation_disable || !writebuf)
1626 struct target_section *p;
1627 for (p = record_core_start; p < record_core_end; p++)
1629 if (offset >= p->addr)
1631 struct record_core_buf_entry *entry;
1633 if (offset >= p->endaddr)
1636 if (offset + len > p->endaddr)
1637 len = p->endaddr - offset;
1641 /* Read readbuf or write writebuf p, offset, len. */
1643 if (p->the_bfd_section->flags & SEC_CONSTRUCTOR
1644 || (p->the_bfd_section->flags & SEC_HAS_CONTENTS) == 0)
1647 memset (readbuf, 0, len);
1650 /* Get record_core_buf_entry. */
1651 for (entry = record_core_buf_list; entry;
1652 entry = entry->prev)
1659 /* Add a new entry. */
1661 = (struct record_core_buf_entry *)
1663 (sizeof (struct record_core_buf_entry));
1665 if (!bfd_malloc_and_get_section (p->bfd,
1672 entry->prev = record_core_buf_list;
1673 record_core_buf_list = entry;
1676 memcpy (entry->buf + offset, writebuf, (size_t) len);
1681 return record_beneath_to_xfer_partial
1682 (record_beneath_to_xfer_partial_ops,
1683 object, annex, readbuf, writebuf,
1686 memcpy (readbuf, entry->buf + offset, (size_t) len);
1696 error (_("You can't do that without a process to debug."));
1699 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops,
1700 object, annex, readbuf, writebuf,
1704 /* "to_insert_breakpoint" method for prec over corefile. */
1707 record_core_insert_breakpoint (struct gdbarch *gdbarch,
1708 struct bp_target_info *bp_tgt)
1713 /* "to_remove_breakpoint" method for prec over corefile. */
1716 record_core_remove_breakpoint (struct gdbarch *gdbarch,
1717 struct bp_target_info *bp_tgt)
1722 /* "to_has_execution" method for prec over corefile. */
1725 record_core_has_execution (struct target_ops *ops)
1731 init_record_core_ops (void)
1733 record_core_ops.to_shortname = "record_core";
1734 record_core_ops.to_longname = "Process record and replay target";
1735 record_core_ops.to_doc =
1736 "Log program while executing and replay execution from log.";
1737 record_core_ops.to_open = record_open;
1738 record_core_ops.to_close = record_close;
1739 record_core_ops.to_resume = record_core_resume;
1740 record_core_ops.to_wait = record_wait;
1741 record_core_ops.to_kill = record_core_kill;
1742 record_core_ops.to_fetch_registers = record_core_fetch_registers;
1743 record_core_ops.to_prepare_to_store = record_core_prepare_to_store;
1744 record_core_ops.to_store_registers = record_core_store_registers;
1745 record_core_ops.to_xfer_partial = record_core_xfer_partial;
1746 record_core_ops.to_insert_breakpoint = record_core_insert_breakpoint;
1747 record_core_ops.to_remove_breakpoint = record_core_remove_breakpoint;
1748 record_core_ops.to_can_execute_reverse = record_can_execute_reverse;
1749 record_core_ops.to_has_execution = record_core_has_execution;
1750 record_core_ops.to_stratum = record_stratum;
1751 record_core_ops.to_magic = OPS_MAGIC;
1754 /* Implement "show record debug" command. */
1757 show_record_debug (struct ui_file *file, int from_tty,
1758 struct cmd_list_element *c, const char *value)
1760 fprintf_filtered (file, _("Debugging of process record target is %s.\n"),
1764 /* Alias for "target record". */
1767 cmd_record_start (char *args, int from_tty)
1769 execute_command ("target record", from_tty);
1772 /* Truncate the record log from the present point
1773 of replay until the end. */
1776 cmd_record_delete (char *args, int from_tty)
1778 if (current_target.to_stratum == record_stratum)
1780 if (RECORD_IS_REPLAY)
1782 if (!from_tty || query (_("Delete the log from this point forward "
1783 "and begin to record the running message "
1785 record_list_release_following (record_list);
1788 printf_unfiltered (_("Already at end of record list.\n"));
1792 printf_unfiltered (_("Process record is not started.\n"));
1795 /* Implement the "stoprecord" or "record stop" command. */
1798 cmd_record_stop (char *args, int from_tty)
1800 if (current_target.to_stratum == record_stratum)
1802 unpush_target (&record_ops);
1803 printf_unfiltered (_("Process record is stopped and all execution "
1804 "logs are deleted.\n"));
1807 printf_unfiltered (_("Process record is not started.\n"));
1810 /* Set upper limit of record log size. */
1813 set_record_insn_max_num (char *args, int from_tty, struct cmd_list_element *c)
1815 if (record_insn_num > record_insn_max_num && record_insn_max_num)
1817 /* Count down record_insn_num while releasing records from list. */
1818 while (record_insn_num > record_insn_max_num)
1820 record_list_release_first ();
1826 static struct cmd_list_element *record_cmdlist, *set_record_cmdlist,
1827 *show_record_cmdlist, *info_record_cmdlist;
1830 set_record_command (char *args, int from_tty)
1832 printf_unfiltered (_("\
1833 \"set record\" must be followed by an apporpriate subcommand.\n"));
1834 help_list (set_record_cmdlist, "set record ", all_commands, gdb_stdout);
1838 show_record_command (char *args, int from_tty)
1840 cmd_show_list (show_record_cmdlist, from_tty, "");
1843 /* Display some statistics about the execution log. */
1846 info_record_command (char *args, int from_tty)
1848 struct record_entry *p;
1850 if (current_target.to_stratum == record_stratum)
1852 if (RECORD_IS_REPLAY)
1853 printf_filtered (_("Replay mode:\n"));
1855 printf_filtered (_("Record mode:\n"));
1857 /* Find entry for first actual instruction in the log. */
1858 for (p = record_first.next;
1859 p != NULL && p->type != record_end;
1863 /* Do we have a log at all? */
1864 if (p != NULL && p->type == record_end)
1866 /* Display instruction number for first instruction in the log. */
1867 printf_filtered (_("Lowest recorded instruction number is %s.\n"),
1868 pulongest (p->u.end.insn_num));
1870 /* If in replay mode, display where we are in the log. */
1871 if (RECORD_IS_REPLAY)
1872 printf_filtered (_("Current instruction number is %s.\n"),
1873 pulongest (record_list->u.end.insn_num));
1875 /* Display instruction number for last instruction in the log. */
1876 printf_filtered (_("Highest recorded instruction number is %s.\n"),
1877 pulongest (record_insn_count));
1879 /* Display log count. */
1880 printf_filtered (_("Log contains %d instructions.\n"),
1885 printf_filtered (_("No instructions have been logged.\n"));
1890 printf_filtered (_("target record is not active.\n"));
1893 /* Display max log size. */
1894 printf_filtered (_("Max logged instructions is %d.\n"),
1895 record_insn_max_num);
1898 /* Record log save-file format
1899 Version 1 (never released)
1902 4 bytes: magic number htonl(0x20090829).
1903 NOTE: be sure to change whenever this file format changes!
1907 1 byte: record type (record_end, see enum record_type).
1909 1 byte: record type (record_reg, see enum record_type).
1910 8 bytes: register id (network byte order).
1911 MAX_REGISTER_SIZE bytes: register value.
1913 1 byte: record type (record_mem, see enum record_type).
1914 8 bytes: memory length (network byte order).
1915 8 bytes: memory address (network byte order).
1916 n bytes: memory value (n == memory length).
1919 4 bytes: magic number netorder32(0x20091016).
1920 NOTE: be sure to change whenever this file format changes!
1924 1 byte: record type (record_end, see enum record_type).
1926 4 bytes: instruction count
1928 1 byte: record type (record_reg, see enum record_type).
1929 4 bytes: register id (network byte order).
1930 n bytes: register value (n == actual register size).
1931 (eg. 4 bytes for x86 general registers).
1933 1 byte: record type (record_mem, see enum record_type).
1934 4 bytes: memory length (network byte order).
1935 8 bytes: memory address (network byte order).
1936 n bytes: memory value (n == memory length).
1940 /* bfdcore_read -- read bytes from a core file section. */
1943 bfdcore_read (bfd *obfd, asection *osec, void *buf, int len, int *offset)
1945 int ret = bfd_get_section_contents (obfd, osec, buf, *offset, len);
1950 error (_("Failed to read %d bytes from core file %s ('%s').\n"),
1951 len, bfd_get_filename (obfd),
1952 bfd_errmsg (bfd_get_error ()));
1955 static inline uint64_t
1956 netorder64 (uint64_t fromfile)
1958 return (BYTE_ORDER == BFD_ENDIAN_LITTLE)
1959 ? bswap_64 (fromfile)
1963 static inline uint32_t
1964 netorder32 (uint32_t fromfile)
1966 return (BYTE_ORDER == BFD_ENDIAN_LITTLE)
1967 ? bswap_32 (fromfile)
1971 static inline uint16_t
1972 netorder16 (uint16_t fromfile)
1974 return (BYTE_ORDER == BFD_ENDIAN_LITTLE)
1975 ? bswap_16 (fromfile)
1979 /* Restore the execution log from a core_bfd file. */
1981 record_restore (void)
1984 struct cleanup *old_cleanups;
1985 struct record_entry *rec;
1989 struct regcache *regcache;
1991 /* We restore the execution log from the open core bfd,
1993 if (core_bfd == NULL)
1996 /* "record_restore" can only be called when record list is empty. */
1997 gdb_assert (record_first.next == NULL);
2000 printf_filtered ("Restoring recording from core file.\n");
2002 /* Now need to find our special note section. */
2003 osec = bfd_get_section_by_name (core_bfd, "null0");
2004 osec_size = bfd_section_size (core_bfd, osec);
2006 printf_filtered ("Find precord section %s.\n",
2007 osec ? "succeeded" : "failed");
2011 printf_filtered ("%s", bfd_section_name (core_bfd, osec));
2013 /* Check the magic code. */
2014 bfdcore_read (core_bfd, osec, &magic, sizeof (magic), &bfd_offset);
2015 if (magic != RECORD_FILE_MAGIC)
2016 error (_("Version mis-match or file format error in core file %s."),
2017 bfd_get_filename (core_bfd));
2020 Reading 4-byte magic cookie RECORD_FILE_MAGIC (0x%s)\n",
2021 phex_nz (netorder32 (magic), 4));
2023 /* Restore the entries in recfd into record_arch_list_head and
2024 record_arch_list_tail. */
2025 record_arch_list_head = NULL;
2026 record_arch_list_tail = NULL;
2027 record_insn_num = 0;
2028 old_cleanups = make_cleanup (record_arch_list_cleanups, 0);
2029 regcache = get_current_regcache ();
2035 uint32_t regnum, len, signal, count;
2038 /* We are finished when offset reaches osec_size. */
2039 if (bfd_offset >= osec_size)
2041 bfdcore_read (core_bfd, osec, &tmpu8, sizeof (tmpu8), &bfd_offset);
2045 case record_reg: /* reg */
2046 /* Get register number to regnum. */
2047 bfdcore_read (core_bfd, osec, ®num,
2048 sizeof (regnum), &bfd_offset);
2049 regnum = netorder32 (regnum);
2051 rec = record_reg_alloc (regcache, regnum);
2054 bfdcore_read (core_bfd, osec, record_get_loc (rec),
2055 rec->u.reg.len, &bfd_offset);
2059 Reading register %d (1 plus %lu plus %d bytes)\n",
2061 (unsigned long) sizeof (regnum),
2065 case record_mem: /* mem */
2067 bfdcore_read (core_bfd, osec, &len,
2068 sizeof (len), &bfd_offset);
2069 len = netorder32 (len);
2072 bfdcore_read (core_bfd, osec, &addr,
2073 sizeof (addr), &bfd_offset);
2074 addr = netorder64 (addr);
2076 rec = record_mem_alloc (addr, len);
2079 bfdcore_read (core_bfd, osec, record_get_loc (rec),
2080 rec->u.mem.len, &bfd_offset);
2084 Reading memory %s (1 plus %lu plus %lu plus %d bytes)\n",
2085 paddress (get_current_arch (),
2087 (unsigned long) sizeof (addr),
2088 (unsigned long) sizeof (len),
2092 case record_end: /* end */
2093 rec = record_end_alloc ();
2096 /* Get signal value. */
2097 bfdcore_read (core_bfd, osec, &signal,
2098 sizeof (signal), &bfd_offset);
2099 signal = netorder32 (signal);
2100 rec->u.end.sigval = signal;
2102 /* Get insn count. */
2103 bfdcore_read (core_bfd, osec, &count,
2104 sizeof (count), &bfd_offset);
2105 count = netorder32 (count);
2106 rec->u.end.insn_num = count;
2107 record_insn_count = count + 1;
2110 Reading record_end (1 + %lu + %lu bytes), offset == %s\n",
2111 (unsigned long) sizeof (signal),
2112 (unsigned long) sizeof (count),
2113 paddress (get_current_arch (),
2118 error (_("Bad entry type in core file %s."),
2119 bfd_get_filename (core_bfd));
2123 /* Add rec to record arch list. */
2124 record_arch_list_add (rec);
2127 discard_cleanups (old_cleanups);
2129 /* Add record_arch_list_head to the end of record list. */
2130 record_first.next = record_arch_list_head;
2131 record_arch_list_head->prev = &record_first;
2132 record_arch_list_tail->next = NULL;
2133 record_list = &record_first;
2135 /* Update record_insn_max_num. */
2136 if (record_insn_num > record_insn_max_num)
2138 record_insn_max_num = record_insn_num;
2139 warning (_("Auto increase record/replay buffer limit to %d."),
2140 record_insn_max_num);
2144 printf_filtered (_("Restored records from core file %s.\n"),
2145 bfd_get_filename (core_bfd));
2147 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
2150 /* bfdcore_write -- write bytes into a core file section. */
2153 bfdcore_write (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2155 int ret = bfd_set_section_contents (obfd, osec, buf, *offset, len);
2160 error (_("Failed to write %d bytes to core file %s ('%s').\n"),
2161 len, bfd_get_filename (obfd),
2162 bfd_errmsg (bfd_get_error ()));
2165 /* Restore the execution log from a file. We use a modified elf
2166 corefile format, with an extra section for our data. */
2169 cmd_record_restore (char *args, int from_tty)
2171 core_file_command (args, from_tty);
2172 record_open (args, from_tty);
2176 record_save_cleanups (void *data)
2179 char *pathname = xstrdup (bfd_get_filename (obfd));
2185 /* Save the execution log to a file. We use a modified elf corefile
2186 format, with an extra section for our data. */
2189 cmd_record_save (char *args, int from_tty)
2191 char *recfilename, recfilename_buffer[40];
2193 struct record_entry *cur_record_list;
2195 struct regcache *regcache;
2196 struct gdbarch *gdbarch;
2197 struct cleanup *old_cleanups;
2198 struct cleanup *set_cleanups;
2201 asection *osec = NULL;
2204 if (strcmp (current_target.to_shortname, "record") != 0)
2205 error (_("This command can only be used with target 'record'.\n"
2206 "Use 'target record' first.\n"));
2212 /* Default recfile name is "gdb_record.PID". */
2213 snprintf (recfilename_buffer, sizeof (recfilename_buffer),
2214 "gdb_record.%d", PIDGET (inferior_ptid));
2215 recfilename = recfilename_buffer;
2218 /* Open the save file. */
2220 printf_filtered ("Saving execution log to core file '%s'\n", recfilename);
2222 /* Open the output file. */
2223 obfd = create_gcore_bfd (recfilename);
2224 old_cleanups = make_cleanup (record_save_cleanups, obfd);
2226 /* Save the current record entry to "cur_record_list". */
2227 cur_record_list = record_list;
2229 /* Get the values of regcache and gdbarch. */
2230 regcache = get_current_regcache ();
2231 gdbarch = get_regcache_arch (regcache);
2233 /* Disable the GDB operation record. */
2234 set_cleanups = record_gdb_operation_disable_set ();
2236 /* Reverse execute to the begin of record list. */
2239 /* Check for beginning and end of log. */
2240 if (record_list == &record_first)
2243 record_exec_insn (regcache, gdbarch, record_list);
2245 if (record_list->prev)
2246 record_list = record_list->prev;
2249 /* Compute the size needed for the extra bfd section. */
2250 save_size = 4; /* magic cookie */
2251 for (record_list = record_first.next; record_list;
2252 record_list = record_list->next)
2253 switch (record_list->type)
2256 save_size += 1 + 4 + 4;
2259 save_size += 1 + 4 + record_list->u.reg.len;
2262 save_size += 1 + 4 + 8 + record_list->u.mem.len;
2266 /* Make the new bfd section. */
2267 osec = bfd_make_section_anyway_with_flags (obfd, "precord",
2271 error (_("Failed to create 'precord' section for corefile %s: %s"),
2273 bfd_errmsg (bfd_get_error ()));
2274 bfd_set_section_size (obfd, osec, save_size);
2275 bfd_set_section_vma (obfd, osec, 0);
2276 bfd_set_section_alignment (obfd, osec, 0);
2277 bfd_section_lma (obfd, osec) = 0;
2279 /* Save corefile state. */
2280 write_gcore_file (obfd);
2282 /* Write out the record log. */
2283 /* Write the magic code. */
2284 magic = RECORD_FILE_MAGIC;
2287 Writing 4-byte magic cookie RECORD_FILE_MAGIC (0x%s)\n",
2288 phex_nz (magic, 4));
2289 bfdcore_write (obfd, osec, &magic, sizeof (magic), &bfd_offset);
2291 /* Save the entries to recfd and forward execute to the end of
2293 record_list = &record_first;
2297 if (record_list != &record_first)
2300 uint32_t regnum, len, signal, count;
2303 type = record_list->type;
2304 bfdcore_write (obfd, osec, &type, sizeof (type), &bfd_offset);
2306 switch (record_list->type)
2308 case record_reg: /* reg */
2311 Writing register %d (1 plus %lu plus %d bytes)\n",
2312 record_list->u.reg.num,
2313 (unsigned long) sizeof (regnum),
2314 record_list->u.reg.len);
2317 regnum = netorder32 (record_list->u.reg.num);
2318 bfdcore_write (obfd, osec, ®num,
2319 sizeof (regnum), &bfd_offset);
2322 bfdcore_write (obfd, osec, record_get_loc (record_list),
2323 record_list->u.reg.len, &bfd_offset);
2326 case record_mem: /* mem */
2329 Writing memory %s (1 plus %lu plus %lu plus %d bytes)\n",
2331 record_list->u.mem.addr),
2332 (unsigned long) sizeof (addr),
2333 (unsigned long) sizeof (len),
2334 record_list->u.mem.len);
2337 len = netorder32 (record_list->u.mem.len);
2338 bfdcore_write (obfd, osec, &len, sizeof (len), &bfd_offset);
2340 /* Write memaddr. */
2341 addr = netorder64 (record_list->u.mem.addr);
2342 bfdcore_write (obfd, osec, &addr,
2343 sizeof (addr), &bfd_offset);
2346 bfdcore_write (obfd, osec, record_get_loc (record_list),
2347 record_list->u.mem.len, &bfd_offset);
2353 Writing record_end (1 + %lu + %lu bytes)\n",
2354 (unsigned long) sizeof (signal),
2355 (unsigned long) sizeof (count));
2356 /* Write signal value. */
2357 signal = netorder32 (record_list->u.end.sigval);
2358 bfdcore_write (obfd, osec, &signal,
2359 sizeof (signal), &bfd_offset);
2361 /* Write insn count. */
2362 count = netorder32 (record_list->u.end.insn_num);
2363 bfdcore_write (obfd, osec, &count,
2364 sizeof (count), &bfd_offset);
2369 /* Execute entry. */
2370 record_exec_insn (regcache, gdbarch, record_list);
2372 if (record_list->next)
2373 record_list = record_list->next;
2378 /* Reverse execute to cur_record_list. */
2381 /* Check for beginning and end of log. */
2382 if (record_list == cur_record_list)
2385 record_exec_insn (regcache, gdbarch, record_list);
2387 if (record_list->prev)
2388 record_list = record_list->prev;
2391 do_cleanups (set_cleanups);
2393 discard_cleanups (old_cleanups);
2396 printf_filtered (_("Saved core file %s with execution log.\n"),
2401 _initialize_record (void)
2403 struct cmd_list_element *c;
2405 /* Init record_first. */
2406 record_first.prev = NULL;
2407 record_first.next = NULL;
2408 record_first.type = record_end;
2411 add_target (&record_ops);
2412 init_record_core_ops ();
2413 add_target (&record_core_ops);
2415 add_setshow_zinteger_cmd ("record", no_class, &record_debug,
2416 _("Set debugging of record/replay feature."),
2417 _("Show debugging of record/replay feature."),
2418 _("When enabled, debugging output for "
2419 "record/replay feature is displayed."),
2420 NULL, show_record_debug, &setdebuglist,
2423 c = add_prefix_cmd ("record", class_obscure, cmd_record_start,
2424 _("Abbreviated form of \"target record\" command."),
2425 &record_cmdlist, "record ", 0, &cmdlist);
2426 set_cmd_completer (c, filename_completer);
2428 add_com_alias ("rec", "record", class_obscure, 1);
2429 add_prefix_cmd ("record", class_support, set_record_command,
2430 _("Set record options"), &set_record_cmdlist,
2431 "set record ", 0, &setlist);
2432 add_alias_cmd ("rec", "record", class_obscure, 1, &setlist);
2433 add_prefix_cmd ("record", class_support, show_record_command,
2434 _("Show record options"), &show_record_cmdlist,
2435 "show record ", 0, &showlist);
2436 add_alias_cmd ("rec", "record", class_obscure, 1, &showlist);
2437 add_prefix_cmd ("record", class_support, info_record_command,
2438 _("Info record options"), &info_record_cmdlist,
2439 "info record ", 0, &infolist);
2440 add_alias_cmd ("rec", "record", class_obscure, 1, &infolist);
2442 c = add_cmd ("save", class_obscure, cmd_record_save,
2443 _("Save the execution log to a file.\n\
2444 Argument is optional filename.\n\
2445 Default filename is 'gdb_record.<process_id>'."),
2447 set_cmd_completer (c, filename_completer);
2449 c = add_cmd ("restore", class_obscure, cmd_record_restore,
2450 _("Restore the execution log from a file.\n\
2451 Argument is filename. File must be created with 'record save'."),
2453 set_cmd_completer (c, filename_completer);
2455 add_cmd ("delete", class_obscure, cmd_record_delete,
2456 _("Delete the rest of execution log and start recording it anew."),
2458 add_alias_cmd ("d", "delete", class_obscure, 1, &record_cmdlist);
2459 add_alias_cmd ("del", "delete", class_obscure, 1, &record_cmdlist);
2461 add_cmd ("stop", class_obscure, cmd_record_stop,
2462 _("Stop the record/replay target."),
2464 add_alias_cmd ("s", "stop", class_obscure, 1, &record_cmdlist);
2466 /* Record instructions number limit command. */
2467 add_setshow_boolean_cmd ("stop-at-limit", no_class,
2468 &record_stop_at_limit, _("\
2469 Set whether record/replay stops when record/replay buffer becomes full."), _("\
2470 Show whether record/replay stops when record/replay buffer becomes full."), _("\
2472 When ON, if the record/replay buffer becomes full, ask user what to do.\n\
2473 When OFF, if the record/replay buffer becomes full,\n\
2474 delete the oldest recorded instruction to make room for each new one."),
2476 &set_record_cmdlist, &show_record_cmdlist);
2477 add_setshow_uinteger_cmd ("insn-number-max", no_class,
2478 &record_insn_max_num,
2479 _("Set record/replay buffer limit."),
2480 _("Show record/replay buffer limit."), _("\
2481 Set the maximum number of instructions to be stored in the\n\
2482 record/replay buffer. Zero means unlimited. Default is 200000."),
2483 set_record_insn_max_num,
2484 NULL, &set_record_cmdlist, &show_record_cmdlist);