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