2dd6fafd5cf087617dbb4bed38a2ba142775d1e2
[deliverable/binutils-gdb.git] / gdb / record.c
1 /* Process record and replay target for GDB, the GNU debugger.
2
3 Copyright (C) 2008, 2009 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "gdbcmd.h"
22 #include "regcache.h"
23 #include "gdbthread.h"
24 #include "event-top.h"
25 #include "exceptions.h"
26 #include "record.h"
27
28 #include <signal.h>
29
30 #define DEFAULT_RECORD_INSN_MAX_NUM 200000
31
32 #define RECORD_IS_REPLAY \
33 (record_list->next || execution_direction == EXEC_REVERSE)
34
35 /* These are the core structs of the process record functionality.
36
37 A record_entry is a record of the value change of a register
38 ("record_reg") or a part of memory ("record_mem"). And each
39 instruction must have a struct record_entry ("record_end") that
40 indicates that this is the last struct record_entry of this
41 instruction.
42
43 Each struct record_entry is linked to "record_list" by "prev" and
44 "next" pointers. */
45
46 struct record_mem_entry
47 {
48 CORE_ADDR addr;
49 int len;
50 /* Set this flag if target memory for this entry
51 can no longer be accessed. */
52 int mem_entry_not_accessible;
53 union
54 {
55 gdb_byte *ptr;
56 gdb_byte buf[sizeof (gdb_byte *)];
57 } u;
58 };
59
60 struct record_reg_entry
61 {
62 unsigned short num;
63 unsigned short len;
64 union
65 {
66 gdb_byte *ptr;
67 gdb_byte buf[2 * sizeof (gdb_byte *)];
68 } u;
69 };
70
71 struct record_end_entry
72 {
73 enum target_signal sigval;
74 };
75
76 enum record_type
77 {
78 record_end = 0,
79 record_reg,
80 record_mem
81 };
82
83 struct record_entry
84 {
85 struct record_entry *prev;
86 struct record_entry *next;
87 enum record_type type;
88 union
89 {
90 /* reg */
91 struct record_reg_entry reg;
92 /* mem */
93 struct record_mem_entry mem;
94 /* end */
95 struct record_end_entry end;
96 } u;
97 };
98
99 /* This is the debug switch for process record. */
100 int record_debug = 0;
101
102 /* These list is for execution log. */
103 static struct record_entry record_first;
104 static struct record_entry *record_list = &record_first;
105 static struct record_entry *record_arch_list_head = NULL;
106 static struct record_entry *record_arch_list_tail = NULL;
107
108 /* 1 ask user. 0 auto delete the last struct record_entry. */
109 static int record_stop_at_limit = 1;
110 static unsigned int record_insn_max_num = DEFAULT_RECORD_INSN_MAX_NUM;
111 static int record_insn_num = 0;
112
113 /* The target_ops of process record. */
114 static struct target_ops record_ops;
115
116 /* The beneath function pointers. */
117 static struct target_ops *record_beneath_to_resume_ops;
118 static void (*record_beneath_to_resume) (struct target_ops *, ptid_t, int,
119 enum target_signal);
120 static struct target_ops *record_beneath_to_wait_ops;
121 static ptid_t (*record_beneath_to_wait) (struct target_ops *, ptid_t,
122 struct target_waitstatus *,
123 int);
124 static struct target_ops *record_beneath_to_store_registers_ops;
125 static void (*record_beneath_to_store_registers) (struct target_ops *,
126 struct regcache *,
127 int regno);
128 static struct target_ops *record_beneath_to_xfer_partial_ops;
129 static LONGEST (*record_beneath_to_xfer_partial) (struct target_ops *ops,
130 enum target_object object,
131 const char *annex,
132 gdb_byte *readbuf,
133 const gdb_byte *writebuf,
134 ULONGEST offset,
135 LONGEST len);
136 static int (*record_beneath_to_insert_breakpoint) (struct gdbarch *,
137 struct bp_target_info *);
138 static int (*record_beneath_to_remove_breakpoint) (struct gdbarch *,
139 struct bp_target_info *);
140
141 /* Alloc and free functions for record_reg, record_mem, and record_end
142 entries. */
143
144 /* Alloc a record_reg record entry. */
145
146 static inline struct record_entry *
147 record_reg_alloc (struct regcache *regcache, int regnum)
148 {
149 struct record_entry *rec;
150 struct gdbarch *gdbarch = get_regcache_arch (regcache);
151
152 rec = (struct record_entry *) xcalloc (1, sizeof (struct record_entry));
153 rec->type = record_reg;
154 rec->u.reg.num = regnum;
155 rec->u.reg.len = register_size (gdbarch, regnum);
156 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
157 rec->u.reg.u.ptr = (gdb_byte *) xmalloc (rec->u.reg.len);
158
159 return rec;
160 }
161
162 /* Free a record_reg record entry. */
163
164 static inline void
165 record_reg_release (struct record_entry *rec)
166 {
167 gdb_assert (rec->type == record_reg);
168 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
169 xfree (rec->u.reg.u.ptr);
170 xfree (rec);
171 }
172
173 /* Alloc a record_mem record entry. */
174
175 static inline struct record_entry *
176 record_mem_alloc (CORE_ADDR addr, int len)
177 {
178 struct record_entry *rec;
179
180 rec = (struct record_entry *) xcalloc (1, sizeof (struct record_entry));
181 rec->type = record_mem;
182 rec->u.mem.addr = addr;
183 rec->u.mem.len = len;
184 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
185 rec->u.mem.u.ptr = (gdb_byte *) xmalloc (len);
186
187 return rec;
188 }
189
190 /* Free a record_mem record entry. */
191
192 static inline void
193 record_mem_release (struct record_entry *rec)
194 {
195 gdb_assert (rec->type == record_mem);
196 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
197 xfree (rec->u.mem.u.ptr);
198 xfree (rec);
199 }
200
201 /* Alloc a record_end record entry. */
202
203 static inline struct record_entry *
204 record_end_alloc (void)
205 {
206 struct record_entry *rec;
207
208 rec = (struct record_entry *) xcalloc (1, sizeof (struct record_entry));
209 rec->type = record_end;
210
211 return rec;
212 }
213
214 /* Free a record_end record entry. */
215
216 static inline void
217 record_end_release (struct record_entry *rec)
218 {
219 xfree (rec);
220 }
221
222 /* Free one record entry, any type.
223 Return entry->type, in case caller wants to know. */
224
225 static inline enum record_type
226 record_entry_release (struct record_entry *rec)
227 {
228 enum record_type type = rec->type;
229
230 switch (type) {
231 case record_reg:
232 record_reg_release (rec);
233 break;
234 case record_mem:
235 record_mem_release (rec);
236 break;
237 case record_end:
238 record_end_release (rec);
239 break;
240 }
241 return type;
242 }
243
244 /* Free all record entries in list pointed to by REC. */
245
246 static void
247 record_list_release (struct record_entry *rec)
248 {
249 if (!rec)
250 return;
251
252 while (rec->next)
253 rec = rec->next;
254
255 while (rec->prev)
256 {
257 rec = rec->prev;
258 record_entry_release (rec->next);
259 }
260
261 if (rec == &record_first)
262 {
263 record_insn_num = 0;
264 record_first.next = NULL;
265 }
266 else
267 record_entry_release (rec);
268 }
269
270 /* Free all record entries forward of the given list position. */
271
272 static void
273 record_list_release_following (struct record_entry *rec)
274 {
275 struct record_entry *tmp = rec->next;
276
277 rec->next = NULL;
278 while (tmp)
279 {
280 rec = tmp->next;
281 if (record_entry_release (tmp) == record_end)
282 record_insn_num--;
283 tmp = rec;
284 }
285 }
286
287 /* Delete the first instruction from the beginning of the log, to make
288 room for adding a new instruction at the end of the log.
289
290 Note -- this function does not modify record_insn_num. */
291
292 static void
293 record_list_release_first (void)
294 {
295 struct record_entry *tmp;
296
297 if (!record_first.next)
298 return;
299
300 /* Loop until a record_end. */
301 while (1)
302 {
303 /* Cut record_first.next out of the linked list. */
304 tmp = record_first.next;
305 record_first.next = tmp->next;
306 tmp->next->prev = &record_first;
307
308 /* tmp is now isolated, and can be deleted. */
309 if (record_entry_release (tmp) == record_end)
310 {
311 record_insn_num--;
312 break; /* End loop at first record_end. */
313 }
314
315 if (!record_first.next)
316 {
317 gdb_assert (record_insn_num == 1);
318 break; /* End loop when list is empty. */
319 }
320 }
321 }
322
323 /* Add a struct record_entry to record_arch_list. */
324
325 static void
326 record_arch_list_add (struct record_entry *rec)
327 {
328 if (record_debug > 1)
329 fprintf_unfiltered (gdb_stdlog,
330 "Process record: record_arch_list_add %s.\n",
331 host_address_to_string (rec));
332
333 if (record_arch_list_tail)
334 {
335 record_arch_list_tail->next = rec;
336 rec->prev = record_arch_list_tail;
337 record_arch_list_tail = rec;
338 }
339 else
340 {
341 record_arch_list_head = rec;
342 record_arch_list_tail = rec;
343 }
344 }
345
346 /* Return the value storage location of a record entry. */
347 static inline gdb_byte *
348 record_get_loc (struct record_entry *rec)
349 {
350 switch (rec->type) {
351 case record_mem:
352 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
353 return rec->u.mem.u.ptr;
354 else
355 return rec->u.mem.u.buf;
356 case record_reg:
357 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
358 return rec->u.reg.u.ptr;
359 else
360 return rec->u.reg.u.buf;
361 case record_end:
362 default:
363 gdb_assert (0);
364 return NULL;
365 }
366 }
367
368 /* Record the value of a register NUM to record_arch_list. */
369
370 int
371 record_arch_list_add_reg (struct regcache *regcache, int regnum)
372 {
373 struct record_entry *rec;
374
375 if (record_debug > 1)
376 fprintf_unfiltered (gdb_stdlog,
377 "Process record: add register num = %d to "
378 "record list.\n",
379 regnum);
380
381 rec = record_reg_alloc (regcache, regnum);
382
383 regcache_raw_read (regcache, regnum, record_get_loc (rec));
384
385 record_arch_list_add (rec);
386
387 return 0;
388 }
389
390 /* Record the value of a region of memory whose address is ADDR and
391 length is LEN to record_arch_list. */
392
393 int
394 record_arch_list_add_mem (CORE_ADDR addr, int len)
395 {
396 struct record_entry *rec;
397
398 if (record_debug > 1)
399 fprintf_unfiltered (gdb_stdlog,
400 "Process record: add mem addr = %s len = %d to "
401 "record list.\n",
402 paddress (target_gdbarch, addr), len);
403
404 if (!addr) /* FIXME: Why? Some arch must permit it... */
405 return 0;
406
407 rec = record_mem_alloc (addr, len);
408
409 if (target_read_memory (addr, record_get_loc (rec), len))
410 {
411 if (record_debug)
412 fprintf_unfiltered (gdb_stdlog,
413 "Process record: error reading memory at "
414 "addr = %s len = %d.\n",
415 paddress (target_gdbarch, addr), len);
416 record_mem_release (rec);
417 return -1;
418 }
419
420 record_arch_list_add (rec);
421
422 return 0;
423 }
424
425 /* Add a record_end type struct record_entry to record_arch_list. */
426
427 int
428 record_arch_list_add_end (void)
429 {
430 struct record_entry *rec;
431
432 if (record_debug > 1)
433 fprintf_unfiltered (gdb_stdlog,
434 "Process record: add end to arch list.\n");
435
436 rec = record_end_alloc ();
437 rec->u.end.sigval = TARGET_SIGNAL_0;
438
439 record_arch_list_add (rec);
440
441 return 0;
442 }
443
444 static void
445 record_check_insn_num (int set_terminal)
446 {
447 if (record_insn_max_num)
448 {
449 gdb_assert (record_insn_num <= record_insn_max_num);
450 if (record_insn_num == record_insn_max_num)
451 {
452 /* Ask user what to do. */
453 if (record_stop_at_limit)
454 {
455 int q;
456 if (set_terminal)
457 target_terminal_ours ();
458 q = yquery (_("Do you want to auto delete previous execution "
459 "log entries when record/replay buffer becomes "
460 "full (record stop-at-limit)?"));
461 if (set_terminal)
462 target_terminal_inferior ();
463 if (q)
464 record_stop_at_limit = 0;
465 else
466 error (_("Process record: inferior program stopped."));
467 }
468 }
469 }
470 }
471
472 /* Before inferior step (when GDB record the running message, inferior
473 only can step), GDB will call this function to record the values to
474 record_list. This function will call gdbarch_process_record to
475 record the running message of inferior and set them to
476 record_arch_list, and add it to record_list. */
477
478 static void
479 record_message_cleanups (void *ignore)
480 {
481 record_list_release (record_arch_list_tail);
482 }
483
484 struct record_message_args {
485 struct regcache *regcache;
486 enum target_signal signal;
487 };
488
489 static int
490 record_message (void *args)
491 {
492 int ret;
493 struct record_message_args *myargs = args;
494 struct gdbarch *gdbarch = get_regcache_arch (myargs->regcache);
495 struct cleanup *old_cleanups = make_cleanup (record_message_cleanups, 0);
496
497 record_arch_list_head = NULL;
498 record_arch_list_tail = NULL;
499
500 /* Check record_insn_num. */
501 record_check_insn_num (1);
502
503 /* If gdb sends a signal value to target_resume,
504 save it in the 'end' field of the previous instruction.
505
506 Maybe process record should record what really happened,
507 rather than what gdb pretends has happened.
508
509 So if Linux delivered the signal to the child process during
510 the record mode, we will record it and deliver it again in
511 the replay mode.
512
513 If user says "ignore this signal" during the record mode, then
514 it will be ignored again during the replay mode (no matter if
515 the user says something different, like "deliver this signal"
516 during the replay mode).
517
518 User should understand that nothing he does during the replay
519 mode will change the behavior of the child. If he tries,
520 then that is a user error.
521
522 But we should still deliver the signal to gdb during the replay,
523 if we delivered it during the recording. Therefore we should
524 record the signal during record_wait, not record_resume. */
525 if (record_list != &record_first) /* FIXME better way to check */
526 {
527 gdb_assert (record_list->type == record_end);
528 record_list->u.end.sigval = myargs->signal;
529 }
530
531 if (myargs->signal == TARGET_SIGNAL_0
532 || !gdbarch_process_record_signal_p (gdbarch))
533 ret = gdbarch_process_record (gdbarch,
534 myargs->regcache,
535 regcache_read_pc (myargs->regcache));
536 else
537 ret = gdbarch_process_record_signal (gdbarch,
538 myargs->regcache,
539 myargs->signal);
540
541 if (ret > 0)
542 error (_("Process record: inferior program stopped."));
543 if (ret < 0)
544 error (_("Process record: failed to record execution log."));
545
546 discard_cleanups (old_cleanups);
547
548 record_list->next = record_arch_list_head;
549 record_arch_list_head->prev = record_list;
550 record_list = record_arch_list_tail;
551
552 if (record_insn_num == record_insn_max_num && record_insn_max_num)
553 record_list_release_first ();
554 else
555 record_insn_num++;
556
557 return 1;
558 }
559
560 static int
561 do_record_message (struct regcache *regcache,
562 enum target_signal signal)
563 {
564 struct record_message_args args;
565
566 args.regcache = regcache;
567 args.signal = signal;
568 return catch_errors (record_message, &args, NULL, RETURN_MASK_ALL);
569 }
570
571 /* Set to 1 if record_store_registers and record_xfer_partial
572 doesn't need record. */
573
574 static int record_gdb_operation_disable = 0;
575
576 struct cleanup *
577 record_gdb_operation_disable_set (void)
578 {
579 struct cleanup *old_cleanups = NULL;
580
581 old_cleanups =
582 make_cleanup_restore_integer (&record_gdb_operation_disable);
583 record_gdb_operation_disable = 1;
584
585 return old_cleanups;
586 }
587
588 static void
589 record_open (char *name, int from_tty)
590 {
591 struct target_ops *t;
592
593 if (record_debug)
594 fprintf_unfiltered (gdb_stdlog, "Process record: record_open\n");
595
596 /* check exec */
597 if (!target_has_execution)
598 error (_("Process record: the program is not being run."));
599 if (non_stop)
600 error (_("Process record target can't debug inferior in non-stop mode "
601 "(non-stop)."));
602 if (target_async_permitted)
603 error (_("Process record target can't debug inferior in asynchronous "
604 "mode (target-async)."));
605
606 if (!gdbarch_process_record_p (target_gdbarch))
607 error (_("Process record: the current architecture doesn't support "
608 "record function."));
609
610 /* Check if record target is already running. */
611 if (current_target.to_stratum == record_stratum)
612 error (_("Process record target already running. Use \"record stop\" to "
613 "stop record target first."));
614
615 /*Reset the beneath function pointers. */
616 record_beneath_to_resume = NULL;
617 record_beneath_to_wait = NULL;
618 record_beneath_to_store_registers = NULL;
619 record_beneath_to_xfer_partial = NULL;
620 record_beneath_to_insert_breakpoint = NULL;
621 record_beneath_to_remove_breakpoint = NULL;
622
623 /* Set the beneath function pointers. */
624 for (t = current_target.beneath; t != NULL; t = t->beneath)
625 {
626 if (!record_beneath_to_resume)
627 {
628 record_beneath_to_resume = t->to_resume;
629 record_beneath_to_resume_ops = t;
630 }
631 if (!record_beneath_to_wait)
632 {
633 record_beneath_to_wait = t->to_wait;
634 record_beneath_to_wait_ops = t;
635 }
636 if (!record_beneath_to_store_registers)
637 {
638 record_beneath_to_store_registers = t->to_store_registers;
639 record_beneath_to_store_registers_ops = t;
640 }
641 if (!record_beneath_to_xfer_partial)
642 {
643 record_beneath_to_xfer_partial = t->to_xfer_partial;
644 record_beneath_to_xfer_partial_ops = t;
645 }
646 if (!record_beneath_to_insert_breakpoint)
647 record_beneath_to_insert_breakpoint = t->to_insert_breakpoint;
648 if (!record_beneath_to_remove_breakpoint)
649 record_beneath_to_remove_breakpoint = t->to_remove_breakpoint;
650 }
651 if (!record_beneath_to_resume)
652 error (_("Process record can't get to_resume."));
653 if (!record_beneath_to_wait)
654 error (_("Process record can't get to_wait."));
655 if (!record_beneath_to_store_registers)
656 error (_("Process record can't get to_store_registers."));
657 if (!record_beneath_to_xfer_partial)
658 error (_("Process record can't get to_xfer_partial."));
659 if (!record_beneath_to_insert_breakpoint)
660 error (_("Process record can't get to_insert_breakpoint."));
661 if (!record_beneath_to_remove_breakpoint)
662 error (_("Process record can't get to_remove_breakpoint."));
663
664 push_target (&record_ops);
665
666 /* Reset */
667 record_insn_num = 0;
668 record_list = &record_first;
669 record_list->next = NULL;
670 }
671
672 static void
673 record_close (int quitting)
674 {
675 if (record_debug)
676 fprintf_unfiltered (gdb_stdlog, "Process record: record_close\n");
677
678 record_list_release (record_list);
679 }
680
681 static int record_resume_step = 0;
682 static int record_resume_error;
683
684 static void
685 record_resume (struct target_ops *ops, ptid_t ptid, int step,
686 enum target_signal signal)
687 {
688 record_resume_step = step;
689
690 if (!RECORD_IS_REPLAY)
691 {
692 if (do_record_message (get_current_regcache (), signal))
693 {
694 record_resume_error = 0;
695 }
696 else
697 {
698 record_resume_error = 1;
699 return;
700 }
701 record_beneath_to_resume (record_beneath_to_resume_ops, ptid, 1,
702 signal);
703 }
704 }
705
706 static int record_get_sig = 0;
707
708 static void
709 record_sig_handler (int signo)
710 {
711 if (record_debug)
712 fprintf_unfiltered (gdb_stdlog, "Process record: get a signal\n");
713
714 /* It will break the running inferior in replay mode. */
715 record_resume_step = 1;
716
717 /* It will let record_wait set inferior status to get the signal
718 SIGINT. */
719 record_get_sig = 1;
720 }
721
722 static void
723 record_wait_cleanups (void *ignore)
724 {
725 if (execution_direction == EXEC_REVERSE)
726 {
727 if (record_list->next)
728 record_list = record_list->next;
729 }
730 else
731 record_list = record_list->prev;
732 }
733
734 /* In replay mode, this function examines the recorded log and
735 determines where to stop. */
736
737 static ptid_t
738 record_wait (struct target_ops *ops,
739 ptid_t ptid, struct target_waitstatus *status,
740 int options)
741 {
742 struct cleanup *set_cleanups = record_gdb_operation_disable_set ();
743
744 if (record_debug)
745 fprintf_unfiltered (gdb_stdlog,
746 "Process record: record_wait "
747 "record_resume_step = %d\n",
748 record_resume_step);
749
750 if (!RECORD_IS_REPLAY)
751 {
752 if (record_resume_error)
753 {
754 /* If record_resume get error, return directly. */
755 status->kind = TARGET_WAITKIND_STOPPED;
756 status->value.sig = TARGET_SIGNAL_ABRT;
757 return inferior_ptid;
758 }
759
760 if (record_resume_step)
761 {
762 /* This is a single step. */
763 return record_beneath_to_wait (record_beneath_to_wait_ops,
764 ptid, status, options);
765 }
766 else
767 {
768 /* This is not a single step. */
769 ptid_t ret;
770 CORE_ADDR tmp_pc;
771
772 while (1)
773 {
774 ret = record_beneath_to_wait (record_beneath_to_wait_ops,
775 ptid, status, options);
776
777 /* Is this a SIGTRAP? */
778 if (status->kind == TARGET_WAITKIND_STOPPED
779 && status->value.sig == TARGET_SIGNAL_TRAP)
780 {
781 /* Yes -- check if there is a breakpoint. */
782 registers_changed ();
783 tmp_pc = regcache_read_pc (get_current_regcache ());
784 if (breakpoint_inserted_here_p (tmp_pc))
785 {
786 /* There is a breakpoint. GDB will want to stop. */
787 CORE_ADDR decr_pc_after_break =
788 gdbarch_decr_pc_after_break
789 (get_regcache_arch (get_current_regcache ()));
790 if (decr_pc_after_break)
791 {
792 regcache_write_pc (get_thread_regcache (ret),
793 tmp_pc + decr_pc_after_break);
794 }
795 }
796 else
797 {
798 /* There is not a breakpoint, and gdb is not
799 stepping, therefore gdb will not stop.
800 Therefore we will not return to gdb.
801 Record the insn and resume. */
802 if (!do_record_message (get_current_regcache (),
803 TARGET_SIGNAL_0))
804 {
805 break;
806 }
807 record_beneath_to_resume (record_beneath_to_resume_ops,
808 ptid, 1,
809 TARGET_SIGNAL_0);
810 continue;
811 }
812 }
813
814 /* The inferior is broken by a breakpoint or a signal. */
815 break;
816 }
817
818 return ret;
819 }
820 }
821 else
822 {
823 struct regcache *regcache = get_current_regcache ();
824 struct gdbarch *gdbarch = get_regcache_arch (regcache);
825 int continue_flag = 1;
826 int first_record_end = 1;
827 struct cleanup *old_cleanups = make_cleanup (record_wait_cleanups, 0);
828 CORE_ADDR tmp_pc;
829
830 status->kind = TARGET_WAITKIND_STOPPED;
831
832 /* Check breakpoint when forward execute. */
833 if (execution_direction == EXEC_FORWARD)
834 {
835 tmp_pc = regcache_read_pc (regcache);
836 if (breakpoint_inserted_here_p (tmp_pc))
837 {
838 if (record_debug)
839 fprintf_unfiltered (gdb_stdlog,
840 "Process record: break at %s.\n",
841 paddress (gdbarch, tmp_pc));
842 if (gdbarch_decr_pc_after_break (gdbarch)
843 && !record_resume_step)
844 regcache_write_pc (regcache,
845 tmp_pc +
846 gdbarch_decr_pc_after_break (gdbarch));
847 goto replay_out;
848 }
849 }
850
851 record_get_sig = 0;
852 signal (SIGINT, record_sig_handler);
853 /* If GDB is in terminal_inferior mode, it will not get the signal.
854 And in GDB replay mode, GDB doesn't need to be in terminal_inferior
855 mode, because inferior will not executed.
856 Then set it to terminal_ours to make GDB get the signal. */
857 target_terminal_ours ();
858
859 /* In EXEC_FORWARD mode, record_list points to the tail of prev
860 instruction. */
861 if (execution_direction == EXEC_FORWARD && record_list->next)
862 record_list = record_list->next;
863
864 /* Loop over the record_list, looking for the next place to
865 stop. */
866 do
867 {
868 /* Check for beginning and end of log. */
869 if (execution_direction == EXEC_REVERSE
870 && record_list == &record_first)
871 {
872 /* Hit beginning of record log in reverse. */
873 status->kind = TARGET_WAITKIND_NO_HISTORY;
874 break;
875 }
876 if (execution_direction != EXEC_REVERSE && !record_list->next)
877 {
878 /* Hit end of record log going forward. */
879 status->kind = TARGET_WAITKIND_NO_HISTORY;
880 break;
881 }
882
883 /* Set ptid, register and memory according to record_list. */
884 if (record_list->type == record_reg)
885 {
886 /* reg */
887 gdb_byte reg[MAX_REGISTER_SIZE];
888 if (record_debug > 1)
889 fprintf_unfiltered (gdb_stdlog,
890 "Process record: record_reg %s to "
891 "inferior num = %d.\n",
892 host_address_to_string (record_list),
893 record_list->u.reg.num);
894 regcache_cooked_read (regcache, record_list->u.reg.num, reg);
895 regcache_cooked_write (regcache, record_list->u.reg.num,
896 record_get_loc (record_list));
897 memcpy (record_get_loc (record_list), reg,
898 record_list->u.reg.len);
899 }
900 else if (record_list->type == record_mem)
901 {
902 /* mem */
903 /* Nothing to do if the entry is flagged not_accessible. */
904 if (!record_list->u.mem.mem_entry_not_accessible)
905 {
906 gdb_byte *mem = alloca (record_list->u.mem.len);
907 if (record_debug > 1)
908 fprintf_unfiltered (gdb_stdlog,
909 "Process record: record_mem %s to "
910 "inferior addr = %s len = %d.\n",
911 host_address_to_string (record_list),
912 paddress (gdbarch,
913 record_list->u.mem.addr),
914 record_list->u.mem.len);
915
916 if (target_read_memory (record_list->u.mem.addr, mem,
917 record_list->u.mem.len))
918 {
919 if (execution_direction != EXEC_REVERSE)
920 error (_("Process record: error reading memory at "
921 "addr = %s len = %d."),
922 paddress (gdbarch, record_list->u.mem.addr),
923 record_list->u.mem.len);
924 else
925 /* Read failed --
926 flag entry as not_accessible. */
927 record_list->u.mem.mem_entry_not_accessible = 1;
928 }
929 else
930 {
931 if (target_write_memory (record_list->u.mem.addr,
932 record_get_loc (record_list),
933 record_list->u.mem.len))
934 {
935 if (execution_direction != EXEC_REVERSE)
936 error (_("Process record: error writing memory at "
937 "addr = %s len = %d."),
938 paddress (gdbarch, record_list->u.mem.addr),
939 record_list->u.mem.len);
940 else
941 /* Write failed --
942 flag entry as not_accessible. */
943 record_list->u.mem.mem_entry_not_accessible = 1;
944 }
945 else
946 {
947 memcpy (record_get_loc (record_list), mem,
948 record_list->u.mem.len);
949 }
950 }
951 }
952 }
953 else
954 {
955 if (record_debug > 1)
956 fprintf_unfiltered (gdb_stdlog,
957 "Process record: record_end %s to "
958 "inferior.\n",
959 host_address_to_string (record_list));
960
961 if (first_record_end && execution_direction == EXEC_REVERSE)
962 {
963 /* When reverse excute, the first record_end is the part of
964 current instruction. */
965 first_record_end = 0;
966 }
967 else
968 {
969 /* In EXEC_REVERSE mode, this is the record_end of prev
970 instruction.
971 In EXEC_FORWARD mode, this is the record_end of current
972 instruction. */
973 /* step */
974 if (record_resume_step)
975 {
976 if (record_debug > 1)
977 fprintf_unfiltered (gdb_stdlog,
978 "Process record: step.\n");
979 continue_flag = 0;
980 }
981
982 /* check breakpoint */
983 tmp_pc = regcache_read_pc (regcache);
984 if (breakpoint_inserted_here_p (tmp_pc))
985 {
986 if (record_debug)
987 fprintf_unfiltered (gdb_stdlog,
988 "Process record: break "
989 "at %s.\n",
990 paddress (gdbarch, tmp_pc));
991 if (gdbarch_decr_pc_after_break (gdbarch)
992 && execution_direction == EXEC_FORWARD
993 && !record_resume_step)
994 regcache_write_pc (regcache,
995 tmp_pc +
996 gdbarch_decr_pc_after_break (gdbarch));
997 continue_flag = 0;
998 }
999 /* Check target signal */
1000 if (record_list->u.end.sigval != TARGET_SIGNAL_0)
1001 /* FIXME: better way to check */
1002 continue_flag = 0;
1003 }
1004 }
1005
1006 if (continue_flag)
1007 {
1008 if (execution_direction == EXEC_REVERSE)
1009 {
1010 if (record_list->prev)
1011 record_list = record_list->prev;
1012 }
1013 else
1014 {
1015 if (record_list->next)
1016 record_list = record_list->next;
1017 }
1018 }
1019 }
1020 while (continue_flag);
1021
1022 signal (SIGINT, handle_sigint);
1023
1024 replay_out:
1025 if (record_get_sig)
1026 status->value.sig = TARGET_SIGNAL_INT;
1027 else if (record_list->u.end.sigval != TARGET_SIGNAL_0)
1028 /* FIXME: better way to check */
1029 status->value.sig = record_list->u.end.sigval;
1030 else
1031 status->value.sig = TARGET_SIGNAL_TRAP;
1032
1033 discard_cleanups (old_cleanups);
1034 }
1035
1036 do_cleanups (set_cleanups);
1037 return inferior_ptid;
1038 }
1039
1040 static void
1041 record_disconnect (struct target_ops *target, char *args, int from_tty)
1042 {
1043 if (record_debug)
1044 fprintf_unfiltered (gdb_stdlog, "Process record: record_disconnect\n");
1045
1046 unpush_target (&record_ops);
1047 target_disconnect (args, from_tty);
1048 }
1049
1050 static void
1051 record_detach (struct target_ops *ops, char *args, int from_tty)
1052 {
1053 if (record_debug)
1054 fprintf_unfiltered (gdb_stdlog, "Process record: record_detach\n");
1055
1056 unpush_target (&record_ops);
1057 target_detach (args, from_tty);
1058 }
1059
1060 static void
1061 record_mourn_inferior (struct target_ops *ops)
1062 {
1063 if (record_debug)
1064 fprintf_unfiltered (gdb_stdlog, "Process record: "
1065 "record_mourn_inferior\n");
1066
1067 unpush_target (&record_ops);
1068 target_mourn_inferior ();
1069 }
1070
1071 /* Close process record target before killing the inferior process. */
1072
1073 static void
1074 record_kill (struct target_ops *ops)
1075 {
1076 if (record_debug)
1077 fprintf_unfiltered (gdb_stdlog, "Process record: record_kill\n");
1078
1079 unpush_target (&record_ops);
1080 target_kill ();
1081 }
1082
1083 /* Record registers change (by user or by GDB) to list as an instruction. */
1084
1085 static void
1086 record_registers_change (struct regcache *regcache, int regnum)
1087 {
1088 /* Check record_insn_num. */
1089 record_check_insn_num (0);
1090
1091 record_arch_list_head = NULL;
1092 record_arch_list_tail = NULL;
1093
1094 if (regnum < 0)
1095 {
1096 int i;
1097 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
1098 {
1099 if (record_arch_list_add_reg (regcache, i))
1100 {
1101 record_list_release (record_arch_list_tail);
1102 error (_("Process record: failed to record execution log."));
1103 }
1104 }
1105 }
1106 else
1107 {
1108 if (record_arch_list_add_reg (regcache, regnum))
1109 {
1110 record_list_release (record_arch_list_tail);
1111 error (_("Process record: failed to record execution log."));
1112 }
1113 }
1114 if (record_arch_list_add_end ())
1115 {
1116 record_list_release (record_arch_list_tail);
1117 error (_("Process record: failed to record execution log."));
1118 }
1119 record_list->next = record_arch_list_head;
1120 record_arch_list_head->prev = record_list;
1121 record_list = record_arch_list_tail;
1122
1123 if (record_insn_num == record_insn_max_num && record_insn_max_num)
1124 record_list_release_first ();
1125 else
1126 record_insn_num++;
1127 }
1128
1129 static void
1130 record_store_registers (struct target_ops *ops, struct regcache *regcache,
1131 int regno)
1132 {
1133 if (!record_gdb_operation_disable)
1134 {
1135 if (RECORD_IS_REPLAY)
1136 {
1137 int n;
1138
1139 /* Let user choose if he wants to write register or not. */
1140 if (regno < 0)
1141 n =
1142 query (_("Because GDB is in replay mode, changing the "
1143 "value of a register will make the execution "
1144 "log unusable from this point onward. "
1145 "Change all registers?"));
1146 else
1147 n =
1148 query (_("Because GDB is in replay mode, changing the value "
1149 "of a register will make the execution log unusable "
1150 "from this point onward. Change register %s?"),
1151 gdbarch_register_name (get_regcache_arch (regcache),
1152 regno));
1153
1154 if (!n)
1155 {
1156 /* Invalidate the value of regcache that was set in function
1157 "regcache_raw_write". */
1158 if (regno < 0)
1159 {
1160 int i;
1161 for (i = 0;
1162 i < gdbarch_num_regs (get_regcache_arch (regcache));
1163 i++)
1164 regcache_invalidate (regcache, i);
1165 }
1166 else
1167 regcache_invalidate (regcache, regno);
1168
1169 error (_("Process record canceled the operation."));
1170 }
1171
1172 /* Destroy the record from here forward. */
1173 record_list_release_following (record_list);
1174 }
1175
1176 record_registers_change (regcache, regno);
1177 }
1178 record_beneath_to_store_registers (record_beneath_to_store_registers_ops,
1179 regcache, regno);
1180 }
1181
1182 /* Behavior is conditional on RECORD_IS_REPLAY.
1183 In replay mode, we cannot write memory unles we are willing to
1184 invalidate the record/replay log from this point forward. */
1185
1186 static LONGEST
1187 record_xfer_partial (struct target_ops *ops, enum target_object object,
1188 const char *annex, gdb_byte *readbuf,
1189 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
1190 {
1191 if (!record_gdb_operation_disable
1192 && (object == TARGET_OBJECT_MEMORY
1193 || object == TARGET_OBJECT_RAW_MEMORY) && writebuf)
1194 {
1195 if (RECORD_IS_REPLAY)
1196 {
1197 /* Let user choose if he wants to write memory or not. */
1198 if (!query (_("Because GDB is in replay mode, writing to memory "
1199 "will make the execution log unusable from this "
1200 "point onward. Write memory at address %s?"),
1201 paddress (target_gdbarch, offset)))
1202 error (_("Process record canceled the operation."));
1203
1204 /* Destroy the record from here forward. */
1205 record_list_release_following (record_list);
1206 }
1207
1208 /* Check record_insn_num */
1209 record_check_insn_num (0);
1210
1211 /* Record registers change to list as an instruction. */
1212 record_arch_list_head = NULL;
1213 record_arch_list_tail = NULL;
1214 if (record_arch_list_add_mem (offset, len))
1215 {
1216 record_list_release (record_arch_list_tail);
1217 if (record_debug)
1218 fprintf_unfiltered (gdb_stdlog,
1219 _("Process record: failed to record "
1220 "execution log."));
1221 return -1;
1222 }
1223 if (record_arch_list_add_end ())
1224 {
1225 record_list_release (record_arch_list_tail);
1226 if (record_debug)
1227 fprintf_unfiltered (gdb_stdlog,
1228 _("Process record: failed to record "
1229 "execution log."));
1230 return -1;
1231 }
1232 record_list->next = record_arch_list_head;
1233 record_arch_list_head->prev = record_list;
1234 record_list = record_arch_list_tail;
1235
1236 if (record_insn_num == record_insn_max_num && record_insn_max_num)
1237 record_list_release_first ();
1238 else
1239 record_insn_num++;
1240 }
1241
1242 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops,
1243 object, annex, readbuf, writebuf,
1244 offset, len);
1245 }
1246
1247 /* Behavior is conditional on RECORD_IS_REPLAY.
1248 We will not actually insert or remove breakpoints when replaying,
1249 nor when recording. */
1250
1251 static int
1252 record_insert_breakpoint (struct gdbarch *gdbarch,
1253 struct bp_target_info *bp_tgt)
1254 {
1255 if (!RECORD_IS_REPLAY)
1256 {
1257 struct cleanup *old_cleanups = record_gdb_operation_disable_set ();
1258 int ret = record_beneath_to_insert_breakpoint (gdbarch, bp_tgt);
1259
1260 do_cleanups (old_cleanups);
1261
1262 return ret;
1263 }
1264
1265 return 0;
1266 }
1267
1268 static int
1269 record_remove_breakpoint (struct gdbarch *gdbarch,
1270 struct bp_target_info *bp_tgt)
1271 {
1272 if (!RECORD_IS_REPLAY)
1273 {
1274 struct cleanup *old_cleanups = record_gdb_operation_disable_set ();
1275 int ret = record_beneath_to_remove_breakpoint (gdbarch, bp_tgt);
1276
1277 do_cleanups (old_cleanups);
1278
1279 return ret;
1280 }
1281
1282 return 0;
1283 }
1284
1285 static int
1286 record_can_execute_reverse (void)
1287 {
1288 return 1;
1289 }
1290
1291 static void
1292 init_record_ops (void)
1293 {
1294 record_ops.to_shortname = "record";
1295 record_ops.to_longname = "Process record and replay target";
1296 record_ops.to_doc =
1297 "Log program while executing and replay execution from log.";
1298 record_ops.to_open = record_open;
1299 record_ops.to_close = record_close;
1300 record_ops.to_resume = record_resume;
1301 record_ops.to_wait = record_wait;
1302 record_ops.to_disconnect = record_disconnect;
1303 record_ops.to_detach = record_detach;
1304 record_ops.to_mourn_inferior = record_mourn_inferior;
1305 record_ops.to_kill = record_kill;
1306 record_ops.to_create_inferior = find_default_create_inferior;
1307 record_ops.to_store_registers = record_store_registers;
1308 record_ops.to_xfer_partial = record_xfer_partial;
1309 record_ops.to_insert_breakpoint = record_insert_breakpoint;
1310 record_ops.to_remove_breakpoint = record_remove_breakpoint;
1311 record_ops.to_can_execute_reverse = record_can_execute_reverse;
1312 record_ops.to_stratum = record_stratum;
1313 record_ops.to_magic = OPS_MAGIC;
1314 }
1315
1316 static void
1317 show_record_debug (struct ui_file *file, int from_tty,
1318 struct cmd_list_element *c, const char *value)
1319 {
1320 fprintf_filtered (file, _("Debugging of process record target is %s.\n"),
1321 value);
1322 }
1323
1324 /* Alias for "target record". */
1325
1326 static void
1327 cmd_record_start (char *args, int from_tty)
1328 {
1329 execute_command ("target record", from_tty);
1330 }
1331
1332 /* Truncate the record log from the present point
1333 of replay until the end. */
1334
1335 static void
1336 cmd_record_delete (char *args, int from_tty)
1337 {
1338 if (current_target.to_stratum == record_stratum)
1339 {
1340 if (RECORD_IS_REPLAY)
1341 {
1342 if (!from_tty || query (_("Delete the log from this point forward "
1343 "and begin to record the running message "
1344 "at current PC?")))
1345 record_list_release_following (record_list);
1346 }
1347 else
1348 printf_unfiltered (_("Already at end of record list.\n"));
1349
1350 }
1351 else
1352 printf_unfiltered (_("Process record is not started.\n"));
1353 }
1354
1355 /* Implement the "stoprecord" command. */
1356
1357 static void
1358 cmd_record_stop (char *args, int from_tty)
1359 {
1360 if (current_target.to_stratum == record_stratum)
1361 {
1362 unpush_target (&record_ops);
1363 printf_unfiltered (_("Process record is stoped and all execution "
1364 "log is deleted.\n"));
1365 }
1366 else
1367 printf_unfiltered (_("Process record is not started.\n"));
1368 }
1369
1370 /* Set upper limit of record log size. */
1371
1372 static void
1373 set_record_insn_max_num (char *args, int from_tty, struct cmd_list_element *c)
1374 {
1375 if (record_insn_num > record_insn_max_num && record_insn_max_num)
1376 {
1377 /* Count down record_insn_num while releasing records from list. */
1378 while (record_insn_num > record_insn_max_num)
1379 {
1380 record_list_release_first ();
1381 record_insn_num--;
1382 }
1383 }
1384 }
1385
1386 /* Print the current index into the record log (number of insns recorded
1387 so far). */
1388
1389 static void
1390 show_record_insn_number (char *ignore, int from_tty)
1391 {
1392 printf_unfiltered (_("Record instruction number is %d.\n"),
1393 record_insn_num);
1394 }
1395
1396 static struct cmd_list_element *record_cmdlist, *set_record_cmdlist,
1397 *show_record_cmdlist, *info_record_cmdlist;
1398
1399 static void
1400 set_record_command (char *args, int from_tty)
1401 {
1402 printf_unfiltered (_("\
1403 \"set record\" must be followed by an apporpriate subcommand.\n"));
1404 help_list (set_record_cmdlist, "set record ", all_commands, gdb_stdout);
1405 }
1406
1407 static void
1408 show_record_command (char *args, int from_tty)
1409 {
1410 cmd_show_list (show_record_cmdlist, from_tty, "");
1411 }
1412
1413 static void
1414 info_record_command (char *args, int from_tty)
1415 {
1416 cmd_show_list (info_record_cmdlist, from_tty, "");
1417 }
1418
1419 void
1420 _initialize_record (void)
1421 {
1422 /* Init record_first. */
1423 record_first.prev = NULL;
1424 record_first.next = NULL;
1425 record_first.type = record_end;
1426
1427 init_record_ops ();
1428 add_target (&record_ops);
1429
1430 add_setshow_zinteger_cmd ("record", no_class, &record_debug,
1431 _("Set debugging of record/replay feature."),
1432 _("Show debugging of record/replay feature."),
1433 _("When enabled, debugging output for "
1434 "record/replay feature is displayed."),
1435 NULL, show_record_debug, &setdebuglist,
1436 &showdebuglist);
1437
1438 add_prefix_cmd ("record", class_obscure, cmd_record_start,
1439 _("Abbreviated form of \"target record\" command."),
1440 &record_cmdlist, "record ", 0, &cmdlist);
1441 add_com_alias ("rec", "record", class_obscure, 1);
1442 add_prefix_cmd ("record", class_support, set_record_command,
1443 _("Set record options"), &set_record_cmdlist,
1444 "set record ", 0, &setlist);
1445 add_alias_cmd ("rec", "record", class_obscure, 1, &setlist);
1446 add_prefix_cmd ("record", class_support, show_record_command,
1447 _("Show record options"), &show_record_cmdlist,
1448 "show record ", 0, &showlist);
1449 add_alias_cmd ("rec", "record", class_obscure, 1, &showlist);
1450 add_prefix_cmd ("record", class_support, info_record_command,
1451 _("Info record options"), &info_record_cmdlist,
1452 "info record ", 0, &infolist);
1453 add_alias_cmd ("rec", "record", class_obscure, 1, &infolist);
1454
1455
1456 add_cmd ("delete", class_obscure, cmd_record_delete,
1457 _("Delete the rest of execution log and start recording it anew."),
1458 &record_cmdlist);
1459 add_alias_cmd ("d", "delete", class_obscure, 1, &record_cmdlist);
1460 add_alias_cmd ("del", "delete", class_obscure, 1, &record_cmdlist);
1461
1462 add_cmd ("stop", class_obscure, cmd_record_stop,
1463 _("Stop the record/replay target."),
1464 &record_cmdlist);
1465 add_alias_cmd ("s", "stop", class_obscure, 1, &record_cmdlist);
1466
1467 /* Record instructions number limit command. */
1468 add_setshow_boolean_cmd ("stop-at-limit", no_class,
1469 &record_stop_at_limit, _("\
1470 Set whether record/replay stops when record/replay buffer becomes full."), _("\
1471 Show whether record/replay stops when record/replay buffer becomes full."), _("\
1472 Default is ON.\n\
1473 When ON, if the record/replay buffer becomes full, ask user what to do.\n\
1474 When OFF, if the record/replay buffer becomes full,\n\
1475 delete the oldest recorded instruction to make room for each new one."),
1476 NULL, NULL,
1477 &set_record_cmdlist, &show_record_cmdlist);
1478 add_setshow_uinteger_cmd ("insn-number-max", no_class,
1479 &record_insn_max_num,
1480 _("Set record/replay buffer limit."),
1481 _("Show record/replay buffer limit."), _("\
1482 Set the maximum number of instructions to be stored in the\n\
1483 record/replay buffer. Zero means unlimited. Default is 200000."),
1484 set_record_insn_max_num,
1485 NULL, &set_record_cmdlist, &show_record_cmdlist);
1486 add_cmd ("insn-number", class_obscure, show_record_insn_number,
1487 _("Show the current number of instructions in the "
1488 "record/replay buffer."), &info_record_cmdlist);
1489 }
This page took 0.091016 seconds and 3 git commands to generate.