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