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