]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/record-full.c
Remove a VEC from record-full.c
[thirdparty/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 (entry->u.reg.num, record_full_get_loc (entry));
838 memcpy (record_full_get_loc (entry), reg.data (), entry->u.reg.len);
839 }
840 break;
841
842 case record_full_mem: /* mem */
843 {
844 /* Nothing to do if the entry is flagged not_accessible. */
845 if (!entry->u.mem.mem_entry_not_accessible)
846 {
847 gdb::byte_vector mem (entry->u.mem.len);
848
849 if (record_debug > 1)
850 fprintf_unfiltered (gdb_stdlog,
851 "Process record: record_full_mem %s to "
852 "inferior addr = %s len = %d.\n",
853 host_address_to_string (entry),
854 paddress (gdbarch, entry->u.mem.addr),
855 entry->u.mem.len);
856
857 if (record_read_memory (gdbarch,
858 entry->u.mem.addr, mem.data (),
859 entry->u.mem.len))
860 entry->u.mem.mem_entry_not_accessible = 1;
861 else
862 {
863 if (target_write_memory (entry->u.mem.addr,
864 record_full_get_loc (entry),
865 entry->u.mem.len))
866 {
867 entry->u.mem.mem_entry_not_accessible = 1;
868 if (record_debug)
869 warning (_("Process record: error writing memory at "
870 "addr = %s len = %d."),
871 paddress (gdbarch, entry->u.mem.addr),
872 entry->u.mem.len);
873 }
874 else
875 {
876 memcpy (record_full_get_loc (entry), mem.data (),
877 entry->u.mem.len);
878
879 /* We've changed memory --- check if a hardware
880 watchpoint should trap. Note that this
881 presently assumes the target beneath supports
882 continuable watchpoints. On non-continuable
883 watchpoints target, we'll want to check this
884 _before_ actually doing the memory change, and
885 not doing the change at all if the watchpoint
886 traps. */
887 if (hardware_watchpoint_inserted_in_range
888 (regcache->aspace (),
889 entry->u.mem.addr, entry->u.mem.len))
890 record_full_stop_reason = TARGET_STOPPED_BY_WATCHPOINT;
891 }
892 }
893 }
894 }
895 break;
896 }
897 }
898
899 static void record_full_restore (void);
900
901 /* Asynchronous signal handle registered as event loop source for when
902 we have pending events ready to be passed to the core. */
903
904 static struct async_event_handler *record_full_async_inferior_event_token;
905
906 static void
907 record_full_async_inferior_event_handler (gdb_client_data data)
908 {
909 inferior_event_handler (INF_REG_EVENT, NULL);
910 }
911
912 /* Open the process record target for 'core' files. */
913
914 static void
915 record_full_core_open_1 (const char *name, int from_tty)
916 {
917 struct regcache *regcache = get_current_regcache ();
918 int regnum = gdbarch_num_regs (regcache->arch ());
919 int i;
920
921 /* Get record_full_core_regbuf. */
922 target_fetch_registers (regcache, -1);
923 record_full_core_regbuf = new detached_regcache (regcache->arch (), false);
924
925 for (i = 0; i < regnum; i ++)
926 record_full_core_regbuf->raw_supply (i, *regcache);
927
928 /* Get record_full_core_start and record_full_core_end. */
929 if (build_section_table (core_bfd, &record_full_core_start,
930 &record_full_core_end))
931 {
932 delete record_full_core_regbuf;
933 record_full_core_regbuf = NULL;
934 error (_("\"%s\": Can't find sections: %s"),
935 bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ()));
936 }
937
938 push_target (&record_full_core_ops);
939 record_full_restore ();
940 }
941
942 /* Open the process record target for 'live' processes. */
943
944 static void
945 record_full_open_1 (const char *name, int from_tty)
946 {
947 if (record_debug)
948 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_open_1\n");
949
950 /* check exec */
951 if (!target_has_execution)
952 error (_("Process record: the program is not being run."));
953 if (non_stop)
954 error (_("Process record target can't debug inferior in non-stop mode "
955 "(non-stop)."));
956
957 if (!gdbarch_process_record_p (target_gdbarch ()))
958 error (_("Process record: the current architecture doesn't support "
959 "record function."));
960
961 push_target (&record_full_ops);
962 }
963
964 static void record_full_init_record_breakpoints (void);
965
966 /* Open the process record target. */
967
968 static void
969 record_full_open (const char *name, int from_tty)
970 {
971 if (record_debug)
972 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_open\n");
973
974 record_preopen ();
975
976 /* Reset */
977 record_full_insn_num = 0;
978 record_full_insn_count = 0;
979 record_full_list = &record_full_first;
980 record_full_list->next = NULL;
981
982 if (core_bfd)
983 record_full_core_open_1 (name, from_tty);
984 else
985 record_full_open_1 (name, from_tty);
986
987 /* Register extra event sources in the event loop. */
988 record_full_async_inferior_event_token
989 = create_async_event_handler (record_full_async_inferior_event_handler,
990 NULL);
991
992 record_full_init_record_breakpoints ();
993
994 gdb::observers::record_changed.notify (current_inferior (), 1, "full", NULL);
995 }
996
997 /* "close" target method. Close the process record target. */
998
999 void
1000 record_full_base_target::close ()
1001 {
1002 struct record_full_core_buf_entry *entry;
1003
1004 if (record_debug)
1005 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_close\n");
1006
1007 record_full_list_release (record_full_list);
1008
1009 /* Release record_full_core_regbuf. */
1010 if (record_full_core_regbuf)
1011 {
1012 delete record_full_core_regbuf;
1013 record_full_core_regbuf = NULL;
1014 }
1015
1016 /* Release record_full_core_buf_list. */
1017 if (record_full_core_buf_list)
1018 {
1019 for (entry = record_full_core_buf_list->prev; entry;
1020 entry = entry->prev)
1021 {
1022 xfree (record_full_core_buf_list);
1023 record_full_core_buf_list = entry;
1024 }
1025 record_full_core_buf_list = NULL;
1026 }
1027
1028 if (record_full_async_inferior_event_token)
1029 delete_async_event_handler (&record_full_async_inferior_event_token);
1030 }
1031
1032 /* "async" target method. */
1033
1034 void
1035 record_full_base_target::async (int enable)
1036 {
1037 if (enable)
1038 mark_async_event_handler (record_full_async_inferior_event_token);
1039 else
1040 clear_async_event_handler (record_full_async_inferior_event_token);
1041
1042 beneath ()->async (enable);
1043 }
1044
1045 static int record_full_resume_step = 0;
1046
1047 /* True if we've been resumed, and so each record_full_wait call should
1048 advance execution. If this is false, record_full_wait will return a
1049 TARGET_WAITKIND_IGNORE. */
1050 static int record_full_resumed = 0;
1051
1052 /* The execution direction of the last resume we got. This is
1053 necessary for async mode. Vis (order is not strictly accurate):
1054
1055 1. user has the global execution direction set to forward
1056 2. user does a reverse-step command
1057 3. record_full_resume is called with global execution direction
1058 temporarily switched to reverse
1059 4. GDB's execution direction is reverted back to forward
1060 5. target record notifies event loop there's an event to handle
1061 6. infrun asks the target which direction was it going, and switches
1062 the global execution direction accordingly (to reverse)
1063 7. infrun polls an event out of the record target, and handles it
1064 8. GDB goes back to the event loop, and goto #4.
1065 */
1066 static enum exec_direction_kind record_full_execution_dir = EXEC_FORWARD;
1067
1068 /* "resume" target method. Resume the process record target. */
1069
1070 void
1071 record_full_target::resume (ptid_t ptid, int step, enum gdb_signal signal)
1072 {
1073 record_full_resume_step = step;
1074 record_full_resumed = 1;
1075 record_full_execution_dir = ::execution_direction;
1076
1077 if (!RECORD_FULL_IS_REPLAY)
1078 {
1079 struct gdbarch *gdbarch = target_thread_architecture (ptid);
1080
1081 record_full_message (get_current_regcache (), signal);
1082
1083 if (!step)
1084 {
1085 /* This is not hard single step. */
1086 if (!gdbarch_software_single_step_p (gdbarch))
1087 {
1088 /* This is a normal continue. */
1089 step = 1;
1090 }
1091 else
1092 {
1093 /* This arch supports soft single step. */
1094 if (thread_has_single_step_breakpoints_set (inferior_thread ()))
1095 {
1096 /* This is a soft single step. */
1097 record_full_resume_step = 1;
1098 }
1099 else
1100 step = !insert_single_step_breakpoints (gdbarch);
1101 }
1102 }
1103
1104 /* Make sure the target beneath reports all signals. */
1105 target_pass_signals (0, NULL);
1106
1107 this->beneath ()->resume (ptid, step, signal);
1108 }
1109
1110 /* We are about to start executing the inferior (or simulate it),
1111 let's register it with the event loop. */
1112 if (target_can_async_p ())
1113 target_async (1);
1114 }
1115
1116 /* "commit_resume" method for process record target. */
1117
1118 void
1119 record_full_target::commit_resume ()
1120 {
1121 if (!RECORD_FULL_IS_REPLAY)
1122 beneath ()->commit_resume ();
1123 }
1124
1125 static int record_full_get_sig = 0;
1126
1127 /* SIGINT signal handler, registered by "wait" method. */
1128
1129 static void
1130 record_full_sig_handler (int signo)
1131 {
1132 if (record_debug)
1133 fprintf_unfiltered (gdb_stdlog, "Process record: get a signal\n");
1134
1135 /* It will break the running inferior in replay mode. */
1136 record_full_resume_step = 1;
1137
1138 /* It will let record_full_wait set inferior status to get the signal
1139 SIGINT. */
1140 record_full_get_sig = 1;
1141 }
1142
1143 static void
1144 record_full_wait_cleanups (void *ignore)
1145 {
1146 if (execution_direction == EXEC_REVERSE)
1147 {
1148 if (record_full_list->next)
1149 record_full_list = record_full_list->next;
1150 }
1151 else
1152 record_full_list = record_full_list->prev;
1153 }
1154
1155 /* "wait" target method for process record target.
1156
1157 In record mode, the target is always run in singlestep mode
1158 (even when gdb says to continue). The wait method intercepts
1159 the stop events and determines which ones are to be passed on to
1160 gdb. Most stop events are just singlestep events that gdb is not
1161 to know about, so the wait method just records them and keeps
1162 singlestepping.
1163
1164 In replay mode, this function emulates the recorded execution log,
1165 one instruction at a time (forward or backward), and determines
1166 where to stop. */
1167
1168 static ptid_t
1169 record_full_wait_1 (struct target_ops *ops,
1170 ptid_t ptid, struct target_waitstatus *status,
1171 int options)
1172 {
1173 scoped_restore restore_operation_disable
1174 = record_full_gdb_operation_disable_set ();
1175
1176 if (record_debug)
1177 fprintf_unfiltered (gdb_stdlog,
1178 "Process record: record_full_wait "
1179 "record_full_resume_step = %d, "
1180 "record_full_resumed = %d, direction=%s\n",
1181 record_full_resume_step, record_full_resumed,
1182 record_full_execution_dir == EXEC_FORWARD
1183 ? "forward" : "reverse");
1184
1185 if (!record_full_resumed)
1186 {
1187 gdb_assert ((options & TARGET_WNOHANG) != 0);
1188
1189 /* No interesting event. */
1190 status->kind = TARGET_WAITKIND_IGNORE;
1191 return minus_one_ptid;
1192 }
1193
1194 record_full_get_sig = 0;
1195 signal (SIGINT, record_full_sig_handler);
1196
1197 record_full_stop_reason = TARGET_STOPPED_BY_NO_REASON;
1198
1199 if (!RECORD_FULL_IS_REPLAY && ops != &record_full_core_ops)
1200 {
1201 if (record_full_resume_step)
1202 {
1203 /* This is a single step. */
1204 return ops->beneath ()->wait (ptid, status, options);
1205 }
1206 else
1207 {
1208 /* This is not a single step. */
1209 ptid_t ret;
1210 CORE_ADDR tmp_pc;
1211 struct gdbarch *gdbarch = target_thread_architecture (inferior_ptid);
1212
1213 while (1)
1214 {
1215 struct thread_info *tp;
1216
1217 ret = ops->beneath ()->wait (ptid, status, options);
1218 if (status->kind == TARGET_WAITKIND_IGNORE)
1219 {
1220 if (record_debug)
1221 fprintf_unfiltered (gdb_stdlog,
1222 "Process record: record_full_wait "
1223 "target beneath not done yet\n");
1224 return ret;
1225 }
1226
1227 ALL_NON_EXITED_THREADS (tp)
1228 delete_single_step_breakpoints (tp);
1229
1230 if (record_full_resume_step)
1231 return ret;
1232
1233 /* Is this a SIGTRAP? */
1234 if (status->kind == TARGET_WAITKIND_STOPPED
1235 && status->value.sig == GDB_SIGNAL_TRAP)
1236 {
1237 struct regcache *regcache;
1238 enum target_stop_reason *stop_reason_p
1239 = &record_full_stop_reason;
1240
1241 /* Yes -- this is likely our single-step finishing,
1242 but check if there's any reason the core would be
1243 interested in the event. */
1244
1245 registers_changed ();
1246 regcache = get_current_regcache ();
1247 tmp_pc = regcache_read_pc (regcache);
1248 const struct address_space *aspace = regcache->aspace ();
1249
1250 if (target_stopped_by_watchpoint ())
1251 {
1252 /* Always interested in watchpoints. */
1253 }
1254 else if (record_check_stopped_by_breakpoint (aspace, tmp_pc,
1255 stop_reason_p))
1256 {
1257 /* There is a breakpoint here. Let the core
1258 handle it. */
1259 }
1260 else
1261 {
1262 /* This is a single-step trap. Record the
1263 insn and issue another step.
1264 FIXME: this part can be a random SIGTRAP too.
1265 But GDB cannot handle it. */
1266 int step = 1;
1267
1268 if (!record_full_message_wrapper_safe (regcache,
1269 GDB_SIGNAL_0))
1270 {
1271 status->kind = TARGET_WAITKIND_STOPPED;
1272 status->value.sig = GDB_SIGNAL_0;
1273 break;
1274 }
1275
1276 if (gdbarch_software_single_step_p (gdbarch))
1277 {
1278 /* Try to insert the software single step breakpoint.
1279 If insert success, set step to 0. */
1280 set_executing (inferior_ptid, 0);
1281 reinit_frame_cache ();
1282
1283 step = !insert_single_step_breakpoints (gdbarch);
1284
1285 set_executing (inferior_ptid, 1);
1286 }
1287
1288 if (record_debug)
1289 fprintf_unfiltered (gdb_stdlog,
1290 "Process record: record_full_wait "
1291 "issuing one more step in the "
1292 "target beneath\n");
1293 ops->beneath ()->resume (ptid, step, GDB_SIGNAL_0);
1294 ops->beneath ()->commit_resume ();
1295 continue;
1296 }
1297 }
1298
1299 /* The inferior is broken by a breakpoint or a signal. */
1300 break;
1301 }
1302
1303 return ret;
1304 }
1305 }
1306 else
1307 {
1308 struct regcache *regcache = get_current_regcache ();
1309 struct gdbarch *gdbarch = regcache->arch ();
1310 const struct address_space *aspace = regcache->aspace ();
1311 int continue_flag = 1;
1312 int first_record_full_end = 1;
1313 struct cleanup *old_cleanups
1314 = make_cleanup (record_full_wait_cleanups, 0);
1315 CORE_ADDR tmp_pc;
1316
1317 record_full_stop_reason = TARGET_STOPPED_BY_NO_REASON;
1318 status->kind = TARGET_WAITKIND_STOPPED;
1319
1320 /* Check breakpoint when forward execute. */
1321 if (execution_direction == EXEC_FORWARD)
1322 {
1323 tmp_pc = regcache_read_pc (regcache);
1324 if (record_check_stopped_by_breakpoint (aspace, tmp_pc,
1325 &record_full_stop_reason))
1326 {
1327 if (record_debug)
1328 fprintf_unfiltered (gdb_stdlog,
1329 "Process record: break at %s.\n",
1330 paddress (gdbarch, tmp_pc));
1331 goto replay_out;
1332 }
1333 }
1334
1335 /* If GDB is in terminal_inferior mode, it will not get the signal.
1336 And in GDB replay mode, GDB doesn't need to be in terminal_inferior
1337 mode, because inferior will not executed.
1338 Then set it to terminal_ours to make GDB get the signal. */
1339 target_terminal::ours ();
1340
1341 /* In EXEC_FORWARD mode, record_full_list points to the tail of prev
1342 instruction. */
1343 if (execution_direction == EXEC_FORWARD && record_full_list->next)
1344 record_full_list = record_full_list->next;
1345
1346 /* Loop over the record_full_list, looking for the next place to
1347 stop. */
1348 do
1349 {
1350 /* Check for beginning and end of log. */
1351 if (execution_direction == EXEC_REVERSE
1352 && record_full_list == &record_full_first)
1353 {
1354 /* Hit beginning of record log in reverse. */
1355 status->kind = TARGET_WAITKIND_NO_HISTORY;
1356 break;
1357 }
1358 if (execution_direction != EXEC_REVERSE && !record_full_list->next)
1359 {
1360 /* Hit end of record log going forward. */
1361 status->kind = TARGET_WAITKIND_NO_HISTORY;
1362 break;
1363 }
1364
1365 record_full_exec_insn (regcache, gdbarch, record_full_list);
1366
1367 if (record_full_list->type == record_full_end)
1368 {
1369 if (record_debug > 1)
1370 fprintf_unfiltered (gdb_stdlog,
1371 "Process record: record_full_end %s to "
1372 "inferior.\n",
1373 host_address_to_string (record_full_list));
1374
1375 if (first_record_full_end && execution_direction == EXEC_REVERSE)
1376 {
1377 /* When reverse excute, the first record_full_end is the
1378 part of current instruction. */
1379 first_record_full_end = 0;
1380 }
1381 else
1382 {
1383 /* In EXEC_REVERSE mode, this is the record_full_end of prev
1384 instruction.
1385 In EXEC_FORWARD mode, this is the record_full_end of
1386 current instruction. */
1387 /* step */
1388 if (record_full_resume_step)
1389 {
1390 if (record_debug > 1)
1391 fprintf_unfiltered (gdb_stdlog,
1392 "Process record: step.\n");
1393 continue_flag = 0;
1394 }
1395
1396 /* check breakpoint */
1397 tmp_pc = regcache_read_pc (regcache);
1398 if (record_check_stopped_by_breakpoint (aspace, tmp_pc,
1399 &record_full_stop_reason))
1400 {
1401 if (record_debug)
1402 fprintf_unfiltered (gdb_stdlog,
1403 "Process record: break "
1404 "at %s.\n",
1405 paddress (gdbarch, tmp_pc));
1406
1407 continue_flag = 0;
1408 }
1409
1410 if (record_full_stop_reason == TARGET_STOPPED_BY_WATCHPOINT)
1411 {
1412 if (record_debug)
1413 fprintf_unfiltered (gdb_stdlog,
1414 "Process record: hit hw "
1415 "watchpoint.\n");
1416 continue_flag = 0;
1417 }
1418 /* Check target signal */
1419 if (record_full_list->u.end.sigval != GDB_SIGNAL_0)
1420 /* FIXME: better way to check */
1421 continue_flag = 0;
1422 }
1423 }
1424
1425 if (continue_flag)
1426 {
1427 if (execution_direction == EXEC_REVERSE)
1428 {
1429 if (record_full_list->prev)
1430 record_full_list = record_full_list->prev;
1431 }
1432 else
1433 {
1434 if (record_full_list->next)
1435 record_full_list = record_full_list->next;
1436 }
1437 }
1438 }
1439 while (continue_flag);
1440
1441 replay_out:
1442 if (record_full_get_sig)
1443 status->value.sig = GDB_SIGNAL_INT;
1444 else if (record_full_list->u.end.sigval != GDB_SIGNAL_0)
1445 /* FIXME: better way to check */
1446 status->value.sig = record_full_list->u.end.sigval;
1447 else
1448 status->value.sig = GDB_SIGNAL_TRAP;
1449
1450 discard_cleanups (old_cleanups);
1451 }
1452
1453 signal (SIGINT, handle_sigint);
1454
1455 return inferior_ptid;
1456 }
1457
1458 ptid_t
1459 record_full_base_target::wait (ptid_t ptid, struct target_waitstatus *status,
1460 int options)
1461 {
1462 ptid_t return_ptid;
1463
1464 return_ptid = record_full_wait_1 (this, ptid, status, options);
1465 if (status->kind != TARGET_WAITKIND_IGNORE)
1466 {
1467 /* We're reporting a stop. Make sure any spurious
1468 target_wait(WNOHANG) doesn't advance the target until the
1469 core wants us resumed again. */
1470 record_full_resumed = 0;
1471 }
1472 return return_ptid;
1473 }
1474
1475 bool
1476 record_full_base_target::stopped_by_watchpoint ()
1477 {
1478 if (RECORD_FULL_IS_REPLAY)
1479 return record_full_stop_reason == TARGET_STOPPED_BY_WATCHPOINT;
1480 else
1481 return beneath ()->stopped_by_watchpoint ();
1482 }
1483
1484 bool
1485 record_full_base_target::stopped_data_address (CORE_ADDR *addr_p)
1486 {
1487 if (RECORD_FULL_IS_REPLAY)
1488 return false;
1489 else
1490 return this->beneath ()->stopped_data_address (addr_p);
1491 }
1492
1493 /* The stopped_by_sw_breakpoint method of target record-full. */
1494
1495 bool
1496 record_full_base_target::stopped_by_sw_breakpoint ()
1497 {
1498 return record_full_stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT;
1499 }
1500
1501 /* The supports_stopped_by_sw_breakpoint method of target
1502 record-full. */
1503
1504 bool
1505 record_full_base_target::supports_stopped_by_sw_breakpoint ()
1506 {
1507 return true;
1508 }
1509
1510 /* The stopped_by_hw_breakpoint method of target record-full. */
1511
1512 bool
1513 record_full_base_target::stopped_by_hw_breakpoint ()
1514 {
1515 return record_full_stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT;
1516 }
1517
1518 /* The supports_stopped_by_sw_breakpoint method of target
1519 record-full. */
1520
1521 bool
1522 record_full_base_target::supports_stopped_by_hw_breakpoint ()
1523 {
1524 return true;
1525 }
1526
1527 /* Record registers change (by user or by GDB) to list as an instruction. */
1528
1529 static void
1530 record_full_registers_change (struct regcache *regcache, int regnum)
1531 {
1532 /* Check record_full_insn_num. */
1533 record_full_check_insn_num ();
1534
1535 record_full_arch_list_head = NULL;
1536 record_full_arch_list_tail = NULL;
1537
1538 if (regnum < 0)
1539 {
1540 int i;
1541
1542 for (i = 0; i < gdbarch_num_regs (regcache->arch ()); i++)
1543 {
1544 if (record_full_arch_list_add_reg (regcache, i))
1545 {
1546 record_full_list_release (record_full_arch_list_tail);
1547 error (_("Process record: failed to record execution log."));
1548 }
1549 }
1550 }
1551 else
1552 {
1553 if (record_full_arch_list_add_reg (regcache, regnum))
1554 {
1555 record_full_list_release (record_full_arch_list_tail);
1556 error (_("Process record: failed to record execution log."));
1557 }
1558 }
1559 if (record_full_arch_list_add_end ())
1560 {
1561 record_full_list_release (record_full_arch_list_tail);
1562 error (_("Process record: failed to record execution log."));
1563 }
1564 record_full_list->next = record_full_arch_list_head;
1565 record_full_arch_list_head->prev = record_full_list;
1566 record_full_list = record_full_arch_list_tail;
1567
1568 if (record_full_insn_num == record_full_insn_max_num)
1569 record_full_list_release_first ();
1570 else
1571 record_full_insn_num++;
1572 }
1573
1574 /* "store_registers" method for process record target. */
1575
1576 void
1577 record_full_target::store_registers (struct regcache *regcache, int regno)
1578 {
1579 if (!record_full_gdb_operation_disable)
1580 {
1581 if (RECORD_FULL_IS_REPLAY)
1582 {
1583 int n;
1584
1585 /* Let user choose if he wants to write register or not. */
1586 if (regno < 0)
1587 n =
1588 query (_("Because GDB is in replay mode, changing the "
1589 "value of a register will make the execution "
1590 "log unusable from this point onward. "
1591 "Change all registers?"));
1592 else
1593 n =
1594 query (_("Because GDB is in replay mode, changing the value "
1595 "of a register will make the execution log unusable "
1596 "from this point onward. Change register %s?"),
1597 gdbarch_register_name (regcache->arch (),
1598 regno));
1599
1600 if (!n)
1601 {
1602 /* Invalidate the value of regcache that was set in function
1603 "regcache_raw_write". */
1604 if (regno < 0)
1605 {
1606 int i;
1607
1608 for (i = 0;
1609 i < gdbarch_num_regs (regcache->arch ());
1610 i++)
1611 regcache->invalidate (i);
1612 }
1613 else
1614 regcache->invalidate (regno);
1615
1616 error (_("Process record canceled the operation."));
1617 }
1618
1619 /* Destroy the record from here forward. */
1620 record_full_list_release_following (record_full_list);
1621 }
1622
1623 record_full_registers_change (regcache, regno);
1624 }
1625 this->beneath ()->store_registers (regcache, regno);
1626 }
1627
1628 /* "xfer_partial" method. Behavior is conditional on
1629 RECORD_FULL_IS_REPLAY.
1630 In replay mode, we cannot write memory unles we are willing to
1631 invalidate the record/replay log from this point forward. */
1632
1633 enum target_xfer_status
1634 record_full_target::xfer_partial (enum target_object object,
1635 const char *annex, gdb_byte *readbuf,
1636 const gdb_byte *writebuf, ULONGEST offset,
1637 ULONGEST len, ULONGEST *xfered_len)
1638 {
1639 if (!record_full_gdb_operation_disable
1640 && (object == TARGET_OBJECT_MEMORY
1641 || object == TARGET_OBJECT_RAW_MEMORY) && writebuf)
1642 {
1643 if (RECORD_FULL_IS_REPLAY)
1644 {
1645 /* Let user choose if he wants to write memory or not. */
1646 if (!query (_("Because GDB is in replay mode, writing to memory "
1647 "will make the execution log unusable from this "
1648 "point onward. Write memory at address %s?"),
1649 paddress (target_gdbarch (), offset)))
1650 error (_("Process record canceled the operation."));
1651
1652 /* Destroy the record from here forward. */
1653 record_full_list_release_following (record_full_list);
1654 }
1655
1656 /* Check record_full_insn_num */
1657 record_full_check_insn_num ();
1658
1659 /* Record registers change to list as an instruction. */
1660 record_full_arch_list_head = NULL;
1661 record_full_arch_list_tail = NULL;
1662 if (record_full_arch_list_add_mem (offset, len))
1663 {
1664 record_full_list_release (record_full_arch_list_tail);
1665 if (record_debug)
1666 fprintf_unfiltered (gdb_stdlog,
1667 "Process record: failed to record "
1668 "execution log.");
1669 return TARGET_XFER_E_IO;
1670 }
1671 if (record_full_arch_list_add_end ())
1672 {
1673 record_full_list_release (record_full_arch_list_tail);
1674 if (record_debug)
1675 fprintf_unfiltered (gdb_stdlog,
1676 "Process record: failed to record "
1677 "execution log.");
1678 return TARGET_XFER_E_IO;
1679 }
1680 record_full_list->next = record_full_arch_list_head;
1681 record_full_arch_list_head->prev = record_full_list;
1682 record_full_list = record_full_arch_list_tail;
1683
1684 if (record_full_insn_num == record_full_insn_max_num)
1685 record_full_list_release_first ();
1686 else
1687 record_full_insn_num++;
1688 }
1689
1690 return this->beneath ()->xfer_partial (object, annex, readbuf, writebuf,
1691 offset, len, xfered_len);
1692 }
1693
1694 /* This structure represents a breakpoint inserted while the record
1695 target is active. We use this to know when to install/remove
1696 breakpoints in/from the target beneath. For example, a breakpoint
1697 may be inserted while recording, but removed when not replaying nor
1698 recording. In that case, the breakpoint had not been inserted on
1699 the target beneath, so we should not try to remove it there. */
1700
1701 struct record_full_breakpoint
1702 {
1703 record_full_breakpoint (struct address_space *address_space_,
1704 CORE_ADDR addr_,
1705 bool in_target_beneath_)
1706 : address_space (address_space_),
1707 addr (addr_),
1708 in_target_beneath (in_target_beneath_)
1709 {
1710 }
1711
1712 /* The address and address space the breakpoint was set at. */
1713 struct address_space *address_space;
1714 CORE_ADDR addr;
1715
1716 /* True when the breakpoint has been also installed in the target
1717 beneath. This will be false for breakpoints set during replay or
1718 when recording. */
1719 bool in_target_beneath;
1720 };
1721
1722 /* The list of breakpoints inserted while the record target is
1723 active. */
1724 static std::vector<record_full_breakpoint> record_full_breakpoints;
1725
1726 static void
1727 record_full_sync_record_breakpoints (struct bp_location *loc, void *data)
1728 {
1729 if (loc->loc_type != bp_loc_software_breakpoint)
1730 return;
1731
1732 if (loc->inserted)
1733 {
1734 record_full_breakpoints.emplace_back
1735 (loc->target_info.placed_address_space,
1736 loc->target_info.placed_address,
1737 1);
1738 }
1739 }
1740
1741 /* Sync existing breakpoints to record_full_breakpoints. */
1742
1743 static void
1744 record_full_init_record_breakpoints (void)
1745 {
1746 record_full_breakpoints.clear ();
1747
1748 iterate_over_bp_locations (record_full_sync_record_breakpoints);
1749 }
1750
1751 /* Behavior is conditional on RECORD_FULL_IS_REPLAY. We will not actually
1752 insert or remove breakpoints in the real target when replaying, nor
1753 when recording. */
1754
1755 int
1756 record_full_target::insert_breakpoint (struct gdbarch *gdbarch,
1757 struct bp_target_info *bp_tgt)
1758 {
1759 bool in_target_beneath = false;
1760
1761 if (!RECORD_FULL_IS_REPLAY)
1762 {
1763 /* When recording, we currently always single-step, so we don't
1764 really need to install regular breakpoints in the inferior.
1765 However, we do have to insert software single-step
1766 breakpoints, in case the target can't hardware step. To keep
1767 things simple, we always insert. */
1768
1769 scoped_restore restore_operation_disable
1770 = record_full_gdb_operation_disable_set ();
1771
1772 int ret = this->beneath ()->insert_breakpoint (gdbarch, bp_tgt);
1773 if (ret != 0)
1774 return ret;
1775
1776 in_target_beneath = true;
1777 }
1778
1779 /* Use the existing entries if found in order to avoid duplication
1780 in record_full_breakpoints. */
1781
1782 for (struct record_full_breakpoint &bp : record_full_breakpoints)
1783 {
1784 if (bp.addr == bp_tgt->placed_address
1785 && bp.address_space == bp_tgt->placed_address_space)
1786 {
1787 gdb_assert (bp.in_target_beneath == in_target_beneath);
1788 return 0;
1789 }
1790 }
1791
1792 record_full_breakpoints.emplace_back (bp_tgt->placed_address_space,
1793 bp_tgt->placed_address,
1794 in_target_beneath);
1795 return 0;
1796 }
1797
1798 /* "remove_breakpoint" method for process record target. */
1799
1800 int
1801 record_full_target::remove_breakpoint (struct gdbarch *gdbarch,
1802 struct bp_target_info *bp_tgt,
1803 enum remove_bp_reason reason)
1804 {
1805 for (auto iter = record_full_breakpoints.begin ();
1806 iter != record_full_breakpoints.end ();
1807 ++iter)
1808 {
1809 struct record_full_breakpoint &bp = *iter;
1810
1811 if (bp.addr == bp_tgt->placed_address
1812 && bp.address_space == bp_tgt->placed_address_space)
1813 {
1814 if (bp.in_target_beneath)
1815 {
1816 scoped_restore restore_operation_disable
1817 = record_full_gdb_operation_disable_set ();
1818
1819 int ret = this->beneath ()->remove_breakpoint (gdbarch, bp_tgt,
1820 reason);
1821 if (ret != 0)
1822 return ret;
1823 }
1824
1825 if (reason == REMOVE_BREAKPOINT)
1826 unordered_remove (record_full_breakpoints, iter);
1827 return 0;
1828 }
1829 }
1830
1831 gdb_assert_not_reached ("removing unknown breakpoint");
1832 }
1833
1834 /* "can_execute_reverse" method for process record target. */
1835
1836 bool
1837 record_full_base_target::can_execute_reverse ()
1838 {
1839 return true;
1840 }
1841
1842 /* "get_bookmark" method for process record and prec over core. */
1843
1844 gdb_byte *
1845 record_full_base_target::get_bookmark (const char *args, int from_tty)
1846 {
1847 char *ret = NULL;
1848
1849 /* Return stringified form of instruction count. */
1850 if (record_full_list && record_full_list->type == record_full_end)
1851 ret = xstrdup (pulongest (record_full_list->u.end.insn_num));
1852
1853 if (record_debug)
1854 {
1855 if (ret)
1856 fprintf_unfiltered (gdb_stdlog,
1857 "record_full_get_bookmark returns %s\n", ret);
1858 else
1859 fprintf_unfiltered (gdb_stdlog,
1860 "record_full_get_bookmark returns NULL\n");
1861 }
1862 return (gdb_byte *) ret;
1863 }
1864
1865 /* "goto_bookmark" method for process record and prec over core. */
1866
1867 void
1868 record_full_base_target::goto_bookmark (const gdb_byte *raw_bookmark,
1869 int from_tty)
1870 {
1871 const char *bookmark = (const char *) raw_bookmark;
1872
1873 if (record_debug)
1874 fprintf_unfiltered (gdb_stdlog,
1875 "record_full_goto_bookmark receives %s\n", bookmark);
1876
1877 std::string name_holder;
1878 if (bookmark[0] == '\'' || bookmark[0] == '\"')
1879 {
1880 if (bookmark[strlen (bookmark) - 1] != bookmark[0])
1881 error (_("Unbalanced quotes: %s"), bookmark);
1882
1883 name_holder = std::string (bookmark + 1, strlen (bookmark) - 2);
1884 bookmark = name_holder.c_str ();
1885 }
1886
1887 record_goto (bookmark);
1888 }
1889
1890 enum exec_direction_kind
1891 record_full_base_target::execution_direction ()
1892 {
1893 return record_full_execution_dir;
1894 }
1895
1896 /* The record_method method of target record-full. */
1897
1898 enum record_method
1899 record_full_base_target::record_method (ptid_t ptid)
1900 {
1901 return RECORD_METHOD_FULL;
1902 }
1903
1904 void
1905 record_full_base_target::info_record ()
1906 {
1907 struct record_full_entry *p;
1908
1909 if (RECORD_FULL_IS_REPLAY)
1910 printf_filtered (_("Replay mode:\n"));
1911 else
1912 printf_filtered (_("Record mode:\n"));
1913
1914 /* Find entry for first actual instruction in the log. */
1915 for (p = record_full_first.next;
1916 p != NULL && p->type != record_full_end;
1917 p = p->next)
1918 ;
1919
1920 /* Do we have a log at all? */
1921 if (p != NULL && p->type == record_full_end)
1922 {
1923 /* Display instruction number for first instruction in the log. */
1924 printf_filtered (_("Lowest recorded instruction number is %s.\n"),
1925 pulongest (p->u.end.insn_num));
1926
1927 /* If in replay mode, display where we are in the log. */
1928 if (RECORD_FULL_IS_REPLAY)
1929 printf_filtered (_("Current instruction number is %s.\n"),
1930 pulongest (record_full_list->u.end.insn_num));
1931
1932 /* Display instruction number for last instruction in the log. */
1933 printf_filtered (_("Highest recorded instruction number is %s.\n"),
1934 pulongest (record_full_insn_count));
1935
1936 /* Display log count. */
1937 printf_filtered (_("Log contains %u instructions.\n"),
1938 record_full_insn_num);
1939 }
1940 else
1941 printf_filtered (_("No instructions have been logged.\n"));
1942
1943 /* Display max log size. */
1944 printf_filtered (_("Max logged instructions is %u.\n"),
1945 record_full_insn_max_num);
1946 }
1947
1948 bool
1949 record_full_base_target::supports_delete_record ()
1950 {
1951 return true;
1952 }
1953
1954 /* The "delete_record" target method. */
1955
1956 void
1957 record_full_base_target::delete_record ()
1958 {
1959 record_full_list_release_following (record_full_list);
1960 }
1961
1962 /* The "record_is_replaying" target method. */
1963
1964 bool
1965 record_full_base_target::record_is_replaying (ptid_t ptid)
1966 {
1967 return RECORD_FULL_IS_REPLAY;
1968 }
1969
1970 /* The "record_will_replay" target method. */
1971
1972 bool
1973 record_full_base_target::record_will_replay (ptid_t ptid, int dir)
1974 {
1975 /* We can currently only record when executing forwards. Should we be able
1976 to record when executing backwards on targets that support reverse
1977 execution, this needs to be changed. */
1978
1979 return RECORD_FULL_IS_REPLAY || dir == EXEC_REVERSE;
1980 }
1981
1982 /* Go to a specific entry. */
1983
1984 static void
1985 record_full_goto_entry (struct record_full_entry *p)
1986 {
1987 if (p == NULL)
1988 error (_("Target insn not found."));
1989 else if (p == record_full_list)
1990 error (_("Already at target insn."));
1991 else if (p->u.end.insn_num > record_full_list->u.end.insn_num)
1992 {
1993 printf_filtered (_("Go forward to insn number %s\n"),
1994 pulongest (p->u.end.insn_num));
1995 record_full_goto_insn (p, EXEC_FORWARD);
1996 }
1997 else
1998 {
1999 printf_filtered (_("Go backward to insn number %s\n"),
2000 pulongest (p->u.end.insn_num));
2001 record_full_goto_insn (p, EXEC_REVERSE);
2002 }
2003
2004 registers_changed ();
2005 reinit_frame_cache ();
2006 stop_pc = regcache_read_pc (get_current_regcache ());
2007 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
2008 }
2009
2010 /* The "goto_record_begin" target method. */
2011
2012 void
2013 record_full_base_target::goto_record_begin ()
2014 {
2015 struct record_full_entry *p = NULL;
2016
2017 for (p = &record_full_first; p != NULL; p = p->next)
2018 if (p->type == record_full_end)
2019 break;
2020
2021 record_full_goto_entry (p);
2022 }
2023
2024 /* The "goto_record_end" target method. */
2025
2026 void
2027 record_full_base_target::goto_record_end ()
2028 {
2029 struct record_full_entry *p = NULL;
2030
2031 for (p = record_full_list; p->next != NULL; p = p->next)
2032 ;
2033 for (; p!= NULL; p = p->prev)
2034 if (p->type == record_full_end)
2035 break;
2036
2037 record_full_goto_entry (p);
2038 }
2039
2040 /* The "goto_record" target method. */
2041
2042 void
2043 record_full_base_target::goto_record (ULONGEST target_insn)
2044 {
2045 struct record_full_entry *p = NULL;
2046
2047 for (p = &record_full_first; p != NULL; p = p->next)
2048 if (p->type == record_full_end && p->u.end.insn_num == target_insn)
2049 break;
2050
2051 record_full_goto_entry (p);
2052 }
2053
2054 /* The "record_stop_replaying" target method. */
2055
2056 void
2057 record_full_base_target::record_stop_replaying ()
2058 {
2059 goto_record_end ();
2060 }
2061
2062 /* "resume" method for prec over corefile. */
2063
2064 void
2065 record_full_core_target::resume (ptid_t ptid, int step,
2066 enum gdb_signal signal)
2067 {
2068 record_full_resume_step = step;
2069 record_full_resumed = 1;
2070 record_full_execution_dir = ::execution_direction;
2071
2072 /* We are about to start executing the inferior (or simulate it),
2073 let's register it with the event loop. */
2074 if (target_can_async_p ())
2075 target_async (1);
2076 }
2077
2078 /* "kill" method for prec over corefile. */
2079
2080 void
2081 record_full_core_target::kill ()
2082 {
2083 if (record_debug)
2084 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_core_kill\n");
2085
2086 unpush_target (this);
2087 }
2088
2089 /* "fetch_registers" method for prec over corefile. */
2090
2091 void
2092 record_full_core_target::fetch_registers (struct regcache *regcache,
2093 int regno)
2094 {
2095 if (regno < 0)
2096 {
2097 int num = gdbarch_num_regs (regcache->arch ());
2098 int i;
2099
2100 for (i = 0; i < num; i ++)
2101 regcache->raw_supply (i, *record_full_core_regbuf);
2102 }
2103 else
2104 regcache->raw_supply (regno, *record_full_core_regbuf);
2105 }
2106
2107 /* "prepare_to_store" method for prec over corefile. */
2108
2109 void
2110 record_full_core_target::prepare_to_store (struct regcache *regcache)
2111 {
2112 }
2113
2114 /* "store_registers" method for prec over corefile. */
2115
2116 void
2117 record_full_core_target::store_registers (struct regcache *regcache,
2118 int regno)
2119 {
2120 if (record_full_gdb_operation_disable)
2121 record_full_core_regbuf->raw_supply (regno, *regcache);
2122 else
2123 error (_("You can't do that without a process to debug."));
2124 }
2125
2126 /* "xfer_partial" method for prec over corefile. */
2127
2128 enum target_xfer_status
2129 record_full_core_target::xfer_partial (enum target_object object,
2130 const char *annex, gdb_byte *readbuf,
2131 const gdb_byte *writebuf, ULONGEST offset,
2132 ULONGEST len, ULONGEST *xfered_len)
2133 {
2134 if (object == TARGET_OBJECT_MEMORY)
2135 {
2136 if (record_full_gdb_operation_disable || !writebuf)
2137 {
2138 struct target_section *p;
2139
2140 for (p = record_full_core_start; p < record_full_core_end; p++)
2141 {
2142 if (offset >= p->addr)
2143 {
2144 struct record_full_core_buf_entry *entry;
2145 ULONGEST sec_offset;
2146
2147 if (offset >= p->endaddr)
2148 continue;
2149
2150 if (offset + len > p->endaddr)
2151 len = p->endaddr - offset;
2152
2153 sec_offset = offset - p->addr;
2154
2155 /* Read readbuf or write writebuf p, offset, len. */
2156 /* Check flags. */
2157 if (p->the_bfd_section->flags & SEC_CONSTRUCTOR
2158 || (p->the_bfd_section->flags & SEC_HAS_CONTENTS) == 0)
2159 {
2160 if (readbuf)
2161 memset (readbuf, 0, len);
2162
2163 *xfered_len = len;
2164 return TARGET_XFER_OK;
2165 }
2166 /* Get record_full_core_buf_entry. */
2167 for (entry = record_full_core_buf_list; entry;
2168 entry = entry->prev)
2169 if (entry->p == p)
2170 break;
2171 if (writebuf)
2172 {
2173 if (!entry)
2174 {
2175 /* Add a new entry. */
2176 entry = XNEW (struct record_full_core_buf_entry);
2177 entry->p = p;
2178 if (!bfd_malloc_and_get_section
2179 (p->the_bfd_section->owner,
2180 p->the_bfd_section,
2181 &entry->buf))
2182 {
2183 xfree (entry);
2184 return TARGET_XFER_EOF;
2185 }
2186 entry->prev = record_full_core_buf_list;
2187 record_full_core_buf_list = entry;
2188 }
2189
2190 memcpy (entry->buf + sec_offset, writebuf,
2191 (size_t) len);
2192 }
2193 else
2194 {
2195 if (!entry)
2196 return this->beneath ()->xfer_partial (object, annex,
2197 readbuf, writebuf,
2198 offset, len,
2199 xfered_len);
2200
2201 memcpy (readbuf, entry->buf + sec_offset,
2202 (size_t) len);
2203 }
2204
2205 *xfered_len = len;
2206 return TARGET_XFER_OK;
2207 }
2208 }
2209
2210 return TARGET_XFER_E_IO;
2211 }
2212 else
2213 error (_("You can't do that without a process to debug."));
2214 }
2215
2216 return this->beneath ()->xfer_partial (object, annex,
2217 readbuf, writebuf, offset, len,
2218 xfered_len);
2219 }
2220
2221 /* "insert_breakpoint" method for prec over corefile. */
2222
2223 int
2224 record_full_core_target::insert_breakpoint (struct gdbarch *gdbarch,
2225 struct bp_target_info *bp_tgt)
2226 {
2227 return 0;
2228 }
2229
2230 /* "remove_breakpoint" method for prec over corefile. */
2231
2232 int
2233 record_full_core_target::remove_breakpoint (struct gdbarch *gdbarch,
2234 struct bp_target_info *bp_tgt,
2235 enum remove_bp_reason reason)
2236 {
2237 return 0;
2238 }
2239
2240 /* "has_execution" method for prec over corefile. */
2241
2242 bool
2243 record_full_core_target::has_execution (ptid_t the_ptid)
2244 {
2245 return true;
2246 }
2247
2248 /* Record log save-file format
2249 Version 1 (never released)
2250
2251 Header:
2252 4 bytes: magic number htonl(0x20090829).
2253 NOTE: be sure to change whenever this file format changes!
2254
2255 Records:
2256 record_full_end:
2257 1 byte: record type (record_full_end, see enum record_full_type).
2258 record_full_reg:
2259 1 byte: record type (record_full_reg, see enum record_full_type).
2260 8 bytes: register id (network byte order).
2261 MAX_REGISTER_SIZE bytes: register value.
2262 record_full_mem:
2263 1 byte: record type (record_full_mem, see enum record_full_type).
2264 8 bytes: memory length (network byte order).
2265 8 bytes: memory address (network byte order).
2266 n bytes: memory value (n == memory length).
2267
2268 Version 2
2269 4 bytes: magic number netorder32(0x20091016).
2270 NOTE: be sure to change whenever this file format changes!
2271
2272 Records:
2273 record_full_end:
2274 1 byte: record type (record_full_end, see enum record_full_type).
2275 4 bytes: signal
2276 4 bytes: instruction count
2277 record_full_reg:
2278 1 byte: record type (record_full_reg, see enum record_full_type).
2279 4 bytes: register id (network byte order).
2280 n bytes: register value (n == actual register size).
2281 (eg. 4 bytes for x86 general registers).
2282 record_full_mem:
2283 1 byte: record type (record_full_mem, see enum record_full_type).
2284 4 bytes: memory length (network byte order).
2285 8 bytes: memory address (network byte order).
2286 n bytes: memory value (n == memory length).
2287
2288 */
2289
2290 /* bfdcore_read -- read bytes from a core file section. */
2291
2292 static inline void
2293 bfdcore_read (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2294 {
2295 int ret = bfd_get_section_contents (obfd, osec, buf, *offset, len);
2296
2297 if (ret)
2298 *offset += len;
2299 else
2300 error (_("Failed to read %d bytes from core file %s ('%s')."),
2301 len, bfd_get_filename (obfd),
2302 bfd_errmsg (bfd_get_error ()));
2303 }
2304
2305 static inline uint64_t
2306 netorder64 (uint64_t input)
2307 {
2308 uint64_t ret;
2309
2310 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2311 BFD_ENDIAN_BIG, input);
2312 return ret;
2313 }
2314
2315 static inline uint32_t
2316 netorder32 (uint32_t input)
2317 {
2318 uint32_t ret;
2319
2320 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2321 BFD_ENDIAN_BIG, input);
2322 return ret;
2323 }
2324
2325 /* Restore the execution log from a core_bfd file. */
2326 static void
2327 record_full_restore (void)
2328 {
2329 uint32_t magic;
2330 struct cleanup *old_cleanups;
2331 struct record_full_entry *rec;
2332 asection *osec;
2333 uint32_t osec_size;
2334 int bfd_offset = 0;
2335 struct regcache *regcache;
2336
2337 /* We restore the execution log from the open core bfd,
2338 if there is one. */
2339 if (core_bfd == NULL)
2340 return;
2341
2342 /* "record_full_restore" can only be called when record list is empty. */
2343 gdb_assert (record_full_first.next == NULL);
2344
2345 if (record_debug)
2346 fprintf_unfiltered (gdb_stdlog, "Restoring recording from core file.\n");
2347
2348 /* Now need to find our special note section. */
2349 osec = bfd_get_section_by_name (core_bfd, "null0");
2350 if (record_debug)
2351 fprintf_unfiltered (gdb_stdlog, "Find precord section %s.\n",
2352 osec ? "succeeded" : "failed");
2353 if (osec == NULL)
2354 return;
2355 osec_size = bfd_section_size (core_bfd, osec);
2356 if (record_debug)
2357 fprintf_unfiltered (gdb_stdlog, "%s", bfd_section_name (core_bfd, osec));
2358
2359 /* Check the magic code. */
2360 bfdcore_read (core_bfd, osec, &magic, sizeof (magic), &bfd_offset);
2361 if (magic != RECORD_FULL_FILE_MAGIC)
2362 error (_("Version mis-match or file format error in core file %s."),
2363 bfd_get_filename (core_bfd));
2364 if (record_debug)
2365 fprintf_unfiltered (gdb_stdlog,
2366 " Reading 4-byte magic cookie "
2367 "RECORD_FULL_FILE_MAGIC (0x%s)\n",
2368 phex_nz (netorder32 (magic), 4));
2369
2370 /* Restore the entries in recfd into record_full_arch_list_head and
2371 record_full_arch_list_tail. */
2372 record_full_arch_list_head = NULL;
2373 record_full_arch_list_tail = NULL;
2374 record_full_insn_num = 0;
2375 old_cleanups = make_cleanup (record_full_arch_list_cleanups, 0);
2376 regcache = get_current_regcache ();
2377
2378 while (1)
2379 {
2380 uint8_t rectype;
2381 uint32_t regnum, len, signal, count;
2382 uint64_t addr;
2383
2384 /* We are finished when offset reaches osec_size. */
2385 if (bfd_offset >= osec_size)
2386 break;
2387 bfdcore_read (core_bfd, osec, &rectype, sizeof (rectype), &bfd_offset);
2388
2389 switch (rectype)
2390 {
2391 case record_full_reg: /* reg */
2392 /* Get register number to regnum. */
2393 bfdcore_read (core_bfd, osec, &regnum,
2394 sizeof (regnum), &bfd_offset);
2395 regnum = netorder32 (regnum);
2396
2397 rec = record_full_reg_alloc (regcache, regnum);
2398
2399 /* Get val. */
2400 bfdcore_read (core_bfd, osec, record_full_get_loc (rec),
2401 rec->u.reg.len, &bfd_offset);
2402
2403 if (record_debug)
2404 fprintf_unfiltered (gdb_stdlog,
2405 " Reading register %d (1 "
2406 "plus %lu plus %d bytes)\n",
2407 rec->u.reg.num,
2408 (unsigned long) sizeof (regnum),
2409 rec->u.reg.len);
2410 break;
2411
2412 case record_full_mem: /* mem */
2413 /* Get len. */
2414 bfdcore_read (core_bfd, osec, &len,
2415 sizeof (len), &bfd_offset);
2416 len = netorder32 (len);
2417
2418 /* Get addr. */
2419 bfdcore_read (core_bfd, osec, &addr,
2420 sizeof (addr), &bfd_offset);
2421 addr = netorder64 (addr);
2422
2423 rec = record_full_mem_alloc (addr, len);
2424
2425 /* Get val. */
2426 bfdcore_read (core_bfd, osec, record_full_get_loc (rec),
2427 rec->u.mem.len, &bfd_offset);
2428
2429 if (record_debug)
2430 fprintf_unfiltered (gdb_stdlog,
2431 " Reading memory %s (1 plus "
2432 "%lu plus %lu plus %d bytes)\n",
2433 paddress (get_current_arch (),
2434 rec->u.mem.addr),
2435 (unsigned long) sizeof (addr),
2436 (unsigned long) sizeof (len),
2437 rec->u.mem.len);
2438 break;
2439
2440 case record_full_end: /* end */
2441 rec = record_full_end_alloc ();
2442 record_full_insn_num ++;
2443
2444 /* Get signal value. */
2445 bfdcore_read (core_bfd, osec, &signal,
2446 sizeof (signal), &bfd_offset);
2447 signal = netorder32 (signal);
2448 rec->u.end.sigval = (enum gdb_signal) signal;
2449
2450 /* Get insn count. */
2451 bfdcore_read (core_bfd, osec, &count,
2452 sizeof (count), &bfd_offset);
2453 count = netorder32 (count);
2454 rec->u.end.insn_num = count;
2455 record_full_insn_count = count + 1;
2456 if (record_debug)
2457 fprintf_unfiltered (gdb_stdlog,
2458 " Reading record_full_end (1 + "
2459 "%lu + %lu bytes), offset == %s\n",
2460 (unsigned long) sizeof (signal),
2461 (unsigned long) sizeof (count),
2462 paddress (get_current_arch (),
2463 bfd_offset));
2464 break;
2465
2466 default:
2467 error (_("Bad entry type in core file %s."),
2468 bfd_get_filename (core_bfd));
2469 break;
2470 }
2471
2472 /* Add rec to record arch list. */
2473 record_full_arch_list_add (rec);
2474 }
2475
2476 discard_cleanups (old_cleanups);
2477
2478 /* Add record_full_arch_list_head to the end of record list. */
2479 record_full_first.next = record_full_arch_list_head;
2480 record_full_arch_list_head->prev = &record_full_first;
2481 record_full_arch_list_tail->next = NULL;
2482 record_full_list = &record_full_first;
2483
2484 /* Update record_full_insn_max_num. */
2485 if (record_full_insn_num > record_full_insn_max_num)
2486 {
2487 record_full_insn_max_num = record_full_insn_num;
2488 warning (_("Auto increase record/replay buffer limit to %u."),
2489 record_full_insn_max_num);
2490 }
2491
2492 /* Succeeded. */
2493 printf_filtered (_("Restored records from core file %s.\n"),
2494 bfd_get_filename (core_bfd));
2495
2496 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
2497 }
2498
2499 /* bfdcore_write -- write bytes into a core file section. */
2500
2501 static inline void
2502 bfdcore_write (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2503 {
2504 int ret = bfd_set_section_contents (obfd, osec, buf, *offset, len);
2505
2506 if (ret)
2507 *offset += len;
2508 else
2509 error (_("Failed to write %d bytes to core file %s ('%s')."),
2510 len, bfd_get_filename (obfd),
2511 bfd_errmsg (bfd_get_error ()));
2512 }
2513
2514 /* Restore the execution log from a file. We use a modified elf
2515 corefile format, with an extra section for our data. */
2516
2517 static void
2518 cmd_record_full_restore (const char *args, int from_tty)
2519 {
2520 core_file_command (args, from_tty);
2521 record_full_open (args, from_tty);
2522 }
2523
2524 /* Save the execution log to a file. We use a modified elf corefile
2525 format, with an extra section for our data. */
2526
2527 void
2528 record_full_base_target::save_record (const char *recfilename)
2529 {
2530 struct record_full_entry *cur_record_full_list;
2531 uint32_t magic;
2532 struct regcache *regcache;
2533 struct gdbarch *gdbarch;
2534 int save_size = 0;
2535 asection *osec = NULL;
2536 int bfd_offset = 0;
2537
2538 /* Open the save file. */
2539 if (record_debug)
2540 fprintf_unfiltered (gdb_stdlog, "Saving execution log to core file '%s'\n",
2541 recfilename);
2542
2543 /* Open the output file. */
2544 gdb_bfd_ref_ptr obfd (create_gcore_bfd (recfilename));
2545
2546 /* Arrange to remove the output file on failure. */
2547 gdb::unlinker unlink_file (recfilename);
2548
2549 /* Save the current record entry to "cur_record_full_list". */
2550 cur_record_full_list = record_full_list;
2551
2552 /* Get the values of regcache and gdbarch. */
2553 regcache = get_current_regcache ();
2554 gdbarch = regcache->arch ();
2555
2556 /* Disable the GDB operation record. */
2557 scoped_restore restore_operation_disable
2558 = record_full_gdb_operation_disable_set ();
2559
2560 /* Reverse execute to the begin of record list. */
2561 while (1)
2562 {
2563 /* Check for beginning and end of log. */
2564 if (record_full_list == &record_full_first)
2565 break;
2566
2567 record_full_exec_insn (regcache, gdbarch, record_full_list);
2568
2569 if (record_full_list->prev)
2570 record_full_list = record_full_list->prev;
2571 }
2572
2573 /* Compute the size needed for the extra bfd section. */
2574 save_size = 4; /* magic cookie */
2575 for (record_full_list = record_full_first.next; record_full_list;
2576 record_full_list = record_full_list->next)
2577 switch (record_full_list->type)
2578 {
2579 case record_full_end:
2580 save_size += 1 + 4 + 4;
2581 break;
2582 case record_full_reg:
2583 save_size += 1 + 4 + record_full_list->u.reg.len;
2584 break;
2585 case record_full_mem:
2586 save_size += 1 + 4 + 8 + record_full_list->u.mem.len;
2587 break;
2588 }
2589
2590 /* Make the new bfd section. */
2591 osec = bfd_make_section_anyway_with_flags (obfd.get (), "precord",
2592 SEC_HAS_CONTENTS
2593 | SEC_READONLY);
2594 if (osec == NULL)
2595 error (_("Failed to create 'precord' section for corefile %s: %s"),
2596 recfilename,
2597 bfd_errmsg (bfd_get_error ()));
2598 bfd_set_section_size (obfd.get (), osec, save_size);
2599 bfd_set_section_vma (obfd.get (), osec, 0);
2600 bfd_set_section_alignment (obfd.get (), osec, 0);
2601 bfd_section_lma (obfd.get (), osec) = 0;
2602
2603 /* Save corefile state. */
2604 write_gcore_file (obfd.get ());
2605
2606 /* Write out the record log. */
2607 /* Write the magic code. */
2608 magic = RECORD_FULL_FILE_MAGIC;
2609 if (record_debug)
2610 fprintf_unfiltered (gdb_stdlog,
2611 " Writing 4-byte magic cookie "
2612 "RECORD_FULL_FILE_MAGIC (0x%s)\n",
2613 phex_nz (magic, 4));
2614 bfdcore_write (obfd.get (), osec, &magic, sizeof (magic), &bfd_offset);
2615
2616 /* Save the entries to recfd and forward execute to the end of
2617 record list. */
2618 record_full_list = &record_full_first;
2619 while (1)
2620 {
2621 /* Save entry. */
2622 if (record_full_list != &record_full_first)
2623 {
2624 uint8_t type;
2625 uint32_t regnum, len, signal, count;
2626 uint64_t addr;
2627
2628 type = record_full_list->type;
2629 bfdcore_write (obfd.get (), osec, &type, sizeof (type), &bfd_offset);
2630
2631 switch (record_full_list->type)
2632 {
2633 case record_full_reg: /* reg */
2634 if (record_debug)
2635 fprintf_unfiltered (gdb_stdlog,
2636 " Writing register %d (1 "
2637 "plus %lu plus %d bytes)\n",
2638 record_full_list->u.reg.num,
2639 (unsigned long) sizeof (regnum),
2640 record_full_list->u.reg.len);
2641
2642 /* Write regnum. */
2643 regnum = netorder32 (record_full_list->u.reg.num);
2644 bfdcore_write (obfd.get (), osec, &regnum,
2645 sizeof (regnum), &bfd_offset);
2646
2647 /* Write regval. */
2648 bfdcore_write (obfd.get (), osec,
2649 record_full_get_loc (record_full_list),
2650 record_full_list->u.reg.len, &bfd_offset);
2651 break;
2652
2653 case record_full_mem: /* mem */
2654 if (record_debug)
2655 fprintf_unfiltered (gdb_stdlog,
2656 " Writing memory %s (1 plus "
2657 "%lu plus %lu plus %d bytes)\n",
2658 paddress (gdbarch,
2659 record_full_list->u.mem.addr),
2660 (unsigned long) sizeof (addr),
2661 (unsigned long) sizeof (len),
2662 record_full_list->u.mem.len);
2663
2664 /* Write memlen. */
2665 len = netorder32 (record_full_list->u.mem.len);
2666 bfdcore_write (obfd.get (), osec, &len, sizeof (len),
2667 &bfd_offset);
2668
2669 /* Write memaddr. */
2670 addr = netorder64 (record_full_list->u.mem.addr);
2671 bfdcore_write (obfd.get (), osec, &addr,
2672 sizeof (addr), &bfd_offset);
2673
2674 /* Write memval. */
2675 bfdcore_write (obfd.get (), osec,
2676 record_full_get_loc (record_full_list),
2677 record_full_list->u.mem.len, &bfd_offset);
2678 break;
2679
2680 case record_full_end:
2681 if (record_debug)
2682 fprintf_unfiltered (gdb_stdlog,
2683 " Writing record_full_end (1 + "
2684 "%lu + %lu bytes)\n",
2685 (unsigned long) sizeof (signal),
2686 (unsigned long) sizeof (count));
2687 /* Write signal value. */
2688 signal = netorder32 (record_full_list->u.end.sigval);
2689 bfdcore_write (obfd.get (), osec, &signal,
2690 sizeof (signal), &bfd_offset);
2691
2692 /* Write insn count. */
2693 count = netorder32 (record_full_list->u.end.insn_num);
2694 bfdcore_write (obfd.get (), osec, &count,
2695 sizeof (count), &bfd_offset);
2696 break;
2697 }
2698 }
2699
2700 /* Execute entry. */
2701 record_full_exec_insn (regcache, gdbarch, record_full_list);
2702
2703 if (record_full_list->next)
2704 record_full_list = record_full_list->next;
2705 else
2706 break;
2707 }
2708
2709 /* Reverse execute to cur_record_full_list. */
2710 while (1)
2711 {
2712 /* Check for beginning and end of log. */
2713 if (record_full_list == cur_record_full_list)
2714 break;
2715
2716 record_full_exec_insn (regcache, gdbarch, record_full_list);
2717
2718 if (record_full_list->prev)
2719 record_full_list = record_full_list->prev;
2720 }
2721
2722 unlink_file.keep ();
2723
2724 /* Succeeded. */
2725 printf_filtered (_("Saved core file %s with execution log.\n"),
2726 recfilename);
2727 }
2728
2729 /* record_full_goto_insn -- rewind the record log (forward or backward,
2730 depending on DIR) to the given entry, changing the program state
2731 correspondingly. */
2732
2733 static void
2734 record_full_goto_insn (struct record_full_entry *entry,
2735 enum exec_direction_kind dir)
2736 {
2737 scoped_restore restore_operation_disable
2738 = record_full_gdb_operation_disable_set ();
2739 struct regcache *regcache = get_current_regcache ();
2740 struct gdbarch *gdbarch = regcache->arch ();
2741
2742 /* Assume everything is valid: we will hit the entry,
2743 and we will not hit the end of the recording. */
2744
2745 if (dir == EXEC_FORWARD)
2746 record_full_list = record_full_list->next;
2747
2748 do
2749 {
2750 record_full_exec_insn (regcache, gdbarch, record_full_list);
2751 if (dir == EXEC_REVERSE)
2752 record_full_list = record_full_list->prev;
2753 else
2754 record_full_list = record_full_list->next;
2755 } while (record_full_list != entry);
2756 }
2757
2758 /* Alias for "target record-full". */
2759
2760 static void
2761 cmd_record_full_start (const char *args, int from_tty)
2762 {
2763 execute_command ("target record-full", from_tty);
2764 }
2765
2766 static void
2767 set_record_full_insn_max_num (const char *args, int from_tty,
2768 struct cmd_list_element *c)
2769 {
2770 if (record_full_insn_num > record_full_insn_max_num)
2771 {
2772 /* Count down record_full_insn_num while releasing records from list. */
2773 while (record_full_insn_num > record_full_insn_max_num)
2774 {
2775 record_full_list_release_first ();
2776 record_full_insn_num--;
2777 }
2778 }
2779 }
2780
2781 /* The "set record full" command. */
2782
2783 static void
2784 set_record_full_command (const char *args, int from_tty)
2785 {
2786 printf_unfiltered (_("\"set record full\" must be followed "
2787 "by an appropriate subcommand.\n"));
2788 help_list (set_record_full_cmdlist, "set record full ", all_commands,
2789 gdb_stdout);
2790 }
2791
2792 /* The "show record full" command. */
2793
2794 static void
2795 show_record_full_command (const char *args, int from_tty)
2796 {
2797 cmd_show_list (show_record_full_cmdlist, from_tty, "");
2798 }
2799
2800 void
2801 _initialize_record_full (void)
2802 {
2803 struct cmd_list_element *c;
2804
2805 /* Init record_full_first. */
2806 record_full_first.prev = NULL;
2807 record_full_first.next = NULL;
2808 record_full_first.type = record_full_end;
2809
2810 add_target (record_full_target_info, record_full_open);
2811 add_deprecated_target_alias (record_full_target_info, "record");
2812 add_target (record_full_core_target_info, record_full_open);
2813
2814 add_prefix_cmd ("full", class_obscure, cmd_record_full_start,
2815 _("Start full execution recording."), &record_full_cmdlist,
2816 "record full ", 0, &record_cmdlist);
2817
2818 c = add_cmd ("restore", class_obscure, cmd_record_full_restore,
2819 _("Restore the execution log from a file.\n\
2820 Argument is filename. File must be created with 'record save'."),
2821 &record_full_cmdlist);
2822 set_cmd_completer (c, filename_completer);
2823
2824 /* Deprecate the old version without "full" prefix. */
2825 c = add_alias_cmd ("restore", "full restore", class_obscure, 1,
2826 &record_cmdlist);
2827 set_cmd_completer (c, filename_completer);
2828 deprecate_cmd (c, "record full restore");
2829
2830 add_prefix_cmd ("full", class_support, set_record_full_command,
2831 _("Set record options"), &set_record_full_cmdlist,
2832 "set record full ", 0, &set_record_cmdlist);
2833
2834 add_prefix_cmd ("full", class_support, show_record_full_command,
2835 _("Show record options"), &show_record_full_cmdlist,
2836 "show record full ", 0, &show_record_cmdlist);
2837
2838 /* Record instructions number limit command. */
2839 add_setshow_boolean_cmd ("stop-at-limit", no_class,
2840 &record_full_stop_at_limit, _("\
2841 Set whether record/replay stops when record/replay buffer becomes full."), _("\
2842 Show whether record/replay stops when record/replay buffer becomes full."),
2843 _("Default is ON.\n\
2844 When ON, if the record/replay buffer becomes full, ask user what to do.\n\
2845 When OFF, if the record/replay buffer becomes full,\n\
2846 delete the oldest recorded instruction to make room for each new one."),
2847 NULL, NULL,
2848 &set_record_full_cmdlist, &show_record_full_cmdlist);
2849
2850 c = add_alias_cmd ("stop-at-limit", "full stop-at-limit", no_class, 1,
2851 &set_record_cmdlist);
2852 deprecate_cmd (c, "set record full stop-at-limit");
2853
2854 c = add_alias_cmd ("stop-at-limit", "full stop-at-limit", no_class, 1,
2855 &show_record_cmdlist);
2856 deprecate_cmd (c, "show record full stop-at-limit");
2857
2858 add_setshow_uinteger_cmd ("insn-number-max", no_class,
2859 &record_full_insn_max_num,
2860 _("Set record/replay buffer limit."),
2861 _("Show record/replay buffer limit."), _("\
2862 Set the maximum number of instructions to be stored in the\n\
2863 record/replay buffer. A value of either \"unlimited\" or zero means no\n\
2864 limit. Default is 200000."),
2865 set_record_full_insn_max_num,
2866 NULL, &set_record_full_cmdlist,
2867 &show_record_full_cmdlist);
2868
2869 c = add_alias_cmd ("insn-number-max", "full insn-number-max", no_class, 1,
2870 &set_record_cmdlist);
2871 deprecate_cmd (c, "set record full insn-number-max");
2872
2873 c = add_alias_cmd ("insn-number-max", "full insn-number-max", no_class, 1,
2874 &show_record_cmdlist);
2875 deprecate_cmd (c, "show record full insn-number-max");
2876
2877 add_setshow_boolean_cmd ("memory-query", no_class,
2878 &record_full_memory_query, _("\
2879 Set whether query if PREC cannot record memory change of next instruction."),
2880 _("\
2881 Show whether query if PREC cannot record memory change of next instruction."),
2882 _("\
2883 Default is OFF.\n\
2884 When ON, query if PREC cannot record memory change of next instruction."),
2885 NULL, NULL,
2886 &set_record_full_cmdlist,
2887 &show_record_full_cmdlist);
2888
2889 c = add_alias_cmd ("memory-query", "full memory-query", no_class, 1,
2890 &set_record_cmdlist);
2891 deprecate_cmd (c, "set record full memory-query");
2892
2893 c = add_alias_cmd ("memory-query", "full memory-query", no_class, 1,
2894 &show_record_cmdlist);
2895 deprecate_cmd (c, "show record full memory-query");
2896 }