record-btrace: indicate gaps
[external/binutils.git] / gdb / btrace.h
1 /* Branch trace support for GDB, the GNU debugger.
2
3    Copyright (C) 2013-2015 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 coarse instruction classification.  */
35 enum btrace_insn_class
36 {
37   /* The instruction is something not listed below.  */
38   BTRACE_INSN_OTHER,
39
40   /* The instruction is a function call.  */
41   BTRACE_INSN_CALL,
42
43   /* The instruction is a function return.  */
44   BTRACE_INSN_RETURN,
45
46   /* The instruction is an unconditional jump.  */
47   BTRACE_INSN_JUMP
48 };
49
50 /* A branch trace instruction.
51
52    This represents a single instruction in a branch trace.  */
53 struct btrace_insn
54 {
55   /* The address of this instruction.  */
56   CORE_ADDR pc;
57
58   /* The size of this instruction in bytes.  */
59   gdb_byte size;
60
61   /* The instruction class of this instruction.  */
62   enum btrace_insn_class iclass;
63 };
64
65 /* A vector of branch trace instructions.  */
66 typedef struct btrace_insn btrace_insn_s;
67 DEF_VEC_O (btrace_insn_s);
68
69 /* A doubly-linked list of branch trace function segments.  */
70 struct btrace_func_link
71 {
72   struct btrace_function *prev;
73   struct btrace_function *next;
74 };
75
76 /* Flags for btrace function segments.  */
77 enum btrace_function_flag
78 {
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),
83
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)
87 };
88
89 /* Decode errors for the BTS recording format.  */
90 enum btrace_bts_error
91 {
92   /* The instruction trace overflowed the end of the trace block.  */
93   BDE_BTS_OVERFLOW = 1,
94
95   /* The instruction size could not be determined.  */
96   BDE_BTS_INSN_SIZE
97 };
98
99 /* A branch trace function segment.
100
101    This represents a function segment in a branch trace, i.e. a consecutive
102    number of instructions belonging to the same function.
103
104    In case of decode errors, we add an empty function segment to indicate
105    the gap in the trace.
106
107    We do not allow function segments without instructions otherwise.  */
108 struct btrace_function
109 {
110   /* The full and minimal symbol for the function.  Both may be NULL.  */
111   struct minimal_symbol *msym;
112   struct symbol *sym;
113
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;
118
119   /* The previous and next function in control flow order.  */
120   struct btrace_func_link flow;
121
122   /* The directly preceding function segment in a (fake) call stack.  */
123   struct btrace_function *up;
124
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;
129
130   /* The error code of a decode error that led to a gap.
131      Must be zero unless INSN is empty; non-zero otherwise.  */
132   int errcode;
133
134   /* The instruction number offset for the first instruction in this
135      function segment.
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;
139
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.  */
143   unsigned int number;
144
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.
147
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.  */
151   int level;
152
153   /* The source line range of this function segment (both inclusive).  */
154   int lbegin, lend;
155
156   /* A bit-vector of btrace_function_flag.  */
157   enum btrace_function_flag flags;
158 };
159
160 /* A branch trace instruction iterator.  */
161 struct btrace_insn_iterator
162 {
163   /* The branch trace function segment containing the instruction.
164      Will never be NULL.  */
165   const struct btrace_function *function;
166
167   /* The index into the function segment's instruction vector.  */
168   unsigned int index;
169 };
170
171 /* A branch trace function call iterator.  */
172 struct btrace_call_iterator
173 {
174   /* The branch trace information for this thread.  Will never be NULL.  */
175   const struct btrace_thread_info *btinfo;
176
177   /* The branch trace function segment.
178      This will be NULL for the iterator pointing to the end of the trace.  */
179   const struct btrace_function *function;
180 };
181
182 /* Branch trace iteration state for "record instruction-history".  */
183 struct btrace_insn_history
184 {
185   /* The branch trace instruction range from BEGIN (inclusive) to
186      END (exclusive) that has been covered last time.  */
187   struct btrace_insn_iterator begin;
188   struct btrace_insn_iterator end;
189 };
190
191 /* Branch trace iteration state for "record function-call-history".  */
192 struct btrace_call_history
193 {
194   /* The branch trace function range from BEGIN (inclusive) to END (exclusive)
195      that has been covered last time.  */
196   struct btrace_call_iterator begin;
197   struct btrace_call_iterator end;
198 };
199
200 /* Branch trace thread flags.  */
201 enum btrace_thread_flag
202 {
203   /* The thread is to be stepped forwards.  */
204   BTHR_STEP = (1 << 0),
205
206   /* The thread is to be stepped backwards.  */
207   BTHR_RSTEP = (1 << 1),
208
209   /* The thread is to be continued forwards.  */
210   BTHR_CONT = (1 << 2),
211
212   /* The thread is to be continued backwards.  */
213   BTHR_RCONT = (1 << 3),
214
215   /* The thread is to be moved.  */
216   BTHR_MOVE = (BTHR_STEP | BTHR_RSTEP | BTHR_CONT | BTHR_RCONT)
217 };
218
219 /* Branch trace information per thread.
220
221    This represents the branch trace configuration as well as the entry point
222    into the branch trace data.  For the latter, it also contains the index into
223    an array of branch trace blocks used for iterating though the branch trace
224    blocks of a thread.  */
225 struct btrace_thread_info
226 {
227   /* The target branch trace information for this thread.
228
229      This contains the branch trace configuration as well as any
230      target-specific information necessary for implementing branch tracing on
231      the underlying architecture.  */
232   struct btrace_target_info *target;
233
234   /* The current branch trace for this thread (both inclusive).
235
236      The last instruction of END is the current instruction, which is not
237      part of the execution history.
238      Both will be NULL if there is no branch trace available.  If there is
239      branch trace available, both will be non-NULL.  */
240   struct btrace_function *begin;
241   struct btrace_function *end;
242
243   /* The function level offset.  When added to each function's LEVEL,
244      this normalizes the function levels such that the smallest level
245      becomes zero.  */
246   int level;
247
248   /* The number of gaps in the trace.  */
249   unsigned int ngaps;
250
251   /* A bit-vector of btrace_thread_flag.  */
252   enum btrace_thread_flag flags;
253
254   /* The instruction history iterator.  */
255   struct btrace_insn_history *insn_history;
256
257   /* The function call history iterator.  */
258   struct btrace_call_history *call_history;
259
260   /* The current replay position.  NULL if not replaying.
261      Gaps are skipped during replay, so REPLAY always points to a valid
262      instruction.  */
263   struct btrace_insn_iterator *replay;
264 };
265
266 /* Enable branch tracing for a thread.  */
267 extern void btrace_enable (struct thread_info *tp,
268                            const struct btrace_config *conf);
269
270 /* Get the branch trace configuration for a thread.
271    Return NULL if branch tracing is not enabled for that thread.  */
272 extern const struct btrace_config *
273   btrace_conf (const struct btrace_thread_info *);
274
275 /* Disable branch tracing for a thread.
276    This will also delete the current branch trace data.  */
277 extern void btrace_disable (struct thread_info *);
278
279 /* Disable branch tracing for a thread during teardown.
280    This is similar to btrace_disable, except that it will use
281    target_teardown_btrace instead of target_disable_btrace.  */
282 extern void btrace_teardown (struct thread_info *);
283
284 /* Fetch the branch trace for a single thread.  */
285 extern void btrace_fetch (struct thread_info *);
286
287 /* Clear the branch trace for a single thread.  */
288 extern void btrace_clear (struct thread_info *);
289
290 /* Clear the branch trace for all threads when an object file goes away.  */
291 extern void btrace_free_objfile (struct objfile *);
292
293 /* Parse a branch trace xml document XML into DATA.  */
294 extern void parse_xml_btrace (struct btrace_data *data, const char *xml);
295
296 /* Parse a branch trace configuration xml document XML into CONF.  */
297 extern void parse_xml_btrace_conf (struct btrace_config *conf, const char *xml);
298
299 /* Dereference a branch trace instruction iterator.  Return a pointer to the
300    instruction the iterator points to.
301    May return NULL if the iterator points to a gap in the trace.  */
302 extern const struct btrace_insn *
303   btrace_insn_get (const struct btrace_insn_iterator *);
304
305 /* Return the instruction number for a branch trace iterator.
306    Returns one past the maximum instruction number for the end iterator.
307    Returns zero if the iterator does not point to a valid instruction.  */
308 extern unsigned int btrace_insn_number (const struct btrace_insn_iterator *);
309
310 /* Initialize a branch trace instruction iterator to point to the begin/end of
311    the branch trace.  Throws an error if there is no branch trace.  */
312 extern void btrace_insn_begin (struct btrace_insn_iterator *,
313                                const struct btrace_thread_info *);
314 extern void btrace_insn_end (struct btrace_insn_iterator *,
315                              const struct btrace_thread_info *);
316
317 /* Increment/decrement a branch trace instruction iterator by at most STRIDE
318    instructions.  Return the number of instructions by which the instruction
319    iterator has been advanced.
320    Returns zero, if the operation failed or STRIDE had been zero.  */
321 extern unsigned int btrace_insn_next (struct btrace_insn_iterator *,
322                                       unsigned int stride);
323 extern unsigned int btrace_insn_prev (struct btrace_insn_iterator *,
324                                       unsigned int stride);
325
326 /* Compare two branch trace instruction iterators.
327    Return a negative number if LHS < RHS.
328    Return zero if LHS == RHS.
329    Return a positive number if LHS > RHS.  */
330 extern int btrace_insn_cmp (const struct btrace_insn_iterator *lhs,
331                             const struct btrace_insn_iterator *rhs);
332
333 /* Find an instruction in the function branch trace by its number.
334    If the instruction is found, initialize the branch trace instruction
335    iterator to point to this instruction and return non-zero.
336    Return zero otherwise.  */
337 extern int btrace_find_insn_by_number (struct btrace_insn_iterator *,
338                                        const struct btrace_thread_info *,
339                                        unsigned int number);
340
341 /* Dereference a branch trace call iterator.  Return a pointer to the
342    function the iterator points to or NULL if the interator points past
343    the end of the branch trace.  */
344 extern const struct btrace_function *
345   btrace_call_get (const struct btrace_call_iterator *);
346
347 /* Return the function number for a branch trace call iterator.
348    Returns one past the maximum function number for the end iterator.
349    Returns zero if the iterator does not point to a valid function.  */
350 extern unsigned int btrace_call_number (const struct btrace_call_iterator *);
351
352 /* Initialize a branch trace call iterator to point to the begin/end of
353    the branch trace.  Throws an error if there is no branch trace.  */
354 extern void btrace_call_begin (struct btrace_call_iterator *,
355                                const struct btrace_thread_info *);
356 extern void btrace_call_end (struct btrace_call_iterator *,
357                              const struct btrace_thread_info *);
358
359 /* Increment/decrement a branch trace call iterator by at most STRIDE function
360    segments.  Return the number of function segments by which the call
361    iterator has been advanced.
362    Returns zero, if the operation failed or STRIDE had been zero.  */
363 extern unsigned int btrace_call_next (struct btrace_call_iterator *,
364                                       unsigned int stride);
365 extern unsigned int btrace_call_prev (struct btrace_call_iterator *,
366                                       unsigned int stride);
367
368 /* Compare two branch trace call iterators.
369    Return a negative number if LHS < RHS.
370    Return zero if LHS == RHS.
371    Return a positive number if LHS > RHS.  */
372 extern int btrace_call_cmp (const struct btrace_call_iterator *lhs,
373                             const struct btrace_call_iterator *rhs);
374
375 /* Find a function in the function branch trace by its NUMBER.
376    If the function is found, initialize the branch trace call
377    iterator to point to this function and return non-zero.
378    Return zero otherwise.  */
379 extern int btrace_find_call_by_number (struct btrace_call_iterator *,
380                                        const struct btrace_thread_info *,
381                                        unsigned int number);
382
383 /* Set the branch trace instruction history from BEGIN (inclusive) to
384    END (exclusive).  */
385 extern void btrace_set_insn_history (struct btrace_thread_info *,
386                                      const struct btrace_insn_iterator *begin,
387                                      const struct btrace_insn_iterator *end);
388
389 /* Set the branch trace function call history from BEGIN (inclusive) to
390    END (exclusive).  */
391 extern void btrace_set_call_history (struct btrace_thread_info *,
392                                      const struct btrace_call_iterator *begin,
393                                      const struct btrace_call_iterator *end);
394
395 /* Determine if branch tracing is currently replaying TP.  */
396 extern int btrace_is_replaying (struct thread_info *tp);
397
398 /* Return non-zero if the branch trace for TP is empty; zero otherwise.  */
399 extern int btrace_is_empty (struct thread_info *tp);
400
401 /* Create a cleanup for DATA.  */
402 extern struct cleanup *make_cleanup_btrace_data (struct btrace_data *data);
403
404 #endif /* BTRACE_H */