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