Split record.h into record.h and record-full.h.
[external/binutils.git] / gdb / record.c
1 /* Process record and replay target for GDB, the GNU debugger.
2
3    Copyright (C) 2008-2013 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 "completer.h"
23 #include "record.h"
24 #include "observer.h"
25 #include "inferior.h"
26 #include "common/common-utils.h"
27
28 /* This is the debug switch for process record.  */
29 unsigned int record_debug = 0;
30
31 struct cmd_list_element *record_cmdlist = NULL;
32 struct cmd_list_element *set_record_cmdlist = NULL;
33 struct cmd_list_element *show_record_cmdlist = NULL;
34 struct cmd_list_element *info_record_cmdlist = NULL;
35
36 /* Find the record target in the target stack.  */
37
38 static struct target_ops *
39 find_record_target (void)
40 {
41   struct target_ops *t;
42
43   for (t = current_target.beneath; t != NULL; t = t->beneath)
44     if (t->to_stratum == record_stratum)
45       return t;
46
47   return NULL;
48 }
49
50 /* Check that recording is active.  Throw an error, if it isn't.  */
51
52 static struct target_ops *
53 require_record_target (void)
54 {
55   struct target_ops *t;
56
57   t = find_record_target ();
58   if (t == NULL)
59     error (_("No record target is currently active.\n"
60              "Use one of the \"target record-<tab><tab>\" commands first."));
61
62   return t;
63 }
64
65 /* See record.h.  */
66
67 int
68 record_read_memory (struct gdbarch *gdbarch,
69                     CORE_ADDR memaddr, gdb_byte *myaddr,
70                     ssize_t len)
71 {
72   int ret = target_read_memory (memaddr, myaddr, len);
73
74   if (ret && record_debug)
75     printf_unfiltered (_("Process record: error reading memory "
76                          "at addr %s len = %ld.\n"),
77                        paddress (gdbarch, memaddr), (long) len);
78   return ret;
79 }
80
81 /* Implement "show record debug" command.  */
82
83 static void
84 show_record_debug (struct ui_file *file, int from_tty,
85                    struct cmd_list_element *c, const char *value)
86 {
87   fprintf_filtered (file, _("Debugging of process record target is %s.\n"),
88                     value);
89 }
90
91 /* Alias for "target record".  */
92
93 static void
94 cmd_record_start (char *args, int from_tty)
95 {
96   execute_command ("target record-full", from_tty);
97 }
98
99 /* Truncate the record log from the present point
100    of replay until the end.  */
101
102 static void
103 cmd_record_delete (char *args, int from_tty)
104 {
105   require_record_target ();
106
107   if (!target_record_is_replaying ())
108     {
109       printf_unfiltered (_("Already at end of record list.\n"));
110       return;
111     }
112
113   if (!target_supports_delete_record ())
114     {
115       printf_unfiltered (_("The current record target does not support "
116                            "this operation.\n"));
117       return;
118     }
119
120   if (!from_tty || query (_("Delete the log from this point forward "
121                             "and begin to record the running message "
122                             "at current PC?")))
123     target_delete_record ();
124 }
125
126 /* Implement the "stoprecord" or "record stop" command.  */
127
128 static void
129 cmd_record_stop (char *args, int from_tty)
130 {
131   struct target_ops *t;
132
133   t = require_record_target ();
134   unpush_target (t);
135
136   printf_unfiltered (_("Process record is stopped and all execution "
137                        "logs are deleted.\n"));
138
139   observer_notify_record_changed (current_inferior (), 0);
140 }
141
142 /* The "set record" command.  */
143
144 static void
145 set_record_command (char *args, int from_tty)
146 {
147   printf_unfiltered (_("\"set record\" must be followed "
148                        "by an apporpriate subcommand.\n"));
149   help_list (set_record_cmdlist, "set record ", all_commands, gdb_stdout);
150 }
151
152 /* The "show record" command.  */
153
154 static void
155 show_record_command (char *args, int from_tty)
156 {
157   cmd_show_list (show_record_cmdlist, from_tty, "");
158 }
159
160 /* The "info record" command.  */
161
162 static void
163 info_record_command (char *args, int from_tty)
164 {
165   struct target_ops *t;
166
167   t = find_record_target ();
168   if (t == NULL)
169     {
170       printf_filtered (_("No record target is currently active.\n"));
171       return;
172     }
173
174   printf_filtered (_("Active record target: %s\n"), t->to_shortname);
175   if (t->to_info_record != NULL)
176     t->to_info_record ();
177 }
178
179 /* The "record save" command.  */
180
181 static void
182 cmd_record_save (char *args, int from_tty)
183 {
184   char *recfilename, recfilename_buffer[40];
185
186   require_record_target ();
187
188   if (args != NULL && *args != 0)
189     recfilename = args;
190   else
191     {
192       /* Default recfile name is "gdb_record.PID".  */
193       xsnprintf (recfilename_buffer, sizeof (recfilename_buffer),
194                 "gdb_record.%d", PIDGET (inferior_ptid));
195       recfilename = recfilename_buffer;
196     }
197
198   target_save_record (recfilename);
199 }
200
201 /* "record goto" command.  Argument is an instruction number,
202    as given by "info record".
203
204    Rewinds the recording (forward or backward) to the given instruction.  */
205
206 void
207 cmd_record_goto (char *arg, int from_tty)
208 {
209   require_record_target ();
210
211   if (arg == NULL || *arg == '\0')
212     error (_("Command requires an argument (insn number to go to)."));
213
214   if (strncmp (arg, "start", strlen ("start")) == 0
215       || strncmp (arg, "begin", strlen ("begin")) == 0)
216     target_goto_record_begin ();
217   else if (strncmp (arg, "end", strlen ("end")) == 0)
218     target_goto_record_end ();
219   else
220     {
221       ULONGEST insn;
222
223       insn = parse_and_eval_long (arg);
224       target_goto_record (insn);
225     }
226 }
227
228 /* Provide a prototype to silence -Wmissing-prototypes.  */
229 extern initialize_file_ftype _initialize_record;
230
231 void
232 _initialize_record (void)
233 {
234   struct cmd_list_element *c;
235
236   add_setshow_zuinteger_cmd ("record", no_class, &record_debug,
237                              _("Set debugging of record/replay feature."),
238                              _("Show debugging of record/replay feature."),
239                              _("When enabled, debugging output for "
240                                "record/replay feature is displayed."),
241                              NULL, show_record_debug, &setdebuglist,
242                              &showdebuglist);
243
244   c = add_prefix_cmd ("record", class_obscure, cmd_record_start,
245                       _("Start recording."),
246                       &record_cmdlist, "record ", 0, &cmdlist);
247   set_cmd_completer (c, filename_completer);
248
249   add_com_alias ("rec", "record", class_obscure, 1);
250   add_prefix_cmd ("record", class_support, set_record_command,
251                   _("Set record options"), &set_record_cmdlist,
252                   "set record ", 0, &setlist);
253   add_alias_cmd ("rec", "record", class_obscure, 1, &setlist);
254   add_prefix_cmd ("record", class_support, show_record_command,
255                   _("Show record options"), &show_record_cmdlist,
256                   "show record ", 0, &showlist);
257   add_alias_cmd ("rec", "record", class_obscure, 1, &showlist);
258   add_prefix_cmd ("record", class_support, info_record_command,
259                   _("Info record options"), &info_record_cmdlist,
260                   "info record ", 0, &infolist);
261   add_alias_cmd ("rec", "record", class_obscure, 1, &infolist);
262
263   c = add_cmd ("save", class_obscure, cmd_record_save,
264                _("Save the execution log to a file.\n\
265 Argument is optional filename.\n\
266 Default filename is 'gdb_record.<process_id>'."),
267                &record_cmdlist);
268   set_cmd_completer (c, filename_completer);
269
270   add_cmd ("delete", class_obscure, cmd_record_delete,
271            _("Delete the rest of execution log and start recording it anew."),
272            &record_cmdlist);
273   add_alias_cmd ("d", "delete", class_obscure, 1, &record_cmdlist);
274   add_alias_cmd ("del", "delete", class_obscure, 1, &record_cmdlist);
275
276   add_cmd ("stop", class_obscure, cmd_record_stop,
277            _("Stop the record/replay target."),
278            &record_cmdlist);
279   add_alias_cmd ("s", "stop", class_obscure, 1, &record_cmdlist);
280
281   add_cmd ("goto", class_obscure, cmd_record_goto, _("\
282 Restore the program to its state at instruction number N.\n\
283 Argument is instruction number, as shown by 'info record'."),
284            &record_cmdlist);
285 }