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