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