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