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