2 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
4 * Parts came from builtin-annotate.c, see those files for further
7 * Released under the GPL v2. (and only v2, not any later version)
22 #include <linux/bitops.h>
24 const char *disassembler_style;
25 const char *objdump_path;
26 static regex_t file_lineno;
28 static struct ins *ins__find(const char *name);
29 static int disasm_line__parse(char *line, char **namep, char **rawp);
31 static void ins__delete(struct ins_operands *ops)
35 zfree(&ops->source.raw);
36 zfree(&ops->source.name);
37 zfree(&ops->target.raw);
38 zfree(&ops->target.name);
41 static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size,
42 struct ins_operands *ops)
44 return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->raw);
47 int ins__scnprintf(struct ins *ins, char *bf, size_t size,
48 struct ins_operands *ops)
50 if (ins->ops->scnprintf)
51 return ins->ops->scnprintf(ins, bf, size, ops);
53 return ins__raw_scnprintf(ins, bf, size, ops);
56 static int call__parse(struct ins_operands *ops)
58 char *endptr, *tok, *name;
60 ops->target.addr = strtoull(ops->raw, &endptr, 16);
62 name = strchr(endptr, '<');
68 tok = strchr(name, '>');
73 ops->target.name = strdup(name);
76 return ops->target.name == NULL ? -1 : 0;
79 tok = strchr(endptr, '(');
85 tok = strchr(endptr, '*');
89 ops->target.addr = strtoull(tok + 1, NULL, 16);
93 static int call__scnprintf(struct ins *ins, char *bf, size_t size,
94 struct ins_operands *ops)
97 return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->target.name);
99 if (ops->target.addr == 0)
100 return ins__raw_scnprintf(ins, bf, size, ops);
102 return scnprintf(bf, size, "%-6.6s *%" PRIx64, ins->name, ops->target.addr);
105 static struct ins_ops call_ops = {
106 .parse = call__parse,
107 .scnprintf = call__scnprintf,
110 bool ins__is_call(const struct ins *ins)
112 return ins->ops == &call_ops;
115 static int jump__parse(struct ins_operands *ops)
117 const char *s = strchr(ops->raw, '+');
119 ops->target.addr = strtoull(ops->raw, NULL, 16);
122 ops->target.offset = strtoull(s, NULL, 16);
124 ops->target.offset = UINT64_MAX;
129 static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
130 struct ins_operands *ops)
132 return scnprintf(bf, size, "%-6.6s %" PRIx64, ins->name, ops->target.offset);
135 static struct ins_ops jump_ops = {
136 .parse = jump__parse,
137 .scnprintf = jump__scnprintf,
140 bool ins__is_jump(const struct ins *ins)
142 return ins->ops == &jump_ops;
145 static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep)
147 char *endptr, *name, *t;
149 if (strstr(raw, "(%rip)") == NULL)
152 *addrp = strtoull(comment, &endptr, 16);
153 name = strchr(endptr, '<');
159 t = strchr(name, '>');
164 *namep = strdup(name);
170 static int lock__parse(struct ins_operands *ops)
174 ops->locked.ops = zalloc(sizeof(*ops->locked.ops));
175 if (ops->locked.ops == NULL)
178 if (disasm_line__parse(ops->raw, &name, &ops->locked.ops->raw) < 0)
181 ops->locked.ins = ins__find(name);
184 if (ops->locked.ins == NULL)
187 if (!ops->locked.ins->ops)
190 if (ops->locked.ins->ops->parse &&
191 ops->locked.ins->ops->parse(ops->locked.ops) < 0)
197 zfree(&ops->locked.ops);
201 static int lock__scnprintf(struct ins *ins, char *bf, size_t size,
202 struct ins_operands *ops)
206 if (ops->locked.ins == NULL)
207 return ins__raw_scnprintf(ins, bf, size, ops);
209 printed = scnprintf(bf, size, "%-6.6s ", ins->name);
210 return printed + ins__scnprintf(ops->locked.ins, bf + printed,
211 size - printed, ops->locked.ops);
214 static void lock__delete(struct ins_operands *ops)
216 struct ins *ins = ops->locked.ins;
218 if (ins && ins->ops->free)
219 ins->ops->free(ops->locked.ops);
221 ins__delete(ops->locked.ops);
223 zfree(&ops->locked.ops);
224 zfree(&ops->target.raw);
225 zfree(&ops->target.name);
228 static struct ins_ops lock_ops = {
229 .free = lock__delete,
230 .parse = lock__parse,
231 .scnprintf = lock__scnprintf,
234 static int mov__parse(struct ins_operands *ops)
236 char *s = strchr(ops->raw, ','), *target, *comment, prev;
242 ops->source.raw = strdup(ops->raw);
245 if (ops->source.raw == NULL)
249 comment = strchr(s, '#');
254 s = strchr(s, '\0') - 1;
256 while (s > target && isspace(s[0]))
262 ops->target.raw = strdup(target);
265 if (ops->target.raw == NULL)
266 goto out_free_source;
271 while (comment[0] != '\0' && isspace(comment[0]))
274 comment__symbol(ops->source.raw, comment, &ops->source.addr, &ops->source.name);
275 comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name);
280 zfree(&ops->source.raw);
284 static int mov__scnprintf(struct ins *ins, char *bf, size_t size,
285 struct ins_operands *ops)
287 return scnprintf(bf, size, "%-6.6s %s,%s", ins->name,
288 ops->source.name ?: ops->source.raw,
289 ops->target.name ?: ops->target.raw);
292 static struct ins_ops mov_ops = {
294 .scnprintf = mov__scnprintf,
297 static int dec__parse(struct ins_operands *ops)
299 char *target, *comment, *s, prev;
301 target = s = ops->raw;
303 while (s[0] != '\0' && !isspace(s[0]))
308 ops->target.raw = strdup(target);
311 if (ops->target.raw == NULL)
314 comment = strchr(s, '#');
318 while (comment[0] != '\0' && isspace(comment[0]))
321 comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name);
326 static int dec__scnprintf(struct ins *ins, char *bf, size_t size,
327 struct ins_operands *ops)
329 return scnprintf(bf, size, "%-6.6s %s", ins->name,
330 ops->target.name ?: ops->target.raw);
333 static struct ins_ops dec_ops = {
335 .scnprintf = dec__scnprintf,
338 static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size,
339 struct ins_operands *ops __maybe_unused)
341 return scnprintf(bf, size, "%-6.6s", "nop");
344 static struct ins_ops nop_ops = {
345 .scnprintf = nop__scnprintf,
349 * Must be sorted by name!
351 static struct ins instructions[] = {
352 { .name = "add", .ops = &mov_ops, },
353 { .name = "addl", .ops = &mov_ops, },
354 { .name = "addq", .ops = &mov_ops, },
355 { .name = "addw", .ops = &mov_ops, },
356 { .name = "and", .ops = &mov_ops, },
357 { .name = "bts", .ops = &mov_ops, },
358 { .name = "call", .ops = &call_ops, },
359 { .name = "callq", .ops = &call_ops, },
360 { .name = "cmp", .ops = &mov_ops, },
361 { .name = "cmpb", .ops = &mov_ops, },
362 { .name = "cmpl", .ops = &mov_ops, },
363 { .name = "cmpq", .ops = &mov_ops, },
364 { .name = "cmpw", .ops = &mov_ops, },
365 { .name = "cmpxch", .ops = &mov_ops, },
366 { .name = "dec", .ops = &dec_ops, },
367 { .name = "decl", .ops = &dec_ops, },
368 { .name = "imul", .ops = &mov_ops, },
369 { .name = "inc", .ops = &dec_ops, },
370 { .name = "incl", .ops = &dec_ops, },
371 { .name = "ja", .ops = &jump_ops, },
372 { .name = "jae", .ops = &jump_ops, },
373 { .name = "jb", .ops = &jump_ops, },
374 { .name = "jbe", .ops = &jump_ops, },
375 { .name = "jc", .ops = &jump_ops, },
376 { .name = "jcxz", .ops = &jump_ops, },
377 { .name = "je", .ops = &jump_ops, },
378 { .name = "jecxz", .ops = &jump_ops, },
379 { .name = "jg", .ops = &jump_ops, },
380 { .name = "jge", .ops = &jump_ops, },
381 { .name = "jl", .ops = &jump_ops, },
382 { .name = "jle", .ops = &jump_ops, },
383 { .name = "jmp", .ops = &jump_ops, },
384 { .name = "jmpq", .ops = &jump_ops, },
385 { .name = "jna", .ops = &jump_ops, },
386 { .name = "jnae", .ops = &jump_ops, },
387 { .name = "jnb", .ops = &jump_ops, },
388 { .name = "jnbe", .ops = &jump_ops, },
389 { .name = "jnc", .ops = &jump_ops, },
390 { .name = "jne", .ops = &jump_ops, },
391 { .name = "jng", .ops = &jump_ops, },
392 { .name = "jnge", .ops = &jump_ops, },
393 { .name = "jnl", .ops = &jump_ops, },
394 { .name = "jnle", .ops = &jump_ops, },
395 { .name = "jno", .ops = &jump_ops, },
396 { .name = "jnp", .ops = &jump_ops, },
397 { .name = "jns", .ops = &jump_ops, },
398 { .name = "jnz", .ops = &jump_ops, },
399 { .name = "jo", .ops = &jump_ops, },
400 { .name = "jp", .ops = &jump_ops, },
401 { .name = "jpe", .ops = &jump_ops, },
402 { .name = "jpo", .ops = &jump_ops, },
403 { .name = "jrcxz", .ops = &jump_ops, },
404 { .name = "js", .ops = &jump_ops, },
405 { .name = "jz", .ops = &jump_ops, },
406 { .name = "lea", .ops = &mov_ops, },
407 { .name = "lock", .ops = &lock_ops, },
408 { .name = "mov", .ops = &mov_ops, },
409 { .name = "movb", .ops = &mov_ops, },
410 { .name = "movdqa",.ops = &mov_ops, },
411 { .name = "movl", .ops = &mov_ops, },
412 { .name = "movq", .ops = &mov_ops, },
413 { .name = "movslq", .ops = &mov_ops, },
414 { .name = "movzbl", .ops = &mov_ops, },
415 { .name = "movzwl", .ops = &mov_ops, },
416 { .name = "nop", .ops = &nop_ops, },
417 { .name = "nopl", .ops = &nop_ops, },
418 { .name = "nopw", .ops = &nop_ops, },
419 { .name = "or", .ops = &mov_ops, },
420 { .name = "orl", .ops = &mov_ops, },
421 { .name = "test", .ops = &mov_ops, },
422 { .name = "testb", .ops = &mov_ops, },
423 { .name = "testl", .ops = &mov_ops, },
424 { .name = "xadd", .ops = &mov_ops, },
425 { .name = "xbeginl", .ops = &jump_ops, },
426 { .name = "xbeginq", .ops = &jump_ops, },
429 static int ins__cmp(const void *name, const void *insp)
431 const struct ins *ins = insp;
433 return strcmp(name, ins->name);
436 static struct ins *ins__find(const char *name)
438 const int nmemb = ARRAY_SIZE(instructions);
440 return bsearch(name, instructions, nmemb, sizeof(struct ins), ins__cmp);
443 int symbol__annotate_init(struct map *map __maybe_unused, struct symbol *sym)
445 struct annotation *notes = symbol__annotation(sym);
446 pthread_mutex_init(¬es->lock, NULL);
450 int symbol__alloc_hist(struct symbol *sym)
452 struct annotation *notes = symbol__annotation(sym);
453 const size_t size = symbol__size(sym);
454 size_t sizeof_sym_hist;
456 /* Check for overflow when calculating sizeof_sym_hist */
457 if (size > (SIZE_MAX - sizeof(struct sym_hist)) / sizeof(u64))
460 sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64));
462 /* Check for overflow in zalloc argument */
463 if (sizeof_sym_hist > (SIZE_MAX - sizeof(*notes->src))
464 / symbol_conf.nr_events)
467 notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist);
468 if (notes->src == NULL)
470 notes->src->sizeof_sym_hist = sizeof_sym_hist;
471 notes->src->nr_histograms = symbol_conf.nr_events;
472 INIT_LIST_HEAD(¬es->src->source);
476 /* The cycles histogram is lazily allocated. */
477 static int symbol__alloc_hist_cycles(struct symbol *sym)
479 struct annotation *notes = symbol__annotation(sym);
480 const size_t size = symbol__size(sym);
482 notes->src->cycles_hist = calloc(size, sizeof(struct cyc_hist));
483 if (notes->src->cycles_hist == NULL)
488 void symbol__annotate_zero_histograms(struct symbol *sym)
490 struct annotation *notes = symbol__annotation(sym);
492 pthread_mutex_lock(¬es->lock);
493 if (notes->src != NULL) {
494 memset(notes->src->histograms, 0,
495 notes->src->nr_histograms * notes->src->sizeof_sym_hist);
496 if (notes->src->cycles_hist)
497 memset(notes->src->cycles_hist, 0,
498 symbol__size(sym) * sizeof(struct cyc_hist));
500 pthread_mutex_unlock(¬es->lock);
503 static int __symbol__account_cycles(struct annotation *notes,
505 unsigned offset, unsigned cycles,
510 ch = notes->src->cycles_hist;
512 * For now we can only account one basic block per
513 * final jump. But multiple could be overlapping.
514 * Always account the longest one. So when
515 * a shorter one has been already seen throw it away.
517 * We separately always account the full cycles.
519 ch[offset].num_aggr++;
520 ch[offset].cycles_aggr += cycles;
522 if (!have_start && ch[offset].have_start)
524 if (ch[offset].num) {
525 if (have_start && (!ch[offset].have_start ||
526 ch[offset].start > start)) {
527 ch[offset].have_start = 0;
528 ch[offset].cycles = 0;
530 if (ch[offset].reset < 0xffff)
532 } else if (have_start &&
533 ch[offset].start < start)
536 ch[offset].have_start = have_start;
537 ch[offset].start = start;
538 ch[offset].cycles += cycles;
543 static int __symbol__inc_addr_samples(struct symbol *sym, struct map *map,
544 struct annotation *notes, int evidx, u64 addr)
549 pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
551 if (addr < sym->start || addr >= sym->end)
554 offset = addr - sym->start;
555 h = annotation__histogram(notes, evidx);
559 pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
560 ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name,
561 addr, addr - sym->start, evidx, h->addr[offset]);
565 static struct annotation *symbol__get_annotation(struct symbol *sym, bool cycles)
567 struct annotation *notes = symbol__annotation(sym);
569 if (notes->src == NULL) {
570 if (symbol__alloc_hist(sym) < 0)
573 if (!notes->src->cycles_hist && cycles) {
574 if (symbol__alloc_hist_cycles(sym) < 0)
580 static int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
583 struct annotation *notes;
587 notes = symbol__get_annotation(sym, false);
590 return __symbol__inc_addr_samples(sym, map, notes, evidx, addr);
593 static int symbol__account_cycles(u64 addr, u64 start,
594 struct symbol *sym, unsigned cycles)
596 struct annotation *notes;
601 notes = symbol__get_annotation(sym, true);
604 if (addr < sym->start || addr >= sym->end)
608 if (start < sym->start || start >= sym->end)
613 offset = addr - sym->start;
614 return __symbol__account_cycles(notes,
615 start ? start - sym->start : 0,
620 int addr_map_symbol__account_cycles(struct addr_map_symbol *ams,
621 struct addr_map_symbol *start,
631 * Only set start when IPC can be computed. We can only
632 * compute it when the basic block is completely in a single
634 * Special case the case when the jump is elsewhere, but
635 * it starts on the function start.
638 (start->sym == ams->sym ||
640 start->addr == ams->sym->start + ams->map->start)))
641 saddr = start->al_addr;
643 pr_debug2("BB with bad start: addr %"PRIx64" start %"PRIx64" sym %"PRIx64" saddr %"PRIx64"\n",
645 start ? start->addr : 0,
646 ams->sym ? ams->sym->start + ams->map->start : 0,
648 err = symbol__account_cycles(ams->al_addr, saddr, ams->sym, cycles);
650 pr_debug2("account_cycles failed %d\n", err);
654 int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, int evidx)
656 return symbol__inc_addr_samples(ams->sym, ams->map, evidx, ams->al_addr);
659 int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
661 return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
664 static void disasm_line__init_ins(struct disasm_line *dl)
666 dl->ins = ins__find(dl->name);
674 if (dl->ins->ops->parse && dl->ins->ops->parse(&dl->ops) < 0)
678 static int disasm_line__parse(char *line, char **namep, char **rawp)
680 char *name = line, tmp;
682 while (isspace(name[0]))
690 while ((*rawp)[0] != '\0' && !isspace((*rawp)[0]))
695 *namep = strdup(name);
702 if ((*rawp)[0] != '\0') {
704 while (isspace((*rawp)[0]))
715 static struct disasm_line *disasm_line__new(s64 offset, char *line,
716 size_t privsize, int line_nr)
718 struct disasm_line *dl = zalloc(sizeof(*dl) + privsize);
722 dl->line = strdup(line);
723 dl->line_nr = line_nr;
724 if (dl->line == NULL)
728 if (disasm_line__parse(dl->line, &dl->name, &dl->ops.raw) < 0)
731 disasm_line__init_ins(dl);
744 void disasm_line__free(struct disasm_line *dl)
748 if (dl->ins && dl->ins->ops->free)
749 dl->ins->ops->free(&dl->ops);
751 ins__delete(&dl->ops);
755 int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw)
758 return scnprintf(bf, size, "%-6.6s %s", dl->name, dl->ops.raw);
760 return ins__scnprintf(dl->ins, bf, size, &dl->ops);
763 static void disasm__add(struct list_head *head, struct disasm_line *line)
765 list_add_tail(&line->node, head);
768 struct disasm_line *disasm__get_next_ip_line(struct list_head *head, struct disasm_line *pos)
770 list_for_each_entry_continue(pos, head, node)
771 if (pos->offset >= 0)
777 double disasm__calc_percent(struct annotation *notes, int evidx, s64 offset,
778 s64 end, const char **path, u64 *nr_samples)
780 struct source_line *src_line = notes->src->lines;
781 double percent = 0.0;
785 size_t sizeof_src_line = sizeof(*src_line) +
786 sizeof(src_line->samples) * (src_line->nr_pcnt - 1);
788 while (offset < end) {
789 src_line = (void *)notes->src->lines +
790 (sizeof_src_line * offset);
793 *path = src_line->path;
795 percent += src_line->samples[evidx].percent;
796 *nr_samples += src_line->samples[evidx].nr;
800 struct sym_hist *h = annotation__histogram(notes, evidx);
801 unsigned int hits = 0;
804 hits += h->addr[offset++];
808 percent = 100.0 * hits / h->sum;
815 static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 start,
816 struct perf_evsel *evsel, u64 len, int min_pcnt, int printed,
817 int max_lines, struct disasm_line *queue)
819 static const char *prev_line;
820 static const char *prev_color;
822 if (dl->offset != -1) {
823 const char *path = NULL;
825 double percent, max_percent = 0.0;
826 double *ppercents = &percent;
827 u64 *psamples = &nr_samples;
828 int i, nr_percent = 1;
830 struct annotation *notes = symbol__annotation(sym);
831 s64 offset = dl->offset;
832 const u64 addr = start + offset;
833 struct disasm_line *next;
835 next = disasm__get_next_ip_line(¬es->src->source, dl);
837 if (perf_evsel__is_group_event(evsel)) {
838 nr_percent = evsel->nr_members;
839 ppercents = calloc(nr_percent, sizeof(double));
840 psamples = calloc(nr_percent, sizeof(u64));
841 if (ppercents == NULL || psamples == NULL) {
846 for (i = 0; i < nr_percent; i++) {
847 percent = disasm__calc_percent(notes,
848 notes->src->lines ? i : evsel->idx + i,
850 next ? next->offset : (s64) len,
853 ppercents[i] = percent;
854 psamples[i] = nr_samples;
855 if (percent > max_percent)
856 max_percent = percent;
859 if (max_percent < min_pcnt)
862 if (max_lines && printed >= max_lines)
866 list_for_each_entry_from(queue, ¬es->src->source, node) {
869 disasm_line__print(queue, sym, start, evsel, len,
874 color = get_percent_color(max_percent);
877 * Also color the filename and line if needed, with
878 * the same color than the percentage. Don't print it
879 * twice for close colored addr with the same filename:line
882 if (!prev_line || strcmp(prev_line, path)
883 || color != prev_color) {
884 color_fprintf(stdout, color, " %s", path);
890 for (i = 0; i < nr_percent; i++) {
891 percent = ppercents[i];
892 nr_samples = psamples[i];
893 color = get_percent_color(percent);
895 if (symbol_conf.show_total_period)
896 color_fprintf(stdout, color, " %7" PRIu64,
899 color_fprintf(stdout, color, " %7.2f", percent);
903 color_fprintf(stdout, PERF_COLOR_MAGENTA, " %" PRIx64 ":", addr);
904 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", dl->line);
906 if (ppercents != &percent)
909 if (psamples != &nr_samples)
912 } else if (max_lines && printed >= max_lines)
920 if (perf_evsel__is_group_event(evsel))
921 width *= evsel->nr_members;
924 printf(" %*s:\n", width, " ");
926 printf(" %*s: %s\n", width, " ", dl->line);
933 * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw)
934 * which looks like following
936 * 0000000000415500 <_init>:
937 * 415500: sub $0x8,%rsp
938 * 415504: mov 0x2f5ad5(%rip),%rax # 70afe0 <_DYNAMIC+0x2f8>
939 * 41550b: test %rax,%rax
940 * 41550e: je 415515 <_init+0x15>
941 * 415510: callq 416e70 <__gmon_start__@plt>
942 * 415515: add $0x8,%rsp
945 * it will be parsed and saved into struct disasm_line as
946 * <offset> <name> <ops.raw>
948 * The offset will be a relative offset from the start of the symbol and -1
949 * means that it's not a disassembly line so should be treated differently.
950 * The ops.raw part will be parsed further according to type of the instruction.
952 static int symbol__parse_objdump_line(struct symbol *sym, struct map *map,
953 FILE *file, size_t privsize,
956 struct annotation *notes = symbol__annotation(sym);
957 struct disasm_line *dl;
958 char *line = NULL, *parsed_line, *tmp, *tmp2, *c;
960 s64 line_ip, offset = -1;
963 if (getline(&line, &line_len, file) < 0)
969 while (line_len != 0 && isspace(line[line_len - 1]))
970 line[--line_len] = '\0';
972 c = strchr(line, '\n');
979 /* /filename:linenr ? Save line number and ignore. */
980 if (regexec(&file_lineno, line, 2, match, 0) == 0) {
981 *line_nr = atoi(line + match[1].rm_so);
986 * Strip leading spaces:
997 * Parse hexa addresses followed by ':'
999 line_ip = strtoull(tmp, &tmp2, 16);
1000 if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
1004 if (line_ip != -1) {
1005 u64 start = map__rip_2objdump(map, sym->start),
1006 end = map__rip_2objdump(map, sym->end);
1008 offset = line_ip - start;
1009 if ((u64)line_ip < start || (u64)line_ip >= end)
1012 parsed_line = tmp2 + 1;
1015 dl = disasm_line__new(offset, parsed_line, privsize, *line_nr);
1022 if (dl->ops.target.offset == UINT64_MAX)
1023 dl->ops.target.offset = dl->ops.target.addr -
1024 map__rip_2objdump(map, sym->start);
1026 /* kcore has no symbols, so add the call target name */
1027 if (dl->ins && ins__is_call(dl->ins) && !dl->ops.target.name) {
1028 struct addr_map_symbol target = {
1030 .addr = dl->ops.target.addr,
1033 if (!map_groups__find_ams(&target, NULL) &&
1034 target.sym->start == target.al_addr)
1035 dl->ops.target.name = strdup(target.sym->name);
1038 disasm__add(¬es->src->source, dl);
1043 static __attribute__((constructor)) void symbol__init_regexpr(void)
1045 regcomp(&file_lineno, "^/[^:]+:([0-9]+)", REG_EXTENDED);
1048 static void delete_last_nop(struct symbol *sym)
1050 struct annotation *notes = symbol__annotation(sym);
1051 struct list_head *list = ¬es->src->source;
1052 struct disasm_line *dl;
1054 while (!list_empty(list)) {
1055 dl = list_entry(list->prev, struct disasm_line, node);
1057 if (dl->ins && dl->ins->ops) {
1058 if (dl->ins->ops != &nop_ops)
1061 if (!strstr(dl->line, " nop ") &&
1062 !strstr(dl->line, " nopl ") &&
1063 !strstr(dl->line, " nopw "))
1067 list_del(&dl->node);
1068 disasm_line__free(dl);
1072 int symbol__annotate(struct symbol *sym, struct map *map, size_t privsize)
1074 struct dso *dso = map->dso;
1075 char *filename = dso__build_id_filename(dso, NULL, 0);
1076 bool free_filename = true;
1077 char command[PATH_MAX * 2];
1080 char symfs_filename[PATH_MAX];
1081 struct kcore_extract kce;
1082 bool delete_extract = false;
1086 symbol__join_symfs(symfs_filename, filename);
1088 if (filename == NULL) {
1089 if (dso->has_build_id) {
1090 pr_err("Can't annotate %s: not enough memory\n",
1095 } else if (dso__is_kcore(dso)) {
1097 } else if (readlink(symfs_filename, command, sizeof(command)) < 0 ||
1098 strstr(command, "[kernel.kallsyms]") ||
1099 access(symfs_filename, R_OK)) {
1103 * If we don't have build-ids or the build-id file isn't in the
1104 * cache, or is just a kallsyms file, well, lets hope that this
1105 * DSO is the same as when 'perf record' ran.
1107 filename = (char *)dso->long_name;
1108 symbol__join_symfs(symfs_filename, filename);
1109 free_filename = false;
1112 if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
1113 !dso__is_kcore(dso)) {
1114 char bf[BUILD_ID_SIZE * 2 + 16] = " with build id ";
1115 char *build_id_msg = NULL;
1117 if (dso->annotate_warned)
1118 goto out_free_filename;
1120 if (dso->has_build_id) {
1121 build_id__sprintf(dso->build_id,
1122 sizeof(dso->build_id), bf + 15);
1126 dso->annotate_warned = 1;
1127 pr_err("Can't annotate %s:\n\n"
1128 "No vmlinux file%s\nwas found in the path.\n\n"
1129 "Note that annotation using /proc/kcore requires CAP_SYS_RAWIO capability.\n\n"
1131 " perf buildid-cache -vu vmlinux\n\n"
1133 " --vmlinux vmlinux\n",
1134 sym->name, build_id_msg ?: "");
1135 goto out_free_filename;
1138 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
1139 filename, sym->name, map->unmap_ip(map, sym->start),
1140 map->unmap_ip(map, sym->end));
1142 pr_debug("annotating [%p] %30s : [%p] %30s\n",
1143 dso, dso->long_name, sym, sym->name);
1145 if (dso__is_kcore(dso)) {
1146 kce.kcore_filename = symfs_filename;
1147 kce.addr = map__rip_2objdump(map, sym->start);
1148 kce.offs = sym->start;
1149 kce.len = sym->end - sym->start;
1150 if (!kcore_extract__create(&kce)) {
1151 delete_extract = true;
1152 strlcpy(symfs_filename, kce.extract_filename,
1153 sizeof(symfs_filename));
1154 if (free_filename) {
1156 free_filename = false;
1158 filename = symfs_filename;
1160 } else if (dso__needs_decompress(dso)) {
1166 if (kmod_path__parse_ext(&m, symfs_filename))
1167 goto out_free_filename;
1169 snprintf(tmp, PATH_MAX, "/tmp/perf-kmod-XXXXXX");
1174 goto out_free_filename;
1177 ret = decompress_to_file(m.ext, symfs_filename, fd);
1183 goto out_free_filename;
1185 strcpy(symfs_filename, tmp);
1188 snprintf(command, sizeof(command),
1189 "%s %s%s --start-address=0x%016" PRIx64
1190 " --stop-address=0x%016" PRIx64
1191 " -l -d %s %s -C %s 2>/dev/null|grep -v %s|expand",
1192 objdump_path ? objdump_path : "objdump",
1193 disassembler_style ? "-M " : "",
1194 disassembler_style ? disassembler_style : "",
1195 map__rip_2objdump(map, sym->start),
1196 map__rip_2objdump(map, sym->end),
1197 symbol_conf.annotate_asm_raw ? "" : "--no-show-raw",
1198 symbol_conf.annotate_src ? "-S" : "",
1199 symfs_filename, filename);
1201 pr_debug("Executing: %s\n", command);
1203 file = popen(command, "r");
1205 goto out_remove_tmp;
1208 if (symbol__parse_objdump_line(sym, map, file, privsize,
1213 * kallsyms does not have symbol sizes so there may a nop at the end.
1216 if (dso__is_kcore(dso))
1217 delete_last_nop(sym);
1222 if (dso__needs_decompress(dso))
1223 unlink(symfs_filename);
1226 kcore_extract__delete(&kce);
1232 static void insert_source_line(struct rb_root *root, struct source_line *src_line)
1234 struct source_line *iter;
1235 struct rb_node **p = &root->rb_node;
1236 struct rb_node *parent = NULL;
1239 while (*p != NULL) {
1241 iter = rb_entry(parent, struct source_line, node);
1243 ret = strcmp(iter->path, src_line->path);
1245 for (i = 0; i < src_line->nr_pcnt; i++)
1246 iter->samples[i].percent_sum += src_line->samples[i].percent;
1253 p = &(*p)->rb_right;
1256 for (i = 0; i < src_line->nr_pcnt; i++)
1257 src_line->samples[i].percent_sum = src_line->samples[i].percent;
1259 rb_link_node(&src_line->node, parent, p);
1260 rb_insert_color(&src_line->node, root);
1263 static int cmp_source_line(struct source_line *a, struct source_line *b)
1267 for (i = 0; i < a->nr_pcnt; i++) {
1268 if (a->samples[i].percent_sum == b->samples[i].percent_sum)
1270 return a->samples[i].percent_sum > b->samples[i].percent_sum;
1276 static void __resort_source_line(struct rb_root *root, struct source_line *src_line)
1278 struct source_line *iter;
1279 struct rb_node **p = &root->rb_node;
1280 struct rb_node *parent = NULL;
1282 while (*p != NULL) {
1284 iter = rb_entry(parent, struct source_line, node);
1286 if (cmp_source_line(src_line, iter))
1289 p = &(*p)->rb_right;
1292 rb_link_node(&src_line->node, parent, p);
1293 rb_insert_color(&src_line->node, root);
1296 static void resort_source_line(struct rb_root *dest_root, struct rb_root *src_root)
1298 struct source_line *src_line;
1299 struct rb_node *node;
1301 node = rb_first(src_root);
1303 struct rb_node *next;
1305 src_line = rb_entry(node, struct source_line, node);
1306 next = rb_next(node);
1307 rb_erase(node, src_root);
1309 __resort_source_line(dest_root, src_line);
1314 static void symbol__free_source_line(struct symbol *sym, int len)
1316 struct annotation *notes = symbol__annotation(sym);
1317 struct source_line *src_line = notes->src->lines;
1318 size_t sizeof_src_line;
1321 sizeof_src_line = sizeof(*src_line) +
1322 (sizeof(src_line->samples) * (src_line->nr_pcnt - 1));
1324 for (i = 0; i < len; i++) {
1325 free_srcline(src_line->path);
1326 src_line = (void *)src_line + sizeof_src_line;
1329 zfree(¬es->src->lines);
1332 /* Get the filename:line for the colored entries */
1333 static int symbol__get_source_line(struct symbol *sym, struct map *map,
1334 struct perf_evsel *evsel,
1335 struct rb_root *root, int len)
1339 int evidx = evsel->idx;
1340 struct source_line *src_line;
1341 struct annotation *notes = symbol__annotation(sym);
1342 struct sym_hist *h = annotation__histogram(notes, evidx);
1343 struct rb_root tmp_root = RB_ROOT;
1346 size_t sizeof_src_line = sizeof(struct source_line);
1348 if (perf_evsel__is_group_event(evsel)) {
1349 for (i = 1; i < evsel->nr_members; i++) {
1350 h = annotation__histogram(notes, evidx + i);
1353 nr_pcnt = evsel->nr_members;
1354 sizeof_src_line += (nr_pcnt - 1) * sizeof(src_line->samples);
1360 src_line = notes->src->lines = calloc(len, sizeof_src_line);
1361 if (!notes->src->lines)
1364 start = map__rip_2objdump(map, sym->start);
1366 for (i = 0; i < len; i++) {
1368 double percent_max = 0.0;
1370 src_line->nr_pcnt = nr_pcnt;
1372 for (k = 0; k < nr_pcnt; k++) {
1373 h = annotation__histogram(notes, evidx + k);
1374 src_line->samples[k].percent = 100.0 * h->addr[i] / h->sum;
1376 if (src_line->samples[k].percent > percent_max)
1377 percent_max = src_line->samples[k].percent;
1380 if (percent_max <= 0.5)
1384 src_line->path = get_srcline(map->dso, offset, NULL, false);
1385 insert_source_line(&tmp_root, src_line);
1388 src_line = (void *)src_line + sizeof_src_line;
1391 resort_source_line(root, &tmp_root);
1395 static void print_summary(struct rb_root *root, const char *filename)
1397 struct source_line *src_line;
1398 struct rb_node *node;
1400 printf("\nSorted summary for file %s\n", filename);
1401 printf("----------------------------------------------\n\n");
1403 if (RB_EMPTY_ROOT(root)) {
1404 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
1408 node = rb_first(root);
1410 double percent, percent_max = 0.0;
1415 src_line = rb_entry(node, struct source_line, node);
1416 for (i = 0; i < src_line->nr_pcnt; i++) {
1417 percent = src_line->samples[i].percent_sum;
1418 color = get_percent_color(percent);
1419 color_fprintf(stdout, color, " %7.2f", percent);
1421 if (percent > percent_max)
1422 percent_max = percent;
1425 path = src_line->path;
1426 color = get_percent_color(percent_max);
1427 color_fprintf(stdout, color, " %s\n", path);
1429 node = rb_next(node);
1433 static void symbol__annotate_hits(struct symbol *sym, struct perf_evsel *evsel)
1435 struct annotation *notes = symbol__annotation(sym);
1436 struct sym_hist *h = annotation__histogram(notes, evsel->idx);
1437 u64 len = symbol__size(sym), offset;
1439 for (offset = 0; offset < len; ++offset)
1440 if (h->addr[offset] != 0)
1441 printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
1442 sym->start + offset, h->addr[offset]);
1443 printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum);
1446 int symbol__annotate_printf(struct symbol *sym, struct map *map,
1447 struct perf_evsel *evsel, bool full_paths,
1448 int min_pcnt, int max_lines, int context)
1450 struct dso *dso = map->dso;
1452 const char *d_filename;
1453 const char *evsel_name = perf_evsel__name(evsel);
1454 struct annotation *notes = symbol__annotation(sym);
1455 struct disasm_line *pos, *queue = NULL;
1456 u64 start = map__rip_2objdump(map, sym->start);
1457 int printed = 2, queue_len = 0;
1461 int namelen, evsel_name_len, graph_dotted_len;
1463 filename = strdup(dso->long_name);
1468 d_filename = filename;
1470 d_filename = basename(filename);
1472 len = symbol__size(sym);
1473 namelen = strlen(d_filename);
1474 evsel_name_len = strlen(evsel_name);
1476 if (perf_evsel__is_group_event(evsel))
1477 width *= evsel->nr_members;
1479 printf(" %-*.*s| Source code & Disassembly of %s for %s\n",
1480 width, width, "Percent", d_filename, evsel_name);
1482 graph_dotted_len = width + namelen + evsel_name_len;
1483 printf("-%-*.*s-----------------------------------------\n",
1484 graph_dotted_len, graph_dotted_len, graph_dotted_line);
1487 symbol__annotate_hits(sym, evsel);
1489 list_for_each_entry(pos, ¬es->src->source, node) {
1490 if (context && queue == NULL) {
1495 switch (disasm_line__print(pos, sym, start, evsel, len,
1496 min_pcnt, printed, max_lines,
1501 printed += queue_len;
1507 /* filtered by max_lines */
1513 * Filtered by min_pcnt or non IP lines when
1518 if (queue_len == context)
1519 queue = list_entry(queue->node.next, typeof(*queue), node);
1531 void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
1533 struct annotation *notes = symbol__annotation(sym);
1534 struct sym_hist *h = annotation__histogram(notes, evidx);
1536 memset(h, 0, notes->src->sizeof_sym_hist);
1539 void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
1541 struct annotation *notes = symbol__annotation(sym);
1542 struct sym_hist *h = annotation__histogram(notes, evidx);
1543 int len = symbol__size(sym), offset;
1546 for (offset = 0; offset < len; ++offset) {
1547 h->addr[offset] = h->addr[offset] * 7 / 8;
1548 h->sum += h->addr[offset];
1552 void disasm__purge(struct list_head *head)
1554 struct disasm_line *pos, *n;
1556 list_for_each_entry_safe(pos, n, head, node) {
1557 list_del(&pos->node);
1558 disasm_line__free(pos);
1562 static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp)
1566 if (dl->offset == -1)
1567 return fprintf(fp, "%s\n", dl->line);
1569 printed = fprintf(fp, "%#" PRIx64 " %s", dl->offset, dl->name);
1571 if (dl->ops.raw[0] != '\0') {
1572 printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
1576 return printed + fprintf(fp, "\n");
1579 size_t disasm__fprintf(struct list_head *head, FILE *fp)
1581 struct disasm_line *pos;
1584 list_for_each_entry(pos, head, node)
1585 printed += disasm_line__fprintf(pos, fp);
1590 int symbol__tty_annotate(struct symbol *sym, struct map *map,
1591 struct perf_evsel *evsel, bool print_lines,
1592 bool full_paths, int min_pcnt, int max_lines)
1594 struct dso *dso = map->dso;
1595 struct rb_root source_line = RB_ROOT;
1598 if (symbol__annotate(sym, map, 0) < 0)
1601 len = symbol__size(sym);
1604 symbol__get_source_line(sym, map, evsel, &source_line, len);
1605 print_summary(&source_line, dso->long_name);
1608 symbol__annotate_printf(sym, map, evsel, full_paths,
1609 min_pcnt, max_lines, 0);
1611 symbol__free_source_line(sym, len);
1613 disasm__purge(&symbol__annotation(sym)->src->source);
1618 int hist_entry__annotate(struct hist_entry *he, size_t privsize)
1620 return symbol__annotate(he->ms.sym, he->ms.map, privsize);
1623 bool ui__has_annotation(void)
1625 return use_browser == 1 && sort__has_sym;