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