]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/record.c
* ada-tasks.c (read_known_tasks_array): Use target_gdbarch instead
[thirdparty/binutils-gdb.git] / gdb / record.c
CommitLineData
69d05d38
HZ
1/* Process record and replay target for GDB, the GNU debugger.
2
3 Copyright (C) 2008, 2009 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20#include "defs.h"
21#include "gdbcmd.h"
22#include "regcache.h"
23#include "gdbthread.h"
24#include "event-top.h"
25#include "exceptions.h"
26#include "record.h"
27
28#include <signal.h>
29
30#define DEFAULT_RECORD_INSN_MAX_NUM 200000
31
32#define RECORD_IS_REPLAY \
33 (record_list->next || execution_direction == EXEC_REVERSE)
34
35/* These are the core struct of record function.
36
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.
41
42 Each struct record_entry is linked to "record_list" by "prev" and "next". */
43
44struct record_reg_entry
45{
46 int num;
47 gdb_byte *val;
48};
49
50struct record_mem_entry
51{
52 CORE_ADDR addr;
53 int len;
54 gdb_byte *val;
55};
56
57enum record_type
58{
59 record_end = 0,
60 record_reg,
61 record_mem
62};
63
64struct record_entry
65{
66 struct record_entry *prev;
67 struct record_entry *next;
68 enum record_type type;
69 union
70 {
71 /* reg */
72 struct record_reg_entry reg;
73 /* mem */
74 struct record_mem_entry mem;
75 } u;
76};
77
78/* This is the debug switch for process record. */
79int record_debug = 0;
80
81/* These list is for execution log. */
82static struct record_entry record_first;
83static struct record_entry *record_list = &record_first;
84static struct record_entry *record_arch_list_head = NULL;
85static struct record_entry *record_arch_list_tail = NULL;
86
87/* 1 ask user. 0 auto delete the last struct record_entry. */
88static int record_stop_at_limit = 1;
89static int record_insn_max_num = DEFAULT_RECORD_INSN_MAX_NUM;
90static int record_insn_num = 0;
91
92/* The target_ops of process record. */
93static struct target_ops record_ops;
94
95/* The beneath function pointers. */
96static struct target_ops *record_beneath_to_resume_ops;
97static void (*record_beneath_to_resume) (struct target_ops *, ptid_t, int,
98 enum target_signal);
99static struct target_ops *record_beneath_to_wait_ops;
100static ptid_t (*record_beneath_to_wait) (struct target_ops *, ptid_t,
47608cb1
PA
101 struct target_waitstatus *,
102 int);
69d05d38
HZ
103static struct target_ops *record_beneath_to_store_registers_ops;
104static void (*record_beneath_to_store_registers) (struct target_ops *,
105 struct regcache *,
106 int regno);
107static struct target_ops *record_beneath_to_xfer_partial_ops;
108static 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);
115static int (*record_beneath_to_insert_breakpoint) (struct bp_target_info *);
116static int (*record_beneath_to_remove_breakpoint) (struct bp_target_info *);
117
118static void
119record_list_release (struct record_entry *rec)
120{
121 struct record_entry *tmp;
122
123 if (!rec)
124 return;
125
126 while (rec->next)
127 {
128 rec = rec->next;
129 }
130
131 while (rec->prev)
132 {
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);
140 }
141
142 if (rec != &record_first)
143 xfree (rec);
144}
145
146static void
147record_list_release_next (void)
148{
149 struct record_entry *rec = record_list;
150 struct record_entry *tmp = rec->next;
151 rec->next = NULL;
152 while (tmp)
153 {
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;
163 }
164}
165
166static void
167record_list_release_first (void)
168{
169 struct record_entry *tmp = NULL;
170 enum record_type type;
171
172 if (!record_first.next)
173 return;
174
175 while (1)
176 {
177 type = record_first.next->type;
178
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);
186
187 if (!record_first.next)
188 {
189 gdb_assert (record_insn_num == 1);
190 break;
191 }
192
193 record_first.next->prev = &record_first;
194
195 if (type == record_end)
196 break;
197 }
198
199 record_insn_num--;
200}
201
202/* Add a struct record_entry to record_arch_list. */
203
204static void
205record_arch_list_add (struct record_entry *rec)
206{
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));
211
212 if (record_arch_list_tail)
213 {
214 record_arch_list_tail->next = rec;
215 rec->prev = record_arch_list_tail;
216 record_arch_list_tail = rec;
217 }
218 else
219 {
220 record_arch_list_head = rec;
221 record_arch_list_tail = rec;
222 }
223}
224
225/* Record the value of a register NUM to record_arch_list. */
226
227int
228record_arch_list_add_reg (struct regcache *regcache, int num)
229{
230 struct record_entry *rec;
231
232 if (record_debug > 1)
233 fprintf_unfiltered (gdb_stdlog,
234 "Process record: add register num = %d to "
235 "record list.\n",
236 num);
237
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;
244
245 regcache_raw_read (regcache, num, rec->u.reg.val);
246
247 record_arch_list_add (rec);
248
249 return 0;
250}
251
252/* Record the value of a region of memory whose address is ADDR and
253 length is LEN to record_arch_list. */
254
255int
256record_arch_list_add_mem (CORE_ADDR addr, int len)
257{
258 struct record_entry *rec;
259
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);
265
266 if (!addr)
267 return 0;
268
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;
276
277 if (target_read_memory (addr, rec->u.mem.val, len))
278 {
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;
287 }
288
289 record_arch_list_add (rec);
290
291 return 0;
292}
293
294/* Add a record_end type struct record_entry to record_arch_list. */
295
296int
297record_arch_list_add_end (void)
298{
299 struct record_entry *rec;
300
301 if (record_debug > 1)
302 fprintf_unfiltered (gdb_stdlog,
303 "Process record: add end to arch list.\n");
304
305 rec = (struct record_entry *) xmalloc (sizeof (struct record_entry));
306 rec->prev = NULL;
307 rec->next = NULL;
308 rec->type = record_end;
309
310 record_arch_list_add (rec);
311
312 return 0;
313}
314
315static void
316record_check_insn_num (int set_terminal)
317{
318 if (record_insn_max_num)
319 {
320 gdb_assert (record_insn_num <= record_insn_max_num);
321 if (record_insn_num == record_insn_max_num)
322 {
323 /* Ask user what to do. */
324 if (record_stop_at_limit)
325 {
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."));
338 }
339 }
340 }
341}
342
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. */
348
349static void
350record_message_cleanups (void *ignore)
351{
352 record_list_release (record_arch_list_tail);
353}
354
355static int
356record_message (void *args)
357{
358 int ret;
359 struct regcache *regcache = args;
360 struct cleanup *old_cleanups = make_cleanup (record_message_cleanups, 0);
361
362 record_arch_list_head = NULL;
363 record_arch_list_tail = NULL;
364
365 /* Check record_insn_num. */
366 record_check_insn_num (1);
367
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."));
375
376 discard_cleanups (old_cleanups);
377
378 record_list->next = record_arch_list_head;
379 record_arch_list_head->prev = record_list;
380 record_list = record_arch_list_tail;
381
382 if (record_insn_num == record_insn_max_num && record_insn_max_num)
383 record_list_release_first ();
384 else
385 record_insn_num++;
386
387 return 1;
388}
389
390static int
391do_record_message (struct regcache *regcache)
392{
393 return catch_errors (record_message, regcache, NULL, RETURN_MASK_ALL);
394}
395
396/* Set to 1 if record_store_registers and record_xfer_partial
397 doesn't need record. */
398
399static int record_gdb_operation_disable = 0;
400
401struct cleanup *
402record_gdb_operation_disable_set (void)
403{
404 struct cleanup *old_cleanups = NULL;
405
406 old_cleanups =
407 make_cleanup_restore_integer (&record_gdb_operation_disable);
408 record_gdb_operation_disable = 1;
409
410 return old_cleanups;
411}
412
413static void
414record_open (char *name, int from_tty)
415{
416 struct target_ops *t;
417
418 if (record_debug)
419 fprintf_unfiltered (gdb_stdlog, "Process record: record_open\n");
420
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)."));
430
a97b0ac8 431 if (!gdbarch_process_record_p (target_gdbarch))
69d05d38
HZ
432 error (_("Process record: the current architecture doesn't support "
433 "record function."));
434
435 /* Check if record target is already running. */
436 if (current_target.to_stratum == record_stratum)
437 {
438 if (!nquery
439 (_("Process record target already running, do you want to delete "
440 "the old record log?")))
441 return;
442 }
443
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;
451
452 /* Set the beneath function pointers. */
453 for (t = current_target.beneath; t != NULL; t = t->beneath)
454 {
455 if (!record_beneath_to_resume)
456 {
457 record_beneath_to_resume = t->to_resume;
458 record_beneath_to_resume_ops = t;
459 }
460 if (!record_beneath_to_wait)
461 {
462 record_beneath_to_wait = t->to_wait;
463 record_beneath_to_wait_ops = t;
464 }
465 if (!record_beneath_to_store_registers)
466 {
467 record_beneath_to_store_registers = t->to_store_registers;
468 record_beneath_to_store_registers_ops = t;
469 }
470 if (!record_beneath_to_xfer_partial)
471 {
472 record_beneath_to_xfer_partial = t->to_xfer_partial;
473 record_beneath_to_xfer_partial_ops = t;
474 }
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;
479 }
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."));
492
493 push_target (&record_ops);
494
495 /* Reset */
496 record_insn_num = 0;
497 record_list = &record_first;
498 record_list->next = NULL;
499}
500
501static void
502record_close (int quitting)
503{
504 if (record_debug)
505 fprintf_unfiltered (gdb_stdlog, "Process record: record_close\n");
506
507 record_list_release (record_list);
508}
509
510static int record_resume_step = 0;
511static enum target_signal record_resume_siggnal;
512static int record_resume_error;
513
514static void
515record_resume (struct target_ops *ops, ptid_t ptid, int step,
516 enum target_signal siggnal)
517{
518 record_resume_step = step;
519 record_resume_siggnal = siggnal;
520
521 if (!RECORD_IS_REPLAY)
522 {
523 if (do_record_message (get_current_regcache ()))
524 {
525 record_resume_error = 0;
526 }
527 else
528 {
529 record_resume_error = 1;
530 return;
531 }
532 record_beneath_to_resume (record_beneath_to_resume_ops, ptid, 1,
533 siggnal);
534 }
535}
536
537static int record_get_sig = 0;
538
539static void
540record_sig_handler (int signo)
541{
542 if (record_debug)
543 fprintf_unfiltered (gdb_stdlog, "Process record: get a signal\n");
544
545 /* It will break the running inferior in replay mode. */
546 record_resume_step = 1;
547
548 /* It will let record_wait set inferior status to get the signal
549 SIGINT. */
550 record_get_sig = 1;
551}
552
553static void
554record_wait_cleanups (void *ignore)
555{
556 if (execution_direction == EXEC_REVERSE)
557 {
558 if (record_list->next)
559 record_list = record_list->next;
560 }
561 else
562 record_list = record_list->prev;
563}
564
565/* In replay mode, this function examines the recorded log and
566 determines where to stop. */
567
568static ptid_t
569record_wait (struct target_ops *ops,
47608cb1
PA
570 ptid_t ptid, struct target_waitstatus *status,
571 int options)
69d05d38
HZ
572{
573 struct cleanup *set_cleanups = record_gdb_operation_disable_set ();
574
575 if (record_debug)
576 fprintf_unfiltered (gdb_stdlog,
577 "Process record: record_wait "
578 "record_resume_step = %d\n",
579 record_resume_step);
580
581 if (!RECORD_IS_REPLAY)
582 {
583 if (record_resume_error)
584 {
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;
589 }
590
591 if (record_resume_step)
592 {
593 /* This is a single step. */
594 return record_beneath_to_wait (record_beneath_to_wait_ops,
47608cb1 595 ptid, status, 0);
69d05d38
HZ
596 }
597 else
598 {
599 /* This is not a single step. */
600 ptid_t ret;
601 CORE_ADDR tmp_pc;
602
603 while (1)
604 {
605 ret = record_beneath_to_wait (record_beneath_to_wait_ops,
47608cb1 606 ptid, status, 0);
69d05d38
HZ
607
608 if (status->kind == TARGET_WAITKIND_STOPPED
609 && status->value.sig == TARGET_SIGNAL_TRAP)
610 {
611 /* Check if there is a breakpoint. */
612 registers_changed ();
fb14de7b 613 tmp_pc = regcache_read_pc (get_current_regcache ());
69d05d38
HZ
614 if (breakpoint_inserted_here_p (tmp_pc))
615 {
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)
621 {
622 regcache_write_pc (get_thread_regcache (ret),
623 tmp_pc + decr_pc_after_break);
624 }
625 }
626 else
627 {
628 /* There is not a breakpoint. */
629 if (!do_record_message (get_current_regcache ()))
630 {
631 break;
632 }
633 record_beneath_to_resume (record_beneath_to_resume_ops,
634 ptid, 1,
635 record_resume_siggnal);
636 continue;
637 }
638 }
639
640 /* The inferior is broken by a breakpoint or a signal. */
641 break;
642 }
643
644 return ret;
645 }
646 }
647 else
648 {
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;
654
655 status->kind = TARGET_WAITKIND_STOPPED;
656
657 /* Check breakpoint when forward execute. */
658 if (execution_direction == EXEC_FORWARD)
659 {
660 tmp_pc = regcache_read_pc (regcache);
661 if (breakpoint_inserted_here_p (tmp_pc))
662 {
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;
674 }
675 }
676
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 ();
684
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;
689
690 /* Loop over the record_list, looking for the next place to
691 stop. */
692 do
693 {
694 /* Check for beginning and end of log. */
695 if (execution_direction == EXEC_REVERSE
696 && record_list == &record_first)
697 {
698 /* Hit beginning of record log in reverse. */
699 status->kind = TARGET_WAITKIND_NO_HISTORY;
700 break;
701 }
702 if (execution_direction != EXEC_REVERSE && !record_list->next)
703 {
704 /* Hit end of record log going forward. */
705 status->kind = TARGET_WAITKIND_NO_HISTORY;
706 break;
707 }
708
709 /* Set ptid, register and memory according to record_list. */
710 if (record_list->type == record_reg)
711 {
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);
724 }
725 else if (record_list->type == record_mem)
726 {
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);
736
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);
743
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);
752
753 memcpy (record_list->u.mem.val, mem, record_list->u.mem.len);
754 }
755 else
756 {
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));
762
763 if (first_record_end && execution_direction == EXEC_REVERSE)
764 {
765 /* When reverse excute, the first record_end is the part of
766 current instruction. */
767 first_record_end = 0;
768 }
769 else
770 {
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)
777 {
778 if (record_debug > 1)
779 fprintf_unfiltered (gdb_stdlog,
780 "Process record: step.\n");
781 continue_flag = 0;
782 }
783
784 /* check breakpoint */
785 tmp_pc = regcache_read_pc (regcache);
786 if (breakpoint_inserted_here_p (tmp_pc))
787 {
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;
801 }
802 }
803 }
804
805 if (continue_flag)
806 {
807 if (execution_direction == EXEC_REVERSE)
808 {
809 if (record_list->prev)
810 record_list = record_list->prev;
811 }
812 else
813 {
814 if (record_list->next)
815 record_list = record_list->next;
816 }
817 }
818 }
819 while (continue_flag);
820
821 signal (SIGINT, handle_sigint);
822
823replay_out:
824 if (record_get_sig)
825 status->value.sig = TARGET_SIGNAL_INT;
826 else
827 status->value.sig = TARGET_SIGNAL_TRAP;
828
829 discard_cleanups (old_cleanups);
830 }
831
832 do_cleanups (set_cleanups);
833 return inferior_ptid;
834}
835
836static void
837record_disconnect (struct target_ops *target, char *args, int from_tty)
838{
839 if (record_debug)
840 fprintf_unfiltered (gdb_stdlog, "Process record: record_disconnect\n");
841
842 unpush_target (&record_ops);
843 target_disconnect (args, from_tty);
844}
845
846static void
847record_detach (struct target_ops *ops, char *args, int from_tty)
848{
849 if (record_debug)
850 fprintf_unfiltered (gdb_stdlog, "Process record: record_detach\n");
851
852 unpush_target (&record_ops);
853 target_detach (args, from_tty);
854}
855
856static void
857record_mourn_inferior (struct target_ops *ops)
858{
859 if (record_debug)
860 fprintf_unfiltered (gdb_stdlog, "Process record: "
861 "record_mourn_inferior\n");
862
863 unpush_target (&record_ops);
864 target_mourn_inferior ();
865}
866
867/* Close process record target before killing the inferior process. */
868
869static void
870record_kill (struct target_ops *ops)
871{
872 if (record_debug)
873 fprintf_unfiltered (gdb_stdlog, "Process record: record_kill\n");
874
875 unpush_target (&record_ops);
876 target_kill ();
877}
878
879/* Record registers change (by user or by GDB) to list as an instruction. */
880
881static void
882record_registers_change (struct regcache *regcache, int regnum)
883{
884 /* Check record_insn_num. */
885 record_check_insn_num (0);
886
887 record_arch_list_head = NULL;
888 record_arch_list_tail = NULL;
889
890 if (regnum < 0)
891 {
892 int i;
893 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
894 {
895 if (record_arch_list_add_reg (regcache, i))
896 {
897 record_list_release (record_arch_list_tail);
898 error (_("Process record: failed to record execution log."));
899 }
900 }
901 }
902 else
903 {
904 if (record_arch_list_add_reg (regcache, regnum))
905 {
906 record_list_release (record_arch_list_tail);
907 error (_("Process record: failed to record execution log."));
908 }
909 }
910 if (record_arch_list_add_end ())
911 {
912 record_list_release (record_arch_list_tail);
913 error (_("Process record: failed to record execution log."));
914 }
915 record_list->next = record_arch_list_head;
916 record_arch_list_head->prev = record_list;
917 record_list = record_arch_list_tail;
918
919 if (record_insn_num == record_insn_max_num && record_insn_max_num)
920 record_list_release_first ();
921 else
922 record_insn_num++;
923}
924
925static void
926record_store_registers (struct target_ops *ops, struct regcache *regcache,
927 int regno)
928{
929 if (!record_gdb_operation_disable)
930 {
931 if (RECORD_IS_REPLAY)
932 {
933 int n;
934 struct cleanup *old_cleanups;
935
936 /* Let user choose if he wants to write register or not. */
937 if (regno < 0)
938 n =
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
944 n =
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));
950
951 if (!n)
952 {
953 /* Invalidate the value of regcache that was set in function
954 "regcache_raw_write". */
955 if (regno < 0)
956 {
957 int i;
958 for (i = 0;
959 i < gdbarch_num_regs (get_regcache_arch (regcache));
960 i++)
961 regcache_invalidate (regcache, i);
962 }
963 else
964 regcache_invalidate (regcache, regno);
965
966 error (_("Process record canceled the operation."));
967 }
968
969 /* Destroy the record from here forward. */
970 record_list_release_next ();
971 }
972
973 record_registers_change (regcache, regno);
974 }
975 record_beneath_to_store_registers (record_beneath_to_store_registers_ops,
976 regcache, regno);
977}
978
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. */
982
983static LONGEST
984record_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)
987{
988 if (!record_gdb_operation_disable
989 && (object == TARGET_OBJECT_MEMORY
990 || object == TARGET_OBJECT_RAW_MEMORY) && writebuf)
991 {
992 if (RECORD_IS_REPLAY)
993 {
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;
1000
1001 /* Destroy the record from here forward. */
1002 record_list_release_next ();
1003 }
1004
1005 /* Check record_insn_num */
1006 record_check_insn_num (0);
1007
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))
1012 {
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;
1019 }
1020 if (record_arch_list_add_end ())
1021 {
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;
1028 }
1029 record_list->next = record_arch_list_head;
1030 record_arch_list_head->prev = record_list;
1031 record_list = record_arch_list_tail;
1032
1033 if (record_insn_num == record_insn_max_num && record_insn_max_num)
1034 record_list_release_first ();
1035 else
1036 record_insn_num++;
1037 }
1038
1039 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops,
1040 object, annex, readbuf, writebuf,
1041 offset, len);
1042}
1043
1044/* Behavior is conditional on RECORD_IS_REPLAY.
1045 We will not actually insert or remove breakpoints when replaying,
1046 nor when recording. */
1047
1048static int
1049record_insert_breakpoint (struct bp_target_info *bp_tgt)
1050{
1051 if (!RECORD_IS_REPLAY)
1052 {
1053 struct cleanup *old_cleanups = record_gdb_operation_disable_set ();
1054 int ret = record_beneath_to_insert_breakpoint (bp_tgt);
1055
1056 do_cleanups (old_cleanups);
1057
1058 return ret;
1059 }
1060
1061 return 0;
1062}
1063
1064static int
1065record_remove_breakpoint (struct bp_target_info *bp_tgt)
1066{
1067 if (!RECORD_IS_REPLAY)
1068 {
1069 struct cleanup *old_cleanups = record_gdb_operation_disable_set ();
1070 int ret = record_beneath_to_remove_breakpoint (bp_tgt);
1071
1072 do_cleanups (old_cleanups);
1073
1074 return ret;
1075 }
1076
1077 return 0;
1078}
1079
1080static int
1081record_can_execute_reverse (void)
1082{
1083 return 1;
1084}
1085
1086static void
1087init_record_ops (void)
1088{
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;
1109}
1110
1111static void
1112show_record_debug (struct ui_file *file, int from_tty,
1113 struct cmd_list_element *c, const char *value)
1114{
1115 fprintf_filtered (file, _("Debugging of process record target is %s.\n"),
1116 value);
1117}
1118
1119/* Alias for "target record". */
1120
1121static void
1122cmd_record_start (char *args, int from_tty)
1123{
1124 execute_command ("target record", from_tty);
1125}
1126
1127/* Truncate the record log from the present point
1128 of replay until the end. */
1129
1130static void
1131cmd_record_delete (char *args, int from_tty)
1132{
1133 if (current_target.to_stratum == record_stratum)
1134 {
1135 if (RECORD_IS_REPLAY)
1136 {
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 ();
1141 }
1142 else
1143 printf_unfiltered (_("Already at end of record list.\n"));
1144
1145 }
1146 else
1147 printf_unfiltered (_("Process record is not started.\n"));
1148}
1149
1150/* Implement the "stoprecord" command. */
1151
1152static void
1153cmd_record_stop (char *args, int from_tty)
1154{
1155 if (current_target.to_stratum == record_stratum)
1156 {
1157 if (!record_list || !from_tty || query (_("Delete recorded log and "
1158 "stop recording?")))
1159 unpush_target (&record_ops);
1160 }
1161 else
1162 printf_unfiltered (_("Process record is not started.\n"));
1163}
1164
1165/* Set upper limit of record log size. */
1166
1167static void
1168set_record_insn_max_num (char *args, int from_tty, struct cmd_list_element *c)
1169{
1170 if (record_insn_num > record_insn_max_num && record_insn_max_num)
1171 {
1172 printf_unfiltered (_("Record instructions number is bigger than "
1173 "record instructions max number. Auto delete "
1174 "the first ones?\n"));
1175
1176 while (record_insn_num > record_insn_max_num)
1177 record_list_release_first ();
1178 }
1179}
1180
1181/* Print the current index into the record log (number of insns recorded
1182 so far). */
1183
1184static void
1185show_record_insn_number (char *ignore, int from_tty)
1186{
1187 printf_unfiltered (_("Record instruction number is %d.\n"),
1188 record_insn_num);
1189}
1190
1191static struct cmd_list_element *record_cmdlist, *set_record_cmdlist,
1192 *show_record_cmdlist, *info_record_cmdlist;
1193
1194static void
1195set_record_command (char *args, int from_tty)
1196{
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);
1200}
1201
1202static void
1203show_record_command (char *args, int from_tty)
1204{
1205 cmd_show_list (show_record_cmdlist, from_tty, "");
1206}
1207
1208static void
1209info_record_command (char *args, int from_tty)
1210{
1211 cmd_show_list (info_record_cmdlist, from_tty, "");
1212}
1213
1214void
1215_initialize_record (void)
1216{
1217 /* Init record_first. */
1218 record_first.prev = NULL;
1219 record_first.next = NULL;
1220 record_first.type = record_end;
1221
1222 init_record_ops ();
1223 add_target (&record_ops);
1224
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);
1232
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);
1249
1250
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);
1256
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);
1261
1262 /* Record instructions number limit command. */
1263 add_setshow_boolean_cmd ("stop-at-limit", no_class,
299a410e
EZ
1264 &record_stop_at_limit, _("\
1265Set whether record/replay stops when record/replay buffer becomes full."), _("\
1266Show whether record/replay stops when record/replay buffer becomes full."), _("\
1267Default is ON.\n\
1268When ON, if the record/replay buffer becomes full, ask user what to do.\n\
1269When OFF, if the record/replay buffer becomes full,\n\
1270delete the oldest recorded instruction to make room for each new one."),
69d05d38
HZ
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."),
299a410e
EZ
1276 _("Show record/replay buffer limit."), _("\
1277Set the maximum number of instructions to be stored in the\n\
1278record/replay buffer. Zero means unlimited. Default is 200000."),
69d05d38
HZ
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);
1284}