2010-10-11 Andreas Krebbel <Andreas.Krebbel@de.ibm.com>
[binutils-gdb.git] / gdb / record.c
blob3d423a5e63cf00b0404e30f228a2b3e11dd5e773
1 /* Process record and replay target for GDB, the GNU debugger.
3 Copyright (C) 2008, 2009, 2010 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 "elf-bfd.h"
32 #include "gcore.h"
34 #include <signal.h>
36 /* This module implements "target record", also known as "process
37 record and replay". This target sits on top of a "normal" target
38 (a target that "has execution"), and provides a record and replay
39 functionality, including reverse debugging.
41 Target record has two modes: recording, and replaying.
43 In record mode, we intercept the to_resume and to_wait methods.
44 Whenever gdb resumes the target, we run the target in single step
45 mode, and we build up an execution log in which, for each executed
46 instruction, we record all changes in memory and register state.
47 This is invisible to the user, to whom it just looks like an
48 ordinary debugging session (except for performance degredation).
50 In replay mode, instead of actually letting the inferior run as a
51 process, we simulate its execution by playing back the recorded
52 execution log. For each instruction in the log, we simulate the
53 instruction's side effects by duplicating the changes that it would
54 have made on memory and registers. */
56 #define DEFAULT_RECORD_INSN_MAX_NUM 200000
58 #define RECORD_IS_REPLAY \
59 (record_list->next || execution_direction == EXEC_REVERSE)
61 #define RECORD_FILE_MAGIC netorder32(0x20091016)
63 /* These are the core structs of the process record functionality.
65 A record_entry is a record of the value change of a register
66 ("record_reg") or a part of memory ("record_mem"). And each
67 instruction must have a struct record_entry ("record_end") that
68 indicates that this is the last struct record_entry of this
69 instruction.
71 Each struct record_entry is linked to "record_list" by "prev" and
72 "next" pointers. */
74 struct record_mem_entry
76 CORE_ADDR addr;
77 int len;
78 /* Set this flag if target memory for this entry
79 can no longer be accessed. */
80 int mem_entry_not_accessible;
81 union
83 gdb_byte *ptr;
84 gdb_byte buf[sizeof (gdb_byte *)];
85 } u;
88 struct record_reg_entry
90 unsigned short num;
91 unsigned short len;
92 union
94 gdb_byte *ptr;
95 gdb_byte buf[2 * sizeof (gdb_byte *)];
96 } u;
99 struct record_end_entry
101 enum target_signal sigval;
102 ULONGEST insn_num;
105 enum record_type
107 record_end = 0,
108 record_reg,
109 record_mem
112 /* This is the data structure that makes up the execution log.
114 The execution log consists of a single linked list of entries
115 of type "struct record_entry". It is doubly linked so that it
116 can be traversed in either direction.
118 The start of the list is anchored by a struct called
119 "record_first". The pointer "record_list" either points to the
120 last entry that was added to the list (in record mode), or to the
121 next entry in the list that will be executed (in replay mode).
123 Each list element (struct record_entry), in addition to next and
124 prev pointers, consists of a union of three entry types: mem, reg,
125 and end. A field called "type" determines which entry type is
126 represented by a given list element.
128 Each instruction that is added to the execution log is represented
129 by a variable number of list elements ('entries'). The instruction
130 will have one "reg" entry for each register that is changed by
131 executing the instruction (including the PC in every case). It
132 will also have one "mem" entry for each memory change. Finally,
133 each instruction will have an "end" entry that separates it from
134 the changes associated with the next instruction. */
136 struct record_entry
138 struct record_entry *prev;
139 struct record_entry *next;
140 enum record_type type;
141 union
143 /* reg */
144 struct record_reg_entry reg;
145 /* mem */
146 struct record_mem_entry mem;
147 /* end */
148 struct record_end_entry end;
149 } u;
152 /* This is the debug switch for process record. */
153 int record_debug = 0;
155 /* If true, query if PREC cannot record memory
156 change of next instruction. */
157 int record_memory_query = 0;
159 struct record_core_buf_entry
161 struct record_core_buf_entry *prev;
162 struct target_section *p;
163 bfd_byte *buf;
166 /* Record buf with core target. */
167 static gdb_byte *record_core_regbuf = NULL;
168 static struct target_section *record_core_start;
169 static struct target_section *record_core_end;
170 static struct record_core_buf_entry *record_core_buf_list = NULL;
172 /* The following variables are used for managing the linked list that
173 represents the execution log.
175 record_first is the anchor that holds down the beginning of the list.
177 record_list serves two functions:
178 1) In record mode, it anchors the end of the list.
179 2) In replay mode, it traverses the list and points to
180 the next instruction that must be emulated.
182 record_arch_list_head and record_arch_list_tail are used to manage
183 a separate list, which is used to build up the change elements of
184 the currently executing instruction during record mode. When this
185 instruction has been completely annotated in the "arch list", it
186 will be appended to the main execution log. */
188 static struct record_entry record_first;
189 static struct record_entry *record_list = &record_first;
190 static struct record_entry *record_arch_list_head = NULL;
191 static struct record_entry *record_arch_list_tail = NULL;
193 /* 1 ask user. 0 auto delete the last struct record_entry. */
194 static int record_stop_at_limit = 1;
195 /* Maximum allowed number of insns in execution log. */
196 static unsigned int record_insn_max_num = DEFAULT_RECORD_INSN_MAX_NUM;
197 /* Actual count of insns presently in execution log. */
198 static int record_insn_num = 0;
199 /* Count of insns logged so far (may be larger
200 than count of insns presently in execution log). */
201 static ULONGEST record_insn_count;
203 /* The target_ops of process record. */
204 static struct target_ops record_ops;
205 static struct target_ops record_core_ops;
207 /* The beneath function pointers. */
208 static struct target_ops *record_beneath_to_resume_ops;
209 static void (*record_beneath_to_resume) (struct target_ops *, ptid_t, int,
210 enum target_signal);
211 static struct target_ops *record_beneath_to_wait_ops;
212 static ptid_t (*record_beneath_to_wait) (struct target_ops *, ptid_t,
213 struct target_waitstatus *,
214 int);
215 static struct target_ops *record_beneath_to_store_registers_ops;
216 static void (*record_beneath_to_store_registers) (struct target_ops *,
217 struct regcache *,
218 int regno);
219 static struct target_ops *record_beneath_to_xfer_partial_ops;
220 static LONGEST (*record_beneath_to_xfer_partial) (struct target_ops *ops,
221 enum target_object object,
222 const char *annex,
223 gdb_byte *readbuf,
224 const gdb_byte *writebuf,
225 ULONGEST offset,
226 LONGEST len);
227 static int (*record_beneath_to_insert_breakpoint) (struct gdbarch *,
228 struct bp_target_info *);
229 static int (*record_beneath_to_remove_breakpoint) (struct gdbarch *,
230 struct bp_target_info *);
231 static int (*record_beneath_to_stopped_by_watchpoint) (void);
232 static int (*record_beneath_to_stopped_data_address) (struct target_ops *,
233 CORE_ADDR *);
235 /* Alloc and free functions for record_reg, record_mem, and record_end
236 entries. */
238 /* Alloc a record_reg record entry. */
240 static inline struct record_entry *
241 record_reg_alloc (struct regcache *regcache, int regnum)
243 struct record_entry *rec;
244 struct gdbarch *gdbarch = get_regcache_arch (regcache);
246 rec = (struct record_entry *) xcalloc (1, sizeof (struct record_entry));
247 rec->type = record_reg;
248 rec->u.reg.num = regnum;
249 rec->u.reg.len = register_size (gdbarch, regnum);
250 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
251 rec->u.reg.u.ptr = (gdb_byte *) xmalloc (rec->u.reg.len);
253 return rec;
256 /* Free a record_reg record entry. */
258 static inline void
259 record_reg_release (struct record_entry *rec)
261 gdb_assert (rec->type == record_reg);
262 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
263 xfree (rec->u.reg.u.ptr);
264 xfree (rec);
267 /* Alloc a record_mem record entry. */
269 static inline struct record_entry *
270 record_mem_alloc (CORE_ADDR addr, int len)
272 struct record_entry *rec;
274 rec = (struct record_entry *) xcalloc (1, sizeof (struct record_entry));
275 rec->type = record_mem;
276 rec->u.mem.addr = addr;
277 rec->u.mem.len = len;
278 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
279 rec->u.mem.u.ptr = (gdb_byte *) xmalloc (len);
281 return rec;
284 /* Free a record_mem record entry. */
286 static inline void
287 record_mem_release (struct record_entry *rec)
289 gdb_assert (rec->type == record_mem);
290 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
291 xfree (rec->u.mem.u.ptr);
292 xfree (rec);
295 /* Alloc a record_end record entry. */
297 static inline struct record_entry *
298 record_end_alloc (void)
300 struct record_entry *rec;
302 rec = (struct record_entry *) xcalloc (1, sizeof (struct record_entry));
303 rec->type = record_end;
305 return rec;
308 /* Free a record_end record entry. */
310 static inline void
311 record_end_release (struct record_entry *rec)
313 xfree (rec);
316 /* Free one record entry, any type.
317 Return entry->type, in case caller wants to know. */
319 static inline enum record_type
320 record_entry_release (struct record_entry *rec)
322 enum record_type type = rec->type;
324 switch (type) {
325 case record_reg:
326 record_reg_release (rec);
327 break;
328 case record_mem:
329 record_mem_release (rec);
330 break;
331 case record_end:
332 record_end_release (rec);
333 break;
335 return type;
338 /* Free all record entries in list pointed to by REC. */
340 static void
341 record_list_release (struct record_entry *rec)
343 if (!rec)
344 return;
346 while (rec->next)
347 rec = rec->next;
349 while (rec->prev)
351 rec = rec->prev;
352 record_entry_release (rec->next);
355 if (rec == &record_first)
357 record_insn_num = 0;
358 record_first.next = NULL;
360 else
361 record_entry_release (rec);
364 /* Free all record entries forward of the given list position. */
366 static void
367 record_list_release_following (struct record_entry *rec)
369 struct record_entry *tmp = rec->next;
371 rec->next = NULL;
372 while (tmp)
374 rec = tmp->next;
375 if (record_entry_release (tmp) == record_end)
377 record_insn_num--;
378 record_insn_count--;
380 tmp = rec;
384 /* Delete the first instruction from the beginning of the log, to make
385 room for adding a new instruction at the end of the log.
387 Note -- this function does not modify record_insn_num. */
389 static void
390 record_list_release_first (void)
392 struct record_entry *tmp;
394 if (!record_first.next)
395 return;
397 /* Loop until a record_end. */
398 while (1)
400 /* Cut record_first.next out of the linked list. */
401 tmp = record_first.next;
402 record_first.next = tmp->next;
403 tmp->next->prev = &record_first;
405 /* tmp is now isolated, and can be deleted. */
406 if (record_entry_release (tmp) == record_end)
407 break; /* End loop at first record_end. */
409 if (!record_first.next)
411 gdb_assert (record_insn_num == 1);
412 break; /* End loop when list is empty. */
417 /* Add a struct record_entry to record_arch_list. */
419 static void
420 record_arch_list_add (struct record_entry *rec)
422 if (record_debug > 1)
423 fprintf_unfiltered (gdb_stdlog,
424 "Process record: record_arch_list_add %s.\n",
425 host_address_to_string (rec));
427 if (record_arch_list_tail)
429 record_arch_list_tail->next = rec;
430 rec->prev = record_arch_list_tail;
431 record_arch_list_tail = rec;
433 else
435 record_arch_list_head = rec;
436 record_arch_list_tail = rec;
440 /* Return the value storage location of a record entry. */
441 static inline gdb_byte *
442 record_get_loc (struct record_entry *rec)
444 switch (rec->type) {
445 case record_mem:
446 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
447 return rec->u.mem.u.ptr;
448 else
449 return rec->u.mem.u.buf;
450 case record_reg:
451 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
452 return rec->u.reg.u.ptr;
453 else
454 return rec->u.reg.u.buf;
455 case record_end:
456 default:
457 gdb_assert_not_reached ("unexpected record_entry type");
458 return NULL;
462 /* Record the value of a register NUM to record_arch_list. */
465 record_arch_list_add_reg (struct regcache *regcache, int regnum)
467 struct record_entry *rec;
469 if (record_debug > 1)
470 fprintf_unfiltered (gdb_stdlog,
471 "Process record: add register num = %d to "
472 "record list.\n",
473 regnum);
475 rec = record_reg_alloc (regcache, regnum);
477 regcache_raw_read (regcache, regnum, record_get_loc (rec));
479 record_arch_list_add (rec);
481 return 0;
484 /* Record the value of a region of memory whose address is ADDR and
485 length is LEN to record_arch_list. */
488 record_arch_list_add_mem (CORE_ADDR addr, int len)
490 struct record_entry *rec;
492 if (record_debug > 1)
493 fprintf_unfiltered (gdb_stdlog,
494 "Process record: add mem addr = %s len = %d to "
495 "record list.\n",
496 paddress (target_gdbarch, addr), len);
498 if (!addr) /* FIXME: Why? Some arch must permit it... */
499 return 0;
501 rec = record_mem_alloc (addr, len);
503 if (target_read_memory (addr, record_get_loc (rec), len))
505 if (record_debug)
506 fprintf_unfiltered (gdb_stdlog,
507 "Process record: error reading memory at "
508 "addr = %s len = %d.\n",
509 paddress (target_gdbarch, addr), len);
510 record_mem_release (rec);
511 return -1;
514 record_arch_list_add (rec);
516 return 0;
519 /* Add a record_end type struct record_entry to record_arch_list. */
522 record_arch_list_add_end (void)
524 struct record_entry *rec;
526 if (record_debug > 1)
527 fprintf_unfiltered (gdb_stdlog,
528 "Process record: add end to arch list.\n");
530 rec = record_end_alloc ();
531 rec->u.end.sigval = TARGET_SIGNAL_0;
532 rec->u.end.insn_num = ++record_insn_count;
534 record_arch_list_add (rec);
536 return 0;
539 static void
540 record_check_insn_num (int set_terminal)
542 if (record_insn_max_num)
544 gdb_assert (record_insn_num <= record_insn_max_num);
545 if (record_insn_num == record_insn_max_num)
547 /* Ask user what to do. */
548 if (record_stop_at_limit)
550 int q;
552 if (set_terminal)
553 target_terminal_ours ();
554 q = yquery (_("Do you want to auto delete previous execution "
555 "log entries when record/replay buffer becomes "
556 "full (record stop-at-limit)?"));
557 if (set_terminal)
558 target_terminal_inferior ();
559 if (q)
560 record_stop_at_limit = 0;
561 else
562 error (_("Process record: stopped by user."));
568 static void
569 record_arch_list_cleanups (void *ignore)
571 record_list_release (record_arch_list_tail);
574 /* Before inferior step (when GDB record the running message, inferior
575 only can step), GDB will call this function to record the values to
576 record_list. This function will call gdbarch_process_record to
577 record the running message of inferior and set them to
578 record_arch_list, and add it to record_list. */
580 static int
581 record_message (struct regcache *regcache, enum target_signal signal)
583 int ret;
584 struct gdbarch *gdbarch = get_regcache_arch (regcache);
585 struct cleanup *old_cleanups = make_cleanup (record_arch_list_cleanups, 0);
587 record_arch_list_head = NULL;
588 record_arch_list_tail = NULL;
590 /* Check record_insn_num. */
591 record_check_insn_num (1);
593 /* If gdb sends a signal value to target_resume,
594 save it in the 'end' field of the previous instruction.
596 Maybe process record should record what really happened,
597 rather than what gdb pretends has happened.
599 So if Linux delivered the signal to the child process during
600 the record mode, we will record it and deliver it again in
601 the replay mode.
603 If user says "ignore this signal" during the record mode, then
604 it will be ignored again during the replay mode (no matter if
605 the user says something different, like "deliver this signal"
606 during the replay mode).
608 User should understand that nothing he does during the replay
609 mode will change the behavior of the child. If he tries,
610 then that is a user error.
612 But we should still deliver the signal to gdb during the replay,
613 if we delivered it during the recording. Therefore we should
614 record the signal during record_wait, not record_resume. */
615 if (record_list != &record_first) /* FIXME better way to check */
617 gdb_assert (record_list->type == record_end);
618 record_list->u.end.sigval = signal;
621 if (signal == TARGET_SIGNAL_0
622 || !gdbarch_process_record_signal_p (gdbarch))
623 ret = gdbarch_process_record (gdbarch,
624 regcache,
625 regcache_read_pc (regcache));
626 else
627 ret = gdbarch_process_record_signal (gdbarch,
628 regcache,
629 signal);
631 if (ret > 0)
632 error (_("Process record: inferior program stopped."));
633 if (ret < 0)
634 error (_("Process record: failed to record execution log."));
636 discard_cleanups (old_cleanups);
638 record_list->next = record_arch_list_head;
639 record_arch_list_head->prev = record_list;
640 record_list = record_arch_list_tail;
642 if (record_insn_num == record_insn_max_num && record_insn_max_num)
643 record_list_release_first ();
644 else
645 record_insn_num++;
647 return 1;
650 struct record_message_args {
651 struct regcache *regcache;
652 enum target_signal signal;
655 static int
656 record_message_wrapper (void *args)
658 struct record_message_args *record_args = args;
660 return record_message (record_args->regcache, record_args->signal);
663 static int
664 record_message_wrapper_safe (struct regcache *regcache,
665 enum target_signal signal)
667 struct record_message_args args;
669 args.regcache = regcache;
670 args.signal = signal;
672 return catch_errors (record_message_wrapper, &args, NULL, RETURN_MASK_ALL);
675 /* Set to 1 if record_store_registers and record_xfer_partial
676 doesn't need record. */
678 static int record_gdb_operation_disable = 0;
680 struct cleanup *
681 record_gdb_operation_disable_set (void)
683 struct cleanup *old_cleanups = NULL;
685 old_cleanups =
686 make_cleanup_restore_integer (&record_gdb_operation_disable);
687 record_gdb_operation_disable = 1;
689 return old_cleanups;
692 /* Flag set to TRUE for target_stopped_by_watchpoint. */
693 static int record_hw_watchpoint = 0;
695 /* Execute one instruction from the record log. Each instruction in
696 the log will be represented by an arbitrary sequence of register
697 entries and memory entries, followed by an 'end' entry. */
699 static inline void
700 record_exec_insn (struct regcache *regcache, struct gdbarch *gdbarch,
701 struct record_entry *entry)
703 switch (entry->type)
705 case record_reg: /* reg */
707 gdb_byte reg[MAX_REGISTER_SIZE];
709 if (record_debug > 1)
710 fprintf_unfiltered (gdb_stdlog,
711 "Process record: record_reg %s to "
712 "inferior num = %d.\n",
713 host_address_to_string (entry),
714 entry->u.reg.num);
716 regcache_cooked_read (regcache, entry->u.reg.num, reg);
717 regcache_cooked_write (regcache, entry->u.reg.num,
718 record_get_loc (entry));
719 memcpy (record_get_loc (entry), reg, entry->u.reg.len);
721 break;
723 case record_mem: /* mem */
725 /* Nothing to do if the entry is flagged not_accessible. */
726 if (!entry->u.mem.mem_entry_not_accessible)
728 gdb_byte *mem = alloca (entry->u.mem.len);
730 if (record_debug > 1)
731 fprintf_unfiltered (gdb_stdlog,
732 "Process record: record_mem %s to "
733 "inferior addr = %s len = %d.\n",
734 host_address_to_string (entry),
735 paddress (gdbarch, entry->u.mem.addr),
736 entry->u.mem.len);
738 if (target_read_memory (entry->u.mem.addr, mem, entry->u.mem.len))
740 entry->u.mem.mem_entry_not_accessible = 1;
741 if (record_debug)
742 warning ("Process record: error reading memory at "
743 "addr = %s len = %d.",
744 paddress (gdbarch, entry->u.mem.addr),
745 entry->u.mem.len);
747 else
749 if (target_write_memory (entry->u.mem.addr,
750 record_get_loc (entry),
751 entry->u.mem.len))
753 entry->u.mem.mem_entry_not_accessible = 1;
754 if (record_debug)
755 warning ("Process record: error writing memory at "
756 "addr = %s len = %d.",
757 paddress (gdbarch, entry->u.mem.addr),
758 entry->u.mem.len);
760 else
762 memcpy (record_get_loc (entry), mem, entry->u.mem.len);
764 /* We've changed memory --- check if a hardware
765 watchpoint should trap. Note that this
766 presently assumes the target beneath supports
767 continuable watchpoints. On non-continuable
768 watchpoints target, we'll want to check this
769 _before_ actually doing the memory change, and
770 not doing the change at all if the watchpoint
771 traps. */
772 if (hardware_watchpoint_inserted_in_range
773 (get_regcache_aspace (regcache),
774 entry->u.mem.addr, entry->u.mem.len))
775 record_hw_watchpoint = 1;
780 break;
784 static struct target_ops *tmp_to_resume_ops;
785 static void (*tmp_to_resume) (struct target_ops *, ptid_t, int,
786 enum target_signal);
787 static struct target_ops *tmp_to_wait_ops;
788 static ptid_t (*tmp_to_wait) (struct target_ops *, ptid_t,
789 struct target_waitstatus *,
790 int);
791 static struct target_ops *tmp_to_store_registers_ops;
792 static void (*tmp_to_store_registers) (struct target_ops *,
793 struct regcache *,
794 int regno);
795 static struct target_ops *tmp_to_xfer_partial_ops;
796 static LONGEST (*tmp_to_xfer_partial) (struct target_ops *ops,
797 enum target_object object,
798 const char *annex,
799 gdb_byte *readbuf,
800 const gdb_byte *writebuf,
801 ULONGEST offset,
802 LONGEST len);
803 static int (*tmp_to_insert_breakpoint) (struct gdbarch *,
804 struct bp_target_info *);
805 static int (*tmp_to_remove_breakpoint) (struct gdbarch *,
806 struct bp_target_info *);
807 static int (*tmp_to_stopped_by_watchpoint) (void);
808 static int (*tmp_to_stopped_data_address) (struct target_ops *, CORE_ADDR *);
810 static void record_restore (void);
812 /* Open the process record target. */
814 static void
815 record_core_open_1 (char *name, int from_tty)
817 struct regcache *regcache = get_current_regcache ();
818 int regnum = gdbarch_num_regs (get_regcache_arch (regcache));
819 int i;
821 /* Get record_core_regbuf. */
822 target_fetch_registers (regcache, -1);
823 record_core_regbuf = xmalloc (MAX_REGISTER_SIZE * regnum);
824 for (i = 0; i < regnum; i ++)
825 regcache_raw_collect (regcache, i,
826 record_core_regbuf + MAX_REGISTER_SIZE * i);
828 /* Get record_core_start and record_core_end. */
829 if (build_section_table (core_bfd, &record_core_start, &record_core_end))
831 xfree (record_core_regbuf);
832 record_core_regbuf = NULL;
833 error (_("\"%s\": Can't find sections: %s"),
834 bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ()));
837 push_target (&record_core_ops);
838 record_restore ();
841 /* "to_open" target method for 'live' processes. */
843 static void
844 record_open_1 (char *name, int from_tty)
846 if (record_debug)
847 fprintf_unfiltered (gdb_stdlog, "Process record: record_open\n");
849 /* check exec */
850 if (!target_has_execution)
851 error (_("Process record: the program is not being run."));
852 if (non_stop)
853 error (_("Process record target can't debug inferior in non-stop mode "
854 "(non-stop)."));
855 if (target_async_permitted)
856 error (_("Process record target can't debug inferior in asynchronous "
857 "mode (target-async)."));
859 if (!gdbarch_process_record_p (target_gdbarch))
860 error (_("Process record: the current architecture doesn't support "
861 "record function."));
863 if (!tmp_to_resume)
864 error (_("Could not find 'to_resume' method on the target stack."));
865 if (!tmp_to_wait)
866 error (_("Could not find 'to_wait' method on the target stack."));
867 if (!tmp_to_store_registers)
868 error (_("Could not find 'to_store_registers' method on the target stack."));
869 if (!tmp_to_insert_breakpoint)
870 error (_("Could not find 'to_insert_breakpoint' method on the target stack."));
871 if (!tmp_to_remove_breakpoint)
872 error (_("Could not find 'to_remove_breakpoint' method on the target stack."));
873 if (!tmp_to_stopped_by_watchpoint)
874 error (_("Could not find 'to_stopped_by_watchpoint' method on the target stack."));
875 if (!tmp_to_stopped_data_address)
876 error (_("Could not find 'to_stopped_data_address' method on the target stack."));
878 push_target (&record_ops);
881 /* "to_open" target method. Open the process record target. */
883 static void
884 record_open (char *name, int from_tty)
886 struct target_ops *t;
888 if (record_debug)
889 fprintf_unfiltered (gdb_stdlog, "Process record: record_open\n");
891 /* Check if record target is already running. */
892 if (current_target.to_stratum == record_stratum)
893 error (_("Process record target already running. Use \"record stop\" to "
894 "stop record target first."));
896 /* Reset the tmp beneath pointers. */
897 tmp_to_resume_ops = NULL;
898 tmp_to_resume = NULL;
899 tmp_to_wait_ops = NULL;
900 tmp_to_wait = NULL;
901 tmp_to_store_registers_ops = NULL;
902 tmp_to_store_registers = NULL;
903 tmp_to_xfer_partial_ops = NULL;
904 tmp_to_xfer_partial = NULL;
905 tmp_to_insert_breakpoint = NULL;
906 tmp_to_remove_breakpoint = NULL;
907 tmp_to_stopped_by_watchpoint = NULL;
908 tmp_to_stopped_data_address = NULL;
910 /* Set the beneath function pointers. */
911 for (t = current_target.beneath; t != NULL; t = t->beneath)
913 if (!tmp_to_resume)
915 tmp_to_resume = t->to_resume;
916 tmp_to_resume_ops = t;
918 if (!tmp_to_wait)
920 tmp_to_wait = t->to_wait;
921 tmp_to_wait_ops = t;
923 if (!tmp_to_store_registers)
925 tmp_to_store_registers = t->to_store_registers;
926 tmp_to_store_registers_ops = t;
928 if (!tmp_to_xfer_partial)
930 tmp_to_xfer_partial = t->to_xfer_partial;
931 tmp_to_xfer_partial_ops = t;
933 if (!tmp_to_insert_breakpoint)
934 tmp_to_insert_breakpoint = t->to_insert_breakpoint;
935 if (!tmp_to_remove_breakpoint)
936 tmp_to_remove_breakpoint = t->to_remove_breakpoint;
937 if (!tmp_to_stopped_by_watchpoint)
938 tmp_to_stopped_by_watchpoint = t->to_stopped_by_watchpoint;
939 if (!tmp_to_stopped_data_address)
940 tmp_to_stopped_data_address = t->to_stopped_data_address;
942 if (!tmp_to_xfer_partial)
943 error (_("Could not find 'to_xfer_partial' method on the target stack."));
945 /* Reset */
946 record_insn_num = 0;
947 record_insn_count = 0;
948 record_list = &record_first;
949 record_list->next = NULL;
951 /* Set the tmp beneath pointers to beneath pointers. */
952 record_beneath_to_resume_ops = tmp_to_resume_ops;
953 record_beneath_to_resume = tmp_to_resume;
954 record_beneath_to_wait_ops = tmp_to_wait_ops;
955 record_beneath_to_wait = tmp_to_wait;
956 record_beneath_to_store_registers_ops = tmp_to_store_registers_ops;
957 record_beneath_to_store_registers = tmp_to_store_registers;
958 record_beneath_to_xfer_partial_ops = tmp_to_xfer_partial_ops;
959 record_beneath_to_xfer_partial = tmp_to_xfer_partial;
960 record_beneath_to_insert_breakpoint = tmp_to_insert_breakpoint;
961 record_beneath_to_remove_breakpoint = tmp_to_remove_breakpoint;
962 record_beneath_to_stopped_by_watchpoint = tmp_to_stopped_by_watchpoint;
963 record_beneath_to_stopped_data_address = tmp_to_stopped_data_address;
965 if (core_bfd)
966 record_core_open_1 (name, from_tty);
967 else
968 record_open_1 (name, from_tty);
971 /* "to_close" target method. Close the process record target. */
973 static void
974 record_close (int quitting)
976 struct record_core_buf_entry *entry;
978 if (record_debug)
979 fprintf_unfiltered (gdb_stdlog, "Process record: record_close\n");
981 record_list_release (record_list);
983 /* Release record_core_regbuf. */
984 if (record_core_regbuf)
986 xfree (record_core_regbuf);
987 record_core_regbuf = NULL;
990 /* Release record_core_buf_list. */
991 if (record_core_buf_list)
993 for (entry = record_core_buf_list->prev; entry; entry = entry->prev)
995 xfree (record_core_buf_list);
996 record_core_buf_list = entry;
998 record_core_buf_list = NULL;
1002 static int record_resume_step = 0;
1004 /* "to_resume" target method. Resume the process record target. */
1006 static void
1007 record_resume (struct target_ops *ops, ptid_t ptid, int step,
1008 enum target_signal signal)
1010 record_resume_step = step;
1012 if (!RECORD_IS_REPLAY)
1014 struct gdbarch *gdbarch = target_thread_architecture (ptid);
1016 record_message (get_current_regcache (), signal);
1018 if (!step)
1020 /* This is not hard single step. */
1021 if (!gdbarch_software_single_step_p (gdbarch))
1023 /* This is a normal continue. */
1024 step = 1;
1026 else
1028 /* This arch support soft sigle step. */
1029 if (single_step_breakpoints_inserted ())
1031 /* This is a soft single step. */
1032 record_resume_step = 1;
1034 else
1036 /* This is a continue.
1037 Try to insert a soft single step breakpoint. */
1038 if (!gdbarch_software_single_step (gdbarch,
1039 get_current_frame ()))
1041 /* This system don't want use soft single step.
1042 Use hard sigle step. */
1043 step = 1;
1049 record_beneath_to_resume (record_beneath_to_resume_ops,
1050 ptid, step, signal);
1054 static int record_get_sig = 0;
1056 /* SIGINT signal handler, registered by "to_wait" method. */
1058 static void
1059 record_sig_handler (int signo)
1061 if (record_debug)
1062 fprintf_unfiltered (gdb_stdlog, "Process record: get a signal\n");
1064 /* It will break the running inferior in replay mode. */
1065 record_resume_step = 1;
1067 /* It will let record_wait set inferior status to get the signal
1068 SIGINT. */
1069 record_get_sig = 1;
1072 static void
1073 record_wait_cleanups (void *ignore)
1075 if (execution_direction == EXEC_REVERSE)
1077 if (record_list->next)
1078 record_list = record_list->next;
1080 else
1081 record_list = record_list->prev;
1084 /* "to_wait" target method for process record target.
1086 In record mode, the target is always run in singlestep mode
1087 (even when gdb says to continue). The to_wait method intercepts
1088 the stop events and determines which ones are to be passed on to
1089 gdb. Most stop events are just singlestep events that gdb is not
1090 to know about, so the to_wait method just records them and keeps
1091 singlestepping.
1093 In replay mode, this function emulates the recorded execution log,
1094 one instruction at a time (forward or backward), and determines
1095 where to stop. */
1097 static ptid_t
1098 record_wait (struct target_ops *ops,
1099 ptid_t ptid, struct target_waitstatus *status,
1100 int options)
1102 struct cleanup *set_cleanups = record_gdb_operation_disable_set ();
1104 if (record_debug)
1105 fprintf_unfiltered (gdb_stdlog,
1106 "Process record: record_wait "
1107 "record_resume_step = %d\n",
1108 record_resume_step);
1110 record_get_sig = 0;
1111 signal (SIGINT, record_sig_handler);
1113 if (!RECORD_IS_REPLAY && ops != &record_core_ops)
1115 if (record_resume_step)
1117 /* This is a single step. */
1118 return record_beneath_to_wait (record_beneath_to_wait_ops,
1119 ptid, status, options);
1121 else
1123 /* This is not a single step. */
1124 ptid_t ret;
1125 CORE_ADDR tmp_pc;
1126 struct gdbarch *gdbarch = target_thread_architecture (inferior_ptid);
1128 while (1)
1130 ret = record_beneath_to_wait (record_beneath_to_wait_ops,
1131 ptid, status, options);
1133 if (single_step_breakpoints_inserted ())
1134 remove_single_step_breakpoints ();
1136 if (record_resume_step)
1137 return ret;
1139 /* Is this a SIGTRAP? */
1140 if (status->kind == TARGET_WAITKIND_STOPPED
1141 && status->value.sig == TARGET_SIGNAL_TRAP)
1143 struct regcache *regcache;
1144 struct address_space *aspace;
1146 /* Yes -- this is likely our single-step finishing,
1147 but check if there's any reason the core would be
1148 interested in the event. */
1150 registers_changed ();
1151 regcache = get_current_regcache ();
1152 tmp_pc = regcache_read_pc (regcache);
1153 aspace = get_regcache_aspace (regcache);
1155 if (target_stopped_by_watchpoint ())
1157 /* Always interested in watchpoints. */
1159 else if (breakpoint_inserted_here_p (aspace, tmp_pc))
1161 /* There is a breakpoint here. Let the core
1162 handle it. */
1163 if (software_breakpoint_inserted_here_p (aspace, tmp_pc))
1165 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1166 CORE_ADDR decr_pc_after_break
1167 = gdbarch_decr_pc_after_break (gdbarch);
1168 if (decr_pc_after_break)
1169 regcache_write_pc (regcache,
1170 tmp_pc + decr_pc_after_break);
1173 else
1175 /* This is a single-step trap. Record the
1176 insn and issue another step.
1177 FIXME: this part can be a random SIGTRAP too.
1178 But GDB cannot handle it. */
1179 int step = 1;
1181 if (!record_message_wrapper_safe (regcache,
1182 TARGET_SIGNAL_0))
1184 status->kind = TARGET_WAITKIND_STOPPED;
1185 status->value.sig = TARGET_SIGNAL_0;
1186 break;
1189 if (gdbarch_software_single_step_p (gdbarch))
1191 /* Try to insert the software single step breakpoint.
1192 If insert success, set step to 0. */
1193 set_executing (inferior_ptid, 0);
1194 reinit_frame_cache ();
1195 if (gdbarch_software_single_step (gdbarch,
1196 get_current_frame ()))
1197 step = 0;
1198 set_executing (inferior_ptid, 1);
1201 record_beneath_to_resume (record_beneath_to_resume_ops,
1202 ptid, step,
1203 TARGET_SIGNAL_0);
1204 continue;
1208 /* The inferior is broken by a breakpoint or a signal. */
1209 break;
1212 return ret;
1215 else
1217 struct regcache *regcache = get_current_regcache ();
1218 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1219 struct address_space *aspace = get_regcache_aspace (regcache);
1220 int continue_flag = 1;
1221 int first_record_end = 1;
1222 struct cleanup *old_cleanups = make_cleanup (record_wait_cleanups, 0);
1223 CORE_ADDR tmp_pc;
1225 record_hw_watchpoint = 0;
1226 status->kind = TARGET_WAITKIND_STOPPED;
1228 /* Check breakpoint when forward execute. */
1229 if (execution_direction == EXEC_FORWARD)
1231 tmp_pc = regcache_read_pc (regcache);
1232 if (breakpoint_inserted_here_p (aspace, tmp_pc))
1234 int decr_pc_after_break = gdbarch_decr_pc_after_break (gdbarch);
1236 if (record_debug)
1237 fprintf_unfiltered (gdb_stdlog,
1238 "Process record: break at %s.\n",
1239 paddress (gdbarch, tmp_pc));
1241 if (decr_pc_after_break
1242 && !record_resume_step
1243 && software_breakpoint_inserted_here_p (aspace, tmp_pc))
1244 regcache_write_pc (regcache,
1245 tmp_pc + decr_pc_after_break);
1246 goto replay_out;
1250 /* If GDB is in terminal_inferior mode, it will not get the signal.
1251 And in GDB replay mode, GDB doesn't need to be in terminal_inferior
1252 mode, because inferior will not executed.
1253 Then set it to terminal_ours to make GDB get the signal. */
1254 target_terminal_ours ();
1256 /* In EXEC_FORWARD mode, record_list points to the tail of prev
1257 instruction. */
1258 if (execution_direction == EXEC_FORWARD && record_list->next)
1259 record_list = record_list->next;
1261 /* Loop over the record_list, looking for the next place to
1262 stop. */
1265 /* Check for beginning and end of log. */
1266 if (execution_direction == EXEC_REVERSE
1267 && record_list == &record_first)
1269 /* Hit beginning of record log in reverse. */
1270 status->kind = TARGET_WAITKIND_NO_HISTORY;
1271 break;
1273 if (execution_direction != EXEC_REVERSE && !record_list->next)
1275 /* Hit end of record log going forward. */
1276 status->kind = TARGET_WAITKIND_NO_HISTORY;
1277 break;
1280 record_exec_insn (regcache, gdbarch, record_list);
1282 if (record_list->type == record_end)
1284 if (record_debug > 1)
1285 fprintf_unfiltered (gdb_stdlog,
1286 "Process record: record_end %s to "
1287 "inferior.\n",
1288 host_address_to_string (record_list));
1290 if (first_record_end && execution_direction == EXEC_REVERSE)
1292 /* When reverse excute, the first record_end is the part of
1293 current instruction. */
1294 first_record_end = 0;
1296 else
1298 /* In EXEC_REVERSE mode, this is the record_end of prev
1299 instruction.
1300 In EXEC_FORWARD mode, this is the record_end of current
1301 instruction. */
1302 /* step */
1303 if (record_resume_step)
1305 if (record_debug > 1)
1306 fprintf_unfiltered (gdb_stdlog,
1307 "Process record: step.\n");
1308 continue_flag = 0;
1311 /* check breakpoint */
1312 tmp_pc = regcache_read_pc (regcache);
1313 if (breakpoint_inserted_here_p (aspace, tmp_pc))
1315 int decr_pc_after_break
1316 = gdbarch_decr_pc_after_break (gdbarch);
1318 if (record_debug)
1319 fprintf_unfiltered (gdb_stdlog,
1320 "Process record: break "
1321 "at %s.\n",
1322 paddress (gdbarch, tmp_pc));
1323 if (decr_pc_after_break
1324 && execution_direction == EXEC_FORWARD
1325 && !record_resume_step
1326 && software_breakpoint_inserted_here_p (aspace,
1327 tmp_pc))
1328 regcache_write_pc (regcache,
1329 tmp_pc + decr_pc_after_break);
1330 continue_flag = 0;
1333 if (record_hw_watchpoint)
1335 if (record_debug)
1336 fprintf_unfiltered (gdb_stdlog, "\
1337 Process record: hit hw watchpoint.\n");
1338 continue_flag = 0;
1340 /* Check target signal */
1341 if (record_list->u.end.sigval != TARGET_SIGNAL_0)
1342 /* FIXME: better way to check */
1343 continue_flag = 0;
1347 if (continue_flag)
1349 if (execution_direction == EXEC_REVERSE)
1351 if (record_list->prev)
1352 record_list = record_list->prev;
1354 else
1356 if (record_list->next)
1357 record_list = record_list->next;
1361 while (continue_flag);
1363 replay_out:
1364 if (record_get_sig)
1365 status->value.sig = TARGET_SIGNAL_INT;
1366 else if (record_list->u.end.sigval != TARGET_SIGNAL_0)
1367 /* FIXME: better way to check */
1368 status->value.sig = record_list->u.end.sigval;
1369 else
1370 status->value.sig = TARGET_SIGNAL_TRAP;
1372 discard_cleanups (old_cleanups);
1375 signal (SIGINT, handle_sigint);
1377 do_cleanups (set_cleanups);
1378 return inferior_ptid;
1381 static int
1382 record_stopped_by_watchpoint (void)
1384 if (RECORD_IS_REPLAY)
1385 return record_hw_watchpoint;
1386 else
1387 return record_beneath_to_stopped_by_watchpoint ();
1390 static int
1391 record_stopped_data_address (struct target_ops *ops, CORE_ADDR *addr_p)
1393 if (RECORD_IS_REPLAY)
1394 return 0;
1395 else
1396 return record_beneath_to_stopped_data_address (ops, addr_p);
1399 /* "to_disconnect" method for process record target. */
1401 static void
1402 record_disconnect (struct target_ops *target, char *args, int from_tty)
1404 if (record_debug)
1405 fprintf_unfiltered (gdb_stdlog, "Process record: record_disconnect\n");
1407 unpush_target (&record_ops);
1408 target_disconnect (args, from_tty);
1411 /* "to_detach" method for process record target. */
1413 static void
1414 record_detach (struct target_ops *ops, char *args, int from_tty)
1416 if (record_debug)
1417 fprintf_unfiltered (gdb_stdlog, "Process record: record_detach\n");
1419 unpush_target (&record_ops);
1420 target_detach (args, from_tty);
1423 /* "to_mourn_inferior" method for process record target. */
1425 static void
1426 record_mourn_inferior (struct target_ops *ops)
1428 if (record_debug)
1429 fprintf_unfiltered (gdb_stdlog, "Process record: "
1430 "record_mourn_inferior\n");
1432 unpush_target (&record_ops);
1433 target_mourn_inferior ();
1436 /* Close process record target before killing the inferior process. */
1438 static void
1439 record_kill (struct target_ops *ops)
1441 if (record_debug)
1442 fprintf_unfiltered (gdb_stdlog, "Process record: record_kill\n");
1444 unpush_target (&record_ops);
1445 target_kill ();
1448 /* Record registers change (by user or by GDB) to list as an instruction. */
1450 static void
1451 record_registers_change (struct regcache *regcache, int regnum)
1453 /* Check record_insn_num. */
1454 record_check_insn_num (0);
1456 record_arch_list_head = NULL;
1457 record_arch_list_tail = NULL;
1459 if (regnum < 0)
1461 int i;
1463 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
1465 if (record_arch_list_add_reg (regcache, i))
1467 record_list_release (record_arch_list_tail);
1468 error (_("Process record: failed to record execution log."));
1472 else
1474 if (record_arch_list_add_reg (regcache, regnum))
1476 record_list_release (record_arch_list_tail);
1477 error (_("Process record: failed to record execution log."));
1480 if (record_arch_list_add_end ())
1482 record_list_release (record_arch_list_tail);
1483 error (_("Process record: failed to record execution log."));
1485 record_list->next = record_arch_list_head;
1486 record_arch_list_head->prev = record_list;
1487 record_list = record_arch_list_tail;
1489 if (record_insn_num == record_insn_max_num && record_insn_max_num)
1490 record_list_release_first ();
1491 else
1492 record_insn_num++;
1495 /* "to_store_registers" method for process record target. */
1497 static void
1498 record_store_registers (struct target_ops *ops, struct regcache *regcache,
1499 int regno)
1501 if (!record_gdb_operation_disable)
1503 if (RECORD_IS_REPLAY)
1505 int n;
1507 /* Let user choose if he wants to write register or not. */
1508 if (regno < 0)
1510 query (_("Because GDB is in replay mode, changing the "
1511 "value of a register will make the execution "
1512 "log unusable from this point onward. "
1513 "Change all registers?"));
1514 else
1516 query (_("Because GDB is in replay mode, changing the value "
1517 "of a register will make the execution log unusable "
1518 "from this point onward. Change register %s?"),
1519 gdbarch_register_name (get_regcache_arch (regcache),
1520 regno));
1522 if (!n)
1524 /* Invalidate the value of regcache that was set in function
1525 "regcache_raw_write". */
1526 if (regno < 0)
1528 int i;
1530 for (i = 0;
1531 i < gdbarch_num_regs (get_regcache_arch (regcache));
1532 i++)
1533 regcache_invalidate (regcache, i);
1535 else
1536 regcache_invalidate (regcache, regno);
1538 error (_("Process record canceled the operation."));
1541 /* Destroy the record from here forward. */
1542 record_list_release_following (record_list);
1545 record_registers_change (regcache, regno);
1547 record_beneath_to_store_registers (record_beneath_to_store_registers_ops,
1548 regcache, regno);
1551 /* "to_xfer_partial" method. Behavior is conditional on RECORD_IS_REPLAY.
1552 In replay mode, we cannot write memory unles we are willing to
1553 invalidate the record/replay log from this point forward. */
1555 static LONGEST
1556 record_xfer_partial (struct target_ops *ops, enum target_object object,
1557 const char *annex, gdb_byte *readbuf,
1558 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
1560 if (!record_gdb_operation_disable
1561 && (object == TARGET_OBJECT_MEMORY
1562 || object == TARGET_OBJECT_RAW_MEMORY) && writebuf)
1564 if (RECORD_IS_REPLAY)
1566 /* Let user choose if he wants to write memory or not. */
1567 if (!query (_("Because GDB is in replay mode, writing to memory "
1568 "will make the execution log unusable from this "
1569 "point onward. Write memory at address %s?"),
1570 paddress (target_gdbarch, offset)))
1571 error (_("Process record canceled the operation."));
1573 /* Destroy the record from here forward. */
1574 record_list_release_following (record_list);
1577 /* Check record_insn_num */
1578 record_check_insn_num (0);
1580 /* Record registers change to list as an instruction. */
1581 record_arch_list_head = NULL;
1582 record_arch_list_tail = NULL;
1583 if (record_arch_list_add_mem (offset, len))
1585 record_list_release (record_arch_list_tail);
1586 if (record_debug)
1587 fprintf_unfiltered (gdb_stdlog,
1588 "Process record: failed to record "
1589 "execution log.");
1590 return -1;
1592 if (record_arch_list_add_end ())
1594 record_list_release (record_arch_list_tail);
1595 if (record_debug)
1596 fprintf_unfiltered (gdb_stdlog,
1597 "Process record: failed to record "
1598 "execution log.");
1599 return -1;
1601 record_list->next = record_arch_list_head;
1602 record_arch_list_head->prev = record_list;
1603 record_list = record_arch_list_tail;
1605 if (record_insn_num == record_insn_max_num && record_insn_max_num)
1606 record_list_release_first ();
1607 else
1608 record_insn_num++;
1611 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops,
1612 object, annex, readbuf, writebuf,
1613 offset, len);
1616 /* Behavior is conditional on RECORD_IS_REPLAY.
1617 We will not actually insert or remove breakpoints when replaying,
1618 nor when recording. */
1620 static int
1621 record_insert_breakpoint (struct gdbarch *gdbarch,
1622 struct bp_target_info *bp_tgt)
1624 if (!RECORD_IS_REPLAY)
1626 struct cleanup *old_cleanups = record_gdb_operation_disable_set ();
1627 int ret = record_beneath_to_insert_breakpoint (gdbarch, bp_tgt);
1629 do_cleanups (old_cleanups);
1631 return ret;
1634 return 0;
1637 /* "to_remove_breakpoint" method for process record target. */
1639 static int
1640 record_remove_breakpoint (struct gdbarch *gdbarch,
1641 struct bp_target_info *bp_tgt)
1643 if (!RECORD_IS_REPLAY)
1645 struct cleanup *old_cleanups = record_gdb_operation_disable_set ();
1646 int ret = record_beneath_to_remove_breakpoint (gdbarch, bp_tgt);
1648 do_cleanups (old_cleanups);
1650 return ret;
1653 return 0;
1656 /* "to_can_execute_reverse" method for process record target. */
1658 static int
1659 record_can_execute_reverse (void)
1661 return 1;
1664 /* "to_get_bookmark" method for process record and prec over core. */
1666 static gdb_byte *
1667 record_get_bookmark (char *args, int from_tty)
1669 gdb_byte *ret = NULL;
1671 /* Return stringified form of instruction count. */
1672 if (record_list && record_list->type == record_end)
1673 ret = xstrdup (pulongest (record_list->u.end.insn_num));
1675 if (record_debug)
1677 if (ret)
1678 fprintf_unfiltered (gdb_stdlog,
1679 "record_get_bookmark returns %s\n", ret);
1680 else
1681 fprintf_unfiltered (gdb_stdlog,
1682 "record_get_bookmark returns NULL\n");
1684 return ret;
1687 /* The implementation of the command "record goto". */
1688 static void cmd_record_goto (char *, int);
1690 /* "to_goto_bookmark" method for process record and prec over core. */
1692 static void
1693 record_goto_bookmark (gdb_byte *bookmark, int from_tty)
1695 if (record_debug)
1696 fprintf_unfiltered (gdb_stdlog,
1697 "record_goto_bookmark receives %s\n", bookmark);
1699 if (bookmark[0] == '\'' || bookmark[0] == '\"')
1701 if (bookmark[strlen (bookmark) - 1] != bookmark[0])
1702 error (_("Unbalanced quotes: %s"), bookmark);
1704 /* Strip trailing quote. */
1705 bookmark[strlen (bookmark) - 1] = '\0';
1706 /* Strip leading quote. */
1707 bookmark++;
1708 /* Pass along to cmd_record_goto. */
1711 cmd_record_goto ((char *) bookmark, from_tty);
1712 return;
1715 static void
1716 init_record_ops (void)
1718 record_ops.to_shortname = "record";
1719 record_ops.to_longname = "Process record and replay target";
1720 record_ops.to_doc =
1721 "Log program while executing and replay execution from log.";
1722 record_ops.to_open = record_open;
1723 record_ops.to_close = record_close;
1724 record_ops.to_resume = record_resume;
1725 record_ops.to_wait = record_wait;
1726 record_ops.to_disconnect = record_disconnect;
1727 record_ops.to_detach = record_detach;
1728 record_ops.to_mourn_inferior = record_mourn_inferior;
1729 record_ops.to_kill = record_kill;
1730 record_ops.to_create_inferior = find_default_create_inferior;
1731 record_ops.to_store_registers = record_store_registers;
1732 record_ops.to_xfer_partial = record_xfer_partial;
1733 record_ops.to_insert_breakpoint = record_insert_breakpoint;
1734 record_ops.to_remove_breakpoint = record_remove_breakpoint;
1735 record_ops.to_stopped_by_watchpoint = record_stopped_by_watchpoint;
1736 record_ops.to_stopped_data_address = record_stopped_data_address;
1737 record_ops.to_can_execute_reverse = record_can_execute_reverse;
1738 record_ops.to_stratum = record_stratum;
1739 /* Add bookmark target methods. */
1740 record_ops.to_get_bookmark = record_get_bookmark;
1741 record_ops.to_goto_bookmark = record_goto_bookmark;
1742 record_ops.to_magic = OPS_MAGIC;
1745 /* "to_resume" method for prec over corefile. */
1747 static void
1748 record_core_resume (struct target_ops *ops, ptid_t ptid, int step,
1749 enum target_signal signal)
1751 record_resume_step = step;
1754 /* "to_kill" method for prec over corefile. */
1756 static void
1757 record_core_kill (struct target_ops *ops)
1759 if (record_debug)
1760 fprintf_unfiltered (gdb_stdlog, "Process record: record_core_kill\n");
1762 unpush_target (&record_core_ops);
1765 /* "to_fetch_registers" method for prec over corefile. */
1767 static void
1768 record_core_fetch_registers (struct target_ops *ops,
1769 struct regcache *regcache,
1770 int regno)
1772 if (regno < 0)
1774 int num = gdbarch_num_regs (get_regcache_arch (regcache));
1775 int i;
1777 for (i = 0; i < num; i ++)
1778 regcache_raw_supply (regcache, i,
1779 record_core_regbuf + MAX_REGISTER_SIZE * i);
1781 else
1782 regcache_raw_supply (regcache, regno,
1783 record_core_regbuf + MAX_REGISTER_SIZE * regno);
1786 /* "to_prepare_to_store" method for prec over corefile. */
1788 static void
1789 record_core_prepare_to_store (struct regcache *regcache)
1793 /* "to_store_registers" method for prec over corefile. */
1795 static void
1796 record_core_store_registers (struct target_ops *ops,
1797 struct regcache *regcache,
1798 int regno)
1800 if (record_gdb_operation_disable)
1801 regcache_raw_collect (regcache, regno,
1802 record_core_regbuf + MAX_REGISTER_SIZE * regno);
1803 else
1804 error (_("You can't do that without a process to debug."));
1807 /* "to_xfer_partial" method for prec over corefile. */
1809 static LONGEST
1810 record_core_xfer_partial (struct target_ops *ops, enum target_object object,
1811 const char *annex, gdb_byte *readbuf,
1812 const gdb_byte *writebuf, ULONGEST offset,
1813 LONGEST len)
1815 if (object == TARGET_OBJECT_MEMORY)
1817 if (record_gdb_operation_disable || !writebuf)
1819 struct target_section *p;
1821 for (p = record_core_start; p < record_core_end; p++)
1823 if (offset >= p->addr)
1825 struct record_core_buf_entry *entry;
1826 ULONGEST sec_offset;
1828 if (offset >= p->endaddr)
1829 continue;
1831 if (offset + len > p->endaddr)
1832 len = p->endaddr - offset;
1834 sec_offset = offset - p->addr;
1836 /* Read readbuf or write writebuf p, offset, len. */
1837 /* Check flags. */
1838 if (p->the_bfd_section->flags & SEC_CONSTRUCTOR
1839 || (p->the_bfd_section->flags & SEC_HAS_CONTENTS) == 0)
1841 if (readbuf)
1842 memset (readbuf, 0, len);
1843 return len;
1845 /* Get record_core_buf_entry. */
1846 for (entry = record_core_buf_list; entry;
1847 entry = entry->prev)
1848 if (entry->p == p)
1849 break;
1850 if (writebuf)
1852 if (!entry)
1854 /* Add a new entry. */
1855 entry = (struct record_core_buf_entry *)
1856 xmalloc (sizeof (struct record_core_buf_entry));
1857 entry->p = p;
1858 if (!bfd_malloc_and_get_section (p->bfd,
1859 p->the_bfd_section,
1860 &entry->buf))
1862 xfree (entry);
1863 return 0;
1865 entry->prev = record_core_buf_list;
1866 record_core_buf_list = entry;
1869 memcpy (entry->buf + sec_offset, writebuf,
1870 (size_t) len);
1872 else
1874 if (!entry)
1875 return record_beneath_to_xfer_partial
1876 (record_beneath_to_xfer_partial_ops,
1877 object, annex, readbuf, writebuf,
1878 offset, len);
1880 memcpy (readbuf, entry->buf + sec_offset,
1881 (size_t) len);
1884 return len;
1888 return -1;
1890 else
1891 error (_("You can't do that without a process to debug."));
1894 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops,
1895 object, annex, readbuf, writebuf,
1896 offset, len);
1899 /* "to_insert_breakpoint" method for prec over corefile. */
1901 static int
1902 record_core_insert_breakpoint (struct gdbarch *gdbarch,
1903 struct bp_target_info *bp_tgt)
1905 return 0;
1908 /* "to_remove_breakpoint" method for prec over corefile. */
1910 static int
1911 record_core_remove_breakpoint (struct gdbarch *gdbarch,
1912 struct bp_target_info *bp_tgt)
1914 return 0;
1917 /* "to_has_execution" method for prec over corefile. */
1920 record_core_has_execution (struct target_ops *ops)
1922 return 1;
1925 static void
1926 init_record_core_ops (void)
1928 record_core_ops.to_shortname = "record-core";
1929 record_core_ops.to_longname = "Process record and replay target";
1930 record_core_ops.to_doc =
1931 "Log program while executing and replay execution from log.";
1932 record_core_ops.to_open = record_open;
1933 record_core_ops.to_close = record_close;
1934 record_core_ops.to_resume = record_core_resume;
1935 record_core_ops.to_wait = record_wait;
1936 record_core_ops.to_kill = record_core_kill;
1937 record_core_ops.to_fetch_registers = record_core_fetch_registers;
1938 record_core_ops.to_prepare_to_store = record_core_prepare_to_store;
1939 record_core_ops.to_store_registers = record_core_store_registers;
1940 record_core_ops.to_xfer_partial = record_core_xfer_partial;
1941 record_core_ops.to_insert_breakpoint = record_core_insert_breakpoint;
1942 record_core_ops.to_remove_breakpoint = record_core_remove_breakpoint;
1943 record_core_ops.to_stopped_by_watchpoint = record_stopped_by_watchpoint;
1944 record_core_ops.to_stopped_data_address = record_stopped_data_address;
1945 record_core_ops.to_can_execute_reverse = record_can_execute_reverse;
1946 record_core_ops.to_has_execution = record_core_has_execution;
1947 record_core_ops.to_stratum = record_stratum;
1948 /* Add bookmark target methods. */
1949 record_core_ops.to_get_bookmark = record_get_bookmark;
1950 record_core_ops.to_goto_bookmark = record_goto_bookmark;
1951 record_core_ops.to_magic = OPS_MAGIC;
1954 /* Implement "show record debug" command. */
1956 static void
1957 show_record_debug (struct ui_file *file, int from_tty,
1958 struct cmd_list_element *c, const char *value)
1960 fprintf_filtered (file, _("Debugging of process record target is %s.\n"),
1961 value);
1964 /* Alias for "target record". */
1966 static void
1967 cmd_record_start (char *args, int from_tty)
1969 execute_command ("target record", from_tty);
1972 /* Truncate the record log from the present point
1973 of replay until the end. */
1975 static void
1976 cmd_record_delete (char *args, int from_tty)
1978 if (current_target.to_stratum == record_stratum)
1980 if (RECORD_IS_REPLAY)
1982 if (!from_tty || query (_("Delete the log from this point forward "
1983 "and begin to record the running message "
1984 "at current PC?")))
1985 record_list_release_following (record_list);
1987 else
1988 printf_unfiltered (_("Already at end of record list.\n"));
1991 else
1992 printf_unfiltered (_("Process record is not started.\n"));
1995 /* Implement the "stoprecord" or "record stop" command. */
1997 static void
1998 cmd_record_stop (char *args, int from_tty)
2000 if (current_target.to_stratum == record_stratum)
2002 unpush_target (&record_ops);
2003 printf_unfiltered (_("Process record is stopped and all execution "
2004 "logs are deleted.\n"));
2006 else
2007 printf_unfiltered (_("Process record is not started.\n"));
2010 /* Set upper limit of record log size. */
2012 static void
2013 set_record_insn_max_num (char *args, int from_tty, struct cmd_list_element *c)
2015 if (record_insn_num > record_insn_max_num && record_insn_max_num)
2017 /* Count down record_insn_num while releasing records from list. */
2018 while (record_insn_num > record_insn_max_num)
2020 record_list_release_first ();
2021 record_insn_num--;
2026 static struct cmd_list_element *record_cmdlist, *set_record_cmdlist,
2027 *show_record_cmdlist, *info_record_cmdlist;
2029 static void
2030 set_record_command (char *args, int from_tty)
2032 printf_unfiltered (_("\
2033 \"set record\" must be followed by an apporpriate subcommand.\n"));
2034 help_list (set_record_cmdlist, "set record ", all_commands, gdb_stdout);
2037 static void
2038 show_record_command (char *args, int from_tty)
2040 cmd_show_list (show_record_cmdlist, from_tty, "");
2043 /* Display some statistics about the execution log. */
2045 static void
2046 info_record_command (char *args, int from_tty)
2048 struct record_entry *p;
2050 if (current_target.to_stratum == record_stratum)
2052 if (RECORD_IS_REPLAY)
2053 printf_filtered (_("Replay mode:\n"));
2054 else
2055 printf_filtered (_("Record mode:\n"));
2057 /* Find entry for first actual instruction in the log. */
2058 for (p = record_first.next;
2059 p != NULL && p->type != record_end;
2060 p = p->next)
2063 /* Do we have a log at all? */
2064 if (p != NULL && p->type == record_end)
2066 /* Display instruction number for first instruction in the log. */
2067 printf_filtered (_("Lowest recorded instruction number is %s.\n"),
2068 pulongest (p->u.end.insn_num));
2070 /* If in replay mode, display where we are in the log. */
2071 if (RECORD_IS_REPLAY)
2072 printf_filtered (_("Current instruction number is %s.\n"),
2073 pulongest (record_list->u.end.insn_num));
2075 /* Display instruction number for last instruction in the log. */
2076 printf_filtered (_("Highest recorded instruction number is %s.\n"),
2077 pulongest (record_insn_count));
2079 /* Display log count. */
2080 printf_filtered (_("Log contains %d instructions.\n"),
2081 record_insn_num);
2083 else
2085 printf_filtered (_("No instructions have been logged.\n"));
2088 else
2090 printf_filtered (_("target record is not active.\n"));
2093 /* Display max log size. */
2094 printf_filtered (_("Max logged instructions is %d.\n"),
2095 record_insn_max_num);
2098 /* Record log save-file format
2099 Version 1 (never released)
2101 Header:
2102 4 bytes: magic number htonl(0x20090829).
2103 NOTE: be sure to change whenever this file format changes!
2105 Records:
2106 record_end:
2107 1 byte: record type (record_end, see enum record_type).
2108 record_reg:
2109 1 byte: record type (record_reg, see enum record_type).
2110 8 bytes: register id (network byte order).
2111 MAX_REGISTER_SIZE bytes: register value.
2112 record_mem:
2113 1 byte: record type (record_mem, see enum record_type).
2114 8 bytes: memory length (network byte order).
2115 8 bytes: memory address (network byte order).
2116 n bytes: memory value (n == memory length).
2118 Version 2
2119 4 bytes: magic number netorder32(0x20091016).
2120 NOTE: be sure to change whenever this file format changes!
2122 Records:
2123 record_end:
2124 1 byte: record type (record_end, see enum record_type).
2125 4 bytes: signal
2126 4 bytes: instruction count
2127 record_reg:
2128 1 byte: record type (record_reg, see enum record_type).
2129 4 bytes: register id (network byte order).
2130 n bytes: register value (n == actual register size).
2131 (eg. 4 bytes for x86 general registers).
2132 record_mem:
2133 1 byte: record type (record_mem, see enum record_type).
2134 4 bytes: memory length (network byte order).
2135 8 bytes: memory address (network byte order).
2136 n bytes: memory value (n == memory length).
2140 /* bfdcore_read -- read bytes from a core file section. */
2142 static inline void
2143 bfdcore_read (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2145 int ret = bfd_get_section_contents (obfd, osec, buf, *offset, len);
2147 if (ret)
2148 *offset += len;
2149 else
2150 error (_("Failed to read %d bytes from core file %s ('%s').\n"),
2151 len, bfd_get_filename (obfd),
2152 bfd_errmsg (bfd_get_error ()));
2155 static inline uint64_t
2156 netorder64 (uint64_t input)
2158 uint64_t ret;
2160 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2161 BFD_ENDIAN_BIG, input);
2162 return ret;
2165 static inline uint32_t
2166 netorder32 (uint32_t input)
2168 uint32_t ret;
2170 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2171 BFD_ENDIAN_BIG, input);
2172 return ret;
2175 static inline uint16_t
2176 netorder16 (uint16_t input)
2178 uint16_t ret;
2180 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2181 BFD_ENDIAN_BIG, input);
2182 return ret;
2185 /* Restore the execution log from a core_bfd file. */
2186 static void
2187 record_restore (void)
2189 uint32_t magic;
2190 struct cleanup *old_cleanups;
2191 struct record_entry *rec;
2192 asection *osec;
2193 uint32_t osec_size;
2194 int bfd_offset = 0;
2195 struct regcache *regcache;
2197 /* We restore the execution log from the open core bfd,
2198 if there is one. */
2199 if (core_bfd == NULL)
2200 return;
2202 /* "record_restore" can only be called when record list is empty. */
2203 gdb_assert (record_first.next == NULL);
2205 if (record_debug)
2206 fprintf_unfiltered (gdb_stdlog, "Restoring recording from core file.\n");
2208 /* Now need to find our special note section. */
2209 osec = bfd_get_section_by_name (core_bfd, "null0");
2210 osec_size = bfd_section_size (core_bfd, osec);
2211 if (record_debug)
2212 fprintf_unfiltered (gdb_stdlog, "Find precord section %s.\n",
2213 osec ? "succeeded" : "failed");
2214 if (osec == NULL)
2215 return;
2216 if (record_debug)
2217 fprintf_unfiltered (gdb_stdlog, "%s", bfd_section_name (core_bfd, osec));
2219 /* Check the magic code. */
2220 bfdcore_read (core_bfd, osec, &magic, sizeof (magic), &bfd_offset);
2221 if (magic != RECORD_FILE_MAGIC)
2222 error (_("Version mis-match or file format error in core file %s."),
2223 bfd_get_filename (core_bfd));
2224 if (record_debug)
2225 fprintf_unfiltered (gdb_stdlog, "\
2226 Reading 4-byte magic cookie RECORD_FILE_MAGIC (0x%s)\n",
2227 phex_nz (netorder32 (magic), 4));
2229 /* Restore the entries in recfd into record_arch_list_head and
2230 record_arch_list_tail. */
2231 record_arch_list_head = NULL;
2232 record_arch_list_tail = NULL;
2233 record_insn_num = 0;
2234 old_cleanups = make_cleanup (record_arch_list_cleanups, 0);
2235 regcache = get_current_regcache ();
2237 while (1)
2239 uint8_t rectype;
2240 uint32_t regnum, len, signal, count;
2241 uint64_t addr;
2243 /* We are finished when offset reaches osec_size. */
2244 if (bfd_offset >= osec_size)
2245 break;
2246 bfdcore_read (core_bfd, osec, &rectype, sizeof (rectype), &bfd_offset);
2248 switch (rectype)
2250 case record_reg: /* reg */
2251 /* Get register number to regnum. */
2252 bfdcore_read (core_bfd, osec, &regnum,
2253 sizeof (regnum), &bfd_offset);
2254 regnum = netorder32 (regnum);
2256 rec = record_reg_alloc (regcache, regnum);
2258 /* Get val. */
2259 bfdcore_read (core_bfd, osec, record_get_loc (rec),
2260 rec->u.reg.len, &bfd_offset);
2262 if (record_debug)
2263 fprintf_unfiltered (gdb_stdlog, "\
2264 Reading register %d (1 plus %lu plus %d bytes)\n",
2265 rec->u.reg.num,
2266 (unsigned long) sizeof (regnum),
2267 rec->u.reg.len);
2268 break;
2270 case record_mem: /* mem */
2271 /* Get len. */
2272 bfdcore_read (core_bfd, osec, &len,
2273 sizeof (len), &bfd_offset);
2274 len = netorder32 (len);
2276 /* Get addr. */
2277 bfdcore_read (core_bfd, osec, &addr,
2278 sizeof (addr), &bfd_offset);
2279 addr = netorder64 (addr);
2281 rec = record_mem_alloc (addr, len);
2283 /* Get val. */
2284 bfdcore_read (core_bfd, osec, record_get_loc (rec),
2285 rec->u.mem.len, &bfd_offset);
2287 if (record_debug)
2288 fprintf_unfiltered (gdb_stdlog, "\
2289 Reading memory %s (1 plus %lu plus %lu plus %d bytes)\n",
2290 paddress (get_current_arch (),
2291 rec->u.mem.addr),
2292 (unsigned long) sizeof (addr),
2293 (unsigned long) sizeof (len),
2294 rec->u.mem.len);
2295 break;
2297 case record_end: /* end */
2298 rec = record_end_alloc ();
2299 record_insn_num ++;
2301 /* Get signal value. */
2302 bfdcore_read (core_bfd, osec, &signal,
2303 sizeof (signal), &bfd_offset);
2304 signal = netorder32 (signal);
2305 rec->u.end.sigval = signal;
2307 /* Get insn count. */
2308 bfdcore_read (core_bfd, osec, &count,
2309 sizeof (count), &bfd_offset);
2310 count = netorder32 (count);
2311 rec->u.end.insn_num = count;
2312 record_insn_count = count + 1;
2313 if (record_debug)
2314 fprintf_unfiltered (gdb_stdlog, "\
2315 Reading record_end (1 + %lu + %lu bytes), offset == %s\n",
2316 (unsigned long) sizeof (signal),
2317 (unsigned long) sizeof (count),
2318 paddress (get_current_arch (),
2319 bfd_offset));
2320 break;
2322 default:
2323 error (_("Bad entry type in core file %s."),
2324 bfd_get_filename (core_bfd));
2325 break;
2328 /* Add rec to record arch list. */
2329 record_arch_list_add (rec);
2332 discard_cleanups (old_cleanups);
2334 /* Add record_arch_list_head to the end of record list. */
2335 record_first.next = record_arch_list_head;
2336 record_arch_list_head->prev = &record_first;
2337 record_arch_list_tail->next = NULL;
2338 record_list = &record_first;
2340 /* Update record_insn_max_num. */
2341 if (record_insn_num > record_insn_max_num)
2343 record_insn_max_num = record_insn_num;
2344 warning (_("Auto increase record/replay buffer limit to %d."),
2345 record_insn_max_num);
2348 /* Succeeded. */
2349 printf_filtered (_("Restored records from core file %s.\n"),
2350 bfd_get_filename (core_bfd));
2352 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
2355 /* bfdcore_write -- write bytes into a core file section. */
2357 static inline void
2358 bfdcore_write (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2360 int ret = bfd_set_section_contents (obfd, osec, buf, *offset, len);
2362 if (ret)
2363 *offset += len;
2364 else
2365 error (_("Failed to write %d bytes to core file %s ('%s').\n"),
2366 len, bfd_get_filename (obfd),
2367 bfd_errmsg (bfd_get_error ()));
2370 /* Restore the execution log from a file. We use a modified elf
2371 corefile format, with an extra section for our data. */
2373 static void
2374 cmd_record_restore (char *args, int from_tty)
2376 core_file_command (args, from_tty);
2377 record_open (args, from_tty);
2380 static void
2381 record_save_cleanups (void *data)
2383 bfd *obfd = data;
2384 char *pathname = xstrdup (bfd_get_filename (obfd));
2386 bfd_close (obfd);
2387 unlink (pathname);
2388 xfree (pathname);
2391 /* Save the execution log to a file. We use a modified elf corefile
2392 format, with an extra section for our data. */
2394 static void
2395 cmd_record_save (char *args, int from_tty)
2397 char *recfilename, recfilename_buffer[40];
2398 struct record_entry *cur_record_list;
2399 uint32_t magic;
2400 struct regcache *regcache;
2401 struct gdbarch *gdbarch;
2402 struct cleanup *old_cleanups;
2403 struct cleanup *set_cleanups;
2404 bfd *obfd;
2405 int save_size = 0;
2406 asection *osec = NULL;
2407 int bfd_offset = 0;
2409 if (strcmp (current_target.to_shortname, "record") != 0)
2410 error (_("This command can only be used with target 'record'.\n"
2411 "Use 'target record' first.\n"));
2413 if (args && *args)
2414 recfilename = args;
2415 else
2417 /* Default recfile name is "gdb_record.PID". */
2418 snprintf (recfilename_buffer, sizeof (recfilename_buffer),
2419 "gdb_record.%d", PIDGET (inferior_ptid));
2420 recfilename = recfilename_buffer;
2423 /* Open the save file. */
2424 if (record_debug)
2425 fprintf_unfiltered (gdb_stdlog, "Saving execution log to core file '%s'\n",
2426 recfilename);
2428 /* Open the output file. */
2429 obfd = create_gcore_bfd (recfilename);
2430 old_cleanups = make_cleanup (record_save_cleanups, obfd);
2432 /* Save the current record entry to "cur_record_list". */
2433 cur_record_list = record_list;
2435 /* Get the values of regcache and gdbarch. */
2436 regcache = get_current_regcache ();
2437 gdbarch = get_regcache_arch (regcache);
2439 /* Disable the GDB operation record. */
2440 set_cleanups = record_gdb_operation_disable_set ();
2442 /* Reverse execute to the begin of record list. */
2443 while (1)
2445 /* Check for beginning and end of log. */
2446 if (record_list == &record_first)
2447 break;
2449 record_exec_insn (regcache, gdbarch, record_list);
2451 if (record_list->prev)
2452 record_list = record_list->prev;
2455 /* Compute the size needed for the extra bfd section. */
2456 save_size = 4; /* magic cookie */
2457 for (record_list = record_first.next; record_list;
2458 record_list = record_list->next)
2459 switch (record_list->type)
2461 case record_end:
2462 save_size += 1 + 4 + 4;
2463 break;
2464 case record_reg:
2465 save_size += 1 + 4 + record_list->u.reg.len;
2466 break;
2467 case record_mem:
2468 save_size += 1 + 4 + 8 + record_list->u.mem.len;
2469 break;
2472 /* Make the new bfd section. */
2473 osec = bfd_make_section_anyway_with_flags (obfd, "precord",
2474 SEC_HAS_CONTENTS
2475 | SEC_READONLY);
2476 if (osec == NULL)
2477 error (_("Failed to create 'precord' section for corefile %s: %s"),
2478 recfilename,
2479 bfd_errmsg (bfd_get_error ()));
2480 bfd_set_section_size (obfd, osec, save_size);
2481 bfd_set_section_vma (obfd, osec, 0);
2482 bfd_set_section_alignment (obfd, osec, 0);
2483 bfd_section_lma (obfd, osec) = 0;
2485 /* Save corefile state. */
2486 write_gcore_file (obfd);
2488 /* Write out the record log. */
2489 /* Write the magic code. */
2490 magic = RECORD_FILE_MAGIC;
2491 if (record_debug)
2492 fprintf_unfiltered (gdb_stdlog, "\
2493 Writing 4-byte magic cookie RECORD_FILE_MAGIC (0x%s)\n",
2494 phex_nz (magic, 4));
2495 bfdcore_write (obfd, osec, &magic, sizeof (magic), &bfd_offset);
2497 /* Save the entries to recfd and forward execute to the end of
2498 record list. */
2499 record_list = &record_first;
2500 while (1)
2502 /* Save entry. */
2503 if (record_list != &record_first)
2505 uint8_t type;
2506 uint32_t regnum, len, signal, count;
2507 uint64_t addr;
2509 type = record_list->type;
2510 bfdcore_write (obfd, osec, &type, sizeof (type), &bfd_offset);
2512 switch (record_list->type)
2514 case record_reg: /* reg */
2515 if (record_debug)
2516 fprintf_unfiltered (gdb_stdlog, "\
2517 Writing register %d (1 plus %lu plus %d bytes)\n",
2518 record_list->u.reg.num,
2519 (unsigned long) sizeof (regnum),
2520 record_list->u.reg.len);
2522 /* Write regnum. */
2523 regnum = netorder32 (record_list->u.reg.num);
2524 bfdcore_write (obfd, osec, &regnum,
2525 sizeof (regnum), &bfd_offset);
2527 /* Write regval. */
2528 bfdcore_write (obfd, osec, record_get_loc (record_list),
2529 record_list->u.reg.len, &bfd_offset);
2530 break;
2532 case record_mem: /* mem */
2533 if (record_debug)
2534 fprintf_unfiltered (gdb_stdlog, "\
2535 Writing memory %s (1 plus %lu plus %lu plus %d bytes)\n",
2536 paddress (gdbarch,
2537 record_list->u.mem.addr),
2538 (unsigned long) sizeof (addr),
2539 (unsigned long) sizeof (len),
2540 record_list->u.mem.len);
2542 /* Write memlen. */
2543 len = netorder32 (record_list->u.mem.len);
2544 bfdcore_write (obfd, osec, &len, sizeof (len), &bfd_offset);
2546 /* Write memaddr. */
2547 addr = netorder64 (record_list->u.mem.addr);
2548 bfdcore_write (obfd, osec, &addr,
2549 sizeof (addr), &bfd_offset);
2551 /* Write memval. */
2552 bfdcore_write (obfd, osec, record_get_loc (record_list),
2553 record_list->u.mem.len, &bfd_offset);
2554 break;
2556 case record_end:
2557 if (record_debug)
2558 fprintf_unfiltered (gdb_stdlog, "\
2559 Writing record_end (1 + %lu + %lu bytes)\n",
2560 (unsigned long) sizeof (signal),
2561 (unsigned long) sizeof (count));
2562 /* Write signal value. */
2563 signal = netorder32 (record_list->u.end.sigval);
2564 bfdcore_write (obfd, osec, &signal,
2565 sizeof (signal), &bfd_offset);
2567 /* Write insn count. */
2568 count = netorder32 (record_list->u.end.insn_num);
2569 bfdcore_write (obfd, osec, &count,
2570 sizeof (count), &bfd_offset);
2571 break;
2575 /* Execute entry. */
2576 record_exec_insn (regcache, gdbarch, record_list);
2578 if (record_list->next)
2579 record_list = record_list->next;
2580 else
2581 break;
2584 /* Reverse execute to cur_record_list. */
2585 while (1)
2587 /* Check for beginning and end of log. */
2588 if (record_list == cur_record_list)
2589 break;
2591 record_exec_insn (regcache, gdbarch, record_list);
2593 if (record_list->prev)
2594 record_list = record_list->prev;
2597 do_cleanups (set_cleanups);
2598 bfd_close (obfd);
2599 discard_cleanups (old_cleanups);
2601 /* Succeeded. */
2602 printf_filtered (_("Saved core file %s with execution log.\n"),
2603 recfilename);
2606 /* record_goto_insn -- rewind the record log (forward or backward,
2607 depending on DIR) to the given entry, changing the program state
2608 correspondingly. */
2610 static void
2611 record_goto_insn (struct record_entry *entry,
2612 enum exec_direction_kind dir)
2614 struct cleanup *set_cleanups = record_gdb_operation_disable_set ();
2615 struct regcache *regcache = get_current_regcache ();
2616 struct gdbarch *gdbarch = get_regcache_arch (regcache);
2618 /* Assume everything is valid: we will hit the entry,
2619 and we will not hit the end of the recording. */
2621 if (dir == EXEC_FORWARD)
2622 record_list = record_list->next;
2626 record_exec_insn (regcache, gdbarch, record_list);
2627 if (dir == EXEC_REVERSE)
2628 record_list = record_list->prev;
2629 else
2630 record_list = record_list->next;
2631 } while (record_list != entry);
2632 do_cleanups (set_cleanups);
2635 /* "record goto" command. Argument is an instruction number,
2636 as given by "info record".
2638 Rewinds the recording (forward or backward) to the given instruction. */
2640 static void
2641 cmd_record_goto (char *arg, int from_tty)
2643 struct record_entry *p = NULL;
2644 ULONGEST target_insn = 0;
2646 if (arg == NULL || *arg == '\0')
2647 error (_("Command requires an argument (insn number to go to)."));
2649 if (strncmp (arg, "start", strlen ("start")) == 0
2650 || strncmp (arg, "begin", strlen ("begin")) == 0)
2652 /* Special case. Find first insn. */
2653 for (p = &record_first; p != NULL; p = p->next)
2654 if (p->type == record_end)
2655 break;
2656 if (p)
2657 target_insn = p->u.end.insn_num;
2659 else if (strncmp (arg, "end", strlen ("end")) == 0)
2661 /* Special case. Find last insn. */
2662 for (p = record_list; p->next != NULL; p = p->next)
2664 for (; p!= NULL; p = p->prev)
2665 if (p->type == record_end)
2666 break;
2667 if (p)
2668 target_insn = p->u.end.insn_num;
2670 else
2672 /* General case. Find designated insn. */
2673 target_insn = parse_and_eval_long (arg);
2675 for (p = &record_first; p != NULL; p = p->next)
2676 if (p->type == record_end && p->u.end.insn_num == target_insn)
2677 break;
2680 if (p == NULL)
2681 error (_("Target insn '%s' not found."), arg);
2682 else if (p == record_list)
2683 error (_("Already at insn '%s'."), arg);
2684 else if (p->u.end.insn_num > record_list->u.end.insn_num)
2686 printf_filtered (_("Go forward to insn number %s\n"),
2687 pulongest (target_insn));
2688 record_goto_insn (p, EXEC_FORWARD);
2690 else
2692 printf_filtered (_("Go backward to insn number %s\n"),
2693 pulongest (target_insn));
2694 record_goto_insn (p, EXEC_REVERSE);
2696 registers_changed ();
2697 reinit_frame_cache ();
2698 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
2701 void
2702 _initialize_record (void)
2704 struct cmd_list_element *c;
2706 /* Init record_first. */
2707 record_first.prev = NULL;
2708 record_first.next = NULL;
2709 record_first.type = record_end;
2711 init_record_ops ();
2712 add_target (&record_ops);
2713 init_record_core_ops ();
2714 add_target (&record_core_ops);
2716 add_setshow_zinteger_cmd ("record", no_class, &record_debug,
2717 _("Set debugging of record/replay feature."),
2718 _("Show debugging of record/replay feature."),
2719 _("When enabled, debugging output for "
2720 "record/replay feature is displayed."),
2721 NULL, show_record_debug, &setdebuglist,
2722 &showdebuglist);
2724 c = add_prefix_cmd ("record", class_obscure, cmd_record_start,
2725 _("Abbreviated form of \"target record\" command."),
2726 &record_cmdlist, "record ", 0, &cmdlist);
2727 set_cmd_completer (c, filename_completer);
2729 add_com_alias ("rec", "record", class_obscure, 1);
2730 add_prefix_cmd ("record", class_support, set_record_command,
2731 _("Set record options"), &set_record_cmdlist,
2732 "set record ", 0, &setlist);
2733 add_alias_cmd ("rec", "record", class_obscure, 1, &setlist);
2734 add_prefix_cmd ("record", class_support, show_record_command,
2735 _("Show record options"), &show_record_cmdlist,
2736 "show record ", 0, &showlist);
2737 add_alias_cmd ("rec", "record", class_obscure, 1, &showlist);
2738 add_prefix_cmd ("record", class_support, info_record_command,
2739 _("Info record options"), &info_record_cmdlist,
2740 "info record ", 0, &infolist);
2741 add_alias_cmd ("rec", "record", class_obscure, 1, &infolist);
2743 c = add_cmd ("save", class_obscure, cmd_record_save,
2744 _("Save the execution log to a file.\n\
2745 Argument is optional filename.\n\
2746 Default filename is 'gdb_record.<process_id>'."),
2747 &record_cmdlist);
2748 set_cmd_completer (c, filename_completer);
2750 c = add_cmd ("restore", class_obscure, cmd_record_restore,
2751 _("Restore the execution log from a file.\n\
2752 Argument is filename. File must be created with 'record save'."),
2753 &record_cmdlist);
2754 set_cmd_completer (c, filename_completer);
2756 add_cmd ("delete", class_obscure, cmd_record_delete,
2757 _("Delete the rest of execution log and start recording it anew."),
2758 &record_cmdlist);
2759 add_alias_cmd ("d", "delete", class_obscure, 1, &record_cmdlist);
2760 add_alias_cmd ("del", "delete", class_obscure, 1, &record_cmdlist);
2762 add_cmd ("stop", class_obscure, cmd_record_stop,
2763 _("Stop the record/replay target."),
2764 &record_cmdlist);
2765 add_alias_cmd ("s", "stop", class_obscure, 1, &record_cmdlist);
2767 /* Record instructions number limit command. */
2768 add_setshow_boolean_cmd ("stop-at-limit", no_class,
2769 &record_stop_at_limit, _("\
2770 Set whether record/replay stops when record/replay buffer becomes full."), _("\
2771 Show whether record/replay stops when record/replay buffer becomes full."), _("\
2772 Default is ON.\n\
2773 When ON, if the record/replay buffer becomes full, ask user what to do.\n\
2774 When OFF, if the record/replay buffer becomes full,\n\
2775 delete the oldest recorded instruction to make room for each new one."),
2776 NULL, NULL,
2777 &set_record_cmdlist, &show_record_cmdlist);
2778 add_setshow_uinteger_cmd ("insn-number-max", no_class,
2779 &record_insn_max_num,
2780 _("Set record/replay buffer limit."),
2781 _("Show record/replay buffer limit."), _("\
2782 Set the maximum number of instructions to be stored in the\n\
2783 record/replay buffer. Zero means unlimited. Default is 200000."),
2784 set_record_insn_max_num,
2785 NULL, &set_record_cmdlist, &show_record_cmdlist);
2787 add_cmd ("goto", class_obscure, cmd_record_goto, _("\
2788 Restore the program to its state at instruction number N.\n\
2789 Argument is instruction number, as shown by 'info record'."),
2790 &record_cmdlist);
2792 add_setshow_boolean_cmd ("memory-query", no_class,
2793 &record_memory_query, _("\
2794 Set whether query if PREC cannot record memory change of next instruction."),
2795 _("\
2796 Show whether query if PREC cannot record memory change of next instruction."),
2797 _("\
2798 Default is OFF.\n\
2799 When ON, query if PREC cannot record memory change of next instruction."),
2800 NULL, NULL,
2801 &set_record_cmdlist, &show_record_cmdlist);