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 (btinfo
->begin
== NULL
)
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 insns
, calls
;
212 tp
= find_thread_ptid (inferior_ptid
);
214 error (_("No thread."));
221 btinfo
= &tp
->btrace
;
222 if (btinfo
->begin
!= NULL
)
224 struct btrace_call_iterator call
;
225 struct btrace_insn_iterator insn
;
227 btrace_call_end (&call
, btinfo
);
228 btrace_call_prev (&call
, 1);
229 calls
= btrace_call_number (&call
);
231 btrace_insn_end (&insn
, btinfo
);
232 btrace_insn_prev (&insn
, 1);
233 insns
= btrace_insn_number (&insn
);
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
));
241 /* Print an unsigned int. */
244 ui_out_field_uint (struct ui_out
*uiout
, const char *fld
, unsigned int val
)
246 ui_out_field_fmt (uiout
, fld
, "%u", val
);
249 /* Disassemble a section of the recorded instruction trace. */
252 btrace_insn_history (struct ui_out
*uiout
,
253 const struct btrace_insn_iterator
*begin
,
254 const struct btrace_insn_iterator
*end
, int flags
)
256 struct gdbarch
*gdbarch
;
257 struct btrace_insn_iterator it
;
259 DEBUG ("itrace (0x%x): [%u; %u)", flags
, btrace_insn_number (begin
),
260 btrace_insn_number (end
));
262 gdbarch
= target_gdbarch ();
264 for (it
= *begin
; btrace_insn_cmp (&it
, end
) != 0; btrace_insn_next (&it
, 1))
266 const struct btrace_insn
*insn
;
268 insn
= btrace_insn_get (&it
);
270 /* Print the instruction index. */
271 ui_out_field_uint (uiout
, "index", btrace_insn_number (&it
));
272 ui_out_text (uiout
, "\t");
274 /* Disassembly with '/m' flag may not produce the expected result.
276 gdb_disassembly (gdbarch
, uiout
, NULL
, flags
, 1, insn
->pc
, insn
->pc
+ 1);
280 /* The to_insn_history method of target record-btrace. */
283 record_btrace_insn_history (int size
, int flags
)
285 struct btrace_thread_info
*btinfo
;
286 struct btrace_insn_history
*history
;
287 struct btrace_insn_iterator begin
, end
;
288 struct cleanup
*uiout_cleanup
;
289 struct ui_out
*uiout
;
290 unsigned int context
, covered
;
292 uiout
= current_uiout
;
293 uiout_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
,
295 context
= abs (size
);
297 error (_("Bad record instruction-history-size."));
299 btinfo
= require_btrace ();
300 history
= btinfo
->insn_history
;
303 /* No matter the direction, we start with the tail of the trace. */
304 btrace_insn_end (&begin
, btinfo
);
307 DEBUG ("insn-history (0x%x): %d", flags
, size
);
309 covered
= btrace_insn_prev (&begin
, context
);
313 begin
= history
->begin
;
316 DEBUG ("insn-history (0x%x): %d, prev: [%u; %u)", flags
, size
,
317 btrace_insn_number (&begin
), btrace_insn_number (&end
));
322 covered
= btrace_insn_prev (&begin
, context
);
327 covered
= btrace_insn_next (&end
, context
);
332 btrace_insn_history (uiout
, &begin
, &end
, flags
);
336 printf_unfiltered (_("At the start of the branch trace record.\n"));
338 printf_unfiltered (_("At the end of the branch trace record.\n"));
341 btrace_set_insn_history (btinfo
, &begin
, &end
);
342 do_cleanups (uiout_cleanup
);
345 /* The to_insn_history_range method of target record-btrace. */
348 record_btrace_insn_history_range (ULONGEST from
, ULONGEST to
, int flags
)
350 struct btrace_thread_info
*btinfo
;
351 struct btrace_insn_history
*history
;
352 struct btrace_insn_iterator begin
, end
;
353 struct cleanup
*uiout_cleanup
;
354 struct ui_out
*uiout
;
355 unsigned int low
, high
;
358 uiout
= current_uiout
;
359 uiout_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
,
364 DEBUG ("insn-history (0x%x): [%u; %u)", flags
, low
, high
);
366 /* Check for wrap-arounds. */
367 if (low
!= from
|| high
!= to
)
368 error (_("Bad range."));
371 error (_("Bad range."));
373 btinfo
= require_btrace ();
375 found
= btrace_find_insn_by_number (&begin
, btinfo
, low
);
377 error (_("Range out of bounds."));
379 /* Silently truncate the range, if necessary. */
380 found
= btrace_find_insn_by_number (&end
, btinfo
, high
);
382 btrace_insn_end (&end
, btinfo
);
384 btrace_insn_history (uiout
, &begin
, &end
, flags
);
385 btrace_set_insn_history (btinfo
, &begin
, &end
);
387 do_cleanups (uiout_cleanup
);
390 /* The to_insn_history_from method of target record-btrace. */
393 record_btrace_insn_history_from (ULONGEST from
, int size
, int flags
)
395 ULONGEST begin
, end
, context
;
397 context
= abs (size
);
406 begin
= from
- context
;
411 end
= from
+ context
;
413 /* Check for wrap-around. */
418 record_btrace_insn_history_range (begin
, end
, flags
);
421 /* Print the instruction number range for a function call history line. */
424 btrace_call_history_insn_range (struct ui_out
*uiout
,
425 const struct btrace_function
*bfun
)
427 unsigned int begin
, end
, size
;
429 size
= VEC_length (btrace_insn_s
, bfun
->insn
);
430 gdb_assert (size
> 0);
432 begin
= bfun
->insn_offset
;
433 end
= begin
+ size
- 1;
435 ui_out_field_uint (uiout
, "insn begin", begin
);
436 ui_out_text (uiout
, ",");
437 ui_out_field_uint (uiout
, "insn end", end
);
440 /* Print the source line information for a function call history line. */
443 btrace_call_history_src_line (struct ui_out
*uiout
,
444 const struct btrace_function
*bfun
)
453 ui_out_field_string (uiout
, "file",
454 symtab_to_filename_for_display (sym
->symtab
));
456 begin
= bfun
->lbegin
;
462 ui_out_text (uiout
, ":");
463 ui_out_field_int (uiout
, "min line", begin
);
468 ui_out_text (uiout
, ",");
469 ui_out_field_int (uiout
, "max line", end
);
472 /* Disassemble a section of the recorded function trace. */
475 btrace_call_history (struct ui_out
*uiout
,
476 const struct btrace_thread_info
*btinfo
,
477 const struct btrace_call_iterator
*begin
,
478 const struct btrace_call_iterator
*end
,
479 enum record_print_flag flags
)
481 struct btrace_call_iterator it
;
483 DEBUG ("ftrace (0x%x): [%u; %u)", flags
, btrace_call_number (begin
),
484 btrace_call_number (end
));
486 for (it
= *begin
; btrace_call_cmp (&it
, end
) < 0; btrace_call_next (&it
, 1))
488 const struct btrace_function
*bfun
;
489 struct minimal_symbol
*msym
;
492 bfun
= btrace_call_get (&it
);
496 /* Print the function index. */
497 ui_out_field_uint (uiout
, "index", bfun
->number
);
498 ui_out_text (uiout
, "\t");
500 if ((flags
& RECORD_PRINT_INDENT_CALLS
) != 0)
502 int level
= bfun
->level
+ btinfo
->level
, i
;
504 for (i
= 0; i
< level
; ++i
)
505 ui_out_text (uiout
, " ");
509 ui_out_field_string (uiout
, "function", SYMBOL_PRINT_NAME (sym
));
510 else if (msym
!= NULL
)
511 ui_out_field_string (uiout
, "function", SYMBOL_PRINT_NAME (msym
));
512 else if (!ui_out_is_mi_like_p (uiout
))
513 ui_out_field_string (uiout
, "function", "??");
515 if ((flags
& RECORD_PRINT_INSN_RANGE
) != 0)
517 ui_out_text (uiout
, _("\tinst "));
518 btrace_call_history_insn_range (uiout
, bfun
);
521 if ((flags
& RECORD_PRINT_SRC_LINE
) != 0)
523 ui_out_text (uiout
, _("\tat "));
524 btrace_call_history_src_line (uiout
, bfun
);
527 ui_out_text (uiout
, "\n");
531 /* The to_call_history method of target record-btrace. */
534 record_btrace_call_history (int size
, int flags
)
536 struct btrace_thread_info
*btinfo
;
537 struct btrace_call_history
*history
;
538 struct btrace_call_iterator begin
, end
;
539 struct cleanup
*uiout_cleanup
;
540 struct ui_out
*uiout
;
541 unsigned int context
, covered
;
543 uiout
= current_uiout
;
544 uiout_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
,
546 context
= abs (size
);
548 error (_("Bad record function-call-history-size."));
550 btinfo
= require_btrace ();
551 history
= btinfo
->call_history
;
554 /* No matter the direction, we start with the tail of the trace. */
555 btrace_call_end (&begin
, btinfo
);
558 DEBUG ("call-history (0x%x): %d", flags
, size
);
560 covered
= btrace_call_prev (&begin
, context
);
564 begin
= history
->begin
;
567 DEBUG ("call-history (0x%x): %d, prev: [%u; %u)", flags
, size
,
568 btrace_call_number (&begin
), btrace_call_number (&end
));
573 covered
= btrace_call_prev (&begin
, context
);
578 covered
= btrace_call_next (&end
, context
);
583 btrace_call_history (uiout
, btinfo
, &begin
, &end
, flags
);
587 printf_unfiltered (_("At the start of the branch trace record.\n"));
589 printf_unfiltered (_("At the end of the branch trace record.\n"));
592 btrace_set_call_history (btinfo
, &begin
, &end
);
593 do_cleanups (uiout_cleanup
);
596 /* The to_call_history_range method of target record-btrace. */
599 record_btrace_call_history_range (ULONGEST from
, ULONGEST to
, int flags
)
601 struct btrace_thread_info
*btinfo
;
602 struct btrace_call_history
*history
;
603 struct btrace_call_iterator begin
, end
;
604 struct cleanup
*uiout_cleanup
;
605 struct ui_out
*uiout
;
606 unsigned int low
, high
;
609 uiout
= current_uiout
;
610 uiout_cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
,
615 DEBUG ("call-history (0x%x): [%u; %u)", flags
, low
, high
);
617 /* Check for wrap-arounds. */
618 if (low
!= from
|| high
!= to
)
619 error (_("Bad range."));
622 error (_("Bad range."));
624 btinfo
= require_btrace ();
626 found
= btrace_find_call_by_number (&begin
, btinfo
, low
);
628 error (_("Range out of bounds."));
630 /* Silently truncate the range, if necessary. */
631 found
= btrace_find_call_by_number (&end
, btinfo
, high
);
633 btrace_call_end (&end
, btinfo
);
635 btrace_call_history (uiout
, btinfo
, &begin
, &end
, flags
);
636 btrace_set_call_history (btinfo
, &begin
, &end
);
638 do_cleanups (uiout_cleanup
);
641 /* The to_call_history_from method of target record-btrace. */
644 record_btrace_call_history_from (ULONGEST from
, int size
, int flags
)
646 ULONGEST begin
, end
, context
;
648 context
= abs (size
);
657 begin
= from
- context
;
662 end
= from
+ context
;
664 /* Check for wrap-around. */
669 record_btrace_call_history_range (begin
, end
, flags
);
672 /* Initialize the record-btrace target ops. */
675 init_record_btrace_ops (void)
677 struct target_ops
*ops
;
679 ops
= &record_btrace_ops
;
680 ops
->to_shortname
= "record-btrace";
681 ops
->to_longname
= "Branch tracing target";
682 ops
->to_doc
= "Collect control-flow trace and provide the execution history.";
683 ops
->to_open
= record_btrace_open
;
684 ops
->to_close
= record_btrace_close
;
685 ops
->to_detach
= record_detach
;
686 ops
->to_disconnect
= record_disconnect
;
687 ops
->to_mourn_inferior
= record_mourn_inferior
;
688 ops
->to_kill
= record_kill
;
689 ops
->to_create_inferior
= find_default_create_inferior
;
690 ops
->to_stop_recording
= record_btrace_stop_recording
;
691 ops
->to_info_record
= record_btrace_info
;
692 ops
->to_insn_history
= record_btrace_insn_history
;
693 ops
->to_insn_history_from
= record_btrace_insn_history_from
;
694 ops
->to_insn_history_range
= record_btrace_insn_history_range
;
695 ops
->to_call_history
= record_btrace_call_history
;
696 ops
->to_call_history_from
= record_btrace_call_history_from
;
697 ops
->to_call_history_range
= record_btrace_call_history_range
;
698 ops
->to_stratum
= record_stratum
;
699 ops
->to_magic
= OPS_MAGIC
;
702 /* Alias for "target record". */
705 cmd_record_btrace_start (char *args
, int from_tty
)
707 if (args
!= NULL
&& *args
!= 0)
708 error (_("Invalid argument."));
710 execute_command ("target record-btrace", from_tty
);
713 void _initialize_record_btrace (void);
715 /* Initialize btrace commands. */
718 _initialize_record_btrace (void)
720 add_cmd ("btrace", class_obscure
, cmd_record_btrace_start
,
721 _("Start branch trace recording."),
723 add_alias_cmd ("b", "btrace", class_obscure
, 1, &record_cmdlist
);
725 init_record_btrace_ops ();
726 add_target (&record_btrace_ops
);