* remote.c (remote_pid_to_str): If printing a process id and we
[binutils-gdb.git] / gdb / record.c
blob43737af9b2c8cd476fe530ee077f610d9bb72b22
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 /* Set this flag if target memory for this entry
55 can no longer be accessed. */
56 int mem_entry_not_accessible;
57 gdb_byte *val;
60 enum record_type
62 record_end = 0,
63 record_reg,
64 record_mem
67 struct record_entry
69 struct record_entry *prev;
70 struct record_entry *next;
71 enum record_type type;
72 union
74 /* reg */
75 struct record_reg_entry reg;
76 /* mem */
77 struct record_mem_entry mem;
78 } u;
81 /* This is the debug switch for process record. */
82 int record_debug = 0;
84 /* These list is for execution log. */
85 static struct record_entry record_first;
86 static struct record_entry *record_list = &record_first;
87 static struct record_entry *record_arch_list_head = NULL;
88 static struct record_entry *record_arch_list_tail = NULL;
90 /* 1 ask user. 0 auto delete the last struct record_entry. */
91 static int record_stop_at_limit = 1;
92 static int record_insn_max_num = DEFAULT_RECORD_INSN_MAX_NUM;
93 static int record_insn_num = 0;
95 /* The target_ops of process record. */
96 static struct target_ops record_ops;
98 /* The beneath function pointers. */
99 static struct target_ops *record_beneath_to_resume_ops;
100 static void (*record_beneath_to_resume) (struct target_ops *, ptid_t, int,
101 enum target_signal);
102 static struct target_ops *record_beneath_to_wait_ops;
103 static ptid_t (*record_beneath_to_wait) (struct target_ops *, ptid_t,
104 struct target_waitstatus *,
105 int);
106 static struct target_ops *record_beneath_to_store_registers_ops;
107 static void (*record_beneath_to_store_registers) (struct target_ops *,
108 struct regcache *,
109 int regno);
110 static struct target_ops *record_beneath_to_xfer_partial_ops;
111 static LONGEST (*record_beneath_to_xfer_partial) (struct target_ops *ops,
112 enum target_object object,
113 const char *annex,
114 gdb_byte *readbuf,
115 const gdb_byte *writebuf,
116 ULONGEST offset,
117 LONGEST len);
118 static int (*record_beneath_to_insert_breakpoint) (struct gdbarch *,
119 struct bp_target_info *);
120 static int (*record_beneath_to_remove_breakpoint) (struct gdbarch *,
121 struct bp_target_info *);
123 static void
124 record_list_release (struct record_entry *rec)
126 struct record_entry *tmp;
128 if (!rec)
129 return;
131 while (rec->next)
133 rec = rec->next;
136 while (rec->prev)
138 tmp = rec;
139 rec = rec->prev;
140 if (tmp->type == record_reg)
141 xfree (tmp->u.reg.val);
142 else if (tmp->type == record_mem)
143 xfree (tmp->u.mem.val);
144 xfree (tmp);
147 if (rec != &record_first)
148 xfree (rec);
151 static void
152 record_list_release_next (void)
154 struct record_entry *rec = record_list;
155 struct record_entry *tmp = rec->next;
156 rec->next = NULL;
157 while (tmp)
159 rec = tmp->next;
160 if (tmp->type == record_reg)
161 record_insn_num--;
162 else if (tmp->type == record_reg)
163 xfree (tmp->u.reg.val);
164 else if (tmp->type == record_mem)
165 xfree (tmp->u.mem.val);
166 xfree (tmp);
167 tmp = rec;
171 static void
172 record_list_release_first (void)
174 struct record_entry *tmp = NULL;
175 enum record_type type;
177 if (!record_first.next)
178 return;
180 while (1)
182 type = record_first.next->type;
184 if (type == record_reg)
185 xfree (record_first.next->u.reg.val);
186 else if (type == record_mem)
187 xfree (record_first.next->u.mem.val);
188 tmp = record_first.next;
189 record_first.next = tmp->next;
190 xfree (tmp);
192 if (!record_first.next)
194 gdb_assert (record_insn_num == 1);
195 break;
198 record_first.next->prev = &record_first;
200 if (type == record_end)
201 break;
204 record_insn_num--;
207 /* Add a struct record_entry to record_arch_list. */
209 static void
210 record_arch_list_add (struct record_entry *rec)
212 if (record_debug > 1)
213 fprintf_unfiltered (gdb_stdlog,
214 "Process record: record_arch_list_add %s.\n",
215 host_address_to_string (rec));
217 if (record_arch_list_tail)
219 record_arch_list_tail->next = rec;
220 rec->prev = record_arch_list_tail;
221 record_arch_list_tail = rec;
223 else
225 record_arch_list_head = rec;
226 record_arch_list_tail = rec;
230 /* Record the value of a register NUM to record_arch_list. */
233 record_arch_list_add_reg (struct regcache *regcache, int num)
235 struct record_entry *rec;
237 if (record_debug > 1)
238 fprintf_unfiltered (gdb_stdlog,
239 "Process record: add register num = %d to "
240 "record list.\n",
241 num);
243 rec = (struct record_entry *) xmalloc (sizeof (struct record_entry));
244 rec->u.reg.val = (gdb_byte *) xmalloc (MAX_REGISTER_SIZE);
245 rec->prev = NULL;
246 rec->next = NULL;
247 rec->type = record_reg;
248 rec->u.reg.num = num;
250 regcache_raw_read (regcache, num, rec->u.reg.val);
252 record_arch_list_add (rec);
254 return 0;
257 /* Record the value of a region of memory whose address is ADDR and
258 length is LEN to record_arch_list. */
261 record_arch_list_add_mem (CORE_ADDR addr, int len)
263 struct record_entry *rec;
265 if (record_debug > 1)
266 fprintf_unfiltered (gdb_stdlog,
267 "Process record: add mem addr = %s len = %d to "
268 "record list.\n",
269 paddress (target_gdbarch, addr), len);
271 if (!addr)
272 return 0;
274 rec = (struct record_entry *) xmalloc (sizeof (struct record_entry));
275 rec->u.mem.val = (gdb_byte *) xmalloc (len);
276 rec->prev = NULL;
277 rec->next = NULL;
278 rec->type = record_mem;
279 rec->u.mem.addr = addr;
280 rec->u.mem.len = len;
281 rec->u.mem.mem_entry_not_accessible = 0;
283 if (target_read_memory (addr, rec->u.mem.val, len))
285 if (record_debug)
286 fprintf_unfiltered (gdb_stdlog,
287 "Process record: error reading memory at "
288 "addr = %s len = %d.\n",
289 paddress (target_gdbarch, addr), len);
290 xfree (rec->u.mem.val);
291 xfree (rec);
292 return -1;
295 record_arch_list_add (rec);
297 return 0;
300 /* Add a record_end type struct record_entry to record_arch_list. */
303 record_arch_list_add_end (void)
305 struct record_entry *rec;
307 if (record_debug > 1)
308 fprintf_unfiltered (gdb_stdlog,
309 "Process record: add end to arch list.\n");
311 rec = (struct record_entry *) xmalloc (sizeof (struct record_entry));
312 rec->prev = NULL;
313 rec->next = NULL;
314 rec->type = record_end;
316 record_arch_list_add (rec);
318 return 0;
321 static void
322 record_check_insn_num (int set_terminal)
324 if (record_insn_max_num)
326 gdb_assert (record_insn_num <= record_insn_max_num);
327 if (record_insn_num == record_insn_max_num)
329 /* Ask user what to do. */
330 if (record_stop_at_limit)
332 int q;
333 if (set_terminal)
334 target_terminal_ours ();
335 q = yquery (_("Do you want to auto delete previous execution "
336 "log entries when record/replay buffer becomes "
337 "full (record stop-at-limit)?"));
338 if (set_terminal)
339 target_terminal_inferior ();
340 if (q)
341 record_stop_at_limit = 0;
342 else
343 error (_("Process record: inferior program stopped."));
349 /* Before inferior step (when GDB record the running message, inferior
350 only can step), GDB will call this function to record the values to
351 record_list. This function will call gdbarch_process_record to
352 record the running message of inferior and set them to
353 record_arch_list, and add it to record_list. */
355 static void
356 record_message_cleanups (void *ignore)
358 record_list_release (record_arch_list_tail);
361 static int
362 record_message (void *args)
364 int ret;
365 struct regcache *regcache = args;
366 struct cleanup *old_cleanups = make_cleanup (record_message_cleanups, 0);
368 record_arch_list_head = NULL;
369 record_arch_list_tail = NULL;
371 /* Check record_insn_num. */
372 record_check_insn_num (1);
374 ret = gdbarch_process_record (get_regcache_arch (regcache),
375 regcache,
376 regcache_read_pc (regcache));
377 if (ret > 0)
378 error (_("Process record: inferior program stopped."));
379 if (ret < 0)
380 error (_("Process record: failed to record execution log."));
382 discard_cleanups (old_cleanups);
384 record_list->next = record_arch_list_head;
385 record_arch_list_head->prev = record_list;
386 record_list = record_arch_list_tail;
388 if (record_insn_num == record_insn_max_num && record_insn_max_num)
389 record_list_release_first ();
390 else
391 record_insn_num++;
393 return 1;
396 static int
397 do_record_message (struct regcache *regcache)
399 return catch_errors (record_message, regcache, NULL, RETURN_MASK_ALL);
402 /* Set to 1 if record_store_registers and record_xfer_partial
403 doesn't need record. */
405 static int record_gdb_operation_disable = 0;
407 struct cleanup *
408 record_gdb_operation_disable_set (void)
410 struct cleanup *old_cleanups = NULL;
412 old_cleanups =
413 make_cleanup_restore_integer (&record_gdb_operation_disable);
414 record_gdb_operation_disable = 1;
416 return old_cleanups;
419 static void
420 record_open (char *name, int from_tty)
422 struct target_ops *t;
424 if (record_debug)
425 fprintf_unfiltered (gdb_stdlog, "Process record: record_open\n");
427 /* check exec */
428 if (!target_has_execution)
429 error (_("Process record: the program is not being run."));
430 if (non_stop)
431 error (_("Process record target can't debug inferior in non-stop mode "
432 "(non-stop)."));
433 if (target_async_permitted)
434 error (_("Process record target can't debug inferior in asynchronous "
435 "mode (target-async)."));
437 if (!gdbarch_process_record_p (target_gdbarch))
438 error (_("Process record: the current architecture doesn't support "
439 "record function."));
441 /* Check if record target is already running. */
442 if (current_target.to_stratum == record_stratum)
444 if (!nquery
445 (_("Process record target already running, do you want to delete "
446 "the old record log?")))
447 return;
450 /*Reset the beneath function pointers. */
451 record_beneath_to_resume = NULL;
452 record_beneath_to_wait = NULL;
453 record_beneath_to_store_registers = NULL;
454 record_beneath_to_xfer_partial = NULL;
455 record_beneath_to_insert_breakpoint = NULL;
456 record_beneath_to_remove_breakpoint = NULL;
458 /* Set the beneath function pointers. */
459 for (t = current_target.beneath; t != NULL; t = t->beneath)
461 if (!record_beneath_to_resume)
463 record_beneath_to_resume = t->to_resume;
464 record_beneath_to_resume_ops = t;
466 if (!record_beneath_to_wait)
468 record_beneath_to_wait = t->to_wait;
469 record_beneath_to_wait_ops = t;
471 if (!record_beneath_to_store_registers)
473 record_beneath_to_store_registers = t->to_store_registers;
474 record_beneath_to_store_registers_ops = t;
476 if (!record_beneath_to_xfer_partial)
478 record_beneath_to_xfer_partial = t->to_xfer_partial;
479 record_beneath_to_xfer_partial_ops = t;
481 if (!record_beneath_to_insert_breakpoint)
482 record_beneath_to_insert_breakpoint = t->to_insert_breakpoint;
483 if (!record_beneath_to_remove_breakpoint)
484 record_beneath_to_remove_breakpoint = t->to_remove_breakpoint;
486 if (!record_beneath_to_resume)
487 error (_("Process record can't get to_resume."));
488 if (!record_beneath_to_wait)
489 error (_("Process record can't get to_wait."));
490 if (!record_beneath_to_store_registers)
491 error (_("Process record can't get to_store_registers."));
492 if (!record_beneath_to_xfer_partial)
493 error (_("Process record can't get to_xfer_partial."));
494 if (!record_beneath_to_insert_breakpoint)
495 error (_("Process record can't get to_insert_breakpoint."));
496 if (!record_beneath_to_remove_breakpoint)
497 error (_("Process record can't get to_remove_breakpoint."));
499 push_target (&record_ops);
501 /* Reset */
502 record_insn_num = 0;
503 record_list = &record_first;
504 record_list->next = NULL;
507 static void
508 record_close (int quitting)
510 if (record_debug)
511 fprintf_unfiltered (gdb_stdlog, "Process record: record_close\n");
513 record_list_release (record_list);
516 static int record_resume_step = 0;
517 static enum target_signal record_resume_siggnal;
518 static int record_resume_error;
520 static void
521 record_resume (struct target_ops *ops, ptid_t ptid, int step,
522 enum target_signal siggnal)
524 record_resume_step = step;
525 record_resume_siggnal = siggnal;
527 if (!RECORD_IS_REPLAY)
529 if (do_record_message (get_current_regcache ()))
531 record_resume_error = 0;
533 else
535 record_resume_error = 1;
536 return;
538 record_beneath_to_resume (record_beneath_to_resume_ops, ptid, 1,
539 siggnal);
543 static int record_get_sig = 0;
545 static void
546 record_sig_handler (int signo)
548 if (record_debug)
549 fprintf_unfiltered (gdb_stdlog, "Process record: get a signal\n");
551 /* It will break the running inferior in replay mode. */
552 record_resume_step = 1;
554 /* It will let record_wait set inferior status to get the signal
555 SIGINT. */
556 record_get_sig = 1;
559 static void
560 record_wait_cleanups (void *ignore)
562 if (execution_direction == EXEC_REVERSE)
564 if (record_list->next)
565 record_list = record_list->next;
567 else
568 record_list = record_list->prev;
571 /* In replay mode, this function examines the recorded log and
572 determines where to stop. */
574 static ptid_t
575 record_wait (struct target_ops *ops,
576 ptid_t ptid, struct target_waitstatus *status,
577 int options)
579 struct cleanup *set_cleanups = record_gdb_operation_disable_set ();
581 if (record_debug)
582 fprintf_unfiltered (gdb_stdlog,
583 "Process record: record_wait "
584 "record_resume_step = %d\n",
585 record_resume_step);
587 if (!RECORD_IS_REPLAY)
589 if (record_resume_error)
591 /* If record_resume get error, return directly. */
592 status->kind = TARGET_WAITKIND_STOPPED;
593 status->value.sig = TARGET_SIGNAL_ABRT;
594 return inferior_ptid;
597 if (record_resume_step)
599 /* This is a single step. */
600 return record_beneath_to_wait (record_beneath_to_wait_ops,
601 ptid, status, 0);
603 else
605 /* This is not a single step. */
606 ptid_t ret;
607 CORE_ADDR tmp_pc;
609 while (1)
611 ret = record_beneath_to_wait (record_beneath_to_wait_ops,
612 ptid, status, 0);
614 if (status->kind == TARGET_WAITKIND_STOPPED
615 && status->value.sig == TARGET_SIGNAL_TRAP)
617 /* Check if there is a breakpoint. */
618 registers_changed ();
619 tmp_pc = regcache_read_pc (get_current_regcache ());
620 if (breakpoint_inserted_here_p (tmp_pc))
622 /* There is a breakpoint. */
623 CORE_ADDR decr_pc_after_break =
624 gdbarch_decr_pc_after_break
625 (get_regcache_arch (get_current_regcache ()));
626 if (decr_pc_after_break)
628 regcache_write_pc (get_thread_regcache (ret),
629 tmp_pc + decr_pc_after_break);
632 else
634 /* There is not a breakpoint. */
635 if (!do_record_message (get_current_regcache ()))
637 break;
639 record_beneath_to_resume (record_beneath_to_resume_ops,
640 ptid, 1,
641 record_resume_siggnal);
642 continue;
646 /* The inferior is broken by a breakpoint or a signal. */
647 break;
650 return ret;
653 else
655 struct regcache *regcache = get_current_regcache ();
656 struct gdbarch *gdbarch = get_regcache_arch (regcache);
657 int continue_flag = 1;
658 int first_record_end = 1;
659 struct cleanup *old_cleanups = make_cleanup (record_wait_cleanups, 0);
660 CORE_ADDR tmp_pc;
662 status->kind = TARGET_WAITKIND_STOPPED;
664 /* Check breakpoint when forward execute. */
665 if (execution_direction == EXEC_FORWARD)
667 tmp_pc = regcache_read_pc (regcache);
668 if (breakpoint_inserted_here_p (tmp_pc))
670 if (record_debug)
671 fprintf_unfiltered (gdb_stdlog,
672 "Process record: break at %s.\n",
673 paddress (gdbarch, tmp_pc));
674 if (gdbarch_decr_pc_after_break (gdbarch)
675 && !record_resume_step)
676 regcache_write_pc (regcache,
677 tmp_pc +
678 gdbarch_decr_pc_after_break (gdbarch));
679 goto replay_out;
683 record_get_sig = 0;
684 signal (SIGINT, record_sig_handler);
685 /* If GDB is in terminal_inferior mode, it will not get the signal.
686 And in GDB replay mode, GDB doesn't need to be in terminal_inferior
687 mode, because inferior will not executed.
688 Then set it to terminal_ours to make GDB get the signal. */
689 target_terminal_ours ();
691 /* In EXEC_FORWARD mode, record_list points to the tail of prev
692 instruction. */
693 if (execution_direction == EXEC_FORWARD && record_list->next)
694 record_list = record_list->next;
696 /* Loop over the record_list, looking for the next place to
697 stop. */
700 /* Check for beginning and end of log. */
701 if (execution_direction == EXEC_REVERSE
702 && record_list == &record_first)
704 /* Hit beginning of record log in reverse. */
705 status->kind = TARGET_WAITKIND_NO_HISTORY;
706 break;
708 if (execution_direction != EXEC_REVERSE && !record_list->next)
710 /* Hit end of record log going forward. */
711 status->kind = TARGET_WAITKIND_NO_HISTORY;
712 break;
715 /* Set ptid, register and memory according to record_list. */
716 if (record_list->type == record_reg)
718 /* reg */
719 gdb_byte reg[MAX_REGISTER_SIZE];
720 if (record_debug > 1)
721 fprintf_unfiltered (gdb_stdlog,
722 "Process record: record_reg %s to "
723 "inferior num = %d.\n",
724 host_address_to_string (record_list),
725 record_list->u.reg.num);
726 regcache_cooked_read (regcache, record_list->u.reg.num, reg);
727 regcache_cooked_write (regcache, record_list->u.reg.num,
728 record_list->u.reg.val);
729 memcpy (record_list->u.reg.val, reg, MAX_REGISTER_SIZE);
731 else if (record_list->type == record_mem)
733 /* mem */
734 /* Nothing to do if the entry is flagged not_accessible. */
735 if (!record_list->u.mem.mem_entry_not_accessible)
737 gdb_byte *mem = alloca (record_list->u.mem.len);
738 if (record_debug > 1)
739 fprintf_unfiltered (gdb_stdlog,
740 "Process record: record_mem %s to "
741 "inferior addr = %s len = %d.\n",
742 host_address_to_string (record_list),
743 paddress (gdbarch,
744 record_list->u.mem.addr),
745 record_list->u.mem.len);
747 if (target_read_memory (record_list->u.mem.addr, mem,
748 record_list->u.mem.len))
750 if (execution_direction != EXEC_REVERSE)
751 error (_("Process record: error reading memory at "
752 "addr = %s len = %d."),
753 paddress (gdbarch, record_list->u.mem.addr),
754 record_list->u.mem.len);
755 else
756 /* Read failed --
757 flag entry as not_accessible. */
758 record_list->u.mem.mem_entry_not_accessible = 1;
760 else
762 if (target_write_memory (record_list->u.mem.addr,
763 record_list->u.mem.val,
764 record_list->u.mem.len))
766 if (execution_direction != EXEC_REVERSE)
767 error (_("Process record: error writing memory at "
768 "addr = %s len = %d."),
769 paddress (gdbarch, record_list->u.mem.addr),
770 record_list->u.mem.len);
771 else
772 /* Write failed --
773 flag entry as not_accessible. */
774 record_list->u.mem.mem_entry_not_accessible = 1;
776 else
778 memcpy (record_list->u.mem.val, mem,
779 record_list->u.mem.len);
784 else
786 if (record_debug > 1)
787 fprintf_unfiltered (gdb_stdlog,
788 "Process record: record_end %s to "
789 "inferior.\n",
790 host_address_to_string (record_list));
792 if (first_record_end && execution_direction == EXEC_REVERSE)
794 /* When reverse excute, the first record_end is the part of
795 current instruction. */
796 first_record_end = 0;
798 else
800 /* In EXEC_REVERSE mode, this is the record_end of prev
801 instruction.
802 In EXEC_FORWARD mode, this is the record_end of current
803 instruction. */
804 /* step */
805 if (record_resume_step)
807 if (record_debug > 1)
808 fprintf_unfiltered (gdb_stdlog,
809 "Process record: step.\n");
810 continue_flag = 0;
813 /* check breakpoint */
814 tmp_pc = regcache_read_pc (regcache);
815 if (breakpoint_inserted_here_p (tmp_pc))
817 if (record_debug)
818 fprintf_unfiltered (gdb_stdlog,
819 "Process record: break "
820 "at %s.\n",
821 paddress (gdbarch, tmp_pc));
822 if (gdbarch_decr_pc_after_break (gdbarch)
823 && execution_direction == EXEC_FORWARD
824 && !record_resume_step)
825 regcache_write_pc (regcache,
826 tmp_pc +
827 gdbarch_decr_pc_after_break (gdbarch));
828 continue_flag = 0;
833 if (continue_flag)
835 if (execution_direction == EXEC_REVERSE)
837 if (record_list->prev)
838 record_list = record_list->prev;
840 else
842 if (record_list->next)
843 record_list = record_list->next;
847 while (continue_flag);
849 signal (SIGINT, handle_sigint);
851 replay_out:
852 if (record_get_sig)
853 status->value.sig = TARGET_SIGNAL_INT;
854 else
855 status->value.sig = TARGET_SIGNAL_TRAP;
857 discard_cleanups (old_cleanups);
860 do_cleanups (set_cleanups);
861 return inferior_ptid;
864 static void
865 record_disconnect (struct target_ops *target, char *args, int from_tty)
867 if (record_debug)
868 fprintf_unfiltered (gdb_stdlog, "Process record: record_disconnect\n");
870 unpush_target (&record_ops);
871 target_disconnect (args, from_tty);
874 static void
875 record_detach (struct target_ops *ops, char *args, int from_tty)
877 if (record_debug)
878 fprintf_unfiltered (gdb_stdlog, "Process record: record_detach\n");
880 unpush_target (&record_ops);
881 target_detach (args, from_tty);
884 static void
885 record_mourn_inferior (struct target_ops *ops)
887 if (record_debug)
888 fprintf_unfiltered (gdb_stdlog, "Process record: "
889 "record_mourn_inferior\n");
891 unpush_target (&record_ops);
892 target_mourn_inferior ();
895 /* Close process record target before killing the inferior process. */
897 static void
898 record_kill (struct target_ops *ops)
900 if (record_debug)
901 fprintf_unfiltered (gdb_stdlog, "Process record: record_kill\n");
903 unpush_target (&record_ops);
904 target_kill ();
907 /* Record registers change (by user or by GDB) to list as an instruction. */
909 static void
910 record_registers_change (struct regcache *regcache, int regnum)
912 /* Check record_insn_num. */
913 record_check_insn_num (0);
915 record_arch_list_head = NULL;
916 record_arch_list_tail = NULL;
918 if (regnum < 0)
920 int i;
921 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
923 if (record_arch_list_add_reg (regcache, i))
925 record_list_release (record_arch_list_tail);
926 error (_("Process record: failed to record execution log."));
930 else
932 if (record_arch_list_add_reg (regcache, regnum))
934 record_list_release (record_arch_list_tail);
935 error (_("Process record: failed to record execution log."));
938 if (record_arch_list_add_end ())
940 record_list_release (record_arch_list_tail);
941 error (_("Process record: failed to record execution log."));
943 record_list->next = record_arch_list_head;
944 record_arch_list_head->prev = record_list;
945 record_list = record_arch_list_tail;
947 if (record_insn_num == record_insn_max_num && record_insn_max_num)
948 record_list_release_first ();
949 else
950 record_insn_num++;
953 static void
954 record_store_registers (struct target_ops *ops, struct regcache *regcache,
955 int regno)
957 if (!record_gdb_operation_disable)
959 if (RECORD_IS_REPLAY)
961 int n;
963 /* Let user choose if he wants to write register or not. */
964 if (regno < 0)
966 nquery (_("Because GDB is in replay mode, changing the "
967 "value of a register will make the execution "
968 "log unusable from this point onward. "
969 "Change all registers?"));
970 else
972 nquery (_("Because GDB is in replay mode, changing the value "
973 "of a register will make the execution log unusable "
974 "from this point onward. Change register %s?"),
975 gdbarch_register_name (get_regcache_arch (regcache),
976 regno));
978 if (!n)
980 /* Invalidate the value of regcache that was set in function
981 "regcache_raw_write". */
982 if (regno < 0)
984 int i;
985 for (i = 0;
986 i < gdbarch_num_regs (get_regcache_arch (regcache));
987 i++)
988 regcache_invalidate (regcache, i);
990 else
991 regcache_invalidate (regcache, regno);
993 error (_("Process record canceled the operation."));
996 /* Destroy the record from here forward. */
997 record_list_release_next ();
1000 record_registers_change (regcache, regno);
1002 record_beneath_to_store_registers (record_beneath_to_store_registers_ops,
1003 regcache, regno);
1006 /* Behavior is conditional on RECORD_IS_REPLAY.
1007 In replay mode, we cannot write memory unles we are willing to
1008 invalidate the record/replay log from this point forward. */
1010 static LONGEST
1011 record_xfer_partial (struct target_ops *ops, enum target_object object,
1012 const char *annex, gdb_byte *readbuf,
1013 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
1015 if (!record_gdb_operation_disable
1016 && (object == TARGET_OBJECT_MEMORY
1017 || object == TARGET_OBJECT_RAW_MEMORY) && writebuf)
1019 if (RECORD_IS_REPLAY)
1021 /* Let user choose if he wants to write memory or not. */
1022 if (!nquery (_("Because GDB is in replay mode, writing to memory "
1023 "will make the execution log unusable from this "
1024 "point onward. Write memory at address %s?"),
1025 paddress (target_gdbarch, offset)))
1026 error (_("Process record canceled the operation."));
1028 /* Destroy the record from here forward. */
1029 record_list_release_next ();
1032 /* Check record_insn_num */
1033 record_check_insn_num (0);
1035 /* Record registers change to list as an instruction. */
1036 record_arch_list_head = NULL;
1037 record_arch_list_tail = NULL;
1038 if (record_arch_list_add_mem (offset, len))
1040 record_list_release (record_arch_list_tail);
1041 if (record_debug)
1042 fprintf_unfiltered (gdb_stdlog,
1043 _("Process record: failed to record "
1044 "execution log."));
1045 return -1;
1047 if (record_arch_list_add_end ())
1049 record_list_release (record_arch_list_tail);
1050 if (record_debug)
1051 fprintf_unfiltered (gdb_stdlog,
1052 _("Process record: failed to record "
1053 "execution log."));
1054 return -1;
1056 record_list->next = record_arch_list_head;
1057 record_arch_list_head->prev = record_list;
1058 record_list = record_arch_list_tail;
1060 if (record_insn_num == record_insn_max_num && record_insn_max_num)
1061 record_list_release_first ();
1062 else
1063 record_insn_num++;
1066 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops,
1067 object, annex, readbuf, writebuf,
1068 offset, len);
1071 /* Behavior is conditional on RECORD_IS_REPLAY.
1072 We will not actually insert or remove breakpoints when replaying,
1073 nor when recording. */
1075 static int
1076 record_insert_breakpoint (struct gdbarch *gdbarch,
1077 struct bp_target_info *bp_tgt)
1079 if (!RECORD_IS_REPLAY)
1081 struct cleanup *old_cleanups = record_gdb_operation_disable_set ();
1082 int ret = record_beneath_to_insert_breakpoint (gdbarch, bp_tgt);
1084 do_cleanups (old_cleanups);
1086 return ret;
1089 return 0;
1092 static int
1093 record_remove_breakpoint (struct gdbarch *gdbarch,
1094 struct bp_target_info *bp_tgt)
1096 if (!RECORD_IS_REPLAY)
1098 struct cleanup *old_cleanups = record_gdb_operation_disable_set ();
1099 int ret = record_beneath_to_remove_breakpoint (gdbarch, bp_tgt);
1101 do_cleanups (old_cleanups);
1103 return ret;
1106 return 0;
1109 static int
1110 record_can_execute_reverse (void)
1112 return 1;
1115 static void
1116 init_record_ops (void)
1118 record_ops.to_shortname = "record";
1119 record_ops.to_longname = "Process record and replay target";
1120 record_ops.to_doc =
1121 "Log program while executing and replay execution from log.";
1122 record_ops.to_open = record_open;
1123 record_ops.to_close = record_close;
1124 record_ops.to_resume = record_resume;
1125 record_ops.to_wait = record_wait;
1126 record_ops.to_disconnect = record_disconnect;
1127 record_ops.to_detach = record_detach;
1128 record_ops.to_mourn_inferior = record_mourn_inferior;
1129 record_ops.to_kill = record_kill;
1130 record_ops.to_create_inferior = find_default_create_inferior;
1131 record_ops.to_store_registers = record_store_registers;
1132 record_ops.to_xfer_partial = record_xfer_partial;
1133 record_ops.to_insert_breakpoint = record_insert_breakpoint;
1134 record_ops.to_remove_breakpoint = record_remove_breakpoint;
1135 record_ops.to_can_execute_reverse = record_can_execute_reverse;
1136 record_ops.to_stratum = record_stratum;
1137 record_ops.to_magic = OPS_MAGIC;
1140 static void
1141 show_record_debug (struct ui_file *file, int from_tty,
1142 struct cmd_list_element *c, const char *value)
1144 fprintf_filtered (file, _("Debugging of process record target is %s.\n"),
1145 value);
1148 /* Alias for "target record". */
1150 static void
1151 cmd_record_start (char *args, int from_tty)
1153 execute_command ("target record", from_tty);
1156 /* Truncate the record log from the present point
1157 of replay until the end. */
1159 static void
1160 cmd_record_delete (char *args, int from_tty)
1162 if (current_target.to_stratum == record_stratum)
1164 if (RECORD_IS_REPLAY)
1166 if (!from_tty || query (_("Delete the log from this point forward "
1167 "and begin to record the running message "
1168 "at current PC?")))
1169 record_list_release_next ();
1171 else
1172 printf_unfiltered (_("Already at end of record list.\n"));
1175 else
1176 printf_unfiltered (_("Process record is not started.\n"));
1179 /* Implement the "stoprecord" command. */
1181 static void
1182 cmd_record_stop (char *args, int from_tty)
1184 if (current_target.to_stratum == record_stratum)
1186 if (!record_list || !from_tty || query (_("Delete recorded log and "
1187 "stop recording?")))
1188 unpush_target (&record_ops);
1190 else
1191 printf_unfiltered (_("Process record is not started.\n"));
1194 /* Set upper limit of record log size. */
1196 static void
1197 set_record_insn_max_num (char *args, int from_tty, struct cmd_list_element *c)
1199 if (record_insn_num > record_insn_max_num && record_insn_max_num)
1201 printf_unfiltered (_("Record instructions number is bigger than "
1202 "record instructions max number. Auto delete "
1203 "the first ones?\n"));
1205 while (record_insn_num > record_insn_max_num)
1206 record_list_release_first ();
1210 /* Print the current index into the record log (number of insns recorded
1211 so far). */
1213 static void
1214 show_record_insn_number (char *ignore, int from_tty)
1216 printf_unfiltered (_("Record instruction number is %d.\n"),
1217 record_insn_num);
1220 static struct cmd_list_element *record_cmdlist, *set_record_cmdlist,
1221 *show_record_cmdlist, *info_record_cmdlist;
1223 static void
1224 set_record_command (char *args, int from_tty)
1226 printf_unfiltered (_("\
1227 \"set record\" must be followed by an apporpriate subcommand.\n"));
1228 help_list (set_record_cmdlist, "set record ", all_commands, gdb_stdout);
1231 static void
1232 show_record_command (char *args, int from_tty)
1234 cmd_show_list (show_record_cmdlist, from_tty, "");
1237 static void
1238 info_record_command (char *args, int from_tty)
1240 cmd_show_list (info_record_cmdlist, from_tty, "");
1243 void
1244 _initialize_record (void)
1246 /* Init record_first. */
1247 record_first.prev = NULL;
1248 record_first.next = NULL;
1249 record_first.type = record_end;
1251 init_record_ops ();
1252 add_target (&record_ops);
1254 add_setshow_zinteger_cmd ("record", no_class, &record_debug,
1255 _("Set debugging of record/replay feature."),
1256 _("Show debugging of record/replay feature."),
1257 _("When enabled, debugging output for "
1258 "record/replay feature is displayed."),
1259 NULL, show_record_debug, &setdebuglist,
1260 &showdebuglist);
1262 add_prefix_cmd ("record", class_obscure, cmd_record_start,
1263 _("Abbreviated form of \"target record\" command."),
1264 &record_cmdlist, "record ", 0, &cmdlist);
1265 add_com_alias ("rec", "record", class_obscure, 1);
1266 add_prefix_cmd ("record", class_support, set_record_command,
1267 _("Set record options"), &set_record_cmdlist,
1268 "set record ", 0, &setlist);
1269 add_alias_cmd ("rec", "record", class_obscure, 1, &setlist);
1270 add_prefix_cmd ("record", class_support, show_record_command,
1271 _("Show record options"), &show_record_cmdlist,
1272 "show record ", 0, &showlist);
1273 add_alias_cmd ("rec", "record", class_obscure, 1, &showlist);
1274 add_prefix_cmd ("record", class_support, info_record_command,
1275 _("Info record options"), &info_record_cmdlist,
1276 "info record ", 0, &infolist);
1277 add_alias_cmd ("rec", "record", class_obscure, 1, &infolist);
1280 add_cmd ("delete", class_obscure, cmd_record_delete,
1281 _("Delete the rest of execution log and start recording it anew."),
1282 &record_cmdlist);
1283 add_alias_cmd ("d", "delete", class_obscure, 1, &record_cmdlist);
1284 add_alias_cmd ("del", "delete", class_obscure, 1, &record_cmdlist);
1286 add_cmd ("stop", class_obscure, cmd_record_stop,
1287 _("Stop the record/replay target."),
1288 &record_cmdlist);
1289 add_alias_cmd ("s", "stop", class_obscure, 1, &record_cmdlist);
1291 /* Record instructions number limit command. */
1292 add_setshow_boolean_cmd ("stop-at-limit", no_class,
1293 &record_stop_at_limit, _("\
1294 Set whether record/replay stops when record/replay buffer becomes full."), _("\
1295 Show whether record/replay stops when record/replay buffer becomes full."), _("\
1296 Default is ON.\n\
1297 When ON, if the record/replay buffer becomes full, ask user what to do.\n\
1298 When OFF, if the record/replay buffer becomes full,\n\
1299 delete the oldest recorded instruction to make room for each new one."),
1300 NULL, NULL,
1301 &set_record_cmdlist, &show_record_cmdlist);
1302 add_setshow_zinteger_cmd ("insn-number-max", no_class,
1303 &record_insn_max_num,
1304 _("Set record/replay buffer limit."),
1305 _("Show record/replay buffer limit."), _("\
1306 Set the maximum number of instructions to be stored in the\n\
1307 record/replay buffer. Zero means unlimited. Default is 200000."),
1308 set_record_insn_max_num,
1309 NULL, &set_record_cmdlist, &show_record_cmdlist);
1310 add_cmd ("insn-number", class_obscure, show_record_insn_number,
1311 _("Show the current number of instructions in the "
1312 "record/replay buffer."), &info_record_cmdlist);