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