Introduce and use gdb::unlinker
[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
1806static void
630d6a4a 1807record_full_info (struct target_ops *self)
d02ed0bb 1808{
88d1aa9d 1809 struct record_full_entry *p;
d02ed0bb 1810
88d1aa9d 1811 if (RECORD_FULL_IS_REPLAY)
d02ed0bb
MM
1812 printf_filtered (_("Replay mode:\n"));
1813 else
1814 printf_filtered (_("Record mode:\n"));
1815
1816 /* Find entry for first actual instruction in the log. */
88d1aa9d
MM
1817 for (p = record_full_first.next;
1818 p != NULL && p->type != record_full_end;
d02ed0bb
MM
1819 p = p->next)
1820 ;
1821
1822 /* Do we have a log at all? */
88d1aa9d 1823 if (p != NULL && p->type == record_full_end)
d02ed0bb
MM
1824 {
1825 /* Display instruction number for first instruction in the log. */
1826 printf_filtered (_("Lowest recorded instruction number is %s.\n"),
1827 pulongest (p->u.end.insn_num));
1828
1829 /* If in replay mode, display where we are in the log. */
88d1aa9d 1830 if (RECORD_FULL_IS_REPLAY)
d02ed0bb 1831 printf_filtered (_("Current instruction number is %s.\n"),
88d1aa9d 1832 pulongest (record_full_list->u.end.insn_num));
d02ed0bb
MM
1833
1834 /* Display instruction number for last instruction in the log. */
1835 printf_filtered (_("Highest recorded instruction number is %s.\n"),
88d1aa9d 1836 pulongest (record_full_insn_count));
d02ed0bb
MM
1837
1838 /* Display log count. */
7ee70bf5 1839 printf_filtered (_("Log contains %u instructions.\n"),
88d1aa9d 1840 record_full_insn_num);
d02ed0bb
MM
1841 }
1842 else
1843 printf_filtered (_("No instructions have been logged.\n"));
1844
1845 /* Display max log size. */
7ee70bf5 1846 printf_filtered (_("Max logged instructions is %u.\n"),
88d1aa9d 1847 record_full_insn_max_num);
d02ed0bb
MM
1848}
1849
1850/* The "to_record_delete" target method. */
1851
1852static void
d1b55219 1853record_full_delete (struct target_ops *self)
d02ed0bb 1854{
88d1aa9d 1855 record_full_list_release_following (record_full_list);
d02ed0bb
MM
1856}
1857
1858/* The "to_record_is_replaying" target method. */
1859
1860static int
a52eab48 1861record_full_is_replaying (struct target_ops *self, ptid_t ptid)
d02ed0bb 1862{
88d1aa9d 1863 return RECORD_FULL_IS_REPLAY;
d02ed0bb
MM
1864}
1865
7ff27e9b
MM
1866/* The "to_record_will_replay" target method. */
1867
1868static int
1869record_full_will_replay (struct target_ops *self, ptid_t ptid, int dir)
1870{
1871 /* We can currently only record when executing forwards. Should we be able
1872 to record when executing backwards on targets that support reverse
1873 execution, this needs to be changed. */
1874
1875 return RECORD_FULL_IS_REPLAY || dir == EXEC_REVERSE;
1876}
1877
d02ed0bb
MM
1878/* Go to a specific entry. */
1879
1880static void
88d1aa9d 1881record_full_goto_entry (struct record_full_entry *p)
d02ed0bb
MM
1882{
1883 if (p == NULL)
1884 error (_("Target insn not found."));
88d1aa9d 1885 else if (p == record_full_list)
d02ed0bb 1886 error (_("Already at target insn."));
88d1aa9d 1887 else if (p->u.end.insn_num > record_full_list->u.end.insn_num)
d02ed0bb
MM
1888 {
1889 printf_filtered (_("Go forward to insn number %s\n"),
1890 pulongest (p->u.end.insn_num));
88d1aa9d 1891 record_full_goto_insn (p, EXEC_FORWARD);
d02ed0bb
MM
1892 }
1893 else
1894 {
1895 printf_filtered (_("Go backward to insn number %s\n"),
1896 pulongest (p->u.end.insn_num));
88d1aa9d 1897 record_full_goto_insn (p, EXEC_REVERSE);
d02ed0bb
MM
1898 }
1899
1900 registers_changed ();
1901 reinit_frame_cache ();
485668e5 1902 stop_pc = regcache_read_pc (get_current_regcache ());
08d72866 1903 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
d02ed0bb
MM
1904}
1905
1906/* The "to_goto_record_begin" target method. */
1907
1908static void
08475817 1909record_full_goto_begin (struct target_ops *self)
d02ed0bb 1910{
88d1aa9d 1911 struct record_full_entry *p = NULL;
d02ed0bb 1912
88d1aa9d
MM
1913 for (p = &record_full_first; p != NULL; p = p->next)
1914 if (p->type == record_full_end)
d02ed0bb
MM
1915 break;
1916
88d1aa9d 1917 record_full_goto_entry (p);
d02ed0bb
MM
1918}
1919
1920/* The "to_goto_record_end" target method. */
1921
1922static void
307a1b91 1923record_full_goto_end (struct target_ops *self)
d02ed0bb 1924{
88d1aa9d 1925 struct record_full_entry *p = NULL;
d02ed0bb 1926
88d1aa9d 1927 for (p = record_full_list; p->next != NULL; p = p->next)
d02ed0bb
MM
1928 ;
1929 for (; p!= NULL; p = p->prev)
88d1aa9d 1930 if (p->type == record_full_end)
d02ed0bb
MM
1931 break;
1932
88d1aa9d 1933 record_full_goto_entry (p);
d02ed0bb
MM
1934}
1935
1936/* The "to_goto_record" target method. */
1937
1938static void
606183ac 1939record_full_goto (struct target_ops *self, ULONGEST target_insn)
d02ed0bb 1940{
88d1aa9d 1941 struct record_full_entry *p = NULL;
d02ed0bb 1942
88d1aa9d
MM
1943 for (p = &record_full_first; p != NULL; p = p->next)
1944 if (p->type == record_full_end && p->u.end.insn_num == target_insn)
d02ed0bb
MM
1945 break;
1946
88d1aa9d 1947 record_full_goto_entry (p);
d02ed0bb
MM
1948}
1949
797094dd
MM
1950/* The "to_record_stop_replaying" target method. */
1951
1952static void
1953record_full_stop_replaying (struct target_ops *self)
1954{
1955 record_full_goto_end (self);
1956}
1957
d02ed0bb 1958static void
88d1aa9d 1959init_record_full_ops (void)
d02ed0bb 1960{
88d1aa9d
MM
1961 record_full_ops.to_shortname = "record-full";
1962 record_full_ops.to_longname = "Process record and replay target";
1963 record_full_ops.to_doc =
d02ed0bb 1964 "Log program while executing and replay execution from log.";
88d1aa9d
MM
1965 record_full_ops.to_open = record_full_open;
1966 record_full_ops.to_close = record_full_close;
b7d2e916 1967 record_full_ops.to_async = record_full_async;
88d1aa9d 1968 record_full_ops.to_resume = record_full_resume;
85ad3aaf 1969 record_full_ops.to_commit_resume = record_full_commit_resume;
88d1aa9d 1970 record_full_ops.to_wait = record_full_wait;
7c1687a9
MM
1971 record_full_ops.to_disconnect = record_disconnect;
1972 record_full_ops.to_detach = record_detach;
1973 record_full_ops.to_mourn_inferior = record_mourn_inferior;
1974 record_full_ops.to_kill = record_kill;
88d1aa9d
MM
1975 record_full_ops.to_store_registers = record_full_store_registers;
1976 record_full_ops.to_xfer_partial = record_full_xfer_partial;
1977 record_full_ops.to_insert_breakpoint = record_full_insert_breakpoint;
1978 record_full_ops.to_remove_breakpoint = record_full_remove_breakpoint;
1979 record_full_ops.to_stopped_by_watchpoint = record_full_stopped_by_watchpoint;
1980 record_full_ops.to_stopped_data_address = record_full_stopped_data_address;
9e8915c6
PA
1981 record_full_ops.to_stopped_by_sw_breakpoint
1982 = record_full_stopped_by_sw_breakpoint;
1983 record_full_ops.to_supports_stopped_by_sw_breakpoint
1984 = record_full_supports_stopped_by_sw_breakpoint;
1985 record_full_ops.to_stopped_by_hw_breakpoint
1986 = record_full_stopped_by_hw_breakpoint;
1987 record_full_ops.to_supports_stopped_by_hw_breakpoint
1988 = record_full_supports_stopped_by_hw_breakpoint;
88d1aa9d
MM
1989 record_full_ops.to_can_execute_reverse = record_full_can_execute_reverse;
1990 record_full_ops.to_stratum = record_stratum;
d02ed0bb 1991 /* Add bookmark target methods. */
88d1aa9d
MM
1992 record_full_ops.to_get_bookmark = record_full_get_bookmark;
1993 record_full_ops.to_goto_bookmark = record_full_goto_bookmark;
88d1aa9d
MM
1994 record_full_ops.to_execution_direction = record_full_execution_direction;
1995 record_full_ops.to_info_record = record_full_info;
1996 record_full_ops.to_save_record = record_full_save;
1997 record_full_ops.to_delete_record = record_full_delete;
1998 record_full_ops.to_record_is_replaying = record_full_is_replaying;
7ff27e9b 1999 record_full_ops.to_record_will_replay = record_full_will_replay;
797094dd 2000 record_full_ops.to_record_stop_replaying = record_full_stop_replaying;
88d1aa9d
MM
2001 record_full_ops.to_goto_record_begin = record_full_goto_begin;
2002 record_full_ops.to_goto_record_end = record_full_goto_end;
2003 record_full_ops.to_goto_record = record_full_goto;
2004 record_full_ops.to_magic = OPS_MAGIC;
d02ed0bb
MM
2005}
2006
2007/* "to_resume" method for prec over corefile. */
2008
2009static void
88d1aa9d
MM
2010record_full_core_resume (struct target_ops *ops, ptid_t ptid, int step,
2011 enum gdb_signal signal)
d02ed0bb 2012{
88d1aa9d
MM
2013 record_full_resume_step = step;
2014 record_full_resumed = 1;
2015 record_full_execution_dir = execution_direction;
d02ed0bb
MM
2016
2017 /* We are about to start executing the inferior (or simulate it),
2018 let's register it with the event loop. */
2019 if (target_can_async_p ())
6a3753b3 2020 target_async (1);
d02ed0bb
MM
2021}
2022
2023/* "to_kill" method for prec over corefile. */
2024
2025static void
88d1aa9d 2026record_full_core_kill (struct target_ops *ops)
d02ed0bb
MM
2027{
2028 if (record_debug)
88d1aa9d 2029 fprintf_unfiltered (gdb_stdlog, "Process record: record_full_core_kill\n");
d02ed0bb 2030
88d1aa9d 2031 unpush_target (&record_full_core_ops);
d02ed0bb
MM
2032}
2033
2034/* "to_fetch_registers" method for prec over corefile. */
2035
2036static void
88d1aa9d
MM
2037record_full_core_fetch_registers (struct target_ops *ops,
2038 struct regcache *regcache,
2039 int regno)
d02ed0bb
MM
2040{
2041 if (regno < 0)
2042 {
2043 int num = gdbarch_num_regs (get_regcache_arch (regcache));
2044 int i;
2045
2046 for (i = 0; i < num; i ++)
2047 regcache_raw_supply (regcache, i,
88d1aa9d 2048 record_full_core_regbuf + MAX_REGISTER_SIZE * i);
d02ed0bb
MM
2049 }
2050 else
2051 regcache_raw_supply (regcache, regno,
88d1aa9d 2052 record_full_core_regbuf + MAX_REGISTER_SIZE * regno);
d02ed0bb
MM
2053}
2054
2055/* "to_prepare_to_store" method for prec over corefile. */
2056
2057static void
f32dbf8c
MM
2058record_full_core_prepare_to_store (struct target_ops *self,
2059 struct regcache *regcache)
d02ed0bb
MM
2060{
2061}
2062
2063/* "to_store_registers" method for prec over corefile. */
2064
2065static void
88d1aa9d 2066record_full_core_store_registers (struct target_ops *ops,
d02ed0bb
MM
2067 struct regcache *regcache,
2068 int regno)
2069{
88d1aa9d 2070 if (record_full_gdb_operation_disable)
d02ed0bb 2071 regcache_raw_collect (regcache, regno,
88d1aa9d 2072 record_full_core_regbuf + MAX_REGISTER_SIZE * regno);
d02ed0bb
MM
2073 else
2074 error (_("You can't do that without a process to debug."));
2075}
2076
2077/* "to_xfer_partial" method for prec over corefile. */
2078
9b409511 2079static enum target_xfer_status
88d1aa9d
MM
2080record_full_core_xfer_partial (struct target_ops *ops,
2081 enum target_object object,
2082 const char *annex, gdb_byte *readbuf,
2083 const gdb_byte *writebuf, ULONGEST offset,
9b409511 2084 ULONGEST len, ULONGEST *xfered_len)
d02ed0bb
MM
2085{
2086 if (object == TARGET_OBJECT_MEMORY)
2087 {
88d1aa9d 2088 if (record_full_gdb_operation_disable || !writebuf)
d02ed0bb
MM
2089 {
2090 struct target_section *p;
2091
88d1aa9d 2092 for (p = record_full_core_start; p < record_full_core_end; p++)
d02ed0bb
MM
2093 {
2094 if (offset >= p->addr)
2095 {
88d1aa9d 2096 struct record_full_core_buf_entry *entry;
d02ed0bb
MM
2097 ULONGEST sec_offset;
2098
2099 if (offset >= p->endaddr)
2100 continue;
2101
2102 if (offset + len > p->endaddr)
2103 len = p->endaddr - offset;
2104
2105 sec_offset = offset - p->addr;
2106
2107 /* Read readbuf or write writebuf p, offset, len. */
2108 /* Check flags. */
2109 if (p->the_bfd_section->flags & SEC_CONSTRUCTOR
2110 || (p->the_bfd_section->flags & SEC_HAS_CONTENTS) == 0)
2111 {
2112 if (readbuf)
2113 memset (readbuf, 0, len);
9b409511
YQ
2114
2115 *xfered_len = len;
2116 return TARGET_XFER_OK;
d02ed0bb 2117 }
88d1aa9d
MM
2118 /* Get record_full_core_buf_entry. */
2119 for (entry = record_full_core_buf_list; entry;
d02ed0bb
MM
2120 entry = entry->prev)
2121 if (entry->p == p)
2122 break;
2123 if (writebuf)
2124 {
2125 if (!entry)
2126 {
2127 /* Add a new entry. */
8d749320 2128 entry = XNEW (struct record_full_core_buf_entry);
d02ed0bb 2129 entry->p = p;
2b2848e2
DE
2130 if (!bfd_malloc_and_get_section
2131 (p->the_bfd_section->owner,
2132 p->the_bfd_section,
2133 &entry->buf))
d02ed0bb
MM
2134 {
2135 xfree (entry);
9b409511 2136 return TARGET_XFER_EOF;
d02ed0bb 2137 }
88d1aa9d
MM
2138 entry->prev = record_full_core_buf_list;
2139 record_full_core_buf_list = entry;
d02ed0bb
MM
2140 }
2141
2142 memcpy (entry->buf + sec_offset, writebuf,
2143 (size_t) len);
2144 }
2145 else
2146 {
2147 if (!entry)
6b84065d
TT
2148 return ops->beneath->to_xfer_partial (ops->beneath,
2149 object, annex,
2150 readbuf, writebuf,
2151 offset, len,
2152 xfered_len);
d02ed0bb
MM
2153
2154 memcpy (readbuf, entry->buf + sec_offset,
2155 (size_t) len);
2156 }
2157
9b409511
YQ
2158 *xfered_len = len;
2159 return TARGET_XFER_OK;
d02ed0bb
MM
2160 }
2161 }
2162
2ed4b548 2163 return TARGET_XFER_E_IO;
d02ed0bb
MM
2164 }
2165 else
2166 error (_("You can't do that without a process to debug."));
2167 }
2168
6b84065d
TT
2169 return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
2170 readbuf, writebuf, offset, len,
2171 xfered_len);
d02ed0bb
MM
2172}
2173
2174/* "to_insert_breakpoint" method for prec over corefile. */
2175
2176static int
3db08215
MM
2177record_full_core_insert_breakpoint (struct target_ops *ops,
2178 struct gdbarch *gdbarch,
88d1aa9d 2179 struct bp_target_info *bp_tgt)
d02ed0bb
MM
2180{
2181 return 0;
2182}
2183
2184/* "to_remove_breakpoint" method for prec over corefile. */
2185
2186static int
3db08215
MM
2187record_full_core_remove_breakpoint (struct target_ops *ops,
2188 struct gdbarch *gdbarch,
73971819
PA
2189 struct bp_target_info *bp_tgt,
2190 enum remove_bp_reason reason)
d02ed0bb
MM
2191{
2192 return 0;
2193}
2194
2195/* "to_has_execution" method for prec over corefile. */
2196
2197static int
88d1aa9d 2198record_full_core_has_execution (struct target_ops *ops, ptid_t the_ptid)
d02ed0bb
MM
2199{
2200 return 1;
2201}
2202
2203static void
88d1aa9d 2204init_record_full_core_ops (void)
d02ed0bb 2205{
88d1aa9d
MM
2206 record_full_core_ops.to_shortname = "record-core";
2207 record_full_core_ops.to_longname = "Process record and replay target";
2208 record_full_core_ops.to_doc =
d02ed0bb 2209 "Log program while executing and replay execution from log.";
88d1aa9d
MM
2210 record_full_core_ops.to_open = record_full_open;
2211 record_full_core_ops.to_close = record_full_close;
b7d2e916 2212 record_full_core_ops.to_async = record_full_async;
88d1aa9d
MM
2213 record_full_core_ops.to_resume = record_full_core_resume;
2214 record_full_core_ops.to_wait = record_full_wait;
2215 record_full_core_ops.to_kill = record_full_core_kill;
2216 record_full_core_ops.to_fetch_registers = record_full_core_fetch_registers;
2217 record_full_core_ops.to_prepare_to_store = record_full_core_prepare_to_store;
2218 record_full_core_ops.to_store_registers = record_full_core_store_registers;
2219 record_full_core_ops.to_xfer_partial = record_full_core_xfer_partial;
2220 record_full_core_ops.to_insert_breakpoint
2221 = record_full_core_insert_breakpoint;
2222 record_full_core_ops.to_remove_breakpoint
2223 = record_full_core_remove_breakpoint;
2224 record_full_core_ops.to_stopped_by_watchpoint
2225 = record_full_stopped_by_watchpoint;
2226 record_full_core_ops.to_stopped_data_address
2227 = record_full_stopped_data_address;
9e8915c6
PA
2228 record_full_core_ops.to_stopped_by_sw_breakpoint
2229 = record_full_stopped_by_sw_breakpoint;
2230 record_full_core_ops.to_supports_stopped_by_sw_breakpoint
2231 = record_full_supports_stopped_by_sw_breakpoint;
2232 record_full_core_ops.to_stopped_by_hw_breakpoint
2233 = record_full_stopped_by_hw_breakpoint;
2234 record_full_core_ops.to_supports_stopped_by_hw_breakpoint
2235 = record_full_supports_stopped_by_hw_breakpoint;
88d1aa9d
MM
2236 record_full_core_ops.to_can_execute_reverse
2237 = record_full_can_execute_reverse;
2238 record_full_core_ops.to_has_execution = record_full_core_has_execution;
2239 record_full_core_ops.to_stratum = record_stratum;
d02ed0bb 2240 /* Add bookmark target methods. */
88d1aa9d
MM
2241 record_full_core_ops.to_get_bookmark = record_full_get_bookmark;
2242 record_full_core_ops.to_goto_bookmark = record_full_goto_bookmark;
88d1aa9d
MM
2243 record_full_core_ops.to_execution_direction
2244 = record_full_execution_direction;
2245 record_full_core_ops.to_info_record = record_full_info;
2246 record_full_core_ops.to_delete_record = record_full_delete;
2247 record_full_core_ops.to_record_is_replaying = record_full_is_replaying;
7ff27e9b 2248 record_full_core_ops.to_record_will_replay = record_full_will_replay;
88d1aa9d
MM
2249 record_full_core_ops.to_goto_record_begin = record_full_goto_begin;
2250 record_full_core_ops.to_goto_record_end = record_full_goto_end;
2251 record_full_core_ops.to_goto_record = record_full_goto;
2252 record_full_core_ops.to_magic = OPS_MAGIC;
d02ed0bb
MM
2253}
2254
2255/* Record log save-file format
2256 Version 1 (never released)
2257
2258 Header:
2259 4 bytes: magic number htonl(0x20090829).
2260 NOTE: be sure to change whenever this file format changes!
2261
2262 Records:
88d1aa9d
MM
2263 record_full_end:
2264 1 byte: record type (record_full_end, see enum record_full_type).
2265 record_full_reg:
2266 1 byte: record type (record_full_reg, see enum record_full_type).
d02ed0bb
MM
2267 8 bytes: register id (network byte order).
2268 MAX_REGISTER_SIZE bytes: register value.
88d1aa9d
MM
2269 record_full_mem:
2270 1 byte: record type (record_full_mem, see enum record_full_type).
d02ed0bb
MM
2271 8 bytes: memory length (network byte order).
2272 8 bytes: memory address (network byte order).
2273 n bytes: memory value (n == memory length).
2274
2275 Version 2
2276 4 bytes: magic number netorder32(0x20091016).
2277 NOTE: be sure to change whenever this file format changes!
2278
2279 Records:
88d1aa9d
MM
2280 record_full_end:
2281 1 byte: record type (record_full_end, see enum record_full_type).
d02ed0bb
MM
2282 4 bytes: signal
2283 4 bytes: instruction count
88d1aa9d
MM
2284 record_full_reg:
2285 1 byte: record type (record_full_reg, see enum record_full_type).
d02ed0bb
MM
2286 4 bytes: register id (network byte order).
2287 n bytes: register value (n == actual register size).
2288 (eg. 4 bytes for x86 general registers).
88d1aa9d
MM
2289 record_full_mem:
2290 1 byte: record type (record_full_mem, see enum record_full_type).
d02ed0bb
MM
2291 4 bytes: memory length (network byte order).
2292 8 bytes: memory address (network byte order).
2293 n bytes: memory value (n == memory length).
2294
2295*/
2296
2297/* bfdcore_read -- read bytes from a core file section. */
2298
2299static inline void
2300bfdcore_read (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2301{
2302 int ret = bfd_get_section_contents (obfd, osec, buf, *offset, len);
2303
2304 if (ret)
2305 *offset += len;
2306 else
2307 error (_("Failed to read %d bytes from core file %s ('%s')."),
2308 len, bfd_get_filename (obfd),
2309 bfd_errmsg (bfd_get_error ()));
2310}
2311
2312static inline uint64_t
2313netorder64 (uint64_t input)
2314{
2315 uint64_t ret;
2316
2317 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2318 BFD_ENDIAN_BIG, input);
2319 return ret;
2320}
2321
2322static inline uint32_t
2323netorder32 (uint32_t input)
2324{
2325 uint32_t ret;
2326
2327 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2328 BFD_ENDIAN_BIG, input);
2329 return ret;
2330}
2331
2332static inline uint16_t
2333netorder16 (uint16_t input)
2334{
2335 uint16_t ret;
2336
2337 store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
2338 BFD_ENDIAN_BIG, input);
2339 return ret;
2340}
2341
2342/* Restore the execution log from a core_bfd file. */
2343static void
88d1aa9d 2344record_full_restore (void)
d02ed0bb
MM
2345{
2346 uint32_t magic;
2347 struct cleanup *old_cleanups;
88d1aa9d 2348 struct record_full_entry *rec;
d02ed0bb
MM
2349 asection *osec;
2350 uint32_t osec_size;
2351 int bfd_offset = 0;
2352 struct regcache *regcache;
2353
2354 /* We restore the execution log from the open core bfd,
2355 if there is one. */
2356 if (core_bfd == NULL)
2357 return;
2358
88d1aa9d
MM
2359 /* "record_full_restore" can only be called when record list is empty. */
2360 gdb_assert (record_full_first.next == NULL);
d02ed0bb
MM
2361
2362 if (record_debug)
2363 fprintf_unfiltered (gdb_stdlog, "Restoring recording from core file.\n");
2364
2365 /* Now need to find our special note section. */
2366 osec = bfd_get_section_by_name (core_bfd, "null0");
2367 if (record_debug)
2368 fprintf_unfiltered (gdb_stdlog, "Find precord section %s.\n",
2369 osec ? "succeeded" : "failed");
2370 if (osec == NULL)
2371 return;
2372 osec_size = bfd_section_size (core_bfd, osec);
2373 if (record_debug)
2374 fprintf_unfiltered (gdb_stdlog, "%s", bfd_section_name (core_bfd, osec));
2375
2376 /* Check the magic code. */
2377 bfdcore_read (core_bfd, osec, &magic, sizeof (magic), &bfd_offset);
88d1aa9d 2378 if (magic != RECORD_FULL_FILE_MAGIC)
d02ed0bb
MM
2379 error (_("Version mis-match or file format error in core file %s."),
2380 bfd_get_filename (core_bfd));
2381 if (record_debug)
2382 fprintf_unfiltered (gdb_stdlog,
2383 " Reading 4-byte magic cookie "
88d1aa9d 2384 "RECORD_FULL_FILE_MAGIC (0x%s)\n",
d02ed0bb
MM
2385 phex_nz (netorder32 (magic), 4));
2386
88d1aa9d
MM
2387 /* Restore the entries in recfd into record_full_arch_list_head and
2388 record_full_arch_list_tail. */
2389 record_full_arch_list_head = NULL;
2390 record_full_arch_list_tail = NULL;
2391 record_full_insn_num = 0;
2392 old_cleanups = make_cleanup (record_full_arch_list_cleanups, 0);
d02ed0bb
MM
2393 regcache = get_current_regcache ();
2394
2395 while (1)
2396 {
2397 uint8_t rectype;
2398 uint32_t regnum, len, signal, count;
2399 uint64_t addr;
2400
2401 /* We are finished when offset reaches osec_size. */
2402 if (bfd_offset >= osec_size)
2403 break;
2404 bfdcore_read (core_bfd, osec, &rectype, sizeof (rectype), &bfd_offset);
2405
2406 switch (rectype)
2407 {
88d1aa9d 2408 case record_full_reg: /* reg */
d02ed0bb
MM
2409 /* Get register number to regnum. */
2410 bfdcore_read (core_bfd, osec, &regnum,
2411 sizeof (regnum), &bfd_offset);
2412 regnum = netorder32 (regnum);
2413
88d1aa9d 2414 rec = record_full_reg_alloc (regcache, regnum);
d02ed0bb
MM
2415
2416 /* Get val. */
88d1aa9d 2417 bfdcore_read (core_bfd, osec, record_full_get_loc (rec),
d02ed0bb
MM
2418 rec->u.reg.len, &bfd_offset);
2419
2420 if (record_debug)
2421 fprintf_unfiltered (gdb_stdlog,
2422 " Reading register %d (1 "
2423 "plus %lu plus %d bytes)\n",
2424 rec->u.reg.num,
2425 (unsigned long) sizeof (regnum),
2426 rec->u.reg.len);
2427 break;
2428
88d1aa9d 2429 case record_full_mem: /* mem */
d02ed0bb
MM
2430 /* Get len. */
2431 bfdcore_read (core_bfd, osec, &len,
2432 sizeof (len), &bfd_offset);
2433 len = netorder32 (len);
2434
2435 /* Get addr. */
2436 bfdcore_read (core_bfd, osec, &addr,
2437 sizeof (addr), &bfd_offset);
2438 addr = netorder64 (addr);
2439
88d1aa9d 2440 rec = record_full_mem_alloc (addr, len);
d02ed0bb
MM
2441
2442 /* Get val. */
88d1aa9d 2443 bfdcore_read (core_bfd, osec, record_full_get_loc (rec),
d02ed0bb
MM
2444 rec->u.mem.len, &bfd_offset);
2445
2446 if (record_debug)
2447 fprintf_unfiltered (gdb_stdlog,
2448 " Reading memory %s (1 plus "
2449 "%lu plus %lu plus %d bytes)\n",
2450 paddress (get_current_arch (),
2451 rec->u.mem.addr),
2452 (unsigned long) sizeof (addr),
2453 (unsigned long) sizeof (len),
2454 rec->u.mem.len);
2455 break;
2456
88d1aa9d
MM
2457 case record_full_end: /* end */
2458 rec = record_full_end_alloc ();
2459 record_full_insn_num ++;
d02ed0bb
MM
2460
2461 /* Get signal value. */
2462 bfdcore_read (core_bfd, osec, &signal,
2463 sizeof (signal), &bfd_offset);
2464 signal = netorder32 (signal);
aead7601 2465 rec->u.end.sigval = (enum gdb_signal) signal;
d02ed0bb
MM
2466
2467 /* Get insn count. */
2468 bfdcore_read (core_bfd, osec, &count,
2469 sizeof (count), &bfd_offset);
2470 count = netorder32 (count);
2471 rec->u.end.insn_num = count;
88d1aa9d 2472 record_full_insn_count = count + 1;
d02ed0bb
MM
2473 if (record_debug)
2474 fprintf_unfiltered (gdb_stdlog,
88d1aa9d 2475 " Reading record_full_end (1 + "
d02ed0bb
MM
2476 "%lu + %lu bytes), offset == %s\n",
2477 (unsigned long) sizeof (signal),
2478 (unsigned long) sizeof (count),
2479 paddress (get_current_arch (),
2480 bfd_offset));
2481 break;
2482
2483 default:
2484 error (_("Bad entry type in core file %s."),
2485 bfd_get_filename (core_bfd));
2486 break;
2487 }
2488
2489 /* Add rec to record arch list. */
88d1aa9d 2490 record_full_arch_list_add (rec);
d02ed0bb
MM
2491 }
2492
2493 discard_cleanups (old_cleanups);
2494
88d1aa9d
MM
2495 /* Add record_full_arch_list_head to the end of record list. */
2496 record_full_first.next = record_full_arch_list_head;
2497 record_full_arch_list_head->prev = &record_full_first;
2498 record_full_arch_list_tail->next = NULL;
2499 record_full_list = &record_full_first;
d02ed0bb 2500
88d1aa9d
MM
2501 /* Update record_full_insn_max_num. */
2502 if (record_full_insn_num > record_full_insn_max_num)
d02ed0bb 2503 {
88d1aa9d 2504 record_full_insn_max_num = record_full_insn_num;
7ee70bf5 2505 warning (_("Auto increase record/replay buffer limit to %u."),
88d1aa9d 2506 record_full_insn_max_num);
d02ed0bb
MM
2507 }
2508
2509 /* Succeeded. */
2510 printf_filtered (_("Restored records from core file %s.\n"),
2511 bfd_get_filename (core_bfd));
2512
08d72866 2513 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
d02ed0bb
MM
2514}
2515
2516/* bfdcore_write -- write bytes into a core file section. */
2517
2518static inline void
2519bfdcore_write (bfd *obfd, asection *osec, void *buf, int len, int *offset)
2520{
2521 int ret = bfd_set_section_contents (obfd, osec, buf, *offset, len);
2522
2523 if (ret)
2524 *offset += len;
2525 else
2526 error (_("Failed to write %d bytes to core file %s ('%s')."),
2527 len, bfd_get_filename (obfd),
2528 bfd_errmsg (bfd_get_error ()));
2529}
2530
2531/* Restore the execution log from a file. We use a modified elf
2532 corefile format, with an extra section for our data. */
2533
2534static void
88d1aa9d 2535cmd_record_full_restore (char *args, int from_tty)
d02ed0bb
MM
2536{
2537 core_file_command (args, from_tty);
88d1aa9d 2538 record_full_open (args, from_tty);
d02ed0bb
MM
2539}
2540
d02ed0bb
MM
2541/* Save the execution log to a file. We use a modified elf corefile
2542 format, with an extra section for our data. */
2543
2544static void
1390f529 2545record_full_save (struct target_ops *self, const char *recfilename)
d02ed0bb 2546{
88d1aa9d 2547 struct record_full_entry *cur_record_full_list;
d02ed0bb
MM
2548 uint32_t magic;
2549 struct regcache *regcache;
2550 struct gdbarch *gdbarch;
d02ed0bb 2551 struct cleanup *set_cleanups;
d02ed0bb
MM
2552 int save_size = 0;
2553 asection *osec = NULL;
2554 int bfd_offset = 0;
2555
2556 /* Open the save file. */
2557 if (record_debug)
2558 fprintf_unfiltered (gdb_stdlog, "Saving execution log to core file '%s'\n",
2559 recfilename);
2560
2561 /* Open the output file. */
bef155c3
TT
2562 gdb_bfd_ref_ptr obfd (create_gcore_bfd (recfilename));
2563
2564 /* Arrange to remove the output file on failure. */
2565 gdb::unlinker unlink_file (recfilename);
d02ed0bb 2566
88d1aa9d
MM
2567 /* Save the current record entry to "cur_record_full_list". */
2568 cur_record_full_list = record_full_list;
d02ed0bb
MM
2569
2570 /* Get the values of regcache and gdbarch. */
2571 regcache = get_current_regcache ();
2572 gdbarch = get_regcache_arch (regcache);
2573
2574 /* Disable the GDB operation record. */
25ea693b 2575 set_cleanups = record_full_gdb_operation_disable_set ();
d02ed0bb
MM
2576
2577 /* Reverse execute to the begin of record list. */
2578 while (1)
2579 {
2580 /* Check for beginning and end of log. */
88d1aa9d 2581 if (record_full_list == &record_full_first)
d02ed0bb
MM
2582 break;
2583
88d1aa9d 2584 record_full_exec_insn (regcache, gdbarch, record_full_list);
d02ed0bb 2585
88d1aa9d
MM
2586 if (record_full_list->prev)
2587 record_full_list = record_full_list->prev;
d02ed0bb
MM
2588 }
2589
2590 /* Compute the size needed for the extra bfd section. */
2591 save_size = 4; /* magic cookie */
88d1aa9d
MM
2592 for (record_full_list = record_full_first.next; record_full_list;
2593 record_full_list = record_full_list->next)
2594 switch (record_full_list->type)
d02ed0bb 2595 {
88d1aa9d 2596 case record_full_end:
d02ed0bb
MM
2597 save_size += 1 + 4 + 4;
2598 break;
88d1aa9d
MM
2599 case record_full_reg:
2600 save_size += 1 + 4 + record_full_list->u.reg.len;
d02ed0bb 2601 break;
88d1aa9d
MM
2602 case record_full_mem:
2603 save_size += 1 + 4 + 8 + record_full_list->u.mem.len;
d02ed0bb
MM
2604 break;
2605 }
2606
2607 /* Make the new bfd section. */
bef155c3 2608 osec = bfd_make_section_anyway_with_flags (obfd.get (), "precord",
d02ed0bb
MM
2609 SEC_HAS_CONTENTS
2610 | SEC_READONLY);
2611 if (osec == NULL)
2612 error (_("Failed to create 'precord' section for corefile %s: %s"),
2613 recfilename,
2614 bfd_errmsg (bfd_get_error ()));
bef155c3
TT
2615 bfd_set_section_size (obfd.get (), osec, save_size);
2616 bfd_set_section_vma (obfd.get (), osec, 0);
2617 bfd_set_section_alignment (obfd.get (), osec, 0);
2618 bfd_section_lma (obfd.get (), osec) = 0;
d02ed0bb
MM
2619
2620 /* Save corefile state. */
bef155c3 2621 write_gcore_file (obfd.get ());
d02ed0bb
MM
2622
2623 /* Write out the record log. */
2624 /* Write the magic code. */
88d1aa9d 2625 magic = RECORD_FULL_FILE_MAGIC;
d02ed0bb
MM
2626 if (record_debug)
2627 fprintf_unfiltered (gdb_stdlog,
2628 " Writing 4-byte magic cookie "
88d1aa9d 2629 "RECORD_FULL_FILE_MAGIC (0x%s)\n",
d02ed0bb 2630 phex_nz (magic, 4));
bef155c3 2631 bfdcore_write (obfd.get (), osec, &magic, sizeof (magic), &bfd_offset);
d02ed0bb
MM
2632
2633 /* Save the entries to recfd and forward execute to the end of
2634 record list. */
88d1aa9d 2635 record_full_list = &record_full_first;
d02ed0bb
MM
2636 while (1)
2637 {
2638 /* Save entry. */
88d1aa9d 2639 if (record_full_list != &record_full_first)
d02ed0bb
MM
2640 {
2641 uint8_t type;
2642 uint32_t regnum, len, signal, count;
2643 uint64_t addr;
2644
88d1aa9d 2645 type = record_full_list->type;
bef155c3 2646 bfdcore_write (obfd.get (), osec, &type, sizeof (type), &bfd_offset);
d02ed0bb 2647
88d1aa9d 2648 switch (record_full_list->type)
d02ed0bb 2649 {
88d1aa9d 2650 case record_full_reg: /* reg */
d02ed0bb
MM
2651 if (record_debug)
2652 fprintf_unfiltered (gdb_stdlog,
2653 " Writing register %d (1 "
2654 "plus %lu plus %d bytes)\n",
88d1aa9d 2655 record_full_list->u.reg.num,
d02ed0bb 2656 (unsigned long) sizeof (regnum),
88d1aa9d 2657 record_full_list->u.reg.len);
d02ed0bb
MM
2658
2659 /* Write regnum. */
88d1aa9d 2660 regnum = netorder32 (record_full_list->u.reg.num);
bef155c3 2661 bfdcore_write (obfd.get (), osec, &regnum,
d02ed0bb
MM
2662 sizeof (regnum), &bfd_offset);
2663
2664 /* Write regval. */
bef155c3 2665 bfdcore_write (obfd.get (), osec,
88d1aa9d
MM
2666 record_full_get_loc (record_full_list),
2667 record_full_list->u.reg.len, &bfd_offset);
d02ed0bb
MM
2668 break;
2669
88d1aa9d 2670 case record_full_mem: /* mem */
d02ed0bb
MM
2671 if (record_debug)
2672 fprintf_unfiltered (gdb_stdlog,
2673 " Writing memory %s (1 plus "
2674 "%lu plus %lu plus %d bytes)\n",
2675 paddress (gdbarch,
88d1aa9d 2676 record_full_list->u.mem.addr),
d02ed0bb
MM
2677 (unsigned long) sizeof (addr),
2678 (unsigned long) sizeof (len),
88d1aa9d 2679 record_full_list->u.mem.len);
d02ed0bb
MM
2680
2681 /* Write memlen. */
88d1aa9d 2682 len = netorder32 (record_full_list->u.mem.len);
bef155c3
TT
2683 bfdcore_write (obfd.get (), osec, &len, sizeof (len),
2684 &bfd_offset);
d02ed0bb
MM
2685
2686 /* Write memaddr. */
88d1aa9d 2687 addr = netorder64 (record_full_list->u.mem.addr);
bef155c3 2688 bfdcore_write (obfd.get (), osec, &addr,
d02ed0bb
MM
2689 sizeof (addr), &bfd_offset);
2690
2691 /* Write memval. */
bef155c3 2692 bfdcore_write (obfd.get (), osec,
88d1aa9d
MM
2693 record_full_get_loc (record_full_list),
2694 record_full_list->u.mem.len, &bfd_offset);
d02ed0bb
MM
2695 break;
2696
88d1aa9d 2697 case record_full_end:
d02ed0bb
MM
2698 if (record_debug)
2699 fprintf_unfiltered (gdb_stdlog,
88d1aa9d 2700 " Writing record_full_end (1 + "
d02ed0bb
MM
2701 "%lu + %lu bytes)\n",
2702 (unsigned long) sizeof (signal),
2703 (unsigned long) sizeof (count));
2704 /* Write signal value. */
88d1aa9d 2705 signal = netorder32 (record_full_list->u.end.sigval);
bef155c3 2706 bfdcore_write (obfd.get (), osec, &signal,
d02ed0bb
MM
2707 sizeof (signal), &bfd_offset);
2708
2709 /* Write insn count. */
88d1aa9d 2710 count = netorder32 (record_full_list->u.end.insn_num);
bef155c3 2711 bfdcore_write (obfd.get (), osec, &count,
d02ed0bb
MM
2712 sizeof (count), &bfd_offset);
2713 break;
2714 }
2715 }
2716
2717 /* Execute entry. */
88d1aa9d 2718 record_full_exec_insn (regcache, gdbarch, record_full_list);
d02ed0bb 2719
88d1aa9d
MM
2720 if (record_full_list->next)
2721 record_full_list = record_full_list->next;
d02ed0bb
MM
2722 else
2723 break;
2724 }
2725
88d1aa9d 2726 /* Reverse execute to cur_record_full_list. */
d02ed0bb
MM
2727 while (1)
2728 {
2729 /* Check for beginning and end of log. */
88d1aa9d 2730 if (record_full_list == cur_record_full_list)
d02ed0bb
MM
2731 break;
2732
88d1aa9d 2733 record_full_exec_insn (regcache, gdbarch, record_full_list);
d02ed0bb 2734
88d1aa9d
MM
2735 if (record_full_list->prev)
2736 record_full_list = record_full_list->prev;
d02ed0bb
MM
2737 }
2738
2739 do_cleanups (set_cleanups);
bef155c3 2740 unlink_file.keep ();
d02ed0bb
MM
2741
2742 /* Succeeded. */
2743 printf_filtered (_("Saved core file %s with execution log.\n"),
2744 recfilename);
2745}
2746
88d1aa9d 2747/* record_full_goto_insn -- rewind the record log (forward or backward,
d02ed0bb
MM
2748 depending on DIR) to the given entry, changing the program state
2749 correspondingly. */
2750
2751static void
88d1aa9d
MM
2752record_full_goto_insn (struct record_full_entry *entry,
2753 enum exec_direction_kind dir)
d02ed0bb 2754{
25ea693b 2755 struct cleanup *set_cleanups = record_full_gdb_operation_disable_set ();
d02ed0bb
MM
2756 struct regcache *regcache = get_current_regcache ();
2757 struct gdbarch *gdbarch = get_regcache_arch (regcache);
2758
2759 /* Assume everything is valid: we will hit the entry,
2760 and we will not hit the end of the recording. */
2761
2762 if (dir == EXEC_FORWARD)
88d1aa9d 2763 record_full_list = record_full_list->next;
d02ed0bb
MM
2764
2765 do
2766 {
88d1aa9d 2767 record_full_exec_insn (regcache, gdbarch, record_full_list);
d02ed0bb 2768 if (dir == EXEC_REVERSE)
88d1aa9d 2769 record_full_list = record_full_list->prev;
d02ed0bb 2770 else
88d1aa9d
MM
2771 record_full_list = record_full_list->next;
2772 } while (record_full_list != entry);
d02ed0bb
MM
2773 do_cleanups (set_cleanups);
2774}
2775
2776/* Alias for "target record-full". */
2777
2778static void
88d1aa9d 2779cmd_record_full_start (char *args, int from_tty)
d02ed0bb
MM
2780{
2781 execute_command ("target record-full", from_tty);
2782}
2783
2784static void
88d1aa9d
MM
2785set_record_full_insn_max_num (char *args, int from_tty,
2786 struct cmd_list_element *c)
d02ed0bb 2787{
7ee70bf5 2788 if (record_full_insn_num > record_full_insn_max_num)
d02ed0bb 2789 {
88d1aa9d
MM
2790 /* Count down record_full_insn_num while releasing records from list. */
2791 while (record_full_insn_num > record_full_insn_max_num)
d02ed0bb 2792 {
88d1aa9d
MM
2793 record_full_list_release_first ();
2794 record_full_insn_num--;
d02ed0bb
MM
2795 }
2796 }
2797}
2798
2799/* The "set record full" command. */
2800
2801static void
2802set_record_full_command (char *args, int from_tty)
2803{
2804 printf_unfiltered (_("\"set record full\" must be followed "
2805 "by an apporpriate subcommand.\n"));
2806 help_list (set_record_full_cmdlist, "set record full ", all_commands,
2807 gdb_stdout);
2808}
2809
2810/* The "show record full" command. */
2811
2812static void
2813show_record_full_command (char *args, int from_tty)
2814{
2815 cmd_show_list (show_record_full_cmdlist, from_tty, "");
2816}
2817
2818/* Provide a prototype to silence -Wmissing-prototypes. */
2819extern initialize_file_ftype _initialize_record_full;
2820
2821void
2822_initialize_record_full (void)
2823{
2824 struct cmd_list_element *c;
2825
88d1aa9d
MM
2826 /* Init record_full_first. */
2827 record_full_first.prev = NULL;
2828 record_full_first.next = NULL;
2829 record_full_first.type = record_full_end;
d02ed0bb 2830
88d1aa9d
MM
2831 init_record_full_ops ();
2832 add_target (&record_full_ops);
2833 add_deprecated_target_alias (&record_full_ops, "record");
2834 init_record_full_core_ops ();
2835 add_target (&record_full_core_ops);
d02ed0bb 2836
88d1aa9d 2837 add_prefix_cmd ("full", class_obscure, cmd_record_full_start,
d02ed0bb
MM
2838 _("Start full execution recording."), &record_full_cmdlist,
2839 "record full ", 0, &record_cmdlist);
2840
88d1aa9d 2841 c = add_cmd ("restore", class_obscure, cmd_record_full_restore,
d02ed0bb
MM
2842 _("Restore the execution log from a file.\n\
2843Argument is filename. File must be created with 'record save'."),
2844 &record_full_cmdlist);
2845 set_cmd_completer (c, filename_completer);
2846
2847 /* Deprecate the old version without "full" prefix. */
2848 c = add_alias_cmd ("restore", "full restore", class_obscure, 1,
2849 &record_cmdlist);
2850 set_cmd_completer (c, filename_completer);
2851 deprecate_cmd (c, "record full restore");
2852
2853 add_prefix_cmd ("full", class_support, set_record_full_command,
2854 _("Set record options"), &set_record_full_cmdlist,
2855 "set record full ", 0, &set_record_cmdlist);
2856
2857 add_prefix_cmd ("full", class_support, show_record_full_command,
2858 _("Show record options"), &show_record_full_cmdlist,
2859 "show record full ", 0, &show_record_cmdlist);
2860
2861 /* Record instructions number limit command. */
2862 add_setshow_boolean_cmd ("stop-at-limit", no_class,
88d1aa9d 2863 &record_full_stop_at_limit, _("\
d02ed0bb
MM
2864Set whether record/replay stops when record/replay buffer becomes full."), _("\
2865Show whether record/replay stops when record/replay buffer becomes full."),
2866 _("Default is ON.\n\
2867When ON, if the record/replay buffer becomes full, ask user what to do.\n\
2868When OFF, if the record/replay buffer becomes full,\n\
2869delete the oldest recorded instruction to make room for each new one."),
2870 NULL, NULL,
2871 &set_record_full_cmdlist, &show_record_full_cmdlist);
2872
2873 c = add_alias_cmd ("stop-at-limit", "full stop-at-limit", no_class, 1,
2874 &set_record_cmdlist);
2875 deprecate_cmd (c, "set record full stop-at-limit");
2876
2877 c = add_alias_cmd ("stop-at-limit", "full stop-at-limit", no_class, 1,
2878 &show_record_cmdlist);
2879 deprecate_cmd (c, "show record full stop-at-limit");
2880
88d1aa9d
MM
2881 add_setshow_uinteger_cmd ("insn-number-max", no_class,
2882 &record_full_insn_max_num,
d02ed0bb
MM
2883 _("Set record/replay buffer limit."),
2884 _("Show record/replay buffer limit."), _("\
2885Set the maximum number of instructions to be stored in the\n\
f81d1120
PA
2886record/replay buffer. A value of either \"unlimited\" or zero means no\n\
2887limit. Default is 200000."),
88d1aa9d 2888 set_record_full_insn_max_num,
d02ed0bb
MM
2889 NULL, &set_record_full_cmdlist,
2890 &show_record_full_cmdlist);
2891
2892 c = add_alias_cmd ("insn-number-max", "full insn-number-max", no_class, 1,
2893 &set_record_cmdlist);
2894 deprecate_cmd (c, "set record full insn-number-max");
2895
2896 c = add_alias_cmd ("insn-number-max", "full insn-number-max", no_class, 1,
2897 &show_record_cmdlist);
2898 deprecate_cmd (c, "show record full insn-number-max");
2899
88d1aa9d 2900 add_setshow_boolean_cmd ("memory-query", no_class,
25ea693b 2901 &record_full_memory_query, _("\
d02ed0bb
MM
2902Set whether query if PREC cannot record memory change of next instruction."),
2903 _("\
2904Show whether query if PREC cannot record memory change of next instruction."),
2905 _("\
2906Default is OFF.\n\
2907When ON, query if PREC cannot record memory change of next instruction."),
2908 NULL, NULL,
88d1aa9d
MM
2909 &set_record_full_cmdlist,
2910 &show_record_full_cmdlist);
d02ed0bb
MM
2911
2912 c = add_alias_cmd ("memory-query", "full memory-query", no_class, 1,
2913 &set_record_cmdlist);
2914 deprecate_cmd (c, "set record full memory-query");
2915
2916 c = add_alias_cmd ("memory-query", "full memory-query", no_class, 1,
2917 &show_record_cmdlist);
2918 deprecate_cmd (c, "show record full memory-query");
2919}
This page took 0.660624 seconds and 4 git commands to generate.