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 btrace_function
*bfun
)
335 for (; bfun
!= NULL
; bfun
= bfun
->up
)
337 struct btrace_insn
*last
;
339 /* We do not allow empty function segments. */
340 gdb_assert (!VEC_empty (btrace_insn_s
, bfun
->insn
));
342 last
= VEC_last (btrace_insn_s
, bfun
->insn
);
344 if (last
->iclass
== BTRACE_INSN_CALL
)
351 /* Add a continuation segment for a function into which we return.
352 PREV is the chronologically preceding function segment.
353 MFUN and FUN are the symbol information we have for this function. */
355 static struct btrace_function
*
356 ftrace_new_return (struct btrace_function
*prev
,
357 struct minimal_symbol
*mfun
,
360 struct btrace_function
*bfun
, *caller
;
362 bfun
= ftrace_new_function (prev
, mfun
, fun
);
364 /* It is important to start at PREV's caller. Otherwise, we might find
365 PREV itself, if PREV is a recursive function. */
366 caller
= ftrace_find_caller (prev
->up
, mfun
, fun
);
369 /* The caller of PREV is the preceding btrace function segment in this
370 function instance. */
371 gdb_assert (caller
->segment
.next
== NULL
);
373 caller
->segment
.next
= bfun
;
374 bfun
->segment
.prev
= caller
;
376 /* Maintain the function level. */
377 bfun
->level
= caller
->level
;
379 /* Maintain the call stack. */
380 bfun
->up
= caller
->up
;
381 bfun
->flags
= caller
->flags
;
383 ftrace_debug (bfun
, "new return");
387 /* We did not find a caller. This could mean that something went
388 wrong or that the call is simply not included in the trace. */
390 /* Let's search for some actual call. */
391 caller
= ftrace_find_call (prev
->up
);
394 /* There is no call in PREV's back trace. We assume that the
395 branch trace did not include it. */
397 /* Let's find the topmost call function - this skips tail calls. */
398 while (prev
->up
!= NULL
)
401 /* We maintain levels for a series of returns for which we have
403 We start at the preceding function's level in case this has
404 already been a return for which we have not seen the call.
405 We start at level 0 otherwise, to handle tail calls correctly. */
406 bfun
->level
= min (0, prev
->level
) - 1;
408 /* Fix up the call stack for PREV. */
409 ftrace_fixup_caller (prev
, bfun
, BFUN_UP_LINKS_TO_RET
);
411 ftrace_debug (bfun
, "new return - no caller");
415 /* There is a call in PREV's back trace to which we should have
416 returned. Let's remain at this level. */
417 bfun
->level
= prev
->level
;
419 ftrace_debug (bfun
, "new return - unknown caller");
426 /* Add a new function segment for a function switch.
427 PREV is the chronologically preceding function segment.
428 MFUN and FUN are the symbol information we have for this function. */
430 static struct btrace_function
*
431 ftrace_new_switch (struct btrace_function
*prev
,
432 struct minimal_symbol
*mfun
,
435 struct btrace_function
*bfun
;
437 /* This is an unexplained function switch. The call stack will likely
438 be wrong at this point. */
439 bfun
= ftrace_new_function (prev
, mfun
, fun
);
441 /* We keep the function level. */
442 bfun
->level
= prev
->level
;
444 ftrace_debug (bfun
, "new switch");
449 /* Update BFUN with respect to the instruction at PC. This may create new
451 Return the chronologically latest function segment, never NULL. */
453 static struct btrace_function
*
454 ftrace_update_function (struct btrace_function
*bfun
, CORE_ADDR pc
)
456 struct bound_minimal_symbol bmfun
;
457 struct minimal_symbol
*mfun
;
459 struct btrace_insn
*last
;
461 /* Try to determine the function we're in. We use both types of symbols
462 to avoid surprises when we sometimes get a full symbol and sometimes
463 only a minimal symbol. */
464 fun
= find_pc_function (pc
);
465 bmfun
= lookup_minimal_symbol_by_pc (pc
);
468 if (fun
== NULL
&& mfun
== NULL
)
469 DEBUG_FTRACE ("no symbol at %s", core_addr_to_string_nz (pc
));
471 /* If we didn't have a function before, we create one. */
473 return ftrace_new_function (bfun
, mfun
, fun
);
475 /* Check the last instruction, if we have one.
476 We do this check first, since it allows us to fill in the call stack
477 links in addition to the normal flow links. */
479 if (!VEC_empty (btrace_insn_s
, bfun
->insn
))
480 last
= VEC_last (btrace_insn_s
, bfun
->insn
);
484 switch (last
->iclass
)
486 case BTRACE_INSN_RETURN
:
487 return ftrace_new_return (bfun
, mfun
, fun
);
489 case BTRACE_INSN_CALL
:
490 /* Ignore calls to the next instruction. They are used for PIC. */
491 if (last
->pc
+ last
->size
== pc
)
494 return ftrace_new_call (bfun
, mfun
, fun
);
496 case BTRACE_INSN_JUMP
:
500 start
= get_pc_function_start (pc
);
502 /* If we can't determine the function for PC, we treat a jump at
503 the end of the block as tail call. */
504 if (start
== 0 || start
== pc
)
505 return ftrace_new_tailcall (bfun
, mfun
, fun
);
510 /* Check if we're switching functions for some other reason. */
511 if (ftrace_function_switched (bfun
, mfun
, fun
))
513 DEBUG_FTRACE ("switching from %s in %s at %s",
514 ftrace_print_insn_addr (last
),
515 ftrace_print_function_name (bfun
),
516 ftrace_print_filename (bfun
));
518 return ftrace_new_switch (bfun
, mfun
, fun
);
524 /* Update BFUN's source range with respect to the instruction at PC. */
527 ftrace_update_lines (struct btrace_function
*bfun
, CORE_ADDR pc
)
529 struct symtab_and_line sal
;
530 const char *fullname
;
532 sal
= find_pc_line (pc
, 0);
533 if (sal
.symtab
== NULL
|| sal
.line
== 0)
535 DEBUG_FTRACE ("no lines at %s", core_addr_to_string_nz (pc
));
539 /* Check if we switched files. This could happen if, say, a macro that
540 is defined in another file is expanded here. */
541 fullname
= symtab_to_fullname (sal
.symtab
);
542 if (ftrace_skip_file (bfun
, fullname
))
544 DEBUG_FTRACE ("ignoring file at %s, file=%s",
545 core_addr_to_string_nz (pc
), fullname
);
549 /* Update the line range. */
550 bfun
->lbegin
= min (bfun
->lbegin
, sal
.line
);
551 bfun
->lend
= max (bfun
->lend
, sal
.line
);
553 if (record_debug
> 1)
554 ftrace_debug (bfun
, "update lines");
557 /* Add the instruction at PC to BFUN's instructions. */
560 ftrace_update_insns (struct btrace_function
*bfun
,
561 const struct btrace_insn
*insn
)
563 VEC_safe_push (btrace_insn_s
, bfun
->insn
, insn
);
565 if (record_debug
> 1)
566 ftrace_debug (bfun
, "update insn");
569 /* Classify the instruction at PC. */
571 static enum btrace_insn_class
572 ftrace_classify_insn (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
574 volatile struct gdb_exception error
;
575 enum btrace_insn_class iclass
;
577 iclass
= BTRACE_INSN_OTHER
;
578 TRY_CATCH (error
, RETURN_MASK_ERROR
)
580 if (gdbarch_insn_is_call (gdbarch
, pc
))
581 iclass
= BTRACE_INSN_CALL
;
582 else if (gdbarch_insn_is_ret (gdbarch
, pc
))
583 iclass
= BTRACE_INSN_RETURN
;
584 else if (gdbarch_insn_is_jump (gdbarch
, pc
))
585 iclass
= BTRACE_INSN_JUMP
;
591 /* Compute the function branch trace from BTS trace. */
594 btrace_compute_ftrace_bts (struct thread_info
*tp
,
595 const struct btrace_data_bts
*btrace
)
597 struct btrace_thread_info
*btinfo
;
598 struct btrace_function
*begin
, *end
;
599 struct gdbarch
*gdbarch
;
603 gdbarch
= target_gdbarch ();
604 btinfo
= &tp
->btrace
;
605 begin
= btinfo
->begin
;
607 level
= begin
!= NULL
? -btinfo
->level
: INT_MAX
;
608 blk
= VEC_length (btrace_block_s
, btrace
->blocks
);
612 btrace_block_s
*block
;
617 block
= VEC_index (btrace_block_s
, btrace
->blocks
, blk
);
622 volatile struct gdb_exception error
;
623 struct btrace_insn insn
;
626 /* We should hit the end of the block. Warn if we went too far. */
629 warning (_("Recorded trace may be corrupted around %s."),
630 core_addr_to_string_nz (pc
));
634 end
= ftrace_update_function (end
, pc
);
638 /* Maintain the function level offset.
639 For all but the last block, we do it here. */
641 level
= min (level
, end
->level
);
644 TRY_CATCH (error
, RETURN_MASK_ERROR
)
645 size
= gdb_insn_length (gdbarch
, pc
);
649 insn
.iclass
= ftrace_classify_insn (gdbarch
, pc
);
651 ftrace_update_insns (end
, &insn
);
652 ftrace_update_lines (end
, pc
);
654 /* We're done once we pushed the instruction at the end. */
655 if (block
->end
== pc
)
658 /* We can't continue if we fail to compute the size. */
661 warning (_("Recorded trace may be incomplete around %s."),
662 core_addr_to_string_nz (pc
));
668 /* Maintain the function level offset.
669 For the last block, we do it here to not consider the last
671 Since the last instruction corresponds to the current instruction
672 and is not really part of the execution history, it shouldn't
675 level
= min (level
, end
->level
);
679 btinfo
->begin
= begin
;
682 /* LEVEL is the minimal function level of all btrace function segments.
683 Define the global level offset to -LEVEL so all function levels are
684 normalized to start at zero. */
685 btinfo
->level
= -level
;
688 /* Compute the function branch trace from a block branch trace BTRACE for
689 a thread given by BTINFO. */
692 btrace_compute_ftrace (struct thread_info
*tp
, struct btrace_data
*btrace
)
694 DEBUG ("compute ftrace");
696 switch (btrace
->format
)
698 case BTRACE_FORMAT_NONE
:
701 case BTRACE_FORMAT_BTS
:
702 btrace_compute_ftrace_bts (tp
, &btrace
->variant
.bts
);
706 internal_error (__FILE__
, __LINE__
, _("Unkown branch trace format."));
709 /* Add an entry for the current PC. */
712 btrace_add_pc (struct thread_info
*tp
)
714 struct btrace_data btrace
;
715 struct btrace_block
*block
;
716 struct regcache
*regcache
;
717 struct cleanup
*cleanup
;
720 regcache
= get_thread_regcache (tp
->ptid
);
721 pc
= regcache_read_pc (regcache
);
723 btrace_data_init (&btrace
);
724 btrace
.format
= BTRACE_FORMAT_BTS
;
725 btrace
.variant
.bts
.blocks
= NULL
;
727 cleanup
= make_cleanup_btrace_data (&btrace
);
729 block
= VEC_safe_push (btrace_block_s
, btrace
.variant
.bts
.blocks
, NULL
);
733 btrace_compute_ftrace (tp
, &btrace
);
735 do_cleanups (cleanup
);
741 btrace_enable (struct thread_info
*tp
, const struct btrace_config
*conf
)
743 if (tp
->btrace
.target
!= NULL
)
746 if (!target_supports_btrace (conf
->format
))
747 error (_("Target does not support branch tracing."));
749 DEBUG ("enable thread %d (%s)", tp
->num
, target_pid_to_str (tp
->ptid
));
751 tp
->btrace
.target
= target_enable_btrace (tp
->ptid
, conf
);
753 /* Add an entry for the current PC so we start tracing from where we
755 if (tp
->btrace
.target
!= NULL
)
761 const struct btrace_config
*
762 btrace_conf (const struct btrace_thread_info
*btinfo
)
764 if (btinfo
->target
== NULL
)
767 return target_btrace_conf (btinfo
->target
);
773 btrace_disable (struct thread_info
*tp
)
775 struct btrace_thread_info
*btp
= &tp
->btrace
;
778 if (btp
->target
== NULL
)
781 DEBUG ("disable thread %d (%s)", tp
->num
, target_pid_to_str (tp
->ptid
));
783 target_disable_btrace (btp
->target
);
792 btrace_teardown (struct thread_info
*tp
)
794 struct btrace_thread_info
*btp
= &tp
->btrace
;
797 if (btp
->target
== NULL
)
800 DEBUG ("teardown thread %d (%s)", tp
->num
, target_pid_to_str (tp
->ptid
));
802 target_teardown_btrace (btp
->target
);
808 /* Stitch branch trace in BTS format. */
811 btrace_stitch_bts (struct btrace_data_bts
*btrace
,
812 const struct btrace_thread_info
*btinfo
)
814 struct btrace_function
*last_bfun
;
815 struct btrace_insn
*last_insn
;
816 btrace_block_s
*first_new_block
;
818 last_bfun
= btinfo
->end
;
819 gdb_assert (last_bfun
!= NULL
);
821 /* Beware that block trace starts with the most recent block, so the
822 chronologically first block in the new trace is the last block in
823 the new trace's block vector. */
824 gdb_assert (!VEC_empty (btrace_block_s
, btrace
->blocks
));
825 first_new_block
= VEC_last (btrace_block_s
, btrace
->blocks
);
826 last_insn
= VEC_last (btrace_insn_s
, last_bfun
->insn
);
828 /* If the current PC at the end of the block is the same as in our current
829 trace, there are two explanations:
830 1. we executed the instruction and some branch brought us back.
831 2. we have not made any progress.
832 In the first case, the delta trace vector should contain at least two
834 In the second case, the delta trace vector should contain exactly one
835 entry for the partial block containing the current PC. Remove it. */
836 if (first_new_block
->end
== last_insn
->pc
837 && VEC_length (btrace_block_s
, btrace
->blocks
) == 1)
839 VEC_pop (btrace_block_s
, btrace
->blocks
);
843 DEBUG ("stitching %s to %s", ftrace_print_insn_addr (last_insn
),
844 core_addr_to_string_nz (first_new_block
->end
));
846 /* Do a simple sanity check to make sure we don't accidentally end up
847 with a bad block. This should not occur in practice. */
848 if (first_new_block
->end
< last_insn
->pc
)
850 warning (_("Error while trying to read delta trace. Falling back to "
855 /* We adjust the last block to start at the end of our current trace. */
856 gdb_assert (first_new_block
->begin
== 0);
857 first_new_block
->begin
= last_insn
->pc
;
859 /* We simply pop the last insn so we can insert it again as part of
860 the normal branch trace computation.
861 Since instruction iterators are based on indices in the instructions
862 vector, we don't leave any pointers dangling. */
863 DEBUG ("pruning insn at %s for stitching",
864 ftrace_print_insn_addr (last_insn
));
866 VEC_pop (btrace_insn_s
, last_bfun
->insn
);
868 /* The instructions vector may become empty temporarily if this has
869 been the only instruction in this function segment.
870 This violates the invariant but will be remedied shortly by
871 btrace_compute_ftrace when we add the new trace. */
875 /* Adjust the block trace in order to stitch old and new trace together.
876 BTRACE is the new delta trace between the last and the current stop.
877 BTINFO is the old branch trace until the last stop.
878 May modifx BTRACE as well as the existing trace in BTINFO.
879 Return 0 on success, -1 otherwise. */
882 btrace_stitch_trace (struct btrace_data
*btrace
,
883 const struct btrace_thread_info
*btinfo
)
885 /* If we don't have trace, there's nothing to do. */
886 if (btrace_data_empty (btrace
))
889 switch (btrace
->format
)
891 case BTRACE_FORMAT_NONE
:
894 case BTRACE_FORMAT_BTS
:
895 return btrace_stitch_bts (&btrace
->variant
.bts
, btinfo
);
898 internal_error (__FILE__
, __LINE__
, _("Unkown branch trace format."));
901 /* Clear the branch trace histories in BTINFO. */
904 btrace_clear_history (struct btrace_thread_info
*btinfo
)
906 xfree (btinfo
->insn_history
);
907 xfree (btinfo
->call_history
);
908 xfree (btinfo
->replay
);
910 btinfo
->insn_history
= NULL
;
911 btinfo
->call_history
= NULL
;
912 btinfo
->replay
= NULL
;
918 btrace_fetch (struct thread_info
*tp
)
920 struct btrace_thread_info
*btinfo
;
921 struct btrace_target_info
*tinfo
;
922 struct btrace_data btrace
;
923 struct cleanup
*cleanup
;
926 DEBUG ("fetch thread %d (%s)", tp
->num
, target_pid_to_str (tp
->ptid
));
928 btinfo
= &tp
->btrace
;
929 tinfo
= btinfo
->target
;
933 /* There's no way we could get new trace while replaying.
934 On the other hand, delta trace would return a partial record with the
935 current PC, which is the replay PC, not the last PC, as expected. */
936 if (btinfo
->replay
!= NULL
)
939 btrace_data_init (&btrace
);
940 cleanup
= make_cleanup_btrace_data (&btrace
);
942 /* Let's first try to extend the trace we already have. */
943 if (btinfo
->end
!= NULL
)
945 errcode
= target_read_btrace (&btrace
, tinfo
, BTRACE_READ_DELTA
);
948 /* Success. Let's try to stitch the traces together. */
949 errcode
= btrace_stitch_trace (&btrace
, btinfo
);
953 /* We failed to read delta trace. Let's try to read new trace. */
954 errcode
= target_read_btrace (&btrace
, tinfo
, BTRACE_READ_NEW
);
956 /* If we got any new trace, discard what we have. */
957 if (errcode
== 0 && !btrace_data_empty (&btrace
))
961 /* If we were not able to read the trace, we start over. */
965 errcode
= target_read_btrace (&btrace
, tinfo
, BTRACE_READ_ALL
);
969 errcode
= target_read_btrace (&btrace
, tinfo
, BTRACE_READ_ALL
);
971 /* If we were not able to read the branch trace, signal an error. */
973 error (_("Failed to read branch trace."));
975 /* Compute the trace, provided we have any. */
976 if (!btrace_data_empty (&btrace
))
978 btrace_clear_history (btinfo
);
979 btrace_compute_ftrace (tp
, &btrace
);
982 do_cleanups (cleanup
);
988 btrace_clear (struct thread_info
*tp
)
990 struct btrace_thread_info
*btinfo
;
991 struct btrace_function
*it
, *trash
;
993 DEBUG ("clear thread %d (%s)", tp
->num
, target_pid_to_str (tp
->ptid
));
995 /* Make sure btrace frames that may hold a pointer into the branch
996 trace data are destroyed. */
997 reinit_frame_cache ();
999 btinfo
= &tp
->btrace
;
1010 btinfo
->begin
= NULL
;
1013 btrace_clear_history (btinfo
);
1019 btrace_free_objfile (struct objfile
*objfile
)
1021 struct thread_info
*tp
;
1023 DEBUG ("free objfile");
1025 ALL_NON_EXITED_THREADS (tp
)
1029 #if defined (HAVE_LIBEXPAT)
1031 /* Check the btrace document version. */
1034 check_xml_btrace_version (struct gdb_xml_parser
*parser
,
1035 const struct gdb_xml_element
*element
,
1036 void *user_data
, VEC (gdb_xml_value_s
) *attributes
)
1038 const char *version
= xml_find_attribute (attributes
, "version")->value
;
1040 if (strcmp (version
, "1.0") != 0)
1041 gdb_xml_error (parser
, _("Unsupported btrace version: \"%s\""), version
);
1044 /* Parse a btrace "block" xml record. */
1047 parse_xml_btrace_block (struct gdb_xml_parser
*parser
,
1048 const struct gdb_xml_element
*element
,
1049 void *user_data
, VEC (gdb_xml_value_s
) *attributes
)
1051 struct btrace_data
*btrace
;
1052 struct btrace_block
*block
;
1053 ULONGEST
*begin
, *end
;
1057 switch (btrace
->format
)
1059 case BTRACE_FORMAT_BTS
:
1062 case BTRACE_FORMAT_NONE
:
1063 btrace
->format
= BTRACE_FORMAT_BTS
;
1064 btrace
->variant
.bts
.blocks
= NULL
;
1068 gdb_xml_error (parser
, _("Btrace format error."));
1071 begin
= xml_find_attribute (attributes
, "begin")->value
;
1072 end
= xml_find_attribute (attributes
, "end")->value
;
1074 block
= VEC_safe_push (btrace_block_s
, btrace
->variant
.bts
.blocks
, NULL
);
1075 block
->begin
= *begin
;
1079 static const struct gdb_xml_attribute block_attributes
[] = {
1080 { "begin", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
1081 { "end", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
1082 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
1085 static const struct gdb_xml_attribute btrace_attributes
[] = {
1086 { "version", GDB_XML_AF_NONE
, NULL
, NULL
},
1087 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
1090 static const struct gdb_xml_element btrace_children
[] = {
1091 { "block", block_attributes
, NULL
,
1092 GDB_XML_EF_REPEATABLE
| GDB_XML_EF_OPTIONAL
, parse_xml_btrace_block
, NULL
},
1093 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
1096 static const struct gdb_xml_element btrace_elements
[] = {
1097 { "btrace", btrace_attributes
, btrace_children
, GDB_XML_EF_NONE
,
1098 check_xml_btrace_version
, NULL
},
1099 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
1102 #endif /* defined (HAVE_LIBEXPAT) */
1107 parse_xml_btrace (struct btrace_data
*btrace
, const char *buffer
)
1109 struct cleanup
*cleanup
;
1112 #if defined (HAVE_LIBEXPAT)
1114 btrace
->format
= BTRACE_FORMAT_NONE
;
1116 cleanup
= make_cleanup_btrace_data (btrace
);
1117 errcode
= gdb_xml_parse_quick (_("btrace"), "btrace.dtd", btrace_elements
,
1120 error (_("Error parsing branch trace."));
1122 /* Keep parse results. */
1123 discard_cleanups (cleanup
);
1125 #else /* !defined (HAVE_LIBEXPAT) */
1127 error (_("Cannot process branch trace. XML parsing is not supported."));
1129 #endif /* !defined (HAVE_LIBEXPAT) */
1132 #if defined (HAVE_LIBEXPAT)
1134 /* Parse a btrace-conf "bts" xml record. */
1137 parse_xml_btrace_conf_bts (struct gdb_xml_parser
*parser
,
1138 const struct gdb_xml_element
*element
,
1139 void *user_data
, VEC (gdb_xml_value_s
) *attributes
)
1141 struct btrace_config
*conf
;
1142 struct gdb_xml_value
*size
;
1145 conf
->format
= BTRACE_FORMAT_BTS
;
1148 size
= xml_find_attribute (attributes
, "size");
1150 conf
->bts
.size
= (unsigned int) * (ULONGEST
*) size
->value
;
1153 static const struct gdb_xml_attribute btrace_conf_bts_attributes
[] = {
1154 { "size", GDB_XML_AF_OPTIONAL
, gdb_xml_parse_attr_ulongest
, NULL
},
1155 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
1158 static const struct gdb_xml_element btrace_conf_children
[] = {
1159 { "bts", btrace_conf_bts_attributes
, NULL
, GDB_XML_EF_OPTIONAL
,
1160 parse_xml_btrace_conf_bts
, NULL
},
1161 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
1164 static const struct gdb_xml_attribute btrace_conf_attributes
[] = {
1165 { "version", GDB_XML_AF_NONE
, NULL
, NULL
},
1166 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
1169 static const struct gdb_xml_element btrace_conf_elements
[] = {
1170 { "btrace-conf", btrace_conf_attributes
, btrace_conf_children
,
1171 GDB_XML_EF_NONE
, NULL
, NULL
},
1172 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
1175 #endif /* defined (HAVE_LIBEXPAT) */
1180 parse_xml_btrace_conf (struct btrace_config
*conf
, const char *xml
)
1184 #if defined (HAVE_LIBEXPAT)
1186 errcode
= gdb_xml_parse_quick (_("btrace-conf"), "btrace-conf.dtd",
1187 btrace_conf_elements
, xml
, conf
);
1189 error (_("Error parsing branch trace configuration."));
1191 #else /* !defined (HAVE_LIBEXPAT) */
1193 error (_("XML parsing is not supported."));
1195 #endif /* !defined (HAVE_LIBEXPAT) */
1200 const struct btrace_insn
*
1201 btrace_insn_get (const struct btrace_insn_iterator
*it
)
1203 const struct btrace_function
*bfun
;
1204 unsigned int index
, end
;
1207 bfun
= it
->function
;
1209 /* The index is within the bounds of this function's instruction vector. */
1210 end
= VEC_length (btrace_insn_s
, bfun
->insn
);
1211 gdb_assert (0 < end
);
1212 gdb_assert (index
< end
);
1214 return VEC_index (btrace_insn_s
, bfun
->insn
, index
);
1220 btrace_insn_number (const struct btrace_insn_iterator
*it
)
1222 const struct btrace_function
*bfun
;
1224 bfun
= it
->function
;
1225 return bfun
->insn_offset
+ it
->index
;
1231 btrace_insn_begin (struct btrace_insn_iterator
*it
,
1232 const struct btrace_thread_info
*btinfo
)
1234 const struct btrace_function
*bfun
;
1236 bfun
= btinfo
->begin
;
1238 error (_("No trace."));
1240 it
->function
= bfun
;
1247 btrace_insn_end (struct btrace_insn_iterator
*it
,
1248 const struct btrace_thread_info
*btinfo
)
1250 const struct btrace_function
*bfun
;
1251 unsigned int length
;
1255 error (_("No trace."));
1257 /* The last instruction in the last function is the current instruction.
1258 We point to it - it is one past the end of the execution trace. */
1259 length
= VEC_length (btrace_insn_s
, bfun
->insn
);
1261 it
->function
= bfun
;
1262 it
->index
= length
- 1;
1268 btrace_insn_next (struct btrace_insn_iterator
*it
, unsigned int stride
)
1270 const struct btrace_function
*bfun
;
1271 unsigned int index
, steps
;
1273 bfun
= it
->function
;
1279 unsigned int end
, space
, adv
;
1281 end
= VEC_length (btrace_insn_s
, bfun
->insn
);
1283 gdb_assert (0 < end
);
1284 gdb_assert (index
< end
);
1286 /* Compute the number of instructions remaining in this segment. */
1287 space
= end
- index
;
1289 /* Advance the iterator as far as possible within this segment. */
1290 adv
= min (space
, stride
);
1295 /* Move to the next function if we're at the end of this one. */
1298 const struct btrace_function
*next
;
1300 next
= bfun
->flow
.next
;
1303 /* We stepped past the last function.
1305 Let's adjust the index to point to the last instruction in
1306 the previous function. */
1312 /* We now point to the first instruction in the new function. */
1317 /* We did make progress. */
1318 gdb_assert (adv
> 0);
1321 /* Update the iterator. */
1322 it
->function
= bfun
;
1331 btrace_insn_prev (struct btrace_insn_iterator
*it
, unsigned int stride
)
1333 const struct btrace_function
*bfun
;
1334 unsigned int index
, steps
;
1336 bfun
= it
->function
;
1344 /* Move to the previous function if we're at the start of this one. */
1347 const struct btrace_function
*prev
;
1349 prev
= bfun
->flow
.prev
;
1353 /* We point to one after the last instruction in the new function. */
1355 index
= VEC_length (btrace_insn_s
, bfun
->insn
);
1357 /* There is at least one instruction in this function segment. */
1358 gdb_assert (index
> 0);
1361 /* Advance the iterator as far as possible within this segment. */
1362 adv
= min (index
, stride
);
1367 /* We did make progress. */
1368 gdb_assert (adv
> 0);
1371 /* Update the iterator. */
1372 it
->function
= bfun
;
1381 btrace_insn_cmp (const struct btrace_insn_iterator
*lhs
,
1382 const struct btrace_insn_iterator
*rhs
)
1384 unsigned int lnum
, rnum
;
1386 lnum
= btrace_insn_number (lhs
);
1387 rnum
= btrace_insn_number (rhs
);
1389 return (int) (lnum
- rnum
);
1395 btrace_find_insn_by_number (struct btrace_insn_iterator
*it
,
1396 const struct btrace_thread_info
*btinfo
,
1397 unsigned int number
)
1399 const struct btrace_function
*bfun
;
1402 for (bfun
= btinfo
->end
; bfun
!= NULL
; bfun
= bfun
->flow
.prev
)
1403 if (bfun
->insn_offset
<= number
)
1409 end
= bfun
->insn_offset
+ VEC_length (btrace_insn_s
, bfun
->insn
);
1413 it
->function
= bfun
;
1414 it
->index
= number
- bfun
->insn_offset
;
1421 const struct btrace_function
*
1422 btrace_call_get (const struct btrace_call_iterator
*it
)
1424 return it
->function
;
1430 btrace_call_number (const struct btrace_call_iterator
*it
)
1432 const struct btrace_thread_info
*btinfo
;
1433 const struct btrace_function
*bfun
;
1436 btinfo
= it
->btinfo
;
1437 bfun
= it
->function
;
1439 return bfun
->number
;
1441 /* For the end iterator, i.e. bfun == NULL, we return one more than the
1442 number of the last function. */
1444 insns
= VEC_length (btrace_insn_s
, bfun
->insn
);
1446 /* If the function contains only a single instruction (i.e. the current
1447 instruction), it will be skipped and its number is already the number
1450 return bfun
->number
;
1452 /* Otherwise, return one more than the number of the last function. */
1453 return bfun
->number
+ 1;
1459 btrace_call_begin (struct btrace_call_iterator
*it
,
1460 const struct btrace_thread_info
*btinfo
)
1462 const struct btrace_function
*bfun
;
1464 bfun
= btinfo
->begin
;
1466 error (_("No trace."));
1468 it
->btinfo
= btinfo
;
1469 it
->function
= bfun
;
1475 btrace_call_end (struct btrace_call_iterator
*it
,
1476 const struct btrace_thread_info
*btinfo
)
1478 const struct btrace_function
*bfun
;
1482 error (_("No trace."));
1484 it
->btinfo
= btinfo
;
1485 it
->function
= NULL
;
1491 btrace_call_next (struct btrace_call_iterator
*it
, unsigned int stride
)
1493 const struct btrace_function
*bfun
;
1496 bfun
= it
->function
;
1498 while (bfun
!= NULL
)
1500 const struct btrace_function
*next
;
1503 next
= bfun
->flow
.next
;
1506 /* Ignore the last function if it only contains a single
1507 (i.e. the current) instruction. */
1508 insns
= VEC_length (btrace_insn_s
, bfun
->insn
);
1513 if (stride
== steps
)
1520 it
->function
= bfun
;
1527 btrace_call_prev (struct btrace_call_iterator
*it
, unsigned int stride
)
1529 const struct btrace_thread_info
*btinfo
;
1530 const struct btrace_function
*bfun
;
1533 bfun
= it
->function
;
1540 btinfo
= it
->btinfo
;
1545 /* Ignore the last function if it only contains a single
1546 (i.e. the current) instruction. */
1547 insns
= VEC_length (btrace_insn_s
, bfun
->insn
);
1549 bfun
= bfun
->flow
.prev
;
1557 while (steps
< stride
)
1559 const struct btrace_function
*prev
;
1561 prev
= bfun
->flow
.prev
;
1569 it
->function
= bfun
;
1576 btrace_call_cmp (const struct btrace_call_iterator
*lhs
,
1577 const struct btrace_call_iterator
*rhs
)
1579 unsigned int lnum
, rnum
;
1581 lnum
= btrace_call_number (lhs
);
1582 rnum
= btrace_call_number (rhs
);
1584 return (int) (lnum
- rnum
);
1590 btrace_find_call_by_number (struct btrace_call_iterator
*it
,
1591 const struct btrace_thread_info
*btinfo
,
1592 unsigned int number
)
1594 const struct btrace_function
*bfun
;
1596 for (bfun
= btinfo
->end
; bfun
!= NULL
; bfun
= bfun
->flow
.prev
)
1600 bnum
= bfun
->number
;
1603 it
->btinfo
= btinfo
;
1604 it
->function
= bfun
;
1608 /* Functions are ordered and numbered consecutively. We could bail out
1609 earlier. On the other hand, it is very unlikely that we search for
1610 a nonexistent function. */
1619 btrace_set_insn_history (struct btrace_thread_info
*btinfo
,
1620 const struct btrace_insn_iterator
*begin
,
1621 const struct btrace_insn_iterator
*end
)
1623 if (btinfo
->insn_history
== NULL
)
1624 btinfo
->insn_history
= xzalloc (sizeof (*btinfo
->insn_history
));
1626 btinfo
->insn_history
->begin
= *begin
;
1627 btinfo
->insn_history
->end
= *end
;
1633 btrace_set_call_history (struct btrace_thread_info
*btinfo
,
1634 const struct btrace_call_iterator
*begin
,
1635 const struct btrace_call_iterator
*end
)
1637 gdb_assert (begin
->btinfo
== end
->btinfo
);
1639 if (btinfo
->call_history
== NULL
)
1640 btinfo
->call_history
= xzalloc (sizeof (*btinfo
->call_history
));
1642 btinfo
->call_history
->begin
= *begin
;
1643 btinfo
->call_history
->end
= *end
;
1649 btrace_is_replaying (struct thread_info
*tp
)
1651 return tp
->btrace
.replay
!= NULL
;
1657 btrace_is_empty (struct thread_info
*tp
)
1659 struct btrace_insn_iterator begin
, end
;
1660 struct btrace_thread_info
*btinfo
;
1662 btinfo
= &tp
->btrace
;
1664 if (btinfo
->begin
== NULL
)
1667 btrace_insn_begin (&begin
, btinfo
);
1668 btrace_insn_end (&end
, btinfo
);
1670 return btrace_insn_cmp (&begin
, &end
) == 0;
1673 /* Forward the cleanup request. */
1676 do_btrace_data_cleanup (void *arg
)
1678 btrace_data_fini (arg
);
1684 make_cleanup_btrace_data (struct btrace_data
*data
)
1686 return make_cleanup (do_btrace_data_cleanup
, data
);