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