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