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