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