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