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 the function segment with the given NUMBER or NULL if no such segment
160 exists. BTINFO is the branch trace information for the current thread. */
162 static struct btrace_function *
163 ftrace_find_call_by_number (const struct btrace_thread_info *btinfo,
166 if (number == 0 || number > btinfo->functions.size ())
169 return btinfo->functions[number - 1];
172 /* Return non-zero if BFUN does not match MFUN and FUN,
173 return zero otherwise. */
176 ftrace_function_switched (const struct btrace_function *bfun,
177 const struct minimal_symbol *mfun,
178 const struct symbol *fun)
180 struct minimal_symbol *msym;
186 /* If the minimal symbol changed, we certainly switched functions. */
187 if (mfun != NULL && msym != NULL
188 && strcmp (MSYMBOL_LINKAGE_NAME (mfun), MSYMBOL_LINKAGE_NAME (msym)) != 0)
191 /* If the symbol changed, we certainly switched functions. */
192 if (fun != NULL && sym != NULL)
194 const char *bfname, *fname;
196 /* Check the function name. */
197 if (strcmp (SYMBOL_LINKAGE_NAME (fun), SYMBOL_LINKAGE_NAME (sym)) != 0)
200 /* Check the location of those functions, as well. */
201 bfname = symtab_to_fullname (symbol_symtab (sym));
202 fname = symtab_to_fullname (symbol_symtab (fun));
203 if (filename_cmp (fname, bfname) != 0)
207 /* If we lost symbol information, we switched functions. */
208 if (!(msym == NULL && sym == NULL) && mfun == NULL && fun == NULL)
211 /* If we gained symbol information, we switched functions. */
212 if (msym == NULL && sym == NULL && !(mfun == NULL && fun == NULL))
218 /* Allocate and initialize a new branch trace function segment at the end of
220 BTINFO is the branch trace information for the current thread.
221 MFUN and FUN are the symbol information we have for this function. */
223 static struct btrace_function *
224 ftrace_new_function (struct btrace_thread_info *btinfo,
225 struct minimal_symbol *mfun,
228 struct btrace_function *bfun;
230 bfun = XCNEW (struct btrace_function);
235 if (btinfo->functions.empty ())
237 /* Start counting at one. */
239 bfun->insn_offset = 1;
243 struct btrace_function *prev = btinfo->functions.back ();
245 bfun->number = prev->number + 1;
246 bfun->insn_offset = prev->insn_offset + ftrace_call_num_insn (prev);
247 bfun->level = prev->level;
250 btinfo->functions.push_back (bfun);
254 /* Update the UP field of a function segment. */
257 ftrace_update_caller (struct btrace_function *bfun,
258 struct btrace_function *caller,
259 enum btrace_function_flag flags)
262 ftrace_debug (bfun, "updating caller");
264 bfun->up = caller->number;
267 ftrace_debug (bfun, "set caller");
268 ftrace_debug (caller, "..to");
271 /* Fix up the caller for all segments of a function. */
274 ftrace_fixup_caller (struct btrace_thread_info *btinfo,
275 struct btrace_function *bfun,
276 struct btrace_function *caller,
277 enum btrace_function_flag flags)
279 unsigned int prev, next;
283 ftrace_update_caller (bfun, caller, flags);
285 /* Update all function segments belonging to the same function. */
286 for (; prev != 0; prev = bfun->prev)
288 bfun = ftrace_find_call_by_number (btinfo, prev);
289 ftrace_update_caller (bfun, caller, flags);
292 for (; next != 0; next = bfun->next)
294 bfun = ftrace_find_call_by_number (btinfo, next);
295 ftrace_update_caller (bfun, caller, flags);
299 /* Add a new function segment for a call at the end of the trace.
300 BTINFO is the branch trace information for the current thread.
301 MFUN and FUN are the symbol information we have for this function. */
303 static struct btrace_function *
304 ftrace_new_call (struct btrace_thread_info *btinfo,
305 struct minimal_symbol *mfun,
308 const unsigned int length = btinfo->functions.size ();
309 struct btrace_function *bfun = ftrace_new_function (btinfo, mfun, fun);
314 ftrace_debug (bfun, "new call");
319 /* Add a new function segment for a tail call at the end of the trace.
320 BTINFO is the branch trace information for the current thread.
321 MFUN and FUN are the symbol information we have for this function. */
323 static struct btrace_function *
324 ftrace_new_tailcall (struct btrace_thread_info *btinfo,
325 struct minimal_symbol *mfun,
328 const unsigned int length = btinfo->functions.size ();
329 struct btrace_function *bfun = ftrace_new_function (btinfo, mfun, fun);
333 bfun->flags |= BFUN_UP_LINKS_TO_TAILCALL;
335 ftrace_debug (bfun, "new tail call");
340 /* Return the caller of BFUN or NULL if there is none. This function skips
341 tail calls in the call chain. BTINFO is the branch trace information for
342 the current thread. */
343 static struct btrace_function *
344 ftrace_get_caller (struct btrace_thread_info *btinfo,
345 struct btrace_function *bfun)
347 for (; bfun != NULL; bfun = ftrace_find_call_by_number (btinfo, bfun->up))
348 if ((bfun->flags & BFUN_UP_LINKS_TO_TAILCALL) == 0)
349 return ftrace_find_call_by_number (btinfo, bfun->up);
354 /* Find the innermost caller in the back trace of BFUN with MFUN/FUN
355 symbol information. BTINFO is the branch trace information for the current
358 static struct btrace_function *
359 ftrace_find_caller (struct btrace_thread_info *btinfo,
360 struct btrace_function *bfun,
361 struct minimal_symbol *mfun,
364 for (; bfun != NULL; bfun = ftrace_find_call_by_number (btinfo, bfun->up))
366 /* Skip functions with incompatible symbol information. */
367 if (ftrace_function_switched (bfun, mfun, fun))
370 /* This is the function segment we're looking for. */
377 /* Find the innermost caller in the back trace of BFUN, skipping all
378 function segments that do not end with a call instruction (e.g.
379 tail calls ending with a jump). BTINFO is the branch trace information for
380 the current thread. */
382 static struct btrace_function *
383 ftrace_find_call (struct btrace_thread_info *btinfo,
384 struct btrace_function *bfun)
386 for (; bfun != NULL; bfun = ftrace_find_call_by_number (btinfo, bfun->up))
388 struct btrace_insn *last;
391 if (bfun->errcode != 0)
394 last = VEC_last (btrace_insn_s, bfun->insn);
396 if (last->iclass == BTRACE_INSN_CALL)
403 /* Add a continuation segment for a function into which we return at the end of
405 BTINFO is the branch trace information for the current thread.
406 MFUN and FUN are the symbol information we have for this function. */
408 static struct btrace_function *
409 ftrace_new_return (struct btrace_thread_info *btinfo,
410 struct minimal_symbol *mfun,
413 struct btrace_function *prev = btinfo->functions.back ();
414 struct btrace_function *bfun, *caller;
416 bfun = ftrace_new_function (btinfo, mfun, fun);
418 /* It is important to start at PREV's caller. Otherwise, we might find
419 PREV itself, if PREV is a recursive function. */
420 caller = ftrace_find_call_by_number (btinfo, prev->up);
421 caller = ftrace_find_caller (btinfo, caller, mfun, fun);
424 /* The caller of PREV is the preceding btrace function segment in this
425 function instance. */
426 gdb_assert (caller->next == 0);
428 caller->next = bfun->number;
429 bfun->prev = caller->number;
431 /* Maintain the function level. */
432 bfun->level = caller->level;
434 /* Maintain the call stack. */
435 bfun->up = caller->up;
436 bfun->flags = caller->flags;
438 ftrace_debug (bfun, "new return");
442 /* We did not find a caller. This could mean that something went
443 wrong or that the call is simply not included in the trace. */
445 /* Let's search for some actual call. */
446 caller = ftrace_find_call_by_number (btinfo, prev->up);
447 caller = ftrace_find_call (btinfo, caller);
450 /* There is no call in PREV's back trace. We assume that the
451 branch trace did not include it. */
453 /* Let's find the topmost function and add a new caller for it.
454 This should handle a series of initial tail calls. */
455 while (prev->up != 0)
456 prev = ftrace_find_call_by_number (btinfo, prev->up);
458 bfun->level = prev->level - 1;
460 /* Fix up the call stack for PREV. */
461 ftrace_fixup_caller (btinfo, prev, bfun, BFUN_UP_LINKS_TO_RET);
463 ftrace_debug (bfun, "new return - no caller");
467 /* There is a call in PREV's back trace to which we should have
468 returned but didn't. Let's start a new, separate back trace
469 from PREV's level. */
470 bfun->level = prev->level - 1;
472 /* We fix up the back trace for PREV but leave other function segments
473 on the same level as they are.
474 This should handle things like schedule () correctly where we're
475 switching contexts. */
476 prev->up = bfun->number;
477 prev->flags = BFUN_UP_LINKS_TO_RET;
479 ftrace_debug (bfun, "new return - unknown caller");
486 /* Add a new function segment for a function switch at the end of the trace.
487 BTINFO is the branch trace information for the current thread.
488 MFUN and FUN are the symbol information we have for this function. */
490 static struct btrace_function *
491 ftrace_new_switch (struct btrace_thread_info *btinfo,
492 struct minimal_symbol *mfun,
495 struct btrace_function *prev = btinfo->functions.back ();
496 struct btrace_function *bfun;
498 /* This is an unexplained function switch. We can't really be sure about the
499 call stack, yet the best I can think of right now is to preserve it. */
500 bfun = ftrace_new_function (btinfo, mfun, fun);
502 bfun->flags = prev->flags;
504 ftrace_debug (bfun, "new switch");
509 /* Add a new function segment for a gap in the trace due to a decode error at
510 the end of the trace.
511 BTINFO is the branch trace information for the current thread.
512 ERRCODE is the format-specific error code. */
514 static struct btrace_function *
515 ftrace_new_gap (struct btrace_thread_info *btinfo, int errcode)
517 struct btrace_function *bfun;
519 if (btinfo->functions.empty ())
520 bfun = ftrace_new_function (btinfo, NULL, NULL);
523 /* We hijack the previous function segment if it was empty. */
524 bfun = btinfo->functions.back ();
525 if (bfun->errcode != 0 || !VEC_empty (btrace_insn_s, bfun->insn))
526 bfun = ftrace_new_function (btinfo, NULL, NULL);
529 bfun->errcode = errcode;
531 ftrace_debug (bfun, "new gap");
536 /* Update the current function segment at the end of the trace in BTINFO with
537 respect to the instruction at PC. This may create new function segments.
538 Return the chronologically latest function segment, never NULL. */
540 static struct btrace_function *
541 ftrace_update_function (struct btrace_thread_info *btinfo, CORE_ADDR pc)
543 struct bound_minimal_symbol bmfun;
544 struct minimal_symbol *mfun;
546 struct btrace_insn *last;
547 struct btrace_function *bfun;
549 /* Try to determine the function we're in. We use both types of symbols
550 to avoid surprises when we sometimes get a full symbol and sometimes
551 only a minimal symbol. */
552 fun = find_pc_function (pc);
553 bmfun = lookup_minimal_symbol_by_pc (pc);
556 if (fun == NULL && mfun == NULL)
557 DEBUG_FTRACE ("no symbol at %s", core_addr_to_string_nz (pc));
559 /* If we didn't have a function, we create one. */
560 if (btinfo->functions.empty ())
561 return ftrace_new_function (btinfo, mfun, fun);
563 /* If we had a gap before, we create a function. */
564 bfun = btinfo->functions.back ();
565 if (bfun->errcode != 0)
566 return ftrace_new_function (btinfo, mfun, fun);
568 /* Check the last instruction, if we have one.
569 We do this check first, since it allows us to fill in the call stack
570 links in addition to the normal flow links. */
572 if (!VEC_empty (btrace_insn_s, bfun->insn))
573 last = VEC_last (btrace_insn_s, bfun->insn);
577 switch (last->iclass)
579 case BTRACE_INSN_RETURN:
583 /* On some systems, _dl_runtime_resolve returns to the resolved
584 function instead of jumping to it. From our perspective,
585 however, this is a tailcall.
586 If we treated it as return, we wouldn't be able to find the
587 resolved function in our stack back trace. Hence, we would
588 lose the current stack back trace and start anew with an empty
589 back trace. When the resolved function returns, we would then
590 create a stack back trace with the same function names but
591 different frame id's. This will confuse stepping. */
592 fname = ftrace_print_function_name (bfun);
593 if (strcmp (fname, "_dl_runtime_resolve") == 0)
594 return ftrace_new_tailcall (btinfo, mfun, fun);
596 return ftrace_new_return (btinfo, mfun, fun);
599 case BTRACE_INSN_CALL:
600 /* Ignore calls to the next instruction. They are used for PIC. */
601 if (last->pc + last->size == pc)
604 return ftrace_new_call (btinfo, mfun, fun);
606 case BTRACE_INSN_JUMP:
610 start = get_pc_function_start (pc);
612 /* A jump to the start of a function is (typically) a tail call. */
614 return ftrace_new_tailcall (btinfo, mfun, fun);
616 /* If we can't determine the function for PC, we treat a jump at
617 the end of the block as tail call if we're switching functions
618 and as an intra-function branch if we don't. */
619 if (start == 0 && ftrace_function_switched (bfun, mfun, fun))
620 return ftrace_new_tailcall (btinfo, mfun, fun);
627 /* Check if we're switching functions for some other reason. */
628 if (ftrace_function_switched (bfun, mfun, fun))
630 DEBUG_FTRACE ("switching from %s in %s at %s",
631 ftrace_print_insn_addr (last),
632 ftrace_print_function_name (bfun),
633 ftrace_print_filename (bfun));
635 return ftrace_new_switch (btinfo, mfun, fun);
641 /* Add the instruction at PC to BFUN's instructions. */
644 ftrace_update_insns (struct btrace_function *bfun,
645 const struct btrace_insn *insn)
647 VEC_safe_push (btrace_insn_s, bfun->insn, insn);
649 if (record_debug > 1)
650 ftrace_debug (bfun, "update insn");
653 /* Classify the instruction at PC. */
655 static enum btrace_insn_class
656 ftrace_classify_insn (struct gdbarch *gdbarch, CORE_ADDR pc)
658 enum btrace_insn_class iclass;
660 iclass = BTRACE_INSN_OTHER;
663 if (gdbarch_insn_is_call (gdbarch, pc))
664 iclass = BTRACE_INSN_CALL;
665 else if (gdbarch_insn_is_ret (gdbarch, pc))
666 iclass = BTRACE_INSN_RETURN;
667 else if (gdbarch_insn_is_jump (gdbarch, pc))
668 iclass = BTRACE_INSN_JUMP;
670 CATCH (error, RETURN_MASK_ERROR)
678 /* Try to match the back trace at LHS to the back trace at RHS. Returns the
679 number of matching function segments or zero if the back traces do not
680 match. BTINFO is the branch trace information for the current thread. */
683 ftrace_match_backtrace (struct btrace_thread_info *btinfo,
684 struct btrace_function *lhs,
685 struct btrace_function *rhs)
689 for (matches = 0; lhs != NULL && rhs != NULL; ++matches)
691 if (ftrace_function_switched (lhs, rhs->msym, rhs->sym))
694 lhs = ftrace_get_caller (btinfo, lhs);
695 rhs = ftrace_get_caller (btinfo, rhs);
701 /* Add ADJUSTMENT to the level of BFUN and succeeding function segments.
702 BTINFO is the branch trace information for the current thread. */
705 ftrace_fixup_level (struct btrace_thread_info *btinfo,
706 struct btrace_function *bfun, int adjustment)
711 DEBUG_FTRACE ("fixup level (%+d)", adjustment);
712 ftrace_debug (bfun, "..bfun");
716 bfun->level += adjustment;
717 bfun = ftrace_find_call_by_number (btinfo, bfun->number + 1);
721 /* Recompute the global level offset. Traverse the function trace and compute
722 the global level offset as the negative of the minimal function level. */
725 ftrace_compute_global_level_offset (struct btrace_thread_info *btinfo)
732 if (btinfo->functions.empty ())
735 unsigned int length = btinfo->functions.size() - 1;
736 for (unsigned int i = 0; i < length; ++i)
737 level = std::min (level, btinfo->functions[i]->level);
739 /* The last function segment contains the current instruction, which is not
740 really part of the trace. If it contains just this one instruction, we
741 ignore the segment. */
742 struct btrace_function *last = btinfo->functions.back();
743 if (VEC_length (btrace_insn_s, last->insn) != 1)
744 level = std::min (level, last->level);
746 DEBUG_FTRACE ("setting global level offset: %d", -level);
747 btinfo->level = -level;
750 /* Connect the function segments PREV and NEXT in a bottom-to-top walk as in
751 ftrace_connect_backtrace. BTINFO is the branch trace information for the
755 ftrace_connect_bfun (struct btrace_thread_info *btinfo,
756 struct btrace_function *prev,
757 struct btrace_function *next)
759 DEBUG_FTRACE ("connecting...");
760 ftrace_debug (prev, "..prev");
761 ftrace_debug (next, "..next");
763 /* The function segments are not yet connected. */
764 gdb_assert (prev->next == 0);
765 gdb_assert (next->prev == 0);
767 prev->next = next->number;
768 next->prev = prev->number;
770 /* We may have moved NEXT to a different function level. */
771 ftrace_fixup_level (btinfo, next, prev->level - next->level);
773 /* If we run out of back trace for one, let's use the other's. */
776 const btrace_function_flags flags = next->flags;
778 next = ftrace_find_call_by_number (btinfo, next->up);
781 DEBUG_FTRACE ("using next's callers");
782 ftrace_fixup_caller (btinfo, prev, next, flags);
785 else if (next->up == 0)
787 const btrace_function_flags flags = prev->flags;
789 prev = ftrace_find_call_by_number (btinfo, prev->up);
792 DEBUG_FTRACE ("using prev's callers");
793 ftrace_fixup_caller (btinfo, next, prev, flags);
798 /* PREV may have a tailcall caller, NEXT can't. If it does, fixup the up
799 link to add the tail callers to NEXT's back trace.
801 This removes NEXT->UP from NEXT's back trace. It will be added back
802 when connecting NEXT and PREV's callers - provided they exist.
804 If PREV's back trace consists of a series of tail calls without an
805 actual call, there will be no further connection and NEXT's caller will
806 be removed for good. To catch this case, we handle it here and connect
807 the top of PREV's back trace to NEXT's caller. */
808 if ((prev->flags & BFUN_UP_LINKS_TO_TAILCALL) != 0)
810 struct btrace_function *caller;
811 btrace_function_flags next_flags, prev_flags;
813 /* We checked NEXT->UP above so CALLER can't be NULL. */
814 caller = ftrace_find_call_by_number (btinfo, next->up);
815 next_flags = next->flags;
816 prev_flags = prev->flags;
818 DEBUG_FTRACE ("adding prev's tail calls to next");
820 prev = ftrace_find_call_by_number (btinfo, prev->up);
821 ftrace_fixup_caller (btinfo, next, prev, prev_flags);
823 for (; prev != NULL; prev = ftrace_find_call_by_number (btinfo,
826 /* At the end of PREV's back trace, continue with CALLER. */
829 DEBUG_FTRACE ("fixing up link for tailcall chain");
830 ftrace_debug (prev, "..top");
831 ftrace_debug (caller, "..up");
833 ftrace_fixup_caller (btinfo, prev, caller, next_flags);
835 /* If we skipped any tail calls, this may move CALLER to a
836 different function level.
838 Note that changing CALLER's level is only OK because we
839 know that this is the last iteration of the bottom-to-top
840 walk in ftrace_connect_backtrace.
842 Otherwise we will fix up CALLER's level when we connect it
843 to PREV's caller in the next iteration. */
844 ftrace_fixup_level (btinfo, caller,
845 prev->level - caller->level - 1);
849 /* There's nothing to do if we find a real call. */
850 if ((prev->flags & BFUN_UP_LINKS_TO_TAILCALL) == 0)
852 DEBUG_FTRACE ("will fix up link in next iteration");
860 /* Connect function segments on the same level in the back trace at LHS and RHS.
861 The back traces at LHS and RHS are expected to match according to
862 ftrace_match_backtrace. BTINFO is the branch trace information for the
866 ftrace_connect_backtrace (struct btrace_thread_info *btinfo,
867 struct btrace_function *lhs,
868 struct btrace_function *rhs)
870 while (lhs != NULL && rhs != NULL)
872 struct btrace_function *prev, *next;
874 gdb_assert (!ftrace_function_switched (lhs, rhs->msym, rhs->sym));
876 /* Connecting LHS and RHS may change the up link. */
880 lhs = ftrace_get_caller (btinfo, lhs);
881 rhs = ftrace_get_caller (btinfo, rhs);
883 ftrace_connect_bfun (btinfo, prev, next);
887 /* Bridge the gap between two function segments left and right of a gap if their
888 respective back traces match in at least MIN_MATCHES functions. BTINFO is
889 the branch trace information for the current thread.
891 Returns non-zero if the gap could be bridged, zero otherwise. */
894 ftrace_bridge_gap (struct btrace_thread_info *btinfo,
895 struct btrace_function *lhs, struct btrace_function *rhs,
898 struct btrace_function *best_l, *best_r, *cand_l, *cand_r;
901 DEBUG_FTRACE ("checking gap at insn %u (req matches: %d)",
902 rhs->insn_offset - 1, min_matches);
908 /* We search the back traces of LHS and RHS for valid connections and connect
909 the two functon segments that give the longest combined back trace. */
911 for (cand_l = lhs; cand_l != NULL;
912 cand_l = ftrace_get_caller (btinfo, cand_l))
913 for (cand_r = rhs; cand_r != NULL;
914 cand_r = ftrace_get_caller (btinfo, cand_r))
918 matches = ftrace_match_backtrace (btinfo, cand_l, cand_r);
919 if (best_matches < matches)
921 best_matches = matches;
927 /* We need at least MIN_MATCHES matches. */
928 gdb_assert (min_matches > 0);
929 if (best_matches < min_matches)
932 DEBUG_FTRACE ("..matches: %d", best_matches);
934 /* We will fix up the level of BEST_R and succeeding function segments such
935 that BEST_R's level matches BEST_L's when we connect BEST_L to BEST_R.
937 This will ignore the level of RHS and following if BEST_R != RHS. I.e. if
938 BEST_R is a successor of RHS in the back trace of RHS (phases 1 and 3).
940 To catch this, we already fix up the level here where we can start at RHS
941 instead of at BEST_R. We will ignore the level fixup when connecting
942 BEST_L to BEST_R as they will already be on the same level. */
943 ftrace_fixup_level (btinfo, rhs, best_l->level - best_r->level);
945 ftrace_connect_backtrace (btinfo, best_l, best_r);
950 /* Try to bridge gaps due to overflow or decode errors by connecting the
951 function segments that are separated by the gap. */
954 btrace_bridge_gaps (struct thread_info *tp, VEC (bfun_s) **gaps)
956 struct btrace_thread_info *btinfo = &tp->btrace;
957 VEC (bfun_s) *remaining;
958 struct cleanup *old_chain;
961 DEBUG ("bridge gaps");
964 old_chain = make_cleanup (VEC_cleanup (bfun_s), &remaining);
966 /* We require a minimum amount of matches for bridging a gap. The number of
967 required matches will be lowered with each iteration.
969 The more matches the higher our confidence that the bridging is correct.
970 For big gaps or small traces, however, it may not be feasible to require a
971 high number of matches. */
972 for (min_matches = 5; min_matches > 0; --min_matches)
974 /* Let's try to bridge as many gaps as we can. In some cases, we need to
975 skip a gap and revisit it again after we closed later gaps. */
976 while (!VEC_empty (bfun_s, *gaps))
978 struct btrace_function *gap;
981 for (idx = 0; VEC_iterate (bfun_s, *gaps, idx, gap); ++idx)
983 struct btrace_function *lhs, *rhs;
986 /* We may have a sequence of gaps if we run from one error into
987 the next as we try to re-sync onto the trace stream. Ignore
988 all but the leftmost gap in such a sequence.
990 Also ignore gaps at the beginning of the trace. */
991 lhs = ftrace_find_call_by_number (btinfo, gap->number - 1);
992 if (lhs == NULL || lhs->errcode != 0)
995 /* Skip gaps to the right. */
996 rhs = ftrace_find_call_by_number (btinfo, gap->number + 1);
997 while (rhs != NULL && rhs->errcode != 0)
998 rhs = ftrace_find_call_by_number (btinfo, rhs->number + 1);
1000 /* Ignore gaps at the end of the trace. */
1004 bridged = ftrace_bridge_gap (btinfo, lhs, rhs, min_matches);
1006 /* Keep track of gaps we were not able to bridge and try again.
1007 If we just pushed them to the end of GAPS we would risk an
1008 infinite loop in case we simply cannot bridge a gap. */
1010 VEC_safe_push (bfun_s, remaining, gap);
1013 /* Let's see if we made any progress. */
1014 if (VEC_length (bfun_s, remaining) == VEC_length (bfun_s, *gaps))
1017 VEC_free (bfun_s, *gaps);
1023 /* We get here if either GAPS is empty or if GAPS equals REMAINING. */
1024 if (VEC_empty (bfun_s, *gaps))
1027 VEC_free (bfun_s, remaining);
1030 do_cleanups (old_chain);
1032 /* We may omit this in some cases. Not sure it is worth the extra
1033 complication, though. */
1034 ftrace_compute_global_level_offset (btinfo);
1037 /* Compute the function branch trace from BTS trace. */
1040 btrace_compute_ftrace_bts (struct thread_info *tp,
1041 const struct btrace_data_bts *btrace,
1042 VEC (bfun_s) **gaps)
1044 struct btrace_thread_info *btinfo;
1045 struct gdbarch *gdbarch;
1049 gdbarch = target_gdbarch ();
1050 btinfo = &tp->btrace;
1051 blk = VEC_length (btrace_block_s, btrace->blocks);
1053 if (btinfo->functions.empty ())
1056 level = -btinfo->level;
1060 btrace_block_s *block;
1065 block = VEC_index (btrace_block_s, btrace->blocks, blk);
1070 struct btrace_function *bfun;
1071 struct btrace_insn insn;
1074 /* We should hit the end of the block. Warn if we went too far. */
1075 if (block->end < pc)
1077 /* Indicate the gap in the trace. */
1078 bfun = ftrace_new_gap (btinfo, BDE_BTS_OVERFLOW);
1080 VEC_safe_push (bfun_s, *gaps, bfun);
1082 warning (_("Recorded trace may be corrupted at instruction "
1083 "%u (pc = %s)."), bfun->insn_offset - 1,
1084 core_addr_to_string_nz (pc));
1089 bfun = ftrace_update_function (btinfo, pc);
1091 /* Maintain the function level offset.
1092 For all but the last block, we do it here. */
1094 level = std::min (level, bfun->level);
1099 size = gdb_insn_length (gdbarch, pc);
1101 CATCH (error, RETURN_MASK_ERROR)
1108 insn.iclass = ftrace_classify_insn (gdbarch, pc);
1111 ftrace_update_insns (bfun, &insn);
1113 /* We're done once we pushed the instruction at the end. */
1114 if (block->end == pc)
1117 /* We can't continue if we fail to compute the size. */
1120 /* Indicate the gap in the trace. We just added INSN so we're
1121 not at the beginning. */
1122 bfun = ftrace_new_gap (btinfo, BDE_BTS_INSN_SIZE);
1124 VEC_safe_push (bfun_s, *gaps, bfun);
1126 warning (_("Recorded trace may be incomplete at instruction %u "
1127 "(pc = %s)."), bfun->insn_offset - 1,
1128 core_addr_to_string_nz (pc));
1135 /* Maintain the function level offset.
1136 For the last block, we do it here to not consider the last
1138 Since the last instruction corresponds to the current instruction
1139 and is not really part of the execution history, it shouldn't
1140 affect the level. */
1142 level = std::min (level, bfun->level);
1146 /* LEVEL is the minimal function level of all btrace function segments.
1147 Define the global level offset to -LEVEL so all function levels are
1148 normalized to start at zero. */
1149 btinfo->level = -level;
1152 #if defined (HAVE_LIBIPT)
1154 static enum btrace_insn_class
1155 pt_reclassify_insn (enum pt_insn_class iclass)
1160 return BTRACE_INSN_CALL;
1163 return BTRACE_INSN_RETURN;
1166 return BTRACE_INSN_JUMP;
1169 return BTRACE_INSN_OTHER;
1173 /* Return the btrace instruction flags for INSN. */
1175 static btrace_insn_flags
1176 pt_btrace_insn_flags (const struct pt_insn &insn)
1178 btrace_insn_flags flags = 0;
1180 if (insn.speculative)
1181 flags |= BTRACE_INSN_FLAG_SPECULATIVE;
1186 /* Return the btrace instruction for INSN. */
1189 pt_btrace_insn (const struct pt_insn &insn)
1191 return {(CORE_ADDR) insn.ip, (gdb_byte) insn.size,
1192 pt_reclassify_insn (insn.iclass),
1193 pt_btrace_insn_flags (insn)};
1197 /* Add function branch trace to BTINFO using DECODER. */
1200 ftrace_add_pt (struct btrace_thread_info *btinfo,
1201 struct pt_insn_decoder *decoder,
1203 VEC (bfun_s) **gaps)
1205 struct btrace_function *bfun;
1211 struct pt_insn insn;
1213 errcode = pt_insn_sync_forward (decoder);
1216 if (errcode != -pte_eos)
1217 warning (_("Failed to synchronize onto the Intel Processor "
1218 "Trace stream: %s."), pt_errstr (pt_errcode (errcode)));
1224 errcode = pt_insn_next (decoder, &insn, sizeof(insn));
1228 /* Look for gaps in the trace - unless we're at the beginning. */
1229 if (!btinfo->functions.empty ())
1231 /* Tracing is disabled and re-enabled each time we enter the
1232 kernel. Most times, we continue from the same instruction we
1233 stopped before. This is indicated via the RESUMED instruction
1234 flag. The ENABLED instruction flag means that we continued
1235 from some other instruction. Indicate this as a trace gap. */
1238 bfun = ftrace_new_gap (btinfo, BDE_PT_DISABLED);
1240 VEC_safe_push (bfun_s, *gaps, bfun);
1242 pt_insn_get_offset (decoder, &offset);
1244 warning (_("Non-contiguous trace at instruction %u (offset "
1245 "= 0x%" PRIx64 ", pc = 0x%" PRIx64 ")."),
1246 bfun->insn_offset - 1, offset, insn.ip);
1250 /* Indicate trace overflows. */
1253 bfun = ftrace_new_gap (btinfo, BDE_PT_OVERFLOW);
1255 VEC_safe_push (bfun_s, *gaps, bfun);
1257 pt_insn_get_offset (decoder, &offset);
1259 warning (_("Overflow at instruction %u (offset = 0x%" PRIx64
1260 ", pc = 0x%" PRIx64 ")."), bfun->insn_offset - 1,
1264 bfun = ftrace_update_function (btinfo, insn.ip);
1266 /* Maintain the function level offset. */
1267 *plevel = std::min (*plevel, bfun->level);
1269 btrace_insn btinsn = pt_btrace_insn (insn);
1270 ftrace_update_insns (bfun, &btinsn);
1273 if (errcode == -pte_eos)
1276 /* Indicate the gap in the trace. */
1277 bfun = ftrace_new_gap (btinfo, errcode);
1279 VEC_safe_push (bfun_s, *gaps, bfun);
1281 pt_insn_get_offset (decoder, &offset);
1283 warning (_("Decode error (%d) at instruction %u (offset = 0x%" PRIx64
1284 ", pc = 0x%" PRIx64 "): %s."), errcode, bfun->insn_offset - 1,
1285 offset, insn.ip, pt_errstr (pt_errcode (errcode)));
1289 /* A callback function to allow the trace decoder to read the inferior's
1293 btrace_pt_readmem_callback (gdb_byte *buffer, size_t size,
1294 const struct pt_asid *asid, uint64_t pc,
1297 int result, errcode;
1299 result = (int) size;
1302 errcode = target_read_code ((CORE_ADDR) pc, buffer, size);
1304 result = -pte_nomap;
1306 CATCH (error, RETURN_MASK_ERROR)
1308 result = -pte_nomap;
1315 /* Translate the vendor from one enum to another. */
1317 static enum pt_cpu_vendor
1318 pt_translate_cpu_vendor (enum btrace_cpu_vendor vendor)
1330 /* Finalize the function branch trace after decode. */
1332 static void btrace_finalize_ftrace_pt (struct pt_insn_decoder *decoder,
1333 struct thread_info *tp, int level)
1335 pt_insn_free_decoder (decoder);
1337 /* LEVEL is the minimal function level of all btrace function segments.
1338 Define the global level offset to -LEVEL so all function levels are
1339 normalized to start at zero. */
1340 tp->btrace.level = -level;
1342 /* Add a single last instruction entry for the current PC.
1343 This allows us to compute the backtrace at the current PC using both
1344 standard unwind and btrace unwind.
1345 This extra entry is ignored by all record commands. */
1349 /* Compute the function branch trace from Intel Processor Trace
1353 btrace_compute_ftrace_pt (struct thread_info *tp,
1354 const struct btrace_data_pt *btrace,
1355 VEC (bfun_s) **gaps)
1357 struct btrace_thread_info *btinfo;
1358 struct pt_insn_decoder *decoder;
1359 struct pt_config config;
1362 if (btrace->size == 0)
1365 btinfo = &tp->btrace;
1366 if (btinfo->functions.empty ())
1369 level = -btinfo->level;
1371 pt_config_init(&config);
1372 config.begin = btrace->data;
1373 config.end = btrace->data + btrace->size;
1375 config.cpu.vendor = pt_translate_cpu_vendor (btrace->config.cpu.vendor);
1376 config.cpu.family = btrace->config.cpu.family;
1377 config.cpu.model = btrace->config.cpu.model;
1378 config.cpu.stepping = btrace->config.cpu.stepping;
1380 errcode = pt_cpu_errata (&config.errata, &config.cpu);
1382 error (_("Failed to configure the Intel Processor Trace decoder: %s."),
1383 pt_errstr (pt_errcode (errcode)));
1385 decoder = pt_insn_alloc_decoder (&config);
1386 if (decoder == NULL)
1387 error (_("Failed to allocate the Intel Processor Trace decoder."));
1391 struct pt_image *image;
1393 image = pt_insn_get_image(decoder);
1395 error (_("Failed to configure the Intel Processor Trace decoder."));
1397 errcode = pt_image_set_callback(image, btrace_pt_readmem_callback, NULL);
1399 error (_("Failed to configure the Intel Processor Trace decoder: "
1400 "%s."), pt_errstr (pt_errcode (errcode)));
1402 ftrace_add_pt (btinfo, decoder, &level, gaps);
1404 CATCH (error, RETURN_MASK_ALL)
1406 /* Indicate a gap in the trace if we quit trace processing. */
1407 if (error.reason == RETURN_QUIT && !btinfo->functions.empty ())
1409 struct btrace_function *bfun;
1411 bfun = ftrace_new_gap (btinfo, BDE_PT_USER_QUIT);
1413 VEC_safe_push (bfun_s, *gaps, bfun);
1416 btrace_finalize_ftrace_pt (decoder, tp, level);
1418 throw_exception (error);
1422 btrace_finalize_ftrace_pt (decoder, tp, level);
1425 #else /* defined (HAVE_LIBIPT) */
1428 btrace_compute_ftrace_pt (struct thread_info *tp,
1429 const struct btrace_data_pt *btrace,
1430 VEC (bfun_s) **gaps)
1432 internal_error (__FILE__, __LINE__, _("Unexpected branch trace format."));
1435 #endif /* defined (HAVE_LIBIPT) */
1437 /* Compute the function branch trace from a block branch trace BTRACE for
1438 a thread given by BTINFO. */
1441 btrace_compute_ftrace_1 (struct thread_info *tp, struct btrace_data *btrace,
1442 VEC (bfun_s) **gaps)
1444 DEBUG ("compute ftrace");
1446 switch (btrace->format)
1448 case BTRACE_FORMAT_NONE:
1451 case BTRACE_FORMAT_BTS:
1452 btrace_compute_ftrace_bts (tp, &btrace->variant.bts, gaps);
1455 case BTRACE_FORMAT_PT:
1456 btrace_compute_ftrace_pt (tp, &btrace->variant.pt, gaps);
1460 internal_error (__FILE__, __LINE__, _("Unkown branch trace format."));
1464 btrace_finalize_ftrace (struct thread_info *tp, VEC (bfun_s) **gaps)
1466 if (!VEC_empty (bfun_s, *gaps))
1468 tp->btrace.ngaps += VEC_length (bfun_s, *gaps);
1469 btrace_bridge_gaps (tp, gaps);
1474 btrace_compute_ftrace (struct thread_info *tp, struct btrace_data *btrace)
1477 struct cleanup *old_chain;
1480 old_chain = make_cleanup (VEC_cleanup (bfun_s), &gaps);
1484 btrace_compute_ftrace_1 (tp, btrace, &gaps);
1486 CATCH (error, RETURN_MASK_ALL)
1488 btrace_finalize_ftrace (tp, &gaps);
1490 throw_exception (error);
1494 btrace_finalize_ftrace (tp, &gaps);
1496 do_cleanups (old_chain);
1499 /* Add an entry for the current PC. */
1502 btrace_add_pc (struct thread_info *tp)
1504 struct btrace_data btrace;
1505 struct btrace_block *block;
1506 struct regcache *regcache;
1507 struct cleanup *cleanup;
1510 regcache = get_thread_regcache (tp->ptid);
1511 pc = regcache_read_pc (regcache);
1513 btrace_data_init (&btrace);
1514 btrace.format = BTRACE_FORMAT_BTS;
1515 btrace.variant.bts.blocks = NULL;
1517 cleanup = make_cleanup_btrace_data (&btrace);
1519 block = VEC_safe_push (btrace_block_s, btrace.variant.bts.blocks, NULL);
1523 btrace_compute_ftrace (tp, &btrace);
1525 do_cleanups (cleanup);
1531 btrace_enable (struct thread_info *tp, const struct btrace_config *conf)
1533 if (tp->btrace.target != NULL)
1536 #if !defined (HAVE_LIBIPT)
1537 if (conf->format == BTRACE_FORMAT_PT)
1538 error (_("GDB does not support Intel Processor Trace."));
1539 #endif /* !defined (HAVE_LIBIPT) */
1541 if (!target_supports_btrace (conf->format))
1542 error (_("Target does not support branch tracing."));
1544 DEBUG ("enable thread %s (%s)", print_thread_id (tp),
1545 target_pid_to_str (tp->ptid));
1547 tp->btrace.target = target_enable_btrace (tp->ptid, conf);
1549 /* We're done if we failed to enable tracing. */
1550 if (tp->btrace.target == NULL)
1553 /* We need to undo the enable in case of errors. */
1556 /* Add an entry for the current PC so we start tracing from where we
1559 If we can't access TP's registers, TP is most likely running. In this
1560 case, we can't really say where tracing was enabled so it should be
1561 safe to simply skip this step.
1563 This is not relevant for BTRACE_FORMAT_PT since the trace will already
1564 start at the PC at which tracing was enabled. */
1565 if (conf->format != BTRACE_FORMAT_PT
1566 && can_access_registers_ptid (tp->ptid))
1569 CATCH (exception, RETURN_MASK_ALL)
1571 btrace_disable (tp);
1573 throw_exception (exception);
1580 const struct btrace_config *
1581 btrace_conf (const struct btrace_thread_info *btinfo)
1583 if (btinfo->target == NULL)
1586 return target_btrace_conf (btinfo->target);
1592 btrace_disable (struct thread_info *tp)
1594 struct btrace_thread_info *btp = &tp->btrace;
1597 if (btp->target == NULL)
1600 DEBUG ("disable thread %s (%s)", print_thread_id (tp),
1601 target_pid_to_str (tp->ptid));
1603 target_disable_btrace (btp->target);
1612 btrace_teardown (struct thread_info *tp)
1614 struct btrace_thread_info *btp = &tp->btrace;
1617 if (btp->target == NULL)
1620 DEBUG ("teardown thread %s (%s)", print_thread_id (tp),
1621 target_pid_to_str (tp->ptid));
1623 target_teardown_btrace (btp->target);
1629 /* Stitch branch trace in BTS format. */
1632 btrace_stitch_bts (struct btrace_data_bts *btrace, struct thread_info *tp)
1634 struct btrace_thread_info *btinfo;
1635 struct btrace_function *last_bfun;
1636 struct btrace_insn *last_insn;
1637 btrace_block_s *first_new_block;
1639 btinfo = &tp->btrace;
1640 gdb_assert (!btinfo->functions.empty ());
1641 gdb_assert (!VEC_empty (btrace_block_s, btrace->blocks));
1643 last_bfun = btinfo->functions.back ();
1645 /* If the existing trace ends with a gap, we just glue the traces
1646 together. We need to drop the last (i.e. chronologically first) block
1647 of the new trace, though, since we can't fill in the start address.*/
1648 if (VEC_empty (btrace_insn_s, last_bfun->insn))
1650 VEC_pop (btrace_block_s, btrace->blocks);
1654 /* Beware that block trace starts with the most recent block, so the
1655 chronologically first block in the new trace is the last block in
1656 the new trace's block vector. */
1657 first_new_block = VEC_last (btrace_block_s, btrace->blocks);
1658 last_insn = VEC_last (btrace_insn_s, last_bfun->insn);
1660 /* If the current PC at the end of the block is the same as in our current
1661 trace, there are two explanations:
1662 1. we executed the instruction and some branch brought us back.
1663 2. we have not made any progress.
1664 In the first case, the delta trace vector should contain at least two
1666 In the second case, the delta trace vector should contain exactly one
1667 entry for the partial block containing the current PC. Remove it. */
1668 if (first_new_block->end == last_insn->pc
1669 && VEC_length (btrace_block_s, btrace->blocks) == 1)
1671 VEC_pop (btrace_block_s, btrace->blocks);
1675 DEBUG ("stitching %s to %s", ftrace_print_insn_addr (last_insn),
1676 core_addr_to_string_nz (first_new_block->end));
1678 /* Do a simple sanity check to make sure we don't accidentally end up
1679 with a bad block. This should not occur in practice. */
1680 if (first_new_block->end < last_insn->pc)
1682 warning (_("Error while trying to read delta trace. Falling back to "
1687 /* We adjust the last block to start at the end of our current trace. */
1688 gdb_assert (first_new_block->begin == 0);
1689 first_new_block->begin = last_insn->pc;
1691 /* We simply pop the last insn so we can insert it again as part of
1692 the normal branch trace computation.
1693 Since instruction iterators are based on indices in the instructions
1694 vector, we don't leave any pointers dangling. */
1695 DEBUG ("pruning insn at %s for stitching",
1696 ftrace_print_insn_addr (last_insn));
1698 VEC_pop (btrace_insn_s, last_bfun->insn);
1700 /* The instructions vector may become empty temporarily if this has
1701 been the only instruction in this function segment.
1702 This violates the invariant but will be remedied shortly by
1703 btrace_compute_ftrace when we add the new trace. */
1705 /* The only case where this would hurt is if the entire trace consisted
1706 of just that one instruction. If we remove it, we might turn the now
1707 empty btrace function segment into a gap. But we don't want gaps at
1708 the beginning. To avoid this, we remove the entire old trace. */
1709 if (last_bfun->number == 1 && VEC_empty (btrace_insn_s, last_bfun->insn))
1715 /* Adjust the block trace in order to stitch old and new trace together.
1716 BTRACE is the new delta trace between the last and the current stop.
1717 TP is the traced thread.
1718 May modifx BTRACE as well as the existing trace in TP.
1719 Return 0 on success, -1 otherwise. */
1722 btrace_stitch_trace (struct btrace_data *btrace, struct thread_info *tp)
1724 /* If we don't have trace, there's nothing to do. */
1725 if (btrace_data_empty (btrace))
1728 switch (btrace->format)
1730 case BTRACE_FORMAT_NONE:
1733 case BTRACE_FORMAT_BTS:
1734 return btrace_stitch_bts (&btrace->variant.bts, tp);
1736 case BTRACE_FORMAT_PT:
1737 /* Delta reads are not supported. */
1741 internal_error (__FILE__, __LINE__, _("Unkown branch trace format."));
1744 /* Clear the branch trace histories in BTINFO. */
1747 btrace_clear_history (struct btrace_thread_info *btinfo)
1749 xfree (btinfo->insn_history);
1750 xfree (btinfo->call_history);
1751 xfree (btinfo->replay);
1753 btinfo->insn_history = NULL;
1754 btinfo->call_history = NULL;
1755 btinfo->replay = NULL;
1758 /* Clear the branch trace maintenance histories in BTINFO. */
1761 btrace_maint_clear (struct btrace_thread_info *btinfo)
1763 switch (btinfo->data.format)
1768 case BTRACE_FORMAT_BTS:
1769 btinfo->maint.variant.bts.packet_history.begin = 0;
1770 btinfo->maint.variant.bts.packet_history.end = 0;
1773 #if defined (HAVE_LIBIPT)
1774 case BTRACE_FORMAT_PT:
1775 xfree (btinfo->maint.variant.pt.packets);
1777 btinfo->maint.variant.pt.packets = NULL;
1778 btinfo->maint.variant.pt.packet_history.begin = 0;
1779 btinfo->maint.variant.pt.packet_history.end = 0;
1781 #endif /* defined (HAVE_LIBIPT) */
1788 btrace_decode_error (enum btrace_format format, int errcode)
1792 case BTRACE_FORMAT_BTS:
1795 case BDE_BTS_OVERFLOW:
1796 return _("instruction overflow");
1798 case BDE_BTS_INSN_SIZE:
1799 return _("unknown instruction");
1806 #if defined (HAVE_LIBIPT)
1807 case BTRACE_FORMAT_PT:
1810 case BDE_PT_USER_QUIT:
1811 return _("trace decode cancelled");
1813 case BDE_PT_DISABLED:
1814 return _("disabled");
1816 case BDE_PT_OVERFLOW:
1817 return _("overflow");
1821 return pt_errstr (pt_errcode (errcode));
1825 #endif /* defined (HAVE_LIBIPT) */
1831 return _("unknown");
1837 btrace_fetch (struct thread_info *tp)
1839 struct btrace_thread_info *btinfo;
1840 struct btrace_target_info *tinfo;
1841 struct btrace_data btrace;
1842 struct cleanup *cleanup;
1845 DEBUG ("fetch thread %s (%s)", print_thread_id (tp),
1846 target_pid_to_str (tp->ptid));
1848 btinfo = &tp->btrace;
1849 tinfo = btinfo->target;
1853 /* There's no way we could get new trace while replaying.
1854 On the other hand, delta trace would return a partial record with the
1855 current PC, which is the replay PC, not the last PC, as expected. */
1856 if (btinfo->replay != NULL)
1859 /* With CLI usage, TP->PTID always equals INFERIOR_PTID here. Now that we
1860 can store a gdb.Record object in Python referring to a different thread
1861 than the current one, temporarily set INFERIOR_PTID. */
1862 cleanup = save_inferior_ptid ();
1863 inferior_ptid = tp->ptid;
1865 /* We should not be called on running or exited threads. */
1866 gdb_assert (can_access_registers_ptid (tp->ptid));
1868 btrace_data_init (&btrace);
1869 make_cleanup_btrace_data (&btrace);
1871 /* Let's first try to extend the trace we already have. */
1872 if (!btinfo->functions.empty ())
1874 errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_DELTA);
1877 /* Success. Let's try to stitch the traces together. */
1878 errcode = btrace_stitch_trace (&btrace, tp);
1882 /* We failed to read delta trace. Let's try to read new trace. */
1883 errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_NEW);
1885 /* If we got any new trace, discard what we have. */
1886 if (errcode == 0 && !btrace_data_empty (&btrace))
1890 /* If we were not able to read the trace, we start over. */
1894 errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_ALL);
1898 errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_ALL);
1900 /* If we were not able to read the branch trace, signal an error. */
1902 error (_("Failed to read branch trace."));
1904 /* Compute the trace, provided we have any. */
1905 if (!btrace_data_empty (&btrace))
1907 /* Store the raw trace data. The stored data will be cleared in
1908 btrace_clear, so we always append the new trace. */
1909 btrace_data_append (&btinfo->data, &btrace);
1910 btrace_maint_clear (btinfo);
1912 btrace_clear_history (btinfo);
1913 btrace_compute_ftrace (tp, &btrace);
1916 do_cleanups (cleanup);
1922 btrace_clear (struct thread_info *tp)
1924 struct btrace_thread_info *btinfo;
1926 DEBUG ("clear thread %s (%s)", print_thread_id (tp),
1927 target_pid_to_str (tp->ptid));
1929 /* Make sure btrace frames that may hold a pointer into the branch
1930 trace data are destroyed. */
1931 reinit_frame_cache ();
1933 btinfo = &tp->btrace;
1934 for (auto &bfun : btinfo->functions)
1936 VEC_free (btrace_insn_s, bfun->insn);
1940 btinfo->functions.clear ();
1943 /* Must clear the maint data before - it depends on BTINFO->DATA. */
1944 btrace_maint_clear (btinfo);
1945 btrace_data_clear (&btinfo->data);
1946 btrace_clear_history (btinfo);
1952 btrace_free_objfile (struct objfile *objfile)
1954 struct thread_info *tp;
1956 DEBUG ("free objfile");
1958 ALL_NON_EXITED_THREADS (tp)
1962 #if defined (HAVE_LIBEXPAT)
1964 /* Check the btrace document version. */
1967 check_xml_btrace_version (struct gdb_xml_parser *parser,
1968 const struct gdb_xml_element *element,
1969 void *user_data, VEC (gdb_xml_value_s) *attributes)
1972 = (const char *) xml_find_attribute (attributes, "version")->value;
1974 if (strcmp (version, "1.0") != 0)
1975 gdb_xml_error (parser, _("Unsupported btrace version: \"%s\""), version);
1978 /* Parse a btrace "block" xml record. */
1981 parse_xml_btrace_block (struct gdb_xml_parser *parser,
1982 const struct gdb_xml_element *element,
1983 void *user_data, VEC (gdb_xml_value_s) *attributes)
1985 struct btrace_data *btrace;
1986 struct btrace_block *block;
1987 ULONGEST *begin, *end;
1989 btrace = (struct btrace_data *) user_data;
1991 switch (btrace->format)
1993 case BTRACE_FORMAT_BTS:
1996 case BTRACE_FORMAT_NONE:
1997 btrace->format = BTRACE_FORMAT_BTS;
1998 btrace->variant.bts.blocks = NULL;
2002 gdb_xml_error (parser, _("Btrace format error."));
2005 begin = (ULONGEST *) xml_find_attribute (attributes, "begin")->value;
2006 end = (ULONGEST *) xml_find_attribute (attributes, "end")->value;
2008 block = VEC_safe_push (btrace_block_s, btrace->variant.bts.blocks, NULL);
2009 block->begin = *begin;
2013 /* Parse a "raw" xml record. */
2016 parse_xml_raw (struct gdb_xml_parser *parser, const char *body_text,
2017 gdb_byte **pdata, size_t *psize)
2019 struct cleanup *cleanup;
2020 gdb_byte *data, *bin;
2023 len = strlen (body_text);
2025 gdb_xml_error (parser, _("Bad raw data size."));
2029 bin = data = (gdb_byte *) xmalloc (size);
2030 cleanup = make_cleanup (xfree, data);
2032 /* We use hex encoding - see common/rsp-low.h. */
2040 if (hi == 0 || lo == 0)
2041 gdb_xml_error (parser, _("Bad hex encoding."));
2043 *bin++ = fromhex (hi) * 16 + fromhex (lo);
2047 discard_cleanups (cleanup);
2053 /* Parse a btrace pt-config "cpu" xml record. */
2056 parse_xml_btrace_pt_config_cpu (struct gdb_xml_parser *parser,
2057 const struct gdb_xml_element *element,
2059 VEC (gdb_xml_value_s) *attributes)
2061 struct btrace_data *btrace;
2063 ULONGEST *family, *model, *stepping;
2065 vendor = (const char *) xml_find_attribute (attributes, "vendor")->value;
2066 family = (ULONGEST *) xml_find_attribute (attributes, "family")->value;
2067 model = (ULONGEST *) xml_find_attribute (attributes, "model")->value;
2068 stepping = (ULONGEST *) xml_find_attribute (attributes, "stepping")->value;
2070 btrace = (struct btrace_data *) user_data;
2072 if (strcmp (vendor, "GenuineIntel") == 0)
2073 btrace->variant.pt.config.cpu.vendor = CV_INTEL;
2075 btrace->variant.pt.config.cpu.family = *family;
2076 btrace->variant.pt.config.cpu.model = *model;
2077 btrace->variant.pt.config.cpu.stepping = *stepping;
2080 /* Parse a btrace pt "raw" xml record. */
2083 parse_xml_btrace_pt_raw (struct gdb_xml_parser *parser,
2084 const struct gdb_xml_element *element,
2085 void *user_data, const char *body_text)
2087 struct btrace_data *btrace;
2089 btrace = (struct btrace_data *) user_data;
2090 parse_xml_raw (parser, body_text, &btrace->variant.pt.data,
2091 &btrace->variant.pt.size);
2094 /* Parse a btrace "pt" xml record. */
2097 parse_xml_btrace_pt (struct gdb_xml_parser *parser,
2098 const struct gdb_xml_element *element,
2099 void *user_data, VEC (gdb_xml_value_s) *attributes)
2101 struct btrace_data *btrace;
2103 btrace = (struct btrace_data *) user_data;
2104 btrace->format = BTRACE_FORMAT_PT;
2105 btrace->variant.pt.config.cpu.vendor = CV_UNKNOWN;
2106 btrace->variant.pt.data = NULL;
2107 btrace->variant.pt.size = 0;
2110 static const struct gdb_xml_attribute block_attributes[] = {
2111 { "begin", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
2112 { "end", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
2113 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2116 static const struct gdb_xml_attribute btrace_pt_config_cpu_attributes[] = {
2117 { "vendor", GDB_XML_AF_NONE, NULL, NULL },
2118 { "family", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
2119 { "model", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
2120 { "stepping", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
2121 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2124 static const struct gdb_xml_element btrace_pt_config_children[] = {
2125 { "cpu", btrace_pt_config_cpu_attributes, NULL, GDB_XML_EF_OPTIONAL,
2126 parse_xml_btrace_pt_config_cpu, NULL },
2127 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2130 static const struct gdb_xml_element btrace_pt_children[] = {
2131 { "pt-config", NULL, btrace_pt_config_children, GDB_XML_EF_OPTIONAL, NULL,
2133 { "raw", NULL, NULL, GDB_XML_EF_OPTIONAL, NULL, parse_xml_btrace_pt_raw },
2134 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2137 static const struct gdb_xml_attribute btrace_attributes[] = {
2138 { "version", GDB_XML_AF_NONE, NULL, NULL },
2139 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2142 static const struct gdb_xml_element btrace_children[] = {
2143 { "block", block_attributes, NULL,
2144 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL, parse_xml_btrace_block, NULL },
2145 { "pt", NULL, btrace_pt_children, GDB_XML_EF_OPTIONAL, parse_xml_btrace_pt,
2147 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2150 static const struct gdb_xml_element btrace_elements[] = {
2151 { "btrace", btrace_attributes, btrace_children, GDB_XML_EF_NONE,
2152 check_xml_btrace_version, NULL },
2153 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2156 #endif /* defined (HAVE_LIBEXPAT) */
2161 parse_xml_btrace (struct btrace_data *btrace, const char *buffer)
2163 struct cleanup *cleanup;
2166 #if defined (HAVE_LIBEXPAT)
2168 btrace->format = BTRACE_FORMAT_NONE;
2170 cleanup = make_cleanup_btrace_data (btrace);
2171 errcode = gdb_xml_parse_quick (_("btrace"), "btrace.dtd", btrace_elements,
2174 error (_("Error parsing branch trace."));
2176 /* Keep parse results. */
2177 discard_cleanups (cleanup);
2179 #else /* !defined (HAVE_LIBEXPAT) */
2181 error (_("Cannot process branch trace. XML parsing is not supported."));
2183 #endif /* !defined (HAVE_LIBEXPAT) */
2186 #if defined (HAVE_LIBEXPAT)
2188 /* Parse a btrace-conf "bts" xml record. */
2191 parse_xml_btrace_conf_bts (struct gdb_xml_parser *parser,
2192 const struct gdb_xml_element *element,
2193 void *user_data, VEC (gdb_xml_value_s) *attributes)
2195 struct btrace_config *conf;
2196 struct gdb_xml_value *size;
2198 conf = (struct btrace_config *) user_data;
2199 conf->format = BTRACE_FORMAT_BTS;
2202 size = xml_find_attribute (attributes, "size");
2204 conf->bts.size = (unsigned int) *(ULONGEST *) size->value;
2207 /* Parse a btrace-conf "pt" xml record. */
2210 parse_xml_btrace_conf_pt (struct gdb_xml_parser *parser,
2211 const struct gdb_xml_element *element,
2212 void *user_data, VEC (gdb_xml_value_s) *attributes)
2214 struct btrace_config *conf;
2215 struct gdb_xml_value *size;
2217 conf = (struct btrace_config *) user_data;
2218 conf->format = BTRACE_FORMAT_PT;
2221 size = xml_find_attribute (attributes, "size");
2223 conf->pt.size = (unsigned int) *(ULONGEST *) size->value;
2226 static const struct gdb_xml_attribute btrace_conf_pt_attributes[] = {
2227 { "size", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
2228 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2231 static const struct gdb_xml_attribute btrace_conf_bts_attributes[] = {
2232 { "size", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
2233 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2236 static const struct gdb_xml_element btrace_conf_children[] = {
2237 { "bts", btrace_conf_bts_attributes, NULL, GDB_XML_EF_OPTIONAL,
2238 parse_xml_btrace_conf_bts, NULL },
2239 { "pt", btrace_conf_pt_attributes, NULL, GDB_XML_EF_OPTIONAL,
2240 parse_xml_btrace_conf_pt, NULL },
2241 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2244 static const struct gdb_xml_attribute btrace_conf_attributes[] = {
2245 { "version", GDB_XML_AF_NONE, NULL, NULL },
2246 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2249 static const struct gdb_xml_element btrace_conf_elements[] = {
2250 { "btrace-conf", btrace_conf_attributes, btrace_conf_children,
2251 GDB_XML_EF_NONE, NULL, NULL },
2252 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2255 #endif /* defined (HAVE_LIBEXPAT) */
2260 parse_xml_btrace_conf (struct btrace_config *conf, const char *xml)
2264 #if defined (HAVE_LIBEXPAT)
2266 errcode = gdb_xml_parse_quick (_("btrace-conf"), "btrace-conf.dtd",
2267 btrace_conf_elements, xml, conf);
2269 error (_("Error parsing branch trace configuration."));
2271 #else /* !defined (HAVE_LIBEXPAT) */
2273 error (_("XML parsing is not supported."));
2275 #endif /* !defined (HAVE_LIBEXPAT) */
2280 const struct btrace_insn *
2281 btrace_insn_get (const struct btrace_insn_iterator *it)
2283 const struct btrace_function *bfun;
2284 unsigned int index, end;
2286 index = it->insn_index;
2287 bfun = it->btinfo->functions[it->call_index];
2289 /* Check if the iterator points to a gap in the trace. */
2290 if (bfun->errcode != 0)
2293 /* The index is within the bounds of this function's instruction vector. */
2294 end = VEC_length (btrace_insn_s, bfun->insn);
2295 gdb_assert (0 < end);
2296 gdb_assert (index < end);
2298 return VEC_index (btrace_insn_s, bfun->insn, index);
2304 btrace_insn_get_error (const struct btrace_insn_iterator *it)
2306 const struct btrace_function *bfun;
2308 bfun = it->btinfo->functions[it->call_index];
2309 return bfun->errcode;
2315 btrace_insn_number (const struct btrace_insn_iterator *it)
2317 const struct btrace_function *bfun;
2319 bfun = it->btinfo->functions[it->call_index];
2320 return bfun->insn_offset + it->insn_index;
2326 btrace_insn_begin (struct btrace_insn_iterator *it,
2327 const struct btrace_thread_info *btinfo)
2329 if (btinfo->functions.empty ())
2330 error (_("No trace."));
2332 it->btinfo = btinfo;
2340 btrace_insn_end (struct btrace_insn_iterator *it,
2341 const struct btrace_thread_info *btinfo)
2343 const struct btrace_function *bfun;
2344 unsigned int length;
2346 if (btinfo->functions.empty ())
2347 error (_("No trace."));
2349 bfun = btinfo->functions.back ();
2350 length = VEC_length (btrace_insn_s, bfun->insn);
2352 /* The last function may either be a gap or it contains the current
2353 instruction, which is one past the end of the execution trace; ignore
2358 it->btinfo = btinfo;
2359 it->call_index = bfun->number - 1;
2360 it->insn_index = length;
2366 btrace_insn_next (struct btrace_insn_iterator *it, unsigned int stride)
2368 const struct btrace_function *bfun;
2369 unsigned int index, steps;
2371 bfun = it->btinfo->functions[it->call_index];
2373 index = it->insn_index;
2377 unsigned int end, space, adv;
2379 end = VEC_length (btrace_insn_s, bfun->insn);
2381 /* An empty function segment represents a gap in the trace. We count
2382 it as one instruction. */
2385 const struct btrace_function *next;
2387 next = ftrace_find_call_by_number (it->btinfo, bfun->number + 1);
2400 gdb_assert (0 < end);
2401 gdb_assert (index < end);
2403 /* Compute the number of instructions remaining in this segment. */
2404 space = end - index;
2406 /* Advance the iterator as far as possible within this segment. */
2407 adv = std::min (space, stride);
2412 /* Move to the next function if we're at the end of this one. */
2415 const struct btrace_function *next;
2417 next = ftrace_find_call_by_number (it->btinfo, bfun->number + 1);
2420 /* We stepped past the last function.
2422 Let's adjust the index to point to the last instruction in
2423 the previous function. */
2429 /* We now point to the first instruction in the new function. */
2434 /* We did make progress. */
2435 gdb_assert (adv > 0);
2438 /* Update the iterator. */
2439 it->call_index = bfun->number - 1;
2440 it->insn_index = index;
2448 btrace_insn_prev (struct btrace_insn_iterator *it, unsigned int stride)
2450 const struct btrace_function *bfun;
2451 unsigned int index, steps;
2453 bfun = it->btinfo->functions[it->call_index];
2455 index = it->insn_index;
2461 /* Move to the previous function if we're at the start of this one. */
2464 const struct btrace_function *prev;
2466 prev = ftrace_find_call_by_number (it->btinfo, bfun->number - 1);
2470 /* We point to one after the last instruction in the new function. */
2472 index = VEC_length (btrace_insn_s, bfun->insn);
2474 /* An empty function segment represents a gap in the trace. We count
2475 it as one instruction. */
2485 /* Advance the iterator as far as possible within this segment. */
2486 adv = std::min (index, stride);
2492 /* We did make progress. */
2493 gdb_assert (adv > 0);
2496 /* Update the iterator. */
2497 it->call_index = bfun->number - 1;
2498 it->insn_index = index;
2506 btrace_insn_cmp (const struct btrace_insn_iterator *lhs,
2507 const struct btrace_insn_iterator *rhs)
2509 gdb_assert (lhs->btinfo == rhs->btinfo);
2511 if (lhs->call_index != rhs->call_index)
2512 return lhs->call_index - rhs->call_index;
2514 return lhs->insn_index - rhs->insn_index;
2520 btrace_find_insn_by_number (struct btrace_insn_iterator *it,
2521 const struct btrace_thread_info *btinfo,
2522 unsigned int number)
2524 const struct btrace_function *bfun;
2525 unsigned int upper, lower;
2527 if (btinfo->functions.empty ())
2531 bfun = btinfo->functions[lower];
2532 if (number < bfun->insn_offset)
2535 upper = btinfo->functions.size () - 1;
2536 bfun = btinfo->functions[upper];
2537 if (number >= bfun->insn_offset + ftrace_call_num_insn (bfun))
2540 /* We assume that there are no holes in the numbering. */
2543 const unsigned int average = lower + (upper - lower) / 2;
2545 bfun = btinfo->functions[average];
2547 if (number < bfun->insn_offset)
2549 upper = average - 1;
2553 if (number >= bfun->insn_offset + ftrace_call_num_insn (bfun))
2555 lower = average + 1;
2562 it->btinfo = btinfo;
2563 it->call_index = bfun->number - 1;
2564 it->insn_index = number - bfun->insn_offset;
2568 /* Returns true if the recording ends with a function segment that
2569 contains only a single (i.e. the current) instruction. */
2572 btrace_ends_with_single_insn (const struct btrace_thread_info *btinfo)
2574 const btrace_function *bfun;
2576 if (btinfo->functions.empty ())
2579 bfun = btinfo->functions.back ();
2580 if (bfun->errcode != 0)
2583 return ftrace_call_num_insn (bfun) == 1;
2588 const struct btrace_function *
2589 btrace_call_get (const struct btrace_call_iterator *it)
2591 if (it->index >= it->btinfo->functions.size ())
2594 return it->btinfo->functions[it->index];
2600 btrace_call_number (const struct btrace_call_iterator *it)
2602 const unsigned int length = it->btinfo->functions.size ();
2604 /* If the last function segment contains only a single instruction (i.e. the
2605 current instruction), skip it. */
2606 if ((it->index == length) && btrace_ends_with_single_insn (it->btinfo))
2609 return it->index + 1;
2615 btrace_call_begin (struct btrace_call_iterator *it,
2616 const struct btrace_thread_info *btinfo)
2618 if (btinfo->functions.empty ())
2619 error (_("No trace."));
2621 it->btinfo = btinfo;
2628 btrace_call_end (struct btrace_call_iterator *it,
2629 const struct btrace_thread_info *btinfo)
2631 if (btinfo->functions.empty ())
2632 error (_("No trace."));
2634 it->btinfo = btinfo;
2635 it->index = btinfo->functions.size ();
2641 btrace_call_next (struct btrace_call_iterator *it, unsigned int stride)
2643 const unsigned int length = it->btinfo->functions.size ();
2645 if (it->index + stride < length - 1)
2646 /* Default case: Simply advance the iterator. */
2647 it->index += stride;
2648 else if (it->index + stride == length - 1)
2650 /* We land exactly at the last function segment. If it contains only one
2651 instruction (i.e. the current instruction) it is not actually part of
2653 if (btrace_ends_with_single_insn (it->btinfo))
2656 it->index = length - 1;
2660 /* We land past the last function segment and have to adjust the stride.
2661 If the last function segment contains only one instruction (i.e. the
2662 current instruction) it is not actually part of the trace. */
2663 if (btrace_ends_with_single_insn (it->btinfo))
2664 stride = length - it->index - 1;
2666 stride = length - it->index;
2677 btrace_call_prev (struct btrace_call_iterator *it, unsigned int stride)
2679 const unsigned int length = it->btinfo->functions.size ();
2682 gdb_assert (it->index <= length);
2684 if (stride == 0 || it->index == 0)
2687 /* If we are at the end, the first step is a special case. If the last
2688 function segment contains only one instruction (i.e. the current
2689 instruction) it is not actually part of the trace. To be able to step
2690 over this instruction, we need at least one more function segment. */
2691 if ((it->index == length) && (length > 1))
2693 if (btrace_ends_with_single_insn (it->btinfo))
2694 it->index = length - 2;
2696 it->index = length - 1;
2702 stride = std::min (stride, it->index);
2704 it->index -= stride;
2705 return steps + stride;
2711 btrace_call_cmp (const struct btrace_call_iterator *lhs,
2712 const struct btrace_call_iterator *rhs)
2714 gdb_assert (lhs->btinfo == rhs->btinfo);
2715 return (int) (lhs->index - rhs->index);
2721 btrace_find_call_by_number (struct btrace_call_iterator *it,
2722 const struct btrace_thread_info *btinfo,
2723 unsigned int number)
2725 const unsigned int length = btinfo->functions.size ();
2727 if ((number == 0) || (number > length))
2730 it->btinfo = btinfo;
2731 it->index = number - 1;
2738 btrace_set_insn_history (struct btrace_thread_info *btinfo,
2739 const struct btrace_insn_iterator *begin,
2740 const struct btrace_insn_iterator *end)
2742 if (btinfo->insn_history == NULL)
2743 btinfo->insn_history = XCNEW (struct btrace_insn_history);
2745 btinfo->insn_history->begin = *begin;
2746 btinfo->insn_history->end = *end;
2752 btrace_set_call_history (struct btrace_thread_info *btinfo,
2753 const struct btrace_call_iterator *begin,
2754 const struct btrace_call_iterator *end)
2756 gdb_assert (begin->btinfo == end->btinfo);
2758 if (btinfo->call_history == NULL)
2759 btinfo->call_history = XCNEW (struct btrace_call_history);
2761 btinfo->call_history->begin = *begin;
2762 btinfo->call_history->end = *end;
2768 btrace_is_replaying (struct thread_info *tp)
2770 return tp->btrace.replay != NULL;
2776 btrace_is_empty (struct thread_info *tp)
2778 struct btrace_insn_iterator begin, end;
2779 struct btrace_thread_info *btinfo;
2781 btinfo = &tp->btrace;
2783 if (btinfo->functions.empty ())
2786 btrace_insn_begin (&begin, btinfo);
2787 btrace_insn_end (&end, btinfo);
2789 return btrace_insn_cmp (&begin, &end) == 0;
2792 /* Forward the cleanup request. */
2795 do_btrace_data_cleanup (void *arg)
2797 btrace_data_fini ((struct btrace_data *) arg);
2803 make_cleanup_btrace_data (struct btrace_data *data)
2805 return make_cleanup (do_btrace_data_cleanup, data);
2808 #if defined (HAVE_LIBIPT)
2810 /* Print a single packet. */
2813 pt_print_packet (const struct pt_packet *packet)
2815 switch (packet->type)
2818 printf_unfiltered (("[??: %x]"), packet->type);
2822 printf_unfiltered (("psb"));
2826 printf_unfiltered (("psbend"));
2830 printf_unfiltered (("pad"));
2834 printf_unfiltered (("tip %u: 0x%" PRIx64 ""),
2835 packet->payload.ip.ipc,
2836 packet->payload.ip.ip);
2840 printf_unfiltered (("tip.pge %u: 0x%" PRIx64 ""),
2841 packet->payload.ip.ipc,
2842 packet->payload.ip.ip);
2846 printf_unfiltered (("tip.pgd %u: 0x%" PRIx64 ""),
2847 packet->payload.ip.ipc,
2848 packet->payload.ip.ip);
2852 printf_unfiltered (("fup %u: 0x%" PRIx64 ""),
2853 packet->payload.ip.ipc,
2854 packet->payload.ip.ip);
2858 printf_unfiltered (("tnt-8 %u: 0x%" PRIx64 ""),
2859 packet->payload.tnt.bit_size,
2860 packet->payload.tnt.payload);
2864 printf_unfiltered (("tnt-64 %u: 0x%" PRIx64 ""),
2865 packet->payload.tnt.bit_size,
2866 packet->payload.tnt.payload);
2870 printf_unfiltered (("pip %" PRIx64 "%s"), packet->payload.pip.cr3,
2871 packet->payload.pip.nr ? (" nr") : (""));
2875 printf_unfiltered (("tsc %" PRIx64 ""), packet->payload.tsc.tsc);
2879 printf_unfiltered (("cbr %u"), packet->payload.cbr.ratio);
2883 switch (packet->payload.mode.leaf)
2886 printf_unfiltered (("mode %u"), packet->payload.mode.leaf);
2890 printf_unfiltered (("mode.exec%s%s"),
2891 packet->payload.mode.bits.exec.csl
2893 packet->payload.mode.bits.exec.csd
2894 ? (" cs.d") : (""));
2898 printf_unfiltered (("mode.tsx%s%s"),
2899 packet->payload.mode.bits.tsx.intx
2901 packet->payload.mode.bits.tsx.abrt
2902 ? (" abrt") : (""));
2908 printf_unfiltered (("ovf"));
2912 printf_unfiltered (("stop"));
2916 printf_unfiltered (("vmcs %" PRIx64 ""), packet->payload.vmcs.base);
2920 printf_unfiltered (("tma %x %x"), packet->payload.tma.ctc,
2921 packet->payload.tma.fc);
2925 printf_unfiltered (("mtc %x"), packet->payload.mtc.ctc);
2929 printf_unfiltered (("cyc %" PRIx64 ""), packet->payload.cyc.value);
2933 printf_unfiltered (("mnt %" PRIx64 ""), packet->payload.mnt.payload);
2938 /* Decode packets into MAINT using DECODER. */
2941 btrace_maint_decode_pt (struct btrace_maint_info *maint,
2942 struct pt_packet_decoder *decoder)
2948 struct btrace_pt_packet packet;
2950 errcode = pt_pkt_sync_forward (decoder);
2956 pt_pkt_get_offset (decoder, &packet.offset);
2958 errcode = pt_pkt_next (decoder, &packet.packet,
2959 sizeof(packet.packet));
2963 if (maint_btrace_pt_skip_pad == 0 || packet.packet.type != ppt_pad)
2965 packet.errcode = pt_errcode (errcode);
2966 VEC_safe_push (btrace_pt_packet_s, maint->variant.pt.packets,
2971 if (errcode == -pte_eos)
2974 packet.errcode = pt_errcode (errcode);
2975 VEC_safe_push (btrace_pt_packet_s, maint->variant.pt.packets,
2978 warning (_("Error at trace offset 0x%" PRIx64 ": %s."),
2979 packet.offset, pt_errstr (packet.errcode));
2982 if (errcode != -pte_eos)
2983 warning (_("Failed to synchronize onto the Intel Processor Trace "
2984 "stream: %s."), pt_errstr (pt_errcode (errcode)));
2987 /* Update the packet history in BTINFO. */
2990 btrace_maint_update_pt_packets (struct btrace_thread_info *btinfo)
2992 volatile struct gdb_exception except;
2993 struct pt_packet_decoder *decoder;
2994 struct btrace_data_pt *pt;
2995 struct pt_config config;
2998 pt = &btinfo->data.variant.pt;
3000 /* Nothing to do if there is no trace. */
3004 memset (&config, 0, sizeof(config));
3006 config.size = sizeof (config);
3007 config.begin = pt->data;
3008 config.end = pt->data + pt->size;
3010 config.cpu.vendor = pt_translate_cpu_vendor (pt->config.cpu.vendor);
3011 config.cpu.family = pt->config.cpu.family;
3012 config.cpu.model = pt->config.cpu.model;
3013 config.cpu.stepping = pt->config.cpu.stepping;
3015 errcode = pt_cpu_errata (&config.errata, &config.cpu);
3017 error (_("Failed to configure the Intel Processor Trace decoder: %s."),
3018 pt_errstr (pt_errcode (errcode)));
3020 decoder = pt_pkt_alloc_decoder (&config);
3021 if (decoder == NULL)
3022 error (_("Failed to allocate the Intel Processor Trace decoder."));
3026 btrace_maint_decode_pt (&btinfo->maint, decoder);
3028 CATCH (except, RETURN_MASK_ALL)
3030 pt_pkt_free_decoder (decoder);
3032 if (except.reason < 0)
3033 throw_exception (except);
3037 pt_pkt_free_decoder (decoder);
3040 #endif /* !defined (HAVE_LIBIPT) */
3042 /* Update the packet maintenance information for BTINFO and store the
3043 low and high bounds into BEGIN and END, respectively.
3044 Store the current iterator state into FROM and TO. */
3047 btrace_maint_update_packets (struct btrace_thread_info *btinfo,
3048 unsigned int *begin, unsigned int *end,
3049 unsigned int *from, unsigned int *to)
3051 switch (btinfo->data.format)
3060 case BTRACE_FORMAT_BTS:
3061 /* Nothing to do - we operate directly on BTINFO->DATA. */
3063 *end = VEC_length (btrace_block_s, btinfo->data.variant.bts.blocks);
3064 *from = btinfo->maint.variant.bts.packet_history.begin;
3065 *to = btinfo->maint.variant.bts.packet_history.end;
3068 #if defined (HAVE_LIBIPT)
3069 case BTRACE_FORMAT_PT:
3070 if (VEC_empty (btrace_pt_packet_s, btinfo->maint.variant.pt.packets))
3071 btrace_maint_update_pt_packets (btinfo);
3074 *end = VEC_length (btrace_pt_packet_s, btinfo->maint.variant.pt.packets);
3075 *from = btinfo->maint.variant.pt.packet_history.begin;
3076 *to = btinfo->maint.variant.pt.packet_history.end;
3078 #endif /* defined (HAVE_LIBIPT) */
3082 /* Print packets in BTINFO from BEGIN (inclusive) until END (exclusive) and
3083 update the current iterator position. */
3086 btrace_maint_print_packets (struct btrace_thread_info *btinfo,
3087 unsigned int begin, unsigned int end)
3089 switch (btinfo->data.format)
3094 case BTRACE_FORMAT_BTS:
3096 VEC (btrace_block_s) *blocks;
3099 blocks = btinfo->data.variant.bts.blocks;
3100 for (blk = begin; blk < end; ++blk)
3102 const btrace_block_s *block;
3104 block = VEC_index (btrace_block_s, blocks, blk);
3106 printf_unfiltered ("%u\tbegin: %s, end: %s\n", blk,
3107 core_addr_to_string_nz (block->begin),
3108 core_addr_to_string_nz (block->end));
3111 btinfo->maint.variant.bts.packet_history.begin = begin;
3112 btinfo->maint.variant.bts.packet_history.end = end;
3116 #if defined (HAVE_LIBIPT)
3117 case BTRACE_FORMAT_PT:
3119 VEC (btrace_pt_packet_s) *packets;
3122 packets = btinfo->maint.variant.pt.packets;
3123 for (pkt = begin; pkt < end; ++pkt)
3125 const struct btrace_pt_packet *packet;
3127 packet = VEC_index (btrace_pt_packet_s, packets, pkt);
3129 printf_unfiltered ("%u\t", pkt);
3130 printf_unfiltered ("0x%" PRIx64 "\t", packet->offset);
3132 if (packet->errcode == pte_ok)
3133 pt_print_packet (&packet->packet);
3135 printf_unfiltered ("[error: %s]", pt_errstr (packet->errcode));
3137 printf_unfiltered ("\n");
3140 btinfo->maint.variant.pt.packet_history.begin = begin;
3141 btinfo->maint.variant.pt.packet_history.end = end;
3144 #endif /* defined (HAVE_LIBIPT) */
3148 /* Read a number from an argument string. */
3151 get_uint (char **arg)
3153 char *begin, *end, *pos;
3154 unsigned long number;
3157 pos = skip_spaces (begin);
3159 if (!isdigit (*pos))
3160 error (_("Expected positive number, got: %s."), pos);
3162 number = strtoul (pos, &end, 10);
3163 if (number > UINT_MAX)
3164 error (_("Number too big."));
3166 *arg += (end - begin);
3168 return (unsigned int) number;
3171 /* Read a context size from an argument string. */
3174 get_context_size (char **arg)
3179 pos = skip_spaces (*arg);
3181 if (!isdigit (*pos))
3182 error (_("Expected positive number, got: %s."), pos);
3184 return strtol (pos, arg, 10);
3187 /* Complain about junk at the end of an argument string. */
3190 no_chunk (char *arg)
3193 error (_("Junk after argument: %s."), arg);
3196 /* The "maintenance btrace packet-history" command. */
3199 maint_btrace_packet_history_cmd (char *arg, int from_tty)
3201 struct btrace_thread_info *btinfo;
3202 struct thread_info *tp;
3203 unsigned int size, begin, end, from, to;
3205 tp = find_thread_ptid (inferior_ptid);
3207 error (_("No thread."));
3210 btinfo = &tp->btrace;
3212 btrace_maint_update_packets (btinfo, &begin, &end, &from, &to);
3215 printf_unfiltered (_("No trace.\n"));
3219 if (arg == NULL || *arg == 0 || strcmp (arg, "+") == 0)
3223 if (end - from < size)
3227 else if (strcmp (arg, "-") == 0)
3231 if (to - begin < size)
3237 from = get_uint (&arg);
3239 error (_("'%u' is out of range."), from);
3241 arg = skip_spaces (arg);
3244 arg = skip_spaces (++arg);
3249 size = get_context_size (&arg);
3253 if (end - from < size)
3257 else if (*arg == '-')
3260 size = get_context_size (&arg);
3264 /* Include the packet given as first argument. */
3268 if (to - begin < size)
3274 to = get_uint (&arg);
3276 /* Include the packet at the second argument and silently
3277 truncate the range. */
3290 if (end - from < size)
3298 btrace_maint_print_packets (btinfo, from, to);
3301 /* The "maintenance btrace clear-packet-history" command. */
3304 maint_btrace_clear_packet_history_cmd (char *args, int from_tty)
3306 struct btrace_thread_info *btinfo;
3307 struct thread_info *tp;
3309 if (args != NULL && *args != 0)
3310 error (_("Invalid argument."));
3312 tp = find_thread_ptid (inferior_ptid);
3314 error (_("No thread."));
3316 btinfo = &tp->btrace;
3318 /* Must clear the maint data before - it depends on BTINFO->DATA. */
3319 btrace_maint_clear (btinfo);
3320 btrace_data_clear (&btinfo->data);
3323 /* The "maintenance btrace clear" command. */
3326 maint_btrace_clear_cmd (char *args, int from_tty)
3328 struct btrace_thread_info *btinfo;
3329 struct thread_info *tp;
3331 if (args != NULL && *args != 0)
3332 error (_("Invalid argument."));
3334 tp = find_thread_ptid (inferior_ptid);
3336 error (_("No thread."));
3341 /* The "maintenance btrace" command. */
3344 maint_btrace_cmd (char *args, int from_tty)
3346 help_list (maint_btrace_cmdlist, "maintenance btrace ", all_commands,
3350 /* The "maintenance set btrace" command. */
3353 maint_btrace_set_cmd (char *args, int from_tty)
3355 help_list (maint_btrace_set_cmdlist, "maintenance set btrace ", all_commands,
3359 /* The "maintenance show btrace" command. */
3362 maint_btrace_show_cmd (char *args, int from_tty)
3364 help_list (maint_btrace_show_cmdlist, "maintenance show btrace ",
3365 all_commands, gdb_stdout);
3368 /* The "maintenance set btrace pt" command. */
3371 maint_btrace_pt_set_cmd (char *args, int from_tty)
3373 help_list (maint_btrace_pt_set_cmdlist, "maintenance set btrace pt ",
3374 all_commands, gdb_stdout);
3377 /* The "maintenance show btrace pt" command. */
3380 maint_btrace_pt_show_cmd (char *args, int from_tty)
3382 help_list (maint_btrace_pt_show_cmdlist, "maintenance show btrace pt ",
3383 all_commands, gdb_stdout);
3386 /* The "maintenance info btrace" command. */
3389 maint_info_btrace_cmd (char *args, int from_tty)
3391 struct btrace_thread_info *btinfo;
3392 struct thread_info *tp;
3393 const struct btrace_config *conf;
3395 if (args != NULL && *args != 0)
3396 error (_("Invalid argument."));
3398 tp = find_thread_ptid (inferior_ptid);
3400 error (_("No thread."));
3402 btinfo = &tp->btrace;
3404 conf = btrace_conf (btinfo);
3406 error (_("No btrace configuration."));
3408 printf_unfiltered (_("Format: %s.\n"),
3409 btrace_format_string (conf->format));
3411 switch (conf->format)
3416 case BTRACE_FORMAT_BTS:
3417 printf_unfiltered (_("Number of packets: %u.\n"),
3418 VEC_length (btrace_block_s,
3419 btinfo->data.variant.bts.blocks));
3422 #if defined (HAVE_LIBIPT)
3423 case BTRACE_FORMAT_PT:
3425 struct pt_version version;
3427 version = pt_library_version ();
3428 printf_unfiltered (_("Version: %u.%u.%u%s.\n"), version.major,
3429 version.minor, version.build,
3430 version.ext != NULL ? version.ext : "");
3432 btrace_maint_update_pt_packets (btinfo);
3433 printf_unfiltered (_("Number of packets: %u.\n"),
3434 VEC_length (btrace_pt_packet_s,
3435 btinfo->maint.variant.pt.packets));
3438 #endif /* defined (HAVE_LIBIPT) */
3442 /* The "maint show btrace pt skip-pad" show value function. */
3445 show_maint_btrace_pt_skip_pad (struct ui_file *file, int from_tty,
3446 struct cmd_list_element *c,
3449 fprintf_filtered (file, _("Skip PAD packets is %s.\n"), value);
3453 /* Initialize btrace maintenance commands. */
3455 void _initialize_btrace (void);
3457 _initialize_btrace (void)
3459 add_cmd ("btrace", class_maintenance, maint_info_btrace_cmd,
3460 _("Info about branch tracing data."), &maintenanceinfolist);
3462 add_prefix_cmd ("btrace", class_maintenance, maint_btrace_cmd,
3463 _("Branch tracing maintenance commands."),
3464 &maint_btrace_cmdlist, "maintenance btrace ",
3465 0, &maintenancelist);
3467 add_prefix_cmd ("btrace", class_maintenance, maint_btrace_set_cmd, _("\
3468 Set branch tracing specific variables."),
3469 &maint_btrace_set_cmdlist, "maintenance set btrace ",
3470 0, &maintenance_set_cmdlist);
3472 add_prefix_cmd ("pt", class_maintenance, maint_btrace_pt_set_cmd, _("\
3473 Set Intel Processor Trace specific variables."),
3474 &maint_btrace_pt_set_cmdlist, "maintenance set btrace pt ",
3475 0, &maint_btrace_set_cmdlist);
3477 add_prefix_cmd ("btrace", class_maintenance, maint_btrace_show_cmd, _("\
3478 Show branch tracing specific variables."),
3479 &maint_btrace_show_cmdlist, "maintenance show btrace ",
3480 0, &maintenance_show_cmdlist);
3482 add_prefix_cmd ("pt", class_maintenance, maint_btrace_pt_show_cmd, _("\
3483 Show Intel Processor Trace specific variables."),
3484 &maint_btrace_pt_show_cmdlist, "maintenance show btrace pt ",
3485 0, &maint_btrace_show_cmdlist);
3487 add_setshow_boolean_cmd ("skip-pad", class_maintenance,
3488 &maint_btrace_pt_skip_pad, _("\
3489 Set whether PAD packets should be skipped in the btrace packet history."), _("\
3490 Show whether PAD packets should be skipped in the btrace packet history."),_("\
3491 When enabled, PAD packets are ignored in the btrace packet history."),
3492 NULL, show_maint_btrace_pt_skip_pad,
3493 &maint_btrace_pt_set_cmdlist,
3494 &maint_btrace_pt_show_cmdlist);
3496 add_cmd ("packet-history", class_maintenance, maint_btrace_packet_history_cmd,
3497 _("Print the raw branch tracing data.\n\
3498 With no argument, print ten more packets after the previous ten-line print.\n\
3499 With '-' as argument print ten packets before a previous ten-line print.\n\
3500 One argument specifies the starting packet of a ten-line print.\n\
3501 Two arguments with comma between specify starting and ending packets to \
3503 Preceded with '+'/'-' the second argument specifies the distance from the \
3505 &maint_btrace_cmdlist);
3507 add_cmd ("clear-packet-history", class_maintenance,
3508 maint_btrace_clear_packet_history_cmd,
3509 _("Clears the branch tracing packet history.\n\
3510 Discards the raw branch tracing data but not the execution history data.\n\
3512 &maint_btrace_cmdlist);
3514 add_cmd ("clear", class_maintenance, maint_btrace_clear_cmd,
3515 _("Clears the branch tracing data.\n\
3516 Discards the raw branch tracing data and the execution history data.\n\
3517 The next 'record' command will fetch the branch tracing data anew.\n\
3519 &maint_btrace_cmdlist);