Remove cmd_cfunc_ftype
[deliverable/binutils-gdb.git] / gdb / record-full.c
CommitLineData
d02ed0bb
MM
1/* Process record and replay target for GDB, the GNU debugger.
2
61baf725 3 Copyright (C) 2013-2017 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"
36#include "observer.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. */
88d1aa9d
MM
171static gdb_byte *record_full_core_regbuf = NULL;
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 {
394816ee
MK
705 gdb_byte *mem = (gdb_byte *) xmalloc (entry->u.mem.len);
706 struct cleanup *cleanup = make_cleanup (xfree, mem);
d02ed0bb
MM
707
708 if (record_debug > 1)
709 fprintf_unfiltered (gdb_stdlog,
88d1aa9d 710 "Process record: record_full_mem %s to "
d02ed0bb
MM
711 "inferior addr = %s len = %d.\n",
712 host_address_to_string (entry),
713 paddress (gdbarch, entry->u.mem.addr),
714 entry->u.mem.len);
715
716 if (record_read_memory (gdbarch,
717 entry->u.mem.addr, mem, entry->u.mem.len))
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 {
88d1aa9d
MM
734 memcpy (record_full_get_loc (entry), mem,
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 }
394816ee
MK
751
752 do_cleanups (cleanup);
d02ed0bb
MM
753 }
754 }
755 break;
756 }
757}
758
88d1aa9d 759static void record_full_restore (void);
d02ed0bb
MM
760
761/* Asynchronous signal handle registered as event loop source for when
762 we have pending events ready to be passed to the core. */
763
88d1aa9d 764static struct async_event_handler *record_full_async_inferior_event_token;
d02ed0bb
MM
765
766static void
88d1aa9d 767record_full_async_inferior_event_handler (gdb_client_data data)
d02ed0bb
MM
768{
769 inferior_event_handler (INF_REG_EVENT, NULL);
770}
771
772/* Open the process record target. */
773
774static void
014f9477 775record_full_core_open_1 (const char *name, int from_tty)
d02ed0bb
MM
776{
777 struct regcache *regcache = get_current_regcache ();
ac7936df 778 int regnum = gdbarch_num_regs (regcache->arch ());
d02ed0bb
MM
779 int i;
780
88d1aa9d 781 /* Get record_full_core_regbuf. */
d02ed0bb 782 target_fetch_registers (regcache, -1);
224c3ddb 783 record_full_core_regbuf = (gdb_byte *) xmalloc (MAX_REGISTER_SIZE * regnum);
d02ed0bb
MM
784 for (i = 0; i < regnum; i ++)
785 regcache_raw_collect (regcache, i,
88d1aa9d 786 record_full_core_regbuf + MAX_REGISTER_SIZE * i);
d02ed0bb 787
88d1aa9d
MM
788 /* Get record_full_core_start and record_full_core_end. */
789 if (build_section_table (core_bfd, &record_full_core_start,
790 &record_full_core_end))
d02ed0bb 791 {
88d1aa9d
MM
792 xfree (record_full_core_regbuf);
793 record_full_core_regbuf = NULL;
d02ed0bb
MM
794 error (_("\"%s\": Can't find sections: %s"),
795 bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ()));
796 }
797
88d1aa9d
MM
798 push_target (&record_full_core_ops);
799 record_full_restore ();
d02ed0bb
MM
800}
801
802/* "to_open" target method for 'live' processes. */
803
804static void
014f9477 805record_full_open_1 (const char *name, int from_tty)
d02ed0bb
MM
806{
807 if (record_debug)
0a0640e3 808 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_open_1\n");
d02ed0bb
MM
809
810 /* check exec */
811 if (!target_has_execution)
812 error (_("Process record: the program is not being run."));
813 if (non_stop)
814 error (_("Process record target can't debug inferior in non-stop mode "
815 "(non-stop)."));
816
817 if (!gdbarch_process_record_p (target_gdbarch ()))
818 error (_("Process record: the current architecture doesn't support "
819 "record function."));
820
88d1aa9d 821 push_target (&record_full_ops);
d02ed0bb
MM
822}
823
88d1aa9d 824static void record_full_init_record_breakpoints (void);
d02ed0bb
MM
825
826/* "to_open" target method. Open the process record target. */
827
828static void
014f9477 829record_full_open (const char *name, int from_tty)
d02ed0bb
MM
830{
831 struct target_ops *t;
832
833 if (record_debug)
88d1aa9d 834 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_open\n");
d02ed0bb 835
8213266a 836 record_preopen ();
d02ed0bb 837
d02ed0bb 838 /* Reset */
88d1aa9d
MM
839 record_full_insn_num = 0;
840 record_full_insn_count = 0;
841 record_full_list = &record_full_first;
842 record_full_list->next = NULL;
d02ed0bb 843
d02ed0bb 844 if (core_bfd)
88d1aa9d 845 record_full_core_open_1 (name, from_tty);
d02ed0bb 846 else
88d1aa9d 847 record_full_open_1 (name, from_tty);
d02ed0bb
MM
848
849 /* Register extra event sources in the event loop. */
88d1aa9d
MM
850 record_full_async_inferior_event_token
851 = create_async_event_handler (record_full_async_inferior_event_handler,
d02ed0bb
MM
852 NULL);
853
88d1aa9d 854 record_full_init_record_breakpoints ();
d02ed0bb 855
38b022b4 856 observer_notify_record_changed (current_inferior (), 1, "full", NULL);
d02ed0bb
MM
857}
858
859/* "to_close" target method. Close the process record target. */
860
861static void
de90e03d 862record_full_close (struct target_ops *self)
d02ed0bb 863{
88d1aa9d 864 struct record_full_core_buf_entry *entry;
d02ed0bb
MM
865
866 if (record_debug)
88d1aa9d 867 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_close\n");
d02ed0bb 868
88d1aa9d 869 record_full_list_release (record_full_list);
d02ed0bb 870
88d1aa9d
MM
871 /* Release record_full_core_regbuf. */
872 if (record_full_core_regbuf)
d02ed0bb 873 {
88d1aa9d
MM
874 xfree (record_full_core_regbuf);
875 record_full_core_regbuf = NULL;
d02ed0bb
MM
876 }
877
88d1aa9d
MM
878 /* Release record_full_core_buf_list. */
879 if (record_full_core_buf_list)
d02ed0bb 880 {
88d1aa9d
MM
881 for (entry = record_full_core_buf_list->prev; entry;
882 entry = entry->prev)
d02ed0bb 883 {
88d1aa9d
MM
884 xfree (record_full_core_buf_list);
885 record_full_core_buf_list = entry;
d02ed0bb 886 }
88d1aa9d 887 record_full_core_buf_list = NULL;
d02ed0bb
MM
888 }
889
88d1aa9d
MM
890 if (record_full_async_inferior_event_token)
891 delete_async_event_handler (&record_full_async_inferior_event_token);
d02ed0bb
MM
892}
893
b7d2e916
PA
894/* "to_async" target method. */
895
896static void
6a3753b3 897record_full_async (struct target_ops *ops, int enable)
b7d2e916 898{
6a3753b3 899 if (enable)
b7d2e916
PA
900 mark_async_event_handler (record_full_async_inferior_event_token);
901 else
902 clear_async_event_handler (record_full_async_inferior_event_token);
903
6a3753b3 904 ops->beneath->to_async (ops->beneath, enable);
b7d2e916
PA
905}
906
88d1aa9d 907static int record_full_resume_step = 0;
d02ed0bb 908
88d1aa9d
MM
909/* True if we've been resumed, and so each record_full_wait call should
910 advance execution. If this is false, record_full_wait will return a
d02ed0bb 911 TARGET_WAITKIND_IGNORE. */
88d1aa9d 912static int record_full_resumed = 0;
d02ed0bb
MM
913
914/* The execution direction of the last resume we got. This is
915 necessary for async mode. Vis (order is not strictly accurate):
916
917 1. user has the global execution direction set to forward
918 2. user does a reverse-step command
88d1aa9d 919 3. record_full_resume is called with global execution direction
d02ed0bb
MM
920 temporarily switched to reverse
921 4. GDB's execution direction is reverted back to forward
922 5. target record notifies event loop there's an event to handle
923 6. infrun asks the target which direction was it going, and switches
924 the global execution direction accordingly (to reverse)
925 7. infrun polls an event out of the record target, and handles it
926 8. GDB goes back to the event loop, and goto #4.
927*/
88d1aa9d 928static enum exec_direction_kind record_full_execution_dir = EXEC_FORWARD;
d02ed0bb
MM
929
930/* "to_resume" target method. Resume the process record target. */
931
932static void
88d1aa9d
MM
933record_full_resume (struct target_ops *ops, ptid_t ptid, int step,
934 enum gdb_signal signal)
d02ed0bb 935{
88d1aa9d
MM
936 record_full_resume_step = step;
937 record_full_resumed = 1;
938 record_full_execution_dir = execution_direction;
d02ed0bb 939
88d1aa9d 940 if (!RECORD_FULL_IS_REPLAY)
d02ed0bb
MM
941 {
942 struct gdbarch *gdbarch = target_thread_architecture (ptid);
943
88d1aa9d 944 record_full_message (get_current_regcache (), signal);
d02ed0bb
MM
945
946 if (!step)
947 {
948 /* This is not hard single step. */
949 if (!gdbarch_software_single_step_p (gdbarch))
950 {
951 /* This is a normal continue. */
952 step = 1;
953 }
954 else
955 {
8ce9efb0 956 /* This arch supports soft single step. */
34b7e8a6 957 if (thread_has_single_step_breakpoints_set (inferior_thread ()))
d02ed0bb
MM
958 {
959 /* This is a soft single step. */
88d1aa9d 960 record_full_resume_step = 1;
d02ed0bb
MM
961 }
962 else
93f9a11f 963 step = !insert_single_step_breakpoints (gdbarch);
d02ed0bb
MM
964 }
965 }
966
967 /* Make sure the target beneath reports all signals. */
968 target_pass_signals (0, NULL);
969
6b84065d 970 ops->beneath->to_resume (ops->beneath, ptid, step, signal);
d02ed0bb
MM
971 }
972
973 /* We are about to start executing the inferior (or simulate it),
974 let's register it with the event loop. */
975 if (target_can_async_p ())
6a3753b3 976 target_async (1);
d02ed0bb
MM
977}
978
85ad3aaf
PA
979/* "to_commit_resume" method for process record target. */
980
981static void
982record_full_commit_resume (struct target_ops *ops)
983{
984 if (!RECORD_FULL_IS_REPLAY)
985 ops->beneath->to_commit_resume (ops->beneath);
986}
987
88d1aa9d 988static int record_full_get_sig = 0;
d02ed0bb
MM
989
990/* SIGINT signal handler, registered by "to_wait" method. */
991
992static void
88d1aa9d 993record_full_sig_handler (int signo)
d02ed0bb
MM
994{
995 if (record_debug)
996 fprintf_unfiltered (gdb_stdlog, "Process record: get a signal\n");
997
998 /* It will break the running inferior in replay mode. */
88d1aa9d 999 record_full_resume_step = 1;
d02ed0bb 1000
88d1aa9d 1001 /* It will let record_full_wait set inferior status to get the signal
d02ed0bb 1002 SIGINT. */
88d1aa9d 1003 record_full_get_sig = 1;
d02ed0bb
MM
1004}
1005
1006static void
88d1aa9d 1007record_full_wait_cleanups (void *ignore)
d02ed0bb
MM
1008{
1009 if (execution_direction == EXEC_REVERSE)
1010 {
88d1aa9d
MM
1011 if (record_full_list->next)
1012 record_full_list = record_full_list->next;
d02ed0bb
MM
1013 }
1014 else
88d1aa9d 1015 record_full_list = record_full_list->prev;
d02ed0bb
MM
1016}
1017
1018/* "to_wait" target method for process record target.
1019
1020 In record mode, the target is always run in singlestep mode
1021 (even when gdb says to continue). The to_wait method intercepts
1022 the stop events and determines which ones are to be passed on to
1023 gdb. Most stop events are just singlestep events that gdb is not
1024 to know about, so the to_wait method just records them and keeps
1025 singlestepping.
1026
1027 In replay mode, this function emulates the recorded execution log,
1028 one instruction at a time (forward or backward), and determines
1029 where to stop. */
1030
1031static ptid_t
88d1aa9d
MM
1032record_full_wait_1 (struct target_ops *ops,
1033 ptid_t ptid, struct target_waitstatus *status,
1034 int options)
d02ed0bb 1035{
07036511
TT
1036 scoped_restore restore_operation_disable
1037 = record_full_gdb_operation_disable_set ();
d02ed0bb
MM
1038
1039 if (record_debug)
1040 fprintf_unfiltered (gdb_stdlog,
88d1aa9d
MM
1041 "Process record: record_full_wait "
1042 "record_full_resume_step = %d, "
1043 "record_full_resumed = %d, direction=%s\n",
1044 record_full_resume_step, record_full_resumed,
1045 record_full_execution_dir == EXEC_FORWARD
1046 ? "forward" : "reverse");
1047
1048 if (!record_full_resumed)
d02ed0bb
MM
1049 {
1050 gdb_assert ((options & TARGET_WNOHANG) != 0);
1051
1052 /* No interesting event. */
1053 status->kind = TARGET_WAITKIND_IGNORE;
1054 return minus_one_ptid;
1055 }
1056
88d1aa9d
MM
1057 record_full_get_sig = 0;
1058 signal (SIGINT, record_full_sig_handler);
d02ed0bb 1059
9e8915c6
PA
1060 record_full_stop_reason = TARGET_STOPPED_BY_NO_REASON;
1061
88d1aa9d 1062 if (!RECORD_FULL_IS_REPLAY && ops != &record_full_core_ops)
d02ed0bb 1063 {
88d1aa9d 1064 if (record_full_resume_step)
d02ed0bb
MM
1065 {
1066 /* This is a single step. */
6b84065d 1067 return ops->beneath->to_wait (ops->beneath, ptid, status, options);
d02ed0bb
MM
1068 }
1069 else
1070 {
1071 /* This is not a single step. */
1072 ptid_t ret;
1073 CORE_ADDR tmp_pc;
1074 struct gdbarch *gdbarch = target_thread_architecture (inferior_ptid);
1075
1076 while (1)
1077 {
34b7e8a6
PA
1078 struct thread_info *tp;
1079
6b84065d 1080 ret = ops->beneath->to_wait (ops->beneath, ptid, status, options);
d02ed0bb
MM
1081 if (status->kind == TARGET_WAITKIND_IGNORE)
1082 {
1083 if (record_debug)
1084 fprintf_unfiltered (gdb_stdlog,
88d1aa9d 1085 "Process record: record_full_wait "
d02ed0bb
MM
1086 "target beneath not done yet\n");
1087 return ret;
1088 }
1089
34b7e8a6
PA
1090 ALL_NON_EXITED_THREADS (tp)
1091 delete_single_step_breakpoints (tp);
d02ed0bb 1092
88d1aa9d 1093 if (record_full_resume_step)
d02ed0bb
MM
1094 return ret;
1095
1096 /* Is this a SIGTRAP? */
1097 if (status->kind == TARGET_WAITKIND_STOPPED
1098 && status->value.sig == GDB_SIGNAL_TRAP)
1099 {
1100 struct regcache *regcache;
9e8915c6
PA
1101 enum target_stop_reason *stop_reason_p
1102 = &record_full_stop_reason;
d02ed0bb
MM
1103
1104 /* Yes -- this is likely our single-step finishing,
1105 but check if there's any reason the core would be
1106 interested in the event. */
1107
1108 registers_changed ();
1109 regcache = get_current_regcache ();
1110 tmp_pc = regcache_read_pc (regcache);
8b86c959 1111 const struct address_space *aspace = regcache->aspace ();
d02ed0bb
MM
1112
1113 if (target_stopped_by_watchpoint ())
1114 {
1115 /* Always interested in watchpoints. */
1116 }
9e8915c6
PA
1117 else if (record_check_stopped_by_breakpoint (aspace, tmp_pc,
1118 stop_reason_p))
d02ed0bb
MM
1119 {
1120 /* There is a breakpoint here. Let the core
1121 handle it. */
d02ed0bb
MM
1122 }
1123 else
1124 {
1125 /* This is a single-step trap. Record the
1126 insn and issue another step.
1127 FIXME: this part can be a random SIGTRAP too.
1128 But GDB cannot handle it. */
1129 int step = 1;
1130
88d1aa9d
MM
1131 if (!record_full_message_wrapper_safe (regcache,
1132 GDB_SIGNAL_0))
d02ed0bb
MM
1133 {
1134 status->kind = TARGET_WAITKIND_STOPPED;
1135 status->value.sig = GDB_SIGNAL_0;
1136 break;
1137 }
1138
1139 if (gdbarch_software_single_step_p (gdbarch))
1140 {
1141 /* Try to insert the software single step breakpoint.
1142 If insert success, set step to 0. */
1143 set_executing (inferior_ptid, 0);
1144 reinit_frame_cache ();
93f9a11f
YQ
1145
1146 step = !insert_single_step_breakpoints (gdbarch);
1147
d02ed0bb
MM
1148 set_executing (inferior_ptid, 1);
1149 }
1150
1151 if (record_debug)
1152 fprintf_unfiltered (gdb_stdlog,
88d1aa9d
MM
1153 "Process record: record_full_wait "
1154 "issuing one more step in the "
1155 "target beneath\n");
6b84065d
TT
1156 ops->beneath->to_resume (ops->beneath, ptid, step,
1157 GDB_SIGNAL_0);
85ad3aaf 1158 ops->beneath->to_commit_resume (ops->beneath);
d02ed0bb
MM
1159 continue;
1160 }
1161 }
1162
1163 /* The inferior is broken by a breakpoint or a signal. */
1164 break;
1165 }
1166
1167 return ret;
1168 }
1169 }
1170 else
1171 {
1172 struct regcache *regcache = get_current_regcache ();
ac7936df 1173 struct gdbarch *gdbarch = regcache->arch ();
8b86c959 1174 const struct address_space *aspace = regcache->aspace ();
d02ed0bb 1175 int continue_flag = 1;
88d1aa9d
MM
1176 int first_record_full_end = 1;
1177 struct cleanup *old_cleanups
1178 = make_cleanup (record_full_wait_cleanups, 0);
d02ed0bb
MM
1179 CORE_ADDR tmp_pc;
1180
9e8915c6 1181 record_full_stop_reason = TARGET_STOPPED_BY_NO_REASON;
d02ed0bb
MM
1182 status->kind = TARGET_WAITKIND_STOPPED;
1183
1184 /* Check breakpoint when forward execute. */
1185 if (execution_direction == EXEC_FORWARD)
1186 {
1187 tmp_pc = regcache_read_pc (regcache);
9e8915c6
PA
1188 if (record_check_stopped_by_breakpoint (aspace, tmp_pc,
1189 &record_full_stop_reason))
d02ed0bb 1190 {
d02ed0bb
MM
1191 if (record_debug)
1192 fprintf_unfiltered (gdb_stdlog,
1193 "Process record: break at %s.\n",
1194 paddress (gdbarch, tmp_pc));
d02ed0bb
MM
1195 goto replay_out;
1196 }
1197 }
1198
1199 /* If GDB is in terminal_inferior mode, it will not get the signal.
1200 And in GDB replay mode, GDB doesn't need to be in terminal_inferior
1201 mode, because inferior will not executed.
1202 Then set it to terminal_ours to make GDB get the signal. */
223ffa71 1203 target_terminal::ours ();
d02ed0bb 1204
88d1aa9d 1205 /* In EXEC_FORWARD mode, record_full_list points to the tail of prev
d02ed0bb 1206 instruction. */
88d1aa9d
MM
1207 if (execution_direction == EXEC_FORWARD && record_full_list->next)
1208 record_full_list = record_full_list->next;
d02ed0bb 1209
88d1aa9d 1210 /* Loop over the record_full_list, looking for the next place to
d02ed0bb
MM
1211 stop. */
1212 do
1213 {
1214 /* Check for beginning and end of log. */
1215 if (execution_direction == EXEC_REVERSE
88d1aa9d 1216 && record_full_list == &record_full_first)
d02ed0bb
MM
1217 {
1218 /* Hit beginning of record log in reverse. */
1219 status->kind = TARGET_WAITKIND_NO_HISTORY;
1220 break;
1221 }
88d1aa9d 1222 if (execution_direction != EXEC_REVERSE && !record_full_list->next)
d02ed0bb
MM
1223 {
1224 /* Hit end of record log going forward. */
1225 status->kind = TARGET_WAITKIND_NO_HISTORY;
1226 break;
1227 }
1228
88d1aa9d 1229 record_full_exec_insn (regcache, gdbarch, record_full_list);
d02ed0bb 1230
88d1aa9d 1231 if (record_full_list->type == record_full_end)
d02ed0bb
MM
1232 {
1233 if (record_debug > 1)
1234 fprintf_unfiltered (gdb_stdlog,
88d1aa9d 1235 "Process record: record_full_end %s to "
d02ed0bb 1236 "inferior.\n",
88d1aa9d 1237 host_address_to_string (record_full_list));
d02ed0bb 1238
88d1aa9d 1239 if (first_record_full_end && execution_direction == EXEC_REVERSE)
d02ed0bb 1240 {
88d1aa9d
MM
1241 /* When reverse excute, the first record_full_end is the
1242 part of current instruction. */
1243 first_record_full_end = 0;
d02ed0bb
MM
1244 }
1245 else
1246 {
88d1aa9d 1247 /* In EXEC_REVERSE mode, this is the record_full_end of prev
d02ed0bb 1248 instruction.
88d1aa9d
MM
1249 In EXEC_FORWARD mode, this is the record_full_end of
1250 current instruction. */
d02ed0bb 1251 /* step */
88d1aa9d 1252 if (record_full_resume_step)
d02ed0bb
MM
1253 {
1254 if (record_debug > 1)
1255 fprintf_unfiltered (gdb_stdlog,
1256 "Process record: step.\n");
1257 continue_flag = 0;
1258 }
1259
1260 /* check breakpoint */
1261 tmp_pc = regcache_read_pc (regcache);
9e8915c6
PA
1262 if (record_check_stopped_by_breakpoint (aspace, tmp_pc,
1263 &record_full_stop_reason))
d02ed0bb 1264 {
d02ed0bb
MM
1265 if (record_debug)
1266 fprintf_unfiltered (gdb_stdlog,
1267 "Process record: break "
1268 "at %s.\n",
1269 paddress (gdbarch, tmp_pc));
9e8915c6 1270
d02ed0bb
MM
1271 continue_flag = 0;
1272 }
1273
9e8915c6 1274 if (record_full_stop_reason == TARGET_STOPPED_BY_WATCHPOINT)
d02ed0bb
MM
1275 {
1276 if (record_debug)
1277 fprintf_unfiltered (gdb_stdlog,
1278 "Process record: hit hw "
1279 "watchpoint.\n");
1280 continue_flag = 0;
1281 }
1282 /* Check target signal */
88d1aa9d 1283 if (record_full_list->u.end.sigval != GDB_SIGNAL_0)
d02ed0bb
MM
1284 /* FIXME: better way to check */
1285 continue_flag = 0;
1286 }
1287 }
1288
1289 if (continue_flag)
1290 {
1291 if (execution_direction == EXEC_REVERSE)
1292 {
88d1aa9d
MM
1293 if (record_full_list->prev)
1294 record_full_list = record_full_list->prev;
d02ed0bb
MM
1295 }
1296 else
1297 {
88d1aa9d
MM
1298 if (record_full_list->next)
1299 record_full_list = record_full_list->next;
d02ed0bb
MM
1300 }
1301 }
1302 }
1303 while (continue_flag);
1304
1305replay_out:
88d1aa9d 1306 if (record_full_get_sig)
d02ed0bb 1307 status->value.sig = GDB_SIGNAL_INT;
88d1aa9d 1308 else if (record_full_list->u.end.sigval != GDB_SIGNAL_0)
d02ed0bb 1309 /* FIXME: better way to check */
88d1aa9d 1310 status->value.sig = record_full_list->u.end.sigval;
d02ed0bb
MM
1311 else
1312 status->value.sig = GDB_SIGNAL_TRAP;
1313
1314 discard_cleanups (old_cleanups);
1315 }
1316
1317 signal (SIGINT, handle_sigint);
1318
d02ed0bb
MM
1319 return inferior_ptid;
1320}
1321
1322static ptid_t
88d1aa9d
MM
1323record_full_wait (struct target_ops *ops,
1324 ptid_t ptid, struct target_waitstatus *status,
1325 int options)
d02ed0bb
MM
1326{
1327 ptid_t return_ptid;
1328
88d1aa9d 1329 return_ptid = record_full_wait_1 (ops, ptid, status, options);
d02ed0bb
MM
1330 if (status->kind != TARGET_WAITKIND_IGNORE)
1331 {
1332 /* We're reporting a stop. Make sure any spurious
1333 target_wait(WNOHANG) doesn't advance the target until the
1334 core wants us resumed again. */
88d1aa9d 1335 record_full_resumed = 0;
d02ed0bb
MM
1336 }
1337 return return_ptid;
1338}
1339
1340static int
6a109b6b 1341record_full_stopped_by_watchpoint (struct target_ops *ops)
d02ed0bb 1342{
88d1aa9d 1343 if (RECORD_FULL_IS_REPLAY)
9e8915c6 1344 return record_full_stop_reason == TARGET_STOPPED_BY_WATCHPOINT;
d02ed0bb 1345 else
6b84065d 1346 return ops->beneath->to_stopped_by_watchpoint (ops->beneath);
d02ed0bb
MM
1347}
1348
d02ed0bb 1349static int
88d1aa9d 1350record_full_stopped_data_address (struct target_ops *ops, CORE_ADDR *addr_p)
d02ed0bb 1351{
88d1aa9d 1352 if (RECORD_FULL_IS_REPLAY)
d02ed0bb
MM
1353 return 0;
1354 else
6b84065d 1355 return ops->beneath->to_stopped_data_address (ops->beneath, addr_p);
d02ed0bb
MM
1356}
1357
9e8915c6
PA
1358/* The to_stopped_by_sw_breakpoint method of target record-full. */
1359
1360static int
1361record_full_stopped_by_sw_breakpoint (struct target_ops *ops)
1362{
1363 return record_full_stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT;
1364}
1365
1366/* The to_supports_stopped_by_sw_breakpoint method of target
1367 record-full. */
1368
1369static int
1370record_full_supports_stopped_by_sw_breakpoint (struct target_ops *ops)
1371{
1372 return 1;
1373}
1374
1375/* The to_stopped_by_hw_breakpoint method of target record-full. */
1376
1377static int
1378record_full_stopped_by_hw_breakpoint (struct target_ops *ops)
1379{
1380 return record_full_stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT;
1381}
1382
1383/* The to_supports_stopped_by_sw_breakpoint method of target
1384 record-full. */
1385
1386static int
1387record_full_supports_stopped_by_hw_breakpoint (struct target_ops *ops)
1388{
1389 return 1;
1390}
1391
d02ed0bb
MM
1392/* Record registers change (by user or by GDB) to list as an instruction. */
1393
1394static void
88d1aa9d 1395record_full_registers_change (struct regcache *regcache, int regnum)
d02ed0bb 1396{
88d1aa9d 1397 /* Check record_full_insn_num. */
651ce16a 1398 record_full_check_insn_num ();
d02ed0bb 1399
88d1aa9d
MM
1400 record_full_arch_list_head = NULL;
1401 record_full_arch_list_tail = NULL;
d02ed0bb
MM
1402
1403 if (regnum < 0)
1404 {
1405 int i;
1406
ac7936df 1407 for (i = 0; i < gdbarch_num_regs (regcache->arch ()); i++)
d02ed0bb 1408 {
25ea693b 1409 if (record_full_arch_list_add_reg (regcache, i))
d02ed0bb 1410 {
88d1aa9d 1411 record_full_list_release (record_full_arch_list_tail);
d02ed0bb
MM
1412 error (_("Process record: failed to record execution log."));
1413 }
1414 }
1415 }
1416 else
1417 {
25ea693b 1418 if (record_full_arch_list_add_reg (regcache, regnum))
d02ed0bb 1419 {
88d1aa9d 1420 record_full_list_release (record_full_arch_list_tail);
d02ed0bb
MM
1421 error (_("Process record: failed to record execution log."));
1422 }
1423 }
25ea693b 1424 if (record_full_arch_list_add_end ())
d02ed0bb 1425 {
88d1aa9d 1426 record_full_list_release (record_full_arch_list_tail);
d02ed0bb
MM
1427 error (_("Process record: failed to record execution log."));
1428 }
88d1aa9d
MM
1429 record_full_list->next = record_full_arch_list_head;
1430 record_full_arch_list_head->prev = record_full_list;
1431 record_full_list = record_full_arch_list_tail;
d02ed0bb 1432
7ee70bf5 1433 if (record_full_insn_num == record_full_insn_max_num)
88d1aa9d 1434 record_full_list_release_first ();
d02ed0bb 1435 else
88d1aa9d 1436 record_full_insn_num++;
d02ed0bb
MM
1437}
1438
1439/* "to_store_registers" method for process record target. */
1440
1441static void
88d1aa9d
MM
1442record_full_store_registers (struct target_ops *ops,
1443 struct regcache *regcache,
1444 int regno)
d02ed0bb 1445{
88d1aa9d 1446 if (!record_full_gdb_operation_disable)
d02ed0bb 1447 {
88d1aa9d 1448 if (RECORD_FULL_IS_REPLAY)
d02ed0bb
MM
1449 {
1450 int n;
1451
1452 /* Let user choose if he wants to write register or not. */
1453 if (regno < 0)
1454 n =
1455 query (_("Because GDB is in replay mode, changing the "
1456 "value of a register will make the execution "
1457 "log unusable from this point onward. "
1458 "Change all registers?"));
1459 else
1460 n =
1461 query (_("Because GDB is in replay mode, changing the value "
1462 "of a register will make the execution log unusable "
1463 "from this point onward. Change register %s?"),
ac7936df 1464 gdbarch_register_name (regcache->arch (),
d02ed0bb
MM
1465 regno));
1466
1467 if (!n)
1468 {
1469 /* Invalidate the value of regcache that was set in function
1470 "regcache_raw_write". */
1471 if (regno < 0)
1472 {
1473 int i;
1474
1475 for (i = 0;
ac7936df 1476 i < gdbarch_num_regs (regcache->arch ());
d02ed0bb
MM
1477 i++)
1478 regcache_invalidate (regcache, i);
1479 }
1480 else
1481 regcache_invalidate (regcache, regno);
1482
1483 error (_("Process record canceled the operation."));
1484 }
1485
1486 /* Destroy the record from here forward. */
88d1aa9d 1487 record_full_list_release_following (record_full_list);
d02ed0bb
MM
1488 }
1489
88d1aa9d 1490 record_full_registers_change (regcache, regno);
d02ed0bb 1491 }
6b84065d 1492 ops->beneath->to_store_registers (ops->beneath, regcache, regno);
d02ed0bb
MM
1493}
1494
88d1aa9d
MM
1495/* "to_xfer_partial" method. Behavior is conditional on
1496 RECORD_FULL_IS_REPLAY.
d02ed0bb
MM
1497 In replay mode, we cannot write memory unles we are willing to
1498 invalidate the record/replay log from this point forward. */
1499
9b409511 1500static enum target_xfer_status
88d1aa9d
MM
1501record_full_xfer_partial (struct target_ops *ops, enum target_object object,
1502 const char *annex, gdb_byte *readbuf,
1503 const gdb_byte *writebuf, ULONGEST offset,
9b409511 1504 ULONGEST len, ULONGEST *xfered_len)
d02ed0bb 1505{
88d1aa9d 1506 if (!record_full_gdb_operation_disable
d02ed0bb
MM
1507 && (object == TARGET_OBJECT_MEMORY
1508 || object == TARGET_OBJECT_RAW_MEMORY) && writebuf)
1509 {
88d1aa9d 1510 if (RECORD_FULL_IS_REPLAY)
d02ed0bb
MM
1511 {
1512 /* Let user choose if he wants to write memory or not. */
1513 if (!query (_("Because GDB is in replay mode, writing to memory "
1514 "will make the execution log unusable from this "
1515 "point onward. Write memory at address %s?"),
1516 paddress (target_gdbarch (), offset)))
1517 error (_("Process record canceled the operation."));
1518
1519 /* Destroy the record from here forward. */
88d1aa9d 1520 record_full_list_release_following (record_full_list);
d02ed0bb
MM
1521 }
1522
88d1aa9d 1523 /* Check record_full_insn_num */
651ce16a 1524 record_full_check_insn_num ();
d02ed0bb
MM
1525
1526 /* Record registers change to list as an instruction. */
88d1aa9d
MM
1527 record_full_arch_list_head = NULL;
1528 record_full_arch_list_tail = NULL;
25ea693b 1529 if (record_full_arch_list_add_mem (offset, len))
d02ed0bb 1530 {
88d1aa9d 1531 record_full_list_release (record_full_arch_list_tail);
d02ed0bb
MM
1532 if (record_debug)
1533 fprintf_unfiltered (gdb_stdlog,
1534 "Process record: failed to record "
1535 "execution log.");
2ed4b548 1536 return TARGET_XFER_E_IO;
d02ed0bb 1537 }
25ea693b 1538 if (record_full_arch_list_add_end ())
d02ed0bb 1539 {
88d1aa9d 1540 record_full_list_release (record_full_arch_list_tail);
d02ed0bb
MM
1541 if (record_debug)
1542 fprintf_unfiltered (gdb_stdlog,
1543 "Process record: failed to record "
1544 "execution log.");
2ed4b548 1545 return TARGET_XFER_E_IO;
d02ed0bb 1546 }
88d1aa9d
MM
1547 record_full_list->next = record_full_arch_list_head;
1548 record_full_arch_list_head->prev = record_full_list;
1549 record_full_list = record_full_arch_list_tail;
d02ed0bb 1550
7ee70bf5 1551 if (record_full_insn_num == record_full_insn_max_num)
88d1aa9d 1552 record_full_list_release_first ();
d02ed0bb 1553 else
88d1aa9d 1554 record_full_insn_num++;
d02ed0bb
MM
1555 }
1556
6b84065d
TT
1557 return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
1558 readbuf, writebuf, offset,
1559 len, xfered_len);
d02ed0bb
MM
1560}
1561
1562/* This structure represents a breakpoint inserted while the record
1563 target is active. We use this to know when to install/remove
1564 breakpoints in/from the target beneath. For example, a breakpoint
1565 may be inserted while recording, but removed when not replaying nor
1566 recording. In that case, the breakpoint had not been inserted on
1567 the target beneath, so we should not try to remove it there. */
1568
88d1aa9d 1569struct record_full_breakpoint
d02ed0bb
MM
1570{
1571 /* The address and address space the breakpoint was set at. */
1572 struct address_space *address_space;
1573 CORE_ADDR addr;
1574
1575 /* True when the breakpoint has been also installed in the target
1576 beneath. This will be false for breakpoints set during replay or
1577 when recording. */
1578 int in_target_beneath;
1579};
1580
88d1aa9d
MM
1581typedef struct record_full_breakpoint *record_full_breakpoint_p;
1582DEF_VEC_P(record_full_breakpoint_p);
d02ed0bb
MM
1583
1584/* The list of breakpoints inserted while the record target is
1585 active. */
88d1aa9d 1586VEC(record_full_breakpoint_p) *record_full_breakpoints = NULL;
d02ed0bb
MM
1587
1588static void
88d1aa9d 1589record_full_sync_record_breakpoints (struct bp_location *loc, void *data)
d02ed0bb
MM
1590{
1591 if (loc->loc_type != bp_loc_software_breakpoint)
1592 return;
1593
1594 if (loc->inserted)
1595 {
88d1aa9d 1596 struct record_full_breakpoint *bp = XNEW (struct record_full_breakpoint);
d02ed0bb
MM
1597
1598 bp->addr = loc->target_info.placed_address;
1599 bp->address_space = loc->target_info.placed_address_space;
1600
1601 bp->in_target_beneath = 1;
1602
88d1aa9d 1603 VEC_safe_push (record_full_breakpoint_p, record_full_breakpoints, bp);
d02ed0bb
MM
1604 }
1605}
1606
88d1aa9d 1607/* Sync existing breakpoints to record_full_breakpoints. */
d02ed0bb
MM
1608
1609static void
88d1aa9d 1610record_full_init_record_breakpoints (void)
d02ed0bb 1611{
88d1aa9d 1612 VEC_free (record_full_breakpoint_p, record_full_breakpoints);
d02ed0bb 1613
88d1aa9d 1614 iterate_over_bp_locations (record_full_sync_record_breakpoints);
d02ed0bb
MM
1615}
1616
88d1aa9d 1617/* Behavior is conditional on RECORD_FULL_IS_REPLAY. We will not actually
d02ed0bb
MM
1618 insert or remove breakpoints in the real target when replaying, nor
1619 when recording. */
1620
1621static int
3db08215
MM
1622record_full_insert_breakpoint (struct target_ops *ops,
1623 struct gdbarch *gdbarch,
88d1aa9d 1624 struct bp_target_info *bp_tgt)
d02ed0bb 1625{
88d1aa9d 1626 struct record_full_breakpoint *bp;
d02ed0bb 1627 int in_target_beneath = 0;
e390720b 1628 int ix;
d02ed0bb 1629
88d1aa9d 1630 if (!RECORD_FULL_IS_REPLAY)
d02ed0bb
MM
1631 {
1632 /* When recording, we currently always single-step, so we don't
1633 really need to install regular breakpoints in the inferior.
1634 However, we do have to insert software single-step
1635 breakpoints, in case the target can't hardware step. To keep
f99bd5f2 1636 things simple, we always insert. */
d02ed0bb
MM
1637 int ret;
1638
07036511
TT
1639 scoped_restore restore_operation_disable
1640 = record_full_gdb_operation_disable_set ();
6b84065d 1641 ret = ops->beneath->to_insert_breakpoint (ops->beneath, gdbarch, bp_tgt);
d02ed0bb
MM
1642
1643 if (ret != 0)
1644 return ret;
1645
1646 in_target_beneath = 1;
1647 }
1648
e390720b
YQ
1649 /* Use the existing entries if found in order to avoid duplication
1650 in record_full_breakpoints. */
1651
1652 for (ix = 0;
1653 VEC_iterate (record_full_breakpoint_p,
1654 record_full_breakpoints, ix, bp);
1655 ++ix)
1656 {
1657 if (bp->addr == bp_tgt->placed_address
1658 && bp->address_space == bp_tgt->placed_address_space)
1659 {
1660 gdb_assert (bp->in_target_beneath == in_target_beneath);
1661 return 0;
1662 }
1663 }
1664
88d1aa9d 1665 bp = XNEW (struct record_full_breakpoint);
d02ed0bb
MM
1666 bp->addr = bp_tgt->placed_address;
1667 bp->address_space = bp_tgt->placed_address_space;
1668 bp->in_target_beneath = in_target_beneath;
88d1aa9d 1669 VEC_safe_push (record_full_breakpoint_p, record_full_breakpoints, bp);
d02ed0bb
MM
1670 return 0;
1671}
1672
1673/* "to_remove_breakpoint" method for process record target. */
1674
1675static int
3db08215
MM
1676record_full_remove_breakpoint (struct target_ops *ops,
1677 struct gdbarch *gdbarch,
73971819
PA
1678 struct bp_target_info *bp_tgt,
1679 enum remove_bp_reason reason)
d02ed0bb 1680{
88d1aa9d 1681 struct record_full_breakpoint *bp;
d02ed0bb
MM
1682 int ix;
1683
1684 for (ix = 0;
88d1aa9d
MM
1685 VEC_iterate (record_full_breakpoint_p,
1686 record_full_breakpoints, ix, bp);
d02ed0bb
MM
1687 ++ix)
1688 {
1689 if (bp->addr == bp_tgt->placed_address
1690 && bp->address_space == bp_tgt->placed_address_space)
1691 {
1692 if (bp->in_target_beneath)
1693 {
d02ed0bb
MM
1694 int ret;
1695
07036511
TT
1696 scoped_restore restore_operation_disable
1697 = record_full_gdb_operation_disable_set ();
6b84065d 1698 ret = ops->beneath->to_remove_breakpoint (ops->beneath, gdbarch,
73971819 1699 bp_tgt, reason);
d02ed0bb
MM
1700 if (ret != 0)
1701 return ret;
1702 }
1703
01d3dedf
PA
1704 if (reason == REMOVE_BREAKPOINT)
1705 {
1706 VEC_unordered_remove (record_full_breakpoint_p,
1707 record_full_breakpoints, ix);
1708 }
d02ed0bb
MM
1709 return 0;
1710 }
1711 }
1712
1713 gdb_assert_not_reached ("removing unknown breakpoint");
1714}
1715
1716/* "to_can_execute_reverse" method for process record target. */
1717
1718static int
19db3e69 1719record_full_can_execute_reverse (struct target_ops *self)
d02ed0bb
MM
1720{
1721 return 1;
1722}
1723
1724/* "to_get_bookmark" method for process record and prec over core. */
1725
1726static gdb_byte *
c2bcbb1d
TT
1727record_full_get_bookmark (struct target_ops *self, const char *args,
1728 int from_tty)
d02ed0bb 1729{
0f928d68 1730 char *ret = NULL;
d02ed0bb
MM
1731
1732 /* Return stringified form of instruction count. */
88d1aa9d
MM
1733 if (record_full_list && record_full_list->type == record_full_end)
1734 ret = xstrdup (pulongest (record_full_list->u.end.insn_num));
d02ed0bb
MM
1735
1736 if (record_debug)
1737 {
1738 if (ret)
1739 fprintf_unfiltered (gdb_stdlog,
88d1aa9d 1740 "record_full_get_bookmark returns %s\n", ret);
d02ed0bb
MM
1741 else
1742 fprintf_unfiltered (gdb_stdlog,
88d1aa9d 1743 "record_full_get_bookmark returns NULL\n");
d02ed0bb 1744 }
0f928d68 1745 return (gdb_byte *) ret;
d02ed0bb
MM
1746}
1747
1748/* "to_goto_bookmark" method for process record and prec over core. */
1749
1750static void
3c80fb48 1751record_full_goto_bookmark (struct target_ops *self,
c2bcbb1d 1752 const gdb_byte *raw_bookmark, int from_tty)
d02ed0bb 1753{
c2bcbb1d
TT
1754 const char *bookmark = (const char *) raw_bookmark;
1755 struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
0f928d68 1756
d02ed0bb
MM
1757 if (record_debug)
1758 fprintf_unfiltered (gdb_stdlog,
88d1aa9d 1759 "record_full_goto_bookmark receives %s\n", bookmark);
d02ed0bb
MM
1760
1761 if (bookmark[0] == '\'' || bookmark[0] == '\"')
1762 {
c2bcbb1d
TT
1763 char *copy;
1764
d02ed0bb
MM
1765 if (bookmark[strlen (bookmark) - 1] != bookmark[0])
1766 error (_("Unbalanced quotes: %s"), bookmark);
1767
c2bcbb1d
TT
1768
1769 copy = savestring (bookmark + 1, strlen (bookmark) - 2);
1770 make_cleanup (xfree, copy);
1771 bookmark = copy;
d02ed0bb
MM
1772 }
1773
c2bcbb1d
TT
1774 record_goto (bookmark);
1775
1776 do_cleanups (cleanup);
d02ed0bb
MM
1777}
1778
d02ed0bb 1779static enum exec_direction_kind
4c612759 1780record_full_execution_direction (struct target_ops *self)
d02ed0bb 1781{
88d1aa9d 1782 return record_full_execution_dir;
d02ed0bb
MM
1783}
1784
b158a20f
TW
1785/* The to_record_method method of target record-full. */
1786
1787enum record_method
1788record_full_record_method (struct target_ops *self, ptid_t ptid)
1789{
1790 return RECORD_METHOD_FULL;
1791}
1792
d02ed0bb 1793static void
630d6a4a 1794record_full_info (struct target_ops *self)
d02ed0bb 1795{
88d1aa9d 1796 struct record_full_entry *p;
d02ed0bb 1797
88d1aa9d 1798 if (RECORD_FULL_IS_REPLAY)
d02ed0bb
MM
1799 printf_filtered (_("Replay mode:\n"));
1800 else
1801 printf_filtered (_("Record mode:\n"));
1802
1803 /* Find entry for first actual instruction in the log. */
88d1aa9d
MM
1804 for (p = record_full_first.next;
1805 p != NULL && p->type != record_full_end;
d02ed0bb
MM
1806 p = p->next)
1807 ;
1808
1809 /* Do we have a log at all? */
88d1aa9d 1810 if (p != NULL && p->type == record_full_end)
d02ed0bb
MM
1811 {
1812 /* Display instruction number for first instruction in the log. */
1813 printf_filtered (_("Lowest recorded instruction number is %s.\n"),
1814 pulongest (p->u.end.insn_num));
1815
1816 /* If in replay mode, display where we are in the log. */
88d1aa9d 1817 if (RECORD_FULL_IS_REPLAY)
d02ed0bb 1818 printf_filtered (_("Current instruction number is %s.\n"),
88d1aa9d 1819 pulongest (record_full_list->u.end.insn_num));
d02ed0bb
MM
1820
1821 /* Display instruction number for last instruction in the log. */
1822 printf_filtered (_("Highest recorded instruction number is %s.\n"),
88d1aa9d 1823 pulongest (record_full_insn_count));
d02ed0bb
MM
1824
1825 /* Display log count. */
7ee70bf5 1826 printf_filtered (_("Log contains %u instructions.\n"),
88d1aa9d 1827 record_full_insn_num);
d02ed0bb
MM
1828 }
1829 else
1830 printf_filtered (_("No instructions have been logged.\n"));
1831
1832 /* Display max log size. */
7ee70bf5 1833 printf_filtered (_("Max logged instructions is %u.\n"),
88d1aa9d 1834 record_full_insn_max_num);
d02ed0bb
MM
1835}
1836
1837/* The "to_record_delete" target method. */
1838
1839static void
d1b55219 1840record_full_delete (struct target_ops *self)
d02ed0bb 1841{
88d1aa9d 1842 record_full_list_release_following (record_full_list);
d02ed0bb
MM
1843}
1844
1845/* The "to_record_is_replaying" target method. */
1846
1847static int
a52eab48 1848record_full_is_replaying (struct target_ops *self, ptid_t ptid)
d02ed0bb 1849{
88d1aa9d 1850 return RECORD_FULL_IS_REPLAY;
d02ed0bb
MM
1851}
1852
7ff27e9b
MM
1853/* The "to_record_will_replay" target method. */
1854
1855static int
1856record_full_will_replay (struct target_ops *self, ptid_t ptid, int dir)
1857{
1858 /* We can currently only record when executing forwards. Should we be able
1859 to record when executing backwards on targets that support reverse
1860 execution, this needs to be changed. */
1861
1862 return RECORD_FULL_IS_REPLAY || dir == EXEC_REVERSE;
1863}
1864
d02ed0bb
MM
1865/* Go to a specific entry. */
1866
1867static void
88d1aa9d 1868record_full_goto_entry (struct record_full_entry *p)
d02ed0bb
MM
1869{
1870 if (p == NULL)
1871 error (_("Target insn not found."));
88d1aa9d 1872 else if (p == record_full_list)
d02ed0bb 1873 error (_("Already at target insn."));
88d1aa9d 1874 else if (p->u.end.insn_num > record_full_list->u.end.insn_num)
d02ed0bb
MM
1875 {
1876 printf_filtered (_("Go forward to insn number %s\n"),
1877 pulongest (p->u.end.insn_num));
88d1aa9d 1878 record_full_goto_insn (p, EXEC_FORWARD);
d02ed0bb
MM
1879 }
1880 else
1881 {
1882 printf_filtered (_("Go backward to insn number %s\n"),
1883 pulongest (p->u.end.insn_num));
88d1aa9d 1884 record_full_goto_insn (p, EXEC_REVERSE);
d02ed0bb
MM
1885 }
1886
1887 registers_changed ();
1888 reinit_frame_cache ();
485668e5 1889 stop_pc = regcache_read_pc (get_current_regcache ());
08d72866 1890 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
d02ed0bb
MM
1891}
1892
1893/* The "to_goto_record_begin" target method. */
1894
1895static void
08475817 1896record_full_goto_begin (struct target_ops *self)
d02ed0bb 1897{
88d1aa9d 1898 struct record_full_entry *p = NULL;
d02ed0bb 1899
88d1aa9d
MM
1900 for (p = &record_full_first; p != NULL; p = p->next)
1901 if (p->type == record_full_end)
d02ed0bb
MM
1902 break;
1903
88d1aa9d 1904 record_full_goto_entry (p);
d02ed0bb
MM
1905}
1906
1907/* The "to_goto_record_end" target method. */
1908
1909static void
307a1b91 1910record_full_goto_end (struct target_ops *self)
d02ed0bb 1911{
88d1aa9d 1912 struct record_full_entry *p = NULL;
d02ed0bb 1913
88d1aa9d 1914 for (p = record_full_list; p->next != NULL; p = p->next)
d02ed0bb
MM
1915 ;
1916 for (; p!= NULL; p = p->prev)
88d1aa9d 1917 if (p->type == record_full_end)
d02ed0bb
MM
1918 break;
1919
88d1aa9d 1920 record_full_goto_entry (p);
d02ed0bb
MM
1921}
1922
1923/* The "to_goto_record" target method. */
1924
1925static void
606183ac 1926record_full_goto (struct target_ops *self, ULONGEST target_insn)
d02ed0bb 1927{
88d1aa9d 1928 struct record_full_entry *p = NULL;
d02ed0bb 1929
88d1aa9d
MM
1930 for (p = &record_full_first; p != NULL; p = p->next)
1931 if (p->type == record_full_end && p->u.end.insn_num == target_insn)
d02ed0bb
MM
1932 break;
1933
88d1aa9d 1934 record_full_goto_entry (p);
d02ed0bb
MM
1935}
1936
797094dd
MM
1937/* The "to_record_stop_replaying" target method. */
1938
1939static void
1940record_full_stop_replaying (struct target_ops *self)
1941{
1942 record_full_goto_end (self);
1943}
1944
d02ed0bb 1945static void
88d1aa9d 1946init_record_full_ops (void)
d02ed0bb 1947{
88d1aa9d
MM
1948 record_full_ops.to_shortname = "record-full";
1949 record_full_ops.to_longname = "Process record and replay target";
1950 record_full_ops.to_doc =
d02ed0bb 1951 "Log program while executing and replay execution from log.";
88d1aa9d
MM
1952 record_full_ops.to_open = record_full_open;
1953 record_full_ops.to_close = record_full_close;
b7d2e916 1954 record_full_ops.to_async = record_full_async;
88d1aa9d 1955 record_full_ops.to_resume = record_full_resume;
85ad3aaf 1956 record_full_ops.to_commit_resume = record_full_commit_resume;
88d1aa9d 1957 record_full_ops.to_wait = record_full_wait;
7c1687a9
MM
1958 record_full_ops.to_disconnect = record_disconnect;
1959 record_full_ops.to_detach = record_detach;
1960 record_full_ops.to_mourn_inferior = record_mourn_inferior;
1961 record_full_ops.to_kill = record_kill;
88d1aa9d
MM
1962 record_full_ops.to_store_registers = record_full_store_registers;
1963 record_full_ops.to_xfer_partial = record_full_xfer_partial;
1964 record_full_ops.to_insert_breakpoint = record_full_insert_breakpoint;
1965 record_full_ops.to_remove_breakpoint = record_full_remove_breakpoint;
1966 record_full_ops.to_stopped_by_watchpoint = record_full_stopped_by_watchpoint;
1967 record_full_ops.to_stopped_data_address = record_full_stopped_data_address;
9e8915c6
PA
1968 record_full_ops.to_stopped_by_sw_breakpoint
1969 = record_full_stopped_by_sw_breakpoint;
1970 record_full_ops.to_supports_stopped_by_sw_breakpoint
1971 = record_full_supports_stopped_by_sw_breakpoint;
1972 record_full_ops.to_stopped_by_hw_breakpoint
1973 = record_full_stopped_by_hw_breakpoint;
1974 record_full_ops.to_supports_stopped_by_hw_breakpoint
1975 = record_full_supports_stopped_by_hw_breakpoint;
88d1aa9d
MM
1976 record_full_ops.to_can_execute_reverse = record_full_can_execute_reverse;
1977 record_full_ops.to_stratum = record_stratum;
d02ed0bb 1978 /* Add bookmark target methods. */
88d1aa9d
MM
1979 record_full_ops.to_get_bookmark = record_full_get_bookmark;
1980 record_full_ops.to_goto_bookmark = record_full_goto_bookmark;
88d1aa9d 1981 record_full_ops.to_execution_direction = record_full_execution_direction;
b158a20f 1982 record_full_ops.to_record_method = record_full_record_method;
88d1aa9d
MM
1983 record_full_ops.to_info_record = record_full_info;
1984 record_full_ops.to_save_record = record_full_save;
1985 record_full_ops.to_delete_record = record_full_delete;
1986 record_full_ops.to_record_is_replaying = record_full_is_replaying;
7ff27e9b 1987 record_full_ops.to_record_will_replay = record_full_will_replay;
797094dd 1988 record_full_ops.to_record_stop_replaying = record_full_stop_replaying;
88d1aa9d
MM
1989 record_full_ops.to_goto_record_begin = record_full_goto_begin;
1990 record_full_ops.to_goto_record_end = record_full_goto_end;
1991 record_full_ops.to_goto_record = record_full_goto;
1992 record_full_ops.to_magic = OPS_MAGIC;
d02ed0bb
MM
1993}
1994
1995/* "to_resume" method for prec over corefile. */
1996
1997static void
88d1aa9d
MM
1998record_full_core_resume (struct target_ops *ops, ptid_t ptid, int step,
1999 enum gdb_signal signal)
d02ed0bb 2000{
88d1aa9d
MM
2001 record_full_resume_step = step;
2002 record_full_resumed = 1;
2003 record_full_execution_dir = execution_direction;
d02ed0bb
MM
2004
2005 /* We are about to start executing the inferior (or simulate it),
2006 let's register it with the event loop. */
2007 if (target_can_async_p ())
6a3753b3 2008 target_async (1);
d02ed0bb
MM
2009}
2010
2011/* "to_kill" method for prec over corefile. */
2012
2013static void
88d1aa9d 2014record_full_core_kill (struct target_ops *ops)
d02ed0bb
MM
2015{
2016 if (record_debug)
88d1aa9d 2017 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_core_kill\n");
d02ed0bb 2018
88d1aa9d 2019 unpush_target (&record_full_core_ops);
d02ed0bb
MM
2020}
2021
2022/* "to_fetch_registers" method for prec over corefile. */
2023
2024static void
88d1aa9d
MM
2025record_full_core_fetch_registers (struct target_ops *ops,
2026 struct regcache *regcache,
2027 int regno)
d02ed0bb
MM
2028{
2029 if (regno < 0)
2030 {
ac7936df 2031 int num = gdbarch_num_regs (regcache->arch ());
d02ed0bb
MM
2032 int i;
2033
2034 for (i = 0; i < num; i ++)
2035 regcache_raw_supply (regcache, i,
88d1aa9d 2036 record_full_core_regbuf + MAX_REGISTER_SIZE * i);
d02ed0bb
MM
2037 }
2038 else
2039 regcache_raw_supply (regcache, regno,
88d1aa9d 2040 record_full_core_regbuf + MAX_REGISTER_SIZE * regno);
d02ed0bb
MM
2041}
2042
2043/* "to_prepare_to_store" method for prec over corefile. */
2044
2045static void
f32dbf8c
MM
2046record_full_core_prepare_to_store (struct target_ops *self,
2047 struct regcache *regcache)
d02ed0bb
MM
2048{
2049}
2050
2051/* "to_store_registers" method for prec over corefile. */
2052
2053static void
88d1aa9d 2054record_full_core_store_registers (struct target_ops *ops,
d02ed0bb
MM
2055 struct regcache *regcache,
2056 int regno)
2057{
88d1aa9d 2058 if (record_full_gdb_operation_disable)
d02ed0bb 2059 regcache_raw_collect (regcache, regno,
88d1aa9d 2060 record_full_core_regbuf + MAX_REGISTER_SIZE * regno);
d02ed0bb
MM
2061 else
2062 error (_("You can't do that without a process to debug."));
2063}
2064
2065/* "to_xfer_partial" method for prec over corefile. */
2066
9b409511 2067static enum target_xfer_status
88d1aa9d
MM
2068record_full_core_xfer_partial (struct target_ops *ops,
2069 enum target_object object,
2070 const char *annex, gdb_byte *readbuf,
2071 const gdb_byte *writebuf, ULONGEST offset,
9b409511 2072 ULONGEST len, ULONGEST *xfered_len)
d02ed0bb
MM
2073{
2074 if (object == TARGET_OBJECT_MEMORY)
2075 {
88d1aa9d 2076 if (record_full_gdb_operation_disable || !writebuf)
d02ed0bb
MM
2077 {
2078 struct target_section *p;
2079
88d1aa9d 2080 for (p = record_full_core_start; p < record_full_core_end; p++)
d02ed0bb
MM
2081 {
2082 if (offset >= p->addr)
2083 {
88d1aa9d 2084 struct record_full_core_buf_entry *entry;
d02ed0bb
MM
2085 ULONGEST sec_offset;
2086
2087 if (offset >= p->endaddr)
2088 continue;
2089
2090 if (offset + len > p->endaddr)
2091 len = p->endaddr - offset;
2092
2093 sec_offset = offset - p->addr;
2094
2095 /* Read readbuf or write writebuf p, offset, len. */
2096 /* Check flags. */
2097 if (p->the_bfd_section->flags & SEC_CONSTRUCTOR
2098 || (p->the_bfd_section->flags & SEC_HAS_CONTENTS) == 0)
2099 {
2100 if (readbuf)
2101 memset (readbuf, 0, len);
9b409511
YQ
2102
2103 *xfered_len = len;
2104 return TARGET_XFER_OK;
d02ed0bb 2105 }
88d1aa9d
MM
2106 /* Get record_full_core_buf_entry. */
2107 for (entry = record_full_core_buf_list; entry;
d02ed0bb
MM
2108 entry = entry->prev)
2109 if (entry->p == p)
2110 break;
2111 if (writebuf)
2112 {
2113 if (!entry)
2114 {
2115 /* Add a new entry. */
8d749320 2116 entry = XNEW (struct record_full_core_buf_entry);
d02ed0bb 2117 entry->p = p;
2b2848e2
DE
2118 if (!bfd_malloc_and_get_section
2119 (p->the_bfd_section->owner,
2120 p->the_bfd_section,
2121 &entry->buf))
d02ed0bb
MM
2122 {
2123 xfree (entry);
9b409511 2124 return TARGET_XFER_EOF;
d02ed0bb 2125 }
88d1aa9d
MM
2126 entry->prev = record_full_core_buf_list;
2127 record_full_core_buf_list = entry;
d02ed0bb
MM
2128 }
2129
2130 memcpy (entry->buf + sec_offset, writebuf,
2131 (size_t) len);
2132 }
2133 else
2134 {
2135 if (!entry)
6b84065d
TT
2136 return ops->beneath->to_xfer_partial (ops->beneath,
2137 object, annex,
2138 readbuf, writebuf,
2139 offset, len,
2140 xfered_len);
d02ed0bb
MM
2141
2142 memcpy (readbuf, entry->buf + sec_offset,
2143 (size_t) len);
2144 }
2145
9b409511
YQ
2146 *xfered_len = len;
2147 return TARGET_XFER_OK;
d02ed0bb
MM
2148 }
2149 }
2150
2ed4b548 2151 return TARGET_XFER_E_IO;
d02ed0bb
MM
2152 }
2153 else
2154 error (_("You can't do that without a process to debug."));
2155 }
2156
6b84065d
TT
2157 return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
2158 readbuf, writebuf, offset, len,
2159 xfered_len);
d02ed0bb
MM
2160}
2161
2162/* "to_insert_breakpoint" method for prec over corefile. */
2163
2164static int
3db08215
MM
2165record_full_core_insert_breakpoint (struct target_ops *ops,
2166 struct gdbarch *gdbarch,
88d1aa9d 2167 struct bp_target_info *bp_tgt)
d02ed0bb
MM
2168{
2169 return 0;
2170}
2171
2172/* "to_remove_breakpoint" method for prec over corefile. */
2173
2174static int
3db08215
MM
2175record_full_core_remove_breakpoint (struct target_ops *ops,
2176 struct gdbarch *gdbarch,
73971819
PA
2177 struct bp_target_info *bp_tgt,
2178 enum remove_bp_reason reason)
d02ed0bb
MM
2179{
2180 return 0;
2181}
2182
2183/* "to_has_execution" method for prec over corefile. */
2184
2185static int
88d1aa9d 2186record_full_core_has_execution (struct target_ops *ops, ptid_t the_ptid)
d02ed0bb
MM
2187{
2188 return 1;
2189}
2190
2191static void
88d1aa9d 2192init_record_full_core_ops (void)
d02ed0bb 2193{
88d1aa9d
MM
2194 record_full_core_ops.to_shortname = "record-core";
2195 record_full_core_ops.to_longname = "Process record and replay target";
2196 record_full_core_ops.to_doc =
d02ed0bb 2197 "Log program while executing and replay execution from log.";
88d1aa9d
MM
2198 record_full_core_ops.to_open = record_full_open;
2199 record_full_core_ops.to_close = record_full_close;
b7d2e916 2200 record_full_core_ops.to_async = record_full_async;
88d1aa9d
MM
2201 record_full_core_ops.to_resume = record_full_core_resume;
2202 record_full_core_ops.to_wait = record_full_wait;
2203 record_full_core_ops.to_kill = record_full_core_kill;
2204 record_full_core_ops.to_fetch_registers = record_full_core_fetch_registers;
2205 record_full_core_ops.to_prepare_to_store = record_full_core_prepare_to_store;
2206 record_full_core_ops.to_store_registers = record_full_core_store_registers;
2207 record_full_core_ops.to_xfer_partial = record_full_core_xfer_partial;
2208 record_full_core_ops.to_insert_breakpoint
2209 = record_full_core_insert_breakpoint;
2210 record_full_core_ops.to_remove_breakpoint
2211 = record_full_core_remove_breakpoint;
2212 record_full_core_ops.to_stopped_by_watchpoint
2213 = record_full_stopped_by_watchpoint;
2214 record_full_core_ops.to_stopped_data_address
2215 = record_full_stopped_data_address;
9e8915c6
PA
2216 record_full_core_ops.to_stopped_by_sw_breakpoint
2217 = record_full_stopped_by_sw_breakpoint;
2218 record_full_core_ops.to_supports_stopped_by_sw_breakpoint
2219 = record_full_supports_stopped_by_sw_breakpoint;
2220 record_full_core_ops.to_stopped_by_hw_breakpoint
2221 = record_full_stopped_by_hw_breakpoint;
2222 record_full_core_ops.to_supports_stopped_by_hw_breakpoint
2223 = record_full_supports_stopped_by_hw_breakpoint;
88d1aa9d
MM
2224 record_full_core_ops.to_can_execute_reverse
2225 = record_full_can_execute_reverse;
2226 record_full_core_ops.to_has_execution = record_full_core_has_execution;
2227 record_full_core_ops.to_stratum = record_stratum;
d02ed0bb 2228 /* Add bookmark target methods. */
88d1aa9d
MM
2229 record_full_core_ops.to_get_bookmark = record_full_get_bookmark;
2230 record_full_core_ops.to_goto_bookmark = record_full_goto_bookmark;
88d1aa9d
MM
2231 record_full_core_ops.to_execution_direction
2232 = record_full_execution_direction;
b158a20f 2233 record_full_core_ops.to_record_method = record_full_record_method;
88d1aa9d
MM
2234 record_full_core_ops.to_info_record = record_full_info;
2235 record_full_core_ops.to_delete_record = record_full_delete;
2236 record_full_core_ops.to_record_is_replaying = record_full_is_replaying;
7ff27e9b 2237 record_full_core_ops.to_record_will_replay = record_full_will_replay;
88d1aa9d
MM
2238 record_full_core_ops.to_goto_record_begin = record_full_goto_begin;
2239 record_full_core_ops.to_goto_record_end = record_full_goto_end;
2240 record_full_core_ops.to_goto_record = record_full_goto;
2241 record_full_core_ops.to_magic = OPS_MAGIC;
d02ed0bb
MM
2242}
2243
2244/* Record log save-file format
2245 Version 1 (never released)
2246
2247 Header:
2248 4 bytes: magic number htonl(0x20090829).
2249 NOTE: be sure to change whenever this file format changes!
2250
2251 Records:
88d1aa9d
MM
2252 record_full_end:
2253 1 byte: record type (record_full_end, see enum record_full_type).
2254 record_full_reg:
2255 1 byte: record type (record_full_reg, see enum record_full_type).
d02ed0bb
MM
2256 8 bytes: register id (network byte order).
2257 MAX_REGISTER_SIZE bytes: register value.
88d1aa9d
MM
2258 record_full_mem:
2259 1 byte: record type (record_full_mem, see enum record_full_type).
d02ed0bb
MM
2260 8 bytes: memory length (network byte order).
2261 8 bytes: memory address (network byte order).
2262 n bytes: memory value (n == memory length).
2263
2264 Version 2
2265 4 bytes: magic number netorder32(0x20091016).
2266 NOTE: be sure to change whenever this file format changes!
2267
2268 Records:
88d1aa9d
MM
2269 record_full_end:
2270 1 byte: record type (record_full_end, see enum record_full_type).
d02ed0bb
MM
2271 4 bytes: signal
2272 4 bytes: instruction count
88d1aa9d
MM
2273 record_full_reg:
2274 1 byte: record type (record_full_reg, see enum record_full_type).
d02ed0bb
MM
2275 4 bytes: register id (network byte order).
2276 n bytes: register value (n == actual register size).
2277 (eg. 4 bytes for x86 general registers).
88d1aa9d
MM
2278 record_full_mem:
2279 1 byte: record type (record_full_mem, see enum record_full_type).
d02ed0bb
MM
2280 4 bytes: memory length (network byte order).
2281 8 bytes: memory address (network byte order).
2282 n bytes: memory value (n == memory length).
2283
2284*/
2285
2286/* bfdcore_read -- read bytes from a core file section. */
2287
2288static inline void
2289bfdcore_read (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2290{
2291 int ret = bfd_get_section_contents (obfd, osec, buf, *offset, len);
2292
2293 if (ret)
2294 *offset += len;
2295 else
2296 error (_("Failed to read %d bytes from core file %s ('%s')."),
2297 len, bfd_get_filename (obfd),
2298 bfd_errmsg (bfd_get_error ()));
2299}
2300
2301static inline uint64_t
2302netorder64 (uint64_t input)
2303{
2304 uint64_t ret;
2305
2306 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2307 BFD_ENDIAN_BIG, input);
2308 return ret;
2309}
2310
2311static inline uint32_t
2312netorder32 (uint32_t input)
2313{
2314 uint32_t ret;
2315
d02ed0bb
MM
2316 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2317 BFD_ENDIAN_BIG, input);
2318 return ret;
2319}
2320
2321/* Restore the execution log from a core_bfd file. */
2322static void
88d1aa9d 2323record_full_restore (void)
d02ed0bb
MM
2324{
2325 uint32_t magic;
2326 struct cleanup *old_cleanups;
88d1aa9d 2327 struct record_full_entry *rec;
d02ed0bb
MM
2328 asection *osec;
2329 uint32_t osec_size;
2330 int bfd_offset = 0;
2331 struct regcache *regcache;
2332
2333 /* We restore the execution log from the open core bfd,
2334 if there is one. */
2335 if (core_bfd == NULL)
2336 return;
2337
88d1aa9d
MM
2338 /* "record_full_restore" can only be called when record list is empty. */
2339 gdb_assert (record_full_first.next == NULL);
d02ed0bb
MM
2340
2341 if (record_debug)
2342 fprintf_unfiltered (gdb_stdlog, "Restoring recording from core file.\n");
2343
2344 /* Now need to find our special note section. */
2345 osec = bfd_get_section_by_name (core_bfd, "null0");
2346 if (record_debug)
2347 fprintf_unfiltered (gdb_stdlog, "Find precord section %s.\n",
2348 osec ? "succeeded" : "failed");
2349 if (osec == NULL)
2350 return;
2351 osec_size = bfd_section_size (core_bfd, osec);
2352 if (record_debug)
2353 fprintf_unfiltered (gdb_stdlog, "%s", bfd_section_name (core_bfd, osec));
2354
2355 /* Check the magic code. */
2356 bfdcore_read (core_bfd, osec, &magic, sizeof (magic), &bfd_offset);
88d1aa9d 2357 if (magic != RECORD_FULL_FILE_MAGIC)
d02ed0bb
MM
2358 error (_("Version mis-match or file format error in core file %s."),
2359 bfd_get_filename (core_bfd));
2360 if (record_debug)
2361 fprintf_unfiltered (gdb_stdlog,
2362 " Reading 4-byte magic cookie "
88d1aa9d 2363 "RECORD_FULL_FILE_MAGIC (0x%s)\n",
d02ed0bb
MM
2364 phex_nz (netorder32 (magic), 4));
2365
88d1aa9d
MM
2366 /* Restore the entries in recfd into record_full_arch_list_head and
2367 record_full_arch_list_tail. */
2368 record_full_arch_list_head = NULL;
2369 record_full_arch_list_tail = NULL;
2370 record_full_insn_num = 0;
2371 old_cleanups = make_cleanup (record_full_arch_list_cleanups, 0);
d02ed0bb
MM
2372 regcache = get_current_regcache ();
2373
2374 while (1)
2375 {
2376 uint8_t rectype;
2377 uint32_t regnum, len, signal, count;
2378 uint64_t addr;
2379
2380 /* We are finished when offset reaches osec_size. */
2381 if (bfd_offset >= osec_size)
2382 break;
2383 bfdcore_read (core_bfd, osec, &rectype, sizeof (rectype), &bfd_offset);
2384
2385 switch (rectype)
2386 {
88d1aa9d 2387 case record_full_reg: /* reg */
d02ed0bb
MM
2388 /* Get register number to regnum. */
2389 bfdcore_read (core_bfd, osec, &regnum,
2390 sizeof (regnum), &bfd_offset);
2391 regnum = netorder32 (regnum);
2392
88d1aa9d 2393 rec = record_full_reg_alloc (regcache, regnum);
d02ed0bb
MM
2394
2395 /* Get val. */
88d1aa9d 2396 bfdcore_read (core_bfd, osec, record_full_get_loc (rec),
d02ed0bb
MM
2397 rec->u.reg.len, &bfd_offset);
2398
2399 if (record_debug)
2400 fprintf_unfiltered (gdb_stdlog,
2401 " Reading register %d (1 "
2402 "plus %lu plus %d bytes)\n",
2403 rec->u.reg.num,
2404 (unsigned long) sizeof (regnum),
2405 rec->u.reg.len);
2406 break;
2407
88d1aa9d 2408 case record_full_mem: /* mem */
d02ed0bb
MM
2409 /* Get len. */
2410 bfdcore_read (core_bfd, osec, &len,
2411 sizeof (len), &bfd_offset);
2412 len = netorder32 (len);
2413
2414 /* Get addr. */
2415 bfdcore_read (core_bfd, osec, &addr,
2416 sizeof (addr), &bfd_offset);
2417 addr = netorder64 (addr);
2418
88d1aa9d 2419 rec = record_full_mem_alloc (addr, len);
d02ed0bb
MM
2420
2421 /* Get val. */
88d1aa9d 2422 bfdcore_read (core_bfd, osec, record_full_get_loc (rec),
d02ed0bb
MM
2423 rec->u.mem.len, &bfd_offset);
2424
2425 if (record_debug)
2426 fprintf_unfiltered (gdb_stdlog,
2427 " Reading memory %s (1 plus "
2428 "%lu plus %lu plus %d bytes)\n",
2429 paddress (get_current_arch (),
2430 rec->u.mem.addr),
2431 (unsigned long) sizeof (addr),
2432 (unsigned long) sizeof (len),
2433 rec->u.mem.len);
2434 break;
2435
88d1aa9d
MM
2436 case record_full_end: /* end */
2437 rec = record_full_end_alloc ();
2438 record_full_insn_num ++;
d02ed0bb
MM
2439
2440 /* Get signal value. */
2441 bfdcore_read (core_bfd, osec, &signal,
2442 sizeof (signal), &bfd_offset);
2443 signal = netorder32 (signal);
aead7601 2444 rec->u.end.sigval = (enum gdb_signal) signal;
d02ed0bb
MM
2445
2446 /* Get insn count. */
2447 bfdcore_read (core_bfd, osec, &count,
2448 sizeof (count), &bfd_offset);
2449 count = netorder32 (count);
2450 rec->u.end.insn_num = count;
88d1aa9d 2451 record_full_insn_count = count + 1;
d02ed0bb
MM
2452 if (record_debug)
2453 fprintf_unfiltered (gdb_stdlog,
88d1aa9d 2454 " Reading record_full_end (1 + "
d02ed0bb
MM
2455 "%lu + %lu bytes), offset == %s\n",
2456 (unsigned long) sizeof (signal),
2457 (unsigned long) sizeof (count),
2458 paddress (get_current_arch (),
2459 bfd_offset));
2460 break;
2461
2462 default:
2463 error (_("Bad entry type in core file %s."),
2464 bfd_get_filename (core_bfd));
2465 break;
2466 }
2467
2468 /* Add rec to record arch list. */
88d1aa9d 2469 record_full_arch_list_add (rec);
d02ed0bb
MM
2470 }
2471
2472 discard_cleanups (old_cleanups);
2473
88d1aa9d
MM
2474 /* Add record_full_arch_list_head to the end of record list. */
2475 record_full_first.next = record_full_arch_list_head;
2476 record_full_arch_list_head->prev = &record_full_first;
2477 record_full_arch_list_tail->next = NULL;
2478 record_full_list = &record_full_first;
d02ed0bb 2479
88d1aa9d
MM
2480 /* Update record_full_insn_max_num. */
2481 if (record_full_insn_num > record_full_insn_max_num)
d02ed0bb 2482 {
88d1aa9d 2483 record_full_insn_max_num = record_full_insn_num;
7ee70bf5 2484 warning (_("Auto increase record/replay buffer limit to %u."),
88d1aa9d 2485 record_full_insn_max_num);
d02ed0bb
MM
2486 }
2487
2488 /* Succeeded. */
2489 printf_filtered (_("Restored records from core file %s.\n"),
2490 bfd_get_filename (core_bfd));
2491
08d72866 2492 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
d02ed0bb
MM
2493}
2494
2495/* bfdcore_write -- write bytes into a core file section. */
2496
2497static inline void
2498bfdcore_write (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2499{
2500 int ret = bfd_set_section_contents (obfd, osec, buf, *offset, len);
2501
2502 if (ret)
2503 *offset += len;
2504 else
2505 error (_("Failed to write %d bytes to core file %s ('%s')."),
2506 len, bfd_get_filename (obfd),
2507 bfd_errmsg (bfd_get_error ()));
2508}
2509
2510/* Restore the execution log from a file. We use a modified elf
2511 corefile format, with an extra section for our data. */
2512
2513static void
41243651 2514cmd_record_full_restore (const char *args, int from_tty)
d02ed0bb
MM
2515{
2516 core_file_command (args, from_tty);
88d1aa9d 2517 record_full_open (args, from_tty);
d02ed0bb
MM
2518}
2519
d02ed0bb
MM
2520/* Save the execution log to a file. We use a modified elf corefile
2521 format, with an extra section for our data. */
2522
2523static void
1390f529 2524record_full_save (struct target_ops *self, const char *recfilename)
d02ed0bb 2525{
88d1aa9d 2526 struct record_full_entry *cur_record_full_list;
d02ed0bb
MM
2527 uint32_t magic;
2528 struct regcache *regcache;
2529 struct gdbarch *gdbarch;
d02ed0bb
MM
2530 int save_size = 0;
2531 asection *osec = NULL;
2532 int bfd_offset = 0;
2533
2534 /* Open the save file. */
2535 if (record_debug)
2536 fprintf_unfiltered (gdb_stdlog, "Saving execution log to core file '%s'\n",
2537 recfilename);
2538
2539 /* Open the output file. */
bef155c3
TT
2540 gdb_bfd_ref_ptr obfd (create_gcore_bfd (recfilename));
2541
2542 /* Arrange to remove the output file on failure. */
2543 gdb::unlinker unlink_file (recfilename);
d02ed0bb 2544
88d1aa9d
MM
2545 /* Save the current record entry to "cur_record_full_list". */
2546 cur_record_full_list = record_full_list;
d02ed0bb
MM
2547
2548 /* Get the values of regcache and gdbarch. */
2549 regcache = get_current_regcache ();
ac7936df 2550 gdbarch = regcache->arch ();
d02ed0bb
MM
2551
2552 /* Disable the GDB operation record. */
07036511
TT
2553 scoped_restore restore_operation_disable
2554 = record_full_gdb_operation_disable_set ();
d02ed0bb
MM
2555
2556 /* Reverse execute to the begin of record list. */
2557 while (1)
2558 {
2559 /* Check for beginning and end of log. */
88d1aa9d 2560 if (record_full_list == &record_full_first)
d02ed0bb
MM
2561 break;
2562
88d1aa9d 2563 record_full_exec_insn (regcache, gdbarch, record_full_list);
d02ed0bb 2564
88d1aa9d
MM
2565 if (record_full_list->prev)
2566 record_full_list = record_full_list->prev;
d02ed0bb
MM
2567 }
2568
2569 /* Compute the size needed for the extra bfd section. */
2570 save_size = 4; /* magic cookie */
88d1aa9d
MM
2571 for (record_full_list = record_full_first.next; record_full_list;
2572 record_full_list = record_full_list->next)
2573 switch (record_full_list->type)
d02ed0bb 2574 {
88d1aa9d 2575 case record_full_end:
d02ed0bb
MM
2576 save_size += 1 + 4 + 4;
2577 break;
88d1aa9d
MM
2578 case record_full_reg:
2579 save_size += 1 + 4 + record_full_list->u.reg.len;
d02ed0bb 2580 break;
88d1aa9d
MM
2581 case record_full_mem:
2582 save_size += 1 + 4 + 8 + record_full_list->u.mem.len;
d02ed0bb
MM
2583 break;
2584 }
2585
2586 /* Make the new bfd section. */
bef155c3 2587 osec = bfd_make_section_anyway_with_flags (obfd.get (), "precord",
d02ed0bb
MM
2588 SEC_HAS_CONTENTS
2589 | SEC_READONLY);
2590 if (osec == NULL)
2591 error (_("Failed to create 'precord' section for corefile %s: %s"),
2592 recfilename,
2593 bfd_errmsg (bfd_get_error ()));
bef155c3
TT
2594 bfd_set_section_size (obfd.get (), osec, save_size);
2595 bfd_set_section_vma (obfd.get (), osec, 0);
2596 bfd_set_section_alignment (obfd.get (), osec, 0);
2597 bfd_section_lma (obfd.get (), osec) = 0;
d02ed0bb
MM
2598
2599 /* Save corefile state. */
bef155c3 2600 write_gcore_file (obfd.get ());
d02ed0bb
MM
2601
2602 /* Write out the record log. */
2603 /* Write the magic code. */
88d1aa9d 2604 magic = RECORD_FULL_FILE_MAGIC;
d02ed0bb
MM
2605 if (record_debug)
2606 fprintf_unfiltered (gdb_stdlog,
2607 " Writing 4-byte magic cookie "
88d1aa9d 2608 "RECORD_FULL_FILE_MAGIC (0x%s)\n",
d02ed0bb 2609 phex_nz (magic, 4));
bef155c3 2610 bfdcore_write (obfd.get (), osec, &magic, sizeof (magic), &bfd_offset);
d02ed0bb
MM
2611
2612 /* Save the entries to recfd and forward execute to the end of
2613 record list. */
88d1aa9d 2614 record_full_list = &record_full_first;
d02ed0bb
MM
2615 while (1)
2616 {
2617 /* Save entry. */
88d1aa9d 2618 if (record_full_list != &record_full_first)
d02ed0bb
MM
2619 {
2620 uint8_t type;
2621 uint32_t regnum, len, signal, count;
2622 uint64_t addr;
2623
88d1aa9d 2624 type = record_full_list->type;
bef155c3 2625 bfdcore_write (obfd.get (), osec, &type, sizeof (type), &bfd_offset);
d02ed0bb 2626
88d1aa9d 2627 switch (record_full_list->type)
d02ed0bb 2628 {
88d1aa9d 2629 case record_full_reg: /* reg */
d02ed0bb
MM
2630 if (record_debug)
2631 fprintf_unfiltered (gdb_stdlog,
2632 " Writing register %d (1 "
2633 "plus %lu plus %d bytes)\n",
88d1aa9d 2634 record_full_list->u.reg.num,
d02ed0bb 2635 (unsigned long) sizeof (regnum),
88d1aa9d 2636 record_full_list->u.reg.len);
d02ed0bb
MM
2637
2638 /* Write regnum. */
88d1aa9d 2639 regnum = netorder32 (record_full_list->u.reg.num);
bef155c3 2640 bfdcore_write (obfd.get (), osec, &regnum,
d02ed0bb
MM
2641 sizeof (regnum), &bfd_offset);
2642
2643 /* Write regval. */
bef155c3 2644 bfdcore_write (obfd.get (), osec,
88d1aa9d
MM
2645 record_full_get_loc (record_full_list),
2646 record_full_list->u.reg.len, &bfd_offset);
d02ed0bb
MM
2647 break;
2648
88d1aa9d 2649 case record_full_mem: /* mem */
d02ed0bb
MM
2650 if (record_debug)
2651 fprintf_unfiltered (gdb_stdlog,
2652 " Writing memory %s (1 plus "
2653 "%lu plus %lu plus %d bytes)\n",
2654 paddress (gdbarch,
88d1aa9d 2655 record_full_list->u.mem.addr),
d02ed0bb
MM
2656 (unsigned long) sizeof (addr),
2657 (unsigned long) sizeof (len),
88d1aa9d 2658 record_full_list->u.mem.len);
d02ed0bb
MM
2659
2660 /* Write memlen. */
88d1aa9d 2661 len = netorder32 (record_full_list->u.mem.len);
bef155c3
TT
2662 bfdcore_write (obfd.get (), osec, &len, sizeof (len),
2663 &bfd_offset);
d02ed0bb
MM
2664
2665 /* Write memaddr. */
88d1aa9d 2666 addr = netorder64 (record_full_list->u.mem.addr);
bef155c3 2667 bfdcore_write (obfd.get (), osec, &addr,
d02ed0bb
MM
2668 sizeof (addr), &bfd_offset);
2669
2670 /* Write memval. */
bef155c3 2671 bfdcore_write (obfd.get (), osec,
88d1aa9d
MM
2672 record_full_get_loc (record_full_list),
2673 record_full_list->u.mem.len, &bfd_offset);
d02ed0bb
MM
2674 break;
2675
88d1aa9d 2676 case record_full_end:
d02ed0bb
MM
2677 if (record_debug)
2678 fprintf_unfiltered (gdb_stdlog,
88d1aa9d 2679 " Writing record_full_end (1 + "
d02ed0bb
MM
2680 "%lu + %lu bytes)\n",
2681 (unsigned long) sizeof (signal),
2682 (unsigned long) sizeof (count));
2683 /* Write signal value. */
88d1aa9d 2684 signal = netorder32 (record_full_list->u.end.sigval);
bef155c3 2685 bfdcore_write (obfd.get (), osec, &signal,
d02ed0bb
MM
2686 sizeof (signal), &bfd_offset);
2687
2688 /* Write insn count. */
88d1aa9d 2689 count = netorder32 (record_full_list->u.end.insn_num);
bef155c3 2690 bfdcore_write (obfd.get (), osec, &count,
d02ed0bb
MM
2691 sizeof (count), &bfd_offset);
2692 break;
2693 }
2694 }
2695
2696 /* Execute entry. */
88d1aa9d 2697 record_full_exec_insn (regcache, gdbarch, record_full_list);
d02ed0bb 2698
88d1aa9d
MM
2699 if (record_full_list->next)
2700 record_full_list = record_full_list->next;
d02ed0bb
MM
2701 else
2702 break;
2703 }
2704
88d1aa9d 2705 /* Reverse execute to cur_record_full_list. */
d02ed0bb
MM
2706 while (1)
2707 {
2708 /* Check for beginning and end of log. */
88d1aa9d 2709 if (record_full_list == cur_record_full_list)
d02ed0bb
MM
2710 break;
2711
88d1aa9d 2712 record_full_exec_insn (regcache, gdbarch, record_full_list);
d02ed0bb 2713
88d1aa9d
MM
2714 if (record_full_list->prev)
2715 record_full_list = record_full_list->prev;
d02ed0bb
MM
2716 }
2717
bef155c3 2718 unlink_file.keep ();
d02ed0bb
MM
2719
2720 /* Succeeded. */
2721 printf_filtered (_("Saved core file %s with execution log.\n"),
2722 recfilename);
2723}
2724
88d1aa9d 2725/* record_full_goto_insn -- rewind the record log (forward or backward,
d02ed0bb
MM
2726 depending on DIR) to the given entry, changing the program state
2727 correspondingly. */
2728
2729static void
88d1aa9d
MM
2730record_full_goto_insn (struct record_full_entry *entry,
2731 enum exec_direction_kind dir)
d02ed0bb 2732{
07036511
TT
2733 scoped_restore restore_operation_disable
2734 = record_full_gdb_operation_disable_set ();
d02ed0bb 2735 struct regcache *regcache = get_current_regcache ();
ac7936df 2736 struct gdbarch *gdbarch = regcache->arch ();
d02ed0bb
MM
2737
2738 /* Assume everything is valid: we will hit the entry,
2739 and we will not hit the end of the recording. */
2740
2741 if (dir == EXEC_FORWARD)
88d1aa9d 2742 record_full_list = record_full_list->next;
d02ed0bb
MM
2743
2744 do
2745 {
88d1aa9d 2746 record_full_exec_insn (regcache, gdbarch, record_full_list);
d02ed0bb 2747 if (dir == EXEC_REVERSE)
88d1aa9d 2748 record_full_list = record_full_list->prev;
d02ed0bb 2749 else
88d1aa9d
MM
2750 record_full_list = record_full_list->next;
2751 } while (record_full_list != entry);
d02ed0bb
MM
2752}
2753
2754/* Alias for "target record-full". */
2755
2756static void
981a3fb3 2757cmd_record_full_start (const char *args, int from_tty)
d02ed0bb 2758{
9b2eba3d 2759 execute_command ((char *) "target record-full", from_tty);
d02ed0bb
MM
2760}
2761
2762static void
88d1aa9d
MM
2763set_record_full_insn_max_num (char *args, int from_tty,
2764 struct cmd_list_element *c)
d02ed0bb 2765{
7ee70bf5 2766 if (record_full_insn_num > record_full_insn_max_num)
d02ed0bb 2767 {
88d1aa9d
MM
2768 /* Count down record_full_insn_num while releasing records from list. */
2769 while (record_full_insn_num > record_full_insn_max_num)
d02ed0bb 2770 {
88d1aa9d
MM
2771 record_full_list_release_first ();
2772 record_full_insn_num--;
d02ed0bb
MM
2773 }
2774 }
2775}
2776
2777/* The "set record full" command. */
2778
2779static void
981a3fb3 2780set_record_full_command (const char *args, int from_tty)
d02ed0bb
MM
2781{
2782 printf_unfiltered (_("\"set record full\" must be followed "
981a3fb3 2783 "by an appropriate subcommand.\n"));
d02ed0bb
MM
2784 help_list (set_record_full_cmdlist, "set record full ", all_commands,
2785 gdb_stdout);
2786}
2787
2788/* The "show record full" command. */
2789
2790static void
981a3fb3 2791show_record_full_command (const char *args, int from_tty)
d02ed0bb
MM
2792{
2793 cmd_show_list (show_record_full_cmdlist, from_tty, "");
2794}
2795
d02ed0bb
MM
2796void
2797_initialize_record_full (void)
2798{
2799 struct cmd_list_element *c;
2800
88d1aa9d
MM
2801 /* Init record_full_first. */
2802 record_full_first.prev = NULL;
2803 record_full_first.next = NULL;
2804 record_full_first.type = record_full_end;
d02ed0bb 2805
88d1aa9d
MM
2806 init_record_full_ops ();
2807 add_target (&record_full_ops);
2808 add_deprecated_target_alias (&record_full_ops, "record");
2809 init_record_full_core_ops ();
2810 add_target (&record_full_core_ops);
d02ed0bb 2811
88d1aa9d 2812 add_prefix_cmd ("full", class_obscure, cmd_record_full_start,
d02ed0bb
MM
2813 _("Start full execution recording."), &record_full_cmdlist,
2814 "record full ", 0, &record_cmdlist);
2815
88d1aa9d 2816 c = add_cmd ("restore", class_obscure, cmd_record_full_restore,
d02ed0bb
MM
2817 _("Restore the execution log from a file.\n\
2818Argument is filename. File must be created with 'record save'."),
2819 &record_full_cmdlist);
2820 set_cmd_completer (c, filename_completer);
2821
2822 /* Deprecate the old version without "full" prefix. */
2823 c = add_alias_cmd ("restore", "full restore", class_obscure, 1,
2824 &record_cmdlist);
2825 set_cmd_completer (c, filename_completer);
2826 deprecate_cmd (c, "record full restore");
2827
2828 add_prefix_cmd ("full", class_support, set_record_full_command,
2829 _("Set record options"), &set_record_full_cmdlist,
2830 "set record full ", 0, &set_record_cmdlist);
2831
2832 add_prefix_cmd ("full", class_support, show_record_full_command,
2833 _("Show record options"), &show_record_full_cmdlist,
2834 "show record full ", 0, &show_record_cmdlist);
2835
2836 /* Record instructions number limit command. */
2837 add_setshow_boolean_cmd ("stop-at-limit", no_class,
88d1aa9d 2838 &record_full_stop_at_limit, _("\
d02ed0bb
MM
2839Set whether record/replay stops when record/replay buffer becomes full."), _("\
2840Show whether record/replay stops when record/replay buffer becomes full."),
2841 _("Default is ON.\n\
2842When ON, if the record/replay buffer becomes full, ask user what to do.\n\
2843When OFF, if the record/replay buffer becomes full,\n\
2844delete the oldest recorded instruction to make room for each new one."),
2845 NULL, NULL,
2846 &set_record_full_cmdlist, &show_record_full_cmdlist);
2847
2848 c = add_alias_cmd ("stop-at-limit", "full stop-at-limit", no_class, 1,
2849 &set_record_cmdlist);
2850 deprecate_cmd (c, "set record full stop-at-limit");
2851
2852 c = add_alias_cmd ("stop-at-limit", "full stop-at-limit", no_class, 1,
2853 &show_record_cmdlist);
2854 deprecate_cmd (c, "show record full stop-at-limit");
2855
88d1aa9d
MM
2856 add_setshow_uinteger_cmd ("insn-number-max", no_class,
2857 &record_full_insn_max_num,
d02ed0bb
MM
2858 _("Set record/replay buffer limit."),
2859 _("Show record/replay buffer limit."), _("\
2860Set the maximum number of instructions to be stored in the\n\
f81d1120
PA
2861record/replay buffer. A value of either \"unlimited\" or zero means no\n\
2862limit. Default is 200000."),
88d1aa9d 2863 set_record_full_insn_max_num,
d02ed0bb
MM
2864 NULL, &set_record_full_cmdlist,
2865 &show_record_full_cmdlist);
2866
2867 c = add_alias_cmd ("insn-number-max", "full insn-number-max", no_class, 1,
2868 &set_record_cmdlist);
2869 deprecate_cmd (c, "set record full insn-number-max");
2870
2871 c = add_alias_cmd ("insn-number-max", "full insn-number-max", no_class, 1,
2872 &show_record_cmdlist);
2873 deprecate_cmd (c, "show record full insn-number-max");
2874
88d1aa9d 2875 add_setshow_boolean_cmd ("memory-query", no_class,
25ea693b 2876 &record_full_memory_query, _("\
d02ed0bb
MM
2877Set whether query if PREC cannot record memory change of next instruction."),
2878 _("\
2879Show whether query if PREC cannot record memory change of next instruction."),
2880 _("\
2881Default is OFF.\n\
2882When ON, query if PREC cannot record memory change of next instruction."),
2883 NULL, NULL,
88d1aa9d
MM
2884 &set_record_full_cmdlist,
2885 &show_record_full_cmdlist);
d02ed0bb
MM
2886
2887 c = add_alias_cmd ("memory-query", "full memory-query", no_class, 1,
2888 &set_record_cmdlist);
2889 deprecate_cmd (c, "set record full memory-query");
2890
2891 c = add_alias_cmd ("memory-query", "full memory-query", no_class, 1,
2892 &show_record_cmdlist);
2893 deprecate_cmd (c, "show record full memory-query");
2894}
This page took 0.641285 seconds and 4 git commands to generate.