1 /* Branch trace support for GDB, the GNU debugger.
3 Copyright (C) 2013-2025 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/>. */
23 #include "gdbthread.h"
30 #include "filenames.h"
32 #include "gdbsupport/rsp-low.h"
33 #include "cli/cli-cmds.h"
34 #include "cli/cli-utils.h"
35 #include "extension.h"
38 /* For maintenance commands. */
39 #include "record-btrace.h"
46 /* Command lists for btrace maintenance commands. */
47 static struct cmd_list_element
*maint_btrace_cmdlist
;
48 static struct cmd_list_element
*maint_btrace_set_cmdlist
;
49 static struct cmd_list_element
*maint_btrace_show_cmdlist
;
50 static struct cmd_list_element
*maint_btrace_pt_set_cmdlist
;
51 static struct cmd_list_element
*maint_btrace_pt_show_cmdlist
;
53 /* Control whether to skip PAD packets when computing the packet history. */
54 static bool maint_btrace_pt_skip_pad
= true;
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 gdb_printf (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 sym
->print_name ();
88 return msym
->print_name ();
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 (sym
->symtab ());
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
+ bfun
->insn
.size ();
140 DEBUG_FTRACE ("%s: fun = %s, file = %s, level = %d, insn = [%u; %u)",
141 prefix
, fun
, file
, level
, ibegin
, iend
);
144 /* Return the number of instructions in a given function call segment. */
147 ftrace_call_num_insn (const struct btrace_function
* bfun
)
152 /* A gap is always counted as one instruction. */
153 if (bfun
->errcode
!= 0)
156 return bfun
->insn
.size ();
159 /* Return the function segment with the given NUMBER or NULL if no such segment
160 exists. BTINFO is the branch trace information for the current thread. */
162 static struct btrace_function
*
163 ftrace_find_call_by_number (struct btrace_thread_info
*btinfo
,
166 if (number
== 0 || number
> btinfo
->functions
.size ())
169 return &btinfo
->functions
[number
- 1];
172 /* A const version of the function above. */
174 static const struct btrace_function
*
175 ftrace_find_call_by_number (const struct btrace_thread_info
*btinfo
,
178 if (number
== 0 || number
> btinfo
->functions
.size ())
181 return &btinfo
->functions
[number
- 1];
184 /* Return non-zero if BFUN does not match MFUN and FUN,
185 return zero otherwise. */
188 ftrace_function_switched (const struct btrace_function
*bfun
,
189 const struct minimal_symbol
*mfun
,
190 const struct symbol
*fun
)
192 struct minimal_symbol
*msym
;
198 /* If the minimal symbol changed, we certainly switched functions. */
199 if (mfun
!= NULL
&& msym
!= NULL
200 && strcmp (mfun
->linkage_name (), msym
->linkage_name ()) != 0)
203 /* If the symbol changed, we certainly switched functions. */
204 if (fun
!= NULL
&& sym
!= NULL
)
206 const char *bfname
, *fname
;
208 /* Check the function name. */
209 if (strcmp (fun
->linkage_name (), sym
->linkage_name ()) != 0)
212 /* Check the location of those functions, as well. */
213 bfname
= symtab_to_fullname (sym
->symtab ());
214 fname
= symtab_to_fullname (fun
->symtab ());
215 if (filename_cmp (fname
, bfname
) != 0)
219 /* If we lost symbol information, we switched functions. */
220 if (!(msym
== NULL
&& sym
== NULL
) && mfun
== NULL
&& fun
== NULL
)
223 /* If we gained symbol information, we switched functions. */
224 if (msym
== NULL
&& sym
== NULL
&& !(mfun
== NULL
&& fun
== NULL
))
230 /* Allocate and initialize a new branch trace function segment at the end of
232 BTINFO is the branch trace information for the current thread.
233 MFUN and FUN are the symbol information we have for this function.
234 This invalidates all struct btrace_function pointer currently held. */
236 static struct btrace_function
*
237 ftrace_new_function (struct btrace_thread_info
*btinfo
,
238 struct minimal_symbol
*mfun
,
242 unsigned int number
, insn_offset
;
244 if (btinfo
->functions
.empty ())
246 /* Start counting NUMBER and INSN_OFFSET at one. */
253 const struct btrace_function
*prev
= &btinfo
->functions
.back ();
255 number
= prev
->number
+ 1;
256 insn_offset
= prev
->insn_offset
+ ftrace_call_num_insn (prev
);
259 return &btinfo
->functions
.emplace_back (mfun
, fun
, number
, insn_offset
,
263 /* Update the UP field of a function segment. */
266 ftrace_update_caller (struct btrace_function
*bfun
,
267 struct btrace_function
*caller
,
268 btrace_function_flags flags
)
271 ftrace_debug (bfun
, "updating caller");
273 bfun
->up
= caller
->number
;
276 ftrace_debug (bfun
, "set caller");
277 ftrace_debug (caller
, "..to");
280 /* Fix up the caller for all segments of a function. */
283 ftrace_fixup_caller (struct btrace_thread_info
*btinfo
,
284 struct btrace_function
*bfun
,
285 struct btrace_function
*caller
,
286 btrace_function_flags flags
)
288 unsigned int prev
, next
;
292 ftrace_update_caller (bfun
, caller
, flags
);
294 /* Update all function segments belonging to the same function. */
295 for (; prev
!= 0; prev
= bfun
->prev
)
297 bfun
= ftrace_find_call_by_number (btinfo
, prev
);
298 ftrace_update_caller (bfun
, caller
, flags
);
301 for (; next
!= 0; next
= bfun
->next
)
303 bfun
= ftrace_find_call_by_number (btinfo
, next
);
304 ftrace_update_caller (bfun
, caller
, flags
);
308 /* Add a new function segment for a call at the end of the trace.
309 BTINFO is the branch trace information for the current thread.
310 MFUN and FUN are the symbol information we have for this function. */
312 static struct btrace_function
*
313 ftrace_new_call (struct btrace_thread_info
*btinfo
,
314 struct minimal_symbol
*mfun
,
317 const unsigned int length
= btinfo
->functions
.size ();
318 struct btrace_function
*bfun
= ftrace_new_function (btinfo
, mfun
, fun
);
323 ftrace_debug (bfun
, "new call");
328 /* Add a new function segment for a tail call at the end of the trace.
329 BTINFO is the branch trace information for the current thread.
330 MFUN and FUN are the symbol information we have for this function. */
332 static struct btrace_function
*
333 ftrace_new_tailcall (struct btrace_thread_info
*btinfo
,
334 struct minimal_symbol
*mfun
,
337 const unsigned int length
= btinfo
->functions
.size ();
338 struct btrace_function
*bfun
= ftrace_new_function (btinfo
, mfun
, fun
);
342 bfun
->flags
|= BFUN_UP_LINKS_TO_TAILCALL
;
344 ftrace_debug (bfun
, "new tail call");
349 /* Return the caller of BFUN or NULL if there is none. This function skips
350 tail calls in the call chain. BTINFO is the branch trace information for
351 the current thread. */
352 static struct btrace_function
*
353 ftrace_get_caller (struct btrace_thread_info
*btinfo
,
354 struct btrace_function
*bfun
)
356 for (; bfun
!= NULL
; bfun
= ftrace_find_call_by_number (btinfo
, bfun
->up
))
357 if ((bfun
->flags
& BFUN_UP_LINKS_TO_TAILCALL
) == 0)
358 return ftrace_find_call_by_number (btinfo
, bfun
->up
);
363 /* Find the innermost caller in the back trace of BFUN with MFUN/FUN
364 symbol information. BTINFO is the branch trace information for the current
367 static struct btrace_function
*
368 ftrace_find_caller (struct btrace_thread_info
*btinfo
,
369 struct btrace_function
*bfun
,
370 struct minimal_symbol
*mfun
,
373 for (; bfun
!= NULL
; bfun
= ftrace_find_call_by_number (btinfo
, bfun
->up
))
375 /* Skip functions with incompatible symbol information. */
376 if (ftrace_function_switched (bfun
, mfun
, fun
))
379 /* This is the function segment we're looking for. */
386 /* Find the innermost caller in the back trace of BFUN, skipping all
387 function segments that do not end with a call instruction (e.g.
388 tail calls ending with a jump). BTINFO is the branch trace information for
389 the current thread. */
391 static struct btrace_function
*
392 ftrace_find_call (struct btrace_thread_info
*btinfo
,
393 struct btrace_function
*bfun
)
395 for (; bfun
!= NULL
; bfun
= ftrace_find_call_by_number (btinfo
, bfun
->up
))
398 if (bfun
->errcode
!= 0)
401 btrace_insn
&last
= bfun
->insn
.back ();
403 if (last
.iclass
== BTRACE_INSN_CALL
)
410 /* Add a continuation segment for a function into which we return at the end of
412 BTINFO is the branch trace information for the current thread.
413 MFUN and FUN are the symbol information we have for this function. */
415 static struct btrace_function
*
416 ftrace_new_return (struct btrace_thread_info
*btinfo
,
417 struct minimal_symbol
*mfun
,
420 struct btrace_function
*prev
, *bfun
, *caller
;
422 bfun
= ftrace_new_function (btinfo
, mfun
, fun
);
423 prev
= ftrace_find_call_by_number (btinfo
, bfun
->number
- 1);
425 /* It is important to start at PREV's caller. Otherwise, we might find
426 PREV itself, if PREV is a recursive function. */
427 caller
= ftrace_find_call_by_number (btinfo
, prev
->up
);
428 caller
= ftrace_find_caller (btinfo
, caller
, mfun
, fun
);
431 /* The caller of PREV is the preceding btrace function segment in this
432 function instance. */
433 gdb_assert (caller
->next
== 0);
435 caller
->next
= bfun
->number
;
436 bfun
->prev
= caller
->number
;
438 /* Maintain the function level. */
439 bfun
->level
= caller
->level
;
441 /* Maintain the call stack. */
442 bfun
->up
= caller
->up
;
443 bfun
->flags
= caller
->flags
;
445 ftrace_debug (bfun
, "new return");
449 /* We did not find a caller. This could mean that something went
450 wrong or that the call is simply not included in the trace. */
452 /* Let's search for some actual call. */
453 caller
= ftrace_find_call_by_number (btinfo
, prev
->up
);
454 caller
= ftrace_find_call (btinfo
, caller
);
457 /* There is no call in PREV's back trace. We assume that the
458 branch trace did not include it. */
460 /* Let's find the topmost function and add a new caller for it.
461 This should handle a series of initial tail calls. */
462 while (prev
->up
!= 0)
463 prev
= ftrace_find_call_by_number (btinfo
, prev
->up
);
465 bfun
->level
= prev
->level
- 1;
467 /* Fix up the call stack for PREV. */
468 ftrace_fixup_caller (btinfo
, prev
, bfun
, BFUN_UP_LINKS_TO_RET
);
470 ftrace_debug (bfun
, "new return - no caller");
474 /* There is a call in PREV's back trace to which we should have
475 returned but didn't. Let's start a new, separate back trace
476 from PREV's level. */
477 bfun
->level
= prev
->level
- 1;
479 /* We fix up the back trace for PREV but leave other function segments
480 on the same level as they are.
481 This should handle things like schedule () correctly where we're
482 switching contexts. */
483 prev
->up
= bfun
->number
;
484 prev
->flags
= BFUN_UP_LINKS_TO_RET
;
486 ftrace_debug (bfun
, "new return - unknown caller");
493 /* Add a new function segment for a function switch at the end of the trace.
494 BTINFO is the branch trace information for the current thread.
495 MFUN and FUN are the symbol information we have for this function. */
497 static struct btrace_function
*
498 ftrace_new_switch (struct btrace_thread_info
*btinfo
,
499 struct minimal_symbol
*mfun
,
502 struct btrace_function
*prev
, *bfun
;
504 /* This is an unexplained function switch. We can't really be sure about the
505 call stack, yet the best I can think of right now is to preserve it. */
506 bfun
= ftrace_new_function (btinfo
, mfun
, fun
);
507 prev
= ftrace_find_call_by_number (btinfo
, bfun
->number
- 1);
509 bfun
->flags
= prev
->flags
;
511 ftrace_debug (bfun
, "new switch");
516 /* Add a new function segment for a gap in the trace due to a decode error at
517 the end of the trace.
518 BTINFO is the branch trace information for the current thread.
519 ERRCODE is the format-specific error code. */
521 static struct btrace_function
*
522 ftrace_new_gap (struct btrace_thread_info
*btinfo
, int errcode
,
523 std::vector
<unsigned int> &gaps
)
525 struct btrace_function
*bfun
;
527 if (btinfo
->functions
.empty ())
528 bfun
= ftrace_new_function (btinfo
, NULL
, NULL
);
531 /* We hijack the previous function segment if it was empty. */
532 bfun
= &btinfo
->functions
.back ();
533 if (bfun
->errcode
!= 0 || !bfun
->insn
.empty ())
534 bfun
= ftrace_new_function (btinfo
, NULL
, NULL
);
537 bfun
->errcode
= errcode
;
538 gaps
.push_back (bfun
->number
);
540 ftrace_debug (bfun
, "new gap");
545 /* Update the current function segment at the end of the trace in BTINFO with
546 respect to the instruction at PC. This may create new function segments.
547 Return the chronologically latest function segment, never NULL. */
549 static struct btrace_function
*
550 ftrace_update_function (struct btrace_thread_info
*btinfo
,
551 std::optional
<CORE_ADDR
> pc
)
553 struct minimal_symbol
*mfun
= nullptr;
554 struct symbol
*fun
= nullptr;
556 /* Try to determine the function we're in. We use both types of symbols
557 to avoid surprises when we sometimes get a full symbol and sometimes
558 only a minimal symbol. */
561 fun
= find_pc_function (*pc
);
562 bound_minimal_symbol bmfun
= lookup_minimal_symbol_by_pc (*pc
);
565 if (fun
== nullptr && mfun
== nullptr)
566 DEBUG_FTRACE ("no symbol at %s", core_addr_to_string_nz (*pc
));
569 /* If we didn't have a function, we create one. */
570 if (btinfo
->functions
.empty ())
571 return ftrace_new_function (btinfo
, mfun
, fun
);
573 /* If we had a gap before, we create a function. */
574 btrace_function
*bfun
= &btinfo
->functions
.back ();
575 if (bfun
->errcode
!= 0)
576 return ftrace_new_function (btinfo
, mfun
, fun
);
578 /* If there is no valid PC, which can happen for events with a
579 suppressed IP, we can't do more than return the last bfun. */
580 if (!pc
.has_value ())
583 /* Check the last instruction, if we have one.
584 We do this check first, since it allows us to fill in the call stack
585 links in addition to the normal flow links. */
586 btrace_insn
*last
= NULL
;
587 if (!bfun
->insn
.empty ())
588 last
= &bfun
->insn
.back ();
592 switch (last
->iclass
)
594 case BTRACE_INSN_RETURN
:
598 /* On some systems, _dl_runtime_resolve returns to the resolved
599 function instead of jumping to it. From our perspective,
600 however, this is a tailcall.
601 If we treated it as return, we wouldn't be able to find the
602 resolved function in our stack back trace. Hence, we would
603 lose the current stack back trace and start anew with an empty
604 back trace. When the resolved function returns, we would then
605 create a stack back trace with the same function names but
606 different frame id's. This will confuse stepping. */
607 fname
= ftrace_print_function_name (bfun
);
608 if (strcmp (fname
, "_dl_runtime_resolve") == 0)
609 return ftrace_new_tailcall (btinfo
, mfun
, fun
);
611 return ftrace_new_return (btinfo
, mfun
, fun
);
614 case BTRACE_INSN_CALL
:
615 /* Ignore calls to the next instruction. They are used for PIC. */
616 if (last
->pc
+ last
->size
== *pc
)
619 return ftrace_new_call (btinfo
, mfun
, fun
);
621 case BTRACE_INSN_JUMP
:
625 start
= get_pc_function_start (*pc
);
627 /* A jump to the start of a function is (typically) a tail call. */
629 return ftrace_new_tailcall (btinfo
, mfun
, fun
);
631 /* Some versions of _Unwind_RaiseException use an indirect
632 jump to 'return' to the exception handler of the caller
633 handling the exception instead of a return. Let's restrict
634 this heuristic to that and related functions. */
635 const char *fname
= ftrace_print_function_name (bfun
);
636 if (strncmp (fname
, "_Unwind_", strlen ("_Unwind_")) == 0)
638 struct btrace_function
*caller
639 = ftrace_find_call_by_number (btinfo
, bfun
->up
);
640 caller
= ftrace_find_caller (btinfo
, caller
, mfun
, fun
);
642 return ftrace_new_return (btinfo
, mfun
, fun
);
645 /* If we can't determine the function for PC, we treat a jump at
646 the end of the block as tail call if we're switching functions
647 and as an intra-function branch if we don't. */
648 if (start
== 0 && ftrace_function_switched (bfun
, mfun
, fun
))
649 return ftrace_new_tailcall (btinfo
, mfun
, fun
);
654 case BTRACE_INSN_AUX
:
655 /* An aux insn couldn't have switched the function. But the
656 segment might not have had a symbol name resolved yet, as events
657 might not have an IP. Use the current IP in that case and update
659 if (bfun
->sym
== nullptr && bfun
->msym
== nullptr)
668 /* Check if we're switching functions for some other reason. */
669 if (ftrace_function_switched (bfun
, mfun
, fun
))
671 DEBUG_FTRACE ("switching from %s in %s at %s",
672 ftrace_print_insn_addr (last
),
673 ftrace_print_function_name (bfun
),
674 ftrace_print_filename (bfun
));
676 return ftrace_new_switch (btinfo
, mfun
, fun
);
682 /* Add the instruction at PC to BFUN's instructions. */
685 ftrace_update_insns (struct btrace_function
*bfun
, const btrace_insn
&insn
)
687 bfun
->insn
.push_back (insn
);
689 if (insn
.iclass
== BTRACE_INSN_AUX
)
690 bfun
->flags
|= BFUN_CONTAINS_AUX
;
692 bfun
->flags
|= BFUN_CONTAINS_NON_AUX
;
694 if (record_debug
> 1)
695 ftrace_debug (bfun
, "update insn");
698 /* Classify the instruction at PC. */
700 static enum btrace_insn_class
701 ftrace_classify_insn (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
703 enum btrace_insn_class iclass
;
705 iclass
= BTRACE_INSN_OTHER
;
708 if (gdbarch_insn_is_call (gdbarch
, pc
))
709 iclass
= BTRACE_INSN_CALL
;
710 else if (gdbarch_insn_is_ret (gdbarch
, pc
))
711 iclass
= BTRACE_INSN_RETURN
;
712 else if (gdbarch_insn_is_jump (gdbarch
, pc
))
713 iclass
= BTRACE_INSN_JUMP
;
715 catch (const gdb_exception_error
&error
)
722 /* Try to match the back trace at LHS to the back trace at RHS. Returns the
723 number of matching function segments or zero if the back traces do not
724 match. BTINFO is the branch trace information for the current thread. */
727 ftrace_match_backtrace (struct btrace_thread_info
*btinfo
,
728 struct btrace_function
*lhs
,
729 struct btrace_function
*rhs
)
733 for (matches
= 0; lhs
!= NULL
&& rhs
!= NULL
; ++matches
)
735 if (ftrace_function_switched (lhs
, rhs
->msym
, rhs
->sym
))
738 lhs
= ftrace_get_caller (btinfo
, lhs
);
739 rhs
= ftrace_get_caller (btinfo
, rhs
);
745 /* Add ADJUSTMENT to the level of BFUN and succeeding function segments.
746 BTINFO is the branch trace information for the current thread. */
749 ftrace_fixup_level (struct btrace_thread_info
*btinfo
,
750 struct btrace_function
*bfun
, int adjustment
)
755 DEBUG_FTRACE ("fixup level (%+d)", adjustment
);
756 ftrace_debug (bfun
, "..bfun");
760 bfun
->level
+= adjustment
;
761 bfun
= ftrace_find_call_by_number (btinfo
, bfun
->number
+ 1);
765 /* Recompute the global level offset. Traverse the function trace and compute
766 the global level offset as the negative of the minimal function level. */
769 ftrace_compute_global_level_offset (struct btrace_thread_info
*btinfo
)
776 if (btinfo
->functions
.empty ())
779 unsigned int length
= btinfo
->functions
.size() - 1;
780 for (unsigned int i
= 0; i
< length
; ++i
)
781 level
= std::min (level
, btinfo
->functions
[i
].level
);
783 /* The last function segment contains the current instruction, which is not
784 really part of the trace. If it contains just this one instruction, we
785 ignore the segment. */
786 struct btrace_function
*last
= &btinfo
->functions
.back();
787 if (last
->insn
.size () != 1)
788 level
= std::min (level
, last
->level
);
790 DEBUG_FTRACE ("setting global level offset: %d", -level
);
791 btinfo
->level
= -level
;
794 /* Connect the function segments PREV and NEXT in a bottom-to-top walk as in
795 ftrace_connect_backtrace. BTINFO is the branch trace information for the
799 ftrace_connect_bfun (struct btrace_thread_info
*btinfo
,
800 struct btrace_function
*prev
,
801 struct btrace_function
*next
)
803 DEBUG_FTRACE ("connecting...");
804 ftrace_debug (prev
, "..prev");
805 ftrace_debug (next
, "..next");
807 /* The function segments are not yet connected. */
808 gdb_assert (prev
->next
== 0);
809 gdb_assert (next
->prev
== 0);
811 prev
->next
= next
->number
;
812 next
->prev
= prev
->number
;
814 /* We may have moved NEXT to a different function level. */
815 ftrace_fixup_level (btinfo
, next
, prev
->level
- next
->level
);
817 /* If we run out of back trace for one, let's use the other's. */
820 const btrace_function_flags flags
= next
->flags
;
822 next
= ftrace_find_call_by_number (btinfo
, next
->up
);
825 DEBUG_FTRACE ("using next's callers");
826 ftrace_fixup_caller (btinfo
, prev
, next
, flags
);
829 else if (next
->up
== 0)
831 const btrace_function_flags flags
= prev
->flags
;
833 prev
= ftrace_find_call_by_number (btinfo
, prev
->up
);
836 DEBUG_FTRACE ("using prev's callers");
837 ftrace_fixup_caller (btinfo
, next
, prev
, flags
);
842 /* PREV may have a tailcall caller, NEXT can't. If it does, fixup the up
843 link to add the tail callers to NEXT's back trace.
845 This removes NEXT->UP from NEXT's back trace. It will be added back
846 when connecting NEXT and PREV's callers - provided they exist.
848 If PREV's back trace consists of a series of tail calls without an
849 actual call, there will be no further connection and NEXT's caller will
850 be removed for good. To catch this case, we handle it here and connect
851 the top of PREV's back trace to NEXT's caller. */
852 if ((prev
->flags
& BFUN_UP_LINKS_TO_TAILCALL
) != 0)
854 struct btrace_function
*caller
;
855 btrace_function_flags next_flags
, prev_flags
;
857 /* We checked NEXT->UP above so CALLER can't be NULL. */
858 caller
= ftrace_find_call_by_number (btinfo
, next
->up
);
859 next_flags
= next
->flags
;
860 prev_flags
= prev
->flags
;
862 DEBUG_FTRACE ("adding prev's tail calls to next");
864 prev
= ftrace_find_call_by_number (btinfo
, prev
->up
);
865 ftrace_fixup_caller (btinfo
, next
, prev
, prev_flags
);
867 for (; prev
!= NULL
; prev
= ftrace_find_call_by_number (btinfo
,
870 /* At the end of PREV's back trace, continue with CALLER. */
873 DEBUG_FTRACE ("fixing up link for tailcall chain");
874 ftrace_debug (prev
, "..top");
875 ftrace_debug (caller
, "..up");
877 ftrace_fixup_caller (btinfo
, prev
, caller
, next_flags
);
879 /* If we skipped any tail calls, this may move CALLER to a
880 different function level.
882 Note that changing CALLER's level is only OK because we
883 know that this is the last iteration of the bottom-to-top
884 walk in ftrace_connect_backtrace.
886 Otherwise we will fix up CALLER's level when we connect it
887 to PREV's caller in the next iteration. */
888 ftrace_fixup_level (btinfo
, caller
,
889 prev
->level
- caller
->level
- 1);
893 /* There's nothing to do if we find a real call. */
894 if ((prev
->flags
& BFUN_UP_LINKS_TO_TAILCALL
) == 0)
896 DEBUG_FTRACE ("will fix up link in next iteration");
904 /* Connect function segments on the same level in the back trace at LHS and RHS.
905 The back traces at LHS and RHS are expected to match according to
906 ftrace_match_backtrace. BTINFO is the branch trace information for the
910 ftrace_connect_backtrace (struct btrace_thread_info
*btinfo
,
911 struct btrace_function
*lhs
,
912 struct btrace_function
*rhs
)
914 while (lhs
!= NULL
&& rhs
!= NULL
)
916 struct btrace_function
*prev
, *next
;
918 gdb_assert (!ftrace_function_switched (lhs
, rhs
->msym
, rhs
->sym
));
920 /* Connecting LHS and RHS may change the up link. */
924 lhs
= ftrace_get_caller (btinfo
, lhs
);
925 rhs
= ftrace_get_caller (btinfo
, rhs
);
927 ftrace_connect_bfun (btinfo
, prev
, next
);
931 /* Bridge the gap between two function segments left and right of a gap if their
932 respective back traces match in at least MIN_MATCHES functions. BTINFO is
933 the branch trace information for the current thread.
935 Returns non-zero if the gap could be bridged, zero otherwise. */
938 ftrace_bridge_gap (struct btrace_thread_info
*btinfo
,
939 struct btrace_function
*lhs
, struct btrace_function
*rhs
,
942 struct btrace_function
*best_l
, *best_r
, *cand_l
, *cand_r
;
945 DEBUG_FTRACE ("checking gap at insn %u (req matches: %d)",
946 rhs
->insn_offset
- 1, min_matches
);
952 /* We search the back traces of LHS and RHS for valid connections and connect
953 the two function segments that give the longest combined back trace. */
955 for (cand_l
= lhs
; cand_l
!= NULL
;
956 cand_l
= ftrace_get_caller (btinfo
, cand_l
))
957 for (cand_r
= rhs
; cand_r
!= NULL
;
958 cand_r
= ftrace_get_caller (btinfo
, cand_r
))
962 matches
= ftrace_match_backtrace (btinfo
, cand_l
, cand_r
);
963 if (best_matches
< matches
)
965 best_matches
= matches
;
971 /* We need at least MIN_MATCHES matches. */
972 gdb_assert (min_matches
> 0);
973 if (best_matches
< min_matches
)
976 DEBUG_FTRACE ("..matches: %d", best_matches
);
978 /* We will fix up the level of BEST_R and succeeding function segments such
979 that BEST_R's level matches BEST_L's when we connect BEST_L to BEST_R.
981 This will ignore the level of RHS and following if BEST_R != RHS. I.e. if
982 BEST_R is a successor of RHS in the back trace of RHS (phases 1 and 3).
984 To catch this, we already fix up the level here where we can start at RHS
985 instead of at BEST_R. We will ignore the level fixup when connecting
986 BEST_L to BEST_R as they will already be on the same level. */
987 ftrace_fixup_level (btinfo
, rhs
, best_l
->level
- best_r
->level
);
989 ftrace_connect_backtrace (btinfo
, best_l
, best_r
);
994 /* Try to bridge gaps due to overflow or decode errors by connecting the
995 function segments that are separated by the gap. */
998 btrace_bridge_gaps (struct thread_info
*tp
, std::vector
<unsigned int> &gaps
)
1000 struct btrace_thread_info
*btinfo
= &tp
->btrace
;
1001 std::vector
<unsigned int> remaining
;
1004 DEBUG ("bridge gaps");
1006 /* We require a minimum amount of matches for bridging a gap. The number of
1007 required matches will be lowered with each iteration.
1009 The more matches the higher our confidence that the bridging is correct.
1010 For big gaps or small traces, however, it may not be feasible to require a
1011 high number of matches. */
1012 for (min_matches
= 5; min_matches
> 0; --min_matches
)
1014 /* Let's try to bridge as many gaps as we can. In some cases, we need to
1015 skip a gap and revisit it again after we closed later gaps. */
1016 while (!gaps
.empty ())
1018 for (const unsigned int number
: gaps
)
1020 struct btrace_function
*gap
, *lhs
, *rhs
;
1023 gap
= ftrace_find_call_by_number (btinfo
, number
);
1025 /* We may have a sequence of gaps if we run from one error into
1026 the next as we try to re-sync onto the trace stream. Ignore
1027 all but the leftmost gap in such a sequence.
1029 Also ignore gaps at the beginning of the trace. */
1030 lhs
= ftrace_find_call_by_number (btinfo
, gap
->number
- 1);
1031 if (lhs
== NULL
|| lhs
->errcode
!= 0)
1034 /* Skip gaps to the right. */
1035 rhs
= ftrace_find_call_by_number (btinfo
, gap
->number
+ 1);
1036 while (rhs
!= NULL
&& rhs
->errcode
!= 0)
1037 rhs
= ftrace_find_call_by_number (btinfo
, rhs
->number
+ 1);
1039 /* Ignore gaps at the end of the trace. */
1043 bridged
= ftrace_bridge_gap (btinfo
, lhs
, rhs
, min_matches
);
1045 /* Keep track of gaps we were not able to bridge and try again.
1046 If we just pushed them to the end of GAPS we would risk an
1047 infinite loop in case we simply cannot bridge a gap. */
1049 remaining
.push_back (number
);
1052 /* Let's see if we made any progress. */
1053 if (remaining
.size () == gaps
.size ())
1057 gaps
.swap (remaining
);
1060 /* We get here if either GAPS is empty or if GAPS equals REMAINING. */
1067 /* We may omit this in some cases. Not sure it is worth the extra
1068 complication, though. */
1069 ftrace_compute_global_level_offset (btinfo
);
1072 /* Compute the function branch trace from BTS trace. */
1075 btrace_compute_ftrace_bts (struct thread_info
*tp
,
1076 const struct btrace_data_bts
*btrace
,
1077 std::vector
<unsigned int> &gaps
)
1079 /* We may end up doing target calls that require the current thread to be TP,
1080 for example reading memory through gdb_insn_length. Make sure TP is the
1082 scoped_restore_current_thread restore_thread
;
1083 switch_to_thread (tp
);
1085 struct btrace_thread_info
*btinfo
;
1089 gdbarch
*gdbarch
= current_inferior ()->arch ();
1090 btinfo
= &tp
->btrace
;
1091 blk
= btrace
->blocks
->size ();
1093 if (btinfo
->functions
.empty ())
1096 level
= -btinfo
->level
;
1104 const btrace_block
&block
= btrace
->blocks
->at (blk
);
1109 struct btrace_function
*bfun
;
1110 struct btrace_insn insn
;
1113 /* We should hit the end of the block. Warn if we went too far. */
1116 /* Indicate the gap in the trace. */
1117 bfun
= ftrace_new_gap (btinfo
, BDE_BTS_OVERFLOW
, gaps
);
1119 warning (_("Recorded trace may be corrupted at instruction "
1120 "%u (pc = %s)."), bfun
->insn_offset
- 1,
1121 core_addr_to_string_nz (pc
));
1126 bfun
= ftrace_update_function (btinfo
,
1127 std::make_optional
<CORE_ADDR
> (pc
));
1129 /* Maintain the function level offset.
1130 For all but the last block, we do it here. */
1132 level
= std::min (level
, bfun
->level
);
1137 size
= gdb_insn_length (gdbarch
, pc
);
1139 catch (const gdb_exception_error
&error
)
1145 insn
.iclass
= ftrace_classify_insn (gdbarch
, pc
);
1148 ftrace_update_insns (bfun
, insn
);
1150 /* We're done once we pushed the instruction at the end. */
1151 if (block
.end
== pc
)
1154 /* We can't continue if we fail to compute the size. */
1157 /* Indicate the gap in the trace. We just added INSN so we're
1158 not at the beginning. */
1159 bfun
= ftrace_new_gap (btinfo
, BDE_BTS_INSN_SIZE
, gaps
);
1161 warning (_("Recorded trace may be incomplete at instruction %u "
1162 "(pc = %s)."), bfun
->insn_offset
- 1,
1163 core_addr_to_string_nz (pc
));
1170 /* Maintain the function level offset.
1171 For the last block, we do it here to not consider the last
1173 Since the last instruction corresponds to the current instruction
1174 and is not really part of the execution history, it shouldn't
1175 affect the level. */
1177 level
= std::min (level
, bfun
->level
);
1181 /* LEVEL is the minimal function level of all btrace function segments.
1182 Define the global level offset to -LEVEL so all function levels are
1183 normalized to start at zero. */
1184 btinfo
->level
= -level
;
1187 #if defined (HAVE_LIBIPT)
1189 static enum btrace_insn_class
1190 pt_reclassify_insn (enum pt_insn_class iclass
)
1195 return BTRACE_INSN_CALL
;
1198 return BTRACE_INSN_RETURN
;
1201 return BTRACE_INSN_JUMP
;
1204 return BTRACE_INSN_OTHER
;
1208 /* Return the btrace instruction flags for INSN. */
1210 static btrace_insn_flags
1211 pt_btrace_insn_flags (const struct pt_insn
&insn
)
1213 btrace_insn_flags flags
= 0;
1215 if (insn
.speculative
)
1216 flags
|= BTRACE_INSN_FLAG_SPECULATIVE
;
1221 /* Return the btrace instruction for INSN. */
1224 pt_btrace_insn (const struct pt_insn
&insn
)
1226 return {{static_cast<CORE_ADDR
> (insn
.ip
)},
1227 static_cast<gdb_byte
> (insn
.size
),
1228 pt_reclassify_insn (insn
.iclass
),
1229 pt_btrace_insn_flags (insn
)};
1232 #if defined (HAVE_PT_INSN_EVENT)
1233 /* Helper for events that will result in an aux_insn. */
1236 handle_pt_aux_insn (btrace_thread_info
*btinfo
, std::string
&aux_str
,
1237 std::optional
<CORE_ADDR
> pc
)
1239 btinfo
->aux_data
.emplace_back (std::move (aux_str
));
1240 struct btrace_function
*bfun
= ftrace_update_function (btinfo
, pc
);
1242 btrace_insn insn
{{btinfo
->aux_data
.size () - 1}, 0,
1243 BTRACE_INSN_AUX
, 0};
1245 ftrace_update_insns (bfun
, insn
);
1248 /* Check if the recording contains real instructions and not only auxiliary
1249 instructions since the last gap (or since the beginning). */
1252 ftrace_contains_non_aux_since_last_gap (const btrace_thread_info
*btinfo
)
1254 const std::vector
<btrace_function
> &functions
= btinfo
->functions
;
1256 std::vector
<btrace_function
>::const_reverse_iterator rit
;
1257 for (rit
= functions
.crbegin (); rit
!= functions
.crend (); ++rit
)
1259 if (rit
->errcode
!= 0)
1262 if ((rit
->flags
& BFUN_CONTAINS_NON_AUX
) != 0)
1268 #endif /* defined (HAVE_PT_INSN_EVENT) */
1270 #if (LIBIPT_VERSION >= 0x201)
1271 /* Translate an interrupt vector to a mnemonic string as defined for x86.
1272 Returns nullptr if there is none. */
1275 decode_interrupt_vector (const uint8_t vector
)
1277 static const char *mnemonic
[]
1278 = { "#de", "#db", nullptr, "#bp", "#of", "#br", "#ud", "#nm",
1279 "#df", "#mf", "#ts", "#np", "#ss", "#gp", "#pf", nullptr,
1280 "#mf", "#ac", "#mc", "#xm", "#ve", "#cp" };
1282 if (vector
< (sizeof (mnemonic
) / sizeof (mnemonic
[0])))
1283 return mnemonic
[vector
];
1287 #endif /* defined (LIBIPT_VERSION >= 0x201) */
1289 /* Handle instruction decode events (libipt-v2). */
1292 handle_pt_insn_events (struct btrace_thread_info
*btinfo
,
1293 struct pt_insn_decoder
*decoder
,
1294 std::vector
<unsigned int> &gaps
, int status
)
1296 #if defined (HAVE_PT_INSN_EVENT)
1297 while (status
& pts_event_pending
)
1299 struct pt_event event
;
1301 std::optional
<CORE_ADDR
> pc
;
1303 status
= pt_insn_event (decoder
, &event
, sizeof (event
));
1314 if (event
.status_update
!= 0)
1317 /* Only create a new gap if there are non-aux instructions in
1318 the trace since the last gap. We could be at the beginning
1319 of the recording and could already have handled one or more
1320 events, like ptev_iret, that created aux insns. In that
1321 case we don't want to create a gap or print a warning. */
1322 if (event
.variant
.enabled
.resumed
== 0
1323 && ftrace_contains_non_aux_since_last_gap (btinfo
))
1325 struct btrace_function
*bfun
1326 = ftrace_new_gap (btinfo
, BDE_PT_NON_CONTIGUOUS
, gaps
);
1328 pt_insn_get_offset (decoder
, &offset
);
1331 (_("Non-contiguous trace at instruction %u (offset = 0x%"
1332 PRIx64
")."), bfun
->insn_offset
- 1, offset
);
1340 struct btrace_function
*bfun
1341 = ftrace_new_gap (btinfo
, BDE_PT_OVERFLOW
, gaps
);
1343 pt_insn_get_offset (decoder
, &offset
);
1345 warning (_("Overflow at instruction %u (offset = 0x%" PRIx64
")."),
1346 bfun
->insn_offset
- 1, offset
);
1350 #if defined (HAVE_STRUCT_PT_EVENT_VARIANT_PTWRITE)
1353 std::optional
<std::string
> ptw_string
;
1355 /* Lookup the PC if available. The event often doesn't provide
1356 one, so we look into the last function segment as well.
1357 Looking further back makes limited sense for ptwrite. */
1358 if (event
.ip_suppressed
== 0)
1359 pc
= event
.variant
.ptwrite
.ip
;
1360 else if (!btinfo
->functions
.empty ())
1362 std::vector
<btrace_insn
> &insns
1363 = btinfo
->functions
.back ().insn
;
1364 for (auto insn
= insns
.rbegin (); insn
!= insns
.rend ();
1367 switch (insn
->iclass
)
1369 case BTRACE_INSN_AUX
:
1372 case BTRACE_INSN_OTHER
:
1373 case BTRACE_INSN_CALL
:
1374 case BTRACE_INSN_RETURN
:
1375 case BTRACE_INSN_JUMP
:
1378 /* No default to rely on compiler warnings. */
1384 if (!pc
.has_value ())
1385 warning (_("Failed to determine the PC for ptwrite."));
1388 if (btinfo
->ptw_callback_fun
!= nullptr)
1390 = btinfo
->ptw_callback_fun (event
.variant
.ptwrite
.payload
,
1391 pc
, btinfo
->ptw_context
);
1393 if (ptw_string
.has_value () && (*ptw_string
).empty ())
1396 if (!ptw_string
.has_value ())
1397 *ptw_string
= hex_string (event
.variant
.ptwrite
.payload
);
1399 handle_pt_aux_insn (btinfo
, *ptw_string
, pc
);
1403 #endif /* defined (HAVE_STRUCT_PT_EVENT_VARIANT_PTWRITE) */
1405 #if (LIBIPT_VERSION >= 0x201)
1406 case ptev_interrupt
:
1408 std::string aux_string
= std::string (_("interrupt: vector = "))
1409 + hex_string (event
.variant
.interrupt
.vector
);
1412 = decode_interrupt_vector (event
.variant
.interrupt
.vector
);
1413 if (decoded
!= nullptr)
1414 aux_string
+= std::string (" (") + decoded
+ ")";
1416 if (event
.variant
.interrupt
.has_cr2
!= 0)
1418 aux_string
+= std::string (", cr2 = ")
1419 + hex_string (event
.variant
.interrupt
.cr2
);
1422 if (event
.ip_suppressed
== 0)
1424 pc
= event
.variant
.interrupt
.ip
;
1425 aux_string
+= std::string (", ip = ") + hex_string (*pc
);
1428 handle_pt_aux_insn (btinfo
, aux_string
, pc
);
1434 std::string aux_string
= std::string (_("iret"));
1436 if (event
.ip_suppressed
== 0)
1438 pc
= event
.variant
.iret
.ip
;
1439 aux_string
+= std::string (": ip = ") + hex_string (*pc
);
1442 handle_pt_aux_insn (btinfo
, aux_string
, pc
);
1448 std::string aux_string
= std::string (_("smi"));
1450 if (event
.ip_suppressed
== 0)
1452 pc
= event
.variant
.smi
.ip
;
1453 aux_string
+= std::string (": ip = ") + hex_string (*pc
);
1456 handle_pt_aux_insn (btinfo
, aux_string
, pc
);
1462 std::string aux_string
= std::string (_("rsm"));
1464 if (event
.ip_suppressed
== 0)
1466 pc
= event
.variant
.rsm
.ip
;
1467 aux_string
+= std::string (": ip = ") + hex_string (*pc
);
1470 handle_pt_aux_insn (btinfo
, aux_string
, pc
);
1476 std::string aux_string
= std::string (_("sipi: vector = "))
1477 + hex_string (event
.variant
.sipi
.vector
);
1479 handle_pt_aux_insn (btinfo
, aux_string
, pc
);
1485 std::string aux_string
= std::string (_("init"));
1487 if (event
.ip_suppressed
== 0)
1489 pc
= event
.variant
.init
.ip
;
1490 aux_string
+= std::string (": ip = ") + hex_string (*pc
);
1493 handle_pt_aux_insn (btinfo
, aux_string
, pc
);
1499 std::string aux_string
= std::string (_("vmentry"));
1501 if (event
.ip_suppressed
== 0)
1503 pc
= event
.variant
.vmentry
.ip
;
1504 aux_string
+= std::string (": ip = ") + hex_string (*pc
);
1507 handle_pt_aux_insn (btinfo
, aux_string
, pc
);
1513 std::string aux_string
= std::string (_("vmexit"));
1515 if (event
.variant
.vmexit
.has_vector
!= 0
1516 || event
.variant
.vmexit
.has_vmxr
!= 0
1517 || event
.variant
.vmexit
.has_vmxq
!= 0
1518 || event
.ip_suppressed
!= 0)
1519 aux_string
+= std::string (":");
1521 if (event
.variant
.vmexit
.has_vector
!= 0)
1523 aux_string
+= std::string (_(" vector = "))
1524 + hex_string (event
.variant
.vmexit
.vector
);
1526 const char* decoded
= decode_interrupt_vector
1527 (event
.variant
.vmexit
.vector
);
1528 if (decoded
!= nullptr)
1529 aux_string
+= std::string (" (") + decoded
+ ")";
1532 if (event
.variant
.vmexit
.has_vmxr
!= 0)
1534 std::string separator
= aux_string
.back () == ':' ? "" : ",";
1535 aux_string
+= separator
+ std::string (" vmxr = ")
1536 + hex_string (event
.variant
.vmexit
.vmxr
);
1539 if (event
.variant
.vmexit
.has_vmxq
!= 0)
1541 std::string separator
= aux_string
.back () == ':' ? "" : ",";
1542 aux_string
+= separator
+ std::string (" vmxq = ")
1543 + hex_string (event
.variant
.vmexit
.vmxq
);
1546 if (event
.ip_suppressed
== 0)
1548 pc
= event
.variant
.vmexit
.ip
;
1549 std::string separator
= aux_string
.back () == ':' ? "" : ",";
1550 aux_string
+= separator
+ std::string (" ip = ")
1554 handle_pt_aux_insn (btinfo
, aux_string
, pc
);
1560 std::string aux_string
= std::string (_("shutdown"));
1562 if (event
.ip_suppressed
== 0)
1564 pc
= event
.variant
.shutdown
.ip
;
1565 aux_string
+= std::string (": ip = ") + hex_string (*pc
);
1568 handle_pt_aux_insn (btinfo
, aux_string
, pc
);
1574 std::string aux_string
= std::string (_("uintr: vector = "))
1575 + hex_string (event
.variant
.uintr
.vector
);
1577 if (event
.ip_suppressed
== 0)
1579 pc
= event
.variant
.uintr
.ip
;
1580 aux_string
+= std::string (", ip = ") + hex_string (*pc
);
1583 handle_pt_aux_insn (btinfo
, aux_string
, pc
);
1589 std::string aux_string
= std::string (_("uiret"));
1591 if (event
.ip_suppressed
== 0)
1593 pc
= event
.variant
.uiret
.ip
;
1594 aux_string
+= std::string (": ip = ") + hex_string (*pc
);
1597 handle_pt_aux_insn (btinfo
, aux_string
, pc
);
1600 #endif /* defined (LIBIPT_VERSION >= 0x201) */
1603 #endif /* defined (HAVE_PT_INSN_EVENT) */
1608 /* Handle events indicated by flags in INSN (libipt-v1). */
1611 handle_pt_insn_event_flags (struct btrace_thread_info
*btinfo
,
1612 struct pt_insn_decoder
*decoder
,
1613 const struct pt_insn
&insn
,
1614 std::vector
<unsigned int> &gaps
)
1616 #if defined (HAVE_STRUCT_PT_INSN_ENABLED)
1617 /* Tracing is disabled and re-enabled each time we enter the kernel. Most
1618 times, we continue from the same instruction we stopped before. This is
1619 indicated via the RESUMED instruction flag. The ENABLED instruction flag
1620 means that we continued from some other instruction. Indicate this as a
1621 trace gap except when tracing just started. */
1622 if (insn
.enabled
&& !btinfo
->functions
.empty ())
1624 struct btrace_function
*bfun
;
1627 bfun
= ftrace_new_gap (btinfo
, BDE_PT_NON_CONTIGUOUS
, gaps
);
1629 pt_insn_get_offset (decoder
, &offset
);
1631 warning (_("Non-contiguous trace at instruction %u (offset = 0x%" PRIx64
1632 ", pc = 0x%" PRIx64
")."), bfun
->insn_offset
- 1, offset
,
1635 #endif /* defined (HAVE_STRUCT_PT_INSN_ENABLED) */
1637 #if defined (HAVE_STRUCT_PT_INSN_RESYNCED)
1638 /* Indicate trace overflows. */
1641 struct btrace_function
*bfun
;
1644 bfun
= ftrace_new_gap (btinfo
, BDE_PT_OVERFLOW
, gaps
);
1646 pt_insn_get_offset (decoder
, &offset
);
1648 warning (_("Overflow at instruction %u (offset = 0x%" PRIx64
", pc = 0x%"
1649 PRIx64
")."), bfun
->insn_offset
- 1, offset
, insn
.ip
);
1651 #endif /* defined (HAVE_STRUCT_PT_INSN_RESYNCED) */
1654 /* Add function branch trace to BTINFO using DECODER. */
1657 ftrace_add_pt (struct btrace_thread_info
*btinfo
,
1658 struct pt_insn_decoder
*decoder
,
1660 std::vector
<unsigned int> &gaps
)
1662 struct btrace_function
*bfun
;
1666 /* Register the ptwrite filter. */
1667 apply_ext_lang_ptwrite_filter (btinfo
);
1671 struct pt_insn insn
;
1673 status
= pt_insn_sync_forward (decoder
);
1676 if (status
!= -pte_eos
)
1677 warning (_("Failed to synchronize onto the Intel Processor "
1678 "Trace stream: %s."), pt_errstr (pt_errcode (status
)));
1684 /* Handle events from the previous iteration or synchronization. */
1685 status
= handle_pt_insn_events (btinfo
, decoder
, gaps
, status
);
1689 status
= pt_insn_next (decoder
, &insn
, sizeof(insn
));
1693 /* Handle events indicated by flags in INSN. */
1694 handle_pt_insn_event_flags (btinfo
, decoder
, insn
, gaps
);
1697 = ftrace_update_function (btinfo
,
1698 std::make_optional
<CORE_ADDR
> (insn
.ip
));
1700 /* Maintain the function level offset. */
1701 *plevel
= std::min (*plevel
, bfun
->level
);
1703 ftrace_update_insns (bfun
, pt_btrace_insn (insn
));
1706 if (status
== -pte_eos
)
1709 /* Indicate the gap in the trace. */
1710 bfun
= ftrace_new_gap (btinfo
, status
, gaps
);
1712 pt_insn_get_offset (decoder
, &offset
);
1714 warning (_("Decode error (%d) at instruction %u (offset = 0x%" PRIx64
1715 ", pc = 0x%" PRIx64
"): %s."), status
, bfun
->insn_offset
- 1,
1716 offset
, insn
.ip
, pt_errstr (pt_errcode (status
)));
1720 /* A callback function to allow the trace decoder to read the inferior's
1724 btrace_pt_readmem_callback (gdb_byte
*buffer
, size_t size
,
1725 const struct pt_asid
*asid
, uint64_t pc
,
1728 int result
, errcode
;
1730 result
= (int) size
;
1733 errcode
= target_read_code ((CORE_ADDR
) pc
, buffer
, size
);
1735 result
= -pte_nomap
;
1737 catch (const gdb_exception_error
&error
)
1739 result
= -pte_nomap
;
1745 /* Translate the vendor from one enum to another. */
1747 static enum pt_cpu_vendor
1748 pt_translate_cpu_vendor (enum btrace_cpu_vendor vendor
)
1760 /* Finalize the function branch trace after decode. */
1762 static void btrace_finalize_ftrace_pt (struct pt_insn_decoder
*decoder
,
1763 struct thread_info
*tp
, int level
)
1765 pt_insn_free_decoder (decoder
);
1767 /* LEVEL is the minimal function level of all btrace function segments.
1768 Define the global level offset to -LEVEL so all function levels are
1769 normalized to start at zero. */
1770 tp
->btrace
.level
= -level
;
1772 /* Add a single last instruction entry for the current PC.
1773 This allows us to compute the backtrace at the current PC using both
1774 standard unwind and btrace unwind.
1775 This extra entry is ignored by all record commands. */
1779 /* Compute the function branch trace from Intel Processor Trace
1783 btrace_compute_ftrace_pt (struct thread_info
*tp
,
1784 const struct btrace_data_pt
*btrace
,
1785 std::vector
<unsigned int> &gaps
)
1787 /* We may end up doing target calls that require the current thread to be TP,
1788 for example reading memory through btrace_pt_readmem_callback. Make sure
1789 TP is the current thread. */
1790 scoped_restore_current_thread restore_thread
;
1791 switch_to_thread (tp
);
1793 struct btrace_thread_info
*btinfo
;
1794 struct pt_insn_decoder
*decoder
;
1795 struct pt_config config
;
1798 if (btrace
->size
== 0)
1801 btinfo
= &tp
->btrace
;
1802 if (btinfo
->functions
.empty ())
1805 level
= -btinfo
->level
;
1807 pt_config_init(&config
);
1808 config
.begin
= btrace
->data
;
1809 config
.end
= btrace
->data
+ btrace
->size
;
1811 /* We treat an unknown vendor as 'no errata'. */
1812 if (btrace
->config
.cpu
.vendor
!= CV_UNKNOWN
)
1815 = pt_translate_cpu_vendor (btrace
->config
.cpu
.vendor
);
1816 config
.cpu
.family
= btrace
->config
.cpu
.family
;
1817 config
.cpu
.model
= btrace
->config
.cpu
.model
;
1818 config
.cpu
.stepping
= btrace
->config
.cpu
.stepping
;
1820 errcode
= pt_cpu_errata (&config
.errata
, &config
.cpu
);
1822 error (_("Failed to configure the Intel Processor Trace "
1823 "decoder: %s."), pt_errstr (pt_errcode (errcode
)));
1826 decoder
= pt_insn_alloc_decoder (&config
);
1827 if (decoder
== NULL
)
1828 error (_("Failed to allocate the Intel Processor Trace decoder."));
1832 struct pt_image
*image
;
1834 image
= pt_insn_get_image(decoder
);
1836 error (_("Failed to configure the Intel Processor Trace decoder."));
1838 errcode
= pt_image_set_callback(image
, btrace_pt_readmem_callback
, NULL
);
1840 error (_("Failed to configure the Intel Processor Trace decoder: "
1841 "%s."), pt_errstr (pt_errcode (errcode
)));
1843 ftrace_add_pt (btinfo
, decoder
, &level
, gaps
);
1845 catch (const gdb_exception
&error
)
1847 /* Indicate a gap in the trace if we quit trace processing. */
1848 if (error
.reason
== RETURN_QUIT
&& !btinfo
->functions
.empty ())
1849 ftrace_new_gap (btinfo
, BDE_PT_USER_QUIT
, gaps
);
1851 btrace_finalize_ftrace_pt (decoder
, tp
, level
);
1856 btrace_finalize_ftrace_pt (decoder
, tp
, level
);
1859 #else /* defined (HAVE_LIBIPT) */
1862 btrace_compute_ftrace_pt (struct thread_info
*tp
,
1863 const struct btrace_data_pt
*btrace
,
1864 std::vector
<unsigned int> &gaps
)
1866 internal_error (_("Unexpected branch trace format."));
1869 #endif /* defined (HAVE_LIBIPT) */
1871 /* Compute the function branch trace from a block branch trace BTRACE for
1872 a thread given by BTINFO. If CPU is not NULL, overwrite the cpu in the
1873 branch trace configuration. This is currently only used for the PT
1877 btrace_compute_ftrace_1 (struct thread_info
*tp
,
1878 struct btrace_data
*btrace
,
1879 const struct btrace_cpu
*cpu
,
1880 std::vector
<unsigned int> &gaps
)
1882 DEBUG ("compute ftrace");
1884 switch (btrace
->format
)
1886 case BTRACE_FORMAT_NONE
:
1889 case BTRACE_FORMAT_BTS
:
1890 btrace_compute_ftrace_bts (tp
, &btrace
->variant
.bts
, gaps
);
1893 case BTRACE_FORMAT_PT
:
1894 /* Overwrite the cpu we use for enabling errata workarounds. */
1896 btrace
->variant
.pt
.config
.cpu
= *cpu
;
1898 btrace_compute_ftrace_pt (tp
, &btrace
->variant
.pt
, gaps
);
1902 internal_error (_("Unknown branch trace format."));
1906 btrace_finalize_ftrace (struct thread_info
*tp
, std::vector
<unsigned int> &gaps
)
1910 tp
->btrace
.ngaps
+= gaps
.size ();
1911 btrace_bridge_gaps (tp
, gaps
);
1916 btrace_compute_ftrace (struct thread_info
*tp
, struct btrace_data
*btrace
,
1917 const struct btrace_cpu
*cpu
)
1919 std::vector
<unsigned int> gaps
;
1923 btrace_compute_ftrace_1 (tp
, btrace
, cpu
, gaps
);
1925 catch (const gdb_exception
&error
)
1927 btrace_finalize_ftrace (tp
, gaps
);
1932 btrace_finalize_ftrace (tp
, gaps
);
1935 /* Add an entry for the current PC. */
1938 btrace_add_pc (struct thread_info
*tp
)
1940 struct btrace_data btrace
;
1941 struct regcache
*regcache
;
1944 regcache
= get_thread_regcache (tp
);
1945 pc
= regcache_read_pc (regcache
);
1947 btrace
.format
= BTRACE_FORMAT_BTS
;
1948 btrace
.variant
.bts
.blocks
= new std::vector
<btrace_block
>;
1950 btrace
.variant
.bts
.blocks
->emplace_back (pc
, pc
);
1952 btrace_compute_ftrace (tp
, &btrace
, NULL
);
1958 btrace_enable (struct thread_info
*tp
, const struct btrace_config
*conf
)
1960 if (tp
->btrace
.target
!= NULL
)
1961 error (_("Recording already enabled on thread %s (%s)."),
1962 print_thread_id (tp
), target_pid_to_str (tp
->ptid
).c_str ());
1964 #if !defined (HAVE_LIBIPT)
1965 if (conf
->format
== BTRACE_FORMAT_PT
)
1966 error (_("Intel Processor Trace support was disabled at compile time."));
1967 #endif /* !defined (HAVE_LIBIPT) */
1969 DEBUG ("enable thread %s (%s)", print_thread_id (tp
),
1970 tp
->ptid
.to_string ().c_str ());
1972 tp
->btrace
.target
= target_enable_btrace (tp
, conf
);
1974 if (tp
->btrace
.target
== NULL
)
1975 error (_("Failed to enable recording on thread %s (%s)."),
1976 print_thread_id (tp
), target_pid_to_str (tp
->ptid
).c_str ());
1978 /* We need to undo the enable in case of errors. */
1981 /* Add an entry for the current PC so we start tracing from where we
1984 If we can't access TP's registers, TP is most likely running. In this
1985 case, we can't really say where tracing was enabled so it should be
1986 safe to simply skip this step.
1988 This is not relevant for BTRACE_FORMAT_PT since the trace will already
1989 start at the PC at which tracing was enabled. */
1990 if (conf
->format
!= BTRACE_FORMAT_PT
1991 && can_access_registers_thread (tp
))
1994 catch (const gdb_exception
&exception
)
1996 btrace_disable (tp
);
2004 const struct btrace_config
*
2005 btrace_conf (const struct btrace_thread_info
*btinfo
)
2007 if (btinfo
->target
== NULL
)
2010 return target_btrace_conf (btinfo
->target
);
2016 btrace_disable (struct thread_info
*tp
)
2018 struct btrace_thread_info
*btp
= &tp
->btrace
;
2020 if (btp
->target
== NULL
)
2021 error (_("Recording not enabled on thread %s (%s)."),
2022 print_thread_id (tp
), target_pid_to_str (tp
->ptid
).c_str ());
2024 DEBUG ("disable thread %s (%s)", print_thread_id (tp
),
2025 tp
->ptid
.to_string ().c_str ());
2027 target_disable_btrace (btp
->target
);
2036 btrace_teardown (struct thread_info
*tp
)
2038 struct btrace_thread_info
*btp
= &tp
->btrace
;
2040 if (btp
->target
== NULL
)
2043 DEBUG ("teardown thread %s (%s)", print_thread_id (tp
),
2044 tp
->ptid
.to_string ().c_str ());
2046 target_teardown_btrace (btp
->target
);
2052 /* Stitch branch trace in BTS format. */
2055 btrace_stitch_bts (struct btrace_data_bts
*btrace
, struct thread_info
*tp
)
2057 struct btrace_thread_info
*btinfo
;
2058 struct btrace_function
*last_bfun
;
2059 btrace_block
*first_new_block
;
2061 btinfo
= &tp
->btrace
;
2062 gdb_assert (!btinfo
->functions
.empty ());
2063 gdb_assert (!btrace
->blocks
->empty ());
2065 last_bfun
= &btinfo
->functions
.back ();
2067 /* If the existing trace ends with a gap, we just glue the traces
2068 together. We need to drop the last (i.e. chronologically first) block
2069 of the new trace, though, since we can't fill in the start address.*/
2070 if (last_bfun
->insn
.empty ())
2072 btrace
->blocks
->pop_back ();
2076 /* Beware that block trace starts with the most recent block, so the
2077 chronologically first block in the new trace is the last block in
2078 the new trace's block vector. */
2079 first_new_block
= &btrace
->blocks
->back ();
2080 const btrace_insn
&last_insn
= last_bfun
->insn
.back ();
2082 /* If the current PC at the end of the block is the same as in our current
2083 trace, there are two explanations:
2084 1. we executed the instruction and some branch brought us back.
2085 2. we have not made any progress.
2086 In the first case, the delta trace vector should contain at least two
2088 In the second case, the delta trace vector should contain exactly one
2089 entry for the partial block containing the current PC. Remove it. */
2090 if (first_new_block
->end
== last_insn
.pc
&& btrace
->blocks
->size () == 1)
2092 btrace
->blocks
->pop_back ();
2096 DEBUG ("stitching %s to %s", ftrace_print_insn_addr (&last_insn
),
2097 core_addr_to_string_nz (first_new_block
->end
));
2099 /* Do a simple sanity check to make sure we don't accidentally end up
2100 with a bad block. This should not occur in practice. */
2101 if (first_new_block
->end
< last_insn
.pc
)
2103 warning (_("Error while trying to read delta trace. Falling back to "
2108 /* We adjust the last block to start at the end of our current trace. */
2109 gdb_assert (first_new_block
->begin
== 0);
2110 first_new_block
->begin
= last_insn
.pc
;
2112 /* We simply pop the last insn so we can insert it again as part of
2113 the normal branch trace computation.
2114 Since instruction iterators are based on indices in the instructions
2115 vector, we don't leave any pointers dangling. */
2116 DEBUG ("pruning insn at %s for stitching",
2117 ftrace_print_insn_addr (&last_insn
));
2119 last_bfun
->insn
.pop_back ();
2121 /* The instructions vector may become empty temporarily if this has
2122 been the only instruction in this function segment.
2123 This violates the invariant but will be remedied shortly by
2124 btrace_compute_ftrace when we add the new trace. */
2126 /* The only case where this would hurt is if the entire trace consisted
2127 of just that one instruction. If we remove it, we might turn the now
2128 empty btrace function segment into a gap. But we don't want gaps at
2129 the beginning. To avoid this, we remove the entire old trace. */
2130 if (last_bfun
->number
== 1 && last_bfun
->insn
.empty ())
2136 /* Adjust the block trace in order to stitch old and new trace together.
2137 BTRACE is the new delta trace between the last and the current stop.
2138 TP is the traced thread.
2139 May modify BTRACE as well as the existing trace in TP.
2140 Return 0 on success, -1 otherwise. */
2143 btrace_stitch_trace (struct btrace_data
*btrace
, struct thread_info
*tp
)
2145 /* If we don't have trace, there's nothing to do. */
2146 if (btrace
->empty ())
2149 switch (btrace
->format
)
2151 case BTRACE_FORMAT_NONE
:
2154 case BTRACE_FORMAT_BTS
:
2155 return btrace_stitch_bts (&btrace
->variant
.bts
, tp
);
2157 case BTRACE_FORMAT_PT
:
2158 /* Delta reads are not supported. */
2162 internal_error (_("Unknown branch trace format."));
2165 /* Clear the branch trace histories in BTINFO. */
2168 btrace_clear_history (struct btrace_thread_info
*btinfo
)
2170 xfree (btinfo
->insn_history
);
2171 xfree (btinfo
->call_history
);
2172 xfree (btinfo
->replay
);
2174 btinfo
->insn_history
= NULL
;
2175 btinfo
->call_history
= NULL
;
2176 btinfo
->replay
= NULL
;
2178 btinfo
->aux_data
.clear ();
2181 /* Clear the branch trace maintenance histories in BTINFO. */
2184 btrace_maint_clear (struct btrace_thread_info
*btinfo
)
2186 switch (btinfo
->data
.format
)
2191 case BTRACE_FORMAT_BTS
:
2192 btinfo
->maint
.variant
.bts
.packet_history
.begin
= 0;
2193 btinfo
->maint
.variant
.bts
.packet_history
.end
= 0;
2196 #if defined (HAVE_LIBIPT)
2197 case BTRACE_FORMAT_PT
:
2198 delete btinfo
->maint
.variant
.pt
.packets
;
2200 btinfo
->maint
.variant
.pt
.packets
= NULL
;
2201 btinfo
->maint
.variant
.pt
.packet_history
.begin
= 0;
2202 btinfo
->maint
.variant
.pt
.packet_history
.end
= 0;
2204 #endif /* defined (HAVE_LIBIPT) */
2211 btrace_decode_error (enum btrace_format format
, int errcode
)
2215 case BTRACE_FORMAT_BTS
:
2218 case BDE_BTS_OVERFLOW
:
2219 return _("instruction overflow");
2221 case BDE_BTS_INSN_SIZE
:
2222 return _("unknown instruction");
2229 #if defined (HAVE_LIBIPT)
2230 case BTRACE_FORMAT_PT
:
2233 case BDE_PT_USER_QUIT
:
2234 return _("trace decode cancelled");
2236 case BDE_PT_NON_CONTIGUOUS
:
2237 return _("non-contiguous");
2239 case BDE_PT_OVERFLOW
:
2240 return _("overflow");
2244 return pt_errstr (pt_errcode (errcode
));
2248 #endif /* defined (HAVE_LIBIPT) */
2254 return _("unknown");
2260 btrace_fetch (struct thread_info
*tp
, const struct btrace_cpu
*cpu
)
2262 struct btrace_thread_info
*btinfo
;
2263 struct btrace_target_info
*tinfo
;
2264 struct btrace_data btrace
;
2267 DEBUG ("fetch thread %s (%s)", print_thread_id (tp
),
2268 tp
->ptid
.to_string ().c_str ());
2270 btinfo
= &tp
->btrace
;
2271 tinfo
= btinfo
->target
;
2275 /* There's no way we could get new trace while replaying.
2276 On the other hand, delta trace would return a partial record with the
2277 current PC, which is the replay PC, not the last PC, as expected. */
2278 if (btinfo
->replay
!= NULL
)
2281 /* With CLI usage, TP is always the current thread when we get here.
2282 However, since we can also store a gdb.Record object in Python
2283 referring to a different thread than the current one, we need to
2284 temporarily set the current thread. */
2285 scoped_restore_current_thread restore_thread
;
2286 switch_to_thread (tp
);
2288 /* We should not be called on running or exited threads. */
2289 gdb_assert (can_access_registers_thread (tp
));
2291 /* Let's first try to extend the trace we already have. */
2292 if (!btinfo
->functions
.empty ())
2294 errcode
= target_read_btrace (&btrace
, tinfo
, BTRACE_READ_DELTA
);
2297 /* Success. Let's try to stitch the traces together. */
2298 errcode
= btrace_stitch_trace (&btrace
, tp
);
2302 /* We failed to read delta trace. Let's try to read new trace. */
2303 errcode
= target_read_btrace (&btrace
, tinfo
, BTRACE_READ_NEW
);
2305 /* If we got any new trace, discard what we have. */
2306 if (errcode
== 0 && !btrace
.empty ())
2310 /* If we were not able to read the trace, we start over. */
2314 errcode
= target_read_btrace (&btrace
, tinfo
, BTRACE_READ_ALL
);
2318 errcode
= target_read_btrace (&btrace
, tinfo
, BTRACE_READ_ALL
);
2320 /* If we were not able to read the branch trace, signal an error. */
2322 error (_("Failed to read branch trace."));
2324 /* Compute the trace, provided we have any. */
2325 if (!btrace
.empty ())
2327 /* Store the raw trace data. The stored data will be cleared in
2328 btrace_clear, so we always append the new trace. */
2329 btrace_data_append (&btinfo
->data
, &btrace
);
2330 btrace_maint_clear (btinfo
);
2332 btrace_clear_history (btinfo
);
2333 btrace_compute_ftrace (tp
, &btrace
, cpu
);
2340 btrace_clear (struct thread_info
*tp
)
2342 struct btrace_thread_info
*btinfo
;
2344 DEBUG ("clear thread %s (%s)", print_thread_id (tp
),
2345 tp
->ptid
.to_string ().c_str ());
2347 /* Make sure btrace frames that may hold a pointer into the branch
2348 trace data are destroyed. */
2349 reinit_frame_cache ();
2351 btinfo
= &tp
->btrace
;
2353 btinfo
->functions
.clear ();
2356 /* Must clear the maint data before - it depends on BTINFO->DATA. */
2357 btrace_maint_clear (btinfo
);
2358 btinfo
->data
.clear ();
2359 btrace_clear_history (btinfo
);
2365 btrace_free_objfile (struct objfile
*objfile
)
2367 DEBUG ("free objfile");
2369 for (thread_info
*tp
: all_non_exited_threads ())
2375 const struct btrace_insn
*
2376 btrace_insn_get (const struct btrace_insn_iterator
*it
)
2378 const struct btrace_function
*bfun
;
2379 unsigned int index
, end
;
2381 index
= it
->insn_index
;
2382 bfun
= &it
->btinfo
->functions
[it
->call_index
];
2384 /* Check if the iterator points to a gap in the trace. */
2385 if (bfun
->errcode
!= 0)
2388 /* The index is within the bounds of this function's instruction vector. */
2389 end
= bfun
->insn
.size ();
2390 gdb_assert (0 < end
);
2391 gdb_assert (index
< end
);
2393 return &bfun
->insn
[index
];
2399 btrace_insn_get_error (const struct btrace_insn_iterator
*it
)
2401 return it
->btinfo
->functions
[it
->call_index
].errcode
;
2407 btrace_insn_number (const struct btrace_insn_iterator
*it
)
2409 return it
->btinfo
->functions
[it
->call_index
].insn_offset
+ it
->insn_index
;
2415 btrace_insn_begin (struct btrace_insn_iterator
*it
,
2416 const struct btrace_thread_info
*btinfo
)
2418 if (btinfo
->functions
.empty ())
2419 error (_("No trace."));
2421 it
->btinfo
= btinfo
;
2429 btrace_insn_end (struct btrace_insn_iterator
*it
,
2430 const struct btrace_thread_info
*btinfo
)
2432 const struct btrace_function
*bfun
;
2433 unsigned int length
;
2435 if (btinfo
->functions
.empty ())
2436 error (_("No trace."));
2438 bfun
= &btinfo
->functions
.back ();
2439 length
= bfun
->insn
.size ();
2441 /* The last function may either be a gap or it contains the current
2442 instruction, which is one past the end of the execution trace; ignore
2447 it
->btinfo
= btinfo
;
2448 it
->call_index
= bfun
->number
- 1;
2449 it
->insn_index
= length
;
2455 btrace_insn_next (struct btrace_insn_iterator
*it
, unsigned int stride
)
2457 const struct btrace_function
*bfun
;
2458 unsigned int index
, steps
;
2460 bfun
= &it
->btinfo
->functions
[it
->call_index
];
2462 index
= it
->insn_index
;
2466 unsigned int end
, space
, adv
;
2468 end
= bfun
->insn
.size ();
2470 /* An empty function segment represents a gap in the trace. We count
2471 it as one instruction. */
2474 const struct btrace_function
*next
;
2476 next
= ftrace_find_call_by_number (it
->btinfo
, bfun
->number
+ 1);
2489 gdb_assert (0 < end
);
2490 gdb_assert (index
< end
);
2492 /* Compute the number of instructions remaining in this segment. */
2493 space
= end
- index
;
2495 /* Advance the iterator as far as possible within this segment. */
2496 adv
= std::min (space
, stride
);
2501 /* Move to the next function if we're at the end of this one. */
2504 const struct btrace_function
*next
;
2506 next
= ftrace_find_call_by_number (it
->btinfo
, bfun
->number
+ 1);
2509 /* We stepped past the last function.
2511 Let's adjust the index to point to the last instruction in
2512 the previous function. */
2518 /* We now point to the first instruction in the new function. */
2523 /* We did make progress. */
2524 gdb_assert (adv
> 0);
2527 /* Update the iterator. */
2528 it
->call_index
= bfun
->number
- 1;
2529 it
->insn_index
= index
;
2537 btrace_insn_prev (struct btrace_insn_iterator
*it
, unsigned int stride
)
2539 const struct btrace_function
*bfun
;
2540 unsigned int index
, steps
;
2542 bfun
= &it
->btinfo
->functions
[it
->call_index
];
2544 index
= it
->insn_index
;
2550 /* Move to the previous function if we're at the start of this one. */
2553 const struct btrace_function
*prev
;
2555 prev
= ftrace_find_call_by_number (it
->btinfo
, bfun
->number
- 1);
2559 /* We point to one after the last instruction in the new function. */
2561 index
= bfun
->insn
.size ();
2563 /* An empty function segment represents a gap in the trace. We count
2564 it as one instruction. */
2574 /* Advance the iterator as far as possible within this segment. */
2575 adv
= std::min (index
, stride
);
2581 /* We did make progress. */
2582 gdb_assert (adv
> 0);
2585 /* Update the iterator. */
2586 it
->call_index
= bfun
->number
- 1;
2587 it
->insn_index
= index
;
2595 btrace_insn_cmp (const struct btrace_insn_iterator
*lhs
,
2596 const struct btrace_insn_iterator
*rhs
)
2598 gdb_assert (lhs
->btinfo
== rhs
->btinfo
);
2600 if (lhs
->call_index
!= rhs
->call_index
)
2601 return lhs
->call_index
- rhs
->call_index
;
2603 return lhs
->insn_index
- rhs
->insn_index
;
2609 btrace_find_insn_by_number (struct btrace_insn_iterator
*it
,
2610 const struct btrace_thread_info
*btinfo
,
2611 unsigned int number
)
2613 const struct btrace_function
*bfun
;
2614 unsigned int upper
, lower
;
2616 if (btinfo
->functions
.empty ())
2620 bfun
= &btinfo
->functions
[lower
];
2621 if (number
< bfun
->insn_offset
)
2624 upper
= btinfo
->functions
.size () - 1;
2625 bfun
= &btinfo
->functions
[upper
];
2626 if (number
>= bfun
->insn_offset
+ ftrace_call_num_insn (bfun
))
2629 /* We assume that there are no holes in the numbering. */
2632 const unsigned int average
= lower
+ (upper
- lower
) / 2;
2634 bfun
= &btinfo
->functions
[average
];
2636 if (number
< bfun
->insn_offset
)
2638 upper
= average
- 1;
2642 if (number
>= bfun
->insn_offset
+ ftrace_call_num_insn (bfun
))
2644 lower
= average
+ 1;
2651 it
->btinfo
= btinfo
;
2652 it
->call_index
= bfun
->number
- 1;
2653 it
->insn_index
= number
- bfun
->insn_offset
;
2657 /* Returns true if the recording ends with a function segment that
2658 contains only a single (i.e. the current) instruction. */
2661 btrace_ends_with_single_insn (const struct btrace_thread_info
*btinfo
)
2663 const btrace_function
*bfun
;
2665 if (btinfo
->functions
.empty ())
2668 bfun
= &btinfo
->functions
.back ();
2669 if (bfun
->errcode
!= 0)
2672 return ftrace_call_num_insn (bfun
) == 1;
2677 const struct btrace_function
*
2678 btrace_call_get (const struct btrace_call_iterator
*it
)
2680 if (it
->index
>= it
->btinfo
->functions
.size ())
2683 return &it
->btinfo
->functions
[it
->index
];
2689 btrace_call_number (const struct btrace_call_iterator
*it
)
2691 const unsigned int length
= it
->btinfo
->functions
.size ();
2693 /* If the last function segment contains only a single instruction (i.e. the
2694 current instruction), skip it. */
2695 if ((it
->index
== length
) && btrace_ends_with_single_insn (it
->btinfo
))
2698 return it
->index
+ 1;
2704 btrace_call_begin (struct btrace_call_iterator
*it
,
2705 const struct btrace_thread_info
*btinfo
)
2707 if (btinfo
->functions
.empty ())
2708 error (_("No trace."));
2710 it
->btinfo
= btinfo
;
2717 btrace_call_end (struct btrace_call_iterator
*it
,
2718 const struct btrace_thread_info
*btinfo
)
2720 if (btinfo
->functions
.empty ())
2721 error (_("No trace."));
2723 it
->btinfo
= btinfo
;
2724 it
->index
= btinfo
->functions
.size ();
2730 btrace_call_next (struct btrace_call_iterator
*it
, unsigned int stride
)
2732 const unsigned int length
= it
->btinfo
->functions
.size ();
2734 if (it
->index
+ stride
< length
- 1)
2735 /* Default case: Simply advance the iterator. */
2736 it
->index
+= stride
;
2737 else if (it
->index
+ stride
== length
- 1)
2739 /* We land exactly at the last function segment. If it contains only one
2740 instruction (i.e. the current instruction) it is not actually part of
2742 if (btrace_ends_with_single_insn (it
->btinfo
))
2745 it
->index
= length
- 1;
2749 /* We land past the last function segment and have to adjust the stride.
2750 If the last function segment contains only one instruction (i.e. the
2751 current instruction) it is not actually part of the trace. */
2752 if (btrace_ends_with_single_insn (it
->btinfo
))
2753 stride
= length
- it
->index
- 1;
2755 stride
= length
- it
->index
;
2766 btrace_call_prev (struct btrace_call_iterator
*it
, unsigned int stride
)
2768 const unsigned int length
= it
->btinfo
->functions
.size ();
2771 gdb_assert (it
->index
<= length
);
2773 if (stride
== 0 || it
->index
== 0)
2776 /* If we are at the end, the first step is a special case. If the last
2777 function segment contains only one instruction (i.e. the current
2778 instruction) it is not actually part of the trace. To be able to step
2779 over this instruction, we need at least one more function segment. */
2780 if ((it
->index
== length
) && (length
> 1))
2782 if (btrace_ends_with_single_insn (it
->btinfo
))
2783 it
->index
= length
- 2;
2785 it
->index
= length
- 1;
2791 stride
= std::min (stride
, it
->index
);
2793 it
->index
-= stride
;
2794 return steps
+ stride
;
2800 btrace_call_cmp (const struct btrace_call_iterator
*lhs
,
2801 const struct btrace_call_iterator
*rhs
)
2803 gdb_assert (lhs
->btinfo
== rhs
->btinfo
);
2804 return (int) (lhs
->index
- rhs
->index
);
2810 btrace_find_call_by_number (struct btrace_call_iterator
*it
,
2811 const struct btrace_thread_info
*btinfo
,
2812 unsigned int number
)
2814 const unsigned int length
= btinfo
->functions
.size ();
2816 if ((number
== 0) || (number
> length
))
2819 it
->btinfo
= btinfo
;
2820 it
->index
= number
- 1;
2827 btrace_set_insn_history (struct btrace_thread_info
*btinfo
,
2828 const struct btrace_insn_iterator
*begin
,
2829 const struct btrace_insn_iterator
*end
)
2831 if (btinfo
->insn_history
== NULL
)
2832 btinfo
->insn_history
= XCNEW (struct btrace_insn_history
);
2834 btinfo
->insn_history
->begin
= *begin
;
2835 btinfo
->insn_history
->end
= *end
;
2841 btrace_set_call_history (struct btrace_thread_info
*btinfo
,
2842 const struct btrace_call_iterator
*begin
,
2843 const struct btrace_call_iterator
*end
)
2845 gdb_assert (begin
->btinfo
== end
->btinfo
);
2847 if (btinfo
->call_history
== NULL
)
2848 btinfo
->call_history
= XCNEW (struct btrace_call_history
);
2850 btinfo
->call_history
->begin
= *begin
;
2851 btinfo
->call_history
->end
= *end
;
2857 btrace_is_replaying (struct thread_info
*tp
)
2859 return tp
->btrace
.replay
!= NULL
;
2865 btrace_is_empty (struct thread_info
*tp
)
2867 struct btrace_insn_iterator begin
, end
;
2868 struct btrace_thread_info
*btinfo
;
2870 btinfo
= &tp
->btrace
;
2872 if (btinfo
->functions
.empty ())
2875 btrace_insn_begin (&begin
, btinfo
);
2876 btrace_insn_end (&end
, btinfo
);
2878 return btrace_insn_cmp (&begin
, &end
) == 0;
2881 #if defined (HAVE_LIBIPT)
2883 /* Print a single packet. */
2886 pt_print_packet (const struct pt_packet
*packet
)
2888 switch (packet
->type
)
2891 gdb_printf (("[??: %x]"), packet
->type
);
2895 gdb_printf (("psb"));
2899 gdb_printf (("psbend"));
2903 gdb_printf (("pad"));
2907 gdb_printf (("tip %u: 0x%" PRIx64
""),
2908 packet
->payload
.ip
.ipc
,
2909 packet
->payload
.ip
.ip
);
2913 gdb_printf (("tip.pge %u: 0x%" PRIx64
""),
2914 packet
->payload
.ip
.ipc
,
2915 packet
->payload
.ip
.ip
);
2919 gdb_printf (("tip.pgd %u: 0x%" PRIx64
""),
2920 packet
->payload
.ip
.ipc
,
2921 packet
->payload
.ip
.ip
);
2925 gdb_printf (("fup %u: 0x%" PRIx64
""),
2926 packet
->payload
.ip
.ipc
,
2927 packet
->payload
.ip
.ip
);
2931 gdb_printf (("tnt-8 %u: 0x%" PRIx64
""),
2932 packet
->payload
.tnt
.bit_size
,
2933 packet
->payload
.tnt
.payload
);
2937 gdb_printf (("tnt-64 %u: 0x%" PRIx64
""),
2938 packet
->payload
.tnt
.bit_size
,
2939 packet
->payload
.tnt
.payload
);
2943 gdb_printf (("pip %" PRIx64
"%s"), packet
->payload
.pip
.cr3
,
2944 packet
->payload
.pip
.nr
? (" nr") : (""));
2948 gdb_printf (("tsc %" PRIx64
""), packet
->payload
.tsc
.tsc
);
2952 gdb_printf (("cbr %u"), packet
->payload
.cbr
.ratio
);
2956 switch (packet
->payload
.mode
.leaf
)
2959 gdb_printf (("mode %u"), packet
->payload
.mode
.leaf
);
2963 gdb_printf (("mode.exec%s%s"),
2964 packet
->payload
.mode
.bits
.exec
.csl
2966 packet
->payload
.mode
.bits
.exec
.csd
2967 ? (" cs.d") : (""));
2971 gdb_printf (("mode.tsx%s%s"),
2972 packet
->payload
.mode
.bits
.tsx
.intx
2974 packet
->payload
.mode
.bits
.tsx
.abrt
2975 ? (" abrt") : (""));
2981 gdb_printf (("ovf"));
2985 gdb_printf (("stop"));
2989 gdb_printf (("vmcs %" PRIx64
""), packet
->payload
.vmcs
.base
);
2993 gdb_printf (("tma %x %x"), packet
->payload
.tma
.ctc
,
2994 packet
->payload
.tma
.fc
);
2998 gdb_printf (("mtc %x"), packet
->payload
.mtc
.ctc
);
3002 gdb_printf (("cyc %" PRIx64
""), packet
->payload
.cyc
.value
);
3006 gdb_printf (("mnt %" PRIx64
""), packet
->payload
.mnt
.payload
);
3009 #if (LIBIPT_VERSION >= 0x200)
3011 gdb_printf (("ptw %u: 0x%" PRIx64
"%s"), packet
->payload
.ptw
.plc
,
3012 packet
->payload
.ptw
.payload
,
3013 packet
->payload
.ptw
.ip
? (" ip") : (""));
3015 #endif /* defined (LIBIPT_VERSION >= 0x200) */
3017 #if (LIBIPT_VERSION >= 0x201)
3019 gdb_printf (("cfe %u: 0x%x%s"), packet
->payload
.cfe
.type
,
3020 packet
->payload
.cfe
.vector
,
3021 packet
->payload
.cfe
.ip
? (" ip") : (""));
3025 gdb_printf (("evd %u: 0x%" PRIx64
""), packet
->payload
.evd
.type
,
3026 packet
->payload
.evd
.payload
);
3028 #endif /* defined (LIBIPT_VERSION >= 0x201) */
3032 /* Decode packets into MAINT using DECODER. */
3035 btrace_maint_decode_pt (struct btrace_maint_info
*maint
,
3036 struct pt_packet_decoder
*decoder
)
3040 if (maint
->variant
.pt
.packets
== NULL
)
3041 maint
->variant
.pt
.packets
= new std::vector
<btrace_pt_packet
>;
3045 struct btrace_pt_packet packet
;
3047 errcode
= pt_pkt_sync_forward (decoder
);
3053 pt_pkt_get_offset (decoder
, &packet
.offset
);
3055 errcode
= pt_pkt_next (decoder
, &packet
.packet
,
3056 sizeof(packet
.packet
));
3060 if (maint_btrace_pt_skip_pad
== 0 || packet
.packet
.type
!= ppt_pad
)
3062 packet
.errcode
= pt_errcode (errcode
);
3063 maint
->variant
.pt
.packets
->push_back (packet
);
3067 if (errcode
== -pte_eos
)
3070 packet
.errcode
= pt_errcode (errcode
);
3071 maint
->variant
.pt
.packets
->push_back (packet
);
3073 warning (_("Error at trace offset 0x%" PRIx64
": %s."),
3074 packet
.offset
, pt_errstr (packet
.errcode
));
3077 if (errcode
!= -pte_eos
)
3078 warning (_("Failed to synchronize onto the Intel Processor Trace "
3079 "stream: %s."), pt_errstr (pt_errcode (errcode
)));
3082 /* Update the packet history in BTINFO. */
3085 btrace_maint_update_pt_packets (struct btrace_thread_info
*btinfo
)
3087 struct pt_packet_decoder
*decoder
;
3088 const struct btrace_cpu
*cpu
;
3089 struct btrace_data_pt
*pt
;
3090 struct pt_config config
;
3093 pt
= &btinfo
->data
.variant
.pt
;
3095 /* Nothing to do if there is no trace. */
3099 memset (&config
, 0, sizeof(config
));
3101 config
.size
= sizeof (config
);
3102 config
.begin
= pt
->data
;
3103 config
.end
= pt
->data
+ pt
->size
;
3105 cpu
= record_btrace_get_cpu ();
3107 cpu
= &pt
->config
.cpu
;
3109 /* We treat an unknown vendor as 'no errata'. */
3110 if (cpu
->vendor
!= CV_UNKNOWN
)
3112 config
.cpu
.vendor
= pt_translate_cpu_vendor (cpu
->vendor
);
3113 config
.cpu
.family
= cpu
->family
;
3114 config
.cpu
.model
= cpu
->model
;
3115 config
.cpu
.stepping
= cpu
->stepping
;
3117 errcode
= pt_cpu_errata (&config
.errata
, &config
.cpu
);
3119 error (_("Failed to configure the Intel Processor Trace "
3120 "decoder: %s."), pt_errstr (pt_errcode (errcode
)));
3123 decoder
= pt_pkt_alloc_decoder (&config
);
3124 if (decoder
== NULL
)
3125 error (_("Failed to allocate the Intel Processor Trace decoder."));
3129 btrace_maint_decode_pt (&btinfo
->maint
, decoder
);
3131 catch (const gdb_exception
&except
)
3133 pt_pkt_free_decoder (decoder
);
3135 if (except
.reason
< 0)
3139 pt_pkt_free_decoder (decoder
);
3142 #endif /* !defined (HAVE_LIBIPT) */
3144 /* Update the packet maintenance information for BTINFO and store the
3145 low and high bounds into BEGIN and END, respectively.
3146 Store the current iterator state into FROM and TO. */
3149 btrace_maint_update_packets (struct btrace_thread_info
*btinfo
,
3150 unsigned int *begin
, unsigned int *end
,
3151 unsigned int *from
, unsigned int *to
)
3153 switch (btinfo
->data
.format
)
3162 case BTRACE_FORMAT_BTS
:
3163 /* Nothing to do - we operate directly on BTINFO->DATA. */
3165 *end
= btinfo
->data
.variant
.bts
.blocks
->size ();
3166 *from
= btinfo
->maint
.variant
.bts
.packet_history
.begin
;
3167 *to
= btinfo
->maint
.variant
.bts
.packet_history
.end
;
3170 #if defined (HAVE_LIBIPT)
3171 case BTRACE_FORMAT_PT
:
3172 if (btinfo
->maint
.variant
.pt
.packets
== nullptr)
3173 btinfo
->maint
.variant
.pt
.packets
= new std::vector
<btrace_pt_packet
>;
3175 if (btinfo
->maint
.variant
.pt
.packets
->empty ())
3176 btrace_maint_update_pt_packets (btinfo
);
3179 *end
= btinfo
->maint
.variant
.pt
.packets
->size ();
3180 *from
= btinfo
->maint
.variant
.pt
.packet_history
.begin
;
3181 *to
= btinfo
->maint
.variant
.pt
.packet_history
.end
;
3183 #endif /* defined (HAVE_LIBIPT) */
3187 /* Print packets in BTINFO from BEGIN (inclusive) until END (exclusive) and
3188 update the current iterator position. */
3191 btrace_maint_print_packets (struct btrace_thread_info
*btinfo
,
3192 unsigned int begin
, unsigned int end
)
3194 switch (btinfo
->data
.format
)
3199 case BTRACE_FORMAT_BTS
:
3201 const std::vector
<btrace_block
> &blocks
3202 = *btinfo
->data
.variant
.bts
.blocks
;
3205 for (blk
= begin
; blk
< end
; ++blk
)
3207 const btrace_block
&block
= blocks
.at (blk
);
3209 gdb_printf ("%u\tbegin: %s, end: %s\n", blk
,
3210 core_addr_to_string_nz (block
.begin
),
3211 core_addr_to_string_nz (block
.end
));
3214 btinfo
->maint
.variant
.bts
.packet_history
.begin
= begin
;
3215 btinfo
->maint
.variant
.bts
.packet_history
.end
= end
;
3219 #if defined (HAVE_LIBIPT)
3220 case BTRACE_FORMAT_PT
:
3222 const std::vector
<btrace_pt_packet
> &packets
3223 = *btinfo
->maint
.variant
.pt
.packets
;
3226 for (pkt
= begin
; pkt
< end
; ++pkt
)
3228 const struct btrace_pt_packet
&packet
= packets
.at (pkt
);
3230 gdb_printf ("%u\t", pkt
);
3231 gdb_printf ("0x%" PRIx64
"\t", packet
.offset
);
3233 if (packet
.errcode
== pte_ok
)
3234 pt_print_packet (&packet
.packet
);
3236 gdb_printf ("[error: %s]", pt_errstr (packet
.errcode
));
3241 btinfo
->maint
.variant
.pt
.packet_history
.begin
= begin
;
3242 btinfo
->maint
.variant
.pt
.packet_history
.end
= end
;
3245 #endif /* defined (HAVE_LIBIPT) */
3249 /* Read a number from an argument string. */
3252 get_uint (const char **arg
)
3254 const char *begin
, *pos
;
3256 unsigned long number
;
3259 pos
= skip_spaces (begin
);
3261 if (!isdigit (*pos
))
3262 error (_("Expected positive number, got: %s."), pos
);
3264 number
= strtoul (pos
, &end
, 10);
3265 if (number
> UINT_MAX
)
3266 error (_("Number too big."));
3268 *arg
+= (end
- begin
);
3270 return (unsigned int) number
;
3273 /* Read a context size from an argument string. */
3276 get_context_size (const char **arg
)
3278 const char *pos
= skip_spaces (*arg
);
3280 if (!isdigit (*pos
))
3281 error (_("Expected positive number, got: %s."), pos
);
3284 long result
= strtol (pos
, &end
, 10);
3289 /* Complain about junk at the end of an argument string. */
3292 no_chunk (const char *arg
)
3295 error (_("Junk after argument: %s."), arg
);
3298 /* The "maintenance btrace packet-history" command. */
3301 maint_btrace_packet_history_cmd (const char *arg
, int from_tty
)
3303 struct btrace_thread_info
*btinfo
;
3304 unsigned int size
, begin
, end
, from
, to
;
3306 thread_info
*tp
= current_inferior ()->find_thread (inferior_ptid
);
3308 error (_("No thread."));
3311 btinfo
= &tp
->btrace
;
3313 btrace_maint_update_packets (btinfo
, &begin
, &end
, &from
, &to
);
3316 gdb_printf (_("No trace.\n"));
3320 if (arg
== NULL
|| *arg
== 0 || strcmp (arg
, "+") == 0)
3324 if (end
- from
< size
)
3328 else if (strcmp (arg
, "-") == 0)
3332 if (to
- begin
< size
)
3338 from
= get_uint (&arg
);
3340 error (_("'%u' is out of range."), from
);
3342 arg
= skip_spaces (arg
);
3345 arg
= skip_spaces (++arg
);
3350 size
= get_context_size (&arg
);
3354 if (end
- from
< size
)
3358 else if (*arg
== '-')
3361 size
= get_context_size (&arg
);
3365 /* Include the packet given as first argument. */
3369 if (to
- begin
< size
)
3375 to
= get_uint (&arg
);
3377 /* Include the packet at the second argument and silently
3378 truncate the range. */
3391 if (end
- from
< size
)
3399 btrace_maint_print_packets (btinfo
, from
, to
);
3402 /* The "maintenance btrace clear-packet-history" command. */
3405 maint_btrace_clear_packet_history_cmd (const char *args
, int from_tty
)
3407 if (args
!= NULL
&& *args
!= 0)
3408 error (_("Invalid argument."));
3410 if (inferior_ptid
== null_ptid
)
3411 error (_("No thread."));
3413 thread_info
*tp
= inferior_thread ();
3414 btrace_thread_info
*btinfo
= &tp
->btrace
;
3416 /* Must clear the maint data before - it depends on BTINFO->DATA. */
3417 btrace_maint_clear (btinfo
);
3418 btinfo
->data
.clear ();
3421 /* The "maintenance btrace clear" command. */
3424 maint_btrace_clear_cmd (const char *args
, int from_tty
)
3426 if (args
!= NULL
&& *args
!= 0)
3427 error (_("Invalid argument."));
3429 if (inferior_ptid
== null_ptid
)
3430 error (_("No thread."));
3432 thread_info
*tp
= inferior_thread ();
3436 /* The "maintenance info btrace" command. */
3439 maint_info_btrace_cmd (const char *args
, int from_tty
)
3441 struct btrace_thread_info
*btinfo
;
3442 const struct btrace_config
*conf
;
3444 if (args
!= NULL
&& *args
!= 0)
3445 error (_("Invalid argument."));
3447 if (inferior_ptid
== null_ptid
)
3448 error (_("No thread."));
3450 thread_info
*tp
= inferior_thread ();
3452 btinfo
= &tp
->btrace
;
3454 conf
= btrace_conf (btinfo
);
3456 error (_("No btrace configuration."));
3458 gdb_printf (_("Format: %s.\n"),
3459 btrace_format_string (conf
->format
));
3461 switch (conf
->format
)
3466 case BTRACE_FORMAT_BTS
:
3467 gdb_printf (_("Number of packets: %zu.\n"),
3468 btinfo
->data
.variant
.bts
.blocks
->size ());
3471 #if defined (HAVE_LIBIPT)
3472 case BTRACE_FORMAT_PT
:
3474 struct pt_version version
;
3476 version
= pt_library_version ();
3477 gdb_printf (_("Version: %u.%u.%u%s.\n"), version
.major
,
3478 version
.minor
, version
.build
,
3479 version
.ext
!= NULL
? version
.ext
: "");
3481 btrace_maint_update_pt_packets (btinfo
);
3482 gdb_printf (_("Number of packets: %zu.\n"),
3483 ((btinfo
->maint
.variant
.pt
.packets
== nullptr)
3484 ? 0 : btinfo
->maint
.variant
.pt
.packets
->size ()));
3487 #endif /* defined (HAVE_LIBIPT) */
3491 /* The "maint show btrace pt skip-pad" show value function. */
3494 show_maint_btrace_pt_skip_pad (struct ui_file
*file
, int from_tty
,
3495 struct cmd_list_element
*c
,
3498 gdb_printf (file
, _("Skip PAD packets is %s.\n"), value
);
3502 /* Initialize btrace maintenance commands. */
3504 INIT_GDB_FILE (btrace
)
3506 add_cmd ("btrace", class_maintenance
, maint_info_btrace_cmd
,
3507 _("Info about branch tracing data."), &maintenanceinfolist
);
3509 add_basic_prefix_cmd ("btrace", class_maintenance
,
3510 _("Branch tracing maintenance commands."),
3511 &maint_btrace_cmdlist
, 0, &maintenancelist
);
3513 add_setshow_prefix_cmd ("btrace", class_maintenance
,
3514 _("Set branch tracing specific variables."),
3515 _("Show branch tracing specific variables."),
3516 &maint_btrace_set_cmdlist
,
3517 &maint_btrace_show_cmdlist
,
3518 &maintenance_set_cmdlist
,
3519 &maintenance_show_cmdlist
);
3521 add_setshow_prefix_cmd ("pt", class_maintenance
,
3522 _("Set Intel Processor Trace specific variables."),
3523 _("Show Intel Processor Trace specific variables."),
3524 &maint_btrace_pt_set_cmdlist
,
3525 &maint_btrace_pt_show_cmdlist
,
3526 &maint_btrace_set_cmdlist
,
3527 &maint_btrace_show_cmdlist
);
3529 add_setshow_boolean_cmd ("skip-pad", class_maintenance
,
3530 &maint_btrace_pt_skip_pad
, _("\
3531 Set whether PAD packets should be skipped in the btrace packet history."), _("\
3532 Show whether PAD packets should be skipped in the btrace packet history."),_("\
3533 When enabled, PAD packets are ignored in the btrace packet history."),
3534 NULL
, show_maint_btrace_pt_skip_pad
,
3535 &maint_btrace_pt_set_cmdlist
,
3536 &maint_btrace_pt_show_cmdlist
);
3538 add_cmd ("packet-history", class_maintenance
, maint_btrace_packet_history_cmd
,
3539 _("Print the raw branch tracing data.\n\
3540 With no argument, print ten more packets after the previous ten-line print.\n\
3541 With '-' as argument print ten packets before a previous ten-line print.\n\
3542 One argument specifies the starting packet of a ten-line print.\n\
3543 Two arguments with comma between specify starting and ending packets to \
3545 Preceded with '+'/'-' the second argument specifies the distance from the \
3547 &maint_btrace_cmdlist
);
3549 add_cmd ("clear-packet-history", class_maintenance
,
3550 maint_btrace_clear_packet_history_cmd
,
3551 _("Clears the branch tracing packet history.\n\
3552 Discards the raw branch tracing data but not the execution history data."),
3553 &maint_btrace_cmdlist
);
3555 add_cmd ("clear", class_maintenance
, maint_btrace_clear_cmd
,
3556 _("Clears the branch tracing data.\n\
3557 Discards the raw branch tracing data and the execution history data.\n\
3558 The next 'record' command will fetch the branch tracing data anew."),
3559 &maint_btrace_cmdlist
);