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 at the end of
207 BTINFO is the branch trace information for the current thread.
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 minimal_symbol *mfun,
215 struct btrace_function *bfun, *prev;
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);
245 /* Update the UP field of a function segment. */
248 ftrace_update_caller (struct btrace_function *bfun,
249 struct btrace_function *caller,
250 enum btrace_function_flag flags)
252 if (bfun->up != NULL)
253 ftrace_debug (bfun, "updating caller");
258 ftrace_debug (bfun, "set caller");
259 ftrace_debug (caller, "..to");
262 /* Fix up the caller for all segments of a function. */
265 ftrace_fixup_caller (struct btrace_function *bfun,
266 struct btrace_function *caller,
267 enum btrace_function_flag flags)
269 struct btrace_function *prev, *next;
271 ftrace_update_caller (bfun, caller, flags);
273 /* Update all function segments belonging to the same function. */
274 for (prev = bfun->segment.prev; prev != NULL; prev = prev->segment.prev)
275 ftrace_update_caller (prev, caller, flags);
277 for (next = bfun->segment.next; next != NULL; next = next->segment.next)
278 ftrace_update_caller (next, caller, flags);
281 /* Add a new function segment for a call at the end of the trace.
282 BTINFO is the branch trace information for the current thread.
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 minimal_symbol *mfun,
290 struct btrace_function *caller = btinfo->end;
291 struct btrace_function *bfun = ftrace_new_function (btinfo, mfun, fun);
296 ftrace_debug (bfun, "new call");
301 /* Add a new function segment for a tail call at the end of the trace.
302 BTINFO is the branch trace information for the current thread.
303 MFUN and FUN are the symbol information we have for this function. */
305 static struct btrace_function *
306 ftrace_new_tailcall (struct btrace_thread_info *btinfo,
307 struct minimal_symbol *mfun,
310 struct btrace_function *caller = btinfo->end;
311 struct btrace_function *bfun = ftrace_new_function (btinfo, mfun, fun);
315 bfun->flags |= BFUN_UP_LINKS_TO_TAILCALL;
317 ftrace_debug (bfun, "new tail call");
322 /* Return the caller of BFUN or NULL if there is none. This function skips
323 tail calls in the call chain. */
324 static struct btrace_function *
325 ftrace_get_caller (struct btrace_function *bfun)
327 for (; bfun != NULL; bfun = bfun->up)
328 if ((bfun->flags & BFUN_UP_LINKS_TO_TAILCALL) == 0)
334 /* Find the innermost caller in the back trace of BFUN with MFUN/FUN
335 symbol information. */
337 static struct btrace_function *
338 ftrace_find_caller (struct btrace_function *bfun,
339 struct minimal_symbol *mfun,
342 for (; bfun != NULL; bfun = bfun->up)
344 /* Skip functions with incompatible symbol information. */
345 if (ftrace_function_switched (bfun, mfun, fun))
348 /* This is the function segment we're looking for. */
355 /* Find the innermost caller in the back trace of BFUN, skipping all
356 function segments that do not end with a call instruction (e.g.
357 tail calls ending with a jump). */
359 static struct btrace_function *
360 ftrace_find_call (struct btrace_function *bfun)
362 for (; bfun != NULL; bfun = bfun->up)
364 struct btrace_insn *last;
367 if (bfun->errcode != 0)
370 last = VEC_last (btrace_insn_s, bfun->insn);
372 if (last->iclass == BTRACE_INSN_CALL)
379 /* Add a continuation segment for a function into which we return at the end of
381 BTINFO is the branch trace information for the current thread.
382 MFUN and FUN are the symbol information we have for this function. */
384 static struct btrace_function *
385 ftrace_new_return (struct btrace_thread_info *btinfo,
386 struct minimal_symbol *mfun,
389 struct btrace_function *prev = btinfo->end;
390 struct btrace_function *bfun, *caller;
392 bfun = ftrace_new_function (btinfo, mfun, fun);
394 /* It is important to start at PREV's caller. Otherwise, we might find
395 PREV itself, if PREV is a recursive function. */
396 caller = ftrace_find_caller (prev->up, mfun, fun);
399 /* The caller of PREV is the preceding btrace function segment in this
400 function instance. */
401 gdb_assert (caller->segment.next == NULL);
403 caller->segment.next = bfun;
404 bfun->segment.prev = caller;
406 /* Maintain the function level. */
407 bfun->level = caller->level;
409 /* Maintain the call stack. */
410 bfun->up = caller->up;
411 bfun->flags = caller->flags;
413 ftrace_debug (bfun, "new return");
417 /* We did not find a caller. This could mean that something went
418 wrong or that the call is simply not included in the trace. */
420 /* Let's search for some actual call. */
421 caller = ftrace_find_call (prev->up);
424 /* There is no call in PREV's back trace. We assume that the
425 branch trace did not include it. */
427 /* Let's find the topmost function and add a new caller for it.
428 This should handle a series of initial tail calls. */
429 while (prev->up != NULL)
432 bfun->level = prev->level - 1;
434 /* Fix up the call stack for PREV. */
435 ftrace_fixup_caller (prev, bfun, BFUN_UP_LINKS_TO_RET);
437 ftrace_debug (bfun, "new return - no caller");
441 /* There is a call in PREV's back trace to which we should have
442 returned but didn't. Let's start a new, separate back trace
443 from PREV's level. */
444 bfun->level = prev->level - 1;
446 /* We fix up the back trace for PREV but leave other function segments
447 on the same level as they are.
448 This should handle things like schedule () correctly where we're
449 switching contexts. */
451 prev->flags = BFUN_UP_LINKS_TO_RET;
453 ftrace_debug (bfun, "new return - unknown caller");
460 /* Add a new function segment for a function switch at the end of the trace.
461 BTINFO is the branch trace information for the current thread.
462 MFUN and FUN are the symbol information we have for this function. */
464 static struct btrace_function *
465 ftrace_new_switch (struct btrace_thread_info *btinfo,
466 struct minimal_symbol *mfun,
469 struct btrace_function *prev = btinfo->end;
470 struct btrace_function *bfun;
472 /* This is an unexplained function switch. We can't really be sure about the
473 call stack, yet the best I can think of right now is to preserve it. */
474 bfun = ftrace_new_function (btinfo, mfun, fun);
476 bfun->flags = prev->flags;
478 ftrace_debug (bfun, "new switch");
483 /* Add a new function segment for a gap in the trace due to a decode error at
484 the end of the trace.
485 BTINFO is the branch trace information for the current thread.
486 ERRCODE is the format-specific error code. */
488 static struct btrace_function *
489 ftrace_new_gap (struct btrace_thread_info *btinfo, int errcode)
491 struct btrace_function *prev = btinfo->end;
492 struct btrace_function *bfun;
494 /* We hijack prev if it was empty. */
495 if (prev != NULL && prev->errcode == 0
496 && VEC_empty (btrace_insn_s, prev->insn))
499 bfun = ftrace_new_function (btinfo, NULL, NULL);
501 bfun->errcode = errcode;
503 ftrace_debug (bfun, "new gap");
508 /* Update the current function segment at the end of the trace in BTINFO with
509 respect to the instruction at PC. This may create new function segments.
510 Return the chronologically latest function segment, never NULL. */
512 static struct btrace_function *
513 ftrace_update_function (struct btrace_thread_info *btinfo, CORE_ADDR pc)
515 struct bound_minimal_symbol bmfun;
516 struct minimal_symbol *mfun;
518 struct btrace_insn *last;
519 struct btrace_function *bfun = btinfo->end;
521 /* Try to determine the function we're in. We use both types of symbols
522 to avoid surprises when we sometimes get a full symbol and sometimes
523 only a minimal symbol. */
524 fun = find_pc_function (pc);
525 bmfun = lookup_minimal_symbol_by_pc (pc);
528 if (fun == NULL && mfun == NULL)
529 DEBUG_FTRACE ("no symbol at %s", core_addr_to_string_nz (pc));
531 /* If we didn't have a function or if we had a gap before, we create one. */
532 if (bfun == NULL || bfun->errcode != 0)
533 return ftrace_new_function (btinfo, mfun, fun);
535 /* Check the last instruction, if we have one.
536 We do this check first, since it allows us to fill in the call stack
537 links in addition to the normal flow links. */
539 if (!VEC_empty (btrace_insn_s, bfun->insn))
540 last = VEC_last (btrace_insn_s, bfun->insn);
544 switch (last->iclass)
546 case BTRACE_INSN_RETURN:
550 /* On some systems, _dl_runtime_resolve returns to the resolved
551 function instead of jumping to it. From our perspective,
552 however, this is a tailcall.
553 If we treated it as return, we wouldn't be able to find the
554 resolved function in our stack back trace. Hence, we would
555 lose the current stack back trace and start anew with an empty
556 back trace. When the resolved function returns, we would then
557 create a stack back trace with the same function names but
558 different frame id's. This will confuse stepping. */
559 fname = ftrace_print_function_name (bfun);
560 if (strcmp (fname, "_dl_runtime_resolve") == 0)
561 return ftrace_new_tailcall (btinfo, mfun, fun);
563 return ftrace_new_return (btinfo, mfun, fun);
566 case BTRACE_INSN_CALL:
567 /* Ignore calls to the next instruction. They are used for PIC. */
568 if (last->pc + last->size == pc)
571 return ftrace_new_call (btinfo, mfun, fun);
573 case BTRACE_INSN_JUMP:
577 start = get_pc_function_start (pc);
579 /* A jump to the start of a function is (typically) a tail call. */
581 return ftrace_new_tailcall (btinfo, mfun, fun);
583 /* If we can't determine the function for PC, we treat a jump at
584 the end of the block as tail call if we're switching functions
585 and as an intra-function branch if we don't. */
586 if (start == 0 && ftrace_function_switched (bfun, mfun, fun))
587 return ftrace_new_tailcall (btinfo, mfun, fun);
594 /* Check if we're switching functions for some other reason. */
595 if (ftrace_function_switched (bfun, mfun, fun))
597 DEBUG_FTRACE ("switching from %s in %s at %s",
598 ftrace_print_insn_addr (last),
599 ftrace_print_function_name (bfun),
600 ftrace_print_filename (bfun));
602 return ftrace_new_switch (btinfo, mfun, fun);
608 /* Add the instruction at PC to BFUN's instructions. */
611 ftrace_update_insns (struct btrace_function *bfun,
612 const struct btrace_insn *insn)
614 VEC_safe_push (btrace_insn_s, bfun->insn, insn);
616 if (record_debug > 1)
617 ftrace_debug (bfun, "update insn");
620 /* Classify the instruction at PC. */
622 static enum btrace_insn_class
623 ftrace_classify_insn (struct gdbarch *gdbarch, CORE_ADDR pc)
625 enum btrace_insn_class iclass;
627 iclass = BTRACE_INSN_OTHER;
630 if (gdbarch_insn_is_call (gdbarch, pc))
631 iclass = BTRACE_INSN_CALL;
632 else if (gdbarch_insn_is_ret (gdbarch, pc))
633 iclass = BTRACE_INSN_RETURN;
634 else if (gdbarch_insn_is_jump (gdbarch, pc))
635 iclass = BTRACE_INSN_JUMP;
637 CATCH (error, RETURN_MASK_ERROR)
645 /* Try to match the back trace at LHS to the back trace at RHS. Returns the
646 number of matching function segments or zero if the back traces do not
650 ftrace_match_backtrace (struct btrace_function *lhs,
651 struct btrace_function *rhs)
655 for (matches = 0; lhs != NULL && rhs != NULL; ++matches)
657 if (ftrace_function_switched (lhs, rhs->msym, rhs->sym))
660 lhs = ftrace_get_caller (lhs);
661 rhs = ftrace_get_caller (rhs);
667 /* Add ADJUSTMENT to the level of BFUN and succeeding function segments. */
670 ftrace_fixup_level (struct btrace_function *bfun, int adjustment)
675 DEBUG_FTRACE ("fixup level (%+d)", adjustment);
676 ftrace_debug (bfun, "..bfun");
678 for (; bfun != NULL; bfun = bfun->flow.next)
679 bfun->level += adjustment;
682 /* Recompute the global level offset. Traverse the function trace and compute
683 the global level offset as the negative of the minimal function level. */
686 ftrace_compute_global_level_offset (struct btrace_thread_info *btinfo)
688 struct btrace_function *bfun, *end;
694 bfun = btinfo->begin;
698 /* The last function segment contains the current instruction, which is not
699 really part of the trace. If it contains just this one instruction, we
700 stop when we reach it; otherwise, we let the below loop run to the end. */
702 if (VEC_length (btrace_insn_s, end->insn) > 1)
706 for (; bfun != end; bfun = bfun->flow.next)
707 level = std::min (level, bfun->level);
709 DEBUG_FTRACE ("setting global level offset: %d", -level);
710 btinfo->level = -level;
713 /* Connect the function segments PREV and NEXT in a bottom-to-top walk as in
714 ftrace_connect_backtrace. */
717 ftrace_connect_bfun (struct btrace_function *prev,
718 struct btrace_function *next)
720 DEBUG_FTRACE ("connecting...");
721 ftrace_debug (prev, "..prev");
722 ftrace_debug (next, "..next");
724 /* The function segments are not yet connected. */
725 gdb_assert (prev->segment.next == NULL);
726 gdb_assert (next->segment.prev == NULL);
728 prev->segment.next = next;
729 next->segment.prev = prev;
731 /* We may have moved NEXT to a different function level. */
732 ftrace_fixup_level (next, prev->level - next->level);
734 /* If we run out of back trace for one, let's use the other's. */
735 if (prev->up == NULL)
737 if (next->up != NULL)
739 DEBUG_FTRACE ("using next's callers");
740 ftrace_fixup_caller (prev, next->up, next->flags);
743 else if (next->up == NULL)
745 if (prev->up != NULL)
747 DEBUG_FTRACE ("using prev's callers");
748 ftrace_fixup_caller (next, prev->up, prev->flags);
753 /* PREV may have a tailcall caller, NEXT can't. If it does, fixup the up
754 link to add the tail callers to NEXT's back trace.
756 This removes NEXT->UP from NEXT's back trace. It will be added back
757 when connecting NEXT and PREV's callers - provided they exist.
759 If PREV's back trace consists of a series of tail calls without an
760 actual call, there will be no further connection and NEXT's caller will
761 be removed for good. To catch this case, we handle it here and connect
762 the top of PREV's back trace to NEXT's caller. */
763 if ((prev->flags & BFUN_UP_LINKS_TO_TAILCALL) != 0)
765 struct btrace_function *caller;
766 btrace_function_flags flags;
768 /* We checked NEXT->UP above so CALLER can't be NULL. */
772 DEBUG_FTRACE ("adding prev's tail calls to next");
774 ftrace_fixup_caller (next, prev->up, prev->flags);
776 for (prev = prev->up; prev != NULL; prev = prev->up)
778 /* At the end of PREV's back trace, continue with CALLER. */
779 if (prev->up == NULL)
781 DEBUG_FTRACE ("fixing up link for tailcall chain");
782 ftrace_debug (prev, "..top");
783 ftrace_debug (caller, "..up");
785 ftrace_fixup_caller (prev, caller, flags);
787 /* If we skipped any tail calls, this may move CALLER to a
788 different function level.
790 Note that changing CALLER's level is only OK because we
791 know that this is the last iteration of the bottom-to-top
792 walk in ftrace_connect_backtrace.
794 Otherwise we will fix up CALLER's level when we connect it
795 to PREV's caller in the next iteration. */
796 ftrace_fixup_level (caller, prev->level - caller->level - 1);
800 /* There's nothing to do if we find a real call. */
801 if ((prev->flags & BFUN_UP_LINKS_TO_TAILCALL) == 0)
803 DEBUG_FTRACE ("will fix up link in next iteration");
811 /* Connect function segments on the same level in the back trace at LHS and RHS.
812 The back traces at LHS and RHS are expected to match according to
813 ftrace_match_backtrace. */
816 ftrace_connect_backtrace (struct btrace_function *lhs,
817 struct btrace_function *rhs)
819 while (lhs != NULL && rhs != NULL)
821 struct btrace_function *prev, *next;
823 gdb_assert (!ftrace_function_switched (lhs, rhs->msym, rhs->sym));
825 /* Connecting LHS and RHS may change the up link. */
829 lhs = ftrace_get_caller (lhs);
830 rhs = ftrace_get_caller (rhs);
832 ftrace_connect_bfun (prev, next);
836 /* Bridge the gap between two function segments left and right of a gap if their
837 respective back traces match in at least MIN_MATCHES functions.
839 Returns non-zero if the gap could be bridged, zero otherwise. */
842 ftrace_bridge_gap (struct btrace_function *lhs, struct btrace_function *rhs,
845 struct btrace_function *best_l, *best_r, *cand_l, *cand_r;
848 DEBUG_FTRACE ("checking gap at insn %u (req matches: %d)",
849 rhs->insn_offset - 1, min_matches);
855 /* We search the back traces of LHS and RHS for valid connections and connect
856 the two functon segments that give the longest combined back trace. */
858 for (cand_l = lhs; cand_l != NULL; cand_l = ftrace_get_caller (cand_l))
859 for (cand_r = rhs; cand_r != NULL; cand_r = ftrace_get_caller (cand_r))
863 matches = ftrace_match_backtrace (cand_l, cand_r);
864 if (best_matches < matches)
866 best_matches = matches;
872 /* We need at least MIN_MATCHES matches. */
873 gdb_assert (min_matches > 0);
874 if (best_matches < min_matches)
877 DEBUG_FTRACE ("..matches: %d", best_matches);
879 /* We will fix up the level of BEST_R and succeeding function segments such
880 that BEST_R's level matches BEST_L's when we connect BEST_L to BEST_R.
882 This will ignore the level of RHS and following if BEST_R != RHS. I.e. if
883 BEST_R is a successor of RHS in the back trace of RHS (phases 1 and 3).
885 To catch this, we already fix up the level here where we can start at RHS
886 instead of at BEST_R. We will ignore the level fixup when connecting
887 BEST_L to BEST_R as they will already be on the same level. */
888 ftrace_fixup_level (rhs, best_l->level - best_r->level);
890 ftrace_connect_backtrace (best_l, best_r);
895 /* Try to bridge gaps due to overflow or decode errors by connecting the
896 function segments that are separated by the gap. */
899 btrace_bridge_gaps (struct thread_info *tp, VEC (bfun_s) **gaps)
901 VEC (bfun_s) *remaining;
902 struct cleanup *old_chain;
905 DEBUG ("bridge gaps");
908 old_chain = make_cleanup (VEC_cleanup (bfun_s), &remaining);
910 /* We require a minimum amount of matches for bridging a gap. The number of
911 required matches will be lowered with each iteration.
913 The more matches the higher our confidence that the bridging is correct.
914 For big gaps or small traces, however, it may not be feasible to require a
915 high number of matches. */
916 for (min_matches = 5; min_matches > 0; --min_matches)
918 /* Let's try to bridge as many gaps as we can. In some cases, we need to
919 skip a gap and revisit it again after we closed later gaps. */
920 while (!VEC_empty (bfun_s, *gaps))
922 struct btrace_function *gap;
925 for (idx = 0; VEC_iterate (bfun_s, *gaps, idx, gap); ++idx)
927 struct btrace_function *lhs, *rhs;
930 /* We may have a sequence of gaps if we run from one error into
931 the next as we try to re-sync onto the trace stream. Ignore
932 all but the leftmost gap in such a sequence.
934 Also ignore gaps at the beginning of the trace. */
935 lhs = gap->flow.prev;
936 if (lhs == NULL || lhs->errcode != 0)
939 /* Skip gaps to the right. */
940 for (rhs = gap->flow.next; rhs != NULL; rhs = rhs->flow.next)
941 if (rhs->errcode == 0)
944 /* Ignore gaps at the end of the trace. */
948 bridged = ftrace_bridge_gap (lhs, rhs, min_matches);
950 /* Keep track of gaps we were not able to bridge and try again.
951 If we just pushed them to the end of GAPS we would risk an
952 infinite loop in case we simply cannot bridge a gap. */
954 VEC_safe_push (bfun_s, remaining, gap);
957 /* Let's see if we made any progress. */
958 if (VEC_length (bfun_s, remaining) == VEC_length (bfun_s, *gaps))
961 VEC_free (bfun_s, *gaps);
967 /* We get here if either GAPS is empty or if GAPS equals REMAINING. */
968 if (VEC_empty (bfun_s, *gaps))
971 VEC_free (bfun_s, remaining);
974 do_cleanups (old_chain);
976 /* We may omit this in some cases. Not sure it is worth the extra
977 complication, though. */
978 ftrace_compute_global_level_offset (&tp->btrace);
981 /* Compute the function branch trace from BTS trace. */
984 btrace_compute_ftrace_bts (struct thread_info *tp,
985 const struct btrace_data_bts *btrace,
988 struct btrace_thread_info *btinfo;
989 struct btrace_function *begin, *end;
990 struct gdbarch *gdbarch;
994 gdbarch = target_gdbarch ();
995 btinfo = &tp->btrace;
996 begin = btinfo->begin;
998 level = begin != NULL ? -btinfo->level : INT_MAX;
999 blk = VEC_length (btrace_block_s, btrace->blocks);
1003 btrace_block_s *block;
1008 block = VEC_index (btrace_block_s, btrace->blocks, blk);
1013 struct btrace_insn insn;
1016 /* We should hit the end of the block. Warn if we went too far. */
1017 if (block->end < pc)
1019 /* Indicate the gap in the trace. */
1020 end = ftrace_new_gap (btinfo, BDE_BTS_OVERFLOW);
1024 VEC_safe_push (bfun_s, *gaps, end);
1026 warning (_("Recorded trace may be corrupted at instruction "
1027 "%u (pc = %s)."), end->insn_offset - 1,
1028 core_addr_to_string_nz (pc));
1033 end = ftrace_update_function (btinfo, pc);
1037 /* Maintain the function level offset.
1038 For all but the last block, we do it here. */
1040 level = std::min (level, end->level);
1045 size = gdb_insn_length (gdbarch, pc);
1047 CATCH (error, RETURN_MASK_ERROR)
1054 insn.iclass = ftrace_classify_insn (gdbarch, pc);
1057 ftrace_update_insns (end, &insn);
1059 /* We're done once we pushed the instruction at the end. */
1060 if (block->end == pc)
1063 /* We can't continue if we fail to compute the size. */
1066 /* Indicate the gap in the trace. We just added INSN so we're
1067 not at the beginning. */
1068 end = ftrace_new_gap (btinfo, BDE_BTS_INSN_SIZE);
1070 VEC_safe_push (bfun_s, *gaps, end);
1072 warning (_("Recorded trace may be incomplete at instruction %u "
1073 "(pc = %s)."), end->insn_offset - 1,
1074 core_addr_to_string_nz (pc));
1081 /* Maintain the function level offset.
1082 For the last block, we do it here to not consider the last
1084 Since the last instruction corresponds to the current instruction
1085 and is not really part of the execution history, it shouldn't
1086 affect the level. */
1088 level = std::min (level, end->level);
1092 btinfo->begin = begin;
1095 /* LEVEL is the minimal function level of all btrace function segments.
1096 Define the global level offset to -LEVEL so all function levels are
1097 normalized to start at zero. */
1098 btinfo->level = -level;
1101 #if defined (HAVE_LIBIPT)
1103 static enum btrace_insn_class
1104 pt_reclassify_insn (enum pt_insn_class iclass)
1109 return BTRACE_INSN_CALL;
1112 return BTRACE_INSN_RETURN;
1115 return BTRACE_INSN_JUMP;
1118 return BTRACE_INSN_OTHER;
1122 /* Return the btrace instruction flags for INSN. */
1124 static btrace_insn_flags
1125 pt_btrace_insn_flags (const struct pt_insn &insn)
1127 btrace_insn_flags flags = 0;
1129 if (insn.speculative)
1130 flags |= BTRACE_INSN_FLAG_SPECULATIVE;
1135 /* Return the btrace instruction for INSN. */
1138 pt_btrace_insn (const struct pt_insn &insn)
1140 return {(CORE_ADDR) insn.ip, (gdb_byte) insn.size,
1141 pt_reclassify_insn (insn.iclass),
1142 pt_btrace_insn_flags (insn)};
1146 /* Add function branch trace to BTINFO using DECODER. */
1149 ftrace_add_pt (struct btrace_thread_info *btinfo,
1150 struct pt_insn_decoder *decoder,
1151 struct btrace_function **pbegin,
1152 struct btrace_function **pend, int *plevel,
1153 VEC (bfun_s) **gaps)
1155 struct btrace_function *begin, *end, *upd;
1163 struct pt_insn insn;
1165 errcode = pt_insn_sync_forward (decoder);
1168 if (errcode != -pte_eos)
1169 warning (_("Failed to synchronize onto the Intel Processor "
1170 "Trace stream: %s."), pt_errstr (pt_errcode (errcode)));
1176 errcode = pt_insn_next (decoder, &insn, sizeof(insn));
1180 /* Look for gaps in the trace - unless we're at the beginning. */
1183 /* Tracing is disabled and re-enabled each time we enter the
1184 kernel. Most times, we continue from the same instruction we
1185 stopped before. This is indicated via the RESUMED instruction
1186 flag. The ENABLED instruction flag means that we continued
1187 from some other instruction. Indicate this as a trace gap. */
1190 *pend = end = ftrace_new_gap (btinfo, BDE_PT_DISABLED);
1192 VEC_safe_push (bfun_s, *gaps, end);
1194 pt_insn_get_offset (decoder, &offset);
1196 warning (_("Non-contiguous trace at instruction %u (offset "
1197 "= 0x%" PRIx64 ", pc = 0x%" PRIx64 ")."),
1198 end->insn_offset - 1, offset, insn.ip);
1202 /* Indicate trace overflows. */
1205 *pend = end = ftrace_new_gap (btinfo, BDE_PT_OVERFLOW);
1207 *pbegin = begin = end;
1209 VEC_safe_push (bfun_s, *gaps, end);
1211 pt_insn_get_offset (decoder, &offset);
1213 warning (_("Overflow at instruction %u (offset = 0x%" PRIx64
1214 ", pc = 0x%" PRIx64 ")."), end->insn_offset - 1,
1218 upd = ftrace_update_function (btinfo, insn.ip);
1224 *pbegin = begin = upd;
1227 /* Maintain the function level offset. */
1228 *plevel = std::min (*plevel, end->level);
1230 btrace_insn btinsn = pt_btrace_insn (insn);
1231 ftrace_update_insns (end, &btinsn);
1234 if (errcode == -pte_eos)
1237 /* Indicate the gap in the trace. */
1238 *pend = end = ftrace_new_gap (btinfo, errcode);
1240 *pbegin = begin = end;
1242 VEC_safe_push (bfun_s, *gaps, end);
1244 pt_insn_get_offset (decoder, &offset);
1246 warning (_("Decode error (%d) at instruction %u (offset = 0x%" PRIx64
1247 ", pc = 0x%" PRIx64 "): %s."), errcode, end->insn_offset - 1,
1248 offset, insn.ip, pt_errstr (pt_errcode (errcode)));
1252 /* A callback function to allow the trace decoder to read the inferior's
1256 btrace_pt_readmem_callback (gdb_byte *buffer, size_t size,
1257 const struct pt_asid *asid, uint64_t pc,
1260 int result, errcode;
1262 result = (int) size;
1265 errcode = target_read_code ((CORE_ADDR) pc, buffer, size);
1267 result = -pte_nomap;
1269 CATCH (error, RETURN_MASK_ERROR)
1271 result = -pte_nomap;
1278 /* Translate the vendor from one enum to another. */
1280 static enum pt_cpu_vendor
1281 pt_translate_cpu_vendor (enum btrace_cpu_vendor vendor)
1293 /* Finalize the function branch trace after decode. */
1295 static void btrace_finalize_ftrace_pt (struct pt_insn_decoder *decoder,
1296 struct thread_info *tp, int level)
1298 pt_insn_free_decoder (decoder);
1300 /* LEVEL is the minimal function level of all btrace function segments.
1301 Define the global level offset to -LEVEL so all function levels are
1302 normalized to start at zero. */
1303 tp->btrace.level = -level;
1305 /* Add a single last instruction entry for the current PC.
1306 This allows us to compute the backtrace at the current PC using both
1307 standard unwind and btrace unwind.
1308 This extra entry is ignored by all record commands. */
1312 /* Compute the function branch trace from Intel Processor Trace
1316 btrace_compute_ftrace_pt (struct thread_info *tp,
1317 const struct btrace_data_pt *btrace,
1318 VEC (bfun_s) **gaps)
1320 struct btrace_thread_info *btinfo;
1321 struct pt_insn_decoder *decoder;
1322 struct pt_config config;
1325 if (btrace->size == 0)
1328 btinfo = &tp->btrace;
1329 level = btinfo->begin != NULL ? -btinfo->level : INT_MAX;
1331 pt_config_init(&config);
1332 config.begin = btrace->data;
1333 config.end = btrace->data + btrace->size;
1335 config.cpu.vendor = pt_translate_cpu_vendor (btrace->config.cpu.vendor);
1336 config.cpu.family = btrace->config.cpu.family;
1337 config.cpu.model = btrace->config.cpu.model;
1338 config.cpu.stepping = btrace->config.cpu.stepping;
1340 errcode = pt_cpu_errata (&config.errata, &config.cpu);
1342 error (_("Failed to configure the Intel Processor Trace decoder: %s."),
1343 pt_errstr (pt_errcode (errcode)));
1345 decoder = pt_insn_alloc_decoder (&config);
1346 if (decoder == NULL)
1347 error (_("Failed to allocate the Intel Processor Trace decoder."));
1351 struct pt_image *image;
1353 image = pt_insn_get_image(decoder);
1355 error (_("Failed to configure the Intel Processor Trace decoder."));
1357 errcode = pt_image_set_callback(image, btrace_pt_readmem_callback, NULL);
1359 error (_("Failed to configure the Intel Processor Trace decoder: "
1360 "%s."), pt_errstr (pt_errcode (errcode)));
1362 ftrace_add_pt (btinfo, decoder, &btinfo->begin, &btinfo->end, &level,
1365 CATCH (error, RETURN_MASK_ALL)
1367 /* Indicate a gap in the trace if we quit trace processing. */
1368 if (error.reason == RETURN_QUIT && btinfo->end != NULL)
1370 btinfo->end = ftrace_new_gap (btinfo, BDE_PT_USER_QUIT);
1372 VEC_safe_push (bfun_s, *gaps, btinfo->end);
1375 btrace_finalize_ftrace_pt (decoder, tp, level);
1377 throw_exception (error);
1381 btrace_finalize_ftrace_pt (decoder, tp, level);
1384 #else /* defined (HAVE_LIBIPT) */
1387 btrace_compute_ftrace_pt (struct thread_info *tp,
1388 const struct btrace_data_pt *btrace,
1389 VEC (bfun_s) **gaps)
1391 internal_error (__FILE__, __LINE__, _("Unexpected branch trace format."));
1394 #endif /* defined (HAVE_LIBIPT) */
1396 /* Compute the function branch trace from a block branch trace BTRACE for
1397 a thread given by BTINFO. */
1400 btrace_compute_ftrace_1 (struct thread_info *tp, struct btrace_data *btrace,
1401 VEC (bfun_s) **gaps)
1403 DEBUG ("compute ftrace");
1405 switch (btrace->format)
1407 case BTRACE_FORMAT_NONE:
1410 case BTRACE_FORMAT_BTS:
1411 btrace_compute_ftrace_bts (tp, &btrace->variant.bts, gaps);
1414 case BTRACE_FORMAT_PT:
1415 btrace_compute_ftrace_pt (tp, &btrace->variant.pt, gaps);
1419 internal_error (__FILE__, __LINE__, _("Unkown branch trace format."));
1423 btrace_finalize_ftrace (struct thread_info *tp, VEC (bfun_s) **gaps)
1425 if (!VEC_empty (bfun_s, *gaps))
1427 tp->btrace.ngaps += VEC_length (bfun_s, *gaps);
1428 btrace_bridge_gaps (tp, gaps);
1433 btrace_compute_ftrace (struct thread_info *tp, struct btrace_data *btrace)
1436 struct cleanup *old_chain;
1439 old_chain = make_cleanup (VEC_cleanup (bfun_s), &gaps);
1443 btrace_compute_ftrace_1 (tp, btrace, &gaps);
1445 CATCH (error, RETURN_MASK_ALL)
1447 btrace_finalize_ftrace (tp, &gaps);
1449 throw_exception (error);
1453 btrace_finalize_ftrace (tp, &gaps);
1455 do_cleanups (old_chain);
1458 /* Add an entry for the current PC. */
1461 btrace_add_pc (struct thread_info *tp)
1463 struct btrace_data btrace;
1464 struct btrace_block *block;
1465 struct regcache *regcache;
1466 struct cleanup *cleanup;
1469 regcache = get_thread_regcache (tp->ptid);
1470 pc = regcache_read_pc (regcache);
1472 btrace_data_init (&btrace);
1473 btrace.format = BTRACE_FORMAT_BTS;
1474 btrace.variant.bts.blocks = NULL;
1476 cleanup = make_cleanup_btrace_data (&btrace);
1478 block = VEC_safe_push (btrace_block_s, btrace.variant.bts.blocks, NULL);
1482 btrace_compute_ftrace (tp, &btrace);
1484 do_cleanups (cleanup);
1490 btrace_enable (struct thread_info *tp, const struct btrace_config *conf)
1492 if (tp->btrace.target != NULL)
1495 #if !defined (HAVE_LIBIPT)
1496 if (conf->format == BTRACE_FORMAT_PT)
1497 error (_("GDB does not support Intel Processor Trace."));
1498 #endif /* !defined (HAVE_LIBIPT) */
1500 if (!target_supports_btrace (conf->format))
1501 error (_("Target does not support branch tracing."));
1503 DEBUG ("enable thread %s (%s)", print_thread_id (tp),
1504 target_pid_to_str (tp->ptid));
1506 tp->btrace.target = target_enable_btrace (tp->ptid, conf);
1508 /* We're done if we failed to enable tracing. */
1509 if (tp->btrace.target == NULL)
1512 /* We need to undo the enable in case of errors. */
1515 /* Add an entry for the current PC so we start tracing from where we
1518 If we can't access TP's registers, TP is most likely running. In this
1519 case, we can't really say where tracing was enabled so it should be
1520 safe to simply skip this step.
1522 This is not relevant for BTRACE_FORMAT_PT since the trace will already
1523 start at the PC at which tracing was enabled. */
1524 if (conf->format != BTRACE_FORMAT_PT
1525 && can_access_registers_ptid (tp->ptid))
1528 CATCH (exception, RETURN_MASK_ALL)
1530 btrace_disable (tp);
1532 throw_exception (exception);
1539 const struct btrace_config *
1540 btrace_conf (const struct btrace_thread_info *btinfo)
1542 if (btinfo->target == NULL)
1545 return target_btrace_conf (btinfo->target);
1551 btrace_disable (struct thread_info *tp)
1553 struct btrace_thread_info *btp = &tp->btrace;
1556 if (btp->target == NULL)
1559 DEBUG ("disable thread %s (%s)", print_thread_id (tp),
1560 target_pid_to_str (tp->ptid));
1562 target_disable_btrace (btp->target);
1571 btrace_teardown (struct thread_info *tp)
1573 struct btrace_thread_info *btp = &tp->btrace;
1576 if (btp->target == NULL)
1579 DEBUG ("teardown thread %s (%s)", print_thread_id (tp),
1580 target_pid_to_str (tp->ptid));
1582 target_teardown_btrace (btp->target);
1588 /* Stitch branch trace in BTS format. */
1591 btrace_stitch_bts (struct btrace_data_bts *btrace, struct thread_info *tp)
1593 struct btrace_thread_info *btinfo;
1594 struct btrace_function *last_bfun;
1595 struct btrace_insn *last_insn;
1596 btrace_block_s *first_new_block;
1598 btinfo = &tp->btrace;
1599 last_bfun = btinfo->end;
1600 gdb_assert (last_bfun != NULL);
1601 gdb_assert (!VEC_empty (btrace_block_s, btrace->blocks));
1603 /* If the existing trace ends with a gap, we just glue the traces
1604 together. We need to drop the last (i.e. chronologically first) block
1605 of the new trace, though, since we can't fill in the start address.*/
1606 if (VEC_empty (btrace_insn_s, last_bfun->insn))
1608 VEC_pop (btrace_block_s, btrace->blocks);
1612 /* Beware that block trace starts with the most recent block, so the
1613 chronologically first block in the new trace is the last block in
1614 the new trace's block vector. */
1615 first_new_block = VEC_last (btrace_block_s, btrace->blocks);
1616 last_insn = VEC_last (btrace_insn_s, last_bfun->insn);
1618 /* If the current PC at the end of the block is the same as in our current
1619 trace, there are two explanations:
1620 1. we executed the instruction and some branch brought us back.
1621 2. we have not made any progress.
1622 In the first case, the delta trace vector should contain at least two
1624 In the second case, the delta trace vector should contain exactly one
1625 entry for the partial block containing the current PC. Remove it. */
1626 if (first_new_block->end == last_insn->pc
1627 && VEC_length (btrace_block_s, btrace->blocks) == 1)
1629 VEC_pop (btrace_block_s, btrace->blocks);
1633 DEBUG ("stitching %s to %s", ftrace_print_insn_addr (last_insn),
1634 core_addr_to_string_nz (first_new_block->end));
1636 /* Do a simple sanity check to make sure we don't accidentally end up
1637 with a bad block. This should not occur in practice. */
1638 if (first_new_block->end < last_insn->pc)
1640 warning (_("Error while trying to read delta trace. Falling back to "
1645 /* We adjust the last block to start at the end of our current trace. */
1646 gdb_assert (first_new_block->begin == 0);
1647 first_new_block->begin = last_insn->pc;
1649 /* We simply pop the last insn so we can insert it again as part of
1650 the normal branch trace computation.
1651 Since instruction iterators are based on indices in the instructions
1652 vector, we don't leave any pointers dangling. */
1653 DEBUG ("pruning insn at %s for stitching",
1654 ftrace_print_insn_addr (last_insn));
1656 VEC_pop (btrace_insn_s, last_bfun->insn);
1658 /* The instructions vector may become empty temporarily if this has
1659 been the only instruction in this function segment.
1660 This violates the invariant but will be remedied shortly by
1661 btrace_compute_ftrace when we add the new trace. */
1663 /* The only case where this would hurt is if the entire trace consisted
1664 of just that one instruction. If we remove it, we might turn the now
1665 empty btrace function segment into a gap. But we don't want gaps at
1666 the beginning. To avoid this, we remove the entire old trace. */
1667 if (last_bfun == btinfo->begin && VEC_empty (btrace_insn_s, last_bfun->insn))
1673 /* Adjust the block trace in order to stitch old and new trace together.
1674 BTRACE is the new delta trace between the last and the current stop.
1675 TP is the traced thread.
1676 May modifx BTRACE as well as the existing trace in TP.
1677 Return 0 on success, -1 otherwise. */
1680 btrace_stitch_trace (struct btrace_data *btrace, struct thread_info *tp)
1682 /* If we don't have trace, there's nothing to do. */
1683 if (btrace_data_empty (btrace))
1686 switch (btrace->format)
1688 case BTRACE_FORMAT_NONE:
1691 case BTRACE_FORMAT_BTS:
1692 return btrace_stitch_bts (&btrace->variant.bts, tp);
1694 case BTRACE_FORMAT_PT:
1695 /* Delta reads are not supported. */
1699 internal_error (__FILE__, __LINE__, _("Unkown branch trace format."));
1702 /* Clear the branch trace histories in BTINFO. */
1705 btrace_clear_history (struct btrace_thread_info *btinfo)
1707 xfree (btinfo->insn_history);
1708 xfree (btinfo->call_history);
1709 xfree (btinfo->replay);
1711 btinfo->insn_history = NULL;
1712 btinfo->call_history = NULL;
1713 btinfo->replay = NULL;
1716 /* Clear the branch trace maintenance histories in BTINFO. */
1719 btrace_maint_clear (struct btrace_thread_info *btinfo)
1721 switch (btinfo->data.format)
1726 case BTRACE_FORMAT_BTS:
1727 btinfo->maint.variant.bts.packet_history.begin = 0;
1728 btinfo->maint.variant.bts.packet_history.end = 0;
1731 #if defined (HAVE_LIBIPT)
1732 case BTRACE_FORMAT_PT:
1733 xfree (btinfo->maint.variant.pt.packets);
1735 btinfo->maint.variant.pt.packets = NULL;
1736 btinfo->maint.variant.pt.packet_history.begin = 0;
1737 btinfo->maint.variant.pt.packet_history.end = 0;
1739 #endif /* defined (HAVE_LIBIPT) */
1746 btrace_decode_error (enum btrace_format format, int errcode)
1750 case BTRACE_FORMAT_BTS:
1753 case BDE_BTS_OVERFLOW:
1754 return _("instruction overflow");
1756 case BDE_BTS_INSN_SIZE:
1757 return _("unknown instruction");
1764 #if defined (HAVE_LIBIPT)
1765 case BTRACE_FORMAT_PT:
1768 case BDE_PT_USER_QUIT:
1769 return _("trace decode cancelled");
1771 case BDE_PT_DISABLED:
1772 return _("disabled");
1774 case BDE_PT_OVERFLOW:
1775 return _("overflow");
1779 return pt_errstr (pt_errcode (errcode));
1783 #endif /* defined (HAVE_LIBIPT) */
1789 return _("unknown");
1795 btrace_fetch (struct thread_info *tp)
1797 struct btrace_thread_info *btinfo;
1798 struct btrace_target_info *tinfo;
1799 struct btrace_data btrace;
1800 struct cleanup *cleanup;
1803 DEBUG ("fetch thread %s (%s)", print_thread_id (tp),
1804 target_pid_to_str (tp->ptid));
1806 btinfo = &tp->btrace;
1807 tinfo = btinfo->target;
1811 /* There's no way we could get new trace while replaying.
1812 On the other hand, delta trace would return a partial record with the
1813 current PC, which is the replay PC, not the last PC, as expected. */
1814 if (btinfo->replay != NULL)
1817 /* With CLI usage, TP->PTID always equals INFERIOR_PTID here. Now that we
1818 can store a gdb.Record object in Python referring to a different thread
1819 than the current one, temporarily set INFERIOR_PTID. */
1820 cleanup = save_inferior_ptid ();
1821 inferior_ptid = tp->ptid;
1823 /* We should not be called on running or exited threads. */
1824 gdb_assert (can_access_registers_ptid (tp->ptid));
1826 btrace_data_init (&btrace);
1827 make_cleanup_btrace_data (&btrace);
1829 /* Let's first try to extend the trace we already have. */
1830 if (btinfo->end != NULL)
1832 errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_DELTA);
1835 /* Success. Let's try to stitch the traces together. */
1836 errcode = btrace_stitch_trace (&btrace, tp);
1840 /* We failed to read delta trace. Let's try to read new trace. */
1841 errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_NEW);
1843 /* If we got any new trace, discard what we have. */
1844 if (errcode == 0 && !btrace_data_empty (&btrace))
1848 /* If we were not able to read the trace, we start over. */
1852 errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_ALL);
1856 errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_ALL);
1858 /* If we were not able to read the branch trace, signal an error. */
1860 error (_("Failed to read branch trace."));
1862 /* Compute the trace, provided we have any. */
1863 if (!btrace_data_empty (&btrace))
1865 /* Store the raw trace data. The stored data will be cleared in
1866 btrace_clear, so we always append the new trace. */
1867 btrace_data_append (&btinfo->data, &btrace);
1868 btrace_maint_clear (btinfo);
1870 btrace_clear_history (btinfo);
1871 btrace_compute_ftrace (tp, &btrace);
1874 do_cleanups (cleanup);
1880 btrace_clear (struct thread_info *tp)
1882 struct btrace_thread_info *btinfo;
1884 DEBUG ("clear thread %s (%s)", print_thread_id (tp),
1885 target_pid_to_str (tp->ptid));
1887 /* Make sure btrace frames that may hold a pointer into the branch
1888 trace data are destroyed. */
1889 reinit_frame_cache ();
1891 btinfo = &tp->btrace;
1892 for (auto &bfun : btinfo->functions)
1894 VEC_free (btrace_insn_s, bfun->insn);
1898 btinfo->functions.clear ();
1899 btinfo->begin = NULL;
1903 /* Must clear the maint data before - it depends on BTINFO->DATA. */
1904 btrace_maint_clear (btinfo);
1905 btrace_data_clear (&btinfo->data);
1906 btrace_clear_history (btinfo);
1912 btrace_free_objfile (struct objfile *objfile)
1914 struct thread_info *tp;
1916 DEBUG ("free objfile");
1918 ALL_NON_EXITED_THREADS (tp)
1922 #if defined (HAVE_LIBEXPAT)
1924 /* Check the btrace document version. */
1927 check_xml_btrace_version (struct gdb_xml_parser *parser,
1928 const struct gdb_xml_element *element,
1929 void *user_data, VEC (gdb_xml_value_s) *attributes)
1932 = (const char *) xml_find_attribute (attributes, "version")->value;
1934 if (strcmp (version, "1.0") != 0)
1935 gdb_xml_error (parser, _("Unsupported btrace version: \"%s\""), version);
1938 /* Parse a btrace "block" xml record. */
1941 parse_xml_btrace_block (struct gdb_xml_parser *parser,
1942 const struct gdb_xml_element *element,
1943 void *user_data, VEC (gdb_xml_value_s) *attributes)
1945 struct btrace_data *btrace;
1946 struct btrace_block *block;
1947 ULONGEST *begin, *end;
1949 btrace = (struct btrace_data *) user_data;
1951 switch (btrace->format)
1953 case BTRACE_FORMAT_BTS:
1956 case BTRACE_FORMAT_NONE:
1957 btrace->format = BTRACE_FORMAT_BTS;
1958 btrace->variant.bts.blocks = NULL;
1962 gdb_xml_error (parser, _("Btrace format error."));
1965 begin = (ULONGEST *) xml_find_attribute (attributes, "begin")->value;
1966 end = (ULONGEST *) xml_find_attribute (attributes, "end")->value;
1968 block = VEC_safe_push (btrace_block_s, btrace->variant.bts.blocks, NULL);
1969 block->begin = *begin;
1973 /* Parse a "raw" xml record. */
1976 parse_xml_raw (struct gdb_xml_parser *parser, const char *body_text,
1977 gdb_byte **pdata, size_t *psize)
1979 struct cleanup *cleanup;
1980 gdb_byte *data, *bin;
1983 len = strlen (body_text);
1985 gdb_xml_error (parser, _("Bad raw data size."));
1989 bin = data = (gdb_byte *) xmalloc (size);
1990 cleanup = make_cleanup (xfree, data);
1992 /* We use hex encoding - see common/rsp-low.h. */
2000 if (hi == 0 || lo == 0)
2001 gdb_xml_error (parser, _("Bad hex encoding."));
2003 *bin++ = fromhex (hi) * 16 + fromhex (lo);
2007 discard_cleanups (cleanup);
2013 /* Parse a btrace pt-config "cpu" xml record. */
2016 parse_xml_btrace_pt_config_cpu (struct gdb_xml_parser *parser,
2017 const struct gdb_xml_element *element,
2019 VEC (gdb_xml_value_s) *attributes)
2021 struct btrace_data *btrace;
2023 ULONGEST *family, *model, *stepping;
2025 vendor = (const char *) xml_find_attribute (attributes, "vendor")->value;
2026 family = (ULONGEST *) xml_find_attribute (attributes, "family")->value;
2027 model = (ULONGEST *) xml_find_attribute (attributes, "model")->value;
2028 stepping = (ULONGEST *) xml_find_attribute (attributes, "stepping")->value;
2030 btrace = (struct btrace_data *) user_data;
2032 if (strcmp (vendor, "GenuineIntel") == 0)
2033 btrace->variant.pt.config.cpu.vendor = CV_INTEL;
2035 btrace->variant.pt.config.cpu.family = *family;
2036 btrace->variant.pt.config.cpu.model = *model;
2037 btrace->variant.pt.config.cpu.stepping = *stepping;
2040 /* Parse a btrace pt "raw" xml record. */
2043 parse_xml_btrace_pt_raw (struct gdb_xml_parser *parser,
2044 const struct gdb_xml_element *element,
2045 void *user_data, const char *body_text)
2047 struct btrace_data *btrace;
2049 btrace = (struct btrace_data *) user_data;
2050 parse_xml_raw (parser, body_text, &btrace->variant.pt.data,
2051 &btrace->variant.pt.size);
2054 /* Parse a btrace "pt" xml record. */
2057 parse_xml_btrace_pt (struct gdb_xml_parser *parser,
2058 const struct gdb_xml_element *element,
2059 void *user_data, VEC (gdb_xml_value_s) *attributes)
2061 struct btrace_data *btrace;
2063 btrace = (struct btrace_data *) user_data;
2064 btrace->format = BTRACE_FORMAT_PT;
2065 btrace->variant.pt.config.cpu.vendor = CV_UNKNOWN;
2066 btrace->variant.pt.data = NULL;
2067 btrace->variant.pt.size = 0;
2070 static const struct gdb_xml_attribute block_attributes[] = {
2071 { "begin", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
2072 { "end", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
2073 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2076 static const struct gdb_xml_attribute btrace_pt_config_cpu_attributes[] = {
2077 { "vendor", GDB_XML_AF_NONE, NULL, NULL },
2078 { "family", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
2079 { "model", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
2080 { "stepping", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
2081 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2084 static const struct gdb_xml_element btrace_pt_config_children[] = {
2085 { "cpu", btrace_pt_config_cpu_attributes, NULL, GDB_XML_EF_OPTIONAL,
2086 parse_xml_btrace_pt_config_cpu, NULL },
2087 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2090 static const struct gdb_xml_element btrace_pt_children[] = {
2091 { "pt-config", NULL, btrace_pt_config_children, GDB_XML_EF_OPTIONAL, NULL,
2093 { "raw", NULL, NULL, GDB_XML_EF_OPTIONAL, NULL, parse_xml_btrace_pt_raw },
2094 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2097 static const struct gdb_xml_attribute btrace_attributes[] = {
2098 { "version", GDB_XML_AF_NONE, NULL, NULL },
2099 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2102 static const struct gdb_xml_element btrace_children[] = {
2103 { "block", block_attributes, NULL,
2104 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL, parse_xml_btrace_block, NULL },
2105 { "pt", NULL, btrace_pt_children, GDB_XML_EF_OPTIONAL, parse_xml_btrace_pt,
2107 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2110 static const struct gdb_xml_element btrace_elements[] = {
2111 { "btrace", btrace_attributes, btrace_children, GDB_XML_EF_NONE,
2112 check_xml_btrace_version, NULL },
2113 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2116 #endif /* defined (HAVE_LIBEXPAT) */
2121 parse_xml_btrace (struct btrace_data *btrace, const char *buffer)
2123 struct cleanup *cleanup;
2126 #if defined (HAVE_LIBEXPAT)
2128 btrace->format = BTRACE_FORMAT_NONE;
2130 cleanup = make_cleanup_btrace_data (btrace);
2131 errcode = gdb_xml_parse_quick (_("btrace"), "btrace.dtd", btrace_elements,
2134 error (_("Error parsing branch trace."));
2136 /* Keep parse results. */
2137 discard_cleanups (cleanup);
2139 #else /* !defined (HAVE_LIBEXPAT) */
2141 error (_("Cannot process branch trace. XML parsing is not supported."));
2143 #endif /* !defined (HAVE_LIBEXPAT) */
2146 #if defined (HAVE_LIBEXPAT)
2148 /* Parse a btrace-conf "bts" xml record. */
2151 parse_xml_btrace_conf_bts (struct gdb_xml_parser *parser,
2152 const struct gdb_xml_element *element,
2153 void *user_data, VEC (gdb_xml_value_s) *attributes)
2155 struct btrace_config *conf;
2156 struct gdb_xml_value *size;
2158 conf = (struct btrace_config *) user_data;
2159 conf->format = BTRACE_FORMAT_BTS;
2162 size = xml_find_attribute (attributes, "size");
2164 conf->bts.size = (unsigned int) *(ULONGEST *) size->value;
2167 /* Parse a btrace-conf "pt" xml record. */
2170 parse_xml_btrace_conf_pt (struct gdb_xml_parser *parser,
2171 const struct gdb_xml_element *element,
2172 void *user_data, VEC (gdb_xml_value_s) *attributes)
2174 struct btrace_config *conf;
2175 struct gdb_xml_value *size;
2177 conf = (struct btrace_config *) user_data;
2178 conf->format = BTRACE_FORMAT_PT;
2181 size = xml_find_attribute (attributes, "size");
2183 conf->pt.size = (unsigned int) *(ULONGEST *) size->value;
2186 static const struct gdb_xml_attribute btrace_conf_pt_attributes[] = {
2187 { "size", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
2188 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2191 static const struct gdb_xml_attribute btrace_conf_bts_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_element btrace_conf_children[] = {
2197 { "bts", btrace_conf_bts_attributes, NULL, GDB_XML_EF_OPTIONAL,
2198 parse_xml_btrace_conf_bts, NULL },
2199 { "pt", btrace_conf_pt_attributes, NULL, GDB_XML_EF_OPTIONAL,
2200 parse_xml_btrace_conf_pt, NULL },
2201 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2204 static const struct gdb_xml_attribute btrace_conf_attributes[] = {
2205 { "version", GDB_XML_AF_NONE, NULL, NULL },
2206 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2209 static const struct gdb_xml_element btrace_conf_elements[] = {
2210 { "btrace-conf", btrace_conf_attributes, btrace_conf_children,
2211 GDB_XML_EF_NONE, NULL, NULL },
2212 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2215 #endif /* defined (HAVE_LIBEXPAT) */
2220 parse_xml_btrace_conf (struct btrace_config *conf, const char *xml)
2224 #if defined (HAVE_LIBEXPAT)
2226 errcode = gdb_xml_parse_quick (_("btrace-conf"), "btrace-conf.dtd",
2227 btrace_conf_elements, xml, conf);
2229 error (_("Error parsing branch trace configuration."));
2231 #else /* !defined (HAVE_LIBEXPAT) */
2233 error (_("XML parsing is not supported."));
2235 #endif /* !defined (HAVE_LIBEXPAT) */
2240 const struct btrace_insn *
2241 btrace_insn_get (const struct btrace_insn_iterator *it)
2243 const struct btrace_function *bfun;
2244 unsigned int index, end;
2246 index = it->insn_index;
2247 bfun = it->btinfo->functions[it->call_index];
2249 /* Check if the iterator points to a gap in the trace. */
2250 if (bfun->errcode != 0)
2253 /* The index is within the bounds of this function's instruction vector. */
2254 end = VEC_length (btrace_insn_s, bfun->insn);
2255 gdb_assert (0 < end);
2256 gdb_assert (index < end);
2258 return VEC_index (btrace_insn_s, bfun->insn, index);
2264 btrace_insn_get_error (const struct btrace_insn_iterator *it)
2266 const struct btrace_function *bfun;
2268 bfun = it->btinfo->functions[it->call_index];
2269 return bfun->errcode;
2275 btrace_insn_number (const struct btrace_insn_iterator *it)
2277 const struct btrace_function *bfun;
2279 bfun = it->btinfo->functions[it->call_index];
2280 return bfun->insn_offset + it->insn_index;
2286 btrace_insn_begin (struct btrace_insn_iterator *it,
2287 const struct btrace_thread_info *btinfo)
2289 const struct btrace_function *bfun;
2291 bfun = btinfo->begin;
2293 error (_("No trace."));
2295 it->btinfo = btinfo;
2303 btrace_insn_end (struct btrace_insn_iterator *it,
2304 const struct btrace_thread_info *btinfo)
2306 const struct btrace_function *bfun;
2307 unsigned int length;
2311 error (_("No trace."));
2313 length = VEC_length (btrace_insn_s, bfun->insn);
2315 /* The last function may either be a gap or it contains the current
2316 instruction, which is one past the end of the execution trace; ignore
2321 it->btinfo = btinfo;
2322 it->call_index = bfun->number - 1;
2323 it->insn_index = length;
2329 btrace_insn_next (struct btrace_insn_iterator *it, unsigned int stride)
2331 const struct btrace_function *bfun;
2332 unsigned int index, steps;
2334 bfun = it->btinfo->functions[it->call_index];
2336 index = it->insn_index;
2340 unsigned int end, space, adv;
2342 end = VEC_length (btrace_insn_s, bfun->insn);
2344 /* An empty function segment represents a gap in the trace. We count
2345 it as one instruction. */
2348 const struct btrace_function *next;
2350 next = bfun->flow.next;
2363 gdb_assert (0 < end);
2364 gdb_assert (index < end);
2366 /* Compute the number of instructions remaining in this segment. */
2367 space = end - index;
2369 /* Advance the iterator as far as possible within this segment. */
2370 adv = std::min (space, stride);
2375 /* Move to the next function if we're at the end of this one. */
2378 const struct btrace_function *next;
2380 next = bfun->flow.next;
2383 /* We stepped past the last function.
2385 Let's adjust the index to point to the last instruction in
2386 the previous function. */
2392 /* We now point to the first instruction in the new function. */
2397 /* We did make progress. */
2398 gdb_assert (adv > 0);
2401 /* Update the iterator. */
2402 it->call_index = bfun->number - 1;
2403 it->insn_index = index;
2411 btrace_insn_prev (struct btrace_insn_iterator *it, unsigned int stride)
2413 const struct btrace_function *bfun;
2414 unsigned int index, steps;
2416 bfun = it->btinfo->functions[it->call_index];
2418 index = it->insn_index;
2424 /* Move to the previous function if we're at the start of this one. */
2427 const struct btrace_function *prev;
2429 prev = bfun->flow.prev;
2433 /* We point to one after the last instruction in the new function. */
2435 index = VEC_length (btrace_insn_s, bfun->insn);
2437 /* An empty function segment represents a gap in the trace. We count
2438 it as one instruction. */
2448 /* Advance the iterator as far as possible within this segment. */
2449 adv = std::min (index, stride);
2455 /* We did make progress. */
2456 gdb_assert (adv > 0);
2459 /* Update the iterator. */
2460 it->call_index = bfun->number - 1;
2461 it->insn_index = index;
2469 btrace_insn_cmp (const struct btrace_insn_iterator *lhs,
2470 const struct btrace_insn_iterator *rhs)
2472 gdb_assert (lhs->btinfo == rhs->btinfo);
2474 if (lhs->call_index != rhs->call_index)
2475 return lhs->call_index - rhs->call_index;
2477 return lhs->insn_index - rhs->insn_index;
2483 btrace_find_insn_by_number (struct btrace_insn_iterator *it,
2484 const struct btrace_thread_info *btinfo,
2485 unsigned int number)
2487 const struct btrace_function *bfun;
2488 unsigned int upper, lower;
2490 if (btinfo->functions.empty ())
2494 bfun = btinfo->functions[lower];
2495 if (number < bfun->insn_offset)
2498 upper = btinfo->functions.size () - 1;
2499 bfun = btinfo->functions[upper];
2500 if (number >= bfun->insn_offset + ftrace_call_num_insn (bfun))
2503 /* We assume that there are no holes in the numbering. */
2506 const unsigned int average = lower + (upper - lower) / 2;
2508 bfun = btinfo->functions[average];
2510 if (number < bfun->insn_offset)
2512 upper = average - 1;
2516 if (number >= bfun->insn_offset + ftrace_call_num_insn (bfun))
2518 lower = average + 1;
2525 it->btinfo = btinfo;
2526 it->call_index = bfun->number - 1;
2527 it->insn_index = number - bfun->insn_offset;
2531 /* Returns true if the recording ends with a function segment that
2532 contains only a single (i.e. the current) instruction. */
2535 btrace_ends_with_single_insn (const struct btrace_thread_info *btinfo)
2537 const btrace_function *bfun;
2539 if (btinfo->functions.empty ())
2542 bfun = btinfo->functions.back ();
2543 if (bfun->errcode != 0)
2546 return ftrace_call_num_insn (bfun) == 1;
2551 const struct btrace_function *
2552 btrace_call_get (const struct btrace_call_iterator *it)
2554 if (it->index >= it->btinfo->functions.size ())
2557 return it->btinfo->functions[it->index];
2563 btrace_call_number (const struct btrace_call_iterator *it)
2565 const unsigned int length = it->btinfo->functions.size ();
2567 /* If the last function segment contains only a single instruction (i.e. the
2568 current instruction), skip it. */
2569 if ((it->index == length) && btrace_ends_with_single_insn (it->btinfo))
2572 return it->index + 1;
2578 btrace_call_begin (struct btrace_call_iterator *it,
2579 const struct btrace_thread_info *btinfo)
2581 if (btinfo->functions.empty ())
2582 error (_("No trace."));
2584 it->btinfo = btinfo;
2591 btrace_call_end (struct btrace_call_iterator *it,
2592 const struct btrace_thread_info *btinfo)
2594 if (btinfo->functions.empty ())
2595 error (_("No trace."));
2597 it->btinfo = btinfo;
2598 it->index = btinfo->functions.size ();
2604 btrace_call_next (struct btrace_call_iterator *it, unsigned int stride)
2606 const unsigned int length = it->btinfo->functions.size ();
2608 if (it->index + stride < length - 1)
2609 /* Default case: Simply advance the iterator. */
2610 it->index += stride;
2611 else if (it->index + stride == length - 1)
2613 /* We land exactly at the last function segment. If it contains only one
2614 instruction (i.e. the current instruction) it is not actually part of
2616 if (btrace_ends_with_single_insn (it->btinfo))
2619 it->index = length - 1;
2623 /* We land past the last function segment and have to adjust the stride.
2624 If the last function segment contains only one instruction (i.e. the
2625 current instruction) it is not actually part of the trace. */
2626 if (btrace_ends_with_single_insn (it->btinfo))
2627 stride = length - it->index - 1;
2629 stride = length - it->index;
2640 btrace_call_prev (struct btrace_call_iterator *it, unsigned int stride)
2642 const unsigned int length = it->btinfo->functions.size ();
2645 gdb_assert (it->index <= length);
2647 if (stride == 0 || it->index == 0)
2650 /* If we are at the end, the first step is a special case. If the last
2651 function segment contains only one instruction (i.e. the current
2652 instruction) it is not actually part of the trace. To be able to step
2653 over this instruction, we need at least one more function segment. */
2654 if ((it->index == length) && (length > 1))
2656 if (btrace_ends_with_single_insn (it->btinfo))
2657 it->index = length - 2;
2659 it->index = length - 1;
2665 stride = std::min (stride, it->index);
2667 it->index -= stride;
2668 return steps + stride;
2674 btrace_call_cmp (const struct btrace_call_iterator *lhs,
2675 const struct btrace_call_iterator *rhs)
2677 gdb_assert (lhs->btinfo == rhs->btinfo);
2678 return (int) (lhs->index - rhs->index);
2684 btrace_find_call_by_number (struct btrace_call_iterator *it,
2685 const struct btrace_thread_info *btinfo,
2686 unsigned int number)
2688 const unsigned int length = btinfo->functions.size ();
2690 if ((number == 0) || (number > length))
2693 it->btinfo = btinfo;
2694 it->index = number - 1;
2701 btrace_set_insn_history (struct btrace_thread_info *btinfo,
2702 const struct btrace_insn_iterator *begin,
2703 const struct btrace_insn_iterator *end)
2705 if (btinfo->insn_history == NULL)
2706 btinfo->insn_history = XCNEW (struct btrace_insn_history);
2708 btinfo->insn_history->begin = *begin;
2709 btinfo->insn_history->end = *end;
2715 btrace_set_call_history (struct btrace_thread_info *btinfo,
2716 const struct btrace_call_iterator *begin,
2717 const struct btrace_call_iterator *end)
2719 gdb_assert (begin->btinfo == end->btinfo);
2721 if (btinfo->call_history == NULL)
2722 btinfo->call_history = XCNEW (struct btrace_call_history);
2724 btinfo->call_history->begin = *begin;
2725 btinfo->call_history->end = *end;
2731 btrace_is_replaying (struct thread_info *tp)
2733 return tp->btrace.replay != NULL;
2739 btrace_is_empty (struct thread_info *tp)
2741 struct btrace_insn_iterator begin, end;
2742 struct btrace_thread_info *btinfo;
2744 btinfo = &tp->btrace;
2746 if (btinfo->begin == NULL)
2749 btrace_insn_begin (&begin, btinfo);
2750 btrace_insn_end (&end, btinfo);
2752 return btrace_insn_cmp (&begin, &end) == 0;
2755 /* Forward the cleanup request. */
2758 do_btrace_data_cleanup (void *arg)
2760 btrace_data_fini ((struct btrace_data *) arg);
2766 make_cleanup_btrace_data (struct btrace_data *data)
2768 return make_cleanup (do_btrace_data_cleanup, data);
2771 #if defined (HAVE_LIBIPT)
2773 /* Print a single packet. */
2776 pt_print_packet (const struct pt_packet *packet)
2778 switch (packet->type)
2781 printf_unfiltered (("[??: %x]"), packet->type);
2785 printf_unfiltered (("psb"));
2789 printf_unfiltered (("psbend"));
2793 printf_unfiltered (("pad"));
2797 printf_unfiltered (("tip %u: 0x%" PRIx64 ""),
2798 packet->payload.ip.ipc,
2799 packet->payload.ip.ip);
2803 printf_unfiltered (("tip.pge %u: 0x%" PRIx64 ""),
2804 packet->payload.ip.ipc,
2805 packet->payload.ip.ip);
2809 printf_unfiltered (("tip.pgd %u: 0x%" PRIx64 ""),
2810 packet->payload.ip.ipc,
2811 packet->payload.ip.ip);
2815 printf_unfiltered (("fup %u: 0x%" PRIx64 ""),
2816 packet->payload.ip.ipc,
2817 packet->payload.ip.ip);
2821 printf_unfiltered (("tnt-8 %u: 0x%" PRIx64 ""),
2822 packet->payload.tnt.bit_size,
2823 packet->payload.tnt.payload);
2827 printf_unfiltered (("tnt-64 %u: 0x%" PRIx64 ""),
2828 packet->payload.tnt.bit_size,
2829 packet->payload.tnt.payload);
2833 printf_unfiltered (("pip %" PRIx64 "%s"), packet->payload.pip.cr3,
2834 packet->payload.pip.nr ? (" nr") : (""));
2838 printf_unfiltered (("tsc %" PRIx64 ""), packet->payload.tsc.tsc);
2842 printf_unfiltered (("cbr %u"), packet->payload.cbr.ratio);
2846 switch (packet->payload.mode.leaf)
2849 printf_unfiltered (("mode %u"), packet->payload.mode.leaf);
2853 printf_unfiltered (("mode.exec%s%s"),
2854 packet->payload.mode.bits.exec.csl
2856 packet->payload.mode.bits.exec.csd
2857 ? (" cs.d") : (""));
2861 printf_unfiltered (("mode.tsx%s%s"),
2862 packet->payload.mode.bits.tsx.intx
2864 packet->payload.mode.bits.tsx.abrt
2865 ? (" abrt") : (""));
2871 printf_unfiltered (("ovf"));
2875 printf_unfiltered (("stop"));
2879 printf_unfiltered (("vmcs %" PRIx64 ""), packet->payload.vmcs.base);
2883 printf_unfiltered (("tma %x %x"), packet->payload.tma.ctc,
2884 packet->payload.tma.fc);
2888 printf_unfiltered (("mtc %x"), packet->payload.mtc.ctc);
2892 printf_unfiltered (("cyc %" PRIx64 ""), packet->payload.cyc.value);
2896 printf_unfiltered (("mnt %" PRIx64 ""), packet->payload.mnt.payload);
2901 /* Decode packets into MAINT using DECODER. */
2904 btrace_maint_decode_pt (struct btrace_maint_info *maint,
2905 struct pt_packet_decoder *decoder)
2911 struct btrace_pt_packet packet;
2913 errcode = pt_pkt_sync_forward (decoder);
2919 pt_pkt_get_offset (decoder, &packet.offset);
2921 errcode = pt_pkt_next (decoder, &packet.packet,
2922 sizeof(packet.packet));
2926 if (maint_btrace_pt_skip_pad == 0 || packet.packet.type != ppt_pad)
2928 packet.errcode = pt_errcode (errcode);
2929 VEC_safe_push (btrace_pt_packet_s, maint->variant.pt.packets,
2934 if (errcode == -pte_eos)
2937 packet.errcode = pt_errcode (errcode);
2938 VEC_safe_push (btrace_pt_packet_s, maint->variant.pt.packets,
2941 warning (_("Error at trace offset 0x%" PRIx64 ": %s."),
2942 packet.offset, pt_errstr (packet.errcode));
2945 if (errcode != -pte_eos)
2946 warning (_("Failed to synchronize onto the Intel Processor Trace "
2947 "stream: %s."), pt_errstr (pt_errcode (errcode)));
2950 /* Update the packet history in BTINFO. */
2953 btrace_maint_update_pt_packets (struct btrace_thread_info *btinfo)
2955 volatile struct gdb_exception except;
2956 struct pt_packet_decoder *decoder;
2957 struct btrace_data_pt *pt;
2958 struct pt_config config;
2961 pt = &btinfo->data.variant.pt;
2963 /* Nothing to do if there is no trace. */
2967 memset (&config, 0, sizeof(config));
2969 config.size = sizeof (config);
2970 config.begin = pt->data;
2971 config.end = pt->data + pt->size;
2973 config.cpu.vendor = pt_translate_cpu_vendor (pt->config.cpu.vendor);
2974 config.cpu.family = pt->config.cpu.family;
2975 config.cpu.model = pt->config.cpu.model;
2976 config.cpu.stepping = pt->config.cpu.stepping;
2978 errcode = pt_cpu_errata (&config.errata, &config.cpu);
2980 error (_("Failed to configure the Intel Processor Trace decoder: %s."),
2981 pt_errstr (pt_errcode (errcode)));
2983 decoder = pt_pkt_alloc_decoder (&config);
2984 if (decoder == NULL)
2985 error (_("Failed to allocate the Intel Processor Trace decoder."));
2989 btrace_maint_decode_pt (&btinfo->maint, decoder);
2991 CATCH (except, RETURN_MASK_ALL)
2993 pt_pkt_free_decoder (decoder);
2995 if (except.reason < 0)
2996 throw_exception (except);
3000 pt_pkt_free_decoder (decoder);
3003 #endif /* !defined (HAVE_LIBIPT) */
3005 /* Update the packet maintenance information for BTINFO and store the
3006 low and high bounds into BEGIN and END, respectively.
3007 Store the current iterator state into FROM and TO. */
3010 btrace_maint_update_packets (struct btrace_thread_info *btinfo,
3011 unsigned int *begin, unsigned int *end,
3012 unsigned int *from, unsigned int *to)
3014 switch (btinfo->data.format)
3023 case BTRACE_FORMAT_BTS:
3024 /* Nothing to do - we operate directly on BTINFO->DATA. */
3026 *end = VEC_length (btrace_block_s, btinfo->data.variant.bts.blocks);
3027 *from = btinfo->maint.variant.bts.packet_history.begin;
3028 *to = btinfo->maint.variant.bts.packet_history.end;
3031 #if defined (HAVE_LIBIPT)
3032 case BTRACE_FORMAT_PT:
3033 if (VEC_empty (btrace_pt_packet_s, btinfo->maint.variant.pt.packets))
3034 btrace_maint_update_pt_packets (btinfo);
3037 *end = VEC_length (btrace_pt_packet_s, btinfo->maint.variant.pt.packets);
3038 *from = btinfo->maint.variant.pt.packet_history.begin;
3039 *to = btinfo->maint.variant.pt.packet_history.end;
3041 #endif /* defined (HAVE_LIBIPT) */
3045 /* Print packets in BTINFO from BEGIN (inclusive) until END (exclusive) and
3046 update the current iterator position. */
3049 btrace_maint_print_packets (struct btrace_thread_info *btinfo,
3050 unsigned int begin, unsigned int end)
3052 switch (btinfo->data.format)
3057 case BTRACE_FORMAT_BTS:
3059 VEC (btrace_block_s) *blocks;
3062 blocks = btinfo->data.variant.bts.blocks;
3063 for (blk = begin; blk < end; ++blk)
3065 const btrace_block_s *block;
3067 block = VEC_index (btrace_block_s, blocks, blk);
3069 printf_unfiltered ("%u\tbegin: %s, end: %s\n", blk,
3070 core_addr_to_string_nz (block->begin),
3071 core_addr_to_string_nz (block->end));
3074 btinfo->maint.variant.bts.packet_history.begin = begin;
3075 btinfo->maint.variant.bts.packet_history.end = end;
3079 #if defined (HAVE_LIBIPT)
3080 case BTRACE_FORMAT_PT:
3082 VEC (btrace_pt_packet_s) *packets;
3085 packets = btinfo->maint.variant.pt.packets;
3086 for (pkt = begin; pkt < end; ++pkt)
3088 const struct btrace_pt_packet *packet;
3090 packet = VEC_index (btrace_pt_packet_s, packets, pkt);
3092 printf_unfiltered ("%u\t", pkt);
3093 printf_unfiltered ("0x%" PRIx64 "\t", packet->offset);
3095 if (packet->errcode == pte_ok)
3096 pt_print_packet (&packet->packet);
3098 printf_unfiltered ("[error: %s]", pt_errstr (packet->errcode));
3100 printf_unfiltered ("\n");
3103 btinfo->maint.variant.pt.packet_history.begin = begin;
3104 btinfo->maint.variant.pt.packet_history.end = end;
3107 #endif /* defined (HAVE_LIBIPT) */
3111 /* Read a number from an argument string. */
3114 get_uint (char **arg)
3116 char *begin, *end, *pos;
3117 unsigned long number;
3120 pos = skip_spaces (begin);
3122 if (!isdigit (*pos))
3123 error (_("Expected positive number, got: %s."), pos);
3125 number = strtoul (pos, &end, 10);
3126 if (number > UINT_MAX)
3127 error (_("Number too big."));
3129 *arg += (end - begin);
3131 return (unsigned int) number;
3134 /* Read a context size from an argument string. */
3137 get_context_size (char **arg)
3142 pos = skip_spaces (*arg);
3144 if (!isdigit (*pos))
3145 error (_("Expected positive number, got: %s."), pos);
3147 return strtol (pos, arg, 10);
3150 /* Complain about junk at the end of an argument string. */
3153 no_chunk (char *arg)
3156 error (_("Junk after argument: %s."), arg);
3159 /* The "maintenance btrace packet-history" command. */
3162 maint_btrace_packet_history_cmd (char *arg, int from_tty)
3164 struct btrace_thread_info *btinfo;
3165 struct thread_info *tp;
3166 unsigned int size, begin, end, from, to;
3168 tp = find_thread_ptid (inferior_ptid);
3170 error (_("No thread."));
3173 btinfo = &tp->btrace;
3175 btrace_maint_update_packets (btinfo, &begin, &end, &from, &to);
3178 printf_unfiltered (_("No trace.\n"));
3182 if (arg == NULL || *arg == 0 || strcmp (arg, "+") == 0)
3186 if (end - from < size)
3190 else if (strcmp (arg, "-") == 0)
3194 if (to - begin < size)
3200 from = get_uint (&arg);
3202 error (_("'%u' is out of range."), from);
3204 arg = skip_spaces (arg);
3207 arg = skip_spaces (++arg);
3212 size = get_context_size (&arg);
3216 if (end - from < size)
3220 else if (*arg == '-')
3223 size = get_context_size (&arg);
3227 /* Include the packet given as first argument. */
3231 if (to - begin < size)
3237 to = get_uint (&arg);
3239 /* Include the packet at the second argument and silently
3240 truncate the range. */
3253 if (end - from < size)
3261 btrace_maint_print_packets (btinfo, from, to);
3264 /* The "maintenance btrace clear-packet-history" command. */
3267 maint_btrace_clear_packet_history_cmd (char *args, int from_tty)
3269 struct btrace_thread_info *btinfo;
3270 struct thread_info *tp;
3272 if (args != NULL && *args != 0)
3273 error (_("Invalid argument."));
3275 tp = find_thread_ptid (inferior_ptid);
3277 error (_("No thread."));
3279 btinfo = &tp->btrace;
3281 /* Must clear the maint data before - it depends on BTINFO->DATA. */
3282 btrace_maint_clear (btinfo);
3283 btrace_data_clear (&btinfo->data);
3286 /* The "maintenance btrace clear" command. */
3289 maint_btrace_clear_cmd (char *args, int from_tty)
3291 struct btrace_thread_info *btinfo;
3292 struct thread_info *tp;
3294 if (args != NULL && *args != 0)
3295 error (_("Invalid argument."));
3297 tp = find_thread_ptid (inferior_ptid);
3299 error (_("No thread."));
3304 /* The "maintenance btrace" command. */
3307 maint_btrace_cmd (char *args, int from_tty)
3309 help_list (maint_btrace_cmdlist, "maintenance btrace ", all_commands,
3313 /* The "maintenance set btrace" command. */
3316 maint_btrace_set_cmd (char *args, int from_tty)
3318 help_list (maint_btrace_set_cmdlist, "maintenance set btrace ", all_commands,
3322 /* The "maintenance show btrace" command. */
3325 maint_btrace_show_cmd (char *args, int from_tty)
3327 help_list (maint_btrace_show_cmdlist, "maintenance show btrace ",
3328 all_commands, gdb_stdout);
3331 /* The "maintenance set btrace pt" command. */
3334 maint_btrace_pt_set_cmd (char *args, int from_tty)
3336 help_list (maint_btrace_pt_set_cmdlist, "maintenance set btrace pt ",
3337 all_commands, gdb_stdout);
3340 /* The "maintenance show btrace pt" command. */
3343 maint_btrace_pt_show_cmd (char *args, int from_tty)
3345 help_list (maint_btrace_pt_show_cmdlist, "maintenance show btrace pt ",
3346 all_commands, gdb_stdout);
3349 /* The "maintenance info btrace" command. */
3352 maint_info_btrace_cmd (char *args, int from_tty)
3354 struct btrace_thread_info *btinfo;
3355 struct thread_info *tp;
3356 const struct btrace_config *conf;
3358 if (args != NULL && *args != 0)
3359 error (_("Invalid argument."));
3361 tp = find_thread_ptid (inferior_ptid);
3363 error (_("No thread."));
3365 btinfo = &tp->btrace;
3367 conf = btrace_conf (btinfo);
3369 error (_("No btrace configuration."));
3371 printf_unfiltered (_("Format: %s.\n"),
3372 btrace_format_string (conf->format));
3374 switch (conf->format)
3379 case BTRACE_FORMAT_BTS:
3380 printf_unfiltered (_("Number of packets: %u.\n"),
3381 VEC_length (btrace_block_s,
3382 btinfo->data.variant.bts.blocks));
3385 #if defined (HAVE_LIBIPT)
3386 case BTRACE_FORMAT_PT:
3388 struct pt_version version;
3390 version = pt_library_version ();
3391 printf_unfiltered (_("Version: %u.%u.%u%s.\n"), version.major,
3392 version.minor, version.build,
3393 version.ext != NULL ? version.ext : "");
3395 btrace_maint_update_pt_packets (btinfo);
3396 printf_unfiltered (_("Number of packets: %u.\n"),
3397 VEC_length (btrace_pt_packet_s,
3398 btinfo->maint.variant.pt.packets));
3401 #endif /* defined (HAVE_LIBIPT) */
3405 /* The "maint show btrace pt skip-pad" show value function. */
3408 show_maint_btrace_pt_skip_pad (struct ui_file *file, int from_tty,
3409 struct cmd_list_element *c,
3412 fprintf_filtered (file, _("Skip PAD packets is %s.\n"), value);
3416 /* Initialize btrace maintenance commands. */
3418 void _initialize_btrace (void);
3420 _initialize_btrace (void)
3422 add_cmd ("btrace", class_maintenance, maint_info_btrace_cmd,
3423 _("Info about branch tracing data."), &maintenanceinfolist);
3425 add_prefix_cmd ("btrace", class_maintenance, maint_btrace_cmd,
3426 _("Branch tracing maintenance commands."),
3427 &maint_btrace_cmdlist, "maintenance btrace ",
3428 0, &maintenancelist);
3430 add_prefix_cmd ("btrace", class_maintenance, maint_btrace_set_cmd, _("\
3431 Set branch tracing specific variables."),
3432 &maint_btrace_set_cmdlist, "maintenance set btrace ",
3433 0, &maintenance_set_cmdlist);
3435 add_prefix_cmd ("pt", class_maintenance, maint_btrace_pt_set_cmd, _("\
3436 Set Intel Processor Trace specific variables."),
3437 &maint_btrace_pt_set_cmdlist, "maintenance set btrace pt ",
3438 0, &maint_btrace_set_cmdlist);
3440 add_prefix_cmd ("btrace", class_maintenance, maint_btrace_show_cmd, _("\
3441 Show branch tracing specific variables."),
3442 &maint_btrace_show_cmdlist, "maintenance show btrace ",
3443 0, &maintenance_show_cmdlist);
3445 add_prefix_cmd ("pt", class_maintenance, maint_btrace_pt_show_cmd, _("\
3446 Show Intel Processor Trace specific variables."),
3447 &maint_btrace_pt_show_cmdlist, "maintenance show btrace pt ",
3448 0, &maint_btrace_show_cmdlist);
3450 add_setshow_boolean_cmd ("skip-pad", class_maintenance,
3451 &maint_btrace_pt_skip_pad, _("\
3452 Set whether PAD packets should be skipped in the btrace packet history."), _("\
3453 Show whether PAD packets should be skipped in the btrace packet history."),_("\
3454 When enabled, PAD packets are ignored in the btrace packet history."),
3455 NULL, show_maint_btrace_pt_skip_pad,
3456 &maint_btrace_pt_set_cmdlist,
3457 &maint_btrace_pt_show_cmdlist);
3459 add_cmd ("packet-history", class_maintenance, maint_btrace_packet_history_cmd,
3460 _("Print the raw branch tracing data.\n\
3461 With no argument, print ten more packets after the previous ten-line print.\n\
3462 With '-' as argument print ten packets before a previous ten-line print.\n\
3463 One argument specifies the starting packet of a ten-line print.\n\
3464 Two arguments with comma between specify starting and ending packets to \
3466 Preceded with '+'/'-' the second argument specifies the distance from the \
3468 &maint_btrace_cmdlist);
3470 add_cmd ("clear-packet-history", class_maintenance,
3471 maint_btrace_clear_packet_history_cmd,
3472 _("Clears the branch tracing packet history.\n\
3473 Discards the raw branch tracing data but not the execution history data.\n\
3475 &maint_btrace_cmdlist);
3477 add_cmd ("clear", class_maintenance, maint_btrace_clear_cmd,
3478 _("Clears the branch tracing data.\n\
3479 Discards the raw branch tracing data and the execution history data.\n\
3480 The next 'record' command will fetch the branch tracing data anew.\n\
3482 &maint_btrace_cmdlist);