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