1 /* Process record and replay target for GDB, the GNU debugger.
3 Copyright (C) 2013-2014 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"
31 #include "record-full.h"
34 #include "event-loop.h"
41 /* This module implements "target record-full", also known as "process
42 record and replay". This target sits on top of a "normal" target
43 (a target that "has execution"), and provides a record and replay
44 functionality, including reverse debugging.
46 Target record has two modes: recording, and replaying.
48 In record mode, we intercept the to_resume and to_wait methods.
49 Whenever gdb resumes the target, we run the target in single step
50 mode, and we build up an execution log in which, for each executed
51 instruction, we record all changes in memory and register state.
52 This is invisible to the user, to whom it just looks like an
53 ordinary debugging session (except for performance degredation).
55 In replay mode, instead of actually letting the inferior run as a
56 process, we simulate its execution by playing back the recorded
57 execution log. For each instruction in the log, we simulate the
58 instruction's side effects by duplicating the changes that it would
59 have made on memory and registers. */
61 #define DEFAULT_RECORD_FULL_INSN_MAX_NUM 200000
63 #define RECORD_FULL_IS_REPLAY \
64 (record_full_list->next || execution_direction == EXEC_REVERSE)
66 #define RECORD_FULL_FILE_MAGIC netorder32(0x20091016)
68 /* These are the core structs of the process record functionality.
70 A record_full_entry is a record of the value change of a register
71 ("record_full_reg") or a part of memory ("record_full_mem"). And each
72 instruction must have a struct record_full_entry ("record_full_end")
73 that indicates that this is the last struct record_full_entry of this
76 Each struct record_full_entry is linked to "record_full_list" by "prev"
77 and "next" pointers. */
79 struct record_full_mem_entry
83 /* Set this flag if target memory for this entry
84 can no longer be accessed. */
85 int mem_entry_not_accessible;
89 gdb_byte buf[sizeof (gdb_byte *)];
93 struct record_full_reg_entry
100 gdb_byte buf[2 * sizeof (gdb_byte *)];
104 struct record_full_end_entry
106 enum gdb_signal sigval;
110 enum record_full_type
117 /* This is the data structure that makes up the execution log.
119 The execution log consists of a single linked list of entries
120 of type "struct record_full_entry". It is doubly linked so that it
121 can be traversed in either direction.
123 The start of the list is anchored by a struct called
124 "record_full_first". The pointer "record_full_list" either points
125 to the last entry that was added to the list (in record mode), or to
126 the next entry in the list that will be executed (in replay mode).
128 Each list element (struct record_full_entry), in addition to next
129 and prev pointers, consists of a union of three entry types: mem,
130 reg, and end. A field called "type" determines which entry type is
131 represented by a given list element.
133 Each instruction that is added to the execution log is represented
134 by a variable number of list elements ('entries'). The instruction
135 will have one "reg" entry for each register that is changed by
136 executing the instruction (including the PC in every case). It
137 will also have one "mem" entry for each memory change. Finally,
138 each instruction will have an "end" entry that separates it from
139 the changes associated with the next instruction. */
141 struct record_full_entry
143 struct record_full_entry *prev;
144 struct record_full_entry *next;
145 enum record_full_type type;
149 struct record_full_reg_entry reg;
151 struct record_full_mem_entry mem;
153 struct record_full_end_entry end;
157 /* If true, query if PREC cannot record memory
158 change of next instruction. */
159 int record_full_memory_query = 0;
161 struct record_full_core_buf_entry
163 struct record_full_core_buf_entry *prev;
164 struct target_section *p;
168 /* Record buf with core target. */
169 static gdb_byte *record_full_core_regbuf = NULL;
170 static struct target_section *record_full_core_start;
171 static struct target_section *record_full_core_end;
172 static struct record_full_core_buf_entry *record_full_core_buf_list = NULL;
174 /* The following variables are used for managing the linked list that
175 represents the execution log.
177 record_full_first is the anchor that holds down the beginning of
180 record_full_list serves two functions:
181 1) In record mode, it anchors the end of the list.
182 2) In replay mode, it traverses the list and points to
183 the next instruction that must be emulated.
185 record_full_arch_list_head and record_full_arch_list_tail are used
186 to manage a separate list, which is used to build up the change
187 elements of the currently executing instruction during record mode.
188 When this instruction has been completely annotated in the "arch
189 list", it will be appended to the main execution log. */
191 static struct record_full_entry record_full_first;
192 static struct record_full_entry *record_full_list = &record_full_first;
193 static struct record_full_entry *record_full_arch_list_head = NULL;
194 static struct record_full_entry *record_full_arch_list_tail = NULL;
196 /* 1 ask user. 0 auto delete the last struct record_full_entry. */
197 static int record_full_stop_at_limit = 1;
198 /* Maximum allowed number of insns in execution log. */
199 static unsigned int record_full_insn_max_num
200 = DEFAULT_RECORD_FULL_INSN_MAX_NUM;
201 /* Actual count of insns presently in execution log. */
202 static unsigned int record_full_insn_num = 0;
203 /* Count of insns logged so far (may be larger
204 than count of insns presently in execution log). */
205 static ULONGEST record_full_insn_count;
207 /* The target_ops of process record. */
208 static struct target_ops record_full_ops;
209 static struct target_ops record_full_core_ops;
211 /* Command lists for "set/show record full". */
212 static struct cmd_list_element *set_record_full_cmdlist;
213 static struct cmd_list_element *show_record_full_cmdlist;
215 /* Command list for "record full". */
216 static struct cmd_list_element *record_full_cmdlist;
218 /* The beneath function pointers. */
219 static struct target_ops *record_full_beneath_to_resume_ops;
220 static void (*record_full_beneath_to_resume) (struct target_ops *, ptid_t, int,
222 static struct target_ops *record_full_beneath_to_wait_ops;
223 static ptid_t (*record_full_beneath_to_wait) (struct target_ops *, ptid_t,
224 struct target_waitstatus *,
226 static struct target_ops *record_full_beneath_to_store_registers_ops;
227 static void (*record_full_beneath_to_store_registers) (struct target_ops *,
230 static struct target_ops *record_full_beneath_to_xfer_partial_ops;
231 static target_xfer_partial_ftype *record_full_beneath_to_xfer_partial;
233 (*record_full_beneath_to_insert_breakpoint) (struct gdbarch *,
234 struct bp_target_info *);
236 (*record_full_beneath_to_remove_breakpoint) (struct gdbarch *,
237 struct bp_target_info *);
238 static int (*record_full_beneath_to_stopped_by_watchpoint) (void);
239 static int (*record_full_beneath_to_stopped_data_address) (struct target_ops *,
242 (*record_full_beneath_to_async) (void (*) (enum inferior_event_type, void *),
245 static void record_full_goto_insn (struct record_full_entry *entry,
246 enum exec_direction_kind dir);
247 static void record_full_save (const char *recfilename);
249 /* Alloc and free functions for record_full_reg, record_full_mem, and
250 record_full_end entries. */
252 /* Alloc a record_full_reg record entry. */
254 static inline struct record_full_entry *
255 record_full_reg_alloc (struct regcache *regcache, int regnum)
257 struct record_full_entry *rec;
258 struct gdbarch *gdbarch = get_regcache_arch (regcache);
260 rec = xcalloc (1, sizeof (struct record_full_entry));
261 rec->type = record_full_reg;
262 rec->u.reg.num = regnum;
263 rec->u.reg.len = register_size (gdbarch, regnum);
264 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
265 rec->u.reg.u.ptr = (gdb_byte *) xmalloc (rec->u.reg.len);
270 /* Free a record_full_reg record entry. */
273 record_full_reg_release (struct record_full_entry *rec)
275 gdb_assert (rec->type == record_full_reg);
276 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
277 xfree (rec->u.reg.u.ptr);
281 /* Alloc a record_full_mem record entry. */
283 static inline struct record_full_entry *
284 record_full_mem_alloc (CORE_ADDR addr, int len)
286 struct record_full_entry *rec;
288 rec = xcalloc (1, sizeof (struct record_full_entry));
289 rec->type = record_full_mem;
290 rec->u.mem.addr = addr;
291 rec->u.mem.len = len;
292 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
293 rec->u.mem.u.ptr = (gdb_byte *) xmalloc (len);
298 /* Free a record_full_mem record entry. */
301 record_full_mem_release (struct record_full_entry *rec)
303 gdb_assert (rec->type == record_full_mem);
304 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
305 xfree (rec->u.mem.u.ptr);
309 /* Alloc a record_full_end record entry. */
311 static inline struct record_full_entry *
312 record_full_end_alloc (void)
314 struct record_full_entry *rec;
316 rec = xcalloc (1, sizeof (struct record_full_entry));
317 rec->type = record_full_end;
322 /* Free a record_full_end record entry. */
325 record_full_end_release (struct record_full_entry *rec)
330 /* Free one record entry, any type.
331 Return entry->type, in case caller wants to know. */
333 static inline enum record_full_type
334 record_full_entry_release (struct record_full_entry *rec)
336 enum record_full_type type = rec->type;
339 case record_full_reg:
340 record_full_reg_release (rec);
342 case record_full_mem:
343 record_full_mem_release (rec);
345 case record_full_end:
346 record_full_end_release (rec);
352 /* Free all record entries in list pointed to by REC. */
355 record_full_list_release (struct record_full_entry *rec)
366 record_full_entry_release (rec->next);
369 if (rec == &record_full_first)
371 record_full_insn_num = 0;
372 record_full_first.next = NULL;
375 record_full_entry_release (rec);
378 /* Free all record entries forward of the given list position. */
381 record_full_list_release_following (struct record_full_entry *rec)
383 struct record_full_entry *tmp = rec->next;
389 if (record_full_entry_release (tmp) == record_full_end)
391 record_full_insn_num--;
392 record_full_insn_count--;
398 /* Delete the first instruction from the beginning of the log, to make
399 room for adding a new instruction at the end of the log.
401 Note -- this function does not modify record_full_insn_num. */
404 record_full_list_release_first (void)
406 struct record_full_entry *tmp;
408 if (!record_full_first.next)
411 /* Loop until a record_full_end. */
414 /* Cut record_full_first.next out of the linked list. */
415 tmp = record_full_first.next;
416 record_full_first.next = tmp->next;
417 tmp->next->prev = &record_full_first;
419 /* tmp is now isolated, and can be deleted. */
420 if (record_full_entry_release (tmp) == record_full_end)
421 break; /* End loop at first record_full_end. */
423 if (!record_full_first.next)
425 gdb_assert (record_full_insn_num == 1);
426 break; /* End loop when list is empty. */
431 /* Add a struct record_full_entry to record_full_arch_list. */
434 record_full_arch_list_add (struct record_full_entry *rec)
436 if (record_debug > 1)
437 fprintf_unfiltered (gdb_stdlog,
438 "Process record: record_full_arch_list_add %s.\n",
439 host_address_to_string (rec));
441 if (record_full_arch_list_tail)
443 record_full_arch_list_tail->next = rec;
444 rec->prev = record_full_arch_list_tail;
445 record_full_arch_list_tail = rec;
449 record_full_arch_list_head = rec;
450 record_full_arch_list_tail = rec;
454 /* Return the value storage location of a record entry. */
455 static inline gdb_byte *
456 record_full_get_loc (struct record_full_entry *rec)
459 case record_full_mem:
460 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
461 return rec->u.mem.u.ptr;
463 return rec->u.mem.u.buf;
464 case record_full_reg:
465 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
466 return rec->u.reg.u.ptr;
468 return rec->u.reg.u.buf;
469 case record_full_end:
471 gdb_assert_not_reached ("unexpected record_full_entry type");
476 /* Record the value of a register NUM to record_full_arch_list. */
479 record_full_arch_list_add_reg (struct regcache *regcache, int regnum)
481 struct record_full_entry *rec;
483 if (record_debug > 1)
484 fprintf_unfiltered (gdb_stdlog,
485 "Process record: add register num = %d to "
489 rec = record_full_reg_alloc (regcache, regnum);
491 regcache_raw_read (regcache, regnum, record_full_get_loc (rec));
493 record_full_arch_list_add (rec);
498 /* Record the value of a region of memory whose address is ADDR and
499 length is LEN to record_full_arch_list. */
502 record_full_arch_list_add_mem (CORE_ADDR addr, int len)
504 struct record_full_entry *rec;
506 if (record_debug > 1)
507 fprintf_unfiltered (gdb_stdlog,
508 "Process record: add mem addr = %s len = %d to "
510 paddress (target_gdbarch (), addr), len);
512 if (!addr) /* FIXME: Why? Some arch must permit it... */
515 rec = record_full_mem_alloc (addr, len);
517 if (record_read_memory (target_gdbarch (), addr,
518 record_full_get_loc (rec), len))
520 record_full_mem_release (rec);
524 record_full_arch_list_add (rec);
529 /* Add a record_full_end type struct record_full_entry to
530 record_full_arch_list. */
533 record_full_arch_list_add_end (void)
535 struct record_full_entry *rec;
537 if (record_debug > 1)
538 fprintf_unfiltered (gdb_stdlog,
539 "Process record: add end to arch list.\n");
541 rec = record_full_end_alloc ();
542 rec->u.end.sigval = GDB_SIGNAL_0;
543 rec->u.end.insn_num = ++record_full_insn_count;
545 record_full_arch_list_add (rec);
551 record_full_check_insn_num (int set_terminal)
553 if (record_full_insn_num == record_full_insn_max_num)
555 /* Ask user what to do. */
556 if (record_full_stop_at_limit)
561 target_terminal_ours ();
562 q = yquery (_("Do you want to auto delete previous execution "
563 "log entries when record/replay buffer becomes "
564 "full (record full stop-at-limit)?"));
566 target_terminal_inferior ();
568 record_full_stop_at_limit = 0;
570 error (_("Process record: stopped by user."));
576 record_full_arch_list_cleanups (void *ignore)
578 record_full_list_release (record_full_arch_list_tail);
581 /* Before inferior step (when GDB record the running message, inferior
582 only can step), GDB will call this function to record the values to
583 record_full_list. This function will call gdbarch_process_record to
584 record the running message of inferior and set them to
585 record_full_arch_list, and add it to record_full_list. */
588 record_full_message (struct regcache *regcache, enum gdb_signal signal)
591 struct gdbarch *gdbarch = get_regcache_arch (regcache);
592 struct cleanup *old_cleanups
593 = make_cleanup (record_full_arch_list_cleanups, 0);
595 record_full_arch_list_head = NULL;
596 record_full_arch_list_tail = NULL;
598 /* Check record_full_insn_num. */
599 record_full_check_insn_num (1);
601 /* If gdb sends a signal value to target_resume,
602 save it in the 'end' field of the previous instruction.
604 Maybe process record should record what really happened,
605 rather than what gdb pretends has happened.
607 So if Linux delivered the signal to the child process during
608 the record mode, we will record it and deliver it again in
611 If user says "ignore this signal" during the record mode, then
612 it will be ignored again during the replay mode (no matter if
613 the user says something different, like "deliver this signal"
614 during the replay mode).
616 User should understand that nothing he does during the replay
617 mode will change the behavior of the child. If he tries,
618 then that is a user error.
620 But we should still deliver the signal to gdb during the replay,
621 if we delivered it during the recording. Therefore we should
622 record the signal during record_full_wait, not
623 record_full_resume. */
624 if (record_full_list != &record_full_first) /* FIXME better way to check */
626 gdb_assert (record_full_list->type == record_full_end);
627 record_full_list->u.end.sigval = signal;
630 if (signal == GDB_SIGNAL_0
631 || !gdbarch_process_record_signal_p (gdbarch))
632 ret = gdbarch_process_record (gdbarch,
634 regcache_read_pc (regcache));
636 ret = gdbarch_process_record_signal (gdbarch,
641 error (_("Process record: inferior program stopped."));
643 error (_("Process record: failed to record execution log."));
645 discard_cleanups (old_cleanups);
647 record_full_list->next = record_full_arch_list_head;
648 record_full_arch_list_head->prev = record_full_list;
649 record_full_list = record_full_arch_list_tail;
651 if (record_full_insn_num == record_full_insn_max_num)
652 record_full_list_release_first ();
654 record_full_insn_num++;
659 struct record_full_message_args {
660 struct regcache *regcache;
661 enum gdb_signal signal;
665 record_full_message_wrapper (void *args)
667 struct record_full_message_args *record_full_args = args;
669 return record_full_message (record_full_args->regcache,
670 record_full_args->signal);
674 record_full_message_wrapper_safe (struct regcache *regcache,
675 enum gdb_signal signal)
677 struct record_full_message_args args;
679 args.regcache = regcache;
680 args.signal = signal;
682 return catch_errors (record_full_message_wrapper, &args, NULL,
686 /* Set to 1 if record_full_store_registers and record_full_xfer_partial
687 doesn't need record. */
689 static int record_full_gdb_operation_disable = 0;
692 record_full_gdb_operation_disable_set (void)
694 struct cleanup *old_cleanups = NULL;
697 make_cleanup_restore_integer (&record_full_gdb_operation_disable);
698 record_full_gdb_operation_disable = 1;
703 /* Flag set to TRUE for target_stopped_by_watchpoint. */
704 static int record_full_hw_watchpoint = 0;
706 /* Execute one instruction from the record log. Each instruction in
707 the log will be represented by an arbitrary sequence of register
708 entries and memory entries, followed by an 'end' entry. */
711 record_full_exec_insn (struct regcache *regcache,
712 struct gdbarch *gdbarch,
713 struct record_full_entry *entry)
717 case record_full_reg: /* reg */
719 gdb_byte reg[MAX_REGISTER_SIZE];
721 if (record_debug > 1)
722 fprintf_unfiltered (gdb_stdlog,
723 "Process record: record_full_reg %s to "
724 "inferior num = %d.\n",
725 host_address_to_string (entry),
728 regcache_cooked_read (regcache, entry->u.reg.num, reg);
729 regcache_cooked_write (regcache, entry->u.reg.num,
730 record_full_get_loc (entry));
731 memcpy (record_full_get_loc (entry), reg, entry->u.reg.len);
735 case record_full_mem: /* mem */
737 /* Nothing to do if the entry is flagged not_accessible. */
738 if (!entry->u.mem.mem_entry_not_accessible)
740 gdb_byte *mem = alloca (entry->u.mem.len);
742 if (record_debug > 1)
743 fprintf_unfiltered (gdb_stdlog,
744 "Process record: record_full_mem %s to "
745 "inferior addr = %s len = %d.\n",
746 host_address_to_string (entry),
747 paddress (gdbarch, entry->u.mem.addr),
750 if (record_read_memory (gdbarch,
751 entry->u.mem.addr, mem, entry->u.mem.len))
752 entry->u.mem.mem_entry_not_accessible = 1;
755 if (target_write_memory (entry->u.mem.addr,
756 record_full_get_loc (entry),
759 entry->u.mem.mem_entry_not_accessible = 1;
761 warning (_("Process record: error writing memory at "
762 "addr = %s len = %d."),
763 paddress (gdbarch, entry->u.mem.addr),
768 memcpy (record_full_get_loc (entry), mem,
771 /* We've changed memory --- check if a hardware
772 watchpoint should trap. Note that this
773 presently assumes the target beneath supports
774 continuable watchpoints. On non-continuable
775 watchpoints target, we'll want to check this
776 _before_ actually doing the memory change, and
777 not doing the change at all if the watchpoint
779 if (hardware_watchpoint_inserted_in_range
780 (get_regcache_aspace (regcache),
781 entry->u.mem.addr, entry->u.mem.len))
782 record_full_hw_watchpoint = 1;
791 static struct target_ops *tmp_to_resume_ops;
792 static void (*tmp_to_resume) (struct target_ops *, ptid_t, int,
794 static struct target_ops *tmp_to_wait_ops;
795 static ptid_t (*tmp_to_wait) (struct target_ops *, ptid_t,
796 struct target_waitstatus *,
798 static struct target_ops *tmp_to_store_registers_ops;
799 static void (*tmp_to_store_registers) (struct target_ops *,
802 static struct target_ops *tmp_to_xfer_partial_ops;
803 static target_xfer_partial_ftype *tmp_to_xfer_partial;
804 static int (*tmp_to_insert_breakpoint) (struct gdbarch *,
805 struct bp_target_info *);
806 static int (*tmp_to_remove_breakpoint) (struct gdbarch *,
807 struct bp_target_info *);
808 static int (*tmp_to_stopped_by_watchpoint) (void);
809 static int (*tmp_to_stopped_data_address) (struct target_ops *, CORE_ADDR *);
810 static int (*tmp_to_stopped_data_address) (struct target_ops *, CORE_ADDR *);
811 static void (*tmp_to_async) (void (*) (enum inferior_event_type, void *), void *);
813 static void record_full_restore (void);
815 /* Asynchronous signal handle registered as event loop source for when
816 we have pending events ready to be passed to the core. */
818 static struct async_event_handler *record_full_async_inferior_event_token;
821 record_full_async_inferior_event_handler (gdb_client_data data)
823 inferior_event_handler (INF_REG_EVENT, NULL);
826 /* Open the process record target. */
829 record_full_core_open_1 (char *name, int from_tty)
831 struct regcache *regcache = get_current_regcache ();
832 int regnum = gdbarch_num_regs (get_regcache_arch (regcache));
835 /* Get record_full_core_regbuf. */
836 target_fetch_registers (regcache, -1);
837 record_full_core_regbuf = xmalloc (MAX_REGISTER_SIZE * regnum);
838 for (i = 0; i < regnum; i ++)
839 regcache_raw_collect (regcache, i,
840 record_full_core_regbuf + MAX_REGISTER_SIZE * i);
842 /* Get record_full_core_start and record_full_core_end. */
843 if (build_section_table (core_bfd, &record_full_core_start,
844 &record_full_core_end))
846 xfree (record_full_core_regbuf);
847 record_full_core_regbuf = NULL;
848 error (_("\"%s\": Can't find sections: %s"),
849 bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ()));
852 push_target (&record_full_core_ops);
853 record_full_restore ();
856 /* "to_open" target method for 'live' processes. */
859 record_full_open_1 (char *name, int from_tty)
862 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_open\n");
865 if (!target_has_execution)
866 error (_("Process record: the program is not being run."));
868 error (_("Process record target can't debug inferior in non-stop mode "
871 if (!gdbarch_process_record_p (target_gdbarch ()))
872 error (_("Process record: the current architecture doesn't support "
873 "record function."));
876 error (_("Could not find 'to_resume' method on the target stack."));
878 error (_("Could not find 'to_wait' method on the target stack."));
879 if (!tmp_to_store_registers)
880 error (_("Could not find 'to_store_registers' "
881 "method on the target stack."));
882 if (!tmp_to_insert_breakpoint)
883 error (_("Could not find 'to_insert_breakpoint' "
884 "method on the target stack."));
885 if (!tmp_to_remove_breakpoint)
886 error (_("Could not find 'to_remove_breakpoint' "
887 "method on the target stack."));
888 if (!tmp_to_stopped_by_watchpoint)
889 error (_("Could not find 'to_stopped_by_watchpoint' "
890 "method on the target stack."));
891 if (!tmp_to_stopped_data_address)
892 error (_("Could not find 'to_stopped_data_address' "
893 "method on the target stack."));
895 push_target (&record_full_ops);
898 static void record_full_init_record_breakpoints (void);
900 /* "to_open" target method. Open the process record target. */
903 record_full_open (char *name, int from_tty)
905 struct target_ops *t;
908 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_open\n");
910 /* Check if record target is already running. */
911 if (current_target.to_stratum == record_stratum)
912 error (_("Process record target already running. Use \"record stop\" to "
913 "stop record target first."));
915 /* Reset the tmp beneath pointers. */
916 tmp_to_resume_ops = NULL;
917 tmp_to_resume = NULL;
918 tmp_to_wait_ops = NULL;
920 tmp_to_store_registers_ops = NULL;
921 tmp_to_store_registers = NULL;
922 tmp_to_xfer_partial_ops = NULL;
923 tmp_to_xfer_partial = NULL;
924 tmp_to_insert_breakpoint = NULL;
925 tmp_to_remove_breakpoint = NULL;
926 tmp_to_stopped_by_watchpoint = NULL;
927 tmp_to_stopped_data_address = NULL;
930 /* Set the beneath function pointers. */
931 for (t = current_target.beneath; t != NULL; t = t->beneath)
935 tmp_to_resume = t->to_resume;
936 tmp_to_resume_ops = t;
940 tmp_to_wait = t->to_wait;
943 if (!tmp_to_store_registers)
945 tmp_to_store_registers = t->to_store_registers;
946 tmp_to_store_registers_ops = t;
948 if (!tmp_to_xfer_partial)
950 tmp_to_xfer_partial = t->to_xfer_partial;
951 tmp_to_xfer_partial_ops = t;
953 if (!tmp_to_insert_breakpoint)
954 tmp_to_insert_breakpoint = t->to_insert_breakpoint;
955 if (!tmp_to_remove_breakpoint)
956 tmp_to_remove_breakpoint = t->to_remove_breakpoint;
957 if (!tmp_to_stopped_by_watchpoint)
958 tmp_to_stopped_by_watchpoint = t->to_stopped_by_watchpoint;
959 if (!tmp_to_stopped_data_address)
960 tmp_to_stopped_data_address = t->to_stopped_data_address;
962 tmp_to_async = t->to_async;
964 if (!tmp_to_xfer_partial)
965 error (_("Could not find 'to_xfer_partial' method on the target stack."));
968 record_full_insn_num = 0;
969 record_full_insn_count = 0;
970 record_full_list = &record_full_first;
971 record_full_list->next = NULL;
973 /* Set the tmp beneath pointers to beneath pointers. */
974 record_full_beneath_to_resume_ops = tmp_to_resume_ops;
975 record_full_beneath_to_resume = tmp_to_resume;
976 record_full_beneath_to_wait_ops = tmp_to_wait_ops;
977 record_full_beneath_to_wait = tmp_to_wait;
978 record_full_beneath_to_store_registers_ops = tmp_to_store_registers_ops;
979 record_full_beneath_to_store_registers = tmp_to_store_registers;
980 record_full_beneath_to_xfer_partial_ops = tmp_to_xfer_partial_ops;
981 record_full_beneath_to_xfer_partial = tmp_to_xfer_partial;
982 record_full_beneath_to_insert_breakpoint = tmp_to_insert_breakpoint;
983 record_full_beneath_to_remove_breakpoint = tmp_to_remove_breakpoint;
984 record_full_beneath_to_stopped_by_watchpoint = tmp_to_stopped_by_watchpoint;
985 record_full_beneath_to_stopped_data_address = tmp_to_stopped_data_address;
986 record_full_beneath_to_async = tmp_to_async;
989 record_full_core_open_1 (name, from_tty);
991 record_full_open_1 (name, from_tty);
993 /* Register extra event sources in the event loop. */
994 record_full_async_inferior_event_token
995 = create_async_event_handler (record_full_async_inferior_event_handler,
998 record_full_init_record_breakpoints ();
1000 observer_notify_record_changed (current_inferior (), 1);
1003 /* "to_close" target method. Close the process record target. */
1006 record_full_close (void)
1008 struct record_full_core_buf_entry *entry;
1011 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_close\n");
1013 record_full_list_release (record_full_list);
1015 /* Release record_full_core_regbuf. */
1016 if (record_full_core_regbuf)
1018 xfree (record_full_core_regbuf);
1019 record_full_core_regbuf = NULL;
1022 /* Release record_full_core_buf_list. */
1023 if (record_full_core_buf_list)
1025 for (entry = record_full_core_buf_list->prev; entry;
1026 entry = entry->prev)
1028 xfree (record_full_core_buf_list);
1029 record_full_core_buf_list = entry;
1031 record_full_core_buf_list = NULL;
1034 if (record_full_async_inferior_event_token)
1035 delete_async_event_handler (&record_full_async_inferior_event_token);
1038 static int record_full_resume_step = 0;
1040 /* True if we've been resumed, and so each record_full_wait call should
1041 advance execution. If this is false, record_full_wait will return a
1042 TARGET_WAITKIND_IGNORE. */
1043 static int record_full_resumed = 0;
1045 /* The execution direction of the last resume we got. This is
1046 necessary for async mode. Vis (order is not strictly accurate):
1048 1. user has the global execution direction set to forward
1049 2. user does a reverse-step command
1050 3. record_full_resume is called with global execution direction
1051 temporarily switched to reverse
1052 4. GDB's execution direction is reverted back to forward
1053 5. target record notifies event loop there's an event to handle
1054 6. infrun asks the target which direction was it going, and switches
1055 the global execution direction accordingly (to reverse)
1056 7. infrun polls an event out of the record target, and handles it
1057 8. GDB goes back to the event loop, and goto #4.
1059 static enum exec_direction_kind record_full_execution_dir = EXEC_FORWARD;
1061 /* "to_resume" target method. Resume the process record target. */
1064 record_full_resume (struct target_ops *ops, ptid_t ptid, int step,
1065 enum gdb_signal signal)
1067 record_full_resume_step = step;
1068 record_full_resumed = 1;
1069 record_full_execution_dir = execution_direction;
1071 if (!RECORD_FULL_IS_REPLAY)
1073 struct gdbarch *gdbarch = target_thread_architecture (ptid);
1075 record_full_message (get_current_regcache (), signal);
1079 /* This is not hard single step. */
1080 if (!gdbarch_software_single_step_p (gdbarch))
1082 /* This is a normal continue. */
1087 /* This arch support soft sigle step. */
1088 if (single_step_breakpoints_inserted ())
1090 /* This is a soft single step. */
1091 record_full_resume_step = 1;
1095 /* This is a continue.
1096 Try to insert a soft single step breakpoint. */
1097 if (!gdbarch_software_single_step (gdbarch,
1098 get_current_frame ()))
1100 /* This system don't want use soft single step.
1101 Use hard sigle step. */
1108 /* Make sure the target beneath reports all signals. */
1109 target_pass_signals (0, NULL);
1111 record_full_beneath_to_resume (record_full_beneath_to_resume_ops,
1112 ptid, step, signal);
1115 /* We are about to start executing the inferior (or simulate it),
1116 let's register it with the event loop. */
1117 if (target_can_async_p ())
1119 target_async (inferior_event_handler, 0);
1120 /* Notify the event loop there's an event to wait for. We do
1121 most of the work in record_full_wait. */
1122 mark_async_event_handler (record_full_async_inferior_event_token);
1126 static int record_full_get_sig = 0;
1128 /* SIGINT signal handler, registered by "to_wait" method. */
1131 record_full_sig_handler (int signo)
1134 fprintf_unfiltered (gdb_stdlog, "Process record: get a signal\n");
1136 /* It will break the running inferior in replay mode. */
1137 record_full_resume_step = 1;
1139 /* It will let record_full_wait set inferior status to get the signal
1141 record_full_get_sig = 1;
1145 record_full_wait_cleanups (void *ignore)
1147 if (execution_direction == EXEC_REVERSE)
1149 if (record_full_list->next)
1150 record_full_list = record_full_list->next;
1153 record_full_list = record_full_list->prev;
1156 /* "to_wait" target method for process record target.
1158 In record mode, the target is always run in singlestep mode
1159 (even when gdb says to continue). The to_wait method intercepts
1160 the stop events and determines which ones are to be passed on to
1161 gdb. Most stop events are just singlestep events that gdb is not
1162 to know about, so the to_wait method just records them and keeps
1165 In replay mode, this function emulates the recorded execution log,
1166 one instruction at a time (forward or backward), and determines
1170 record_full_wait_1 (struct target_ops *ops,
1171 ptid_t ptid, struct target_waitstatus *status,
1174 struct cleanup *set_cleanups = record_full_gdb_operation_disable_set ();
1177 fprintf_unfiltered (gdb_stdlog,
1178 "Process record: record_full_wait "
1179 "record_full_resume_step = %d, "
1180 "record_full_resumed = %d, direction=%s\n",
1181 record_full_resume_step, record_full_resumed,
1182 record_full_execution_dir == EXEC_FORWARD
1183 ? "forward" : "reverse");
1185 if (!record_full_resumed)
1187 gdb_assert ((options & TARGET_WNOHANG) != 0);
1189 /* No interesting event. */
1190 status->kind = TARGET_WAITKIND_IGNORE;
1191 return minus_one_ptid;
1194 record_full_get_sig = 0;
1195 signal (SIGINT, record_full_sig_handler);
1197 if (!RECORD_FULL_IS_REPLAY && ops != &record_full_core_ops)
1199 if (record_full_resume_step)
1201 /* This is a single step. */
1202 return record_full_beneath_to_wait (record_full_beneath_to_wait_ops,
1203 ptid, status, options);
1207 /* This is not a single step. */
1210 struct gdbarch *gdbarch = target_thread_architecture (inferior_ptid);
1214 ret = record_full_beneath_to_wait
1215 (record_full_beneath_to_wait_ops, ptid, status, options);
1216 if (status->kind == TARGET_WAITKIND_IGNORE)
1219 fprintf_unfiltered (gdb_stdlog,
1220 "Process record: record_full_wait "
1221 "target beneath not done yet\n");
1225 if (single_step_breakpoints_inserted ())
1226 remove_single_step_breakpoints ();
1228 if (record_full_resume_step)
1231 /* Is this a SIGTRAP? */
1232 if (status->kind == TARGET_WAITKIND_STOPPED
1233 && status->value.sig == GDB_SIGNAL_TRAP)
1235 struct regcache *regcache;
1236 struct address_space *aspace;
1238 /* Yes -- this is likely our single-step finishing,
1239 but check if there's any reason the core would be
1240 interested in the event. */
1242 registers_changed ();
1243 regcache = get_current_regcache ();
1244 tmp_pc = regcache_read_pc (regcache);
1245 aspace = get_regcache_aspace (regcache);
1247 if (target_stopped_by_watchpoint ())
1249 /* Always interested in watchpoints. */
1251 else if (breakpoint_inserted_here_p (aspace, tmp_pc))
1253 /* There is a breakpoint here. Let the core
1255 if (software_breakpoint_inserted_here_p (aspace, tmp_pc))
1257 struct gdbarch *gdbarch
1258 = get_regcache_arch (regcache);
1259 CORE_ADDR decr_pc_after_break
1260 = gdbarch_decr_pc_after_break (gdbarch);
1261 if (decr_pc_after_break)
1262 regcache_write_pc (regcache,
1263 tmp_pc + decr_pc_after_break);
1268 /* This is a single-step trap. Record the
1269 insn and issue another step.
1270 FIXME: this part can be a random SIGTRAP too.
1271 But GDB cannot handle it. */
1274 if (!record_full_message_wrapper_safe (regcache,
1277 status->kind = TARGET_WAITKIND_STOPPED;
1278 status->value.sig = GDB_SIGNAL_0;
1282 if (gdbarch_software_single_step_p (gdbarch))
1284 /* Try to insert the software single step breakpoint.
1285 If insert success, set step to 0. */
1286 set_executing (inferior_ptid, 0);
1287 reinit_frame_cache ();
1288 if (gdbarch_software_single_step (gdbarch,
1289 get_current_frame ()))
1291 set_executing (inferior_ptid, 1);
1295 fprintf_unfiltered (gdb_stdlog,
1296 "Process record: record_full_wait "
1297 "issuing one more step in the "
1298 "target beneath\n");
1299 record_full_beneath_to_resume
1300 (record_full_beneath_to_resume_ops, ptid, step,
1306 /* The inferior is broken by a breakpoint or a signal. */
1315 struct regcache *regcache = get_current_regcache ();
1316 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1317 struct address_space *aspace = get_regcache_aspace (regcache);
1318 int continue_flag = 1;
1319 int first_record_full_end = 1;
1320 struct cleanup *old_cleanups
1321 = make_cleanup (record_full_wait_cleanups, 0);
1324 record_full_hw_watchpoint = 0;
1325 status->kind = TARGET_WAITKIND_STOPPED;
1327 /* Check breakpoint when forward execute. */
1328 if (execution_direction == EXEC_FORWARD)
1330 tmp_pc = regcache_read_pc (regcache);
1331 if (breakpoint_inserted_here_p (aspace, tmp_pc))
1333 int decr_pc_after_break = gdbarch_decr_pc_after_break (gdbarch);
1336 fprintf_unfiltered (gdb_stdlog,
1337 "Process record: break at %s.\n",
1338 paddress (gdbarch, tmp_pc));
1340 if (decr_pc_after_break
1341 && !record_full_resume_step
1342 && software_breakpoint_inserted_here_p (aspace, tmp_pc))
1343 regcache_write_pc (regcache,
1344 tmp_pc + decr_pc_after_break);
1349 /* If GDB is in terminal_inferior mode, it will not get the signal.
1350 And in GDB replay mode, GDB doesn't need to be in terminal_inferior
1351 mode, because inferior will not executed.
1352 Then set it to terminal_ours to make GDB get the signal. */
1353 target_terminal_ours ();
1355 /* In EXEC_FORWARD mode, record_full_list points to the tail of prev
1357 if (execution_direction == EXEC_FORWARD && record_full_list->next)
1358 record_full_list = record_full_list->next;
1360 /* Loop over the record_full_list, looking for the next place to
1364 /* Check for beginning and end of log. */
1365 if (execution_direction == EXEC_REVERSE
1366 && record_full_list == &record_full_first)
1368 /* Hit beginning of record log in reverse. */
1369 status->kind = TARGET_WAITKIND_NO_HISTORY;
1372 if (execution_direction != EXEC_REVERSE && !record_full_list->next)
1374 /* Hit end of record log going forward. */
1375 status->kind = TARGET_WAITKIND_NO_HISTORY;
1379 record_full_exec_insn (regcache, gdbarch, record_full_list);
1381 if (record_full_list->type == record_full_end)
1383 if (record_debug > 1)
1384 fprintf_unfiltered (gdb_stdlog,
1385 "Process record: record_full_end %s to "
1387 host_address_to_string (record_full_list));
1389 if (first_record_full_end && execution_direction == EXEC_REVERSE)
1391 /* When reverse excute, the first record_full_end is the
1392 part of current instruction. */
1393 first_record_full_end = 0;
1397 /* In EXEC_REVERSE mode, this is the record_full_end of prev
1399 In EXEC_FORWARD mode, this is the record_full_end of
1400 current instruction. */
1402 if (record_full_resume_step)
1404 if (record_debug > 1)
1405 fprintf_unfiltered (gdb_stdlog,
1406 "Process record: step.\n");
1410 /* check breakpoint */
1411 tmp_pc = regcache_read_pc (regcache);
1412 if (breakpoint_inserted_here_p (aspace, tmp_pc))
1414 int decr_pc_after_break
1415 = gdbarch_decr_pc_after_break (gdbarch);
1418 fprintf_unfiltered (gdb_stdlog,
1419 "Process record: break "
1421 paddress (gdbarch, tmp_pc));
1422 if (decr_pc_after_break
1423 && execution_direction == EXEC_FORWARD
1424 && !record_full_resume_step
1425 && software_breakpoint_inserted_here_p (aspace,
1427 regcache_write_pc (regcache,
1428 tmp_pc + decr_pc_after_break);
1432 if (record_full_hw_watchpoint)
1435 fprintf_unfiltered (gdb_stdlog,
1436 "Process record: hit hw "
1440 /* Check target signal */
1441 if (record_full_list->u.end.sigval != GDB_SIGNAL_0)
1442 /* FIXME: better way to check */
1449 if (execution_direction == EXEC_REVERSE)
1451 if (record_full_list->prev)
1452 record_full_list = record_full_list->prev;
1456 if (record_full_list->next)
1457 record_full_list = record_full_list->next;
1461 while (continue_flag);
1464 if (record_full_get_sig)
1465 status->value.sig = GDB_SIGNAL_INT;
1466 else if (record_full_list->u.end.sigval != GDB_SIGNAL_0)
1467 /* FIXME: better way to check */
1468 status->value.sig = record_full_list->u.end.sigval;
1470 status->value.sig = GDB_SIGNAL_TRAP;
1472 discard_cleanups (old_cleanups);
1475 signal (SIGINT, handle_sigint);
1477 do_cleanups (set_cleanups);
1478 return inferior_ptid;
1482 record_full_wait (struct target_ops *ops,
1483 ptid_t ptid, struct target_waitstatus *status,
1488 return_ptid = record_full_wait_1 (ops, ptid, status, options);
1489 if (status->kind != TARGET_WAITKIND_IGNORE)
1491 /* We're reporting a stop. Make sure any spurious
1492 target_wait(WNOHANG) doesn't advance the target until the
1493 core wants us resumed again. */
1494 record_full_resumed = 0;
1500 record_full_stopped_by_watchpoint (void)
1502 if (RECORD_FULL_IS_REPLAY)
1503 return record_full_hw_watchpoint;
1505 return record_full_beneath_to_stopped_by_watchpoint ();
1509 record_full_stopped_data_address (struct target_ops *ops, CORE_ADDR *addr_p)
1511 if (RECORD_FULL_IS_REPLAY)
1514 return record_full_beneath_to_stopped_data_address (ops, addr_p);
1517 /* Record registers change (by user or by GDB) to list as an instruction. */
1520 record_full_registers_change (struct regcache *regcache, int regnum)
1522 /* Check record_full_insn_num. */
1523 record_full_check_insn_num (0);
1525 record_full_arch_list_head = NULL;
1526 record_full_arch_list_tail = NULL;
1532 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
1534 if (record_full_arch_list_add_reg (regcache, i))
1536 record_full_list_release (record_full_arch_list_tail);
1537 error (_("Process record: failed to record execution log."));
1543 if (record_full_arch_list_add_reg (regcache, regnum))
1545 record_full_list_release (record_full_arch_list_tail);
1546 error (_("Process record: failed to record execution log."));
1549 if (record_full_arch_list_add_end ())
1551 record_full_list_release (record_full_arch_list_tail);
1552 error (_("Process record: failed to record execution log."));
1554 record_full_list->next = record_full_arch_list_head;
1555 record_full_arch_list_head->prev = record_full_list;
1556 record_full_list = record_full_arch_list_tail;
1558 if (record_full_insn_num == record_full_insn_max_num)
1559 record_full_list_release_first ();
1561 record_full_insn_num++;
1564 /* "to_store_registers" method for process record target. */
1567 record_full_store_registers (struct target_ops *ops,
1568 struct regcache *regcache,
1571 if (!record_full_gdb_operation_disable)
1573 if (RECORD_FULL_IS_REPLAY)
1577 /* Let user choose if he wants to write register or not. */
1580 query (_("Because GDB is in replay mode, changing the "
1581 "value of a register will make the execution "
1582 "log unusable from this point onward. "
1583 "Change all registers?"));
1586 query (_("Because GDB is in replay mode, changing the value "
1587 "of a register will make the execution log unusable "
1588 "from this point onward. Change register %s?"),
1589 gdbarch_register_name (get_regcache_arch (regcache),
1594 /* Invalidate the value of regcache that was set in function
1595 "regcache_raw_write". */
1601 i < gdbarch_num_regs (get_regcache_arch (regcache));
1603 regcache_invalidate (regcache, i);
1606 regcache_invalidate (regcache, regno);
1608 error (_("Process record canceled the operation."));
1611 /* Destroy the record from here forward. */
1612 record_full_list_release_following (record_full_list);
1615 record_full_registers_change (regcache, regno);
1617 record_full_beneath_to_store_registers
1618 (record_full_beneath_to_store_registers_ops, regcache, regno);
1621 /* "to_xfer_partial" method. Behavior is conditional on
1622 RECORD_FULL_IS_REPLAY.
1623 In replay mode, we cannot write memory unles we are willing to
1624 invalidate the record/replay log from this point forward. */
1627 record_full_xfer_partial (struct target_ops *ops, enum target_object object,
1628 const char *annex, gdb_byte *readbuf,
1629 const gdb_byte *writebuf, ULONGEST offset,
1632 if (!record_full_gdb_operation_disable
1633 && (object == TARGET_OBJECT_MEMORY
1634 || object == TARGET_OBJECT_RAW_MEMORY) && writebuf)
1636 if (RECORD_FULL_IS_REPLAY)
1638 /* Let user choose if he wants to write memory or not. */
1639 if (!query (_("Because GDB is in replay mode, writing to memory "
1640 "will make the execution log unusable from this "
1641 "point onward. Write memory at address %s?"),
1642 paddress (target_gdbarch (), offset)))
1643 error (_("Process record canceled the operation."));
1645 /* Destroy the record from here forward. */
1646 record_full_list_release_following (record_full_list);
1649 /* Check record_full_insn_num */
1650 record_full_check_insn_num (0);
1652 /* Record registers change to list as an instruction. */
1653 record_full_arch_list_head = NULL;
1654 record_full_arch_list_tail = NULL;
1655 if (record_full_arch_list_add_mem (offset, len))
1657 record_full_list_release (record_full_arch_list_tail);
1659 fprintf_unfiltered (gdb_stdlog,
1660 "Process record: failed to record "
1664 if (record_full_arch_list_add_end ())
1666 record_full_list_release (record_full_arch_list_tail);
1668 fprintf_unfiltered (gdb_stdlog,
1669 "Process record: failed to record "
1673 record_full_list->next = record_full_arch_list_head;
1674 record_full_arch_list_head->prev = record_full_list;
1675 record_full_list = record_full_arch_list_tail;
1677 if (record_full_insn_num == record_full_insn_max_num)
1678 record_full_list_release_first ();
1680 record_full_insn_num++;
1683 return record_full_beneath_to_xfer_partial
1684 (record_full_beneath_to_xfer_partial_ops, object, annex,
1685 readbuf, writebuf, offset, len);
1688 /* This structure represents a breakpoint inserted while the record
1689 target is active. We use this to know when to install/remove
1690 breakpoints in/from the target beneath. For example, a breakpoint
1691 may be inserted while recording, but removed when not replaying nor
1692 recording. In that case, the breakpoint had not been inserted on
1693 the target beneath, so we should not try to remove it there. */
1695 struct record_full_breakpoint
1697 /* The address and address space the breakpoint was set at. */
1698 struct address_space *address_space;
1701 /* True when the breakpoint has been also installed in the target
1702 beneath. This will be false for breakpoints set during replay or
1704 int in_target_beneath;
1707 typedef struct record_full_breakpoint *record_full_breakpoint_p;
1708 DEF_VEC_P(record_full_breakpoint_p);
1710 /* The list of breakpoints inserted while the record target is
1712 VEC(record_full_breakpoint_p) *record_full_breakpoints = NULL;
1715 record_full_sync_record_breakpoints (struct bp_location *loc, void *data)
1717 if (loc->loc_type != bp_loc_software_breakpoint)
1722 struct record_full_breakpoint *bp = XNEW (struct record_full_breakpoint);
1724 bp->addr = loc->target_info.placed_address;
1725 bp->address_space = loc->target_info.placed_address_space;
1727 bp->in_target_beneath = 1;
1729 VEC_safe_push (record_full_breakpoint_p, record_full_breakpoints, bp);
1733 /* Sync existing breakpoints to record_full_breakpoints. */
1736 record_full_init_record_breakpoints (void)
1738 VEC_free (record_full_breakpoint_p, record_full_breakpoints);
1740 iterate_over_bp_locations (record_full_sync_record_breakpoints);
1743 /* Behavior is conditional on RECORD_FULL_IS_REPLAY. We will not actually
1744 insert or remove breakpoints in the real target when replaying, nor
1748 record_full_insert_breakpoint (struct gdbarch *gdbarch,
1749 struct bp_target_info *bp_tgt)
1751 struct record_full_breakpoint *bp;
1752 int in_target_beneath = 0;
1754 if (!RECORD_FULL_IS_REPLAY)
1756 /* When recording, we currently always single-step, so we don't
1757 really need to install regular breakpoints in the inferior.
1758 However, we do have to insert software single-step
1759 breakpoints, in case the target can't hardware step. To keep
1760 things single, we always insert. */
1761 struct cleanup *old_cleanups;
1764 old_cleanups = record_full_gdb_operation_disable_set ();
1765 ret = record_full_beneath_to_insert_breakpoint (gdbarch, bp_tgt);
1766 do_cleanups (old_cleanups);
1771 in_target_beneath = 1;
1774 bp = XNEW (struct record_full_breakpoint);
1775 bp->addr = bp_tgt->placed_address;
1776 bp->address_space = bp_tgt->placed_address_space;
1777 bp->in_target_beneath = in_target_beneath;
1778 VEC_safe_push (record_full_breakpoint_p, record_full_breakpoints, bp);
1782 /* "to_remove_breakpoint" method for process record target. */
1785 record_full_remove_breakpoint (struct gdbarch *gdbarch,
1786 struct bp_target_info *bp_tgt)
1788 struct record_full_breakpoint *bp;
1792 VEC_iterate (record_full_breakpoint_p,
1793 record_full_breakpoints, ix, bp);
1796 if (bp->addr == bp_tgt->placed_address
1797 && bp->address_space == bp_tgt->placed_address_space)
1799 if (bp->in_target_beneath)
1801 struct cleanup *old_cleanups;
1804 old_cleanups = record_full_gdb_operation_disable_set ();
1805 ret = record_full_beneath_to_remove_breakpoint (gdbarch, bp_tgt);
1806 do_cleanups (old_cleanups);
1812 VEC_unordered_remove (record_full_breakpoint_p,
1813 record_full_breakpoints, ix);
1818 gdb_assert_not_reached ("removing unknown breakpoint");
1821 /* "to_can_execute_reverse" method for process record target. */
1824 record_full_can_execute_reverse (void)
1829 /* "to_get_bookmark" method for process record and prec over core. */
1832 record_full_get_bookmark (char *args, int from_tty)
1836 /* Return stringified form of instruction count. */
1837 if (record_full_list && record_full_list->type == record_full_end)
1838 ret = xstrdup (pulongest (record_full_list->u.end.insn_num));
1843 fprintf_unfiltered (gdb_stdlog,
1844 "record_full_get_bookmark returns %s\n", ret);
1846 fprintf_unfiltered (gdb_stdlog,
1847 "record_full_get_bookmark returns NULL\n");
1849 return (gdb_byte *) ret;
1852 /* "to_goto_bookmark" method for process record and prec over core. */
1855 record_full_goto_bookmark (gdb_byte *raw_bookmark, int from_tty)
1857 char *bookmark = (char *) raw_bookmark;
1860 fprintf_unfiltered (gdb_stdlog,
1861 "record_full_goto_bookmark receives %s\n", bookmark);
1863 if (bookmark[0] == '\'' || bookmark[0] == '\"')
1865 if (bookmark[strlen (bookmark) - 1] != bookmark[0])
1866 error (_("Unbalanced quotes: %s"), bookmark);
1868 /* Strip trailing quote. */
1869 bookmark[strlen (bookmark) - 1] = '\0';
1870 /* Strip leading quote. */
1872 /* Pass along to cmd_record_full_goto. */
1875 cmd_record_goto (bookmark, from_tty);
1880 record_full_async (void (*callback) (enum inferior_event_type event_type,
1881 void *context), void *context)
1883 /* If we're on top of a line target (e.g., linux-nat, remote), then
1884 set it to async mode as well. Will be NULL if we're sitting on
1885 top of the core target, for "record restore". */
1886 if (record_full_beneath_to_async != NULL)
1887 record_full_beneath_to_async (callback, context);
1891 record_full_can_async_p (void)
1893 /* We only enable async when the user specifically asks for it. */
1894 return target_async_permitted;
1898 record_full_is_async_p (void)
1900 /* We only enable async when the user specifically asks for it. */
1901 return target_async_permitted;
1904 static enum exec_direction_kind
1905 record_full_execution_direction (void)
1907 return record_full_execution_dir;
1911 record_full_info (void)
1913 struct record_full_entry *p;
1915 if (RECORD_FULL_IS_REPLAY)
1916 printf_filtered (_("Replay mode:\n"));
1918 printf_filtered (_("Record mode:\n"));
1920 /* Find entry for first actual instruction in the log. */
1921 for (p = record_full_first.next;
1922 p != NULL && p->type != record_full_end;
1926 /* Do we have a log at all? */
1927 if (p != NULL && p->type == record_full_end)
1929 /* Display instruction number for first instruction in the log. */
1930 printf_filtered (_("Lowest recorded instruction number is %s.\n"),
1931 pulongest (p->u.end.insn_num));
1933 /* If in replay mode, display where we are in the log. */
1934 if (RECORD_FULL_IS_REPLAY)
1935 printf_filtered (_("Current instruction number is %s.\n"),
1936 pulongest (record_full_list->u.end.insn_num));
1938 /* Display instruction number for last instruction in the log. */
1939 printf_filtered (_("Highest recorded instruction number is %s.\n"),
1940 pulongest (record_full_insn_count));
1942 /* Display log count. */
1943 printf_filtered (_("Log contains %u instructions.\n"),
1944 record_full_insn_num);
1947 printf_filtered (_("No instructions have been logged.\n"));
1949 /* Display max log size. */
1950 printf_filtered (_("Max logged instructions is %u.\n"),
1951 record_full_insn_max_num);
1954 /* The "to_record_delete" target method. */
1957 record_full_delete (void)
1959 record_full_list_release_following (record_full_list);
1962 /* The "to_record_is_replaying" target method. */
1965 record_full_is_replaying (void)
1967 return RECORD_FULL_IS_REPLAY;
1970 /* Go to a specific entry. */
1973 record_full_goto_entry (struct record_full_entry *p)
1976 error (_("Target insn not found."));
1977 else if (p == record_full_list)
1978 error (_("Already at target insn."));
1979 else if (p->u.end.insn_num > record_full_list->u.end.insn_num)
1981 printf_filtered (_("Go forward to insn number %s\n"),
1982 pulongest (p->u.end.insn_num));
1983 record_full_goto_insn (p, EXEC_FORWARD);
1987 printf_filtered (_("Go backward to insn number %s\n"),
1988 pulongest (p->u.end.insn_num));
1989 record_full_goto_insn (p, EXEC_REVERSE);
1992 registers_changed ();
1993 reinit_frame_cache ();
1994 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
1997 /* The "to_goto_record_begin" target method. */
2000 record_full_goto_begin (void)
2002 struct record_full_entry *p = NULL;
2004 for (p = &record_full_first; p != NULL; p = p->next)
2005 if (p->type == record_full_end)
2008 record_full_goto_entry (p);
2011 /* The "to_goto_record_end" target method. */
2014 record_full_goto_end (void)
2016 struct record_full_entry *p = NULL;
2018 for (p = record_full_list; p->next != NULL; p = p->next)
2020 for (; p!= NULL; p = p->prev)
2021 if (p->type == record_full_end)
2024 record_full_goto_entry (p);
2027 /* The "to_goto_record" target method. */
2030 record_full_goto (ULONGEST target_insn)
2032 struct record_full_entry *p = NULL;
2034 for (p = &record_full_first; p != NULL; p = p->next)
2035 if (p->type == record_full_end && p->u.end.insn_num == target_insn)
2038 record_full_goto_entry (p);
2042 init_record_full_ops (void)
2044 record_full_ops.to_shortname = "record-full";
2045 record_full_ops.to_longname = "Process record and replay target";
2046 record_full_ops.to_doc =
2047 "Log program while executing and replay execution from log.";
2048 record_full_ops.to_open = record_full_open;
2049 record_full_ops.to_close = record_full_close;
2050 record_full_ops.to_resume = record_full_resume;
2051 record_full_ops.to_wait = record_full_wait;
2052 record_full_ops.to_disconnect = record_disconnect;
2053 record_full_ops.to_detach = record_detach;
2054 record_full_ops.to_mourn_inferior = record_mourn_inferior;
2055 record_full_ops.to_kill = record_kill;
2056 record_full_ops.to_create_inferior = find_default_create_inferior;
2057 record_full_ops.to_store_registers = record_full_store_registers;
2058 record_full_ops.to_xfer_partial = record_full_xfer_partial;
2059 record_full_ops.to_insert_breakpoint = record_full_insert_breakpoint;
2060 record_full_ops.to_remove_breakpoint = record_full_remove_breakpoint;
2061 record_full_ops.to_stopped_by_watchpoint = record_full_stopped_by_watchpoint;
2062 record_full_ops.to_stopped_data_address = record_full_stopped_data_address;
2063 record_full_ops.to_can_execute_reverse = record_full_can_execute_reverse;
2064 record_full_ops.to_stratum = record_stratum;
2065 /* Add bookmark target methods. */
2066 record_full_ops.to_get_bookmark = record_full_get_bookmark;
2067 record_full_ops.to_goto_bookmark = record_full_goto_bookmark;
2068 record_full_ops.to_async = record_full_async;
2069 record_full_ops.to_can_async_p = record_full_can_async_p;
2070 record_full_ops.to_is_async_p = record_full_is_async_p;
2071 record_full_ops.to_execution_direction = record_full_execution_direction;
2072 record_full_ops.to_info_record = record_full_info;
2073 record_full_ops.to_save_record = record_full_save;
2074 record_full_ops.to_delete_record = record_full_delete;
2075 record_full_ops.to_record_is_replaying = record_full_is_replaying;
2076 record_full_ops.to_goto_record_begin = record_full_goto_begin;
2077 record_full_ops.to_goto_record_end = record_full_goto_end;
2078 record_full_ops.to_goto_record = record_full_goto;
2079 record_full_ops.to_magic = OPS_MAGIC;
2082 /* "to_resume" method for prec over corefile. */
2085 record_full_core_resume (struct target_ops *ops, ptid_t ptid, int step,
2086 enum gdb_signal signal)
2088 record_full_resume_step = step;
2089 record_full_resumed = 1;
2090 record_full_execution_dir = execution_direction;
2092 /* We are about to start executing the inferior (or simulate it),
2093 let's register it with the event loop. */
2094 if (target_can_async_p ())
2096 target_async (inferior_event_handler, 0);
2098 /* Notify the event loop there's an event to wait for. */
2099 mark_async_event_handler (record_full_async_inferior_event_token);
2103 /* "to_kill" method for prec over corefile. */
2106 record_full_core_kill (struct target_ops *ops)
2109 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_core_kill\n");
2111 unpush_target (&record_full_core_ops);
2114 /* "to_fetch_registers" method for prec over corefile. */
2117 record_full_core_fetch_registers (struct target_ops *ops,
2118 struct regcache *regcache,
2123 int num = gdbarch_num_regs (get_regcache_arch (regcache));
2126 for (i = 0; i < num; i ++)
2127 regcache_raw_supply (regcache, i,
2128 record_full_core_regbuf + MAX_REGISTER_SIZE * i);
2131 regcache_raw_supply (regcache, regno,
2132 record_full_core_regbuf + MAX_REGISTER_SIZE * regno);
2135 /* "to_prepare_to_store" method for prec over corefile. */
2138 record_full_core_prepare_to_store (struct regcache *regcache)
2142 /* "to_store_registers" method for prec over corefile. */
2145 record_full_core_store_registers (struct target_ops *ops,
2146 struct regcache *regcache,
2149 if (record_full_gdb_operation_disable)
2150 regcache_raw_collect (regcache, regno,
2151 record_full_core_regbuf + MAX_REGISTER_SIZE * regno);
2153 error (_("You can't do that without a process to debug."));
2156 /* "to_xfer_partial" method for prec over corefile. */
2159 record_full_core_xfer_partial (struct target_ops *ops,
2160 enum target_object object,
2161 const char *annex, gdb_byte *readbuf,
2162 const gdb_byte *writebuf, ULONGEST offset,
2165 if (object == TARGET_OBJECT_MEMORY)
2167 if (record_full_gdb_operation_disable || !writebuf)
2169 struct target_section *p;
2171 for (p = record_full_core_start; p < record_full_core_end; p++)
2173 if (offset >= p->addr)
2175 struct record_full_core_buf_entry *entry;
2176 ULONGEST sec_offset;
2178 if (offset >= p->endaddr)
2181 if (offset + len > p->endaddr)
2182 len = p->endaddr - offset;
2184 sec_offset = offset - p->addr;
2186 /* Read readbuf or write writebuf p, offset, len. */
2188 if (p->the_bfd_section->flags & SEC_CONSTRUCTOR
2189 || (p->the_bfd_section->flags & SEC_HAS_CONTENTS) == 0)
2192 memset (readbuf, 0, len);
2195 /* Get record_full_core_buf_entry. */
2196 for (entry = record_full_core_buf_list; entry;
2197 entry = entry->prev)
2204 /* Add a new entry. */
2205 entry = (struct record_full_core_buf_entry *)
2207 (sizeof (struct record_full_core_buf_entry));
2209 if (!bfd_malloc_and_get_section
2210 (p->the_bfd_section->owner,
2217 entry->prev = record_full_core_buf_list;
2218 record_full_core_buf_list = entry;
2221 memcpy (entry->buf + sec_offset, writebuf,
2227 return record_full_beneath_to_xfer_partial
2228 (record_full_beneath_to_xfer_partial_ops,
2229 object, annex, readbuf, writebuf,
2232 memcpy (readbuf, entry->buf + sec_offset,
2243 error (_("You can't do that without a process to debug."));
2246 return record_full_beneath_to_xfer_partial
2247 (record_full_beneath_to_xfer_partial_ops, object, annex,
2248 readbuf, writebuf, offset, len);
2251 /* "to_insert_breakpoint" method for prec over corefile. */
2254 record_full_core_insert_breakpoint (struct gdbarch *gdbarch,
2255 struct bp_target_info *bp_tgt)
2260 /* "to_remove_breakpoint" method for prec over corefile. */
2263 record_full_core_remove_breakpoint (struct gdbarch *gdbarch,
2264 struct bp_target_info *bp_tgt)
2269 /* "to_has_execution" method for prec over corefile. */
2272 record_full_core_has_execution (struct target_ops *ops, ptid_t the_ptid)
2278 init_record_full_core_ops (void)
2280 record_full_core_ops.to_shortname = "record-core";
2281 record_full_core_ops.to_longname = "Process record and replay target";
2282 record_full_core_ops.to_doc =
2283 "Log program while executing and replay execution from log.";
2284 record_full_core_ops.to_open = record_full_open;
2285 record_full_core_ops.to_close = record_full_close;
2286 record_full_core_ops.to_resume = record_full_core_resume;
2287 record_full_core_ops.to_wait = record_full_wait;
2288 record_full_core_ops.to_kill = record_full_core_kill;
2289 record_full_core_ops.to_fetch_registers = record_full_core_fetch_registers;
2290 record_full_core_ops.to_prepare_to_store = record_full_core_prepare_to_store;
2291 record_full_core_ops.to_store_registers = record_full_core_store_registers;
2292 record_full_core_ops.to_xfer_partial = record_full_core_xfer_partial;
2293 record_full_core_ops.to_insert_breakpoint
2294 = record_full_core_insert_breakpoint;
2295 record_full_core_ops.to_remove_breakpoint
2296 = record_full_core_remove_breakpoint;
2297 record_full_core_ops.to_stopped_by_watchpoint
2298 = record_full_stopped_by_watchpoint;
2299 record_full_core_ops.to_stopped_data_address
2300 = record_full_stopped_data_address;
2301 record_full_core_ops.to_can_execute_reverse
2302 = record_full_can_execute_reverse;
2303 record_full_core_ops.to_has_execution = record_full_core_has_execution;
2304 record_full_core_ops.to_stratum = record_stratum;
2305 /* Add bookmark target methods. */
2306 record_full_core_ops.to_get_bookmark = record_full_get_bookmark;
2307 record_full_core_ops.to_goto_bookmark = record_full_goto_bookmark;
2308 record_full_core_ops.to_async = record_full_async;
2309 record_full_core_ops.to_can_async_p = record_full_can_async_p;
2310 record_full_core_ops.to_is_async_p = record_full_is_async_p;
2311 record_full_core_ops.to_execution_direction
2312 = record_full_execution_direction;
2313 record_full_core_ops.to_info_record = record_full_info;
2314 record_full_core_ops.to_delete_record = record_full_delete;
2315 record_full_core_ops.to_record_is_replaying = record_full_is_replaying;
2316 record_full_core_ops.to_goto_record_begin = record_full_goto_begin;
2317 record_full_core_ops.to_goto_record_end = record_full_goto_end;
2318 record_full_core_ops.to_goto_record = record_full_goto;
2319 record_full_core_ops.to_magic = OPS_MAGIC;
2322 /* Record log save-file format
2323 Version 1 (never released)
2326 4 bytes: magic number htonl(0x20090829).
2327 NOTE: be sure to change whenever this file format changes!
2331 1 byte: record type (record_full_end, see enum record_full_type).
2333 1 byte: record type (record_full_reg, see enum record_full_type).
2334 8 bytes: register id (network byte order).
2335 MAX_REGISTER_SIZE bytes: register value.
2337 1 byte: record type (record_full_mem, see enum record_full_type).
2338 8 bytes: memory length (network byte order).
2339 8 bytes: memory address (network byte order).
2340 n bytes: memory value (n == memory length).
2343 4 bytes: magic number netorder32(0x20091016).
2344 NOTE: be sure to change whenever this file format changes!
2348 1 byte: record type (record_full_end, see enum record_full_type).
2350 4 bytes: instruction count
2352 1 byte: record type (record_full_reg, see enum record_full_type).
2353 4 bytes: register id (network byte order).
2354 n bytes: register value (n == actual register size).
2355 (eg. 4 bytes for x86 general registers).
2357 1 byte: record type (record_full_mem, see enum record_full_type).
2358 4 bytes: memory length (network byte order).
2359 8 bytes: memory address (network byte order).
2360 n bytes: memory value (n == memory length).
2364 /* bfdcore_read -- read bytes from a core file section. */
2367 bfdcore_read (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2369 int ret = bfd_get_section_contents (obfd, osec, buf, *offset, len);
2374 error (_("Failed to read %d bytes from core file %s ('%s')."),
2375 len, bfd_get_filename (obfd),
2376 bfd_errmsg (bfd_get_error ()));
2379 static inline uint64_t
2380 netorder64 (uint64_t input)
2384 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2385 BFD_ENDIAN_BIG, input);
2389 static inline uint32_t
2390 netorder32 (uint32_t input)
2394 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2395 BFD_ENDIAN_BIG, input);
2399 static inline uint16_t
2400 netorder16 (uint16_t input)
2404 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2405 BFD_ENDIAN_BIG, input);
2409 /* Restore the execution log from a core_bfd file. */
2411 record_full_restore (void)
2414 struct cleanup *old_cleanups;
2415 struct record_full_entry *rec;
2419 struct regcache *regcache;
2421 /* We restore the execution log from the open core bfd,
2423 if (core_bfd == NULL)
2426 /* "record_full_restore" can only be called when record list is empty. */
2427 gdb_assert (record_full_first.next == NULL);
2430 fprintf_unfiltered (gdb_stdlog, "Restoring recording from core file.\n");
2432 /* Now need to find our special note section. */
2433 osec = bfd_get_section_by_name (core_bfd, "null0");
2435 fprintf_unfiltered (gdb_stdlog, "Find precord section %s.\n",
2436 osec ? "succeeded" : "failed");
2439 osec_size = bfd_section_size (core_bfd, osec);
2441 fprintf_unfiltered (gdb_stdlog, "%s", bfd_section_name (core_bfd, osec));
2443 /* Check the magic code. */
2444 bfdcore_read (core_bfd, osec, &magic, sizeof (magic), &bfd_offset);
2445 if (magic != RECORD_FULL_FILE_MAGIC)
2446 error (_("Version mis-match or file format error in core file %s."),
2447 bfd_get_filename (core_bfd));
2449 fprintf_unfiltered (gdb_stdlog,
2450 " Reading 4-byte magic cookie "
2451 "RECORD_FULL_FILE_MAGIC (0x%s)\n",
2452 phex_nz (netorder32 (magic), 4));
2454 /* Restore the entries in recfd into record_full_arch_list_head and
2455 record_full_arch_list_tail. */
2456 record_full_arch_list_head = NULL;
2457 record_full_arch_list_tail = NULL;
2458 record_full_insn_num = 0;
2459 old_cleanups = make_cleanup (record_full_arch_list_cleanups, 0);
2460 regcache = get_current_regcache ();
2465 uint32_t regnum, len, signal, count;
2468 /* We are finished when offset reaches osec_size. */
2469 if (bfd_offset >= osec_size)
2471 bfdcore_read (core_bfd, osec, &rectype, sizeof (rectype), &bfd_offset);
2475 case record_full_reg: /* reg */
2476 /* Get register number to regnum. */
2477 bfdcore_read (core_bfd, osec, ®num,
2478 sizeof (regnum), &bfd_offset);
2479 regnum = netorder32 (regnum);
2481 rec = record_full_reg_alloc (regcache, regnum);
2484 bfdcore_read (core_bfd, osec, record_full_get_loc (rec),
2485 rec->u.reg.len, &bfd_offset);
2488 fprintf_unfiltered (gdb_stdlog,
2489 " Reading register %d (1 "
2490 "plus %lu plus %d bytes)\n",
2492 (unsigned long) sizeof (regnum),
2496 case record_full_mem: /* mem */
2498 bfdcore_read (core_bfd, osec, &len,
2499 sizeof (len), &bfd_offset);
2500 len = netorder32 (len);
2503 bfdcore_read (core_bfd, osec, &addr,
2504 sizeof (addr), &bfd_offset);
2505 addr = netorder64 (addr);
2507 rec = record_full_mem_alloc (addr, len);
2510 bfdcore_read (core_bfd, osec, record_full_get_loc (rec),
2511 rec->u.mem.len, &bfd_offset);
2514 fprintf_unfiltered (gdb_stdlog,
2515 " Reading memory %s (1 plus "
2516 "%lu plus %lu plus %d bytes)\n",
2517 paddress (get_current_arch (),
2519 (unsigned long) sizeof (addr),
2520 (unsigned long) sizeof (len),
2524 case record_full_end: /* end */
2525 rec = record_full_end_alloc ();
2526 record_full_insn_num ++;
2528 /* Get signal value. */
2529 bfdcore_read (core_bfd, osec, &signal,
2530 sizeof (signal), &bfd_offset);
2531 signal = netorder32 (signal);
2532 rec->u.end.sigval = signal;
2534 /* Get insn count. */
2535 bfdcore_read (core_bfd, osec, &count,
2536 sizeof (count), &bfd_offset);
2537 count = netorder32 (count);
2538 rec->u.end.insn_num = count;
2539 record_full_insn_count = count + 1;
2541 fprintf_unfiltered (gdb_stdlog,
2542 " Reading record_full_end (1 + "
2543 "%lu + %lu bytes), offset == %s\n",
2544 (unsigned long) sizeof (signal),
2545 (unsigned long) sizeof (count),
2546 paddress (get_current_arch (),
2551 error (_("Bad entry type in core file %s."),
2552 bfd_get_filename (core_bfd));
2556 /* Add rec to record arch list. */
2557 record_full_arch_list_add (rec);
2560 discard_cleanups (old_cleanups);
2562 /* Add record_full_arch_list_head to the end of record list. */
2563 record_full_first.next = record_full_arch_list_head;
2564 record_full_arch_list_head->prev = &record_full_first;
2565 record_full_arch_list_tail->next = NULL;
2566 record_full_list = &record_full_first;
2568 /* Update record_full_insn_max_num. */
2569 if (record_full_insn_num > record_full_insn_max_num)
2571 record_full_insn_max_num = record_full_insn_num;
2572 warning (_("Auto increase record/replay buffer limit to %u."),
2573 record_full_insn_max_num);
2577 printf_filtered (_("Restored records from core file %s.\n"),
2578 bfd_get_filename (core_bfd));
2580 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
2583 /* bfdcore_write -- write bytes into a core file section. */
2586 bfdcore_write (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2588 int ret = bfd_set_section_contents (obfd, osec, buf, *offset, len);
2593 error (_("Failed to write %d bytes to core file %s ('%s')."),
2594 len, bfd_get_filename (obfd),
2595 bfd_errmsg (bfd_get_error ()));
2598 /* Restore the execution log from a file. We use a modified elf
2599 corefile format, with an extra section for our data. */
2602 cmd_record_full_restore (char *args, int from_tty)
2604 core_file_command (args, from_tty);
2605 record_full_open (args, from_tty);
2609 record_full_save_cleanups (void *data)
2612 char *pathname = xstrdup (bfd_get_filename (obfd));
2614 gdb_bfd_unref (obfd);
2619 /* Save the execution log to a file. We use a modified elf corefile
2620 format, with an extra section for our data. */
2623 record_full_save (const char *recfilename)
2625 struct record_full_entry *cur_record_full_list;
2627 struct regcache *regcache;
2628 struct gdbarch *gdbarch;
2629 struct cleanup *old_cleanups;
2630 struct cleanup *set_cleanups;
2633 asection *osec = NULL;
2636 /* Open the save file. */
2638 fprintf_unfiltered (gdb_stdlog, "Saving execution log to core file '%s'\n",
2641 /* Open the output file. */
2642 obfd = create_gcore_bfd (recfilename);
2643 old_cleanups = make_cleanup (record_full_save_cleanups, obfd);
2645 /* Save the current record entry to "cur_record_full_list". */
2646 cur_record_full_list = record_full_list;
2648 /* Get the values of regcache and gdbarch. */
2649 regcache = get_current_regcache ();
2650 gdbarch = get_regcache_arch (regcache);
2652 /* Disable the GDB operation record. */
2653 set_cleanups = record_full_gdb_operation_disable_set ();
2655 /* Reverse execute to the begin of record list. */
2658 /* Check for beginning and end of log. */
2659 if (record_full_list == &record_full_first)
2662 record_full_exec_insn (regcache, gdbarch, record_full_list);
2664 if (record_full_list->prev)
2665 record_full_list = record_full_list->prev;
2668 /* Compute the size needed for the extra bfd section. */
2669 save_size = 4; /* magic cookie */
2670 for (record_full_list = record_full_first.next; record_full_list;
2671 record_full_list = record_full_list->next)
2672 switch (record_full_list->type)
2674 case record_full_end:
2675 save_size += 1 + 4 + 4;
2677 case record_full_reg:
2678 save_size += 1 + 4 + record_full_list->u.reg.len;
2680 case record_full_mem:
2681 save_size += 1 + 4 + 8 + record_full_list->u.mem.len;
2685 /* Make the new bfd section. */
2686 osec = bfd_make_section_anyway_with_flags (obfd, "precord",
2690 error (_("Failed to create 'precord' section for corefile %s: %s"),
2692 bfd_errmsg (bfd_get_error ()));
2693 bfd_set_section_size (obfd, osec, save_size);
2694 bfd_set_section_vma (obfd, osec, 0);
2695 bfd_set_section_alignment (obfd, osec, 0);
2696 bfd_section_lma (obfd, osec) = 0;
2698 /* Save corefile state. */
2699 write_gcore_file (obfd);
2701 /* Write out the record log. */
2702 /* Write the magic code. */
2703 magic = RECORD_FULL_FILE_MAGIC;
2705 fprintf_unfiltered (gdb_stdlog,
2706 " Writing 4-byte magic cookie "
2707 "RECORD_FULL_FILE_MAGIC (0x%s)\n",
2708 phex_nz (magic, 4));
2709 bfdcore_write (obfd, osec, &magic, sizeof (magic), &bfd_offset);
2711 /* Save the entries to recfd and forward execute to the end of
2713 record_full_list = &record_full_first;
2717 if (record_full_list != &record_full_first)
2720 uint32_t regnum, len, signal, count;
2723 type = record_full_list->type;
2724 bfdcore_write (obfd, osec, &type, sizeof (type), &bfd_offset);
2726 switch (record_full_list->type)
2728 case record_full_reg: /* reg */
2730 fprintf_unfiltered (gdb_stdlog,
2731 " Writing register %d (1 "
2732 "plus %lu plus %d bytes)\n",
2733 record_full_list->u.reg.num,
2734 (unsigned long) sizeof (regnum),
2735 record_full_list->u.reg.len);
2738 regnum = netorder32 (record_full_list->u.reg.num);
2739 bfdcore_write (obfd, osec, ®num,
2740 sizeof (regnum), &bfd_offset);
2743 bfdcore_write (obfd, osec,
2744 record_full_get_loc (record_full_list),
2745 record_full_list->u.reg.len, &bfd_offset);
2748 case record_full_mem: /* mem */
2750 fprintf_unfiltered (gdb_stdlog,
2751 " Writing memory %s (1 plus "
2752 "%lu plus %lu plus %d bytes)\n",
2754 record_full_list->u.mem.addr),
2755 (unsigned long) sizeof (addr),
2756 (unsigned long) sizeof (len),
2757 record_full_list->u.mem.len);
2760 len = netorder32 (record_full_list->u.mem.len);
2761 bfdcore_write (obfd, osec, &len, sizeof (len), &bfd_offset);
2763 /* Write memaddr. */
2764 addr = netorder64 (record_full_list->u.mem.addr);
2765 bfdcore_write (obfd, osec, &addr,
2766 sizeof (addr), &bfd_offset);
2769 bfdcore_write (obfd, osec,
2770 record_full_get_loc (record_full_list),
2771 record_full_list->u.mem.len, &bfd_offset);
2774 case record_full_end:
2776 fprintf_unfiltered (gdb_stdlog,
2777 " Writing record_full_end (1 + "
2778 "%lu + %lu bytes)\n",
2779 (unsigned long) sizeof (signal),
2780 (unsigned long) sizeof (count));
2781 /* Write signal value. */
2782 signal = netorder32 (record_full_list->u.end.sigval);
2783 bfdcore_write (obfd, osec, &signal,
2784 sizeof (signal), &bfd_offset);
2786 /* Write insn count. */
2787 count = netorder32 (record_full_list->u.end.insn_num);
2788 bfdcore_write (obfd, osec, &count,
2789 sizeof (count), &bfd_offset);
2794 /* Execute entry. */
2795 record_full_exec_insn (regcache, gdbarch, record_full_list);
2797 if (record_full_list->next)
2798 record_full_list = record_full_list->next;
2803 /* Reverse execute to cur_record_full_list. */
2806 /* Check for beginning and end of log. */
2807 if (record_full_list == cur_record_full_list)
2810 record_full_exec_insn (regcache, gdbarch, record_full_list);
2812 if (record_full_list->prev)
2813 record_full_list = record_full_list->prev;
2816 do_cleanups (set_cleanups);
2817 gdb_bfd_unref (obfd);
2818 discard_cleanups (old_cleanups);
2821 printf_filtered (_("Saved core file %s with execution log.\n"),
2825 /* record_full_goto_insn -- rewind the record log (forward or backward,
2826 depending on DIR) to the given entry, changing the program state
2830 record_full_goto_insn (struct record_full_entry *entry,
2831 enum exec_direction_kind dir)
2833 struct cleanup *set_cleanups = record_full_gdb_operation_disable_set ();
2834 struct regcache *regcache = get_current_regcache ();
2835 struct gdbarch *gdbarch = get_regcache_arch (regcache);
2837 /* Assume everything is valid: we will hit the entry,
2838 and we will not hit the end of the recording. */
2840 if (dir == EXEC_FORWARD)
2841 record_full_list = record_full_list->next;
2845 record_full_exec_insn (regcache, gdbarch, record_full_list);
2846 if (dir == EXEC_REVERSE)
2847 record_full_list = record_full_list->prev;
2849 record_full_list = record_full_list->next;
2850 } while (record_full_list != entry);
2851 do_cleanups (set_cleanups);
2854 /* Alias for "target record-full". */
2857 cmd_record_full_start (char *args, int from_tty)
2859 execute_command ("target record-full", from_tty);
2863 set_record_full_insn_max_num (char *args, int from_tty,
2864 struct cmd_list_element *c)
2866 if (record_full_insn_num > record_full_insn_max_num)
2868 /* Count down record_full_insn_num while releasing records from list. */
2869 while (record_full_insn_num > record_full_insn_max_num)
2871 record_full_list_release_first ();
2872 record_full_insn_num--;
2877 /* The "set record full" command. */
2880 set_record_full_command (char *args, int from_tty)
2882 printf_unfiltered (_("\"set record full\" must be followed "
2883 "by an apporpriate subcommand.\n"));
2884 help_list (set_record_full_cmdlist, "set record full ", all_commands,
2888 /* The "show record full" command. */
2891 show_record_full_command (char *args, int from_tty)
2893 cmd_show_list (show_record_full_cmdlist, from_tty, "");
2896 /* Provide a prototype to silence -Wmissing-prototypes. */
2897 extern initialize_file_ftype _initialize_record_full;
2900 _initialize_record_full (void)
2902 struct cmd_list_element *c;
2904 /* Init record_full_first. */
2905 record_full_first.prev = NULL;
2906 record_full_first.next = NULL;
2907 record_full_first.type = record_full_end;
2909 init_record_full_ops ();
2910 add_target (&record_full_ops);
2911 add_deprecated_target_alias (&record_full_ops, "record");
2912 init_record_full_core_ops ();
2913 add_target (&record_full_core_ops);
2915 add_prefix_cmd ("full", class_obscure, cmd_record_full_start,
2916 _("Start full execution recording."), &record_full_cmdlist,
2917 "record full ", 0, &record_cmdlist);
2919 c = add_cmd ("restore", class_obscure, cmd_record_full_restore,
2920 _("Restore the execution log from a file.\n\
2921 Argument is filename. File must be created with 'record save'."),
2922 &record_full_cmdlist);
2923 set_cmd_completer (c, filename_completer);
2925 /* Deprecate the old version without "full" prefix. */
2926 c = add_alias_cmd ("restore", "full restore", class_obscure, 1,
2928 set_cmd_completer (c, filename_completer);
2929 deprecate_cmd (c, "record full restore");
2931 add_prefix_cmd ("full", class_support, set_record_full_command,
2932 _("Set record options"), &set_record_full_cmdlist,
2933 "set record full ", 0, &set_record_cmdlist);
2935 add_prefix_cmd ("full", class_support, show_record_full_command,
2936 _("Show record options"), &show_record_full_cmdlist,
2937 "show record full ", 0, &show_record_cmdlist);
2939 /* Record instructions number limit command. */
2940 add_setshow_boolean_cmd ("stop-at-limit", no_class,
2941 &record_full_stop_at_limit, _("\
2942 Set whether record/replay stops when record/replay buffer becomes full."), _("\
2943 Show whether record/replay stops when record/replay buffer becomes full."),
2944 _("Default is ON.\n\
2945 When ON, if the record/replay buffer becomes full, ask user what to do.\n\
2946 When OFF, if the record/replay buffer becomes full,\n\
2947 delete the oldest recorded instruction to make room for each new one."),
2949 &set_record_full_cmdlist, &show_record_full_cmdlist);
2951 c = add_alias_cmd ("stop-at-limit", "full stop-at-limit", no_class, 1,
2952 &set_record_cmdlist);
2953 deprecate_cmd (c, "set record full stop-at-limit");
2955 c = add_alias_cmd ("stop-at-limit", "full stop-at-limit", no_class, 1,
2956 &show_record_cmdlist);
2957 deprecate_cmd (c, "show record full stop-at-limit");
2959 add_setshow_uinteger_cmd ("insn-number-max", no_class,
2960 &record_full_insn_max_num,
2961 _("Set record/replay buffer limit."),
2962 _("Show record/replay buffer limit."), _("\
2963 Set the maximum number of instructions to be stored in the\n\
2964 record/replay buffer. A value of either \"unlimited\" or zero means no\n\
2965 limit. Default is 200000."),
2966 set_record_full_insn_max_num,
2967 NULL, &set_record_full_cmdlist,
2968 &show_record_full_cmdlist);
2970 c = add_alias_cmd ("insn-number-max", "full insn-number-max", no_class, 1,
2971 &set_record_cmdlist);
2972 deprecate_cmd (c, "set record full insn-number-max");
2974 c = add_alias_cmd ("insn-number-max", "full insn-number-max", no_class, 1,
2975 &show_record_cmdlist);
2976 deprecate_cmd (c, "show record full insn-number-max");
2978 add_setshow_boolean_cmd ("memory-query", no_class,
2979 &record_full_memory_query, _("\
2980 Set whether query if PREC cannot record memory change of next instruction."),
2982 Show whether query if PREC cannot record memory change of next instruction."),
2985 When ON, query if PREC cannot record memory change of next instruction."),
2987 &set_record_full_cmdlist,
2988 &show_record_full_cmdlist);
2990 c = add_alias_cmd ("memory-query", "full memory-query", no_class, 1,
2991 &set_record_cmdlist);
2992 deprecate_cmd (c, "set record full memory-query");
2994 c = add_alias_cmd ("memory-query", "full memory-query", no_class, 1,
2995 &show_record_cmdlist);
2996 deprecate_cmd (c, "show record full memory-query");