btrace: extend struct btrace_insn
[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 /* A branch trace function segment.
90
91    This represents a function segment in a branch trace, i.e. a consecutive
92    number of instructions belonging to the same function.
93
94    We do not allow function segments without any instructions.  */
95 struct btrace_function
96 {
97   /* The full and minimal symbol for the function.  Both may be NULL.  */
98   struct minimal_symbol *msym;
99   struct symbol *sym;
100
101   /* The previous and next segment belonging to the same function.
102      If a function calls another function, the former will have at least
103      two segments: one before the call and another after the return.  */
104   struct btrace_func_link segment;
105
106   /* The previous and next function in control flow order.  */
107   struct btrace_func_link flow;
108
109   /* The directly preceding function segment in a (fake) call stack.  */
110   struct btrace_function *up;
111
112   /* The instructions in this function segment.
113      The instruction vector will never be empty.  */
114   VEC (btrace_insn_s) *insn;
115
116   /* The instruction number offset for the first instruction in this
117      function segment.  */
118   unsigned int insn_offset;
119
120   /* The function number in control-flow order.  */
121   unsigned int number;
122
123   /* The function level in a back trace across the entire branch trace.
124      A caller's level is one lower than the level of its callee.
125
126      Levels can be negative if we see returns for which we have not seen
127      the corresponding calls.  The branch trace thread information provides
128      a fixup to normalize function levels so the smallest level is zero.  */
129   int level;
130
131   /* The source line range of this function segment (both inclusive).  */
132   int lbegin, lend;
133
134   /* A bit-vector of btrace_function_flag.  */
135   enum btrace_function_flag flags;
136 };
137
138 /* A branch trace instruction iterator.  */
139 struct btrace_insn_iterator
140 {
141   /* The branch trace function segment containing the instruction.
142      Will never be NULL.  */
143   const struct btrace_function *function;
144
145   /* The index into the function segment's instruction vector.  */
146   unsigned int index;
147 };
148
149 /* A branch trace function call iterator.  */
150 struct btrace_call_iterator
151 {
152   /* The branch trace information for this thread.  Will never be NULL.  */
153   const struct btrace_thread_info *btinfo;
154
155   /* The branch trace function segment.
156      This will be NULL for the iterator pointing to the end of the trace.  */
157   const struct btrace_function *function;
158 };
159
160 /* Branch trace iteration state for "record instruction-history".  */
161 struct btrace_insn_history
162 {
163   /* The branch trace instruction range from BEGIN (inclusive) to
164      END (exclusive) that has been covered last time.  */
165   struct btrace_insn_iterator begin;
166   struct btrace_insn_iterator end;
167 };
168
169 /* Branch trace iteration state for "record function-call-history".  */
170 struct btrace_call_history
171 {
172   /* The branch trace function range from BEGIN (inclusive) to END (exclusive)
173      that has been covered last time.  */
174   struct btrace_call_iterator begin;
175   struct btrace_call_iterator end;
176 };
177
178 /* Branch trace thread flags.  */
179 enum btrace_thread_flag
180 {
181   /* The thread is to be stepped forwards.  */
182   BTHR_STEP = (1 << 0),
183
184   /* The thread is to be stepped backwards.  */
185   BTHR_RSTEP = (1 << 1),
186
187   /* The thread is to be continued forwards.  */
188   BTHR_CONT = (1 << 2),
189
190   /* The thread is to be continued backwards.  */
191   BTHR_RCONT = (1 << 3),
192
193   /* The thread is to be moved.  */
194   BTHR_MOVE = (BTHR_STEP | BTHR_RSTEP | BTHR_CONT | BTHR_RCONT)
195 };
196
197 /* Branch trace information per thread.
198
199    This represents the branch trace configuration as well as the entry point
200    into the branch trace data.  For the latter, it also contains the index into
201    an array of branch trace blocks used for iterating though the branch trace
202    blocks of a thread.  */
203 struct btrace_thread_info
204 {
205   /* The target branch trace information for this thread.
206
207      This contains the branch trace configuration as well as any
208      target-specific information necessary for implementing branch tracing on
209      the underlying architecture.  */
210   struct btrace_target_info *target;
211
212   /* The current branch trace for this thread (both inclusive).
213
214      The last instruction of END is the current instruction, which is not
215      part of the execution history.
216      Both will be NULL if there is no branch trace available.  If there is
217      branch trace available, both will be non-NULL.  */
218   struct btrace_function *begin;
219   struct btrace_function *end;
220
221   /* The function level offset.  When added to each function's LEVEL,
222      this normalizes the function levels such that the smallest level
223      becomes zero.  */
224   int level;
225
226   /* A bit-vector of btrace_thread_flag.  */
227   enum btrace_thread_flag flags;
228
229   /* The instruction history iterator.  */
230   struct btrace_insn_history *insn_history;
231
232   /* The function call history iterator.  */
233   struct btrace_call_history *call_history;
234
235   /* The current replay position.  NULL if not replaying.  */
236   struct btrace_insn_iterator *replay;
237 };
238
239 /* Enable branch tracing for a thread.  */
240 extern void btrace_enable (struct thread_info *tp,
241                            const struct btrace_config *conf);
242
243 /* Get the branch trace configuration for a thread.
244    Return NULL if branch tracing is not enabled for that thread.  */
245 extern const struct btrace_config *
246   btrace_conf (const struct btrace_thread_info *);
247
248 /* Disable branch tracing for a thread.
249    This will also delete the current branch trace data.  */
250 extern void btrace_disable (struct thread_info *);
251
252 /* Disable branch tracing for a thread during teardown.
253    This is similar to btrace_disable, except that it will use
254    target_teardown_btrace instead of target_disable_btrace.  */
255 extern void btrace_teardown (struct thread_info *);
256
257 /* Fetch the branch trace for a single thread.  */
258 extern void btrace_fetch (struct thread_info *);
259
260 /* Clear the branch trace for a single thread.  */
261 extern void btrace_clear (struct thread_info *);
262
263 /* Clear the branch trace for all threads when an object file goes away.  */
264 extern void btrace_free_objfile (struct objfile *);
265
266 /* Parse a branch trace xml document XML into DATA.  */
267 extern void parse_xml_btrace (struct btrace_data *data, const char *xml);
268
269 /* Parse a branch trace configuration xml document XML into CONF.  */
270 extern void parse_xml_btrace_conf (struct btrace_config *conf, const char *xml);
271
272 /* Dereference a branch trace instruction iterator.  Return a pointer to the
273    instruction the iterator points to.  */
274 extern const struct btrace_insn *
275   btrace_insn_get (const struct btrace_insn_iterator *);
276
277 /* Return the instruction number for a branch trace iterator.
278    Returns one past the maximum instruction number for the end iterator.
279    Returns zero if the iterator does not point to a valid instruction.  */
280 extern unsigned int btrace_insn_number (const struct btrace_insn_iterator *);
281
282 /* Initialize a branch trace instruction iterator to point to the begin/end of
283    the branch trace.  Throws an error if there is no branch trace.  */
284 extern void btrace_insn_begin (struct btrace_insn_iterator *,
285                                const struct btrace_thread_info *);
286 extern void btrace_insn_end (struct btrace_insn_iterator *,
287                              const struct btrace_thread_info *);
288
289 /* Increment/decrement a branch trace instruction iterator by at most STRIDE
290    instructions.  Return the number of instructions by which the instruction
291    iterator has been advanced.
292    Returns zero, if the operation failed or STRIDE had been zero.  */
293 extern unsigned int btrace_insn_next (struct btrace_insn_iterator *,
294                                       unsigned int stride);
295 extern unsigned int btrace_insn_prev (struct btrace_insn_iterator *,
296                                       unsigned int stride);
297
298 /* Compare two branch trace instruction iterators.
299    Return a negative number if LHS < RHS.
300    Return zero if LHS == RHS.
301    Return a positive number if LHS > RHS.  */
302 extern int btrace_insn_cmp (const struct btrace_insn_iterator *lhs,
303                             const struct btrace_insn_iterator *rhs);
304
305 /* Find an instruction in the function branch trace by its number.
306    If the instruction is found, initialize the branch trace instruction
307    iterator to point to this instruction and return non-zero.
308    Return zero otherwise.  */
309 extern int btrace_find_insn_by_number (struct btrace_insn_iterator *,
310                                        const struct btrace_thread_info *,
311                                        unsigned int number);
312
313 /* Dereference a branch trace call iterator.  Return a pointer to the
314    function the iterator points to or NULL if the interator points past
315    the end of the branch trace.  */
316 extern const struct btrace_function *
317   btrace_call_get (const struct btrace_call_iterator *);
318
319 /* Return the function number for a branch trace call iterator.
320    Returns one past the maximum function number for the end iterator.
321    Returns zero if the iterator does not point to a valid function.  */
322 extern unsigned int btrace_call_number (const struct btrace_call_iterator *);
323
324 /* Initialize a branch trace call iterator to point to the begin/end of
325    the branch trace.  Throws an error if there is no branch trace.  */
326 extern void btrace_call_begin (struct btrace_call_iterator *,
327                                const struct btrace_thread_info *);
328 extern void btrace_call_end (struct btrace_call_iterator *,
329                              const struct btrace_thread_info *);
330
331 /* Increment/decrement a branch trace call iterator by at most STRIDE function
332    segments.  Return the number of function segments by which the call
333    iterator has been advanced.
334    Returns zero, if the operation failed or STRIDE had been zero.  */
335 extern unsigned int btrace_call_next (struct btrace_call_iterator *,
336                                       unsigned int stride);
337 extern unsigned int btrace_call_prev (struct btrace_call_iterator *,
338                                       unsigned int stride);
339
340 /* Compare two branch trace call iterators.
341    Return a negative number if LHS < RHS.
342    Return zero if LHS == RHS.
343    Return a positive number if LHS > RHS.  */
344 extern int btrace_call_cmp (const struct btrace_call_iterator *lhs,
345                             const struct btrace_call_iterator *rhs);
346
347 /* Find a function in the function branch trace by its NUMBER.
348    If the function is found, initialize the branch trace call
349    iterator to point to this function and return non-zero.
350    Return zero otherwise.  */
351 extern int btrace_find_call_by_number (struct btrace_call_iterator *,
352                                        const struct btrace_thread_info *,
353                                        unsigned int number);
354
355 /* Set the branch trace instruction history from BEGIN (inclusive) to
356    END (exclusive).  */
357 extern void btrace_set_insn_history (struct btrace_thread_info *,
358                                      const struct btrace_insn_iterator *begin,
359                                      const struct btrace_insn_iterator *end);
360
361 /* Set the branch trace function call history from BEGIN (inclusive) to
362    END (exclusive).  */
363 extern void btrace_set_call_history (struct btrace_thread_info *,
364                                      const struct btrace_call_iterator *begin,
365                                      const struct btrace_call_iterator *end);
366
367 /* Determine if branch tracing is currently replaying TP.  */
368 extern int btrace_is_replaying (struct thread_info *tp);
369
370 /* Return non-zero if the branch trace for TP is empty; zero otherwise.  */
371 extern int btrace_is_empty (struct thread_info *tp);
372
373 /* Create a cleanup for DATA.  */
374 extern struct cleanup *make_cleanup_btrace_data (struct btrace_data *data);
375
376 #endif /* BTRACE_H */