2009-09-07 Michael Snyder <msnyder@vmware.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 structs of the process record functionality.
36
37 A 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 have a struct record_entry ("record_end") that
40 indicates that this is the last struct record_entry of this
41 instruction.
42
43 Each struct record_entry is linked to "record_list" by "prev" and
44 "next" pointers. */
45
46 struct record_reg_entry
47 {
48 int num;
49 gdb_byte *val;
50 };
51
52 struct record_mem_entry
53 {
54 CORE_ADDR addr;
55 int len;
56 /* Set this flag if target memory for this entry
57 can no longer be accessed. */
58 int mem_entry_not_accessible;
59 gdb_byte *val;
60 };
61
62 enum record_type
63 {
64 record_end = 0,
65 record_reg,
66 record_mem
67 };
68
69 struct record_entry
70 {
71 struct record_entry *prev;
72 struct record_entry *next;
73 enum record_type type;
74 union
75 {
76 /* reg */
77 struct record_reg_entry reg;
78 /* mem */
79 struct record_mem_entry mem;
80 } u;
81 };
82
83 /* This is the debug switch for process record. */
84 int record_debug = 0;
85
86 /* These list is for execution log. */
87 static struct record_entry record_first;
88 static struct record_entry *record_list = &record_first;
89 static struct record_entry *record_arch_list_head = NULL;
90 static struct record_entry *record_arch_list_tail = NULL;
91
92 /* 1 ask user. 0 auto delete the last struct record_entry. */
93 static int record_stop_at_limit = 1;
94 static int record_insn_max_num = DEFAULT_RECORD_INSN_MAX_NUM;
95 static int record_insn_num = 0;
96
97 /* The target_ops of process record. */
98 static struct target_ops record_ops;
99
100 /* The beneath function pointers. */
101 static struct target_ops *record_beneath_to_resume_ops;
102 static void (*record_beneath_to_resume) (struct target_ops *, ptid_t, int,
103 enum target_signal);
104 static struct target_ops *record_beneath_to_wait_ops;
105 static ptid_t (*record_beneath_to_wait) (struct target_ops *, ptid_t,
106 struct target_waitstatus *,
107 int);
108 static struct target_ops *record_beneath_to_store_registers_ops;
109 static void (*record_beneath_to_store_registers) (struct target_ops *,
110 struct regcache *,
111 int regno);
112 static struct target_ops *record_beneath_to_xfer_partial_ops;
113 static LONGEST (*record_beneath_to_xfer_partial) (struct target_ops *ops,
114 enum target_object object,
115 const char *annex,
116 gdb_byte *readbuf,
117 const gdb_byte *writebuf,
118 ULONGEST offset,
119 LONGEST len);
120 static int (*record_beneath_to_insert_breakpoint) (struct gdbarch *,
121 struct bp_target_info *);
122 static int (*record_beneath_to_remove_breakpoint) (struct gdbarch *,
123 struct bp_target_info *);
124
125 static void
126 record_list_release (struct record_entry *rec)
127 {
128 struct record_entry *tmp;
129
130 if (!rec)
131 return;
132
133 while (rec->next)
134 {
135 rec = rec->next;
136 }
137
138 while (rec->prev)
139 {
140 tmp = rec;
141 rec = rec->prev;
142 if (tmp->type == record_reg)
143 xfree (tmp->u.reg.val);
144 else if (tmp->type == record_mem)
145 xfree (tmp->u.mem.val);
146 xfree (tmp);
147 }
148
149 if (rec != &record_first)
150 xfree (rec);
151 }
152
153 static void
154 record_list_release_next (void)
155 {
156 struct record_entry *rec = record_list;
157 struct record_entry *tmp = rec->next;
158 rec->next = NULL;
159 while (tmp)
160 {
161 rec = tmp->next;
162 if (tmp->type == record_end)
163 record_insn_num--;
164 else if (tmp->type == record_reg)
165 xfree (tmp->u.reg.val);
166 else if (tmp->type == record_mem)
167 xfree (tmp->u.mem.val);
168 xfree (tmp);
169 tmp = rec;
170 }
171 }
172
173 static void
174 record_list_release_first (void)
175 {
176 struct record_entry *tmp = NULL;
177 enum record_type type;
178
179 if (!record_first.next)
180 return;
181
182 while (1)
183 {
184 type = record_first.next->type;
185
186 if (type == record_reg)
187 xfree (record_first.next->u.reg.val);
188 else if (type == record_mem)
189 xfree (record_first.next->u.mem.val);
190 tmp = record_first.next;
191 record_first.next = tmp->next;
192 xfree (tmp);
193
194 if (!record_first.next)
195 {
196 gdb_assert (record_insn_num == 1);
197 break;
198 }
199
200 record_first.next->prev = &record_first;
201
202 if (type == record_end)
203 break;
204 }
205
206 record_insn_num--;
207 }
208
209 /* Add a struct record_entry to record_arch_list. */
210
211 static void
212 record_arch_list_add (struct record_entry *rec)
213 {
214 if (record_debug > 1)
215 fprintf_unfiltered (gdb_stdlog,
216 "Process record: record_arch_list_add %s.\n",
217 host_address_to_string (rec));
218
219 if (record_arch_list_tail)
220 {
221 record_arch_list_tail->next = rec;
222 rec->prev = record_arch_list_tail;
223 record_arch_list_tail = rec;
224 }
225 else
226 {
227 record_arch_list_head = rec;
228 record_arch_list_tail = rec;
229 }
230 }
231
232 /* Record the value of a register NUM to record_arch_list. */
233
234 int
235 record_arch_list_add_reg (struct regcache *regcache, int num)
236 {
237 struct record_entry *rec;
238
239 if (record_debug > 1)
240 fprintf_unfiltered (gdb_stdlog,
241 "Process record: add register num = %d to "
242 "record list.\n",
243 num);
244
245 rec = (struct record_entry *) xmalloc (sizeof (struct record_entry));
246 rec->u.reg.val = (gdb_byte *) xmalloc (MAX_REGISTER_SIZE);
247 rec->prev = NULL;
248 rec->next = NULL;
249 rec->type = record_reg;
250 rec->u.reg.num = num;
251
252 regcache_raw_read (regcache, num, rec->u.reg.val);
253
254 record_arch_list_add (rec);
255
256 return 0;
257 }
258
259 /* Record the value of a region of memory whose address is ADDR and
260 length is LEN to record_arch_list. */
261
262 int
263 record_arch_list_add_mem (CORE_ADDR addr, int len)
264 {
265 struct record_entry *rec;
266
267 if (record_debug > 1)
268 fprintf_unfiltered (gdb_stdlog,
269 "Process record: add mem addr = %s len = %d to "
270 "record list.\n",
271 paddress (target_gdbarch, addr), len);
272
273 if (!addr)
274 return 0;
275
276 rec = (struct record_entry *) xmalloc (sizeof (struct record_entry));
277 rec->u.mem.val = (gdb_byte *) xmalloc (len);
278 rec->prev = NULL;
279 rec->next = NULL;
280 rec->type = record_mem;
281 rec->u.mem.addr = addr;
282 rec->u.mem.len = len;
283 rec->u.mem.mem_entry_not_accessible = 0;
284
285 if (target_read_memory (addr, rec->u.mem.val, len))
286 {
287 if (record_debug)
288 fprintf_unfiltered (gdb_stdlog,
289 "Process record: error reading memory at "
290 "addr = %s len = %d.\n",
291 paddress (target_gdbarch, addr), len);
292 xfree (rec->u.mem.val);
293 xfree (rec);
294 return -1;
295 }
296
297 record_arch_list_add (rec);
298
299 return 0;
300 }
301
302 /* Add a record_end type struct record_entry to record_arch_list. */
303
304 int
305 record_arch_list_add_end (void)
306 {
307 struct record_entry *rec;
308
309 if (record_debug > 1)
310 fprintf_unfiltered (gdb_stdlog,
311 "Process record: add end to arch list.\n");
312
313 rec = (struct record_entry *) xmalloc (sizeof (struct record_entry));
314 rec->prev = NULL;
315 rec->next = NULL;
316 rec->type = record_end;
317
318 record_arch_list_add (rec);
319
320 return 0;
321 }
322
323 static void
324 record_check_insn_num (int set_terminal)
325 {
326 if (record_insn_max_num)
327 {
328 gdb_assert (record_insn_num <= record_insn_max_num);
329 if (record_insn_num == record_insn_max_num)
330 {
331 /* Ask user what to do. */
332 if (record_stop_at_limit)
333 {
334 int q;
335 if (set_terminal)
336 target_terminal_ours ();
337 q = yquery (_("Do you want to auto delete previous execution "
338 "log entries when record/replay buffer becomes "
339 "full (record stop-at-limit)?"));
340 if (set_terminal)
341 target_terminal_inferior ();
342 if (q)
343 record_stop_at_limit = 0;
344 else
345 error (_("Process record: inferior program stopped."));
346 }
347 }
348 }
349 }
350
351 /* Before inferior step (when GDB record the running message, inferior
352 only can step), GDB will call this function to record the values to
353 record_list. This function will call gdbarch_process_record to
354 record the running message of inferior and set them to
355 record_arch_list, and add it to record_list. */
356
357 static void
358 record_message_cleanups (void *ignore)
359 {
360 record_list_release (record_arch_list_tail);
361 }
362
363 static int
364 record_message (void *args)
365 {
366 int ret;
367 struct regcache *regcache = args;
368 struct cleanup *old_cleanups = make_cleanup (record_message_cleanups, 0);
369
370 record_arch_list_head = NULL;
371 record_arch_list_tail = NULL;
372
373 /* Check record_insn_num. */
374 record_check_insn_num (1);
375
376 ret = gdbarch_process_record (get_regcache_arch (regcache),
377 regcache,
378 regcache_read_pc (regcache));
379 if (ret > 0)
380 error (_("Process record: inferior program stopped."));
381 if (ret < 0)
382 error (_("Process record: failed to record execution log."));
383
384 discard_cleanups (old_cleanups);
385
386 record_list->next = record_arch_list_head;
387 record_arch_list_head->prev = record_list;
388 record_list = record_arch_list_tail;
389
390 if (record_insn_num == record_insn_max_num && record_insn_max_num)
391 record_list_release_first ();
392 else
393 record_insn_num++;
394
395 return 1;
396 }
397
398 static int
399 do_record_message (struct regcache *regcache)
400 {
401 return catch_errors (record_message, regcache, NULL, RETURN_MASK_ALL);
402 }
403
404 /* Set to 1 if record_store_registers and record_xfer_partial
405 doesn't need record. */
406
407 static int record_gdb_operation_disable = 0;
408
409 struct cleanup *
410 record_gdb_operation_disable_set (void)
411 {
412 struct cleanup *old_cleanups = NULL;
413
414 old_cleanups =
415 make_cleanup_restore_integer (&record_gdb_operation_disable);
416 record_gdb_operation_disable = 1;
417
418 return old_cleanups;
419 }
420
421 static void
422 record_open (char *name, int from_tty)
423 {
424 struct target_ops *t;
425
426 if (record_debug)
427 fprintf_unfiltered (gdb_stdlog, "Process record: record_open\n");
428
429 /* check exec */
430 if (!target_has_execution)
431 error (_("Process record: the program is not being run."));
432 if (non_stop)
433 error (_("Process record target can't debug inferior in non-stop mode "
434 "(non-stop)."));
435 if (target_async_permitted)
436 error (_("Process record target can't debug inferior in asynchronous "
437 "mode (target-async)."));
438
439 if (!gdbarch_process_record_p (target_gdbarch))
440 error (_("Process record: the current architecture doesn't support "
441 "record function."));
442
443 /* Check if record target is already running. */
444 if (current_target.to_stratum == record_stratum)
445 {
446 if (!nquery
447 (_("Process record target already running, do you want to delete "
448 "the old record log?")))
449 return;
450 }
451
452 /*Reset the beneath function pointers. */
453 record_beneath_to_resume = NULL;
454 record_beneath_to_wait = NULL;
455 record_beneath_to_store_registers = NULL;
456 record_beneath_to_xfer_partial = NULL;
457 record_beneath_to_insert_breakpoint = NULL;
458 record_beneath_to_remove_breakpoint = NULL;
459
460 /* Set the beneath function pointers. */
461 for (t = current_target.beneath; t != NULL; t = t->beneath)
462 {
463 if (!record_beneath_to_resume)
464 {
465 record_beneath_to_resume = t->to_resume;
466 record_beneath_to_resume_ops = t;
467 }
468 if (!record_beneath_to_wait)
469 {
470 record_beneath_to_wait = t->to_wait;
471 record_beneath_to_wait_ops = t;
472 }
473 if (!record_beneath_to_store_registers)
474 {
475 record_beneath_to_store_registers = t->to_store_registers;
476 record_beneath_to_store_registers_ops = t;
477 }
478 if (!record_beneath_to_xfer_partial)
479 {
480 record_beneath_to_xfer_partial = t->to_xfer_partial;
481 record_beneath_to_xfer_partial_ops = t;
482 }
483 if (!record_beneath_to_insert_breakpoint)
484 record_beneath_to_insert_breakpoint = t->to_insert_breakpoint;
485 if (!record_beneath_to_remove_breakpoint)
486 record_beneath_to_remove_breakpoint = t->to_remove_breakpoint;
487 }
488 if (!record_beneath_to_resume)
489 error (_("Process record can't get to_resume."));
490 if (!record_beneath_to_wait)
491 error (_("Process record can't get to_wait."));
492 if (!record_beneath_to_store_registers)
493 error (_("Process record can't get to_store_registers."));
494 if (!record_beneath_to_xfer_partial)
495 error (_("Process record can't get to_xfer_partial."));
496 if (!record_beneath_to_insert_breakpoint)
497 error (_("Process record can't get to_insert_breakpoint."));
498 if (!record_beneath_to_remove_breakpoint)
499 error (_("Process record can't get to_remove_breakpoint."));
500
501 push_target (&record_ops);
502
503 /* Reset */
504 record_insn_num = 0;
505 record_list = &record_first;
506 record_list->next = NULL;
507 }
508
509 static void
510 record_close (int quitting)
511 {
512 if (record_debug)
513 fprintf_unfiltered (gdb_stdlog, "Process record: record_close\n");
514
515 record_list_release (record_list);
516 }
517
518 static int record_resume_step = 0;
519 static int record_resume_error;
520
521 static void
522 record_resume (struct target_ops *ops, ptid_t ptid, int step,
523 enum target_signal siggnal)
524 {
525 record_resume_step = step;
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, options);
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, options);
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 TARGET_SIGNAL_0);
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
963 /* Let user choose if he wants to write register or not. */
964 if (regno < 0)
965 n =
966 nquery (_("Because GDB is in replay mode, changing the "
967 "value of a register will make the execution "
968 "log unusable from this point onward. "
969 "Change all registers?"));
970 else
971 n =
972 nquery (_("Because GDB is in replay mode, changing the value "
973 "of a register will make the execution log unusable "
974 "from this point onward. Change register %s?"),
975 gdbarch_register_name (get_regcache_arch (regcache),
976 regno));
977
978 if (!n)
979 {
980 /* Invalidate the value of regcache that was set in function
981 "regcache_raw_write". */
982 if (regno < 0)
983 {
984 int i;
985 for (i = 0;
986 i < gdbarch_num_regs (get_regcache_arch (regcache));
987 i++)
988 regcache_invalidate (regcache, i);
989 }
990 else
991 regcache_invalidate (regcache, regno);
992
993 error (_("Process record canceled the operation."));
994 }
995
996 /* Destroy the record from here forward. */
997 record_list_release_next ();
998 }
999
1000 record_registers_change (regcache, regno);
1001 }
1002 record_beneath_to_store_registers (record_beneath_to_store_registers_ops,
1003 regcache, regno);
1004 }
1005
1006 /* Behavior is conditional on RECORD_IS_REPLAY.
1007 In replay mode, we cannot write memory unles we are willing to
1008 invalidate the record/replay log from this point forward. */
1009
1010 static LONGEST
1011 record_xfer_partial (struct target_ops *ops, enum target_object object,
1012 const char *annex, gdb_byte *readbuf,
1013 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
1014 {
1015 if (!record_gdb_operation_disable
1016 && (object == TARGET_OBJECT_MEMORY
1017 || object == TARGET_OBJECT_RAW_MEMORY) && writebuf)
1018 {
1019 if (RECORD_IS_REPLAY)
1020 {
1021 /* Let user choose if he wants to write memory or not. */
1022 if (!nquery (_("Because GDB is in replay mode, writing to memory "
1023 "will make the execution log unusable from this "
1024 "point onward. Write memory at address %s?"),
1025 paddress (target_gdbarch, offset)))
1026 error (_("Process record canceled the operation."));
1027
1028 /* Destroy the record from here forward. */
1029 record_list_release_next ();
1030 }
1031
1032 /* Check record_insn_num */
1033 record_check_insn_num (0);
1034
1035 /* Record registers change to list as an instruction. */
1036 record_arch_list_head = NULL;
1037 record_arch_list_tail = NULL;
1038 if (record_arch_list_add_mem (offset, len))
1039 {
1040 record_list_release (record_arch_list_tail);
1041 if (record_debug)
1042 fprintf_unfiltered (gdb_stdlog,
1043 _("Process record: failed to record "
1044 "execution log."));
1045 return -1;
1046 }
1047 if (record_arch_list_add_end ())
1048 {
1049 record_list_release (record_arch_list_tail);
1050 if (record_debug)
1051 fprintf_unfiltered (gdb_stdlog,
1052 _("Process record: failed to record "
1053 "execution log."));
1054 return -1;
1055 }
1056 record_list->next = record_arch_list_head;
1057 record_arch_list_head->prev = record_list;
1058 record_list = record_arch_list_tail;
1059
1060 if (record_insn_num == record_insn_max_num && record_insn_max_num)
1061 record_list_release_first ();
1062 else
1063 record_insn_num++;
1064 }
1065
1066 return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops,
1067 object, annex, readbuf, writebuf,
1068 offset, len);
1069 }
1070
1071 /* Behavior is conditional on RECORD_IS_REPLAY.
1072 We will not actually insert or remove breakpoints when replaying,
1073 nor when recording. */
1074
1075 static int
1076 record_insert_breakpoint (struct gdbarch *gdbarch,
1077 struct bp_target_info *bp_tgt)
1078 {
1079 if (!RECORD_IS_REPLAY)
1080 {
1081 struct cleanup *old_cleanups = record_gdb_operation_disable_set ();
1082 int ret = record_beneath_to_insert_breakpoint (gdbarch, bp_tgt);
1083
1084 do_cleanups (old_cleanups);
1085
1086 return ret;
1087 }
1088
1089 return 0;
1090 }
1091
1092 static int
1093 record_remove_breakpoint (struct gdbarch *gdbarch,
1094 struct bp_target_info *bp_tgt)
1095 {
1096 if (!RECORD_IS_REPLAY)
1097 {
1098 struct cleanup *old_cleanups = record_gdb_operation_disable_set ();
1099 int ret = record_beneath_to_remove_breakpoint (gdbarch, bp_tgt);
1100
1101 do_cleanups (old_cleanups);
1102
1103 return ret;
1104 }
1105
1106 return 0;
1107 }
1108
1109 static int
1110 record_can_execute_reverse (void)
1111 {
1112 return 1;
1113 }
1114
1115 static void
1116 init_record_ops (void)
1117 {
1118 record_ops.to_shortname = "record";
1119 record_ops.to_longname = "Process record and replay target";
1120 record_ops.to_doc =
1121 "Log program while executing and replay execution from log.";
1122 record_ops.to_open = record_open;
1123 record_ops.to_close = record_close;
1124 record_ops.to_resume = record_resume;
1125 record_ops.to_wait = record_wait;
1126 record_ops.to_disconnect = record_disconnect;
1127 record_ops.to_detach = record_detach;
1128 record_ops.to_mourn_inferior = record_mourn_inferior;
1129 record_ops.to_kill = record_kill;
1130 record_ops.to_create_inferior = find_default_create_inferior;
1131 record_ops.to_store_registers = record_store_registers;
1132 record_ops.to_xfer_partial = record_xfer_partial;
1133 record_ops.to_insert_breakpoint = record_insert_breakpoint;
1134 record_ops.to_remove_breakpoint = record_remove_breakpoint;
1135 record_ops.to_can_execute_reverse = record_can_execute_reverse;
1136 record_ops.to_stratum = record_stratum;
1137 record_ops.to_magic = OPS_MAGIC;
1138 }
1139
1140 static void
1141 show_record_debug (struct ui_file *file, int from_tty,
1142 struct cmd_list_element *c, const char *value)
1143 {
1144 fprintf_filtered (file, _("Debugging of process record target is %s.\n"),
1145 value);
1146 }
1147
1148 /* Alias for "target record". */
1149
1150 static void
1151 cmd_record_start (char *args, int from_tty)
1152 {
1153 execute_command ("target record", from_tty);
1154 }
1155
1156 /* Truncate the record log from the present point
1157 of replay until the end. */
1158
1159 static void
1160 cmd_record_delete (char *args, int from_tty)
1161 {
1162 if (current_target.to_stratum == record_stratum)
1163 {
1164 if (RECORD_IS_REPLAY)
1165 {
1166 if (!from_tty || query (_("Delete the log from this point forward "
1167 "and begin to record the running message "
1168 "at current PC?")))
1169 record_list_release_next ();
1170 }
1171 else
1172 printf_unfiltered (_("Already at end of record list.\n"));
1173
1174 }
1175 else
1176 printf_unfiltered (_("Process record is not started.\n"));
1177 }
1178
1179 /* Implement the "stoprecord" command. */
1180
1181 static void
1182 cmd_record_stop (char *args, int from_tty)
1183 {
1184 if (current_target.to_stratum == record_stratum)
1185 {
1186 if (!record_list || !from_tty || query (_("Delete recorded log and "
1187 "stop recording?")))
1188 unpush_target (&record_ops);
1189 }
1190 else
1191 printf_unfiltered (_("Process record is not started.\n"));
1192 }
1193
1194 /* Set upper limit of record log size. */
1195
1196 static void
1197 set_record_insn_max_num (char *args, int from_tty, struct cmd_list_element *c)
1198 {
1199 if (record_insn_num > record_insn_max_num && record_insn_max_num)
1200 {
1201 printf_unfiltered (_("Record instructions number is bigger than "
1202 "record instructions max number. Auto delete "
1203 "the first ones?\n"));
1204
1205 while (record_insn_num > record_insn_max_num)
1206 record_list_release_first ();
1207 }
1208 }
1209
1210 /* Print the current index into the record log (number of insns recorded
1211 so far). */
1212
1213 static void
1214 show_record_insn_number (char *ignore, int from_tty)
1215 {
1216 printf_unfiltered (_("Record instruction number is %d.\n"),
1217 record_insn_num);
1218 }
1219
1220 static struct cmd_list_element *record_cmdlist, *set_record_cmdlist,
1221 *show_record_cmdlist, *info_record_cmdlist;
1222
1223 static void
1224 set_record_command (char *args, int from_tty)
1225 {
1226 printf_unfiltered (_("\
1227 \"set record\" must be followed by an apporpriate subcommand.\n"));
1228 help_list (set_record_cmdlist, "set record ", all_commands, gdb_stdout);
1229 }
1230
1231 static void
1232 show_record_command (char *args, int from_tty)
1233 {
1234 cmd_show_list (show_record_cmdlist, from_tty, "");
1235 }
1236
1237 static void
1238 info_record_command (char *args, int from_tty)
1239 {
1240 cmd_show_list (info_record_cmdlist, from_tty, "");
1241 }
1242
1243 void
1244 _initialize_record (void)
1245 {
1246 /* Init record_first. */
1247 record_first.prev = NULL;
1248 record_first.next = NULL;
1249 record_first.type = record_end;
1250
1251 init_record_ops ();
1252 add_target (&record_ops);
1253
1254 add_setshow_zinteger_cmd ("record", no_class, &record_debug,
1255 _("Set debugging of record/replay feature."),
1256 _("Show debugging of record/replay feature."),
1257 _("When enabled, debugging output for "
1258 "record/replay feature is displayed."),
1259 NULL, show_record_debug, &setdebuglist,
1260 &showdebuglist);
1261
1262 add_prefix_cmd ("record", class_obscure, cmd_record_start,
1263 _("Abbreviated form of \"target record\" command."),
1264 &record_cmdlist, "record ", 0, &cmdlist);
1265 add_com_alias ("rec", "record", class_obscure, 1);
1266 add_prefix_cmd ("record", class_support, set_record_command,
1267 _("Set record options"), &set_record_cmdlist,
1268 "set record ", 0, &setlist);
1269 add_alias_cmd ("rec", "record", class_obscure, 1, &setlist);
1270 add_prefix_cmd ("record", class_support, show_record_command,
1271 _("Show record options"), &show_record_cmdlist,
1272 "show record ", 0, &showlist);
1273 add_alias_cmd ("rec", "record", class_obscure, 1, &showlist);
1274 add_prefix_cmd ("record", class_support, info_record_command,
1275 _("Info record options"), &info_record_cmdlist,
1276 "info record ", 0, &infolist);
1277 add_alias_cmd ("rec", "record", class_obscure, 1, &infolist);
1278
1279
1280 add_cmd ("delete", class_obscure, cmd_record_delete,
1281 _("Delete the rest of execution log and start recording it anew."),
1282 &record_cmdlist);
1283 add_alias_cmd ("d", "delete", class_obscure, 1, &record_cmdlist);
1284 add_alias_cmd ("del", "delete", class_obscure, 1, &record_cmdlist);
1285
1286 add_cmd ("stop", class_obscure, cmd_record_stop,
1287 _("Stop the record/replay target."),
1288 &record_cmdlist);
1289 add_alias_cmd ("s", "stop", class_obscure, 1, &record_cmdlist);
1290
1291 /* Record instructions number limit command. */
1292 add_setshow_boolean_cmd ("stop-at-limit", no_class,
1293 &record_stop_at_limit, _("\
1294 Set whether record/replay stops when record/replay buffer becomes full."), _("\
1295 Show whether record/replay stops when record/replay buffer becomes full."), _("\
1296 Default is ON.\n\
1297 When ON, if the record/replay buffer becomes full, ask user what to do.\n\
1298 When OFF, if the record/replay buffer becomes full,\n\
1299 delete the oldest recorded instruction to make room for each new one."),
1300 NULL, NULL,
1301 &set_record_cmdlist, &show_record_cmdlist);
1302 add_setshow_zinteger_cmd ("insn-number-max", no_class,
1303 &record_insn_max_num,
1304 _("Set record/replay buffer limit."),
1305 _("Show record/replay buffer limit."), _("\
1306 Set the maximum number of instructions to be stored in the\n\
1307 record/replay buffer. Zero means unlimited. Default is 200000."),
1308 set_record_insn_max_num,
1309 NULL, &set_record_cmdlist, &show_record_cmdlist);
1310 add_cmd ("insn-number", class_obscure, show_record_insn_number,
1311 _("Show the current number of instructions in the "
1312 "record/replay buffer."), &info_record_cmdlist);
1313 }
This page took 0.056267 seconds and 4 git commands to generate.