650f2a1399e3a75981d9ec66abc1312e4765c959
[deliverable/binutils-gdb.git] / gdb / record.c
1 /* Process record and replay target for GDB, the GNU debugger.
2
3 Copyright (C) 2008, 2009 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "gdbcmd.h"
22 #include "regcache.h"
23 #include "gdbthread.h"
24 #include "event-top.h"
25 #include "exceptions.h"
26 #include "record.h"
27
28 #include <signal.h>
29
30 #define DEFAULT_RECORD_INSN_MAX_NUM 200000
31
32 #define RECORD_IS_REPLAY \
33 (record_list->next || execution_direction == EXEC_REVERSE)
34
35 /* These are the core 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
44 struct record_reg_entry
45 {
46 int num;
47 gdb_byte *val;
48 };
49
50 struct record_mem_entry
51 {
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;
58 };
59
60 enum record_type
61 {
62 record_end = 0,
63 record_reg,
64 record_mem
65 };
66
67 struct record_entry
68 {
69 struct record_entry *prev;
70 struct record_entry *next;
71 enum record_type type;
72 union
73 {
74 /* reg */
75 struct record_reg_entry reg;
76 /* mem */
77 struct record_mem_entry mem;
78 } u;
79 };
80
81 /* This is the debug switch for process record. */
82 int record_debug = 0;
83
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;
89
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;
94
95 /* The target_ops of process record. */
96 static struct target_ops record_ops;
97
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 *);
122
123 static void
124 record_list_release (struct record_entry *rec)
125 {
126 struct record_entry *tmp;
127
128 if (!rec)
129 return;
130
131 while (rec->next)
132 {
133 rec = rec->next;
134 }
135
136 while (rec->prev)
137 {
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);
145 }
146
147 if (rec != &record_first)
148 xfree (rec);
149 }
150
151 static void
152 record_list_release_next (void)
153 {
154 struct record_entry *rec = record_list;
155 struct record_entry *tmp = rec->next;
156 rec->next = NULL;
157 while (tmp)
158 {
159 rec = tmp->next;
160 if (tmp->type == record_end)
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;
168 }
169 }
170
171 static void
172 record_list_release_first (void)
173 {
174 struct record_entry *tmp = NULL;
175 enum record_type type;
176
177 if (!record_first.next)
178 return;
179
180 while (1)
181 {
182 type = record_first.next->type;
183
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);
191
192 if (!record_first.next)
193 {
194 gdb_assert (record_insn_num == 1);
195 break;
196 }
197
198 record_first.next->prev = &record_first;
199
200 if (type == record_end)
201 break;
202 }
203
204 record_insn_num--;
205 }
206
207 /* Add a struct record_entry to record_arch_list. */
208
209 static void
210 record_arch_list_add (struct record_entry *rec)
211 {
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));
216
217 if (record_arch_list_tail)
218 {
219 record_arch_list_tail->next = rec;
220 rec->prev = record_arch_list_tail;
221 record_arch_list_tail = rec;
222 }
223 else
224 {
225 record_arch_list_head = rec;
226 record_arch_list_tail = rec;
227 }
228 }
229
230 /* Record the value of a register NUM to record_arch_list. */
231
232 int
233 record_arch_list_add_reg (struct regcache *regcache, int num)
234 {
235 struct record_entry *rec;
236
237 if (record_debug > 1)
238 fprintf_unfiltered (gdb_stdlog,
239 "Process record: add register num = %d to "
240 "record list.\n",
241 num);
242
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;
249
250 regcache_raw_read (regcache, num, rec->u.reg.val);
251
252 record_arch_list_add (rec);
253
254 return 0;
255 }
256
257 /* Record the value of a region of memory whose address is ADDR and
258 length is LEN to record_arch_list. */
259
260 int
261 record_arch_list_add_mem (CORE_ADDR addr, int len)
262 {
263 struct record_entry *rec;
264
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);
270
271 if (!addr)
272 return 0;
273
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;
282
283 if (target_read_memory (addr, rec->u.mem.val, len))
284 {
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;
293 }
294
295 record_arch_list_add (rec);
296
297 return 0;
298 }
299
300 /* Add a record_end type struct record_entry to record_arch_list. */
301
302 int
303 record_arch_list_add_end (void)
304 {
305 struct record_entry *rec;
306
307 if (record_debug > 1)
308 fprintf_unfiltered (gdb_stdlog,
309 "Process record: add end to arch list.\n");
310
311 rec = (struct record_entry *) xmalloc (sizeof (struct record_entry));
312 rec->prev = NULL;
313 rec->next = NULL;
314 rec->type = record_end;
315
316 record_arch_list_add (rec);
317
318 return 0;
319 }
320
321 static void
322 record_check_insn_num (int set_terminal)
323 {
324 if (record_insn_max_num)
325 {
326 gdb_assert (record_insn_num <= record_insn_max_num);
327 if (record_insn_num == record_insn_max_num)
328 {
329 /* Ask user what to do. */
330 if (record_stop_at_limit)
331 {
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."));
344 }
345 }
346 }
347 }
348
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. */
354
355 static void
356 record_message_cleanups (void *ignore)
357 {
358 record_list_release (record_arch_list_tail);
359 }
360
361 static int
362 record_message (void *args)
363 {
364 int ret;
365 struct regcache *regcache = args;
366 struct cleanup *old_cleanups = make_cleanup (record_message_cleanups, 0);
367
368 record_arch_list_head = NULL;
369 record_arch_list_tail = NULL;
370
371 /* Check record_insn_num. */
372 record_check_insn_num (1);
373
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."));
381
382 discard_cleanups (old_cleanups);
383
384 record_list->next = record_arch_list_head;
385 record_arch_list_head->prev = record_list;
386 record_list = record_arch_list_tail;
387
388 if (record_insn_num == record_insn_max_num && record_insn_max_num)
389 record_list_release_first ();
390 else
391 record_insn_num++;
392
393 return 1;
394 }
395
396 static int
397 do_record_message (struct regcache *regcache)
398 {
399 return catch_errors (record_message, regcache, NULL, RETURN_MASK_ALL);
400 }
401
402 /* Set to 1 if record_store_registers and record_xfer_partial
403 doesn't need record. */
404
405 static int record_gdb_operation_disable = 0;
406
407 struct cleanup *
408 record_gdb_operation_disable_set (void)
409 {
410 struct cleanup *old_cleanups = NULL;
411
412 old_cleanups =
413 make_cleanup_restore_integer (&record_gdb_operation_disable);
414 record_gdb_operation_disable = 1;
415
416 return old_cleanups;
417 }
418
419 static void
420 record_open (char *name, int from_tty)
421 {
422 struct target_ops *t;
423
424 if (record_debug)
425 fprintf_unfiltered (gdb_stdlog, "Process record: record_open\n");
426
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)."));
436
437 if (!gdbarch_process_record_p (target_gdbarch))
438 error (_("Process record: the current architecture doesn't support "
439 "record function."));
440
441 /* Check if record target is already running. */
442 if (current_target.to_stratum == record_stratum)
443 {
444 if (!nquery
445 (_("Process record target already running, do you want to delete "
446 "the old record log?")))
447 return;
448 }
449
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;
457
458 /* Set the beneath function pointers. */
459 for (t = current_target.beneath; t != NULL; t = t->beneath)
460 {
461 if (!record_beneath_to_resume)
462 {
463 record_beneath_to_resume = t->to_resume;
464 record_beneath_to_resume_ops = t;
465 }
466 if (!record_beneath_to_wait)
467 {
468 record_beneath_to_wait = t->to_wait;
469 record_beneath_to_wait_ops = t;
470 }
471 if (!record_beneath_to_store_registers)
472 {
473 record_beneath_to_store_registers = t->to_store_registers;
474 record_beneath_to_store_registers_ops = t;
475 }
476 if (!record_beneath_to_xfer_partial)
477 {
478 record_beneath_to_xfer_partial = t->to_xfer_partial;
479 record_beneath_to_xfer_partial_ops = t;
480 }
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;
485 }
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."));
498
499 push_target (&record_ops);
500
501 /* Reset */
502 record_insn_num = 0;
503 record_list = &record_first;
504 record_list->next = NULL;
505 }
506
507 static void
508 record_close (int quitting)
509 {
510 if (record_debug)
511 fprintf_unfiltered (gdb_stdlog, "Process record: record_close\n");
512
513 record_list_release (record_list);
514 }
515
516 static int record_resume_step = 0;
517 static int record_resume_error;
518
519 static void
520 record_resume (struct target_ops *ops, ptid_t ptid, int step,
521 enum target_signal siggnal)
522 {
523 record_resume_step = step;
524
525 if (!RECORD_IS_REPLAY)
526 {
527 if (do_record_message (get_current_regcache ()))
528 {
529 record_resume_error = 0;
530 }
531 else
532 {
533 record_resume_error = 1;
534 return;
535 }
536 record_beneath_to_resume (record_beneath_to_resume_ops, ptid, 1,
537 siggnal);
538 }
539 }
540
541 static int record_get_sig = 0;
542
543 static void
544 record_sig_handler (int signo)
545 {
546 if (record_debug)
547 fprintf_unfiltered (gdb_stdlog, "Process record: get a signal\n");
548
549 /* It will break the running inferior in replay mode. */
550 record_resume_step = 1;
551
552 /* It will let record_wait set inferior status to get the signal
553 SIGINT. */
554 record_get_sig = 1;
555 }
556
557 static void
558 record_wait_cleanups (void *ignore)
559 {
560 if (execution_direction == EXEC_REVERSE)
561 {
562 if (record_list->next)
563 record_list = record_list->next;
564 }
565 else
566 record_list = record_list->prev;
567 }
568
569 /* In replay mode, this function examines the recorded log and
570 determines where to stop. */
571
572 static ptid_t
573 record_wait (struct target_ops *ops,
574 ptid_t ptid, struct target_waitstatus *status,
575 int options)
576 {
577 struct cleanup *set_cleanups = record_gdb_operation_disable_set ();
578
579 if (record_debug)
580 fprintf_unfiltered (gdb_stdlog,
581 "Process record: record_wait "
582 "record_resume_step = %d\n",
583 record_resume_step);
584
585 if (!RECORD_IS_REPLAY)
586 {
587 if (record_resume_error)
588 {
589 /* If record_resume get error, return directly. */
590 status->kind = TARGET_WAITKIND_STOPPED;
591 status->value.sig = TARGET_SIGNAL_ABRT;
592 return inferior_ptid;
593 }
594
595 if (record_resume_step)
596 {
597 /* This is a single step. */
598 return record_beneath_to_wait (record_beneath_to_wait_ops,
599 ptid, status, options);
600 }
601 else
602 {
603 /* This is not a single step. */
604 ptid_t ret;
605 CORE_ADDR tmp_pc;
606
607 while (1)
608 {
609 ret = record_beneath_to_wait (record_beneath_to_wait_ops,
610 ptid, status, options);
611
612 if (status->kind == TARGET_WAITKIND_STOPPED
613 && status->value.sig == TARGET_SIGNAL_TRAP)
614 {
615 /* Check if there is a breakpoint. */
616 registers_changed ();
617 tmp_pc = regcache_read_pc (get_current_regcache ());
618 if (breakpoint_inserted_here_p (tmp_pc))
619 {
620 /* There is a breakpoint. */
621 CORE_ADDR decr_pc_after_break =
622 gdbarch_decr_pc_after_break
623 (get_regcache_arch (get_current_regcache ()));
624 if (decr_pc_after_break)
625 {
626 regcache_write_pc (get_thread_regcache (ret),
627 tmp_pc + decr_pc_after_break);
628 }
629 }
630 else
631 {
632 /* There is not a breakpoint. */
633 if (!do_record_message (get_current_regcache ()))
634 {
635 break;
636 }
637 record_beneath_to_resume (record_beneath_to_resume_ops,
638 ptid, 1,
639 TARGET_SIGNAL_0);
640 continue;
641 }
642 }
643
644 /* The inferior is broken by a breakpoint or a signal. */
645 break;
646 }
647
648 return ret;
649 }
650 }
651 else
652 {
653 struct regcache *regcache = get_current_regcache ();
654 struct gdbarch *gdbarch = get_regcache_arch (regcache);
655 int continue_flag = 1;
656 int first_record_end = 1;
657 struct cleanup *old_cleanups = make_cleanup (record_wait_cleanups, 0);
658 CORE_ADDR tmp_pc;
659
660 status->kind = TARGET_WAITKIND_STOPPED;
661
662 /* Check breakpoint when forward execute. */
663 if (execution_direction == EXEC_FORWARD)
664 {
665 tmp_pc = regcache_read_pc (regcache);
666 if (breakpoint_inserted_here_p (tmp_pc))
667 {
668 if (record_debug)
669 fprintf_unfiltered (gdb_stdlog,
670 "Process record: break at %s.\n",
671 paddress (gdbarch, tmp_pc));
672 if (gdbarch_decr_pc_after_break (gdbarch)
673 && !record_resume_step)
674 regcache_write_pc (regcache,
675 tmp_pc +
676 gdbarch_decr_pc_after_break (gdbarch));
677 goto replay_out;
678 }
679 }
680
681 record_get_sig = 0;
682 signal (SIGINT, record_sig_handler);
683 /* If GDB is in terminal_inferior mode, it will not get the signal.
684 And in GDB replay mode, GDB doesn't need to be in terminal_inferior
685 mode, because inferior will not executed.
686 Then set it to terminal_ours to make GDB get the signal. */
687 target_terminal_ours ();
688
689 /* In EXEC_FORWARD mode, record_list points to the tail of prev
690 instruction. */
691 if (execution_direction == EXEC_FORWARD && record_list->next)
692 record_list = record_list->next;
693
694 /* Loop over the record_list, looking for the next place to
695 stop. */
696 do
697 {
698 /* Check for beginning and end of log. */
699 if (execution_direction == EXEC_REVERSE
700 && record_list == &record_first)
701 {
702 /* Hit beginning of record log in reverse. */
703 status->kind = TARGET_WAITKIND_NO_HISTORY;
704 break;
705 }
706 if (execution_direction != EXEC_REVERSE && !record_list->next)
707 {
708 /* Hit end of record log going forward. */
709 status->kind = TARGET_WAITKIND_NO_HISTORY;
710 break;
711 }
712
713 /* Set ptid, register and memory according to record_list. */
714 if (record_list->type == record_reg)
715 {
716 /* reg */
717 gdb_byte reg[MAX_REGISTER_SIZE];
718 if (record_debug > 1)
719 fprintf_unfiltered (gdb_stdlog,
720 "Process record: record_reg %s to "
721 "inferior num = %d.\n",
722 host_address_to_string (record_list),
723 record_list->u.reg.num);
724 regcache_cooked_read (regcache, record_list->u.reg.num, reg);
725 regcache_cooked_write (regcache, record_list->u.reg.num,
726 record_list->u.reg.val);
727 memcpy (record_list->u.reg.val, reg, MAX_REGISTER_SIZE);
728 }
729 else if (record_list->type == record_mem)
730 {
731 /* mem */
732 /* Nothing to do if the entry is flagged not_accessible. */
733 if (!record_list->u.mem.mem_entry_not_accessible)
734 {
735 gdb_byte *mem = alloca (record_list->u.mem.len);
736 if (record_debug > 1)
737 fprintf_unfiltered (gdb_stdlog,
738 "Process record: record_mem %s to "
739 "inferior addr = %s len = %d.\n",
740 host_address_to_string (record_list),
741 paddress (gdbarch,
742 record_list->u.mem.addr),
743 record_list->u.mem.len);
744
745 if (target_read_memory (record_list->u.mem.addr, mem,
746 record_list->u.mem.len))
747 {
748 if (execution_direction != EXEC_REVERSE)
749 error (_("Process record: error reading memory at "
750 "addr = %s len = %d."),
751 paddress (gdbarch, record_list->u.mem.addr),
752 record_list->u.mem.len);
753 else
754 /* Read failed --
755 flag entry as not_accessible. */
756 record_list->u.mem.mem_entry_not_accessible = 1;
757 }
758 else
759 {
760 if (target_write_memory (record_list->u.mem.addr,
761 record_list->u.mem.val,
762 record_list->u.mem.len))
763 {
764 if (execution_direction != EXEC_REVERSE)
765 error (_("Process record: error writing memory at "
766 "addr = %s len = %d."),
767 paddress (gdbarch, record_list->u.mem.addr),
768 record_list->u.mem.len);
769 else
770 /* Write failed --
771 flag entry as not_accessible. */
772 record_list->u.mem.mem_entry_not_accessible = 1;
773 }
774 else
775 {
776 memcpy (record_list->u.mem.val, mem,
777 record_list->u.mem.len);
778 }
779 }
780 }
781 }
782 else
783 {
784 if (record_debug > 1)
785 fprintf_unfiltered (gdb_stdlog,
786 "Process record: record_end %s to "
787 "inferior.\n",
788 host_address_to_string (record_list));
789
790 if (first_record_end && execution_direction == EXEC_REVERSE)
791 {
792 /* When reverse excute, the first record_end is the part of
793 current instruction. */
794 first_record_end = 0;
795 }
796 else
797 {
798 /* In EXEC_REVERSE mode, this is the record_end of prev
799 instruction.
800 In EXEC_FORWARD mode, this is the record_end of current
801 instruction. */
802 /* step */
803 if (record_resume_step)
804 {
805 if (record_debug > 1)
806 fprintf_unfiltered (gdb_stdlog,
807 "Process record: step.\n");
808 continue_flag = 0;
809 }
810
811 /* check breakpoint */
812 tmp_pc = regcache_read_pc (regcache);
813 if (breakpoint_inserted_here_p (tmp_pc))
814 {
815 if (record_debug)
816 fprintf_unfiltered (gdb_stdlog,
817 "Process record: break "
818 "at %s.\n",
819 paddress (gdbarch, tmp_pc));
820 if (gdbarch_decr_pc_after_break (gdbarch)
821 && execution_direction == EXEC_FORWARD
822 && !record_resume_step)
823 regcache_write_pc (regcache,
824 tmp_pc +
825 gdbarch_decr_pc_after_break (gdbarch));
826 continue_flag = 0;
827 }
828 }
829 }
830
831 if (continue_flag)
832 {
833 if (execution_direction == EXEC_REVERSE)
834 {
835 if (record_list->prev)
836 record_list = record_list->prev;
837 }
838 else
839 {
840 if (record_list->next)
841 record_list = record_list->next;
842 }
843 }
844 }
845 while (continue_flag);
846
847 signal (SIGINT, handle_sigint);
848
849 replay_out:
850 if (record_get_sig)
851 status->value.sig = TARGET_SIGNAL_INT;
852 else
853 status->value.sig = TARGET_SIGNAL_TRAP;
854
855 discard_cleanups (old_cleanups);
856 }
857
858 do_cleanups (set_cleanups);
859 return inferior_ptid;
860 }
861
862 static void
863 record_disconnect (struct target_ops *target, char *args, int from_tty)
864 {
865 if (record_debug)
866 fprintf_unfiltered (gdb_stdlog, "Process record: record_disconnect\n");
867
868 unpush_target (&record_ops);
869 target_disconnect (args, from_tty);
870 }
871
872 static void
873 record_detach (struct target_ops *ops, char *args, int from_tty)
874 {
875 if (record_debug)
876 fprintf_unfiltered (gdb_stdlog, "Process record: record_detach\n");
877
878 unpush_target (&record_ops);
879 target_detach (args, from_tty);
880 }
881
882 static void
883 record_mourn_inferior (struct target_ops *ops)
884 {
885 if (record_debug)
886 fprintf_unfiltered (gdb_stdlog, "Process record: "
887 "record_mourn_inferior\n");
888
889 unpush_target (&record_ops);
890 target_mourn_inferior ();
891 }
892
893 /* Close process record target before killing the inferior process. */
894
895 static void
896 record_kill (struct target_ops *ops)
897 {
898 if (record_debug)
899 fprintf_unfiltered (gdb_stdlog, "Process record: record_kill\n");
900
901 unpush_target (&record_ops);
902 target_kill ();
903 }
904
905 /* Record registers change (by user or by GDB) to list as an instruction. */
906
907 static void
908 record_registers_change (struct regcache *regcache, int regnum)
909 {
910 /* Check record_insn_num. */
911 record_check_insn_num (0);
912
913 record_arch_list_head = NULL;
914 record_arch_list_tail = NULL;
915
916 if (regnum < 0)
917 {
918 int i;
919 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
920 {
921 if (record_arch_list_add_reg (regcache, i))
922 {
923 record_list_release (record_arch_list_tail);
924 error (_("Process record: failed to record execution log."));
925 }
926 }
927 }
928 else
929 {
930 if (record_arch_list_add_reg (regcache, regnum))
931 {
932 record_list_release (record_arch_list_tail);
933 error (_("Process record: failed to record execution log."));
934 }
935 }
936 if (record_arch_list_add_end ())
937 {
938 record_list_release (record_arch_list_tail);
939 error (_("Process record: failed to record execution log."));
940 }
941 record_list->next = record_arch_list_head;
942 record_arch_list_head->prev = record_list;
943 record_list = record_arch_list_tail;
944
945 if (record_insn_num == record_insn_max_num && record_insn_max_num)
946 record_list_release_first ();
947 else
948 record_insn_num++;
949 }
950
951 static void
952 record_store_registers (struct target_ops *ops, struct regcache *regcache,
953 int regno)
954 {
955 if (!record_gdb_operation_disable)
956 {
957 if (RECORD_IS_REPLAY)
958 {
959 int n;
960
961 /* Let user choose if he wants to write register or not. */
962 if (regno < 0)
963 n =
964 nquery (_("Because GDB is in replay mode, changing the "
965 "value of a register will make the execution "
966 "log unusable from this point onward. "
967 "Change all registers?"));
968 else
969 n =
970 nquery (_("Because GDB is in replay mode, changing the value "
971 "of a register will make the execution log unusable "
972 "from this point onward. Change register %s?"),
973 gdbarch_register_name (get_regcache_arch (regcache),
974 regno));
975
976 if (!n)
977 {
978 /* Invalidate the value of regcache that was set in function
979 "regcache_raw_write". */
980 if (regno < 0)
981 {
982 int i;
983 for (i = 0;
984 i < gdbarch_num_regs (get_regcache_arch (regcache));
985 i++)
986 regcache_invalidate (regcache, i);
987 }
988 else
989 regcache_invalidate (regcache, regno);
990
991 error (_("Process record canceled the operation."));
992 }
993
994 /* Destroy the record from here forward. */
995 record_list_release_next ();
996 }
997
998 record_registers_change (regcache, regno);
999 }
1000 record_beneath_to_store_registers (record_beneath_to_store_registers_ops,
1001 regcache, regno);
1002 }
1003
1004 /* Behavior is conditional on RECORD_IS_REPLAY.
1005 In replay mode, we cannot write memory unles we are willing to
1006 invalidate the record/replay log from this point forward. */
1007
1008 static LONGEST
1009 record_xfer_partial (struct target_ops *ops, enum target_object object,
1010 const char *annex, gdb_byte *readbuf,
1011 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
1012 {
1013 if (!record_gdb_operation_disable
1014 && (object == TARGET_OBJECT_MEMORY
1015 || object == TARGET_OBJECT_RAW_MEMORY) && writebuf)
1016 {
1017 if (RECORD_IS_REPLAY)
1018 {
1019 /* Let user choose if he wants to write memory or not. */
1020 if (!nquery (_("Because GDB is in replay mode, writing to memory "
1021 "will make the execution log unusable from this "
1022 "point onward. Write memory at address %s?"),
1023 paddress (target_gdbarch, offset)))
1024 error (_("Process record canceled the operation."));
1025
1026 /* Destroy the record from here forward. */
1027 record_list_release_next ();
1028 }
1029
1030 /* Check record_insn_num */
1031 record_check_insn_num (0);
1032
1033 /* Record registers change to list as an instruction. */
1034 record_arch_list_head = NULL;
1035 record_arch_list_tail = NULL;
1036 if (record_arch_list_add_mem (offset, len))
1037 {
1038 record_list_release (record_arch_list_tail);
1039 if (record_debug)
1040 fprintf_unfiltered (gdb_stdlog,
1041 _("Process record: failed to record "
1042 "execution log."));
1043 return -1;
1044 }
1045 if (record_arch_list_add_end ())
1046 {
1047 record_list_release (record_arch_list_tail);
1048 if (record_debug)
1049 fprintf_unfiltered (gdb_stdlog,
1050 _("Process record: failed to record "
1051 "execution log."));
1052 return -1;
1053 }
1054 record_list->next = record_arch_list_head;
1055 record_arch_list_head->prev = record_list;
1056 record_list = record_arch_list_tail;
1057
1058 if (record_insn_num == record_insn_max_num && record_insn_max_num)
1059 record_list_release_first ();
1060 else
1061 record_insn_num++;
1062 }
1063
1064 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops,
1065 object, annex, readbuf, writebuf,
1066 offset, len);
1067 }
1068
1069 /* Behavior is conditional on RECORD_IS_REPLAY.
1070 We will not actually insert or remove breakpoints when replaying,
1071 nor when recording. */
1072
1073 static int
1074 record_insert_breakpoint (struct gdbarch *gdbarch,
1075 struct bp_target_info *bp_tgt)
1076 {
1077 if (!RECORD_IS_REPLAY)
1078 {
1079 struct cleanup *old_cleanups = record_gdb_operation_disable_set ();
1080 int ret = record_beneath_to_insert_breakpoint (gdbarch, bp_tgt);
1081
1082 do_cleanups (old_cleanups);
1083
1084 return ret;
1085 }
1086
1087 return 0;
1088 }
1089
1090 static int
1091 record_remove_breakpoint (struct gdbarch *gdbarch,
1092 struct bp_target_info *bp_tgt)
1093 {
1094 if (!RECORD_IS_REPLAY)
1095 {
1096 struct cleanup *old_cleanups = record_gdb_operation_disable_set ();
1097 int ret = record_beneath_to_remove_breakpoint (gdbarch, bp_tgt);
1098
1099 do_cleanups (old_cleanups);
1100
1101 return ret;
1102 }
1103
1104 return 0;
1105 }
1106
1107 static int
1108 record_can_execute_reverse (void)
1109 {
1110 return 1;
1111 }
1112
1113 static void
1114 init_record_ops (void)
1115 {
1116 record_ops.to_shortname = "record";
1117 record_ops.to_longname = "Process record and replay target";
1118 record_ops.to_doc =
1119 "Log program while executing and replay execution from log.";
1120 record_ops.to_open = record_open;
1121 record_ops.to_close = record_close;
1122 record_ops.to_resume = record_resume;
1123 record_ops.to_wait = record_wait;
1124 record_ops.to_disconnect = record_disconnect;
1125 record_ops.to_detach = record_detach;
1126 record_ops.to_mourn_inferior = record_mourn_inferior;
1127 record_ops.to_kill = record_kill;
1128 record_ops.to_create_inferior = find_default_create_inferior;
1129 record_ops.to_store_registers = record_store_registers;
1130 record_ops.to_xfer_partial = record_xfer_partial;
1131 record_ops.to_insert_breakpoint = record_insert_breakpoint;
1132 record_ops.to_remove_breakpoint = record_remove_breakpoint;
1133 record_ops.to_can_execute_reverse = record_can_execute_reverse;
1134 record_ops.to_stratum = record_stratum;
1135 record_ops.to_magic = OPS_MAGIC;
1136 }
1137
1138 static void
1139 show_record_debug (struct ui_file *file, int from_tty,
1140 struct cmd_list_element *c, const char *value)
1141 {
1142 fprintf_filtered (file, _("Debugging of process record target is %s.\n"),
1143 value);
1144 }
1145
1146 /* Alias for "target record". */
1147
1148 static void
1149 cmd_record_start (char *args, int from_tty)
1150 {
1151 execute_command ("target record", from_tty);
1152 }
1153
1154 /* Truncate the record log from the present point
1155 of replay until the end. */
1156
1157 static void
1158 cmd_record_delete (char *args, int from_tty)
1159 {
1160 if (current_target.to_stratum == record_stratum)
1161 {
1162 if (RECORD_IS_REPLAY)
1163 {
1164 if (!from_tty || query (_("Delete the log from this point forward "
1165 "and begin to record the running message "
1166 "at current PC?")))
1167 record_list_release_next ();
1168 }
1169 else
1170 printf_unfiltered (_("Already at end of record list.\n"));
1171
1172 }
1173 else
1174 printf_unfiltered (_("Process record is not started.\n"));
1175 }
1176
1177 /* Implement the "stoprecord" command. */
1178
1179 static void
1180 cmd_record_stop (char *args, int from_tty)
1181 {
1182 if (current_target.to_stratum == record_stratum)
1183 {
1184 if (!record_list || !from_tty || query (_("Delete recorded log and "
1185 "stop recording?")))
1186 unpush_target (&record_ops);
1187 }
1188 else
1189 printf_unfiltered (_("Process record is not started.\n"));
1190 }
1191
1192 /* Set upper limit of record log size. */
1193
1194 static void
1195 set_record_insn_max_num (char *args, int from_tty, struct cmd_list_element *c)
1196 {
1197 if (record_insn_num > record_insn_max_num && record_insn_max_num)
1198 {
1199 printf_unfiltered (_("Record instructions number is bigger than "
1200 "record instructions max number. Auto delete "
1201 "the first ones?\n"));
1202
1203 while (record_insn_num > record_insn_max_num)
1204 record_list_release_first ();
1205 }
1206 }
1207
1208 /* Print the current index into the record log (number of insns recorded
1209 so far). */
1210
1211 static void
1212 show_record_insn_number (char *ignore, int from_tty)
1213 {
1214 printf_unfiltered (_("Record instruction number is %d.\n"),
1215 record_insn_num);
1216 }
1217
1218 static struct cmd_list_element *record_cmdlist, *set_record_cmdlist,
1219 *show_record_cmdlist, *info_record_cmdlist;
1220
1221 static void
1222 set_record_command (char *args, int from_tty)
1223 {
1224 printf_unfiltered (_("\
1225 \"set record\" must be followed by an apporpriate subcommand.\n"));
1226 help_list (set_record_cmdlist, "set record ", all_commands, gdb_stdout);
1227 }
1228
1229 static void
1230 show_record_command (char *args, int from_tty)
1231 {
1232 cmd_show_list (show_record_cmdlist, from_tty, "");
1233 }
1234
1235 static void
1236 info_record_command (char *args, int from_tty)
1237 {
1238 cmd_show_list (info_record_cmdlist, from_tty, "");
1239 }
1240
1241 void
1242 _initialize_record (void)
1243 {
1244 /* Init record_first. */
1245 record_first.prev = NULL;
1246 record_first.next = NULL;
1247 record_first.type = record_end;
1248
1249 init_record_ops ();
1250 add_target (&record_ops);
1251
1252 add_setshow_zinteger_cmd ("record", no_class, &record_debug,
1253 _("Set debugging of record/replay feature."),
1254 _("Show debugging of record/replay feature."),
1255 _("When enabled, debugging output for "
1256 "record/replay feature is displayed."),
1257 NULL, show_record_debug, &setdebuglist,
1258 &showdebuglist);
1259
1260 add_prefix_cmd ("record", class_obscure, cmd_record_start,
1261 _("Abbreviated form of \"target record\" command."),
1262 &record_cmdlist, "record ", 0, &cmdlist);
1263 add_com_alias ("rec", "record", class_obscure, 1);
1264 add_prefix_cmd ("record", class_support, set_record_command,
1265 _("Set record options"), &set_record_cmdlist,
1266 "set record ", 0, &setlist);
1267 add_alias_cmd ("rec", "record", class_obscure, 1, &setlist);
1268 add_prefix_cmd ("record", class_support, show_record_command,
1269 _("Show record options"), &show_record_cmdlist,
1270 "show record ", 0, &showlist);
1271 add_alias_cmd ("rec", "record", class_obscure, 1, &showlist);
1272 add_prefix_cmd ("record", class_support, info_record_command,
1273 _("Info record options"), &info_record_cmdlist,
1274 "info record ", 0, &infolist);
1275 add_alias_cmd ("rec", "record", class_obscure, 1, &infolist);
1276
1277
1278 add_cmd ("delete", class_obscure, cmd_record_delete,
1279 _("Delete the rest of execution log and start recording it anew."),
1280 &record_cmdlist);
1281 add_alias_cmd ("d", "delete", class_obscure, 1, &record_cmdlist);
1282 add_alias_cmd ("del", "delete", class_obscure, 1, &record_cmdlist);
1283
1284 add_cmd ("stop", class_obscure, cmd_record_stop,
1285 _("Stop the record/replay target."),
1286 &record_cmdlist);
1287 add_alias_cmd ("s", "stop", class_obscure, 1, &record_cmdlist);
1288
1289 /* Record instructions number limit command. */
1290 add_setshow_boolean_cmd ("stop-at-limit", no_class,
1291 &record_stop_at_limit, _("\
1292 Set whether record/replay stops when record/replay buffer becomes full."), _("\
1293 Show whether record/replay stops when record/replay buffer becomes full."), _("\
1294 Default is ON.\n\
1295 When ON, if the record/replay buffer becomes full, ask user what to do.\n\
1296 When OFF, if the record/replay buffer becomes full,\n\
1297 delete the oldest recorded instruction to make room for each new one."),
1298 NULL, NULL,
1299 &set_record_cmdlist, &show_record_cmdlist);
1300 add_setshow_zinteger_cmd ("insn-number-max", no_class,
1301 &record_insn_max_num,
1302 _("Set record/replay buffer limit."),
1303 _("Show record/replay buffer limit."), _("\
1304 Set the maximum number of instructions to be stored in the\n\
1305 record/replay buffer. Zero means unlimited. Default is 200000."),
1306 set_record_insn_max_num,
1307 NULL, &set_record_cmdlist, &show_record_cmdlist);
1308 add_cmd ("insn-number", class_obscure, show_record_insn_number,
1309 _("Show the current number of instructions in the "
1310 "record/replay buffer."), &info_record_cmdlist);
1311 }
This page took 0.056763 seconds and 4 git commands to generate.