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