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