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