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