Tests for validate symbol file using build-id.
[gdb/archer.git] / gdb / record-full.c
blob03d287d0f03d849b74f197046bb756046385de2d
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 unsigned int record_full_insn_num = 0;
203 /* Count of insns logged so far (may be larger
204 than count of insns presently in execution log). */
205 static ULONGEST record_full_insn_count;
207 /* The target_ops of process record. */
208 static struct target_ops record_full_ops;
209 static struct target_ops record_full_core_ops;
211 /* Command lists for "set/show record full". */
212 static struct cmd_list_element *set_record_full_cmdlist;
213 static struct cmd_list_element *show_record_full_cmdlist;
215 /* Command list for "record full". */
216 static struct cmd_list_element *record_full_cmdlist;
218 /* The beneath function pointers. */
219 static struct target_ops *record_full_beneath_to_resume_ops;
220 static void (*record_full_beneath_to_resume) (struct target_ops *, ptid_t, int,
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_num == record_full_insn_max_num)
562 /* Ask user what to do. */
563 if (record_full_stop_at_limit)
565 int q;
567 if (set_terminal)
568 target_terminal_ours ();
569 q = yquery (_("Do you want to auto delete previous execution "
570 "log entries when record/replay buffer becomes "
571 "full (record full stop-at-limit)?"));
572 if (set_terminal)
573 target_terminal_inferior ();
574 if (q)
575 record_full_stop_at_limit = 0;
576 else
577 error (_("Process record: stopped by user."));
582 static void
583 record_full_arch_list_cleanups (void *ignore)
585 record_full_list_release (record_full_arch_list_tail);
588 /* Before inferior step (when GDB record the running message, inferior
589 only can step), GDB will call this function to record the values to
590 record_full_list. This function will call gdbarch_process_record to
591 record the running message of inferior and set them to
592 record_full_arch_list, and add it to record_full_list. */
594 static int
595 record_full_message (struct regcache *regcache, enum gdb_signal signal)
597 int ret;
598 struct gdbarch *gdbarch = get_regcache_arch (regcache);
599 struct cleanup *old_cleanups
600 = make_cleanup (record_full_arch_list_cleanups, 0);
602 record_full_arch_list_head = NULL;
603 record_full_arch_list_tail = NULL;
605 /* Check record_full_insn_num. */
606 record_full_check_insn_num (1);
608 /* If gdb sends a signal value to target_resume,
609 save it in the 'end' field of the previous instruction.
611 Maybe process record should record what really happened,
612 rather than what gdb pretends has happened.
614 So if Linux delivered the signal to the child process during
615 the record mode, we will record it and deliver it again in
616 the replay mode.
618 If user says "ignore this signal" during the record mode, then
619 it will be ignored again during the replay mode (no matter if
620 the user says something different, like "deliver this signal"
621 during the replay mode).
623 User should understand that nothing he does during the replay
624 mode will change the behavior of the child. If he tries,
625 then that is a user error.
627 But we should still deliver the signal to gdb during the replay,
628 if we delivered it during the recording. Therefore we should
629 record the signal during record_full_wait, not
630 record_full_resume. */
631 if (record_full_list != &record_full_first) /* FIXME better way to check */
633 gdb_assert (record_full_list->type == record_full_end);
634 record_full_list->u.end.sigval = signal;
637 if (signal == GDB_SIGNAL_0
638 || !gdbarch_process_record_signal_p (gdbarch))
639 ret = gdbarch_process_record (gdbarch,
640 regcache,
641 regcache_read_pc (regcache));
642 else
643 ret = gdbarch_process_record_signal (gdbarch,
644 regcache,
645 signal);
647 if (ret > 0)
648 error (_("Process record: inferior program stopped."));
649 if (ret < 0)
650 error (_("Process record: failed to record execution log."));
652 discard_cleanups (old_cleanups);
654 record_full_list->next = record_full_arch_list_head;
655 record_full_arch_list_head->prev = record_full_list;
656 record_full_list = record_full_arch_list_tail;
658 if (record_full_insn_num == record_full_insn_max_num)
659 record_full_list_release_first ();
660 else
661 record_full_insn_num++;
663 return 1;
666 struct record_full_message_args {
667 struct regcache *regcache;
668 enum gdb_signal signal;
671 static int
672 record_full_message_wrapper (void *args)
674 struct record_full_message_args *record_full_args = args;
676 return record_full_message (record_full_args->regcache,
677 record_full_args->signal);
680 static int
681 record_full_message_wrapper_safe (struct regcache *regcache,
682 enum gdb_signal signal)
684 struct record_full_message_args args;
686 args.regcache = regcache;
687 args.signal = signal;
689 return catch_errors (record_full_message_wrapper, &args, NULL,
690 RETURN_MASK_ALL);
693 /* Set to 1 if record_full_store_registers and record_full_xfer_partial
694 doesn't need record. */
696 static int record_full_gdb_operation_disable = 0;
698 struct cleanup *
699 record_full_gdb_operation_disable_set (void)
701 struct cleanup *old_cleanups = NULL;
703 old_cleanups =
704 make_cleanup_restore_integer (&record_full_gdb_operation_disable);
705 record_full_gdb_operation_disable = 1;
707 return old_cleanups;
710 /* Flag set to TRUE for target_stopped_by_watchpoint. */
711 static int record_full_hw_watchpoint = 0;
713 /* Execute one instruction from the record log. Each instruction in
714 the log will be represented by an arbitrary sequence of register
715 entries and memory entries, followed by an 'end' entry. */
717 static inline void
718 record_full_exec_insn (struct regcache *regcache,
719 struct gdbarch *gdbarch,
720 struct record_full_entry *entry)
722 switch (entry->type)
724 case record_full_reg: /* reg */
726 gdb_byte reg[MAX_REGISTER_SIZE];
728 if (record_debug > 1)
729 fprintf_unfiltered (gdb_stdlog,
730 "Process record: record_full_reg %s to "
731 "inferior num = %d.\n",
732 host_address_to_string (entry),
733 entry->u.reg.num);
735 regcache_cooked_read (regcache, entry->u.reg.num, reg);
736 regcache_cooked_write (regcache, entry->u.reg.num,
737 record_full_get_loc (entry));
738 memcpy (record_full_get_loc (entry), reg, entry->u.reg.len);
740 break;
742 case record_full_mem: /* mem */
744 /* Nothing to do if the entry is flagged not_accessible. */
745 if (!entry->u.mem.mem_entry_not_accessible)
747 gdb_byte *mem = alloca (entry->u.mem.len);
749 if (record_debug > 1)
750 fprintf_unfiltered (gdb_stdlog,
751 "Process record: record_full_mem %s to "
752 "inferior addr = %s len = %d.\n",
753 host_address_to_string (entry),
754 paddress (gdbarch, entry->u.mem.addr),
755 entry->u.mem.len);
757 if (record_read_memory (gdbarch,
758 entry->u.mem.addr, mem, entry->u.mem.len))
759 entry->u.mem.mem_entry_not_accessible = 1;
760 else
762 if (target_write_memory (entry->u.mem.addr,
763 record_full_get_loc (entry),
764 entry->u.mem.len))
766 entry->u.mem.mem_entry_not_accessible = 1;
767 if (record_debug)
768 warning (_("Process record: error writing memory at "
769 "addr = %s len = %d."),
770 paddress (gdbarch, entry->u.mem.addr),
771 entry->u.mem.len);
773 else
775 memcpy (record_full_get_loc (entry), mem,
776 entry->u.mem.len);
778 /* We've changed memory --- check if a hardware
779 watchpoint should trap. Note that this
780 presently assumes the target beneath supports
781 continuable watchpoints. On non-continuable
782 watchpoints target, we'll want to check this
783 _before_ actually doing the memory change, and
784 not doing the change at all if the watchpoint
785 traps. */
786 if (hardware_watchpoint_inserted_in_range
787 (get_regcache_aspace (regcache),
788 entry->u.mem.addr, entry->u.mem.len))
789 record_full_hw_watchpoint = 1;
794 break;
798 static struct target_ops *tmp_to_resume_ops;
799 static void (*tmp_to_resume) (struct target_ops *, ptid_t, int,
800 enum gdb_signal);
801 static struct target_ops *tmp_to_wait_ops;
802 static ptid_t (*tmp_to_wait) (struct target_ops *, ptid_t,
803 struct target_waitstatus *,
804 int);
805 static struct target_ops *tmp_to_store_registers_ops;
806 static void (*tmp_to_store_registers) (struct target_ops *,
807 struct regcache *,
808 int regno);
809 static struct target_ops *tmp_to_xfer_partial_ops;
810 static LONGEST (*tmp_to_xfer_partial) (struct target_ops *ops,
811 enum target_object object,
812 const char *annex,
813 gdb_byte *readbuf,
814 const gdb_byte *writebuf,
815 ULONGEST offset,
816 LONGEST len);
817 static int (*tmp_to_insert_breakpoint) (struct gdbarch *,
818 struct bp_target_info *);
819 static int (*tmp_to_remove_breakpoint) (struct gdbarch *,
820 struct bp_target_info *);
821 static int (*tmp_to_stopped_by_watchpoint) (void);
822 static int (*tmp_to_stopped_data_address) (struct target_ops *, CORE_ADDR *);
823 static int (*tmp_to_stopped_data_address) (struct target_ops *, CORE_ADDR *);
824 static void (*tmp_to_async) (void (*) (enum inferior_event_type, void *), void *);
826 static void record_full_restore (void);
828 /* Asynchronous signal handle registered as event loop source for when
829 we have pending events ready to be passed to the core. */
831 static struct async_event_handler *record_full_async_inferior_event_token;
833 static void
834 record_full_async_inferior_event_handler (gdb_client_data data)
836 inferior_event_handler (INF_REG_EVENT, NULL);
839 /* Open the process record target. */
841 static void
842 record_full_core_open_1 (char *name, int from_tty)
844 struct regcache *regcache = get_current_regcache ();
845 int regnum = gdbarch_num_regs (get_regcache_arch (regcache));
846 int i;
848 /* Get record_full_core_regbuf. */
849 target_fetch_registers (regcache, -1);
850 record_full_core_regbuf = xmalloc (MAX_REGISTER_SIZE * regnum);
851 for (i = 0; i < regnum; i ++)
852 regcache_raw_collect (regcache, i,
853 record_full_core_regbuf + MAX_REGISTER_SIZE * i);
855 /* Get record_full_core_start and record_full_core_end. */
856 if (build_section_table (core_bfd, &record_full_core_start,
857 &record_full_core_end))
859 xfree (record_full_core_regbuf);
860 record_full_core_regbuf = NULL;
861 error (_("\"%s\": Can't find sections: %s"),
862 bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ()));
865 push_target (&record_full_core_ops);
866 record_full_restore ();
869 /* "to_open" target method for 'live' processes. */
871 static void
872 record_full_open_1 (char *name, int from_tty)
874 if (record_debug)
875 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_open\n");
877 /* check exec */
878 if (!target_has_execution)
879 error (_("Process record: the program is not being run."));
880 if (non_stop)
881 error (_("Process record target can't debug inferior in non-stop mode "
882 "(non-stop)."));
884 if (!gdbarch_process_record_p (target_gdbarch ()))
885 error (_("Process record: the current architecture doesn't support "
886 "record function."));
888 if (!tmp_to_resume)
889 error (_("Could not find 'to_resume' method on the target stack."));
890 if (!tmp_to_wait)
891 error (_("Could not find 'to_wait' method on the target stack."));
892 if (!tmp_to_store_registers)
893 error (_("Could not find 'to_store_registers' "
894 "method on the target stack."));
895 if (!tmp_to_insert_breakpoint)
896 error (_("Could not find 'to_insert_breakpoint' "
897 "method on the target stack."));
898 if (!tmp_to_remove_breakpoint)
899 error (_("Could not find 'to_remove_breakpoint' "
900 "method on the target stack."));
901 if (!tmp_to_stopped_by_watchpoint)
902 error (_("Could not find 'to_stopped_by_watchpoint' "
903 "method on the target stack."));
904 if (!tmp_to_stopped_data_address)
905 error (_("Could not find 'to_stopped_data_address' "
906 "method on the target stack."));
908 push_target (&record_full_ops);
911 static void record_full_init_record_breakpoints (void);
913 /* "to_open" target method. Open the process record target. */
915 static void
916 record_full_open (char *name, int from_tty)
918 struct target_ops *t;
920 if (record_debug)
921 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_open\n");
923 /* Check if record target is already running. */
924 if (current_target.to_stratum == record_stratum)
925 error (_("Process record target already running. Use \"record stop\" to "
926 "stop record target first."));
928 /* Reset the tmp beneath pointers. */
929 tmp_to_resume_ops = NULL;
930 tmp_to_resume = NULL;
931 tmp_to_wait_ops = NULL;
932 tmp_to_wait = NULL;
933 tmp_to_store_registers_ops = NULL;
934 tmp_to_store_registers = NULL;
935 tmp_to_xfer_partial_ops = NULL;
936 tmp_to_xfer_partial = NULL;
937 tmp_to_insert_breakpoint = NULL;
938 tmp_to_remove_breakpoint = NULL;
939 tmp_to_stopped_by_watchpoint = NULL;
940 tmp_to_stopped_data_address = NULL;
941 tmp_to_async = NULL;
943 /* Set the beneath function pointers. */
944 for (t = current_target.beneath; t != NULL; t = t->beneath)
946 if (!tmp_to_resume)
948 tmp_to_resume = t->to_resume;
949 tmp_to_resume_ops = t;
951 if (!tmp_to_wait)
953 tmp_to_wait = t->to_wait;
954 tmp_to_wait_ops = t;
956 if (!tmp_to_store_registers)
958 tmp_to_store_registers = t->to_store_registers;
959 tmp_to_store_registers_ops = t;
961 if (!tmp_to_xfer_partial)
963 tmp_to_xfer_partial = t->to_xfer_partial;
964 tmp_to_xfer_partial_ops = t;
966 if (!tmp_to_insert_breakpoint)
967 tmp_to_insert_breakpoint = t->to_insert_breakpoint;
968 if (!tmp_to_remove_breakpoint)
969 tmp_to_remove_breakpoint = t->to_remove_breakpoint;
970 if (!tmp_to_stopped_by_watchpoint)
971 tmp_to_stopped_by_watchpoint = t->to_stopped_by_watchpoint;
972 if (!tmp_to_stopped_data_address)
973 tmp_to_stopped_data_address = t->to_stopped_data_address;
974 if (!tmp_to_async)
975 tmp_to_async = t->to_async;
977 if (!tmp_to_xfer_partial)
978 error (_("Could not find 'to_xfer_partial' method on the target stack."));
980 /* Reset */
981 record_full_insn_num = 0;
982 record_full_insn_count = 0;
983 record_full_list = &record_full_first;
984 record_full_list->next = NULL;
986 /* Set the tmp beneath pointers to beneath pointers. */
987 record_full_beneath_to_resume_ops = tmp_to_resume_ops;
988 record_full_beneath_to_resume = tmp_to_resume;
989 record_full_beneath_to_wait_ops = tmp_to_wait_ops;
990 record_full_beneath_to_wait = tmp_to_wait;
991 record_full_beneath_to_store_registers_ops = tmp_to_store_registers_ops;
992 record_full_beneath_to_store_registers = tmp_to_store_registers;
993 record_full_beneath_to_xfer_partial_ops = tmp_to_xfer_partial_ops;
994 record_full_beneath_to_xfer_partial = tmp_to_xfer_partial;
995 record_full_beneath_to_insert_breakpoint = tmp_to_insert_breakpoint;
996 record_full_beneath_to_remove_breakpoint = tmp_to_remove_breakpoint;
997 record_full_beneath_to_stopped_by_watchpoint = tmp_to_stopped_by_watchpoint;
998 record_full_beneath_to_stopped_data_address = tmp_to_stopped_data_address;
999 record_full_beneath_to_async = tmp_to_async;
1001 if (core_bfd)
1002 record_full_core_open_1 (name, from_tty);
1003 else
1004 record_full_open_1 (name, from_tty);
1006 /* Register extra event sources in the event loop. */
1007 record_full_async_inferior_event_token
1008 = create_async_event_handler (record_full_async_inferior_event_handler,
1009 NULL);
1011 record_full_init_record_breakpoints ();
1013 observer_notify_record_changed (current_inferior (), 1);
1016 /* "to_close" target method. Close the process record target. */
1018 static void
1019 record_full_close (void)
1021 struct record_full_core_buf_entry *entry;
1023 if (record_debug)
1024 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_close\n");
1026 record_full_list_release (record_full_list);
1028 /* Release record_full_core_regbuf. */
1029 if (record_full_core_regbuf)
1031 xfree (record_full_core_regbuf);
1032 record_full_core_regbuf = NULL;
1035 /* Release record_full_core_buf_list. */
1036 if (record_full_core_buf_list)
1038 for (entry = record_full_core_buf_list->prev; entry;
1039 entry = entry->prev)
1041 xfree (record_full_core_buf_list);
1042 record_full_core_buf_list = entry;
1044 record_full_core_buf_list = NULL;
1047 if (record_full_async_inferior_event_token)
1048 delete_async_event_handler (&record_full_async_inferior_event_token);
1051 static int record_full_resume_step = 0;
1053 /* True if we've been resumed, and so each record_full_wait call should
1054 advance execution. If this is false, record_full_wait will return a
1055 TARGET_WAITKIND_IGNORE. */
1056 static int record_full_resumed = 0;
1058 /* The execution direction of the last resume we got. This is
1059 necessary for async mode. Vis (order is not strictly accurate):
1061 1. user has the global execution direction set to forward
1062 2. user does a reverse-step command
1063 3. record_full_resume is called with global execution direction
1064 temporarily switched to reverse
1065 4. GDB's execution direction is reverted back to forward
1066 5. target record notifies event loop there's an event to handle
1067 6. infrun asks the target which direction was it going, and switches
1068 the global execution direction accordingly (to reverse)
1069 7. infrun polls an event out of the record target, and handles it
1070 8. GDB goes back to the event loop, and goto #4.
1072 static enum exec_direction_kind record_full_execution_dir = EXEC_FORWARD;
1074 /* "to_resume" target method. Resume the process record target. */
1076 static void
1077 record_full_resume (struct target_ops *ops, ptid_t ptid, int step,
1078 enum gdb_signal signal)
1080 record_full_resume_step = step;
1081 record_full_resumed = 1;
1082 record_full_execution_dir = execution_direction;
1084 if (!RECORD_FULL_IS_REPLAY)
1086 struct gdbarch *gdbarch = target_thread_architecture (ptid);
1088 record_full_message (get_current_regcache (), signal);
1090 if (!step)
1092 /* This is not hard single step. */
1093 if (!gdbarch_software_single_step_p (gdbarch))
1095 /* This is a normal continue. */
1096 step = 1;
1098 else
1100 /* This arch support soft sigle step. */
1101 if (single_step_breakpoints_inserted ())
1103 /* This is a soft single step. */
1104 record_full_resume_step = 1;
1106 else
1108 /* This is a continue.
1109 Try to insert a soft single step breakpoint. */
1110 if (!gdbarch_software_single_step (gdbarch,
1111 get_current_frame ()))
1113 /* This system don't want use soft single step.
1114 Use hard sigle step. */
1115 step = 1;
1121 /* Make sure the target beneath reports all signals. */
1122 target_pass_signals (0, NULL);
1124 record_full_beneath_to_resume (record_full_beneath_to_resume_ops,
1125 ptid, step, signal);
1128 /* We are about to start executing the inferior (or simulate it),
1129 let's register it with the event loop. */
1130 if (target_can_async_p ())
1132 target_async (inferior_event_handler, 0);
1133 /* Notify the event loop there's an event to wait for. We do
1134 most of the work in record_full_wait. */
1135 mark_async_event_handler (record_full_async_inferior_event_token);
1139 static int record_full_get_sig = 0;
1141 /* SIGINT signal handler, registered by "to_wait" method. */
1143 static void
1144 record_full_sig_handler (int signo)
1146 if (record_debug)
1147 fprintf_unfiltered (gdb_stdlog, "Process record: get a signal\n");
1149 /* It will break the running inferior in replay mode. */
1150 record_full_resume_step = 1;
1152 /* It will let record_full_wait set inferior status to get the signal
1153 SIGINT. */
1154 record_full_get_sig = 1;
1157 static void
1158 record_full_wait_cleanups (void *ignore)
1160 if (execution_direction == EXEC_REVERSE)
1162 if (record_full_list->next)
1163 record_full_list = record_full_list->next;
1165 else
1166 record_full_list = record_full_list->prev;
1169 /* "to_wait" target method for process record target.
1171 In record mode, the target is always run in singlestep mode
1172 (even when gdb says to continue). The to_wait method intercepts
1173 the stop events and determines which ones are to be passed on to
1174 gdb. Most stop events are just singlestep events that gdb is not
1175 to know about, so the to_wait method just records them and keeps
1176 singlestepping.
1178 In replay mode, this function emulates the recorded execution log,
1179 one instruction at a time (forward or backward), and determines
1180 where to stop. */
1182 static ptid_t
1183 record_full_wait_1 (struct target_ops *ops,
1184 ptid_t ptid, struct target_waitstatus *status,
1185 int options)
1187 struct cleanup *set_cleanups = record_full_gdb_operation_disable_set ();
1189 if (record_debug)
1190 fprintf_unfiltered (gdb_stdlog,
1191 "Process record: record_full_wait "
1192 "record_full_resume_step = %d, "
1193 "record_full_resumed = %d, direction=%s\n",
1194 record_full_resume_step, record_full_resumed,
1195 record_full_execution_dir == EXEC_FORWARD
1196 ? "forward" : "reverse");
1198 if (!record_full_resumed)
1200 gdb_assert ((options & TARGET_WNOHANG) != 0);
1202 /* No interesting event. */
1203 status->kind = TARGET_WAITKIND_IGNORE;
1204 return minus_one_ptid;
1207 record_full_get_sig = 0;
1208 signal (SIGINT, record_full_sig_handler);
1210 if (!RECORD_FULL_IS_REPLAY && ops != &record_full_core_ops)
1212 if (record_full_resume_step)
1214 /* This is a single step. */
1215 return record_full_beneath_to_wait (record_full_beneath_to_wait_ops,
1216 ptid, status, options);
1218 else
1220 /* This is not a single step. */
1221 ptid_t ret;
1222 CORE_ADDR tmp_pc;
1223 struct gdbarch *gdbarch = target_thread_architecture (inferior_ptid);
1225 while (1)
1227 ret = record_full_beneath_to_wait
1228 (record_full_beneath_to_wait_ops, ptid, status, options);
1229 if (status->kind == TARGET_WAITKIND_IGNORE)
1231 if (record_debug)
1232 fprintf_unfiltered (gdb_stdlog,
1233 "Process record: record_full_wait "
1234 "target beneath not done yet\n");
1235 return ret;
1238 if (single_step_breakpoints_inserted ())
1239 remove_single_step_breakpoints ();
1241 if (record_full_resume_step)
1242 return ret;
1244 /* Is this a SIGTRAP? */
1245 if (status->kind == TARGET_WAITKIND_STOPPED
1246 && status->value.sig == GDB_SIGNAL_TRAP)
1248 struct regcache *regcache;
1249 struct address_space *aspace;
1251 /* Yes -- this is likely our single-step finishing,
1252 but check if there's any reason the core would be
1253 interested in the event. */
1255 registers_changed ();
1256 regcache = get_current_regcache ();
1257 tmp_pc = regcache_read_pc (regcache);
1258 aspace = get_regcache_aspace (regcache);
1260 if (target_stopped_by_watchpoint ())
1262 /* Always interested in watchpoints. */
1264 else if (breakpoint_inserted_here_p (aspace, tmp_pc))
1266 /* There is a breakpoint here. Let the core
1267 handle it. */
1268 if (software_breakpoint_inserted_here_p (aspace, tmp_pc))
1270 struct gdbarch *gdbarch
1271 = get_regcache_arch (regcache);
1272 CORE_ADDR decr_pc_after_break
1273 = gdbarch_decr_pc_after_break (gdbarch);
1274 if (decr_pc_after_break)
1275 regcache_write_pc (regcache,
1276 tmp_pc + decr_pc_after_break);
1279 else
1281 /* This is a single-step trap. Record the
1282 insn and issue another step.
1283 FIXME: this part can be a random SIGTRAP too.
1284 But GDB cannot handle it. */
1285 int step = 1;
1287 if (!record_full_message_wrapper_safe (regcache,
1288 GDB_SIGNAL_0))
1290 status->kind = TARGET_WAITKIND_STOPPED;
1291 status->value.sig = GDB_SIGNAL_0;
1292 break;
1295 if (gdbarch_software_single_step_p (gdbarch))
1297 /* Try to insert the software single step breakpoint.
1298 If insert success, set step to 0. */
1299 set_executing (inferior_ptid, 0);
1300 reinit_frame_cache ();
1301 if (gdbarch_software_single_step (gdbarch,
1302 get_current_frame ()))
1303 step = 0;
1304 set_executing (inferior_ptid, 1);
1307 if (record_debug)
1308 fprintf_unfiltered (gdb_stdlog,
1309 "Process record: record_full_wait "
1310 "issuing one more step in the "
1311 "target beneath\n");
1312 record_full_beneath_to_resume
1313 (record_full_beneath_to_resume_ops, ptid, step,
1314 GDB_SIGNAL_0);
1315 continue;
1319 /* The inferior is broken by a breakpoint or a signal. */
1320 break;
1323 return ret;
1326 else
1328 struct regcache *regcache = get_current_regcache ();
1329 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1330 struct address_space *aspace = get_regcache_aspace (regcache);
1331 int continue_flag = 1;
1332 int first_record_full_end = 1;
1333 struct cleanup *old_cleanups
1334 = make_cleanup (record_full_wait_cleanups, 0);
1335 CORE_ADDR tmp_pc;
1337 record_full_hw_watchpoint = 0;
1338 status->kind = TARGET_WAITKIND_STOPPED;
1340 /* Check breakpoint when forward execute. */
1341 if (execution_direction == EXEC_FORWARD)
1343 tmp_pc = regcache_read_pc (regcache);
1344 if (breakpoint_inserted_here_p (aspace, tmp_pc))
1346 int decr_pc_after_break = gdbarch_decr_pc_after_break (gdbarch);
1348 if (record_debug)
1349 fprintf_unfiltered (gdb_stdlog,
1350 "Process record: break at %s.\n",
1351 paddress (gdbarch, tmp_pc));
1353 if (decr_pc_after_break
1354 && !record_full_resume_step
1355 && software_breakpoint_inserted_here_p (aspace, tmp_pc))
1356 regcache_write_pc (regcache,
1357 tmp_pc + decr_pc_after_break);
1358 goto replay_out;
1362 /* If GDB is in terminal_inferior mode, it will not get the signal.
1363 And in GDB replay mode, GDB doesn't need to be in terminal_inferior
1364 mode, because inferior will not executed.
1365 Then set it to terminal_ours to make GDB get the signal. */
1366 target_terminal_ours ();
1368 /* In EXEC_FORWARD mode, record_full_list points to the tail of prev
1369 instruction. */
1370 if (execution_direction == EXEC_FORWARD && record_full_list->next)
1371 record_full_list = record_full_list->next;
1373 /* Loop over the record_full_list, looking for the next place to
1374 stop. */
1377 /* Check for beginning and end of log. */
1378 if (execution_direction == EXEC_REVERSE
1379 && record_full_list == &record_full_first)
1381 /* Hit beginning of record log in reverse. */
1382 status->kind = TARGET_WAITKIND_NO_HISTORY;
1383 break;
1385 if (execution_direction != EXEC_REVERSE && !record_full_list->next)
1387 /* Hit end of record log going forward. */
1388 status->kind = TARGET_WAITKIND_NO_HISTORY;
1389 break;
1392 record_full_exec_insn (regcache, gdbarch, record_full_list);
1394 if (record_full_list->type == record_full_end)
1396 if (record_debug > 1)
1397 fprintf_unfiltered (gdb_stdlog,
1398 "Process record: record_full_end %s to "
1399 "inferior.\n",
1400 host_address_to_string (record_full_list));
1402 if (first_record_full_end && execution_direction == EXEC_REVERSE)
1404 /* When reverse excute, the first record_full_end is the
1405 part of current instruction. */
1406 first_record_full_end = 0;
1408 else
1410 /* In EXEC_REVERSE mode, this is the record_full_end of prev
1411 instruction.
1412 In EXEC_FORWARD mode, this is the record_full_end of
1413 current instruction. */
1414 /* step */
1415 if (record_full_resume_step)
1417 if (record_debug > 1)
1418 fprintf_unfiltered (gdb_stdlog,
1419 "Process record: step.\n");
1420 continue_flag = 0;
1423 /* check breakpoint */
1424 tmp_pc = regcache_read_pc (regcache);
1425 if (breakpoint_inserted_here_p (aspace, tmp_pc))
1427 int decr_pc_after_break
1428 = gdbarch_decr_pc_after_break (gdbarch);
1430 if (record_debug)
1431 fprintf_unfiltered (gdb_stdlog,
1432 "Process record: break "
1433 "at %s.\n",
1434 paddress (gdbarch, tmp_pc));
1435 if (decr_pc_after_break
1436 && execution_direction == EXEC_FORWARD
1437 && !record_full_resume_step
1438 && software_breakpoint_inserted_here_p (aspace,
1439 tmp_pc))
1440 regcache_write_pc (regcache,
1441 tmp_pc + decr_pc_after_break);
1442 continue_flag = 0;
1445 if (record_full_hw_watchpoint)
1447 if (record_debug)
1448 fprintf_unfiltered (gdb_stdlog,
1449 "Process record: hit hw "
1450 "watchpoint.\n");
1451 continue_flag = 0;
1453 /* Check target signal */
1454 if (record_full_list->u.end.sigval != GDB_SIGNAL_0)
1455 /* FIXME: better way to check */
1456 continue_flag = 0;
1460 if (continue_flag)
1462 if (execution_direction == EXEC_REVERSE)
1464 if (record_full_list->prev)
1465 record_full_list = record_full_list->prev;
1467 else
1469 if (record_full_list->next)
1470 record_full_list = record_full_list->next;
1474 while (continue_flag);
1476 replay_out:
1477 if (record_full_get_sig)
1478 status->value.sig = GDB_SIGNAL_INT;
1479 else if (record_full_list->u.end.sigval != GDB_SIGNAL_0)
1480 /* FIXME: better way to check */
1481 status->value.sig = record_full_list->u.end.sigval;
1482 else
1483 status->value.sig = GDB_SIGNAL_TRAP;
1485 discard_cleanups (old_cleanups);
1488 signal (SIGINT, handle_sigint);
1490 do_cleanups (set_cleanups);
1491 return inferior_ptid;
1494 static ptid_t
1495 record_full_wait (struct target_ops *ops,
1496 ptid_t ptid, struct target_waitstatus *status,
1497 int options)
1499 ptid_t return_ptid;
1501 return_ptid = record_full_wait_1 (ops, ptid, status, options);
1502 if (status->kind != TARGET_WAITKIND_IGNORE)
1504 /* We're reporting a stop. Make sure any spurious
1505 target_wait(WNOHANG) doesn't advance the target until the
1506 core wants us resumed again. */
1507 record_full_resumed = 0;
1509 return return_ptid;
1512 static int
1513 record_full_stopped_by_watchpoint (void)
1515 if (RECORD_FULL_IS_REPLAY)
1516 return record_full_hw_watchpoint;
1517 else
1518 return record_full_beneath_to_stopped_by_watchpoint ();
1521 static int
1522 record_full_stopped_data_address (struct target_ops *ops, CORE_ADDR *addr_p)
1524 if (RECORD_FULL_IS_REPLAY)
1525 return 0;
1526 else
1527 return record_full_beneath_to_stopped_data_address (ops, addr_p);
1530 /* Record registers change (by user or by GDB) to list as an instruction. */
1532 static void
1533 record_full_registers_change (struct regcache *regcache, int regnum)
1535 /* Check record_full_insn_num. */
1536 record_full_check_insn_num (0);
1538 record_full_arch_list_head = NULL;
1539 record_full_arch_list_tail = NULL;
1541 if (regnum < 0)
1543 int i;
1545 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
1547 if (record_full_arch_list_add_reg (regcache, i))
1549 record_full_list_release (record_full_arch_list_tail);
1550 error (_("Process record: failed to record execution log."));
1554 else
1556 if (record_full_arch_list_add_reg (regcache, regnum))
1558 record_full_list_release (record_full_arch_list_tail);
1559 error (_("Process record: failed to record execution log."));
1562 if (record_full_arch_list_add_end ())
1564 record_full_list_release (record_full_arch_list_tail);
1565 error (_("Process record: failed to record execution log."));
1567 record_full_list->next = record_full_arch_list_head;
1568 record_full_arch_list_head->prev = record_full_list;
1569 record_full_list = record_full_arch_list_tail;
1571 if (record_full_insn_num == record_full_insn_max_num)
1572 record_full_list_release_first ();
1573 else
1574 record_full_insn_num++;
1577 /* "to_store_registers" method for process record target. */
1579 static void
1580 record_full_store_registers (struct target_ops *ops,
1581 struct regcache *regcache,
1582 int regno)
1584 if (!record_full_gdb_operation_disable)
1586 if (RECORD_FULL_IS_REPLAY)
1588 int n;
1590 /* Let user choose if he wants to write register or not. */
1591 if (regno < 0)
1593 query (_("Because GDB is in replay mode, changing the "
1594 "value of a register will make the execution "
1595 "log unusable from this point onward. "
1596 "Change all registers?"));
1597 else
1599 query (_("Because GDB is in replay mode, changing the value "
1600 "of a register will make the execution log unusable "
1601 "from this point onward. Change register %s?"),
1602 gdbarch_register_name (get_regcache_arch (regcache),
1603 regno));
1605 if (!n)
1607 /* Invalidate the value of regcache that was set in function
1608 "regcache_raw_write". */
1609 if (regno < 0)
1611 int i;
1613 for (i = 0;
1614 i < gdbarch_num_regs (get_regcache_arch (regcache));
1615 i++)
1616 regcache_invalidate (regcache, i);
1618 else
1619 regcache_invalidate (regcache, regno);
1621 error (_("Process record canceled the operation."));
1624 /* Destroy the record from here forward. */
1625 record_full_list_release_following (record_full_list);
1628 record_full_registers_change (regcache, regno);
1630 record_full_beneath_to_store_registers
1631 (record_full_beneath_to_store_registers_ops, regcache, regno);
1634 /* "to_xfer_partial" method. Behavior is conditional on
1635 RECORD_FULL_IS_REPLAY.
1636 In replay mode, we cannot write memory unles we are willing to
1637 invalidate the record/replay log from this point forward. */
1639 static LONGEST
1640 record_full_xfer_partial (struct target_ops *ops, enum target_object object,
1641 const char *annex, gdb_byte *readbuf,
1642 const gdb_byte *writebuf, ULONGEST offset,
1643 LONGEST len)
1645 if (!record_full_gdb_operation_disable
1646 && (object == TARGET_OBJECT_MEMORY
1647 || object == TARGET_OBJECT_RAW_MEMORY) && writebuf)
1649 if (RECORD_FULL_IS_REPLAY)
1651 /* Let user choose if he wants to write memory or not. */
1652 if (!query (_("Because GDB is in replay mode, writing to memory "
1653 "will make the execution log unusable from this "
1654 "point onward. Write memory at address %s?"),
1655 paddress (target_gdbarch (), offset)))
1656 error (_("Process record canceled the operation."));
1658 /* Destroy the record from here forward. */
1659 record_full_list_release_following (record_full_list);
1662 /* Check record_full_insn_num */
1663 record_full_check_insn_num (0);
1665 /* Record registers change to list as an instruction. */
1666 record_full_arch_list_head = NULL;
1667 record_full_arch_list_tail = NULL;
1668 if (record_full_arch_list_add_mem (offset, len))
1670 record_full_list_release (record_full_arch_list_tail);
1671 if (record_debug)
1672 fprintf_unfiltered (gdb_stdlog,
1673 "Process record: failed to record "
1674 "execution log.");
1675 return -1;
1677 if (record_full_arch_list_add_end ())
1679 record_full_list_release (record_full_arch_list_tail);
1680 if (record_debug)
1681 fprintf_unfiltered (gdb_stdlog,
1682 "Process record: failed to record "
1683 "execution log.");
1684 return -1;
1686 record_full_list->next = record_full_arch_list_head;
1687 record_full_arch_list_head->prev = record_full_list;
1688 record_full_list = record_full_arch_list_tail;
1690 if (record_full_insn_num == record_full_insn_max_num)
1691 record_full_list_release_first ();
1692 else
1693 record_full_insn_num++;
1696 return record_full_beneath_to_xfer_partial
1697 (record_full_beneath_to_xfer_partial_ops, object, annex,
1698 readbuf, writebuf, offset, len);
1701 /* This structure represents a breakpoint inserted while the record
1702 target is active. We use this to know when to install/remove
1703 breakpoints in/from the target beneath. For example, a breakpoint
1704 may be inserted while recording, but removed when not replaying nor
1705 recording. In that case, the breakpoint had not been inserted on
1706 the target beneath, so we should not try to remove it there. */
1708 struct record_full_breakpoint
1710 /* The address and address space the breakpoint was set at. */
1711 struct address_space *address_space;
1712 CORE_ADDR addr;
1714 /* True when the breakpoint has been also installed in the target
1715 beneath. This will be false for breakpoints set during replay or
1716 when recording. */
1717 int in_target_beneath;
1720 typedef struct record_full_breakpoint *record_full_breakpoint_p;
1721 DEF_VEC_P(record_full_breakpoint_p);
1723 /* The list of breakpoints inserted while the record target is
1724 active. */
1725 VEC(record_full_breakpoint_p) *record_full_breakpoints = NULL;
1727 static void
1728 record_full_sync_record_breakpoints (struct bp_location *loc, void *data)
1730 if (loc->loc_type != bp_loc_software_breakpoint)
1731 return;
1733 if (loc->inserted)
1735 struct record_full_breakpoint *bp = XNEW (struct record_full_breakpoint);
1737 bp->addr = loc->target_info.placed_address;
1738 bp->address_space = loc->target_info.placed_address_space;
1740 bp->in_target_beneath = 1;
1742 VEC_safe_push (record_full_breakpoint_p, record_full_breakpoints, bp);
1746 /* Sync existing breakpoints to record_full_breakpoints. */
1748 static void
1749 record_full_init_record_breakpoints (void)
1751 VEC_free (record_full_breakpoint_p, record_full_breakpoints);
1753 iterate_over_bp_locations (record_full_sync_record_breakpoints);
1756 /* Behavior is conditional on RECORD_FULL_IS_REPLAY. We will not actually
1757 insert or remove breakpoints in the real target when replaying, nor
1758 when recording. */
1760 static int
1761 record_full_insert_breakpoint (struct gdbarch *gdbarch,
1762 struct bp_target_info *bp_tgt)
1764 struct record_full_breakpoint *bp;
1765 int in_target_beneath = 0;
1767 if (!RECORD_FULL_IS_REPLAY)
1769 /* When recording, we currently always single-step, so we don't
1770 really need to install regular breakpoints in the inferior.
1771 However, we do have to insert software single-step
1772 breakpoints, in case the target can't hardware step. To keep
1773 things single, we always insert. */
1774 struct cleanup *old_cleanups;
1775 int ret;
1777 old_cleanups = record_full_gdb_operation_disable_set ();
1778 ret = record_full_beneath_to_insert_breakpoint (gdbarch, bp_tgt);
1779 do_cleanups (old_cleanups);
1781 if (ret != 0)
1782 return ret;
1784 in_target_beneath = 1;
1787 bp = XNEW (struct record_full_breakpoint);
1788 bp->addr = bp_tgt->placed_address;
1789 bp->address_space = bp_tgt->placed_address_space;
1790 bp->in_target_beneath = in_target_beneath;
1791 VEC_safe_push (record_full_breakpoint_p, record_full_breakpoints, bp);
1792 return 0;
1795 /* "to_remove_breakpoint" method for process record target. */
1797 static int
1798 record_full_remove_breakpoint (struct gdbarch *gdbarch,
1799 struct bp_target_info *bp_tgt)
1801 struct record_full_breakpoint *bp;
1802 int ix;
1804 for (ix = 0;
1805 VEC_iterate (record_full_breakpoint_p,
1806 record_full_breakpoints, ix, bp);
1807 ++ix)
1809 if (bp->addr == bp_tgt->placed_address
1810 && bp->address_space == bp_tgt->placed_address_space)
1812 if (bp->in_target_beneath)
1814 struct cleanup *old_cleanups;
1815 int ret;
1817 old_cleanups = record_full_gdb_operation_disable_set ();
1818 ret = record_full_beneath_to_remove_breakpoint (gdbarch, bp_tgt);
1819 do_cleanups (old_cleanups);
1821 if (ret != 0)
1822 return ret;
1825 VEC_unordered_remove (record_full_breakpoint_p,
1826 record_full_breakpoints, ix);
1827 return 0;
1831 gdb_assert_not_reached ("removing unknown breakpoint");
1834 /* "to_can_execute_reverse" method for process record target. */
1836 static int
1837 record_full_can_execute_reverse (void)
1839 return 1;
1842 /* "to_get_bookmark" method for process record and prec over core. */
1844 static gdb_byte *
1845 record_full_get_bookmark (char *args, int from_tty)
1847 gdb_byte *ret = NULL;
1849 /* Return stringified form of instruction count. */
1850 if (record_full_list && record_full_list->type == record_full_end)
1851 ret = xstrdup (pulongest (record_full_list->u.end.insn_num));
1853 if (record_debug)
1855 if (ret)
1856 fprintf_unfiltered (gdb_stdlog,
1857 "record_full_get_bookmark returns %s\n", ret);
1858 else
1859 fprintf_unfiltered (gdb_stdlog,
1860 "record_full_get_bookmark returns NULL\n");
1862 return ret;
1865 /* "to_goto_bookmark" method for process record and prec over core. */
1867 static void
1868 record_full_goto_bookmark (gdb_byte *bookmark, int from_tty)
1870 if (record_debug)
1871 fprintf_unfiltered (gdb_stdlog,
1872 "record_full_goto_bookmark receives %s\n", bookmark);
1874 if (bookmark[0] == '\'' || bookmark[0] == '\"')
1876 if (bookmark[strlen (bookmark) - 1] != bookmark[0])
1877 error (_("Unbalanced quotes: %s"), bookmark);
1879 /* Strip trailing quote. */
1880 bookmark[strlen (bookmark) - 1] = '\0';
1881 /* Strip leading quote. */
1882 bookmark++;
1883 /* Pass along to cmd_record_full_goto. */
1886 cmd_record_goto ((char *) bookmark, from_tty);
1887 return;
1890 static void
1891 record_full_async (void (*callback) (enum inferior_event_type event_type,
1892 void *context), void *context)
1894 /* If we're on top of a line target (e.g., linux-nat, remote), then
1895 set it to async mode as well. Will be NULL if we're sitting on
1896 top of the core target, for "record restore". */
1897 if (record_full_beneath_to_async != NULL)
1898 record_full_beneath_to_async (callback, context);
1901 static int
1902 record_full_can_async_p (void)
1904 /* We only enable async when the user specifically asks for it. */
1905 return target_async_permitted;
1908 static int
1909 record_full_is_async_p (void)
1911 /* We only enable async when the user specifically asks for it. */
1912 return target_async_permitted;
1915 static enum exec_direction_kind
1916 record_full_execution_direction (void)
1918 return record_full_execution_dir;
1921 static void
1922 record_full_info (void)
1924 struct record_full_entry *p;
1926 if (RECORD_FULL_IS_REPLAY)
1927 printf_filtered (_("Replay mode:\n"));
1928 else
1929 printf_filtered (_("Record mode:\n"));
1931 /* Find entry for first actual instruction in the log. */
1932 for (p = record_full_first.next;
1933 p != NULL && p->type != record_full_end;
1934 p = p->next)
1937 /* Do we have a log at all? */
1938 if (p != NULL && p->type == record_full_end)
1940 /* Display instruction number for first instruction in the log. */
1941 printf_filtered (_("Lowest recorded instruction number is %s.\n"),
1942 pulongest (p->u.end.insn_num));
1944 /* If in replay mode, display where we are in the log. */
1945 if (RECORD_FULL_IS_REPLAY)
1946 printf_filtered (_("Current instruction number is %s.\n"),
1947 pulongest (record_full_list->u.end.insn_num));
1949 /* Display instruction number for last instruction in the log. */
1950 printf_filtered (_("Highest recorded instruction number is %s.\n"),
1951 pulongest (record_full_insn_count));
1953 /* Display log count. */
1954 printf_filtered (_("Log contains %u instructions.\n"),
1955 record_full_insn_num);
1957 else
1958 printf_filtered (_("No instructions have been logged.\n"));
1960 /* Display max log size. */
1961 printf_filtered (_("Max logged instructions is %u.\n"),
1962 record_full_insn_max_num);
1965 /* The "to_record_delete" target method. */
1967 static void
1968 record_full_delete (void)
1970 record_full_list_release_following (record_full_list);
1973 /* The "to_record_is_replaying" target method. */
1975 static int
1976 record_full_is_replaying (void)
1978 return RECORD_FULL_IS_REPLAY;
1981 /* Go to a specific entry. */
1983 static void
1984 record_full_goto_entry (struct record_full_entry *p)
1986 if (p == NULL)
1987 error (_("Target insn not found."));
1988 else if (p == record_full_list)
1989 error (_("Already at target insn."));
1990 else if (p->u.end.insn_num > record_full_list->u.end.insn_num)
1992 printf_filtered (_("Go forward to insn number %s\n"),
1993 pulongest (p->u.end.insn_num));
1994 record_full_goto_insn (p, EXEC_FORWARD);
1996 else
1998 printf_filtered (_("Go backward to insn number %s\n"),
1999 pulongest (p->u.end.insn_num));
2000 record_full_goto_insn (p, EXEC_REVERSE);
2003 registers_changed ();
2004 reinit_frame_cache ();
2005 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
2008 /* The "to_goto_record_begin" target method. */
2010 static void
2011 record_full_goto_begin (void)
2013 struct record_full_entry *p = NULL;
2015 for (p = &record_full_first; p != NULL; p = p->next)
2016 if (p->type == record_full_end)
2017 break;
2019 record_full_goto_entry (p);
2022 /* The "to_goto_record_end" target method. */
2024 static void
2025 record_full_goto_end (void)
2027 struct record_full_entry *p = NULL;
2029 for (p = record_full_list; p->next != NULL; p = p->next)
2031 for (; p!= NULL; p = p->prev)
2032 if (p->type == record_full_end)
2033 break;
2035 record_full_goto_entry (p);
2038 /* The "to_goto_record" target method. */
2040 static void
2041 record_full_goto (ULONGEST target_insn)
2043 struct record_full_entry *p = NULL;
2045 for (p = &record_full_first; p != NULL; p = p->next)
2046 if (p->type == record_full_end && p->u.end.insn_num == target_insn)
2047 break;
2049 record_full_goto_entry (p);
2052 static void
2053 init_record_full_ops (void)
2055 record_full_ops.to_shortname = "record-full";
2056 record_full_ops.to_longname = "Process record and replay target";
2057 record_full_ops.to_doc =
2058 "Log program while executing and replay execution from log.";
2059 record_full_ops.to_open = record_full_open;
2060 record_full_ops.to_close = record_full_close;
2061 record_full_ops.to_resume = record_full_resume;
2062 record_full_ops.to_wait = record_full_wait;
2063 record_full_ops.to_disconnect = record_disconnect;
2064 record_full_ops.to_detach = record_detach;
2065 record_full_ops.to_mourn_inferior = record_mourn_inferior;
2066 record_full_ops.to_kill = record_kill;
2067 record_full_ops.to_create_inferior = find_default_create_inferior;
2068 record_full_ops.to_store_registers = record_full_store_registers;
2069 record_full_ops.to_xfer_partial = record_full_xfer_partial;
2070 record_full_ops.to_insert_breakpoint = record_full_insert_breakpoint;
2071 record_full_ops.to_remove_breakpoint = record_full_remove_breakpoint;
2072 record_full_ops.to_stopped_by_watchpoint = record_full_stopped_by_watchpoint;
2073 record_full_ops.to_stopped_data_address = record_full_stopped_data_address;
2074 record_full_ops.to_can_execute_reverse = record_full_can_execute_reverse;
2075 record_full_ops.to_stratum = record_stratum;
2076 /* Add bookmark target methods. */
2077 record_full_ops.to_get_bookmark = record_full_get_bookmark;
2078 record_full_ops.to_goto_bookmark = record_full_goto_bookmark;
2079 record_full_ops.to_async = record_full_async;
2080 record_full_ops.to_can_async_p = record_full_can_async_p;
2081 record_full_ops.to_is_async_p = record_full_is_async_p;
2082 record_full_ops.to_execution_direction = record_full_execution_direction;
2083 record_full_ops.to_info_record = record_full_info;
2084 record_full_ops.to_save_record = record_full_save;
2085 record_full_ops.to_delete_record = record_full_delete;
2086 record_full_ops.to_record_is_replaying = record_full_is_replaying;
2087 record_full_ops.to_goto_record_begin = record_full_goto_begin;
2088 record_full_ops.to_goto_record_end = record_full_goto_end;
2089 record_full_ops.to_goto_record = record_full_goto;
2090 record_full_ops.to_magic = OPS_MAGIC;
2093 /* "to_resume" method for prec over corefile. */
2095 static void
2096 record_full_core_resume (struct target_ops *ops, ptid_t ptid, int step,
2097 enum gdb_signal signal)
2099 record_full_resume_step = step;
2100 record_full_resumed = 1;
2101 record_full_execution_dir = execution_direction;
2103 /* We are about to start executing the inferior (or simulate it),
2104 let's register it with the event loop. */
2105 if (target_can_async_p ())
2107 target_async (inferior_event_handler, 0);
2109 /* Notify the event loop there's an event to wait for. */
2110 mark_async_event_handler (record_full_async_inferior_event_token);
2114 /* "to_kill" method for prec over corefile. */
2116 static void
2117 record_full_core_kill (struct target_ops *ops)
2119 if (record_debug)
2120 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_core_kill\n");
2122 unpush_target (&record_full_core_ops);
2125 /* "to_fetch_registers" method for prec over corefile. */
2127 static void
2128 record_full_core_fetch_registers (struct target_ops *ops,
2129 struct regcache *regcache,
2130 int regno)
2132 if (regno < 0)
2134 int num = gdbarch_num_regs (get_regcache_arch (regcache));
2135 int i;
2137 for (i = 0; i < num; i ++)
2138 regcache_raw_supply (regcache, i,
2139 record_full_core_regbuf + MAX_REGISTER_SIZE * i);
2141 else
2142 regcache_raw_supply (regcache, regno,
2143 record_full_core_regbuf + MAX_REGISTER_SIZE * regno);
2146 /* "to_prepare_to_store" method for prec over corefile. */
2148 static void
2149 record_full_core_prepare_to_store (struct regcache *regcache)
2153 /* "to_store_registers" method for prec over corefile. */
2155 static void
2156 record_full_core_store_registers (struct target_ops *ops,
2157 struct regcache *regcache,
2158 int regno)
2160 if (record_full_gdb_operation_disable)
2161 regcache_raw_collect (regcache, regno,
2162 record_full_core_regbuf + MAX_REGISTER_SIZE * regno);
2163 else
2164 error (_("You can't do that without a process to debug."));
2167 /* "to_xfer_partial" method for prec over corefile. */
2169 static LONGEST
2170 record_full_core_xfer_partial (struct target_ops *ops,
2171 enum target_object object,
2172 const char *annex, gdb_byte *readbuf,
2173 const gdb_byte *writebuf, ULONGEST offset,
2174 LONGEST len)
2176 if (object == TARGET_OBJECT_MEMORY)
2178 if (record_full_gdb_operation_disable || !writebuf)
2180 struct target_section *p;
2182 for (p = record_full_core_start; p < record_full_core_end; p++)
2184 if (offset >= p->addr)
2186 struct record_full_core_buf_entry *entry;
2187 ULONGEST sec_offset;
2189 if (offset >= p->endaddr)
2190 continue;
2192 if (offset + len > p->endaddr)
2193 len = p->endaddr - offset;
2195 sec_offset = offset - p->addr;
2197 /* Read readbuf or write writebuf p, offset, len. */
2198 /* Check flags. */
2199 if (p->the_bfd_section->flags & SEC_CONSTRUCTOR
2200 || (p->the_bfd_section->flags & SEC_HAS_CONTENTS) == 0)
2202 if (readbuf)
2203 memset (readbuf, 0, len);
2204 return len;
2206 /* Get record_full_core_buf_entry. */
2207 for (entry = record_full_core_buf_list; entry;
2208 entry = entry->prev)
2209 if (entry->p == p)
2210 break;
2211 if (writebuf)
2213 if (!entry)
2215 /* Add a new entry. */
2216 entry = (struct record_full_core_buf_entry *)
2217 xmalloc
2218 (sizeof (struct record_full_core_buf_entry));
2219 entry->p = p;
2220 if (!bfd_malloc_and_get_section (p->bfd,
2221 p->the_bfd_section,
2222 &entry->buf))
2224 xfree (entry);
2225 return 0;
2227 entry->prev = record_full_core_buf_list;
2228 record_full_core_buf_list = entry;
2231 memcpy (entry->buf + sec_offset, writebuf,
2232 (size_t) len);
2234 else
2236 if (!entry)
2237 return record_full_beneath_to_xfer_partial
2238 (record_full_beneath_to_xfer_partial_ops,
2239 object, annex, readbuf, writebuf,
2240 offset, len);
2242 memcpy (readbuf, entry->buf + sec_offset,
2243 (size_t) len);
2246 return len;
2250 return -1;
2252 else
2253 error (_("You can't do that without a process to debug."));
2256 return record_full_beneath_to_xfer_partial
2257 (record_full_beneath_to_xfer_partial_ops, object, annex,
2258 readbuf, writebuf, offset, len);
2261 /* "to_insert_breakpoint" method for prec over corefile. */
2263 static int
2264 record_full_core_insert_breakpoint (struct gdbarch *gdbarch,
2265 struct bp_target_info *bp_tgt)
2267 return 0;
2270 /* "to_remove_breakpoint" method for prec over corefile. */
2272 static int
2273 record_full_core_remove_breakpoint (struct gdbarch *gdbarch,
2274 struct bp_target_info *bp_tgt)
2276 return 0;
2279 /* "to_has_execution" method for prec over corefile. */
2281 static int
2282 record_full_core_has_execution (struct target_ops *ops, ptid_t the_ptid)
2284 return 1;
2287 static void
2288 init_record_full_core_ops (void)
2290 record_full_core_ops.to_shortname = "record-core";
2291 record_full_core_ops.to_longname = "Process record and replay target";
2292 record_full_core_ops.to_doc =
2293 "Log program while executing and replay execution from log.";
2294 record_full_core_ops.to_open = record_full_open;
2295 record_full_core_ops.to_close = record_full_close;
2296 record_full_core_ops.to_resume = record_full_core_resume;
2297 record_full_core_ops.to_wait = record_full_wait;
2298 record_full_core_ops.to_kill = record_full_core_kill;
2299 record_full_core_ops.to_fetch_registers = record_full_core_fetch_registers;
2300 record_full_core_ops.to_prepare_to_store = record_full_core_prepare_to_store;
2301 record_full_core_ops.to_store_registers = record_full_core_store_registers;
2302 record_full_core_ops.to_xfer_partial = record_full_core_xfer_partial;
2303 record_full_core_ops.to_insert_breakpoint
2304 = record_full_core_insert_breakpoint;
2305 record_full_core_ops.to_remove_breakpoint
2306 = record_full_core_remove_breakpoint;
2307 record_full_core_ops.to_stopped_by_watchpoint
2308 = record_full_stopped_by_watchpoint;
2309 record_full_core_ops.to_stopped_data_address
2310 = record_full_stopped_data_address;
2311 record_full_core_ops.to_can_execute_reverse
2312 = record_full_can_execute_reverse;
2313 record_full_core_ops.to_has_execution = record_full_core_has_execution;
2314 record_full_core_ops.to_stratum = record_stratum;
2315 /* Add bookmark target methods. */
2316 record_full_core_ops.to_get_bookmark = record_full_get_bookmark;
2317 record_full_core_ops.to_goto_bookmark = record_full_goto_bookmark;
2318 record_full_core_ops.to_async = record_full_async;
2319 record_full_core_ops.to_can_async_p = record_full_can_async_p;
2320 record_full_core_ops.to_is_async_p = record_full_is_async_p;
2321 record_full_core_ops.to_execution_direction
2322 = record_full_execution_direction;
2323 record_full_core_ops.to_info_record = record_full_info;
2324 record_full_core_ops.to_delete_record = record_full_delete;
2325 record_full_core_ops.to_record_is_replaying = record_full_is_replaying;
2326 record_full_core_ops.to_goto_record_begin = record_full_goto_begin;
2327 record_full_core_ops.to_goto_record_end = record_full_goto_end;
2328 record_full_core_ops.to_goto_record = record_full_goto;
2329 record_full_core_ops.to_magic = OPS_MAGIC;
2332 /* Record log save-file format
2333 Version 1 (never released)
2335 Header:
2336 4 bytes: magic number htonl(0x20090829).
2337 NOTE: be sure to change whenever this file format changes!
2339 Records:
2340 record_full_end:
2341 1 byte: record type (record_full_end, see enum record_full_type).
2342 record_full_reg:
2343 1 byte: record type (record_full_reg, see enum record_full_type).
2344 8 bytes: register id (network byte order).
2345 MAX_REGISTER_SIZE bytes: register value.
2346 record_full_mem:
2347 1 byte: record type (record_full_mem, see enum record_full_type).
2348 8 bytes: memory length (network byte order).
2349 8 bytes: memory address (network byte order).
2350 n bytes: memory value (n == memory length).
2352 Version 2
2353 4 bytes: magic number netorder32(0x20091016).
2354 NOTE: be sure to change whenever this file format changes!
2356 Records:
2357 record_full_end:
2358 1 byte: record type (record_full_end, see enum record_full_type).
2359 4 bytes: signal
2360 4 bytes: instruction count
2361 record_full_reg:
2362 1 byte: record type (record_full_reg, see enum record_full_type).
2363 4 bytes: register id (network byte order).
2364 n bytes: register value (n == actual register size).
2365 (eg. 4 bytes for x86 general registers).
2366 record_full_mem:
2367 1 byte: record type (record_full_mem, see enum record_full_type).
2368 4 bytes: memory length (network byte order).
2369 8 bytes: memory address (network byte order).
2370 n bytes: memory value (n == memory length).
2374 /* bfdcore_read -- read bytes from a core file section. */
2376 static inline void
2377 bfdcore_read (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2379 int ret = bfd_get_section_contents (obfd, osec, buf, *offset, len);
2381 if (ret)
2382 *offset += len;
2383 else
2384 error (_("Failed to read %d bytes from core file %s ('%s')."),
2385 len, bfd_get_filename (obfd),
2386 bfd_errmsg (bfd_get_error ()));
2389 static inline uint64_t
2390 netorder64 (uint64_t input)
2392 uint64_t ret;
2394 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2395 BFD_ENDIAN_BIG, input);
2396 return ret;
2399 static inline uint32_t
2400 netorder32 (uint32_t input)
2402 uint32_t ret;
2404 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2405 BFD_ENDIAN_BIG, input);
2406 return ret;
2409 static inline uint16_t
2410 netorder16 (uint16_t input)
2412 uint16_t ret;
2414 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2415 BFD_ENDIAN_BIG, input);
2416 return ret;
2419 /* Restore the execution log from a core_bfd file. */
2420 static void
2421 record_full_restore (void)
2423 uint32_t magic;
2424 struct cleanup *old_cleanups;
2425 struct record_full_entry *rec;
2426 asection *osec;
2427 uint32_t osec_size;
2428 int bfd_offset = 0;
2429 struct regcache *regcache;
2431 /* We restore the execution log from the open core bfd,
2432 if there is one. */
2433 if (core_bfd == NULL)
2434 return;
2436 /* "record_full_restore" can only be called when record list is empty. */
2437 gdb_assert (record_full_first.next == NULL);
2439 if (record_debug)
2440 fprintf_unfiltered (gdb_stdlog, "Restoring recording from core file.\n");
2442 /* Now need to find our special note section. */
2443 osec = bfd_get_section_by_name (core_bfd, "null0");
2444 if (record_debug)
2445 fprintf_unfiltered (gdb_stdlog, "Find precord section %s.\n",
2446 osec ? "succeeded" : "failed");
2447 if (osec == NULL)
2448 return;
2449 osec_size = bfd_section_size (core_bfd, osec);
2450 if (record_debug)
2451 fprintf_unfiltered (gdb_stdlog, "%s", bfd_section_name (core_bfd, osec));
2453 /* Check the magic code. */
2454 bfdcore_read (core_bfd, osec, &magic, sizeof (magic), &bfd_offset);
2455 if (magic != RECORD_FULL_FILE_MAGIC)
2456 error (_("Version mis-match or file format error in core file %s."),
2457 bfd_get_filename (core_bfd));
2458 if (record_debug)
2459 fprintf_unfiltered (gdb_stdlog,
2460 " Reading 4-byte magic cookie "
2461 "RECORD_FULL_FILE_MAGIC (0x%s)\n",
2462 phex_nz (netorder32 (magic), 4));
2464 /* Restore the entries in recfd into record_full_arch_list_head and
2465 record_full_arch_list_tail. */
2466 record_full_arch_list_head = NULL;
2467 record_full_arch_list_tail = NULL;
2468 record_full_insn_num = 0;
2469 old_cleanups = make_cleanup (record_full_arch_list_cleanups, 0);
2470 regcache = get_current_regcache ();
2472 while (1)
2474 uint8_t rectype;
2475 uint32_t regnum, len, signal, count;
2476 uint64_t addr;
2478 /* We are finished when offset reaches osec_size. */
2479 if (bfd_offset >= osec_size)
2480 break;
2481 bfdcore_read (core_bfd, osec, &rectype, sizeof (rectype), &bfd_offset);
2483 switch (rectype)
2485 case record_full_reg: /* reg */
2486 /* Get register number to regnum. */
2487 bfdcore_read (core_bfd, osec, &regnum,
2488 sizeof (regnum), &bfd_offset);
2489 regnum = netorder32 (regnum);
2491 rec = record_full_reg_alloc (regcache, regnum);
2493 /* Get val. */
2494 bfdcore_read (core_bfd, osec, record_full_get_loc (rec),
2495 rec->u.reg.len, &bfd_offset);
2497 if (record_debug)
2498 fprintf_unfiltered (gdb_stdlog,
2499 " Reading register %d (1 "
2500 "plus %lu plus %d bytes)\n",
2501 rec->u.reg.num,
2502 (unsigned long) sizeof (regnum),
2503 rec->u.reg.len);
2504 break;
2506 case record_full_mem: /* mem */
2507 /* Get len. */
2508 bfdcore_read (core_bfd, osec, &len,
2509 sizeof (len), &bfd_offset);
2510 len = netorder32 (len);
2512 /* Get addr. */
2513 bfdcore_read (core_bfd, osec, &addr,
2514 sizeof (addr), &bfd_offset);
2515 addr = netorder64 (addr);
2517 rec = record_full_mem_alloc (addr, len);
2519 /* Get val. */
2520 bfdcore_read (core_bfd, osec, record_full_get_loc (rec),
2521 rec->u.mem.len, &bfd_offset);
2523 if (record_debug)
2524 fprintf_unfiltered (gdb_stdlog,
2525 " Reading memory %s (1 plus "
2526 "%lu plus %lu plus %d bytes)\n",
2527 paddress (get_current_arch (),
2528 rec->u.mem.addr),
2529 (unsigned long) sizeof (addr),
2530 (unsigned long) sizeof (len),
2531 rec->u.mem.len);
2532 break;
2534 case record_full_end: /* end */
2535 rec = record_full_end_alloc ();
2536 record_full_insn_num ++;
2538 /* Get signal value. */
2539 bfdcore_read (core_bfd, osec, &signal,
2540 sizeof (signal), &bfd_offset);
2541 signal = netorder32 (signal);
2542 rec->u.end.sigval = signal;
2544 /* Get insn count. */
2545 bfdcore_read (core_bfd, osec, &count,
2546 sizeof (count), &bfd_offset);
2547 count = netorder32 (count);
2548 rec->u.end.insn_num = count;
2549 record_full_insn_count = count + 1;
2550 if (record_debug)
2551 fprintf_unfiltered (gdb_stdlog,
2552 " Reading record_full_end (1 + "
2553 "%lu + %lu bytes), offset == %s\n",
2554 (unsigned long) sizeof (signal),
2555 (unsigned long) sizeof (count),
2556 paddress (get_current_arch (),
2557 bfd_offset));
2558 break;
2560 default:
2561 error (_("Bad entry type in core file %s."),
2562 bfd_get_filename (core_bfd));
2563 break;
2566 /* Add rec to record arch list. */
2567 record_full_arch_list_add (rec);
2570 discard_cleanups (old_cleanups);
2572 /* Add record_full_arch_list_head to the end of record list. */
2573 record_full_first.next = record_full_arch_list_head;
2574 record_full_arch_list_head->prev = &record_full_first;
2575 record_full_arch_list_tail->next = NULL;
2576 record_full_list = &record_full_first;
2578 /* Update record_full_insn_max_num. */
2579 if (record_full_insn_num > record_full_insn_max_num)
2581 record_full_insn_max_num = record_full_insn_num;
2582 warning (_("Auto increase record/replay buffer limit to %u."),
2583 record_full_insn_max_num);
2586 /* Succeeded. */
2587 printf_filtered (_("Restored records from core file %s.\n"),
2588 bfd_get_filename (core_bfd));
2590 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
2593 /* bfdcore_write -- write bytes into a core file section. */
2595 static inline void
2596 bfdcore_write (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2598 int ret = bfd_set_section_contents (obfd, osec, buf, *offset, len);
2600 if (ret)
2601 *offset += len;
2602 else
2603 error (_("Failed to write %d bytes to core file %s ('%s')."),
2604 len, bfd_get_filename (obfd),
2605 bfd_errmsg (bfd_get_error ()));
2608 /* Restore the execution log from a file. We use a modified elf
2609 corefile format, with an extra section for our data. */
2611 static void
2612 cmd_record_full_restore (char *args, int from_tty)
2614 core_file_command (args, from_tty);
2615 record_full_open (args, from_tty);
2618 static void
2619 record_full_save_cleanups (void *data)
2621 bfd *obfd = data;
2622 char *pathname = xstrdup (bfd_get_filename (obfd));
2624 gdb_bfd_unref (obfd);
2625 unlink (pathname);
2626 xfree (pathname);
2629 /* Save the execution log to a file. We use a modified elf corefile
2630 format, with an extra section for our data. */
2632 static void
2633 record_full_save (char *recfilename)
2635 struct record_full_entry *cur_record_full_list;
2636 uint32_t magic;
2637 struct regcache *regcache;
2638 struct gdbarch *gdbarch;
2639 struct cleanup *old_cleanups;
2640 struct cleanup *set_cleanups;
2641 bfd *obfd;
2642 int save_size = 0;
2643 asection *osec = NULL;
2644 int bfd_offset = 0;
2646 /* Open the save file. */
2647 if (record_debug)
2648 fprintf_unfiltered (gdb_stdlog, "Saving execution log to core file '%s'\n",
2649 recfilename);
2651 /* Open the output file. */
2652 obfd = create_gcore_bfd (recfilename);
2653 old_cleanups = make_cleanup (record_full_save_cleanups, obfd);
2655 /* Save the current record entry to "cur_record_full_list". */
2656 cur_record_full_list = record_full_list;
2658 /* Get the values of regcache and gdbarch. */
2659 regcache = get_current_regcache ();
2660 gdbarch = get_regcache_arch (regcache);
2662 /* Disable the GDB operation record. */
2663 set_cleanups = record_full_gdb_operation_disable_set ();
2665 /* Reverse execute to the begin of record list. */
2666 while (1)
2668 /* Check for beginning and end of log. */
2669 if (record_full_list == &record_full_first)
2670 break;
2672 record_full_exec_insn (regcache, gdbarch, record_full_list);
2674 if (record_full_list->prev)
2675 record_full_list = record_full_list->prev;
2678 /* Compute the size needed for the extra bfd section. */
2679 save_size = 4; /* magic cookie */
2680 for (record_full_list = record_full_first.next; record_full_list;
2681 record_full_list = record_full_list->next)
2682 switch (record_full_list->type)
2684 case record_full_end:
2685 save_size += 1 + 4 + 4;
2686 break;
2687 case record_full_reg:
2688 save_size += 1 + 4 + record_full_list->u.reg.len;
2689 break;
2690 case record_full_mem:
2691 save_size += 1 + 4 + 8 + record_full_list->u.mem.len;
2692 break;
2695 /* Make the new bfd section. */
2696 osec = bfd_make_section_anyway_with_flags (obfd, "precord",
2697 SEC_HAS_CONTENTS
2698 | SEC_READONLY);
2699 if (osec == NULL)
2700 error (_("Failed to create 'precord' section for corefile %s: %s"),
2701 recfilename,
2702 bfd_errmsg (bfd_get_error ()));
2703 bfd_set_section_size (obfd, osec, save_size);
2704 bfd_set_section_vma (obfd, osec, 0);
2705 bfd_set_section_alignment (obfd, osec, 0);
2706 bfd_section_lma (obfd, osec) = 0;
2708 /* Save corefile state. */
2709 write_gcore_file (obfd);
2711 /* Write out the record log. */
2712 /* Write the magic code. */
2713 magic = RECORD_FULL_FILE_MAGIC;
2714 if (record_debug)
2715 fprintf_unfiltered (gdb_stdlog,
2716 " Writing 4-byte magic cookie "
2717 "RECORD_FULL_FILE_MAGIC (0x%s)\n",
2718 phex_nz (magic, 4));
2719 bfdcore_write (obfd, osec, &magic, sizeof (magic), &bfd_offset);
2721 /* Save the entries to recfd and forward execute to the end of
2722 record list. */
2723 record_full_list = &record_full_first;
2724 while (1)
2726 /* Save entry. */
2727 if (record_full_list != &record_full_first)
2729 uint8_t type;
2730 uint32_t regnum, len, signal, count;
2731 uint64_t addr;
2733 type = record_full_list->type;
2734 bfdcore_write (obfd, osec, &type, sizeof (type), &bfd_offset);
2736 switch (record_full_list->type)
2738 case record_full_reg: /* reg */
2739 if (record_debug)
2740 fprintf_unfiltered (gdb_stdlog,
2741 " Writing register %d (1 "
2742 "plus %lu plus %d bytes)\n",
2743 record_full_list->u.reg.num,
2744 (unsigned long) sizeof (regnum),
2745 record_full_list->u.reg.len);
2747 /* Write regnum. */
2748 regnum = netorder32 (record_full_list->u.reg.num);
2749 bfdcore_write (obfd, osec, &regnum,
2750 sizeof (regnum), &bfd_offset);
2752 /* Write regval. */
2753 bfdcore_write (obfd, osec,
2754 record_full_get_loc (record_full_list),
2755 record_full_list->u.reg.len, &bfd_offset);
2756 break;
2758 case record_full_mem: /* mem */
2759 if (record_debug)
2760 fprintf_unfiltered (gdb_stdlog,
2761 " Writing memory %s (1 plus "
2762 "%lu plus %lu plus %d bytes)\n",
2763 paddress (gdbarch,
2764 record_full_list->u.mem.addr),
2765 (unsigned long) sizeof (addr),
2766 (unsigned long) sizeof (len),
2767 record_full_list->u.mem.len);
2769 /* Write memlen. */
2770 len = netorder32 (record_full_list->u.mem.len);
2771 bfdcore_write (obfd, osec, &len, sizeof (len), &bfd_offset);
2773 /* Write memaddr. */
2774 addr = netorder64 (record_full_list->u.mem.addr);
2775 bfdcore_write (obfd, osec, &addr,
2776 sizeof (addr), &bfd_offset);
2778 /* Write memval. */
2779 bfdcore_write (obfd, osec,
2780 record_full_get_loc (record_full_list),
2781 record_full_list->u.mem.len, &bfd_offset);
2782 break;
2784 case record_full_end:
2785 if (record_debug)
2786 fprintf_unfiltered (gdb_stdlog,
2787 " Writing record_full_end (1 + "
2788 "%lu + %lu bytes)\n",
2789 (unsigned long) sizeof (signal),
2790 (unsigned long) sizeof (count));
2791 /* Write signal value. */
2792 signal = netorder32 (record_full_list->u.end.sigval);
2793 bfdcore_write (obfd, osec, &signal,
2794 sizeof (signal), &bfd_offset);
2796 /* Write insn count. */
2797 count = netorder32 (record_full_list->u.end.insn_num);
2798 bfdcore_write (obfd, osec, &count,
2799 sizeof (count), &bfd_offset);
2800 break;
2804 /* Execute entry. */
2805 record_full_exec_insn (regcache, gdbarch, record_full_list);
2807 if (record_full_list->next)
2808 record_full_list = record_full_list->next;
2809 else
2810 break;
2813 /* Reverse execute to cur_record_full_list. */
2814 while (1)
2816 /* Check for beginning and end of log. */
2817 if (record_full_list == cur_record_full_list)
2818 break;
2820 record_full_exec_insn (regcache, gdbarch, record_full_list);
2822 if (record_full_list->prev)
2823 record_full_list = record_full_list->prev;
2826 do_cleanups (set_cleanups);
2827 gdb_bfd_unref (obfd);
2828 discard_cleanups (old_cleanups);
2830 /* Succeeded. */
2831 printf_filtered (_("Saved core file %s with execution log.\n"),
2832 recfilename);
2835 /* record_full_goto_insn -- rewind the record log (forward or backward,
2836 depending on DIR) to the given entry, changing the program state
2837 correspondingly. */
2839 static void
2840 record_full_goto_insn (struct record_full_entry *entry,
2841 enum exec_direction_kind dir)
2843 struct cleanup *set_cleanups = record_full_gdb_operation_disable_set ();
2844 struct regcache *regcache = get_current_regcache ();
2845 struct gdbarch *gdbarch = get_regcache_arch (regcache);
2847 /* Assume everything is valid: we will hit the entry,
2848 and we will not hit the end of the recording. */
2850 if (dir == EXEC_FORWARD)
2851 record_full_list = record_full_list->next;
2855 record_full_exec_insn (regcache, gdbarch, record_full_list);
2856 if (dir == EXEC_REVERSE)
2857 record_full_list = record_full_list->prev;
2858 else
2859 record_full_list = record_full_list->next;
2860 } while (record_full_list != entry);
2861 do_cleanups (set_cleanups);
2864 /* Alias for "target record-full". */
2866 static void
2867 cmd_record_full_start (char *args, int from_tty)
2869 execute_command ("target record-full", from_tty);
2872 static void
2873 set_record_full_insn_max_num (char *args, int from_tty,
2874 struct cmd_list_element *c)
2876 if (record_full_insn_num > record_full_insn_max_num)
2878 /* Count down record_full_insn_num while releasing records from list. */
2879 while (record_full_insn_num > record_full_insn_max_num)
2881 record_full_list_release_first ();
2882 record_full_insn_num--;
2887 /* The "set record full" command. */
2889 static void
2890 set_record_full_command (char *args, int from_tty)
2892 printf_unfiltered (_("\"set record full\" must be followed "
2893 "by an apporpriate subcommand.\n"));
2894 help_list (set_record_full_cmdlist, "set record full ", all_commands,
2895 gdb_stdout);
2898 /* The "show record full" command. */
2900 static void
2901 show_record_full_command (char *args, int from_tty)
2903 cmd_show_list (show_record_full_cmdlist, from_tty, "");
2906 /* Provide a prototype to silence -Wmissing-prototypes. */
2907 extern initialize_file_ftype _initialize_record_full;
2909 void
2910 _initialize_record_full (void)
2912 struct cmd_list_element *c;
2914 /* Init record_full_first. */
2915 record_full_first.prev = NULL;
2916 record_full_first.next = NULL;
2917 record_full_first.type = record_full_end;
2919 init_record_full_ops ();
2920 add_target (&record_full_ops);
2921 add_deprecated_target_alias (&record_full_ops, "record");
2922 init_record_full_core_ops ();
2923 add_target (&record_full_core_ops);
2925 add_prefix_cmd ("full", class_obscure, cmd_record_full_start,
2926 _("Start full execution recording."), &record_full_cmdlist,
2927 "record full ", 0, &record_cmdlist);
2929 c = add_cmd ("restore", class_obscure, cmd_record_full_restore,
2930 _("Restore the execution log from a file.\n\
2931 Argument is filename. File must be created with 'record save'."),
2932 &record_full_cmdlist);
2933 set_cmd_completer (c, filename_completer);
2935 /* Deprecate the old version without "full" prefix. */
2936 c = add_alias_cmd ("restore", "full restore", class_obscure, 1,
2937 &record_cmdlist);
2938 set_cmd_completer (c, filename_completer);
2939 deprecate_cmd (c, "record full restore");
2941 add_prefix_cmd ("full", class_support, set_record_full_command,
2942 _("Set record options"), &set_record_full_cmdlist,
2943 "set record full ", 0, &set_record_cmdlist);
2945 add_prefix_cmd ("full", class_support, show_record_full_command,
2946 _("Show record options"), &show_record_full_cmdlist,
2947 "show record full ", 0, &show_record_cmdlist);
2949 /* Record instructions number limit command. */
2950 add_setshow_boolean_cmd ("stop-at-limit", no_class,
2951 &record_full_stop_at_limit, _("\
2952 Set whether record/replay stops when record/replay buffer becomes full."), _("\
2953 Show whether record/replay stops when record/replay buffer becomes full."),
2954 _("Default is ON.\n\
2955 When ON, if the record/replay buffer becomes full, ask user what to do.\n\
2956 When OFF, if the record/replay buffer becomes full,\n\
2957 delete the oldest recorded instruction to make room for each new one."),
2958 NULL, NULL,
2959 &set_record_full_cmdlist, &show_record_full_cmdlist);
2961 c = add_alias_cmd ("stop-at-limit", "full stop-at-limit", no_class, 1,
2962 &set_record_cmdlist);
2963 deprecate_cmd (c, "set record full stop-at-limit");
2965 c = add_alias_cmd ("stop-at-limit", "full stop-at-limit", no_class, 1,
2966 &show_record_cmdlist);
2967 deprecate_cmd (c, "show record full stop-at-limit");
2969 add_setshow_uinteger_cmd ("insn-number-max", no_class,
2970 &record_full_insn_max_num,
2971 _("Set record/replay buffer limit."),
2972 _("Show record/replay buffer limit."), _("\
2973 Set the maximum number of instructions to be stored in the\n\
2974 record/replay buffer. A value of either \"unlimited\" or zero means no\n\
2975 limit. Default is 200000."),
2976 set_record_full_insn_max_num,
2977 NULL, &set_record_full_cmdlist,
2978 &show_record_full_cmdlist);
2980 c = add_alias_cmd ("insn-number-max", "full insn-number-max", no_class, 1,
2981 &set_record_cmdlist);
2982 deprecate_cmd (c, "set record full insn-number-max");
2984 c = add_alias_cmd ("insn-number-max", "full insn-number-max", no_class, 1,
2985 &show_record_cmdlist);
2986 deprecate_cmd (c, "show record full insn-number-max");
2988 add_setshow_boolean_cmd ("memory-query", no_class,
2989 &record_full_memory_query, _("\
2990 Set whether query if PREC cannot record memory change of next instruction."),
2991 _("\
2992 Show whether query if PREC cannot record memory change of next instruction."),
2993 _("\
2994 Default is OFF.\n\
2995 When ON, query if PREC cannot record memory change of next instruction."),
2996 NULL, NULL,
2997 &set_record_full_cmdlist,
2998 &show_record_full_cmdlist);
3000 c = add_alias_cmd ("memory-query", "full memory-query", no_class, 1,
3001 &set_record_cmdlist);
3002 deprecate_cmd (c, "set record full memory-query");
3004 c = add_alias_cmd ("memory-query", "full memory-query", no_class, 1,
3005 &show_record_cmdlist);
3006 deprecate_cmd (c, "show record full memory-query");