1 /* Branch trace support for GDB, the GNU debugger.
3 Copyright (C) 2013 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"
36 /* The target_ops of record-btrace. */
37 static struct target_ops record_btrace_ops;
39 /* A new thread observer enabling branch tracing for the new thread. */
40 static struct observer *record_btrace_thread_observer;
42 /* Print a record-btrace debug message. Use do ... while (0) to avoid
43 ambiguities when used in if statements. */
45 #define DEBUG(msg, args...) \
48 if (record_debug != 0) \
49 fprintf_unfiltered (gdb_stdlog, \
50 "[record-btrace] " msg "\n", ##args); \
55 /* Update the branch trace for the current thread and return a pointer to its
56 branch trace information struct.
58 Throws an error if there is no thread or no trace. This function never
61 static struct btrace_thread_info *
64 struct thread_info *tp;
65 struct btrace_thread_info *btinfo;
69 tp = find_thread_ptid (inferior_ptid);
71 error (_("No thread."));
77 if (VEC_empty (btrace_inst_s, btinfo->itrace))
78 error (_("No trace."));
83 /* Enable branch tracing for one thread. Warn on errors. */
86 record_btrace_enable_warn (struct thread_info *tp)
88 volatile struct gdb_exception error;
90 TRY_CATCH (error, RETURN_MASK_ERROR)
93 if (error.message != NULL)
94 warning ("%s", error.message);
97 /* Callback function to disable branch tracing for one thread. */
100 record_btrace_disable_callback (void *arg)
102 struct thread_info *tp;
109 /* Enable automatic tracing of new threads. */
112 record_btrace_auto_enable (void)
114 DEBUG ("attach thread observer");
116 record_btrace_thread_observer
117 = observer_attach_new_thread (record_btrace_enable_warn);
120 /* Disable automatic tracing of new threads. */
123 record_btrace_auto_disable (void)
125 /* The observer may have been detached, already. */
126 if (record_btrace_thread_observer == NULL)
129 DEBUG ("detach thread observer");
131 observer_detach_new_thread (record_btrace_thread_observer);
132 record_btrace_thread_observer = NULL;
135 /* The to_open method of target record-btrace. */
138 record_btrace_open (char *args, int from_tty)
140 struct cleanup *disable_chain;
141 struct thread_info *tp;
146 error (_("The process is already being recorded."));
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 insts, funcs;
213 tp = find_thread_ptid (inferior_ptid);
215 error (_("No thread."));
219 btinfo = &tp->btrace;
220 insts = VEC_length (btrace_inst_s, btinfo->itrace);
221 funcs = VEC_length (btrace_func_s, btinfo->ftrace);
223 printf_unfiltered (_("Recorded %u instructions in %u functions for thread "
224 "%d (%s).\n"), insts, funcs, tp->num,
225 target_pid_to_str (tp->ptid));
228 /* Print an unsigned int. */
231 ui_out_field_uint (struct ui_out *uiout, const char *fld, unsigned int val)
233 ui_out_field_fmt (uiout, fld, "%u", val);
236 /* Disassemble a section of the recorded instruction trace. */
239 btrace_insn_history (struct btrace_thread_info *btinfo, struct ui_out *uiout,
240 unsigned int begin, unsigned int end, int flags)
242 struct gdbarch *gdbarch;
243 struct btrace_inst *inst;
246 DEBUG ("itrace (0x%x): [%u; %u[", flags, begin, end);
248 gdbarch = target_gdbarch ();
250 for (idx = begin; VEC_iterate (btrace_inst_s, btinfo->itrace, idx, inst)
253 /* Print the instruction index. */
254 ui_out_field_uint (uiout, "index", idx);
255 ui_out_text (uiout, "\t");
257 /* Disassembly with '/m' flag may not produce the expected result.
259 gdb_disassembly (gdbarch, uiout, NULL, flags, 1, inst->pc, inst->pc + 1);
263 /* The to_insn_history method of target record-btrace. */
266 record_btrace_insn_history (int size, int flags)
268 struct btrace_thread_info *btinfo;
269 struct cleanup *uiout_cleanup;
270 struct ui_out *uiout;
271 unsigned int context, last, begin, end;
273 uiout = current_uiout;
274 uiout_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout,
276 btinfo = require_btrace ();
277 last = VEC_length (btrace_inst_s, btinfo->itrace);
279 context = abs (size);
280 begin = btinfo->insn_iterator.begin;
281 end = btinfo->insn_iterator.end;
283 DEBUG ("insn-history (0x%x): %d, prev: [%u; %u[", flags, size, begin, end);
286 error (_("Bad record instruction-history-size."));
288 /* We start at the end. */
291 /* Truncate the context, if necessary. */
292 context = min (context, last);
295 begin = end - context;
301 printf_unfiltered (_("At the start of the branch trace record.\n"));
303 btinfo->insn_iterator.end = 0;
307 /* Truncate the context, if necessary. */
308 context = min (context, begin);
317 printf_unfiltered (_("At the end of the branch trace record.\n"));
319 btinfo->insn_iterator.begin = last;
323 /* Truncate the context, if necessary. */
324 context = min (context, last - end);
330 btrace_insn_history (btinfo, uiout, begin, end, flags);
332 btinfo->insn_iterator.begin = begin;
333 btinfo->insn_iterator.end = end;
335 do_cleanups (uiout_cleanup);
338 /* The to_insn_history_range method of target record-btrace. */
341 record_btrace_insn_history_range (ULONGEST from, ULONGEST to, int flags)
343 struct btrace_thread_info *btinfo;
344 struct cleanup *uiout_cleanup;
345 struct ui_out *uiout;
346 unsigned int last, begin, end;
348 uiout = current_uiout;
349 uiout_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout,
351 btinfo = require_btrace ();
352 last = VEC_length (btrace_inst_s, btinfo->itrace);
354 begin = (unsigned int) from;
355 end = (unsigned int) to;
357 DEBUG ("insn-history (0x%x): [%u; %u[", flags, begin, end);
359 /* Check for wrap-arounds. */
360 if (begin != from || end != to)
361 error (_("Bad range."));
364 error (_("Bad range."));
367 error (_("Range out of bounds."));
369 /* Truncate the range, if necessary. */
373 btrace_insn_history (btinfo, uiout, begin, end, flags);
375 btinfo->insn_iterator.begin = begin;
376 btinfo->insn_iterator.end = end;
378 do_cleanups (uiout_cleanup);
381 /* The to_insn_history_from method of target record-btrace. */
384 record_btrace_insn_history_from (ULONGEST from, int size, int flags)
386 ULONGEST begin, end, context;
388 context = abs (size);
397 begin = from - context;
402 end = from + context;
404 /* Check for wrap-around. */
409 record_btrace_insn_history_range (begin, end, flags);
412 /* Print the instruction number range for a function call history line. */
415 btrace_func_history_insn_range (struct ui_out *uiout, struct btrace_func *bfun)
417 ui_out_field_uint (uiout, "insn begin", bfun->ibegin);
419 if (bfun->ibegin == bfun->iend)
422 ui_out_text (uiout, "-");
423 ui_out_field_uint (uiout, "insn end", bfun->iend);
426 /* Print the source line information for a function call history line. */
429 btrace_func_history_src_line (struct ui_out *uiout, struct btrace_func *bfun)
437 ui_out_field_string (uiout, "file",
438 symtab_to_filename_for_display (sym->symtab));
443 ui_out_text (uiout, ":");
444 ui_out_field_int (uiout, "min line", bfun->lbegin);
446 if (bfun->lend == bfun->lbegin)
449 ui_out_text (uiout, "-");
450 ui_out_field_int (uiout, "max line", bfun->lend);
453 /* Disassemble a section of the recorded function trace. */
456 btrace_func_history (struct btrace_thread_info *btinfo, struct ui_out *uiout,
457 unsigned int begin, unsigned int end,
458 enum record_print_flag flags)
460 struct btrace_func *bfun;
463 DEBUG ("ftrace (0x%x): [%u; %u[", flags, begin, end);
465 for (idx = begin; VEC_iterate (btrace_func_s, btinfo->ftrace, idx, bfun)
468 /* Print the function index. */
469 ui_out_field_uint (uiout, "index", idx);
470 ui_out_text (uiout, "\t");
472 if ((flags & record_print_insn_range) != 0)
474 btrace_func_history_insn_range (uiout, bfun);
475 ui_out_text (uiout, "\t");
478 if ((flags & record_print_src_line) != 0)
480 btrace_func_history_src_line (uiout, bfun);
481 ui_out_text (uiout, "\t");
484 if (bfun->sym != NULL)
485 ui_out_field_string (uiout, "function", SYMBOL_PRINT_NAME (bfun->sym));
486 else if (bfun->msym != NULL)
487 ui_out_field_string (uiout, "function", SYMBOL_PRINT_NAME (bfun->msym));
488 ui_out_text (uiout, "\n");
492 /* The to_call_history method of target record-btrace. */
495 record_btrace_call_history (int size, int flags)
497 struct btrace_thread_info *btinfo;
498 struct cleanup *uiout_cleanup;
499 struct ui_out *uiout;
500 unsigned int context, last, begin, end;
502 uiout = current_uiout;
503 uiout_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout,
505 btinfo = require_btrace ();
506 last = VEC_length (btrace_func_s, btinfo->ftrace);
508 context = abs (size);
509 begin = btinfo->func_iterator.begin;
510 end = btinfo->func_iterator.end;
512 DEBUG ("func-history (0x%x): %d, prev: [%u; %u[", flags, size, begin, end);
515 error (_("Bad record function-call-history-size."));
517 /* We start at the end. */
520 /* Truncate the context, if necessary. */
521 context = min (context, last);
524 begin = end - context;
530 printf_unfiltered (_("At the start of the branch trace record.\n"));
532 btinfo->func_iterator.end = 0;
536 /* Truncate the context, if necessary. */
537 context = min (context, begin);
546 printf_unfiltered (_("At the end of the branch trace record.\n"));
548 btinfo->func_iterator.begin = last;
552 /* Truncate the context, if necessary. */
553 context = min (context, last - end);
559 btrace_func_history (btinfo, uiout, begin, end, flags);
561 btinfo->func_iterator.begin = begin;
562 btinfo->func_iterator.end = end;
564 do_cleanups (uiout_cleanup);
567 /* The to_call_history_range method of target record-btrace. */
570 record_btrace_call_history_range (ULONGEST from, ULONGEST to, int flags)
572 struct btrace_thread_info *btinfo;
573 struct cleanup *uiout_cleanup;
574 struct ui_out *uiout;
575 unsigned int last, begin, end;
577 uiout = current_uiout;
578 uiout_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout,
580 btinfo = require_btrace ();
581 last = VEC_length (btrace_func_s, btinfo->ftrace);
583 begin = (unsigned int) from;
584 end = (unsigned int) to;
586 DEBUG ("func-history (0x%x): [%u; %u[", flags, begin, end);
588 /* Check for wrap-arounds. */
589 if (begin != from || end != to)
590 error (_("Bad range."));
593 error (_("Bad range."));
596 error (_("Range out of bounds."));
598 /* Truncate the range, if necessary. */
602 btrace_func_history (btinfo, uiout, begin, end, flags);
604 btinfo->func_iterator.begin = begin;
605 btinfo->func_iterator.end = end;
607 do_cleanups (uiout_cleanup);
610 /* The to_call_history_from method of target record-btrace. */
613 record_btrace_call_history_from (ULONGEST from, int size, int flags)
615 ULONGEST begin, end, context;
617 context = abs (size);
626 begin = from - context;
631 end = from + context;
633 /* Check for wrap-around. */
638 record_btrace_call_history_range (begin, end, flags);
641 /* Initialize the record-btrace target ops. */
644 init_record_btrace_ops (void)
646 struct target_ops *ops;
648 ops = &record_btrace_ops;
649 ops->to_shortname = "record-btrace";
650 ops->to_longname = "Branch tracing target";
651 ops->to_doc = "Collect control-flow trace and provide the execution history.";
652 ops->to_open = record_btrace_open;
653 ops->to_close = record_btrace_close;
654 ops->to_detach = record_detach;
655 ops->to_disconnect = record_disconnect;
656 ops->to_mourn_inferior = record_mourn_inferior;
657 ops->to_kill = record_kill;
658 ops->to_create_inferior = find_default_create_inferior;
659 ops->to_stop_recording = record_btrace_stop_recording;
660 ops->to_info_record = record_btrace_info;
661 ops->to_insn_history = record_btrace_insn_history;
662 ops->to_insn_history_from = record_btrace_insn_history_from;
663 ops->to_insn_history_range = record_btrace_insn_history_range;
664 ops->to_call_history = record_btrace_call_history;
665 ops->to_call_history_from = record_btrace_call_history_from;
666 ops->to_call_history_range = record_btrace_call_history_range;
667 ops->to_stratum = record_stratum;
668 ops->to_magic = OPS_MAGIC;
671 /* Alias for "target record". */
674 cmd_record_btrace_start (char *args, int from_tty)
676 if (args != NULL && *args != 0)
677 error (_("Invalid argument."));
679 execute_command ("target record-btrace", from_tty);
682 void _initialize_record_btrace (void);
684 /* Initialize btrace commands. */
687 _initialize_record_btrace (void)
689 add_cmd ("btrace", class_obscure, cmd_record_btrace_start,
690 _("Start branch trace recording."),
692 add_alias_cmd ("b", "btrace", class_obscure, 1, &record_cmdlist);
694 init_record_btrace_ops ();
695 add_target (&record_btrace_ops);