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