2009-09-21 Hui Zhu <teawater@gmail.com>
[external/binutils.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 struct record_end_entry
63 {
64   enum target_signal sigval;
65 };
66
67 enum record_type
68 {
69   record_end = 0,
70   record_reg,
71   record_mem
72 };
73
74 struct record_entry
75 {
76   struct record_entry *prev;
77   struct record_entry *next;
78   enum record_type type;
79   union
80   {
81     /* reg */
82     struct record_reg_entry reg;
83     /* mem */
84     struct record_mem_entry mem;
85     /* end */
86     struct record_end_entry end;
87   } u;
88 };
89
90 /* This is the debug switch for process record.  */
91 int record_debug = 0;
92
93 /* These list is for execution log.  */
94 static struct record_entry record_first;
95 static struct record_entry *record_list = &record_first;
96 static struct record_entry *record_arch_list_head = NULL;
97 static struct record_entry *record_arch_list_tail = NULL;
98
99 /* 1 ask user. 0 auto delete the last struct record_entry.  */
100 static int record_stop_at_limit = 1;
101 static int record_insn_max_num = DEFAULT_RECORD_INSN_MAX_NUM;
102 static int record_insn_num = 0;
103
104 /* The target_ops of process record.  */
105 static struct target_ops record_ops;
106
107 /* The beneath function pointers.  */
108 static struct target_ops *record_beneath_to_resume_ops;
109 static void (*record_beneath_to_resume) (struct target_ops *, ptid_t, int,
110                                          enum target_signal);
111 static struct target_ops *record_beneath_to_wait_ops;
112 static ptid_t (*record_beneath_to_wait) (struct target_ops *, ptid_t,
113                                          struct target_waitstatus *,
114                                          int);
115 static struct target_ops *record_beneath_to_store_registers_ops;
116 static void (*record_beneath_to_store_registers) (struct target_ops *,
117                                                   struct regcache *,
118                                                   int regno);
119 static struct target_ops *record_beneath_to_xfer_partial_ops;
120 static LONGEST (*record_beneath_to_xfer_partial) (struct target_ops *ops,
121                                                   enum target_object object,
122                                                   const char *annex,
123                                                   gdb_byte *readbuf,
124                                                   const gdb_byte *writebuf,
125                                                   ULONGEST offset,
126                                                   LONGEST len);
127 static int (*record_beneath_to_insert_breakpoint) (struct gdbarch *,
128                                                    struct bp_target_info *);
129 static int (*record_beneath_to_remove_breakpoint) (struct gdbarch *,
130                                                    struct bp_target_info *);
131
132 static void
133 record_list_release (struct record_entry *rec)
134 {
135   struct record_entry *tmp;
136
137   if (!rec)
138     return;
139
140   while (rec->next)
141     {
142       rec = rec->next;
143     }
144
145   while (rec->prev)
146     {
147       tmp = rec;
148       rec = rec->prev;
149       if (tmp->type == record_reg)
150         xfree (tmp->u.reg.val);
151       else if (tmp->type == record_mem)
152         xfree (tmp->u.mem.val);
153       xfree (tmp);
154     }
155
156   if (rec != &record_first)
157     xfree (rec);
158 }
159
160 static void
161 record_list_release_next (void)
162 {
163   struct record_entry *rec = record_list;
164   struct record_entry *tmp = rec->next;
165   rec->next = NULL;
166   while (tmp)
167     {
168       rec = tmp->next;
169       if (tmp->type == record_end)
170         record_insn_num--;
171       else if (tmp->type == record_reg)
172         xfree (tmp->u.reg.val);
173       else if (tmp->type == record_mem)
174         xfree (tmp->u.mem.val);
175       xfree (tmp);
176       tmp = rec;
177     }
178 }
179
180 static void
181 record_list_release_first (void)
182 {
183   struct record_entry *tmp = NULL;
184   enum record_type type;
185
186   if (!record_first.next)
187     return;
188
189   while (1)
190     {
191       type = record_first.next->type;
192
193       if (type == record_reg)
194         xfree (record_first.next->u.reg.val);
195       else if (type == record_mem)
196         xfree (record_first.next->u.mem.val);
197       tmp = record_first.next;
198       record_first.next = tmp->next;
199       xfree (tmp);
200
201       if (!record_first.next)
202         {
203           gdb_assert (record_insn_num == 1);
204           break;
205         }
206
207       record_first.next->prev = &record_first;
208
209       if (type == record_end)
210         break;
211     }
212
213   record_insn_num--;
214 }
215
216 /* Add a struct record_entry to record_arch_list.  */
217
218 static void
219 record_arch_list_add (struct record_entry *rec)
220 {
221   if (record_debug > 1)
222     fprintf_unfiltered (gdb_stdlog,
223                         "Process record: record_arch_list_add %s.\n",
224                         host_address_to_string (rec));
225
226   if (record_arch_list_tail)
227     {
228       record_arch_list_tail->next = rec;
229       rec->prev = record_arch_list_tail;
230       record_arch_list_tail = rec;
231     }
232   else
233     {
234       record_arch_list_head = rec;
235       record_arch_list_tail = rec;
236     }
237 }
238
239 /* Record the value of a register NUM to record_arch_list.  */
240
241 int
242 record_arch_list_add_reg (struct regcache *regcache, int num)
243 {
244   struct record_entry *rec;
245
246   if (record_debug > 1)
247     fprintf_unfiltered (gdb_stdlog,
248                         "Process record: add register num = %d to "
249                         "record list.\n",
250                         num);
251
252   rec = (struct record_entry *) xmalloc (sizeof (struct record_entry));
253   rec->u.reg.val = (gdb_byte *) xmalloc (MAX_REGISTER_SIZE);
254   rec->prev = NULL;
255   rec->next = NULL;
256   rec->type = record_reg;
257   rec->u.reg.num = num;
258
259   regcache_raw_read (regcache, num, rec->u.reg.val);
260
261   record_arch_list_add (rec);
262
263   return 0;
264 }
265
266 /* Record the value of a region of memory whose address is ADDR and
267    length is LEN to record_arch_list.  */
268
269 int
270 record_arch_list_add_mem (CORE_ADDR addr, int len)
271 {
272   struct record_entry *rec;
273
274   if (record_debug > 1)
275     fprintf_unfiltered (gdb_stdlog,
276                         "Process record: add mem addr = %s len = %d to "
277                         "record list.\n",
278                         paddress (target_gdbarch, addr), len);
279
280   if (!addr)
281     return 0;
282
283   rec = (struct record_entry *) xmalloc (sizeof (struct record_entry));
284   rec->u.mem.val = (gdb_byte *) xmalloc (len);
285   rec->prev = NULL;
286   rec->next = NULL;
287   rec->type = record_mem;
288   rec->u.mem.addr = addr;
289   rec->u.mem.len = len;
290   rec->u.mem.mem_entry_not_accessible = 0;
291
292   if (target_read_memory (addr, rec->u.mem.val, len))
293     {
294       if (record_debug)
295         fprintf_unfiltered (gdb_stdlog,
296                             "Process record: error reading memory at "
297                             "addr = %s len = %d.\n",
298                             paddress (target_gdbarch, addr), len);
299       xfree (rec->u.mem.val);
300       xfree (rec);
301       return -1;
302     }
303
304   record_arch_list_add (rec);
305
306   return 0;
307 }
308
309 /* Add a record_end type struct record_entry to record_arch_list.  */
310
311 int
312 record_arch_list_add_end (void)
313 {
314   struct record_entry *rec;
315
316   if (record_debug > 1)
317     fprintf_unfiltered (gdb_stdlog,
318                         "Process record: add end to arch list.\n");
319
320   rec = (struct record_entry *) xmalloc (sizeof (struct record_entry));
321   rec->prev = NULL;
322   rec->next = NULL;
323   rec->type = record_end;
324   rec->u.end.sigval = TARGET_SIGNAL_0;
325
326   record_arch_list_add (rec);
327
328   return 0;
329 }
330
331 static void
332 record_check_insn_num (int set_terminal)
333 {
334   if (record_insn_max_num)
335     {
336       gdb_assert (record_insn_num <= record_insn_max_num);
337       if (record_insn_num == record_insn_max_num)
338         {
339           /* Ask user what to do.  */
340           if (record_stop_at_limit)
341             {
342               int q;
343               if (set_terminal)
344                 target_terminal_ours ();
345               q = yquery (_("Do you want to auto delete previous execution "
346                             "log entries when record/replay buffer becomes "
347                             "full (record stop-at-limit)?"));
348               if (set_terminal)
349                 target_terminal_inferior ();
350               if (q)
351                 record_stop_at_limit = 0;
352               else
353                 error (_("Process record: inferior program stopped."));
354             }
355         }
356     }
357 }
358
359 /* Before inferior step (when GDB record the running message, inferior
360    only can step), GDB will call this function to record the values to
361    record_list.  This function will call gdbarch_process_record to
362    record the running message of inferior and set them to
363    record_arch_list, and add it to record_list.  */
364
365 static void
366 record_message_cleanups (void *ignore)
367 {
368   record_list_release (record_arch_list_tail);
369 }
370
371 struct record_message_args {
372   struct regcache *regcache;
373   enum target_signal signal;
374 };
375
376 static int
377 record_message (void *args)
378 {
379   int ret;
380   struct record_message_args *myargs = args;
381   struct gdbarch *gdbarch = get_regcache_arch (myargs->regcache);
382   struct cleanup *old_cleanups = make_cleanup (record_message_cleanups, 0);
383
384   record_arch_list_head = NULL;
385   record_arch_list_tail = NULL;
386
387   /* Check record_insn_num.  */
388   record_check_insn_num (1);
389
390   /* If gdb sends a signal value to target_resume,
391      save it in the 'end' field of the previous instruction.
392
393      Maybe process record should record what really happened,
394      rather than what gdb pretends has happened.
395
396      So if Linux delivered the signal to the child process during
397      the record mode, we will record it and deliver it again in
398      the replay mode.
399
400      If user says "ignore this signal" during the record mode, then
401      it will be ignored again during the replay mode (no matter if
402      the user says something different, like "deliver this signal"
403      during the replay mode).
404
405      User should understand that nothing he does during the replay
406      mode will change the behavior of the child.  If he tries,
407      then that is a user error.
408
409      But we should still deliver the signal to gdb during the replay,
410      if we delivered it during the recording.  Therefore we should
411      record the signal during record_wait, not record_resume.  */
412   if (record_list != &record_first)    /* FIXME better way to check */
413     {
414       gdb_assert (record_list->type == record_end);
415       record_list->u.end.sigval = myargs->signal;
416     }
417
418   if (myargs->signal == TARGET_SIGNAL_0
419       || !gdbarch_process_record_signal_p (gdbarch))
420     ret = gdbarch_process_record (gdbarch,
421                                   myargs->regcache,
422                                   regcache_read_pc (myargs->regcache));
423   else
424     ret = gdbarch_process_record_signal (gdbarch,
425                                          myargs->regcache,
426                                          myargs->signal);
427
428   if (ret > 0)
429     error (_("Process record: inferior program stopped."));
430   if (ret < 0)
431     error (_("Process record: failed to record execution log."));
432
433   discard_cleanups (old_cleanups);
434
435   record_list->next = record_arch_list_head;
436   record_arch_list_head->prev = record_list;
437   record_list = record_arch_list_tail;
438
439   if (record_insn_num == record_insn_max_num && record_insn_max_num)
440     record_list_release_first ();
441   else
442     record_insn_num++;
443
444   return 1;
445 }
446
447 static int
448 do_record_message (struct regcache *regcache,
449                    enum target_signal signal)
450 {
451   struct record_message_args args;
452
453   args.regcache = regcache;
454   args.signal = signal;
455   return catch_errors (record_message, &args, NULL, RETURN_MASK_ALL);
456 }
457
458 /* Set to 1 if record_store_registers and record_xfer_partial
459    doesn't need record.  */
460
461 static int record_gdb_operation_disable = 0;
462
463 struct cleanup *
464 record_gdb_operation_disable_set (void)
465 {
466   struct cleanup *old_cleanups = NULL;
467
468   old_cleanups =
469     make_cleanup_restore_integer (&record_gdb_operation_disable);
470   record_gdb_operation_disable = 1;
471
472   return old_cleanups;
473 }
474
475 static void
476 record_open (char *name, int from_tty)
477 {
478   struct target_ops *t;
479
480   if (record_debug)
481     fprintf_unfiltered (gdb_stdlog, "Process record: record_open\n");
482
483   /* check exec */
484   if (!target_has_execution)
485     error (_("Process record: the program is not being run."));
486   if (non_stop)
487     error (_("Process record target can't debug inferior in non-stop mode "
488              "(non-stop)."));
489   if (target_async_permitted)
490     error (_("Process record target can't debug inferior in asynchronous "
491              "mode (target-async)."));
492
493   if (!gdbarch_process_record_p (target_gdbarch))
494     error (_("Process record: the current architecture doesn't support "
495              "record function."));
496
497   /* Check if record target is already running.  */
498   if (current_target.to_stratum == record_stratum)
499     {
500       if (!nquery
501           (_("Process record target already running, do you want to delete "
502              "the old record log?")))
503         return;
504     }
505
506   /*Reset the beneath function pointers.  */
507   record_beneath_to_resume = NULL;
508   record_beneath_to_wait = NULL;
509   record_beneath_to_store_registers = NULL;
510   record_beneath_to_xfer_partial = NULL;
511   record_beneath_to_insert_breakpoint = NULL;
512   record_beneath_to_remove_breakpoint = NULL;
513
514   /* Set the beneath function pointers.  */
515   for (t = current_target.beneath; t != NULL; t = t->beneath)
516     {
517       if (!record_beneath_to_resume)
518         {
519           record_beneath_to_resume = t->to_resume;
520           record_beneath_to_resume_ops = t;
521         }
522       if (!record_beneath_to_wait)
523         {
524           record_beneath_to_wait = t->to_wait;
525           record_beneath_to_wait_ops = t;
526         }
527       if (!record_beneath_to_store_registers)
528         {
529           record_beneath_to_store_registers = t->to_store_registers;
530           record_beneath_to_store_registers_ops = t;
531         }
532       if (!record_beneath_to_xfer_partial)
533         {
534           record_beneath_to_xfer_partial = t->to_xfer_partial;
535           record_beneath_to_xfer_partial_ops = t;
536         }
537       if (!record_beneath_to_insert_breakpoint)
538         record_beneath_to_insert_breakpoint = t->to_insert_breakpoint;
539       if (!record_beneath_to_remove_breakpoint)
540         record_beneath_to_remove_breakpoint = t->to_remove_breakpoint;
541     }
542   if (!record_beneath_to_resume)
543     error (_("Process record can't get to_resume."));
544   if (!record_beneath_to_wait)
545     error (_("Process record can't get to_wait."));
546   if (!record_beneath_to_store_registers)
547     error (_("Process record can't get to_store_registers."));
548   if (!record_beneath_to_xfer_partial)
549     error (_("Process record can't get to_xfer_partial."));
550   if (!record_beneath_to_insert_breakpoint)
551     error (_("Process record can't get to_insert_breakpoint."));
552   if (!record_beneath_to_remove_breakpoint)
553     error (_("Process record can't get to_remove_breakpoint."));
554
555   push_target (&record_ops);
556
557   /* Reset */
558   record_insn_num = 0;
559   record_list = &record_first;
560   record_list->next = NULL;
561 }
562
563 static void
564 record_close (int quitting)
565 {
566   if (record_debug)
567     fprintf_unfiltered (gdb_stdlog, "Process record: record_close\n");
568
569   record_list_release (record_list);
570 }
571
572 static int record_resume_step = 0;
573 static int record_resume_error;
574
575 static void
576 record_resume (struct target_ops *ops, ptid_t ptid, int step,
577                enum target_signal signal)
578 {
579   record_resume_step = step;
580
581   if (!RECORD_IS_REPLAY)
582     {
583       if (do_record_message (get_current_regcache (), signal))
584         {
585           record_resume_error = 0;
586         }
587       else
588         {
589           record_resume_error = 1;
590           return;
591         }
592       record_beneath_to_resume (record_beneath_to_resume_ops, ptid, 1,
593                                 signal);
594     }
595 }
596
597 static int record_get_sig = 0;
598
599 static void
600 record_sig_handler (int signo)
601 {
602   if (record_debug)
603     fprintf_unfiltered (gdb_stdlog, "Process record: get a signal\n");
604
605   /* It will break the running inferior in replay mode.  */
606   record_resume_step = 1;
607
608   /* It will let record_wait set inferior status to get the signal
609      SIGINT.  */
610   record_get_sig = 1;
611 }
612
613 static void
614 record_wait_cleanups (void *ignore)
615 {
616   if (execution_direction == EXEC_REVERSE)
617     {
618       if (record_list->next)
619         record_list = record_list->next;
620     }
621   else
622     record_list = record_list->prev;
623 }
624
625 /* In replay mode, this function examines the recorded log and
626    determines where to stop.  */
627
628 static ptid_t
629 record_wait (struct target_ops *ops,
630              ptid_t ptid, struct target_waitstatus *status,
631              int options)
632 {
633   struct cleanup *set_cleanups = record_gdb_operation_disable_set ();
634
635   if (record_debug)
636     fprintf_unfiltered (gdb_stdlog,
637                         "Process record: record_wait "
638                         "record_resume_step = %d\n",
639                         record_resume_step);
640
641   if (!RECORD_IS_REPLAY)
642     {
643       if (record_resume_error)
644         {
645           /* If record_resume get error, return directly.  */
646           status->kind = TARGET_WAITKIND_STOPPED;
647           status->value.sig = TARGET_SIGNAL_ABRT;
648           return inferior_ptid;
649         }
650
651       if (record_resume_step)
652         {
653           /* This is a single step.  */
654           return record_beneath_to_wait (record_beneath_to_wait_ops,
655                                          ptid, status, options);
656         }
657       else
658         {
659           /* This is not a single step.  */
660           ptid_t ret;
661           CORE_ADDR tmp_pc;
662
663           while (1)
664             {
665               ret = record_beneath_to_wait (record_beneath_to_wait_ops,
666                                             ptid, status, options);
667
668               /* Is this a SIGTRAP?  */
669               if (status->kind == TARGET_WAITKIND_STOPPED
670                   && status->value.sig == TARGET_SIGNAL_TRAP)
671                 {
672                   /* Yes -- check if there is a breakpoint.  */
673                   registers_changed ();
674                   tmp_pc = regcache_read_pc (get_current_regcache ());
675                   if (breakpoint_inserted_here_p (tmp_pc))
676                     {
677                       /* There is a breakpoint.  GDB will want to stop.  */
678                       CORE_ADDR decr_pc_after_break =
679                         gdbarch_decr_pc_after_break
680                         (get_regcache_arch (get_current_regcache ()));
681                       if (decr_pc_after_break)
682                         {
683                           regcache_write_pc (get_thread_regcache (ret),
684                                              tmp_pc + decr_pc_after_break);
685                         }
686                     }
687                   else
688                     {
689                       /* There is not a breakpoint, and gdb is not
690                          stepping, therefore gdb will not stop.
691                          Therefore we will not return to gdb.
692                          Record the insn and resume.  */
693                       if (!do_record_message (get_current_regcache (),
694                                               TARGET_SIGNAL_0))
695                         {
696                           break;
697                         }
698                       record_beneath_to_resume (record_beneath_to_resume_ops,
699                                                 ptid, 1,
700                                                 TARGET_SIGNAL_0);
701                       continue;
702                     }
703                 }
704
705               /* The inferior is broken by a breakpoint or a signal.  */
706               break;
707             }
708
709           return ret;
710         }
711     }
712   else
713     {
714       struct regcache *regcache = get_current_regcache ();
715       struct gdbarch *gdbarch = get_regcache_arch (regcache);
716       int continue_flag = 1;
717       int first_record_end = 1;
718       struct cleanup *old_cleanups = make_cleanup (record_wait_cleanups, 0);
719       CORE_ADDR tmp_pc;
720
721       status->kind = TARGET_WAITKIND_STOPPED;
722
723       /* Check breakpoint when forward execute.  */
724       if (execution_direction == EXEC_FORWARD)
725         {
726           tmp_pc = regcache_read_pc (regcache);
727           if (breakpoint_inserted_here_p (tmp_pc))
728             {
729               if (record_debug)
730                 fprintf_unfiltered (gdb_stdlog,
731                                     "Process record: break at %s.\n",
732                                     paddress (gdbarch, tmp_pc));
733               if (gdbarch_decr_pc_after_break (gdbarch)
734                   && !record_resume_step)
735                 regcache_write_pc (regcache,
736                                    tmp_pc +
737                                    gdbarch_decr_pc_after_break (gdbarch));
738               goto replay_out;
739             }
740         }
741
742       record_get_sig = 0;
743       signal (SIGINT, record_sig_handler);
744       /* If GDB is in terminal_inferior mode, it will not get the signal.
745          And in GDB replay mode, GDB doesn't need to be in terminal_inferior
746          mode, because inferior will not executed.
747          Then set it to terminal_ours to make GDB get the signal.  */
748       target_terminal_ours ();
749
750       /* In EXEC_FORWARD mode, record_list points to the tail of prev
751          instruction.  */
752       if (execution_direction == EXEC_FORWARD && record_list->next)
753         record_list = record_list->next;
754
755       /* Loop over the record_list, looking for the next place to
756          stop.  */
757       do
758         {
759           /* Check for beginning and end of log.  */
760           if (execution_direction == EXEC_REVERSE
761               && record_list == &record_first)
762             {
763               /* Hit beginning of record log in reverse.  */
764               status->kind = TARGET_WAITKIND_NO_HISTORY;
765               break;
766             }
767           if (execution_direction != EXEC_REVERSE && !record_list->next)
768             {
769               /* Hit end of record log going forward.  */
770               status->kind = TARGET_WAITKIND_NO_HISTORY;
771               break;
772             }
773
774           /* Set ptid, register and memory according to record_list.  */
775           if (record_list->type == record_reg)
776             {
777               /* reg */
778               gdb_byte reg[MAX_REGISTER_SIZE];
779               if (record_debug > 1)
780                 fprintf_unfiltered (gdb_stdlog,
781                                     "Process record: record_reg %s to "
782                                     "inferior num = %d.\n",
783                                     host_address_to_string (record_list),
784                                     record_list->u.reg.num);
785               regcache_cooked_read (regcache, record_list->u.reg.num, reg);
786               regcache_cooked_write (regcache, record_list->u.reg.num,
787                                      record_list->u.reg.val);
788               memcpy (record_list->u.reg.val, reg, MAX_REGISTER_SIZE);
789             }
790           else if (record_list->type == record_mem)
791             {
792               /* mem */
793               /* Nothing to do if the entry is flagged not_accessible.  */
794               if (!record_list->u.mem.mem_entry_not_accessible)
795                 {
796                   gdb_byte *mem = alloca (record_list->u.mem.len);
797                   if (record_debug > 1)
798                     fprintf_unfiltered (gdb_stdlog,
799                                         "Process record: record_mem %s to "
800                                         "inferior addr = %s len = %d.\n",
801                                         host_address_to_string (record_list),
802                                         paddress (gdbarch,
803                                                   record_list->u.mem.addr),
804                                         record_list->u.mem.len);
805
806                   if (target_read_memory (record_list->u.mem.addr, mem,
807                                           record_list->u.mem.len))
808                     {
809                       if (execution_direction != EXEC_REVERSE)
810                         error (_("Process record: error reading memory at "
811                                  "addr = %s len = %d."),
812                                paddress (gdbarch, record_list->u.mem.addr),
813                                record_list->u.mem.len);
814                       else
815                         /* Read failed -- 
816                            flag entry as not_accessible.  */
817                         record_list->u.mem.mem_entry_not_accessible = 1;
818                     }
819                   else
820                     {
821                       if (target_write_memory (record_list->u.mem.addr,
822                                                record_list->u.mem.val,
823                                                record_list->u.mem.len))
824                         {
825                           if (execution_direction != EXEC_REVERSE)
826                             error (_("Process record: error writing memory at "
827                                      "addr = %s len = %d."),
828                                    paddress (gdbarch, record_list->u.mem.addr),
829                                    record_list->u.mem.len);
830                           else
831                             /* Write failed -- 
832                                flag entry as not_accessible.  */
833                             record_list->u.mem.mem_entry_not_accessible = 1;
834                         }
835                       else
836                         {
837                           memcpy (record_list->u.mem.val, mem,
838                                   record_list->u.mem.len);
839                         }
840                     }
841                 }
842             }
843           else
844             {
845               if (record_debug > 1)
846                 fprintf_unfiltered (gdb_stdlog,
847                                     "Process record: record_end %s to "
848                                     "inferior.\n",
849                                     host_address_to_string (record_list));
850
851               if (first_record_end && execution_direction == EXEC_REVERSE)
852                 {
853                   /* When reverse excute, the first record_end is the part of
854                      current instruction.  */
855                   first_record_end = 0;
856                 }
857               else
858                 {
859                   /* In EXEC_REVERSE mode, this is the record_end of prev
860                      instruction.
861                      In EXEC_FORWARD mode, this is the record_end of current
862                      instruction.  */
863                   /* step */
864                   if (record_resume_step)
865                     {
866                       if (record_debug > 1)
867                         fprintf_unfiltered (gdb_stdlog,
868                                             "Process record: step.\n");
869                       continue_flag = 0;
870                     }
871
872                   /* check breakpoint */
873                   tmp_pc = regcache_read_pc (regcache);
874                   if (breakpoint_inserted_here_p (tmp_pc))
875                     {
876                       if (record_debug)
877                         fprintf_unfiltered (gdb_stdlog,
878                                             "Process record: break "
879                                             "at %s.\n",
880                                             paddress (gdbarch, tmp_pc));
881                       if (gdbarch_decr_pc_after_break (gdbarch)
882                           && execution_direction == EXEC_FORWARD
883                           && !record_resume_step)
884                         regcache_write_pc (regcache,
885                                            tmp_pc +
886                                            gdbarch_decr_pc_after_break (gdbarch));
887                       continue_flag = 0;
888                     }
889                   /* Check target signal */
890                   if (record_list->u.end.sigval != TARGET_SIGNAL_0)
891                     /* FIXME: better way to check */
892                     continue_flag = 0;
893                 }
894             }
895
896           if (continue_flag)
897             {
898               if (execution_direction == EXEC_REVERSE)
899                 {
900                   if (record_list->prev)
901                     record_list = record_list->prev;
902                 }
903               else
904                 {
905                   if (record_list->next)
906                     record_list = record_list->next;
907                 }
908             }
909         }
910       while (continue_flag);
911
912       signal (SIGINT, handle_sigint);
913
914 replay_out:
915       if (record_get_sig)
916         status->value.sig = TARGET_SIGNAL_INT;
917       else if (record_list->u.end.sigval != TARGET_SIGNAL_0)
918         /* FIXME: better way to check */
919         status->value.sig = record_list->u.end.sigval;
920       else
921         status->value.sig = TARGET_SIGNAL_TRAP;
922
923       discard_cleanups (old_cleanups);
924     }
925
926   do_cleanups (set_cleanups);
927   return inferior_ptid;
928 }
929
930 static void
931 record_disconnect (struct target_ops *target, char *args, int from_tty)
932 {
933   if (record_debug)
934     fprintf_unfiltered (gdb_stdlog, "Process record: record_disconnect\n");
935
936   unpush_target (&record_ops);
937   target_disconnect (args, from_tty);
938 }
939
940 static void
941 record_detach (struct target_ops *ops, char *args, int from_tty)
942 {
943   if (record_debug)
944     fprintf_unfiltered (gdb_stdlog, "Process record: record_detach\n");
945
946   unpush_target (&record_ops);
947   target_detach (args, from_tty);
948 }
949
950 static void
951 record_mourn_inferior (struct target_ops *ops)
952 {
953   if (record_debug)
954     fprintf_unfiltered (gdb_stdlog, "Process record: "
955                                     "record_mourn_inferior\n");
956
957   unpush_target (&record_ops);
958   target_mourn_inferior ();
959 }
960
961 /* Close process record target before killing the inferior process.  */
962
963 static void
964 record_kill (struct target_ops *ops)
965 {
966   if (record_debug)
967     fprintf_unfiltered (gdb_stdlog, "Process record: record_kill\n");
968
969   unpush_target (&record_ops);
970   target_kill ();
971 }
972
973 /* Record registers change (by user or by GDB) to list as an instruction.  */
974
975 static void
976 record_registers_change (struct regcache *regcache, int regnum)
977 {
978   /* Check record_insn_num.  */
979   record_check_insn_num (0);
980
981   record_arch_list_head = NULL;
982   record_arch_list_tail = NULL;
983
984   if (regnum < 0)
985     {
986       int i;
987       for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
988         {
989           if (record_arch_list_add_reg (regcache, i))
990             {
991               record_list_release (record_arch_list_tail);
992               error (_("Process record: failed to record execution log."));
993             }
994         }
995     }
996   else
997     {
998       if (record_arch_list_add_reg (regcache, regnum))
999         {
1000           record_list_release (record_arch_list_tail);
1001           error (_("Process record: failed to record execution log."));
1002         }
1003     }
1004   if (record_arch_list_add_end ())
1005     {
1006       record_list_release (record_arch_list_tail);
1007       error (_("Process record: failed to record execution log."));
1008     }
1009   record_list->next = record_arch_list_head;
1010   record_arch_list_head->prev = record_list;
1011   record_list = record_arch_list_tail;
1012
1013   if (record_insn_num == record_insn_max_num && record_insn_max_num)
1014     record_list_release_first ();
1015   else
1016     record_insn_num++;
1017 }
1018
1019 static void
1020 record_store_registers (struct target_ops *ops, struct regcache *regcache,
1021                         int regno)
1022 {
1023   if (!record_gdb_operation_disable)
1024     {
1025       if (RECORD_IS_REPLAY)
1026         {
1027           int n;
1028
1029           /* Let user choose if he wants to write register or not.  */
1030           if (regno < 0)
1031             n =
1032               nquery (_("Because GDB is in replay mode, changing the "
1033                         "value of a register will make the execution "
1034                         "log unusable from this point onward.  "
1035                         "Change all registers?"));
1036           else
1037             n =
1038               nquery (_("Because GDB is in replay mode, changing the value "
1039                         "of a register will make the execution log unusable "
1040                         "from this point onward.  Change register %s?"),
1041                       gdbarch_register_name (get_regcache_arch (regcache),
1042                                                regno));
1043
1044           if (!n)
1045             {
1046               /* Invalidate the value of regcache that was set in function
1047                  "regcache_raw_write".  */
1048               if (regno < 0)
1049                 {
1050                   int i;
1051                   for (i = 0;
1052                        i < gdbarch_num_regs (get_regcache_arch (regcache));
1053                        i++)
1054                     regcache_invalidate (regcache, i);
1055                 }
1056               else
1057                 regcache_invalidate (regcache, regno);
1058
1059               error (_("Process record canceled the operation."));
1060             }
1061
1062           /* Destroy the record from here forward.  */
1063           record_list_release_next ();
1064         }
1065
1066       record_registers_change (regcache, regno);
1067     }
1068   record_beneath_to_store_registers (record_beneath_to_store_registers_ops,
1069                                      regcache, regno);
1070 }
1071
1072 /* Behavior is conditional on RECORD_IS_REPLAY.
1073    In replay mode, we cannot write memory unles we are willing to
1074    invalidate the record/replay log from this point forward.  */
1075
1076 static LONGEST
1077 record_xfer_partial (struct target_ops *ops, enum target_object object,
1078                      const char *annex, gdb_byte *readbuf,
1079                      const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
1080 {
1081   if (!record_gdb_operation_disable
1082       && (object == TARGET_OBJECT_MEMORY
1083           || object == TARGET_OBJECT_RAW_MEMORY) && writebuf)
1084     {
1085       if (RECORD_IS_REPLAY)
1086         {
1087           /* Let user choose if he wants to write memory or not.  */
1088           if (!nquery (_("Because GDB is in replay mode, writing to memory "
1089                          "will make the execution log unusable from this "
1090                          "point onward.  Write memory at address %s?"),
1091                        paddress (target_gdbarch, offset)))
1092             error (_("Process record canceled the operation."));
1093
1094           /* Destroy the record from here forward.  */
1095           record_list_release_next ();
1096         }
1097
1098       /* Check record_insn_num */
1099       record_check_insn_num (0);
1100
1101       /* Record registers change to list as an instruction.  */
1102       record_arch_list_head = NULL;
1103       record_arch_list_tail = NULL;
1104       if (record_arch_list_add_mem (offset, len))
1105         {
1106           record_list_release (record_arch_list_tail);
1107           if (record_debug)
1108             fprintf_unfiltered (gdb_stdlog,
1109                                 _("Process record: failed to record "
1110                                   "execution log."));
1111           return -1;
1112         }
1113       if (record_arch_list_add_end ())
1114         {
1115           record_list_release (record_arch_list_tail);
1116           if (record_debug)
1117             fprintf_unfiltered (gdb_stdlog,
1118                                 _("Process record: failed to record "
1119                                   "execution log."));
1120           return -1;
1121         }
1122       record_list->next = record_arch_list_head;
1123       record_arch_list_head->prev = record_list;
1124       record_list = record_arch_list_tail;
1125
1126       if (record_insn_num == record_insn_max_num && record_insn_max_num)
1127         record_list_release_first ();
1128       else
1129         record_insn_num++;
1130     }
1131
1132   return record_beneath_to_xfer_partial (record_beneath_to_xfer_partial_ops,
1133                                          object, annex, readbuf, writebuf,
1134                                          offset, len);
1135 }
1136
1137 /* Behavior is conditional on RECORD_IS_REPLAY.
1138    We will not actually insert or remove breakpoints when replaying,
1139    nor when recording.  */
1140
1141 static int
1142 record_insert_breakpoint (struct gdbarch *gdbarch,
1143                           struct bp_target_info *bp_tgt)
1144 {
1145   if (!RECORD_IS_REPLAY)
1146     {
1147       struct cleanup *old_cleanups = record_gdb_operation_disable_set ();
1148       int ret = record_beneath_to_insert_breakpoint (gdbarch, bp_tgt);
1149
1150       do_cleanups (old_cleanups);
1151
1152       return ret;
1153     }
1154
1155   return 0;
1156 }
1157
1158 static int
1159 record_remove_breakpoint (struct gdbarch *gdbarch,
1160                           struct bp_target_info *bp_tgt)
1161 {
1162   if (!RECORD_IS_REPLAY)
1163     {
1164       struct cleanup *old_cleanups = record_gdb_operation_disable_set ();
1165       int ret = record_beneath_to_remove_breakpoint (gdbarch, bp_tgt);
1166
1167       do_cleanups (old_cleanups);
1168
1169       return ret;
1170     }
1171
1172   return 0;
1173 }
1174
1175 static int
1176 record_can_execute_reverse (void)
1177 {
1178   return 1;
1179 }
1180
1181 static void
1182 init_record_ops (void)
1183 {
1184   record_ops.to_shortname = "record";
1185   record_ops.to_longname = "Process record and replay target";
1186   record_ops.to_doc =
1187     "Log program while executing and replay execution from log.";
1188   record_ops.to_open = record_open;
1189   record_ops.to_close = record_close;
1190   record_ops.to_resume = record_resume;
1191   record_ops.to_wait = record_wait;
1192   record_ops.to_disconnect = record_disconnect;
1193   record_ops.to_detach = record_detach;
1194   record_ops.to_mourn_inferior = record_mourn_inferior;
1195   record_ops.to_kill = record_kill;
1196   record_ops.to_create_inferior = find_default_create_inferior;
1197   record_ops.to_store_registers = record_store_registers;
1198   record_ops.to_xfer_partial = record_xfer_partial;
1199   record_ops.to_insert_breakpoint = record_insert_breakpoint;
1200   record_ops.to_remove_breakpoint = record_remove_breakpoint;
1201   record_ops.to_can_execute_reverse = record_can_execute_reverse;
1202   record_ops.to_stratum = record_stratum;
1203   record_ops.to_magic = OPS_MAGIC;
1204 }
1205
1206 static void
1207 show_record_debug (struct ui_file *file, int from_tty,
1208                    struct cmd_list_element *c, const char *value)
1209 {
1210   fprintf_filtered (file, _("Debugging of process record target is %s.\n"),
1211                     value);
1212 }
1213
1214 /* Alias for "target record".  */
1215
1216 static void
1217 cmd_record_start (char *args, int from_tty)
1218 {
1219   execute_command ("target record", from_tty);
1220 }
1221
1222 /* Truncate the record log from the present point
1223    of replay until the end.  */
1224
1225 static void
1226 cmd_record_delete (char *args, int from_tty)
1227 {
1228   if (current_target.to_stratum == record_stratum)
1229     {
1230       if (RECORD_IS_REPLAY)
1231         {
1232           if (!from_tty || query (_("Delete the log from this point forward "
1233                                     "and begin to record the running message "
1234                                     "at current PC?")))
1235             record_list_release_next ();
1236         }
1237       else
1238           printf_unfiltered (_("Already at end of record list.\n"));
1239
1240     }
1241   else
1242     printf_unfiltered (_("Process record is not started.\n"));
1243 }
1244
1245 /* Implement the "stoprecord" command.  */
1246
1247 static void
1248 cmd_record_stop (char *args, int from_tty)
1249 {
1250   if (current_target.to_stratum == record_stratum)
1251     {
1252       if (!record_list || !from_tty || query (_("Delete recorded log and "
1253                                                 "stop recording?")))
1254         unpush_target (&record_ops);
1255     }
1256   else
1257     printf_unfiltered (_("Process record is not started.\n"));
1258 }
1259
1260 /* Set upper limit of record log size.  */
1261
1262 static void
1263 set_record_insn_max_num (char *args, int from_tty, struct cmd_list_element *c)
1264 {
1265   if (record_insn_num > record_insn_max_num && record_insn_max_num)
1266     {
1267       printf_unfiltered (_("Record instructions number is bigger than "
1268                            "record instructions max number.  Auto delete "
1269                            "the first ones?\n"));
1270
1271       while (record_insn_num > record_insn_max_num)
1272         record_list_release_first ();
1273     }
1274 }
1275
1276 /* Print the current index into the record log (number of insns recorded
1277    so far).  */
1278
1279 static void
1280 show_record_insn_number (char *ignore, int from_tty)
1281 {
1282   printf_unfiltered (_("Record instruction number is %d.\n"),
1283                      record_insn_num);
1284 }
1285
1286 static struct cmd_list_element *record_cmdlist, *set_record_cmdlist,
1287                                *show_record_cmdlist, *info_record_cmdlist;
1288
1289 static void
1290 set_record_command (char *args, int from_tty)
1291 {
1292   printf_unfiltered (_("\
1293 \"set record\" must be followed by an apporpriate subcommand.\n"));
1294   help_list (set_record_cmdlist, "set record ", all_commands, gdb_stdout);
1295 }
1296
1297 static void
1298 show_record_command (char *args, int from_tty)
1299 {
1300   cmd_show_list (show_record_cmdlist, from_tty, "");
1301 }
1302
1303 static void
1304 info_record_command (char *args, int from_tty)
1305 {
1306   cmd_show_list (info_record_cmdlist, from_tty, "");
1307 }
1308
1309 void
1310 _initialize_record (void)
1311 {
1312   /* Init record_first.  */
1313   record_first.prev = NULL;
1314   record_first.next = NULL;
1315   record_first.type = record_end;
1316
1317   init_record_ops ();
1318   add_target (&record_ops);
1319
1320   add_setshow_zinteger_cmd ("record", no_class, &record_debug,
1321                             _("Set debugging of record/replay feature."),
1322                             _("Show debugging of record/replay feature."),
1323                             _("When enabled, debugging output for "
1324                               "record/replay feature is displayed."),
1325                             NULL, show_record_debug, &setdebuglist,
1326                             &showdebuglist);
1327
1328   add_prefix_cmd ("record", class_obscure, cmd_record_start,
1329                   _("Abbreviated form of \"target record\" command."),
1330                   &record_cmdlist, "record ", 0, &cmdlist);
1331   add_com_alias ("rec", "record", class_obscure, 1);
1332   add_prefix_cmd ("record", class_support, set_record_command,
1333                   _("Set record options"), &set_record_cmdlist,
1334                   "set record ", 0, &setlist);
1335   add_alias_cmd ("rec", "record", class_obscure, 1, &setlist);
1336   add_prefix_cmd ("record", class_support, show_record_command,
1337                   _("Show record options"), &show_record_cmdlist,
1338                   "show record ", 0, &showlist);
1339   add_alias_cmd ("rec", "record", class_obscure, 1, &showlist);
1340   add_prefix_cmd ("record", class_support, info_record_command,
1341                   _("Info record options"), &info_record_cmdlist,
1342                   "info record ", 0, &infolist);
1343   add_alias_cmd ("rec", "record", class_obscure, 1, &infolist);
1344
1345
1346   add_cmd ("delete", class_obscure, cmd_record_delete,
1347            _("Delete the rest of execution log and start recording it anew."),
1348            &record_cmdlist);
1349   add_alias_cmd ("d", "delete", class_obscure, 1, &record_cmdlist);
1350   add_alias_cmd ("del", "delete", class_obscure, 1, &record_cmdlist);
1351
1352   add_cmd ("stop", class_obscure, cmd_record_stop,
1353            _("Stop the record/replay target."),
1354            &record_cmdlist);
1355   add_alias_cmd ("s", "stop", class_obscure, 1, &record_cmdlist);
1356
1357   /* Record instructions number limit command.  */
1358   add_setshow_boolean_cmd ("stop-at-limit", no_class,
1359                            &record_stop_at_limit, _("\
1360 Set whether record/replay stops when record/replay buffer becomes full."), _("\
1361 Show whether record/replay stops when record/replay buffer becomes full."), _("\
1362 Default is ON.\n\
1363 When ON, if the record/replay buffer becomes full, ask user what to do.\n\
1364 When OFF, if the record/replay buffer becomes full,\n\
1365 delete the oldest recorded instruction to make room for each new one."),
1366                            NULL, NULL,
1367                            &set_record_cmdlist, &show_record_cmdlist);
1368   add_setshow_zinteger_cmd ("insn-number-max", no_class,
1369                             &record_insn_max_num,
1370                             _("Set record/replay buffer limit."),
1371                             _("Show record/replay buffer limit."), _("\
1372 Set the maximum number of instructions to be stored in the\n\
1373 record/replay buffer.  Zero means unlimited.  Default is 200000."),
1374                             set_record_insn_max_num,
1375                             NULL, &set_record_cmdlist, &show_record_cmdlist);
1376   add_cmd ("insn-number", class_obscure, show_record_insn_number,
1377            _("Show the current number of instructions in the "
1378              "record/replay buffer."), &info_record_cmdlist);
1379 }