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 target_ops
*,
248 struct bp_target_info
*);
249 static struct target_ops
*record_full_beneath_to_insert_breakpoint_ops
;
251 (*record_full_beneath_to_remove_breakpoint
) (struct target_ops
*,
253 struct bp_target_info
*);
254 static struct target_ops
*record_full_beneath_to_remove_breakpoint_ops
;
255 static int (*record_full_beneath_to_stopped_by_watchpoint
) (struct target_ops
*);
256 static int (*record_full_beneath_to_stopped_data_address
) (struct target_ops
*,
259 (*record_full_beneath_to_async
) (struct target_ops
*,
260 void (*) (enum inferior_event_type
, void *),
263 static void record_full_goto_insn (struct record_full_entry
*entry
,
264 enum exec_direction_kind dir
);
265 static void record_full_save (const char *recfilename
);
267 /* Alloc and free functions for record_full_reg, record_full_mem, and
268 record_full_end entries. */
270 /* Alloc a record_full_reg record entry. */
272 static inline struct record_full_entry
*
273 record_full_reg_alloc (struct regcache
*regcache
, int regnum
)
275 struct record_full_entry
*rec
;
276 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
278 rec
= xcalloc (1, sizeof (struct record_full_entry
));
279 rec
->type
= record_full_reg
;
280 rec
->u
.reg
.num
= regnum
;
281 rec
->u
.reg
.len
= register_size (gdbarch
, regnum
);
282 if (rec
->u
.reg
.len
> sizeof (rec
->u
.reg
.u
.buf
))
283 rec
->u
.reg
.u
.ptr
= (gdb_byte
*) xmalloc (rec
->u
.reg
.len
);
288 /* Free a record_full_reg record entry. */
291 record_full_reg_release (struct record_full_entry
*rec
)
293 gdb_assert (rec
->type
== record_full_reg
);
294 if (rec
->u
.reg
.len
> sizeof (rec
->u
.reg
.u
.buf
))
295 xfree (rec
->u
.reg
.u
.ptr
);
299 /* Alloc a record_full_mem record entry. */
301 static inline struct record_full_entry
*
302 record_full_mem_alloc (CORE_ADDR addr
, int len
)
304 struct record_full_entry
*rec
;
306 rec
= xcalloc (1, sizeof (struct record_full_entry
));
307 rec
->type
= record_full_mem
;
308 rec
->u
.mem
.addr
= addr
;
309 rec
->u
.mem
.len
= len
;
310 if (rec
->u
.mem
.len
> sizeof (rec
->u
.mem
.u
.buf
))
311 rec
->u
.mem
.u
.ptr
= (gdb_byte
*) xmalloc (len
);
316 /* Free a record_full_mem record entry. */
319 record_full_mem_release (struct record_full_entry
*rec
)
321 gdb_assert (rec
->type
== record_full_mem
);
322 if (rec
->u
.mem
.len
> sizeof (rec
->u
.mem
.u
.buf
))
323 xfree (rec
->u
.mem
.u
.ptr
);
327 /* Alloc a record_full_end record entry. */
329 static inline struct record_full_entry
*
330 record_full_end_alloc (void)
332 struct record_full_entry
*rec
;
334 rec
= xcalloc (1, sizeof (struct record_full_entry
));
335 rec
->type
= record_full_end
;
340 /* Free a record_full_end record entry. */
343 record_full_end_release (struct record_full_entry
*rec
)
348 /* Free one record entry, any type.
349 Return entry->type, in case caller wants to know. */
351 static inline enum record_full_type
352 record_full_entry_release (struct record_full_entry
*rec
)
354 enum record_full_type type
= rec
->type
;
357 case record_full_reg
:
358 record_full_reg_release (rec
);
360 case record_full_mem
:
361 record_full_mem_release (rec
);
363 case record_full_end
:
364 record_full_end_release (rec
);
370 /* Free all record entries in list pointed to by REC. */
373 record_full_list_release (struct record_full_entry
*rec
)
384 record_full_entry_release (rec
->next
);
387 if (rec
== &record_full_first
)
389 record_full_insn_num
= 0;
390 record_full_first
.next
= NULL
;
393 record_full_entry_release (rec
);
396 /* Free all record entries forward of the given list position. */
399 record_full_list_release_following (struct record_full_entry
*rec
)
401 struct record_full_entry
*tmp
= rec
->next
;
407 if (record_full_entry_release (tmp
) == record_full_end
)
409 record_full_insn_num
--;
410 record_full_insn_count
--;
416 /* Delete the first instruction from the beginning of the log, to make
417 room for adding a new instruction at the end of the log.
419 Note -- this function does not modify record_full_insn_num. */
422 record_full_list_release_first (void)
424 struct record_full_entry
*tmp
;
426 if (!record_full_first
.next
)
429 /* Loop until a record_full_end. */
432 /* Cut record_full_first.next out of the linked list. */
433 tmp
= record_full_first
.next
;
434 record_full_first
.next
= tmp
->next
;
435 tmp
->next
->prev
= &record_full_first
;
437 /* tmp is now isolated, and can be deleted. */
438 if (record_full_entry_release (tmp
) == record_full_end
)
439 break; /* End loop at first record_full_end. */
441 if (!record_full_first
.next
)
443 gdb_assert (record_full_insn_num
== 1);
444 break; /* End loop when list is empty. */
449 /* Add a struct record_full_entry to record_full_arch_list. */
452 record_full_arch_list_add (struct record_full_entry
*rec
)
454 if (record_debug
> 1)
455 fprintf_unfiltered (gdb_stdlog
,
456 "Process record: record_full_arch_list_add %s.\n",
457 host_address_to_string (rec
));
459 if (record_full_arch_list_tail
)
461 record_full_arch_list_tail
->next
= rec
;
462 rec
->prev
= record_full_arch_list_tail
;
463 record_full_arch_list_tail
= rec
;
467 record_full_arch_list_head
= rec
;
468 record_full_arch_list_tail
= rec
;
472 /* Return the value storage location of a record entry. */
473 static inline gdb_byte
*
474 record_full_get_loc (struct record_full_entry
*rec
)
477 case record_full_mem
:
478 if (rec
->u
.mem
.len
> sizeof (rec
->u
.mem
.u
.buf
))
479 return rec
->u
.mem
.u
.ptr
;
481 return rec
->u
.mem
.u
.buf
;
482 case record_full_reg
:
483 if (rec
->u
.reg
.len
> sizeof (rec
->u
.reg
.u
.buf
))
484 return rec
->u
.reg
.u
.ptr
;
486 return rec
->u
.reg
.u
.buf
;
487 case record_full_end
:
489 gdb_assert_not_reached ("unexpected record_full_entry type");
494 /* Record the value of a register NUM to record_full_arch_list. */
497 record_full_arch_list_add_reg (struct regcache
*regcache
, int regnum
)
499 struct record_full_entry
*rec
;
501 if (record_debug
> 1)
502 fprintf_unfiltered (gdb_stdlog
,
503 "Process record: add register num = %d to "
507 rec
= record_full_reg_alloc (regcache
, regnum
);
509 regcache_raw_read (regcache
, regnum
, record_full_get_loc (rec
));
511 record_full_arch_list_add (rec
);
516 /* Record the value of a region of memory whose address is ADDR and
517 length is LEN to record_full_arch_list. */
520 record_full_arch_list_add_mem (CORE_ADDR addr
, int len
)
522 struct record_full_entry
*rec
;
524 if (record_debug
> 1)
525 fprintf_unfiltered (gdb_stdlog
,
526 "Process record: add mem addr = %s len = %d to "
528 paddress (target_gdbarch (), addr
), len
);
530 if (!addr
) /* FIXME: Why? Some arch must permit it... */
533 rec
= record_full_mem_alloc (addr
, len
);
535 if (record_read_memory (target_gdbarch (), addr
,
536 record_full_get_loc (rec
), len
))
538 record_full_mem_release (rec
);
542 record_full_arch_list_add (rec
);
547 /* Add a record_full_end type struct record_full_entry to
548 record_full_arch_list. */
551 record_full_arch_list_add_end (void)
553 struct record_full_entry
*rec
;
555 if (record_debug
> 1)
556 fprintf_unfiltered (gdb_stdlog
,
557 "Process record: add end to arch list.\n");
559 rec
= record_full_end_alloc ();
560 rec
->u
.end
.sigval
= GDB_SIGNAL_0
;
561 rec
->u
.end
.insn_num
= ++record_full_insn_count
;
563 record_full_arch_list_add (rec
);
569 record_full_check_insn_num (int set_terminal
)
571 if (record_full_insn_num
== record_full_insn_max_num
)
573 /* Ask user what to do. */
574 if (record_full_stop_at_limit
)
579 target_terminal_ours ();
580 q
= yquery (_("Do you want to auto delete previous execution "
581 "log entries when record/replay buffer becomes "
582 "full (record full stop-at-limit)?"));
584 target_terminal_inferior ();
586 record_full_stop_at_limit
= 0;
588 error (_("Process record: stopped by user."));
594 record_full_arch_list_cleanups (void *ignore
)
596 record_full_list_release (record_full_arch_list_tail
);
599 /* Before inferior step (when GDB record the running message, inferior
600 only can step), GDB will call this function to record the values to
601 record_full_list. This function will call gdbarch_process_record to
602 record the running message of inferior and set them to
603 record_full_arch_list, and add it to record_full_list. */
606 record_full_message (struct regcache
*regcache
, enum gdb_signal signal
)
609 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
610 struct cleanup
*old_cleanups
611 = make_cleanup (record_full_arch_list_cleanups
, 0);
613 record_full_arch_list_head
= NULL
;
614 record_full_arch_list_tail
= NULL
;
616 /* Check record_full_insn_num. */
617 record_full_check_insn_num (1);
619 /* If gdb sends a signal value to target_resume,
620 save it in the 'end' field of the previous instruction.
622 Maybe process record should record what really happened,
623 rather than what gdb pretends has happened.
625 So if Linux delivered the signal to the child process during
626 the record mode, we will record it and deliver it again in
629 If user says "ignore this signal" during the record mode, then
630 it will be ignored again during the replay mode (no matter if
631 the user says something different, like "deliver this signal"
632 during the replay mode).
634 User should understand that nothing he does during the replay
635 mode will change the behavior of the child. If he tries,
636 then that is a user error.
638 But we should still deliver the signal to gdb during the replay,
639 if we delivered it during the recording. Therefore we should
640 record the signal during record_full_wait, not
641 record_full_resume. */
642 if (record_full_list
!= &record_full_first
) /* FIXME better way to check */
644 gdb_assert (record_full_list
->type
== record_full_end
);
645 record_full_list
->u
.end
.sigval
= signal
;
648 if (signal
== GDB_SIGNAL_0
649 || !gdbarch_process_record_signal_p (gdbarch
))
650 ret
= gdbarch_process_record (gdbarch
,
652 regcache_read_pc (regcache
));
654 ret
= gdbarch_process_record_signal (gdbarch
,
659 error (_("Process record: inferior program stopped."));
661 error (_("Process record: failed to record execution log."));
663 discard_cleanups (old_cleanups
);
665 record_full_list
->next
= record_full_arch_list_head
;
666 record_full_arch_list_head
->prev
= record_full_list
;
667 record_full_list
= record_full_arch_list_tail
;
669 if (record_full_insn_num
== record_full_insn_max_num
)
670 record_full_list_release_first ();
672 record_full_insn_num
++;
677 struct record_full_message_args
{
678 struct regcache
*regcache
;
679 enum gdb_signal signal
;
683 record_full_message_wrapper (void *args
)
685 struct record_full_message_args
*record_full_args
= args
;
687 return record_full_message (record_full_args
->regcache
,
688 record_full_args
->signal
);
692 record_full_message_wrapper_safe (struct regcache
*regcache
,
693 enum gdb_signal signal
)
695 struct record_full_message_args args
;
697 args
.regcache
= regcache
;
698 args
.signal
= signal
;
700 return catch_errors (record_full_message_wrapper
, &args
, NULL
,
704 /* Set to 1 if record_full_store_registers and record_full_xfer_partial
705 doesn't need record. */
707 static int record_full_gdb_operation_disable
= 0;
710 record_full_gdb_operation_disable_set (void)
712 struct cleanup
*old_cleanups
= NULL
;
715 make_cleanup_restore_integer (&record_full_gdb_operation_disable
);
716 record_full_gdb_operation_disable
= 1;
721 /* Flag set to TRUE for target_stopped_by_watchpoint. */
722 static int record_full_hw_watchpoint
= 0;
724 /* Execute one instruction from the record log. Each instruction in
725 the log will be represented by an arbitrary sequence of register
726 entries and memory entries, followed by an 'end' entry. */
729 record_full_exec_insn (struct regcache
*regcache
,
730 struct gdbarch
*gdbarch
,
731 struct record_full_entry
*entry
)
735 case record_full_reg
: /* reg */
737 gdb_byte reg
[MAX_REGISTER_SIZE
];
739 if (record_debug
> 1)
740 fprintf_unfiltered (gdb_stdlog
,
741 "Process record: record_full_reg %s to "
742 "inferior num = %d.\n",
743 host_address_to_string (entry
),
746 regcache_cooked_read (regcache
, entry
->u
.reg
.num
, reg
);
747 regcache_cooked_write (regcache
, entry
->u
.reg
.num
,
748 record_full_get_loc (entry
));
749 memcpy (record_full_get_loc (entry
), reg
, entry
->u
.reg
.len
);
753 case record_full_mem
: /* mem */
755 /* Nothing to do if the entry is flagged not_accessible. */
756 if (!entry
->u
.mem
.mem_entry_not_accessible
)
758 gdb_byte
*mem
= alloca (entry
->u
.mem
.len
);
760 if (record_debug
> 1)
761 fprintf_unfiltered (gdb_stdlog
,
762 "Process record: record_full_mem %s to "
763 "inferior addr = %s len = %d.\n",
764 host_address_to_string (entry
),
765 paddress (gdbarch
, entry
->u
.mem
.addr
),
768 if (record_read_memory (gdbarch
,
769 entry
->u
.mem
.addr
, mem
, entry
->u
.mem
.len
))
770 entry
->u
.mem
.mem_entry_not_accessible
= 1;
773 if (target_write_memory (entry
->u
.mem
.addr
,
774 record_full_get_loc (entry
),
777 entry
->u
.mem
.mem_entry_not_accessible
= 1;
779 warning (_("Process record: error writing memory at "
780 "addr = %s len = %d."),
781 paddress (gdbarch
, entry
->u
.mem
.addr
),
786 memcpy (record_full_get_loc (entry
), mem
,
789 /* We've changed memory --- check if a hardware
790 watchpoint should trap. Note that this
791 presently assumes the target beneath supports
792 continuable watchpoints. On non-continuable
793 watchpoints target, we'll want to check this
794 _before_ actually doing the memory change, and
795 not doing the change at all if the watchpoint
797 if (hardware_watchpoint_inserted_in_range
798 (get_regcache_aspace (regcache
),
799 entry
->u
.mem
.addr
, entry
->u
.mem
.len
))
800 record_full_hw_watchpoint
= 1;
809 static struct target_ops
*tmp_to_resume_ops
;
810 static void (*tmp_to_resume
) (struct target_ops
*, ptid_t
, int,
812 static struct target_ops
*tmp_to_wait_ops
;
813 static ptid_t (*tmp_to_wait
) (struct target_ops
*, ptid_t
,
814 struct target_waitstatus
*,
816 static struct target_ops
*tmp_to_store_registers_ops
;
817 static void (*tmp_to_store_registers
) (struct target_ops
*,
820 static struct target_ops
*tmp_to_xfer_partial_ops
;
821 static target_xfer_partial_ftype
*tmp_to_xfer_partial
;
822 static int (*tmp_to_insert_breakpoint
) (struct target_ops
*, struct gdbarch
*,
823 struct bp_target_info
*);
824 static struct target_ops
*tmp_to_insert_breakpoint_ops
;
825 static int (*tmp_to_remove_breakpoint
) (struct target_ops
*, struct gdbarch
*,
826 struct bp_target_info
*);
827 static struct target_ops
*tmp_to_remove_breakpoint_ops
;
828 static int (*tmp_to_stopped_by_watchpoint
) (struct target_ops
*);
829 static int (*tmp_to_stopped_data_address
) (struct target_ops
*, CORE_ADDR
*);
830 static void (*tmp_to_async
) (struct target_ops
*,
831 void (*) (enum inferior_event_type
, void *), void *);
833 static void record_full_restore (void);
835 /* Asynchronous signal handle registered as event loop source for when
836 we have pending events ready to be passed to the core. */
838 static struct async_event_handler
*record_full_async_inferior_event_token
;
841 record_full_async_inferior_event_handler (gdb_client_data data
)
843 inferior_event_handler (INF_REG_EVENT
, NULL
);
846 /* Open the process record target. */
849 record_full_core_open_1 (char *name
, int from_tty
)
851 struct regcache
*regcache
= get_current_regcache ();
852 int regnum
= gdbarch_num_regs (get_regcache_arch (regcache
));
855 /* Get record_full_core_regbuf. */
856 target_fetch_registers (regcache
, -1);
857 record_full_core_regbuf
= xmalloc (MAX_REGISTER_SIZE
* regnum
);
858 for (i
= 0; i
< regnum
; i
++)
859 regcache_raw_collect (regcache
, i
,
860 record_full_core_regbuf
+ MAX_REGISTER_SIZE
* i
);
862 /* Get record_full_core_start and record_full_core_end. */
863 if (build_section_table (core_bfd
, &record_full_core_start
,
864 &record_full_core_end
))
866 xfree (record_full_core_regbuf
);
867 record_full_core_regbuf
= NULL
;
868 error (_("\"%s\": Can't find sections: %s"),
869 bfd_get_filename (core_bfd
), bfd_errmsg (bfd_get_error ()));
872 push_target (&record_full_core_ops
);
873 record_full_restore ();
876 /* "to_open" target method for 'live' processes. */
879 record_full_open_1 (char *name
, int from_tty
)
882 fprintf_unfiltered (gdb_stdlog
, "Process record: record_full_open\n");
885 if (!target_has_execution
)
886 error (_("Process record: the program is not being run."));
888 error (_("Process record target can't debug inferior in non-stop mode "
891 if (!gdbarch_process_record_p (target_gdbarch ()))
892 error (_("Process record: the current architecture doesn't support "
893 "record function."));
896 error (_("Could not find 'to_resume' method on the target stack."));
898 error (_("Could not find 'to_wait' method on the target stack."));
899 if (!tmp_to_store_registers
)
900 error (_("Could not find 'to_store_registers' "
901 "method on the target stack."));
902 if (!tmp_to_insert_breakpoint
)
903 error (_("Could not find 'to_insert_breakpoint' "
904 "method on the target stack."));
905 if (!tmp_to_remove_breakpoint
)
906 error (_("Could not find 'to_remove_breakpoint' "
907 "method on the target stack."));
908 if (!tmp_to_stopped_by_watchpoint
)
909 error (_("Could not find 'to_stopped_by_watchpoint' "
910 "method on the target stack."));
911 if (!tmp_to_stopped_data_address
)
912 error (_("Could not find 'to_stopped_data_address' "
913 "method on the target stack."));
915 push_target (&record_full_ops
);
918 static void record_full_init_record_breakpoints (void);
920 /* "to_open" target method. Open the process record target. */
923 record_full_open (char *name
, int from_tty
)
925 struct target_ops
*t
;
928 fprintf_unfiltered (gdb_stdlog
, "Process record: record_full_open\n");
932 /* Reset the tmp beneath pointers. */
933 tmp_to_resume_ops
= NULL
;
934 tmp_to_resume
= NULL
;
935 tmp_to_wait_ops
= NULL
;
937 tmp_to_store_registers_ops
= NULL
;
938 tmp_to_store_registers
= NULL
;
939 tmp_to_xfer_partial_ops
= NULL
;
940 tmp_to_xfer_partial
= NULL
;
941 tmp_to_insert_breakpoint
= NULL
;
942 tmp_to_remove_breakpoint
= NULL
;
943 tmp_to_stopped_by_watchpoint
= NULL
;
944 tmp_to_stopped_data_address
= NULL
;
946 tmp_to_insert_breakpoint_ops
= NULL
;
947 tmp_to_remove_breakpoint_ops
= NULL
;
949 /* Set the beneath function pointers. */
950 for (t
= current_target
.beneath
; t
!= NULL
; t
= t
->beneath
)
954 tmp_to_resume
= t
->to_resume
;
955 tmp_to_resume_ops
= t
;
959 tmp_to_wait
= t
->to_wait
;
962 if (!tmp_to_store_registers
)
964 tmp_to_store_registers
= t
->to_store_registers
;
965 tmp_to_store_registers_ops
= t
;
967 if (!tmp_to_xfer_partial
)
969 tmp_to_xfer_partial
= t
->to_xfer_partial
;
970 tmp_to_xfer_partial_ops
= t
;
972 if (!tmp_to_insert_breakpoint
)
974 tmp_to_insert_breakpoint
= t
->to_insert_breakpoint
;
975 tmp_to_insert_breakpoint_ops
= t
;
977 if (!tmp_to_remove_breakpoint
)
979 tmp_to_remove_breakpoint
= t
->to_remove_breakpoint
;
980 tmp_to_remove_breakpoint_ops
= t
;
982 if (!tmp_to_stopped_by_watchpoint
)
983 tmp_to_stopped_by_watchpoint
= t
->to_stopped_by_watchpoint
;
984 if (!tmp_to_stopped_data_address
)
985 tmp_to_stopped_data_address
= t
->to_stopped_data_address
;
987 tmp_to_async
= t
->to_async
;
989 if (!tmp_to_xfer_partial
)
990 error (_("Could not find 'to_xfer_partial' method on the target stack."));
993 record_full_insn_num
= 0;
994 record_full_insn_count
= 0;
995 record_full_list
= &record_full_first
;
996 record_full_list
->next
= NULL
;
998 /* Set the tmp beneath pointers to beneath pointers. */
999 record_full_beneath_to_resume_ops
= tmp_to_resume_ops
;
1000 record_full_beneath_to_resume
= tmp_to_resume
;
1001 record_full_beneath_to_wait_ops
= tmp_to_wait_ops
;
1002 record_full_beneath_to_wait
= tmp_to_wait
;
1003 record_full_beneath_to_store_registers_ops
= tmp_to_store_registers_ops
;
1004 record_full_beneath_to_store_registers
= tmp_to_store_registers
;
1005 record_full_beneath_to_xfer_partial_ops
= tmp_to_xfer_partial_ops
;
1006 record_full_beneath_to_xfer_partial
= tmp_to_xfer_partial
;
1007 record_full_beneath_to_insert_breakpoint
= tmp_to_insert_breakpoint
;
1008 record_full_beneath_to_insert_breakpoint_ops
= tmp_to_insert_breakpoint_ops
;
1009 record_full_beneath_to_remove_breakpoint
= tmp_to_remove_breakpoint
;
1010 record_full_beneath_to_remove_breakpoint_ops
= tmp_to_remove_breakpoint_ops
;
1011 record_full_beneath_to_stopped_by_watchpoint
= tmp_to_stopped_by_watchpoint
;
1012 record_full_beneath_to_stopped_data_address
= tmp_to_stopped_data_address
;
1013 record_full_beneath_to_async
= tmp_to_async
;
1016 record_full_core_open_1 (name
, from_tty
);
1018 record_full_open_1 (name
, from_tty
);
1020 /* Register extra event sources in the event loop. */
1021 record_full_async_inferior_event_token
1022 = create_async_event_handler (record_full_async_inferior_event_handler
,
1025 record_full_init_record_breakpoints ();
1027 observer_notify_record_changed (current_inferior (), 1);
1030 /* "to_close" target method. Close the process record target. */
1033 record_full_close (void)
1035 struct record_full_core_buf_entry
*entry
;
1038 fprintf_unfiltered (gdb_stdlog
, "Process record: record_full_close\n");
1040 record_full_list_release (record_full_list
);
1042 /* Release record_full_core_regbuf. */
1043 if (record_full_core_regbuf
)
1045 xfree (record_full_core_regbuf
);
1046 record_full_core_regbuf
= NULL
;
1049 /* Release record_full_core_buf_list. */
1050 if (record_full_core_buf_list
)
1052 for (entry
= record_full_core_buf_list
->prev
; entry
;
1053 entry
= entry
->prev
)
1055 xfree (record_full_core_buf_list
);
1056 record_full_core_buf_list
= entry
;
1058 record_full_core_buf_list
= NULL
;
1061 if (record_full_async_inferior_event_token
)
1062 delete_async_event_handler (&record_full_async_inferior_event_token
);
1065 static int record_full_resume_step
= 0;
1067 /* True if we've been resumed, and so each record_full_wait call should
1068 advance execution. If this is false, record_full_wait will return a
1069 TARGET_WAITKIND_IGNORE. */
1070 static int record_full_resumed
= 0;
1072 /* The execution direction of the last resume we got. This is
1073 necessary for async mode. Vis (order is not strictly accurate):
1075 1. user has the global execution direction set to forward
1076 2. user does a reverse-step command
1077 3. record_full_resume is called with global execution direction
1078 temporarily switched to reverse
1079 4. GDB's execution direction is reverted back to forward
1080 5. target record notifies event loop there's an event to handle
1081 6. infrun asks the target which direction was it going, and switches
1082 the global execution direction accordingly (to reverse)
1083 7. infrun polls an event out of the record target, and handles it
1084 8. GDB goes back to the event loop, and goto #4.
1086 static enum exec_direction_kind record_full_execution_dir
= EXEC_FORWARD
;
1088 /* "to_resume" target method. Resume the process record target. */
1091 record_full_resume (struct target_ops
*ops
, ptid_t ptid
, int step
,
1092 enum gdb_signal signal
)
1094 record_full_resume_step
= step
;
1095 record_full_resumed
= 1;
1096 record_full_execution_dir
= execution_direction
;
1098 if (!RECORD_FULL_IS_REPLAY
)
1100 struct gdbarch
*gdbarch
= target_thread_architecture (ptid
);
1102 record_full_message (get_current_regcache (), signal
);
1106 /* This is not hard single step. */
1107 if (!gdbarch_software_single_step_p (gdbarch
))
1109 /* This is a normal continue. */
1114 /* This arch support soft sigle step. */
1115 if (single_step_breakpoints_inserted ())
1117 /* This is a soft single step. */
1118 record_full_resume_step
= 1;
1122 /* This is a continue.
1123 Try to insert a soft single step breakpoint. */
1124 if (!gdbarch_software_single_step (gdbarch
,
1125 get_current_frame ()))
1127 /* This system don't want use soft single step.
1128 Use hard sigle step. */
1135 /* Make sure the target beneath reports all signals. */
1136 target_pass_signals (0, NULL
);
1138 record_full_beneath_to_resume (record_full_beneath_to_resume_ops
,
1139 ptid
, step
, signal
);
1142 /* We are about to start executing the inferior (or simulate it),
1143 let's register it with the event loop. */
1144 if (target_can_async_p ())
1146 target_async (inferior_event_handler
, 0);
1147 /* Notify the event loop there's an event to wait for. We do
1148 most of the work in record_full_wait. */
1149 mark_async_event_handler (record_full_async_inferior_event_token
);
1153 static int record_full_get_sig
= 0;
1155 /* SIGINT signal handler, registered by "to_wait" method. */
1158 record_full_sig_handler (int signo
)
1161 fprintf_unfiltered (gdb_stdlog
, "Process record: get a signal\n");
1163 /* It will break the running inferior in replay mode. */
1164 record_full_resume_step
= 1;
1166 /* It will let record_full_wait set inferior status to get the signal
1168 record_full_get_sig
= 1;
1172 record_full_wait_cleanups (void *ignore
)
1174 if (execution_direction
== EXEC_REVERSE
)
1176 if (record_full_list
->next
)
1177 record_full_list
= record_full_list
->next
;
1180 record_full_list
= record_full_list
->prev
;
1183 /* "to_wait" target method for process record target.
1185 In record mode, the target is always run in singlestep mode
1186 (even when gdb says to continue). The to_wait method intercepts
1187 the stop events and determines which ones are to be passed on to
1188 gdb. Most stop events are just singlestep events that gdb is not
1189 to know about, so the to_wait method just records them and keeps
1192 In replay mode, this function emulates the recorded execution log,
1193 one instruction at a time (forward or backward), and determines
1197 record_full_wait_1 (struct target_ops
*ops
,
1198 ptid_t ptid
, struct target_waitstatus
*status
,
1201 struct cleanup
*set_cleanups
= record_full_gdb_operation_disable_set ();
1204 fprintf_unfiltered (gdb_stdlog
,
1205 "Process record: record_full_wait "
1206 "record_full_resume_step = %d, "
1207 "record_full_resumed = %d, direction=%s\n",
1208 record_full_resume_step
, record_full_resumed
,
1209 record_full_execution_dir
== EXEC_FORWARD
1210 ? "forward" : "reverse");
1212 if (!record_full_resumed
)
1214 gdb_assert ((options
& TARGET_WNOHANG
) != 0);
1216 /* No interesting event. */
1217 status
->kind
= TARGET_WAITKIND_IGNORE
;
1218 return minus_one_ptid
;
1221 record_full_get_sig
= 0;
1222 signal (SIGINT
, record_full_sig_handler
);
1224 if (!RECORD_FULL_IS_REPLAY
&& ops
!= &record_full_core_ops
)
1226 if (record_full_resume_step
)
1228 /* This is a single step. */
1229 return record_full_beneath_to_wait (record_full_beneath_to_wait_ops
,
1230 ptid
, status
, options
);
1234 /* This is not a single step. */
1237 struct gdbarch
*gdbarch
= target_thread_architecture (inferior_ptid
);
1241 ret
= record_full_beneath_to_wait
1242 (record_full_beneath_to_wait_ops
, ptid
, status
, options
);
1243 if (status
->kind
== TARGET_WAITKIND_IGNORE
)
1246 fprintf_unfiltered (gdb_stdlog
,
1247 "Process record: record_full_wait "
1248 "target beneath not done yet\n");
1252 if (single_step_breakpoints_inserted ())
1253 remove_single_step_breakpoints ();
1255 if (record_full_resume_step
)
1258 /* Is this a SIGTRAP? */
1259 if (status
->kind
== TARGET_WAITKIND_STOPPED
1260 && status
->value
.sig
== GDB_SIGNAL_TRAP
)
1262 struct regcache
*regcache
;
1263 struct address_space
*aspace
;
1265 /* Yes -- this is likely our single-step finishing,
1266 but check if there's any reason the core would be
1267 interested in the event. */
1269 registers_changed ();
1270 regcache
= get_current_regcache ();
1271 tmp_pc
= regcache_read_pc (regcache
);
1272 aspace
= get_regcache_aspace (regcache
);
1274 if (target_stopped_by_watchpoint ())
1276 /* Always interested in watchpoints. */
1278 else if (breakpoint_inserted_here_p (aspace
, tmp_pc
))
1280 /* There is a breakpoint here. Let the core
1282 if (software_breakpoint_inserted_here_p (aspace
, tmp_pc
))
1284 struct gdbarch
*gdbarch
1285 = get_regcache_arch (regcache
);
1286 CORE_ADDR decr_pc_after_break
1287 = target_decr_pc_after_break (gdbarch
);
1288 if (decr_pc_after_break
)
1289 regcache_write_pc (regcache
,
1290 tmp_pc
+ decr_pc_after_break
);
1295 /* This is a single-step trap. Record the
1296 insn and issue another step.
1297 FIXME: this part can be a random SIGTRAP too.
1298 But GDB cannot handle it. */
1301 if (!record_full_message_wrapper_safe (regcache
,
1304 status
->kind
= TARGET_WAITKIND_STOPPED
;
1305 status
->value
.sig
= GDB_SIGNAL_0
;
1309 if (gdbarch_software_single_step_p (gdbarch
))
1311 /* Try to insert the software single step breakpoint.
1312 If insert success, set step to 0. */
1313 set_executing (inferior_ptid
, 0);
1314 reinit_frame_cache ();
1315 if (gdbarch_software_single_step (gdbarch
,
1316 get_current_frame ()))
1318 set_executing (inferior_ptid
, 1);
1322 fprintf_unfiltered (gdb_stdlog
,
1323 "Process record: record_full_wait "
1324 "issuing one more step in the "
1325 "target beneath\n");
1326 record_full_beneath_to_resume
1327 (record_full_beneath_to_resume_ops
, ptid
, step
,
1333 /* The inferior is broken by a breakpoint or a signal. */
1342 struct regcache
*regcache
= get_current_regcache ();
1343 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1344 struct address_space
*aspace
= get_regcache_aspace (regcache
);
1345 int continue_flag
= 1;
1346 int first_record_full_end
= 1;
1347 struct cleanup
*old_cleanups
1348 = make_cleanup (record_full_wait_cleanups
, 0);
1351 record_full_hw_watchpoint
= 0;
1352 status
->kind
= TARGET_WAITKIND_STOPPED
;
1354 /* Check breakpoint when forward execute. */
1355 if (execution_direction
== EXEC_FORWARD
)
1357 tmp_pc
= regcache_read_pc (regcache
);
1358 if (breakpoint_inserted_here_p (aspace
, tmp_pc
))
1360 int decr_pc_after_break
= target_decr_pc_after_break (gdbarch
);
1363 fprintf_unfiltered (gdb_stdlog
,
1364 "Process record: break at %s.\n",
1365 paddress (gdbarch
, tmp_pc
));
1367 if (decr_pc_after_break
1368 && !record_full_resume_step
1369 && software_breakpoint_inserted_here_p (aspace
, tmp_pc
))
1370 regcache_write_pc (regcache
,
1371 tmp_pc
+ decr_pc_after_break
);
1376 /* If GDB is in terminal_inferior mode, it will not get the signal.
1377 And in GDB replay mode, GDB doesn't need to be in terminal_inferior
1378 mode, because inferior will not executed.
1379 Then set it to terminal_ours to make GDB get the signal. */
1380 target_terminal_ours ();
1382 /* In EXEC_FORWARD mode, record_full_list points to the tail of prev
1384 if (execution_direction
== EXEC_FORWARD
&& record_full_list
->next
)
1385 record_full_list
= record_full_list
->next
;
1387 /* Loop over the record_full_list, looking for the next place to
1391 /* Check for beginning and end of log. */
1392 if (execution_direction
== EXEC_REVERSE
1393 && record_full_list
== &record_full_first
)
1395 /* Hit beginning of record log in reverse. */
1396 status
->kind
= TARGET_WAITKIND_NO_HISTORY
;
1399 if (execution_direction
!= EXEC_REVERSE
&& !record_full_list
->next
)
1401 /* Hit end of record log going forward. */
1402 status
->kind
= TARGET_WAITKIND_NO_HISTORY
;
1406 record_full_exec_insn (regcache
, gdbarch
, record_full_list
);
1408 if (record_full_list
->type
== record_full_end
)
1410 if (record_debug
> 1)
1411 fprintf_unfiltered (gdb_stdlog
,
1412 "Process record: record_full_end %s to "
1414 host_address_to_string (record_full_list
));
1416 if (first_record_full_end
&& execution_direction
== EXEC_REVERSE
)
1418 /* When reverse excute, the first record_full_end is the
1419 part of current instruction. */
1420 first_record_full_end
= 0;
1424 /* In EXEC_REVERSE mode, this is the record_full_end of prev
1426 In EXEC_FORWARD mode, this is the record_full_end of
1427 current instruction. */
1429 if (record_full_resume_step
)
1431 if (record_debug
> 1)
1432 fprintf_unfiltered (gdb_stdlog
,
1433 "Process record: step.\n");
1437 /* check breakpoint */
1438 tmp_pc
= regcache_read_pc (regcache
);
1439 if (breakpoint_inserted_here_p (aspace
, tmp_pc
))
1441 int decr_pc_after_break
1442 = target_decr_pc_after_break (gdbarch
);
1445 fprintf_unfiltered (gdb_stdlog
,
1446 "Process record: break "
1448 paddress (gdbarch
, tmp_pc
));
1449 if (decr_pc_after_break
1450 && execution_direction
== EXEC_FORWARD
1451 && !record_full_resume_step
1452 && software_breakpoint_inserted_here_p (aspace
,
1454 regcache_write_pc (regcache
,
1455 tmp_pc
+ decr_pc_after_break
);
1459 if (record_full_hw_watchpoint
)
1462 fprintf_unfiltered (gdb_stdlog
,
1463 "Process record: hit hw "
1467 /* Check target signal */
1468 if (record_full_list
->u
.end
.sigval
!= GDB_SIGNAL_0
)
1469 /* FIXME: better way to check */
1476 if (execution_direction
== EXEC_REVERSE
)
1478 if (record_full_list
->prev
)
1479 record_full_list
= record_full_list
->prev
;
1483 if (record_full_list
->next
)
1484 record_full_list
= record_full_list
->next
;
1488 while (continue_flag
);
1491 if (record_full_get_sig
)
1492 status
->value
.sig
= GDB_SIGNAL_INT
;
1493 else if (record_full_list
->u
.end
.sigval
!= GDB_SIGNAL_0
)
1494 /* FIXME: better way to check */
1495 status
->value
.sig
= record_full_list
->u
.end
.sigval
;
1497 status
->value
.sig
= GDB_SIGNAL_TRAP
;
1499 discard_cleanups (old_cleanups
);
1502 signal (SIGINT
, handle_sigint
);
1504 do_cleanups (set_cleanups
);
1505 return inferior_ptid
;
1509 record_full_wait (struct target_ops
*ops
,
1510 ptid_t ptid
, struct target_waitstatus
*status
,
1515 return_ptid
= record_full_wait_1 (ops
, ptid
, status
, options
);
1516 if (status
->kind
!= TARGET_WAITKIND_IGNORE
)
1518 /* We're reporting a stop. Make sure any spurious
1519 target_wait(WNOHANG) doesn't advance the target until the
1520 core wants us resumed again. */
1521 record_full_resumed
= 0;
1527 record_full_stopped_by_watchpoint (struct target_ops
*ops
)
1529 if (RECORD_FULL_IS_REPLAY
)
1530 return record_full_hw_watchpoint
;
1533 struct target_ops
*beneath
= find_target_beneath (ops
);
1535 return record_full_beneath_to_stopped_by_watchpoint (beneath
);
1540 record_full_stopped_data_address (struct target_ops
*ops
, CORE_ADDR
*addr_p
)
1542 if (RECORD_FULL_IS_REPLAY
)
1545 return record_full_beneath_to_stopped_data_address (ops
, addr_p
);
1548 /* Record registers change (by user or by GDB) to list as an instruction. */
1551 record_full_registers_change (struct regcache
*regcache
, int regnum
)
1553 /* Check record_full_insn_num. */
1554 record_full_check_insn_num (0);
1556 record_full_arch_list_head
= NULL
;
1557 record_full_arch_list_tail
= NULL
;
1563 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
1565 if (record_full_arch_list_add_reg (regcache
, i
))
1567 record_full_list_release (record_full_arch_list_tail
);
1568 error (_("Process record: failed to record execution log."));
1574 if (record_full_arch_list_add_reg (regcache
, regnum
))
1576 record_full_list_release (record_full_arch_list_tail
);
1577 error (_("Process record: failed to record execution log."));
1580 if (record_full_arch_list_add_end ())
1582 record_full_list_release (record_full_arch_list_tail
);
1583 error (_("Process record: failed to record execution log."));
1585 record_full_list
->next
= record_full_arch_list_head
;
1586 record_full_arch_list_head
->prev
= record_full_list
;
1587 record_full_list
= record_full_arch_list_tail
;
1589 if (record_full_insn_num
== record_full_insn_max_num
)
1590 record_full_list_release_first ();
1592 record_full_insn_num
++;
1595 /* "to_store_registers" method for process record target. */
1598 record_full_store_registers (struct target_ops
*ops
,
1599 struct regcache
*regcache
,
1602 if (!record_full_gdb_operation_disable
)
1604 if (RECORD_FULL_IS_REPLAY
)
1608 /* Let user choose if he wants to write register or not. */
1611 query (_("Because GDB is in replay mode, changing the "
1612 "value of a register will make the execution "
1613 "log unusable from this point onward. "
1614 "Change all registers?"));
1617 query (_("Because GDB is in replay mode, changing the value "
1618 "of a register will make the execution log unusable "
1619 "from this point onward. Change register %s?"),
1620 gdbarch_register_name (get_regcache_arch (regcache
),
1625 /* Invalidate the value of regcache that was set in function
1626 "regcache_raw_write". */
1632 i
< gdbarch_num_regs (get_regcache_arch (regcache
));
1634 regcache_invalidate (regcache
, i
);
1637 regcache_invalidate (regcache
, regno
);
1639 error (_("Process record canceled the operation."));
1642 /* Destroy the record from here forward. */
1643 record_full_list_release_following (record_full_list
);
1646 record_full_registers_change (regcache
, regno
);
1648 record_full_beneath_to_store_registers
1649 (record_full_beneath_to_store_registers_ops
, regcache
, regno
);
1652 /* "to_xfer_partial" method. Behavior is conditional on
1653 RECORD_FULL_IS_REPLAY.
1654 In replay mode, we cannot write memory unles we are willing to
1655 invalidate the record/replay log from this point forward. */
1657 static enum target_xfer_status
1658 record_full_xfer_partial (struct target_ops
*ops
, enum target_object object
,
1659 const char *annex
, gdb_byte
*readbuf
,
1660 const gdb_byte
*writebuf
, ULONGEST offset
,
1661 ULONGEST len
, ULONGEST
*xfered_len
)
1663 if (!record_full_gdb_operation_disable
1664 && (object
== TARGET_OBJECT_MEMORY
1665 || object
== TARGET_OBJECT_RAW_MEMORY
) && writebuf
)
1667 if (RECORD_FULL_IS_REPLAY
)
1669 /* Let user choose if he wants to write memory or not. */
1670 if (!query (_("Because GDB is in replay mode, writing to memory "
1671 "will make the execution log unusable from this "
1672 "point onward. Write memory at address %s?"),
1673 paddress (target_gdbarch (), offset
)))
1674 error (_("Process record canceled the operation."));
1676 /* Destroy the record from here forward. */
1677 record_full_list_release_following (record_full_list
);
1680 /* Check record_full_insn_num */
1681 record_full_check_insn_num (0);
1683 /* Record registers change to list as an instruction. */
1684 record_full_arch_list_head
= NULL
;
1685 record_full_arch_list_tail
= NULL
;
1686 if (record_full_arch_list_add_mem (offset
, len
))
1688 record_full_list_release (record_full_arch_list_tail
);
1690 fprintf_unfiltered (gdb_stdlog
,
1691 "Process record: failed to record "
1693 return TARGET_XFER_E_IO
;
1695 if (record_full_arch_list_add_end ())
1697 record_full_list_release (record_full_arch_list_tail
);
1699 fprintf_unfiltered (gdb_stdlog
,
1700 "Process record: failed to record "
1702 return TARGET_XFER_E_IO
;
1704 record_full_list
->next
= record_full_arch_list_head
;
1705 record_full_arch_list_head
->prev
= record_full_list
;
1706 record_full_list
= record_full_arch_list_tail
;
1708 if (record_full_insn_num
== record_full_insn_max_num
)
1709 record_full_list_release_first ();
1711 record_full_insn_num
++;
1714 return record_full_beneath_to_xfer_partial
1715 (record_full_beneath_to_xfer_partial_ops
, object
, annex
,
1716 readbuf
, writebuf
, offset
, len
, xfered_len
);
1719 /* This structure represents a breakpoint inserted while the record
1720 target is active. We use this to know when to install/remove
1721 breakpoints in/from the target beneath. For example, a breakpoint
1722 may be inserted while recording, but removed when not replaying nor
1723 recording. In that case, the breakpoint had not been inserted on
1724 the target beneath, so we should not try to remove it there. */
1726 struct record_full_breakpoint
1728 /* The address and address space the breakpoint was set at. */
1729 struct address_space
*address_space
;
1732 /* True when the breakpoint has been also installed in the target
1733 beneath. This will be false for breakpoints set during replay or
1735 int in_target_beneath
;
1738 typedef struct record_full_breakpoint
*record_full_breakpoint_p
;
1739 DEF_VEC_P(record_full_breakpoint_p
);
1741 /* The list of breakpoints inserted while the record target is
1743 VEC(record_full_breakpoint_p
) *record_full_breakpoints
= NULL
;
1746 record_full_sync_record_breakpoints (struct bp_location
*loc
, void *data
)
1748 if (loc
->loc_type
!= bp_loc_software_breakpoint
)
1753 struct record_full_breakpoint
*bp
= XNEW (struct record_full_breakpoint
);
1755 bp
->addr
= loc
->target_info
.placed_address
;
1756 bp
->address_space
= loc
->target_info
.placed_address_space
;
1758 bp
->in_target_beneath
= 1;
1760 VEC_safe_push (record_full_breakpoint_p
, record_full_breakpoints
, bp
);
1764 /* Sync existing breakpoints to record_full_breakpoints. */
1767 record_full_init_record_breakpoints (void)
1769 VEC_free (record_full_breakpoint_p
, record_full_breakpoints
);
1771 iterate_over_bp_locations (record_full_sync_record_breakpoints
);
1774 /* Behavior is conditional on RECORD_FULL_IS_REPLAY. We will not actually
1775 insert or remove breakpoints in the real target when replaying, nor
1779 record_full_insert_breakpoint (struct target_ops
*ops
,
1780 struct gdbarch
*gdbarch
,
1781 struct bp_target_info
*bp_tgt
)
1783 struct record_full_breakpoint
*bp
;
1784 int in_target_beneath
= 0;
1786 if (!RECORD_FULL_IS_REPLAY
)
1788 /* When recording, we currently always single-step, so we don't
1789 really need to install regular breakpoints in the inferior.
1790 However, we do have to insert software single-step
1791 breakpoints, in case the target can't hardware step. To keep
1792 things single, we always insert. */
1793 struct cleanup
*old_cleanups
;
1796 old_cleanups
= record_full_gdb_operation_disable_set ();
1797 ops
= record_full_beneath_to_insert_breakpoint_ops
;
1798 ret
= record_full_beneath_to_insert_breakpoint (ops
, gdbarch
,
1800 do_cleanups (old_cleanups
);
1805 in_target_beneath
= 1;
1808 bp
= XNEW (struct record_full_breakpoint
);
1809 bp
->addr
= bp_tgt
->placed_address
;
1810 bp
->address_space
= bp_tgt
->placed_address_space
;
1811 bp
->in_target_beneath
= in_target_beneath
;
1812 VEC_safe_push (record_full_breakpoint_p
, record_full_breakpoints
, bp
);
1816 /* "to_remove_breakpoint" method for process record target. */
1819 record_full_remove_breakpoint (struct target_ops
*ops
,
1820 struct gdbarch
*gdbarch
,
1821 struct bp_target_info
*bp_tgt
)
1823 struct record_full_breakpoint
*bp
;
1827 VEC_iterate (record_full_breakpoint_p
,
1828 record_full_breakpoints
, ix
, bp
);
1831 if (bp
->addr
== bp_tgt
->placed_address
1832 && bp
->address_space
== bp_tgt
->placed_address_space
)
1834 if (bp
->in_target_beneath
)
1836 struct cleanup
*old_cleanups
;
1839 old_cleanups
= record_full_gdb_operation_disable_set ();
1840 ops
= record_full_beneath_to_remove_breakpoint_ops
;
1841 ret
= record_full_beneath_to_remove_breakpoint (ops
, gdbarch
,
1843 do_cleanups (old_cleanups
);
1849 VEC_unordered_remove (record_full_breakpoint_p
,
1850 record_full_breakpoints
, ix
);
1855 gdb_assert_not_reached ("removing unknown breakpoint");
1858 /* "to_can_execute_reverse" method for process record target. */
1861 record_full_can_execute_reverse (void)
1866 /* "to_get_bookmark" method for process record and prec over core. */
1869 record_full_get_bookmark (char *args
, int from_tty
)
1873 /* Return stringified form of instruction count. */
1874 if (record_full_list
&& record_full_list
->type
== record_full_end
)
1875 ret
= xstrdup (pulongest (record_full_list
->u
.end
.insn_num
));
1880 fprintf_unfiltered (gdb_stdlog
,
1881 "record_full_get_bookmark returns %s\n", ret
);
1883 fprintf_unfiltered (gdb_stdlog
,
1884 "record_full_get_bookmark returns NULL\n");
1886 return (gdb_byte
*) ret
;
1889 /* "to_goto_bookmark" method for process record and prec over core. */
1892 record_full_goto_bookmark (gdb_byte
*raw_bookmark
, int from_tty
)
1894 char *bookmark
= (char *) raw_bookmark
;
1897 fprintf_unfiltered (gdb_stdlog
,
1898 "record_full_goto_bookmark receives %s\n", bookmark
);
1900 if (bookmark
[0] == '\'' || bookmark
[0] == '\"')
1902 if (bookmark
[strlen (bookmark
) - 1] != bookmark
[0])
1903 error (_("Unbalanced quotes: %s"), bookmark
);
1905 /* Strip trailing quote. */
1906 bookmark
[strlen (bookmark
) - 1] = '\0';
1907 /* Strip leading quote. */
1909 /* Pass along to cmd_record_full_goto. */
1912 cmd_record_goto (bookmark
, from_tty
);
1917 record_full_async (struct target_ops
*ops
,
1918 void (*callback
) (enum inferior_event_type event_type
,
1919 void *context
), void *context
)
1921 /* If we're on top of a line target (e.g., linux-nat, remote), then
1922 set it to async mode as well. Will be NULL if we're sitting on
1923 top of the core target, for "record restore". */
1924 if (record_full_beneath_to_async
!= NULL
)
1925 record_full_beneath_to_async (find_target_beneath (ops
), callback
, context
);
1929 record_full_can_async_p (struct target_ops
*ops
)
1931 /* We only enable async when the user specifically asks for it. */
1932 return target_async_permitted
;
1936 record_full_is_async_p (struct target_ops
*ops
)
1938 /* We only enable async when the user specifically asks for it. */
1939 return target_async_permitted
;
1942 static enum exec_direction_kind
1943 record_full_execution_direction (void)
1945 return record_full_execution_dir
;
1949 record_full_info (void)
1951 struct record_full_entry
*p
;
1953 if (RECORD_FULL_IS_REPLAY
)
1954 printf_filtered (_("Replay mode:\n"));
1956 printf_filtered (_("Record mode:\n"));
1958 /* Find entry for first actual instruction in the log. */
1959 for (p
= record_full_first
.next
;
1960 p
!= NULL
&& p
->type
!= record_full_end
;
1964 /* Do we have a log at all? */
1965 if (p
!= NULL
&& p
->type
== record_full_end
)
1967 /* Display instruction number for first instruction in the log. */
1968 printf_filtered (_("Lowest recorded instruction number is %s.\n"),
1969 pulongest (p
->u
.end
.insn_num
));
1971 /* If in replay mode, display where we are in the log. */
1972 if (RECORD_FULL_IS_REPLAY
)
1973 printf_filtered (_("Current instruction number is %s.\n"),
1974 pulongest (record_full_list
->u
.end
.insn_num
));
1976 /* Display instruction number for last instruction in the log. */
1977 printf_filtered (_("Highest recorded instruction number is %s.\n"),
1978 pulongest (record_full_insn_count
));
1980 /* Display log count. */
1981 printf_filtered (_("Log contains %u instructions.\n"),
1982 record_full_insn_num
);
1985 printf_filtered (_("No instructions have been logged.\n"));
1987 /* Display max log size. */
1988 printf_filtered (_("Max logged instructions is %u.\n"),
1989 record_full_insn_max_num
);
1992 /* The "to_record_delete" target method. */
1995 record_full_delete (void)
1997 record_full_list_release_following (record_full_list
);
2000 /* The "to_record_is_replaying" target method. */
2003 record_full_is_replaying (void)
2005 return RECORD_FULL_IS_REPLAY
;
2008 /* Go to a specific entry. */
2011 record_full_goto_entry (struct record_full_entry
*p
)
2014 error (_("Target insn not found."));
2015 else if (p
== record_full_list
)
2016 error (_("Already at target insn."));
2017 else if (p
->u
.end
.insn_num
> record_full_list
->u
.end
.insn_num
)
2019 printf_filtered (_("Go forward to insn number %s\n"),
2020 pulongest (p
->u
.end
.insn_num
));
2021 record_full_goto_insn (p
, EXEC_FORWARD
);
2025 printf_filtered (_("Go backward to insn number %s\n"),
2026 pulongest (p
->u
.end
.insn_num
));
2027 record_full_goto_insn (p
, EXEC_REVERSE
);
2030 registers_changed ();
2031 reinit_frame_cache ();
2032 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
2035 /* The "to_goto_record_begin" target method. */
2038 record_full_goto_begin (void)
2040 struct record_full_entry
*p
= NULL
;
2042 for (p
= &record_full_first
; p
!= NULL
; p
= p
->next
)
2043 if (p
->type
== record_full_end
)
2046 record_full_goto_entry (p
);
2049 /* The "to_goto_record_end" target method. */
2052 record_full_goto_end (void)
2054 struct record_full_entry
*p
= NULL
;
2056 for (p
= record_full_list
; p
->next
!= NULL
; p
= p
->next
)
2058 for (; p
!= NULL
; p
= p
->prev
)
2059 if (p
->type
== record_full_end
)
2062 record_full_goto_entry (p
);
2065 /* The "to_goto_record" target method. */
2068 record_full_goto (ULONGEST target_insn
)
2070 struct record_full_entry
*p
= NULL
;
2072 for (p
= &record_full_first
; p
!= NULL
; p
= p
->next
)
2073 if (p
->type
== record_full_end
&& p
->u
.end
.insn_num
== target_insn
)
2076 record_full_goto_entry (p
);
2080 init_record_full_ops (void)
2082 record_full_ops
.to_shortname
= "record-full";
2083 record_full_ops
.to_longname
= "Process record and replay target";
2084 record_full_ops
.to_doc
=
2085 "Log program while executing and replay execution from log.";
2086 record_full_ops
.to_open
= record_full_open
;
2087 record_full_ops
.to_close
= record_full_close
;
2088 record_full_ops
.to_resume
= record_full_resume
;
2089 record_full_ops
.to_wait
= record_full_wait
;
2090 record_full_ops
.to_disconnect
= record_disconnect
;
2091 record_full_ops
.to_detach
= record_detach
;
2092 record_full_ops
.to_mourn_inferior
= record_mourn_inferior
;
2093 record_full_ops
.to_kill
= record_kill
;
2094 record_full_ops
.to_create_inferior
= find_default_create_inferior
;
2095 record_full_ops
.to_store_registers
= record_full_store_registers
;
2096 record_full_ops
.to_xfer_partial
= record_full_xfer_partial
;
2097 record_full_ops
.to_insert_breakpoint
= record_full_insert_breakpoint
;
2098 record_full_ops
.to_remove_breakpoint
= record_full_remove_breakpoint
;
2099 record_full_ops
.to_stopped_by_watchpoint
= record_full_stopped_by_watchpoint
;
2100 record_full_ops
.to_stopped_data_address
= record_full_stopped_data_address
;
2101 record_full_ops
.to_can_execute_reverse
= record_full_can_execute_reverse
;
2102 record_full_ops
.to_stratum
= record_stratum
;
2103 /* Add bookmark target methods. */
2104 record_full_ops
.to_get_bookmark
= record_full_get_bookmark
;
2105 record_full_ops
.to_goto_bookmark
= record_full_goto_bookmark
;
2106 record_full_ops
.to_async
= record_full_async
;
2107 record_full_ops
.to_can_async_p
= record_full_can_async_p
;
2108 record_full_ops
.to_is_async_p
= record_full_is_async_p
;
2109 record_full_ops
.to_execution_direction
= record_full_execution_direction
;
2110 record_full_ops
.to_info_record
= record_full_info
;
2111 record_full_ops
.to_save_record
= record_full_save
;
2112 record_full_ops
.to_delete_record
= record_full_delete
;
2113 record_full_ops
.to_record_is_replaying
= record_full_is_replaying
;
2114 record_full_ops
.to_goto_record_begin
= record_full_goto_begin
;
2115 record_full_ops
.to_goto_record_end
= record_full_goto_end
;
2116 record_full_ops
.to_goto_record
= record_full_goto
;
2117 record_full_ops
.to_magic
= OPS_MAGIC
;
2120 /* "to_resume" method for prec over corefile. */
2123 record_full_core_resume (struct target_ops
*ops
, ptid_t ptid
, int step
,
2124 enum gdb_signal signal
)
2126 record_full_resume_step
= step
;
2127 record_full_resumed
= 1;
2128 record_full_execution_dir
= execution_direction
;
2130 /* We are about to start executing the inferior (or simulate it),
2131 let's register it with the event loop. */
2132 if (target_can_async_p ())
2134 target_async (inferior_event_handler
, 0);
2136 /* Notify the event loop there's an event to wait for. */
2137 mark_async_event_handler (record_full_async_inferior_event_token
);
2141 /* "to_kill" method for prec over corefile. */
2144 record_full_core_kill (struct target_ops
*ops
)
2147 fprintf_unfiltered (gdb_stdlog
, "Process record: record_full_core_kill\n");
2149 unpush_target (&record_full_core_ops
);
2152 /* "to_fetch_registers" method for prec over corefile. */
2155 record_full_core_fetch_registers (struct target_ops
*ops
,
2156 struct regcache
*regcache
,
2161 int num
= gdbarch_num_regs (get_regcache_arch (regcache
));
2164 for (i
= 0; i
< num
; i
++)
2165 regcache_raw_supply (regcache
, i
,
2166 record_full_core_regbuf
+ MAX_REGISTER_SIZE
* i
);
2169 regcache_raw_supply (regcache
, regno
,
2170 record_full_core_regbuf
+ MAX_REGISTER_SIZE
* regno
);
2173 /* "to_prepare_to_store" method for prec over corefile. */
2176 record_full_core_prepare_to_store (struct target_ops
*self
,
2177 struct regcache
*regcache
)
2181 /* "to_store_registers" method for prec over corefile. */
2184 record_full_core_store_registers (struct target_ops
*ops
,
2185 struct regcache
*regcache
,
2188 if (record_full_gdb_operation_disable
)
2189 regcache_raw_collect (regcache
, regno
,
2190 record_full_core_regbuf
+ MAX_REGISTER_SIZE
* regno
);
2192 error (_("You can't do that without a process to debug."));
2195 /* "to_xfer_partial" method for prec over corefile. */
2197 static enum target_xfer_status
2198 record_full_core_xfer_partial (struct target_ops
*ops
,
2199 enum target_object object
,
2200 const char *annex
, gdb_byte
*readbuf
,
2201 const gdb_byte
*writebuf
, ULONGEST offset
,
2202 ULONGEST len
, ULONGEST
*xfered_len
)
2204 if (object
== TARGET_OBJECT_MEMORY
)
2206 if (record_full_gdb_operation_disable
|| !writebuf
)
2208 struct target_section
*p
;
2210 for (p
= record_full_core_start
; p
< record_full_core_end
; p
++)
2212 if (offset
>= p
->addr
)
2214 struct record_full_core_buf_entry
*entry
;
2215 ULONGEST sec_offset
;
2217 if (offset
>= p
->endaddr
)
2220 if (offset
+ len
> p
->endaddr
)
2221 len
= p
->endaddr
- offset
;
2223 sec_offset
= offset
- p
->addr
;
2225 /* Read readbuf or write writebuf p, offset, len. */
2227 if (p
->the_bfd_section
->flags
& SEC_CONSTRUCTOR
2228 || (p
->the_bfd_section
->flags
& SEC_HAS_CONTENTS
) == 0)
2231 memset (readbuf
, 0, len
);
2234 return TARGET_XFER_OK
;
2236 /* Get record_full_core_buf_entry. */
2237 for (entry
= record_full_core_buf_list
; entry
;
2238 entry
= entry
->prev
)
2245 /* Add a new entry. */
2246 entry
= (struct record_full_core_buf_entry
*)
2248 (sizeof (struct record_full_core_buf_entry
));
2250 if (!bfd_malloc_and_get_section
2251 (p
->the_bfd_section
->owner
,
2256 return TARGET_XFER_EOF
;
2258 entry
->prev
= record_full_core_buf_list
;
2259 record_full_core_buf_list
= entry
;
2262 memcpy (entry
->buf
+ sec_offset
, writebuf
,
2268 return record_full_beneath_to_xfer_partial
2269 (record_full_beneath_to_xfer_partial_ops
,
2270 object
, annex
, readbuf
, writebuf
,
2271 offset
, len
, xfered_len
);
2273 memcpy (readbuf
, entry
->buf
+ sec_offset
,
2278 return TARGET_XFER_OK
;
2282 return TARGET_XFER_E_IO
;
2285 error (_("You can't do that without a process to debug."));
2288 return record_full_beneath_to_xfer_partial
2289 (record_full_beneath_to_xfer_partial_ops
, object
, annex
,
2290 readbuf
, writebuf
, offset
, len
, xfered_len
);
2293 /* "to_insert_breakpoint" method for prec over corefile. */
2296 record_full_core_insert_breakpoint (struct target_ops
*ops
,
2297 struct gdbarch
*gdbarch
,
2298 struct bp_target_info
*bp_tgt
)
2303 /* "to_remove_breakpoint" method for prec over corefile. */
2306 record_full_core_remove_breakpoint (struct target_ops
*ops
,
2307 struct gdbarch
*gdbarch
,
2308 struct bp_target_info
*bp_tgt
)
2313 /* "to_has_execution" method for prec over corefile. */
2316 record_full_core_has_execution (struct target_ops
*ops
, ptid_t the_ptid
)
2322 init_record_full_core_ops (void)
2324 record_full_core_ops
.to_shortname
= "record-core";
2325 record_full_core_ops
.to_longname
= "Process record and replay target";
2326 record_full_core_ops
.to_doc
=
2327 "Log program while executing and replay execution from log.";
2328 record_full_core_ops
.to_open
= record_full_open
;
2329 record_full_core_ops
.to_close
= record_full_close
;
2330 record_full_core_ops
.to_resume
= record_full_core_resume
;
2331 record_full_core_ops
.to_wait
= record_full_wait
;
2332 record_full_core_ops
.to_kill
= record_full_core_kill
;
2333 record_full_core_ops
.to_fetch_registers
= record_full_core_fetch_registers
;
2334 record_full_core_ops
.to_prepare_to_store
= record_full_core_prepare_to_store
;
2335 record_full_core_ops
.to_store_registers
= record_full_core_store_registers
;
2336 record_full_core_ops
.to_xfer_partial
= record_full_core_xfer_partial
;
2337 record_full_core_ops
.to_insert_breakpoint
2338 = record_full_core_insert_breakpoint
;
2339 record_full_core_ops
.to_remove_breakpoint
2340 = record_full_core_remove_breakpoint
;
2341 record_full_core_ops
.to_stopped_by_watchpoint
2342 = record_full_stopped_by_watchpoint
;
2343 record_full_core_ops
.to_stopped_data_address
2344 = record_full_stopped_data_address
;
2345 record_full_core_ops
.to_can_execute_reverse
2346 = record_full_can_execute_reverse
;
2347 record_full_core_ops
.to_has_execution
= record_full_core_has_execution
;
2348 record_full_core_ops
.to_stratum
= record_stratum
;
2349 /* Add bookmark target methods. */
2350 record_full_core_ops
.to_get_bookmark
= record_full_get_bookmark
;
2351 record_full_core_ops
.to_goto_bookmark
= record_full_goto_bookmark
;
2352 record_full_core_ops
.to_async
= record_full_async
;
2353 record_full_core_ops
.to_can_async_p
= record_full_can_async_p
;
2354 record_full_core_ops
.to_is_async_p
= record_full_is_async_p
;
2355 record_full_core_ops
.to_execution_direction
2356 = record_full_execution_direction
;
2357 record_full_core_ops
.to_info_record
= record_full_info
;
2358 record_full_core_ops
.to_delete_record
= record_full_delete
;
2359 record_full_core_ops
.to_record_is_replaying
= record_full_is_replaying
;
2360 record_full_core_ops
.to_goto_record_begin
= record_full_goto_begin
;
2361 record_full_core_ops
.to_goto_record_end
= record_full_goto_end
;
2362 record_full_core_ops
.to_goto_record
= record_full_goto
;
2363 record_full_core_ops
.to_magic
= OPS_MAGIC
;
2366 /* Record log save-file format
2367 Version 1 (never released)
2370 4 bytes: magic number htonl(0x20090829).
2371 NOTE: be sure to change whenever this file format changes!
2375 1 byte: record type (record_full_end, see enum record_full_type).
2377 1 byte: record type (record_full_reg, see enum record_full_type).
2378 8 bytes: register id (network byte order).
2379 MAX_REGISTER_SIZE bytes: register value.
2381 1 byte: record type (record_full_mem, see enum record_full_type).
2382 8 bytes: memory length (network byte order).
2383 8 bytes: memory address (network byte order).
2384 n bytes: memory value (n == memory length).
2387 4 bytes: magic number netorder32(0x20091016).
2388 NOTE: be sure to change whenever this file format changes!
2392 1 byte: record type (record_full_end, see enum record_full_type).
2394 4 bytes: instruction count
2396 1 byte: record type (record_full_reg, see enum record_full_type).
2397 4 bytes: register id (network byte order).
2398 n bytes: register value (n == actual register size).
2399 (eg. 4 bytes for x86 general registers).
2401 1 byte: record type (record_full_mem, see enum record_full_type).
2402 4 bytes: memory length (network byte order).
2403 8 bytes: memory address (network byte order).
2404 n bytes: memory value (n == memory length).
2408 /* bfdcore_read -- read bytes from a core file section. */
2411 bfdcore_read (bfd
*obfd
, asection
*osec
, void *buf
, int len
, int *offset
)
2413 int ret
= bfd_get_section_contents (obfd
, osec
, buf
, *offset
, len
);
2418 error (_("Failed to read %d bytes from core file %s ('%s')."),
2419 len
, bfd_get_filename (obfd
),
2420 bfd_errmsg (bfd_get_error ()));
2423 static inline uint64_t
2424 netorder64 (uint64_t input
)
2428 store_unsigned_integer ((gdb_byte
*) &ret
, sizeof (ret
),
2429 BFD_ENDIAN_BIG
, input
);
2433 static inline uint32_t
2434 netorder32 (uint32_t input
)
2438 store_unsigned_integer ((gdb_byte
*) &ret
, sizeof (ret
),
2439 BFD_ENDIAN_BIG
, input
);
2443 static inline uint16_t
2444 netorder16 (uint16_t input
)
2448 store_unsigned_integer ((gdb_byte
*) &ret
, sizeof (ret
),
2449 BFD_ENDIAN_BIG
, input
);
2453 /* Restore the execution log from a core_bfd file. */
2455 record_full_restore (void)
2458 struct cleanup
*old_cleanups
;
2459 struct record_full_entry
*rec
;
2463 struct regcache
*regcache
;
2465 /* We restore the execution log from the open core bfd,
2467 if (core_bfd
== NULL
)
2470 /* "record_full_restore" can only be called when record list is empty. */
2471 gdb_assert (record_full_first
.next
== NULL
);
2474 fprintf_unfiltered (gdb_stdlog
, "Restoring recording from core file.\n");
2476 /* Now need to find our special note section. */
2477 osec
= bfd_get_section_by_name (core_bfd
, "null0");
2479 fprintf_unfiltered (gdb_stdlog
, "Find precord section %s.\n",
2480 osec
? "succeeded" : "failed");
2483 osec_size
= bfd_section_size (core_bfd
, osec
);
2485 fprintf_unfiltered (gdb_stdlog
, "%s", bfd_section_name (core_bfd
, osec
));
2487 /* Check the magic code. */
2488 bfdcore_read (core_bfd
, osec
, &magic
, sizeof (magic
), &bfd_offset
);
2489 if (magic
!= RECORD_FULL_FILE_MAGIC
)
2490 error (_("Version mis-match or file format error in core file %s."),
2491 bfd_get_filename (core_bfd
));
2493 fprintf_unfiltered (gdb_stdlog
,
2494 " Reading 4-byte magic cookie "
2495 "RECORD_FULL_FILE_MAGIC (0x%s)\n",
2496 phex_nz (netorder32 (magic
), 4));
2498 /* Restore the entries in recfd into record_full_arch_list_head and
2499 record_full_arch_list_tail. */
2500 record_full_arch_list_head
= NULL
;
2501 record_full_arch_list_tail
= NULL
;
2502 record_full_insn_num
= 0;
2503 old_cleanups
= make_cleanup (record_full_arch_list_cleanups
, 0);
2504 regcache
= get_current_regcache ();
2509 uint32_t regnum
, len
, signal
, count
;
2512 /* We are finished when offset reaches osec_size. */
2513 if (bfd_offset
>= osec_size
)
2515 bfdcore_read (core_bfd
, osec
, &rectype
, sizeof (rectype
), &bfd_offset
);
2519 case record_full_reg
: /* reg */
2520 /* Get register number to regnum. */
2521 bfdcore_read (core_bfd
, osec
, ®num
,
2522 sizeof (regnum
), &bfd_offset
);
2523 regnum
= netorder32 (regnum
);
2525 rec
= record_full_reg_alloc (regcache
, regnum
);
2528 bfdcore_read (core_bfd
, osec
, record_full_get_loc (rec
),
2529 rec
->u
.reg
.len
, &bfd_offset
);
2532 fprintf_unfiltered (gdb_stdlog
,
2533 " Reading register %d (1 "
2534 "plus %lu plus %d bytes)\n",
2536 (unsigned long) sizeof (regnum
),
2540 case record_full_mem
: /* mem */
2542 bfdcore_read (core_bfd
, osec
, &len
,
2543 sizeof (len
), &bfd_offset
);
2544 len
= netorder32 (len
);
2547 bfdcore_read (core_bfd
, osec
, &addr
,
2548 sizeof (addr
), &bfd_offset
);
2549 addr
= netorder64 (addr
);
2551 rec
= record_full_mem_alloc (addr
, len
);
2554 bfdcore_read (core_bfd
, osec
, record_full_get_loc (rec
),
2555 rec
->u
.mem
.len
, &bfd_offset
);
2558 fprintf_unfiltered (gdb_stdlog
,
2559 " Reading memory %s (1 plus "
2560 "%lu plus %lu plus %d bytes)\n",
2561 paddress (get_current_arch (),
2563 (unsigned long) sizeof (addr
),
2564 (unsigned long) sizeof (len
),
2568 case record_full_end
: /* end */
2569 rec
= record_full_end_alloc ();
2570 record_full_insn_num
++;
2572 /* Get signal value. */
2573 bfdcore_read (core_bfd
, osec
, &signal
,
2574 sizeof (signal
), &bfd_offset
);
2575 signal
= netorder32 (signal
);
2576 rec
->u
.end
.sigval
= signal
;
2578 /* Get insn count. */
2579 bfdcore_read (core_bfd
, osec
, &count
,
2580 sizeof (count
), &bfd_offset
);
2581 count
= netorder32 (count
);
2582 rec
->u
.end
.insn_num
= count
;
2583 record_full_insn_count
= count
+ 1;
2585 fprintf_unfiltered (gdb_stdlog
,
2586 " Reading record_full_end (1 + "
2587 "%lu + %lu bytes), offset == %s\n",
2588 (unsigned long) sizeof (signal
),
2589 (unsigned long) sizeof (count
),
2590 paddress (get_current_arch (),
2595 error (_("Bad entry type in core file %s."),
2596 bfd_get_filename (core_bfd
));
2600 /* Add rec to record arch list. */
2601 record_full_arch_list_add (rec
);
2604 discard_cleanups (old_cleanups
);
2606 /* Add record_full_arch_list_head to the end of record list. */
2607 record_full_first
.next
= record_full_arch_list_head
;
2608 record_full_arch_list_head
->prev
= &record_full_first
;
2609 record_full_arch_list_tail
->next
= NULL
;
2610 record_full_list
= &record_full_first
;
2612 /* Update record_full_insn_max_num. */
2613 if (record_full_insn_num
> record_full_insn_max_num
)
2615 record_full_insn_max_num
= record_full_insn_num
;
2616 warning (_("Auto increase record/replay buffer limit to %u."),
2617 record_full_insn_max_num
);
2621 printf_filtered (_("Restored records from core file %s.\n"),
2622 bfd_get_filename (core_bfd
));
2624 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
2627 /* bfdcore_write -- write bytes into a core file section. */
2630 bfdcore_write (bfd
*obfd
, asection
*osec
, void *buf
, int len
, int *offset
)
2632 int ret
= bfd_set_section_contents (obfd
, osec
, buf
, *offset
, len
);
2637 error (_("Failed to write %d bytes to core file %s ('%s')."),
2638 len
, bfd_get_filename (obfd
),
2639 bfd_errmsg (bfd_get_error ()));
2642 /* Restore the execution log from a file. We use a modified elf
2643 corefile format, with an extra section for our data. */
2646 cmd_record_full_restore (char *args
, int from_tty
)
2648 core_file_command (args
, from_tty
);
2649 record_full_open (args
, from_tty
);
2653 record_full_save_cleanups (void *data
)
2656 char *pathname
= xstrdup (bfd_get_filename (obfd
));
2658 gdb_bfd_unref (obfd
);
2663 /* Save the execution log to a file. We use a modified elf corefile
2664 format, with an extra section for our data. */
2667 record_full_save (const char *recfilename
)
2669 struct record_full_entry
*cur_record_full_list
;
2671 struct regcache
*regcache
;
2672 struct gdbarch
*gdbarch
;
2673 struct cleanup
*old_cleanups
;
2674 struct cleanup
*set_cleanups
;
2677 asection
*osec
= NULL
;
2680 /* Open the save file. */
2682 fprintf_unfiltered (gdb_stdlog
, "Saving execution log to core file '%s'\n",
2685 /* Open the output file. */
2686 obfd
= create_gcore_bfd (recfilename
);
2687 old_cleanups
= make_cleanup (record_full_save_cleanups
, obfd
);
2689 /* Save the current record entry to "cur_record_full_list". */
2690 cur_record_full_list
= record_full_list
;
2692 /* Get the values of regcache and gdbarch. */
2693 regcache
= get_current_regcache ();
2694 gdbarch
= get_regcache_arch (regcache
);
2696 /* Disable the GDB operation record. */
2697 set_cleanups
= record_full_gdb_operation_disable_set ();
2699 /* Reverse execute to the begin of record list. */
2702 /* Check for beginning and end of log. */
2703 if (record_full_list
== &record_full_first
)
2706 record_full_exec_insn (regcache
, gdbarch
, record_full_list
);
2708 if (record_full_list
->prev
)
2709 record_full_list
= record_full_list
->prev
;
2712 /* Compute the size needed for the extra bfd section. */
2713 save_size
= 4; /* magic cookie */
2714 for (record_full_list
= record_full_first
.next
; record_full_list
;
2715 record_full_list
= record_full_list
->next
)
2716 switch (record_full_list
->type
)
2718 case record_full_end
:
2719 save_size
+= 1 + 4 + 4;
2721 case record_full_reg
:
2722 save_size
+= 1 + 4 + record_full_list
->u
.reg
.len
;
2724 case record_full_mem
:
2725 save_size
+= 1 + 4 + 8 + record_full_list
->u
.mem
.len
;
2729 /* Make the new bfd section. */
2730 osec
= bfd_make_section_anyway_with_flags (obfd
, "precord",
2734 error (_("Failed to create 'precord' section for corefile %s: %s"),
2736 bfd_errmsg (bfd_get_error ()));
2737 bfd_set_section_size (obfd
, osec
, save_size
);
2738 bfd_set_section_vma (obfd
, osec
, 0);
2739 bfd_set_section_alignment (obfd
, osec
, 0);
2740 bfd_section_lma (obfd
, osec
) = 0;
2742 /* Save corefile state. */
2743 write_gcore_file (obfd
);
2745 /* Write out the record log. */
2746 /* Write the magic code. */
2747 magic
= RECORD_FULL_FILE_MAGIC
;
2749 fprintf_unfiltered (gdb_stdlog
,
2750 " Writing 4-byte magic cookie "
2751 "RECORD_FULL_FILE_MAGIC (0x%s)\n",
2752 phex_nz (magic
, 4));
2753 bfdcore_write (obfd
, osec
, &magic
, sizeof (magic
), &bfd_offset
);
2755 /* Save the entries to recfd and forward execute to the end of
2757 record_full_list
= &record_full_first
;
2761 if (record_full_list
!= &record_full_first
)
2764 uint32_t regnum
, len
, signal
, count
;
2767 type
= record_full_list
->type
;
2768 bfdcore_write (obfd
, osec
, &type
, sizeof (type
), &bfd_offset
);
2770 switch (record_full_list
->type
)
2772 case record_full_reg
: /* reg */
2774 fprintf_unfiltered (gdb_stdlog
,
2775 " Writing register %d (1 "
2776 "plus %lu plus %d bytes)\n",
2777 record_full_list
->u
.reg
.num
,
2778 (unsigned long) sizeof (regnum
),
2779 record_full_list
->u
.reg
.len
);
2782 regnum
= netorder32 (record_full_list
->u
.reg
.num
);
2783 bfdcore_write (obfd
, osec
, ®num
,
2784 sizeof (regnum
), &bfd_offset
);
2787 bfdcore_write (obfd
, osec
,
2788 record_full_get_loc (record_full_list
),
2789 record_full_list
->u
.reg
.len
, &bfd_offset
);
2792 case record_full_mem
: /* mem */
2794 fprintf_unfiltered (gdb_stdlog
,
2795 " Writing memory %s (1 plus "
2796 "%lu plus %lu plus %d bytes)\n",
2798 record_full_list
->u
.mem
.addr
),
2799 (unsigned long) sizeof (addr
),
2800 (unsigned long) sizeof (len
),
2801 record_full_list
->u
.mem
.len
);
2804 len
= netorder32 (record_full_list
->u
.mem
.len
);
2805 bfdcore_write (obfd
, osec
, &len
, sizeof (len
), &bfd_offset
);
2807 /* Write memaddr. */
2808 addr
= netorder64 (record_full_list
->u
.mem
.addr
);
2809 bfdcore_write (obfd
, osec
, &addr
,
2810 sizeof (addr
), &bfd_offset
);
2813 bfdcore_write (obfd
, osec
,
2814 record_full_get_loc (record_full_list
),
2815 record_full_list
->u
.mem
.len
, &bfd_offset
);
2818 case record_full_end
:
2820 fprintf_unfiltered (gdb_stdlog
,
2821 " Writing record_full_end (1 + "
2822 "%lu + %lu bytes)\n",
2823 (unsigned long) sizeof (signal
),
2824 (unsigned long) sizeof (count
));
2825 /* Write signal value. */
2826 signal
= netorder32 (record_full_list
->u
.end
.sigval
);
2827 bfdcore_write (obfd
, osec
, &signal
,
2828 sizeof (signal
), &bfd_offset
);
2830 /* Write insn count. */
2831 count
= netorder32 (record_full_list
->u
.end
.insn_num
);
2832 bfdcore_write (obfd
, osec
, &count
,
2833 sizeof (count
), &bfd_offset
);
2838 /* Execute entry. */
2839 record_full_exec_insn (regcache
, gdbarch
, record_full_list
);
2841 if (record_full_list
->next
)
2842 record_full_list
= record_full_list
->next
;
2847 /* Reverse execute to cur_record_full_list. */
2850 /* Check for beginning and end of log. */
2851 if (record_full_list
== cur_record_full_list
)
2854 record_full_exec_insn (regcache
, gdbarch
, record_full_list
);
2856 if (record_full_list
->prev
)
2857 record_full_list
= record_full_list
->prev
;
2860 do_cleanups (set_cleanups
);
2861 gdb_bfd_unref (obfd
);
2862 discard_cleanups (old_cleanups
);
2865 printf_filtered (_("Saved core file %s with execution log.\n"),
2869 /* record_full_goto_insn -- rewind the record log (forward or backward,
2870 depending on DIR) to the given entry, changing the program state
2874 record_full_goto_insn (struct record_full_entry
*entry
,
2875 enum exec_direction_kind dir
)
2877 struct cleanup
*set_cleanups
= record_full_gdb_operation_disable_set ();
2878 struct regcache
*regcache
= get_current_regcache ();
2879 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
2881 /* Assume everything is valid: we will hit the entry,
2882 and we will not hit the end of the recording. */
2884 if (dir
== EXEC_FORWARD
)
2885 record_full_list
= record_full_list
->next
;
2889 record_full_exec_insn (regcache
, gdbarch
, record_full_list
);
2890 if (dir
== EXEC_REVERSE
)
2891 record_full_list
= record_full_list
->prev
;
2893 record_full_list
= record_full_list
->next
;
2894 } while (record_full_list
!= entry
);
2895 do_cleanups (set_cleanups
);
2898 /* Alias for "target record-full". */
2901 cmd_record_full_start (char *args
, int from_tty
)
2903 execute_command ("target record-full", from_tty
);
2907 set_record_full_insn_max_num (char *args
, int from_tty
,
2908 struct cmd_list_element
*c
)
2910 if (record_full_insn_num
> record_full_insn_max_num
)
2912 /* Count down record_full_insn_num while releasing records from list. */
2913 while (record_full_insn_num
> record_full_insn_max_num
)
2915 record_full_list_release_first ();
2916 record_full_insn_num
--;
2921 /* The "set record full" command. */
2924 set_record_full_command (char *args
, int from_tty
)
2926 printf_unfiltered (_("\"set record full\" must be followed "
2927 "by an apporpriate subcommand.\n"));
2928 help_list (set_record_full_cmdlist
, "set record full ", all_commands
,
2932 /* The "show record full" command. */
2935 show_record_full_command (char *args
, int from_tty
)
2937 cmd_show_list (show_record_full_cmdlist
, from_tty
, "");
2940 /* Provide a prototype to silence -Wmissing-prototypes. */
2941 extern initialize_file_ftype _initialize_record_full
;
2944 _initialize_record_full (void)
2946 struct cmd_list_element
*c
;
2948 /* Init record_full_first. */
2949 record_full_first
.prev
= NULL
;
2950 record_full_first
.next
= NULL
;
2951 record_full_first
.type
= record_full_end
;
2953 init_record_full_ops ();
2954 add_target (&record_full_ops
);
2955 add_deprecated_target_alias (&record_full_ops
, "record");
2956 init_record_full_core_ops ();
2957 add_target (&record_full_core_ops
);
2959 add_prefix_cmd ("full", class_obscure
, cmd_record_full_start
,
2960 _("Start full execution recording."), &record_full_cmdlist
,
2961 "record full ", 0, &record_cmdlist
);
2963 c
= add_cmd ("restore", class_obscure
, cmd_record_full_restore
,
2964 _("Restore the execution log from a file.\n\
2965 Argument is filename. File must be created with 'record save'."),
2966 &record_full_cmdlist
);
2967 set_cmd_completer (c
, filename_completer
);
2969 /* Deprecate the old version without "full" prefix. */
2970 c
= add_alias_cmd ("restore", "full restore", class_obscure
, 1,
2972 set_cmd_completer (c
, filename_completer
);
2973 deprecate_cmd (c
, "record full restore");
2975 add_prefix_cmd ("full", class_support
, set_record_full_command
,
2976 _("Set record options"), &set_record_full_cmdlist
,
2977 "set record full ", 0, &set_record_cmdlist
);
2979 add_prefix_cmd ("full", class_support
, show_record_full_command
,
2980 _("Show record options"), &show_record_full_cmdlist
,
2981 "show record full ", 0, &show_record_cmdlist
);
2983 /* Record instructions number limit command. */
2984 add_setshow_boolean_cmd ("stop-at-limit", no_class
,
2985 &record_full_stop_at_limit
, _("\
2986 Set whether record/replay stops when record/replay buffer becomes full."), _("\
2987 Show whether record/replay stops when record/replay buffer becomes full."),
2988 _("Default is ON.\n\
2989 When ON, if the record/replay buffer becomes full, ask user what to do.\n\
2990 When OFF, if the record/replay buffer becomes full,\n\
2991 delete the oldest recorded instruction to make room for each new one."),
2993 &set_record_full_cmdlist
, &show_record_full_cmdlist
);
2995 c
= add_alias_cmd ("stop-at-limit", "full stop-at-limit", no_class
, 1,
2996 &set_record_cmdlist
);
2997 deprecate_cmd (c
, "set record full stop-at-limit");
2999 c
= add_alias_cmd ("stop-at-limit", "full stop-at-limit", no_class
, 1,
3000 &show_record_cmdlist
);
3001 deprecate_cmd (c
, "show record full stop-at-limit");
3003 add_setshow_uinteger_cmd ("insn-number-max", no_class
,
3004 &record_full_insn_max_num
,
3005 _("Set record/replay buffer limit."),
3006 _("Show record/replay buffer limit."), _("\
3007 Set the maximum number of instructions to be stored in the\n\
3008 record/replay buffer. A value of either \"unlimited\" or zero means no\n\
3009 limit. Default is 200000."),
3010 set_record_full_insn_max_num
,
3011 NULL
, &set_record_full_cmdlist
,
3012 &show_record_full_cmdlist
);
3014 c
= add_alias_cmd ("insn-number-max", "full insn-number-max", no_class
, 1,
3015 &set_record_cmdlist
);
3016 deprecate_cmd (c
, "set record full insn-number-max");
3018 c
= add_alias_cmd ("insn-number-max", "full insn-number-max", no_class
, 1,
3019 &show_record_cmdlist
);
3020 deprecate_cmd (c
, "show record full insn-number-max");
3022 add_setshow_boolean_cmd ("memory-query", no_class
,
3023 &record_full_memory_query
, _("\
3024 Set whether query if PREC cannot record memory change of next instruction."),
3026 Show whether query if PREC cannot record memory change of next instruction."),
3029 When ON, query if PREC cannot record memory change of next instruction."),
3031 &set_record_full_cmdlist
,
3032 &show_record_full_cmdlist
);
3034 c
= add_alias_cmd ("memory-query", "full memory-query", no_class
, 1,
3035 &set_record_cmdlist
);
3036 deprecate_cmd (c
, "set record full memory-query");
3038 c
= add_alias_cmd ("memory-query", "full memory-query", no_class
, 1,
3039 &show_record_cmdlist
);
3040 deprecate_cmd (c
, "show record full memory-query");