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