]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/record.c
Provide default target methods for record targets that are likely to be shared
[thirdparty/binutils-gdb.git] / gdb / record.c
CommitLineData
69d05d38
HZ
1/* Process record and replay target for GDB, the GNU debugger.
2
28e7fd62 3 Copyright (C) 2008-2013 Free Software Foundation, Inc.
69d05d38
HZ
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"
0156b218 22#include "completer.h"
69d05d38 23#include "record.h"
82a90ccf 24#include "observer.h"
d02ed0bb
MM
25#include "inferior.h"
26#include "common/common-utils.h"
69d05d38 27
d02ed0bb
MM
28/* This is the debug switch for process record. */
29unsigned int record_debug = 0;
27699eea 30
d02ed0bb
MM
31struct cmd_list_element *record_cmdlist = NULL;
32struct cmd_list_element *set_record_cmdlist = NULL;
33struct cmd_list_element *show_record_cmdlist = NULL;
34struct cmd_list_element *info_record_cmdlist = NULL;
27699eea 35
7c1687a9
MM
36#define DEBUG(msg, args...) \
37 if (record_debug) \
38 fprintf_unfiltered (gdb_stdlog, "record: " msg "\n", ##args)
39
d02ed0bb 40/* Find the record target in the target stack. */
27699eea 41
d02ed0bb
MM
42static struct target_ops *
43find_record_target (void)
27699eea 44{
d02ed0bb 45 struct target_ops *t;
123f5f96 46
d02ed0bb
MM
47 for (t = current_target.beneath; t != NULL; t = t->beneath)
48 if (t->to_stratum == record_stratum)
49 return t;
27699eea 50
d02ed0bb 51 return NULL;
27699eea
MS
52}
53
d02ed0bb 54/* Check that recording is active. Throw an error, if it isn't. */
27699eea 55
d02ed0bb
MM
56static struct target_ops *
57require_record_target (void)
27699eea 58{
d02ed0bb 59 struct target_ops *t;
27699eea 60
d02ed0bb
MM
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."));
27699eea 65
d02ed0bb 66 return t;
27699eea
MS
67}
68
d02ed0bb 69/* See record.h. */
27699eea 70
d02ed0bb
MM
71int
72record_read_memory (struct gdbarch *gdbarch,
73 CORE_ADDR memaddr, gdb_byte *myaddr,
74 ssize_t len)
27699eea 75{
d02ed0bb 76 int ret = target_read_memory (memaddr, myaddr, len);
27699eea 77
7c1687a9
MM
78 if (ret != 0)
79 DEBUG ("error reading memory at addr %s len = %ld.\n",
80 paddress (gdbarch, memaddr), (long) len);
81
d02ed0bb 82 return ret;
27699eea
MS
83}
84
7c1687a9
MM
85/* Stop recording. */
86
87static void
88record_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
98static void
99record_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
108void
109record_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
123void
124record_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
138void
139record_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
154void
155record_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
6df67667
MS
168/* Implement "show record debug" command. */
169
69d05d38
HZ
170static void
171show_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
180static void
181cmd_record_start (char *args, int from_tty)
182{
d02ed0bb 183 execute_command ("target record-full", from_tty);
69d05d38
HZ
184}
185
186/* Truncate the record log from the present point
187 of replay until the end. */
188
189static void
190cmd_record_delete (char *args, int from_tty)
191{
d02ed0bb
MM
192 require_record_target ();
193
194 if (!target_record_is_replaying ())
69d05d38 195 {
d02ed0bb
MM
196 printf_unfiltered (_("Already at end of record list.\n"));
197 return;
198 }
69d05d38 199
d02ed0bb
MM
200 if (!target_supports_delete_record ())
201 {
202 printf_unfiltered (_("The current record target does not support "
203 "this operation.\n"));
204 return;
69d05d38 205 }
d02ed0bb
MM
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 ();
69d05d38
HZ
211}
212
6df67667 213/* Implement the "stoprecord" or "record stop" command. */
69d05d38
HZ
214
215static void
216cmd_record_stop (char *args, int from_tty)
217{
d02ed0bb 218 struct target_ops *t;
82a90ccf 219
d02ed0bb 220 t = require_record_target ();
7c1687a9
MM
221
222 record_stop (t);
223 record_unpush (t);
69d05d38 224
d02ed0bb
MM
225 printf_unfiltered (_("Process record is stopped and all execution "
226 "logs are deleted.\n"));
69d05d38 227
d02ed0bb 228 observer_notify_record_changed (current_inferior (), 0);
69d05d38
HZ
229}
230
d02ed0bb 231/* The "set record" command. */
69d05d38
HZ
232
233static void
234set_record_command (char *args, int from_tty)
235{
3e43a32a
MS
236 printf_unfiltered (_("\"set record\" must be followed "
237 "by an apporpriate subcommand.\n"));
69d05d38
HZ
238 help_list (set_record_cmdlist, "set record ", all_commands, gdb_stdout);
239}
240
d02ed0bb
MM
241/* The "show record" command. */
242
69d05d38
HZ
243static void
244show_record_command (char *args, int from_tty)
245{
246 cmd_show_list (show_record_cmdlist, from_tty, "");
247}
248
d02ed0bb 249/* The "info record" command. */
b54295a7 250
69d05d38
HZ
251static void
252info_record_command (char *args, int from_tty)
253{
d02ed0bb 254 struct target_ops *t;
0156b218 255
d02ed0bb
MM
256 t = find_record_target ();
257 if (t == NULL)
0156b218 258 {
d02ed0bb
MM
259 printf_filtered (_("No record target is currently active.\n"));
260 return;
0156b218
MS
261 }
262
d02ed0bb
MM
263 printf_filtered (_("Active record target: %s\n"), t->to_shortname);
264 if (t->to_info_record != NULL)
265 t->to_info_record ();
0156b218
MS
266}
267
d02ed0bb 268/* The "record save" command. */
0156b218
MS
269
270static void
271cmd_record_save (char *args, int from_tty)
272{
273 char *recfilename, recfilename_buffer[40];
0156b218 274
d02ed0bb 275 require_record_target ();
0156b218 276
d02ed0bb 277 if (args != NULL && *args != 0)
0156b218
MS
278 recfilename = args;
279 else
280 {
281 /* Default recfile name is "gdb_record.PID". */
d02ed0bb 282 xsnprintf (recfilename_buffer, sizeof (recfilename_buffer),
0156b218
MS
283 "gdb_record.%d", PIDGET (inferior_ptid));
284 recfilename = recfilename_buffer;
285 }
286
d02ed0bb 287 target_save_record (recfilename);
6b04bdb7
MS
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
d02ed0bb 295void
6b04bdb7
MS
296cmd_record_goto (char *arg, int from_tty)
297{
d02ed0bb 298 require_record_target ();
6b04bdb7
MS
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)
d02ed0bb 305 target_goto_record_begin ();
6b04bdb7 306 else if (strncmp (arg, "end", strlen ("end")) == 0)
d02ed0bb 307 target_goto_record_end ();
6b04bdb7
MS
308 else
309 {
d02ed0bb 310 ULONGEST insn;
6b04bdb7 311
d02ed0bb
MM
312 insn = parse_and_eval_long (arg);
313 target_goto_record (insn);
6b04bdb7 314 }
6b04bdb7
MS
315}
316
70221824
PA
317/* Provide a prototype to silence -Wmissing-prototypes. */
318extern initialize_file_ftype _initialize_record;
319
69d05d38
HZ
320void
321_initialize_record (void)
322{
0156b218
MS
323 struct cmd_list_element *c;
324
ccce17b0
YQ
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);
69d05d38 332
0156b218 333 c = add_prefix_cmd ("record", class_obscure, cmd_record_start,
d02ed0bb 334 _("Start recording."),
0156b218
MS
335 &record_cmdlist, "record ", 0, &cmdlist);
336 set_cmd_completer (c, filename_completer);
337
69d05d38
HZ
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
0156b218
MS
352 c = add_cmd ("save", class_obscure, cmd_record_save,
353 _("Save the execution log to a file.\n\
354Argument is optional filename.\n\
355Default filename is 'gdb_record.<process_id>'."),
356 &record_cmdlist);
357 set_cmd_completer (c, filename_completer);
358
69d05d38
HZ
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
6b04bdb7
MS
370 add_cmd ("goto", class_obscure, cmd_record_goto, _("\
371Restore the program to its state at instruction number N.\n\
372Argument is instruction number, as shown by 'info record'."),
373 &record_cmdlist);
69d05d38 374}