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_PERCPU_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_PERCPU_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 the reg location holds the pointer value, dereference the type */ 872 if (!var->is_reg_var_addr && is_pointer_type(&mem_die) && 873 __die_get_real_type(&mem_die, &mem_die) == NULL) 874 continue; 875 876 if (var->reg != DWARF_REG_FB) 877 offset -= fb_offset; 878 879 stack = find_stack_state(state, offset); 880 if (stack && stack->kind == TSR_KIND_TYPE && 881 !is_better_type(&stack->type, &mem_die)) 882 continue; 883 884 findnew_stack_state(state, offset, TSR_KIND_TYPE, 885 &mem_die); 886 887 if (var->reg == state->stack_reg) { 888 pr_debug_dtp("var [%"PRIx64"] %#x(reg%d)", 889 insn_offset, offset, state->stack_reg); 890 } else { 891 pr_debug_dtp("var [%"PRIx64"] -%#x(stack)", 892 insn_offset, -offset); 893 } 894 pr_debug_type_name(&mem_die, TSR_KIND_TYPE); 895 } else if (has_reg_type(state, var->reg) && var->offset == 0) { 896 struct type_state_reg *reg; 897 Dwarf_Die orig_type; 898 899 reg = &state->regs[var->reg]; 900 901 /* For gp registers, skip the address registers for now */ 902 if (var->is_reg_var_addr) 903 continue; 904 905 if (reg->ok && reg->kind == TSR_KIND_TYPE && 906 !is_better_type(®->type, &mem_die)) 907 continue; 908 909 orig_type = reg->type; 910 911 reg->type = mem_die; 912 reg->kind = TSR_KIND_TYPE; 913 reg->ok = true; 914 915 pr_debug_dtp("var [%"PRIx64"] reg%d", 916 insn_offset, var->reg); 917 pr_debug_type_name(&mem_die, TSR_KIND_TYPE); 918 919 /* 920 * If this register is directly copied from another and it gets a 921 * better type, also update the type of the source register. This 922 * is usually the case of container_of() macro with offset of 0. 923 */ 924 if (has_reg_type(state, reg->copied_from)) { 925 struct type_state_reg *copy_reg; 926 927 copy_reg = &state->regs[reg->copied_from]; 928 929 /* TODO: check if type is compatible or embedded */ 930 if (!copy_reg->ok || (copy_reg->kind != TSR_KIND_TYPE) || 931 !die_is_same(©_reg->type, &orig_type) || 932 !is_better_type(©_reg->type, &mem_die)) 933 continue; 934 935 copy_reg->type = mem_die; 936 937 pr_debug_dtp("var [%"PRIx64"] copyback reg%d", 938 insn_offset, reg->copied_from); 939 pr_debug_type_name(&mem_die, TSR_KIND_TYPE); 940 } 941 } 942 } 943 } 944 945 /** 946 * update_insn_state - Update type state for an instruction 947 * @state: type state table 948 * @dloc: data location info 949 * @cu_die: compile unit debug entry 950 * @dl: disasm line for the instruction 951 * 952 * This function updates the @state table for the target operand of the 953 * instruction at @dl if it transfers the type like MOV on x86. Since it 954 * tracks the type, it won't care about the values like in arithmetic 955 * instructions like ADD/SUB/MUL/DIV and INC/DEC. 956 * 957 * Note that ops->reg2 is only available when both mem_ref and multi_regs 958 * are true. 959 */ 960 static void update_insn_state(struct type_state *state, struct data_loc_info *dloc, 961 Dwarf_Die *cu_die, struct disasm_line *dl) 962 { 963 if (dloc->arch->update_insn_state) 964 dloc->arch->update_insn_state(state, dloc, cu_die, dl); 965 } 966 967 /* 968 * Prepend this_blocks (from the outer scope) to full_blocks, removing 969 * duplicate disasm line. 970 */ 971 static void prepend_basic_blocks(struct list_head *this_blocks, 972 struct list_head *full_blocks) 973 { 974 struct annotated_basic_block *first_bb, *last_bb; 975 976 last_bb = list_last_entry(this_blocks, typeof(*last_bb), list); 977 first_bb = list_first_entry(full_blocks, typeof(*first_bb), list); 978 979 if (list_empty(full_blocks)) 980 goto out; 981 982 /* Last insn in this_blocks should be same as first insn in full_blocks */ 983 if (last_bb->end != first_bb->begin) { 984 pr_debug("prepend basic blocks: mismatched disasm line %"PRIx64" -> %"PRIx64"\n", 985 last_bb->end->al.offset, first_bb->begin->al.offset); 986 goto out; 987 } 988 989 /* Is the basic block have only one disasm_line? */ 990 if (last_bb->begin == last_bb->end) { 991 list_del(&last_bb->list); 992 free(last_bb); 993 goto out; 994 } 995 996 /* Point to the insn before the last when adding this block to full_blocks */ 997 last_bb->end = list_prev_entry(last_bb->end, al.node); 998 999 out: 1000 list_splice(this_blocks, full_blocks); 1001 } 1002 1003 static void delete_basic_blocks(struct list_head *basic_blocks) 1004 { 1005 struct annotated_basic_block *bb, *tmp; 1006 1007 list_for_each_entry_safe(bb, tmp, basic_blocks, list) { 1008 list_del(&bb->list); 1009 free(bb); 1010 } 1011 } 1012 1013 /* Make sure all variables have a valid start address */ 1014 static void fixup_var_address(struct die_var_type *var_types, u64 addr) 1015 { 1016 while (var_types) { 1017 /* 1018 * Some variables have no address range meaning it's always 1019 * available in the whole scope. Let's adjust the start 1020 * address to the start of the scope. 1021 */ 1022 if (var_types->addr == 0) 1023 var_types->addr = addr; 1024 1025 var_types = var_types->next; 1026 } 1027 } 1028 1029 static void delete_var_types(struct die_var_type *var_types) 1030 { 1031 while (var_types) { 1032 struct die_var_type *next = var_types->next; 1033 1034 free(var_types); 1035 var_types = next; 1036 } 1037 } 1038 1039 /* should match to is_stack_canary() in util/annotate.c */ 1040 static void setup_stack_canary(struct data_loc_info *dloc) 1041 { 1042 if (arch__is(dloc->arch, "x86")) { 1043 dloc->op->segment = INSN_SEG_X86_GS; 1044 dloc->op->imm = true; 1045 dloc->op->offset = 40; 1046 } 1047 } 1048 1049 /* 1050 * It's at the target address, check if it has a matching type. 1051 * It returns PERF_TMR_BAIL_OUT when it looks up per-cpu variables which 1052 * are similar to global variables and no additional info is needed. 1053 */ 1054 static enum type_match_result check_matching_type(struct type_state *state, 1055 struct data_loc_info *dloc, 1056 Dwarf_Die *cu_die, 1057 struct disasm_line *dl, 1058 Dwarf_Die *type_die) 1059 { 1060 Dwarf_Word size; 1061 u32 insn_offset = dl->al.offset; 1062 int reg = dloc->op->reg1; 1063 int offset = dloc->op->offset; 1064 const char *offset_sign = ""; 1065 bool retry = true; 1066 1067 if (offset < 0) { 1068 offset = -offset; 1069 offset_sign = "-"; 1070 } 1071 1072 again: 1073 pr_debug_dtp("chk [%x] reg%d offset=%s%#x ok=%d kind=%d ", 1074 insn_offset, reg, offset_sign, offset, 1075 state->regs[reg].ok, state->regs[reg].kind); 1076 1077 if (!state->regs[reg].ok) 1078 goto check_non_register; 1079 1080 if (state->regs[reg].kind == TSR_KIND_TYPE) { 1081 Dwarf_Die sized_type; 1082 struct strbuf sb; 1083 1084 strbuf_init(&sb, 32); 1085 die_get_typename_from_type(&state->regs[reg].type, &sb); 1086 pr_debug_dtp("(%s)", sb.buf); 1087 strbuf_release(&sb); 1088 1089 /* 1090 * Normal registers should hold a pointer (or array) to 1091 * dereference a memory location. 1092 */ 1093 if (!is_pointer_type(&state->regs[reg].type)) { 1094 if (dloc->op->offset < 0 && reg != state->stack_reg) 1095 goto check_kernel; 1096 1097 return PERF_TMR_NO_POINTER; 1098 } 1099 1100 /* Remove the pointer and get the target type */ 1101 if (__die_get_real_type(&state->regs[reg].type, type_die) == NULL) 1102 return PERF_TMR_NO_POINTER; 1103 1104 dloc->type_offset = dloc->op->offset; 1105 1106 if (dwarf_tag(type_die) == DW_TAG_typedef) 1107 die_get_real_type(type_die, &sized_type); 1108 else 1109 sized_type = *type_die; 1110 1111 /* Get the size of the actual type */ 1112 if (dwarf_aggregate_size(&sized_type, &size) < 0 || 1113 (unsigned)dloc->type_offset >= size) 1114 return PERF_TMR_BAD_OFFSET; 1115 1116 return PERF_TMR_OK; 1117 } 1118 1119 if (state->regs[reg].kind == TSR_KIND_PERCPU_POINTER) { 1120 pr_debug_dtp("percpu ptr"); 1121 1122 /* 1123 * It's actaully pointer but the address was calculated using 1124 * some arithmetic. So it points to the actual type already. 1125 */ 1126 *type_die = state->regs[reg].type; 1127 1128 dloc->type_offset = dloc->op->offset; 1129 1130 /* Get the size of the actual type */ 1131 if (dwarf_aggregate_size(type_die, &size) < 0 || 1132 (unsigned)dloc->type_offset >= size) 1133 return PERF_TMR_BAIL_OUT; 1134 1135 return PERF_TMR_OK; 1136 } 1137 1138 if (state->regs[reg].kind == TSR_KIND_CANARY) { 1139 pr_debug_dtp("stack canary"); 1140 1141 /* 1142 * This is a saved value of the stack canary which will be handled 1143 * in the outer logic when it returns failure here. Pretend it's 1144 * from the stack canary directly. 1145 */ 1146 setup_stack_canary(dloc); 1147 1148 return PERF_TMR_BAIL_OUT; 1149 } 1150 1151 if (state->regs[reg].kind == TSR_KIND_PERCPU_BASE) { 1152 u64 var_addr = dloc->op->offset; 1153 int var_offset; 1154 1155 pr_debug_dtp("percpu var"); 1156 1157 if (dloc->op->multi_regs) { 1158 int reg2 = dloc->op->reg2; 1159 1160 if (dloc->op->reg2 == reg) 1161 reg2 = dloc->op->reg1; 1162 1163 if (has_reg_type(state, reg2) && state->regs[reg2].ok && 1164 state->regs[reg2].kind == TSR_KIND_CONST) 1165 var_addr += state->regs[reg2].imm_value; 1166 } 1167 1168 if (get_global_var_type(cu_die, dloc, dloc->ip, var_addr, 1169 &var_offset, type_die)) { 1170 dloc->type_offset = var_offset; 1171 return PERF_TMR_OK; 1172 } 1173 /* No need to retry per-cpu (global) variables */ 1174 return PERF_TMR_BAIL_OUT; 1175 } 1176 1177 check_non_register: 1178 if (reg == dloc->fbreg || reg == state->stack_reg) { 1179 struct type_state_stack *stack; 1180 1181 pr_debug_dtp("%s", reg == dloc->fbreg ? "fbreg" : "stack"); 1182 1183 stack = find_stack_state(state, dloc->type_offset); 1184 if (stack == NULL) { 1185 if (retry) { 1186 pr_debug_dtp(" : retry\n"); 1187 retry = false; 1188 1189 /* update type info it's the first store to the stack */ 1190 update_insn_state(state, dloc, cu_die, dl); 1191 goto again; 1192 } 1193 return PERF_TMR_NO_TYPE; 1194 } 1195 1196 if (stack->kind == TSR_KIND_CANARY) { 1197 setup_stack_canary(dloc); 1198 return PERF_TMR_BAIL_OUT; 1199 } 1200 1201 if (stack->kind != TSR_KIND_TYPE) 1202 return PERF_TMR_NO_TYPE; 1203 1204 *type_die = stack->type; 1205 /* Update the type offset from the start of slot */ 1206 dloc->type_offset -= stack->offset; 1207 1208 return PERF_TMR_OK; 1209 } 1210 1211 if (dloc->fb_cfa) { 1212 struct type_state_stack *stack; 1213 u64 pc = map__rip_2objdump(dloc->ms->map, dloc->ip); 1214 int fbreg, fboff; 1215 1216 pr_debug_dtp("cfa"); 1217 1218 if (die_get_cfa(dloc->di->dbg, pc, &fbreg, &fboff) < 0) 1219 fbreg = -1; 1220 1221 if (reg != fbreg) 1222 return PERF_TMR_NO_TYPE; 1223 1224 stack = find_stack_state(state, dloc->type_offset - fboff); 1225 if (stack == NULL) { 1226 if (retry) { 1227 pr_debug_dtp(" : retry\n"); 1228 retry = false; 1229 1230 /* update type info it's the first store to the stack */ 1231 update_insn_state(state, dloc, cu_die, dl); 1232 goto again; 1233 } 1234 return PERF_TMR_NO_TYPE; 1235 } 1236 1237 if (stack->kind == TSR_KIND_CANARY) { 1238 setup_stack_canary(dloc); 1239 return PERF_TMR_BAIL_OUT; 1240 } 1241 1242 if (stack->kind != TSR_KIND_TYPE) 1243 return PERF_TMR_NO_TYPE; 1244 1245 *type_die = stack->type; 1246 /* Update the type offset from the start of slot */ 1247 dloc->type_offset -= fboff + stack->offset; 1248 1249 return PERF_TMR_OK; 1250 } 1251 1252 check_kernel: 1253 if (dso__kernel(map__dso(dloc->ms->map))) { 1254 u64 addr; 1255 1256 /* Direct this-cpu access like "%gs:0x34740" */ 1257 if (dloc->op->segment == INSN_SEG_X86_GS && dloc->op->imm && 1258 arch__is(dloc->arch, "x86")) { 1259 pr_debug_dtp("this-cpu var"); 1260 1261 addr = dloc->op->offset; 1262 1263 if (get_global_var_type(cu_die, dloc, dloc->ip, addr, 1264 &offset, type_die)) { 1265 dloc->type_offset = offset; 1266 return PERF_TMR_OK; 1267 } 1268 return PERF_TMR_BAIL_OUT; 1269 } 1270 1271 /* Access to global variable like "-0x7dcf0500(,%rdx,8)" */ 1272 if (dloc->op->offset < 0 && reg != state->stack_reg) { 1273 addr = (s64) dloc->op->offset; 1274 1275 if (get_global_var_type(cu_die, dloc, dloc->ip, addr, 1276 &offset, type_die)) { 1277 pr_debug_dtp("global var"); 1278 1279 dloc->type_offset = offset; 1280 return PERF_TMR_OK; 1281 } 1282 return PERF_TMR_BAIL_OUT; 1283 } 1284 } 1285 1286 return PERF_TMR_UNKNOWN; 1287 } 1288 1289 /* Iterate instructions in basic blocks and update type table */ 1290 static enum type_match_result find_data_type_insn(struct data_loc_info *dloc, 1291 struct list_head *basic_blocks, 1292 struct die_var_type *var_types, 1293 Dwarf_Die *cu_die, 1294 Dwarf_Die *type_die) 1295 { 1296 struct type_state state; 1297 struct symbol *sym = dloc->ms->sym; 1298 struct annotation *notes = symbol__annotation(sym); 1299 struct annotated_basic_block *bb; 1300 enum type_match_result ret = PERF_TMR_UNKNOWN; 1301 1302 init_type_state(&state, dloc->arch); 1303 1304 list_for_each_entry(bb, basic_blocks, list) { 1305 struct disasm_line *dl = bb->begin; 1306 1307 BUG_ON(bb->begin->al.offset == -1 || bb->end->al.offset == -1); 1308 1309 pr_debug_dtp("bb: [%"PRIx64" - %"PRIx64"]\n", 1310 bb->begin->al.offset, bb->end->al.offset); 1311 1312 list_for_each_entry_from(dl, ¬es->src->source, al.node) { 1313 u64 this_ip = sym->start + dl->al.offset; 1314 u64 addr = map__rip_2objdump(dloc->ms->map, this_ip); 1315 1316 /* Skip comment or debug info lines */ 1317 if (dl->al.offset == -1) 1318 continue; 1319 1320 /* Update variable type at this address */ 1321 update_var_state(&state, dloc, addr, dl->al.offset, var_types); 1322 1323 if (this_ip == dloc->ip) { 1324 ret = check_matching_type(&state, dloc, 1325 cu_die, dl, type_die); 1326 pr_debug_dtp(" : %s\n", match_result_str(ret)); 1327 goto out; 1328 } 1329 1330 /* Update type table after processing the instruction */ 1331 update_insn_state(&state, dloc, cu_die, dl); 1332 if (dl == bb->end) 1333 break; 1334 } 1335 } 1336 1337 out: 1338 exit_type_state(&state); 1339 return ret; 1340 } 1341 1342 static int arch_supports_insn_tracking(struct data_loc_info *dloc) 1343 { 1344 if ((arch__is(dloc->arch, "x86")) || (arch__is(dloc->arch, "powerpc"))) 1345 return 1; 1346 return 0; 1347 } 1348 1349 /* 1350 * Construct a list of basic blocks for each scope with variables and try to find 1351 * the data type by updating a type state table through instructions. 1352 */ 1353 static enum type_match_result find_data_type_block(struct data_loc_info *dloc, 1354 Dwarf_Die *cu_die, 1355 Dwarf_Die *scopes, 1356 int nr_scopes, 1357 Dwarf_Die *type_die) 1358 { 1359 LIST_HEAD(basic_blocks); 1360 struct die_var_type *var_types = NULL; 1361 u64 src_ip, dst_ip, prev_dst_ip; 1362 enum type_match_result ret = PERF_TMR_UNKNOWN; 1363 1364 /* TODO: other architecture support */ 1365 if (!arch_supports_insn_tracking(dloc)) 1366 return PERF_TMR_BAIL_OUT; 1367 1368 prev_dst_ip = dst_ip = dloc->ip; 1369 for (int i = nr_scopes - 1; i >= 0; i--) { 1370 Dwarf_Addr base, start, end; 1371 LIST_HEAD(this_blocks); 1372 1373 if (dwarf_ranges(&scopes[i], 0, &base, &start, &end) < 0) 1374 break; 1375 1376 pr_debug_dtp("scope: [%d/%d] ", i + 1, nr_scopes); 1377 pr_debug_scope(&scopes[i]); 1378 1379 src_ip = map__objdump_2rip(dloc->ms->map, start); 1380 1381 again: 1382 /* Get basic blocks for this scope */ 1383 if (annotate_get_basic_blocks(dloc->ms->sym, src_ip, dst_ip, 1384 &this_blocks) < 0) { 1385 /* Try previous block if they are not connected */ 1386 if (prev_dst_ip != dst_ip) { 1387 dst_ip = prev_dst_ip; 1388 goto again; 1389 } 1390 1391 pr_debug_dtp("cannot find a basic block from %"PRIx64" to %"PRIx64"\n", 1392 src_ip - dloc->ms->sym->start, 1393 dst_ip - dloc->ms->sym->start); 1394 continue; 1395 } 1396 prepend_basic_blocks(&this_blocks, &basic_blocks); 1397 1398 /* Get variable info for this scope and add to var_types list */ 1399 die_collect_vars(&scopes[i], &var_types); 1400 fixup_var_address(var_types, start); 1401 1402 /* Find from start of this scope to the target instruction */ 1403 ret = find_data_type_insn(dloc, &basic_blocks, var_types, 1404 cu_die, type_die); 1405 if (ret == PERF_TMR_OK) { 1406 char buf[64]; 1407 int offset = dloc->op->offset; 1408 const char *offset_sign = ""; 1409 1410 if (offset < 0) { 1411 offset = -offset; 1412 offset_sign = "-"; 1413 } 1414 1415 if (dloc->op->multi_regs) 1416 snprintf(buf, sizeof(buf), "reg%d, reg%d", 1417 dloc->op->reg1, dloc->op->reg2); 1418 else 1419 snprintf(buf, sizeof(buf), "reg%d", dloc->op->reg1); 1420 1421 pr_debug_dtp("found by insn track: %s%#x(%s) type-offset=%#x\n", 1422 offset_sign, offset, buf, dloc->type_offset); 1423 break; 1424 } 1425 1426 if (ret == PERF_TMR_BAIL_OUT) 1427 break; 1428 1429 /* Go up to the next scope and find blocks to the start */ 1430 prev_dst_ip = dst_ip; 1431 dst_ip = src_ip; 1432 } 1433 1434 delete_basic_blocks(&basic_blocks); 1435 delete_var_types(var_types); 1436 return ret; 1437 } 1438 1439 /* The result will be saved in @type_die */ 1440 static int find_data_type_die(struct data_loc_info *dloc, Dwarf_Die *type_die) 1441 { 1442 struct annotated_op_loc *loc = dloc->op; 1443 Dwarf_Die cu_die, var_die; 1444 Dwarf_Die *scopes = NULL; 1445 int reg, offset = loc->offset; 1446 int ret = -1; 1447 int i, nr_scopes; 1448 int fbreg = -1; 1449 int fb_offset = 0; 1450 bool is_fbreg = false; 1451 bool found = false; 1452 u64 pc; 1453 char buf[64]; 1454 enum type_match_result result = PERF_TMR_UNKNOWN; 1455 const char *offset_sign = ""; 1456 1457 if (dloc->op->multi_regs) 1458 snprintf(buf, sizeof(buf), "reg%d, reg%d", dloc->op->reg1, dloc->op->reg2); 1459 else if (dloc->op->reg1 == DWARF_REG_PC) 1460 snprintf(buf, sizeof(buf), "PC"); 1461 else 1462 snprintf(buf, sizeof(buf), "reg%d", dloc->op->reg1); 1463 1464 if (offset < 0) { 1465 offset = -offset; 1466 offset_sign = "-"; 1467 } 1468 1469 pr_debug_dtp("-----------------------------------------------------------\n"); 1470 pr_debug_dtp("find data type for %s%#x(%s) at %s+%#"PRIx64"\n", 1471 offset_sign, offset, buf, 1472 dloc->ms->sym->name, dloc->ip - dloc->ms->sym->start); 1473 1474 /* 1475 * IP is a relative instruction address from the start of the map, as 1476 * it can be randomized/relocated, it needs to translate to PC which is 1477 * a file address for DWARF processing. 1478 */ 1479 pc = map__rip_2objdump(dloc->ms->map, dloc->ip); 1480 1481 /* Get a compile_unit for this address */ 1482 if (!find_cu_die(dloc->di, pc, &cu_die)) { 1483 pr_debug_dtp("cannot find CU for address %"PRIx64"\n", pc); 1484 ann_data_stat.no_cuinfo++; 1485 return -1; 1486 } 1487 1488 reg = loc->reg1; 1489 offset = loc->offset; 1490 1491 pr_debug_dtp("CU for %s (die:%#lx)\n", 1492 dwarf_diename(&cu_die), (long)dwarf_dieoffset(&cu_die)); 1493 1494 if (reg == DWARF_REG_PC) { 1495 if (get_global_var_type(&cu_die, dloc, dloc->ip, dloc->var_addr, 1496 &offset, type_die)) { 1497 dloc->type_offset = offset; 1498 1499 pr_debug_dtp("found by addr=%#"PRIx64" type_offset=%#x\n", 1500 dloc->var_addr, offset); 1501 pr_debug_type_name(type_die, TSR_KIND_TYPE); 1502 found = true; 1503 goto out; 1504 } 1505 } 1506 1507 /* Get a list of nested scopes - i.e. (inlined) functions and blocks. */ 1508 nr_scopes = die_get_scopes(&cu_die, pc, &scopes); 1509 1510 if (reg != DWARF_REG_PC && dwarf_hasattr(&scopes[0], DW_AT_frame_base)) { 1511 Dwarf_Attribute attr; 1512 Dwarf_Block block; 1513 1514 /* Check if the 'reg' is assigned as frame base register */ 1515 if (dwarf_attr(&scopes[0], DW_AT_frame_base, &attr) != NULL && 1516 dwarf_formblock(&attr, &block) == 0 && block.length == 1) { 1517 switch (*block.data) { 1518 case DW_OP_reg0 ... DW_OP_reg31: 1519 fbreg = dloc->fbreg = *block.data - DW_OP_reg0; 1520 break; 1521 case DW_OP_call_frame_cfa: 1522 dloc->fb_cfa = true; 1523 if (die_get_cfa(dloc->di->dbg, pc, &fbreg, 1524 &fb_offset) < 0) 1525 fbreg = -1; 1526 break; 1527 default: 1528 break; 1529 } 1530 1531 pr_debug_dtp("frame base: cfa=%d fbreg=%d\n", 1532 dloc->fb_cfa, fbreg); 1533 } 1534 } 1535 1536 retry: 1537 is_fbreg = (reg == fbreg); 1538 if (is_fbreg) 1539 offset = loc->offset - fb_offset; 1540 1541 /* Search from the inner-most scope to the outer */ 1542 for (i = nr_scopes - 1; i >= 0; i--) { 1543 Dwarf_Die mem_die; 1544 int type_offset = offset; 1545 1546 if (reg == DWARF_REG_PC) { 1547 if (!die_find_variable_by_addr(&scopes[i], dloc->var_addr, 1548 &var_die, &type_offset)) 1549 continue; 1550 } else { 1551 /* Look up variables/parameters in this scope */ 1552 if (!die_find_variable_by_reg(&scopes[i], pc, reg, 1553 &type_offset, is_fbreg, &var_die)) 1554 continue; 1555 } 1556 1557 pr_debug_dtp("found \"%s\" (die: %#lx) in scope=%d/%d (die: %#lx) ", 1558 dwarf_diename(&var_die), (long)dwarf_dieoffset(&var_die), 1559 i+1, nr_scopes, (long)dwarf_dieoffset(&scopes[i])); 1560 1561 /* Found a variable, see if it's correct */ 1562 result = check_variable(dloc, &var_die, &mem_die, reg, type_offset, is_fbreg); 1563 if (result == PERF_TMR_OK) { 1564 if (reg == DWARF_REG_PC) { 1565 pr_debug_dtp("addr=%#"PRIx64" type_offset=%#x\n", 1566 dloc->var_addr, type_offset); 1567 } else if (reg == DWARF_REG_FB || is_fbreg) { 1568 pr_debug_dtp("stack_offset=%#x type_offset=%#x\n", 1569 fb_offset, type_offset); 1570 } else { 1571 pr_debug_dtp("type_offset=%#x\n", type_offset); 1572 } 1573 1574 if (!found || is_better_type(type_die, &mem_die)) { 1575 *type_die = mem_die; 1576 dloc->type_offset = type_offset; 1577 found = true; 1578 } 1579 } else { 1580 pr_debug_dtp("failed: %s\n", match_result_str(result)); 1581 } 1582 1583 pr_debug_location(&var_die, pc, reg); 1584 pr_debug_type_name(&mem_die, TSR_KIND_TYPE); 1585 } 1586 1587 if (!found && loc->multi_regs && reg == loc->reg1 && loc->reg1 != loc->reg2) { 1588 reg = loc->reg2; 1589 goto retry; 1590 } 1591 1592 if (!found && reg != DWARF_REG_PC) { 1593 result = find_data_type_block(dloc, &cu_die, scopes, 1594 nr_scopes, type_die); 1595 if (result == PERF_TMR_OK) { 1596 ann_data_stat.insn_track++; 1597 found = true; 1598 } 1599 } 1600 1601 out: 1602 pr_debug_dtp("final result: "); 1603 if (found) { 1604 pr_debug_type_name(type_die, TSR_KIND_TYPE); 1605 ret = 0; 1606 } else { 1607 switch (result) { 1608 case PERF_TMR_NO_TYPE: 1609 case PERF_TMR_NO_POINTER: 1610 pr_debug_dtp("%s\n", match_result_str(result)); 1611 ann_data_stat.no_typeinfo++; 1612 break; 1613 case PERF_TMR_NO_SIZE: 1614 pr_debug_dtp("%s\n", match_result_str(result)); 1615 ann_data_stat.invalid_size++; 1616 break; 1617 case PERF_TMR_BAD_OFFSET: 1618 pr_debug_dtp("%s\n", match_result_str(result)); 1619 ann_data_stat.bad_offset++; 1620 break; 1621 case PERF_TMR_UNKNOWN: 1622 case PERF_TMR_BAIL_OUT: 1623 case PERF_TMR_OK: /* should not reach here */ 1624 default: 1625 pr_debug_dtp("no variable found\n"); 1626 ann_data_stat.no_var++; 1627 break; 1628 } 1629 ret = -1; 1630 } 1631 1632 free(scopes); 1633 return ret; 1634 } 1635 1636 /** 1637 * find_data_type - Return a data type at the location 1638 * @dloc: data location 1639 * 1640 * This functions searches the debug information of the binary to get the data 1641 * type it accesses. The exact location is expressed by (ip, reg, offset) 1642 * for pointer variables or (ip, addr) for global variables. Note that global 1643 * variables might update the @dloc->type_offset after finding the start of the 1644 * variable. If it cannot find a global variable by address, it tried to find 1645 * a declaration of the variable using var_name. In that case, @dloc->offset 1646 * won't be updated. 1647 * 1648 * It return %NULL if not found. 1649 */ 1650 struct annotated_data_type *find_data_type(struct data_loc_info *dloc) 1651 { 1652 struct dso *dso = map__dso(dloc->ms->map); 1653 Dwarf_Die type_die; 1654 1655 /* 1656 * The type offset is the same as instruction offset by default. 1657 * But when finding a global variable, the offset won't be valid. 1658 */ 1659 dloc->type_offset = dloc->op->offset; 1660 1661 dloc->fbreg = -1; 1662 1663 if (find_data_type_die(dloc, &type_die) < 0) 1664 return NULL; 1665 1666 return dso__findnew_data_type(dso, &type_die); 1667 } 1668 1669 static int alloc_data_type_histograms(struct annotated_data_type *adt, int nr_entries) 1670 { 1671 int i; 1672 size_t sz = sizeof(struct type_hist); 1673 1674 sz += sizeof(struct type_hist_entry) * adt->self.size; 1675 1676 /* Allocate a table of pointers for each event */ 1677 adt->histograms = calloc(nr_entries, sizeof(*adt->histograms)); 1678 if (adt->histograms == NULL) 1679 return -ENOMEM; 1680 1681 /* 1682 * Each histogram is allocated for the whole size of the type. 1683 * TODO: Probably we can move the histogram to members. 1684 */ 1685 for (i = 0; i < nr_entries; i++) { 1686 adt->histograms[i] = zalloc(sz); 1687 if (adt->histograms[i] == NULL) 1688 goto err; 1689 } 1690 1691 adt->nr_histograms = nr_entries; 1692 return 0; 1693 1694 err: 1695 while (--i >= 0) 1696 zfree(&(adt->histograms[i])); 1697 zfree(&adt->histograms); 1698 return -ENOMEM; 1699 } 1700 1701 static void delete_data_type_histograms(struct annotated_data_type *adt) 1702 { 1703 for (int i = 0; i < adt->nr_histograms; i++) 1704 zfree(&(adt->histograms[i])); 1705 1706 zfree(&adt->histograms); 1707 adt->nr_histograms = 0; 1708 } 1709 1710 void annotated_data_type__tree_delete(struct rb_root *root) 1711 { 1712 struct annotated_data_type *pos; 1713 1714 while (!RB_EMPTY_ROOT(root)) { 1715 struct rb_node *node = rb_first(root); 1716 1717 rb_erase(node, root); 1718 pos = rb_entry(node, struct annotated_data_type, node); 1719 delete_members(&pos->self); 1720 delete_data_type_histograms(pos); 1721 zfree(&pos->self.type_name); 1722 free(pos); 1723 } 1724 } 1725 1726 /** 1727 * annotated_data_type__update_samples - Update histogram 1728 * @adt: Data type to update 1729 * @evsel: Event to update 1730 * @offset: Offset in the type 1731 * @nr_samples: Number of samples at this offset 1732 * @period: Event count at this offset 1733 * 1734 * This function updates type histogram at @ofs for @evsel. Samples are 1735 * aggregated before calling this function so it can be called with more 1736 * than one samples at a certain offset. 1737 */ 1738 int annotated_data_type__update_samples(struct annotated_data_type *adt, 1739 struct evsel *evsel, int offset, 1740 int nr_samples, u64 period) 1741 { 1742 struct type_hist *h; 1743 1744 if (adt == NULL) 1745 return 0; 1746 1747 if (adt->histograms == NULL) { 1748 int nr = evsel->evlist->core.nr_entries; 1749 1750 if (alloc_data_type_histograms(adt, nr) < 0) 1751 return -1; 1752 } 1753 1754 if (offset < 0 || offset >= adt->self.size) 1755 return -1; 1756 1757 h = adt->histograms[evsel->core.idx]; 1758 1759 h->nr_samples += nr_samples; 1760 h->addr[offset].nr_samples += nr_samples; 1761 h->period += period; 1762 h->addr[offset].period += period; 1763 return 0; 1764 } 1765 1766 static void print_annotated_data_header(struct hist_entry *he, struct evsel *evsel) 1767 { 1768 struct dso *dso = map__dso(he->ms.map); 1769 int nr_members = 1; 1770 int nr_samples = he->stat.nr_events; 1771 int width = 7; 1772 const char *val_hdr = "Percent"; 1773 1774 if (evsel__is_group_event(evsel)) { 1775 struct hist_entry *pair; 1776 1777 list_for_each_entry(pair, &he->pairs.head, pairs.node) 1778 nr_samples += pair->stat.nr_events; 1779 } 1780 1781 printf("Annotate type: '%s' in %s (%d samples):\n", 1782 he->mem_type->self.type_name, dso__name(dso), nr_samples); 1783 1784 if (evsel__is_group_event(evsel)) { 1785 struct evsel *pos; 1786 int i = 0; 1787 1788 nr_members = 0; 1789 for_each_group_evsel(pos, evsel) { 1790 if (symbol_conf.skip_empty && 1791 evsel__hists(pos)->stats.nr_samples == 0) 1792 continue; 1793 1794 printf(" event[%d] = %s\n", i++, pos->name); 1795 nr_members++; 1796 } 1797 } 1798 1799 if (symbol_conf.show_total_period) { 1800 width = 11; 1801 val_hdr = "Period"; 1802 } else if (symbol_conf.show_nr_samples) { 1803 width = 7; 1804 val_hdr = "Samples"; 1805 } 1806 1807 printf("============================================================================\n"); 1808 printf("%*s %10s %10s %s\n", (width + 1) * nr_members, val_hdr, 1809 "offset", "size", "field"); 1810 } 1811 1812 static void print_annotated_data_value(struct type_hist *h, u64 period, int nr_samples) 1813 { 1814 double percent = h->period ? (100.0 * period / h->period) : 0; 1815 const char *color = get_percent_color(percent); 1816 1817 if (symbol_conf.show_total_period) 1818 color_fprintf(stdout, color, " %11" PRIu64, period); 1819 else if (symbol_conf.show_nr_samples) 1820 color_fprintf(stdout, color, " %7d", nr_samples); 1821 else 1822 color_fprintf(stdout, color, " %7.2f", percent); 1823 } 1824 1825 static void print_annotated_data_type(struct annotated_data_type *mem_type, 1826 struct annotated_member *member, 1827 struct evsel *evsel, int indent) 1828 { 1829 struct annotated_member *child; 1830 struct type_hist *h = mem_type->histograms[evsel->core.idx]; 1831 int i, nr_events = 0, samples = 0; 1832 u64 period = 0; 1833 int width = symbol_conf.show_total_period ? 11 : 7; 1834 struct evsel *pos; 1835 1836 for_each_group_evsel(pos, evsel) { 1837 h = mem_type->histograms[pos->core.idx]; 1838 1839 if (symbol_conf.skip_empty && 1840 evsel__hists(pos)->stats.nr_samples == 0) 1841 continue; 1842 1843 samples = 0; 1844 period = 0; 1845 for (i = 0; i < member->size; i++) { 1846 samples += h->addr[member->offset + i].nr_samples; 1847 period += h->addr[member->offset + i].period; 1848 } 1849 print_annotated_data_value(h, period, samples); 1850 nr_events++; 1851 } 1852 1853 printf(" %#10x %#10x %*s%s\t%s", 1854 member->offset, member->size, indent, "", member->type_name, 1855 member->var_name ?: ""); 1856 1857 if (!list_empty(&member->children)) 1858 printf(" {\n"); 1859 1860 list_for_each_entry(child, &member->children, node) 1861 print_annotated_data_type(mem_type, child, evsel, indent + 4); 1862 1863 if (!list_empty(&member->children)) 1864 printf("%*s}", (width + 1) * nr_events + 24 + indent, ""); 1865 printf(";\n"); 1866 } 1867 1868 int hist_entry__annotate_data_tty(struct hist_entry *he, struct evsel *evsel) 1869 { 1870 print_annotated_data_header(he, evsel); 1871 print_annotated_data_type(he->mem_type, &he->mem_type->self, evsel, 0); 1872 printf("\n"); 1873 1874 /* move to the next entry */ 1875 return '>'; 1876 } 1877