1 /* Branch trace support for GDB, the GNU debugger.
3 Copyright (C) 2013-2014 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 branch trace instruction.
36 This represents a single instruction in a branch trace. */
39 /* The address of this instruction. */
43 /* A vector of branch trace instructions. */
44 typedef struct btrace_insn btrace_insn_s;
45 DEF_VEC_O (btrace_insn_s);
47 /* A doubly-linked list of branch trace function segments. */
48 struct btrace_func_link
50 struct btrace_function *prev;
51 struct btrace_function *next;
54 /* Flags for btrace function segments. */
55 enum btrace_function_flag
57 /* The 'up' link interpretation.
58 If set, it points to the function segment we returned to.
59 If clear, it points to the function segment we called from. */
60 BFUN_UP_LINKS_TO_RET = (1 << 0),
62 /* The 'up' link points to a tail call. This obviously only makes sense
63 if bfun_up_links_to_ret is clear. */
64 BFUN_UP_LINKS_TO_TAILCALL = (1 << 1)
67 /* A branch trace function segment.
69 This represents a function segment in a branch trace, i.e. a consecutive
70 number of instructions belonging to the same function.
72 We do not allow function segments without any instructions. */
73 struct btrace_function
75 /* The full and minimal symbol for the function. Both may be NULL. */
76 struct minimal_symbol *msym;
79 /* The previous and next segment belonging to the same function.
80 If a function calls another function, the former will have at least
81 two segments: one before the call and another after the return. */
82 struct btrace_func_link segment;
84 /* The previous and next function in control flow order. */
85 struct btrace_func_link flow;
87 /* The directly preceding function segment in a (fake) call stack. */
88 struct btrace_function *up;
90 /* The instructions in this function segment.
91 The instruction vector will never be empty. */
92 VEC (btrace_insn_s) *insn;
94 /* The instruction number offset for the first instruction in this
96 unsigned int insn_offset;
98 /* The function number in control-flow order. */
101 /* The function level in a back trace across the entire branch trace.
102 A caller's level is one lower than the level of its callee.
104 Levels can be negative if we see returns for which we have not seen
105 the corresponding calls. The branch trace thread information provides
106 a fixup to normalize function levels so the smallest level is zero. */
109 /* The source line range of this function segment (both inclusive). */
112 /* A bit-vector of btrace_function_flag. */
113 enum btrace_function_flag flags;
116 /* A branch trace instruction iterator. */
117 struct btrace_insn_iterator
119 /* The branch trace function segment containing the instruction.
120 Will never be NULL. */
121 const struct btrace_function *function;
123 /* The index into the function segment's instruction vector. */
127 /* A branch trace function call iterator. */
128 struct btrace_call_iterator
130 /* The branch trace information for this thread. Will never be NULL. */
131 const struct btrace_thread_info *btinfo;
133 /* The branch trace function segment.
134 This will be NULL for the iterator pointing to the end of the trace. */
135 const struct btrace_function *function;
138 /* Branch trace iteration state for "record instruction-history". */
139 struct btrace_insn_history
141 /* The branch trace instruction range from BEGIN (inclusive) to
142 END (exclusive) that has been covered last time. */
143 struct btrace_insn_iterator begin;
144 struct btrace_insn_iterator end;
147 /* Branch trace iteration state for "record function-call-history". */
148 struct btrace_call_history
150 /* The branch trace function range from BEGIN (inclusive) to END (exclusive)
151 that has been covered last time. */
152 struct btrace_call_iterator begin;
153 struct btrace_call_iterator end;
156 /* Branch trace thread flags. */
157 enum btrace_thread_flag
159 /* The thread is to be stepped forwards. */
160 BTHR_STEP = (1 << 0),
162 /* The thread is to be stepped backwards. */
163 BTHR_RSTEP = (1 << 1),
165 /* The thread is to be continued forwards. */
166 BTHR_CONT = (1 << 2),
168 /* The thread is to be continued backwards. */
169 BTHR_RCONT = (1 << 3),
171 /* The thread is to be moved. */
172 BTHR_MOVE = (BTHR_STEP | BTHR_RSTEP | BTHR_CONT | BTHR_RCONT)
175 /* Branch trace information per thread.
177 This represents the branch trace configuration as well as the entry point
178 into the branch trace data. For the latter, it also contains the index into
179 an array of branch trace blocks used for iterating though the branch trace
180 blocks of a thread. */
181 struct btrace_thread_info
183 /* The target branch trace information for this thread.
185 This contains the branch trace configuration as well as any
186 target-specific information necessary for implementing branch tracing on
187 the underlying architecture. */
188 struct btrace_target_info *target;
190 /* The current branch trace for this thread (both inclusive).
192 The last instruction of END is the current instruction, which is not
193 part of the execution history.
194 Both will be NULL if there is no branch trace available. If there is
195 branch trace available, both will be non-NULL. */
196 struct btrace_function *begin;
197 struct btrace_function *end;
199 /* The function level offset. When added to each function's LEVEL,
200 this normalizes the function levels such that the smallest level
204 /* A bit-vector of btrace_thread_flag. */
205 enum btrace_thread_flag flags;
207 /* The instruction history iterator. */
208 struct btrace_insn_history *insn_history;
210 /* The function call history iterator. */
211 struct btrace_call_history *call_history;
213 /* The current replay position. NULL if not replaying. */
214 struct btrace_insn_iterator *replay;
217 /* Enable branch tracing for a thread. */
218 extern void btrace_enable (struct thread_info *tp);
220 /* Disable branch tracing for a thread.
221 This will also delete the current branch trace data. */
222 extern void btrace_disable (struct thread_info *);
224 /* Disable branch tracing for a thread during teardown.
225 This is similar to btrace_disable, except that it will use
226 target_teardown_btrace instead of target_disable_btrace. */
227 extern void btrace_teardown (struct thread_info *);
229 /* Fetch the branch trace for a single thread. */
230 extern void btrace_fetch (struct thread_info *);
232 /* Clear the branch trace for a single thread. */
233 extern void btrace_clear (struct thread_info *);
235 /* Clear the branch trace for all threads when an object file goes away. */
236 extern void btrace_free_objfile (struct objfile *);
238 /* Parse a branch trace xml document into a block vector. */
239 extern VEC (btrace_block_s) *parse_xml_btrace (const char*);
241 /* Dereference a branch trace instruction iterator. Return a pointer to the
242 instruction the iterator points to. */
243 extern const struct btrace_insn *
244 btrace_insn_get (const struct btrace_insn_iterator *);
246 /* Return the instruction number for a branch trace iterator.
247 Returns one past the maximum instruction number for the end iterator.
248 Returns zero if the iterator does not point to a valid instruction. */
249 extern unsigned int btrace_insn_number (const struct btrace_insn_iterator *);
251 /* Initialize a branch trace instruction iterator to point to the begin/end of
252 the branch trace. Throws an error if there is no branch trace. */
253 extern void btrace_insn_begin (struct btrace_insn_iterator *,
254 const struct btrace_thread_info *);
255 extern void btrace_insn_end (struct btrace_insn_iterator *,
256 const struct btrace_thread_info *);
258 /* Increment/decrement a branch trace instruction iterator by at most STRIDE
259 instructions. Return the number of instructions by which the instruction
260 iterator has been advanced.
261 Returns zero, if the operation failed or STRIDE had been zero. */
262 extern unsigned int btrace_insn_next (struct btrace_insn_iterator *,
263 unsigned int stride);
264 extern unsigned int btrace_insn_prev (struct btrace_insn_iterator *,
265 unsigned int stride);
267 /* Compare two branch trace instruction iterators.
268 Return a negative number if LHS < RHS.
269 Return zero if LHS == RHS.
270 Return a positive number if LHS > RHS. */
271 extern int btrace_insn_cmp (const struct btrace_insn_iterator *lhs,
272 const struct btrace_insn_iterator *rhs);
274 /* Find an instruction in the function branch trace by its number.
275 If the instruction is found, initialize the branch trace instruction
276 iterator to point to this instruction and return non-zero.
277 Return zero otherwise. */
278 extern int btrace_find_insn_by_number (struct btrace_insn_iterator *,
279 const struct btrace_thread_info *,
280 unsigned int number);
282 /* Dereference a branch trace call iterator. Return a pointer to the
283 function the iterator points to or NULL if the interator points past
284 the end of the branch trace. */
285 extern const struct btrace_function *
286 btrace_call_get (const struct btrace_call_iterator *);
288 /* Return the function number for a branch trace call iterator.
289 Returns one past the maximum function number for the end iterator.
290 Returns zero if the iterator does not point to a valid function. */
291 extern unsigned int btrace_call_number (const struct btrace_call_iterator *);
293 /* Initialize a branch trace call iterator to point to the begin/end of
294 the branch trace. Throws an error if there is no branch trace. */
295 extern void btrace_call_begin (struct btrace_call_iterator *,
296 const struct btrace_thread_info *);
297 extern void btrace_call_end (struct btrace_call_iterator *,
298 const struct btrace_thread_info *);
300 /* Increment/decrement a branch trace call iterator by at most STRIDE function
301 segments. Return the number of function segments by which the call
302 iterator has been advanced.
303 Returns zero, if the operation failed or STRIDE had been zero. */
304 extern unsigned int btrace_call_next (struct btrace_call_iterator *,
305 unsigned int stride);
306 extern unsigned int btrace_call_prev (struct btrace_call_iterator *,
307 unsigned int stride);
309 /* Compare two branch trace call iterators.
310 Return a negative number if LHS < RHS.
311 Return zero if LHS == RHS.
312 Return a positive number if LHS > RHS. */
313 extern int btrace_call_cmp (const struct btrace_call_iterator *lhs,
314 const struct btrace_call_iterator *rhs);
316 /* Find a function in the function branch trace by its NUMBER.
317 If the function is found, initialize the branch trace call
318 iterator to point to this function and return non-zero.
319 Return zero otherwise. */
320 extern int btrace_find_call_by_number (struct btrace_call_iterator *,
321 const struct btrace_thread_info *,
322 unsigned int number);
324 /* Set the branch trace instruction history from BEGIN (inclusive) to
326 extern void btrace_set_insn_history (struct btrace_thread_info *,
327 const struct btrace_insn_iterator *begin,
328 const struct btrace_insn_iterator *end);
330 /* Set the branch trace function call history from BEGIN (inclusive) to
332 extern void btrace_set_call_history (struct btrace_thread_info *,
333 const struct btrace_call_iterator *begin,
334 const struct btrace_call_iterator *end);
336 /* Determine if branch tracing is currently replaying TP. */
337 extern int btrace_is_replaying (struct thread_info *tp);
339 /* Return non-zero if the branch trace for TP is empty; zero otherwise. */
340 extern int btrace_is_empty (struct thread_info *tp);
343 #endif /* BTRACE_H */