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