1 /* Branch trace support for GDB, the GNU debugger.
3 Copyright (C) 2013-2016 Free Software Foundation, Inc.
5 Contributed by Intel Corp. <markus.t.metzger@intel.com>
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "gdbthread.h"
31 #include "filenames.h"
32 #include "xml-support.h"
36 #include "cli/cli-utils.h"
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
= XCNEW (struct btrace_function
);
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
);
665 ftrace_update_insns (end
, &insn
);
667 /* We're done once we pushed the instruction at the end. */
668 if (block
->end
== pc
)
671 /* We can't continue if we fail to compute the size. */
674 warning (_("Recorded trace may be incomplete around %s."),
675 core_addr_to_string_nz (pc
));
677 /* Indicate the gap in the trace. We just added INSN so we're
678 not at the beginning. */
679 end
= ftrace_new_gap (end
, BDE_BTS_INSN_SIZE
);
687 /* Maintain the function level offset.
688 For the last block, we do it here to not consider the last
690 Since the last instruction corresponds to the current instruction
691 and is not really part of the execution history, it shouldn't
694 level
= min (level
, end
->level
);
698 btinfo
->begin
= begin
;
700 btinfo
->ngaps
= ngaps
;
702 /* LEVEL is the minimal function level of all btrace function segments.
703 Define the global level offset to -LEVEL so all function levels are
704 normalized to start at zero. */
705 btinfo
->level
= -level
;
708 #if defined (HAVE_LIBIPT)
710 static enum btrace_insn_class
711 pt_reclassify_insn (enum pt_insn_class iclass
)
716 return BTRACE_INSN_CALL
;
719 return BTRACE_INSN_RETURN
;
722 return BTRACE_INSN_JUMP
;
725 return BTRACE_INSN_OTHER
;
729 /* Return the btrace instruction flags for INSN. */
731 static enum btrace_insn_flag
732 pt_btrace_insn_flags (const struct pt_insn
*insn
)
734 enum btrace_insn_flag flags
= 0;
736 if (insn
->speculative
)
737 flags
|= BTRACE_INSN_FLAG_SPECULATIVE
;
742 /* Add function branch trace using DECODER. */
745 ftrace_add_pt (struct pt_insn_decoder
*decoder
,
746 struct btrace_function
**pbegin
,
747 struct btrace_function
**pend
, int *plevel
,
750 struct btrace_function
*begin
, *end
, *upd
;
752 int errcode
, nerrors
;
759 struct btrace_insn btinsn
;
762 errcode
= pt_insn_sync_forward (decoder
);
765 if (errcode
!= -pte_eos
)
766 warning (_("Failed to synchronize onto the Intel(R) Processor "
767 "Trace stream: %s."), pt_errstr (pt_errcode (errcode
)));
771 memset (&btinsn
, 0, sizeof (btinsn
));
774 errcode
= pt_insn_next (decoder
, &insn
, sizeof(insn
));
778 /* Look for gaps in the trace - unless we're at the beginning. */
781 /* Tracing is disabled and re-enabled each time we enter the
782 kernel. Most times, we continue from the same instruction we
783 stopped before. This is indicated via the RESUMED instruction
784 flag. The ENABLED instruction flag means that we continued
785 from some other instruction. Indicate this as a trace gap. */
787 *pend
= end
= ftrace_new_gap (end
, BDE_PT_DISABLED
);
789 /* Indicate trace overflows. */
791 *pend
= end
= ftrace_new_gap (end
, BDE_PT_OVERFLOW
);
794 upd
= ftrace_update_function (end
, insn
.ip
);
800 *pbegin
= begin
= upd
;
803 /* Maintain the function level offset. */
804 *plevel
= min (*plevel
, end
->level
);
806 btinsn
.pc
= (CORE_ADDR
) insn
.ip
;
807 btinsn
.size
= (gdb_byte
) insn
.size
;
808 btinsn
.iclass
= pt_reclassify_insn (insn
.iclass
);
809 btinsn
.flags
= pt_btrace_insn_flags (&insn
);
811 ftrace_update_insns (end
, &btinsn
);
814 if (errcode
== -pte_eos
)
817 /* If the gap is at the very beginning, we ignore it - we will have
818 less trace, but we won't have any holes in the trace. */
822 pt_insn_get_offset (decoder
, &offset
);
824 warning (_("Failed to decode Intel(R) Processor Trace near trace "
825 "offset 0x%" PRIx64
" near recorded PC 0x%" PRIx64
": %s."),
826 offset
, insn
.ip
, pt_errstr (pt_errcode (errcode
)));
828 /* Indicate the gap in the trace. */
829 *pend
= end
= ftrace_new_gap (end
, errcode
);
834 warning (_("The recorded execution trace may have gaps."));
837 /* A callback function to allow the trace decoder to read the inferior's
841 btrace_pt_readmem_callback (gdb_byte
*buffer
, size_t size
,
842 const struct pt_asid
*asid
, uint64_t pc
,
849 errcode
= target_read_code ((CORE_ADDR
) pc
, buffer
, size
);
853 CATCH (error
, RETURN_MASK_ERROR
)
862 /* Translate the vendor from one enum to another. */
864 static enum pt_cpu_vendor
865 pt_translate_cpu_vendor (enum btrace_cpu_vendor vendor
)
877 /* Finalize the function branch trace after decode. */
879 static void btrace_finalize_ftrace_pt (struct pt_insn_decoder
*decoder
,
880 struct thread_info
*tp
, int level
)
882 pt_insn_free_decoder (decoder
);
884 /* LEVEL is the minimal function level of all btrace function segments.
885 Define the global level offset to -LEVEL so all function levels are
886 normalized to start at zero. */
887 tp
->btrace
.level
= -level
;
889 /* Add a single last instruction entry for the current PC.
890 This allows us to compute the backtrace at the current PC using both
891 standard unwind and btrace unwind.
892 This extra entry is ignored by all record commands. */
896 /* Compute the function branch trace from Intel(R) Processor Trace. */
899 btrace_compute_ftrace_pt (struct thread_info
*tp
,
900 const struct btrace_data_pt
*btrace
)
902 struct btrace_thread_info
*btinfo
;
903 struct pt_insn_decoder
*decoder
;
904 struct pt_config config
;
907 if (btrace
->size
== 0)
910 btinfo
= &tp
->btrace
;
911 level
= btinfo
->begin
!= NULL
? -btinfo
->level
: INT_MAX
;
913 pt_config_init(&config
);
914 config
.begin
= btrace
->data
;
915 config
.end
= btrace
->data
+ btrace
->size
;
917 config
.cpu
.vendor
= pt_translate_cpu_vendor (btrace
->config
.cpu
.vendor
);
918 config
.cpu
.family
= btrace
->config
.cpu
.family
;
919 config
.cpu
.model
= btrace
->config
.cpu
.model
;
920 config
.cpu
.stepping
= btrace
->config
.cpu
.stepping
;
922 errcode
= pt_cpu_errata (&config
.errata
, &config
.cpu
);
924 error (_("Failed to configure the Intel(R) Processor Trace decoder: %s."),
925 pt_errstr (pt_errcode (errcode
)));
927 decoder
= pt_insn_alloc_decoder (&config
);
929 error (_("Failed to allocate the Intel(R) Processor Trace decoder."));
933 struct pt_image
*image
;
935 image
= pt_insn_get_image(decoder
);
937 error (_("Failed to configure the Intel(R) Processor Trace decoder."));
939 errcode
= pt_image_set_callback(image
, btrace_pt_readmem_callback
, NULL
);
941 error (_("Failed to configure the Intel(R) Processor Trace decoder: "
942 "%s."), pt_errstr (pt_errcode (errcode
)));
944 ftrace_add_pt (decoder
, &btinfo
->begin
, &btinfo
->end
, &level
,
947 CATCH (error
, RETURN_MASK_ALL
)
949 /* Indicate a gap in the trace if we quit trace processing. */
950 if (error
.reason
== RETURN_QUIT
&& btinfo
->end
!= NULL
)
952 btinfo
->end
= ftrace_new_gap (btinfo
->end
, BDE_PT_USER_QUIT
);
956 btrace_finalize_ftrace_pt (decoder
, tp
, level
);
958 throw_exception (error
);
962 btrace_finalize_ftrace_pt (decoder
, tp
, level
);
965 #else /* defined (HAVE_LIBIPT) */
968 btrace_compute_ftrace_pt (struct thread_info
*tp
,
969 const struct btrace_data_pt
*btrace
)
971 internal_error (__FILE__
, __LINE__
, _("Unexpected branch trace format."));
974 #endif /* defined (HAVE_LIBIPT) */
976 /* Compute the function branch trace from a block branch trace BTRACE for
977 a thread given by BTINFO. */
980 btrace_compute_ftrace (struct thread_info
*tp
, struct btrace_data
*btrace
)
982 DEBUG ("compute ftrace");
984 switch (btrace
->format
)
986 case BTRACE_FORMAT_NONE
:
989 case BTRACE_FORMAT_BTS
:
990 btrace_compute_ftrace_bts (tp
, &btrace
->variant
.bts
);
993 case BTRACE_FORMAT_PT
:
994 btrace_compute_ftrace_pt (tp
, &btrace
->variant
.pt
);
998 internal_error (__FILE__
, __LINE__
, _("Unkown branch trace format."));
1001 /* Add an entry for the current PC. */
1004 btrace_add_pc (struct thread_info
*tp
)
1006 struct btrace_data btrace
;
1007 struct btrace_block
*block
;
1008 struct regcache
*regcache
;
1009 struct cleanup
*cleanup
;
1012 regcache
= get_thread_regcache (tp
->ptid
);
1013 pc
= regcache_read_pc (regcache
);
1015 btrace_data_init (&btrace
);
1016 btrace
.format
= BTRACE_FORMAT_BTS
;
1017 btrace
.variant
.bts
.blocks
= NULL
;
1019 cleanup
= make_cleanup_btrace_data (&btrace
);
1021 block
= VEC_safe_push (btrace_block_s
, btrace
.variant
.bts
.blocks
, NULL
);
1025 btrace_compute_ftrace (tp
, &btrace
);
1027 do_cleanups (cleanup
);
1033 btrace_enable (struct thread_info
*tp
, const struct btrace_config
*conf
)
1035 if (tp
->btrace
.target
!= NULL
)
1038 #if !defined (HAVE_LIBIPT)
1039 if (conf
->format
== BTRACE_FORMAT_PT
)
1040 error (_("GDB does not support Intel(R) Processor Trace."));
1041 #endif /* !defined (HAVE_LIBIPT) */
1043 if (!target_supports_btrace (conf
->format
))
1044 error (_("Target does not support branch tracing."));
1046 DEBUG ("enable thread %d (%s)", tp
->num
, target_pid_to_str (tp
->ptid
));
1048 tp
->btrace
.target
= target_enable_btrace (tp
->ptid
, conf
);
1050 /* Add an entry for the current PC so we start tracing from where we
1052 if (tp
->btrace
.target
!= NULL
)
1058 const struct btrace_config
*
1059 btrace_conf (const struct btrace_thread_info
*btinfo
)
1061 if (btinfo
->target
== NULL
)
1064 return target_btrace_conf (btinfo
->target
);
1070 btrace_disable (struct thread_info
*tp
)
1072 struct btrace_thread_info
*btp
= &tp
->btrace
;
1075 if (btp
->target
== NULL
)
1078 DEBUG ("disable thread %d (%s)", tp
->num
, target_pid_to_str (tp
->ptid
));
1080 target_disable_btrace (btp
->target
);
1089 btrace_teardown (struct thread_info
*tp
)
1091 struct btrace_thread_info
*btp
= &tp
->btrace
;
1094 if (btp
->target
== NULL
)
1097 DEBUG ("teardown thread %d (%s)", tp
->num
, target_pid_to_str (tp
->ptid
));
1099 target_teardown_btrace (btp
->target
);
1105 /* Stitch branch trace in BTS format. */
1108 btrace_stitch_bts (struct btrace_data_bts
*btrace
, struct thread_info
*tp
)
1110 struct btrace_thread_info
*btinfo
;
1111 struct btrace_function
*last_bfun
;
1112 struct btrace_insn
*last_insn
;
1113 btrace_block_s
*first_new_block
;
1115 btinfo
= &tp
->btrace
;
1116 last_bfun
= btinfo
->end
;
1117 gdb_assert (last_bfun
!= NULL
);
1118 gdb_assert (!VEC_empty (btrace_block_s
, btrace
->blocks
));
1120 /* If the existing trace ends with a gap, we just glue the traces
1121 together. We need to drop the last (i.e. chronologically first) block
1122 of the new trace, though, since we can't fill in the start address.*/
1123 if (VEC_empty (btrace_insn_s
, last_bfun
->insn
))
1125 VEC_pop (btrace_block_s
, btrace
->blocks
);
1129 /* Beware that block trace starts with the most recent block, so the
1130 chronologically first block in the new trace is the last block in
1131 the new trace's block vector. */
1132 first_new_block
= VEC_last (btrace_block_s
, btrace
->blocks
);
1133 last_insn
= VEC_last (btrace_insn_s
, last_bfun
->insn
);
1135 /* If the current PC at the end of the block is the same as in our current
1136 trace, there are two explanations:
1137 1. we executed the instruction and some branch brought us back.
1138 2. we have not made any progress.
1139 In the first case, the delta trace vector should contain at least two
1141 In the second case, the delta trace vector should contain exactly one
1142 entry for the partial block containing the current PC. Remove it. */
1143 if (first_new_block
->end
== last_insn
->pc
1144 && VEC_length (btrace_block_s
, btrace
->blocks
) == 1)
1146 VEC_pop (btrace_block_s
, btrace
->blocks
);
1150 DEBUG ("stitching %s to %s", ftrace_print_insn_addr (last_insn
),
1151 core_addr_to_string_nz (first_new_block
->end
));
1153 /* Do a simple sanity check to make sure we don't accidentally end up
1154 with a bad block. This should not occur in practice. */
1155 if (first_new_block
->end
< last_insn
->pc
)
1157 warning (_("Error while trying to read delta trace. Falling back to "
1162 /* We adjust the last block to start at the end of our current trace. */
1163 gdb_assert (first_new_block
->begin
== 0);
1164 first_new_block
->begin
= last_insn
->pc
;
1166 /* We simply pop the last insn so we can insert it again as part of
1167 the normal branch trace computation.
1168 Since instruction iterators are based on indices in the instructions
1169 vector, we don't leave any pointers dangling. */
1170 DEBUG ("pruning insn at %s for stitching",
1171 ftrace_print_insn_addr (last_insn
));
1173 VEC_pop (btrace_insn_s
, last_bfun
->insn
);
1175 /* The instructions vector may become empty temporarily if this has
1176 been the only instruction in this function segment.
1177 This violates the invariant but will be remedied shortly by
1178 btrace_compute_ftrace when we add the new trace. */
1180 /* The only case where this would hurt is if the entire trace consisted
1181 of just that one instruction. If we remove it, we might turn the now
1182 empty btrace function segment into a gap. But we don't want gaps at
1183 the beginning. To avoid this, we remove the entire old trace. */
1184 if (last_bfun
== btinfo
->begin
&& VEC_empty (btrace_insn_s
, last_bfun
->insn
))
1190 /* Adjust the block trace in order to stitch old and new trace together.
1191 BTRACE is the new delta trace between the last and the current stop.
1192 TP is the traced thread.
1193 May modifx BTRACE as well as the existing trace in TP.
1194 Return 0 on success, -1 otherwise. */
1197 btrace_stitch_trace (struct btrace_data
*btrace
, struct thread_info
*tp
)
1199 /* If we don't have trace, there's nothing to do. */
1200 if (btrace_data_empty (btrace
))
1203 switch (btrace
->format
)
1205 case BTRACE_FORMAT_NONE
:
1208 case BTRACE_FORMAT_BTS
:
1209 return btrace_stitch_bts (&btrace
->variant
.bts
, tp
);
1211 case BTRACE_FORMAT_PT
:
1212 /* Delta reads are not supported. */
1216 internal_error (__FILE__
, __LINE__
, _("Unkown branch trace format."));
1219 /* Clear the branch trace histories in BTINFO. */
1222 btrace_clear_history (struct btrace_thread_info
*btinfo
)
1224 xfree (btinfo
->insn_history
);
1225 xfree (btinfo
->call_history
);
1226 xfree (btinfo
->replay
);
1228 btinfo
->insn_history
= NULL
;
1229 btinfo
->call_history
= NULL
;
1230 btinfo
->replay
= NULL
;
1233 /* Clear the branch trace maintenance histories in BTINFO. */
1236 btrace_maint_clear (struct btrace_thread_info
*btinfo
)
1238 switch (btinfo
->data
.format
)
1243 case BTRACE_FORMAT_BTS
:
1244 btinfo
->maint
.variant
.bts
.packet_history
.begin
= 0;
1245 btinfo
->maint
.variant
.bts
.packet_history
.end
= 0;
1248 #if defined (HAVE_LIBIPT)
1249 case BTRACE_FORMAT_PT
:
1250 xfree (btinfo
->maint
.variant
.pt
.packets
);
1252 btinfo
->maint
.variant
.pt
.packets
= NULL
;
1253 btinfo
->maint
.variant
.pt
.packet_history
.begin
= 0;
1254 btinfo
->maint
.variant
.pt
.packet_history
.end
= 0;
1256 #endif /* defined (HAVE_LIBIPT) */
1263 btrace_fetch (struct thread_info
*tp
)
1265 struct btrace_thread_info
*btinfo
;
1266 struct btrace_target_info
*tinfo
;
1267 struct btrace_data btrace
;
1268 struct cleanup
*cleanup
;
1271 DEBUG ("fetch thread %d (%s)", tp
->num
, target_pid_to_str (tp
->ptid
));
1273 btinfo
= &tp
->btrace
;
1274 tinfo
= btinfo
->target
;
1278 /* There's no way we could get new trace while replaying.
1279 On the other hand, delta trace would return a partial record with the
1280 current PC, which is the replay PC, not the last PC, as expected. */
1281 if (btinfo
->replay
!= NULL
)
1284 btrace_data_init (&btrace
);
1285 cleanup
= make_cleanup_btrace_data (&btrace
);
1287 /* Let's first try to extend the trace we already have. */
1288 if (btinfo
->end
!= NULL
)
1290 errcode
= target_read_btrace (&btrace
, tinfo
, BTRACE_READ_DELTA
);
1293 /* Success. Let's try to stitch the traces together. */
1294 errcode
= btrace_stitch_trace (&btrace
, tp
);
1298 /* We failed to read delta trace. Let's try to read new trace. */
1299 errcode
= target_read_btrace (&btrace
, tinfo
, BTRACE_READ_NEW
);
1301 /* If we got any new trace, discard what we have. */
1302 if (errcode
== 0 && !btrace_data_empty (&btrace
))
1306 /* If we were not able to read the trace, we start over. */
1310 errcode
= target_read_btrace (&btrace
, tinfo
, BTRACE_READ_ALL
);
1314 errcode
= target_read_btrace (&btrace
, tinfo
, BTRACE_READ_ALL
);
1316 /* If we were not able to read the branch trace, signal an error. */
1318 error (_("Failed to read branch trace."));
1320 /* Compute the trace, provided we have any. */
1321 if (!btrace_data_empty (&btrace
))
1323 /* Store the raw trace data. The stored data will be cleared in
1324 btrace_clear, so we always append the new trace. */
1325 btrace_data_append (&btinfo
->data
, &btrace
);
1326 btrace_maint_clear (btinfo
);
1328 btrace_clear_history (btinfo
);
1329 btrace_compute_ftrace (tp
, &btrace
);
1332 do_cleanups (cleanup
);
1338 btrace_clear (struct thread_info
*tp
)
1340 struct btrace_thread_info
*btinfo
;
1341 struct btrace_function
*it
, *trash
;
1343 DEBUG ("clear thread %d (%s)", tp
->num
, target_pid_to_str (tp
->ptid
));
1345 /* Make sure btrace frames that may hold a pointer into the branch
1346 trace data are destroyed. */
1347 reinit_frame_cache ();
1349 btinfo
= &tp
->btrace
;
1360 btinfo
->begin
= NULL
;
1364 /* Must clear the maint data before - it depends on BTINFO->DATA. */
1365 btrace_maint_clear (btinfo
);
1366 btrace_data_clear (&btinfo
->data
);
1367 btrace_clear_history (btinfo
);
1373 btrace_free_objfile (struct objfile
*objfile
)
1375 struct thread_info
*tp
;
1377 DEBUG ("free objfile");
1379 ALL_NON_EXITED_THREADS (tp
)
1383 #if defined (HAVE_LIBEXPAT)
1385 /* Check the btrace document version. */
1388 check_xml_btrace_version (struct gdb_xml_parser
*parser
,
1389 const struct gdb_xml_element
*element
,
1390 void *user_data
, VEC (gdb_xml_value_s
) *attributes
)
1393 = (const char *) xml_find_attribute (attributes
, "version")->value
;
1395 if (strcmp (version
, "1.0") != 0)
1396 gdb_xml_error (parser
, _("Unsupported btrace version: \"%s\""), version
);
1399 /* Parse a btrace "block" xml record. */
1402 parse_xml_btrace_block (struct gdb_xml_parser
*parser
,
1403 const struct gdb_xml_element
*element
,
1404 void *user_data
, VEC (gdb_xml_value_s
) *attributes
)
1406 struct btrace_data
*btrace
;
1407 struct btrace_block
*block
;
1408 ULONGEST
*begin
, *end
;
1410 btrace
= (struct btrace_data
*) user_data
;
1412 switch (btrace
->format
)
1414 case BTRACE_FORMAT_BTS
:
1417 case BTRACE_FORMAT_NONE
:
1418 btrace
->format
= BTRACE_FORMAT_BTS
;
1419 btrace
->variant
.bts
.blocks
= NULL
;
1423 gdb_xml_error (parser
, _("Btrace format error."));
1426 begin
= (ULONGEST
*) xml_find_attribute (attributes
, "begin")->value
;
1427 end
= (ULONGEST
*) xml_find_attribute (attributes
, "end")->value
;
1429 block
= VEC_safe_push (btrace_block_s
, btrace
->variant
.bts
.blocks
, NULL
);
1430 block
->begin
= *begin
;
1434 /* Parse a "raw" xml record. */
1437 parse_xml_raw (struct gdb_xml_parser
*parser
, const char *body_text
,
1438 gdb_byte
**pdata
, size_t *psize
)
1440 struct cleanup
*cleanup
;
1441 gdb_byte
*data
, *bin
;
1444 len
= strlen (body_text
);
1446 gdb_xml_error (parser
, _("Bad raw data size."));
1450 bin
= data
= (gdb_byte
*) xmalloc (size
);
1451 cleanup
= make_cleanup (xfree
, data
);
1453 /* We use hex encoding - see common/rsp-low.h. */
1461 if (hi
== 0 || lo
== 0)
1462 gdb_xml_error (parser
, _("Bad hex encoding."));
1464 *bin
++ = fromhex (hi
) * 16 + fromhex (lo
);
1468 discard_cleanups (cleanup
);
1474 /* Parse a btrace pt-config "cpu" xml record. */
1477 parse_xml_btrace_pt_config_cpu (struct gdb_xml_parser
*parser
,
1478 const struct gdb_xml_element
*element
,
1480 VEC (gdb_xml_value_s
) *attributes
)
1482 struct btrace_data
*btrace
;
1484 ULONGEST
*family
, *model
, *stepping
;
1486 vendor
= (const char *) xml_find_attribute (attributes
, "vendor")->value
;
1487 family
= (ULONGEST
*) xml_find_attribute (attributes
, "family")->value
;
1488 model
= (ULONGEST
*) xml_find_attribute (attributes
, "model")->value
;
1489 stepping
= (ULONGEST
*) xml_find_attribute (attributes
, "stepping")->value
;
1491 btrace
= (struct btrace_data
*) user_data
;
1493 if (strcmp (vendor
, "GenuineIntel") == 0)
1494 btrace
->variant
.pt
.config
.cpu
.vendor
= CV_INTEL
;
1496 btrace
->variant
.pt
.config
.cpu
.family
= *family
;
1497 btrace
->variant
.pt
.config
.cpu
.model
= *model
;
1498 btrace
->variant
.pt
.config
.cpu
.stepping
= *stepping
;
1501 /* Parse a btrace pt "raw" xml record. */
1504 parse_xml_btrace_pt_raw (struct gdb_xml_parser
*parser
,
1505 const struct gdb_xml_element
*element
,
1506 void *user_data
, const char *body_text
)
1508 struct btrace_data
*btrace
;
1510 btrace
= (struct btrace_data
*) user_data
;
1511 parse_xml_raw (parser
, body_text
, &btrace
->variant
.pt
.data
,
1512 &btrace
->variant
.pt
.size
);
1515 /* Parse a btrace "pt" xml record. */
1518 parse_xml_btrace_pt (struct gdb_xml_parser
*parser
,
1519 const struct gdb_xml_element
*element
,
1520 void *user_data
, VEC (gdb_xml_value_s
) *attributes
)
1522 struct btrace_data
*btrace
;
1524 btrace
= (struct btrace_data
*) user_data
;
1525 btrace
->format
= BTRACE_FORMAT_PT
;
1526 btrace
->variant
.pt
.config
.cpu
.vendor
= CV_UNKNOWN
;
1527 btrace
->variant
.pt
.data
= NULL
;
1528 btrace
->variant
.pt
.size
= 0;
1531 static const struct gdb_xml_attribute block_attributes
[] = {
1532 { "begin", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
1533 { "end", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
1534 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
1537 static const struct gdb_xml_attribute btrace_pt_config_cpu_attributes
[] = {
1538 { "vendor", GDB_XML_AF_NONE
, NULL
, NULL
},
1539 { "family", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
1540 { "model", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
1541 { "stepping", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
1542 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
1545 static const struct gdb_xml_element btrace_pt_config_children
[] = {
1546 { "cpu", btrace_pt_config_cpu_attributes
, NULL
, GDB_XML_EF_OPTIONAL
,
1547 parse_xml_btrace_pt_config_cpu
, NULL
},
1548 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
1551 static const struct gdb_xml_element btrace_pt_children
[] = {
1552 { "pt-config", NULL
, btrace_pt_config_children
, GDB_XML_EF_OPTIONAL
, NULL
,
1554 { "raw", NULL
, NULL
, GDB_XML_EF_OPTIONAL
, NULL
, parse_xml_btrace_pt_raw
},
1555 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
1558 static const struct gdb_xml_attribute btrace_attributes
[] = {
1559 { "version", GDB_XML_AF_NONE
, NULL
, NULL
},
1560 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
1563 static const struct gdb_xml_element btrace_children
[] = {
1564 { "block", block_attributes
, NULL
,
1565 GDB_XML_EF_REPEATABLE
| GDB_XML_EF_OPTIONAL
, parse_xml_btrace_block
, NULL
},
1566 { "pt", NULL
, btrace_pt_children
, GDB_XML_EF_OPTIONAL
, parse_xml_btrace_pt
,
1568 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
1571 static const struct gdb_xml_element btrace_elements
[] = {
1572 { "btrace", btrace_attributes
, btrace_children
, GDB_XML_EF_NONE
,
1573 check_xml_btrace_version
, NULL
},
1574 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
1577 #endif /* defined (HAVE_LIBEXPAT) */
1582 parse_xml_btrace (struct btrace_data
*btrace
, const char *buffer
)
1584 struct cleanup
*cleanup
;
1587 #if defined (HAVE_LIBEXPAT)
1589 btrace
->format
= BTRACE_FORMAT_NONE
;
1591 cleanup
= make_cleanup_btrace_data (btrace
);
1592 errcode
= gdb_xml_parse_quick (_("btrace"), "btrace.dtd", btrace_elements
,
1595 error (_("Error parsing branch trace."));
1597 /* Keep parse results. */
1598 discard_cleanups (cleanup
);
1600 #else /* !defined (HAVE_LIBEXPAT) */
1602 error (_("Cannot process branch trace. XML parsing is not supported."));
1604 #endif /* !defined (HAVE_LIBEXPAT) */
1607 #if defined (HAVE_LIBEXPAT)
1609 /* Parse a btrace-conf "bts" xml record. */
1612 parse_xml_btrace_conf_bts (struct gdb_xml_parser
*parser
,
1613 const struct gdb_xml_element
*element
,
1614 void *user_data
, VEC (gdb_xml_value_s
) *attributes
)
1616 struct btrace_config
*conf
;
1617 struct gdb_xml_value
*size
;
1619 conf
= (struct btrace_config
*) user_data
;
1620 conf
->format
= BTRACE_FORMAT_BTS
;
1623 size
= xml_find_attribute (attributes
, "size");
1625 conf
->bts
.size
= (unsigned int) *(ULONGEST
*) size
->value
;
1628 /* Parse a btrace-conf "pt" xml record. */
1631 parse_xml_btrace_conf_pt (struct gdb_xml_parser
*parser
,
1632 const struct gdb_xml_element
*element
,
1633 void *user_data
, VEC (gdb_xml_value_s
) *attributes
)
1635 struct btrace_config
*conf
;
1636 struct gdb_xml_value
*size
;
1638 conf
= (struct btrace_config
*) user_data
;
1639 conf
->format
= BTRACE_FORMAT_PT
;
1642 size
= xml_find_attribute (attributes
, "size");
1644 conf
->pt
.size
= (unsigned int) *(ULONGEST
*) size
->value
;
1647 static const struct gdb_xml_attribute btrace_conf_pt_attributes
[] = {
1648 { "size", GDB_XML_AF_OPTIONAL
, gdb_xml_parse_attr_ulongest
, NULL
},
1649 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
1652 static const struct gdb_xml_attribute btrace_conf_bts_attributes
[] = {
1653 { "size", GDB_XML_AF_OPTIONAL
, gdb_xml_parse_attr_ulongest
, NULL
},
1654 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
1657 static const struct gdb_xml_element btrace_conf_children
[] = {
1658 { "bts", btrace_conf_bts_attributes
, NULL
, GDB_XML_EF_OPTIONAL
,
1659 parse_xml_btrace_conf_bts
, NULL
},
1660 { "pt", btrace_conf_pt_attributes
, NULL
, GDB_XML_EF_OPTIONAL
,
1661 parse_xml_btrace_conf_pt
, NULL
},
1662 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
1665 static const struct gdb_xml_attribute btrace_conf_attributes
[] = {
1666 { "version", GDB_XML_AF_NONE
, NULL
, NULL
},
1667 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
1670 static const struct gdb_xml_element btrace_conf_elements
[] = {
1671 { "btrace-conf", btrace_conf_attributes
, btrace_conf_children
,
1672 GDB_XML_EF_NONE
, NULL
, NULL
},
1673 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
1676 #endif /* defined (HAVE_LIBEXPAT) */
1681 parse_xml_btrace_conf (struct btrace_config
*conf
, const char *xml
)
1685 #if defined (HAVE_LIBEXPAT)
1687 errcode
= gdb_xml_parse_quick (_("btrace-conf"), "btrace-conf.dtd",
1688 btrace_conf_elements
, xml
, conf
);
1690 error (_("Error parsing branch trace configuration."));
1692 #else /* !defined (HAVE_LIBEXPAT) */
1694 error (_("XML parsing is not supported."));
1696 #endif /* !defined (HAVE_LIBEXPAT) */
1701 const struct btrace_insn
*
1702 btrace_insn_get (const struct btrace_insn_iterator
*it
)
1704 const struct btrace_function
*bfun
;
1705 unsigned int index
, end
;
1708 bfun
= it
->function
;
1710 /* Check if the iterator points to a gap in the trace. */
1711 if (bfun
->errcode
!= 0)
1714 /* The index is within the bounds of this function's instruction vector. */
1715 end
= VEC_length (btrace_insn_s
, bfun
->insn
);
1716 gdb_assert (0 < end
);
1717 gdb_assert (index
< end
);
1719 return VEC_index (btrace_insn_s
, bfun
->insn
, index
);
1725 btrace_insn_number (const struct btrace_insn_iterator
*it
)
1727 const struct btrace_function
*bfun
;
1729 bfun
= it
->function
;
1731 /* Return zero if the iterator points to a gap in the trace. */
1732 if (bfun
->errcode
!= 0)
1735 return bfun
->insn_offset
+ it
->index
;
1741 btrace_insn_begin (struct btrace_insn_iterator
*it
,
1742 const struct btrace_thread_info
*btinfo
)
1744 const struct btrace_function
*bfun
;
1746 bfun
= btinfo
->begin
;
1748 error (_("No trace."));
1750 it
->function
= bfun
;
1757 btrace_insn_end (struct btrace_insn_iterator
*it
,
1758 const struct btrace_thread_info
*btinfo
)
1760 const struct btrace_function
*bfun
;
1761 unsigned int length
;
1765 error (_("No trace."));
1767 length
= VEC_length (btrace_insn_s
, bfun
->insn
);
1769 /* The last function may either be a gap or it contains the current
1770 instruction, which is one past the end of the execution trace; ignore
1775 it
->function
= bfun
;
1782 btrace_insn_next (struct btrace_insn_iterator
*it
, unsigned int stride
)
1784 const struct btrace_function
*bfun
;
1785 unsigned int index
, steps
;
1787 bfun
= it
->function
;
1793 unsigned int end
, space
, adv
;
1795 end
= VEC_length (btrace_insn_s
, bfun
->insn
);
1797 /* An empty function segment represents a gap in the trace. We count
1798 it as one instruction. */
1801 const struct btrace_function
*next
;
1803 next
= bfun
->flow
.next
;
1816 gdb_assert (0 < end
);
1817 gdb_assert (index
< end
);
1819 /* Compute the number of instructions remaining in this segment. */
1820 space
= end
- index
;
1822 /* Advance the iterator as far as possible within this segment. */
1823 adv
= min (space
, stride
);
1828 /* Move to the next function if we're at the end of this one. */
1831 const struct btrace_function
*next
;
1833 next
= bfun
->flow
.next
;
1836 /* We stepped past the last function.
1838 Let's adjust the index to point to the last instruction in
1839 the previous function. */
1845 /* We now point to the first instruction in the new function. */
1850 /* We did make progress. */
1851 gdb_assert (adv
> 0);
1854 /* Update the iterator. */
1855 it
->function
= bfun
;
1864 btrace_insn_prev (struct btrace_insn_iterator
*it
, unsigned int stride
)
1866 const struct btrace_function
*bfun
;
1867 unsigned int index
, steps
;
1869 bfun
= it
->function
;
1877 /* Move to the previous function if we're at the start of this one. */
1880 const struct btrace_function
*prev
;
1882 prev
= bfun
->flow
.prev
;
1886 /* We point to one after the last instruction in the new function. */
1888 index
= VEC_length (btrace_insn_s
, bfun
->insn
);
1890 /* An empty function segment represents a gap in the trace. We count
1891 it as one instruction. */
1901 /* Advance the iterator as far as possible within this segment. */
1902 adv
= min (index
, stride
);
1908 /* We did make progress. */
1909 gdb_assert (adv
> 0);
1912 /* Update the iterator. */
1913 it
->function
= bfun
;
1922 btrace_insn_cmp (const struct btrace_insn_iterator
*lhs
,
1923 const struct btrace_insn_iterator
*rhs
)
1925 unsigned int lnum
, rnum
;
1927 lnum
= btrace_insn_number (lhs
);
1928 rnum
= btrace_insn_number (rhs
);
1930 /* A gap has an instruction number of zero. Things are getting more
1931 complicated if gaps are involved.
1933 We take the instruction number offset from the iterator's function.
1934 This is the number of the first instruction after the gap.
1936 This is OK as long as both lhs and rhs point to gaps. If only one of
1937 them does, we need to adjust the number based on the other's regular
1938 instruction number. Otherwise, a gap might compare equal to an
1941 if (lnum
== 0 && rnum
== 0)
1943 lnum
= lhs
->function
->insn_offset
;
1944 rnum
= rhs
->function
->insn_offset
;
1948 lnum
= lhs
->function
->insn_offset
;
1955 rnum
= rhs
->function
->insn_offset
;
1961 return (int) (lnum
- rnum
);
1967 btrace_find_insn_by_number (struct btrace_insn_iterator
*it
,
1968 const struct btrace_thread_info
*btinfo
,
1969 unsigned int number
)
1971 const struct btrace_function
*bfun
;
1972 unsigned int end
, length
;
1974 for (bfun
= btinfo
->end
; bfun
!= NULL
; bfun
= bfun
->flow
.prev
)
1977 if (bfun
->errcode
!= 0)
1980 if (bfun
->insn_offset
<= number
)
1987 length
= VEC_length (btrace_insn_s
, bfun
->insn
);
1988 gdb_assert (length
> 0);
1990 end
= bfun
->insn_offset
+ length
;
1994 it
->function
= bfun
;
1995 it
->index
= number
- bfun
->insn_offset
;
2002 const struct btrace_function
*
2003 btrace_call_get (const struct btrace_call_iterator
*it
)
2005 return it
->function
;
2011 btrace_call_number (const struct btrace_call_iterator
*it
)
2013 const struct btrace_thread_info
*btinfo
;
2014 const struct btrace_function
*bfun
;
2017 btinfo
= it
->btinfo
;
2018 bfun
= it
->function
;
2020 return bfun
->number
;
2022 /* For the end iterator, i.e. bfun == NULL, we return one more than the
2023 number of the last function. */
2025 insns
= VEC_length (btrace_insn_s
, bfun
->insn
);
2027 /* If the function contains only a single instruction (i.e. the current
2028 instruction), it will be skipped and its number is already the number
2031 return bfun
->number
;
2033 /* Otherwise, return one more than the number of the last function. */
2034 return bfun
->number
+ 1;
2040 btrace_call_begin (struct btrace_call_iterator
*it
,
2041 const struct btrace_thread_info
*btinfo
)
2043 const struct btrace_function
*bfun
;
2045 bfun
= btinfo
->begin
;
2047 error (_("No trace."));
2049 it
->btinfo
= btinfo
;
2050 it
->function
= bfun
;
2056 btrace_call_end (struct btrace_call_iterator
*it
,
2057 const struct btrace_thread_info
*btinfo
)
2059 const struct btrace_function
*bfun
;
2063 error (_("No trace."));
2065 it
->btinfo
= btinfo
;
2066 it
->function
= NULL
;
2072 btrace_call_next (struct btrace_call_iterator
*it
, unsigned int stride
)
2074 const struct btrace_function
*bfun
;
2077 bfun
= it
->function
;
2079 while (bfun
!= NULL
)
2081 const struct btrace_function
*next
;
2084 next
= bfun
->flow
.next
;
2087 /* Ignore the last function if it only contains a single
2088 (i.e. the current) instruction. */
2089 insns
= VEC_length (btrace_insn_s
, bfun
->insn
);
2094 if (stride
== steps
)
2101 it
->function
= bfun
;
2108 btrace_call_prev (struct btrace_call_iterator
*it
, unsigned int stride
)
2110 const struct btrace_thread_info
*btinfo
;
2111 const struct btrace_function
*bfun
;
2114 bfun
= it
->function
;
2121 btinfo
= it
->btinfo
;
2126 /* Ignore the last function if it only contains a single
2127 (i.e. the current) instruction. */
2128 insns
= VEC_length (btrace_insn_s
, bfun
->insn
);
2130 bfun
= bfun
->flow
.prev
;
2138 while (steps
< stride
)
2140 const struct btrace_function
*prev
;
2142 prev
= bfun
->flow
.prev
;
2150 it
->function
= bfun
;
2157 btrace_call_cmp (const struct btrace_call_iterator
*lhs
,
2158 const struct btrace_call_iterator
*rhs
)
2160 unsigned int lnum
, rnum
;
2162 lnum
= btrace_call_number (lhs
);
2163 rnum
= btrace_call_number (rhs
);
2165 return (int) (lnum
- rnum
);
2171 btrace_find_call_by_number (struct btrace_call_iterator
*it
,
2172 const struct btrace_thread_info
*btinfo
,
2173 unsigned int number
)
2175 const struct btrace_function
*bfun
;
2177 for (bfun
= btinfo
->end
; bfun
!= NULL
; bfun
= bfun
->flow
.prev
)
2181 bnum
= bfun
->number
;
2184 it
->btinfo
= btinfo
;
2185 it
->function
= bfun
;
2189 /* Functions are ordered and numbered consecutively. We could bail out
2190 earlier. On the other hand, it is very unlikely that we search for
2191 a nonexistent function. */
2200 btrace_set_insn_history (struct btrace_thread_info
*btinfo
,
2201 const struct btrace_insn_iterator
*begin
,
2202 const struct btrace_insn_iterator
*end
)
2204 if (btinfo
->insn_history
== NULL
)
2205 btinfo
->insn_history
= XCNEW (struct btrace_insn_history
);
2207 btinfo
->insn_history
->begin
= *begin
;
2208 btinfo
->insn_history
->end
= *end
;
2214 btrace_set_call_history (struct btrace_thread_info
*btinfo
,
2215 const struct btrace_call_iterator
*begin
,
2216 const struct btrace_call_iterator
*end
)
2218 gdb_assert (begin
->btinfo
== end
->btinfo
);
2220 if (btinfo
->call_history
== NULL
)
2221 btinfo
->call_history
= XCNEW (struct btrace_call_history
);
2223 btinfo
->call_history
->begin
= *begin
;
2224 btinfo
->call_history
->end
= *end
;
2230 btrace_is_replaying (struct thread_info
*tp
)
2232 return tp
->btrace
.replay
!= NULL
;
2238 btrace_is_empty (struct thread_info
*tp
)
2240 struct btrace_insn_iterator begin
, end
;
2241 struct btrace_thread_info
*btinfo
;
2243 btinfo
= &tp
->btrace
;
2245 if (btinfo
->begin
== NULL
)
2248 btrace_insn_begin (&begin
, btinfo
);
2249 btrace_insn_end (&end
, btinfo
);
2251 return btrace_insn_cmp (&begin
, &end
) == 0;
2254 /* Forward the cleanup request. */
2257 do_btrace_data_cleanup (void *arg
)
2259 btrace_data_fini ((struct btrace_data
*) arg
);
2265 make_cleanup_btrace_data (struct btrace_data
*data
)
2267 return make_cleanup (do_btrace_data_cleanup
, data
);
2270 #if defined (HAVE_LIBIPT)
2272 /* Print a single packet. */
2275 pt_print_packet (const struct pt_packet
*packet
)
2277 switch (packet
->type
)
2280 printf_unfiltered (("[??: %x]"), packet
->type
);
2284 printf_unfiltered (("psb"));
2288 printf_unfiltered (("psbend"));
2292 printf_unfiltered (("pad"));
2296 printf_unfiltered (("tip %u: 0x%" PRIx64
""),
2297 packet
->payload
.ip
.ipc
,
2298 packet
->payload
.ip
.ip
);
2302 printf_unfiltered (("tip.pge %u: 0x%" PRIx64
""),
2303 packet
->payload
.ip
.ipc
,
2304 packet
->payload
.ip
.ip
);
2308 printf_unfiltered (("tip.pgd %u: 0x%" PRIx64
""),
2309 packet
->payload
.ip
.ipc
,
2310 packet
->payload
.ip
.ip
);
2314 printf_unfiltered (("fup %u: 0x%" PRIx64
""),
2315 packet
->payload
.ip
.ipc
,
2316 packet
->payload
.ip
.ip
);
2320 printf_unfiltered (("tnt-8 %u: 0x%" PRIx64
""),
2321 packet
->payload
.tnt
.bit_size
,
2322 packet
->payload
.tnt
.payload
);
2326 printf_unfiltered (("tnt-64 %u: 0x%" PRIx64
""),
2327 packet
->payload
.tnt
.bit_size
,
2328 packet
->payload
.tnt
.payload
);
2332 printf_unfiltered (("pip %" PRIx64
"%s"), packet
->payload
.pip
.cr3
,
2333 packet
->payload
.pip
.nr
? (" nr") : (""));
2337 printf_unfiltered (("tsc %" PRIx64
""), packet
->payload
.tsc
.tsc
);
2341 printf_unfiltered (("cbr %u"), packet
->payload
.cbr
.ratio
);
2345 switch (packet
->payload
.mode
.leaf
)
2348 printf_unfiltered (("mode %u"), packet
->payload
.mode
.leaf
);
2352 printf_unfiltered (("mode.exec%s%s"),
2353 packet
->payload
.mode
.bits
.exec
.csl
2355 packet
->payload
.mode
.bits
.exec
.csd
2356 ? (" cs.d") : (""));
2360 printf_unfiltered (("mode.tsx%s%s"),
2361 packet
->payload
.mode
.bits
.tsx
.intx
2363 packet
->payload
.mode
.bits
.tsx
.abrt
2364 ? (" abrt") : (""));
2370 printf_unfiltered (("ovf"));
2374 printf_unfiltered (("stop"));
2378 printf_unfiltered (("vmcs %" PRIx64
""), packet
->payload
.vmcs
.base
);
2382 printf_unfiltered (("tma %x %x"), packet
->payload
.tma
.ctc
,
2383 packet
->payload
.tma
.fc
);
2387 printf_unfiltered (("mtc %x"), packet
->payload
.mtc
.ctc
);
2391 printf_unfiltered (("cyc %" PRIx64
""), packet
->payload
.cyc
.value
);
2395 printf_unfiltered (("mnt %" PRIx64
""), packet
->payload
.mnt
.payload
);
2400 /* Decode packets into MAINT using DECODER. */
2403 btrace_maint_decode_pt (struct btrace_maint_info
*maint
,
2404 struct pt_packet_decoder
*decoder
)
2410 struct btrace_pt_packet packet
;
2412 errcode
= pt_pkt_sync_forward (decoder
);
2418 pt_pkt_get_offset (decoder
, &packet
.offset
);
2420 errcode
= pt_pkt_next (decoder
, &packet
.packet
,
2421 sizeof(packet
.packet
));
2425 if (maint_btrace_pt_skip_pad
== 0 || packet
.packet
.type
!= ppt_pad
)
2427 packet
.errcode
= pt_errcode (errcode
);
2428 VEC_safe_push (btrace_pt_packet_s
, maint
->variant
.pt
.packets
,
2433 if (errcode
== -pte_eos
)
2436 packet
.errcode
= pt_errcode (errcode
);
2437 VEC_safe_push (btrace_pt_packet_s
, maint
->variant
.pt
.packets
,
2440 warning (_("Error at trace offset 0x%" PRIx64
": %s."),
2441 packet
.offset
, pt_errstr (packet
.errcode
));
2444 if (errcode
!= -pte_eos
)
2445 warning (_("Failed to synchronize onto the Intel(R) Processor Trace "
2446 "stream: %s."), pt_errstr (pt_errcode (errcode
)));
2449 /* Update the packet history in BTINFO. */
2452 btrace_maint_update_pt_packets (struct btrace_thread_info
*btinfo
)
2454 volatile struct gdb_exception except
;
2455 struct pt_packet_decoder
*decoder
;
2456 struct btrace_data_pt
*pt
;
2457 struct pt_config config
;
2460 pt
= &btinfo
->data
.variant
.pt
;
2462 /* Nothing to do if there is no trace. */
2466 memset (&config
, 0, sizeof(config
));
2468 config
.size
= sizeof (config
);
2469 config
.begin
= pt
->data
;
2470 config
.end
= pt
->data
+ pt
->size
;
2472 config
.cpu
.vendor
= pt_translate_cpu_vendor (pt
->config
.cpu
.vendor
);
2473 config
.cpu
.family
= pt
->config
.cpu
.family
;
2474 config
.cpu
.model
= pt
->config
.cpu
.model
;
2475 config
.cpu
.stepping
= pt
->config
.cpu
.stepping
;
2477 errcode
= pt_cpu_errata (&config
.errata
, &config
.cpu
);
2479 error (_("Failed to configure the Intel(R) Processor Trace decoder: %s."),
2480 pt_errstr (pt_errcode (errcode
)));
2482 decoder
= pt_pkt_alloc_decoder (&config
);
2483 if (decoder
== NULL
)
2484 error (_("Failed to allocate the Intel(R) Processor Trace decoder."));
2488 btrace_maint_decode_pt (&btinfo
->maint
, decoder
);
2490 CATCH (except
, RETURN_MASK_ALL
)
2492 pt_pkt_free_decoder (decoder
);
2494 if (except
.reason
< 0)
2495 throw_exception (except
);
2499 pt_pkt_free_decoder (decoder
);
2502 #endif /* !defined (HAVE_LIBIPT) */
2504 /* Update the packet maintenance information for BTINFO and store the
2505 low and high bounds into BEGIN and END, respectively.
2506 Store the current iterator state into FROM and TO. */
2509 btrace_maint_update_packets (struct btrace_thread_info
*btinfo
,
2510 unsigned int *begin
, unsigned int *end
,
2511 unsigned int *from
, unsigned int *to
)
2513 switch (btinfo
->data
.format
)
2522 case BTRACE_FORMAT_BTS
:
2523 /* Nothing to do - we operate directly on BTINFO->DATA. */
2525 *end
= VEC_length (btrace_block_s
, btinfo
->data
.variant
.bts
.blocks
);
2526 *from
= btinfo
->maint
.variant
.bts
.packet_history
.begin
;
2527 *to
= btinfo
->maint
.variant
.bts
.packet_history
.end
;
2530 #if defined (HAVE_LIBIPT)
2531 case BTRACE_FORMAT_PT
:
2532 if (VEC_empty (btrace_pt_packet_s
, btinfo
->maint
.variant
.pt
.packets
))
2533 btrace_maint_update_pt_packets (btinfo
);
2536 *end
= VEC_length (btrace_pt_packet_s
, btinfo
->maint
.variant
.pt
.packets
);
2537 *from
= btinfo
->maint
.variant
.pt
.packet_history
.begin
;
2538 *to
= btinfo
->maint
.variant
.pt
.packet_history
.end
;
2540 #endif /* defined (HAVE_LIBIPT) */
2544 /* Print packets in BTINFO from BEGIN (inclusive) until END (exclusive) and
2545 update the current iterator position. */
2548 btrace_maint_print_packets (struct btrace_thread_info
*btinfo
,
2549 unsigned int begin
, unsigned int end
)
2551 switch (btinfo
->data
.format
)
2556 case BTRACE_FORMAT_BTS
:
2558 VEC (btrace_block_s
) *blocks
;
2561 blocks
= btinfo
->data
.variant
.bts
.blocks
;
2562 for (blk
= begin
; blk
< end
; ++blk
)
2564 const btrace_block_s
*block
;
2566 block
= VEC_index (btrace_block_s
, blocks
, blk
);
2568 printf_unfiltered ("%u\tbegin: %s, end: %s\n", blk
,
2569 core_addr_to_string_nz (block
->begin
),
2570 core_addr_to_string_nz (block
->end
));
2573 btinfo
->maint
.variant
.bts
.packet_history
.begin
= begin
;
2574 btinfo
->maint
.variant
.bts
.packet_history
.end
= end
;
2578 #if defined (HAVE_LIBIPT)
2579 case BTRACE_FORMAT_PT
:
2581 VEC (btrace_pt_packet_s
) *packets
;
2584 packets
= btinfo
->maint
.variant
.pt
.packets
;
2585 for (pkt
= begin
; pkt
< end
; ++pkt
)
2587 const struct btrace_pt_packet
*packet
;
2589 packet
= VEC_index (btrace_pt_packet_s
, packets
, pkt
);
2591 printf_unfiltered ("%u\t", pkt
);
2592 printf_unfiltered ("0x%" PRIx64
"\t", packet
->offset
);
2594 if (packet
->errcode
== pte_ok
)
2595 pt_print_packet (&packet
->packet
);
2597 printf_unfiltered ("[error: %s]", pt_errstr (packet
->errcode
));
2599 printf_unfiltered ("\n");
2602 btinfo
->maint
.variant
.pt
.packet_history
.begin
= begin
;
2603 btinfo
->maint
.variant
.pt
.packet_history
.end
= end
;
2606 #endif /* defined (HAVE_LIBIPT) */
2610 /* Read a number from an argument string. */
2613 get_uint (char **arg
)
2615 char *begin
, *end
, *pos
;
2616 unsigned long number
;
2619 pos
= skip_spaces (begin
);
2621 if (!isdigit (*pos
))
2622 error (_("Expected positive number, got: %s."), pos
);
2624 number
= strtoul (pos
, &end
, 10);
2625 if (number
> UINT_MAX
)
2626 error (_("Number too big."));
2628 *arg
+= (end
- begin
);
2630 return (unsigned int) number
;
2633 /* Read a context size from an argument string. */
2636 get_context_size (char **arg
)
2641 pos
= skip_spaces (*arg
);
2643 if (!isdigit (*pos
))
2644 error (_("Expected positive number, got: %s."), pos
);
2646 return strtol (pos
, arg
, 10);
2649 /* Complain about junk at the end of an argument string. */
2652 no_chunk (char *arg
)
2655 error (_("Junk after argument: %s."), arg
);
2658 /* The "maintenance btrace packet-history" command. */
2661 maint_btrace_packet_history_cmd (char *arg
, int from_tty
)
2663 struct btrace_thread_info
*btinfo
;
2664 struct thread_info
*tp
;
2665 unsigned int size
, begin
, end
, from
, to
;
2667 tp
= find_thread_ptid (inferior_ptid
);
2669 error (_("No thread."));
2672 btinfo
= &tp
->btrace
;
2674 btrace_maint_update_packets (btinfo
, &begin
, &end
, &from
, &to
);
2677 printf_unfiltered (_("No trace.\n"));
2681 if (arg
== NULL
|| *arg
== 0 || strcmp (arg
, "+") == 0)
2685 if (end
- from
< size
)
2689 else if (strcmp (arg
, "-") == 0)
2693 if (to
- begin
< size
)
2699 from
= get_uint (&arg
);
2701 error (_("'%u' is out of range."), from
);
2703 arg
= skip_spaces (arg
);
2706 arg
= skip_spaces (++arg
);
2711 size
= get_context_size (&arg
);
2715 if (end
- from
< size
)
2719 else if (*arg
== '-')
2722 size
= get_context_size (&arg
);
2726 /* Include the packet given as first argument. */
2730 if (to
- begin
< size
)
2736 to
= get_uint (&arg
);
2738 /* Include the packet at the second argument and silently
2739 truncate the range. */
2752 if (end
- from
< size
)
2760 btrace_maint_print_packets (btinfo
, from
, to
);
2763 /* The "maintenance btrace clear-packet-history" command. */
2766 maint_btrace_clear_packet_history_cmd (char *args
, int from_tty
)
2768 struct btrace_thread_info
*btinfo
;
2769 struct thread_info
*tp
;
2771 if (args
!= NULL
&& *args
!= 0)
2772 error (_("Invalid argument."));
2774 tp
= find_thread_ptid (inferior_ptid
);
2776 error (_("No thread."));
2778 btinfo
= &tp
->btrace
;
2780 /* Must clear the maint data before - it depends on BTINFO->DATA. */
2781 btrace_maint_clear (btinfo
);
2782 btrace_data_clear (&btinfo
->data
);
2785 /* The "maintenance btrace clear" command. */
2788 maint_btrace_clear_cmd (char *args
, int from_tty
)
2790 struct btrace_thread_info
*btinfo
;
2791 struct thread_info
*tp
;
2793 if (args
!= NULL
&& *args
!= 0)
2794 error (_("Invalid argument."));
2796 tp
= find_thread_ptid (inferior_ptid
);
2798 error (_("No thread."));
2803 /* The "maintenance btrace" command. */
2806 maint_btrace_cmd (char *args
, int from_tty
)
2808 help_list (maint_btrace_cmdlist
, "maintenance btrace ", all_commands
,
2812 /* The "maintenance set btrace" command. */
2815 maint_btrace_set_cmd (char *args
, int from_tty
)
2817 help_list (maint_btrace_set_cmdlist
, "maintenance set btrace ", all_commands
,
2821 /* The "maintenance show btrace" command. */
2824 maint_btrace_show_cmd (char *args
, int from_tty
)
2826 help_list (maint_btrace_show_cmdlist
, "maintenance show btrace ",
2827 all_commands
, gdb_stdout
);
2830 /* The "maintenance set btrace pt" command. */
2833 maint_btrace_pt_set_cmd (char *args
, int from_tty
)
2835 help_list (maint_btrace_pt_set_cmdlist
, "maintenance set btrace pt ",
2836 all_commands
, gdb_stdout
);
2839 /* The "maintenance show btrace pt" command. */
2842 maint_btrace_pt_show_cmd (char *args
, int from_tty
)
2844 help_list (maint_btrace_pt_show_cmdlist
, "maintenance show btrace pt ",
2845 all_commands
, gdb_stdout
);
2848 /* The "maintenance info btrace" command. */
2851 maint_info_btrace_cmd (char *args
, int from_tty
)
2853 struct btrace_thread_info
*btinfo
;
2854 struct thread_info
*tp
;
2855 const struct btrace_config
*conf
;
2857 if (args
!= NULL
&& *args
!= 0)
2858 error (_("Invalid argument."));
2860 tp
= find_thread_ptid (inferior_ptid
);
2862 error (_("No thread."));
2864 btinfo
= &tp
->btrace
;
2866 conf
= btrace_conf (btinfo
);
2868 error (_("No btrace configuration."));
2870 printf_unfiltered (_("Format: %s.\n"),
2871 btrace_format_string (conf
->format
));
2873 switch (conf
->format
)
2878 case BTRACE_FORMAT_BTS
:
2879 printf_unfiltered (_("Number of packets: %u.\n"),
2880 VEC_length (btrace_block_s
,
2881 btinfo
->data
.variant
.bts
.blocks
));
2884 #if defined (HAVE_LIBIPT)
2885 case BTRACE_FORMAT_PT
:
2887 struct pt_version version
;
2889 version
= pt_library_version ();
2890 printf_unfiltered (_("Version: %u.%u.%u%s.\n"), version
.major
,
2891 version
.minor
, version
.build
,
2892 version
.ext
!= NULL
? version
.ext
: "");
2894 btrace_maint_update_pt_packets (btinfo
);
2895 printf_unfiltered (_("Number of packets: %u.\n"),
2896 VEC_length (btrace_pt_packet_s
,
2897 btinfo
->maint
.variant
.pt
.packets
));
2900 #endif /* defined (HAVE_LIBIPT) */
2904 /* The "maint show btrace pt skip-pad" show value function. */
2907 show_maint_btrace_pt_skip_pad (struct ui_file
*file
, int from_tty
,
2908 struct cmd_list_element
*c
,
2911 fprintf_filtered (file
, _("Skip PAD packets is %s.\n"), value
);
2915 /* Initialize btrace maintenance commands. */
2917 void _initialize_btrace (void);
2919 _initialize_btrace (void)
2921 add_cmd ("btrace", class_maintenance
, maint_info_btrace_cmd
,
2922 _("Info about branch tracing data."), &maintenanceinfolist
);
2924 add_prefix_cmd ("btrace", class_maintenance
, maint_btrace_cmd
,
2925 _("Branch tracing maintenance commands."),
2926 &maint_btrace_cmdlist
, "maintenance btrace ",
2927 0, &maintenancelist
);
2929 add_prefix_cmd ("btrace", class_maintenance
, maint_btrace_set_cmd
, _("\
2930 Set branch tracing specific variables."),
2931 &maint_btrace_set_cmdlist
, "maintenance set btrace ",
2932 0, &maintenance_set_cmdlist
);
2934 add_prefix_cmd ("pt", class_maintenance
, maint_btrace_pt_set_cmd
, _("\
2935 Set Intel(R) Processor Trace specific variables."),
2936 &maint_btrace_pt_set_cmdlist
, "maintenance set btrace pt ",
2937 0, &maint_btrace_set_cmdlist
);
2939 add_prefix_cmd ("btrace", class_maintenance
, maint_btrace_show_cmd
, _("\
2940 Show branch tracing specific variables."),
2941 &maint_btrace_show_cmdlist
, "maintenance show btrace ",
2942 0, &maintenance_show_cmdlist
);
2944 add_prefix_cmd ("pt", class_maintenance
, maint_btrace_pt_show_cmd
, _("\
2945 Show Intel(R) Processor Trace specific variables."),
2946 &maint_btrace_pt_show_cmdlist
, "maintenance show btrace pt ",
2947 0, &maint_btrace_show_cmdlist
);
2949 add_setshow_boolean_cmd ("skip-pad", class_maintenance
,
2950 &maint_btrace_pt_skip_pad
, _("\
2951 Set whether PAD packets should be skipped in the btrace packet history."), _("\
2952 Show whether PAD packets should be skipped in the btrace packet history."),_("\
2953 When enabled, PAD packets are ignored in the btrace packet history."),
2954 NULL
, show_maint_btrace_pt_skip_pad
,
2955 &maint_btrace_pt_set_cmdlist
,
2956 &maint_btrace_pt_show_cmdlist
);
2958 add_cmd ("packet-history", class_maintenance
, maint_btrace_packet_history_cmd
,
2959 _("Print the raw branch tracing data.\n\
2960 With no argument, print ten more packets after the previous ten-line print.\n\
2961 With '-' as argument print ten packets before a previous ten-line print.\n\
2962 One argument specifies the starting packet of a ten-line print.\n\
2963 Two arguments with comma between specify starting and ending packets to \
2965 Preceded with '+'/'-' the second argument specifies the distance from the \
2967 &maint_btrace_cmdlist
);
2969 add_cmd ("clear-packet-history", class_maintenance
,
2970 maint_btrace_clear_packet_history_cmd
,
2971 _("Clears the branch tracing packet history.\n\
2972 Discards the raw branch tracing data but not the execution history data.\n\
2974 &maint_btrace_cmdlist
);
2976 add_cmd ("clear", class_maintenance
, maint_btrace_clear_cmd
,
2977 _("Clears the branch tracing data.\n\
2978 Discards the raw branch tracing data and the execution history data.\n\
2979 The next 'record' command will fetch the branch tracing data anew.\n\
2981 &maint_btrace_cmdlist
);