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 (const struct btrace_thread_info *btinfo,
162 if (number == 0 || number > btinfo->functions.size ())
165 return btinfo->functions[number - 1];
168 /* Return non-zero if BFUN does not match MFUN and FUN,
169 return zero otherwise. */
172 ftrace_function_switched (const struct btrace_function *bfun,
173 const struct minimal_symbol *mfun,
174 const struct symbol *fun)
176 struct minimal_symbol *msym;
182 /* If the minimal symbol changed, we certainly switched functions. */
183 if (mfun != NULL && msym != NULL
184 && strcmp (MSYMBOL_LINKAGE_NAME (mfun), MSYMBOL_LINKAGE_NAME (msym)) != 0)
187 /* If the symbol changed, we certainly switched functions. */
188 if (fun != NULL && sym != NULL)
190 const char *bfname, *fname;
192 /* Check the function name. */
193 if (strcmp (SYMBOL_LINKAGE_NAME (fun), SYMBOL_LINKAGE_NAME (sym)) != 0)
196 /* Check the location of those functions, as well. */
197 bfname = symtab_to_fullname (symbol_symtab (sym));
198 fname = symtab_to_fullname (symbol_symtab (fun));
199 if (filename_cmp (fname, bfname) != 0)
203 /* If we lost symbol information, we switched functions. */
204 if (!(msym == NULL && sym == NULL) && mfun == NULL && fun == NULL)
207 /* If we gained symbol information, we switched functions. */
208 if (msym == NULL && sym == NULL && !(mfun == NULL && fun == NULL))
214 /* Allocate and initialize a new branch trace function segment at the end of
216 BTINFO is the branch trace information for the current thread.
217 MFUN and FUN are the symbol information we have for this function. */
219 static struct btrace_function *
220 ftrace_new_function (struct btrace_thread_info *btinfo,
221 struct minimal_symbol *mfun,
224 struct btrace_function *bfun;
226 bfun = XCNEW (struct btrace_function);
231 if (btinfo->functions.empty ())
233 /* Start counting at one. */
235 bfun->insn_offset = 1;
239 struct btrace_function *prev = btinfo->functions.back ();
241 bfun->number = prev->number + 1;
242 bfun->insn_offset = prev->insn_offset + ftrace_call_num_insn (prev);
243 bfun->level = prev->level;
246 btinfo->functions.push_back (bfun);
250 /* Update the UP field of a function segment. */
253 ftrace_update_caller (struct btrace_function *bfun,
254 struct btrace_function *caller,
255 enum btrace_function_flag flags)
258 ftrace_debug (bfun, "updating caller");
260 bfun->up = caller->number;
263 ftrace_debug (bfun, "set caller");
264 ftrace_debug (caller, "..to");
267 /* Fix up the caller for all segments of a function. */
270 ftrace_fixup_caller (struct btrace_thread_info *btinfo,
271 struct btrace_function *bfun,
272 struct btrace_function *caller,
273 enum btrace_function_flag flags)
275 unsigned int prev, next;
279 ftrace_update_caller (bfun, caller, flags);
281 /* Update all function segments belonging to the same function. */
282 for (; prev != 0; prev = bfun->prev)
284 bfun = ftrace_find_call_by_number (btinfo, prev);
285 ftrace_update_caller (bfun, caller, flags);
288 for (; next != 0; next = bfun->next)
290 bfun = ftrace_find_call_by_number (btinfo, next);
291 ftrace_update_caller (bfun, caller, flags);
295 /* Add a new function segment for a call at the end of the trace.
296 BTINFO is the branch trace information for the current thread.
297 MFUN and FUN are the symbol information we have for this function. */
299 static struct btrace_function *
300 ftrace_new_call (struct btrace_thread_info *btinfo,
301 struct minimal_symbol *mfun,
304 const unsigned int length = btinfo->functions.size ();
305 struct btrace_function *bfun = ftrace_new_function (btinfo, mfun, fun);
310 ftrace_debug (bfun, "new call");
315 /* Add a new function segment for a tail call at the end of the trace.
316 BTINFO is the branch trace information for the current thread.
317 MFUN and FUN are the symbol information we have for this function. */
319 static struct btrace_function *
320 ftrace_new_tailcall (struct btrace_thread_info *btinfo,
321 struct minimal_symbol *mfun,
324 const unsigned int length = btinfo->functions.size ();
325 struct btrace_function *bfun = ftrace_new_function (btinfo, mfun, fun);
329 bfun->flags |= BFUN_UP_LINKS_TO_TAILCALL;
331 ftrace_debug (bfun, "new tail call");
336 /* Return the caller of BFUN or NULL if there is none. This function skips
337 tail calls in the call chain. BTINFO is the branch trace information for
338 the current thread. */
339 static struct btrace_function *
340 ftrace_get_caller (struct btrace_thread_info *btinfo,
341 struct btrace_function *bfun)
343 for (; bfun != NULL; bfun = ftrace_find_call_by_number (btinfo, bfun->up))
344 if ((bfun->flags & BFUN_UP_LINKS_TO_TAILCALL) == 0)
345 return ftrace_find_call_by_number (btinfo, bfun->up);
350 /* Find the innermost caller in the back trace of BFUN with MFUN/FUN
351 symbol information. BTINFO is the branch trace information for the current
354 static struct btrace_function *
355 ftrace_find_caller (struct btrace_thread_info *btinfo,
356 struct btrace_function *bfun,
357 struct minimal_symbol *mfun,
360 for (; bfun != NULL; bfun = ftrace_find_call_by_number (btinfo, bfun->up))
362 /* Skip functions with incompatible symbol information. */
363 if (ftrace_function_switched (bfun, mfun, fun))
366 /* This is the function segment we're looking for. */
373 /* Find the innermost caller in the back trace of BFUN, skipping all
374 function segments that do not end with a call instruction (e.g.
375 tail calls ending with a jump). BTINFO is the branch trace information for
376 the current thread. */
378 static struct btrace_function *
379 ftrace_find_call (struct btrace_thread_info *btinfo,
380 struct btrace_function *bfun)
382 for (; bfun != NULL; bfun = ftrace_find_call_by_number (btinfo, bfun->up))
384 struct btrace_insn *last;
387 if (bfun->errcode != 0)
390 last = VEC_last (btrace_insn_s, bfun->insn);
392 if (last->iclass == BTRACE_INSN_CALL)
399 /* Add a continuation segment for a function into which we return at the end of
401 BTINFO is the branch trace information for the current thread.
402 MFUN and FUN are the symbol information we have for this function. */
404 static struct btrace_function *
405 ftrace_new_return (struct btrace_thread_info *btinfo,
406 struct minimal_symbol *mfun,
409 struct btrace_function *prev = btinfo->functions.back ();
410 struct btrace_function *bfun, *caller;
412 bfun = ftrace_new_function (btinfo, mfun, fun);
414 /* It is important to start at PREV's caller. Otherwise, we might find
415 PREV itself, if PREV is a recursive function. */
416 caller = ftrace_find_call_by_number (btinfo, prev->up);
417 caller = ftrace_find_caller (btinfo, caller, mfun, fun);
420 /* The caller of PREV is the preceding btrace function segment in this
421 function instance. */
422 gdb_assert (caller->next == 0);
424 caller->next = bfun->number;
425 bfun->prev = caller->number;
427 /* Maintain the function level. */
428 bfun->level = caller->level;
430 /* Maintain the call stack. */
431 bfun->up = caller->up;
432 bfun->flags = caller->flags;
434 ftrace_debug (bfun, "new return");
438 /* We did not find a caller. This could mean that something went
439 wrong or that the call is simply not included in the trace. */
441 /* Let's search for some actual call. */
442 caller = ftrace_find_call_by_number (btinfo, prev->up);
443 caller = ftrace_find_call (btinfo, caller);
446 /* There is no call in PREV's back trace. We assume that the
447 branch trace did not include it. */
449 /* Let's find the topmost function and add a new caller for it.
450 This should handle a series of initial tail calls. */
451 while (prev->up != 0)
452 prev = ftrace_find_call_by_number (btinfo, prev->up);
454 bfun->level = prev->level - 1;
456 /* Fix up the call stack for PREV. */
457 ftrace_fixup_caller (btinfo, prev, bfun, BFUN_UP_LINKS_TO_RET);
459 ftrace_debug (bfun, "new return - no caller");
463 /* There is a call in PREV's back trace to which we should have
464 returned but didn't. Let's start a new, separate back trace
465 from PREV's level. */
466 bfun->level = prev->level - 1;
468 /* We fix up the back trace for PREV but leave other function segments
469 on the same level as they are.
470 This should handle things like schedule () correctly where we're
471 switching contexts. */
472 prev->up = bfun->number;
473 prev->flags = BFUN_UP_LINKS_TO_RET;
475 ftrace_debug (bfun, "new return - unknown caller");
482 /* Add a new function segment for a function switch at the end of the trace.
483 BTINFO is the branch trace information for the current thread.
484 MFUN and FUN are the symbol information we have for this function. */
486 static struct btrace_function *
487 ftrace_new_switch (struct btrace_thread_info *btinfo,
488 struct minimal_symbol *mfun,
491 struct btrace_function *prev = btinfo->functions.back ();
492 struct btrace_function *bfun;
494 /* This is an unexplained function switch. We can't really be sure about the
495 call stack, yet the best I can think of right now is to preserve it. */
496 bfun = ftrace_new_function (btinfo, mfun, fun);
498 bfun->flags = prev->flags;
500 ftrace_debug (bfun, "new switch");
505 /* Add a new function segment for a gap in the trace due to a decode error at
506 the end of the trace.
507 BTINFO is the branch trace information for the current thread.
508 ERRCODE is the format-specific error code. */
510 static struct btrace_function *
511 ftrace_new_gap (struct btrace_thread_info *btinfo, int errcode,
512 std::vector<unsigned int> &gaps)
514 struct btrace_function *bfun;
516 if (btinfo->functions.empty ())
517 bfun = ftrace_new_function (btinfo, NULL, NULL);
520 /* We hijack the previous function segment if it was empty. */
521 bfun = btinfo->functions.back ();
522 if (bfun->errcode != 0 || !VEC_empty (btrace_insn_s, bfun->insn))
523 bfun = ftrace_new_function (btinfo, NULL, NULL);
526 bfun->errcode = errcode;
527 gaps.push_back (bfun->number);
529 ftrace_debug (bfun, "new gap");
534 /* Update the current function segment at the end of the trace in BTINFO with
535 respect to the instruction at PC. This may create new function segments.
536 Return the chronologically latest function segment, never NULL. */
538 static struct btrace_function *
539 ftrace_update_function (struct btrace_thread_info *btinfo, CORE_ADDR pc)
541 struct bound_minimal_symbol bmfun;
542 struct minimal_symbol *mfun;
544 struct btrace_insn *last;
545 struct btrace_function *bfun;
547 /* Try to determine the function we're in. We use both types of symbols
548 to avoid surprises when we sometimes get a full symbol and sometimes
549 only a minimal symbol. */
550 fun = find_pc_function (pc);
551 bmfun = lookup_minimal_symbol_by_pc (pc);
554 if (fun == NULL && mfun == NULL)
555 DEBUG_FTRACE ("no symbol at %s", core_addr_to_string_nz (pc));
557 /* If we didn't have a function, we create one. */
558 if (btinfo->functions.empty ())
559 return ftrace_new_function (btinfo, mfun, fun);
561 /* If we had a gap before, we create a function. */
562 bfun = btinfo->functions.back ();
563 if (bfun->errcode != 0)
564 return ftrace_new_function (btinfo, mfun, fun);
566 /* Check the last instruction, if we have one.
567 We do this check first, since it allows us to fill in the call stack
568 links in addition to the normal flow links. */
570 if (!VEC_empty (btrace_insn_s, bfun->insn))
571 last = VEC_last (btrace_insn_s, bfun->insn);
575 switch (last->iclass)
577 case BTRACE_INSN_RETURN:
581 /* On some systems, _dl_runtime_resolve returns to the resolved
582 function instead of jumping to it. From our perspective,
583 however, this is a tailcall.
584 If we treated it as return, we wouldn't be able to find the
585 resolved function in our stack back trace. Hence, we would
586 lose the current stack back trace and start anew with an empty
587 back trace. When the resolved function returns, we would then
588 create a stack back trace with the same function names but
589 different frame id's. This will confuse stepping. */
590 fname = ftrace_print_function_name (bfun);
591 if (strcmp (fname, "_dl_runtime_resolve") == 0)
592 return ftrace_new_tailcall (btinfo, mfun, fun);
594 return ftrace_new_return (btinfo, mfun, fun);
597 case BTRACE_INSN_CALL:
598 /* Ignore calls to the next instruction. They are used for PIC. */
599 if (last->pc + last->size == pc)
602 return ftrace_new_call (btinfo, mfun, fun);
604 case BTRACE_INSN_JUMP:
608 start = get_pc_function_start (pc);
610 /* A jump to the start of a function is (typically) a tail call. */
612 return ftrace_new_tailcall (btinfo, mfun, fun);
614 /* If we can't determine the function for PC, we treat a jump at
615 the end of the block as tail call if we're switching functions
616 and as an intra-function branch if we don't. */
617 if (start == 0 && ftrace_function_switched (bfun, mfun, fun))
618 return ftrace_new_tailcall (btinfo, mfun, fun);
625 /* Check if we're switching functions for some other reason. */
626 if (ftrace_function_switched (bfun, mfun, fun))
628 DEBUG_FTRACE ("switching from %s in %s at %s",
629 ftrace_print_insn_addr (last),
630 ftrace_print_function_name (bfun),
631 ftrace_print_filename (bfun));
633 return ftrace_new_switch (btinfo, mfun, fun);
639 /* Add the instruction at PC to BFUN's instructions. */
642 ftrace_update_insns (struct btrace_function *bfun,
643 const struct btrace_insn *insn)
645 VEC_safe_push (btrace_insn_s, bfun->insn, insn);
647 if (record_debug > 1)
648 ftrace_debug (bfun, "update insn");
651 /* Classify the instruction at PC. */
653 static enum btrace_insn_class
654 ftrace_classify_insn (struct gdbarch *gdbarch, CORE_ADDR pc)
656 enum btrace_insn_class iclass;
658 iclass = BTRACE_INSN_OTHER;
661 if (gdbarch_insn_is_call (gdbarch, pc))
662 iclass = BTRACE_INSN_CALL;
663 else if (gdbarch_insn_is_ret (gdbarch, pc))
664 iclass = BTRACE_INSN_RETURN;
665 else if (gdbarch_insn_is_jump (gdbarch, pc))
666 iclass = BTRACE_INSN_JUMP;
668 CATCH (error, RETURN_MASK_ERROR)
676 /* Try to match the back trace at LHS to the back trace at RHS. Returns the
677 number of matching function segments or zero if the back traces do not
678 match. BTINFO is the branch trace information for the current thread. */
681 ftrace_match_backtrace (struct btrace_thread_info *btinfo,
682 struct btrace_function *lhs,
683 struct btrace_function *rhs)
687 for (matches = 0; lhs != NULL && rhs != NULL; ++matches)
689 if (ftrace_function_switched (lhs, rhs->msym, rhs->sym))
692 lhs = ftrace_get_caller (btinfo, lhs);
693 rhs = ftrace_get_caller (btinfo, rhs);
699 /* Add ADJUSTMENT to the level of BFUN and succeeding function segments.
700 BTINFO is the branch trace information for the current thread. */
703 ftrace_fixup_level (struct btrace_thread_info *btinfo,
704 struct btrace_function *bfun, int adjustment)
709 DEBUG_FTRACE ("fixup level (%+d)", adjustment);
710 ftrace_debug (bfun, "..bfun");
714 bfun->level += adjustment;
715 bfun = ftrace_find_call_by_number (btinfo, bfun->number + 1);
719 /* Recompute the global level offset. Traverse the function trace and compute
720 the global level offset as the negative of the minimal function level. */
723 ftrace_compute_global_level_offset (struct btrace_thread_info *btinfo)
730 if (btinfo->functions.empty ())
733 unsigned int length = btinfo->functions.size() - 1;
734 for (unsigned int i = 0; i < length; ++i)
735 level = std::min (level, btinfo->functions[i]->level);
737 /* The last function segment contains the current instruction, which is not
738 really part of the trace. If it contains just this one instruction, we
739 ignore the segment. */
740 struct btrace_function *last = btinfo->functions.back();
741 if (VEC_length (btrace_insn_s, last->insn) != 1)
742 level = std::min (level, last->level);
744 DEBUG_FTRACE ("setting global level offset: %d", -level);
745 btinfo->level = -level;
748 /* Connect the function segments PREV and NEXT in a bottom-to-top walk as in
749 ftrace_connect_backtrace. BTINFO is the branch trace information for the
753 ftrace_connect_bfun (struct btrace_thread_info *btinfo,
754 struct btrace_function *prev,
755 struct btrace_function *next)
757 DEBUG_FTRACE ("connecting...");
758 ftrace_debug (prev, "..prev");
759 ftrace_debug (next, "..next");
761 /* The function segments are not yet connected. */
762 gdb_assert (prev->next == 0);
763 gdb_assert (next->prev == 0);
765 prev->next = next->number;
766 next->prev = prev->number;
768 /* We may have moved NEXT to a different function level. */
769 ftrace_fixup_level (btinfo, next, prev->level - next->level);
771 /* If we run out of back trace for one, let's use the other's. */
774 const btrace_function_flags flags = next->flags;
776 next = ftrace_find_call_by_number (btinfo, next->up);
779 DEBUG_FTRACE ("using next's callers");
780 ftrace_fixup_caller (btinfo, prev, next, flags);
783 else if (next->up == 0)
785 const btrace_function_flags flags = prev->flags;
787 prev = ftrace_find_call_by_number (btinfo, prev->up);
790 DEBUG_FTRACE ("using prev's callers");
791 ftrace_fixup_caller (btinfo, next, prev, flags);
796 /* PREV may have a tailcall caller, NEXT can't. If it does, fixup the up
797 link to add the tail callers to NEXT's back trace.
799 This removes NEXT->UP from NEXT's back trace. It will be added back
800 when connecting NEXT and PREV's callers - provided they exist.
802 If PREV's back trace consists of a series of tail calls without an
803 actual call, there will be no further connection and NEXT's caller will
804 be removed for good. To catch this case, we handle it here and connect
805 the top of PREV's back trace to NEXT's caller. */
806 if ((prev->flags & BFUN_UP_LINKS_TO_TAILCALL) != 0)
808 struct btrace_function *caller;
809 btrace_function_flags next_flags, prev_flags;
811 /* We checked NEXT->UP above so CALLER can't be NULL. */
812 caller = ftrace_find_call_by_number (btinfo, next->up);
813 next_flags = next->flags;
814 prev_flags = prev->flags;
816 DEBUG_FTRACE ("adding prev's tail calls to next");
818 prev = ftrace_find_call_by_number (btinfo, prev->up);
819 ftrace_fixup_caller (btinfo, next, prev, prev_flags);
821 for (; prev != NULL; prev = ftrace_find_call_by_number (btinfo,
824 /* At the end of PREV's back trace, continue with CALLER. */
827 DEBUG_FTRACE ("fixing up link for tailcall chain");
828 ftrace_debug (prev, "..top");
829 ftrace_debug (caller, "..up");
831 ftrace_fixup_caller (btinfo, prev, caller, next_flags);
833 /* If we skipped any tail calls, this may move CALLER to a
834 different function level.
836 Note that changing CALLER's level is only OK because we
837 know that this is the last iteration of the bottom-to-top
838 walk in ftrace_connect_backtrace.
840 Otherwise we will fix up CALLER's level when we connect it
841 to PREV's caller in the next iteration. */
842 ftrace_fixup_level (btinfo, caller,
843 prev->level - caller->level - 1);
847 /* There's nothing to do if we find a real call. */
848 if ((prev->flags & BFUN_UP_LINKS_TO_TAILCALL) == 0)
850 DEBUG_FTRACE ("will fix up link in next iteration");
858 /* Connect function segments on the same level in the back trace at LHS and RHS.
859 The back traces at LHS and RHS are expected to match according to
860 ftrace_match_backtrace. BTINFO is the branch trace information for the
864 ftrace_connect_backtrace (struct btrace_thread_info *btinfo,
865 struct btrace_function *lhs,
866 struct btrace_function *rhs)
868 while (lhs != NULL && rhs != NULL)
870 struct btrace_function *prev, *next;
872 gdb_assert (!ftrace_function_switched (lhs, rhs->msym, rhs->sym));
874 /* Connecting LHS and RHS may change the up link. */
878 lhs = ftrace_get_caller (btinfo, lhs);
879 rhs = ftrace_get_caller (btinfo, rhs);
881 ftrace_connect_bfun (btinfo, prev, next);
885 /* Bridge the gap between two function segments left and right of a gap if their
886 respective back traces match in at least MIN_MATCHES functions. BTINFO is
887 the branch trace information for the current thread.
889 Returns non-zero if the gap could be bridged, zero otherwise. */
892 ftrace_bridge_gap (struct btrace_thread_info *btinfo,
893 struct btrace_function *lhs, struct btrace_function *rhs,
896 struct btrace_function *best_l, *best_r, *cand_l, *cand_r;
899 DEBUG_FTRACE ("checking gap at insn %u (req matches: %d)",
900 rhs->insn_offset - 1, min_matches);
906 /* We search the back traces of LHS and RHS for valid connections and connect
907 the two functon segments that give the longest combined back trace. */
909 for (cand_l = lhs; cand_l != NULL;
910 cand_l = ftrace_get_caller (btinfo, cand_l))
911 for (cand_r = rhs; cand_r != NULL;
912 cand_r = ftrace_get_caller (btinfo, cand_r))
916 matches = ftrace_match_backtrace (btinfo, cand_l, cand_r);
917 if (best_matches < matches)
919 best_matches = matches;
925 /* We need at least MIN_MATCHES matches. */
926 gdb_assert (min_matches > 0);
927 if (best_matches < min_matches)
930 DEBUG_FTRACE ("..matches: %d", best_matches);
932 /* We will fix up the level of BEST_R and succeeding function segments such
933 that BEST_R's level matches BEST_L's when we connect BEST_L to BEST_R.
935 This will ignore the level of RHS and following if BEST_R != RHS. I.e. if
936 BEST_R is a successor of RHS in the back trace of RHS (phases 1 and 3).
938 To catch this, we already fix up the level here where we can start at RHS
939 instead of at BEST_R. We will ignore the level fixup when connecting
940 BEST_L to BEST_R as they will already be on the same level. */
941 ftrace_fixup_level (btinfo, rhs, best_l->level - best_r->level);
943 ftrace_connect_backtrace (btinfo, best_l, best_r);
948 /* Try to bridge gaps due to overflow or decode errors by connecting the
949 function segments that are separated by the gap. */
952 btrace_bridge_gaps (struct thread_info *tp, std::vector<unsigned int> &gaps)
954 struct btrace_thread_info *btinfo = &tp->btrace;
955 std::vector<unsigned int> remaining;
958 DEBUG ("bridge gaps");
960 /* We require a minimum amount of matches for bridging a gap. The number of
961 required matches will be lowered with each iteration.
963 The more matches the higher our confidence that the bridging is correct.
964 For big gaps or small traces, however, it may not be feasible to require a
965 high number of matches. */
966 for (min_matches = 5; min_matches > 0; --min_matches)
968 /* Let's try to bridge as many gaps as we can. In some cases, we need to
969 skip a gap and revisit it again after we closed later gaps. */
970 while (!gaps.empty ())
972 for (const unsigned int number : gaps)
974 struct btrace_function *gap, *lhs, *rhs;
977 gap = ftrace_find_call_by_number (btinfo, number);
979 /* We may have a sequence of gaps if we run from one error into
980 the next as we try to re-sync onto the trace stream. Ignore
981 all but the leftmost gap in such a sequence.
983 Also ignore gaps at the beginning of the trace. */
984 lhs = ftrace_find_call_by_number (btinfo, gap->number - 1);
985 if (lhs == NULL || lhs->errcode != 0)
988 /* Skip gaps to the right. */
989 rhs = ftrace_find_call_by_number (btinfo, gap->number + 1);
990 while (rhs != NULL && rhs->errcode != 0)
991 rhs = ftrace_find_call_by_number (btinfo, rhs->number + 1);
993 /* Ignore gaps at the end of the trace. */
997 bridged = ftrace_bridge_gap (btinfo, lhs, rhs, min_matches);
999 /* Keep track of gaps we were not able to bridge and try again.
1000 If we just pushed them to the end of GAPS we would risk an
1001 infinite loop in case we simply cannot bridge a gap. */
1003 remaining.push_back (number);
1006 /* Let's see if we made any progress. */
1007 if (remaining.size () == gaps.size ())
1011 gaps.swap (remaining);
1014 /* We get here if either GAPS is empty or if GAPS equals REMAINING. */
1021 /* We may omit this in some cases. Not sure it is worth the extra
1022 complication, though. */
1023 ftrace_compute_global_level_offset (btinfo);
1026 /* Compute the function branch trace from BTS trace. */
1029 btrace_compute_ftrace_bts (struct thread_info *tp,
1030 const struct btrace_data_bts *btrace,
1031 std::vector<unsigned int> &gaps)
1033 struct btrace_thread_info *btinfo;
1034 struct gdbarch *gdbarch;
1038 gdbarch = target_gdbarch ();
1039 btinfo = &tp->btrace;
1040 blk = VEC_length (btrace_block_s, btrace->blocks);
1042 if (btinfo->functions.empty ())
1045 level = -btinfo->level;
1049 btrace_block_s *block;
1054 block = VEC_index (btrace_block_s, btrace->blocks, blk);
1059 struct btrace_function *bfun;
1060 struct btrace_insn insn;
1063 /* We should hit the end of the block. Warn if we went too far. */
1064 if (block->end < pc)
1066 /* Indicate the gap in the trace. */
1067 bfun = ftrace_new_gap (btinfo, BDE_BTS_OVERFLOW, gaps);
1069 warning (_("Recorded trace may be corrupted at instruction "
1070 "%u (pc = %s)."), bfun->insn_offset - 1,
1071 core_addr_to_string_nz (pc));
1076 bfun = ftrace_update_function (btinfo, pc);
1078 /* Maintain the function level offset.
1079 For all but the last block, we do it here. */
1081 level = std::min (level, bfun->level);
1086 size = gdb_insn_length (gdbarch, pc);
1088 CATCH (error, RETURN_MASK_ERROR)
1095 insn.iclass = ftrace_classify_insn (gdbarch, pc);
1098 ftrace_update_insns (bfun, &insn);
1100 /* We're done once we pushed the instruction at the end. */
1101 if (block->end == pc)
1104 /* We can't continue if we fail to compute the size. */
1107 /* Indicate the gap in the trace. We just added INSN so we're
1108 not at the beginning. */
1109 bfun = ftrace_new_gap (btinfo, BDE_BTS_INSN_SIZE, gaps);
1111 warning (_("Recorded trace may be incomplete at instruction %u "
1112 "(pc = %s)."), bfun->insn_offset - 1,
1113 core_addr_to_string_nz (pc));
1120 /* Maintain the function level offset.
1121 For the last block, we do it here to not consider the last
1123 Since the last instruction corresponds to the current instruction
1124 and is not really part of the execution history, it shouldn't
1125 affect the level. */
1127 level = std::min (level, bfun->level);
1131 /* LEVEL is the minimal function level of all btrace function segments.
1132 Define the global level offset to -LEVEL so all function levels are
1133 normalized to start at zero. */
1134 btinfo->level = -level;
1137 #if defined (HAVE_LIBIPT)
1139 static enum btrace_insn_class
1140 pt_reclassify_insn (enum pt_insn_class iclass)
1145 return BTRACE_INSN_CALL;
1148 return BTRACE_INSN_RETURN;
1151 return BTRACE_INSN_JUMP;
1154 return BTRACE_INSN_OTHER;
1158 /* Return the btrace instruction flags for INSN. */
1160 static btrace_insn_flags
1161 pt_btrace_insn_flags (const struct pt_insn &insn)
1163 btrace_insn_flags flags = 0;
1165 if (insn.speculative)
1166 flags |= BTRACE_INSN_FLAG_SPECULATIVE;
1171 /* Return the btrace instruction for INSN. */
1174 pt_btrace_insn (const struct pt_insn &insn)
1176 return {(CORE_ADDR) insn.ip, (gdb_byte) insn.size,
1177 pt_reclassify_insn (insn.iclass),
1178 pt_btrace_insn_flags (insn)};
1182 /* Add function branch trace to BTINFO using DECODER. */
1185 ftrace_add_pt (struct btrace_thread_info *btinfo,
1186 struct pt_insn_decoder *decoder,
1188 std::vector<unsigned int> &gaps)
1190 struct btrace_function *bfun;
1196 struct pt_insn insn;
1198 errcode = pt_insn_sync_forward (decoder);
1201 if (errcode != -pte_eos)
1202 warning (_("Failed to synchronize onto the Intel Processor "
1203 "Trace stream: %s."), pt_errstr (pt_errcode (errcode)));
1209 errcode = pt_insn_next (decoder, &insn, sizeof(insn));
1213 /* Look for gaps in the trace - unless we're at the beginning. */
1214 if (!btinfo->functions.empty ())
1216 /* Tracing is disabled and re-enabled each time we enter the
1217 kernel. Most times, we continue from the same instruction we
1218 stopped before. This is indicated via the RESUMED instruction
1219 flag. The ENABLED instruction flag means that we continued
1220 from some other instruction. Indicate this as a trace gap. */
1223 bfun = ftrace_new_gap (btinfo, BDE_PT_DISABLED, gaps);
1225 pt_insn_get_offset (decoder, &offset);
1227 warning (_("Non-contiguous trace at instruction %u (offset "
1228 "= 0x%" PRIx64 ", pc = 0x%" PRIx64 ")."),
1229 bfun->insn_offset - 1, offset, insn.ip);
1233 /* Indicate trace overflows. */
1236 bfun = ftrace_new_gap (btinfo, BDE_PT_OVERFLOW, gaps);
1238 pt_insn_get_offset (decoder, &offset);
1240 warning (_("Overflow at instruction %u (offset = 0x%" PRIx64
1241 ", pc = 0x%" PRIx64 ")."), bfun->insn_offset - 1,
1245 bfun = ftrace_update_function (btinfo, insn.ip);
1247 /* Maintain the function level offset. */
1248 *plevel = std::min (*plevel, bfun->level);
1250 btrace_insn btinsn = pt_btrace_insn (insn);
1251 ftrace_update_insns (bfun, &btinsn);
1254 if (errcode == -pte_eos)
1257 /* Indicate the gap in the trace. */
1258 bfun = ftrace_new_gap (btinfo, errcode, gaps);
1260 pt_insn_get_offset (decoder, &offset);
1262 warning (_("Decode error (%d) at instruction %u (offset = 0x%" PRIx64
1263 ", pc = 0x%" PRIx64 "): %s."), errcode, bfun->insn_offset - 1,
1264 offset, insn.ip, pt_errstr (pt_errcode (errcode)));
1268 /* A callback function to allow the trace decoder to read the inferior's
1272 btrace_pt_readmem_callback (gdb_byte *buffer, size_t size,
1273 const struct pt_asid *asid, uint64_t pc,
1276 int result, errcode;
1278 result = (int) size;
1281 errcode = target_read_code ((CORE_ADDR) pc, buffer, size);
1283 result = -pte_nomap;
1285 CATCH (error, RETURN_MASK_ERROR)
1287 result = -pte_nomap;
1294 /* Translate the vendor from one enum to another. */
1296 static enum pt_cpu_vendor
1297 pt_translate_cpu_vendor (enum btrace_cpu_vendor vendor)
1309 /* Finalize the function branch trace after decode. */
1311 static void btrace_finalize_ftrace_pt (struct pt_insn_decoder *decoder,
1312 struct thread_info *tp, int level)
1314 pt_insn_free_decoder (decoder);
1316 /* LEVEL is the minimal function level of all btrace function segments.
1317 Define the global level offset to -LEVEL so all function levels are
1318 normalized to start at zero. */
1319 tp->btrace.level = -level;
1321 /* Add a single last instruction entry for the current PC.
1322 This allows us to compute the backtrace at the current PC using both
1323 standard unwind and btrace unwind.
1324 This extra entry is ignored by all record commands. */
1328 /* Compute the function branch trace from Intel Processor Trace
1332 btrace_compute_ftrace_pt (struct thread_info *tp,
1333 const struct btrace_data_pt *btrace,
1334 std::vector<unsigned int> &gaps)
1336 struct btrace_thread_info *btinfo;
1337 struct pt_insn_decoder *decoder;
1338 struct pt_config config;
1341 if (btrace->size == 0)
1344 btinfo = &tp->btrace;
1345 if (btinfo->functions.empty ())
1348 level = -btinfo->level;
1350 pt_config_init(&config);
1351 config.begin = btrace->data;
1352 config.end = btrace->data + btrace->size;
1354 config.cpu.vendor = pt_translate_cpu_vendor (btrace->config.cpu.vendor);
1355 config.cpu.family = btrace->config.cpu.family;
1356 config.cpu.model = btrace->config.cpu.model;
1357 config.cpu.stepping = btrace->config.cpu.stepping;
1359 errcode = pt_cpu_errata (&config.errata, &config.cpu);
1361 error (_("Failed to configure the Intel Processor Trace decoder: %s."),
1362 pt_errstr (pt_errcode (errcode)));
1364 decoder = pt_insn_alloc_decoder (&config);
1365 if (decoder == NULL)
1366 error (_("Failed to allocate the Intel Processor Trace decoder."));
1370 struct pt_image *image;
1372 image = pt_insn_get_image(decoder);
1374 error (_("Failed to configure the Intel Processor Trace decoder."));
1376 errcode = pt_image_set_callback(image, btrace_pt_readmem_callback, NULL);
1378 error (_("Failed to configure the Intel Processor Trace decoder: "
1379 "%s."), pt_errstr (pt_errcode (errcode)));
1381 ftrace_add_pt (btinfo, decoder, &level, gaps);
1383 CATCH (error, RETURN_MASK_ALL)
1385 /* Indicate a gap in the trace if we quit trace processing. */
1386 if (error.reason == RETURN_QUIT && !btinfo->functions.empty ())
1387 ftrace_new_gap (btinfo, BDE_PT_USER_QUIT, gaps);
1389 btrace_finalize_ftrace_pt (decoder, tp, level);
1391 throw_exception (error);
1395 btrace_finalize_ftrace_pt (decoder, tp, level);
1398 #else /* defined (HAVE_LIBIPT) */
1401 btrace_compute_ftrace_pt (struct thread_info *tp,
1402 const struct btrace_data_pt *btrace,
1403 std::vector<unsigned int> &gaps)
1405 internal_error (__FILE__, __LINE__, _("Unexpected branch trace format."));
1408 #endif /* defined (HAVE_LIBIPT) */
1410 /* Compute the function branch trace from a block branch trace BTRACE for
1411 a thread given by BTINFO. */
1414 btrace_compute_ftrace_1 (struct thread_info *tp, struct btrace_data *btrace,
1415 std::vector<unsigned int> &gaps)
1417 DEBUG ("compute ftrace");
1419 switch (btrace->format)
1421 case BTRACE_FORMAT_NONE:
1424 case BTRACE_FORMAT_BTS:
1425 btrace_compute_ftrace_bts (tp, &btrace->variant.bts, gaps);
1428 case BTRACE_FORMAT_PT:
1429 btrace_compute_ftrace_pt (tp, &btrace->variant.pt, gaps);
1433 internal_error (__FILE__, __LINE__, _("Unkown branch trace format."));
1437 btrace_finalize_ftrace (struct thread_info *tp, std::vector<unsigned int> &gaps)
1441 tp->btrace.ngaps += gaps.size ();
1442 btrace_bridge_gaps (tp, gaps);
1447 btrace_compute_ftrace (struct thread_info *tp, struct btrace_data *btrace)
1449 std::vector<unsigned int> gaps;
1453 btrace_compute_ftrace_1 (tp, btrace, gaps);
1455 CATCH (error, RETURN_MASK_ALL)
1457 btrace_finalize_ftrace (tp, gaps);
1459 throw_exception (error);
1463 btrace_finalize_ftrace (tp, gaps);
1466 /* Add an entry for the current PC. */
1469 btrace_add_pc (struct thread_info *tp)
1471 struct btrace_data btrace;
1472 struct btrace_block *block;
1473 struct regcache *regcache;
1474 struct cleanup *cleanup;
1477 regcache = get_thread_regcache (tp->ptid);
1478 pc = regcache_read_pc (regcache);
1480 btrace_data_init (&btrace);
1481 btrace.format = BTRACE_FORMAT_BTS;
1482 btrace.variant.bts.blocks = NULL;
1484 cleanup = make_cleanup_btrace_data (&btrace);
1486 block = VEC_safe_push (btrace_block_s, btrace.variant.bts.blocks, NULL);
1490 btrace_compute_ftrace (tp, &btrace);
1492 do_cleanups (cleanup);
1498 btrace_enable (struct thread_info *tp, const struct btrace_config *conf)
1500 if (tp->btrace.target != NULL)
1503 #if !defined (HAVE_LIBIPT)
1504 if (conf->format == BTRACE_FORMAT_PT)
1505 error (_("GDB does not support Intel Processor Trace."));
1506 #endif /* !defined (HAVE_LIBIPT) */
1508 if (!target_supports_btrace (conf->format))
1509 error (_("Target does not support branch tracing."));
1511 DEBUG ("enable thread %s (%s)", print_thread_id (tp),
1512 target_pid_to_str (tp->ptid));
1514 tp->btrace.target = target_enable_btrace (tp->ptid, conf);
1516 /* We're done if we failed to enable tracing. */
1517 if (tp->btrace.target == NULL)
1520 /* We need to undo the enable in case of errors. */
1523 /* Add an entry for the current PC so we start tracing from where we
1526 If we can't access TP's registers, TP is most likely running. In this
1527 case, we can't really say where tracing was enabled so it should be
1528 safe to simply skip this step.
1530 This is not relevant for BTRACE_FORMAT_PT since the trace will already
1531 start at the PC at which tracing was enabled. */
1532 if (conf->format != BTRACE_FORMAT_PT
1533 && can_access_registers_ptid (tp->ptid))
1536 CATCH (exception, RETURN_MASK_ALL)
1538 btrace_disable (tp);
1540 throw_exception (exception);
1547 const struct btrace_config *
1548 btrace_conf (const struct btrace_thread_info *btinfo)
1550 if (btinfo->target == NULL)
1553 return target_btrace_conf (btinfo->target);
1559 btrace_disable (struct thread_info *tp)
1561 struct btrace_thread_info *btp = &tp->btrace;
1564 if (btp->target == NULL)
1567 DEBUG ("disable thread %s (%s)", print_thread_id (tp),
1568 target_pid_to_str (tp->ptid));
1570 target_disable_btrace (btp->target);
1579 btrace_teardown (struct thread_info *tp)
1581 struct btrace_thread_info *btp = &tp->btrace;
1584 if (btp->target == NULL)
1587 DEBUG ("teardown thread %s (%s)", print_thread_id (tp),
1588 target_pid_to_str (tp->ptid));
1590 target_teardown_btrace (btp->target);
1596 /* Stitch branch trace in BTS format. */
1599 btrace_stitch_bts (struct btrace_data_bts *btrace, struct thread_info *tp)
1601 struct btrace_thread_info *btinfo;
1602 struct btrace_function *last_bfun;
1603 struct btrace_insn *last_insn;
1604 btrace_block_s *first_new_block;
1606 btinfo = &tp->btrace;
1607 gdb_assert (!btinfo->functions.empty ());
1608 gdb_assert (!VEC_empty (btrace_block_s, btrace->blocks));
1610 last_bfun = btinfo->functions.back ();
1612 /* If the existing trace ends with a gap, we just glue the traces
1613 together. We need to drop the last (i.e. chronologically first) block
1614 of the new trace, though, since we can't fill in the start address.*/
1615 if (VEC_empty (btrace_insn_s, last_bfun->insn))
1617 VEC_pop (btrace_block_s, btrace->blocks);
1621 /* Beware that block trace starts with the most recent block, so the
1622 chronologically first block in the new trace is the last block in
1623 the new trace's block vector. */
1624 first_new_block = VEC_last (btrace_block_s, btrace->blocks);
1625 last_insn = VEC_last (btrace_insn_s, last_bfun->insn);
1627 /* If the current PC at the end of the block is the same as in our current
1628 trace, there are two explanations:
1629 1. we executed the instruction and some branch brought us back.
1630 2. we have not made any progress.
1631 In the first case, the delta trace vector should contain at least two
1633 In the second case, the delta trace vector should contain exactly one
1634 entry for the partial block containing the current PC. Remove it. */
1635 if (first_new_block->end == last_insn->pc
1636 && VEC_length (btrace_block_s, btrace->blocks) == 1)
1638 VEC_pop (btrace_block_s, btrace->blocks);
1642 DEBUG ("stitching %s to %s", ftrace_print_insn_addr (last_insn),
1643 core_addr_to_string_nz (first_new_block->end));
1645 /* Do a simple sanity check to make sure we don't accidentally end up
1646 with a bad block. This should not occur in practice. */
1647 if (first_new_block->end < last_insn->pc)
1649 warning (_("Error while trying to read delta trace. Falling back to "
1654 /* We adjust the last block to start at the end of our current trace. */
1655 gdb_assert (first_new_block->begin == 0);
1656 first_new_block->begin = last_insn->pc;
1658 /* We simply pop the last insn so we can insert it again as part of
1659 the normal branch trace computation.
1660 Since instruction iterators are based on indices in the instructions
1661 vector, we don't leave any pointers dangling. */
1662 DEBUG ("pruning insn at %s for stitching",
1663 ftrace_print_insn_addr (last_insn));
1665 VEC_pop (btrace_insn_s, last_bfun->insn);
1667 /* The instructions vector may become empty temporarily if this has
1668 been the only instruction in this function segment.
1669 This violates the invariant but will be remedied shortly by
1670 btrace_compute_ftrace when we add the new trace. */
1672 /* The only case where this would hurt is if the entire trace consisted
1673 of just that one instruction. If we remove it, we might turn the now
1674 empty btrace function segment into a gap. But we don't want gaps at
1675 the beginning. To avoid this, we remove the entire old trace. */
1676 if (last_bfun->number == 1 && VEC_empty (btrace_insn_s, last_bfun->insn))
1682 /* Adjust the block trace in order to stitch old and new trace together.
1683 BTRACE is the new delta trace between the last and the current stop.
1684 TP is the traced thread.
1685 May modifx BTRACE as well as the existing trace in TP.
1686 Return 0 on success, -1 otherwise. */
1689 btrace_stitch_trace (struct btrace_data *btrace, struct thread_info *tp)
1691 /* If we don't have trace, there's nothing to do. */
1692 if (btrace_data_empty (btrace))
1695 switch (btrace->format)
1697 case BTRACE_FORMAT_NONE:
1700 case BTRACE_FORMAT_BTS:
1701 return btrace_stitch_bts (&btrace->variant.bts, tp);
1703 case BTRACE_FORMAT_PT:
1704 /* Delta reads are not supported. */
1708 internal_error (__FILE__, __LINE__, _("Unkown branch trace format."));
1711 /* Clear the branch trace histories in BTINFO. */
1714 btrace_clear_history (struct btrace_thread_info *btinfo)
1716 xfree (btinfo->insn_history);
1717 xfree (btinfo->call_history);
1718 xfree (btinfo->replay);
1720 btinfo->insn_history = NULL;
1721 btinfo->call_history = NULL;
1722 btinfo->replay = NULL;
1725 /* Clear the branch trace maintenance histories in BTINFO. */
1728 btrace_maint_clear (struct btrace_thread_info *btinfo)
1730 switch (btinfo->data.format)
1735 case BTRACE_FORMAT_BTS:
1736 btinfo->maint.variant.bts.packet_history.begin = 0;
1737 btinfo->maint.variant.bts.packet_history.end = 0;
1740 #if defined (HAVE_LIBIPT)
1741 case BTRACE_FORMAT_PT:
1742 xfree (btinfo->maint.variant.pt.packets);
1744 btinfo->maint.variant.pt.packets = NULL;
1745 btinfo->maint.variant.pt.packet_history.begin = 0;
1746 btinfo->maint.variant.pt.packet_history.end = 0;
1748 #endif /* defined (HAVE_LIBIPT) */
1755 btrace_decode_error (enum btrace_format format, int errcode)
1759 case BTRACE_FORMAT_BTS:
1762 case BDE_BTS_OVERFLOW:
1763 return _("instruction overflow");
1765 case BDE_BTS_INSN_SIZE:
1766 return _("unknown instruction");
1773 #if defined (HAVE_LIBIPT)
1774 case BTRACE_FORMAT_PT:
1777 case BDE_PT_USER_QUIT:
1778 return _("trace decode cancelled");
1780 case BDE_PT_DISABLED:
1781 return _("disabled");
1783 case BDE_PT_OVERFLOW:
1784 return _("overflow");
1788 return pt_errstr (pt_errcode (errcode));
1792 #endif /* defined (HAVE_LIBIPT) */
1798 return _("unknown");
1804 btrace_fetch (struct thread_info *tp)
1806 struct btrace_thread_info *btinfo;
1807 struct btrace_target_info *tinfo;
1808 struct btrace_data btrace;
1809 struct cleanup *cleanup;
1812 DEBUG ("fetch thread %s (%s)", print_thread_id (tp),
1813 target_pid_to_str (tp->ptid));
1815 btinfo = &tp->btrace;
1816 tinfo = btinfo->target;
1820 /* There's no way we could get new trace while replaying.
1821 On the other hand, delta trace would return a partial record with the
1822 current PC, which is the replay PC, not the last PC, as expected. */
1823 if (btinfo->replay != NULL)
1826 /* With CLI usage, TP->PTID always equals INFERIOR_PTID here. Now that we
1827 can store a gdb.Record object in Python referring to a different thread
1828 than the current one, temporarily set INFERIOR_PTID. */
1829 cleanup = save_inferior_ptid ();
1830 inferior_ptid = tp->ptid;
1832 /* We should not be called on running or exited threads. */
1833 gdb_assert (can_access_registers_ptid (tp->ptid));
1835 btrace_data_init (&btrace);
1836 make_cleanup_btrace_data (&btrace);
1838 /* Let's first try to extend the trace we already have. */
1839 if (!btinfo->functions.empty ())
1841 errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_DELTA);
1844 /* Success. Let's try to stitch the traces together. */
1845 errcode = btrace_stitch_trace (&btrace, tp);
1849 /* We failed to read delta trace. Let's try to read new trace. */
1850 errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_NEW);
1852 /* If we got any new trace, discard what we have. */
1853 if (errcode == 0 && !btrace_data_empty (&btrace))
1857 /* If we were not able to read the trace, we start over. */
1861 errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_ALL);
1865 errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_ALL);
1867 /* If we were not able to read the branch trace, signal an error. */
1869 error (_("Failed to read branch trace."));
1871 /* Compute the trace, provided we have any. */
1872 if (!btrace_data_empty (&btrace))
1874 /* Store the raw trace data. The stored data will be cleared in
1875 btrace_clear, so we always append the new trace. */
1876 btrace_data_append (&btinfo->data, &btrace);
1877 btrace_maint_clear (btinfo);
1879 btrace_clear_history (btinfo);
1880 btrace_compute_ftrace (tp, &btrace);
1883 do_cleanups (cleanup);
1889 btrace_clear (struct thread_info *tp)
1891 struct btrace_thread_info *btinfo;
1893 DEBUG ("clear thread %s (%s)", print_thread_id (tp),
1894 target_pid_to_str (tp->ptid));
1896 /* Make sure btrace frames that may hold a pointer into the branch
1897 trace data are destroyed. */
1898 reinit_frame_cache ();
1900 btinfo = &tp->btrace;
1901 for (auto &bfun : btinfo->functions)
1903 VEC_free (btrace_insn_s, bfun->insn);
1907 btinfo->functions.clear ();
1910 /* Must clear the maint data before - it depends on BTINFO->DATA. */
1911 btrace_maint_clear (btinfo);
1912 btrace_data_clear (&btinfo->data);
1913 btrace_clear_history (btinfo);
1919 btrace_free_objfile (struct objfile *objfile)
1921 struct thread_info *tp;
1923 DEBUG ("free objfile");
1925 ALL_NON_EXITED_THREADS (tp)
1929 #if defined (HAVE_LIBEXPAT)
1931 /* Check the btrace document version. */
1934 check_xml_btrace_version (struct gdb_xml_parser *parser,
1935 const struct gdb_xml_element *element,
1936 void *user_data, VEC (gdb_xml_value_s) *attributes)
1939 = (const char *) xml_find_attribute (attributes, "version")->value;
1941 if (strcmp (version, "1.0") != 0)
1942 gdb_xml_error (parser, _("Unsupported btrace version: \"%s\""), version);
1945 /* Parse a btrace "block" xml record. */
1948 parse_xml_btrace_block (struct gdb_xml_parser *parser,
1949 const struct gdb_xml_element *element,
1950 void *user_data, VEC (gdb_xml_value_s) *attributes)
1952 struct btrace_data *btrace;
1953 struct btrace_block *block;
1954 ULONGEST *begin, *end;
1956 btrace = (struct btrace_data *) user_data;
1958 switch (btrace->format)
1960 case BTRACE_FORMAT_BTS:
1963 case BTRACE_FORMAT_NONE:
1964 btrace->format = BTRACE_FORMAT_BTS;
1965 btrace->variant.bts.blocks = NULL;
1969 gdb_xml_error (parser, _("Btrace format error."));
1972 begin = (ULONGEST *) xml_find_attribute (attributes, "begin")->value;
1973 end = (ULONGEST *) xml_find_attribute (attributes, "end")->value;
1975 block = VEC_safe_push (btrace_block_s, btrace->variant.bts.blocks, NULL);
1976 block->begin = *begin;
1980 /* Parse a "raw" xml record. */
1983 parse_xml_raw (struct gdb_xml_parser *parser, const char *body_text,
1984 gdb_byte **pdata, size_t *psize)
1986 struct cleanup *cleanup;
1987 gdb_byte *data, *bin;
1990 len = strlen (body_text);
1992 gdb_xml_error (parser, _("Bad raw data size."));
1996 bin = data = (gdb_byte *) xmalloc (size);
1997 cleanup = make_cleanup (xfree, data);
1999 /* We use hex encoding - see common/rsp-low.h. */
2007 if (hi == 0 || lo == 0)
2008 gdb_xml_error (parser, _("Bad hex encoding."));
2010 *bin++ = fromhex (hi) * 16 + fromhex (lo);
2014 discard_cleanups (cleanup);
2020 /* Parse a btrace pt-config "cpu" xml record. */
2023 parse_xml_btrace_pt_config_cpu (struct gdb_xml_parser *parser,
2024 const struct gdb_xml_element *element,
2026 VEC (gdb_xml_value_s) *attributes)
2028 struct btrace_data *btrace;
2030 ULONGEST *family, *model, *stepping;
2032 vendor = (const char *) xml_find_attribute (attributes, "vendor")->value;
2033 family = (ULONGEST *) xml_find_attribute (attributes, "family")->value;
2034 model = (ULONGEST *) xml_find_attribute (attributes, "model")->value;
2035 stepping = (ULONGEST *) xml_find_attribute (attributes, "stepping")->value;
2037 btrace = (struct btrace_data *) user_data;
2039 if (strcmp (vendor, "GenuineIntel") == 0)
2040 btrace->variant.pt.config.cpu.vendor = CV_INTEL;
2042 btrace->variant.pt.config.cpu.family = *family;
2043 btrace->variant.pt.config.cpu.model = *model;
2044 btrace->variant.pt.config.cpu.stepping = *stepping;
2047 /* Parse a btrace pt "raw" xml record. */
2050 parse_xml_btrace_pt_raw (struct gdb_xml_parser *parser,
2051 const struct gdb_xml_element *element,
2052 void *user_data, const char *body_text)
2054 struct btrace_data *btrace;
2056 btrace = (struct btrace_data *) user_data;
2057 parse_xml_raw (parser, body_text, &btrace->variant.pt.data,
2058 &btrace->variant.pt.size);
2061 /* Parse a btrace "pt" xml record. */
2064 parse_xml_btrace_pt (struct gdb_xml_parser *parser,
2065 const struct gdb_xml_element *element,
2066 void *user_data, VEC (gdb_xml_value_s) *attributes)
2068 struct btrace_data *btrace;
2070 btrace = (struct btrace_data *) user_data;
2071 btrace->format = BTRACE_FORMAT_PT;
2072 btrace->variant.pt.config.cpu.vendor = CV_UNKNOWN;
2073 btrace->variant.pt.data = NULL;
2074 btrace->variant.pt.size = 0;
2077 static const struct gdb_xml_attribute block_attributes[] = {
2078 { "begin", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
2079 { "end", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
2080 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2083 static const struct gdb_xml_attribute btrace_pt_config_cpu_attributes[] = {
2084 { "vendor", GDB_XML_AF_NONE, NULL, NULL },
2085 { "family", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
2086 { "model", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
2087 { "stepping", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
2088 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2091 static const struct gdb_xml_element btrace_pt_config_children[] = {
2092 { "cpu", btrace_pt_config_cpu_attributes, NULL, GDB_XML_EF_OPTIONAL,
2093 parse_xml_btrace_pt_config_cpu, NULL },
2094 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2097 static const struct gdb_xml_element btrace_pt_children[] = {
2098 { "pt-config", NULL, btrace_pt_config_children, GDB_XML_EF_OPTIONAL, NULL,
2100 { "raw", NULL, NULL, GDB_XML_EF_OPTIONAL, NULL, parse_xml_btrace_pt_raw },
2101 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2104 static const struct gdb_xml_attribute btrace_attributes[] = {
2105 { "version", GDB_XML_AF_NONE, NULL, NULL },
2106 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2109 static const struct gdb_xml_element btrace_children[] = {
2110 { "block", block_attributes, NULL,
2111 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL, parse_xml_btrace_block, NULL },
2112 { "pt", NULL, btrace_pt_children, GDB_XML_EF_OPTIONAL, parse_xml_btrace_pt,
2114 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2117 static const struct gdb_xml_element btrace_elements[] = {
2118 { "btrace", btrace_attributes, btrace_children, GDB_XML_EF_NONE,
2119 check_xml_btrace_version, NULL },
2120 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2123 #endif /* defined (HAVE_LIBEXPAT) */
2128 parse_xml_btrace (struct btrace_data *btrace, const char *buffer)
2130 struct cleanup *cleanup;
2133 #if defined (HAVE_LIBEXPAT)
2135 btrace->format = BTRACE_FORMAT_NONE;
2137 cleanup = make_cleanup_btrace_data (btrace);
2138 errcode = gdb_xml_parse_quick (_("btrace"), "btrace.dtd", btrace_elements,
2141 error (_("Error parsing branch trace."));
2143 /* Keep parse results. */
2144 discard_cleanups (cleanup);
2146 #else /* !defined (HAVE_LIBEXPAT) */
2148 error (_("Cannot process branch trace. XML parsing is not supported."));
2150 #endif /* !defined (HAVE_LIBEXPAT) */
2153 #if defined (HAVE_LIBEXPAT)
2155 /* Parse a btrace-conf "bts" xml record. */
2158 parse_xml_btrace_conf_bts (struct gdb_xml_parser *parser,
2159 const struct gdb_xml_element *element,
2160 void *user_data, VEC (gdb_xml_value_s) *attributes)
2162 struct btrace_config *conf;
2163 struct gdb_xml_value *size;
2165 conf = (struct btrace_config *) user_data;
2166 conf->format = BTRACE_FORMAT_BTS;
2169 size = xml_find_attribute (attributes, "size");
2171 conf->bts.size = (unsigned int) *(ULONGEST *) size->value;
2174 /* Parse a btrace-conf "pt" xml record. */
2177 parse_xml_btrace_conf_pt (struct gdb_xml_parser *parser,
2178 const struct gdb_xml_element *element,
2179 void *user_data, VEC (gdb_xml_value_s) *attributes)
2181 struct btrace_config *conf;
2182 struct gdb_xml_value *size;
2184 conf = (struct btrace_config *) user_data;
2185 conf->format = BTRACE_FORMAT_PT;
2188 size = xml_find_attribute (attributes, "size");
2190 conf->pt.size = (unsigned int) *(ULONGEST *) size->value;
2193 static const struct gdb_xml_attribute btrace_conf_pt_attributes[] = {
2194 { "size", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
2195 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2198 static const struct gdb_xml_attribute btrace_conf_bts_attributes[] = {
2199 { "size", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
2200 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2203 static const struct gdb_xml_element btrace_conf_children[] = {
2204 { "bts", btrace_conf_bts_attributes, NULL, GDB_XML_EF_OPTIONAL,
2205 parse_xml_btrace_conf_bts, NULL },
2206 { "pt", btrace_conf_pt_attributes, NULL, GDB_XML_EF_OPTIONAL,
2207 parse_xml_btrace_conf_pt, NULL },
2208 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2211 static const struct gdb_xml_attribute btrace_conf_attributes[] = {
2212 { "version", GDB_XML_AF_NONE, NULL, NULL },
2213 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2216 static const struct gdb_xml_element btrace_conf_elements[] = {
2217 { "btrace-conf", btrace_conf_attributes, btrace_conf_children,
2218 GDB_XML_EF_NONE, NULL, NULL },
2219 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2222 #endif /* defined (HAVE_LIBEXPAT) */
2227 parse_xml_btrace_conf (struct btrace_config *conf, const char *xml)
2231 #if defined (HAVE_LIBEXPAT)
2233 errcode = gdb_xml_parse_quick (_("btrace-conf"), "btrace-conf.dtd",
2234 btrace_conf_elements, xml, conf);
2236 error (_("Error parsing branch trace configuration."));
2238 #else /* !defined (HAVE_LIBEXPAT) */
2240 error (_("XML parsing is not supported."));
2242 #endif /* !defined (HAVE_LIBEXPAT) */
2247 const struct btrace_insn *
2248 btrace_insn_get (const struct btrace_insn_iterator *it)
2250 const struct btrace_function *bfun;
2251 unsigned int index, end;
2253 index = it->insn_index;
2254 bfun = it->btinfo->functions[it->call_index];
2256 /* Check if the iterator points to a gap in the trace. */
2257 if (bfun->errcode != 0)
2260 /* The index is within the bounds of this function's instruction vector. */
2261 end = VEC_length (btrace_insn_s, bfun->insn);
2262 gdb_assert (0 < end);
2263 gdb_assert (index < end);
2265 return VEC_index (btrace_insn_s, bfun->insn, index);
2271 btrace_insn_get_error (const struct btrace_insn_iterator *it)
2273 const struct btrace_function *bfun;
2275 bfun = it->btinfo->functions[it->call_index];
2276 return bfun->errcode;
2282 btrace_insn_number (const struct btrace_insn_iterator *it)
2284 const struct btrace_function *bfun;
2286 bfun = it->btinfo->functions[it->call_index];
2287 return bfun->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);