btrace: change branch trace data structure
[external/binutils.git] / gdb / btrace.h
1 /* Branch trace support for GDB, the GNU debugger.
2
3    Copyright (C) 2013-2014 Free Software Foundation, Inc.
4
5    Contributed by Intel Corp. <markus.t.metzger@intel.com>.
6
7    This file is part of GDB.
8
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.
13
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.
18
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/>.  */
21
22 #ifndef BTRACE_H
23 #define BTRACE_H
24
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.  */
28
29 #include "btrace-common.h"
30
31 struct thread_info;
32 struct btrace_function;
33
34 /* A branch trace instruction.
35
36    This represents a single instruction in a branch trace.  */
37 struct btrace_insn
38 {
39   /* The address of this instruction.  */
40   CORE_ADDR pc;
41 };
42
43 /* A vector of branch trace instructions.  */
44 typedef struct btrace_insn btrace_insn_s;
45 DEF_VEC_O (btrace_insn_s);
46
47 /* A doubly-linked list of branch trace function segments.  */
48 struct btrace_func_link
49 {
50   struct btrace_function *prev;
51   struct btrace_function *next;
52 };
53
54 /* Flags for btrace function segments.  */
55 enum btrace_function_flag
56 {
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),
61
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)
65 };
66
67 /* A branch trace function segment.
68
69    This represents a function segment in a branch trace, i.e. a consecutive
70    number of instructions belonging to the same function.
71
72    We do not allow function segments without any instructions.  */
73 struct btrace_function
74 {
75   /* The full and minimal symbol for the function.  Both may be NULL.  */
76   struct minimal_symbol *msym;
77   struct symbol *sym;
78
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;
83
84   /* The previous and next function in control flow order.  */
85   struct btrace_func_link flow;
86
87   /* The directly preceding function segment in a (fake) call stack.  */
88   struct btrace_function *up;
89
90   /* The instructions in this function segment.
91      The instruction vector will never be empty.  */
92   VEC (btrace_insn_s) *insn;
93
94   /* The instruction number offset for the first instruction in this
95      function segment.  */
96   unsigned int insn_offset;
97
98   /* The function number in control-flow order.  */
99   unsigned int number;
100
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.
103
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.  */
107   int level;
108
109   /* The source line range of this function segment (both inclusive).  */
110   int lbegin, lend;
111
112   /* A bit-vector of btrace_function_flag.  */
113   enum btrace_function_flag flags;
114 };
115
116 /* A branch trace instruction iterator.  */
117 struct btrace_insn_iterator
118 {
119   /* The branch trace function segment containing the instruction.
120      Will never be NULL.  */
121   const struct btrace_function *function;
122
123   /* The index into the function segment's instruction vector.  */
124   unsigned int index;
125 };
126
127 /* A branch trace function call iterator.  */
128 struct btrace_call_iterator
129 {
130   /* The branch trace information for this thread.  Will never be NULL.  */
131   const struct btrace_thread_info *btinfo;
132
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;
136 };
137
138 /* Branch trace iteration state for "record instruction-history".  */
139 struct btrace_insn_history
140 {
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;
145 };
146
147 /* Branch trace iteration state for "record function-call-history".  */
148 struct btrace_call_history
149 {
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;
154 };
155
156 /* Branch trace information per thread.
157
158    This represents the branch trace configuration as well as the entry point
159    into the branch trace data.  For the latter, it also contains the index into
160    an array of branch trace blocks used for iterating though the branch trace
161    blocks of a thread.  */
162 struct btrace_thread_info
163 {
164   /* The target branch trace information for this thread.
165
166      This contains the branch trace configuration as well as any
167      target-specific information necessary for implementing branch tracing on
168      the underlying architecture.  */
169   struct btrace_target_info *target;
170
171   /* The current branch trace for this thread (both inclusive).
172
173      The last instruction of END is the current instruction, which is not
174      part of the execution history.
175      Both will be NULL if there is no branch trace available.  If there is
176      branch trace available, both will be non-NULL.  */
177   struct btrace_function *begin;
178   struct btrace_function *end;
179
180   /* The function level offset.  When added to each function's LEVEL,
181      this normalizes the function levels such that the smallest level
182      becomes zero.  */
183   int level;
184
185   /* The instruction history iterator.  */
186   struct btrace_insn_history *insn_history;
187
188   /* The function call history iterator.  */
189   struct btrace_call_history *call_history;
190 };
191
192 /* Enable branch tracing for a thread.  */
193 extern void btrace_enable (struct thread_info *tp);
194
195 /* Disable branch tracing for a thread.
196    This will also delete the current branch trace data.  */
197 extern void btrace_disable (struct thread_info *);
198
199 /* Disable branch tracing for a thread during teardown.
200    This is similar to btrace_disable, except that it will use
201    target_teardown_btrace instead of target_disable_btrace.  */
202 extern void btrace_teardown (struct thread_info *);
203
204 /* Fetch the branch trace for a single thread.  */
205 extern void btrace_fetch (struct thread_info *);
206
207 /* Clear the branch trace for a single thread.  */
208 extern void btrace_clear (struct thread_info *);
209
210 /* Clear the branch trace for all threads when an object file goes away.  */
211 extern void btrace_free_objfile (struct objfile *);
212
213 /* Parse a branch trace xml document into a block vector.  */
214 extern VEC (btrace_block_s) *parse_xml_btrace (const char*);
215
216 /* Dereference a branch trace instruction iterator.  Return a pointer to the
217    instruction the iterator points to.  */
218 extern const struct btrace_insn *
219   btrace_insn_get (const struct btrace_insn_iterator *);
220
221 /* Return the instruction number for a branch trace iterator.
222    Returns one past the maximum instruction number for the end iterator.
223    Returns zero if the iterator does not point to a valid instruction.  */
224 extern unsigned int btrace_insn_number (const struct btrace_insn_iterator *);
225
226 /* Initialize a branch trace instruction iterator to point to the begin/end of
227    the branch trace.  Throws an error if there is no branch trace.  */
228 extern void btrace_insn_begin (struct btrace_insn_iterator *,
229                                const struct btrace_thread_info *);
230 extern void btrace_insn_end (struct btrace_insn_iterator *,
231                              const struct btrace_thread_info *);
232
233 /* Increment/decrement a branch trace instruction iterator by at most STRIDE
234    instructions.  Return the number of instructions by which the instruction
235    iterator has been advanced.
236    Returns zero, if the operation failed or STRIDE had been zero.  */
237 extern unsigned int btrace_insn_next (struct btrace_insn_iterator *,
238                                       unsigned int stride);
239 extern unsigned int btrace_insn_prev (struct btrace_insn_iterator *,
240                                       unsigned int stride);
241
242 /* Compare two branch trace instruction iterators.
243    Return a negative number if LHS < RHS.
244    Return zero if LHS == RHS.
245    Return a positive number if LHS > RHS.  */
246 extern int btrace_insn_cmp (const struct btrace_insn_iterator *lhs,
247                             const struct btrace_insn_iterator *rhs);
248
249 /* Find an instruction in the function branch trace by its number.
250    If the instruction is found, initialize the branch trace instruction
251    iterator to point to this instruction and return non-zero.
252    Return zero otherwise.  */
253 extern int btrace_find_insn_by_number (struct btrace_insn_iterator *,
254                                        const struct btrace_thread_info *,
255                                        unsigned int number);
256
257 /* Dereference a branch trace call iterator.  Return a pointer to the
258    function the iterator points to or NULL if the interator points past
259    the end of the branch trace.  */
260 extern const struct btrace_function *
261   btrace_call_get (const struct btrace_call_iterator *);
262
263 /* Return the function number for a branch trace call iterator.
264    Returns one past the maximum function number for the end iterator.
265    Returns zero if the iterator does not point to a valid function.  */
266 extern unsigned int btrace_call_number (const struct btrace_call_iterator *);
267
268 /* Initialize a branch trace call iterator to point to the begin/end of
269    the branch trace.  Throws an error if there is no branch trace.  */
270 extern void btrace_call_begin (struct btrace_call_iterator *,
271                                const struct btrace_thread_info *);
272 extern void btrace_call_end (struct btrace_call_iterator *,
273                              const struct btrace_thread_info *);
274
275 /* Increment/decrement a branch trace call iterator by at most STRIDE function
276    segments.  Return the number of function segments by which the call
277    iterator has been advanced.
278    Returns zero, if the operation failed or STRIDE had been zero.  */
279 extern unsigned int btrace_call_next (struct btrace_call_iterator *,
280                                       unsigned int stride);
281 extern unsigned int btrace_call_prev (struct btrace_call_iterator *,
282                                       unsigned int stride);
283
284 /* Compare two branch trace call iterators.
285    Return a negative number if LHS < RHS.
286    Return zero if LHS == RHS.
287    Return a positive number if LHS > RHS.  */
288 extern int btrace_call_cmp (const struct btrace_call_iterator *lhs,
289                             const struct btrace_call_iterator *rhs);
290
291 /* Find a function in the function branch trace by its NUMBER.
292    If the function is found, initialize the branch trace call
293    iterator to point to this function and return non-zero.
294    Return zero otherwise.  */
295 extern int btrace_find_call_by_number (struct btrace_call_iterator *,
296                                        const struct btrace_thread_info *,
297                                        unsigned int number);
298
299 /* Set the branch trace instruction history from BEGIN (inclusive) to
300    END (exclusive).  */
301 extern void btrace_set_insn_history (struct btrace_thread_info *,
302                                      const struct btrace_insn_iterator *begin,
303                                      const struct btrace_insn_iterator *end);
304
305 /* Set the branch trace function call history from BEGIN (inclusive) to
306    END (exclusive).  */
307 extern void btrace_set_call_history (struct btrace_thread_info *,
308                                      const struct btrace_call_iterator *begin,
309                                      const struct btrace_call_iterator *end);
310
311 #endif /* BTRACE_H */