1 /* Branch trace support for GDB, the GNU debugger.
3 Copyright (C) 2013-2014 Free Software Foundation, Inc.
5 Contributed by Intel Corp. <markus.t.metzger@intel.com>
7 This file is part of GDB.
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.
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.
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/>. */
24 #include "gdbthread.h"
29 #include "exceptions.h"
30 #include "cli/cli-utils.h"
34 #include "filenames.h"
37 /* The target_ops of record-btrace. */
38 static struct target_ops record_btrace_ops;
40 /* A new thread observer enabling branch tracing for the new thread. */
41 static struct observer *record_btrace_thread_observer;
43 /* Print a record-btrace debug message. Use do ... while (0) to avoid
44 ambiguities when used in if statements. */
46 #define DEBUG(msg, args...) \
49 if (record_debug != 0) \
50 fprintf_unfiltered (gdb_stdlog, \
51 "[record-btrace] " msg "\n", ##args); \
56 /* Update the branch trace for the current thread and return a pointer to its
57 branch trace information struct.
59 Throws an error if there is no thread or no trace. This function never
62 static struct btrace_thread_info *
65 struct thread_info *tp;
66 struct btrace_thread_info *btinfo;
70 tp = find_thread_ptid (inferior_ptid);
72 error (_("No thread."));
78 if (btinfo->begin == NULL)
79 error (_("No trace."));
84 /* Enable branch tracing for one thread. Warn on errors. */
87 record_btrace_enable_warn (struct thread_info *tp)
89 volatile struct gdb_exception error;
91 TRY_CATCH (error, RETURN_MASK_ERROR)
94 if (error.message != NULL)
95 warning ("%s", error.message);
98 /* Callback function to disable branch tracing for one thread. */
101 record_btrace_disable_callback (void *arg)
103 struct thread_info *tp;
110 /* Enable automatic tracing of new threads. */
113 record_btrace_auto_enable (void)
115 DEBUG ("attach thread observer");
117 record_btrace_thread_observer
118 = observer_attach_new_thread (record_btrace_enable_warn);
121 /* Disable automatic tracing of new threads. */
124 record_btrace_auto_disable (void)
126 /* The observer may have been detached, already. */
127 if (record_btrace_thread_observer == NULL)
130 DEBUG ("detach thread observer");
132 observer_detach_new_thread (record_btrace_thread_observer);
133 record_btrace_thread_observer = NULL;
136 /* The to_open method of target record-btrace. */
139 record_btrace_open (char *args, int from_tty)
141 struct cleanup *disable_chain;
142 struct thread_info *tp;
148 if (!target_has_execution)
149 error (_("The program is not being run."));
151 if (!target_supports_btrace ())
152 error (_("Target does not support branch tracing."));
154 gdb_assert (record_btrace_thread_observer == NULL);
156 disable_chain = make_cleanup (null_cleanup, NULL);
158 if (args == NULL || *args == 0 || number_is_in_list (args, tp->num))
162 make_cleanup (record_btrace_disable_callback, tp);
165 record_btrace_auto_enable ();
167 push_target (&record_btrace_ops);
169 observer_notify_record_changed (current_inferior (), 1);
171 discard_cleanups (disable_chain);
174 /* The to_stop_recording method of target record-btrace. */
177 record_btrace_stop_recording (void)
179 struct thread_info *tp;
181 DEBUG ("stop recording");
183 record_btrace_auto_disable ();
186 if (tp->btrace.target != NULL)
190 /* The to_close method of target record-btrace. */
193 record_btrace_close (void)
195 /* Make sure automatic recording gets disabled even if we did not stop
196 recording before closing the record-btrace target. */
197 record_btrace_auto_disable ();
199 /* We already stopped recording. */
202 /* The to_info_record method of target record-btrace. */
205 record_btrace_info (void)
207 struct btrace_thread_info *btinfo;
208 struct thread_info *tp;
209 unsigned int insns, calls;
213 tp = find_thread_ptid (inferior_ptid);
215 error (_("No thread."));
222 btinfo = &tp->btrace;
223 if (btinfo->begin != NULL)
225 struct btrace_call_iterator call;
226 struct btrace_insn_iterator insn;
228 btrace_call_end (&call, btinfo);
229 btrace_call_prev (&call, 1);
230 calls = btrace_call_number (&call);
232 btrace_insn_end (&insn, btinfo);
233 btrace_insn_prev (&insn, 1);
234 insns = btrace_insn_number (&insn);
237 printf_unfiltered (_("Recorded %u instructions in %u functions for thread "
238 "%d (%s).\n"), insns, calls, tp->num,
239 target_pid_to_str (tp->ptid));
241 if (btrace_is_replaying (tp))
242 printf_unfiltered (_("Replay in progress. At instruction %u.\n"),
243 btrace_insn_number (btinfo->replay));
246 /* Print an unsigned int. */
249 ui_out_field_uint (struct ui_out *uiout, const char *fld, unsigned int val)
251 ui_out_field_fmt (uiout, fld, "%u", val);
254 /* Disassemble a section of the recorded instruction trace. */
257 btrace_insn_history (struct ui_out *uiout,
258 const struct btrace_insn_iterator *begin,
259 const struct btrace_insn_iterator *end, int flags)
261 struct gdbarch *gdbarch;
262 struct btrace_insn_iterator it;
264 DEBUG ("itrace (0x%x): [%u; %u)", flags, btrace_insn_number (begin),
265 btrace_insn_number (end));
267 gdbarch = target_gdbarch ();
269 for (it = *begin; btrace_insn_cmp (&it, end) != 0; btrace_insn_next (&it, 1))
271 const struct btrace_insn *insn;
273 insn = btrace_insn_get (&it);
275 /* Print the instruction index. */
276 ui_out_field_uint (uiout, "index", btrace_insn_number (&it));
277 ui_out_text (uiout, "\t");
279 /* Disassembly with '/m' flag may not produce the expected result.
281 gdb_disassembly (gdbarch, uiout, NULL, flags, 1, insn->pc, insn->pc + 1);
285 /* The to_insn_history method of target record-btrace. */
288 record_btrace_insn_history (int size, int flags)
290 struct btrace_thread_info *btinfo;
291 struct btrace_insn_history *history;
292 struct btrace_insn_iterator begin, end;
293 struct cleanup *uiout_cleanup;
294 struct ui_out *uiout;
295 unsigned int context, covered;
297 uiout = current_uiout;
298 uiout_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout,
300 context = abs (size);
302 error (_("Bad record instruction-history-size."));
304 btinfo = require_btrace ();
305 history = btinfo->insn_history;
308 struct btrace_insn_iterator *replay;
310 DEBUG ("insn-history (0x%x): %d", flags, size);
312 /* If we're replaying, we start at the replay position. Otherwise, we
313 start at the tail of the trace. */
314 replay = btinfo->replay;
318 btrace_insn_end (&begin, btinfo);
320 /* We start from here and expand in the requested direction. Then we
321 expand in the other direction, as well, to fill up any remaining
326 /* We want the current position covered, as well. */
327 covered = btrace_insn_next (&end, 1);
328 covered += btrace_insn_prev (&begin, context - covered);
329 covered += btrace_insn_next (&end, context - covered);
333 covered = btrace_insn_next (&end, context);
334 covered += btrace_insn_prev (&begin, context - covered);
339 begin = history->begin;
342 DEBUG ("insn-history (0x%x): %d, prev: [%u; %u)", flags, size,
343 btrace_insn_number (&begin), btrace_insn_number (&end));
348 covered = btrace_insn_prev (&begin, context);
353 covered = btrace_insn_next (&end, context);
358 btrace_insn_history (uiout, &begin, &end, flags);
362 printf_unfiltered (_("At the start of the branch trace record.\n"));
364 printf_unfiltered (_("At the end of the branch trace record.\n"));
367 btrace_set_insn_history (btinfo, &begin, &end);
368 do_cleanups (uiout_cleanup);
371 /* The to_insn_history_range method of target record-btrace. */
374 record_btrace_insn_history_range (ULONGEST from, ULONGEST to, int flags)
376 struct btrace_thread_info *btinfo;
377 struct btrace_insn_history *history;
378 struct btrace_insn_iterator begin, end;
379 struct cleanup *uiout_cleanup;
380 struct ui_out *uiout;
381 unsigned int low, high;
384 uiout = current_uiout;
385 uiout_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout,
390 DEBUG ("insn-history (0x%x): [%u; %u)", flags, low, high);
392 /* Check for wrap-arounds. */
393 if (low != from || high != to)
394 error (_("Bad range."));
397 error (_("Bad range."));
399 btinfo = require_btrace ();
401 found = btrace_find_insn_by_number (&begin, btinfo, low);
403 error (_("Range out of bounds."));
405 found = btrace_find_insn_by_number (&end, btinfo, high);
408 /* Silently truncate the range. */
409 btrace_insn_end (&end, btinfo);
413 /* We want both begin and end to be inclusive. */
414 btrace_insn_next (&end, 1);
417 btrace_insn_history (uiout, &begin, &end, flags);
418 btrace_set_insn_history (btinfo, &begin, &end);
420 do_cleanups (uiout_cleanup);
423 /* The to_insn_history_from method of target record-btrace. */
426 record_btrace_insn_history_from (ULONGEST from, int size, int flags)
428 ULONGEST begin, end, context;
430 context = abs (size);
432 error (_("Bad record instruction-history-size."));
441 begin = from - context + 1;
446 end = from + context - 1;
448 /* Check for wrap-around. */
453 record_btrace_insn_history_range (begin, end, flags);
456 /* Print the instruction number range for a function call history line. */
459 btrace_call_history_insn_range (struct ui_out *uiout,
460 const struct btrace_function *bfun)
462 unsigned int begin, end, size;
464 size = VEC_length (btrace_insn_s, bfun->insn);
465 gdb_assert (size > 0);
467 begin = bfun->insn_offset;
468 end = begin + size - 1;
470 ui_out_field_uint (uiout, "insn begin", begin);
471 ui_out_text (uiout, ",");
472 ui_out_field_uint (uiout, "insn end", end);
475 /* Print the source line information for a function call history line. */
478 btrace_call_history_src_line (struct ui_out *uiout,
479 const struct btrace_function *bfun)
488 ui_out_field_string (uiout, "file",
489 symtab_to_filename_for_display (sym->symtab));
491 begin = bfun->lbegin;
497 ui_out_text (uiout, ":");
498 ui_out_field_int (uiout, "min line", begin);
503 ui_out_text (uiout, ",");
504 ui_out_field_int (uiout, "max line", end);
507 /* Disassemble a section of the recorded function trace. */
510 btrace_call_history (struct ui_out *uiout,
511 const struct btrace_thread_info *btinfo,
512 const struct btrace_call_iterator *begin,
513 const struct btrace_call_iterator *end,
514 enum record_print_flag flags)
516 struct btrace_call_iterator it;
518 DEBUG ("ftrace (0x%x): [%u; %u)", flags, btrace_call_number (begin),
519 btrace_call_number (end));
521 for (it = *begin; btrace_call_cmp (&it, end) < 0; btrace_call_next (&it, 1))
523 const struct btrace_function *bfun;
524 struct minimal_symbol *msym;
527 bfun = btrace_call_get (&it);
531 /* Print the function index. */
532 ui_out_field_uint (uiout, "index", bfun->number);
533 ui_out_text (uiout, "\t");
535 if ((flags & RECORD_PRINT_INDENT_CALLS) != 0)
537 int level = bfun->level + btinfo->level, i;
539 for (i = 0; i < level; ++i)
540 ui_out_text (uiout, " ");
544 ui_out_field_string (uiout, "function", SYMBOL_PRINT_NAME (sym));
545 else if (msym != NULL)
546 ui_out_field_string (uiout, "function", SYMBOL_PRINT_NAME (msym));
547 else if (!ui_out_is_mi_like_p (uiout))
548 ui_out_field_string (uiout, "function", "??");
550 if ((flags & RECORD_PRINT_INSN_RANGE) != 0)
552 ui_out_text (uiout, _("\tinst "));
553 btrace_call_history_insn_range (uiout, bfun);
556 if ((flags & RECORD_PRINT_SRC_LINE) != 0)
558 ui_out_text (uiout, _("\tat "));
559 btrace_call_history_src_line (uiout, bfun);
562 ui_out_text (uiout, "\n");
566 /* The to_call_history method of target record-btrace. */
569 record_btrace_call_history (int size, int flags)
571 struct btrace_thread_info *btinfo;
572 struct btrace_call_history *history;
573 struct btrace_call_iterator begin, end;
574 struct cleanup *uiout_cleanup;
575 struct ui_out *uiout;
576 unsigned int context, covered;
578 uiout = current_uiout;
579 uiout_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout,
581 context = abs (size);
583 error (_("Bad record function-call-history-size."));
585 btinfo = require_btrace ();
586 history = btinfo->call_history;
589 struct btrace_insn_iterator *replay;
591 DEBUG ("call-history (0x%x): %d", flags, size);
593 /* If we're replaying, we start at the replay position. Otherwise, we
594 start at the tail of the trace. */
595 replay = btinfo->replay;
598 begin.function = replay->function;
599 begin.btinfo = btinfo;
602 btrace_call_end (&begin, btinfo);
604 /* We start from here and expand in the requested direction. Then we
605 expand in the other direction, as well, to fill up any remaining
610 /* We want the current position covered, as well. */
611 covered = btrace_call_next (&end, 1);
612 covered += btrace_call_prev (&begin, context - covered);
613 covered += btrace_call_next (&end, context - covered);
617 covered = btrace_call_next (&end, context);
618 covered += btrace_call_prev (&begin, context- covered);
623 begin = history->begin;
626 DEBUG ("call-history (0x%x): %d, prev: [%u; %u)", flags, size,
627 btrace_call_number (&begin), btrace_call_number (&end));
632 covered = btrace_call_prev (&begin, context);
637 covered = btrace_call_next (&end, context);
642 btrace_call_history (uiout, btinfo, &begin, &end, flags);
646 printf_unfiltered (_("At the start of the branch trace record.\n"));
648 printf_unfiltered (_("At the end of the branch trace record.\n"));
651 btrace_set_call_history (btinfo, &begin, &end);
652 do_cleanups (uiout_cleanup);
655 /* The to_call_history_range method of target record-btrace. */
658 record_btrace_call_history_range (ULONGEST from, ULONGEST to, int flags)
660 struct btrace_thread_info *btinfo;
661 struct btrace_call_history *history;
662 struct btrace_call_iterator begin, end;
663 struct cleanup *uiout_cleanup;
664 struct ui_out *uiout;
665 unsigned int low, high;
668 uiout = current_uiout;
669 uiout_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout,
674 DEBUG ("call-history (0x%x): [%u; %u)", flags, low, high);
676 /* Check for wrap-arounds. */
677 if (low != from || high != to)
678 error (_("Bad range."));
681 error (_("Bad range."));
683 btinfo = require_btrace ();
685 found = btrace_find_call_by_number (&begin, btinfo, low);
687 error (_("Range out of bounds."));
689 found = btrace_find_call_by_number (&end, btinfo, high);
692 /* Silently truncate the range. */
693 btrace_call_end (&end, btinfo);
697 /* We want both begin and end to be inclusive. */
698 btrace_call_next (&end, 1);
701 btrace_call_history (uiout, btinfo, &begin, &end, flags);
702 btrace_set_call_history (btinfo, &begin, &end);
704 do_cleanups (uiout_cleanup);
707 /* The to_call_history_from method of target record-btrace. */
710 record_btrace_call_history_from (ULONGEST from, int size, int flags)
712 ULONGEST begin, end, context;
714 context = abs (size);
716 error (_("Bad record function-call-history-size."));
725 begin = from - context + 1;
730 end = from + context - 1;
732 /* Check for wrap-around. */
737 record_btrace_call_history_range (begin, end, flags);
740 /* The to_record_is_replaying method of target record-btrace. */
743 record_btrace_is_replaying (void)
745 struct thread_info *tp;
748 if (btrace_is_replaying (tp))
754 /* The to_fetch_registers method of target record-btrace. */
757 record_btrace_fetch_registers (struct target_ops *ops,
758 struct regcache *regcache, int regno)
760 struct btrace_insn_iterator *replay;
761 struct thread_info *tp;
763 tp = find_thread_ptid (inferior_ptid);
764 gdb_assert (tp != NULL);
766 replay = tp->btrace.replay;
769 const struct btrace_insn *insn;
770 struct gdbarch *gdbarch;
773 gdbarch = get_regcache_arch (regcache);
774 pcreg = gdbarch_pc_regnum (gdbarch);
778 /* We can only provide the PC register. */
779 if (regno >= 0 && regno != pcreg)
782 insn = btrace_insn_get (replay);
783 gdb_assert (insn != NULL);
785 regcache_raw_supply (regcache, regno, &insn->pc);
789 struct target_ops *t;
791 for (t = ops->beneath; t != NULL; t = t->beneath)
792 if (t->to_fetch_registers != NULL)
794 t->to_fetch_registers (t, regcache, regno);
800 /* The to_store_registers method of target record-btrace. */
803 record_btrace_store_registers (struct target_ops *ops,
804 struct regcache *regcache, int regno)
806 struct target_ops *t;
808 if (record_btrace_is_replaying ())
809 error (_("This record target does not allow writing registers."));
811 gdb_assert (may_write_registers != 0);
813 for (t = ops->beneath; t != NULL; t = t->beneath)
814 if (t->to_store_registers != NULL)
816 t->to_store_registers (t, regcache, regno);
823 /* The to_prepare_to_store method of target record-btrace. */
826 record_btrace_prepare_to_store (struct target_ops *ops,
827 struct regcache *regcache)
829 struct target_ops *t;
831 if (record_btrace_is_replaying ())
834 for (t = ops->beneath; t != NULL; t = t->beneath)
835 if (t->to_prepare_to_store != NULL)
837 t->to_prepare_to_store (t, regcache);
842 /* Initialize the record-btrace target ops. */
845 init_record_btrace_ops (void)
847 struct target_ops *ops;
849 ops = &record_btrace_ops;
850 ops->to_shortname = "record-btrace";
851 ops->to_longname = "Branch tracing target";
852 ops->to_doc = "Collect control-flow trace and provide the execution history.";
853 ops->to_open = record_btrace_open;
854 ops->to_close = record_btrace_close;
855 ops->to_detach = record_detach;
856 ops->to_disconnect = record_disconnect;
857 ops->to_mourn_inferior = record_mourn_inferior;
858 ops->to_kill = record_kill;
859 ops->to_create_inferior = find_default_create_inferior;
860 ops->to_stop_recording = record_btrace_stop_recording;
861 ops->to_info_record = record_btrace_info;
862 ops->to_insn_history = record_btrace_insn_history;
863 ops->to_insn_history_from = record_btrace_insn_history_from;
864 ops->to_insn_history_range = record_btrace_insn_history_range;
865 ops->to_call_history = record_btrace_call_history;
866 ops->to_call_history_from = record_btrace_call_history_from;
867 ops->to_call_history_range = record_btrace_call_history_range;
868 ops->to_record_is_replaying = record_btrace_is_replaying;
869 ops->to_fetch_registers = record_btrace_fetch_registers;
870 ops->to_store_registers = record_btrace_store_registers;
871 ops->to_prepare_to_store = record_btrace_prepare_to_store;
872 ops->to_stratum = record_stratum;
873 ops->to_magic = OPS_MAGIC;
876 /* Alias for "target record". */
879 cmd_record_btrace_start (char *args, int from_tty)
881 if (args != NULL && *args != 0)
882 error (_("Invalid argument."));
884 execute_command ("target record-btrace", from_tty);
887 void _initialize_record_btrace (void);
889 /* Initialize btrace commands. */
892 _initialize_record_btrace (void)
894 add_cmd ("btrace", class_obscure, cmd_record_btrace_start,
895 _("Start branch trace recording."),
897 add_alias_cmd ("b", "btrace", class_obscure, 1, &record_cmdlist);
899 init_record_btrace_ops ();
900 add_target (&record_btrace_ops);