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