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 static void btrace_add_pc (struct thread_info *tp);
54 /* Print a record debug message. Use do ... while (0) to avoid ambiguities
55 when used in if statements. */
57 #define DEBUG(msg, args...) \
60 if (record_debug != 0) \
61 fprintf_unfiltered (gdb_stdlog, \
62 "[btrace] " msg "\n", ##args); \
66 #define DEBUG_FTRACE(msg, args...) DEBUG ("[ftrace] " msg, ##args)
68 /* Return the function name of a recorded function segment for printing.
69 This function never returns NULL. */
72 ftrace_print_function_name (const struct btrace_function *bfun)
74 struct minimal_symbol *msym;
81 return SYMBOL_PRINT_NAME (sym);
84 return MSYMBOL_PRINT_NAME (msym);
89 /* Return the file name of a recorded function segment for printing.
90 This function never returns NULL. */
93 ftrace_print_filename (const struct btrace_function *bfun)
101 filename = symtab_to_filename_for_display (symbol_symtab (sym));
103 filename = "<unknown>";
108 /* Return a string representation of the address of an instruction.
109 This function never returns NULL. */
112 ftrace_print_insn_addr (const struct btrace_insn *insn)
117 return core_addr_to_string_nz (insn->pc);
120 /* Print an ftrace debug status message. */
123 ftrace_debug (const struct btrace_function *bfun, const char *prefix)
125 const char *fun, *file;
126 unsigned int ibegin, iend;
129 fun = ftrace_print_function_name (bfun);
130 file = ftrace_print_filename (bfun);
133 ibegin = bfun->insn_offset;
134 iend = ibegin + VEC_length (btrace_insn_s, bfun->insn);
136 DEBUG_FTRACE ("%s: fun = %s, file = %s, level = %d, insn = [%u; %u)",
137 prefix, fun, file, level, ibegin, iend);
140 /* Return the number of instructions in a given function call segment. */
143 ftrace_call_num_insn (const struct btrace_function* bfun)
148 /* A gap is always counted as one instruction. */
149 if (bfun->errcode != 0)
152 return VEC_length (btrace_insn_s, bfun->insn);
155 /* Return the function segment with the given NUMBER or NULL if no such segment
156 exists. BTINFO is the branch trace information for the current thread. */
158 static struct btrace_function *
159 ftrace_find_call_by_number (struct btrace_thread_info *btinfo,
162 if (number == 0 || number > btinfo->functions.size ())
165 return &btinfo->functions[number - 1];
168 /* A const version of the function above. */
170 static const struct btrace_function *
171 ftrace_find_call_by_number (const struct btrace_thread_info *btinfo,
174 if (number == 0 || number > btinfo->functions.size ())
177 return &btinfo->functions[number - 1];
180 /* Return non-zero if BFUN does not match MFUN and FUN,
181 return zero otherwise. */
184 ftrace_function_switched (const struct btrace_function *bfun,
185 const struct minimal_symbol *mfun,
186 const struct symbol *fun)
188 struct minimal_symbol *msym;
194 /* If the minimal symbol changed, we certainly switched functions. */
195 if (mfun != NULL && msym != NULL
196 && strcmp (MSYMBOL_LINKAGE_NAME (mfun), MSYMBOL_LINKAGE_NAME (msym)) != 0)
199 /* If the symbol changed, we certainly switched functions. */
200 if (fun != NULL && sym != NULL)
202 const char *bfname, *fname;
204 /* Check the function name. */
205 if (strcmp (SYMBOL_LINKAGE_NAME (fun), SYMBOL_LINKAGE_NAME (sym)) != 0)
208 /* Check the location of those functions, as well. */
209 bfname = symtab_to_fullname (symbol_symtab (sym));
210 fname = symtab_to_fullname (symbol_symtab (fun));
211 if (filename_cmp (fname, bfname) != 0)
215 /* If we lost symbol information, we switched functions. */
216 if (!(msym == NULL && sym == NULL) && mfun == NULL && fun == NULL)
219 /* If we gained symbol information, we switched functions. */
220 if (msym == NULL && sym == NULL && !(mfun == NULL && fun == NULL))
226 /* Allocate and initialize a new branch trace function segment at the end of
228 BTINFO is the branch trace information for the current thread.
229 MFUN and FUN are the symbol information we have for this function.
230 This invalidates all struct btrace_function pointer currently held. */
232 static struct btrace_function *
233 ftrace_new_function (struct btrace_thread_info *btinfo,
234 struct minimal_symbol *mfun,
238 unsigned int number, insn_offset;
240 if (btinfo->functions.empty ())
242 /* Start counting NUMBER and INSN_OFFSET at one. */
249 const struct btrace_function *prev = &btinfo->functions.back ();
251 number = prev->number + 1;
252 insn_offset = prev->insn_offset + ftrace_call_num_insn (prev);
255 btinfo->functions.emplace_back (mfun, fun, number, insn_offset, level);
256 return &btinfo->functions.back ();
259 /* Update the UP field of a function segment. */
262 ftrace_update_caller (struct btrace_function *bfun,
263 struct btrace_function *caller,
264 enum btrace_function_flag flags)
267 ftrace_debug (bfun, "updating caller");
269 bfun->up = caller->number;
272 ftrace_debug (bfun, "set caller");
273 ftrace_debug (caller, "..to");
276 /* Fix up the caller for all segments of a function. */
279 ftrace_fixup_caller (struct btrace_thread_info *btinfo,
280 struct btrace_function *bfun,
281 struct btrace_function *caller,
282 enum btrace_function_flag flags)
284 unsigned int prev, next;
288 ftrace_update_caller (bfun, caller, flags);
290 /* Update all function segments belonging to the same function. */
291 for (; prev != 0; prev = bfun->prev)
293 bfun = ftrace_find_call_by_number (btinfo, prev);
294 ftrace_update_caller (bfun, caller, flags);
297 for (; next != 0; next = bfun->next)
299 bfun = ftrace_find_call_by_number (btinfo, next);
300 ftrace_update_caller (bfun, caller, flags);
304 /* Add a new function segment for a call at the end of the trace.
305 BTINFO is the branch trace information for the current thread.
306 MFUN and FUN are the symbol information we have for this function. */
308 static struct btrace_function *
309 ftrace_new_call (struct btrace_thread_info *btinfo,
310 struct minimal_symbol *mfun,
313 const unsigned int length = btinfo->functions.size ();
314 struct btrace_function *bfun = ftrace_new_function (btinfo, mfun, fun);
319 ftrace_debug (bfun, "new call");
324 /* Add a new function segment for a tail call at the end of the trace.
325 BTINFO is the branch trace information for the current thread.
326 MFUN and FUN are the symbol information we have for this function. */
328 static struct btrace_function *
329 ftrace_new_tailcall (struct btrace_thread_info *btinfo,
330 struct minimal_symbol *mfun,
333 const unsigned int length = btinfo->functions.size ();
334 struct btrace_function *bfun = ftrace_new_function (btinfo, mfun, fun);
338 bfun->flags |= BFUN_UP_LINKS_TO_TAILCALL;
340 ftrace_debug (bfun, "new tail call");
345 /* Return the caller of BFUN or NULL if there is none. This function skips
346 tail calls in the call chain. BTINFO is the branch trace information for
347 the current thread. */
348 static struct btrace_function *
349 ftrace_get_caller (struct btrace_thread_info *btinfo,
350 struct btrace_function *bfun)
352 for (; bfun != NULL; bfun = ftrace_find_call_by_number (btinfo, bfun->up))
353 if ((bfun->flags & BFUN_UP_LINKS_TO_TAILCALL) == 0)
354 return ftrace_find_call_by_number (btinfo, bfun->up);
359 /* Find the innermost caller in the back trace of BFUN with MFUN/FUN
360 symbol information. BTINFO is the branch trace information for the current
363 static struct btrace_function *
364 ftrace_find_caller (struct btrace_thread_info *btinfo,
365 struct btrace_function *bfun,
366 struct minimal_symbol *mfun,
369 for (; bfun != NULL; bfun = ftrace_find_call_by_number (btinfo, bfun->up))
371 /* Skip functions with incompatible symbol information. */
372 if (ftrace_function_switched (bfun, mfun, fun))
375 /* This is the function segment we're looking for. */
382 /* Find the innermost caller in the back trace of BFUN, skipping all
383 function segments that do not end with a call instruction (e.g.
384 tail calls ending with a jump). BTINFO is the branch trace information for
385 the current thread. */
387 static struct btrace_function *
388 ftrace_find_call (struct btrace_thread_info *btinfo,
389 struct btrace_function *bfun)
391 for (; bfun != NULL; bfun = ftrace_find_call_by_number (btinfo, bfun->up))
393 struct btrace_insn *last;
396 if (bfun->errcode != 0)
399 last = VEC_last (btrace_insn_s, bfun->insn);
401 if (last->iclass == BTRACE_INSN_CALL)
408 /* Add a continuation segment for a function into which we return at the end of
410 BTINFO is the branch trace information for the current thread.
411 MFUN and FUN are the symbol information we have for this function. */
413 static struct btrace_function *
414 ftrace_new_return (struct btrace_thread_info *btinfo,
415 struct minimal_symbol *mfun,
418 struct btrace_function *prev, *bfun, *caller;
420 bfun = ftrace_new_function (btinfo, mfun, fun);
421 prev = ftrace_find_call_by_number (btinfo, bfun->number - 1);
423 /* It is important to start at PREV's caller. Otherwise, we might find
424 PREV itself, if PREV is a recursive function. */
425 caller = ftrace_find_call_by_number (btinfo, prev->up);
426 caller = ftrace_find_caller (btinfo, caller, mfun, fun);
429 /* The caller of PREV is the preceding btrace function segment in this
430 function instance. */
431 gdb_assert (caller->next == 0);
433 caller->next = bfun->number;
434 bfun->prev = caller->number;
436 /* Maintain the function level. */
437 bfun->level = caller->level;
439 /* Maintain the call stack. */
440 bfun->up = caller->up;
441 bfun->flags = caller->flags;
443 ftrace_debug (bfun, "new return");
447 /* We did not find a caller. This could mean that something went
448 wrong or that the call is simply not included in the trace. */
450 /* Let's search for some actual call. */
451 caller = ftrace_find_call_by_number (btinfo, prev->up);
452 caller = ftrace_find_call (btinfo, caller);
455 /* There is no call in PREV's back trace. We assume that the
456 branch trace did not include it. */
458 /* Let's find the topmost function and add a new caller for it.
459 This should handle a series of initial tail calls. */
460 while (prev->up != 0)
461 prev = ftrace_find_call_by_number (btinfo, prev->up);
463 bfun->level = prev->level - 1;
465 /* Fix up the call stack for PREV. */
466 ftrace_fixup_caller (btinfo, prev, bfun, BFUN_UP_LINKS_TO_RET);
468 ftrace_debug (bfun, "new return - no caller");
472 /* There is a call in PREV's back trace to which we should have
473 returned but didn't. Let's start a new, separate back trace
474 from PREV's level. */
475 bfun->level = prev->level - 1;
477 /* We fix up the back trace for PREV but leave other function segments
478 on the same level as they are.
479 This should handle things like schedule () correctly where we're
480 switching contexts. */
481 prev->up = bfun->number;
482 prev->flags = BFUN_UP_LINKS_TO_RET;
484 ftrace_debug (bfun, "new return - unknown caller");
491 /* Add a new function segment for a function switch at the end of the trace.
492 BTINFO is the branch trace information for the current thread.
493 MFUN and FUN are the symbol information we have for this function. */
495 static struct btrace_function *
496 ftrace_new_switch (struct btrace_thread_info *btinfo,
497 struct minimal_symbol *mfun,
500 struct btrace_function *prev, *bfun;
502 /* This is an unexplained function switch. We can't really be sure about the
503 call stack, yet the best I can think of right now is to preserve it. */
504 bfun = ftrace_new_function (btinfo, mfun, fun);
505 prev = ftrace_find_call_by_number (btinfo, bfun->number - 1);
507 bfun->flags = prev->flags;
509 ftrace_debug (bfun, "new switch");
514 /* Add a new function segment for a gap in the trace due to a decode error at
515 the end of the trace.
516 BTINFO is the branch trace information for the current thread.
517 ERRCODE is the format-specific error code. */
519 static struct btrace_function *
520 ftrace_new_gap (struct btrace_thread_info *btinfo, int errcode,
521 std::vector<unsigned int> &gaps)
523 struct btrace_function *bfun;
525 if (btinfo->functions.empty ())
526 bfun = ftrace_new_function (btinfo, NULL, NULL);
529 /* We hijack the previous function segment if it was empty. */
530 bfun = &btinfo->functions.back ();
531 if (bfun->errcode != 0 || !VEC_empty (btrace_insn_s, bfun->insn))
532 bfun = ftrace_new_function (btinfo, NULL, NULL);
535 bfun->errcode = errcode;
536 gaps.push_back (bfun->number);
538 ftrace_debug (bfun, "new gap");
543 /* Update the current function segment at the end of the trace in BTINFO with
544 respect to the instruction at PC. This may create new function segments.
545 Return the chronologically latest function segment, never NULL. */
547 static struct btrace_function *
548 ftrace_update_function (struct btrace_thread_info *btinfo, CORE_ADDR pc)
550 struct bound_minimal_symbol bmfun;
551 struct minimal_symbol *mfun;
553 struct btrace_insn *last;
554 struct btrace_function *bfun;
556 /* Try to determine the function we're in. We use both types of symbols
557 to avoid surprises when we sometimes get a full symbol and sometimes
558 only a minimal symbol. */
559 fun = find_pc_function (pc);
560 bmfun = lookup_minimal_symbol_by_pc (pc);
563 if (fun == NULL && mfun == NULL)
564 DEBUG_FTRACE ("no symbol at %s", core_addr_to_string_nz (pc));
566 /* If we didn't have a function, we create one. */
567 if (btinfo->functions.empty ())
568 return ftrace_new_function (btinfo, mfun, fun);
570 /* If we had a gap before, we create a function. */
571 bfun = &btinfo->functions.back ();
572 if (bfun->errcode != 0)
573 return ftrace_new_function (btinfo, mfun, fun);
575 /* Check the last instruction, if we have one.
576 We do this check first, since it allows us to fill in the call stack
577 links in addition to the normal flow links. */
579 if (!VEC_empty (btrace_insn_s, bfun->insn))
580 last = VEC_last (btrace_insn_s, bfun->insn);
584 switch (last->iclass)
586 case BTRACE_INSN_RETURN:
590 /* On some systems, _dl_runtime_resolve returns to the resolved
591 function instead of jumping to it. From our perspective,
592 however, this is a tailcall.
593 If we treated it as return, we wouldn't be able to find the
594 resolved function in our stack back trace. Hence, we would
595 lose the current stack back trace and start anew with an empty
596 back trace. When the resolved function returns, we would then
597 create a stack back trace with the same function names but
598 different frame id's. This will confuse stepping. */
599 fname = ftrace_print_function_name (bfun);
600 if (strcmp (fname, "_dl_runtime_resolve") == 0)
601 return ftrace_new_tailcall (btinfo, mfun, fun);
603 return ftrace_new_return (btinfo, mfun, fun);
606 case BTRACE_INSN_CALL:
607 /* Ignore calls to the next instruction. They are used for PIC. */
608 if (last->pc + last->size == pc)
611 return ftrace_new_call (btinfo, mfun, fun);
613 case BTRACE_INSN_JUMP:
617 start = get_pc_function_start (pc);
619 /* A jump to the start of a function is (typically) a tail call. */
621 return ftrace_new_tailcall (btinfo, mfun, fun);
623 /* If we can't determine the function for PC, we treat a jump at
624 the end of the block as tail call if we're switching functions
625 and as an intra-function branch if we don't. */
626 if (start == 0 && ftrace_function_switched (bfun, mfun, fun))
627 return ftrace_new_tailcall (btinfo, mfun, fun);
634 /* Check if we're switching functions for some other reason. */
635 if (ftrace_function_switched (bfun, mfun, fun))
637 DEBUG_FTRACE ("switching from %s in %s at %s",
638 ftrace_print_insn_addr (last),
639 ftrace_print_function_name (bfun),
640 ftrace_print_filename (bfun));
642 return ftrace_new_switch (btinfo, mfun, fun);
648 /* Add the instruction at PC to BFUN's instructions. */
651 ftrace_update_insns (struct btrace_function *bfun,
652 const struct btrace_insn *insn)
654 VEC_safe_push (btrace_insn_s, bfun->insn, insn);
656 if (record_debug > 1)
657 ftrace_debug (bfun, "update insn");
660 /* Classify the instruction at PC. */
662 static enum btrace_insn_class
663 ftrace_classify_insn (struct gdbarch *gdbarch, CORE_ADDR pc)
665 enum btrace_insn_class iclass;
667 iclass = BTRACE_INSN_OTHER;
670 if (gdbarch_insn_is_call (gdbarch, pc))
671 iclass = BTRACE_INSN_CALL;
672 else if (gdbarch_insn_is_ret (gdbarch, pc))
673 iclass = BTRACE_INSN_RETURN;
674 else if (gdbarch_insn_is_jump (gdbarch, pc))
675 iclass = BTRACE_INSN_JUMP;
677 CATCH (error, RETURN_MASK_ERROR)
685 /* Try to match the back trace at LHS to the back trace at RHS. Returns the
686 number of matching function segments or zero if the back traces do not
687 match. BTINFO is the branch trace information for the current thread. */
690 ftrace_match_backtrace (struct btrace_thread_info *btinfo,
691 struct btrace_function *lhs,
692 struct btrace_function *rhs)
696 for (matches = 0; lhs != NULL && rhs != NULL; ++matches)
698 if (ftrace_function_switched (lhs, rhs->msym, rhs->sym))
701 lhs = ftrace_get_caller (btinfo, lhs);
702 rhs = ftrace_get_caller (btinfo, rhs);
708 /* Add ADJUSTMENT to the level of BFUN and succeeding function segments.
709 BTINFO is the branch trace information for the current thread. */
712 ftrace_fixup_level (struct btrace_thread_info *btinfo,
713 struct btrace_function *bfun, int adjustment)
718 DEBUG_FTRACE ("fixup level (%+d)", adjustment);
719 ftrace_debug (bfun, "..bfun");
723 bfun->level += adjustment;
724 bfun = ftrace_find_call_by_number (btinfo, bfun->number + 1);
728 /* Recompute the global level offset. Traverse the function trace and compute
729 the global level offset as the negative of the minimal function level. */
732 ftrace_compute_global_level_offset (struct btrace_thread_info *btinfo)
739 if (btinfo->functions.empty ())
742 unsigned int length = btinfo->functions.size() - 1;
743 for (unsigned int i = 0; i < length; ++i)
744 level = std::min (level, btinfo->functions[i].level);
746 /* The last function segment contains the current instruction, which is not
747 really part of the trace. If it contains just this one instruction, we
748 ignore the segment. */
749 struct btrace_function *last = &btinfo->functions.back();
750 if (VEC_length (btrace_insn_s, last->insn) != 1)
751 level = std::min (level, last->level);
753 DEBUG_FTRACE ("setting global level offset: %d", -level);
754 btinfo->level = -level;
757 /* Connect the function segments PREV and NEXT in a bottom-to-top walk as in
758 ftrace_connect_backtrace. BTINFO is the branch trace information for the
762 ftrace_connect_bfun (struct btrace_thread_info *btinfo,
763 struct btrace_function *prev,
764 struct btrace_function *next)
766 DEBUG_FTRACE ("connecting...");
767 ftrace_debug (prev, "..prev");
768 ftrace_debug (next, "..next");
770 /* The function segments are not yet connected. */
771 gdb_assert (prev->next == 0);
772 gdb_assert (next->prev == 0);
774 prev->next = next->number;
775 next->prev = prev->number;
777 /* We may have moved NEXT to a different function level. */
778 ftrace_fixup_level (btinfo, next, prev->level - next->level);
780 /* If we run out of back trace for one, let's use the other's. */
783 const btrace_function_flags flags = next->flags;
785 next = ftrace_find_call_by_number (btinfo, next->up);
788 DEBUG_FTRACE ("using next's callers");
789 ftrace_fixup_caller (btinfo, prev, next, flags);
792 else if (next->up == 0)
794 const btrace_function_flags flags = prev->flags;
796 prev = ftrace_find_call_by_number (btinfo, prev->up);
799 DEBUG_FTRACE ("using prev's callers");
800 ftrace_fixup_caller (btinfo, next, prev, flags);
805 /* PREV may have a tailcall caller, NEXT can't. If it does, fixup the up
806 link to add the tail callers to NEXT's back trace.
808 This removes NEXT->UP from NEXT's back trace. It will be added back
809 when connecting NEXT and PREV's callers - provided they exist.
811 If PREV's back trace consists of a series of tail calls without an
812 actual call, there will be no further connection and NEXT's caller will
813 be removed for good. To catch this case, we handle it here and connect
814 the top of PREV's back trace to NEXT's caller. */
815 if ((prev->flags & BFUN_UP_LINKS_TO_TAILCALL) != 0)
817 struct btrace_function *caller;
818 btrace_function_flags next_flags, prev_flags;
820 /* We checked NEXT->UP above so CALLER can't be NULL. */
821 caller = ftrace_find_call_by_number (btinfo, next->up);
822 next_flags = next->flags;
823 prev_flags = prev->flags;
825 DEBUG_FTRACE ("adding prev's tail calls to next");
827 prev = ftrace_find_call_by_number (btinfo, prev->up);
828 ftrace_fixup_caller (btinfo, next, prev, prev_flags);
830 for (; prev != NULL; prev = ftrace_find_call_by_number (btinfo,
833 /* At the end of PREV's back trace, continue with CALLER. */
836 DEBUG_FTRACE ("fixing up link for tailcall chain");
837 ftrace_debug (prev, "..top");
838 ftrace_debug (caller, "..up");
840 ftrace_fixup_caller (btinfo, prev, caller, next_flags);
842 /* If we skipped any tail calls, this may move CALLER to a
843 different function level.
845 Note that changing CALLER's level is only OK because we
846 know that this is the last iteration of the bottom-to-top
847 walk in ftrace_connect_backtrace.
849 Otherwise we will fix up CALLER's level when we connect it
850 to PREV's caller in the next iteration. */
851 ftrace_fixup_level (btinfo, caller,
852 prev->level - caller->level - 1);
856 /* There's nothing to do if we find a real call. */
857 if ((prev->flags & BFUN_UP_LINKS_TO_TAILCALL) == 0)
859 DEBUG_FTRACE ("will fix up link in next iteration");
867 /* Connect function segments on the same level in the back trace at LHS and RHS.
868 The back traces at LHS and RHS are expected to match according to
869 ftrace_match_backtrace. BTINFO is the branch trace information for the
873 ftrace_connect_backtrace (struct btrace_thread_info *btinfo,
874 struct btrace_function *lhs,
875 struct btrace_function *rhs)
877 while (lhs != NULL && rhs != NULL)
879 struct btrace_function *prev, *next;
881 gdb_assert (!ftrace_function_switched (lhs, rhs->msym, rhs->sym));
883 /* Connecting LHS and RHS may change the up link. */
887 lhs = ftrace_get_caller (btinfo, lhs);
888 rhs = ftrace_get_caller (btinfo, rhs);
890 ftrace_connect_bfun (btinfo, prev, next);
894 /* Bridge the gap between two function segments left and right of a gap if their
895 respective back traces match in at least MIN_MATCHES functions. BTINFO is
896 the branch trace information for the current thread.
898 Returns non-zero if the gap could be bridged, zero otherwise. */
901 ftrace_bridge_gap (struct btrace_thread_info *btinfo,
902 struct btrace_function *lhs, struct btrace_function *rhs,
905 struct btrace_function *best_l, *best_r, *cand_l, *cand_r;
908 DEBUG_FTRACE ("checking gap at insn %u (req matches: %d)",
909 rhs->insn_offset - 1, min_matches);
915 /* We search the back traces of LHS and RHS for valid connections and connect
916 the two functon segments that give the longest combined back trace. */
918 for (cand_l = lhs; cand_l != NULL;
919 cand_l = ftrace_get_caller (btinfo, cand_l))
920 for (cand_r = rhs; cand_r != NULL;
921 cand_r = ftrace_get_caller (btinfo, cand_r))
925 matches = ftrace_match_backtrace (btinfo, cand_l, cand_r);
926 if (best_matches < matches)
928 best_matches = matches;
934 /* We need at least MIN_MATCHES matches. */
935 gdb_assert (min_matches > 0);
936 if (best_matches < min_matches)
939 DEBUG_FTRACE ("..matches: %d", best_matches);
941 /* We will fix up the level of BEST_R and succeeding function segments such
942 that BEST_R's level matches BEST_L's when we connect BEST_L to BEST_R.
944 This will ignore the level of RHS and following if BEST_R != RHS. I.e. if
945 BEST_R is a successor of RHS in the back trace of RHS (phases 1 and 3).
947 To catch this, we already fix up the level here where we can start at RHS
948 instead of at BEST_R. We will ignore the level fixup when connecting
949 BEST_L to BEST_R as they will already be on the same level. */
950 ftrace_fixup_level (btinfo, rhs, best_l->level - best_r->level);
952 ftrace_connect_backtrace (btinfo, best_l, best_r);
957 /* Try to bridge gaps due to overflow or decode errors by connecting the
958 function segments that are separated by the gap. */
961 btrace_bridge_gaps (struct thread_info *tp, std::vector<unsigned int> &gaps)
963 struct btrace_thread_info *btinfo = &tp->btrace;
964 std::vector<unsigned int> remaining;
967 DEBUG ("bridge gaps");
969 /* We require a minimum amount of matches for bridging a gap. The number of
970 required matches will be lowered with each iteration.
972 The more matches the higher our confidence that the bridging is correct.
973 For big gaps or small traces, however, it may not be feasible to require a
974 high number of matches. */
975 for (min_matches = 5; min_matches > 0; --min_matches)
977 /* Let's try to bridge as many gaps as we can. In some cases, we need to
978 skip a gap and revisit it again after we closed later gaps. */
979 while (!gaps.empty ())
981 for (const unsigned int number : gaps)
983 struct btrace_function *gap, *lhs, *rhs;
986 gap = ftrace_find_call_by_number (btinfo, number);
988 /* We may have a sequence of gaps if we run from one error into
989 the next as we try to re-sync onto the trace stream. Ignore
990 all but the leftmost gap in such a sequence.
992 Also ignore gaps at the beginning of the trace. */
993 lhs = ftrace_find_call_by_number (btinfo, gap->number - 1);
994 if (lhs == NULL || lhs->errcode != 0)
997 /* Skip gaps to the right. */
998 rhs = ftrace_find_call_by_number (btinfo, gap->number + 1);
999 while (rhs != NULL && rhs->errcode != 0)
1000 rhs = ftrace_find_call_by_number (btinfo, rhs->number + 1);
1002 /* Ignore gaps at the end of the trace. */
1006 bridged = ftrace_bridge_gap (btinfo, lhs, rhs, min_matches);
1008 /* Keep track of gaps we were not able to bridge and try again.
1009 If we just pushed them to the end of GAPS we would risk an
1010 infinite loop in case we simply cannot bridge a gap. */
1012 remaining.push_back (number);
1015 /* Let's see if we made any progress. */
1016 if (remaining.size () == gaps.size ())
1020 gaps.swap (remaining);
1023 /* We get here if either GAPS is empty or if GAPS equals REMAINING. */
1030 /* We may omit this in some cases. Not sure it is worth the extra
1031 complication, though. */
1032 ftrace_compute_global_level_offset (btinfo);
1035 /* Compute the function branch trace from BTS trace. */
1038 btrace_compute_ftrace_bts (struct thread_info *tp,
1039 const struct btrace_data_bts *btrace,
1040 std::vector<unsigned int> &gaps)
1042 struct btrace_thread_info *btinfo;
1043 struct gdbarch *gdbarch;
1047 gdbarch = target_gdbarch ();
1048 btinfo = &tp->btrace;
1049 blk = VEC_length (btrace_block_s, btrace->blocks);
1051 if (btinfo->functions.empty ())
1054 level = -btinfo->level;
1058 btrace_block_s *block;
1063 block = VEC_index (btrace_block_s, btrace->blocks, blk);
1068 struct btrace_function *bfun;
1069 struct btrace_insn insn;
1072 /* We should hit the end of the block. Warn if we went too far. */
1073 if (block->end < pc)
1075 /* Indicate the gap in the trace. */
1076 bfun = ftrace_new_gap (btinfo, BDE_BTS_OVERFLOW, gaps);
1078 warning (_("Recorded trace may be corrupted at instruction "
1079 "%u (pc = %s)."), bfun->insn_offset - 1,
1080 core_addr_to_string_nz (pc));
1085 bfun = ftrace_update_function (btinfo, pc);
1087 /* Maintain the function level offset.
1088 For all but the last block, we do it here. */
1090 level = std::min (level, bfun->level);
1095 size = gdb_insn_length (gdbarch, pc);
1097 CATCH (error, RETURN_MASK_ERROR)
1104 insn.iclass = ftrace_classify_insn (gdbarch, pc);
1107 ftrace_update_insns (bfun, &insn);
1109 /* We're done once we pushed the instruction at the end. */
1110 if (block->end == pc)
1113 /* We can't continue if we fail to compute the size. */
1116 /* Indicate the gap in the trace. We just added INSN so we're
1117 not at the beginning. */
1118 bfun = ftrace_new_gap (btinfo, BDE_BTS_INSN_SIZE, gaps);
1120 warning (_("Recorded trace may be incomplete at instruction %u "
1121 "(pc = %s)."), bfun->insn_offset - 1,
1122 core_addr_to_string_nz (pc));
1129 /* Maintain the function level offset.
1130 For the last block, we do it here to not consider the last
1132 Since the last instruction corresponds to the current instruction
1133 and is not really part of the execution history, it shouldn't
1134 affect the level. */
1136 level = std::min (level, bfun->level);
1140 /* LEVEL is the minimal function level of all btrace function segments.
1141 Define the global level offset to -LEVEL so all function levels are
1142 normalized to start at zero. */
1143 btinfo->level = -level;
1146 #if defined (HAVE_LIBIPT)
1148 static enum btrace_insn_class
1149 pt_reclassify_insn (enum pt_insn_class iclass)
1154 return BTRACE_INSN_CALL;
1157 return BTRACE_INSN_RETURN;
1160 return BTRACE_INSN_JUMP;
1163 return BTRACE_INSN_OTHER;
1167 /* Return the btrace instruction flags for INSN. */
1169 static btrace_insn_flags
1170 pt_btrace_insn_flags (const struct pt_insn &insn)
1172 btrace_insn_flags flags = 0;
1174 if (insn.speculative)
1175 flags |= BTRACE_INSN_FLAG_SPECULATIVE;
1180 /* Return the btrace instruction for INSN. */
1183 pt_btrace_insn (const struct pt_insn &insn)
1185 return {(CORE_ADDR) insn.ip, (gdb_byte) insn.size,
1186 pt_reclassify_insn (insn.iclass),
1187 pt_btrace_insn_flags (insn)};
1191 /* Add function branch trace to BTINFO using DECODER. */
1194 ftrace_add_pt (struct btrace_thread_info *btinfo,
1195 struct pt_insn_decoder *decoder,
1197 std::vector<unsigned int> &gaps)
1199 struct btrace_function *bfun;
1205 struct pt_insn insn;
1207 errcode = pt_insn_sync_forward (decoder);
1210 if (errcode != -pte_eos)
1211 warning (_("Failed to synchronize onto the Intel Processor "
1212 "Trace stream: %s."), pt_errstr (pt_errcode (errcode)));
1218 errcode = pt_insn_next (decoder, &insn, sizeof(insn));
1222 /* Look for gaps in the trace - unless we're at the beginning. */
1223 if (!btinfo->functions.empty ())
1225 /* Tracing is disabled and re-enabled each time we enter the
1226 kernel. Most times, we continue from the same instruction we
1227 stopped before. This is indicated via the RESUMED instruction
1228 flag. The ENABLED instruction flag means that we continued
1229 from some other instruction. Indicate this as a trace gap. */
1232 bfun = ftrace_new_gap (btinfo, BDE_PT_DISABLED, gaps);
1234 pt_insn_get_offset (decoder, &offset);
1236 warning (_("Non-contiguous trace at instruction %u (offset "
1237 "= 0x%" PRIx64 ", pc = 0x%" PRIx64 ")."),
1238 bfun->insn_offset - 1, offset, insn.ip);
1242 /* Indicate trace overflows. */
1245 bfun = ftrace_new_gap (btinfo, BDE_PT_OVERFLOW, gaps);
1247 pt_insn_get_offset (decoder, &offset);
1249 warning (_("Overflow at instruction %u (offset = 0x%" PRIx64
1250 ", pc = 0x%" PRIx64 ")."), bfun->insn_offset - 1,
1254 bfun = ftrace_update_function (btinfo, insn.ip);
1256 /* Maintain the function level offset. */
1257 *plevel = std::min (*plevel, bfun->level);
1259 btrace_insn btinsn = pt_btrace_insn (insn);
1260 ftrace_update_insns (bfun, &btinsn);
1263 if (errcode == -pte_eos)
1266 /* Indicate the gap in the trace. */
1267 bfun = ftrace_new_gap (btinfo, errcode, gaps);
1269 pt_insn_get_offset (decoder, &offset);
1271 warning (_("Decode error (%d) at instruction %u (offset = 0x%" PRIx64
1272 ", pc = 0x%" PRIx64 "): %s."), errcode, bfun->insn_offset - 1,
1273 offset, insn.ip, pt_errstr (pt_errcode (errcode)));
1277 /* A callback function to allow the trace decoder to read the inferior's
1281 btrace_pt_readmem_callback (gdb_byte *buffer, size_t size,
1282 const struct pt_asid *asid, uint64_t pc,
1285 int result, errcode;
1287 result = (int) size;
1290 errcode = target_read_code ((CORE_ADDR) pc, buffer, size);
1292 result = -pte_nomap;
1294 CATCH (error, RETURN_MASK_ERROR)
1296 result = -pte_nomap;
1303 /* Translate the vendor from one enum to another. */
1305 static enum pt_cpu_vendor
1306 pt_translate_cpu_vendor (enum btrace_cpu_vendor vendor)
1318 /* Finalize the function branch trace after decode. */
1320 static void btrace_finalize_ftrace_pt (struct pt_insn_decoder *decoder,
1321 struct thread_info *tp, int level)
1323 pt_insn_free_decoder (decoder);
1325 /* LEVEL is the minimal function level of all btrace function segments.
1326 Define the global level offset to -LEVEL so all function levels are
1327 normalized to start at zero. */
1328 tp->btrace.level = -level;
1330 /* Add a single last instruction entry for the current PC.
1331 This allows us to compute the backtrace at the current PC using both
1332 standard unwind and btrace unwind.
1333 This extra entry is ignored by all record commands. */
1337 /* Compute the function branch trace from Intel Processor Trace
1341 btrace_compute_ftrace_pt (struct thread_info *tp,
1342 const struct btrace_data_pt *btrace,
1343 std::vector<unsigned int> &gaps)
1345 struct btrace_thread_info *btinfo;
1346 struct pt_insn_decoder *decoder;
1347 struct pt_config config;
1350 if (btrace->size == 0)
1353 btinfo = &tp->btrace;
1354 if (btinfo->functions.empty ())
1357 level = -btinfo->level;
1359 pt_config_init(&config);
1360 config.begin = btrace->data;
1361 config.end = btrace->data + btrace->size;
1363 config.cpu.vendor = pt_translate_cpu_vendor (btrace->config.cpu.vendor);
1364 config.cpu.family = btrace->config.cpu.family;
1365 config.cpu.model = btrace->config.cpu.model;
1366 config.cpu.stepping = btrace->config.cpu.stepping;
1368 errcode = pt_cpu_errata (&config.errata, &config.cpu);
1370 error (_("Failed to configure the Intel Processor Trace decoder: %s."),
1371 pt_errstr (pt_errcode (errcode)));
1373 decoder = pt_insn_alloc_decoder (&config);
1374 if (decoder == NULL)
1375 error (_("Failed to allocate the Intel Processor Trace decoder."));
1379 struct pt_image *image;
1381 image = pt_insn_get_image(decoder);
1383 error (_("Failed to configure the Intel Processor Trace decoder."));
1385 errcode = pt_image_set_callback(image, btrace_pt_readmem_callback, NULL);
1387 error (_("Failed to configure the Intel Processor Trace decoder: "
1388 "%s."), pt_errstr (pt_errcode (errcode)));
1390 ftrace_add_pt (btinfo, decoder, &level, gaps);
1392 CATCH (error, RETURN_MASK_ALL)
1394 /* Indicate a gap in the trace if we quit trace processing. */
1395 if (error.reason == RETURN_QUIT && !btinfo->functions.empty ())
1396 ftrace_new_gap (btinfo, BDE_PT_USER_QUIT, gaps);
1398 btrace_finalize_ftrace_pt (decoder, tp, level);
1400 throw_exception (error);
1404 btrace_finalize_ftrace_pt (decoder, tp, level);
1407 #else /* defined (HAVE_LIBIPT) */
1410 btrace_compute_ftrace_pt (struct thread_info *tp,
1411 const struct btrace_data_pt *btrace,
1412 std::vector<unsigned int> &gaps)
1414 internal_error (__FILE__, __LINE__, _("Unexpected branch trace format."));
1417 #endif /* defined (HAVE_LIBIPT) */
1419 /* Compute the function branch trace from a block branch trace BTRACE for
1420 a thread given by BTINFO. */
1423 btrace_compute_ftrace_1 (struct thread_info *tp, struct btrace_data *btrace,
1424 std::vector<unsigned int> &gaps)
1426 DEBUG ("compute ftrace");
1428 switch (btrace->format)
1430 case BTRACE_FORMAT_NONE:
1433 case BTRACE_FORMAT_BTS:
1434 btrace_compute_ftrace_bts (tp, &btrace->variant.bts, gaps);
1437 case BTRACE_FORMAT_PT:
1438 btrace_compute_ftrace_pt (tp, &btrace->variant.pt, gaps);
1442 internal_error (__FILE__, __LINE__, _("Unkown branch trace format."));
1446 btrace_finalize_ftrace (struct thread_info *tp, std::vector<unsigned int> &gaps)
1450 tp->btrace.ngaps += gaps.size ();
1451 btrace_bridge_gaps (tp, gaps);
1456 btrace_compute_ftrace (struct thread_info *tp, struct btrace_data *btrace)
1458 std::vector<unsigned int> gaps;
1462 btrace_compute_ftrace_1 (tp, btrace, gaps);
1464 CATCH (error, RETURN_MASK_ALL)
1466 btrace_finalize_ftrace (tp, gaps);
1468 throw_exception (error);
1472 btrace_finalize_ftrace (tp, gaps);
1475 /* Add an entry for the current PC. */
1478 btrace_add_pc (struct thread_info *tp)
1480 struct btrace_data btrace;
1481 struct btrace_block *block;
1482 struct regcache *regcache;
1483 struct cleanup *cleanup;
1486 regcache = get_thread_regcache (tp->ptid);
1487 pc = regcache_read_pc (regcache);
1489 btrace_data_init (&btrace);
1490 btrace.format = BTRACE_FORMAT_BTS;
1491 btrace.variant.bts.blocks = NULL;
1493 cleanup = make_cleanup_btrace_data (&btrace);
1495 block = VEC_safe_push (btrace_block_s, btrace.variant.bts.blocks, NULL);
1499 btrace_compute_ftrace (tp, &btrace);
1501 do_cleanups (cleanup);
1507 btrace_enable (struct thread_info *tp, const struct btrace_config *conf)
1509 if (tp->btrace.target != NULL)
1512 #if !defined (HAVE_LIBIPT)
1513 if (conf->format == BTRACE_FORMAT_PT)
1514 error (_("GDB does not support Intel Processor Trace."));
1515 #endif /* !defined (HAVE_LIBIPT) */
1517 if (!target_supports_btrace (conf->format))
1518 error (_("Target does not support branch tracing."));
1520 DEBUG ("enable thread %s (%s)", print_thread_id (tp),
1521 target_pid_to_str (tp->ptid));
1523 tp->btrace.target = target_enable_btrace (tp->ptid, conf);
1525 /* We're done if we failed to enable tracing. */
1526 if (tp->btrace.target == NULL)
1529 /* We need to undo the enable in case of errors. */
1532 /* Add an entry for the current PC so we start tracing from where we
1535 If we can't access TP's registers, TP is most likely running. In this
1536 case, we can't really say where tracing was enabled so it should be
1537 safe to simply skip this step.
1539 This is not relevant for BTRACE_FORMAT_PT since the trace will already
1540 start at the PC at which tracing was enabled. */
1541 if (conf->format != BTRACE_FORMAT_PT
1542 && can_access_registers_ptid (tp->ptid))
1545 CATCH (exception, RETURN_MASK_ALL)
1547 btrace_disable (tp);
1549 throw_exception (exception);
1556 const struct btrace_config *
1557 btrace_conf (const struct btrace_thread_info *btinfo)
1559 if (btinfo->target == NULL)
1562 return target_btrace_conf (btinfo->target);
1568 btrace_disable (struct thread_info *tp)
1570 struct btrace_thread_info *btp = &tp->btrace;
1573 if (btp->target == NULL)
1576 DEBUG ("disable thread %s (%s)", print_thread_id (tp),
1577 target_pid_to_str (tp->ptid));
1579 target_disable_btrace (btp->target);
1588 btrace_teardown (struct thread_info *tp)
1590 struct btrace_thread_info *btp = &tp->btrace;
1593 if (btp->target == NULL)
1596 DEBUG ("teardown thread %s (%s)", print_thread_id (tp),
1597 target_pid_to_str (tp->ptid));
1599 target_teardown_btrace (btp->target);
1605 /* Stitch branch trace in BTS format. */
1608 btrace_stitch_bts (struct btrace_data_bts *btrace, struct thread_info *tp)
1610 struct btrace_thread_info *btinfo;
1611 struct btrace_function *last_bfun;
1612 struct btrace_insn *last_insn;
1613 btrace_block_s *first_new_block;
1615 btinfo = &tp->btrace;
1616 gdb_assert (!btinfo->functions.empty ());
1617 gdb_assert (!VEC_empty (btrace_block_s, btrace->blocks));
1619 last_bfun = &btinfo->functions.back ();
1621 /* If the existing trace ends with a gap, we just glue the traces
1622 together. We need to drop the last (i.e. chronologically first) block
1623 of the new trace, though, since we can't fill in the start address.*/
1624 if (VEC_empty (btrace_insn_s, last_bfun->insn))
1626 VEC_pop (btrace_block_s, btrace->blocks);
1630 /* Beware that block trace starts with the most recent block, so the
1631 chronologically first block in the new trace is the last block in
1632 the new trace's block vector. */
1633 first_new_block = VEC_last (btrace_block_s, btrace->blocks);
1634 last_insn = VEC_last (btrace_insn_s, last_bfun->insn);
1636 /* If the current PC at the end of the block is the same as in our current
1637 trace, there are two explanations:
1638 1. we executed the instruction and some branch brought us back.
1639 2. we have not made any progress.
1640 In the first case, the delta trace vector should contain at least two
1642 In the second case, the delta trace vector should contain exactly one
1643 entry for the partial block containing the current PC. Remove it. */
1644 if (first_new_block->end == last_insn->pc
1645 && VEC_length (btrace_block_s, btrace->blocks) == 1)
1647 VEC_pop (btrace_block_s, btrace->blocks);
1651 DEBUG ("stitching %s to %s", ftrace_print_insn_addr (last_insn),
1652 core_addr_to_string_nz (first_new_block->end));
1654 /* Do a simple sanity check to make sure we don't accidentally end up
1655 with a bad block. This should not occur in practice. */
1656 if (first_new_block->end < last_insn->pc)
1658 warning (_("Error while trying to read delta trace. Falling back to "
1663 /* We adjust the last block to start at the end of our current trace. */
1664 gdb_assert (first_new_block->begin == 0);
1665 first_new_block->begin = last_insn->pc;
1667 /* We simply pop the last insn so we can insert it again as part of
1668 the normal branch trace computation.
1669 Since instruction iterators are based on indices in the instructions
1670 vector, we don't leave any pointers dangling. */
1671 DEBUG ("pruning insn at %s for stitching",
1672 ftrace_print_insn_addr (last_insn));
1674 VEC_pop (btrace_insn_s, last_bfun->insn);
1676 /* The instructions vector may become empty temporarily if this has
1677 been the only instruction in this function segment.
1678 This violates the invariant but will be remedied shortly by
1679 btrace_compute_ftrace when we add the new trace. */
1681 /* The only case where this would hurt is if the entire trace consisted
1682 of just that one instruction. If we remove it, we might turn the now
1683 empty btrace function segment into a gap. But we don't want gaps at
1684 the beginning. To avoid this, we remove the entire old trace. */
1685 if (last_bfun->number == 1 && VEC_empty (btrace_insn_s, last_bfun->insn))
1691 /* Adjust the block trace in order to stitch old and new trace together.
1692 BTRACE is the new delta trace between the last and the current stop.
1693 TP is the traced thread.
1694 May modifx BTRACE as well as the existing trace in TP.
1695 Return 0 on success, -1 otherwise. */
1698 btrace_stitch_trace (struct btrace_data *btrace, struct thread_info *tp)
1700 /* If we don't have trace, there's nothing to do. */
1701 if (btrace_data_empty (btrace))
1704 switch (btrace->format)
1706 case BTRACE_FORMAT_NONE:
1709 case BTRACE_FORMAT_BTS:
1710 return btrace_stitch_bts (&btrace->variant.bts, tp);
1712 case BTRACE_FORMAT_PT:
1713 /* Delta reads are not supported. */
1717 internal_error (__FILE__, __LINE__, _("Unkown branch trace format."));
1720 /* Clear the branch trace histories in BTINFO. */
1723 btrace_clear_history (struct btrace_thread_info *btinfo)
1725 xfree (btinfo->insn_history);
1726 xfree (btinfo->call_history);
1727 xfree (btinfo->replay);
1729 btinfo->insn_history = NULL;
1730 btinfo->call_history = NULL;
1731 btinfo->replay = NULL;
1734 /* Clear the branch trace maintenance histories in BTINFO. */
1737 btrace_maint_clear (struct btrace_thread_info *btinfo)
1739 switch (btinfo->data.format)
1744 case BTRACE_FORMAT_BTS:
1745 btinfo->maint.variant.bts.packet_history.begin = 0;
1746 btinfo->maint.variant.bts.packet_history.end = 0;
1749 #if defined (HAVE_LIBIPT)
1750 case BTRACE_FORMAT_PT:
1751 xfree (btinfo->maint.variant.pt.packets);
1753 btinfo->maint.variant.pt.packets = NULL;
1754 btinfo->maint.variant.pt.packet_history.begin = 0;
1755 btinfo->maint.variant.pt.packet_history.end = 0;
1757 #endif /* defined (HAVE_LIBIPT) */
1764 btrace_decode_error (enum btrace_format format, int errcode)
1768 case BTRACE_FORMAT_BTS:
1771 case BDE_BTS_OVERFLOW:
1772 return _("instruction overflow");
1774 case BDE_BTS_INSN_SIZE:
1775 return _("unknown instruction");
1782 #if defined (HAVE_LIBIPT)
1783 case BTRACE_FORMAT_PT:
1786 case BDE_PT_USER_QUIT:
1787 return _("trace decode cancelled");
1789 case BDE_PT_DISABLED:
1790 return _("disabled");
1792 case BDE_PT_OVERFLOW:
1793 return _("overflow");
1797 return pt_errstr (pt_errcode (errcode));
1801 #endif /* defined (HAVE_LIBIPT) */
1807 return _("unknown");
1813 btrace_fetch (struct thread_info *tp)
1815 struct btrace_thread_info *btinfo;
1816 struct btrace_target_info *tinfo;
1817 struct btrace_data btrace;
1818 struct cleanup *cleanup;
1821 DEBUG ("fetch thread %s (%s)", print_thread_id (tp),
1822 target_pid_to_str (tp->ptid));
1824 btinfo = &tp->btrace;
1825 tinfo = btinfo->target;
1829 /* There's no way we could get new trace while replaying.
1830 On the other hand, delta trace would return a partial record with the
1831 current PC, which is the replay PC, not the last PC, as expected. */
1832 if (btinfo->replay != NULL)
1835 /* With CLI usage, TP->PTID always equals INFERIOR_PTID here. Now that we
1836 can store a gdb.Record object in Python referring to a different thread
1837 than the current one, temporarily set INFERIOR_PTID. */
1838 cleanup = save_inferior_ptid ();
1839 inferior_ptid = tp->ptid;
1841 /* We should not be called on running or exited threads. */
1842 gdb_assert (can_access_registers_ptid (tp->ptid));
1844 btrace_data_init (&btrace);
1845 make_cleanup_btrace_data (&btrace);
1847 /* Let's first try to extend the trace we already have. */
1848 if (!btinfo->functions.empty ())
1850 errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_DELTA);
1853 /* Success. Let's try to stitch the traces together. */
1854 errcode = btrace_stitch_trace (&btrace, tp);
1858 /* We failed to read delta trace. Let's try to read new trace. */
1859 errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_NEW);
1861 /* If we got any new trace, discard what we have. */
1862 if (errcode == 0 && !btrace_data_empty (&btrace))
1866 /* If we were not able to read the trace, we start over. */
1870 errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_ALL);
1874 errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_ALL);
1876 /* If we were not able to read the branch trace, signal an error. */
1878 error (_("Failed to read branch trace."));
1880 /* Compute the trace, provided we have any. */
1881 if (!btrace_data_empty (&btrace))
1883 /* Store the raw trace data. The stored data will be cleared in
1884 btrace_clear, so we always append the new trace. */
1885 btrace_data_append (&btinfo->data, &btrace);
1886 btrace_maint_clear (btinfo);
1888 btrace_clear_history (btinfo);
1889 btrace_compute_ftrace (tp, &btrace);
1892 do_cleanups (cleanup);
1898 btrace_clear (struct thread_info *tp)
1900 struct btrace_thread_info *btinfo;
1902 DEBUG ("clear thread %s (%s)", print_thread_id (tp),
1903 target_pid_to_str (tp->ptid));
1905 /* Make sure btrace frames that may hold a pointer into the branch
1906 trace data are destroyed. */
1907 reinit_frame_cache ();
1909 btinfo = &tp->btrace;
1910 for (auto &bfun : btinfo->functions)
1911 VEC_free (btrace_insn_s, bfun.insn);
1913 btinfo->functions.clear ();
1916 /* Must clear the maint data before - it depends on BTINFO->DATA. */
1917 btrace_maint_clear (btinfo);
1918 btrace_data_clear (&btinfo->data);
1919 btrace_clear_history (btinfo);
1925 btrace_free_objfile (struct objfile *objfile)
1927 struct thread_info *tp;
1929 DEBUG ("free objfile");
1931 ALL_NON_EXITED_THREADS (tp)
1935 #if defined (HAVE_LIBEXPAT)
1937 /* Check the btrace document version. */
1940 check_xml_btrace_version (struct gdb_xml_parser *parser,
1941 const struct gdb_xml_element *element,
1942 void *user_data, VEC (gdb_xml_value_s) *attributes)
1945 = (const char *) xml_find_attribute (attributes, "version")->value;
1947 if (strcmp (version, "1.0") != 0)
1948 gdb_xml_error (parser, _("Unsupported btrace version: \"%s\""), version);
1951 /* Parse a btrace "block" xml record. */
1954 parse_xml_btrace_block (struct gdb_xml_parser *parser,
1955 const struct gdb_xml_element *element,
1956 void *user_data, VEC (gdb_xml_value_s) *attributes)
1958 struct btrace_data *btrace;
1959 struct btrace_block *block;
1960 ULONGEST *begin, *end;
1962 btrace = (struct btrace_data *) user_data;
1964 switch (btrace->format)
1966 case BTRACE_FORMAT_BTS:
1969 case BTRACE_FORMAT_NONE:
1970 btrace->format = BTRACE_FORMAT_BTS;
1971 btrace->variant.bts.blocks = NULL;
1975 gdb_xml_error (parser, _("Btrace format error."));
1978 begin = (ULONGEST *) xml_find_attribute (attributes, "begin")->value;
1979 end = (ULONGEST *) xml_find_attribute (attributes, "end")->value;
1981 block = VEC_safe_push (btrace_block_s, btrace->variant.bts.blocks, NULL);
1982 block->begin = *begin;
1986 /* Parse a "raw" xml record. */
1989 parse_xml_raw (struct gdb_xml_parser *parser, const char *body_text,
1990 gdb_byte **pdata, size_t *psize)
1992 struct cleanup *cleanup;
1993 gdb_byte *data, *bin;
1996 len = strlen (body_text);
1998 gdb_xml_error (parser, _("Bad raw data size."));
2002 bin = data = (gdb_byte *) xmalloc (size);
2003 cleanup = make_cleanup (xfree, data);
2005 /* We use hex encoding - see common/rsp-low.h. */
2013 if (hi == 0 || lo == 0)
2014 gdb_xml_error (parser, _("Bad hex encoding."));
2016 *bin++ = fromhex (hi) * 16 + fromhex (lo);
2020 discard_cleanups (cleanup);
2026 /* Parse a btrace pt-config "cpu" xml record. */
2029 parse_xml_btrace_pt_config_cpu (struct gdb_xml_parser *parser,
2030 const struct gdb_xml_element *element,
2032 VEC (gdb_xml_value_s) *attributes)
2034 struct btrace_data *btrace;
2036 ULONGEST *family, *model, *stepping;
2038 vendor = (const char *) xml_find_attribute (attributes, "vendor")->value;
2039 family = (ULONGEST *) xml_find_attribute (attributes, "family")->value;
2040 model = (ULONGEST *) xml_find_attribute (attributes, "model")->value;
2041 stepping = (ULONGEST *) xml_find_attribute (attributes, "stepping")->value;
2043 btrace = (struct btrace_data *) user_data;
2045 if (strcmp (vendor, "GenuineIntel") == 0)
2046 btrace->variant.pt.config.cpu.vendor = CV_INTEL;
2048 btrace->variant.pt.config.cpu.family = *family;
2049 btrace->variant.pt.config.cpu.model = *model;
2050 btrace->variant.pt.config.cpu.stepping = *stepping;
2053 /* Parse a btrace pt "raw" xml record. */
2056 parse_xml_btrace_pt_raw (struct gdb_xml_parser *parser,
2057 const struct gdb_xml_element *element,
2058 void *user_data, const char *body_text)
2060 struct btrace_data *btrace;
2062 btrace = (struct btrace_data *) user_data;
2063 parse_xml_raw (parser, body_text, &btrace->variant.pt.data,
2064 &btrace->variant.pt.size);
2067 /* Parse a btrace "pt" xml record. */
2070 parse_xml_btrace_pt (struct gdb_xml_parser *parser,
2071 const struct gdb_xml_element *element,
2072 void *user_data, VEC (gdb_xml_value_s) *attributes)
2074 struct btrace_data *btrace;
2076 btrace = (struct btrace_data *) user_data;
2077 btrace->format = BTRACE_FORMAT_PT;
2078 btrace->variant.pt.config.cpu.vendor = CV_UNKNOWN;
2079 btrace->variant.pt.data = NULL;
2080 btrace->variant.pt.size = 0;
2083 static const struct gdb_xml_attribute block_attributes[] = {
2084 { "begin", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
2085 { "end", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
2086 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2089 static const struct gdb_xml_attribute btrace_pt_config_cpu_attributes[] = {
2090 { "vendor", GDB_XML_AF_NONE, NULL, NULL },
2091 { "family", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
2092 { "model", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
2093 { "stepping", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
2094 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2097 static const struct gdb_xml_element btrace_pt_config_children[] = {
2098 { "cpu", btrace_pt_config_cpu_attributes, NULL, GDB_XML_EF_OPTIONAL,
2099 parse_xml_btrace_pt_config_cpu, NULL },
2100 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2103 static const struct gdb_xml_element btrace_pt_children[] = {
2104 { "pt-config", NULL, btrace_pt_config_children, GDB_XML_EF_OPTIONAL, NULL,
2106 { "raw", NULL, NULL, GDB_XML_EF_OPTIONAL, NULL, parse_xml_btrace_pt_raw },
2107 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2110 static const struct gdb_xml_attribute btrace_attributes[] = {
2111 { "version", GDB_XML_AF_NONE, NULL, NULL },
2112 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2115 static const struct gdb_xml_element btrace_children[] = {
2116 { "block", block_attributes, NULL,
2117 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL, parse_xml_btrace_block, NULL },
2118 { "pt", NULL, btrace_pt_children, GDB_XML_EF_OPTIONAL, parse_xml_btrace_pt,
2120 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2123 static const struct gdb_xml_element btrace_elements[] = {
2124 { "btrace", btrace_attributes, btrace_children, GDB_XML_EF_NONE,
2125 check_xml_btrace_version, NULL },
2126 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2129 #endif /* defined (HAVE_LIBEXPAT) */
2134 parse_xml_btrace (struct btrace_data *btrace, const char *buffer)
2136 struct cleanup *cleanup;
2139 #if defined (HAVE_LIBEXPAT)
2141 btrace->format = BTRACE_FORMAT_NONE;
2143 cleanup = make_cleanup_btrace_data (btrace);
2144 errcode = gdb_xml_parse_quick (_("btrace"), "btrace.dtd", btrace_elements,
2147 error (_("Error parsing branch trace."));
2149 /* Keep parse results. */
2150 discard_cleanups (cleanup);
2152 #else /* !defined (HAVE_LIBEXPAT) */
2154 error (_("Cannot process branch trace. XML parsing is not supported."));
2156 #endif /* !defined (HAVE_LIBEXPAT) */
2159 #if defined (HAVE_LIBEXPAT)
2161 /* Parse a btrace-conf "bts" xml record. */
2164 parse_xml_btrace_conf_bts (struct gdb_xml_parser *parser,
2165 const struct gdb_xml_element *element,
2166 void *user_data, VEC (gdb_xml_value_s) *attributes)
2168 struct btrace_config *conf;
2169 struct gdb_xml_value *size;
2171 conf = (struct btrace_config *) user_data;
2172 conf->format = BTRACE_FORMAT_BTS;
2175 size = xml_find_attribute (attributes, "size");
2177 conf->bts.size = (unsigned int) *(ULONGEST *) size->value;
2180 /* Parse a btrace-conf "pt" xml record. */
2183 parse_xml_btrace_conf_pt (struct gdb_xml_parser *parser,
2184 const struct gdb_xml_element *element,
2185 void *user_data, VEC (gdb_xml_value_s) *attributes)
2187 struct btrace_config *conf;
2188 struct gdb_xml_value *size;
2190 conf = (struct btrace_config *) user_data;
2191 conf->format = BTRACE_FORMAT_PT;
2194 size = xml_find_attribute (attributes, "size");
2196 conf->pt.size = (unsigned int) *(ULONGEST *) size->value;
2199 static const struct gdb_xml_attribute btrace_conf_pt_attributes[] = {
2200 { "size", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
2201 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2204 static const struct gdb_xml_attribute btrace_conf_bts_attributes[] = {
2205 { "size", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
2206 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2209 static const struct gdb_xml_element btrace_conf_children[] = {
2210 { "bts", btrace_conf_bts_attributes, NULL, GDB_XML_EF_OPTIONAL,
2211 parse_xml_btrace_conf_bts, NULL },
2212 { "pt", btrace_conf_pt_attributes, NULL, GDB_XML_EF_OPTIONAL,
2213 parse_xml_btrace_conf_pt, NULL },
2214 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2217 static const struct gdb_xml_attribute btrace_conf_attributes[] = {
2218 { "version", GDB_XML_AF_NONE, NULL, NULL },
2219 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2222 static const struct gdb_xml_element btrace_conf_elements[] = {
2223 { "btrace-conf", btrace_conf_attributes, btrace_conf_children,
2224 GDB_XML_EF_NONE, NULL, NULL },
2225 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2228 #endif /* defined (HAVE_LIBEXPAT) */
2233 parse_xml_btrace_conf (struct btrace_config *conf, const char *xml)
2237 #if defined (HAVE_LIBEXPAT)
2239 errcode = gdb_xml_parse_quick (_("btrace-conf"), "btrace-conf.dtd",
2240 btrace_conf_elements, xml, conf);
2242 error (_("Error parsing branch trace configuration."));
2244 #else /* !defined (HAVE_LIBEXPAT) */
2246 error (_("XML parsing is not supported."));
2248 #endif /* !defined (HAVE_LIBEXPAT) */
2253 const struct btrace_insn *
2254 btrace_insn_get (const struct btrace_insn_iterator *it)
2256 const struct btrace_function *bfun;
2257 unsigned int index, end;
2259 index = it->insn_index;
2260 bfun = &it->btinfo->functions[it->call_index];
2262 /* Check if the iterator points to a gap in the trace. */
2263 if (bfun->errcode != 0)
2266 /* The index is within the bounds of this function's instruction vector. */
2267 end = VEC_length (btrace_insn_s, bfun->insn);
2268 gdb_assert (0 < end);
2269 gdb_assert (index < end);
2271 return VEC_index (btrace_insn_s, bfun->insn, index);
2277 btrace_insn_get_error (const struct btrace_insn_iterator *it)
2279 return it->btinfo->functions[it->call_index].errcode;
2285 btrace_insn_number (const struct btrace_insn_iterator *it)
2287 return it->btinfo->functions[it->call_index].insn_offset + it->insn_index;
2293 btrace_insn_begin (struct btrace_insn_iterator *it,
2294 const struct btrace_thread_info *btinfo)
2296 if (btinfo->functions.empty ())
2297 error (_("No trace."));
2299 it->btinfo = btinfo;
2307 btrace_insn_end (struct btrace_insn_iterator *it,
2308 const struct btrace_thread_info *btinfo)
2310 const struct btrace_function *bfun;
2311 unsigned int length;
2313 if (btinfo->functions.empty ())
2314 error (_("No trace."));
2316 bfun = &btinfo->functions.back ();
2317 length = VEC_length (btrace_insn_s, bfun->insn);
2319 /* The last function may either be a gap or it contains the current
2320 instruction, which is one past the end of the execution trace; ignore
2325 it->btinfo = btinfo;
2326 it->call_index = bfun->number - 1;
2327 it->insn_index = length;
2333 btrace_insn_next (struct btrace_insn_iterator *it, unsigned int stride)
2335 const struct btrace_function *bfun;
2336 unsigned int index, steps;
2338 bfun = &it->btinfo->functions[it->call_index];
2340 index = it->insn_index;
2344 unsigned int end, space, adv;
2346 end = VEC_length (btrace_insn_s, bfun->insn);
2348 /* An empty function segment represents a gap in the trace. We count
2349 it as one instruction. */
2352 const struct btrace_function *next;
2354 next = ftrace_find_call_by_number (it->btinfo, bfun->number + 1);
2367 gdb_assert (0 < end);
2368 gdb_assert (index < end);
2370 /* Compute the number of instructions remaining in this segment. */
2371 space = end - index;
2373 /* Advance the iterator as far as possible within this segment. */
2374 adv = std::min (space, stride);
2379 /* Move to the next function if we're at the end of this one. */
2382 const struct btrace_function *next;
2384 next = ftrace_find_call_by_number (it->btinfo, bfun->number + 1);
2387 /* We stepped past the last function.
2389 Let's adjust the index to point to the last instruction in
2390 the previous function. */
2396 /* We now point to the first instruction in the new function. */
2401 /* We did make progress. */
2402 gdb_assert (adv > 0);
2405 /* Update the iterator. */
2406 it->call_index = bfun->number - 1;
2407 it->insn_index = index;
2415 btrace_insn_prev (struct btrace_insn_iterator *it, unsigned int stride)
2417 const struct btrace_function *bfun;
2418 unsigned int index, steps;
2420 bfun = &it->btinfo->functions[it->call_index];
2422 index = it->insn_index;
2428 /* Move to the previous function if we're at the start of this one. */
2431 const struct btrace_function *prev;
2433 prev = ftrace_find_call_by_number (it->btinfo, bfun->number - 1);
2437 /* We point to one after the last instruction in the new function. */
2439 index = VEC_length (btrace_insn_s, bfun->insn);
2441 /* An empty function segment represents a gap in the trace. We count
2442 it as one instruction. */
2452 /* Advance the iterator as far as possible within this segment. */
2453 adv = std::min (index, stride);
2459 /* We did make progress. */
2460 gdb_assert (adv > 0);
2463 /* Update the iterator. */
2464 it->call_index = bfun->number - 1;
2465 it->insn_index = index;
2473 btrace_insn_cmp (const struct btrace_insn_iterator *lhs,
2474 const struct btrace_insn_iterator *rhs)
2476 gdb_assert (lhs->btinfo == rhs->btinfo);
2478 if (lhs->call_index != rhs->call_index)
2479 return lhs->call_index - rhs->call_index;
2481 return lhs->insn_index - rhs->insn_index;
2487 btrace_find_insn_by_number (struct btrace_insn_iterator *it,
2488 const struct btrace_thread_info *btinfo,
2489 unsigned int number)
2491 const struct btrace_function *bfun;
2492 unsigned int upper, lower;
2494 if (btinfo->functions.empty ())
2498 bfun = &btinfo->functions[lower];
2499 if (number < bfun->insn_offset)
2502 upper = btinfo->functions.size () - 1;
2503 bfun = &btinfo->functions[upper];
2504 if (number >= bfun->insn_offset + ftrace_call_num_insn (bfun))
2507 /* We assume that there are no holes in the numbering. */
2510 const unsigned int average = lower + (upper - lower) / 2;
2512 bfun = &btinfo->functions[average];
2514 if (number < bfun->insn_offset)
2516 upper = average - 1;
2520 if (number >= bfun->insn_offset + ftrace_call_num_insn (bfun))
2522 lower = average + 1;
2529 it->btinfo = btinfo;
2530 it->call_index = bfun->number - 1;
2531 it->insn_index = number - bfun->insn_offset;
2535 /* Returns true if the recording ends with a function segment that
2536 contains only a single (i.e. the current) instruction. */
2539 btrace_ends_with_single_insn (const struct btrace_thread_info *btinfo)
2541 const btrace_function *bfun;
2543 if (btinfo->functions.empty ())
2546 bfun = &btinfo->functions.back ();
2547 if (bfun->errcode != 0)
2550 return ftrace_call_num_insn (bfun) == 1;
2555 const struct btrace_function *
2556 btrace_call_get (const struct btrace_call_iterator *it)
2558 if (it->index >= it->btinfo->functions.size ())
2561 return &it->btinfo->functions[it->index];
2567 btrace_call_number (const struct btrace_call_iterator *it)
2569 const unsigned int length = it->btinfo->functions.size ();
2571 /* If the last function segment contains only a single instruction (i.e. the
2572 current instruction), skip it. */
2573 if ((it->index == length) && btrace_ends_with_single_insn (it->btinfo))
2576 return it->index + 1;
2582 btrace_call_begin (struct btrace_call_iterator *it,
2583 const struct btrace_thread_info *btinfo)
2585 if (btinfo->functions.empty ())
2586 error (_("No trace."));
2588 it->btinfo = btinfo;
2595 btrace_call_end (struct btrace_call_iterator *it,
2596 const struct btrace_thread_info *btinfo)
2598 if (btinfo->functions.empty ())
2599 error (_("No trace."));
2601 it->btinfo = btinfo;
2602 it->index = btinfo->functions.size ();
2608 btrace_call_next (struct btrace_call_iterator *it, unsigned int stride)
2610 const unsigned int length = it->btinfo->functions.size ();
2612 if (it->index + stride < length - 1)
2613 /* Default case: Simply advance the iterator. */
2614 it->index += stride;
2615 else if (it->index + stride == length - 1)
2617 /* We land exactly at the last function segment. If it contains only one
2618 instruction (i.e. the current instruction) it is not actually part of
2620 if (btrace_ends_with_single_insn (it->btinfo))
2623 it->index = length - 1;
2627 /* We land past the last function segment and have to adjust the stride.
2628 If the last function segment contains only one instruction (i.e. the
2629 current instruction) it is not actually part of the trace. */
2630 if (btrace_ends_with_single_insn (it->btinfo))
2631 stride = length - it->index - 1;
2633 stride = length - it->index;
2644 btrace_call_prev (struct btrace_call_iterator *it, unsigned int stride)
2646 const unsigned int length = it->btinfo->functions.size ();
2649 gdb_assert (it->index <= length);
2651 if (stride == 0 || it->index == 0)
2654 /* If we are at the end, the first step is a special case. If the last
2655 function segment contains only one instruction (i.e. the current
2656 instruction) it is not actually part of the trace. To be able to step
2657 over this instruction, we need at least one more function segment. */
2658 if ((it->index == length) && (length > 1))
2660 if (btrace_ends_with_single_insn (it->btinfo))
2661 it->index = length - 2;
2663 it->index = length - 1;
2669 stride = std::min (stride, it->index);
2671 it->index -= stride;
2672 return steps + stride;
2678 btrace_call_cmp (const struct btrace_call_iterator *lhs,
2679 const struct btrace_call_iterator *rhs)
2681 gdb_assert (lhs->btinfo == rhs->btinfo);
2682 return (int) (lhs->index - rhs->index);
2688 btrace_find_call_by_number (struct btrace_call_iterator *it,
2689 const struct btrace_thread_info *btinfo,
2690 unsigned int number)
2692 const unsigned int length = btinfo->functions.size ();
2694 if ((number == 0) || (number > length))
2697 it->btinfo = btinfo;
2698 it->index = number - 1;
2705 btrace_set_insn_history (struct btrace_thread_info *btinfo,
2706 const struct btrace_insn_iterator *begin,
2707 const struct btrace_insn_iterator *end)
2709 if (btinfo->insn_history == NULL)
2710 btinfo->insn_history = XCNEW (struct btrace_insn_history);
2712 btinfo->insn_history->begin = *begin;
2713 btinfo->insn_history->end = *end;
2719 btrace_set_call_history (struct btrace_thread_info *btinfo,
2720 const struct btrace_call_iterator *begin,
2721 const struct btrace_call_iterator *end)
2723 gdb_assert (begin->btinfo == end->btinfo);
2725 if (btinfo->call_history == NULL)
2726 btinfo->call_history = XCNEW (struct btrace_call_history);
2728 btinfo->call_history->begin = *begin;
2729 btinfo->call_history->end = *end;
2735 btrace_is_replaying (struct thread_info *tp)
2737 return tp->btrace.replay != NULL;
2743 btrace_is_empty (struct thread_info *tp)
2745 struct btrace_insn_iterator begin, end;
2746 struct btrace_thread_info *btinfo;
2748 btinfo = &tp->btrace;
2750 if (btinfo->functions.empty ())
2753 btrace_insn_begin (&begin, btinfo);
2754 btrace_insn_end (&end, btinfo);
2756 return btrace_insn_cmp (&begin, &end) == 0;
2759 /* Forward the cleanup request. */
2762 do_btrace_data_cleanup (void *arg)
2764 btrace_data_fini ((struct btrace_data *) arg);
2770 make_cleanup_btrace_data (struct btrace_data *data)
2772 return make_cleanup (do_btrace_data_cleanup, data);
2775 #if defined (HAVE_LIBIPT)
2777 /* Print a single packet. */
2780 pt_print_packet (const struct pt_packet *packet)
2782 switch (packet->type)
2785 printf_unfiltered (("[??: %x]"), packet->type);
2789 printf_unfiltered (("psb"));
2793 printf_unfiltered (("psbend"));
2797 printf_unfiltered (("pad"));
2801 printf_unfiltered (("tip %u: 0x%" PRIx64 ""),
2802 packet->payload.ip.ipc,
2803 packet->payload.ip.ip);
2807 printf_unfiltered (("tip.pge %u: 0x%" PRIx64 ""),
2808 packet->payload.ip.ipc,
2809 packet->payload.ip.ip);
2813 printf_unfiltered (("tip.pgd %u: 0x%" PRIx64 ""),
2814 packet->payload.ip.ipc,
2815 packet->payload.ip.ip);
2819 printf_unfiltered (("fup %u: 0x%" PRIx64 ""),
2820 packet->payload.ip.ipc,
2821 packet->payload.ip.ip);
2825 printf_unfiltered (("tnt-8 %u: 0x%" PRIx64 ""),
2826 packet->payload.tnt.bit_size,
2827 packet->payload.tnt.payload);
2831 printf_unfiltered (("tnt-64 %u: 0x%" PRIx64 ""),
2832 packet->payload.tnt.bit_size,
2833 packet->payload.tnt.payload);
2837 printf_unfiltered (("pip %" PRIx64 "%s"), packet->payload.pip.cr3,
2838 packet->payload.pip.nr ? (" nr") : (""));
2842 printf_unfiltered (("tsc %" PRIx64 ""), packet->payload.tsc.tsc);
2846 printf_unfiltered (("cbr %u"), packet->payload.cbr.ratio);
2850 switch (packet->payload.mode.leaf)
2853 printf_unfiltered (("mode %u"), packet->payload.mode.leaf);
2857 printf_unfiltered (("mode.exec%s%s"),
2858 packet->payload.mode.bits.exec.csl
2860 packet->payload.mode.bits.exec.csd
2861 ? (" cs.d") : (""));
2865 printf_unfiltered (("mode.tsx%s%s"),
2866 packet->payload.mode.bits.tsx.intx
2868 packet->payload.mode.bits.tsx.abrt
2869 ? (" abrt") : (""));
2875 printf_unfiltered (("ovf"));
2879 printf_unfiltered (("stop"));
2883 printf_unfiltered (("vmcs %" PRIx64 ""), packet->payload.vmcs.base);
2887 printf_unfiltered (("tma %x %x"), packet->payload.tma.ctc,
2888 packet->payload.tma.fc);
2892 printf_unfiltered (("mtc %x"), packet->payload.mtc.ctc);
2896 printf_unfiltered (("cyc %" PRIx64 ""), packet->payload.cyc.value);
2900 printf_unfiltered (("mnt %" PRIx64 ""), packet->payload.mnt.payload);
2905 /* Decode packets into MAINT using DECODER. */
2908 btrace_maint_decode_pt (struct btrace_maint_info *maint,
2909 struct pt_packet_decoder *decoder)
2915 struct btrace_pt_packet packet;
2917 errcode = pt_pkt_sync_forward (decoder);
2923 pt_pkt_get_offset (decoder, &packet.offset);
2925 errcode = pt_pkt_next (decoder, &packet.packet,
2926 sizeof(packet.packet));
2930 if (maint_btrace_pt_skip_pad == 0 || packet.packet.type != ppt_pad)
2932 packet.errcode = pt_errcode (errcode);
2933 VEC_safe_push (btrace_pt_packet_s, maint->variant.pt.packets,
2938 if (errcode == -pte_eos)
2941 packet.errcode = pt_errcode (errcode);
2942 VEC_safe_push (btrace_pt_packet_s, maint->variant.pt.packets,
2945 warning (_("Error at trace offset 0x%" PRIx64 ": %s."),
2946 packet.offset, pt_errstr (packet.errcode));
2949 if (errcode != -pte_eos)
2950 warning (_("Failed to synchronize onto the Intel Processor Trace "
2951 "stream: %s."), pt_errstr (pt_errcode (errcode)));
2954 /* Update the packet history in BTINFO. */
2957 btrace_maint_update_pt_packets (struct btrace_thread_info *btinfo)
2959 volatile struct gdb_exception except;
2960 struct pt_packet_decoder *decoder;
2961 struct btrace_data_pt *pt;
2962 struct pt_config config;
2965 pt = &btinfo->data.variant.pt;
2967 /* Nothing to do if there is no trace. */
2971 memset (&config, 0, sizeof(config));
2973 config.size = sizeof (config);
2974 config.begin = pt->data;
2975 config.end = pt->data + pt->size;
2977 config.cpu.vendor = pt_translate_cpu_vendor (pt->config.cpu.vendor);
2978 config.cpu.family = pt->config.cpu.family;
2979 config.cpu.model = pt->config.cpu.model;
2980 config.cpu.stepping = pt->config.cpu.stepping;
2982 errcode = pt_cpu_errata (&config.errata, &config.cpu);
2984 error (_("Failed to configure the Intel Processor Trace decoder: %s."),
2985 pt_errstr (pt_errcode (errcode)));
2987 decoder = pt_pkt_alloc_decoder (&config);
2988 if (decoder == NULL)
2989 error (_("Failed to allocate the Intel Processor Trace decoder."));
2993 btrace_maint_decode_pt (&btinfo->maint, decoder);
2995 CATCH (except, RETURN_MASK_ALL)
2997 pt_pkt_free_decoder (decoder);
2999 if (except.reason < 0)
3000 throw_exception (except);
3004 pt_pkt_free_decoder (decoder);
3007 #endif /* !defined (HAVE_LIBIPT) */
3009 /* Update the packet maintenance information for BTINFO and store the
3010 low and high bounds into BEGIN and END, respectively.
3011 Store the current iterator state into FROM and TO. */
3014 btrace_maint_update_packets (struct btrace_thread_info *btinfo,
3015 unsigned int *begin, unsigned int *end,
3016 unsigned int *from, unsigned int *to)
3018 switch (btinfo->data.format)
3027 case BTRACE_FORMAT_BTS:
3028 /* Nothing to do - we operate directly on BTINFO->DATA. */
3030 *end = VEC_length (btrace_block_s, btinfo->data.variant.bts.blocks);
3031 *from = btinfo->maint.variant.bts.packet_history.begin;
3032 *to = btinfo->maint.variant.bts.packet_history.end;
3035 #if defined (HAVE_LIBIPT)
3036 case BTRACE_FORMAT_PT:
3037 if (VEC_empty (btrace_pt_packet_s, btinfo->maint.variant.pt.packets))
3038 btrace_maint_update_pt_packets (btinfo);
3041 *end = VEC_length (btrace_pt_packet_s, btinfo->maint.variant.pt.packets);
3042 *from = btinfo->maint.variant.pt.packet_history.begin;
3043 *to = btinfo->maint.variant.pt.packet_history.end;
3045 #endif /* defined (HAVE_LIBIPT) */
3049 /* Print packets in BTINFO from BEGIN (inclusive) until END (exclusive) and
3050 update the current iterator position. */
3053 btrace_maint_print_packets (struct btrace_thread_info *btinfo,
3054 unsigned int begin, unsigned int end)
3056 switch (btinfo->data.format)
3061 case BTRACE_FORMAT_BTS:
3063 VEC (btrace_block_s) *blocks;
3066 blocks = btinfo->data.variant.bts.blocks;
3067 for (blk = begin; blk < end; ++blk)
3069 const btrace_block_s *block;
3071 block = VEC_index (btrace_block_s, blocks, blk);
3073 printf_unfiltered ("%u\tbegin: %s, end: %s\n", blk,
3074 core_addr_to_string_nz (block->begin),
3075 core_addr_to_string_nz (block->end));
3078 btinfo->maint.variant.bts.packet_history.begin = begin;
3079 btinfo->maint.variant.bts.packet_history.end = end;
3083 #if defined (HAVE_LIBIPT)
3084 case BTRACE_FORMAT_PT:
3086 VEC (btrace_pt_packet_s) *packets;
3089 packets = btinfo->maint.variant.pt.packets;
3090 for (pkt = begin; pkt < end; ++pkt)
3092 const struct btrace_pt_packet *packet;
3094 packet = VEC_index (btrace_pt_packet_s, packets, pkt);
3096 printf_unfiltered ("%u\t", pkt);
3097 printf_unfiltered ("0x%" PRIx64 "\t", packet->offset);
3099 if (packet->errcode == pte_ok)
3100 pt_print_packet (&packet->packet);
3102 printf_unfiltered ("[error: %s]", pt_errstr (packet->errcode));
3104 printf_unfiltered ("\n");
3107 btinfo->maint.variant.pt.packet_history.begin = begin;
3108 btinfo->maint.variant.pt.packet_history.end = end;
3111 #endif /* defined (HAVE_LIBIPT) */
3115 /* Read a number from an argument string. */
3118 get_uint (char **arg)
3120 char *begin, *end, *pos;
3121 unsigned long number;
3124 pos = skip_spaces (begin);
3126 if (!isdigit (*pos))
3127 error (_("Expected positive number, got: %s."), pos);
3129 number = strtoul (pos, &end, 10);
3130 if (number > UINT_MAX)
3131 error (_("Number too big."));
3133 *arg += (end - begin);
3135 return (unsigned int) number;
3138 /* Read a context size from an argument string. */
3141 get_context_size (char **arg)
3146 pos = skip_spaces (*arg);
3148 if (!isdigit (*pos))
3149 error (_("Expected positive number, got: %s."), pos);
3151 return strtol (pos, arg, 10);
3154 /* Complain about junk at the end of an argument string. */
3157 no_chunk (char *arg)
3160 error (_("Junk after argument: %s."), arg);
3163 /* The "maintenance btrace packet-history" command. */
3166 maint_btrace_packet_history_cmd (char *arg, int from_tty)
3168 struct btrace_thread_info *btinfo;
3169 struct thread_info *tp;
3170 unsigned int size, begin, end, from, to;
3172 tp = find_thread_ptid (inferior_ptid);
3174 error (_("No thread."));
3177 btinfo = &tp->btrace;
3179 btrace_maint_update_packets (btinfo, &begin, &end, &from, &to);
3182 printf_unfiltered (_("No trace.\n"));
3186 if (arg == NULL || *arg == 0 || strcmp (arg, "+") == 0)
3190 if (end - from < size)
3194 else if (strcmp (arg, "-") == 0)
3198 if (to - begin < size)
3204 from = get_uint (&arg);
3206 error (_("'%u' is out of range."), from);
3208 arg = skip_spaces (arg);
3211 arg = skip_spaces (++arg);
3216 size = get_context_size (&arg);
3220 if (end - from < size)
3224 else if (*arg == '-')
3227 size = get_context_size (&arg);
3231 /* Include the packet given as first argument. */
3235 if (to - begin < size)
3241 to = get_uint (&arg);
3243 /* Include the packet at the second argument and silently
3244 truncate the range. */
3257 if (end - from < size)
3265 btrace_maint_print_packets (btinfo, from, to);
3268 /* The "maintenance btrace clear-packet-history" command. */
3271 maint_btrace_clear_packet_history_cmd (char *args, int from_tty)
3273 struct btrace_thread_info *btinfo;
3274 struct thread_info *tp;
3276 if (args != NULL && *args != 0)
3277 error (_("Invalid argument."));
3279 tp = find_thread_ptid (inferior_ptid);
3281 error (_("No thread."));
3283 btinfo = &tp->btrace;
3285 /* Must clear the maint data before - it depends on BTINFO->DATA. */
3286 btrace_maint_clear (btinfo);
3287 btrace_data_clear (&btinfo->data);
3290 /* The "maintenance btrace clear" command. */
3293 maint_btrace_clear_cmd (char *args, int from_tty)
3295 struct btrace_thread_info *btinfo;
3296 struct thread_info *tp;
3298 if (args != NULL && *args != 0)
3299 error (_("Invalid argument."));
3301 tp = find_thread_ptid (inferior_ptid);
3303 error (_("No thread."));
3308 /* The "maintenance btrace" command. */
3311 maint_btrace_cmd (char *args, int from_tty)
3313 help_list (maint_btrace_cmdlist, "maintenance btrace ", all_commands,
3317 /* The "maintenance set btrace" command. */
3320 maint_btrace_set_cmd (char *args, int from_tty)
3322 help_list (maint_btrace_set_cmdlist, "maintenance set btrace ", all_commands,
3326 /* The "maintenance show btrace" command. */
3329 maint_btrace_show_cmd (char *args, int from_tty)
3331 help_list (maint_btrace_show_cmdlist, "maintenance show btrace ",
3332 all_commands, gdb_stdout);
3335 /* The "maintenance set btrace pt" command. */
3338 maint_btrace_pt_set_cmd (char *args, int from_tty)
3340 help_list (maint_btrace_pt_set_cmdlist, "maintenance set btrace pt ",
3341 all_commands, gdb_stdout);
3344 /* The "maintenance show btrace pt" command. */
3347 maint_btrace_pt_show_cmd (char *args, int from_tty)
3349 help_list (maint_btrace_pt_show_cmdlist, "maintenance show btrace pt ",
3350 all_commands, gdb_stdout);
3353 /* The "maintenance info btrace" command. */
3356 maint_info_btrace_cmd (char *args, int from_tty)
3358 struct btrace_thread_info *btinfo;
3359 struct thread_info *tp;
3360 const struct btrace_config *conf;
3362 if (args != NULL && *args != 0)
3363 error (_("Invalid argument."));
3365 tp = find_thread_ptid (inferior_ptid);
3367 error (_("No thread."));
3369 btinfo = &tp->btrace;
3371 conf = btrace_conf (btinfo);
3373 error (_("No btrace configuration."));
3375 printf_unfiltered (_("Format: %s.\n"),
3376 btrace_format_string (conf->format));
3378 switch (conf->format)
3383 case BTRACE_FORMAT_BTS:
3384 printf_unfiltered (_("Number of packets: %u.\n"),
3385 VEC_length (btrace_block_s,
3386 btinfo->data.variant.bts.blocks));
3389 #if defined (HAVE_LIBIPT)
3390 case BTRACE_FORMAT_PT:
3392 struct pt_version version;
3394 version = pt_library_version ();
3395 printf_unfiltered (_("Version: %u.%u.%u%s.\n"), version.major,
3396 version.minor, version.build,
3397 version.ext != NULL ? version.ext : "");
3399 btrace_maint_update_pt_packets (btinfo);
3400 printf_unfiltered (_("Number of packets: %u.\n"),
3401 VEC_length (btrace_pt_packet_s,
3402 btinfo->maint.variant.pt.packets));
3405 #endif /* defined (HAVE_LIBIPT) */
3409 /* The "maint show btrace pt skip-pad" show value function. */
3412 show_maint_btrace_pt_skip_pad (struct ui_file *file, int from_tty,
3413 struct cmd_list_element *c,
3416 fprintf_filtered (file, _("Skip PAD packets is %s.\n"), value);
3420 /* Initialize btrace maintenance commands. */
3422 void _initialize_btrace (void);
3424 _initialize_btrace (void)
3426 add_cmd ("btrace", class_maintenance, maint_info_btrace_cmd,
3427 _("Info about branch tracing data."), &maintenanceinfolist);
3429 add_prefix_cmd ("btrace", class_maintenance, maint_btrace_cmd,
3430 _("Branch tracing maintenance commands."),
3431 &maint_btrace_cmdlist, "maintenance btrace ",
3432 0, &maintenancelist);
3434 add_prefix_cmd ("btrace", class_maintenance, maint_btrace_set_cmd, _("\
3435 Set branch tracing specific variables."),
3436 &maint_btrace_set_cmdlist, "maintenance set btrace ",
3437 0, &maintenance_set_cmdlist);
3439 add_prefix_cmd ("pt", class_maintenance, maint_btrace_pt_set_cmd, _("\
3440 Set Intel Processor Trace specific variables."),
3441 &maint_btrace_pt_set_cmdlist, "maintenance set btrace pt ",
3442 0, &maint_btrace_set_cmdlist);
3444 add_prefix_cmd ("btrace", class_maintenance, maint_btrace_show_cmd, _("\
3445 Show branch tracing specific variables."),
3446 &maint_btrace_show_cmdlist, "maintenance show btrace ",
3447 0, &maintenance_show_cmdlist);
3449 add_prefix_cmd ("pt", class_maintenance, maint_btrace_pt_show_cmd, _("\
3450 Show Intel Processor Trace specific variables."),
3451 &maint_btrace_pt_show_cmdlist, "maintenance show btrace pt ",
3452 0, &maint_btrace_show_cmdlist);
3454 add_setshow_boolean_cmd ("skip-pad", class_maintenance,
3455 &maint_btrace_pt_skip_pad, _("\
3456 Set whether PAD packets should be skipped in the btrace packet history."), _("\
3457 Show whether PAD packets should be skipped in the btrace packet history."),_("\
3458 When enabled, PAD packets are ignored in the btrace packet history."),
3459 NULL, show_maint_btrace_pt_skip_pad,
3460 &maint_btrace_pt_set_cmdlist,
3461 &maint_btrace_pt_show_cmdlist);
3463 add_cmd ("packet-history", class_maintenance, maint_btrace_packet_history_cmd,
3464 _("Print the raw branch tracing data.\n\
3465 With no argument, print ten more packets after the previous ten-line print.\n\
3466 With '-' as argument print ten packets before a previous ten-line print.\n\
3467 One argument specifies the starting packet of a ten-line print.\n\
3468 Two arguments with comma between specify starting and ending packets to \
3470 Preceded with '+'/'-' the second argument specifies the distance from the \
3472 &maint_btrace_cmdlist);
3474 add_cmd ("clear-packet-history", class_maintenance,
3475 maint_btrace_clear_packet_history_cmd,
3476 _("Clears the branch tracing packet history.\n\
3477 Discards the raw branch tracing data but not the execution history data.\n\
3479 &maint_btrace_cmdlist);
3481 add_cmd ("clear", class_maintenance, maint_btrace_clear_cmd,
3482 _("Clears the branch tracing data.\n\
3483 Discards the raw branch tracing data and the execution history data.\n\
3484 The next 'record' command will fetch the branch tracing data anew.\n\
3486 &maint_btrace_cmdlist);