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