gdb/
[external/binutils.git] / gdb / record-btrace.c
1 /* Branch trace support for GDB, the GNU debugger.
2
3    Copyright (C) 2013 Free Software Foundation, Inc.
4
5    Contributed by Intel Corp. <markus.t.metzger@intel.com>
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
22 #include "defs.h"
23 #include "record.h"
24 #include "gdbthread.h"
25 #include "target.h"
26 #include "gdbcmd.h"
27 #include "disasm.h"
28 #include "observer.h"
29 #include "exceptions.h"
30 #include "cli/cli-utils.h"
31 #include "source.h"
32 #include "ui-out.h"
33 #include "symtab.h"
34 #include "filenames.h"
35
36 /* The target_ops of record-btrace.  */
37 static struct target_ops record_btrace_ops;
38
39 /* A new thread observer enabling branch tracing for the new thread.  */
40 static struct observer *record_btrace_thread_observer;
41
42 /* Print a record-btrace debug message.  Use do ... while (0) to avoid
43    ambiguities when used in if statements.  */
44
45 #define DEBUG(msg, args...)                                             \
46   do                                                                    \
47     {                                                                   \
48       if (record_debug != 0)                                            \
49         fprintf_unfiltered (gdb_stdlog,                                 \
50                             "[record-btrace] " msg "\n", ##args);       \
51     }                                                                   \
52   while (0)
53
54
55 /* Update the branch trace for the current thread and return a pointer to its
56    branch trace information struct.
57
58    Throws an error if there is no thread or no trace.  This function never
59    returns NULL.  */
60
61 static struct btrace_thread_info *
62 require_btrace (void)
63 {
64   struct thread_info *tp;
65   struct btrace_thread_info *btinfo;
66
67   DEBUG ("require");
68
69   tp = find_thread_ptid (inferior_ptid);
70   if (tp == NULL)
71     error (_("No thread."));
72
73   btrace_fetch (tp);
74
75   btinfo = &tp->btrace;
76
77   if (VEC_empty (btrace_inst_s, btinfo->itrace))
78     error (_("No trace."));
79
80   return btinfo;
81 }
82
83 /* Enable branch tracing for one thread.  Warn on errors.  */
84
85 static void
86 record_btrace_enable_warn (struct thread_info *tp)
87 {
88   volatile struct gdb_exception error;
89
90   TRY_CATCH (error, RETURN_MASK_ERROR)
91     btrace_enable (tp);
92
93   if (error.message != NULL)
94     warning ("%s", error.message);
95 }
96
97 /* Callback function to disable branch tracing for one thread.  */
98
99 static void
100 record_btrace_disable_callback (void *arg)
101 {
102   struct thread_info *tp;
103
104   tp = arg;
105
106   btrace_disable (tp);
107 }
108
109 /* Enable automatic tracing of new threads.  */
110
111 static void
112 record_btrace_auto_enable (void)
113 {
114   DEBUG ("attach thread observer");
115
116   record_btrace_thread_observer
117     = observer_attach_new_thread (record_btrace_enable_warn);
118 }
119
120 /* Disable automatic tracing of new threads.  */
121
122 static void
123 record_btrace_auto_disable (void)
124 {
125   /* The observer may have been detached, already.  */
126   if (record_btrace_thread_observer == NULL)
127     return;
128
129   DEBUG ("detach thread observer");
130
131   observer_detach_new_thread (record_btrace_thread_observer);
132   record_btrace_thread_observer = NULL;
133 }
134
135 /* The to_open method of target record-btrace.  */
136
137 static void
138 record_btrace_open (char *args, int from_tty)
139 {
140   struct cleanup *disable_chain;
141   struct thread_info *tp;
142
143   DEBUG ("open");
144
145   if (RECORD_IS_USED)
146     error (_("The process is already being recorded."));
147
148   if (!target_has_execution)
149     error (_("The program is not being run."));
150
151   if (!target_supports_btrace ())
152     error (_("Target does not support branch tracing."));
153
154   gdb_assert (record_btrace_thread_observer == NULL);
155
156   disable_chain = make_cleanup (null_cleanup, NULL);
157   ALL_THREADS (tp)
158     if (args == NULL || *args == 0 || number_is_in_list (args, tp->num))
159       {
160         btrace_enable (tp);
161
162         make_cleanup (record_btrace_disable_callback, tp);
163       }
164
165   record_btrace_auto_enable ();
166
167   push_target (&record_btrace_ops);
168
169   observer_notify_record_changed (current_inferior (),  1);
170
171   discard_cleanups (disable_chain);
172 }
173
174 /* The to_stop_recording method of target record-btrace.  */
175
176 static void
177 record_btrace_stop_recording (void)
178 {
179   struct thread_info *tp;
180
181   DEBUG ("stop recording");
182
183   record_btrace_auto_disable ();
184
185   ALL_THREADS (tp)
186     if (tp->btrace.target != NULL)
187       btrace_disable (tp);
188 }
189
190 /* The to_close method of target record-btrace.  */
191
192 static void
193 record_btrace_close (void)
194 {
195   /* We already stopped recording.  */
196 }
197
198 /* The to_info_record method of target record-btrace.  */
199
200 static void
201 record_btrace_info (void)
202 {
203   struct btrace_thread_info *btinfo;
204   struct thread_info *tp;
205   unsigned int insts, funcs;
206
207   DEBUG ("info");
208
209   tp = find_thread_ptid (inferior_ptid);
210   if (tp == NULL)
211     error (_("No thread."));
212
213   btrace_fetch (tp);
214
215   btinfo = &tp->btrace;
216   insts = VEC_length (btrace_inst_s, btinfo->itrace);
217   funcs = VEC_length (btrace_func_s, btinfo->ftrace);
218
219   printf_unfiltered (_("Recorded %u instructions in %u functions for thread "
220                        "%d (%s).\n"), insts, funcs, tp->num,
221                      target_pid_to_str (tp->ptid));
222 }
223
224 /* Print an unsigned int.  */
225
226 static void
227 ui_out_field_uint (struct ui_out *uiout, const char *fld, unsigned int val)
228 {
229   ui_out_field_fmt (uiout, fld, "%u", val);
230 }
231
232 /* Disassemble a section of the recorded instruction trace.  */
233
234 static void
235 btrace_insn_history (struct btrace_thread_info *btinfo, struct ui_out *uiout,
236                      unsigned int begin, unsigned int end, int flags)
237 {
238   struct gdbarch *gdbarch;
239   struct btrace_inst *inst;
240   unsigned int idx;
241
242   DEBUG ("itrace (0x%x): [%u; %u[", flags, begin, end);
243
244   gdbarch = target_gdbarch ();
245
246   for (idx = begin; VEC_iterate (btrace_inst_s, btinfo->itrace, idx, inst)
247          && idx < end; ++idx)
248     {
249       /* Print the instruction index.  */
250       ui_out_field_uint (uiout, "index", idx);
251       ui_out_text (uiout, "\t");
252
253       /* Disassembly with '/m' flag may not produce the expected result.
254          See PR gdb/11833.  */
255       gdb_disassembly (gdbarch, uiout, NULL, flags, 1, inst->pc, inst->pc + 1);
256     }
257 }
258
259 /* The to_insn_history method of target record-btrace.  */
260
261 static void
262 record_btrace_insn_history (int size, int flags)
263 {
264   struct btrace_thread_info *btinfo;
265   struct cleanup *uiout_cleanup;
266   struct ui_out *uiout;
267   unsigned int context, last, begin, end;
268
269   uiout = current_uiout;
270   uiout_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout,
271                                                        "insn history");
272   btinfo = require_btrace ();
273   last = VEC_length (btrace_inst_s, btinfo->itrace);
274
275   context = abs (size);
276   begin = btinfo->insn_iterator.begin;
277   end = btinfo->insn_iterator.end;
278
279   DEBUG ("insn-history (0x%x): %d, prev: [%u; %u[", flags, size, begin, end);
280
281   if (context == 0)
282     error (_("Bad record instruction-history-size."));
283
284   /* We start at the end.  */
285   if (end < begin)
286     {
287       /* Truncate the context, if necessary.  */
288       context = min (context, last);
289
290       end = last;
291       begin = end - context;
292     }
293   else if (size < 0)
294     {
295       if (begin == 0)
296         {
297           printf_unfiltered (_("At the start of the branch trace record.\n"));
298
299           btinfo->insn_iterator.end = 0;
300           return;
301         }
302
303       /* Truncate the context, if necessary.  */
304       context = min (context, begin);
305
306       end = begin;
307       begin -= context;
308     }
309   else
310     {
311       if (end == last)
312         {
313           printf_unfiltered (_("At the end of the branch trace record.\n"));
314
315           btinfo->insn_iterator.begin = last;
316           return;
317         }
318
319       /* Truncate the context, if necessary.  */
320       context = min (context, last - end);
321
322       begin = end;
323       end += context;
324     }
325
326   btrace_insn_history (btinfo, uiout, begin, end, flags);
327
328   btinfo->insn_iterator.begin = begin;
329   btinfo->insn_iterator.end = end;
330
331   do_cleanups (uiout_cleanup);
332 }
333
334 /* The to_insn_history_range method of target record-btrace.  */
335
336 static void
337 record_btrace_insn_history_range (ULONGEST from, ULONGEST to, int flags)
338 {
339   struct btrace_thread_info *btinfo;
340   struct cleanup *uiout_cleanup;
341   struct ui_out *uiout;
342   unsigned int last, begin, end;
343
344   uiout = current_uiout;
345   uiout_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout,
346                                                        "insn history");
347   btinfo = require_btrace ();
348   last = VEC_length (btrace_inst_s, btinfo->itrace);
349
350   begin = (unsigned int) from;
351   end = (unsigned int) to;
352
353   DEBUG ("insn-history (0x%x): [%u; %u[", flags, begin, end);
354
355   /* Check for wrap-arounds.  */
356   if (begin != from || end != to)
357     error (_("Bad range."));
358
359   if (end <= begin)
360     error (_("Bad range."));
361
362   if (last <= begin)
363     error (_("Range out of bounds."));
364
365   /* Truncate the range, if necessary.  */
366   if (last < end)
367     end = last;
368
369   btrace_insn_history (btinfo, uiout, begin, end, flags);
370
371   btinfo->insn_iterator.begin = begin;
372   btinfo->insn_iterator.end = end;
373
374   do_cleanups (uiout_cleanup);
375 }
376
377 /* The to_insn_history_from method of target record-btrace.  */
378
379 static void
380 record_btrace_insn_history_from (ULONGEST from, int size, int flags)
381 {
382   ULONGEST begin, end, context;
383
384   context = abs (size);
385
386   if (size < 0)
387     {
388       end = from;
389
390       if (from < context)
391         begin = 0;
392       else
393         begin = from - context;
394     }
395   else
396     {
397       begin = from;
398       end = from + context;
399
400       /* Check for wrap-around.  */
401       if (end < begin)
402         end = ULONGEST_MAX;
403     }
404
405   record_btrace_insn_history_range (begin, end, flags);
406 }
407
408 /* Print the instruction number range for a function call history line.  */
409
410 static void
411 btrace_func_history_insn_range (struct ui_out *uiout, struct btrace_func *bfun)
412 {
413   ui_out_field_uint (uiout, "insn begin", bfun->ibegin);
414
415   if (bfun->ibegin == bfun->iend)
416     return;
417
418   ui_out_text (uiout, "-");
419   ui_out_field_uint (uiout, "insn end", bfun->iend);
420 }
421
422 /* Print the source line information for a function call history line.  */
423
424 static void
425 btrace_func_history_src_line (struct ui_out *uiout, struct btrace_func *bfun)
426 {
427   struct symbol *sym;
428
429   sym = bfun->sym;
430   if (sym == NULL)
431     return;
432
433   ui_out_field_string (uiout, "file",
434                        symtab_to_filename_for_display (sym->symtab));
435
436   if (bfun->lend == 0)
437     return;
438
439   ui_out_text (uiout, ":");
440   ui_out_field_int (uiout, "min line", bfun->lbegin);
441
442   if (bfun->lend == bfun->lbegin)
443     return;
444
445   ui_out_text (uiout, "-");
446   ui_out_field_int (uiout, "max line", bfun->lend);
447 }
448
449 /* Disassemble a section of the recorded function trace.  */
450
451 static void
452 btrace_func_history (struct btrace_thread_info *btinfo, struct ui_out *uiout,
453                      unsigned int begin, unsigned int end,
454                      enum record_print_flag flags)
455 {
456   struct btrace_func *bfun;
457   unsigned int idx;
458
459   DEBUG ("ftrace (0x%x): [%u; %u[", flags, begin, end);
460
461   for (idx = begin; VEC_iterate (btrace_func_s, btinfo->ftrace, idx, bfun)
462          && idx < end; ++idx)
463     {
464       /* Print the function index.  */
465       ui_out_field_uint (uiout, "index", idx);
466       ui_out_text (uiout, "\t");
467
468       if ((flags & record_print_insn_range) != 0)
469         {
470           btrace_func_history_insn_range (uiout, bfun);
471           ui_out_text (uiout, "\t");
472         }
473
474       if ((flags & record_print_src_line) != 0)
475         {
476           btrace_func_history_src_line (uiout, bfun);
477           ui_out_text (uiout, "\t");
478         }
479
480       if (bfun->sym != NULL)
481         ui_out_field_string (uiout, "function", SYMBOL_PRINT_NAME (bfun->sym));
482       else if (bfun->msym != NULL)
483         ui_out_field_string (uiout, "function", SYMBOL_PRINT_NAME (bfun->msym));
484       ui_out_text (uiout, "\n");
485     }
486 }
487
488 /* The to_call_history method of target record-btrace.  */
489
490 static void
491 record_btrace_call_history (int size, int flags)
492 {
493   struct btrace_thread_info *btinfo;
494   struct cleanup *uiout_cleanup;
495   struct ui_out *uiout;
496   unsigned int context, last, begin, end;
497
498   uiout = current_uiout;
499   uiout_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout,
500                                                        "insn history");
501   btinfo = require_btrace ();
502   last = VEC_length (btrace_func_s, btinfo->ftrace);
503
504   context = abs (size);
505   begin = btinfo->func_iterator.begin;
506   end = btinfo->func_iterator.end;
507
508   DEBUG ("func-history (0x%x): %d, prev: [%u; %u[", flags, size, begin, end);
509
510   if (context == 0)
511     error (_("Bad record function-call-history-size."));
512
513   /* We start at the end.  */
514   if (end < begin)
515     {
516       /* Truncate the context, if necessary.  */
517       context = min (context, last);
518
519       end = last;
520       begin = end - context;
521     }
522   else if (size < 0)
523     {
524       if (begin == 0)
525         {
526           printf_unfiltered (_("At the start of the branch trace record.\n"));
527
528           btinfo->func_iterator.end = 0;
529           return;
530         }
531
532       /* Truncate the context, if necessary.  */
533       context = min (context, begin);
534
535       end = begin;
536       begin -= context;
537     }
538   else
539     {
540       if (end == last)
541         {
542           printf_unfiltered (_("At the end of the branch trace record.\n"));
543
544           btinfo->func_iterator.begin = last;
545           return;
546         }
547
548       /* Truncate the context, if necessary.  */
549       context = min (context, last - end);
550
551       begin = end;
552       end += context;
553     }
554
555   btrace_func_history (btinfo, uiout, begin, end, flags);
556
557   btinfo->func_iterator.begin = begin;
558   btinfo->func_iterator.end = end;
559
560   do_cleanups (uiout_cleanup);
561 }
562
563 /* The to_call_history_range method of target record-btrace.  */
564
565 static void
566 record_btrace_call_history_range (ULONGEST from, ULONGEST to, int flags)
567 {
568   struct btrace_thread_info *btinfo;
569   struct cleanup *uiout_cleanup;
570   struct ui_out *uiout;
571   unsigned int last, begin, end;
572
573   uiout = current_uiout;
574   uiout_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout,
575                                                        "func history");
576   btinfo = require_btrace ();
577   last = VEC_length (btrace_func_s, btinfo->ftrace);
578
579   begin = (unsigned int) from;
580   end = (unsigned int) to;
581
582   DEBUG ("func-history (0x%x): [%u; %u[", flags, begin, end);
583
584   /* Check for wrap-arounds.  */
585   if (begin != from || end != to)
586     error (_("Bad range."));
587
588   if (end <= begin)
589     error (_("Bad range."));
590
591   if (last <= begin)
592     error (_("Range out of bounds."));
593
594   /* Truncate the range, if necessary.  */
595   if (last < end)
596     end = last;
597
598   btrace_func_history (btinfo, uiout, begin, end, flags);
599
600   btinfo->func_iterator.begin = begin;
601   btinfo->func_iterator.end = end;
602
603   do_cleanups (uiout_cleanup);
604 }
605
606 /* The to_call_history_from method of target record-btrace.  */
607
608 static void
609 record_btrace_call_history_from (ULONGEST from, int size, int flags)
610 {
611   ULONGEST begin, end, context;
612
613   context = abs (size);
614
615   if (size < 0)
616     {
617       end = from;
618
619       if (from < context)
620         begin = 0;
621       else
622         begin = from - context;
623     }
624   else
625     {
626       begin = from;
627       end = from + context;
628
629       /* Check for wrap-around.  */
630       if (end < begin)
631         end = ULONGEST_MAX;
632     }
633
634   record_btrace_call_history_range (begin, end, flags);
635 }
636
637 /* Initialize the record-btrace target ops.  */
638
639 static void
640 init_record_btrace_ops (void)
641 {
642   struct target_ops *ops;
643
644   ops = &record_btrace_ops;
645   ops->to_shortname = "record-btrace";
646   ops->to_longname = "Branch tracing target";
647   ops->to_doc = "Collect control-flow trace and provide the execution history.";
648   ops->to_open = record_btrace_open;
649   ops->to_close = record_btrace_close;
650   ops->to_detach = record_detach;
651   ops->to_disconnect = record_disconnect;
652   ops->to_mourn_inferior = record_mourn_inferior;
653   ops->to_kill = record_kill;
654   ops->to_create_inferior = find_default_create_inferior;
655   ops->to_stop_recording = record_btrace_stop_recording;
656   ops->to_info_record = record_btrace_info;
657   ops->to_insn_history = record_btrace_insn_history;
658   ops->to_insn_history_from = record_btrace_insn_history_from;
659   ops->to_insn_history_range = record_btrace_insn_history_range;
660   ops->to_call_history = record_btrace_call_history;
661   ops->to_call_history_from = record_btrace_call_history_from;
662   ops->to_call_history_range = record_btrace_call_history_range;
663   ops->to_stratum = record_stratum;
664   ops->to_magic = OPS_MAGIC;
665 }
666
667 /* Alias for "target record".  */
668
669 static void
670 cmd_record_btrace_start (char *args, int from_tty)
671 {
672   if (args != NULL && *args != 0)
673     error (_("Invalid argument."));
674
675   execute_command ("target record-btrace", from_tty);
676 }
677
678 void _initialize_record_btrace (void);
679
680 /* Initialize btrace commands.  */
681
682 void
683 _initialize_record_btrace (void)
684 {
685   add_cmd ("btrace", class_obscure, cmd_record_btrace_start,
686            _("Start branch trace recording."),
687            &record_cmdlist);
688   add_alias_cmd ("b", "btrace", class_obscure, 1, &record_cmdlist);
689
690   init_record_btrace_ops ();
691   add_target (&record_btrace_ops);
692 }