gdb/record-full.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Process record and replay target for GDB, the GNU debugger.

  2.    Copyright (C) 2013-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "gdbcmd.h"
  16. #include "regcache.h"
  17. #include "gdbthread.h"
  18. #include "event-top.h"
  19. #include "completer.h"
  20. #include "arch-utils.h"
  21. #include "gdbcore.h"
  22. #include "exec.h"
  23. #include "record.h"
  24. #include "record-full.h"
  25. #include "elf-bfd.h"
  26. #include "gcore.h"
  27. #include "event-loop.h"
  28. #include "inf-loop.h"
  29. #include "gdb_bfd.h"
  30. #include "observer.h"
  31. #include "infrun.h"

  32. #include <signal.h>

  33. /* This module implements "target record-full", also known as "process
  34.    record and replay".  This target sits on top of a "normal" target
  35.    (a target that "has execution"), and provides a record and replay
  36.    functionality, including reverse debugging.

  37.    Target record has two modes: recording, and replaying.

  38.    In record mode, we intercept the to_resume and to_wait methods.
  39.    Whenever gdb resumes the target, we run the target in single step
  40.    mode, and we build up an execution log in which, for each executed
  41.    instruction, we record all changes in memory and register state.
  42.    This is invisible to the user, to whom it just looks like an
  43.    ordinary debugging session (except for performance degredation).

  44.    In replay mode, instead of actually letting the inferior run as a
  45.    process, we simulate its execution by playing back the recorded
  46.    execution log.  For each instruction in the log, we simulate the
  47.    instruction's side effects by duplicating the changes that it would
  48.    have made on memory and registers.  */

  49. #define DEFAULT_RECORD_FULL_INSN_MAX_NUM        200000

  50. #define RECORD_FULL_IS_REPLAY \
  51.      (record_full_list->next || execution_direction == EXEC_REVERSE)

  52. #define RECORD_FULL_FILE_MAGIC        netorder32(0x20091016)

  53. /* These are the core structs of the process record functionality.

  54.    A record_full_entry is a record of the value change of a register
  55.    ("record_full_reg") or a part of memory ("record_full_mem").  And each
  56.    instruction must have a struct record_full_entry ("record_full_end")
  57.    that indicates that this is the last struct record_full_entry of this
  58.    instruction.

  59.    Each struct record_full_entry is linked to "record_full_list" by "prev"
  60.    and "next" pointers.  */

  61. struct record_full_mem_entry
  62. {
  63.   CORE_ADDR addr;
  64.   int len;
  65.   /* Set this flag if target memory for this entry
  66.      can no longer be accessed.  */
  67.   int mem_entry_not_accessible;
  68.   union
  69.   {
  70.     gdb_byte *ptr;
  71.     gdb_byte buf[sizeof (gdb_byte *)];
  72.   } u;
  73. };

  74. struct record_full_reg_entry
  75. {
  76.   unsigned short num;
  77.   unsigned short len;
  78.   union
  79.   {
  80.     gdb_byte *ptr;
  81.     gdb_byte buf[2 * sizeof (gdb_byte *)];
  82.   } u;
  83. };

  84. struct record_full_end_entry
  85. {
  86.   enum gdb_signal sigval;
  87.   ULONGEST insn_num;
  88. };

  89. enum record_full_type
  90. {
  91.   record_full_end = 0,
  92.   record_full_reg,
  93.   record_full_mem
  94. };

  95. /* This is the data structure that makes up the execution log.

  96.    The execution log consists of a single linked list of entries
  97.    of type "struct record_full_entry".  It is doubly linked so that it
  98.    can be traversed in either direction.

  99.    The start of the list is anchored by a struct called
  100.    "record_full_first".  The pointer "record_full_list" either points
  101.    to the last entry that was added to the list (in record mode), or to
  102.    the next entry in the list that will be executed (in replay mode).

  103.    Each list element (struct record_full_entry), in addition to next
  104.    and prev pointers, consists of a union of three entry types: mem,
  105.    reg, and endA field called "type" determines which entry type is
  106.    represented by a given list element.

  107.    Each instruction that is added to the execution log is represented
  108.    by a variable number of list elements ('entries').  The instruction
  109.    will have one "reg" entry for each register that is changed by
  110.    executing the instruction (including the PC in every case).  It
  111.    will also have one "mem" entry for each memory change.  Finally,
  112.    each instruction will have an "end" entry that separates it from
  113.    the changes associated with the next instruction.  */

  114. struct record_full_entry
  115. {
  116.   struct record_full_entry *prev;
  117.   struct record_full_entry *next;
  118.   enum record_full_type type;
  119.   union
  120.   {
  121.     /* reg */
  122.     struct record_full_reg_entry reg;
  123.     /* mem */
  124.     struct record_full_mem_entry mem;
  125.     /* end */
  126.     struct record_full_end_entry end;
  127.   } u;
  128. };

  129. /* If true, query if PREC cannot record memory
  130.    change of next instruction.  */
  131. int record_full_memory_query = 0;

  132. struct record_full_core_buf_entry
  133. {
  134.   struct record_full_core_buf_entry *prev;
  135.   struct target_section *p;
  136.   bfd_byte *buf;
  137. };

  138. /* Record buf with core target.  */
  139. static gdb_byte *record_full_core_regbuf = NULL;
  140. static struct target_section *record_full_core_start;
  141. static struct target_section *record_full_core_end;
  142. static struct record_full_core_buf_entry *record_full_core_buf_list = NULL;

  143. /* The following variables are used for managing the linked list that
  144.    represents the execution log.

  145.    record_full_first is the anchor that holds down the beginning of
  146.    the list.

  147.    record_full_list serves two functions:
  148.      1) In record mode, it anchors the end of the list.
  149.      2) In replay mode, it traverses the list and points to
  150.         the next instruction that must be emulated.

  151.    record_full_arch_list_head and record_full_arch_list_tail are used
  152.    to manage a separate list, which is used to build up the change
  153.    elements of the currently executing instruction during record mode.
  154.    When this instruction has been completely annotated in the "arch
  155.    list", it will be appended to the main execution log.  */

  156. static struct record_full_entry record_full_first;
  157. static struct record_full_entry *record_full_list = &record_full_first;
  158. static struct record_full_entry *record_full_arch_list_head = NULL;
  159. static struct record_full_entry *record_full_arch_list_tail = NULL;

  160. /* 1 ask user. 0 auto delete the last struct record_full_entry.  */
  161. static int record_full_stop_at_limit = 1;
  162. /* Maximum allowed number of insns in execution log.  */
  163. static unsigned int record_full_insn_max_num
  164.         = DEFAULT_RECORD_FULL_INSN_MAX_NUM;
  165. /* Actual count of insns presently in execution log.  */
  166. static unsigned int record_full_insn_num = 0;
  167. /* Count of insns logged so far (may be larger
  168.    than count of insns presently in execution log).  */
  169. static ULONGEST record_full_insn_count;

  170. /* The target_ops of process record.  */
  171. static struct target_ops record_full_ops;
  172. static struct target_ops record_full_core_ops;

  173. /* See record-full.h.  */

  174. int
  175. record_full_is_used (void)
  176. {
  177.   struct target_ops *t;

  178.   t = find_record_target ();
  179.   return (t == &record_full_ops
  180.           || t == &record_full_core_ops);
  181. }


  182. /* Command lists for "set/show record full".  */
  183. static struct cmd_list_element *set_record_full_cmdlist;
  184. static struct cmd_list_element *show_record_full_cmdlist;

  185. /* Command list for "record full".  */
  186. static struct cmd_list_element *record_full_cmdlist;

  187. static void record_full_goto_insn (struct record_full_entry *entry,
  188.                                    enum exec_direction_kind dir);
  189. static void record_full_save (struct target_ops *self,
  190.                               const char *recfilename);

  191. /* Alloc and free functions for record_full_reg, record_full_mem, and
  192.    record_full_end entries.  */

  193. /* Alloc a record_full_reg record entry.  */

  194. static inline struct record_full_entry *
  195. record_full_reg_alloc (struct regcache *regcache, int regnum)
  196. {
  197.   struct record_full_entry *rec;
  198.   struct gdbarch *gdbarch = get_regcache_arch (regcache);

  199.   rec = xcalloc (1, sizeof (struct record_full_entry));
  200.   rec->type = record_full_reg;
  201.   rec->u.reg.num = regnum;
  202.   rec->u.reg.len = register_size (gdbarch, regnum);
  203.   if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
  204.     rec->u.reg.u.ptr = (gdb_byte *) xmalloc (rec->u.reg.len);

  205.   return rec;
  206. }

  207. /* Free a record_full_reg record entry.  */

  208. static inline void
  209. record_full_reg_release (struct record_full_entry *rec)
  210. {
  211.   gdb_assert (rec->type == record_full_reg);
  212.   if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
  213.     xfree (rec->u.reg.u.ptr);
  214.   xfree (rec);
  215. }

  216. /* Alloc a record_full_mem record entry.  */

  217. static inline struct record_full_entry *
  218. record_full_mem_alloc (CORE_ADDR addr, int len)
  219. {
  220.   struct record_full_entry *rec;

  221.   rec = xcalloc (1, sizeof (struct record_full_entry));
  222.   rec->type = record_full_mem;
  223.   rec->u.mem.addr = addr;
  224.   rec->u.mem.len = len;
  225.   if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
  226.     rec->u.mem.u.ptr = (gdb_byte *) xmalloc (len);

  227.   return rec;
  228. }

  229. /* Free a record_full_mem record entry.  */

  230. static inline void
  231. record_full_mem_release (struct record_full_entry *rec)
  232. {
  233.   gdb_assert (rec->type == record_full_mem);
  234.   if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
  235.     xfree (rec->u.mem.u.ptr);
  236.   xfree (rec);
  237. }

  238. /* Alloc a record_full_end record entry.  */

  239. static inline struct record_full_entry *
  240. record_full_end_alloc (void)
  241. {
  242.   struct record_full_entry *rec;

  243.   rec = xcalloc (1, sizeof (struct record_full_entry));
  244.   rec->type = record_full_end;

  245.   return rec;
  246. }

  247. /* Free a record_full_end record entry.  */

  248. static inline void
  249. record_full_end_release (struct record_full_entry *rec)
  250. {
  251.   xfree (rec);
  252. }

  253. /* Free one record entry, any type.
  254.    Return entry->type, in case caller wants to know.  */

  255. static inline enum record_full_type
  256. record_full_entry_release (struct record_full_entry *rec)
  257. {
  258.   enum record_full_type type = rec->type;

  259.   switch (type) {
  260.   case record_full_reg:
  261.     record_full_reg_release (rec);
  262.     break;
  263.   case record_full_mem:
  264.     record_full_mem_release (rec);
  265.     break;
  266.   case record_full_end:
  267.     record_full_end_release (rec);
  268.     break;
  269.   }
  270.   return type;
  271. }

  272. /* Free all record entries in list pointed to by REC.  */

  273. static void
  274. record_full_list_release (struct record_full_entry *rec)
  275. {
  276.   if (!rec)
  277.     return;

  278.   while (rec->next)
  279.     rec = rec->next;

  280.   while (rec->prev)
  281.     {
  282.       rec = rec->prev;
  283.       record_full_entry_release (rec->next);
  284.     }

  285.   if (rec == &record_full_first)
  286.     {
  287.       record_full_insn_num = 0;
  288.       record_full_first.next = NULL;
  289.     }
  290.   else
  291.     record_full_entry_release (rec);
  292. }

  293. /* Free all record entries forward of the given list position.  */

  294. static void
  295. record_full_list_release_following (struct record_full_entry *rec)
  296. {
  297.   struct record_full_entry *tmp = rec->next;

  298.   rec->next = NULL;
  299.   while (tmp)
  300.     {
  301.       rec = tmp->next;
  302.       if (record_full_entry_release (tmp) == record_full_end)
  303.         {
  304.           record_full_insn_num--;
  305.           record_full_insn_count--;
  306.         }
  307.       tmp = rec;
  308.     }
  309. }

  310. /* Delete the first instruction from the beginning of the log, to make
  311.    room for adding a new instruction at the end of the log.

  312.    Note -- this function does not modify record_full_insn_num.  */

  313. static void
  314. record_full_list_release_first (void)
  315. {
  316.   struct record_full_entry *tmp;

  317.   if (!record_full_first.next)
  318.     return;

  319.   /* Loop until a record_full_end.  */
  320.   while (1)
  321.     {
  322.       /* Cut record_full_first.next out of the linked list.  */
  323.       tmp = record_full_first.next;
  324.       record_full_first.next = tmp->next;
  325.       tmp->next->prev = &record_full_first;

  326.       /* tmp is now isolated, and can be deleted.  */
  327.       if (record_full_entry_release (tmp) == record_full_end)
  328.         break;        /* End loop at first record_full_end.  */

  329.       if (!record_full_first.next)
  330.         {
  331.           gdb_assert (record_full_insn_num == 1);
  332.           break;        /* End loop when list is empty.  */
  333.         }
  334.     }
  335. }

  336. /* Add a struct record_full_entry to record_full_arch_list.  */

  337. static void
  338. record_full_arch_list_add (struct record_full_entry *rec)
  339. {
  340.   if (record_debug > 1)
  341.     fprintf_unfiltered (gdb_stdlog,
  342.                         "Process record: record_full_arch_list_add %s.\n",
  343.                         host_address_to_string (rec));

  344.   if (record_full_arch_list_tail)
  345.     {
  346.       record_full_arch_list_tail->next = rec;
  347.       rec->prev = record_full_arch_list_tail;
  348.       record_full_arch_list_tail = rec;
  349.     }
  350.   else
  351.     {
  352.       record_full_arch_list_head = rec;
  353.       record_full_arch_list_tail = rec;
  354.     }
  355. }

  356. /* Return the value storage location of a record entry.  */
  357. static inline gdb_byte *
  358. record_full_get_loc (struct record_full_entry *rec)
  359. {
  360.   switch (rec->type) {
  361.   case record_full_mem:
  362.     if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
  363.       return rec->u.mem.u.ptr;
  364.     else
  365.       return rec->u.mem.u.buf;
  366.   case record_full_reg:
  367.     if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
  368.       return rec->u.reg.u.ptr;
  369.     else
  370.       return rec->u.reg.u.buf;
  371.   case record_full_end:
  372.   default:
  373.     gdb_assert_not_reached ("unexpected record_full_entry type");
  374.     return NULL;
  375.   }
  376. }

  377. /* Record the value of a register NUM to record_full_arch_list.  */

  378. int
  379. record_full_arch_list_add_reg (struct regcache *regcache, int regnum)
  380. {
  381.   struct record_full_entry *rec;

  382.   if (record_debug > 1)
  383.     fprintf_unfiltered (gdb_stdlog,
  384.                         "Process record: add register num = %d to "
  385.                         "record list.\n",
  386.                         regnum);

  387.   rec = record_full_reg_alloc (regcache, regnum);

  388.   regcache_raw_read (regcache, regnum, record_full_get_loc (rec));

  389.   record_full_arch_list_add (rec);

  390.   return 0;
  391. }

  392. /* Record the value of a region of memory whose address is ADDR and
  393.    length is LEN to record_full_arch_list.  */

  394. int
  395. record_full_arch_list_add_mem (CORE_ADDR addr, int len)
  396. {
  397.   struct record_full_entry *rec;

  398.   if (record_debug > 1)
  399.     fprintf_unfiltered (gdb_stdlog,
  400.                         "Process record: add mem addr = %s len = %d to "
  401.                         "record list.\n",
  402.                         paddress (target_gdbarch (), addr), len);

  403.   if (!addr)        /* FIXME: Why?  Some arch must permit it...  */
  404.     return 0;

  405.   rec = record_full_mem_alloc (addr, len);

  406.   if (record_read_memory (target_gdbarch (), addr,
  407.                           record_full_get_loc (rec), len))
  408.     {
  409.       record_full_mem_release (rec);
  410.       return -1;
  411.     }

  412.   record_full_arch_list_add (rec);

  413.   return 0;
  414. }

  415. /* Add a record_full_end type struct record_full_entry to
  416.    record_full_arch_list.  */

  417. int
  418. record_full_arch_list_add_end (void)
  419. {
  420.   struct record_full_entry *rec;

  421.   if (record_debug > 1)
  422.     fprintf_unfiltered (gdb_stdlog,
  423.                         "Process record: add end to arch list.\n");

  424.   rec = record_full_end_alloc ();
  425.   rec->u.end.sigval = GDB_SIGNAL_0;
  426.   rec->u.end.insn_num = ++record_full_insn_count;

  427.   record_full_arch_list_add (rec);

  428.   return 0;
  429. }

  430. static void
  431. record_full_check_insn_num (int set_terminal)
  432. {
  433.   if (record_full_insn_num == record_full_insn_max_num)
  434.     {
  435.       /* Ask user what to do.  */
  436.       if (record_full_stop_at_limit)
  437.         {
  438.           int q;

  439.           if (set_terminal)
  440.             target_terminal_ours ();
  441.           q = yquery (_("Do you want to auto delete previous execution "
  442.                         "log entries when record/replay buffer becomes "
  443.                         "full (record full stop-at-limit)?"));
  444.           if (set_terminal)
  445.             target_terminal_inferior ();
  446.           if (q)
  447.             record_full_stop_at_limit = 0;
  448.           else
  449.             error (_("Process record: stopped by user."));
  450.         }
  451.     }
  452. }

  453. static void
  454. record_full_arch_list_cleanups (void *ignore)
  455. {
  456.   record_full_list_release (record_full_arch_list_tail);
  457. }

  458. /* Before inferior step (when GDB record the running message, inferior
  459.    only can step), GDB will call this function to record the values to
  460.    record_full_list.  This function will call gdbarch_process_record to
  461.    record the running message of inferior and set them to
  462.    record_full_arch_list, and add it to record_full_list.  */

  463. static int
  464. record_full_message (struct regcache *regcache, enum gdb_signal signal)
  465. {
  466.   int ret;
  467.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  468.   struct cleanup *old_cleanups
  469.     = make_cleanup (record_full_arch_list_cleanups, 0);

  470.   record_full_arch_list_head = NULL;
  471.   record_full_arch_list_tail = NULL;

  472.   /* Check record_full_insn_num.  */
  473.   record_full_check_insn_num (1);

  474.   /* If gdb sends a signal value to target_resume,
  475.      save it in the 'end' field of the previous instruction.

  476.      Maybe process record should record what really happened,
  477.      rather than what gdb pretends has happened.

  478.      So if Linux delivered the signal to the child process during
  479.      the record mode, we will record it and deliver it again in
  480.      the replay mode.

  481.      If user says "ignore this signal" during the record mode, then
  482.      it will be ignored again during the replay mode (no matter if
  483.      the user says something different, like "deliver this signal"
  484.      during the replay mode).

  485.      User should understand that nothing he does during the replay
  486.      mode will change the behavior of the child.  If he tries,
  487.      then that is a user error.

  488.      But we should still deliver the signal to gdb during the replay,
  489.      if we delivered it during the recording.  Therefore we should
  490.      record the signal during record_full_wait, not
  491.      record_full_resume.  */
  492.   if (record_full_list != &record_full_first/* FIXME better way to check */
  493.     {
  494.       gdb_assert (record_full_list->type == record_full_end);
  495.       record_full_list->u.end.sigval = signal;
  496.     }

  497.   if (signal == GDB_SIGNAL_0
  498.       || !gdbarch_process_record_signal_p (gdbarch))
  499.     ret = gdbarch_process_record (gdbarch,
  500.                                   regcache,
  501.                                   regcache_read_pc (regcache));
  502.   else
  503.     ret = gdbarch_process_record_signal (gdbarch,
  504.                                          regcache,
  505.                                          signal);

  506.   if (ret > 0)
  507.     error (_("Process record: inferior program stopped."));
  508.   if (ret < 0)
  509.     error (_("Process record: failed to record execution log."));

  510.   discard_cleanups (old_cleanups);

  511.   record_full_list->next = record_full_arch_list_head;
  512.   record_full_arch_list_head->prev = record_full_list;
  513.   record_full_list = record_full_arch_list_tail;

  514.   if (record_full_insn_num == record_full_insn_max_num)
  515.     record_full_list_release_first ();
  516.   else
  517.     record_full_insn_num++;

  518.   return 1;
  519. }

  520. struct record_full_message_args {
  521.   struct regcache *regcache;
  522.   enum gdb_signal signal;
  523. };

  524. static int
  525. record_full_message_wrapper (void *args)
  526. {
  527.   struct record_full_message_args *record_full_args = args;

  528.   return record_full_message (record_full_args->regcache,
  529.                               record_full_args->signal);
  530. }

  531. static int
  532. record_full_message_wrapper_safe (struct regcache *regcache,
  533.                                   enum gdb_signal signal)
  534. {
  535.   struct record_full_message_args args;

  536.   args.regcache = regcache;
  537.   args.signal = signal;

  538.   return catch_errors (record_full_message_wrapper, &args, NULL,
  539.                        RETURN_MASK_ALL);
  540. }

  541. /* Set to 1 if record_full_store_registers and record_full_xfer_partial
  542.    doesn't need record.  */

  543. static int record_full_gdb_operation_disable = 0;

  544. struct cleanup *
  545. record_full_gdb_operation_disable_set (void)
  546. {
  547.   struct cleanup *old_cleanups = NULL;

  548.   old_cleanups =
  549.     make_cleanup_restore_integer (&record_full_gdb_operation_disable);
  550.   record_full_gdb_operation_disable = 1;

  551.   return old_cleanups;
  552. }

  553. /* Flag set to TRUE for target_stopped_by_watchpoint.  */
  554. static int record_full_hw_watchpoint = 0;

  555. /* Execute one instruction from the record log.  Each instruction in
  556.    the log will be represented by an arbitrary sequence of register
  557.    entries and memory entries, followed by an 'end' entry.  */

  558. static inline void
  559. record_full_exec_insn (struct regcache *regcache,
  560.                        struct gdbarch *gdbarch,
  561.                        struct record_full_entry *entry)
  562. {
  563.   switch (entry->type)
  564.     {
  565.     case record_full_reg: /* reg */
  566.       {
  567.         gdb_byte reg[MAX_REGISTER_SIZE];

  568.         if (record_debug > 1)
  569.           fprintf_unfiltered (gdb_stdlog,
  570.                               "Process record: record_full_reg %s to "
  571.                               "inferior num = %d.\n",
  572.                               host_address_to_string (entry),
  573.                               entry->u.reg.num);

  574.         regcache_cooked_read (regcache, entry->u.reg.num, reg);
  575.         regcache_cooked_write (regcache, entry->u.reg.num,
  576.                                record_full_get_loc (entry));
  577.         memcpy (record_full_get_loc (entry), reg, entry->u.reg.len);
  578.       }
  579.       break;

  580.     case record_full_mem: /* mem */
  581.       {
  582.         /* Nothing to do if the entry is flagged not_accessible.  */
  583.         if (!entry->u.mem.mem_entry_not_accessible)
  584.           {
  585.             gdb_byte *mem = alloca (entry->u.mem.len);

  586.             if (record_debug > 1)
  587.               fprintf_unfiltered (gdb_stdlog,
  588.                                   "Process record: record_full_mem %s to "
  589.                                   "inferior addr = %s len = %d.\n",
  590.                                   host_address_to_string (entry),
  591.                                   paddress (gdbarch, entry->u.mem.addr),
  592.                                   entry->u.mem.len);

  593.             if (record_read_memory (gdbarch,
  594.                                     entry->u.mem.addr, mem, entry->u.mem.len))
  595.               entry->u.mem.mem_entry_not_accessible = 1;
  596.             else
  597.               {
  598.                 if (target_write_memory (entry->u.mem.addr,
  599.                                          record_full_get_loc (entry),
  600.                                          entry->u.mem.len))
  601.                   {
  602.                     entry->u.mem.mem_entry_not_accessible = 1;
  603.                     if (record_debug)
  604.                       warning (_("Process record: error writing memory at "
  605.                                  "addr = %s len = %d."),
  606.                                paddress (gdbarch, entry->u.mem.addr),
  607.                                entry->u.mem.len);
  608.                   }
  609.                 else
  610.                   {
  611.                     memcpy (record_full_get_loc (entry), mem,
  612.                             entry->u.mem.len);

  613.                     /* We've changed memory --- check if a hardware
  614.                        watchpoint should trap.  Note that this
  615.                        presently assumes the target beneath supports
  616.                        continuable watchpoints.  On non-continuable
  617.                        watchpoints target, we'll want to check this
  618.                        _before_ actually doing the memory change, and
  619.                        not doing the change at all if the watchpoint
  620.                        traps.  */
  621.                     if (hardware_watchpoint_inserted_in_range
  622.                         (get_regcache_aspace (regcache),
  623.                          entry->u.mem.addr, entry->u.mem.len))
  624.                       record_full_hw_watchpoint = 1;
  625.                   }
  626.               }
  627.           }
  628.       }
  629.       break;
  630.     }
  631. }

  632. static void record_full_restore (void);

  633. /* Asynchronous signal handle registered as event loop source for when
  634.    we have pending events ready to be passed to the core.  */

  635. static struct async_event_handler *record_full_async_inferior_event_token;

  636. static void
  637. record_full_async_inferior_event_handler (gdb_client_data data)
  638. {
  639.   inferior_event_handler (INF_REG_EVENT, NULL);
  640. }

  641. /* Open the process record target.  */

  642. static void
  643. record_full_core_open_1 (const char *name, int from_tty)
  644. {
  645.   struct regcache *regcache = get_current_regcache ();
  646.   int regnum = gdbarch_num_regs (get_regcache_arch (regcache));
  647.   int i;

  648.   /* Get record_full_core_regbuf.  */
  649.   target_fetch_registers (regcache, -1);
  650.   record_full_core_regbuf = xmalloc (MAX_REGISTER_SIZE * regnum);
  651.   for (i = 0; i < regnum; i ++)
  652.     regcache_raw_collect (regcache, i,
  653.                           record_full_core_regbuf + MAX_REGISTER_SIZE * i);

  654.   /* Get record_full_core_start and record_full_core_end.  */
  655.   if (build_section_table (core_bfd, &record_full_core_start,
  656.                            &record_full_core_end))
  657.     {
  658.       xfree (record_full_core_regbuf);
  659.       record_full_core_regbuf = NULL;
  660.       error (_("\"%s\": Can't find sections: %s"),
  661.              bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ()));
  662.     }

  663.   push_target (&record_full_core_ops);
  664.   record_full_restore ();
  665. }

  666. /* "to_open" target method for 'live' processes.  */

  667. static void
  668. record_full_open_1 (const char *name, int from_tty)
  669. {
  670.   if (record_debug)
  671.     fprintf_unfiltered (gdb_stdlog, "Process record: record_full_open\n");

  672.   /* check exec */
  673.   if (!target_has_execution)
  674.     error (_("Process record: the program is not being run."));
  675.   if (non_stop)
  676.     error (_("Process record target can't debug inferior in non-stop mode "
  677.              "(non-stop)."));

  678.   if (!gdbarch_process_record_p (target_gdbarch ()))
  679.     error (_("Process record: the current architecture doesn't support "
  680.              "record function."));

  681.   push_target (&record_full_ops);
  682. }

  683. static void record_full_init_record_breakpoints (void);

  684. /* "to_open" target method.  Open the process record target.  */

  685. static void
  686. record_full_open (const char *name, int from_tty)
  687. {
  688.   struct target_ops *t;

  689.   if (record_debug)
  690.     fprintf_unfiltered (gdb_stdlog, "Process record: record_full_open\n");

  691.   record_preopen ();

  692.   /* Reset */
  693.   record_full_insn_num = 0;
  694.   record_full_insn_count = 0;
  695.   record_full_list = &record_full_first;
  696.   record_full_list->next = NULL;

  697.   if (core_bfd)
  698.     record_full_core_open_1 (name, from_tty);
  699.   else
  700.     record_full_open_1 (name, from_tty);

  701.   /* Register extra event sources in the event loop.  */
  702.   record_full_async_inferior_event_token
  703.     = create_async_event_handler (record_full_async_inferior_event_handler,
  704.                                   NULL);

  705.   record_full_init_record_breakpoints ();

  706.   observer_notify_record_changed (current_inferior (),  1);
  707. }

  708. /* "to_close" target method.  Close the process record target.  */

  709. static void
  710. record_full_close (struct target_ops *self)
  711. {
  712.   struct record_full_core_buf_entry *entry;

  713.   if (record_debug)
  714.     fprintf_unfiltered (gdb_stdlog, "Process record: record_full_close\n");

  715.   record_full_list_release (record_full_list);

  716.   /* Release record_full_core_regbuf.  */
  717.   if (record_full_core_regbuf)
  718.     {
  719.       xfree (record_full_core_regbuf);
  720.       record_full_core_regbuf = NULL;
  721.     }

  722.   /* Release record_full_core_buf_list.  */
  723.   if (record_full_core_buf_list)
  724.     {
  725.       for (entry = record_full_core_buf_list->prev; entry;
  726.            entry = entry->prev)
  727.         {
  728.           xfree (record_full_core_buf_list);
  729.           record_full_core_buf_list = entry;
  730.         }
  731.       record_full_core_buf_list = NULL;
  732.     }

  733.   if (record_full_async_inferior_event_token)
  734.     delete_async_event_handler (&record_full_async_inferior_event_token);
  735. }

  736. static int record_full_resume_step = 0;

  737. /* True if we've been resumed, and so each record_full_wait call should
  738.    advance execution.  If this is false, record_full_wait will return a
  739.    TARGET_WAITKIND_IGNORE.  */
  740. static int record_full_resumed = 0;

  741. /* The execution direction of the last resume we got.  This is
  742.    necessary for async mode.  Vis (order is not strictly accurate):

  743.    1. user has the global execution direction set to forward
  744.    2. user does a reverse-step command
  745.    3. record_full_resume is called with global execution direction
  746.       temporarily switched to reverse
  747.    4. GDB's execution direction is reverted back to forward
  748.    5. target record notifies event loop there's an event to handle
  749.    6. infrun asks the target which direction was it going, and switches
  750.       the global execution direction accordingly (to reverse)
  751.    7. infrun polls an event out of the record target, and handles it
  752.    8. GDB goes back to the event loop, and goto #4.
  753. */
  754. static enum exec_direction_kind record_full_execution_dir = EXEC_FORWARD;

  755. /* "to_resume" target method.  Resume the process record target.  */

  756. static void
  757. record_full_resume (struct target_ops *ops, ptid_t ptid, int step,
  758.                     enum gdb_signal signal)
  759. {
  760.   record_full_resume_step = step;
  761.   record_full_resumed = 1;
  762.   record_full_execution_dir = execution_direction;

  763.   if (!RECORD_FULL_IS_REPLAY)
  764.     {
  765.       struct gdbarch *gdbarch = target_thread_architecture (ptid);

  766.       record_full_message (get_current_regcache (), signal);

  767.       if (!step)
  768.         {
  769.           /* This is not hard single step.  */
  770.           if (!gdbarch_software_single_step_p (gdbarch))
  771.             {
  772.               /* This is a normal continue.  */
  773.               step = 1;
  774.             }
  775.           else
  776.             {
  777.               /* This arch support soft sigle step.  */
  778.               if (thread_has_single_step_breakpoints_set (inferior_thread ()))
  779.                 {
  780.                   /* This is a soft single step.  */
  781.                   record_full_resume_step = 1;
  782.                 }
  783.               else
  784.                 {
  785.                   /* This is a continue.
  786.                      Try to insert a soft single step breakpoint.  */
  787.                   if (!gdbarch_software_single_step (gdbarch,
  788.                                                      get_current_frame ()))
  789.                     {
  790.                       /* This system don't want use soft single step.
  791.                          Use hard sigle step.  */
  792.                       step = 1;
  793.                     }
  794.                 }
  795.             }
  796.         }

  797.       /* Make sure the target beneath reports all signals.  */
  798.       target_pass_signals (0, NULL);

  799.       ops->beneath->to_resume (ops->beneath, ptid, step, signal);
  800.     }

  801.   /* We are about to start executing the inferior (or simulate it),
  802.      let's register it with the event loop.  */
  803.   if (target_can_async_p ())
  804.     {
  805.       target_async (inferior_event_handler, 0);
  806.       /* Notify the event loop there's an event to wait for.  We do
  807.          most of the work in record_full_wait.  */
  808.       mark_async_event_handler (record_full_async_inferior_event_token);
  809.     }
  810. }

  811. static int record_full_get_sig = 0;

  812. /* SIGINT signal handler, registered by "to_wait" method.  */

  813. static void
  814. record_full_sig_handler (int signo)
  815. {
  816.   if (record_debug)
  817.     fprintf_unfiltered (gdb_stdlog, "Process record: get a signal\n");

  818.   /* It will break the running inferior in replay mode.  */
  819.   record_full_resume_step = 1;

  820.   /* It will let record_full_wait set inferior status to get the signal
  821.      SIGINT.  */
  822.   record_full_get_sig = 1;
  823. }

  824. static void
  825. record_full_wait_cleanups (void *ignore)
  826. {
  827.   if (execution_direction == EXEC_REVERSE)
  828.     {
  829.       if (record_full_list->next)
  830.         record_full_list = record_full_list->next;
  831.     }
  832.   else
  833.     record_full_list = record_full_list->prev;
  834. }

  835. /* "to_wait" target method for process record target.

  836.    In record mode, the target is always run in singlestep mode
  837.    (even when gdb says to continue).  The to_wait method intercepts
  838.    the stop events and determines which ones are to be passed on to
  839.    gdb.  Most stop events are just singlestep events that gdb is not
  840.    to know about, so the to_wait method just records them and keeps
  841.    singlestepping.

  842.    In replay mode, this function emulates the recorded execution log,
  843.    one instruction at a time (forward or backward), and determines
  844.    where to stop.  */

  845. static ptid_t
  846. record_full_wait_1 (struct target_ops *ops,
  847.                     ptid_t ptid, struct target_waitstatus *status,
  848.                     int options)
  849. {
  850.   struct cleanup *set_cleanups = record_full_gdb_operation_disable_set ();

  851.   if (record_debug)
  852.     fprintf_unfiltered (gdb_stdlog,
  853.                         "Process record: record_full_wait "
  854.                         "record_full_resume_step = %d, "
  855.                         "record_full_resumed = %d, direction=%s\n",
  856.                         record_full_resume_step, record_full_resumed,
  857.                         record_full_execution_dir == EXEC_FORWARD
  858.                         ? "forward" : "reverse");

  859.   if (!record_full_resumed)
  860.     {
  861.       gdb_assert ((options & TARGET_WNOHANG) != 0);

  862.       /* No interesting event.  */
  863.       status->kind = TARGET_WAITKIND_IGNORE;
  864.       return minus_one_ptid;
  865.     }

  866.   record_full_get_sig = 0;
  867.   signal (SIGINT, record_full_sig_handler);

  868.   if (!RECORD_FULL_IS_REPLAY && ops != &record_full_core_ops)
  869.     {
  870.       if (record_full_resume_step)
  871.         {
  872.           /* This is a single step.  */
  873.           return ops->beneath->to_wait (ops->beneath, ptid, status, options);
  874.         }
  875.       else
  876.         {
  877.           /* This is not a single step.  */
  878.           ptid_t ret;
  879.           CORE_ADDR tmp_pc;
  880.           struct gdbarch *gdbarch = target_thread_architecture (inferior_ptid);

  881.           while (1)
  882.             {
  883.               struct thread_info *tp;

  884.               ret = ops->beneath->to_wait (ops->beneath, ptid, status, options);
  885.               if (status->kind == TARGET_WAITKIND_IGNORE)
  886.                 {
  887.                   if (record_debug)
  888.                     fprintf_unfiltered (gdb_stdlog,
  889.                                         "Process record: record_full_wait "
  890.                                         "target beneath not done yet\n");
  891.                   return ret;
  892.                 }

  893.               ALL_NON_EXITED_THREADS (tp)
  894.                 delete_single_step_breakpoints (tp);

  895.               if (record_full_resume_step)
  896.                 return ret;

  897.               /* Is this a SIGTRAP?  */
  898.               if (status->kind == TARGET_WAITKIND_STOPPED
  899.                   && status->value.sig == GDB_SIGNAL_TRAP)
  900.                 {
  901.                   struct regcache *regcache;
  902.                   struct address_space *aspace;

  903.                   /* Yes -- this is likely our single-step finishing,
  904.                      but check if there's any reason the core would be
  905.                      interested in the event.  */

  906.                   registers_changed ();
  907.                   regcache = get_current_regcache ();
  908.                   tmp_pc = regcache_read_pc (regcache);
  909.                   aspace = get_regcache_aspace (regcache);

  910.                   if (target_stopped_by_watchpoint ())
  911.                     {
  912.                       /* Always interested in watchpoints.  */
  913.                     }
  914.                   else if (breakpoint_inserted_here_p (aspace, tmp_pc))
  915.                     {
  916.                       /* There is a breakpoint here.  Let the core
  917.                          handle it.  */
  918.                       if (software_breakpoint_inserted_here_p (aspace, tmp_pc))
  919.                         {
  920.                           struct gdbarch *gdbarch
  921.                             = get_regcache_arch (regcache);
  922.                           CORE_ADDR decr_pc_after_break
  923.                             = target_decr_pc_after_break (gdbarch);
  924.                           if (decr_pc_after_break)
  925.                             regcache_write_pc (regcache,
  926.                                                tmp_pc + decr_pc_after_break);
  927.                         }
  928.                     }
  929.                   else
  930.                     {
  931.                       /* This is a single-step trap.  Record the
  932.                          insn and issue another step.
  933.                          FIXME: this part can be a random SIGTRAP too.
  934.                          But GDB cannot handle it.  */
  935.                       int step = 1;

  936.                       if (!record_full_message_wrapper_safe (regcache,
  937.                                                              GDB_SIGNAL_0))
  938.                           {
  939.                            status->kind = TARGET_WAITKIND_STOPPED;
  940.                            status->value.sig = GDB_SIGNAL_0;
  941.                            break;
  942.                           }

  943.                       if (gdbarch_software_single_step_p (gdbarch))
  944.                         {
  945.                           /* Try to insert the software single step breakpoint.
  946.                              If insert success, set step to 0.  */
  947.                           set_executing (inferior_ptid, 0);
  948.                           reinit_frame_cache ();
  949.                           if (gdbarch_software_single_step (gdbarch,
  950.                                                             get_current_frame ()))
  951.                             step = 0;
  952.                           set_executing (inferior_ptid, 1);
  953.                         }

  954.                       if (record_debug)
  955.                         fprintf_unfiltered (gdb_stdlog,
  956.                                             "Process record: record_full_wait "
  957.                                             "issuing one more step in the "
  958.                                             "target beneath\n");
  959.                       ops->beneath->to_resume (ops->beneath, ptid, step,
  960.                                                GDB_SIGNAL_0);
  961.                       continue;
  962.                     }
  963.                 }

  964.               /* The inferior is broken by a breakpoint or a signal.  */
  965.               break;
  966.             }

  967.           return ret;
  968.         }
  969.     }
  970.   else
  971.     {
  972.       struct regcache *regcache = get_current_regcache ();
  973.       struct gdbarch *gdbarch = get_regcache_arch (regcache);
  974.       struct address_space *aspace = get_regcache_aspace (regcache);
  975.       int continue_flag = 1;
  976.       int first_record_full_end = 1;
  977.       struct cleanup *old_cleanups
  978.         = make_cleanup (record_full_wait_cleanups, 0);
  979.       CORE_ADDR tmp_pc;

  980.       record_full_hw_watchpoint = 0;
  981.       status->kind = TARGET_WAITKIND_STOPPED;

  982.       /* Check breakpoint when forward execute.  */
  983.       if (execution_direction == EXEC_FORWARD)
  984.         {
  985.           tmp_pc = regcache_read_pc (regcache);
  986.           if (breakpoint_inserted_here_p (aspace, tmp_pc))
  987.             {
  988.               int decr_pc_after_break = target_decr_pc_after_break (gdbarch);

  989.               if (record_debug)
  990.                 fprintf_unfiltered (gdb_stdlog,
  991.                                     "Process record: break at %s.\n",
  992.                                     paddress (gdbarch, tmp_pc));

  993.               if (decr_pc_after_break
  994.                   && !record_full_resume_step
  995.                   && software_breakpoint_inserted_here_p (aspace, tmp_pc))
  996.                 regcache_write_pc (regcache,
  997.                                    tmp_pc + decr_pc_after_break);
  998.               goto replay_out;
  999.             }
  1000.         }

  1001.       /* If GDB is in terminal_inferior mode, it will not get the signal.
  1002.          And in GDB replay mode, GDB doesn't need to be in terminal_inferior
  1003.          mode, because inferior will not executed.
  1004.          Then set it to terminal_ours to make GDB get the signal.  */
  1005.       target_terminal_ours ();

  1006.       /* In EXEC_FORWARD mode, record_full_list points to the tail of prev
  1007.          instruction.  */
  1008.       if (execution_direction == EXEC_FORWARD && record_full_list->next)
  1009.         record_full_list = record_full_list->next;

  1010.       /* Loop over the record_full_list, looking for the next place to
  1011.          stop.  */
  1012.       do
  1013.         {
  1014.           /* Check for beginning and end of log.  */
  1015.           if (execution_direction == EXEC_REVERSE
  1016.               && record_full_list == &record_full_first)
  1017.             {
  1018.               /* Hit beginning of record log in reverse.  */
  1019.               status->kind = TARGET_WAITKIND_NO_HISTORY;
  1020.               break;
  1021.             }
  1022.           if (execution_direction != EXEC_REVERSE && !record_full_list->next)
  1023.             {
  1024.               /* Hit end of record log going forward.  */
  1025.               status->kind = TARGET_WAITKIND_NO_HISTORY;
  1026.               break;
  1027.             }

  1028.           record_full_exec_insn (regcache, gdbarch, record_full_list);

  1029.           if (record_full_list->type == record_full_end)
  1030.             {
  1031.               if (record_debug > 1)
  1032.                 fprintf_unfiltered (gdb_stdlog,
  1033.                                     "Process record: record_full_end %s to "
  1034.                                     "inferior.\n",
  1035.                                     host_address_to_string (record_full_list));

  1036.               if (first_record_full_end && execution_direction == EXEC_REVERSE)
  1037.                 {
  1038.                   /* When reverse excute, the first record_full_end is the
  1039.                      part of current instruction.  */
  1040.                   first_record_full_end = 0;
  1041.                 }
  1042.               else
  1043.                 {
  1044.                   /* In EXEC_REVERSE mode, this is the record_full_end of prev
  1045.                      instruction.
  1046.                      In EXEC_FORWARD mode, this is the record_full_end of
  1047.                      current instruction.  */
  1048.                   /* step */
  1049.                   if (record_full_resume_step)
  1050.                     {
  1051.                       if (record_debug > 1)
  1052.                         fprintf_unfiltered (gdb_stdlog,
  1053.                                             "Process record: step.\n");
  1054.                       continue_flag = 0;
  1055.                     }

  1056.                   /* check breakpoint */
  1057.                   tmp_pc = regcache_read_pc (regcache);
  1058.                   if (breakpoint_inserted_here_p (aspace, tmp_pc))
  1059.                     {
  1060.                       int decr_pc_after_break
  1061.                         = target_decr_pc_after_break (gdbarch);

  1062.                       if (record_debug)
  1063.                         fprintf_unfiltered (gdb_stdlog,
  1064.                                             "Process record: break "
  1065.                                             "at %s.\n",
  1066.                                             paddress (gdbarch, tmp_pc));
  1067.                       if (decr_pc_after_break
  1068.                           && execution_direction == EXEC_FORWARD
  1069.                           && !record_full_resume_step
  1070.                           && software_breakpoint_inserted_here_p (aspace,
  1071.                                                                   tmp_pc))
  1072.                         regcache_write_pc (regcache,
  1073.                                            tmp_pc + decr_pc_after_break);
  1074.                       continue_flag = 0;
  1075.                     }

  1076.                   if (record_full_hw_watchpoint)
  1077.                     {
  1078.                       if (record_debug)
  1079.                         fprintf_unfiltered (gdb_stdlog,
  1080.                                             "Process record: hit hw "
  1081.                                             "watchpoint.\n");
  1082.                       continue_flag = 0;
  1083.                     }
  1084.                   /* Check target signal */
  1085.                   if (record_full_list->u.end.sigval != GDB_SIGNAL_0)
  1086.                     /* FIXME: better way to check */
  1087.                     continue_flag = 0;
  1088.                 }
  1089.             }

  1090.           if (continue_flag)
  1091.             {
  1092.               if (execution_direction == EXEC_REVERSE)
  1093.                 {
  1094.                   if (record_full_list->prev)
  1095.                     record_full_list = record_full_list->prev;
  1096.                 }
  1097.               else
  1098.                 {
  1099.                   if (record_full_list->next)
  1100.                     record_full_list = record_full_list->next;
  1101.                 }
  1102.             }
  1103.         }
  1104.       while (continue_flag);

  1105. replay_out:
  1106.       if (record_full_get_sig)
  1107.         status->value.sig = GDB_SIGNAL_INT;
  1108.       else if (record_full_list->u.end.sigval != GDB_SIGNAL_0)
  1109.         /* FIXME: better way to check */
  1110.         status->value.sig = record_full_list->u.end.sigval;
  1111.       else
  1112.         status->value.sig = GDB_SIGNAL_TRAP;

  1113.       discard_cleanups (old_cleanups);
  1114.     }

  1115.   signal (SIGINT, handle_sigint);

  1116.   do_cleanups (set_cleanups);
  1117.   return inferior_ptid;
  1118. }

  1119. static ptid_t
  1120. record_full_wait (struct target_ops *ops,
  1121.                   ptid_t ptid, struct target_waitstatus *status,
  1122.                   int options)
  1123. {
  1124.   ptid_t return_ptid;

  1125.   return_ptid = record_full_wait_1 (ops, ptid, status, options);
  1126.   if (status->kind != TARGET_WAITKIND_IGNORE)
  1127.     {
  1128.       /* We're reporting a stop.  Make sure any spurious
  1129.          target_wait(WNOHANG) doesn't advance the target until the
  1130.          core wants us resumed again.  */
  1131.       record_full_resumed = 0;
  1132.     }
  1133.   return return_ptid;
  1134. }

  1135. static int
  1136. record_full_stopped_by_watchpoint (struct target_ops *ops)
  1137. {
  1138.   if (RECORD_FULL_IS_REPLAY)
  1139.     return record_full_hw_watchpoint;
  1140.   else
  1141.     return ops->beneath->to_stopped_by_watchpoint (ops->beneath);
  1142. }

  1143. static int
  1144. record_full_stopped_data_address (struct target_ops *ops, CORE_ADDR *addr_p)
  1145. {
  1146.   if (RECORD_FULL_IS_REPLAY)
  1147.     return 0;
  1148.   else
  1149.     return ops->beneath->to_stopped_data_address (ops->beneath, addr_p);
  1150. }

  1151. /* Record registers change (by user or by GDB) to list as an instruction.  */

  1152. static void
  1153. record_full_registers_change (struct regcache *regcache, int regnum)
  1154. {
  1155.   /* Check record_full_insn_num.  */
  1156.   record_full_check_insn_num (0);

  1157.   record_full_arch_list_head = NULL;
  1158.   record_full_arch_list_tail = NULL;

  1159.   if (regnum < 0)
  1160.     {
  1161.       int i;

  1162.       for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
  1163.         {
  1164.           if (record_full_arch_list_add_reg (regcache, i))
  1165.             {
  1166.               record_full_list_release (record_full_arch_list_tail);
  1167.               error (_("Process record: failed to record execution log."));
  1168.             }
  1169.         }
  1170.     }
  1171.   else
  1172.     {
  1173.       if (record_full_arch_list_add_reg (regcache, regnum))
  1174.         {
  1175.           record_full_list_release (record_full_arch_list_tail);
  1176.           error (_("Process record: failed to record execution log."));
  1177.         }
  1178.     }
  1179.   if (record_full_arch_list_add_end ())
  1180.     {
  1181.       record_full_list_release (record_full_arch_list_tail);
  1182.       error (_("Process record: failed to record execution log."));
  1183.     }
  1184.   record_full_list->next = record_full_arch_list_head;
  1185.   record_full_arch_list_head->prev = record_full_list;
  1186.   record_full_list = record_full_arch_list_tail;

  1187.   if (record_full_insn_num == record_full_insn_max_num)
  1188.     record_full_list_release_first ();
  1189.   else
  1190.     record_full_insn_num++;
  1191. }

  1192. /* "to_store_registers" method for process record target.  */

  1193. static void
  1194. record_full_store_registers (struct target_ops *ops,
  1195.                              struct regcache *regcache,
  1196.                              int regno)
  1197. {
  1198.   if (!record_full_gdb_operation_disable)
  1199.     {
  1200.       if (RECORD_FULL_IS_REPLAY)
  1201.         {
  1202.           int n;

  1203.           /* Let user choose if he wants to write register or not.  */
  1204.           if (regno < 0)
  1205.             n =
  1206.               query (_("Because GDB is in replay mode, changing the "
  1207.                        "value of a register will make the execution "
  1208.                        "log unusable from this point onward.  "
  1209.                        "Change all registers?"));
  1210.           else
  1211.             n =
  1212.               query (_("Because GDB is in replay mode, changing the value "
  1213.                        "of a register will make the execution log unusable "
  1214.                        "from this point onward.  Change register %s?"),
  1215.                       gdbarch_register_name (get_regcache_arch (regcache),
  1216.                                                regno));

  1217.           if (!n)
  1218.             {
  1219.               /* Invalidate the value of regcache that was set in function
  1220.                  "regcache_raw_write".  */
  1221.               if (regno < 0)
  1222.                 {
  1223.                   int i;

  1224.                   for (i = 0;
  1225.                        i < gdbarch_num_regs (get_regcache_arch (regcache));
  1226.                        i++)
  1227.                     regcache_invalidate (regcache, i);
  1228.                 }
  1229.               else
  1230.                 regcache_invalidate (regcache, regno);

  1231.               error (_("Process record canceled the operation."));
  1232.             }

  1233.           /* Destroy the record from here forward.  */
  1234.           record_full_list_release_following (record_full_list);
  1235.         }

  1236.       record_full_registers_change (regcache, regno);
  1237.     }
  1238.   ops->beneath->to_store_registers (ops->beneath, regcache, regno);
  1239. }

  1240. /* "to_xfer_partial" method.  Behavior is conditional on
  1241.    RECORD_FULL_IS_REPLAY.
  1242.    In replay mode, we cannot write memory unles we are willing to
  1243.    invalidate the record/replay log from this point forward.  */

  1244. static enum target_xfer_status
  1245. record_full_xfer_partial (struct target_ops *ops, enum target_object object,
  1246.                           const char *annex, gdb_byte *readbuf,
  1247.                           const gdb_byte *writebuf, ULONGEST offset,
  1248.                           ULONGEST len, ULONGEST *xfered_len)
  1249. {
  1250.   if (!record_full_gdb_operation_disable
  1251.       && (object == TARGET_OBJECT_MEMORY
  1252.           || object == TARGET_OBJECT_RAW_MEMORY) && writebuf)
  1253.     {
  1254.       if (RECORD_FULL_IS_REPLAY)
  1255.         {
  1256.           /* Let user choose if he wants to write memory or not.  */
  1257.           if (!query (_("Because GDB is in replay mode, writing to memory "
  1258.                         "will make the execution log unusable from this "
  1259.                         "point onward.  Write memory at address %s?"),
  1260.                        paddress (target_gdbarch (), offset)))
  1261.             error (_("Process record canceled the operation."));

  1262.           /* Destroy the record from here forward.  */
  1263.           record_full_list_release_following (record_full_list);
  1264.         }

  1265.       /* Check record_full_insn_num */
  1266.       record_full_check_insn_num (0);

  1267.       /* Record registers change to list as an instruction.  */
  1268.       record_full_arch_list_head = NULL;
  1269.       record_full_arch_list_tail = NULL;
  1270.       if (record_full_arch_list_add_mem (offset, len))
  1271.         {
  1272.           record_full_list_release (record_full_arch_list_tail);
  1273.           if (record_debug)
  1274.             fprintf_unfiltered (gdb_stdlog,
  1275.                                 "Process record: failed to record "
  1276.                                 "execution log.");
  1277.           return TARGET_XFER_E_IO;
  1278.         }
  1279.       if (record_full_arch_list_add_end ())
  1280.         {
  1281.           record_full_list_release (record_full_arch_list_tail);
  1282.           if (record_debug)
  1283.             fprintf_unfiltered (gdb_stdlog,
  1284.                                 "Process record: failed to record "
  1285.                                 "execution log.");
  1286.           return TARGET_XFER_E_IO;
  1287.         }
  1288.       record_full_list->next = record_full_arch_list_head;
  1289.       record_full_arch_list_head->prev = record_full_list;
  1290.       record_full_list = record_full_arch_list_tail;

  1291.       if (record_full_insn_num == record_full_insn_max_num)
  1292.         record_full_list_release_first ();
  1293.       else
  1294.         record_full_insn_num++;
  1295.     }

  1296.   return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
  1297.                                         readbuf, writebuf, offset,
  1298.                                         len, xfered_len);
  1299. }

  1300. /* This structure represents a breakpoint inserted while the record
  1301.    target is active.  We use this to know when to install/remove
  1302.    breakpoints in/from the target beneath.  For example, a breakpoint
  1303.    may be inserted while recording, but removed when not replaying nor
  1304.    recording.  In that case, the breakpoint had not been inserted on
  1305.    the target beneath, so we should not try to remove it there.  */

  1306. struct record_full_breakpoint
  1307. {
  1308.   /* The address and address space the breakpoint was set at.  */
  1309.   struct address_space *address_space;
  1310.   CORE_ADDR addr;

  1311.   /* True when the breakpoint has been also installed in the target
  1312.      beneath.  This will be false for breakpoints set during replay or
  1313.      when recording.  */
  1314.   int in_target_beneath;
  1315. };

  1316. typedef struct record_full_breakpoint *record_full_breakpoint_p;
  1317. DEF_VEC_P(record_full_breakpoint_p);

  1318. /* The list of breakpoints inserted while the record target is
  1319.    active.  */
  1320. VEC(record_full_breakpoint_p) *record_full_breakpoints = NULL;

  1321. static void
  1322. record_full_sync_record_breakpoints (struct bp_location *loc, void *data)
  1323. {
  1324.   if (loc->loc_type != bp_loc_software_breakpoint)
  1325.       return;

  1326.   if (loc->inserted)
  1327.     {
  1328.       struct record_full_breakpoint *bp = XNEW (struct record_full_breakpoint);

  1329.       bp->addr = loc->target_info.placed_address;
  1330.       bp->address_space = loc->target_info.placed_address_space;

  1331.       bp->in_target_beneath = 1;

  1332.       VEC_safe_push (record_full_breakpoint_p, record_full_breakpoints, bp);
  1333.     }
  1334. }

  1335. /* Sync existing breakpoints to record_full_breakpoints.  */

  1336. static void
  1337. record_full_init_record_breakpoints (void)
  1338. {
  1339.   VEC_free (record_full_breakpoint_p, record_full_breakpoints);

  1340.   iterate_over_bp_locations (record_full_sync_record_breakpoints);
  1341. }

  1342. /* Behavior is conditional on RECORD_FULL_IS_REPLAY.  We will not actually
  1343.    insert or remove breakpoints in the real target when replaying, nor
  1344.    when recording.  */

  1345. static int
  1346. record_full_insert_breakpoint (struct target_ops *ops,
  1347.                                struct gdbarch *gdbarch,
  1348.                                struct bp_target_info *bp_tgt)
  1349. {
  1350.   struct record_full_breakpoint *bp;
  1351.   int in_target_beneath = 0;

  1352.   if (!RECORD_FULL_IS_REPLAY)
  1353.     {
  1354.       /* When recording, we currently always single-step, so we don't
  1355.          really need to install regular breakpoints in the inferior.
  1356.          However, we do have to insert software single-step
  1357.          breakpoints, in case the target can't hardware step.  To keep
  1358.          things single, we always insert.  */
  1359.       struct cleanup *old_cleanups;
  1360.       int ret;

  1361.       old_cleanups = record_full_gdb_operation_disable_set ();
  1362.       ret = ops->beneath->to_insert_breakpoint (ops->beneath, gdbarch, bp_tgt);
  1363.       do_cleanups (old_cleanups);

  1364.       if (ret != 0)
  1365.         return ret;

  1366.       in_target_beneath = 1;
  1367.     }

  1368.   bp = XNEW (struct record_full_breakpoint);
  1369.   bp->addr = bp_tgt->placed_address;
  1370.   bp->address_space = bp_tgt->placed_address_space;
  1371.   bp->in_target_beneath = in_target_beneath;
  1372.   VEC_safe_push (record_full_breakpoint_p, record_full_breakpoints, bp);
  1373.   return 0;
  1374. }

  1375. /* "to_remove_breakpoint" method for process record target.  */

  1376. static int
  1377. record_full_remove_breakpoint (struct target_ops *ops,
  1378.                                struct gdbarch *gdbarch,
  1379.                                struct bp_target_info *bp_tgt)
  1380. {
  1381.   struct record_full_breakpoint *bp;
  1382.   int ix;

  1383.   for (ix = 0;
  1384.        VEC_iterate (record_full_breakpoint_p,
  1385.                     record_full_breakpoints, ix, bp);
  1386.        ++ix)
  1387.     {
  1388.       if (bp->addr == bp_tgt->placed_address
  1389.           && bp->address_space == bp_tgt->placed_address_space)
  1390.         {
  1391.           if (bp->in_target_beneath)
  1392.             {
  1393.               struct cleanup *old_cleanups;
  1394.               int ret;

  1395.               old_cleanups = record_full_gdb_operation_disable_set ();
  1396.               ret = ops->beneath->to_remove_breakpoint (ops->beneath, gdbarch,
  1397.                                                         bp_tgt);
  1398.               do_cleanups (old_cleanups);

  1399.               if (ret != 0)
  1400.                 return ret;
  1401.             }

  1402.           VEC_unordered_remove (record_full_breakpoint_p,
  1403.                                 record_full_breakpoints, ix);
  1404.           return 0;
  1405.         }
  1406.     }

  1407.   gdb_assert_not_reached ("removing unknown breakpoint");
  1408. }

  1409. /* "to_can_execute_reverse" method for process record target.  */

  1410. static int
  1411. record_full_can_execute_reverse (struct target_ops *self)
  1412. {
  1413.   return 1;
  1414. }

  1415. /* "to_get_bookmark" method for process record and prec over core.  */

  1416. static gdb_byte *
  1417. record_full_get_bookmark (struct target_ops *self, const char *args,
  1418.                           int from_tty)
  1419. {
  1420.   char *ret = NULL;

  1421.   /* Return stringified form of instruction count.  */
  1422.   if (record_full_list && record_full_list->type == record_full_end)
  1423.     ret = xstrdup (pulongest (record_full_list->u.end.insn_num));

  1424.   if (record_debug)
  1425.     {
  1426.       if (ret)
  1427.         fprintf_unfiltered (gdb_stdlog,
  1428.                             "record_full_get_bookmark returns %s\n", ret);
  1429.       else
  1430.         fprintf_unfiltered (gdb_stdlog,
  1431.                             "record_full_get_bookmark returns NULL\n");
  1432.     }
  1433.   return (gdb_byte *) ret;
  1434. }

  1435. /* "to_goto_bookmark" method for process record and prec over core.  */

  1436. static void
  1437. record_full_goto_bookmark (struct target_ops *self,
  1438.                            const gdb_byte *raw_bookmark, int from_tty)
  1439. {
  1440.   const char *bookmark = (const char *) raw_bookmark;
  1441.   struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);

  1442.   if (record_debug)
  1443.     fprintf_unfiltered (gdb_stdlog,
  1444.                         "record_full_goto_bookmark receives %s\n", bookmark);

  1445.   if (bookmark[0] == '\'' || bookmark[0] == '\"')
  1446.     {
  1447.       char *copy;

  1448.       if (bookmark[strlen (bookmark) - 1] != bookmark[0])
  1449.         error (_("Unbalanced quotes: %s"), bookmark);


  1450.       copy = savestring (bookmark + 1, strlen (bookmark) - 2);
  1451.       make_cleanup (xfree, copy);
  1452.       bookmark = copy;
  1453.     }

  1454.   record_goto (bookmark);

  1455.   do_cleanups (cleanup);
  1456. }

  1457. static enum exec_direction_kind
  1458. record_full_execution_direction (struct target_ops *self)
  1459. {
  1460.   return record_full_execution_dir;
  1461. }

  1462. static void
  1463. record_full_info (struct target_ops *self)
  1464. {
  1465.   struct record_full_entry *p;

  1466.   if (RECORD_FULL_IS_REPLAY)
  1467.     printf_filtered (_("Replay mode:\n"));
  1468.   else
  1469.     printf_filtered (_("Record mode:\n"));

  1470.   /* Find entry for first actual instruction in the log.  */
  1471.   for (p = record_full_first.next;
  1472.        p != NULL && p->type != record_full_end;
  1473.        p = p->next)
  1474.     ;

  1475.   /* Do we have a log at all?  */
  1476.   if (p != NULL && p->type == record_full_end)
  1477.     {
  1478.       /* Display instruction number for first instruction in the log.  */
  1479.       printf_filtered (_("Lowest recorded instruction number is %s.\n"),
  1480.                        pulongest (p->u.end.insn_num));

  1481.       /* If in replay mode, display where we are in the log.  */
  1482.       if (RECORD_FULL_IS_REPLAY)
  1483.         printf_filtered (_("Current instruction number is %s.\n"),
  1484.                          pulongest (record_full_list->u.end.insn_num));

  1485.       /* Display instruction number for last instruction in the log.  */
  1486.       printf_filtered (_("Highest recorded instruction number is %s.\n"),
  1487.                        pulongest (record_full_insn_count));

  1488.       /* Display log count.  */
  1489.       printf_filtered (_("Log contains %u instructions.\n"),
  1490.                        record_full_insn_num);
  1491.     }
  1492.   else
  1493.     printf_filtered (_("No instructions have been logged.\n"));

  1494.   /* Display max log size.  */
  1495.   printf_filtered (_("Max logged instructions is %u.\n"),
  1496.                    record_full_insn_max_num);
  1497. }

  1498. /* The "to_record_delete" target method.  */

  1499. static void
  1500. record_full_delete (struct target_ops *self)
  1501. {
  1502.   record_full_list_release_following (record_full_list);
  1503. }

  1504. /* The "to_record_is_replaying" target method.  */

  1505. static int
  1506. record_full_is_replaying (struct target_ops *self)
  1507. {
  1508.   return RECORD_FULL_IS_REPLAY;
  1509. }

  1510. /* Go to a specific entry.  */

  1511. static void
  1512. record_full_goto_entry (struct record_full_entry *p)
  1513. {
  1514.   if (p == NULL)
  1515.     error (_("Target insn not found."));
  1516.   else if (p == record_full_list)
  1517.     error (_("Already at target insn."));
  1518.   else if (p->u.end.insn_num > record_full_list->u.end.insn_num)
  1519.     {
  1520.       printf_filtered (_("Go forward to insn number %s\n"),
  1521.                        pulongest (p->u.end.insn_num));
  1522.       record_full_goto_insn (p, EXEC_FORWARD);
  1523.     }
  1524.   else
  1525.     {
  1526.       printf_filtered (_("Go backward to insn number %s\n"),
  1527.                        pulongest (p->u.end.insn_num));
  1528.       record_full_goto_insn (p, EXEC_REVERSE);
  1529.     }

  1530.   registers_changed ();
  1531.   reinit_frame_cache ();
  1532.   print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
  1533. }

  1534. /* The "to_goto_record_begin" target method.  */

  1535. static void
  1536. record_full_goto_begin (struct target_ops *self)
  1537. {
  1538.   struct record_full_entry *p = NULL;

  1539.   for (p = &record_full_first; p != NULL; p = p->next)
  1540.     if (p->type == record_full_end)
  1541.       break;

  1542.   record_full_goto_entry (p);
  1543. }

  1544. /* The "to_goto_record_end" target method.  */

  1545. static void
  1546. record_full_goto_end (struct target_ops *self)
  1547. {
  1548.   struct record_full_entry *p = NULL;

  1549.   for (p = record_full_list; p->next != NULL; p = p->next)
  1550.     ;
  1551.   for (; p!= NULL; p = p->prev)
  1552.     if (p->type == record_full_end)
  1553.       break;

  1554.   record_full_goto_entry (p);
  1555. }

  1556. /* The "to_goto_record" target method.  */

  1557. static void
  1558. record_full_goto (struct target_ops *self, ULONGEST target_insn)
  1559. {
  1560.   struct record_full_entry *p = NULL;

  1561.   for (p = &record_full_first; p != NULL; p = p->next)
  1562.     if (p->type == record_full_end && p->u.end.insn_num == target_insn)
  1563.       break;

  1564.   record_full_goto_entry (p);
  1565. }

  1566. static void
  1567. init_record_full_ops (void)
  1568. {
  1569.   record_full_ops.to_shortname = "record-full";
  1570.   record_full_ops.to_longname = "Process record and replay target";
  1571.   record_full_ops.to_doc =
  1572.     "Log program while executing and replay execution from log.";
  1573.   record_full_ops.to_open = record_full_open;
  1574.   record_full_ops.to_close = record_full_close;
  1575.   record_full_ops.to_resume = record_full_resume;
  1576.   record_full_ops.to_wait = record_full_wait;
  1577.   record_full_ops.to_disconnect = record_disconnect;
  1578.   record_full_ops.to_detach = record_detach;
  1579.   record_full_ops.to_mourn_inferior = record_mourn_inferior;
  1580.   record_full_ops.to_kill = record_kill;
  1581.   record_full_ops.to_store_registers = record_full_store_registers;
  1582.   record_full_ops.to_xfer_partial = record_full_xfer_partial;
  1583.   record_full_ops.to_insert_breakpoint = record_full_insert_breakpoint;
  1584.   record_full_ops.to_remove_breakpoint = record_full_remove_breakpoint;
  1585.   record_full_ops.to_stopped_by_watchpoint = record_full_stopped_by_watchpoint;
  1586.   record_full_ops.to_stopped_data_address = record_full_stopped_data_address;
  1587.   record_full_ops.to_can_execute_reverse = record_full_can_execute_reverse;
  1588.   record_full_ops.to_stratum = record_stratum;
  1589.   /* Add bookmark target methods.  */
  1590.   record_full_ops.to_get_bookmark = record_full_get_bookmark;
  1591.   record_full_ops.to_goto_bookmark = record_full_goto_bookmark;
  1592.   record_full_ops.to_execution_direction = record_full_execution_direction;
  1593.   record_full_ops.to_info_record = record_full_info;
  1594.   record_full_ops.to_save_record = record_full_save;
  1595.   record_full_ops.to_delete_record = record_full_delete;
  1596.   record_full_ops.to_record_is_replaying = record_full_is_replaying;
  1597.   record_full_ops.to_goto_record_begin = record_full_goto_begin;
  1598.   record_full_ops.to_goto_record_end = record_full_goto_end;
  1599.   record_full_ops.to_goto_record = record_full_goto;
  1600.   record_full_ops.to_magic = OPS_MAGIC;
  1601. }

  1602. /* "to_resume" method for prec over corefile.  */

  1603. static void
  1604. record_full_core_resume (struct target_ops *ops, ptid_t ptid, int step,
  1605.                          enum gdb_signal signal)
  1606. {
  1607.   record_full_resume_step = step;
  1608.   record_full_resumed = 1;
  1609.   record_full_execution_dir = execution_direction;

  1610.   /* We are about to start executing the inferior (or simulate it),
  1611.      let's register it with the event loop.  */
  1612.   if (target_can_async_p ())
  1613.     {
  1614.       target_async (inferior_event_handler, 0);

  1615.       /* Notify the event loop there's an event to wait for.  */
  1616.       mark_async_event_handler (record_full_async_inferior_event_token);
  1617.     }
  1618. }

  1619. /* "to_kill" method for prec over corefile.  */

  1620. static void
  1621. record_full_core_kill (struct target_ops *ops)
  1622. {
  1623.   if (record_debug)
  1624.     fprintf_unfiltered (gdb_stdlog, "Process record: record_full_core_kill\n");

  1625.   unpush_target (&record_full_core_ops);
  1626. }

  1627. /* "to_fetch_registers" method for prec over corefile.  */

  1628. static void
  1629. record_full_core_fetch_registers (struct target_ops *ops,
  1630.                                   struct regcache *regcache,
  1631.                                   int regno)
  1632. {
  1633.   if (regno < 0)
  1634.     {
  1635.       int num = gdbarch_num_regs (get_regcache_arch (regcache));
  1636.       int i;

  1637.       for (i = 0; i < num; i ++)
  1638.         regcache_raw_supply (regcache, i,
  1639.                              record_full_core_regbuf + MAX_REGISTER_SIZE * i);
  1640.     }
  1641.   else
  1642.     regcache_raw_supply (regcache, regno,
  1643.                          record_full_core_regbuf + MAX_REGISTER_SIZE * regno);
  1644. }

  1645. /* "to_prepare_to_store" method for prec over corefile.  */

  1646. static void
  1647. record_full_core_prepare_to_store (struct target_ops *self,
  1648.                                    struct regcache *regcache)
  1649. {
  1650. }

  1651. /* "to_store_registers" method for prec over corefile.  */

  1652. static void
  1653. record_full_core_store_registers (struct target_ops *ops,
  1654.                              struct regcache *regcache,
  1655.                              int regno)
  1656. {
  1657.   if (record_full_gdb_operation_disable)
  1658.     regcache_raw_collect (regcache, regno,
  1659.                           record_full_core_regbuf + MAX_REGISTER_SIZE * regno);
  1660.   else
  1661.     error (_("You can't do that without a process to debug."));
  1662. }

  1663. /* "to_xfer_partial" method for prec over corefile.  */

  1664. static enum target_xfer_status
  1665. record_full_core_xfer_partial (struct target_ops *ops,
  1666.                                enum target_object object,
  1667.                                const char *annex, gdb_byte *readbuf,
  1668.                                const gdb_byte *writebuf, ULONGEST offset,
  1669.                                ULONGEST len, ULONGEST *xfered_len)
  1670. {
  1671.   if (object == TARGET_OBJECT_MEMORY)
  1672.     {
  1673.       if (record_full_gdb_operation_disable || !writebuf)
  1674.         {
  1675.           struct target_section *p;

  1676.           for (p = record_full_core_start; p < record_full_core_end; p++)
  1677.             {
  1678.               if (offset >= p->addr)
  1679.                 {
  1680.                   struct record_full_core_buf_entry *entry;
  1681.                   ULONGEST sec_offset;

  1682.                   if (offset >= p->endaddr)
  1683.                     continue;

  1684.                   if (offset + len > p->endaddr)
  1685.                     len = p->endaddr - offset;

  1686.                   sec_offset = offset - p->addr;

  1687.                   /* Read readbuf or write writebuf p, offset, len.  */
  1688.                   /* Check flags.  */
  1689.                   if (p->the_bfd_section->flags & SEC_CONSTRUCTOR
  1690.                       || (p->the_bfd_section->flags & SEC_HAS_CONTENTS) == 0)
  1691.                     {
  1692.                       if (readbuf)
  1693.                         memset (readbuf, 0, len);

  1694.                       *xfered_len = len;
  1695.                       return TARGET_XFER_OK;
  1696.                     }
  1697.                   /* Get record_full_core_buf_entry.  */
  1698.                   for (entry = record_full_core_buf_list; entry;
  1699.                        entry = entry->prev)
  1700.                     if (entry->p == p)
  1701.                       break;
  1702.                   if (writebuf)
  1703.                     {
  1704.                       if (!entry)
  1705.                         {
  1706.                           /* Add a new entry.  */
  1707.                           entry = (struct record_full_core_buf_entry *)
  1708.                             xmalloc
  1709.                             (sizeof (struct record_full_core_buf_entry));
  1710.                           entry->p = p;
  1711.                           if (!bfd_malloc_and_get_section
  1712.                                 (p->the_bfd_section->owner,
  1713.                                  p->the_bfd_section,
  1714.                                  &entry->buf))
  1715.                             {
  1716.                               xfree (entry);
  1717.                               return TARGET_XFER_EOF;
  1718.                             }
  1719.                           entry->prev = record_full_core_buf_list;
  1720.                           record_full_core_buf_list = entry;
  1721.                         }

  1722.                       memcpy (entry->buf + sec_offset, writebuf,
  1723.                               (size_t) len);
  1724.                     }
  1725.                   else
  1726.                     {
  1727.                       if (!entry)
  1728.                         return ops->beneath->to_xfer_partial (ops->beneath,
  1729.                                                               object, annex,
  1730.                                                               readbuf, writebuf,
  1731.                                                               offset, len,
  1732.                                                               xfered_len);

  1733.                       memcpy (readbuf, entry->buf + sec_offset,
  1734.                               (size_t) len);
  1735.                     }

  1736.                   *xfered_len = len;
  1737.                   return TARGET_XFER_OK;
  1738.                 }
  1739.             }

  1740.           return TARGET_XFER_E_IO;
  1741.         }
  1742.       else
  1743.         error (_("You can't do that without a process to debug."));
  1744.     }

  1745.   return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
  1746.                                         readbuf, writebuf, offset, len,
  1747.                                         xfered_len);
  1748. }

  1749. /* "to_insert_breakpoint" method for prec over corefile.  */

  1750. static int
  1751. record_full_core_insert_breakpoint (struct target_ops *ops,
  1752.                                     struct gdbarch *gdbarch,
  1753.                                     struct bp_target_info *bp_tgt)
  1754. {
  1755.   return 0;
  1756. }

  1757. /* "to_remove_breakpoint" method for prec over corefile.  */

  1758. static int
  1759. record_full_core_remove_breakpoint (struct target_ops *ops,
  1760.                                     struct gdbarch *gdbarch,
  1761.                                     struct bp_target_info *bp_tgt)
  1762. {
  1763.   return 0;
  1764. }

  1765. /* "to_has_execution" method for prec over corefile.  */

  1766. static int
  1767. record_full_core_has_execution (struct target_ops *ops, ptid_t the_ptid)
  1768. {
  1769.   return 1;
  1770. }

  1771. static void
  1772. init_record_full_core_ops (void)
  1773. {
  1774.   record_full_core_ops.to_shortname = "record-core";
  1775.   record_full_core_ops.to_longname = "Process record and replay target";
  1776.   record_full_core_ops.to_doc =
  1777.     "Log program while executing and replay execution from log.";
  1778.   record_full_core_ops.to_open = record_full_open;
  1779.   record_full_core_ops.to_close = record_full_close;
  1780.   record_full_core_ops.to_resume = record_full_core_resume;
  1781.   record_full_core_ops.to_wait = record_full_wait;
  1782.   record_full_core_ops.to_kill = record_full_core_kill;
  1783.   record_full_core_ops.to_fetch_registers = record_full_core_fetch_registers;
  1784.   record_full_core_ops.to_prepare_to_store = record_full_core_prepare_to_store;
  1785.   record_full_core_ops.to_store_registers = record_full_core_store_registers;
  1786.   record_full_core_ops.to_xfer_partial = record_full_core_xfer_partial;
  1787.   record_full_core_ops.to_insert_breakpoint
  1788.     = record_full_core_insert_breakpoint;
  1789.   record_full_core_ops.to_remove_breakpoint
  1790.     = record_full_core_remove_breakpoint;
  1791.   record_full_core_ops.to_stopped_by_watchpoint
  1792.     = record_full_stopped_by_watchpoint;
  1793.   record_full_core_ops.to_stopped_data_address
  1794.     = record_full_stopped_data_address;
  1795.   record_full_core_ops.to_can_execute_reverse
  1796.     = record_full_can_execute_reverse;
  1797.   record_full_core_ops.to_has_execution = record_full_core_has_execution;
  1798.   record_full_core_ops.to_stratum = record_stratum;
  1799.   /* Add bookmark target methods.  */
  1800.   record_full_core_ops.to_get_bookmark = record_full_get_bookmark;
  1801.   record_full_core_ops.to_goto_bookmark = record_full_goto_bookmark;
  1802.   record_full_core_ops.to_execution_direction
  1803.     = record_full_execution_direction;
  1804.   record_full_core_ops.to_info_record = record_full_info;
  1805.   record_full_core_ops.to_delete_record = record_full_delete;
  1806.   record_full_core_ops.to_record_is_replaying = record_full_is_replaying;
  1807.   record_full_core_ops.to_goto_record_begin = record_full_goto_begin;
  1808.   record_full_core_ops.to_goto_record_end = record_full_goto_end;
  1809.   record_full_core_ops.to_goto_record = record_full_goto;
  1810.   record_full_core_ops.to_magic = OPS_MAGIC;
  1811. }

  1812. /* Record log save-file format
  1813.    Version 1 (never released)

  1814.    Header:
  1815.      4 bytes: magic number htonl(0x20090829).
  1816.        NOTE: be sure to change whenever this file format changes!

  1817.    Records:
  1818.      record_full_end:
  1819.        1 byte:  record type (record_full_end, see enum record_full_type).
  1820.      record_full_reg:
  1821.        1 byte:  record type (record_full_reg, see enum record_full_type).
  1822.        8 bytes: register id (network byte order).
  1823.        MAX_REGISTER_SIZE bytes: register value.
  1824.      record_full_mem:
  1825.        1 byte:  record type (record_full_mem, see enum record_full_type).
  1826.        8 bytes: memory length (network byte order).
  1827.        8 bytes: memory address (network byte order).
  1828.        n bytes: memory value (n == memory length).

  1829.    Version 2
  1830.      4 bytes: magic number netorder32(0x20091016).
  1831.        NOTE: be sure to change whenever this file format changes!

  1832.    Records:
  1833.      record_full_end:
  1834.        1 byte:  record type (record_full_end, see enum record_full_type).
  1835.        4 bytes: signal
  1836.        4 bytes: instruction count
  1837.      record_full_reg:
  1838.        1 byte:  record type (record_full_reg, see enum record_full_type).
  1839.        4 bytes: register id (network byte order).
  1840.        n bytes: register value (n == actual register size).
  1841.                 (eg. 4 bytes for x86 general registers).
  1842.      record_full_mem:
  1843.        1 byte:  record type (record_full_mem, see enum record_full_type).
  1844.        4 bytes: memory length (network byte order).
  1845.        8 bytes: memory address (network byte order).
  1846.        n bytes: memory value (n == memory length).

  1847. */

  1848. /* bfdcore_read -- read bytes from a core file section.  */

  1849. static inline void
  1850. bfdcore_read (bfd *obfd, asection *osec, void *buf, int len, int *offset)
  1851. {
  1852.   int ret = bfd_get_section_contents (obfd, osec, buf, *offset, len);

  1853.   if (ret)
  1854.     *offset += len;
  1855.   else
  1856.     error (_("Failed to read %d bytes from core file %s ('%s')."),
  1857.            len, bfd_get_filename (obfd),
  1858.            bfd_errmsg (bfd_get_error ()));
  1859. }

  1860. static inline uint64_t
  1861. netorder64 (uint64_t input)
  1862. {
  1863.   uint64_t ret;

  1864.   store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
  1865.                           BFD_ENDIAN_BIG, input);
  1866.   return ret;
  1867. }

  1868. static inline uint32_t
  1869. netorder32 (uint32_t input)
  1870. {
  1871.   uint32_t ret;

  1872.   store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
  1873.                           BFD_ENDIAN_BIG, input);
  1874.   return ret;
  1875. }

  1876. static inline uint16_t
  1877. netorder16 (uint16_t input)
  1878. {
  1879.   uint16_t ret;

  1880.   store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
  1881.                           BFD_ENDIAN_BIG, input);
  1882.   return ret;
  1883. }

  1884. /* Restore the execution log from a core_bfd file.  */
  1885. static void
  1886. record_full_restore (void)
  1887. {
  1888.   uint32_t magic;
  1889.   struct cleanup *old_cleanups;
  1890.   struct record_full_entry *rec;
  1891.   asection *osec;
  1892.   uint32_t osec_size;
  1893.   int bfd_offset = 0;
  1894.   struct regcache *regcache;

  1895.   /* We restore the execution log from the open core bfd,
  1896.      if there is one.  */
  1897.   if (core_bfd == NULL)
  1898.     return;

  1899.   /* "record_full_restore" can only be called when record list is empty.  */
  1900.   gdb_assert (record_full_first.next == NULL);

  1901.   if (record_debug)
  1902.     fprintf_unfiltered (gdb_stdlog, "Restoring recording from core file.\n");

  1903.   /* Now need to find our special note section.  */
  1904.   osec = bfd_get_section_by_name (core_bfd, "null0");
  1905.   if (record_debug)
  1906.     fprintf_unfiltered (gdb_stdlog, "Find precord section %s.\n",
  1907.                         osec ? "succeeded" : "failed");
  1908.   if (osec == NULL)
  1909.     return;
  1910.   osec_size = bfd_section_size (core_bfd, osec);
  1911.   if (record_debug)
  1912.     fprintf_unfiltered (gdb_stdlog, "%s", bfd_section_name (core_bfd, osec));

  1913.   /* Check the magic code.  */
  1914.   bfdcore_read (core_bfd, osec, &magic, sizeof (magic), &bfd_offset);
  1915.   if (magic != RECORD_FULL_FILE_MAGIC)
  1916.     error (_("Version mis-match or file format error in core file %s."),
  1917.            bfd_get_filename (core_bfd));
  1918.   if (record_debug)
  1919.     fprintf_unfiltered (gdb_stdlog,
  1920.                         "  Reading 4-byte magic cookie "
  1921.                         "RECORD_FULL_FILE_MAGIC (0x%s)\n",
  1922.                         phex_nz (netorder32 (magic), 4));

  1923.   /* Restore the entries in recfd into record_full_arch_list_head and
  1924.      record_full_arch_list_tail.  */
  1925.   record_full_arch_list_head = NULL;
  1926.   record_full_arch_list_tail = NULL;
  1927.   record_full_insn_num = 0;
  1928.   old_cleanups = make_cleanup (record_full_arch_list_cleanups, 0);
  1929.   regcache = get_current_regcache ();

  1930.   while (1)
  1931.     {
  1932.       uint8_t rectype;
  1933.       uint32_t regnum, len, signal, count;
  1934.       uint64_t addr;

  1935.       /* We are finished when offset reaches osec_size.  */
  1936.       if (bfd_offset >= osec_size)
  1937.         break;
  1938.       bfdcore_read (core_bfd, osec, &rectype, sizeof (rectype), &bfd_offset);

  1939.       switch (rectype)
  1940.         {
  1941.         case record_full_reg: /* reg */
  1942.           /* Get register number to regnum.  */
  1943.           bfdcore_read (core_bfd, osec, &regnum,
  1944.                         sizeof (regnum), &bfd_offset);
  1945.           regnum = netorder32 (regnum);

  1946.           rec = record_full_reg_alloc (regcache, regnum);

  1947.           /* Get val.  */
  1948.           bfdcore_read (core_bfd, osec, record_full_get_loc (rec),
  1949.                         rec->u.reg.len, &bfd_offset);

  1950.           if (record_debug)
  1951.             fprintf_unfiltered (gdb_stdlog,
  1952.                                 "  Reading register %d (1 "
  1953.                                 "plus %lu plus %d bytes)\n",
  1954.                                 rec->u.reg.num,
  1955.                                 (unsigned long) sizeof (regnum),
  1956.                                 rec->u.reg.len);
  1957.           break;

  1958.         case record_full_mem: /* mem */
  1959.           /* Get len.  */
  1960.           bfdcore_read (core_bfd, osec, &len,
  1961.                         sizeof (len), &bfd_offset);
  1962.           len = netorder32 (len);

  1963.           /* Get addr.  */
  1964.           bfdcore_read (core_bfd, osec, &addr,
  1965.                         sizeof (addr), &bfd_offset);
  1966.           addr = netorder64 (addr);

  1967.           rec = record_full_mem_alloc (addr, len);

  1968.           /* Get val.  */
  1969.           bfdcore_read (core_bfd, osec, record_full_get_loc (rec),
  1970.                         rec->u.mem.len, &bfd_offset);

  1971.           if (record_debug)
  1972.             fprintf_unfiltered (gdb_stdlog,
  1973.                                 "  Reading memory %s (1 plus "
  1974.                                 "%lu plus %lu plus %d bytes)\n",
  1975.                                 paddress (get_current_arch (),
  1976.                                           rec->u.mem.addr),
  1977.                                 (unsigned long) sizeof (addr),
  1978.                                 (unsigned long) sizeof (len),
  1979.                                 rec->u.mem.len);
  1980.           break;

  1981.         case record_full_end: /* end */
  1982.           rec = record_full_end_alloc ();
  1983.           record_full_insn_num ++;

  1984.           /* Get signal value.  */
  1985.           bfdcore_read (core_bfd, osec, &signal,
  1986.                         sizeof (signal), &bfd_offset);
  1987.           signal = netorder32 (signal);
  1988.           rec->u.end.sigval = signal;

  1989.           /* Get insn count.  */
  1990.           bfdcore_read (core_bfd, osec, &count,
  1991.                         sizeof (count), &bfd_offset);
  1992.           count = netorder32 (count);
  1993.           rec->u.end.insn_num = count;
  1994.           record_full_insn_count = count + 1;
  1995.           if (record_debug)
  1996.             fprintf_unfiltered (gdb_stdlog,
  1997.                                 "  Reading record_full_end (1 + "
  1998.                                 "%lu + %lu bytes), offset == %s\n",
  1999.                                 (unsigned long) sizeof (signal),
  2000.                                 (unsigned long) sizeof (count),
  2001.                                 paddress (get_current_arch (),
  2002.                                           bfd_offset));
  2003.           break;

  2004.         default:
  2005.           error (_("Bad entry type in core file %s."),
  2006.                  bfd_get_filename (core_bfd));
  2007.           break;
  2008.         }

  2009.       /* Add rec to record arch list.  */
  2010.       record_full_arch_list_add (rec);
  2011.     }

  2012.   discard_cleanups (old_cleanups);

  2013.   /* Add record_full_arch_list_head to the end of record list.  */
  2014.   record_full_first.next = record_full_arch_list_head;
  2015.   record_full_arch_list_head->prev = &record_full_first;
  2016.   record_full_arch_list_tail->next = NULL;
  2017.   record_full_list = &record_full_first;

  2018.   /* Update record_full_insn_max_num.  */
  2019.   if (record_full_insn_num > record_full_insn_max_num)
  2020.     {
  2021.       record_full_insn_max_num = record_full_insn_num;
  2022.       warning (_("Auto increase record/replay buffer limit to %u."),
  2023.                record_full_insn_max_num);
  2024.     }

  2025.   /* Succeeded.  */
  2026.   printf_filtered (_("Restored records from core file %s.\n"),
  2027.                    bfd_get_filename (core_bfd));

  2028.   print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
  2029. }

  2030. /* bfdcore_write -- write bytes into a core file section.  */

  2031. static inline void
  2032. bfdcore_write (bfd *obfd, asection *osec, void *buf, int len, int *offset)
  2033. {
  2034.   int ret = bfd_set_section_contents (obfd, osec, buf, *offset, len);

  2035.   if (ret)
  2036.     *offset += len;
  2037.   else
  2038.     error (_("Failed to write %d bytes to core file %s ('%s')."),
  2039.            len, bfd_get_filename (obfd),
  2040.            bfd_errmsg (bfd_get_error ()));
  2041. }

  2042. /* Restore the execution log from a file.  We use a modified elf
  2043.    corefile format, with an extra section for our data.  */

  2044. static void
  2045. cmd_record_full_restore (char *args, int from_tty)
  2046. {
  2047.   core_file_command (args, from_tty);
  2048.   record_full_open (args, from_tty);
  2049. }

  2050. static void
  2051. record_full_save_cleanups (void *data)
  2052. {
  2053.   bfd *obfd = data;
  2054.   char *pathname = xstrdup (bfd_get_filename (obfd));

  2055.   gdb_bfd_unref (obfd);
  2056.   unlink (pathname);
  2057.   xfree (pathname);
  2058. }

  2059. /* Save the execution log to a file.  We use a modified elf corefile
  2060.    format, with an extra section for our data.  */

  2061. static void
  2062. record_full_save (struct target_ops *self, const char *recfilename)
  2063. {
  2064.   struct record_full_entry *cur_record_full_list;
  2065.   uint32_t magic;
  2066.   struct regcache *regcache;
  2067.   struct gdbarch *gdbarch;
  2068.   struct cleanup *old_cleanups;
  2069.   struct cleanup *set_cleanups;
  2070.   bfd *obfd;
  2071.   int save_size = 0;
  2072.   asection *osec = NULL;
  2073.   int bfd_offset = 0;

  2074.   /* Open the save file.  */
  2075.   if (record_debug)
  2076.     fprintf_unfiltered (gdb_stdlog, "Saving execution log to core file '%s'\n",
  2077.                         recfilename);

  2078.   /* Open the output file.  */
  2079.   obfd = create_gcore_bfd (recfilename);
  2080.   old_cleanups = make_cleanup (record_full_save_cleanups, obfd);

  2081.   /* Save the current record entry to "cur_record_full_list".  */
  2082.   cur_record_full_list = record_full_list;

  2083.   /* Get the values of regcache and gdbarch.  */
  2084.   regcache = get_current_regcache ();
  2085.   gdbarch = get_regcache_arch (regcache);

  2086.   /* Disable the GDB operation record.  */
  2087.   set_cleanups = record_full_gdb_operation_disable_set ();

  2088.   /* Reverse execute to the begin of record list.  */
  2089.   while (1)
  2090.     {
  2091.       /* Check for beginning and end of log.  */
  2092.       if (record_full_list == &record_full_first)
  2093.         break;

  2094.       record_full_exec_insn (regcache, gdbarch, record_full_list);

  2095.       if (record_full_list->prev)
  2096.         record_full_list = record_full_list->prev;
  2097.     }

  2098.   /* Compute the size needed for the extra bfd section.  */
  2099.   save_size = 4;        /* magic cookie */
  2100.   for (record_full_list = record_full_first.next; record_full_list;
  2101.        record_full_list = record_full_list->next)
  2102.     switch (record_full_list->type)
  2103.       {
  2104.       case record_full_end:
  2105.         save_size += 1 + 4 + 4;
  2106.         break;
  2107.       case record_full_reg:
  2108.         save_size += 1 + 4 + record_full_list->u.reg.len;
  2109.         break;
  2110.       case record_full_mem:
  2111.         save_size += 1 + 4 + 8 + record_full_list->u.mem.len;
  2112.         break;
  2113.       }

  2114.   /* Make the new bfd section.  */
  2115.   osec = bfd_make_section_anyway_with_flags (obfd, "precord",
  2116.                                              SEC_HAS_CONTENTS
  2117.                                              | SEC_READONLY);
  2118.   if (osec == NULL)
  2119.     error (_("Failed to create 'precord' section for corefile %s: %s"),
  2120.            recfilename,
  2121.            bfd_errmsg (bfd_get_error ()));
  2122.   bfd_set_section_size (obfd, osec, save_size);
  2123.   bfd_set_section_vma (obfd, osec, 0);
  2124.   bfd_set_section_alignment (obfd, osec, 0);
  2125.   bfd_section_lma (obfd, osec) = 0;

  2126.   /* Save corefile state.  */
  2127.   write_gcore_file (obfd);

  2128.   /* Write out the record log.  */
  2129.   /* Write the magic code.  */
  2130.   magic = RECORD_FULL_FILE_MAGIC;
  2131.   if (record_debug)
  2132.     fprintf_unfiltered (gdb_stdlog,
  2133.                         "  Writing 4-byte magic cookie "
  2134.                         "RECORD_FULL_FILE_MAGIC (0x%s)\n",
  2135.                       phex_nz (magic, 4));
  2136.   bfdcore_write (obfd, osec, &magic, sizeof (magic), &bfd_offset);

  2137.   /* Save the entries to recfd and forward execute to the end of
  2138.      record list.  */
  2139.   record_full_list = &record_full_first;
  2140.   while (1)
  2141.     {
  2142.       /* Save entry.  */
  2143.       if (record_full_list != &record_full_first)
  2144.         {
  2145.           uint8_t type;
  2146.           uint32_t regnum, len, signal, count;
  2147.           uint64_t addr;

  2148.           type = record_full_list->type;
  2149.           bfdcore_write (obfd, osec, &type, sizeof (type), &bfd_offset);

  2150.           switch (record_full_list->type)
  2151.             {
  2152.             case record_full_reg: /* reg */
  2153.               if (record_debug)
  2154.                 fprintf_unfiltered (gdb_stdlog,
  2155.                                     "  Writing register %d (1 "
  2156.                                     "plus %lu plus %d bytes)\n",
  2157.                                     record_full_list->u.reg.num,
  2158.                                     (unsigned long) sizeof (regnum),
  2159.                                     record_full_list->u.reg.len);

  2160.               /* Write regnum.  */
  2161.               regnum = netorder32 (record_full_list->u.reg.num);
  2162.               bfdcore_write (obfd, osec, &regnum,
  2163.                              sizeof (regnum), &bfd_offset);

  2164.               /* Write regval.  */
  2165.               bfdcore_write (obfd, osec,
  2166.                              record_full_get_loc (record_full_list),
  2167.                              record_full_list->u.reg.len, &bfd_offset);
  2168.               break;

  2169.             case record_full_mem: /* mem */
  2170.               if (record_debug)
  2171.                 fprintf_unfiltered (gdb_stdlog,
  2172.                                     "  Writing memory %s (1 plus "
  2173.                                     "%lu plus %lu plus %d bytes)\n",
  2174.                                     paddress (gdbarch,
  2175.                                               record_full_list->u.mem.addr),
  2176.                                     (unsigned long) sizeof (addr),
  2177.                                     (unsigned long) sizeof (len),
  2178.                                     record_full_list->u.mem.len);

  2179.               /* Write memlen.  */
  2180.               len = netorder32 (record_full_list->u.mem.len);
  2181.               bfdcore_write (obfd, osec, &len, sizeof (len), &bfd_offset);

  2182.               /* Write memaddr.  */
  2183.               addr = netorder64 (record_full_list->u.mem.addr);
  2184.               bfdcore_write (obfd, osec, &addr,
  2185.                              sizeof (addr), &bfd_offset);

  2186.               /* Write memval.  */
  2187.               bfdcore_write (obfd, osec,
  2188.                              record_full_get_loc (record_full_list),
  2189.                              record_full_list->u.mem.len, &bfd_offset);
  2190.               break;

  2191.               case record_full_end:
  2192.                 if (record_debug)
  2193.                   fprintf_unfiltered (gdb_stdlog,
  2194.                                       "  Writing record_full_end (1 + "
  2195.                                       "%lu + %lu bytes)\n",
  2196.                                       (unsigned long) sizeof (signal),
  2197.                                       (unsigned long) sizeof (count));
  2198.                 /* Write signal value.  */
  2199.                 signal = netorder32 (record_full_list->u.end.sigval);
  2200.                 bfdcore_write (obfd, osec, &signal,
  2201.                                sizeof (signal), &bfd_offset);

  2202.                 /* Write insn count.  */
  2203.                 count = netorder32 (record_full_list->u.end.insn_num);
  2204.                 bfdcore_write (obfd, osec, &count,
  2205.                                sizeof (count), &bfd_offset);
  2206.                 break;
  2207.             }
  2208.         }

  2209.       /* Execute entry.  */
  2210.       record_full_exec_insn (regcache, gdbarch, record_full_list);

  2211.       if (record_full_list->next)
  2212.         record_full_list = record_full_list->next;
  2213.       else
  2214.         break;
  2215.     }

  2216.   /* Reverse execute to cur_record_full_list.  */
  2217.   while (1)
  2218.     {
  2219.       /* Check for beginning and end of log.  */
  2220.       if (record_full_list == cur_record_full_list)
  2221.         break;

  2222.       record_full_exec_insn (regcache, gdbarch, record_full_list);

  2223.       if (record_full_list->prev)
  2224.         record_full_list = record_full_list->prev;
  2225.     }

  2226.   do_cleanups (set_cleanups);
  2227.   gdb_bfd_unref (obfd);
  2228.   discard_cleanups (old_cleanups);

  2229.   /* Succeeded.  */
  2230.   printf_filtered (_("Saved core file %s with execution log.\n"),
  2231.                    recfilename);
  2232. }

  2233. /* record_full_goto_insn -- rewind the record log (forward or backward,
  2234.    depending on DIR) to the given entry, changing the program state
  2235.    correspondingly.  */

  2236. static void
  2237. record_full_goto_insn (struct record_full_entry *entry,
  2238.                        enum exec_direction_kind dir)
  2239. {
  2240.   struct cleanup *set_cleanups = record_full_gdb_operation_disable_set ();
  2241.   struct regcache *regcache = get_current_regcache ();
  2242.   struct gdbarch *gdbarch = get_regcache_arch (regcache);

  2243.   /* Assume everything is valid: we will hit the entry,
  2244.      and we will not hit the end of the recording.  */

  2245.   if (dir == EXEC_FORWARD)
  2246.     record_full_list = record_full_list->next;

  2247.   do
  2248.     {
  2249.       record_full_exec_insn (regcache, gdbarch, record_full_list);
  2250.       if (dir == EXEC_REVERSE)
  2251.         record_full_list = record_full_list->prev;
  2252.       else
  2253.         record_full_list = record_full_list->next;
  2254.     } while (record_full_list != entry);
  2255.   do_cleanups (set_cleanups);
  2256. }

  2257. /* Alias for "target record-full".  */

  2258. static void
  2259. cmd_record_full_start (char *args, int from_tty)
  2260. {
  2261.   execute_command ("target record-full", from_tty);
  2262. }

  2263. static void
  2264. set_record_full_insn_max_num (char *args, int from_tty,
  2265.                               struct cmd_list_element *c)
  2266. {
  2267.   if (record_full_insn_num > record_full_insn_max_num)
  2268.     {
  2269.       /* Count down record_full_insn_num while releasing records from list.  */
  2270.       while (record_full_insn_num > record_full_insn_max_num)
  2271.        {
  2272.          record_full_list_release_first ();
  2273.          record_full_insn_num--;
  2274.        }
  2275.     }
  2276. }

  2277. /* The "set record full" command.  */

  2278. static void
  2279. set_record_full_command (char *args, int from_tty)
  2280. {
  2281.   printf_unfiltered (_("\"set record full\" must be followed "
  2282.                        "by an apporpriate subcommand.\n"));
  2283.   help_list (set_record_full_cmdlist, "set record full ", all_commands,
  2284.              gdb_stdout);
  2285. }

  2286. /* The "show record full" command.  */

  2287. static void
  2288. show_record_full_command (char *args, int from_tty)
  2289. {
  2290.   cmd_show_list (show_record_full_cmdlist, from_tty, "");
  2291. }

  2292. /* Provide a prototype to silence -Wmissing-prototypes.  */
  2293. extern initialize_file_ftype _initialize_record_full;

  2294. void
  2295. _initialize_record_full (void)
  2296. {
  2297.   struct cmd_list_element *c;

  2298.   /* Init record_full_first.  */
  2299.   record_full_first.prev = NULL;
  2300.   record_full_first.next = NULL;
  2301.   record_full_first.type = record_full_end;

  2302.   init_record_full_ops ();
  2303.   add_target (&record_full_ops);
  2304.   add_deprecated_target_alias (&record_full_ops, "record");
  2305.   init_record_full_core_ops ();
  2306.   add_target (&record_full_core_ops);

  2307.   add_prefix_cmd ("full", class_obscure, cmd_record_full_start,
  2308.                   _("Start full execution recording."), &record_full_cmdlist,
  2309.                   "record full ", 0, &record_cmdlist);

  2310.   c = add_cmd ("restore", class_obscure, cmd_record_full_restore,
  2311.                _("Restore the execution log from a file.\n\
  2312. Argument is filename.  File must be created with 'record save'."),
  2313.                &record_full_cmdlist);
  2314.   set_cmd_completer (c, filename_completer);

  2315.   /* Deprecate the old version without "full" prefix.  */
  2316.   c = add_alias_cmd ("restore", "full restore", class_obscure, 1,
  2317.                      &record_cmdlist);
  2318.   set_cmd_completer (c, filename_completer);
  2319.   deprecate_cmd (c, "record full restore");

  2320.   add_prefix_cmd ("full", class_support, set_record_full_command,
  2321.                   _("Set record options"), &set_record_full_cmdlist,
  2322.                   "set record full ", 0, &set_record_cmdlist);

  2323.   add_prefix_cmd ("full", class_support, show_record_full_command,
  2324.                   _("Show record options"), &show_record_full_cmdlist,
  2325.                   "show record full ", 0, &show_record_cmdlist);

  2326.   /* Record instructions number limit command.  */
  2327.   add_setshow_boolean_cmd ("stop-at-limit", no_class,
  2328.                            &record_full_stop_at_limit, _("\
  2329. Set whether record/replay stops when record/replay buffer becomes full."), _("\
  2330. Show whether record/replay stops when record/replay buffer becomes full."),
  2331.                            _("Default is ON.\n\
  2332. When ON, if the record/replay buffer becomes full, ask user what to do.\n\
  2333. When OFF, if the record/replay buffer becomes full,\n\
  2334. delete the oldest recorded instruction to make room for each new one."),
  2335.                            NULL, NULL,
  2336.                            &set_record_full_cmdlist, &show_record_full_cmdlist);

  2337.   c = add_alias_cmd ("stop-at-limit", "full stop-at-limit", no_class, 1,
  2338.                      &set_record_cmdlist);
  2339.   deprecate_cmd (c, "set record full stop-at-limit");

  2340.   c = add_alias_cmd ("stop-at-limit", "full stop-at-limit", no_class, 1,
  2341.                      &show_record_cmdlist);
  2342.   deprecate_cmd (c, "show record full stop-at-limit");

  2343.   add_setshow_uinteger_cmd ("insn-number-max", no_class,
  2344.                             &record_full_insn_max_num,
  2345.                             _("Set record/replay buffer limit."),
  2346.                             _("Show record/replay buffer limit."), _("\
  2347. Set the maximum number of instructions to be stored in the\n\
  2348. record/replay bufferA value of either \"unlimited\" or zero means no\n\
  2349. limit.  Default is 200000."),
  2350.                             set_record_full_insn_max_num,
  2351.                             NULL, &set_record_full_cmdlist,
  2352.                             &show_record_full_cmdlist);

  2353.   c = add_alias_cmd ("insn-number-max", "full insn-number-max", no_class, 1,
  2354.                      &set_record_cmdlist);
  2355.   deprecate_cmd (c, "set record full insn-number-max");

  2356.   c = add_alias_cmd ("insn-number-max", "full insn-number-max", no_class, 1,
  2357.                      &show_record_cmdlist);
  2358.   deprecate_cmd (c, "show record full insn-number-max");

  2359.   add_setshow_boolean_cmd ("memory-query", no_class,
  2360.                            &record_full_memory_query, _("\
  2361. Set whether query if PREC cannot record memory change of next instruction."),
  2362.                            _("\
  2363. Show whether query if PREC cannot record memory change of next instruction."),
  2364.                            _("\
  2365. Default is OFF.\n\
  2366. When ON, query if PREC cannot record memory change of next instruction."),
  2367.                            NULL, NULL,
  2368.                            &set_record_full_cmdlist,
  2369.                            &show_record_full_cmdlist);

  2370.   c = add_alias_cmd ("memory-query", "full memory-query", no_class, 1,
  2371.                      &set_record_cmdlist);
  2372.   deprecate_cmd (c, "set record full memory-query");

  2373.   c = add_alias_cmd ("memory-query", "full memory-query", no_class, 1,
  2374.                      &show_record_cmdlist);
  2375.   deprecate_cmd (c, "show record full memory-query");
  2376. }