2009-08-04 Hui Zhu <teawater@gmail.com>
[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_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;
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 enum target_signal record_resume_siggnal;
518 static int record_resume_error;
519
520 static void
521 record_resume (struct target_ops *ops, ptid_t ptid, int step,
522 enum target_signal siggnal)
523 {
524 record_resume_step = step;
525 record_resume_siggnal = siggnal;
526
527 if (!RECORD_IS_REPLAY)
528 {
529 if (do_record_message (get_current_regcache ()))
530 {
531 record_resume_error = 0;
532 }
533 else
534 {
535 record_resume_error = 1;
536 return;
537 }
538 record_beneath_to_resume (record_beneath_to_resume_ops, ptid, 1,
539 siggnal);
540 }
541 }
542
543 static int record_get_sig = 0;
544
545 static void
546 record_sig_handler (int signo)
547 {
548 if (record_debug)
549 fprintf_unfiltered (gdb_stdlog, "Process record: get a signal\n");
550
551 /* It will break the running inferior in replay mode. */
552 record_resume_step = 1;
553
554 /* It will let record_wait set inferior status to get the signal
555 SIGINT. */
556 record_get_sig = 1;
557 }
558
559 static void
560 record_wait_cleanups (void *ignore)
561 {
562 if (execution_direction == EXEC_REVERSE)
563 {
564 if (record_list->next)
565 record_list = record_list->next;
566 }
567 else
568 record_list = record_list->prev;
569 }
570
571 /* In replay mode, this function examines the recorded log and
572 determines where to stop. */
573
574 static ptid_t
575 record_wait (struct target_ops *ops,
576 ptid_t ptid, struct target_waitstatus *status,
577 int options)
578 {
579 struct cleanup *set_cleanups = record_gdb_operation_disable_set ();
580
581 if (record_debug)
582 fprintf_unfiltered (gdb_stdlog,
583 "Process record: record_wait "
584 "record_resume_step = %d\n",
585 record_resume_step);
586
587 if (!RECORD_IS_REPLAY)
588 {
589 if (record_resume_error)
590 {
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;
595 }
596
597 if (record_resume_step)
598 {
599 /* This is a single step. */
600 return record_beneath_to_wait (record_beneath_to_wait_ops,
601 ptid, status, 0);
602 }
603 else
604 {
605 /* This is not a single step. */
606 ptid_t ret;
607 CORE_ADDR tmp_pc;
608
609 while (1)
610 {
611 ret = record_beneath_to_wait (record_beneath_to_wait_ops,
612 ptid, status, 0);
613
614 if (status->kind == TARGET_WAITKIND_STOPPED
615 && status->value.sig == TARGET_SIGNAL_TRAP)
616 {
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))
621 {
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)
627 {
628 regcache_write_pc (get_thread_regcache (ret),
629 tmp_pc + decr_pc_after_break);
630 }
631 }
632 else
633 {
634 /* There is not a breakpoint. */
635 if (!do_record_message (get_current_regcache ()))
636 {
637 break;
638 }
639 record_beneath_to_resume (record_beneath_to_resume_ops,
640 ptid, 1,
641 record_resume_siggnal);
642 continue;
643 }
644 }
645
646 /* The inferior is broken by a breakpoint or a signal. */
647 break;
648 }
649
650 return ret;
651 }
652 }
653 else
654 {
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;
661
662 status->kind = TARGET_WAITKIND_STOPPED;
663
664 /* Check breakpoint when forward execute. */
665 if (execution_direction == EXEC_FORWARD)
666 {
667 tmp_pc = regcache_read_pc (regcache);
668 if (breakpoint_inserted_here_p (tmp_pc))
669 {
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;
680 }
681 }
682
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 ();
690
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;
695
696 /* Loop over the record_list, looking for the next place to
697 stop. */
698 do
699 {
700 /* Check for beginning and end of log. */
701 if (execution_direction == EXEC_REVERSE
702 && record_list == &record_first)
703 {
704 /* Hit beginning of record log in reverse. */
705 status->kind = TARGET_WAITKIND_NO_HISTORY;
706 break;
707 }
708 if (execution_direction != EXEC_REVERSE && !record_list->next)
709 {
710 /* Hit end of record log going forward. */
711 status->kind = TARGET_WAITKIND_NO_HISTORY;
712 break;
713 }
714
715 /* Set ptid, register and memory according to record_list. */
716 if (record_list->type == record_reg)
717 {
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);
730 }
731 else if (record_list->type == record_mem)
732 {
733 /* mem */
734 /* Nothing to do if the entry is flagged not_accessible. */
735 if (!record_list->u.mem.mem_entry_not_accessible)
736 {
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);
746
747 if (target_read_memory (record_list->u.mem.addr, mem,
748 record_list->u.mem.len))
749 {
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;
759 }
760 else
761 {
762 if (target_write_memory (record_list->u.mem.addr,
763 record_list->u.mem.val,
764 record_list->u.mem.len))
765 {
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;
775 }
776 else
777 {
778 memcpy (record_list->u.mem.val, mem,
779 record_list->u.mem.len);
780 }
781 }
782 }
783 }
784 else
785 {
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));
791
792 if (first_record_end && execution_direction == EXEC_REVERSE)
793 {
794 /* When reverse excute, the first record_end is the part of
795 current instruction. */
796 first_record_end = 0;
797 }
798 else
799 {
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)
806 {
807 if (record_debug > 1)
808 fprintf_unfiltered (gdb_stdlog,
809 "Process record: step.\n");
810 continue_flag = 0;
811 }
812
813 /* check breakpoint */
814 tmp_pc = regcache_read_pc (regcache);
815 if (breakpoint_inserted_here_p (tmp_pc))
816 {
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;
829 }
830 }
831 }
832
833 if (continue_flag)
834 {
835 if (execution_direction == EXEC_REVERSE)
836 {
837 if (record_list->prev)
838 record_list = record_list->prev;
839 }
840 else
841 {
842 if (record_list->next)
843 record_list = record_list->next;
844 }
845 }
846 }
847 while (continue_flag);
848
849 signal (SIGINT, handle_sigint);
850
851 replay_out:
852 if (record_get_sig)
853 status->value.sig = TARGET_SIGNAL_INT;
854 else
855 status->value.sig = TARGET_SIGNAL_TRAP;
856
857 discard_cleanups (old_cleanups);
858 }
859
860 do_cleanups (set_cleanups);
861 return inferior_ptid;
862 }
863
864 static void
865 record_disconnect (struct target_ops *target, char *args, int from_tty)
866 {
867 if (record_debug)
868 fprintf_unfiltered (gdb_stdlog, "Process record: record_disconnect\n");
869
870 unpush_target (&record_ops);
871 target_disconnect (args, from_tty);
872 }
873
874 static void
875 record_detach (struct target_ops *ops, char *args, int from_tty)
876 {
877 if (record_debug)
878 fprintf_unfiltered (gdb_stdlog, "Process record: record_detach\n");
879
880 unpush_target (&record_ops);
881 target_detach (args, from_tty);
882 }
883
884 static void
885 record_mourn_inferior (struct target_ops *ops)
886 {
887 if (record_debug)
888 fprintf_unfiltered (gdb_stdlog, "Process record: "
889 "record_mourn_inferior\n");
890
891 unpush_target (&record_ops);
892 target_mourn_inferior ();
893 }
894
895 /* Close process record target before killing the inferior process. */
896
897 static void
898 record_kill (struct target_ops *ops)
899 {
900 if (record_debug)
901 fprintf_unfiltered (gdb_stdlog, "Process record: record_kill\n");
902
903 unpush_target (&record_ops);
904 target_kill ();
905 }
906
907 /* Record registers change (by user or by GDB) to list as an instruction. */
908
909 static void
910 record_registers_change (struct regcache *regcache, int regnum)
911 {
912 /* Check record_insn_num. */
913 record_check_insn_num (0);
914
915 record_arch_list_head = NULL;
916 record_arch_list_tail = NULL;
917
918 if (regnum < 0)
919 {
920 int i;
921 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
922 {
923 if (record_arch_list_add_reg (regcache, i))
924 {
925 record_list_release (record_arch_list_tail);
926 error (_("Process record: failed to record execution log."));
927 }
928 }
929 }
930 else
931 {
932 if (record_arch_list_add_reg (regcache, regnum))
933 {
934 record_list_release (record_arch_list_tail);
935 error (_("Process record: failed to record execution log."));
936 }
937 }
938 if (record_arch_list_add_end ())
939 {
940 record_list_release (record_arch_list_tail);
941 error (_("Process record: failed to record execution log."));
942 }
943 record_list->next = record_arch_list_head;
944 record_arch_list_head->prev = record_list;
945 record_list = record_arch_list_tail;
946
947 if (record_insn_num == record_insn_max_num && record_insn_max_num)
948 record_list_release_first ();
949 else
950 record_insn_num++;
951 }
952
953 static void
954 record_store_registers (struct target_ops *ops, struct regcache *regcache,
955 int regno)
956 {
957 if (!record_gdb_operation_disable)
958 {
959 if (RECORD_IS_REPLAY)
960 {
961 int n;
962 struct cleanup *old_cleanups;
963
964 /* Let user choose if he wants to write register or not. */
965 if (regno < 0)
966 n =
967 nquery (_("Because GDB is in replay mode, changing the "
968 "value of a register will make the execution "
969 "log unusable from this point onward. "
970 "Change all registers?"));
971 else
972 n =
973 nquery (_("Because GDB is in replay mode, changing the value "
974 "of a register will make the execution log unusable "
975 "from this point onward. Change register %s?"),
976 gdbarch_register_name (get_regcache_arch (regcache),
977 regno));
978
979 if (!n)
980 {
981 /* Invalidate the value of regcache that was set in function
982 "regcache_raw_write". */
983 if (regno < 0)
984 {
985 int i;
986 for (i = 0;
987 i < gdbarch_num_regs (get_regcache_arch (regcache));
988 i++)
989 regcache_invalidate (regcache, i);
990 }
991 else
992 regcache_invalidate (regcache, regno);
993
994 error (_("Process record canceled the operation."));
995 }
996
997 /* Destroy the record from here forward. */
998 record_list_release_next ();
999 }
1000
1001 record_registers_change (regcache, regno);
1002 }
1003 record_beneath_to_store_registers (record_beneath_to_store_registers_ops,
1004 regcache, regno);
1005 }
1006
1007 /* Behavior is conditional on RECORD_IS_REPLAY.
1008 In replay mode, we cannot write memory unles we are willing to
1009 invalidate the record/replay log from this point forward. */
1010
1011 static LONGEST
1012 record_xfer_partial (struct target_ops *ops, enum target_object object,
1013 const char *annex, gdb_byte *readbuf,
1014 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
1015 {
1016 if (!record_gdb_operation_disable
1017 && (object == TARGET_OBJECT_MEMORY
1018 || object == TARGET_OBJECT_RAW_MEMORY) && writebuf)
1019 {
1020 if (RECORD_IS_REPLAY)
1021 {
1022 /* Let user choose if he wants to write memory or not. */
1023 if (!nquery (_("Because GDB is in replay mode, writing to memory "
1024 "will make the execution log unusable from this "
1025 "point onward. Write memory at address %s?"),
1026 paddress (target_gdbarch, offset)))
1027 error (_("Process record canceled the operation."));
1028
1029 /* Destroy the record from here forward. */
1030 record_list_release_next ();
1031 }
1032
1033 /* Check record_insn_num */
1034 record_check_insn_num (0);
1035
1036 /* Record registers change to list as an instruction. */
1037 record_arch_list_head = NULL;
1038 record_arch_list_tail = NULL;
1039 if (record_arch_list_add_mem (offset, len))
1040 {
1041 record_list_release (record_arch_list_tail);
1042 if (record_debug)
1043 fprintf_unfiltered (gdb_stdlog,
1044 _("Process record: failed to record "
1045 "execution log."));
1046 return -1;
1047 }
1048 if (record_arch_list_add_end ())
1049 {
1050 record_list_release (record_arch_list_tail);
1051 if (record_debug)
1052 fprintf_unfiltered (gdb_stdlog,
1053 _("Process record: failed to record "
1054 "execution log."));
1055 return -1;
1056 }
1057 record_list->next = record_arch_list_head;
1058 record_arch_list_head->prev = record_list;
1059 record_list = record_arch_list_tail;
1060
1061 if (record_insn_num == record_insn_max_num && record_insn_max_num)
1062 record_list_release_first ();
1063 else
1064 record_insn_num++;
1065 }
1066
1067 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops,
1068 object, annex, readbuf, writebuf,
1069 offset, len);
1070 }
1071
1072 /* Behavior is conditional on RECORD_IS_REPLAY.
1073 We will not actually insert or remove breakpoints when replaying,
1074 nor when recording. */
1075
1076 static int
1077 record_insert_breakpoint (struct gdbarch *gdbarch,
1078 struct bp_target_info *bp_tgt)
1079 {
1080 if (!RECORD_IS_REPLAY)
1081 {
1082 struct cleanup *old_cleanups = record_gdb_operation_disable_set ();
1083 int ret = record_beneath_to_insert_breakpoint (gdbarch, bp_tgt);
1084
1085 do_cleanups (old_cleanups);
1086
1087 return ret;
1088 }
1089
1090 return 0;
1091 }
1092
1093 static int
1094 record_remove_breakpoint (struct gdbarch *gdbarch,
1095 struct bp_target_info *bp_tgt)
1096 {
1097 if (!RECORD_IS_REPLAY)
1098 {
1099 struct cleanup *old_cleanups = record_gdb_operation_disable_set ();
1100 int ret = record_beneath_to_remove_breakpoint (gdbarch, bp_tgt);
1101
1102 do_cleanups (old_cleanups);
1103
1104 return ret;
1105 }
1106
1107 return 0;
1108 }
1109
1110 static int
1111 record_can_execute_reverse (void)
1112 {
1113 return 1;
1114 }
1115
1116 static void
1117 init_record_ops (void)
1118 {
1119 record_ops.to_shortname = "record";
1120 record_ops.to_longname = "Process record and replay target";
1121 record_ops.to_doc =
1122 "Log program while executing and replay execution from log.";
1123 record_ops.to_open = record_open;
1124 record_ops.to_close = record_close;
1125 record_ops.to_resume = record_resume;
1126 record_ops.to_wait = record_wait;
1127 record_ops.to_disconnect = record_disconnect;
1128 record_ops.to_detach = record_detach;
1129 record_ops.to_mourn_inferior = record_mourn_inferior;
1130 record_ops.to_kill = record_kill;
1131 record_ops.to_create_inferior = find_default_create_inferior;
1132 record_ops.to_store_registers = record_store_registers;
1133 record_ops.to_xfer_partial = record_xfer_partial;
1134 record_ops.to_insert_breakpoint = record_insert_breakpoint;
1135 record_ops.to_remove_breakpoint = record_remove_breakpoint;
1136 record_ops.to_can_execute_reverse = record_can_execute_reverse;
1137 record_ops.to_stratum = record_stratum;
1138 record_ops.to_magic = OPS_MAGIC;
1139 }
1140
1141 static void
1142 show_record_debug (struct ui_file *file, int from_tty,
1143 struct cmd_list_element *c, const char *value)
1144 {
1145 fprintf_filtered (file, _("Debugging of process record target is %s.\n"),
1146 value);
1147 }
1148
1149 /* Alias for "target record". */
1150
1151 static void
1152 cmd_record_start (char *args, int from_tty)
1153 {
1154 execute_command ("target record", from_tty);
1155 }
1156
1157 /* Truncate the record log from the present point
1158 of replay until the end. */
1159
1160 static void
1161 cmd_record_delete (char *args, int from_tty)
1162 {
1163 if (current_target.to_stratum == record_stratum)
1164 {
1165 if (RECORD_IS_REPLAY)
1166 {
1167 if (!from_tty || query (_("Delete the log from this point forward "
1168 "and begin to record the running message "
1169 "at current PC?")))
1170 record_list_release_next ();
1171 }
1172 else
1173 printf_unfiltered (_("Already at end of record list.\n"));
1174
1175 }
1176 else
1177 printf_unfiltered (_("Process record is not started.\n"));
1178 }
1179
1180 /* Implement the "stoprecord" command. */
1181
1182 static void
1183 cmd_record_stop (char *args, int from_tty)
1184 {
1185 if (current_target.to_stratum == record_stratum)
1186 {
1187 if (!record_list || !from_tty || query (_("Delete recorded log and "
1188 "stop recording?")))
1189 unpush_target (&record_ops);
1190 }
1191 else
1192 printf_unfiltered (_("Process record is not started.\n"));
1193 }
1194
1195 /* Set upper limit of record log size. */
1196
1197 static void
1198 set_record_insn_max_num (char *args, int from_tty, struct cmd_list_element *c)
1199 {
1200 if (record_insn_num > record_insn_max_num && record_insn_max_num)
1201 {
1202 printf_unfiltered (_("Record instructions number is bigger than "
1203 "record instructions max number. Auto delete "
1204 "the first ones?\n"));
1205
1206 while (record_insn_num > record_insn_max_num)
1207 record_list_release_first ();
1208 }
1209 }
1210
1211 /* Print the current index into the record log (number of insns recorded
1212 so far). */
1213
1214 static void
1215 show_record_insn_number (char *ignore, int from_tty)
1216 {
1217 printf_unfiltered (_("Record instruction number is %d.\n"),
1218 record_insn_num);
1219 }
1220
1221 static struct cmd_list_element *record_cmdlist, *set_record_cmdlist,
1222 *show_record_cmdlist, *info_record_cmdlist;
1223
1224 static void
1225 set_record_command (char *args, int from_tty)
1226 {
1227 printf_unfiltered (_("\
1228 \"set record\" must be followed by an apporpriate subcommand.\n"));
1229 help_list (set_record_cmdlist, "set record ", all_commands, gdb_stdout);
1230 }
1231
1232 static void
1233 show_record_command (char *args, int from_tty)
1234 {
1235 cmd_show_list (show_record_cmdlist, from_tty, "");
1236 }
1237
1238 static void
1239 info_record_command (char *args, int from_tty)
1240 {
1241 cmd_show_list (info_record_cmdlist, from_tty, "");
1242 }
1243
1244 void
1245 _initialize_record (void)
1246 {
1247 /* Init record_first. */
1248 record_first.prev = NULL;
1249 record_first.next = NULL;
1250 record_first.type = record_end;
1251
1252 init_record_ops ();
1253 add_target (&record_ops);
1254
1255 add_setshow_zinteger_cmd ("record", no_class, &record_debug,
1256 _("Set debugging of record/replay feature."),
1257 _("Show debugging of record/replay feature."),
1258 _("When enabled, debugging output for "
1259 "record/replay feature is displayed."),
1260 NULL, show_record_debug, &setdebuglist,
1261 &showdebuglist);
1262
1263 add_prefix_cmd ("record", class_obscure, cmd_record_start,
1264 _("Abbreviated form of \"target record\" command."),
1265 &record_cmdlist, "record ", 0, &cmdlist);
1266 add_com_alias ("rec", "record", class_obscure, 1);
1267 add_prefix_cmd ("record", class_support, set_record_command,
1268 _("Set record options"), &set_record_cmdlist,
1269 "set record ", 0, &setlist);
1270 add_alias_cmd ("rec", "record", class_obscure, 1, &setlist);
1271 add_prefix_cmd ("record", class_support, show_record_command,
1272 _("Show record options"), &show_record_cmdlist,
1273 "show record ", 0, &showlist);
1274 add_alias_cmd ("rec", "record", class_obscure, 1, &showlist);
1275 add_prefix_cmd ("record", class_support, info_record_command,
1276 _("Info record options"), &info_record_cmdlist,
1277 "info record ", 0, &infolist);
1278 add_alias_cmd ("rec", "record", class_obscure, 1, &infolist);
1279
1280
1281 add_cmd ("delete", class_obscure, cmd_record_delete,
1282 _("Delete the rest of execution log and start recording it anew."),
1283 &record_cmdlist);
1284 add_alias_cmd ("d", "delete", class_obscure, 1, &record_cmdlist);
1285 add_alias_cmd ("del", "delete", class_obscure, 1, &record_cmdlist);
1286
1287 add_cmd ("stop", class_obscure, cmd_record_stop,
1288 _("Stop the record/replay target."),
1289 &record_cmdlist);
1290 add_alias_cmd ("s", "stop", class_obscure, 1, &record_cmdlist);
1291
1292 /* Record instructions number limit command. */
1293 add_setshow_boolean_cmd ("stop-at-limit", no_class,
1294 &record_stop_at_limit, _("\
1295 Set whether record/replay stops when record/replay buffer becomes full."), _("\
1296 Show whether record/replay stops when record/replay buffer becomes full."), _("\
1297 Default is ON.\n\
1298 When ON, if the record/replay buffer becomes full, ask user what to do.\n\
1299 When OFF, if the record/replay buffer becomes full,\n\
1300 delete the oldest recorded instruction to make room for each new one."),
1301 NULL, NULL,
1302 &set_record_cmdlist, &show_record_cmdlist);
1303 add_setshow_zinteger_cmd ("insn-number-max", no_class,
1304 &record_insn_max_num,
1305 _("Set record/replay buffer limit."),
1306 _("Show record/replay buffer limit."), _("\
1307 Set the maximum number of instructions to be stored in the\n\
1308 record/replay buffer. Zero means unlimited. Default is 200000."),
1309 set_record_insn_max_num,
1310 NULL, &set_record_cmdlist, &show_record_cmdlist);
1311 add_cmd ("insn-number", class_obscure, show_record_insn_number,
1312 _("Show the current number of instructions in the "
1313 "record/replay buffer."), &info_record_cmdlist);
1314 }
This page took 0.054154 seconds and 5 git commands to generate.