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/>. */
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"
32 struct btrace_function
;
34 /* A coarse instruction classification. */
35 enum btrace_insn_class
37 /* The instruction is something not listed below. */
40 /* The instruction is a function call. */
43 /* The instruction is a function return. */
46 /* The instruction is an unconditional jump. */
50 /* A branch trace instruction.
52 This represents a single instruction in a branch trace. */
55 /* The address of this instruction. */
58 /* The size of this instruction in bytes. */
61 /* The instruction class of this instruction. */
62 enum btrace_insn_class iclass
;
65 /* A vector of branch trace instructions. */
66 typedef struct btrace_insn btrace_insn_s
;
67 DEF_VEC_O (btrace_insn_s
);
69 /* A doubly-linked list of branch trace function segments. */
70 struct btrace_func_link
72 struct btrace_function
*prev
;
73 struct btrace_function
*next
;
76 /* Flags for btrace function segments. */
77 enum btrace_function_flag
79 /* The 'up' link interpretation.
80 If set, it points to the function segment we returned to.
81 If clear, it points to the function segment we called from. */
82 BFUN_UP_LINKS_TO_RET
= (1 << 0),
84 /* The 'up' link points to a tail call. This obviously only makes sense
85 if bfun_up_links_to_ret is clear. */
86 BFUN_UP_LINKS_TO_TAILCALL
= (1 << 1)
89 /* Decode errors for the BTS recording format. */
92 /* The instruction trace overflowed the end of the trace block. */
95 /* The instruction size could not be determined. */
99 /* A branch trace function segment.
101 This represents a function segment in a branch trace, i.e. a consecutive
102 number of instructions belonging to the same function.
104 In case of decode errors, we add an empty function segment to indicate
105 the gap in the trace.
107 We do not allow function segments without instructions otherwise. */
108 struct btrace_function
110 /* The full and minimal symbol for the function. Both may be NULL. */
111 struct minimal_symbol
*msym
;
114 /* The previous and next segment belonging to the same function.
115 If a function calls another function, the former will have at least
116 two segments: one before the call and another after the return. */
117 struct btrace_func_link segment
;
119 /* The previous and next function in control flow order. */
120 struct btrace_func_link flow
;
122 /* The directly preceding function segment in a (fake) call stack. */
123 struct btrace_function
*up
;
125 /* The instructions in this function segment.
126 The instruction vector will be empty if the function segment
127 represents a decode error. */
128 VEC (btrace_insn_s
) *insn
;
130 /* The error code of a decode error that led to a gap.
131 Must be zero unless INSN is empty; non-zero otherwise. */
134 /* The instruction number offset for the first instruction in this
136 If INSN is empty this is the insn_offset of the succeding function
137 segment in control-flow order. */
138 unsigned int insn_offset
;
140 /* The function number in control-flow order.
141 If INSN is empty indicating a gap in the trace due to a decode error,
142 we still count the gap as a function. */
145 /* The function level in a back trace across the entire branch trace.
146 A caller's level is one lower than the level of its callee.
148 Levels can be negative if we see returns for which we have not seen
149 the corresponding calls. The branch trace thread information provides
150 a fixup to normalize function levels so the smallest level is zero. */
153 /* A bit-vector of btrace_function_flag. */
154 enum btrace_function_flag flags
;
157 /* A branch trace instruction iterator. */
158 struct btrace_insn_iterator
160 /* The branch trace function segment containing the instruction.
161 Will never be NULL. */
162 const struct btrace_function
*function
;
164 /* The index into the function segment's instruction vector. */
168 /* A branch trace function call iterator. */
169 struct btrace_call_iterator
171 /* The branch trace information for this thread. Will never be NULL. */
172 const struct btrace_thread_info
*btinfo
;
174 /* The branch trace function segment.
175 This will be NULL for the iterator pointing to the end of the trace. */
176 const struct btrace_function
*function
;
179 /* Branch trace iteration state for "record instruction-history". */
180 struct btrace_insn_history
182 /* The branch trace instruction range from BEGIN (inclusive) to
183 END (exclusive) that has been covered last time. */
184 struct btrace_insn_iterator begin
;
185 struct btrace_insn_iterator end
;
188 /* Branch trace iteration state for "record function-call-history". */
189 struct btrace_call_history
191 /* The branch trace function range from BEGIN (inclusive) to END (exclusive)
192 that has been covered last time. */
193 struct btrace_call_iterator begin
;
194 struct btrace_call_iterator end
;
197 /* Branch trace thread flags. */
198 enum btrace_thread_flag
200 /* The thread is to be stepped forwards. */
201 BTHR_STEP
= (1 << 0),
203 /* The thread is to be stepped backwards. */
204 BTHR_RSTEP
= (1 << 1),
206 /* The thread is to be continued forwards. */
207 BTHR_CONT
= (1 << 2),
209 /* The thread is to be continued backwards. */
210 BTHR_RCONT
= (1 << 3),
212 /* The thread is to be moved. */
213 BTHR_MOVE
= (BTHR_STEP
| BTHR_RSTEP
| BTHR_CONT
| BTHR_RCONT
)
216 /* Branch trace information per thread.
218 This represents the branch trace configuration as well as the entry point
219 into the branch trace data. For the latter, it also contains the index into
220 an array of branch trace blocks used for iterating though the branch trace
221 blocks of a thread. */
222 struct btrace_thread_info
224 /* The target branch trace information for this thread.
226 This contains the branch trace configuration as well as any
227 target-specific information necessary for implementing branch tracing on
228 the underlying architecture. */
229 struct btrace_target_info
*target
;
231 /* The current branch trace for this thread (both inclusive).
233 The last instruction of END is the current instruction, which is not
234 part of the execution history.
235 Both will be NULL if there is no branch trace available. If there is
236 branch trace available, both will be non-NULL. */
237 struct btrace_function
*begin
;
238 struct btrace_function
*end
;
240 /* The function level offset. When added to each function's LEVEL,
241 this normalizes the function levels such that the smallest level
245 /* The number of gaps in the trace. */
248 /* A bit-vector of btrace_thread_flag. */
249 enum btrace_thread_flag flags
;
251 /* The instruction history iterator. */
252 struct btrace_insn_history
*insn_history
;
254 /* The function call history iterator. */
255 struct btrace_call_history
*call_history
;
257 /* The current replay position. NULL if not replaying.
258 Gaps are skipped during replay, so REPLAY always points to a valid
260 struct btrace_insn_iterator
*replay
;
263 /* Enable branch tracing for a thread. */
264 extern void btrace_enable (struct thread_info
*tp
,
265 const struct btrace_config
*conf
);
267 /* Get the branch trace configuration for a thread.
268 Return NULL if branch tracing is not enabled for that thread. */
269 extern const struct btrace_config
*
270 btrace_conf (const struct btrace_thread_info
*);
272 /* Disable branch tracing for a thread.
273 This will also delete the current branch trace data. */
274 extern void btrace_disable (struct thread_info
*);
276 /* Disable branch tracing for a thread during teardown.
277 This is similar to btrace_disable, except that it will use
278 target_teardown_btrace instead of target_disable_btrace. */
279 extern void btrace_teardown (struct thread_info
*);
281 /* Fetch the branch trace for a single thread. */
282 extern void btrace_fetch (struct thread_info
*);
284 /* Clear the branch trace for a single thread. */
285 extern void btrace_clear (struct thread_info
*);
287 /* Clear the branch trace for all threads when an object file goes away. */
288 extern void btrace_free_objfile (struct objfile
*);
290 /* Parse a branch trace xml document XML into DATA. */
291 extern void parse_xml_btrace (struct btrace_data
*data
, const char *xml
);
293 /* Parse a branch trace configuration xml document XML into CONF. */
294 extern void parse_xml_btrace_conf (struct btrace_config
*conf
, const char *xml
);
296 /* Dereference a branch trace instruction iterator. Return a pointer to the
297 instruction the iterator points to.
298 May return NULL if the iterator points to a gap in the trace. */
299 extern const struct btrace_insn
*
300 btrace_insn_get (const struct btrace_insn_iterator
*);
302 /* Return the instruction number for a branch trace iterator.
303 Returns one past the maximum instruction number for the end iterator.
304 Returns zero if the iterator does not point to a valid instruction. */
305 extern unsigned int btrace_insn_number (const struct btrace_insn_iterator
*);
307 /* Initialize a branch trace instruction iterator to point to the begin/end of
308 the branch trace. Throws an error if there is no branch trace. */
309 extern void btrace_insn_begin (struct btrace_insn_iterator
*,
310 const struct btrace_thread_info
*);
311 extern void btrace_insn_end (struct btrace_insn_iterator
*,
312 const struct btrace_thread_info
*);
314 /* Increment/decrement a branch trace instruction iterator by at most STRIDE
315 instructions. Return the number of instructions by which the instruction
316 iterator has been advanced.
317 Returns zero, if the operation failed or STRIDE had been zero. */
318 extern unsigned int btrace_insn_next (struct btrace_insn_iterator
*,
319 unsigned int stride
);
320 extern unsigned int btrace_insn_prev (struct btrace_insn_iterator
*,
321 unsigned int stride
);
323 /* Compare two branch trace instruction iterators.
324 Return a negative number if LHS < RHS.
325 Return zero if LHS == RHS.
326 Return a positive number if LHS > RHS. */
327 extern int btrace_insn_cmp (const struct btrace_insn_iterator
*lhs
,
328 const struct btrace_insn_iterator
*rhs
);
330 /* Find an instruction in the function branch trace by its number.
331 If the instruction is found, initialize the branch trace instruction
332 iterator to point to this instruction and return non-zero.
333 Return zero otherwise. */
334 extern int btrace_find_insn_by_number (struct btrace_insn_iterator
*,
335 const struct btrace_thread_info
*,
336 unsigned int number
);
338 /* Dereference a branch trace call iterator. Return a pointer to the
339 function the iterator points to or NULL if the interator points past
340 the end of the branch trace. */
341 extern const struct btrace_function
*
342 btrace_call_get (const struct btrace_call_iterator
*);
344 /* Return the function number for a branch trace call iterator.
345 Returns one past the maximum function number for the end iterator.
346 Returns zero if the iterator does not point to a valid function. */
347 extern unsigned int btrace_call_number (const struct btrace_call_iterator
*);
349 /* Initialize a branch trace call iterator to point to the begin/end of
350 the branch trace. Throws an error if there is no branch trace. */
351 extern void btrace_call_begin (struct btrace_call_iterator
*,
352 const struct btrace_thread_info
*);
353 extern void btrace_call_end (struct btrace_call_iterator
*,
354 const struct btrace_thread_info
*);
356 /* Increment/decrement a branch trace call iterator by at most STRIDE function
357 segments. Return the number of function segments by which the call
358 iterator has been advanced.
359 Returns zero, if the operation failed or STRIDE had been zero. */
360 extern unsigned int btrace_call_next (struct btrace_call_iterator
*,
361 unsigned int stride
);
362 extern unsigned int btrace_call_prev (struct btrace_call_iterator
*,
363 unsigned int stride
);
365 /* Compare two branch trace call iterators.
366 Return a negative number if LHS < RHS.
367 Return zero if LHS == RHS.
368 Return a positive number if LHS > RHS. */
369 extern int btrace_call_cmp (const struct btrace_call_iterator
*lhs
,
370 const struct btrace_call_iterator
*rhs
);
372 /* Find a function in the function branch trace by its NUMBER.
373 If the function is found, initialize the branch trace call
374 iterator to point to this function and return non-zero.
375 Return zero otherwise. */
376 extern int btrace_find_call_by_number (struct btrace_call_iterator
*,
377 const struct btrace_thread_info
*,
378 unsigned int number
);
380 /* Set the branch trace instruction history from BEGIN (inclusive) to
382 extern void btrace_set_insn_history (struct btrace_thread_info
*,
383 const struct btrace_insn_iterator
*begin
,
384 const struct btrace_insn_iterator
*end
);
386 /* Set the branch trace function call history from BEGIN (inclusive) to
388 extern void btrace_set_call_history (struct btrace_thread_info
*,
389 const struct btrace_call_iterator
*begin
,
390 const struct btrace_call_iterator
*end
);
392 /* Determine if branch tracing is currently replaying TP. */
393 extern int btrace_is_replaying (struct thread_info
*tp
);
395 /* Return non-zero if the branch trace for TP is empty; zero otherwise. */
396 extern int btrace_is_empty (struct thread_info
*tp
);
398 /* Create a cleanup for DATA. */
399 extern struct cleanup
*make_cleanup_btrace_data (struct btrace_data
*data
);
401 #endif /* BTRACE_H */