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