056b03b3fc70fadfbdf9835e55c56ab6b0495cb1
[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 (ptid_t) 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 static int record_full_resume_step = 0;
1040
1041 /* True if we've been resumed, and so each record_full_wait call should
1042 advance execution. If this is false, record_full_wait will return a
1043 TARGET_WAITKIND_IGNORE. */
1044 static int record_full_resumed = 0;
1045
1046 /* The execution direction of the last resume we got. This is
1047 necessary for async mode. Vis (order is not strictly accurate):
1048
1049 1. user has the global execution direction set to forward
1050 2. user does a reverse-step command
1051 3. record_full_resume is called with global execution direction
1052 temporarily switched to reverse
1053 4. GDB's execution direction is reverted back to forward
1054 5. target record notifies event loop there's an event to handle
1055 6. infrun asks the target which direction was it going, and switches
1056 the global execution direction accordingly (to reverse)
1057 7. infrun polls an event out of the record target, and handles it
1058 8. GDB goes back to the event loop, and goto #4.
1059 */
1060 static enum exec_direction_kind record_full_execution_dir = EXEC_FORWARD;
1061
1062 /* "resume" target method. Resume the process record target. */
1063
1064 void
1065 record_full_target::resume (ptid_t ptid, int step, enum gdb_signal signal)
1066 {
1067 record_full_resume_step = step;
1068 record_full_resumed = 1;
1069 record_full_execution_dir = ::execution_direction;
1070
1071 if (!RECORD_FULL_IS_REPLAY)
1072 {
1073 struct gdbarch *gdbarch = target_thread_architecture (ptid);
1074
1075 record_full_message (get_current_regcache (), signal);
1076
1077 if (!step)
1078 {
1079 /* This is not hard single step. */
1080 if (!gdbarch_software_single_step_p (gdbarch))
1081 {
1082 /* This is a normal continue. */
1083 step = 1;
1084 }
1085 else
1086 {
1087 /* This arch supports soft single step. */
1088 if (thread_has_single_step_breakpoints_set (inferior_thread ()))
1089 {
1090 /* This is a soft single step. */
1091 record_full_resume_step = 1;
1092 }
1093 else
1094 step = !insert_single_step_breakpoints (gdbarch);
1095 }
1096 }
1097
1098 /* Make sure the target beneath reports all signals. */
1099 target_pass_signals ({});
1100
1101 this->beneath ()->resume (ptid, step, signal);
1102 }
1103
1104 /* We are about to start executing the inferior (or simulate it),
1105 let's register it with the event loop. */
1106 if (target_can_async_p ())
1107 target_async (1);
1108 }
1109
1110 /* "commit_resume" method for process record target. */
1111
1112 void
1113 record_full_target::commit_resume ()
1114 {
1115 if (!RECORD_FULL_IS_REPLAY)
1116 beneath ()->commit_resume ();
1117 }
1118
1119 static int record_full_get_sig = 0;
1120
1121 /* SIGINT signal handler, registered by "wait" method. */
1122
1123 static void
1124 record_full_sig_handler (int signo)
1125 {
1126 if (record_debug)
1127 fprintf_unfiltered (gdb_stdlog, "Process record: get a signal\n");
1128
1129 /* It will break the running inferior in replay mode. */
1130 record_full_resume_step = 1;
1131
1132 /* It will let record_full_wait set inferior status to get the signal
1133 SIGINT. */
1134 record_full_get_sig = 1;
1135 }
1136
1137 /* "wait" target method for process record target.
1138
1139 In record mode, the target is always run in singlestep mode
1140 (even when gdb says to continue). The wait method intercepts
1141 the stop events and determines which ones are to be passed on to
1142 gdb. Most stop events are just singlestep events that gdb is not
1143 to know about, so the wait method just records them and keeps
1144 singlestepping.
1145
1146 In replay mode, this function emulates the recorded execution log,
1147 one instruction at a time (forward or backward), and determines
1148 where to stop. */
1149
1150 static ptid_t
1151 record_full_wait_1 (struct target_ops *ops,
1152 ptid_t ptid, struct target_waitstatus *status,
1153 int options)
1154 {
1155 scoped_restore restore_operation_disable
1156 = record_full_gdb_operation_disable_set ();
1157
1158 if (record_debug)
1159 fprintf_unfiltered (gdb_stdlog,
1160 "Process record: record_full_wait "
1161 "record_full_resume_step = %d, "
1162 "record_full_resumed = %d, direction=%s\n",
1163 record_full_resume_step, record_full_resumed,
1164 record_full_execution_dir == EXEC_FORWARD
1165 ? "forward" : "reverse");
1166
1167 if (!record_full_resumed)
1168 {
1169 gdb_assert ((options & TARGET_WNOHANG) != 0);
1170
1171 /* No interesting event. */
1172 status->kind = TARGET_WAITKIND_IGNORE;
1173 return minus_one_ptid;
1174 }
1175
1176 record_full_get_sig = 0;
1177 signal (SIGINT, record_full_sig_handler);
1178
1179 record_full_stop_reason = TARGET_STOPPED_BY_NO_REASON;
1180
1181 if (!RECORD_FULL_IS_REPLAY && ops != &record_full_core_ops)
1182 {
1183 if (record_full_resume_step)
1184 {
1185 /* This is a single step. */
1186 return ops->beneath ()->wait (ptid, status, options);
1187 }
1188 else
1189 {
1190 /* This is not a single step. */
1191 ptid_t ret;
1192 CORE_ADDR tmp_pc;
1193 struct gdbarch *gdbarch = target_thread_architecture (inferior_ptid);
1194
1195 while (1)
1196 {
1197 ret = ops->beneath ()->wait (ptid, status, options);
1198 if (status->kind == TARGET_WAITKIND_IGNORE)
1199 {
1200 if (record_debug)
1201 fprintf_unfiltered (gdb_stdlog,
1202 "Process record: record_full_wait "
1203 "target beneath not done yet\n");
1204 return ret;
1205 }
1206
1207 for (thread_info *tp : all_non_exited_threads ())
1208 delete_single_step_breakpoints (tp);
1209
1210 if (record_full_resume_step)
1211 return ret;
1212
1213 /* Is this a SIGTRAP? */
1214 if (status->kind == TARGET_WAITKIND_STOPPED
1215 && status->value.sig == GDB_SIGNAL_TRAP)
1216 {
1217 struct regcache *regcache;
1218 enum target_stop_reason *stop_reason_p
1219 = &record_full_stop_reason;
1220
1221 /* Yes -- this is likely our single-step finishing,
1222 but check if there's any reason the core would be
1223 interested in the event. */
1224
1225 registers_changed ();
1226 regcache = get_current_regcache ();
1227 tmp_pc = regcache_read_pc (regcache);
1228 const struct address_space *aspace = regcache->aspace ();
1229
1230 if (target_stopped_by_watchpoint ())
1231 {
1232 /* Always interested in watchpoints. */
1233 }
1234 else if (record_check_stopped_by_breakpoint (aspace, tmp_pc,
1235 stop_reason_p))
1236 {
1237 /* There is a breakpoint here. Let the core
1238 handle it. */
1239 }
1240 else
1241 {
1242 /* This is a single-step trap. Record the
1243 insn and issue another step.
1244 FIXME: this part can be a random SIGTRAP too.
1245 But GDB cannot handle it. */
1246 int step = 1;
1247
1248 if (!record_full_message_wrapper_safe (regcache,
1249 GDB_SIGNAL_0))
1250 {
1251 status->kind = TARGET_WAITKIND_STOPPED;
1252 status->value.sig = GDB_SIGNAL_0;
1253 break;
1254 }
1255
1256 if (gdbarch_software_single_step_p (gdbarch))
1257 {
1258 /* Try to insert the software single step breakpoint.
1259 If insert success, set step to 0. */
1260 set_executing (inferior_ptid, 0);
1261 reinit_frame_cache ();
1262
1263 step = !insert_single_step_breakpoints (gdbarch);
1264
1265 set_executing (inferior_ptid, 1);
1266 }
1267
1268 if (record_debug)
1269 fprintf_unfiltered (gdb_stdlog,
1270 "Process record: record_full_wait "
1271 "issuing one more step in the "
1272 "target beneath\n");
1273 ops->beneath ()->resume (ptid, step, GDB_SIGNAL_0);
1274 ops->beneath ()->commit_resume ();
1275 continue;
1276 }
1277 }
1278
1279 /* The inferior is broken by a breakpoint or a signal. */
1280 break;
1281 }
1282
1283 return ret;
1284 }
1285 }
1286 else
1287 {
1288 struct regcache *regcache = get_current_regcache ();
1289 struct gdbarch *gdbarch = regcache->arch ();
1290 const struct address_space *aspace = regcache->aspace ();
1291 int continue_flag = 1;
1292 int first_record_full_end = 1;
1293
1294 try
1295 {
1296 CORE_ADDR tmp_pc;
1297
1298 record_full_stop_reason = TARGET_STOPPED_BY_NO_REASON;
1299 status->kind = TARGET_WAITKIND_STOPPED;
1300
1301 /* Check breakpoint when forward execute. */
1302 if (execution_direction == EXEC_FORWARD)
1303 {
1304 tmp_pc = regcache_read_pc (regcache);
1305 if (record_check_stopped_by_breakpoint (aspace, tmp_pc,
1306 &record_full_stop_reason))
1307 {
1308 if (record_debug)
1309 fprintf_unfiltered (gdb_stdlog,
1310 "Process record: break at %s.\n",
1311 paddress (gdbarch, tmp_pc));
1312 goto replay_out;
1313 }
1314 }
1315
1316 /* If GDB is in terminal_inferior mode, it will not get the
1317 signal. And in GDB replay mode, GDB doesn't need to be
1318 in terminal_inferior mode, because inferior will not
1319 executed. Then set it to terminal_ours to make GDB get
1320 the signal. */
1321 target_terminal::ours ();
1322
1323 /* In EXEC_FORWARD mode, record_full_list points to the tail of prev
1324 instruction. */
1325 if (execution_direction == EXEC_FORWARD && record_full_list->next)
1326 record_full_list = record_full_list->next;
1327
1328 /* Loop over the record_full_list, looking for the next place to
1329 stop. */
1330 do
1331 {
1332 /* Check for beginning and end of log. */
1333 if (execution_direction == EXEC_REVERSE
1334 && record_full_list == &record_full_first)
1335 {
1336 /* Hit beginning of record log in reverse. */
1337 status->kind = TARGET_WAITKIND_NO_HISTORY;
1338 break;
1339 }
1340 if (execution_direction != EXEC_REVERSE
1341 && !record_full_list->next)
1342 {
1343 /* Hit end of record log going forward. */
1344 status->kind = TARGET_WAITKIND_NO_HISTORY;
1345 break;
1346 }
1347
1348 record_full_exec_insn (regcache, gdbarch, record_full_list);
1349
1350 if (record_full_list->type == record_full_end)
1351 {
1352 if (record_debug > 1)
1353 fprintf_unfiltered
1354 (gdb_stdlog,
1355 "Process record: record_full_end %s to "
1356 "inferior.\n",
1357 host_address_to_string (record_full_list));
1358
1359 if (first_record_full_end
1360 && execution_direction == EXEC_REVERSE)
1361 {
1362 /* When reverse execute, the first
1363 record_full_end is the part of current
1364 instruction. */
1365 first_record_full_end = 0;
1366 }
1367 else
1368 {
1369 /* In EXEC_REVERSE mode, this is the
1370 record_full_end of prev instruction. In
1371 EXEC_FORWARD mode, this is the
1372 record_full_end of current instruction. */
1373 /* step */
1374 if (record_full_resume_step)
1375 {
1376 if (record_debug > 1)
1377 fprintf_unfiltered (gdb_stdlog,
1378 "Process record: step.\n");
1379 continue_flag = 0;
1380 }
1381
1382 /* check breakpoint */
1383 tmp_pc = regcache_read_pc (regcache);
1384 if (record_check_stopped_by_breakpoint
1385 (aspace, tmp_pc, &record_full_stop_reason))
1386 {
1387 if (record_debug)
1388 fprintf_unfiltered (gdb_stdlog,
1389 "Process record: break "
1390 "at %s.\n",
1391 paddress (gdbarch, tmp_pc));
1392
1393 continue_flag = 0;
1394 }
1395
1396 if (record_full_stop_reason
1397 == TARGET_STOPPED_BY_WATCHPOINT)
1398 {
1399 if (record_debug)
1400 fprintf_unfiltered (gdb_stdlog,
1401 "Process record: hit hw "
1402 "watchpoint.\n");
1403 continue_flag = 0;
1404 }
1405 /* Check target signal */
1406 if (record_full_list->u.end.sigval != GDB_SIGNAL_0)
1407 /* FIXME: better way to check */
1408 continue_flag = 0;
1409 }
1410 }
1411
1412 if (continue_flag)
1413 {
1414 if (execution_direction == EXEC_REVERSE)
1415 {
1416 if (record_full_list->prev)
1417 record_full_list = record_full_list->prev;
1418 }
1419 else
1420 {
1421 if (record_full_list->next)
1422 record_full_list = record_full_list->next;
1423 }
1424 }
1425 }
1426 while (continue_flag);
1427
1428 replay_out:
1429 if (record_full_get_sig)
1430 status->value.sig = GDB_SIGNAL_INT;
1431 else if (record_full_list->u.end.sigval != GDB_SIGNAL_0)
1432 /* FIXME: better way to check */
1433 status->value.sig = record_full_list->u.end.sigval;
1434 else
1435 status->value.sig = GDB_SIGNAL_TRAP;
1436 }
1437 catch (const gdb_exception &ex)
1438 {
1439 if (execution_direction == EXEC_REVERSE)
1440 {
1441 if (record_full_list->next)
1442 record_full_list = record_full_list->next;
1443 }
1444 else
1445 record_full_list = record_full_list->prev;
1446
1447 throw;
1448 }
1449 }
1450
1451 signal (SIGINT, handle_sigint);
1452
1453 return inferior_ptid;
1454 }
1455
1456 ptid_t
1457 record_full_base_target::wait (ptid_t ptid, struct target_waitstatus *status,
1458 int options)
1459 {
1460 ptid_t return_ptid;
1461
1462 return_ptid = record_full_wait_1 (this, ptid, status, options);
1463 if (status->kind != TARGET_WAITKIND_IGNORE)
1464 {
1465 /* We're reporting a stop. Make sure any spurious
1466 target_wait(WNOHANG) doesn't advance the target until the
1467 core wants us resumed again. */
1468 record_full_resumed = 0;
1469 }
1470 return return_ptid;
1471 }
1472
1473 bool
1474 record_full_base_target::stopped_by_watchpoint ()
1475 {
1476 if (RECORD_FULL_IS_REPLAY)
1477 return record_full_stop_reason == TARGET_STOPPED_BY_WATCHPOINT;
1478 else
1479 return beneath ()->stopped_by_watchpoint ();
1480 }
1481
1482 bool
1483 record_full_base_target::stopped_data_address (CORE_ADDR *addr_p)
1484 {
1485 if (RECORD_FULL_IS_REPLAY)
1486 return false;
1487 else
1488 return this->beneath ()->stopped_data_address (addr_p);
1489 }
1490
1491 /* The stopped_by_sw_breakpoint method of target record-full. */
1492
1493 bool
1494 record_full_base_target::stopped_by_sw_breakpoint ()
1495 {
1496 return record_full_stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT;
1497 }
1498
1499 /* The supports_stopped_by_sw_breakpoint method of target
1500 record-full. */
1501
1502 bool
1503 record_full_base_target::supports_stopped_by_sw_breakpoint ()
1504 {
1505 return true;
1506 }
1507
1508 /* The stopped_by_hw_breakpoint method of target record-full. */
1509
1510 bool
1511 record_full_base_target::stopped_by_hw_breakpoint ()
1512 {
1513 return record_full_stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT;
1514 }
1515
1516 /* The supports_stopped_by_sw_breakpoint method of target
1517 record-full. */
1518
1519 bool
1520 record_full_base_target::supports_stopped_by_hw_breakpoint ()
1521 {
1522 return true;
1523 }
1524
1525 /* Record registers change (by user or by GDB) to list as an instruction. */
1526
1527 static void
1528 record_full_registers_change (struct regcache *regcache, int regnum)
1529 {
1530 /* Check record_full_insn_num. */
1531 record_full_check_insn_num ();
1532
1533 record_full_arch_list_head = NULL;
1534 record_full_arch_list_tail = NULL;
1535
1536 if (regnum < 0)
1537 {
1538 int i;
1539
1540 for (i = 0; i < gdbarch_num_regs (regcache->arch ()); i++)
1541 {
1542 if (record_full_arch_list_add_reg (regcache, i))
1543 {
1544 record_full_list_release (record_full_arch_list_tail);
1545 error (_("Process record: failed to record execution log."));
1546 }
1547 }
1548 }
1549 else
1550 {
1551 if (record_full_arch_list_add_reg (regcache, regnum))
1552 {
1553 record_full_list_release (record_full_arch_list_tail);
1554 error (_("Process record: failed to record execution log."));
1555 }
1556 }
1557 if (record_full_arch_list_add_end ())
1558 {
1559 record_full_list_release (record_full_arch_list_tail);
1560 error (_("Process record: failed to record execution log."));
1561 }
1562 record_full_list->next = record_full_arch_list_head;
1563 record_full_arch_list_head->prev = record_full_list;
1564 record_full_list = record_full_arch_list_tail;
1565
1566 if (record_full_insn_num == record_full_insn_max_num)
1567 record_full_list_release_first ();
1568 else
1569 record_full_insn_num++;
1570 }
1571
1572 /* "store_registers" method for process record target. */
1573
1574 void
1575 record_full_target::store_registers (struct regcache *regcache, int regno)
1576 {
1577 if (!record_full_gdb_operation_disable)
1578 {
1579 if (RECORD_FULL_IS_REPLAY)
1580 {
1581 int n;
1582
1583 /* Let user choose if he wants to write register or not. */
1584 if (regno < 0)
1585 n =
1586 query (_("Because GDB is in replay mode, changing the "
1587 "value of a register will make the execution "
1588 "log unusable from this point onward. "
1589 "Change all registers?"));
1590 else
1591 n =
1592 query (_("Because GDB is in replay mode, changing the value "
1593 "of a register will make the execution log unusable "
1594 "from this point onward. Change register %s?"),
1595 gdbarch_register_name (regcache->arch (),
1596 regno));
1597
1598 if (!n)
1599 {
1600 /* Invalidate the value of regcache that was set in function
1601 "regcache_raw_write". */
1602 if (regno < 0)
1603 {
1604 int i;
1605
1606 for (i = 0;
1607 i < gdbarch_num_regs (regcache->arch ());
1608 i++)
1609 regcache->invalidate (i);
1610 }
1611 else
1612 regcache->invalidate (regno);
1613
1614 error (_("Process record canceled the operation."));
1615 }
1616
1617 /* Destroy the record from here forward. */
1618 record_full_list_release_following (record_full_list);
1619 }
1620
1621 record_full_registers_change (regcache, regno);
1622 }
1623 this->beneath ()->store_registers (regcache, regno);
1624 }
1625
1626 /* "xfer_partial" method. Behavior is conditional on
1627 RECORD_FULL_IS_REPLAY.
1628 In replay mode, we cannot write memory unles we are willing to
1629 invalidate the record/replay log from this point forward. */
1630
1631 enum target_xfer_status
1632 record_full_target::xfer_partial (enum target_object object,
1633 const char *annex, gdb_byte *readbuf,
1634 const gdb_byte *writebuf, ULONGEST offset,
1635 ULONGEST len, ULONGEST *xfered_len)
1636 {
1637 if (!record_full_gdb_operation_disable
1638 && (object == TARGET_OBJECT_MEMORY
1639 || object == TARGET_OBJECT_RAW_MEMORY) && writebuf)
1640 {
1641 if (RECORD_FULL_IS_REPLAY)
1642 {
1643 /* Let user choose if he wants to write memory or not. */
1644 if (!query (_("Because GDB is in replay mode, writing to memory "
1645 "will make the execution log unusable from this "
1646 "point onward. Write memory at address %s?"),
1647 paddress (target_gdbarch (), offset)))
1648 error (_("Process record canceled the operation."));
1649
1650 /* Destroy the record from here forward. */
1651 record_full_list_release_following (record_full_list);
1652 }
1653
1654 /* Check record_full_insn_num */
1655 record_full_check_insn_num ();
1656
1657 /* Record registers change to list as an instruction. */
1658 record_full_arch_list_head = NULL;
1659 record_full_arch_list_tail = NULL;
1660 if (record_full_arch_list_add_mem (offset, len))
1661 {
1662 record_full_list_release (record_full_arch_list_tail);
1663 if (record_debug)
1664 fprintf_unfiltered (gdb_stdlog,
1665 "Process record: failed to record "
1666 "execution log.");
1667 return TARGET_XFER_E_IO;
1668 }
1669 if (record_full_arch_list_add_end ())
1670 {
1671 record_full_list_release (record_full_arch_list_tail);
1672 if (record_debug)
1673 fprintf_unfiltered (gdb_stdlog,
1674 "Process record: failed to record "
1675 "execution log.");
1676 return TARGET_XFER_E_IO;
1677 }
1678 record_full_list->next = record_full_arch_list_head;
1679 record_full_arch_list_head->prev = record_full_list;
1680 record_full_list = record_full_arch_list_tail;
1681
1682 if (record_full_insn_num == record_full_insn_max_num)
1683 record_full_list_release_first ();
1684 else
1685 record_full_insn_num++;
1686 }
1687
1688 return this->beneath ()->xfer_partial (object, annex, readbuf, writebuf,
1689 offset, len, xfered_len);
1690 }
1691
1692 /* This structure represents a breakpoint inserted while the record
1693 target is active. We use this to know when to install/remove
1694 breakpoints in/from the target beneath. For example, a breakpoint
1695 may be inserted while recording, but removed when not replaying nor
1696 recording. In that case, the breakpoint had not been inserted on
1697 the target beneath, so we should not try to remove it there. */
1698
1699 struct record_full_breakpoint
1700 {
1701 record_full_breakpoint (struct address_space *address_space_,
1702 CORE_ADDR addr_,
1703 bool in_target_beneath_)
1704 : address_space (address_space_),
1705 addr (addr_),
1706 in_target_beneath (in_target_beneath_)
1707 {
1708 }
1709
1710 /* The address and address space the breakpoint was set at. */
1711 struct address_space *address_space;
1712 CORE_ADDR addr;
1713
1714 /* True when the breakpoint has been also installed in the target
1715 beneath. This will be false for breakpoints set during replay or
1716 when recording. */
1717 bool in_target_beneath;
1718 };
1719
1720 /* The list of breakpoints inserted while the record target is
1721 active. */
1722 static std::vector<record_full_breakpoint> record_full_breakpoints;
1723
1724 static void
1725 record_full_sync_record_breakpoints (struct bp_location *loc, void *data)
1726 {
1727 if (loc->loc_type != bp_loc_software_breakpoint)
1728 return;
1729
1730 if (loc->inserted)
1731 {
1732 record_full_breakpoints.emplace_back
1733 (loc->target_info.placed_address_space,
1734 loc->target_info.placed_address,
1735 1);
1736 }
1737 }
1738
1739 /* Sync existing breakpoints to record_full_breakpoints. */
1740
1741 static void
1742 record_full_init_record_breakpoints (void)
1743 {
1744 record_full_breakpoints.clear ();
1745
1746 iterate_over_bp_locations (record_full_sync_record_breakpoints);
1747 }
1748
1749 /* Behavior is conditional on RECORD_FULL_IS_REPLAY. We will not actually
1750 insert or remove breakpoints in the real target when replaying, nor
1751 when recording. */
1752
1753 int
1754 record_full_target::insert_breakpoint (struct gdbarch *gdbarch,
1755 struct bp_target_info *bp_tgt)
1756 {
1757 bool in_target_beneath = false;
1758
1759 if (!RECORD_FULL_IS_REPLAY)
1760 {
1761 /* When recording, we currently always single-step, so we don't
1762 really need to install regular breakpoints in the inferior.
1763 However, we do have to insert software single-step
1764 breakpoints, in case the target can't hardware step. To keep
1765 things simple, we always insert. */
1766
1767 scoped_restore restore_operation_disable
1768 = record_full_gdb_operation_disable_set ();
1769
1770 int ret = this->beneath ()->insert_breakpoint (gdbarch, bp_tgt);
1771 if (ret != 0)
1772 return ret;
1773
1774 in_target_beneath = true;
1775 }
1776
1777 /* Use the existing entries if found in order to avoid duplication
1778 in record_full_breakpoints. */
1779
1780 for (const record_full_breakpoint &bp : record_full_breakpoints)
1781 {
1782 if (bp.addr == bp_tgt->placed_address
1783 && bp.address_space == bp_tgt->placed_address_space)
1784 {
1785 gdb_assert (bp.in_target_beneath == in_target_beneath);
1786 return 0;
1787 }
1788 }
1789
1790 record_full_breakpoints.emplace_back (bp_tgt->placed_address_space,
1791 bp_tgt->placed_address,
1792 in_target_beneath);
1793 return 0;
1794 }
1795
1796 /* "remove_breakpoint" method for process record target. */
1797
1798 int
1799 record_full_target::remove_breakpoint (struct gdbarch *gdbarch,
1800 struct bp_target_info *bp_tgt,
1801 enum remove_bp_reason reason)
1802 {
1803 for (auto iter = record_full_breakpoints.begin ();
1804 iter != record_full_breakpoints.end ();
1805 ++iter)
1806 {
1807 struct record_full_breakpoint &bp = *iter;
1808
1809 if (bp.addr == bp_tgt->placed_address
1810 && bp.address_space == bp_tgt->placed_address_space)
1811 {
1812 if (bp.in_target_beneath)
1813 {
1814 scoped_restore restore_operation_disable
1815 = record_full_gdb_operation_disable_set ();
1816
1817 int ret = this->beneath ()->remove_breakpoint (gdbarch, bp_tgt,
1818 reason);
1819 if (ret != 0)
1820 return ret;
1821 }
1822
1823 if (reason == REMOVE_BREAKPOINT)
1824 unordered_remove (record_full_breakpoints, iter);
1825 return 0;
1826 }
1827 }
1828
1829 gdb_assert_not_reached ("removing unknown breakpoint");
1830 }
1831
1832 /* "can_execute_reverse" method for process record target. */
1833
1834 bool
1835 record_full_base_target::can_execute_reverse ()
1836 {
1837 return true;
1838 }
1839
1840 /* "get_bookmark" method for process record and prec over core. */
1841
1842 gdb_byte *
1843 record_full_base_target::get_bookmark (const char *args, int from_tty)
1844 {
1845 char *ret = NULL;
1846
1847 /* Return stringified form of instruction count. */
1848 if (record_full_list && record_full_list->type == record_full_end)
1849 ret = xstrdup (pulongest (record_full_list->u.end.insn_num));
1850
1851 if (record_debug)
1852 {
1853 if (ret)
1854 fprintf_unfiltered (gdb_stdlog,
1855 "record_full_get_bookmark returns %s\n", ret);
1856 else
1857 fprintf_unfiltered (gdb_stdlog,
1858 "record_full_get_bookmark returns NULL\n");
1859 }
1860 return (gdb_byte *) ret;
1861 }
1862
1863 /* "goto_bookmark" method for process record and prec over core. */
1864
1865 void
1866 record_full_base_target::goto_bookmark (const gdb_byte *raw_bookmark,
1867 int from_tty)
1868 {
1869 const char *bookmark = (const char *) raw_bookmark;
1870
1871 if (record_debug)
1872 fprintf_unfiltered (gdb_stdlog,
1873 "record_full_goto_bookmark receives %s\n", bookmark);
1874
1875 std::string name_holder;
1876 if (bookmark[0] == '\'' || bookmark[0] == '\"')
1877 {
1878 if (bookmark[strlen (bookmark) - 1] != bookmark[0])
1879 error (_("Unbalanced quotes: %s"), bookmark);
1880
1881 name_holder = std::string (bookmark + 1, strlen (bookmark) - 2);
1882 bookmark = name_holder.c_str ();
1883 }
1884
1885 record_goto (bookmark);
1886 }
1887
1888 enum exec_direction_kind
1889 record_full_base_target::execution_direction ()
1890 {
1891 return record_full_execution_dir;
1892 }
1893
1894 /* The record_method method of target record-full. */
1895
1896 enum record_method
1897 record_full_base_target::record_method (ptid_t ptid)
1898 {
1899 return RECORD_METHOD_FULL;
1900 }
1901
1902 void
1903 record_full_base_target::info_record ()
1904 {
1905 struct record_full_entry *p;
1906
1907 if (RECORD_FULL_IS_REPLAY)
1908 printf_filtered (_("Replay mode:\n"));
1909 else
1910 printf_filtered (_("Record mode:\n"));
1911
1912 /* Find entry for first actual instruction in the log. */
1913 for (p = record_full_first.next;
1914 p != NULL && p->type != record_full_end;
1915 p = p->next)
1916 ;
1917
1918 /* Do we have a log at all? */
1919 if (p != NULL && p->type == record_full_end)
1920 {
1921 /* Display instruction number for first instruction in the log. */
1922 printf_filtered (_("Lowest recorded instruction number is %s.\n"),
1923 pulongest (p->u.end.insn_num));
1924
1925 /* If in replay mode, display where we are in the log. */
1926 if (RECORD_FULL_IS_REPLAY)
1927 printf_filtered (_("Current instruction number is %s.\n"),
1928 pulongest (record_full_list->u.end.insn_num));
1929
1930 /* Display instruction number for last instruction in the log. */
1931 printf_filtered (_("Highest recorded instruction number is %s.\n"),
1932 pulongest (record_full_insn_count));
1933
1934 /* Display log count. */
1935 printf_filtered (_("Log contains %u instructions.\n"),
1936 record_full_insn_num);
1937 }
1938 else
1939 printf_filtered (_("No instructions have been logged.\n"));
1940
1941 /* Display max log size. */
1942 printf_filtered (_("Max logged instructions is %u.\n"),
1943 record_full_insn_max_num);
1944 }
1945
1946 bool
1947 record_full_base_target::supports_delete_record ()
1948 {
1949 return true;
1950 }
1951
1952 /* The "delete_record" target method. */
1953
1954 void
1955 record_full_base_target::delete_record ()
1956 {
1957 record_full_list_release_following (record_full_list);
1958 }
1959
1960 /* The "record_is_replaying" target method. */
1961
1962 bool
1963 record_full_base_target::record_is_replaying (ptid_t ptid)
1964 {
1965 return RECORD_FULL_IS_REPLAY;
1966 }
1967
1968 /* The "record_will_replay" target method. */
1969
1970 bool
1971 record_full_base_target::record_will_replay (ptid_t ptid, int dir)
1972 {
1973 /* We can currently only record when executing forwards. Should we be able
1974 to record when executing backwards on targets that support reverse
1975 execution, this needs to be changed. */
1976
1977 return RECORD_FULL_IS_REPLAY || dir == EXEC_REVERSE;
1978 }
1979
1980 /* Go to a specific entry. */
1981
1982 static void
1983 record_full_goto_entry (struct record_full_entry *p)
1984 {
1985 if (p == NULL)
1986 error (_("Target insn not found."));
1987 else if (p == record_full_list)
1988 error (_("Already at target insn."));
1989 else if (p->u.end.insn_num > record_full_list->u.end.insn_num)
1990 {
1991 printf_filtered (_("Go forward to insn number %s\n"),
1992 pulongest (p->u.end.insn_num));
1993 record_full_goto_insn (p, EXEC_FORWARD);
1994 }
1995 else
1996 {
1997 printf_filtered (_("Go backward to insn number %s\n"),
1998 pulongest (p->u.end.insn_num));
1999 record_full_goto_insn (p, EXEC_REVERSE);
2000 }
2001
2002 registers_changed ();
2003 reinit_frame_cache ();
2004 inferior_thread ()->suspend.stop_pc
2005 = regcache_read_pc (get_current_regcache ());
2006 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
2007 }
2008
2009 /* The "goto_record_begin" target method. */
2010
2011 void
2012 record_full_base_target::goto_record_begin ()
2013 {
2014 struct record_full_entry *p = NULL;
2015
2016 for (p = &record_full_first; p != NULL; p = p->next)
2017 if (p->type == record_full_end)
2018 break;
2019
2020 record_full_goto_entry (p);
2021 }
2022
2023 /* The "goto_record_end" target method. */
2024
2025 void
2026 record_full_base_target::goto_record_end ()
2027 {
2028 struct record_full_entry *p = NULL;
2029
2030 for (p = record_full_list; p->next != NULL; p = p->next)
2031 ;
2032 for (; p!= NULL; p = p->prev)
2033 if (p->type == record_full_end)
2034 break;
2035
2036 record_full_goto_entry (p);
2037 }
2038
2039 /* The "goto_record" target method. */
2040
2041 void
2042 record_full_base_target::goto_record (ULONGEST target_insn)
2043 {
2044 struct record_full_entry *p = NULL;
2045
2046 for (p = &record_full_first; p != NULL; p = p->next)
2047 if (p->type == record_full_end && p->u.end.insn_num == target_insn)
2048 break;
2049
2050 record_full_goto_entry (p);
2051 }
2052
2053 /* The "record_stop_replaying" target method. */
2054
2055 void
2056 record_full_base_target::record_stop_replaying ()
2057 {
2058 goto_record_end ();
2059 }
2060
2061 /* "resume" method for prec over corefile. */
2062
2063 void
2064 record_full_core_target::resume (ptid_t ptid, int step,
2065 enum gdb_signal signal)
2066 {
2067 record_full_resume_step = step;
2068 record_full_resumed = 1;
2069 record_full_execution_dir = ::execution_direction;
2070
2071 /* We are about to start executing the inferior (or simulate it),
2072 let's register it with the event loop. */
2073 if (target_can_async_p ())
2074 target_async (1);
2075 }
2076
2077 /* "kill" method for prec over corefile. */
2078
2079 void
2080 record_full_core_target::kill ()
2081 {
2082 if (record_debug)
2083 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_core_kill\n");
2084
2085 unpush_target (this);
2086 }
2087
2088 /* "fetch_registers" method for prec over corefile. */
2089
2090 void
2091 record_full_core_target::fetch_registers (struct regcache *regcache,
2092 int regno)
2093 {
2094 if (regno < 0)
2095 {
2096 int num = gdbarch_num_regs (regcache->arch ());
2097 int i;
2098
2099 for (i = 0; i < num; i ++)
2100 regcache->raw_supply (i, *record_full_core_regbuf);
2101 }
2102 else
2103 regcache->raw_supply (regno, *record_full_core_regbuf);
2104 }
2105
2106 /* "prepare_to_store" method for prec over corefile. */
2107
2108 void
2109 record_full_core_target::prepare_to_store (struct regcache *regcache)
2110 {
2111 }
2112
2113 /* "store_registers" method for prec over corefile. */
2114
2115 void
2116 record_full_core_target::store_registers (struct regcache *regcache,
2117 int regno)
2118 {
2119 if (record_full_gdb_operation_disable)
2120 record_full_core_regbuf->raw_supply (regno, *regcache);
2121 else
2122 error (_("You can't do that without a process to debug."));
2123 }
2124
2125 /* "xfer_partial" method for prec over corefile. */
2126
2127 enum target_xfer_status
2128 record_full_core_target::xfer_partial (enum target_object object,
2129 const char *annex, gdb_byte *readbuf,
2130 const gdb_byte *writebuf, ULONGEST offset,
2131 ULONGEST len, ULONGEST *xfered_len)
2132 {
2133 if (object == TARGET_OBJECT_MEMORY)
2134 {
2135 if (record_full_gdb_operation_disable || !writebuf)
2136 {
2137 struct target_section *p;
2138
2139 for (p = record_full_core_start; p < record_full_core_end; p++)
2140 {
2141 if (offset >= p->addr)
2142 {
2143 struct record_full_core_buf_entry *entry;
2144 ULONGEST sec_offset;
2145
2146 if (offset >= p->endaddr)
2147 continue;
2148
2149 if (offset + len > p->endaddr)
2150 len = p->endaddr - offset;
2151
2152 sec_offset = offset - p->addr;
2153
2154 /* Read readbuf or write writebuf p, offset, len. */
2155 /* Check flags. */
2156 if (p->the_bfd_section->flags & SEC_CONSTRUCTOR
2157 || (p->the_bfd_section->flags & SEC_HAS_CONTENTS) == 0)
2158 {
2159 if (readbuf)
2160 memset (readbuf, 0, len);
2161
2162 *xfered_len = len;
2163 return TARGET_XFER_OK;
2164 }
2165 /* Get record_full_core_buf_entry. */
2166 for (entry = record_full_core_buf_list; entry;
2167 entry = entry->prev)
2168 if (entry->p == p)
2169 break;
2170 if (writebuf)
2171 {
2172 if (!entry)
2173 {
2174 /* Add a new entry. */
2175 entry = XNEW (struct record_full_core_buf_entry);
2176 entry->p = p;
2177 if (!bfd_malloc_and_get_section
2178 (p->the_bfd_section->owner,
2179 p->the_bfd_section,
2180 &entry->buf))
2181 {
2182 xfree (entry);
2183 return TARGET_XFER_EOF;
2184 }
2185 entry->prev = record_full_core_buf_list;
2186 record_full_core_buf_list = entry;
2187 }
2188
2189 memcpy (entry->buf + sec_offset, writebuf,
2190 (size_t) len);
2191 }
2192 else
2193 {
2194 if (!entry)
2195 return this->beneath ()->xfer_partial (object, annex,
2196 readbuf, writebuf,
2197 offset, len,
2198 xfered_len);
2199
2200 memcpy (readbuf, entry->buf + sec_offset,
2201 (size_t) len);
2202 }
2203
2204 *xfered_len = len;
2205 return TARGET_XFER_OK;
2206 }
2207 }
2208
2209 return TARGET_XFER_E_IO;
2210 }
2211 else
2212 error (_("You can't do that without a process to debug."));
2213 }
2214
2215 return this->beneath ()->xfer_partial (object, annex,
2216 readbuf, writebuf, offset, len,
2217 xfered_len);
2218 }
2219
2220 /* "insert_breakpoint" method for prec over corefile. */
2221
2222 int
2223 record_full_core_target::insert_breakpoint (struct gdbarch *gdbarch,
2224 struct bp_target_info *bp_tgt)
2225 {
2226 return 0;
2227 }
2228
2229 /* "remove_breakpoint" method for prec over corefile. */
2230
2231 int
2232 record_full_core_target::remove_breakpoint (struct gdbarch *gdbarch,
2233 struct bp_target_info *bp_tgt,
2234 enum remove_bp_reason reason)
2235 {
2236 return 0;
2237 }
2238
2239 /* "has_execution" method for prec over corefile. */
2240
2241 bool
2242 record_full_core_target::has_execution (ptid_t the_ptid)
2243 {
2244 return true;
2245 }
2246
2247 /* Record log save-file format
2248 Version 1 (never released)
2249
2250 Header:
2251 4 bytes: magic number htonl(0x20090829).
2252 NOTE: be sure to change whenever this file format changes!
2253
2254 Records:
2255 record_full_end:
2256 1 byte: record type (record_full_end, see enum record_full_type).
2257 record_full_reg:
2258 1 byte: record type (record_full_reg, see enum record_full_type).
2259 8 bytes: register id (network byte order).
2260 MAX_REGISTER_SIZE bytes: register value.
2261 record_full_mem:
2262 1 byte: record type (record_full_mem, see enum record_full_type).
2263 8 bytes: memory length (network byte order).
2264 8 bytes: memory address (network byte order).
2265 n bytes: memory value (n == memory length).
2266
2267 Version 2
2268 4 bytes: magic number netorder32(0x20091016).
2269 NOTE: be sure to change whenever this file format changes!
2270
2271 Records:
2272 record_full_end:
2273 1 byte: record type (record_full_end, see enum record_full_type).
2274 4 bytes: signal
2275 4 bytes: instruction count
2276 record_full_reg:
2277 1 byte: record type (record_full_reg, see enum record_full_type).
2278 4 bytes: register id (network byte order).
2279 n bytes: register value (n == actual register size).
2280 (eg. 4 bytes for x86 general registers).
2281 record_full_mem:
2282 1 byte: record type (record_full_mem, see enum record_full_type).
2283 4 bytes: memory length (network byte order).
2284 8 bytes: memory address (network byte order).
2285 n bytes: memory value (n == memory length).
2286
2287 */
2288
2289 /* bfdcore_read -- read bytes from a core file section. */
2290
2291 static inline void
2292 bfdcore_read (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2293 {
2294 int ret = bfd_get_section_contents (obfd, osec, buf, *offset, len);
2295
2296 if (ret)
2297 *offset += len;
2298 else
2299 error (_("Failed to read %d bytes from core file %s ('%s')."),
2300 len, bfd_get_filename (obfd),
2301 bfd_errmsg (bfd_get_error ()));
2302 }
2303
2304 static inline uint64_t
2305 netorder64 (uint64_t input)
2306 {
2307 uint64_t ret;
2308
2309 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2310 BFD_ENDIAN_BIG, input);
2311 return ret;
2312 }
2313
2314 static inline uint32_t
2315 netorder32 (uint32_t input)
2316 {
2317 uint32_t ret;
2318
2319 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2320 BFD_ENDIAN_BIG, input);
2321 return ret;
2322 }
2323
2324 /* Restore the execution log from a core_bfd file. */
2325 static void
2326 record_full_restore (void)
2327 {
2328 uint32_t magic;
2329 struct record_full_entry *rec;
2330 asection *osec;
2331 uint32_t osec_size;
2332 int bfd_offset = 0;
2333 struct regcache *regcache;
2334
2335 /* We restore the execution log from the open core bfd,
2336 if there is one. */
2337 if (core_bfd == NULL)
2338 return;
2339
2340 /* "record_full_restore" can only be called when record list is empty. */
2341 gdb_assert (record_full_first.next == NULL);
2342
2343 if (record_debug)
2344 fprintf_unfiltered (gdb_stdlog, "Restoring recording from core file.\n");
2345
2346 /* Now need to find our special note section. */
2347 osec = bfd_get_section_by_name (core_bfd, "null0");
2348 if (record_debug)
2349 fprintf_unfiltered (gdb_stdlog, "Find precord section %s.\n",
2350 osec ? "succeeded" : "failed");
2351 if (osec == NULL)
2352 return;
2353 osec_size = bfd_section_size (osec);
2354 if (record_debug)
2355 fprintf_unfiltered (gdb_stdlog, "%s", bfd_section_name (osec));
2356
2357 /* Check the magic code. */
2358 bfdcore_read (core_bfd, osec, &magic, sizeof (magic), &bfd_offset);
2359 if (magic != RECORD_FULL_FILE_MAGIC)
2360 error (_("Version mis-match or file format error in core file %s."),
2361 bfd_get_filename (core_bfd));
2362 if (record_debug)
2363 fprintf_unfiltered (gdb_stdlog,
2364 " Reading 4-byte magic cookie "
2365 "RECORD_FULL_FILE_MAGIC (0x%s)\n",
2366 phex_nz (netorder32 (magic), 4));
2367
2368 /* Restore the entries in recfd into record_full_arch_list_head and
2369 record_full_arch_list_tail. */
2370 record_full_arch_list_head = NULL;
2371 record_full_arch_list_tail = NULL;
2372 record_full_insn_num = 0;
2373
2374 try
2375 {
2376 regcache = get_current_regcache ();
2377
2378 while (1)
2379 {
2380 uint8_t rectype;
2381 uint32_t regnum, len, signal, count;
2382 uint64_t addr;
2383
2384 /* We are finished when offset reaches osec_size. */
2385 if (bfd_offset >= osec_size)
2386 break;
2387 bfdcore_read (core_bfd, osec, &rectype, sizeof (rectype), &bfd_offset);
2388
2389 switch (rectype)
2390 {
2391 case record_full_reg: /* reg */
2392 /* Get register number to regnum. */
2393 bfdcore_read (core_bfd, osec, &regnum,
2394 sizeof (regnum), &bfd_offset);
2395 regnum = netorder32 (regnum);
2396
2397 rec = record_full_reg_alloc (regcache, regnum);
2398
2399 /* Get val. */
2400 bfdcore_read (core_bfd, osec, record_full_get_loc (rec),
2401 rec->u.reg.len, &bfd_offset);
2402
2403 if (record_debug)
2404 fprintf_unfiltered (gdb_stdlog,
2405 " Reading register %d (1 "
2406 "plus %lu plus %d bytes)\n",
2407 rec->u.reg.num,
2408 (unsigned long) sizeof (regnum),
2409 rec->u.reg.len);
2410 break;
2411
2412 case record_full_mem: /* mem */
2413 /* Get len. */
2414 bfdcore_read (core_bfd, osec, &len,
2415 sizeof (len), &bfd_offset);
2416 len = netorder32 (len);
2417
2418 /* Get addr. */
2419 bfdcore_read (core_bfd, osec, &addr,
2420 sizeof (addr), &bfd_offset);
2421 addr = netorder64 (addr);
2422
2423 rec = record_full_mem_alloc (addr, len);
2424
2425 /* Get val. */
2426 bfdcore_read (core_bfd, osec, record_full_get_loc (rec),
2427 rec->u.mem.len, &bfd_offset);
2428
2429 if (record_debug)
2430 fprintf_unfiltered (gdb_stdlog,
2431 " Reading memory %s (1 plus "
2432 "%lu plus %lu plus %d bytes)\n",
2433 paddress (get_current_arch (),
2434 rec->u.mem.addr),
2435 (unsigned long) sizeof (addr),
2436 (unsigned long) sizeof (len),
2437 rec->u.mem.len);
2438 break;
2439
2440 case record_full_end: /* end */
2441 rec = record_full_end_alloc ();
2442 record_full_insn_num ++;
2443
2444 /* Get signal value. */
2445 bfdcore_read (core_bfd, osec, &signal,
2446 sizeof (signal), &bfd_offset);
2447 signal = netorder32 (signal);
2448 rec->u.end.sigval = (enum gdb_signal) signal;
2449
2450 /* Get insn count. */
2451 bfdcore_read (core_bfd, osec, &count,
2452 sizeof (count), &bfd_offset);
2453 count = netorder32 (count);
2454 rec->u.end.insn_num = count;
2455 record_full_insn_count = count + 1;
2456 if (record_debug)
2457 fprintf_unfiltered (gdb_stdlog,
2458 " Reading record_full_end (1 + "
2459 "%lu + %lu bytes), offset == %s\n",
2460 (unsigned long) sizeof (signal),
2461 (unsigned long) sizeof (count),
2462 paddress (get_current_arch (),
2463 bfd_offset));
2464 break;
2465
2466 default:
2467 error (_("Bad entry type in core file %s."),
2468 bfd_get_filename (core_bfd));
2469 break;
2470 }
2471
2472 /* Add rec to record arch list. */
2473 record_full_arch_list_add (rec);
2474 }
2475 }
2476 catch (const gdb_exception &ex)
2477 {
2478 record_full_list_release (record_full_arch_list_tail);
2479 throw;
2480 }
2481
2482 /* Add record_full_arch_list_head to the end of record list. */
2483 record_full_first.next = record_full_arch_list_head;
2484 record_full_arch_list_head->prev = &record_full_first;
2485 record_full_arch_list_tail->next = NULL;
2486 record_full_list = &record_full_first;
2487
2488 /* Update record_full_insn_max_num. */
2489 if (record_full_insn_num > record_full_insn_max_num)
2490 {
2491 record_full_insn_max_num = record_full_insn_num;
2492 warning (_("Auto increase record/replay buffer limit to %u."),
2493 record_full_insn_max_num);
2494 }
2495
2496 /* Succeeded. */
2497 printf_filtered (_("Restored records from core file %s.\n"),
2498 bfd_get_filename (core_bfd));
2499
2500 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
2501 }
2502
2503 /* bfdcore_write -- write bytes into a core file section. */
2504
2505 static inline void
2506 bfdcore_write (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2507 {
2508 int ret = bfd_set_section_contents (obfd, osec, buf, *offset, len);
2509
2510 if (ret)
2511 *offset += len;
2512 else
2513 error (_("Failed to write %d bytes to core file %s ('%s')."),
2514 len, bfd_get_filename (obfd),
2515 bfd_errmsg (bfd_get_error ()));
2516 }
2517
2518 /* Restore the execution log from a file. We use a modified elf
2519 corefile format, with an extra section for our data. */
2520
2521 static void
2522 cmd_record_full_restore (const char *args, int from_tty)
2523 {
2524 core_file_command (args, from_tty);
2525 record_full_open (args, from_tty);
2526 }
2527
2528 /* Save the execution log to a file. We use a modified elf corefile
2529 format, with an extra section for our data. */
2530
2531 void
2532 record_full_base_target::save_record (const char *recfilename)
2533 {
2534 struct record_full_entry *cur_record_full_list;
2535 uint32_t magic;
2536 struct regcache *regcache;
2537 struct gdbarch *gdbarch;
2538 int save_size = 0;
2539 asection *osec = NULL;
2540 int bfd_offset = 0;
2541
2542 /* Open the save file. */
2543 if (record_debug)
2544 fprintf_unfiltered (gdb_stdlog, "Saving execution log to core file '%s'\n",
2545 recfilename);
2546
2547 /* Open the output file. */
2548 gdb_bfd_ref_ptr obfd (create_gcore_bfd (recfilename));
2549
2550 /* Arrange to remove the output file on failure. */
2551 gdb::unlinker unlink_file (recfilename);
2552
2553 /* Save the current record entry to "cur_record_full_list". */
2554 cur_record_full_list = record_full_list;
2555
2556 /* Get the values of regcache and gdbarch. */
2557 regcache = get_current_regcache ();
2558 gdbarch = regcache->arch ();
2559
2560 /* Disable the GDB operation record. */
2561 scoped_restore restore_operation_disable
2562 = record_full_gdb_operation_disable_set ();
2563
2564 /* Reverse execute to the begin of record list. */
2565 while (1)
2566 {
2567 /* Check for beginning and end of log. */
2568 if (record_full_list == &record_full_first)
2569 break;
2570
2571 record_full_exec_insn (regcache, gdbarch, record_full_list);
2572
2573 if (record_full_list->prev)
2574 record_full_list = record_full_list->prev;
2575 }
2576
2577 /* Compute the size needed for the extra bfd section. */
2578 save_size = 4; /* magic cookie */
2579 for (record_full_list = record_full_first.next; record_full_list;
2580 record_full_list = record_full_list->next)
2581 switch (record_full_list->type)
2582 {
2583 case record_full_end:
2584 save_size += 1 + 4 + 4;
2585 break;
2586 case record_full_reg:
2587 save_size += 1 + 4 + record_full_list->u.reg.len;
2588 break;
2589 case record_full_mem:
2590 save_size += 1 + 4 + 8 + record_full_list->u.mem.len;
2591 break;
2592 }
2593
2594 /* Make the new bfd section. */
2595 osec = bfd_make_section_anyway_with_flags (obfd.get (), "precord",
2596 SEC_HAS_CONTENTS
2597 | SEC_READONLY);
2598 if (osec == NULL)
2599 error (_("Failed to create 'precord' section for corefile %s: %s"),
2600 recfilename,
2601 bfd_errmsg (bfd_get_error ()));
2602 bfd_set_section_size (osec, save_size);
2603 bfd_set_section_vma (osec, 0);
2604 bfd_set_section_alignment (osec, 0);
2605
2606 /* Save corefile state. */
2607 write_gcore_file (obfd.get ());
2608
2609 /* Write out the record log. */
2610 /* Write the magic code. */
2611 magic = RECORD_FULL_FILE_MAGIC;
2612 if (record_debug)
2613 fprintf_unfiltered (gdb_stdlog,
2614 " Writing 4-byte magic cookie "
2615 "RECORD_FULL_FILE_MAGIC (0x%s)\n",
2616 phex_nz (magic, 4));
2617 bfdcore_write (obfd.get (), osec, &magic, sizeof (magic), &bfd_offset);
2618
2619 /* Save the entries to recfd and forward execute to the end of
2620 record list. */
2621 record_full_list = &record_full_first;
2622 while (1)
2623 {
2624 /* Save entry. */
2625 if (record_full_list != &record_full_first)
2626 {
2627 uint8_t type;
2628 uint32_t regnum, len, signal, count;
2629 uint64_t addr;
2630
2631 type = record_full_list->type;
2632 bfdcore_write (obfd.get (), osec, &type, sizeof (type), &bfd_offset);
2633
2634 switch (record_full_list->type)
2635 {
2636 case record_full_reg: /* reg */
2637 if (record_debug)
2638 fprintf_unfiltered (gdb_stdlog,
2639 " Writing register %d (1 "
2640 "plus %lu plus %d bytes)\n",
2641 record_full_list->u.reg.num,
2642 (unsigned long) sizeof (regnum),
2643 record_full_list->u.reg.len);
2644
2645 /* Write regnum. */
2646 regnum = netorder32 (record_full_list->u.reg.num);
2647 bfdcore_write (obfd.get (), osec, &regnum,
2648 sizeof (regnum), &bfd_offset);
2649
2650 /* Write regval. */
2651 bfdcore_write (obfd.get (), osec,
2652 record_full_get_loc (record_full_list),
2653 record_full_list->u.reg.len, &bfd_offset);
2654 break;
2655
2656 case record_full_mem: /* mem */
2657 if (record_debug)
2658 fprintf_unfiltered (gdb_stdlog,
2659 " Writing memory %s (1 plus "
2660 "%lu plus %lu plus %d bytes)\n",
2661 paddress (gdbarch,
2662 record_full_list->u.mem.addr),
2663 (unsigned long) sizeof (addr),
2664 (unsigned long) sizeof (len),
2665 record_full_list->u.mem.len);
2666
2667 /* Write memlen. */
2668 len = netorder32 (record_full_list->u.mem.len);
2669 bfdcore_write (obfd.get (), osec, &len, sizeof (len),
2670 &bfd_offset);
2671
2672 /* Write memaddr. */
2673 addr = netorder64 (record_full_list->u.mem.addr);
2674 bfdcore_write (obfd.get (), osec, &addr,
2675 sizeof (addr), &bfd_offset);
2676
2677 /* Write memval. */
2678 bfdcore_write (obfd.get (), osec,
2679 record_full_get_loc (record_full_list),
2680 record_full_list->u.mem.len, &bfd_offset);
2681 break;
2682
2683 case record_full_end:
2684 if (record_debug)
2685 fprintf_unfiltered (gdb_stdlog,
2686 " Writing record_full_end (1 + "
2687 "%lu + %lu bytes)\n",
2688 (unsigned long) sizeof (signal),
2689 (unsigned long) sizeof (count));
2690 /* Write signal value. */
2691 signal = netorder32 (record_full_list->u.end.sigval);
2692 bfdcore_write (obfd.get (), osec, &signal,
2693 sizeof (signal), &bfd_offset);
2694
2695 /* Write insn count. */
2696 count = netorder32 (record_full_list->u.end.insn_num);
2697 bfdcore_write (obfd.get (), osec, &count,
2698 sizeof (count), &bfd_offset);
2699 break;
2700 }
2701 }
2702
2703 /* Execute entry. */
2704 record_full_exec_insn (regcache, gdbarch, record_full_list);
2705
2706 if (record_full_list->next)
2707 record_full_list = record_full_list->next;
2708 else
2709 break;
2710 }
2711
2712 /* Reverse execute to cur_record_full_list. */
2713 while (1)
2714 {
2715 /* Check for beginning and end of log. */
2716 if (record_full_list == cur_record_full_list)
2717 break;
2718
2719 record_full_exec_insn (regcache, gdbarch, record_full_list);
2720
2721 if (record_full_list->prev)
2722 record_full_list = record_full_list->prev;
2723 }
2724
2725 unlink_file.keep ();
2726
2727 /* Succeeded. */
2728 printf_filtered (_("Saved core file %s with execution log.\n"),
2729 recfilename);
2730 }
2731
2732 /* record_full_goto_insn -- rewind the record log (forward or backward,
2733 depending on DIR) to the given entry, changing the program state
2734 correspondingly. */
2735
2736 static void
2737 record_full_goto_insn (struct record_full_entry *entry,
2738 enum exec_direction_kind dir)
2739 {
2740 scoped_restore restore_operation_disable
2741 = record_full_gdb_operation_disable_set ();
2742 struct regcache *regcache = get_current_regcache ();
2743 struct gdbarch *gdbarch = regcache->arch ();
2744
2745 /* Assume everything is valid: we will hit the entry,
2746 and we will not hit the end of the recording. */
2747
2748 if (dir == EXEC_FORWARD)
2749 record_full_list = record_full_list->next;
2750
2751 do
2752 {
2753 record_full_exec_insn (regcache, gdbarch, record_full_list);
2754 if (dir == EXEC_REVERSE)
2755 record_full_list = record_full_list->prev;
2756 else
2757 record_full_list = record_full_list->next;
2758 } while (record_full_list != entry);
2759 }
2760
2761 /* Alias for "target record-full". */
2762
2763 static void
2764 cmd_record_full_start (const char *args, int from_tty)
2765 {
2766 execute_command ("target record-full", from_tty);
2767 }
2768
2769 static void
2770 set_record_full_insn_max_num (const char *args, int from_tty,
2771 struct cmd_list_element *c)
2772 {
2773 if (record_full_insn_num > record_full_insn_max_num)
2774 {
2775 /* Count down record_full_insn_num while releasing records from list. */
2776 while (record_full_insn_num > record_full_insn_max_num)
2777 {
2778 record_full_list_release_first ();
2779 record_full_insn_num--;
2780 }
2781 }
2782 }
2783
2784 /* The "set record full" command. */
2785
2786 static void
2787 set_record_full_command (const char *args, int from_tty)
2788 {
2789 printf_unfiltered (_("\"set record full\" must be followed "
2790 "by an appropriate subcommand.\n"));
2791 help_list (set_record_full_cmdlist, "set record full ", all_commands,
2792 gdb_stdout);
2793 }
2794
2795 /* The "show record full" command. */
2796
2797 static void
2798 show_record_full_command (const char *args, int from_tty)
2799 {
2800 cmd_show_list (show_record_full_cmdlist, from_tty, "");
2801 }
2802
2803 void
2804 _initialize_record_full (void)
2805 {
2806 struct cmd_list_element *c;
2807
2808 /* Init record_full_first. */
2809 record_full_first.prev = NULL;
2810 record_full_first.next = NULL;
2811 record_full_first.type = record_full_end;
2812
2813 add_target (record_full_target_info, record_full_open);
2814 add_deprecated_target_alias (record_full_target_info, "record");
2815 add_target (record_full_core_target_info, record_full_open);
2816
2817 add_prefix_cmd ("full", class_obscure, cmd_record_full_start,
2818 _("Start full execution recording."), &record_full_cmdlist,
2819 "record full ", 0, &record_cmdlist);
2820
2821 c = add_cmd ("restore", class_obscure, cmd_record_full_restore,
2822 _("Restore the execution log from a file.\n\
2823 Argument is filename. File must be created with 'record save'."),
2824 &record_full_cmdlist);
2825 set_cmd_completer (c, filename_completer);
2826
2827 /* Deprecate the old version without "full" prefix. */
2828 c = add_alias_cmd ("restore", "full restore", class_obscure, 1,
2829 &record_cmdlist);
2830 set_cmd_completer (c, filename_completer);
2831 deprecate_cmd (c, "record full restore");
2832
2833 add_prefix_cmd ("full", class_support, set_record_full_command,
2834 _("Set record options."), &set_record_full_cmdlist,
2835 "set record full ", 0, &set_record_cmdlist);
2836
2837 add_prefix_cmd ("full", class_support, show_record_full_command,
2838 _("Show record options."), &show_record_full_cmdlist,
2839 "show record full ", 0, &show_record_cmdlist);
2840
2841 /* Record instructions number limit command. */
2842 add_setshow_boolean_cmd ("stop-at-limit", no_class,
2843 &record_full_stop_at_limit, _("\
2844 Set whether record/replay stops when record/replay buffer becomes full."), _("\
2845 Show whether record/replay stops when record/replay buffer becomes full."),
2846 _("Default is ON.\n\
2847 When ON, if the record/replay buffer becomes full, ask user what to do.\n\
2848 When OFF, if the record/replay buffer becomes full,\n\
2849 delete the oldest recorded instruction to make room for each new one."),
2850 NULL, NULL,
2851 &set_record_full_cmdlist, &show_record_full_cmdlist);
2852
2853 c = add_alias_cmd ("stop-at-limit", "full stop-at-limit", no_class, 1,
2854 &set_record_cmdlist);
2855 deprecate_cmd (c, "set record full stop-at-limit");
2856
2857 c = add_alias_cmd ("stop-at-limit", "full stop-at-limit", no_class, 1,
2858 &show_record_cmdlist);
2859 deprecate_cmd (c, "show record full stop-at-limit");
2860
2861 add_setshow_uinteger_cmd ("insn-number-max", no_class,
2862 &record_full_insn_max_num,
2863 _("Set record/replay buffer limit."),
2864 _("Show record/replay buffer limit."), _("\
2865 Set the maximum number of instructions to be stored in the\n\
2866 record/replay buffer. A value of either \"unlimited\" or zero means no\n\
2867 limit. Default is 200000."),
2868 set_record_full_insn_max_num,
2869 NULL, &set_record_full_cmdlist,
2870 &show_record_full_cmdlist);
2871
2872 c = add_alias_cmd ("insn-number-max", "full insn-number-max", no_class, 1,
2873 &set_record_cmdlist);
2874 deprecate_cmd (c, "set record full insn-number-max");
2875
2876 c = add_alias_cmd ("insn-number-max", "full insn-number-max", no_class, 1,
2877 &show_record_cmdlist);
2878 deprecate_cmd (c, "show record full insn-number-max");
2879
2880 add_setshow_boolean_cmd ("memory-query", no_class,
2881 &record_full_memory_query, _("\
2882 Set whether query if PREC cannot record memory change of next instruction."),
2883 _("\
2884 Show whether query if PREC cannot record memory change of next instruction."),
2885 _("\
2886 Default is OFF.\n\
2887 When ON, query if PREC cannot record memory change of next instruction."),
2888 NULL, NULL,
2889 &set_record_full_cmdlist,
2890 &show_record_full_cmdlist);
2891
2892 c = add_alias_cmd ("memory-query", "full memory-query", no_class, 1,
2893 &set_record_cmdlist);
2894 deprecate_cmd (c, "set record full memory-query");
2895
2896 c = add_alias_cmd ("memory-query", "full memory-query", no_class, 1,
2897 &show_record_cmdlist);
2898 deprecate_cmd (c, "show record full memory-query");
2899 }
This page took 0.13072 seconds and 3 git commands to generate.