1 /* Branch trace support for GDB, the GNU debugger.
3 Copyright (C) 2013-2017 Free Software Foundation, Inc.
5 Contributed by Intel Corp. <markus.t.metzger@intel.com>.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
25 /* Branch tracing (btrace) is a per-thread control-flow execution trace of the
26 inferior. For presentation purposes, the branch trace is represented as a
27 list of sequential control-flow blocks, one such list per thread. */
29 #include "btrace-common.h"
30 #include "target/waitstatus.h" /* For enum target_stop_reason. */
31 #include "common/enum-flags.h"
33 #if defined (HAVE_LIBIPT)
34 # include <intel-pt.h>
40 struct btrace_function;
42 /* A coarse instruction classification. */
43 enum btrace_insn_class
45 /* The instruction is something not listed below. */
48 /* The instruction is a function call. */
51 /* The instruction is a function return. */
54 /* The instruction is an unconditional jump. */
58 /* Instruction flags. */
61 /* The instruction has been executed speculatively. */
62 BTRACE_INSN_FLAG_SPECULATIVE = (1 << 0)
64 DEF_ENUM_FLAGS_TYPE (enum btrace_insn_flag, btrace_insn_flags);
66 /* A branch trace instruction.
68 This represents a single instruction in a branch trace. */
71 /* The address of this instruction. */
74 /* The size of this instruction in bytes. */
77 /* The instruction class of this instruction. */
78 enum btrace_insn_class iclass;
80 /* A bit vector of BTRACE_INSN_FLAGS. */
81 btrace_insn_flags flags;
84 /* A vector of branch trace instructions. */
85 typedef struct btrace_insn btrace_insn_s;
86 DEF_VEC_O (btrace_insn_s);
88 /* A doubly-linked list of branch trace function segments. */
89 struct btrace_func_link
91 struct btrace_function *prev;
92 struct btrace_function *next;
95 /* Flags for btrace function segments. */
96 enum btrace_function_flag
98 /* The 'up' link interpretation.
99 If set, it points to the function segment we returned to.
100 If clear, it points to the function segment we called from. */
101 BFUN_UP_LINKS_TO_RET = (1 << 0),
103 /* The 'up' link points to a tail call. This obviously only makes sense
104 if bfun_up_links_to_ret is clear. */
105 BFUN_UP_LINKS_TO_TAILCALL = (1 << 1)
107 DEF_ENUM_FLAGS_TYPE (enum btrace_function_flag, btrace_function_flags);
109 /* Decode errors for the BTS recording format. */
110 enum btrace_bts_error
112 /* The instruction trace overflowed the end of the trace block. */
113 BDE_BTS_OVERFLOW = 1,
115 /* The instruction size could not be determined. */
119 /* Decode errors for the Intel Processor Trace recording format. */
122 /* The user cancelled trace processing. */
123 BDE_PT_USER_QUIT = 1,
125 /* Tracing was temporarily disabled. */
128 /* Trace recording overflowed. */
131 /* Negative numbers are used by the decoder library. */
134 /* A branch trace function segment.
136 This represents a function segment in a branch trace, i.e. a consecutive
137 number of instructions belonging to the same function.
139 In case of decode errors, we add an empty function segment to indicate
140 the gap in the trace.
142 We do not allow function segments without instructions otherwise. */
143 struct btrace_function
145 /* The full and minimal symbol for the function. Both may be NULL. */
146 struct minimal_symbol *msym;
149 /* The previous and next segment belonging to the same function.
150 If a function calls another function, the former will have at least
151 two segments: one before the call and another after the return. */
152 struct btrace_func_link segment;
154 /* The previous and next function in control flow order. */
155 struct btrace_func_link flow;
157 /* The directly preceding function segment in a (fake) call stack. */
158 struct btrace_function *up;
160 /* The instructions in this function segment.
161 The instruction vector will be empty if the function segment
162 represents a decode error. */
163 VEC (btrace_insn_s) *insn;
165 /* The error code of a decode error that led to a gap.
166 Must be zero unless INSN is empty; non-zero otherwise. */
169 /* The instruction number offset for the first instruction in this
171 If INSN is empty this is the insn_offset of the succeding function
172 segment in control-flow order. */
173 unsigned int insn_offset;
175 /* The 1-based function number in control-flow order.
176 If INSN is empty indicating a gap in the trace due to a decode error,
177 we still count the gap as a function. */
180 /* The function level in a back trace across the entire branch trace.
181 A caller's level is one lower than the level of its callee.
183 Levels can be negative if we see returns for which we have not seen
184 the corresponding calls. The branch trace thread information provides
185 a fixup to normalize function levels so the smallest level is zero. */
188 /* A bit-vector of btrace_function_flag. */
189 btrace_function_flags flags;
192 /* A branch trace instruction iterator. */
193 struct btrace_insn_iterator
195 /* The branch trace information for this thread. Will never be NULL. */
196 const struct btrace_thread_info *btinfo;
198 /* The branch trace function segment containing the instruction.
199 Will never be NULL. */
200 const struct btrace_function *function;
202 /* The index into the function segment's instruction vector. */
206 /* A branch trace function call iterator. */
207 struct btrace_call_iterator
209 /* The branch trace information for this thread. Will never be NULL. */
210 const struct btrace_thread_info *btinfo;
212 /* The index of the function segment in BTINFO->FUNCTIONS. */
216 /* Branch trace iteration state for "record instruction-history". */
217 struct btrace_insn_history
219 /* The branch trace instruction range from BEGIN (inclusive) to
220 END (exclusive) that has been covered last time. */
221 struct btrace_insn_iterator begin;
222 struct btrace_insn_iterator end;
225 /* Branch trace iteration state for "record function-call-history". */
226 struct btrace_call_history
228 /* The branch trace function range from BEGIN (inclusive) to END (exclusive)
229 that has been covered last time. */
230 struct btrace_call_iterator begin;
231 struct btrace_call_iterator end;
234 /* Branch trace thread flags. */
235 enum btrace_thread_flag
237 /* The thread is to be stepped forwards. */
238 BTHR_STEP = (1 << 0),
240 /* The thread is to be stepped backwards. */
241 BTHR_RSTEP = (1 << 1),
243 /* The thread is to be continued forwards. */
244 BTHR_CONT = (1 << 2),
246 /* The thread is to be continued backwards. */
247 BTHR_RCONT = (1 << 3),
249 /* The thread is to be moved. */
250 BTHR_MOVE = (BTHR_STEP | BTHR_RSTEP | BTHR_CONT | BTHR_RCONT),
252 /* The thread is to be stopped. */
255 DEF_ENUM_FLAGS_TYPE (enum btrace_thread_flag, btrace_thread_flags);
257 #if defined (HAVE_LIBIPT)
259 struct btrace_pt_packet
261 /* The offset in the trace stream. */
264 /* The decode error code. */
265 enum pt_error_code errcode;
267 /* The decoded packet. Only valid if ERRCODE == pte_ok. */
268 struct pt_packet packet;
271 /* Define functions operating on a vector of packets. */
272 typedef struct btrace_pt_packet btrace_pt_packet_s;
273 DEF_VEC_O (btrace_pt_packet_s);
274 #endif /* defined (HAVE_LIBIPT) */
276 /* Branch trace iteration state for "maintenance btrace packet-history". */
277 struct btrace_maint_packet_history
279 /* The branch trace packet range from BEGIN (inclusive) to
280 END (exclusive) that has been covered last time. */
285 /* Branch trace maintenance information per thread.
287 This information is used by "maintenance btrace" commands. */
288 struct btrace_maint_info
290 /* Most information is format-specific.
291 The format can be found in the BTRACE.DATA.FORMAT field of each thread. */
294 /* BTRACE.DATA.FORMAT == BTRACE_FORMAT_BTS */
297 /* The packet history iterator.
298 We are iterating over BTRACE.DATA.FORMAT.VARIANT.BTS.BLOCKS. */
299 struct btrace_maint_packet_history packet_history;
302 #if defined (HAVE_LIBIPT)
303 /* BTRACE.DATA.FORMAT == BTRACE_FORMAT_PT */
306 /* A vector of decoded packets. */
307 VEC (btrace_pt_packet_s) *packets;
309 /* The packet history iterator.
310 We are iterating over the above PACKETS vector. */
311 struct btrace_maint_packet_history packet_history;
313 #endif /* defined (HAVE_LIBIPT) */
317 /* Branch trace information per thread.
319 This represents the branch trace configuration as well as the entry point
320 into the branch trace data. For the latter, it also contains the index into
321 an array of branch trace blocks used for iterating though the branch trace
322 blocks of a thread. */
323 struct btrace_thread_info
325 /* The target branch trace information for this thread.
327 This contains the branch trace configuration as well as any
328 target-specific information necessary for implementing branch tracing on
329 the underlying architecture. */
330 struct btrace_target_info *target;
332 /* The raw branch trace data for the below branch trace. */
333 struct btrace_data data;
335 /* The current branch trace for this thread (both inclusive).
337 The last instruction of END is the current instruction, which is not
338 part of the execution history.
339 Both will be NULL if there is no branch trace available. If there is
340 branch trace available, both will be non-NULL. */
341 struct btrace_function *begin;
342 struct btrace_function *end;
344 /* Vector of pointer to decoded function segments. These are in execution
345 order with the first element == BEGIN and the last element == END. */
346 std::vector<btrace_function *> functions;
348 /* The function level offset. When added to each function's LEVEL,
349 this normalizes the function levels such that the smallest level
353 /* The number of gaps in the trace. */
356 /* A bit-vector of btrace_thread_flag. */
357 btrace_thread_flags flags;
359 /* The instruction history iterator. */
360 struct btrace_insn_history *insn_history;
362 /* The function call history iterator. */
363 struct btrace_call_history *call_history;
365 /* The current replay position. NULL if not replaying.
366 Gaps are skipped during replay, so REPLAY always points to a valid
368 struct btrace_insn_iterator *replay;
370 /* Why the thread stopped, if we need to track it. */
371 enum target_stop_reason stop_reason;
373 /* Maintenance information. */
374 struct btrace_maint_info maint;
377 /* Enable branch tracing for a thread. */
378 extern void btrace_enable (struct thread_info *tp,
379 const struct btrace_config *conf);
381 /* Get the branch trace configuration for a thread.
382 Return NULL if branch tracing is not enabled for that thread. */
383 extern const struct btrace_config *
384 btrace_conf (const struct btrace_thread_info *);
386 /* Disable branch tracing for a thread.
387 This will also delete the current branch trace data. */
388 extern void btrace_disable (struct thread_info *);
390 /* Disable branch tracing for a thread during teardown.
391 This is similar to btrace_disable, except that it will use
392 target_teardown_btrace instead of target_disable_btrace. */
393 extern void btrace_teardown (struct thread_info *);
395 /* Return a human readable error string for the given ERRCODE in FORMAT.
396 The pointer will never be NULL and must not be freed. */
398 extern const char *btrace_decode_error (enum btrace_format format, int errcode);
400 /* Fetch the branch trace for a single thread. */
401 extern void btrace_fetch (struct thread_info *);
403 /* Clear the branch trace for a single thread. */
404 extern void btrace_clear (struct thread_info *);
406 /* Clear the branch trace for all threads when an object file goes away. */
407 extern void btrace_free_objfile (struct objfile *);
409 /* Parse a branch trace xml document XML into DATA. */
410 extern void parse_xml_btrace (struct btrace_data *data, const char *xml);
412 /* Parse a branch trace configuration xml document XML into CONF. */
413 extern void parse_xml_btrace_conf (struct btrace_config *conf, const char *xml);
415 /* Dereference a branch trace instruction iterator. Return a pointer to the
416 instruction the iterator points to.
417 May return NULL if the iterator points to a gap in the trace. */
418 extern const struct btrace_insn *
419 btrace_insn_get (const struct btrace_insn_iterator *);
421 /* Return the error code for a branch trace instruction iterator. Returns zero
422 if there is no error, i.e. the instruction is valid. */
423 extern int btrace_insn_get_error (const struct btrace_insn_iterator *);
425 /* Return the instruction number for a branch trace iterator.
426 Returns one past the maximum instruction number for the end iterator. */
427 extern unsigned int btrace_insn_number (const struct btrace_insn_iterator *);
429 /* Initialize a branch trace instruction iterator to point to the begin/end of
430 the branch trace. Throws an error if there is no branch trace. */
431 extern void btrace_insn_begin (struct btrace_insn_iterator *,
432 const struct btrace_thread_info *);
433 extern void btrace_insn_end (struct btrace_insn_iterator *,
434 const struct btrace_thread_info *);
436 /* Increment/decrement a branch trace instruction iterator by at most STRIDE
437 instructions. Return the number of instructions by which the instruction
438 iterator has been advanced.
439 Returns zero, if the operation failed or STRIDE had been zero. */
440 extern unsigned int btrace_insn_next (struct btrace_insn_iterator *,
441 unsigned int stride);
442 extern unsigned int btrace_insn_prev (struct btrace_insn_iterator *,
443 unsigned int stride);
445 /* Compare two branch trace instruction iterators.
446 Return a negative number if LHS < RHS.
447 Return zero if LHS == RHS.
448 Return a positive number if LHS > RHS. */
449 extern int btrace_insn_cmp (const struct btrace_insn_iterator *lhs,
450 const struct btrace_insn_iterator *rhs);
452 /* Find an instruction or gap in the function branch trace by its number.
453 If the instruction is found, initialize the branch trace instruction
454 iterator to point to this instruction and return non-zero.
455 Return zero otherwise. */
456 extern int btrace_find_insn_by_number (struct btrace_insn_iterator *,
457 const struct btrace_thread_info *,
458 unsigned int number);
460 /* Dereference a branch trace call iterator. Return a pointer to the
461 function the iterator points to or NULL if the interator points past
462 the end of the branch trace. */
463 extern const struct btrace_function *
464 btrace_call_get (const struct btrace_call_iterator *);
466 /* Return the function number for a branch trace call iterator.
467 Returns one past the maximum function number for the end iterator.
468 Returns zero if the iterator does not point to a valid function. */
469 extern unsigned int btrace_call_number (const struct btrace_call_iterator *);
471 /* Initialize a branch trace call iterator to point to the begin/end of
472 the branch trace. Throws an error if there is no branch trace. */
473 extern void btrace_call_begin (struct btrace_call_iterator *,
474 const struct btrace_thread_info *);
475 extern void btrace_call_end (struct btrace_call_iterator *,
476 const struct btrace_thread_info *);
478 /* Increment/decrement a branch trace call iterator by at most STRIDE function
479 segments. Return the number of function segments by which the call
480 iterator has been advanced.
481 Returns zero, if the operation failed or STRIDE had been zero. */
482 extern unsigned int btrace_call_next (struct btrace_call_iterator *,
483 unsigned int stride);
484 extern unsigned int btrace_call_prev (struct btrace_call_iterator *,
485 unsigned int stride);
487 /* Compare two branch trace call iterators.
488 Return a negative number if LHS < RHS.
489 Return zero if LHS == RHS.
490 Return a positive number if LHS > RHS. */
491 extern int btrace_call_cmp (const struct btrace_call_iterator *lhs,
492 const struct btrace_call_iterator *rhs);
494 /* Find a function in the function branch trace by its NUMBER.
495 If the function is found, initialize the branch trace call
496 iterator to point to this function and return non-zero.
497 Return zero otherwise. */
498 extern int btrace_find_call_by_number (struct btrace_call_iterator *,
499 const struct btrace_thread_info *,
500 unsigned int number);
502 /* Set the branch trace instruction history from BEGIN (inclusive) to
504 extern void btrace_set_insn_history (struct btrace_thread_info *,
505 const struct btrace_insn_iterator *begin,
506 const struct btrace_insn_iterator *end);
508 /* Set the branch trace function call history from BEGIN (inclusive) to
510 extern void btrace_set_call_history (struct btrace_thread_info *,
511 const struct btrace_call_iterator *begin,
512 const struct btrace_call_iterator *end);
514 /* Determine if branch tracing is currently replaying TP. */
515 extern int btrace_is_replaying (struct thread_info *tp);
517 /* Return non-zero if the branch trace for TP is empty; zero otherwise. */
518 extern int btrace_is_empty (struct thread_info *tp);
520 /* Create a cleanup for DATA. */
521 extern struct cleanup *make_cleanup_btrace_data (struct btrace_data *data);
523 #endif /* BTRACE_H */