* record.c (record_core_xfer_partial): Pass correct offset to
[binutils-gdb.git] / gdb / record.c
1 /* Process record and replay target for GDB, the GNU debugger.
2
3 Copyright (C) 2008, 2009 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
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
34 #include <byteswap.h>
35 #include <signal.h>
36
37 /* This module implements "target record", also known as "process
38 record and replay". This target sits on top of a "normal" target
39 (a target that "has execution"), and provides a record and replay
40 functionality, including reverse debugging.
41
42 Target record has two modes: recording, and replaying.
43
44 In record mode, we intercept the to_resume and to_wait methods.
45 Whenever gdb resumes the target, we run the target in single step
46 mode, and we build up an execution log in which, for each executed
47 instruction, we record all changes in memory and register state.
48 This is invisible to the user, to whom it just looks like an
49 ordinary debugging session (except for performance degredation).
50
51 In replay mode, instead of actually letting the inferior run as a
52 process, we simulate its execution by playing back the recorded
53 execution log. For each instruction in the log, we simulate the
54 instruction's side effects by duplicating the changes that it would
55 have made on memory and registers. */
56
57 #define DEFAULT_RECORD_INSN_MAX_NUM 200000
58
59 #define RECORD_IS_REPLAY \
60 (record_list->next || execution_direction == EXEC_REVERSE)
61
62 #define RECORD_FILE_MAGIC netorder32(0x20091016)
63
64 /* These are the core structs of the process record functionality.
65
66 A record_entry is a record of the value change of a register
67 ("record_reg") or a part of memory ("record_mem"). And each
68 instruction must have a struct record_entry ("record_end") that
69 indicates that this is the last struct record_entry of this
70 instruction.
71
72 Each struct record_entry is linked to "record_list" by "prev" and
73 "next" pointers. */
74
75 struct record_mem_entry
76 {
77 CORE_ADDR addr;
78 int len;
79 /* Set this flag if target memory for this entry
80 can no longer be accessed. */
81 int mem_entry_not_accessible;
82 union
83 {
84 gdb_byte *ptr;
85 gdb_byte buf[sizeof (gdb_byte *)];
86 } u;
87 };
88
89 struct record_reg_entry
90 {
91 unsigned short num;
92 unsigned short len;
93 union
94 {
95 gdb_byte *ptr;
96 gdb_byte buf[2 * sizeof (gdb_byte *)];
97 } u;
98 };
99
100 struct record_end_entry
101 {
102 enum target_signal sigval;
103 ULONGEST insn_num;
104 };
105
106 enum record_type
107 {
108 record_end = 0,
109 record_reg,
110 record_mem
111 };
112
113 /* This is the data structure that makes up the execution log.
114
115 The execution log consists of a single linked list of entries
116 of type "struct record_entry". It is doubly linked so that it
117 can be traversed in either direction.
118
119 The start of the list is anchored by a struct called
120 "record_first". The pointer "record_list" either points to the
121 last entry that was added to the list (in record mode), or to the
122 next entry in the list that will be executed (in replay mode).
123
124 Each list element (struct record_entry), in addition to next and
125 prev pointers, consists of a union of three entry types: mem, reg,
126 and end. A field called "type" determines which entry type is
127 represented by a given list element.
128
129 Each instruction that is added to the execution log is represented
130 by a variable number of list elements ('entries'). The instruction
131 will have one "reg" entry for each register that is changed by
132 executing the instruction (including the PC in every case). It
133 will also have one "mem" entry for each memory change. Finally,
134 each instruction will have an "end" entry that separates it from
135 the changes associated with the next instruction. */
136
137 struct record_entry
138 {
139 struct record_entry *prev;
140 struct record_entry *next;
141 enum record_type type;
142 union
143 {
144 /* reg */
145 struct record_reg_entry reg;
146 /* mem */
147 struct record_mem_entry mem;
148 /* end */
149 struct record_end_entry end;
150 } u;
151 };
152
153 /* This is the debug switch for process record. */
154 int record_debug = 0;
155
156 struct record_core_buf_entry
157 {
158 struct record_core_buf_entry *prev;
159 struct target_section *p;
160 bfd_byte *buf;
161 };
162
163 /* Record buf with core target. */
164 static gdb_byte *record_core_regbuf = NULL;
165 static struct target_section *record_core_start;
166 static struct target_section *record_core_end;
167 static struct record_core_buf_entry *record_core_buf_list = NULL;
168
169 /* The following variables are used for managing the linked list that
170 represents the execution log.
171
172 record_first is the anchor that holds down the beginning of the list.
173
174 record_list serves two functions:
175 1) In record mode, it anchors the end of the list.
176 2) In replay mode, it traverses the list and points to
177 the next instruction that must be emulated.
178
179 record_arch_list_head and record_arch_list_tail are used to manage
180 a separate list, which is used to build up the change elements of
181 the currently executing instruction during record mode. When this
182 instruction has been completely annotated in the "arch list", it
183 will be appended to the main execution log. */
184
185 static struct record_entry record_first;
186 static struct record_entry *record_list = &record_first;
187 static struct record_entry *record_arch_list_head = NULL;
188 static struct record_entry *record_arch_list_tail = NULL;
189
190 /* 1 ask user. 0 auto delete the last struct record_entry. */
191 static int record_stop_at_limit = 1;
192 /* Maximum allowed number of insns in execution log. */
193 static unsigned int record_insn_max_num = DEFAULT_RECORD_INSN_MAX_NUM;
194 /* Actual count of insns presently in execution log. */
195 static int record_insn_num = 0;
196 /* Count of insns logged so far (may be larger
197 than count of insns presently in execution log). */
198 static ULONGEST record_insn_count;
199
200 /* The target_ops of process record. */
201 static struct target_ops record_ops;
202 static struct target_ops record_core_ops;
203
204 /* The beneath function pointers. */
205 static struct target_ops *record_beneath_to_resume_ops;
206 static void (*record_beneath_to_resume) (struct target_ops *, ptid_t, int,
207 enum target_signal);
208 static struct target_ops *record_beneath_to_wait_ops;
209 static ptid_t (*record_beneath_to_wait) (struct target_ops *, ptid_t,
210 struct target_waitstatus *,
211 int);
212 static struct target_ops *record_beneath_to_store_registers_ops;
213 static void (*record_beneath_to_store_registers) (struct target_ops *,
214 struct regcache *,
215 int regno);
216 static struct target_ops *record_beneath_to_xfer_partial_ops;
217 static LONGEST (*record_beneath_to_xfer_partial) (struct target_ops *ops,
218 enum target_object object,
219 const char *annex,
220 gdb_byte *readbuf,
221 const gdb_byte *writebuf,
222 ULONGEST offset,
223 LONGEST len);
224 static int (*record_beneath_to_insert_breakpoint) (struct gdbarch *,
225 struct bp_target_info *);
226 static int (*record_beneath_to_remove_breakpoint) (struct gdbarch *,
227 struct bp_target_info *);
228
229 /* Alloc and free functions for record_reg, record_mem, and record_end
230 entries. */
231
232 /* Alloc a record_reg record entry. */
233
234 static inline struct record_entry *
235 record_reg_alloc (struct regcache *regcache, int regnum)
236 {
237 struct record_entry *rec;
238 struct gdbarch *gdbarch = get_regcache_arch (regcache);
239
240 rec = (struct record_entry *) xcalloc (1, sizeof (struct record_entry));
241 rec->type = record_reg;
242 rec->u.reg.num = regnum;
243 rec->u.reg.len = register_size (gdbarch, regnum);
244 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
245 rec->u.reg.u.ptr = (gdb_byte *) xmalloc (rec->u.reg.len);
246
247 return rec;
248 }
249
250 /* Free a record_reg record entry. */
251
252 static inline void
253 record_reg_release (struct record_entry *rec)
254 {
255 gdb_assert (rec->type == record_reg);
256 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
257 xfree (rec->u.reg.u.ptr);
258 xfree (rec);
259 }
260
261 /* Alloc a record_mem record entry. */
262
263 static inline struct record_entry *
264 record_mem_alloc (CORE_ADDR addr, int len)
265 {
266 struct record_entry *rec;
267
268 rec = (struct record_entry *) xcalloc (1, sizeof (struct record_entry));
269 rec->type = record_mem;
270 rec->u.mem.addr = addr;
271 rec->u.mem.len = len;
272 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
273 rec->u.mem.u.ptr = (gdb_byte *) xmalloc (len);
274
275 return rec;
276 }
277
278 /* Free a record_mem record entry. */
279
280 static inline void
281 record_mem_release (struct record_entry *rec)
282 {
283 gdb_assert (rec->type == record_mem);
284 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
285 xfree (rec->u.mem.u.ptr);
286 xfree (rec);
287 }
288
289 /* Alloc a record_end record entry. */
290
291 static inline struct record_entry *
292 record_end_alloc (void)
293 {
294 struct record_entry *rec;
295
296 rec = (struct record_entry *) xcalloc (1, sizeof (struct record_entry));
297 rec->type = record_end;
298
299 return rec;
300 }
301
302 /* Free a record_end record entry. */
303
304 static inline void
305 record_end_release (struct record_entry *rec)
306 {
307 xfree (rec);
308 }
309
310 /* Free one record entry, any type.
311 Return entry->type, in case caller wants to know. */
312
313 static inline enum record_type
314 record_entry_release (struct record_entry *rec)
315 {
316 enum record_type type = rec->type;
317
318 switch (type) {
319 case record_reg:
320 record_reg_release (rec);
321 break;
322 case record_mem:
323 record_mem_release (rec);
324 break;
325 case record_end:
326 record_end_release (rec);
327 break;
328 }
329 return type;
330 }
331
332 /* Free all record entries in list pointed to by REC. */
333
334 static void
335 record_list_release (struct record_entry *rec)
336 {
337 if (!rec)
338 return;
339
340 while (rec->next)
341 rec = rec->next;
342
343 while (rec->prev)
344 {
345 rec = rec->prev;
346 record_entry_release (rec->next);
347 }
348
349 if (rec == &record_first)
350 {
351 record_insn_num = 0;
352 record_first.next = NULL;
353 }
354 else
355 record_entry_release (rec);
356 }
357
358 /* Free all record entries forward of the given list position. */
359
360 static void
361 record_list_release_following (struct record_entry *rec)
362 {
363 struct record_entry *tmp = rec->next;
364
365 rec->next = NULL;
366 while (tmp)
367 {
368 rec = tmp->next;
369 if (record_entry_release (tmp) == record_end)
370 {
371 record_insn_num--;
372 record_insn_count--;
373 }
374 tmp = rec;
375 }
376 }
377
378 /* Delete the first instruction from the beginning of the log, to make
379 room for adding a new instruction at the end of the log.
380
381 Note -- this function does not modify record_insn_num. */
382
383 static void
384 record_list_release_first (void)
385 {
386 struct record_entry *tmp;
387
388 if (!record_first.next)
389 return;
390
391 /* Loop until a record_end. */
392 while (1)
393 {
394 /* Cut record_first.next out of the linked list. */
395 tmp = record_first.next;
396 record_first.next = tmp->next;
397 tmp->next->prev = &record_first;
398
399 /* tmp is now isolated, and can be deleted. */
400 if (record_entry_release (tmp) == record_end)
401 break; /* End loop at first record_end. */
402
403 if (!record_first.next)
404 {
405 gdb_assert (record_insn_num == 1);
406 break; /* End loop when list is empty. */
407 }
408 }
409 }
410
411 /* Add a struct record_entry to record_arch_list. */
412
413 static void
414 record_arch_list_add (struct record_entry *rec)
415 {
416 if (record_debug > 1)
417 fprintf_unfiltered (gdb_stdlog,
418 "Process record: record_arch_list_add %s.\n",
419 host_address_to_string (rec));
420
421 if (record_arch_list_tail)
422 {
423 record_arch_list_tail->next = rec;
424 rec->prev = record_arch_list_tail;
425 record_arch_list_tail = rec;
426 }
427 else
428 {
429 record_arch_list_head = rec;
430 record_arch_list_tail = rec;
431 }
432 }
433
434 /* Return the value storage location of a record entry. */
435 static inline gdb_byte *
436 record_get_loc (struct record_entry *rec)
437 {
438 switch (rec->type) {
439 case record_mem:
440 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
441 return rec->u.mem.u.ptr;
442 else
443 return rec->u.mem.u.buf;
444 case record_reg:
445 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
446 return rec->u.reg.u.ptr;
447 else
448 return rec->u.reg.u.buf;
449 case record_end:
450 default:
451 gdb_assert (0);
452 return NULL;
453 }
454 }
455
456 /* Record the value of a register NUM to record_arch_list. */
457
458 int
459 record_arch_list_add_reg (struct regcache *regcache, int regnum)
460 {
461 struct record_entry *rec;
462
463 if (record_debug > 1)
464 fprintf_unfiltered (gdb_stdlog,
465 "Process record: add register num = %d to "
466 "record list.\n",
467 regnum);
468
469 rec = record_reg_alloc (regcache, regnum);
470
471 regcache_raw_read (regcache, regnum, record_get_loc (rec));
472
473 record_arch_list_add (rec);
474
475 return 0;
476 }
477
478 /* Record the value of a region of memory whose address is ADDR and
479 length is LEN to record_arch_list. */
480
481 int
482 record_arch_list_add_mem (CORE_ADDR addr, int len)
483 {
484 struct record_entry *rec;
485
486 if (record_debug > 1)
487 fprintf_unfiltered (gdb_stdlog,
488 "Process record: add mem addr = %s len = %d to "
489 "record list.\n",
490 paddress (target_gdbarch, addr), len);
491
492 if (!addr) /* FIXME: Why? Some arch must permit it... */
493 return 0;
494
495 rec = record_mem_alloc (addr, len);
496
497 if (target_read_memory (addr, record_get_loc (rec), len))
498 {
499 if (record_debug)
500 fprintf_unfiltered (gdb_stdlog,
501 "Process record: error reading memory at "
502 "addr = %s len = %d.\n",
503 paddress (target_gdbarch, addr), len);
504 record_mem_release (rec);
505 return -1;
506 }
507
508 record_arch_list_add (rec);
509
510 return 0;
511 }
512
513 /* Add a record_end type struct record_entry to record_arch_list. */
514
515 int
516 record_arch_list_add_end (void)
517 {
518 struct record_entry *rec;
519
520 if (record_debug > 1)
521 fprintf_unfiltered (gdb_stdlog,
522 "Process record: add end to arch list.\n");
523
524 rec = record_end_alloc ();
525 rec->u.end.sigval = TARGET_SIGNAL_0;
526 rec->u.end.insn_num = ++record_insn_count;
527
528 record_arch_list_add (rec);
529
530 return 0;
531 }
532
533 static void
534 record_check_insn_num (int set_terminal)
535 {
536 if (record_insn_max_num)
537 {
538 gdb_assert (record_insn_num <= record_insn_max_num);
539 if (record_insn_num == record_insn_max_num)
540 {
541 /* Ask user what to do. */
542 if (record_stop_at_limit)
543 {
544 int q;
545 if (set_terminal)
546 target_terminal_ours ();
547 q = yquery (_("Do you want to auto delete previous execution "
548 "log entries when record/replay buffer becomes "
549 "full (record stop-at-limit)?"));
550 if (set_terminal)
551 target_terminal_inferior ();
552 if (q)
553 record_stop_at_limit = 0;
554 else
555 error (_("Process record: stopped by user."));
556 }
557 }
558 }
559 }
560
561 static void
562 record_arch_list_cleanups (void *ignore)
563 {
564 record_list_release (record_arch_list_tail);
565 }
566
567 /* Before inferior step (when GDB record the running message, inferior
568 only can step), GDB will call this function to record the values to
569 record_list. This function will call gdbarch_process_record to
570 record the running message of inferior and set them to
571 record_arch_list, and add it to record_list. */
572
573 struct record_message_args {
574 struct regcache *regcache;
575 enum target_signal signal;
576 };
577
578 static int
579 record_message (void *args)
580 {
581 int ret;
582 struct record_message_args *myargs = args;
583 struct gdbarch *gdbarch = get_regcache_arch (myargs->regcache);
584 struct cleanup *old_cleanups = make_cleanup (record_arch_list_cleanups, 0);
585
586 record_arch_list_head = NULL;
587 record_arch_list_tail = NULL;
588
589 /* Check record_insn_num. */
590 record_check_insn_num (1);
591
592 /* If gdb sends a signal value to target_resume,
593 save it in the 'end' field of the previous instruction.
594
595 Maybe process record should record what really happened,
596 rather than what gdb pretends has happened.
597
598 So if Linux delivered the signal to the child process during
599 the record mode, we will record it and deliver it again in
600 the replay mode.
601
602 If user says "ignore this signal" during the record mode, then
603 it will be ignored again during the replay mode (no matter if
604 the user says something different, like "deliver this signal"
605 during the replay mode).
606
607 User should understand that nothing he does during the replay
608 mode will change the behavior of the child. If he tries,
609 then that is a user error.
610
611 But we should still deliver the signal to gdb during the replay,
612 if we delivered it during the recording. Therefore we should
613 record the signal during record_wait, not record_resume. */
614 if (record_list != &record_first) /* FIXME better way to check */
615 {
616 gdb_assert (record_list->type == record_end);
617 record_list->u.end.sigval = myargs->signal;
618 }
619
620 if (myargs->signal == TARGET_SIGNAL_0
621 || !gdbarch_process_record_signal_p (gdbarch))
622 ret = gdbarch_process_record (gdbarch,
623 myargs->regcache,
624 regcache_read_pc (myargs->regcache));
625 else
626 ret = gdbarch_process_record_signal (gdbarch,
627 myargs->regcache,
628 myargs->signal);
629
630 if (ret > 0)
631 error (_("Process record: inferior program stopped."));
632 if (ret < 0)
633 error (_("Process record: failed to record execution log."));
634
635 discard_cleanups (old_cleanups);
636
637 record_list->next = record_arch_list_head;
638 record_arch_list_head->prev = record_list;
639 record_list = record_arch_list_tail;
640
641 if (record_insn_num == record_insn_max_num && record_insn_max_num)
642 record_list_release_first ();
643 else
644 record_insn_num++;
645
646 return 1;
647 }
648
649 static int
650 do_record_message (struct regcache *regcache,
651 enum target_signal signal)
652 {
653 struct record_message_args args;
654
655 args.regcache = regcache;
656 args.signal = signal;
657 return catch_errors (record_message, &args, NULL, RETURN_MASK_ALL);
658 }
659
660 /* Set to 1 if record_store_registers and record_xfer_partial
661 doesn't need record. */
662
663 static int record_gdb_operation_disable = 0;
664
665 struct cleanup *
666 record_gdb_operation_disable_set (void)
667 {
668 struct cleanup *old_cleanups = NULL;
669
670 old_cleanups =
671 make_cleanup_restore_integer (&record_gdb_operation_disable);
672 record_gdb_operation_disable = 1;
673
674 return old_cleanups;
675 }
676
677 /* Execute one instruction from the record log. Each instruction in
678 the log will be represented by an arbitrary sequence of register
679 entries and memory entries, followed by an 'end' entry. */
680
681 static inline void
682 record_exec_insn (struct regcache *regcache, struct gdbarch *gdbarch,
683 struct record_entry *entry)
684 {
685 switch (entry->type)
686 {
687 case record_reg: /* reg */
688 {
689 gdb_byte reg[MAX_REGISTER_SIZE];
690
691 if (record_debug > 1)
692 fprintf_unfiltered (gdb_stdlog,
693 "Process record: record_reg %s to "
694 "inferior num = %d.\n",
695 host_address_to_string (entry),
696 entry->u.reg.num);
697
698 regcache_cooked_read (regcache, entry->u.reg.num, reg);
699 regcache_cooked_write (regcache, entry->u.reg.num,
700 record_get_loc (entry));
701 memcpy (record_get_loc (entry), reg, entry->u.reg.len);
702 }
703 break;
704
705 case record_mem: /* mem */
706 {
707 /* Nothing to do if the entry is flagged not_accessible. */
708 if (!entry->u.mem.mem_entry_not_accessible)
709 {
710 gdb_byte *mem = alloca (entry->u.mem.len);
711
712 if (record_debug > 1)
713 fprintf_unfiltered (gdb_stdlog,
714 "Process record: record_mem %s to "
715 "inferior addr = %s len = %d.\n",
716 host_address_to_string (entry),
717 paddress (gdbarch, entry->u.mem.addr),
718 entry->u.mem.len);
719
720 if (target_read_memory (entry->u.mem.addr, mem, entry->u.mem.len))
721 {
722 entry->u.mem.mem_entry_not_accessible = 1;
723 if (record_debug)
724 warning ("Process record: error reading memory at "
725 "addr = %s len = %d.",
726 paddress (gdbarch, entry->u.mem.addr),
727 entry->u.mem.len);
728 }
729 else
730 {
731 if (target_write_memory (entry->u.mem.addr,
732 record_get_loc (entry),
733 entry->u.mem.len))
734 {
735 entry->u.mem.mem_entry_not_accessible = 1;
736 if (record_debug)
737 warning ("Process record: error writing memory at "
738 "addr = %s len = %d.",
739 paddress (gdbarch, entry->u.mem.addr),
740 entry->u.mem.len);
741 }
742 else
743 memcpy (record_get_loc (entry), mem, entry->u.mem.len);
744 }
745 }
746 }
747 break;
748 }
749 }
750
751 static struct target_ops *tmp_to_resume_ops;
752 static void (*tmp_to_resume) (struct target_ops *, ptid_t, int,
753 enum target_signal);
754 static struct target_ops *tmp_to_wait_ops;
755 static ptid_t (*tmp_to_wait) (struct target_ops *, ptid_t,
756 struct target_waitstatus *,
757 int);
758 static struct target_ops *tmp_to_store_registers_ops;
759 static void (*tmp_to_store_registers) (struct target_ops *,
760 struct regcache *,
761 int regno);
762 static struct target_ops *tmp_to_xfer_partial_ops;
763 static LONGEST (*tmp_to_xfer_partial) (struct target_ops *ops,
764 enum target_object object,
765 const char *annex,
766 gdb_byte *readbuf,
767 const gdb_byte *writebuf,
768 ULONGEST offset,
769 LONGEST len);
770 static int (*tmp_to_insert_breakpoint) (struct gdbarch *,
771 struct bp_target_info *);
772 static int (*tmp_to_remove_breakpoint) (struct gdbarch *,
773 struct bp_target_info *);
774
775 static void record_restore (void);
776
777 /* Open the process record target. */
778
779 static void
780 record_core_open_1 (char *name, int from_tty)
781 {
782 struct regcache *regcache = get_current_regcache ();
783 int regnum = gdbarch_num_regs (get_regcache_arch (regcache));
784 int i;
785
786 /* Get record_core_regbuf. */
787 target_fetch_registers (regcache, -1);
788 record_core_regbuf = xmalloc (MAX_REGISTER_SIZE * regnum);
789 for (i = 0; i < regnum; i ++)
790 regcache_raw_collect (regcache, i,
791 record_core_regbuf + MAX_REGISTER_SIZE * i);
792
793 /* Get record_core_start and record_core_end. */
794 if (build_section_table (core_bfd, &record_core_start, &record_core_end))
795 {
796 xfree (record_core_regbuf);
797 record_core_regbuf = NULL;
798 error (_("\"%s\": Can't find sections: %s"),
799 bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ()));
800 }
801
802 push_target (&record_core_ops);
803 record_restore ();
804 }
805
806 /* "to_open" target method for 'live' processes. */
807
808 static void
809 record_open_1 (char *name, int from_tty)
810 {
811 struct target_ops *t;
812
813 if (record_debug)
814 fprintf_unfiltered (gdb_stdlog, "Process record: record_open\n");
815
816 /* check exec */
817 if (!target_has_execution)
818 error (_("Process record: the program is not being run."));
819 if (non_stop)
820 error (_("Process record target can't debug inferior in non-stop mode "
821 "(non-stop)."));
822 if (target_async_permitted)
823 error (_("Process record target can't debug inferior in asynchronous "
824 "mode (target-async)."));
825
826 if (!gdbarch_process_record_p (target_gdbarch))
827 error (_("Process record: the current architecture doesn't support "
828 "record function."));
829
830 if (!tmp_to_resume)
831 error (_("Could not find 'to_resume' method on the target stack."));
832 if (!tmp_to_wait)
833 error (_("Could not find 'to_wait' method on the target stack."));
834 if (!tmp_to_store_registers)
835 error (_("Could not find 'to_store_registers' method on the target stack."));
836 if (!tmp_to_insert_breakpoint)
837 error (_("Could not find 'to_insert_breakpoint' method on the target stack."));
838 if (!tmp_to_remove_breakpoint)
839 error (_("Could not find 'to_remove_breakpoint' method on the target stack."));
840
841 push_target (&record_ops);
842 }
843
844 /* "to_open" target method. Open the process record target. */
845
846 static void
847 record_open (char *name, int from_tty)
848 {
849 struct target_ops *t;
850
851 if (record_debug)
852 fprintf_unfiltered (gdb_stdlog, "Process record: record_open\n");
853
854 /* Check if record target is already running. */
855 if (current_target.to_stratum == record_stratum)
856 error (_("Process record target already running. Use \"record stop\" to "
857 "stop record target first."));
858
859 /* Reset the tmp beneath pointers. */
860 tmp_to_resume_ops = NULL;
861 tmp_to_resume = NULL;
862 tmp_to_wait_ops = NULL;
863 tmp_to_wait = NULL;
864 tmp_to_store_registers_ops = NULL;
865 tmp_to_store_registers = NULL;
866 tmp_to_xfer_partial_ops = NULL;
867 tmp_to_xfer_partial = NULL;
868 tmp_to_insert_breakpoint = NULL;
869 tmp_to_remove_breakpoint = NULL;
870
871 /* Set the beneath function pointers. */
872 for (t = current_target.beneath; t != NULL; t = t->beneath)
873 {
874 if (!tmp_to_resume)
875 {
876 tmp_to_resume = t->to_resume;
877 tmp_to_resume_ops = t;
878 }
879 if (!tmp_to_wait)
880 {
881 tmp_to_wait = t->to_wait;
882 tmp_to_wait_ops = t;
883 }
884 if (!tmp_to_store_registers)
885 {
886 tmp_to_store_registers = t->to_store_registers;
887 tmp_to_store_registers_ops = t;
888 }
889 if (!tmp_to_xfer_partial)
890 {
891 tmp_to_xfer_partial = t->to_xfer_partial;
892 tmp_to_xfer_partial_ops = t;
893 }
894 if (!tmp_to_insert_breakpoint)
895 tmp_to_insert_breakpoint = t->to_insert_breakpoint;
896 if (!tmp_to_remove_breakpoint)
897 tmp_to_remove_breakpoint = t->to_remove_breakpoint;
898 }
899 if (!tmp_to_xfer_partial)
900 error (_("Could not find 'to_xfer_partial' method on the target stack."));
901
902 /* Reset */
903 record_insn_num = 0;
904 record_insn_count = 0;
905 record_list = &record_first;
906 record_list->next = NULL;
907
908 /* Set the tmp beneath pointers to beneath pointers. */
909 record_beneath_to_resume_ops = tmp_to_resume_ops;
910 record_beneath_to_resume = tmp_to_resume;
911 record_beneath_to_wait_ops = tmp_to_wait_ops;
912 record_beneath_to_wait = tmp_to_wait;
913 record_beneath_to_store_registers_ops = tmp_to_store_registers_ops;
914 record_beneath_to_store_registers = tmp_to_store_registers;
915 record_beneath_to_xfer_partial_ops = tmp_to_xfer_partial_ops;
916 record_beneath_to_xfer_partial = tmp_to_xfer_partial;
917 record_beneath_to_insert_breakpoint = tmp_to_insert_breakpoint;
918 record_beneath_to_remove_breakpoint = tmp_to_remove_breakpoint;
919
920 if (current_target.to_stratum == core_stratum)
921 record_core_open_1 (name, from_tty);
922 else
923 record_open_1 (name, from_tty);
924 }
925
926 /* "to_close" target method. Close the process record target. */
927
928 static void
929 record_close (int quitting)
930 {
931 struct record_core_buf_entry *entry;
932
933 if (record_debug)
934 fprintf_unfiltered (gdb_stdlog, "Process record: record_close\n");
935
936 record_list_release (record_list);
937
938 /* Release record_core_regbuf. */
939 if (record_core_regbuf)
940 {
941 xfree (record_core_regbuf);
942 record_core_regbuf = NULL;
943 }
944
945 /* Release record_core_buf_list. */
946 if (record_core_buf_list)
947 {
948 for (entry = record_core_buf_list->prev; entry; entry = entry->prev)
949 {
950 xfree (record_core_buf_list);
951 record_core_buf_list = entry;
952 }
953 record_core_buf_list = NULL;
954 }
955 }
956
957 static int record_resume_step = 0;
958 static int record_resume_error;
959
960 /* "to_resume" target method. Resume the process record target. */
961
962 static void
963 record_resume (struct target_ops *ops, ptid_t ptid, int step,
964 enum target_signal signal)
965 {
966 record_resume_step = step;
967
968 if (!RECORD_IS_REPLAY)
969 {
970 if (do_record_message (get_current_regcache (), signal))
971 {
972 record_resume_error = 0;
973 }
974 else
975 {
976 record_resume_error = 1;
977 return;
978 }
979 record_beneath_to_resume (record_beneath_to_resume_ops, ptid, 1,
980 signal);
981 }
982 }
983
984 static int record_get_sig = 0;
985
986 /* SIGINT signal handler, registered by "to_wait" method. */
987
988 static void
989 record_sig_handler (int signo)
990 {
991 if (record_debug)
992 fprintf_unfiltered (gdb_stdlog, "Process record: get a signal\n");
993
994 /* It will break the running inferior in replay mode. */
995 record_resume_step = 1;
996
997 /* It will let record_wait set inferior status to get the signal
998 SIGINT. */
999 record_get_sig = 1;
1000 }
1001
1002 static void
1003 record_wait_cleanups (void *ignore)
1004 {
1005 if (execution_direction == EXEC_REVERSE)
1006 {
1007 if (record_list->next)
1008 record_list = record_list->next;
1009 }
1010 else
1011 record_list = record_list->prev;
1012 }
1013
1014 /* "to_wait" target method for process record target.
1015
1016 In record mode, the target is always run in singlestep mode
1017 (even when gdb says to continue). The to_wait method intercepts
1018 the stop events and determines which ones are to be passed on to
1019 gdb. Most stop events are just singlestep events that gdb is not
1020 to know about, so the to_wait method just records them and keeps
1021 singlestepping.
1022
1023 In replay mode, this function emulates the recorded execution log,
1024 one instruction at a time (forward or backward), and determines
1025 where to stop. */
1026
1027 static ptid_t
1028 record_wait (struct target_ops *ops,
1029 ptid_t ptid, struct target_waitstatus *status,
1030 int options)
1031 {
1032 struct cleanup *set_cleanups = record_gdb_operation_disable_set ();
1033
1034 if (record_debug)
1035 fprintf_unfiltered (gdb_stdlog,
1036 "Process record: record_wait "
1037 "record_resume_step = %d\n",
1038 record_resume_step);
1039
1040 if (!RECORD_IS_REPLAY && ops != &record_core_ops)
1041 {
1042 if (record_resume_error)
1043 {
1044 /* If record_resume get error, return directly. */
1045 status->kind = TARGET_WAITKIND_STOPPED;
1046 status->value.sig = TARGET_SIGNAL_ABRT;
1047 return inferior_ptid;
1048 }
1049
1050 if (record_resume_step)
1051 {
1052 /* This is a single step. */
1053 return record_beneath_to_wait (record_beneath_to_wait_ops,
1054 ptid, status, options);
1055 }
1056 else
1057 {
1058 /* This is not a single step. */
1059 ptid_t ret;
1060 CORE_ADDR tmp_pc;
1061
1062 while (1)
1063 {
1064 ret = record_beneath_to_wait (record_beneath_to_wait_ops,
1065 ptid, status, options);
1066
1067 /* Is this a SIGTRAP? */
1068 if (status->kind == TARGET_WAITKIND_STOPPED
1069 && status->value.sig == TARGET_SIGNAL_TRAP)
1070 {
1071 struct regcache *regcache;
1072
1073 /* Yes -- check if there is a breakpoint. */
1074 registers_changed ();
1075 regcache = get_current_regcache ();
1076 tmp_pc = regcache_read_pc (regcache);
1077 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache),
1078 tmp_pc))
1079 {
1080 /* There is a breakpoint. GDB will want to stop. */
1081 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1082 CORE_ADDR decr_pc_after_break
1083 = gdbarch_decr_pc_after_break (gdbarch);
1084 if (decr_pc_after_break)
1085 regcache_write_pc (regcache,
1086 tmp_pc + decr_pc_after_break);
1087 }
1088 else
1089 {
1090 /* There is not a breakpoint, and gdb is not
1091 stepping, therefore gdb will not stop.
1092 Therefore we will not return to gdb.
1093 Record the insn and resume. */
1094 if (!do_record_message (regcache, TARGET_SIGNAL_0))
1095 break;
1096
1097 record_beneath_to_resume (record_beneath_to_resume_ops,
1098 ptid, 1,
1099 TARGET_SIGNAL_0);
1100 continue;
1101 }
1102 }
1103
1104 /* The inferior is broken by a breakpoint or a signal. */
1105 break;
1106 }
1107
1108 return ret;
1109 }
1110 }
1111 else
1112 {
1113 struct regcache *regcache = get_current_regcache ();
1114 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1115 int continue_flag = 1;
1116 int first_record_end = 1;
1117 struct cleanup *old_cleanups = make_cleanup (record_wait_cleanups, 0);
1118 CORE_ADDR tmp_pc;
1119
1120 status->kind = TARGET_WAITKIND_STOPPED;
1121
1122 /* Check breakpoint when forward execute. */
1123 if (execution_direction == EXEC_FORWARD)
1124 {
1125 tmp_pc = regcache_read_pc (regcache);
1126 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache),
1127 tmp_pc))
1128 {
1129 if (record_debug)
1130 fprintf_unfiltered (gdb_stdlog,
1131 "Process record: break at %s.\n",
1132 paddress (gdbarch, tmp_pc));
1133 if (gdbarch_decr_pc_after_break (gdbarch)
1134 && !record_resume_step)
1135 regcache_write_pc (regcache,
1136 tmp_pc +
1137 gdbarch_decr_pc_after_break (gdbarch));
1138 goto replay_out;
1139 }
1140 }
1141
1142 record_get_sig = 0;
1143 signal (SIGINT, record_sig_handler);
1144 /* If GDB is in terminal_inferior mode, it will not get the signal.
1145 And in GDB replay mode, GDB doesn't need to be in terminal_inferior
1146 mode, because inferior will not executed.
1147 Then set it to terminal_ours to make GDB get the signal. */
1148 target_terminal_ours ();
1149
1150 /* In EXEC_FORWARD mode, record_list points to the tail of prev
1151 instruction. */
1152 if (execution_direction == EXEC_FORWARD && record_list->next)
1153 record_list = record_list->next;
1154
1155 /* Loop over the record_list, looking for the next place to
1156 stop. */
1157 do
1158 {
1159 /* Check for beginning and end of log. */
1160 if (execution_direction == EXEC_REVERSE
1161 && record_list == &record_first)
1162 {
1163 /* Hit beginning of record log in reverse. */
1164 status->kind = TARGET_WAITKIND_NO_HISTORY;
1165 break;
1166 }
1167 if (execution_direction != EXEC_REVERSE && !record_list->next)
1168 {
1169 /* Hit end of record log going forward. */
1170 status->kind = TARGET_WAITKIND_NO_HISTORY;
1171 break;
1172 }
1173
1174 record_exec_insn (regcache, gdbarch, record_list);
1175
1176 if (record_list->type == record_end)
1177 {
1178 if (record_debug > 1)
1179 fprintf_unfiltered (gdb_stdlog,
1180 "Process record: record_end %s to "
1181 "inferior.\n",
1182 host_address_to_string (record_list));
1183
1184 if (first_record_end && execution_direction == EXEC_REVERSE)
1185 {
1186 /* When reverse excute, the first record_end is the part of
1187 current instruction. */
1188 first_record_end = 0;
1189 }
1190 else
1191 {
1192 /* In EXEC_REVERSE mode, this is the record_end of prev
1193 instruction.
1194 In EXEC_FORWARD mode, this is the record_end of current
1195 instruction. */
1196 /* step */
1197 if (record_resume_step)
1198 {
1199 if (record_debug > 1)
1200 fprintf_unfiltered (gdb_stdlog,
1201 "Process record: step.\n");
1202 continue_flag = 0;
1203 }
1204
1205 /* check breakpoint */
1206 tmp_pc = regcache_read_pc (regcache);
1207 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache),
1208 tmp_pc))
1209 {
1210 if (record_debug)
1211 fprintf_unfiltered (gdb_stdlog,
1212 "Process record: break "
1213 "at %s.\n",
1214 paddress (gdbarch, tmp_pc));
1215 if (gdbarch_decr_pc_after_break (gdbarch)
1216 && execution_direction == EXEC_FORWARD
1217 && !record_resume_step)
1218 regcache_write_pc (regcache,
1219 tmp_pc +
1220 gdbarch_decr_pc_after_break (gdbarch));
1221 continue_flag = 0;
1222 }
1223 /* Check target signal */
1224 if (record_list->u.end.sigval != TARGET_SIGNAL_0)
1225 /* FIXME: better way to check */
1226 continue_flag = 0;
1227 }
1228 }
1229
1230 if (continue_flag)
1231 {
1232 if (execution_direction == EXEC_REVERSE)
1233 {
1234 if (record_list->prev)
1235 record_list = record_list->prev;
1236 }
1237 else
1238 {
1239 if (record_list->next)
1240 record_list = record_list->next;
1241 }
1242 }
1243 }
1244 while (continue_flag);
1245
1246 signal (SIGINT, handle_sigint);
1247
1248 replay_out:
1249 if (record_get_sig)
1250 status->value.sig = TARGET_SIGNAL_INT;
1251 else if (record_list->u.end.sigval != TARGET_SIGNAL_0)
1252 /* FIXME: better way to check */
1253 status->value.sig = record_list->u.end.sigval;
1254 else
1255 status->value.sig = TARGET_SIGNAL_TRAP;
1256
1257 discard_cleanups (old_cleanups);
1258 }
1259
1260 do_cleanups (set_cleanups);
1261 return inferior_ptid;
1262 }
1263
1264 /* "to_disconnect" method for process record target. */
1265
1266 static void
1267 record_disconnect (struct target_ops *target, char *args, int from_tty)
1268 {
1269 if (record_debug)
1270 fprintf_unfiltered (gdb_stdlog, "Process record: record_disconnect\n");
1271
1272 unpush_target (&record_ops);
1273 target_disconnect (args, from_tty);
1274 }
1275
1276 /* "to_detach" method for process record target. */
1277
1278 static void
1279 record_detach (struct target_ops *ops, char *args, int from_tty)
1280 {
1281 if (record_debug)
1282 fprintf_unfiltered (gdb_stdlog, "Process record: record_detach\n");
1283
1284 unpush_target (&record_ops);
1285 target_detach (args, from_tty);
1286 }
1287
1288 /* "to_mourn_inferior" method for process record target. */
1289
1290 static void
1291 record_mourn_inferior (struct target_ops *ops)
1292 {
1293 if (record_debug)
1294 fprintf_unfiltered (gdb_stdlog, "Process record: "
1295 "record_mourn_inferior\n");
1296
1297 unpush_target (&record_ops);
1298 target_mourn_inferior ();
1299 }
1300
1301 /* Close process record target before killing the inferior process. */
1302
1303 static void
1304 record_kill (struct target_ops *ops)
1305 {
1306 if (record_debug)
1307 fprintf_unfiltered (gdb_stdlog, "Process record: record_kill\n");
1308
1309 unpush_target (&record_ops);
1310 target_kill ();
1311 }
1312
1313 /* Record registers change (by user or by GDB) to list as an instruction. */
1314
1315 static void
1316 record_registers_change (struct regcache *regcache, int regnum)
1317 {
1318 /* Check record_insn_num. */
1319 record_check_insn_num (0);
1320
1321 record_arch_list_head = NULL;
1322 record_arch_list_tail = NULL;
1323
1324 if (regnum < 0)
1325 {
1326 int i;
1327 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
1328 {
1329 if (record_arch_list_add_reg (regcache, i))
1330 {
1331 record_list_release (record_arch_list_tail);
1332 error (_("Process record: failed to record execution log."));
1333 }
1334 }
1335 }
1336 else
1337 {
1338 if (record_arch_list_add_reg (regcache, regnum))
1339 {
1340 record_list_release (record_arch_list_tail);
1341 error (_("Process record: failed to record execution log."));
1342 }
1343 }
1344 if (record_arch_list_add_end ())
1345 {
1346 record_list_release (record_arch_list_tail);
1347 error (_("Process record: failed to record execution log."));
1348 }
1349 record_list->next = record_arch_list_head;
1350 record_arch_list_head->prev = record_list;
1351 record_list = record_arch_list_tail;
1352
1353 if (record_insn_num == record_insn_max_num && record_insn_max_num)
1354 record_list_release_first ();
1355 else
1356 record_insn_num++;
1357 }
1358
1359 /* "to_store_registers" method for process record target. */
1360
1361 static void
1362 record_store_registers (struct target_ops *ops, struct regcache *regcache,
1363 int regno)
1364 {
1365 if (!record_gdb_operation_disable)
1366 {
1367 if (RECORD_IS_REPLAY)
1368 {
1369 int n;
1370
1371 /* Let user choose if he wants to write register or not. */
1372 if (regno < 0)
1373 n =
1374 query (_("Because GDB is in replay mode, changing the "
1375 "value of a register will make the execution "
1376 "log unusable from this point onward. "
1377 "Change all registers?"));
1378 else
1379 n =
1380 query (_("Because GDB is in replay mode, changing the value "
1381 "of a register will make the execution log unusable "
1382 "from this point onward. Change register %s?"),
1383 gdbarch_register_name (get_regcache_arch (regcache),
1384 regno));
1385
1386 if (!n)
1387 {
1388 /* Invalidate the value of regcache that was set in function
1389 "regcache_raw_write". */
1390 if (regno < 0)
1391 {
1392 int i;
1393 for (i = 0;
1394 i < gdbarch_num_regs (get_regcache_arch (regcache));
1395 i++)
1396 regcache_invalidate (regcache, i);
1397 }
1398 else
1399 regcache_invalidate (regcache, regno);
1400
1401 error (_("Process record canceled the operation."));
1402 }
1403
1404 /* Destroy the record from here forward. */
1405 record_list_release_following (record_list);
1406 }
1407
1408 record_registers_change (regcache, regno);
1409 }
1410 record_beneath_to_store_registers (record_beneath_to_store_registers_ops,
1411 regcache, regno);
1412 }
1413
1414 /* "to_xfer_partial" method. Behavior is conditional on RECORD_IS_REPLAY.
1415 In replay mode, we cannot write memory unles we are willing to
1416 invalidate the record/replay log from this point forward. */
1417
1418 static LONGEST
1419 record_xfer_partial (struct target_ops *ops, enum target_object object,
1420 const char *annex, gdb_byte *readbuf,
1421 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
1422 {
1423 if (!record_gdb_operation_disable
1424 && (object == TARGET_OBJECT_MEMORY
1425 || object == TARGET_OBJECT_RAW_MEMORY) && writebuf)
1426 {
1427 if (RECORD_IS_REPLAY)
1428 {
1429 /* Let user choose if he wants to write memory or not. */
1430 if (!query (_("Because GDB is in replay mode, writing to memory "
1431 "will make the execution log unusable from this "
1432 "point onward. Write memory at address %s?"),
1433 paddress (target_gdbarch, offset)))
1434 error (_("Process record canceled the operation."));
1435
1436 /* Destroy the record from here forward. */
1437 record_list_release_following (record_list);
1438 }
1439
1440 /* Check record_insn_num */
1441 record_check_insn_num (0);
1442
1443 /* Record registers change to list as an instruction. */
1444 record_arch_list_head = NULL;
1445 record_arch_list_tail = NULL;
1446 if (record_arch_list_add_mem (offset, len))
1447 {
1448 record_list_release (record_arch_list_tail);
1449 if (record_debug)
1450 fprintf_unfiltered (gdb_stdlog,
1451 "Process record: failed to record "
1452 "execution log.");
1453 return -1;
1454 }
1455 if (record_arch_list_add_end ())
1456 {
1457 record_list_release (record_arch_list_tail);
1458 if (record_debug)
1459 fprintf_unfiltered (gdb_stdlog,
1460 "Process record: failed to record "
1461 "execution log.");
1462 return -1;
1463 }
1464 record_list->next = record_arch_list_head;
1465 record_arch_list_head->prev = record_list;
1466 record_list = record_arch_list_tail;
1467
1468 if (record_insn_num == record_insn_max_num && record_insn_max_num)
1469 record_list_release_first ();
1470 else
1471 record_insn_num++;
1472 }
1473
1474 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops,
1475 object, annex, readbuf, writebuf,
1476 offset, len);
1477 }
1478
1479 /* Behavior is conditional on RECORD_IS_REPLAY.
1480 We will not actually insert or remove breakpoints when replaying,
1481 nor when recording. */
1482
1483 static int
1484 record_insert_breakpoint (struct gdbarch *gdbarch,
1485 struct bp_target_info *bp_tgt)
1486 {
1487 if (!RECORD_IS_REPLAY)
1488 {
1489 struct cleanup *old_cleanups = record_gdb_operation_disable_set ();
1490 int ret = record_beneath_to_insert_breakpoint (gdbarch, bp_tgt);
1491
1492 do_cleanups (old_cleanups);
1493
1494 return ret;
1495 }
1496
1497 return 0;
1498 }
1499
1500 /* "to_remove_breakpoint" method for process record target. */
1501
1502 static int
1503 record_remove_breakpoint (struct gdbarch *gdbarch,
1504 struct bp_target_info *bp_tgt)
1505 {
1506 if (!RECORD_IS_REPLAY)
1507 {
1508 struct cleanup *old_cleanups = record_gdb_operation_disable_set ();
1509 int ret = record_beneath_to_remove_breakpoint (gdbarch, bp_tgt);
1510
1511 do_cleanups (old_cleanups);
1512
1513 return ret;
1514 }
1515
1516 return 0;
1517 }
1518
1519 /* "to_can_execute_reverse" method for process record target. */
1520
1521 static int
1522 record_can_execute_reverse (void)
1523 {
1524 return 1;
1525 }
1526
1527 static void
1528 init_record_ops (void)
1529 {
1530 record_ops.to_shortname = "record";
1531 record_ops.to_longname = "Process record and replay target";
1532 record_ops.to_doc =
1533 "Log program while executing and replay execution from log.";
1534 record_ops.to_open = record_open;
1535 record_ops.to_close = record_close;
1536 record_ops.to_resume = record_resume;
1537 record_ops.to_wait = record_wait;
1538 record_ops.to_disconnect = record_disconnect;
1539 record_ops.to_detach = record_detach;
1540 record_ops.to_mourn_inferior = record_mourn_inferior;
1541 record_ops.to_kill = record_kill;
1542 record_ops.to_create_inferior = find_default_create_inferior;
1543 record_ops.to_store_registers = record_store_registers;
1544 record_ops.to_xfer_partial = record_xfer_partial;
1545 record_ops.to_insert_breakpoint = record_insert_breakpoint;
1546 record_ops.to_remove_breakpoint = record_remove_breakpoint;
1547 record_ops.to_can_execute_reverse = record_can_execute_reverse;
1548 record_ops.to_stratum = record_stratum;
1549 record_ops.to_magic = OPS_MAGIC;
1550 }
1551
1552 /* "to_resume" method for prec over corefile. */
1553
1554 static void
1555 record_core_resume (struct target_ops *ops, ptid_t ptid, int step,
1556 enum target_signal signal)
1557 {
1558 record_resume_step = step;
1559 }
1560
1561 /* "to_kill" method for prec over corefile. */
1562
1563 static void
1564 record_core_kill (struct target_ops *ops)
1565 {
1566 if (record_debug)
1567 fprintf_unfiltered (gdb_stdlog, "Process record: record_core_kill\n");
1568
1569 unpush_target (&record_core_ops);
1570 }
1571
1572 /* "to_fetch_registers" method for prec over corefile. */
1573
1574 static void
1575 record_core_fetch_registers (struct target_ops *ops,
1576 struct regcache *regcache,
1577 int regno)
1578 {
1579 if (regno < 0)
1580 {
1581 int num = gdbarch_num_regs (get_regcache_arch (regcache));
1582 int i;
1583
1584 for (i = 0; i < num; i ++)
1585 regcache_raw_supply (regcache, i,
1586 record_core_regbuf + MAX_REGISTER_SIZE * i);
1587 }
1588 else
1589 regcache_raw_supply (regcache, regno,
1590 record_core_regbuf + MAX_REGISTER_SIZE * regno);
1591 }
1592
1593 /* "to_prepare_to_store" method for prec over corefile. */
1594
1595 static void
1596 record_core_prepare_to_store (struct regcache *regcache)
1597 {
1598 }
1599
1600 /* "to_store_registers" method for prec over corefile. */
1601
1602 static void
1603 record_core_store_registers (struct target_ops *ops,
1604 struct regcache *regcache,
1605 int regno)
1606 {
1607 if (record_gdb_operation_disable)
1608 regcache_raw_collect (regcache, regno,
1609 record_core_regbuf + MAX_REGISTER_SIZE * regno);
1610 else
1611 error (_("You can't do that without a process to debug."));
1612 }
1613
1614 /* "to_xfer_partial" method for prec over corefile. */
1615
1616 static LONGEST
1617 record_core_xfer_partial (struct target_ops *ops, enum target_object object,
1618 const char *annex, gdb_byte *readbuf,
1619 const gdb_byte *writebuf, ULONGEST offset,
1620 LONGEST len)
1621 {
1622 if (object == TARGET_OBJECT_MEMORY)
1623 {
1624 if (record_gdb_operation_disable || !writebuf)
1625 {
1626 struct target_section *p;
1627 for (p = record_core_start; p < record_core_end; p++)
1628 {
1629 if (offset >= p->addr)
1630 {
1631 struct record_core_buf_entry *entry;
1632 ULONGEST sec_offset;
1633
1634 if (offset >= p->endaddr)
1635 continue;
1636
1637 if (offset + len > p->endaddr)
1638 len = p->endaddr - offset;
1639
1640 sec_offset = offset - p->addr;
1641
1642 /* Read readbuf or write writebuf p, offset, len. */
1643 /* Check flags. */
1644 if (p->the_bfd_section->flags & SEC_CONSTRUCTOR
1645 || (p->the_bfd_section->flags & SEC_HAS_CONTENTS) == 0)
1646 {
1647 if (readbuf)
1648 memset (readbuf, 0, len);
1649 return len;
1650 }
1651 /* Get record_core_buf_entry. */
1652 for (entry = record_core_buf_list; entry;
1653 entry = entry->prev)
1654 if (entry->p == p)
1655 break;
1656 if (writebuf)
1657 {
1658 if (!entry)
1659 {
1660 /* Add a new entry. */
1661 entry
1662 = (struct record_core_buf_entry *)
1663 xmalloc
1664 (sizeof (struct record_core_buf_entry));
1665 entry->p = p;
1666 if (!bfd_malloc_and_get_section (p->bfd,
1667 p->the_bfd_section,
1668 &entry->buf))
1669 {
1670 xfree (entry);
1671 return 0;
1672 }
1673 entry->prev = record_core_buf_list;
1674 record_core_buf_list = entry;
1675 }
1676
1677 memcpy (entry->buf + sec_offset, writebuf,
1678 (size_t) len);
1679 }
1680 else
1681 {
1682 if (!entry)
1683 return record_beneath_to_xfer_partial
1684 (record_beneath_to_xfer_partial_ops,
1685 object, annex, readbuf, writebuf,
1686 offset, len);
1687
1688 memcpy (readbuf, entry->buf + sec_offset,
1689 (size_t) len);
1690 }
1691
1692 return len;
1693 }
1694 }
1695
1696 return -1;
1697 }
1698 else
1699 error (_("You can't do that without a process to debug."));
1700 }
1701
1702 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops,
1703 object, annex, readbuf, writebuf,
1704 offset, len);
1705 }
1706
1707 /* "to_insert_breakpoint" method for prec over corefile. */
1708
1709 static int
1710 record_core_insert_breakpoint (struct gdbarch *gdbarch,
1711 struct bp_target_info *bp_tgt)
1712 {
1713 return 0;
1714 }
1715
1716 /* "to_remove_breakpoint" method for prec over corefile. */
1717
1718 static int
1719 record_core_remove_breakpoint (struct gdbarch *gdbarch,
1720 struct bp_target_info *bp_tgt)
1721 {
1722 return 0;
1723 }
1724
1725 /* "to_has_execution" method for prec over corefile. */
1726
1727 int
1728 record_core_has_execution (struct target_ops *ops)
1729 {
1730 return 1;
1731 }
1732
1733 static void
1734 init_record_core_ops (void)
1735 {
1736 record_core_ops.to_shortname = "record_core";
1737 record_core_ops.to_longname = "Process record and replay target";
1738 record_core_ops.to_doc =
1739 "Log program while executing and replay execution from log.";
1740 record_core_ops.to_open = record_open;
1741 record_core_ops.to_close = record_close;
1742 record_core_ops.to_resume = record_core_resume;
1743 record_core_ops.to_wait = record_wait;
1744 record_core_ops.to_kill = record_core_kill;
1745 record_core_ops.to_fetch_registers = record_core_fetch_registers;
1746 record_core_ops.to_prepare_to_store = record_core_prepare_to_store;
1747 record_core_ops.to_store_registers = record_core_store_registers;
1748 record_core_ops.to_xfer_partial = record_core_xfer_partial;
1749 record_core_ops.to_insert_breakpoint = record_core_insert_breakpoint;
1750 record_core_ops.to_remove_breakpoint = record_core_remove_breakpoint;
1751 record_core_ops.to_can_execute_reverse = record_can_execute_reverse;
1752 record_core_ops.to_has_execution = record_core_has_execution;
1753 record_core_ops.to_stratum = record_stratum;
1754 record_core_ops.to_magic = OPS_MAGIC;
1755 }
1756
1757 /* Implement "show record debug" command. */
1758
1759 static void
1760 show_record_debug (struct ui_file *file, int from_tty,
1761 struct cmd_list_element *c, const char *value)
1762 {
1763 fprintf_filtered (file, _("Debugging of process record target is %s.\n"),
1764 value);
1765 }
1766
1767 /* Alias for "target record". */
1768
1769 static void
1770 cmd_record_start (char *args, int from_tty)
1771 {
1772 execute_command ("target record", from_tty);
1773 }
1774
1775 /* Truncate the record log from the present point
1776 of replay until the end. */
1777
1778 static void
1779 cmd_record_delete (char *args, int from_tty)
1780 {
1781 if (current_target.to_stratum == record_stratum)
1782 {
1783 if (RECORD_IS_REPLAY)
1784 {
1785 if (!from_tty || query (_("Delete the log from this point forward "
1786 "and begin to record the running message "
1787 "at current PC?")))
1788 record_list_release_following (record_list);
1789 }
1790 else
1791 printf_unfiltered (_("Already at end of record list.\n"));
1792
1793 }
1794 else
1795 printf_unfiltered (_("Process record is not started.\n"));
1796 }
1797
1798 /* Implement the "stoprecord" or "record stop" command. */
1799
1800 static void
1801 cmd_record_stop (char *args, int from_tty)
1802 {
1803 if (current_target.to_stratum == record_stratum)
1804 {
1805 unpush_target (&record_ops);
1806 printf_unfiltered (_("Process record is stopped and all execution "
1807 "logs are deleted.\n"));
1808 }
1809 else
1810 printf_unfiltered (_("Process record is not started.\n"));
1811 }
1812
1813 /* Set upper limit of record log size. */
1814
1815 static void
1816 set_record_insn_max_num (char *args, int from_tty, struct cmd_list_element *c)
1817 {
1818 if (record_insn_num > record_insn_max_num && record_insn_max_num)
1819 {
1820 /* Count down record_insn_num while releasing records from list. */
1821 while (record_insn_num > record_insn_max_num)
1822 {
1823 record_list_release_first ();
1824 record_insn_num--;
1825 }
1826 }
1827 }
1828
1829 static struct cmd_list_element *record_cmdlist, *set_record_cmdlist,
1830 *show_record_cmdlist, *info_record_cmdlist;
1831
1832 static void
1833 set_record_command (char *args, int from_tty)
1834 {
1835 printf_unfiltered (_("\
1836 \"set record\" must be followed by an apporpriate subcommand.\n"));
1837 help_list (set_record_cmdlist, "set record ", all_commands, gdb_stdout);
1838 }
1839
1840 static void
1841 show_record_command (char *args, int from_tty)
1842 {
1843 cmd_show_list (show_record_cmdlist, from_tty, "");
1844 }
1845
1846 /* Display some statistics about the execution log. */
1847
1848 static void
1849 info_record_command (char *args, int from_tty)
1850 {
1851 struct record_entry *p;
1852
1853 if (current_target.to_stratum == record_stratum)
1854 {
1855 if (RECORD_IS_REPLAY)
1856 printf_filtered (_("Replay mode:\n"));
1857 else
1858 printf_filtered (_("Record mode:\n"));
1859
1860 /* Find entry for first actual instruction in the log. */
1861 for (p = record_first.next;
1862 p != NULL && p->type != record_end;
1863 p = p->next)
1864 ;
1865
1866 /* Do we have a log at all? */
1867 if (p != NULL && p->type == record_end)
1868 {
1869 /* Display instruction number for first instruction in the log. */
1870 printf_filtered (_("Lowest recorded instruction number is %s.\n"),
1871 pulongest (p->u.end.insn_num));
1872
1873 /* If in replay mode, display where we are in the log. */
1874 if (RECORD_IS_REPLAY)
1875 printf_filtered (_("Current instruction number is %s.\n"),
1876 pulongest (record_list->u.end.insn_num));
1877
1878 /* Display instruction number for last instruction in the log. */
1879 printf_filtered (_("Highest recorded instruction number is %s.\n"),
1880 pulongest (record_insn_count));
1881
1882 /* Display log count. */
1883 printf_filtered (_("Log contains %d instructions.\n"),
1884 record_insn_num);
1885 }
1886 else
1887 {
1888 printf_filtered (_("No instructions have been logged.\n"));
1889 }
1890 }
1891 else
1892 {
1893 printf_filtered (_("target record is not active.\n"));
1894 }
1895
1896 /* Display max log size. */
1897 printf_filtered (_("Max logged instructions is %d.\n"),
1898 record_insn_max_num);
1899 }
1900
1901 /* Record log save-file format
1902 Version 1 (never released)
1903
1904 Header:
1905 4 bytes: magic number htonl(0x20090829).
1906 NOTE: be sure to change whenever this file format changes!
1907
1908 Records:
1909 record_end:
1910 1 byte: record type (record_end, see enum record_type).
1911 record_reg:
1912 1 byte: record type (record_reg, see enum record_type).
1913 8 bytes: register id (network byte order).
1914 MAX_REGISTER_SIZE bytes: register value.
1915 record_mem:
1916 1 byte: record type (record_mem, see enum record_type).
1917 8 bytes: memory length (network byte order).
1918 8 bytes: memory address (network byte order).
1919 n bytes: memory value (n == memory length).
1920
1921 Version 2
1922 4 bytes: magic number netorder32(0x20091016).
1923 NOTE: be sure to change whenever this file format changes!
1924
1925 Records:
1926 record_end:
1927 1 byte: record type (record_end, see enum record_type).
1928 4 bytes: signal
1929 4 bytes: instruction count
1930 record_reg:
1931 1 byte: record type (record_reg, see enum record_type).
1932 4 bytes: register id (network byte order).
1933 n bytes: register value (n == actual register size).
1934 (eg. 4 bytes for x86 general registers).
1935 record_mem:
1936 1 byte: record type (record_mem, see enum record_type).
1937 4 bytes: memory length (network byte order).
1938 8 bytes: memory address (network byte order).
1939 n bytes: memory value (n == memory length).
1940
1941 */
1942
1943 /* bfdcore_read -- read bytes from a core file section. */
1944
1945 static inline void
1946 bfdcore_read (bfd *obfd, asection *osec, void *buf, int len, int *offset)
1947 {
1948 int ret = bfd_get_section_contents (obfd, osec, buf, *offset, len);
1949
1950 if (ret)
1951 *offset += len;
1952 else
1953 error (_("Failed to read %d bytes from core file %s ('%s').\n"),
1954 len, bfd_get_filename (obfd),
1955 bfd_errmsg (bfd_get_error ()));
1956 }
1957
1958 static inline uint64_t
1959 netorder64 (uint64_t fromfile)
1960 {
1961 return (BYTE_ORDER == BFD_ENDIAN_LITTLE)
1962 ? bswap_64 (fromfile)
1963 : fromfile;
1964 }
1965
1966 static inline uint32_t
1967 netorder32 (uint32_t fromfile)
1968 {
1969 return (BYTE_ORDER == BFD_ENDIAN_LITTLE)
1970 ? bswap_32 (fromfile)
1971 : fromfile;
1972 }
1973
1974 static inline uint16_t
1975 netorder16 (uint16_t fromfile)
1976 {
1977 return (BYTE_ORDER == BFD_ENDIAN_LITTLE)
1978 ? bswap_16 (fromfile)
1979 : fromfile;
1980 }
1981
1982 /* Restore the execution log from a core_bfd file. */
1983 static void
1984 record_restore (void)
1985 {
1986 uint32_t magic;
1987 struct cleanup *old_cleanups;
1988 struct record_entry *rec;
1989 asection *osec;
1990 uint32_t osec_size;
1991 int bfd_offset = 0;
1992 struct regcache *regcache;
1993
1994 /* We restore the execution log from the open core bfd,
1995 if there is one. */
1996 if (core_bfd == NULL)
1997 return;
1998
1999 /* "record_restore" can only be called when record list is empty. */
2000 gdb_assert (record_first.next == NULL);
2001
2002 if (record_debug)
2003 printf_filtered ("Restoring recording from core file.\n");
2004
2005 /* Now need to find our special note section. */
2006 osec = bfd_get_section_by_name (core_bfd, "null0");
2007 osec_size = bfd_section_size (core_bfd, osec);
2008 if (record_debug)
2009 printf_filtered ("Find precord section %s.\n",
2010 osec ? "succeeded" : "failed");
2011 if (!osec)
2012 return;
2013 if (record_debug)
2014 printf_filtered ("%s", bfd_section_name (core_bfd, osec));
2015
2016 /* Check the magic code. */
2017 bfdcore_read (core_bfd, osec, &magic, sizeof (magic), &bfd_offset);
2018 if (magic != RECORD_FILE_MAGIC)
2019 error (_("Version mis-match or file format error in core file %s."),
2020 bfd_get_filename (core_bfd));
2021 if (record_debug)
2022 printf_filtered ("\
2023 Reading 4-byte magic cookie RECORD_FILE_MAGIC (0x%s)\n",
2024 phex_nz (netorder32 (magic), 4));
2025
2026 /* Restore the entries in recfd into record_arch_list_head and
2027 record_arch_list_tail. */
2028 record_arch_list_head = NULL;
2029 record_arch_list_tail = NULL;
2030 record_insn_num = 0;
2031 old_cleanups = make_cleanup (record_arch_list_cleanups, 0);
2032 regcache = get_current_regcache ();
2033
2034 while (1)
2035 {
2036 int ret;
2037 uint8_t tmpu8;
2038 uint32_t regnum, len, signal, count;
2039 uint64_t addr;
2040
2041 /* We are finished when offset reaches osec_size. */
2042 if (bfd_offset >= osec_size)
2043 break;
2044 bfdcore_read (core_bfd, osec, &tmpu8, sizeof (tmpu8), &bfd_offset);
2045
2046 switch (tmpu8)
2047 {
2048 case record_reg: /* reg */
2049 /* Get register number to regnum. */
2050 bfdcore_read (core_bfd, osec, &regnum,
2051 sizeof (regnum), &bfd_offset);
2052 regnum = netorder32 (regnum);
2053
2054 rec = record_reg_alloc (regcache, regnum);
2055
2056 /* Get val. */
2057 bfdcore_read (core_bfd, osec, record_get_loc (rec),
2058 rec->u.reg.len, &bfd_offset);
2059
2060 if (record_debug)
2061 printf_filtered ("\
2062 Reading register %d (1 plus %lu plus %d bytes)\n",
2063 rec->u.reg.num,
2064 (unsigned long) sizeof (regnum),
2065 rec->u.reg.len);
2066 break;
2067
2068 case record_mem: /* mem */
2069 /* Get len. */
2070 bfdcore_read (core_bfd, osec, &len,
2071 sizeof (len), &bfd_offset);
2072 len = netorder32 (len);
2073
2074 /* Get addr. */
2075 bfdcore_read (core_bfd, osec, &addr,
2076 sizeof (addr), &bfd_offset);
2077 addr = netorder64 (addr);
2078
2079 rec = record_mem_alloc (addr, len);
2080
2081 /* Get val. */
2082 bfdcore_read (core_bfd, osec, record_get_loc (rec),
2083 rec->u.mem.len, &bfd_offset);
2084
2085 if (record_debug)
2086 printf_filtered ("\
2087 Reading memory %s (1 plus %lu plus %lu plus %d bytes)\n",
2088 paddress (get_current_arch (),
2089 rec->u.mem.addr),
2090 (unsigned long) sizeof (addr),
2091 (unsigned long) sizeof (len),
2092 rec->u.mem.len);
2093 break;
2094
2095 case record_end: /* end */
2096 rec = record_end_alloc ();
2097 record_insn_num ++;
2098
2099 /* Get signal value. */
2100 bfdcore_read (core_bfd, osec, &signal,
2101 sizeof (signal), &bfd_offset);
2102 signal = netorder32 (signal);
2103 rec->u.end.sigval = signal;
2104
2105 /* Get insn count. */
2106 bfdcore_read (core_bfd, osec, &count,
2107 sizeof (count), &bfd_offset);
2108 count = netorder32 (count);
2109 rec->u.end.insn_num = count;
2110 record_insn_count = count + 1;
2111 if (record_debug)
2112 printf_filtered ("\
2113 Reading record_end (1 + %lu + %lu bytes), offset == %s\n",
2114 (unsigned long) sizeof (signal),
2115 (unsigned long) sizeof (count),
2116 paddress (get_current_arch (),
2117 bfd_offset));
2118 break;
2119
2120 default:
2121 error (_("Bad entry type in core file %s."),
2122 bfd_get_filename (core_bfd));
2123 break;
2124 }
2125
2126 /* Add rec to record arch list. */
2127 record_arch_list_add (rec);
2128 }
2129
2130 discard_cleanups (old_cleanups);
2131
2132 /* Add record_arch_list_head to the end of record list. */
2133 record_first.next = record_arch_list_head;
2134 record_arch_list_head->prev = &record_first;
2135 record_arch_list_tail->next = NULL;
2136 record_list = &record_first;
2137
2138 /* Update record_insn_max_num. */
2139 if (record_insn_num > record_insn_max_num)
2140 {
2141 record_insn_max_num = record_insn_num;
2142 warning (_("Auto increase record/replay buffer limit to %d."),
2143 record_insn_max_num);
2144 }
2145
2146 /* Succeeded. */
2147 printf_filtered (_("Restored records from core file %s.\n"),
2148 bfd_get_filename (core_bfd));
2149
2150 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
2151 }
2152
2153 /* bfdcore_write -- write bytes into a core file section. */
2154
2155 static inline void
2156 bfdcore_write (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2157 {
2158 int ret = bfd_set_section_contents (obfd, osec, buf, *offset, len);
2159
2160 if (ret)
2161 *offset += len;
2162 else
2163 error (_("Failed to write %d bytes to core file %s ('%s').\n"),
2164 len, bfd_get_filename (obfd),
2165 bfd_errmsg (bfd_get_error ()));
2166 }
2167
2168 /* Restore the execution log from a file. We use a modified elf
2169 corefile format, with an extra section for our data. */
2170
2171 static void
2172 cmd_record_restore (char *args, int from_tty)
2173 {
2174 core_file_command (args, from_tty);
2175 record_open (args, from_tty);
2176 }
2177
2178 static void
2179 record_save_cleanups (void *data)
2180 {
2181 bfd *obfd = data;
2182 char *pathname = xstrdup (bfd_get_filename (obfd));
2183 bfd_close (obfd);
2184 unlink (pathname);
2185 xfree (pathname);
2186 }
2187
2188 /* Save the execution log to a file. We use a modified elf corefile
2189 format, with an extra section for our data. */
2190
2191 static void
2192 cmd_record_save (char *args, int from_tty)
2193 {
2194 char *recfilename, recfilename_buffer[40];
2195 int recfd;
2196 struct record_entry *cur_record_list;
2197 uint32_t magic;
2198 struct regcache *regcache;
2199 struct gdbarch *gdbarch;
2200 struct cleanup *old_cleanups;
2201 struct cleanup *set_cleanups;
2202 bfd *obfd;
2203 int save_size = 0;
2204 asection *osec = NULL;
2205 int bfd_offset = 0;
2206
2207 if (strcmp (current_target.to_shortname, "record") != 0)
2208 error (_("This command can only be used with target 'record'.\n"
2209 "Use 'target record' first.\n"));
2210
2211 if (args && *args)
2212 recfilename = args;
2213 else
2214 {
2215 /* Default recfile name is "gdb_record.PID". */
2216 snprintf (recfilename_buffer, sizeof (recfilename_buffer),
2217 "gdb_record.%d", PIDGET (inferior_ptid));
2218 recfilename = recfilename_buffer;
2219 }
2220
2221 /* Open the save file. */
2222 if (record_debug)
2223 printf_filtered ("Saving execution log to core file '%s'\n", recfilename);
2224
2225 /* Open the output file. */
2226 obfd = create_gcore_bfd (recfilename);
2227 old_cleanups = make_cleanup (record_save_cleanups, obfd);
2228
2229 /* Save the current record entry to "cur_record_list". */
2230 cur_record_list = record_list;
2231
2232 /* Get the values of regcache and gdbarch. */
2233 regcache = get_current_regcache ();
2234 gdbarch = get_regcache_arch (regcache);
2235
2236 /* Disable the GDB operation record. */
2237 set_cleanups = record_gdb_operation_disable_set ();
2238
2239 /* Reverse execute to the begin of record list. */
2240 while (1)
2241 {
2242 /* Check for beginning and end of log. */
2243 if (record_list == &record_first)
2244 break;
2245
2246 record_exec_insn (regcache, gdbarch, record_list);
2247
2248 if (record_list->prev)
2249 record_list = record_list->prev;
2250 }
2251
2252 /* Compute the size needed for the extra bfd section. */
2253 save_size = 4; /* magic cookie */
2254 for (record_list = record_first.next; record_list;
2255 record_list = record_list->next)
2256 switch (record_list->type)
2257 {
2258 case record_end:
2259 save_size += 1 + 4 + 4;
2260 break;
2261 case record_reg:
2262 save_size += 1 + 4 + record_list->u.reg.len;
2263 break;
2264 case record_mem:
2265 save_size += 1 + 4 + 8 + record_list->u.mem.len;
2266 break;
2267 }
2268
2269 /* Make the new bfd section. */
2270 osec = bfd_make_section_anyway_with_flags (obfd, "precord",
2271 SEC_HAS_CONTENTS
2272 | SEC_READONLY);
2273 if (osec == NULL)
2274 error (_("Failed to create 'precord' section for corefile %s: %s"),
2275 recfilename,
2276 bfd_errmsg (bfd_get_error ()));
2277 bfd_set_section_size (obfd, osec, save_size);
2278 bfd_set_section_vma (obfd, osec, 0);
2279 bfd_set_section_alignment (obfd, osec, 0);
2280 bfd_section_lma (obfd, osec) = 0;
2281
2282 /* Save corefile state. */
2283 write_gcore_file (obfd);
2284
2285 /* Write out the record log. */
2286 /* Write the magic code. */
2287 magic = RECORD_FILE_MAGIC;
2288 if (record_debug)
2289 printf_filtered ("\
2290 Writing 4-byte magic cookie RECORD_FILE_MAGIC (0x%s)\n",
2291 phex_nz (magic, 4));
2292 bfdcore_write (obfd, osec, &magic, sizeof (magic), &bfd_offset);
2293
2294 /* Save the entries to recfd and forward execute to the end of
2295 record list. */
2296 record_list = &record_first;
2297 while (1)
2298 {
2299 /* Save entry. */
2300 if (record_list != &record_first)
2301 {
2302 uint8_t type;
2303 uint32_t regnum, len, signal, count;
2304 uint64_t addr;
2305
2306 type = record_list->type;
2307 bfdcore_write (obfd, osec, &type, sizeof (type), &bfd_offset);
2308
2309 switch (record_list->type)
2310 {
2311 case record_reg: /* reg */
2312 if (record_debug)
2313 printf_filtered ("\
2314 Writing register %d (1 plus %lu plus %d bytes)\n",
2315 record_list->u.reg.num,
2316 (unsigned long) sizeof (regnum),
2317 record_list->u.reg.len);
2318
2319 /* Write regnum. */
2320 regnum = netorder32 (record_list->u.reg.num);
2321 bfdcore_write (obfd, osec, &regnum,
2322 sizeof (regnum), &bfd_offset);
2323
2324 /* Write regval. */
2325 bfdcore_write (obfd, osec, record_get_loc (record_list),
2326 record_list->u.reg.len, &bfd_offset);
2327 break;
2328
2329 case record_mem: /* mem */
2330 if (record_debug)
2331 printf_filtered ("\
2332 Writing memory %s (1 plus %lu plus %lu plus %d bytes)\n",
2333 paddress (gdbarch,
2334 record_list->u.mem.addr),
2335 (unsigned long) sizeof (addr),
2336 (unsigned long) sizeof (len),
2337 record_list->u.mem.len);
2338
2339 /* Write memlen. */
2340 len = netorder32 (record_list->u.mem.len);
2341 bfdcore_write (obfd, osec, &len, sizeof (len), &bfd_offset);
2342
2343 /* Write memaddr. */
2344 addr = netorder64 (record_list->u.mem.addr);
2345 bfdcore_write (obfd, osec, &addr,
2346 sizeof (addr), &bfd_offset);
2347
2348 /* Write memval. */
2349 bfdcore_write (obfd, osec, record_get_loc (record_list),
2350 record_list->u.mem.len, &bfd_offset);
2351 break;
2352
2353 case record_end:
2354 if (record_debug)
2355 printf_filtered ("\
2356 Writing record_end (1 + %lu + %lu bytes)\n",
2357 (unsigned long) sizeof (signal),
2358 (unsigned long) sizeof (count));
2359 /* Write signal value. */
2360 signal = netorder32 (record_list->u.end.sigval);
2361 bfdcore_write (obfd, osec, &signal,
2362 sizeof (signal), &bfd_offset);
2363
2364 /* Write insn count. */
2365 count = netorder32 (record_list->u.end.insn_num);
2366 bfdcore_write (obfd, osec, &count,
2367 sizeof (count), &bfd_offset);
2368 break;
2369 }
2370 }
2371
2372 /* Execute entry. */
2373 record_exec_insn (regcache, gdbarch, record_list);
2374
2375 if (record_list->next)
2376 record_list = record_list->next;
2377 else
2378 break;
2379 }
2380
2381 /* Reverse execute to cur_record_list. */
2382 while (1)
2383 {
2384 /* Check for beginning and end of log. */
2385 if (record_list == cur_record_list)
2386 break;
2387
2388 record_exec_insn (regcache, gdbarch, record_list);
2389
2390 if (record_list->prev)
2391 record_list = record_list->prev;
2392 }
2393
2394 do_cleanups (set_cleanups);
2395 bfd_close (obfd);
2396 discard_cleanups (old_cleanups);
2397
2398 /* Succeeded. */
2399 printf_filtered (_("Saved core file %s with execution log.\n"),
2400 recfilename);
2401 }
2402
2403 void
2404 _initialize_record (void)
2405 {
2406 struct cmd_list_element *c;
2407
2408 /* Init record_first. */
2409 record_first.prev = NULL;
2410 record_first.next = NULL;
2411 record_first.type = record_end;
2412
2413 init_record_ops ();
2414 add_target (&record_ops);
2415 init_record_core_ops ();
2416 add_target (&record_core_ops);
2417
2418 add_setshow_zinteger_cmd ("record", no_class, &record_debug,
2419 _("Set debugging of record/replay feature."),
2420 _("Show debugging of record/replay feature."),
2421 _("When enabled, debugging output for "
2422 "record/replay feature is displayed."),
2423 NULL, show_record_debug, &setdebuglist,
2424 &showdebuglist);
2425
2426 c = add_prefix_cmd ("record", class_obscure, cmd_record_start,
2427 _("Abbreviated form of \"target record\" command."),
2428 &record_cmdlist, "record ", 0, &cmdlist);
2429 set_cmd_completer (c, filename_completer);
2430
2431 add_com_alias ("rec", "record", class_obscure, 1);
2432 add_prefix_cmd ("record", class_support, set_record_command,
2433 _("Set record options"), &set_record_cmdlist,
2434 "set record ", 0, &setlist);
2435 add_alias_cmd ("rec", "record", class_obscure, 1, &setlist);
2436 add_prefix_cmd ("record", class_support, show_record_command,
2437 _("Show record options"), &show_record_cmdlist,
2438 "show record ", 0, &showlist);
2439 add_alias_cmd ("rec", "record", class_obscure, 1, &showlist);
2440 add_prefix_cmd ("record", class_support, info_record_command,
2441 _("Info record options"), &info_record_cmdlist,
2442 "info record ", 0, &infolist);
2443 add_alias_cmd ("rec", "record", class_obscure, 1, &infolist);
2444
2445 c = add_cmd ("save", class_obscure, cmd_record_save,
2446 _("Save the execution log to a file.\n\
2447 Argument is optional filename.\n\
2448 Default filename is 'gdb_record.<process_id>'."),
2449 &record_cmdlist);
2450 set_cmd_completer (c, filename_completer);
2451
2452 c = add_cmd ("restore", class_obscure, cmd_record_restore,
2453 _("Restore the execution log from a file.\n\
2454 Argument is filename. File must be created with 'record save'."),
2455 &record_cmdlist);
2456 set_cmd_completer (c, filename_completer);
2457
2458 add_cmd ("delete", class_obscure, cmd_record_delete,
2459 _("Delete the rest of execution log and start recording it anew."),
2460 &record_cmdlist);
2461 add_alias_cmd ("d", "delete", class_obscure, 1, &record_cmdlist);
2462 add_alias_cmd ("del", "delete", class_obscure, 1, &record_cmdlist);
2463
2464 add_cmd ("stop", class_obscure, cmd_record_stop,
2465 _("Stop the record/replay target."),
2466 &record_cmdlist);
2467 add_alias_cmd ("s", "stop", class_obscure, 1, &record_cmdlist);
2468
2469 /* Record instructions number limit command. */
2470 add_setshow_boolean_cmd ("stop-at-limit", no_class,
2471 &record_stop_at_limit, _("\
2472 Set whether record/replay stops when record/replay buffer becomes full."), _("\
2473 Show whether record/replay stops when record/replay buffer becomes full."), _("\
2474 Default is ON.\n\
2475 When ON, if the record/replay buffer becomes full, ask user what to do.\n\
2476 When OFF, if the record/replay buffer becomes full,\n\
2477 delete the oldest recorded instruction to make room for each new one."),
2478 NULL, NULL,
2479 &set_record_cmdlist, &show_record_cmdlist);
2480 add_setshow_uinteger_cmd ("insn-number-max", no_class,
2481 &record_insn_max_num,
2482 _("Set record/replay buffer limit."),
2483 _("Show record/replay buffer limit."), _("\
2484 Set the maximum number of instructions to be stored in the\n\
2485 record/replay buffer. Zero means unlimited. Default is 200000."),
2486 set_record_insn_max_num,
2487 NULL, &set_record_cmdlist, &show_record_cmdlist);
2488 }