2012-11-06 Tristan Gingold <gingold@adacore.com>
[binutils-gdb.git] / gdb / record.c
blobc98ed321b985c7bc32df253efdb2bfa8647b211d
1 /* Process record and replay target for GDB, the GNU debugger.
3 Copyright (C) 2008-2012 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"
33 #include "event-loop.h"
34 #include "inf-loop.h"
35 #include "gdb_bfd.h"
36 #include "observer.h"
38 #include <signal.h>
40 /* This module implements "target record", also known as "process
41 record and replay". This target sits on top of a "normal" target
42 (a target that "has execution"), and provides a record and replay
43 functionality, including reverse debugging.
45 Target record has two modes: recording, and replaying.
47 In record mode, we intercept the to_resume and to_wait methods.
48 Whenever gdb resumes the target, we run the target in single step
49 mode, and we build up an execution log in which, for each executed
50 instruction, we record all changes in memory and register state.
51 This is invisible to the user, to whom it just looks like an
52 ordinary debugging session (except for performance degredation).
54 In replay mode, instead of actually letting the inferior run as a
55 process, we simulate its execution by playing back the recorded
56 execution log. For each instruction in the log, we simulate the
57 instruction's side effects by duplicating the changes that it would
58 have made on memory and registers. */
60 #define DEFAULT_RECORD_INSN_MAX_NUM 200000
62 #define RECORD_IS_REPLAY \
63 (record_list->next || execution_direction == EXEC_REVERSE)
65 #define RECORD_FILE_MAGIC netorder32(0x20091016)
67 /* These are the core structs of the process record functionality.
69 A record_entry is a record of the value change of a register
70 ("record_reg") or a part of memory ("record_mem"). And each
71 instruction must have a struct record_entry ("record_end") that
72 indicates that this is the last struct record_entry of this
73 instruction.
75 Each struct record_entry is linked to "record_list" by "prev" and
76 "next" pointers. */
78 struct record_mem_entry
80 CORE_ADDR addr;
81 int len;
82 /* Set this flag if target memory for this entry
83 can no longer be accessed. */
84 int mem_entry_not_accessible;
85 union
87 gdb_byte *ptr;
88 gdb_byte buf[sizeof (gdb_byte *)];
89 } u;
92 struct record_reg_entry
94 unsigned short num;
95 unsigned short len;
96 union
98 gdb_byte *ptr;
99 gdb_byte buf[2 * sizeof (gdb_byte *)];
100 } u;
103 struct record_end_entry
105 enum gdb_signal sigval;
106 ULONGEST insn_num;
109 enum record_type
111 record_end = 0,
112 record_reg,
113 record_mem
116 /* This is the data structure that makes up the execution log.
118 The execution log consists of a single linked list of entries
119 of type "struct record_entry". It is doubly linked so that it
120 can be traversed in either direction.
122 The start of the list is anchored by a struct called
123 "record_first". The pointer "record_list" either points to the
124 last entry that was added to the list (in record mode), or to the
125 next entry in the list that will be executed (in replay mode).
127 Each list element (struct record_entry), in addition to next and
128 prev pointers, consists of a union of three entry types: mem, reg,
129 and end. A field called "type" determines which entry type is
130 represented by a given list element.
132 Each instruction that is added to the execution log is represented
133 by a variable number of list elements ('entries'). The instruction
134 will have one "reg" entry for each register that is changed by
135 executing the instruction (including the PC in every case). It
136 will also have one "mem" entry for each memory change. Finally,
137 each instruction will have an "end" entry that separates it from
138 the changes associated with the next instruction. */
140 struct record_entry
142 struct record_entry *prev;
143 struct record_entry *next;
144 enum record_type type;
145 union
147 /* reg */
148 struct record_reg_entry reg;
149 /* mem */
150 struct record_mem_entry mem;
151 /* end */
152 struct record_end_entry end;
153 } u;
156 /* This is the debug switch for process record. */
157 unsigned int record_debug = 0;
159 /* If true, query if PREC cannot record memory
160 change of next instruction. */
161 int record_memory_query = 0;
163 struct record_core_buf_entry
165 struct record_core_buf_entry *prev;
166 struct target_section *p;
167 bfd_byte *buf;
170 /* Record buf with core target. */
171 static gdb_byte *record_core_regbuf = NULL;
172 static struct target_section *record_core_start;
173 static struct target_section *record_core_end;
174 static struct record_core_buf_entry *record_core_buf_list = NULL;
176 /* The following variables are used for managing the linked list that
177 represents the execution log.
179 record_first is the anchor that holds down the beginning of the list.
181 record_list serves two functions:
182 1) In record mode, it anchors the end of the list.
183 2) In replay mode, it traverses the list and points to
184 the next instruction that must be emulated.
186 record_arch_list_head and record_arch_list_tail are used to manage
187 a separate list, which is used to build up the change elements of
188 the currently executing instruction during record mode. When this
189 instruction has been completely annotated in the "arch list", it
190 will be appended to the main execution log. */
192 static struct record_entry record_first;
193 static struct record_entry *record_list = &record_first;
194 static struct record_entry *record_arch_list_head = NULL;
195 static struct record_entry *record_arch_list_tail = NULL;
197 /* 1 ask user. 0 auto delete the last struct record_entry. */
198 static int record_stop_at_limit = 1;
199 /* Maximum allowed number of insns in execution log. */
200 static unsigned int record_insn_max_num = DEFAULT_RECORD_INSN_MAX_NUM;
201 /* Actual count of insns presently in execution log. */
202 static int record_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_insn_count;
207 /* The target_ops of process record. */
208 static struct target_ops record_ops;
209 static struct target_ops record_core_ops;
211 /* The beneath function pointers. */
212 static struct target_ops *record_beneath_to_resume_ops;
213 static void (*record_beneath_to_resume) (struct target_ops *, ptid_t, int,
214 enum gdb_signal);
215 static struct target_ops *record_beneath_to_wait_ops;
216 static ptid_t (*record_beneath_to_wait) (struct target_ops *, ptid_t,
217 struct target_waitstatus *,
218 int);
219 static struct target_ops *record_beneath_to_store_registers_ops;
220 static void (*record_beneath_to_store_registers) (struct target_ops *,
221 struct regcache *,
222 int regno);
223 static struct target_ops *record_beneath_to_xfer_partial_ops;
224 static LONGEST (*record_beneath_to_xfer_partial) (struct target_ops *ops,
225 enum target_object object,
226 const char *annex,
227 gdb_byte *readbuf,
228 const gdb_byte *writebuf,
229 ULONGEST offset,
230 LONGEST len);
231 static int (*record_beneath_to_insert_breakpoint) (struct gdbarch *,
232 struct bp_target_info *);
233 static int (*record_beneath_to_remove_breakpoint) (struct gdbarch *,
234 struct bp_target_info *);
235 static int (*record_beneath_to_stopped_by_watchpoint) (void);
236 static int (*record_beneath_to_stopped_data_address) (struct target_ops *,
237 CORE_ADDR *);
238 static void (*record_beneath_to_async) (void (*) (enum inferior_event_type, void *), void *);
240 /* Alloc and free functions for record_reg, record_mem, and record_end
241 entries. */
243 /* Alloc a record_reg record entry. */
245 static inline struct record_entry *
246 record_reg_alloc (struct regcache *regcache, int regnum)
248 struct record_entry *rec;
249 struct gdbarch *gdbarch = get_regcache_arch (regcache);
251 rec = (struct record_entry *) xcalloc (1, sizeof (struct record_entry));
252 rec->type = record_reg;
253 rec->u.reg.num = regnum;
254 rec->u.reg.len = register_size (gdbarch, regnum);
255 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
256 rec->u.reg.u.ptr = (gdb_byte *) xmalloc (rec->u.reg.len);
258 return rec;
261 /* Free a record_reg record entry. */
263 static inline void
264 record_reg_release (struct record_entry *rec)
266 gdb_assert (rec->type == record_reg);
267 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
268 xfree (rec->u.reg.u.ptr);
269 xfree (rec);
272 /* Alloc a record_mem record entry. */
274 static inline struct record_entry *
275 record_mem_alloc (CORE_ADDR addr, int len)
277 struct record_entry *rec;
279 rec = (struct record_entry *) xcalloc (1, sizeof (struct record_entry));
280 rec->type = record_mem;
281 rec->u.mem.addr = addr;
282 rec->u.mem.len = len;
283 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
284 rec->u.mem.u.ptr = (gdb_byte *) xmalloc (len);
286 return rec;
289 /* Free a record_mem record entry. */
291 static inline void
292 record_mem_release (struct record_entry *rec)
294 gdb_assert (rec->type == record_mem);
295 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
296 xfree (rec->u.mem.u.ptr);
297 xfree (rec);
300 /* Alloc a record_end record entry. */
302 static inline struct record_entry *
303 record_end_alloc (void)
305 struct record_entry *rec;
307 rec = (struct record_entry *) xcalloc (1, sizeof (struct record_entry));
308 rec->type = record_end;
310 return rec;
313 /* Free a record_end record entry. */
315 static inline void
316 record_end_release (struct record_entry *rec)
318 xfree (rec);
321 /* Free one record entry, any type.
322 Return entry->type, in case caller wants to know. */
324 static inline enum record_type
325 record_entry_release (struct record_entry *rec)
327 enum record_type type = rec->type;
329 switch (type) {
330 case record_reg:
331 record_reg_release (rec);
332 break;
333 case record_mem:
334 record_mem_release (rec);
335 break;
336 case record_end:
337 record_end_release (rec);
338 break;
340 return type;
343 /* Free all record entries in list pointed to by REC. */
345 static void
346 record_list_release (struct record_entry *rec)
348 if (!rec)
349 return;
351 while (rec->next)
352 rec = rec->next;
354 while (rec->prev)
356 rec = rec->prev;
357 record_entry_release (rec->next);
360 if (rec == &record_first)
362 record_insn_num = 0;
363 record_first.next = NULL;
365 else
366 record_entry_release (rec);
369 /* Free all record entries forward of the given list position. */
371 static void
372 record_list_release_following (struct record_entry *rec)
374 struct record_entry *tmp = rec->next;
376 rec->next = NULL;
377 while (tmp)
379 rec = tmp->next;
380 if (record_entry_release (tmp) == record_end)
382 record_insn_num--;
383 record_insn_count--;
385 tmp = rec;
389 /* Delete the first instruction from the beginning of the log, to make
390 room for adding a new instruction at the end of the log.
392 Note -- this function does not modify record_insn_num. */
394 static void
395 record_list_release_first (void)
397 struct record_entry *tmp;
399 if (!record_first.next)
400 return;
402 /* Loop until a record_end. */
403 while (1)
405 /* Cut record_first.next out of the linked list. */
406 tmp = record_first.next;
407 record_first.next = tmp->next;
408 tmp->next->prev = &record_first;
410 /* tmp is now isolated, and can be deleted. */
411 if (record_entry_release (tmp) == record_end)
412 break; /* End loop at first record_end. */
414 if (!record_first.next)
416 gdb_assert (record_insn_num == 1);
417 break; /* End loop when list is empty. */
422 /* Add a struct record_entry to record_arch_list. */
424 static void
425 record_arch_list_add (struct record_entry *rec)
427 if (record_debug > 1)
428 fprintf_unfiltered (gdb_stdlog,
429 "Process record: record_arch_list_add %s.\n",
430 host_address_to_string (rec));
432 if (record_arch_list_tail)
434 record_arch_list_tail->next = rec;
435 rec->prev = record_arch_list_tail;
436 record_arch_list_tail = rec;
438 else
440 record_arch_list_head = rec;
441 record_arch_list_tail = rec;
445 /* Return the value storage location of a record entry. */
446 static inline gdb_byte *
447 record_get_loc (struct record_entry *rec)
449 switch (rec->type) {
450 case record_mem:
451 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
452 return rec->u.mem.u.ptr;
453 else
454 return rec->u.mem.u.buf;
455 case record_reg:
456 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
457 return rec->u.reg.u.ptr;
458 else
459 return rec->u.reg.u.buf;
460 case record_end:
461 default:
462 gdb_assert_not_reached ("unexpected record_entry type");
463 return NULL;
467 /* Record the value of a register NUM to record_arch_list. */
470 record_arch_list_add_reg (struct regcache *regcache, int regnum)
472 struct record_entry *rec;
474 if (record_debug > 1)
475 fprintf_unfiltered (gdb_stdlog,
476 "Process record: add register num = %d to "
477 "record list.\n",
478 regnum);
480 rec = record_reg_alloc (regcache, regnum);
482 regcache_raw_read (regcache, regnum, record_get_loc (rec));
484 record_arch_list_add (rec);
486 return 0;
490 record_read_memory (struct gdbarch *gdbarch,
491 CORE_ADDR memaddr, gdb_byte *myaddr,
492 ssize_t len)
494 int ret = target_read_memory (memaddr, myaddr, len);
496 if (ret && record_debug)
497 printf_unfiltered (_("Process record: error reading memory "
498 "at addr %s len = %ld.\n"),
499 paddress (gdbarch, memaddr), (long) len);
500 return ret;
503 /* Record the value of a region of memory whose address is ADDR and
504 length is LEN to record_arch_list. */
507 record_arch_list_add_mem (CORE_ADDR addr, int len)
509 struct record_entry *rec;
511 if (record_debug > 1)
512 fprintf_unfiltered (gdb_stdlog,
513 "Process record: add mem addr = %s len = %d to "
514 "record list.\n",
515 paddress (target_gdbarch, addr), len);
517 if (!addr) /* FIXME: Why? Some arch must permit it... */
518 return 0;
520 rec = record_mem_alloc (addr, len);
522 if (record_read_memory (target_gdbarch, addr, record_get_loc (rec), len))
524 record_mem_release (rec);
525 return -1;
528 record_arch_list_add (rec);
530 return 0;
533 /* Add a record_end type struct record_entry to record_arch_list. */
536 record_arch_list_add_end (void)
538 struct record_entry *rec;
540 if (record_debug > 1)
541 fprintf_unfiltered (gdb_stdlog,
542 "Process record: add end to arch list.\n");
544 rec = record_end_alloc ();
545 rec->u.end.sigval = GDB_SIGNAL_0;
546 rec->u.end.insn_num = ++record_insn_count;
548 record_arch_list_add (rec);
550 return 0;
553 static void
554 record_check_insn_num (int set_terminal)
556 if (record_insn_max_num)
558 gdb_assert (record_insn_num <= record_insn_max_num);
559 if (record_insn_num == record_insn_max_num)
561 /* Ask user what to do. */
562 if (record_stop_at_limit)
564 int q;
566 if (set_terminal)
567 target_terminal_ours ();
568 q = yquery (_("Do you want to auto delete previous execution "
569 "log entries when record/replay buffer becomes "
570 "full (record stop-at-limit)?"));
571 if (set_terminal)
572 target_terminal_inferior ();
573 if (q)
574 record_stop_at_limit = 0;
575 else
576 error (_("Process record: stopped by user."));
582 static void
583 record_arch_list_cleanups (void *ignore)
585 record_list_release (record_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_list. This function will call gdbarch_process_record to
591 record the running message of inferior and set them to
592 record_arch_list, and add it to record_list. */
594 static int
595 record_message (struct regcache *regcache, enum gdb_signal signal)
597 int ret;
598 struct gdbarch *gdbarch = get_regcache_arch (regcache);
599 struct cleanup *old_cleanups = make_cleanup (record_arch_list_cleanups, 0);
601 record_arch_list_head = NULL;
602 record_arch_list_tail = NULL;
604 /* Check record_insn_num. */
605 record_check_insn_num (1);
607 /* If gdb sends a signal value to target_resume,
608 save it in the 'end' field of the previous instruction.
610 Maybe process record should record what really happened,
611 rather than what gdb pretends has happened.
613 So if Linux delivered the signal to the child process during
614 the record mode, we will record it and deliver it again in
615 the replay mode.
617 If user says "ignore this signal" during the record mode, then
618 it will be ignored again during the replay mode (no matter if
619 the user says something different, like "deliver this signal"
620 during the replay mode).
622 User should understand that nothing he does during the replay
623 mode will change the behavior of the child. If he tries,
624 then that is a user error.
626 But we should still deliver the signal to gdb during the replay,
627 if we delivered it during the recording. Therefore we should
628 record the signal during record_wait, not record_resume. */
629 if (record_list != &record_first) /* FIXME better way to check */
631 gdb_assert (record_list->type == record_end);
632 record_list->u.end.sigval = signal;
635 if (signal == GDB_SIGNAL_0
636 || !gdbarch_process_record_signal_p (gdbarch))
637 ret = gdbarch_process_record (gdbarch,
638 regcache,
639 regcache_read_pc (regcache));
640 else
641 ret = gdbarch_process_record_signal (gdbarch,
642 regcache,
643 signal);
645 if (ret > 0)
646 error (_("Process record: inferior program stopped."));
647 if (ret < 0)
648 error (_("Process record: failed to record execution log."));
650 discard_cleanups (old_cleanups);
652 record_list->next = record_arch_list_head;
653 record_arch_list_head->prev = record_list;
654 record_list = record_arch_list_tail;
656 if (record_insn_num == record_insn_max_num && record_insn_max_num)
657 record_list_release_first ();
658 else
659 record_insn_num++;
661 return 1;
664 struct record_message_args {
665 struct regcache *regcache;
666 enum gdb_signal signal;
669 static int
670 record_message_wrapper (void *args)
672 struct record_message_args *record_args = args;
674 return record_message (record_args->regcache, record_args->signal);
677 static int
678 record_message_wrapper_safe (struct regcache *regcache,
679 enum gdb_signal signal)
681 struct record_message_args args;
683 args.regcache = regcache;
684 args.signal = signal;
686 return catch_errors (record_message_wrapper, &args, NULL, RETURN_MASK_ALL);
689 /* Set to 1 if record_store_registers and record_xfer_partial
690 doesn't need record. */
692 static int record_gdb_operation_disable = 0;
694 struct cleanup *
695 record_gdb_operation_disable_set (void)
697 struct cleanup *old_cleanups = NULL;
699 old_cleanups =
700 make_cleanup_restore_integer (&record_gdb_operation_disable);
701 record_gdb_operation_disable = 1;
703 return old_cleanups;
706 /* Flag set to TRUE for target_stopped_by_watchpoint. */
707 static int record_hw_watchpoint = 0;
709 /* Execute one instruction from the record log. Each instruction in
710 the log will be represented by an arbitrary sequence of register
711 entries and memory entries, followed by an 'end' entry. */
713 static inline void
714 record_exec_insn (struct regcache *regcache, struct gdbarch *gdbarch,
715 struct record_entry *entry)
717 switch (entry->type)
719 case record_reg: /* reg */
721 gdb_byte reg[MAX_REGISTER_SIZE];
723 if (record_debug > 1)
724 fprintf_unfiltered (gdb_stdlog,
725 "Process record: record_reg %s to "
726 "inferior num = %d.\n",
727 host_address_to_string (entry),
728 entry->u.reg.num);
730 regcache_cooked_read (regcache, entry->u.reg.num, reg);
731 regcache_cooked_write (regcache, entry->u.reg.num,
732 record_get_loc (entry));
733 memcpy (record_get_loc (entry), reg, entry->u.reg.len);
735 break;
737 case record_mem: /* mem */
739 /* Nothing to do if the entry is flagged not_accessible. */
740 if (!entry->u.mem.mem_entry_not_accessible)
742 gdb_byte *mem = alloca (entry->u.mem.len);
744 if (record_debug > 1)
745 fprintf_unfiltered (gdb_stdlog,
746 "Process record: record_mem %s to "
747 "inferior addr = %s len = %d.\n",
748 host_address_to_string (entry),
749 paddress (gdbarch, entry->u.mem.addr),
750 entry->u.mem.len);
752 if (record_read_memory (gdbarch,
753 entry->u.mem.addr, mem, entry->u.mem.len))
754 entry->u.mem.mem_entry_not_accessible = 1;
755 else
757 if (target_write_memory (entry->u.mem.addr,
758 record_get_loc (entry),
759 entry->u.mem.len))
761 entry->u.mem.mem_entry_not_accessible = 1;
762 if (record_debug)
763 warning (_("Process record: error writing memory at "
764 "addr = %s len = %d."),
765 paddress (gdbarch, entry->u.mem.addr),
766 entry->u.mem.len);
768 else
770 memcpy (record_get_loc (entry), mem, entry->u.mem.len);
772 /* We've changed memory --- check if a hardware
773 watchpoint should trap. Note that this
774 presently assumes the target beneath supports
775 continuable watchpoints. On non-continuable
776 watchpoints target, we'll want to check this
777 _before_ actually doing the memory change, and
778 not doing the change at all if the watchpoint
779 traps. */
780 if (hardware_watchpoint_inserted_in_range
781 (get_regcache_aspace (regcache),
782 entry->u.mem.addr, entry->u.mem.len))
783 record_hw_watchpoint = 1;
788 break;
792 static struct target_ops *tmp_to_resume_ops;
793 static void (*tmp_to_resume) (struct target_ops *, ptid_t, int,
794 enum gdb_signal);
795 static struct target_ops *tmp_to_wait_ops;
796 static ptid_t (*tmp_to_wait) (struct target_ops *, ptid_t,
797 struct target_waitstatus *,
798 int);
799 static struct target_ops *tmp_to_store_registers_ops;
800 static void (*tmp_to_store_registers) (struct target_ops *,
801 struct regcache *,
802 int regno);
803 static struct target_ops *tmp_to_xfer_partial_ops;
804 static LONGEST (*tmp_to_xfer_partial) (struct target_ops *ops,
805 enum target_object object,
806 const char *annex,
807 gdb_byte *readbuf,
808 const gdb_byte *writebuf,
809 ULONGEST offset,
810 LONGEST len);
811 static int (*tmp_to_insert_breakpoint) (struct gdbarch *,
812 struct bp_target_info *);
813 static int (*tmp_to_remove_breakpoint) (struct gdbarch *,
814 struct bp_target_info *);
815 static int (*tmp_to_stopped_by_watchpoint) (void);
816 static int (*tmp_to_stopped_data_address) (struct target_ops *, CORE_ADDR *);
817 static int (*tmp_to_stopped_data_address) (struct target_ops *, CORE_ADDR *);
818 static void (*tmp_to_async) (void (*) (enum inferior_event_type, void *), void *);
820 static void record_restore (void);
822 /* Asynchronous signal handle registered as event loop source for when
823 we have pending events ready to be passed to the core. */
825 static struct async_event_handler *record_async_inferior_event_token;
827 static void
828 record_async_inferior_event_handler (gdb_client_data data)
830 inferior_event_handler (INF_REG_EVENT, NULL);
833 /* Open the process record target. */
835 static void
836 record_core_open_1 (char *name, int from_tty)
838 struct regcache *regcache = get_current_regcache ();
839 int regnum = gdbarch_num_regs (get_regcache_arch (regcache));
840 int i;
842 /* Get record_core_regbuf. */
843 target_fetch_registers (regcache, -1);
844 record_core_regbuf = xmalloc (MAX_REGISTER_SIZE * regnum);
845 for (i = 0; i < regnum; i ++)
846 regcache_raw_collect (regcache, i,
847 record_core_regbuf + MAX_REGISTER_SIZE * i);
849 /* Get record_core_start and record_core_end. */
850 if (build_section_table (core_bfd, &record_core_start, &record_core_end))
852 xfree (record_core_regbuf);
853 record_core_regbuf = NULL;
854 error (_("\"%s\": Can't find sections: %s"),
855 bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ()));
858 push_target (&record_core_ops);
859 record_restore ();
862 /* "to_open" target method for 'live' processes. */
864 static void
865 record_open_1 (char *name, int from_tty)
867 if (record_debug)
868 fprintf_unfiltered (gdb_stdlog, "Process record: record_open\n");
870 /* check exec */
871 if (!target_has_execution)
872 error (_("Process record: the program is not being run."));
873 if (non_stop)
874 error (_("Process record target can't debug inferior in non-stop mode "
875 "(non-stop)."));
877 if (!gdbarch_process_record_p (target_gdbarch))
878 error (_("Process record: the current architecture doesn't support "
879 "record function."));
881 if (!tmp_to_resume)
882 error (_("Could not find 'to_resume' method on the target stack."));
883 if (!tmp_to_wait)
884 error (_("Could not find 'to_wait' method on the target stack."));
885 if (!tmp_to_store_registers)
886 error (_("Could not find 'to_store_registers' "
887 "method on the target stack."));
888 if (!tmp_to_insert_breakpoint)
889 error (_("Could not find 'to_insert_breakpoint' "
890 "method on the target stack."));
891 if (!tmp_to_remove_breakpoint)
892 error (_("Could not find 'to_remove_breakpoint' "
893 "method on the target stack."));
894 if (!tmp_to_stopped_by_watchpoint)
895 error (_("Could not find 'to_stopped_by_watchpoint' "
896 "method on the target stack."));
897 if (!tmp_to_stopped_data_address)
898 error (_("Could not find 'to_stopped_data_address' "
899 "method on the target stack."));
901 push_target (&record_ops);
904 static void record_init_record_breakpoints (void);
906 /* "to_open" target method. Open the process record target. */
908 static void
909 record_open (char *name, int from_tty)
911 struct target_ops *t;
913 if (record_debug)
914 fprintf_unfiltered (gdb_stdlog, "Process record: record_open\n");
916 /* Check if record target is already running. */
917 if (current_target.to_stratum == record_stratum)
918 error (_("Process record target already running. Use \"record stop\" to "
919 "stop record target first."));
921 /* Reset the tmp beneath pointers. */
922 tmp_to_resume_ops = NULL;
923 tmp_to_resume = NULL;
924 tmp_to_wait_ops = NULL;
925 tmp_to_wait = NULL;
926 tmp_to_store_registers_ops = NULL;
927 tmp_to_store_registers = NULL;
928 tmp_to_xfer_partial_ops = NULL;
929 tmp_to_xfer_partial = NULL;
930 tmp_to_insert_breakpoint = NULL;
931 tmp_to_remove_breakpoint = NULL;
932 tmp_to_stopped_by_watchpoint = NULL;
933 tmp_to_stopped_data_address = NULL;
934 tmp_to_async = NULL;
936 /* Set the beneath function pointers. */
937 for (t = current_target.beneath; t != NULL; t = t->beneath)
939 if (!tmp_to_resume)
941 tmp_to_resume = t->to_resume;
942 tmp_to_resume_ops = t;
944 if (!tmp_to_wait)
946 tmp_to_wait = t->to_wait;
947 tmp_to_wait_ops = t;
949 if (!tmp_to_store_registers)
951 tmp_to_store_registers = t->to_store_registers;
952 tmp_to_store_registers_ops = t;
954 if (!tmp_to_xfer_partial)
956 tmp_to_xfer_partial = t->to_xfer_partial;
957 tmp_to_xfer_partial_ops = t;
959 if (!tmp_to_insert_breakpoint)
960 tmp_to_insert_breakpoint = t->to_insert_breakpoint;
961 if (!tmp_to_remove_breakpoint)
962 tmp_to_remove_breakpoint = t->to_remove_breakpoint;
963 if (!tmp_to_stopped_by_watchpoint)
964 tmp_to_stopped_by_watchpoint = t->to_stopped_by_watchpoint;
965 if (!tmp_to_stopped_data_address)
966 tmp_to_stopped_data_address = t->to_stopped_data_address;
967 if (!tmp_to_async)
968 tmp_to_async = t->to_async;
970 if (!tmp_to_xfer_partial)
971 error (_("Could not find 'to_xfer_partial' method on the target stack."));
973 /* Reset */
974 record_insn_num = 0;
975 record_insn_count = 0;
976 record_list = &record_first;
977 record_list->next = NULL;
979 /* Set the tmp beneath pointers to beneath pointers. */
980 record_beneath_to_resume_ops = tmp_to_resume_ops;
981 record_beneath_to_resume = tmp_to_resume;
982 record_beneath_to_wait_ops = tmp_to_wait_ops;
983 record_beneath_to_wait = tmp_to_wait;
984 record_beneath_to_store_registers_ops = tmp_to_store_registers_ops;
985 record_beneath_to_store_registers = tmp_to_store_registers;
986 record_beneath_to_xfer_partial_ops = tmp_to_xfer_partial_ops;
987 record_beneath_to_xfer_partial = tmp_to_xfer_partial;
988 record_beneath_to_insert_breakpoint = tmp_to_insert_breakpoint;
989 record_beneath_to_remove_breakpoint = tmp_to_remove_breakpoint;
990 record_beneath_to_stopped_by_watchpoint = tmp_to_stopped_by_watchpoint;
991 record_beneath_to_stopped_data_address = tmp_to_stopped_data_address;
992 record_beneath_to_async = tmp_to_async;
994 if (core_bfd)
995 record_core_open_1 (name, from_tty);
996 else
997 record_open_1 (name, from_tty);
999 /* Register extra event sources in the event loop. */
1000 record_async_inferior_event_token
1001 = create_async_event_handler (record_async_inferior_event_handler,
1002 NULL);
1004 record_init_record_breakpoints ();
1006 observer_notify_record_changed (current_inferior (), 1);
1009 /* "to_close" target method. Close the process record target. */
1011 static void
1012 record_close (int quitting)
1014 struct record_core_buf_entry *entry;
1016 if (record_debug)
1017 fprintf_unfiltered (gdb_stdlog, "Process record: record_close\n");
1019 record_list_release (record_list);
1021 /* Release record_core_regbuf. */
1022 if (record_core_regbuf)
1024 xfree (record_core_regbuf);
1025 record_core_regbuf = NULL;
1028 /* Release record_core_buf_list. */
1029 if (record_core_buf_list)
1031 for (entry = record_core_buf_list->prev; entry; entry = entry->prev)
1033 xfree (record_core_buf_list);
1034 record_core_buf_list = entry;
1036 record_core_buf_list = NULL;
1039 if (record_async_inferior_event_token)
1040 delete_async_event_handler (&record_async_inferior_event_token);
1043 static int record_resume_step = 0;
1045 /* True if we've been resumed, and so each record_wait call should
1046 advance execution. If this is false, record_wait will return a
1047 TARGET_WAITKIND_IGNORE. */
1048 static int record_resumed = 0;
1050 /* The execution direction of the last resume we got. This is
1051 necessary for async mode. Vis (order is not strictly accurate):
1053 1. user has the global execution direction set to forward
1054 2. user does a reverse-step command
1055 3. record_resume is called with global execution direction
1056 temporarily switched to reverse
1057 4. GDB's execution direction is reverted back to forward
1058 5. target record notifies event loop there's an event to handle
1059 6. infrun asks the target which direction was it going, and switches
1060 the global execution direction accordingly (to reverse)
1061 7. infrun polls an event out of the record target, and handles it
1062 8. GDB goes back to the event loop, and goto #4.
1064 static enum exec_direction_kind record_execution_dir = EXEC_FORWARD;
1066 /* "to_resume" target method. Resume the process record target. */
1068 static void
1069 record_resume (struct target_ops *ops, ptid_t ptid, int step,
1070 enum gdb_signal signal)
1072 record_resume_step = step;
1073 record_resumed = 1;
1074 record_execution_dir = execution_direction;
1076 if (!RECORD_IS_REPLAY)
1078 struct gdbarch *gdbarch = target_thread_architecture (ptid);
1080 record_message (get_current_regcache (), signal);
1082 if (!step)
1084 /* This is not hard single step. */
1085 if (!gdbarch_software_single_step_p (gdbarch))
1087 /* This is a normal continue. */
1088 step = 1;
1090 else
1092 /* This arch support soft sigle step. */
1093 if (single_step_breakpoints_inserted ())
1095 /* This is a soft single step. */
1096 record_resume_step = 1;
1098 else
1100 /* This is a continue.
1101 Try to insert a soft single step breakpoint. */
1102 if (!gdbarch_software_single_step (gdbarch,
1103 get_current_frame ()))
1105 /* This system don't want use soft single step.
1106 Use hard sigle step. */
1107 step = 1;
1113 /* Make sure the target beneath reports all signals. */
1114 target_pass_signals (0, NULL);
1116 record_beneath_to_resume (record_beneath_to_resume_ops,
1117 ptid, step, signal);
1120 /* We are about to start executing the inferior (or simulate it),
1121 let's register it with the event loop. */
1122 if (target_can_async_p ())
1124 target_async (inferior_event_handler, 0);
1125 /* Notify the event loop there's an event to wait for. We do
1126 most of the work in record_wait. */
1127 mark_async_event_handler (record_async_inferior_event_token);
1131 static int record_get_sig = 0;
1133 /* SIGINT signal handler, registered by "to_wait" method. */
1135 static void
1136 record_sig_handler (int signo)
1138 if (record_debug)
1139 fprintf_unfiltered (gdb_stdlog, "Process record: get a signal\n");
1141 /* It will break the running inferior in replay mode. */
1142 record_resume_step = 1;
1144 /* It will let record_wait set inferior status to get the signal
1145 SIGINT. */
1146 record_get_sig = 1;
1149 static void
1150 record_wait_cleanups (void *ignore)
1152 if (execution_direction == EXEC_REVERSE)
1154 if (record_list->next)
1155 record_list = record_list->next;
1157 else
1158 record_list = record_list->prev;
1161 /* "to_wait" target method for process record target.
1163 In record mode, the target is always run in singlestep mode
1164 (even when gdb says to continue). The to_wait method intercepts
1165 the stop events and determines which ones are to be passed on to
1166 gdb. Most stop events are just singlestep events that gdb is not
1167 to know about, so the to_wait method just records them and keeps
1168 singlestepping.
1170 In replay mode, this function emulates the recorded execution log,
1171 one instruction at a time (forward or backward), and determines
1172 where to stop. */
1174 static ptid_t
1175 record_wait_1 (struct target_ops *ops,
1176 ptid_t ptid, struct target_waitstatus *status,
1177 int options)
1179 struct cleanup *set_cleanups = record_gdb_operation_disable_set ();
1181 if (record_debug)
1182 fprintf_unfiltered (gdb_stdlog,
1183 "Process record: record_wait "
1184 "record_resume_step = %d, record_resumed = %d, direction=%s\n",
1185 record_resume_step, record_resumed,
1186 record_execution_dir == EXEC_FORWARD ? "forward" : "reverse");
1188 if (!record_resumed)
1190 gdb_assert ((options & TARGET_WNOHANG) != 0);
1192 /* No interesting event. */
1193 status->kind = TARGET_WAITKIND_IGNORE;
1194 return minus_one_ptid;
1197 record_get_sig = 0;
1198 signal (SIGINT, record_sig_handler);
1200 if (!RECORD_IS_REPLAY && ops != &record_core_ops)
1202 if (record_resume_step)
1204 /* This is a single step. */
1205 return record_beneath_to_wait (record_beneath_to_wait_ops,
1206 ptid, status, options);
1208 else
1210 /* This is not a single step. */
1211 ptid_t ret;
1212 CORE_ADDR tmp_pc;
1213 struct gdbarch *gdbarch = target_thread_architecture (inferior_ptid);
1215 while (1)
1217 ret = record_beneath_to_wait (record_beneath_to_wait_ops,
1218 ptid, status, options);
1219 if (status->kind == TARGET_WAITKIND_IGNORE)
1221 if (record_debug)
1222 fprintf_unfiltered (gdb_stdlog,
1223 "Process record: record_wait "
1224 "target beneath not done yet\n");
1225 return ret;
1228 if (single_step_breakpoints_inserted ())
1229 remove_single_step_breakpoints ();
1231 if (record_resume_step)
1232 return ret;
1234 /* Is this a SIGTRAP? */
1235 if (status->kind == TARGET_WAITKIND_STOPPED
1236 && status->value.sig == GDB_SIGNAL_TRAP)
1238 struct regcache *regcache;
1239 struct address_space *aspace;
1241 /* Yes -- this is likely our single-step finishing,
1242 but check if there's any reason the core would be
1243 interested in the event. */
1245 registers_changed ();
1246 regcache = get_current_regcache ();
1247 tmp_pc = regcache_read_pc (regcache);
1248 aspace = get_regcache_aspace (regcache);
1250 if (target_stopped_by_watchpoint ())
1252 /* Always interested in watchpoints. */
1254 else if (breakpoint_inserted_here_p (aspace, tmp_pc))
1256 /* There is a breakpoint here. Let the core
1257 handle it. */
1258 if (software_breakpoint_inserted_here_p (aspace, tmp_pc))
1260 struct gdbarch *gdbarch
1261 = get_regcache_arch (regcache);
1262 CORE_ADDR decr_pc_after_break
1263 = gdbarch_decr_pc_after_break (gdbarch);
1264 if (decr_pc_after_break)
1265 regcache_write_pc (regcache,
1266 tmp_pc + decr_pc_after_break);
1269 else
1271 /* This is a single-step trap. Record the
1272 insn and issue another step.
1273 FIXME: this part can be a random SIGTRAP too.
1274 But GDB cannot handle it. */
1275 int step = 1;
1277 if (!record_message_wrapper_safe (regcache,
1278 GDB_SIGNAL_0))
1280 status->kind = TARGET_WAITKIND_STOPPED;
1281 status->value.sig = GDB_SIGNAL_0;
1282 break;
1285 if (gdbarch_software_single_step_p (gdbarch))
1287 /* Try to insert the software single step breakpoint.
1288 If insert success, set step to 0. */
1289 set_executing (inferior_ptid, 0);
1290 reinit_frame_cache ();
1291 if (gdbarch_software_single_step (gdbarch,
1292 get_current_frame ()))
1293 step = 0;
1294 set_executing (inferior_ptid, 1);
1297 if (record_debug)
1298 fprintf_unfiltered (gdb_stdlog,
1299 "Process record: record_wait "
1300 "issuing one more step in the target beneath\n");
1301 record_beneath_to_resume (record_beneath_to_resume_ops,
1302 ptid, step,
1303 GDB_SIGNAL_0);
1304 continue;
1308 /* The inferior is broken by a breakpoint or a signal. */
1309 break;
1312 return ret;
1315 else
1317 struct regcache *regcache = get_current_regcache ();
1318 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1319 struct address_space *aspace = get_regcache_aspace (regcache);
1320 int continue_flag = 1;
1321 int first_record_end = 1;
1322 struct cleanup *old_cleanups = make_cleanup (record_wait_cleanups, 0);
1323 CORE_ADDR tmp_pc;
1325 record_hw_watchpoint = 0;
1326 status->kind = TARGET_WAITKIND_STOPPED;
1328 /* Check breakpoint when forward execute. */
1329 if (execution_direction == EXEC_FORWARD)
1331 tmp_pc = regcache_read_pc (regcache);
1332 if (breakpoint_inserted_here_p (aspace, tmp_pc))
1334 int decr_pc_after_break = gdbarch_decr_pc_after_break (gdbarch);
1336 if (record_debug)
1337 fprintf_unfiltered (gdb_stdlog,
1338 "Process record: break at %s.\n",
1339 paddress (gdbarch, tmp_pc));
1341 if (decr_pc_after_break
1342 && !record_resume_step
1343 && software_breakpoint_inserted_here_p (aspace, tmp_pc))
1344 regcache_write_pc (regcache,
1345 tmp_pc + decr_pc_after_break);
1346 goto replay_out;
1350 /* If GDB is in terminal_inferior mode, it will not get the signal.
1351 And in GDB replay mode, GDB doesn't need to be in terminal_inferior
1352 mode, because inferior will not executed.
1353 Then set it to terminal_ours to make GDB get the signal. */
1354 target_terminal_ours ();
1356 /* In EXEC_FORWARD mode, record_list points to the tail of prev
1357 instruction. */
1358 if (execution_direction == EXEC_FORWARD && record_list->next)
1359 record_list = record_list->next;
1361 /* Loop over the record_list, looking for the next place to
1362 stop. */
1365 /* Check for beginning and end of log. */
1366 if (execution_direction == EXEC_REVERSE
1367 && record_list == &record_first)
1369 /* Hit beginning of record log in reverse. */
1370 status->kind = TARGET_WAITKIND_NO_HISTORY;
1371 break;
1373 if (execution_direction != EXEC_REVERSE && !record_list->next)
1375 /* Hit end of record log going forward. */
1376 status->kind = TARGET_WAITKIND_NO_HISTORY;
1377 break;
1380 record_exec_insn (regcache, gdbarch, record_list);
1382 if (record_list->type == record_end)
1384 if (record_debug > 1)
1385 fprintf_unfiltered (gdb_stdlog,
1386 "Process record: record_end %s to "
1387 "inferior.\n",
1388 host_address_to_string (record_list));
1390 if (first_record_end && execution_direction == EXEC_REVERSE)
1392 /* When reverse excute, the first record_end is the part of
1393 current instruction. */
1394 first_record_end = 0;
1396 else
1398 /* In EXEC_REVERSE mode, this is the record_end of prev
1399 instruction.
1400 In EXEC_FORWARD mode, this is the record_end of current
1401 instruction. */
1402 /* step */
1403 if (record_resume_step)
1405 if (record_debug > 1)
1406 fprintf_unfiltered (gdb_stdlog,
1407 "Process record: step.\n");
1408 continue_flag = 0;
1411 /* check breakpoint */
1412 tmp_pc = regcache_read_pc (regcache);
1413 if (breakpoint_inserted_here_p (aspace, tmp_pc))
1415 int decr_pc_after_break
1416 = gdbarch_decr_pc_after_break (gdbarch);
1418 if (record_debug)
1419 fprintf_unfiltered (gdb_stdlog,
1420 "Process record: break "
1421 "at %s.\n",
1422 paddress (gdbarch, tmp_pc));
1423 if (decr_pc_after_break
1424 && execution_direction == EXEC_FORWARD
1425 && !record_resume_step
1426 && software_breakpoint_inserted_here_p (aspace,
1427 tmp_pc))
1428 regcache_write_pc (regcache,
1429 tmp_pc + decr_pc_after_break);
1430 continue_flag = 0;
1433 if (record_hw_watchpoint)
1435 if (record_debug)
1436 fprintf_unfiltered (gdb_stdlog,
1437 "Process record: hit hw "
1438 "watchpoint.\n");
1439 continue_flag = 0;
1441 /* Check target signal */
1442 if (record_list->u.end.sigval != GDB_SIGNAL_0)
1443 /* FIXME: better way to check */
1444 continue_flag = 0;
1448 if (continue_flag)
1450 if (execution_direction == EXEC_REVERSE)
1452 if (record_list->prev)
1453 record_list = record_list->prev;
1455 else
1457 if (record_list->next)
1458 record_list = record_list->next;
1462 while (continue_flag);
1464 replay_out:
1465 if (record_get_sig)
1466 status->value.sig = GDB_SIGNAL_INT;
1467 else if (record_list->u.end.sigval != GDB_SIGNAL_0)
1468 /* FIXME: better way to check */
1469 status->value.sig = record_list->u.end.sigval;
1470 else
1471 status->value.sig = GDB_SIGNAL_TRAP;
1473 discard_cleanups (old_cleanups);
1476 signal (SIGINT, handle_sigint);
1478 do_cleanups (set_cleanups);
1479 return inferior_ptid;
1482 static ptid_t
1483 record_wait (struct target_ops *ops,
1484 ptid_t ptid, struct target_waitstatus *status,
1485 int options)
1487 ptid_t return_ptid;
1489 return_ptid = record_wait_1 (ops, ptid, status, options);
1490 if (status->kind != TARGET_WAITKIND_IGNORE)
1492 /* We're reporting a stop. Make sure any spurious
1493 target_wait(WNOHANG) doesn't advance the target until the
1494 core wants us resumed again. */
1495 record_resumed = 0;
1497 return return_ptid;
1500 static int
1501 record_stopped_by_watchpoint (void)
1503 if (RECORD_IS_REPLAY)
1504 return record_hw_watchpoint;
1505 else
1506 return record_beneath_to_stopped_by_watchpoint ();
1509 static int
1510 record_stopped_data_address (struct target_ops *ops, CORE_ADDR *addr_p)
1512 if (RECORD_IS_REPLAY)
1513 return 0;
1514 else
1515 return record_beneath_to_stopped_data_address (ops, addr_p);
1518 /* "to_disconnect" method for process record target. */
1520 static void
1521 record_disconnect (struct target_ops *target, char *args, int from_tty)
1523 if (record_debug)
1524 fprintf_unfiltered (gdb_stdlog, "Process record: record_disconnect\n");
1526 unpush_target (&record_ops);
1527 target_disconnect (args, from_tty);
1530 /* "to_detach" method for process record target. */
1532 static void
1533 record_detach (struct target_ops *ops, char *args, int from_tty)
1535 if (record_debug)
1536 fprintf_unfiltered (gdb_stdlog, "Process record: record_detach\n");
1538 unpush_target (&record_ops);
1539 target_detach (args, from_tty);
1542 /* "to_mourn_inferior" method for process record target. */
1544 static void
1545 record_mourn_inferior (struct target_ops *ops)
1547 if (record_debug)
1548 fprintf_unfiltered (gdb_stdlog, "Process record: "
1549 "record_mourn_inferior\n");
1551 unpush_target (&record_ops);
1552 target_mourn_inferior ();
1555 /* Close process record target before killing the inferior process. */
1557 static void
1558 record_kill (struct target_ops *ops)
1560 if (record_debug)
1561 fprintf_unfiltered (gdb_stdlog, "Process record: record_kill\n");
1563 unpush_target (&record_ops);
1564 target_kill ();
1567 /* Record registers change (by user or by GDB) to list as an instruction. */
1569 static void
1570 record_registers_change (struct regcache *regcache, int regnum)
1572 /* Check record_insn_num. */
1573 record_check_insn_num (0);
1575 record_arch_list_head = NULL;
1576 record_arch_list_tail = NULL;
1578 if (regnum < 0)
1580 int i;
1582 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
1584 if (record_arch_list_add_reg (regcache, i))
1586 record_list_release (record_arch_list_tail);
1587 error (_("Process record: failed to record execution log."));
1591 else
1593 if (record_arch_list_add_reg (regcache, regnum))
1595 record_list_release (record_arch_list_tail);
1596 error (_("Process record: failed to record execution log."));
1599 if (record_arch_list_add_end ())
1601 record_list_release (record_arch_list_tail);
1602 error (_("Process record: failed to record execution log."));
1604 record_list->next = record_arch_list_head;
1605 record_arch_list_head->prev = record_list;
1606 record_list = record_arch_list_tail;
1608 if (record_insn_num == record_insn_max_num && record_insn_max_num)
1609 record_list_release_first ();
1610 else
1611 record_insn_num++;
1614 /* "to_store_registers" method for process record target. */
1616 static void
1617 record_store_registers (struct target_ops *ops, struct regcache *regcache,
1618 int regno)
1620 if (!record_gdb_operation_disable)
1622 if (RECORD_IS_REPLAY)
1624 int n;
1626 /* Let user choose if he wants to write register or not. */
1627 if (regno < 0)
1629 query (_("Because GDB is in replay mode, changing the "
1630 "value of a register will make the execution "
1631 "log unusable from this point onward. "
1632 "Change all registers?"));
1633 else
1635 query (_("Because GDB is in replay mode, changing the value "
1636 "of a register will make the execution log unusable "
1637 "from this point onward. Change register %s?"),
1638 gdbarch_register_name (get_regcache_arch (regcache),
1639 regno));
1641 if (!n)
1643 /* Invalidate the value of regcache that was set in function
1644 "regcache_raw_write". */
1645 if (regno < 0)
1647 int i;
1649 for (i = 0;
1650 i < gdbarch_num_regs (get_regcache_arch (regcache));
1651 i++)
1652 regcache_invalidate (regcache, i);
1654 else
1655 regcache_invalidate (regcache, regno);
1657 error (_("Process record canceled the operation."));
1660 /* Destroy the record from here forward. */
1661 record_list_release_following (record_list);
1664 record_registers_change (regcache, regno);
1666 record_beneath_to_store_registers (record_beneath_to_store_registers_ops,
1667 regcache, regno);
1670 /* "to_xfer_partial" method. Behavior is conditional on RECORD_IS_REPLAY.
1671 In replay mode, we cannot write memory unles we are willing to
1672 invalidate the record/replay log from this point forward. */
1674 static LONGEST
1675 record_xfer_partial (struct target_ops *ops, enum target_object object,
1676 const char *annex, gdb_byte *readbuf,
1677 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
1679 if (!record_gdb_operation_disable
1680 && (object == TARGET_OBJECT_MEMORY
1681 || object == TARGET_OBJECT_RAW_MEMORY) && writebuf)
1683 if (RECORD_IS_REPLAY)
1685 /* Let user choose if he wants to write memory or not. */
1686 if (!query (_("Because GDB is in replay mode, writing to memory "
1687 "will make the execution log unusable from this "
1688 "point onward. Write memory at address %s?"),
1689 paddress (target_gdbarch, offset)))
1690 error (_("Process record canceled the operation."));
1692 /* Destroy the record from here forward. */
1693 record_list_release_following (record_list);
1696 /* Check record_insn_num */
1697 record_check_insn_num (0);
1699 /* Record registers change to list as an instruction. */
1700 record_arch_list_head = NULL;
1701 record_arch_list_tail = NULL;
1702 if (record_arch_list_add_mem (offset, len))
1704 record_list_release (record_arch_list_tail);
1705 if (record_debug)
1706 fprintf_unfiltered (gdb_stdlog,
1707 "Process record: failed to record "
1708 "execution log.");
1709 return -1;
1711 if (record_arch_list_add_end ())
1713 record_list_release (record_arch_list_tail);
1714 if (record_debug)
1715 fprintf_unfiltered (gdb_stdlog,
1716 "Process record: failed to record "
1717 "execution log.");
1718 return -1;
1720 record_list->next = record_arch_list_head;
1721 record_arch_list_head->prev = record_list;
1722 record_list = record_arch_list_tail;
1724 if (record_insn_num == record_insn_max_num && record_insn_max_num)
1725 record_list_release_first ();
1726 else
1727 record_insn_num++;
1730 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops,
1731 object, annex, readbuf, writebuf,
1732 offset, len);
1735 /* This structure represents a breakpoint inserted while the record
1736 target is active. We use this to know when to install/remove
1737 breakpoints in/from the target beneath. For example, a breakpoint
1738 may be inserted while recording, but removed when not replaying nor
1739 recording. In that case, the breakpoint had not been inserted on
1740 the target beneath, so we should not try to remove it there. */
1742 struct record_breakpoint
1744 /* The address and address space the breakpoint was set at. */
1745 struct address_space *address_space;
1746 CORE_ADDR addr;
1748 /* True when the breakpoint has been also installed in the target
1749 beneath. This will be false for breakpoints set during replay or
1750 when recording. */
1751 int in_target_beneath;
1754 typedef struct record_breakpoint *record_breakpoint_p;
1755 DEF_VEC_P(record_breakpoint_p);
1757 /* The list of breakpoints inserted while the record target is
1758 active. */
1759 VEC(record_breakpoint_p) *record_breakpoints = NULL;
1761 static void
1762 record_sync_record_breakpoints (struct bp_location *loc, void *data)
1764 if (loc->loc_type != bp_loc_software_breakpoint)
1765 return;
1767 if (loc->inserted)
1769 struct record_breakpoint *bp = XNEW (struct record_breakpoint);
1771 bp->addr = loc->target_info.placed_address;
1772 bp->address_space = loc->target_info.placed_address_space;
1774 bp->in_target_beneath = 1;
1776 VEC_safe_push (record_breakpoint_p, record_breakpoints, bp);
1780 /* Sync existing breakpoints to record_breakpoints. */
1782 static void
1783 record_init_record_breakpoints (void)
1785 VEC_free (record_breakpoint_p, record_breakpoints);
1787 iterate_over_bp_locations (record_sync_record_breakpoints);
1790 /* Behavior is conditional on RECORD_IS_REPLAY. We will not actually
1791 insert or remove breakpoints in the real target when replaying, nor
1792 when recording. */
1794 static int
1795 record_insert_breakpoint (struct gdbarch *gdbarch,
1796 struct bp_target_info *bp_tgt)
1798 struct record_breakpoint *bp;
1799 int in_target_beneath = 0;
1801 if (!RECORD_IS_REPLAY)
1803 /* When recording, we currently always single-step, so we don't
1804 really need to install regular breakpoints in the inferior.
1805 However, we do have to insert software single-step
1806 breakpoints, in case the target can't hardware step. To keep
1807 things single, we always insert. */
1808 struct cleanup *old_cleanups;
1809 int ret;
1811 old_cleanups = record_gdb_operation_disable_set ();
1812 ret = record_beneath_to_insert_breakpoint (gdbarch, bp_tgt);
1813 do_cleanups (old_cleanups);
1815 if (ret != 0)
1816 return ret;
1818 in_target_beneath = 1;
1821 bp = XNEW (struct record_breakpoint);
1822 bp->addr = bp_tgt->placed_address;
1823 bp->address_space = bp_tgt->placed_address_space;
1824 bp->in_target_beneath = in_target_beneath;
1825 VEC_safe_push (record_breakpoint_p, record_breakpoints, bp);
1826 return 0;
1829 /* "to_remove_breakpoint" method for process record target. */
1831 static int
1832 record_remove_breakpoint (struct gdbarch *gdbarch,
1833 struct bp_target_info *bp_tgt)
1835 struct record_breakpoint *bp;
1836 int ix;
1838 for (ix = 0;
1839 VEC_iterate (record_breakpoint_p, record_breakpoints, ix, bp);
1840 ++ix)
1842 if (bp->addr == bp_tgt->placed_address
1843 && bp->address_space == bp_tgt->placed_address_space)
1845 if (bp->in_target_beneath)
1847 struct cleanup *old_cleanups;
1848 int ret;
1850 old_cleanups = record_gdb_operation_disable_set ();
1851 ret = record_beneath_to_remove_breakpoint (gdbarch, bp_tgt);
1852 do_cleanups (old_cleanups);
1854 if (ret != 0)
1855 return ret;
1858 VEC_unordered_remove (record_breakpoint_p, record_breakpoints, ix);
1859 return 0;
1863 gdb_assert_not_reached ("removing unknown breakpoint");
1866 /* "to_can_execute_reverse" method for process record target. */
1868 static int
1869 record_can_execute_reverse (void)
1871 return 1;
1874 /* "to_get_bookmark" method for process record and prec over core. */
1876 static gdb_byte *
1877 record_get_bookmark (char *args, int from_tty)
1879 gdb_byte *ret = NULL;
1881 /* Return stringified form of instruction count. */
1882 if (record_list && record_list->type == record_end)
1883 ret = xstrdup (pulongest (record_list->u.end.insn_num));
1885 if (record_debug)
1887 if (ret)
1888 fprintf_unfiltered (gdb_stdlog,
1889 "record_get_bookmark returns %s\n", ret);
1890 else
1891 fprintf_unfiltered (gdb_stdlog,
1892 "record_get_bookmark returns NULL\n");
1894 return ret;
1897 /* The implementation of the command "record goto". */
1898 static void cmd_record_goto (char *, int);
1900 /* "to_goto_bookmark" method for process record and prec over core. */
1902 static void
1903 record_goto_bookmark (gdb_byte *bookmark, int from_tty)
1905 if (record_debug)
1906 fprintf_unfiltered (gdb_stdlog,
1907 "record_goto_bookmark receives %s\n", bookmark);
1909 if (bookmark[0] == '\'' || bookmark[0] == '\"')
1911 if (bookmark[strlen (bookmark) - 1] != bookmark[0])
1912 error (_("Unbalanced quotes: %s"), bookmark);
1914 /* Strip trailing quote. */
1915 bookmark[strlen (bookmark) - 1] = '\0';
1916 /* Strip leading quote. */
1917 bookmark++;
1918 /* Pass along to cmd_record_goto. */
1921 cmd_record_goto ((char *) bookmark, from_tty);
1922 return;
1925 static void
1926 record_async (void (*callback) (enum inferior_event_type event_type,
1927 void *context), void *context)
1929 /* If we're on top of a line target (e.g., linux-nat, remote), then
1930 set it to async mode as well. Will be NULL if we're sitting on
1931 top of the core target, for "record restore". */
1932 if (record_beneath_to_async != NULL)
1933 record_beneath_to_async (callback, context);
1936 static int
1937 record_can_async_p (void)
1939 /* We only enable async when the user specifically asks for it. */
1940 return target_async_permitted;
1943 static int
1944 record_is_async_p (void)
1946 /* We only enable async when the user specifically asks for it. */
1947 return target_async_permitted;
1950 static enum exec_direction_kind
1951 record_execution_direction (void)
1953 return record_execution_dir;
1956 static void
1957 init_record_ops (void)
1959 record_ops.to_shortname = "record";
1960 record_ops.to_longname = "Process record and replay target";
1961 record_ops.to_doc =
1962 "Log program while executing and replay execution from log.";
1963 record_ops.to_open = record_open;
1964 record_ops.to_close = record_close;
1965 record_ops.to_resume = record_resume;
1966 record_ops.to_wait = record_wait;
1967 record_ops.to_disconnect = record_disconnect;
1968 record_ops.to_detach = record_detach;
1969 record_ops.to_mourn_inferior = record_mourn_inferior;
1970 record_ops.to_kill = record_kill;
1971 record_ops.to_create_inferior = find_default_create_inferior;
1972 record_ops.to_store_registers = record_store_registers;
1973 record_ops.to_xfer_partial = record_xfer_partial;
1974 record_ops.to_insert_breakpoint = record_insert_breakpoint;
1975 record_ops.to_remove_breakpoint = record_remove_breakpoint;
1976 record_ops.to_stopped_by_watchpoint = record_stopped_by_watchpoint;
1977 record_ops.to_stopped_data_address = record_stopped_data_address;
1978 record_ops.to_can_execute_reverse = record_can_execute_reverse;
1979 record_ops.to_stratum = record_stratum;
1980 /* Add bookmark target methods. */
1981 record_ops.to_get_bookmark = record_get_bookmark;
1982 record_ops.to_goto_bookmark = record_goto_bookmark;
1983 record_ops.to_async = record_async;
1984 record_ops.to_can_async_p = record_can_async_p;
1985 record_ops.to_is_async_p = record_is_async_p;
1986 record_ops.to_execution_direction = record_execution_direction;
1987 record_ops.to_magic = OPS_MAGIC;
1990 /* "to_resume" method for prec over corefile. */
1992 static void
1993 record_core_resume (struct target_ops *ops, ptid_t ptid, int step,
1994 enum gdb_signal signal)
1996 record_resume_step = step;
1997 record_resumed = 1;
1998 record_execution_dir = execution_direction;
2000 /* We are about to start executing the inferior (or simulate it),
2001 let's register it with the event loop. */
2002 if (target_can_async_p ())
2004 target_async (inferior_event_handler, 0);
2006 /* Notify the event loop there's an event to wait for. */
2007 mark_async_event_handler (record_async_inferior_event_token);
2011 /* "to_kill" method for prec over corefile. */
2013 static void
2014 record_core_kill (struct target_ops *ops)
2016 if (record_debug)
2017 fprintf_unfiltered (gdb_stdlog, "Process record: record_core_kill\n");
2019 unpush_target (&record_core_ops);
2022 /* "to_fetch_registers" method for prec over corefile. */
2024 static void
2025 record_core_fetch_registers (struct target_ops *ops,
2026 struct regcache *regcache,
2027 int regno)
2029 if (regno < 0)
2031 int num = gdbarch_num_regs (get_regcache_arch (regcache));
2032 int i;
2034 for (i = 0; i < num; i ++)
2035 regcache_raw_supply (regcache, i,
2036 record_core_regbuf + MAX_REGISTER_SIZE * i);
2038 else
2039 regcache_raw_supply (regcache, regno,
2040 record_core_regbuf + MAX_REGISTER_SIZE * regno);
2043 /* "to_prepare_to_store" method for prec over corefile. */
2045 static void
2046 record_core_prepare_to_store (struct regcache *regcache)
2050 /* "to_store_registers" method for prec over corefile. */
2052 static void
2053 record_core_store_registers (struct target_ops *ops,
2054 struct regcache *regcache,
2055 int regno)
2057 if (record_gdb_operation_disable)
2058 regcache_raw_collect (regcache, regno,
2059 record_core_regbuf + MAX_REGISTER_SIZE * regno);
2060 else
2061 error (_("You can't do that without a process to debug."));
2064 /* "to_xfer_partial" method for prec over corefile. */
2066 static LONGEST
2067 record_core_xfer_partial (struct target_ops *ops, enum target_object object,
2068 const char *annex, gdb_byte *readbuf,
2069 const gdb_byte *writebuf, ULONGEST offset,
2070 LONGEST len)
2072 if (object == TARGET_OBJECT_MEMORY)
2074 if (record_gdb_operation_disable || !writebuf)
2076 struct target_section *p;
2078 for (p = record_core_start; p < record_core_end; p++)
2080 if (offset >= p->addr)
2082 struct record_core_buf_entry *entry;
2083 ULONGEST sec_offset;
2085 if (offset >= p->endaddr)
2086 continue;
2088 if (offset + len > p->endaddr)
2089 len = p->endaddr - offset;
2091 sec_offset = offset - p->addr;
2093 /* Read readbuf or write writebuf p, offset, len. */
2094 /* Check flags. */
2095 if (p->the_bfd_section->flags & SEC_CONSTRUCTOR
2096 || (p->the_bfd_section->flags & SEC_HAS_CONTENTS) == 0)
2098 if (readbuf)
2099 memset (readbuf, 0, len);
2100 return len;
2102 /* Get record_core_buf_entry. */
2103 for (entry = record_core_buf_list; entry;
2104 entry = entry->prev)
2105 if (entry->p == p)
2106 break;
2107 if (writebuf)
2109 if (!entry)
2111 /* Add a new entry. */
2112 entry = (struct record_core_buf_entry *)
2113 xmalloc (sizeof (struct record_core_buf_entry));
2114 entry->p = p;
2115 if (!bfd_malloc_and_get_section (p->bfd,
2116 p->the_bfd_section,
2117 &entry->buf))
2119 xfree (entry);
2120 return 0;
2122 entry->prev = record_core_buf_list;
2123 record_core_buf_list = entry;
2126 memcpy (entry->buf + sec_offset, writebuf,
2127 (size_t) len);
2129 else
2131 if (!entry)
2132 return record_beneath_to_xfer_partial
2133 (record_beneath_to_xfer_partial_ops,
2134 object, annex, readbuf, writebuf,
2135 offset, len);
2137 memcpy (readbuf, entry->buf + sec_offset,
2138 (size_t) len);
2141 return len;
2145 return -1;
2147 else
2148 error (_("You can't do that without a process to debug."));
2151 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops,
2152 object, annex, readbuf, writebuf,
2153 offset, len);
2156 /* "to_insert_breakpoint" method for prec over corefile. */
2158 static int
2159 record_core_insert_breakpoint (struct gdbarch *gdbarch,
2160 struct bp_target_info *bp_tgt)
2162 return 0;
2165 /* "to_remove_breakpoint" method for prec over corefile. */
2167 static int
2168 record_core_remove_breakpoint (struct gdbarch *gdbarch,
2169 struct bp_target_info *bp_tgt)
2171 return 0;
2174 /* "to_has_execution" method for prec over corefile. */
2176 static int
2177 record_core_has_execution (struct target_ops *ops, ptid_t the_ptid)
2179 return 1;
2182 static void
2183 init_record_core_ops (void)
2185 record_core_ops.to_shortname = "record-core";
2186 record_core_ops.to_longname = "Process record and replay target";
2187 record_core_ops.to_doc =
2188 "Log program while executing and replay execution from log.";
2189 record_core_ops.to_open = record_open;
2190 record_core_ops.to_close = record_close;
2191 record_core_ops.to_resume = record_core_resume;
2192 record_core_ops.to_wait = record_wait;
2193 record_core_ops.to_kill = record_core_kill;
2194 record_core_ops.to_fetch_registers = record_core_fetch_registers;
2195 record_core_ops.to_prepare_to_store = record_core_prepare_to_store;
2196 record_core_ops.to_store_registers = record_core_store_registers;
2197 record_core_ops.to_xfer_partial = record_core_xfer_partial;
2198 record_core_ops.to_insert_breakpoint = record_core_insert_breakpoint;
2199 record_core_ops.to_remove_breakpoint = record_core_remove_breakpoint;
2200 record_core_ops.to_stopped_by_watchpoint = record_stopped_by_watchpoint;
2201 record_core_ops.to_stopped_data_address = record_stopped_data_address;
2202 record_core_ops.to_can_execute_reverse = record_can_execute_reverse;
2203 record_core_ops.to_has_execution = record_core_has_execution;
2204 record_core_ops.to_stratum = record_stratum;
2205 /* Add bookmark target methods. */
2206 record_core_ops.to_get_bookmark = record_get_bookmark;
2207 record_core_ops.to_goto_bookmark = record_goto_bookmark;
2208 record_core_ops.to_async = record_async;
2209 record_core_ops.to_can_async_p = record_can_async_p;
2210 record_core_ops.to_is_async_p = record_is_async_p;
2211 record_core_ops.to_execution_direction = record_execution_direction;
2212 record_core_ops.to_magic = OPS_MAGIC;
2215 /* Implement "show record debug" command. */
2217 static void
2218 show_record_debug (struct ui_file *file, int from_tty,
2219 struct cmd_list_element *c, const char *value)
2221 fprintf_filtered (file, _("Debugging of process record target is %s.\n"),
2222 value);
2225 /* Alias for "target record". */
2227 static void
2228 cmd_record_start (char *args, int from_tty)
2230 execute_command ("target record", from_tty);
2233 /* Truncate the record log from the present point
2234 of replay until the end. */
2236 static void
2237 cmd_record_delete (char *args, int from_tty)
2239 if (current_target.to_stratum == record_stratum)
2241 if (RECORD_IS_REPLAY)
2243 if (!from_tty || query (_("Delete the log from this point forward "
2244 "and begin to record the running message "
2245 "at current PC?")))
2246 record_list_release_following (record_list);
2248 else
2249 printf_unfiltered (_("Already at end of record list.\n"));
2252 else
2253 printf_unfiltered (_("Process record is not started.\n"));
2256 /* Implement the "stoprecord" or "record stop" command. */
2258 static void
2259 cmd_record_stop (char *args, int from_tty)
2261 if (current_target.to_stratum == record_stratum)
2263 unpush_target (&record_ops);
2264 printf_unfiltered (_("Process record is stopped and all execution "
2265 "logs are deleted.\n"));
2267 observer_notify_record_changed (current_inferior (), 0);
2269 else
2270 printf_unfiltered (_("Process record is not started.\n"));
2273 /* Set upper limit of record log size. */
2275 static void
2276 set_record_insn_max_num (char *args, int from_tty, struct cmd_list_element *c)
2278 if (record_insn_num > record_insn_max_num && record_insn_max_num)
2280 /* Count down record_insn_num while releasing records from list. */
2281 while (record_insn_num > record_insn_max_num)
2283 record_list_release_first ();
2284 record_insn_num--;
2289 static struct cmd_list_element *record_cmdlist, *set_record_cmdlist,
2290 *show_record_cmdlist, *info_record_cmdlist;
2292 static void
2293 set_record_command (char *args, int from_tty)
2295 printf_unfiltered (_("\"set record\" must be followed "
2296 "by an apporpriate subcommand.\n"));
2297 help_list (set_record_cmdlist, "set record ", all_commands, gdb_stdout);
2300 static void
2301 show_record_command (char *args, int from_tty)
2303 cmd_show_list (show_record_cmdlist, from_tty, "");
2306 /* Display some statistics about the execution log. */
2308 static void
2309 info_record_command (char *args, int from_tty)
2311 struct record_entry *p;
2313 if (current_target.to_stratum == record_stratum)
2315 if (RECORD_IS_REPLAY)
2316 printf_filtered (_("Replay mode:\n"));
2317 else
2318 printf_filtered (_("Record mode:\n"));
2320 /* Find entry for first actual instruction in the log. */
2321 for (p = record_first.next;
2322 p != NULL && p->type != record_end;
2323 p = p->next)
2326 /* Do we have a log at all? */
2327 if (p != NULL && p->type == record_end)
2329 /* Display instruction number for first instruction in the log. */
2330 printf_filtered (_("Lowest recorded instruction number is %s.\n"),
2331 pulongest (p->u.end.insn_num));
2333 /* If in replay mode, display where we are in the log. */
2334 if (RECORD_IS_REPLAY)
2335 printf_filtered (_("Current instruction number is %s.\n"),
2336 pulongest (record_list->u.end.insn_num));
2338 /* Display instruction number for last instruction in the log. */
2339 printf_filtered (_("Highest recorded instruction number is %s.\n"),
2340 pulongest (record_insn_count));
2342 /* Display log count. */
2343 printf_filtered (_("Log contains %d instructions.\n"),
2344 record_insn_num);
2346 else
2348 printf_filtered (_("No instructions have been logged.\n"));
2351 else
2353 printf_filtered (_("target record is not active.\n"));
2356 /* Display max log size. */
2357 printf_filtered (_("Max logged instructions is %d.\n"),
2358 record_insn_max_num);
2361 /* Record log save-file format
2362 Version 1 (never released)
2364 Header:
2365 4 bytes: magic number htonl(0x20090829).
2366 NOTE: be sure to change whenever this file format changes!
2368 Records:
2369 record_end:
2370 1 byte: record type (record_end, see enum record_type).
2371 record_reg:
2372 1 byte: record type (record_reg, see enum record_type).
2373 8 bytes: register id (network byte order).
2374 MAX_REGISTER_SIZE bytes: register value.
2375 record_mem:
2376 1 byte: record type (record_mem, see enum record_type).
2377 8 bytes: memory length (network byte order).
2378 8 bytes: memory address (network byte order).
2379 n bytes: memory value (n == memory length).
2381 Version 2
2382 4 bytes: magic number netorder32(0x20091016).
2383 NOTE: be sure to change whenever this file format changes!
2385 Records:
2386 record_end:
2387 1 byte: record type (record_end, see enum record_type).
2388 4 bytes: signal
2389 4 bytes: instruction count
2390 record_reg:
2391 1 byte: record type (record_reg, see enum record_type).
2392 4 bytes: register id (network byte order).
2393 n bytes: register value (n == actual register size).
2394 (eg. 4 bytes for x86 general registers).
2395 record_mem:
2396 1 byte: record type (record_mem, see enum record_type).
2397 4 bytes: memory length (network byte order).
2398 8 bytes: memory address (network byte order).
2399 n bytes: memory value (n == memory length).
2403 /* bfdcore_read -- read bytes from a core file section. */
2405 static inline void
2406 bfdcore_read (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2408 int ret = bfd_get_section_contents (obfd, osec, buf, *offset, len);
2410 if (ret)
2411 *offset += len;
2412 else
2413 error (_("Failed to read %d bytes from core file %s ('%s')."),
2414 len, bfd_get_filename (obfd),
2415 bfd_errmsg (bfd_get_error ()));
2418 static inline uint64_t
2419 netorder64 (uint64_t input)
2421 uint64_t ret;
2423 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2424 BFD_ENDIAN_BIG, input);
2425 return ret;
2428 static inline uint32_t
2429 netorder32 (uint32_t input)
2431 uint32_t ret;
2433 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2434 BFD_ENDIAN_BIG, input);
2435 return ret;
2438 static inline uint16_t
2439 netorder16 (uint16_t input)
2441 uint16_t ret;
2443 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2444 BFD_ENDIAN_BIG, input);
2445 return ret;
2448 /* Restore the execution log from a core_bfd file. */
2449 static void
2450 record_restore (void)
2452 uint32_t magic;
2453 struct cleanup *old_cleanups;
2454 struct record_entry *rec;
2455 asection *osec;
2456 uint32_t osec_size;
2457 int bfd_offset = 0;
2458 struct regcache *regcache;
2460 /* We restore the execution log from the open core bfd,
2461 if there is one. */
2462 if (core_bfd == NULL)
2463 return;
2465 /* "record_restore" can only be called when record list is empty. */
2466 gdb_assert (record_first.next == NULL);
2468 if (record_debug)
2469 fprintf_unfiltered (gdb_stdlog, "Restoring recording from core file.\n");
2471 /* Now need to find our special note section. */
2472 osec = bfd_get_section_by_name (core_bfd, "null0");
2473 if (record_debug)
2474 fprintf_unfiltered (gdb_stdlog, "Find precord section %s.\n",
2475 osec ? "succeeded" : "failed");
2476 if (osec == NULL)
2477 return;
2478 osec_size = bfd_section_size (core_bfd, osec);
2479 if (record_debug)
2480 fprintf_unfiltered (gdb_stdlog, "%s", bfd_section_name (core_bfd, osec));
2482 /* Check the magic code. */
2483 bfdcore_read (core_bfd, osec, &magic, sizeof (magic), &bfd_offset);
2484 if (magic != RECORD_FILE_MAGIC)
2485 error (_("Version mis-match or file format error in core file %s."),
2486 bfd_get_filename (core_bfd));
2487 if (record_debug)
2488 fprintf_unfiltered (gdb_stdlog,
2489 " Reading 4-byte magic cookie "
2490 "RECORD_FILE_MAGIC (0x%s)\n",
2491 phex_nz (netorder32 (magic), 4));
2493 /* Restore the entries in recfd into record_arch_list_head and
2494 record_arch_list_tail. */
2495 record_arch_list_head = NULL;
2496 record_arch_list_tail = NULL;
2497 record_insn_num = 0;
2498 old_cleanups = make_cleanup (record_arch_list_cleanups, 0);
2499 regcache = get_current_regcache ();
2501 while (1)
2503 uint8_t rectype;
2504 uint32_t regnum, len, signal, count;
2505 uint64_t addr;
2507 /* We are finished when offset reaches osec_size. */
2508 if (bfd_offset >= osec_size)
2509 break;
2510 bfdcore_read (core_bfd, osec, &rectype, sizeof (rectype), &bfd_offset);
2512 switch (rectype)
2514 case record_reg: /* reg */
2515 /* Get register number to regnum. */
2516 bfdcore_read (core_bfd, osec, &regnum,
2517 sizeof (regnum), &bfd_offset);
2518 regnum = netorder32 (regnum);
2520 rec = record_reg_alloc (regcache, regnum);
2522 /* Get val. */
2523 bfdcore_read (core_bfd, osec, record_get_loc (rec),
2524 rec->u.reg.len, &bfd_offset);
2526 if (record_debug)
2527 fprintf_unfiltered (gdb_stdlog,
2528 " Reading register %d (1 "
2529 "plus %lu plus %d bytes)\n",
2530 rec->u.reg.num,
2531 (unsigned long) sizeof (regnum),
2532 rec->u.reg.len);
2533 break;
2535 case record_mem: /* mem */
2536 /* Get len. */
2537 bfdcore_read (core_bfd, osec, &len,
2538 sizeof (len), &bfd_offset);
2539 len = netorder32 (len);
2541 /* Get addr. */
2542 bfdcore_read (core_bfd, osec, &addr,
2543 sizeof (addr), &bfd_offset);
2544 addr = netorder64 (addr);
2546 rec = record_mem_alloc (addr, len);
2548 /* Get val. */
2549 bfdcore_read (core_bfd, osec, record_get_loc (rec),
2550 rec->u.mem.len, &bfd_offset);
2552 if (record_debug)
2553 fprintf_unfiltered (gdb_stdlog,
2554 " Reading memory %s (1 plus "
2555 "%lu plus %lu plus %d bytes)\n",
2556 paddress (get_current_arch (),
2557 rec->u.mem.addr),
2558 (unsigned long) sizeof (addr),
2559 (unsigned long) sizeof (len),
2560 rec->u.mem.len);
2561 break;
2563 case record_end: /* end */
2564 rec = record_end_alloc ();
2565 record_insn_num ++;
2567 /* Get signal value. */
2568 bfdcore_read (core_bfd, osec, &signal,
2569 sizeof (signal), &bfd_offset);
2570 signal = netorder32 (signal);
2571 rec->u.end.sigval = signal;
2573 /* Get insn count. */
2574 bfdcore_read (core_bfd, osec, &count,
2575 sizeof (count), &bfd_offset);
2576 count = netorder32 (count);
2577 rec->u.end.insn_num = count;
2578 record_insn_count = count + 1;
2579 if (record_debug)
2580 fprintf_unfiltered (gdb_stdlog,
2581 " Reading record_end (1 + "
2582 "%lu + %lu bytes), offset == %s\n",
2583 (unsigned long) sizeof (signal),
2584 (unsigned long) sizeof (count),
2585 paddress (get_current_arch (),
2586 bfd_offset));
2587 break;
2589 default:
2590 error (_("Bad entry type in core file %s."),
2591 bfd_get_filename (core_bfd));
2592 break;
2595 /* Add rec to record arch list. */
2596 record_arch_list_add (rec);
2599 discard_cleanups (old_cleanups);
2601 /* Add record_arch_list_head to the end of record list. */
2602 record_first.next = record_arch_list_head;
2603 record_arch_list_head->prev = &record_first;
2604 record_arch_list_tail->next = NULL;
2605 record_list = &record_first;
2607 /* Update record_insn_max_num. */
2608 if (record_insn_num > record_insn_max_num)
2610 record_insn_max_num = record_insn_num;
2611 warning (_("Auto increase record/replay buffer limit to %d."),
2612 record_insn_max_num);
2615 /* Succeeded. */
2616 printf_filtered (_("Restored records from core file %s.\n"),
2617 bfd_get_filename (core_bfd));
2619 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
2622 /* bfdcore_write -- write bytes into a core file section. */
2624 static inline void
2625 bfdcore_write (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2627 int ret = bfd_set_section_contents (obfd, osec, buf, *offset, len);
2629 if (ret)
2630 *offset += len;
2631 else
2632 error (_("Failed to write %d bytes to core file %s ('%s')."),
2633 len, bfd_get_filename (obfd),
2634 bfd_errmsg (bfd_get_error ()));
2637 /* Restore the execution log from a file. We use a modified elf
2638 corefile format, with an extra section for our data. */
2640 static void
2641 cmd_record_restore (char *args, int from_tty)
2643 core_file_command (args, from_tty);
2644 record_open (args, from_tty);
2647 static void
2648 record_save_cleanups (void *data)
2650 bfd *obfd = data;
2651 char *pathname = xstrdup (bfd_get_filename (obfd));
2653 gdb_bfd_unref (obfd);
2654 unlink (pathname);
2655 xfree (pathname);
2658 /* Save the execution log to a file. We use a modified elf corefile
2659 format, with an extra section for our data. */
2661 static void
2662 cmd_record_save (char *args, int from_tty)
2664 char *recfilename, recfilename_buffer[40];
2665 struct record_entry *cur_record_list;
2666 uint32_t magic;
2667 struct regcache *regcache;
2668 struct gdbarch *gdbarch;
2669 struct cleanup *old_cleanups;
2670 struct cleanup *set_cleanups;
2671 bfd *obfd;
2672 int save_size = 0;
2673 asection *osec = NULL;
2674 int bfd_offset = 0;
2676 if (strcmp (current_target.to_shortname, "record") != 0)
2677 error (_("This command can only be used with target 'record'.\n"
2678 "Use 'target record' first.\n"));
2680 if (args && *args)
2681 recfilename = args;
2682 else
2684 /* Default recfile name is "gdb_record.PID". */
2685 snprintf (recfilename_buffer, sizeof (recfilename_buffer),
2686 "gdb_record.%d", PIDGET (inferior_ptid));
2687 recfilename = recfilename_buffer;
2690 /* Open the save file. */
2691 if (record_debug)
2692 fprintf_unfiltered (gdb_stdlog, "Saving execution log to core file '%s'\n",
2693 recfilename);
2695 /* Open the output file. */
2696 obfd = create_gcore_bfd (recfilename);
2697 old_cleanups = make_cleanup (record_save_cleanups, obfd);
2699 /* Save the current record entry to "cur_record_list". */
2700 cur_record_list = record_list;
2702 /* Get the values of regcache and gdbarch. */
2703 regcache = get_current_regcache ();
2704 gdbarch = get_regcache_arch (regcache);
2706 /* Disable the GDB operation record. */
2707 set_cleanups = record_gdb_operation_disable_set ();
2709 /* Reverse execute to the begin of record list. */
2710 while (1)
2712 /* Check for beginning and end of log. */
2713 if (record_list == &record_first)
2714 break;
2716 record_exec_insn (regcache, gdbarch, record_list);
2718 if (record_list->prev)
2719 record_list = record_list->prev;
2722 /* Compute the size needed for the extra bfd section. */
2723 save_size = 4; /* magic cookie */
2724 for (record_list = record_first.next; record_list;
2725 record_list = record_list->next)
2726 switch (record_list->type)
2728 case record_end:
2729 save_size += 1 + 4 + 4;
2730 break;
2731 case record_reg:
2732 save_size += 1 + 4 + record_list->u.reg.len;
2733 break;
2734 case record_mem:
2735 save_size += 1 + 4 + 8 + record_list->u.mem.len;
2736 break;
2739 /* Make the new bfd section. */
2740 osec = bfd_make_section_anyway_with_flags (obfd, "precord",
2741 SEC_HAS_CONTENTS
2742 | SEC_READONLY);
2743 if (osec == NULL)
2744 error (_("Failed to create 'precord' section for corefile %s: %s"),
2745 recfilename,
2746 bfd_errmsg (bfd_get_error ()));
2747 bfd_set_section_size (obfd, osec, save_size);
2748 bfd_set_section_vma (obfd, osec, 0);
2749 bfd_set_section_alignment (obfd, osec, 0);
2750 bfd_section_lma (obfd, osec) = 0;
2752 /* Save corefile state. */
2753 write_gcore_file (obfd);
2755 /* Write out the record log. */
2756 /* Write the magic code. */
2757 magic = RECORD_FILE_MAGIC;
2758 if (record_debug)
2759 fprintf_unfiltered (gdb_stdlog,
2760 " Writing 4-byte magic cookie "
2761 "RECORD_FILE_MAGIC (0x%s)\n",
2762 phex_nz (magic, 4));
2763 bfdcore_write (obfd, osec, &magic, sizeof (magic), &bfd_offset);
2765 /* Save the entries to recfd and forward execute to the end of
2766 record list. */
2767 record_list = &record_first;
2768 while (1)
2770 /* Save entry. */
2771 if (record_list != &record_first)
2773 uint8_t type;
2774 uint32_t regnum, len, signal, count;
2775 uint64_t addr;
2777 type = record_list->type;
2778 bfdcore_write (obfd, osec, &type, sizeof (type), &bfd_offset);
2780 switch (record_list->type)
2782 case record_reg: /* reg */
2783 if (record_debug)
2784 fprintf_unfiltered (gdb_stdlog,
2785 " Writing register %d (1 "
2786 "plus %lu plus %d bytes)\n",
2787 record_list->u.reg.num,
2788 (unsigned long) sizeof (regnum),
2789 record_list->u.reg.len);
2791 /* Write regnum. */
2792 regnum = netorder32 (record_list->u.reg.num);
2793 bfdcore_write (obfd, osec, &regnum,
2794 sizeof (regnum), &bfd_offset);
2796 /* Write regval. */
2797 bfdcore_write (obfd, osec, record_get_loc (record_list),
2798 record_list->u.reg.len, &bfd_offset);
2799 break;
2801 case record_mem: /* mem */
2802 if (record_debug)
2803 fprintf_unfiltered (gdb_stdlog,
2804 " Writing memory %s (1 plus "
2805 "%lu plus %lu plus %d bytes)\n",
2806 paddress (gdbarch,
2807 record_list->u.mem.addr),
2808 (unsigned long) sizeof (addr),
2809 (unsigned long) sizeof (len),
2810 record_list->u.mem.len);
2812 /* Write memlen. */
2813 len = netorder32 (record_list->u.mem.len);
2814 bfdcore_write (obfd, osec, &len, sizeof (len), &bfd_offset);
2816 /* Write memaddr. */
2817 addr = netorder64 (record_list->u.mem.addr);
2818 bfdcore_write (obfd, osec, &addr,
2819 sizeof (addr), &bfd_offset);
2821 /* Write memval. */
2822 bfdcore_write (obfd, osec, record_get_loc (record_list),
2823 record_list->u.mem.len, &bfd_offset);
2824 break;
2826 case record_end:
2827 if (record_debug)
2828 fprintf_unfiltered (gdb_stdlog,
2829 " Writing record_end (1 + "
2830 "%lu + %lu bytes)\n",
2831 (unsigned long) sizeof (signal),
2832 (unsigned long) sizeof (count));
2833 /* Write signal value. */
2834 signal = netorder32 (record_list->u.end.sigval);
2835 bfdcore_write (obfd, osec, &signal,
2836 sizeof (signal), &bfd_offset);
2838 /* Write insn count. */
2839 count = netorder32 (record_list->u.end.insn_num);
2840 bfdcore_write (obfd, osec, &count,
2841 sizeof (count), &bfd_offset);
2842 break;
2846 /* Execute entry. */
2847 record_exec_insn (regcache, gdbarch, record_list);
2849 if (record_list->next)
2850 record_list = record_list->next;
2851 else
2852 break;
2855 /* Reverse execute to cur_record_list. */
2856 while (1)
2858 /* Check for beginning and end of log. */
2859 if (record_list == cur_record_list)
2860 break;
2862 record_exec_insn (regcache, gdbarch, record_list);
2864 if (record_list->prev)
2865 record_list = record_list->prev;
2868 do_cleanups (set_cleanups);
2869 gdb_bfd_unref (obfd);
2870 discard_cleanups (old_cleanups);
2872 /* Succeeded. */
2873 printf_filtered (_("Saved core file %s with execution log.\n"),
2874 recfilename);
2877 /* record_goto_insn -- rewind the record log (forward or backward,
2878 depending on DIR) to the given entry, changing the program state
2879 correspondingly. */
2881 static void
2882 record_goto_insn (struct record_entry *entry,
2883 enum exec_direction_kind dir)
2885 struct cleanup *set_cleanups = record_gdb_operation_disable_set ();
2886 struct regcache *regcache = get_current_regcache ();
2887 struct gdbarch *gdbarch = get_regcache_arch (regcache);
2889 /* Assume everything is valid: we will hit the entry,
2890 and we will not hit the end of the recording. */
2892 if (dir == EXEC_FORWARD)
2893 record_list = record_list->next;
2897 record_exec_insn (regcache, gdbarch, record_list);
2898 if (dir == EXEC_REVERSE)
2899 record_list = record_list->prev;
2900 else
2901 record_list = record_list->next;
2902 } while (record_list != entry);
2903 do_cleanups (set_cleanups);
2906 /* "record goto" command. Argument is an instruction number,
2907 as given by "info record".
2909 Rewinds the recording (forward or backward) to the given instruction. */
2911 static void
2912 cmd_record_goto (char *arg, int from_tty)
2914 struct record_entry *p = NULL;
2915 ULONGEST target_insn = 0;
2917 if (arg == NULL || *arg == '\0')
2918 error (_("Command requires an argument (insn number to go to)."));
2920 if (strncmp (arg, "start", strlen ("start")) == 0
2921 || strncmp (arg, "begin", strlen ("begin")) == 0)
2923 /* Special case. Find first insn. */
2924 for (p = &record_first; p != NULL; p = p->next)
2925 if (p->type == record_end)
2926 break;
2927 if (p)
2928 target_insn = p->u.end.insn_num;
2930 else if (strncmp (arg, "end", strlen ("end")) == 0)
2932 /* Special case. Find last insn. */
2933 for (p = record_list; p->next != NULL; p = p->next)
2935 for (; p!= NULL; p = p->prev)
2936 if (p->type == record_end)
2937 break;
2938 if (p)
2939 target_insn = p->u.end.insn_num;
2941 else
2943 /* General case. Find designated insn. */
2944 target_insn = parse_and_eval_long (arg);
2946 for (p = &record_first; p != NULL; p = p->next)
2947 if (p->type == record_end && p->u.end.insn_num == target_insn)
2948 break;
2951 if (p == NULL)
2952 error (_("Target insn '%s' not found."), arg);
2953 else if (p == record_list)
2954 error (_("Already at insn '%s'."), arg);
2955 else if (p->u.end.insn_num > record_list->u.end.insn_num)
2957 printf_filtered (_("Go forward to insn number %s\n"),
2958 pulongest (target_insn));
2959 record_goto_insn (p, EXEC_FORWARD);
2961 else
2963 printf_filtered (_("Go backward to insn number %s\n"),
2964 pulongest (target_insn));
2965 record_goto_insn (p, EXEC_REVERSE);
2967 registers_changed ();
2968 reinit_frame_cache ();
2969 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
2972 /* Provide a prototype to silence -Wmissing-prototypes. */
2973 extern initialize_file_ftype _initialize_record;
2975 void
2976 _initialize_record (void)
2978 struct cmd_list_element *c;
2980 /* Init record_first. */
2981 record_first.prev = NULL;
2982 record_first.next = NULL;
2983 record_first.type = record_end;
2985 init_record_ops ();
2986 add_target (&record_ops);
2987 init_record_core_ops ();
2988 add_target (&record_core_ops);
2990 add_setshow_zuinteger_cmd ("record", no_class, &record_debug,
2991 _("Set debugging of record/replay feature."),
2992 _("Show debugging of record/replay feature."),
2993 _("When enabled, debugging output for "
2994 "record/replay feature is displayed."),
2995 NULL, show_record_debug, &setdebuglist,
2996 &showdebuglist);
2998 c = add_prefix_cmd ("record", class_obscure, cmd_record_start,
2999 _("Abbreviated form of \"target record\" command."),
3000 &record_cmdlist, "record ", 0, &cmdlist);
3001 set_cmd_completer (c, filename_completer);
3003 add_com_alias ("rec", "record", class_obscure, 1);
3004 add_prefix_cmd ("record", class_support, set_record_command,
3005 _("Set record options"), &set_record_cmdlist,
3006 "set record ", 0, &setlist);
3007 add_alias_cmd ("rec", "record", class_obscure, 1, &setlist);
3008 add_prefix_cmd ("record", class_support, show_record_command,
3009 _("Show record options"), &show_record_cmdlist,
3010 "show record ", 0, &showlist);
3011 add_alias_cmd ("rec", "record", class_obscure, 1, &showlist);
3012 add_prefix_cmd ("record", class_support, info_record_command,
3013 _("Info record options"), &info_record_cmdlist,
3014 "info record ", 0, &infolist);
3015 add_alias_cmd ("rec", "record", class_obscure, 1, &infolist);
3017 c = add_cmd ("save", class_obscure, cmd_record_save,
3018 _("Save the execution log to a file.\n\
3019 Argument is optional filename.\n\
3020 Default filename is 'gdb_record.<process_id>'."),
3021 &record_cmdlist);
3022 set_cmd_completer (c, filename_completer);
3024 c = add_cmd ("restore", class_obscure, cmd_record_restore,
3025 _("Restore the execution log from a file.\n\
3026 Argument is filename. File must be created with 'record save'."),
3027 &record_cmdlist);
3028 set_cmd_completer (c, filename_completer);
3030 add_cmd ("delete", class_obscure, cmd_record_delete,
3031 _("Delete the rest of execution log and start recording it anew."),
3032 &record_cmdlist);
3033 add_alias_cmd ("d", "delete", class_obscure, 1, &record_cmdlist);
3034 add_alias_cmd ("del", "delete", class_obscure, 1, &record_cmdlist);
3036 add_cmd ("stop", class_obscure, cmd_record_stop,
3037 _("Stop the record/replay target."),
3038 &record_cmdlist);
3039 add_alias_cmd ("s", "stop", class_obscure, 1, &record_cmdlist);
3041 /* Record instructions number limit command. */
3042 add_setshow_boolean_cmd ("stop-at-limit", no_class,
3043 &record_stop_at_limit, _("\
3044 Set whether record/replay stops when record/replay buffer becomes full."), _("\
3045 Show whether record/replay stops when record/replay buffer becomes full."),
3046 _("Default is ON.\n\
3047 When ON, if the record/replay buffer becomes full, ask user what to do.\n\
3048 When OFF, if the record/replay buffer becomes full,\n\
3049 delete the oldest recorded instruction to make room for each new one."),
3050 NULL, NULL,
3051 &set_record_cmdlist, &show_record_cmdlist);
3052 add_setshow_uinteger_cmd ("insn-number-max", no_class,
3053 &record_insn_max_num,
3054 _("Set record/replay buffer limit."),
3055 _("Show record/replay buffer limit."), _("\
3056 Set the maximum number of instructions to be stored in the\n\
3057 record/replay buffer. Zero means unlimited. Default is 200000."),
3058 set_record_insn_max_num,
3059 NULL, &set_record_cmdlist, &show_record_cmdlist);
3061 add_cmd ("goto", class_obscure, cmd_record_goto, _("\
3062 Restore the program to its state at instruction number N.\n\
3063 Argument is instruction number, as shown by 'info record'."),
3064 &record_cmdlist);
3066 add_setshow_boolean_cmd ("memory-query", no_class,
3067 &record_memory_query, _("\
3068 Set whether query if PREC cannot record memory change of next instruction."),
3069 _("\
3070 Show whether query if PREC cannot record memory change of next instruction."),
3071 _("\
3072 Default is OFF.\n\
3073 When ON, query if PREC cannot record memory change of next instruction."),
3074 NULL, NULL,
3075 &set_record_cmdlist, &show_record_cmdlist);