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