btrace: add replay position to btrace thread info
[external/binutils.git] / gdb / record-btrace.c
1 /* Branch trace support for GDB, the GNU debugger.
2
3    Copyright (C) 2013-2014 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 (btinfo->begin == NULL)
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   record_preopen ();
146
147   if (!target_has_execution)
148     error (_("The program is not being run."));
149
150   if (!target_supports_btrace ())
151     error (_("Target does not support branch tracing."));
152
153   gdb_assert (record_btrace_thread_observer == NULL);
154
155   disable_chain = make_cleanup (null_cleanup, NULL);
156   ALL_THREADS (tp)
157     if (args == NULL || *args == 0 || number_is_in_list (args, tp->num))
158       {
159         btrace_enable (tp);
160
161         make_cleanup (record_btrace_disable_callback, tp);
162       }
163
164   record_btrace_auto_enable ();
165
166   push_target (&record_btrace_ops);
167
168   observer_notify_record_changed (current_inferior (),  1);
169
170   discard_cleanups (disable_chain);
171 }
172
173 /* The to_stop_recording method of target record-btrace.  */
174
175 static void
176 record_btrace_stop_recording (void)
177 {
178   struct thread_info *tp;
179
180   DEBUG ("stop recording");
181
182   record_btrace_auto_disable ();
183
184   ALL_THREADS (tp)
185     if (tp->btrace.target != NULL)
186       btrace_disable (tp);
187 }
188
189 /* The to_close method of target record-btrace.  */
190
191 static void
192 record_btrace_close (void)
193 {
194   /* Make sure automatic recording gets disabled even if we did not stop
195      recording before closing the record-btrace target.  */
196   record_btrace_auto_disable ();
197
198   /* We already stopped recording.  */
199 }
200
201 /* The to_info_record method of target record-btrace.  */
202
203 static void
204 record_btrace_info (void)
205 {
206   struct btrace_thread_info *btinfo;
207   struct thread_info *tp;
208   unsigned int insns, calls;
209
210   DEBUG ("info");
211
212   tp = find_thread_ptid (inferior_ptid);
213   if (tp == NULL)
214     error (_("No thread."));
215
216   btrace_fetch (tp);
217
218   insns = 0;
219   calls = 0;
220
221   btinfo = &tp->btrace;
222   if (btinfo->begin != NULL)
223     {
224       struct btrace_call_iterator call;
225       struct btrace_insn_iterator insn;
226
227       btrace_call_end (&call, btinfo);
228       btrace_call_prev (&call, 1);
229       calls = btrace_call_number (&call);
230
231       btrace_insn_end (&insn, btinfo);
232       btrace_insn_prev (&insn, 1);
233       insns = btrace_insn_number (&insn);
234     }
235
236   printf_unfiltered (_("Recorded %u instructions in %u functions for thread "
237                        "%d (%s).\n"), insns, calls, tp->num,
238                      target_pid_to_str (tp->ptid));
239
240   if (btrace_is_replaying (tp))
241     printf_unfiltered (_("Replay in progress.  At instruction %u.\n"),
242                        btrace_insn_number (btinfo->replay));
243 }
244
245 /* Print an unsigned int.  */
246
247 static void
248 ui_out_field_uint (struct ui_out *uiout, const char *fld, unsigned int val)
249 {
250   ui_out_field_fmt (uiout, fld, "%u", val);
251 }
252
253 /* Disassemble a section of the recorded instruction trace.  */
254
255 static void
256 btrace_insn_history (struct ui_out *uiout,
257                      const struct btrace_insn_iterator *begin,
258                      const struct btrace_insn_iterator *end, int flags)
259 {
260   struct gdbarch *gdbarch;
261   struct btrace_insn_iterator it;
262
263   DEBUG ("itrace (0x%x): [%u; %u)", flags, btrace_insn_number (begin),
264          btrace_insn_number (end));
265
266   gdbarch = target_gdbarch ();
267
268   for (it = *begin; btrace_insn_cmp (&it, end) != 0; btrace_insn_next (&it, 1))
269     {
270       const struct btrace_insn *insn;
271
272       insn = btrace_insn_get (&it);
273
274       /* Print the instruction index.  */
275       ui_out_field_uint (uiout, "index", btrace_insn_number (&it));
276       ui_out_text (uiout, "\t");
277
278       /* Disassembly with '/m' flag may not produce the expected result.
279          See PR gdb/11833.  */
280       gdb_disassembly (gdbarch, uiout, NULL, flags, 1, insn->pc, insn->pc + 1);
281     }
282 }
283
284 /* The to_insn_history method of target record-btrace.  */
285
286 static void
287 record_btrace_insn_history (int size, int flags)
288 {
289   struct btrace_thread_info *btinfo;
290   struct btrace_insn_history *history;
291   struct btrace_insn_iterator begin, end;
292   struct cleanup *uiout_cleanup;
293   struct ui_out *uiout;
294   unsigned int context, covered;
295
296   uiout = current_uiout;
297   uiout_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout,
298                                                        "insn history");
299   context = abs (size);
300   if (context == 0)
301     error (_("Bad record instruction-history-size."));
302
303   btinfo = require_btrace ();
304   history = btinfo->insn_history;
305   if (history == NULL)
306     {
307       struct btrace_insn_iterator *replay;
308
309       DEBUG ("insn-history (0x%x): %d", flags, size);
310
311       /* If we're replaying, we start at the replay position.  Otherwise, we
312          start at the tail of the trace.  */
313       replay = btinfo->replay;
314       if (replay != NULL)
315         begin = *replay;
316       else
317         btrace_insn_end (&begin, btinfo);
318
319       /* We start from here and expand in the requested direction.  Then we
320          expand in the other direction, as well, to fill up any remaining
321          context.  */
322       end = begin;
323       if (size < 0)
324         {
325           /* We want the current position covered, as well.  */
326           covered = btrace_insn_next (&end, 1);
327           covered += btrace_insn_prev (&begin, context - covered);
328           covered += btrace_insn_next (&end, context - covered);
329         }
330       else
331         {
332           covered = btrace_insn_next (&end, context);
333           covered += btrace_insn_prev (&begin, context - covered);
334         }
335     }
336   else
337     {
338       begin = history->begin;
339       end = history->end;
340
341       DEBUG ("insn-history (0x%x): %d, prev: [%u; %u)", flags, size,
342              btrace_insn_number (&begin), btrace_insn_number (&end));
343
344       if (size < 0)
345         {
346           end = begin;
347           covered = btrace_insn_prev (&begin, context);
348         }
349       else
350         {
351           begin = end;
352           covered = btrace_insn_next (&end, context);
353         }
354     }
355
356   if (covered > 0)
357     btrace_insn_history (uiout, &begin, &end, flags);
358   else
359     {
360       if (size < 0)
361         printf_unfiltered (_("At the start of the branch trace record.\n"));
362       else
363         printf_unfiltered (_("At the end of the branch trace record.\n"));
364     }
365
366   btrace_set_insn_history (btinfo, &begin, &end);
367   do_cleanups (uiout_cleanup);
368 }
369
370 /* The to_insn_history_range method of target record-btrace.  */
371
372 static void
373 record_btrace_insn_history_range (ULONGEST from, ULONGEST to, int flags)
374 {
375   struct btrace_thread_info *btinfo;
376   struct btrace_insn_history *history;
377   struct btrace_insn_iterator begin, end;
378   struct cleanup *uiout_cleanup;
379   struct ui_out *uiout;
380   unsigned int low, high;
381   int found;
382
383   uiout = current_uiout;
384   uiout_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout,
385                                                        "insn history");
386   low = from;
387   high = to;
388
389   DEBUG ("insn-history (0x%x): [%u; %u)", flags, low, high);
390
391   /* Check for wrap-arounds.  */
392   if (low != from || high != to)
393     error (_("Bad range."));
394
395   if (high < low)
396     error (_("Bad range."));
397
398   btinfo = require_btrace ();
399
400   found = btrace_find_insn_by_number (&begin, btinfo, low);
401   if (found == 0)
402     error (_("Range out of bounds."));
403
404   found = btrace_find_insn_by_number (&end, btinfo, high);
405   if (found == 0)
406     {
407       /* Silently truncate the range.  */
408       btrace_insn_end (&end, btinfo);
409     }
410   else
411     {
412       /* We want both begin and end to be inclusive.  */
413       btrace_insn_next (&end, 1);
414     }
415
416   btrace_insn_history (uiout, &begin, &end, flags);
417   btrace_set_insn_history (btinfo, &begin, &end);
418
419   do_cleanups (uiout_cleanup);
420 }
421
422 /* The to_insn_history_from method of target record-btrace.  */
423
424 static void
425 record_btrace_insn_history_from (ULONGEST from, int size, int flags)
426 {
427   ULONGEST begin, end, context;
428
429   context = abs (size);
430   if (context == 0)
431     error (_("Bad record instruction-history-size."));
432
433   if (size < 0)
434     {
435       end = from;
436
437       if (from < context)
438         begin = 0;
439       else
440         begin = from - context + 1;
441     }
442   else
443     {
444       begin = from;
445       end = from + context - 1;
446
447       /* Check for wrap-around.  */
448       if (end < begin)
449         end = ULONGEST_MAX;
450     }
451
452   record_btrace_insn_history_range (begin, end, flags);
453 }
454
455 /* Print the instruction number range for a function call history line.  */
456
457 static void
458 btrace_call_history_insn_range (struct ui_out *uiout,
459                                 const struct btrace_function *bfun)
460 {
461   unsigned int begin, end, size;
462
463   size = VEC_length (btrace_insn_s, bfun->insn);
464   gdb_assert (size > 0);
465
466   begin = bfun->insn_offset;
467   end = begin + size - 1;
468
469   ui_out_field_uint (uiout, "insn begin", begin);
470   ui_out_text (uiout, ",");
471   ui_out_field_uint (uiout, "insn end", end);
472 }
473
474 /* Print the source line information for a function call history line.  */
475
476 static void
477 btrace_call_history_src_line (struct ui_out *uiout,
478                               const struct btrace_function *bfun)
479 {
480   struct symbol *sym;
481   int begin, end;
482
483   sym = bfun->sym;
484   if (sym == NULL)
485     return;
486
487   ui_out_field_string (uiout, "file",
488                        symtab_to_filename_for_display (sym->symtab));
489
490   begin = bfun->lbegin;
491   end = bfun->lend;
492
493   if (end < begin)
494     return;
495
496   ui_out_text (uiout, ":");
497   ui_out_field_int (uiout, "min line", begin);
498
499   if (end == begin)
500     return;
501
502   ui_out_text (uiout, ",");
503   ui_out_field_int (uiout, "max line", end);
504 }
505
506 /* Disassemble a section of the recorded function trace.  */
507
508 static void
509 btrace_call_history (struct ui_out *uiout,
510                      const struct btrace_thread_info *btinfo,
511                      const struct btrace_call_iterator *begin,
512                      const struct btrace_call_iterator *end,
513                      enum record_print_flag flags)
514 {
515   struct btrace_call_iterator it;
516
517   DEBUG ("ftrace (0x%x): [%u; %u)", flags, btrace_call_number (begin),
518          btrace_call_number (end));
519
520   for (it = *begin; btrace_call_cmp (&it, end) < 0; btrace_call_next (&it, 1))
521     {
522       const struct btrace_function *bfun;
523       struct minimal_symbol *msym;
524       struct symbol *sym;
525
526       bfun = btrace_call_get (&it);
527       msym = bfun->msym;
528       sym = bfun->sym;
529
530       /* Print the function index.  */
531       ui_out_field_uint (uiout, "index", bfun->number);
532       ui_out_text (uiout, "\t");
533
534       if ((flags & RECORD_PRINT_INDENT_CALLS) != 0)
535         {
536           int level = bfun->level + btinfo->level, i;
537
538           for (i = 0; i < level; ++i)
539             ui_out_text (uiout, "  ");
540         }
541
542       if (sym != NULL)
543         ui_out_field_string (uiout, "function", SYMBOL_PRINT_NAME (sym));
544       else if (msym != NULL)
545         ui_out_field_string (uiout, "function", SYMBOL_PRINT_NAME (msym));
546       else if (!ui_out_is_mi_like_p (uiout))
547         ui_out_field_string (uiout, "function", "??");
548
549       if ((flags & RECORD_PRINT_INSN_RANGE) != 0)
550         {
551           ui_out_text (uiout, _("\tinst "));
552           btrace_call_history_insn_range (uiout, bfun);
553         }
554
555       if ((flags & RECORD_PRINT_SRC_LINE) != 0)
556         {
557           ui_out_text (uiout, _("\tat "));
558           btrace_call_history_src_line (uiout, bfun);
559         }
560
561       ui_out_text (uiout, "\n");
562     }
563 }
564
565 /* The to_call_history method of target record-btrace.  */
566
567 static void
568 record_btrace_call_history (int size, int flags)
569 {
570   struct btrace_thread_info *btinfo;
571   struct btrace_call_history *history;
572   struct btrace_call_iterator begin, end;
573   struct cleanup *uiout_cleanup;
574   struct ui_out *uiout;
575   unsigned int context, covered;
576
577   uiout = current_uiout;
578   uiout_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout,
579                                                        "insn history");
580   context = abs (size);
581   if (context == 0)
582     error (_("Bad record function-call-history-size."));
583
584   btinfo = require_btrace ();
585   history = btinfo->call_history;
586   if (history == NULL)
587     {
588       struct btrace_insn_iterator *replay;
589
590       DEBUG ("call-history (0x%x): %d", flags, size);
591
592       /* If we're replaying, we start at the replay position.  Otherwise, we
593          start at the tail of the trace.  */
594       replay = btinfo->replay;
595       if (replay != NULL)
596         {
597           begin.function = replay->function;
598           begin.btinfo = btinfo;
599         }
600       else
601         btrace_call_end (&begin, btinfo);
602
603       /* We start from here and expand in the requested direction.  Then we
604          expand in the other direction, as well, to fill up any remaining
605          context.  */
606       end = begin;
607       if (size < 0)
608         {
609           /* We want the current position covered, as well.  */
610           covered = btrace_call_next (&end, 1);
611           covered += btrace_call_prev (&begin, context - covered);
612           covered += btrace_call_next (&end, context - covered);
613         }
614       else
615         {
616           covered = btrace_call_next (&end, context);
617           covered += btrace_call_prev (&begin, context- covered);
618         }
619     }
620   else
621     {
622       begin = history->begin;
623       end = history->end;
624
625       DEBUG ("call-history (0x%x): %d, prev: [%u; %u)", flags, size,
626              btrace_call_number (&begin), btrace_call_number (&end));
627
628       if (size < 0)
629         {
630           end = begin;
631           covered = btrace_call_prev (&begin, context);
632         }
633       else
634         {
635           begin = end;
636           covered = btrace_call_next (&end, context);
637         }
638     }
639
640   if (covered > 0)
641     btrace_call_history (uiout, btinfo, &begin, &end, flags);
642   else
643     {
644       if (size < 0)
645         printf_unfiltered (_("At the start of the branch trace record.\n"));
646       else
647         printf_unfiltered (_("At the end of the branch trace record.\n"));
648     }
649
650   btrace_set_call_history (btinfo, &begin, &end);
651   do_cleanups (uiout_cleanup);
652 }
653
654 /* The to_call_history_range method of target record-btrace.  */
655
656 static void
657 record_btrace_call_history_range (ULONGEST from, ULONGEST to, int flags)
658 {
659   struct btrace_thread_info *btinfo;
660   struct btrace_call_history *history;
661   struct btrace_call_iterator begin, end;
662   struct cleanup *uiout_cleanup;
663   struct ui_out *uiout;
664   unsigned int low, high;
665   int found;
666
667   uiout = current_uiout;
668   uiout_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout,
669                                                        "func history");
670   low = from;
671   high = to;
672
673   DEBUG ("call-history (0x%x): [%u; %u)", flags, low, high);
674
675   /* Check for wrap-arounds.  */
676   if (low != from || high != to)
677     error (_("Bad range."));
678
679   if (high < low)
680     error (_("Bad range."));
681
682   btinfo = require_btrace ();
683
684   found = btrace_find_call_by_number (&begin, btinfo, low);
685   if (found == 0)
686     error (_("Range out of bounds."));
687
688   found = btrace_find_call_by_number (&end, btinfo, high);
689   if (found == 0)
690     {
691       /* Silently truncate the range.  */
692       btrace_call_end (&end, btinfo);
693     }
694   else
695     {
696       /* We want both begin and end to be inclusive.  */
697       btrace_call_next (&end, 1);
698     }
699
700   btrace_call_history (uiout, btinfo, &begin, &end, flags);
701   btrace_set_call_history (btinfo, &begin, &end);
702
703   do_cleanups (uiout_cleanup);
704 }
705
706 /* The to_call_history_from method of target record-btrace.  */
707
708 static void
709 record_btrace_call_history_from (ULONGEST from, int size, int flags)
710 {
711   ULONGEST begin, end, context;
712
713   context = abs (size);
714   if (context == 0)
715     error (_("Bad record function-call-history-size."));
716
717   if (size < 0)
718     {
719       end = from;
720
721       if (from < context)
722         begin = 0;
723       else
724         begin = from - context + 1;
725     }
726   else
727     {
728       begin = from;
729       end = from + context - 1;
730
731       /* Check for wrap-around.  */
732       if (end < begin)
733         end = ULONGEST_MAX;
734     }
735
736   record_btrace_call_history_range (begin, end, flags);
737 }
738
739 /* The to_record_is_replaying method of target record-btrace.  */
740
741 static int
742 record_btrace_is_replaying (void)
743 {
744   struct thread_info *tp;
745
746   ALL_THREADS (tp)
747     if (btrace_is_replaying (tp))
748       return 1;
749
750   return 0;
751 }
752
753 /* Initialize the record-btrace target ops.  */
754
755 static void
756 init_record_btrace_ops (void)
757 {
758   struct target_ops *ops;
759
760   ops = &record_btrace_ops;
761   ops->to_shortname = "record-btrace";
762   ops->to_longname = "Branch tracing target";
763   ops->to_doc = "Collect control-flow trace and provide the execution history.";
764   ops->to_open = record_btrace_open;
765   ops->to_close = record_btrace_close;
766   ops->to_detach = record_detach;
767   ops->to_disconnect = record_disconnect;
768   ops->to_mourn_inferior = record_mourn_inferior;
769   ops->to_kill = record_kill;
770   ops->to_create_inferior = find_default_create_inferior;
771   ops->to_stop_recording = record_btrace_stop_recording;
772   ops->to_info_record = record_btrace_info;
773   ops->to_insn_history = record_btrace_insn_history;
774   ops->to_insn_history_from = record_btrace_insn_history_from;
775   ops->to_insn_history_range = record_btrace_insn_history_range;
776   ops->to_call_history = record_btrace_call_history;
777   ops->to_call_history_from = record_btrace_call_history_from;
778   ops->to_call_history_range = record_btrace_call_history_range;
779   ops->to_record_is_replaying = record_btrace_is_replaying;
780   ops->to_stratum = record_stratum;
781   ops->to_magic = OPS_MAGIC;
782 }
783
784 /* Alias for "target record".  */
785
786 static void
787 cmd_record_btrace_start (char *args, int from_tty)
788 {
789   if (args != NULL && *args != 0)
790     error (_("Invalid argument."));
791
792   execute_command ("target record-btrace", from_tty);
793 }
794
795 void _initialize_record_btrace (void);
796
797 /* Initialize btrace commands.  */
798
799 void
800 _initialize_record_btrace (void)
801 {
802   add_cmd ("btrace", class_obscure, cmd_record_btrace_start,
803            _("Start branch trace recording."),
804            &record_cmdlist);
805   add_alias_cmd ("b", "btrace", class_obscure, 1, &record_cmdlist);
806
807   init_record_btrace_ops ();
808   add_target (&record_btrace_ops);
809 }