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