Provide default target methods for record targets that are likely to be shared
[deliverable/binutils-gdb.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 #define DEBUG(msg, args...) \
37 if (record_debug) \
38 fprintf_unfiltered (gdb_stdlog, "record: " msg "\n", ##args)
39
40 /* Find the record target in the target stack. */
41
42 static struct target_ops *
43 find_record_target (void)
44 {
45 struct target_ops *t;
46
47 for (t = current_target.beneath; t != NULL; t = t->beneath)
48 if (t->to_stratum == record_stratum)
49 return t;
50
51 return NULL;
52 }
53
54 /* Check that recording is active. Throw an error, if it isn't. */
55
56 static struct target_ops *
57 require_record_target (void)
58 {
59 struct target_ops *t;
60
61 t = find_record_target ();
62 if (t == NULL)
63 error (_("No record target is currently active.\n"
64 "Use one of the \"target record-<tab><tab>\" commands first."));
65
66 return t;
67 }
68
69 /* See record.h. */
70
71 int
72 record_read_memory (struct gdbarch *gdbarch,
73 CORE_ADDR memaddr, gdb_byte *myaddr,
74 ssize_t len)
75 {
76 int ret = target_read_memory (memaddr, myaddr, len);
77
78 if (ret != 0)
79 DEBUG ("error reading memory at addr %s len = %ld.\n",
80 paddress (gdbarch, memaddr), (long) len);
81
82 return ret;
83 }
84
85 /* Stop recording. */
86
87 static void
88 record_stop (struct target_ops *t)
89 {
90 DEBUG ("stop %s", t->to_shortname);
91
92 if (t->to_stop_recording != NULL)
93 t->to_stop_recording ();
94 }
95
96 /* Unpush the record target. */
97
98 static void
99 record_unpush (struct target_ops *t)
100 {
101 DEBUG ("unpush %s", t->to_shortname);
102
103 unpush_target (t);
104 }
105
106 /* See record.h. */
107
108 void
109 record_disconnect (struct target_ops *t, char *args, int from_tty)
110 {
111 gdb_assert (t->to_stratum == record_stratum);
112
113 DEBUG ("disconnect %s", t->to_shortname);
114
115 record_stop (t);
116 record_unpush (t);
117
118 target_disconnect (args, from_tty);
119 }
120
121 /* See record.h. */
122
123 void
124 record_detach (struct target_ops *t, char *args, int from_tty)
125 {
126 gdb_assert (t->to_stratum == record_stratum);
127
128 DEBUG ("detach %s", t->to_shortname);
129
130 record_stop (t);
131 record_unpush (t);
132
133 target_detach (args, from_tty);
134 }
135
136 /* See record.h. */
137
138 void
139 record_mourn_inferior (struct target_ops *t)
140 {
141 gdb_assert (t->to_stratum == record_stratum);
142
143 DEBUG ("mourn inferior %s", t->to_shortname);
144
145 /* It is safer to not stop recording. Resources will be freed when
146 threads are discarded. */
147 record_unpush (t);
148
149 target_mourn_inferior ();
150 }
151
152 /* See record.h. */
153
154 void
155 record_kill (struct target_ops *t)
156 {
157 gdb_assert (t->to_stratum == record_stratum);
158
159 DEBUG ("kill %s", t->to_shortname);
160
161 /* It is safer to not stop recording. Resources will be freed when
162 threads are discarded. */
163 record_unpush (t);
164
165 target_kill ();
166 }
167
168 /* Implement "show record debug" command. */
169
170 static void
171 show_record_debug (struct ui_file *file, int from_tty,
172 struct cmd_list_element *c, const char *value)
173 {
174 fprintf_filtered (file, _("Debugging of process record target is %s.\n"),
175 value);
176 }
177
178 /* Alias for "target record". */
179
180 static void
181 cmd_record_start (char *args, int from_tty)
182 {
183 execute_command ("target record-full", from_tty);
184 }
185
186 /* Truncate the record log from the present point
187 of replay until the end. */
188
189 static void
190 cmd_record_delete (char *args, int from_tty)
191 {
192 require_record_target ();
193
194 if (!target_record_is_replaying ())
195 {
196 printf_unfiltered (_("Already at end of record list.\n"));
197 return;
198 }
199
200 if (!target_supports_delete_record ())
201 {
202 printf_unfiltered (_("The current record target does not support "
203 "this operation.\n"));
204 return;
205 }
206
207 if (!from_tty || query (_("Delete the log from this point forward "
208 "and begin to record the running message "
209 "at current PC?")))
210 target_delete_record ();
211 }
212
213 /* Implement the "stoprecord" or "record stop" command. */
214
215 static void
216 cmd_record_stop (char *args, int from_tty)
217 {
218 struct target_ops *t;
219
220 t = require_record_target ();
221
222 record_stop (t);
223 record_unpush (t);
224
225 printf_unfiltered (_("Process record is stopped and all execution "
226 "logs are deleted.\n"));
227
228 observer_notify_record_changed (current_inferior (), 0);
229 }
230
231 /* The "set record" command. */
232
233 static void
234 set_record_command (char *args, int from_tty)
235 {
236 printf_unfiltered (_("\"set record\" must be followed "
237 "by an apporpriate subcommand.\n"));
238 help_list (set_record_cmdlist, "set record ", all_commands, gdb_stdout);
239 }
240
241 /* The "show record" command. */
242
243 static void
244 show_record_command (char *args, int from_tty)
245 {
246 cmd_show_list (show_record_cmdlist, from_tty, "");
247 }
248
249 /* The "info record" command. */
250
251 static void
252 info_record_command (char *args, int from_tty)
253 {
254 struct target_ops *t;
255
256 t = find_record_target ();
257 if (t == NULL)
258 {
259 printf_filtered (_("No record target is currently active.\n"));
260 return;
261 }
262
263 printf_filtered (_("Active record target: %s\n"), t->to_shortname);
264 if (t->to_info_record != NULL)
265 t->to_info_record ();
266 }
267
268 /* The "record save" command. */
269
270 static void
271 cmd_record_save (char *args, int from_tty)
272 {
273 char *recfilename, recfilename_buffer[40];
274
275 require_record_target ();
276
277 if (args != NULL && *args != 0)
278 recfilename = args;
279 else
280 {
281 /* Default recfile name is "gdb_record.PID". */
282 xsnprintf (recfilename_buffer, sizeof (recfilename_buffer),
283 "gdb_record.%d", PIDGET (inferior_ptid));
284 recfilename = recfilename_buffer;
285 }
286
287 target_save_record (recfilename);
288 }
289
290 /* "record goto" command. Argument is an instruction number,
291 as given by "info record".
292
293 Rewinds the recording (forward or backward) to the given instruction. */
294
295 void
296 cmd_record_goto (char *arg, int from_tty)
297 {
298 require_record_target ();
299
300 if (arg == NULL || *arg == '\0')
301 error (_("Command requires an argument (insn number to go to)."));
302
303 if (strncmp (arg, "start", strlen ("start")) == 0
304 || strncmp (arg, "begin", strlen ("begin")) == 0)
305 target_goto_record_begin ();
306 else if (strncmp (arg, "end", strlen ("end")) == 0)
307 target_goto_record_end ();
308 else
309 {
310 ULONGEST insn;
311
312 insn = parse_and_eval_long (arg);
313 target_goto_record (insn);
314 }
315 }
316
317 /* Provide a prototype to silence -Wmissing-prototypes. */
318 extern initialize_file_ftype _initialize_record;
319
320 void
321 _initialize_record (void)
322 {
323 struct cmd_list_element *c;
324
325 add_setshow_zuinteger_cmd ("record", no_class, &record_debug,
326 _("Set debugging of record/replay feature."),
327 _("Show debugging of record/replay feature."),
328 _("When enabled, debugging output for "
329 "record/replay feature is displayed."),
330 NULL, show_record_debug, &setdebuglist,
331 &showdebuglist);
332
333 c = add_prefix_cmd ("record", class_obscure, cmd_record_start,
334 _("Start recording."),
335 &record_cmdlist, "record ", 0, &cmdlist);
336 set_cmd_completer (c, filename_completer);
337
338 add_com_alias ("rec", "record", class_obscure, 1);
339 add_prefix_cmd ("record", class_support, set_record_command,
340 _("Set record options"), &set_record_cmdlist,
341 "set record ", 0, &setlist);
342 add_alias_cmd ("rec", "record", class_obscure, 1, &setlist);
343 add_prefix_cmd ("record", class_support, show_record_command,
344 _("Show record options"), &show_record_cmdlist,
345 "show record ", 0, &showlist);
346 add_alias_cmd ("rec", "record", class_obscure, 1, &showlist);
347 add_prefix_cmd ("record", class_support, info_record_command,
348 _("Info record options"), &info_record_cmdlist,
349 "info record ", 0, &infolist);
350 add_alias_cmd ("rec", "record", class_obscure, 1, &infolist);
351
352 c = add_cmd ("save", class_obscure, cmd_record_save,
353 _("Save the execution log to a file.\n\
354 Argument is optional filename.\n\
355 Default filename is 'gdb_record.<process_id>'."),
356 &record_cmdlist);
357 set_cmd_completer (c, filename_completer);
358
359 add_cmd ("delete", class_obscure, cmd_record_delete,
360 _("Delete the rest of execution log and start recording it anew."),
361 &record_cmdlist);
362 add_alias_cmd ("d", "delete", class_obscure, 1, &record_cmdlist);
363 add_alias_cmd ("del", "delete", class_obscure, 1, &record_cmdlist);
364
365 add_cmd ("stop", class_obscure, cmd_record_stop,
366 _("Stop the record/replay target."),
367 &record_cmdlist);
368 add_alias_cmd ("s", "stop", class_obscure, 1, &record_cmdlist);
369
370 add_cmd ("goto", class_obscure, cmd_record_goto, _("\
371 Restore the program to its state at instruction number N.\n\
372 Argument is instruction number, as shown by 'info record'."),
373 &record_cmdlist);
374 }
This page took 0.037769 seconds and 5 git commands to generate.