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