1 /* Branch trace support for GDB, the GNU debugger.
3 Copyright (C) 2013-2017 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"
36 #include "cli/cli-utils.h"
42 /* Command lists for btrace maintenance commands. */
43 static struct cmd_list_element *maint_btrace_cmdlist;
44 static struct cmd_list_element *maint_btrace_set_cmdlist;
45 static struct cmd_list_element *maint_btrace_show_cmdlist;
46 static struct cmd_list_element *maint_btrace_pt_set_cmdlist;
47 static struct cmd_list_element *maint_btrace_pt_show_cmdlist;
49 /* Control whether to skip PAD packets when computing the packet history. */
50 static int maint_btrace_pt_skip_pad = 1;
52 /* A vector of function segments. */
53 typedef struct btrace_function * bfun_s;
56 static void btrace_add_pc (struct thread_info *tp);
58 /* Print a record debug message. Use do ... while (0) to avoid ambiguities
59 when used in if statements. */
61 #define DEBUG(msg, args...) \
64 if (record_debug != 0) \
65 fprintf_unfiltered (gdb_stdlog, \
66 "[btrace] " msg "\n", ##args); \
70 #define DEBUG_FTRACE(msg, args...) DEBUG ("[ftrace] " msg, ##args)
72 /* Return the function name of a recorded function segment for printing.
73 This function never returns NULL. */
76 ftrace_print_function_name (const struct btrace_function *bfun)
78 struct minimal_symbol *msym;
85 return SYMBOL_PRINT_NAME (sym);
88 return MSYMBOL_PRINT_NAME (msym);
93 /* Return the file name of a recorded function segment for printing.
94 This function never returns NULL. */
97 ftrace_print_filename (const struct btrace_function *bfun)
100 const char *filename;
105 filename = symtab_to_filename_for_display (symbol_symtab (sym));
107 filename = "<unknown>";
112 /* Return a string representation of the address of an instruction.
113 This function never returns NULL. */
116 ftrace_print_insn_addr (const struct btrace_insn *insn)
121 return core_addr_to_string_nz (insn->pc);
124 /* Print an ftrace debug status message. */
127 ftrace_debug (const struct btrace_function *bfun, const char *prefix)
129 const char *fun, *file;
130 unsigned int ibegin, iend;
133 fun = ftrace_print_function_name (bfun);
134 file = ftrace_print_filename (bfun);
137 ibegin = bfun->insn_offset;
138 iend = ibegin + VEC_length (btrace_insn_s, bfun->insn);
140 DEBUG_FTRACE ("%s: fun = %s, file = %s, level = %d, insn = [%u; %u)",
141 prefix, fun, file, level, ibegin, iend);
144 /* Return the number of instructions in a given function call segment. */
147 ftrace_call_num_insn (const struct btrace_function* bfun)
152 /* A gap is always counted as one instruction. */
153 if (bfun->errcode != 0)
156 return VEC_length (btrace_insn_s, bfun->insn);
159 /* Return non-zero if BFUN does not match MFUN and FUN,
160 return zero otherwise. */
163 ftrace_function_switched (const struct btrace_function *bfun,
164 const struct minimal_symbol *mfun,
165 const struct symbol *fun)
167 struct minimal_symbol *msym;
173 /* If the minimal symbol changed, we certainly switched functions. */
174 if (mfun != NULL && msym != NULL
175 && strcmp (MSYMBOL_LINKAGE_NAME (mfun), MSYMBOL_LINKAGE_NAME (msym)) != 0)
178 /* If the symbol changed, we certainly switched functions. */
179 if (fun != NULL && sym != NULL)
181 const char *bfname, *fname;
183 /* Check the function name. */
184 if (strcmp (SYMBOL_LINKAGE_NAME (fun), SYMBOL_LINKAGE_NAME (sym)) != 0)
187 /* Check the location of those functions, as well. */
188 bfname = symtab_to_fullname (symbol_symtab (sym));
189 fname = symtab_to_fullname (symbol_symtab (fun));
190 if (filename_cmp (fname, bfname) != 0)
194 /* If we lost symbol information, we switched functions. */
195 if (!(msym == NULL && sym == NULL) && mfun == NULL && fun == NULL)
198 /* If we gained symbol information, we switched functions. */
199 if (msym == NULL && sym == NULL && !(mfun == NULL && fun == NULL))
205 /* Allocate and initialize a new branch trace function segment.
206 BTINFO is the branch trace information for the current thread.
207 PREV is the chronologically preceding function segment.
208 MFUN and FUN are the symbol information we have for this function. */
210 static struct btrace_function *
211 ftrace_new_function (struct btrace_thread_info *btinfo,
212 struct btrace_function *prev,
213 struct minimal_symbol *mfun,
216 struct btrace_function *bfun;
218 bfun = XCNEW (struct btrace_function);
222 bfun->flow.prev = prev;
226 /* Start counting at one. */
228 bfun->insn_offset = 1;
232 gdb_assert (prev->flow.next == NULL);
233 prev->flow.next = bfun;
235 bfun->number = prev->number + 1;
236 bfun->insn_offset = prev->insn_offset + ftrace_call_num_insn (prev);
237 bfun->level = prev->level;
240 btinfo->functions.push_back (bfun);
244 /* Update the UP field of a function segment. */
247 ftrace_update_caller (struct btrace_function *bfun,
248 struct btrace_function *caller,
249 enum btrace_function_flag flags)
251 if (bfun->up != NULL)
252 ftrace_debug (bfun, "updating caller");
257 ftrace_debug (bfun, "set caller");
258 ftrace_debug (caller, "..to");
261 /* Fix up the caller for all segments of a function. */
264 ftrace_fixup_caller (struct btrace_function *bfun,
265 struct btrace_function *caller,
266 enum btrace_function_flag flags)
268 struct btrace_function *prev, *next;
270 ftrace_update_caller (bfun, caller, flags);
272 /* Update all function segments belonging to the same function. */
273 for (prev = bfun->segment.prev; prev != NULL; prev = prev->segment.prev)
274 ftrace_update_caller (prev, caller, flags);
276 for (next = bfun->segment.next; next != NULL; next = next->segment.next)
277 ftrace_update_caller (next, caller, flags);
280 /* Add a new function segment for a call.
281 BTINFO is the branch trace information for the current thread.
282 CALLER is the chronologically preceding function segment.
283 MFUN and FUN are the symbol information we have for this function. */
285 static struct btrace_function *
286 ftrace_new_call (struct btrace_thread_info *btinfo,
287 struct btrace_function *caller,
288 struct minimal_symbol *mfun,
291 struct btrace_function *bfun;
293 bfun = ftrace_new_function (btinfo, caller, mfun, fun);
297 ftrace_debug (bfun, "new call");
302 /* Add a new function segment for a tail call.
303 BTINFO is the branch trace information for the current thread.
304 CALLER is the chronologically preceding function segment.
305 MFUN and FUN are the symbol information we have for this function. */
307 static struct btrace_function *
308 ftrace_new_tailcall (struct btrace_thread_info *btinfo,
309 struct btrace_function *caller,
310 struct minimal_symbol *mfun,
313 struct btrace_function *bfun;
315 bfun = ftrace_new_function (btinfo, caller, mfun, fun);
318 bfun->flags |= BFUN_UP_LINKS_TO_TAILCALL;
320 ftrace_debug (bfun, "new tail call");
325 /* Return the caller of BFUN or NULL if there is none. This function skips
326 tail calls in the call chain. */
327 static struct btrace_function *
328 ftrace_get_caller (struct btrace_function *bfun)
330 for (; bfun != NULL; bfun = bfun->up)
331 if ((bfun->flags & BFUN_UP_LINKS_TO_TAILCALL) == 0)
337 /* Find the innermost caller in the back trace of BFUN with MFUN/FUN
338 symbol information. */
340 static struct btrace_function *
341 ftrace_find_caller (struct btrace_function *bfun,
342 struct minimal_symbol *mfun,
345 for (; bfun != NULL; bfun = bfun->up)
347 /* Skip functions with incompatible symbol information. */
348 if (ftrace_function_switched (bfun, mfun, fun))
351 /* This is the function segment we're looking for. */
358 /* Find the innermost caller in the back trace of BFUN, skipping all
359 function segments that do not end with a call instruction (e.g.
360 tail calls ending with a jump). */
362 static struct btrace_function *
363 ftrace_find_call (struct btrace_function *bfun)
365 for (; bfun != NULL; bfun = bfun->up)
367 struct btrace_insn *last;
370 if (bfun->errcode != 0)
373 last = VEC_last (btrace_insn_s, bfun->insn);
375 if (last->iclass == BTRACE_INSN_CALL)
382 /* Add a continuation segment for a function into which we return.
383 BTINFO is the branch trace information for the current thread.
384 PREV is the chronologically preceding function segment.
385 MFUN and FUN are the symbol information we have for this function. */
387 static struct btrace_function *
388 ftrace_new_return (struct btrace_thread_info *btinfo,
389 struct btrace_function *prev,
390 struct minimal_symbol *mfun,
393 struct btrace_function *bfun, *caller;
395 bfun = ftrace_new_function (btinfo, prev, mfun, fun);
397 /* It is important to start at PREV's caller. Otherwise, we might find
398 PREV itself, if PREV is a recursive function. */
399 caller = ftrace_find_caller (prev->up, mfun, fun);
402 /* The caller of PREV is the preceding btrace function segment in this
403 function instance. */
404 gdb_assert (caller->segment.next == NULL);
406 caller->segment.next = bfun;
407 bfun->segment.prev = caller;
409 /* Maintain the function level. */
410 bfun->level = caller->level;
412 /* Maintain the call stack. */
413 bfun->up = caller->up;
414 bfun->flags = caller->flags;
416 ftrace_debug (bfun, "new return");
420 /* We did not find a caller. This could mean that something went
421 wrong or that the call is simply not included in the trace. */
423 /* Let's search for some actual call. */
424 caller = ftrace_find_call (prev->up);
427 /* There is no call in PREV's back trace. We assume that the
428 branch trace did not include it. */
430 /* Let's find the topmost function and add a new caller for it.
431 This should handle a series of initial tail calls. */
432 while (prev->up != NULL)
435 bfun->level = prev->level - 1;
437 /* Fix up the call stack for PREV. */
438 ftrace_fixup_caller (prev, bfun, BFUN_UP_LINKS_TO_RET);
440 ftrace_debug (bfun, "new return - no caller");
444 /* There is a call in PREV's back trace to which we should have
445 returned but didn't. Let's start a new, separate back trace
446 from PREV's level. */
447 bfun->level = prev->level - 1;
449 /* We fix up the back trace for PREV but leave other function segments
450 on the same level as they are.
451 This should handle things like schedule () correctly where we're
452 switching contexts. */
454 prev->flags = BFUN_UP_LINKS_TO_RET;
456 ftrace_debug (bfun, "new return - unknown caller");
463 /* Add a new function segment for a function switch.
464 BTINFO is the branch trace information for the current thread.
465 PREV is the chronologically preceding function segment.
466 MFUN and FUN are the symbol information we have for this function. */
468 static struct btrace_function *
469 ftrace_new_switch (struct btrace_thread_info *btinfo,
470 struct btrace_function *prev,
471 struct minimal_symbol *mfun,
474 struct btrace_function *bfun;
476 /* This is an unexplained function switch. We can't really be sure about the
477 call stack, yet the best I can think of right now is to preserve it. */
478 bfun = ftrace_new_function (btinfo, prev, mfun, fun);
480 bfun->flags = prev->flags;
482 ftrace_debug (bfun, "new switch");
487 /* Add a new function segment for a gap in the trace due to a decode error.
488 BTINFO is the branch trace information for the current thread.
489 PREV is the chronologically preceding function segment.
490 ERRCODE is the format-specific error code. */
492 static struct btrace_function *
493 ftrace_new_gap (struct btrace_thread_info *btinfo,
494 struct btrace_function *prev, int errcode)
496 struct btrace_function *bfun;
498 /* We hijack prev if it was empty. */
499 if (prev != NULL && prev->errcode == 0
500 && VEC_empty (btrace_insn_s, prev->insn))
503 bfun = ftrace_new_function (btinfo, prev, NULL, NULL);
505 bfun->errcode = errcode;
507 ftrace_debug (bfun, "new gap");
512 /* Update BFUN with respect to the instruction at PC. BTINFO is the branch
513 trace information for the current thread. This may create new function
515 Return the chronologically latest function segment, never NULL. */
517 static struct btrace_function *
518 ftrace_update_function (struct btrace_thread_info *btinfo,
519 struct btrace_function *bfun, CORE_ADDR pc)
521 struct bound_minimal_symbol bmfun;
522 struct minimal_symbol *mfun;
524 struct btrace_insn *last;
526 /* Try to determine the function we're in. We use both types of symbols
527 to avoid surprises when we sometimes get a full symbol and sometimes
528 only a minimal symbol. */
529 fun = find_pc_function (pc);
530 bmfun = lookup_minimal_symbol_by_pc (pc);
533 if (fun == NULL && mfun == NULL)
534 DEBUG_FTRACE ("no symbol at %s", core_addr_to_string_nz (pc));
536 /* If we didn't have a function or if we had a gap before, we create one. */
537 if (bfun == NULL || bfun->errcode != 0)
538 return ftrace_new_function (btinfo, bfun, mfun, fun);
540 /* Check the last instruction, if we have one.
541 We do this check first, since it allows us to fill in the call stack
542 links in addition to the normal flow links. */
544 if (!VEC_empty (btrace_insn_s, bfun->insn))
545 last = VEC_last (btrace_insn_s, bfun->insn);
549 switch (last->iclass)
551 case BTRACE_INSN_RETURN:
555 /* On some systems, _dl_runtime_resolve returns to the resolved
556 function instead of jumping to it. From our perspective,
557 however, this is a tailcall.
558 If we treated it as return, we wouldn't be able to find the
559 resolved function in our stack back trace. Hence, we would
560 lose the current stack back trace and start anew with an empty
561 back trace. When the resolved function returns, we would then
562 create a stack back trace with the same function names but
563 different frame id's. This will confuse stepping. */
564 fname = ftrace_print_function_name (bfun);
565 if (strcmp (fname, "_dl_runtime_resolve") == 0)
566 return ftrace_new_tailcall (btinfo, bfun, mfun, fun);
568 return ftrace_new_return (btinfo, bfun, mfun, fun);
571 case BTRACE_INSN_CALL:
572 /* Ignore calls to the next instruction. They are used for PIC. */
573 if (last->pc + last->size == pc)
576 return ftrace_new_call (btinfo, bfun, mfun, fun);
578 case BTRACE_INSN_JUMP:
582 start = get_pc_function_start (pc);
584 /* A jump to the start of a function is (typically) a tail call. */
586 return ftrace_new_tailcall (btinfo, bfun, mfun, fun);
588 /* If we can't determine the function for PC, we treat a jump at
589 the end of the block as tail call if we're switching functions
590 and as an intra-function branch if we don't. */
591 if (start == 0 && ftrace_function_switched (bfun, mfun, fun))
592 return ftrace_new_tailcall (btinfo, bfun, mfun, fun);
599 /* Check if we're switching functions for some other reason. */
600 if (ftrace_function_switched (bfun, mfun, fun))
602 DEBUG_FTRACE ("switching from %s in %s at %s",
603 ftrace_print_insn_addr (last),
604 ftrace_print_function_name (bfun),
605 ftrace_print_filename (bfun));
607 return ftrace_new_switch (btinfo, bfun, mfun, fun);
613 /* Add the instruction at PC to BFUN's instructions. */
616 ftrace_update_insns (struct btrace_function *bfun,
617 const struct btrace_insn *insn)
619 VEC_safe_push (btrace_insn_s, bfun->insn, insn);
621 if (record_debug > 1)
622 ftrace_debug (bfun, "update insn");
625 /* Classify the instruction at PC. */
627 static enum btrace_insn_class
628 ftrace_classify_insn (struct gdbarch *gdbarch, CORE_ADDR pc)
630 enum btrace_insn_class iclass;
632 iclass = BTRACE_INSN_OTHER;
635 if (gdbarch_insn_is_call (gdbarch, pc))
636 iclass = BTRACE_INSN_CALL;
637 else if (gdbarch_insn_is_ret (gdbarch, pc))
638 iclass = BTRACE_INSN_RETURN;
639 else if (gdbarch_insn_is_jump (gdbarch, pc))
640 iclass = BTRACE_INSN_JUMP;
642 CATCH (error, RETURN_MASK_ERROR)
650 /* Try to match the back trace at LHS to the back trace at RHS. Returns the
651 number of matching function segments or zero if the back traces do not
655 ftrace_match_backtrace (struct btrace_function *lhs,
656 struct btrace_function *rhs)
660 for (matches = 0; lhs != NULL && rhs != NULL; ++matches)
662 if (ftrace_function_switched (lhs, rhs->msym, rhs->sym))
665 lhs = ftrace_get_caller (lhs);
666 rhs = ftrace_get_caller (rhs);
672 /* Add ADJUSTMENT to the level of BFUN and succeeding function segments. */
675 ftrace_fixup_level (struct btrace_function *bfun, int adjustment)
680 DEBUG_FTRACE ("fixup level (%+d)", adjustment);
681 ftrace_debug (bfun, "..bfun");
683 for (; bfun != NULL; bfun = bfun->flow.next)
684 bfun->level += adjustment;
687 /* Recompute the global level offset. Traverse the function trace and compute
688 the global level offset as the negative of the minimal function level. */
691 ftrace_compute_global_level_offset (struct btrace_thread_info *btinfo)
693 struct btrace_function *bfun, *end;
699 bfun = btinfo->begin;
703 /* The last function segment contains the current instruction, which is not
704 really part of the trace. If it contains just this one instruction, we
705 stop when we reach it; otherwise, we let the below loop run to the end. */
707 if (VEC_length (btrace_insn_s, end->insn) > 1)
711 for (; bfun != end; bfun = bfun->flow.next)
712 level = std::min (level, bfun->level);
714 DEBUG_FTRACE ("setting global level offset: %d", -level);
715 btinfo->level = -level;
718 /* Connect the function segments PREV and NEXT in a bottom-to-top walk as in
719 ftrace_connect_backtrace. */
722 ftrace_connect_bfun (struct btrace_function *prev,
723 struct btrace_function *next)
725 DEBUG_FTRACE ("connecting...");
726 ftrace_debug (prev, "..prev");
727 ftrace_debug (next, "..next");
729 /* The function segments are not yet connected. */
730 gdb_assert (prev->segment.next == NULL);
731 gdb_assert (next->segment.prev == NULL);
733 prev->segment.next = next;
734 next->segment.prev = prev;
736 /* We may have moved NEXT to a different function level. */
737 ftrace_fixup_level (next, prev->level - next->level);
739 /* If we run out of back trace for one, let's use the other's. */
740 if (prev->up == NULL)
742 if (next->up != NULL)
744 DEBUG_FTRACE ("using next's callers");
745 ftrace_fixup_caller (prev, next->up, next->flags);
748 else if (next->up == NULL)
750 if (prev->up != NULL)
752 DEBUG_FTRACE ("using prev's callers");
753 ftrace_fixup_caller (next, prev->up, prev->flags);
758 /* PREV may have a tailcall caller, NEXT can't. If it does, fixup the up
759 link to add the tail callers to NEXT's back trace.
761 This removes NEXT->UP from NEXT's back trace. It will be added back
762 when connecting NEXT and PREV's callers - provided they exist.
764 If PREV's back trace consists of a series of tail calls without an
765 actual call, there will be no further connection and NEXT's caller will
766 be removed for good. To catch this case, we handle it here and connect
767 the top of PREV's back trace to NEXT's caller. */
768 if ((prev->flags & BFUN_UP_LINKS_TO_TAILCALL) != 0)
770 struct btrace_function *caller;
771 btrace_function_flags flags;
773 /* We checked NEXT->UP above so CALLER can't be NULL. */
777 DEBUG_FTRACE ("adding prev's tail calls to next");
779 ftrace_fixup_caller (next, prev->up, prev->flags);
781 for (prev = prev->up; prev != NULL; prev = prev->up)
783 /* At the end of PREV's back trace, continue with CALLER. */
784 if (prev->up == NULL)
786 DEBUG_FTRACE ("fixing up link for tailcall chain");
787 ftrace_debug (prev, "..top");
788 ftrace_debug (caller, "..up");
790 ftrace_fixup_caller (prev, caller, flags);
792 /* If we skipped any tail calls, this may move CALLER to a
793 different function level.
795 Note that changing CALLER's level is only OK because we
796 know that this is the last iteration of the bottom-to-top
797 walk in ftrace_connect_backtrace.
799 Otherwise we will fix up CALLER's level when we connect it
800 to PREV's caller in the next iteration. */
801 ftrace_fixup_level (caller, prev->level - caller->level - 1);
805 /* There's nothing to do if we find a real call. */
806 if ((prev->flags & BFUN_UP_LINKS_TO_TAILCALL) == 0)
808 DEBUG_FTRACE ("will fix up link in next iteration");
816 /* Connect function segments on the same level in the back trace at LHS and RHS.
817 The back traces at LHS and RHS are expected to match according to
818 ftrace_match_backtrace. */
821 ftrace_connect_backtrace (struct btrace_function *lhs,
822 struct btrace_function *rhs)
824 while (lhs != NULL && rhs != NULL)
826 struct btrace_function *prev, *next;
828 gdb_assert (!ftrace_function_switched (lhs, rhs->msym, rhs->sym));
830 /* Connecting LHS and RHS may change the up link. */
834 lhs = ftrace_get_caller (lhs);
835 rhs = ftrace_get_caller (rhs);
837 ftrace_connect_bfun (prev, next);
841 /* Bridge the gap between two function segments left and right of a gap if their
842 respective back traces match in at least MIN_MATCHES functions.
844 Returns non-zero if the gap could be bridged, zero otherwise. */
847 ftrace_bridge_gap (struct btrace_function *lhs, struct btrace_function *rhs,
850 struct btrace_function *best_l, *best_r, *cand_l, *cand_r;
853 DEBUG_FTRACE ("checking gap at insn %u (req matches: %d)",
854 rhs->insn_offset - 1, min_matches);
860 /* We search the back traces of LHS and RHS for valid connections and connect
861 the two functon segments that give the longest combined back trace. */
863 for (cand_l = lhs; cand_l != NULL; cand_l = ftrace_get_caller (cand_l))
864 for (cand_r = rhs; cand_r != NULL; cand_r = ftrace_get_caller (cand_r))
868 matches = ftrace_match_backtrace (cand_l, cand_r);
869 if (best_matches < matches)
871 best_matches = matches;
877 /* We need at least MIN_MATCHES matches. */
878 gdb_assert (min_matches > 0);
879 if (best_matches < min_matches)
882 DEBUG_FTRACE ("..matches: %d", best_matches);
884 /* We will fix up the level of BEST_R and succeeding function segments such
885 that BEST_R's level matches BEST_L's when we connect BEST_L to BEST_R.
887 This will ignore the level of RHS and following if BEST_R != RHS. I.e. if
888 BEST_R is a successor of RHS in the back trace of RHS (phases 1 and 3).
890 To catch this, we already fix up the level here where we can start at RHS
891 instead of at BEST_R. We will ignore the level fixup when connecting
892 BEST_L to BEST_R as they will already be on the same level. */
893 ftrace_fixup_level (rhs, best_l->level - best_r->level);
895 ftrace_connect_backtrace (best_l, best_r);
900 /* Try to bridge gaps due to overflow or decode errors by connecting the
901 function segments that are separated by the gap. */
904 btrace_bridge_gaps (struct thread_info *tp, VEC (bfun_s) **gaps)
906 VEC (bfun_s) *remaining;
907 struct cleanup *old_chain;
910 DEBUG ("bridge gaps");
913 old_chain = make_cleanup (VEC_cleanup (bfun_s), &remaining);
915 /* We require a minimum amount of matches for bridging a gap. The number of
916 required matches will be lowered with each iteration.
918 The more matches the higher our confidence that the bridging is correct.
919 For big gaps or small traces, however, it may not be feasible to require a
920 high number of matches. */
921 for (min_matches = 5; min_matches > 0; --min_matches)
923 /* Let's try to bridge as many gaps as we can. In some cases, we need to
924 skip a gap and revisit it again after we closed later gaps. */
925 while (!VEC_empty (bfun_s, *gaps))
927 struct btrace_function *gap;
930 for (idx = 0; VEC_iterate (bfun_s, *gaps, idx, gap); ++idx)
932 struct btrace_function *lhs, *rhs;
935 /* We may have a sequence of gaps if we run from one error into
936 the next as we try to re-sync onto the trace stream. Ignore
937 all but the leftmost gap in such a sequence.
939 Also ignore gaps at the beginning of the trace. */
940 lhs = gap->flow.prev;
941 if (lhs == NULL || lhs->errcode != 0)
944 /* Skip gaps to the right. */
945 for (rhs = gap->flow.next; rhs != NULL; rhs = rhs->flow.next)
946 if (rhs->errcode == 0)
949 /* Ignore gaps at the end of the trace. */
953 bridged = ftrace_bridge_gap (lhs, rhs, min_matches);
955 /* Keep track of gaps we were not able to bridge and try again.
956 If we just pushed them to the end of GAPS we would risk an
957 infinite loop in case we simply cannot bridge a gap. */
959 VEC_safe_push (bfun_s, remaining, gap);
962 /* Let's see if we made any progress. */
963 if (VEC_length (bfun_s, remaining) == VEC_length (bfun_s, *gaps))
966 VEC_free (bfun_s, *gaps);
972 /* We get here if either GAPS is empty or if GAPS equals REMAINING. */
973 if (VEC_empty (bfun_s, *gaps))
976 VEC_free (bfun_s, remaining);
979 do_cleanups (old_chain);
981 /* We may omit this in some cases. Not sure it is worth the extra
982 complication, though. */
983 ftrace_compute_global_level_offset (&tp->btrace);
986 /* Compute the function branch trace from BTS trace. */
989 btrace_compute_ftrace_bts (struct thread_info *tp,
990 const struct btrace_data_bts *btrace,
993 struct btrace_thread_info *btinfo;
994 struct btrace_function *begin, *end;
995 struct gdbarch *gdbarch;
999 gdbarch = target_gdbarch ();
1000 btinfo = &tp->btrace;
1001 begin = btinfo->begin;
1003 level = begin != NULL ? -btinfo->level : INT_MAX;
1004 blk = VEC_length (btrace_block_s, btrace->blocks);
1008 btrace_block_s *block;
1013 block = VEC_index (btrace_block_s, btrace->blocks, blk);
1018 struct btrace_insn insn;
1021 /* We should hit the end of the block. Warn if we went too far. */
1022 if (block->end < pc)
1024 /* Indicate the gap in the trace. */
1025 end = ftrace_new_gap (btinfo, end, BDE_BTS_OVERFLOW);
1029 VEC_safe_push (bfun_s, *gaps, end);
1031 warning (_("Recorded trace may be corrupted at instruction "
1032 "%u (pc = %s)."), end->insn_offset - 1,
1033 core_addr_to_string_nz (pc));
1038 end = ftrace_update_function (btinfo, end, pc);
1042 /* Maintain the function level offset.
1043 For all but the last block, we do it here. */
1045 level = std::min (level, end->level);
1050 size = gdb_insn_length (gdbarch, pc);
1052 CATCH (error, RETURN_MASK_ERROR)
1059 insn.iclass = ftrace_classify_insn (gdbarch, pc);
1062 ftrace_update_insns (end, &insn);
1064 /* We're done once we pushed the instruction at the end. */
1065 if (block->end == pc)
1068 /* We can't continue if we fail to compute the size. */
1071 /* Indicate the gap in the trace. We just added INSN so we're
1072 not at the beginning. */
1073 end = ftrace_new_gap (btinfo, end, BDE_BTS_INSN_SIZE);
1075 VEC_safe_push (bfun_s, *gaps, end);
1077 warning (_("Recorded trace may be incomplete at instruction %u "
1078 "(pc = %s)."), end->insn_offset - 1,
1079 core_addr_to_string_nz (pc));
1086 /* Maintain the function level offset.
1087 For the last block, we do it here to not consider the last
1089 Since the last instruction corresponds to the current instruction
1090 and is not really part of the execution history, it shouldn't
1091 affect the level. */
1093 level = std::min (level, end->level);
1097 btinfo->begin = begin;
1100 /* LEVEL is the minimal function level of all btrace function segments.
1101 Define the global level offset to -LEVEL so all function levels are
1102 normalized to start at zero. */
1103 btinfo->level = -level;
1106 #if defined (HAVE_LIBIPT)
1108 static enum btrace_insn_class
1109 pt_reclassify_insn (enum pt_insn_class iclass)
1114 return BTRACE_INSN_CALL;
1117 return BTRACE_INSN_RETURN;
1120 return BTRACE_INSN_JUMP;
1123 return BTRACE_INSN_OTHER;
1127 /* Return the btrace instruction flags for INSN. */
1129 static btrace_insn_flags
1130 pt_btrace_insn_flags (const struct pt_insn &insn)
1132 btrace_insn_flags flags = 0;
1134 if (insn.speculative)
1135 flags |= BTRACE_INSN_FLAG_SPECULATIVE;
1140 /* Return the btrace instruction for INSN. */
1143 pt_btrace_insn (const struct pt_insn &insn)
1145 return {(CORE_ADDR) insn.ip, (gdb_byte) insn.size,
1146 pt_reclassify_insn (insn.iclass),
1147 pt_btrace_insn_flags (insn)};
1151 /* Add function branch trace to BTINFO using DECODER. */
1154 ftrace_add_pt (struct btrace_thread_info *btinfo,
1155 struct pt_insn_decoder *decoder,
1156 struct btrace_function **pbegin,
1157 struct btrace_function **pend, int *plevel,
1158 VEC (bfun_s) **gaps)
1160 struct btrace_function *begin, *end, *upd;
1168 struct pt_insn insn;
1170 errcode = pt_insn_sync_forward (decoder);
1173 if (errcode != -pte_eos)
1174 warning (_("Failed to synchronize onto the Intel Processor "
1175 "Trace stream: %s."), pt_errstr (pt_errcode (errcode)));
1181 errcode = pt_insn_next (decoder, &insn, sizeof(insn));
1185 /* Look for gaps in the trace - unless we're at the beginning. */
1188 /* Tracing is disabled and re-enabled each time we enter the
1189 kernel. Most times, we continue from the same instruction we
1190 stopped before. This is indicated via the RESUMED instruction
1191 flag. The ENABLED instruction flag means that we continued
1192 from some other instruction. Indicate this as a trace gap. */
1195 *pend = end = ftrace_new_gap (btinfo, end, BDE_PT_DISABLED);
1197 VEC_safe_push (bfun_s, *gaps, end);
1199 pt_insn_get_offset (decoder, &offset);
1201 warning (_("Non-contiguous trace at instruction %u (offset "
1202 "= 0x%" PRIx64 ", pc = 0x%" PRIx64 ")."),
1203 end->insn_offset - 1, offset, insn.ip);
1207 /* Indicate trace overflows. */
1210 *pend = end = ftrace_new_gap (btinfo, end, BDE_PT_OVERFLOW);
1212 *pbegin = begin = end;
1214 VEC_safe_push (bfun_s, *gaps, end);
1216 pt_insn_get_offset (decoder, &offset);
1218 warning (_("Overflow at instruction %u (offset = 0x%" PRIx64
1219 ", pc = 0x%" PRIx64 ")."), end->insn_offset - 1,
1223 upd = ftrace_update_function (btinfo, end, insn.ip);
1229 *pbegin = begin = upd;
1232 /* Maintain the function level offset. */
1233 *plevel = std::min (*plevel, end->level);
1235 btrace_insn btinsn = pt_btrace_insn (insn);
1236 ftrace_update_insns (end, &btinsn);
1239 if (errcode == -pte_eos)
1242 /* Indicate the gap in the trace. */
1243 *pend = end = ftrace_new_gap (btinfo, end, errcode);
1245 *pbegin = begin = end;
1247 VEC_safe_push (bfun_s, *gaps, end);
1249 pt_insn_get_offset (decoder, &offset);
1251 warning (_("Decode error (%d) at instruction %u (offset = 0x%" PRIx64
1252 ", pc = 0x%" PRIx64 "): %s."), errcode, end->insn_offset - 1,
1253 offset, insn.ip, pt_errstr (pt_errcode (errcode)));
1257 /* A callback function to allow the trace decoder to read the inferior's
1261 btrace_pt_readmem_callback (gdb_byte *buffer, size_t size,
1262 const struct pt_asid *asid, uint64_t pc,
1265 int result, errcode;
1267 result = (int) size;
1270 errcode = target_read_code ((CORE_ADDR) pc, buffer, size);
1272 result = -pte_nomap;
1274 CATCH (error, RETURN_MASK_ERROR)
1276 result = -pte_nomap;
1283 /* Translate the vendor from one enum to another. */
1285 static enum pt_cpu_vendor
1286 pt_translate_cpu_vendor (enum btrace_cpu_vendor vendor)
1298 /* Finalize the function branch trace after decode. */
1300 static void btrace_finalize_ftrace_pt (struct pt_insn_decoder *decoder,
1301 struct thread_info *tp, int level)
1303 pt_insn_free_decoder (decoder);
1305 /* LEVEL is the minimal function level of all btrace function segments.
1306 Define the global level offset to -LEVEL so all function levels are
1307 normalized to start at zero. */
1308 tp->btrace.level = -level;
1310 /* Add a single last instruction entry for the current PC.
1311 This allows us to compute the backtrace at the current PC using both
1312 standard unwind and btrace unwind.
1313 This extra entry is ignored by all record commands. */
1317 /* Compute the function branch trace from Intel Processor Trace
1321 btrace_compute_ftrace_pt (struct thread_info *tp,
1322 const struct btrace_data_pt *btrace,
1323 VEC (bfun_s) **gaps)
1325 struct btrace_thread_info *btinfo;
1326 struct pt_insn_decoder *decoder;
1327 struct pt_config config;
1330 if (btrace->size == 0)
1333 btinfo = &tp->btrace;
1334 level = btinfo->begin != NULL ? -btinfo->level : INT_MAX;
1336 pt_config_init(&config);
1337 config.begin = btrace->data;
1338 config.end = btrace->data + btrace->size;
1340 config.cpu.vendor = pt_translate_cpu_vendor (btrace->config.cpu.vendor);
1341 config.cpu.family = btrace->config.cpu.family;
1342 config.cpu.model = btrace->config.cpu.model;
1343 config.cpu.stepping = btrace->config.cpu.stepping;
1345 errcode = pt_cpu_errata (&config.errata, &config.cpu);
1347 error (_("Failed to configure the Intel Processor Trace decoder: %s."),
1348 pt_errstr (pt_errcode (errcode)));
1350 decoder = pt_insn_alloc_decoder (&config);
1351 if (decoder == NULL)
1352 error (_("Failed to allocate the Intel Processor Trace decoder."));
1356 struct pt_image *image;
1358 image = pt_insn_get_image(decoder);
1360 error (_("Failed to configure the Intel Processor Trace decoder."));
1362 errcode = pt_image_set_callback(image, btrace_pt_readmem_callback, NULL);
1364 error (_("Failed to configure the Intel Processor Trace decoder: "
1365 "%s."), pt_errstr (pt_errcode (errcode)));
1367 ftrace_add_pt (btinfo, decoder, &btinfo->begin, &btinfo->end, &level,
1370 CATCH (error, RETURN_MASK_ALL)
1372 /* Indicate a gap in the trace if we quit trace processing. */
1373 if (error.reason == RETURN_QUIT && btinfo->end != NULL)
1375 btinfo->end = ftrace_new_gap (btinfo, btinfo->end, BDE_PT_USER_QUIT);
1377 VEC_safe_push (bfun_s, *gaps, btinfo->end);
1380 btrace_finalize_ftrace_pt (decoder, tp, level);
1382 throw_exception (error);
1386 btrace_finalize_ftrace_pt (decoder, tp, level);
1389 #else /* defined (HAVE_LIBIPT) */
1392 btrace_compute_ftrace_pt (struct thread_info *tp,
1393 const struct btrace_data_pt *btrace,
1394 VEC (bfun_s) **gaps)
1396 internal_error (__FILE__, __LINE__, _("Unexpected branch trace format."));
1399 #endif /* defined (HAVE_LIBIPT) */
1401 /* Compute the function branch trace from a block branch trace BTRACE for
1402 a thread given by BTINFO. */
1405 btrace_compute_ftrace_1 (struct thread_info *tp, struct btrace_data *btrace,
1406 VEC (bfun_s) **gaps)
1408 DEBUG ("compute ftrace");
1410 switch (btrace->format)
1412 case BTRACE_FORMAT_NONE:
1415 case BTRACE_FORMAT_BTS:
1416 btrace_compute_ftrace_bts (tp, &btrace->variant.bts, gaps);
1419 case BTRACE_FORMAT_PT:
1420 btrace_compute_ftrace_pt (tp, &btrace->variant.pt, gaps);
1424 internal_error (__FILE__, __LINE__, _("Unkown branch trace format."));
1428 btrace_finalize_ftrace (struct thread_info *tp, VEC (bfun_s) **gaps)
1430 if (!VEC_empty (bfun_s, *gaps))
1432 tp->btrace.ngaps += VEC_length (bfun_s, *gaps);
1433 btrace_bridge_gaps (tp, gaps);
1438 btrace_compute_ftrace (struct thread_info *tp, struct btrace_data *btrace)
1441 struct cleanup *old_chain;
1444 old_chain = make_cleanup (VEC_cleanup (bfun_s), &gaps);
1448 btrace_compute_ftrace_1 (tp, btrace, &gaps);
1450 CATCH (error, RETURN_MASK_ALL)
1452 btrace_finalize_ftrace (tp, &gaps);
1454 throw_exception (error);
1458 btrace_finalize_ftrace (tp, &gaps);
1460 do_cleanups (old_chain);
1463 /* Add an entry for the current PC. */
1466 btrace_add_pc (struct thread_info *tp)
1468 struct btrace_data btrace;
1469 struct btrace_block *block;
1470 struct regcache *regcache;
1471 struct cleanup *cleanup;
1474 regcache = get_thread_regcache (tp->ptid);
1475 pc = regcache_read_pc (regcache);
1477 btrace_data_init (&btrace);
1478 btrace.format = BTRACE_FORMAT_BTS;
1479 btrace.variant.bts.blocks = NULL;
1481 cleanup = make_cleanup_btrace_data (&btrace);
1483 block = VEC_safe_push (btrace_block_s, btrace.variant.bts.blocks, NULL);
1487 btrace_compute_ftrace (tp, &btrace);
1489 do_cleanups (cleanup);
1495 btrace_enable (struct thread_info *tp, const struct btrace_config *conf)
1497 if (tp->btrace.target != NULL)
1500 #if !defined (HAVE_LIBIPT)
1501 if (conf->format == BTRACE_FORMAT_PT)
1502 error (_("GDB does not support Intel Processor Trace."));
1503 #endif /* !defined (HAVE_LIBIPT) */
1505 if (!target_supports_btrace (conf->format))
1506 error (_("Target does not support branch tracing."));
1508 DEBUG ("enable thread %s (%s)", print_thread_id (tp),
1509 target_pid_to_str (tp->ptid));
1511 tp->btrace.target = target_enable_btrace (tp->ptid, conf);
1513 /* We're done if we failed to enable tracing. */
1514 if (tp->btrace.target == NULL)
1517 /* We need to undo the enable in case of errors. */
1520 /* Add an entry for the current PC so we start tracing from where we
1523 If we can't access TP's registers, TP is most likely running. In this
1524 case, we can't really say where tracing was enabled so it should be
1525 safe to simply skip this step.
1527 This is not relevant for BTRACE_FORMAT_PT since the trace will already
1528 start at the PC at which tracing was enabled. */
1529 if (conf->format != BTRACE_FORMAT_PT
1530 && can_access_registers_ptid (tp->ptid))
1533 CATCH (exception, RETURN_MASK_ALL)
1535 btrace_disable (tp);
1537 throw_exception (exception);
1544 const struct btrace_config *
1545 btrace_conf (const struct btrace_thread_info *btinfo)
1547 if (btinfo->target == NULL)
1550 return target_btrace_conf (btinfo->target);
1556 btrace_disable (struct thread_info *tp)
1558 struct btrace_thread_info *btp = &tp->btrace;
1561 if (btp->target == NULL)
1564 DEBUG ("disable thread %s (%s)", print_thread_id (tp),
1565 target_pid_to_str (tp->ptid));
1567 target_disable_btrace (btp->target);
1576 btrace_teardown (struct thread_info *tp)
1578 struct btrace_thread_info *btp = &tp->btrace;
1581 if (btp->target == NULL)
1584 DEBUG ("teardown thread %s (%s)", print_thread_id (tp),
1585 target_pid_to_str (tp->ptid));
1587 target_teardown_btrace (btp->target);
1593 /* Stitch branch trace in BTS format. */
1596 btrace_stitch_bts (struct btrace_data_bts *btrace, struct thread_info *tp)
1598 struct btrace_thread_info *btinfo;
1599 struct btrace_function *last_bfun;
1600 struct btrace_insn *last_insn;
1601 btrace_block_s *first_new_block;
1603 btinfo = &tp->btrace;
1604 last_bfun = btinfo->end;
1605 gdb_assert (last_bfun != NULL);
1606 gdb_assert (!VEC_empty (btrace_block_s, btrace->blocks));
1608 /* If the existing trace ends with a gap, we just glue the traces
1609 together. We need to drop the last (i.e. chronologically first) block
1610 of the new trace, though, since we can't fill in the start address.*/
1611 if (VEC_empty (btrace_insn_s, last_bfun->insn))
1613 VEC_pop (btrace_block_s, btrace->blocks);
1617 /* Beware that block trace starts with the most recent block, so the
1618 chronologically first block in the new trace is the last block in
1619 the new trace's block vector. */
1620 first_new_block = VEC_last (btrace_block_s, btrace->blocks);
1621 last_insn = VEC_last (btrace_insn_s, last_bfun->insn);
1623 /* If the current PC at the end of the block is the same as in our current
1624 trace, there are two explanations:
1625 1. we executed the instruction and some branch brought us back.
1626 2. we have not made any progress.
1627 In the first case, the delta trace vector should contain at least two
1629 In the second case, the delta trace vector should contain exactly one
1630 entry for the partial block containing the current PC. Remove it. */
1631 if (first_new_block->end == last_insn->pc
1632 && VEC_length (btrace_block_s, btrace->blocks) == 1)
1634 VEC_pop (btrace_block_s, btrace->blocks);
1638 DEBUG ("stitching %s to %s", ftrace_print_insn_addr (last_insn),
1639 core_addr_to_string_nz (first_new_block->end));
1641 /* Do a simple sanity check to make sure we don't accidentally end up
1642 with a bad block. This should not occur in practice. */
1643 if (first_new_block->end < last_insn->pc)
1645 warning (_("Error while trying to read delta trace. Falling back to "
1650 /* We adjust the last block to start at the end of our current trace. */
1651 gdb_assert (first_new_block->begin == 0);
1652 first_new_block->begin = last_insn->pc;
1654 /* We simply pop the last insn so we can insert it again as part of
1655 the normal branch trace computation.
1656 Since instruction iterators are based on indices in the instructions
1657 vector, we don't leave any pointers dangling. */
1658 DEBUG ("pruning insn at %s for stitching",
1659 ftrace_print_insn_addr (last_insn));
1661 VEC_pop (btrace_insn_s, last_bfun->insn);
1663 /* The instructions vector may become empty temporarily if this has
1664 been the only instruction in this function segment.
1665 This violates the invariant but will be remedied shortly by
1666 btrace_compute_ftrace when we add the new trace. */
1668 /* The only case where this would hurt is if the entire trace consisted
1669 of just that one instruction. If we remove it, we might turn the now
1670 empty btrace function segment into a gap. But we don't want gaps at
1671 the beginning. To avoid this, we remove the entire old trace. */
1672 if (last_bfun == btinfo->begin && VEC_empty (btrace_insn_s, last_bfun->insn))
1678 /* Adjust the block trace in order to stitch old and new trace together.
1679 BTRACE is the new delta trace between the last and the current stop.
1680 TP is the traced thread.
1681 May modifx BTRACE as well as the existing trace in TP.
1682 Return 0 on success, -1 otherwise. */
1685 btrace_stitch_trace (struct btrace_data *btrace, struct thread_info *tp)
1687 /* If we don't have trace, there's nothing to do. */
1688 if (btrace_data_empty (btrace))
1691 switch (btrace->format)
1693 case BTRACE_FORMAT_NONE:
1696 case BTRACE_FORMAT_BTS:
1697 return btrace_stitch_bts (&btrace->variant.bts, tp);
1699 case BTRACE_FORMAT_PT:
1700 /* Delta reads are not supported. */
1704 internal_error (__FILE__, __LINE__, _("Unkown branch trace format."));
1707 /* Clear the branch trace histories in BTINFO. */
1710 btrace_clear_history (struct btrace_thread_info *btinfo)
1712 xfree (btinfo->insn_history);
1713 xfree (btinfo->call_history);
1714 xfree (btinfo->replay);
1716 btinfo->insn_history = NULL;
1717 btinfo->call_history = NULL;
1718 btinfo->replay = NULL;
1721 /* Clear the branch trace maintenance histories in BTINFO. */
1724 btrace_maint_clear (struct btrace_thread_info *btinfo)
1726 switch (btinfo->data.format)
1731 case BTRACE_FORMAT_BTS:
1732 btinfo->maint.variant.bts.packet_history.begin = 0;
1733 btinfo->maint.variant.bts.packet_history.end = 0;
1736 #if defined (HAVE_LIBIPT)
1737 case BTRACE_FORMAT_PT:
1738 xfree (btinfo->maint.variant.pt.packets);
1740 btinfo->maint.variant.pt.packets = NULL;
1741 btinfo->maint.variant.pt.packet_history.begin = 0;
1742 btinfo->maint.variant.pt.packet_history.end = 0;
1744 #endif /* defined (HAVE_LIBIPT) */
1751 btrace_decode_error (enum btrace_format format, int errcode)
1755 case BTRACE_FORMAT_BTS:
1758 case BDE_BTS_OVERFLOW:
1759 return _("instruction overflow");
1761 case BDE_BTS_INSN_SIZE:
1762 return _("unknown instruction");
1769 #if defined (HAVE_LIBIPT)
1770 case BTRACE_FORMAT_PT:
1773 case BDE_PT_USER_QUIT:
1774 return _("trace decode cancelled");
1776 case BDE_PT_DISABLED:
1777 return _("disabled");
1779 case BDE_PT_OVERFLOW:
1780 return _("overflow");
1784 return pt_errstr (pt_errcode (errcode));
1788 #endif /* defined (HAVE_LIBIPT) */
1794 return _("unknown");
1800 btrace_fetch (struct thread_info *tp)
1802 struct btrace_thread_info *btinfo;
1803 struct btrace_target_info *tinfo;
1804 struct btrace_data btrace;
1805 struct cleanup *cleanup;
1808 DEBUG ("fetch thread %s (%s)", print_thread_id (tp),
1809 target_pid_to_str (tp->ptid));
1811 btinfo = &tp->btrace;
1812 tinfo = btinfo->target;
1816 /* There's no way we could get new trace while replaying.
1817 On the other hand, delta trace would return a partial record with the
1818 current PC, which is the replay PC, not the last PC, as expected. */
1819 if (btinfo->replay != NULL)
1822 /* With CLI usage, TP->PTID always equals INFERIOR_PTID here. Now that we
1823 can store a gdb.Record object in Python referring to a different thread
1824 than the current one, temporarily set INFERIOR_PTID. */
1825 cleanup = save_inferior_ptid ();
1826 inferior_ptid = tp->ptid;
1828 /* We should not be called on running or exited threads. */
1829 gdb_assert (can_access_registers_ptid (tp->ptid));
1831 btrace_data_init (&btrace);
1832 make_cleanup_btrace_data (&btrace);
1834 /* Let's first try to extend the trace we already have. */
1835 if (btinfo->end != NULL)
1837 errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_DELTA);
1840 /* Success. Let's try to stitch the traces together. */
1841 errcode = btrace_stitch_trace (&btrace, tp);
1845 /* We failed to read delta trace. Let's try to read new trace. */
1846 errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_NEW);
1848 /* If we got any new trace, discard what we have. */
1849 if (errcode == 0 && !btrace_data_empty (&btrace))
1853 /* If we were not able to read the trace, we start over. */
1857 errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_ALL);
1861 errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_ALL);
1863 /* If we were not able to read the branch trace, signal an error. */
1865 error (_("Failed to read branch trace."));
1867 /* Compute the trace, provided we have any. */
1868 if (!btrace_data_empty (&btrace))
1870 /* Store the raw trace data. The stored data will be cleared in
1871 btrace_clear, so we always append the new trace. */
1872 btrace_data_append (&btinfo->data, &btrace);
1873 btrace_maint_clear (btinfo);
1875 btrace_clear_history (btinfo);
1876 btrace_compute_ftrace (tp, &btrace);
1879 do_cleanups (cleanup);
1885 btrace_clear (struct thread_info *tp)
1887 struct btrace_thread_info *btinfo;
1889 DEBUG ("clear thread %s (%s)", print_thread_id (tp),
1890 target_pid_to_str (tp->ptid));
1892 /* Make sure btrace frames that may hold a pointer into the branch
1893 trace data are destroyed. */
1894 reinit_frame_cache ();
1896 btinfo = &tp->btrace;
1897 for (auto &bfun : btinfo->functions)
1899 VEC_free (btrace_insn_s, bfun->insn);
1903 btinfo->functions.clear ();
1904 btinfo->begin = NULL;
1908 /* Must clear the maint data before - it depends on BTINFO->DATA. */
1909 btrace_maint_clear (btinfo);
1910 btrace_data_clear (&btinfo->data);
1911 btrace_clear_history (btinfo);
1917 btrace_free_objfile (struct objfile *objfile)
1919 struct thread_info *tp;
1921 DEBUG ("free objfile");
1923 ALL_NON_EXITED_THREADS (tp)
1927 #if defined (HAVE_LIBEXPAT)
1929 /* Check the btrace document version. */
1932 check_xml_btrace_version (struct gdb_xml_parser *parser,
1933 const struct gdb_xml_element *element,
1934 void *user_data, VEC (gdb_xml_value_s) *attributes)
1937 = (const char *) xml_find_attribute (attributes, "version")->value;
1939 if (strcmp (version, "1.0") != 0)
1940 gdb_xml_error (parser, _("Unsupported btrace version: \"%s\""), version);
1943 /* Parse a btrace "block" xml record. */
1946 parse_xml_btrace_block (struct gdb_xml_parser *parser,
1947 const struct gdb_xml_element *element,
1948 void *user_data, VEC (gdb_xml_value_s) *attributes)
1950 struct btrace_data *btrace;
1951 struct btrace_block *block;
1952 ULONGEST *begin, *end;
1954 btrace = (struct btrace_data *) user_data;
1956 switch (btrace->format)
1958 case BTRACE_FORMAT_BTS:
1961 case BTRACE_FORMAT_NONE:
1962 btrace->format = BTRACE_FORMAT_BTS;
1963 btrace->variant.bts.blocks = NULL;
1967 gdb_xml_error (parser, _("Btrace format error."));
1970 begin = (ULONGEST *) xml_find_attribute (attributes, "begin")->value;
1971 end = (ULONGEST *) xml_find_attribute (attributes, "end")->value;
1973 block = VEC_safe_push (btrace_block_s, btrace->variant.bts.blocks, NULL);
1974 block->begin = *begin;
1978 /* Parse a "raw" xml record. */
1981 parse_xml_raw (struct gdb_xml_parser *parser, const char *body_text,
1982 gdb_byte **pdata, size_t *psize)
1984 struct cleanup *cleanup;
1985 gdb_byte *data, *bin;
1988 len = strlen (body_text);
1990 gdb_xml_error (parser, _("Bad raw data size."));
1994 bin = data = (gdb_byte *) xmalloc (size);
1995 cleanup = make_cleanup (xfree, data);
1997 /* We use hex encoding - see common/rsp-low.h. */
2005 if (hi == 0 || lo == 0)
2006 gdb_xml_error (parser, _("Bad hex encoding."));
2008 *bin++ = fromhex (hi) * 16 + fromhex (lo);
2012 discard_cleanups (cleanup);
2018 /* Parse a btrace pt-config "cpu" xml record. */
2021 parse_xml_btrace_pt_config_cpu (struct gdb_xml_parser *parser,
2022 const struct gdb_xml_element *element,
2024 VEC (gdb_xml_value_s) *attributes)
2026 struct btrace_data *btrace;
2028 ULONGEST *family, *model, *stepping;
2030 vendor = (const char *) xml_find_attribute (attributes, "vendor")->value;
2031 family = (ULONGEST *) xml_find_attribute (attributes, "family")->value;
2032 model = (ULONGEST *) xml_find_attribute (attributes, "model")->value;
2033 stepping = (ULONGEST *) xml_find_attribute (attributes, "stepping")->value;
2035 btrace = (struct btrace_data *) user_data;
2037 if (strcmp (vendor, "GenuineIntel") == 0)
2038 btrace->variant.pt.config.cpu.vendor = CV_INTEL;
2040 btrace->variant.pt.config.cpu.family = *family;
2041 btrace->variant.pt.config.cpu.model = *model;
2042 btrace->variant.pt.config.cpu.stepping = *stepping;
2045 /* Parse a btrace pt "raw" xml record. */
2048 parse_xml_btrace_pt_raw (struct gdb_xml_parser *parser,
2049 const struct gdb_xml_element *element,
2050 void *user_data, const char *body_text)
2052 struct btrace_data *btrace;
2054 btrace = (struct btrace_data *) user_data;
2055 parse_xml_raw (parser, body_text, &btrace->variant.pt.data,
2056 &btrace->variant.pt.size);
2059 /* Parse a btrace "pt" xml record. */
2062 parse_xml_btrace_pt (struct gdb_xml_parser *parser,
2063 const struct gdb_xml_element *element,
2064 void *user_data, VEC (gdb_xml_value_s) *attributes)
2066 struct btrace_data *btrace;
2068 btrace = (struct btrace_data *) user_data;
2069 btrace->format = BTRACE_FORMAT_PT;
2070 btrace->variant.pt.config.cpu.vendor = CV_UNKNOWN;
2071 btrace->variant.pt.data = NULL;
2072 btrace->variant.pt.size = 0;
2075 static const struct gdb_xml_attribute block_attributes[] = {
2076 { "begin", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
2077 { "end", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
2078 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2081 static const struct gdb_xml_attribute btrace_pt_config_cpu_attributes[] = {
2082 { "vendor", GDB_XML_AF_NONE, NULL, NULL },
2083 { "family", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
2084 { "model", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
2085 { "stepping", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
2086 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2089 static const struct gdb_xml_element btrace_pt_config_children[] = {
2090 { "cpu", btrace_pt_config_cpu_attributes, NULL, GDB_XML_EF_OPTIONAL,
2091 parse_xml_btrace_pt_config_cpu, NULL },
2092 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2095 static const struct gdb_xml_element btrace_pt_children[] = {
2096 { "pt-config", NULL, btrace_pt_config_children, GDB_XML_EF_OPTIONAL, NULL,
2098 { "raw", NULL, NULL, GDB_XML_EF_OPTIONAL, NULL, parse_xml_btrace_pt_raw },
2099 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2102 static const struct gdb_xml_attribute btrace_attributes[] = {
2103 { "version", GDB_XML_AF_NONE, NULL, NULL },
2104 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2107 static const struct gdb_xml_element btrace_children[] = {
2108 { "block", block_attributes, NULL,
2109 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL, parse_xml_btrace_block, NULL },
2110 { "pt", NULL, btrace_pt_children, GDB_XML_EF_OPTIONAL, parse_xml_btrace_pt,
2112 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2115 static const struct gdb_xml_element btrace_elements[] = {
2116 { "btrace", btrace_attributes, btrace_children, GDB_XML_EF_NONE,
2117 check_xml_btrace_version, NULL },
2118 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2121 #endif /* defined (HAVE_LIBEXPAT) */
2126 parse_xml_btrace (struct btrace_data *btrace, const char *buffer)
2128 struct cleanup *cleanup;
2131 #if defined (HAVE_LIBEXPAT)
2133 btrace->format = BTRACE_FORMAT_NONE;
2135 cleanup = make_cleanup_btrace_data (btrace);
2136 errcode = gdb_xml_parse_quick (_("btrace"), "btrace.dtd", btrace_elements,
2139 error (_("Error parsing branch trace."));
2141 /* Keep parse results. */
2142 discard_cleanups (cleanup);
2144 #else /* !defined (HAVE_LIBEXPAT) */
2146 error (_("Cannot process branch trace. XML parsing is not supported."));
2148 #endif /* !defined (HAVE_LIBEXPAT) */
2151 #if defined (HAVE_LIBEXPAT)
2153 /* Parse a btrace-conf "bts" xml record. */
2156 parse_xml_btrace_conf_bts (struct gdb_xml_parser *parser,
2157 const struct gdb_xml_element *element,
2158 void *user_data, VEC (gdb_xml_value_s) *attributes)
2160 struct btrace_config *conf;
2161 struct gdb_xml_value *size;
2163 conf = (struct btrace_config *) user_data;
2164 conf->format = BTRACE_FORMAT_BTS;
2167 size = xml_find_attribute (attributes, "size");
2169 conf->bts.size = (unsigned int) *(ULONGEST *) size->value;
2172 /* Parse a btrace-conf "pt" xml record. */
2175 parse_xml_btrace_conf_pt (struct gdb_xml_parser *parser,
2176 const struct gdb_xml_element *element,
2177 void *user_data, VEC (gdb_xml_value_s) *attributes)
2179 struct btrace_config *conf;
2180 struct gdb_xml_value *size;
2182 conf = (struct btrace_config *) user_data;
2183 conf->format = BTRACE_FORMAT_PT;
2186 size = xml_find_attribute (attributes, "size");
2188 conf->pt.size = (unsigned int) *(ULONGEST *) size->value;
2191 static const struct gdb_xml_attribute btrace_conf_pt_attributes[] = {
2192 { "size", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
2193 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2196 static const struct gdb_xml_attribute btrace_conf_bts_attributes[] = {
2197 { "size", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
2198 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2201 static const struct gdb_xml_element btrace_conf_children[] = {
2202 { "bts", btrace_conf_bts_attributes, NULL, GDB_XML_EF_OPTIONAL,
2203 parse_xml_btrace_conf_bts, NULL },
2204 { "pt", btrace_conf_pt_attributes, NULL, GDB_XML_EF_OPTIONAL,
2205 parse_xml_btrace_conf_pt, NULL },
2206 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2209 static const struct gdb_xml_attribute btrace_conf_attributes[] = {
2210 { "version", GDB_XML_AF_NONE, NULL, NULL },
2211 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2214 static const struct gdb_xml_element btrace_conf_elements[] = {
2215 { "btrace-conf", btrace_conf_attributes, btrace_conf_children,
2216 GDB_XML_EF_NONE, NULL, NULL },
2217 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2220 #endif /* defined (HAVE_LIBEXPAT) */
2225 parse_xml_btrace_conf (struct btrace_config *conf, const char *xml)
2229 #if defined (HAVE_LIBEXPAT)
2231 errcode = gdb_xml_parse_quick (_("btrace-conf"), "btrace-conf.dtd",
2232 btrace_conf_elements, xml, conf);
2234 error (_("Error parsing branch trace configuration."));
2236 #else /* !defined (HAVE_LIBEXPAT) */
2238 error (_("XML parsing is not supported."));
2240 #endif /* !defined (HAVE_LIBEXPAT) */
2245 const struct btrace_insn *
2246 btrace_insn_get (const struct btrace_insn_iterator *it)
2248 const struct btrace_function *bfun;
2249 unsigned int index, end;
2251 index = it->insn_index;
2252 bfun = it->btinfo->functions[it->call_index];
2254 /* Check if the iterator points to a gap in the trace. */
2255 if (bfun->errcode != 0)
2258 /* The index is within the bounds of this function's instruction vector. */
2259 end = VEC_length (btrace_insn_s, bfun->insn);
2260 gdb_assert (0 < end);
2261 gdb_assert (index < end);
2263 return VEC_index (btrace_insn_s, bfun->insn, index);
2269 btrace_insn_get_error (const struct btrace_insn_iterator *it)
2271 const struct btrace_function *bfun;
2273 bfun = it->btinfo->functions[it->call_index];
2274 return bfun->errcode;
2280 btrace_insn_number (const struct btrace_insn_iterator *it)
2282 const struct btrace_function *bfun;
2284 bfun = it->btinfo->functions[it->call_index];
2285 return bfun->insn_offset + it->insn_index;
2291 btrace_insn_begin (struct btrace_insn_iterator *it,
2292 const struct btrace_thread_info *btinfo)
2294 const struct btrace_function *bfun;
2296 bfun = btinfo->begin;
2298 error (_("No trace."));
2300 it->btinfo = btinfo;
2308 btrace_insn_end (struct btrace_insn_iterator *it,
2309 const struct btrace_thread_info *btinfo)
2311 const struct btrace_function *bfun;
2312 unsigned int length;
2316 error (_("No trace."));
2318 length = VEC_length (btrace_insn_s, bfun->insn);
2320 /* The last function may either be a gap or it contains the current
2321 instruction, which is one past the end of the execution trace; ignore
2326 it->btinfo = btinfo;
2327 it->call_index = bfun->number - 1;
2328 it->insn_index = length;
2334 btrace_insn_next (struct btrace_insn_iterator *it, unsigned int stride)
2336 const struct btrace_function *bfun;
2337 unsigned int index, steps;
2339 bfun = it->btinfo->functions[it->call_index];
2341 index = it->insn_index;
2345 unsigned int end, space, adv;
2347 end = VEC_length (btrace_insn_s, bfun->insn);
2349 /* An empty function segment represents a gap in the trace. We count
2350 it as one instruction. */
2353 const struct btrace_function *next;
2355 next = bfun->flow.next;
2368 gdb_assert (0 < end);
2369 gdb_assert (index < end);
2371 /* Compute the number of instructions remaining in this segment. */
2372 space = end - index;
2374 /* Advance the iterator as far as possible within this segment. */
2375 adv = std::min (space, stride);
2380 /* Move to the next function if we're at the end of this one. */
2383 const struct btrace_function *next;
2385 next = bfun->flow.next;
2388 /* We stepped past the last function.
2390 Let's adjust the index to point to the last instruction in
2391 the previous function. */
2397 /* We now point to the first instruction in the new function. */
2402 /* We did make progress. */
2403 gdb_assert (adv > 0);
2406 /* Update the iterator. */
2407 it->call_index = bfun->number - 1;
2408 it->insn_index = index;
2416 btrace_insn_prev (struct btrace_insn_iterator *it, unsigned int stride)
2418 const struct btrace_function *bfun;
2419 unsigned int index, steps;
2421 bfun = it->btinfo->functions[it->call_index];
2423 index = it->insn_index;
2429 /* Move to the previous function if we're at the start of this one. */
2432 const struct btrace_function *prev;
2434 prev = bfun->flow.prev;
2438 /* We point to one after the last instruction in the new function. */
2440 index = VEC_length (btrace_insn_s, bfun->insn);
2442 /* An empty function segment represents a gap in the trace. We count
2443 it as one instruction. */
2453 /* Advance the iterator as far as possible within this segment. */
2454 adv = std::min (index, stride);
2460 /* We did make progress. */
2461 gdb_assert (adv > 0);
2464 /* Update the iterator. */
2465 it->call_index = bfun->number - 1;
2466 it->insn_index = index;
2474 btrace_insn_cmp (const struct btrace_insn_iterator *lhs,
2475 const struct btrace_insn_iterator *rhs)
2477 gdb_assert (lhs->btinfo == rhs->btinfo);
2479 if (lhs->call_index != rhs->call_index)
2480 return lhs->call_index - rhs->call_index;
2482 return lhs->insn_index - rhs->insn_index;
2488 btrace_find_insn_by_number (struct btrace_insn_iterator *it,
2489 const struct btrace_thread_info *btinfo,
2490 unsigned int number)
2492 const struct btrace_function *bfun;
2493 unsigned int upper, lower;
2495 if (btinfo->functions.empty ())
2499 bfun = btinfo->functions[lower];
2500 if (number < bfun->insn_offset)
2503 upper = btinfo->functions.size () - 1;
2504 bfun = btinfo->functions[upper];
2505 if (number >= bfun->insn_offset + ftrace_call_num_insn (bfun))
2508 /* We assume that there are no holes in the numbering. */
2511 const unsigned int average = lower + (upper - lower) / 2;
2513 bfun = btinfo->functions[average];
2515 if (number < bfun->insn_offset)
2517 upper = average - 1;
2521 if (number >= bfun->insn_offset + ftrace_call_num_insn (bfun))
2523 lower = average + 1;
2530 it->btinfo = btinfo;
2531 it->call_index = bfun->number - 1;
2532 it->insn_index = number - bfun->insn_offset;
2536 /* Returns true if the recording ends with a function segment that
2537 contains only a single (i.e. the current) instruction. */
2540 btrace_ends_with_single_insn (const struct btrace_thread_info *btinfo)
2542 const btrace_function *bfun;
2544 if (btinfo->functions.empty ())
2547 bfun = btinfo->functions.back ();
2548 if (bfun->errcode != 0)
2551 return ftrace_call_num_insn (bfun) == 1;
2556 const struct btrace_function *
2557 btrace_call_get (const struct btrace_call_iterator *it)
2559 if (it->index >= it->btinfo->functions.size ())
2562 return it->btinfo->functions[it->index];
2568 btrace_call_number (const struct btrace_call_iterator *it)
2570 const unsigned int length = it->btinfo->functions.size ();
2572 /* If the last function segment contains only a single instruction (i.e. the
2573 current instruction), skip it. */
2574 if ((it->index == length) && btrace_ends_with_single_insn (it->btinfo))
2577 return it->index + 1;
2583 btrace_call_begin (struct btrace_call_iterator *it,
2584 const struct btrace_thread_info *btinfo)
2586 if (btinfo->functions.empty ())
2587 error (_("No trace."));
2589 it->btinfo = btinfo;
2596 btrace_call_end (struct btrace_call_iterator *it,
2597 const struct btrace_thread_info *btinfo)
2599 if (btinfo->functions.empty ())
2600 error (_("No trace."));
2602 it->btinfo = btinfo;
2603 it->index = btinfo->functions.size ();
2609 btrace_call_next (struct btrace_call_iterator *it, unsigned int stride)
2611 const unsigned int length = it->btinfo->functions.size ();
2613 if (it->index + stride < length - 1)
2614 /* Default case: Simply advance the iterator. */
2615 it->index += stride;
2616 else if (it->index + stride == length - 1)
2618 /* We land exactly at the last function segment. If it contains only one
2619 instruction (i.e. the current instruction) it is not actually part of
2621 if (btrace_ends_with_single_insn (it->btinfo))
2624 it->index = length - 1;
2628 /* We land past the last function segment and have to adjust the stride.
2629 If the last function segment contains only one instruction (i.e. the
2630 current instruction) it is not actually part of the trace. */
2631 if (btrace_ends_with_single_insn (it->btinfo))
2632 stride = length - it->index - 1;
2634 stride = length - it->index;
2645 btrace_call_prev (struct btrace_call_iterator *it, unsigned int stride)
2647 const unsigned int length = it->btinfo->functions.size ();
2650 gdb_assert (it->index <= length);
2652 if (stride == 0 || it->index == 0)
2655 /* If we are at the end, the first step is a special case. If the last
2656 function segment contains only one instruction (i.e. the current
2657 instruction) it is not actually part of the trace. To be able to step
2658 over this instruction, we need at least one more function segment. */
2659 if ((it->index == length) && (length > 1))
2661 if (btrace_ends_with_single_insn (it->btinfo))
2662 it->index = length - 2;
2664 it->index = length - 1;
2670 stride = std::min (stride, it->index);
2672 it->index -= stride;
2673 return steps + stride;
2679 btrace_call_cmp (const struct btrace_call_iterator *lhs,
2680 const struct btrace_call_iterator *rhs)
2682 gdb_assert (lhs->btinfo == rhs->btinfo);
2683 return (int) (lhs->index - rhs->index);
2689 btrace_find_call_by_number (struct btrace_call_iterator *it,
2690 const struct btrace_thread_info *btinfo,
2691 unsigned int number)
2693 const unsigned int length = btinfo->functions.size ();
2695 if ((number == 0) || (number > length))
2698 it->btinfo = btinfo;
2699 it->index = number - 1;
2706 btrace_set_insn_history (struct btrace_thread_info *btinfo,
2707 const struct btrace_insn_iterator *begin,
2708 const struct btrace_insn_iterator *end)
2710 if (btinfo->insn_history == NULL)
2711 btinfo->insn_history = XCNEW (struct btrace_insn_history);
2713 btinfo->insn_history->begin = *begin;
2714 btinfo->insn_history->end = *end;
2720 btrace_set_call_history (struct btrace_thread_info *btinfo,
2721 const struct btrace_call_iterator *begin,
2722 const struct btrace_call_iterator *end)
2724 gdb_assert (begin->btinfo == end->btinfo);
2726 if (btinfo->call_history == NULL)
2727 btinfo->call_history = XCNEW (struct btrace_call_history);
2729 btinfo->call_history->begin = *begin;
2730 btinfo->call_history->end = *end;
2736 btrace_is_replaying (struct thread_info *tp)
2738 return tp->btrace.replay != NULL;
2744 btrace_is_empty (struct thread_info *tp)
2746 struct btrace_insn_iterator begin, end;
2747 struct btrace_thread_info *btinfo;
2749 btinfo = &tp->btrace;
2751 if (btinfo->begin == NULL)
2754 btrace_insn_begin (&begin, btinfo);
2755 btrace_insn_end (&end, btinfo);
2757 return btrace_insn_cmp (&begin, &end) == 0;
2760 /* Forward the cleanup request. */
2763 do_btrace_data_cleanup (void *arg)
2765 btrace_data_fini ((struct btrace_data *) arg);
2771 make_cleanup_btrace_data (struct btrace_data *data)
2773 return make_cleanup (do_btrace_data_cleanup, data);
2776 #if defined (HAVE_LIBIPT)
2778 /* Print a single packet. */
2781 pt_print_packet (const struct pt_packet *packet)
2783 switch (packet->type)
2786 printf_unfiltered (("[??: %x]"), packet->type);
2790 printf_unfiltered (("psb"));
2794 printf_unfiltered (("psbend"));
2798 printf_unfiltered (("pad"));
2802 printf_unfiltered (("tip %u: 0x%" PRIx64 ""),
2803 packet->payload.ip.ipc,
2804 packet->payload.ip.ip);
2808 printf_unfiltered (("tip.pge %u: 0x%" PRIx64 ""),
2809 packet->payload.ip.ipc,
2810 packet->payload.ip.ip);
2814 printf_unfiltered (("tip.pgd %u: 0x%" PRIx64 ""),
2815 packet->payload.ip.ipc,
2816 packet->payload.ip.ip);
2820 printf_unfiltered (("fup %u: 0x%" PRIx64 ""),
2821 packet->payload.ip.ipc,
2822 packet->payload.ip.ip);
2826 printf_unfiltered (("tnt-8 %u: 0x%" PRIx64 ""),
2827 packet->payload.tnt.bit_size,
2828 packet->payload.tnt.payload);
2832 printf_unfiltered (("tnt-64 %u: 0x%" PRIx64 ""),
2833 packet->payload.tnt.bit_size,
2834 packet->payload.tnt.payload);
2838 printf_unfiltered (("pip %" PRIx64 "%s"), packet->payload.pip.cr3,
2839 packet->payload.pip.nr ? (" nr") : (""));
2843 printf_unfiltered (("tsc %" PRIx64 ""), packet->payload.tsc.tsc);
2847 printf_unfiltered (("cbr %u"), packet->payload.cbr.ratio);
2851 switch (packet->payload.mode.leaf)
2854 printf_unfiltered (("mode %u"), packet->payload.mode.leaf);
2858 printf_unfiltered (("mode.exec%s%s"),
2859 packet->payload.mode.bits.exec.csl
2861 packet->payload.mode.bits.exec.csd
2862 ? (" cs.d") : (""));
2866 printf_unfiltered (("mode.tsx%s%s"),
2867 packet->payload.mode.bits.tsx.intx
2869 packet->payload.mode.bits.tsx.abrt
2870 ? (" abrt") : (""));
2876 printf_unfiltered (("ovf"));
2880 printf_unfiltered (("stop"));
2884 printf_unfiltered (("vmcs %" PRIx64 ""), packet->payload.vmcs.base);
2888 printf_unfiltered (("tma %x %x"), packet->payload.tma.ctc,
2889 packet->payload.tma.fc);
2893 printf_unfiltered (("mtc %x"), packet->payload.mtc.ctc);
2897 printf_unfiltered (("cyc %" PRIx64 ""), packet->payload.cyc.value);
2901 printf_unfiltered (("mnt %" PRIx64 ""), packet->payload.mnt.payload);
2906 /* Decode packets into MAINT using DECODER. */
2909 btrace_maint_decode_pt (struct btrace_maint_info *maint,
2910 struct pt_packet_decoder *decoder)
2916 struct btrace_pt_packet packet;
2918 errcode = pt_pkt_sync_forward (decoder);
2924 pt_pkt_get_offset (decoder, &packet.offset);
2926 errcode = pt_pkt_next (decoder, &packet.packet,
2927 sizeof(packet.packet));
2931 if (maint_btrace_pt_skip_pad == 0 || packet.packet.type != ppt_pad)
2933 packet.errcode = pt_errcode (errcode);
2934 VEC_safe_push (btrace_pt_packet_s, maint->variant.pt.packets,
2939 if (errcode == -pte_eos)
2942 packet.errcode = pt_errcode (errcode);
2943 VEC_safe_push (btrace_pt_packet_s, maint->variant.pt.packets,
2946 warning (_("Error at trace offset 0x%" PRIx64 ": %s."),
2947 packet.offset, pt_errstr (packet.errcode));
2950 if (errcode != -pte_eos)
2951 warning (_("Failed to synchronize onto the Intel Processor Trace "
2952 "stream: %s."), pt_errstr (pt_errcode (errcode)));
2955 /* Update the packet history in BTINFO. */
2958 btrace_maint_update_pt_packets (struct btrace_thread_info *btinfo)
2960 volatile struct gdb_exception except;
2961 struct pt_packet_decoder *decoder;
2962 struct btrace_data_pt *pt;
2963 struct pt_config config;
2966 pt = &btinfo->data.variant.pt;
2968 /* Nothing to do if there is no trace. */
2972 memset (&config, 0, sizeof(config));
2974 config.size = sizeof (config);
2975 config.begin = pt->data;
2976 config.end = pt->data + pt->size;
2978 config.cpu.vendor = pt_translate_cpu_vendor (pt->config.cpu.vendor);
2979 config.cpu.family = pt->config.cpu.family;
2980 config.cpu.model = pt->config.cpu.model;
2981 config.cpu.stepping = pt->config.cpu.stepping;
2983 errcode = pt_cpu_errata (&config.errata, &config.cpu);
2985 error (_("Failed to configure the Intel Processor Trace decoder: %s."),
2986 pt_errstr (pt_errcode (errcode)));
2988 decoder = pt_pkt_alloc_decoder (&config);
2989 if (decoder == NULL)
2990 error (_("Failed to allocate the Intel Processor Trace decoder."));
2994 btrace_maint_decode_pt (&btinfo->maint, decoder);
2996 CATCH (except, RETURN_MASK_ALL)
2998 pt_pkt_free_decoder (decoder);
3000 if (except.reason < 0)
3001 throw_exception (except);
3005 pt_pkt_free_decoder (decoder);
3008 #endif /* !defined (HAVE_LIBIPT) */
3010 /* Update the packet maintenance information for BTINFO and store the
3011 low and high bounds into BEGIN and END, respectively.
3012 Store the current iterator state into FROM and TO. */
3015 btrace_maint_update_packets (struct btrace_thread_info *btinfo,
3016 unsigned int *begin, unsigned int *end,
3017 unsigned int *from, unsigned int *to)
3019 switch (btinfo->data.format)
3028 case BTRACE_FORMAT_BTS:
3029 /* Nothing to do - we operate directly on BTINFO->DATA. */
3031 *end = VEC_length (btrace_block_s, btinfo->data.variant.bts.blocks);
3032 *from = btinfo->maint.variant.bts.packet_history.begin;
3033 *to = btinfo->maint.variant.bts.packet_history.end;
3036 #if defined (HAVE_LIBIPT)
3037 case BTRACE_FORMAT_PT:
3038 if (VEC_empty (btrace_pt_packet_s, btinfo->maint.variant.pt.packets))
3039 btrace_maint_update_pt_packets (btinfo);
3042 *end = VEC_length (btrace_pt_packet_s, btinfo->maint.variant.pt.packets);
3043 *from = btinfo->maint.variant.pt.packet_history.begin;
3044 *to = btinfo->maint.variant.pt.packet_history.end;
3046 #endif /* defined (HAVE_LIBIPT) */
3050 /* Print packets in BTINFO from BEGIN (inclusive) until END (exclusive) and
3051 update the current iterator position. */
3054 btrace_maint_print_packets (struct btrace_thread_info *btinfo,
3055 unsigned int begin, unsigned int end)
3057 switch (btinfo->data.format)
3062 case BTRACE_FORMAT_BTS:
3064 VEC (btrace_block_s) *blocks;
3067 blocks = btinfo->data.variant.bts.blocks;
3068 for (blk = begin; blk < end; ++blk)
3070 const btrace_block_s *block;
3072 block = VEC_index (btrace_block_s, blocks, blk);
3074 printf_unfiltered ("%u\tbegin: %s, end: %s\n", blk,
3075 core_addr_to_string_nz (block->begin),
3076 core_addr_to_string_nz (block->end));
3079 btinfo->maint.variant.bts.packet_history.begin = begin;
3080 btinfo->maint.variant.bts.packet_history.end = end;
3084 #if defined (HAVE_LIBIPT)
3085 case BTRACE_FORMAT_PT:
3087 VEC (btrace_pt_packet_s) *packets;
3090 packets = btinfo->maint.variant.pt.packets;
3091 for (pkt = begin; pkt < end; ++pkt)
3093 const struct btrace_pt_packet *packet;
3095 packet = VEC_index (btrace_pt_packet_s, packets, pkt);
3097 printf_unfiltered ("%u\t", pkt);
3098 printf_unfiltered ("0x%" PRIx64 "\t", packet->offset);
3100 if (packet->errcode == pte_ok)
3101 pt_print_packet (&packet->packet);
3103 printf_unfiltered ("[error: %s]", pt_errstr (packet->errcode));
3105 printf_unfiltered ("\n");
3108 btinfo->maint.variant.pt.packet_history.begin = begin;
3109 btinfo->maint.variant.pt.packet_history.end = end;
3112 #endif /* defined (HAVE_LIBIPT) */
3116 /* Read a number from an argument string. */
3119 get_uint (char **arg)
3121 char *begin, *end, *pos;
3122 unsigned long number;
3125 pos = skip_spaces (begin);
3127 if (!isdigit (*pos))
3128 error (_("Expected positive number, got: %s."), pos);
3130 number = strtoul (pos, &end, 10);
3131 if (number > UINT_MAX)
3132 error (_("Number too big."));
3134 *arg += (end - begin);
3136 return (unsigned int) number;
3139 /* Read a context size from an argument string. */
3142 get_context_size (char **arg)
3147 pos = skip_spaces (*arg);
3149 if (!isdigit (*pos))
3150 error (_("Expected positive number, got: %s."), pos);
3152 return strtol (pos, arg, 10);
3155 /* Complain about junk at the end of an argument string. */
3158 no_chunk (char *arg)
3161 error (_("Junk after argument: %s."), arg);
3164 /* The "maintenance btrace packet-history" command. */
3167 maint_btrace_packet_history_cmd (char *arg, int from_tty)
3169 struct btrace_thread_info *btinfo;
3170 struct thread_info *tp;
3171 unsigned int size, begin, end, from, to;
3173 tp = find_thread_ptid (inferior_ptid);
3175 error (_("No thread."));
3178 btinfo = &tp->btrace;
3180 btrace_maint_update_packets (btinfo, &begin, &end, &from, &to);
3183 printf_unfiltered (_("No trace.\n"));
3187 if (arg == NULL || *arg == 0 || strcmp (arg, "+") == 0)
3191 if (end - from < size)
3195 else if (strcmp (arg, "-") == 0)
3199 if (to - begin < size)
3205 from = get_uint (&arg);
3207 error (_("'%u' is out of range."), from);
3209 arg = skip_spaces (arg);
3212 arg = skip_spaces (++arg);
3217 size = get_context_size (&arg);
3221 if (end - from < size)
3225 else if (*arg == '-')
3228 size = get_context_size (&arg);
3232 /* Include the packet given as first argument. */
3236 if (to - begin < size)
3242 to = get_uint (&arg);
3244 /* Include the packet at the second argument and silently
3245 truncate the range. */
3258 if (end - from < size)
3266 btrace_maint_print_packets (btinfo, from, to);
3269 /* The "maintenance btrace clear-packet-history" command. */
3272 maint_btrace_clear_packet_history_cmd (char *args, int from_tty)
3274 struct btrace_thread_info *btinfo;
3275 struct thread_info *tp;
3277 if (args != NULL && *args != 0)
3278 error (_("Invalid argument."));
3280 tp = find_thread_ptid (inferior_ptid);
3282 error (_("No thread."));
3284 btinfo = &tp->btrace;
3286 /* Must clear the maint data before - it depends on BTINFO->DATA. */
3287 btrace_maint_clear (btinfo);
3288 btrace_data_clear (&btinfo->data);
3291 /* The "maintenance btrace clear" command. */
3294 maint_btrace_clear_cmd (char *args, int from_tty)
3296 struct btrace_thread_info *btinfo;
3297 struct thread_info *tp;
3299 if (args != NULL && *args != 0)
3300 error (_("Invalid argument."));
3302 tp = find_thread_ptid (inferior_ptid);
3304 error (_("No thread."));
3309 /* The "maintenance btrace" command. */
3312 maint_btrace_cmd (char *args, int from_tty)
3314 help_list (maint_btrace_cmdlist, "maintenance btrace ", all_commands,
3318 /* The "maintenance set btrace" command. */
3321 maint_btrace_set_cmd (char *args, int from_tty)
3323 help_list (maint_btrace_set_cmdlist, "maintenance set btrace ", all_commands,
3327 /* The "maintenance show btrace" command. */
3330 maint_btrace_show_cmd (char *args, int from_tty)
3332 help_list (maint_btrace_show_cmdlist, "maintenance show btrace ",
3333 all_commands, gdb_stdout);
3336 /* The "maintenance set btrace pt" command. */
3339 maint_btrace_pt_set_cmd (char *args, int from_tty)
3341 help_list (maint_btrace_pt_set_cmdlist, "maintenance set btrace pt ",
3342 all_commands, gdb_stdout);
3345 /* The "maintenance show btrace pt" command. */
3348 maint_btrace_pt_show_cmd (char *args, int from_tty)
3350 help_list (maint_btrace_pt_show_cmdlist, "maintenance show btrace pt ",
3351 all_commands, gdb_stdout);
3354 /* The "maintenance info btrace" command. */
3357 maint_info_btrace_cmd (char *args, int from_tty)
3359 struct btrace_thread_info *btinfo;
3360 struct thread_info *tp;
3361 const struct btrace_config *conf;
3363 if (args != NULL && *args != 0)
3364 error (_("Invalid argument."));
3366 tp = find_thread_ptid (inferior_ptid);
3368 error (_("No thread."));
3370 btinfo = &tp->btrace;
3372 conf = btrace_conf (btinfo);
3374 error (_("No btrace configuration."));
3376 printf_unfiltered (_("Format: %s.\n"),
3377 btrace_format_string (conf->format));
3379 switch (conf->format)
3384 case BTRACE_FORMAT_BTS:
3385 printf_unfiltered (_("Number of packets: %u.\n"),
3386 VEC_length (btrace_block_s,
3387 btinfo->data.variant.bts.blocks));
3390 #if defined (HAVE_LIBIPT)
3391 case BTRACE_FORMAT_PT:
3393 struct pt_version version;
3395 version = pt_library_version ();
3396 printf_unfiltered (_("Version: %u.%u.%u%s.\n"), version.major,
3397 version.minor, version.build,
3398 version.ext != NULL ? version.ext : "");
3400 btrace_maint_update_pt_packets (btinfo);
3401 printf_unfiltered (_("Number of packets: %u.\n"),
3402 VEC_length (btrace_pt_packet_s,
3403 btinfo->maint.variant.pt.packets));
3406 #endif /* defined (HAVE_LIBIPT) */
3410 /* The "maint show btrace pt skip-pad" show value function. */
3413 show_maint_btrace_pt_skip_pad (struct ui_file *file, int from_tty,
3414 struct cmd_list_element *c,
3417 fprintf_filtered (file, _("Skip PAD packets is %s.\n"), value);
3421 /* Initialize btrace maintenance commands. */
3423 void _initialize_btrace (void);
3425 _initialize_btrace (void)
3427 add_cmd ("btrace", class_maintenance, maint_info_btrace_cmd,
3428 _("Info about branch tracing data."), &maintenanceinfolist);
3430 add_prefix_cmd ("btrace", class_maintenance, maint_btrace_cmd,
3431 _("Branch tracing maintenance commands."),
3432 &maint_btrace_cmdlist, "maintenance btrace ",
3433 0, &maintenancelist);
3435 add_prefix_cmd ("btrace", class_maintenance, maint_btrace_set_cmd, _("\
3436 Set branch tracing specific variables."),
3437 &maint_btrace_set_cmdlist, "maintenance set btrace ",
3438 0, &maintenance_set_cmdlist);
3440 add_prefix_cmd ("pt", class_maintenance, maint_btrace_pt_set_cmd, _("\
3441 Set Intel Processor Trace specific variables."),
3442 &maint_btrace_pt_set_cmdlist, "maintenance set btrace pt ",
3443 0, &maint_btrace_set_cmdlist);
3445 add_prefix_cmd ("btrace", class_maintenance, maint_btrace_show_cmd, _("\
3446 Show branch tracing specific variables."),
3447 &maint_btrace_show_cmdlist, "maintenance show btrace ",
3448 0, &maintenance_show_cmdlist);
3450 add_prefix_cmd ("pt", class_maintenance, maint_btrace_pt_show_cmd, _("\
3451 Show Intel Processor Trace specific variables."),
3452 &maint_btrace_pt_show_cmdlist, "maintenance show btrace pt ",
3453 0, &maint_btrace_show_cmdlist);
3455 add_setshow_boolean_cmd ("skip-pad", class_maintenance,
3456 &maint_btrace_pt_skip_pad, _("\
3457 Set whether PAD packets should be skipped in the btrace packet history."), _("\
3458 Show whether PAD packets should be skipped in the btrace packet history."),_("\
3459 When enabled, PAD packets are ignored in the btrace packet history."),
3460 NULL, show_maint_btrace_pt_skip_pad,
3461 &maint_btrace_pt_set_cmdlist,
3462 &maint_btrace_pt_show_cmdlist);
3464 add_cmd ("packet-history", class_maintenance, maint_btrace_packet_history_cmd,
3465 _("Print the raw branch tracing data.\n\
3466 With no argument, print ten more packets after the previous ten-line print.\n\
3467 With '-' as argument print ten packets before a previous ten-line print.\n\
3468 One argument specifies the starting packet of a ten-line print.\n\
3469 Two arguments with comma between specify starting and ending packets to \
3471 Preceded with '+'/'-' the second argument specifies the distance from the \
3473 &maint_btrace_cmdlist);
3475 add_cmd ("clear-packet-history", class_maintenance,
3476 maint_btrace_clear_packet_history_cmd,
3477 _("Clears the branch tracing packet history.\n\
3478 Discards the raw branch tracing data but not the execution history data.\n\
3480 &maint_btrace_cmdlist);
3482 add_cmd ("clear", class_maintenance, maint_btrace_clear_cmd,
3483 _("Clears the branch tracing data.\n\
3484 Discards the raw branch tracing data and the execution history data.\n\
3485 The next 'record' command will fetch the branch tracing data anew.\n\
3487 &maint_btrace_cmdlist);