* hppa-tdep.c (_initialize_hppa_tdep): Add declaration.
[binutils-gdb.git] / gdb / record.c
blobdfd7487106fc8744eab2ae78ba08ee5e67eb3100
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/>. */
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"
28 #include <signal.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 struct of record function.
37 An 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 has a struct record_entry ("record_end") that points out this
40 is the last struct record_entry of this instruction.
42 Each struct record_entry is linked to "record_list" by "prev" and "next". */
44 struct record_reg_entry
46 int num;
47 gdb_byte *val;
50 struct record_mem_entry
52 CORE_ADDR addr;
53 int len;
54 gdb_byte *val;
57 enum record_type
59 record_end = 0,
60 record_reg,
61 record_mem
64 struct record_entry
66 struct record_entry *prev;
67 struct record_entry *next;
68 enum record_type type;
69 union
71 /* reg */
72 struct record_reg_entry reg;
73 /* mem */
74 struct record_mem_entry mem;
75 } u;
78 /* This is the debug switch for process record. */
79 int record_debug = 0;
81 /* These list is for execution log. */
82 static struct record_entry record_first;
83 static struct record_entry *record_list = &record_first;
84 static struct record_entry *record_arch_list_head = NULL;
85 static struct record_entry *record_arch_list_tail = NULL;
87 /* 1 ask user. 0 auto delete the last struct record_entry. */
88 static int record_stop_at_limit = 1;
89 static int record_insn_max_num = DEFAULT_RECORD_INSN_MAX_NUM;
90 static int record_insn_num = 0;
92 /* The target_ops of process record. */
93 static struct target_ops record_ops;
95 /* The beneath function pointers. */
96 static struct target_ops *record_beneath_to_resume_ops;
97 static void (*record_beneath_to_resume) (struct target_ops *, ptid_t, int,
98 enum target_signal);
99 static struct target_ops *record_beneath_to_wait_ops;
100 static ptid_t (*record_beneath_to_wait) (struct target_ops *, ptid_t,
101 struct target_waitstatus *,
102 int);
103 static struct target_ops *record_beneath_to_store_registers_ops;
104 static void (*record_beneath_to_store_registers) (struct target_ops *,
105 struct regcache *,
106 int regno);
107 static struct target_ops *record_beneath_to_xfer_partial_ops;
108 static LONGEST (*record_beneath_to_xfer_partial) (struct target_ops *ops,
109 enum target_object object,
110 const char *annex,
111 gdb_byte *readbuf,
112 const gdb_byte *writebuf,
113 ULONGEST offset,
114 LONGEST len);
115 static int (*record_beneath_to_insert_breakpoint) (struct bp_target_info *);
116 static int (*record_beneath_to_remove_breakpoint) (struct bp_target_info *);
118 static void
119 record_list_release (struct record_entry *rec)
121 struct record_entry *tmp;
123 if (!rec)
124 return;
126 while (rec->next)
128 rec = rec->next;
131 while (rec->prev)
133 tmp = rec;
134 rec = rec->prev;
135 if (tmp->type == record_reg)
136 xfree (tmp->u.reg.val);
137 else if (tmp->type == record_mem)
138 xfree (tmp->u.mem.val);
139 xfree (tmp);
142 if (rec != &record_first)
143 xfree (rec);
146 static void
147 record_list_release_next (void)
149 struct record_entry *rec = record_list;
150 struct record_entry *tmp = rec->next;
151 rec->next = NULL;
152 while (tmp)
154 rec = tmp->next;
155 if (tmp->type == record_reg)
156 record_insn_num--;
157 else if (tmp->type == record_reg)
158 xfree (tmp->u.reg.val);
159 else if (tmp->type == record_mem)
160 xfree (tmp->u.mem.val);
161 xfree (tmp);
162 tmp = rec;
166 static void
167 record_list_release_first (void)
169 struct record_entry *tmp = NULL;
170 enum record_type type;
172 if (!record_first.next)
173 return;
175 while (1)
177 type = record_first.next->type;
179 if (type == record_reg)
180 xfree (record_first.next->u.reg.val);
181 else if (type == record_mem)
182 xfree (record_first.next->u.mem.val);
183 tmp = record_first.next;
184 record_first.next = tmp->next;
185 xfree (tmp);
187 if (!record_first.next)
189 gdb_assert (record_insn_num == 1);
190 break;
193 record_first.next->prev = &record_first;
195 if (type == record_end)
196 break;
199 record_insn_num--;
202 /* Add a struct record_entry to record_arch_list. */
204 static void
205 record_arch_list_add (struct record_entry *rec)
207 if (record_debug > 1)
208 fprintf_unfiltered (gdb_stdlog,
209 "Process record: record_arch_list_add %s.\n",
210 host_address_to_string (rec));
212 if (record_arch_list_tail)
214 record_arch_list_tail->next = rec;
215 rec->prev = record_arch_list_tail;
216 record_arch_list_tail = rec;
218 else
220 record_arch_list_head = rec;
221 record_arch_list_tail = rec;
225 /* Record the value of a register NUM to record_arch_list. */
228 record_arch_list_add_reg (struct regcache *regcache, int num)
230 struct record_entry *rec;
232 if (record_debug > 1)
233 fprintf_unfiltered (gdb_stdlog,
234 "Process record: add register num = %d to "
235 "record list.\n",
236 num);
238 rec = (struct record_entry *) xmalloc (sizeof (struct record_entry));
239 rec->u.reg.val = (gdb_byte *) xmalloc (MAX_REGISTER_SIZE);
240 rec->prev = NULL;
241 rec->next = NULL;
242 rec->type = record_reg;
243 rec->u.reg.num = num;
245 regcache_raw_read (regcache, num, rec->u.reg.val);
247 record_arch_list_add (rec);
249 return 0;
252 /* Record the value of a region of memory whose address is ADDR and
253 length is LEN to record_arch_list. */
256 record_arch_list_add_mem (CORE_ADDR addr, int len)
258 struct record_entry *rec;
260 if (record_debug > 1)
261 fprintf_unfiltered (gdb_stdlog,
262 "Process record: add mem addr = 0x%s len = %d to "
263 "record list.\n",
264 paddr_nz (addr), len);
266 if (!addr)
267 return 0;
269 rec = (struct record_entry *) xmalloc (sizeof (struct record_entry));
270 rec->u.mem.val = (gdb_byte *) xmalloc (len);
271 rec->prev = NULL;
272 rec->next = NULL;
273 rec->type = record_mem;
274 rec->u.mem.addr = addr;
275 rec->u.mem.len = len;
277 if (target_read_memory (addr, rec->u.mem.val, len))
279 if (record_debug)
280 fprintf_unfiltered (gdb_stdlog,
281 "Process record: error reading memory at "
282 "addr = 0x%s len = %d.\n",
283 paddr_nz (addr), len);
284 xfree (rec->u.mem.val);
285 xfree (rec);
286 return -1;
289 record_arch_list_add (rec);
291 return 0;
294 /* Add a record_end type struct record_entry to record_arch_list. */
297 record_arch_list_add_end (void)
299 struct record_entry *rec;
301 if (record_debug > 1)
302 fprintf_unfiltered (gdb_stdlog,
303 "Process record: add end to arch list.\n");
305 rec = (struct record_entry *) xmalloc (sizeof (struct record_entry));
306 rec->prev = NULL;
307 rec->next = NULL;
308 rec->type = record_end;
310 record_arch_list_add (rec);
312 return 0;
315 static void
316 record_check_insn_num (int set_terminal)
318 if (record_insn_max_num)
320 gdb_assert (record_insn_num <= record_insn_max_num);
321 if (record_insn_num == record_insn_max_num)
323 /* Ask user what to do. */
324 if (record_stop_at_limit)
326 int q;
327 if (set_terminal)
328 target_terminal_ours ();
329 q = yquery (_("Do you want to auto delete previous execution "
330 "log entries when record/replay buffer becomes "
331 "full (record stop-at-limit)?"));
332 if (set_terminal)
333 target_terminal_inferior ();
334 if (q)
335 record_stop_at_limit = 0;
336 else
337 error (_("Process record: inferior program stopped."));
343 /* Before inferior step (when GDB record the running message, inferior
344 only can step), GDB will call this function to record the values to
345 record_list. This function will call gdbarch_process_record to
346 record the running message of inferior and set them to
347 record_arch_list, and add it to record_list. */
349 static void
350 record_message_cleanups (void *ignore)
352 record_list_release (record_arch_list_tail);
355 static int
356 record_message (void *args)
358 int ret;
359 struct regcache *regcache = args;
360 struct cleanup *old_cleanups = make_cleanup (record_message_cleanups, 0);
362 record_arch_list_head = NULL;
363 record_arch_list_tail = NULL;
365 /* Check record_insn_num. */
366 record_check_insn_num (1);
368 ret = gdbarch_process_record (get_regcache_arch (regcache),
369 regcache,
370 regcache_read_pc (regcache));
371 if (ret > 0)
372 error (_("Process record: inferior program stopped."));
373 if (ret < 0)
374 error (_("Process record: failed to record execution log."));
376 discard_cleanups (old_cleanups);
378 record_list->next = record_arch_list_head;
379 record_arch_list_head->prev = record_list;
380 record_list = record_arch_list_tail;
382 if (record_insn_num == record_insn_max_num && record_insn_max_num)
383 record_list_release_first ();
384 else
385 record_insn_num++;
387 return 1;
390 static int
391 do_record_message (struct regcache *regcache)
393 return catch_errors (record_message, regcache, NULL, RETURN_MASK_ALL);
396 /* Set to 1 if record_store_registers and record_xfer_partial
397 doesn't need record. */
399 static int record_gdb_operation_disable = 0;
401 struct cleanup *
402 record_gdb_operation_disable_set (void)
404 struct cleanup *old_cleanups = NULL;
406 old_cleanups =
407 make_cleanup_restore_integer (&record_gdb_operation_disable);
408 record_gdb_operation_disable = 1;
410 return old_cleanups;
413 static void
414 record_open (char *name, int from_tty)
416 struct target_ops *t;
418 if (record_debug)
419 fprintf_unfiltered (gdb_stdlog, "Process record: record_open\n");
421 /* check exec */
422 if (!target_has_execution)
423 error (_("Process record: the program is not being run."));
424 if (non_stop)
425 error (_("Process record target can't debug inferior in non-stop mode "
426 "(non-stop)."));
427 if (target_async_permitted)
428 error (_("Process record target can't debug inferior in asynchronous "
429 "mode (target-async)."));
431 if (!gdbarch_process_record_p (current_gdbarch))
432 error (_("Process record: the current architecture doesn't support "
433 "record function."));
435 /* Check if record target is already running. */
436 if (current_target.to_stratum == record_stratum)
438 if (!nquery
439 (_("Process record target already running, do you want to delete "
440 "the old record log?")))
441 return;
444 /*Reset the beneath function pointers. */
445 record_beneath_to_resume = NULL;
446 record_beneath_to_wait = NULL;
447 record_beneath_to_store_registers = NULL;
448 record_beneath_to_xfer_partial = NULL;
449 record_beneath_to_insert_breakpoint = NULL;
450 record_beneath_to_remove_breakpoint = NULL;
452 /* Set the beneath function pointers. */
453 for (t = current_target.beneath; t != NULL; t = t->beneath)
455 if (!record_beneath_to_resume)
457 record_beneath_to_resume = t->to_resume;
458 record_beneath_to_resume_ops = t;
460 if (!record_beneath_to_wait)
462 record_beneath_to_wait = t->to_wait;
463 record_beneath_to_wait_ops = t;
465 if (!record_beneath_to_store_registers)
467 record_beneath_to_store_registers = t->to_store_registers;
468 record_beneath_to_store_registers_ops = t;
470 if (!record_beneath_to_xfer_partial)
472 record_beneath_to_xfer_partial = t->to_xfer_partial;
473 record_beneath_to_xfer_partial_ops = t;
475 if (!record_beneath_to_insert_breakpoint)
476 record_beneath_to_insert_breakpoint = t->to_insert_breakpoint;
477 if (!record_beneath_to_remove_breakpoint)
478 record_beneath_to_remove_breakpoint = t->to_remove_breakpoint;
480 if (!record_beneath_to_resume)
481 error (_("Process record can't get to_resume."));
482 if (!record_beneath_to_wait)
483 error (_("Process record can't get to_wait."));
484 if (!record_beneath_to_store_registers)
485 error (_("Process record can't get to_store_registers."));
486 if (!record_beneath_to_xfer_partial)
487 error (_("Process record can't get to_xfer_partial."));
488 if (!record_beneath_to_insert_breakpoint)
489 error (_("Process record can't get to_insert_breakpoint."));
490 if (!record_beneath_to_remove_breakpoint)
491 error (_("Process record can't get to_remove_breakpoint."));
493 push_target (&record_ops);
495 /* Reset */
496 record_insn_num = 0;
497 record_list = &record_first;
498 record_list->next = NULL;
501 static void
502 record_close (int quitting)
504 if (record_debug)
505 fprintf_unfiltered (gdb_stdlog, "Process record: record_close\n");
507 record_list_release (record_list);
510 static int record_resume_step = 0;
511 static enum target_signal record_resume_siggnal;
512 static int record_resume_error;
514 static void
515 record_resume (struct target_ops *ops, ptid_t ptid, int step,
516 enum target_signal siggnal)
518 record_resume_step = step;
519 record_resume_siggnal = siggnal;
521 if (!RECORD_IS_REPLAY)
523 if (do_record_message (get_current_regcache ()))
525 record_resume_error = 0;
527 else
529 record_resume_error = 1;
530 return;
532 record_beneath_to_resume (record_beneath_to_resume_ops, ptid, 1,
533 siggnal);
537 static int record_get_sig = 0;
539 static void
540 record_sig_handler (int signo)
542 if (record_debug)
543 fprintf_unfiltered (gdb_stdlog, "Process record: get a signal\n");
545 /* It will break the running inferior in replay mode. */
546 record_resume_step = 1;
548 /* It will let record_wait set inferior status to get the signal
549 SIGINT. */
550 record_get_sig = 1;
553 static void
554 record_wait_cleanups (void *ignore)
556 if (execution_direction == EXEC_REVERSE)
558 if (record_list->next)
559 record_list = record_list->next;
561 else
562 record_list = record_list->prev;
565 /* In replay mode, this function examines the recorded log and
566 determines where to stop. */
568 static ptid_t
569 record_wait (struct target_ops *ops,
570 ptid_t ptid, struct target_waitstatus *status,
571 int options)
573 struct cleanup *set_cleanups = record_gdb_operation_disable_set ();
575 if (record_debug)
576 fprintf_unfiltered (gdb_stdlog,
577 "Process record: record_wait "
578 "record_resume_step = %d\n",
579 record_resume_step);
581 if (!RECORD_IS_REPLAY)
583 if (record_resume_error)
585 /* If record_resume get error, return directly. */
586 status->kind = TARGET_WAITKIND_STOPPED;
587 status->value.sig = TARGET_SIGNAL_ABRT;
588 return inferior_ptid;
591 if (record_resume_step)
593 /* This is a single step. */
594 return record_beneath_to_wait (record_beneath_to_wait_ops,
595 ptid, status, 0);
597 else
599 /* This is not a single step. */
600 ptid_t ret;
601 CORE_ADDR tmp_pc;
603 while (1)
605 ret = record_beneath_to_wait (record_beneath_to_wait_ops,
606 ptid, status, 0);
608 if (status->kind == TARGET_WAITKIND_STOPPED
609 && status->value.sig == TARGET_SIGNAL_TRAP)
611 /* Check if there is a breakpoint. */
612 registers_changed ();
613 tmp_pc = regcache_read_pc (get_current_regcache ());
614 if (breakpoint_inserted_here_p (tmp_pc))
616 /* There is a breakpoint. */
617 CORE_ADDR decr_pc_after_break =
618 gdbarch_decr_pc_after_break
619 (get_regcache_arch (get_current_regcache ()));
620 if (decr_pc_after_break)
622 regcache_write_pc (get_thread_regcache (ret),
623 tmp_pc + decr_pc_after_break);
626 else
628 /* There is not a breakpoint. */
629 if (!do_record_message (get_current_regcache ()))
631 break;
633 record_beneath_to_resume (record_beneath_to_resume_ops,
634 ptid, 1,
635 record_resume_siggnal);
636 continue;
640 /* The inferior is broken by a breakpoint or a signal. */
641 break;
644 return ret;
647 else
649 struct regcache *regcache = get_current_regcache ();
650 int continue_flag = 1;
651 int first_record_end = 1;
652 struct cleanup *old_cleanups = make_cleanup (record_wait_cleanups, 0);
653 CORE_ADDR tmp_pc;
655 status->kind = TARGET_WAITKIND_STOPPED;
657 /* Check breakpoint when forward execute. */
658 if (execution_direction == EXEC_FORWARD)
660 tmp_pc = regcache_read_pc (regcache);
661 if (breakpoint_inserted_here_p (tmp_pc))
663 if (record_debug)
664 fprintf_unfiltered (gdb_stdlog,
665 "Process record: break at 0x%s.\n",
666 paddr_nz (tmp_pc));
667 if (gdbarch_decr_pc_after_break (get_regcache_arch (regcache))
668 && !record_resume_step)
669 regcache_write_pc (regcache,
670 tmp_pc +
671 gdbarch_decr_pc_after_break
672 (get_regcache_arch (regcache)));
673 goto replay_out;
677 record_get_sig = 0;
678 signal (SIGINT, record_sig_handler);
679 /* If GDB is in terminal_inferior mode, it will not get the signal.
680 And in GDB replay mode, GDB doesn't need to be in terminal_inferior
681 mode, because inferior will not executed.
682 Then set it to terminal_ours to make GDB get the signal. */
683 target_terminal_ours ();
685 /* In EXEC_FORWARD mode, record_list points to the tail of prev
686 instruction. */
687 if (execution_direction == EXEC_FORWARD && record_list->next)
688 record_list = record_list->next;
690 /* Loop over the record_list, looking for the next place to
691 stop. */
694 /* Check for beginning and end of log. */
695 if (execution_direction == EXEC_REVERSE
696 && record_list == &record_first)
698 /* Hit beginning of record log in reverse. */
699 status->kind = TARGET_WAITKIND_NO_HISTORY;
700 break;
702 if (execution_direction != EXEC_REVERSE && !record_list->next)
704 /* Hit end of record log going forward. */
705 status->kind = TARGET_WAITKIND_NO_HISTORY;
706 break;
709 /* Set ptid, register and memory according to record_list. */
710 if (record_list->type == record_reg)
712 /* reg */
713 gdb_byte reg[MAX_REGISTER_SIZE];
714 if (record_debug > 1)
715 fprintf_unfiltered (gdb_stdlog,
716 "Process record: record_reg %s to "
717 "inferior num = %d.\n",
718 host_address_to_string (record_list),
719 record_list->u.reg.num);
720 regcache_cooked_read (regcache, record_list->u.reg.num, reg);
721 regcache_cooked_write (regcache, record_list->u.reg.num,
722 record_list->u.reg.val);
723 memcpy (record_list->u.reg.val, reg, MAX_REGISTER_SIZE);
725 else if (record_list->type == record_mem)
727 /* mem */
728 gdb_byte *mem = alloca (record_list->u.mem.len);
729 if (record_debug > 1)
730 fprintf_unfiltered (gdb_stdlog,
731 "Process record: record_mem %s to "
732 "inferior addr = 0x%s len = %d.\n",
733 host_address_to_string (record_list),
734 paddr_nz (record_list->u.mem.addr),
735 record_list->u.mem.len);
737 if (target_read_memory
738 (record_list->u.mem.addr, mem, record_list->u.mem.len))
739 error (_("Process record: error reading memory at "
740 "addr = 0x%s len = %d."),
741 paddr_nz (record_list->u.mem.addr),
742 record_list->u.mem.len);
744 if (target_write_memory
745 (record_list->u.mem.addr, record_list->u.mem.val,
746 record_list->u.mem.len))
747 error (_
748 ("Process record: error writing memory at "
749 "addr = 0x%s len = %d."),
750 paddr_nz (record_list->u.mem.addr),
751 record_list->u.mem.len);
753 memcpy (record_list->u.mem.val, mem, record_list->u.mem.len);
755 else
757 if (record_debug > 1)
758 fprintf_unfiltered (gdb_stdlog,
759 "Process record: record_end %s to "
760 "inferior.\n",
761 host_address_to_string (record_list));
763 if (first_record_end && execution_direction == EXEC_REVERSE)
765 /* When reverse excute, the first record_end is the part of
766 current instruction. */
767 first_record_end = 0;
769 else
771 /* In EXEC_REVERSE mode, this is the record_end of prev
772 instruction.
773 In EXEC_FORWARD mode, this is the record_end of current
774 instruction. */
775 /* step */
776 if (record_resume_step)
778 if (record_debug > 1)
779 fprintf_unfiltered (gdb_stdlog,
780 "Process record: step.\n");
781 continue_flag = 0;
784 /* check breakpoint */
785 tmp_pc = regcache_read_pc (regcache);
786 if (breakpoint_inserted_here_p (tmp_pc))
788 if (record_debug)
789 fprintf_unfiltered (gdb_stdlog,
790 "Process record: break "
791 "at 0x%s.\n",
792 paddr_nz (tmp_pc));
793 if (gdbarch_decr_pc_after_break (get_regcache_arch (regcache))
794 && execution_direction == EXEC_FORWARD
795 && !record_resume_step)
796 regcache_write_pc (regcache,
797 tmp_pc +
798 gdbarch_decr_pc_after_break
799 (get_regcache_arch (regcache)));
800 continue_flag = 0;
805 if (continue_flag)
807 if (execution_direction == EXEC_REVERSE)
809 if (record_list->prev)
810 record_list = record_list->prev;
812 else
814 if (record_list->next)
815 record_list = record_list->next;
819 while (continue_flag);
821 signal (SIGINT, handle_sigint);
823 replay_out:
824 if (record_get_sig)
825 status->value.sig = TARGET_SIGNAL_INT;
826 else
827 status->value.sig = TARGET_SIGNAL_TRAP;
829 discard_cleanups (old_cleanups);
832 do_cleanups (set_cleanups);
833 return inferior_ptid;
836 static void
837 record_disconnect (struct target_ops *target, char *args, int from_tty)
839 if (record_debug)
840 fprintf_unfiltered (gdb_stdlog, "Process record: record_disconnect\n");
842 unpush_target (&record_ops);
843 target_disconnect (args, from_tty);
846 static void
847 record_detach (struct target_ops *ops, char *args, int from_tty)
849 if (record_debug)
850 fprintf_unfiltered (gdb_stdlog, "Process record: record_detach\n");
852 unpush_target (&record_ops);
853 target_detach (args, from_tty);
856 static void
857 record_mourn_inferior (struct target_ops *ops)
859 if (record_debug)
860 fprintf_unfiltered (gdb_stdlog, "Process record: "
861 "record_mourn_inferior\n");
863 unpush_target (&record_ops);
864 target_mourn_inferior ();
867 /* Close process record target before killing the inferior process. */
869 static void
870 record_kill (struct target_ops *ops)
872 if (record_debug)
873 fprintf_unfiltered (gdb_stdlog, "Process record: record_kill\n");
875 unpush_target (&record_ops);
876 target_kill ();
879 /* Record registers change (by user or by GDB) to list as an instruction. */
881 static void
882 record_registers_change (struct regcache *regcache, int regnum)
884 /* Check record_insn_num. */
885 record_check_insn_num (0);
887 record_arch_list_head = NULL;
888 record_arch_list_tail = NULL;
890 if (regnum < 0)
892 int i;
893 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
895 if (record_arch_list_add_reg (regcache, i))
897 record_list_release (record_arch_list_tail);
898 error (_("Process record: failed to record execution log."));
902 else
904 if (record_arch_list_add_reg (regcache, regnum))
906 record_list_release (record_arch_list_tail);
907 error (_("Process record: failed to record execution log."));
910 if (record_arch_list_add_end ())
912 record_list_release (record_arch_list_tail);
913 error (_("Process record: failed to record execution log."));
915 record_list->next = record_arch_list_head;
916 record_arch_list_head->prev = record_list;
917 record_list = record_arch_list_tail;
919 if (record_insn_num == record_insn_max_num && record_insn_max_num)
920 record_list_release_first ();
921 else
922 record_insn_num++;
925 static void
926 record_store_registers (struct target_ops *ops, struct regcache *regcache,
927 int regno)
929 if (!record_gdb_operation_disable)
931 if (RECORD_IS_REPLAY)
933 int n;
934 struct cleanup *old_cleanups;
936 /* Let user choose if he wants to write register or not. */
937 if (regno < 0)
939 nquery (_("Because GDB is in replay mode, changing the "
940 "value of a register will make the execution "
941 "log unusable from this point onward. "
942 "Change all registers?"));
943 else
945 nquery (_("Because GDB is in replay mode, changing the value "
946 "of a register will make the execution log unusable "
947 "from this point onward. Change register %s?"),
948 gdbarch_register_name (get_regcache_arch (regcache),
949 regno));
951 if (!n)
953 /* Invalidate the value of regcache that was set in function
954 "regcache_raw_write". */
955 if (regno < 0)
957 int i;
958 for (i = 0;
959 i < gdbarch_num_regs (get_regcache_arch (regcache));
960 i++)
961 regcache_invalidate (regcache, i);
963 else
964 regcache_invalidate (regcache, regno);
966 error (_("Process record canceled the operation."));
969 /* Destroy the record from here forward. */
970 record_list_release_next ();
973 record_registers_change (regcache, regno);
975 record_beneath_to_store_registers (record_beneath_to_store_registers_ops,
976 regcache, regno);
979 /* Behavior is conditional on RECORD_IS_REPLAY.
980 In replay mode, we cannot write memory unles we are willing to
981 invalidate the record/replay log from this point forward. */
983 static LONGEST
984 record_xfer_partial (struct target_ops *ops, enum target_object object,
985 const char *annex, gdb_byte *readbuf,
986 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
988 if (!record_gdb_operation_disable
989 && (object == TARGET_OBJECT_MEMORY
990 || object == TARGET_OBJECT_RAW_MEMORY) && writebuf)
992 if (RECORD_IS_REPLAY)
994 /* Let user choose if he wants to write memory or not. */
995 if (!nquery (_("Because GDB is in replay mode, writing to memory "
996 "will make the execution log unusable from this "
997 "point onward. Write memory at address 0x%s?"),
998 paddr_nz (offset)))
999 return -1;
1001 /* Destroy the record from here forward. */
1002 record_list_release_next ();
1005 /* Check record_insn_num */
1006 record_check_insn_num (0);
1008 /* Record registers change to list as an instruction. */
1009 record_arch_list_head = NULL;
1010 record_arch_list_tail = NULL;
1011 if (record_arch_list_add_mem (offset, len))
1013 record_list_release (record_arch_list_tail);
1014 if (record_debug)
1015 fprintf_unfiltered (gdb_stdlog,
1016 _("Process record: failed to record "
1017 "execution log."));
1018 return -1;
1020 if (record_arch_list_add_end ())
1022 record_list_release (record_arch_list_tail);
1023 if (record_debug)
1024 fprintf_unfiltered (gdb_stdlog,
1025 _("Process record: failed to record "
1026 "execution log."));
1027 return -1;
1029 record_list->next = record_arch_list_head;
1030 record_arch_list_head->prev = record_list;
1031 record_list = record_arch_list_tail;
1033 if (record_insn_num == record_insn_max_num && record_insn_max_num)
1034 record_list_release_first ();
1035 else
1036 record_insn_num++;
1039 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops,
1040 object, annex, readbuf, writebuf,
1041 offset, len);
1044 /* Behavior is conditional on RECORD_IS_REPLAY.
1045 We will not actually insert or remove breakpoints when replaying,
1046 nor when recording. */
1048 static int
1049 record_insert_breakpoint (struct bp_target_info *bp_tgt)
1051 if (!RECORD_IS_REPLAY)
1053 struct cleanup *old_cleanups = record_gdb_operation_disable_set ();
1054 int ret = record_beneath_to_insert_breakpoint (bp_tgt);
1056 do_cleanups (old_cleanups);
1058 return ret;
1061 return 0;
1064 static int
1065 record_remove_breakpoint (struct bp_target_info *bp_tgt)
1067 if (!RECORD_IS_REPLAY)
1069 struct cleanup *old_cleanups = record_gdb_operation_disable_set ();
1070 int ret = record_beneath_to_remove_breakpoint (bp_tgt);
1072 do_cleanups (old_cleanups);
1074 return ret;
1077 return 0;
1080 static int
1081 record_can_execute_reverse (void)
1083 return 1;
1086 static void
1087 init_record_ops (void)
1089 record_ops.to_shortname = "record";
1090 record_ops.to_longname = "Process record and replay target";
1091 record_ops.to_doc =
1092 "Log program while executing and replay execution from log.";
1093 record_ops.to_open = record_open;
1094 record_ops.to_close = record_close;
1095 record_ops.to_resume = record_resume;
1096 record_ops.to_wait = record_wait;
1097 record_ops.to_disconnect = record_disconnect;
1098 record_ops.to_detach = record_detach;
1099 record_ops.to_mourn_inferior = record_mourn_inferior;
1100 record_ops.to_kill = record_kill;
1101 record_ops.to_create_inferior = find_default_create_inferior;
1102 record_ops.to_store_registers = record_store_registers;
1103 record_ops.to_xfer_partial = record_xfer_partial;
1104 record_ops.to_insert_breakpoint = record_insert_breakpoint;
1105 record_ops.to_remove_breakpoint = record_remove_breakpoint;
1106 record_ops.to_can_execute_reverse = record_can_execute_reverse;
1107 record_ops.to_stratum = record_stratum;
1108 record_ops.to_magic = OPS_MAGIC;
1111 static void
1112 show_record_debug (struct ui_file *file, int from_tty,
1113 struct cmd_list_element *c, const char *value)
1115 fprintf_filtered (file, _("Debugging of process record target is %s.\n"),
1116 value);
1119 /* Alias for "target record". */
1121 static void
1122 cmd_record_start (char *args, int from_tty)
1124 execute_command ("target record", from_tty);
1127 /* Truncate the record log from the present point
1128 of replay until the end. */
1130 static void
1131 cmd_record_delete (char *args, int from_tty)
1133 if (current_target.to_stratum == record_stratum)
1135 if (RECORD_IS_REPLAY)
1137 if (!from_tty || query (_("Delete the log from this point forward "
1138 "and begin to record the running message "
1139 "at current PC?")))
1140 record_list_release_next ();
1142 else
1143 printf_unfiltered (_("Already at end of record list.\n"));
1146 else
1147 printf_unfiltered (_("Process record is not started.\n"));
1150 /* Implement the "stoprecord" command. */
1152 static void
1153 cmd_record_stop (char *args, int from_tty)
1155 if (current_target.to_stratum == record_stratum)
1157 if (!record_list || !from_tty || query (_("Delete recorded log and "
1158 "stop recording?")))
1159 unpush_target (&record_ops);
1161 else
1162 printf_unfiltered (_("Process record is not started.\n"));
1165 /* Set upper limit of record log size. */
1167 static void
1168 set_record_insn_max_num (char *args, int from_tty, struct cmd_list_element *c)
1170 if (record_insn_num > record_insn_max_num && record_insn_max_num)
1172 printf_unfiltered (_("Record instructions number is bigger than "
1173 "record instructions max number. Auto delete "
1174 "the first ones?\n"));
1176 while (record_insn_num > record_insn_max_num)
1177 record_list_release_first ();
1181 /* Print the current index into the record log (number of insns recorded
1182 so far). */
1184 static void
1185 show_record_insn_number (char *ignore, int from_tty)
1187 printf_unfiltered (_("Record instruction number is %d.\n"),
1188 record_insn_num);
1191 static struct cmd_list_element *record_cmdlist, *set_record_cmdlist,
1192 *show_record_cmdlist, *info_record_cmdlist;
1194 static void
1195 set_record_command (char *args, int from_tty)
1197 printf_unfiltered (_("\
1198 \"set record\" must be followed by an apporpriate subcommand.\n"));
1199 help_list (set_record_cmdlist, "set record ", all_commands, gdb_stdout);
1202 static void
1203 show_record_command (char *args, int from_tty)
1205 cmd_show_list (show_record_cmdlist, from_tty, "");
1208 static void
1209 info_record_command (char *args, int from_tty)
1211 cmd_show_list (info_record_cmdlist, from_tty, "");
1214 void
1215 _initialize_record (void)
1217 /* Init record_first. */
1218 record_first.prev = NULL;
1219 record_first.next = NULL;
1220 record_first.type = record_end;
1222 init_record_ops ();
1223 add_target (&record_ops);
1225 add_setshow_zinteger_cmd ("record", no_class, &record_debug,
1226 _("Set debugging of record/replay feature."),
1227 _("Show debugging of record/replay feature."),
1228 _("When enabled, debugging output for "
1229 "record/replay feature is displayed."),
1230 NULL, show_record_debug, &setdebuglist,
1231 &showdebuglist);
1233 add_prefix_cmd ("record", class_obscure, cmd_record_start,
1234 _("Abbreviated form of \"target record\" command."),
1235 &record_cmdlist, "record ", 0, &cmdlist);
1236 add_com_alias ("rec", "record", class_obscure, 1);
1237 add_prefix_cmd ("record", class_support, set_record_command,
1238 _("Set record options"), &set_record_cmdlist,
1239 "set record ", 0, &setlist);
1240 add_alias_cmd ("rec", "record", class_obscure, 1, &setlist);
1241 add_prefix_cmd ("record", class_support, show_record_command,
1242 _("Show record options"), &show_record_cmdlist,
1243 "show record ", 0, &showlist);
1244 add_alias_cmd ("rec", "record", class_obscure, 1, &showlist);
1245 add_prefix_cmd ("record", class_support, info_record_command,
1246 _("Info record options"), &info_record_cmdlist,
1247 "info record ", 0, &infolist);
1248 add_alias_cmd ("rec", "record", class_obscure, 1, &infolist);
1251 add_cmd ("delete", class_obscure, cmd_record_delete,
1252 _("Delete the rest of execution log and start recording it anew."),
1253 &record_cmdlist);
1254 add_alias_cmd ("d", "delete", class_obscure, 1, &record_cmdlist);
1255 add_alias_cmd ("del", "delete", class_obscure, 1, &record_cmdlist);
1257 add_cmd ("stop", class_obscure, cmd_record_stop,
1258 _("Stop the record/replay target."),
1259 &record_cmdlist);
1260 add_alias_cmd ("s", "stop", class_obscure, 1, &record_cmdlist);
1262 /* Record instructions number limit command. */
1263 add_setshow_boolean_cmd ("stop-at-limit", no_class,
1264 &record_stop_at_limit, _("\
1265 Set whether record/replay stops when record/replay buffer becomes full."), _("\
1266 Show whether record/replay stops when record/replay buffer becomes full."), _("\
1267 Default is ON.\n\
1268 When ON, if the record/replay buffer becomes full, ask user what to do.\n\
1269 When OFF, if the record/replay buffer becomes full,\n\
1270 delete the oldest recorded instruction to make room for each new one."),
1271 NULL, NULL,
1272 &set_record_cmdlist, &show_record_cmdlist);
1273 add_setshow_zinteger_cmd ("insn-number-max", no_class,
1274 &record_insn_max_num,
1275 _("Set record/replay buffer limit."),
1276 _("Show record/replay buffer limit."), _("\
1277 Set the maximum number of instructions to be stored in the\n\
1278 record/replay buffer. Zero means unlimited. Default is 200000."),
1279 set_record_insn_max_num,
1280 NULL, &set_record_cmdlist, &show_record_cmdlist);
1281 add_cmd ("insn-number", class_obscure, show_record_insn_number,
1282 _("Show the current number of instructions in the "
1283 "record/replay buffer."), &info_record_cmdlist);