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