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