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"
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
;
147 if (!target_has_execution
)
148 error (_("The program is not being run."));
150 if (!target_supports_btrace ())
151 error (_("Target does not support branch tracing."));
153 gdb_assert (record_btrace_thread_observer
== NULL
);
155 disable_chain
= make_cleanup (null_cleanup
, NULL
);
157 if (args
== NULL
|| *args
== 0 || number_is_in_list (args
, tp
->num
))
161 make_cleanup (record_btrace_disable_callback
, tp
);
164 record_btrace_auto_enable ();
166 push_target (&record_btrace_ops
);
168 observer_notify_record_changed (current_inferior (), 1);
170 discard_cleanups (disable_chain
);
173 /* The to_stop_recording method of target record-btrace. */
176 record_btrace_stop_recording (void)
178 struct thread_info
*tp
;
180 DEBUG ("stop recording");
182 record_btrace_auto_disable ();
185 if (tp
->btrace
.target
!= NULL
)
189 /* The to_close method of target record-btrace. */
192 record_btrace_close (void)
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 ();
198 /* We already stopped recording. */
201 /* The to_info_record method of target record-btrace. */
204 record_btrace_info (void)
206 struct btrace_thread_info
*btinfo
;
207 struct thread_info
*tp
;
208 unsigned int insts
, funcs
;
212 tp
= find_thread_ptid (inferior_ptid
);
214 error (_("No thread."));
218 btinfo
= &tp
->btrace
;
219 insts
= VEC_length (btrace_inst_s
, btinfo
->itrace
);
220 funcs
= VEC_length (btrace_func_s
, btinfo
->ftrace
);
222 printf_unfiltered (_("Recorded %u instructions in %u functions for thread "
223 "%d (%s).\n"), insts
, funcs
, tp
->num
,
224 target_pid_to_str (tp
->ptid
));
227 /* Print an unsigned int. */
230 ui_out_field_uint (struct ui_out
*uiout
, const char *fld
, unsigned int val
)
232 ui_out_field_fmt (uiout
, fld
, "%u", val
);
235 /* Disassemble a section of the recorded instruction trace. */
238 btrace_insn_history (struct btrace_thread_info
*btinfo
, struct ui_out
*uiout
,
239 unsigned int begin
, unsigned int end
, int flags
)
241 struct gdbarch
*gdbarch
;
242 struct btrace_inst
*inst
;
245 DEBUG ("itrace (0x%x): [%u; %u[", flags
, begin
, end
);
247 gdbarch
= target_gdbarch ();
249 for (idx
= begin
; VEC_iterate (btrace_inst_s
, btinfo
->itrace
, idx
, inst
)
252 /* Print the instruction index. */
253 ui_out_field_uint (uiout
, "index", idx
);
254 ui_out_text (uiout
, "\t");
256 /* Disassembly with '/m' flag may not produce the expected result.
258 gdb_disassembly (gdbarch
, uiout
, NULL
, flags
, 1, inst
->pc
, inst
->pc
+ 1);
262 /* The to_insn_history method of target record-btrace. */
265 record_btrace_insn_history (int size
, int flags
)
267 struct btrace_thread_info
*btinfo
;
268 struct cleanup
*uiout_cleanup
;
269 struct ui_out
*uiout
;
270 unsigned int context
, last
, begin
, end
;
272 uiout
= current_uiout
;
273 uiout_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
,
275 btinfo
= require_btrace ();
276 last
= VEC_length (btrace_inst_s
, btinfo
->itrace
);
278 context
= abs (size
);
279 begin
= btinfo
->insn_iterator
.begin
;
280 end
= btinfo
->insn_iterator
.end
;
282 DEBUG ("insn-history (0x%x): %d, prev: [%u; %u[", flags
, size
, begin
, end
);
285 error (_("Bad record instruction-history-size."));
287 /* We start at the end. */
290 /* Truncate the context, if necessary. */
291 context
= min (context
, last
);
294 begin
= end
- context
;
300 printf_unfiltered (_("At the start of the branch trace record.\n"));
302 btinfo
->insn_iterator
.end
= 0;
306 /* Truncate the context, if necessary. */
307 context
= min (context
, begin
);
316 printf_unfiltered (_("At the end of the branch trace record.\n"));
318 btinfo
->insn_iterator
.begin
= last
;
322 /* Truncate the context, if necessary. */
323 context
= min (context
, last
- end
);
329 btrace_insn_history (btinfo
, uiout
, begin
, end
, flags
);
331 btinfo
->insn_iterator
.begin
= begin
;
332 btinfo
->insn_iterator
.end
= end
;
334 do_cleanups (uiout_cleanup
);
337 /* The to_insn_history_range method of target record-btrace. */
340 record_btrace_insn_history_range (ULONGEST from
, ULONGEST to
, int flags
)
342 struct btrace_thread_info
*btinfo
;
343 struct cleanup
*uiout_cleanup
;
344 struct ui_out
*uiout
;
345 unsigned int last
, begin
, end
;
347 uiout
= current_uiout
;
348 uiout_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
,
350 btinfo
= require_btrace ();
351 last
= VEC_length (btrace_inst_s
, btinfo
->itrace
);
353 begin
= (unsigned int) from
;
354 end
= (unsigned int) to
;
356 DEBUG ("insn-history (0x%x): [%u; %u[", flags
, begin
, end
);
358 /* Check for wrap-arounds. */
359 if (begin
!= from
|| end
!= to
)
360 error (_("Bad range."));
363 error (_("Bad range."));
366 error (_("Range out of bounds."));
368 /* Truncate the range, if necessary. */
372 btrace_insn_history (btinfo
, uiout
, begin
, end
, flags
);
374 btinfo
->insn_iterator
.begin
= begin
;
375 btinfo
->insn_iterator
.end
= end
;
377 do_cleanups (uiout_cleanup
);
380 /* The to_insn_history_from method of target record-btrace. */
383 record_btrace_insn_history_from (ULONGEST from
, int size
, int flags
)
385 ULONGEST begin
, end
, context
;
387 context
= abs (size
);
396 begin
= from
- context
;
401 end
= from
+ context
;
403 /* Check for wrap-around. */
408 record_btrace_insn_history_range (begin
, end
, flags
);
411 /* Print the instruction number range for a function call history line. */
414 btrace_func_history_insn_range (struct ui_out
*uiout
, struct btrace_func
*bfun
)
416 ui_out_field_uint (uiout
, "insn begin", bfun
->ibegin
);
418 if (bfun
->ibegin
== bfun
->iend
)
421 ui_out_text (uiout
, "-");
422 ui_out_field_uint (uiout
, "insn end", bfun
->iend
);
425 /* Print the source line information for a function call history line. */
428 btrace_func_history_src_line (struct ui_out
*uiout
, struct btrace_func
*bfun
)
436 ui_out_field_string (uiout
, "file",
437 symtab_to_filename_for_display (sym
->symtab
));
442 ui_out_text (uiout
, ":");
443 ui_out_field_int (uiout
, "min line", bfun
->lbegin
);
445 if (bfun
->lend
== bfun
->lbegin
)
448 ui_out_text (uiout
, "-");
449 ui_out_field_int (uiout
, "max line", bfun
->lend
);
452 /* Disassemble a section of the recorded function trace. */
455 btrace_func_history (struct btrace_thread_info
*btinfo
, struct ui_out
*uiout
,
456 unsigned int begin
, unsigned int end
,
457 enum record_print_flag flags
)
459 struct btrace_func
*bfun
;
462 DEBUG ("ftrace (0x%x): [%u; %u[", flags
, begin
, end
);
464 for (idx
= begin
; VEC_iterate (btrace_func_s
, btinfo
->ftrace
, idx
, bfun
)
467 /* Print the function index. */
468 ui_out_field_uint (uiout
, "index", idx
);
469 ui_out_text (uiout
, "\t");
471 if ((flags
& RECORD_PRINT_INSN_RANGE
) != 0)
473 btrace_func_history_insn_range (uiout
, bfun
);
474 ui_out_text (uiout
, "\t");
477 if ((flags
& RECORD_PRINT_SRC_LINE
) != 0)
479 btrace_func_history_src_line (uiout
, bfun
);
480 ui_out_text (uiout
, "\t");
483 if (bfun
->sym
!= NULL
)
484 ui_out_field_string (uiout
, "function", SYMBOL_PRINT_NAME (bfun
->sym
));
485 else if (bfun
->msym
!= NULL
)
486 ui_out_field_string (uiout
, "function", SYMBOL_PRINT_NAME (bfun
->msym
));
487 ui_out_text (uiout
, "\n");
491 /* The to_call_history method of target record-btrace. */
494 record_btrace_call_history (int size
, int flags
)
496 struct btrace_thread_info
*btinfo
;
497 struct cleanup
*uiout_cleanup
;
498 struct ui_out
*uiout
;
499 unsigned int context
, last
, begin
, end
;
501 uiout
= current_uiout
;
502 uiout_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
,
504 btinfo
= require_btrace ();
505 last
= VEC_length (btrace_func_s
, btinfo
->ftrace
);
507 context
= abs (size
);
508 begin
= btinfo
->func_iterator
.begin
;
509 end
= btinfo
->func_iterator
.end
;
511 DEBUG ("func-history (0x%x): %d, prev: [%u; %u[", flags
, size
, begin
, end
);
514 error (_("Bad record function-call-history-size."));
516 /* We start at the end. */
519 /* Truncate the context, if necessary. */
520 context
= min (context
, last
);
523 begin
= end
- context
;
529 printf_unfiltered (_("At the start of the branch trace record.\n"));
531 btinfo
->func_iterator
.end
= 0;
535 /* Truncate the context, if necessary. */
536 context
= min (context
, begin
);
545 printf_unfiltered (_("At the end of the branch trace record.\n"));
547 btinfo
->func_iterator
.begin
= last
;
551 /* Truncate the context, if necessary. */
552 context
= min (context
, last
- end
);
558 btrace_func_history (btinfo
, uiout
, begin
, end
, flags
);
560 btinfo
->func_iterator
.begin
= begin
;
561 btinfo
->func_iterator
.end
= end
;
563 do_cleanups (uiout_cleanup
);
566 /* The to_call_history_range method of target record-btrace. */
569 record_btrace_call_history_range (ULONGEST from
, ULONGEST to
, int flags
)
571 struct btrace_thread_info
*btinfo
;
572 struct cleanup
*uiout_cleanup
;
573 struct ui_out
*uiout
;
574 unsigned int last
, begin
, end
;
576 uiout
= current_uiout
;
577 uiout_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
,
579 btinfo
= require_btrace ();
580 last
= VEC_length (btrace_func_s
, btinfo
->ftrace
);
582 begin
= (unsigned int) from
;
583 end
= (unsigned int) to
;
585 DEBUG ("func-history (0x%x): [%u; %u[", flags
, begin
, end
);
587 /* Check for wrap-arounds. */
588 if (begin
!= from
|| end
!= to
)
589 error (_("Bad range."));
592 error (_("Bad range."));
595 error (_("Range out of bounds."));
597 /* Truncate the range, if necessary. */
601 btrace_func_history (btinfo
, uiout
, begin
, end
, flags
);
603 btinfo
->func_iterator
.begin
= begin
;
604 btinfo
->func_iterator
.end
= end
;
606 do_cleanups (uiout_cleanup
);
609 /* The to_call_history_from method of target record-btrace. */
612 record_btrace_call_history_from (ULONGEST from
, int size
, int flags
)
614 ULONGEST begin
, end
, context
;
616 context
= abs (size
);
625 begin
= from
- context
;
630 end
= from
+ context
;
632 /* Check for wrap-around. */
637 record_btrace_call_history_range (begin
, end
, flags
);
640 /* Initialize the record-btrace target ops. */
643 init_record_btrace_ops (void)
645 struct target_ops
*ops
;
647 ops
= &record_btrace_ops
;
648 ops
->to_shortname
= "record-btrace";
649 ops
->to_longname
= "Branch tracing target";
650 ops
->to_doc
= "Collect control-flow trace and provide the execution history.";
651 ops
->to_open
= record_btrace_open
;
652 ops
->to_close
= record_btrace_close
;
653 ops
->to_detach
= record_detach
;
654 ops
->to_disconnect
= record_disconnect
;
655 ops
->to_mourn_inferior
= record_mourn_inferior
;
656 ops
->to_kill
= record_kill
;
657 ops
->to_create_inferior
= find_default_create_inferior
;
658 ops
->to_stop_recording
= record_btrace_stop_recording
;
659 ops
->to_info_record
= record_btrace_info
;
660 ops
->to_insn_history
= record_btrace_insn_history
;
661 ops
->to_insn_history_from
= record_btrace_insn_history_from
;
662 ops
->to_insn_history_range
= record_btrace_insn_history_range
;
663 ops
->to_call_history
= record_btrace_call_history
;
664 ops
->to_call_history_from
= record_btrace_call_history_from
;
665 ops
->to_call_history_range
= record_btrace_call_history_range
;
666 ops
->to_stratum
= record_stratum
;
667 ops
->to_magic
= OPS_MAGIC
;
670 /* Alias for "target record". */
673 cmd_record_btrace_start (char *args
, int from_tty
)
675 if (args
!= NULL
&& *args
!= 0)
676 error (_("Invalid argument."));
678 execute_command ("target record-btrace", from_tty
);
681 void _initialize_record_btrace (void);
683 /* Initialize btrace commands. */
686 _initialize_record_btrace (void)
688 add_cmd ("btrace", class_obscure
, cmd_record_btrace_start
,
689 _("Start branch trace recording."),
691 add_alias_cmd ("b", "btrace", class_obscure
, 1, &record_cmdlist
);
693 init_record_btrace_ops ();
694 add_target (&record_btrace_ops
);