| 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 | #include "defs.h" |
| 23 | #include "btrace.h" |
| 24 | #include "gdbthread.h" |
| 25 | #include "inferior.h" |
| 26 | #include "target.h" |
| 27 | #include "record.h" |
| 28 | #include "symtab.h" |
| 29 | #include "disasm.h" |
| 30 | #include "source.h" |
| 31 | #include "filenames.h" |
| 32 | #include "xml-support.h" |
| 33 | #include "regcache.h" |
| 34 | #include "rsp-low.h" |
| 35 | #include "gdbcmd.h" |
| 36 | #include "cli/cli-utils.h" |
| 37 | |
| 38 | #include <inttypes.h> |
| 39 | #include <ctype.h> |
| 40 | #include <algorithm> |
| 41 | |
| 42 | /* Command lists for btrace maintenance commands. */ |
| 43 | static struct cmd_list_element *maint_btrace_cmdlist; |
| 44 | static struct cmd_list_element *maint_btrace_set_cmdlist; |
| 45 | static struct cmd_list_element *maint_btrace_show_cmdlist; |
| 46 | static struct cmd_list_element *maint_btrace_pt_set_cmdlist; |
| 47 | static struct cmd_list_element *maint_btrace_pt_show_cmdlist; |
| 48 | |
| 49 | /* Control whether to skip PAD packets when computing the packet history. */ |
| 50 | static int maint_btrace_pt_skip_pad = 1; |
| 51 | |
| 52 | /* A vector of function segments. */ |
| 53 | typedef struct btrace_function * bfun_s; |
| 54 | DEF_VEC_P (bfun_s); |
| 55 | |
| 56 | static void btrace_add_pc (struct thread_info *tp); |
| 57 | |
| 58 | /* Print a record debug message. Use do ... while (0) to avoid ambiguities |
| 59 | when used in if statements. */ |
| 60 | |
| 61 | #define DEBUG(msg, args...) \ |
| 62 | do \ |
| 63 | { \ |
| 64 | if (record_debug != 0) \ |
| 65 | fprintf_unfiltered (gdb_stdlog, \ |
| 66 | "[btrace] " msg "\n", ##args); \ |
| 67 | } \ |
| 68 | while (0) |
| 69 | |
| 70 | #define DEBUG_FTRACE(msg, args...) DEBUG ("[ftrace] " msg, ##args) |
| 71 | |
| 72 | /* Return the function name of a recorded function segment for printing. |
| 73 | This function never returns NULL. */ |
| 74 | |
| 75 | static const char * |
| 76 | ftrace_print_function_name (const struct btrace_function *bfun) |
| 77 | { |
| 78 | struct minimal_symbol *msym; |
| 79 | struct symbol *sym; |
| 80 | |
| 81 | msym = bfun->msym; |
| 82 | sym = bfun->sym; |
| 83 | |
| 84 | if (sym != NULL) |
| 85 | return SYMBOL_PRINT_NAME (sym); |
| 86 | |
| 87 | if (msym != NULL) |
| 88 | return MSYMBOL_PRINT_NAME (msym); |
| 89 | |
| 90 | return "<unknown>"; |
| 91 | } |
| 92 | |
| 93 | /* Return the file name of a recorded function segment for printing. |
| 94 | This function never returns NULL. */ |
| 95 | |
| 96 | static const char * |
| 97 | ftrace_print_filename (const struct btrace_function *bfun) |
| 98 | { |
| 99 | struct symbol *sym; |
| 100 | const char *filename; |
| 101 | |
| 102 | sym = bfun->sym; |
| 103 | |
| 104 | if (sym != NULL) |
| 105 | filename = symtab_to_filename_for_display (symbol_symtab (sym)); |
| 106 | else |
| 107 | filename = "<unknown>"; |
| 108 | |
| 109 | return filename; |
| 110 | } |
| 111 | |
| 112 | /* Return a string representation of the address of an instruction. |
| 113 | This function never returns NULL. */ |
| 114 | |
| 115 | static const char * |
| 116 | ftrace_print_insn_addr (const struct btrace_insn *insn) |
| 117 | { |
| 118 | if (insn == NULL) |
| 119 | return "<nil>"; |
| 120 | |
| 121 | return core_addr_to_string_nz (insn->pc); |
| 122 | } |
| 123 | |
| 124 | /* Print an ftrace debug status message. */ |
| 125 | |
| 126 | static void |
| 127 | ftrace_debug (const struct btrace_function *bfun, const char *prefix) |
| 128 | { |
| 129 | const char *fun, *file; |
| 130 | unsigned int ibegin, iend; |
| 131 | int level; |
| 132 | |
| 133 | fun = ftrace_print_function_name (bfun); |
| 134 | file = ftrace_print_filename (bfun); |
| 135 | level = bfun->level; |
| 136 | |
| 137 | ibegin = bfun->insn_offset; |
| 138 | iend = ibegin + VEC_length (btrace_insn_s, bfun->insn); |
| 139 | |
| 140 | DEBUG_FTRACE ("%s: fun = %s, file = %s, level = %d, insn = [%u; %u)", |
| 141 | prefix, fun, file, level, ibegin, iend); |
| 142 | } |
| 143 | |
| 144 | /* Return non-zero if BFUN does not match MFUN and FUN, |
| 145 | return zero otherwise. */ |
| 146 | |
| 147 | static int |
| 148 | ftrace_function_switched (const struct btrace_function *bfun, |
| 149 | const struct minimal_symbol *mfun, |
| 150 | const struct symbol *fun) |
| 151 | { |
| 152 | struct minimal_symbol *msym; |
| 153 | struct symbol *sym; |
| 154 | |
| 155 | msym = bfun->msym; |
| 156 | sym = bfun->sym; |
| 157 | |
| 158 | /* If the minimal symbol changed, we certainly switched functions. */ |
| 159 | if (mfun != NULL && msym != NULL |
| 160 | && strcmp (MSYMBOL_LINKAGE_NAME (mfun), MSYMBOL_LINKAGE_NAME (msym)) != 0) |
| 161 | return 1; |
| 162 | |
| 163 | /* If the symbol changed, we certainly switched functions. */ |
| 164 | if (fun != NULL && sym != NULL) |
| 165 | { |
| 166 | const char *bfname, *fname; |
| 167 | |
| 168 | /* Check the function name. */ |
| 169 | if (strcmp (SYMBOL_LINKAGE_NAME (fun), SYMBOL_LINKAGE_NAME (sym)) != 0) |
| 170 | return 1; |
| 171 | |
| 172 | /* Check the location of those functions, as well. */ |
| 173 | bfname = symtab_to_fullname (symbol_symtab (sym)); |
| 174 | fname = symtab_to_fullname (symbol_symtab (fun)); |
| 175 | if (filename_cmp (fname, bfname) != 0) |
| 176 | return 1; |
| 177 | } |
| 178 | |
| 179 | /* If we lost symbol information, we switched functions. */ |
| 180 | if (!(msym == NULL && sym == NULL) && mfun == NULL && fun == NULL) |
| 181 | return 1; |
| 182 | |
| 183 | /* If we gained symbol information, we switched functions. */ |
| 184 | if (msym == NULL && sym == NULL && !(mfun == NULL && fun == NULL)) |
| 185 | return 1; |
| 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | /* Allocate and initialize a new branch trace function segment. |
| 191 | PREV is the chronologically preceding function segment. |
| 192 | MFUN and FUN are the symbol information we have for this function. */ |
| 193 | |
| 194 | static struct btrace_function * |
| 195 | ftrace_new_function (struct btrace_function *prev, |
| 196 | struct minimal_symbol *mfun, |
| 197 | struct symbol *fun) |
| 198 | { |
| 199 | struct btrace_function *bfun; |
| 200 | |
| 201 | bfun = XCNEW (struct btrace_function); |
| 202 | |
| 203 | bfun->msym = mfun; |
| 204 | bfun->sym = fun; |
| 205 | bfun->flow.prev = prev; |
| 206 | |
| 207 | if (prev == NULL) |
| 208 | { |
| 209 | /* Start counting at one. */ |
| 210 | bfun->number = 1; |
| 211 | bfun->insn_offset = 1; |
| 212 | } |
| 213 | else |
| 214 | { |
| 215 | gdb_assert (prev->flow.next == NULL); |
| 216 | prev->flow.next = bfun; |
| 217 | |
| 218 | bfun->number = prev->number + 1; |
| 219 | bfun->insn_offset = (prev->insn_offset |
| 220 | + VEC_length (btrace_insn_s, prev->insn)); |
| 221 | bfun->level = prev->level; |
| 222 | } |
| 223 | |
| 224 | return bfun; |
| 225 | } |
| 226 | |
| 227 | /* Update the UP field of a function segment. */ |
| 228 | |
| 229 | static void |
| 230 | ftrace_update_caller (struct btrace_function *bfun, |
| 231 | struct btrace_function *caller, |
| 232 | enum btrace_function_flag flags) |
| 233 | { |
| 234 | if (bfun->up != NULL) |
| 235 | ftrace_debug (bfun, "updating caller"); |
| 236 | |
| 237 | bfun->up = caller; |
| 238 | bfun->flags = flags; |
| 239 | |
| 240 | ftrace_debug (bfun, "set caller"); |
| 241 | ftrace_debug (caller, "..to"); |
| 242 | } |
| 243 | |
| 244 | /* Fix up the caller for all segments of a function. */ |
| 245 | |
| 246 | static void |
| 247 | ftrace_fixup_caller (struct btrace_function *bfun, |
| 248 | struct btrace_function *caller, |
| 249 | enum btrace_function_flag flags) |
| 250 | { |
| 251 | struct btrace_function *prev, *next; |
| 252 | |
| 253 | ftrace_update_caller (bfun, caller, flags); |
| 254 | |
| 255 | /* Update all function segments belonging to the same function. */ |
| 256 | for (prev = bfun->segment.prev; prev != NULL; prev = prev->segment.prev) |
| 257 | ftrace_update_caller (prev, caller, flags); |
| 258 | |
| 259 | for (next = bfun->segment.next; next != NULL; next = next->segment.next) |
| 260 | ftrace_update_caller (next, caller, flags); |
| 261 | } |
| 262 | |
| 263 | /* Add a new function segment for a call. |
| 264 | CALLER is the chronologically preceding function segment. |
| 265 | MFUN and FUN are the symbol information we have for this function. */ |
| 266 | |
| 267 | static struct btrace_function * |
| 268 | ftrace_new_call (struct btrace_function *caller, |
| 269 | struct minimal_symbol *mfun, |
| 270 | struct symbol *fun) |
| 271 | { |
| 272 | struct btrace_function *bfun; |
| 273 | |
| 274 | bfun = ftrace_new_function (caller, mfun, fun); |
| 275 | bfun->up = caller; |
| 276 | bfun->level += 1; |
| 277 | |
| 278 | ftrace_debug (bfun, "new call"); |
| 279 | |
| 280 | return bfun; |
| 281 | } |
| 282 | |
| 283 | /* Add a new function segment for a tail call. |
| 284 | CALLER is the chronologically preceding function segment. |
| 285 | MFUN and FUN are the symbol information we have for this function. */ |
| 286 | |
| 287 | static struct btrace_function * |
| 288 | ftrace_new_tailcall (struct btrace_function *caller, |
| 289 | struct minimal_symbol *mfun, |
| 290 | struct symbol *fun) |
| 291 | { |
| 292 | struct btrace_function *bfun; |
| 293 | |
| 294 | bfun = ftrace_new_function (caller, mfun, fun); |
| 295 | bfun->up = caller; |
| 296 | bfun->level += 1; |
| 297 | bfun->flags |= BFUN_UP_LINKS_TO_TAILCALL; |
| 298 | |
| 299 | ftrace_debug (bfun, "new tail call"); |
| 300 | |
| 301 | return bfun; |
| 302 | } |
| 303 | |
| 304 | /* Return the caller of BFUN or NULL if there is none. This function skips |
| 305 | tail calls in the call chain. */ |
| 306 | static struct btrace_function * |
| 307 | ftrace_get_caller (struct btrace_function *bfun) |
| 308 | { |
| 309 | for (; bfun != NULL; bfun = bfun->up) |
| 310 | if ((bfun->flags & BFUN_UP_LINKS_TO_TAILCALL) == 0) |
| 311 | return bfun->up; |
| 312 | |
| 313 | return NULL; |
| 314 | } |
| 315 | |
| 316 | /* Find the innermost caller in the back trace of BFUN with MFUN/FUN |
| 317 | symbol information. */ |
| 318 | |
| 319 | static struct btrace_function * |
| 320 | ftrace_find_caller (struct btrace_function *bfun, |
| 321 | struct minimal_symbol *mfun, |
| 322 | struct symbol *fun) |
| 323 | { |
| 324 | for (; bfun != NULL; bfun = bfun->up) |
| 325 | { |
| 326 | /* Skip functions with incompatible symbol information. */ |
| 327 | if (ftrace_function_switched (bfun, mfun, fun)) |
| 328 | continue; |
| 329 | |
| 330 | /* This is the function segment we're looking for. */ |
| 331 | break; |
| 332 | } |
| 333 | |
| 334 | return bfun; |
| 335 | } |
| 336 | |
| 337 | /* Find the innermost caller in the back trace of BFUN, skipping all |
| 338 | function segments that do not end with a call instruction (e.g. |
| 339 | tail calls ending with a jump). */ |
| 340 | |
| 341 | static struct btrace_function * |
| 342 | ftrace_find_call (struct btrace_function *bfun) |
| 343 | { |
| 344 | for (; bfun != NULL; bfun = bfun->up) |
| 345 | { |
| 346 | struct btrace_insn *last; |
| 347 | |
| 348 | /* Skip gaps. */ |
| 349 | if (bfun->errcode != 0) |
| 350 | continue; |
| 351 | |
| 352 | last = VEC_last (btrace_insn_s, bfun->insn); |
| 353 | |
| 354 | if (last->iclass == BTRACE_INSN_CALL) |
| 355 | break; |
| 356 | } |
| 357 | |
| 358 | return bfun; |
| 359 | } |
| 360 | |
| 361 | /* Add a continuation segment for a function into which we return. |
| 362 | PREV is the chronologically preceding function segment. |
| 363 | MFUN and FUN are the symbol information we have for this function. */ |
| 364 | |
| 365 | static struct btrace_function * |
| 366 | ftrace_new_return (struct btrace_function *prev, |
| 367 | struct minimal_symbol *mfun, |
| 368 | struct symbol *fun) |
| 369 | { |
| 370 | struct btrace_function *bfun, *caller; |
| 371 | |
| 372 | bfun = ftrace_new_function (prev, mfun, fun); |
| 373 | |
| 374 | /* It is important to start at PREV's caller. Otherwise, we might find |
| 375 | PREV itself, if PREV is a recursive function. */ |
| 376 | caller = ftrace_find_caller (prev->up, mfun, fun); |
| 377 | if (caller != NULL) |
| 378 | { |
| 379 | /* The caller of PREV is the preceding btrace function segment in this |
| 380 | function instance. */ |
| 381 | gdb_assert (caller->segment.next == NULL); |
| 382 | |
| 383 | caller->segment.next = bfun; |
| 384 | bfun->segment.prev = caller; |
| 385 | |
| 386 | /* Maintain the function level. */ |
| 387 | bfun->level = caller->level; |
| 388 | |
| 389 | /* Maintain the call stack. */ |
| 390 | bfun->up = caller->up; |
| 391 | bfun->flags = caller->flags; |
| 392 | |
| 393 | ftrace_debug (bfun, "new return"); |
| 394 | } |
| 395 | else |
| 396 | { |
| 397 | /* We did not find a caller. This could mean that something went |
| 398 | wrong or that the call is simply not included in the trace. */ |
| 399 | |
| 400 | /* Let's search for some actual call. */ |
| 401 | caller = ftrace_find_call (prev->up); |
| 402 | if (caller == NULL) |
| 403 | { |
| 404 | /* There is no call in PREV's back trace. We assume that the |
| 405 | branch trace did not include it. */ |
| 406 | |
| 407 | /* Let's find the topmost function and add a new caller for it. |
| 408 | This should handle a series of initial tail calls. */ |
| 409 | while (prev->up != NULL) |
| 410 | prev = prev->up; |
| 411 | |
| 412 | bfun->level = prev->level - 1; |
| 413 | |
| 414 | /* Fix up the call stack for PREV. */ |
| 415 | ftrace_fixup_caller (prev, bfun, BFUN_UP_LINKS_TO_RET); |
| 416 | |
| 417 | ftrace_debug (bfun, "new return - no caller"); |
| 418 | } |
| 419 | else |
| 420 | { |
| 421 | /* There is a call in PREV's back trace to which we should have |
| 422 | returned but didn't. Let's start a new, separate back trace |
| 423 | from PREV's level. */ |
| 424 | bfun->level = prev->level - 1; |
| 425 | |
| 426 | /* We fix up the back trace for PREV but leave other function segments |
| 427 | on the same level as they are. |
| 428 | This should handle things like schedule () correctly where we're |
| 429 | switching contexts. */ |
| 430 | prev->up = bfun; |
| 431 | prev->flags = BFUN_UP_LINKS_TO_RET; |
| 432 | |
| 433 | ftrace_debug (bfun, "new return - unknown caller"); |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | return bfun; |
| 438 | } |
| 439 | |
| 440 | /* Add a new function segment for a function switch. |
| 441 | PREV is the chronologically preceding function segment. |
| 442 | MFUN and FUN are the symbol information we have for this function. */ |
| 443 | |
| 444 | static struct btrace_function * |
| 445 | ftrace_new_switch (struct btrace_function *prev, |
| 446 | struct minimal_symbol *mfun, |
| 447 | struct symbol *fun) |
| 448 | { |
| 449 | struct btrace_function *bfun; |
| 450 | |
| 451 | /* This is an unexplained function switch. The call stack will likely |
| 452 | be wrong at this point. */ |
| 453 | bfun = ftrace_new_function (prev, mfun, fun); |
| 454 | |
| 455 | ftrace_debug (bfun, "new switch"); |
| 456 | |
| 457 | return bfun; |
| 458 | } |
| 459 | |
| 460 | /* Add a new function segment for a gap in the trace due to a decode error. |
| 461 | PREV is the chronologically preceding function segment. |
| 462 | ERRCODE is the format-specific error code. */ |
| 463 | |
| 464 | static struct btrace_function * |
| 465 | ftrace_new_gap (struct btrace_function *prev, int errcode) |
| 466 | { |
| 467 | struct btrace_function *bfun; |
| 468 | |
| 469 | /* We hijack prev if it was empty. */ |
| 470 | if (prev != NULL && prev->errcode == 0 |
| 471 | && VEC_empty (btrace_insn_s, prev->insn)) |
| 472 | bfun = prev; |
| 473 | else |
| 474 | bfun = ftrace_new_function (prev, NULL, NULL); |
| 475 | |
| 476 | bfun->errcode = errcode; |
| 477 | |
| 478 | ftrace_debug (bfun, "new gap"); |
| 479 | |
| 480 | return bfun; |
| 481 | } |
| 482 | |
| 483 | /* Update BFUN with respect to the instruction at PC. This may create new |
| 484 | function segments. |
| 485 | Return the chronologically latest function segment, never NULL. */ |
| 486 | |
| 487 | static struct btrace_function * |
| 488 | ftrace_update_function (struct btrace_function *bfun, CORE_ADDR pc) |
| 489 | { |
| 490 | struct bound_minimal_symbol bmfun; |
| 491 | struct minimal_symbol *mfun; |
| 492 | struct symbol *fun; |
| 493 | struct btrace_insn *last; |
| 494 | |
| 495 | /* Try to determine the function we're in. We use both types of symbols |
| 496 | to avoid surprises when we sometimes get a full symbol and sometimes |
| 497 | only a minimal symbol. */ |
| 498 | fun = find_pc_function (pc); |
| 499 | bmfun = lookup_minimal_symbol_by_pc (pc); |
| 500 | mfun = bmfun.minsym; |
| 501 | |
| 502 | if (fun == NULL && mfun == NULL) |
| 503 | DEBUG_FTRACE ("no symbol at %s", core_addr_to_string_nz (pc)); |
| 504 | |
| 505 | /* If we didn't have a function or if we had a gap before, we create one. */ |
| 506 | if (bfun == NULL || bfun->errcode != 0) |
| 507 | return ftrace_new_function (bfun, mfun, fun); |
| 508 | |
| 509 | /* Check the last instruction, if we have one. |
| 510 | We do this check first, since it allows us to fill in the call stack |
| 511 | links in addition to the normal flow links. */ |
| 512 | last = NULL; |
| 513 | if (!VEC_empty (btrace_insn_s, bfun->insn)) |
| 514 | last = VEC_last (btrace_insn_s, bfun->insn); |
| 515 | |
| 516 | if (last != NULL) |
| 517 | { |
| 518 | switch (last->iclass) |
| 519 | { |
| 520 | case BTRACE_INSN_RETURN: |
| 521 | { |
| 522 | const char *fname; |
| 523 | |
| 524 | /* On some systems, _dl_runtime_resolve returns to the resolved |
| 525 | function instead of jumping to it. From our perspective, |
| 526 | however, this is a tailcall. |
| 527 | If we treated it as return, we wouldn't be able to find the |
| 528 | resolved function in our stack back trace. Hence, we would |
| 529 | lose the current stack back trace and start anew with an empty |
| 530 | back trace. When the resolved function returns, we would then |
| 531 | create a stack back trace with the same function names but |
| 532 | different frame id's. This will confuse stepping. */ |
| 533 | fname = ftrace_print_function_name (bfun); |
| 534 | if (strcmp (fname, "_dl_runtime_resolve") == 0) |
| 535 | return ftrace_new_tailcall (bfun, mfun, fun); |
| 536 | |
| 537 | return ftrace_new_return (bfun, mfun, fun); |
| 538 | } |
| 539 | |
| 540 | case BTRACE_INSN_CALL: |
| 541 | /* Ignore calls to the next instruction. They are used for PIC. */ |
| 542 | if (last->pc + last->size == pc) |
| 543 | break; |
| 544 | |
| 545 | return ftrace_new_call (bfun, mfun, fun); |
| 546 | |
| 547 | case BTRACE_INSN_JUMP: |
| 548 | { |
| 549 | CORE_ADDR start; |
| 550 | |
| 551 | start = get_pc_function_start (pc); |
| 552 | |
| 553 | /* A jump to the start of a function is (typically) a tail call. */ |
| 554 | if (start == pc) |
| 555 | return ftrace_new_tailcall (bfun, mfun, fun); |
| 556 | |
| 557 | /* If we can't determine the function for PC, we treat a jump at |
| 558 | the end of the block as tail call if we're switching functions |
| 559 | and as an intra-function branch if we don't. */ |
| 560 | if (start == 0 && ftrace_function_switched (bfun, mfun, fun)) |
| 561 | return ftrace_new_tailcall (bfun, mfun, fun); |
| 562 | |
| 563 | break; |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | /* Check if we're switching functions for some other reason. */ |
| 569 | if (ftrace_function_switched (bfun, mfun, fun)) |
| 570 | { |
| 571 | DEBUG_FTRACE ("switching from %s in %s at %s", |
| 572 | ftrace_print_insn_addr (last), |
| 573 | ftrace_print_function_name (bfun), |
| 574 | ftrace_print_filename (bfun)); |
| 575 | |
| 576 | return ftrace_new_switch (bfun, mfun, fun); |
| 577 | } |
| 578 | |
| 579 | return bfun; |
| 580 | } |
| 581 | |
| 582 | /* Add the instruction at PC to BFUN's instructions. */ |
| 583 | |
| 584 | static void |
| 585 | ftrace_update_insns (struct btrace_function *bfun, |
| 586 | const struct btrace_insn *insn) |
| 587 | { |
| 588 | VEC_safe_push (btrace_insn_s, bfun->insn, insn); |
| 589 | |
| 590 | if (record_debug > 1) |
| 591 | ftrace_debug (bfun, "update insn"); |
| 592 | } |
| 593 | |
| 594 | /* Classify the instruction at PC. */ |
| 595 | |
| 596 | static enum btrace_insn_class |
| 597 | ftrace_classify_insn (struct gdbarch *gdbarch, CORE_ADDR pc) |
| 598 | { |
| 599 | enum btrace_insn_class iclass; |
| 600 | |
| 601 | iclass = BTRACE_INSN_OTHER; |
| 602 | TRY |
| 603 | { |
| 604 | if (gdbarch_insn_is_call (gdbarch, pc)) |
| 605 | iclass = BTRACE_INSN_CALL; |
| 606 | else if (gdbarch_insn_is_ret (gdbarch, pc)) |
| 607 | iclass = BTRACE_INSN_RETURN; |
| 608 | else if (gdbarch_insn_is_jump (gdbarch, pc)) |
| 609 | iclass = BTRACE_INSN_JUMP; |
| 610 | } |
| 611 | CATCH (error, RETURN_MASK_ERROR) |
| 612 | { |
| 613 | } |
| 614 | END_CATCH |
| 615 | |
| 616 | return iclass; |
| 617 | } |
| 618 | |
| 619 | /* Try to match the back trace at LHS to the back trace at RHS. Returns the |
| 620 | number of matching function segments or zero if the back traces do not |
| 621 | match. */ |
| 622 | |
| 623 | static int |
| 624 | ftrace_match_backtrace (struct btrace_function *lhs, |
| 625 | struct btrace_function *rhs) |
| 626 | { |
| 627 | int matches; |
| 628 | |
| 629 | for (matches = 0; lhs != NULL && rhs != NULL; ++matches) |
| 630 | { |
| 631 | if (ftrace_function_switched (lhs, rhs->msym, rhs->sym)) |
| 632 | return 0; |
| 633 | |
| 634 | lhs = ftrace_get_caller (lhs); |
| 635 | rhs = ftrace_get_caller (rhs); |
| 636 | } |
| 637 | |
| 638 | return matches; |
| 639 | } |
| 640 | |
| 641 | /* Add ADJUSTMENT to the level of BFUN and succeeding function segments. */ |
| 642 | |
| 643 | static void |
| 644 | ftrace_fixup_level (struct btrace_function *bfun, int adjustment) |
| 645 | { |
| 646 | if (adjustment == 0) |
| 647 | return; |
| 648 | |
| 649 | DEBUG_FTRACE ("fixup level (%+d)", adjustment); |
| 650 | ftrace_debug (bfun, "..bfun"); |
| 651 | |
| 652 | for (; bfun != NULL; bfun = bfun->flow.next) |
| 653 | bfun->level += adjustment; |
| 654 | } |
| 655 | |
| 656 | /* Recompute the global level offset. Traverse the function trace and compute |
| 657 | the global level offset as the negative of the minimal function level. */ |
| 658 | |
| 659 | static void |
| 660 | ftrace_compute_global_level_offset (struct btrace_thread_info *btinfo) |
| 661 | { |
| 662 | struct btrace_function *bfun, *end; |
| 663 | int level; |
| 664 | |
| 665 | if (btinfo == NULL) |
| 666 | return; |
| 667 | |
| 668 | bfun = btinfo->begin; |
| 669 | if (bfun == NULL) |
| 670 | return; |
| 671 | |
| 672 | /* The last function segment contains the current instruction, which is not |
| 673 | really part of the trace. If it contains just this one instruction, we |
| 674 | stop when we reach it; otherwise, we let the below loop run to the end. */ |
| 675 | end = btinfo->end; |
| 676 | if (VEC_length (btrace_insn_s, end->insn) > 1) |
| 677 | end = NULL; |
| 678 | |
| 679 | level = INT_MAX; |
| 680 | for (; bfun != end; bfun = bfun->flow.next) |
| 681 | level = std::min (level, bfun->level); |
| 682 | |
| 683 | DEBUG_FTRACE ("setting global level offset: %d", -level); |
| 684 | btinfo->level = -level; |
| 685 | } |
| 686 | |
| 687 | /* Connect the function segments PREV and NEXT in a bottom-to-top walk as in |
| 688 | ftrace_connect_backtrace. */ |
| 689 | |
| 690 | static void |
| 691 | ftrace_connect_bfun (struct btrace_function *prev, |
| 692 | struct btrace_function *next) |
| 693 | { |
| 694 | DEBUG_FTRACE ("connecting..."); |
| 695 | ftrace_debug (prev, "..prev"); |
| 696 | ftrace_debug (next, "..next"); |
| 697 | |
| 698 | /* The function segments are not yet connected. */ |
| 699 | gdb_assert (prev->segment.next == NULL); |
| 700 | gdb_assert (next->segment.prev == NULL); |
| 701 | |
| 702 | prev->segment.next = next; |
| 703 | next->segment.prev = prev; |
| 704 | |
| 705 | /* We may have moved NEXT to a different function level. */ |
| 706 | ftrace_fixup_level (next, prev->level - next->level); |
| 707 | |
| 708 | /* If we run out of back trace for one, let's use the other's. */ |
| 709 | if (prev->up == NULL) |
| 710 | { |
| 711 | if (next->up != NULL) |
| 712 | { |
| 713 | DEBUG_FTRACE ("using next's callers"); |
| 714 | ftrace_fixup_caller (prev, next->up, next->flags); |
| 715 | } |
| 716 | } |
| 717 | else if (next->up == NULL) |
| 718 | { |
| 719 | if (prev->up != NULL) |
| 720 | { |
| 721 | DEBUG_FTRACE ("using prev's callers"); |
| 722 | ftrace_fixup_caller (next, prev->up, prev->flags); |
| 723 | } |
| 724 | } |
| 725 | else |
| 726 | { |
| 727 | /* PREV may have a tailcall caller, NEXT can't. If it does, fixup the up |
| 728 | link to add the tail callers to NEXT's back trace. |
| 729 | |
| 730 | This removes NEXT->UP from NEXT's back trace. It will be added back |
| 731 | when connecting NEXT and PREV's callers - provided they exist. |
| 732 | |
| 733 | If PREV's back trace consists of a series of tail calls without an |
| 734 | actual call, there will be no further connection and NEXT's caller will |
| 735 | be removed for good. To catch this case, we handle it here and connect |
| 736 | the top of PREV's back trace to NEXT's caller. */ |
| 737 | if ((prev->flags & BFUN_UP_LINKS_TO_TAILCALL) != 0) |
| 738 | { |
| 739 | struct btrace_function *caller; |
| 740 | btrace_function_flags flags; |
| 741 | |
| 742 | /* We checked NEXT->UP above so CALLER can't be NULL. */ |
| 743 | caller = next->up; |
| 744 | flags = next->flags; |
| 745 | |
| 746 | DEBUG_FTRACE ("adding prev's tail calls to next"); |
| 747 | |
| 748 | ftrace_fixup_caller (next, prev->up, prev->flags); |
| 749 | |
| 750 | for (prev = prev->up; prev != NULL; prev = prev->up) |
| 751 | { |
| 752 | /* At the end of PREV's back trace, continue with CALLER. */ |
| 753 | if (prev->up == NULL) |
| 754 | { |
| 755 | DEBUG_FTRACE ("fixing up link for tailcall chain"); |
| 756 | ftrace_debug (prev, "..top"); |
| 757 | ftrace_debug (caller, "..up"); |
| 758 | |
| 759 | ftrace_fixup_caller (prev, caller, flags); |
| 760 | |
| 761 | /* If we skipped any tail calls, this may move CALLER to a |
| 762 | different function level. |
| 763 | |
| 764 | Note that changing CALLER's level is only OK because we |
| 765 | know that this is the last iteration of the bottom-to-top |
| 766 | walk in ftrace_connect_backtrace. |
| 767 | |
| 768 | Otherwise we will fix up CALLER's level when we connect it |
| 769 | to PREV's caller in the next iteration. */ |
| 770 | ftrace_fixup_level (caller, prev->level - caller->level - 1); |
| 771 | break; |
| 772 | } |
| 773 | |
| 774 | /* There's nothing to do if we find a real call. */ |
| 775 | if ((prev->flags & BFUN_UP_LINKS_TO_TAILCALL) == 0) |
| 776 | { |
| 777 | DEBUG_FTRACE ("will fix up link in next iteration"); |
| 778 | break; |
| 779 | } |
| 780 | } |
| 781 | } |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | /* Connect function segments on the same level in the back trace at LHS and RHS. |
| 786 | The back traces at LHS and RHS are expected to match according to |
| 787 | ftrace_match_backtrace. */ |
| 788 | |
| 789 | static void |
| 790 | ftrace_connect_backtrace (struct btrace_function *lhs, |
| 791 | struct btrace_function *rhs) |
| 792 | { |
| 793 | while (lhs != NULL && rhs != NULL) |
| 794 | { |
| 795 | struct btrace_function *prev, *next; |
| 796 | |
| 797 | gdb_assert (!ftrace_function_switched (lhs, rhs->msym, rhs->sym)); |
| 798 | |
| 799 | /* Connecting LHS and RHS may change the up link. */ |
| 800 | prev = lhs; |
| 801 | next = rhs; |
| 802 | |
| 803 | lhs = ftrace_get_caller (lhs); |
| 804 | rhs = ftrace_get_caller (rhs); |
| 805 | |
| 806 | ftrace_connect_bfun (prev, next); |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | /* Bridge the gap between two function segments left and right of a gap if their |
| 811 | respective back traces match in at least MIN_MATCHES functions. |
| 812 | |
| 813 | Returns non-zero if the gap could be bridged, zero otherwise. */ |
| 814 | |
| 815 | static int |
| 816 | ftrace_bridge_gap (struct btrace_function *lhs, struct btrace_function *rhs, |
| 817 | int min_matches) |
| 818 | { |
| 819 | struct btrace_function *best_l, *best_r, *cand_l, *cand_r; |
| 820 | int best_matches; |
| 821 | |
| 822 | DEBUG_FTRACE ("checking gap at insn %u (req matches: %d)", |
| 823 | rhs->insn_offset - 1, min_matches); |
| 824 | |
| 825 | best_matches = 0; |
| 826 | best_l = NULL; |
| 827 | best_r = NULL; |
| 828 | |
| 829 | /* We search the back traces of LHS and RHS for valid connections and connect |
| 830 | the two functon segments that give the longest combined back trace. */ |
| 831 | |
| 832 | for (cand_l = lhs; cand_l != NULL; cand_l = ftrace_get_caller (cand_l)) |
| 833 | for (cand_r = rhs; cand_r != NULL; cand_r = ftrace_get_caller (cand_r)) |
| 834 | { |
| 835 | int matches; |
| 836 | |
| 837 | matches = ftrace_match_backtrace (cand_l, cand_r); |
| 838 | if (best_matches < matches) |
| 839 | { |
| 840 | best_matches = matches; |
| 841 | best_l = cand_l; |
| 842 | best_r = cand_r; |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | /* We need at least MIN_MATCHES matches. */ |
| 847 | gdb_assert (min_matches > 0); |
| 848 | if (best_matches < min_matches) |
| 849 | return 0; |
| 850 | |
| 851 | DEBUG_FTRACE ("..matches: %d", best_matches); |
| 852 | |
| 853 | /* We will fix up the level of BEST_R and succeeding function segments such |
| 854 | that BEST_R's level matches BEST_L's when we connect BEST_L to BEST_R. |
| 855 | |
| 856 | This will ignore the level of RHS and following if BEST_R != RHS. I.e. if |
| 857 | BEST_R is a successor of RHS in the back trace of RHS (phases 1 and 3). |
| 858 | |
| 859 | To catch this, we already fix up the level here where we can start at RHS |
| 860 | instead of at BEST_R. We will ignore the level fixup when connecting |
| 861 | BEST_L to BEST_R as they will already be on the same level. */ |
| 862 | ftrace_fixup_level (rhs, best_l->level - best_r->level); |
| 863 | |
| 864 | ftrace_connect_backtrace (best_l, best_r); |
| 865 | |
| 866 | return best_matches; |
| 867 | } |
| 868 | |
| 869 | /* Try to bridge gaps due to overflow or decode errors by connecting the |
| 870 | function segments that are separated by the gap. */ |
| 871 | |
| 872 | static void |
| 873 | btrace_bridge_gaps (struct thread_info *tp, VEC (bfun_s) **gaps) |
| 874 | { |
| 875 | VEC (bfun_s) *remaining; |
| 876 | struct cleanup *old_chain; |
| 877 | int min_matches; |
| 878 | |
| 879 | DEBUG ("bridge gaps"); |
| 880 | |
| 881 | remaining = NULL; |
| 882 | old_chain = make_cleanup (VEC_cleanup (bfun_s), &remaining); |
| 883 | |
| 884 | /* We require a minimum amount of matches for bridging a gap. The number of |
| 885 | required matches will be lowered with each iteration. |
| 886 | |
| 887 | The more matches the higher our confidence that the bridging is correct. |
| 888 | For big gaps or small traces, however, it may not be feasible to require a |
| 889 | high number of matches. */ |
| 890 | for (min_matches = 5; min_matches > 0; --min_matches) |
| 891 | { |
| 892 | /* Let's try to bridge as many gaps as we can. In some cases, we need to |
| 893 | skip a gap and revisit it again after we closed later gaps. */ |
| 894 | while (!VEC_empty (bfun_s, *gaps)) |
| 895 | { |
| 896 | struct btrace_function *gap; |
| 897 | unsigned int idx; |
| 898 | |
| 899 | for (idx = 0; VEC_iterate (bfun_s, *gaps, idx, gap); ++idx) |
| 900 | { |
| 901 | struct btrace_function *lhs, *rhs; |
| 902 | int bridged; |
| 903 | |
| 904 | /* We may have a sequence of gaps if we run from one error into |
| 905 | the next as we try to re-sync onto the trace stream. Ignore |
| 906 | all but the leftmost gap in such a sequence. |
| 907 | |
| 908 | Also ignore gaps at the beginning of the trace. */ |
| 909 | lhs = gap->flow.prev; |
| 910 | if (lhs == NULL || lhs->errcode != 0) |
| 911 | continue; |
| 912 | |
| 913 | /* Skip gaps to the right. */ |
| 914 | for (rhs = gap->flow.next; rhs != NULL; rhs = rhs->flow.next) |
| 915 | if (rhs->errcode == 0) |
| 916 | break; |
| 917 | |
| 918 | /* Ignore gaps at the end of the trace. */ |
| 919 | if (rhs == NULL) |
| 920 | continue; |
| 921 | |
| 922 | bridged = ftrace_bridge_gap (lhs, rhs, min_matches); |
| 923 | |
| 924 | /* Keep track of gaps we were not able to bridge and try again. |
| 925 | If we just pushed them to the end of GAPS we would risk an |
| 926 | infinite loop in case we simply cannot bridge a gap. */ |
| 927 | if (bridged == 0) |
| 928 | VEC_safe_push (bfun_s, remaining, gap); |
| 929 | } |
| 930 | |
| 931 | /* Let's see if we made any progress. */ |
| 932 | if (VEC_length (bfun_s, remaining) == VEC_length (bfun_s, *gaps)) |
| 933 | break; |
| 934 | |
| 935 | VEC_free (bfun_s, *gaps); |
| 936 | |
| 937 | *gaps = remaining; |
| 938 | remaining = NULL; |
| 939 | } |
| 940 | |
| 941 | /* We get here if either GAPS is empty or if GAPS equals REMAINING. */ |
| 942 | if (VEC_empty (bfun_s, *gaps)) |
| 943 | break; |
| 944 | |
| 945 | VEC_free (bfun_s, remaining); |
| 946 | } |
| 947 | |
| 948 | do_cleanups (old_chain); |
| 949 | |
| 950 | /* We may omit this in some cases. Not sure it is worth the extra |
| 951 | complication, though. */ |
| 952 | ftrace_compute_global_level_offset (&tp->btrace); |
| 953 | } |
| 954 | |
| 955 | /* Compute the function branch trace from BTS trace. */ |
| 956 | |
| 957 | static void |
| 958 | btrace_compute_ftrace_bts (struct thread_info *tp, |
| 959 | const struct btrace_data_bts *btrace, |
| 960 | VEC (bfun_s) **gaps) |
| 961 | { |
| 962 | struct btrace_thread_info *btinfo; |
| 963 | struct btrace_function *begin, *end; |
| 964 | struct gdbarch *gdbarch; |
| 965 | unsigned int blk; |
| 966 | int level; |
| 967 | |
| 968 | gdbarch = target_gdbarch (); |
| 969 | btinfo = &tp->btrace; |
| 970 | begin = btinfo->begin; |
| 971 | end = btinfo->end; |
| 972 | level = begin != NULL ? -btinfo->level : INT_MAX; |
| 973 | blk = VEC_length (btrace_block_s, btrace->blocks); |
| 974 | |
| 975 | while (blk != 0) |
| 976 | { |
| 977 | btrace_block_s *block; |
| 978 | CORE_ADDR pc; |
| 979 | |
| 980 | blk -= 1; |
| 981 | |
| 982 | block = VEC_index (btrace_block_s, btrace->blocks, blk); |
| 983 | pc = block->begin; |
| 984 | |
| 985 | for (;;) |
| 986 | { |
| 987 | struct btrace_insn insn; |
| 988 | int size; |
| 989 | |
| 990 | /* We should hit the end of the block. Warn if we went too far. */ |
| 991 | if (block->end < pc) |
| 992 | { |
| 993 | /* Indicate the gap in the trace. */ |
| 994 | end = ftrace_new_gap (end, BDE_BTS_OVERFLOW); |
| 995 | if (begin == NULL) |
| 996 | begin = end; |
| 997 | |
| 998 | VEC_safe_push (bfun_s, *gaps, end); |
| 999 | |
| 1000 | warning (_("Recorded trace may be corrupted at instruction " |
| 1001 | "%u (pc = %s)."), end->insn_offset - 1, |
| 1002 | core_addr_to_string_nz (pc)); |
| 1003 | |
| 1004 | break; |
| 1005 | } |
| 1006 | |
| 1007 | end = ftrace_update_function (end, pc); |
| 1008 | if (begin == NULL) |
| 1009 | begin = end; |
| 1010 | |
| 1011 | /* Maintain the function level offset. |
| 1012 | For all but the last block, we do it here. */ |
| 1013 | if (blk != 0) |
| 1014 | level = std::min (level, end->level); |
| 1015 | |
| 1016 | size = 0; |
| 1017 | TRY |
| 1018 | { |
| 1019 | size = gdb_insn_length (gdbarch, pc); |
| 1020 | } |
| 1021 | CATCH (error, RETURN_MASK_ERROR) |
| 1022 | { |
| 1023 | } |
| 1024 | END_CATCH |
| 1025 | |
| 1026 | insn.pc = pc; |
| 1027 | insn.size = size; |
| 1028 | insn.iclass = ftrace_classify_insn (gdbarch, pc); |
| 1029 | insn.flags = 0; |
| 1030 | |
| 1031 | ftrace_update_insns (end, &insn); |
| 1032 | |
| 1033 | /* We're done once we pushed the instruction at the end. */ |
| 1034 | if (block->end == pc) |
| 1035 | break; |
| 1036 | |
| 1037 | /* We can't continue if we fail to compute the size. */ |
| 1038 | if (size <= 0) |
| 1039 | { |
| 1040 | /* Indicate the gap in the trace. We just added INSN so we're |
| 1041 | not at the beginning. */ |
| 1042 | end = ftrace_new_gap (end, BDE_BTS_INSN_SIZE); |
| 1043 | |
| 1044 | VEC_safe_push (bfun_s, *gaps, end); |
| 1045 | |
| 1046 | warning (_("Recorded trace may be incomplete at instruction %u " |
| 1047 | "(pc = %s)."), end->insn_offset - 1, |
| 1048 | core_addr_to_string_nz (pc)); |
| 1049 | |
| 1050 | break; |
| 1051 | } |
| 1052 | |
| 1053 | pc += size; |
| 1054 | |
| 1055 | /* Maintain the function level offset. |
| 1056 | For the last block, we do it here to not consider the last |
| 1057 | instruction. |
| 1058 | Since the last instruction corresponds to the current instruction |
| 1059 | and is not really part of the execution history, it shouldn't |
| 1060 | affect the level. */ |
| 1061 | if (blk == 0) |
| 1062 | level = std::min (level, end->level); |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | btinfo->begin = begin; |
| 1067 | btinfo->end = end; |
| 1068 | |
| 1069 | /* LEVEL is the minimal function level of all btrace function segments. |
| 1070 | Define the global level offset to -LEVEL so all function levels are |
| 1071 | normalized to start at zero. */ |
| 1072 | btinfo->level = -level; |
| 1073 | } |
| 1074 | |
| 1075 | #if defined (HAVE_LIBIPT) |
| 1076 | |
| 1077 | static enum btrace_insn_class |
| 1078 | pt_reclassify_insn (enum pt_insn_class iclass) |
| 1079 | { |
| 1080 | switch (iclass) |
| 1081 | { |
| 1082 | case ptic_call: |
| 1083 | return BTRACE_INSN_CALL; |
| 1084 | |
| 1085 | case ptic_return: |
| 1086 | return BTRACE_INSN_RETURN; |
| 1087 | |
| 1088 | case ptic_jump: |
| 1089 | return BTRACE_INSN_JUMP; |
| 1090 | |
| 1091 | default: |
| 1092 | return BTRACE_INSN_OTHER; |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | /* Return the btrace instruction flags for INSN. */ |
| 1097 | |
| 1098 | static btrace_insn_flags |
| 1099 | pt_btrace_insn_flags (const struct pt_insn *insn) |
| 1100 | { |
| 1101 | btrace_insn_flags flags = 0; |
| 1102 | |
| 1103 | if (insn->speculative) |
| 1104 | flags |= BTRACE_INSN_FLAG_SPECULATIVE; |
| 1105 | |
| 1106 | return flags; |
| 1107 | } |
| 1108 | |
| 1109 | /* Add function branch trace using DECODER. */ |
| 1110 | |
| 1111 | static void |
| 1112 | ftrace_add_pt (struct pt_insn_decoder *decoder, |
| 1113 | struct btrace_function **pbegin, |
| 1114 | struct btrace_function **pend, int *plevel, |
| 1115 | VEC (bfun_s) **gaps) |
| 1116 | { |
| 1117 | struct btrace_function *begin, *end, *upd; |
| 1118 | uint64_t offset; |
| 1119 | int errcode; |
| 1120 | |
| 1121 | begin = *pbegin; |
| 1122 | end = *pend; |
| 1123 | for (;;) |
| 1124 | { |
| 1125 | struct btrace_insn btinsn; |
| 1126 | struct pt_insn insn; |
| 1127 | |
| 1128 | errcode = pt_insn_sync_forward (decoder); |
| 1129 | if (errcode < 0) |
| 1130 | { |
| 1131 | if (errcode != -pte_eos) |
| 1132 | warning (_("Failed to synchronize onto the Intel Processor " |
| 1133 | "Trace stream: %s."), pt_errstr (pt_errcode (errcode))); |
| 1134 | break; |
| 1135 | } |
| 1136 | |
| 1137 | memset (&btinsn, 0, sizeof (btinsn)); |
| 1138 | for (;;) |
| 1139 | { |
| 1140 | errcode = pt_insn_next (decoder, &insn, sizeof(insn)); |
| 1141 | if (errcode < 0) |
| 1142 | break; |
| 1143 | |
| 1144 | /* Look for gaps in the trace - unless we're at the beginning. */ |
| 1145 | if (begin != NULL) |
| 1146 | { |
| 1147 | /* Tracing is disabled and re-enabled each time we enter the |
| 1148 | kernel. Most times, we continue from the same instruction we |
| 1149 | stopped before. This is indicated via the RESUMED instruction |
| 1150 | flag. The ENABLED instruction flag means that we continued |
| 1151 | from some other instruction. Indicate this as a trace gap. */ |
| 1152 | if (insn.enabled) |
| 1153 | { |
| 1154 | *pend = end = ftrace_new_gap (end, BDE_PT_DISABLED); |
| 1155 | |
| 1156 | VEC_safe_push (bfun_s, *gaps, end); |
| 1157 | |
| 1158 | pt_insn_get_offset (decoder, &offset); |
| 1159 | |
| 1160 | warning (_("Non-contiguous trace at instruction %u (offset " |
| 1161 | "= 0x%" PRIx64 ", pc = 0x%" PRIx64 ")."), |
| 1162 | end->insn_offset - 1, offset, insn.ip); |
| 1163 | } |
| 1164 | } |
| 1165 | |
| 1166 | /* Indicate trace overflows. */ |
| 1167 | if (insn.resynced) |
| 1168 | { |
| 1169 | *pend = end = ftrace_new_gap (end, BDE_PT_OVERFLOW); |
| 1170 | if (begin == NULL) |
| 1171 | *pbegin = begin = end; |
| 1172 | |
| 1173 | VEC_safe_push (bfun_s, *gaps, end); |
| 1174 | |
| 1175 | pt_insn_get_offset (decoder, &offset); |
| 1176 | |
| 1177 | warning (_("Overflow at instruction %u (offset = 0x%" PRIx64 |
| 1178 | ", pc = 0x%" PRIx64 ")."), end->insn_offset - 1, |
| 1179 | offset, insn.ip); |
| 1180 | } |
| 1181 | |
| 1182 | upd = ftrace_update_function (end, insn.ip); |
| 1183 | if (upd != end) |
| 1184 | { |
| 1185 | *pend = end = upd; |
| 1186 | |
| 1187 | if (begin == NULL) |
| 1188 | *pbegin = begin = upd; |
| 1189 | } |
| 1190 | |
| 1191 | /* Maintain the function level offset. */ |
| 1192 | *plevel = std::min (*plevel, end->level); |
| 1193 | |
| 1194 | btinsn.pc = (CORE_ADDR) insn.ip; |
| 1195 | btinsn.size = (gdb_byte) insn.size; |
| 1196 | btinsn.iclass = pt_reclassify_insn (insn.iclass); |
| 1197 | btinsn.flags = pt_btrace_insn_flags (&insn); |
| 1198 | |
| 1199 | ftrace_update_insns (end, &btinsn); |
| 1200 | } |
| 1201 | |
| 1202 | if (errcode == -pte_eos) |
| 1203 | break; |
| 1204 | |
| 1205 | /* Indicate the gap in the trace. */ |
| 1206 | *pend = end = ftrace_new_gap (end, errcode); |
| 1207 | if (begin == NULL) |
| 1208 | *pbegin = begin = end; |
| 1209 | |
| 1210 | VEC_safe_push (bfun_s, *gaps, end); |
| 1211 | |
| 1212 | pt_insn_get_offset (decoder, &offset); |
| 1213 | |
| 1214 | warning (_("Decode error (%d) at instruction %u (offset = 0x%" PRIx64 |
| 1215 | ", pc = 0x%" PRIx64 "): %s."), errcode, end->insn_offset - 1, |
| 1216 | offset, insn.ip, pt_errstr (pt_errcode (errcode))); |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | /* A callback function to allow the trace decoder to read the inferior's |
| 1221 | memory. */ |
| 1222 | |
| 1223 | static int |
| 1224 | btrace_pt_readmem_callback (gdb_byte *buffer, size_t size, |
| 1225 | const struct pt_asid *asid, uint64_t pc, |
| 1226 | void *context) |
| 1227 | { |
| 1228 | int result, errcode; |
| 1229 | |
| 1230 | result = (int) size; |
| 1231 | TRY |
| 1232 | { |
| 1233 | errcode = target_read_code ((CORE_ADDR) pc, buffer, size); |
| 1234 | if (errcode != 0) |
| 1235 | result = -pte_nomap; |
| 1236 | } |
| 1237 | CATCH (error, RETURN_MASK_ERROR) |
| 1238 | { |
| 1239 | result = -pte_nomap; |
| 1240 | } |
| 1241 | END_CATCH |
| 1242 | |
| 1243 | return result; |
| 1244 | } |
| 1245 | |
| 1246 | /* Translate the vendor from one enum to another. */ |
| 1247 | |
| 1248 | static enum pt_cpu_vendor |
| 1249 | pt_translate_cpu_vendor (enum btrace_cpu_vendor vendor) |
| 1250 | { |
| 1251 | switch (vendor) |
| 1252 | { |
| 1253 | default: |
| 1254 | return pcv_unknown; |
| 1255 | |
| 1256 | case CV_INTEL: |
| 1257 | return pcv_intel; |
| 1258 | } |
| 1259 | } |
| 1260 | |
| 1261 | /* Finalize the function branch trace after decode. */ |
| 1262 | |
| 1263 | static void btrace_finalize_ftrace_pt (struct pt_insn_decoder *decoder, |
| 1264 | struct thread_info *tp, int level) |
| 1265 | { |
| 1266 | pt_insn_free_decoder (decoder); |
| 1267 | |
| 1268 | /* LEVEL is the minimal function level of all btrace function segments. |
| 1269 | Define the global level offset to -LEVEL so all function levels are |
| 1270 | normalized to start at zero. */ |
| 1271 | tp->btrace.level = -level; |
| 1272 | |
| 1273 | /* Add a single last instruction entry for the current PC. |
| 1274 | This allows us to compute the backtrace at the current PC using both |
| 1275 | standard unwind and btrace unwind. |
| 1276 | This extra entry is ignored by all record commands. */ |
| 1277 | btrace_add_pc (tp); |
| 1278 | } |
| 1279 | |
| 1280 | /* Compute the function branch trace from Intel Processor Trace |
| 1281 | format. */ |
| 1282 | |
| 1283 | static void |
| 1284 | btrace_compute_ftrace_pt (struct thread_info *tp, |
| 1285 | const struct btrace_data_pt *btrace, |
| 1286 | VEC (bfun_s) **gaps) |
| 1287 | { |
| 1288 | struct btrace_thread_info *btinfo; |
| 1289 | struct pt_insn_decoder *decoder; |
| 1290 | struct pt_config config; |
| 1291 | int level, errcode; |
| 1292 | |
| 1293 | if (btrace->size == 0) |
| 1294 | return; |
| 1295 | |
| 1296 | btinfo = &tp->btrace; |
| 1297 | level = btinfo->begin != NULL ? -btinfo->level : INT_MAX; |
| 1298 | |
| 1299 | pt_config_init(&config); |
| 1300 | config.begin = btrace->data; |
| 1301 | config.end = btrace->data + btrace->size; |
| 1302 | |
| 1303 | config.cpu.vendor = pt_translate_cpu_vendor (btrace->config.cpu.vendor); |
| 1304 | config.cpu.family = btrace->config.cpu.family; |
| 1305 | config.cpu.model = btrace->config.cpu.model; |
| 1306 | config.cpu.stepping = btrace->config.cpu.stepping; |
| 1307 | |
| 1308 | errcode = pt_cpu_errata (&config.errata, &config.cpu); |
| 1309 | if (errcode < 0) |
| 1310 | error (_("Failed to configure the Intel Processor Trace decoder: %s."), |
| 1311 | pt_errstr (pt_errcode (errcode))); |
| 1312 | |
| 1313 | decoder = pt_insn_alloc_decoder (&config); |
| 1314 | if (decoder == NULL) |
| 1315 | error (_("Failed to allocate the Intel Processor Trace decoder.")); |
| 1316 | |
| 1317 | TRY |
| 1318 | { |
| 1319 | struct pt_image *image; |
| 1320 | |
| 1321 | image = pt_insn_get_image(decoder); |
| 1322 | if (image == NULL) |
| 1323 | error (_("Failed to configure the Intel Processor Trace decoder.")); |
| 1324 | |
| 1325 | errcode = pt_image_set_callback(image, btrace_pt_readmem_callback, NULL); |
| 1326 | if (errcode < 0) |
| 1327 | error (_("Failed to configure the Intel Processor Trace decoder: " |
| 1328 | "%s."), pt_errstr (pt_errcode (errcode))); |
| 1329 | |
| 1330 | ftrace_add_pt (decoder, &btinfo->begin, &btinfo->end, &level, gaps); |
| 1331 | } |
| 1332 | CATCH (error, RETURN_MASK_ALL) |
| 1333 | { |
| 1334 | /* Indicate a gap in the trace if we quit trace processing. */ |
| 1335 | if (error.reason == RETURN_QUIT && btinfo->end != NULL) |
| 1336 | { |
| 1337 | btinfo->end = ftrace_new_gap (btinfo->end, BDE_PT_USER_QUIT); |
| 1338 | |
| 1339 | VEC_safe_push (bfun_s, *gaps, btinfo->end); |
| 1340 | } |
| 1341 | |
| 1342 | btrace_finalize_ftrace_pt (decoder, tp, level); |
| 1343 | |
| 1344 | throw_exception (error); |
| 1345 | } |
| 1346 | END_CATCH |
| 1347 | |
| 1348 | btrace_finalize_ftrace_pt (decoder, tp, level); |
| 1349 | } |
| 1350 | |
| 1351 | #else /* defined (HAVE_LIBIPT) */ |
| 1352 | |
| 1353 | static void |
| 1354 | btrace_compute_ftrace_pt (struct thread_info *tp, |
| 1355 | const struct btrace_data_pt *btrace, |
| 1356 | VEC (bfun_s) **gaps) |
| 1357 | { |
| 1358 | internal_error (__FILE__, __LINE__, _("Unexpected branch trace format.")); |
| 1359 | } |
| 1360 | |
| 1361 | #endif /* defined (HAVE_LIBIPT) */ |
| 1362 | |
| 1363 | /* Compute the function branch trace from a block branch trace BTRACE for |
| 1364 | a thread given by BTINFO. */ |
| 1365 | |
| 1366 | static void |
| 1367 | btrace_compute_ftrace_1 (struct thread_info *tp, struct btrace_data *btrace, |
| 1368 | VEC (bfun_s) **gaps) |
| 1369 | { |
| 1370 | DEBUG ("compute ftrace"); |
| 1371 | |
| 1372 | switch (btrace->format) |
| 1373 | { |
| 1374 | case BTRACE_FORMAT_NONE: |
| 1375 | return; |
| 1376 | |
| 1377 | case BTRACE_FORMAT_BTS: |
| 1378 | btrace_compute_ftrace_bts (tp, &btrace->variant.bts, gaps); |
| 1379 | return; |
| 1380 | |
| 1381 | case BTRACE_FORMAT_PT: |
| 1382 | btrace_compute_ftrace_pt (tp, &btrace->variant.pt, gaps); |
| 1383 | return; |
| 1384 | } |
| 1385 | |
| 1386 | internal_error (__FILE__, __LINE__, _("Unkown branch trace format.")); |
| 1387 | } |
| 1388 | |
| 1389 | static void |
| 1390 | btrace_finalize_ftrace (struct thread_info *tp, VEC (bfun_s) **gaps) |
| 1391 | { |
| 1392 | if (!VEC_empty (bfun_s, *gaps)) |
| 1393 | { |
| 1394 | tp->btrace.ngaps += VEC_length (bfun_s, *gaps); |
| 1395 | btrace_bridge_gaps (tp, gaps); |
| 1396 | } |
| 1397 | } |
| 1398 | |
| 1399 | static void |
| 1400 | btrace_compute_ftrace (struct thread_info *tp, struct btrace_data *btrace) |
| 1401 | { |
| 1402 | VEC (bfun_s) *gaps; |
| 1403 | struct cleanup *old_chain; |
| 1404 | |
| 1405 | gaps = NULL; |
| 1406 | old_chain = make_cleanup (VEC_cleanup (bfun_s), &gaps); |
| 1407 | |
| 1408 | TRY |
| 1409 | { |
| 1410 | btrace_compute_ftrace_1 (tp, btrace, &gaps); |
| 1411 | } |
| 1412 | CATCH (error, RETURN_MASK_ALL) |
| 1413 | { |
| 1414 | btrace_finalize_ftrace (tp, &gaps); |
| 1415 | |
| 1416 | throw_exception (error); |
| 1417 | } |
| 1418 | END_CATCH |
| 1419 | |
| 1420 | btrace_finalize_ftrace (tp, &gaps); |
| 1421 | |
| 1422 | do_cleanups (old_chain); |
| 1423 | } |
| 1424 | |
| 1425 | /* Add an entry for the current PC. */ |
| 1426 | |
| 1427 | static void |
| 1428 | btrace_add_pc (struct thread_info *tp) |
| 1429 | { |
| 1430 | struct btrace_data btrace; |
| 1431 | struct btrace_block *block; |
| 1432 | struct regcache *regcache; |
| 1433 | struct cleanup *cleanup; |
| 1434 | CORE_ADDR pc; |
| 1435 | |
| 1436 | regcache = get_thread_regcache (tp->ptid); |
| 1437 | pc = regcache_read_pc (regcache); |
| 1438 | |
| 1439 | btrace_data_init (&btrace); |
| 1440 | btrace.format = BTRACE_FORMAT_BTS; |
| 1441 | btrace.variant.bts.blocks = NULL; |
| 1442 | |
| 1443 | cleanup = make_cleanup_btrace_data (&btrace); |
| 1444 | |
| 1445 | block = VEC_safe_push (btrace_block_s, btrace.variant.bts.blocks, NULL); |
| 1446 | block->begin = pc; |
| 1447 | block->end = pc; |
| 1448 | |
| 1449 | btrace_compute_ftrace (tp, &btrace); |
| 1450 | |
| 1451 | do_cleanups (cleanup); |
| 1452 | } |
| 1453 | |
| 1454 | /* See btrace.h. */ |
| 1455 | |
| 1456 | void |
| 1457 | btrace_enable (struct thread_info *tp, const struct btrace_config *conf) |
| 1458 | { |
| 1459 | if (tp->btrace.target != NULL) |
| 1460 | return; |
| 1461 | |
| 1462 | #if !defined (HAVE_LIBIPT) |
| 1463 | if (conf->format == BTRACE_FORMAT_PT) |
| 1464 | error (_("GDB does not support Intel Processor Trace.")); |
| 1465 | #endif /* !defined (HAVE_LIBIPT) */ |
| 1466 | |
| 1467 | if (!target_supports_btrace (conf->format)) |
| 1468 | error (_("Target does not support branch tracing.")); |
| 1469 | |
| 1470 | DEBUG ("enable thread %s (%s)", print_thread_id (tp), |
| 1471 | target_pid_to_str (tp->ptid)); |
| 1472 | |
| 1473 | tp->btrace.target = target_enable_btrace (tp->ptid, conf); |
| 1474 | |
| 1475 | /* Add an entry for the current PC so we start tracing from where we |
| 1476 | enabled it. */ |
| 1477 | if (tp->btrace.target != NULL) |
| 1478 | btrace_add_pc (tp); |
| 1479 | } |
| 1480 | |
| 1481 | /* See btrace.h. */ |
| 1482 | |
| 1483 | const struct btrace_config * |
| 1484 | btrace_conf (const struct btrace_thread_info *btinfo) |
| 1485 | { |
| 1486 | if (btinfo->target == NULL) |
| 1487 | return NULL; |
| 1488 | |
| 1489 | return target_btrace_conf (btinfo->target); |
| 1490 | } |
| 1491 | |
| 1492 | /* See btrace.h. */ |
| 1493 | |
| 1494 | void |
| 1495 | btrace_disable (struct thread_info *tp) |
| 1496 | { |
| 1497 | struct btrace_thread_info *btp = &tp->btrace; |
| 1498 | int errcode = 0; |
| 1499 | |
| 1500 | if (btp->target == NULL) |
| 1501 | return; |
| 1502 | |
| 1503 | DEBUG ("disable thread %s (%s)", print_thread_id (tp), |
| 1504 | target_pid_to_str (tp->ptid)); |
| 1505 | |
| 1506 | target_disable_btrace (btp->target); |
| 1507 | btp->target = NULL; |
| 1508 | |
| 1509 | btrace_clear (tp); |
| 1510 | } |
| 1511 | |
| 1512 | /* See btrace.h. */ |
| 1513 | |
| 1514 | void |
| 1515 | btrace_teardown (struct thread_info *tp) |
| 1516 | { |
| 1517 | struct btrace_thread_info *btp = &tp->btrace; |
| 1518 | int errcode = 0; |
| 1519 | |
| 1520 | if (btp->target == NULL) |
| 1521 | return; |
| 1522 | |
| 1523 | DEBUG ("teardown thread %s (%s)", print_thread_id (tp), |
| 1524 | target_pid_to_str (tp->ptid)); |
| 1525 | |
| 1526 | target_teardown_btrace (btp->target); |
| 1527 | btp->target = NULL; |
| 1528 | |
| 1529 | btrace_clear (tp); |
| 1530 | } |
| 1531 | |
| 1532 | /* Stitch branch trace in BTS format. */ |
| 1533 | |
| 1534 | static int |
| 1535 | btrace_stitch_bts (struct btrace_data_bts *btrace, struct thread_info *tp) |
| 1536 | { |
| 1537 | struct btrace_thread_info *btinfo; |
| 1538 | struct btrace_function *last_bfun; |
| 1539 | struct btrace_insn *last_insn; |
| 1540 | btrace_block_s *first_new_block; |
| 1541 | |
| 1542 | btinfo = &tp->btrace; |
| 1543 | last_bfun = btinfo->end; |
| 1544 | gdb_assert (last_bfun != NULL); |
| 1545 | gdb_assert (!VEC_empty (btrace_block_s, btrace->blocks)); |
| 1546 | |
| 1547 | /* If the existing trace ends with a gap, we just glue the traces |
| 1548 | together. We need to drop the last (i.e. chronologically first) block |
| 1549 | of the new trace, though, since we can't fill in the start address.*/ |
| 1550 | if (VEC_empty (btrace_insn_s, last_bfun->insn)) |
| 1551 | { |
| 1552 | VEC_pop (btrace_block_s, btrace->blocks); |
| 1553 | return 0; |
| 1554 | } |
| 1555 | |
| 1556 | /* Beware that block trace starts with the most recent block, so the |
| 1557 | chronologically first block in the new trace is the last block in |
| 1558 | the new trace's block vector. */ |
| 1559 | first_new_block = VEC_last (btrace_block_s, btrace->blocks); |
| 1560 | last_insn = VEC_last (btrace_insn_s, last_bfun->insn); |
| 1561 | |
| 1562 | /* If the current PC at the end of the block is the same as in our current |
| 1563 | trace, there are two explanations: |
| 1564 | 1. we executed the instruction and some branch brought us back. |
| 1565 | 2. we have not made any progress. |
| 1566 | In the first case, the delta trace vector should contain at least two |
| 1567 | entries. |
| 1568 | In the second case, the delta trace vector should contain exactly one |
| 1569 | entry for the partial block containing the current PC. Remove it. */ |
| 1570 | if (first_new_block->end == last_insn->pc |
| 1571 | && VEC_length (btrace_block_s, btrace->blocks) == 1) |
| 1572 | { |
| 1573 | VEC_pop (btrace_block_s, btrace->blocks); |
| 1574 | return 0; |
| 1575 | } |
| 1576 | |
| 1577 | DEBUG ("stitching %s to %s", ftrace_print_insn_addr (last_insn), |
| 1578 | core_addr_to_string_nz (first_new_block->end)); |
| 1579 | |
| 1580 | /* Do a simple sanity check to make sure we don't accidentally end up |
| 1581 | with a bad block. This should not occur in practice. */ |
| 1582 | if (first_new_block->end < last_insn->pc) |
| 1583 | { |
| 1584 | warning (_("Error while trying to read delta trace. Falling back to " |
| 1585 | "a full read.")); |
| 1586 | return -1; |
| 1587 | } |
| 1588 | |
| 1589 | /* We adjust the last block to start at the end of our current trace. */ |
| 1590 | gdb_assert (first_new_block->begin == 0); |
| 1591 | first_new_block->begin = last_insn->pc; |
| 1592 | |
| 1593 | /* We simply pop the last insn so we can insert it again as part of |
| 1594 | the normal branch trace computation. |
| 1595 | Since instruction iterators are based on indices in the instructions |
| 1596 | vector, we don't leave any pointers dangling. */ |
| 1597 | DEBUG ("pruning insn at %s for stitching", |
| 1598 | ftrace_print_insn_addr (last_insn)); |
| 1599 | |
| 1600 | VEC_pop (btrace_insn_s, last_bfun->insn); |
| 1601 | |
| 1602 | /* The instructions vector may become empty temporarily if this has |
| 1603 | been the only instruction in this function segment. |
| 1604 | This violates the invariant but will be remedied shortly by |
| 1605 | btrace_compute_ftrace when we add the new trace. */ |
| 1606 | |
| 1607 | /* The only case where this would hurt is if the entire trace consisted |
| 1608 | of just that one instruction. If we remove it, we might turn the now |
| 1609 | empty btrace function segment into a gap. But we don't want gaps at |
| 1610 | the beginning. To avoid this, we remove the entire old trace. */ |
| 1611 | if (last_bfun == btinfo->begin && VEC_empty (btrace_insn_s, last_bfun->insn)) |
| 1612 | btrace_clear (tp); |
| 1613 | |
| 1614 | return 0; |
| 1615 | } |
| 1616 | |
| 1617 | /* Adjust the block trace in order to stitch old and new trace together. |
| 1618 | BTRACE is the new delta trace between the last and the current stop. |
| 1619 | TP is the traced thread. |
| 1620 | May modifx BTRACE as well as the existing trace in TP. |
| 1621 | Return 0 on success, -1 otherwise. */ |
| 1622 | |
| 1623 | static int |
| 1624 | btrace_stitch_trace (struct btrace_data *btrace, struct thread_info *tp) |
| 1625 | { |
| 1626 | /* If we don't have trace, there's nothing to do. */ |
| 1627 | if (btrace_data_empty (btrace)) |
| 1628 | return 0; |
| 1629 | |
| 1630 | switch (btrace->format) |
| 1631 | { |
| 1632 | case BTRACE_FORMAT_NONE: |
| 1633 | return 0; |
| 1634 | |
| 1635 | case BTRACE_FORMAT_BTS: |
| 1636 | return btrace_stitch_bts (&btrace->variant.bts, tp); |
| 1637 | |
| 1638 | case BTRACE_FORMAT_PT: |
| 1639 | /* Delta reads are not supported. */ |
| 1640 | return -1; |
| 1641 | } |
| 1642 | |
| 1643 | internal_error (__FILE__, __LINE__, _("Unkown branch trace format.")); |
| 1644 | } |
| 1645 | |
| 1646 | /* Clear the branch trace histories in BTINFO. */ |
| 1647 | |
| 1648 | static void |
| 1649 | btrace_clear_history (struct btrace_thread_info *btinfo) |
| 1650 | { |
| 1651 | xfree (btinfo->insn_history); |
| 1652 | xfree (btinfo->call_history); |
| 1653 | xfree (btinfo->replay); |
| 1654 | |
| 1655 | btinfo->insn_history = NULL; |
| 1656 | btinfo->call_history = NULL; |
| 1657 | btinfo->replay = NULL; |
| 1658 | } |
| 1659 | |
| 1660 | /* Clear the branch trace maintenance histories in BTINFO. */ |
| 1661 | |
| 1662 | static void |
| 1663 | btrace_maint_clear (struct btrace_thread_info *btinfo) |
| 1664 | { |
| 1665 | switch (btinfo->data.format) |
| 1666 | { |
| 1667 | default: |
| 1668 | break; |
| 1669 | |
| 1670 | case BTRACE_FORMAT_BTS: |
| 1671 | btinfo->maint.variant.bts.packet_history.begin = 0; |
| 1672 | btinfo->maint.variant.bts.packet_history.end = 0; |
| 1673 | break; |
| 1674 | |
| 1675 | #if defined (HAVE_LIBIPT) |
| 1676 | case BTRACE_FORMAT_PT: |
| 1677 | xfree (btinfo->maint.variant.pt.packets); |
| 1678 | |
| 1679 | btinfo->maint.variant.pt.packets = NULL; |
| 1680 | btinfo->maint.variant.pt.packet_history.begin = 0; |
| 1681 | btinfo->maint.variant.pt.packet_history.end = 0; |
| 1682 | break; |
| 1683 | #endif /* defined (HAVE_LIBIPT) */ |
| 1684 | } |
| 1685 | } |
| 1686 | |
| 1687 | /* See btrace.h. */ |
| 1688 | |
| 1689 | void |
| 1690 | btrace_fetch (struct thread_info *tp) |
| 1691 | { |
| 1692 | struct btrace_thread_info *btinfo; |
| 1693 | struct btrace_target_info *tinfo; |
| 1694 | struct btrace_data btrace; |
| 1695 | struct cleanup *cleanup; |
| 1696 | int errcode; |
| 1697 | |
| 1698 | DEBUG ("fetch thread %s (%s)", print_thread_id (tp), |
| 1699 | target_pid_to_str (tp->ptid)); |
| 1700 | |
| 1701 | btinfo = &tp->btrace; |
| 1702 | tinfo = btinfo->target; |
| 1703 | if (tinfo == NULL) |
| 1704 | return; |
| 1705 | |
| 1706 | /* There's no way we could get new trace while replaying. |
| 1707 | On the other hand, delta trace would return a partial record with the |
| 1708 | current PC, which is the replay PC, not the last PC, as expected. */ |
| 1709 | if (btinfo->replay != NULL) |
| 1710 | return; |
| 1711 | |
| 1712 | btrace_data_init (&btrace); |
| 1713 | cleanup = make_cleanup_btrace_data (&btrace); |
| 1714 | |
| 1715 | /* Let's first try to extend the trace we already have. */ |
| 1716 | if (btinfo->end != NULL) |
| 1717 | { |
| 1718 | errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_DELTA); |
| 1719 | if (errcode == 0) |
| 1720 | { |
| 1721 | /* Success. Let's try to stitch the traces together. */ |
| 1722 | errcode = btrace_stitch_trace (&btrace, tp); |
| 1723 | } |
| 1724 | else |
| 1725 | { |
| 1726 | /* We failed to read delta trace. Let's try to read new trace. */ |
| 1727 | errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_NEW); |
| 1728 | |
| 1729 | /* If we got any new trace, discard what we have. */ |
| 1730 | if (errcode == 0 && !btrace_data_empty (&btrace)) |
| 1731 | btrace_clear (tp); |
| 1732 | } |
| 1733 | |
| 1734 | /* If we were not able to read the trace, we start over. */ |
| 1735 | if (errcode != 0) |
| 1736 | { |
| 1737 | btrace_clear (tp); |
| 1738 | errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_ALL); |
| 1739 | } |
| 1740 | } |
| 1741 | else |
| 1742 | errcode = target_read_btrace (&btrace, tinfo, BTRACE_READ_ALL); |
| 1743 | |
| 1744 | /* If we were not able to read the branch trace, signal an error. */ |
| 1745 | if (errcode != 0) |
| 1746 | error (_("Failed to read branch trace.")); |
| 1747 | |
| 1748 | /* Compute the trace, provided we have any. */ |
| 1749 | if (!btrace_data_empty (&btrace)) |
| 1750 | { |
| 1751 | /* Store the raw trace data. The stored data will be cleared in |
| 1752 | btrace_clear, so we always append the new trace. */ |
| 1753 | btrace_data_append (&btinfo->data, &btrace); |
| 1754 | btrace_maint_clear (btinfo); |
| 1755 | |
| 1756 | btrace_clear_history (btinfo); |
| 1757 | btrace_compute_ftrace (tp, &btrace); |
| 1758 | } |
| 1759 | |
| 1760 | do_cleanups (cleanup); |
| 1761 | } |
| 1762 | |
| 1763 | /* See btrace.h. */ |
| 1764 | |
| 1765 | void |
| 1766 | btrace_clear (struct thread_info *tp) |
| 1767 | { |
| 1768 | struct btrace_thread_info *btinfo; |
| 1769 | struct btrace_function *it, *trash; |
| 1770 | |
| 1771 | DEBUG ("clear thread %s (%s)", print_thread_id (tp), |
| 1772 | target_pid_to_str (tp->ptid)); |
| 1773 | |
| 1774 | /* Make sure btrace frames that may hold a pointer into the branch |
| 1775 | trace data are destroyed. */ |
| 1776 | reinit_frame_cache (); |
| 1777 | |
| 1778 | btinfo = &tp->btrace; |
| 1779 | |
| 1780 | it = btinfo->begin; |
| 1781 | while (it != NULL) |
| 1782 | { |
| 1783 | trash = it; |
| 1784 | it = it->flow.next; |
| 1785 | |
| 1786 | xfree (trash); |
| 1787 | } |
| 1788 | |
| 1789 | btinfo->begin = NULL; |
| 1790 | btinfo->end = NULL; |
| 1791 | btinfo->ngaps = 0; |
| 1792 | |
| 1793 | /* Must clear the maint data before - it depends on BTINFO->DATA. */ |
| 1794 | btrace_maint_clear (btinfo); |
| 1795 | btrace_data_clear (&btinfo->data); |
| 1796 | btrace_clear_history (btinfo); |
| 1797 | } |
| 1798 | |
| 1799 | /* See btrace.h. */ |
| 1800 | |
| 1801 | void |
| 1802 | btrace_free_objfile (struct objfile *objfile) |
| 1803 | { |
| 1804 | struct thread_info *tp; |
| 1805 | |
| 1806 | DEBUG ("free objfile"); |
| 1807 | |
| 1808 | ALL_NON_EXITED_THREADS (tp) |
| 1809 | btrace_clear (tp); |
| 1810 | } |
| 1811 | |
| 1812 | #if defined (HAVE_LIBEXPAT) |
| 1813 | |
| 1814 | /* Check the btrace document version. */ |
| 1815 | |
| 1816 | static void |
| 1817 | check_xml_btrace_version (struct gdb_xml_parser *parser, |
| 1818 | const struct gdb_xml_element *element, |
| 1819 | void *user_data, VEC (gdb_xml_value_s) *attributes) |
| 1820 | { |
| 1821 | const char *version |
| 1822 | = (const char *) xml_find_attribute (attributes, "version")->value; |
| 1823 | |
| 1824 | if (strcmp (version, "1.0") != 0) |
| 1825 | gdb_xml_error (parser, _("Unsupported btrace version: \"%s\""), version); |
| 1826 | } |
| 1827 | |
| 1828 | /* Parse a btrace "block" xml record. */ |
| 1829 | |
| 1830 | static void |
| 1831 | parse_xml_btrace_block (struct gdb_xml_parser *parser, |
| 1832 | const struct gdb_xml_element *element, |
| 1833 | void *user_data, VEC (gdb_xml_value_s) *attributes) |
| 1834 | { |
| 1835 | struct btrace_data *btrace; |
| 1836 | struct btrace_block *block; |
| 1837 | ULONGEST *begin, *end; |
| 1838 | |
| 1839 | btrace = (struct btrace_data *) user_data; |
| 1840 | |
| 1841 | switch (btrace->format) |
| 1842 | { |
| 1843 | case BTRACE_FORMAT_BTS: |
| 1844 | break; |
| 1845 | |
| 1846 | case BTRACE_FORMAT_NONE: |
| 1847 | btrace->format = BTRACE_FORMAT_BTS; |
| 1848 | btrace->variant.bts.blocks = NULL; |
| 1849 | break; |
| 1850 | |
| 1851 | default: |
| 1852 | gdb_xml_error (parser, _("Btrace format error.")); |
| 1853 | } |
| 1854 | |
| 1855 | begin = (ULONGEST *) xml_find_attribute (attributes, "begin")->value; |
| 1856 | end = (ULONGEST *) xml_find_attribute (attributes, "end")->value; |
| 1857 | |
| 1858 | block = VEC_safe_push (btrace_block_s, btrace->variant.bts.blocks, NULL); |
| 1859 | block->begin = *begin; |
| 1860 | block->end = *end; |
| 1861 | } |
| 1862 | |
| 1863 | /* Parse a "raw" xml record. */ |
| 1864 | |
| 1865 | static void |
| 1866 | parse_xml_raw (struct gdb_xml_parser *parser, const char *body_text, |
| 1867 | gdb_byte **pdata, size_t *psize) |
| 1868 | { |
| 1869 | struct cleanup *cleanup; |
| 1870 | gdb_byte *data, *bin; |
| 1871 | size_t len, size; |
| 1872 | |
| 1873 | len = strlen (body_text); |
| 1874 | if (len % 2 != 0) |
| 1875 | gdb_xml_error (parser, _("Bad raw data size.")); |
| 1876 | |
| 1877 | size = len / 2; |
| 1878 | |
| 1879 | bin = data = (gdb_byte *) xmalloc (size); |
| 1880 | cleanup = make_cleanup (xfree, data); |
| 1881 | |
| 1882 | /* We use hex encoding - see common/rsp-low.h. */ |
| 1883 | while (len > 0) |
| 1884 | { |
| 1885 | char hi, lo; |
| 1886 | |
| 1887 | hi = *body_text++; |
| 1888 | lo = *body_text++; |
| 1889 | |
| 1890 | if (hi == 0 || lo == 0) |
| 1891 | gdb_xml_error (parser, _("Bad hex encoding.")); |
| 1892 | |
| 1893 | *bin++ = fromhex (hi) * 16 + fromhex (lo); |
| 1894 | len -= 2; |
| 1895 | } |
| 1896 | |
| 1897 | discard_cleanups (cleanup); |
| 1898 | |
| 1899 | *pdata = data; |
| 1900 | *psize = size; |
| 1901 | } |
| 1902 | |
| 1903 | /* Parse a btrace pt-config "cpu" xml record. */ |
| 1904 | |
| 1905 | static void |
| 1906 | parse_xml_btrace_pt_config_cpu (struct gdb_xml_parser *parser, |
| 1907 | const struct gdb_xml_element *element, |
| 1908 | void *user_data, |
| 1909 | VEC (gdb_xml_value_s) *attributes) |
| 1910 | { |
| 1911 | struct btrace_data *btrace; |
| 1912 | const char *vendor; |
| 1913 | ULONGEST *family, *model, *stepping; |
| 1914 | |
| 1915 | vendor = (const char *) xml_find_attribute (attributes, "vendor")->value; |
| 1916 | family = (ULONGEST *) xml_find_attribute (attributes, "family")->value; |
| 1917 | model = (ULONGEST *) xml_find_attribute (attributes, "model")->value; |
| 1918 | stepping = (ULONGEST *) xml_find_attribute (attributes, "stepping")->value; |
| 1919 | |
| 1920 | btrace = (struct btrace_data *) user_data; |
| 1921 | |
| 1922 | if (strcmp (vendor, "GenuineIntel") == 0) |
| 1923 | btrace->variant.pt.config.cpu.vendor = CV_INTEL; |
| 1924 | |
| 1925 | btrace->variant.pt.config.cpu.family = *family; |
| 1926 | btrace->variant.pt.config.cpu.model = *model; |
| 1927 | btrace->variant.pt.config.cpu.stepping = *stepping; |
| 1928 | } |
| 1929 | |
| 1930 | /* Parse a btrace pt "raw" xml record. */ |
| 1931 | |
| 1932 | static void |
| 1933 | parse_xml_btrace_pt_raw (struct gdb_xml_parser *parser, |
| 1934 | const struct gdb_xml_element *element, |
| 1935 | void *user_data, const char *body_text) |
| 1936 | { |
| 1937 | struct btrace_data *btrace; |
| 1938 | |
| 1939 | btrace = (struct btrace_data *) user_data; |
| 1940 | parse_xml_raw (parser, body_text, &btrace->variant.pt.data, |
| 1941 | &btrace->variant.pt.size); |
| 1942 | } |
| 1943 | |
| 1944 | /* Parse a btrace "pt" xml record. */ |
| 1945 | |
| 1946 | static void |
| 1947 | parse_xml_btrace_pt (struct gdb_xml_parser *parser, |
| 1948 | const struct gdb_xml_element *element, |
| 1949 | void *user_data, VEC (gdb_xml_value_s) *attributes) |
| 1950 | { |
| 1951 | struct btrace_data *btrace; |
| 1952 | |
| 1953 | btrace = (struct btrace_data *) user_data; |
| 1954 | btrace->format = BTRACE_FORMAT_PT; |
| 1955 | btrace->variant.pt.config.cpu.vendor = CV_UNKNOWN; |
| 1956 | btrace->variant.pt.data = NULL; |
| 1957 | btrace->variant.pt.size = 0; |
| 1958 | } |
| 1959 | |
| 1960 | static const struct gdb_xml_attribute block_attributes[] = { |
| 1961 | { "begin", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL }, |
| 1962 | { "end", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL }, |
| 1963 | { NULL, GDB_XML_AF_NONE, NULL, NULL } |
| 1964 | }; |
| 1965 | |
| 1966 | static const struct gdb_xml_attribute btrace_pt_config_cpu_attributes[] = { |
| 1967 | { "vendor", GDB_XML_AF_NONE, NULL, NULL }, |
| 1968 | { "family", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL }, |
| 1969 | { "model", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL }, |
| 1970 | { "stepping", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL }, |
| 1971 | { NULL, GDB_XML_AF_NONE, NULL, NULL } |
| 1972 | }; |
| 1973 | |
| 1974 | static const struct gdb_xml_element btrace_pt_config_children[] = { |
| 1975 | { "cpu", btrace_pt_config_cpu_attributes, NULL, GDB_XML_EF_OPTIONAL, |
| 1976 | parse_xml_btrace_pt_config_cpu, NULL }, |
| 1977 | { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } |
| 1978 | }; |
| 1979 | |
| 1980 | static const struct gdb_xml_element btrace_pt_children[] = { |
| 1981 | { "pt-config", NULL, btrace_pt_config_children, GDB_XML_EF_OPTIONAL, NULL, |
| 1982 | NULL }, |
| 1983 | { "raw", NULL, NULL, GDB_XML_EF_OPTIONAL, NULL, parse_xml_btrace_pt_raw }, |
| 1984 | { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } |
| 1985 | }; |
| 1986 | |
| 1987 | static const struct gdb_xml_attribute btrace_attributes[] = { |
| 1988 | { "version", GDB_XML_AF_NONE, NULL, NULL }, |
| 1989 | { NULL, GDB_XML_AF_NONE, NULL, NULL } |
| 1990 | }; |
| 1991 | |
| 1992 | static const struct gdb_xml_element btrace_children[] = { |
| 1993 | { "block", block_attributes, NULL, |
| 1994 | GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL, parse_xml_btrace_block, NULL }, |
| 1995 | { "pt", NULL, btrace_pt_children, GDB_XML_EF_OPTIONAL, parse_xml_btrace_pt, |
| 1996 | NULL }, |
| 1997 | { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } |
| 1998 | }; |
| 1999 | |
| 2000 | static const struct gdb_xml_element btrace_elements[] = { |
| 2001 | { "btrace", btrace_attributes, btrace_children, GDB_XML_EF_NONE, |
| 2002 | check_xml_btrace_version, NULL }, |
| 2003 | { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } |
| 2004 | }; |
| 2005 | |
| 2006 | #endif /* defined (HAVE_LIBEXPAT) */ |
| 2007 | |
| 2008 | /* See btrace.h. */ |
| 2009 | |
| 2010 | void |
| 2011 | parse_xml_btrace (struct btrace_data *btrace, const char *buffer) |
| 2012 | { |
| 2013 | struct cleanup *cleanup; |
| 2014 | int errcode; |
| 2015 | |
| 2016 | #if defined (HAVE_LIBEXPAT) |
| 2017 | |
| 2018 | btrace->format = BTRACE_FORMAT_NONE; |
| 2019 | |
| 2020 | cleanup = make_cleanup_btrace_data (btrace); |
| 2021 | errcode = gdb_xml_parse_quick (_("btrace"), "btrace.dtd", btrace_elements, |
| 2022 | buffer, btrace); |
| 2023 | if (errcode != 0) |
| 2024 | error (_("Error parsing branch trace.")); |
| 2025 | |
| 2026 | /* Keep parse results. */ |
| 2027 | discard_cleanups (cleanup); |
| 2028 | |
| 2029 | #else /* !defined (HAVE_LIBEXPAT) */ |
| 2030 | |
| 2031 | error (_("Cannot process branch trace. XML parsing is not supported.")); |
| 2032 | |
| 2033 | #endif /* !defined (HAVE_LIBEXPAT) */ |
| 2034 | } |
| 2035 | |
| 2036 | #if defined (HAVE_LIBEXPAT) |
| 2037 | |
| 2038 | /* Parse a btrace-conf "bts" xml record. */ |
| 2039 | |
| 2040 | static void |
| 2041 | parse_xml_btrace_conf_bts (struct gdb_xml_parser *parser, |
| 2042 | const struct gdb_xml_element *element, |
| 2043 | void *user_data, VEC (gdb_xml_value_s) *attributes) |
| 2044 | { |
| 2045 | struct btrace_config *conf; |
| 2046 | struct gdb_xml_value *size; |
| 2047 | |
| 2048 | conf = (struct btrace_config *) user_data; |
| 2049 | conf->format = BTRACE_FORMAT_BTS; |
| 2050 | conf->bts.size = 0; |
| 2051 | |
| 2052 | size = xml_find_attribute (attributes, "size"); |
| 2053 | if (size != NULL) |
| 2054 | conf->bts.size = (unsigned int) *(ULONGEST *) size->value; |
| 2055 | } |
| 2056 | |
| 2057 | /* Parse a btrace-conf "pt" xml record. */ |
| 2058 | |
| 2059 | static void |
| 2060 | parse_xml_btrace_conf_pt (struct gdb_xml_parser *parser, |
| 2061 | const struct gdb_xml_element *element, |
| 2062 | void *user_data, VEC (gdb_xml_value_s) *attributes) |
| 2063 | { |
| 2064 | struct btrace_config *conf; |
| 2065 | struct gdb_xml_value *size; |
| 2066 | |
| 2067 | conf = (struct btrace_config *) user_data; |
| 2068 | conf->format = BTRACE_FORMAT_PT; |
| 2069 | conf->pt.size = 0; |
| 2070 | |
| 2071 | size = xml_find_attribute (attributes, "size"); |
| 2072 | if (size != NULL) |
| 2073 | conf->pt.size = (unsigned int) *(ULONGEST *) size->value; |
| 2074 | } |
| 2075 | |
| 2076 | static const struct gdb_xml_attribute btrace_conf_pt_attributes[] = { |
| 2077 | { "size", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL }, |
| 2078 | { NULL, GDB_XML_AF_NONE, NULL, NULL } |
| 2079 | }; |
| 2080 | |
| 2081 | static const struct gdb_xml_attribute btrace_conf_bts_attributes[] = { |
| 2082 | { "size", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL }, |
| 2083 | { NULL, GDB_XML_AF_NONE, NULL, NULL } |
| 2084 | }; |
| 2085 | |
| 2086 | static const struct gdb_xml_element btrace_conf_children[] = { |
| 2087 | { "bts", btrace_conf_bts_attributes, NULL, GDB_XML_EF_OPTIONAL, |
| 2088 | parse_xml_btrace_conf_bts, NULL }, |
| 2089 | { "pt", btrace_conf_pt_attributes, NULL, GDB_XML_EF_OPTIONAL, |
| 2090 | parse_xml_btrace_conf_pt, NULL }, |
| 2091 | { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } |
| 2092 | }; |
| 2093 | |
| 2094 | static const struct gdb_xml_attribute btrace_conf_attributes[] = { |
| 2095 | { "version", GDB_XML_AF_NONE, NULL, NULL }, |
| 2096 | { NULL, GDB_XML_AF_NONE, NULL, NULL } |
| 2097 | }; |
| 2098 | |
| 2099 | static const struct gdb_xml_element btrace_conf_elements[] = { |
| 2100 | { "btrace-conf", btrace_conf_attributes, btrace_conf_children, |
| 2101 | GDB_XML_EF_NONE, NULL, NULL }, |
| 2102 | { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL } |
| 2103 | }; |
| 2104 | |
| 2105 | #endif /* defined (HAVE_LIBEXPAT) */ |
| 2106 | |
| 2107 | /* See btrace.h. */ |
| 2108 | |
| 2109 | void |
| 2110 | parse_xml_btrace_conf (struct btrace_config *conf, const char *xml) |
| 2111 | { |
| 2112 | int errcode; |
| 2113 | |
| 2114 | #if defined (HAVE_LIBEXPAT) |
| 2115 | |
| 2116 | errcode = gdb_xml_parse_quick (_("btrace-conf"), "btrace-conf.dtd", |
| 2117 | btrace_conf_elements, xml, conf); |
| 2118 | if (errcode != 0) |
| 2119 | error (_("Error parsing branch trace configuration.")); |
| 2120 | |
| 2121 | #else /* !defined (HAVE_LIBEXPAT) */ |
| 2122 | |
| 2123 | error (_("XML parsing is not supported.")); |
| 2124 | |
| 2125 | #endif /* !defined (HAVE_LIBEXPAT) */ |
| 2126 | } |
| 2127 | |
| 2128 | /* See btrace.h. */ |
| 2129 | |
| 2130 | const struct btrace_insn * |
| 2131 | btrace_insn_get (const struct btrace_insn_iterator *it) |
| 2132 | { |
| 2133 | const struct btrace_function *bfun; |
| 2134 | unsigned int index, end; |
| 2135 | |
| 2136 | index = it->index; |
| 2137 | bfun = it->function; |
| 2138 | |
| 2139 | /* Check if the iterator points to a gap in the trace. */ |
| 2140 | if (bfun->errcode != 0) |
| 2141 | return NULL; |
| 2142 | |
| 2143 | /* The index is within the bounds of this function's instruction vector. */ |
| 2144 | end = VEC_length (btrace_insn_s, bfun->insn); |
| 2145 | gdb_assert (0 < end); |
| 2146 | gdb_assert (index < end); |
| 2147 | |
| 2148 | return VEC_index (btrace_insn_s, bfun->insn, index); |
| 2149 | } |
| 2150 | |
| 2151 | /* See btrace.h. */ |
| 2152 | |
| 2153 | unsigned int |
| 2154 | btrace_insn_number (const struct btrace_insn_iterator *it) |
| 2155 | { |
| 2156 | const struct btrace_function *bfun; |
| 2157 | |
| 2158 | bfun = it->function; |
| 2159 | |
| 2160 | /* Return zero if the iterator points to a gap in the trace. */ |
| 2161 | if (bfun->errcode != 0) |
| 2162 | return 0; |
| 2163 | |
| 2164 | return bfun->insn_offset + it->index; |
| 2165 | } |
| 2166 | |
| 2167 | /* See btrace.h. */ |
| 2168 | |
| 2169 | void |
| 2170 | btrace_insn_begin (struct btrace_insn_iterator *it, |
| 2171 | const struct btrace_thread_info *btinfo) |
| 2172 | { |
| 2173 | const struct btrace_function *bfun; |
| 2174 | |
| 2175 | bfun = btinfo->begin; |
| 2176 | if (bfun == NULL) |
| 2177 | error (_("No trace.")); |
| 2178 | |
| 2179 | it->function = bfun; |
| 2180 | it->index = 0; |
| 2181 | } |
| 2182 | |
| 2183 | /* See btrace.h. */ |
| 2184 | |
| 2185 | void |
| 2186 | btrace_insn_end (struct btrace_insn_iterator *it, |
| 2187 | const struct btrace_thread_info *btinfo) |
| 2188 | { |
| 2189 | const struct btrace_function *bfun; |
| 2190 | unsigned int length; |
| 2191 | |
| 2192 | bfun = btinfo->end; |
| 2193 | if (bfun == NULL) |
| 2194 | error (_("No trace.")); |
| 2195 | |
| 2196 | length = VEC_length (btrace_insn_s, bfun->insn); |
| 2197 | |
| 2198 | /* The last function may either be a gap or it contains the current |
| 2199 | instruction, which is one past the end of the execution trace; ignore |
| 2200 | it. */ |
| 2201 | if (length > 0) |
| 2202 | length -= 1; |
| 2203 | |
| 2204 | it->function = bfun; |
| 2205 | it->index = length; |
| 2206 | } |
| 2207 | |
| 2208 | /* See btrace.h. */ |
| 2209 | |
| 2210 | unsigned int |
| 2211 | btrace_insn_next (struct btrace_insn_iterator *it, unsigned int stride) |
| 2212 | { |
| 2213 | const struct btrace_function *bfun; |
| 2214 | unsigned int index, steps; |
| 2215 | |
| 2216 | bfun = it->function; |
| 2217 | steps = 0; |
| 2218 | index = it->index; |
| 2219 | |
| 2220 | while (stride != 0) |
| 2221 | { |
| 2222 | unsigned int end, space, adv; |
| 2223 | |
| 2224 | end = VEC_length (btrace_insn_s, bfun->insn); |
| 2225 | |
| 2226 | /* An empty function segment represents a gap in the trace. We count |
| 2227 | it as one instruction. */ |
| 2228 | if (end == 0) |
| 2229 | { |
| 2230 | const struct btrace_function *next; |
| 2231 | |
| 2232 | next = bfun->flow.next; |
| 2233 | if (next == NULL) |
| 2234 | break; |
| 2235 | |
| 2236 | stride -= 1; |
| 2237 | steps += 1; |
| 2238 | |
| 2239 | bfun = next; |
| 2240 | index = 0; |
| 2241 | |
| 2242 | continue; |
| 2243 | } |
| 2244 | |
| 2245 | gdb_assert (0 < end); |
| 2246 | gdb_assert (index < end); |
| 2247 | |
| 2248 | /* Compute the number of instructions remaining in this segment. */ |
| 2249 | space = end - index; |
| 2250 | |
| 2251 | /* Advance the iterator as far as possible within this segment. */ |
| 2252 | adv = std::min (space, stride); |
| 2253 | stride -= adv; |
| 2254 | index += adv; |
| 2255 | steps += adv; |
| 2256 | |
| 2257 | /* Move to the next function if we're at the end of this one. */ |
| 2258 | if (index == end) |
| 2259 | { |
| 2260 | const struct btrace_function *next; |
| 2261 | |
| 2262 | next = bfun->flow.next; |
| 2263 | if (next == NULL) |
| 2264 | { |
| 2265 | /* We stepped past the last function. |
| 2266 | |
| 2267 | Let's adjust the index to point to the last instruction in |
| 2268 | the previous function. */ |
| 2269 | index -= 1; |
| 2270 | steps -= 1; |
| 2271 | break; |
| 2272 | } |
| 2273 | |
| 2274 | /* We now point to the first instruction in the new function. */ |
| 2275 | bfun = next; |
| 2276 | index = 0; |
| 2277 | } |
| 2278 | |
| 2279 | /* We did make progress. */ |
| 2280 | gdb_assert (adv > 0); |
| 2281 | } |
| 2282 | |
| 2283 | /* Update the iterator. */ |
| 2284 | it->function = bfun; |
| 2285 | it->index = index; |
| 2286 | |
| 2287 | return steps; |
| 2288 | } |
| 2289 | |
| 2290 | /* See btrace.h. */ |
| 2291 | |
| 2292 | unsigned int |
| 2293 | btrace_insn_prev (struct btrace_insn_iterator *it, unsigned int stride) |
| 2294 | { |
| 2295 | const struct btrace_function *bfun; |
| 2296 | unsigned int index, steps; |
| 2297 | |
| 2298 | bfun = it->function; |
| 2299 | steps = 0; |
| 2300 | index = it->index; |
| 2301 | |
| 2302 | while (stride != 0) |
| 2303 | { |
| 2304 | unsigned int adv; |
| 2305 | |
| 2306 | /* Move to the previous function if we're at the start of this one. */ |
| 2307 | if (index == 0) |
| 2308 | { |
| 2309 | const struct btrace_function *prev; |
| 2310 | |
| 2311 | prev = bfun->flow.prev; |
| 2312 | if (prev == NULL) |
| 2313 | break; |
| 2314 | |
| 2315 | /* We point to one after the last instruction in the new function. */ |
| 2316 | bfun = prev; |
| 2317 | index = VEC_length (btrace_insn_s, bfun->insn); |
| 2318 | |
| 2319 | /* An empty function segment represents a gap in the trace. We count |
| 2320 | it as one instruction. */ |
| 2321 | if (index == 0) |
| 2322 | { |
| 2323 | stride -= 1; |
| 2324 | steps += 1; |
| 2325 | |
| 2326 | continue; |
| 2327 | } |
| 2328 | } |
| 2329 | |
| 2330 | /* Advance the iterator as far as possible within this segment. */ |
| 2331 | adv = std::min (index, stride); |
| 2332 | |
| 2333 | stride -= adv; |
| 2334 | index -= adv; |
| 2335 | steps += adv; |
| 2336 | |
| 2337 | /* We did make progress. */ |
| 2338 | gdb_assert (adv > 0); |
| 2339 | } |
| 2340 | |
| 2341 | /* Update the iterator. */ |
| 2342 | it->function = bfun; |
| 2343 | it->index = index; |
| 2344 | |
| 2345 | return steps; |
| 2346 | } |
| 2347 | |
| 2348 | /* See btrace.h. */ |
| 2349 | |
| 2350 | int |
| 2351 | btrace_insn_cmp (const struct btrace_insn_iterator *lhs, |
| 2352 | const struct btrace_insn_iterator *rhs) |
| 2353 | { |
| 2354 | unsigned int lnum, rnum; |
| 2355 | |
| 2356 | lnum = btrace_insn_number (lhs); |
| 2357 | rnum = btrace_insn_number (rhs); |
| 2358 | |
| 2359 | /* A gap has an instruction number of zero. Things are getting more |
| 2360 | complicated if gaps are involved. |
| 2361 | |
| 2362 | We take the instruction number offset from the iterator's function. |
| 2363 | This is the number of the first instruction after the gap. |
| 2364 | |
| 2365 | This is OK as long as both lhs and rhs point to gaps. If only one of |
| 2366 | them does, we need to adjust the number based on the other's regular |
| 2367 | instruction number. Otherwise, a gap might compare equal to an |
| 2368 | instruction. */ |
| 2369 | |
| 2370 | if (lnum == 0 && rnum == 0) |
| 2371 | { |
| 2372 | lnum = lhs->function->insn_offset; |
| 2373 | rnum = rhs->function->insn_offset; |
| 2374 | } |
| 2375 | else if (lnum == 0) |
| 2376 | { |
| 2377 | lnum = lhs->function->insn_offset; |
| 2378 | |
| 2379 | if (lnum == rnum) |
| 2380 | lnum -= 1; |
| 2381 | } |
| 2382 | else if (rnum == 0) |
| 2383 | { |
| 2384 | rnum = rhs->function->insn_offset; |
| 2385 | |
| 2386 | if (rnum == lnum) |
| 2387 | rnum -= 1; |
| 2388 | } |
| 2389 | |
| 2390 | return (int) (lnum - rnum); |
| 2391 | } |
| 2392 | |
| 2393 | /* See btrace.h. */ |
| 2394 | |
| 2395 | int |
| 2396 | btrace_find_insn_by_number (struct btrace_insn_iterator *it, |
| 2397 | const struct btrace_thread_info *btinfo, |
| 2398 | unsigned int number) |
| 2399 | { |
| 2400 | const struct btrace_function *bfun; |
| 2401 | unsigned int end, length; |
| 2402 | |
| 2403 | for (bfun = btinfo->end; bfun != NULL; bfun = bfun->flow.prev) |
| 2404 | { |
| 2405 | /* Skip gaps. */ |
| 2406 | if (bfun->errcode != 0) |
| 2407 | continue; |
| 2408 | |
| 2409 | if (bfun->insn_offset <= number) |
| 2410 | break; |
| 2411 | } |
| 2412 | |
| 2413 | if (bfun == NULL) |
| 2414 | return 0; |
| 2415 | |
| 2416 | length = VEC_length (btrace_insn_s, bfun->insn); |
| 2417 | gdb_assert (length > 0); |
| 2418 | |
| 2419 | end = bfun->insn_offset + length; |
| 2420 | if (end <= number) |
| 2421 | return 0; |
| 2422 | |
| 2423 | it->function = bfun; |
| 2424 | it->index = number - bfun->insn_offset; |
| 2425 | |
| 2426 | return 1; |
| 2427 | } |
| 2428 | |
| 2429 | /* See btrace.h. */ |
| 2430 | |
| 2431 | const struct btrace_function * |
| 2432 | btrace_call_get (const struct btrace_call_iterator *it) |
| 2433 | { |
| 2434 | return it->function; |
| 2435 | } |
| 2436 | |
| 2437 | /* See btrace.h. */ |
| 2438 | |
| 2439 | unsigned int |
| 2440 | btrace_call_number (const struct btrace_call_iterator *it) |
| 2441 | { |
| 2442 | const struct btrace_thread_info *btinfo; |
| 2443 | const struct btrace_function *bfun; |
| 2444 | unsigned int insns; |
| 2445 | |
| 2446 | btinfo = it->btinfo; |
| 2447 | bfun = it->function; |
| 2448 | if (bfun != NULL) |
| 2449 | return bfun->number; |
| 2450 | |
| 2451 | /* For the end iterator, i.e. bfun == NULL, we return one more than the |
| 2452 | number of the last function. */ |
| 2453 | bfun = btinfo->end; |
| 2454 | insns = VEC_length (btrace_insn_s, bfun->insn); |
| 2455 | |
| 2456 | /* If the function contains only a single instruction (i.e. the current |
| 2457 | instruction), it will be skipped and its number is already the number |
| 2458 | we seek. */ |
| 2459 | if (insns == 1) |
| 2460 | return bfun->number; |
| 2461 | |
| 2462 | /* Otherwise, return one more than the number of the last function. */ |
| 2463 | return bfun->number + 1; |
| 2464 | } |
| 2465 | |
| 2466 | /* See btrace.h. */ |
| 2467 | |
| 2468 | void |
| 2469 | btrace_call_begin (struct btrace_call_iterator *it, |
| 2470 | const struct btrace_thread_info *btinfo) |
| 2471 | { |
| 2472 | const struct btrace_function *bfun; |
| 2473 | |
| 2474 | bfun = btinfo->begin; |
| 2475 | if (bfun == NULL) |
| 2476 | error (_("No trace.")); |
| 2477 | |
| 2478 | it->btinfo = btinfo; |
| 2479 | it->function = bfun; |
| 2480 | } |
| 2481 | |
| 2482 | /* See btrace.h. */ |
| 2483 | |
| 2484 | void |
| 2485 | btrace_call_end (struct btrace_call_iterator *it, |
| 2486 | const struct btrace_thread_info *btinfo) |
| 2487 | { |
| 2488 | const struct btrace_function *bfun; |
| 2489 | |
| 2490 | bfun = btinfo->end; |
| 2491 | if (bfun == NULL) |
| 2492 | error (_("No trace.")); |
| 2493 | |
| 2494 | it->btinfo = btinfo; |
| 2495 | it->function = NULL; |
| 2496 | } |
| 2497 | |
| 2498 | /* See btrace.h. */ |
| 2499 | |
| 2500 | unsigned int |
| 2501 | btrace_call_next (struct btrace_call_iterator *it, unsigned int stride) |
| 2502 | { |
| 2503 | const struct btrace_function *bfun; |
| 2504 | unsigned int steps; |
| 2505 | |
| 2506 | bfun = it->function; |
| 2507 | steps = 0; |
| 2508 | while (bfun != NULL) |
| 2509 | { |
| 2510 | const struct btrace_function *next; |
| 2511 | unsigned int insns; |
| 2512 | |
| 2513 | next = bfun->flow.next; |
| 2514 | if (next == NULL) |
| 2515 | { |
| 2516 | /* Ignore the last function if it only contains a single |
| 2517 | (i.e. the current) instruction. */ |
| 2518 | insns = VEC_length (btrace_insn_s, bfun->insn); |
| 2519 | if (insns == 1) |
| 2520 | steps -= 1; |
| 2521 | } |
| 2522 | |
| 2523 | if (stride == steps) |
| 2524 | break; |
| 2525 | |
| 2526 | bfun = next; |
| 2527 | steps += 1; |
| 2528 | } |
| 2529 | |
| 2530 | it->function = bfun; |
| 2531 | return steps; |
| 2532 | } |
| 2533 | |
| 2534 | /* See btrace.h. */ |
| 2535 | |
| 2536 | unsigned int |
| 2537 | btrace_call_prev (struct btrace_call_iterator *it, unsigned int stride) |
| 2538 | { |
| 2539 | const struct btrace_thread_info *btinfo; |
| 2540 | const struct btrace_function *bfun; |
| 2541 | unsigned int steps; |
| 2542 | |
| 2543 | bfun = it->function; |
| 2544 | steps = 0; |
| 2545 | |
| 2546 | if (bfun == NULL) |
| 2547 | { |
| 2548 | unsigned int insns; |
| 2549 | |
| 2550 | btinfo = it->btinfo; |
| 2551 | bfun = btinfo->end; |
| 2552 | if (bfun == NULL) |
| 2553 | return 0; |
| 2554 | |
| 2555 | /* Ignore the last function if it only contains a single |
| 2556 | (i.e. the current) instruction. */ |
| 2557 | insns = VEC_length (btrace_insn_s, bfun->insn); |
| 2558 | if (insns == 1) |
| 2559 | bfun = bfun->flow.prev; |
| 2560 | |
| 2561 | if (bfun == NULL) |
| 2562 | return 0; |
| 2563 | |
| 2564 | steps += 1; |
| 2565 | } |
| 2566 | |
| 2567 | while (steps < stride) |
| 2568 | { |
| 2569 | const struct btrace_function *prev; |
| 2570 | |
| 2571 | prev = bfun->flow.prev; |
| 2572 | if (prev == NULL) |
| 2573 | break; |
| 2574 | |
| 2575 | bfun = prev; |
| 2576 | steps += 1; |
| 2577 | } |
| 2578 | |
| 2579 | it->function = bfun; |
| 2580 | return steps; |
| 2581 | } |
| 2582 | |
| 2583 | /* See btrace.h. */ |
| 2584 | |
| 2585 | int |
| 2586 | btrace_call_cmp (const struct btrace_call_iterator *lhs, |
| 2587 | const struct btrace_call_iterator *rhs) |
| 2588 | { |
| 2589 | unsigned int lnum, rnum; |
| 2590 | |
| 2591 | lnum = btrace_call_number (lhs); |
| 2592 | rnum = btrace_call_number (rhs); |
| 2593 | |
| 2594 | return (int) (lnum - rnum); |
| 2595 | } |
| 2596 | |
| 2597 | /* See btrace.h. */ |
| 2598 | |
| 2599 | int |
| 2600 | btrace_find_call_by_number (struct btrace_call_iterator *it, |
| 2601 | const struct btrace_thread_info *btinfo, |
| 2602 | unsigned int number) |
| 2603 | { |
| 2604 | const struct btrace_function *bfun; |
| 2605 | |
| 2606 | for (bfun = btinfo->end; bfun != NULL; bfun = bfun->flow.prev) |
| 2607 | { |
| 2608 | unsigned int bnum; |
| 2609 | |
| 2610 | bnum = bfun->number; |
| 2611 | if (number == bnum) |
| 2612 | { |
| 2613 | it->btinfo = btinfo; |
| 2614 | it->function = bfun; |
| 2615 | return 1; |
| 2616 | } |
| 2617 | |
| 2618 | /* Functions are ordered and numbered consecutively. We could bail out |
| 2619 | earlier. On the other hand, it is very unlikely that we search for |
| 2620 | a nonexistent function. */ |
| 2621 | } |
| 2622 | |
| 2623 | return 0; |
| 2624 | } |
| 2625 | |
| 2626 | /* See btrace.h. */ |
| 2627 | |
| 2628 | void |
| 2629 | btrace_set_insn_history (struct btrace_thread_info *btinfo, |
| 2630 | const struct btrace_insn_iterator *begin, |
| 2631 | const struct btrace_insn_iterator *end) |
| 2632 | { |
| 2633 | if (btinfo->insn_history == NULL) |
| 2634 | btinfo->insn_history = XCNEW (struct btrace_insn_history); |
| 2635 | |
| 2636 | btinfo->insn_history->begin = *begin; |
| 2637 | btinfo->insn_history->end = *end; |
| 2638 | } |
| 2639 | |
| 2640 | /* See btrace.h. */ |
| 2641 | |
| 2642 | void |
| 2643 | btrace_set_call_history (struct btrace_thread_info *btinfo, |
| 2644 | const struct btrace_call_iterator *begin, |
| 2645 | const struct btrace_call_iterator *end) |
| 2646 | { |
| 2647 | gdb_assert (begin->btinfo == end->btinfo); |
| 2648 | |
| 2649 | if (btinfo->call_history == NULL) |
| 2650 | btinfo->call_history = XCNEW (struct btrace_call_history); |
| 2651 | |
| 2652 | btinfo->call_history->begin = *begin; |
| 2653 | btinfo->call_history->end = *end; |
| 2654 | } |
| 2655 | |
| 2656 | /* See btrace.h. */ |
| 2657 | |
| 2658 | int |
| 2659 | btrace_is_replaying (struct thread_info *tp) |
| 2660 | { |
| 2661 | return tp->btrace.replay != NULL; |
| 2662 | } |
| 2663 | |
| 2664 | /* See btrace.h. */ |
| 2665 | |
| 2666 | int |
| 2667 | btrace_is_empty (struct thread_info *tp) |
| 2668 | { |
| 2669 | struct btrace_insn_iterator begin, end; |
| 2670 | struct btrace_thread_info *btinfo; |
| 2671 | |
| 2672 | btinfo = &tp->btrace; |
| 2673 | |
| 2674 | if (btinfo->begin == NULL) |
| 2675 | return 1; |
| 2676 | |
| 2677 | btrace_insn_begin (&begin, btinfo); |
| 2678 | btrace_insn_end (&end, btinfo); |
| 2679 | |
| 2680 | return btrace_insn_cmp (&begin, &end) == 0; |
| 2681 | } |
| 2682 | |
| 2683 | /* Forward the cleanup request. */ |
| 2684 | |
| 2685 | static void |
| 2686 | do_btrace_data_cleanup (void *arg) |
| 2687 | { |
| 2688 | btrace_data_fini ((struct btrace_data *) arg); |
| 2689 | } |
| 2690 | |
| 2691 | /* See btrace.h. */ |
| 2692 | |
| 2693 | struct cleanup * |
| 2694 | make_cleanup_btrace_data (struct btrace_data *data) |
| 2695 | { |
| 2696 | return make_cleanup (do_btrace_data_cleanup, data); |
| 2697 | } |
| 2698 | |
| 2699 | #if defined (HAVE_LIBIPT) |
| 2700 | |
| 2701 | /* Print a single packet. */ |
| 2702 | |
| 2703 | static void |
| 2704 | pt_print_packet (const struct pt_packet *packet) |
| 2705 | { |
| 2706 | switch (packet->type) |
| 2707 | { |
| 2708 | default: |
| 2709 | printf_unfiltered (("[??: %x]"), packet->type); |
| 2710 | break; |
| 2711 | |
| 2712 | case ppt_psb: |
| 2713 | printf_unfiltered (("psb")); |
| 2714 | break; |
| 2715 | |
| 2716 | case ppt_psbend: |
| 2717 | printf_unfiltered (("psbend")); |
| 2718 | break; |
| 2719 | |
| 2720 | case ppt_pad: |
| 2721 | printf_unfiltered (("pad")); |
| 2722 | break; |
| 2723 | |
| 2724 | case ppt_tip: |
| 2725 | printf_unfiltered (("tip %u: 0x%" PRIx64 ""), |
| 2726 | packet->payload.ip.ipc, |
| 2727 | packet->payload.ip.ip); |
| 2728 | break; |
| 2729 | |
| 2730 | case ppt_tip_pge: |
| 2731 | printf_unfiltered (("tip.pge %u: 0x%" PRIx64 ""), |
| 2732 | packet->payload.ip.ipc, |
| 2733 | packet->payload.ip.ip); |
| 2734 | break; |
| 2735 | |
| 2736 | case ppt_tip_pgd: |
| 2737 | printf_unfiltered (("tip.pgd %u: 0x%" PRIx64 ""), |
| 2738 | packet->payload.ip.ipc, |
| 2739 | packet->payload.ip.ip); |
| 2740 | break; |
| 2741 | |
| 2742 | case ppt_fup: |
| 2743 | printf_unfiltered (("fup %u: 0x%" PRIx64 ""), |
| 2744 | packet->payload.ip.ipc, |
| 2745 | packet->payload.ip.ip); |
| 2746 | break; |
| 2747 | |
| 2748 | case ppt_tnt_8: |
| 2749 | printf_unfiltered (("tnt-8 %u: 0x%" PRIx64 ""), |
| 2750 | packet->payload.tnt.bit_size, |
| 2751 | packet->payload.tnt.payload); |
| 2752 | break; |
| 2753 | |
| 2754 | case ppt_tnt_64: |
| 2755 | printf_unfiltered (("tnt-64 %u: 0x%" PRIx64 ""), |
| 2756 | packet->payload.tnt.bit_size, |
| 2757 | packet->payload.tnt.payload); |
| 2758 | break; |
| 2759 | |
| 2760 | case ppt_pip: |
| 2761 | printf_unfiltered (("pip %" PRIx64 "%s"), packet->payload.pip.cr3, |
| 2762 | packet->payload.pip.nr ? (" nr") : ("")); |
| 2763 | break; |
| 2764 | |
| 2765 | case ppt_tsc: |
| 2766 | printf_unfiltered (("tsc %" PRIx64 ""), packet->payload.tsc.tsc); |
| 2767 | break; |
| 2768 | |
| 2769 | case ppt_cbr: |
| 2770 | printf_unfiltered (("cbr %u"), packet->payload.cbr.ratio); |
| 2771 | break; |
| 2772 | |
| 2773 | case ppt_mode: |
| 2774 | switch (packet->payload.mode.leaf) |
| 2775 | { |
| 2776 | default: |
| 2777 | printf_unfiltered (("mode %u"), packet->payload.mode.leaf); |
| 2778 | break; |
| 2779 | |
| 2780 | case pt_mol_exec: |
| 2781 | printf_unfiltered (("mode.exec%s%s"), |
| 2782 | packet->payload.mode.bits.exec.csl |
| 2783 | ? (" cs.l") : (""), |
| 2784 | packet->payload.mode.bits.exec.csd |
| 2785 | ? (" cs.d") : ("")); |
| 2786 | break; |
| 2787 | |
| 2788 | case pt_mol_tsx: |
| 2789 | printf_unfiltered (("mode.tsx%s%s"), |
| 2790 | packet->payload.mode.bits.tsx.intx |
| 2791 | ? (" intx") : (""), |
| 2792 | packet->payload.mode.bits.tsx.abrt |
| 2793 | ? (" abrt") : ("")); |
| 2794 | break; |
| 2795 | } |
| 2796 | break; |
| 2797 | |
| 2798 | case ppt_ovf: |
| 2799 | printf_unfiltered (("ovf")); |
| 2800 | break; |
| 2801 | |
| 2802 | case ppt_stop: |
| 2803 | printf_unfiltered (("stop")); |
| 2804 | break; |
| 2805 | |
| 2806 | case ppt_vmcs: |
| 2807 | printf_unfiltered (("vmcs %" PRIx64 ""), packet->payload.vmcs.base); |
| 2808 | break; |
| 2809 | |
| 2810 | case ppt_tma: |
| 2811 | printf_unfiltered (("tma %x %x"), packet->payload.tma.ctc, |
| 2812 | packet->payload.tma.fc); |
| 2813 | break; |
| 2814 | |
| 2815 | case ppt_mtc: |
| 2816 | printf_unfiltered (("mtc %x"), packet->payload.mtc.ctc); |
| 2817 | break; |
| 2818 | |
| 2819 | case ppt_cyc: |
| 2820 | printf_unfiltered (("cyc %" PRIx64 ""), packet->payload.cyc.value); |
| 2821 | break; |
| 2822 | |
| 2823 | case ppt_mnt: |
| 2824 | printf_unfiltered (("mnt %" PRIx64 ""), packet->payload.mnt.payload); |
| 2825 | break; |
| 2826 | } |
| 2827 | } |
| 2828 | |
| 2829 | /* Decode packets into MAINT using DECODER. */ |
| 2830 | |
| 2831 | static void |
| 2832 | btrace_maint_decode_pt (struct btrace_maint_info *maint, |
| 2833 | struct pt_packet_decoder *decoder) |
| 2834 | { |
| 2835 | int errcode; |
| 2836 | |
| 2837 | for (;;) |
| 2838 | { |
| 2839 | struct btrace_pt_packet packet; |
| 2840 | |
| 2841 | errcode = pt_pkt_sync_forward (decoder); |
| 2842 | if (errcode < 0) |
| 2843 | break; |
| 2844 | |
| 2845 | for (;;) |
| 2846 | { |
| 2847 | pt_pkt_get_offset (decoder, &packet.offset); |
| 2848 | |
| 2849 | errcode = pt_pkt_next (decoder, &packet.packet, |
| 2850 | sizeof(packet.packet)); |
| 2851 | if (errcode < 0) |
| 2852 | break; |
| 2853 | |
| 2854 | if (maint_btrace_pt_skip_pad == 0 || packet.packet.type != ppt_pad) |
| 2855 | { |
| 2856 | packet.errcode = pt_errcode (errcode); |
| 2857 | VEC_safe_push (btrace_pt_packet_s, maint->variant.pt.packets, |
| 2858 | &packet); |
| 2859 | } |
| 2860 | } |
| 2861 | |
| 2862 | if (errcode == -pte_eos) |
| 2863 | break; |
| 2864 | |
| 2865 | packet.errcode = pt_errcode (errcode); |
| 2866 | VEC_safe_push (btrace_pt_packet_s, maint->variant.pt.packets, |
| 2867 | &packet); |
| 2868 | |
| 2869 | warning (_("Error at trace offset 0x%" PRIx64 ": %s."), |
| 2870 | packet.offset, pt_errstr (packet.errcode)); |
| 2871 | } |
| 2872 | |
| 2873 | if (errcode != -pte_eos) |
| 2874 | warning (_("Failed to synchronize onto the Intel Processor Trace " |
| 2875 | "stream: %s."), pt_errstr (pt_errcode (errcode))); |
| 2876 | } |
| 2877 | |
| 2878 | /* Update the packet history in BTINFO. */ |
| 2879 | |
| 2880 | static void |
| 2881 | btrace_maint_update_pt_packets (struct btrace_thread_info *btinfo) |
| 2882 | { |
| 2883 | volatile struct gdb_exception except; |
| 2884 | struct pt_packet_decoder *decoder; |
| 2885 | struct btrace_data_pt *pt; |
| 2886 | struct pt_config config; |
| 2887 | int errcode; |
| 2888 | |
| 2889 | pt = &btinfo->data.variant.pt; |
| 2890 | |
| 2891 | /* Nothing to do if there is no trace. */ |
| 2892 | if (pt->size == 0) |
| 2893 | return; |
| 2894 | |
| 2895 | memset (&config, 0, sizeof(config)); |
| 2896 | |
| 2897 | config.size = sizeof (config); |
| 2898 | config.begin = pt->data; |
| 2899 | config.end = pt->data + pt->size; |
| 2900 | |
| 2901 | config.cpu.vendor = pt_translate_cpu_vendor (pt->config.cpu.vendor); |
| 2902 | config.cpu.family = pt->config.cpu.family; |
| 2903 | config.cpu.model = pt->config.cpu.model; |
| 2904 | config.cpu.stepping = pt->config.cpu.stepping; |
| 2905 | |
| 2906 | errcode = pt_cpu_errata (&config.errata, &config.cpu); |
| 2907 | if (errcode < 0) |
| 2908 | error (_("Failed to configure the Intel Processor Trace decoder: %s."), |
| 2909 | pt_errstr (pt_errcode (errcode))); |
| 2910 | |
| 2911 | decoder = pt_pkt_alloc_decoder (&config); |
| 2912 | if (decoder == NULL) |
| 2913 | error (_("Failed to allocate the Intel Processor Trace decoder.")); |
| 2914 | |
| 2915 | TRY |
| 2916 | { |
| 2917 | btrace_maint_decode_pt (&btinfo->maint, decoder); |
| 2918 | } |
| 2919 | CATCH (except, RETURN_MASK_ALL) |
| 2920 | { |
| 2921 | pt_pkt_free_decoder (decoder); |
| 2922 | |
| 2923 | if (except.reason < 0) |
| 2924 | throw_exception (except); |
| 2925 | } |
| 2926 | END_CATCH |
| 2927 | |
| 2928 | pt_pkt_free_decoder (decoder); |
| 2929 | } |
| 2930 | |
| 2931 | #endif /* !defined (HAVE_LIBIPT) */ |
| 2932 | |
| 2933 | /* Update the packet maintenance information for BTINFO and store the |
| 2934 | low and high bounds into BEGIN and END, respectively. |
| 2935 | Store the current iterator state into FROM and TO. */ |
| 2936 | |
| 2937 | static void |
| 2938 | btrace_maint_update_packets (struct btrace_thread_info *btinfo, |
| 2939 | unsigned int *begin, unsigned int *end, |
| 2940 | unsigned int *from, unsigned int *to) |
| 2941 | { |
| 2942 | switch (btinfo->data.format) |
| 2943 | { |
| 2944 | default: |
| 2945 | *begin = 0; |
| 2946 | *end = 0; |
| 2947 | *from = 0; |
| 2948 | *to = 0; |
| 2949 | break; |
| 2950 | |
| 2951 | case BTRACE_FORMAT_BTS: |
| 2952 | /* Nothing to do - we operate directly on BTINFO->DATA. */ |
| 2953 | *begin = 0; |
| 2954 | *end = VEC_length (btrace_block_s, btinfo->data.variant.bts.blocks); |
| 2955 | *from = btinfo->maint.variant.bts.packet_history.begin; |
| 2956 | *to = btinfo->maint.variant.bts.packet_history.end; |
| 2957 | break; |
| 2958 | |
| 2959 | #if defined (HAVE_LIBIPT) |
| 2960 | case BTRACE_FORMAT_PT: |
| 2961 | if (VEC_empty (btrace_pt_packet_s, btinfo->maint.variant.pt.packets)) |
| 2962 | btrace_maint_update_pt_packets (btinfo); |
| 2963 | |
| 2964 | *begin = 0; |
| 2965 | *end = VEC_length (btrace_pt_packet_s, btinfo->maint.variant.pt.packets); |
| 2966 | *from = btinfo->maint.variant.pt.packet_history.begin; |
| 2967 | *to = btinfo->maint.variant.pt.packet_history.end; |
| 2968 | break; |
| 2969 | #endif /* defined (HAVE_LIBIPT) */ |
| 2970 | } |
| 2971 | } |
| 2972 | |
| 2973 | /* Print packets in BTINFO from BEGIN (inclusive) until END (exclusive) and |
| 2974 | update the current iterator position. */ |
| 2975 | |
| 2976 | static void |
| 2977 | btrace_maint_print_packets (struct btrace_thread_info *btinfo, |
| 2978 | unsigned int begin, unsigned int end) |
| 2979 | { |
| 2980 | switch (btinfo->data.format) |
| 2981 | { |
| 2982 | default: |
| 2983 | break; |
| 2984 | |
| 2985 | case BTRACE_FORMAT_BTS: |
| 2986 | { |
| 2987 | VEC (btrace_block_s) *blocks; |
| 2988 | unsigned int blk; |
| 2989 | |
| 2990 | blocks = btinfo->data.variant.bts.blocks; |
| 2991 | for (blk = begin; blk < end; ++blk) |
| 2992 | { |
| 2993 | const btrace_block_s *block; |
| 2994 | |
| 2995 | block = VEC_index (btrace_block_s, blocks, blk); |
| 2996 | |
| 2997 | printf_unfiltered ("%u\tbegin: %s, end: %s\n", blk, |
| 2998 | core_addr_to_string_nz (block->begin), |
| 2999 | core_addr_to_string_nz (block->end)); |
| 3000 | } |
| 3001 | |
| 3002 | btinfo->maint.variant.bts.packet_history.begin = begin; |
| 3003 | btinfo->maint.variant.bts.packet_history.end = end; |
| 3004 | } |
| 3005 | break; |
| 3006 | |
| 3007 | #if defined (HAVE_LIBIPT) |
| 3008 | case BTRACE_FORMAT_PT: |
| 3009 | { |
| 3010 | VEC (btrace_pt_packet_s) *packets; |
| 3011 | unsigned int pkt; |
| 3012 | |
| 3013 | packets = btinfo->maint.variant.pt.packets; |
| 3014 | for (pkt = begin; pkt < end; ++pkt) |
| 3015 | { |
| 3016 | const struct btrace_pt_packet *packet; |
| 3017 | |
| 3018 | packet = VEC_index (btrace_pt_packet_s, packets, pkt); |
| 3019 | |
| 3020 | printf_unfiltered ("%u\t", pkt); |
| 3021 | printf_unfiltered ("0x%" PRIx64 "\t", packet->offset); |
| 3022 | |
| 3023 | if (packet->errcode == pte_ok) |
| 3024 | pt_print_packet (&packet->packet); |
| 3025 | else |
| 3026 | printf_unfiltered ("[error: %s]", pt_errstr (packet->errcode)); |
| 3027 | |
| 3028 | printf_unfiltered ("\n"); |
| 3029 | } |
| 3030 | |
| 3031 | btinfo->maint.variant.pt.packet_history.begin = begin; |
| 3032 | btinfo->maint.variant.pt.packet_history.end = end; |
| 3033 | } |
| 3034 | break; |
| 3035 | #endif /* defined (HAVE_LIBIPT) */ |
| 3036 | } |
| 3037 | } |
| 3038 | |
| 3039 | /* Read a number from an argument string. */ |
| 3040 | |
| 3041 | static unsigned int |
| 3042 | get_uint (char **arg) |
| 3043 | { |
| 3044 | char *begin, *end, *pos; |
| 3045 | unsigned long number; |
| 3046 | |
| 3047 | begin = *arg; |
| 3048 | pos = skip_spaces (begin); |
| 3049 | |
| 3050 | if (!isdigit (*pos)) |
| 3051 | error (_("Expected positive number, got: %s."), pos); |
| 3052 | |
| 3053 | number = strtoul (pos, &end, 10); |
| 3054 | if (number > UINT_MAX) |
| 3055 | error (_("Number too big.")); |
| 3056 | |
| 3057 | *arg += (end - begin); |
| 3058 | |
| 3059 | return (unsigned int) number; |
| 3060 | } |
| 3061 | |
| 3062 | /* Read a context size from an argument string. */ |
| 3063 | |
| 3064 | static int |
| 3065 | get_context_size (char **arg) |
| 3066 | { |
| 3067 | char *pos; |
| 3068 | int number; |
| 3069 | |
| 3070 | pos = skip_spaces (*arg); |
| 3071 | |
| 3072 | if (!isdigit (*pos)) |
| 3073 | error (_("Expected positive number, got: %s."), pos); |
| 3074 | |
| 3075 | return strtol (pos, arg, 10); |
| 3076 | } |
| 3077 | |
| 3078 | /* Complain about junk at the end of an argument string. */ |
| 3079 | |
| 3080 | static void |
| 3081 | no_chunk (char *arg) |
| 3082 | { |
| 3083 | if (*arg != 0) |
| 3084 | error (_("Junk after argument: %s."), arg); |
| 3085 | } |
| 3086 | |
| 3087 | /* The "maintenance btrace packet-history" command. */ |
| 3088 | |
| 3089 | static void |
| 3090 | maint_btrace_packet_history_cmd (char *arg, int from_tty) |
| 3091 | { |
| 3092 | struct btrace_thread_info *btinfo; |
| 3093 | struct thread_info *tp; |
| 3094 | unsigned int size, begin, end, from, to; |
| 3095 | |
| 3096 | tp = find_thread_ptid (inferior_ptid); |
| 3097 | if (tp == NULL) |
| 3098 | error (_("No thread.")); |
| 3099 | |
| 3100 | size = 10; |
| 3101 | btinfo = &tp->btrace; |
| 3102 | |
| 3103 | btrace_maint_update_packets (btinfo, &begin, &end, &from, &to); |
| 3104 | if (begin == end) |
| 3105 | { |
| 3106 | printf_unfiltered (_("No trace.\n")); |
| 3107 | return; |
| 3108 | } |
| 3109 | |
| 3110 | if (arg == NULL || *arg == 0 || strcmp (arg, "+") == 0) |
| 3111 | { |
| 3112 | from = to; |
| 3113 | |
| 3114 | if (end - from < size) |
| 3115 | size = end - from; |
| 3116 | to = from + size; |
| 3117 | } |
| 3118 | else if (strcmp (arg, "-") == 0) |
| 3119 | { |
| 3120 | to = from; |
| 3121 | |
| 3122 | if (to - begin < size) |
| 3123 | size = to - begin; |
| 3124 | from = to - size; |
| 3125 | } |
| 3126 | else |
| 3127 | { |
| 3128 | from = get_uint (&arg); |
| 3129 | if (end <= from) |
| 3130 | error (_("'%u' is out of range."), from); |
| 3131 | |
| 3132 | arg = skip_spaces (arg); |
| 3133 | if (*arg == ',') |
| 3134 | { |
| 3135 | arg = skip_spaces (++arg); |
| 3136 | |
| 3137 | if (*arg == '+') |
| 3138 | { |
| 3139 | arg += 1; |
| 3140 | size = get_context_size (&arg); |
| 3141 | |
| 3142 | no_chunk (arg); |
| 3143 | |
| 3144 | if (end - from < size) |
| 3145 | size = end - from; |
| 3146 | to = from + size; |
| 3147 | } |
| 3148 | else if (*arg == '-') |
| 3149 | { |
| 3150 | arg += 1; |
| 3151 | size = get_context_size (&arg); |
| 3152 | |
| 3153 | no_chunk (arg); |
| 3154 | |
| 3155 | /* Include the packet given as first argument. */ |
| 3156 | from += 1; |
| 3157 | to = from; |
| 3158 | |
| 3159 | if (to - begin < size) |
| 3160 | size = to - begin; |
| 3161 | from = to - size; |
| 3162 | } |
| 3163 | else |
| 3164 | { |
| 3165 | to = get_uint (&arg); |
| 3166 | |
| 3167 | /* Include the packet at the second argument and silently |
| 3168 | truncate the range. */ |
| 3169 | if (to < end) |
| 3170 | to += 1; |
| 3171 | else |
| 3172 | to = end; |
| 3173 | |
| 3174 | no_chunk (arg); |
| 3175 | } |
| 3176 | } |
| 3177 | else |
| 3178 | { |
| 3179 | no_chunk (arg); |
| 3180 | |
| 3181 | if (end - from < size) |
| 3182 | size = end - from; |
| 3183 | to = from + size; |
| 3184 | } |
| 3185 | |
| 3186 | dont_repeat (); |
| 3187 | } |
| 3188 | |
| 3189 | btrace_maint_print_packets (btinfo, from, to); |
| 3190 | } |
| 3191 | |
| 3192 | /* The "maintenance btrace clear-packet-history" command. */ |
| 3193 | |
| 3194 | static void |
| 3195 | maint_btrace_clear_packet_history_cmd (char *args, int from_tty) |
| 3196 | { |
| 3197 | struct btrace_thread_info *btinfo; |
| 3198 | struct thread_info *tp; |
| 3199 | |
| 3200 | if (args != NULL && *args != 0) |
| 3201 | error (_("Invalid argument.")); |
| 3202 | |
| 3203 | tp = find_thread_ptid (inferior_ptid); |
| 3204 | if (tp == NULL) |
| 3205 | error (_("No thread.")); |
| 3206 | |
| 3207 | btinfo = &tp->btrace; |
| 3208 | |
| 3209 | /* Must clear the maint data before - it depends on BTINFO->DATA. */ |
| 3210 | btrace_maint_clear (btinfo); |
| 3211 | btrace_data_clear (&btinfo->data); |
| 3212 | } |
| 3213 | |
| 3214 | /* The "maintenance btrace clear" command. */ |
| 3215 | |
| 3216 | static void |
| 3217 | maint_btrace_clear_cmd (char *args, int from_tty) |
| 3218 | { |
| 3219 | struct btrace_thread_info *btinfo; |
| 3220 | struct thread_info *tp; |
| 3221 | |
| 3222 | if (args != NULL && *args != 0) |
| 3223 | error (_("Invalid argument.")); |
| 3224 | |
| 3225 | tp = find_thread_ptid (inferior_ptid); |
| 3226 | if (tp == NULL) |
| 3227 | error (_("No thread.")); |
| 3228 | |
| 3229 | btrace_clear (tp); |
| 3230 | } |
| 3231 | |
| 3232 | /* The "maintenance btrace" command. */ |
| 3233 | |
| 3234 | static void |
| 3235 | maint_btrace_cmd (char *args, int from_tty) |
| 3236 | { |
| 3237 | help_list (maint_btrace_cmdlist, "maintenance btrace ", all_commands, |
| 3238 | gdb_stdout); |
| 3239 | } |
| 3240 | |
| 3241 | /* The "maintenance set btrace" command. */ |
| 3242 | |
| 3243 | static void |
| 3244 | maint_btrace_set_cmd (char *args, int from_tty) |
| 3245 | { |
| 3246 | help_list (maint_btrace_set_cmdlist, "maintenance set btrace ", all_commands, |
| 3247 | gdb_stdout); |
| 3248 | } |
| 3249 | |
| 3250 | /* The "maintenance show btrace" command. */ |
| 3251 | |
| 3252 | static void |
| 3253 | maint_btrace_show_cmd (char *args, int from_tty) |
| 3254 | { |
| 3255 | help_list (maint_btrace_show_cmdlist, "maintenance show btrace ", |
| 3256 | all_commands, gdb_stdout); |
| 3257 | } |
| 3258 | |
| 3259 | /* The "maintenance set btrace pt" command. */ |
| 3260 | |
| 3261 | static void |
| 3262 | maint_btrace_pt_set_cmd (char *args, int from_tty) |
| 3263 | { |
| 3264 | help_list (maint_btrace_pt_set_cmdlist, "maintenance set btrace pt ", |
| 3265 | all_commands, gdb_stdout); |
| 3266 | } |
| 3267 | |
| 3268 | /* The "maintenance show btrace pt" command. */ |
| 3269 | |
| 3270 | static void |
| 3271 | maint_btrace_pt_show_cmd (char *args, int from_tty) |
| 3272 | { |
| 3273 | help_list (maint_btrace_pt_show_cmdlist, "maintenance show btrace pt ", |
| 3274 | all_commands, gdb_stdout); |
| 3275 | } |
| 3276 | |
| 3277 | /* The "maintenance info btrace" command. */ |
| 3278 | |
| 3279 | static void |
| 3280 | maint_info_btrace_cmd (char *args, int from_tty) |
| 3281 | { |
| 3282 | struct btrace_thread_info *btinfo; |
| 3283 | struct thread_info *tp; |
| 3284 | const struct btrace_config *conf; |
| 3285 | |
| 3286 | if (args != NULL && *args != 0) |
| 3287 | error (_("Invalid argument.")); |
| 3288 | |
| 3289 | tp = find_thread_ptid (inferior_ptid); |
| 3290 | if (tp == NULL) |
| 3291 | error (_("No thread.")); |
| 3292 | |
| 3293 | btinfo = &tp->btrace; |
| 3294 | |
| 3295 | conf = btrace_conf (btinfo); |
| 3296 | if (conf == NULL) |
| 3297 | error (_("No btrace configuration.")); |
| 3298 | |
| 3299 | printf_unfiltered (_("Format: %s.\n"), |
| 3300 | btrace_format_string (conf->format)); |
| 3301 | |
| 3302 | switch (conf->format) |
| 3303 | { |
| 3304 | default: |
| 3305 | break; |
| 3306 | |
| 3307 | case BTRACE_FORMAT_BTS: |
| 3308 | printf_unfiltered (_("Number of packets: %u.\n"), |
| 3309 | VEC_length (btrace_block_s, |
| 3310 | btinfo->data.variant.bts.blocks)); |
| 3311 | break; |
| 3312 | |
| 3313 | #if defined (HAVE_LIBIPT) |
| 3314 | case BTRACE_FORMAT_PT: |
| 3315 | { |
| 3316 | struct pt_version version; |
| 3317 | |
| 3318 | version = pt_library_version (); |
| 3319 | printf_unfiltered (_("Version: %u.%u.%u%s.\n"), version.major, |
| 3320 | version.minor, version.build, |
| 3321 | version.ext != NULL ? version.ext : ""); |
| 3322 | |
| 3323 | btrace_maint_update_pt_packets (btinfo); |
| 3324 | printf_unfiltered (_("Number of packets: %u.\n"), |
| 3325 | VEC_length (btrace_pt_packet_s, |
| 3326 | btinfo->maint.variant.pt.packets)); |
| 3327 | } |
| 3328 | break; |
| 3329 | #endif /* defined (HAVE_LIBIPT) */ |
| 3330 | } |
| 3331 | } |
| 3332 | |
| 3333 | /* The "maint show btrace pt skip-pad" show value function. */ |
| 3334 | |
| 3335 | static void |
| 3336 | show_maint_btrace_pt_skip_pad (struct ui_file *file, int from_tty, |
| 3337 | struct cmd_list_element *c, |
| 3338 | const char *value) |
| 3339 | { |
| 3340 | fprintf_filtered (file, _("Skip PAD packets is %s.\n"), value); |
| 3341 | } |
| 3342 | |
| 3343 | |
| 3344 | /* Initialize btrace maintenance commands. */ |
| 3345 | |
| 3346 | void _initialize_btrace (void); |
| 3347 | void |
| 3348 | _initialize_btrace (void) |
| 3349 | { |
| 3350 | add_cmd ("btrace", class_maintenance, maint_info_btrace_cmd, |
| 3351 | _("Info about branch tracing data."), &maintenanceinfolist); |
| 3352 | |
| 3353 | add_prefix_cmd ("btrace", class_maintenance, maint_btrace_cmd, |
| 3354 | _("Branch tracing maintenance commands."), |
| 3355 | &maint_btrace_cmdlist, "maintenance btrace ", |
| 3356 | 0, &maintenancelist); |
| 3357 | |
| 3358 | add_prefix_cmd ("btrace", class_maintenance, maint_btrace_set_cmd, _("\ |
| 3359 | Set branch tracing specific variables."), |
| 3360 | &maint_btrace_set_cmdlist, "maintenance set btrace ", |
| 3361 | 0, &maintenance_set_cmdlist); |
| 3362 | |
| 3363 | add_prefix_cmd ("pt", class_maintenance, maint_btrace_pt_set_cmd, _("\ |
| 3364 | Set Intel Processor Trace specific variables."), |
| 3365 | &maint_btrace_pt_set_cmdlist, "maintenance set btrace pt ", |
| 3366 | 0, &maint_btrace_set_cmdlist); |
| 3367 | |
| 3368 | add_prefix_cmd ("btrace", class_maintenance, maint_btrace_show_cmd, _("\ |
| 3369 | Show branch tracing specific variables."), |
| 3370 | &maint_btrace_show_cmdlist, "maintenance show btrace ", |
| 3371 | 0, &maintenance_show_cmdlist); |
| 3372 | |
| 3373 | add_prefix_cmd ("pt", class_maintenance, maint_btrace_pt_show_cmd, _("\ |
| 3374 | Show Intel Processor Trace specific variables."), |
| 3375 | &maint_btrace_pt_show_cmdlist, "maintenance show btrace pt ", |
| 3376 | 0, &maint_btrace_show_cmdlist); |
| 3377 | |
| 3378 | add_setshow_boolean_cmd ("skip-pad", class_maintenance, |
| 3379 | &maint_btrace_pt_skip_pad, _("\ |
| 3380 | Set whether PAD packets should be skipped in the btrace packet history."), _("\ |
| 3381 | Show whether PAD packets should be skipped in the btrace packet history."),_("\ |
| 3382 | When enabled, PAD packets are ignored in the btrace packet history."), |
| 3383 | NULL, show_maint_btrace_pt_skip_pad, |
| 3384 | &maint_btrace_pt_set_cmdlist, |
| 3385 | &maint_btrace_pt_show_cmdlist); |
| 3386 | |
| 3387 | add_cmd ("packet-history", class_maintenance, maint_btrace_packet_history_cmd, |
| 3388 | _("Print the raw branch tracing data.\n\ |
| 3389 | With no argument, print ten more packets after the previous ten-line print.\n\ |
| 3390 | With '-' as argument print ten packets before a previous ten-line print.\n\ |
| 3391 | One argument specifies the starting packet of a ten-line print.\n\ |
| 3392 | Two arguments with comma between specify starting and ending packets to \ |
| 3393 | print.\n\ |
| 3394 | Preceded with '+'/'-' the second argument specifies the distance from the \ |
| 3395 | first.\n"), |
| 3396 | &maint_btrace_cmdlist); |
| 3397 | |
| 3398 | add_cmd ("clear-packet-history", class_maintenance, |
| 3399 | maint_btrace_clear_packet_history_cmd, |
| 3400 | _("Clears the branch tracing packet history.\n\ |
| 3401 | Discards the raw branch tracing data but not the execution history data.\n\ |
| 3402 | "), |
| 3403 | &maint_btrace_cmdlist); |
| 3404 | |
| 3405 | add_cmd ("clear", class_maintenance, maint_btrace_clear_cmd, |
| 3406 | _("Clears the branch tracing data.\n\ |
| 3407 | Discards the raw branch tracing data and the execution history data.\n\ |
| 3408 | The next 'record' command will fetch the branch tracing data anew.\n\ |
| 3409 | "), |
| 3410 | &maint_btrace_cmdlist); |
| 3411 | |
| 3412 | } |