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