b8460d9c5d872fce1f7b1dcc936fe7406eb66b46
[deliverable/binutils-gdb.git] / gdb / record-full.c
1 /* Process record and replay target for GDB, the GNU debugger.
2
3 Copyright (C) 2013-2018 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 "completer.h"
26 #include "arch-utils.h"
27 #include "gdbcore.h"
28 #include "exec.h"
29 #include "record.h"
30 #include "record-full.h"
31 #include "elf-bfd.h"
32 #include "gcore.h"
33 #include "event-loop.h"
34 #include "inf-loop.h"
35 #include "gdb_bfd.h"
36 #include "observable.h"
37 #include "infrun.h"
38 #include "common/gdb_unlinker.h"
39 #include "common/byte-vector.h"
40
41 #include <signal.h>
42
43 /* This module implements "target record-full", also known as "process
44 record and replay". This target sits on top of a "normal" target
45 (a target that "has execution"), and provides a record and replay
46 functionality, including reverse debugging.
47
48 Target record has two modes: recording, and replaying.
49
50 In record mode, we intercept the resume and wait methods.
51 Whenever gdb resumes the target, we run the target in single step
52 mode, and we build up an execution log in which, for each executed
53 instruction, we record all changes in memory and register state.
54 This is invisible to the user, to whom it just looks like an
55 ordinary debugging session (except for performance degredation).
56
57 In replay mode, instead of actually letting the inferior run as a
58 process, we simulate its execution by playing back the recorded
59 execution log. For each instruction in the log, we simulate the
60 instruction's side effects by duplicating the changes that it would
61 have made on memory and registers. */
62
63 #define DEFAULT_RECORD_FULL_INSN_MAX_NUM 200000
64
65 #define RECORD_FULL_IS_REPLAY \
66 (record_full_list->next || ::execution_direction == EXEC_REVERSE)
67
68 #define RECORD_FULL_FILE_MAGIC netorder32(0x20091016)
69
70 /* These are the core structs of the process record functionality.
71
72 A record_full_entry is a record of the value change of a register
73 ("record_full_reg") or a part of memory ("record_full_mem"). And each
74 instruction must have a struct record_full_entry ("record_full_end")
75 that indicates that this is the last struct record_full_entry of this
76 instruction.
77
78 Each struct record_full_entry is linked to "record_full_list" by "prev"
79 and "next" pointers. */
80
81 struct record_full_mem_entry
82 {
83 CORE_ADDR addr;
84 int len;
85 /* Set this flag if target memory for this entry
86 can no longer be accessed. */
87 int mem_entry_not_accessible;
88 union
89 {
90 gdb_byte *ptr;
91 gdb_byte buf[sizeof (gdb_byte *)];
92 } u;
93 };
94
95 struct record_full_reg_entry
96 {
97 unsigned short num;
98 unsigned short len;
99 union
100 {
101 gdb_byte *ptr;
102 gdb_byte buf[2 * sizeof (gdb_byte *)];
103 } u;
104 };
105
106 struct record_full_end_entry
107 {
108 enum gdb_signal sigval;
109 ULONGEST insn_num;
110 };
111
112 enum record_full_type
113 {
114 record_full_end = 0,
115 record_full_reg,
116 record_full_mem
117 };
118
119 /* This is the data structure that makes up the execution log.
120
121 The execution log consists of a single linked list of entries
122 of type "struct record_full_entry". It is doubly linked so that it
123 can be traversed in either direction.
124
125 The start of the list is anchored by a struct called
126 "record_full_first". The pointer "record_full_list" either points
127 to the last entry that was added to the list (in record mode), or to
128 the next entry in the list that will be executed (in replay mode).
129
130 Each list element (struct record_full_entry), in addition to next
131 and prev pointers, consists of a union of three entry types: mem,
132 reg, and end. A field called "type" determines which entry type is
133 represented by a given list element.
134
135 Each instruction that is added to the execution log is represented
136 by a variable number of list elements ('entries'). The instruction
137 will have one "reg" entry for each register that is changed by
138 executing the instruction (including the PC in every case). It
139 will also have one "mem" entry for each memory change. Finally,
140 each instruction will have an "end" entry that separates it from
141 the changes associated with the next instruction. */
142
143 struct record_full_entry
144 {
145 struct record_full_entry *prev;
146 struct record_full_entry *next;
147 enum record_full_type type;
148 union
149 {
150 /* reg */
151 struct record_full_reg_entry reg;
152 /* mem */
153 struct record_full_mem_entry mem;
154 /* end */
155 struct record_full_end_entry end;
156 } u;
157 };
158
159 /* If true, query if PREC cannot record memory
160 change of next instruction. */
161 int record_full_memory_query = 0;
162
163 struct record_full_core_buf_entry
164 {
165 struct record_full_core_buf_entry *prev;
166 struct target_section *p;
167 bfd_byte *buf;
168 };
169
170 /* Record buf with core target. */
171 static detached_regcache *record_full_core_regbuf = NULL;
172 static struct target_section *record_full_core_start;
173 static struct target_section *record_full_core_end;
174 static struct record_full_core_buf_entry *record_full_core_buf_list = NULL;
175
176 /* The following variables are used for managing the linked list that
177 represents the execution log.
178
179 record_full_first is the anchor that holds down the beginning of
180 the list.
181
182 record_full_list serves two functions:
183 1) In record mode, it anchors the end of the list.
184 2) In replay mode, it traverses the list and points to
185 the next instruction that must be emulated.
186
187 record_full_arch_list_head and record_full_arch_list_tail are used
188 to manage a separate list, which is used to build up the change
189 elements of the currently executing instruction during record mode.
190 When this instruction has been completely annotated in the "arch
191 list", it will be appended to the main execution log. */
192
193 static struct record_full_entry record_full_first;
194 static struct record_full_entry *record_full_list = &record_full_first;
195 static struct record_full_entry *record_full_arch_list_head = NULL;
196 static struct record_full_entry *record_full_arch_list_tail = NULL;
197
198 /* 1 ask user. 0 auto delete the last struct record_full_entry. */
199 static int record_full_stop_at_limit = 1;
200 /* Maximum allowed number of insns in execution log. */
201 static unsigned int record_full_insn_max_num
202 = DEFAULT_RECORD_FULL_INSN_MAX_NUM;
203 /* Actual count of insns presently in execution log. */
204 static unsigned int record_full_insn_num = 0;
205 /* Count of insns logged so far (may be larger
206 than count of insns presently in execution log). */
207 static ULONGEST record_full_insn_count;
208
209 static const char record_longname[]
210 = N_("Process record and replay target");
211 static const char record_doc[]
212 = N_("Log program while executing and replay execution from log.");
213
214 /* Base class implementing functionality common to both the
215 "record-full" and "record-core" targets. */
216
217 class record_full_base_target : public target_ops
218 {
219 public:
220 record_full_base_target ()
221 { to_stratum = record_stratum; }
222
223 const target_info &info () const override = 0;
224
225 void close () override;
226 void async (int) override;
227 ptid_t wait (ptid_t, struct target_waitstatus *, int) override;
228 bool stopped_by_watchpoint () override;
229 bool stopped_data_address (CORE_ADDR *) override;
230
231 bool stopped_by_sw_breakpoint () override;
232 bool supports_stopped_by_sw_breakpoint () override;
233
234 bool stopped_by_hw_breakpoint () override;
235 bool supports_stopped_by_hw_breakpoint () override;
236
237 bool can_execute_reverse () override;
238
239 /* Add bookmark target methods. */
240 gdb_byte *get_bookmark (const char *, int) override;
241 void goto_bookmark (const gdb_byte *, int) override;
242 enum exec_direction_kind execution_direction () override;
243 enum record_method record_method (ptid_t ptid) override;
244 void info_record () override;
245 void save_record (const char *filename) override;
246 bool supports_delete_record () override;
247 void delete_record () override;
248 bool record_is_replaying (ptid_t ptid) override;
249 bool record_will_replay (ptid_t ptid, int dir) override;
250 void record_stop_replaying () override;
251 void goto_record_begin () override;
252 void goto_record_end () override;
253 void goto_record (ULONGEST insn) override;
254 };
255
256 /* The "record-full" target. */
257
258 static const target_info record_full_target_info = {
259 "record-full",
260 record_longname,
261 record_doc,
262 };
263
264 class record_full_target final : public record_full_base_target
265 {
266 public:
267 const target_info &info () const override
268 { return record_full_target_info; }
269
270 void commit_resume () override;
271 void resume (ptid_t, int, enum gdb_signal) override;
272 void disconnect (const char *, int) override;
273 void detach (inferior *, int) override;
274 void mourn_inferior () override;
275 void kill () override;
276 void store_registers (struct regcache *, int) override;
277 enum target_xfer_status xfer_partial (enum target_object object,
278 const char *annex,
279 gdb_byte *readbuf,
280 const gdb_byte *writebuf,
281 ULONGEST offset, ULONGEST len,
282 ULONGEST *xfered_len) override;
283 int insert_breakpoint (struct gdbarch *,
284 struct bp_target_info *) override;
285 int remove_breakpoint (struct gdbarch *,
286 struct bp_target_info *,
287 enum remove_bp_reason) override;
288 };
289
290 /* The "record-core" target. */
291
292 static const target_info record_full_core_target_info = {
293 "record-core",
294 record_longname,
295 record_doc,
296 };
297
298 class record_full_core_target final : public record_full_base_target
299 {
300 public:
301 const target_info &info () const override
302 { return record_full_core_target_info; }
303
304 void resume (ptid_t, int, enum gdb_signal) override;
305 void disconnect (const char *, int) override;
306 void kill () override;
307 void fetch_registers (struct regcache *regcache, int regno) override;
308 void prepare_to_store (struct regcache *regcache) override;
309 void store_registers (struct regcache *, int) override;
310 enum target_xfer_status xfer_partial (enum target_object object,
311 const char *annex,
312 gdb_byte *readbuf,
313 const gdb_byte *writebuf,
314 ULONGEST offset, ULONGEST len,
315 ULONGEST *xfered_len) override;
316 int insert_breakpoint (struct gdbarch *,
317 struct bp_target_info *) override;
318 int remove_breakpoint (struct gdbarch *,
319 struct bp_target_info *,
320 enum remove_bp_reason) override;
321
322 bool has_execution (ptid_t) override;
323 };
324
325 static record_full_target record_full_ops;
326 static record_full_core_target record_full_core_ops;
327
328 void
329 record_full_target::detach (inferior *inf, int from_tty)
330 {
331 record_detach (this, inf, from_tty);
332 }
333
334 void
335 record_full_target::disconnect (const char *args, int from_tty)
336 {
337 record_disconnect (this, args, from_tty);
338 }
339
340 void
341 record_full_core_target::disconnect (const char *args, int from_tty)
342 {
343 record_disconnect (this, args, from_tty);
344 }
345
346 void
347 record_full_target::mourn_inferior ()
348 {
349 record_mourn_inferior (this);
350 }
351
352 void
353 record_full_target::kill ()
354 {
355 record_kill (this);
356 }
357
358 /* See record-full.h. */
359
360 int
361 record_full_is_used (void)
362 {
363 struct target_ops *t;
364
365 t = find_record_target ();
366 return (t == &record_full_ops
367 || t == &record_full_core_ops);
368 }
369
370
371 /* Command lists for "set/show record full". */
372 static struct cmd_list_element *set_record_full_cmdlist;
373 static struct cmd_list_element *show_record_full_cmdlist;
374
375 /* Command list for "record full". */
376 static struct cmd_list_element *record_full_cmdlist;
377
378 static void record_full_goto_insn (struct record_full_entry *entry,
379 enum exec_direction_kind dir);
380
381 /* Alloc and free functions for record_full_reg, record_full_mem, and
382 record_full_end entries. */
383
384 /* Alloc a record_full_reg record entry. */
385
386 static inline struct record_full_entry *
387 record_full_reg_alloc (struct regcache *regcache, int regnum)
388 {
389 struct record_full_entry *rec;
390 struct gdbarch *gdbarch = regcache->arch ();
391
392 rec = XCNEW (struct record_full_entry);
393 rec->type = record_full_reg;
394 rec->u.reg.num = regnum;
395 rec->u.reg.len = register_size (gdbarch, regnum);
396 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
397 rec->u.reg.u.ptr = (gdb_byte *) xmalloc (rec->u.reg.len);
398
399 return rec;
400 }
401
402 /* Free a record_full_reg record entry. */
403
404 static inline void
405 record_full_reg_release (struct record_full_entry *rec)
406 {
407 gdb_assert (rec->type == record_full_reg);
408 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
409 xfree (rec->u.reg.u.ptr);
410 xfree (rec);
411 }
412
413 /* Alloc a record_full_mem record entry. */
414
415 static inline struct record_full_entry *
416 record_full_mem_alloc (CORE_ADDR addr, int len)
417 {
418 struct record_full_entry *rec;
419
420 rec = XCNEW (struct record_full_entry);
421 rec->type = record_full_mem;
422 rec->u.mem.addr = addr;
423 rec->u.mem.len = len;
424 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
425 rec->u.mem.u.ptr = (gdb_byte *) xmalloc (len);
426
427 return rec;
428 }
429
430 /* Free a record_full_mem record entry. */
431
432 static inline void
433 record_full_mem_release (struct record_full_entry *rec)
434 {
435 gdb_assert (rec->type == record_full_mem);
436 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
437 xfree (rec->u.mem.u.ptr);
438 xfree (rec);
439 }
440
441 /* Alloc a record_full_end record entry. */
442
443 static inline struct record_full_entry *
444 record_full_end_alloc (void)
445 {
446 struct record_full_entry *rec;
447
448 rec = XCNEW (struct record_full_entry);
449 rec->type = record_full_end;
450
451 return rec;
452 }
453
454 /* Free a record_full_end record entry. */
455
456 static inline void
457 record_full_end_release (struct record_full_entry *rec)
458 {
459 xfree (rec);
460 }
461
462 /* Free one record entry, any type.
463 Return entry->type, in case caller wants to know. */
464
465 static inline enum record_full_type
466 record_full_entry_release (struct record_full_entry *rec)
467 {
468 enum record_full_type type = rec->type;
469
470 switch (type) {
471 case record_full_reg:
472 record_full_reg_release (rec);
473 break;
474 case record_full_mem:
475 record_full_mem_release (rec);
476 break;
477 case record_full_end:
478 record_full_end_release (rec);
479 break;
480 }
481 return type;
482 }
483
484 /* Free all record entries in list pointed to by REC. */
485
486 static void
487 record_full_list_release (struct record_full_entry *rec)
488 {
489 if (!rec)
490 return;
491
492 while (rec->next)
493 rec = rec->next;
494
495 while (rec->prev)
496 {
497 rec = rec->prev;
498 record_full_entry_release (rec->next);
499 }
500
501 if (rec == &record_full_first)
502 {
503 record_full_insn_num = 0;
504 record_full_first.next = NULL;
505 }
506 else
507 record_full_entry_release (rec);
508 }
509
510 /* Free all record entries forward of the given list position. */
511
512 static void
513 record_full_list_release_following (struct record_full_entry *rec)
514 {
515 struct record_full_entry *tmp = rec->next;
516
517 rec->next = NULL;
518 while (tmp)
519 {
520 rec = tmp->next;
521 if (record_full_entry_release (tmp) == record_full_end)
522 {
523 record_full_insn_num--;
524 record_full_insn_count--;
525 }
526 tmp = rec;
527 }
528 }
529
530 /* Delete the first instruction from the beginning of the log, to make
531 room for adding a new instruction at the end of the log.
532
533 Note -- this function does not modify record_full_insn_num. */
534
535 static void
536 record_full_list_release_first (void)
537 {
538 struct record_full_entry *tmp;
539
540 if (!record_full_first.next)
541 return;
542
543 /* Loop until a record_full_end. */
544 while (1)
545 {
546 /* Cut record_full_first.next out of the linked list. */
547 tmp = record_full_first.next;
548 record_full_first.next = tmp->next;
549 tmp->next->prev = &record_full_first;
550
551 /* tmp is now isolated, and can be deleted. */
552 if (record_full_entry_release (tmp) == record_full_end)
553 break; /* End loop at first record_full_end. */
554
555 if (!record_full_first.next)
556 {
557 gdb_assert (record_full_insn_num == 1);
558 break; /* End loop when list is empty. */
559 }
560 }
561 }
562
563 /* Add a struct record_full_entry to record_full_arch_list. */
564
565 static void
566 record_full_arch_list_add (struct record_full_entry *rec)
567 {
568 if (record_debug > 1)
569 fprintf_unfiltered (gdb_stdlog,
570 "Process record: record_full_arch_list_add %s.\n",
571 host_address_to_string (rec));
572
573 if (record_full_arch_list_tail)
574 {
575 record_full_arch_list_tail->next = rec;
576 rec->prev = record_full_arch_list_tail;
577 record_full_arch_list_tail = rec;
578 }
579 else
580 {
581 record_full_arch_list_head = rec;
582 record_full_arch_list_tail = rec;
583 }
584 }
585
586 /* Return the value storage location of a record entry. */
587 static inline gdb_byte *
588 record_full_get_loc (struct record_full_entry *rec)
589 {
590 switch (rec->type) {
591 case record_full_mem:
592 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
593 return rec->u.mem.u.ptr;
594 else
595 return rec->u.mem.u.buf;
596 case record_full_reg:
597 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
598 return rec->u.reg.u.ptr;
599 else
600 return rec->u.reg.u.buf;
601 case record_full_end:
602 default:
603 gdb_assert_not_reached ("unexpected record_full_entry type");
604 return NULL;
605 }
606 }
607
608 /* Record the value of a register NUM to record_full_arch_list. */
609
610 int
611 record_full_arch_list_add_reg (struct regcache *regcache, int regnum)
612 {
613 struct record_full_entry *rec;
614
615 if (record_debug > 1)
616 fprintf_unfiltered (gdb_stdlog,
617 "Process record: add register num = %d to "
618 "record list.\n",
619 regnum);
620
621 rec = record_full_reg_alloc (regcache, regnum);
622
623 regcache->raw_read (regnum, record_full_get_loc (rec));
624
625 record_full_arch_list_add (rec);
626
627 return 0;
628 }
629
630 /* Record the value of a region of memory whose address is ADDR and
631 length is LEN to record_full_arch_list. */
632
633 int
634 record_full_arch_list_add_mem (CORE_ADDR addr, int len)
635 {
636 struct record_full_entry *rec;
637
638 if (record_debug > 1)
639 fprintf_unfiltered (gdb_stdlog,
640 "Process record: add mem addr = %s len = %d to "
641 "record list.\n",
642 paddress (target_gdbarch (), addr), len);
643
644 if (!addr) /* FIXME: Why? Some arch must permit it... */
645 return 0;
646
647 rec = record_full_mem_alloc (addr, len);
648
649 if (record_read_memory (target_gdbarch (), addr,
650 record_full_get_loc (rec), len))
651 {
652 record_full_mem_release (rec);
653 return -1;
654 }
655
656 record_full_arch_list_add (rec);
657
658 return 0;
659 }
660
661 /* Add a record_full_end type struct record_full_entry to
662 record_full_arch_list. */
663
664 int
665 record_full_arch_list_add_end (void)
666 {
667 struct record_full_entry *rec;
668
669 if (record_debug > 1)
670 fprintf_unfiltered (gdb_stdlog,
671 "Process record: add end to arch list.\n");
672
673 rec = record_full_end_alloc ();
674 rec->u.end.sigval = GDB_SIGNAL_0;
675 rec->u.end.insn_num = ++record_full_insn_count;
676
677 record_full_arch_list_add (rec);
678
679 return 0;
680 }
681
682 static void
683 record_full_check_insn_num (void)
684 {
685 if (record_full_insn_num == record_full_insn_max_num)
686 {
687 /* Ask user what to do. */
688 if (record_full_stop_at_limit)
689 {
690 if (!yquery (_("Do you want to auto delete previous execution "
691 "log entries when record/replay buffer becomes "
692 "full (record full stop-at-limit)?")))
693 error (_("Process record: stopped by user."));
694 record_full_stop_at_limit = 0;
695 }
696 }
697 }
698
699 static void
700 record_full_arch_list_cleanups (void *ignore)
701 {
702 record_full_list_release (record_full_arch_list_tail);
703 }
704
705 /* Before inferior step (when GDB record the running message, inferior
706 only can step), GDB will call this function to record the values to
707 record_full_list. This function will call gdbarch_process_record to
708 record the running message of inferior and set them to
709 record_full_arch_list, and add it to record_full_list. */
710
711 static void
712 record_full_message (struct regcache *regcache, enum gdb_signal signal)
713 {
714 int ret;
715 struct gdbarch *gdbarch = regcache->arch ();
716 struct cleanup *old_cleanups
717 = make_cleanup (record_full_arch_list_cleanups, 0);
718
719 record_full_arch_list_head = NULL;
720 record_full_arch_list_tail = NULL;
721
722 /* Check record_full_insn_num. */
723 record_full_check_insn_num ();
724
725 /* If gdb sends a signal value to target_resume,
726 save it in the 'end' field of the previous instruction.
727
728 Maybe process record should record what really happened,
729 rather than what gdb pretends has happened.
730
731 So if Linux delivered the signal to the child process during
732 the record mode, we will record it and deliver it again in
733 the replay mode.
734
735 If user says "ignore this signal" during the record mode, then
736 it will be ignored again during the replay mode (no matter if
737 the user says something different, like "deliver this signal"
738 during the replay mode).
739
740 User should understand that nothing he does during the replay
741 mode will change the behavior of the child. If he tries,
742 then that is a user error.
743
744 But we should still deliver the signal to gdb during the replay,
745 if we delivered it during the recording. Therefore we should
746 record the signal during record_full_wait, not
747 record_full_resume. */
748 if (record_full_list != &record_full_first) /* FIXME better way to check */
749 {
750 gdb_assert (record_full_list->type == record_full_end);
751 record_full_list->u.end.sigval = signal;
752 }
753
754 if (signal == GDB_SIGNAL_0
755 || !gdbarch_process_record_signal_p (gdbarch))
756 ret = gdbarch_process_record (gdbarch,
757 regcache,
758 regcache_read_pc (regcache));
759 else
760 ret = gdbarch_process_record_signal (gdbarch,
761 regcache,
762 signal);
763
764 if (ret > 0)
765 error (_("Process record: inferior program stopped."));
766 if (ret < 0)
767 error (_("Process record: failed to record execution log."));
768
769 discard_cleanups (old_cleanups);
770
771 record_full_list->next = record_full_arch_list_head;
772 record_full_arch_list_head->prev = record_full_list;
773 record_full_list = record_full_arch_list_tail;
774
775 if (record_full_insn_num == record_full_insn_max_num)
776 record_full_list_release_first ();
777 else
778 record_full_insn_num++;
779 }
780
781 static bool
782 record_full_message_wrapper_safe (struct regcache *regcache,
783 enum gdb_signal signal)
784 {
785 TRY
786 {
787 record_full_message (regcache, signal);
788 }
789 CATCH (ex, RETURN_MASK_ALL)
790 {
791 exception_print (gdb_stderr, ex);
792 return false;
793 }
794 END_CATCH
795
796 return true;
797 }
798
799 /* Set to 1 if record_full_store_registers and record_full_xfer_partial
800 doesn't need record. */
801
802 static int record_full_gdb_operation_disable = 0;
803
804 scoped_restore_tmpl<int>
805 record_full_gdb_operation_disable_set (void)
806 {
807 return make_scoped_restore (&record_full_gdb_operation_disable, 1);
808 }
809
810 /* Flag set to TRUE for target_stopped_by_watchpoint. */
811 static enum target_stop_reason record_full_stop_reason
812 = TARGET_STOPPED_BY_NO_REASON;
813
814 /* Execute one instruction from the record log. Each instruction in
815 the log will be represented by an arbitrary sequence of register
816 entries and memory entries, followed by an 'end' entry. */
817
818 static inline void
819 record_full_exec_insn (struct regcache *regcache,
820 struct gdbarch *gdbarch,
821 struct record_full_entry *entry)
822 {
823 switch (entry->type)
824 {
825 case record_full_reg: /* reg */
826 {
827 gdb::byte_vector reg (entry->u.reg.len);
828
829 if (record_debug > 1)
830 fprintf_unfiltered (gdb_stdlog,
831 "Process record: record_full_reg %s to "
832 "inferior num = %d.\n",
833 host_address_to_string (entry),
834 entry->u.reg.num);
835
836 regcache->cooked_read (entry->u.reg.num, reg.data ());
837 regcache_cooked_write (regcache, entry->u.reg.num,
838 record_full_get_loc (entry));
839 memcpy (record_full_get_loc (entry), reg.data (), entry->u.reg.len);
840 }
841 break;
842
843 case record_full_mem: /* mem */
844 {
845 /* Nothing to do if the entry is flagged not_accessible. */
846 if (!entry->u.mem.mem_entry_not_accessible)
847 {
848 gdb::byte_vector mem (entry->u.mem.len);
849
850 if (record_debug > 1)
851 fprintf_unfiltered (gdb_stdlog,
852 "Process record: record_full_mem %s to "
853 "inferior addr = %s len = %d.\n",
854 host_address_to_string (entry),
855 paddress (gdbarch, entry->u.mem.addr),
856 entry->u.mem.len);
857
858 if (record_read_memory (gdbarch,
859 entry->u.mem.addr, mem.data (),
860 entry->u.mem.len))
861 entry->u.mem.mem_entry_not_accessible = 1;
862 else
863 {
864 if (target_write_memory (entry->u.mem.addr,
865 record_full_get_loc (entry),
866 entry->u.mem.len))
867 {
868 entry->u.mem.mem_entry_not_accessible = 1;
869 if (record_debug)
870 warning (_("Process record: error writing memory at "
871 "addr = %s len = %d."),
872 paddress (gdbarch, entry->u.mem.addr),
873 entry->u.mem.len);
874 }
875 else
876 {
877 memcpy (record_full_get_loc (entry), mem.data (),
878 entry->u.mem.len);
879
880 /* We've changed memory --- check if a hardware
881 watchpoint should trap. Note that this
882 presently assumes the target beneath supports
883 continuable watchpoints. On non-continuable
884 watchpoints target, we'll want to check this
885 _before_ actually doing the memory change, and
886 not doing the change at all if the watchpoint
887 traps. */
888 if (hardware_watchpoint_inserted_in_range
889 (regcache->aspace (),
890 entry->u.mem.addr, entry->u.mem.len))
891 record_full_stop_reason = TARGET_STOPPED_BY_WATCHPOINT;
892 }
893 }
894 }
895 }
896 break;
897 }
898 }
899
900 static void record_full_restore (void);
901
902 /* Asynchronous signal handle registered as event loop source for when
903 we have pending events ready to be passed to the core. */
904
905 static struct async_event_handler *record_full_async_inferior_event_token;
906
907 static void
908 record_full_async_inferior_event_handler (gdb_client_data data)
909 {
910 inferior_event_handler (INF_REG_EVENT, NULL);
911 }
912
913 /* Open the process record target for 'core' files. */
914
915 static void
916 record_full_core_open_1 (const char *name, int from_tty)
917 {
918 struct regcache *regcache = get_current_regcache ();
919 int regnum = gdbarch_num_regs (regcache->arch ());
920 int i;
921
922 /* Get record_full_core_regbuf. */
923 target_fetch_registers (regcache, -1);
924 record_full_core_regbuf = new detached_regcache (regcache->arch (), false);
925
926 for (i = 0; i < regnum; i ++)
927 record_full_core_regbuf->raw_supply (i, *regcache);
928
929 /* Get record_full_core_start and record_full_core_end. */
930 if (build_section_table (core_bfd, &record_full_core_start,
931 &record_full_core_end))
932 {
933 delete record_full_core_regbuf;
934 record_full_core_regbuf = NULL;
935 error (_("\"%s\": Can't find sections: %s"),
936 bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ()));
937 }
938
939 push_target (&record_full_core_ops);
940 record_full_restore ();
941 }
942
943 /* Open the process record target for 'live' processes. */
944
945 static void
946 record_full_open_1 (const char *name, int from_tty)
947 {
948 if (record_debug)
949 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_open_1\n");
950
951 /* check exec */
952 if (!target_has_execution)
953 error (_("Process record: the program is not being run."));
954 if (non_stop)
955 error (_("Process record target can't debug inferior in non-stop mode "
956 "(non-stop)."));
957
958 if (!gdbarch_process_record_p (target_gdbarch ()))
959 error (_("Process record: the current architecture doesn't support "
960 "record function."));
961
962 push_target (&record_full_ops);
963 }
964
965 static void record_full_init_record_breakpoints (void);
966
967 /* Open the process record target. */
968
969 static void
970 record_full_open (const char *name, int from_tty)
971 {
972 if (record_debug)
973 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_open\n");
974
975 record_preopen ();
976
977 /* Reset */
978 record_full_insn_num = 0;
979 record_full_insn_count = 0;
980 record_full_list = &record_full_first;
981 record_full_list->next = NULL;
982
983 if (core_bfd)
984 record_full_core_open_1 (name, from_tty);
985 else
986 record_full_open_1 (name, from_tty);
987
988 /* Register extra event sources in the event loop. */
989 record_full_async_inferior_event_token
990 = create_async_event_handler (record_full_async_inferior_event_handler,
991 NULL);
992
993 record_full_init_record_breakpoints ();
994
995 gdb::observers::record_changed.notify (current_inferior (), 1, "full", NULL);
996 }
997
998 /* "close" target method. Close the process record target. */
999
1000 void
1001 record_full_base_target::close ()
1002 {
1003 struct record_full_core_buf_entry *entry;
1004
1005 if (record_debug)
1006 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_close\n");
1007
1008 record_full_list_release (record_full_list);
1009
1010 /* Release record_full_core_regbuf. */
1011 if (record_full_core_regbuf)
1012 {
1013 delete record_full_core_regbuf;
1014 record_full_core_regbuf = NULL;
1015 }
1016
1017 /* Release record_full_core_buf_list. */
1018 if (record_full_core_buf_list)
1019 {
1020 for (entry = record_full_core_buf_list->prev; entry;
1021 entry = entry->prev)
1022 {
1023 xfree (record_full_core_buf_list);
1024 record_full_core_buf_list = entry;
1025 }
1026 record_full_core_buf_list = NULL;
1027 }
1028
1029 if (record_full_async_inferior_event_token)
1030 delete_async_event_handler (&record_full_async_inferior_event_token);
1031 }
1032
1033 /* "async" target method. */
1034
1035 void
1036 record_full_base_target::async (int enable)
1037 {
1038 if (enable)
1039 mark_async_event_handler (record_full_async_inferior_event_token);
1040 else
1041 clear_async_event_handler (record_full_async_inferior_event_token);
1042
1043 beneath->async (enable);
1044 }
1045
1046 static int record_full_resume_step = 0;
1047
1048 /* True if we've been resumed, and so each record_full_wait call should
1049 advance execution. If this is false, record_full_wait will return a
1050 TARGET_WAITKIND_IGNORE. */
1051 static int record_full_resumed = 0;
1052
1053 /* The execution direction of the last resume we got. This is
1054 necessary for async mode. Vis (order is not strictly accurate):
1055
1056 1. user has the global execution direction set to forward
1057 2. user does a reverse-step command
1058 3. record_full_resume is called with global execution direction
1059 temporarily switched to reverse
1060 4. GDB's execution direction is reverted back to forward
1061 5. target record notifies event loop there's an event to handle
1062 6. infrun asks the target which direction was it going, and switches
1063 the global execution direction accordingly (to reverse)
1064 7. infrun polls an event out of the record target, and handles it
1065 8. GDB goes back to the event loop, and goto #4.
1066 */
1067 static enum exec_direction_kind record_full_execution_dir = EXEC_FORWARD;
1068
1069 /* "resume" target method. Resume the process record target. */
1070
1071 void
1072 record_full_target::resume (ptid_t ptid, int step, enum gdb_signal signal)
1073 {
1074 record_full_resume_step = step;
1075 record_full_resumed = 1;
1076 record_full_execution_dir = ::execution_direction;
1077
1078 if (!RECORD_FULL_IS_REPLAY)
1079 {
1080 struct gdbarch *gdbarch = target_thread_architecture (ptid);
1081
1082 record_full_message (get_current_regcache (), signal);
1083
1084 if (!step)
1085 {
1086 /* This is not hard single step. */
1087 if (!gdbarch_software_single_step_p (gdbarch))
1088 {
1089 /* This is a normal continue. */
1090 step = 1;
1091 }
1092 else
1093 {
1094 /* This arch supports soft single step. */
1095 if (thread_has_single_step_breakpoints_set (inferior_thread ()))
1096 {
1097 /* This is a soft single step. */
1098 record_full_resume_step = 1;
1099 }
1100 else
1101 step = !insert_single_step_breakpoints (gdbarch);
1102 }
1103 }
1104
1105 /* Make sure the target beneath reports all signals. */
1106 target_pass_signals (0, NULL);
1107
1108 this->beneath->resume (ptid, step, signal);
1109 }
1110
1111 /* We are about to start executing the inferior (or simulate it),
1112 let's register it with the event loop. */
1113 if (target_can_async_p ())
1114 target_async (1);
1115 }
1116
1117 /* "commit_resume" method for process record target. */
1118
1119 void
1120 record_full_target::commit_resume ()
1121 {
1122 if (!RECORD_FULL_IS_REPLAY)
1123 beneath->commit_resume ();
1124 }
1125
1126 static int record_full_get_sig = 0;
1127
1128 /* SIGINT signal handler, registered by "wait" method. */
1129
1130 static void
1131 record_full_sig_handler (int signo)
1132 {
1133 if (record_debug)
1134 fprintf_unfiltered (gdb_stdlog, "Process record: get a signal\n");
1135
1136 /* It will break the running inferior in replay mode. */
1137 record_full_resume_step = 1;
1138
1139 /* It will let record_full_wait set inferior status to get the signal
1140 SIGINT. */
1141 record_full_get_sig = 1;
1142 }
1143
1144 static void
1145 record_full_wait_cleanups (void *ignore)
1146 {
1147 if (execution_direction == EXEC_REVERSE)
1148 {
1149 if (record_full_list->next)
1150 record_full_list = record_full_list->next;
1151 }
1152 else
1153 record_full_list = record_full_list->prev;
1154 }
1155
1156 /* "wait" target method for process record target.
1157
1158 In record mode, the target is always run in singlestep mode
1159 (even when gdb says to continue). The wait method intercepts
1160 the stop events and determines which ones are to be passed on to
1161 gdb. Most stop events are just singlestep events that gdb is not
1162 to know about, so the wait method just records them and keeps
1163 singlestepping.
1164
1165 In replay mode, this function emulates the recorded execution log,
1166 one instruction at a time (forward or backward), and determines
1167 where to stop. */
1168
1169 static ptid_t
1170 record_full_wait_1 (struct target_ops *ops,
1171 ptid_t ptid, struct target_waitstatus *status,
1172 int options)
1173 {
1174 scoped_restore restore_operation_disable
1175 = record_full_gdb_operation_disable_set ();
1176
1177 if (record_debug)
1178 fprintf_unfiltered (gdb_stdlog,
1179 "Process record: record_full_wait "
1180 "record_full_resume_step = %d, "
1181 "record_full_resumed = %d, direction=%s\n",
1182 record_full_resume_step, record_full_resumed,
1183 record_full_execution_dir == EXEC_FORWARD
1184 ? "forward" : "reverse");
1185
1186 if (!record_full_resumed)
1187 {
1188 gdb_assert ((options & TARGET_WNOHANG) != 0);
1189
1190 /* No interesting event. */
1191 status->kind = TARGET_WAITKIND_IGNORE;
1192 return minus_one_ptid;
1193 }
1194
1195 record_full_get_sig = 0;
1196 signal (SIGINT, record_full_sig_handler);
1197
1198 record_full_stop_reason = TARGET_STOPPED_BY_NO_REASON;
1199
1200 if (!RECORD_FULL_IS_REPLAY && ops != &record_full_core_ops)
1201 {
1202 if (record_full_resume_step)
1203 {
1204 /* This is a single step. */
1205 return ops->beneath->wait (ptid, status, options);
1206 }
1207 else
1208 {
1209 /* This is not a single step. */
1210 ptid_t ret;
1211 CORE_ADDR tmp_pc;
1212 struct gdbarch *gdbarch = target_thread_architecture (inferior_ptid);
1213
1214 while (1)
1215 {
1216 struct thread_info *tp;
1217
1218 ret = ops->beneath->wait (ptid, status, options);
1219 if (status->kind == TARGET_WAITKIND_IGNORE)
1220 {
1221 if (record_debug)
1222 fprintf_unfiltered (gdb_stdlog,
1223 "Process record: record_full_wait "
1224 "target beneath not done yet\n");
1225 return ret;
1226 }
1227
1228 ALL_NON_EXITED_THREADS (tp)
1229 delete_single_step_breakpoints (tp);
1230
1231 if (record_full_resume_step)
1232 return ret;
1233
1234 /* Is this a SIGTRAP? */
1235 if (status->kind == TARGET_WAITKIND_STOPPED
1236 && status->value.sig == GDB_SIGNAL_TRAP)
1237 {
1238 struct regcache *regcache;
1239 enum target_stop_reason *stop_reason_p
1240 = &record_full_stop_reason;
1241
1242 /* Yes -- this is likely our single-step finishing,
1243 but check if there's any reason the core would be
1244 interested in the event. */
1245
1246 registers_changed ();
1247 regcache = get_current_regcache ();
1248 tmp_pc = regcache_read_pc (regcache);
1249 const struct address_space *aspace = regcache->aspace ();
1250
1251 if (target_stopped_by_watchpoint ())
1252 {
1253 /* Always interested in watchpoints. */
1254 }
1255 else if (record_check_stopped_by_breakpoint (aspace, tmp_pc,
1256 stop_reason_p))
1257 {
1258 /* There is a breakpoint here. Let the core
1259 handle it. */
1260 }
1261 else
1262 {
1263 /* This is a single-step trap. Record the
1264 insn and issue another step.
1265 FIXME: this part can be a random SIGTRAP too.
1266 But GDB cannot handle it. */
1267 int step = 1;
1268
1269 if (!record_full_message_wrapper_safe (regcache,
1270 GDB_SIGNAL_0))
1271 {
1272 status->kind = TARGET_WAITKIND_STOPPED;
1273 status->value.sig = GDB_SIGNAL_0;
1274 break;
1275 }
1276
1277 if (gdbarch_software_single_step_p (gdbarch))
1278 {
1279 /* Try to insert the software single step breakpoint.
1280 If insert success, set step to 0. */
1281 set_executing (inferior_ptid, 0);
1282 reinit_frame_cache ();
1283
1284 step = !insert_single_step_breakpoints (gdbarch);
1285
1286 set_executing (inferior_ptid, 1);
1287 }
1288
1289 if (record_debug)
1290 fprintf_unfiltered (gdb_stdlog,
1291 "Process record: record_full_wait "
1292 "issuing one more step in the "
1293 "target beneath\n");
1294 ops->beneath->resume (ptid, step, GDB_SIGNAL_0);
1295 ops->beneath->commit_resume ();
1296 continue;
1297 }
1298 }
1299
1300 /* The inferior is broken by a breakpoint or a signal. */
1301 break;
1302 }
1303
1304 return ret;
1305 }
1306 }
1307 else
1308 {
1309 struct regcache *regcache = get_current_regcache ();
1310 struct gdbarch *gdbarch = regcache->arch ();
1311 const struct address_space *aspace = regcache->aspace ();
1312 int continue_flag = 1;
1313 int first_record_full_end = 1;
1314 struct cleanup *old_cleanups
1315 = make_cleanup (record_full_wait_cleanups, 0);
1316 CORE_ADDR tmp_pc;
1317
1318 record_full_stop_reason = TARGET_STOPPED_BY_NO_REASON;
1319 status->kind = TARGET_WAITKIND_STOPPED;
1320
1321 /* Check breakpoint when forward execute. */
1322 if (execution_direction == EXEC_FORWARD)
1323 {
1324 tmp_pc = regcache_read_pc (regcache);
1325 if (record_check_stopped_by_breakpoint (aspace, tmp_pc,
1326 &record_full_stop_reason))
1327 {
1328 if (record_debug)
1329 fprintf_unfiltered (gdb_stdlog,
1330 "Process record: break at %s.\n",
1331 paddress (gdbarch, tmp_pc));
1332 goto replay_out;
1333 }
1334 }
1335
1336 /* If GDB is in terminal_inferior mode, it will not get the signal.
1337 And in GDB replay mode, GDB doesn't need to be in terminal_inferior
1338 mode, because inferior will not executed.
1339 Then set it to terminal_ours to make GDB get the signal. */
1340 target_terminal::ours ();
1341
1342 /* In EXEC_FORWARD mode, record_full_list points to the tail of prev
1343 instruction. */
1344 if (execution_direction == EXEC_FORWARD && record_full_list->next)
1345 record_full_list = record_full_list->next;
1346
1347 /* Loop over the record_full_list, looking for the next place to
1348 stop. */
1349 do
1350 {
1351 /* Check for beginning and end of log. */
1352 if (execution_direction == EXEC_REVERSE
1353 && record_full_list == &record_full_first)
1354 {
1355 /* Hit beginning of record log in reverse. */
1356 status->kind = TARGET_WAITKIND_NO_HISTORY;
1357 break;
1358 }
1359 if (execution_direction != EXEC_REVERSE && !record_full_list->next)
1360 {
1361 /* Hit end of record log going forward. */
1362 status->kind = TARGET_WAITKIND_NO_HISTORY;
1363 break;
1364 }
1365
1366 record_full_exec_insn (regcache, gdbarch, record_full_list);
1367
1368 if (record_full_list->type == record_full_end)
1369 {
1370 if (record_debug > 1)
1371 fprintf_unfiltered (gdb_stdlog,
1372 "Process record: record_full_end %s to "
1373 "inferior.\n",
1374 host_address_to_string (record_full_list));
1375
1376 if (first_record_full_end && execution_direction == EXEC_REVERSE)
1377 {
1378 /* When reverse excute, the first record_full_end is the
1379 part of current instruction. */
1380 first_record_full_end = 0;
1381 }
1382 else
1383 {
1384 /* In EXEC_REVERSE mode, this is the record_full_end of prev
1385 instruction.
1386 In EXEC_FORWARD mode, this is the record_full_end of
1387 current instruction. */
1388 /* step */
1389 if (record_full_resume_step)
1390 {
1391 if (record_debug > 1)
1392 fprintf_unfiltered (gdb_stdlog,
1393 "Process record: step.\n");
1394 continue_flag = 0;
1395 }
1396
1397 /* check breakpoint */
1398 tmp_pc = regcache_read_pc (regcache);
1399 if (record_check_stopped_by_breakpoint (aspace, tmp_pc,
1400 &record_full_stop_reason))
1401 {
1402 if (record_debug)
1403 fprintf_unfiltered (gdb_stdlog,
1404 "Process record: break "
1405 "at %s.\n",
1406 paddress (gdbarch, tmp_pc));
1407
1408 continue_flag = 0;
1409 }
1410
1411 if (record_full_stop_reason == TARGET_STOPPED_BY_WATCHPOINT)
1412 {
1413 if (record_debug)
1414 fprintf_unfiltered (gdb_stdlog,
1415 "Process record: hit hw "
1416 "watchpoint.\n");
1417 continue_flag = 0;
1418 }
1419 /* Check target signal */
1420 if (record_full_list->u.end.sigval != GDB_SIGNAL_0)
1421 /* FIXME: better way to check */
1422 continue_flag = 0;
1423 }
1424 }
1425
1426 if (continue_flag)
1427 {
1428 if (execution_direction == EXEC_REVERSE)
1429 {
1430 if (record_full_list->prev)
1431 record_full_list = record_full_list->prev;
1432 }
1433 else
1434 {
1435 if (record_full_list->next)
1436 record_full_list = record_full_list->next;
1437 }
1438 }
1439 }
1440 while (continue_flag);
1441
1442 replay_out:
1443 if (record_full_get_sig)
1444 status->value.sig = GDB_SIGNAL_INT;
1445 else if (record_full_list->u.end.sigval != GDB_SIGNAL_0)
1446 /* FIXME: better way to check */
1447 status->value.sig = record_full_list->u.end.sigval;
1448 else
1449 status->value.sig = GDB_SIGNAL_TRAP;
1450
1451 discard_cleanups (old_cleanups);
1452 }
1453
1454 signal (SIGINT, handle_sigint);
1455
1456 return inferior_ptid;
1457 }
1458
1459 ptid_t
1460 record_full_base_target::wait (ptid_t ptid, struct target_waitstatus *status,
1461 int options)
1462 {
1463 ptid_t return_ptid;
1464
1465 return_ptid = record_full_wait_1 (this, ptid, status, options);
1466 if (status->kind != TARGET_WAITKIND_IGNORE)
1467 {
1468 /* We're reporting a stop. Make sure any spurious
1469 target_wait(WNOHANG) doesn't advance the target until the
1470 core wants us resumed again. */
1471 record_full_resumed = 0;
1472 }
1473 return return_ptid;
1474 }
1475
1476 bool
1477 record_full_base_target::stopped_by_watchpoint ()
1478 {
1479 if (RECORD_FULL_IS_REPLAY)
1480 return record_full_stop_reason == TARGET_STOPPED_BY_WATCHPOINT;
1481 else
1482 return beneath->stopped_by_watchpoint ();
1483 }
1484
1485 bool
1486 record_full_base_target::stopped_data_address (CORE_ADDR *addr_p)
1487 {
1488 if (RECORD_FULL_IS_REPLAY)
1489 return false;
1490 else
1491 return this->beneath->stopped_data_address (addr_p);
1492 }
1493
1494 /* The stopped_by_sw_breakpoint method of target record-full. */
1495
1496 bool
1497 record_full_base_target::stopped_by_sw_breakpoint ()
1498 {
1499 return record_full_stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT;
1500 }
1501
1502 /* The supports_stopped_by_sw_breakpoint method of target
1503 record-full. */
1504
1505 bool
1506 record_full_base_target::supports_stopped_by_sw_breakpoint ()
1507 {
1508 return true;
1509 }
1510
1511 /* The stopped_by_hw_breakpoint method of target record-full. */
1512
1513 bool
1514 record_full_base_target::stopped_by_hw_breakpoint ()
1515 {
1516 return record_full_stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT;
1517 }
1518
1519 /* The supports_stopped_by_sw_breakpoint method of target
1520 record-full. */
1521
1522 bool
1523 record_full_base_target::supports_stopped_by_hw_breakpoint ()
1524 {
1525 return true;
1526 }
1527
1528 /* Record registers change (by user or by GDB) to list as an instruction. */
1529
1530 static void
1531 record_full_registers_change (struct regcache *regcache, int regnum)
1532 {
1533 /* Check record_full_insn_num. */
1534 record_full_check_insn_num ();
1535
1536 record_full_arch_list_head = NULL;
1537 record_full_arch_list_tail = NULL;
1538
1539 if (regnum < 0)
1540 {
1541 int i;
1542
1543 for (i = 0; i < gdbarch_num_regs (regcache->arch ()); i++)
1544 {
1545 if (record_full_arch_list_add_reg (regcache, i))
1546 {
1547 record_full_list_release (record_full_arch_list_tail);
1548 error (_("Process record: failed to record execution log."));
1549 }
1550 }
1551 }
1552 else
1553 {
1554 if (record_full_arch_list_add_reg (regcache, regnum))
1555 {
1556 record_full_list_release (record_full_arch_list_tail);
1557 error (_("Process record: failed to record execution log."));
1558 }
1559 }
1560 if (record_full_arch_list_add_end ())
1561 {
1562 record_full_list_release (record_full_arch_list_tail);
1563 error (_("Process record: failed to record execution log."));
1564 }
1565 record_full_list->next = record_full_arch_list_head;
1566 record_full_arch_list_head->prev = record_full_list;
1567 record_full_list = record_full_arch_list_tail;
1568
1569 if (record_full_insn_num == record_full_insn_max_num)
1570 record_full_list_release_first ();
1571 else
1572 record_full_insn_num++;
1573 }
1574
1575 /* "store_registers" method for process record target. */
1576
1577 void
1578 record_full_target::store_registers (struct regcache *regcache, int regno)
1579 {
1580 if (!record_full_gdb_operation_disable)
1581 {
1582 if (RECORD_FULL_IS_REPLAY)
1583 {
1584 int n;
1585
1586 /* Let user choose if he wants to write register or not. */
1587 if (regno < 0)
1588 n =
1589 query (_("Because GDB is in replay mode, changing the "
1590 "value of a register will make the execution "
1591 "log unusable from this point onward. "
1592 "Change all registers?"));
1593 else
1594 n =
1595 query (_("Because GDB is in replay mode, changing the value "
1596 "of a register will make the execution log unusable "
1597 "from this point onward. Change register %s?"),
1598 gdbarch_register_name (regcache->arch (),
1599 regno));
1600
1601 if (!n)
1602 {
1603 /* Invalidate the value of regcache that was set in function
1604 "regcache_raw_write". */
1605 if (regno < 0)
1606 {
1607 int i;
1608
1609 for (i = 0;
1610 i < gdbarch_num_regs (regcache->arch ());
1611 i++)
1612 regcache_invalidate (regcache, i);
1613 }
1614 else
1615 regcache_invalidate (regcache, regno);
1616
1617 error (_("Process record canceled the operation."));
1618 }
1619
1620 /* Destroy the record from here forward. */
1621 record_full_list_release_following (record_full_list);
1622 }
1623
1624 record_full_registers_change (regcache, regno);
1625 }
1626 this->beneath->store_registers (regcache, regno);
1627 }
1628
1629 /* "xfer_partial" method. Behavior is conditional on
1630 RECORD_FULL_IS_REPLAY.
1631 In replay mode, we cannot write memory unles we are willing to
1632 invalidate the record/replay log from this point forward. */
1633
1634 enum target_xfer_status
1635 record_full_target::xfer_partial (enum target_object object,
1636 const char *annex, gdb_byte *readbuf,
1637 const gdb_byte *writebuf, ULONGEST offset,
1638 ULONGEST len, ULONGEST *xfered_len)
1639 {
1640 if (!record_full_gdb_operation_disable
1641 && (object == TARGET_OBJECT_MEMORY
1642 || object == TARGET_OBJECT_RAW_MEMORY) && writebuf)
1643 {
1644 if (RECORD_FULL_IS_REPLAY)
1645 {
1646 /* Let user choose if he wants to write memory or not. */
1647 if (!query (_("Because GDB is in replay mode, writing to memory "
1648 "will make the execution log unusable from this "
1649 "point onward. Write memory at address %s?"),
1650 paddress (target_gdbarch (), offset)))
1651 error (_("Process record canceled the operation."));
1652
1653 /* Destroy the record from here forward. */
1654 record_full_list_release_following (record_full_list);
1655 }
1656
1657 /* Check record_full_insn_num */
1658 record_full_check_insn_num ();
1659
1660 /* Record registers change to list as an instruction. */
1661 record_full_arch_list_head = NULL;
1662 record_full_arch_list_tail = NULL;
1663 if (record_full_arch_list_add_mem (offset, len))
1664 {
1665 record_full_list_release (record_full_arch_list_tail);
1666 if (record_debug)
1667 fprintf_unfiltered (gdb_stdlog,
1668 "Process record: failed to record "
1669 "execution log.");
1670 return TARGET_XFER_E_IO;
1671 }
1672 if (record_full_arch_list_add_end ())
1673 {
1674 record_full_list_release (record_full_arch_list_tail);
1675 if (record_debug)
1676 fprintf_unfiltered (gdb_stdlog,
1677 "Process record: failed to record "
1678 "execution log.");
1679 return TARGET_XFER_E_IO;
1680 }
1681 record_full_list->next = record_full_arch_list_head;
1682 record_full_arch_list_head->prev = record_full_list;
1683 record_full_list = record_full_arch_list_tail;
1684
1685 if (record_full_insn_num == record_full_insn_max_num)
1686 record_full_list_release_first ();
1687 else
1688 record_full_insn_num++;
1689 }
1690
1691 return this->beneath->xfer_partial (object, annex, readbuf, writebuf, offset,
1692 len, xfered_len);
1693 }
1694
1695 /* This structure represents a breakpoint inserted while the record
1696 target is active. We use this to know when to install/remove
1697 breakpoints in/from the target beneath. For example, a breakpoint
1698 may be inserted while recording, but removed when not replaying nor
1699 recording. In that case, the breakpoint had not been inserted on
1700 the target beneath, so we should not try to remove it there. */
1701
1702 struct record_full_breakpoint
1703 {
1704 /* The address and address space the breakpoint was set at. */
1705 struct address_space *address_space;
1706 CORE_ADDR addr;
1707
1708 /* True when the breakpoint has been also installed in the target
1709 beneath. This will be false for breakpoints set during replay or
1710 when recording. */
1711 int in_target_beneath;
1712 };
1713
1714 typedef struct record_full_breakpoint *record_full_breakpoint_p;
1715 DEF_VEC_P(record_full_breakpoint_p);
1716
1717 /* The list of breakpoints inserted while the record target is
1718 active. */
1719 VEC(record_full_breakpoint_p) *record_full_breakpoints = NULL;
1720
1721 static void
1722 record_full_sync_record_breakpoints (struct bp_location *loc, void *data)
1723 {
1724 if (loc->loc_type != bp_loc_software_breakpoint)
1725 return;
1726
1727 if (loc->inserted)
1728 {
1729 struct record_full_breakpoint *bp = XNEW (struct record_full_breakpoint);
1730
1731 bp->addr = loc->target_info.placed_address;
1732 bp->address_space = loc->target_info.placed_address_space;
1733
1734 bp->in_target_beneath = 1;
1735
1736 VEC_safe_push (record_full_breakpoint_p, record_full_breakpoints, bp);
1737 }
1738 }
1739
1740 /* Sync existing breakpoints to record_full_breakpoints. */
1741
1742 static void
1743 record_full_init_record_breakpoints (void)
1744 {
1745 VEC_free (record_full_breakpoint_p, record_full_breakpoints);
1746
1747 iterate_over_bp_locations (record_full_sync_record_breakpoints);
1748 }
1749
1750 /* Behavior is conditional on RECORD_FULL_IS_REPLAY. We will not actually
1751 insert or remove breakpoints in the real target when replaying, nor
1752 when recording. */
1753
1754 int
1755 record_full_target::insert_breakpoint (struct gdbarch *gdbarch,
1756 struct bp_target_info *bp_tgt)
1757 {
1758 struct record_full_breakpoint *bp;
1759 int in_target_beneath = 0;
1760 int ix;
1761
1762 if (!RECORD_FULL_IS_REPLAY)
1763 {
1764 /* When recording, we currently always single-step, so we don't
1765 really need to install regular breakpoints in the inferior.
1766 However, we do have to insert software single-step
1767 breakpoints, in case the target can't hardware step. To keep
1768 things simple, we always insert. */
1769
1770 scoped_restore restore_operation_disable
1771 = record_full_gdb_operation_disable_set ();
1772
1773 int ret = this->beneath->insert_breakpoint (gdbarch, bp_tgt);
1774 if (ret != 0)
1775 return ret;
1776
1777 in_target_beneath = 1;
1778 }
1779
1780 /* Use the existing entries if found in order to avoid duplication
1781 in record_full_breakpoints. */
1782
1783 for (ix = 0;
1784 VEC_iterate (record_full_breakpoint_p,
1785 record_full_breakpoints, ix, bp);
1786 ++ix)
1787 {
1788 if (bp->addr == bp_tgt->placed_address
1789 && bp->address_space == bp_tgt->placed_address_space)
1790 {
1791 gdb_assert (bp->in_target_beneath == in_target_beneath);
1792 return 0;
1793 }
1794 }
1795
1796 bp = XNEW (struct record_full_breakpoint);
1797 bp->addr = bp_tgt->placed_address;
1798 bp->address_space = bp_tgt->placed_address_space;
1799 bp->in_target_beneath = in_target_beneath;
1800 VEC_safe_push (record_full_breakpoint_p, record_full_breakpoints, bp);
1801 return 0;
1802 }
1803
1804 /* "remove_breakpoint" method for process record target. */
1805
1806 int
1807 record_full_target::remove_breakpoint (struct gdbarch *gdbarch,
1808 struct bp_target_info *bp_tgt,
1809 enum remove_bp_reason reason)
1810 {
1811 struct record_full_breakpoint *bp;
1812 int ix;
1813
1814 for (ix = 0;
1815 VEC_iterate (record_full_breakpoint_p,
1816 record_full_breakpoints, ix, bp);
1817 ++ix)
1818 {
1819 if (bp->addr == bp_tgt->placed_address
1820 && bp->address_space == bp_tgt->placed_address_space)
1821 {
1822 if (bp->in_target_beneath)
1823 {
1824 scoped_restore restore_operation_disable
1825 = record_full_gdb_operation_disable_set ();
1826
1827 int ret = this->beneath->remove_breakpoint (gdbarch, bp_tgt,
1828 reason);
1829 if (ret != 0)
1830 return ret;
1831 }
1832
1833 if (reason == REMOVE_BREAKPOINT)
1834 {
1835 VEC_unordered_remove (record_full_breakpoint_p,
1836 record_full_breakpoints, ix);
1837 }
1838 return 0;
1839 }
1840 }
1841
1842 gdb_assert_not_reached ("removing unknown breakpoint");
1843 }
1844
1845 /* "can_execute_reverse" method for process record target. */
1846
1847 bool
1848 record_full_base_target::can_execute_reverse ()
1849 {
1850 return true;
1851 }
1852
1853 /* "get_bookmark" method for process record and prec over core. */
1854
1855 gdb_byte *
1856 record_full_base_target::get_bookmark (const char *args, int from_tty)
1857 {
1858 char *ret = NULL;
1859
1860 /* Return stringified form of instruction count. */
1861 if (record_full_list && record_full_list->type == record_full_end)
1862 ret = xstrdup (pulongest (record_full_list->u.end.insn_num));
1863
1864 if (record_debug)
1865 {
1866 if (ret)
1867 fprintf_unfiltered (gdb_stdlog,
1868 "record_full_get_bookmark returns %s\n", ret);
1869 else
1870 fprintf_unfiltered (gdb_stdlog,
1871 "record_full_get_bookmark returns NULL\n");
1872 }
1873 return (gdb_byte *) ret;
1874 }
1875
1876 /* "goto_bookmark" method for process record and prec over core. */
1877
1878 void
1879 record_full_base_target::goto_bookmark (const gdb_byte *raw_bookmark,
1880 int from_tty)
1881 {
1882 const char *bookmark = (const char *) raw_bookmark;
1883
1884 if (record_debug)
1885 fprintf_unfiltered (gdb_stdlog,
1886 "record_full_goto_bookmark receives %s\n", bookmark);
1887
1888 std::string name_holder;
1889 if (bookmark[0] == '\'' || bookmark[0] == '\"')
1890 {
1891 if (bookmark[strlen (bookmark) - 1] != bookmark[0])
1892 error (_("Unbalanced quotes: %s"), bookmark);
1893
1894 name_holder = std::string (bookmark + 1, strlen (bookmark) - 2);
1895 bookmark = name_holder.c_str ();
1896 }
1897
1898 record_goto (bookmark);
1899 }
1900
1901 enum exec_direction_kind
1902 record_full_base_target::execution_direction ()
1903 {
1904 return record_full_execution_dir;
1905 }
1906
1907 /* The record_method method of target record-full. */
1908
1909 enum record_method
1910 record_full_base_target::record_method (ptid_t ptid)
1911 {
1912 return RECORD_METHOD_FULL;
1913 }
1914
1915 void
1916 record_full_base_target::info_record ()
1917 {
1918 struct record_full_entry *p;
1919
1920 if (RECORD_FULL_IS_REPLAY)
1921 printf_filtered (_("Replay mode:\n"));
1922 else
1923 printf_filtered (_("Record mode:\n"));
1924
1925 /* Find entry for first actual instruction in the log. */
1926 for (p = record_full_first.next;
1927 p != NULL && p->type != record_full_end;
1928 p = p->next)
1929 ;
1930
1931 /* Do we have a log at all? */
1932 if (p != NULL && p->type == record_full_end)
1933 {
1934 /* Display instruction number for first instruction in the log. */
1935 printf_filtered (_("Lowest recorded instruction number is %s.\n"),
1936 pulongest (p->u.end.insn_num));
1937
1938 /* If in replay mode, display where we are in the log. */
1939 if (RECORD_FULL_IS_REPLAY)
1940 printf_filtered (_("Current instruction number is %s.\n"),
1941 pulongest (record_full_list->u.end.insn_num));
1942
1943 /* Display instruction number for last instruction in the log. */
1944 printf_filtered (_("Highest recorded instruction number is %s.\n"),
1945 pulongest (record_full_insn_count));
1946
1947 /* Display log count. */
1948 printf_filtered (_("Log contains %u instructions.\n"),
1949 record_full_insn_num);
1950 }
1951 else
1952 printf_filtered (_("No instructions have been logged.\n"));
1953
1954 /* Display max log size. */
1955 printf_filtered (_("Max logged instructions is %u.\n"),
1956 record_full_insn_max_num);
1957 }
1958
1959 bool
1960 record_full_base_target::supports_delete_record ()
1961 {
1962 return true;
1963 }
1964
1965 /* The "delete_record" target method. */
1966
1967 void
1968 record_full_base_target::delete_record ()
1969 {
1970 record_full_list_release_following (record_full_list);
1971 }
1972
1973 /* The "record_is_replaying" target method. */
1974
1975 bool
1976 record_full_base_target::record_is_replaying (ptid_t ptid)
1977 {
1978 return RECORD_FULL_IS_REPLAY;
1979 }
1980
1981 /* The "record_will_replay" target method. */
1982
1983 bool
1984 record_full_base_target::record_will_replay (ptid_t ptid, int dir)
1985 {
1986 /* We can currently only record when executing forwards. Should we be able
1987 to record when executing backwards on targets that support reverse
1988 execution, this needs to be changed. */
1989
1990 return RECORD_FULL_IS_REPLAY || dir == EXEC_REVERSE;
1991 }
1992
1993 /* Go to a specific entry. */
1994
1995 static void
1996 record_full_goto_entry (struct record_full_entry *p)
1997 {
1998 if (p == NULL)
1999 error (_("Target insn not found."));
2000 else if (p == record_full_list)
2001 error (_("Already at target insn."));
2002 else if (p->u.end.insn_num > record_full_list->u.end.insn_num)
2003 {
2004 printf_filtered (_("Go forward to insn number %s\n"),
2005 pulongest (p->u.end.insn_num));
2006 record_full_goto_insn (p, EXEC_FORWARD);
2007 }
2008 else
2009 {
2010 printf_filtered (_("Go backward to insn number %s\n"),
2011 pulongest (p->u.end.insn_num));
2012 record_full_goto_insn (p, EXEC_REVERSE);
2013 }
2014
2015 registers_changed ();
2016 reinit_frame_cache ();
2017 stop_pc = regcache_read_pc (get_current_regcache ());
2018 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
2019 }
2020
2021 /* The "goto_record_begin" target method. */
2022
2023 void
2024 record_full_base_target::goto_record_begin ()
2025 {
2026 struct record_full_entry *p = NULL;
2027
2028 for (p = &record_full_first; p != NULL; p = p->next)
2029 if (p->type == record_full_end)
2030 break;
2031
2032 record_full_goto_entry (p);
2033 }
2034
2035 /* The "goto_record_end" target method. */
2036
2037 void
2038 record_full_base_target::goto_record_end ()
2039 {
2040 struct record_full_entry *p = NULL;
2041
2042 for (p = record_full_list; p->next != NULL; p = p->next)
2043 ;
2044 for (; p!= NULL; p = p->prev)
2045 if (p->type == record_full_end)
2046 break;
2047
2048 record_full_goto_entry (p);
2049 }
2050
2051 /* The "goto_record" target method. */
2052
2053 void
2054 record_full_base_target::goto_record (ULONGEST target_insn)
2055 {
2056 struct record_full_entry *p = NULL;
2057
2058 for (p = &record_full_first; p != NULL; p = p->next)
2059 if (p->type == record_full_end && p->u.end.insn_num == target_insn)
2060 break;
2061
2062 record_full_goto_entry (p);
2063 }
2064
2065 /* The "record_stop_replaying" target method. */
2066
2067 void
2068 record_full_base_target::record_stop_replaying ()
2069 {
2070 goto_record_end ();
2071 }
2072
2073 /* "resume" method for prec over corefile. */
2074
2075 void
2076 record_full_core_target::resume (ptid_t ptid, int step,
2077 enum gdb_signal signal)
2078 {
2079 record_full_resume_step = step;
2080 record_full_resumed = 1;
2081 record_full_execution_dir = ::execution_direction;
2082
2083 /* We are about to start executing the inferior (or simulate it),
2084 let's register it with the event loop. */
2085 if (target_can_async_p ())
2086 target_async (1);
2087 }
2088
2089 /* "kill" method for prec over corefile. */
2090
2091 void
2092 record_full_core_target::kill ()
2093 {
2094 if (record_debug)
2095 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_core_kill\n");
2096
2097 unpush_target (this);
2098 }
2099
2100 /* "fetch_registers" method for prec over corefile. */
2101
2102 void
2103 record_full_core_target::fetch_registers (struct regcache *regcache,
2104 int regno)
2105 {
2106 if (regno < 0)
2107 {
2108 int num = gdbarch_num_regs (regcache->arch ());
2109 int i;
2110
2111 for (i = 0; i < num; i ++)
2112 regcache->raw_supply (i, *record_full_core_regbuf);
2113 }
2114 else
2115 regcache->raw_supply (regno, *record_full_core_regbuf);
2116 }
2117
2118 /* "prepare_to_store" method for prec over corefile. */
2119
2120 void
2121 record_full_core_target::prepare_to_store (struct regcache *regcache)
2122 {
2123 }
2124
2125 /* "store_registers" method for prec over corefile. */
2126
2127 void
2128 record_full_core_target::store_registers (struct regcache *regcache,
2129 int regno)
2130 {
2131 if (record_full_gdb_operation_disable)
2132 record_full_core_regbuf->raw_supply (regno, *regcache);
2133 else
2134 error (_("You can't do that without a process to debug."));
2135 }
2136
2137 /* "xfer_partial" method for prec over corefile. */
2138
2139 enum target_xfer_status
2140 record_full_core_target::xfer_partial (enum target_object object,
2141 const char *annex, gdb_byte *readbuf,
2142 const gdb_byte *writebuf, ULONGEST offset,
2143 ULONGEST len, ULONGEST *xfered_len)
2144 {
2145 if (object == TARGET_OBJECT_MEMORY)
2146 {
2147 if (record_full_gdb_operation_disable || !writebuf)
2148 {
2149 struct target_section *p;
2150
2151 for (p = record_full_core_start; p < record_full_core_end; p++)
2152 {
2153 if (offset >= p->addr)
2154 {
2155 struct record_full_core_buf_entry *entry;
2156 ULONGEST sec_offset;
2157
2158 if (offset >= p->endaddr)
2159 continue;
2160
2161 if (offset + len > p->endaddr)
2162 len = p->endaddr - offset;
2163
2164 sec_offset = offset - p->addr;
2165
2166 /* Read readbuf or write writebuf p, offset, len. */
2167 /* Check flags. */
2168 if (p->the_bfd_section->flags & SEC_CONSTRUCTOR
2169 || (p->the_bfd_section->flags & SEC_HAS_CONTENTS) == 0)
2170 {
2171 if (readbuf)
2172 memset (readbuf, 0, len);
2173
2174 *xfered_len = len;
2175 return TARGET_XFER_OK;
2176 }
2177 /* Get record_full_core_buf_entry. */
2178 for (entry = record_full_core_buf_list; entry;
2179 entry = entry->prev)
2180 if (entry->p == p)
2181 break;
2182 if (writebuf)
2183 {
2184 if (!entry)
2185 {
2186 /* Add a new entry. */
2187 entry = XNEW (struct record_full_core_buf_entry);
2188 entry->p = p;
2189 if (!bfd_malloc_and_get_section
2190 (p->the_bfd_section->owner,
2191 p->the_bfd_section,
2192 &entry->buf))
2193 {
2194 xfree (entry);
2195 return TARGET_XFER_EOF;
2196 }
2197 entry->prev = record_full_core_buf_list;
2198 record_full_core_buf_list = entry;
2199 }
2200
2201 memcpy (entry->buf + sec_offset, writebuf,
2202 (size_t) len);
2203 }
2204 else
2205 {
2206 if (!entry)
2207 return this->beneath->xfer_partial (object, annex,
2208 readbuf, writebuf,
2209 offset, len,
2210 xfered_len);
2211
2212 memcpy (readbuf, entry->buf + sec_offset,
2213 (size_t) len);
2214 }
2215
2216 *xfered_len = len;
2217 return TARGET_XFER_OK;
2218 }
2219 }
2220
2221 return TARGET_XFER_E_IO;
2222 }
2223 else
2224 error (_("You can't do that without a process to debug."));
2225 }
2226
2227 return this->beneath->xfer_partial (object, annex,
2228 readbuf, writebuf, offset, len,
2229 xfered_len);
2230 }
2231
2232 /* "insert_breakpoint" method for prec over corefile. */
2233
2234 int
2235 record_full_core_target::insert_breakpoint (struct gdbarch *gdbarch,
2236 struct bp_target_info *bp_tgt)
2237 {
2238 return 0;
2239 }
2240
2241 /* "remove_breakpoint" method for prec over corefile. */
2242
2243 int
2244 record_full_core_target::remove_breakpoint (struct gdbarch *gdbarch,
2245 struct bp_target_info *bp_tgt,
2246 enum remove_bp_reason reason)
2247 {
2248 return 0;
2249 }
2250
2251 /* "has_execution" method for prec over corefile. */
2252
2253 bool
2254 record_full_core_target::has_execution (ptid_t the_ptid)
2255 {
2256 return true;
2257 }
2258
2259 /* Record log save-file format
2260 Version 1 (never released)
2261
2262 Header:
2263 4 bytes: magic number htonl(0x20090829).
2264 NOTE: be sure to change whenever this file format changes!
2265
2266 Records:
2267 record_full_end:
2268 1 byte: record type (record_full_end, see enum record_full_type).
2269 record_full_reg:
2270 1 byte: record type (record_full_reg, see enum record_full_type).
2271 8 bytes: register id (network byte order).
2272 MAX_REGISTER_SIZE bytes: register value.
2273 record_full_mem:
2274 1 byte: record type (record_full_mem, see enum record_full_type).
2275 8 bytes: memory length (network byte order).
2276 8 bytes: memory address (network byte order).
2277 n bytes: memory value (n == memory length).
2278
2279 Version 2
2280 4 bytes: magic number netorder32(0x20091016).
2281 NOTE: be sure to change whenever this file format changes!
2282
2283 Records:
2284 record_full_end:
2285 1 byte: record type (record_full_end, see enum record_full_type).
2286 4 bytes: signal
2287 4 bytes: instruction count
2288 record_full_reg:
2289 1 byte: record type (record_full_reg, see enum record_full_type).
2290 4 bytes: register id (network byte order).
2291 n bytes: register value (n == actual register size).
2292 (eg. 4 bytes for x86 general registers).
2293 record_full_mem:
2294 1 byte: record type (record_full_mem, see enum record_full_type).
2295 4 bytes: memory length (network byte order).
2296 8 bytes: memory address (network byte order).
2297 n bytes: memory value (n == memory length).
2298
2299 */
2300
2301 /* bfdcore_read -- read bytes from a core file section. */
2302
2303 static inline void
2304 bfdcore_read (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2305 {
2306 int ret = bfd_get_section_contents (obfd, osec, buf, *offset, len);
2307
2308 if (ret)
2309 *offset += len;
2310 else
2311 error (_("Failed to read %d bytes from core file %s ('%s')."),
2312 len, bfd_get_filename (obfd),
2313 bfd_errmsg (bfd_get_error ()));
2314 }
2315
2316 static inline uint64_t
2317 netorder64 (uint64_t input)
2318 {
2319 uint64_t ret;
2320
2321 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2322 BFD_ENDIAN_BIG, input);
2323 return ret;
2324 }
2325
2326 static inline uint32_t
2327 netorder32 (uint32_t input)
2328 {
2329 uint32_t ret;
2330
2331 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2332 BFD_ENDIAN_BIG, input);
2333 return ret;
2334 }
2335
2336 /* Restore the execution log from a core_bfd file. */
2337 static void
2338 record_full_restore (void)
2339 {
2340 uint32_t magic;
2341 struct cleanup *old_cleanups;
2342 struct record_full_entry *rec;
2343 asection *osec;
2344 uint32_t osec_size;
2345 int bfd_offset = 0;
2346 struct regcache *regcache;
2347
2348 /* We restore the execution log from the open core bfd,
2349 if there is one. */
2350 if (core_bfd == NULL)
2351 return;
2352
2353 /* "record_full_restore" can only be called when record list is empty. */
2354 gdb_assert (record_full_first.next == NULL);
2355
2356 if (record_debug)
2357 fprintf_unfiltered (gdb_stdlog, "Restoring recording from core file.\n");
2358
2359 /* Now need to find our special note section. */
2360 osec = bfd_get_section_by_name (core_bfd, "null0");
2361 if (record_debug)
2362 fprintf_unfiltered (gdb_stdlog, "Find precord section %s.\n",
2363 osec ? "succeeded" : "failed");
2364 if (osec == NULL)
2365 return;
2366 osec_size = bfd_section_size (core_bfd, osec);
2367 if (record_debug)
2368 fprintf_unfiltered (gdb_stdlog, "%s", bfd_section_name (core_bfd, osec));
2369
2370 /* Check the magic code. */
2371 bfdcore_read (core_bfd, osec, &magic, sizeof (magic), &bfd_offset);
2372 if (magic != RECORD_FULL_FILE_MAGIC)
2373 error (_("Version mis-match or file format error in core file %s."),
2374 bfd_get_filename (core_bfd));
2375 if (record_debug)
2376 fprintf_unfiltered (gdb_stdlog,
2377 " Reading 4-byte magic cookie "
2378 "RECORD_FULL_FILE_MAGIC (0x%s)\n",
2379 phex_nz (netorder32 (magic), 4));
2380
2381 /* Restore the entries in recfd into record_full_arch_list_head and
2382 record_full_arch_list_tail. */
2383 record_full_arch_list_head = NULL;
2384 record_full_arch_list_tail = NULL;
2385 record_full_insn_num = 0;
2386 old_cleanups = make_cleanup (record_full_arch_list_cleanups, 0);
2387 regcache = get_current_regcache ();
2388
2389 while (1)
2390 {
2391 uint8_t rectype;
2392 uint32_t regnum, len, signal, count;
2393 uint64_t addr;
2394
2395 /* We are finished when offset reaches osec_size. */
2396 if (bfd_offset >= osec_size)
2397 break;
2398 bfdcore_read (core_bfd, osec, &rectype, sizeof (rectype), &bfd_offset);
2399
2400 switch (rectype)
2401 {
2402 case record_full_reg: /* reg */
2403 /* Get register number to regnum. */
2404 bfdcore_read (core_bfd, osec, &regnum,
2405 sizeof (regnum), &bfd_offset);
2406 regnum = netorder32 (regnum);
2407
2408 rec = record_full_reg_alloc (regcache, regnum);
2409
2410 /* Get val. */
2411 bfdcore_read (core_bfd, osec, record_full_get_loc (rec),
2412 rec->u.reg.len, &bfd_offset);
2413
2414 if (record_debug)
2415 fprintf_unfiltered (gdb_stdlog,
2416 " Reading register %d (1 "
2417 "plus %lu plus %d bytes)\n",
2418 rec->u.reg.num,
2419 (unsigned long) sizeof (regnum),
2420 rec->u.reg.len);
2421 break;
2422
2423 case record_full_mem: /* mem */
2424 /* Get len. */
2425 bfdcore_read (core_bfd, osec, &len,
2426 sizeof (len), &bfd_offset);
2427 len = netorder32 (len);
2428
2429 /* Get addr. */
2430 bfdcore_read (core_bfd, osec, &addr,
2431 sizeof (addr), &bfd_offset);
2432 addr = netorder64 (addr);
2433
2434 rec = record_full_mem_alloc (addr, len);
2435
2436 /* Get val. */
2437 bfdcore_read (core_bfd, osec, record_full_get_loc (rec),
2438 rec->u.mem.len, &bfd_offset);
2439
2440 if (record_debug)
2441 fprintf_unfiltered (gdb_stdlog,
2442 " Reading memory %s (1 plus "
2443 "%lu plus %lu plus %d bytes)\n",
2444 paddress (get_current_arch (),
2445 rec->u.mem.addr),
2446 (unsigned long) sizeof (addr),
2447 (unsigned long) sizeof (len),
2448 rec->u.mem.len);
2449 break;
2450
2451 case record_full_end: /* end */
2452 rec = record_full_end_alloc ();
2453 record_full_insn_num ++;
2454
2455 /* Get signal value. */
2456 bfdcore_read (core_bfd, osec, &signal,
2457 sizeof (signal), &bfd_offset);
2458 signal = netorder32 (signal);
2459 rec->u.end.sigval = (enum gdb_signal) signal;
2460
2461 /* Get insn count. */
2462 bfdcore_read (core_bfd, osec, &count,
2463 sizeof (count), &bfd_offset);
2464 count = netorder32 (count);
2465 rec->u.end.insn_num = count;
2466 record_full_insn_count = count + 1;
2467 if (record_debug)
2468 fprintf_unfiltered (gdb_stdlog,
2469 " Reading record_full_end (1 + "
2470 "%lu + %lu bytes), offset == %s\n",
2471 (unsigned long) sizeof (signal),
2472 (unsigned long) sizeof (count),
2473 paddress (get_current_arch (),
2474 bfd_offset));
2475 break;
2476
2477 default:
2478 error (_("Bad entry type in core file %s."),
2479 bfd_get_filename (core_bfd));
2480 break;
2481 }
2482
2483 /* Add rec to record arch list. */
2484 record_full_arch_list_add (rec);
2485 }
2486
2487 discard_cleanups (old_cleanups);
2488
2489 /* Add record_full_arch_list_head to the end of record list. */
2490 record_full_first.next = record_full_arch_list_head;
2491 record_full_arch_list_head->prev = &record_full_first;
2492 record_full_arch_list_tail->next = NULL;
2493 record_full_list = &record_full_first;
2494
2495 /* Update record_full_insn_max_num. */
2496 if (record_full_insn_num > record_full_insn_max_num)
2497 {
2498 record_full_insn_max_num = record_full_insn_num;
2499 warning (_("Auto increase record/replay buffer limit to %u."),
2500 record_full_insn_max_num);
2501 }
2502
2503 /* Succeeded. */
2504 printf_filtered (_("Restored records from core file %s.\n"),
2505 bfd_get_filename (core_bfd));
2506
2507 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
2508 }
2509
2510 /* bfdcore_write -- write bytes into a core file section. */
2511
2512 static inline void
2513 bfdcore_write (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2514 {
2515 int ret = bfd_set_section_contents (obfd, osec, buf, *offset, len);
2516
2517 if (ret)
2518 *offset += len;
2519 else
2520 error (_("Failed to write %d bytes to core file %s ('%s')."),
2521 len, bfd_get_filename (obfd),
2522 bfd_errmsg (bfd_get_error ()));
2523 }
2524
2525 /* Restore the execution log from a file. We use a modified elf
2526 corefile format, with an extra section for our data. */
2527
2528 static void
2529 cmd_record_full_restore (const char *args, int from_tty)
2530 {
2531 core_file_command (args, from_tty);
2532 record_full_open (args, from_tty);
2533 }
2534
2535 /* Save the execution log to a file. We use a modified elf corefile
2536 format, with an extra section for our data. */
2537
2538 void
2539 record_full_base_target::save_record (const char *recfilename)
2540 {
2541 struct record_full_entry *cur_record_full_list;
2542 uint32_t magic;
2543 struct regcache *regcache;
2544 struct gdbarch *gdbarch;
2545 int save_size = 0;
2546 asection *osec = NULL;
2547 int bfd_offset = 0;
2548
2549 /* Open the save file. */
2550 if (record_debug)
2551 fprintf_unfiltered (gdb_stdlog, "Saving execution log to core file '%s'\n",
2552 recfilename);
2553
2554 /* Open the output file. */
2555 gdb_bfd_ref_ptr obfd (create_gcore_bfd (recfilename));
2556
2557 /* Arrange to remove the output file on failure. */
2558 gdb::unlinker unlink_file (recfilename);
2559
2560 /* Save the current record entry to "cur_record_full_list". */
2561 cur_record_full_list = record_full_list;
2562
2563 /* Get the values of regcache and gdbarch. */
2564 regcache = get_current_regcache ();
2565 gdbarch = regcache->arch ();
2566
2567 /* Disable the GDB operation record. */
2568 scoped_restore restore_operation_disable
2569 = record_full_gdb_operation_disable_set ();
2570
2571 /* Reverse execute to the begin of record list. */
2572 while (1)
2573 {
2574 /* Check for beginning and end of log. */
2575 if (record_full_list == &record_full_first)
2576 break;
2577
2578 record_full_exec_insn (regcache, gdbarch, record_full_list);
2579
2580 if (record_full_list->prev)
2581 record_full_list = record_full_list->prev;
2582 }
2583
2584 /* Compute the size needed for the extra bfd section. */
2585 save_size = 4; /* magic cookie */
2586 for (record_full_list = record_full_first.next; record_full_list;
2587 record_full_list = record_full_list->next)
2588 switch (record_full_list->type)
2589 {
2590 case record_full_end:
2591 save_size += 1 + 4 + 4;
2592 break;
2593 case record_full_reg:
2594 save_size += 1 + 4 + record_full_list->u.reg.len;
2595 break;
2596 case record_full_mem:
2597 save_size += 1 + 4 + 8 + record_full_list->u.mem.len;
2598 break;
2599 }
2600
2601 /* Make the new bfd section. */
2602 osec = bfd_make_section_anyway_with_flags (obfd.get (), "precord",
2603 SEC_HAS_CONTENTS
2604 | SEC_READONLY);
2605 if (osec == NULL)
2606 error (_("Failed to create 'precord' section for corefile %s: %s"),
2607 recfilename,
2608 bfd_errmsg (bfd_get_error ()));
2609 bfd_set_section_size (obfd.get (), osec, save_size);
2610 bfd_set_section_vma (obfd.get (), osec, 0);
2611 bfd_set_section_alignment (obfd.get (), osec, 0);
2612 bfd_section_lma (obfd.get (), osec) = 0;
2613
2614 /* Save corefile state. */
2615 write_gcore_file (obfd.get ());
2616
2617 /* Write out the record log. */
2618 /* Write the magic code. */
2619 magic = RECORD_FULL_FILE_MAGIC;
2620 if (record_debug)
2621 fprintf_unfiltered (gdb_stdlog,
2622 " Writing 4-byte magic cookie "
2623 "RECORD_FULL_FILE_MAGIC (0x%s)\n",
2624 phex_nz (magic, 4));
2625 bfdcore_write (obfd.get (), osec, &magic, sizeof (magic), &bfd_offset);
2626
2627 /* Save the entries to recfd and forward execute to the end of
2628 record list. */
2629 record_full_list = &record_full_first;
2630 while (1)
2631 {
2632 /* Save entry. */
2633 if (record_full_list != &record_full_first)
2634 {
2635 uint8_t type;
2636 uint32_t regnum, len, signal, count;
2637 uint64_t addr;
2638
2639 type = record_full_list->type;
2640 bfdcore_write (obfd.get (), osec, &type, sizeof (type), &bfd_offset);
2641
2642 switch (record_full_list->type)
2643 {
2644 case record_full_reg: /* reg */
2645 if (record_debug)
2646 fprintf_unfiltered (gdb_stdlog,
2647 " Writing register %d (1 "
2648 "plus %lu plus %d bytes)\n",
2649 record_full_list->u.reg.num,
2650 (unsigned long) sizeof (regnum),
2651 record_full_list->u.reg.len);
2652
2653 /* Write regnum. */
2654 regnum = netorder32 (record_full_list->u.reg.num);
2655 bfdcore_write (obfd.get (), osec, &regnum,
2656 sizeof (regnum), &bfd_offset);
2657
2658 /* Write regval. */
2659 bfdcore_write (obfd.get (), osec,
2660 record_full_get_loc (record_full_list),
2661 record_full_list->u.reg.len, &bfd_offset);
2662 break;
2663
2664 case record_full_mem: /* mem */
2665 if (record_debug)
2666 fprintf_unfiltered (gdb_stdlog,
2667 " Writing memory %s (1 plus "
2668 "%lu plus %lu plus %d bytes)\n",
2669 paddress (gdbarch,
2670 record_full_list->u.mem.addr),
2671 (unsigned long) sizeof (addr),
2672 (unsigned long) sizeof (len),
2673 record_full_list->u.mem.len);
2674
2675 /* Write memlen. */
2676 len = netorder32 (record_full_list->u.mem.len);
2677 bfdcore_write (obfd.get (), osec, &len, sizeof (len),
2678 &bfd_offset);
2679
2680 /* Write memaddr. */
2681 addr = netorder64 (record_full_list->u.mem.addr);
2682 bfdcore_write (obfd.get (), osec, &addr,
2683 sizeof (addr), &bfd_offset);
2684
2685 /* Write memval. */
2686 bfdcore_write (obfd.get (), osec,
2687 record_full_get_loc (record_full_list),
2688 record_full_list->u.mem.len, &bfd_offset);
2689 break;
2690
2691 case record_full_end:
2692 if (record_debug)
2693 fprintf_unfiltered (gdb_stdlog,
2694 " Writing record_full_end (1 + "
2695 "%lu + %lu bytes)\n",
2696 (unsigned long) sizeof (signal),
2697 (unsigned long) sizeof (count));
2698 /* Write signal value. */
2699 signal = netorder32 (record_full_list->u.end.sigval);
2700 bfdcore_write (obfd.get (), osec, &signal,
2701 sizeof (signal), &bfd_offset);
2702
2703 /* Write insn count. */
2704 count = netorder32 (record_full_list->u.end.insn_num);
2705 bfdcore_write (obfd.get (), osec, &count,
2706 sizeof (count), &bfd_offset);
2707 break;
2708 }
2709 }
2710
2711 /* Execute entry. */
2712 record_full_exec_insn (regcache, gdbarch, record_full_list);
2713
2714 if (record_full_list->next)
2715 record_full_list = record_full_list->next;
2716 else
2717 break;
2718 }
2719
2720 /* Reverse execute to cur_record_full_list. */
2721 while (1)
2722 {
2723 /* Check for beginning and end of log. */
2724 if (record_full_list == cur_record_full_list)
2725 break;
2726
2727 record_full_exec_insn (regcache, gdbarch, record_full_list);
2728
2729 if (record_full_list->prev)
2730 record_full_list = record_full_list->prev;
2731 }
2732
2733 unlink_file.keep ();
2734
2735 /* Succeeded. */
2736 printf_filtered (_("Saved core file %s with execution log.\n"),
2737 recfilename);
2738 }
2739
2740 /* record_full_goto_insn -- rewind the record log (forward or backward,
2741 depending on DIR) to the given entry, changing the program state
2742 correspondingly. */
2743
2744 static void
2745 record_full_goto_insn (struct record_full_entry *entry,
2746 enum exec_direction_kind dir)
2747 {
2748 scoped_restore restore_operation_disable
2749 = record_full_gdb_operation_disable_set ();
2750 struct regcache *regcache = get_current_regcache ();
2751 struct gdbarch *gdbarch = regcache->arch ();
2752
2753 /* Assume everything is valid: we will hit the entry,
2754 and we will not hit the end of the recording. */
2755
2756 if (dir == EXEC_FORWARD)
2757 record_full_list = record_full_list->next;
2758
2759 do
2760 {
2761 record_full_exec_insn (regcache, gdbarch, record_full_list);
2762 if (dir == EXEC_REVERSE)
2763 record_full_list = record_full_list->prev;
2764 else
2765 record_full_list = record_full_list->next;
2766 } while (record_full_list != entry);
2767 }
2768
2769 /* Alias for "target record-full". */
2770
2771 static void
2772 cmd_record_full_start (const char *args, int from_tty)
2773 {
2774 execute_command ("target record-full", from_tty);
2775 }
2776
2777 static void
2778 set_record_full_insn_max_num (const char *args, int from_tty,
2779 struct cmd_list_element *c)
2780 {
2781 if (record_full_insn_num > record_full_insn_max_num)
2782 {
2783 /* Count down record_full_insn_num while releasing records from list. */
2784 while (record_full_insn_num > record_full_insn_max_num)
2785 {
2786 record_full_list_release_first ();
2787 record_full_insn_num--;
2788 }
2789 }
2790 }
2791
2792 /* The "set record full" command. */
2793
2794 static void
2795 set_record_full_command (const char *args, int from_tty)
2796 {
2797 printf_unfiltered (_("\"set record full\" must be followed "
2798 "by an appropriate subcommand.\n"));
2799 help_list (set_record_full_cmdlist, "set record full ", all_commands,
2800 gdb_stdout);
2801 }
2802
2803 /* The "show record full" command. */
2804
2805 static void
2806 show_record_full_command (const char *args, int from_tty)
2807 {
2808 cmd_show_list (show_record_full_cmdlist, from_tty, "");
2809 }
2810
2811 void
2812 _initialize_record_full (void)
2813 {
2814 struct cmd_list_element *c;
2815
2816 /* Init record_full_first. */
2817 record_full_first.prev = NULL;
2818 record_full_first.next = NULL;
2819 record_full_first.type = record_full_end;
2820
2821 add_target (record_full_target_info, record_full_open);
2822 add_deprecated_target_alias (record_full_target_info, "record");
2823 add_target (record_full_core_target_info, record_full_open);
2824
2825 add_prefix_cmd ("full", class_obscure, cmd_record_full_start,
2826 _("Start full execution recording."), &record_full_cmdlist,
2827 "record full ", 0, &record_cmdlist);
2828
2829 c = add_cmd ("restore", class_obscure, cmd_record_full_restore,
2830 _("Restore the execution log from a file.\n\
2831 Argument is filename. File must be created with 'record save'."),
2832 &record_full_cmdlist);
2833 set_cmd_completer (c, filename_completer);
2834
2835 /* Deprecate the old version without "full" prefix. */
2836 c = add_alias_cmd ("restore", "full restore", class_obscure, 1,
2837 &record_cmdlist);
2838 set_cmd_completer (c, filename_completer);
2839 deprecate_cmd (c, "record full restore");
2840
2841 add_prefix_cmd ("full", class_support, set_record_full_command,
2842 _("Set record options"), &set_record_full_cmdlist,
2843 "set record full ", 0, &set_record_cmdlist);
2844
2845 add_prefix_cmd ("full", class_support, show_record_full_command,
2846 _("Show record options"), &show_record_full_cmdlist,
2847 "show record full ", 0, &show_record_cmdlist);
2848
2849 /* Record instructions number limit command. */
2850 add_setshow_boolean_cmd ("stop-at-limit", no_class,
2851 &record_full_stop_at_limit, _("\
2852 Set whether record/replay stops when record/replay buffer becomes full."), _("\
2853 Show whether record/replay stops when record/replay buffer becomes full."),
2854 _("Default is ON.\n\
2855 When ON, if the record/replay buffer becomes full, ask user what to do.\n\
2856 When OFF, if the record/replay buffer becomes full,\n\
2857 delete the oldest recorded instruction to make room for each new one."),
2858 NULL, NULL,
2859 &set_record_full_cmdlist, &show_record_full_cmdlist);
2860
2861 c = add_alias_cmd ("stop-at-limit", "full stop-at-limit", no_class, 1,
2862 &set_record_cmdlist);
2863 deprecate_cmd (c, "set record full stop-at-limit");
2864
2865 c = add_alias_cmd ("stop-at-limit", "full stop-at-limit", no_class, 1,
2866 &show_record_cmdlist);
2867 deprecate_cmd (c, "show record full stop-at-limit");
2868
2869 add_setshow_uinteger_cmd ("insn-number-max", no_class,
2870 &record_full_insn_max_num,
2871 _("Set record/replay buffer limit."),
2872 _("Show record/replay buffer limit."), _("\
2873 Set the maximum number of instructions to be stored in the\n\
2874 record/replay buffer. A value of either \"unlimited\" or zero means no\n\
2875 limit. Default is 200000."),
2876 set_record_full_insn_max_num,
2877 NULL, &set_record_full_cmdlist,
2878 &show_record_full_cmdlist);
2879
2880 c = add_alias_cmd ("insn-number-max", "full insn-number-max", no_class, 1,
2881 &set_record_cmdlist);
2882 deprecate_cmd (c, "set record full insn-number-max");
2883
2884 c = add_alias_cmd ("insn-number-max", "full insn-number-max", no_class, 1,
2885 &show_record_cmdlist);
2886 deprecate_cmd (c, "show record full insn-number-max");
2887
2888 add_setshow_boolean_cmd ("memory-query", no_class,
2889 &record_full_memory_query, _("\
2890 Set whether query if PREC cannot record memory change of next instruction."),
2891 _("\
2892 Show whether query if PREC cannot record memory change of next instruction."),
2893 _("\
2894 Default is OFF.\n\
2895 When ON, query if PREC cannot record memory change of next instruction."),
2896 NULL, NULL,
2897 &set_record_full_cmdlist,
2898 &show_record_full_cmdlist);
2899
2900 c = add_alias_cmd ("memory-query", "full memory-query", no_class, 1,
2901 &set_record_cmdlist);
2902 deprecate_cmd (c, "set record full memory-query");
2903
2904 c = add_alias_cmd ("memory-query", "full memory-query", no_class, 1,
2905 &show_record_cmdlist);
2906 deprecate_cmd (c, "show record full memory-query");
2907 }
This page took 0.090309 seconds and 4 git commands to generate.