1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef __PERF_ANNOTATE_H 3 #define __PERF_ANNOTATE_H 4 5 #include <stdbool.h> 6 #include <stdint.h> 7 #include <stdio.h> 8 #include <linux/types.h> 9 #include <linux/bitops.h> 10 #include <linux/list.h> 11 #include <linux/rbtree.h> 12 #include <asm/bug.h> 13 #include "symbol_conf.h" 14 #include "mutex.h" 15 #include "spark.h" 16 #include "hashmap.h" 17 #include "disasm.h" 18 #include "branch.h" 19 #include "evsel.h" 20 21 struct hist_browser_timer; 22 struct hist_entry; 23 struct map; 24 struct map_symbol; 25 struct addr_map_symbol; 26 struct option; 27 struct perf_sample; 28 struct symbol; 29 struct annotated_data_type; 30 31 #define ANNOTATION__IPC_WIDTH 6 32 #define ANNOTATION__CYCLES_WIDTH 6 33 #define ANNOTATION__MINMAX_CYCLES_WIDTH 19 34 #define ANNOTATION__AVG_IPC_WIDTH 36 35 #define ANNOTATION__BR_CNTR_WIDTH 30 36 #define ANNOTATION_DUMMY_LEN 256 37 38 enum perf_disassembler { 39 PERF_DISASM_UNKNOWN = 0, 40 PERF_DISASM_LLVM, 41 PERF_DISASM_CAPSTONE, 42 PERF_DISASM_OBJDUMP, 43 }; 44 #define MAX_DISASSEMBLERS (PERF_DISASM_OBJDUMP + 1) 45 46 struct annotation_options { 47 bool hide_src_code, 48 hide_src_code_on_title, 49 use_offset, 50 jump_arrows, 51 print_lines, 52 full_path, 53 show_linenr, 54 show_fileloc, 55 show_nr_jumps, 56 show_minmax_cycle, 57 show_asm_raw, 58 show_br_cntr, 59 annotate_src, 60 code_with_type, 61 full_addr; 62 u8 offset_level; 63 u8 disassemblers[MAX_DISASSEMBLERS]; 64 u8 disassembler_used; 65 int min_pcnt; 66 int max_lines; 67 int context; 68 char *objdump_path; 69 char *disassembler_style; 70 const char *prefix; 71 const char *prefix_strip; 72 unsigned int percent_type; 73 }; 74 75 extern struct annotation_options annotate_opts; 76 77 enum { 78 ANNOTATION__OFFSET_JUMP_TARGETS = 1, 79 ANNOTATION__OFFSET_CALL, 80 ANNOTATION__MAX_OFFSET_LEVEL, 81 }; 82 83 #define ANNOTATION__MIN_OFFSET_LEVEL ANNOTATION__OFFSET_JUMP_TARGETS 84 85 struct annotation; 86 87 struct sym_hist_entry { 88 u64 nr_samples; 89 u64 period; 90 u64 weight_sum[WEIGHT_WEIGHT3 + 1]; 91 u64 weight_num[WEIGHT_WEIGHT3 + 1]; 92 }; 93 94 enum { 95 PERCENT_HITS_LOCAL, 96 PERCENT_HITS_GLOBAL, 97 PERCENT_PERIOD_LOCAL, 98 PERCENT_PERIOD_GLOBAL, 99 PERCENT_MAX, 100 }; 101 102 struct annotation_data { 103 double percent[PERCENT_MAX]; 104 double percent_sum; 105 struct sym_hist_entry he; 106 }; 107 108 struct cycles_info { 109 float ipc; 110 u64 avg; 111 u64 max; 112 u64 min; 113 }; 114 115 struct annotation_line { 116 struct list_head node; 117 struct rb_node rb_node; 118 s64 offset; 119 char *line; 120 int line_nr; 121 char *fileloc; 122 char *path; 123 struct cycles_info *cycles; 124 int num_aggr; 125 int br_cntr_nr; 126 u64 *br_cntr; 127 struct evsel *evsel; 128 int jump_sources; 129 u32 idx; 130 int idx_asm; 131 int data_nr; 132 struct annotation_data data[]; 133 }; 134 135 struct disasm_line { 136 struct ins ins; 137 struct ins_operands ops; 138 union { 139 u8 bytes[4]; 140 u32 raw_insn; 141 } raw; 142 /* This needs to be at the end. */ 143 struct annotation_line al; 144 }; 145 146 extern const char * const perf_disassembler__strs[]; 147 148 void annotation_line__add(struct annotation_line *al, struct list_head *head); 149 150 static inline double annotation_data__percent(struct annotation_data *data, 151 unsigned int which) 152 { 153 return which < PERCENT_MAX ? data->percent[which] : -1; 154 } 155 156 static inline const char *percent_type_str(unsigned int type) 157 { 158 static const char *str[PERCENT_MAX] = { 159 "local hits", 160 "global hits", 161 "local period", 162 "global period", 163 }; 164 165 if (WARN_ON(type >= PERCENT_MAX)) 166 return "N/A"; 167 168 return str[type]; 169 } 170 171 static inline struct disasm_line *disasm_line(struct annotation_line *al) 172 { 173 return al ? container_of(al, struct disasm_line, al) : NULL; 174 } 175 176 /* 177 * Is this offset in the same function as the line it is used? 178 * asm functions jump to other functions, for instance. 179 */ 180 static inline bool disasm_line__has_local_offset(const struct disasm_line *dl) 181 { 182 return dl->ops.target.offset_avail && !dl->ops.target.outside; 183 } 184 185 /* 186 * Can we draw an arrow from the jump to its target, for instance? I.e. 187 * is the jump and its target in the same function? 188 */ 189 bool disasm_line__is_valid_local_jump(struct disasm_line *dl, struct symbol *sym); 190 191 struct annotation_line * 192 annotation_line__next(struct annotation_line *pos, struct list_head *head); 193 194 struct annotation_write_ops { 195 bool first_line, current_entry, change_color; 196 int width; 197 void *obj; 198 int (*set_color)(void *obj, int color); 199 void (*set_percent_color)(void *obj, double percent, bool current); 200 int (*set_jumps_percent_color)(void *obj, int nr, bool current); 201 void (*printf)(void *obj, const char *fmt, ...); 202 void (*write_graph)(void *obj, int graph); 203 }; 204 205 struct annotation_print_data { 206 struct hist_entry *he; 207 struct evsel *evsel; 208 const struct arch *arch; 209 struct debuginfo *dbg; 210 /* save data type info keyed by al->offset */ 211 struct hashmap *type_hash; 212 /* It'll be set in hist_entry__annotate_printf() */ 213 int addr_fmt_width; 214 }; 215 216 void annotation_line__write(struct annotation_line *al, struct annotation *notes, 217 const struct annotation_write_ops *ops, 218 struct annotation_print_data *apd); 219 220 int __annotation__scnprintf_samples_period(struct annotation *notes, 221 char *bf, size_t size, 222 struct evsel *evsel, 223 bool show_freq); 224 225 size_t disasm__fprintf(struct list_head *head, FILE *fp); 226 void symbol__calc_percent(struct symbol *sym, struct evsel *evsel); 227 228 /** 229 * struct sym_hist - symbol histogram information for an event 230 * 231 * @nr_samples: Total number of samples. 232 * @period: Sum of sample periods. 233 */ 234 struct sym_hist { 235 u64 nr_samples; 236 u64 period; 237 u8 weight_mask; 238 }; 239 240 /* Can be set asynchronously by top. */ 241 static inline u8 sym_hist__weight_mask(const struct sym_hist *hist) 242 { 243 return __atomic_load_n(&hist->weight_mask, __ATOMIC_RELAXED); 244 } 245 246 static inline void sym_hist__set_weight_mask(struct sym_hist *hist, u8 mask) 247 { 248 __atomic_fetch_or(&hist->weight_mask, mask, __ATOMIC_RELAXED); 249 } 250 251 /** 252 * struct cyc_hist - (CPU) cycle histogram for a basic block 253 * 254 * @start: Start address of current block (if known). 255 * @cycles: Sum of cycles for the longest basic block. 256 * @cycles_aggr: Total cycles for this address. 257 * @cycles_max: Max cycles for this address. 258 * @cycles_min: Min cycles for this address. 259 * @cycles_spark: History of cycles for the longest basic block. 260 * @num: Number of samples for the longest basic block. 261 * @num_aggr: Total number of samples for this address. 262 * @have_start: Whether the current branch info has a start address. 263 * @reset: Number of resets due to a different start address. 264 * 265 * If sample has branch_stack and cycles info, it can construct basic blocks 266 * between two adjacent branches. It'd have start and end addresses but 267 * sometimes the start address may not be available. So the cycles are 268 * accounted at the end address. If multiple basic blocks end at the same 269 * address, it will take the longest one. 270 * 271 * The @start, @cycles, @cycles_spark and @num fields are used for the longest 272 * block only. Other fields are used for all cases. 273 * 274 * See __symbol__account_cycles(). 275 */ 276 struct cyc_hist { 277 u64 start; 278 u64 cycles; 279 u64 cycles_aggr; 280 u64 cycles_max; 281 u64 cycles_min; 282 s64 cycles_spark[NUM_SPARKS]; 283 u32 num; 284 u32 num_aggr; 285 u8 have_start; 286 /* 1 byte padding */ 287 u16 reset; 288 }; 289 290 /** 291 * struct annotated_source - symbols with hits have this attached as in annotation 292 * 293 * @source: List head for annotated_line (embedded in disasm_line). 294 * @histograms: Array of symbol histograms per event to maintain the total number 295 * of samples and period. 296 * @nr_histograms: This may not be the same as evsel->evlist->core.nr_entries if 297 * we have more than a group in a evlist, where we will want 298 * to see each group separately, that is why symbol__annotate2() 299 * sets src->nr_histograms to evsel->nr_members. 300 * @samples: Hash map of sym_hist_entry. Keyed by event index and offset in symbol. 301 * @nr_events: Number of events in the current output. 302 * @nr_entries: Number of annotated_line in the source list. 303 * @nr_asm_entries: Number of annotated_line with actual asm instruction in the 304 * source list. 305 * @max_jump_sources: Maximum number of jump instructions targeting to the same 306 * instruction. 307 * @widths: Precalculated width of each column in the TUI output. 308 * 309 * disasm_lines are allocated, percentages calculated and all sorted by percentage 310 * when the annotation is about to be presented, so the percentages are for 311 * one of the entries in the histogram array, i.e. for the event/counter being 312 * presented. It is deallocated right after symbol__{tui,tty,etc}_annotate 313 * returns. 314 */ 315 struct annotated_source { 316 struct list_head source; 317 struct sym_hist *histograms; 318 struct hashmap *samples; 319 int nr_histograms; 320 int nr_events; 321 int nr_entries; 322 int nr_asm_entries; 323 int max_jump_sources; 324 bool tried_source; 325 u64 start; 326 struct { 327 u8 addr; 328 u8 jumps; 329 u8 target; 330 u8 min_addr; 331 u8 max_addr; 332 u8 max_ins_name; 333 u16 max_line_len; 334 } widths; 335 }; 336 337 struct annotation_line *annotated_source__get_line(struct annotated_source *src, 338 s64 offset); 339 340 /* A branch counter once saturated */ 341 #define ANNOTATION__BR_CNTR_SATURATED_FLAG (1ULL << 63) 342 343 /** 344 * struct annotated_branch - basic block and IPC information for a symbol. 345 * 346 * @hit_cycles: Total executed cycles. 347 * @hit_insn: Total number of instructions executed. 348 * @total_insn: Number of instructions in the function. 349 * @cover_insn: Number of distinct, actually executed instructions. 350 * @cycles_hist: Array of cyc_hist for each instruction. 351 * @max_coverage: Maximum number of covered basic block (used for block-range). 352 * @br_cntr: Array of the occurrences of events (branch counters) during a block. 353 * 354 * This struct is used by two different codes when the sample has branch stack 355 * and cycles information. annotation__compute_ipc() calculates average IPC 356 * using @hit_insn / @hit_cycles. The actual coverage can be calculated using 357 * @cover_insn / @total_insn. The @cycles_hist can give IPC for each (longest) 358 * basic block ends at the given address. 359 * process_basic_block() calculates coverage of instructions (or basic blocks) 360 * in the function. 361 */ 362 struct annotated_branch { 363 u64 hit_cycles; 364 u64 hit_insn; 365 unsigned int total_insn; 366 unsigned int cover_insn; 367 struct cyc_hist *cycles_hist; 368 u64 max_coverage; 369 u64 *br_cntr; 370 }; 371 372 struct LOCKABLE annotation { 373 struct annotated_source *src; 374 struct annotated_branch *branch; 375 }; 376 377 static inline void annotation__init(struct annotation *notes __maybe_unused) 378 { 379 } 380 void annotation__exit(struct annotation *notes); 381 382 void annotation__lock(struct annotation *notes) EXCLUSIVE_LOCK_FUNCTION(*notes); 383 void annotation__unlock(struct annotation *notes) UNLOCK_FUNCTION(*notes); 384 bool annotation__trylock(struct annotation *notes) EXCLUSIVE_TRYLOCK_FUNCTION(true, *notes); 385 386 static inline int annotation__cycles_width(struct annotation *notes) 387 { 388 if (notes->branch && annotate_opts.show_minmax_cycle) 389 return ANNOTATION__IPC_WIDTH + ANNOTATION__MINMAX_CYCLES_WIDTH; 390 391 return notes->branch ? ANNOTATION__IPC_WIDTH + ANNOTATION__CYCLES_WIDTH : 0; 392 } 393 394 static inline u8 annotation__weight_mask(struct annotation *notes, 395 const struct evsel *evsel) 396 { 397 u8 mask = 0; 398 struct evsel *pos; 399 int i; 400 401 if (!symbol_conf.annotate_weight) 402 return 0; 403 404 if (evsel__is_group_event((struct evsel *)evsel)) { 405 pos = (struct evsel *)evsel; 406 for (i = 0; i < evsel->core.nr_members; i++) { 407 mask |= sym_hist__weight_mask(¬es->src->histograms[pos->core.idx]); 408 pos = evsel__next(pos); 409 } 410 return mask; 411 } 412 413 return sym_hist__weight_mask(¬es->src->histograms[evsel->core.idx]); 414 } 415 416 static inline int annotation__pcnt_width(struct annotation *notes, 417 const struct evsel *evsel) 418 { 419 int extra = hweight8(annotation__weight_mask(notes, evsel)) * 8; 420 return ((symbol_conf.show_total_period ? 12 : 8) + extra) * 421 notes->src->nr_events; 422 } 423 424 static inline bool annotation_line__filter(struct annotation_line *al) 425 { 426 return annotate_opts.hide_src_code && al->offset == -1; 427 } 428 429 static inline u8 annotation__br_cntr_width(void) 430 { 431 return annotate_opts.show_br_cntr ? ANNOTATION__BR_CNTR_WIDTH : 0; 432 } 433 434 void annotation__update_column_widths(struct annotation *notes); 435 void annotation__toggle_full_addr(struct annotation *notes, struct map_symbol *ms); 436 437 static inline struct sym_hist *annotated_source__histogram(struct annotated_source *src, 438 const struct evsel *evsel) 439 { 440 return &src->histograms[evsel->core.idx]; 441 } 442 443 static inline struct sym_hist *annotation__histogram(struct annotation *notes, 444 const struct evsel *evsel) 445 { 446 return annotated_source__histogram(notes->src, evsel); 447 } 448 449 struct sym_hist_entry * 450 annotated_source__hist_entry(struct annotated_source *src, const struct evsel *evsel, u64 offset); 451 452 static inline struct annotation *symbol__annotation(struct symbol *sym) 453 { 454 return (void *)sym - symbol_conf.priv_size; 455 } 456 457 int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, struct perf_sample *sample); 458 459 struct annotated_branch *annotation__get_branch(struct annotation *notes); 460 461 int addr_map_symbol__account_cycles(struct addr_map_symbol *ams, 462 struct addr_map_symbol *start, 463 unsigned cycles, 464 struct evsel *evsel, 465 u64 br_cntr); 466 467 int hist_entry__inc_addr_samples(struct hist_entry *he, struct perf_sample *sample, u64 addr); 468 469 struct annotated_source *symbol__hists(struct symbol *sym, int nr_hists); 470 void symbol__annotate_zero_histograms(struct symbol *sym); 471 472 int symbol__annotate(struct map_symbol *ms, 473 struct evsel *evsel, 474 const struct arch **parch); 475 int symbol__annotate2(struct map_symbol *ms, 476 struct evsel *evsel, 477 const struct arch **parch); 478 479 enum symbol_disassemble_errno { 480 SYMBOL_ANNOTATE_ERRNO__SUCCESS = 0, 481 482 /* 483 * Choose an arbitrary negative big number not to clash with standard 484 * errno since SUS requires the errno has distinct positive values. 485 * See 'Issue 6' in the link below. 486 * 487 * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/errno.h.html 488 */ 489 __SYMBOL_ANNOTATE_ERRNO__START = -10000, 490 491 SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX = __SYMBOL_ANNOTATE_ERRNO__START, 492 SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF, 493 SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_CPUID_PARSING, 494 SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_REGEXP, 495 SYMBOL_ANNOTATE_ERRNO__BPF_INVALID_FILE, 496 SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF, 497 SYMBOL_ANNOTATE_ERRNO__COULDNT_DETERMINE_FILE_TYPE, 498 499 __SYMBOL_ANNOTATE_ERRNO__END, 500 }; 501 502 int symbol__strerror_disassemble(struct map_symbol *ms, int errnum, char *buf, size_t buflen); 503 504 void symbol__annotate_zero_histogram(struct symbol *sym, struct evsel *evsel); 505 void symbol__annotate_decay_histogram(struct symbol *sym, struct evsel *evsel); 506 void annotated_source__purge(struct annotated_source *as); 507 508 int map_symbol__annotation_dump(struct map_symbol *ms, struct evsel *evsel, 509 struct hist_entry *he); 510 511 bool ui__has_annotation(void); 512 513 int hist_entry__annotate_printf(struct hist_entry *he, struct evsel *evsel); 514 int hist_entry__tty_annotate(struct hist_entry *he, struct evsel *evsel); 515 int hist_entry__tty_annotate2(struct hist_entry *he, struct evsel *evsel); 516 517 void annotation_options__init(void); 518 void annotation_options__exit(void); 519 520 void annotation_config__init(void); 521 522 int annotate_parse_percent_type(const struct option *opt, const char *_str, 523 int unset); 524 525 int annotate_check_args(void); 526 527 int arch__dwarf_regnum(const struct arch *arch, const char *str); 528 529 /** 530 * struct annotated_op_loc - Location info of instruction operand 531 * @reg1: First register in the operand 532 * @reg2: Second register in the operand 533 * @offset: Memory access offset in the operand 534 * @segment: Segment selector register 535 * @addr_mode: Addressing mode, only valid if @mem_ref is true 536 * @extend_type: Operand extension type (enum annotated_ext_type) 537 * @shift_type: Operand shift type (enum annotated_shift_type) 538 * @amount: Number of bits to shift 539 * @mem_ref: Whether the operand accesses memory 540 * @multi_regs: Whether the second register is used 541 * @imm: Whether the operand is an immediate value (in offset) 542 */ 543 struct annotated_op_loc { 544 int reg1; 545 int reg2; 546 int offset; 547 u8 segment; 548 u8 addr_mode; 549 u8 extend_type; 550 u8 shift_type; 551 u8 amount; 552 bool mem_ref; 553 bool multi_regs; 554 bool imm; 555 }; 556 557 enum annotated_insn_ops { 558 INSN_OP_SOURCE = 0, 559 INSN_OP_TARGET = 1, 560 561 INSN_OP_MAX, 562 }; 563 564 enum annotated_x86_segment { 565 INSN_SEG_NONE = 0, 566 567 INSN_SEG_X86_CS, 568 INSN_SEG_X86_DS, 569 INSN_SEG_X86_ES, 570 INSN_SEG_X86_FS, 571 INSN_SEG_X86_GS, 572 INSN_SEG_X86_SS, 573 }; 574 575 /* 576 * ARM64 addressing modes for memory operations. 577 * 578 * [Xn, #imm] -> SIGNED_OFFSET (base + offset, base unchanged) 579 * [Xn, #imm]! -> PRE_INDEX (base += offset, then access) 580 * [Xn], #imm -> POST_INDEX (access, then base += offset) 581 */ 582 enum annotated_addr_mode { 583 PERF_AAM_NONE = 0, 584 585 PERF_AAM_SIGNED_OFFSET, 586 PERF_AAM_PRE_INDEX, 587 PERF_AAM_POST_INDEX, 588 }; 589 590 /* 591 * ARM64 register extension types. 592 * UXT* = zero-extend, SXT* = sign-extend. 593 * B=8bit, H=16bit, W=32bit, X=64bit. 594 * 595 * Example: UXTW = zero-extend 32-bit Wn to 64-bit Xn 596 */ 597 enum annotated_ext_type { 598 PERF_EXT_NONE = 0, 599 600 PERF_EXT_UXTB, 601 PERF_EXT_UXTH, 602 PERF_EXT_UXTW, 603 PERF_EXT_UXTX, 604 PERF_EXT_SXTB, 605 PERF_EXT_SXTH, 606 PERF_EXT_SXTW, 607 PERF_EXT_SXTX, 608 }; 609 610 /* 611 * ARM64 operand shift types. 612 * LSL = logical shift left. 613 * LSR = logical shift right. 614 * ASR = arithmetic shift right. 615 * ROR = rotate right. 616 * 617 * Example: LSL #3 = shift the operand left by 3 bits. 618 */ 619 enum annotated_shift_type { 620 PERF_SHIFT_NONE = 0, 621 622 PERF_SHIFT_LSL, 623 PERF_SHIFT_LSR, 624 PERF_SHIFT_ASR, 625 PERF_SHIFT_ROR, 626 }; 627 628 /** 629 * struct annotated_insn_loc - Location info of instruction 630 * @ops: Array of location info for source and target operands 631 */ 632 struct annotated_insn_loc { 633 struct annotated_op_loc ops[INSN_OP_MAX]; 634 }; 635 636 #define for_each_insn_op_loc(insn_loc, i, op_loc) \ 637 for (i = INSN_OP_SOURCE, op_loc = &(insn_loc)->ops[i]; \ 638 i < INSN_OP_MAX; \ 639 i++, op_loc++) 640 641 /* Get detailed location info in the instruction */ 642 int annotate_get_insn_location(const struct arch *arch, struct disasm_line *dl, 643 struct annotated_insn_loc *loc); 644 645 /* Returns a data type from the sample instruction (if any) */ 646 struct annotated_data_type *hist_entry__get_data_type(struct hist_entry *he); 647 648 struct annotated_item_stat { 649 struct list_head list; 650 char *name; 651 int good; 652 int bad; 653 }; 654 extern struct list_head ann_insn_stat; 655 656 /* Calculate PC-relative address */ 657 u64 annotate_calc_pcrel(struct map_symbol *ms, u64 ip, int offset, 658 struct disasm_line *dl); 659 660 /** 661 * struct annotated_basic_block - Basic block of instructions 662 * @list: List node 663 * @begin: start instruction in the block 664 * @end: end instruction in the block 665 */ 666 struct annotated_basic_block { 667 struct list_head list; 668 struct disasm_line *begin; 669 struct disasm_line *end; 670 }; 671 672 /* Get a list of basic blocks from src to dst addresses */ 673 int annotate_get_basic_blocks(struct symbol *sym, s64 src, s64 dst, 674 struct list_head *head); 675 676 void debuginfo_cache__delete(void); 677 678 int annotation_br_cntr_entry(char **str, int br_cntr_nr, u64 *br_cntr, 679 int num_aggr, struct evsel *evsel); 680 int annotation_br_cntr_abbr_list(char **str, struct evsel *evsel, bool header); 681 682 683 int map_symbol__get_arch(struct map_symbol *ms, const struct arch **parch); 684 #endif /* __PERF_ANNOTATE_H */ 685