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