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