1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Convert sample address to data type using DWARF debug info. 4 * 5 * Written by Namhyung Kim <namhyung@kernel.org> 6 */ 7 8 #include <stdio.h> 9 #include <stdlib.h> 10 #include <inttypes.h> 11 #include <linux/zalloc.h> 12 13 #include "annotate.h" 14 #include "annotate-data.h" 15 #include "debuginfo.h" 16 #include "debug.h" 17 #include "dso.h" 18 #include "dwarf-regs.h" 19 #include "evsel.h" 20 #include "evlist.h" 21 #include "map.h" 22 #include "map_symbol.h" 23 #include "sort.h" 24 #include "strbuf.h" 25 #include "symbol.h" 26 #include "symbol_conf.h" 27 #include "thread.h" 28 29 /* register number of the stack pointer */ 30 #define X86_REG_SP 7 31 32 static void delete_var_types(struct die_var_type *var_types); 33 34 #define pr_debug_dtp(fmt, ...) \ 35 do { \ 36 if (debug_type_profile) \ 37 pr_info(fmt, ##__VA_ARGS__); \ 38 else \ 39 pr_debug3(fmt, ##__VA_ARGS__); \ 40 } while (0) 41 42 void pr_debug_type_name(Dwarf_Die *die, enum type_state_kind kind) 43 { 44 struct strbuf sb; 45 char *str; 46 Dwarf_Word size = 0; 47 48 if (!debug_type_profile && verbose < 3) 49 return; 50 51 switch (kind) { 52 case TSR_KIND_INVALID: 53 pr_info("\n"); 54 return; 55 case TSR_KIND_PERCPU_BASE: 56 pr_info(" percpu base\n"); 57 return; 58 case TSR_KIND_CONST: 59 pr_info(" constant\n"); 60 return; 61 case TSR_KIND_POINTER: 62 pr_info(" pointer"); 63 /* it also prints the type info */ 64 break; 65 case TSR_KIND_CANARY: 66 pr_info(" stack canary\n"); 67 return; 68 case TSR_KIND_TYPE: 69 default: 70 break; 71 } 72 73 dwarf_aggregate_size(die, &size); 74 75 strbuf_init(&sb, 32); 76 die_get_typename_from_type(die, &sb); 77 str = strbuf_detach(&sb, NULL); 78 pr_info(" type='%s' size=%#lx (die:%#lx)\n", 79 str, (long)size, (long)dwarf_dieoffset(die)); 80 free(str); 81 } 82 83 static void pr_debug_location(Dwarf_Die *die, u64 pc, int reg) 84 { 85 ptrdiff_t off = 0; 86 Dwarf_Attribute attr; 87 Dwarf_Addr base, start, end; 88 Dwarf_Op *ops; 89 size_t nops; 90 91 if (!debug_type_profile && verbose < 3) 92 return; 93 94 if (dwarf_attr(die, DW_AT_location, &attr) == NULL) 95 return; 96 97 while ((off = dwarf_getlocations(&attr, off, &base, &start, &end, &ops, &nops)) > 0) { 98 if (reg != DWARF_REG_PC && end <= pc) 99 continue; 100 if (reg != DWARF_REG_PC && start > pc) 101 break; 102 103 pr_info(" variable location: "); 104 switch (ops->atom) { 105 case DW_OP_reg0 ...DW_OP_reg31: 106 pr_info("reg%d\n", ops->atom - DW_OP_reg0); 107 break; 108 case DW_OP_breg0 ...DW_OP_breg31: 109 pr_info("base=reg%d, offset=%#lx\n", 110 ops->atom - DW_OP_breg0, (long)ops->number); 111 break; 112 case DW_OP_regx: 113 pr_info("reg%ld\n", (long)ops->number); 114 break; 115 case DW_OP_bregx: 116 pr_info("base=reg%ld, offset=%#lx\n", 117 (long)ops->number, (long)ops->number2); 118 break; 119 case DW_OP_fbreg: 120 pr_info("use frame base, offset=%#lx\n", (long)ops->number); 121 break; 122 case DW_OP_addr: 123 pr_info("address=%#lx\n", (long)ops->number); 124 break; 125 default: 126 pr_info("unknown: code=%#x, number=%#lx\n", 127 ops->atom, (long)ops->number); 128 break; 129 } 130 break; 131 } 132 } 133 134 static void pr_debug_scope(Dwarf_Die *scope_die) 135 { 136 int tag; 137 138 if (!debug_type_profile && verbose < 3) 139 return; 140 141 pr_info("(die:%lx) ", (long)dwarf_dieoffset(scope_die)); 142 143 tag = dwarf_tag(scope_die); 144 if (tag == DW_TAG_subprogram) 145 pr_info("[function] %s\n", dwarf_diename(scope_die)); 146 else if (tag == DW_TAG_inlined_subroutine) 147 pr_info("[inlined] %s\n", dwarf_diename(scope_die)); 148 else if (tag == DW_TAG_lexical_block) 149 pr_info("[block]\n"); 150 else 151 pr_info("[unknown] tag=%x\n", tag); 152 } 153 154 bool has_reg_type(struct type_state *state, int reg) 155 { 156 return (unsigned)reg < ARRAY_SIZE(state->regs); 157 } 158 159 static void init_type_state(struct type_state *state, struct arch *arch) 160 { 161 memset(state, 0, sizeof(*state)); 162 INIT_LIST_HEAD(&state->stack_vars); 163 164 if (arch__is(arch, "x86")) { 165 state->regs[0].caller_saved = true; 166 state->regs[1].caller_saved = true; 167 state->regs[2].caller_saved = true; 168 state->regs[4].caller_saved = true; 169 state->regs[5].caller_saved = true; 170 state->regs[8].caller_saved = true; 171 state->regs[9].caller_saved = true; 172 state->regs[10].caller_saved = true; 173 state->regs[11].caller_saved = true; 174 state->ret_reg = 0; 175 state->stack_reg = X86_REG_SP; 176 } 177 } 178 179 static void exit_type_state(struct type_state *state) 180 { 181 struct type_state_stack *stack, *tmp; 182 183 list_for_each_entry_safe(stack, tmp, &state->stack_vars, list) { 184 list_del(&stack->list); 185 free(stack); 186 } 187 } 188 189 /* 190 * Compare type name and size to maintain them in a tree. 191 * I'm not sure if DWARF would have information of a single type in many 192 * different places (compilation units). If not, it could compare the 193 * offset of the type entry in the .debug_info section. 194 */ 195 static int data_type_cmp(const void *_key, const struct rb_node *node) 196 { 197 const struct annotated_data_type *key = _key; 198 struct annotated_data_type *type; 199 200 type = rb_entry(node, struct annotated_data_type, node); 201 202 if (key->self.size != type->self.size) 203 return key->self.size - type->self.size; 204 return strcmp(key->self.type_name, type->self.type_name); 205 } 206 207 static bool data_type_less(struct rb_node *node_a, const struct rb_node *node_b) 208 { 209 struct annotated_data_type *a, *b; 210 211 a = rb_entry(node_a, struct annotated_data_type, node); 212 b = rb_entry(node_b, struct annotated_data_type, node); 213 214 if (a->self.size != b->self.size) 215 return a->self.size < b->self.size; 216 return strcmp(a->self.type_name, b->self.type_name) < 0; 217 } 218 219 /* Recursively add new members for struct/union */ 220 static int __add_member_cb(Dwarf_Die *die, void *arg) 221 { 222 struct annotated_member *parent = arg; 223 struct annotated_member *member; 224 Dwarf_Die member_type, die_mem; 225 Dwarf_Word size, loc, bit_size = 0; 226 Dwarf_Attribute attr; 227 struct strbuf sb; 228 int tag; 229 230 if (dwarf_tag(die) != DW_TAG_member) 231 return DIE_FIND_CB_SIBLING; 232 233 member = zalloc(sizeof(*member)); 234 if (member == NULL) 235 return DIE_FIND_CB_END; 236 237 strbuf_init(&sb, 32); 238 die_get_typename(die, &sb); 239 240 __die_get_real_type(die, &member_type); 241 if (dwarf_tag(&member_type) == DW_TAG_typedef) 242 die_get_real_type(&member_type, &die_mem); 243 else 244 die_mem = member_type; 245 246 if (dwarf_aggregate_size(&die_mem, &size) < 0) 247 size = 0; 248 249 if (dwarf_attr_integrate(die, DW_AT_data_member_location, &attr)) 250 dwarf_formudata(&attr, &loc); 251 else { 252 /* bitfield member */ 253 if (dwarf_attr_integrate(die, DW_AT_data_bit_offset, &attr) && 254 dwarf_formudata(&attr, &loc) == 0) 255 loc /= 8; 256 else 257 loc = 0; 258 259 if (dwarf_attr_integrate(die, DW_AT_bit_size, &attr) && 260 dwarf_formudata(&attr, &bit_size) == 0) 261 size = (bit_size + 7) / 8; 262 } 263 264 member->type_name = strbuf_detach(&sb, NULL); 265 /* member->var_name can be NULL */ 266 if (dwarf_diename(die)) { 267 if (bit_size) { 268 if (asprintf(&member->var_name, "%s:%ld", 269 dwarf_diename(die), (long)bit_size) < 0) 270 member->var_name = NULL; 271 } else { 272 member->var_name = strdup(dwarf_diename(die)); 273 } 274 275 if (member->var_name == NULL) { 276 free(member); 277 return DIE_FIND_CB_END; 278 } 279 } 280 member->size = size; 281 member->offset = loc + parent->offset; 282 INIT_LIST_HEAD(&member->children); 283 list_add_tail(&member->node, &parent->children); 284 285 tag = dwarf_tag(&die_mem); 286 switch (tag) { 287 case DW_TAG_structure_type: 288 case DW_TAG_union_type: 289 die_find_child(&die_mem, __add_member_cb, member, &die_mem); 290 break; 291 default: 292 break; 293 } 294 return DIE_FIND_CB_SIBLING; 295 } 296 297 static void add_member_types(struct annotated_data_type *parent, Dwarf_Die *type) 298 { 299 Dwarf_Die die_mem; 300 301 die_find_child(type, __add_member_cb, &parent->self, &die_mem); 302 } 303 304 static void delete_members(struct annotated_member *member) 305 { 306 struct annotated_member *child, *tmp; 307 308 list_for_each_entry_safe(child, tmp, &member->children, node) { 309 list_del(&child->node); 310 delete_members(child); 311 zfree(&child->type_name); 312 zfree(&child->var_name); 313 free(child); 314 } 315 } 316 317 static int fill_member_name(char *buf, size_t sz, struct annotated_member *m, 318 int offset, bool first) 319 { 320 struct annotated_member *child; 321 322 if (list_empty(&m->children)) 323 return 0; 324 325 list_for_each_entry(child, &m->children, node) { 326 int len; 327 328 if (offset < child->offset || offset >= child->offset + child->size) 329 continue; 330 331 /* It can have anonymous struct/union members */ 332 if (child->var_name) { 333 len = scnprintf(buf, sz, "%s%s", 334 first ? "" : ".", child->var_name); 335 first = false; 336 } else { 337 len = 0; 338 } 339 340 return fill_member_name(buf + len, sz - len, child, offset, first) + len; 341 } 342 return 0; 343 } 344 345 int annotated_data_type__get_member_name(struct annotated_data_type *adt, 346 char *buf, size_t sz, int member_offset) 347 { 348 return fill_member_name(buf, sz, &adt->self, member_offset, /*first=*/true); 349 } 350 351 static struct annotated_data_type *dso__findnew_data_type(struct dso *dso, 352 Dwarf_Die *type_die) 353 { 354 struct annotated_data_type *result = NULL; 355 struct annotated_data_type key; 356 struct rb_node *node; 357 struct strbuf sb; 358 char *type_name; 359 Dwarf_Word size; 360 361 strbuf_init(&sb, 32); 362 if (die_get_typename_from_type(type_die, &sb) < 0) 363 strbuf_add(&sb, "(unknown type)", 14); 364 type_name = strbuf_detach(&sb, NULL); 365 366 if (dwarf_tag(type_die) == DW_TAG_typedef) 367 die_get_real_type(type_die, type_die); 368 369 dwarf_aggregate_size(type_die, &size); 370 371 /* Check existing nodes in dso->data_types tree */ 372 key.self.type_name = type_name; 373 key.self.size = size; 374 node = rb_find(&key, dso__data_types(dso), data_type_cmp); 375 if (node) { 376 result = rb_entry(node, struct annotated_data_type, node); 377 free(type_name); 378 return result; 379 } 380 381 /* If not, add a new one */ 382 result = zalloc(sizeof(*result)); 383 if (result == NULL) { 384 free(type_name); 385 return NULL; 386 } 387 388 result->self.type_name = type_name; 389 result->self.size = size; 390 INIT_LIST_HEAD(&result->self.children); 391 392 if (symbol_conf.annotate_data_member) 393 add_member_types(result, type_die); 394 395 rb_add(&result->node, dso__data_types(dso), data_type_less); 396 return result; 397 } 398 399 static bool find_cu_die(struct debuginfo *di, u64 pc, Dwarf_Die *cu_die) 400 { 401 Dwarf_Off off, next_off; 402 size_t header_size; 403 404 if (dwarf_addrdie(di->dbg, pc, cu_die) != NULL) 405 return cu_die; 406 407 /* 408 * There are some kernels don't have full aranges and contain only a few 409 * aranges entries. Fallback to iterate all CU entries in .debug_info 410 * in case it's missing. 411 */ 412 off = 0; 413 while (dwarf_nextcu(di->dbg, off, &next_off, &header_size, 414 NULL, NULL, NULL) == 0) { 415 if (dwarf_offdie(di->dbg, off + header_size, cu_die) && 416 dwarf_haspc(cu_die, pc)) 417 return true; 418 419 off = next_off; 420 } 421 return false; 422 } 423 424 enum type_match_result { 425 PERF_TMR_UNKNOWN = 0, 426 PERF_TMR_OK, 427 PERF_TMR_NO_TYPE, 428 PERF_TMR_NO_POINTER, 429 PERF_TMR_NO_SIZE, 430 PERF_TMR_BAD_OFFSET, 431 PERF_TMR_BAIL_OUT, 432 }; 433 434 static const char *match_result_str(enum type_match_result tmr) 435 { 436 switch (tmr) { 437 case PERF_TMR_OK: 438 return "Good!"; 439 case PERF_TMR_NO_TYPE: 440 return "no type information"; 441 case PERF_TMR_NO_POINTER: 442 return "no/void pointer"; 443 case PERF_TMR_NO_SIZE: 444 return "type size is unknown"; 445 case PERF_TMR_BAD_OFFSET: 446 return "offset bigger than size"; 447 case PERF_TMR_UNKNOWN: 448 case PERF_TMR_BAIL_OUT: 449 default: 450 return "invalid state"; 451 } 452 } 453 454 static bool is_pointer_type(Dwarf_Die *type_die) 455 { 456 int tag = dwarf_tag(type_die); 457 458 return tag == DW_TAG_pointer_type || tag == DW_TAG_array_type; 459 } 460 461 static bool is_compound_type(Dwarf_Die *type_die) 462 { 463 int tag = dwarf_tag(type_die); 464 465 return tag == DW_TAG_structure_type || tag == DW_TAG_union_type; 466 } 467 468 /* returns if Type B has better information than Type A */ 469 static bool is_better_type(Dwarf_Die *type_a, Dwarf_Die *type_b) 470 { 471 Dwarf_Word size_a, size_b; 472 Dwarf_Die die_a, die_b; 473 474 /* pointer type is preferred */ 475 if (is_pointer_type(type_a) != is_pointer_type(type_b)) 476 return is_pointer_type(type_b); 477 478 if (is_pointer_type(type_b)) { 479 /* 480 * We want to compare the target type, but 'void *' can fail to 481 * get the target type. 482 */ 483 if (die_get_real_type(type_a, &die_a) == NULL) 484 return true; 485 if (die_get_real_type(type_b, &die_b) == NULL) 486 return false; 487 488 type_a = &die_a; 489 type_b = &die_b; 490 } 491 492 /* bigger type is preferred */ 493 if (dwarf_aggregate_size(type_a, &size_a) < 0 || 494 dwarf_aggregate_size(type_b, &size_b) < 0) 495 return false; 496 497 if (size_a != size_b) 498 return size_a < size_b; 499 500 /* struct or union is preferred */ 501 if (is_compound_type(type_a) != is_compound_type(type_b)) 502 return is_compound_type(type_b); 503 504 /* typedef is preferred */ 505 if (dwarf_tag(type_b) == DW_TAG_typedef) 506 return true; 507 508 return false; 509 } 510 511 /* The type info will be saved in @type_die */ 512 static enum type_match_result check_variable(struct data_loc_info *dloc, 513 Dwarf_Die *var_die, 514 Dwarf_Die *type_die, int reg, 515 int offset, bool is_fbreg) 516 { 517 Dwarf_Word size; 518 bool needs_pointer = true; 519 Dwarf_Die sized_type; 520 521 if (reg == DWARF_REG_PC) 522 needs_pointer = false; 523 else if (reg == dloc->fbreg || is_fbreg) 524 needs_pointer = false; 525 else if (arch__is(dloc->arch, "x86") && reg == X86_REG_SP) 526 needs_pointer = false; 527 528 /* Get the type of the variable */ 529 if (__die_get_real_type(var_die, type_die) == NULL) 530 return PERF_TMR_NO_TYPE; 531 532 /* 533 * Usually it expects a pointer type for a memory access. 534 * Convert to a real type it points to. But global variables 535 * and local variables are accessed directly without a pointer. 536 */ 537 if (needs_pointer) { 538 if (!is_pointer_type(type_die) || 539 __die_get_real_type(type_die, type_die) == NULL) 540 return PERF_TMR_NO_POINTER; 541 } 542 543 if (dwarf_tag(type_die) == DW_TAG_typedef) 544 die_get_real_type(type_die, &sized_type); 545 else 546 sized_type = *type_die; 547 548 /* Get the size of the actual type */ 549 if (dwarf_aggregate_size(&sized_type, &size) < 0) 550 return PERF_TMR_NO_SIZE; 551 552 /* Minimal sanity check */ 553 if ((unsigned)offset >= size) 554 return PERF_TMR_BAD_OFFSET; 555 556 return PERF_TMR_OK; 557 } 558 559 struct type_state_stack *find_stack_state(struct type_state *state, 560 int offset) 561 { 562 struct type_state_stack *stack; 563 564 list_for_each_entry(stack, &state->stack_vars, list) { 565 if (offset == stack->offset) 566 return stack; 567 568 if (stack->compound && stack->offset < offset && 569 offset < stack->offset + stack->size) 570 return stack; 571 } 572 return NULL; 573 } 574 575 void set_stack_state(struct type_state_stack *stack, int offset, u8 kind, 576 Dwarf_Die *type_die) 577 { 578 int tag; 579 Dwarf_Word size; 580 581 if (dwarf_aggregate_size(type_die, &size) < 0) 582 size = 0; 583 584 tag = dwarf_tag(type_die); 585 586 stack->type = *type_die; 587 stack->size = size; 588 stack->offset = offset; 589 stack->kind = kind; 590 591 switch (tag) { 592 case DW_TAG_structure_type: 593 case DW_TAG_union_type: 594 stack->compound = (kind != TSR_KIND_POINTER); 595 break; 596 default: 597 stack->compound = false; 598 break; 599 } 600 } 601 602 struct type_state_stack *findnew_stack_state(struct type_state *state, 603 int offset, u8 kind, 604 Dwarf_Die *type_die) 605 { 606 struct type_state_stack *stack = find_stack_state(state, offset); 607 608 if (stack) { 609 set_stack_state(stack, offset, kind, type_die); 610 return stack; 611 } 612 613 stack = malloc(sizeof(*stack)); 614 if (stack) { 615 set_stack_state(stack, offset, kind, type_die); 616 list_add(&stack->list, &state->stack_vars); 617 } 618 return stack; 619 } 620 621 /* Maintain a cache for quick global variable lookup */ 622 struct global_var_entry { 623 struct rb_node node; 624 char *name; 625 u64 start; 626 u64 end; 627 u64 die_offset; 628 }; 629 630 static int global_var_cmp(const void *_key, const struct rb_node *node) 631 { 632 const u64 addr = (uintptr_t)_key; 633 struct global_var_entry *gvar; 634 635 gvar = rb_entry(node, struct global_var_entry, node); 636 637 if (gvar->start <= addr && addr < gvar->end) 638 return 0; 639 return gvar->start > addr ? -1 : 1; 640 } 641 642 static bool global_var_less(struct rb_node *node_a, const struct rb_node *node_b) 643 { 644 struct global_var_entry *gvar_a, *gvar_b; 645 646 gvar_a = rb_entry(node_a, struct global_var_entry, node); 647 gvar_b = rb_entry(node_b, struct global_var_entry, node); 648 649 return gvar_a->start < gvar_b->start; 650 } 651 652 static struct global_var_entry *global_var__find(struct data_loc_info *dloc, u64 addr) 653 { 654 struct dso *dso = map__dso(dloc->ms->map); 655 struct rb_node *node; 656 657 node = rb_find((void *)(uintptr_t)addr, dso__global_vars(dso), global_var_cmp); 658 if (node == NULL) 659 return NULL; 660 661 return rb_entry(node, struct global_var_entry, node); 662 } 663 664 static bool global_var__add(struct data_loc_info *dloc, u64 addr, 665 const char *name, Dwarf_Die *type_die) 666 { 667 struct dso *dso = map__dso(dloc->ms->map); 668 struct global_var_entry *gvar; 669 Dwarf_Word size; 670 671 if (dwarf_aggregate_size(type_die, &size) < 0) 672 return false; 673 674 gvar = malloc(sizeof(*gvar)); 675 if (gvar == NULL) 676 return false; 677 678 gvar->name = name ? strdup(name) : NULL; 679 if (name && gvar->name == NULL) { 680 free(gvar); 681 return false; 682 } 683 684 gvar->start = addr; 685 gvar->end = addr + size; 686 gvar->die_offset = dwarf_dieoffset(type_die); 687 688 rb_add(&gvar->node, dso__global_vars(dso), global_var_less); 689 return true; 690 } 691 692 void global_var_type__tree_delete(struct rb_root *root) 693 { 694 struct global_var_entry *gvar; 695 696 while (!RB_EMPTY_ROOT(root)) { 697 struct rb_node *node = rb_first(root); 698 699 rb_erase(node, root); 700 gvar = rb_entry(node, struct global_var_entry, node); 701 zfree(&gvar->name); 702 free(gvar); 703 } 704 } 705 706 bool get_global_var_info(struct data_loc_info *dloc, u64 addr, 707 const char **var_name, int *var_offset) 708 { 709 struct addr_location al; 710 struct symbol *sym; 711 u64 mem_addr; 712 713 /* Kernel symbols might be relocated */ 714 mem_addr = addr + map__reloc(dloc->ms->map); 715 716 addr_location__init(&al); 717 sym = thread__find_symbol_fb(dloc->thread, dloc->cpumode, 718 mem_addr, &al); 719 if (sym) { 720 *var_name = sym->name; 721 /* Calculate type offset from the start of variable */ 722 *var_offset = mem_addr - map__unmap_ip(al.map, sym->start); 723 } else { 724 *var_name = NULL; 725 } 726 addr_location__exit(&al); 727 if (*var_name == NULL) 728 return false; 729 730 return true; 731 } 732 733 static void global_var__collect(struct data_loc_info *dloc) 734 { 735 Dwarf *dwarf = dloc->di->dbg; 736 Dwarf_Off off, next_off; 737 Dwarf_Die cu_die, type_die; 738 size_t header_size; 739 740 /* Iterate all CU and collect global variables that have no location in a register. */ 741 off = 0; 742 while (dwarf_nextcu(dwarf, off, &next_off, &header_size, 743 NULL, NULL, NULL) == 0) { 744 struct die_var_type *var_types = NULL; 745 struct die_var_type *pos; 746 747 if (dwarf_offdie(dwarf, off + header_size, &cu_die) == NULL) { 748 off = next_off; 749 continue; 750 } 751 752 die_collect_global_vars(&cu_die, &var_types); 753 754 for (pos = var_types; pos; pos = pos->next) { 755 const char *var_name = NULL; 756 int var_offset = 0; 757 758 if (pos->reg != -1) 759 continue; 760 761 if (!dwarf_offdie(dwarf, pos->die_off, &type_die)) 762 continue; 763 764 if (!get_global_var_info(dloc, pos->addr, &var_name, 765 &var_offset)) 766 continue; 767 768 if (var_offset != 0) 769 continue; 770 771 global_var__add(dloc, pos->addr, var_name, &type_die); 772 } 773 774 delete_var_types(var_types); 775 776 off = next_off; 777 } 778 } 779 780 bool get_global_var_type(Dwarf_Die *cu_die, struct data_loc_info *dloc, 781 u64 ip, u64 var_addr, int *var_offset, 782 Dwarf_Die *type_die) 783 { 784 u64 pc; 785 int offset; 786 const char *var_name = NULL; 787 struct global_var_entry *gvar; 788 struct dso *dso = map__dso(dloc->ms->map); 789 Dwarf_Die var_die; 790 791 if (RB_EMPTY_ROOT(dso__global_vars(dso))) 792 global_var__collect(dloc); 793 794 gvar = global_var__find(dloc, var_addr); 795 if (gvar) { 796 if (!dwarf_offdie(dloc->di->dbg, gvar->die_offset, type_die)) 797 return false; 798 799 *var_offset = var_addr - gvar->start; 800 return true; 801 } 802 803 /* Try to get the variable by address first */ 804 if (die_find_variable_by_addr(cu_die, var_addr, &var_die, &offset) && 805 check_variable(dloc, &var_die, type_die, DWARF_REG_PC, offset, 806 /*is_fbreg=*/false) == PERF_TMR_OK) { 807 var_name = dwarf_diename(&var_die); 808 *var_offset = offset; 809 goto ok; 810 } 811 812 if (!get_global_var_info(dloc, var_addr, &var_name, var_offset)) 813 return false; 814 815 pc = map__rip_2objdump(dloc->ms->map, ip); 816 817 /* Try to get the name of global variable */ 818 if (die_find_variable_at(cu_die, var_name, pc, &var_die) && 819 check_variable(dloc, &var_die, type_die, DWARF_REG_PC, *var_offset, 820 /*is_fbreg=*/false) == PERF_TMR_OK) 821 goto ok; 822 823 return false; 824 825 ok: 826 /* The address should point to the start of the variable */ 827 global_var__add(dloc, var_addr - *var_offset, var_name, type_die); 828 return true; 829 } 830 831 static bool die_is_same(Dwarf_Die *die_a, Dwarf_Die *die_b) 832 { 833 return (die_a->cu == die_b->cu) && (die_a->addr == die_b->addr); 834 } 835 836 /** 837 * update_var_state - Update type state using given variables 838 * @state: type state table 839 * @dloc: data location info 840 * @addr: instruction address to match with variable 841 * @insn_offset: instruction offset (for debug) 842 * @var_types: list of variables with type info 843 * 844 * This function fills the @state table using @var_types info. Each variable 845 * is used only at the given location and updates an entry in the table. 846 */ 847 static void update_var_state(struct type_state *state, struct data_loc_info *dloc, 848 u64 addr, u64 insn_offset, struct die_var_type *var_types) 849 { 850 Dwarf_Die mem_die; 851 struct die_var_type *var; 852 int fbreg = dloc->fbreg; 853 int fb_offset = 0; 854 855 if (dloc->fb_cfa) { 856 if (die_get_cfa(dloc->di->dbg, addr, &fbreg, &fb_offset) < 0) 857 fbreg = -1; 858 } 859 860 for (var = var_types; var != NULL; var = var->next) { 861 if (var->addr != addr) 862 continue; 863 /* Get the type DIE using the offset */ 864 if (!dwarf_offdie(dloc->di->dbg, var->die_off, &mem_die)) 865 continue; 866 867 if (var->reg == DWARF_REG_FB || var->reg == fbreg || var->reg == state->stack_reg) { 868 int offset = var->offset; 869 struct type_state_stack *stack; 870 871 if (var->reg != DWARF_REG_FB) 872 offset -= fb_offset; 873 874 stack = find_stack_state(state, offset); 875 if (stack && stack->kind == TSR_KIND_TYPE && 876 !is_better_type(&stack->type, &mem_die)) 877 continue; 878 879 findnew_stack_state(state, offset, TSR_KIND_TYPE, 880 &mem_die); 881 882 if (var->reg == state->stack_reg) { 883 pr_debug_dtp("var [%"PRIx64"] %#x(reg%d)", 884 insn_offset, offset, state->stack_reg); 885 } else { 886 pr_debug_dtp("var [%"PRIx64"] -%#x(stack)", 887 insn_offset, -offset); 888 } 889 pr_debug_type_name(&mem_die, TSR_KIND_TYPE); 890 } else if (has_reg_type(state, var->reg) && var->offset == 0) { 891 struct type_state_reg *reg; 892 Dwarf_Die orig_type; 893 894 reg = &state->regs[var->reg]; 895 896 if (reg->ok && reg->kind == TSR_KIND_TYPE && 897 !is_better_type(®->type, &mem_die)) 898 continue; 899 900 orig_type = reg->type; 901 902 reg->type = mem_die; 903 reg->kind = TSR_KIND_TYPE; 904 reg->ok = true; 905 906 pr_debug_dtp("var [%"PRIx64"] reg%d", 907 insn_offset, var->reg); 908 pr_debug_type_name(&mem_die, TSR_KIND_TYPE); 909 910 /* 911 * If this register is directly copied from another and it gets a 912 * better type, also update the type of the source register. This 913 * is usually the case of container_of() macro with offset of 0. 914 */ 915 if (has_reg_type(state, reg->copied_from)) { 916 struct type_state_reg *copy_reg; 917 918 copy_reg = &state->regs[reg->copied_from]; 919 920 /* TODO: check if type is compatible or embedded */ 921 if (!copy_reg->ok || (copy_reg->kind != TSR_KIND_TYPE) || 922 !die_is_same(©_reg->type, &orig_type) || 923 !is_better_type(©_reg->type, &mem_die)) 924 continue; 925 926 copy_reg->type = mem_die; 927 928 pr_debug_dtp("var [%"PRIx64"] copyback reg%d", 929 insn_offset, reg->copied_from); 930 pr_debug_type_name(&mem_die, TSR_KIND_TYPE); 931 } 932 } 933 } 934 } 935 936 /** 937 * update_insn_state - Update type state for an instruction 938 * @state: type state table 939 * @dloc: data location info 940 * @cu_die: compile unit debug entry 941 * @dl: disasm line for the instruction 942 * 943 * This function updates the @state table for the target operand of the 944 * instruction at @dl if it transfers the type like MOV on x86. Since it 945 * tracks the type, it won't care about the values like in arithmetic 946 * instructions like ADD/SUB/MUL/DIV and INC/DEC. 947 * 948 * Note that ops->reg2 is only available when both mem_ref and multi_regs 949 * are true. 950 */ 951 static void update_insn_state(struct type_state *state, struct data_loc_info *dloc, 952 Dwarf_Die *cu_die, struct disasm_line *dl) 953 { 954 if (dloc->arch->update_insn_state) 955 dloc->arch->update_insn_state(state, dloc, cu_die, dl); 956 } 957 958 /* 959 * Prepend this_blocks (from the outer scope) to full_blocks, removing 960 * duplicate disasm line. 961 */ 962 static void prepend_basic_blocks(struct list_head *this_blocks, 963 struct list_head *full_blocks) 964 { 965 struct annotated_basic_block *first_bb, *last_bb; 966 967 last_bb = list_last_entry(this_blocks, typeof(*last_bb), list); 968 first_bb = list_first_entry(full_blocks, typeof(*first_bb), list); 969 970 if (list_empty(full_blocks)) 971 goto out; 972 973 /* Last insn in this_blocks should be same as first insn in full_blocks */ 974 if (last_bb->end != first_bb->begin) { 975 pr_debug("prepend basic blocks: mismatched disasm line %"PRIx64" -> %"PRIx64"\n", 976 last_bb->end->al.offset, first_bb->begin->al.offset); 977 goto out; 978 } 979 980 /* Is the basic block have only one disasm_line? */ 981 if (last_bb->begin == last_bb->end) { 982 list_del(&last_bb->list); 983 free(last_bb); 984 goto out; 985 } 986 987 /* Point to the insn before the last when adding this block to full_blocks */ 988 last_bb->end = list_prev_entry(last_bb->end, al.node); 989 990 out: 991 list_splice(this_blocks, full_blocks); 992 } 993 994 static void delete_basic_blocks(struct list_head *basic_blocks) 995 { 996 struct annotated_basic_block *bb, *tmp; 997 998 list_for_each_entry_safe(bb, tmp, basic_blocks, list) { 999 list_del(&bb->list); 1000 free(bb); 1001 } 1002 } 1003 1004 /* Make sure all variables have a valid start address */ 1005 static void fixup_var_address(struct die_var_type *var_types, u64 addr) 1006 { 1007 while (var_types) { 1008 /* 1009 * Some variables have no address range meaning it's always 1010 * available in the whole scope. Let's adjust the start 1011 * address to the start of the scope. 1012 */ 1013 if (var_types->addr == 0) 1014 var_types->addr = addr; 1015 1016 var_types = var_types->next; 1017 } 1018 } 1019 1020 static void delete_var_types(struct die_var_type *var_types) 1021 { 1022 while (var_types) { 1023 struct die_var_type *next = var_types->next; 1024 1025 free(var_types); 1026 var_types = next; 1027 } 1028 } 1029 1030 /* should match to is_stack_canary() in util/annotate.c */ 1031 static void setup_stack_canary(struct data_loc_info *dloc) 1032 { 1033 if (arch__is(dloc->arch, "x86")) { 1034 dloc->op->segment = INSN_SEG_X86_GS; 1035 dloc->op->imm = true; 1036 dloc->op->offset = 40; 1037 } 1038 } 1039 1040 /* 1041 * It's at the target address, check if it has a matching type. 1042 * It returns PERF_TMR_BAIL_OUT when it looks up per-cpu variables which 1043 * are similar to global variables and no additional info is needed. 1044 */ 1045 static enum type_match_result check_matching_type(struct type_state *state, 1046 struct data_loc_info *dloc, 1047 Dwarf_Die *cu_die, 1048 struct disasm_line *dl, 1049 Dwarf_Die *type_die) 1050 { 1051 Dwarf_Word size; 1052 u32 insn_offset = dl->al.offset; 1053 int reg = dloc->op->reg1; 1054 int offset = dloc->op->offset; 1055 const char *offset_sign = ""; 1056 bool retry = true; 1057 1058 if (offset < 0) { 1059 offset = -offset; 1060 offset_sign = "-"; 1061 } 1062 1063 again: 1064 pr_debug_dtp("chk [%x] reg%d offset=%s%#x ok=%d kind=%d ", 1065 insn_offset, reg, offset_sign, offset, 1066 state->regs[reg].ok, state->regs[reg].kind); 1067 1068 if (!state->regs[reg].ok) 1069 goto check_non_register; 1070 1071 if (state->regs[reg].kind == TSR_KIND_TYPE) { 1072 Dwarf_Die sized_type; 1073 struct strbuf sb; 1074 1075 strbuf_init(&sb, 32); 1076 die_get_typename_from_type(&state->regs[reg].type, &sb); 1077 pr_debug_dtp("(%s)", sb.buf); 1078 strbuf_release(&sb); 1079 1080 /* 1081 * Normal registers should hold a pointer (or array) to 1082 * dereference a memory location. 1083 */ 1084 if (!is_pointer_type(&state->regs[reg].type)) { 1085 if (dloc->op->offset < 0 && reg != state->stack_reg) 1086 goto check_kernel; 1087 1088 return PERF_TMR_NO_POINTER; 1089 } 1090 1091 /* Remove the pointer and get the target type */ 1092 if (__die_get_real_type(&state->regs[reg].type, type_die) == NULL) 1093 return PERF_TMR_NO_POINTER; 1094 1095 dloc->type_offset = dloc->op->offset; 1096 1097 if (dwarf_tag(type_die) == DW_TAG_typedef) 1098 die_get_real_type(type_die, &sized_type); 1099 else 1100 sized_type = *type_die; 1101 1102 /* Get the size of the actual type */ 1103 if (dwarf_aggregate_size(&sized_type, &size) < 0 || 1104 (unsigned)dloc->type_offset >= size) 1105 return PERF_TMR_BAD_OFFSET; 1106 1107 return PERF_TMR_OK; 1108 } 1109 1110 if (state->regs[reg].kind == TSR_KIND_POINTER) { 1111 pr_debug_dtp("percpu ptr"); 1112 1113 /* 1114 * It's actaully pointer but the address was calculated using 1115 * some arithmetic. So it points to the actual type already. 1116 */ 1117 *type_die = state->regs[reg].type; 1118 1119 dloc->type_offset = dloc->op->offset; 1120 1121 /* Get the size of the actual type */ 1122 if (dwarf_aggregate_size(type_die, &size) < 0 || 1123 (unsigned)dloc->type_offset >= size) 1124 return PERF_TMR_BAIL_OUT; 1125 1126 return PERF_TMR_OK; 1127 } 1128 1129 if (state->regs[reg].kind == TSR_KIND_CANARY) { 1130 pr_debug_dtp("stack canary"); 1131 1132 /* 1133 * This is a saved value of the stack canary which will be handled 1134 * in the outer logic when it returns failure here. Pretend it's 1135 * from the stack canary directly. 1136 */ 1137 setup_stack_canary(dloc); 1138 1139 return PERF_TMR_BAIL_OUT; 1140 } 1141 1142 if (state->regs[reg].kind == TSR_KIND_PERCPU_BASE) { 1143 u64 var_addr = dloc->op->offset; 1144 int var_offset; 1145 1146 pr_debug_dtp("percpu var"); 1147 1148 if (dloc->op->multi_regs) { 1149 int reg2 = dloc->op->reg2; 1150 1151 if (dloc->op->reg2 == reg) 1152 reg2 = dloc->op->reg1; 1153 1154 if (has_reg_type(state, reg2) && state->regs[reg2].ok && 1155 state->regs[reg2].kind == TSR_KIND_CONST) 1156 var_addr += state->regs[reg2].imm_value; 1157 } 1158 1159 if (get_global_var_type(cu_die, dloc, dloc->ip, var_addr, 1160 &var_offset, type_die)) { 1161 dloc->type_offset = var_offset; 1162 return PERF_TMR_OK; 1163 } 1164 /* No need to retry per-cpu (global) variables */ 1165 return PERF_TMR_BAIL_OUT; 1166 } 1167 1168 check_non_register: 1169 if (reg == dloc->fbreg || reg == state->stack_reg) { 1170 struct type_state_stack *stack; 1171 1172 pr_debug_dtp("%s", reg == dloc->fbreg ? "fbreg" : "stack"); 1173 1174 stack = find_stack_state(state, dloc->type_offset); 1175 if (stack == NULL) { 1176 if (retry) { 1177 pr_debug_dtp(" : retry\n"); 1178 retry = false; 1179 1180 /* update type info it's the first store to the stack */ 1181 update_insn_state(state, dloc, cu_die, dl); 1182 goto again; 1183 } 1184 return PERF_TMR_NO_TYPE; 1185 } 1186 1187 if (stack->kind == TSR_KIND_CANARY) { 1188 setup_stack_canary(dloc); 1189 return PERF_TMR_BAIL_OUT; 1190 } 1191 1192 if (stack->kind != TSR_KIND_TYPE) 1193 return PERF_TMR_NO_TYPE; 1194 1195 *type_die = stack->type; 1196 /* Update the type offset from the start of slot */ 1197 dloc->type_offset -= stack->offset; 1198 1199 return PERF_TMR_OK; 1200 } 1201 1202 if (dloc->fb_cfa) { 1203 struct type_state_stack *stack; 1204 u64 pc = map__rip_2objdump(dloc->ms->map, dloc->ip); 1205 int fbreg, fboff; 1206 1207 pr_debug_dtp("cfa"); 1208 1209 if (die_get_cfa(dloc->di->dbg, pc, &fbreg, &fboff) < 0) 1210 fbreg = -1; 1211 1212 if (reg != fbreg) 1213 return PERF_TMR_NO_TYPE; 1214 1215 stack = find_stack_state(state, dloc->type_offset - fboff); 1216 if (stack == NULL) { 1217 if (retry) { 1218 pr_debug_dtp(" : retry\n"); 1219 retry = false; 1220 1221 /* update type info it's the first store to the stack */ 1222 update_insn_state(state, dloc, cu_die, dl); 1223 goto again; 1224 } 1225 return PERF_TMR_NO_TYPE; 1226 } 1227 1228 if (stack->kind == TSR_KIND_CANARY) { 1229 setup_stack_canary(dloc); 1230 return PERF_TMR_BAIL_OUT; 1231 } 1232 1233 if (stack->kind != TSR_KIND_TYPE) 1234 return PERF_TMR_NO_TYPE; 1235 1236 *type_die = stack->type; 1237 /* Update the type offset from the start of slot */ 1238 dloc->type_offset -= fboff + stack->offset; 1239 1240 return PERF_TMR_OK; 1241 } 1242 1243 check_kernel: 1244 if (dso__kernel(map__dso(dloc->ms->map))) { 1245 u64 addr; 1246 1247 /* Direct this-cpu access like "%gs:0x34740" */ 1248 if (dloc->op->segment == INSN_SEG_X86_GS && dloc->op->imm && 1249 arch__is(dloc->arch, "x86")) { 1250 pr_debug_dtp("this-cpu var"); 1251 1252 addr = dloc->op->offset; 1253 1254 if (get_global_var_type(cu_die, dloc, dloc->ip, addr, 1255 &offset, type_die)) { 1256 dloc->type_offset = offset; 1257 return PERF_TMR_OK; 1258 } 1259 return PERF_TMR_BAIL_OUT; 1260 } 1261 1262 /* Access to global variable like "-0x7dcf0500(,%rdx,8)" */ 1263 if (dloc->op->offset < 0 && reg != state->stack_reg) { 1264 addr = (s64) dloc->op->offset; 1265 1266 if (get_global_var_type(cu_die, dloc, dloc->ip, addr, 1267 &offset, type_die)) { 1268 pr_debug_dtp("global var"); 1269 1270 dloc->type_offset = offset; 1271 return PERF_TMR_OK; 1272 } 1273 return PERF_TMR_BAIL_OUT; 1274 } 1275 } 1276 1277 return PERF_TMR_UNKNOWN; 1278 } 1279 1280 /* Iterate instructions in basic blocks and update type table */ 1281 static enum type_match_result find_data_type_insn(struct data_loc_info *dloc, 1282 struct list_head *basic_blocks, 1283 struct die_var_type *var_types, 1284 Dwarf_Die *cu_die, 1285 Dwarf_Die *type_die) 1286 { 1287 struct type_state state; 1288 struct symbol *sym = dloc->ms->sym; 1289 struct annotation *notes = symbol__annotation(sym); 1290 struct annotated_basic_block *bb; 1291 enum type_match_result ret = PERF_TMR_UNKNOWN; 1292 1293 init_type_state(&state, dloc->arch); 1294 1295 list_for_each_entry(bb, basic_blocks, list) { 1296 struct disasm_line *dl = bb->begin; 1297 1298 BUG_ON(bb->begin->al.offset == -1 || bb->end->al.offset == -1); 1299 1300 pr_debug_dtp("bb: [%"PRIx64" - %"PRIx64"]\n", 1301 bb->begin->al.offset, bb->end->al.offset); 1302 1303 list_for_each_entry_from(dl, ¬es->src->source, al.node) { 1304 u64 this_ip = sym->start + dl->al.offset; 1305 u64 addr = map__rip_2objdump(dloc->ms->map, this_ip); 1306 1307 /* Skip comment or debug info lines */ 1308 if (dl->al.offset == -1) 1309 continue; 1310 1311 /* Update variable type at this address */ 1312 update_var_state(&state, dloc, addr, dl->al.offset, var_types); 1313 1314 if (this_ip == dloc->ip) { 1315 ret = check_matching_type(&state, dloc, 1316 cu_die, dl, type_die); 1317 pr_debug_dtp(" : %s\n", match_result_str(ret)); 1318 goto out; 1319 } 1320 1321 /* Update type table after processing the instruction */ 1322 update_insn_state(&state, dloc, cu_die, dl); 1323 if (dl == bb->end) 1324 break; 1325 } 1326 } 1327 1328 out: 1329 exit_type_state(&state); 1330 return ret; 1331 } 1332 1333 static int arch_supports_insn_tracking(struct data_loc_info *dloc) 1334 { 1335 if ((arch__is(dloc->arch, "x86")) || (arch__is(dloc->arch, "powerpc"))) 1336 return 1; 1337 return 0; 1338 } 1339 1340 /* 1341 * Construct a list of basic blocks for each scope with variables and try to find 1342 * the data type by updating a type state table through instructions. 1343 */ 1344 static enum type_match_result find_data_type_block(struct data_loc_info *dloc, 1345 Dwarf_Die *cu_die, 1346 Dwarf_Die *scopes, 1347 int nr_scopes, 1348 Dwarf_Die *type_die) 1349 { 1350 LIST_HEAD(basic_blocks); 1351 struct die_var_type *var_types = NULL; 1352 u64 src_ip, dst_ip, prev_dst_ip; 1353 enum type_match_result ret = PERF_TMR_UNKNOWN; 1354 1355 /* TODO: other architecture support */ 1356 if (!arch_supports_insn_tracking(dloc)) 1357 return PERF_TMR_BAIL_OUT; 1358 1359 prev_dst_ip = dst_ip = dloc->ip; 1360 for (int i = nr_scopes - 1; i >= 0; i--) { 1361 Dwarf_Addr base, start, end; 1362 LIST_HEAD(this_blocks); 1363 1364 if (dwarf_ranges(&scopes[i], 0, &base, &start, &end) < 0) 1365 break; 1366 1367 pr_debug_dtp("scope: [%d/%d] ", i + 1, nr_scopes); 1368 pr_debug_scope(&scopes[i]); 1369 1370 src_ip = map__objdump_2rip(dloc->ms->map, start); 1371 1372 again: 1373 /* Get basic blocks for this scope */ 1374 if (annotate_get_basic_blocks(dloc->ms->sym, src_ip, dst_ip, 1375 &this_blocks) < 0) { 1376 /* Try previous block if they are not connected */ 1377 if (prev_dst_ip != dst_ip) { 1378 dst_ip = prev_dst_ip; 1379 goto again; 1380 } 1381 1382 pr_debug_dtp("cannot find a basic block from %"PRIx64" to %"PRIx64"\n", 1383 src_ip - dloc->ms->sym->start, 1384 dst_ip - dloc->ms->sym->start); 1385 continue; 1386 } 1387 prepend_basic_blocks(&this_blocks, &basic_blocks); 1388 1389 /* Get variable info for this scope and add to var_types list */ 1390 die_collect_vars(&scopes[i], &var_types); 1391 fixup_var_address(var_types, start); 1392 1393 /* Find from start of this scope to the target instruction */ 1394 ret = find_data_type_insn(dloc, &basic_blocks, var_types, 1395 cu_die, type_die); 1396 if (ret == PERF_TMR_OK) { 1397 char buf[64]; 1398 int offset = dloc->op->offset; 1399 const char *offset_sign = ""; 1400 1401 if (offset < 0) { 1402 offset = -offset; 1403 offset_sign = "-"; 1404 } 1405 1406 if (dloc->op->multi_regs) 1407 snprintf(buf, sizeof(buf), "reg%d, reg%d", 1408 dloc->op->reg1, dloc->op->reg2); 1409 else 1410 snprintf(buf, sizeof(buf), "reg%d", dloc->op->reg1); 1411 1412 pr_debug_dtp("found by insn track: %s%#x(%s) type-offset=%#x\n", 1413 offset_sign, offset, buf, dloc->type_offset); 1414 break; 1415 } 1416 1417 if (ret == PERF_TMR_BAIL_OUT) 1418 break; 1419 1420 /* Go up to the next scope and find blocks to the start */ 1421 prev_dst_ip = dst_ip; 1422 dst_ip = src_ip; 1423 } 1424 1425 delete_basic_blocks(&basic_blocks); 1426 delete_var_types(var_types); 1427 return ret; 1428 } 1429 1430 /* The result will be saved in @type_die */ 1431 static int find_data_type_die(struct data_loc_info *dloc, Dwarf_Die *type_die) 1432 { 1433 struct annotated_op_loc *loc = dloc->op; 1434 Dwarf_Die cu_die, var_die; 1435 Dwarf_Die *scopes = NULL; 1436 int reg, offset = loc->offset; 1437 int ret = -1; 1438 int i, nr_scopes; 1439 int fbreg = -1; 1440 int fb_offset = 0; 1441 bool is_fbreg = false; 1442 bool found = false; 1443 u64 pc; 1444 char buf[64]; 1445 enum type_match_result result = PERF_TMR_UNKNOWN; 1446 const char *offset_sign = ""; 1447 1448 if (dloc->op->multi_regs) 1449 snprintf(buf, sizeof(buf), "reg%d, reg%d", dloc->op->reg1, dloc->op->reg2); 1450 else if (dloc->op->reg1 == DWARF_REG_PC) 1451 snprintf(buf, sizeof(buf), "PC"); 1452 else 1453 snprintf(buf, sizeof(buf), "reg%d", dloc->op->reg1); 1454 1455 if (offset < 0) { 1456 offset = -offset; 1457 offset_sign = "-"; 1458 } 1459 1460 pr_debug_dtp("-----------------------------------------------------------\n"); 1461 pr_debug_dtp("find data type for %s%#x(%s) at %s+%#"PRIx64"\n", 1462 offset_sign, offset, buf, 1463 dloc->ms->sym->name, dloc->ip - dloc->ms->sym->start); 1464 1465 /* 1466 * IP is a relative instruction address from the start of the map, as 1467 * it can be randomized/relocated, it needs to translate to PC which is 1468 * a file address for DWARF processing. 1469 */ 1470 pc = map__rip_2objdump(dloc->ms->map, dloc->ip); 1471 1472 /* Get a compile_unit for this address */ 1473 if (!find_cu_die(dloc->di, pc, &cu_die)) { 1474 pr_debug_dtp("cannot find CU for address %"PRIx64"\n", pc); 1475 ann_data_stat.no_cuinfo++; 1476 return -1; 1477 } 1478 1479 reg = loc->reg1; 1480 offset = loc->offset; 1481 1482 pr_debug_dtp("CU for %s (die:%#lx)\n", 1483 dwarf_diename(&cu_die), (long)dwarf_dieoffset(&cu_die)); 1484 1485 if (reg == DWARF_REG_PC) { 1486 if (get_global_var_type(&cu_die, dloc, dloc->ip, dloc->var_addr, 1487 &offset, type_die)) { 1488 dloc->type_offset = offset; 1489 1490 pr_debug_dtp("found by addr=%#"PRIx64" type_offset=%#x\n", 1491 dloc->var_addr, offset); 1492 pr_debug_type_name(type_die, TSR_KIND_TYPE); 1493 found = true; 1494 goto out; 1495 } 1496 } 1497 1498 /* Get a list of nested scopes - i.e. (inlined) functions and blocks. */ 1499 nr_scopes = die_get_scopes(&cu_die, pc, &scopes); 1500 1501 if (reg != DWARF_REG_PC && dwarf_hasattr(&scopes[0], DW_AT_frame_base)) { 1502 Dwarf_Attribute attr; 1503 Dwarf_Block block; 1504 1505 /* Check if the 'reg' is assigned as frame base register */ 1506 if (dwarf_attr(&scopes[0], DW_AT_frame_base, &attr) != NULL && 1507 dwarf_formblock(&attr, &block) == 0 && block.length == 1) { 1508 switch (*block.data) { 1509 case DW_OP_reg0 ... DW_OP_reg31: 1510 fbreg = dloc->fbreg = *block.data - DW_OP_reg0; 1511 break; 1512 case DW_OP_call_frame_cfa: 1513 dloc->fb_cfa = true; 1514 if (die_get_cfa(dloc->di->dbg, pc, &fbreg, 1515 &fb_offset) < 0) 1516 fbreg = -1; 1517 break; 1518 default: 1519 break; 1520 } 1521 1522 pr_debug_dtp("frame base: cfa=%d fbreg=%d\n", 1523 dloc->fb_cfa, fbreg); 1524 } 1525 } 1526 1527 retry: 1528 is_fbreg = (reg == fbreg); 1529 if (is_fbreg) 1530 offset = loc->offset - fb_offset; 1531 1532 /* Search from the inner-most scope to the outer */ 1533 for (i = nr_scopes - 1; i >= 0; i--) { 1534 Dwarf_Die mem_die; 1535 int type_offset = offset; 1536 1537 if (reg == DWARF_REG_PC) { 1538 if (!die_find_variable_by_addr(&scopes[i], dloc->var_addr, 1539 &var_die, &type_offset)) 1540 continue; 1541 } else { 1542 /* Look up variables/parameters in this scope */ 1543 if (!die_find_variable_by_reg(&scopes[i], pc, reg, 1544 &type_offset, is_fbreg, &var_die)) 1545 continue; 1546 } 1547 1548 pr_debug_dtp("found \"%s\" (die: %#lx) in scope=%d/%d (die: %#lx) ", 1549 dwarf_diename(&var_die), (long)dwarf_dieoffset(&var_die), 1550 i+1, nr_scopes, (long)dwarf_dieoffset(&scopes[i])); 1551 1552 /* Found a variable, see if it's correct */ 1553 result = check_variable(dloc, &var_die, &mem_die, reg, type_offset, is_fbreg); 1554 if (result == PERF_TMR_OK) { 1555 if (reg == DWARF_REG_PC) { 1556 pr_debug_dtp("addr=%#"PRIx64" type_offset=%#x\n", 1557 dloc->var_addr, type_offset); 1558 } else if (reg == DWARF_REG_FB || is_fbreg) { 1559 pr_debug_dtp("stack_offset=%#x type_offset=%#x\n", 1560 fb_offset, type_offset); 1561 } else { 1562 pr_debug_dtp("type_offset=%#x\n", type_offset); 1563 } 1564 1565 if (!found || is_better_type(type_die, &mem_die)) { 1566 *type_die = mem_die; 1567 dloc->type_offset = type_offset; 1568 found = true; 1569 } 1570 } else { 1571 pr_debug_dtp("failed: %s\n", match_result_str(result)); 1572 } 1573 1574 pr_debug_location(&var_die, pc, reg); 1575 pr_debug_type_name(&mem_die, TSR_KIND_TYPE); 1576 } 1577 1578 if (!found && loc->multi_regs && reg == loc->reg1 && loc->reg1 != loc->reg2) { 1579 reg = loc->reg2; 1580 goto retry; 1581 } 1582 1583 if (!found && reg != DWARF_REG_PC) { 1584 result = find_data_type_block(dloc, &cu_die, scopes, 1585 nr_scopes, type_die); 1586 if (result == PERF_TMR_OK) { 1587 ann_data_stat.insn_track++; 1588 found = true; 1589 } 1590 } 1591 1592 out: 1593 pr_debug_dtp("final result: "); 1594 if (found) { 1595 pr_debug_type_name(type_die, TSR_KIND_TYPE); 1596 ret = 0; 1597 } else { 1598 switch (result) { 1599 case PERF_TMR_NO_TYPE: 1600 case PERF_TMR_NO_POINTER: 1601 pr_debug_dtp("%s\n", match_result_str(result)); 1602 ann_data_stat.no_typeinfo++; 1603 break; 1604 case PERF_TMR_NO_SIZE: 1605 pr_debug_dtp("%s\n", match_result_str(result)); 1606 ann_data_stat.invalid_size++; 1607 break; 1608 case PERF_TMR_BAD_OFFSET: 1609 pr_debug_dtp("%s\n", match_result_str(result)); 1610 ann_data_stat.bad_offset++; 1611 break; 1612 case PERF_TMR_UNKNOWN: 1613 case PERF_TMR_BAIL_OUT: 1614 case PERF_TMR_OK: /* should not reach here */ 1615 default: 1616 pr_debug_dtp("no variable found\n"); 1617 ann_data_stat.no_var++; 1618 break; 1619 } 1620 ret = -1; 1621 } 1622 1623 free(scopes); 1624 return ret; 1625 } 1626 1627 /** 1628 * find_data_type - Return a data type at the location 1629 * @dloc: data location 1630 * 1631 * This functions searches the debug information of the binary to get the data 1632 * type it accesses. The exact location is expressed by (ip, reg, offset) 1633 * for pointer variables or (ip, addr) for global variables. Note that global 1634 * variables might update the @dloc->type_offset after finding the start of the 1635 * variable. If it cannot find a global variable by address, it tried to find 1636 * a declaration of the variable using var_name. In that case, @dloc->offset 1637 * won't be updated. 1638 * 1639 * It return %NULL if not found. 1640 */ 1641 struct annotated_data_type *find_data_type(struct data_loc_info *dloc) 1642 { 1643 struct dso *dso = map__dso(dloc->ms->map); 1644 Dwarf_Die type_die; 1645 1646 /* 1647 * The type offset is the same as instruction offset by default. 1648 * But when finding a global variable, the offset won't be valid. 1649 */ 1650 dloc->type_offset = dloc->op->offset; 1651 1652 dloc->fbreg = -1; 1653 1654 if (find_data_type_die(dloc, &type_die) < 0) 1655 return NULL; 1656 1657 return dso__findnew_data_type(dso, &type_die); 1658 } 1659 1660 static int alloc_data_type_histograms(struct annotated_data_type *adt, int nr_entries) 1661 { 1662 int i; 1663 size_t sz = sizeof(struct type_hist); 1664 1665 sz += sizeof(struct type_hist_entry) * adt->self.size; 1666 1667 /* Allocate a table of pointers for each event */ 1668 adt->histograms = calloc(nr_entries, sizeof(*adt->histograms)); 1669 if (adt->histograms == NULL) 1670 return -ENOMEM; 1671 1672 /* 1673 * Each histogram is allocated for the whole size of the type. 1674 * TODO: Probably we can move the histogram to members. 1675 */ 1676 for (i = 0; i < nr_entries; i++) { 1677 adt->histograms[i] = zalloc(sz); 1678 if (adt->histograms[i] == NULL) 1679 goto err; 1680 } 1681 1682 adt->nr_histograms = nr_entries; 1683 return 0; 1684 1685 err: 1686 while (--i >= 0) 1687 zfree(&(adt->histograms[i])); 1688 zfree(&adt->histograms); 1689 return -ENOMEM; 1690 } 1691 1692 static void delete_data_type_histograms(struct annotated_data_type *adt) 1693 { 1694 for (int i = 0; i < adt->nr_histograms; i++) 1695 zfree(&(adt->histograms[i])); 1696 1697 zfree(&adt->histograms); 1698 adt->nr_histograms = 0; 1699 } 1700 1701 void annotated_data_type__tree_delete(struct rb_root *root) 1702 { 1703 struct annotated_data_type *pos; 1704 1705 while (!RB_EMPTY_ROOT(root)) { 1706 struct rb_node *node = rb_first(root); 1707 1708 rb_erase(node, root); 1709 pos = rb_entry(node, struct annotated_data_type, node); 1710 delete_members(&pos->self); 1711 delete_data_type_histograms(pos); 1712 zfree(&pos->self.type_name); 1713 free(pos); 1714 } 1715 } 1716 1717 /** 1718 * annotated_data_type__update_samples - Update histogram 1719 * @adt: Data type to update 1720 * @evsel: Event to update 1721 * @offset: Offset in the type 1722 * @nr_samples: Number of samples at this offset 1723 * @period: Event count at this offset 1724 * 1725 * This function updates type histogram at @ofs for @evsel. Samples are 1726 * aggregated before calling this function so it can be called with more 1727 * than one samples at a certain offset. 1728 */ 1729 int annotated_data_type__update_samples(struct annotated_data_type *adt, 1730 struct evsel *evsel, int offset, 1731 int nr_samples, u64 period) 1732 { 1733 struct type_hist *h; 1734 1735 if (adt == NULL) 1736 return 0; 1737 1738 if (adt->histograms == NULL) { 1739 int nr = evsel->evlist->core.nr_entries; 1740 1741 if (alloc_data_type_histograms(adt, nr) < 0) 1742 return -1; 1743 } 1744 1745 if (offset < 0 || offset >= adt->self.size) 1746 return -1; 1747 1748 h = adt->histograms[evsel->core.idx]; 1749 1750 h->nr_samples += nr_samples; 1751 h->addr[offset].nr_samples += nr_samples; 1752 h->period += period; 1753 h->addr[offset].period += period; 1754 return 0; 1755 } 1756 1757 static void print_annotated_data_header(struct hist_entry *he, struct evsel *evsel) 1758 { 1759 struct dso *dso = map__dso(he->ms.map); 1760 int nr_members = 1; 1761 int nr_samples = he->stat.nr_events; 1762 int width = 7; 1763 const char *val_hdr = "Percent"; 1764 1765 if (evsel__is_group_event(evsel)) { 1766 struct hist_entry *pair; 1767 1768 list_for_each_entry(pair, &he->pairs.head, pairs.node) 1769 nr_samples += pair->stat.nr_events; 1770 } 1771 1772 printf("Annotate type: '%s' in %s (%d samples):\n", 1773 he->mem_type->self.type_name, dso__name(dso), nr_samples); 1774 1775 if (evsel__is_group_event(evsel)) { 1776 struct evsel *pos; 1777 int i = 0; 1778 1779 nr_members = 0; 1780 for_each_group_evsel(pos, evsel) { 1781 if (symbol_conf.skip_empty && 1782 evsel__hists(pos)->stats.nr_samples == 0) 1783 continue; 1784 1785 printf(" event[%d] = %s\n", i++, pos->name); 1786 nr_members++; 1787 } 1788 } 1789 1790 if (symbol_conf.show_total_period) { 1791 width = 11; 1792 val_hdr = "Period"; 1793 } else if (symbol_conf.show_nr_samples) { 1794 width = 7; 1795 val_hdr = "Samples"; 1796 } 1797 1798 printf("============================================================================\n"); 1799 printf("%*s %10s %10s %s\n", (width + 1) * nr_members, val_hdr, 1800 "offset", "size", "field"); 1801 } 1802 1803 static void print_annotated_data_value(struct type_hist *h, u64 period, int nr_samples) 1804 { 1805 double percent = h->period ? (100.0 * period / h->period) : 0; 1806 const char *color = get_percent_color(percent); 1807 1808 if (symbol_conf.show_total_period) 1809 color_fprintf(stdout, color, " %11" PRIu64, period); 1810 else if (symbol_conf.show_nr_samples) 1811 color_fprintf(stdout, color, " %7d", nr_samples); 1812 else 1813 color_fprintf(stdout, color, " %7.2f", percent); 1814 } 1815 1816 static void print_annotated_data_type(struct annotated_data_type *mem_type, 1817 struct annotated_member *member, 1818 struct evsel *evsel, int indent) 1819 { 1820 struct annotated_member *child; 1821 struct type_hist *h = mem_type->histograms[evsel->core.idx]; 1822 int i, nr_events = 0, samples = 0; 1823 u64 period = 0; 1824 int width = symbol_conf.show_total_period ? 11 : 7; 1825 struct evsel *pos; 1826 1827 for_each_group_evsel(pos, evsel) { 1828 h = mem_type->histograms[pos->core.idx]; 1829 1830 if (symbol_conf.skip_empty && 1831 evsel__hists(pos)->stats.nr_samples == 0) 1832 continue; 1833 1834 samples = 0; 1835 period = 0; 1836 for (i = 0; i < member->size; i++) { 1837 samples += h->addr[member->offset + i].nr_samples; 1838 period += h->addr[member->offset + i].period; 1839 } 1840 print_annotated_data_value(h, period, samples); 1841 nr_events++; 1842 } 1843 1844 printf(" %#10x %#10x %*s%s\t%s", 1845 member->offset, member->size, indent, "", member->type_name, 1846 member->var_name ?: ""); 1847 1848 if (!list_empty(&member->children)) 1849 printf(" {\n"); 1850 1851 list_for_each_entry(child, &member->children, node) 1852 print_annotated_data_type(mem_type, child, evsel, indent + 4); 1853 1854 if (!list_empty(&member->children)) 1855 printf("%*s}", (width + 1) * nr_events + 24 + indent, ""); 1856 printf(";\n"); 1857 } 1858 1859 int hist_entry__annotate_data_tty(struct hist_entry *he, struct evsel *evsel) 1860 { 1861 print_annotated_data_header(he, evsel); 1862 print_annotated_data_type(he->mem_type, &he->mem_type->self, evsel, 0); 1863 printf("\n"); 1864 1865 /* move to the next entry */ 1866 return '>'; 1867 } 1868