1 /* Branch trace support for GDB, the GNU debugger.
3 Copyright (C) 2013-2016 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 non-zero if BFUN does not match MFUN and FUN,
145 return zero otherwise. */
148 ftrace_function_switched (const struct btrace_function *bfun,
149 const struct minimal_symbol *mfun,
150 const struct symbol *fun)
152 struct minimal_symbol *msym;
158 /* If the minimal symbol changed, we certainly switched functions. */
159 if (mfun != NULL && msym != NULL
160 && strcmp (MSYMBOL_LINKAGE_NAME (mfun), MSYMBOL_LINKAGE_NAME (msym)) != 0)
163 /* If the symbol changed, we certainly switched functions. */
164 if (fun != NULL && sym != NULL)
166 const char *bfname, *fname;
168 /* Check the function name. */
169 if (strcmp (SYMBOL_LINKAGE_NAME (fun), SYMBOL_LINKAGE_NAME (sym)) != 0)
172 /* Check the location of those functions, as well. */
173 bfname = symtab_to_fullname (symbol_symtab (sym));
174 fname = symtab_to_fullname (symbol_symtab (fun));
175 if (filename_cmp (fname, bfname) != 0)
179 /* If we lost symbol information, we switched functions. */
180 if (!(msym == NULL && sym == NULL) && mfun == NULL && fun == NULL)
183 /* If we gained symbol information, we switched functions. */
184 if (msym == NULL && sym == NULL && !(mfun == NULL && fun == NULL))
190 /* Allocate and initialize a new branch trace function segment.
191 PREV is the chronologically preceding function segment.
192 MFUN and FUN are the symbol information we have for this function. */
194 static struct btrace_function *
195 ftrace_new_function (struct btrace_function *prev,
196 struct minimal_symbol *mfun,
199 struct btrace_function *bfun;
201 bfun = XCNEW (struct btrace_function);
205 bfun->flow.prev = prev;
209 /* Start counting at one. */
211 bfun->insn_offset = 1;
215 gdb_assert (prev->flow.next == NULL);
216 prev->flow.next = bfun;
218 bfun->number = prev->number + 1;
219 bfun->insn_offset = (prev->insn_offset
220 + VEC_length (btrace_insn_s, prev->insn));
221 bfun->level = prev->level;
227 /* Update the UP field of a function segment. */
230 ftrace_update_caller (struct btrace_function *bfun,
231 struct btrace_function *caller,
232 enum btrace_function_flag flags)
234 if (bfun->up != NULL)
235 ftrace_debug (bfun, "updating caller");
240 ftrace_debug (bfun, "set caller");
241 ftrace_debug (caller, "..to");
244 /* Fix up the caller for all segments of a function. */
247 ftrace_fixup_caller (struct btrace_function *bfun,
248 struct btrace_function *caller,
249 enum btrace_function_flag flags)
251 struct btrace_function *prev, *next;
253 ftrace_update_caller (bfun, caller, flags);
255 /* Update all function segments belonging to the same function. */
256 for (prev = bfun->segment.prev; prev != NULL; prev = prev->segment.prev)
257 ftrace_update_caller (prev, caller, flags);
259 for (next = bfun->segment.next; next != NULL; next = next->segment.next)
260 ftrace_update_caller (next, caller, flags);
263 /* Add a new function segment for a call.
264 CALLER is the chronologically preceding function segment.
265 MFUN and FUN are the symbol information we have for this function. */
267 static struct btrace_function *
268 ftrace_new_call (struct btrace_function *caller,
269 struct minimal_symbol *mfun,
272 struct btrace_function *bfun;
274 bfun = ftrace_new_function (caller, mfun, fun);
278 ftrace_debug (bfun, "new call");
283 /* Add a new function segment for a tail call.
284 CALLER is the chronologically preceding function segment.
285 MFUN and FUN are the symbol information we have for this function. */
287 static struct btrace_function *
288 ftrace_new_tailcall (struct btrace_function *caller,
289 struct minimal_symbol *mfun,
292 struct btrace_function *bfun;
294 bfun = ftrace_new_function (caller, mfun, fun);
297 bfun->flags |= BFUN_UP_LINKS_TO_TAILCALL;
299 ftrace_debug (bfun, "new tail call");
304 /* Return the caller of BFUN or NULL if there is none. This function skips
305 tail calls in the call chain. */
306 static struct btrace_function *
307 ftrace_get_caller (struct btrace_function *bfun)
309 for (; bfun != NULL; bfun = bfun->up)
310 if ((bfun->flags & BFUN_UP_LINKS_TO_TAILCALL) == 0)
316 /* Find the innermost caller in the back trace of BFUN with MFUN/FUN
317 symbol information. */
319 static struct btrace_function *
320 ftrace_find_caller (struct btrace_function *bfun,
321 struct minimal_symbol *mfun,
324 for (; bfun != NULL; bfun = bfun->up)
326 /* Skip functions with incompatible symbol information. */
327 if (ftrace_function_switched (bfun, mfun, fun))
330 /* This is the function segment we're looking for. */
337 /* Find the innermost caller in the back trace of BFUN, skipping all
338 function segments that do not end with a call instruction (e.g.
339 tail calls ending with a jump). */
341 static struct btrace_function *
342 ftrace_find_call (struct btrace_function *bfun)
344 for (; bfun != NULL; bfun = bfun->up)
346 struct btrace_insn *last;
349 if (bfun->errcode != 0)
352 last = VEC_last (btrace_insn_s, bfun->insn);
354 if (last->iclass == BTRACE_INSN_CALL)
361 /* Add a continuation segment for a function into which we return.
362 PREV is the chronologically preceding function segment.
363 MFUN and FUN are the symbol information we have for this function. */
365 static struct btrace_function *
366 ftrace_new_return (struct btrace_function *prev,
367 struct minimal_symbol *mfun,
370 struct btrace_function *bfun, *caller;
372 bfun = ftrace_new_function (prev, mfun, fun);
374 /* It is important to start at PREV's caller. Otherwise, we might find
375 PREV itself, if PREV is a recursive function. */
376 caller = ftrace_find_caller (prev->up, mfun, fun);
379 /* The caller of PREV is the preceding btrace function segment in this
380 function instance. */
381 gdb_assert (caller->segment.next == NULL);
383 caller->segment.next = bfun;
384 bfun->segment.prev = caller;
386 /* Maintain the function level. */
387 bfun->level = caller->level;
389 /* Maintain the call stack. */
390 bfun->up = caller->up;
391 bfun->flags = caller->flags;
393 ftrace_debug (bfun, "new return");
397 /* We did not find a caller. This could mean that something went
398 wrong or that the call is simply not included in the trace. */
400 /* Let's search for some actual call. */
401 caller = ftrace_find_call (prev->up);
404 /* There is no call in PREV's back trace. We assume that the
405 branch trace did not include it. */
407 /* Let's find the topmost function and add a new caller for it.
408 This should handle a series of initial tail calls. */
409 while (prev->up != NULL)
412 bfun->level = prev->level - 1;
414 /* Fix up the call stack for PREV. */
415 ftrace_fixup_caller (prev, bfun, BFUN_UP_LINKS_TO_RET);
417 ftrace_debug (bfun, "new return - no caller");
421 /* There is a call in PREV's back trace to which we should have
422 returned but didn't. Let's start a new, separate back trace
423 from PREV's level. */
424 bfun->level = prev->level - 1;
426 /* We fix up the back trace for PREV but leave other function segments
427 on the same level as they are.
428 This should handle things like schedule () correctly where we're
429 switching contexts. */
431 prev->flags = BFUN_UP_LINKS_TO_RET;
433 ftrace_debug (bfun, "new return - unknown caller");
440 /* Add a new function segment for a function switch.
441 PREV is the chronologically preceding function segment.
442 MFUN and FUN are the symbol information we have for this function. */
444 static struct btrace_function *
445 ftrace_new_switch (struct btrace_function *prev,
446 struct minimal_symbol *mfun,
449 struct btrace_function *bfun;
451 /* This is an unexplained function switch. The call stack will likely
452 be wrong at this point. */
453 bfun = ftrace_new_function (prev, mfun, fun);
455 ftrace_debug (bfun, "new switch");
460 /* Add a new function segment for a gap in the trace due to a decode error.
461 PREV is the chronologically preceding function segment.
462 ERRCODE is the format-specific error code. */
464 static struct btrace_function *
465 ftrace_new_gap (struct btrace_function *prev, int errcode)
467 struct btrace_function *bfun;
469 /* We hijack prev if it was empty. */
470 if (prev != NULL && prev->errcode == 0
471 && VEC_empty (btrace_insn_s, prev->insn))
474 bfun = ftrace_new_function (prev, NULL, NULL);
476 bfun->errcode = errcode;
478 ftrace_debug (bfun, "new gap");
483 /* Update BFUN with respect to the instruction at PC. This may create new
485 Return the chronologically latest function segment, never NULL. */
487 static struct btrace_function *
488 ftrace_update_function (struct btrace_function *bfun, CORE_ADDR pc)
490 struct bound_minimal_symbol bmfun;
491 struct minimal_symbol *mfun;
493 struct btrace_insn *last;
495 /* Try to determine the function we're in. We use both types of symbols
496 to avoid surprises when we sometimes get a full symbol and sometimes
497 only a minimal symbol. */
498 fun = find_pc_function (pc);
499 bmfun = lookup_minimal_symbol_by_pc (pc);
502 if (fun == NULL && mfun == NULL)
503 DEBUG_FTRACE ("no symbol at %s", core_addr_to_string_nz (pc));
505 /* If we didn't have a function or if we had a gap before, we create one. */
506 if (bfun == NULL || bfun->errcode != 0)
507 return ftrace_new_function (bfun, mfun, fun);
509 /* Check the last instruction, if we have one.
510 We do this check first, since it allows us to fill in the call stack
511 links in addition to the normal flow links. */
513 if (!VEC_empty (btrace_insn_s, bfun->insn))
514 last = VEC_last (btrace_insn_s, bfun->insn);
518 switch (last->iclass)
520 case BTRACE_INSN_RETURN:
524 /* On some systems, _dl_runtime_resolve returns to the resolved
525 function instead of jumping to it. From our perspective,
526 however, this is a tailcall.
527 If we treated it as return, we wouldn't be able to find the
528 resolved function in our stack back trace. Hence, we would
529 lose the current stack back trace and start anew with an empty
530 back trace. When the resolved function returns, we would then
531 create a stack back trace with the same function names but
532 different frame id's. This will confuse stepping. */
533 fname = ftrace_print_function_name (bfun);
534 if (strcmp (fname, "_dl_runtime_resolve") == 0)
535 return ftrace_new_tailcall (bfun, mfun, fun);
537 return ftrace_new_return (bfun, mfun, fun);
540 case BTRACE_INSN_CALL:
541 /* Ignore calls to the next instruction. They are used for PIC. */
542 if (last->pc + last->size == pc)
545 return ftrace_new_call (bfun, mfun, fun);
547 case BTRACE_INSN_JUMP:
551 start = get_pc_function_start (pc);
553 /* A jump to the start of a function is (typically) a tail call. */
555 return ftrace_new_tailcall (bfun, mfun, fun);
557 /* If we can't determine the function for PC, we treat a jump at
558 the end of the block as tail call if we're switching functions
559 and as an intra-function branch if we don't. */
560 if (start == 0 && ftrace_function_switched (bfun, mfun, fun))
561 return ftrace_new_tailcall (bfun, mfun, fun);
568 /* Check if we're switching functions for some other reason. */
569 if (ftrace_function_switched (bfun, mfun, fun))
571 DEBUG_FTRACE ("switching from %s in %s at %s",
572 ftrace_print_insn_addr (last),
573 ftrace_print_function_name (bfun),
574 ftrace_print_filename (bfun));
576 return ftrace_new_switch (bfun, mfun, fun);
582 /* Add the instruction at PC to BFUN's instructions. */
585 ftrace_update_insns (struct btrace_function *bfun,
586 const struct btrace_insn *insn)
588 VEC_safe_push (btrace_insn_s, bfun->insn, insn);
590 if (record_debug > 1)
591 ftrace_debug (bfun, "update insn");
594 /* Classify the instruction at PC. */
596 static enum btrace_insn_class
597 ftrace_classify_insn (struct gdbarch *gdbarch, CORE_ADDR pc)
599 enum btrace_insn_class iclass;
601 iclass = BTRACE_INSN_OTHER;
604 if (gdbarch_insn_is_call (gdbarch, pc))
605 iclass = BTRACE_INSN_CALL;
606 else if (gdbarch_insn_is_ret (gdbarch, pc))
607 iclass = BTRACE_INSN_RETURN;
608 else if (gdbarch_insn_is_jump (gdbarch, pc))
609 iclass = BTRACE_INSN_JUMP;
611 CATCH (error, RETURN_MASK_ERROR)
619 /* Try to match the back trace at LHS to the back trace at RHS. Returns the
620 number of matching function segments or zero if the back traces do not
624 ftrace_match_backtrace (struct btrace_function *lhs,
625 struct btrace_function *rhs)
629 for (matches = 0; lhs != NULL && rhs != NULL; ++matches)
631 if (ftrace_function_switched (lhs, rhs->msym, rhs->sym))
634 lhs = ftrace_get_caller (lhs);
635 rhs = ftrace_get_caller (rhs);
641 /* Add ADJUSTMENT to the level of BFUN and succeeding function segments. */
644 ftrace_fixup_level (struct btrace_function *bfun, int adjustment)
649 DEBUG_FTRACE ("fixup level (%+d)", adjustment);
650 ftrace_debug (bfun, "..bfun");
652 for (; bfun != NULL; bfun = bfun->flow.next)
653 bfun->level += adjustment;
656 /* Recompute the global level offset. Traverse the function trace and compute
657 the global level offset as the negative of the minimal function level. */
660 ftrace_compute_global_level_offset (struct btrace_thread_info *btinfo)
662 struct btrace_function *bfun, *end;
668 bfun = btinfo->begin;
672 /* The last function segment contains the current instruction, which is not
673 really part of the trace. If it contains just this one instruction, we
674 stop when we reach it; otherwise, we let the below loop run to the end. */
676 if (VEC_length (btrace_insn_s, end->insn) > 1)
680 for (; bfun != end; bfun = bfun->flow.next)
681 level = std::min (level, bfun->level);
683 DEBUG_FTRACE ("setting global level offset: %d", -level);
684 btinfo->level = -level;
687 /* Connect the function segments PREV and NEXT in a bottom-to-top walk as in
688 ftrace_connect_backtrace. */
691 ftrace_connect_bfun (struct btrace_function *prev,
692 struct btrace_function *next)
694 DEBUG_FTRACE ("connecting...");
695 ftrace_debug (prev, "..prev");
696 ftrace_debug (next, "..next");
698 /* The function segments are not yet connected. */
699 gdb_assert (prev->segment.next == NULL);
700 gdb_assert (next->segment.prev == NULL);
702 prev->segment.next = next;
703 next->segment.prev = prev;
705 /* We may have moved NEXT to a different function level. */
706 ftrace_fixup_level (next, prev->level - next->level);
708 /* If we run out of back trace for one, let's use the other's. */
709 if (prev->up == NULL)
711 if (next->up != NULL)
713 DEBUG_FTRACE ("using next's callers");
714 ftrace_fixup_caller (prev, next->up, next->flags);
717 else if (next->up == NULL)
719 if (prev->up != NULL)
721 DEBUG_FTRACE ("using prev's callers");
722 ftrace_fixup_caller (next, prev->up, prev->flags);
727 /* PREV may have a tailcall caller, NEXT can't. If it does, fixup the up
728 link to add the tail callers to NEXT's back trace.
730 This removes NEXT->UP from NEXT's back trace. It will be added back
731 when connecting NEXT and PREV's callers - provided they exist.
733 If PREV's back trace consists of a series of tail calls without an
734 actual call, there will be no further connection and NEXT's caller will
735 be removed for good. To catch this case, we handle it here and connect
736 the top of PREV's back trace to NEXT's caller. */
737 if ((prev->flags & BFUN_UP_LINKS_TO_TAILCALL) != 0)
739 struct btrace_function *caller;
740 btrace_function_flags flags;
742 /* We checked NEXT->UP above so CALLER can't be NULL. */
746 DEBUG_FTRACE ("adding prev's tail calls to next");
748 ftrace_fixup_caller (next, prev->up, prev->flags);
750 for (prev = prev->up; prev != NULL; prev = prev->up)
752 /* At the end of PREV's back trace, continue with CALLER. */
753 if (prev->up == NULL)
755 DEBUG_FTRACE ("fixing up link for tailcall chain");
756 ftrace_debug (prev, "..top");
757 ftrace_debug (caller, "..up");
759 ftrace_fixup_caller (prev, caller, flags);
761 /* If we skipped any tail calls, this may move CALLER to a
762 different function level.
764 Note that changing CALLER's level is only OK because we
765 know that this is the last iteration of the bottom-to-top
766 walk in ftrace_connect_backtrace.
768 Otherwise we will fix up CALLER's level when we connect it
769 to PREV's caller in the next iteration. */
770 ftrace_fixup_level (caller, prev->level - caller->level - 1);
774 /* There's nothing to do if we find a real call. */
775 if ((prev->flags & BFUN_UP_LINKS_TO_TAILCALL) == 0)
777 DEBUG_FTRACE ("will fix up link in next iteration");
785 /* Connect function segments on the same level in the back trace at LHS and RHS.
786 The back traces at LHS and RHS are expected to match according to
787 ftrace_match_backtrace. */
790 ftrace_connect_backtrace (struct btrace_function *lhs,
791 struct btrace_function *rhs)
793 while (lhs != NULL && rhs != NULL)
795 struct btrace_function *prev, *next;
797 gdb_assert (!ftrace_function_switched (lhs, rhs->msym, rhs->sym));
799 /* Connecting LHS and RHS may change the up link. */
803 lhs = ftrace_get_caller (lhs);
804 rhs = ftrace_get_caller (rhs);
806 ftrace_connect_bfun (prev, next);
810 /* Bridge the gap between two function segments left and right of a gap if their
811 respective back traces match in at least MIN_MATCHES functions.
813 Returns non-zero if the gap could be bridged, zero otherwise. */
816 ftrace_bridge_gap (struct btrace_function *lhs, struct btrace_function *rhs,
819 struct btrace_function *best_l, *best_r, *cand_l, *cand_r;
822 DEBUG_FTRACE ("checking gap at insn %u (req matches: %d)",
823 rhs->insn_offset - 1, min_matches);
829 /* We search the back traces of LHS and RHS for valid connections and connect
830 the two functon segments that give the longest combined back trace. */
832 for (cand_l = lhs; cand_l != NULL; cand_l = ftrace_get_caller (cand_l))
833 for (cand_r = rhs; cand_r != NULL; cand_r = ftrace_get_caller (cand_r))
837 matches = ftrace_match_backtrace (cand_l, cand_r);
838 if (best_matches < matches)
840 best_matches = matches;
846 /* We need at least MIN_MATCHES matches. */
847 gdb_assert (min_matches > 0);
848 if (best_matches < min_matches)
851 DEBUG_FTRACE ("..matches: %d", best_matches);
853 /* We will fix up the level of BEST_R and succeeding function segments such
854 that BEST_R's level matches BEST_L's when we connect BEST_L to BEST_R.
856 This will ignore the level of RHS and following if BEST_R != RHS. I.e. if
857 BEST_R is a successor of RHS in the back trace of RHS (phases 1 and 3).
859 To catch this, we already fix up the level here where we can start at RHS
860 instead of at BEST_R. We will ignore the level fixup when connecting
861 BEST_L to BEST_R as they will already be on the same level. */
862 ftrace_fixup_level (rhs, best_l->level - best_r->level);
864 ftrace_connect_backtrace (best_l, best_r);
869 /* Try to bridge gaps due to overflow or decode errors by connecting the
870 function segments that are separated by the gap. */
873 btrace_bridge_gaps (struct thread_info *tp, VEC (bfun_s) **gaps)
875 VEC (bfun_s) *remaining;
876 struct cleanup *old_chain;
879 DEBUG ("bridge gaps");
882 old_chain = make_cleanup (VEC_cleanup (bfun_s), &remaining);
884 /* We require a minimum amount of matches for bridging a gap. The number of
885 required matches will be lowered with each iteration.
887 The more matches the higher our confidence that the bridging is correct.
888 For big gaps or small traces, however, it may not be feasible to require a
889 high number of matches. */
890 for (min_matches = 5; min_matches > 0; --min_matches)
892 /* Let's try to bridge as many gaps as we can. In some cases, we need to
893 skip a gap and revisit it again after we closed later gaps. */
894 while (!VEC_empty (bfun_s, *gaps))
896 struct btrace_function *gap;
899 for (idx = 0; VEC_iterate (bfun_s, *gaps, idx, gap); ++idx)
901 struct btrace_function *lhs, *rhs;
904 /* We may have a sequence of gaps if we run from one error into
905 the next as we try to re-sync onto the trace stream. Ignore
906 all but the leftmost gap in such a sequence.
908 Also ignore gaps at the beginning of the trace. */
909 lhs = gap->flow.prev;
910 if (lhs == NULL || lhs->errcode != 0)
913 /* Skip gaps to the right. */
914 for (rhs = gap->flow.next; rhs != NULL; rhs = rhs->flow.next)
915 if (rhs->errcode == 0)
918 /* Ignore gaps at the end of the trace. */
922 bridged = ftrace_bridge_gap (lhs, rhs, min_matches);
924 /* Keep track of gaps we were not able to bridge and try again.
925 If we just pushed them to the end of GAPS we would risk an
926 infinite loop in case we simply cannot bridge a gap. */
928 VEC_safe_push (bfun_s, remaining, gap);
931 /* Let's see if we made any progress. */
932 if (VEC_length (bfun_s, remaining) == VEC_length (bfun_s, *gaps))
935 VEC_free (bfun_s, *gaps);
941 /* We get here if either GAPS is empty or if GAPS equals REMAINING. */
942 if (VEC_empty (bfun_s, *gaps))
945 VEC_free (bfun_s, remaining);
948 do_cleanups (old_chain);
950 /* We may omit this in some cases. Not sure it is worth the extra
951 complication, though. */
952 ftrace_compute_global_level_offset (&tp->btrace);
955 /* Compute the function branch trace from BTS trace. */
958 btrace_compute_ftrace_bts (struct thread_info *tp,
959 const struct btrace_data_bts *btrace,
962 struct btrace_thread_info *btinfo;
963 struct btrace_function *begin, *end;
964 struct gdbarch *gdbarch;
968 gdbarch = target_gdbarch ();
969 btinfo = &tp->btrace;
970 begin = btinfo->begin;
972 level = begin != NULL ? -btinfo->level : INT_MAX;
973 blk = VEC_length (btrace_block_s, btrace->blocks);
977 btrace_block_s *block;
982 block = VEC_index (btrace_block_s, btrace->blocks, blk);
987 struct btrace_insn insn;
990 /* We should hit the end of the block. Warn if we went too far. */
993 /* Indicate the gap in the trace. */
994 end = ftrace_new_gap (end, BDE_BTS_OVERFLOW);
998 VEC_safe_push (bfun_s, *gaps, end);
1000 warning (_("Recorded trace may be corrupted at instruction "
1001 "%u (pc = %s)."), end->insn_offset - 1,
1002 core_addr_to_string_nz (pc));
1007 end = ftrace_update_function (end, pc);
1011 /* Maintain the function level offset.
1012 For all but the last block, we do it here. */
1014 level = std::min (level, end->level);
1019 size = gdb_insn_length (gdbarch, pc);
1021 CATCH (error, RETURN_MASK_ERROR)
1028 insn.iclass = ftrace_classify_insn (gdbarch, pc);
1031 ftrace_update_insns (end, &insn);
1033 /* We're done once we pushed the instruction at the end. */
1034 if (block->end == pc)
1037 /* We can't continue if we fail to compute the size. */
1040 /* Indicate the gap in the trace. We just added INSN so we're
1041 not at the beginning. */
1042 end = ftrace_new_gap (end, BDE_BTS_INSN_SIZE);
1044 VEC_safe_push (bfun_s, *gaps, end);
1046 warning (_("Recorded trace may be incomplete at instruction %u "
1047 "(pc = %s)."), end->insn_offset - 1,
1048 core_addr_to_string_nz (pc));
1055 /* Maintain the function level offset.
1056 For the last block, we do it here to not consider the last
1058 Since the last instruction corresponds to the current instruction
1059 and is not really part of the execution history, it shouldn't
1060 affect the level. */
1062 level = std::min (level, end->level);
1066 btinfo->begin = begin;
1069 /* LEVEL is the minimal function level of all btrace function segments.
1070 Define the global level offset to -LEVEL so all function levels are
1071 normalized to start at zero. */
1072 btinfo->level = -level;
1075 #if defined (HAVE_LIBIPT)
1077 static enum btrace_insn_class
1078 pt_reclassify_insn (enum pt_insn_class iclass)
1083 return BTRACE_INSN_CALL;
1086 return BTRACE_INSN_RETURN;
1089 return BTRACE_INSN_JUMP;
1092 return BTRACE_INSN_OTHER;
1096 /* Return the btrace instruction flags for INSN. */
1098 static btrace_insn_flags
1099 pt_btrace_insn_flags (const struct pt_insn *insn)
1101 btrace_insn_flags flags = 0;
1103 if (insn->speculative)
1104 flags |= BTRACE_INSN_FLAG_SPECULATIVE;
1109 /* Add function branch trace using DECODER. */
1112 ftrace_add_pt (struct pt_insn_decoder *decoder,
1113 struct btrace_function **pbegin,
1114 struct btrace_function **pend, int *plevel,
1115 VEC (bfun_s) **gaps)
1117 struct btrace_function *begin, *end, *upd;
1125 struct btrace_insn btinsn;
1126 struct pt_insn insn;
1128 errcode = pt_insn_sync_forward (decoder);
1131 if (errcode != -pte_eos)
1132 warning (_("Failed to synchronize onto the Intel Processor "
1133 "Trace stream: %s."), pt_errstr (pt_errcode (errcode)));
1137 memset (&btinsn, 0, sizeof (btinsn));
1140 errcode = pt_insn_next (decoder, &insn, sizeof(insn));
1144 /* Look for gaps in the trace - unless we're at the beginning. */
1147 /* Tracing is disabled and re-enabled each time we enter the
1148 kernel. Most times, we continue from the same instruction we
1149 stopped before. This is indicated via the RESUMED instruction
1150 flag. The ENABLED instruction flag means that we continued
1151 from some other instruction. Indicate this as a trace gap. */
1154 *pend = end = ftrace_new_gap (end, BDE_PT_DISABLED);
1156 VEC_safe_push (bfun_s, *gaps, end);
1158 pt_insn_get_offset (decoder, &offset);
1160 warning (_("Non-contiguous trace at instruction %u (offset "
1161 "= 0x%" PRIx64 ", pc = 0x%" PRIx64 ")."),
1162 end->insn_offset - 1, offset, insn.ip);
1166 /* Indicate trace overflows. */
1169 *pend = end = ftrace_new_gap (end, BDE_PT_OVERFLOW);
1171 *pbegin = begin = end;
1173 VEC_safe_push (bfun_s, *gaps, end);
1175 pt_insn_get_offset (decoder, &offset);
1177 warning (_("Overflow at instruction %u (offset = 0x%" PRIx64
1178 ", pc = 0x%" PRIx64 ")."), end->insn_offset - 1,
1182 upd = ftrace_update_function (end, insn.ip);
1188 *pbegin = begin = upd;
1191 /* Maintain the function level offset. */
1192 *plevel = std::min (*plevel, end->level);
1194 btinsn.pc = (CORE_ADDR) insn.ip;
1195 btinsn.size = (gdb_byte) insn.size;
1196 btinsn.iclass = pt_reclassify_insn (insn.iclass);
1197 btinsn.flags = pt_btrace_insn_flags (&insn);
1199 ftrace_update_insns (end, &btinsn);
1202 if (errcode == -pte_eos)
1205 /* Indicate the gap in the trace. */
1206 *pend = end = ftrace_new_gap (end, errcode);
1208 *pbegin = begin = end;
1210 VEC_safe_push (bfun_s, *gaps, end);
1212 pt_insn_get_offset (decoder, &offset);
1214 warning (_("Decode error (%d) at instruction %u (offset = 0x%" PRIx64
1215 ", pc = 0x%" PRIx64 "): %s."), errcode, end->insn_offset - 1,
1216 offset, insn.ip, pt_errstr (pt_errcode (errcode)));
1220 /* A callback function to allow the trace decoder to read the inferior's
1224 btrace_pt_readmem_callback (gdb_byte *buffer, size_t size,
1225 const struct pt_asid *asid, uint64_t pc,
1228 int result, errcode;
1230 result = (int) size;
1233 errcode = target_read_code ((CORE_ADDR) pc, buffer, size);
1235 result = -pte_nomap;
1237 CATCH (error, RETURN_MASK_ERROR)
1239 result = -pte_nomap;
1246 /* Translate the vendor from one enum to another. */
1248 static enum pt_cpu_vendor
1249 pt_translate_cpu_vendor (enum btrace_cpu_vendor vendor)
1261 /* Finalize the function branch trace after decode. */
1263 static void btrace_finalize_ftrace_pt (struct pt_insn_decoder *decoder,
1264 struct thread_info *tp, int level)
1266 pt_insn_free_decoder (decoder);
1268 /* LEVEL is the minimal function level of all btrace function segments.
1269 Define the global level offset to -LEVEL so all function levels are
1270 normalized to start at zero. */
1271 tp->btrace.level = -level;
1273 /* Add a single last instruction entry for the current PC.
1274 This allows us to compute the backtrace at the current PC using both
1275 standard unwind and btrace unwind.
1276 This extra entry is ignored by all record commands. */
1280 /* Compute the function branch trace from Intel Processor Trace
1284 btrace_compute_ftrace_pt (struct thread_info *tp,
1285 const struct btrace_data_pt *btrace,
1286 VEC (bfun_s) **gaps)
1288 struct btrace_thread_info *btinfo;
1289 struct pt_insn_decoder *decoder;
1290 struct pt_config config;
1293 if (btrace->size == 0)
1296 btinfo = &tp->btrace;
1297 level = btinfo->begin != NULL ? -btinfo->level : INT_MAX;
1299 pt_config_init(&config);
1300 config.begin = btrace->data;
1301 config.end = btrace->data + btrace->size;
1303 config.cpu.vendor = pt_translate_cpu_vendor (btrace->config.cpu.vendor);
1304 config.cpu.family = btrace->config.cpu.family;
1305 config.cpu.model = btrace->config.cpu.model;
1306 config.cpu.stepping = btrace->config.cpu.stepping;
1308 errcode = pt_cpu_errata (&config.errata, &config.cpu);
1310 error (_("Failed to configure the Intel Processor Trace decoder: %s."),
1311 pt_errstr (pt_errcode (errcode)));
1313 decoder = pt_insn_alloc_decoder (&config);
1314 if (decoder == NULL)
1315 error (_("Failed to allocate the Intel Processor Trace decoder."));
1319 struct pt_image *image;
1321 image = pt_insn_get_image(decoder);
1323 error (_("Failed to configure the Intel Processor Trace decoder."));
1325 errcode = pt_image_set_callback(image, btrace_pt_readmem_callback, NULL);
1327 error (_("Failed to configure the Intel Processor Trace decoder: "
1328 "%s."), pt_errstr (pt_errcode (errcode)));
1330 ftrace_add_pt (decoder, &btinfo->begin, &btinfo->end, &level, gaps);
1332 CATCH (error, RETURN_MASK_ALL)
1334 /* Indicate a gap in the trace if we quit trace processing. */
1335 if (error.reason == RETURN_QUIT && btinfo->end != NULL)
1337 btinfo->end = ftrace_new_gap (btinfo->end, BDE_PT_USER_QUIT);
1339 VEC_safe_push (bfun_s, *gaps, btinfo->end);
1342 btrace_finalize_ftrace_pt (decoder, tp, level);
1344 throw_exception (error);
1348 btrace_finalize_ftrace_pt (decoder, tp, level);
1351 #else /* defined (HAVE_LIBIPT) */
1354 btrace_compute_ftrace_pt (struct thread_info *tp,
1355 const struct btrace_data_pt *btrace,
1356 VEC (bfun_s) **gaps)
1358 internal_error (__FILE__, __LINE__, _("Unexpected branch trace format."));
1361 #endif /* defined (HAVE_LIBIPT) */
1363 /* Compute the function branch trace from a block branch trace BTRACE for
1364 a thread given by BTINFO. */
1367 btrace_compute_ftrace_1 (struct thread_info *tp, struct btrace_data *btrace,
1368 VEC (bfun_s) **gaps)
1370 DEBUG ("compute ftrace");
1372 switch (btrace->format)
1374 case BTRACE_FORMAT_NONE:
1377 case BTRACE_FORMAT_BTS:
1378 btrace_compute_ftrace_bts (tp, &btrace->variant.bts, gaps);
1381 case BTRACE_FORMAT_PT:
1382 btrace_compute_ftrace_pt (tp, &btrace->variant.pt, gaps);
1386 internal_error (__FILE__, __LINE__, _("Unkown branch trace format."));
1390 btrace_finalize_ftrace (struct thread_info *tp, VEC (bfun_s) **gaps)
1392 if (!VEC_empty (bfun_s, *gaps))
1394 tp->btrace.ngaps += VEC_length (bfun_s, *gaps);
1395 btrace_bridge_gaps (tp, gaps);
1400 btrace_compute_ftrace (struct thread_info *tp, struct btrace_data *btrace)
1403 struct cleanup *old_chain;
1406 old_chain = make_cleanup (VEC_cleanup (bfun_s), &gaps);
1410 btrace_compute_ftrace_1 (tp, btrace, &gaps);
1412 CATCH (error, RETURN_MASK_ALL)
1414 btrace_finalize_ftrace (tp, &gaps);
1416 throw_exception (error);
1420 btrace_finalize_ftrace (tp, &gaps);
1422 do_cleanups (old_chain);
1425 /* Add an entry for the current PC. */
1428 btrace_add_pc (struct thread_info *tp)
1430 struct btrace_data btrace;
1431 struct btrace_block *block;
1432 struct regcache *regcache;
1433 struct cleanup *cleanup;
1436 regcache = get_thread_regcache (tp->ptid);
1437 pc = regcache_read_pc (regcache);
1439 btrace_data_init (&btrace);
1440 btrace.format = BTRACE_FORMAT_BTS;
1441 btrace.variant.bts.blocks = NULL;
1443 cleanup = make_cleanup_btrace_data (&btrace);
1445 block = VEC_safe_push (btrace_block_s, btrace.variant.bts.blocks, NULL);
1449 btrace_compute_ftrace (tp, &btrace);
1451 do_cleanups (cleanup);
1457 btrace_enable (struct thread_info *tp, const struct btrace_config *conf)
1459 if (tp->btrace.target != NULL)
1462 #if !defined (HAVE_LIBIPT)
1463 if (conf->format == BTRACE_FORMAT_PT)
1464 error (_("GDB does not support Intel Processor Trace."));
1465 #endif /* !defined (HAVE_LIBIPT) */
1467 if (!target_supports_btrace (conf->format))
1468 error (_("Target does not support branch tracing."));
1470 DEBUG ("enable thread %s (%s)", print_thread_id (tp),
1471 target_pid_to_str (tp->ptid));
1473 tp->btrace.target = target_enable_btrace (tp->ptid, conf);
1475 /* Add an entry for the current PC so we start tracing from where we
1477 if (tp->btrace.target != NULL)
1483 const struct btrace_config *
1484 btrace_conf (const struct btrace_thread_info *btinfo)
1486 if (btinfo->target == NULL)
1489 return target_btrace_conf (btinfo->target);
1495 btrace_disable (struct thread_info *tp)
1497 struct btrace_thread_info *btp = &tp->btrace;
1500 if (btp->target == NULL)
1503 DEBUG ("disable thread %s (%s)", print_thread_id (tp),
1504 target_pid_to_str (tp->ptid));
1506 target_disable_btrace (btp->target);
1515 btrace_teardown (struct thread_info *tp)
1517 struct btrace_thread_info *btp = &tp->btrace;
1520 if (btp->target == NULL)
1523 DEBUG ("teardown thread %s (%s)", print_thread_id (tp),
1524 target_pid_to_str (tp->ptid));
1526 target_teardown_btrace (btp->target);
1532 /* Stitch branch trace in BTS format. */
1535 btrace_stitch_bts (struct btrace_data_bts *btrace, struct thread_info *tp)
1537 struct btrace_thread_info *btinfo;
1538 struct btrace_function *last_bfun;
1539 struct btrace_insn *last_insn;
1540 btrace_block_s *first_new_block;
1542 btinfo = &tp->btrace;
1543 last_bfun = btinfo->end;
1544 gdb_assert (last_bfun != NULL);
1545 gdb_assert (!VEC_empty (btrace_block_s, btrace->blocks));
1547 /* If the existing trace ends with a gap, we just glue the traces
1548 together. We need to drop the last (i.e. chronologically first) block
1549 of the new trace, though, since we can't fill in the start address.*/
1550 if (VEC_empty (btrace_insn_s, last_bfun->insn))
1552 VEC_pop (btrace_block_s, btrace->blocks);
1556 /* Beware that block trace starts with the most recent block, so the
1557 chronologically first block in the new trace is the last block in
1558 the new trace's block vector. */
1559 first_new_block = VEC_last (btrace_block_s, btrace->blocks);
1560 last_insn = VEC_last (btrace_insn_s, last_bfun->insn);
1562 /* If the current PC at the end of the block is the same as in our current
1563 trace, there are two explanations:
1564 1. we executed the instruction and some branch brought us back.
1565 2. we have not made any progress.
1566 In the first case, the delta trace vector should contain at least two
1568 In the second case, the delta trace vector should contain exactly one
1569 entry for the partial block containing the current PC. Remove it. */
1570 if (first_new_block->end == last_insn->pc
1571 && VEC_length (btrace_block_s, btrace->blocks) == 1)
1573 VEC_pop (btrace_block_s, btrace->blocks);
1577 DEBUG ("stitching %s to %s", ftrace_print_insn_addr (last_insn),
1578 core_addr_to_string_nz (first_new_block->end));
1580 /* Do a simple sanity check to make sure we don't accidentally end up
1581 with a bad block. This should not occur in practice. */
1582 if (first_new_block->end < last_insn->pc)
1584 warning (_("Error while trying to read delta trace. Falling back to "
1589 /* We adjust the last block to start at the end of our current trace. */
1590 gdb_assert (first_new_block->begin == 0);
1591 first_new_block->begin = last_insn->pc;
1593 /* We simply pop the last insn so we can insert it again as part of
1594 the normal branch trace computation.
1595 Since instruction iterators are based on indices in the instructions
1596 vector, we don't leave any pointers dangling. */
1597 DEBUG ("pruning insn at %s for stitching",
1598 ftrace_print_insn_addr (last_insn));
1600 VEC_pop (btrace_insn_s, last_bfun->insn);
1602 /* The instructions vector may become empty temporarily if this has
1603 been the only instruction in this function segment.
1604 This violates the invariant but will be remedied shortly by
1605 btrace_compute_ftrace when we add the new trace. */
1607 /* The only case where this would hurt is if the entire trace consisted
1608 of just that one instruction. If we remove it, we might turn the now
1609 empty btrace function segment into a gap. But we don't want gaps at
1610 the beginning. To avoid this, we remove the entire old trace. */
1611 if (last_bfun == btinfo->begin && VEC_empty (btrace_insn_s, last_bfun->insn))
1617 /* Adjust the block trace in order to stitch old and new trace together.
1618 BTRACE is the new delta trace between the last and the current stop.
1619 TP is the traced thread.
1620 May modifx BTRACE as well as the existing trace in TP.
1621 Return 0 on success, -1 otherwise. */
1624 btrace_stitch_trace (struct btrace_data *btrace, struct thread_info *tp)
1626 /* If we don't have trace, there's nothing to do. */
1627 if (btrace_data_empty (btrace))
1630 switch (btrace->format)
1632 case BTRACE_FORMAT_NONE:
1635 case BTRACE_FORMAT_BTS:
1636 return btrace_stitch_bts (&btrace->variant.bts, tp);
1638 case BTRACE_FORMAT_PT:
1639 /* Delta reads are not supported. */
1643 internal_error (__FILE__, __LINE__, _("Unkown branch trace format."));
1646 /* Clear the branch trace histories in BTINFO. */
1649 btrace_clear_history (struct btrace_thread_info *btinfo)
1651 xfree (btinfo->insn_history);
1652 xfree (btinfo->call_history);
1653 xfree (btinfo->replay);
1655 btinfo->insn_history = NULL;
1656 btinfo->call_history = NULL;
1657 btinfo->replay = NULL;
1660 /* Clear the branch trace maintenance histories in BTINFO. */
1663 btrace_maint_clear (struct btrace_thread_info *btinfo)
1665 switch (btinfo->data.format)
1670 case BTRACE_FORMAT_BTS:
1671 btinfo->maint.variant.bts.packet_history.begin = 0;
1672 btinfo->maint.variant.bts.packet_history.end = 0;
1675 #if defined (HAVE_LIBIPT)
1676 case BTRACE_FORMAT_PT:
1677 xfree (btinfo->maint.variant.pt.packets);
1679 btinfo->maint.variant.pt.packets = NULL;
1680 btinfo->maint.variant.pt.packet_history.begin = 0;
1681 btinfo->maint.variant.pt.packet_history.end = 0;
1683 #endif /* defined (HAVE_LIBIPT) */
1690 btrace_fetch (struct thread_info *tp)
1692 struct btrace_thread_info *btinfo;
1693 struct btrace_target_info *tinfo;
1694 struct btrace_data btrace;
1695 struct cleanup *cleanup;
1698 DEBUG ("fetch thread %s (%s)", print_thread_id (tp),
1699 target_pid_to_str (tp->ptid));
1701 btinfo = &tp->btrace;
1702 tinfo = btinfo->target;
1706 /* There's no way we could get new trace while replaying.
1707 On the other hand, delta trace would return a partial record with the
1708 current PC, which is the replay PC, not the last PC, as expected. */
1709 if (btinfo->replay != NULL)
1712 btrace_data_init (&btrace);
1713 cleanup = make_cleanup_btrace_data (&btrace);
1715 /* Let's first try to extend the trace we already have. */
1716 if (btinfo->end != NULL)
1718 errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_DELTA);
1721 /* Success. Let's try to stitch the traces together. */
1722 errcode = btrace_stitch_trace (&btrace, tp);
1726 /* We failed to read delta trace. Let's try to read new trace. */
1727 errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_NEW);
1729 /* If we got any new trace, discard what we have. */
1730 if (errcode == 0 && !btrace_data_empty (&btrace))
1734 /* If we were not able to read the trace, we start over. */
1738 errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_ALL);
1742 errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_ALL);
1744 /* If we were not able to read the branch trace, signal an error. */
1746 error (_("Failed to read branch trace."));
1748 /* Compute the trace, provided we have any. */
1749 if (!btrace_data_empty (&btrace))
1751 /* Store the raw trace data. The stored data will be cleared in
1752 btrace_clear, so we always append the new trace. */
1753 btrace_data_append (&btinfo->data, &btrace);
1754 btrace_maint_clear (btinfo);
1756 btrace_clear_history (btinfo);
1757 btrace_compute_ftrace (tp, &btrace);
1760 do_cleanups (cleanup);
1766 btrace_clear (struct thread_info *tp)
1768 struct btrace_thread_info *btinfo;
1769 struct btrace_function *it, *trash;
1771 DEBUG ("clear thread %s (%s)", print_thread_id (tp),
1772 target_pid_to_str (tp->ptid));
1774 /* Make sure btrace frames that may hold a pointer into the branch
1775 trace data are destroyed. */
1776 reinit_frame_cache ();
1778 btinfo = &tp->btrace;
1789 btinfo->begin = NULL;
1793 /* Must clear the maint data before - it depends on BTINFO->DATA. */
1794 btrace_maint_clear (btinfo);
1795 btrace_data_clear (&btinfo->data);
1796 btrace_clear_history (btinfo);
1802 btrace_free_objfile (struct objfile *objfile)
1804 struct thread_info *tp;
1806 DEBUG ("free objfile");
1808 ALL_NON_EXITED_THREADS (tp)
1812 #if defined (HAVE_LIBEXPAT)
1814 /* Check the btrace document version. */
1817 check_xml_btrace_version (struct gdb_xml_parser *parser,
1818 const struct gdb_xml_element *element,
1819 void *user_data, VEC (gdb_xml_value_s) *attributes)
1822 = (const char *) xml_find_attribute (attributes, "version")->value;
1824 if (strcmp (version, "1.0") != 0)
1825 gdb_xml_error (parser, _("Unsupported btrace version: \"%s\""), version);
1828 /* Parse a btrace "block" xml record. */
1831 parse_xml_btrace_block (struct gdb_xml_parser *parser,
1832 const struct gdb_xml_element *element,
1833 void *user_data, VEC (gdb_xml_value_s) *attributes)
1835 struct btrace_data *btrace;
1836 struct btrace_block *block;
1837 ULONGEST *begin, *end;
1839 btrace = (struct btrace_data *) user_data;
1841 switch (btrace->format)
1843 case BTRACE_FORMAT_BTS:
1846 case BTRACE_FORMAT_NONE:
1847 btrace->format = BTRACE_FORMAT_BTS;
1848 btrace->variant.bts.blocks = NULL;
1852 gdb_xml_error (parser, _("Btrace format error."));
1855 begin = (ULONGEST *) xml_find_attribute (attributes, "begin")->value;
1856 end = (ULONGEST *) xml_find_attribute (attributes, "end")->value;
1858 block = VEC_safe_push (btrace_block_s, btrace->variant.bts.blocks, NULL);
1859 block->begin = *begin;
1863 /* Parse a "raw" xml record. */
1866 parse_xml_raw (struct gdb_xml_parser *parser, const char *body_text,
1867 gdb_byte **pdata, size_t *psize)
1869 struct cleanup *cleanup;
1870 gdb_byte *data, *bin;
1873 len = strlen (body_text);
1875 gdb_xml_error (parser, _("Bad raw data size."));
1879 bin = data = (gdb_byte *) xmalloc (size);
1880 cleanup = make_cleanup (xfree, data);
1882 /* We use hex encoding - see common/rsp-low.h. */
1890 if (hi == 0 || lo == 0)
1891 gdb_xml_error (parser, _("Bad hex encoding."));
1893 *bin++ = fromhex (hi) * 16 + fromhex (lo);
1897 discard_cleanups (cleanup);
1903 /* Parse a btrace pt-config "cpu" xml record. */
1906 parse_xml_btrace_pt_config_cpu (struct gdb_xml_parser *parser,
1907 const struct gdb_xml_element *element,
1909 VEC (gdb_xml_value_s) *attributes)
1911 struct btrace_data *btrace;
1913 ULONGEST *family, *model, *stepping;
1915 vendor = (const char *) xml_find_attribute (attributes, "vendor")->value;
1916 family = (ULONGEST *) xml_find_attribute (attributes, "family")->value;
1917 model = (ULONGEST *) xml_find_attribute (attributes, "model")->value;
1918 stepping = (ULONGEST *) xml_find_attribute (attributes, "stepping")->value;
1920 btrace = (struct btrace_data *) user_data;
1922 if (strcmp (vendor, "GenuineIntel") == 0)
1923 btrace->variant.pt.config.cpu.vendor = CV_INTEL;
1925 btrace->variant.pt.config.cpu.family = *family;
1926 btrace->variant.pt.config.cpu.model = *model;
1927 btrace->variant.pt.config.cpu.stepping = *stepping;
1930 /* Parse a btrace pt "raw" xml record. */
1933 parse_xml_btrace_pt_raw (struct gdb_xml_parser *parser,
1934 const struct gdb_xml_element *element,
1935 void *user_data, const char *body_text)
1937 struct btrace_data *btrace;
1939 btrace = (struct btrace_data *) user_data;
1940 parse_xml_raw (parser, body_text, &btrace->variant.pt.data,
1941 &btrace->variant.pt.size);
1944 /* Parse a btrace "pt" xml record. */
1947 parse_xml_btrace_pt (struct gdb_xml_parser *parser,
1948 const struct gdb_xml_element *element,
1949 void *user_data, VEC (gdb_xml_value_s) *attributes)
1951 struct btrace_data *btrace;
1953 btrace = (struct btrace_data *) user_data;
1954 btrace->format = BTRACE_FORMAT_PT;
1955 btrace->variant.pt.config.cpu.vendor = CV_UNKNOWN;
1956 btrace->variant.pt.data = NULL;
1957 btrace->variant.pt.size = 0;
1960 static const struct gdb_xml_attribute block_attributes[] = {
1961 { "begin", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
1962 { "end", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
1963 { NULL, GDB_XML_AF_NONE, NULL, NULL }
1966 static const struct gdb_xml_attribute btrace_pt_config_cpu_attributes[] = {
1967 { "vendor", GDB_XML_AF_NONE, NULL, NULL },
1968 { "family", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
1969 { "model", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
1970 { "stepping", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
1971 { NULL, GDB_XML_AF_NONE, NULL, NULL }
1974 static const struct gdb_xml_element btrace_pt_config_children[] = {
1975 { "cpu", btrace_pt_config_cpu_attributes, NULL, GDB_XML_EF_OPTIONAL,
1976 parse_xml_btrace_pt_config_cpu, NULL },
1977 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
1980 static const struct gdb_xml_element btrace_pt_children[] = {
1981 { "pt-config", NULL, btrace_pt_config_children, GDB_XML_EF_OPTIONAL, NULL,
1983 { "raw", NULL, NULL, GDB_XML_EF_OPTIONAL, NULL, parse_xml_btrace_pt_raw },
1984 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
1987 static const struct gdb_xml_attribute btrace_attributes[] = {
1988 { "version", GDB_XML_AF_NONE, NULL, NULL },
1989 { NULL, GDB_XML_AF_NONE, NULL, NULL }
1992 static const struct gdb_xml_element btrace_children[] = {
1993 { "block", block_attributes, NULL,
1994 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL, parse_xml_btrace_block, NULL },
1995 { "pt", NULL, btrace_pt_children, GDB_XML_EF_OPTIONAL, parse_xml_btrace_pt,
1997 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2000 static const struct gdb_xml_element btrace_elements[] = {
2001 { "btrace", btrace_attributes, btrace_children, GDB_XML_EF_NONE,
2002 check_xml_btrace_version, NULL },
2003 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2006 #endif /* defined (HAVE_LIBEXPAT) */
2011 parse_xml_btrace (struct btrace_data *btrace, const char *buffer)
2013 struct cleanup *cleanup;
2016 #if defined (HAVE_LIBEXPAT)
2018 btrace->format = BTRACE_FORMAT_NONE;
2020 cleanup = make_cleanup_btrace_data (btrace);
2021 errcode = gdb_xml_parse_quick (_("btrace"), "btrace.dtd", btrace_elements,
2024 error (_("Error parsing branch trace."));
2026 /* Keep parse results. */
2027 discard_cleanups (cleanup);
2029 #else /* !defined (HAVE_LIBEXPAT) */
2031 error (_("Cannot process branch trace. XML parsing is not supported."));
2033 #endif /* !defined (HAVE_LIBEXPAT) */
2036 #if defined (HAVE_LIBEXPAT)
2038 /* Parse a btrace-conf "bts" xml record. */
2041 parse_xml_btrace_conf_bts (struct gdb_xml_parser *parser,
2042 const struct gdb_xml_element *element,
2043 void *user_data, VEC (gdb_xml_value_s) *attributes)
2045 struct btrace_config *conf;
2046 struct gdb_xml_value *size;
2048 conf = (struct btrace_config *) user_data;
2049 conf->format = BTRACE_FORMAT_BTS;
2052 size = xml_find_attribute (attributes, "size");
2054 conf->bts.size = (unsigned int) *(ULONGEST *) size->value;
2057 /* Parse a btrace-conf "pt" xml record. */
2060 parse_xml_btrace_conf_pt (struct gdb_xml_parser *parser,
2061 const struct gdb_xml_element *element,
2062 void *user_data, VEC (gdb_xml_value_s) *attributes)
2064 struct btrace_config *conf;
2065 struct gdb_xml_value *size;
2067 conf = (struct btrace_config *) user_data;
2068 conf->format = BTRACE_FORMAT_PT;
2071 size = xml_find_attribute (attributes, "size");
2073 conf->pt.size = (unsigned int) *(ULONGEST *) size->value;
2076 static const struct gdb_xml_attribute btrace_conf_pt_attributes[] = {
2077 { "size", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
2078 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2081 static const struct gdb_xml_attribute btrace_conf_bts_attributes[] = {
2082 { "size", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
2083 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2086 static const struct gdb_xml_element btrace_conf_children[] = {
2087 { "bts", btrace_conf_bts_attributes, NULL, GDB_XML_EF_OPTIONAL,
2088 parse_xml_btrace_conf_bts, NULL },
2089 { "pt", btrace_conf_pt_attributes, NULL, GDB_XML_EF_OPTIONAL,
2090 parse_xml_btrace_conf_pt, NULL },
2091 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2094 static const struct gdb_xml_attribute btrace_conf_attributes[] = {
2095 { "version", GDB_XML_AF_NONE, NULL, NULL },
2096 { NULL, GDB_XML_AF_NONE, NULL, NULL }
2099 static const struct gdb_xml_element btrace_conf_elements[] = {
2100 { "btrace-conf", btrace_conf_attributes, btrace_conf_children,
2101 GDB_XML_EF_NONE, NULL, NULL },
2102 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
2105 #endif /* defined (HAVE_LIBEXPAT) */
2110 parse_xml_btrace_conf (struct btrace_config *conf, const char *xml)
2114 #if defined (HAVE_LIBEXPAT)
2116 errcode = gdb_xml_parse_quick (_("btrace-conf"), "btrace-conf.dtd",
2117 btrace_conf_elements, xml, conf);
2119 error (_("Error parsing branch trace configuration."));
2121 #else /* !defined (HAVE_LIBEXPAT) */
2123 error (_("XML parsing is not supported."));
2125 #endif /* !defined (HAVE_LIBEXPAT) */
2130 const struct btrace_insn *
2131 btrace_insn_get (const struct btrace_insn_iterator *it)
2133 const struct btrace_function *bfun;
2134 unsigned int index, end;
2137 bfun = it->function;
2139 /* Check if the iterator points to a gap in the trace. */
2140 if (bfun->errcode != 0)
2143 /* The index is within the bounds of this function's instruction vector. */
2144 end = VEC_length (btrace_insn_s, bfun->insn);
2145 gdb_assert (0 < end);
2146 gdb_assert (index < end);
2148 return VEC_index (btrace_insn_s, bfun->insn, index);
2154 btrace_insn_number (const struct btrace_insn_iterator *it)
2156 const struct btrace_function *bfun;
2158 bfun = it->function;
2160 /* Return zero if the iterator points to a gap in the trace. */
2161 if (bfun->errcode != 0)
2164 return bfun->insn_offset + it->index;
2170 btrace_insn_begin (struct btrace_insn_iterator *it,
2171 const struct btrace_thread_info *btinfo)
2173 const struct btrace_function *bfun;
2175 bfun = btinfo->begin;
2177 error (_("No trace."));
2179 it->function = bfun;
2186 btrace_insn_end (struct btrace_insn_iterator *it,
2187 const struct btrace_thread_info *btinfo)
2189 const struct btrace_function *bfun;
2190 unsigned int length;
2194 error (_("No trace."));
2196 length = VEC_length (btrace_insn_s, bfun->insn);
2198 /* The last function may either be a gap or it contains the current
2199 instruction, which is one past the end of the execution trace; ignore
2204 it->function = bfun;
2211 btrace_insn_next (struct btrace_insn_iterator *it, unsigned int stride)
2213 const struct btrace_function *bfun;
2214 unsigned int index, steps;
2216 bfun = it->function;
2222 unsigned int end, space, adv;
2224 end = VEC_length (btrace_insn_s, bfun->insn);
2226 /* An empty function segment represents a gap in the trace. We count
2227 it as one instruction. */
2230 const struct btrace_function *next;
2232 next = bfun->flow.next;
2245 gdb_assert (0 < end);
2246 gdb_assert (index < end);
2248 /* Compute the number of instructions remaining in this segment. */
2249 space = end - index;
2251 /* Advance the iterator as far as possible within this segment. */
2252 adv = std::min (space, stride);
2257 /* Move to the next function if we're at the end of this one. */
2260 const struct btrace_function *next;
2262 next = bfun->flow.next;
2265 /* We stepped past the last function.
2267 Let's adjust the index to point to the last instruction in
2268 the previous function. */
2274 /* We now point to the first instruction in the new function. */
2279 /* We did make progress. */
2280 gdb_assert (adv > 0);
2283 /* Update the iterator. */
2284 it->function = bfun;
2293 btrace_insn_prev (struct btrace_insn_iterator *it, unsigned int stride)
2295 const struct btrace_function *bfun;
2296 unsigned int index, steps;
2298 bfun = it->function;
2306 /* Move to the previous function if we're at the start of this one. */
2309 const struct btrace_function *prev;
2311 prev = bfun->flow.prev;
2315 /* We point to one after the last instruction in the new function. */
2317 index = VEC_length (btrace_insn_s, bfun->insn);
2319 /* An empty function segment represents a gap in the trace. We count
2320 it as one instruction. */
2330 /* Advance the iterator as far as possible within this segment. */
2331 adv = std::min (index, stride);
2337 /* We did make progress. */
2338 gdb_assert (adv > 0);
2341 /* Update the iterator. */
2342 it->function = bfun;
2351 btrace_insn_cmp (const struct btrace_insn_iterator *lhs,
2352 const struct btrace_insn_iterator *rhs)
2354 unsigned int lnum, rnum;
2356 lnum = btrace_insn_number (lhs);
2357 rnum = btrace_insn_number (rhs);
2359 /* A gap has an instruction number of zero. Things are getting more
2360 complicated if gaps are involved.
2362 We take the instruction number offset from the iterator's function.
2363 This is the number of the first instruction after the gap.
2365 This is OK as long as both lhs and rhs point to gaps. If only one of
2366 them does, we need to adjust the number based on the other's regular
2367 instruction number. Otherwise, a gap might compare equal to an
2370 if (lnum == 0 && rnum == 0)
2372 lnum = lhs->function->insn_offset;
2373 rnum = rhs->function->insn_offset;
2377 lnum = lhs->function->insn_offset;
2384 rnum = rhs->function->insn_offset;
2390 return (int) (lnum - rnum);
2396 btrace_find_insn_by_number (struct btrace_insn_iterator *it,
2397 const struct btrace_thread_info *btinfo,
2398 unsigned int number)
2400 const struct btrace_function *bfun;
2401 unsigned int end, length;
2403 for (bfun = btinfo->end; bfun != NULL; bfun = bfun->flow.prev)
2406 if (bfun->errcode != 0)
2409 if (bfun->insn_offset <= number)
2416 length = VEC_length (btrace_insn_s, bfun->insn);
2417 gdb_assert (length > 0);
2419 end = bfun->insn_offset + length;
2423 it->function = bfun;
2424 it->index = number - bfun->insn_offset;
2431 const struct btrace_function *
2432 btrace_call_get (const struct btrace_call_iterator *it)
2434 return it->function;
2440 btrace_call_number (const struct btrace_call_iterator *it)
2442 const struct btrace_thread_info *btinfo;
2443 const struct btrace_function *bfun;
2446 btinfo = it->btinfo;
2447 bfun = it->function;
2449 return bfun->number;
2451 /* For the end iterator, i.e. bfun == NULL, we return one more than the
2452 number of the last function. */
2454 insns = VEC_length (btrace_insn_s, bfun->insn);
2456 /* If the function contains only a single instruction (i.e. the current
2457 instruction), it will be skipped and its number is already the number
2460 return bfun->number;
2462 /* Otherwise, return one more than the number of the last function. */
2463 return bfun->number + 1;
2469 btrace_call_begin (struct btrace_call_iterator *it,
2470 const struct btrace_thread_info *btinfo)
2472 const struct btrace_function *bfun;
2474 bfun = btinfo->begin;
2476 error (_("No trace."));
2478 it->btinfo = btinfo;
2479 it->function = bfun;
2485 btrace_call_end (struct btrace_call_iterator *it,
2486 const struct btrace_thread_info *btinfo)
2488 const struct btrace_function *bfun;
2492 error (_("No trace."));
2494 it->btinfo = btinfo;
2495 it->function = NULL;
2501 btrace_call_next (struct btrace_call_iterator *it, unsigned int stride)
2503 const struct btrace_function *bfun;
2506 bfun = it->function;
2508 while (bfun != NULL)
2510 const struct btrace_function *next;
2513 next = bfun->flow.next;
2516 /* Ignore the last function if it only contains a single
2517 (i.e. the current) instruction. */
2518 insns = VEC_length (btrace_insn_s, bfun->insn);
2523 if (stride == steps)
2530 it->function = bfun;
2537 btrace_call_prev (struct btrace_call_iterator *it, unsigned int stride)
2539 const struct btrace_thread_info *btinfo;
2540 const struct btrace_function *bfun;
2543 bfun = it->function;
2550 btinfo = it->btinfo;
2555 /* Ignore the last function if it only contains a single
2556 (i.e. the current) instruction. */
2557 insns = VEC_length (btrace_insn_s, bfun->insn);
2559 bfun = bfun->flow.prev;
2567 while (steps < stride)
2569 const struct btrace_function *prev;
2571 prev = bfun->flow.prev;
2579 it->function = bfun;
2586 btrace_call_cmp (const struct btrace_call_iterator *lhs,
2587 const struct btrace_call_iterator *rhs)
2589 unsigned int lnum, rnum;
2591 lnum = btrace_call_number (lhs);
2592 rnum = btrace_call_number (rhs);
2594 return (int) (lnum - rnum);
2600 btrace_find_call_by_number (struct btrace_call_iterator *it,
2601 const struct btrace_thread_info *btinfo,
2602 unsigned int number)
2604 const struct btrace_function *bfun;
2606 for (bfun = btinfo->end; bfun != NULL; bfun = bfun->flow.prev)
2610 bnum = bfun->number;
2613 it->btinfo = btinfo;
2614 it->function = bfun;
2618 /* Functions are ordered and numbered consecutively. We could bail out
2619 earlier. On the other hand, it is very unlikely that we search for
2620 a nonexistent function. */
2629 btrace_set_insn_history (struct btrace_thread_info *btinfo,
2630 const struct btrace_insn_iterator *begin,
2631 const struct btrace_insn_iterator *end)
2633 if (btinfo->insn_history == NULL)
2634 btinfo->insn_history = XCNEW (struct btrace_insn_history);
2636 btinfo->insn_history->begin = *begin;
2637 btinfo->insn_history->end = *end;
2643 btrace_set_call_history (struct btrace_thread_info *btinfo,
2644 const struct btrace_call_iterator *begin,
2645 const struct btrace_call_iterator *end)
2647 gdb_assert (begin->btinfo == end->btinfo);
2649 if (btinfo->call_history == NULL)
2650 btinfo->call_history = XCNEW (struct btrace_call_history);
2652 btinfo->call_history->begin = *begin;
2653 btinfo->call_history->end = *end;
2659 btrace_is_replaying (struct thread_info *tp)
2661 return tp->btrace.replay != NULL;
2667 btrace_is_empty (struct thread_info *tp)
2669 struct btrace_insn_iterator begin, end;
2670 struct btrace_thread_info *btinfo;
2672 btinfo = &tp->btrace;
2674 if (btinfo->begin == NULL)
2677 btrace_insn_begin (&begin, btinfo);
2678 btrace_insn_end (&end, btinfo);
2680 return btrace_insn_cmp (&begin, &end) == 0;
2683 /* Forward the cleanup request. */
2686 do_btrace_data_cleanup (void *arg)
2688 btrace_data_fini ((struct btrace_data *) arg);
2694 make_cleanup_btrace_data (struct btrace_data *data)
2696 return make_cleanup (do_btrace_data_cleanup, data);
2699 #if defined (HAVE_LIBIPT)
2701 /* Print a single packet. */
2704 pt_print_packet (const struct pt_packet *packet)
2706 switch (packet->type)
2709 printf_unfiltered (("[??: %x]"), packet->type);
2713 printf_unfiltered (("psb"));
2717 printf_unfiltered (("psbend"));
2721 printf_unfiltered (("pad"));
2725 printf_unfiltered (("tip %u: 0x%" PRIx64 ""),
2726 packet->payload.ip.ipc,
2727 packet->payload.ip.ip);
2731 printf_unfiltered (("tip.pge %u: 0x%" PRIx64 ""),
2732 packet->payload.ip.ipc,
2733 packet->payload.ip.ip);
2737 printf_unfiltered (("tip.pgd %u: 0x%" PRIx64 ""),
2738 packet->payload.ip.ipc,
2739 packet->payload.ip.ip);
2743 printf_unfiltered (("fup %u: 0x%" PRIx64 ""),
2744 packet->payload.ip.ipc,
2745 packet->payload.ip.ip);
2749 printf_unfiltered (("tnt-8 %u: 0x%" PRIx64 ""),
2750 packet->payload.tnt.bit_size,
2751 packet->payload.tnt.payload);
2755 printf_unfiltered (("tnt-64 %u: 0x%" PRIx64 ""),
2756 packet->payload.tnt.bit_size,
2757 packet->payload.tnt.payload);
2761 printf_unfiltered (("pip %" PRIx64 "%s"), packet->payload.pip.cr3,
2762 packet->payload.pip.nr ? (" nr") : (""));
2766 printf_unfiltered (("tsc %" PRIx64 ""), packet->payload.tsc.tsc);
2770 printf_unfiltered (("cbr %u"), packet->payload.cbr.ratio);
2774 switch (packet->payload.mode.leaf)
2777 printf_unfiltered (("mode %u"), packet->payload.mode.leaf);
2781 printf_unfiltered (("mode.exec%s%s"),
2782 packet->payload.mode.bits.exec.csl
2784 packet->payload.mode.bits.exec.csd
2785 ? (" cs.d") : (""));
2789 printf_unfiltered (("mode.tsx%s%s"),
2790 packet->payload.mode.bits.tsx.intx
2792 packet->payload.mode.bits.tsx.abrt
2793 ? (" abrt") : (""));
2799 printf_unfiltered (("ovf"));
2803 printf_unfiltered (("stop"));
2807 printf_unfiltered (("vmcs %" PRIx64 ""), packet->payload.vmcs.base);
2811 printf_unfiltered (("tma %x %x"), packet->payload.tma.ctc,
2812 packet->payload.tma.fc);
2816 printf_unfiltered (("mtc %x"), packet->payload.mtc.ctc);
2820 printf_unfiltered (("cyc %" PRIx64 ""), packet->payload.cyc.value);
2824 printf_unfiltered (("mnt %" PRIx64 ""), packet->payload.mnt.payload);
2829 /* Decode packets into MAINT using DECODER. */
2832 btrace_maint_decode_pt (struct btrace_maint_info *maint,
2833 struct pt_packet_decoder *decoder)
2839 struct btrace_pt_packet packet;
2841 errcode = pt_pkt_sync_forward (decoder);
2847 pt_pkt_get_offset (decoder, &packet.offset);
2849 errcode = pt_pkt_next (decoder, &packet.packet,
2850 sizeof(packet.packet));
2854 if (maint_btrace_pt_skip_pad == 0 || packet.packet.type != ppt_pad)
2856 packet.errcode = pt_errcode (errcode);
2857 VEC_safe_push (btrace_pt_packet_s, maint->variant.pt.packets,
2862 if (errcode == -pte_eos)
2865 packet.errcode = pt_errcode (errcode);
2866 VEC_safe_push (btrace_pt_packet_s, maint->variant.pt.packets,
2869 warning (_("Error at trace offset 0x%" PRIx64 ": %s."),
2870 packet.offset, pt_errstr (packet.errcode));
2873 if (errcode != -pte_eos)
2874 warning (_("Failed to synchronize onto the Intel Processor Trace "
2875 "stream: %s."), pt_errstr (pt_errcode (errcode)));
2878 /* Update the packet history in BTINFO. */
2881 btrace_maint_update_pt_packets (struct btrace_thread_info *btinfo)
2883 volatile struct gdb_exception except;
2884 struct pt_packet_decoder *decoder;
2885 struct btrace_data_pt *pt;
2886 struct pt_config config;
2889 pt = &btinfo->data.variant.pt;
2891 /* Nothing to do if there is no trace. */
2895 memset (&config, 0, sizeof(config));
2897 config.size = sizeof (config);
2898 config.begin = pt->data;
2899 config.end = pt->data + pt->size;
2901 config.cpu.vendor = pt_translate_cpu_vendor (pt->config.cpu.vendor);
2902 config.cpu.family = pt->config.cpu.family;
2903 config.cpu.model = pt->config.cpu.model;
2904 config.cpu.stepping = pt->config.cpu.stepping;
2906 errcode = pt_cpu_errata (&config.errata, &config.cpu);
2908 error (_("Failed to configure the Intel Processor Trace decoder: %s."),
2909 pt_errstr (pt_errcode (errcode)));
2911 decoder = pt_pkt_alloc_decoder (&config);
2912 if (decoder == NULL)
2913 error (_("Failed to allocate the Intel Processor Trace decoder."));
2917 btrace_maint_decode_pt (&btinfo->maint, decoder);
2919 CATCH (except, RETURN_MASK_ALL)
2921 pt_pkt_free_decoder (decoder);
2923 if (except.reason < 0)
2924 throw_exception (except);
2928 pt_pkt_free_decoder (decoder);
2931 #endif /* !defined (HAVE_LIBIPT) */
2933 /* Update the packet maintenance information for BTINFO and store the
2934 low and high bounds into BEGIN and END, respectively.
2935 Store the current iterator state into FROM and TO. */
2938 btrace_maint_update_packets (struct btrace_thread_info *btinfo,
2939 unsigned int *begin, unsigned int *end,
2940 unsigned int *from, unsigned int *to)
2942 switch (btinfo->data.format)
2951 case BTRACE_FORMAT_BTS:
2952 /* Nothing to do - we operate directly on BTINFO->DATA. */
2954 *end = VEC_length (btrace_block_s, btinfo->data.variant.bts.blocks);
2955 *from = btinfo->maint.variant.bts.packet_history.begin;
2956 *to = btinfo->maint.variant.bts.packet_history.end;
2959 #if defined (HAVE_LIBIPT)
2960 case BTRACE_FORMAT_PT:
2961 if (VEC_empty (btrace_pt_packet_s, btinfo->maint.variant.pt.packets))
2962 btrace_maint_update_pt_packets (btinfo);
2965 *end = VEC_length (btrace_pt_packet_s, btinfo->maint.variant.pt.packets);
2966 *from = btinfo->maint.variant.pt.packet_history.begin;
2967 *to = btinfo->maint.variant.pt.packet_history.end;
2969 #endif /* defined (HAVE_LIBIPT) */
2973 /* Print packets in BTINFO from BEGIN (inclusive) until END (exclusive) and
2974 update the current iterator position. */
2977 btrace_maint_print_packets (struct btrace_thread_info *btinfo,
2978 unsigned int begin, unsigned int end)
2980 switch (btinfo->data.format)
2985 case BTRACE_FORMAT_BTS:
2987 VEC (btrace_block_s) *blocks;
2990 blocks = btinfo->data.variant.bts.blocks;
2991 for (blk = begin; blk < end; ++blk)
2993 const btrace_block_s *block;
2995 block = VEC_index (btrace_block_s, blocks, blk);
2997 printf_unfiltered ("%u\tbegin: %s, end: %s\n", blk,
2998 core_addr_to_string_nz (block->begin),
2999 core_addr_to_string_nz (block->end));
3002 btinfo->maint.variant.bts.packet_history.begin = begin;
3003 btinfo->maint.variant.bts.packet_history.end = end;
3007 #if defined (HAVE_LIBIPT)
3008 case BTRACE_FORMAT_PT:
3010 VEC (btrace_pt_packet_s) *packets;
3013 packets = btinfo->maint.variant.pt.packets;
3014 for (pkt = begin; pkt < end; ++pkt)
3016 const struct btrace_pt_packet *packet;
3018 packet = VEC_index (btrace_pt_packet_s, packets, pkt);
3020 printf_unfiltered ("%u\t", pkt);
3021 printf_unfiltered ("0x%" PRIx64 "\t", packet->offset);
3023 if (packet->errcode == pte_ok)
3024 pt_print_packet (&packet->packet);
3026 printf_unfiltered ("[error: %s]", pt_errstr (packet->errcode));
3028 printf_unfiltered ("\n");
3031 btinfo->maint.variant.pt.packet_history.begin = begin;
3032 btinfo->maint.variant.pt.packet_history.end = end;
3035 #endif /* defined (HAVE_LIBIPT) */
3039 /* Read a number from an argument string. */
3042 get_uint (char **arg)
3044 char *begin, *end, *pos;
3045 unsigned long number;
3048 pos = skip_spaces (begin);
3050 if (!isdigit (*pos))
3051 error (_("Expected positive number, got: %s."), pos);
3053 number = strtoul (pos, &end, 10);
3054 if (number > UINT_MAX)
3055 error (_("Number too big."));
3057 *arg += (end - begin);
3059 return (unsigned int) number;
3062 /* Read a context size from an argument string. */
3065 get_context_size (char **arg)
3070 pos = skip_spaces (*arg);
3072 if (!isdigit (*pos))
3073 error (_("Expected positive number, got: %s."), pos);
3075 return strtol (pos, arg, 10);
3078 /* Complain about junk at the end of an argument string. */
3081 no_chunk (char *arg)
3084 error (_("Junk after argument: %s."), arg);
3087 /* The "maintenance btrace packet-history" command. */
3090 maint_btrace_packet_history_cmd (char *arg, int from_tty)
3092 struct btrace_thread_info *btinfo;
3093 struct thread_info *tp;
3094 unsigned int size, begin, end, from, to;
3096 tp = find_thread_ptid (inferior_ptid);
3098 error (_("No thread."));
3101 btinfo = &tp->btrace;
3103 btrace_maint_update_packets (btinfo, &begin, &end, &from, &to);
3106 printf_unfiltered (_("No trace.\n"));
3110 if (arg == NULL || *arg == 0 || strcmp (arg, "+") == 0)
3114 if (end - from < size)
3118 else if (strcmp (arg, "-") == 0)
3122 if (to - begin < size)
3128 from = get_uint (&arg);
3130 error (_("'%u' is out of range."), from);
3132 arg = skip_spaces (arg);
3135 arg = skip_spaces (++arg);
3140 size = get_context_size (&arg);
3144 if (end - from < size)
3148 else if (*arg == '-')
3151 size = get_context_size (&arg);
3155 /* Include the packet given as first argument. */
3159 if (to - begin < size)
3165 to = get_uint (&arg);
3167 /* Include the packet at the second argument and silently
3168 truncate the range. */
3181 if (end - from < size)
3189 btrace_maint_print_packets (btinfo, from, to);
3192 /* The "maintenance btrace clear-packet-history" command. */
3195 maint_btrace_clear_packet_history_cmd (char *args, int from_tty)
3197 struct btrace_thread_info *btinfo;
3198 struct thread_info *tp;
3200 if (args != NULL && *args != 0)
3201 error (_("Invalid argument."));
3203 tp = find_thread_ptid (inferior_ptid);
3205 error (_("No thread."));
3207 btinfo = &tp->btrace;
3209 /* Must clear the maint data before - it depends on BTINFO->DATA. */
3210 btrace_maint_clear (btinfo);
3211 btrace_data_clear (&btinfo->data);
3214 /* The "maintenance btrace clear" command. */
3217 maint_btrace_clear_cmd (char *args, int from_tty)
3219 struct btrace_thread_info *btinfo;
3220 struct thread_info *tp;
3222 if (args != NULL && *args != 0)
3223 error (_("Invalid argument."));
3225 tp = find_thread_ptid (inferior_ptid);
3227 error (_("No thread."));
3232 /* The "maintenance btrace" command. */
3235 maint_btrace_cmd (char *args, int from_tty)
3237 help_list (maint_btrace_cmdlist, "maintenance btrace ", all_commands,
3241 /* The "maintenance set btrace" command. */
3244 maint_btrace_set_cmd (char *args, int from_tty)
3246 help_list (maint_btrace_set_cmdlist, "maintenance set btrace ", all_commands,
3250 /* The "maintenance show btrace" command. */
3253 maint_btrace_show_cmd (char *args, int from_tty)
3255 help_list (maint_btrace_show_cmdlist, "maintenance show btrace ",
3256 all_commands, gdb_stdout);
3259 /* The "maintenance set btrace pt" command. */
3262 maint_btrace_pt_set_cmd (char *args, int from_tty)
3264 help_list (maint_btrace_pt_set_cmdlist, "maintenance set btrace pt ",
3265 all_commands, gdb_stdout);
3268 /* The "maintenance show btrace pt" command. */
3271 maint_btrace_pt_show_cmd (char *args, int from_tty)
3273 help_list (maint_btrace_pt_show_cmdlist, "maintenance show btrace pt ",
3274 all_commands, gdb_stdout);
3277 /* The "maintenance info btrace" command. */
3280 maint_info_btrace_cmd (char *args, int from_tty)
3282 struct btrace_thread_info *btinfo;
3283 struct thread_info *tp;
3284 const struct btrace_config *conf;
3286 if (args != NULL && *args != 0)
3287 error (_("Invalid argument."));
3289 tp = find_thread_ptid (inferior_ptid);
3291 error (_("No thread."));
3293 btinfo = &tp->btrace;
3295 conf = btrace_conf (btinfo);
3297 error (_("No btrace configuration."));
3299 printf_unfiltered (_("Format: %s.\n"),
3300 btrace_format_string (conf->format));
3302 switch (conf->format)
3307 case BTRACE_FORMAT_BTS:
3308 printf_unfiltered (_("Number of packets: %u.\n"),
3309 VEC_length (btrace_block_s,
3310 btinfo->data.variant.bts.blocks));
3313 #if defined (HAVE_LIBIPT)
3314 case BTRACE_FORMAT_PT:
3316 struct pt_version version;
3318 version = pt_library_version ();
3319 printf_unfiltered (_("Version: %u.%u.%u%s.\n"), version.major,
3320 version.minor, version.build,
3321 version.ext != NULL ? version.ext : "");
3323 btrace_maint_update_pt_packets (btinfo);
3324 printf_unfiltered (_("Number of packets: %u.\n"),
3325 VEC_length (btrace_pt_packet_s,
3326 btinfo->maint.variant.pt.packets));
3329 #endif /* defined (HAVE_LIBIPT) */
3333 /* The "maint show btrace pt skip-pad" show value function. */
3336 show_maint_btrace_pt_skip_pad (struct ui_file *file, int from_tty,
3337 struct cmd_list_element *c,
3340 fprintf_filtered (file, _("Skip PAD packets is %s.\n"), value);
3344 /* Initialize btrace maintenance commands. */
3346 void _initialize_btrace (void);
3348 _initialize_btrace (void)
3350 add_cmd ("btrace", class_maintenance, maint_info_btrace_cmd,
3351 _("Info about branch tracing data."), &maintenanceinfolist);
3353 add_prefix_cmd ("btrace", class_maintenance, maint_btrace_cmd,
3354 _("Branch tracing maintenance commands."),
3355 &maint_btrace_cmdlist, "maintenance btrace ",
3356 0, &maintenancelist);
3358 add_prefix_cmd ("btrace", class_maintenance, maint_btrace_set_cmd, _("\
3359 Set branch tracing specific variables."),
3360 &maint_btrace_set_cmdlist, "maintenance set btrace ",
3361 0, &maintenance_set_cmdlist);
3363 add_prefix_cmd ("pt", class_maintenance, maint_btrace_pt_set_cmd, _("\
3364 Set Intel Processor Trace specific variables."),
3365 &maint_btrace_pt_set_cmdlist, "maintenance set btrace pt ",
3366 0, &maint_btrace_set_cmdlist);
3368 add_prefix_cmd ("btrace", class_maintenance, maint_btrace_show_cmd, _("\
3369 Show branch tracing specific variables."),
3370 &maint_btrace_show_cmdlist, "maintenance show btrace ",
3371 0, &maintenance_show_cmdlist);
3373 add_prefix_cmd ("pt", class_maintenance, maint_btrace_pt_show_cmd, _("\
3374 Show Intel Processor Trace specific variables."),
3375 &maint_btrace_pt_show_cmdlist, "maintenance show btrace pt ",
3376 0, &maint_btrace_show_cmdlist);
3378 add_setshow_boolean_cmd ("skip-pad", class_maintenance,
3379 &maint_btrace_pt_skip_pad, _("\
3380 Set whether PAD packets should be skipped in the btrace packet history."), _("\
3381 Show whether PAD packets should be skipped in the btrace packet history."),_("\
3382 When enabled, PAD packets are ignored in the btrace packet history."),
3383 NULL, show_maint_btrace_pt_skip_pad,
3384 &maint_btrace_pt_set_cmdlist,
3385 &maint_btrace_pt_show_cmdlist);
3387 add_cmd ("packet-history", class_maintenance, maint_btrace_packet_history_cmd,
3388 _("Print the raw branch tracing data.\n\
3389 With no argument, print ten more packets after the previous ten-line print.\n\
3390 With '-' as argument print ten packets before a previous ten-line print.\n\
3391 One argument specifies the starting packet of a ten-line print.\n\
3392 Two arguments with comma between specify starting and ending packets to \
3394 Preceded with '+'/'-' the second argument specifies the distance from the \
3396 &maint_btrace_cmdlist);
3398 add_cmd ("clear-packet-history", class_maintenance,
3399 maint_btrace_clear_packet_history_cmd,
3400 _("Clears the branch tracing packet history.\n\
3401 Discards the raw branch tracing data but not the execution history data.\n\
3403 &maint_btrace_cmdlist);
3405 add_cmd ("clear", class_maintenance, maint_btrace_clear_cmd,
3406 _("Clears the branch tracing data.\n\
3407 Discards the raw branch tracing data and the execution history data.\n\
3408 The next 'record' command will fetch the branch tracing data anew.\n\
3410 &maint_btrace_cmdlist);