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