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