Accept "set foo unlimited" in integer/uinteger/zuinteger_unlimited commands.
[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 /* The variable registered as control variable in the "record
39 instruction-history" command. Necessary for extra input
40 validation. */
41 static unsigned int record_insn_history_size_setshow_var;
42
43 /* The number of functions to print in "record function-call-history". */
44 static unsigned int record_call_history_size = 10;
45
46 /* The variable registered as control variable in the "record
47 call-history" command. Necessary for extra input validation. */
48 static unsigned int record_call_history_size_setshow_var;
49
50 struct cmd_list_element *record_cmdlist = NULL;
51 struct cmd_list_element *set_record_cmdlist = NULL;
52 struct cmd_list_element *show_record_cmdlist = NULL;
53 struct cmd_list_element *info_record_cmdlist = NULL;
54
55 #define DEBUG(msg, args...) \
56 if (record_debug) \
57 fprintf_unfiltered (gdb_stdlog, "record: " msg "\n", ##args)
58
59 /* Find the record target in the target stack. */
60
61 static struct target_ops *
62 find_record_target (void)
63 {
64 struct target_ops *t;
65
66 for (t = current_target.beneath; t != NULL; t = t->beneath)
67 if (t->to_stratum == record_stratum)
68 return t;
69
70 return NULL;
71 }
72
73 /* Check that recording is active. Throw an error, if it isn't. */
74
75 static struct target_ops *
76 require_record_target (void)
77 {
78 struct target_ops *t;
79
80 t = find_record_target ();
81 if (t == NULL)
82 error (_("No record target is currently active.\n"
83 "Use one of the \"target record-<tab><tab>\" commands first."));
84
85 return t;
86 }
87
88 /* See record.h. */
89
90 int
91 record_read_memory (struct gdbarch *gdbarch,
92 CORE_ADDR memaddr, gdb_byte *myaddr,
93 ssize_t len)
94 {
95 int ret = target_read_memory (memaddr, myaddr, len);
96
97 if (ret != 0)
98 DEBUG ("error reading memory at addr %s len = %ld.\n",
99 paddress (gdbarch, memaddr), (long) len);
100
101 return ret;
102 }
103
104 /* Stop recording. */
105
106 static void
107 record_stop (struct target_ops *t)
108 {
109 DEBUG ("stop %s", t->to_shortname);
110
111 if (t->to_stop_recording != NULL)
112 t->to_stop_recording ();
113 }
114
115 /* Unpush the record target. */
116
117 static void
118 record_unpush (struct target_ops *t)
119 {
120 DEBUG ("unpush %s", t->to_shortname);
121
122 unpush_target (t);
123 }
124
125 /* See record.h. */
126
127 void
128 record_disconnect (struct target_ops *t, char *args, int from_tty)
129 {
130 gdb_assert (t->to_stratum == record_stratum);
131
132 DEBUG ("disconnect %s", t->to_shortname);
133
134 record_stop (t);
135 record_unpush (t);
136
137 target_disconnect (args, from_tty);
138 }
139
140 /* See record.h. */
141
142 void
143 record_detach (struct target_ops *t, char *args, int from_tty)
144 {
145 gdb_assert (t->to_stratum == record_stratum);
146
147 DEBUG ("detach %s", t->to_shortname);
148
149 record_stop (t);
150 record_unpush (t);
151
152 target_detach (args, from_tty);
153 }
154
155 /* See record.h. */
156
157 void
158 record_mourn_inferior (struct target_ops *t)
159 {
160 gdb_assert (t->to_stratum == record_stratum);
161
162 DEBUG ("mourn inferior %s", t->to_shortname);
163
164 /* It is safer to not stop recording. Resources will be freed when
165 threads are discarded. */
166 record_unpush (t);
167
168 target_mourn_inferior ();
169 }
170
171 /* See record.h. */
172
173 void
174 record_kill (struct target_ops *t)
175 {
176 gdb_assert (t->to_stratum == record_stratum);
177
178 DEBUG ("kill %s", t->to_shortname);
179
180 /* It is safer to not stop recording. Resources will be freed when
181 threads are discarded. */
182 record_unpush (t);
183
184 target_kill ();
185 }
186
187 /* Implement "show record debug" command. */
188
189 static void
190 show_record_debug (struct ui_file *file, int from_tty,
191 struct cmd_list_element *c, const char *value)
192 {
193 fprintf_filtered (file, _("Debugging of process record target is %s.\n"),
194 value);
195 }
196
197 /* Alias for "target record". */
198
199 static void
200 cmd_record_start (char *args, int from_tty)
201 {
202 execute_command ("target record-full", from_tty);
203 }
204
205 /* Truncate the record log from the present point
206 of replay until the end. */
207
208 static void
209 cmd_record_delete (char *args, int from_tty)
210 {
211 require_record_target ();
212
213 if (!target_record_is_replaying ())
214 {
215 printf_unfiltered (_("Already at end of record list.\n"));
216 return;
217 }
218
219 if (!target_supports_delete_record ())
220 {
221 printf_unfiltered (_("The current record target does not support "
222 "this operation.\n"));
223 return;
224 }
225
226 if (!from_tty || query (_("Delete the log from this point forward "
227 "and begin to record the running message "
228 "at current PC?")))
229 target_delete_record ();
230 }
231
232 /* Implement the "stoprecord" or "record stop" command. */
233
234 static void
235 cmd_record_stop (char *args, int from_tty)
236 {
237 struct target_ops *t;
238
239 t = require_record_target ();
240
241 record_stop (t);
242 record_unpush (t);
243
244 printf_unfiltered (_("Process record is stopped and all execution "
245 "logs are deleted.\n"));
246
247 observer_notify_record_changed (current_inferior (), 0);
248 }
249
250 /* The "set record" command. */
251
252 static void
253 set_record_command (char *args, int from_tty)
254 {
255 printf_unfiltered (_("\"set record\" must be followed "
256 "by an apporpriate subcommand.\n"));
257 help_list (set_record_cmdlist, "set record ", all_commands, gdb_stdout);
258 }
259
260 /* The "show record" command. */
261
262 static void
263 show_record_command (char *args, int from_tty)
264 {
265 cmd_show_list (show_record_cmdlist, from_tty, "");
266 }
267
268 /* The "info record" command. */
269
270 static void
271 info_record_command (char *args, int from_tty)
272 {
273 struct target_ops *t;
274
275 t = find_record_target ();
276 if (t == NULL)
277 {
278 printf_filtered (_("No record target is currently active.\n"));
279 return;
280 }
281
282 printf_filtered (_("Active record target: %s\n"), t->to_shortname);
283 if (t->to_info_record != NULL)
284 t->to_info_record ();
285 }
286
287 /* The "record save" command. */
288
289 static void
290 cmd_record_save (char *args, int from_tty)
291 {
292 char *recfilename, recfilename_buffer[40];
293
294 require_record_target ();
295
296 if (args != NULL && *args != 0)
297 recfilename = args;
298 else
299 {
300 /* Default recfile name is "gdb_record.PID". */
301 xsnprintf (recfilename_buffer, sizeof (recfilename_buffer),
302 "gdb_record.%d", PIDGET (inferior_ptid));
303 recfilename = recfilename_buffer;
304 }
305
306 target_save_record (recfilename);
307 }
308
309 /* "record goto" command. Argument is an instruction number,
310 as given by "info record".
311
312 Rewinds the recording (forward or backward) to the given instruction. */
313
314 void
315 cmd_record_goto (char *arg, int from_tty)
316 {
317 require_record_target ();
318
319 if (arg == NULL || *arg == '\0')
320 error (_("Command requires an argument (insn number to go to)."));
321
322 if (strncmp (arg, "start", strlen ("start")) == 0
323 || strncmp (arg, "begin", strlen ("begin")) == 0)
324 target_goto_record_begin ();
325 else if (strncmp (arg, "end", strlen ("end")) == 0)
326 target_goto_record_end ();
327 else
328 {
329 ULONGEST insn;
330
331 insn = parse_and_eval_long (arg);
332 target_goto_record (insn);
333 }
334 }
335
336 /* Read an instruction number from an argument string. */
337
338 static ULONGEST
339 get_insn_number (char **arg)
340 {
341 ULONGEST number;
342 const char *begin, *end, *pos;
343
344 begin = *arg;
345 pos = skip_spaces_const (begin);
346
347 if (!isdigit (*pos))
348 error (_("Expected positive number, got: %s."), pos);
349
350 number = strtoulst (pos, &end, 10);
351
352 *arg += (end - begin);
353
354 return number;
355 }
356
357 /* Read a context size from an argument string. */
358
359 static int
360 get_context_size (char **arg)
361 {
362 char *pos;
363 int number;
364
365 pos = skip_spaces (*arg);
366
367 if (!isdigit (*pos))
368 error (_("Expected positive number, got: %s."), pos);
369
370 return strtol (pos, arg, 10);
371 }
372
373 /* Complain about junk at the end of an argument string. */
374
375 static void
376 no_chunk (char *arg)
377 {
378 if (*arg != 0)
379 error (_("Junk after argument: %s."), arg);
380 }
381
382 /* Read instruction-history modifiers from an argument string. */
383
384 static int
385 get_insn_history_modifiers (char **arg)
386 {
387 int modifiers;
388 char *args;
389
390 modifiers = 0;
391 args = *arg;
392
393 if (args == NULL)
394 return modifiers;
395
396 while (*args == '/')
397 {
398 ++args;
399
400 if (*args == '\0')
401 error (_("Missing modifier."));
402
403 for (; *args; ++args)
404 {
405 if (isspace (*args))
406 break;
407
408 if (*args == '/')
409 continue;
410
411 switch (*args)
412 {
413 case 'm':
414 modifiers |= DISASSEMBLY_SOURCE;
415 modifiers |= DISASSEMBLY_FILENAME;
416 break;
417 case 'r':
418 modifiers |= DISASSEMBLY_RAW_INSN;
419 break;
420 case 'f':
421 modifiers |= DISASSEMBLY_OMIT_FNAME;
422 break;
423 case 'p':
424 modifiers |= DISASSEMBLY_OMIT_PC;
425 break;
426 default:
427 error (_("Invalid modifier: %c."), *args);
428 }
429 }
430
431 args = skip_spaces (args);
432 }
433
434 /* Update the argument string. */
435 *arg = args;
436
437 return modifiers;
438 }
439
440 /* The "set record instruction-history-size / set record
441 function-call-history-size" commands are unsigned, with UINT_MAX
442 meaning unlimited. The target interfaces works with signed int
443 though, to indicate direction, so map "unlimited" to INT_MAX, which
444 is about the same as unlimited in practice. If the user does have
445 a log that huge, she can fetch it in chunks across several requests,
446 but she'll likely have other problems first... */
447
448 static int
449 command_size_to_target_size (unsigned int size)
450 {
451 gdb_assert (size <= INT_MAX || size == UINT_MAX);
452
453 if (size == UINT_MAX)
454 return INT_MAX;
455 else
456 return size;
457 }
458
459 /* The "record instruction-history" command. */
460
461 static void
462 cmd_record_insn_history (char *arg, int from_tty)
463 {
464 int flags, size;
465
466 require_record_target ();
467
468 flags = get_insn_history_modifiers (&arg);
469
470 size = command_size_to_target_size (record_insn_history_size);
471
472 if (arg == NULL || *arg == 0 || strcmp (arg, "+") == 0)
473 target_insn_history (size, flags);
474 else if (strcmp (arg, "-") == 0)
475 target_insn_history (-size, flags);
476 else
477 {
478 ULONGEST begin, end;
479
480 begin = get_insn_number (&arg);
481
482 if (*arg == ',')
483 {
484 arg = skip_spaces (++arg);
485
486 if (*arg == '+')
487 {
488 arg += 1;
489 size = get_context_size (&arg);
490
491 no_chunk (arg);
492
493 target_insn_history_from (begin, size, flags);
494 }
495 else if (*arg == '-')
496 {
497 arg += 1;
498 size = get_context_size (&arg);
499
500 no_chunk (arg);
501
502 target_insn_history_from (begin, -size, flags);
503 }
504 else
505 {
506 end = get_insn_number (&arg);
507
508 no_chunk (arg);
509
510 target_insn_history_range (begin, end, flags);
511 }
512 }
513 else
514 {
515 no_chunk (arg);
516
517 target_insn_history_from (begin, size, flags);
518 }
519
520 dont_repeat ();
521 }
522 }
523
524 /* Read function-call-history modifiers from an argument string. */
525
526 static int
527 get_call_history_modifiers (char **arg)
528 {
529 int modifiers;
530 char *args;
531
532 modifiers = 0;
533 args = *arg;
534
535 if (args == NULL)
536 return modifiers;
537
538 while (*args == '/')
539 {
540 ++args;
541
542 if (*args == '\0')
543 error (_("Missing modifier."));
544
545 for (; *args; ++args)
546 {
547 if (isspace (*args))
548 break;
549
550 if (*args == '/')
551 continue;
552
553 switch (*args)
554 {
555 case 'l':
556 modifiers |= record_print_src_line;
557 break;
558 case 'i':
559 modifiers |= record_print_insn_range;
560 break;
561 default:
562 error (_("Invalid modifier: %c."), *args);
563 }
564 }
565
566 args = skip_spaces (args);
567 }
568
569 /* Update the argument string. */
570 *arg = args;
571
572 return modifiers;
573 }
574
575 /* The "record function-call-history" command. */
576
577 static void
578 cmd_record_call_history (char *arg, int from_tty)
579 {
580 int flags, size;
581
582 require_record_target ();
583
584 flags = get_call_history_modifiers (&arg);
585
586 size = command_size_to_target_size (record_call_history_size);
587
588 if (arg == NULL || *arg == 0 || strcmp (arg, "+") == 0)
589 target_call_history (size, flags);
590 else if (strcmp (arg, "-") == 0)
591 target_call_history (-size, flags);
592 else
593 {
594 ULONGEST begin, end;
595
596 begin = get_insn_number (&arg);
597
598 if (*arg == ',')
599 {
600 arg = skip_spaces (++arg);
601
602 if (*arg == '+')
603 {
604 arg += 1;
605 size = get_context_size (&arg);
606
607 no_chunk (arg);
608
609 target_call_history_from (begin, size, flags);
610 }
611 else if (*arg == '-')
612 {
613 arg += 1;
614 size = get_context_size (&arg);
615
616 no_chunk (arg);
617
618 target_call_history_from (begin, -size, flags);
619 }
620 else
621 {
622 end = get_insn_number (&arg);
623
624 no_chunk (arg);
625
626 target_call_history_range (begin, end, flags);
627 }
628 }
629 else
630 {
631 no_chunk (arg);
632
633 target_call_history_from (begin, size, flags);
634 }
635
636 dont_repeat ();
637 }
638 }
639
640 /* Helper for "set record instruction-history-size" and "set record
641 function-call-history-size" input validation. COMMAND_VAR is the
642 variable registered in the command as control variable. *SETTING
643 is the real setting the command allows changing. */
644
645 static void
646 validate_history_size (unsigned int *command_var, int *setting)
647 {
648 if (*command_var != UINT_MAX && *command_var > INT_MAX)
649 {
650 unsigned int new_value = *command_var;
651
652 /* Restore previous value. */
653 *command_var = *setting;
654 error (_("integer %u out of range"), new_value);
655 }
656
657 /* Commit new value. */
658 *setting = *command_var;
659 }
660
661 /* Called by do_setshow_command. We only want values in the
662 [0..INT_MAX] range, while the command's machinery accepts
663 [0..UINT_MAX]. See command_size_to_target_size. */
664
665 static void
666 set_record_insn_history_size (char *args, int from_tty,
667 struct cmd_list_element *c)
668 {
669 validate_history_size (&record_insn_history_size_setshow_var,
670 &record_insn_history_size);
671 }
672
673 /* Called by do_setshow_command. We only want values in the
674 [0..INT_MAX] range, while the command's machinery accepts
675 [0..UINT_MAX]. See command_size_to_target_size. */
676
677 static void
678 set_record_call_history_size (char *args, int from_tty,
679 struct cmd_list_element *c)
680 {
681 validate_history_size (&record_call_history_size_setshow_var,
682 &record_call_history_size);
683 }
684
685 /* Provide a prototype to silence -Wmissing-prototypes. */
686 extern initialize_file_ftype _initialize_record;
687
688 void
689 _initialize_record (void)
690 {
691 struct cmd_list_element *c;
692
693 add_setshow_zuinteger_cmd ("record", no_class, &record_debug,
694 _("Set debugging of record/replay feature."),
695 _("Show debugging of record/replay feature."),
696 _("When enabled, debugging output for "
697 "record/replay feature is displayed."),
698 NULL, show_record_debug, &setdebuglist,
699 &showdebuglist);
700
701 add_setshow_uinteger_cmd ("instruction-history-size", no_class,
702 &record_insn_history_size_setshow_var, _("\
703 Set number of instructions to print in \"record instruction-history\"."), _("\
704 Show number of instructions to print in \"record instruction-history\"."), _("\
705 A size of \"unlimited\" means unlimited instructions. The default is 10."),
706 set_record_insn_history_size, NULL,
707 &set_record_cmdlist, &show_record_cmdlist);
708
709 add_setshow_uinteger_cmd ("function-call-history-size", no_class,
710 &record_call_history_size_setshow_var, _("\
711 Set number of function to print in \"record function-call-history\"."), _("\
712 Show number of functions to print in \"record function-call-history\"."), _("\
713 A size of \"unlimited\" means unlimited lines. The default is 10."),
714 set_record_call_history_size, NULL,
715 &set_record_cmdlist, &show_record_cmdlist);
716
717 c = add_prefix_cmd ("record", class_obscure, cmd_record_start,
718 _("Start recording."),
719 &record_cmdlist, "record ", 0, &cmdlist);
720 set_cmd_completer (c, filename_completer);
721
722 add_com_alias ("rec", "record", class_obscure, 1);
723 add_prefix_cmd ("record", class_support, set_record_command,
724 _("Set record options"), &set_record_cmdlist,
725 "set record ", 0, &setlist);
726 add_alias_cmd ("rec", "record", class_obscure, 1, &setlist);
727 add_prefix_cmd ("record", class_support, show_record_command,
728 _("Show record options"), &show_record_cmdlist,
729 "show record ", 0, &showlist);
730 add_alias_cmd ("rec", "record", class_obscure, 1, &showlist);
731 add_prefix_cmd ("record", class_support, info_record_command,
732 _("Info record options"), &info_record_cmdlist,
733 "info record ", 0, &infolist);
734 add_alias_cmd ("rec", "record", class_obscure, 1, &infolist);
735
736 c = add_cmd ("save", class_obscure, cmd_record_save,
737 _("Save the execution log to a file.\n\
738 Argument is optional filename.\n\
739 Default filename is 'gdb_record.<process_id>'."),
740 &record_cmdlist);
741 set_cmd_completer (c, filename_completer);
742
743 add_cmd ("delete", class_obscure, cmd_record_delete,
744 _("Delete the rest of execution log and start recording it anew."),
745 &record_cmdlist);
746 add_alias_cmd ("d", "delete", class_obscure, 1, &record_cmdlist);
747 add_alias_cmd ("del", "delete", class_obscure, 1, &record_cmdlist);
748
749 add_cmd ("stop", class_obscure, cmd_record_stop,
750 _("Stop the record/replay target."),
751 &record_cmdlist);
752 add_alias_cmd ("s", "stop", class_obscure, 1, &record_cmdlist);
753
754 add_cmd ("goto", class_obscure, cmd_record_goto, _("\
755 Restore the program to its state at instruction number N.\n\
756 Argument is instruction number, as shown by 'info record'."),
757 &record_cmdlist);
758
759 add_cmd ("instruction-history", class_obscure, cmd_record_insn_history, _("\
760 Print disassembled instructions stored in the execution log.\n\
761 With a /m modifier, source lines are included (if available).\n\
762 With a /r modifier, raw instructions in hex are included.\n\
763 With a /f modifier, function names are omitted.\n\
764 With a /p modifier, current position markers are omitted.\n\
765 With no argument, disassembles ten more instructions after the previous \
766 disassembly.\n\
767 \"record instruction-history -\" disassembles ten instructions before a \
768 previous disassembly.\n\
769 One argument specifies an instruction number as shown by 'info record', and \
770 ten instructions are disassembled after that instruction.\n\
771 Two arguments with comma between them specify starting and ending instruction \
772 numbers to disassemble.\n\
773 If the second argument is preceded by '+' or '-', it specifies the distance \
774 from the first argument.\n\
775 The number of instructions to disassemble can be defined with \"set record \
776 instruction-history-size\"."),
777 &record_cmdlist);
778
779 add_cmd ("function-call-history", class_obscure, cmd_record_call_history, _("\
780 Prints the execution history at function granularity.\n\
781 It prints one line for each sequence of instructions that belong to the same \
782 function.\n\
783 Without modifiers, it prints the function name.\n\
784 With a /l modifier, the source file and line number range is included.\n\
785 With a /i modifier, the instruction number range is included.\n\
786 With no argument, prints ten more lines after the previous ten-line print.\n\
787 \"record function-call-history -\" prints ten lines before a previous ten-line \
788 print.\n\
789 One argument specifies a function number as shown by 'info record', and \
790 ten lines are printed after that function.\n\
791 Two arguments with comma between them specify a range of functions to print.\n\
792 If the second argument is preceded by '+' or '-', it specifies the distance \
793 from the first argument.\n\
794 The number of functions to print can be defined with \"set record \
795 function-call-history-size\"."),
796 &record_cmdlist);
797
798 /* Sync command control variables. */
799 record_insn_history_size_setshow_var = record_insn_history_size;
800 record_call_history_size_setshow_var = record_call_history_size;
801 }
This page took 0.061598 seconds and 5 git commands to generate.