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