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