]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/record-full.c
gdbserver: initialize the members of lwp_info in-class
[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. */
1158 status->kind = TARGET_WAITKIND_IGNORE;
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);
d02ed0bb
MM
1185 if (status->kind == TARGET_WAITKIND_IGNORE)
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? */
1201 if (status->kind == TARGET_WAITKIND_STOPPED
1202 && status->value.sig == GDB_SIGNAL_TRAP)
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 {
dda83cd7
SM
1240 status->kind = TARGET_WAITKIND_STOPPED;
1241 status->value.sig = GDB_SIGNAL_0;
1242 break;
24b21115 1243 }
d02ed0bb 1244
1192f124
SM
1245 process_stratum_target *proc_target
1246 = current_inferior ()->process_target ();
1247
dda83cd7 1248 if (gdbarch_software_single_step_p (gdbarch))
d02ed0bb
MM
1249 {
1250 /* Try to insert the software single step breakpoint.
1251 If insert success, set step to 0. */
719546c4 1252 set_executing (proc_target, inferior_ptid, false);
d02ed0bb 1253 reinit_frame_cache ();
93f9a11f
YQ
1254
1255 step = !insert_single_step_breakpoints (gdbarch);
1256
719546c4 1257 set_executing (proc_target, inferior_ptid, true);
d02ed0bb
MM
1258 }
1259
1260 if (record_debug)
1261 fprintf_unfiltered (gdb_stdlog,
88d1aa9d
MM
1262 "Process record: record_full_wait "
1263 "issuing one more step in the "
1264 "target beneath\n");
b6a8c27b 1265 ops->beneath ()->resume (ptid, step, GDB_SIGNAL_0);
1192f124
SM
1266 proc_target->commit_resumed_state = true;
1267 proc_target->commit_resumed ();
1268 proc_target->commit_resumed_state = false;
d02ed0bb
MM
1269 continue;
1270 }
1271 }
1272
1273 /* The inferior is broken by a breakpoint or a signal. */
1274 break;
1275 }
1276
1277 return ret;
1278 }
1279 }
1280 else
1281 {
5b6d1e4f
PA
1282 switch_to_thread (current_inferior ()->process_target (),
1283 record_full_resume_ptid);
d02ed0bb 1284 struct regcache *regcache = get_current_regcache ();
ac7936df 1285 struct gdbarch *gdbarch = regcache->arch ();
8b86c959 1286 const struct address_space *aspace = regcache->aspace ();
d02ed0bb 1287 int continue_flag = 1;
88d1aa9d 1288 int first_record_full_end = 1;
d02ed0bb 1289
a70b8144 1290 try
d02ed0bb 1291 {
1ddbba9d 1292 CORE_ADDR tmp_pc;
d02ed0bb 1293
1ddbba9d
TT
1294 record_full_stop_reason = TARGET_STOPPED_BY_NO_REASON;
1295 status->kind = TARGET_WAITKIND_STOPPED;
d02ed0bb 1296
1ddbba9d
TT
1297 /* Check breakpoint when forward execute. */
1298 if (execution_direction == EXEC_FORWARD)
d02ed0bb 1299 {
1ddbba9d
TT
1300 tmp_pc = regcache_read_pc (regcache);
1301 if (record_check_stopped_by_breakpoint (aspace, tmp_pc,
1302 &record_full_stop_reason))
1303 {
1304 if (record_debug)
1305 fprintf_unfiltered (gdb_stdlog,
1306 "Process record: break at %s.\n",
1307 paddress (gdbarch, tmp_pc));
1308 goto replay_out;
1309 }
d02ed0bb
MM
1310 }
1311
1ddbba9d
TT
1312 /* If GDB is in terminal_inferior mode, it will not get the
1313 signal. And in GDB replay mode, GDB doesn't need to be
1314 in terminal_inferior mode, because inferior will not
1315 executed. Then set it to terminal_ours to make GDB get
1316 the signal. */
1317 target_terminal::ours ();
1318
1319 /* In EXEC_FORWARD mode, record_full_list points to the tail of prev
1320 instruction. */
1321 if (execution_direction == EXEC_FORWARD && record_full_list->next)
1322 record_full_list = record_full_list->next;
1323
1324 /* Loop over the record_full_list, looking for the next place to
1325 stop. */
1326 do
d02ed0bb 1327 {
1ddbba9d
TT
1328 /* Check for beginning and end of log. */
1329 if (execution_direction == EXEC_REVERSE
1330 && record_full_list == &record_full_first)
d02ed0bb 1331 {
1ddbba9d
TT
1332 /* Hit beginning of record log in reverse. */
1333 status->kind = TARGET_WAITKIND_NO_HISTORY;
1334 break;
d02ed0bb 1335 }
1ddbba9d
TT
1336 if (execution_direction != EXEC_REVERSE
1337 && !record_full_list->next)
1338 {
1339 /* Hit end of record log going forward. */
1340 status->kind = TARGET_WAITKIND_NO_HISTORY;
1341 break;
1342 }
1343
1344 record_full_exec_insn (regcache, gdbarch, record_full_list);
1345
1346 if (record_full_list->type == record_full_end)
d02ed0bb 1347 {
1ddbba9d
TT
1348 if (record_debug > 1)
1349 fprintf_unfiltered
1350 (gdb_stdlog,
1351 "Process record: record_full_end %s to "
1352 "inferior.\n",
1353 host_address_to_string (record_full_list));
1354
1355 if (first_record_full_end
1356 && execution_direction == EXEC_REVERSE)
d02ed0bb 1357 {
85102364 1358 /* When reverse execute, the first
1ddbba9d
TT
1359 record_full_end is the part of current
1360 instruction. */
1361 first_record_full_end = 0;
d02ed0bb 1362 }
1ddbba9d 1363 else
d02ed0bb 1364 {
1ddbba9d
TT
1365 /* In EXEC_REVERSE mode, this is the
1366 record_full_end of prev instruction. In
1367 EXEC_FORWARD mode, this is the
1368 record_full_end of current instruction. */
1369 /* step */
1370 if (record_full_resume_step)
1371 {
1372 if (record_debug > 1)
1373 fprintf_unfiltered (gdb_stdlog,
1374 "Process record: step.\n");
1375 continue_flag = 0;
1376 }
9e8915c6 1377
1ddbba9d
TT
1378 /* check breakpoint */
1379 tmp_pc = regcache_read_pc (regcache);
1380 if (record_check_stopped_by_breakpoint
1381 (aspace, tmp_pc, &record_full_stop_reason))
1382 {
1383 if (record_debug)
1384 fprintf_unfiltered (gdb_stdlog,
1385 "Process record: break "
1386 "at %s.\n",
1387 paddress (gdbarch, tmp_pc));
d02ed0bb 1388
1ddbba9d
TT
1389 continue_flag = 0;
1390 }
1391
1392 if (record_full_stop_reason
1393 == TARGET_STOPPED_BY_WATCHPOINT)
1394 {
1395 if (record_debug)
1396 fprintf_unfiltered (gdb_stdlog,
1397 "Process record: hit hw "
1398 "watchpoint.\n");
1399 continue_flag = 0;
1400 }
1401 /* Check target signal */
1402 if (record_full_list->u.end.sigval != GDB_SIGNAL_0)
1403 /* FIXME: better way to check */
1404 continue_flag = 0;
d02ed0bb 1405 }
d02ed0bb 1406 }
d02ed0bb 1407
1ddbba9d 1408 if (continue_flag)
d02ed0bb 1409 {
1ddbba9d
TT
1410 if (execution_direction == EXEC_REVERSE)
1411 {
1412 if (record_full_list->prev)
1413 record_full_list = record_full_list->prev;
1414 }
1415 else
1416 {
1417 if (record_full_list->next)
1418 record_full_list = record_full_list->next;
1419 }
d02ed0bb
MM
1420 }
1421 }
1ddbba9d
TT
1422 while (continue_flag);
1423
1424 replay_out:
1425 if (record_full_get_sig)
1426 status->value.sig = GDB_SIGNAL_INT;
1427 else if (record_full_list->u.end.sigval != GDB_SIGNAL_0)
1428 /* FIXME: better way to check */
1429 status->value.sig = record_full_list->u.end.sigval;
1430 else
1431 status->value.sig = GDB_SIGNAL_TRAP;
d02ed0bb 1432 }
230d2906 1433 catch (const gdb_exception &ex)
1ddbba9d
TT
1434 {
1435 if (execution_direction == EXEC_REVERSE)
1436 {
1437 if (record_full_list->next)
1438 record_full_list = record_full_list->next;
1439 }
1440 else
1441 record_full_list = record_full_list->prev;
d02ed0bb 1442
eedc3f4f 1443 throw;
1ddbba9d 1444 }
d02ed0bb
MM
1445 }
1446
1447 signal (SIGINT, handle_sigint);
1448
d02ed0bb
MM
1449 return inferior_ptid;
1450}
1451
f6ac5f3d
PA
1452ptid_t
1453record_full_base_target::wait (ptid_t ptid, struct target_waitstatus *status,
b60cea74 1454 target_wait_flags options)
d02ed0bb
MM
1455{
1456 ptid_t return_ptid;
1457
fdbc5215
SM
1458 clear_async_event_handler (record_full_async_inferior_event_token);
1459
f6ac5f3d 1460 return_ptid = record_full_wait_1 (this, ptid, status, options);
d02ed0bb
MM
1461 if (status->kind != TARGET_WAITKIND_IGNORE)
1462 {
1463 /* We're reporting a stop. Make sure any spurious
1464 target_wait(WNOHANG) doesn't advance the target until the
1465 core wants us resumed again. */
88d1aa9d 1466 record_full_resumed = 0;
d02ed0bb
MM
1467 }
1468 return return_ptid;
1469}
1470
57810aa7 1471bool
f6ac5f3d 1472record_full_base_target::stopped_by_watchpoint ()
d02ed0bb 1473{
88d1aa9d 1474 if (RECORD_FULL_IS_REPLAY)
9e8915c6 1475 return record_full_stop_reason == TARGET_STOPPED_BY_WATCHPOINT;
d02ed0bb 1476 else
b6a8c27b 1477 return beneath ()->stopped_by_watchpoint ();
d02ed0bb
MM
1478}
1479
57810aa7 1480bool
f6ac5f3d 1481record_full_base_target::stopped_data_address (CORE_ADDR *addr_p)
d02ed0bb 1482{
88d1aa9d 1483 if (RECORD_FULL_IS_REPLAY)
57810aa7 1484 return false;
d02ed0bb 1485 else
b6a8c27b 1486 return this->beneath ()->stopped_data_address (addr_p);
d02ed0bb
MM
1487}
1488
f6ac5f3d 1489/* The stopped_by_sw_breakpoint method of target record-full. */
9e8915c6 1490
57810aa7 1491bool
f6ac5f3d 1492record_full_base_target::stopped_by_sw_breakpoint ()
9e8915c6
PA
1493{
1494 return record_full_stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT;
1495}
1496
f6ac5f3d 1497/* The supports_stopped_by_sw_breakpoint method of target
9e8915c6
PA
1498 record-full. */
1499
57810aa7 1500bool
f6ac5f3d 1501record_full_base_target::supports_stopped_by_sw_breakpoint ()
9e8915c6 1502{
57810aa7 1503 return true;
9e8915c6
PA
1504}
1505
f6ac5f3d 1506/* The stopped_by_hw_breakpoint method of target record-full. */
9e8915c6 1507
57810aa7 1508bool
f6ac5f3d 1509record_full_base_target::stopped_by_hw_breakpoint ()
9e8915c6
PA
1510{
1511 return record_full_stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT;
1512}
1513
f6ac5f3d 1514/* The supports_stopped_by_sw_breakpoint method of target
9e8915c6
PA
1515 record-full. */
1516
57810aa7 1517bool
f6ac5f3d 1518record_full_base_target::supports_stopped_by_hw_breakpoint ()
9e8915c6 1519{
57810aa7 1520 return true;
9e8915c6
PA
1521}
1522
d02ed0bb
MM
1523/* Record registers change (by user or by GDB) to list as an instruction. */
1524
1525static void
88d1aa9d 1526record_full_registers_change (struct regcache *regcache, int regnum)
d02ed0bb 1527{
88d1aa9d 1528 /* Check record_full_insn_num. */
651ce16a 1529 record_full_check_insn_num ();
d02ed0bb 1530
88d1aa9d
MM
1531 record_full_arch_list_head = NULL;
1532 record_full_arch_list_tail = NULL;
d02ed0bb
MM
1533
1534 if (regnum < 0)
1535 {
1536 int i;
1537
ac7936df 1538 for (i = 0; i < gdbarch_num_regs (regcache->arch ()); i++)
d02ed0bb 1539 {
25ea693b 1540 if (record_full_arch_list_add_reg (regcache, i))
d02ed0bb 1541 {
88d1aa9d 1542 record_full_list_release (record_full_arch_list_tail);
d02ed0bb
MM
1543 error (_("Process record: failed to record execution log."));
1544 }
1545 }
1546 }
1547 else
1548 {
25ea693b 1549 if (record_full_arch_list_add_reg (regcache, regnum))
d02ed0bb 1550 {
88d1aa9d 1551 record_full_list_release (record_full_arch_list_tail);
d02ed0bb
MM
1552 error (_("Process record: failed to record execution log."));
1553 }
1554 }
25ea693b 1555 if (record_full_arch_list_add_end ())
d02ed0bb 1556 {
88d1aa9d 1557 record_full_list_release (record_full_arch_list_tail);
d02ed0bb
MM
1558 error (_("Process record: failed to record execution log."));
1559 }
88d1aa9d
MM
1560 record_full_list->next = record_full_arch_list_head;
1561 record_full_arch_list_head->prev = record_full_list;
1562 record_full_list = record_full_arch_list_tail;
d02ed0bb 1563
7ee70bf5 1564 if (record_full_insn_num == record_full_insn_max_num)
88d1aa9d 1565 record_full_list_release_first ();
d02ed0bb 1566 else
88d1aa9d 1567 record_full_insn_num++;
d02ed0bb
MM
1568}
1569
f6ac5f3d 1570/* "store_registers" method for process record target. */
d02ed0bb 1571
f6ac5f3d
PA
1572void
1573record_full_target::store_registers (struct regcache *regcache, int regno)
d02ed0bb 1574{
88d1aa9d 1575 if (!record_full_gdb_operation_disable)
d02ed0bb 1576 {
88d1aa9d 1577 if (RECORD_FULL_IS_REPLAY)
d02ed0bb
MM
1578 {
1579 int n;
1580
1581 /* Let user choose if he wants to write register or not. */
1582 if (regno < 0)
1583 n =
1584 query (_("Because GDB is in replay mode, changing the "
1585 "value of a register will make the execution "
1586 "log unusable from this point onward. "
1587 "Change all registers?"));
1588 else
1589 n =
1590 query (_("Because GDB is in replay mode, changing the value "
1591 "of a register will make the execution log unusable "
1592 "from this point onward. Change register %s?"),
ac7936df 1593 gdbarch_register_name (regcache->arch (),
d02ed0bb
MM
1594 regno));
1595
1596 if (!n)
1597 {
1598 /* Invalidate the value of regcache that was set in function
dda83cd7 1599 "regcache_raw_write". */
d02ed0bb
MM
1600 if (regno < 0)
1601 {
1602 int i;
1603
1604 for (i = 0;
ac7936df 1605 i < gdbarch_num_regs (regcache->arch ());
d02ed0bb 1606 i++)
6aa7d724 1607 regcache->invalidate (i);
d02ed0bb
MM
1608 }
1609 else
6aa7d724 1610 regcache->invalidate (regno);
d02ed0bb
MM
1611
1612 error (_("Process record canceled the operation."));
1613 }
1614
1615 /* Destroy the record from here forward. */
88d1aa9d 1616 record_full_list_release_following (record_full_list);
d02ed0bb
MM
1617 }
1618
88d1aa9d 1619 record_full_registers_change (regcache, regno);
d02ed0bb 1620 }
b6a8c27b 1621 this->beneath ()->store_registers (regcache, regno);
d02ed0bb
MM
1622}
1623
f6ac5f3d 1624/* "xfer_partial" method. Behavior is conditional on
88d1aa9d 1625 RECORD_FULL_IS_REPLAY.
d02ed0bb
MM
1626 In replay mode, we cannot write memory unles we are willing to
1627 invalidate the record/replay log from this point forward. */
1628
f6ac5f3d
PA
1629enum target_xfer_status
1630record_full_target::xfer_partial (enum target_object object,
1631 const char *annex, gdb_byte *readbuf,
1632 const gdb_byte *writebuf, ULONGEST offset,
1633 ULONGEST len, ULONGEST *xfered_len)
d02ed0bb 1634{
88d1aa9d 1635 if (!record_full_gdb_operation_disable
d02ed0bb
MM
1636 && (object == TARGET_OBJECT_MEMORY
1637 || object == TARGET_OBJECT_RAW_MEMORY) && writebuf)
1638 {
88d1aa9d 1639 if (RECORD_FULL_IS_REPLAY)
d02ed0bb
MM
1640 {
1641 /* Let user choose if he wants to write memory or not. */
1642 if (!query (_("Because GDB is in replay mode, writing to memory "
dda83cd7
SM
1643 "will make the execution log unusable from this "
1644 "point onward. Write memory at address %s?"),
d02ed0bb
MM
1645 paddress (target_gdbarch (), offset)))
1646 error (_("Process record canceled the operation."));
1647
1648 /* Destroy the record from here forward. */
88d1aa9d 1649 record_full_list_release_following (record_full_list);
d02ed0bb
MM
1650 }
1651
88d1aa9d 1652 /* Check record_full_insn_num */
651ce16a 1653 record_full_check_insn_num ();
d02ed0bb
MM
1654
1655 /* Record registers change to list as an instruction. */
88d1aa9d
MM
1656 record_full_arch_list_head = NULL;
1657 record_full_arch_list_tail = NULL;
25ea693b 1658 if (record_full_arch_list_add_mem (offset, len))
d02ed0bb 1659 {
88d1aa9d 1660 record_full_list_release (record_full_arch_list_tail);
d02ed0bb
MM
1661 if (record_debug)
1662 fprintf_unfiltered (gdb_stdlog,
1663 "Process record: failed to record "
1664 "execution log.");
2ed4b548 1665 return TARGET_XFER_E_IO;
d02ed0bb 1666 }
25ea693b 1667 if (record_full_arch_list_add_end ())
d02ed0bb 1668 {
88d1aa9d 1669 record_full_list_release (record_full_arch_list_tail);
d02ed0bb
MM
1670 if (record_debug)
1671 fprintf_unfiltered (gdb_stdlog,
1672 "Process record: failed to record "
1673 "execution log.");
2ed4b548 1674 return TARGET_XFER_E_IO;
d02ed0bb 1675 }
88d1aa9d
MM
1676 record_full_list->next = record_full_arch_list_head;
1677 record_full_arch_list_head->prev = record_full_list;
1678 record_full_list = record_full_arch_list_tail;
d02ed0bb 1679
7ee70bf5 1680 if (record_full_insn_num == record_full_insn_max_num)
88d1aa9d 1681 record_full_list_release_first ();
d02ed0bb 1682 else
88d1aa9d 1683 record_full_insn_num++;
d02ed0bb
MM
1684 }
1685
b6a8c27b
PA
1686 return this->beneath ()->xfer_partial (object, annex, readbuf, writebuf,
1687 offset, len, xfered_len);
d02ed0bb
MM
1688}
1689
1690/* This structure represents a breakpoint inserted while the record
1691 target is active. We use this to know when to install/remove
1692 breakpoints in/from the target beneath. For example, a breakpoint
1693 may be inserted while recording, but removed when not replaying nor
1694 recording. In that case, the breakpoint had not been inserted on
1695 the target beneath, so we should not try to remove it there. */
1696
88d1aa9d 1697struct record_full_breakpoint
d02ed0bb 1698{
219605fd
TT
1699 record_full_breakpoint (struct address_space *address_space_,
1700 CORE_ADDR addr_,
1701 bool in_target_beneath_)
1702 : address_space (address_space_),
1703 addr (addr_),
1704 in_target_beneath (in_target_beneath_)
1705 {
1706 }
1707
d02ed0bb
MM
1708 /* The address and address space the breakpoint was set at. */
1709 struct address_space *address_space;
1710 CORE_ADDR addr;
1711
1712 /* True when the breakpoint has been also installed in the target
1713 beneath. This will be false for breakpoints set during replay or
1714 when recording. */
219605fd 1715 bool in_target_beneath;
d02ed0bb
MM
1716};
1717
d02ed0bb
MM
1718/* The list of breakpoints inserted while the record target is
1719 active. */
219605fd 1720static std::vector<record_full_breakpoint> record_full_breakpoints;
d02ed0bb 1721
88d1aa9d 1722/* Sync existing breakpoints to record_full_breakpoints. */
d02ed0bb
MM
1723
1724static void
88d1aa9d 1725record_full_init_record_breakpoints (void)
d02ed0bb 1726{
219605fd 1727 record_full_breakpoints.clear ();
d02ed0bb 1728
055c879f
SM
1729 for (bp_location *loc : all_bp_locations ())
1730 {
1731 if (loc->loc_type != bp_loc_software_breakpoint)
1732 continue;
1733
1734 if (loc->inserted)
1735 record_full_breakpoints.emplace_back
1736 (loc->target_info.placed_address_space,
1737 loc->target_info.placed_address, 1);
1738 }
d02ed0bb
MM
1739}
1740
88d1aa9d 1741/* Behavior is conditional on RECORD_FULL_IS_REPLAY. We will not actually
d02ed0bb
MM
1742 insert or remove breakpoints in the real target when replaying, nor
1743 when recording. */
1744
f6ac5f3d
PA
1745int
1746record_full_target::insert_breakpoint (struct gdbarch *gdbarch,
1747 struct bp_target_info *bp_tgt)
d02ed0bb 1748{
219605fd 1749 bool in_target_beneath = false;
d02ed0bb 1750
88d1aa9d 1751 if (!RECORD_FULL_IS_REPLAY)
d02ed0bb
MM
1752 {
1753 /* When recording, we currently always single-step, so we don't
1754 really need to install regular breakpoints in the inferior.
1755 However, we do have to insert software single-step
1756 breakpoints, in case the target can't hardware step. To keep
f99bd5f2 1757 things simple, we always insert. */
d02ed0bb 1758
07036511
TT
1759 scoped_restore restore_operation_disable
1760 = record_full_gdb_operation_disable_set ();
d02ed0bb 1761
b6a8c27b 1762 int ret = this->beneath ()->insert_breakpoint (gdbarch, bp_tgt);
d02ed0bb
MM
1763 if (ret != 0)
1764 return ret;
1765
219605fd 1766 in_target_beneath = true;
d02ed0bb
MM
1767 }
1768
e390720b
YQ
1769 /* Use the existing entries if found in order to avoid duplication
1770 in record_full_breakpoints. */
1771
19f3f25f 1772 for (const record_full_breakpoint &bp : record_full_breakpoints)
e390720b 1773 {
219605fd
TT
1774 if (bp.addr == bp_tgt->placed_address
1775 && bp.address_space == bp_tgt->placed_address_space)
e390720b 1776 {
219605fd 1777 gdb_assert (bp.in_target_beneath == in_target_beneath);
e390720b
YQ
1778 return 0;
1779 }
1780 }
1781
219605fd
TT
1782 record_full_breakpoints.emplace_back (bp_tgt->placed_address_space,
1783 bp_tgt->placed_address,
1784 in_target_beneath);
d02ed0bb
MM
1785 return 0;
1786}
1787
f6ac5f3d 1788/* "remove_breakpoint" method for process record target. */
d02ed0bb 1789
f6ac5f3d
PA
1790int
1791record_full_target::remove_breakpoint (struct gdbarch *gdbarch,
1792 struct bp_target_info *bp_tgt,
1793 enum remove_bp_reason reason)
d02ed0bb 1794{
219605fd
TT
1795 for (auto iter = record_full_breakpoints.begin ();
1796 iter != record_full_breakpoints.end ();
1797 ++iter)
d02ed0bb 1798 {
219605fd
TT
1799 struct record_full_breakpoint &bp = *iter;
1800
1801 if (bp.addr == bp_tgt->placed_address
1802 && bp.address_space == bp_tgt->placed_address_space)
d02ed0bb 1803 {
219605fd 1804 if (bp.in_target_beneath)
d02ed0bb 1805 {
07036511
TT
1806 scoped_restore restore_operation_disable
1807 = record_full_gdb_operation_disable_set ();
f6ac5f3d 1808
b6a8c27b
PA
1809 int ret = this->beneath ()->remove_breakpoint (gdbarch, bp_tgt,
1810 reason);
d02ed0bb
MM
1811 if (ret != 0)
1812 return ret;
1813 }
1814
01d3dedf 1815 if (reason == REMOVE_BREAKPOINT)
219605fd 1816 unordered_remove (record_full_breakpoints, iter);
d02ed0bb
MM
1817 return 0;
1818 }
1819 }
1820
1821 gdb_assert_not_reached ("removing unknown breakpoint");
1822}
1823
f6ac5f3d 1824/* "can_execute_reverse" method for process record target. */
d02ed0bb 1825
57810aa7 1826bool
f6ac5f3d 1827record_full_base_target::can_execute_reverse ()
d02ed0bb 1828{
57810aa7 1829 return true;
d02ed0bb
MM
1830}
1831
f6ac5f3d 1832/* "get_bookmark" method for process record and prec over core. */
d02ed0bb 1833
f6ac5f3d
PA
1834gdb_byte *
1835record_full_base_target::get_bookmark (const char *args, int from_tty)
d02ed0bb 1836{
0f928d68 1837 char *ret = NULL;
d02ed0bb
MM
1838
1839 /* Return stringified form of instruction count. */
88d1aa9d
MM
1840 if (record_full_list && record_full_list->type == record_full_end)
1841 ret = xstrdup (pulongest (record_full_list->u.end.insn_num));
d02ed0bb
MM
1842
1843 if (record_debug)
1844 {
1845 if (ret)
1846 fprintf_unfiltered (gdb_stdlog,
88d1aa9d 1847 "record_full_get_bookmark returns %s\n", ret);
d02ed0bb
MM
1848 else
1849 fprintf_unfiltered (gdb_stdlog,
88d1aa9d 1850 "record_full_get_bookmark returns NULL\n");
d02ed0bb 1851 }
0f928d68 1852 return (gdb_byte *) ret;
d02ed0bb
MM
1853}
1854
f6ac5f3d 1855/* "goto_bookmark" method for process record and prec over core. */
d02ed0bb 1856
f6ac5f3d
PA
1857void
1858record_full_base_target::goto_bookmark (const gdb_byte *raw_bookmark,
1859 int from_tty)
d02ed0bb 1860{
c2bcbb1d 1861 const char *bookmark = (const char *) raw_bookmark;
0f928d68 1862
d02ed0bb
MM
1863 if (record_debug)
1864 fprintf_unfiltered (gdb_stdlog,
88d1aa9d 1865 "record_full_goto_bookmark receives %s\n", bookmark);
d02ed0bb 1866
a2b2bc12 1867 std::string name_holder;
d02ed0bb
MM
1868 if (bookmark[0] == '\'' || bookmark[0] == '\"')
1869 {
1870 if (bookmark[strlen (bookmark) - 1] != bookmark[0])
1871 error (_("Unbalanced quotes: %s"), bookmark);
1872
a2b2bc12
TT
1873 name_holder = std::string (bookmark + 1, strlen (bookmark) - 2);
1874 bookmark = name_holder.c_str ();
d02ed0bb
MM
1875 }
1876
c2bcbb1d 1877 record_goto (bookmark);
d02ed0bb
MM
1878}
1879
f6ac5f3d
PA
1880enum exec_direction_kind
1881record_full_base_target::execution_direction ()
d02ed0bb 1882{
88d1aa9d 1883 return record_full_execution_dir;
d02ed0bb
MM
1884}
1885
f6ac5f3d 1886/* The record_method method of target record-full. */
b158a20f
TW
1887
1888enum record_method
f6ac5f3d 1889record_full_base_target::record_method (ptid_t ptid)
b158a20f
TW
1890{
1891 return RECORD_METHOD_FULL;
1892}
1893
f6ac5f3d
PA
1894void
1895record_full_base_target::info_record ()
d02ed0bb 1896{
88d1aa9d 1897 struct record_full_entry *p;
d02ed0bb 1898
88d1aa9d 1899 if (RECORD_FULL_IS_REPLAY)
d02ed0bb
MM
1900 printf_filtered (_("Replay mode:\n"));
1901 else
1902 printf_filtered (_("Record mode:\n"));
1903
1904 /* Find entry for first actual instruction in the log. */
88d1aa9d
MM
1905 for (p = record_full_first.next;
1906 p != NULL && p->type != record_full_end;
d02ed0bb
MM
1907 p = p->next)
1908 ;
1909
1910 /* Do we have a log at all? */
88d1aa9d 1911 if (p != NULL && p->type == record_full_end)
d02ed0bb
MM
1912 {
1913 /* Display instruction number for first instruction in the log. */
1914 printf_filtered (_("Lowest recorded instruction number is %s.\n"),
1915 pulongest (p->u.end.insn_num));
1916
1917 /* If in replay mode, display where we are in the log. */
88d1aa9d 1918 if (RECORD_FULL_IS_REPLAY)
d02ed0bb 1919 printf_filtered (_("Current instruction number is %s.\n"),
88d1aa9d 1920 pulongest (record_full_list->u.end.insn_num));
d02ed0bb
MM
1921
1922 /* Display instruction number for last instruction in the log. */
1923 printf_filtered (_("Highest recorded instruction number is %s.\n"),
88d1aa9d 1924 pulongest (record_full_insn_count));
d02ed0bb
MM
1925
1926 /* Display log count. */
7ee70bf5 1927 printf_filtered (_("Log contains %u instructions.\n"),
88d1aa9d 1928 record_full_insn_num);
d02ed0bb
MM
1929 }
1930 else
1931 printf_filtered (_("No instructions have been logged.\n"));
1932
1933 /* Display max log size. */
7ee70bf5 1934 printf_filtered (_("Max logged instructions is %u.\n"),
88d1aa9d 1935 record_full_insn_max_num);
d02ed0bb
MM
1936}
1937
f6ac5f3d
PA
1938bool
1939record_full_base_target::supports_delete_record ()
1940{
1941 return true;
1942}
d02ed0bb 1943
f6ac5f3d
PA
1944/* The "delete_record" target method. */
1945
1946void
1947record_full_base_target::delete_record ()
d02ed0bb 1948{
88d1aa9d 1949 record_full_list_release_following (record_full_list);
d02ed0bb
MM
1950}
1951
f6ac5f3d 1952/* The "record_is_replaying" target method. */
d02ed0bb 1953
57810aa7 1954bool
f6ac5f3d 1955record_full_base_target::record_is_replaying (ptid_t ptid)
d02ed0bb 1956{
88d1aa9d 1957 return RECORD_FULL_IS_REPLAY;
d02ed0bb
MM
1958}
1959
f6ac5f3d 1960/* The "record_will_replay" target method. */
7ff27e9b 1961
57810aa7 1962bool
f6ac5f3d 1963record_full_base_target::record_will_replay (ptid_t ptid, int dir)
7ff27e9b
MM
1964{
1965 /* We can currently only record when executing forwards. Should we be able
1966 to record when executing backwards on targets that support reverse
1967 execution, this needs to be changed. */
1968
1969 return RECORD_FULL_IS_REPLAY || dir == EXEC_REVERSE;
1970}
1971
d02ed0bb
MM
1972/* Go to a specific entry. */
1973
1974static void
88d1aa9d 1975record_full_goto_entry (struct record_full_entry *p)
d02ed0bb
MM
1976{
1977 if (p == NULL)
1978 error (_("Target insn not found."));
88d1aa9d 1979 else if (p == record_full_list)
d02ed0bb 1980 error (_("Already at target insn."));
88d1aa9d 1981 else if (p->u.end.insn_num > record_full_list->u.end.insn_num)
d02ed0bb
MM
1982 {
1983 printf_filtered (_("Go forward to insn number %s\n"),
1984 pulongest (p->u.end.insn_num));
88d1aa9d 1985 record_full_goto_insn (p, EXEC_FORWARD);
d02ed0bb
MM
1986 }
1987 else
1988 {
1989 printf_filtered (_("Go backward to insn number %s\n"),
1990 pulongest (p->u.end.insn_num));
88d1aa9d 1991 record_full_goto_insn (p, EXEC_REVERSE);
d02ed0bb
MM
1992 }
1993
1994 registers_changed ();
1995 reinit_frame_cache ();
1edb66d8 1996 inferior_thread ()->set_stop_pc (regcache_read_pc (get_current_regcache ()));
08d72866 1997 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
d02ed0bb
MM
1998}
1999
f6ac5f3d 2000/* The "goto_record_begin" target method. */
d02ed0bb 2001
f6ac5f3d
PA
2002void
2003record_full_base_target::goto_record_begin ()
d02ed0bb 2004{
88d1aa9d 2005 struct record_full_entry *p = NULL;
d02ed0bb 2006
88d1aa9d
MM
2007 for (p = &record_full_first; p != NULL; p = p->next)
2008 if (p->type == record_full_end)
d02ed0bb
MM
2009 break;
2010
88d1aa9d 2011 record_full_goto_entry (p);
d02ed0bb
MM
2012}
2013
f6ac5f3d 2014/* The "goto_record_end" target method. */
d02ed0bb 2015
f6ac5f3d
PA
2016void
2017record_full_base_target::goto_record_end ()
d02ed0bb 2018{
88d1aa9d 2019 struct record_full_entry *p = NULL;
d02ed0bb 2020
88d1aa9d 2021 for (p = record_full_list; p->next != NULL; p = p->next)
d02ed0bb
MM
2022 ;
2023 for (; p!= NULL; p = p->prev)
88d1aa9d 2024 if (p->type == record_full_end)
d02ed0bb
MM
2025 break;
2026
88d1aa9d 2027 record_full_goto_entry (p);
d02ed0bb
MM
2028}
2029
f6ac5f3d 2030/* The "goto_record" target method. */
d02ed0bb 2031
f6ac5f3d
PA
2032void
2033record_full_base_target::goto_record (ULONGEST target_insn)
d02ed0bb 2034{
88d1aa9d 2035 struct record_full_entry *p = NULL;
d02ed0bb 2036
88d1aa9d
MM
2037 for (p = &record_full_first; p != NULL; p = p->next)
2038 if (p->type == record_full_end && p->u.end.insn_num == target_insn)
d02ed0bb
MM
2039 break;
2040
88d1aa9d 2041 record_full_goto_entry (p);
d02ed0bb
MM
2042}
2043
f6ac5f3d 2044/* The "record_stop_replaying" target method. */
797094dd 2045
f6ac5f3d
PA
2046void
2047record_full_base_target::record_stop_replaying ()
797094dd 2048{
f6ac5f3d 2049 goto_record_end ();
797094dd
MM
2050}
2051
f6ac5f3d 2052/* "resume" method for prec over corefile. */
d02ed0bb 2053
f6ac5f3d
PA
2054void
2055record_full_core_target::resume (ptid_t ptid, int step,
2056 enum gdb_signal signal)
d02ed0bb 2057{
88d1aa9d
MM
2058 record_full_resume_step = step;
2059 record_full_resumed = 1;
f6ac5f3d 2060 record_full_execution_dir = ::execution_direction;
d02ed0bb
MM
2061
2062 /* We are about to start executing the inferior (or simulate it),
2063 let's register it with the event loop. */
2064 if (target_can_async_p ())
6a3753b3 2065 target_async (1);
d02ed0bb
MM
2066}
2067
f6ac5f3d 2068/* "kill" method for prec over corefile. */
d02ed0bb 2069
f6ac5f3d
PA
2070void
2071record_full_core_target::kill ()
d02ed0bb
MM
2072{
2073 if (record_debug)
88d1aa9d 2074 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_core_kill\n");
d02ed0bb 2075
fadf6add 2076 current_inferior ()->unpush_target (this);
d02ed0bb
MM
2077}
2078
f6ac5f3d 2079/* "fetch_registers" method for prec over corefile. */
d02ed0bb 2080
f6ac5f3d
PA
2081void
2082record_full_core_target::fetch_registers (struct regcache *regcache,
2083 int regno)
d02ed0bb
MM
2084{
2085 if (regno < 0)
2086 {
ac7936df 2087 int num = gdbarch_num_regs (regcache->arch ());
d02ed0bb
MM
2088 int i;
2089
2090 for (i = 0; i < num; i ++)
c8ec2f33 2091 regcache->raw_supply (i, *record_full_core_regbuf);
d02ed0bb
MM
2092 }
2093 else
c8ec2f33 2094 regcache->raw_supply (regno, *record_full_core_regbuf);
d02ed0bb
MM
2095}
2096
f6ac5f3d 2097/* "prepare_to_store" method for prec over corefile. */
d02ed0bb 2098
f6ac5f3d
PA
2099void
2100record_full_core_target::prepare_to_store (struct regcache *regcache)
d02ed0bb
MM
2101{
2102}
2103
f6ac5f3d 2104/* "store_registers" method for prec over corefile. */
d02ed0bb 2105
f6ac5f3d
PA
2106void
2107record_full_core_target::store_registers (struct regcache *regcache,
2108 int regno)
d02ed0bb 2109{
88d1aa9d 2110 if (record_full_gdb_operation_disable)
c8ec2f33 2111 record_full_core_regbuf->raw_supply (regno, *regcache);
d02ed0bb
MM
2112 else
2113 error (_("You can't do that without a process to debug."));
2114}
2115
f6ac5f3d 2116/* "xfer_partial" method for prec over corefile. */
d02ed0bb 2117
f6ac5f3d
PA
2118enum target_xfer_status
2119record_full_core_target::xfer_partial (enum target_object object,
2120 const char *annex, gdb_byte *readbuf,
2121 const gdb_byte *writebuf, ULONGEST offset,
2122 ULONGEST len, ULONGEST *xfered_len)
d02ed0bb
MM
2123{
2124 if (object == TARGET_OBJECT_MEMORY)
2125 {
88d1aa9d 2126 if (record_full_gdb_operation_disable || !writebuf)
d02ed0bb 2127 {
d7a78e5c 2128 for (target_section &p : record_full_core_sections)
d02ed0bb 2129 {
bb2a6777 2130 if (offset >= p.addr)
d02ed0bb 2131 {
88d1aa9d 2132 struct record_full_core_buf_entry *entry;
d02ed0bb
MM
2133 ULONGEST sec_offset;
2134
bb2a6777 2135 if (offset >= p.endaddr)
d02ed0bb
MM
2136 continue;
2137
bb2a6777
TT
2138 if (offset + len > p.endaddr)
2139 len = p.endaddr - offset;
d02ed0bb 2140
bb2a6777 2141 sec_offset = offset - p.addr;
d02ed0bb
MM
2142
2143 /* Read readbuf or write writebuf p, offset, len. */
2144 /* Check flags. */
bb2a6777
TT
2145 if (p.the_bfd_section->flags & SEC_CONSTRUCTOR
2146 || (p.the_bfd_section->flags & SEC_HAS_CONTENTS) == 0)
d02ed0bb
MM
2147 {
2148 if (readbuf)
2149 memset (readbuf, 0, len);
9b409511
YQ
2150
2151 *xfered_len = len;
2152 return TARGET_XFER_OK;
d02ed0bb 2153 }
88d1aa9d
MM
2154 /* Get record_full_core_buf_entry. */
2155 for (entry = record_full_core_buf_list; entry;
d02ed0bb 2156 entry = entry->prev)
bb2a6777 2157 if (entry->p == &p)
d02ed0bb
MM
2158 break;
2159 if (writebuf)
2160 {
2161 if (!entry)
2162 {
2163 /* Add a new entry. */
8d749320 2164 entry = XNEW (struct record_full_core_buf_entry);
bb2a6777 2165 entry->p = &p;
2b2848e2 2166 if (!bfd_malloc_and_get_section
dda83cd7 2167 (p.the_bfd_section->owner,
bb2a6777 2168 p.the_bfd_section,
2b2848e2 2169 &entry->buf))
d02ed0bb
MM
2170 {
2171 xfree (entry);
9b409511 2172 return TARGET_XFER_EOF;
d02ed0bb 2173 }
88d1aa9d
MM
2174 entry->prev = record_full_core_buf_list;
2175 record_full_core_buf_list = entry;
d02ed0bb
MM
2176 }
2177
2178 memcpy (entry->buf + sec_offset, writebuf,
2179 (size_t) len);
2180 }
2181 else
2182 {
2183 if (!entry)
b6a8c27b
PA
2184 return this->beneath ()->xfer_partial (object, annex,
2185 readbuf, writebuf,
2186 offset, len,
2187 xfered_len);
d02ed0bb
MM
2188
2189 memcpy (readbuf, entry->buf + sec_offset,
2190 (size_t) len);
2191 }
2192
9b409511
YQ
2193 *xfered_len = len;
2194 return TARGET_XFER_OK;
d02ed0bb
MM
2195 }
2196 }
2197
2ed4b548 2198 return TARGET_XFER_E_IO;
d02ed0bb
MM
2199 }
2200 else
2201 error (_("You can't do that without a process to debug."));
2202 }
2203
b6a8c27b
PA
2204 return this->beneath ()->xfer_partial (object, annex,
2205 readbuf, writebuf, offset, len,
2206 xfered_len);
d02ed0bb
MM
2207}
2208
f6ac5f3d 2209/* "insert_breakpoint" method for prec over corefile. */
d02ed0bb 2210
f6ac5f3d
PA
2211int
2212record_full_core_target::insert_breakpoint (struct gdbarch *gdbarch,
2213 struct bp_target_info *bp_tgt)
d02ed0bb
MM
2214{
2215 return 0;
2216}
2217
f6ac5f3d 2218/* "remove_breakpoint" method for prec over corefile. */
d02ed0bb 2219
f6ac5f3d
PA
2220int
2221record_full_core_target::remove_breakpoint (struct gdbarch *gdbarch,
2222 struct bp_target_info *bp_tgt,
2223 enum remove_bp_reason reason)
d02ed0bb
MM
2224{
2225 return 0;
2226}
2227
f6ac5f3d 2228/* "has_execution" method for prec over corefile. */
d02ed0bb 2229
57810aa7 2230bool
5018ce90 2231record_full_core_target::has_execution (inferior *inf)
d02ed0bb 2232{
57810aa7 2233 return true;
d02ed0bb
MM
2234}
2235
d02ed0bb
MM
2236/* Record log save-file format
2237 Version 1 (never released)
2238
2239 Header:
2240 4 bytes: magic number htonl(0x20090829).
2241 NOTE: be sure to change whenever this file format changes!
2242
2243 Records:
88d1aa9d
MM
2244 record_full_end:
2245 1 byte: record type (record_full_end, see enum record_full_type).
2246 record_full_reg:
2247 1 byte: record type (record_full_reg, see enum record_full_type).
d02ed0bb
MM
2248 8 bytes: register id (network byte order).
2249 MAX_REGISTER_SIZE bytes: register value.
88d1aa9d
MM
2250 record_full_mem:
2251 1 byte: record type (record_full_mem, see enum record_full_type).
d02ed0bb
MM
2252 8 bytes: memory length (network byte order).
2253 8 bytes: memory address (network byte order).
2254 n bytes: memory value (n == memory length).
2255
2256 Version 2
2257 4 bytes: magic number netorder32(0x20091016).
2258 NOTE: be sure to change whenever this file format changes!
2259
2260 Records:
88d1aa9d
MM
2261 record_full_end:
2262 1 byte: record type (record_full_end, see enum record_full_type).
d02ed0bb
MM
2263 4 bytes: signal
2264 4 bytes: instruction count
88d1aa9d
MM
2265 record_full_reg:
2266 1 byte: record type (record_full_reg, see enum record_full_type).
d02ed0bb
MM
2267 4 bytes: register id (network byte order).
2268 n bytes: register value (n == actual register size).
dda83cd7 2269 (eg. 4 bytes for x86 general registers).
88d1aa9d
MM
2270 record_full_mem:
2271 1 byte: record type (record_full_mem, see enum record_full_type).
d02ed0bb
MM
2272 4 bytes: memory length (network byte order).
2273 8 bytes: memory address (network byte order).
2274 n bytes: memory value (n == memory length).
2275
2276*/
2277
2278/* bfdcore_read -- read bytes from a core file section. */
2279
2280static inline void
2281bfdcore_read (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2282{
2283 int ret = bfd_get_section_contents (obfd, osec, buf, *offset, len);
2284
2285 if (ret)
2286 *offset += len;
2287 else
2288 error (_("Failed to read %d bytes from core file %s ('%s')."),
2289 len, bfd_get_filename (obfd),
2290 bfd_errmsg (bfd_get_error ()));
2291}
2292
2293static inline uint64_t
2294netorder64 (uint64_t input)
2295{
2296 uint64_t ret;
2297
2298 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2299 BFD_ENDIAN_BIG, input);
2300 return ret;
2301}
2302
2303static inline uint32_t
2304netorder32 (uint32_t input)
2305{
2306 uint32_t ret;
2307
d02ed0bb
MM
2308 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2309 BFD_ENDIAN_BIG, input);
2310 return ret;
2311}
2312
2313/* Restore the execution log from a core_bfd file. */
2314static void
88d1aa9d 2315record_full_restore (void)
d02ed0bb
MM
2316{
2317 uint32_t magic;
88d1aa9d 2318 struct record_full_entry *rec;
d02ed0bb
MM
2319 asection *osec;
2320 uint32_t osec_size;
2321 int bfd_offset = 0;
2322 struct regcache *regcache;
2323
2324 /* We restore the execution log from the open core bfd,
2325 if there is one. */
2326 if (core_bfd == NULL)
2327 return;
2328
88d1aa9d
MM
2329 /* "record_full_restore" can only be called when record list is empty. */
2330 gdb_assert (record_full_first.next == NULL);
d02ed0bb
MM
2331
2332 if (record_debug)
2333 fprintf_unfiltered (gdb_stdlog, "Restoring recording from core file.\n");
2334
2335 /* Now need to find our special note section. */
2336 osec = bfd_get_section_by_name (core_bfd, "null0");
2337 if (record_debug)
2338 fprintf_unfiltered (gdb_stdlog, "Find precord section %s.\n",
2339 osec ? "succeeded" : "failed");
2340 if (osec == NULL)
2341 return;
fd361982 2342 osec_size = bfd_section_size (osec);
d02ed0bb 2343 if (record_debug)
fd361982 2344 fprintf_unfiltered (gdb_stdlog, "%s", bfd_section_name (osec));
d02ed0bb
MM
2345
2346 /* Check the magic code. */
2347 bfdcore_read (core_bfd, osec, &magic, sizeof (magic), &bfd_offset);
88d1aa9d 2348 if (magic != RECORD_FULL_FILE_MAGIC)
d02ed0bb
MM
2349 error (_("Version mis-match or file format error in core file %s."),
2350 bfd_get_filename (core_bfd));
2351 if (record_debug)
2352 fprintf_unfiltered (gdb_stdlog,
2353 " Reading 4-byte magic cookie "
88d1aa9d 2354 "RECORD_FULL_FILE_MAGIC (0x%s)\n",
d02ed0bb
MM
2355 phex_nz (netorder32 (magic), 4));
2356
88d1aa9d
MM
2357 /* Restore the entries in recfd into record_full_arch_list_head and
2358 record_full_arch_list_tail. */
2359 record_full_arch_list_head = NULL;
2360 record_full_arch_list_tail = NULL;
2361 record_full_insn_num = 0;
d02ed0bb 2362
a70b8144 2363 try
d02ed0bb 2364 {
1ddbba9d
TT
2365 regcache = get_current_regcache ();
2366
2367 while (1)
2368 {
2369 uint8_t rectype;
2370 uint32_t regnum, len, signal, count;
2371 uint64_t addr;
d02ed0bb 2372
1ddbba9d
TT
2373 /* We are finished when offset reaches osec_size. */
2374 if (bfd_offset >= osec_size)
2375 break;
2376 bfdcore_read (core_bfd, osec, &rectype, sizeof (rectype), &bfd_offset);
d02ed0bb 2377
1ddbba9d
TT
2378 switch (rectype)
2379 {
2380 case record_full_reg: /* reg */
2381 /* Get register number to regnum. */
2382 bfdcore_read (core_bfd, osec, &regnum,
2383 sizeof (regnum), &bfd_offset);
2384 regnum = netorder32 (regnum);
d02ed0bb 2385
1ddbba9d 2386 rec = record_full_reg_alloc (regcache, regnum);
d02ed0bb 2387
1ddbba9d
TT
2388 /* Get val. */
2389 bfdcore_read (core_bfd, osec, record_full_get_loc (rec),
2390 rec->u.reg.len, &bfd_offset);
d02ed0bb 2391
1ddbba9d
TT
2392 if (record_debug)
2393 fprintf_unfiltered (gdb_stdlog,
2394 " Reading register %d (1 "
2395 "plus %lu plus %d bytes)\n",
2396 rec->u.reg.num,
2397 (unsigned long) sizeof (regnum),
2398 rec->u.reg.len);
2399 break;
d02ed0bb 2400
1ddbba9d
TT
2401 case record_full_mem: /* mem */
2402 /* Get len. */
2403 bfdcore_read (core_bfd, osec, &len,
2404 sizeof (len), &bfd_offset);
2405 len = netorder32 (len);
d02ed0bb 2406
1ddbba9d
TT
2407 /* Get addr. */
2408 bfdcore_read (core_bfd, osec, &addr,
2409 sizeof (addr), &bfd_offset);
2410 addr = netorder64 (addr);
2411
2412 rec = record_full_mem_alloc (addr, len);
2413
2414 /* Get val. */
2415 bfdcore_read (core_bfd, osec, record_full_get_loc (rec),
2416 rec->u.mem.len, &bfd_offset);
2417
2418 if (record_debug)
2419 fprintf_unfiltered (gdb_stdlog,
2420 " Reading memory %s (1 plus "
2421 "%lu plus %lu plus %d bytes)\n",
2422 paddress (get_current_arch (),
2423 rec->u.mem.addr),
2424 (unsigned long) sizeof (addr),
2425 (unsigned long) sizeof (len),
2426 rec->u.mem.len);
2427 break;
2428
2429 case record_full_end: /* end */
2430 rec = record_full_end_alloc ();
2431 record_full_insn_num ++;
2432
2433 /* Get signal value. */
2434 bfdcore_read (core_bfd, osec, &signal,
2435 sizeof (signal), &bfd_offset);
2436 signal = netorder32 (signal);
2437 rec->u.end.sigval = (enum gdb_signal) signal;
2438
2439 /* Get insn count. */
2440 bfdcore_read (core_bfd, osec, &count,
2441 sizeof (count), &bfd_offset);
2442 count = netorder32 (count);
2443 rec->u.end.insn_num = count;
2444 record_full_insn_count = count + 1;
2445 if (record_debug)
2446 fprintf_unfiltered (gdb_stdlog,
2447 " Reading record_full_end (1 + "
2448 "%lu + %lu bytes), offset == %s\n",
2449 (unsigned long) sizeof (signal),
2450 (unsigned long) sizeof (count),
2451 paddress (get_current_arch (),
2452 bfd_offset));
2453 break;
2454
2455 default:
2456 error (_("Bad entry type in core file %s."),
2457 bfd_get_filename (core_bfd));
2458 break;
2459 }
d02ed0bb 2460
1ddbba9d
TT
2461 /* Add rec to record arch list. */
2462 record_full_arch_list_add (rec);
2463 }
2464 }
230d2906 2465 catch (const gdb_exception &ex)
1ddbba9d
TT
2466 {
2467 record_full_list_release (record_full_arch_list_tail);
eedc3f4f 2468 throw;
1ddbba9d 2469 }
d02ed0bb 2470
88d1aa9d
MM
2471 /* Add record_full_arch_list_head to the end of record list. */
2472 record_full_first.next = record_full_arch_list_head;
2473 record_full_arch_list_head->prev = &record_full_first;
2474 record_full_arch_list_tail->next = NULL;
2475 record_full_list = &record_full_first;
d02ed0bb 2476
88d1aa9d
MM
2477 /* Update record_full_insn_max_num. */
2478 if (record_full_insn_num > record_full_insn_max_num)
d02ed0bb 2479 {
88d1aa9d 2480 record_full_insn_max_num = record_full_insn_num;
7ee70bf5 2481 warning (_("Auto increase record/replay buffer limit to %u."),
dda83cd7 2482 record_full_insn_max_num);
d02ed0bb
MM
2483 }
2484
2485 /* Succeeded. */
2486 printf_filtered (_("Restored records from core file %s.\n"),
2487 bfd_get_filename (core_bfd));
2488
08d72866 2489 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
d02ed0bb
MM
2490}
2491
2492/* bfdcore_write -- write bytes into a core file section. */
2493
2494static inline void
2495bfdcore_write (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2496{
2497 int ret = bfd_set_section_contents (obfd, osec, buf, *offset, len);
2498
2499 if (ret)
2500 *offset += len;
2501 else
2502 error (_("Failed to write %d bytes to core file %s ('%s')."),
2503 len, bfd_get_filename (obfd),
2504 bfd_errmsg (bfd_get_error ()));
2505}
2506
2507/* Restore the execution log from a file. We use a modified elf
2508 corefile format, with an extra section for our data. */
2509
2510static void
41243651 2511cmd_record_full_restore (const char *args, int from_tty)
d02ed0bb
MM
2512{
2513 core_file_command (args, from_tty);
d9f719f1 2514 record_full_open (args, from_tty);
d02ed0bb
MM
2515}
2516
d02ed0bb
MM
2517/* Save the execution log to a file. We use a modified elf corefile
2518 format, with an extra section for our data. */
2519
f6ac5f3d
PA
2520void
2521record_full_base_target::save_record (const char *recfilename)
d02ed0bb 2522{
88d1aa9d 2523 struct record_full_entry *cur_record_full_list;
d02ed0bb
MM
2524 uint32_t magic;
2525 struct regcache *regcache;
2526 struct gdbarch *gdbarch;
d02ed0bb
MM
2527 int save_size = 0;
2528 asection *osec = NULL;
2529 int bfd_offset = 0;
2530
2531 /* Open the save file. */
2532 if (record_debug)
2533 fprintf_unfiltered (gdb_stdlog, "Saving execution log to core file '%s'\n",
2534 recfilename);
2535
2536 /* Open the output file. */
bef155c3
TT
2537 gdb_bfd_ref_ptr obfd (create_gcore_bfd (recfilename));
2538
2539 /* Arrange to remove the output file on failure. */
2540 gdb::unlinker unlink_file (recfilename);
d02ed0bb 2541
88d1aa9d
MM
2542 /* Save the current record entry to "cur_record_full_list". */
2543 cur_record_full_list = record_full_list;
d02ed0bb
MM
2544
2545 /* Get the values of regcache and gdbarch. */
2546 regcache = get_current_regcache ();
ac7936df 2547 gdbarch = regcache->arch ();
d02ed0bb
MM
2548
2549 /* Disable the GDB operation record. */
07036511
TT
2550 scoped_restore restore_operation_disable
2551 = record_full_gdb_operation_disable_set ();
d02ed0bb
MM
2552
2553 /* Reverse execute to the begin of record list. */
2554 while (1)
2555 {
2556 /* Check for beginning and end of log. */
88d1aa9d 2557 if (record_full_list == &record_full_first)
dda83cd7 2558 break;
d02ed0bb 2559
88d1aa9d 2560 record_full_exec_insn (regcache, gdbarch, record_full_list);
d02ed0bb 2561
88d1aa9d 2562 if (record_full_list->prev)
dda83cd7 2563 record_full_list = record_full_list->prev;
d02ed0bb
MM
2564 }
2565
2566 /* Compute the size needed for the extra bfd section. */
2567 save_size = 4; /* magic cookie */
88d1aa9d
MM
2568 for (record_full_list = record_full_first.next; record_full_list;
2569 record_full_list = record_full_list->next)
2570 switch (record_full_list->type)
d02ed0bb 2571 {
88d1aa9d 2572 case record_full_end:
d02ed0bb
MM
2573 save_size += 1 + 4 + 4;
2574 break;
88d1aa9d
MM
2575 case record_full_reg:
2576 save_size += 1 + 4 + record_full_list->u.reg.len;
d02ed0bb 2577 break;
88d1aa9d
MM
2578 case record_full_mem:
2579 save_size += 1 + 4 + 8 + record_full_list->u.mem.len;
d02ed0bb
MM
2580 break;
2581 }
2582
2583 /* Make the new bfd section. */
bef155c3 2584 osec = bfd_make_section_anyway_with_flags (obfd.get (), "precord",
dda83cd7
SM
2585 SEC_HAS_CONTENTS
2586 | SEC_READONLY);
d02ed0bb
MM
2587 if (osec == NULL)
2588 error (_("Failed to create 'precord' section for corefile %s: %s"),
2589 recfilename,
dda83cd7 2590 bfd_errmsg (bfd_get_error ()));
fd361982
AM
2591 bfd_set_section_size (osec, save_size);
2592 bfd_set_section_vma (osec, 0);
2593 bfd_set_section_alignment (osec, 0);
d02ed0bb
MM
2594
2595 /* Save corefile state. */
bef155c3 2596 write_gcore_file (obfd.get ());
d02ed0bb
MM
2597
2598 /* Write out the record log. */
2599 /* Write the magic code. */
88d1aa9d 2600 magic = RECORD_FULL_FILE_MAGIC;
d02ed0bb
MM
2601 if (record_debug)
2602 fprintf_unfiltered (gdb_stdlog,
2603 " Writing 4-byte magic cookie "
88d1aa9d 2604 "RECORD_FULL_FILE_MAGIC (0x%s)\n",
d02ed0bb 2605 phex_nz (magic, 4));
bef155c3 2606 bfdcore_write (obfd.get (), osec, &magic, sizeof (magic), &bfd_offset);
d02ed0bb
MM
2607
2608 /* Save the entries to recfd and forward execute to the end of
2609 record list. */
88d1aa9d 2610 record_full_list = &record_full_first;
d02ed0bb
MM
2611 while (1)
2612 {
2613 /* Save entry. */
88d1aa9d 2614 if (record_full_list != &record_full_first)
dda83cd7 2615 {
d02ed0bb
MM
2616 uint8_t type;
2617 uint32_t regnum, len, signal, count;
dda83cd7 2618 uint64_t addr;
d02ed0bb 2619
88d1aa9d 2620 type = record_full_list->type;
dda83cd7 2621 bfdcore_write (obfd.get (), osec, &type, sizeof (type), &bfd_offset);
d02ed0bb 2622
dda83cd7
SM
2623 switch (record_full_list->type)
2624 {
2625 case record_full_reg: /* reg */
d02ed0bb
MM
2626 if (record_debug)
2627 fprintf_unfiltered (gdb_stdlog,
2628 " Writing register %d (1 "
2629 "plus %lu plus %d bytes)\n",
88d1aa9d 2630 record_full_list->u.reg.num,
d02ed0bb 2631 (unsigned long) sizeof (regnum),
88d1aa9d 2632 record_full_list->u.reg.len);
d02ed0bb 2633
dda83cd7
SM
2634 /* Write regnum. */
2635 regnum = netorder32 (record_full_list->u.reg.num);
2636 bfdcore_write (obfd.get (), osec, &regnum,
d02ed0bb
MM
2637 sizeof (regnum), &bfd_offset);
2638
dda83cd7
SM
2639 /* Write regval. */
2640 bfdcore_write (obfd.get (), osec,
88d1aa9d
MM
2641 record_full_get_loc (record_full_list),
2642 record_full_list->u.reg.len, &bfd_offset);
dda83cd7 2643 break;
d02ed0bb 2644
dda83cd7 2645 case record_full_mem: /* mem */
d02ed0bb
MM
2646 if (record_debug)
2647 fprintf_unfiltered (gdb_stdlog,
2648 " Writing memory %s (1 plus "
2649 "%lu plus %lu plus %d bytes)\n",
2650 paddress (gdbarch,
88d1aa9d 2651 record_full_list->u.mem.addr),
d02ed0bb
MM
2652 (unsigned long) sizeof (addr),
2653 (unsigned long) sizeof (len),
88d1aa9d 2654 record_full_list->u.mem.len);
d02ed0bb
MM
2655
2656 /* Write memlen. */
88d1aa9d 2657 len = netorder32 (record_full_list->u.mem.len);
bef155c3
TT
2658 bfdcore_write (obfd.get (), osec, &len, sizeof (len),
2659 &bfd_offset);
d02ed0bb
MM
2660
2661 /* Write memaddr. */
88d1aa9d 2662 addr = netorder64 (record_full_list->u.mem.addr);
bef155c3 2663 bfdcore_write (obfd.get (), osec, &addr,
d02ed0bb
MM
2664 sizeof (addr), &bfd_offset);
2665
2666 /* Write memval. */
bef155c3 2667 bfdcore_write (obfd.get (), osec,
88d1aa9d
MM
2668 record_full_get_loc (record_full_list),
2669 record_full_list->u.mem.len, &bfd_offset);
dda83cd7 2670 break;
d02ed0bb 2671
dda83cd7 2672 case record_full_end:
d02ed0bb
MM
2673 if (record_debug)
2674 fprintf_unfiltered (gdb_stdlog,
88d1aa9d 2675 " Writing record_full_end (1 + "
d02ed0bb
MM
2676 "%lu + %lu bytes)\n",
2677 (unsigned long) sizeof (signal),
2678 (unsigned long) sizeof (count));
2679 /* Write signal value. */
88d1aa9d 2680 signal = netorder32 (record_full_list->u.end.sigval);
bef155c3 2681 bfdcore_write (obfd.get (), osec, &signal,
d02ed0bb
MM
2682 sizeof (signal), &bfd_offset);
2683
2684 /* Write insn count. */
88d1aa9d 2685 count = netorder32 (record_full_list->u.end.insn_num);
bef155c3 2686 bfdcore_write (obfd.get (), osec, &count,
d02ed0bb 2687 sizeof (count), &bfd_offset);
dda83cd7
SM
2688 break;
2689 }
2690 }
d02ed0bb
MM
2691
2692 /* Execute entry. */
88d1aa9d 2693 record_full_exec_insn (regcache, gdbarch, record_full_list);
d02ed0bb 2694
88d1aa9d 2695 if (record_full_list->next)
dda83cd7 2696 record_full_list = record_full_list->next;
d02ed0bb 2697 else
dda83cd7 2698 break;
d02ed0bb
MM
2699 }
2700
88d1aa9d 2701 /* Reverse execute to cur_record_full_list. */
d02ed0bb
MM
2702 while (1)
2703 {
2704 /* Check for beginning and end of log. */
88d1aa9d 2705 if (record_full_list == cur_record_full_list)
dda83cd7 2706 break;
d02ed0bb 2707
88d1aa9d 2708 record_full_exec_insn (regcache, gdbarch, record_full_list);
d02ed0bb 2709
88d1aa9d 2710 if (record_full_list->prev)
dda83cd7 2711 record_full_list = record_full_list->prev;
d02ed0bb
MM
2712 }
2713
bef155c3 2714 unlink_file.keep ();
d02ed0bb
MM
2715
2716 /* Succeeded. */
2717 printf_filtered (_("Saved core file %s with execution log.\n"),
2718 recfilename);
2719}
2720
88d1aa9d 2721/* record_full_goto_insn -- rewind the record log (forward or backward,
d02ed0bb
MM
2722 depending on DIR) to the given entry, changing the program state
2723 correspondingly. */
2724
2725static void
88d1aa9d
MM
2726record_full_goto_insn (struct record_full_entry *entry,
2727 enum exec_direction_kind dir)
d02ed0bb 2728{
07036511
TT
2729 scoped_restore restore_operation_disable
2730 = record_full_gdb_operation_disable_set ();
d02ed0bb 2731 struct regcache *regcache = get_current_regcache ();
ac7936df 2732 struct gdbarch *gdbarch = regcache->arch ();
d02ed0bb
MM
2733
2734 /* Assume everything is valid: we will hit the entry,
2735 and we will not hit the end of the recording. */
2736
2737 if (dir == EXEC_FORWARD)
88d1aa9d 2738 record_full_list = record_full_list->next;
d02ed0bb
MM
2739
2740 do
2741 {
88d1aa9d 2742 record_full_exec_insn (regcache, gdbarch, record_full_list);
d02ed0bb 2743 if (dir == EXEC_REVERSE)
88d1aa9d 2744 record_full_list = record_full_list->prev;
d02ed0bb 2745 else
88d1aa9d
MM
2746 record_full_list = record_full_list->next;
2747 } while (record_full_list != entry);
d02ed0bb
MM
2748}
2749
2750/* Alias for "target record-full". */
2751
2752static void
981a3fb3 2753cmd_record_full_start (const char *args, int from_tty)
d02ed0bb 2754{
95a6b0a1 2755 execute_command ("target record-full", from_tty);
d02ed0bb
MM
2756}
2757
2758static void
eb4c3f4a 2759set_record_full_insn_max_num (const char *args, int from_tty,
88d1aa9d 2760 struct cmd_list_element *c)
d02ed0bb 2761{
7ee70bf5 2762 if (record_full_insn_num > record_full_insn_max_num)
d02ed0bb 2763 {
88d1aa9d
MM
2764 /* Count down record_full_insn_num while releasing records from list. */
2765 while (record_full_insn_num > record_full_insn_max_num)
d02ed0bb 2766 {
dda83cd7
SM
2767 record_full_list_release_first ();
2768 record_full_insn_num--;
d02ed0bb
MM
2769 }
2770 }
2771}
2772
6c265988 2773void _initialize_record_full ();
d02ed0bb 2774void
6c265988 2775_initialize_record_full ()
d02ed0bb
MM
2776{
2777 struct cmd_list_element *c;
2778
88d1aa9d
MM
2779 /* Init record_full_first. */
2780 record_full_first.prev = NULL;
2781 record_full_first.next = NULL;
2782 record_full_first.type = record_full_end;
d02ed0bb 2783
d9f719f1
PA
2784 add_target (record_full_target_info, record_full_open);
2785 add_deprecated_target_alias (record_full_target_info, "record");
2786 add_target (record_full_core_target_info, record_full_open);
d02ed0bb 2787
88d1aa9d 2788 add_prefix_cmd ("full", class_obscure, cmd_record_full_start,
d02ed0bb 2789 _("Start full execution recording."), &record_full_cmdlist,
2f822da5 2790 0, &record_cmdlist);
d02ed0bb 2791
5e84b7ee
SM
2792 cmd_list_element *record_full_restore_cmd
2793 = add_cmd ("restore", class_obscure, cmd_record_full_restore,
d02ed0bb
MM
2794 _("Restore the execution log from a file.\n\
2795Argument is filename. File must be created with 'record save'."),
2796 &record_full_cmdlist);
5e84b7ee 2797 set_cmd_completer (record_full_restore_cmd, filename_completer);
d02ed0bb
MM
2798
2799 /* Deprecate the old version without "full" prefix. */
5e84b7ee 2800 c = add_alias_cmd ("restore", record_full_restore_cmd, class_obscure, 1,
d02ed0bb
MM
2801 &record_cmdlist);
2802 set_cmd_completer (c, filename_completer);
2803 deprecate_cmd (c, "record full restore");
2804
0743fc83
TT
2805 add_basic_prefix_cmd ("full", class_support,
2806 _("Set record options."), &set_record_full_cmdlist,
2f822da5 2807 0, &set_record_cmdlist);
d02ed0bb 2808
0743fc83
TT
2809 add_show_prefix_cmd ("full", class_support,
2810 _("Show record options."), &show_record_full_cmdlist,
2f822da5 2811 0, &show_record_cmdlist);
d02ed0bb
MM
2812
2813 /* Record instructions number limit command. */
5e84b7ee
SM
2814 set_show_commands set_record_full_stop_at_limit_cmds
2815 = add_setshow_boolean_cmd ("stop-at-limit", no_class,
2816 &record_full_stop_at_limit, _("\
d02ed0bb
MM
2817Set whether record/replay stops when record/replay buffer becomes full."), _("\
2818Show whether record/replay stops when record/replay buffer becomes full."),
2819 _("Default is ON.\n\
2820When ON, if the record/replay buffer becomes full, ask user what to do.\n\
2821When OFF, if the record/replay buffer becomes full,\n\
2822delete the oldest recorded instruction to make room for each new one."),
5e84b7ee
SM
2823 NULL, NULL,
2824 &set_record_full_cmdlist,
2825 &show_record_full_cmdlist);
d02ed0bb 2826
5e84b7ee
SM
2827 c = add_alias_cmd ("stop-at-limit",
2828 set_record_full_stop_at_limit_cmds.set, no_class, 1,
d02ed0bb
MM
2829 &set_record_cmdlist);
2830 deprecate_cmd (c, "set record full stop-at-limit");
2831
5e84b7ee
SM
2832 c = add_alias_cmd ("stop-at-limit",
2833 set_record_full_stop_at_limit_cmds.show, no_class, 1,
d02ed0bb
MM
2834 &show_record_cmdlist);
2835 deprecate_cmd (c, "show record full stop-at-limit");
2836
5e84b7ee
SM
2837 set_show_commands record_full_insn_number_max_cmds
2838 = add_setshow_uinteger_cmd ("insn-number-max", no_class,
2839 &record_full_insn_max_num,
2840 _("Set record/replay buffer limit."),
2841 _("Show record/replay buffer limit."), _("\
d02ed0bb 2842Set the maximum number of instructions to be stored in the\n\
f81d1120
PA
2843record/replay buffer. A value of either \"unlimited\" or zero means no\n\
2844limit. Default is 200000."),
5e84b7ee
SM
2845 set_record_full_insn_max_num,
2846 NULL, &set_record_full_cmdlist,
2847 &show_record_full_cmdlist);
d02ed0bb 2848
5e84b7ee
SM
2849 c = add_alias_cmd ("insn-number-max", record_full_insn_number_max_cmds.set,
2850 no_class, 1, &set_record_cmdlist);
d02ed0bb
MM
2851 deprecate_cmd (c, "set record full insn-number-max");
2852
5e84b7ee
SM
2853 c = add_alias_cmd ("insn-number-max", record_full_insn_number_max_cmds.show,
2854 no_class, 1, &show_record_cmdlist);
d02ed0bb
MM
2855 deprecate_cmd (c, "show record full insn-number-max");
2856
5e84b7ee
SM
2857 set_show_commands record_full_memory_query_cmds
2858 = add_setshow_boolean_cmd ("memory-query", no_class,
2859 &record_full_memory_query, _("\
d02ed0bb 2860Set whether query if PREC cannot record memory change of next instruction."),
5e84b7ee 2861 _("\
d02ed0bb 2862Show whether query if PREC cannot record memory change of next instruction."),
5e84b7ee 2863 _("\
d02ed0bb
MM
2864Default is OFF.\n\
2865When ON, query if PREC cannot record memory change of next instruction."),
5e84b7ee
SM
2866 NULL, NULL,
2867 &set_record_full_cmdlist,
2868 &show_record_full_cmdlist);
d02ed0bb 2869
5e84b7ee
SM
2870 c = add_alias_cmd ("memory-query", record_full_memory_query_cmds.set,
2871 no_class, 1, &set_record_cmdlist);
d02ed0bb
MM
2872 deprecate_cmd (c, "set record full memory-query");
2873
5e84b7ee
SM
2874 c = add_alias_cmd ("memory-query", record_full_memory_query_cmds.show,
2875 no_class, 1,&show_record_cmdlist);
d02ed0bb
MM
2876 deprecate_cmd (c, "show record full memory-query");
2877}