1 /* Process record and replay target for GDB, the GNU debugger.
3 Copyright (C) 2008, 2009 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
23 #include "gdbthread.h"
24 #include "event-top.h"
25 #include "exceptions.h"
30 #define DEFAULT_RECORD_INSN_MAX_NUM 200000
32 #define RECORD_IS_REPLAY \
33 (record_list->next || execution_direction == EXEC_REVERSE)
35 /* These are the core structs of the process record functionality.
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
43 Each struct record_entry is linked to "record_list" by "prev" and
46 struct record_mem_entry
50 /* Set this flag if target memory for this entry
51 can no longer be accessed. */
52 int mem_entry_not_accessible;
56 gdb_byte buf[sizeof (gdb_byte *)];
60 struct record_reg_entry
67 gdb_byte buf[2 * sizeof (gdb_byte *)];
71 struct record_end_entry
73 enum target_signal sigval;
85 struct record_entry *prev;
86 struct record_entry *next;
87 enum record_type type;
91 struct record_reg_entry reg;
93 struct record_mem_entry mem;
95 struct record_end_entry end;
99 /* This is the debug switch for process record. */
100 int record_debug = 0;
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;
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;
113 /* The target_ops of process record. */
114 static struct target_ops record_ops;
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,
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 *,
124 static struct target_ops *record_beneath_to_store_registers_ops;
125 static void (*record_beneath_to_store_registers) (struct target_ops *,
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,
133 const gdb_byte *writebuf,
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 *);
141 /* Alloc and free functions for record_reg, record_mem, and record_end
144 /* Alloc a record_reg record entry. */
146 static inline struct record_entry *
147 record_reg_alloc (struct regcache *regcache, int regnum)
149 struct record_entry *rec;
150 struct gdbarch *gdbarch = get_regcache_arch (regcache);
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);
162 /* Free a record_reg record entry. */
165 record_reg_release (struct record_entry *rec)
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);
173 /* Alloc a record_mem record entry. */
175 static inline struct record_entry *
176 record_mem_alloc (CORE_ADDR addr, int len)
178 struct record_entry *rec;
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);
190 /* Free a record_mem record entry. */
193 record_mem_release (struct record_entry *rec)
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);
201 /* Alloc a record_end record entry. */
203 static inline struct record_entry *
204 record_end_alloc (void)
206 struct record_entry *rec;
208 rec = (struct record_entry *) xcalloc (1, sizeof (struct record_entry));
209 rec->type = record_end;
214 /* Free a record_end record entry. */
217 record_end_release (struct record_entry *rec)
222 /* Free one record entry, any type.
223 Return entry->type, in case caller wants to know. */
225 static inline enum record_type
226 record_entry_release (struct record_entry *rec)
228 enum record_type type = rec->type;
232 record_reg_release (rec);
235 record_mem_release (rec);
238 record_end_release (rec);
244 /* Free all record entries in list pointed to by REC. */
247 record_list_release (struct record_entry *rec)
258 record_entry_release (rec->next);
261 if (rec == &record_first)
264 record_first.next = NULL;
267 record_entry_release (rec);
270 /* Free all record entries forward of the given list position. */
273 record_list_release_following (struct record_entry *rec)
275 struct record_entry *tmp = rec->next;
281 if (record_entry_release (tmp) == record_end)
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.
290 Note -- this function does not modify record_insn_num. */
293 record_list_release_first (void)
295 struct record_entry *tmp;
297 if (!record_first.next)
300 /* Loop until a record_end. */
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;
308 /* tmp is now isolated, and can be deleted. */
309 if (record_entry_release (tmp) == record_end)
312 break; /* End loop at first record_end. */
315 if (!record_first.next)
317 gdb_assert (record_insn_num == 1);
318 break; /* End loop when list is empty. */
323 /* Add a struct record_entry to record_arch_list. */
326 record_arch_list_add (struct record_entry *rec)
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));
333 if (record_arch_list_tail)
335 record_arch_list_tail->next = rec;
336 rec->prev = record_arch_list_tail;
337 record_arch_list_tail = rec;
341 record_arch_list_head = rec;
342 record_arch_list_tail = rec;
346 /* Return the value storage location of a record entry. */
347 static inline gdb_byte *
348 record_get_loc (struct record_entry *rec)
352 if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
353 return rec->u.mem.u.ptr;
355 return rec->u.mem.u.buf;
357 if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
358 return rec->u.reg.u.ptr;
360 return rec->u.reg.u.buf;
368 /* Record the value of a register NUM to record_arch_list. */
371 record_arch_list_add_reg (struct regcache *regcache, int regnum)
373 struct record_entry *rec;
375 if (record_debug > 1)
376 fprintf_unfiltered (gdb_stdlog,
377 "Process record: add register num = %d to "
381 rec = record_reg_alloc (regcache, regnum);
383 regcache_raw_read (regcache, regnum, record_get_loc (rec));
385 record_arch_list_add (rec);
390 /* Record the value of a region of memory whose address is ADDR and
391 length is LEN to record_arch_list. */
394 record_arch_list_add_mem (CORE_ADDR addr, int len)
396 struct record_entry *rec;
398 if (record_debug > 1)
399 fprintf_unfiltered (gdb_stdlog,
400 "Process record: add mem addr = %s len = %d to "
402 paddress (target_gdbarch, addr), len);
404 if (!addr) /* FIXME: Why? Some arch must permit it... */
407 rec = record_mem_alloc (addr, len);
409 if (target_read_memory (addr, record_get_loc (rec), len))
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);
420 record_arch_list_add (rec);
425 /* Add a record_end type struct record_entry to record_arch_list. */
428 record_arch_list_add_end (void)
430 struct record_entry *rec;
432 if (record_debug > 1)
433 fprintf_unfiltered (gdb_stdlog,
434 "Process record: add end to arch list.\n");
436 rec = record_end_alloc ();
437 rec->u.end.sigval = TARGET_SIGNAL_0;
439 record_arch_list_add (rec);
445 record_check_insn_num (int set_terminal)
447 if (record_insn_max_num)
449 gdb_assert (record_insn_num <= record_insn_max_num);
450 if (record_insn_num == record_insn_max_num)
452 /* Ask user what to do. */
453 if (record_stop_at_limit)
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)?"));
462 target_terminal_inferior ();
464 record_stop_at_limit = 0;
466 error (_("Process record: inferior program stopped."));
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. */
479 record_message_cleanups (void *ignore)
481 record_list_release (record_arch_list_tail);
484 struct record_message_args {
485 struct regcache *regcache;
486 enum target_signal signal;
490 record_message (void *args)
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);
497 record_arch_list_head = NULL;
498 record_arch_list_tail = NULL;
500 /* Check record_insn_num. */
501 record_check_insn_num (1);
503 /* If gdb sends a signal value to target_resume,
504 save it in the 'end' field of the previous instruction.
506 Maybe process record should record what really happened,
507 rather than what gdb pretends has happened.
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
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).
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.
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 */
527 gdb_assert (record_list->type == record_end);
528 record_list->u.end.sigval = myargs->signal;
531 if (myargs->signal == TARGET_SIGNAL_0
532 || !gdbarch_process_record_signal_p (gdbarch))
533 ret = gdbarch_process_record (gdbarch,
535 regcache_read_pc (myargs->regcache));
537 ret = gdbarch_process_record_signal (gdbarch,
542 error (_("Process record: inferior program stopped."));
544 error (_("Process record: failed to record execution log."));
546 discard_cleanups (old_cleanups);
548 record_list->next = record_arch_list_head;
549 record_arch_list_head->prev = record_list;
550 record_list = record_arch_list_tail;
552 if (record_insn_num == record_insn_max_num && record_insn_max_num)
553 record_list_release_first ();
561 do_record_message (struct regcache *regcache,
562 enum target_signal signal)
564 struct record_message_args args;
566 args.regcache = regcache;
567 args.signal = signal;
568 return catch_errors (record_message, &args, NULL, RETURN_MASK_ALL);
571 /* Set to 1 if record_store_registers and record_xfer_partial
572 doesn't need record. */
574 static int record_gdb_operation_disable = 0;
577 record_gdb_operation_disable_set (void)
579 struct cleanup *old_cleanups = NULL;
582 make_cleanup_restore_integer (&record_gdb_operation_disable);
583 record_gdb_operation_disable = 1;
588 /* Execute one instruction from the record log. Each instruction in
589 the log will be represented by an arbitrary sequence of register
590 entries and memory entries, followed by an 'end' entry. */
593 record_exec_insn (struct regcache *regcache, struct gdbarch *gdbarch,
594 struct record_entry *entry)
598 case record_reg: /* reg */
600 gdb_byte reg[MAX_REGISTER_SIZE];
602 if (record_debug > 1)
603 fprintf_unfiltered (gdb_stdlog,
604 "Process record: record_reg %s to "
605 "inferior num = %d.\n",
606 host_address_to_string (entry),
609 regcache_cooked_read (regcache, entry->u.reg.num, reg);
610 regcache_cooked_write (regcache, entry->u.reg.num,
611 record_get_loc (entry));
612 memcpy (record_get_loc (entry), reg, entry->u.reg.len);
616 case record_mem: /* mem */
618 /* Nothing to do if the entry is flagged not_accessible. */
619 if (!entry->u.mem.mem_entry_not_accessible)
621 gdb_byte *mem = alloca (entry->u.mem.len);
623 if (record_debug > 1)
624 fprintf_unfiltered (gdb_stdlog,
625 "Process record: record_mem %s to "
626 "inferior addr = %s len = %d.\n",
627 host_address_to_string (entry),
628 paddress (gdbarch, entry->u.mem.addr),
631 if (target_read_memory (entry->u.mem.addr, mem, entry->u.mem.len))
633 entry->u.mem.mem_entry_not_accessible = 1;
635 warning (_("Process record: error reading memory at "
636 "addr = %s len = %d."),
637 paddress (gdbarch, entry->u.mem.addr),
642 if (target_write_memory (entry->u.mem.addr,
643 record_get_loc (entry),
646 entry->u.mem.mem_entry_not_accessible = 1;
648 warning (_("Process record: error writing memory at "
649 "addr = %s len = %d."),
650 paddress (gdbarch, entry->u.mem.addr),
654 memcpy (record_get_loc (entry), mem, entry->u.mem.len);
663 record_open (char *name, int from_tty)
665 struct target_ops *t;
668 fprintf_unfiltered (gdb_stdlog, "Process record: record_open\n");
671 if (!target_has_execution)
672 error (_("Process record: the program is not being run."));
674 error (_("Process record target can't debug inferior in non-stop mode "
676 if (target_async_permitted)
677 error (_("Process record target can't debug inferior in asynchronous "
678 "mode (target-async)."));
680 if (!gdbarch_process_record_p (target_gdbarch))
681 error (_("Process record: the current architecture doesn't support "
682 "record function."));
684 /* Check if record target is already running. */
685 if (current_target.to_stratum == record_stratum)
686 error (_("Process record target already running. Use \"record stop\" to "
687 "stop record target first."));
689 /*Reset the beneath function pointers. */
690 record_beneath_to_resume = NULL;
691 record_beneath_to_wait = NULL;
692 record_beneath_to_store_registers = NULL;
693 record_beneath_to_xfer_partial = NULL;
694 record_beneath_to_insert_breakpoint = NULL;
695 record_beneath_to_remove_breakpoint = NULL;
697 /* Set the beneath function pointers. */
698 for (t = current_target.beneath; t != NULL; t = t->beneath)
700 if (!record_beneath_to_resume)
702 record_beneath_to_resume = t->to_resume;
703 record_beneath_to_resume_ops = t;
705 if (!record_beneath_to_wait)
707 record_beneath_to_wait = t->to_wait;
708 record_beneath_to_wait_ops = t;
710 if (!record_beneath_to_store_registers)
712 record_beneath_to_store_registers = t->to_store_registers;
713 record_beneath_to_store_registers_ops = t;
715 if (!record_beneath_to_xfer_partial)
717 record_beneath_to_xfer_partial = t->to_xfer_partial;
718 record_beneath_to_xfer_partial_ops = t;
720 if (!record_beneath_to_insert_breakpoint)
721 record_beneath_to_insert_breakpoint = t->to_insert_breakpoint;
722 if (!record_beneath_to_remove_breakpoint)
723 record_beneath_to_remove_breakpoint = t->to_remove_breakpoint;
725 if (!record_beneath_to_resume)
726 error (_("Process record can't get to_resume."));
727 if (!record_beneath_to_wait)
728 error (_("Process record can't get to_wait."));
729 if (!record_beneath_to_store_registers)
730 error (_("Process record can't get to_store_registers."));
731 if (!record_beneath_to_xfer_partial)
732 error (_("Process record can't get to_xfer_partial."));
733 if (!record_beneath_to_insert_breakpoint)
734 error (_("Process record can't get to_insert_breakpoint."));
735 if (!record_beneath_to_remove_breakpoint)
736 error (_("Process record can't get to_remove_breakpoint."));
738 push_target (&record_ops);
742 record_list = &record_first;
743 record_list->next = NULL;
747 record_close (int quitting)
750 fprintf_unfiltered (gdb_stdlog, "Process record: record_close\n");
752 record_list_release (record_list);
755 static int record_resume_step = 0;
756 static int record_resume_error;
759 record_resume (struct target_ops *ops, ptid_t ptid, int step,
760 enum target_signal signal)
762 record_resume_step = step;
764 if (!RECORD_IS_REPLAY)
766 if (do_record_message (get_current_regcache (), signal))
768 record_resume_error = 0;
772 record_resume_error = 1;
775 record_beneath_to_resume (record_beneath_to_resume_ops, ptid, 1,
780 static int record_get_sig = 0;
783 record_sig_handler (int signo)
786 fprintf_unfiltered (gdb_stdlog, "Process record: get a signal\n");
788 /* It will break the running inferior in replay mode. */
789 record_resume_step = 1;
791 /* It will let record_wait set inferior status to get the signal
797 record_wait_cleanups (void *ignore)
799 if (execution_direction == EXEC_REVERSE)
801 if (record_list->next)
802 record_list = record_list->next;
805 record_list = record_list->prev;
808 /* In replay mode, this function examines the recorded log and
809 determines where to stop. */
812 record_wait (struct target_ops *ops,
813 ptid_t ptid, struct target_waitstatus *status,
816 struct cleanup *set_cleanups = record_gdb_operation_disable_set ();
819 fprintf_unfiltered (gdb_stdlog,
820 "Process record: record_wait "
821 "record_resume_step = %d\n",
824 if (!RECORD_IS_REPLAY)
826 if (record_resume_error)
828 /* If record_resume get error, return directly. */
829 status->kind = TARGET_WAITKIND_STOPPED;
830 status->value.sig = TARGET_SIGNAL_ABRT;
831 return inferior_ptid;
834 if (record_resume_step)
836 /* This is a single step. */
837 return record_beneath_to_wait (record_beneath_to_wait_ops,
838 ptid, status, options);
842 /* This is not a single step. */
848 ret = record_beneath_to_wait (record_beneath_to_wait_ops,
849 ptid, status, options);
851 /* Is this a SIGTRAP? */
852 if (status->kind == TARGET_WAITKIND_STOPPED
853 && status->value.sig == TARGET_SIGNAL_TRAP)
855 struct regcache *regcache;
857 /* Yes -- check if there is a breakpoint. */
858 registers_changed ();
859 regcache = get_current_regcache ();
860 tmp_pc = regcache_read_pc (regcache);
861 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache),
864 /* There is a breakpoint. GDB will want to stop. */
865 struct gdbarch *gdbarch = get_regcache_arch (regcache);
866 CORE_ADDR decr_pc_after_break
867 = gdbarch_decr_pc_after_break (gdbarch);
868 if (decr_pc_after_break)
869 regcache_write_pc (regcache,
870 tmp_pc + decr_pc_after_break);
874 /* There is not a breakpoint, and gdb is not
875 stepping, therefore gdb will not stop.
876 Therefore we will not return to gdb.
877 Record the insn and resume. */
878 if (!do_record_message (regcache, TARGET_SIGNAL_0))
881 record_beneath_to_resume (record_beneath_to_resume_ops,
888 /* The inferior is broken by a breakpoint or a signal. */
897 struct regcache *regcache = get_current_regcache ();
898 struct gdbarch *gdbarch = get_regcache_arch (regcache);
899 int continue_flag = 1;
900 int first_record_end = 1;
901 struct cleanup *old_cleanups = make_cleanup (record_wait_cleanups, 0);
904 status->kind = TARGET_WAITKIND_STOPPED;
906 /* Check breakpoint when forward execute. */
907 if (execution_direction == EXEC_FORWARD)
909 tmp_pc = regcache_read_pc (regcache);
910 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache),
914 fprintf_unfiltered (gdb_stdlog,
915 "Process record: break at %s.\n",
916 paddress (gdbarch, tmp_pc));
917 if (gdbarch_decr_pc_after_break (gdbarch)
918 && !record_resume_step)
919 regcache_write_pc (regcache,
921 gdbarch_decr_pc_after_break (gdbarch));
927 signal (SIGINT, record_sig_handler);
928 /* If GDB is in terminal_inferior mode, it will not get the signal.
929 And in GDB replay mode, GDB doesn't need to be in terminal_inferior
930 mode, because inferior will not executed.
931 Then set it to terminal_ours to make GDB get the signal. */
932 target_terminal_ours ();
934 /* In EXEC_FORWARD mode, record_list points to the tail of prev
936 if (execution_direction == EXEC_FORWARD && record_list->next)
937 record_list = record_list->next;
939 /* Loop over the record_list, looking for the next place to
943 /* Check for beginning and end of log. */
944 if (execution_direction == EXEC_REVERSE
945 && record_list == &record_first)
947 /* Hit beginning of record log in reverse. */
948 status->kind = TARGET_WAITKIND_NO_HISTORY;
951 if (execution_direction != EXEC_REVERSE && !record_list->next)
953 /* Hit end of record log going forward. */
954 status->kind = TARGET_WAITKIND_NO_HISTORY;
958 record_exec_insn (regcache, gdbarch, record_list);
960 if (record_list->type == record_end)
962 if (record_debug > 1)
963 fprintf_unfiltered (gdb_stdlog,
964 "Process record: record_end %s to "
966 host_address_to_string (record_list));
968 if (first_record_end && execution_direction == EXEC_REVERSE)
970 /* When reverse excute, the first record_end is the part of
971 current instruction. */
972 first_record_end = 0;
976 /* In EXEC_REVERSE mode, this is the record_end of prev
978 In EXEC_FORWARD mode, this is the record_end of current
981 if (record_resume_step)
983 if (record_debug > 1)
984 fprintf_unfiltered (gdb_stdlog,
985 "Process record: step.\n");
989 /* check breakpoint */
990 tmp_pc = regcache_read_pc (regcache);
991 if (breakpoint_inserted_here_p (get_regcache_aspace (regcache),
995 fprintf_unfiltered (gdb_stdlog,
996 "Process record: break "
998 paddress (gdbarch, tmp_pc));
999 if (gdbarch_decr_pc_after_break (gdbarch)
1000 && execution_direction == EXEC_FORWARD
1001 && !record_resume_step)
1002 regcache_write_pc (regcache,
1004 gdbarch_decr_pc_after_break (gdbarch));
1007 /* Check target signal */
1008 if (record_list->u.end.sigval != TARGET_SIGNAL_0)
1009 /* FIXME: better way to check */
1016 if (execution_direction == EXEC_REVERSE)
1018 if (record_list->prev)
1019 record_list = record_list->prev;
1023 if (record_list->next)
1024 record_list = record_list->next;
1028 while (continue_flag);
1030 signal (SIGINT, handle_sigint);
1034 status->value.sig = TARGET_SIGNAL_INT;
1035 else if (record_list->u.end.sigval != TARGET_SIGNAL_0)
1036 /* FIXME: better way to check */
1037 status->value.sig = record_list->u.end.sigval;
1039 status->value.sig = TARGET_SIGNAL_TRAP;
1041 discard_cleanups (old_cleanups);
1044 do_cleanups (set_cleanups);
1045 return inferior_ptid;
1049 record_disconnect (struct target_ops *target, char *args, int from_tty)
1052 fprintf_unfiltered (gdb_stdlog, "Process record: record_disconnect\n");
1054 unpush_target (&record_ops);
1055 target_disconnect (args, from_tty);
1059 record_detach (struct target_ops *ops, char *args, int from_tty)
1062 fprintf_unfiltered (gdb_stdlog, "Process record: record_detach\n");
1064 unpush_target (&record_ops);
1065 target_detach (args, from_tty);
1069 record_mourn_inferior (struct target_ops *ops)
1072 fprintf_unfiltered (gdb_stdlog, "Process record: "
1073 "record_mourn_inferior\n");
1075 unpush_target (&record_ops);
1076 target_mourn_inferior ();
1079 /* Close process record target before killing the inferior process. */
1082 record_kill (struct target_ops *ops)
1085 fprintf_unfiltered (gdb_stdlog, "Process record: record_kill\n");
1087 unpush_target (&record_ops);
1091 /* Record registers change (by user or by GDB) to list as an instruction. */
1094 record_registers_change (struct regcache *regcache, int regnum)
1096 /* Check record_insn_num. */
1097 record_check_insn_num (0);
1099 record_arch_list_head = NULL;
1100 record_arch_list_tail = NULL;
1105 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
1107 if (record_arch_list_add_reg (regcache, i))
1109 record_list_release (record_arch_list_tail);
1110 error (_("Process record: failed to record execution log."));
1116 if (record_arch_list_add_reg (regcache, regnum))
1118 record_list_release (record_arch_list_tail);
1119 error (_("Process record: failed to record execution log."));
1122 if (record_arch_list_add_end ())
1124 record_list_release (record_arch_list_tail);
1125 error (_("Process record: failed to record execution log."));
1127 record_list->next = record_arch_list_head;
1128 record_arch_list_head->prev = record_list;
1129 record_list = record_arch_list_tail;
1131 if (record_insn_num == record_insn_max_num && record_insn_max_num)
1132 record_list_release_first ();
1138 record_store_registers (struct target_ops *ops, struct regcache *regcache,
1141 if (!record_gdb_operation_disable)
1143 if (RECORD_IS_REPLAY)
1147 /* Let user choose if he wants to write register or not. */
1150 query (_("Because GDB is in replay mode, changing the "
1151 "value of a register will make the execution "
1152 "log unusable from this point onward. "
1153 "Change all registers?"));
1156 query (_("Because GDB is in replay mode, changing the value "
1157 "of a register will make the execution log unusable "
1158 "from this point onward. Change register %s?"),
1159 gdbarch_register_name (get_regcache_arch (regcache),
1164 /* Invalidate the value of regcache that was set in function
1165 "regcache_raw_write". */
1170 i < gdbarch_num_regs (get_regcache_arch (regcache));
1172 regcache_invalidate (regcache, i);
1175 regcache_invalidate (regcache, regno);
1177 error (_("Process record canceled the operation."));
1180 /* Destroy the record from here forward. */
1181 record_list_release_following (record_list);
1184 record_registers_change (regcache, regno);
1186 record_beneath_to_store_registers (record_beneath_to_store_registers_ops,
1190 /* Behavior is conditional on RECORD_IS_REPLAY.
1191 In replay mode, we cannot write memory unles we are willing to
1192 invalidate the record/replay log from this point forward. */
1195 record_xfer_partial (struct target_ops *ops, enum target_object object,
1196 const char *annex, gdb_byte *readbuf,
1197 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
1199 if (!record_gdb_operation_disable
1200 && (object == TARGET_OBJECT_MEMORY
1201 || object == TARGET_OBJECT_RAW_MEMORY) && writebuf)
1203 if (RECORD_IS_REPLAY)
1205 /* Let user choose if he wants to write memory or not. */
1206 if (!query (_("Because GDB is in replay mode, writing to memory "
1207 "will make the execution log unusable from this "
1208 "point onward. Write memory at address %s?"),
1209 paddress (target_gdbarch, offset)))
1210 error (_("Process record canceled the operation."));
1212 /* Destroy the record from here forward. */
1213 record_list_release_following (record_list);
1216 /* Check record_insn_num */
1217 record_check_insn_num (0);
1219 /* Record registers change to list as an instruction. */
1220 record_arch_list_head = NULL;
1221 record_arch_list_tail = NULL;
1222 if (record_arch_list_add_mem (offset, len))
1224 record_list_release (record_arch_list_tail);
1226 fprintf_unfiltered (gdb_stdlog,
1227 _("Process record: failed to record "
1231 if (record_arch_list_add_end ())
1233 record_list_release (record_arch_list_tail);
1235 fprintf_unfiltered (gdb_stdlog,
1236 _("Process record: failed to record "
1240 record_list->next = record_arch_list_head;
1241 record_arch_list_head->prev = record_list;
1242 record_list = record_arch_list_tail;
1244 if (record_insn_num == record_insn_max_num && record_insn_max_num)
1245 record_list_release_first ();
1250 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops,
1251 object, annex, readbuf, writebuf,
1255 /* Behavior is conditional on RECORD_IS_REPLAY.
1256 We will not actually insert or remove breakpoints when replaying,
1257 nor when recording. */
1260 record_insert_breakpoint (struct gdbarch *gdbarch,
1261 struct bp_target_info *bp_tgt)
1263 if (!RECORD_IS_REPLAY)
1265 struct cleanup *old_cleanups = record_gdb_operation_disable_set ();
1266 int ret = record_beneath_to_insert_breakpoint (gdbarch, bp_tgt);
1268 do_cleanups (old_cleanups);
1277 record_remove_breakpoint (struct gdbarch *gdbarch,
1278 struct bp_target_info *bp_tgt)
1280 if (!RECORD_IS_REPLAY)
1282 struct cleanup *old_cleanups = record_gdb_operation_disable_set ();
1283 int ret = record_beneath_to_remove_breakpoint (gdbarch, bp_tgt);
1285 do_cleanups (old_cleanups);
1294 record_can_execute_reverse (void)
1300 init_record_ops (void)
1302 record_ops.to_shortname = "record";
1303 record_ops.to_longname = "Process record and replay target";
1305 "Log program while executing and replay execution from log.";
1306 record_ops.to_open = record_open;
1307 record_ops.to_close = record_close;
1308 record_ops.to_resume = record_resume;
1309 record_ops.to_wait = record_wait;
1310 record_ops.to_disconnect = record_disconnect;
1311 record_ops.to_detach = record_detach;
1312 record_ops.to_mourn_inferior = record_mourn_inferior;
1313 record_ops.to_kill = record_kill;
1314 record_ops.to_create_inferior = find_default_create_inferior;
1315 record_ops.to_store_registers = record_store_registers;
1316 record_ops.to_xfer_partial = record_xfer_partial;
1317 record_ops.to_insert_breakpoint = record_insert_breakpoint;
1318 record_ops.to_remove_breakpoint = record_remove_breakpoint;
1319 record_ops.to_can_execute_reverse = record_can_execute_reverse;
1320 record_ops.to_stratum = record_stratum;
1321 record_ops.to_magic = OPS_MAGIC;
1325 show_record_debug (struct ui_file *file, int from_tty,
1326 struct cmd_list_element *c, const char *value)
1328 fprintf_filtered (file, _("Debugging of process record target is %s.\n"),
1332 /* Alias for "target record". */
1335 cmd_record_start (char *args, int from_tty)
1337 execute_command ("target record", from_tty);
1340 /* Truncate the record log from the present point
1341 of replay until the end. */
1344 cmd_record_delete (char *args, int from_tty)
1346 if (current_target.to_stratum == record_stratum)
1348 if (RECORD_IS_REPLAY)
1350 if (!from_tty || query (_("Delete the log from this point forward "
1351 "and begin to record the running message "
1353 record_list_release_following (record_list);
1356 printf_unfiltered (_("Already at end of record list.\n"));
1360 printf_unfiltered (_("Process record is not started.\n"));
1363 /* Implement the "stoprecord" command. */
1366 cmd_record_stop (char *args, int from_tty)
1368 if (current_target.to_stratum == record_stratum)
1370 unpush_target (&record_ops);
1371 printf_unfiltered (_("Process record is stoped and all execution "
1372 "log is deleted.\n"));
1375 printf_unfiltered (_("Process record is not started.\n"));
1378 /* Set upper limit of record log size. */
1381 set_record_insn_max_num (char *args, int from_tty, struct cmd_list_element *c)
1383 if (record_insn_num > record_insn_max_num && record_insn_max_num)
1385 /* Count down record_insn_num while releasing records from list. */
1386 while (record_insn_num > record_insn_max_num)
1388 record_list_release_first ();
1394 /* Print the current index into the record log (number of insns recorded
1398 show_record_insn_number (char *ignore, int from_tty)
1400 printf_unfiltered (_("Record instruction number is %d.\n"),
1404 static struct cmd_list_element *record_cmdlist, *set_record_cmdlist,
1405 *show_record_cmdlist, *info_record_cmdlist;
1408 set_record_command (char *args, int from_tty)
1410 printf_unfiltered (_("\
1411 \"set record\" must be followed by an apporpriate subcommand.\n"));
1412 help_list (set_record_cmdlist, "set record ", all_commands, gdb_stdout);
1416 show_record_command (char *args, int from_tty)
1418 cmd_show_list (show_record_cmdlist, from_tty, "");
1422 info_record_command (char *args, int from_tty)
1424 cmd_show_list (info_record_cmdlist, from_tty, "");
1428 _initialize_record (void)
1430 /* Init record_first. */
1431 record_first.prev = NULL;
1432 record_first.next = NULL;
1433 record_first.type = record_end;
1436 add_target (&record_ops);
1438 add_setshow_zinteger_cmd ("record", no_class, &record_debug,
1439 _("Set debugging of record/replay feature."),
1440 _("Show debugging of record/replay feature."),
1441 _("When enabled, debugging output for "
1442 "record/replay feature is displayed."),
1443 NULL, show_record_debug, &setdebuglist,
1446 add_prefix_cmd ("record", class_obscure, cmd_record_start,
1447 _("Abbreviated form of \"target record\" command."),
1448 &record_cmdlist, "record ", 0, &cmdlist);
1449 add_com_alias ("rec", "record", class_obscure, 1);
1450 add_prefix_cmd ("record", class_support, set_record_command,
1451 _("Set record options"), &set_record_cmdlist,
1452 "set record ", 0, &setlist);
1453 add_alias_cmd ("rec", "record", class_obscure, 1, &setlist);
1454 add_prefix_cmd ("record", class_support, show_record_command,
1455 _("Show record options"), &show_record_cmdlist,
1456 "show record ", 0, &showlist);
1457 add_alias_cmd ("rec", "record", class_obscure, 1, &showlist);
1458 add_prefix_cmd ("record", class_support, info_record_command,
1459 _("Info record options"), &info_record_cmdlist,
1460 "info record ", 0, &infolist);
1461 add_alias_cmd ("rec", "record", class_obscure, 1, &infolist);
1464 add_cmd ("delete", class_obscure, cmd_record_delete,
1465 _("Delete the rest of execution log and start recording it anew."),
1467 add_alias_cmd ("d", "delete", class_obscure, 1, &record_cmdlist);
1468 add_alias_cmd ("del", "delete", class_obscure, 1, &record_cmdlist);
1470 add_cmd ("stop", class_obscure, cmd_record_stop,
1471 _("Stop the record/replay target."),
1473 add_alias_cmd ("s", "stop", class_obscure, 1, &record_cmdlist);
1475 /* Record instructions number limit command. */
1476 add_setshow_boolean_cmd ("stop-at-limit", no_class,
1477 &record_stop_at_limit, _("\
1478 Set whether record/replay stops when record/replay buffer becomes full."), _("\
1479 Show whether record/replay stops when record/replay buffer becomes full."), _("\
1481 When ON, if the record/replay buffer becomes full, ask user what to do.\n\
1482 When OFF, if the record/replay buffer becomes full,\n\
1483 delete the oldest recorded instruction to make room for each new one."),
1485 &set_record_cmdlist, &show_record_cmdlist);
1486 add_setshow_uinteger_cmd ("insn-number-max", no_class,
1487 &record_insn_max_num,
1488 _("Set record/replay buffer limit."),
1489 _("Show record/replay buffer limit."), _("\
1490 Set the maximum number of instructions to be stored in the\n\
1491 record/replay buffer. Zero means unlimited. Default is 200000."),
1492 set_record_insn_max_num,
1493 NULL, &set_record_cmdlist, &show_record_cmdlist);
1494 add_cmd ("insn-number", class_obscure, show_record_insn_number,
1495 _("Show the current number of instructions in the "
1496 "record/replay buffer."), &info_record_cmdlist);