Commit | Line | Data |
---|---|---|
02d27625 MM |
1 | /* Branch trace support for GDB, the GNU debugger. |
2 | ||
32d0add0 | 3 | Copyright (C) 2013-2015 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. */ |
02d27625 | 31 | |
b20a6524 MM |
32 | #if defined (HAVE_LIBIPT) |
33 | # include <intel-pt.h> | |
34 | #endif | |
35 | ||
02d27625 | 36 | struct thread_info; |
23a7fe75 | 37 | struct btrace_function; |
02d27625 | 38 | |
7d5c24b3 MM |
39 | /* A coarse instruction classification. */ |
40 | enum btrace_insn_class | |
41 | { | |
42 | /* The instruction is something not listed below. */ | |
43 | BTRACE_INSN_OTHER, | |
44 | ||
45 | /* The instruction is a function call. */ | |
46 | BTRACE_INSN_CALL, | |
47 | ||
48 | /* The instruction is a function return. */ | |
49 | BTRACE_INSN_RETURN, | |
50 | ||
51 | /* The instruction is an unconditional jump. */ | |
52 | BTRACE_INSN_JUMP | |
53 | }; | |
54 | ||
02d27625 MM |
55 | /* A branch trace instruction. |
56 | ||
57 | This represents a single instruction in a branch trace. */ | |
23a7fe75 | 58 | struct btrace_insn |
02d27625 MM |
59 | { |
60 | /* The address of this instruction. */ | |
61 | CORE_ADDR pc; | |
7d5c24b3 MM |
62 | |
63 | /* The size of this instruction in bytes. */ | |
64 | gdb_byte size; | |
65 | ||
66 | /* The instruction class of this instruction. */ | |
67 | enum btrace_insn_class iclass; | |
02d27625 MM |
68 | }; |
69 | ||
23a7fe75 MM |
70 | /* A vector of branch trace instructions. */ |
71 | typedef struct btrace_insn btrace_insn_s; | |
72 | DEF_VEC_O (btrace_insn_s); | |
73 | ||
74 | /* A doubly-linked list of branch trace function segments. */ | |
75 | struct btrace_func_link | |
76 | { | |
77 | struct btrace_function *prev; | |
78 | struct btrace_function *next; | |
79 | }; | |
80 | ||
81 | /* Flags for btrace function segments. */ | |
82 | enum btrace_function_flag | |
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. */ | |
87 | BFUN_UP_LINKS_TO_RET = (1 << 0), | |
88 | ||
89 | /* The 'up' link points to a tail call. This obviously only makes sense | |
90 | if bfun_up_links_to_ret is clear. */ | |
91 | BFUN_UP_LINKS_TO_TAILCALL = (1 << 1) | |
92 | }; | |
93 | ||
31fd9caa MM |
94 | /* Decode errors for the BTS recording format. */ |
95 | enum btrace_bts_error | |
96 | { | |
97 | /* The instruction trace overflowed the end of the trace block. */ | |
98 | BDE_BTS_OVERFLOW = 1, | |
99 | ||
100 | /* The instruction size could not be determined. */ | |
101 | BDE_BTS_INSN_SIZE | |
102 | }; | |
103 | ||
b20a6524 MM |
104 | /* Decode errors for the Intel(R) Processor Trace recording format. */ |
105 | enum btrace_pt_error | |
106 | { | |
107 | /* The user cancelled trace processing. */ | |
108 | BDE_PT_USER_QUIT = 1, | |
109 | ||
110 | /* Tracing was temporarily disabled. */ | |
111 | BDE_PT_DISABLED, | |
112 | ||
113 | /* Trace recording overflowed. */ | |
114 | BDE_PT_OVERFLOW | |
115 | ||
116 | /* Negative numbers are used by the decoder library. */ | |
117 | }; | |
118 | ||
23a7fe75 | 119 | /* A branch trace function segment. |
02d27625 MM |
120 | |
121 | This represents a function segment in a branch trace, i.e. a consecutive | |
23a7fe75 MM |
122 | number of instructions belonging to the same function. |
123 | ||
31fd9caa MM |
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. */ | |
23a7fe75 | 128 | struct btrace_function |
02d27625 | 129 | { |
23a7fe75 | 130 | /* The full and minimal symbol for the function. Both may be NULL. */ |
02d27625 MM |
131 | struct minimal_symbol *msym; |
132 | struct symbol *sym; | |
133 | ||
23a7fe75 MM |
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. */ | |
137 | struct btrace_func_link segment; | |
138 | ||
139 | /* The previous and next function in control flow order. */ | |
140 | struct btrace_func_link flow; | |
141 | ||
142 | /* The directly preceding function segment in a (fake) call stack. */ | |
143 | struct btrace_function *up; | |
144 | ||
145 | /* The instructions in this function segment. | |
31fd9caa MM |
146 | The instruction vector will be empty if the function segment |
147 | represents a decode error. */ | |
23a7fe75 MM |
148 | VEC (btrace_insn_s) *insn; |
149 | ||
31fd9caa MM |
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 | ||
23a7fe75 | 154 | /* The instruction number offset for the first instruction in this |
31fd9caa MM |
155 | function segment. |
156 | If INSN is empty this is the insn_offset of the succeding function | |
157 | segment in control-flow order. */ | |
23a7fe75 MM |
158 | unsigned int insn_offset; |
159 | ||
31fd9caa MM |
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. */ | |
23a7fe75 MM |
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 | ||
23a7fe75 MM |
173 | /* A bit-vector of btrace_function_flag. */ |
174 | enum btrace_function_flag flags; | |
02d27625 MM |
175 | }; |
176 | ||
23a7fe75 MM |
177 | /* A branch trace instruction iterator. */ |
178 | struct btrace_insn_iterator | |
179 | { | |
180 | /* The branch trace function segment containing the instruction. | |
181 | Will never be NULL. */ | |
182 | const struct btrace_function *function; | |
02d27625 | 183 | |
23a7fe75 MM |
184 | /* The index into the function segment's instruction vector. */ |
185 | unsigned int index; | |
186 | }; | |
02d27625 | 187 | |
23a7fe75 MM |
188 | /* A branch trace function call iterator. */ |
189 | struct btrace_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 | }; | |
02d27625 MM |
198 | |
199 | /* Branch trace iteration state for "record instruction-history". */ | |
23a7fe75 | 200 | struct btrace_insn_history |
02d27625 | 201 | { |
23a7fe75 MM |
202 | /* The branch trace instruction range from BEGIN (inclusive) to |
203 | END (exclusive) that has been covered last time. */ | |
204 | struct btrace_insn_iterator begin; | |
205 | struct btrace_insn_iterator end; | |
02d27625 MM |
206 | }; |
207 | ||
208 | /* Branch trace iteration state for "record function-call-history". */ | |
23a7fe75 | 209 | struct btrace_call_history |
02d27625 | 210 | { |
23a7fe75 MM |
211 | /* The branch trace function range from BEGIN (inclusive) to END (exclusive) |
212 | that has been covered last time. */ | |
213 | struct btrace_call_iterator begin; | |
214 | struct btrace_call_iterator end; | |
02d27625 MM |
215 | }; |
216 | ||
52834460 MM |
217 | /* Branch trace thread flags. */ |
218 | enum btrace_thread_flag | |
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. */ | |
233 | BTHR_MOVE = (BTHR_STEP | BTHR_RSTEP | BTHR_CONT | BTHR_RCONT) | |
234 | }; | |
235 | ||
02d27625 MM |
236 | /* Branch trace information per thread. |
237 | ||
238 | This represents the branch trace configuration as well as the entry point | |
239 | into the branch trace data. For the latter, it also contains the index into | |
240 | an array of branch trace blocks used for iterating though the branch trace | |
241 | blocks of a thread. */ | |
242 | struct btrace_thread_info | |
243 | { | |
244 | /* The target branch trace information for this thread. | |
245 | ||
246 | This contains the branch trace configuration as well as any | |
247 | target-specific information necessary for implementing branch tracing on | |
248 | the underlying architecture. */ | |
249 | struct btrace_target_info *target; | |
250 | ||
23a7fe75 MM |
251 | /* The current branch trace for this thread (both inclusive). |
252 | ||
253 | The last instruction of END is the current instruction, which is not | |
254 | part of the execution history. | |
255 | Both will be NULL if there is no branch trace available. If there is | |
256 | branch trace available, both will be non-NULL. */ | |
257 | struct btrace_function *begin; | |
258 | struct btrace_function *end; | |
259 | ||
260 | /* The function level offset. When added to each function's LEVEL, | |
261 | this normalizes the function levels such that the smallest level | |
262 | becomes zero. */ | |
263 | int level; | |
02d27625 | 264 | |
31fd9caa MM |
265 | /* The number of gaps in the trace. */ |
266 | unsigned int ngaps; | |
267 | ||
52834460 MM |
268 | /* A bit-vector of btrace_thread_flag. */ |
269 | enum btrace_thread_flag flags; | |
270 | ||
02d27625 | 271 | /* The instruction history iterator. */ |
23a7fe75 | 272 | struct btrace_insn_history *insn_history; |
02d27625 MM |
273 | |
274 | /* The function call history iterator. */ | |
23a7fe75 | 275 | struct btrace_call_history *call_history; |
07bbe694 | 276 | |
31fd9caa MM |
277 | /* The current replay position. NULL if not replaying. |
278 | Gaps are skipped during replay, so REPLAY always points to a valid | |
279 | instruction. */ | |
07bbe694 | 280 | struct btrace_insn_iterator *replay; |
9e8915c6 PA |
281 | |
282 | /* Why the thread stopped, if we need to track it. */ | |
283 | enum target_stop_reason stop_reason; | |
02d27625 MM |
284 | }; |
285 | ||
286 | /* Enable branch tracing for a thread. */ | |
f4abbc16 MM |
287 | extern void btrace_enable (struct thread_info *tp, |
288 | const struct btrace_config *conf); | |
289 | ||
290 | /* Get the branch trace configuration for a thread. | |
291 | Return NULL if branch tracing is not enabled for that thread. */ | |
292 | extern const struct btrace_config * | |
293 | btrace_conf (const struct btrace_thread_info *); | |
02d27625 MM |
294 | |
295 | /* Disable branch tracing for a thread. | |
296 | This will also delete the current branch trace data. */ | |
297 | extern void btrace_disable (struct thread_info *); | |
298 | ||
299 | /* Disable branch tracing for a thread during teardown. | |
300 | This is similar to btrace_disable, except that it will use | |
301 | target_teardown_btrace instead of target_disable_btrace. */ | |
302 | extern void btrace_teardown (struct thread_info *); | |
303 | ||
304 | /* Fetch the branch trace for a single thread. */ | |
305 | extern void btrace_fetch (struct thread_info *); | |
306 | ||
307 | /* Clear the branch trace for a single thread. */ | |
308 | extern void btrace_clear (struct thread_info *); | |
309 | ||
310 | /* Clear the branch trace for all threads when an object file goes away. */ | |
311 | extern void btrace_free_objfile (struct objfile *); | |
312 | ||
734b0e4b MM |
313 | /* Parse a branch trace xml document XML into DATA. */ |
314 | extern void parse_xml_btrace (struct btrace_data *data, const char *xml); | |
c12a2917 | 315 | |
f4abbc16 MM |
316 | /* Parse a branch trace configuration xml document XML into CONF. */ |
317 | extern void parse_xml_btrace_conf (struct btrace_config *conf, const char *xml); | |
318 | ||
23a7fe75 | 319 | /* Dereference a branch trace instruction iterator. Return a pointer to the |
31fd9caa MM |
320 | instruction the iterator points to. |
321 | May return NULL if the iterator points to a gap in the trace. */ | |
23a7fe75 MM |
322 | extern const struct btrace_insn * |
323 | btrace_insn_get (const struct btrace_insn_iterator *); | |
324 | ||
325 | /* Return the instruction number for a branch trace iterator. | |
326 | Returns one past the maximum instruction number for the end iterator. | |
327 | Returns zero if the iterator does not point to a valid instruction. */ | |
328 | extern unsigned int btrace_insn_number (const struct btrace_insn_iterator *); | |
329 | ||
330 | /* Initialize a branch trace instruction iterator to point to the begin/end of | |
331 | the branch trace. Throws an error if there is no branch trace. */ | |
332 | extern void btrace_insn_begin (struct btrace_insn_iterator *, | |
333 | const struct btrace_thread_info *); | |
334 | extern void btrace_insn_end (struct btrace_insn_iterator *, | |
335 | const struct btrace_thread_info *); | |
336 | ||
337 | /* Increment/decrement a branch trace instruction iterator by at most STRIDE | |
338 | instructions. Return the number of instructions by which the instruction | |
339 | iterator has been advanced. | |
340 | Returns zero, if the operation failed or STRIDE had been zero. */ | |
341 | extern unsigned int btrace_insn_next (struct btrace_insn_iterator *, | |
342 | unsigned int stride); | |
343 | extern unsigned int btrace_insn_prev (struct btrace_insn_iterator *, | |
344 | unsigned int stride); | |
345 | ||
346 | /* Compare two branch trace instruction iterators. | |
347 | Return a negative number if LHS < RHS. | |
348 | Return zero if LHS == RHS. | |
349 | Return a positive number if LHS > RHS. */ | |
350 | extern int btrace_insn_cmp (const struct btrace_insn_iterator *lhs, | |
351 | const struct btrace_insn_iterator *rhs); | |
352 | ||
353 | /* Find an instruction in the function branch trace by its number. | |
354 | If the instruction is found, initialize the branch trace instruction | |
355 | iterator to point to this instruction and return non-zero. | |
356 | Return zero otherwise. */ | |
357 | extern int btrace_find_insn_by_number (struct btrace_insn_iterator *, | |
358 | const struct btrace_thread_info *, | |
359 | unsigned int number); | |
360 | ||
361 | /* Dereference a branch trace call iterator. Return a pointer to the | |
362 | function the iterator points to or NULL if the interator points past | |
363 | the end of the branch trace. */ | |
364 | extern const struct btrace_function * | |
365 | btrace_call_get (const struct btrace_call_iterator *); | |
366 | ||
367 | /* Return the function number for a branch trace call iterator. | |
368 | Returns one past the maximum function number for the end iterator. | |
369 | Returns zero if the iterator does not point to a valid function. */ | |
370 | extern unsigned int btrace_call_number (const struct btrace_call_iterator *); | |
371 | ||
372 | /* Initialize a branch trace call iterator to point to the begin/end of | |
373 | the branch trace. Throws an error if there is no branch trace. */ | |
374 | extern void btrace_call_begin (struct btrace_call_iterator *, | |
375 | const struct btrace_thread_info *); | |
376 | extern void btrace_call_end (struct btrace_call_iterator *, | |
377 | const struct btrace_thread_info *); | |
378 | ||
379 | /* Increment/decrement a branch trace call iterator by at most STRIDE function | |
380 | segments. Return the number of function segments by which the call | |
381 | iterator has been advanced. | |
382 | Returns zero, if the operation failed or STRIDE had been zero. */ | |
383 | extern unsigned int btrace_call_next (struct btrace_call_iterator *, | |
384 | unsigned int stride); | |
385 | extern unsigned int btrace_call_prev (struct btrace_call_iterator *, | |
386 | unsigned int stride); | |
387 | ||
388 | /* Compare two branch trace call iterators. | |
389 | Return a negative number if LHS < RHS. | |
390 | Return zero if LHS == RHS. | |
391 | Return a positive number if LHS > RHS. */ | |
392 | extern int btrace_call_cmp (const struct btrace_call_iterator *lhs, | |
393 | const struct btrace_call_iterator *rhs); | |
394 | ||
395 | /* Find a function in the function branch trace by its NUMBER. | |
396 | If the function is found, initialize the branch trace call | |
397 | iterator to point to this function and return non-zero. | |
398 | Return zero otherwise. */ | |
399 | extern int btrace_find_call_by_number (struct btrace_call_iterator *, | |
400 | const struct btrace_thread_info *, | |
401 | unsigned int number); | |
402 | ||
403 | /* Set the branch trace instruction history from BEGIN (inclusive) to | |
404 | END (exclusive). */ | |
405 | extern void btrace_set_insn_history (struct btrace_thread_info *, | |
406 | const struct btrace_insn_iterator *begin, | |
407 | const struct btrace_insn_iterator *end); | |
408 | ||
409 | /* Set the branch trace function call history from BEGIN (inclusive) to | |
410 | END (exclusive). */ | |
411 | extern void btrace_set_call_history (struct btrace_thread_info *, | |
412 | const struct btrace_call_iterator *begin, | |
413 | const struct btrace_call_iterator *end); | |
414 | ||
07bbe694 MM |
415 | /* Determine if branch tracing is currently replaying TP. */ |
416 | extern int btrace_is_replaying (struct thread_info *tp); | |
417 | ||
6e07b1d2 MM |
418 | /* Return non-zero if the branch trace for TP is empty; zero otherwise. */ |
419 | extern int btrace_is_empty (struct thread_info *tp); | |
420 | ||
734b0e4b MM |
421 | /* Create a cleanup for DATA. */ |
422 | extern struct cleanup *make_cleanup_btrace_data (struct btrace_data *data); | |
6e07b1d2 | 423 | |
02d27625 | 424 | #endif /* BTRACE_H */ |