1 /* Branch trace support for GDB, the GNU debugger.
3 Copyright (C) 2013-2015 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"
41 /* Command lists for btrace maintenance commands. */
42 static struct cmd_list_element
*maint_btrace_cmdlist
;
43 static struct cmd_list_element
*maint_btrace_set_cmdlist
;
44 static struct cmd_list_element
*maint_btrace_show_cmdlist
;
45 static struct cmd_list_element
*maint_btrace_pt_set_cmdlist
;
46 static struct cmd_list_element
*maint_btrace_pt_show_cmdlist
;
48 /* Control whether to skip PAD packets when computing the packet history. */
49 static int maint_btrace_pt_skip_pad
= 1;
51 static void btrace_add_pc (struct thread_info
*tp
);
53 /* Print a record debug message. Use do ... while (0) to avoid ambiguities
54 when used in if statements. */
56 #define DEBUG(msg, args...) \
59 if (record_debug != 0) \
60 fprintf_unfiltered (gdb_stdlog, \
61 "[btrace] " msg "\n", ##args); \
65 #define DEBUG_FTRACE(msg, args...) DEBUG ("[ftrace] " msg, ##args)
67 /* Return the function name of a recorded function segment for printing.
68 This function never returns NULL. */
71 ftrace_print_function_name (const struct btrace_function
*bfun
)
73 struct minimal_symbol
*msym
;
80 return SYMBOL_PRINT_NAME (sym
);
83 return MSYMBOL_PRINT_NAME (msym
);
88 /* Return the file name of a recorded function segment for printing.
89 This function never returns NULL. */
92 ftrace_print_filename (const struct btrace_function
*bfun
)
100 filename
= symtab_to_filename_for_display (symbol_symtab (sym
));
102 filename
= "<unknown>";
107 /* Return a string representation of the address of an instruction.
108 This function never returns NULL. */
111 ftrace_print_insn_addr (const struct btrace_insn
*insn
)
116 return core_addr_to_string_nz (insn
->pc
);
119 /* Print an ftrace debug status message. */
122 ftrace_debug (const struct btrace_function
*bfun
, const char *prefix
)
124 const char *fun
, *file
;
125 unsigned int ibegin
, iend
;
128 fun
= ftrace_print_function_name (bfun
);
129 file
= ftrace_print_filename (bfun
);
132 ibegin
= bfun
->insn_offset
;
133 iend
= ibegin
+ VEC_length (btrace_insn_s
, bfun
->insn
);
135 DEBUG_FTRACE ("%s: fun = %s, file = %s, level = %d, insn = [%u; %u)",
136 prefix
, fun
, file
, level
, ibegin
, iend
);
139 /* Return non-zero if BFUN does not match MFUN and FUN,
140 return zero otherwise. */
143 ftrace_function_switched (const struct btrace_function
*bfun
,
144 const struct minimal_symbol
*mfun
,
145 const struct symbol
*fun
)
147 struct minimal_symbol
*msym
;
153 /* If the minimal symbol changed, we certainly switched functions. */
154 if (mfun
!= NULL
&& msym
!= NULL
155 && strcmp (MSYMBOL_LINKAGE_NAME (mfun
), MSYMBOL_LINKAGE_NAME (msym
)) != 0)
158 /* If the symbol changed, we certainly switched functions. */
159 if (fun
!= NULL
&& sym
!= NULL
)
161 const char *bfname
, *fname
;
163 /* Check the function name. */
164 if (strcmp (SYMBOL_LINKAGE_NAME (fun
), SYMBOL_LINKAGE_NAME (sym
)) != 0)
167 /* Check the location of those functions, as well. */
168 bfname
= symtab_to_fullname (symbol_symtab (sym
));
169 fname
= symtab_to_fullname (symbol_symtab (fun
));
170 if (filename_cmp (fname
, bfname
) != 0)
174 /* If we lost symbol information, we switched functions. */
175 if (!(msym
== NULL
&& sym
== NULL
) && mfun
== NULL
&& fun
== NULL
)
178 /* If we gained symbol information, we switched functions. */
179 if (msym
== NULL
&& sym
== NULL
&& !(mfun
== NULL
&& fun
== NULL
))
185 /* Allocate and initialize a new branch trace function segment.
186 PREV is the chronologically preceding function segment.
187 MFUN and FUN are the symbol information we have for this function. */
189 static struct btrace_function
*
190 ftrace_new_function (struct btrace_function
*prev
,
191 struct minimal_symbol
*mfun
,
194 struct btrace_function
*bfun
;
196 bfun
= xzalloc (sizeof (*bfun
));
200 bfun
->flow
.prev
= prev
;
204 /* Start counting at one. */
206 bfun
->insn_offset
= 1;
210 gdb_assert (prev
->flow
.next
== NULL
);
211 prev
->flow
.next
= bfun
;
213 bfun
->number
= prev
->number
+ 1;
214 bfun
->insn_offset
= (prev
->insn_offset
215 + VEC_length (btrace_insn_s
, prev
->insn
));
216 bfun
->level
= prev
->level
;
222 /* Update the UP field of a function segment. */
225 ftrace_update_caller (struct btrace_function
*bfun
,
226 struct btrace_function
*caller
,
227 enum btrace_function_flag flags
)
229 if (bfun
->up
!= NULL
)
230 ftrace_debug (bfun
, "updating caller");
235 ftrace_debug (bfun
, "set caller");
238 /* Fix up the caller for all segments of a function. */
241 ftrace_fixup_caller (struct btrace_function
*bfun
,
242 struct btrace_function
*caller
,
243 enum btrace_function_flag flags
)
245 struct btrace_function
*prev
, *next
;
247 ftrace_update_caller (bfun
, caller
, flags
);
249 /* Update all function segments belonging to the same function. */
250 for (prev
= bfun
->segment
.prev
; prev
!= NULL
; prev
= prev
->segment
.prev
)
251 ftrace_update_caller (prev
, caller
, flags
);
253 for (next
= bfun
->segment
.next
; next
!= NULL
; next
= next
->segment
.next
)
254 ftrace_update_caller (next
, caller
, flags
);
257 /* Add a new function segment for a call.
258 CALLER is the chronologically preceding function segment.
259 MFUN and FUN are the symbol information we have for this function. */
261 static struct btrace_function
*
262 ftrace_new_call (struct btrace_function
*caller
,
263 struct minimal_symbol
*mfun
,
266 struct btrace_function
*bfun
;
268 bfun
= ftrace_new_function (caller
, mfun
, fun
);
272 ftrace_debug (bfun
, "new call");
277 /* Add a new function segment for a tail call.
278 CALLER is the chronologically preceding function segment.
279 MFUN and FUN are the symbol information we have for this function. */
281 static struct btrace_function
*
282 ftrace_new_tailcall (struct btrace_function
*caller
,
283 struct minimal_symbol
*mfun
,
286 struct btrace_function
*bfun
;
288 bfun
= ftrace_new_function (caller
, mfun
, fun
);
291 bfun
->flags
|= BFUN_UP_LINKS_TO_TAILCALL
;
293 ftrace_debug (bfun
, "new tail call");
298 /* Find the innermost caller in the back trace of BFUN with MFUN/FUN
299 symbol information. */
301 static struct btrace_function
*
302 ftrace_find_caller (struct btrace_function
*bfun
,
303 struct minimal_symbol
*mfun
,
306 for (; bfun
!= NULL
; bfun
= bfun
->up
)
308 /* Skip functions with incompatible symbol information. */
309 if (ftrace_function_switched (bfun
, mfun
, fun
))
312 /* This is the function segment we're looking for. */
319 /* Find the innermost caller in the back trace of BFUN, skipping all
320 function segments that do not end with a call instruction (e.g.
321 tail calls ending with a jump). */
323 static struct btrace_function
*
324 ftrace_find_call (struct btrace_function
*bfun
)
326 for (; bfun
!= NULL
; bfun
= bfun
->up
)
328 struct btrace_insn
*last
;
331 if (bfun
->errcode
!= 0)
334 last
= VEC_last (btrace_insn_s
, bfun
->insn
);
336 if (last
->iclass
== BTRACE_INSN_CALL
)
343 /* Add a continuation segment for a function into which we return.
344 PREV is the chronologically preceding function segment.
345 MFUN and FUN are the symbol information we have for this function. */
347 static struct btrace_function
*
348 ftrace_new_return (struct btrace_function
*prev
,
349 struct minimal_symbol
*mfun
,
352 struct btrace_function
*bfun
, *caller
;
354 bfun
= ftrace_new_function (prev
, mfun
, fun
);
356 /* It is important to start at PREV's caller. Otherwise, we might find
357 PREV itself, if PREV is a recursive function. */
358 caller
= ftrace_find_caller (prev
->up
, mfun
, fun
);
361 /* The caller of PREV is the preceding btrace function segment in this
362 function instance. */
363 gdb_assert (caller
->segment
.next
== NULL
);
365 caller
->segment
.next
= bfun
;
366 bfun
->segment
.prev
= caller
;
368 /* Maintain the function level. */
369 bfun
->level
= caller
->level
;
371 /* Maintain the call stack. */
372 bfun
->up
= caller
->up
;
373 bfun
->flags
= caller
->flags
;
375 ftrace_debug (bfun
, "new return");
379 /* We did not find a caller. This could mean that something went
380 wrong or that the call is simply not included in the trace. */
382 /* Let's search for some actual call. */
383 caller
= ftrace_find_call (prev
->up
);
386 /* There is no call in PREV's back trace. We assume that the
387 branch trace did not include it. */
389 /* Let's find the topmost call function - this skips tail calls. */
390 while (prev
->up
!= NULL
)
393 /* We maintain levels for a series of returns for which we have
395 We start at the preceding function's level in case this has
396 already been a return for which we have not seen the call.
397 We start at level 0 otherwise, to handle tail calls correctly. */
398 bfun
->level
= min (0, prev
->level
) - 1;
400 /* Fix up the call stack for PREV. */
401 ftrace_fixup_caller (prev
, bfun
, BFUN_UP_LINKS_TO_RET
);
403 ftrace_debug (bfun
, "new return - no caller");
407 /* There is a call in PREV's back trace to which we should have
408 returned. Let's remain at this level. */
409 bfun
->level
= prev
->level
;
411 ftrace_debug (bfun
, "new return - unknown caller");
418 /* Add a new function segment for a function switch.
419 PREV is the chronologically preceding function segment.
420 MFUN and FUN are the symbol information we have for this function. */
422 static struct btrace_function
*
423 ftrace_new_switch (struct btrace_function
*prev
,
424 struct minimal_symbol
*mfun
,
427 struct btrace_function
*bfun
;
429 /* This is an unexplained function switch. The call stack will likely
430 be wrong at this point. */
431 bfun
= ftrace_new_function (prev
, mfun
, fun
);
433 ftrace_debug (bfun
, "new switch");
438 /* Add a new function segment for a gap in the trace due to a decode error.
439 PREV is the chronologically preceding function segment.
440 ERRCODE is the format-specific error code. */
442 static struct btrace_function
*
443 ftrace_new_gap (struct btrace_function
*prev
, int errcode
)
445 struct btrace_function
*bfun
;
447 /* We hijack prev if it was empty. */
448 if (prev
!= NULL
&& prev
->errcode
== 0
449 && VEC_empty (btrace_insn_s
, prev
->insn
))
452 bfun
= ftrace_new_function (prev
, NULL
, NULL
);
454 bfun
->errcode
= errcode
;
456 ftrace_debug (bfun
, "new gap");
461 /* Update BFUN with respect to the instruction at PC. This may create new
463 Return the chronologically latest function segment, never NULL. */
465 static struct btrace_function
*
466 ftrace_update_function (struct btrace_function
*bfun
, CORE_ADDR pc
)
468 struct bound_minimal_symbol bmfun
;
469 struct minimal_symbol
*mfun
;
471 struct btrace_insn
*last
;
473 /* Try to determine the function we're in. We use both types of symbols
474 to avoid surprises when we sometimes get a full symbol and sometimes
475 only a minimal symbol. */
476 fun
= find_pc_function (pc
);
477 bmfun
= lookup_minimal_symbol_by_pc (pc
);
480 if (fun
== NULL
&& mfun
== NULL
)
481 DEBUG_FTRACE ("no symbol at %s", core_addr_to_string_nz (pc
));
483 /* If we didn't have a function or if we had a gap before, we create one. */
484 if (bfun
== NULL
|| bfun
->errcode
!= 0)
485 return ftrace_new_function (bfun
, mfun
, fun
);
487 /* Check the last instruction, if we have one.
488 We do this check first, since it allows us to fill in the call stack
489 links in addition to the normal flow links. */
491 if (!VEC_empty (btrace_insn_s
, bfun
->insn
))
492 last
= VEC_last (btrace_insn_s
, bfun
->insn
);
496 switch (last
->iclass
)
498 case BTRACE_INSN_RETURN
:
502 /* On some systems, _dl_runtime_resolve returns to the resolved
503 function instead of jumping to it. From our perspective,
504 however, this is a tailcall.
505 If we treated it as return, we wouldn't be able to find the
506 resolved function in our stack back trace. Hence, we would
507 lose the current stack back trace and start anew with an empty
508 back trace. When the resolved function returns, we would then
509 create a stack back trace with the same function names but
510 different frame id's. This will confuse stepping. */
511 fname
= ftrace_print_function_name (bfun
);
512 if (strcmp (fname
, "_dl_runtime_resolve") == 0)
513 return ftrace_new_tailcall (bfun
, mfun
, fun
);
515 return ftrace_new_return (bfun
, mfun
, fun
);
518 case BTRACE_INSN_CALL
:
519 /* Ignore calls to the next instruction. They are used for PIC. */
520 if (last
->pc
+ last
->size
== pc
)
523 return ftrace_new_call (bfun
, mfun
, fun
);
525 case BTRACE_INSN_JUMP
:
529 start
= get_pc_function_start (pc
);
531 /* If we can't determine the function for PC, we treat a jump at
532 the end of the block as tail call. */
533 if (start
== 0 || start
== pc
)
534 return ftrace_new_tailcall (bfun
, mfun
, fun
);
539 /* Check if we're switching functions for some other reason. */
540 if (ftrace_function_switched (bfun
, mfun
, fun
))
542 DEBUG_FTRACE ("switching from %s in %s at %s",
543 ftrace_print_insn_addr (last
),
544 ftrace_print_function_name (bfun
),
545 ftrace_print_filename (bfun
));
547 return ftrace_new_switch (bfun
, mfun
, fun
);
553 /* Add the instruction at PC to BFUN's instructions. */
556 ftrace_update_insns (struct btrace_function
*bfun
,
557 const struct btrace_insn
*insn
)
559 VEC_safe_push (btrace_insn_s
, bfun
->insn
, insn
);
561 if (record_debug
> 1)
562 ftrace_debug (bfun
, "update insn");
565 /* Classify the instruction at PC. */
567 static enum btrace_insn_class
568 ftrace_classify_insn (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
570 enum btrace_insn_class iclass
;
572 iclass
= BTRACE_INSN_OTHER
;
575 if (gdbarch_insn_is_call (gdbarch
, pc
))
576 iclass
= BTRACE_INSN_CALL
;
577 else if (gdbarch_insn_is_ret (gdbarch
, pc
))
578 iclass
= BTRACE_INSN_RETURN
;
579 else if (gdbarch_insn_is_jump (gdbarch
, pc
))
580 iclass
= BTRACE_INSN_JUMP
;
582 CATCH (error
, RETURN_MASK_ERROR
)
590 /* Compute the function branch trace from BTS trace. */
593 btrace_compute_ftrace_bts (struct thread_info
*tp
,
594 const struct btrace_data_bts
*btrace
)
596 struct btrace_thread_info
*btinfo
;
597 struct btrace_function
*begin
, *end
;
598 struct gdbarch
*gdbarch
;
599 unsigned int blk
, ngaps
;
602 gdbarch
= target_gdbarch ();
603 btinfo
= &tp
->btrace
;
604 begin
= btinfo
->begin
;
606 ngaps
= btinfo
->ngaps
;
607 level
= begin
!= NULL
? -btinfo
->level
: INT_MAX
;
608 blk
= VEC_length (btrace_block_s
, btrace
->blocks
);
612 btrace_block_s
*block
;
617 block
= VEC_index (btrace_block_s
, btrace
->blocks
, blk
);
622 struct btrace_insn insn
;
625 /* We should hit the end of the block. Warn if we went too far. */
628 /* Indicate the gap in the trace - unless we're at the
632 warning (_("Recorded trace may be corrupted around %s."),
633 core_addr_to_string_nz (pc
));
635 end
= ftrace_new_gap (end
, BDE_BTS_OVERFLOW
);
641 end
= ftrace_update_function (end
, pc
);
645 /* Maintain the function level offset.
646 For all but the last block, we do it here. */
648 level
= min (level
, end
->level
);
653 size
= gdb_insn_length (gdbarch
, pc
);
655 CATCH (error
, RETURN_MASK_ERROR
)
662 insn
.iclass
= ftrace_classify_insn (gdbarch
, pc
);
664 ftrace_update_insns (end
, &insn
);
666 /* We're done once we pushed the instruction at the end. */
667 if (block
->end
== pc
)
670 /* We can't continue if we fail to compute the size. */
673 warning (_("Recorded trace may be incomplete around %s."),
674 core_addr_to_string_nz (pc
));
676 /* Indicate the gap in the trace. We just added INSN so we're
677 not at the beginning. */
678 end
= ftrace_new_gap (end
, BDE_BTS_INSN_SIZE
);
686 /* Maintain the function level offset.
687 For the last block, we do it here to not consider the last
689 Since the last instruction corresponds to the current instruction
690 and is not really part of the execution history, it shouldn't
693 level
= min (level
, end
->level
);
697 btinfo
->begin
= begin
;
699 btinfo
->ngaps
= ngaps
;
701 /* LEVEL is the minimal function level of all btrace function segments.
702 Define the global level offset to -LEVEL so all function levels are
703 normalized to start at zero. */
704 btinfo
->level
= -level
;
707 #if defined (HAVE_LIBIPT)
709 static enum btrace_insn_class
710 pt_reclassify_insn (enum pt_insn_class iclass
)
715 return BTRACE_INSN_CALL
;
718 return BTRACE_INSN_RETURN
;
721 return BTRACE_INSN_JUMP
;
724 return BTRACE_INSN_OTHER
;
728 /* Add function branch trace using DECODER. */
731 ftrace_add_pt (struct pt_insn_decoder
*decoder
,
732 struct btrace_function
**pbegin
,
733 struct btrace_function
**pend
, int *plevel
,
736 struct btrace_function
*begin
, *end
, *upd
;
738 int errcode
, nerrors
;
745 struct btrace_insn btinsn
;
748 errcode
= pt_insn_sync_forward (decoder
);
751 if (errcode
!= -pte_eos
)
752 warning (_("Failed to synchronize onto the Intel(R) Processor "
753 "Trace stream: %s."), pt_errstr (pt_errcode (errcode
)));
757 memset (&btinsn
, 0, sizeof (btinsn
));
760 errcode
= pt_insn_next (decoder
, &insn
, sizeof(insn
));
764 /* Look for gaps in the trace - unless we're at the beginning. */
767 /* Tracing is disabled and re-enabled each time we enter the
768 kernel. Most times, we continue from the same instruction we
769 stopped before. This is indicated via the RESUMED instruction
770 flag. The ENABLED instruction flag means that we continued
771 from some other instruction. Indicate this as a trace gap. */
773 *pend
= end
= ftrace_new_gap (end
, BDE_PT_DISABLED
);
775 /* Indicate trace overflows. */
777 *pend
= end
= ftrace_new_gap (end
, BDE_PT_OVERFLOW
);
780 upd
= ftrace_update_function (end
, insn
.ip
);
786 *pbegin
= begin
= upd
;
789 /* Maintain the function level offset. */
790 *plevel
= min (*plevel
, end
->level
);
792 btinsn
.pc
= (CORE_ADDR
) insn
.ip
;
793 btinsn
.size
= (gdb_byte
) insn
.size
;
794 btinsn
.iclass
= pt_reclassify_insn (insn
.iclass
);
796 ftrace_update_insns (end
, &btinsn
);
799 if (errcode
== -pte_eos
)
802 /* If the gap is at the very beginning, we ignore it - we will have
803 less trace, but we won't have any holes in the trace. */
807 pt_insn_get_offset (decoder
, &offset
);
809 warning (_("Failed to decode Intel(R) Processor Trace near trace "
810 "offset 0x%" PRIx64
" near recorded PC 0x%" PRIx64
": %s."),
811 offset
, insn
.ip
, pt_errstr (pt_errcode (errcode
)));
813 /* Indicate the gap in the trace. */
814 *pend
= end
= ftrace_new_gap (end
, errcode
);
819 warning (_("The recorded execution trace may have gaps."));
822 /* A callback function to allow the trace decoder to read the inferior's
826 btrace_pt_readmem_callback (gdb_byte
*buffer
, size_t size
,
827 const struct pt_asid
*asid
, uint64_t pc
,
834 errcode
= target_read_code ((CORE_ADDR
) pc
, buffer
, size
);
838 CATCH (error
, RETURN_MASK_ERROR
)
847 /* Translate the vendor from one enum to another. */
849 static enum pt_cpu_vendor
850 pt_translate_cpu_vendor (enum btrace_cpu_vendor vendor
)
862 /* Finalize the function branch trace after decode. */
864 static void btrace_finalize_ftrace_pt (struct pt_insn_decoder
*decoder
,
865 struct thread_info
*tp
, int level
)
867 pt_insn_free_decoder (decoder
);
869 /* LEVEL is the minimal function level of all btrace function segments.
870 Define the global level offset to -LEVEL so all function levels are
871 normalized to start at zero. */
872 tp
->btrace
.level
= -level
;
874 /* Add a single last instruction entry for the current PC.
875 This allows us to compute the backtrace at the current PC using both
876 standard unwind and btrace unwind.
877 This extra entry is ignored by all record commands. */
881 /* Compute the function branch trace from Intel(R) Processor Trace. */
884 btrace_compute_ftrace_pt (struct thread_info
*tp
,
885 const struct btrace_data_pt
*btrace
)
887 struct btrace_thread_info
*btinfo
;
888 struct pt_insn_decoder
*decoder
;
889 struct pt_config config
;
892 if (btrace
->size
== 0)
895 btinfo
= &tp
->btrace
;
896 level
= btinfo
->begin
!= NULL
? -btinfo
->level
: INT_MAX
;
898 pt_config_init(&config
);
899 config
.begin
= btrace
->data
;
900 config
.end
= btrace
->data
+ btrace
->size
;
902 config
.cpu
.vendor
= pt_translate_cpu_vendor (btrace
->config
.cpu
.vendor
);
903 config
.cpu
.family
= btrace
->config
.cpu
.family
;
904 config
.cpu
.model
= btrace
->config
.cpu
.model
;
905 config
.cpu
.stepping
= btrace
->config
.cpu
.stepping
;
907 errcode
= pt_cpu_errata (&config
.errata
, &config
.cpu
);
909 error (_("Failed to configure the Intel(R) Processor Trace decoder: %s."),
910 pt_errstr (pt_errcode (errcode
)));
912 decoder
= pt_insn_alloc_decoder (&config
);
914 error (_("Failed to allocate the Intel(R) Processor Trace decoder."));
918 struct pt_image
*image
;
920 image
= pt_insn_get_image(decoder
);
922 error (_("Failed to configure the Intel(R) Processor Trace decoder."));
924 errcode
= pt_image_set_callback(image
, btrace_pt_readmem_callback
, NULL
);
926 error (_("Failed to configure the Intel(R) Processor Trace decoder: "
927 "%s."), pt_errstr (pt_errcode (errcode
)));
929 ftrace_add_pt (decoder
, &btinfo
->begin
, &btinfo
->end
, &level
,
932 CATCH (error
, RETURN_MASK_ALL
)
934 /* Indicate a gap in the trace if we quit trace processing. */
935 if (error
.reason
== RETURN_QUIT
&& btinfo
->end
!= NULL
)
937 btinfo
->end
= ftrace_new_gap (btinfo
->end
, BDE_PT_USER_QUIT
);
941 btrace_finalize_ftrace_pt (decoder
, tp
, level
);
943 throw_exception (error
);
947 btrace_finalize_ftrace_pt (decoder
, tp
, level
);
950 #else /* defined (HAVE_LIBIPT) */
953 btrace_compute_ftrace_pt (struct thread_info
*tp
,
954 const struct btrace_data_pt
*btrace
)
956 internal_error (__FILE__
, __LINE__
, _("Unexpected branch trace format."));
959 #endif /* defined (HAVE_LIBIPT) */
961 /* Compute the function branch trace from a block branch trace BTRACE for
962 a thread given by BTINFO. */
965 btrace_compute_ftrace (struct thread_info
*tp
, struct btrace_data
*btrace
)
967 DEBUG ("compute ftrace");
969 switch (btrace
->format
)
971 case BTRACE_FORMAT_NONE
:
974 case BTRACE_FORMAT_BTS
:
975 btrace_compute_ftrace_bts (tp
, &btrace
->variant
.bts
);
978 case BTRACE_FORMAT_PT
:
979 btrace_compute_ftrace_pt (tp
, &btrace
->variant
.pt
);
983 internal_error (__FILE__
, __LINE__
, _("Unkown branch trace format."));
986 /* Add an entry for the current PC. */
989 btrace_add_pc (struct thread_info
*tp
)
991 struct btrace_data btrace
;
992 struct btrace_block
*block
;
993 struct regcache
*regcache
;
994 struct cleanup
*cleanup
;
997 regcache
= get_thread_regcache (tp
->ptid
);
998 pc
= regcache_read_pc (regcache
);
1000 btrace_data_init (&btrace
);
1001 btrace
.format
= BTRACE_FORMAT_BTS
;
1002 btrace
.variant
.bts
.blocks
= NULL
;
1004 cleanup
= make_cleanup_btrace_data (&btrace
);
1006 block
= VEC_safe_push (btrace_block_s
, btrace
.variant
.bts
.blocks
, NULL
);
1010 btrace_compute_ftrace (tp
, &btrace
);
1012 do_cleanups (cleanup
);
1018 btrace_enable (struct thread_info
*tp
, const struct btrace_config
*conf
)
1020 if (tp
->btrace
.target
!= NULL
)
1023 if (!target_supports_btrace (conf
->format
))
1024 error (_("Target does not support branch tracing."));
1026 DEBUG ("enable thread %d (%s)", tp
->num
, target_pid_to_str (tp
->ptid
));
1028 tp
->btrace
.target
= target_enable_btrace (tp
->ptid
, conf
);
1030 /* Add an entry for the current PC so we start tracing from where we
1032 if (tp
->btrace
.target
!= NULL
)
1038 const struct btrace_config
*
1039 btrace_conf (const struct btrace_thread_info
*btinfo
)
1041 if (btinfo
->target
== NULL
)
1044 return target_btrace_conf (btinfo
->target
);
1050 btrace_disable (struct thread_info
*tp
)
1052 struct btrace_thread_info
*btp
= &tp
->btrace
;
1055 if (btp
->target
== NULL
)
1058 DEBUG ("disable thread %d (%s)", tp
->num
, target_pid_to_str (tp
->ptid
));
1060 target_disable_btrace (btp
->target
);
1069 btrace_teardown (struct thread_info
*tp
)
1071 struct btrace_thread_info
*btp
= &tp
->btrace
;
1074 if (btp
->target
== NULL
)
1077 DEBUG ("teardown thread %d (%s)", tp
->num
, target_pid_to_str (tp
->ptid
));
1079 target_teardown_btrace (btp
->target
);
1085 /* Stitch branch trace in BTS format. */
1088 btrace_stitch_bts (struct btrace_data_bts
*btrace
, struct thread_info
*tp
)
1090 struct btrace_thread_info
*btinfo
;
1091 struct btrace_function
*last_bfun
;
1092 struct btrace_insn
*last_insn
;
1093 btrace_block_s
*first_new_block
;
1095 btinfo
= &tp
->btrace
;
1096 last_bfun
= btinfo
->end
;
1097 gdb_assert (last_bfun
!= NULL
);
1098 gdb_assert (!VEC_empty (btrace_block_s
, btrace
->blocks
));
1100 /* If the existing trace ends with a gap, we just glue the traces
1101 together. We need to drop the last (i.e. chronologically first) block
1102 of the new trace, though, since we can't fill in the start address.*/
1103 if (VEC_empty (btrace_insn_s
, last_bfun
->insn
))
1105 VEC_pop (btrace_block_s
, btrace
->blocks
);
1109 /* Beware that block trace starts with the most recent block, so the
1110 chronologically first block in the new trace is the last block in
1111 the new trace's block vector. */
1112 first_new_block
= VEC_last (btrace_block_s
, btrace
->blocks
);
1113 last_insn
= VEC_last (btrace_insn_s
, last_bfun
->insn
);
1115 /* If the current PC at the end of the block is the same as in our current
1116 trace, there are two explanations:
1117 1. we executed the instruction and some branch brought us back.
1118 2. we have not made any progress.
1119 In the first case, the delta trace vector should contain at least two
1121 In the second case, the delta trace vector should contain exactly one
1122 entry for the partial block containing the current PC. Remove it. */
1123 if (first_new_block
->end
== last_insn
->pc
1124 && VEC_length (btrace_block_s
, btrace
->blocks
) == 1)
1126 VEC_pop (btrace_block_s
, btrace
->blocks
);
1130 DEBUG ("stitching %s to %s", ftrace_print_insn_addr (last_insn
),
1131 core_addr_to_string_nz (first_new_block
->end
));
1133 /* Do a simple sanity check to make sure we don't accidentally end up
1134 with a bad block. This should not occur in practice. */
1135 if (first_new_block
->end
< last_insn
->pc
)
1137 warning (_("Error while trying to read delta trace. Falling back to "
1142 /* We adjust the last block to start at the end of our current trace. */
1143 gdb_assert (first_new_block
->begin
== 0);
1144 first_new_block
->begin
= last_insn
->pc
;
1146 /* We simply pop the last insn so we can insert it again as part of
1147 the normal branch trace computation.
1148 Since instruction iterators are based on indices in the instructions
1149 vector, we don't leave any pointers dangling. */
1150 DEBUG ("pruning insn at %s for stitching",
1151 ftrace_print_insn_addr (last_insn
));
1153 VEC_pop (btrace_insn_s
, last_bfun
->insn
);
1155 /* The instructions vector may become empty temporarily if this has
1156 been the only instruction in this function segment.
1157 This violates the invariant but will be remedied shortly by
1158 btrace_compute_ftrace when we add the new trace. */
1160 /* The only case where this would hurt is if the entire trace consisted
1161 of just that one instruction. If we remove it, we might turn the now
1162 empty btrace function segment into a gap. But we don't want gaps at
1163 the beginning. To avoid this, we remove the entire old trace. */
1164 if (last_bfun
== btinfo
->begin
&& VEC_empty (btrace_insn_s
, last_bfun
->insn
))
1170 /* Adjust the block trace in order to stitch old and new trace together.
1171 BTRACE is the new delta trace between the last and the current stop.
1172 TP is the traced thread.
1173 May modifx BTRACE as well as the existing trace in TP.
1174 Return 0 on success, -1 otherwise. */
1177 btrace_stitch_trace (struct btrace_data
*btrace
, struct thread_info
*tp
)
1179 /* If we don't have trace, there's nothing to do. */
1180 if (btrace_data_empty (btrace
))
1183 switch (btrace
->format
)
1185 case BTRACE_FORMAT_NONE
:
1188 case BTRACE_FORMAT_BTS
:
1189 return btrace_stitch_bts (&btrace
->variant
.bts
, tp
);
1191 case BTRACE_FORMAT_PT
:
1192 /* Delta reads are not supported. */
1196 internal_error (__FILE__
, __LINE__
, _("Unkown branch trace format."));
1199 /* Clear the branch trace histories in BTINFO. */
1202 btrace_clear_history (struct btrace_thread_info
*btinfo
)
1204 xfree (btinfo
->insn_history
);
1205 xfree (btinfo
->call_history
);
1206 xfree (btinfo
->replay
);
1208 btinfo
->insn_history
= NULL
;
1209 btinfo
->call_history
= NULL
;
1210 btinfo
->replay
= NULL
;
1213 /* Clear the branch trace maintenance histories in BTINFO. */
1216 btrace_maint_clear (struct btrace_thread_info
*btinfo
)
1218 switch (btinfo
->data
.format
)
1223 case BTRACE_FORMAT_BTS
:
1224 btinfo
->maint
.variant
.bts
.packet_history
.begin
= 0;
1225 btinfo
->maint
.variant
.bts
.packet_history
.end
= 0;
1228 #if defined (HAVE_LIBIPT)
1229 case BTRACE_FORMAT_PT
:
1230 xfree (btinfo
->maint
.variant
.pt
.packets
);
1232 btinfo
->maint
.variant
.pt
.packets
= NULL
;
1233 btinfo
->maint
.variant
.pt
.packet_history
.begin
= 0;
1234 btinfo
->maint
.variant
.pt
.packet_history
.end
= 0;
1236 #endif /* defined (HAVE_LIBIPT) */
1243 btrace_fetch (struct thread_info
*tp
)
1245 struct btrace_thread_info
*btinfo
;
1246 struct btrace_target_info
*tinfo
;
1247 struct btrace_data btrace
;
1248 struct cleanup
*cleanup
;
1251 DEBUG ("fetch thread %d (%s)", tp
->num
, target_pid_to_str (tp
->ptid
));
1253 btinfo
= &tp
->btrace
;
1254 tinfo
= btinfo
->target
;
1258 /* There's no way we could get new trace while replaying.
1259 On the other hand, delta trace would return a partial record with the
1260 current PC, which is the replay PC, not the last PC, as expected. */
1261 if (btinfo
->replay
!= NULL
)
1264 btrace_data_init (&btrace
);
1265 cleanup
= make_cleanup_btrace_data (&btrace
);
1267 /* Let's first try to extend the trace we already have. */
1268 if (btinfo
->end
!= NULL
)
1270 errcode
= target_read_btrace (&btrace
, tinfo
, BTRACE_READ_DELTA
);
1273 /* Success. Let's try to stitch the traces together. */
1274 errcode
= btrace_stitch_trace (&btrace
, tp
);
1278 /* We failed to read delta trace. Let's try to read new trace. */
1279 errcode
= target_read_btrace (&btrace
, tinfo
, BTRACE_READ_NEW
);
1281 /* If we got any new trace, discard what we have. */
1282 if (errcode
== 0 && !btrace_data_empty (&btrace
))
1286 /* If we were not able to read the trace, we start over. */
1290 errcode
= target_read_btrace (&btrace
, tinfo
, BTRACE_READ_ALL
);
1294 errcode
= target_read_btrace (&btrace
, tinfo
, BTRACE_READ_ALL
);
1296 /* If we were not able to read the branch trace, signal an error. */
1298 error (_("Failed to read branch trace."));
1300 /* Compute the trace, provided we have any. */
1301 if (!btrace_data_empty (&btrace
))
1303 /* Store the raw trace data. The stored data will be cleared in
1304 btrace_clear, so we always append the new trace. */
1305 btrace_data_append (&btinfo
->data
, &btrace
);
1306 btrace_maint_clear (btinfo
);
1308 btrace_clear_history (btinfo
);
1309 btrace_compute_ftrace (tp
, &btrace
);
1312 do_cleanups (cleanup
);
1318 btrace_clear (struct thread_info
*tp
)
1320 struct btrace_thread_info
*btinfo
;
1321 struct btrace_function
*it
, *trash
;
1323 DEBUG ("clear thread %d (%s)", tp
->num
, target_pid_to_str (tp
->ptid
));
1325 /* Make sure btrace frames that may hold a pointer into the branch
1326 trace data are destroyed. */
1327 reinit_frame_cache ();
1329 btinfo
= &tp
->btrace
;
1340 btinfo
->begin
= NULL
;
1344 /* Must clear the maint data before - it depends on BTINFO->DATA. */
1345 btrace_maint_clear (btinfo
);
1346 btrace_data_clear (&btinfo
->data
);
1347 btrace_clear_history (btinfo
);
1353 btrace_free_objfile (struct objfile
*objfile
)
1355 struct thread_info
*tp
;
1357 DEBUG ("free objfile");
1359 ALL_NON_EXITED_THREADS (tp
)
1363 #if defined (HAVE_LIBEXPAT)
1365 /* Check the btrace document version. */
1368 check_xml_btrace_version (struct gdb_xml_parser
*parser
,
1369 const struct gdb_xml_element
*element
,
1370 void *user_data
, VEC (gdb_xml_value_s
) *attributes
)
1372 const char *version
= xml_find_attribute (attributes
, "version")->value
;
1374 if (strcmp (version
, "1.0") != 0)
1375 gdb_xml_error (parser
, _("Unsupported btrace version: \"%s\""), version
);
1378 /* Parse a btrace "block" xml record. */
1381 parse_xml_btrace_block (struct gdb_xml_parser
*parser
,
1382 const struct gdb_xml_element
*element
,
1383 void *user_data
, VEC (gdb_xml_value_s
) *attributes
)
1385 struct btrace_data
*btrace
;
1386 struct btrace_block
*block
;
1387 ULONGEST
*begin
, *end
;
1391 switch (btrace
->format
)
1393 case BTRACE_FORMAT_BTS
:
1396 case BTRACE_FORMAT_NONE
:
1397 btrace
->format
= BTRACE_FORMAT_BTS
;
1398 btrace
->variant
.bts
.blocks
= NULL
;
1402 gdb_xml_error (parser
, _("Btrace format error."));
1405 begin
= xml_find_attribute (attributes
, "begin")->value
;
1406 end
= xml_find_attribute (attributes
, "end")->value
;
1408 block
= VEC_safe_push (btrace_block_s
, btrace
->variant
.bts
.blocks
, NULL
);
1409 block
->begin
= *begin
;
1413 /* Parse a "raw" xml record. */
1416 parse_xml_raw (struct gdb_xml_parser
*parser
, const char *body_text
,
1417 gdb_byte
**pdata
, size_t *psize
)
1419 struct cleanup
*cleanup
;
1420 gdb_byte
*data
, *bin
;
1423 len
= strlen (body_text
);
1425 gdb_xml_error (parser
, _("Bad raw data size."));
1429 bin
= data
= xmalloc (size
);
1430 cleanup
= make_cleanup (xfree
, data
);
1432 /* We use hex encoding - see common/rsp-low.h. */
1440 if (hi
== 0 || lo
== 0)
1441 gdb_xml_error (parser
, _("Bad hex encoding."));
1443 *bin
++ = fromhex (hi
) * 16 + fromhex (lo
);
1447 discard_cleanups (cleanup
);
1453 /* Parse a btrace pt-config "cpu" xml record. */
1456 parse_xml_btrace_pt_config_cpu (struct gdb_xml_parser
*parser
,
1457 const struct gdb_xml_element
*element
,
1459 VEC (gdb_xml_value_s
) *attributes
)
1461 struct btrace_data
*btrace
;
1463 ULONGEST
*family
, *model
, *stepping
;
1465 vendor
= xml_find_attribute (attributes
, "vendor")->value
;
1466 family
= xml_find_attribute (attributes
, "family")->value
;
1467 model
= xml_find_attribute (attributes
, "model")->value
;
1468 stepping
= xml_find_attribute (attributes
, "stepping")->value
;
1472 if (strcmp (vendor
, "GenuineIntel") == 0)
1473 btrace
->variant
.pt
.config
.cpu
.vendor
= CV_INTEL
;
1475 btrace
->variant
.pt
.config
.cpu
.family
= *family
;
1476 btrace
->variant
.pt
.config
.cpu
.model
= *model
;
1477 btrace
->variant
.pt
.config
.cpu
.stepping
= *stepping
;
1480 /* Parse a btrace pt "raw" xml record. */
1483 parse_xml_btrace_pt_raw (struct gdb_xml_parser
*parser
,
1484 const struct gdb_xml_element
*element
,
1485 void *user_data
, const char *body_text
)
1487 struct btrace_data
*btrace
;
1490 parse_xml_raw (parser
, body_text
, &btrace
->variant
.pt
.data
,
1491 &btrace
->variant
.pt
.size
);
1494 /* Parse a btrace "pt" xml record. */
1497 parse_xml_btrace_pt (struct gdb_xml_parser
*parser
,
1498 const struct gdb_xml_element
*element
,
1499 void *user_data
, VEC (gdb_xml_value_s
) *attributes
)
1501 struct btrace_data
*btrace
;
1504 btrace
->format
= BTRACE_FORMAT_PT
;
1505 btrace
->variant
.pt
.config
.cpu
.vendor
= CV_UNKNOWN
;
1506 btrace
->variant
.pt
.data
= NULL
;
1507 btrace
->variant
.pt
.size
= 0;
1510 static const struct gdb_xml_attribute block_attributes
[] = {
1511 { "begin", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
1512 { "end", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
1513 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
1516 static const struct gdb_xml_attribute btrace_pt_config_cpu_attributes
[] = {
1517 { "vendor", GDB_XML_AF_NONE
, NULL
, NULL
},
1518 { "family", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
1519 { "model", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
1520 { "stepping", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
1521 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
1524 static const struct gdb_xml_element btrace_pt_config_children
[] = {
1525 { "cpu", btrace_pt_config_cpu_attributes
, NULL
, GDB_XML_EF_OPTIONAL
,
1526 parse_xml_btrace_pt_config_cpu
, NULL
},
1527 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
1530 static const struct gdb_xml_element btrace_pt_children
[] = {
1531 { "pt-config", NULL
, btrace_pt_config_children
, GDB_XML_EF_OPTIONAL
, NULL
,
1533 { "raw", NULL
, NULL
, GDB_XML_EF_OPTIONAL
, NULL
, parse_xml_btrace_pt_raw
},
1534 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
1537 static const struct gdb_xml_attribute btrace_attributes
[] = {
1538 { "version", GDB_XML_AF_NONE
, NULL
, NULL
},
1539 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
1542 static const struct gdb_xml_element btrace_children
[] = {
1543 { "block", block_attributes
, NULL
,
1544 GDB_XML_EF_REPEATABLE
| GDB_XML_EF_OPTIONAL
, parse_xml_btrace_block
, NULL
},
1545 { "pt", NULL
, btrace_pt_children
, GDB_XML_EF_OPTIONAL
, parse_xml_btrace_pt
,
1547 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
1550 static const struct gdb_xml_element btrace_elements
[] = {
1551 { "btrace", btrace_attributes
, btrace_children
, GDB_XML_EF_NONE
,
1552 check_xml_btrace_version
, NULL
},
1553 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
1556 #endif /* defined (HAVE_LIBEXPAT) */
1561 parse_xml_btrace (struct btrace_data
*btrace
, const char *buffer
)
1563 struct cleanup
*cleanup
;
1566 #if defined (HAVE_LIBEXPAT)
1568 btrace
->format
= BTRACE_FORMAT_NONE
;
1570 cleanup
= make_cleanup_btrace_data (btrace
);
1571 errcode
= gdb_xml_parse_quick (_("btrace"), "btrace.dtd", btrace_elements
,
1574 error (_("Error parsing branch trace."));
1576 /* Keep parse results. */
1577 discard_cleanups (cleanup
);
1579 #else /* !defined (HAVE_LIBEXPAT) */
1581 error (_("Cannot process branch trace. XML parsing is not supported."));
1583 #endif /* !defined (HAVE_LIBEXPAT) */
1586 #if defined (HAVE_LIBEXPAT)
1588 /* Parse a btrace-conf "bts" xml record. */
1591 parse_xml_btrace_conf_bts (struct gdb_xml_parser
*parser
,
1592 const struct gdb_xml_element
*element
,
1593 void *user_data
, VEC (gdb_xml_value_s
) *attributes
)
1595 struct btrace_config
*conf
;
1596 struct gdb_xml_value
*size
;
1599 conf
->format
= BTRACE_FORMAT_BTS
;
1602 size
= xml_find_attribute (attributes
, "size");
1604 conf
->bts
.size
= (unsigned int) *(ULONGEST
*) size
->value
;
1607 /* Parse a btrace-conf "pt" xml record. */
1610 parse_xml_btrace_conf_pt (struct gdb_xml_parser
*parser
,
1611 const struct gdb_xml_element
*element
,
1612 void *user_data
, VEC (gdb_xml_value_s
) *attributes
)
1614 struct btrace_config
*conf
;
1615 struct gdb_xml_value
*size
;
1618 conf
->format
= BTRACE_FORMAT_PT
;
1621 size
= xml_find_attribute (attributes
, "size");
1623 conf
->pt
.size
= (unsigned int) *(ULONGEST
*) size
->value
;
1626 static const struct gdb_xml_attribute btrace_conf_pt_attributes
[] = {
1627 { "size", GDB_XML_AF_OPTIONAL
, gdb_xml_parse_attr_ulongest
, NULL
},
1628 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
1631 static const struct gdb_xml_attribute btrace_conf_bts_attributes
[] = {
1632 { "size", GDB_XML_AF_OPTIONAL
, gdb_xml_parse_attr_ulongest
, NULL
},
1633 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
1636 static const struct gdb_xml_element btrace_conf_children
[] = {
1637 { "bts", btrace_conf_bts_attributes
, NULL
, GDB_XML_EF_OPTIONAL
,
1638 parse_xml_btrace_conf_bts
, NULL
},
1639 { "pt", btrace_conf_pt_attributes
, NULL
, GDB_XML_EF_OPTIONAL
,
1640 parse_xml_btrace_conf_pt
, NULL
},
1641 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
1644 static const struct gdb_xml_attribute btrace_conf_attributes
[] = {
1645 { "version", GDB_XML_AF_NONE
, NULL
, NULL
},
1646 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
1649 static const struct gdb_xml_element btrace_conf_elements
[] = {
1650 { "btrace-conf", btrace_conf_attributes
, btrace_conf_children
,
1651 GDB_XML_EF_NONE
, NULL
, NULL
},
1652 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
1655 #endif /* defined (HAVE_LIBEXPAT) */
1660 parse_xml_btrace_conf (struct btrace_config
*conf
, const char *xml
)
1664 #if defined (HAVE_LIBEXPAT)
1666 errcode
= gdb_xml_parse_quick (_("btrace-conf"), "btrace-conf.dtd",
1667 btrace_conf_elements
, xml
, conf
);
1669 error (_("Error parsing branch trace configuration."));
1671 #else /* !defined (HAVE_LIBEXPAT) */
1673 error (_("XML parsing is not supported."));
1675 #endif /* !defined (HAVE_LIBEXPAT) */
1680 const struct btrace_insn
*
1681 btrace_insn_get (const struct btrace_insn_iterator
*it
)
1683 const struct btrace_function
*bfun
;
1684 unsigned int index
, end
;
1687 bfun
= it
->function
;
1689 /* Check if the iterator points to a gap in the trace. */
1690 if (bfun
->errcode
!= 0)
1693 /* The index is within the bounds of this function's instruction vector. */
1694 end
= VEC_length (btrace_insn_s
, bfun
->insn
);
1695 gdb_assert (0 < end
);
1696 gdb_assert (index
< end
);
1698 return VEC_index (btrace_insn_s
, bfun
->insn
, index
);
1704 btrace_insn_number (const struct btrace_insn_iterator
*it
)
1706 const struct btrace_function
*bfun
;
1708 bfun
= it
->function
;
1710 /* Return zero if the iterator points to a gap in the trace. */
1711 if (bfun
->errcode
!= 0)
1714 return bfun
->insn_offset
+ it
->index
;
1720 btrace_insn_begin (struct btrace_insn_iterator
*it
,
1721 const struct btrace_thread_info
*btinfo
)
1723 const struct btrace_function
*bfun
;
1725 bfun
= btinfo
->begin
;
1727 error (_("No trace."));
1729 it
->function
= bfun
;
1736 btrace_insn_end (struct btrace_insn_iterator
*it
,
1737 const struct btrace_thread_info
*btinfo
)
1739 const struct btrace_function
*bfun
;
1740 unsigned int length
;
1744 error (_("No trace."));
1746 length
= VEC_length (btrace_insn_s
, bfun
->insn
);
1748 /* The last function may either be a gap or it contains the current
1749 instruction, which is one past the end of the execution trace; ignore
1754 it
->function
= bfun
;
1761 btrace_insn_next (struct btrace_insn_iterator
*it
, unsigned int stride
)
1763 const struct btrace_function
*bfun
;
1764 unsigned int index
, steps
;
1766 bfun
= it
->function
;
1772 unsigned int end
, space
, adv
;
1774 end
= VEC_length (btrace_insn_s
, bfun
->insn
);
1776 /* An empty function segment represents a gap in the trace. We count
1777 it as one instruction. */
1780 const struct btrace_function
*next
;
1782 next
= bfun
->flow
.next
;
1795 gdb_assert (0 < end
);
1796 gdb_assert (index
< end
);
1798 /* Compute the number of instructions remaining in this segment. */
1799 space
= end
- index
;
1801 /* Advance the iterator as far as possible within this segment. */
1802 adv
= min (space
, stride
);
1807 /* Move to the next function if we're at the end of this one. */
1810 const struct btrace_function
*next
;
1812 next
= bfun
->flow
.next
;
1815 /* We stepped past the last function.
1817 Let's adjust the index to point to the last instruction in
1818 the previous function. */
1824 /* We now point to the first instruction in the new function. */
1829 /* We did make progress. */
1830 gdb_assert (adv
> 0);
1833 /* Update the iterator. */
1834 it
->function
= bfun
;
1843 btrace_insn_prev (struct btrace_insn_iterator
*it
, unsigned int stride
)
1845 const struct btrace_function
*bfun
;
1846 unsigned int index
, steps
;
1848 bfun
= it
->function
;
1856 /* Move to the previous function if we're at the start of this one. */
1859 const struct btrace_function
*prev
;
1861 prev
= bfun
->flow
.prev
;
1865 /* We point to one after the last instruction in the new function. */
1867 index
= VEC_length (btrace_insn_s
, bfun
->insn
);
1869 /* An empty function segment represents a gap in the trace. We count
1870 it as one instruction. */
1880 /* Advance the iterator as far as possible within this segment. */
1881 adv
= min (index
, stride
);
1887 /* We did make progress. */
1888 gdb_assert (adv
> 0);
1891 /* Update the iterator. */
1892 it
->function
= bfun
;
1901 btrace_insn_cmp (const struct btrace_insn_iterator
*lhs
,
1902 const struct btrace_insn_iterator
*rhs
)
1904 unsigned int lnum
, rnum
;
1906 lnum
= btrace_insn_number (lhs
);
1907 rnum
= btrace_insn_number (rhs
);
1909 /* A gap has an instruction number of zero. Things are getting more
1910 complicated if gaps are involved.
1912 We take the instruction number offset from the iterator's function.
1913 This is the number of the first instruction after the gap.
1915 This is OK as long as both lhs and rhs point to gaps. If only one of
1916 them does, we need to adjust the number based on the other's regular
1917 instruction number. Otherwise, a gap might compare equal to an
1920 if (lnum
== 0 && rnum
== 0)
1922 lnum
= lhs
->function
->insn_offset
;
1923 rnum
= rhs
->function
->insn_offset
;
1927 lnum
= lhs
->function
->insn_offset
;
1934 rnum
= rhs
->function
->insn_offset
;
1940 return (int) (lnum
- rnum
);
1946 btrace_find_insn_by_number (struct btrace_insn_iterator
*it
,
1947 const struct btrace_thread_info
*btinfo
,
1948 unsigned int number
)
1950 const struct btrace_function
*bfun
;
1951 unsigned int end
, length
;
1953 for (bfun
= btinfo
->end
; bfun
!= NULL
; bfun
= bfun
->flow
.prev
)
1956 if (bfun
->errcode
!= 0)
1959 if (bfun
->insn_offset
<= number
)
1966 length
= VEC_length (btrace_insn_s
, bfun
->insn
);
1967 gdb_assert (length
> 0);
1969 end
= bfun
->insn_offset
+ length
;
1973 it
->function
= bfun
;
1974 it
->index
= number
- bfun
->insn_offset
;
1981 const struct btrace_function
*
1982 btrace_call_get (const struct btrace_call_iterator
*it
)
1984 return it
->function
;
1990 btrace_call_number (const struct btrace_call_iterator
*it
)
1992 const struct btrace_thread_info
*btinfo
;
1993 const struct btrace_function
*bfun
;
1996 btinfo
= it
->btinfo
;
1997 bfun
= it
->function
;
1999 return bfun
->number
;
2001 /* For the end iterator, i.e. bfun == NULL, we return one more than the
2002 number of the last function. */
2004 insns
= VEC_length (btrace_insn_s
, bfun
->insn
);
2006 /* If the function contains only a single instruction (i.e. the current
2007 instruction), it will be skipped and its number is already the number
2010 return bfun
->number
;
2012 /* Otherwise, return one more than the number of the last function. */
2013 return bfun
->number
+ 1;
2019 btrace_call_begin (struct btrace_call_iterator
*it
,
2020 const struct btrace_thread_info
*btinfo
)
2022 const struct btrace_function
*bfun
;
2024 bfun
= btinfo
->begin
;
2026 error (_("No trace."));
2028 it
->btinfo
= btinfo
;
2029 it
->function
= bfun
;
2035 btrace_call_end (struct btrace_call_iterator
*it
,
2036 const struct btrace_thread_info
*btinfo
)
2038 const struct btrace_function
*bfun
;
2042 error (_("No trace."));
2044 it
->btinfo
= btinfo
;
2045 it
->function
= NULL
;
2051 btrace_call_next (struct btrace_call_iterator
*it
, unsigned int stride
)
2053 const struct btrace_function
*bfun
;
2056 bfun
= it
->function
;
2058 while (bfun
!= NULL
)
2060 const struct btrace_function
*next
;
2063 next
= bfun
->flow
.next
;
2066 /* Ignore the last function if it only contains a single
2067 (i.e. the current) instruction. */
2068 insns
= VEC_length (btrace_insn_s
, bfun
->insn
);
2073 if (stride
== steps
)
2080 it
->function
= bfun
;
2087 btrace_call_prev (struct btrace_call_iterator
*it
, unsigned int stride
)
2089 const struct btrace_thread_info
*btinfo
;
2090 const struct btrace_function
*bfun
;
2093 bfun
= it
->function
;
2100 btinfo
= it
->btinfo
;
2105 /* Ignore the last function if it only contains a single
2106 (i.e. the current) instruction. */
2107 insns
= VEC_length (btrace_insn_s
, bfun
->insn
);
2109 bfun
= bfun
->flow
.prev
;
2117 while (steps
< stride
)
2119 const struct btrace_function
*prev
;
2121 prev
= bfun
->flow
.prev
;
2129 it
->function
= bfun
;
2136 btrace_call_cmp (const struct btrace_call_iterator
*lhs
,
2137 const struct btrace_call_iterator
*rhs
)
2139 unsigned int lnum
, rnum
;
2141 lnum
= btrace_call_number (lhs
);
2142 rnum
= btrace_call_number (rhs
);
2144 return (int) (lnum
- rnum
);
2150 btrace_find_call_by_number (struct btrace_call_iterator
*it
,
2151 const struct btrace_thread_info
*btinfo
,
2152 unsigned int number
)
2154 const struct btrace_function
*bfun
;
2156 for (bfun
= btinfo
->end
; bfun
!= NULL
; bfun
= bfun
->flow
.prev
)
2160 bnum
= bfun
->number
;
2163 it
->btinfo
= btinfo
;
2164 it
->function
= bfun
;
2168 /* Functions are ordered and numbered consecutively. We could bail out
2169 earlier. On the other hand, it is very unlikely that we search for
2170 a nonexistent function. */
2179 btrace_set_insn_history (struct btrace_thread_info
*btinfo
,
2180 const struct btrace_insn_iterator
*begin
,
2181 const struct btrace_insn_iterator
*end
)
2183 if (btinfo
->insn_history
== NULL
)
2184 btinfo
->insn_history
= xzalloc (sizeof (*btinfo
->insn_history
));
2186 btinfo
->insn_history
->begin
= *begin
;
2187 btinfo
->insn_history
->end
= *end
;
2193 btrace_set_call_history (struct btrace_thread_info
*btinfo
,
2194 const struct btrace_call_iterator
*begin
,
2195 const struct btrace_call_iterator
*end
)
2197 gdb_assert (begin
->btinfo
== end
->btinfo
);
2199 if (btinfo
->call_history
== NULL
)
2200 btinfo
->call_history
= xzalloc (sizeof (*btinfo
->call_history
));
2202 btinfo
->call_history
->begin
= *begin
;
2203 btinfo
->call_history
->end
= *end
;
2209 btrace_is_replaying (struct thread_info
*tp
)
2211 return tp
->btrace
.replay
!= NULL
;
2217 btrace_is_empty (struct thread_info
*tp
)
2219 struct btrace_insn_iterator begin
, end
;
2220 struct btrace_thread_info
*btinfo
;
2222 btinfo
= &tp
->btrace
;
2224 if (btinfo
->begin
== NULL
)
2227 btrace_insn_begin (&begin
, btinfo
);
2228 btrace_insn_end (&end
, btinfo
);
2230 return btrace_insn_cmp (&begin
, &end
) == 0;
2233 /* Forward the cleanup request. */
2236 do_btrace_data_cleanup (void *arg
)
2238 btrace_data_fini (arg
);
2244 make_cleanup_btrace_data (struct btrace_data
*data
)
2246 return make_cleanup (do_btrace_data_cleanup
, data
);
2249 #if defined (HAVE_LIBIPT)
2251 /* Print a single packet. */
2254 pt_print_packet (const struct pt_packet
*packet
)
2256 switch (packet
->type
)
2259 printf_unfiltered (("[??: %x]"), packet
->type
);
2263 printf_unfiltered (("psb"));
2267 printf_unfiltered (("psbend"));
2271 printf_unfiltered (("pad"));
2275 printf_unfiltered (("tip %u: 0x%" PRIx64
""),
2276 packet
->payload
.ip
.ipc
,
2277 packet
->payload
.ip
.ip
);
2281 printf_unfiltered (("tip.pge %u: 0x%" PRIx64
""),
2282 packet
->payload
.ip
.ipc
,
2283 packet
->payload
.ip
.ip
);
2287 printf_unfiltered (("tip.pgd %u: 0x%" PRIx64
""),
2288 packet
->payload
.ip
.ipc
,
2289 packet
->payload
.ip
.ip
);
2293 printf_unfiltered (("fup %u: 0x%" PRIx64
""),
2294 packet
->payload
.ip
.ipc
,
2295 packet
->payload
.ip
.ip
);
2299 printf_unfiltered (("tnt-8 %u: 0x%" PRIx64
""),
2300 packet
->payload
.tnt
.bit_size
,
2301 packet
->payload
.tnt
.payload
);
2305 printf_unfiltered (("tnt-64 %u: 0x%" PRIx64
""),
2306 packet
->payload
.tnt
.bit_size
,
2307 packet
->payload
.tnt
.payload
);
2311 printf_unfiltered (("pip %" PRIx64
"%s"), packet
->payload
.pip
.cr3
,
2312 packet
->payload
.pip
.nr
? (" nr") : (""));
2316 printf_unfiltered (("tsc %" PRIx64
""), packet
->payload
.tsc
.tsc
);
2320 printf_unfiltered (("cbr %u"), packet
->payload
.cbr
.ratio
);
2324 switch (packet
->payload
.mode
.leaf
)
2327 printf_unfiltered (("mode %u"), packet
->payload
.mode
.leaf
);
2331 printf_unfiltered (("mode.exec%s%s"),
2332 packet
->payload
.mode
.bits
.exec
.csl
2334 packet
->payload
.mode
.bits
.exec
.csd
2335 ? (" cs.d") : (""));
2339 printf_unfiltered (("mode.tsx%s%s"),
2340 packet
->payload
.mode
.bits
.tsx
.intx
2342 packet
->payload
.mode
.bits
.tsx
.abrt
2343 ? (" abrt") : (""));
2349 printf_unfiltered (("ovf"));
2353 printf_unfiltered (("stop"));
2357 printf_unfiltered (("vmcs %" PRIx64
""), packet
->payload
.vmcs
.base
);
2361 printf_unfiltered (("tma %x %x"), packet
->payload
.tma
.ctc
,
2362 packet
->payload
.tma
.fc
);
2366 printf_unfiltered (("mtc %x"), packet
->payload
.mtc
.ctc
);
2370 printf_unfiltered (("cyc %" PRIx64
""), packet
->payload
.cyc
.value
);
2374 printf_unfiltered (("mnt %" PRIx64
""), packet
->payload
.mnt
.payload
);
2379 /* Decode packets into MAINT using DECODER. */
2382 btrace_maint_decode_pt (struct btrace_maint_info
*maint
,
2383 struct pt_packet_decoder
*decoder
)
2389 struct btrace_pt_packet packet
;
2391 errcode
= pt_pkt_sync_forward (decoder
);
2397 pt_pkt_get_offset (decoder
, &packet
.offset
);
2399 errcode
= pt_pkt_next (decoder
, &packet
.packet
,
2400 sizeof(packet
.packet
));
2404 if (maint_btrace_pt_skip_pad
== 0 || packet
.packet
.type
!= ppt_pad
)
2406 packet
.errcode
= pt_errcode (errcode
);
2407 VEC_safe_push (btrace_pt_packet_s
, maint
->variant
.pt
.packets
,
2412 if (errcode
== -pte_eos
)
2415 packet
.errcode
= pt_errcode (errcode
);
2416 VEC_safe_push (btrace_pt_packet_s
, maint
->variant
.pt
.packets
,
2419 warning (_("Error at trace offset 0x%" PRIx64
": %s."),
2420 packet
.offset
, pt_errstr (packet
.errcode
));
2423 if (errcode
!= -pte_eos
)
2424 warning (_("Failed to synchronize onto the Intel(R) Processor Trace "
2425 "stream: %s."), pt_errstr (pt_errcode (errcode
)));
2428 /* Update the packet history in BTINFO. */
2431 btrace_maint_update_pt_packets (struct btrace_thread_info
*btinfo
)
2433 volatile struct gdb_exception except
;
2434 struct pt_packet_decoder
*decoder
;
2435 struct btrace_data_pt
*pt
;
2436 struct pt_config config
;
2439 pt
= &btinfo
->data
.variant
.pt
;
2441 /* Nothing to do if there is no trace. */
2445 memset (&config
, 0, sizeof(config
));
2447 config
.size
= sizeof (config
);
2448 config
.begin
= pt
->data
;
2449 config
.end
= pt
->data
+ pt
->size
;
2451 config
.cpu
.vendor
= pt_translate_cpu_vendor (pt
->config
.cpu
.vendor
);
2452 config
.cpu
.family
= pt
->config
.cpu
.family
;
2453 config
.cpu
.model
= pt
->config
.cpu
.model
;
2454 config
.cpu
.stepping
= pt
->config
.cpu
.stepping
;
2456 errcode
= pt_cpu_errata (&config
.errata
, &config
.cpu
);
2458 error (_("Failed to configure the Intel(R) Processor Trace decoder: %s."),
2459 pt_errstr (pt_errcode (errcode
)));
2461 decoder
= pt_pkt_alloc_decoder (&config
);
2462 if (decoder
== NULL
)
2463 error (_("Failed to allocate the Intel(R) Processor Trace decoder."));
2467 btrace_maint_decode_pt (&btinfo
->maint
, decoder
);
2469 CATCH (except
, RETURN_MASK_ALL
)
2471 pt_pkt_free_decoder (decoder
);
2473 if (except
.reason
< 0)
2474 throw_exception (except
);
2478 pt_pkt_free_decoder (decoder
);
2481 #endif /* !defined (HAVE_LIBIPT) */
2483 /* Update the packet maintenance information for BTINFO and store the
2484 low and high bounds into BEGIN and END, respectively.
2485 Store the current iterator state into FROM and TO. */
2488 btrace_maint_update_packets (struct btrace_thread_info
*btinfo
,
2489 unsigned int *begin
, unsigned int *end
,
2490 unsigned int *from
, unsigned int *to
)
2492 switch (btinfo
->data
.format
)
2501 case BTRACE_FORMAT_BTS
:
2502 /* Nothing to do - we operate directly on BTINFO->DATA. */
2504 *end
= VEC_length (btrace_block_s
, btinfo
->data
.variant
.bts
.blocks
);
2505 *from
= btinfo
->maint
.variant
.bts
.packet_history
.begin
;
2506 *to
= btinfo
->maint
.variant
.bts
.packet_history
.end
;
2509 #if defined (HAVE_LIBIPT)
2510 case BTRACE_FORMAT_PT
:
2511 if (VEC_empty (btrace_pt_packet_s
, btinfo
->maint
.variant
.pt
.packets
))
2512 btrace_maint_update_pt_packets (btinfo
);
2515 *end
= VEC_length (btrace_pt_packet_s
, btinfo
->maint
.variant
.pt
.packets
);
2516 *from
= btinfo
->maint
.variant
.pt
.packet_history
.begin
;
2517 *to
= btinfo
->maint
.variant
.pt
.packet_history
.end
;
2519 #endif /* defined (HAVE_LIBIPT) */
2523 /* Print packets in BTINFO from BEGIN (inclusive) until END (exclusive) and
2524 update the current iterator position. */
2527 btrace_maint_print_packets (struct btrace_thread_info
*btinfo
,
2528 unsigned int begin
, unsigned int end
)
2530 switch (btinfo
->data
.format
)
2535 case BTRACE_FORMAT_BTS
:
2537 VEC (btrace_block_s
) *blocks
;
2540 blocks
= btinfo
->data
.variant
.bts
.blocks
;
2541 for (blk
= begin
; blk
< end
; ++blk
)
2543 const btrace_block_s
*block
;
2545 block
= VEC_index (btrace_block_s
, blocks
, blk
);
2547 printf_unfiltered ("%u\tbegin: %s, end: %s\n", blk
,
2548 core_addr_to_string_nz (block
->begin
),
2549 core_addr_to_string_nz (block
->end
));
2552 btinfo
->maint
.variant
.bts
.packet_history
.begin
= begin
;
2553 btinfo
->maint
.variant
.bts
.packet_history
.end
= end
;
2557 #if defined (HAVE_LIBIPT)
2558 case BTRACE_FORMAT_PT
:
2560 VEC (btrace_pt_packet_s
) *packets
;
2563 packets
= btinfo
->maint
.variant
.pt
.packets
;
2564 for (pkt
= begin
; pkt
< end
; ++pkt
)
2566 const struct btrace_pt_packet
*packet
;
2568 packet
= VEC_index (btrace_pt_packet_s
, packets
, pkt
);
2570 printf_unfiltered ("%u\t", pkt
);
2571 printf_unfiltered ("0x%" PRIx64
"\t", packet
->offset
);
2573 if (packet
->errcode
== pte_ok
)
2574 pt_print_packet (&packet
->packet
);
2576 printf_unfiltered ("[error: %s]", pt_errstr (packet
->errcode
));
2578 printf_unfiltered ("\n");
2581 btinfo
->maint
.variant
.pt
.packet_history
.begin
= begin
;
2582 btinfo
->maint
.variant
.pt
.packet_history
.end
= end
;
2585 #endif /* defined (HAVE_LIBIPT) */
2589 /* Read a number from an argument string. */
2592 get_uint (char **arg
)
2594 char *begin
, *end
, *pos
;
2595 unsigned long number
;
2598 pos
= skip_spaces (begin
);
2600 if (!isdigit (*pos
))
2601 error (_("Expected positive number, got: %s."), pos
);
2603 number
= strtoul (pos
, &end
, 10);
2604 if (number
> UINT_MAX
)
2605 error (_("Number too big."));
2607 *arg
+= (end
- begin
);
2609 return (unsigned int) number
;
2612 /* Read a context size from an argument string. */
2615 get_context_size (char **arg
)
2620 pos
= skip_spaces (*arg
);
2622 if (!isdigit (*pos
))
2623 error (_("Expected positive number, got: %s."), pos
);
2625 return strtol (pos
, arg
, 10);
2628 /* Complain about junk at the end of an argument string. */
2631 no_chunk (char *arg
)
2634 error (_("Junk after argument: %s."), arg
);
2637 /* The "maintenance btrace packet-history" command. */
2640 maint_btrace_packet_history_cmd (char *arg
, int from_tty
)
2642 struct btrace_thread_info
*btinfo
;
2643 struct thread_info
*tp
;
2644 unsigned int size
, begin
, end
, from
, to
;
2646 tp
= find_thread_ptid (inferior_ptid
);
2648 error (_("No thread."));
2651 btinfo
= &tp
->btrace
;
2653 btrace_maint_update_packets (btinfo
, &begin
, &end
, &from
, &to
);
2656 printf_unfiltered (_("No trace.\n"));
2660 if (arg
== NULL
|| *arg
== 0 || strcmp (arg
, "+") == 0)
2664 if (end
- from
< size
)
2668 else if (strcmp (arg
, "-") == 0)
2672 if (to
- begin
< size
)
2678 from
= get_uint (&arg
);
2680 error (_("'%u' is out of range."), from
);
2682 arg
= skip_spaces (arg
);
2685 arg
= skip_spaces (++arg
);
2690 size
= get_context_size (&arg
);
2694 if (end
- from
< size
)
2698 else if (*arg
== '-')
2701 size
= get_context_size (&arg
);
2705 /* Include the packet given as first argument. */
2709 if (to
- begin
< size
)
2715 to
= get_uint (&arg
);
2717 /* Include the packet at the second argument and silently
2718 truncate the range. */
2731 if (end
- from
< size
)
2739 btrace_maint_print_packets (btinfo
, from
, to
);
2742 /* The "maintenance btrace clear-packet-history" command. */
2745 maint_btrace_clear_packet_history_cmd (char *args
, int from_tty
)
2747 struct btrace_thread_info
*btinfo
;
2748 struct thread_info
*tp
;
2750 if (args
!= NULL
&& *args
!= 0)
2751 error (_("Invalid argument."));
2753 tp
= find_thread_ptid (inferior_ptid
);
2755 error (_("No thread."));
2757 btinfo
= &tp
->btrace
;
2759 /* Must clear the maint data before - it depends on BTINFO->DATA. */
2760 btrace_maint_clear (btinfo
);
2761 btrace_data_clear (&btinfo
->data
);
2764 /* The "maintenance btrace clear" command. */
2767 maint_btrace_clear_cmd (char *args
, int from_tty
)
2769 struct btrace_thread_info
*btinfo
;
2770 struct thread_info
*tp
;
2772 if (args
!= NULL
&& *args
!= 0)
2773 error (_("Invalid argument."));
2775 tp
= find_thread_ptid (inferior_ptid
);
2777 error (_("No thread."));
2782 /* The "maintenance btrace" command. */
2785 maint_btrace_cmd (char *args
, int from_tty
)
2787 help_list (maint_btrace_cmdlist
, "maintenance btrace ", all_commands
,
2791 /* The "maintenance set btrace" command. */
2794 maint_btrace_set_cmd (char *args
, int from_tty
)
2796 help_list (maint_btrace_set_cmdlist
, "maintenance set btrace ", all_commands
,
2800 /* The "maintenance show btrace" command. */
2803 maint_btrace_show_cmd (char *args
, int from_tty
)
2805 help_list (maint_btrace_show_cmdlist
, "maintenance show btrace ",
2806 all_commands
, gdb_stdout
);
2809 /* The "maintenance set btrace pt" command. */
2812 maint_btrace_pt_set_cmd (char *args
, int from_tty
)
2814 help_list (maint_btrace_pt_set_cmdlist
, "maintenance set btrace pt ",
2815 all_commands
, gdb_stdout
);
2818 /* The "maintenance show btrace pt" command. */
2821 maint_btrace_pt_show_cmd (char *args
, int from_tty
)
2823 help_list (maint_btrace_pt_show_cmdlist
, "maintenance show btrace pt ",
2824 all_commands
, gdb_stdout
);
2827 /* The "maintenance info btrace" command. */
2830 maint_info_btrace_cmd (char *args
, int from_tty
)
2832 struct btrace_thread_info
*btinfo
;
2833 struct thread_info
*tp
;
2834 const struct btrace_config
*conf
;
2836 if (args
!= NULL
&& *args
!= 0)
2837 error (_("Invalid argument."));
2839 tp
= find_thread_ptid (inferior_ptid
);
2841 error (_("No thread."));
2843 btinfo
= &tp
->btrace
;
2845 conf
= btrace_conf (btinfo
);
2847 error (_("No btrace configuration."));
2849 printf_unfiltered (_("Format: %s.\n"),
2850 btrace_format_string (conf
->format
));
2852 switch (conf
->format
)
2857 case BTRACE_FORMAT_BTS
:
2858 printf_unfiltered (_("Number of packets: %u.\n"),
2859 VEC_length (btrace_block_s
,
2860 btinfo
->data
.variant
.bts
.blocks
));
2863 #if defined (HAVE_LIBIPT)
2864 case BTRACE_FORMAT_PT
:
2866 struct pt_version version
;
2868 version
= pt_library_version ();
2869 printf_unfiltered (_("Version: %u.%u.%u%s.\n"), version
.major
,
2870 version
.minor
, version
.build
,
2871 version
.ext
!= NULL
? version
.ext
: "");
2873 btrace_maint_update_pt_packets (btinfo
);
2874 printf_unfiltered (_("Number of packets: %u.\n"),
2875 VEC_length (btrace_pt_packet_s
,
2876 btinfo
->maint
.variant
.pt
.packets
));
2879 #endif /* defined (HAVE_LIBIPT) */
2883 /* The "maint show btrace pt skip-pad" show value function. */
2886 show_maint_btrace_pt_skip_pad (struct ui_file
*file
, int from_tty
,
2887 struct cmd_list_element
*c
,
2890 fprintf_filtered (file
, _("Skip PAD packets is %s.\n"), value
);
2894 /* Initialize btrace maintenance commands. */
2896 void _initialize_btrace (void);
2898 _initialize_btrace (void)
2900 add_cmd ("btrace", class_maintenance
, maint_info_btrace_cmd
,
2901 _("Info about branch tracing data."), &maintenanceinfolist
);
2903 add_prefix_cmd ("btrace", class_maintenance
, maint_btrace_cmd
,
2904 _("Branch tracing maintenance commands."),
2905 &maint_btrace_cmdlist
, "maintenance btrace ",
2906 0, &maintenancelist
);
2908 add_prefix_cmd ("btrace", class_maintenance
, maint_btrace_set_cmd
, _("\
2909 Set branch tracing specific variables."),
2910 &maint_btrace_set_cmdlist
, "maintenance set btrace ",
2911 0, &maintenance_set_cmdlist
);
2913 add_prefix_cmd ("pt", class_maintenance
, maint_btrace_pt_set_cmd
, _("\
2914 Set Intel(R) Processor Trace specific variables."),
2915 &maint_btrace_pt_set_cmdlist
, "maintenance set btrace pt ",
2916 0, &maint_btrace_set_cmdlist
);
2918 add_prefix_cmd ("btrace", class_maintenance
, maint_btrace_show_cmd
, _("\
2919 Show branch tracing specific variables."),
2920 &maint_btrace_show_cmdlist
, "maintenance show btrace ",
2921 0, &maintenance_show_cmdlist
);
2923 add_prefix_cmd ("pt", class_maintenance
, maint_btrace_pt_show_cmd
, _("\
2924 Show Intel(R) Processor Trace specific variables."),
2925 &maint_btrace_pt_show_cmdlist
, "maintenance show btrace pt ",
2926 0, &maint_btrace_show_cmdlist
);
2928 add_setshow_boolean_cmd ("skip-pad", class_maintenance
,
2929 &maint_btrace_pt_skip_pad
, _("\
2930 Set whether PAD packets should be skipped in the btrace packet history."), _("\
2931 Show whether PAD packets should be skipped in the btrace packet history."),_("\
2932 When enabled, PAD packets are ignored in the btrace packet history."),
2933 NULL
, show_maint_btrace_pt_skip_pad
,
2934 &maint_btrace_pt_set_cmdlist
,
2935 &maint_btrace_pt_show_cmdlist
);
2937 add_cmd ("packet-history", class_maintenance
, maint_btrace_packet_history_cmd
,
2938 _("Print the raw branch tracing data.\n\
2939 With no argument, print ten more packets after the previous ten-line print.\n\
2940 With '-' as argument print ten packets before a previous ten-line print.\n\
2941 One argument specifies the starting packet of a ten-line print.\n\
2942 Two arguments with comma between specify starting and ending packets to \
2944 Preceded with '+'/'-' the second argument specifies the distance from the \
2946 &maint_btrace_cmdlist
);
2948 add_cmd ("clear-packet-history", class_maintenance
,
2949 maint_btrace_clear_packet_history_cmd
,
2950 _("Clears the branch tracing packet history.\n\
2951 Discards the raw branch tracing data but not the execution history data.\n\
2953 &maint_btrace_cmdlist
);
2955 add_cmd ("clear", class_maintenance
, maint_btrace_clear_cmd
,
2956 _("Clears the branch tracing data.\n\
2957 Discards the raw branch tracing data and the execution history data.\n\
2958 The next 'record' command will fetch the branch tracing data anew.\n\
2960 &maint_btrace_cmdlist
);