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