Add a command to provide a disassembly of the execution trace log.
[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 #include "cli/cli-utils.h"
28 #include "disasm.h"
29
30 #include <ctype.h>
31
32 /* This is the debug switch for process record. */
33 unsigned int record_debug = 0;
34
35 /* The number of instructions to print in "record instruction-history". */
36 static unsigned int record_insn_history_size = 10;
37
38 struct cmd_list_element *record_cmdlist = NULL;
39 struct cmd_list_element *set_record_cmdlist = NULL;
40 struct cmd_list_element *show_record_cmdlist = NULL;
41 struct cmd_list_element *info_record_cmdlist = NULL;
42
43 #define DEBUG(msg, args...) \
44 if (record_debug) \
45 fprintf_unfiltered (gdb_stdlog, "record: " msg "\n", ##args)
46
47 /* Find the record target in the target stack. */
48
49 static struct target_ops *
50 find_record_target (void)
51 {
52 struct target_ops *t;
53
54 for (t = current_target.beneath; t != NULL; t = t->beneath)
55 if (t->to_stratum == record_stratum)
56 return t;
57
58 return NULL;
59 }
60
61 /* Check that recording is active. Throw an error, if it isn't. */
62
63 static struct target_ops *
64 require_record_target (void)
65 {
66 struct target_ops *t;
67
68 t = find_record_target ();
69 if (t == NULL)
70 error (_("No record target is currently active.\n"
71 "Use one of the \"target record-<tab><tab>\" commands first."));
72
73 return t;
74 }
75
76 /* See record.h. */
77
78 int
79 record_read_memory (struct gdbarch *gdbarch,
80 CORE_ADDR memaddr, gdb_byte *myaddr,
81 ssize_t len)
82 {
83 int ret = target_read_memory (memaddr, myaddr, len);
84
85 if (ret != 0)
86 DEBUG ("error reading memory at addr %s len = %ld.\n",
87 paddress (gdbarch, memaddr), (long) len);
88
89 return ret;
90 }
91
92 /* Stop recording. */
93
94 static void
95 record_stop (struct target_ops *t)
96 {
97 DEBUG ("stop %s", t->to_shortname);
98
99 if (t->to_stop_recording != NULL)
100 t->to_stop_recording ();
101 }
102
103 /* Unpush the record target. */
104
105 static void
106 record_unpush (struct target_ops *t)
107 {
108 DEBUG ("unpush %s", t->to_shortname);
109
110 unpush_target (t);
111 }
112
113 /* See record.h. */
114
115 void
116 record_disconnect (struct target_ops *t, char *args, int from_tty)
117 {
118 gdb_assert (t->to_stratum == record_stratum);
119
120 DEBUG ("disconnect %s", t->to_shortname);
121
122 record_stop (t);
123 record_unpush (t);
124
125 target_disconnect (args, from_tty);
126 }
127
128 /* See record.h. */
129
130 void
131 record_detach (struct target_ops *t, char *args, int from_tty)
132 {
133 gdb_assert (t->to_stratum == record_stratum);
134
135 DEBUG ("detach %s", t->to_shortname);
136
137 record_stop (t);
138 record_unpush (t);
139
140 target_detach (args, from_tty);
141 }
142
143 /* See record.h. */
144
145 void
146 record_mourn_inferior (struct target_ops *t)
147 {
148 gdb_assert (t->to_stratum == record_stratum);
149
150 DEBUG ("mourn inferior %s", t->to_shortname);
151
152 /* It is safer to not stop recording. Resources will be freed when
153 threads are discarded. */
154 record_unpush (t);
155
156 target_mourn_inferior ();
157 }
158
159 /* See record.h. */
160
161 void
162 record_kill (struct target_ops *t)
163 {
164 gdb_assert (t->to_stratum == record_stratum);
165
166 DEBUG ("kill %s", t->to_shortname);
167
168 /* It is safer to not stop recording. Resources will be freed when
169 threads are discarded. */
170 record_unpush (t);
171
172 target_kill ();
173 }
174
175 /* Implement "show record debug" command. */
176
177 static void
178 show_record_debug (struct ui_file *file, int from_tty,
179 struct cmd_list_element *c, const char *value)
180 {
181 fprintf_filtered (file, _("Debugging of process record target is %s.\n"),
182 value);
183 }
184
185 /* Alias for "target record". */
186
187 static void
188 cmd_record_start (char *args, int from_tty)
189 {
190 execute_command ("target record-full", from_tty);
191 }
192
193 /* Truncate the record log from the present point
194 of replay until the end. */
195
196 static void
197 cmd_record_delete (char *args, int from_tty)
198 {
199 require_record_target ();
200
201 if (!target_record_is_replaying ())
202 {
203 printf_unfiltered (_("Already at end of record list.\n"));
204 return;
205 }
206
207 if (!target_supports_delete_record ())
208 {
209 printf_unfiltered (_("The current record target does not support "
210 "this operation.\n"));
211 return;
212 }
213
214 if (!from_tty || query (_("Delete the log from this point forward "
215 "and begin to record the running message "
216 "at current PC?")))
217 target_delete_record ();
218 }
219
220 /* Implement the "stoprecord" or "record stop" command. */
221
222 static void
223 cmd_record_stop (char *args, int from_tty)
224 {
225 struct target_ops *t;
226
227 t = require_record_target ();
228
229 record_stop (t);
230 record_unpush (t);
231
232 printf_unfiltered (_("Process record is stopped and all execution "
233 "logs are deleted.\n"));
234
235 observer_notify_record_changed (current_inferior (), 0);
236 }
237
238 /* The "set record" command. */
239
240 static void
241 set_record_command (char *args, int from_tty)
242 {
243 printf_unfiltered (_("\"set record\" must be followed "
244 "by an apporpriate subcommand.\n"));
245 help_list (set_record_cmdlist, "set record ", all_commands, gdb_stdout);
246 }
247
248 /* The "show record" command. */
249
250 static void
251 show_record_command (char *args, int from_tty)
252 {
253 cmd_show_list (show_record_cmdlist, from_tty, "");
254 }
255
256 /* The "info record" command. */
257
258 static void
259 info_record_command (char *args, int from_tty)
260 {
261 struct target_ops *t;
262
263 t = find_record_target ();
264 if (t == NULL)
265 {
266 printf_filtered (_("No record target is currently active.\n"));
267 return;
268 }
269
270 printf_filtered (_("Active record target: %s\n"), t->to_shortname);
271 if (t->to_info_record != NULL)
272 t->to_info_record ();
273 }
274
275 /* The "record save" command. */
276
277 static void
278 cmd_record_save (char *args, int from_tty)
279 {
280 char *recfilename, recfilename_buffer[40];
281
282 require_record_target ();
283
284 if (args != NULL && *args != 0)
285 recfilename = args;
286 else
287 {
288 /* Default recfile name is "gdb_record.PID". */
289 xsnprintf (recfilename_buffer, sizeof (recfilename_buffer),
290 "gdb_record.%d", PIDGET (inferior_ptid));
291 recfilename = recfilename_buffer;
292 }
293
294 target_save_record (recfilename);
295 }
296
297 /* "record goto" command. Argument is an instruction number,
298 as given by "info record".
299
300 Rewinds the recording (forward or backward) to the given instruction. */
301
302 void
303 cmd_record_goto (char *arg, int from_tty)
304 {
305 require_record_target ();
306
307 if (arg == NULL || *arg == '\0')
308 error (_("Command requires an argument (insn number to go to)."));
309
310 if (strncmp (arg, "start", strlen ("start")) == 0
311 || strncmp (arg, "begin", strlen ("begin")) == 0)
312 target_goto_record_begin ();
313 else if (strncmp (arg, "end", strlen ("end")) == 0)
314 target_goto_record_end ();
315 else
316 {
317 ULONGEST insn;
318
319 insn = parse_and_eval_long (arg);
320 target_goto_record (insn);
321 }
322 }
323
324 /* Read an instruction number from an argument string. */
325
326 static ULONGEST
327 get_insn_number (char **arg)
328 {
329 ULONGEST number;
330 const char *begin, *end, *pos;
331
332 begin = *arg;
333 pos = skip_spaces_const (begin);
334
335 if (!isdigit (*pos))
336 error (_("Expected positive number, got: %s."), pos);
337
338 number = strtoulst (pos, &end, 10);
339
340 *arg += (end - begin);
341
342 return number;
343 }
344
345 /* Read a context size from an argument string. */
346
347 static int
348 get_context_size (char **arg)
349 {
350 char *pos;
351 int number;
352
353 pos = skip_spaces (*arg);
354
355 if (!isdigit (*pos))
356 error (_("Expected positive number, got: %s."), pos);
357
358 return strtol (pos, arg, 10);
359 }
360
361 /* Complain about junk at the end of an argument string. */
362
363 static void
364 no_chunk (char *arg)
365 {
366 if (*arg != 0)
367 error (_("Junk after argument: %s."), arg);
368 }
369
370 /* Read instruction-history modifiers from an argument string. */
371
372 static int
373 get_insn_history_modifiers (char **arg)
374 {
375 int modifiers;
376 char *args;
377
378 modifiers = 0;
379 args = *arg;
380
381 if (args == NULL)
382 return modifiers;
383
384 while (*args == '/')
385 {
386 ++args;
387
388 if (*args == '\0')
389 error (_("Missing modifier."));
390
391 for (; *args; ++args)
392 {
393 if (isspace (*args))
394 break;
395
396 if (*args == '/')
397 continue;
398
399 switch (*args)
400 {
401 case 'm':
402 modifiers |= DISASSEMBLY_SOURCE;
403 modifiers |= DISASSEMBLY_FILENAME;
404 break;
405 case 'r':
406 modifiers |= DISASSEMBLY_RAW_INSN;
407 break;
408 case 'f':
409 modifiers |= DISASSEMBLY_OMIT_FNAME;
410 break;
411 default:
412 error (_("Invalid modifier: %c."), *args);
413 }
414 }
415
416 args = skip_spaces (args);
417 }
418
419 /* Update the argument string. */
420 *arg = args;
421
422 return modifiers;
423 }
424
425 /* The "record instruction-history" command. */
426
427 static void
428 cmd_record_insn_history (char *arg, int from_tty)
429 {
430 int flags, size;
431
432 require_record_target ();
433
434 flags = get_insn_history_modifiers (&arg);
435
436 /* We use a signed size to also indicate the direction. Make sure that
437 unlimited remains unlimited. */
438 size = (int) record_insn_history_size;
439 if (size < 0)
440 size = INT_MAX;
441
442 if (arg == NULL || *arg == 0 || strcmp (arg, "+") == 0)
443 target_insn_history (size, flags);
444 else if (strcmp (arg, "-") == 0)
445 target_insn_history (-size, flags);
446 else
447 {
448 ULONGEST begin, end;
449
450 begin = get_insn_number (&arg);
451
452 if (*arg == ',')
453 {
454 arg = skip_spaces (++arg);
455
456 if (*arg == '+')
457 {
458 arg += 1;
459 size = get_context_size (&arg);
460
461 no_chunk (arg);
462
463 target_insn_history_from (begin, size, flags);
464 }
465 else if (*arg == '-')
466 {
467 arg += 1;
468 size = get_context_size (&arg);
469
470 no_chunk (arg);
471
472 target_insn_history_from (begin, -size, flags);
473 }
474 else
475 {
476 end = get_insn_number (&arg);
477
478 no_chunk (arg);
479
480 target_insn_history_range (begin, end, flags);
481 }
482 }
483 else
484 {
485 no_chunk (arg);
486
487 target_insn_history_from (begin, size, flags);
488 }
489
490 dont_repeat ();
491 }
492 }
493
494 /* Provide a prototype to silence -Wmissing-prototypes. */
495 extern initialize_file_ftype _initialize_record;
496
497 void
498 _initialize_record (void)
499 {
500 struct cmd_list_element *c;
501
502 add_setshow_zuinteger_cmd ("record", no_class, &record_debug,
503 _("Set debugging of record/replay feature."),
504 _("Show debugging of record/replay feature."),
505 _("When enabled, debugging output for "
506 "record/replay feature is displayed."),
507 NULL, show_record_debug, &setdebuglist,
508 &showdebuglist);
509
510 add_setshow_uinteger_cmd ("instruction-history-size", no_class,
511 &record_insn_history_size, _("\
512 Set number of instructions to print in \"record instruction-history\"."), _("\
513 Show number of instructions to print in \"record instruction-history\"."),
514 NULL, NULL, NULL, &set_record_cmdlist,
515 &show_record_cmdlist);
516
517 c = add_prefix_cmd ("record", class_obscure, cmd_record_start,
518 _("Start recording."),
519 &record_cmdlist, "record ", 0, &cmdlist);
520 set_cmd_completer (c, filename_completer);
521
522 add_com_alias ("rec", "record", class_obscure, 1);
523 add_prefix_cmd ("record", class_support, set_record_command,
524 _("Set record options"), &set_record_cmdlist,
525 "set record ", 0, &setlist);
526 add_alias_cmd ("rec", "record", class_obscure, 1, &setlist);
527 add_prefix_cmd ("record", class_support, show_record_command,
528 _("Show record options"), &show_record_cmdlist,
529 "show record ", 0, &showlist);
530 add_alias_cmd ("rec", "record", class_obscure, 1, &showlist);
531 add_prefix_cmd ("record", class_support, info_record_command,
532 _("Info record options"), &info_record_cmdlist,
533 "info record ", 0, &infolist);
534 add_alias_cmd ("rec", "record", class_obscure, 1, &infolist);
535
536 c = add_cmd ("save", class_obscure, cmd_record_save,
537 _("Save the execution log to a file.\n\
538 Argument is optional filename.\n\
539 Default filename is 'gdb_record.<process_id>'."),
540 &record_cmdlist);
541 set_cmd_completer (c, filename_completer);
542
543 add_cmd ("delete", class_obscure, cmd_record_delete,
544 _("Delete the rest of execution log and start recording it anew."),
545 &record_cmdlist);
546 add_alias_cmd ("d", "delete", class_obscure, 1, &record_cmdlist);
547 add_alias_cmd ("del", "delete", class_obscure, 1, &record_cmdlist);
548
549 add_cmd ("stop", class_obscure, cmd_record_stop,
550 _("Stop the record/replay target."),
551 &record_cmdlist);
552 add_alias_cmd ("s", "stop", class_obscure, 1, &record_cmdlist);
553
554 add_cmd ("goto", class_obscure, cmd_record_goto, _("\
555 Restore the program to its state at instruction number N.\n\
556 Argument is instruction number, as shown by 'info record'."),
557 &record_cmdlist);
558
559 add_cmd ("instruction-history", class_obscure, cmd_record_insn_history, _("\
560 Print disassembled instructions stored in the execution log.\n\
561 With a /m modifier, source lines are included (if available).\n\
562 With a /r modifier, raw instructions in hex are included.\n\
563 With a /f modifier, function names are omitted.\n\
564 With no argument, disassembles ten more instructions after the previous \
565 disassembly.\n\
566 \"record instruction-history -\" disassembles ten instructions before a \
567 previous disassembly.\n\
568 One argument specifies an instruction number as shown by 'info record', and \
569 ten instructions are disassembled after that instruction.\n\
570 Two arguments with comma between them specify starting and ending instruction \
571 numbers to disassemble.\n\
572 If the second argument is preceded by '+' or '-', it specifies the distance \
573 from the first argument.\n\
574 The number of instructions to disassemble can be defined with \"set record \
575 instruction-history-size\"."),
576 &record_cmdlist);
577 }
This page took 0.049026 seconds and 5 git commands to generate.