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