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