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