2009-10-25 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 /* "to_get_bookmark" method for process record and prec over core. */
1527
1528 static gdb_byte *
1529 record_get_bookmark (char *args, int from_tty)
1530 {
1531 gdb_byte *ret = NULL;
1532
1533 /* Return stringified form of instruction count. */
1534 if (record_list && record_list->type == record_end)
1535 ret = xstrdup (pulongest (record_list->u.end.insn_num));
1536
1537 if (record_debug)
1538 {
1539 if (ret)
1540 fprintf_unfiltered (gdb_stdlog,
1541 "record_get_bookmark returns %s\n", ret);
1542 else
1543 fprintf_unfiltered (gdb_stdlog,
1544 "record_get_bookmark returns NULL\n");
1545 }
1546 return ret;
1547 }
1548
1549 /* The implementation of the command "record goto". */
1550 static void cmd_record_goto (char *, int);
1551
1552 /* "to_goto_bookmark" method for process record and prec over core. */
1553
1554 static void
1555 record_goto_bookmark (gdb_byte *bookmark, int from_tty)
1556 {
1557 if (record_debug)
1558 fprintf_unfiltered (gdb_stdlog,
1559 "record_goto_bookmark receives %s\n", bookmark);
1560
1561 if (bookmark[0] == '\'' || bookmark[0] == '\"')
1562 {
1563 if (bookmark[strlen (bookmark) - 1] != bookmark[0])
1564 error (_("Unbalanced quotes: %s"), bookmark);
1565
1566 /* Strip trailing quote. */
1567 bookmark[strlen (bookmark) - 1] = '\0';
1568 /* Strip leading quote. */
1569 bookmark++;
1570 /* Pass along to cmd_record_goto. */
1571 }
1572
1573 cmd_record_goto ((char *) bookmark, from_tty);
1574 return;
1575 }
1576
1577 static void
1578 init_record_ops (void)
1579 {
1580 record_ops.to_shortname = "record";
1581 record_ops.to_longname = "Process record and replay target";
1582 record_ops.to_doc =
1583 "Log program while executing and replay execution from log.";
1584 record_ops.to_open = record_open;
1585 record_ops.to_close = record_close;
1586 record_ops.to_resume = record_resume;
1587 record_ops.to_wait = record_wait;
1588 record_ops.to_disconnect = record_disconnect;
1589 record_ops.to_detach = record_detach;
1590 record_ops.to_mourn_inferior = record_mourn_inferior;
1591 record_ops.to_kill = record_kill;
1592 record_ops.to_create_inferior = find_default_create_inferior;
1593 record_ops.to_store_registers = record_store_registers;
1594 record_ops.to_xfer_partial = record_xfer_partial;
1595 record_ops.to_insert_breakpoint = record_insert_breakpoint;
1596 record_ops.to_remove_breakpoint = record_remove_breakpoint;
1597 record_ops.to_can_execute_reverse = record_can_execute_reverse;
1598 record_ops.to_stratum = record_stratum;
1599 /* Add bookmark target methods. */
1600 record_ops.to_get_bookmark = record_get_bookmark;
1601 record_ops.to_goto_bookmark = record_goto_bookmark;
1602 record_ops.to_magic = OPS_MAGIC;
1603 }
1604
1605 /* "to_resume" method for prec over corefile. */
1606
1607 static void
1608 record_core_resume (struct target_ops *ops, ptid_t ptid, int step,
1609 enum target_signal signal)
1610 {
1611 record_resume_step = step;
1612 }
1613
1614 /* "to_kill" method for prec over corefile. */
1615
1616 static void
1617 record_core_kill (struct target_ops *ops)
1618 {
1619 if (record_debug)
1620 fprintf_unfiltered (gdb_stdlog, "Process record: record_core_kill\n");
1621
1622 unpush_target (&record_core_ops);
1623 }
1624
1625 /* "to_fetch_registers" method for prec over corefile. */
1626
1627 static void
1628 record_core_fetch_registers (struct target_ops *ops,
1629 struct regcache *regcache,
1630 int regno)
1631 {
1632 if (regno < 0)
1633 {
1634 int num = gdbarch_num_regs (get_regcache_arch (regcache));
1635 int i;
1636
1637 for (i = 0; i < num; i ++)
1638 regcache_raw_supply (regcache, i,
1639 record_core_regbuf + MAX_REGISTER_SIZE * i);
1640 }
1641 else
1642 regcache_raw_supply (regcache, regno,
1643 record_core_regbuf + MAX_REGISTER_SIZE * regno);
1644 }
1645
1646 /* "to_prepare_to_store" method for prec over corefile. */
1647
1648 static void
1649 record_core_prepare_to_store (struct regcache *regcache)
1650 {
1651 }
1652
1653 /* "to_store_registers" method for prec over corefile. */
1654
1655 static void
1656 record_core_store_registers (struct target_ops *ops,
1657 struct regcache *regcache,
1658 int regno)
1659 {
1660 if (record_gdb_operation_disable)
1661 regcache_raw_collect (regcache, regno,
1662 record_core_regbuf + MAX_REGISTER_SIZE * regno);
1663 else
1664 error (_("You can't do that without a process to debug."));
1665 }
1666
1667 /* "to_xfer_partial" method for prec over corefile. */
1668
1669 static LONGEST
1670 record_core_xfer_partial (struct target_ops *ops, enum target_object object,
1671 const char *annex, gdb_byte *readbuf,
1672 const gdb_byte *writebuf, ULONGEST offset,
1673 LONGEST len)
1674 {
1675 if (object == TARGET_OBJECT_MEMORY)
1676 {
1677 if (record_gdb_operation_disable || !writebuf)
1678 {
1679 struct target_section *p;
1680 for (p = record_core_start; p < record_core_end; p++)
1681 {
1682 if (offset >= p->addr)
1683 {
1684 struct record_core_buf_entry *entry;
1685 ULONGEST sec_offset;
1686
1687 if (offset >= p->endaddr)
1688 continue;
1689
1690 if (offset + len > p->endaddr)
1691 len = p->endaddr - offset;
1692
1693 sec_offset = offset - p->addr;
1694
1695 /* Read readbuf or write writebuf p, offset, len. */
1696 /* Check flags. */
1697 if (p->the_bfd_section->flags & SEC_CONSTRUCTOR
1698 || (p->the_bfd_section->flags & SEC_HAS_CONTENTS) == 0)
1699 {
1700 if (readbuf)
1701 memset (readbuf, 0, len);
1702 return len;
1703 }
1704 /* Get record_core_buf_entry. */
1705 for (entry = record_core_buf_list; entry;
1706 entry = entry->prev)
1707 if (entry->p == p)
1708 break;
1709 if (writebuf)
1710 {
1711 if (!entry)
1712 {
1713 /* Add a new entry. */
1714 entry
1715 = (struct record_core_buf_entry *)
1716 xmalloc
1717 (sizeof (struct record_core_buf_entry));
1718 entry->p = p;
1719 if (!bfd_malloc_and_get_section (p->bfd,
1720 p->the_bfd_section,
1721 &entry->buf))
1722 {
1723 xfree (entry);
1724 return 0;
1725 }
1726 entry->prev = record_core_buf_list;
1727 record_core_buf_list = entry;
1728 }
1729
1730 memcpy (entry->buf + sec_offset, writebuf,
1731 (size_t) len);
1732 }
1733 else
1734 {
1735 if (!entry)
1736 return record_beneath_to_xfer_partial
1737 (record_beneath_to_xfer_partial_ops,
1738 object, annex, readbuf, writebuf,
1739 offset, len);
1740
1741 memcpy (readbuf, entry->buf + sec_offset,
1742 (size_t) len);
1743 }
1744
1745 return len;
1746 }
1747 }
1748
1749 return -1;
1750 }
1751 else
1752 error (_("You can't do that without a process to debug."));
1753 }
1754
1755 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops,
1756 object, annex, readbuf, writebuf,
1757 offset, len);
1758 }
1759
1760 /* "to_insert_breakpoint" method for prec over corefile. */
1761
1762 static int
1763 record_core_insert_breakpoint (struct gdbarch *gdbarch,
1764 struct bp_target_info *bp_tgt)
1765 {
1766 return 0;
1767 }
1768
1769 /* "to_remove_breakpoint" method for prec over corefile. */
1770
1771 static int
1772 record_core_remove_breakpoint (struct gdbarch *gdbarch,
1773 struct bp_target_info *bp_tgt)
1774 {
1775 return 0;
1776 }
1777
1778 /* "to_has_execution" method for prec over corefile. */
1779
1780 int
1781 record_core_has_execution (struct target_ops *ops)
1782 {
1783 return 1;
1784 }
1785
1786 static void
1787 init_record_core_ops (void)
1788 {
1789 record_core_ops.to_shortname = "record_core";
1790 record_core_ops.to_longname = "Process record and replay target";
1791 record_core_ops.to_doc =
1792 "Log program while executing and replay execution from log.";
1793 record_core_ops.to_open = record_open;
1794 record_core_ops.to_close = record_close;
1795 record_core_ops.to_resume = record_core_resume;
1796 record_core_ops.to_wait = record_wait;
1797 record_core_ops.to_kill = record_core_kill;
1798 record_core_ops.to_fetch_registers = record_core_fetch_registers;
1799 record_core_ops.to_prepare_to_store = record_core_prepare_to_store;
1800 record_core_ops.to_store_registers = record_core_store_registers;
1801 record_core_ops.to_xfer_partial = record_core_xfer_partial;
1802 record_core_ops.to_insert_breakpoint = record_core_insert_breakpoint;
1803 record_core_ops.to_remove_breakpoint = record_core_remove_breakpoint;
1804 record_core_ops.to_can_execute_reverse = record_can_execute_reverse;
1805 record_core_ops.to_has_execution = record_core_has_execution;
1806 record_core_ops.to_stratum = record_stratum;
1807 /* Add bookmark target methods. */
1808 record_core_ops.to_get_bookmark = record_get_bookmark;
1809 record_core_ops.to_goto_bookmark = record_goto_bookmark;
1810 record_core_ops.to_magic = OPS_MAGIC;
1811 }
1812
1813 /* Implement "show record debug" command. */
1814
1815 static void
1816 show_record_debug (struct ui_file *file, int from_tty,
1817 struct cmd_list_element *c, const char *value)
1818 {
1819 fprintf_filtered (file, _("Debugging of process record target is %s.\n"),
1820 value);
1821 }
1822
1823 /* Alias for "target record". */
1824
1825 static void
1826 cmd_record_start (char *args, int from_tty)
1827 {
1828 execute_command ("target record", from_tty);
1829 }
1830
1831 /* Truncate the record log from the present point
1832 of replay until the end. */
1833
1834 static void
1835 cmd_record_delete (char *args, int from_tty)
1836 {
1837 if (current_target.to_stratum == record_stratum)
1838 {
1839 if (RECORD_IS_REPLAY)
1840 {
1841 if (!from_tty || query (_("Delete the log from this point forward "
1842 "and begin to record the running message "
1843 "at current PC?")))
1844 record_list_release_following (record_list);
1845 }
1846 else
1847 printf_unfiltered (_("Already at end of record list.\n"));
1848
1849 }
1850 else
1851 printf_unfiltered (_("Process record is not started.\n"));
1852 }
1853
1854 /* Implement the "stoprecord" or "record stop" command. */
1855
1856 static void
1857 cmd_record_stop (char *args, int from_tty)
1858 {
1859 if (current_target.to_stratum == record_stratum)
1860 {
1861 unpush_target (&record_ops);
1862 printf_unfiltered (_("Process record is stopped and all execution "
1863 "logs are deleted.\n"));
1864 }
1865 else
1866 printf_unfiltered (_("Process record is not started.\n"));
1867 }
1868
1869 /* Set upper limit of record log size. */
1870
1871 static void
1872 set_record_insn_max_num (char *args, int from_tty, struct cmd_list_element *c)
1873 {
1874 if (record_insn_num > record_insn_max_num && record_insn_max_num)
1875 {
1876 /* Count down record_insn_num while releasing records from list. */
1877 while (record_insn_num > record_insn_max_num)
1878 {
1879 record_list_release_first ();
1880 record_insn_num--;
1881 }
1882 }
1883 }
1884
1885 static struct cmd_list_element *record_cmdlist, *set_record_cmdlist,
1886 *show_record_cmdlist, *info_record_cmdlist;
1887
1888 static void
1889 set_record_command (char *args, int from_tty)
1890 {
1891 printf_unfiltered (_("\
1892 \"set record\" must be followed by an apporpriate subcommand.\n"));
1893 help_list (set_record_cmdlist, "set record ", all_commands, gdb_stdout);
1894 }
1895
1896 static void
1897 show_record_command (char *args, int from_tty)
1898 {
1899 cmd_show_list (show_record_cmdlist, from_tty, "");
1900 }
1901
1902 /* Display some statistics about the execution log. */
1903
1904 static void
1905 info_record_command (char *args, int from_tty)
1906 {
1907 struct record_entry *p;
1908
1909 if (current_target.to_stratum == record_stratum)
1910 {
1911 if (RECORD_IS_REPLAY)
1912 printf_filtered (_("Replay mode:\n"));
1913 else
1914 printf_filtered (_("Record mode:\n"));
1915
1916 /* Find entry for first actual instruction in the log. */
1917 for (p = record_first.next;
1918 p != NULL && p->type != record_end;
1919 p = p->next)
1920 ;
1921
1922 /* Do we have a log at all? */
1923 if (p != NULL && p->type == record_end)
1924 {
1925 /* Display instruction number for first instruction in the log. */
1926 printf_filtered (_("Lowest recorded instruction number is %s.\n"),
1927 pulongest (p->u.end.insn_num));
1928
1929 /* If in replay mode, display where we are in the log. */
1930 if (RECORD_IS_REPLAY)
1931 printf_filtered (_("Current instruction number is %s.\n"),
1932 pulongest (record_list->u.end.insn_num));
1933
1934 /* Display instruction number for last instruction in the log. */
1935 printf_filtered (_("Highest recorded instruction number is %s.\n"),
1936 pulongest (record_insn_count));
1937
1938 /* Display log count. */
1939 printf_filtered (_("Log contains %d instructions.\n"),
1940 record_insn_num);
1941 }
1942 else
1943 {
1944 printf_filtered (_("No instructions have been logged.\n"));
1945 }
1946 }
1947 else
1948 {
1949 printf_filtered (_("target record is not active.\n"));
1950 }
1951
1952 /* Display max log size. */
1953 printf_filtered (_("Max logged instructions is %d.\n"),
1954 record_insn_max_num);
1955 }
1956
1957 /* Record log save-file format
1958 Version 1 (never released)
1959
1960 Header:
1961 4 bytes: magic number htonl(0x20090829).
1962 NOTE: be sure to change whenever this file format changes!
1963
1964 Records:
1965 record_end:
1966 1 byte: record type (record_end, see enum record_type).
1967 record_reg:
1968 1 byte: record type (record_reg, see enum record_type).
1969 8 bytes: register id (network byte order).
1970 MAX_REGISTER_SIZE bytes: register value.
1971 record_mem:
1972 1 byte: record type (record_mem, see enum record_type).
1973 8 bytes: memory length (network byte order).
1974 8 bytes: memory address (network byte order).
1975 n bytes: memory value (n == memory length).
1976
1977 Version 2
1978 4 bytes: magic number netorder32(0x20091016).
1979 NOTE: be sure to change whenever this file format changes!
1980
1981 Records:
1982 record_end:
1983 1 byte: record type (record_end, see enum record_type).
1984 4 bytes: signal
1985 4 bytes: instruction count
1986 record_reg:
1987 1 byte: record type (record_reg, see enum record_type).
1988 4 bytes: register id (network byte order).
1989 n bytes: register value (n == actual register size).
1990 (eg. 4 bytes for x86 general registers).
1991 record_mem:
1992 1 byte: record type (record_mem, see enum record_type).
1993 4 bytes: memory length (network byte order).
1994 8 bytes: memory address (network byte order).
1995 n bytes: memory value (n == memory length).
1996
1997 */
1998
1999 /* bfdcore_read -- read bytes from a core file section. */
2000
2001 static inline void
2002 bfdcore_read (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2003 {
2004 int ret = bfd_get_section_contents (obfd, osec, buf, *offset, len);
2005
2006 if (ret)
2007 *offset += len;
2008 else
2009 error (_("Failed to read %d bytes from core file %s ('%s').\n"),
2010 len, bfd_get_filename (obfd),
2011 bfd_errmsg (bfd_get_error ()));
2012 }
2013
2014 static inline uint64_t
2015 netorder64 (uint64_t input)
2016 {
2017 uint64_t ret;
2018
2019 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2020 BFD_ENDIAN_BIG, input);
2021 return ret;
2022 }
2023
2024 static inline uint32_t
2025 netorder32 (uint32_t input)
2026 {
2027 uint32_t ret;
2028
2029 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2030 BFD_ENDIAN_BIG, input);
2031 return ret;
2032 }
2033
2034 static inline uint16_t
2035 netorder16 (uint16_t input)
2036 {
2037 uint16_t ret;
2038
2039 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2040 BFD_ENDIAN_BIG, input);
2041 return ret;
2042 }
2043
2044 /* Restore the execution log from a core_bfd file. */
2045 static void
2046 record_restore (void)
2047 {
2048 uint32_t magic;
2049 struct cleanup *old_cleanups;
2050 struct record_entry *rec;
2051 asection *osec;
2052 uint32_t osec_size;
2053 int bfd_offset = 0;
2054 struct regcache *regcache;
2055
2056 /* We restore the execution log from the open core bfd,
2057 if there is one. */
2058 if (core_bfd == NULL)
2059 return;
2060
2061 /* "record_restore" can only be called when record list is empty. */
2062 gdb_assert (record_first.next == NULL);
2063
2064 if (record_debug)
2065 fprintf_unfiltered (gdb_stdlog, "Restoring recording from core file.\n");
2066
2067 /* Now need to find our special note section. */
2068 osec = bfd_get_section_by_name (core_bfd, "null0");
2069 osec_size = bfd_section_size (core_bfd, osec);
2070 if (record_debug)
2071 fprintf_unfiltered (gdb_stdlog, "Find precord section %s.\n",
2072 osec ? "succeeded" : "failed");
2073 if (osec == NULL)
2074 return;
2075 if (record_debug)
2076 fprintf_unfiltered (gdb_stdlog, "%s", bfd_section_name (core_bfd, osec));
2077
2078 /* Check the magic code. */
2079 bfdcore_read (core_bfd, osec, &magic, sizeof (magic), &bfd_offset);
2080 if (magic != RECORD_FILE_MAGIC)
2081 error (_("Version mis-match or file format error in core file %s."),
2082 bfd_get_filename (core_bfd));
2083 if (record_debug)
2084 fprintf_unfiltered (gdb_stdlog, "\
2085 Reading 4-byte magic cookie RECORD_FILE_MAGIC (0x%s)\n",
2086 phex_nz (netorder32 (magic), 4));
2087
2088 /* Restore the entries in recfd into record_arch_list_head and
2089 record_arch_list_tail. */
2090 record_arch_list_head = NULL;
2091 record_arch_list_tail = NULL;
2092 record_insn_num = 0;
2093 old_cleanups = make_cleanup (record_arch_list_cleanups, 0);
2094 regcache = get_current_regcache ();
2095
2096 while (1)
2097 {
2098 int ret;
2099 uint8_t tmpu8;
2100 uint32_t regnum, len, signal, count;
2101 uint64_t addr;
2102
2103 /* We are finished when offset reaches osec_size. */
2104 if (bfd_offset >= osec_size)
2105 break;
2106 bfdcore_read (core_bfd, osec, &tmpu8, sizeof (tmpu8), &bfd_offset);
2107
2108 switch (tmpu8)
2109 {
2110 case record_reg: /* reg */
2111 /* Get register number to regnum. */
2112 bfdcore_read (core_bfd, osec, &regnum,
2113 sizeof (regnum), &bfd_offset);
2114 regnum = netorder32 (regnum);
2115
2116 rec = record_reg_alloc (regcache, regnum);
2117
2118 /* Get val. */
2119 bfdcore_read (core_bfd, osec, record_get_loc (rec),
2120 rec->u.reg.len, &bfd_offset);
2121
2122 if (record_debug)
2123 fprintf_unfiltered (gdb_stdlog, "\
2124 Reading register %d (1 plus %lu plus %d bytes)\n",
2125 rec->u.reg.num,
2126 (unsigned long) sizeof (regnum),
2127 rec->u.reg.len);
2128 break;
2129
2130 case record_mem: /* mem */
2131 /* Get len. */
2132 bfdcore_read (core_bfd, osec, &len,
2133 sizeof (len), &bfd_offset);
2134 len = netorder32 (len);
2135
2136 /* Get addr. */
2137 bfdcore_read (core_bfd, osec, &addr,
2138 sizeof (addr), &bfd_offset);
2139 addr = netorder64 (addr);
2140
2141 rec = record_mem_alloc (addr, len);
2142
2143 /* Get val. */
2144 bfdcore_read (core_bfd, osec, record_get_loc (rec),
2145 rec->u.mem.len, &bfd_offset);
2146
2147 if (record_debug)
2148 fprintf_unfiltered (gdb_stdlog, "\
2149 Reading memory %s (1 plus %lu plus %lu plus %d bytes)\n",
2150 paddress (get_current_arch (),
2151 rec->u.mem.addr),
2152 (unsigned long) sizeof (addr),
2153 (unsigned long) sizeof (len),
2154 rec->u.mem.len);
2155 break;
2156
2157 case record_end: /* end */
2158 rec = record_end_alloc ();
2159 record_insn_num ++;
2160
2161 /* Get signal value. */
2162 bfdcore_read (core_bfd, osec, &signal,
2163 sizeof (signal), &bfd_offset);
2164 signal = netorder32 (signal);
2165 rec->u.end.sigval = signal;
2166
2167 /* Get insn count. */
2168 bfdcore_read (core_bfd, osec, &count,
2169 sizeof (count), &bfd_offset);
2170 count = netorder32 (count);
2171 rec->u.end.insn_num = count;
2172 record_insn_count = count + 1;
2173 if (record_debug)
2174 fprintf_unfiltered (gdb_stdlog, "\
2175 Reading record_end (1 + %lu + %lu bytes), offset == %s\n",
2176 (unsigned long) sizeof (signal),
2177 (unsigned long) sizeof (count),
2178 paddress (get_current_arch (),
2179 bfd_offset));
2180 break;
2181
2182 default:
2183 error (_("Bad entry type in core file %s."),
2184 bfd_get_filename (core_bfd));
2185 break;
2186 }
2187
2188 /* Add rec to record arch list. */
2189 record_arch_list_add (rec);
2190 }
2191
2192 discard_cleanups (old_cleanups);
2193
2194 /* Add record_arch_list_head to the end of record list. */
2195 record_first.next = record_arch_list_head;
2196 record_arch_list_head->prev = &record_first;
2197 record_arch_list_tail->next = NULL;
2198 record_list = &record_first;
2199
2200 /* Update record_insn_max_num. */
2201 if (record_insn_num > record_insn_max_num)
2202 {
2203 record_insn_max_num = record_insn_num;
2204 warning (_("Auto increase record/replay buffer limit to %d."),
2205 record_insn_max_num);
2206 }
2207
2208 /* Succeeded. */
2209 printf_filtered (_("Restored records from core file %s.\n"),
2210 bfd_get_filename (core_bfd));
2211
2212 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
2213 }
2214
2215 /* bfdcore_write -- write bytes into a core file section. */
2216
2217 static inline void
2218 bfdcore_write (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2219 {
2220 int ret = bfd_set_section_contents (obfd, osec, buf, *offset, len);
2221
2222 if (ret)
2223 *offset += len;
2224 else
2225 error (_("Failed to write %d bytes to core file %s ('%s').\n"),
2226 len, bfd_get_filename (obfd),
2227 bfd_errmsg (bfd_get_error ()));
2228 }
2229
2230 /* Restore the execution log from a file. We use a modified elf
2231 corefile format, with an extra section for our data. */
2232
2233 static void
2234 cmd_record_restore (char *args, int from_tty)
2235 {
2236 core_file_command (args, from_tty);
2237 record_open (args, from_tty);
2238 }
2239
2240 static void
2241 record_save_cleanups (void *data)
2242 {
2243 bfd *obfd = data;
2244 char *pathname = xstrdup (bfd_get_filename (obfd));
2245 bfd_close (obfd);
2246 unlink (pathname);
2247 xfree (pathname);
2248 }
2249
2250 /* Save the execution log to a file. We use a modified elf corefile
2251 format, with an extra section for our data. */
2252
2253 static void
2254 cmd_record_save (char *args, int from_tty)
2255 {
2256 char *recfilename, recfilename_buffer[40];
2257 int recfd;
2258 struct record_entry *cur_record_list;
2259 uint32_t magic;
2260 struct regcache *regcache;
2261 struct gdbarch *gdbarch;
2262 struct cleanup *old_cleanups;
2263 struct cleanup *set_cleanups;
2264 bfd *obfd;
2265 int save_size = 0;
2266 asection *osec = NULL;
2267 int bfd_offset = 0;
2268
2269 if (strcmp (current_target.to_shortname, "record") != 0)
2270 error (_("This command can only be used with target 'record'.\n"
2271 "Use 'target record' first.\n"));
2272
2273 if (args && *args)
2274 recfilename = args;
2275 else
2276 {
2277 /* Default recfile name is "gdb_record.PID". */
2278 snprintf (recfilename_buffer, sizeof (recfilename_buffer),
2279 "gdb_record.%d", PIDGET (inferior_ptid));
2280 recfilename = recfilename_buffer;
2281 }
2282
2283 /* Open the save file. */
2284 if (record_debug)
2285 fprintf_unfiltered (gdb_stdlog, "Saving execution log to core file '%s'\n",
2286 recfilename);
2287
2288 /* Open the output file. */
2289 obfd = create_gcore_bfd (recfilename);
2290 old_cleanups = make_cleanup (record_save_cleanups, obfd);
2291
2292 /* Save the current record entry to "cur_record_list". */
2293 cur_record_list = record_list;
2294
2295 /* Get the values of regcache and gdbarch. */
2296 regcache = get_current_regcache ();
2297 gdbarch = get_regcache_arch (regcache);
2298
2299 /* Disable the GDB operation record. */
2300 set_cleanups = record_gdb_operation_disable_set ();
2301
2302 /* Reverse execute to the begin of record list. */
2303 while (1)
2304 {
2305 /* Check for beginning and end of log. */
2306 if (record_list == &record_first)
2307 break;
2308
2309 record_exec_insn (regcache, gdbarch, record_list);
2310
2311 if (record_list->prev)
2312 record_list = record_list->prev;
2313 }
2314
2315 /* Compute the size needed for the extra bfd section. */
2316 save_size = 4; /* magic cookie */
2317 for (record_list = record_first.next; record_list;
2318 record_list = record_list->next)
2319 switch (record_list->type)
2320 {
2321 case record_end:
2322 save_size += 1 + 4 + 4;
2323 break;
2324 case record_reg:
2325 save_size += 1 + 4 + record_list->u.reg.len;
2326 break;
2327 case record_mem:
2328 save_size += 1 + 4 + 8 + record_list->u.mem.len;
2329 break;
2330 }
2331
2332 /* Make the new bfd section. */
2333 osec = bfd_make_section_anyway_with_flags (obfd, "precord",
2334 SEC_HAS_CONTENTS
2335 | SEC_READONLY);
2336 if (osec == NULL)
2337 error (_("Failed to create 'precord' section for corefile %s: %s"),
2338 recfilename,
2339 bfd_errmsg (bfd_get_error ()));
2340 bfd_set_section_size (obfd, osec, save_size);
2341 bfd_set_section_vma (obfd, osec, 0);
2342 bfd_set_section_alignment (obfd, osec, 0);
2343 bfd_section_lma (obfd, osec) = 0;
2344
2345 /* Save corefile state. */
2346 write_gcore_file (obfd);
2347
2348 /* Write out the record log. */
2349 /* Write the magic code. */
2350 magic = RECORD_FILE_MAGIC;
2351 if (record_debug)
2352 fprintf_unfiltered (gdb_stdlog, "\
2353 Writing 4-byte magic cookie RECORD_FILE_MAGIC (0x%s)\n",
2354 phex_nz (magic, 4));
2355 bfdcore_write (obfd, osec, &magic, sizeof (magic), &bfd_offset);
2356
2357 /* Save the entries to recfd and forward execute to the end of
2358 record list. */
2359 record_list = &record_first;
2360 while (1)
2361 {
2362 /* Save entry. */
2363 if (record_list != &record_first)
2364 {
2365 uint8_t type;
2366 uint32_t regnum, len, signal, count;
2367 uint64_t addr;
2368
2369 type = record_list->type;
2370 bfdcore_write (obfd, osec, &type, sizeof (type), &bfd_offset);
2371
2372 switch (record_list->type)
2373 {
2374 case record_reg: /* reg */
2375 if (record_debug)
2376 fprintf_unfiltered (gdb_stdlog, "\
2377 Writing register %d (1 plus %lu plus %d bytes)\n",
2378 record_list->u.reg.num,
2379 (unsigned long) sizeof (regnum),
2380 record_list->u.reg.len);
2381
2382 /* Write regnum. */
2383 regnum = netorder32 (record_list->u.reg.num);
2384 bfdcore_write (obfd, osec, &regnum,
2385 sizeof (regnum), &bfd_offset);
2386
2387 /* Write regval. */
2388 bfdcore_write (obfd, osec, record_get_loc (record_list),
2389 record_list->u.reg.len, &bfd_offset);
2390 break;
2391
2392 case record_mem: /* mem */
2393 if (record_debug)
2394 fprintf_unfiltered (gdb_stdlog, "\
2395 Writing memory %s (1 plus %lu plus %lu plus %d bytes)\n",
2396 paddress (gdbarch,
2397 record_list->u.mem.addr),
2398 (unsigned long) sizeof (addr),
2399 (unsigned long) sizeof (len),
2400 record_list->u.mem.len);
2401
2402 /* Write memlen. */
2403 len = netorder32 (record_list->u.mem.len);
2404 bfdcore_write (obfd, osec, &len, sizeof (len), &bfd_offset);
2405
2406 /* Write memaddr. */
2407 addr = netorder64 (record_list->u.mem.addr);
2408 bfdcore_write (obfd, osec, &addr,
2409 sizeof (addr), &bfd_offset);
2410
2411 /* Write memval. */
2412 bfdcore_write (obfd, osec, record_get_loc (record_list),
2413 record_list->u.mem.len, &bfd_offset);
2414 break;
2415
2416 case record_end:
2417 if (record_debug)
2418 fprintf_unfiltered (gdb_stdlog, "\
2419 Writing record_end (1 + %lu + %lu bytes)\n",
2420 (unsigned long) sizeof (signal),
2421 (unsigned long) sizeof (count));
2422 /* Write signal value. */
2423 signal = netorder32 (record_list->u.end.sigval);
2424 bfdcore_write (obfd, osec, &signal,
2425 sizeof (signal), &bfd_offset);
2426
2427 /* Write insn count. */
2428 count = netorder32 (record_list->u.end.insn_num);
2429 bfdcore_write (obfd, osec, &count,
2430 sizeof (count), &bfd_offset);
2431 break;
2432 }
2433 }
2434
2435 /* Execute entry. */
2436 record_exec_insn (regcache, gdbarch, record_list);
2437
2438 if (record_list->next)
2439 record_list = record_list->next;
2440 else
2441 break;
2442 }
2443
2444 /* Reverse execute to cur_record_list. */
2445 while (1)
2446 {
2447 /* Check for beginning and end of log. */
2448 if (record_list == cur_record_list)
2449 break;
2450
2451 record_exec_insn (regcache, gdbarch, record_list);
2452
2453 if (record_list->prev)
2454 record_list = record_list->prev;
2455 }
2456
2457 do_cleanups (set_cleanups);
2458 bfd_close (obfd);
2459 discard_cleanups (old_cleanups);
2460
2461 /* Succeeded. */
2462 printf_filtered (_("Saved core file %s with execution log.\n"),
2463 recfilename);
2464 }
2465
2466 /* record_goto_insn -- rewind the record log (forward or backward,
2467 depending on DIR) to the given entry, changing the program state
2468 correspondingly. */
2469
2470 static void
2471 record_goto_insn (struct record_entry *entry,
2472 enum exec_direction_kind dir)
2473 {
2474 struct cleanup *set_cleanups = record_gdb_operation_disable_set ();
2475 struct regcache *regcache = get_current_regcache ();
2476 struct gdbarch *gdbarch = get_regcache_arch (regcache);
2477
2478 /* Assume everything is valid: we will hit the entry,
2479 and we will not hit the end of the recording. */
2480
2481 if (dir == EXEC_FORWARD)
2482 record_list = record_list->next;
2483
2484 do
2485 {
2486 record_exec_insn (regcache, gdbarch, record_list);
2487 if (dir == EXEC_REVERSE)
2488 record_list = record_list->prev;
2489 else
2490 record_list = record_list->next;
2491 } while (record_list != entry);
2492 do_cleanups (set_cleanups);
2493 }
2494
2495 /* "record goto" command. Argument is an instruction number,
2496 as given by "info record".
2497
2498 Rewinds the recording (forward or backward) to the given instruction. */
2499
2500 static void
2501 cmd_record_goto (char *arg, int from_tty)
2502 {
2503 struct record_entry *p = NULL;
2504 ULONGEST target_insn = 0;
2505
2506 if (arg == NULL || *arg == '\0')
2507 error (_("Command requires an argument (insn number to go to)."));
2508
2509 if (strncmp (arg, "start", strlen ("start")) == 0
2510 || strncmp (arg, "begin", strlen ("begin")) == 0)
2511 {
2512 /* Special case. Find first insn. */
2513 for (p = &record_first; p != NULL; p = p->next)
2514 if (p->type == record_end)
2515 break;
2516 if (p)
2517 target_insn = p->u.end.insn_num;
2518 }
2519 else if (strncmp (arg, "end", strlen ("end")) == 0)
2520 {
2521 /* Special case. Find last insn. */
2522 for (p = record_list; p->next != NULL; p = p->next)
2523 ;
2524 for (; p!= NULL; p = p->prev)
2525 if (p->type == record_end)
2526 break;
2527 if (p)
2528 target_insn = p->u.end.insn_num;
2529 }
2530 else
2531 {
2532 /* General case. Find designated insn. */
2533 target_insn = parse_and_eval_long (arg);
2534
2535 for (p = &record_first; p != NULL; p = p->next)
2536 if (p->type == record_end && p->u.end.insn_num == target_insn)
2537 break;
2538 }
2539
2540 if (p == NULL)
2541 error (_("Target insn '%s' not found."), arg);
2542 else if (p == record_list)
2543 error (_("Already at insn '%s'."), arg);
2544 else if (p->u.end.insn_num > record_list->u.end.insn_num)
2545 {
2546 printf_filtered (_("Go forward to insn number %s\n"),
2547 pulongest (target_insn));
2548 record_goto_insn (p, EXEC_FORWARD);
2549 }
2550 else
2551 {
2552 printf_filtered (_("Go backward to insn number %s\n"),
2553 pulongest (target_insn));
2554 record_goto_insn (p, EXEC_REVERSE);
2555 }
2556 registers_changed ();
2557 reinit_frame_cache ();
2558 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
2559 }
2560
2561 void
2562 _initialize_record (void)
2563 {
2564 struct cmd_list_element *c;
2565
2566 /* Init record_first. */
2567 record_first.prev = NULL;
2568 record_first.next = NULL;
2569 record_first.type = record_end;
2570
2571 init_record_ops ();
2572 add_target (&record_ops);
2573 init_record_core_ops ();
2574 add_target (&record_core_ops);
2575
2576 add_setshow_zinteger_cmd ("record", no_class, &record_debug,
2577 _("Set debugging of record/replay feature."),
2578 _("Show debugging of record/replay feature."),
2579 _("When enabled, debugging output for "
2580 "record/replay feature is displayed."),
2581 NULL, show_record_debug, &setdebuglist,
2582 &showdebuglist);
2583
2584 c = add_prefix_cmd ("record", class_obscure, cmd_record_start,
2585 _("Abbreviated form of \"target record\" command."),
2586 &record_cmdlist, "record ", 0, &cmdlist);
2587 set_cmd_completer (c, filename_completer);
2588
2589 add_com_alias ("rec", "record", class_obscure, 1);
2590 add_prefix_cmd ("record", class_support, set_record_command,
2591 _("Set record options"), &set_record_cmdlist,
2592 "set record ", 0, &setlist);
2593 add_alias_cmd ("rec", "record", class_obscure, 1, &setlist);
2594 add_prefix_cmd ("record", class_support, show_record_command,
2595 _("Show record options"), &show_record_cmdlist,
2596 "show record ", 0, &showlist);
2597 add_alias_cmd ("rec", "record", class_obscure, 1, &showlist);
2598 add_prefix_cmd ("record", class_support, info_record_command,
2599 _("Info record options"), &info_record_cmdlist,
2600 "info record ", 0, &infolist);
2601 add_alias_cmd ("rec", "record", class_obscure, 1, &infolist);
2602
2603 c = add_cmd ("save", class_obscure, cmd_record_save,
2604 _("Save the execution log to a file.\n\
2605 Argument is optional filename.\n\
2606 Default filename is 'gdb_record.<process_id>'."),
2607 &record_cmdlist);
2608 set_cmd_completer (c, filename_completer);
2609
2610 c = add_cmd ("restore", class_obscure, cmd_record_restore,
2611 _("Restore the execution log from a file.\n\
2612 Argument is filename. File must be created with 'record save'."),
2613 &record_cmdlist);
2614 set_cmd_completer (c, filename_completer);
2615
2616 add_cmd ("delete", class_obscure, cmd_record_delete,
2617 _("Delete the rest of execution log and start recording it anew."),
2618 &record_cmdlist);
2619 add_alias_cmd ("d", "delete", class_obscure, 1, &record_cmdlist);
2620 add_alias_cmd ("del", "delete", class_obscure, 1, &record_cmdlist);
2621
2622 add_cmd ("stop", class_obscure, cmd_record_stop,
2623 _("Stop the record/replay target."),
2624 &record_cmdlist);
2625 add_alias_cmd ("s", "stop", class_obscure, 1, &record_cmdlist);
2626
2627 /* Record instructions number limit command. */
2628 add_setshow_boolean_cmd ("stop-at-limit", no_class,
2629 &record_stop_at_limit, _("\
2630 Set whether record/replay stops when record/replay buffer becomes full."), _("\
2631 Show whether record/replay stops when record/replay buffer becomes full."), _("\
2632 Default is ON.\n\
2633 When ON, if the record/replay buffer becomes full, ask user what to do.\n\
2634 When OFF, if the record/replay buffer becomes full,\n\
2635 delete the oldest recorded instruction to make room for each new one."),
2636 NULL, NULL,
2637 &set_record_cmdlist, &show_record_cmdlist);
2638 add_setshow_uinteger_cmd ("insn-number-max", no_class,
2639 &record_insn_max_num,
2640 _("Set record/replay buffer limit."),
2641 _("Show record/replay buffer limit."), _("\
2642 Set the maximum number of instructions to be stored in the\n\
2643 record/replay buffer. Zero means unlimited. Default is 200000."),
2644 set_record_insn_max_num,
2645 NULL, &set_record_cmdlist, &show_record_cmdlist);
2646
2647 add_cmd ("goto", class_obscure, cmd_record_goto, _("\
2648 Restore the program to its state at instruction number N.\n\
2649 Argument is instruction number, as shown by 'info record'."),
2650 &record_cmdlist);
2651 }
This page took 0.142904 seconds and 4 git commands to generate.