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