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