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