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