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