1 /* Branch trace support for GDB, the GNU debugger.
3 Copyright (C) 2013-2015 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"
31 #include "filenames.h"
32 #include "xml-support.h"
35 /* Print a record debug message. Use do ... while (0) to avoid ambiguities
36 when used in if statements. */
38 #define DEBUG(msg, args...) \
41 if (record_debug != 0) \
42 fprintf_unfiltered (gdb_stdlog, \
43 "[btrace] " msg "\n", ##args); \
47 #define DEBUG_FTRACE(msg, args...) DEBUG ("[ftrace] " msg, ##args)
49 /* Return the function name of a recorded function segment for printing.
50 This function never returns NULL. */
53 ftrace_print_function_name (const struct btrace_function
*bfun
)
55 struct minimal_symbol
*msym
;
62 return SYMBOL_PRINT_NAME (sym
);
65 return MSYMBOL_PRINT_NAME (msym
);
70 /* Return the file name of a recorded function segment for printing.
71 This function never returns NULL. */
74 ftrace_print_filename (const struct btrace_function
*bfun
)
82 filename
= symtab_to_filename_for_display (symbol_symtab (sym
));
84 filename
= "<unknown>";
89 /* Return a string representation of the address of an instruction.
90 This function never returns NULL. */
93 ftrace_print_insn_addr (const struct btrace_insn
*insn
)
98 return core_addr_to_string_nz (insn
->pc
);
101 /* Print an ftrace debug status message. */
104 ftrace_debug (const struct btrace_function
*bfun
, const char *prefix
)
106 const char *fun
, *file
;
107 unsigned int ibegin
, iend
;
108 int lbegin
, lend
, level
;
110 fun
= ftrace_print_function_name (bfun
);
111 file
= ftrace_print_filename (bfun
);
114 lbegin
= bfun
->lbegin
;
117 ibegin
= bfun
->insn_offset
;
118 iend
= ibegin
+ VEC_length (btrace_insn_s
, bfun
->insn
);
120 DEBUG_FTRACE ("%s: fun = %s, file = %s, level = %d, lines = [%d; %d], "
121 "insn = [%u; %u)", prefix
, fun
, file
, level
, lbegin
, lend
,
125 /* Return non-zero if BFUN does not match MFUN and FUN,
126 return zero otherwise. */
129 ftrace_function_switched (const struct btrace_function
*bfun
,
130 const struct minimal_symbol
*mfun
,
131 const struct symbol
*fun
)
133 struct minimal_symbol
*msym
;
139 /* If the minimal symbol changed, we certainly switched functions. */
140 if (mfun
!= NULL
&& msym
!= NULL
141 && strcmp (MSYMBOL_LINKAGE_NAME (mfun
), MSYMBOL_LINKAGE_NAME (msym
)) != 0)
144 /* If the symbol changed, we certainly switched functions. */
145 if (fun
!= NULL
&& sym
!= NULL
)
147 const char *bfname
, *fname
;
149 /* Check the function name. */
150 if (strcmp (SYMBOL_LINKAGE_NAME (fun
), SYMBOL_LINKAGE_NAME (sym
)) != 0)
153 /* Check the location of those functions, as well. */
154 bfname
= symtab_to_fullname (symbol_symtab (sym
));
155 fname
= symtab_to_fullname (symbol_symtab (fun
));
156 if (filename_cmp (fname
, bfname
) != 0)
160 /* If we lost symbol information, we switched functions. */
161 if (!(msym
== NULL
&& sym
== NULL
) && mfun
== NULL
&& fun
== NULL
)
164 /* If we gained symbol information, we switched functions. */
165 if (msym
== NULL
&& sym
== NULL
&& !(mfun
== NULL
&& fun
== NULL
))
171 /* Return non-zero if we should skip this file when generating the function
172 call history, zero otherwise.
173 We would want to do that if, say, a macro that is defined in another file
174 is expanded in this function. */
177 ftrace_skip_file (const struct btrace_function
*bfun
, const char *fullname
)
186 bfile
= symtab_to_fullname (symbol_symtab (sym
));
188 return (filename_cmp (bfile
, fullname
) != 0);
191 /* Allocate and initialize a new branch trace function segment.
192 PREV is the chronologically preceding function segment.
193 MFUN and FUN are the symbol information we have for this function. */
195 static struct btrace_function
*
196 ftrace_new_function (struct btrace_function
*prev
,
197 struct minimal_symbol
*mfun
,
200 struct btrace_function
*bfun
;
202 bfun
= xzalloc (sizeof (*bfun
));
206 bfun
->flow
.prev
= prev
;
208 /* We start with the identities of min and max, respectively. */
209 bfun
->lbegin
= INT_MAX
;
210 bfun
->lend
= INT_MIN
;
214 /* Start counting at one. */
216 bfun
->insn_offset
= 1;
220 gdb_assert (prev
->flow
.next
== NULL
);
221 prev
->flow
.next
= bfun
;
223 bfun
->number
= prev
->number
+ 1;
224 bfun
->insn_offset
= (prev
->insn_offset
225 + VEC_length (btrace_insn_s
, prev
->insn
));
231 /* Update the UP field of a function segment. */
234 ftrace_update_caller (struct btrace_function
*bfun
,
235 struct btrace_function
*caller
,
236 enum btrace_function_flag flags
)
238 if (bfun
->up
!= NULL
)
239 ftrace_debug (bfun
, "updating caller");
244 ftrace_debug (bfun
, "set caller");
247 /* Fix up the caller for all segments of a function. */
250 ftrace_fixup_caller (struct btrace_function
*bfun
,
251 struct btrace_function
*caller
,
252 enum btrace_function_flag flags
)
254 struct btrace_function
*prev
, *next
;
256 ftrace_update_caller (bfun
, caller
, flags
);
258 /* Update all function segments belonging to the same function. */
259 for (prev
= bfun
->segment
.prev
; prev
!= NULL
; prev
= prev
->segment
.prev
)
260 ftrace_update_caller (prev
, caller
, flags
);
262 for (next
= bfun
->segment
.next
; next
!= NULL
; next
= next
->segment
.next
)
263 ftrace_update_caller (next
, caller
, flags
);
266 /* Add a new function segment for a call.
267 CALLER is the chronologically preceding function segment.
268 MFUN and FUN are the symbol information we have for this function. */
270 static struct btrace_function
*
271 ftrace_new_call (struct btrace_function
*caller
,
272 struct minimal_symbol
*mfun
,
275 struct btrace_function
*bfun
;
277 bfun
= ftrace_new_function (caller
, mfun
, fun
);
279 bfun
->level
= caller
->level
+ 1;
281 ftrace_debug (bfun
, "new call");
286 /* Add a new function segment for a tail call.
287 CALLER is the chronologically preceding function segment.
288 MFUN and FUN are the symbol information we have for this function. */
290 static struct btrace_function
*
291 ftrace_new_tailcall (struct btrace_function
*caller
,
292 struct minimal_symbol
*mfun
,
295 struct btrace_function
*bfun
;
297 bfun
= ftrace_new_function (caller
, mfun
, fun
);
299 bfun
->level
= caller
->level
+ 1;
300 bfun
->flags
|= BFUN_UP_LINKS_TO_TAILCALL
;
302 ftrace_debug (bfun
, "new tail call");
307 /* Find the innermost caller in the back trace of BFUN with MFUN/FUN
308 symbol information. */
310 static struct btrace_function
*
311 ftrace_find_caller (struct btrace_function
*bfun
,
312 struct minimal_symbol
*mfun
,
315 for (; bfun
!= NULL
; bfun
= bfun
->up
)
317 /* Skip functions with incompatible symbol information. */
318 if (ftrace_function_switched (bfun
, mfun
, fun
))
321 /* This is the function segment we're looking for. */
328 /* Find the innermost caller in the back trace of BFUN, skipping all
329 function segments that do not end with a call instruction (e.g.
330 tail calls ending with a jump). */
332 static struct btrace_function
*
333 ftrace_find_call (struct gdbarch
*gdbarch
, struct btrace_function
*bfun
)
335 for (; bfun
!= NULL
; bfun
= bfun
->up
)
337 struct btrace_insn
*last
;
340 /* We do not allow empty function segments. */
341 gdb_assert (!VEC_empty (btrace_insn_s
, bfun
->insn
));
343 last
= VEC_last (btrace_insn_s
, bfun
->insn
);
346 if (gdbarch_insn_is_call (gdbarch
, pc
))
353 /* Add a continuation segment for a function into which we return.
354 PREV is the chronologically preceding function segment.
355 MFUN and FUN are the symbol information we have for this function. */
357 static struct btrace_function
*
358 ftrace_new_return (struct gdbarch
*gdbarch
,
359 struct btrace_function
*prev
,
360 struct minimal_symbol
*mfun
,
363 struct btrace_function
*bfun
, *caller
;
365 bfun
= ftrace_new_function (prev
, mfun
, fun
);
367 /* It is important to start at PREV's caller. Otherwise, we might find
368 PREV itself, if PREV is a recursive function. */
369 caller
= ftrace_find_caller (prev
->up
, mfun
, fun
);
372 /* The caller of PREV is the preceding btrace function segment in this
373 function instance. */
374 gdb_assert (caller
->segment
.next
== NULL
);
376 caller
->segment
.next
= bfun
;
377 bfun
->segment
.prev
= caller
;
379 /* Maintain the function level. */
380 bfun
->level
= caller
->level
;
382 /* Maintain the call stack. */
383 bfun
->up
= caller
->up
;
384 bfun
->flags
= caller
->flags
;
386 ftrace_debug (bfun
, "new return");
390 /* We did not find a caller. This could mean that something went
391 wrong or that the call is simply not included in the trace. */
393 /* Let's search for some actual call. */
394 caller
= ftrace_find_call (gdbarch
, prev
->up
);
397 /* There is no call in PREV's back trace. We assume that the
398 branch trace did not include it. */
400 /* Let's find the topmost call function - this skips tail calls. */
401 while (prev
->up
!= NULL
)
404 /* We maintain levels for a series of returns for which we have
406 We start at the preceding function's level in case this has
407 already been a return for which we have not seen the call.
408 We start at level 0 otherwise, to handle tail calls correctly. */
409 bfun
->level
= min (0, prev
->level
) - 1;
411 /* Fix up the call stack for PREV. */
412 ftrace_fixup_caller (prev
, bfun
, BFUN_UP_LINKS_TO_RET
);
414 ftrace_debug (bfun
, "new return - no caller");
418 /* There is a call in PREV's back trace to which we should have
419 returned. Let's remain at this level. */
420 bfun
->level
= prev
->level
;
422 ftrace_debug (bfun
, "new return - unknown caller");
429 /* Add a new function segment for a function switch.
430 PREV is the chronologically preceding function segment.
431 MFUN and FUN are the symbol information we have for this function. */
433 static struct btrace_function
*
434 ftrace_new_switch (struct btrace_function
*prev
,
435 struct minimal_symbol
*mfun
,
438 struct btrace_function
*bfun
;
440 /* This is an unexplained function switch. The call stack will likely
441 be wrong at this point. */
442 bfun
= ftrace_new_function (prev
, mfun
, fun
);
444 /* We keep the function level. */
445 bfun
->level
= prev
->level
;
447 ftrace_debug (bfun
, "new switch");
452 /* Update BFUN with respect to the instruction at PC. This may create new
454 Return the chronologically latest function segment, never NULL. */
456 static struct btrace_function
*
457 ftrace_update_function (struct gdbarch
*gdbarch
,
458 struct btrace_function
*bfun
, CORE_ADDR pc
)
460 struct bound_minimal_symbol bmfun
;
461 struct minimal_symbol
*mfun
;
463 struct btrace_insn
*last
;
465 /* Try to determine the function we're in. We use both types of symbols
466 to avoid surprises when we sometimes get a full symbol and sometimes
467 only a minimal symbol. */
468 fun
= find_pc_function (pc
);
469 bmfun
= lookup_minimal_symbol_by_pc (pc
);
472 if (fun
== NULL
&& mfun
== NULL
)
473 DEBUG_FTRACE ("no symbol at %s", core_addr_to_string_nz (pc
));
475 /* If we didn't have a function before, we create one. */
477 return ftrace_new_function (bfun
, mfun
, fun
);
479 /* Check the last instruction, if we have one.
480 We do this check first, since it allows us to fill in the call stack
481 links in addition to the normal flow links. */
483 if (!VEC_empty (btrace_insn_s
, bfun
->insn
))
484 last
= VEC_last (btrace_insn_s
, bfun
->insn
);
492 /* Check for returns. */
493 if (gdbarch_insn_is_ret (gdbarch
, lpc
))
494 return ftrace_new_return (gdbarch
, bfun
, mfun
, fun
);
496 /* Check for calls. */
497 if (gdbarch_insn_is_call (gdbarch
, lpc
))
501 size
= gdb_insn_length (gdbarch
, lpc
);
503 /* Ignore calls to the next instruction. They are used for PIC. */
504 if (lpc
+ size
!= pc
)
505 return ftrace_new_call (bfun
, mfun
, fun
);
509 /* Check if we're switching functions for some other reason. */
510 if (ftrace_function_switched (bfun
, mfun
, fun
))
512 DEBUG_FTRACE ("switching from %s in %s at %s",
513 ftrace_print_insn_addr (last
),
514 ftrace_print_function_name (bfun
),
515 ftrace_print_filename (bfun
));
519 CORE_ADDR start
, lpc
;
521 start
= get_pc_function_start (pc
);
523 /* If we can't determine the function for PC, we treat a jump at
524 the end of the block as tail call. */
530 /* Jumps indicate optimized tail calls. */
531 if (start
== pc
&& gdbarch_insn_is_jump (gdbarch
, lpc
))
532 return ftrace_new_tailcall (bfun
, mfun
, fun
);
535 return ftrace_new_switch (bfun
, mfun
, fun
);
541 /* Update BFUN's source range with respect to the instruction at PC. */
544 ftrace_update_lines (struct btrace_function
*bfun
, CORE_ADDR pc
)
546 struct symtab_and_line sal
;
547 const char *fullname
;
549 sal
= find_pc_line (pc
, 0);
550 if (sal
.symtab
== NULL
|| sal
.line
== 0)
552 DEBUG_FTRACE ("no lines at %s", core_addr_to_string_nz (pc
));
556 /* Check if we switched files. This could happen if, say, a macro that
557 is defined in another file is expanded here. */
558 fullname
= symtab_to_fullname (sal
.symtab
);
559 if (ftrace_skip_file (bfun
, fullname
))
561 DEBUG_FTRACE ("ignoring file at %s, file=%s",
562 core_addr_to_string_nz (pc
), fullname
);
566 /* Update the line range. */
567 bfun
->lbegin
= min (bfun
->lbegin
, sal
.line
);
568 bfun
->lend
= max (bfun
->lend
, sal
.line
);
570 if (record_debug
> 1)
571 ftrace_debug (bfun
, "update lines");
574 /* Add the instruction at PC to BFUN's instructions. */
577 ftrace_update_insns (struct btrace_function
*bfun
, CORE_ADDR pc
)
579 struct btrace_insn
*insn
;
581 insn
= VEC_safe_push (btrace_insn_s
, bfun
->insn
, NULL
);
584 if (record_debug
> 1)
585 ftrace_debug (bfun
, "update insn");
588 /* Compute the function branch trace from BTS trace. */
591 btrace_compute_ftrace_bts (struct btrace_thread_info
*btinfo
,
592 const struct btrace_data_bts
*btrace
)
594 struct btrace_function
*begin
, *end
;
595 struct gdbarch
*gdbarch
;
599 gdbarch
= target_gdbarch ();
600 begin
= btinfo
->begin
;
602 level
= begin
!= NULL
? -btinfo
->level
: INT_MAX
;
603 blk
= VEC_length (btrace_block_s
, btrace
->blocks
);
607 btrace_block_s
*block
;
612 block
= VEC_index (btrace_block_s
, btrace
->blocks
, blk
);
619 /* We should hit the end of the block. Warn if we went too far. */
622 warning (_("Recorded trace may be corrupted around %s."),
623 core_addr_to_string_nz (pc
));
627 end
= ftrace_update_function (gdbarch
, end
, pc
);
631 /* Maintain the function level offset.
632 For all but the last block, we do it here. */
634 level
= min (level
, end
->level
);
636 ftrace_update_insns (end
, pc
);
637 ftrace_update_lines (end
, pc
);
639 /* We're done once we pushed the instruction at the end. */
640 if (block
->end
== pc
)
643 size
= gdb_insn_length (gdbarch
, pc
);
645 /* Make sure we terminate if we fail to compute the size. */
648 warning (_("Recorded trace may be incomplete around %s."),
649 core_addr_to_string_nz (pc
));
655 /* Maintain the function level offset.
656 For the last block, we do it here to not consider the last
658 Since the last instruction corresponds to the current instruction
659 and is not really part of the execution history, it shouldn't
662 level
= min (level
, end
->level
);
666 btinfo
->begin
= begin
;
669 /* LEVEL is the minimal function level of all btrace function segments.
670 Define the global level offset to -LEVEL so all function levels are
671 normalized to start at zero. */
672 btinfo
->level
= -level
;
675 /* Compute the function branch trace from a block branch trace BTRACE for
676 a thread given by BTINFO. */
679 btrace_compute_ftrace (struct btrace_thread_info
*btinfo
,
680 struct btrace_data
*btrace
)
682 DEBUG ("compute ftrace");
684 switch (btrace
->format
)
686 case BTRACE_FORMAT_NONE
:
689 case BTRACE_FORMAT_BTS
:
690 btrace_compute_ftrace_bts (btinfo
, &btrace
->variant
.bts
);
694 internal_error (__FILE__
, __LINE__
, _("Unkown branch trace format."));
697 /* Add an entry for the current PC. */
700 btrace_add_pc (struct thread_info
*tp
)
702 struct btrace_data btrace
;
703 struct btrace_block
*block
;
704 struct regcache
*regcache
;
705 struct cleanup
*cleanup
;
708 regcache
= get_thread_regcache (tp
->ptid
);
709 pc
= regcache_read_pc (regcache
);
711 btrace_data_init (&btrace
);
712 btrace
.format
= BTRACE_FORMAT_BTS
;
713 btrace
.variant
.bts
.blocks
= NULL
;
715 cleanup
= make_cleanup_btrace_data (&btrace
);
717 block
= VEC_safe_push (btrace_block_s
, btrace
.variant
.bts
.blocks
, NULL
);
721 btrace_compute_ftrace (&tp
->btrace
, &btrace
);
723 do_cleanups (cleanup
);
729 btrace_enable (struct thread_info
*tp
)
731 if (tp
->btrace
.target
!= NULL
)
734 if (!target_supports_btrace ())
735 error (_("Target does not support branch tracing."));
737 DEBUG ("enable thread %d (%s)", tp
->num
, target_pid_to_str (tp
->ptid
));
739 tp
->btrace
.target
= target_enable_btrace (tp
->ptid
);
741 /* Add an entry for the current PC so we start tracing from where we
743 if (tp
->btrace
.target
!= NULL
)
750 btrace_disable (struct thread_info
*tp
)
752 struct btrace_thread_info
*btp
= &tp
->btrace
;
755 if (btp
->target
== NULL
)
758 DEBUG ("disable thread %d (%s)", tp
->num
, target_pid_to_str (tp
->ptid
));
760 target_disable_btrace (btp
->target
);
769 btrace_teardown (struct thread_info
*tp
)
771 struct btrace_thread_info
*btp
= &tp
->btrace
;
774 if (btp
->target
== NULL
)
777 DEBUG ("teardown thread %d (%s)", tp
->num
, target_pid_to_str (tp
->ptid
));
779 target_teardown_btrace (btp
->target
);
785 /* Stitch branch trace in BTS format. */
788 btrace_stitch_bts (struct btrace_data_bts
*btrace
,
789 const struct btrace_thread_info
*btinfo
)
791 struct btrace_function
*last_bfun
;
792 struct btrace_insn
*last_insn
;
793 btrace_block_s
*first_new_block
;
795 last_bfun
= btinfo
->end
;
796 gdb_assert (last_bfun
!= NULL
);
798 /* Beware that block trace starts with the most recent block, so the
799 chronologically first block in the new trace is the last block in
800 the new trace's block vector. */
801 gdb_assert (!VEC_empty (btrace_block_s
, btrace
->blocks
));
802 first_new_block
= VEC_last (btrace_block_s
, btrace
->blocks
);
803 last_insn
= VEC_last (btrace_insn_s
, last_bfun
->insn
);
805 /* If the current PC at the end of the block is the same as in our current
806 trace, there are two explanations:
807 1. we executed the instruction and some branch brought us back.
808 2. we have not made any progress.
809 In the first case, the delta trace vector should contain at least two
811 In the second case, the delta trace vector should contain exactly one
812 entry for the partial block containing the current PC. Remove it. */
813 if (first_new_block
->end
== last_insn
->pc
814 && VEC_length (btrace_block_s
, btrace
->blocks
) == 1)
816 VEC_pop (btrace_block_s
, btrace
->blocks
);
820 DEBUG ("stitching %s to %s", ftrace_print_insn_addr (last_insn
),
821 core_addr_to_string_nz (first_new_block
->end
));
823 /* Do a simple sanity check to make sure we don't accidentally end up
824 with a bad block. This should not occur in practice. */
825 if (first_new_block
->end
< last_insn
->pc
)
827 warning (_("Error while trying to read delta trace. Falling back to "
832 /* We adjust the last block to start at the end of our current trace. */
833 gdb_assert (first_new_block
->begin
== 0);
834 first_new_block
->begin
= last_insn
->pc
;
836 /* We simply pop the last insn so we can insert it again as part of
837 the normal branch trace computation.
838 Since instruction iterators are based on indices in the instructions
839 vector, we don't leave any pointers dangling. */
840 DEBUG ("pruning insn at %s for stitching",
841 ftrace_print_insn_addr (last_insn
));
843 VEC_pop (btrace_insn_s
, last_bfun
->insn
);
845 /* The instructions vector may become empty temporarily if this has
846 been the only instruction in this function segment.
847 This violates the invariant but will be remedied shortly by
848 btrace_compute_ftrace when we add the new trace. */
852 /* Adjust the block trace in order to stitch old and new trace together.
853 BTRACE is the new delta trace between the last and the current stop.
854 BTINFO is the old branch trace until the last stop.
855 May modifx BTRACE as well as the existing trace in BTINFO.
856 Return 0 on success, -1 otherwise. */
859 btrace_stitch_trace (struct btrace_data
*btrace
,
860 const struct btrace_thread_info
*btinfo
)
862 /* If we don't have trace, there's nothing to do. */
863 if (btrace_data_empty (btrace
))
866 switch (btrace
->format
)
868 case BTRACE_FORMAT_NONE
:
871 case BTRACE_FORMAT_BTS
:
872 return btrace_stitch_bts (&btrace
->variant
.bts
, btinfo
);
875 internal_error (__FILE__
, __LINE__
, _("Unkown branch trace format."));
878 /* Clear the branch trace histories in BTINFO. */
881 btrace_clear_history (struct btrace_thread_info
*btinfo
)
883 xfree (btinfo
->insn_history
);
884 xfree (btinfo
->call_history
);
885 xfree (btinfo
->replay
);
887 btinfo
->insn_history
= NULL
;
888 btinfo
->call_history
= NULL
;
889 btinfo
->replay
= NULL
;
895 btrace_fetch (struct thread_info
*tp
)
897 struct btrace_thread_info
*btinfo
;
898 struct btrace_target_info
*tinfo
;
899 struct btrace_data btrace
;
900 struct cleanup
*cleanup
;
903 DEBUG ("fetch thread %d (%s)", tp
->num
, target_pid_to_str (tp
->ptid
));
905 btinfo
= &tp
->btrace
;
906 tinfo
= btinfo
->target
;
910 /* There's no way we could get new trace while replaying.
911 On the other hand, delta trace would return a partial record with the
912 current PC, which is the replay PC, not the last PC, as expected. */
913 if (btinfo
->replay
!= NULL
)
916 btrace_data_init (&btrace
);
917 cleanup
= make_cleanup_btrace_data (&btrace
);
919 /* Let's first try to extend the trace we already have. */
920 if (btinfo
->end
!= NULL
)
922 errcode
= target_read_btrace (&btrace
, tinfo
, BTRACE_READ_DELTA
);
925 /* Success. Let's try to stitch the traces together. */
926 errcode
= btrace_stitch_trace (&btrace
, btinfo
);
930 /* We failed to read delta trace. Let's try to read new trace. */
931 errcode
= target_read_btrace (&btrace
, tinfo
, BTRACE_READ_NEW
);
933 /* If we got any new trace, discard what we have. */
934 if (errcode
== 0 && !btrace_data_empty (&btrace
))
938 /* If we were not able to read the trace, we start over. */
942 errcode
= target_read_btrace (&btrace
, tinfo
, BTRACE_READ_ALL
);
946 errcode
= target_read_btrace (&btrace
, tinfo
, BTRACE_READ_ALL
);
948 /* If we were not able to read the branch trace, signal an error. */
950 error (_("Failed to read branch trace."));
952 /* Compute the trace, provided we have any. */
953 if (!btrace_data_empty (&btrace
))
955 btrace_clear_history (btinfo
);
956 btrace_compute_ftrace (btinfo
, &btrace
);
959 do_cleanups (cleanup
);
965 btrace_clear (struct thread_info
*tp
)
967 struct btrace_thread_info
*btinfo
;
968 struct btrace_function
*it
, *trash
;
970 DEBUG ("clear thread %d (%s)", tp
->num
, target_pid_to_str (tp
->ptid
));
972 /* Make sure btrace frames that may hold a pointer into the branch
973 trace data are destroyed. */
974 reinit_frame_cache ();
976 btinfo
= &tp
->btrace
;
987 btinfo
->begin
= NULL
;
990 btrace_clear_history (btinfo
);
996 btrace_free_objfile (struct objfile
*objfile
)
998 struct thread_info
*tp
;
1000 DEBUG ("free objfile");
1002 ALL_NON_EXITED_THREADS (tp
)
1006 #if defined (HAVE_LIBEXPAT)
1008 /* Check the btrace document version. */
1011 check_xml_btrace_version (struct gdb_xml_parser
*parser
,
1012 const struct gdb_xml_element
*element
,
1013 void *user_data
, VEC (gdb_xml_value_s
) *attributes
)
1015 const char *version
= xml_find_attribute (attributes
, "version")->value
;
1017 if (strcmp (version
, "1.0") != 0)
1018 gdb_xml_error (parser
, _("Unsupported btrace version: \"%s\""), version
);
1021 /* Parse a btrace "block" xml record. */
1024 parse_xml_btrace_block (struct gdb_xml_parser
*parser
,
1025 const struct gdb_xml_element
*element
,
1026 void *user_data
, VEC (gdb_xml_value_s
) *attributes
)
1028 struct btrace_data
*btrace
;
1029 struct btrace_block
*block
;
1030 ULONGEST
*begin
, *end
;
1034 switch (btrace
->format
)
1036 case BTRACE_FORMAT_BTS
:
1039 case BTRACE_FORMAT_NONE
:
1040 btrace
->format
= BTRACE_FORMAT_BTS
;
1041 btrace
->variant
.bts
.blocks
= NULL
;
1045 gdb_xml_error (parser
, _("Btrace format error."));
1048 begin
= xml_find_attribute (attributes
, "begin")->value
;
1049 end
= xml_find_attribute (attributes
, "end")->value
;
1051 block
= VEC_safe_push (btrace_block_s
, btrace
->variant
.bts
.blocks
, NULL
);
1052 block
->begin
= *begin
;
1056 static const struct gdb_xml_attribute block_attributes
[] = {
1057 { "begin", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
1058 { "end", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
1059 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
1062 static const struct gdb_xml_attribute btrace_attributes
[] = {
1063 { "version", GDB_XML_AF_NONE
, NULL
, NULL
},
1064 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
1067 static const struct gdb_xml_element btrace_children
[] = {
1068 { "block", block_attributes
, NULL
,
1069 GDB_XML_EF_REPEATABLE
| GDB_XML_EF_OPTIONAL
, parse_xml_btrace_block
, NULL
},
1070 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
1073 static const struct gdb_xml_element btrace_elements
[] = {
1074 { "btrace", btrace_attributes
, btrace_children
, GDB_XML_EF_NONE
,
1075 check_xml_btrace_version
, NULL
},
1076 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
1079 #endif /* defined (HAVE_LIBEXPAT) */
1084 parse_xml_btrace (struct btrace_data
*btrace
, const char *buffer
)
1086 struct cleanup
*cleanup
;
1089 #if defined (HAVE_LIBEXPAT)
1091 btrace
->format
= BTRACE_FORMAT_NONE
;
1093 cleanup
= make_cleanup_btrace_data (btrace
);
1094 errcode
= gdb_xml_parse_quick (_("btrace"), "btrace.dtd", btrace_elements
,
1097 error (_("Error parsing branch trace."));
1099 /* Keep parse results. */
1100 discard_cleanups (cleanup
);
1102 #else /* !defined (HAVE_LIBEXPAT) */
1104 error (_("Cannot process branch trace. XML parsing is not supported."));
1106 #endif /* !defined (HAVE_LIBEXPAT) */
1111 const struct btrace_insn
*
1112 btrace_insn_get (const struct btrace_insn_iterator
*it
)
1114 const struct btrace_function
*bfun
;
1115 unsigned int index
, end
;
1118 bfun
= it
->function
;
1120 /* The index is within the bounds of this function's instruction vector. */
1121 end
= VEC_length (btrace_insn_s
, bfun
->insn
);
1122 gdb_assert (0 < end
);
1123 gdb_assert (index
< end
);
1125 return VEC_index (btrace_insn_s
, bfun
->insn
, index
);
1131 btrace_insn_number (const struct btrace_insn_iterator
*it
)
1133 const struct btrace_function
*bfun
;
1135 bfun
= it
->function
;
1136 return bfun
->insn_offset
+ it
->index
;
1142 btrace_insn_begin (struct btrace_insn_iterator
*it
,
1143 const struct btrace_thread_info
*btinfo
)
1145 const struct btrace_function
*bfun
;
1147 bfun
= btinfo
->begin
;
1149 error (_("No trace."));
1151 it
->function
= bfun
;
1158 btrace_insn_end (struct btrace_insn_iterator
*it
,
1159 const struct btrace_thread_info
*btinfo
)
1161 const struct btrace_function
*bfun
;
1162 unsigned int length
;
1166 error (_("No trace."));
1168 /* The last instruction in the last function is the current instruction.
1169 We point to it - it is one past the end of the execution trace. */
1170 length
= VEC_length (btrace_insn_s
, bfun
->insn
);
1172 it
->function
= bfun
;
1173 it
->index
= length
- 1;
1179 btrace_insn_next (struct btrace_insn_iterator
*it
, unsigned int stride
)
1181 const struct btrace_function
*bfun
;
1182 unsigned int index
, steps
;
1184 bfun
= it
->function
;
1190 unsigned int end
, space
, adv
;
1192 end
= VEC_length (btrace_insn_s
, bfun
->insn
);
1194 gdb_assert (0 < end
);
1195 gdb_assert (index
< end
);
1197 /* Compute the number of instructions remaining in this segment. */
1198 space
= end
- index
;
1200 /* Advance the iterator as far as possible within this segment. */
1201 adv
= min (space
, stride
);
1206 /* Move to the next function if we're at the end of this one. */
1209 const struct btrace_function
*next
;
1211 next
= bfun
->flow
.next
;
1214 /* We stepped past the last function.
1216 Let's adjust the index to point to the last instruction in
1217 the previous function. */
1223 /* We now point to the first instruction in the new function. */
1228 /* We did make progress. */
1229 gdb_assert (adv
> 0);
1232 /* Update the iterator. */
1233 it
->function
= bfun
;
1242 btrace_insn_prev (struct btrace_insn_iterator
*it
, unsigned int stride
)
1244 const struct btrace_function
*bfun
;
1245 unsigned int index
, steps
;
1247 bfun
= it
->function
;
1255 /* Move to the previous function if we're at the start of this one. */
1258 const struct btrace_function
*prev
;
1260 prev
= bfun
->flow
.prev
;
1264 /* We point to one after the last instruction in the new function. */
1266 index
= VEC_length (btrace_insn_s
, bfun
->insn
);
1268 /* There is at least one instruction in this function segment. */
1269 gdb_assert (index
> 0);
1272 /* Advance the iterator as far as possible within this segment. */
1273 adv
= min (index
, stride
);
1278 /* We did make progress. */
1279 gdb_assert (adv
> 0);
1282 /* Update the iterator. */
1283 it
->function
= bfun
;
1292 btrace_insn_cmp (const struct btrace_insn_iterator
*lhs
,
1293 const struct btrace_insn_iterator
*rhs
)
1295 unsigned int lnum
, rnum
;
1297 lnum
= btrace_insn_number (lhs
);
1298 rnum
= btrace_insn_number (rhs
);
1300 return (int) (lnum
- rnum
);
1306 btrace_find_insn_by_number (struct btrace_insn_iterator
*it
,
1307 const struct btrace_thread_info
*btinfo
,
1308 unsigned int number
)
1310 const struct btrace_function
*bfun
;
1313 for (bfun
= btinfo
->end
; bfun
!= NULL
; bfun
= bfun
->flow
.prev
)
1314 if (bfun
->insn_offset
<= number
)
1320 end
= bfun
->insn_offset
+ VEC_length (btrace_insn_s
, bfun
->insn
);
1324 it
->function
= bfun
;
1325 it
->index
= number
- bfun
->insn_offset
;
1332 const struct btrace_function
*
1333 btrace_call_get (const struct btrace_call_iterator
*it
)
1335 return it
->function
;
1341 btrace_call_number (const struct btrace_call_iterator
*it
)
1343 const struct btrace_thread_info
*btinfo
;
1344 const struct btrace_function
*bfun
;
1347 btinfo
= it
->btinfo
;
1348 bfun
= it
->function
;
1350 return bfun
->number
;
1352 /* For the end iterator, i.e. bfun == NULL, we return one more than the
1353 number of the last function. */
1355 insns
= VEC_length (btrace_insn_s
, bfun
->insn
);
1357 /* If the function contains only a single instruction (i.e. the current
1358 instruction), it will be skipped and its number is already the number
1361 return bfun
->number
;
1363 /* Otherwise, return one more than the number of the last function. */
1364 return bfun
->number
+ 1;
1370 btrace_call_begin (struct btrace_call_iterator
*it
,
1371 const struct btrace_thread_info
*btinfo
)
1373 const struct btrace_function
*bfun
;
1375 bfun
= btinfo
->begin
;
1377 error (_("No trace."));
1379 it
->btinfo
= btinfo
;
1380 it
->function
= bfun
;
1386 btrace_call_end (struct btrace_call_iterator
*it
,
1387 const struct btrace_thread_info
*btinfo
)
1389 const struct btrace_function
*bfun
;
1393 error (_("No trace."));
1395 it
->btinfo
= btinfo
;
1396 it
->function
= NULL
;
1402 btrace_call_next (struct btrace_call_iterator
*it
, unsigned int stride
)
1404 const struct btrace_function
*bfun
;
1407 bfun
= it
->function
;
1409 while (bfun
!= NULL
)
1411 const struct btrace_function
*next
;
1414 next
= bfun
->flow
.next
;
1417 /* Ignore the last function if it only contains a single
1418 (i.e. the current) instruction. */
1419 insns
= VEC_length (btrace_insn_s
, bfun
->insn
);
1424 if (stride
== steps
)
1431 it
->function
= bfun
;
1438 btrace_call_prev (struct btrace_call_iterator
*it
, unsigned int stride
)
1440 const struct btrace_thread_info
*btinfo
;
1441 const struct btrace_function
*bfun
;
1444 bfun
= it
->function
;
1451 btinfo
= it
->btinfo
;
1456 /* Ignore the last function if it only contains a single
1457 (i.e. the current) instruction. */
1458 insns
= VEC_length (btrace_insn_s
, bfun
->insn
);
1460 bfun
= bfun
->flow
.prev
;
1468 while (steps
< stride
)
1470 const struct btrace_function
*prev
;
1472 prev
= bfun
->flow
.prev
;
1480 it
->function
= bfun
;
1487 btrace_call_cmp (const struct btrace_call_iterator
*lhs
,
1488 const struct btrace_call_iterator
*rhs
)
1490 unsigned int lnum
, rnum
;
1492 lnum
= btrace_call_number (lhs
);
1493 rnum
= btrace_call_number (rhs
);
1495 return (int) (lnum
- rnum
);
1501 btrace_find_call_by_number (struct btrace_call_iterator
*it
,
1502 const struct btrace_thread_info
*btinfo
,
1503 unsigned int number
)
1505 const struct btrace_function
*bfun
;
1507 for (bfun
= btinfo
->end
; bfun
!= NULL
; bfun
= bfun
->flow
.prev
)
1511 bnum
= bfun
->number
;
1514 it
->btinfo
= btinfo
;
1515 it
->function
= bfun
;
1519 /* Functions are ordered and numbered consecutively. We could bail out
1520 earlier. On the other hand, it is very unlikely that we search for
1521 a nonexistent function. */
1530 btrace_set_insn_history (struct btrace_thread_info
*btinfo
,
1531 const struct btrace_insn_iterator
*begin
,
1532 const struct btrace_insn_iterator
*end
)
1534 if (btinfo
->insn_history
== NULL
)
1535 btinfo
->insn_history
= xzalloc (sizeof (*btinfo
->insn_history
));
1537 btinfo
->insn_history
->begin
= *begin
;
1538 btinfo
->insn_history
->end
= *end
;
1544 btrace_set_call_history (struct btrace_thread_info
*btinfo
,
1545 const struct btrace_call_iterator
*begin
,
1546 const struct btrace_call_iterator
*end
)
1548 gdb_assert (begin
->btinfo
== end
->btinfo
);
1550 if (btinfo
->call_history
== NULL
)
1551 btinfo
->call_history
= xzalloc (sizeof (*btinfo
->call_history
));
1553 btinfo
->call_history
->begin
= *begin
;
1554 btinfo
->call_history
->end
= *end
;
1560 btrace_is_replaying (struct thread_info
*tp
)
1562 return tp
->btrace
.replay
!= NULL
;
1568 btrace_is_empty (struct thread_info
*tp
)
1570 struct btrace_insn_iterator begin
, end
;
1571 struct btrace_thread_info
*btinfo
;
1573 btinfo
= &tp
->btrace
;
1575 if (btinfo
->begin
== NULL
)
1578 btrace_insn_begin (&begin
, btinfo
);
1579 btrace_insn_end (&end
, btinfo
);
1581 return btrace_insn_cmp (&begin
, &end
) == 0;
1584 /* Forward the cleanup request. */
1587 do_btrace_data_cleanup (void *arg
)
1589 btrace_data_fini (arg
);
1595 make_cleanup_btrace_data (struct btrace_data
*data
)
1597 return make_cleanup (do_btrace_data_cleanup
, data
);