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