rtld - do not allow both dynamic DTV index and static TLS offset
[dragonfly.git] / contrib / gdb-7 / gdb / record-full.c
blobe7af504d5afd71900a97cb9e7c00df55ed46f311
1 /* Process record and replay target for GDB, the GNU debugger.
3 Copyright (C) 2013 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/>. */
20 #include "defs.h"
21 #include "gdbcmd.h"
22 #include "regcache.h"
23 #include "gdbthread.h"
24 #include "event-top.h"
25 #include "exceptions.h"
26 #include "completer.h"
27 #include "arch-utils.h"
28 #include "gdbcore.h"
29 #include "exec.h"
30 #include "record.h"
31 #include "record-full.h"
32 #include "elf-bfd.h"
33 #include "gcore.h"
34 #include "event-loop.h"
35 #include "inf-loop.h"
36 #include "gdb_bfd.h"
37 #include "observer.h"
39 #include <signal.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
74 instruction.
76 Each struct record_full_entry is linked to "record_full_list" by "prev"
77 and "next" pointers. */
79 struct record_full_mem_entry
81 CORE_ADDR addr;
82 int len;
83 /* Set this flag if target memory for this entry
84 can no longer be accessed. */
85 int mem_entry_not_accessible;
86 union
88 gdb_byte *ptr;
89 gdb_byte buf[sizeof (gdb_byte *)];
90 } u;
93 struct record_full_reg_entry
95 unsigned short num;
96 unsigned short len;
97 union
99 gdb_byte *ptr;
100 gdb_byte buf[2 * sizeof (gdb_byte *)];
101 } u;
104 struct record_full_end_entry
106 enum gdb_signal sigval;
107 ULONGEST insn_num;
110 enum record_full_type
112 record_full_end = 0,
113 record_full_reg,
114 record_full_mem
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;
146 union
148 /* reg */
149 struct record_full_reg_entry reg;
150 /* mem */
151 struct record_full_mem_entry mem;
152 /* end */
153 struct record_full_end_entry end;
154 } u;
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;
165 bfd_byte *buf;
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
178 the list.
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 int record_full_insn_num = 0;
203 /* Count of insns logged so far (may be larger
204 than count of insns presently in execution log). */
205 static ULONGEST record_full_insn_count;
207 /* The target_ops of process record. */
208 static struct target_ops record_full_ops;
209 static struct target_ops record_full_core_ops;
211 /* Command lists for "set/show record full". */
212 static struct cmd_list_element *set_record_full_cmdlist;
213 static struct cmd_list_element *show_record_full_cmdlist;
215 /* Command list for "record full". */
216 static struct cmd_list_element *record_full_cmdlist;
218 /* The beneath function pointers. */
219 static struct target_ops *record_full_beneath_to_resume_ops;
220 static void (*record_full_beneath_to_resume) (struct target_ops *, ptid_t, int,
221 enum gdb_signal);
222 static struct target_ops *record_full_beneath_to_wait_ops;
223 static ptid_t (*record_full_beneath_to_wait) (struct target_ops *, ptid_t,
224 struct target_waitstatus *,
225 int);
226 static struct target_ops *record_full_beneath_to_store_registers_ops;
227 static void (*record_full_beneath_to_store_registers) (struct target_ops *,
228 struct regcache *,
229 int regno);
230 static struct target_ops *record_full_beneath_to_xfer_partial_ops;
231 static LONGEST
232 (*record_full_beneath_to_xfer_partial) (struct target_ops *ops,
233 enum target_object object,
234 const char *annex,
235 gdb_byte *readbuf,
236 const gdb_byte *writebuf,
237 ULONGEST offset,
238 LONGEST len);
239 static int
240 (*record_full_beneath_to_insert_breakpoint) (struct gdbarch *,
241 struct bp_target_info *);
242 static int
243 (*record_full_beneath_to_remove_breakpoint) (struct gdbarch *,
244 struct bp_target_info *);
245 static int (*record_full_beneath_to_stopped_by_watchpoint) (void);
246 static int (*record_full_beneath_to_stopped_data_address) (struct target_ops *,
247 CORE_ADDR *);
248 static void
249 (*record_full_beneath_to_async) (void (*) (enum inferior_event_type, void *),
250 void *);
252 static void record_full_goto_insn (struct record_full_entry *entry,
253 enum exec_direction_kind dir);
254 static void record_full_save (char *recfilename);
256 /* Alloc and free functions for record_full_reg, record_full_mem, and
257 record_full_end entries. */
259 /* Alloc a record_full_reg record entry. */
261 static inline struct record_full_entry *
262 record_full_reg_alloc (struct regcache *regcache, int regnum)
264 struct record_full_entry *rec;
265 struct gdbarch *gdbarch = get_regcache_arch (regcache);
267 rec = xcalloc (1, sizeof (struct record_full_entry));
268 rec->type = record_full_reg;
269 rec->u.reg.num = regnum;
270 rec->u.reg.len = register_size (gdbarch, regnum);
271 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
272 rec->u.reg.u.ptr = (gdb_byte *) xmalloc (rec->u.reg.len);
274 return rec;
277 /* Free a record_full_reg record entry. */
279 static inline void
280 record_full_reg_release (struct record_full_entry *rec)
282 gdb_assert (rec->type == record_full_reg);
283 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
284 xfree (rec->u.reg.u.ptr);
285 xfree (rec);
288 /* Alloc a record_full_mem record entry. */
290 static inline struct record_full_entry *
291 record_full_mem_alloc (CORE_ADDR addr, int len)
293 struct record_full_entry *rec;
295 rec = xcalloc (1, sizeof (struct record_full_entry));
296 rec->type = record_full_mem;
297 rec->u.mem.addr = addr;
298 rec->u.mem.len = len;
299 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
300 rec->u.mem.u.ptr = (gdb_byte *) xmalloc (len);
302 return rec;
305 /* Free a record_full_mem record entry. */
307 static inline void
308 record_full_mem_release (struct record_full_entry *rec)
310 gdb_assert (rec->type == record_full_mem);
311 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
312 xfree (rec->u.mem.u.ptr);
313 xfree (rec);
316 /* Alloc a record_full_end record entry. */
318 static inline struct record_full_entry *
319 record_full_end_alloc (void)
321 struct record_full_entry *rec;
323 rec = xcalloc (1, sizeof (struct record_full_entry));
324 rec->type = record_full_end;
326 return rec;
329 /* Free a record_full_end record entry. */
331 static inline void
332 record_full_end_release (struct record_full_entry *rec)
334 xfree (rec);
337 /* Free one record entry, any type.
338 Return entry->type, in case caller wants to know. */
340 static inline enum record_full_type
341 record_full_entry_release (struct record_full_entry *rec)
343 enum record_full_type type = rec->type;
345 switch (type) {
346 case record_full_reg:
347 record_full_reg_release (rec);
348 break;
349 case record_full_mem:
350 record_full_mem_release (rec);
351 break;
352 case record_full_end:
353 record_full_end_release (rec);
354 break;
356 return type;
359 /* Free all record entries in list pointed to by REC. */
361 static void
362 record_full_list_release (struct record_full_entry *rec)
364 if (!rec)
365 return;
367 while (rec->next)
368 rec = rec->next;
370 while (rec->prev)
372 rec = rec->prev;
373 record_full_entry_release (rec->next);
376 if (rec == &record_full_first)
378 record_full_insn_num = 0;
379 record_full_first.next = NULL;
381 else
382 record_full_entry_release (rec);
385 /* Free all record entries forward of the given list position. */
387 static void
388 record_full_list_release_following (struct record_full_entry *rec)
390 struct record_full_entry *tmp = rec->next;
392 rec->next = NULL;
393 while (tmp)
395 rec = tmp->next;
396 if (record_full_entry_release (tmp) == record_full_end)
398 record_full_insn_num--;
399 record_full_insn_count--;
401 tmp = rec;
405 /* Delete the first instruction from the beginning of the log, to make
406 room for adding a new instruction at the end of the log.
408 Note -- this function does not modify record_full_insn_num. */
410 static void
411 record_full_list_release_first (void)
413 struct record_full_entry *tmp;
415 if (!record_full_first.next)
416 return;
418 /* Loop until a record_full_end. */
419 while (1)
421 /* Cut record_full_first.next out of the linked list. */
422 tmp = record_full_first.next;
423 record_full_first.next = tmp->next;
424 tmp->next->prev = &record_full_first;
426 /* tmp is now isolated, and can be deleted. */
427 if (record_full_entry_release (tmp) == record_full_end)
428 break; /* End loop at first record_full_end. */
430 if (!record_full_first.next)
432 gdb_assert (record_full_insn_num == 1);
433 break; /* End loop when list is empty. */
438 /* Add a struct record_full_entry to record_full_arch_list. */
440 static void
441 record_full_arch_list_add (struct record_full_entry *rec)
443 if (record_debug > 1)
444 fprintf_unfiltered (gdb_stdlog,
445 "Process record: record_full_arch_list_add %s.\n",
446 host_address_to_string (rec));
448 if (record_full_arch_list_tail)
450 record_full_arch_list_tail->next = rec;
451 rec->prev = record_full_arch_list_tail;
452 record_full_arch_list_tail = rec;
454 else
456 record_full_arch_list_head = rec;
457 record_full_arch_list_tail = rec;
461 /* Return the value storage location of a record entry. */
462 static inline gdb_byte *
463 record_full_get_loc (struct record_full_entry *rec)
465 switch (rec->type) {
466 case record_full_mem:
467 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
468 return rec->u.mem.u.ptr;
469 else
470 return rec->u.mem.u.buf;
471 case record_full_reg:
472 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
473 return rec->u.reg.u.ptr;
474 else
475 return rec->u.reg.u.buf;
476 case record_full_end:
477 default:
478 gdb_assert_not_reached ("unexpected record_full_entry type");
479 return NULL;
483 /* Record the value of a register NUM to record_full_arch_list. */
486 record_full_arch_list_add_reg (struct regcache *regcache, int regnum)
488 struct record_full_entry *rec;
490 if (record_debug > 1)
491 fprintf_unfiltered (gdb_stdlog,
492 "Process record: add register num = %d to "
493 "record list.\n",
494 regnum);
496 rec = record_full_reg_alloc (regcache, regnum);
498 regcache_raw_read (regcache, regnum, record_full_get_loc (rec));
500 record_full_arch_list_add (rec);
502 return 0;
505 /* Record the value of a region of memory whose address is ADDR and
506 length is LEN to record_full_arch_list. */
509 record_full_arch_list_add_mem (CORE_ADDR addr, int len)
511 struct record_full_entry *rec;
513 if (record_debug > 1)
514 fprintf_unfiltered (gdb_stdlog,
515 "Process record: add mem addr = %s len = %d to "
516 "record list.\n",
517 paddress (target_gdbarch (), addr), len);
519 if (!addr) /* FIXME: Why? Some arch must permit it... */
520 return 0;
522 rec = record_full_mem_alloc (addr, len);
524 if (record_read_memory (target_gdbarch (), addr,
525 record_full_get_loc (rec), len))
527 record_full_mem_release (rec);
528 return -1;
531 record_full_arch_list_add (rec);
533 return 0;
536 /* Add a record_full_end type struct record_full_entry to
537 record_full_arch_list. */
540 record_full_arch_list_add_end (void)
542 struct record_full_entry *rec;
544 if (record_debug > 1)
545 fprintf_unfiltered (gdb_stdlog,
546 "Process record: add end to arch list.\n");
548 rec = record_full_end_alloc ();
549 rec->u.end.sigval = GDB_SIGNAL_0;
550 rec->u.end.insn_num = ++record_full_insn_count;
552 record_full_arch_list_add (rec);
554 return 0;
557 static void
558 record_full_check_insn_num (int set_terminal)
560 if (record_full_insn_max_num)
562 gdb_assert (record_full_insn_num <= record_full_insn_max_num);
563 if (record_full_insn_num == record_full_insn_max_num)
565 /* Ask user what to do. */
566 if (record_full_stop_at_limit)
568 int q;
570 if (set_terminal)
571 target_terminal_ours ();
572 q = yquery (_("Do you want to auto delete previous execution "
573 "log entries when record/replay buffer becomes "
574 "full (record full stop-at-limit)?"));
575 if (set_terminal)
576 target_terminal_inferior ();
577 if (q)
578 record_full_stop_at_limit = 0;
579 else
580 error (_("Process record: stopped by user."));
586 static void
587 record_full_arch_list_cleanups (void *ignore)
589 record_full_list_release (record_full_arch_list_tail);
592 /* Before inferior step (when GDB record the running message, inferior
593 only can step), GDB will call this function to record the values to
594 record_full_list. This function will call gdbarch_process_record to
595 record the running message of inferior and set them to
596 record_full_arch_list, and add it to record_full_list. */
598 static int
599 record_full_message (struct regcache *regcache, enum gdb_signal signal)
601 int ret;
602 struct gdbarch *gdbarch = get_regcache_arch (regcache);
603 struct cleanup *old_cleanups
604 = make_cleanup (record_full_arch_list_cleanups, 0);
606 record_full_arch_list_head = NULL;
607 record_full_arch_list_tail = NULL;
609 /* Check record_full_insn_num. */
610 record_full_check_insn_num (1);
612 /* If gdb sends a signal value to target_resume,
613 save it in the 'end' field of the previous instruction.
615 Maybe process record should record what really happened,
616 rather than what gdb pretends has happened.
618 So if Linux delivered the signal to the child process during
619 the record mode, we will record it and deliver it again in
620 the replay mode.
622 If user says "ignore this signal" during the record mode, then
623 it will be ignored again during the replay mode (no matter if
624 the user says something different, like "deliver this signal"
625 during the replay mode).
627 User should understand that nothing he does during the replay
628 mode will change the behavior of the child. If he tries,
629 then that is a user error.
631 But we should still deliver the signal to gdb during the replay,
632 if we delivered it during the recording. Therefore we should
633 record the signal during record_full_wait, not
634 record_full_resume. */
635 if (record_full_list != &record_full_first) /* FIXME better way to check */
637 gdb_assert (record_full_list->type == record_full_end);
638 record_full_list->u.end.sigval = signal;
641 if (signal == GDB_SIGNAL_0
642 || !gdbarch_process_record_signal_p (gdbarch))
643 ret = gdbarch_process_record (gdbarch,
644 regcache,
645 regcache_read_pc (regcache));
646 else
647 ret = gdbarch_process_record_signal (gdbarch,
648 regcache,
649 signal);
651 if (ret > 0)
652 error (_("Process record: inferior program stopped."));
653 if (ret < 0)
654 error (_("Process record: failed to record execution log."));
656 discard_cleanups (old_cleanups);
658 record_full_list->next = record_full_arch_list_head;
659 record_full_arch_list_head->prev = record_full_list;
660 record_full_list = record_full_arch_list_tail;
662 if (record_full_insn_num == record_full_insn_max_num
663 && record_full_insn_max_num)
664 record_full_list_release_first ();
665 else
666 record_full_insn_num++;
668 return 1;
671 struct record_full_message_args {
672 struct regcache *regcache;
673 enum gdb_signal signal;
676 static int
677 record_full_message_wrapper (void *args)
679 struct record_full_message_args *record_full_args = args;
681 return record_full_message (record_full_args->regcache,
682 record_full_args->signal);
685 static int
686 record_full_message_wrapper_safe (struct regcache *regcache,
687 enum gdb_signal signal)
689 struct record_full_message_args args;
691 args.regcache = regcache;
692 args.signal = signal;
694 return catch_errors (record_full_message_wrapper, &args, NULL,
695 RETURN_MASK_ALL);
698 /* Set to 1 if record_full_store_registers and record_full_xfer_partial
699 doesn't need record. */
701 static int record_full_gdb_operation_disable = 0;
703 struct cleanup *
704 record_full_gdb_operation_disable_set (void)
706 struct cleanup *old_cleanups = NULL;
708 old_cleanups =
709 make_cleanup_restore_integer (&record_full_gdb_operation_disable);
710 record_full_gdb_operation_disable = 1;
712 return old_cleanups;
715 /* Flag set to TRUE for target_stopped_by_watchpoint. */
716 static int record_full_hw_watchpoint = 0;
718 /* Execute one instruction from the record log. Each instruction in
719 the log will be represented by an arbitrary sequence of register
720 entries and memory entries, followed by an 'end' entry. */
722 static inline void
723 record_full_exec_insn (struct regcache *regcache,
724 struct gdbarch *gdbarch,
725 struct record_full_entry *entry)
727 switch (entry->type)
729 case record_full_reg: /* reg */
731 gdb_byte reg[MAX_REGISTER_SIZE];
733 if (record_debug > 1)
734 fprintf_unfiltered (gdb_stdlog,
735 "Process record: record_full_reg %s to "
736 "inferior num = %d.\n",
737 host_address_to_string (entry),
738 entry->u.reg.num);
740 regcache_cooked_read (regcache, entry->u.reg.num, reg);
741 regcache_cooked_write (regcache, entry->u.reg.num,
742 record_full_get_loc (entry));
743 memcpy (record_full_get_loc (entry), reg, entry->u.reg.len);
745 break;
747 case record_full_mem: /* mem */
749 /* Nothing to do if the entry is flagged not_accessible. */
750 if (!entry->u.mem.mem_entry_not_accessible)
752 gdb_byte *mem = alloca (entry->u.mem.len);
754 if (record_debug > 1)
755 fprintf_unfiltered (gdb_stdlog,
756 "Process record: record_full_mem %s to "
757 "inferior addr = %s len = %d.\n",
758 host_address_to_string (entry),
759 paddress (gdbarch, entry->u.mem.addr),
760 entry->u.mem.len);
762 if (record_read_memory (gdbarch,
763 entry->u.mem.addr, mem, entry->u.mem.len))
764 entry->u.mem.mem_entry_not_accessible = 1;
765 else
767 if (target_write_memory (entry->u.mem.addr,
768 record_full_get_loc (entry),
769 entry->u.mem.len))
771 entry->u.mem.mem_entry_not_accessible = 1;
772 if (record_debug)
773 warning (_("Process record: error writing memory at "
774 "addr = %s len = %d."),
775 paddress (gdbarch, entry->u.mem.addr),
776 entry->u.mem.len);
778 else
780 memcpy (record_full_get_loc (entry), mem,
781 entry->u.mem.len);
783 /* We've changed memory --- check if a hardware
784 watchpoint should trap. Note that this
785 presently assumes the target beneath supports
786 continuable watchpoints. On non-continuable
787 watchpoints target, we'll want to check this
788 _before_ actually doing the memory change, and
789 not doing the change at all if the watchpoint
790 traps. */
791 if (hardware_watchpoint_inserted_in_range
792 (get_regcache_aspace (regcache),
793 entry->u.mem.addr, entry->u.mem.len))
794 record_full_hw_watchpoint = 1;
799 break;
803 static struct target_ops *tmp_to_resume_ops;
804 static void (*tmp_to_resume) (struct target_ops *, ptid_t, int,
805 enum gdb_signal);
806 static struct target_ops *tmp_to_wait_ops;
807 static ptid_t (*tmp_to_wait) (struct target_ops *, ptid_t,
808 struct target_waitstatus *,
809 int);
810 static struct target_ops *tmp_to_store_registers_ops;
811 static void (*tmp_to_store_registers) (struct target_ops *,
812 struct regcache *,
813 int regno);
814 static struct target_ops *tmp_to_xfer_partial_ops;
815 static LONGEST (*tmp_to_xfer_partial) (struct target_ops *ops,
816 enum target_object object,
817 const char *annex,
818 gdb_byte *readbuf,
819 const gdb_byte *writebuf,
820 ULONGEST offset,
821 LONGEST len);
822 static int (*tmp_to_insert_breakpoint) (struct gdbarch *,
823 struct bp_target_info *);
824 static int (*tmp_to_remove_breakpoint) (struct gdbarch *,
825 struct bp_target_info *);
826 static int (*tmp_to_stopped_by_watchpoint) (void);
827 static int (*tmp_to_stopped_data_address) (struct target_ops *, CORE_ADDR *);
828 static int (*tmp_to_stopped_data_address) (struct target_ops *, CORE_ADDR *);
829 static void (*tmp_to_async) (void (*) (enum inferior_event_type, void *), void *);
831 static void record_full_restore (void);
833 /* Asynchronous signal handle registered as event loop source for when
834 we have pending events ready to be passed to the core. */
836 static struct async_event_handler *record_full_async_inferior_event_token;
838 static void
839 record_full_async_inferior_event_handler (gdb_client_data data)
841 inferior_event_handler (INF_REG_EVENT, NULL);
844 /* Open the process record target. */
846 static void
847 record_full_core_open_1 (char *name, int from_tty)
849 struct regcache *regcache = get_current_regcache ();
850 int regnum = gdbarch_num_regs (get_regcache_arch (regcache));
851 int i;
853 /* Get record_full_core_regbuf. */
854 target_fetch_registers (regcache, -1);
855 record_full_core_regbuf = xmalloc (MAX_REGISTER_SIZE * regnum);
856 for (i = 0; i < regnum; i ++)
857 regcache_raw_collect (regcache, i,
858 record_full_core_regbuf + MAX_REGISTER_SIZE * i);
860 /* Get record_full_core_start and record_full_core_end. */
861 if (build_section_table (core_bfd, &record_full_core_start,
862 &record_full_core_end))
864 xfree (record_full_core_regbuf);
865 record_full_core_regbuf = NULL;
866 error (_("\"%s\": Can't find sections: %s"),
867 bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ()));
870 push_target (&record_full_core_ops);
871 record_full_restore ();
874 /* "to_open" target method for 'live' processes. */
876 static void
877 record_full_open_1 (char *name, int from_tty)
879 if (record_debug)
880 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_open\n");
882 /* check exec */
883 if (!target_has_execution)
884 error (_("Process record: the program is not being run."));
885 if (non_stop)
886 error (_("Process record target can't debug inferior in non-stop mode "
887 "(non-stop)."));
889 if (!gdbarch_process_record_p (target_gdbarch ()))
890 error (_("Process record: the current architecture doesn't support "
891 "record function."));
893 if (!tmp_to_resume)
894 error (_("Could not find 'to_resume' method on the target stack."));
895 if (!tmp_to_wait)
896 error (_("Could not find 'to_wait' method on the target stack."));
897 if (!tmp_to_store_registers)
898 error (_("Could not find 'to_store_registers' "
899 "method on the target stack."));
900 if (!tmp_to_insert_breakpoint)
901 error (_("Could not find 'to_insert_breakpoint' "
902 "method on the target stack."));
903 if (!tmp_to_remove_breakpoint)
904 error (_("Could not find 'to_remove_breakpoint' "
905 "method on the target stack."));
906 if (!tmp_to_stopped_by_watchpoint)
907 error (_("Could not find 'to_stopped_by_watchpoint' "
908 "method on the target stack."));
909 if (!tmp_to_stopped_data_address)
910 error (_("Could not find 'to_stopped_data_address' "
911 "method on the target stack."));
913 push_target (&record_full_ops);
916 static void record_full_init_record_breakpoints (void);
918 /* "to_open" target method. Open the process record target. */
920 static void
921 record_full_open (char *name, int from_tty)
923 struct target_ops *t;
925 if (record_debug)
926 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_open\n");
928 /* Check if record target is already running. */
929 if (current_target.to_stratum == record_stratum)
930 error (_("Process record target already running. Use \"record stop\" to "
931 "stop record target first."));
933 /* Reset the tmp beneath pointers. */
934 tmp_to_resume_ops = NULL;
935 tmp_to_resume = NULL;
936 tmp_to_wait_ops = NULL;
937 tmp_to_wait = NULL;
938 tmp_to_store_registers_ops = NULL;
939 tmp_to_store_registers = NULL;
940 tmp_to_xfer_partial_ops = NULL;
941 tmp_to_xfer_partial = NULL;
942 tmp_to_insert_breakpoint = NULL;
943 tmp_to_remove_breakpoint = NULL;
944 tmp_to_stopped_by_watchpoint = NULL;
945 tmp_to_stopped_data_address = NULL;
946 tmp_to_async = NULL;
948 /* Set the beneath function pointers. */
949 for (t = current_target.beneath; t != NULL; t = t->beneath)
951 if (!tmp_to_resume)
953 tmp_to_resume = t->to_resume;
954 tmp_to_resume_ops = t;
956 if (!tmp_to_wait)
958 tmp_to_wait = t->to_wait;
959 tmp_to_wait_ops = t;
961 if (!tmp_to_store_registers)
963 tmp_to_store_registers = t->to_store_registers;
964 tmp_to_store_registers_ops = t;
966 if (!tmp_to_xfer_partial)
968 tmp_to_xfer_partial = t->to_xfer_partial;
969 tmp_to_xfer_partial_ops = t;
971 if (!tmp_to_insert_breakpoint)
972 tmp_to_insert_breakpoint = t->to_insert_breakpoint;
973 if (!tmp_to_remove_breakpoint)
974 tmp_to_remove_breakpoint = t->to_remove_breakpoint;
975 if (!tmp_to_stopped_by_watchpoint)
976 tmp_to_stopped_by_watchpoint = t->to_stopped_by_watchpoint;
977 if (!tmp_to_stopped_data_address)
978 tmp_to_stopped_data_address = t->to_stopped_data_address;
979 if (!tmp_to_async)
980 tmp_to_async = t->to_async;
982 if (!tmp_to_xfer_partial)
983 error (_("Could not find 'to_xfer_partial' method on the target stack."));
985 /* Reset */
986 record_full_insn_num = 0;
987 record_full_insn_count = 0;
988 record_full_list = &record_full_first;
989 record_full_list->next = NULL;
991 /* Set the tmp beneath pointers to beneath pointers. */
992 record_full_beneath_to_resume_ops = tmp_to_resume_ops;
993 record_full_beneath_to_resume = tmp_to_resume;
994 record_full_beneath_to_wait_ops = tmp_to_wait_ops;
995 record_full_beneath_to_wait = tmp_to_wait;
996 record_full_beneath_to_store_registers_ops = tmp_to_store_registers_ops;
997 record_full_beneath_to_store_registers = tmp_to_store_registers;
998 record_full_beneath_to_xfer_partial_ops = tmp_to_xfer_partial_ops;
999 record_full_beneath_to_xfer_partial = tmp_to_xfer_partial;
1000 record_full_beneath_to_insert_breakpoint = tmp_to_insert_breakpoint;
1001 record_full_beneath_to_remove_breakpoint = tmp_to_remove_breakpoint;
1002 record_full_beneath_to_stopped_by_watchpoint = tmp_to_stopped_by_watchpoint;
1003 record_full_beneath_to_stopped_data_address = tmp_to_stopped_data_address;
1004 record_full_beneath_to_async = tmp_to_async;
1006 if (core_bfd)
1007 record_full_core_open_1 (name, from_tty);
1008 else
1009 record_full_open_1 (name, from_tty);
1011 /* Register extra event sources in the event loop. */
1012 record_full_async_inferior_event_token
1013 = create_async_event_handler (record_full_async_inferior_event_handler,
1014 NULL);
1016 record_full_init_record_breakpoints ();
1018 observer_notify_record_changed (current_inferior (), 1);
1021 /* "to_close" target method. Close the process record target. */
1023 static void
1024 record_full_close (int quitting)
1026 struct record_full_core_buf_entry *entry;
1028 if (record_debug)
1029 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_close\n");
1031 record_full_list_release (record_full_list);
1033 /* Release record_full_core_regbuf. */
1034 if (record_full_core_regbuf)
1036 xfree (record_full_core_regbuf);
1037 record_full_core_regbuf = NULL;
1040 /* Release record_full_core_buf_list. */
1041 if (record_full_core_buf_list)
1043 for (entry = record_full_core_buf_list->prev; entry;
1044 entry = entry->prev)
1046 xfree (record_full_core_buf_list);
1047 record_full_core_buf_list = entry;
1049 record_full_core_buf_list = NULL;
1052 if (record_full_async_inferior_event_token)
1053 delete_async_event_handler (&record_full_async_inferior_event_token);
1056 static int record_full_resume_step = 0;
1058 /* True if we've been resumed, and so each record_full_wait call should
1059 advance execution. If this is false, record_full_wait will return a
1060 TARGET_WAITKIND_IGNORE. */
1061 static int record_full_resumed = 0;
1063 /* The execution direction of the last resume we got. This is
1064 necessary for async mode. Vis (order is not strictly accurate):
1066 1. user has the global execution direction set to forward
1067 2. user does a reverse-step command
1068 3. record_full_resume is called with global execution direction
1069 temporarily switched to reverse
1070 4. GDB's execution direction is reverted back to forward
1071 5. target record notifies event loop there's an event to handle
1072 6. infrun asks the target which direction was it going, and switches
1073 the global execution direction accordingly (to reverse)
1074 7. infrun polls an event out of the record target, and handles it
1075 8. GDB goes back to the event loop, and goto #4.
1077 static enum exec_direction_kind record_full_execution_dir = EXEC_FORWARD;
1079 /* "to_resume" target method. Resume the process record target. */
1081 static void
1082 record_full_resume (struct target_ops *ops, ptid_t ptid, int step,
1083 enum gdb_signal signal)
1085 record_full_resume_step = step;
1086 record_full_resumed = 1;
1087 record_full_execution_dir = execution_direction;
1089 if (!RECORD_FULL_IS_REPLAY)
1091 struct gdbarch *gdbarch = target_thread_architecture (ptid);
1093 record_full_message (get_current_regcache (), signal);
1095 if (!step)
1097 /* This is not hard single step. */
1098 if (!gdbarch_software_single_step_p (gdbarch))
1100 /* This is a normal continue. */
1101 step = 1;
1103 else
1105 /* This arch support soft sigle step. */
1106 if (single_step_breakpoints_inserted ())
1108 /* This is a soft single step. */
1109 record_full_resume_step = 1;
1111 else
1113 /* This is a continue.
1114 Try to insert a soft single step breakpoint. */
1115 if (!gdbarch_software_single_step (gdbarch,
1116 get_current_frame ()))
1118 /* This system don't want use soft single step.
1119 Use hard sigle step. */
1120 step = 1;
1126 /* Make sure the target beneath reports all signals. */
1127 target_pass_signals (0, NULL);
1129 record_full_beneath_to_resume (record_full_beneath_to_resume_ops,
1130 ptid, step, signal);
1133 /* We are about to start executing the inferior (or simulate it),
1134 let's register it with the event loop. */
1135 if (target_can_async_p ())
1137 target_async (inferior_event_handler, 0);
1138 /* Notify the event loop there's an event to wait for. We do
1139 most of the work in record_full_wait. */
1140 mark_async_event_handler (record_full_async_inferior_event_token);
1144 static int record_full_get_sig = 0;
1146 /* SIGINT signal handler, registered by "to_wait" method. */
1148 static void
1149 record_full_sig_handler (int signo)
1151 if (record_debug)
1152 fprintf_unfiltered (gdb_stdlog, "Process record: get a signal\n");
1154 /* It will break the running inferior in replay mode. */
1155 record_full_resume_step = 1;
1157 /* It will let record_full_wait set inferior status to get the signal
1158 SIGINT. */
1159 record_full_get_sig = 1;
1162 static void
1163 record_full_wait_cleanups (void *ignore)
1165 if (execution_direction == EXEC_REVERSE)
1167 if (record_full_list->next)
1168 record_full_list = record_full_list->next;
1170 else
1171 record_full_list = record_full_list->prev;
1174 /* "to_wait" target method for process record target.
1176 In record mode, the target is always run in singlestep mode
1177 (even when gdb says to continue). The to_wait method intercepts
1178 the stop events and determines which ones are to be passed on to
1179 gdb. Most stop events are just singlestep events that gdb is not
1180 to know about, so the to_wait method just records them and keeps
1181 singlestepping.
1183 In replay mode, this function emulates the recorded execution log,
1184 one instruction at a time (forward or backward), and determines
1185 where to stop. */
1187 static ptid_t
1188 record_full_wait_1 (struct target_ops *ops,
1189 ptid_t ptid, struct target_waitstatus *status,
1190 int options)
1192 struct cleanup *set_cleanups = record_full_gdb_operation_disable_set ();
1194 if (record_debug)
1195 fprintf_unfiltered (gdb_stdlog,
1196 "Process record: record_full_wait "
1197 "record_full_resume_step = %d, "
1198 "record_full_resumed = %d, direction=%s\n",
1199 record_full_resume_step, record_full_resumed,
1200 record_full_execution_dir == EXEC_FORWARD
1201 ? "forward" : "reverse");
1203 if (!record_full_resumed)
1205 gdb_assert ((options & TARGET_WNOHANG) != 0);
1207 /* No interesting event. */
1208 status->kind = TARGET_WAITKIND_IGNORE;
1209 return minus_one_ptid;
1212 record_full_get_sig = 0;
1213 signal (SIGINT, record_full_sig_handler);
1215 if (!RECORD_FULL_IS_REPLAY && ops != &record_full_core_ops)
1217 if (record_full_resume_step)
1219 /* This is a single step. */
1220 return record_full_beneath_to_wait (record_full_beneath_to_wait_ops,
1221 ptid, status, options);
1223 else
1225 /* This is not a single step. */
1226 ptid_t ret;
1227 CORE_ADDR tmp_pc;
1228 struct gdbarch *gdbarch = target_thread_architecture (inferior_ptid);
1230 while (1)
1232 ret = record_full_beneath_to_wait
1233 (record_full_beneath_to_wait_ops, ptid, status, options);
1234 if (status->kind == TARGET_WAITKIND_IGNORE)
1236 if (record_debug)
1237 fprintf_unfiltered (gdb_stdlog,
1238 "Process record: record_full_wait "
1239 "target beneath not done yet\n");
1240 return ret;
1243 if (single_step_breakpoints_inserted ())
1244 remove_single_step_breakpoints ();
1246 if (record_full_resume_step)
1247 return ret;
1249 /* Is this a SIGTRAP? */
1250 if (status->kind == TARGET_WAITKIND_STOPPED
1251 && status->value.sig == GDB_SIGNAL_TRAP)
1253 struct regcache *regcache;
1254 struct address_space *aspace;
1256 /* Yes -- this is likely our single-step finishing,
1257 but check if there's any reason the core would be
1258 interested in the event. */
1260 registers_changed ();
1261 regcache = get_current_regcache ();
1262 tmp_pc = regcache_read_pc (regcache);
1263 aspace = get_regcache_aspace (regcache);
1265 if (target_stopped_by_watchpoint ())
1267 /* Always interested in watchpoints. */
1269 else if (breakpoint_inserted_here_p (aspace, tmp_pc))
1271 /* There is a breakpoint here. Let the core
1272 handle it. */
1273 if (software_breakpoint_inserted_here_p (aspace, tmp_pc))
1275 struct gdbarch *gdbarch
1276 = get_regcache_arch (regcache);
1277 CORE_ADDR decr_pc_after_break
1278 = gdbarch_decr_pc_after_break (gdbarch);
1279 if (decr_pc_after_break)
1280 regcache_write_pc (regcache,
1281 tmp_pc + decr_pc_after_break);
1284 else
1286 /* This is a single-step trap. Record the
1287 insn and issue another step.
1288 FIXME: this part can be a random SIGTRAP too.
1289 But GDB cannot handle it. */
1290 int step = 1;
1292 if (!record_full_message_wrapper_safe (regcache,
1293 GDB_SIGNAL_0))
1295 status->kind = TARGET_WAITKIND_STOPPED;
1296 status->value.sig = GDB_SIGNAL_0;
1297 break;
1300 if (gdbarch_software_single_step_p (gdbarch))
1302 /* Try to insert the software single step breakpoint.
1303 If insert success, set step to 0. */
1304 set_executing (inferior_ptid, 0);
1305 reinit_frame_cache ();
1306 if (gdbarch_software_single_step (gdbarch,
1307 get_current_frame ()))
1308 step = 0;
1309 set_executing (inferior_ptid, 1);
1312 if (record_debug)
1313 fprintf_unfiltered (gdb_stdlog,
1314 "Process record: record_full_wait "
1315 "issuing one more step in the "
1316 "target beneath\n");
1317 record_full_beneath_to_resume
1318 (record_full_beneath_to_resume_ops, ptid, step,
1319 GDB_SIGNAL_0);
1320 continue;
1324 /* The inferior is broken by a breakpoint or a signal. */
1325 break;
1328 return ret;
1331 else
1333 struct regcache *regcache = get_current_regcache ();
1334 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1335 struct address_space *aspace = get_regcache_aspace (regcache);
1336 int continue_flag = 1;
1337 int first_record_full_end = 1;
1338 struct cleanup *old_cleanups
1339 = make_cleanup (record_full_wait_cleanups, 0);
1340 CORE_ADDR tmp_pc;
1342 record_full_hw_watchpoint = 0;
1343 status->kind = TARGET_WAITKIND_STOPPED;
1345 /* Check breakpoint when forward execute. */
1346 if (execution_direction == EXEC_FORWARD)
1348 tmp_pc = regcache_read_pc (regcache);
1349 if (breakpoint_inserted_here_p (aspace, tmp_pc))
1351 int decr_pc_after_break = gdbarch_decr_pc_after_break (gdbarch);
1353 if (record_debug)
1354 fprintf_unfiltered (gdb_stdlog,
1355 "Process record: break at %s.\n",
1356 paddress (gdbarch, tmp_pc));
1358 if (decr_pc_after_break
1359 && !record_full_resume_step
1360 && software_breakpoint_inserted_here_p (aspace, tmp_pc))
1361 regcache_write_pc (regcache,
1362 tmp_pc + decr_pc_after_break);
1363 goto replay_out;
1367 /* If GDB is in terminal_inferior mode, it will not get the signal.
1368 And in GDB replay mode, GDB doesn't need to be in terminal_inferior
1369 mode, because inferior will not executed.
1370 Then set it to terminal_ours to make GDB get the signal. */
1371 target_terminal_ours ();
1373 /* In EXEC_FORWARD mode, record_full_list points to the tail of prev
1374 instruction. */
1375 if (execution_direction == EXEC_FORWARD && record_full_list->next)
1376 record_full_list = record_full_list->next;
1378 /* Loop over the record_full_list, looking for the next place to
1379 stop. */
1382 /* Check for beginning and end of log. */
1383 if (execution_direction == EXEC_REVERSE
1384 && record_full_list == &record_full_first)
1386 /* Hit beginning of record log in reverse. */
1387 status->kind = TARGET_WAITKIND_NO_HISTORY;
1388 break;
1390 if (execution_direction != EXEC_REVERSE && !record_full_list->next)
1392 /* Hit end of record log going forward. */
1393 status->kind = TARGET_WAITKIND_NO_HISTORY;
1394 break;
1397 record_full_exec_insn (regcache, gdbarch, record_full_list);
1399 if (record_full_list->type == record_full_end)
1401 if (record_debug > 1)
1402 fprintf_unfiltered (gdb_stdlog,
1403 "Process record: record_full_end %s to "
1404 "inferior.\n",
1405 host_address_to_string (record_full_list));
1407 if (first_record_full_end && execution_direction == EXEC_REVERSE)
1409 /* When reverse excute, the first record_full_end is the
1410 part of current instruction. */
1411 first_record_full_end = 0;
1413 else
1415 /* In EXEC_REVERSE mode, this is the record_full_end of prev
1416 instruction.
1417 In EXEC_FORWARD mode, this is the record_full_end of
1418 current instruction. */
1419 /* step */
1420 if (record_full_resume_step)
1422 if (record_debug > 1)
1423 fprintf_unfiltered (gdb_stdlog,
1424 "Process record: step.\n");
1425 continue_flag = 0;
1428 /* check breakpoint */
1429 tmp_pc = regcache_read_pc (regcache);
1430 if (breakpoint_inserted_here_p (aspace, tmp_pc))
1432 int decr_pc_after_break
1433 = gdbarch_decr_pc_after_break (gdbarch);
1435 if (record_debug)
1436 fprintf_unfiltered (gdb_stdlog,
1437 "Process record: break "
1438 "at %s.\n",
1439 paddress (gdbarch, tmp_pc));
1440 if (decr_pc_after_break
1441 && execution_direction == EXEC_FORWARD
1442 && !record_full_resume_step
1443 && software_breakpoint_inserted_here_p (aspace,
1444 tmp_pc))
1445 regcache_write_pc (regcache,
1446 tmp_pc + decr_pc_after_break);
1447 continue_flag = 0;
1450 if (record_full_hw_watchpoint)
1452 if (record_debug)
1453 fprintf_unfiltered (gdb_stdlog,
1454 "Process record: hit hw "
1455 "watchpoint.\n");
1456 continue_flag = 0;
1458 /* Check target signal */
1459 if (record_full_list->u.end.sigval != GDB_SIGNAL_0)
1460 /* FIXME: better way to check */
1461 continue_flag = 0;
1465 if (continue_flag)
1467 if (execution_direction == EXEC_REVERSE)
1469 if (record_full_list->prev)
1470 record_full_list = record_full_list->prev;
1472 else
1474 if (record_full_list->next)
1475 record_full_list = record_full_list->next;
1479 while (continue_flag);
1481 replay_out:
1482 if (record_full_get_sig)
1483 status->value.sig = GDB_SIGNAL_INT;
1484 else if (record_full_list->u.end.sigval != GDB_SIGNAL_0)
1485 /* FIXME: better way to check */
1486 status->value.sig = record_full_list->u.end.sigval;
1487 else
1488 status->value.sig = GDB_SIGNAL_TRAP;
1490 discard_cleanups (old_cleanups);
1493 signal (SIGINT, handle_sigint);
1495 do_cleanups (set_cleanups);
1496 return inferior_ptid;
1499 static ptid_t
1500 record_full_wait (struct target_ops *ops,
1501 ptid_t ptid, struct target_waitstatus *status,
1502 int options)
1504 ptid_t return_ptid;
1506 return_ptid = record_full_wait_1 (ops, ptid, status, options);
1507 if (status->kind != TARGET_WAITKIND_IGNORE)
1509 /* We're reporting a stop. Make sure any spurious
1510 target_wait(WNOHANG) doesn't advance the target until the
1511 core wants us resumed again. */
1512 record_full_resumed = 0;
1514 return return_ptid;
1517 static int
1518 record_full_stopped_by_watchpoint (void)
1520 if (RECORD_FULL_IS_REPLAY)
1521 return record_full_hw_watchpoint;
1522 else
1523 return record_full_beneath_to_stopped_by_watchpoint ();
1526 static int
1527 record_full_stopped_data_address (struct target_ops *ops, CORE_ADDR *addr_p)
1529 if (RECORD_FULL_IS_REPLAY)
1530 return 0;
1531 else
1532 return record_full_beneath_to_stopped_data_address (ops, addr_p);
1535 /* Record registers change (by user or by GDB) to list as an instruction. */
1537 static void
1538 record_full_registers_change (struct regcache *regcache, int regnum)
1540 /* Check record_full_insn_num. */
1541 record_full_check_insn_num (0);
1543 record_full_arch_list_head = NULL;
1544 record_full_arch_list_tail = NULL;
1546 if (regnum < 0)
1548 int i;
1550 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
1552 if (record_full_arch_list_add_reg (regcache, i))
1554 record_full_list_release (record_full_arch_list_tail);
1555 error (_("Process record: failed to record execution log."));
1559 else
1561 if (record_full_arch_list_add_reg (regcache, regnum))
1563 record_full_list_release (record_full_arch_list_tail);
1564 error (_("Process record: failed to record execution log."));
1567 if (record_full_arch_list_add_end ())
1569 record_full_list_release (record_full_arch_list_tail);
1570 error (_("Process record: failed to record execution log."));
1572 record_full_list->next = record_full_arch_list_head;
1573 record_full_arch_list_head->prev = record_full_list;
1574 record_full_list = record_full_arch_list_tail;
1576 if (record_full_insn_num == record_full_insn_max_num
1577 && record_full_insn_max_num)
1578 record_full_list_release_first ();
1579 else
1580 record_full_insn_num++;
1583 /* "to_store_registers" method for process record target. */
1585 static void
1586 record_full_store_registers (struct target_ops *ops,
1587 struct regcache *regcache,
1588 int regno)
1590 if (!record_full_gdb_operation_disable)
1592 if (RECORD_FULL_IS_REPLAY)
1594 int n;
1596 /* Let user choose if he wants to write register or not. */
1597 if (regno < 0)
1599 query (_("Because GDB is in replay mode, changing the "
1600 "value of a register will make the execution "
1601 "log unusable from this point onward. "
1602 "Change all registers?"));
1603 else
1605 query (_("Because GDB is in replay mode, changing the value "
1606 "of a register will make the execution log unusable "
1607 "from this point onward. Change register %s?"),
1608 gdbarch_register_name (get_regcache_arch (regcache),
1609 regno));
1611 if (!n)
1613 /* Invalidate the value of regcache that was set in function
1614 "regcache_raw_write". */
1615 if (regno < 0)
1617 int i;
1619 for (i = 0;
1620 i < gdbarch_num_regs (get_regcache_arch (regcache));
1621 i++)
1622 regcache_invalidate (regcache, i);
1624 else
1625 regcache_invalidate (regcache, regno);
1627 error (_("Process record canceled the operation."));
1630 /* Destroy the record from here forward. */
1631 record_full_list_release_following (record_full_list);
1634 record_full_registers_change (regcache, regno);
1636 record_full_beneath_to_store_registers
1637 (record_full_beneath_to_store_registers_ops, regcache, regno);
1640 /* "to_xfer_partial" method. Behavior is conditional on
1641 RECORD_FULL_IS_REPLAY.
1642 In replay mode, we cannot write memory unles we are willing to
1643 invalidate the record/replay log from this point forward. */
1645 static LONGEST
1646 record_full_xfer_partial (struct target_ops *ops, enum target_object object,
1647 const char *annex, gdb_byte *readbuf,
1648 const gdb_byte *writebuf, ULONGEST offset,
1649 LONGEST len)
1651 if (!record_full_gdb_operation_disable
1652 && (object == TARGET_OBJECT_MEMORY
1653 || object == TARGET_OBJECT_RAW_MEMORY) && writebuf)
1655 if (RECORD_FULL_IS_REPLAY)
1657 /* Let user choose if he wants to write memory or not. */
1658 if (!query (_("Because GDB is in replay mode, writing to memory "
1659 "will make the execution log unusable from this "
1660 "point onward. Write memory at address %s?"),
1661 paddress (target_gdbarch (), offset)))
1662 error (_("Process record canceled the operation."));
1664 /* Destroy the record from here forward. */
1665 record_full_list_release_following (record_full_list);
1668 /* Check record_full_insn_num */
1669 record_full_check_insn_num (0);
1671 /* Record registers change to list as an instruction. */
1672 record_full_arch_list_head = NULL;
1673 record_full_arch_list_tail = NULL;
1674 if (record_full_arch_list_add_mem (offset, len))
1676 record_full_list_release (record_full_arch_list_tail);
1677 if (record_debug)
1678 fprintf_unfiltered (gdb_stdlog,
1679 "Process record: failed to record "
1680 "execution log.");
1681 return -1;
1683 if (record_full_arch_list_add_end ())
1685 record_full_list_release (record_full_arch_list_tail);
1686 if (record_debug)
1687 fprintf_unfiltered (gdb_stdlog,
1688 "Process record: failed to record "
1689 "execution log.");
1690 return -1;
1692 record_full_list->next = record_full_arch_list_head;
1693 record_full_arch_list_head->prev = record_full_list;
1694 record_full_list = record_full_arch_list_tail;
1696 if (record_full_insn_num == record_full_insn_max_num
1697 && record_full_insn_max_num)
1698 record_full_list_release_first ();
1699 else
1700 record_full_insn_num++;
1703 return record_full_beneath_to_xfer_partial
1704 (record_full_beneath_to_xfer_partial_ops, object, annex,
1705 readbuf, writebuf, offset, len);
1708 /* This structure represents a breakpoint inserted while the record
1709 target is active. We use this to know when to install/remove
1710 breakpoints in/from the target beneath. For example, a breakpoint
1711 may be inserted while recording, but removed when not replaying nor
1712 recording. In that case, the breakpoint had not been inserted on
1713 the target beneath, so we should not try to remove it there. */
1715 struct record_full_breakpoint
1717 /* The address and address space the breakpoint was set at. */
1718 struct address_space *address_space;
1719 CORE_ADDR addr;
1721 /* True when the breakpoint has been also installed in the target
1722 beneath. This will be false for breakpoints set during replay or
1723 when recording. */
1724 int in_target_beneath;
1727 typedef struct record_full_breakpoint *record_full_breakpoint_p;
1728 DEF_VEC_P(record_full_breakpoint_p);
1730 /* The list of breakpoints inserted while the record target is
1731 active. */
1732 VEC(record_full_breakpoint_p) *record_full_breakpoints = NULL;
1734 static void
1735 record_full_sync_record_breakpoints (struct bp_location *loc, void *data)
1737 if (loc->loc_type != bp_loc_software_breakpoint)
1738 return;
1740 if (loc->inserted)
1742 struct record_full_breakpoint *bp = XNEW (struct record_full_breakpoint);
1744 bp->addr = loc->target_info.placed_address;
1745 bp->address_space = loc->target_info.placed_address_space;
1747 bp->in_target_beneath = 1;
1749 VEC_safe_push (record_full_breakpoint_p, record_full_breakpoints, bp);
1753 /* Sync existing breakpoints to record_full_breakpoints. */
1755 static void
1756 record_full_init_record_breakpoints (void)
1758 VEC_free (record_full_breakpoint_p, record_full_breakpoints);
1760 iterate_over_bp_locations (record_full_sync_record_breakpoints);
1763 /* Behavior is conditional on RECORD_FULL_IS_REPLAY. We will not actually
1764 insert or remove breakpoints in the real target when replaying, nor
1765 when recording. */
1767 static int
1768 record_full_insert_breakpoint (struct gdbarch *gdbarch,
1769 struct bp_target_info *bp_tgt)
1771 struct record_full_breakpoint *bp;
1772 int in_target_beneath = 0;
1774 if (!RECORD_FULL_IS_REPLAY)
1776 /* When recording, we currently always single-step, so we don't
1777 really need to install regular breakpoints in the inferior.
1778 However, we do have to insert software single-step
1779 breakpoints, in case the target can't hardware step. To keep
1780 things single, we always insert. */
1781 struct cleanup *old_cleanups;
1782 int ret;
1784 old_cleanups = record_full_gdb_operation_disable_set ();
1785 ret = record_full_beneath_to_insert_breakpoint (gdbarch, bp_tgt);
1786 do_cleanups (old_cleanups);
1788 if (ret != 0)
1789 return ret;
1791 in_target_beneath = 1;
1794 bp = XNEW (struct record_full_breakpoint);
1795 bp->addr = bp_tgt->placed_address;
1796 bp->address_space = bp_tgt->placed_address_space;
1797 bp->in_target_beneath = in_target_beneath;
1798 VEC_safe_push (record_full_breakpoint_p, record_full_breakpoints, bp);
1799 return 0;
1802 /* "to_remove_breakpoint" method for process record target. */
1804 static int
1805 record_full_remove_breakpoint (struct gdbarch *gdbarch,
1806 struct bp_target_info *bp_tgt)
1808 struct record_full_breakpoint *bp;
1809 int ix;
1811 for (ix = 0;
1812 VEC_iterate (record_full_breakpoint_p,
1813 record_full_breakpoints, ix, bp);
1814 ++ix)
1816 if (bp->addr == bp_tgt->placed_address
1817 && bp->address_space == bp_tgt->placed_address_space)
1819 if (bp->in_target_beneath)
1821 struct cleanup *old_cleanups;
1822 int ret;
1824 old_cleanups = record_full_gdb_operation_disable_set ();
1825 ret = record_full_beneath_to_remove_breakpoint (gdbarch, bp_tgt);
1826 do_cleanups (old_cleanups);
1828 if (ret != 0)
1829 return ret;
1832 VEC_unordered_remove (record_full_breakpoint_p,
1833 record_full_breakpoints, ix);
1834 return 0;
1838 gdb_assert_not_reached ("removing unknown breakpoint");
1841 /* "to_can_execute_reverse" method for process record target. */
1843 static int
1844 record_full_can_execute_reverse (void)
1846 return 1;
1849 /* "to_get_bookmark" method for process record and prec over core. */
1851 static gdb_byte *
1852 record_full_get_bookmark (char *args, int from_tty)
1854 gdb_byte *ret = NULL;
1856 /* Return stringified form of instruction count. */
1857 if (record_full_list && record_full_list->type == record_full_end)
1858 ret = xstrdup (pulongest (record_full_list->u.end.insn_num));
1860 if (record_debug)
1862 if (ret)
1863 fprintf_unfiltered (gdb_stdlog,
1864 "record_full_get_bookmark returns %s\n", ret);
1865 else
1866 fprintf_unfiltered (gdb_stdlog,
1867 "record_full_get_bookmark returns NULL\n");
1869 return ret;
1872 /* "to_goto_bookmark" method for process record and prec over core. */
1874 static void
1875 record_full_goto_bookmark (gdb_byte *bookmark, int from_tty)
1877 if (record_debug)
1878 fprintf_unfiltered (gdb_stdlog,
1879 "record_full_goto_bookmark receives %s\n", bookmark);
1881 if (bookmark[0] == '\'' || bookmark[0] == '\"')
1883 if (bookmark[strlen (bookmark) - 1] != bookmark[0])
1884 error (_("Unbalanced quotes: %s"), bookmark);
1886 /* Strip trailing quote. */
1887 bookmark[strlen (bookmark) - 1] = '\0';
1888 /* Strip leading quote. */
1889 bookmark++;
1890 /* Pass along to cmd_record_full_goto. */
1893 cmd_record_goto ((char *) bookmark, from_tty);
1894 return;
1897 static void
1898 record_full_async (void (*callback) (enum inferior_event_type event_type,
1899 void *context), void *context)
1901 /* If we're on top of a line target (e.g., linux-nat, remote), then
1902 set it to async mode as well. Will be NULL if we're sitting on
1903 top of the core target, for "record restore". */
1904 if (record_full_beneath_to_async != NULL)
1905 record_full_beneath_to_async (callback, context);
1908 static int
1909 record_full_can_async_p (void)
1911 /* We only enable async when the user specifically asks for it. */
1912 return target_async_permitted;
1915 static int
1916 record_full_is_async_p (void)
1918 /* We only enable async when the user specifically asks for it. */
1919 return target_async_permitted;
1922 static enum exec_direction_kind
1923 record_full_execution_direction (void)
1925 return record_full_execution_dir;
1928 static void
1929 record_full_info (void)
1931 struct record_full_entry *p;
1933 if (RECORD_FULL_IS_REPLAY)
1934 printf_filtered (_("Replay mode:\n"));
1935 else
1936 printf_filtered (_("Record mode:\n"));
1938 /* Find entry for first actual instruction in the log. */
1939 for (p = record_full_first.next;
1940 p != NULL && p->type != record_full_end;
1941 p = p->next)
1944 /* Do we have a log at all? */
1945 if (p != NULL && p->type == record_full_end)
1947 /* Display instruction number for first instruction in the log. */
1948 printf_filtered (_("Lowest recorded instruction number is %s.\n"),
1949 pulongest (p->u.end.insn_num));
1951 /* If in replay mode, display where we are in the log. */
1952 if (RECORD_FULL_IS_REPLAY)
1953 printf_filtered (_("Current instruction number is %s.\n"),
1954 pulongest (record_full_list->u.end.insn_num));
1956 /* Display instruction number for last instruction in the log. */
1957 printf_filtered (_("Highest recorded instruction number is %s.\n"),
1958 pulongest (record_full_insn_count));
1960 /* Display log count. */
1961 printf_filtered (_("Log contains %d instructions.\n"),
1962 record_full_insn_num);
1964 else
1965 printf_filtered (_("No instructions have been logged.\n"));
1967 /* Display max log size. */
1968 printf_filtered (_("Max logged instructions is %d.\n"),
1969 record_full_insn_max_num);
1972 /* The "to_record_delete" target method. */
1974 static void
1975 record_full_delete (void)
1977 record_full_list_release_following (record_full_list);
1980 /* The "to_record_is_replaying" target method. */
1982 static int
1983 record_full_is_replaying (void)
1985 return RECORD_FULL_IS_REPLAY;
1988 /* Go to a specific entry. */
1990 static void
1991 record_full_goto_entry (struct record_full_entry *p)
1993 if (p == NULL)
1994 error (_("Target insn not found."));
1995 else if (p == record_full_list)
1996 error (_("Already at target insn."));
1997 else if (p->u.end.insn_num > record_full_list->u.end.insn_num)
1999 printf_filtered (_("Go forward to insn number %s\n"),
2000 pulongest (p->u.end.insn_num));
2001 record_full_goto_insn (p, EXEC_FORWARD);
2003 else
2005 printf_filtered (_("Go backward to insn number %s\n"),
2006 pulongest (p->u.end.insn_num));
2007 record_full_goto_insn (p, EXEC_REVERSE);
2010 registers_changed ();
2011 reinit_frame_cache ();
2012 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
2015 /* The "to_goto_record_begin" target method. */
2017 static void
2018 record_full_goto_begin (void)
2020 struct record_full_entry *p = NULL;
2022 for (p = &record_full_first; p != NULL; p = p->next)
2023 if (p->type == record_full_end)
2024 break;
2026 record_full_goto_entry (p);
2029 /* The "to_goto_record_end" target method. */
2031 static void
2032 record_full_goto_end (void)
2034 struct record_full_entry *p = NULL;
2036 for (p = record_full_list; p->next != NULL; p = p->next)
2038 for (; p!= NULL; p = p->prev)
2039 if (p->type == record_full_end)
2040 break;
2042 record_full_goto_entry (p);
2045 /* The "to_goto_record" target method. */
2047 static void
2048 record_full_goto (ULONGEST target_insn)
2050 struct record_full_entry *p = NULL;
2052 for (p = &record_full_first; p != NULL; p = p->next)
2053 if (p->type == record_full_end && p->u.end.insn_num == target_insn)
2054 break;
2056 record_full_goto_entry (p);
2059 static void
2060 init_record_full_ops (void)
2062 record_full_ops.to_shortname = "record-full";
2063 record_full_ops.to_longname = "Process record and replay target";
2064 record_full_ops.to_doc =
2065 "Log program while executing and replay execution from log.";
2066 record_full_ops.to_open = record_full_open;
2067 record_full_ops.to_close = record_full_close;
2068 record_full_ops.to_resume = record_full_resume;
2069 record_full_ops.to_wait = record_full_wait;
2070 record_full_ops.to_disconnect = record_disconnect;
2071 record_full_ops.to_detach = record_detach;
2072 record_full_ops.to_mourn_inferior = record_mourn_inferior;
2073 record_full_ops.to_kill = record_kill;
2074 record_full_ops.to_create_inferior = find_default_create_inferior;
2075 record_full_ops.to_store_registers = record_full_store_registers;
2076 record_full_ops.to_xfer_partial = record_full_xfer_partial;
2077 record_full_ops.to_insert_breakpoint = record_full_insert_breakpoint;
2078 record_full_ops.to_remove_breakpoint = record_full_remove_breakpoint;
2079 record_full_ops.to_stopped_by_watchpoint = record_full_stopped_by_watchpoint;
2080 record_full_ops.to_stopped_data_address = record_full_stopped_data_address;
2081 record_full_ops.to_can_execute_reverse = record_full_can_execute_reverse;
2082 record_full_ops.to_stratum = record_stratum;
2083 /* Add bookmark target methods. */
2084 record_full_ops.to_get_bookmark = record_full_get_bookmark;
2085 record_full_ops.to_goto_bookmark = record_full_goto_bookmark;
2086 record_full_ops.to_async = record_full_async;
2087 record_full_ops.to_can_async_p = record_full_can_async_p;
2088 record_full_ops.to_is_async_p = record_full_is_async_p;
2089 record_full_ops.to_execution_direction = record_full_execution_direction;
2090 record_full_ops.to_info_record = record_full_info;
2091 record_full_ops.to_save_record = record_full_save;
2092 record_full_ops.to_delete_record = record_full_delete;
2093 record_full_ops.to_record_is_replaying = record_full_is_replaying;
2094 record_full_ops.to_goto_record_begin = record_full_goto_begin;
2095 record_full_ops.to_goto_record_end = record_full_goto_end;
2096 record_full_ops.to_goto_record = record_full_goto;
2097 record_full_ops.to_magic = OPS_MAGIC;
2100 /* "to_resume" method for prec over corefile. */
2102 static void
2103 record_full_core_resume (struct target_ops *ops, ptid_t ptid, int step,
2104 enum gdb_signal signal)
2106 record_full_resume_step = step;
2107 record_full_resumed = 1;
2108 record_full_execution_dir = execution_direction;
2110 /* We are about to start executing the inferior (or simulate it),
2111 let's register it with the event loop. */
2112 if (target_can_async_p ())
2114 target_async (inferior_event_handler, 0);
2116 /* Notify the event loop there's an event to wait for. */
2117 mark_async_event_handler (record_full_async_inferior_event_token);
2121 /* "to_kill" method for prec over corefile. */
2123 static void
2124 record_full_core_kill (struct target_ops *ops)
2126 if (record_debug)
2127 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_core_kill\n");
2129 unpush_target (&record_full_core_ops);
2132 /* "to_fetch_registers" method for prec over corefile. */
2134 static void
2135 record_full_core_fetch_registers (struct target_ops *ops,
2136 struct regcache *regcache,
2137 int regno)
2139 if (regno < 0)
2141 int num = gdbarch_num_regs (get_regcache_arch (regcache));
2142 int i;
2144 for (i = 0; i < num; i ++)
2145 regcache_raw_supply (regcache, i,
2146 record_full_core_regbuf + MAX_REGISTER_SIZE * i);
2148 else
2149 regcache_raw_supply (regcache, regno,
2150 record_full_core_regbuf + MAX_REGISTER_SIZE * regno);
2153 /* "to_prepare_to_store" method for prec over corefile. */
2155 static void
2156 record_full_core_prepare_to_store (struct regcache *regcache)
2160 /* "to_store_registers" method for prec over corefile. */
2162 static void
2163 record_full_core_store_registers (struct target_ops *ops,
2164 struct regcache *regcache,
2165 int regno)
2167 if (record_full_gdb_operation_disable)
2168 regcache_raw_collect (regcache, regno,
2169 record_full_core_regbuf + MAX_REGISTER_SIZE * regno);
2170 else
2171 error (_("You can't do that without a process to debug."));
2174 /* "to_xfer_partial" method for prec over corefile. */
2176 static LONGEST
2177 record_full_core_xfer_partial (struct target_ops *ops,
2178 enum target_object object,
2179 const char *annex, gdb_byte *readbuf,
2180 const gdb_byte *writebuf, ULONGEST offset,
2181 LONGEST len)
2183 if (object == TARGET_OBJECT_MEMORY)
2185 if (record_full_gdb_operation_disable || !writebuf)
2187 struct target_section *p;
2189 for (p = record_full_core_start; p < record_full_core_end; p++)
2191 if (offset >= p->addr)
2193 struct record_full_core_buf_entry *entry;
2194 ULONGEST sec_offset;
2196 if (offset >= p->endaddr)
2197 continue;
2199 if (offset + len > p->endaddr)
2200 len = p->endaddr - offset;
2202 sec_offset = offset - p->addr;
2204 /* Read readbuf or write writebuf p, offset, len. */
2205 /* Check flags. */
2206 if (p->the_bfd_section->flags & SEC_CONSTRUCTOR
2207 || (p->the_bfd_section->flags & SEC_HAS_CONTENTS) == 0)
2209 if (readbuf)
2210 memset (readbuf, 0, len);
2211 return len;
2213 /* Get record_full_core_buf_entry. */
2214 for (entry = record_full_core_buf_list; entry;
2215 entry = entry->prev)
2216 if (entry->p == p)
2217 break;
2218 if (writebuf)
2220 if (!entry)
2222 /* Add a new entry. */
2223 entry = (struct record_full_core_buf_entry *)
2224 xmalloc
2225 (sizeof (struct record_full_core_buf_entry));
2226 entry->p = p;
2227 if (!bfd_malloc_and_get_section (p->bfd,
2228 p->the_bfd_section,
2229 &entry->buf))
2231 xfree (entry);
2232 return 0;
2234 entry->prev = record_full_core_buf_list;
2235 record_full_core_buf_list = entry;
2238 memcpy (entry->buf + sec_offset, writebuf,
2239 (size_t) len);
2241 else
2243 if (!entry)
2244 return record_full_beneath_to_xfer_partial
2245 (record_full_beneath_to_xfer_partial_ops,
2246 object, annex, readbuf, writebuf,
2247 offset, len);
2249 memcpy (readbuf, entry->buf + sec_offset,
2250 (size_t) len);
2253 return len;
2257 return -1;
2259 else
2260 error (_("You can't do that without a process to debug."));
2263 return record_full_beneath_to_xfer_partial
2264 (record_full_beneath_to_xfer_partial_ops, object, annex,
2265 readbuf, writebuf, offset, len);
2268 /* "to_insert_breakpoint" method for prec over corefile. */
2270 static int
2271 record_full_core_insert_breakpoint (struct gdbarch *gdbarch,
2272 struct bp_target_info *bp_tgt)
2274 return 0;
2277 /* "to_remove_breakpoint" method for prec over corefile. */
2279 static int
2280 record_full_core_remove_breakpoint (struct gdbarch *gdbarch,
2281 struct bp_target_info *bp_tgt)
2283 return 0;
2286 /* "to_has_execution" method for prec over corefile. */
2288 static int
2289 record_full_core_has_execution (struct target_ops *ops, ptid_t the_ptid)
2291 return 1;
2294 static void
2295 init_record_full_core_ops (void)
2297 record_full_core_ops.to_shortname = "record-core";
2298 record_full_core_ops.to_longname = "Process record and replay target";
2299 record_full_core_ops.to_doc =
2300 "Log program while executing and replay execution from log.";
2301 record_full_core_ops.to_open = record_full_open;
2302 record_full_core_ops.to_close = record_full_close;
2303 record_full_core_ops.to_resume = record_full_core_resume;
2304 record_full_core_ops.to_wait = record_full_wait;
2305 record_full_core_ops.to_kill = record_full_core_kill;
2306 record_full_core_ops.to_fetch_registers = record_full_core_fetch_registers;
2307 record_full_core_ops.to_prepare_to_store = record_full_core_prepare_to_store;
2308 record_full_core_ops.to_store_registers = record_full_core_store_registers;
2309 record_full_core_ops.to_xfer_partial = record_full_core_xfer_partial;
2310 record_full_core_ops.to_insert_breakpoint
2311 = record_full_core_insert_breakpoint;
2312 record_full_core_ops.to_remove_breakpoint
2313 = record_full_core_remove_breakpoint;
2314 record_full_core_ops.to_stopped_by_watchpoint
2315 = record_full_stopped_by_watchpoint;
2316 record_full_core_ops.to_stopped_data_address
2317 = record_full_stopped_data_address;
2318 record_full_core_ops.to_can_execute_reverse
2319 = record_full_can_execute_reverse;
2320 record_full_core_ops.to_has_execution = record_full_core_has_execution;
2321 record_full_core_ops.to_stratum = record_stratum;
2322 /* Add bookmark target methods. */
2323 record_full_core_ops.to_get_bookmark = record_full_get_bookmark;
2324 record_full_core_ops.to_goto_bookmark = record_full_goto_bookmark;
2325 record_full_core_ops.to_async = record_full_async;
2326 record_full_core_ops.to_can_async_p = record_full_can_async_p;
2327 record_full_core_ops.to_is_async_p = record_full_is_async_p;
2328 record_full_core_ops.to_execution_direction
2329 = record_full_execution_direction;
2330 record_full_core_ops.to_info_record = record_full_info;
2331 record_full_core_ops.to_delete_record = record_full_delete;
2332 record_full_core_ops.to_record_is_replaying = record_full_is_replaying;
2333 record_full_core_ops.to_goto_record_begin = record_full_goto_begin;
2334 record_full_core_ops.to_goto_record_end = record_full_goto_end;
2335 record_full_core_ops.to_goto_record = record_full_goto;
2336 record_full_core_ops.to_magic = OPS_MAGIC;
2339 /* Record log save-file format
2340 Version 1 (never released)
2342 Header:
2343 4 bytes: magic number htonl(0x20090829).
2344 NOTE: be sure to change whenever this file format changes!
2346 Records:
2347 record_full_end:
2348 1 byte: record type (record_full_end, see enum record_full_type).
2349 record_full_reg:
2350 1 byte: record type (record_full_reg, see enum record_full_type).
2351 8 bytes: register id (network byte order).
2352 MAX_REGISTER_SIZE bytes: register value.
2353 record_full_mem:
2354 1 byte: record type (record_full_mem, see enum record_full_type).
2355 8 bytes: memory length (network byte order).
2356 8 bytes: memory address (network byte order).
2357 n bytes: memory value (n == memory length).
2359 Version 2
2360 4 bytes: magic number netorder32(0x20091016).
2361 NOTE: be sure to change whenever this file format changes!
2363 Records:
2364 record_full_end:
2365 1 byte: record type (record_full_end, see enum record_full_type).
2366 4 bytes: signal
2367 4 bytes: instruction count
2368 record_full_reg:
2369 1 byte: record type (record_full_reg, see enum record_full_type).
2370 4 bytes: register id (network byte order).
2371 n bytes: register value (n == actual register size).
2372 (eg. 4 bytes for x86 general registers).
2373 record_full_mem:
2374 1 byte: record type (record_full_mem, see enum record_full_type).
2375 4 bytes: memory length (network byte order).
2376 8 bytes: memory address (network byte order).
2377 n bytes: memory value (n == memory length).
2381 /* bfdcore_read -- read bytes from a core file section. */
2383 static inline void
2384 bfdcore_read (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2386 int ret = bfd_get_section_contents (obfd, osec, buf, *offset, len);
2388 if (ret)
2389 *offset += len;
2390 else
2391 error (_("Failed to read %d bytes from core file %s ('%s')."),
2392 len, bfd_get_filename (obfd),
2393 bfd_errmsg (bfd_get_error ()));
2396 static inline uint64_t
2397 netorder64 (uint64_t input)
2399 uint64_t ret;
2401 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2402 BFD_ENDIAN_BIG, input);
2403 return ret;
2406 static inline uint32_t
2407 netorder32 (uint32_t input)
2409 uint32_t ret;
2411 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2412 BFD_ENDIAN_BIG, input);
2413 return ret;
2416 static inline uint16_t
2417 netorder16 (uint16_t input)
2419 uint16_t ret;
2421 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2422 BFD_ENDIAN_BIG, input);
2423 return ret;
2426 /* Restore the execution log from a core_bfd file. */
2427 static void
2428 record_full_restore (void)
2430 uint32_t magic;
2431 struct cleanup *old_cleanups;
2432 struct record_full_entry *rec;
2433 asection *osec;
2434 uint32_t osec_size;
2435 int bfd_offset = 0;
2436 struct regcache *regcache;
2438 /* We restore the execution log from the open core bfd,
2439 if there is one. */
2440 if (core_bfd == NULL)
2441 return;
2443 /* "record_full_restore" can only be called when record list is empty. */
2444 gdb_assert (record_full_first.next == NULL);
2446 if (record_debug)
2447 fprintf_unfiltered (gdb_stdlog, "Restoring recording from core file.\n");
2449 /* Now need to find our special note section. */
2450 osec = bfd_get_section_by_name (core_bfd, "null0");
2451 if (record_debug)
2452 fprintf_unfiltered (gdb_stdlog, "Find precord section %s.\n",
2453 osec ? "succeeded" : "failed");
2454 if (osec == NULL)
2455 return;
2456 osec_size = bfd_section_size (core_bfd, osec);
2457 if (record_debug)
2458 fprintf_unfiltered (gdb_stdlog, "%s", bfd_section_name (core_bfd, osec));
2460 /* Check the magic code. */
2461 bfdcore_read (core_bfd, osec, &magic, sizeof (magic), &bfd_offset);
2462 if (magic != RECORD_FULL_FILE_MAGIC)
2463 error (_("Version mis-match or file format error in core file %s."),
2464 bfd_get_filename (core_bfd));
2465 if (record_debug)
2466 fprintf_unfiltered (gdb_stdlog,
2467 " Reading 4-byte magic cookie "
2468 "RECORD_FULL_FILE_MAGIC (0x%s)\n",
2469 phex_nz (netorder32 (magic), 4));
2471 /* Restore the entries in recfd into record_full_arch_list_head and
2472 record_full_arch_list_tail. */
2473 record_full_arch_list_head = NULL;
2474 record_full_arch_list_tail = NULL;
2475 record_full_insn_num = 0;
2476 old_cleanups = make_cleanup (record_full_arch_list_cleanups, 0);
2477 regcache = get_current_regcache ();
2479 while (1)
2481 uint8_t rectype;
2482 uint32_t regnum, len, signal, count;
2483 uint64_t addr;
2485 /* We are finished when offset reaches osec_size. */
2486 if (bfd_offset >= osec_size)
2487 break;
2488 bfdcore_read (core_bfd, osec, &rectype, sizeof (rectype), &bfd_offset);
2490 switch (rectype)
2492 case record_full_reg: /* reg */
2493 /* Get register number to regnum. */
2494 bfdcore_read (core_bfd, osec, &regnum,
2495 sizeof (regnum), &bfd_offset);
2496 regnum = netorder32 (regnum);
2498 rec = record_full_reg_alloc (regcache, regnum);
2500 /* Get val. */
2501 bfdcore_read (core_bfd, osec, record_full_get_loc (rec),
2502 rec->u.reg.len, &bfd_offset);
2504 if (record_debug)
2505 fprintf_unfiltered (gdb_stdlog,
2506 " Reading register %d (1 "
2507 "plus %lu plus %d bytes)\n",
2508 rec->u.reg.num,
2509 (unsigned long) sizeof (regnum),
2510 rec->u.reg.len);
2511 break;
2513 case record_full_mem: /* mem */
2514 /* Get len. */
2515 bfdcore_read (core_bfd, osec, &len,
2516 sizeof (len), &bfd_offset);
2517 len = netorder32 (len);
2519 /* Get addr. */
2520 bfdcore_read (core_bfd, osec, &addr,
2521 sizeof (addr), &bfd_offset);
2522 addr = netorder64 (addr);
2524 rec = record_full_mem_alloc (addr, len);
2526 /* Get val. */
2527 bfdcore_read (core_bfd, osec, record_full_get_loc (rec),
2528 rec->u.mem.len, &bfd_offset);
2530 if (record_debug)
2531 fprintf_unfiltered (gdb_stdlog,
2532 " Reading memory %s (1 plus "
2533 "%lu plus %lu plus %d bytes)\n",
2534 paddress (get_current_arch (),
2535 rec->u.mem.addr),
2536 (unsigned long) sizeof (addr),
2537 (unsigned long) sizeof (len),
2538 rec->u.mem.len);
2539 break;
2541 case record_full_end: /* end */
2542 rec = record_full_end_alloc ();
2543 record_full_insn_num ++;
2545 /* Get signal value. */
2546 bfdcore_read (core_bfd, osec, &signal,
2547 sizeof (signal), &bfd_offset);
2548 signal = netorder32 (signal);
2549 rec->u.end.sigval = signal;
2551 /* Get insn count. */
2552 bfdcore_read (core_bfd, osec, &count,
2553 sizeof (count), &bfd_offset);
2554 count = netorder32 (count);
2555 rec->u.end.insn_num = count;
2556 record_full_insn_count = count + 1;
2557 if (record_debug)
2558 fprintf_unfiltered (gdb_stdlog,
2559 " Reading record_full_end (1 + "
2560 "%lu + %lu bytes), offset == %s\n",
2561 (unsigned long) sizeof (signal),
2562 (unsigned long) sizeof (count),
2563 paddress (get_current_arch (),
2564 bfd_offset));
2565 break;
2567 default:
2568 error (_("Bad entry type in core file %s."),
2569 bfd_get_filename (core_bfd));
2570 break;
2573 /* Add rec to record arch list. */
2574 record_full_arch_list_add (rec);
2577 discard_cleanups (old_cleanups);
2579 /* Add record_full_arch_list_head to the end of record list. */
2580 record_full_first.next = record_full_arch_list_head;
2581 record_full_arch_list_head->prev = &record_full_first;
2582 record_full_arch_list_tail->next = NULL;
2583 record_full_list = &record_full_first;
2585 /* Update record_full_insn_max_num. */
2586 if (record_full_insn_num > record_full_insn_max_num)
2588 record_full_insn_max_num = record_full_insn_num;
2589 warning (_("Auto increase record/replay buffer limit to %d."),
2590 record_full_insn_max_num);
2593 /* Succeeded. */
2594 printf_filtered (_("Restored records from core file %s.\n"),
2595 bfd_get_filename (core_bfd));
2597 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
2600 /* bfdcore_write -- write bytes into a core file section. */
2602 static inline void
2603 bfdcore_write (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2605 int ret = bfd_set_section_contents (obfd, osec, buf, *offset, len);
2607 if (ret)
2608 *offset += len;
2609 else
2610 error (_("Failed to write %d bytes to core file %s ('%s')."),
2611 len, bfd_get_filename (obfd),
2612 bfd_errmsg (bfd_get_error ()));
2615 /* Restore the execution log from a file. We use a modified elf
2616 corefile format, with an extra section for our data. */
2618 static void
2619 cmd_record_full_restore (char *args, int from_tty)
2621 core_file_command (args, from_tty);
2622 record_full_open (args, from_tty);
2625 static void
2626 record_full_save_cleanups (void *data)
2628 bfd *obfd = data;
2629 char *pathname = xstrdup (bfd_get_filename (obfd));
2631 gdb_bfd_unref (obfd);
2632 unlink (pathname);
2633 xfree (pathname);
2636 /* Save the execution log to a file. We use a modified elf corefile
2637 format, with an extra section for our data. */
2639 static void
2640 record_full_save (char *recfilename)
2642 struct record_full_entry *cur_record_full_list;
2643 uint32_t magic;
2644 struct regcache *regcache;
2645 struct gdbarch *gdbarch;
2646 struct cleanup *old_cleanups;
2647 struct cleanup *set_cleanups;
2648 bfd *obfd;
2649 int save_size = 0;
2650 asection *osec = NULL;
2651 int bfd_offset = 0;
2653 /* Open the save file. */
2654 if (record_debug)
2655 fprintf_unfiltered (gdb_stdlog, "Saving execution log to core file '%s'\n",
2656 recfilename);
2658 /* Open the output file. */
2659 obfd = create_gcore_bfd (recfilename);
2660 old_cleanups = make_cleanup (record_full_save_cleanups, obfd);
2662 /* Save the current record entry to "cur_record_full_list". */
2663 cur_record_full_list = record_full_list;
2665 /* Get the values of regcache and gdbarch. */
2666 regcache = get_current_regcache ();
2667 gdbarch = get_regcache_arch (regcache);
2669 /* Disable the GDB operation record. */
2670 set_cleanups = record_full_gdb_operation_disable_set ();
2672 /* Reverse execute to the begin of record list. */
2673 while (1)
2675 /* Check for beginning and end of log. */
2676 if (record_full_list == &record_full_first)
2677 break;
2679 record_full_exec_insn (regcache, gdbarch, record_full_list);
2681 if (record_full_list->prev)
2682 record_full_list = record_full_list->prev;
2685 /* Compute the size needed for the extra bfd section. */
2686 save_size = 4; /* magic cookie */
2687 for (record_full_list = record_full_first.next; record_full_list;
2688 record_full_list = record_full_list->next)
2689 switch (record_full_list->type)
2691 case record_full_end:
2692 save_size += 1 + 4 + 4;
2693 break;
2694 case record_full_reg:
2695 save_size += 1 + 4 + record_full_list->u.reg.len;
2696 break;
2697 case record_full_mem:
2698 save_size += 1 + 4 + 8 + record_full_list->u.mem.len;
2699 break;
2702 /* Make the new bfd section. */
2703 osec = bfd_make_section_anyway_with_flags (obfd, "precord",
2704 SEC_HAS_CONTENTS
2705 | SEC_READONLY);
2706 if (osec == NULL)
2707 error (_("Failed to create 'precord' section for corefile %s: %s"),
2708 recfilename,
2709 bfd_errmsg (bfd_get_error ()));
2710 bfd_set_section_size (obfd, osec, save_size);
2711 bfd_set_section_vma (obfd, osec, 0);
2712 bfd_set_section_alignment (obfd, osec, 0);
2713 bfd_section_lma (obfd, osec) = 0;
2715 /* Save corefile state. */
2716 write_gcore_file (obfd);
2718 /* Write out the record log. */
2719 /* Write the magic code. */
2720 magic = RECORD_FULL_FILE_MAGIC;
2721 if (record_debug)
2722 fprintf_unfiltered (gdb_stdlog,
2723 " Writing 4-byte magic cookie "
2724 "RECORD_FULL_FILE_MAGIC (0x%s)\n",
2725 phex_nz (magic, 4));
2726 bfdcore_write (obfd, osec, &magic, sizeof (magic), &bfd_offset);
2728 /* Save the entries to recfd and forward execute to the end of
2729 record list. */
2730 record_full_list = &record_full_first;
2731 while (1)
2733 /* Save entry. */
2734 if (record_full_list != &record_full_first)
2736 uint8_t type;
2737 uint32_t regnum, len, signal, count;
2738 uint64_t addr;
2740 type = record_full_list->type;
2741 bfdcore_write (obfd, osec, &type, sizeof (type), &bfd_offset);
2743 switch (record_full_list->type)
2745 case record_full_reg: /* reg */
2746 if (record_debug)
2747 fprintf_unfiltered (gdb_stdlog,
2748 " Writing register %d (1 "
2749 "plus %lu plus %d bytes)\n",
2750 record_full_list->u.reg.num,
2751 (unsigned long) sizeof (regnum),
2752 record_full_list->u.reg.len);
2754 /* Write regnum. */
2755 regnum = netorder32 (record_full_list->u.reg.num);
2756 bfdcore_write (obfd, osec, &regnum,
2757 sizeof (regnum), &bfd_offset);
2759 /* Write regval. */
2760 bfdcore_write (obfd, osec,
2761 record_full_get_loc (record_full_list),
2762 record_full_list->u.reg.len, &bfd_offset);
2763 break;
2765 case record_full_mem: /* mem */
2766 if (record_debug)
2767 fprintf_unfiltered (gdb_stdlog,
2768 " Writing memory %s (1 plus "
2769 "%lu plus %lu plus %d bytes)\n",
2770 paddress (gdbarch,
2771 record_full_list->u.mem.addr),
2772 (unsigned long) sizeof (addr),
2773 (unsigned long) sizeof (len),
2774 record_full_list->u.mem.len);
2776 /* Write memlen. */
2777 len = netorder32 (record_full_list->u.mem.len);
2778 bfdcore_write (obfd, osec, &len, sizeof (len), &bfd_offset);
2780 /* Write memaddr. */
2781 addr = netorder64 (record_full_list->u.mem.addr);
2782 bfdcore_write (obfd, osec, &addr,
2783 sizeof (addr), &bfd_offset);
2785 /* Write memval. */
2786 bfdcore_write (obfd, osec,
2787 record_full_get_loc (record_full_list),
2788 record_full_list->u.mem.len, &bfd_offset);
2789 break;
2791 case record_full_end:
2792 if (record_debug)
2793 fprintf_unfiltered (gdb_stdlog,
2794 " Writing record_full_end (1 + "
2795 "%lu + %lu bytes)\n",
2796 (unsigned long) sizeof (signal),
2797 (unsigned long) sizeof (count));
2798 /* Write signal value. */
2799 signal = netorder32 (record_full_list->u.end.sigval);
2800 bfdcore_write (obfd, osec, &signal,
2801 sizeof (signal), &bfd_offset);
2803 /* Write insn count. */
2804 count = netorder32 (record_full_list->u.end.insn_num);
2805 bfdcore_write (obfd, osec, &count,
2806 sizeof (count), &bfd_offset);
2807 break;
2811 /* Execute entry. */
2812 record_full_exec_insn (regcache, gdbarch, record_full_list);
2814 if (record_full_list->next)
2815 record_full_list = record_full_list->next;
2816 else
2817 break;
2820 /* Reverse execute to cur_record_full_list. */
2821 while (1)
2823 /* Check for beginning and end of log. */
2824 if (record_full_list == cur_record_full_list)
2825 break;
2827 record_full_exec_insn (regcache, gdbarch, record_full_list);
2829 if (record_full_list->prev)
2830 record_full_list = record_full_list->prev;
2833 do_cleanups (set_cleanups);
2834 gdb_bfd_unref (obfd);
2835 discard_cleanups (old_cleanups);
2837 /* Succeeded. */
2838 printf_filtered (_("Saved core file %s with execution log.\n"),
2839 recfilename);
2842 /* record_full_goto_insn -- rewind the record log (forward or backward,
2843 depending on DIR) to the given entry, changing the program state
2844 correspondingly. */
2846 static void
2847 record_full_goto_insn (struct record_full_entry *entry,
2848 enum exec_direction_kind dir)
2850 struct cleanup *set_cleanups = record_full_gdb_operation_disable_set ();
2851 struct regcache *regcache = get_current_regcache ();
2852 struct gdbarch *gdbarch = get_regcache_arch (regcache);
2854 /* Assume everything is valid: we will hit the entry,
2855 and we will not hit the end of the recording. */
2857 if (dir == EXEC_FORWARD)
2858 record_full_list = record_full_list->next;
2862 record_full_exec_insn (regcache, gdbarch, record_full_list);
2863 if (dir == EXEC_REVERSE)
2864 record_full_list = record_full_list->prev;
2865 else
2866 record_full_list = record_full_list->next;
2867 } while (record_full_list != entry);
2868 do_cleanups (set_cleanups);
2871 /* Alias for "target record-full". */
2873 static void
2874 cmd_record_full_start (char *args, int from_tty)
2876 execute_command ("target record-full", from_tty);
2879 static void
2880 set_record_full_insn_max_num (char *args, int from_tty,
2881 struct cmd_list_element *c)
2883 if (record_full_insn_num > record_full_insn_max_num
2884 && record_full_insn_max_num)
2886 /* Count down record_full_insn_num while releasing records from list. */
2887 while (record_full_insn_num > record_full_insn_max_num)
2889 record_full_list_release_first ();
2890 record_full_insn_num--;
2895 /* The "set record full" command. */
2897 static void
2898 set_record_full_command (char *args, int from_tty)
2900 printf_unfiltered (_("\"set record full\" must be followed "
2901 "by an apporpriate subcommand.\n"));
2902 help_list (set_record_full_cmdlist, "set record full ", all_commands,
2903 gdb_stdout);
2906 /* The "show record full" command. */
2908 static void
2909 show_record_full_command (char *args, int from_tty)
2911 cmd_show_list (show_record_full_cmdlist, from_tty, "");
2914 /* Provide a prototype to silence -Wmissing-prototypes. */
2915 extern initialize_file_ftype _initialize_record_full;
2917 void
2918 _initialize_record_full (void)
2920 struct cmd_list_element *c;
2922 /* Init record_full_first. */
2923 record_full_first.prev = NULL;
2924 record_full_first.next = NULL;
2925 record_full_first.type = record_full_end;
2927 init_record_full_ops ();
2928 add_target (&record_full_ops);
2929 add_deprecated_target_alias (&record_full_ops, "record");
2930 init_record_full_core_ops ();
2931 add_target (&record_full_core_ops);
2933 add_prefix_cmd ("full", class_obscure, cmd_record_full_start,
2934 _("Start full execution recording."), &record_full_cmdlist,
2935 "record full ", 0, &record_cmdlist);
2937 c = add_cmd ("restore", class_obscure, cmd_record_full_restore,
2938 _("Restore the execution log from a file.\n\
2939 Argument is filename. File must be created with 'record save'."),
2940 &record_full_cmdlist);
2941 set_cmd_completer (c, filename_completer);
2943 /* Deprecate the old version without "full" prefix. */
2944 c = add_alias_cmd ("restore", "full restore", class_obscure, 1,
2945 &record_cmdlist);
2946 set_cmd_completer (c, filename_completer);
2947 deprecate_cmd (c, "record full restore");
2949 add_prefix_cmd ("full", class_support, set_record_full_command,
2950 _("Set record options"), &set_record_full_cmdlist,
2951 "set record full ", 0, &set_record_cmdlist);
2953 add_prefix_cmd ("full", class_support, show_record_full_command,
2954 _("Show record options"), &show_record_full_cmdlist,
2955 "show record full ", 0, &show_record_cmdlist);
2957 /* Record instructions number limit command. */
2958 add_setshow_boolean_cmd ("stop-at-limit", no_class,
2959 &record_full_stop_at_limit, _("\
2960 Set whether record/replay stops when record/replay buffer becomes full."), _("\
2961 Show whether record/replay stops when record/replay buffer becomes full."),
2962 _("Default is ON.\n\
2963 When ON, if the record/replay buffer becomes full, ask user what to do.\n\
2964 When OFF, if the record/replay buffer becomes full,\n\
2965 delete the oldest recorded instruction to make room for each new one."),
2966 NULL, NULL,
2967 &set_record_full_cmdlist, &show_record_full_cmdlist);
2969 c = add_alias_cmd ("stop-at-limit", "full stop-at-limit", no_class, 1,
2970 &set_record_cmdlist);
2971 deprecate_cmd (c, "set record full stop-at-limit");
2973 c = add_alias_cmd ("stop-at-limit", "full stop-at-limit", no_class, 1,
2974 &show_record_cmdlist);
2975 deprecate_cmd (c, "show record full stop-at-limit");
2977 add_setshow_uinteger_cmd ("insn-number-max", no_class,
2978 &record_full_insn_max_num,
2979 _("Set record/replay buffer limit."),
2980 _("Show record/replay buffer limit."), _("\
2981 Set the maximum number of instructions to be stored in the\n\
2982 record/replay buffer. Zero means unlimited. Default is 200000."),
2983 set_record_full_insn_max_num,
2984 NULL, &set_record_full_cmdlist,
2985 &show_record_full_cmdlist);
2987 c = add_alias_cmd ("insn-number-max", "full insn-number-max", no_class, 1,
2988 &set_record_cmdlist);
2989 deprecate_cmd (c, "set record full insn-number-max");
2991 c = add_alias_cmd ("insn-number-max", "full insn-number-max", no_class, 1,
2992 &show_record_cmdlist);
2993 deprecate_cmd (c, "show record full insn-number-max");
2995 add_setshow_boolean_cmd ("memory-query", no_class,
2996 &record_full_memory_query, _("\
2997 Set whether query if PREC cannot record memory change of next instruction."),
2998 _("\
2999 Show whether query if PREC cannot record memory change of next instruction."),
3000 _("\
3001 Default is OFF.\n\
3002 When ON, query if PREC cannot record memory change of next instruction."),
3003 NULL, NULL,
3004 &set_record_full_cmdlist,
3005 &show_record_full_cmdlist);
3007 c = add_alias_cmd ("memory-query", "full memory-query", no_class, 1,
3008 &set_record_cmdlist);
3009 deprecate_cmd (c, "set record full memory-query");
3011 c = add_alias_cmd ("memory-query", "full memory-query", no_class, 1,
3012 &show_record_cmdlist);
3013 deprecate_cmd (c, "show record full memory-query");