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 (int quitting
)
195 /* We already stopped recording. */
198 /* The to_info_record method of target record-btrace. */
201 record_btrace_info (void)
203 struct btrace_thread_info
*btinfo
;
204 struct thread_info
*tp
;
205 unsigned int insts
, funcs
;
209 tp
= find_thread_ptid (inferior_ptid
);
211 error (_("No thread."));
215 btinfo
= &tp
->btrace
;
216 insts
= VEC_length (btrace_inst_s
, btinfo
->itrace
);
217 funcs
= VEC_length (btrace_func_s
, btinfo
->ftrace
);
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
));
224 /* Print an unsigned int. */
227 ui_out_field_uint (struct ui_out
*uiout
, const char *fld
, unsigned int val
)
229 ui_out_field_fmt (uiout
, fld
, "%u", val
);
232 /* Disassemble a section of the recorded instruction trace. */
235 btrace_insn_history (struct btrace_thread_info
*btinfo
, struct ui_out
*uiout
,
236 unsigned int begin
, unsigned int end
, int flags
)
238 struct gdbarch
*gdbarch
;
239 struct btrace_inst
*inst
;
242 DEBUG ("itrace (0x%x): [%u; %u[", flags
, begin
, end
);
244 gdbarch
= target_gdbarch ();
246 for (idx
= begin
; VEC_iterate (btrace_inst_s
, btinfo
->itrace
, idx
, inst
)
249 /* Print the instruction index. */
250 ui_out_field_uint (uiout
, "index", idx
);
251 ui_out_text (uiout
, "\t");
253 /* Disassembly with '/m' flag may not produce the expected result.
255 gdb_disassembly (gdbarch
, uiout
, NULL
, flags
, 1, inst
->pc
, inst
->pc
+ 1);
259 /* The to_insn_history method of target record-btrace. */
262 record_btrace_insn_history (int size
, int flags
)
264 struct btrace_thread_info
*btinfo
;
265 struct cleanup
*uiout_cleanup
;
266 struct ui_out
*uiout
;
267 unsigned int context
, last
, begin
, end
;
269 uiout
= current_uiout
;
270 uiout_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
,
272 btinfo
= require_btrace ();
273 last
= VEC_length (btrace_inst_s
, btinfo
->itrace
);
275 context
= abs (size
);
276 begin
= btinfo
->insn_iterator
.begin
;
277 end
= btinfo
->insn_iterator
.end
;
279 DEBUG ("insn-history (0x%x): %d, prev: [%u; %u[", flags
, size
, begin
, end
);
282 error (_("Bad record instruction-history-size."));
284 /* We start at the end. */
287 /* Truncate the context, if necessary. */
288 context
= min (context
, last
);
291 begin
= end
- context
;
297 printf_unfiltered (_("At the start of the branch trace record.\n"));
299 btinfo
->insn_iterator
.end
= 0;
303 /* Truncate the context, if necessary. */
304 context
= min (context
, begin
);
313 printf_unfiltered (_("At the end of the branch trace record.\n"));
315 btinfo
->insn_iterator
.begin
= last
;
319 /* Truncate the context, if necessary. */
320 context
= min (context
, last
- end
);
326 btrace_insn_history (btinfo
, uiout
, begin
, end
, flags
);
328 btinfo
->insn_iterator
.begin
= begin
;
329 btinfo
->insn_iterator
.end
= end
;
331 do_cleanups (uiout_cleanup
);
334 /* The to_insn_history_range method of target record-btrace. */
337 record_btrace_insn_history_range (ULONGEST from
, ULONGEST to
, int flags
)
339 struct btrace_thread_info
*btinfo
;
340 struct cleanup
*uiout_cleanup
;
341 struct ui_out
*uiout
;
342 unsigned int last
, begin
, end
;
344 uiout
= current_uiout
;
345 uiout_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
,
347 btinfo
= require_btrace ();
348 last
= VEC_length (btrace_inst_s
, btinfo
->itrace
);
350 begin
= (unsigned int) from
;
351 end
= (unsigned int) to
;
353 DEBUG ("insn-history (0x%x): [%u; %u[", flags
, begin
, end
);
355 /* Check for wrap-arounds. */
356 if (begin
!= from
|| end
!= to
)
357 error (_("Bad range."));
360 error (_("Bad range."));
363 error (_("Range out of bounds."));
365 /* Truncate the range, if necessary. */
369 btrace_insn_history (btinfo
, uiout
, begin
, end
, flags
);
371 btinfo
->insn_iterator
.begin
= begin
;
372 btinfo
->insn_iterator
.end
= end
;
374 do_cleanups (uiout_cleanup
);
377 /* The to_insn_history_from method of target record-btrace. */
380 record_btrace_insn_history_from (ULONGEST from
, int size
, int flags
)
382 ULONGEST begin
, end
, context
;
384 context
= abs (size
);
393 begin
= from
- context
;
398 end
= from
+ context
;
400 /* Check for wrap-around. */
405 record_btrace_insn_history_range (begin
, end
, flags
);
408 /* Print the instruction number range for a function call history line. */
411 btrace_func_history_insn_range (struct ui_out
*uiout
, struct btrace_func
*bfun
)
413 ui_out_field_uint (uiout
, "insn begin", bfun
->ibegin
);
415 if (bfun
->ibegin
== bfun
->iend
)
418 ui_out_text (uiout
, "-");
419 ui_out_field_uint (uiout
, "insn end", bfun
->iend
);
422 /* Print the source line information for a function call history line. */
425 btrace_func_history_src_line (struct ui_out
*uiout
, struct btrace_func
*bfun
)
433 ui_out_field_string (uiout
, "file",
434 symtab_to_filename_for_display (sym
->symtab
));
439 ui_out_text (uiout
, ":");
440 ui_out_field_int (uiout
, "min line", bfun
->lbegin
);
442 if (bfun
->lend
== bfun
->lbegin
)
445 ui_out_text (uiout
, "-");
446 ui_out_field_int (uiout
, "max line", bfun
->lend
);
449 /* Disassemble a section of the recorded function trace. */
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
)
456 struct btrace_func
*bfun
;
459 DEBUG ("ftrace (0x%x): [%u; %u[", flags
, begin
, end
);
461 for (idx
= begin
; VEC_iterate (btrace_func_s
, btinfo
->ftrace
, idx
, bfun
)
464 /* Print the function index. */
465 ui_out_field_uint (uiout
, "index", idx
);
466 ui_out_text (uiout
, "\t");
468 if ((flags
& record_print_insn_range
) != 0)
470 btrace_func_history_insn_range (uiout
, bfun
);
471 ui_out_text (uiout
, "\t");
474 if ((flags
& record_print_src_line
) != 0)
476 btrace_func_history_src_line (uiout
, bfun
);
477 ui_out_text (uiout
, "\t");
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");
488 /* The to_call_history method of target record-btrace. */
491 record_btrace_call_history (int size
, int flags
)
493 struct btrace_thread_info
*btinfo
;
494 struct cleanup
*uiout_cleanup
;
495 struct ui_out
*uiout
;
496 unsigned int context
, last
, begin
, end
;
498 uiout
= current_uiout
;
499 uiout_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
,
501 btinfo
= require_btrace ();
502 last
= VEC_length (btrace_func_s
, btinfo
->ftrace
);
504 context
= abs (size
);
505 begin
= btinfo
->func_iterator
.begin
;
506 end
= btinfo
->func_iterator
.end
;
508 DEBUG ("func-history (0x%x): %d, prev: [%u; %u[", flags
, size
, begin
, end
);
511 error (_("Bad record function-call-history-size."));
513 /* We start at the end. */
516 /* Truncate the context, if necessary. */
517 context
= min (context
, last
);
520 begin
= end
- context
;
526 printf_unfiltered (_("At the start of the branch trace record.\n"));
528 btinfo
->func_iterator
.end
= 0;
532 /* Truncate the context, if necessary. */
533 context
= min (context
, begin
);
542 printf_unfiltered (_("At the end of the branch trace record.\n"));
544 btinfo
->func_iterator
.begin
= last
;
548 /* Truncate the context, if necessary. */
549 context
= min (context
, last
- end
);
555 btrace_func_history (btinfo
, uiout
, begin
, end
, flags
);
557 btinfo
->func_iterator
.begin
= begin
;
558 btinfo
->func_iterator
.end
= end
;
560 do_cleanups (uiout_cleanup
);
563 /* The to_call_history_range method of target record-btrace. */
566 record_btrace_call_history_range (ULONGEST from
, ULONGEST to
, int flags
)
568 struct btrace_thread_info
*btinfo
;
569 struct cleanup
*uiout_cleanup
;
570 struct ui_out
*uiout
;
571 unsigned int last
, begin
, end
;
573 uiout
= current_uiout
;
574 uiout_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
,
576 btinfo
= require_btrace ();
577 last
= VEC_length (btrace_func_s
, btinfo
->ftrace
);
579 begin
= (unsigned int) from
;
580 end
= (unsigned int) to
;
582 DEBUG ("func-history (0x%x): [%u; %u[", flags
, begin
, end
);
584 /* Check for wrap-arounds. */
585 if (begin
!= from
|| end
!= to
)
586 error (_("Bad range."));
589 error (_("Bad range."));
592 error (_("Range out of bounds."));
594 /* Truncate the range, if necessary. */
598 btrace_func_history (btinfo
, uiout
, begin
, end
, flags
);
600 btinfo
->func_iterator
.begin
= begin
;
601 btinfo
->func_iterator
.end
= end
;
603 do_cleanups (uiout_cleanup
);
606 /* The to_call_history_from method of target record-btrace. */
609 record_btrace_call_history_from (ULONGEST from
, int size
, int flags
)
611 ULONGEST begin
, end
, context
;
613 context
= abs (size
);
622 begin
= from
- context
;
627 end
= from
+ context
;
629 /* Check for wrap-around. */
634 record_btrace_call_history_range (begin
, end
, flags
);
637 /* Initialize the record-btrace target ops. */
640 init_record_btrace_ops (void)
642 struct target_ops
*ops
;
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
;
667 /* Alias for "target record". */
670 cmd_record_btrace_start (char *args
, int from_tty
)
672 if (args
!= NULL
&& *args
!= 0)
673 error (_("Invalid argument."));
675 execute_command ("target record-btrace", from_tty
);
678 void _initialize_record_btrace (void);
680 /* Initialize btrace commands. */
683 _initialize_record_btrace (void)
685 add_cmd ("btrace", class_obscure
, cmd_record_btrace_start
,
686 _("Start branch trace recording."),
688 add_alias_cmd ("b", "btrace", class_obscure
, 1, &record_cmdlist
);
690 init_record_btrace_ops ();
691 add_target (&record_btrace_ops
);