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