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"
36 /* This module implements "target record", also known as "process
37 record and replay". This target sits on top of a "normal" target
38 (a target that "has execution"), and provides a record and replay
39 functionality, including reverse debugging.
41 Target record has two modes: recording, and replaying.
43 In record mode, we intercept the to_resume and to_wait methods.
44 Whenever gdb resumes the target, we run the target in single step
45 mode, and we build up an execution log in which, for each executed
46 instruction, we record all changes in memory and register state.
47 This is invisible to the user, to whom it just looks like an
48 ordinary debugging session (except for performance degredation).
50 In replay mode, instead of actually letting the inferior run as a
51 process, we simulate its execution by playing back the recorded
52 execution log. For each instruction in the log, we simulate the
53 instruction's side effects by duplicating the changes that it would
54 have made on memory and registers. */
56 #define DEFAULT_RECORD_INSN_MAX_NUM 200000
58 #define RECORD_IS_REPLAY \
59 (record_list->next || execution_direction == EXEC_REVERSE)
61 #define RECORD_FILE_MAGIC netorder32(0x20091016)
63 /* These are the core structs of the process record functionality.
65 A record_entry is a record of the value change of a register
66 ("record_reg") or a part of memory ("record_mem"). And each
67 instruction must have a struct record_entry ("record_end") that
68 indicates that this is the last struct record_entry of this
71 Each struct record_entry is linked to "record_list" by "prev" and
74 struct record_mem_entry
78 /* Set this flag if target memory for this entry
79 can no longer be accessed. */
80 int mem_entry_not_accessible;
84 gdb_byte buf[sizeof (gdb_byte *)];
88 struct record_reg_entry
95 gdb_byte buf[2 * sizeof (gdb_byte *)];
99 struct record_end_entry
101 enum target_signal sigval;
112 /* This is the data structure that makes up the execution log.
114 The execution log consists of a single linked list of entries
115 of type "struct record_entry". It is doubly linked so that it
116 can be traversed in either direction.
118 The start of the list is anchored by a struct called
119 "record_first". The pointer "record_list" either points to the
120 last entry that was added to the list (in record mode), or to the
121 next entry in the list that will be executed (in replay mode).
123 Each list element (struct record_entry), in addition to next and
124 prev pointers, consists of a union of three entry types: mem, reg,
125 and end. A field called "type" determines which entry type is
126 represented by a given list element.
128 Each instruction that is added to the execution log is represented
129 by a variable number of list elements ('entries'). The instruction
130 will have one "reg" entry for each register that is changed by
131 executing the instruction (including the PC in every case). It
132 will also have one "mem" entry for each memory change. Finally,
133 each instruction will have an "end" entry that separates it from
134 the changes associated with the next instruction. */
138 struct record_entry *prev;
139 struct record_entry *next;
140 enum record_type type;
144 struct record_reg_entry reg;
146 struct record_mem_entry mem;
148 struct record_end_entry end;
152 /* This is the debug switch for process record. */
153 int record_debug = 0;
155 struct record_core_buf_entry
157 struct record_core_buf_entry *prev;
158 struct target_section *p;
162 /* Record buf with core target. */
163 static gdb_byte *record_core_regbuf = NULL;
164 static struct target_section *record_core_start;
165 static struct target_section *record_core_end;
166 static struct record_core_buf_entry *record_core_buf_list = NULL;
168 /* The following variables are used for managing the linked list that
169 represents the execution log.
171 record_first is the anchor that holds down the beginning of the list.
173 record_list serves two functions:
174 1) In record mode, it anchors the end of the list.
175 2) In replay mode, it traverses the list and points to
176 the next instruction that must be emulated.
178 record_arch_list_head and record_arch_list_tail are used to manage
179 a separate list, which is used to build up the change elements of
180 the currently executing instruction during record mode. When this
181 instruction has been completely annotated in the "arch list", it
182 will be appended to the main execution log. */
184 static struct record_entry record_first;
185 static struct record_entry *record_list = &record_first;
186 static struct record_entry *record_arch_list_head = NULL;
187 static struct record_entry *record_arch_list_tail = NULL;
189 /* 1 ask user. 0 auto delete the last struct record_entry. */
190 static int record_stop_at_limit = 1;
191 /* Maximum allowed number of insns in execution log. */
192 static unsigned int record_insn_max_num = DEFAULT_RECORD_INSN_MAX_NUM;
193 /* Actual count of insns presently in execution log. */
194 static int record_insn_num = 0;
195 /* Count of insns logged so far (may be larger
196 than count of insns presently in execution log). */
197 static ULONGEST record_insn_count;
199 /* The target_ops of process record. */
200 static struct target_ops record_ops;
201 static struct target_ops record_core_ops;
203 /* The beneath function pointers. */
204 static struct target_ops *record_beneath_to_resume_ops;
205 static void (*record_beneath_to_resume) (struct target_ops *, ptid_t, int,
207 static struct target_ops *record_beneath_to_wait_ops;
208 static ptid_t (*record_beneath_to_wait) (struct target_ops *, ptid_t,
209 struct target_waitstatus *,
211 static struct target_ops *record_beneath_to_store_registers_ops;
212 static void (*record_beneath_to_store_registers) (struct target_ops *,
215 static struct target_ops *record_beneath_to_xfer_partial_ops;
216 static LONGEST (*record_beneath_to_xfer_partial) (struct target_ops *ops,
217 enum target_object object,
220 const gdb_byte *writebuf,
223 static int (*record_beneath_to_insert_breakpoint) (struct gdbarch *,
224 struct bp_target_info *);
225 static int (*record_beneath_to_remove_breakpoint) (struct gdbarch *,
226 struct bp_target_info *);
228 /* Alloc and free functions for record_reg, record_mem, and record_end
231 /* Alloc a record_reg record entry. */
233 static inline struct record_entry *
234 record_reg_alloc (struct regcache *regcache, int regnum)
236 struct record_entry *rec;
237 struct gdbarch *gdbarch = get_regcache_arch (regcache);
239 rec = (struct record_entry *) xcalloc (1, sizeof (struct record_entry));
240 rec->type = record_reg;
241 rec->u.reg.num = regnum;
242 rec->u.reg.len = register_size (gdbarch, regnum);
243 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
244 rec->u.reg.u.ptr = (gdb_byte *) xmalloc (rec->u.reg.len);
249 /* Free a record_reg record entry. */
252 record_reg_release (struct record_entry *rec)
254 gdb_assert (rec->type == record_reg);
255 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
256 xfree (rec->u.reg.u.ptr);
260 /* Alloc a record_mem record entry. */
262 static inline struct record_entry *
263 record_mem_alloc (CORE_ADDR addr, int len)
265 struct record_entry *rec;
267 rec = (struct record_entry *) xcalloc (1, sizeof (struct record_entry));
268 rec->type = record_mem;
269 rec->u.mem.addr = addr;
270 rec->u.mem.len = len;
271 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
272 rec->u.mem.u.ptr = (gdb_byte *) xmalloc (len);
277 /* Free a record_mem record entry. */
280 record_mem_release (struct record_entry *rec)
282 gdb_assert (rec->type == record_mem);
283 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
284 xfree (rec->u.mem.u.ptr);
288 /* Alloc a record_end record entry. */
290 static inline struct record_entry *
291 record_end_alloc (void)
293 struct record_entry *rec;
295 rec = (struct record_entry *) xcalloc (1, sizeof (struct record_entry));
296 rec->type = record_end;
301 /* Free a record_end record entry. */
304 record_end_release (struct record_entry *rec)
309 /* Free one record entry, any type.
310 Return entry->type, in case caller wants to know. */
312 static inline enum record_type
313 record_entry_release (struct record_entry *rec)
315 enum record_type type = rec->type;
319 record_reg_release (rec);
322 record_mem_release (rec);
325 record_end_release (rec);
331 /* Free all record entries in list pointed to by REC. */
334 record_list_release (struct record_entry *rec)
345 record_entry_release (rec->next);
348 if (rec == &record_first)
351 record_first.next = NULL;
354 record_entry_release (rec);
357 /* Free all record entries forward of the given list position. */
360 record_list_release_following (struct record_entry *rec)
362 struct record_entry *tmp = rec->next;
368 if (record_entry_release (tmp) == record_end)
377 /* Delete the first instruction from the beginning of the log, to make
378 room for adding a new instruction at the end of the log.
380 Note -- this function does not modify record_insn_num. */
383 record_list_release_first (void)
385 struct record_entry *tmp;
387 if (!record_first.next)
390 /* Loop until a record_end. */
393 /* Cut record_first.next out of the linked list. */
394 tmp = record_first.next;
395 record_first.next = tmp->next;
396 tmp->next->prev = &record_first;
398 /* tmp is now isolated, and can be deleted. */
399 if (record_entry_release (tmp) == record_end)
400 break; /* End loop at first record_end. */
402 if (!record_first.next)
404 gdb_assert (record_insn_num == 1);
405 break; /* End loop when list is empty. */
410 /* Add a struct record_entry to record_arch_list. */
413 record_arch_list_add (struct record_entry *rec)
415 if (record_debug > 1)
416 fprintf_unfiltered (gdb_stdlog,
417 "Process record: record_arch_list_add %s.\n",
418 host_address_to_string (rec));
420 if (record_arch_list_tail)
422 record_arch_list_tail->next = rec;
423 rec->prev = record_arch_list_tail;
424 record_arch_list_tail = rec;
428 record_arch_list_head = rec;
429 record_arch_list_tail = rec;
433 /* Return the value storage location of a record entry. */
434 static inline gdb_byte *
435 record_get_loc (struct record_entry *rec)
439 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
440 return rec->u.mem.u.ptr;
442 return rec->u.mem.u.buf;
444 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
445 return rec->u.reg.u.ptr;
447 return rec->u.reg.u.buf;
455 /* Record the value of a register NUM to record_arch_list. */
458 record_arch_list_add_reg (struct regcache *regcache, int regnum)
460 struct record_entry *rec;
462 if (record_debug > 1)
463 fprintf_unfiltered (gdb_stdlog,
464 "Process record: add register num = %d to "
468 rec = record_reg_alloc (regcache, regnum);
470 regcache_raw_read (regcache, regnum, record_get_loc (rec));
472 record_arch_list_add (rec);
477 /* Record the value of a region of memory whose address is ADDR and
478 length is LEN to record_arch_list. */
481 record_arch_list_add_mem (CORE_ADDR addr, int len)
483 struct record_entry *rec;
485 if (record_debug > 1)
486 fprintf_unfiltered (gdb_stdlog,
487 "Process record: add mem addr = %s len = %d to "
489 paddress (target_gdbarch, addr), len);
491 if (!addr) /* FIXME: Why? Some arch must permit it... */
494 rec = record_mem_alloc (addr, len);
496 if (target_read_memory (addr, record_get_loc (rec), len))
499 fprintf_unfiltered (gdb_stdlog,
500 "Process record: error reading memory at "
501 "addr = %s len = %d.\n",
502 paddress (target_gdbarch, addr), len);
503 record_mem_release (rec);
507 record_arch_list_add (rec);
512 /* Add a record_end type struct record_entry to record_arch_list. */
515 record_arch_list_add_end (void)
517 struct record_entry *rec;
519 if (record_debug > 1)
520 fprintf_unfiltered (gdb_stdlog,
521 "Process record: add end to arch list.\n");
523 rec = record_end_alloc ();
524 rec->u.end.sigval = TARGET_SIGNAL_0;
525 rec->u.end.insn_num = ++record_insn_count;
527 record_arch_list_add (rec);
533 record_check_insn_num (int set_terminal)
535 if (record_insn_max_num)
537 gdb_assert (record_insn_num <= record_insn_max_num);
538 if (record_insn_num == record_insn_max_num)
540 /* Ask user what to do. */
541 if (record_stop_at_limit)
545 target_terminal_ours ();
546 q = yquery (_("Do you want to auto delete previous execution "
547 "log entries when record/replay buffer becomes "
548 "full (record stop-at-limit)?"));
550 target_terminal_inferior ();
552 record_stop_at_limit = 0;
554 error (_("Process record: stopped by user."));
561 record_arch_list_cleanups (void *ignore)
563 record_list_release (record_arch_list_tail);
566 /* Before inferior step (when GDB record the running message, inferior
567 only can step), GDB will call this function to record the values to
568 record_list. This function will call gdbarch_process_record to
569 record the running message of inferior and set them to
570 record_arch_list, and add it to record_list. */
572 struct record_message_args {
573 struct regcache *regcache;
574 enum target_signal signal;
578 record_message (void *args)
581 struct record_message_args *myargs = args;
582 struct gdbarch *gdbarch = get_regcache_arch (myargs->regcache);
583 struct cleanup *old_cleanups = make_cleanup (record_arch_list_cleanups, 0);
585 record_arch_list_head = NULL;
586 record_arch_list_tail = NULL;
588 /* Check record_insn_num. */
589 record_check_insn_num (1);
591 /* If gdb sends a signal value to target_resume,
592 save it in the 'end' field of the previous instruction.
594 Maybe process record should record what really happened,
595 rather than what gdb pretends has happened.
597 So if Linux delivered the signal to the child process during
598 the record mode, we will record it and deliver it again in
601 If user says "ignore this signal" during the record mode, then
602 it will be ignored again during the replay mode (no matter if
603 the user says something different, like "deliver this signal"
604 during the replay mode).
606 User should understand that nothing he does during the replay
607 mode will change the behavior of the child. If he tries,
608 then that is a user error.
610 But we should still deliver the signal to gdb during the replay,
611 if we delivered it during the recording. Therefore we should
612 record the signal during record_wait, not record_resume. */
613 if (record_list != &record_first) /* FIXME better way to check */
615 gdb_assert (record_list->type == record_end);
616 record_list->u.end.sigval = myargs->signal;
619 if (myargs->signal == TARGET_SIGNAL_0
620 || !gdbarch_process_record_signal_p (gdbarch))
621 ret = gdbarch_process_record (gdbarch,
623 regcache_read_pc (myargs->regcache));
625 ret = gdbarch_process_record_signal (gdbarch,
630 error (_("Process record: inferior program stopped."));
632 error (_("Process record: failed to record execution log."));
634 discard_cleanups (old_cleanups);
636 record_list->next = record_arch_list_head;
637 record_arch_list_head->prev = record_list;
638 record_list = record_arch_list_tail;
640 if (record_insn_num == record_insn_max_num && record_insn_max_num)
641 record_list_release_first ();
649 do_record_message (struct regcache *regcache,
650 enum target_signal signal)
652 struct record_message_args args;
654 args.regcache = regcache;
655 args.signal = signal;
656 return catch_errors (record_message, &args, NULL, RETURN_MASK_ALL);
659 /* Set to 1 if record_store_registers and record_xfer_partial
660 doesn't need record. */
662 static int record_gdb_operation_disable = 0;
665 record_gdb_operation_disable_set (void)
667 struct cleanup *old_cleanups = NULL;
670 make_cleanup_restore_integer (&record_gdb_operation_disable);
671 record_gdb_operation_disable = 1;
676 /* Execute one instruction from the record log. Each instruction in
677 the log will be represented by an arbitrary sequence of register
678 entries and memory entries, followed by an 'end' entry. */
681 record_exec_insn (struct regcache *regcache, struct gdbarch *gdbarch,
682 struct record_entry *entry)
686 case record_reg: /* reg */
688 gdb_byte reg[MAX_REGISTER_SIZE];
690 if (record_debug > 1)
691 fprintf_unfiltered (gdb_stdlog,
692 "Process record: record_reg %s to "
693 "inferior num = %d.\n",
694 host_address_to_string (entry),
697 regcache_cooked_read (regcache, entry->u.reg.num, reg);
698 regcache_cooked_write (regcache, entry->u.reg.num,
699 record_get_loc (entry));
700 memcpy (record_get_loc (entry), reg, entry->u.reg.len);
704 case record_mem: /* mem */
706 /* Nothing to do if the entry is flagged not_accessible. */
707 if (!entry->u.mem.mem_entry_not_accessible)
709 gdb_byte *mem = alloca (entry->u.mem.len);
711 if (record_debug > 1)
712 fprintf_unfiltered (gdb_stdlog,
713 "Process record: record_mem %s to "
714 "inferior addr = %s len = %d.\n",
715 host_address_to_string (entry),
716 paddress (gdbarch, entry->u.mem.addr),
719 if (target_read_memory (entry->u.mem.addr, mem, entry->u.mem.len))
721 entry->u.mem.mem_entry_not_accessible = 1;
723 warning ("Process record: error reading memory at "
724 "addr = %s len = %d.",
725 paddress (gdbarch, entry->u.mem.addr),
730 if (target_write_memory (entry->u.mem.addr,
731 record_get_loc (entry),
734 entry->u.mem.mem_entry_not_accessible = 1;
736 warning ("Process record: error writing memory at "
737 "addr = %s len = %d.",
738 paddress (gdbarch, entry->u.mem.addr),
742 memcpy (record_get_loc (entry), mem, entry->u.mem.len);
750 static struct target_ops *tmp_to_resume_ops;
751 static void (*tmp_to_resume) (struct target_ops *, ptid_t, int,
753 static struct target_ops *tmp_to_wait_ops;
754 static ptid_t (*tmp_to_wait) (struct target_ops *, ptid_t,
755 struct target_waitstatus *,
757 static struct target_ops *tmp_to_store_registers_ops;
758 static void (*tmp_to_store_registers) (struct target_ops *,
761 static struct target_ops *tmp_to_xfer_partial_ops;
762 static LONGEST (*tmp_to_xfer_partial) (struct target_ops *ops,
763 enum target_object object,
766 const gdb_byte *writebuf,
769 static int (*tmp_to_insert_breakpoint) (struct gdbarch *,
770 struct bp_target_info *);
771 static int (*tmp_to_remove_breakpoint) (struct gdbarch *,
772 struct bp_target_info *);
774 static void record_restore (void);
776 /* Open the process record target. */
779 record_core_open_1 (char *name, int from_tty)
781 struct regcache *regcache = get_current_regcache ();
782 int regnum = gdbarch_num_regs (get_regcache_arch (regcache));
785 /* Get record_core_regbuf. */
786 target_fetch_registers (regcache, -1);
787 record_core_regbuf = xmalloc (MAX_REGISTER_SIZE * regnum);
788 for (i = 0; i < regnum; i ++)
789 regcache_raw_collect (regcache, i,
790 record_core_regbuf + MAX_REGISTER_SIZE * i);
792 /* Get record_core_start and record_core_end. */
793 if (build_section_table (core_bfd, &record_core_start, &record_core_end))
795 xfree (record_core_regbuf);
796 record_core_regbuf = NULL;
797 error (_("\"%s\": Can't find sections: %s"),
798 bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ()));
801 push_target (&record_core_ops);
805 /* "to_open" target method for 'live' processes. */
808 record_open_1 (char *name, int from_tty)
810 struct target_ops *t;
813 fprintf_unfiltered (gdb_stdlog, "Process record: record_open\n");
816 if (!target_has_execution)
817 error (_("Process record: the program is not being run."));
819 error (_("Process record target can't debug inferior in non-stop mode "
821 if (target_async_permitted)
822 error (_("Process record target can't debug inferior in asynchronous "
823 "mode (target-async)."));
825 if (!gdbarch_process_record_p (target_gdbarch))
826 error (_("Process record: the current architecture doesn't support "
827 "record function."));
830 error (_("Could not find 'to_resume' method on the target stack."));
832 error (_("Could not find 'to_wait' method on the target stack."));
833 if (!tmp_to_store_registers)
834 error (_("Could not find 'to_store_registers' method on the target stack."));
835 if (!tmp_to_insert_breakpoint)
836 error (_("Could not find 'to_insert_breakpoint' method on the target stack."));
837 if (!tmp_to_remove_breakpoint)
838 error (_("Could not find 'to_remove_breakpoint' method on the target stack."));
840 push_target (&record_ops);
843 /* "to_open" target method. Open the process record target. */
846 record_open (char *name, int from_tty)
848 struct target_ops *t;
851 fprintf_unfiltered (gdb_stdlog, "Process record: record_open\n");
853 /* Check if record target is already running. */
854 if (current_target.to_stratum == record_stratum)
855 error (_("Process record target already running. Use \"record stop\" to "
856 "stop record target first."));
858 /* Reset the tmp beneath pointers. */
859 tmp_to_resume_ops = NULL;
860 tmp_to_resume = NULL;
861 tmp_to_wait_ops = NULL;
863 tmp_to_store_registers_ops = NULL;
864 tmp_to_store_registers = NULL;
865 tmp_to_xfer_partial_ops = NULL;
866 tmp_to_xfer_partial = NULL;
867 tmp_to_insert_breakpoint = NULL;
868 tmp_to_remove_breakpoint = NULL;
870 /* Set the beneath function pointers. */
871 for (t = current_target.beneath; t != NULL; t = t->beneath)
875 tmp_to_resume = t->to_resume;
876 tmp_to_resume_ops = t;
880 tmp_to_wait = t->to_wait;
883 if (!tmp_to_store_registers)
885 tmp_to_store_registers = t->to_store_registers;
886 tmp_to_store_registers_ops = t;
888 if (!tmp_to_xfer_partial)
890 tmp_to_xfer_partial = t->to_xfer_partial;
891 tmp_to_xfer_partial_ops = t;
893 if (!tmp_to_insert_breakpoint)
894 tmp_to_insert_breakpoint = t->to_insert_breakpoint;
895 if (!tmp_to_remove_breakpoint)
896 tmp_to_remove_breakpoint = t->to_remove_breakpoint;
898 if (!tmp_to_xfer_partial)
899 error (_("Could not find 'to_xfer_partial' method on the target stack."));
903 record_insn_count = 0;
904 record_list = &record_first;
905 record_list->next = NULL;
907 /* Set the tmp beneath pointers to beneath pointers. */
908 record_beneath_to_resume_ops = tmp_to_resume_ops;
909 record_beneath_to_resume = tmp_to_resume;
910 record_beneath_to_wait_ops = tmp_to_wait_ops;
911 record_beneath_to_wait = tmp_to_wait;
912 record_beneath_to_store_registers_ops = tmp_to_store_registers_ops;
913 record_beneath_to_store_registers = tmp_to_store_registers;
914 record_beneath_to_xfer_partial_ops = tmp_to_xfer_partial_ops;
915 record_beneath_to_xfer_partial = tmp_to_xfer_partial;
916 record_beneath_to_insert_breakpoint = tmp_to_insert_breakpoint;
917 record_beneath_to_remove_breakpoint = tmp_to_remove_breakpoint;
919 if (current_target.to_stratum == core_stratum)
920 record_core_open_1 (name, from_tty);
922 record_open_1 (name, from_tty);
925 /* "to_close" target method. Close the process record target. */
928 record_close (int quitting)
930 struct record_core_buf_entry *entry;
933 fprintf_unfiltered (gdb_stdlog, "Process record: record_close\n");
935 record_list_release (record_list);
937 /* Release record_core_regbuf. */
938 if (record_core_regbuf)
940 xfree (record_core_regbuf);
941 record_core_regbuf = NULL;
944 /* Release record_core_buf_list. */
945 if (record_core_buf_list)
947 for (entry = record_core_buf_list->prev; entry; entry = entry->prev)
949 xfree (record_core_buf_list);
950 record_core_buf_list = entry;
952 record_core_buf_list = NULL;
956 static int record_resume_step = 0;
957 static int record_resume_error;
959 /* "to_resume" target method. Resume the process record target. */
962 record_resume (struct target_ops *ops, ptid_t ptid, int step,
963 enum target_signal signal)
965 record_resume_step = step;
967 if (!RECORD_IS_REPLAY)
969 if (do_record_message (get_current_regcache (), signal))
971 record_resume_error = 0;
975 record_resume_error = 1;
978 record_beneath_to_resume (record_beneath_to_resume_ops, ptid, 1,
983 static int record_get_sig = 0;
985 /* SIGINT signal handler, registered by "to_wait" method. */
988 record_sig_handler (int signo)
991 fprintf_unfiltered (gdb_stdlog, "Process record: get a signal\n");
993 /* It will break the running inferior in replay mode. */
994 record_resume_step = 1;
996 /* It will let record_wait set inferior status to get the signal
1002 record_wait_cleanups (void *ignore)
1004 if (execution_direction == EXEC_REVERSE)
1006 if (record_list->next)
1007 record_list = record_list->next;
1010 record_list = record_list->prev;
1013 /* "to_wait" target method for process record target.
1015 In record mode, the target is always run in singlestep mode
1016 (even when gdb says to continue). The to_wait method intercepts
1017 the stop events and determines which ones are to be passed on to
1018 gdb. Most stop events are just singlestep events that gdb is not
1019 to know about, so the to_wait method just records them and keeps
1022 In replay mode, this function emulates the recorded execution log,
1023 one instruction at a time (forward or backward), and determines
1027 record_wait (struct target_ops *ops,
1028 ptid_t ptid, struct target_waitstatus *status,
1031 struct cleanup *set_cleanups = record_gdb_operation_disable_set ();
1034 fprintf_unfiltered (gdb_stdlog,
1035 "Process record: record_wait "
1036 "record_resume_step = %d\n",
1037 record_resume_step);
1039 if (!RECORD_IS_REPLAY && ops != &record_core_ops)
1041 if (record_resume_error)
1043 /* If record_resume get error, return directly. */
1044 status->kind = TARGET_WAITKIND_STOPPED;
1045 status->value.sig = TARGET_SIGNAL_ABRT;
1046 return inferior_ptid;
1049 if (record_resume_step)
1051 /* This is a single step. */
1052 return record_beneath_to_wait (record_beneath_to_wait_ops,
1053 ptid, status, options);
1057 /* This is not a single step. */
1063 ret = record_beneath_to_wait (record_beneath_to_wait_ops,
1064 ptid, status, options);
1066 /* Is this a SIGTRAP? */
1067 if (status->kind == TARGET_WAITKIND_STOPPED
1068 && status->value.sig == TARGET_SIGNAL_TRAP)
1070 struct regcache *regcache;
1072 /* Yes -- check if there is a breakpoint. */
1073 registers_changed ();
1074 regcache = get_current_regcache ();
1075 tmp_pc = regcache_read_pc (regcache);
1076 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache),
1079 /* There is a breakpoint. GDB will want to stop. */
1080 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1081 CORE_ADDR decr_pc_after_break
1082 = gdbarch_decr_pc_after_break (gdbarch);
1083 if (decr_pc_after_break)
1084 regcache_write_pc (regcache,
1085 tmp_pc + decr_pc_after_break);
1089 /* There is not a breakpoint, and gdb is not
1090 stepping, therefore gdb will not stop.
1091 Therefore we will not return to gdb.
1092 Record the insn and resume. */
1093 if (!do_record_message (regcache, TARGET_SIGNAL_0))
1096 record_beneath_to_resume (record_beneath_to_resume_ops,
1103 /* The inferior is broken by a breakpoint or a signal. */
1112 struct regcache *regcache = get_current_regcache ();
1113 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1114 int continue_flag = 1;
1115 int first_record_end = 1;
1116 struct cleanup *old_cleanups = make_cleanup (record_wait_cleanups, 0);
1119 status->kind = TARGET_WAITKIND_STOPPED;
1121 /* Check breakpoint when forward execute. */
1122 if (execution_direction == EXEC_FORWARD)
1124 tmp_pc = regcache_read_pc (regcache);
1125 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache),
1129 fprintf_unfiltered (gdb_stdlog,
1130 "Process record: break at %s.\n",
1131 paddress (gdbarch, tmp_pc));
1132 if (gdbarch_decr_pc_after_break (gdbarch)
1133 && !record_resume_step)
1134 regcache_write_pc (regcache,
1136 gdbarch_decr_pc_after_break (gdbarch));
1142 signal (SIGINT, record_sig_handler);
1143 /* If GDB is in terminal_inferior mode, it will not get the signal.
1144 And in GDB replay mode, GDB doesn't need to be in terminal_inferior
1145 mode, because inferior will not executed.
1146 Then set it to terminal_ours to make GDB get the signal. */
1147 target_terminal_ours ();
1149 /* In EXEC_FORWARD mode, record_list points to the tail of prev
1151 if (execution_direction == EXEC_FORWARD && record_list->next)
1152 record_list = record_list->next;
1154 /* Loop over the record_list, looking for the next place to
1158 /* Check for beginning and end of log. */
1159 if (execution_direction == EXEC_REVERSE
1160 && record_list == &record_first)
1162 /* Hit beginning of record log in reverse. */
1163 status->kind = TARGET_WAITKIND_NO_HISTORY;
1166 if (execution_direction != EXEC_REVERSE && !record_list->next)
1168 /* Hit end of record log going forward. */
1169 status->kind = TARGET_WAITKIND_NO_HISTORY;
1173 record_exec_insn (regcache, gdbarch, record_list);
1175 if (record_list->type == record_end)
1177 if (record_debug > 1)
1178 fprintf_unfiltered (gdb_stdlog,
1179 "Process record: record_end %s to "
1181 host_address_to_string (record_list));
1183 if (first_record_end && execution_direction == EXEC_REVERSE)
1185 /* When reverse excute, the first record_end is the part of
1186 current instruction. */
1187 first_record_end = 0;
1191 /* In EXEC_REVERSE mode, this is the record_end of prev
1193 In EXEC_FORWARD mode, this is the record_end of current
1196 if (record_resume_step)
1198 if (record_debug > 1)
1199 fprintf_unfiltered (gdb_stdlog,
1200 "Process record: step.\n");
1204 /* check breakpoint */
1205 tmp_pc = regcache_read_pc (regcache);
1206 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache),
1210 fprintf_unfiltered (gdb_stdlog,
1211 "Process record: break "
1213 paddress (gdbarch, tmp_pc));
1214 if (gdbarch_decr_pc_after_break (gdbarch)
1215 && execution_direction == EXEC_FORWARD
1216 && !record_resume_step)
1217 regcache_write_pc (regcache,
1219 gdbarch_decr_pc_after_break (gdbarch));
1222 /* Check target signal */
1223 if (record_list->u.end.sigval != TARGET_SIGNAL_0)
1224 /* FIXME: better way to check */
1231 if (execution_direction == EXEC_REVERSE)
1233 if (record_list->prev)
1234 record_list = record_list->prev;
1238 if (record_list->next)
1239 record_list = record_list->next;
1243 while (continue_flag);
1245 signal (SIGINT, handle_sigint);
1249 status->value.sig = TARGET_SIGNAL_INT;
1250 else if (record_list->u.end.sigval != TARGET_SIGNAL_0)
1251 /* FIXME: better way to check */
1252 status->value.sig = record_list->u.end.sigval;
1254 status->value.sig = TARGET_SIGNAL_TRAP;
1256 discard_cleanups (old_cleanups);
1259 do_cleanups (set_cleanups);
1260 return inferior_ptid;
1263 /* "to_disconnect" method for process record target. */
1266 record_disconnect (struct target_ops *target, char *args, int from_tty)
1269 fprintf_unfiltered (gdb_stdlog, "Process record: record_disconnect\n");
1271 unpush_target (&record_ops);
1272 target_disconnect (args, from_tty);
1275 /* "to_detach" method for process record target. */
1278 record_detach (struct target_ops *ops, char *args, int from_tty)
1281 fprintf_unfiltered (gdb_stdlog, "Process record: record_detach\n");
1283 unpush_target (&record_ops);
1284 target_detach (args, from_tty);
1287 /* "to_mourn_inferior" method for process record target. */
1290 record_mourn_inferior (struct target_ops *ops)
1293 fprintf_unfiltered (gdb_stdlog, "Process record: "
1294 "record_mourn_inferior\n");
1296 unpush_target (&record_ops);
1297 target_mourn_inferior ();
1300 /* Close process record target before killing the inferior process. */
1303 record_kill (struct target_ops *ops)
1306 fprintf_unfiltered (gdb_stdlog, "Process record: record_kill\n");
1308 unpush_target (&record_ops);
1312 /* Record registers change (by user or by GDB) to list as an instruction. */
1315 record_registers_change (struct regcache *regcache, int regnum)
1317 /* Check record_insn_num. */
1318 record_check_insn_num (0);
1320 record_arch_list_head = NULL;
1321 record_arch_list_tail = NULL;
1326 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
1328 if (record_arch_list_add_reg (regcache, i))
1330 record_list_release (record_arch_list_tail);
1331 error (_("Process record: failed to record execution log."));
1337 if (record_arch_list_add_reg (regcache, regnum))
1339 record_list_release (record_arch_list_tail);
1340 error (_("Process record: failed to record execution log."));
1343 if (record_arch_list_add_end ())
1345 record_list_release (record_arch_list_tail);
1346 error (_("Process record: failed to record execution log."));
1348 record_list->next = record_arch_list_head;
1349 record_arch_list_head->prev = record_list;
1350 record_list = record_arch_list_tail;
1352 if (record_insn_num == record_insn_max_num && record_insn_max_num)
1353 record_list_release_first ();
1358 /* "to_store_registers" method for process record target. */
1361 record_store_registers (struct target_ops *ops, struct regcache *regcache,
1364 if (!record_gdb_operation_disable)
1366 if (RECORD_IS_REPLAY)
1370 /* Let user choose if he wants to write register or not. */
1373 query (_("Because GDB is in replay mode, changing the "
1374 "value of a register will make the execution "
1375 "log unusable from this point onward. "
1376 "Change all registers?"));
1379 query (_("Because GDB is in replay mode, changing the value "
1380 "of a register will make the execution log unusable "
1381 "from this point onward. Change register %s?"),
1382 gdbarch_register_name (get_regcache_arch (regcache),
1387 /* Invalidate the value of regcache that was set in function
1388 "regcache_raw_write". */
1393 i < gdbarch_num_regs (get_regcache_arch (regcache));
1395 regcache_invalidate (regcache, i);
1398 regcache_invalidate (regcache, regno);
1400 error (_("Process record canceled the operation."));
1403 /* Destroy the record from here forward. */
1404 record_list_release_following (record_list);
1407 record_registers_change (regcache, regno);
1409 record_beneath_to_store_registers (record_beneath_to_store_registers_ops,
1413 /* "to_xfer_partial" method. Behavior is conditional on RECORD_IS_REPLAY.
1414 In replay mode, we cannot write memory unles we are willing to
1415 invalidate the record/replay log from this point forward. */
1418 record_xfer_partial (struct target_ops *ops, enum target_object object,
1419 const char *annex, gdb_byte *readbuf,
1420 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
1422 if (!record_gdb_operation_disable
1423 && (object == TARGET_OBJECT_MEMORY
1424 || object == TARGET_OBJECT_RAW_MEMORY) && writebuf)
1426 if (RECORD_IS_REPLAY)
1428 /* Let user choose if he wants to write memory or not. */
1429 if (!query (_("Because GDB is in replay mode, writing to memory "
1430 "will make the execution log unusable from this "
1431 "point onward. Write memory at address %s?"),
1432 paddress (target_gdbarch, offset)))
1433 error (_("Process record canceled the operation."));
1435 /* Destroy the record from here forward. */
1436 record_list_release_following (record_list);
1439 /* Check record_insn_num */
1440 record_check_insn_num (0);
1442 /* Record registers change to list as an instruction. */
1443 record_arch_list_head = NULL;
1444 record_arch_list_tail = NULL;
1445 if (record_arch_list_add_mem (offset, len))
1447 record_list_release (record_arch_list_tail);
1449 fprintf_unfiltered (gdb_stdlog,
1450 "Process record: failed to record "
1454 if (record_arch_list_add_end ())
1456 record_list_release (record_arch_list_tail);
1458 fprintf_unfiltered (gdb_stdlog,
1459 "Process record: failed to record "
1463 record_list->next = record_arch_list_head;
1464 record_arch_list_head->prev = record_list;
1465 record_list = record_arch_list_tail;
1467 if (record_insn_num == record_insn_max_num && record_insn_max_num)
1468 record_list_release_first ();
1473 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops,
1474 object, annex, readbuf, writebuf,
1478 /* Behavior is conditional on RECORD_IS_REPLAY.
1479 We will not actually insert or remove breakpoints when replaying,
1480 nor when recording. */
1483 record_insert_breakpoint (struct gdbarch *gdbarch,
1484 struct bp_target_info *bp_tgt)
1486 if (!RECORD_IS_REPLAY)
1488 struct cleanup *old_cleanups = record_gdb_operation_disable_set ();
1489 int ret = record_beneath_to_insert_breakpoint (gdbarch, bp_tgt);
1491 do_cleanups (old_cleanups);
1499 /* "to_remove_breakpoint" method for process record target. */
1502 record_remove_breakpoint (struct gdbarch *gdbarch,
1503 struct bp_target_info *bp_tgt)
1505 if (!RECORD_IS_REPLAY)
1507 struct cleanup *old_cleanups = record_gdb_operation_disable_set ();
1508 int ret = record_beneath_to_remove_breakpoint (gdbarch, bp_tgt);
1510 do_cleanups (old_cleanups);
1518 /* "to_can_execute_reverse" method for process record target. */
1521 record_can_execute_reverse (void)
1527 init_record_ops (void)
1529 record_ops.to_shortname = "record";
1530 record_ops.to_longname = "Process record and replay target";
1532 "Log program while executing and replay execution from log.";
1533 record_ops.to_open = record_open;
1534 record_ops.to_close = record_close;
1535 record_ops.to_resume = record_resume;
1536 record_ops.to_wait = record_wait;
1537 record_ops.to_disconnect = record_disconnect;
1538 record_ops.to_detach = record_detach;
1539 record_ops.to_mourn_inferior = record_mourn_inferior;
1540 record_ops.to_kill = record_kill;
1541 record_ops.to_create_inferior = find_default_create_inferior;
1542 record_ops.to_store_registers = record_store_registers;
1543 record_ops.to_xfer_partial = record_xfer_partial;
1544 record_ops.to_insert_breakpoint = record_insert_breakpoint;
1545 record_ops.to_remove_breakpoint = record_remove_breakpoint;
1546 record_ops.to_can_execute_reverse = record_can_execute_reverse;
1547 record_ops.to_stratum = record_stratum;
1548 record_ops.to_magic = OPS_MAGIC;
1551 /* "to_resume" method for prec over corefile. */
1554 record_core_resume (struct target_ops *ops, ptid_t ptid, int step,
1555 enum target_signal signal)
1557 record_resume_step = step;
1560 /* "to_kill" method for prec over corefile. */
1563 record_core_kill (struct target_ops *ops)
1566 fprintf_unfiltered (gdb_stdlog, "Process record: record_core_kill\n");
1568 unpush_target (&record_core_ops);
1571 /* "to_fetch_registers" method for prec over corefile. */
1574 record_core_fetch_registers (struct target_ops *ops,
1575 struct regcache *regcache,
1580 int num = gdbarch_num_regs (get_regcache_arch (regcache));
1583 for (i = 0; i < num; i ++)
1584 regcache_raw_supply (regcache, i,
1585 record_core_regbuf + MAX_REGISTER_SIZE * i);
1588 regcache_raw_supply (regcache, regno,
1589 record_core_regbuf + MAX_REGISTER_SIZE * regno);
1592 /* "to_prepare_to_store" method for prec over corefile. */
1595 record_core_prepare_to_store (struct regcache *regcache)
1599 /* "to_store_registers" method for prec over corefile. */
1602 record_core_store_registers (struct target_ops *ops,
1603 struct regcache *regcache,
1606 if (record_gdb_operation_disable)
1607 regcache_raw_collect (regcache, regno,
1608 record_core_regbuf + MAX_REGISTER_SIZE * regno);
1610 error (_("You can't do that without a process to debug."));
1613 /* "to_xfer_partial" method for prec over corefile. */
1616 record_core_xfer_partial (struct target_ops *ops, enum target_object object,
1617 const char *annex, gdb_byte *readbuf,
1618 const gdb_byte *writebuf, ULONGEST offset,
1621 if (object == TARGET_OBJECT_MEMORY)
1623 if (record_gdb_operation_disable || !writebuf)
1625 struct target_section *p;
1626 for (p = record_core_start; p < record_core_end; p++)
1628 if (offset >= p->addr)
1630 struct record_core_buf_entry *entry;
1631 ULONGEST sec_offset;
1633 if (offset >= p->endaddr)
1636 if (offset + len > p->endaddr)
1637 len = p->endaddr - offset;
1639 sec_offset = offset - p->addr;
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 + sec_offset, writebuf,
1682 return record_beneath_to_xfer_partial
1683 (record_beneath_to_xfer_partial_ops,
1684 object, annex, readbuf, writebuf,
1687 memcpy (readbuf, entry->buf + sec_offset,
1698 error (_("You can't do that without a process to debug."));
1701 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops,
1702 object, annex, readbuf, writebuf,
1706 /* "to_insert_breakpoint" method for prec over corefile. */
1709 record_core_insert_breakpoint (struct gdbarch *gdbarch,
1710 struct bp_target_info *bp_tgt)
1715 /* "to_remove_breakpoint" method for prec over corefile. */
1718 record_core_remove_breakpoint (struct gdbarch *gdbarch,
1719 struct bp_target_info *bp_tgt)
1724 /* "to_has_execution" method for prec over corefile. */
1727 record_core_has_execution (struct target_ops *ops)
1733 init_record_core_ops (void)
1735 record_core_ops.to_shortname = "record_core";
1736 record_core_ops.to_longname = "Process record and replay target";
1737 record_core_ops.to_doc =
1738 "Log program while executing and replay execution from log.";
1739 record_core_ops.to_open = record_open;
1740 record_core_ops.to_close = record_close;
1741 record_core_ops.to_resume = record_core_resume;
1742 record_core_ops.to_wait = record_wait;
1743 record_core_ops.to_kill = record_core_kill;
1744 record_core_ops.to_fetch_registers = record_core_fetch_registers;
1745 record_core_ops.to_prepare_to_store = record_core_prepare_to_store;
1746 record_core_ops.to_store_registers = record_core_store_registers;
1747 record_core_ops.to_xfer_partial = record_core_xfer_partial;
1748 record_core_ops.to_insert_breakpoint = record_core_insert_breakpoint;
1749 record_core_ops.to_remove_breakpoint = record_core_remove_breakpoint;
1750 record_core_ops.to_can_execute_reverse = record_can_execute_reverse;
1751 record_core_ops.to_has_execution = record_core_has_execution;
1752 record_core_ops.to_stratum = record_stratum;
1753 record_core_ops.to_magic = OPS_MAGIC;
1756 /* Implement "show record debug" command. */
1759 show_record_debug (struct ui_file *file, int from_tty,
1760 struct cmd_list_element *c, const char *value)
1762 fprintf_filtered (file, _("Debugging of process record target is %s.\n"),
1766 /* Alias for "target record". */
1769 cmd_record_start (char *args, int from_tty)
1771 execute_command ("target record", from_tty);
1774 /* Truncate the record log from the present point
1775 of replay until the end. */
1778 cmd_record_delete (char *args, int from_tty)
1780 if (current_target.to_stratum == record_stratum)
1782 if (RECORD_IS_REPLAY)
1784 if (!from_tty || query (_("Delete the log from this point forward "
1785 "and begin to record the running message "
1787 record_list_release_following (record_list);
1790 printf_unfiltered (_("Already at end of record list.\n"));
1794 printf_unfiltered (_("Process record is not started.\n"));
1797 /* Implement the "stoprecord" or "record stop" command. */
1800 cmd_record_stop (char *args, int from_tty)
1802 if (current_target.to_stratum == record_stratum)
1804 unpush_target (&record_ops);
1805 printf_unfiltered (_("Process record is stopped and all execution "
1806 "logs are deleted.\n"));
1809 printf_unfiltered (_("Process record is not started.\n"));
1812 /* Set upper limit of record log size. */
1815 set_record_insn_max_num (char *args, int from_tty, struct cmd_list_element *c)
1817 if (record_insn_num > record_insn_max_num && record_insn_max_num)
1819 /* Count down record_insn_num while releasing records from list. */
1820 while (record_insn_num > record_insn_max_num)
1822 record_list_release_first ();
1828 static struct cmd_list_element *record_cmdlist, *set_record_cmdlist,
1829 *show_record_cmdlist, *info_record_cmdlist;
1832 set_record_command (char *args, int from_tty)
1834 printf_unfiltered (_("\
1835 \"set record\" must be followed by an apporpriate subcommand.\n"));
1836 help_list (set_record_cmdlist, "set record ", all_commands, gdb_stdout);
1840 show_record_command (char *args, int from_tty)
1842 cmd_show_list (show_record_cmdlist, from_tty, "");
1845 /* Display some statistics about the execution log. */
1848 info_record_command (char *args, int from_tty)
1850 struct record_entry *p;
1852 if (current_target.to_stratum == record_stratum)
1854 if (RECORD_IS_REPLAY)
1855 printf_filtered (_("Replay mode:\n"));
1857 printf_filtered (_("Record mode:\n"));
1859 /* Find entry for first actual instruction in the log. */
1860 for (p = record_first.next;
1861 p != NULL && p->type != record_end;
1865 /* Do we have a log at all? */
1866 if (p != NULL && p->type == record_end)
1868 /* Display instruction number for first instruction in the log. */
1869 printf_filtered (_("Lowest recorded instruction number is %s.\n"),
1870 pulongest (p->u.end.insn_num));
1872 /* If in replay mode, display where we are in the log. */
1873 if (RECORD_IS_REPLAY)
1874 printf_filtered (_("Current instruction number is %s.\n"),
1875 pulongest (record_list->u.end.insn_num));
1877 /* Display instruction number for last instruction in the log. */
1878 printf_filtered (_("Highest recorded instruction number is %s.\n"),
1879 pulongest (record_insn_count));
1881 /* Display log count. */
1882 printf_filtered (_("Log contains %d instructions.\n"),
1887 printf_filtered (_("No instructions have been logged.\n"));
1892 printf_filtered (_("target record is not active.\n"));
1895 /* Display max log size. */
1896 printf_filtered (_("Max logged instructions is %d.\n"),
1897 record_insn_max_num);
1900 /* Record log save-file format
1901 Version 1 (never released)
1904 4 bytes: magic number htonl(0x20090829).
1905 NOTE: be sure to change whenever this file format changes!
1909 1 byte: record type (record_end, see enum record_type).
1911 1 byte: record type (record_reg, see enum record_type).
1912 8 bytes: register id (network byte order).
1913 MAX_REGISTER_SIZE bytes: register value.
1915 1 byte: record type (record_mem, see enum record_type).
1916 8 bytes: memory length (network byte order).
1917 8 bytes: memory address (network byte order).
1918 n bytes: memory value (n == memory length).
1921 4 bytes: magic number netorder32(0x20091016).
1922 NOTE: be sure to change whenever this file format changes!
1926 1 byte: record type (record_end, see enum record_type).
1928 4 bytes: instruction count
1930 1 byte: record type (record_reg, see enum record_type).
1931 4 bytes: register id (network byte order).
1932 n bytes: register value (n == actual register size).
1933 (eg. 4 bytes for x86 general registers).
1935 1 byte: record type (record_mem, see enum record_type).
1936 4 bytes: memory length (network byte order).
1937 8 bytes: memory address (network byte order).
1938 n bytes: memory value (n == memory length).
1942 /* bfdcore_read -- read bytes from a core file section. */
1945 bfdcore_read (bfd *obfd, asection *osec, void *buf, int len, int *offset)
1947 int ret = bfd_get_section_contents (obfd, osec, buf, *offset, len);
1952 error (_("Failed to read %d bytes from core file %s ('%s').\n"),
1953 len, bfd_get_filename (obfd),
1954 bfd_errmsg (bfd_get_error ()));
1957 static inline uint64_t
1958 netorder64 (uint64_t input)
1962 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
1963 BFD_ENDIAN_BIG, input);
1967 static inline uint32_t
1968 netorder32 (uint32_t input)
1972 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
1973 BFD_ENDIAN_BIG, input);
1977 static inline uint16_t
1978 netorder16 (uint16_t input)
1982 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
1983 BFD_ENDIAN_BIG, input);
1987 /* Restore the execution log from a core_bfd file. */
1989 record_restore (void)
1992 struct cleanup *old_cleanups;
1993 struct record_entry *rec;
1997 struct regcache *regcache;
1999 /* We restore the execution log from the open core bfd,
2001 if (core_bfd == NULL)
2004 /* "record_restore" can only be called when record list is empty. */
2005 gdb_assert (record_first.next == NULL);
2008 printf_filtered ("Restoring recording from core file.\n");
2010 /* Now need to find our special note section. */
2011 osec = bfd_get_section_by_name (core_bfd, "null0");
2012 osec_size = bfd_section_size (core_bfd, osec);
2014 printf_filtered ("Find precord section %s.\n",
2015 osec ? "succeeded" : "failed");
2019 printf_filtered ("%s", bfd_section_name (core_bfd, osec));
2021 /* Check the magic code. */
2022 bfdcore_read (core_bfd, osec, &magic, sizeof (magic), &bfd_offset);
2023 if (magic != RECORD_FILE_MAGIC)
2024 error (_("Version mis-match or file format error in core file %s."),
2025 bfd_get_filename (core_bfd));
2028 Reading 4-byte magic cookie RECORD_FILE_MAGIC (0x%s)\n",
2029 phex_nz (netorder32 (magic), 4));
2031 /* Restore the entries in recfd into record_arch_list_head and
2032 record_arch_list_tail. */
2033 record_arch_list_head = NULL;
2034 record_arch_list_tail = NULL;
2035 record_insn_num = 0;
2036 old_cleanups = make_cleanup (record_arch_list_cleanups, 0);
2037 regcache = get_current_regcache ();
2043 uint32_t regnum, len, signal, count;
2046 /* We are finished when offset reaches osec_size. */
2047 if (bfd_offset >= osec_size)
2049 bfdcore_read (core_bfd, osec, &tmpu8, sizeof (tmpu8), &bfd_offset);
2053 case record_reg: /* reg */
2054 /* Get register number to regnum. */
2055 bfdcore_read (core_bfd, osec, ®num,
2056 sizeof (regnum), &bfd_offset);
2057 regnum = netorder32 (regnum);
2059 rec = record_reg_alloc (regcache, regnum);
2062 bfdcore_read (core_bfd, osec, record_get_loc (rec),
2063 rec->u.reg.len, &bfd_offset);
2067 Reading register %d (1 plus %lu plus %d bytes)\n",
2069 (unsigned long) sizeof (regnum),
2073 case record_mem: /* mem */
2075 bfdcore_read (core_bfd, osec, &len,
2076 sizeof (len), &bfd_offset);
2077 len = netorder32 (len);
2080 bfdcore_read (core_bfd, osec, &addr,
2081 sizeof (addr), &bfd_offset);
2082 addr = netorder64 (addr);
2084 rec = record_mem_alloc (addr, len);
2087 bfdcore_read (core_bfd, osec, record_get_loc (rec),
2088 rec->u.mem.len, &bfd_offset);
2092 Reading memory %s (1 plus %lu plus %lu plus %d bytes)\n",
2093 paddress (get_current_arch (),
2095 (unsigned long) sizeof (addr),
2096 (unsigned long) sizeof (len),
2100 case record_end: /* end */
2101 rec = record_end_alloc ();
2104 /* Get signal value. */
2105 bfdcore_read (core_bfd, osec, &signal,
2106 sizeof (signal), &bfd_offset);
2107 signal = netorder32 (signal);
2108 rec->u.end.sigval = signal;
2110 /* Get insn count. */
2111 bfdcore_read (core_bfd, osec, &count,
2112 sizeof (count), &bfd_offset);
2113 count = netorder32 (count);
2114 rec->u.end.insn_num = count;
2115 record_insn_count = count + 1;
2118 Reading record_end (1 + %lu + %lu bytes), offset == %s\n",
2119 (unsigned long) sizeof (signal),
2120 (unsigned long) sizeof (count),
2121 paddress (get_current_arch (),
2126 error (_("Bad entry type in core file %s."),
2127 bfd_get_filename (core_bfd));
2131 /* Add rec to record arch list. */
2132 record_arch_list_add (rec);
2135 discard_cleanups (old_cleanups);
2137 /* Add record_arch_list_head to the end of record list. */
2138 record_first.next = record_arch_list_head;
2139 record_arch_list_head->prev = &record_first;
2140 record_arch_list_tail->next = NULL;
2141 record_list = &record_first;
2143 /* Update record_insn_max_num. */
2144 if (record_insn_num > record_insn_max_num)
2146 record_insn_max_num = record_insn_num;
2147 warning (_("Auto increase record/replay buffer limit to %d."),
2148 record_insn_max_num);
2152 printf_filtered (_("Restored records from core file %s.\n"),
2153 bfd_get_filename (core_bfd));
2155 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
2158 /* bfdcore_write -- write bytes into a core file section. */
2161 bfdcore_write (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2163 int ret = bfd_set_section_contents (obfd, osec, buf, *offset, len);
2168 error (_("Failed to write %d bytes to core file %s ('%s').\n"),
2169 len, bfd_get_filename (obfd),
2170 bfd_errmsg (bfd_get_error ()));
2173 /* Restore the execution log from a file. We use a modified elf
2174 corefile format, with an extra section for our data. */
2177 cmd_record_restore (char *args, int from_tty)
2179 core_file_command (args, from_tty);
2180 record_open (args, from_tty);
2184 record_save_cleanups (void *data)
2187 char *pathname = xstrdup (bfd_get_filename (obfd));
2193 /* Save the execution log to a file. We use a modified elf corefile
2194 format, with an extra section for our data. */
2197 cmd_record_save (char *args, int from_tty)
2199 char *recfilename, recfilename_buffer[40];
2201 struct record_entry *cur_record_list;
2203 struct regcache *regcache;
2204 struct gdbarch *gdbarch;
2205 struct cleanup *old_cleanups;
2206 struct cleanup *set_cleanups;
2209 asection *osec = NULL;
2212 if (strcmp (current_target.to_shortname, "record") != 0)
2213 error (_("This command can only be used with target 'record'.\n"
2214 "Use 'target record' first.\n"));
2220 /* Default recfile name is "gdb_record.PID". */
2221 snprintf (recfilename_buffer, sizeof (recfilename_buffer),
2222 "gdb_record.%d", PIDGET (inferior_ptid));
2223 recfilename = recfilename_buffer;
2226 /* Open the save file. */
2228 printf_filtered ("Saving execution log to core file '%s'\n", recfilename);
2230 /* Open the output file. */
2231 obfd = create_gcore_bfd (recfilename);
2232 old_cleanups = make_cleanup (record_save_cleanups, obfd);
2234 /* Save the current record entry to "cur_record_list". */
2235 cur_record_list = record_list;
2237 /* Get the values of regcache and gdbarch. */
2238 regcache = get_current_regcache ();
2239 gdbarch = get_regcache_arch (regcache);
2241 /* Disable the GDB operation record. */
2242 set_cleanups = record_gdb_operation_disable_set ();
2244 /* Reverse execute to the begin of record list. */
2247 /* Check for beginning and end of log. */
2248 if (record_list == &record_first)
2251 record_exec_insn (regcache, gdbarch, record_list);
2253 if (record_list->prev)
2254 record_list = record_list->prev;
2257 /* Compute the size needed for the extra bfd section. */
2258 save_size = 4; /* magic cookie */
2259 for (record_list = record_first.next; record_list;
2260 record_list = record_list->next)
2261 switch (record_list->type)
2264 save_size += 1 + 4 + 4;
2267 save_size += 1 + 4 + record_list->u.reg.len;
2270 save_size += 1 + 4 + 8 + record_list->u.mem.len;
2274 /* Make the new bfd section. */
2275 osec = bfd_make_section_anyway_with_flags (obfd, "precord",
2279 error (_("Failed to create 'precord' section for corefile %s: %s"),
2281 bfd_errmsg (bfd_get_error ()));
2282 bfd_set_section_size (obfd, osec, save_size);
2283 bfd_set_section_vma (obfd, osec, 0);
2284 bfd_set_section_alignment (obfd, osec, 0);
2285 bfd_section_lma (obfd, osec) = 0;
2287 /* Save corefile state. */
2288 write_gcore_file (obfd);
2290 /* Write out the record log. */
2291 /* Write the magic code. */
2292 magic = RECORD_FILE_MAGIC;
2295 Writing 4-byte magic cookie RECORD_FILE_MAGIC (0x%s)\n",
2296 phex_nz (magic, 4));
2297 bfdcore_write (obfd, osec, &magic, sizeof (magic), &bfd_offset);
2299 /* Save the entries to recfd and forward execute to the end of
2301 record_list = &record_first;
2305 if (record_list != &record_first)
2308 uint32_t regnum, len, signal, count;
2311 type = record_list->type;
2312 bfdcore_write (obfd, osec, &type, sizeof (type), &bfd_offset);
2314 switch (record_list->type)
2316 case record_reg: /* reg */
2319 Writing register %d (1 plus %lu plus %d bytes)\n",
2320 record_list->u.reg.num,
2321 (unsigned long) sizeof (regnum),
2322 record_list->u.reg.len);
2325 regnum = netorder32 (record_list->u.reg.num);
2326 bfdcore_write (obfd, osec, ®num,
2327 sizeof (regnum), &bfd_offset);
2330 bfdcore_write (obfd, osec, record_get_loc (record_list),
2331 record_list->u.reg.len, &bfd_offset);
2334 case record_mem: /* mem */
2337 Writing memory %s (1 plus %lu plus %lu plus %d bytes)\n",
2339 record_list->u.mem.addr),
2340 (unsigned long) sizeof (addr),
2341 (unsigned long) sizeof (len),
2342 record_list->u.mem.len);
2345 len = netorder32 (record_list->u.mem.len);
2346 bfdcore_write (obfd, osec, &len, sizeof (len), &bfd_offset);
2348 /* Write memaddr. */
2349 addr = netorder64 (record_list->u.mem.addr);
2350 bfdcore_write (obfd, osec, &addr,
2351 sizeof (addr), &bfd_offset);
2354 bfdcore_write (obfd, osec, record_get_loc (record_list),
2355 record_list->u.mem.len, &bfd_offset);
2361 Writing record_end (1 + %lu + %lu bytes)\n",
2362 (unsigned long) sizeof (signal),
2363 (unsigned long) sizeof (count));
2364 /* Write signal value. */
2365 signal = netorder32 (record_list->u.end.sigval);
2366 bfdcore_write (obfd, osec, &signal,
2367 sizeof (signal), &bfd_offset);
2369 /* Write insn count. */
2370 count = netorder32 (record_list->u.end.insn_num);
2371 bfdcore_write (obfd, osec, &count,
2372 sizeof (count), &bfd_offset);
2377 /* Execute entry. */
2378 record_exec_insn (regcache, gdbarch, record_list);
2380 if (record_list->next)
2381 record_list = record_list->next;
2386 /* Reverse execute to cur_record_list. */
2389 /* Check for beginning and end of log. */
2390 if (record_list == cur_record_list)
2393 record_exec_insn (regcache, gdbarch, record_list);
2395 if (record_list->prev)
2396 record_list = record_list->prev;
2399 do_cleanups (set_cleanups);
2401 discard_cleanups (old_cleanups);
2404 printf_filtered (_("Saved core file %s with execution log.\n"),
2409 _initialize_record (void)
2411 struct cmd_list_element *c;
2413 /* Init record_first. */
2414 record_first.prev = NULL;
2415 record_first.next = NULL;
2416 record_first.type = record_end;
2419 add_target (&record_ops);
2420 init_record_core_ops ();
2421 add_target (&record_core_ops);
2423 add_setshow_zinteger_cmd ("record", no_class, &record_debug,
2424 _("Set debugging of record/replay feature."),
2425 _("Show debugging of record/replay feature."),
2426 _("When enabled, debugging output for "
2427 "record/replay feature is displayed."),
2428 NULL, show_record_debug, &setdebuglist,
2431 c = add_prefix_cmd ("record", class_obscure, cmd_record_start,
2432 _("Abbreviated form of \"target record\" command."),
2433 &record_cmdlist, "record ", 0, &cmdlist);
2434 set_cmd_completer (c, filename_completer);
2436 add_com_alias ("rec", "record", class_obscure, 1);
2437 add_prefix_cmd ("record", class_support, set_record_command,
2438 _("Set record options"), &set_record_cmdlist,
2439 "set record ", 0, &setlist);
2440 add_alias_cmd ("rec", "record", class_obscure, 1, &setlist);
2441 add_prefix_cmd ("record", class_support, show_record_command,
2442 _("Show record options"), &show_record_cmdlist,
2443 "show record ", 0, &showlist);
2444 add_alias_cmd ("rec", "record", class_obscure, 1, &showlist);
2445 add_prefix_cmd ("record", class_support, info_record_command,
2446 _("Info record options"), &info_record_cmdlist,
2447 "info record ", 0, &infolist);
2448 add_alias_cmd ("rec", "record", class_obscure, 1, &infolist);
2450 c = add_cmd ("save", class_obscure, cmd_record_save,
2451 _("Save the execution log to a file.\n\
2452 Argument is optional filename.\n\
2453 Default filename is 'gdb_record.<process_id>'."),
2455 set_cmd_completer (c, filename_completer);
2457 c = add_cmd ("restore", class_obscure, cmd_record_restore,
2458 _("Restore the execution log from a file.\n\
2459 Argument is filename. File must be created with 'record save'."),
2461 set_cmd_completer (c, filename_completer);
2463 add_cmd ("delete", class_obscure, cmd_record_delete,
2464 _("Delete the rest of execution log and start recording it anew."),
2466 add_alias_cmd ("d", "delete", class_obscure, 1, &record_cmdlist);
2467 add_alias_cmd ("del", "delete", class_obscure, 1, &record_cmdlist);
2469 add_cmd ("stop", class_obscure, cmd_record_stop,
2470 _("Stop the record/replay target."),
2472 add_alias_cmd ("s", "stop", class_obscure, 1, &record_cmdlist);
2474 /* Record instructions number limit command. */
2475 add_setshow_boolean_cmd ("stop-at-limit", no_class,
2476 &record_stop_at_limit, _("\
2477 Set whether record/replay stops when record/replay buffer becomes full."), _("\
2478 Show whether record/replay stops when record/replay buffer becomes full."), _("\
2480 When ON, if the record/replay buffer becomes full, ask user what to do.\n\
2481 When OFF, if the record/replay buffer becomes full,\n\
2482 delete the oldest recorded instruction to make room for each new one."),
2484 &set_record_cmdlist, &show_record_cmdlist);
2485 add_setshow_uinteger_cmd ("insn-number-max", no_class,
2486 &record_insn_max_num,
2487 _("Set record/replay buffer limit."),
2488 _("Show record/replay buffer limit."), _("\
2489 Set the maximum number of instructions to be stored in the\n\
2490 record/replay buffer. Zero means unlimited. Default is 200000."),
2491 set_record_insn_max_num,
2492 NULL, &set_record_cmdlist, &show_record_cmdlist);