GDB (xrefs)
/tmp/gdb-7.10/gdb/btrace.h
Go to the documentation of this file.
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 #include "target/waitstatus.h" /* For enum target_stop_reason. */
31 
32 #if defined (HAVE_LIBIPT)
33 # include <intel-pt.h>
34 #endif
35 
36 struct thread_info;
37 struct btrace_function;
38 
39 /* A coarse instruction classification. */
41 {
42  /* The instruction is something not listed below. */
44 
45  /* The instruction is a function call. */
47 
48  /* The instruction is a function return. */
50 
51  /* The instruction is an unconditional jump. */
53 };
54 
55 /* A branch trace instruction.
56 
57  This represents a single instruction in a branch trace. */
59 {
60  /* The address of this instruction. */
62 
63  /* The size of this instruction in bytes. */
65 
66  /* The instruction class of this instruction. */
68 };
69 
70 /* A vector of branch trace instructions. */
71 typedef struct btrace_insn btrace_insn_s;
73 
74 /* A doubly-linked list of branch trace function segments. */
76 {
79 };
80 
81 /* Flags for btrace function segments. */
83 {
84  /* The 'up' link interpretation.
85  If set, it points to the function segment we returned to.
86  If clear, it points to the function segment we called from. */
88 
89  /* The 'up' link points to a tail call. This obviously only makes sense
90  if bfun_up_links_to_ret is clear. */
92 };
93 
94 /* Decode errors for the BTS recording format. */
96 {
97  /* The instruction trace overflowed the end of the trace block. */
99 
100  /* The instruction size could not be determined. */
102 };
103 
104 /* Decode errors for the Intel(R) Processor Trace recording format. */
106 {
107  /* The user cancelled trace processing. */
109 
110  /* Tracing was temporarily disabled. */
112 
113  /* Trace recording overflowed. */
115 
116  /* Negative numbers are used by the decoder library. */
117 };
118 
119 /* A branch trace function segment.
120 
121  This represents a function segment in a branch trace, i.e. a consecutive
122  number of instructions belonging to the same function.
123 
124  In case of decode errors, we add an empty function segment to indicate
125  the gap in the trace.
126 
127  We do not allow function segments without instructions otherwise. */
129 {
130  /* The full and minimal symbol for the function. Both may be NULL. */
132  struct symbol *sym;
133 
134  /* The previous and next segment belonging to the same function.
135  If a function calls another function, the former will have at least
136  two segments: one before the call and another after the return. */
138 
139  /* The previous and next function in control flow order. */
141 
142  /* The directly preceding function segment in a (fake) call stack. */
144 
145  /* The instructions in this function segment.
146  The instruction vector will be empty if the function segment
147  represents a decode error. */
148  VEC (btrace_insn_s) *insn;
149 
150  /* The error code of a decode error that led to a gap.
151  Must be zero unless INSN is empty; non-zero otherwise. */
152  int errcode;
153 
154  /* The instruction number offset for the first instruction in this
155  function segment.
156  If INSN is empty this is the insn_offset of the succeding function
157  segment in control-flow order. */
158  unsigned int insn_offset;
159 
160  /* The function number in control-flow order.
161  If INSN is empty indicating a gap in the trace due to a decode error,
162  we still count the gap as a function. */
163  unsigned int number;
164 
165  /* The function level in a back trace across the entire branch trace.
166  A caller's level is one lower than the level of its callee.
167 
168  Levels can be negative if we see returns for which we have not seen
169  the corresponding calls. The branch trace thread information provides
170  a fixup to normalize function levels so the smallest level is zero. */
171  int level;
172 
173  /* A bit-vector of btrace_function_flag. */
175 };
176 
177 /* A branch trace instruction iterator. */
179 {
180  /* The branch trace function segment containing the instruction.
181  Will never be NULL. */
182  const struct btrace_function *function;
183 
184  /* The index into the function segment's instruction vector. */
185  unsigned int index;
186 };
187 
188 /* A branch trace function call iterator. */
190 {
191  /* The branch trace information for this thread. Will never be NULL. */
192  const struct btrace_thread_info *btinfo;
193 
194  /* The branch trace function segment.
195  This will be NULL for the iterator pointing to the end of the trace. */
196  const struct btrace_function *function;
197 };
198 
199 /* Branch trace iteration state for "record instruction-history". */
201 {
202  /* The branch trace instruction range from BEGIN (inclusive) to
203  END (exclusive) that has been covered last time. */
206 };
207 
208 /* Branch trace iteration state for "record function-call-history". */
210 {
211  /* The branch trace function range from BEGIN (inclusive) to END (exclusive)
212  that has been covered last time. */
215 };
216 
217 /* Branch trace thread flags. */
219 {
220  /* The thread is to be stepped forwards. */
221  BTHR_STEP = (1 << 0),
222 
223  /* The thread is to be stepped backwards. */
224  BTHR_RSTEP = (1 << 1),
225 
226  /* The thread is to be continued forwards. */
227  BTHR_CONT = (1 << 2),
228 
229  /* The thread is to be continued backwards. */
230  BTHR_RCONT = (1 << 3),
231 
232  /* The thread is to be moved. */
234 };
235 
236 #if defined (HAVE_LIBIPT)
237 /* A packet. */
238 struct btrace_pt_packet
239 {
240  /* The offset in the trace stream. */
241  uint64_t offset;
242 
243  /* The decode error code. */
244  enum pt_error_code errcode;
245 
246  /* The decoded packet. Only valid if ERRCODE == pte_ok. */
247  struct pt_packet packet;
248 };
249 
250 /* Define functions operating on a vector of packets. */
251 typedef struct btrace_pt_packet btrace_pt_packet_s;
252 DEF_VEC_O (btrace_pt_packet_s);
253 #endif /* defined (HAVE_LIBIPT) */
254 
255 /* Branch trace iteration state for "maintenance btrace packet-history". */
257 {
258  /* The branch trace packet range from BEGIN (inclusive) to
259  END (exclusive) that has been covered last time. */
260  unsigned int begin;
261  unsigned int end;
262 };
263 
264 /* Branch trace maintenance information per thread.
265 
266  This information is used by "maintenance btrace" commands. */
268 {
269  /* Most information is format-specific.
270  The format can be found in the BTRACE.DATA.FORMAT field of each thread. */
271  union
272  {
273  /* BTRACE.DATA.FORMAT == BTRACE_FORMAT_BTS */
274  struct
275  {
276  /* The packet history iterator.
277  We are iterating over BTRACE.DATA.FORMAT.VARIANT.BTS.BLOCKS. */
279  } bts;
280 
281 #if defined (HAVE_LIBIPT)
282  /* BTRACE.DATA.FORMAT == BTRACE_FORMAT_PT */
283  struct
284  {
285  /* A vector of decoded packets. */
286  VEC (btrace_pt_packet_s) *packets;
287 
288  /* The packet history iterator.
289  We are iterating over the above PACKETS vector. */
291  } pt;
292 #endif /* defined (HAVE_LIBIPT) */
293  } variant;
294 };
295 
296 /* Branch trace information per thread.
297 
298  This represents the branch trace configuration as well as the entry point
299  into the branch trace data. For the latter, it also contains the index into
300  an array of branch trace blocks used for iterating though the branch trace
301  blocks of a thread. */
303 {
304  /* The target branch trace information for this thread.
305 
306  This contains the branch trace configuration as well as any
307  target-specific information necessary for implementing branch tracing on
308  the underlying architecture. */
310 
311  /* The raw branch trace data for the below branch trace. */
313 
314  /* The current branch trace for this thread (both inclusive).
315 
316  The last instruction of END is the current instruction, which is not
317  part of the execution history.
318  Both will be NULL if there is no branch trace available. If there is
319  branch trace available, both will be non-NULL. */
322 
323  /* The function level offset. When added to each function's LEVEL,
324  this normalizes the function levels such that the smallest level
325  becomes zero. */
326  int level;
327 
328  /* The number of gaps in the trace. */
329  unsigned int ngaps;
330 
331  /* A bit-vector of btrace_thread_flag. */
333 
334  /* The instruction history iterator. */
336 
337  /* The function call history iterator. */
339 
340  /* The current replay position. NULL if not replaying.
341  Gaps are skipped during replay, so REPLAY always points to a valid
342  instruction. */
344 
345  /* Why the thread stopped, if we need to track it. */
347 
348  /* Maintenance information. */
350 };
351 
352 /* Enable branch tracing for a thread. */
353 extern void btrace_enable (struct thread_info *tp,
354  const struct btrace_config *conf);
355 
356 /* Get the branch trace configuration for a thread.
357  Return NULL if branch tracing is not enabled for that thread. */
358 extern const struct btrace_config *
359  btrace_conf (const struct btrace_thread_info *);
360 
361 /* Disable branch tracing for a thread.
362  This will also delete the current branch trace data. */
363 extern void btrace_disable (struct thread_info *);
364 
365 /* Disable branch tracing for a thread during teardown.
366  This is similar to btrace_disable, except that it will use
367  target_teardown_btrace instead of target_disable_btrace. */
368 extern void btrace_teardown (struct thread_info *);
369 
370 /* Fetch the branch trace for a single thread. */
371 extern void btrace_fetch (struct thread_info *);
372 
373 /* Clear the branch trace for a single thread. */
374 extern void btrace_clear (struct thread_info *);
375 
376 /* Clear the branch trace for all threads when an object file goes away. */
377 extern void btrace_free_objfile (struct objfile *);
378 
379 /* Parse a branch trace xml document XML into DATA. */
380 extern void parse_xml_btrace (struct btrace_data *data, const char *xml);
381 
382 /* Parse a branch trace configuration xml document XML into CONF. */
383 extern void parse_xml_btrace_conf (struct btrace_config *conf, const char *xml);
384 
385 /* Dereference a branch trace instruction iterator. Return a pointer to the
386  instruction the iterator points to.
387  May return NULL if the iterator points to a gap in the trace. */
388 extern const struct btrace_insn *
389  btrace_insn_get (const struct btrace_insn_iterator *);
390 
391 /* Return the instruction number for a branch trace iterator.
392  Returns one past the maximum instruction number for the end iterator.
393  Returns zero if the iterator does not point to a valid instruction. */
394 extern unsigned int btrace_insn_number (const struct btrace_insn_iterator *);
395 
396 /* Initialize a branch trace instruction iterator to point to the begin/end of
397  the branch trace. Throws an error if there is no branch trace. */
398 extern void btrace_insn_begin (struct btrace_insn_iterator *,
399  const struct btrace_thread_info *);
400 extern void btrace_insn_end (struct btrace_insn_iterator *,
401  const struct btrace_thread_info *);
402 
403 /* Increment/decrement a branch trace instruction iterator by at most STRIDE
404  instructions. Return the number of instructions by which the instruction
405  iterator has been advanced.
406  Returns zero, if the operation failed or STRIDE had been zero. */
407 extern unsigned int btrace_insn_next (struct btrace_insn_iterator *,
408  unsigned int stride);
409 extern unsigned int btrace_insn_prev (struct btrace_insn_iterator *,
410  unsigned int stride);
411 
412 /* Compare two branch trace instruction iterators.
413  Return a negative number if LHS < RHS.
414  Return zero if LHS == RHS.
415  Return a positive number if LHS > RHS. */
416 extern int btrace_insn_cmp (const struct btrace_insn_iterator *lhs,
417  const struct btrace_insn_iterator *rhs);
418 
419 /* Find an instruction in the function branch trace by its number.
420  If the instruction is found, initialize the branch trace instruction
421  iterator to point to this instruction and return non-zero.
422  Return zero otherwise. */
424  const struct btrace_thread_info *,
425  unsigned int number);
426 
427 /* Dereference a branch trace call iterator. Return a pointer to the
428  function the iterator points to or NULL if the interator points past
429  the end of the branch trace. */
430 extern const struct btrace_function *
431  btrace_call_get (const struct btrace_call_iterator *);
432 
433 /* Return the function number for a branch trace call iterator.
434  Returns one past the maximum function number for the end iterator.
435  Returns zero if the iterator does not point to a valid function. */
436 extern unsigned int btrace_call_number (const struct btrace_call_iterator *);
437 
438 /* Initialize a branch trace call iterator to point to the begin/end of
439  the branch trace. Throws an error if there is no branch trace. */
440 extern void btrace_call_begin (struct btrace_call_iterator *,
441  const struct btrace_thread_info *);
442 extern void btrace_call_end (struct btrace_call_iterator *,
443  const struct btrace_thread_info *);
444 
445 /* Increment/decrement a branch trace call iterator by at most STRIDE function
446  segments. Return the number of function segments by which the call
447  iterator has been advanced.
448  Returns zero, if the operation failed or STRIDE had been zero. */
449 extern unsigned int btrace_call_next (struct btrace_call_iterator *,
450  unsigned int stride);
451 extern unsigned int btrace_call_prev (struct btrace_call_iterator *,
452  unsigned int stride);
453 
454 /* Compare two branch trace call iterators.
455  Return a negative number if LHS < RHS.
456  Return zero if LHS == RHS.
457  Return a positive number if LHS > RHS. */
458 extern int btrace_call_cmp (const struct btrace_call_iterator *lhs,
459  const struct btrace_call_iterator *rhs);
460 
461 /* Find a function in the function branch trace by its NUMBER.
462  If the function is found, initialize the branch trace call
463  iterator to point to this function and return non-zero.
464  Return zero otherwise. */
466  const struct btrace_thread_info *,
467  unsigned int number);
468 
469 /* Set the branch trace instruction history from BEGIN (inclusive) to
470  END (exclusive). */
471 extern void btrace_set_insn_history (struct btrace_thread_info *,
472  const struct btrace_insn_iterator *begin,
473  const struct btrace_insn_iterator *end);
474 
475 /* Set the branch trace function call history from BEGIN (inclusive) to
476  END (exclusive). */
477 extern void btrace_set_call_history (struct btrace_thread_info *,
478  const struct btrace_call_iterator *begin,
479  const struct btrace_call_iterator *end);
480 
481 /* Determine if branch tracing is currently replaying TP. */
482 extern int btrace_is_replaying (struct thread_info *tp);
483 
484 /* Return non-zero if the branch trace for TP is empty; zero otherwise. */
485 extern int btrace_is_empty (struct thread_info *tp);
486 
487 /* Create a cleanup for DATA. */
488 extern struct cleanup *make_cleanup_btrace_data (struct btrace_data *data);
489 
490 #endif /* BTRACE_H */
void btrace_disable(struct thread_info *)
Definition: btrace.c:1050
bfd_vma CORE_ADDR
Definition: common-types.h:41
btrace_thread_flag
Definition: btrace.h:218
unsigned int btrace_insn_next(struct btrace_insn_iterator *, unsigned int stride)
Definition: btrace.c:1762
struct btrace_function * end
Definition: btrace.h:321
struct symbol * sym
Definition: btrace.h:132
struct btrace_insn_iterator * replay
Definition: btrace.h:343
unsigned int index
Definition: btrace.h:185
unsigned int btrace_call_next(struct btrace_call_iterator *, unsigned int stride)
Definition: btrace.c:2052
unsigned int btrace_insn_prev(struct btrace_insn_iterator *, unsigned int stride)
Definition: btrace.c:1844
struct btrace_call_history * call_history
Definition: btrace.h:338
btrace_insn_class
Definition: btrace.h:40
enum btrace_function_flag flags
Definition: btrace.h:174
DEF_VEC_O(btrace_insn_s)
struct btrace_maint_info::@32::@33 bts
const struct btrace_thread_info * btinfo
Definition: btrace.h:192
enum btrace_thread_flag flags
Definition: btrace.h:332
#define VEC(T)
Definition: vec.h:398
struct btrace_call_iterator begin
Definition: btrace.h:213
unsigned int btrace_call_prev(struct btrace_call_iterator *, unsigned int stride)
Definition: btrace.c:2088
struct btrace_insn_history * insn_history
Definition: btrace.h:335
int btrace_find_call_by_number(struct btrace_call_iterator *, const struct btrace_thread_info *, unsigned int number)
Definition: btrace.c:2151
struct btrace_func_link flow
Definition: btrace.h:140
const struct btrace_insn * btrace_insn_get(const struct btrace_insn_iterator *)
Definition: btrace.c:1682
int btrace_call_cmp(const struct btrace_call_iterator *lhs, const struct btrace_call_iterator *rhs)
Definition: btrace.c:2137
struct btrace_insn_iterator end
Definition: btrace.h:205
struct btrace_function * begin
Definition: btrace.h:320
VEC(btrace_insn_s)*insn
struct btrace_data data
Definition: btrace.h:312
void btrace_call_begin(struct btrace_call_iterator *, const struct btrace_thread_info *)
Definition: btrace.c:2020
struct cleanup * make_cleanup_btrace_data(struct btrace_data *data)
Definition: btrace.c:2245
struct btrace_maint_packet_history packet_history
Definition: btrace.h:278
struct btrace_maint_info maint
Definition: btrace.h:349
void btrace_insn_begin(struct btrace_insn_iterator *, const struct btrace_thread_info *)
Definition: btrace.c:1721
void btrace_teardown(struct thread_info *)
Definition: btrace.c:1069
void btrace_clear(struct thread_info *)
Definition: btrace.c:1318
struct btrace_target_info * target
Definition: btrace.h:309
int btrace_is_empty(struct thread_info *tp)
Definition: btrace.c:2218
void parse_xml_btrace(struct btrace_data *data, const char *xml)
Definition: btrace.c:1562
struct btrace_func_link segment
Definition: btrace.h:137
struct btrace_function * up
Definition: btrace.h:143
void btrace_fetch(struct thread_info *)
Definition: btrace.c:1243
int btrace_find_insn_by_number(struct btrace_insn_iterator *, const struct btrace_thread_info *, unsigned int number)
Definition: btrace.c:1947
void btrace_enable(struct thread_info *tp, const struct btrace_config *conf)
Definition: btrace.c:1018
void btrace_set_call_history(struct btrace_thread_info *, const struct btrace_call_iterator *begin, const struct btrace_call_iterator *end)
Definition: btrace.c:2194
const struct btrace_config * btrace_conf(const struct btrace_thread_info *)
Definition: btrace.c:1039
struct minimal_symbol * msym
Definition: btrace.h:131
unsigned int ngaps
Definition: btrace.h:329
unsigned int insn_offset
Definition: btrace.h:158
CORE_ADDR pc
Definition: btrace.h:61
enum btrace_insn_class iclass
Definition: btrace.h:67
void btrace_insn_end(struct btrace_insn_iterator *, const struct btrace_thread_info *)
Definition: btrace.c:1737
struct btrace_insn_iterator begin
Definition: btrace.h:204
void btrace_free_objfile(struct objfile *)
Definition: btrace.c:1353
bfd_byte gdb_byte
Definition: common-types.h:38
void btrace_set_insn_history(struct btrace_thread_info *, const struct btrace_insn_iterator *begin, const struct btrace_insn_iterator *end)
Definition: btrace.c:2180
unsigned int btrace_call_number(const struct btrace_call_iterator *)
Definition: btrace.c:1991
int offset
Definition: agent.c:65
union btrace_maint_info::@32 variant
enum target_stop_reason stop_reason
Definition: btrace.h:346
btrace_bts_error
Definition: btrace.h:95
const struct btrace_function * btrace_call_get(const struct btrace_call_iterator *)
Definition: btrace.c:1983
gdb_byte size
Definition: btrace.h:64
Definition: symtab.h:703
int btrace_is_replaying(struct thread_info *tp)
Definition: btrace.c:2210
btrace_function_flag
Definition: btrace.h:82
unsigned int number
Definition: btrace.h:163
btrace_pt_error
Definition: btrace.h:105
void btrace_call_end(struct btrace_call_iterator *, const struct btrace_thread_info *)
Definition: btrace.c:2036
struct btrace_call_iterator end
Definition: btrace.h:214
unsigned int btrace_insn_number(const struct btrace_insn_iterator *)
Definition: btrace.c:1705
int btrace_insn_cmp(const struct btrace_insn_iterator *lhs, const struct btrace_insn_iterator *rhs)
Definition: btrace.c:1902
void parse_xml_btrace_conf(struct btrace_config *conf, const char *xml)
Definition: btrace.c:1661
target_stop_reason
Definition: waitstatus.h:121