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