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 /* See record-full.h. */
214 record_full_is_used (void)
216 struct target_ops *t;
218 t = find_record_target ();
219 return (t == &record_full_ops
220 || t == &record_full_core_ops);
224 /* Command lists for "set/show record full". */
225 static struct cmd_list_element *set_record_full_cmdlist;
226 static struct cmd_list_element *show_record_full_cmdlist;
228 /* Command list for "record full". */
229 static struct cmd_list_element *record_full_cmdlist;
231 /* The beneath function pointers. */
232 static struct target_ops *record_full_beneath_to_resume_ops;
233 static void (*record_full_beneath_to_resume) (struct target_ops *, ptid_t, int,
235 static struct target_ops *record_full_beneath_to_wait_ops;
236 static ptid_t (*record_full_beneath_to_wait) (struct target_ops *, ptid_t,
237 struct target_waitstatus *,
239 static struct target_ops *record_full_beneath_to_store_registers_ops;
240 static void (*record_full_beneath_to_store_registers) (struct target_ops *,
243 static struct target_ops *record_full_beneath_to_xfer_partial_ops;
244 static target_xfer_partial_ftype *record_full_beneath_to_xfer_partial;
246 (*record_full_beneath_to_insert_breakpoint) (struct gdbarch *,
247 struct bp_target_info *);
249 (*record_full_beneath_to_remove_breakpoint) (struct gdbarch *,
250 struct bp_target_info *);
251 static int (*record_full_beneath_to_stopped_by_watchpoint) (void);
252 static int (*record_full_beneath_to_stopped_data_address) (struct target_ops *,
255 (*record_full_beneath_to_async) (void (*) (enum inferior_event_type, void *),
258 static void record_full_goto_insn (struct record_full_entry *entry,
259 enum exec_direction_kind dir);
260 static void record_full_save (const char *recfilename);
262 /* Alloc and free functions for record_full_reg, record_full_mem, and
263 record_full_end entries. */
265 /* Alloc a record_full_reg record entry. */
267 static inline struct record_full_entry *
268 record_full_reg_alloc (struct regcache *regcache, int regnum)
270 struct record_full_entry *rec;
271 struct gdbarch *gdbarch = get_regcache_arch (regcache);
273 rec = xcalloc (1, sizeof (struct record_full_entry));
274 rec->type = record_full_reg;
275 rec->u.reg.num = regnum;
276 rec->u.reg.len = register_size (gdbarch, regnum);
277 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
278 rec->u.reg.u.ptr = (gdb_byte *) xmalloc (rec->u.reg.len);
283 /* Free a record_full_reg record entry. */
286 record_full_reg_release (struct record_full_entry *rec)
288 gdb_assert (rec->type == record_full_reg);
289 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
290 xfree (rec->u.reg.u.ptr);
294 /* Alloc a record_full_mem record entry. */
296 static inline struct record_full_entry *
297 record_full_mem_alloc (CORE_ADDR addr, int len)
299 struct record_full_entry *rec;
301 rec = xcalloc (1, sizeof (struct record_full_entry));
302 rec->type = record_full_mem;
303 rec->u.mem.addr = addr;
304 rec->u.mem.len = len;
305 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
306 rec->u.mem.u.ptr = (gdb_byte *) xmalloc (len);
311 /* Free a record_full_mem record entry. */
314 record_full_mem_release (struct record_full_entry *rec)
316 gdb_assert (rec->type == record_full_mem);
317 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
318 xfree (rec->u.mem.u.ptr);
322 /* Alloc a record_full_end record entry. */
324 static inline struct record_full_entry *
325 record_full_end_alloc (void)
327 struct record_full_entry *rec;
329 rec = xcalloc (1, sizeof (struct record_full_entry));
330 rec->type = record_full_end;
335 /* Free a record_full_end record entry. */
338 record_full_end_release (struct record_full_entry *rec)
343 /* Free one record entry, any type.
344 Return entry->type, in case caller wants to know. */
346 static inline enum record_full_type
347 record_full_entry_release (struct record_full_entry *rec)
349 enum record_full_type type = rec->type;
352 case record_full_reg:
353 record_full_reg_release (rec);
355 case record_full_mem:
356 record_full_mem_release (rec);
358 case record_full_end:
359 record_full_end_release (rec);
365 /* Free all record entries in list pointed to by REC. */
368 record_full_list_release (struct record_full_entry *rec)
379 record_full_entry_release (rec->next);
382 if (rec == &record_full_first)
384 record_full_insn_num = 0;
385 record_full_first.next = NULL;
388 record_full_entry_release (rec);
391 /* Free all record entries forward of the given list position. */
394 record_full_list_release_following (struct record_full_entry *rec)
396 struct record_full_entry *tmp = rec->next;
402 if (record_full_entry_release (tmp) == record_full_end)
404 record_full_insn_num--;
405 record_full_insn_count--;
411 /* Delete the first instruction from the beginning of the log, to make
412 room for adding a new instruction at the end of the log.
414 Note -- this function does not modify record_full_insn_num. */
417 record_full_list_release_first (void)
419 struct record_full_entry *tmp;
421 if (!record_full_first.next)
424 /* Loop until a record_full_end. */
427 /* Cut record_full_first.next out of the linked list. */
428 tmp = record_full_first.next;
429 record_full_first.next = tmp->next;
430 tmp->next->prev = &record_full_first;
432 /* tmp is now isolated, and can be deleted. */
433 if (record_full_entry_release (tmp) == record_full_end)
434 break; /* End loop at first record_full_end. */
436 if (!record_full_first.next)
438 gdb_assert (record_full_insn_num == 1);
439 break; /* End loop when list is empty. */
444 /* Add a struct record_full_entry to record_full_arch_list. */
447 record_full_arch_list_add (struct record_full_entry *rec)
449 if (record_debug > 1)
450 fprintf_unfiltered (gdb_stdlog,
451 "Process record: record_full_arch_list_add %s.\n",
452 host_address_to_string (rec));
454 if (record_full_arch_list_tail)
456 record_full_arch_list_tail->next = rec;
457 rec->prev = record_full_arch_list_tail;
458 record_full_arch_list_tail = rec;
462 record_full_arch_list_head = rec;
463 record_full_arch_list_tail = rec;
467 /* Return the value storage location of a record entry. */
468 static inline gdb_byte *
469 record_full_get_loc (struct record_full_entry *rec)
472 case record_full_mem:
473 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
474 return rec->u.mem.u.ptr;
476 return rec->u.mem.u.buf;
477 case record_full_reg:
478 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
479 return rec->u.reg.u.ptr;
481 return rec->u.reg.u.buf;
482 case record_full_end:
484 gdb_assert_not_reached ("unexpected record_full_entry type");
489 /* Record the value of a register NUM to record_full_arch_list. */
492 record_full_arch_list_add_reg (struct regcache *regcache, int regnum)
494 struct record_full_entry *rec;
496 if (record_debug > 1)
497 fprintf_unfiltered (gdb_stdlog,
498 "Process record: add register num = %d to "
502 rec = record_full_reg_alloc (regcache, regnum);
504 regcache_raw_read (regcache, regnum, record_full_get_loc (rec));
506 record_full_arch_list_add (rec);
511 /* Record the value of a region of memory whose address is ADDR and
512 length is LEN to record_full_arch_list. */
515 record_full_arch_list_add_mem (CORE_ADDR addr, int len)
517 struct record_full_entry *rec;
519 if (record_debug > 1)
520 fprintf_unfiltered (gdb_stdlog,
521 "Process record: add mem addr = %s len = %d to "
523 paddress (target_gdbarch (), addr), len);
525 if (!addr) /* FIXME: Why? Some arch must permit it... */
528 rec = record_full_mem_alloc (addr, len);
530 if (record_read_memory (target_gdbarch (), addr,
531 record_full_get_loc (rec), len))
533 record_full_mem_release (rec);
537 record_full_arch_list_add (rec);
542 /* Add a record_full_end type struct record_full_entry to
543 record_full_arch_list. */
546 record_full_arch_list_add_end (void)
548 struct record_full_entry *rec;
550 if (record_debug > 1)
551 fprintf_unfiltered (gdb_stdlog,
552 "Process record: add end to arch list.\n");
554 rec = record_full_end_alloc ();
555 rec->u.end.sigval = GDB_SIGNAL_0;
556 rec->u.end.insn_num = ++record_full_insn_count;
558 record_full_arch_list_add (rec);
564 record_full_check_insn_num (int set_terminal)
566 if (record_full_insn_num == record_full_insn_max_num)
568 /* Ask user what to do. */
569 if (record_full_stop_at_limit)
574 target_terminal_ours ();
575 q = yquery (_("Do you want to auto delete previous execution "
576 "log entries when record/replay buffer becomes "
577 "full (record full stop-at-limit)?"));
579 target_terminal_inferior ();
581 record_full_stop_at_limit = 0;
583 error (_("Process record: stopped by user."));
589 record_full_arch_list_cleanups (void *ignore)
591 record_full_list_release (record_full_arch_list_tail);
594 /* Before inferior step (when GDB record the running message, inferior
595 only can step), GDB will call this function to record the values to
596 record_full_list. This function will call gdbarch_process_record to
597 record the running message of inferior and set them to
598 record_full_arch_list, and add it to record_full_list. */
601 record_full_message (struct regcache *regcache, enum gdb_signal signal)
604 struct gdbarch *gdbarch = get_regcache_arch (regcache);
605 struct cleanup *old_cleanups
606 = make_cleanup (record_full_arch_list_cleanups, 0);
608 record_full_arch_list_head = NULL;
609 record_full_arch_list_tail = NULL;
611 /* Check record_full_insn_num. */
612 record_full_check_insn_num (1);
614 /* If gdb sends a signal value to target_resume,
615 save it in the 'end' field of the previous instruction.
617 Maybe process record should record what really happened,
618 rather than what gdb pretends has happened.
620 So if Linux delivered the signal to the child process during
621 the record mode, we will record it and deliver it again in
624 If user says "ignore this signal" during the record mode, then
625 it will be ignored again during the replay mode (no matter if
626 the user says something different, like "deliver this signal"
627 during the replay mode).
629 User should understand that nothing he does during the replay
630 mode will change the behavior of the child. If he tries,
631 then that is a user error.
633 But we should still deliver the signal to gdb during the replay,
634 if we delivered it during the recording. Therefore we should
635 record the signal during record_full_wait, not
636 record_full_resume. */
637 if (record_full_list != &record_full_first) /* FIXME better way to check */
639 gdb_assert (record_full_list->type == record_full_end);
640 record_full_list->u.end.sigval = signal;
643 if (signal == GDB_SIGNAL_0
644 || !gdbarch_process_record_signal_p (gdbarch))
645 ret = gdbarch_process_record (gdbarch,
647 regcache_read_pc (regcache));
649 ret = gdbarch_process_record_signal (gdbarch,
654 error (_("Process record: inferior program stopped."));
656 error (_("Process record: failed to record execution log."));
658 discard_cleanups (old_cleanups);
660 record_full_list->next = record_full_arch_list_head;
661 record_full_arch_list_head->prev = record_full_list;
662 record_full_list = record_full_arch_list_tail;
664 if (record_full_insn_num == record_full_insn_max_num)
665 record_full_list_release_first ();
667 record_full_insn_num++;
672 struct record_full_message_args {
673 struct regcache *regcache;
674 enum gdb_signal signal;
678 record_full_message_wrapper (void *args)
680 struct record_full_message_args *record_full_args = args;
682 return record_full_message (record_full_args->regcache,
683 record_full_args->signal);
687 record_full_message_wrapper_safe (struct regcache *regcache,
688 enum gdb_signal signal)
690 struct record_full_message_args args;
692 args.regcache = regcache;
693 args.signal = signal;
695 return catch_errors (record_full_message_wrapper, &args, NULL,
699 /* Set to 1 if record_full_store_registers and record_full_xfer_partial
700 doesn't need record. */
702 static int record_full_gdb_operation_disable = 0;
705 record_full_gdb_operation_disable_set (void)
707 struct cleanup *old_cleanups = NULL;
710 make_cleanup_restore_integer (&record_full_gdb_operation_disable);
711 record_full_gdb_operation_disable = 1;
716 /* Flag set to TRUE for target_stopped_by_watchpoint. */
717 static int record_full_hw_watchpoint = 0;
719 /* Execute one instruction from the record log. Each instruction in
720 the log will be represented by an arbitrary sequence of register
721 entries and memory entries, followed by an 'end' entry. */
724 record_full_exec_insn (struct regcache *regcache,
725 struct gdbarch *gdbarch,
726 struct record_full_entry *entry)
730 case record_full_reg: /* reg */
732 gdb_byte reg[MAX_REGISTER_SIZE];
734 if (record_debug > 1)
735 fprintf_unfiltered (gdb_stdlog,
736 "Process record: record_full_reg %s to "
737 "inferior num = %d.\n",
738 host_address_to_string (entry),
741 regcache_cooked_read (regcache, entry->u.reg.num, reg);
742 regcache_cooked_write (regcache, entry->u.reg.num,
743 record_full_get_loc (entry));
744 memcpy (record_full_get_loc (entry), reg, entry->u.reg.len);
748 case record_full_mem: /* mem */
750 /* Nothing to do if the entry is flagged not_accessible. */
751 if (!entry->u.mem.mem_entry_not_accessible)
753 gdb_byte *mem = alloca (entry->u.mem.len);
755 if (record_debug > 1)
756 fprintf_unfiltered (gdb_stdlog,
757 "Process record: record_full_mem %s to "
758 "inferior addr = %s len = %d.\n",
759 host_address_to_string (entry),
760 paddress (gdbarch, entry->u.mem.addr),
763 if (record_read_memory (gdbarch,
764 entry->u.mem.addr, mem, entry->u.mem.len))
765 entry->u.mem.mem_entry_not_accessible = 1;
768 if (target_write_memory (entry->u.mem.addr,
769 record_full_get_loc (entry),
772 entry->u.mem.mem_entry_not_accessible = 1;
774 warning (_("Process record: error writing memory at "
775 "addr = %s len = %d."),
776 paddress (gdbarch, entry->u.mem.addr),
781 memcpy (record_full_get_loc (entry), mem,
784 /* We've changed memory --- check if a hardware
785 watchpoint should trap. Note that this
786 presently assumes the target beneath supports
787 continuable watchpoints. On non-continuable
788 watchpoints target, we'll want to check this
789 _before_ actually doing the memory change, and
790 not doing the change at all if the watchpoint
792 if (hardware_watchpoint_inserted_in_range
793 (get_regcache_aspace (regcache),
794 entry->u.mem.addr, entry->u.mem.len))
795 record_full_hw_watchpoint = 1;
804 static struct target_ops *tmp_to_resume_ops;
805 static void (*tmp_to_resume) (struct target_ops *, ptid_t, int,
807 static struct target_ops *tmp_to_wait_ops;
808 static ptid_t (*tmp_to_wait) (struct target_ops *, ptid_t,
809 struct target_waitstatus *,
811 static struct target_ops *tmp_to_store_registers_ops;
812 static void (*tmp_to_store_registers) (struct target_ops *,
815 static struct target_ops *tmp_to_xfer_partial_ops;
816 static target_xfer_partial_ftype *tmp_to_xfer_partial;
817 static int (*tmp_to_insert_breakpoint) (struct gdbarch *,
818 struct bp_target_info *);
819 static int (*tmp_to_remove_breakpoint) (struct gdbarch *,
820 struct bp_target_info *);
821 static int (*tmp_to_stopped_by_watchpoint) (void);
822 static int (*tmp_to_stopped_data_address) (struct target_ops *, CORE_ADDR *);
823 static int (*tmp_to_stopped_data_address) (struct target_ops *, CORE_ADDR *);
824 static void (*tmp_to_async) (void (*) (enum inferior_event_type, void *), void *);
826 static void record_full_restore (void);
828 /* Asynchronous signal handle registered as event loop source for when
829 we have pending events ready to be passed to the core. */
831 static struct async_event_handler *record_full_async_inferior_event_token;
834 record_full_async_inferior_event_handler (gdb_client_data data)
836 inferior_event_handler (INF_REG_EVENT, NULL);
839 /* Open the process record target. */
842 record_full_core_open_1 (char *name, int from_tty)
844 struct regcache *regcache = get_current_regcache ();
845 int regnum = gdbarch_num_regs (get_regcache_arch (regcache));
848 /* Get record_full_core_regbuf. */
849 target_fetch_registers (regcache, -1);
850 record_full_core_regbuf = xmalloc (MAX_REGISTER_SIZE * regnum);
851 for (i = 0; i < regnum; i ++)
852 regcache_raw_collect (regcache, i,
853 record_full_core_regbuf + MAX_REGISTER_SIZE * i);
855 /* Get record_full_core_start and record_full_core_end. */
856 if (build_section_table (core_bfd, &record_full_core_start,
857 &record_full_core_end))
859 xfree (record_full_core_regbuf);
860 record_full_core_regbuf = NULL;
861 error (_("\"%s\": Can't find sections: %s"),
862 bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ()));
865 push_target (&record_full_core_ops);
866 record_full_restore ();
869 /* "to_open" target method for 'live' processes. */
872 record_full_open_1 (char *name, int from_tty)
875 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_open\n");
878 if (!target_has_execution)
879 error (_("Process record: the program is not being run."));
881 error (_("Process record target can't debug inferior in non-stop mode "
884 if (!gdbarch_process_record_p (target_gdbarch ()))
885 error (_("Process record: the current architecture doesn't support "
886 "record function."));
889 error (_("Could not find 'to_resume' method on the target stack."));
891 error (_("Could not find 'to_wait' method on the target stack."));
892 if (!tmp_to_store_registers)
893 error (_("Could not find 'to_store_registers' "
894 "method on the target stack."));
895 if (!tmp_to_insert_breakpoint)
896 error (_("Could not find 'to_insert_breakpoint' "
897 "method on the target stack."));
898 if (!tmp_to_remove_breakpoint)
899 error (_("Could not find 'to_remove_breakpoint' "
900 "method on the target stack."));
901 if (!tmp_to_stopped_by_watchpoint)
902 error (_("Could not find 'to_stopped_by_watchpoint' "
903 "method on the target stack."));
904 if (!tmp_to_stopped_data_address)
905 error (_("Could not find 'to_stopped_data_address' "
906 "method on the target stack."));
908 push_target (&record_full_ops);
911 static void record_full_init_record_breakpoints (void);
913 /* "to_open" target method. Open the process record target. */
916 record_full_open (char *name, int from_tty)
918 struct target_ops *t;
921 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_open\n");
925 /* Reset the tmp beneath pointers. */
926 tmp_to_resume_ops = NULL;
927 tmp_to_resume = NULL;
928 tmp_to_wait_ops = NULL;
930 tmp_to_store_registers_ops = NULL;
931 tmp_to_store_registers = NULL;
932 tmp_to_xfer_partial_ops = NULL;
933 tmp_to_xfer_partial = NULL;
934 tmp_to_insert_breakpoint = NULL;
935 tmp_to_remove_breakpoint = NULL;
936 tmp_to_stopped_by_watchpoint = NULL;
937 tmp_to_stopped_data_address = NULL;
940 /* Set the beneath function pointers. */
941 for (t = current_target.beneath; t != NULL; t = t->beneath)
945 tmp_to_resume = t->to_resume;
946 tmp_to_resume_ops = t;
950 tmp_to_wait = t->to_wait;
953 if (!tmp_to_store_registers)
955 tmp_to_store_registers = t->to_store_registers;
956 tmp_to_store_registers_ops = t;
958 if (!tmp_to_xfer_partial)
960 tmp_to_xfer_partial = t->to_xfer_partial;
961 tmp_to_xfer_partial_ops = t;
963 if (!tmp_to_insert_breakpoint)
964 tmp_to_insert_breakpoint = t->to_insert_breakpoint;
965 if (!tmp_to_remove_breakpoint)
966 tmp_to_remove_breakpoint = t->to_remove_breakpoint;
967 if (!tmp_to_stopped_by_watchpoint)
968 tmp_to_stopped_by_watchpoint = t->to_stopped_by_watchpoint;
969 if (!tmp_to_stopped_data_address)
970 tmp_to_stopped_data_address = t->to_stopped_data_address;
972 tmp_to_async = t->to_async;
974 if (!tmp_to_xfer_partial)
975 error (_("Could not find 'to_xfer_partial' method on the target stack."));
978 record_full_insn_num = 0;
979 record_full_insn_count = 0;
980 record_full_list = &record_full_first;
981 record_full_list->next = NULL;
983 /* Set the tmp beneath pointers to beneath pointers. */
984 record_full_beneath_to_resume_ops = tmp_to_resume_ops;
985 record_full_beneath_to_resume = tmp_to_resume;
986 record_full_beneath_to_wait_ops = tmp_to_wait_ops;
987 record_full_beneath_to_wait = tmp_to_wait;
988 record_full_beneath_to_store_registers_ops = tmp_to_store_registers_ops;
989 record_full_beneath_to_store_registers = tmp_to_store_registers;
990 record_full_beneath_to_xfer_partial_ops = tmp_to_xfer_partial_ops;
991 record_full_beneath_to_xfer_partial = tmp_to_xfer_partial;
992 record_full_beneath_to_insert_breakpoint = tmp_to_insert_breakpoint;
993 record_full_beneath_to_remove_breakpoint = tmp_to_remove_breakpoint;
994 record_full_beneath_to_stopped_by_watchpoint = tmp_to_stopped_by_watchpoint;
995 record_full_beneath_to_stopped_data_address = tmp_to_stopped_data_address;
996 record_full_beneath_to_async = tmp_to_async;
999 record_full_core_open_1 (name, from_tty);
1001 record_full_open_1 (name, from_tty);
1003 /* Register extra event sources in the event loop. */
1004 record_full_async_inferior_event_token
1005 = create_async_event_handler (record_full_async_inferior_event_handler,
1008 record_full_init_record_breakpoints ();
1010 observer_notify_record_changed (current_inferior (), 1);
1013 /* "to_close" target method. Close the process record target. */
1016 record_full_close (void)
1018 struct record_full_core_buf_entry *entry;
1021 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_close\n");
1023 record_full_list_release (record_full_list);
1025 /* Release record_full_core_regbuf. */
1026 if (record_full_core_regbuf)
1028 xfree (record_full_core_regbuf);
1029 record_full_core_regbuf = NULL;
1032 /* Release record_full_core_buf_list. */
1033 if (record_full_core_buf_list)
1035 for (entry = record_full_core_buf_list->prev; entry;
1036 entry = entry->prev)
1038 xfree (record_full_core_buf_list);
1039 record_full_core_buf_list = entry;
1041 record_full_core_buf_list = NULL;
1044 if (record_full_async_inferior_event_token)
1045 delete_async_event_handler (&record_full_async_inferior_event_token);
1048 static int record_full_resume_step = 0;
1050 /* True if we've been resumed, and so each record_full_wait call should
1051 advance execution. If this is false, record_full_wait will return a
1052 TARGET_WAITKIND_IGNORE. */
1053 static int record_full_resumed = 0;
1055 /* The execution direction of the last resume we got. This is
1056 necessary for async mode. Vis (order is not strictly accurate):
1058 1. user has the global execution direction set to forward
1059 2. user does a reverse-step command
1060 3. record_full_resume is called with global execution direction
1061 temporarily switched to reverse
1062 4. GDB's execution direction is reverted back to forward
1063 5. target record notifies event loop there's an event to handle
1064 6. infrun asks the target which direction was it going, and switches
1065 the global execution direction accordingly (to reverse)
1066 7. infrun polls an event out of the record target, and handles it
1067 8. GDB goes back to the event loop, and goto #4.
1069 static enum exec_direction_kind record_full_execution_dir = EXEC_FORWARD;
1071 /* "to_resume" target method. Resume the process record target. */
1074 record_full_resume (struct target_ops *ops, ptid_t ptid, int step,
1075 enum gdb_signal signal)
1077 record_full_resume_step = step;
1078 record_full_resumed = 1;
1079 record_full_execution_dir = execution_direction;
1081 if (!RECORD_FULL_IS_REPLAY)
1083 struct gdbarch *gdbarch = target_thread_architecture (ptid);
1085 record_full_message (get_current_regcache (), signal);
1089 /* This is not hard single step. */
1090 if (!gdbarch_software_single_step_p (gdbarch))
1092 /* This is a normal continue. */
1097 /* This arch support soft sigle step. */
1098 if (single_step_breakpoints_inserted ())
1100 /* This is a soft single step. */
1101 record_full_resume_step = 1;
1105 /* This is a continue.
1106 Try to insert a soft single step breakpoint. */
1107 if (!gdbarch_software_single_step (gdbarch,
1108 get_current_frame ()))
1110 /* This system don't want use soft single step.
1111 Use hard sigle step. */
1118 /* Make sure the target beneath reports all signals. */
1119 target_pass_signals (0, NULL);
1121 record_full_beneath_to_resume (record_full_beneath_to_resume_ops,
1122 ptid, step, signal);
1125 /* We are about to start executing the inferior (or simulate it),
1126 let's register it with the event loop. */
1127 if (target_can_async_p ())
1129 target_async (inferior_event_handler, 0);
1130 /* Notify the event loop there's an event to wait for. We do
1131 most of the work in record_full_wait. */
1132 mark_async_event_handler (record_full_async_inferior_event_token);
1136 static int record_full_get_sig = 0;
1138 /* SIGINT signal handler, registered by "to_wait" method. */
1141 record_full_sig_handler (int signo)
1144 fprintf_unfiltered (gdb_stdlog, "Process record: get a signal\n");
1146 /* It will break the running inferior in replay mode. */
1147 record_full_resume_step = 1;
1149 /* It will let record_full_wait set inferior status to get the signal
1151 record_full_get_sig = 1;
1155 record_full_wait_cleanups (void *ignore)
1157 if (execution_direction == EXEC_REVERSE)
1159 if (record_full_list->next)
1160 record_full_list = record_full_list->next;
1163 record_full_list = record_full_list->prev;
1166 /* "to_wait" target method for process record target.
1168 In record mode, the target is always run in singlestep mode
1169 (even when gdb says to continue). The to_wait method intercepts
1170 the stop events and determines which ones are to be passed on to
1171 gdb. Most stop events are just singlestep events that gdb is not
1172 to know about, so the to_wait method just records them and keeps
1175 In replay mode, this function emulates the recorded execution log,
1176 one instruction at a time (forward or backward), and determines
1180 record_full_wait_1 (struct target_ops *ops,
1181 ptid_t ptid, struct target_waitstatus *status,
1184 struct cleanup *set_cleanups = record_full_gdb_operation_disable_set ();
1187 fprintf_unfiltered (gdb_stdlog,
1188 "Process record: record_full_wait "
1189 "record_full_resume_step = %d, "
1190 "record_full_resumed = %d, direction=%s\n",
1191 record_full_resume_step, record_full_resumed,
1192 record_full_execution_dir == EXEC_FORWARD
1193 ? "forward" : "reverse");
1195 if (!record_full_resumed)
1197 gdb_assert ((options & TARGET_WNOHANG) != 0);
1199 /* No interesting event. */
1200 status->kind = TARGET_WAITKIND_IGNORE;
1201 return minus_one_ptid;
1204 record_full_get_sig = 0;
1205 signal (SIGINT, record_full_sig_handler);
1207 if (!RECORD_FULL_IS_REPLAY && ops != &record_full_core_ops)
1209 if (record_full_resume_step)
1211 /* This is a single step. */
1212 return record_full_beneath_to_wait (record_full_beneath_to_wait_ops,
1213 ptid, status, options);
1217 /* This is not a single step. */
1220 struct gdbarch *gdbarch = target_thread_architecture (inferior_ptid);
1224 ret = record_full_beneath_to_wait
1225 (record_full_beneath_to_wait_ops, ptid, status, options);
1226 if (status->kind == TARGET_WAITKIND_IGNORE)
1229 fprintf_unfiltered (gdb_stdlog,
1230 "Process record: record_full_wait "
1231 "target beneath not done yet\n");
1235 if (single_step_breakpoints_inserted ())
1236 remove_single_step_breakpoints ();
1238 if (record_full_resume_step)
1241 /* Is this a SIGTRAP? */
1242 if (status->kind == TARGET_WAITKIND_STOPPED
1243 && status->value.sig == GDB_SIGNAL_TRAP)
1245 struct regcache *regcache;
1246 struct address_space *aspace;
1248 /* Yes -- this is likely our single-step finishing,
1249 but check if there's any reason the core would be
1250 interested in the event. */
1252 registers_changed ();
1253 regcache = get_current_regcache ();
1254 tmp_pc = regcache_read_pc (regcache);
1255 aspace = get_regcache_aspace (regcache);
1257 if (target_stopped_by_watchpoint ())
1259 /* Always interested in watchpoints. */
1261 else if (breakpoint_inserted_here_p (aspace, tmp_pc))
1263 /* There is a breakpoint here. Let the core
1265 if (software_breakpoint_inserted_here_p (aspace, tmp_pc))
1267 struct gdbarch *gdbarch
1268 = get_regcache_arch (regcache);
1269 CORE_ADDR decr_pc_after_break
1270 = gdbarch_decr_pc_after_break (gdbarch);
1271 if (decr_pc_after_break)
1272 regcache_write_pc (regcache,
1273 tmp_pc + decr_pc_after_break);
1278 /* This is a single-step trap. Record the
1279 insn and issue another step.
1280 FIXME: this part can be a random SIGTRAP too.
1281 But GDB cannot handle it. */
1284 if (!record_full_message_wrapper_safe (regcache,
1287 status->kind = TARGET_WAITKIND_STOPPED;
1288 status->value.sig = GDB_SIGNAL_0;
1292 if (gdbarch_software_single_step_p (gdbarch))
1294 /* Try to insert the software single step breakpoint.
1295 If insert success, set step to 0. */
1296 set_executing (inferior_ptid, 0);
1297 reinit_frame_cache ();
1298 if (gdbarch_software_single_step (gdbarch,
1299 get_current_frame ()))
1301 set_executing (inferior_ptid, 1);
1305 fprintf_unfiltered (gdb_stdlog,
1306 "Process record: record_full_wait "
1307 "issuing one more step in the "
1308 "target beneath\n");
1309 record_full_beneath_to_resume
1310 (record_full_beneath_to_resume_ops, ptid, step,
1316 /* The inferior is broken by a breakpoint or a signal. */
1325 struct regcache *regcache = get_current_regcache ();
1326 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1327 struct address_space *aspace = get_regcache_aspace (regcache);
1328 int continue_flag = 1;
1329 int first_record_full_end = 1;
1330 struct cleanup *old_cleanups
1331 = make_cleanup (record_full_wait_cleanups, 0);
1334 record_full_hw_watchpoint = 0;
1335 status->kind = TARGET_WAITKIND_STOPPED;
1337 /* Check breakpoint when forward execute. */
1338 if (execution_direction == EXEC_FORWARD)
1340 tmp_pc = regcache_read_pc (regcache);
1341 if (breakpoint_inserted_here_p (aspace, tmp_pc))
1343 int decr_pc_after_break = gdbarch_decr_pc_after_break (gdbarch);
1346 fprintf_unfiltered (gdb_stdlog,
1347 "Process record: break at %s.\n",
1348 paddress (gdbarch, tmp_pc));
1350 if (decr_pc_after_break
1351 && !record_full_resume_step
1352 && software_breakpoint_inserted_here_p (aspace, tmp_pc))
1353 regcache_write_pc (regcache,
1354 tmp_pc + decr_pc_after_break);
1359 /* If GDB is in terminal_inferior mode, it will not get the signal.
1360 And in GDB replay mode, GDB doesn't need to be in terminal_inferior
1361 mode, because inferior will not executed.
1362 Then set it to terminal_ours to make GDB get the signal. */
1363 target_terminal_ours ();
1365 /* In EXEC_FORWARD mode, record_full_list points to the tail of prev
1367 if (execution_direction == EXEC_FORWARD && record_full_list->next)
1368 record_full_list = record_full_list->next;
1370 /* Loop over the record_full_list, looking for the next place to
1374 /* Check for beginning and end of log. */
1375 if (execution_direction == EXEC_REVERSE
1376 && record_full_list == &record_full_first)
1378 /* Hit beginning of record log in reverse. */
1379 status->kind = TARGET_WAITKIND_NO_HISTORY;
1382 if (execution_direction != EXEC_REVERSE && !record_full_list->next)
1384 /* Hit end of record log going forward. */
1385 status->kind = TARGET_WAITKIND_NO_HISTORY;
1389 record_full_exec_insn (regcache, gdbarch, record_full_list);
1391 if (record_full_list->type == record_full_end)
1393 if (record_debug > 1)
1394 fprintf_unfiltered (gdb_stdlog,
1395 "Process record: record_full_end %s to "
1397 host_address_to_string (record_full_list));
1399 if (first_record_full_end && execution_direction == EXEC_REVERSE)
1401 /* When reverse excute, the first record_full_end is the
1402 part of current instruction. */
1403 first_record_full_end = 0;
1407 /* In EXEC_REVERSE mode, this is the record_full_end of prev
1409 In EXEC_FORWARD mode, this is the record_full_end of
1410 current instruction. */
1412 if (record_full_resume_step)
1414 if (record_debug > 1)
1415 fprintf_unfiltered (gdb_stdlog,
1416 "Process record: step.\n");
1420 /* check breakpoint */
1421 tmp_pc = regcache_read_pc (regcache);
1422 if (breakpoint_inserted_here_p (aspace, tmp_pc))
1424 int decr_pc_after_break
1425 = gdbarch_decr_pc_after_break (gdbarch);
1428 fprintf_unfiltered (gdb_stdlog,
1429 "Process record: break "
1431 paddress (gdbarch, tmp_pc));
1432 if (decr_pc_after_break
1433 && execution_direction == EXEC_FORWARD
1434 && !record_full_resume_step
1435 && software_breakpoint_inserted_here_p (aspace,
1437 regcache_write_pc (regcache,
1438 tmp_pc + decr_pc_after_break);
1442 if (record_full_hw_watchpoint)
1445 fprintf_unfiltered (gdb_stdlog,
1446 "Process record: hit hw "
1450 /* Check target signal */
1451 if (record_full_list->u.end.sigval != GDB_SIGNAL_0)
1452 /* FIXME: better way to check */
1459 if (execution_direction == EXEC_REVERSE)
1461 if (record_full_list->prev)
1462 record_full_list = record_full_list->prev;
1466 if (record_full_list->next)
1467 record_full_list = record_full_list->next;
1471 while (continue_flag);
1474 if (record_full_get_sig)
1475 status->value.sig = GDB_SIGNAL_INT;
1476 else if (record_full_list->u.end.sigval != GDB_SIGNAL_0)
1477 /* FIXME: better way to check */
1478 status->value.sig = record_full_list->u.end.sigval;
1480 status->value.sig = GDB_SIGNAL_TRAP;
1482 discard_cleanups (old_cleanups);
1485 signal (SIGINT, handle_sigint);
1487 do_cleanups (set_cleanups);
1488 return inferior_ptid;
1492 record_full_wait (struct target_ops *ops,
1493 ptid_t ptid, struct target_waitstatus *status,
1498 return_ptid = record_full_wait_1 (ops, ptid, status, options);
1499 if (status->kind != TARGET_WAITKIND_IGNORE)
1501 /* We're reporting a stop. Make sure any spurious
1502 target_wait(WNOHANG) doesn't advance the target until the
1503 core wants us resumed again. */
1504 record_full_resumed = 0;
1510 record_full_stopped_by_watchpoint (void)
1512 if (RECORD_FULL_IS_REPLAY)
1513 return record_full_hw_watchpoint;
1515 return record_full_beneath_to_stopped_by_watchpoint ();
1519 record_full_stopped_data_address (struct target_ops *ops, CORE_ADDR *addr_p)
1521 if (RECORD_FULL_IS_REPLAY)
1524 return record_full_beneath_to_stopped_data_address (ops, addr_p);
1527 /* Record registers change (by user or by GDB) to list as an instruction. */
1530 record_full_registers_change (struct regcache *regcache, int regnum)
1532 /* Check record_full_insn_num. */
1533 record_full_check_insn_num (0);
1535 record_full_arch_list_head = NULL;
1536 record_full_arch_list_tail = NULL;
1542 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
1544 if (record_full_arch_list_add_reg (regcache, i))
1546 record_full_list_release (record_full_arch_list_tail);
1547 error (_("Process record: failed to record execution log."));
1553 if (record_full_arch_list_add_reg (regcache, regnum))
1555 record_full_list_release (record_full_arch_list_tail);
1556 error (_("Process record: failed to record execution log."));
1559 if (record_full_arch_list_add_end ())
1561 record_full_list_release (record_full_arch_list_tail);
1562 error (_("Process record: failed to record execution log."));
1564 record_full_list->next = record_full_arch_list_head;
1565 record_full_arch_list_head->prev = record_full_list;
1566 record_full_list = record_full_arch_list_tail;
1568 if (record_full_insn_num == record_full_insn_max_num)
1569 record_full_list_release_first ();
1571 record_full_insn_num++;
1574 /* "to_store_registers" method for process record target. */
1577 record_full_store_registers (struct target_ops *ops,
1578 struct regcache *regcache,
1581 if (!record_full_gdb_operation_disable)
1583 if (RECORD_FULL_IS_REPLAY)
1587 /* Let user choose if he wants to write register or not. */
1590 query (_("Because GDB is in replay mode, changing the "
1591 "value of a register will make the execution "
1592 "log unusable from this point onward. "
1593 "Change all registers?"));
1596 query (_("Because GDB is in replay mode, changing the value "
1597 "of a register will make the execution log unusable "
1598 "from this point onward. Change register %s?"),
1599 gdbarch_register_name (get_regcache_arch (regcache),
1604 /* Invalidate the value of regcache that was set in function
1605 "regcache_raw_write". */
1611 i < gdbarch_num_regs (get_regcache_arch (regcache));
1613 regcache_invalidate (regcache, i);
1616 regcache_invalidate (regcache, regno);
1618 error (_("Process record canceled the operation."));
1621 /* Destroy the record from here forward. */
1622 record_full_list_release_following (record_full_list);
1625 record_full_registers_change (regcache, regno);
1627 record_full_beneath_to_store_registers
1628 (record_full_beneath_to_store_registers_ops, regcache, regno);
1631 /* "to_xfer_partial" method. Behavior is conditional on
1632 RECORD_FULL_IS_REPLAY.
1633 In replay mode, we cannot write memory unles we are willing to
1634 invalidate the record/replay log from this point forward. */
1637 record_full_xfer_partial (struct target_ops *ops, enum target_object object,
1638 const char *annex, gdb_byte *readbuf,
1639 const gdb_byte *writebuf, ULONGEST offset,
1642 if (!record_full_gdb_operation_disable
1643 && (object == TARGET_OBJECT_MEMORY
1644 || object == TARGET_OBJECT_RAW_MEMORY) && writebuf)
1646 if (RECORD_FULL_IS_REPLAY)
1648 /* Let user choose if he wants to write memory or not. */
1649 if (!query (_("Because GDB is in replay mode, writing to memory "
1650 "will make the execution log unusable from this "
1651 "point onward. Write memory at address %s?"),
1652 paddress (target_gdbarch (), offset)))
1653 error (_("Process record canceled the operation."));
1655 /* Destroy the record from here forward. */
1656 record_full_list_release_following (record_full_list);
1659 /* Check record_full_insn_num */
1660 record_full_check_insn_num (0);
1662 /* Record registers change to list as an instruction. */
1663 record_full_arch_list_head = NULL;
1664 record_full_arch_list_tail = NULL;
1665 if (record_full_arch_list_add_mem (offset, len))
1667 record_full_list_release (record_full_arch_list_tail);
1669 fprintf_unfiltered (gdb_stdlog,
1670 "Process record: failed to record "
1674 if (record_full_arch_list_add_end ())
1676 record_full_list_release (record_full_arch_list_tail);
1678 fprintf_unfiltered (gdb_stdlog,
1679 "Process record: failed to record "
1683 record_full_list->next = record_full_arch_list_head;
1684 record_full_arch_list_head->prev = record_full_list;
1685 record_full_list = record_full_arch_list_tail;
1687 if (record_full_insn_num == record_full_insn_max_num)
1688 record_full_list_release_first ();
1690 record_full_insn_num++;
1693 return record_full_beneath_to_xfer_partial
1694 (record_full_beneath_to_xfer_partial_ops, object, annex,
1695 readbuf, writebuf, offset, len);
1698 /* This structure represents a breakpoint inserted while the record
1699 target is active. We use this to know when to install/remove
1700 breakpoints in/from the target beneath. For example, a breakpoint
1701 may be inserted while recording, but removed when not replaying nor
1702 recording. In that case, the breakpoint had not been inserted on
1703 the target beneath, so we should not try to remove it there. */
1705 struct record_full_breakpoint
1707 /* The address and address space the breakpoint was set at. */
1708 struct address_space *address_space;
1711 /* True when the breakpoint has been also installed in the target
1712 beneath. This will be false for breakpoints set during replay or
1714 int in_target_beneath;
1717 typedef struct record_full_breakpoint *record_full_breakpoint_p;
1718 DEF_VEC_P(record_full_breakpoint_p);
1720 /* The list of breakpoints inserted while the record target is
1722 VEC(record_full_breakpoint_p) *record_full_breakpoints = NULL;
1725 record_full_sync_record_breakpoints (struct bp_location *loc, void *data)
1727 if (loc->loc_type != bp_loc_software_breakpoint)
1732 struct record_full_breakpoint *bp = XNEW (struct record_full_breakpoint);
1734 bp->addr = loc->target_info.placed_address;
1735 bp->address_space = loc->target_info.placed_address_space;
1737 bp->in_target_beneath = 1;
1739 VEC_safe_push (record_full_breakpoint_p, record_full_breakpoints, bp);
1743 /* Sync existing breakpoints to record_full_breakpoints. */
1746 record_full_init_record_breakpoints (void)
1748 VEC_free (record_full_breakpoint_p, record_full_breakpoints);
1750 iterate_over_bp_locations (record_full_sync_record_breakpoints);
1753 /* Behavior is conditional on RECORD_FULL_IS_REPLAY. We will not actually
1754 insert or remove breakpoints in the real target when replaying, nor
1758 record_full_insert_breakpoint (struct gdbarch *gdbarch,
1759 struct bp_target_info *bp_tgt)
1761 struct record_full_breakpoint *bp;
1762 int in_target_beneath = 0;
1764 if (!RECORD_FULL_IS_REPLAY)
1766 /* When recording, we currently always single-step, so we don't
1767 really need to install regular breakpoints in the inferior.
1768 However, we do have to insert software single-step
1769 breakpoints, in case the target can't hardware step. To keep
1770 things single, we always insert. */
1771 struct cleanup *old_cleanups;
1774 old_cleanups = record_full_gdb_operation_disable_set ();
1775 ret = record_full_beneath_to_insert_breakpoint (gdbarch, bp_tgt);
1776 do_cleanups (old_cleanups);
1781 in_target_beneath = 1;
1784 bp = XNEW (struct record_full_breakpoint);
1785 bp->addr = bp_tgt->placed_address;
1786 bp->address_space = bp_tgt->placed_address_space;
1787 bp->in_target_beneath = in_target_beneath;
1788 VEC_safe_push (record_full_breakpoint_p, record_full_breakpoints, bp);
1792 /* "to_remove_breakpoint" method for process record target. */
1795 record_full_remove_breakpoint (struct gdbarch *gdbarch,
1796 struct bp_target_info *bp_tgt)
1798 struct record_full_breakpoint *bp;
1802 VEC_iterate (record_full_breakpoint_p,
1803 record_full_breakpoints, ix, bp);
1806 if (bp->addr == bp_tgt->placed_address
1807 && bp->address_space == bp_tgt->placed_address_space)
1809 if (bp->in_target_beneath)
1811 struct cleanup *old_cleanups;
1814 old_cleanups = record_full_gdb_operation_disable_set ();
1815 ret = record_full_beneath_to_remove_breakpoint (gdbarch, bp_tgt);
1816 do_cleanups (old_cleanups);
1822 VEC_unordered_remove (record_full_breakpoint_p,
1823 record_full_breakpoints, ix);
1828 gdb_assert_not_reached ("removing unknown breakpoint");
1831 /* "to_can_execute_reverse" method for process record target. */
1834 record_full_can_execute_reverse (void)
1839 /* "to_get_bookmark" method for process record and prec over core. */
1842 record_full_get_bookmark (char *args, int from_tty)
1846 /* Return stringified form of instruction count. */
1847 if (record_full_list && record_full_list->type == record_full_end)
1848 ret = xstrdup (pulongest (record_full_list->u.end.insn_num));
1853 fprintf_unfiltered (gdb_stdlog,
1854 "record_full_get_bookmark returns %s\n", ret);
1856 fprintf_unfiltered (gdb_stdlog,
1857 "record_full_get_bookmark returns NULL\n");
1859 return (gdb_byte *) ret;
1862 /* "to_goto_bookmark" method for process record and prec over core. */
1865 record_full_goto_bookmark (gdb_byte *raw_bookmark, int from_tty)
1867 char *bookmark = (char *) raw_bookmark;
1870 fprintf_unfiltered (gdb_stdlog,
1871 "record_full_goto_bookmark receives %s\n", bookmark);
1873 if (bookmark[0] == '\'' || bookmark[0] == '\"')
1875 if (bookmark[strlen (bookmark) - 1] != bookmark[0])
1876 error (_("Unbalanced quotes: %s"), bookmark);
1878 /* Strip trailing quote. */
1879 bookmark[strlen (bookmark) - 1] = '\0';
1880 /* Strip leading quote. */
1882 /* Pass along to cmd_record_full_goto. */
1885 cmd_record_goto (bookmark, from_tty);
1890 record_full_async (void (*callback) (enum inferior_event_type event_type,
1891 void *context), void *context)
1893 /* If we're on top of a line target (e.g., linux-nat, remote), then
1894 set it to async mode as well. Will be NULL if we're sitting on
1895 top of the core target, for "record restore". */
1896 if (record_full_beneath_to_async != NULL)
1897 record_full_beneath_to_async (callback, context);
1901 record_full_can_async_p (void)
1903 /* We only enable async when the user specifically asks for it. */
1904 return target_async_permitted;
1908 record_full_is_async_p (void)
1910 /* We only enable async when the user specifically asks for it. */
1911 return target_async_permitted;
1914 static enum exec_direction_kind
1915 record_full_execution_direction (void)
1917 return record_full_execution_dir;
1921 record_full_info (void)
1923 struct record_full_entry *p;
1925 if (RECORD_FULL_IS_REPLAY)
1926 printf_filtered (_("Replay mode:\n"));
1928 printf_filtered (_("Record mode:\n"));
1930 /* Find entry for first actual instruction in the log. */
1931 for (p = record_full_first.next;
1932 p != NULL && p->type != record_full_end;
1936 /* Do we have a log at all? */
1937 if (p != NULL && p->type == record_full_end)
1939 /* Display instruction number for first instruction in the log. */
1940 printf_filtered (_("Lowest recorded instruction number is %s.\n"),
1941 pulongest (p->u.end.insn_num));
1943 /* If in replay mode, display where we are in the log. */
1944 if (RECORD_FULL_IS_REPLAY)
1945 printf_filtered (_("Current instruction number is %s.\n"),
1946 pulongest (record_full_list->u.end.insn_num));
1948 /* Display instruction number for last instruction in the log. */
1949 printf_filtered (_("Highest recorded instruction number is %s.\n"),
1950 pulongest (record_full_insn_count));
1952 /* Display log count. */
1953 printf_filtered (_("Log contains %u instructions.\n"),
1954 record_full_insn_num);
1957 printf_filtered (_("No instructions have been logged.\n"));
1959 /* Display max log size. */
1960 printf_filtered (_("Max logged instructions is %u.\n"),
1961 record_full_insn_max_num);
1964 /* The "to_record_delete" target method. */
1967 record_full_delete (void)
1969 record_full_list_release_following (record_full_list);
1972 /* The "to_record_is_replaying" target method. */
1975 record_full_is_replaying (void)
1977 return RECORD_FULL_IS_REPLAY;
1980 /* Go to a specific entry. */
1983 record_full_goto_entry (struct record_full_entry *p)
1986 error (_("Target insn not found."));
1987 else if (p == record_full_list)
1988 error (_("Already at target insn."));
1989 else if (p->u.end.insn_num > record_full_list->u.end.insn_num)
1991 printf_filtered (_("Go forward to insn number %s\n"),
1992 pulongest (p->u.end.insn_num));
1993 record_full_goto_insn (p, EXEC_FORWARD);
1997 printf_filtered (_("Go backward to insn number %s\n"),
1998 pulongest (p->u.end.insn_num));
1999 record_full_goto_insn (p, EXEC_REVERSE);
2002 registers_changed ();
2003 reinit_frame_cache ();
2004 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
2007 /* The "to_goto_record_begin" target method. */
2010 record_full_goto_begin (void)
2012 struct record_full_entry *p = NULL;
2014 for (p = &record_full_first; p != NULL; p = p->next)
2015 if (p->type == record_full_end)
2018 record_full_goto_entry (p);
2021 /* The "to_goto_record_end" target method. */
2024 record_full_goto_end (void)
2026 struct record_full_entry *p = NULL;
2028 for (p = record_full_list; p->next != NULL; p = p->next)
2030 for (; p!= NULL; p = p->prev)
2031 if (p->type == record_full_end)
2034 record_full_goto_entry (p);
2037 /* The "to_goto_record" target method. */
2040 record_full_goto (ULONGEST target_insn)
2042 struct record_full_entry *p = NULL;
2044 for (p = &record_full_first; p != NULL; p = p->next)
2045 if (p->type == record_full_end && p->u.end.insn_num == target_insn)
2048 record_full_goto_entry (p);
2052 init_record_full_ops (void)
2054 record_full_ops.to_shortname = "record-full";
2055 record_full_ops.to_longname = "Process record and replay target";
2056 record_full_ops.to_doc =
2057 "Log program while executing and replay execution from log.";
2058 record_full_ops.to_open = record_full_open;
2059 record_full_ops.to_close = record_full_close;
2060 record_full_ops.to_resume = record_full_resume;
2061 record_full_ops.to_wait = record_full_wait;
2062 record_full_ops.to_disconnect = record_disconnect;
2063 record_full_ops.to_detach = record_detach;
2064 record_full_ops.to_mourn_inferior = record_mourn_inferior;
2065 record_full_ops.to_kill = record_kill;
2066 record_full_ops.to_create_inferior = find_default_create_inferior;
2067 record_full_ops.to_store_registers = record_full_store_registers;
2068 record_full_ops.to_xfer_partial = record_full_xfer_partial;
2069 record_full_ops.to_insert_breakpoint = record_full_insert_breakpoint;
2070 record_full_ops.to_remove_breakpoint = record_full_remove_breakpoint;
2071 record_full_ops.to_stopped_by_watchpoint = record_full_stopped_by_watchpoint;
2072 record_full_ops.to_stopped_data_address = record_full_stopped_data_address;
2073 record_full_ops.to_can_execute_reverse = record_full_can_execute_reverse;
2074 record_full_ops.to_stratum = record_stratum;
2075 /* Add bookmark target methods. */
2076 record_full_ops.to_get_bookmark = record_full_get_bookmark;
2077 record_full_ops.to_goto_bookmark = record_full_goto_bookmark;
2078 record_full_ops.to_async = record_full_async;
2079 record_full_ops.to_can_async_p = record_full_can_async_p;
2080 record_full_ops.to_is_async_p = record_full_is_async_p;
2081 record_full_ops.to_execution_direction = record_full_execution_direction;
2082 record_full_ops.to_info_record = record_full_info;
2083 record_full_ops.to_save_record = record_full_save;
2084 record_full_ops.to_delete_record = record_full_delete;
2085 record_full_ops.to_record_is_replaying = record_full_is_replaying;
2086 record_full_ops.to_goto_record_begin = record_full_goto_begin;
2087 record_full_ops.to_goto_record_end = record_full_goto_end;
2088 record_full_ops.to_goto_record = record_full_goto;
2089 record_full_ops.to_magic = OPS_MAGIC;
2092 /* "to_resume" method for prec over corefile. */
2095 record_full_core_resume (struct target_ops *ops, ptid_t ptid, int step,
2096 enum gdb_signal signal)
2098 record_full_resume_step = step;
2099 record_full_resumed = 1;
2100 record_full_execution_dir = execution_direction;
2102 /* We are about to start executing the inferior (or simulate it),
2103 let's register it with the event loop. */
2104 if (target_can_async_p ())
2106 target_async (inferior_event_handler, 0);
2108 /* Notify the event loop there's an event to wait for. */
2109 mark_async_event_handler (record_full_async_inferior_event_token);
2113 /* "to_kill" method for prec over corefile. */
2116 record_full_core_kill (struct target_ops *ops)
2119 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_core_kill\n");
2121 unpush_target (&record_full_core_ops);
2124 /* "to_fetch_registers" method for prec over corefile. */
2127 record_full_core_fetch_registers (struct target_ops *ops,
2128 struct regcache *regcache,
2133 int num = gdbarch_num_regs (get_regcache_arch (regcache));
2136 for (i = 0; i < num; i ++)
2137 regcache_raw_supply (regcache, i,
2138 record_full_core_regbuf + MAX_REGISTER_SIZE * i);
2141 regcache_raw_supply (regcache, regno,
2142 record_full_core_regbuf + MAX_REGISTER_SIZE * regno);
2145 /* "to_prepare_to_store" method for prec over corefile. */
2148 record_full_core_prepare_to_store (struct target_ops *self,
2149 struct regcache *regcache)
2153 /* "to_store_registers" method for prec over corefile. */
2156 record_full_core_store_registers (struct target_ops *ops,
2157 struct regcache *regcache,
2160 if (record_full_gdb_operation_disable)
2161 regcache_raw_collect (regcache, regno,
2162 record_full_core_regbuf + MAX_REGISTER_SIZE * regno);
2164 error (_("You can't do that without a process to debug."));
2167 /* "to_xfer_partial" method for prec over corefile. */
2170 record_full_core_xfer_partial (struct target_ops *ops,
2171 enum target_object object,
2172 const char *annex, gdb_byte *readbuf,
2173 const gdb_byte *writebuf, ULONGEST offset,
2176 if (object == TARGET_OBJECT_MEMORY)
2178 if (record_full_gdb_operation_disable || !writebuf)
2180 struct target_section *p;
2182 for (p = record_full_core_start; p < record_full_core_end; p++)
2184 if (offset >= p->addr)
2186 struct record_full_core_buf_entry *entry;
2187 ULONGEST sec_offset;
2189 if (offset >= p->endaddr)
2192 if (offset + len > p->endaddr)
2193 len = p->endaddr - offset;
2195 sec_offset = offset - p->addr;
2197 /* Read readbuf or write writebuf p, offset, len. */
2199 if (p->the_bfd_section->flags & SEC_CONSTRUCTOR
2200 || (p->the_bfd_section->flags & SEC_HAS_CONTENTS) == 0)
2203 memset (readbuf, 0, len);
2206 /* Get record_full_core_buf_entry. */
2207 for (entry = record_full_core_buf_list; entry;
2208 entry = entry->prev)
2215 /* Add a new entry. */
2216 entry = (struct record_full_core_buf_entry *)
2218 (sizeof (struct record_full_core_buf_entry));
2220 if (!bfd_malloc_and_get_section
2221 (p->the_bfd_section->owner,
2228 entry->prev = record_full_core_buf_list;
2229 record_full_core_buf_list = entry;
2232 memcpy (entry->buf + sec_offset, writebuf,
2238 return record_full_beneath_to_xfer_partial
2239 (record_full_beneath_to_xfer_partial_ops,
2240 object, annex, readbuf, writebuf,
2243 memcpy (readbuf, entry->buf + sec_offset,
2254 error (_("You can't do that without a process to debug."));
2257 return record_full_beneath_to_xfer_partial
2258 (record_full_beneath_to_xfer_partial_ops, object, annex,
2259 readbuf, writebuf, offset, len);
2262 /* "to_insert_breakpoint" method for prec over corefile. */
2265 record_full_core_insert_breakpoint (struct gdbarch *gdbarch,
2266 struct bp_target_info *bp_tgt)
2271 /* "to_remove_breakpoint" method for prec over corefile. */
2274 record_full_core_remove_breakpoint (struct gdbarch *gdbarch,
2275 struct bp_target_info *bp_tgt)
2280 /* "to_has_execution" method for prec over corefile. */
2283 record_full_core_has_execution (struct target_ops *ops, ptid_t the_ptid)
2289 init_record_full_core_ops (void)
2291 record_full_core_ops.to_shortname = "record-core";
2292 record_full_core_ops.to_longname = "Process record and replay target";
2293 record_full_core_ops.to_doc =
2294 "Log program while executing and replay execution from log.";
2295 record_full_core_ops.to_open = record_full_open;
2296 record_full_core_ops.to_close = record_full_close;
2297 record_full_core_ops.to_resume = record_full_core_resume;
2298 record_full_core_ops.to_wait = record_full_wait;
2299 record_full_core_ops.to_kill = record_full_core_kill;
2300 record_full_core_ops.to_fetch_registers = record_full_core_fetch_registers;
2301 record_full_core_ops.to_prepare_to_store = record_full_core_prepare_to_store;
2302 record_full_core_ops.to_store_registers = record_full_core_store_registers;
2303 record_full_core_ops.to_xfer_partial = record_full_core_xfer_partial;
2304 record_full_core_ops.to_insert_breakpoint
2305 = record_full_core_insert_breakpoint;
2306 record_full_core_ops.to_remove_breakpoint
2307 = record_full_core_remove_breakpoint;
2308 record_full_core_ops.to_stopped_by_watchpoint
2309 = record_full_stopped_by_watchpoint;
2310 record_full_core_ops.to_stopped_data_address
2311 = record_full_stopped_data_address;
2312 record_full_core_ops.to_can_execute_reverse
2313 = record_full_can_execute_reverse;
2314 record_full_core_ops.to_has_execution = record_full_core_has_execution;
2315 record_full_core_ops.to_stratum = record_stratum;
2316 /* Add bookmark target methods. */
2317 record_full_core_ops.to_get_bookmark = record_full_get_bookmark;
2318 record_full_core_ops.to_goto_bookmark = record_full_goto_bookmark;
2319 record_full_core_ops.to_async = record_full_async;
2320 record_full_core_ops.to_can_async_p = record_full_can_async_p;
2321 record_full_core_ops.to_is_async_p = record_full_is_async_p;
2322 record_full_core_ops.to_execution_direction
2323 = record_full_execution_direction;
2324 record_full_core_ops.to_info_record = record_full_info;
2325 record_full_core_ops.to_delete_record = record_full_delete;
2326 record_full_core_ops.to_record_is_replaying = record_full_is_replaying;
2327 record_full_core_ops.to_goto_record_begin = record_full_goto_begin;
2328 record_full_core_ops.to_goto_record_end = record_full_goto_end;
2329 record_full_core_ops.to_goto_record = record_full_goto;
2330 record_full_core_ops.to_magic = OPS_MAGIC;
2333 /* Record log save-file format
2334 Version 1 (never released)
2337 4 bytes: magic number htonl(0x20090829).
2338 NOTE: be sure to change whenever this file format changes!
2342 1 byte: record type (record_full_end, see enum record_full_type).
2344 1 byte: record type (record_full_reg, see enum record_full_type).
2345 8 bytes: register id (network byte order).
2346 MAX_REGISTER_SIZE bytes: register value.
2348 1 byte: record type (record_full_mem, see enum record_full_type).
2349 8 bytes: memory length (network byte order).
2350 8 bytes: memory address (network byte order).
2351 n bytes: memory value (n == memory length).
2354 4 bytes: magic number netorder32(0x20091016).
2355 NOTE: be sure to change whenever this file format changes!
2359 1 byte: record type (record_full_end, see enum record_full_type).
2361 4 bytes: instruction count
2363 1 byte: record type (record_full_reg, see enum record_full_type).
2364 4 bytes: register id (network byte order).
2365 n bytes: register value (n == actual register size).
2366 (eg. 4 bytes for x86 general registers).
2368 1 byte: record type (record_full_mem, see enum record_full_type).
2369 4 bytes: memory length (network byte order).
2370 8 bytes: memory address (network byte order).
2371 n bytes: memory value (n == memory length).
2375 /* bfdcore_read -- read bytes from a core file section. */
2378 bfdcore_read (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2380 int ret = bfd_get_section_contents (obfd, osec, buf, *offset, len);
2385 error (_("Failed to read %d bytes from core file %s ('%s')."),
2386 len, bfd_get_filename (obfd),
2387 bfd_errmsg (bfd_get_error ()));
2390 static inline uint64_t
2391 netorder64 (uint64_t input)
2395 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2396 BFD_ENDIAN_BIG, input);
2400 static inline uint32_t
2401 netorder32 (uint32_t input)
2405 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2406 BFD_ENDIAN_BIG, input);
2410 static inline uint16_t
2411 netorder16 (uint16_t input)
2415 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2416 BFD_ENDIAN_BIG, input);
2420 /* Restore the execution log from a core_bfd file. */
2422 record_full_restore (void)
2425 struct cleanup *old_cleanups;
2426 struct record_full_entry *rec;
2430 struct regcache *regcache;
2432 /* We restore the execution log from the open core bfd,
2434 if (core_bfd == NULL)
2437 /* "record_full_restore" can only be called when record list is empty. */
2438 gdb_assert (record_full_first.next == NULL);
2441 fprintf_unfiltered (gdb_stdlog, "Restoring recording from core file.\n");
2443 /* Now need to find our special note section. */
2444 osec = bfd_get_section_by_name (core_bfd, "null0");
2446 fprintf_unfiltered (gdb_stdlog, "Find precord section %s.\n",
2447 osec ? "succeeded" : "failed");
2450 osec_size = bfd_section_size (core_bfd, osec);
2452 fprintf_unfiltered (gdb_stdlog, "%s", bfd_section_name (core_bfd, osec));
2454 /* Check the magic code. */
2455 bfdcore_read (core_bfd, osec, &magic, sizeof (magic), &bfd_offset);
2456 if (magic != RECORD_FULL_FILE_MAGIC)
2457 error (_("Version mis-match or file format error in core file %s."),
2458 bfd_get_filename (core_bfd));
2460 fprintf_unfiltered (gdb_stdlog,
2461 " Reading 4-byte magic cookie "
2462 "RECORD_FULL_FILE_MAGIC (0x%s)\n",
2463 phex_nz (netorder32 (magic), 4));
2465 /* Restore the entries in recfd into record_full_arch_list_head and
2466 record_full_arch_list_tail. */
2467 record_full_arch_list_head = NULL;
2468 record_full_arch_list_tail = NULL;
2469 record_full_insn_num = 0;
2470 old_cleanups = make_cleanup (record_full_arch_list_cleanups, 0);
2471 regcache = get_current_regcache ();
2476 uint32_t regnum, len, signal, count;
2479 /* We are finished when offset reaches osec_size. */
2480 if (bfd_offset >= osec_size)
2482 bfdcore_read (core_bfd, osec, &rectype, sizeof (rectype), &bfd_offset);
2486 case record_full_reg: /* reg */
2487 /* Get register number to regnum. */
2488 bfdcore_read (core_bfd, osec, ®num,
2489 sizeof (regnum), &bfd_offset);
2490 regnum = netorder32 (regnum);
2492 rec = record_full_reg_alloc (regcache, regnum);
2495 bfdcore_read (core_bfd, osec, record_full_get_loc (rec),
2496 rec->u.reg.len, &bfd_offset);
2499 fprintf_unfiltered (gdb_stdlog,
2500 " Reading register %d (1 "
2501 "plus %lu plus %d bytes)\n",
2503 (unsigned long) sizeof (regnum),
2507 case record_full_mem: /* mem */
2509 bfdcore_read (core_bfd, osec, &len,
2510 sizeof (len), &bfd_offset);
2511 len = netorder32 (len);
2514 bfdcore_read (core_bfd, osec, &addr,
2515 sizeof (addr), &bfd_offset);
2516 addr = netorder64 (addr);
2518 rec = record_full_mem_alloc (addr, len);
2521 bfdcore_read (core_bfd, osec, record_full_get_loc (rec),
2522 rec->u.mem.len, &bfd_offset);
2525 fprintf_unfiltered (gdb_stdlog,
2526 " Reading memory %s (1 plus "
2527 "%lu plus %lu plus %d bytes)\n",
2528 paddress (get_current_arch (),
2530 (unsigned long) sizeof (addr),
2531 (unsigned long) sizeof (len),
2535 case record_full_end: /* end */
2536 rec = record_full_end_alloc ();
2537 record_full_insn_num ++;
2539 /* Get signal value. */
2540 bfdcore_read (core_bfd, osec, &signal,
2541 sizeof (signal), &bfd_offset);
2542 signal = netorder32 (signal);
2543 rec->u.end.sigval = signal;
2545 /* Get insn count. */
2546 bfdcore_read (core_bfd, osec, &count,
2547 sizeof (count), &bfd_offset);
2548 count = netorder32 (count);
2549 rec->u.end.insn_num = count;
2550 record_full_insn_count = count + 1;
2552 fprintf_unfiltered (gdb_stdlog,
2553 " Reading record_full_end (1 + "
2554 "%lu + %lu bytes), offset == %s\n",
2555 (unsigned long) sizeof (signal),
2556 (unsigned long) sizeof (count),
2557 paddress (get_current_arch (),
2562 error (_("Bad entry type in core file %s."),
2563 bfd_get_filename (core_bfd));
2567 /* Add rec to record arch list. */
2568 record_full_arch_list_add (rec);
2571 discard_cleanups (old_cleanups);
2573 /* Add record_full_arch_list_head to the end of record list. */
2574 record_full_first.next = record_full_arch_list_head;
2575 record_full_arch_list_head->prev = &record_full_first;
2576 record_full_arch_list_tail->next = NULL;
2577 record_full_list = &record_full_first;
2579 /* Update record_full_insn_max_num. */
2580 if (record_full_insn_num > record_full_insn_max_num)
2582 record_full_insn_max_num = record_full_insn_num;
2583 warning (_("Auto increase record/replay buffer limit to %u."),
2584 record_full_insn_max_num);
2588 printf_filtered (_("Restored records from core file %s.\n"),
2589 bfd_get_filename (core_bfd));
2591 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
2594 /* bfdcore_write -- write bytes into a core file section. */
2597 bfdcore_write (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2599 int ret = bfd_set_section_contents (obfd, osec, buf, *offset, len);
2604 error (_("Failed to write %d bytes to core file %s ('%s')."),
2605 len, bfd_get_filename (obfd),
2606 bfd_errmsg (bfd_get_error ()));
2609 /* Restore the execution log from a file. We use a modified elf
2610 corefile format, with an extra section for our data. */
2613 cmd_record_full_restore (char *args, int from_tty)
2615 core_file_command (args, from_tty);
2616 record_full_open (args, from_tty);
2620 record_full_save_cleanups (void *data)
2623 char *pathname = xstrdup (bfd_get_filename (obfd));
2625 gdb_bfd_unref (obfd);
2630 /* Save the execution log to a file. We use a modified elf corefile
2631 format, with an extra section for our data. */
2634 record_full_save (const char *recfilename)
2636 struct record_full_entry *cur_record_full_list;
2638 struct regcache *regcache;
2639 struct gdbarch *gdbarch;
2640 struct cleanup *old_cleanups;
2641 struct cleanup *set_cleanups;
2644 asection *osec = NULL;
2647 /* Open the save file. */
2649 fprintf_unfiltered (gdb_stdlog, "Saving execution log to core file '%s'\n",
2652 /* Open the output file. */
2653 obfd = create_gcore_bfd (recfilename);
2654 old_cleanups = make_cleanup (record_full_save_cleanups, obfd);
2656 /* Save the current record entry to "cur_record_full_list". */
2657 cur_record_full_list = record_full_list;
2659 /* Get the values of regcache and gdbarch. */
2660 regcache = get_current_regcache ();
2661 gdbarch = get_regcache_arch (regcache);
2663 /* Disable the GDB operation record. */
2664 set_cleanups = record_full_gdb_operation_disable_set ();
2666 /* Reverse execute to the begin of record list. */
2669 /* Check for beginning and end of log. */
2670 if (record_full_list == &record_full_first)
2673 record_full_exec_insn (regcache, gdbarch, record_full_list);
2675 if (record_full_list->prev)
2676 record_full_list = record_full_list->prev;
2679 /* Compute the size needed for the extra bfd section. */
2680 save_size = 4; /* magic cookie */
2681 for (record_full_list = record_full_first.next; record_full_list;
2682 record_full_list = record_full_list->next)
2683 switch (record_full_list->type)
2685 case record_full_end:
2686 save_size += 1 + 4 + 4;
2688 case record_full_reg:
2689 save_size += 1 + 4 + record_full_list->u.reg.len;
2691 case record_full_mem:
2692 save_size += 1 + 4 + 8 + record_full_list->u.mem.len;
2696 /* Make the new bfd section. */
2697 osec = bfd_make_section_anyway_with_flags (obfd, "precord",
2701 error (_("Failed to create 'precord' section for corefile %s: %s"),
2703 bfd_errmsg (bfd_get_error ()));
2704 bfd_set_section_size (obfd, osec, save_size);
2705 bfd_set_section_vma (obfd, osec, 0);
2706 bfd_set_section_alignment (obfd, osec, 0);
2707 bfd_section_lma (obfd, osec) = 0;
2709 /* Save corefile state. */
2710 write_gcore_file (obfd);
2712 /* Write out the record log. */
2713 /* Write the magic code. */
2714 magic = RECORD_FULL_FILE_MAGIC;
2716 fprintf_unfiltered (gdb_stdlog,
2717 " Writing 4-byte magic cookie "
2718 "RECORD_FULL_FILE_MAGIC (0x%s)\n",
2719 phex_nz (magic, 4));
2720 bfdcore_write (obfd, osec, &magic, sizeof (magic), &bfd_offset);
2722 /* Save the entries to recfd and forward execute to the end of
2724 record_full_list = &record_full_first;
2728 if (record_full_list != &record_full_first)
2731 uint32_t regnum, len, signal, count;
2734 type = record_full_list->type;
2735 bfdcore_write (obfd, osec, &type, sizeof (type), &bfd_offset);
2737 switch (record_full_list->type)
2739 case record_full_reg: /* reg */
2741 fprintf_unfiltered (gdb_stdlog,
2742 " Writing register %d (1 "
2743 "plus %lu plus %d bytes)\n",
2744 record_full_list->u.reg.num,
2745 (unsigned long) sizeof (regnum),
2746 record_full_list->u.reg.len);
2749 regnum = netorder32 (record_full_list->u.reg.num);
2750 bfdcore_write (obfd, osec, ®num,
2751 sizeof (regnum), &bfd_offset);
2754 bfdcore_write (obfd, osec,
2755 record_full_get_loc (record_full_list),
2756 record_full_list->u.reg.len, &bfd_offset);
2759 case record_full_mem: /* mem */
2761 fprintf_unfiltered (gdb_stdlog,
2762 " Writing memory %s (1 plus "
2763 "%lu plus %lu plus %d bytes)\n",
2765 record_full_list->u.mem.addr),
2766 (unsigned long) sizeof (addr),
2767 (unsigned long) sizeof (len),
2768 record_full_list->u.mem.len);
2771 len = netorder32 (record_full_list->u.mem.len);
2772 bfdcore_write (obfd, osec, &len, sizeof (len), &bfd_offset);
2774 /* Write memaddr. */
2775 addr = netorder64 (record_full_list->u.mem.addr);
2776 bfdcore_write (obfd, osec, &addr,
2777 sizeof (addr), &bfd_offset);
2780 bfdcore_write (obfd, osec,
2781 record_full_get_loc (record_full_list),
2782 record_full_list->u.mem.len, &bfd_offset);
2785 case record_full_end:
2787 fprintf_unfiltered (gdb_stdlog,
2788 " Writing record_full_end (1 + "
2789 "%lu + %lu bytes)\n",
2790 (unsigned long) sizeof (signal),
2791 (unsigned long) sizeof (count));
2792 /* Write signal value. */
2793 signal = netorder32 (record_full_list->u.end.sigval);
2794 bfdcore_write (obfd, osec, &signal,
2795 sizeof (signal), &bfd_offset);
2797 /* Write insn count. */
2798 count = netorder32 (record_full_list->u.end.insn_num);
2799 bfdcore_write (obfd, osec, &count,
2800 sizeof (count), &bfd_offset);
2805 /* Execute entry. */
2806 record_full_exec_insn (regcache, gdbarch, record_full_list);
2808 if (record_full_list->next)
2809 record_full_list = record_full_list->next;
2814 /* Reverse execute to cur_record_full_list. */
2817 /* Check for beginning and end of log. */
2818 if (record_full_list == cur_record_full_list)
2821 record_full_exec_insn (regcache, gdbarch, record_full_list);
2823 if (record_full_list->prev)
2824 record_full_list = record_full_list->prev;
2827 do_cleanups (set_cleanups);
2828 gdb_bfd_unref (obfd);
2829 discard_cleanups (old_cleanups);
2832 printf_filtered (_("Saved core file %s with execution log.\n"),
2836 /* record_full_goto_insn -- rewind the record log (forward or backward,
2837 depending on DIR) to the given entry, changing the program state
2841 record_full_goto_insn (struct record_full_entry *entry,
2842 enum exec_direction_kind dir)
2844 struct cleanup *set_cleanups = record_full_gdb_operation_disable_set ();
2845 struct regcache *regcache = get_current_regcache ();
2846 struct gdbarch *gdbarch = get_regcache_arch (regcache);
2848 /* Assume everything is valid: we will hit the entry,
2849 and we will not hit the end of the recording. */
2851 if (dir == EXEC_FORWARD)
2852 record_full_list = record_full_list->next;
2856 record_full_exec_insn (regcache, gdbarch, record_full_list);
2857 if (dir == EXEC_REVERSE)
2858 record_full_list = record_full_list->prev;
2860 record_full_list = record_full_list->next;
2861 } while (record_full_list != entry);
2862 do_cleanups (set_cleanups);
2865 /* Alias for "target record-full". */
2868 cmd_record_full_start (char *args, int from_tty)
2870 execute_command ("target record-full", from_tty);
2874 set_record_full_insn_max_num (char *args, int from_tty,
2875 struct cmd_list_element *c)
2877 if (record_full_insn_num > record_full_insn_max_num)
2879 /* Count down record_full_insn_num while releasing records from list. */
2880 while (record_full_insn_num > record_full_insn_max_num)
2882 record_full_list_release_first ();
2883 record_full_insn_num--;
2888 /* The "set record full" command. */
2891 set_record_full_command (char *args, int from_tty)
2893 printf_unfiltered (_("\"set record full\" must be followed "
2894 "by an apporpriate subcommand.\n"));
2895 help_list (set_record_full_cmdlist, "set record full ", all_commands,
2899 /* The "show record full" command. */
2902 show_record_full_command (char *args, int from_tty)
2904 cmd_show_list (show_record_full_cmdlist, from_tty, "");
2907 /* Provide a prototype to silence -Wmissing-prototypes. */
2908 extern initialize_file_ftype _initialize_record_full;
2911 _initialize_record_full (void)
2913 struct cmd_list_element *c;
2915 /* Init record_full_first. */
2916 record_full_first.prev = NULL;
2917 record_full_first.next = NULL;
2918 record_full_first.type = record_full_end;
2920 init_record_full_ops ();
2921 add_target (&record_full_ops);
2922 add_deprecated_target_alias (&record_full_ops, "record");
2923 init_record_full_core_ops ();
2924 add_target (&record_full_core_ops);
2926 add_prefix_cmd ("full", class_obscure, cmd_record_full_start,
2927 _("Start full execution recording."), &record_full_cmdlist,
2928 "record full ", 0, &record_cmdlist);
2930 c = add_cmd ("restore", class_obscure, cmd_record_full_restore,
2931 _("Restore the execution log from a file.\n\
2932 Argument is filename. File must be created with 'record save'."),
2933 &record_full_cmdlist);
2934 set_cmd_completer (c, filename_completer);
2936 /* Deprecate the old version without "full" prefix. */
2937 c = add_alias_cmd ("restore", "full restore", class_obscure, 1,
2939 set_cmd_completer (c, filename_completer);
2940 deprecate_cmd (c, "record full restore");
2942 add_prefix_cmd ("full", class_support, set_record_full_command,
2943 _("Set record options"), &set_record_full_cmdlist,
2944 "set record full ", 0, &set_record_cmdlist);
2946 add_prefix_cmd ("full", class_support, show_record_full_command,
2947 _("Show record options"), &show_record_full_cmdlist,
2948 "show record full ", 0, &show_record_cmdlist);
2950 /* Record instructions number limit command. */
2951 add_setshow_boolean_cmd ("stop-at-limit", no_class,
2952 &record_full_stop_at_limit, _("\
2953 Set whether record/replay stops when record/replay buffer becomes full."), _("\
2954 Show whether record/replay stops when record/replay buffer becomes full."),
2955 _("Default is ON.\n\
2956 When ON, if the record/replay buffer becomes full, ask user what to do.\n\
2957 When OFF, if the record/replay buffer becomes full,\n\
2958 delete the oldest recorded instruction to make room for each new one."),
2960 &set_record_full_cmdlist, &show_record_full_cmdlist);
2962 c = add_alias_cmd ("stop-at-limit", "full stop-at-limit", no_class, 1,
2963 &set_record_cmdlist);
2964 deprecate_cmd (c, "set record full stop-at-limit");
2966 c = add_alias_cmd ("stop-at-limit", "full stop-at-limit", no_class, 1,
2967 &show_record_cmdlist);
2968 deprecate_cmd (c, "show record full stop-at-limit");
2970 add_setshow_uinteger_cmd ("insn-number-max", no_class,
2971 &record_full_insn_max_num,
2972 _("Set record/replay buffer limit."),
2973 _("Show record/replay buffer limit."), _("\
2974 Set the maximum number of instructions to be stored in the\n\
2975 record/replay buffer. A value of either \"unlimited\" or zero means no\n\
2976 limit. Default is 200000."),
2977 set_record_full_insn_max_num,
2978 NULL, &set_record_full_cmdlist,
2979 &show_record_full_cmdlist);
2981 c = add_alias_cmd ("insn-number-max", "full insn-number-max", no_class, 1,
2982 &set_record_cmdlist);
2983 deprecate_cmd (c, "set record full insn-number-max");
2985 c = add_alias_cmd ("insn-number-max", "full insn-number-max", no_class, 1,
2986 &show_record_cmdlist);
2987 deprecate_cmd (c, "show record full insn-number-max");
2989 add_setshow_boolean_cmd ("memory-query", no_class,
2990 &record_full_memory_query, _("\
2991 Set whether query if PREC cannot record memory change of next instruction."),
2993 Show whether query if PREC cannot record memory change of next instruction."),
2996 When ON, query if PREC cannot record memory change of next instruction."),
2998 &set_record_full_cmdlist,
2999 &show_record_full_cmdlist);
3001 c = add_alias_cmd ("memory-query", "full memory-query", no_class, 1,
3002 &set_record_cmdlist);
3003 deprecate_cmd (c, "set record full memory-query");
3005 c = add_alias_cmd ("memory-query", "full memory-query", no_class, 1,
3006 &show_record_cmdlist);
3007 deprecate_cmd (c, "show record full memory-query");