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