xref: /openbmc/linux/tools/perf/util/annotate.c (revision e795dd42b716ff36ebaa5384fd1be8458d6c9c34)
1 /*
2  * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
3  *
4  * Parts came from builtin-annotate.c, see those files for further
5  * copyright notes.
6  *
7  * Released under the GPL v2. (and only v2, not any later version)
8  */
9 
10 #include <errno.h>
11 #include <inttypes.h>
12 #include "util.h"
13 #include "ui/ui.h"
14 #include "sort.h"
15 #include "build-id.h"
16 #include "color.h"
17 #include "cache.h"
18 #include "symbol.h"
19 #include "debug.h"
20 #include "annotate.h"
21 #include "evsel.h"
22 #include "block-range.h"
23 #include "string2.h"
24 #include "arch/common.h"
25 #include <regex.h>
26 #include <pthread.h>
27 #include <linux/bitops.h>
28 #include <linux/kernel.h>
29 #include <sys/utsname.h>
30 
31 #include "sane_ctype.h"
32 
33 const char 	*disassembler_style;
34 const char	*objdump_path;
35 static regex_t	 file_lineno;
36 
37 static struct ins_ops *ins__find(struct arch *arch, const char *name);
38 static void ins__sort(struct arch *arch);
39 static int disasm_line__parse(char *line, const char **namep, char **rawp);
40 
41 struct arch {
42 	const char	*name;
43 	struct ins	*instructions;
44 	size_t		nr_instructions;
45 	size_t		nr_instructions_allocated;
46 	struct ins_ops  *(*associate_instruction_ops)(struct arch *arch, const char *name);
47 	bool		sorted_instructions;
48 	bool		initialized;
49 	void		*priv;
50 	unsigned int	model;
51 	unsigned int	family;
52 	int		(*init)(struct arch *arch, char *cpuid);
53 	bool		(*ins_is_fused)(struct arch *arch, const char *ins1,
54 					const char *ins2);
55 	struct		{
56 		char comment_char;
57 		char skip_functions_char;
58 	} objdump;
59 };
60 
61 static struct ins_ops call_ops;
62 static struct ins_ops dec_ops;
63 static struct ins_ops jump_ops;
64 static struct ins_ops mov_ops;
65 static struct ins_ops nop_ops;
66 static struct ins_ops lock_ops;
67 static struct ins_ops ret_ops;
68 
69 static int arch__grow_instructions(struct arch *arch)
70 {
71 	struct ins *new_instructions;
72 	size_t new_nr_allocated;
73 
74 	if (arch->nr_instructions_allocated == 0 && arch->instructions)
75 		goto grow_from_non_allocated_table;
76 
77 	new_nr_allocated = arch->nr_instructions_allocated + 128;
78 	new_instructions = realloc(arch->instructions, new_nr_allocated * sizeof(struct ins));
79 	if (new_instructions == NULL)
80 		return -1;
81 
82 out_update_instructions:
83 	arch->instructions = new_instructions;
84 	arch->nr_instructions_allocated = new_nr_allocated;
85 	return 0;
86 
87 grow_from_non_allocated_table:
88 	new_nr_allocated = arch->nr_instructions + 128;
89 	new_instructions = calloc(new_nr_allocated, sizeof(struct ins));
90 	if (new_instructions == NULL)
91 		return -1;
92 
93 	memcpy(new_instructions, arch->instructions, arch->nr_instructions);
94 	goto out_update_instructions;
95 }
96 
97 static int arch__associate_ins_ops(struct arch* arch, const char *name, struct ins_ops *ops)
98 {
99 	struct ins *ins;
100 
101 	if (arch->nr_instructions == arch->nr_instructions_allocated &&
102 	    arch__grow_instructions(arch))
103 		return -1;
104 
105 	ins = &arch->instructions[arch->nr_instructions];
106 	ins->name = strdup(name);
107 	if (!ins->name)
108 		return -1;
109 
110 	ins->ops  = ops;
111 	arch->nr_instructions++;
112 
113 	ins__sort(arch);
114 	return 0;
115 }
116 
117 #include "arch/arm/annotate/instructions.c"
118 #include "arch/arm64/annotate/instructions.c"
119 #include "arch/x86/annotate/instructions.c"
120 #include "arch/powerpc/annotate/instructions.c"
121 #include "arch/s390/annotate/instructions.c"
122 
123 static struct arch architectures[] = {
124 	{
125 		.name = "arm",
126 		.init = arm__annotate_init,
127 	},
128 	{
129 		.name = "arm64",
130 		.init = arm64__annotate_init,
131 	},
132 	{
133 		.name = "x86",
134 		.init = x86__annotate_init,
135 		.instructions = x86__instructions,
136 		.nr_instructions = ARRAY_SIZE(x86__instructions),
137 		.ins_is_fused = x86__ins_is_fused,
138 		.objdump =  {
139 			.comment_char = '#',
140 		},
141 	},
142 	{
143 		.name = "powerpc",
144 		.init = powerpc__annotate_init,
145 	},
146 	{
147 		.name = "s390",
148 		.init = s390__annotate_init,
149 		.objdump =  {
150 			.comment_char = '#',
151 		},
152 	},
153 };
154 
155 static void ins__delete(struct ins_operands *ops)
156 {
157 	if (ops == NULL)
158 		return;
159 	zfree(&ops->source.raw);
160 	zfree(&ops->source.name);
161 	zfree(&ops->target.raw);
162 	zfree(&ops->target.name);
163 }
164 
165 static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size,
166 			      struct ins_operands *ops)
167 {
168 	return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->raw);
169 }
170 
171 int ins__scnprintf(struct ins *ins, char *bf, size_t size,
172 		  struct ins_operands *ops)
173 {
174 	if (ins->ops->scnprintf)
175 		return ins->ops->scnprintf(ins, bf, size, ops);
176 
177 	return ins__raw_scnprintf(ins, bf, size, ops);
178 }
179 
180 bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2)
181 {
182 	if (!arch || !arch->ins_is_fused)
183 		return false;
184 
185 	return arch->ins_is_fused(arch, ins1, ins2);
186 }
187 
188 static int call__parse(struct arch *arch, struct ins_operands *ops, struct map *map)
189 {
190 	char *endptr, *tok, *name;
191 
192 	ops->target.addr = strtoull(ops->raw, &endptr, 16);
193 
194 	name = strchr(endptr, '<');
195 	if (name == NULL)
196 		goto indirect_call;
197 
198 	name++;
199 
200 	if (arch->objdump.skip_functions_char &&
201 	    strchr(name, arch->objdump.skip_functions_char))
202 		return -1;
203 
204 	tok = strchr(name, '>');
205 	if (tok == NULL)
206 		return -1;
207 
208 	*tok = '\0';
209 	ops->target.name = strdup(name);
210 	*tok = '>';
211 
212 	return ops->target.name == NULL ? -1 : 0;
213 
214 indirect_call:
215 	tok = strchr(endptr, '*');
216 	if (tok == NULL) {
217 		struct symbol *sym = map__find_symbol(map, map->map_ip(map, ops->target.addr));
218 		if (sym != NULL)
219 			ops->target.name = strdup(sym->name);
220 		else
221 			ops->target.addr = 0;
222 		return 0;
223 	}
224 
225 	ops->target.addr = strtoull(tok + 1, NULL, 16);
226 	return 0;
227 }
228 
229 static int call__scnprintf(struct ins *ins, char *bf, size_t size,
230 			   struct ins_operands *ops)
231 {
232 	if (ops->target.name)
233 		return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->target.name);
234 
235 	if (ops->target.addr == 0)
236 		return ins__raw_scnprintf(ins, bf, size, ops);
237 
238 	return scnprintf(bf, size, "%-6.6s *%" PRIx64, ins->name, ops->target.addr);
239 }
240 
241 static struct ins_ops call_ops = {
242 	.parse	   = call__parse,
243 	.scnprintf = call__scnprintf,
244 };
245 
246 bool ins__is_call(const struct ins *ins)
247 {
248 	return ins->ops == &call_ops;
249 }
250 
251 static int jump__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map *map __maybe_unused)
252 {
253 	const char *s = strchr(ops->raw, '+');
254 	const char *c = strchr(ops->raw, ',');
255 
256 	/*
257 	 * skip over possible up to 2 operands to get to address, e.g.:
258 	 * tbnz	 w0, #26, ffff0000083cd190 <security_file_permission+0xd0>
259 	 */
260 	if (c++ != NULL) {
261 		ops->target.addr = strtoull(c, NULL, 16);
262 		if (!ops->target.addr) {
263 			c = strchr(c, ',');
264 			if (c++ != NULL)
265 				ops->target.addr = strtoull(c, NULL, 16);
266 		}
267 	} else {
268 		ops->target.addr = strtoull(ops->raw, NULL, 16);
269 	}
270 
271 	if (s++ != NULL) {
272 		ops->target.offset = strtoull(s, NULL, 16);
273 		ops->target.offset_avail = true;
274 	} else {
275 		ops->target.offset_avail = false;
276 	}
277 
278 	return 0;
279 }
280 
281 static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
282 			   struct ins_operands *ops)
283 {
284 	const char *c = strchr(ops->raw, ',');
285 
286 	if (!ops->target.addr || ops->target.offset < 0)
287 		return ins__raw_scnprintf(ins, bf, size, ops);
288 
289 	if (c != NULL) {
290 		const char *c2 = strchr(c + 1, ',');
291 
292 		/* check for 3-op insn */
293 		if (c2 != NULL)
294 			c = c2;
295 		c++;
296 
297 		/* mirror arch objdump's space-after-comma style */
298 		if (*c == ' ')
299 			c++;
300 	}
301 
302 	return scnprintf(bf, size, "%-6.6s %.*s%" PRIx64,
303 			 ins->name, c ? c - ops->raw : 0, ops->raw,
304 			 ops->target.offset);
305 }
306 
307 static struct ins_ops jump_ops = {
308 	.parse	   = jump__parse,
309 	.scnprintf = jump__scnprintf,
310 };
311 
312 bool ins__is_jump(const struct ins *ins)
313 {
314 	return ins->ops == &jump_ops;
315 }
316 
317 static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep)
318 {
319 	char *endptr, *name, *t;
320 
321 	if (strstr(raw, "(%rip)") == NULL)
322 		return 0;
323 
324 	*addrp = strtoull(comment, &endptr, 16);
325 	name = strchr(endptr, '<');
326 	if (name == NULL)
327 		return -1;
328 
329 	name++;
330 
331 	t = strchr(name, '>');
332 	if (t == NULL)
333 		return 0;
334 
335 	*t = '\0';
336 	*namep = strdup(name);
337 	*t = '>';
338 
339 	return 0;
340 }
341 
342 static int lock__parse(struct arch *arch, struct ins_operands *ops, struct map *map)
343 {
344 	ops->locked.ops = zalloc(sizeof(*ops->locked.ops));
345 	if (ops->locked.ops == NULL)
346 		return 0;
347 
348 	if (disasm_line__parse(ops->raw, &ops->locked.ins.name, &ops->locked.ops->raw) < 0)
349 		goto out_free_ops;
350 
351 	ops->locked.ins.ops = ins__find(arch, ops->locked.ins.name);
352 
353 	if (ops->locked.ins.ops == NULL)
354 		goto out_free_ops;
355 
356 	if (ops->locked.ins.ops->parse &&
357 	    ops->locked.ins.ops->parse(arch, ops->locked.ops, map) < 0)
358 		goto out_free_ops;
359 
360 	return 0;
361 
362 out_free_ops:
363 	zfree(&ops->locked.ops);
364 	return 0;
365 }
366 
367 static int lock__scnprintf(struct ins *ins, char *bf, size_t size,
368 			   struct ins_operands *ops)
369 {
370 	int printed;
371 
372 	if (ops->locked.ins.ops == NULL)
373 		return ins__raw_scnprintf(ins, bf, size, ops);
374 
375 	printed = scnprintf(bf, size, "%-6.6s ", ins->name);
376 	return printed + ins__scnprintf(&ops->locked.ins, bf + printed,
377 					size - printed, ops->locked.ops);
378 }
379 
380 static void lock__delete(struct ins_operands *ops)
381 {
382 	struct ins *ins = &ops->locked.ins;
383 
384 	if (ins->ops && ins->ops->free)
385 		ins->ops->free(ops->locked.ops);
386 	else
387 		ins__delete(ops->locked.ops);
388 
389 	zfree(&ops->locked.ops);
390 	zfree(&ops->target.raw);
391 	zfree(&ops->target.name);
392 }
393 
394 static struct ins_ops lock_ops = {
395 	.free	   = lock__delete,
396 	.parse	   = lock__parse,
397 	.scnprintf = lock__scnprintf,
398 };
399 
400 static int mov__parse(struct arch *arch, struct ins_operands *ops, struct map *map __maybe_unused)
401 {
402 	char *s = strchr(ops->raw, ','), *target, *comment, prev;
403 
404 	if (s == NULL)
405 		return -1;
406 
407 	*s = '\0';
408 	ops->source.raw = strdup(ops->raw);
409 	*s = ',';
410 
411 	if (ops->source.raw == NULL)
412 		return -1;
413 
414 	target = ++s;
415 	comment = strchr(s, arch->objdump.comment_char);
416 
417 	if (comment != NULL)
418 		s = comment - 1;
419 	else
420 		s = strchr(s, '\0') - 1;
421 
422 	while (s > target && isspace(s[0]))
423 		--s;
424 	s++;
425 	prev = *s;
426 	*s = '\0';
427 
428 	ops->target.raw = strdup(target);
429 	*s = prev;
430 
431 	if (ops->target.raw == NULL)
432 		goto out_free_source;
433 
434 	if (comment == NULL)
435 		return 0;
436 
437 	comment = ltrim(comment);
438 	comment__symbol(ops->source.raw, comment, &ops->source.addr, &ops->source.name);
439 	comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name);
440 
441 	return 0;
442 
443 out_free_source:
444 	zfree(&ops->source.raw);
445 	return -1;
446 }
447 
448 static int mov__scnprintf(struct ins *ins, char *bf, size_t size,
449 			   struct ins_operands *ops)
450 {
451 	return scnprintf(bf, size, "%-6.6s %s,%s", ins->name,
452 			 ops->source.name ?: ops->source.raw,
453 			 ops->target.name ?: ops->target.raw);
454 }
455 
456 static struct ins_ops mov_ops = {
457 	.parse	   = mov__parse,
458 	.scnprintf = mov__scnprintf,
459 };
460 
461 static int dec__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map *map __maybe_unused)
462 {
463 	char *target, *comment, *s, prev;
464 
465 	target = s = ops->raw;
466 
467 	while (s[0] != '\0' && !isspace(s[0]))
468 		++s;
469 	prev = *s;
470 	*s = '\0';
471 
472 	ops->target.raw = strdup(target);
473 	*s = prev;
474 
475 	if (ops->target.raw == NULL)
476 		return -1;
477 
478 	comment = strchr(s, arch->objdump.comment_char);
479 	if (comment == NULL)
480 		return 0;
481 
482 	comment = ltrim(comment);
483 	comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name);
484 
485 	return 0;
486 }
487 
488 static int dec__scnprintf(struct ins *ins, char *bf, size_t size,
489 			   struct ins_operands *ops)
490 {
491 	return scnprintf(bf, size, "%-6.6s %s", ins->name,
492 			 ops->target.name ?: ops->target.raw);
493 }
494 
495 static struct ins_ops dec_ops = {
496 	.parse	   = dec__parse,
497 	.scnprintf = dec__scnprintf,
498 };
499 
500 static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size,
501 			  struct ins_operands *ops __maybe_unused)
502 {
503 	return scnprintf(bf, size, "%-6.6s", "nop");
504 }
505 
506 static struct ins_ops nop_ops = {
507 	.scnprintf = nop__scnprintf,
508 };
509 
510 static struct ins_ops ret_ops = {
511 	.scnprintf = ins__raw_scnprintf,
512 };
513 
514 bool ins__is_ret(const struct ins *ins)
515 {
516 	return ins->ops == &ret_ops;
517 }
518 
519 bool ins__is_lock(const struct ins *ins)
520 {
521 	return ins->ops == &lock_ops;
522 }
523 
524 static int ins__key_cmp(const void *name, const void *insp)
525 {
526 	const struct ins *ins = insp;
527 
528 	return strcmp(name, ins->name);
529 }
530 
531 static int ins__cmp(const void *a, const void *b)
532 {
533 	const struct ins *ia = a;
534 	const struct ins *ib = b;
535 
536 	return strcmp(ia->name, ib->name);
537 }
538 
539 static void ins__sort(struct arch *arch)
540 {
541 	const int nmemb = arch->nr_instructions;
542 
543 	qsort(arch->instructions, nmemb, sizeof(struct ins), ins__cmp);
544 }
545 
546 static struct ins_ops *__ins__find(struct arch *arch, const char *name)
547 {
548 	struct ins *ins;
549 	const int nmemb = arch->nr_instructions;
550 
551 	if (!arch->sorted_instructions) {
552 		ins__sort(arch);
553 		arch->sorted_instructions = true;
554 	}
555 
556 	ins = bsearch(name, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp);
557 	return ins ? ins->ops : NULL;
558 }
559 
560 static struct ins_ops *ins__find(struct arch *arch, const char *name)
561 {
562 	struct ins_ops *ops = __ins__find(arch, name);
563 
564 	if (!ops && arch->associate_instruction_ops)
565 		ops = arch->associate_instruction_ops(arch, name);
566 
567 	return ops;
568 }
569 
570 static int arch__key_cmp(const void *name, const void *archp)
571 {
572 	const struct arch *arch = archp;
573 
574 	return strcmp(name, arch->name);
575 }
576 
577 static int arch__cmp(const void *a, const void *b)
578 {
579 	const struct arch *aa = a;
580 	const struct arch *ab = b;
581 
582 	return strcmp(aa->name, ab->name);
583 }
584 
585 static void arch__sort(void)
586 {
587 	const int nmemb = ARRAY_SIZE(architectures);
588 
589 	qsort(architectures, nmemb, sizeof(struct arch), arch__cmp);
590 }
591 
592 static struct arch *arch__find(const char *name)
593 {
594 	const int nmemb = ARRAY_SIZE(architectures);
595 	static bool sorted;
596 
597 	if (!sorted) {
598 		arch__sort();
599 		sorted = true;
600 	}
601 
602 	return bsearch(name, architectures, nmemb, sizeof(struct arch), arch__key_cmp);
603 }
604 
605 int symbol__alloc_hist(struct symbol *sym)
606 {
607 	struct annotation *notes = symbol__annotation(sym);
608 	size_t size = symbol__size(sym);
609 	size_t sizeof_sym_hist;
610 
611 	/*
612 	 * Add buffer of one element for zero length symbol.
613 	 * When sample is taken from first instruction of
614 	 * zero length symbol, perf still resolves it and
615 	 * shows symbol name in perf report and allows to
616 	 * annotate it.
617 	 */
618 	if (size == 0)
619 		size = 1;
620 
621 	/* Check for overflow when calculating sizeof_sym_hist */
622 	if (size > (SIZE_MAX - sizeof(struct sym_hist)) / sizeof(struct sym_hist_entry))
623 		return -1;
624 
625 	sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(struct sym_hist_entry));
626 
627 	/* Check for overflow in zalloc argument */
628 	if (sizeof_sym_hist > (SIZE_MAX - sizeof(*notes->src))
629 				/ symbol_conf.nr_events)
630 		return -1;
631 
632 	notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist);
633 	if (notes->src == NULL)
634 		return -1;
635 	notes->src->sizeof_sym_hist = sizeof_sym_hist;
636 	notes->src->nr_histograms   = symbol_conf.nr_events;
637 	INIT_LIST_HEAD(&notes->src->source);
638 	return 0;
639 }
640 
641 /* The cycles histogram is lazily allocated. */
642 static int symbol__alloc_hist_cycles(struct symbol *sym)
643 {
644 	struct annotation *notes = symbol__annotation(sym);
645 	const size_t size = symbol__size(sym);
646 
647 	notes->src->cycles_hist = calloc(size, sizeof(struct cyc_hist));
648 	if (notes->src->cycles_hist == NULL)
649 		return -1;
650 	return 0;
651 }
652 
653 void symbol__annotate_zero_histograms(struct symbol *sym)
654 {
655 	struct annotation *notes = symbol__annotation(sym);
656 
657 	pthread_mutex_lock(&notes->lock);
658 	if (notes->src != NULL) {
659 		memset(notes->src->histograms, 0,
660 		       notes->src->nr_histograms * notes->src->sizeof_sym_hist);
661 		if (notes->src->cycles_hist)
662 			memset(notes->src->cycles_hist, 0,
663 				symbol__size(sym) * sizeof(struct cyc_hist));
664 	}
665 	pthread_mutex_unlock(&notes->lock);
666 }
667 
668 static int __symbol__account_cycles(struct annotation *notes,
669 				    u64 start,
670 				    unsigned offset, unsigned cycles,
671 				    unsigned have_start)
672 {
673 	struct cyc_hist *ch;
674 
675 	ch = notes->src->cycles_hist;
676 	/*
677 	 * For now we can only account one basic block per
678 	 * final jump. But multiple could be overlapping.
679 	 * Always account the longest one. So when
680 	 * a shorter one has been already seen throw it away.
681 	 *
682 	 * We separately always account the full cycles.
683 	 */
684 	ch[offset].num_aggr++;
685 	ch[offset].cycles_aggr += cycles;
686 
687 	if (!have_start && ch[offset].have_start)
688 		return 0;
689 	if (ch[offset].num) {
690 		if (have_start && (!ch[offset].have_start ||
691 				   ch[offset].start > start)) {
692 			ch[offset].have_start = 0;
693 			ch[offset].cycles = 0;
694 			ch[offset].num = 0;
695 			if (ch[offset].reset < 0xffff)
696 				ch[offset].reset++;
697 		} else if (have_start &&
698 			   ch[offset].start < start)
699 			return 0;
700 	}
701 	ch[offset].have_start = have_start;
702 	ch[offset].start = start;
703 	ch[offset].cycles += cycles;
704 	ch[offset].num++;
705 	return 0;
706 }
707 
708 static int __symbol__inc_addr_samples(struct symbol *sym, struct map *map,
709 				      struct annotation *notes, int evidx, u64 addr,
710 				      struct perf_sample *sample)
711 {
712 	unsigned offset;
713 	struct sym_hist *h;
714 
715 	pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
716 
717 	if ((addr < sym->start || addr >= sym->end) &&
718 	    (addr != sym->end || sym->start != sym->end)) {
719 		pr_debug("%s(%d): ERANGE! sym->name=%s, start=%#" PRIx64 ", addr=%#" PRIx64 ", end=%#" PRIx64 "\n",
720 		       __func__, __LINE__, sym->name, sym->start, addr, sym->end);
721 		return -ERANGE;
722 	}
723 
724 	offset = addr - sym->start;
725 	h = annotation__histogram(notes, evidx);
726 	h->nr_samples++;
727 	h->addr[offset].nr_samples++;
728 	h->period += sample->period;
729 	h->addr[offset].period += sample->period;
730 
731 	pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
732 		  ", evidx=%d] => nr_samples: %" PRIu64 ", period: %" PRIu64 "\n",
733 		  sym->start, sym->name, addr, addr - sym->start, evidx,
734 		  h->addr[offset].nr_samples, h->addr[offset].period);
735 	return 0;
736 }
737 
738 static struct annotation *symbol__get_annotation(struct symbol *sym, bool cycles)
739 {
740 	struct annotation *notes = symbol__annotation(sym);
741 
742 	if (notes->src == NULL) {
743 		if (symbol__alloc_hist(sym) < 0)
744 			return NULL;
745 	}
746 	if (!notes->src->cycles_hist && cycles) {
747 		if (symbol__alloc_hist_cycles(sym) < 0)
748 			return NULL;
749 	}
750 	return notes;
751 }
752 
753 static int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
754 				    int evidx, u64 addr,
755 				    struct perf_sample *sample)
756 {
757 	struct annotation *notes;
758 
759 	if (sym == NULL)
760 		return 0;
761 	notes = symbol__get_annotation(sym, false);
762 	if (notes == NULL)
763 		return -ENOMEM;
764 	return __symbol__inc_addr_samples(sym, map, notes, evidx, addr, sample);
765 }
766 
767 static int symbol__account_cycles(u64 addr, u64 start,
768 				  struct symbol *sym, unsigned cycles)
769 {
770 	struct annotation *notes;
771 	unsigned offset;
772 
773 	if (sym == NULL)
774 		return 0;
775 	notes = symbol__get_annotation(sym, true);
776 	if (notes == NULL)
777 		return -ENOMEM;
778 	if (addr < sym->start || addr >= sym->end)
779 		return -ERANGE;
780 
781 	if (start) {
782 		if (start < sym->start || start >= sym->end)
783 			return -ERANGE;
784 		if (start >= addr)
785 			start = 0;
786 	}
787 	offset = addr - sym->start;
788 	return __symbol__account_cycles(notes,
789 					start ? start - sym->start : 0,
790 					offset, cycles,
791 					!!start);
792 }
793 
794 int addr_map_symbol__account_cycles(struct addr_map_symbol *ams,
795 				    struct addr_map_symbol *start,
796 				    unsigned cycles)
797 {
798 	u64 saddr = 0;
799 	int err;
800 
801 	if (!cycles)
802 		return 0;
803 
804 	/*
805 	 * Only set start when IPC can be computed. We can only
806 	 * compute it when the basic block is completely in a single
807 	 * function.
808 	 * Special case the case when the jump is elsewhere, but
809 	 * it starts on the function start.
810 	 */
811 	if (start &&
812 		(start->sym == ams->sym ||
813 		 (ams->sym &&
814 		   start->addr == ams->sym->start + ams->map->start)))
815 		saddr = start->al_addr;
816 	if (saddr == 0)
817 		pr_debug2("BB with bad start: addr %"PRIx64" start %"PRIx64" sym %"PRIx64" saddr %"PRIx64"\n",
818 			ams->addr,
819 			start ? start->addr : 0,
820 			ams->sym ? ams->sym->start + ams->map->start : 0,
821 			saddr);
822 	err = symbol__account_cycles(ams->al_addr, saddr, ams->sym, cycles);
823 	if (err)
824 		pr_debug2("account_cycles failed %d\n", err);
825 	return err;
826 }
827 
828 int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, struct perf_sample *sample,
829 				 int evidx)
830 {
831 	return symbol__inc_addr_samples(ams->sym, ams->map, evidx, ams->al_addr, sample);
832 }
833 
834 int hist_entry__inc_addr_samples(struct hist_entry *he, struct perf_sample *sample,
835 				 int evidx, u64 ip)
836 {
837 	return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip, sample);
838 }
839 
840 static void disasm_line__init_ins(struct disasm_line *dl, struct arch *arch, struct map *map)
841 {
842 	dl->ins.ops = ins__find(arch, dl->ins.name);
843 
844 	if (!dl->ins.ops)
845 		return;
846 
847 	if (dl->ins.ops->parse && dl->ins.ops->parse(arch, &dl->ops, map) < 0)
848 		dl->ins.ops = NULL;
849 }
850 
851 static int disasm_line__parse(char *line, const char **namep, char **rawp)
852 {
853 	char tmp, *name = ltrim(line);
854 
855 	if (name[0] == '\0')
856 		return -1;
857 
858 	*rawp = name + 1;
859 
860 	while ((*rawp)[0] != '\0' && !isspace((*rawp)[0]))
861 		++*rawp;
862 
863 	tmp = (*rawp)[0];
864 	(*rawp)[0] = '\0';
865 	*namep = strdup(name);
866 
867 	if (*namep == NULL)
868 		goto out_free_name;
869 
870 	(*rawp)[0] = tmp;
871 	*rawp = ltrim(*rawp);
872 
873 	return 0;
874 
875 out_free_name:
876 	free((void *)namep);
877 	*namep = NULL;
878 	return -1;
879 }
880 
881 struct annotate_args {
882 	size_t			 privsize;
883 	struct arch		*arch;
884 	struct map		*map;
885 	struct perf_evsel	*evsel;
886 	s64			 offset;
887 	char			*line;
888 	int			 line_nr;
889 };
890 
891 static void annotation_line__delete(struct annotation_line *al)
892 {
893 	void *ptr = (void *) al - al->privsize;
894 
895 	free_srcline(al->path);
896 	zfree(&al->line);
897 	free(ptr);
898 }
899 
900 /*
901  * Allocating the annotation line data with following
902  * structure:
903  *
904  *    --------------------------------------
905  *    private space | struct annotation_line
906  *    --------------------------------------
907  *
908  * Size of the private space is stored in 'struct annotation_line'.
909  *
910  */
911 static struct annotation_line *
912 annotation_line__new(struct annotate_args *args, size_t privsize)
913 {
914 	struct annotation_line *al;
915 	struct perf_evsel *evsel = args->evsel;
916 	size_t size = privsize + sizeof(*al);
917 	int nr = 1;
918 
919 	if (perf_evsel__is_group_event(evsel))
920 		nr = evsel->nr_members;
921 
922 	size += sizeof(al->samples[0]) * nr;
923 
924 	al = zalloc(size);
925 	if (al) {
926 		al = (void *) al + privsize;
927 		al->privsize   = privsize;
928 		al->offset     = args->offset;
929 		al->line       = strdup(args->line);
930 		al->line_nr    = args->line_nr;
931 		al->samples_nr = nr;
932 	}
933 
934 	return al;
935 }
936 
937 /*
938  * Allocating the disasm annotation line data with
939  * following structure:
940  *
941  *    ------------------------------------------------------------
942  *    privsize space | struct disasm_line | struct annotation_line
943  *    ------------------------------------------------------------
944  *
945  * We have 'struct annotation_line' member as last member
946  * of 'struct disasm_line' to have an easy access.
947  *
948  */
949 static struct disasm_line *disasm_line__new(struct annotate_args *args)
950 {
951 	struct disasm_line *dl = NULL;
952 	struct annotation_line *al;
953 	size_t privsize = args->privsize + offsetof(struct disasm_line, al);
954 
955 	al = annotation_line__new(args, privsize);
956 	if (al != NULL) {
957 		dl = disasm_line(al);
958 
959 		if (dl->al.line == NULL)
960 			goto out_delete;
961 
962 		if (args->offset != -1) {
963 			if (disasm_line__parse(dl->al.line, &dl->ins.name, &dl->ops.raw) < 0)
964 				goto out_free_line;
965 
966 			disasm_line__init_ins(dl, args->arch, args->map);
967 		}
968 	}
969 
970 	return dl;
971 
972 out_free_line:
973 	zfree(&dl->al.line);
974 out_delete:
975 	free(dl);
976 	return NULL;
977 }
978 
979 void disasm_line__free(struct disasm_line *dl)
980 {
981 	if (dl->ins.ops && dl->ins.ops->free)
982 		dl->ins.ops->free(&dl->ops);
983 	else
984 		ins__delete(&dl->ops);
985 	free((void *)dl->ins.name);
986 	dl->ins.name = NULL;
987 	annotation_line__delete(&dl->al);
988 }
989 
990 int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw)
991 {
992 	if (raw || !dl->ins.ops)
993 		return scnprintf(bf, size, "%-6.6s %s", dl->ins.name, dl->ops.raw);
994 
995 	return ins__scnprintf(&dl->ins, bf, size, &dl->ops);
996 }
997 
998 static void annotation_line__add(struct annotation_line *al, struct list_head *head)
999 {
1000 	list_add_tail(&al->node, head);
1001 }
1002 
1003 struct annotation_line *
1004 annotation_line__next(struct annotation_line *pos, struct list_head *head)
1005 {
1006 	list_for_each_entry_continue(pos, head, node)
1007 		if (pos->offset >= 0)
1008 			return pos;
1009 
1010 	return NULL;
1011 }
1012 
1013 static const char *annotate__address_color(struct block_range *br)
1014 {
1015 	double cov = block_range__coverage(br);
1016 
1017 	if (cov >= 0) {
1018 		/* mark red for >75% coverage */
1019 		if (cov > 0.75)
1020 			return PERF_COLOR_RED;
1021 
1022 		/* mark dull for <1% coverage */
1023 		if (cov < 0.01)
1024 			return PERF_COLOR_NORMAL;
1025 	}
1026 
1027 	return PERF_COLOR_MAGENTA;
1028 }
1029 
1030 static const char *annotate__asm_color(struct block_range *br)
1031 {
1032 	double cov = block_range__coverage(br);
1033 
1034 	if (cov >= 0) {
1035 		/* mark dull for <1% coverage */
1036 		if (cov < 0.01)
1037 			return PERF_COLOR_NORMAL;
1038 	}
1039 
1040 	return PERF_COLOR_BLUE;
1041 }
1042 
1043 static void annotate__branch_printf(struct block_range *br, u64 addr)
1044 {
1045 	bool emit_comment = true;
1046 
1047 	if (!br)
1048 		return;
1049 
1050 #if 1
1051 	if (br->is_target && br->start == addr) {
1052 		struct block_range *branch = br;
1053 		double p;
1054 
1055 		/*
1056 		 * Find matching branch to our target.
1057 		 */
1058 		while (!branch->is_branch)
1059 			branch = block_range__next(branch);
1060 
1061 		p = 100 *(double)br->entry / branch->coverage;
1062 
1063 		if (p > 0.1) {
1064 			if (emit_comment) {
1065 				emit_comment = false;
1066 				printf("\t#");
1067 			}
1068 
1069 			/*
1070 			 * The percentage of coverage joined at this target in relation
1071 			 * to the next branch.
1072 			 */
1073 			printf(" +%.2f%%", p);
1074 		}
1075 	}
1076 #endif
1077 	if (br->is_branch && br->end == addr) {
1078 		double p = 100*(double)br->taken / br->coverage;
1079 
1080 		if (p > 0.1) {
1081 			if (emit_comment) {
1082 				emit_comment = false;
1083 				printf("\t#");
1084 			}
1085 
1086 			/*
1087 			 * The percentage of coverage leaving at this branch, and
1088 			 * its prediction ratio.
1089 			 */
1090 			printf(" -%.2f%% (p:%.2f%%)", p, 100*(double)br->pred  / br->taken);
1091 		}
1092 	}
1093 }
1094 
1095 static int disasm_line__print(struct disasm_line *dl, u64 start, int addr_fmt_width)
1096 {
1097 	s64 offset = dl->al.offset;
1098 	const u64 addr = start + offset;
1099 	struct block_range *br;
1100 
1101 	br = block_range__find(addr);
1102 	color_fprintf(stdout, annotate__address_color(br), "  %*" PRIx64 ":", addr_fmt_width, addr);
1103 	color_fprintf(stdout, annotate__asm_color(br), "%s", dl->al.line);
1104 	annotate__branch_printf(br, addr);
1105 	return 0;
1106 }
1107 
1108 static int
1109 annotation_line__print(struct annotation_line *al, struct symbol *sym, u64 start,
1110 		       struct perf_evsel *evsel, u64 len, int min_pcnt, int printed,
1111 		       int max_lines, struct annotation_line *queue, int addr_fmt_width)
1112 {
1113 	struct disasm_line *dl = container_of(al, struct disasm_line, al);
1114 	static const char *prev_line;
1115 	static const char *prev_color;
1116 
1117 	if (al->offset != -1) {
1118 		double max_percent = 0.0;
1119 		int i, nr_percent = 1;
1120 		const char *color;
1121 		struct annotation *notes = symbol__annotation(sym);
1122 
1123 		for (i = 0; i < al->samples_nr; i++) {
1124 			struct annotation_data *sample = &al->samples[i];
1125 
1126 			if (sample->percent > max_percent)
1127 				max_percent = sample->percent;
1128 		}
1129 
1130 		if (max_percent < min_pcnt)
1131 			return -1;
1132 
1133 		if (max_lines && printed >= max_lines)
1134 			return 1;
1135 
1136 		if (queue != NULL) {
1137 			list_for_each_entry_from(queue, &notes->src->source, node) {
1138 				if (queue == al)
1139 					break;
1140 				annotation_line__print(queue, sym, start, evsel, len,
1141 						       0, 0, 1, NULL, addr_fmt_width);
1142 			}
1143 		}
1144 
1145 		color = get_percent_color(max_percent);
1146 
1147 		/*
1148 		 * Also color the filename and line if needed, with
1149 		 * the same color than the percentage. Don't print it
1150 		 * twice for close colored addr with the same filename:line
1151 		 */
1152 		if (al->path) {
1153 			if (!prev_line || strcmp(prev_line, al->path)
1154 				       || color != prev_color) {
1155 				color_fprintf(stdout, color, " %s", al->path);
1156 				prev_line = al->path;
1157 				prev_color = color;
1158 			}
1159 		}
1160 
1161 		for (i = 0; i < nr_percent; i++) {
1162 			struct annotation_data *sample = &al->samples[i];
1163 
1164 			color = get_percent_color(sample->percent);
1165 
1166 			if (symbol_conf.show_total_period)
1167 				color_fprintf(stdout, color, " %11" PRIu64,
1168 					      sample->he.period);
1169 			else if (symbol_conf.show_nr_samples)
1170 				color_fprintf(stdout, color, " %7" PRIu64,
1171 					      sample->he.nr_samples);
1172 			else
1173 				color_fprintf(stdout, color, " %7.2f", sample->percent);
1174 		}
1175 
1176 		printf(" : ");
1177 
1178 		disasm_line__print(dl, start, addr_fmt_width);
1179 		printf("\n");
1180 	} else if (max_lines && printed >= max_lines)
1181 		return 1;
1182 	else {
1183 		int width = symbol_conf.show_total_period ? 12 : 8;
1184 
1185 		if (queue)
1186 			return -1;
1187 
1188 		if (perf_evsel__is_group_event(evsel))
1189 			width *= evsel->nr_members;
1190 
1191 		if (!*al->line)
1192 			printf(" %*s:\n", width, " ");
1193 		else
1194 			printf(" %*s:     %*s %s\n", width, " ", addr_fmt_width, " ", al->line);
1195 	}
1196 
1197 	return 0;
1198 }
1199 
1200 /*
1201  * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw)
1202  * which looks like following
1203  *
1204  *  0000000000415500 <_init>:
1205  *    415500:       sub    $0x8,%rsp
1206  *    415504:       mov    0x2f5ad5(%rip),%rax        # 70afe0 <_DYNAMIC+0x2f8>
1207  *    41550b:       test   %rax,%rax
1208  *    41550e:       je     415515 <_init+0x15>
1209  *    415510:       callq  416e70 <__gmon_start__@plt>
1210  *    415515:       add    $0x8,%rsp
1211  *    415519:       retq
1212  *
1213  * it will be parsed and saved into struct disasm_line as
1214  *  <offset>       <name>  <ops.raw>
1215  *
1216  * The offset will be a relative offset from the start of the symbol and -1
1217  * means that it's not a disassembly line so should be treated differently.
1218  * The ops.raw part will be parsed further according to type of the instruction.
1219  */
1220 static int symbol__parse_objdump_line(struct symbol *sym, FILE *file,
1221 				      struct annotate_args *args,
1222 				      int *line_nr)
1223 {
1224 	struct map *map = args->map;
1225 	struct annotation *notes = symbol__annotation(sym);
1226 	struct disasm_line *dl;
1227 	char *line = NULL, *parsed_line, *tmp, *tmp2;
1228 	size_t line_len;
1229 	s64 line_ip, offset = -1;
1230 	regmatch_t match[2];
1231 
1232 	if (getline(&line, &line_len, file) < 0)
1233 		return -1;
1234 
1235 	if (!line)
1236 		return -1;
1237 
1238 	line_ip = -1;
1239 	parsed_line = rtrim(line);
1240 
1241 	/* /filename:linenr ? Save line number and ignore. */
1242 	if (regexec(&file_lineno, parsed_line, 2, match, 0) == 0) {
1243 		*line_nr = atoi(parsed_line + match[1].rm_so);
1244 		return 0;
1245 	}
1246 
1247 	tmp = ltrim(parsed_line);
1248 	if (*tmp) {
1249 		/*
1250 		 * Parse hexa addresses followed by ':'
1251 		 */
1252 		line_ip = strtoull(tmp, &tmp2, 16);
1253 		if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
1254 			line_ip = -1;
1255 	}
1256 
1257 	if (line_ip != -1) {
1258 		u64 start = map__rip_2objdump(map, sym->start),
1259 		    end = map__rip_2objdump(map, sym->end);
1260 
1261 		offset = line_ip - start;
1262 		if ((u64)line_ip < start || (u64)line_ip >= end)
1263 			offset = -1;
1264 		else
1265 			parsed_line = tmp2 + 1;
1266 	}
1267 
1268 	args->offset  = offset;
1269 	args->line    = parsed_line;
1270 	args->line_nr = *line_nr;
1271 
1272 	dl = disasm_line__new(args);
1273 	free(line);
1274 	(*line_nr)++;
1275 
1276 	if (dl == NULL)
1277 		return -1;
1278 
1279 	if (!disasm_line__has_offset(dl)) {
1280 		dl->ops.target.offset = dl->ops.target.addr -
1281 					map__rip_2objdump(map, sym->start);
1282 		dl->ops.target.offset_avail = true;
1283 	}
1284 
1285 	/* kcore has no symbols, so add the call target name */
1286 	if (dl->ins.ops && ins__is_call(&dl->ins) && !dl->ops.target.name) {
1287 		struct addr_map_symbol target = {
1288 			.map = map,
1289 			.addr = dl->ops.target.addr,
1290 		};
1291 
1292 		if (!map_groups__find_ams(&target) &&
1293 		    target.sym->start == target.al_addr)
1294 			dl->ops.target.name = strdup(target.sym->name);
1295 	}
1296 
1297 	annotation_line__add(&dl->al, &notes->src->source);
1298 
1299 	return 0;
1300 }
1301 
1302 static __attribute__((constructor)) void symbol__init_regexpr(void)
1303 {
1304 	regcomp(&file_lineno, "^/[^:]+:([0-9]+)", REG_EXTENDED);
1305 }
1306 
1307 static void delete_last_nop(struct symbol *sym)
1308 {
1309 	struct annotation *notes = symbol__annotation(sym);
1310 	struct list_head *list = &notes->src->source;
1311 	struct disasm_line *dl;
1312 
1313 	while (!list_empty(list)) {
1314 		dl = list_entry(list->prev, struct disasm_line, al.node);
1315 
1316 		if (dl->ins.ops) {
1317 			if (dl->ins.ops != &nop_ops)
1318 				return;
1319 		} else {
1320 			if (!strstr(dl->al.line, " nop ") &&
1321 			    !strstr(dl->al.line, " nopl ") &&
1322 			    !strstr(dl->al.line, " nopw "))
1323 				return;
1324 		}
1325 
1326 		list_del(&dl->al.node);
1327 		disasm_line__free(dl);
1328 	}
1329 }
1330 
1331 int symbol__strerror_disassemble(struct symbol *sym __maybe_unused, struct map *map,
1332 			      int errnum, char *buf, size_t buflen)
1333 {
1334 	struct dso *dso = map->dso;
1335 
1336 	BUG_ON(buflen == 0);
1337 
1338 	if (errnum >= 0) {
1339 		str_error_r(errnum, buf, buflen);
1340 		return 0;
1341 	}
1342 
1343 	switch (errnum) {
1344 	case SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX: {
1345 		char bf[SBUILD_ID_SIZE + 15] = " with build id ";
1346 		char *build_id_msg = NULL;
1347 
1348 		if (dso->has_build_id) {
1349 			build_id__sprintf(dso->build_id,
1350 					  sizeof(dso->build_id), bf + 15);
1351 			build_id_msg = bf;
1352 		}
1353 		scnprintf(buf, buflen,
1354 			  "No vmlinux file%s\nwas found in the path.\n\n"
1355 			  "Note that annotation using /proc/kcore requires CAP_SYS_RAWIO capability.\n\n"
1356 			  "Please use:\n\n"
1357 			  "  perf buildid-cache -vu vmlinux\n\n"
1358 			  "or:\n\n"
1359 			  "  --vmlinux vmlinux\n", build_id_msg ?: "");
1360 	}
1361 		break;
1362 	default:
1363 		scnprintf(buf, buflen, "Internal error: Invalid %d error code\n", errnum);
1364 		break;
1365 	}
1366 
1367 	return 0;
1368 }
1369 
1370 static int dso__disassemble_filename(struct dso *dso, char *filename, size_t filename_size)
1371 {
1372 	char linkname[PATH_MAX];
1373 	char *build_id_filename;
1374 	char *build_id_path = NULL;
1375 	char *pos;
1376 
1377 	if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
1378 	    !dso__is_kcore(dso))
1379 		return SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX;
1380 
1381 	build_id_filename = dso__build_id_filename(dso, NULL, 0, false);
1382 	if (build_id_filename) {
1383 		__symbol__join_symfs(filename, filename_size, build_id_filename);
1384 		free(build_id_filename);
1385 	} else {
1386 		if (dso->has_build_id)
1387 			return ENOMEM;
1388 		goto fallback;
1389 	}
1390 
1391 	build_id_path = strdup(filename);
1392 	if (!build_id_path)
1393 		return -1;
1394 
1395 	/*
1396 	 * old style build-id cache has name of XX/XXXXXXX.. while
1397 	 * new style has XX/XXXXXXX../{elf,kallsyms,vdso}.
1398 	 * extract the build-id part of dirname in the new style only.
1399 	 */
1400 	pos = strrchr(build_id_path, '/');
1401 	if (pos && strlen(pos) < SBUILD_ID_SIZE - 2)
1402 		dirname(build_id_path);
1403 
1404 	if (dso__is_kcore(dso) ||
1405 	    readlink(build_id_path, linkname, sizeof(linkname)) < 0 ||
1406 	    strstr(linkname, DSO__NAME_KALLSYMS) ||
1407 	    access(filename, R_OK)) {
1408 fallback:
1409 		/*
1410 		 * If we don't have build-ids or the build-id file isn't in the
1411 		 * cache, or is just a kallsyms file, well, lets hope that this
1412 		 * DSO is the same as when 'perf record' ran.
1413 		 */
1414 		__symbol__join_symfs(filename, filename_size, dso->long_name);
1415 	}
1416 
1417 	free(build_id_path);
1418 	return 0;
1419 }
1420 
1421 static const char *annotate__norm_arch(const char *arch_name)
1422 {
1423 	struct utsname uts;
1424 
1425 	if (!arch_name) { /* Assume we are annotating locally. */
1426 		if (uname(&uts) < 0)
1427 			return NULL;
1428 		arch_name = uts.machine;
1429 	}
1430 	return normalize_arch((char *)arch_name);
1431 }
1432 
1433 static int symbol__disassemble(struct symbol *sym, struct annotate_args *args)
1434 {
1435 	struct map *map = args->map;
1436 	struct dso *dso = map->dso;
1437 	char command[PATH_MAX * 2];
1438 	FILE *file;
1439 	char symfs_filename[PATH_MAX];
1440 	struct kcore_extract kce;
1441 	bool delete_extract = false;
1442 	int stdout_fd[2];
1443 	int lineno = 0;
1444 	int nline;
1445 	pid_t pid;
1446 	int err = dso__disassemble_filename(dso, symfs_filename, sizeof(symfs_filename));
1447 
1448 	if (err)
1449 		return err;
1450 
1451 	pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
1452 		 symfs_filename, sym->name, map->unmap_ip(map, sym->start),
1453 		 map->unmap_ip(map, sym->end));
1454 
1455 	pr_debug("annotating [%p] %30s : [%p] %30s\n",
1456 		 dso, dso->long_name, sym, sym->name);
1457 
1458 	if (dso__is_kcore(dso)) {
1459 		kce.kcore_filename = symfs_filename;
1460 		kce.addr = map__rip_2objdump(map, sym->start);
1461 		kce.offs = sym->start;
1462 		kce.len = sym->end - sym->start;
1463 		if (!kcore_extract__create(&kce)) {
1464 			delete_extract = true;
1465 			strlcpy(symfs_filename, kce.extract_filename,
1466 				sizeof(symfs_filename));
1467 		}
1468 	} else if (dso__needs_decompress(dso)) {
1469 		char tmp[KMOD_DECOMP_LEN];
1470 
1471 		if (dso__decompress_kmodule_path(dso, symfs_filename,
1472 						 tmp, sizeof(tmp)) < 0)
1473 			goto out;
1474 
1475 		strcpy(symfs_filename, tmp);
1476 	}
1477 
1478 	snprintf(command, sizeof(command),
1479 		 "%s %s%s --start-address=0x%016" PRIx64
1480 		 " --stop-address=0x%016" PRIx64
1481 		 " -l -d %s %s -C \"%s\" 2>/dev/null|grep -v \"%s:\"|expand",
1482 		 objdump_path ? objdump_path : "objdump",
1483 		 disassembler_style ? "-M " : "",
1484 		 disassembler_style ? disassembler_style : "",
1485 		 map__rip_2objdump(map, sym->start),
1486 		 map__rip_2objdump(map, sym->end),
1487 		 symbol_conf.annotate_asm_raw ? "" : "--no-show-raw",
1488 		 symbol_conf.annotate_src ? "-S" : "",
1489 		 symfs_filename, symfs_filename);
1490 
1491 	pr_debug("Executing: %s\n", command);
1492 
1493 	err = -1;
1494 	if (pipe(stdout_fd) < 0) {
1495 		pr_err("Failure creating the pipe to run %s\n", command);
1496 		goto out_remove_tmp;
1497 	}
1498 
1499 	pid = fork();
1500 	if (pid < 0) {
1501 		pr_err("Failure forking to run %s\n", command);
1502 		goto out_close_stdout;
1503 	}
1504 
1505 	if (pid == 0) {
1506 		close(stdout_fd[0]);
1507 		dup2(stdout_fd[1], 1);
1508 		close(stdout_fd[1]);
1509 		execl("/bin/sh", "sh", "-c", command, NULL);
1510 		perror(command);
1511 		exit(-1);
1512 	}
1513 
1514 	close(stdout_fd[1]);
1515 
1516 	file = fdopen(stdout_fd[0], "r");
1517 	if (!file) {
1518 		pr_err("Failure creating FILE stream for %s\n", command);
1519 		/*
1520 		 * If we were using debug info should retry with
1521 		 * original binary.
1522 		 */
1523 		goto out_remove_tmp;
1524 	}
1525 
1526 	nline = 0;
1527 	while (!feof(file)) {
1528 		/*
1529 		 * The source code line number (lineno) needs to be kept in
1530 		 * accross calls to symbol__parse_objdump_line(), so that it
1531 		 * can associate it with the instructions till the next one.
1532 		 * See disasm_line__new() and struct disasm_line::line_nr.
1533 		 */
1534 		if (symbol__parse_objdump_line(sym, file, args, &lineno) < 0)
1535 			break;
1536 		nline++;
1537 	}
1538 
1539 	if (nline == 0)
1540 		pr_err("No output from %s\n", command);
1541 
1542 	/*
1543 	 * kallsyms does not have symbol sizes so there may a nop at the end.
1544 	 * Remove it.
1545 	 */
1546 	if (dso__is_kcore(dso))
1547 		delete_last_nop(sym);
1548 
1549 	fclose(file);
1550 	err = 0;
1551 out_remove_tmp:
1552 	close(stdout_fd[0]);
1553 
1554 	if (dso__needs_decompress(dso))
1555 		unlink(symfs_filename);
1556 
1557 	if (delete_extract)
1558 		kcore_extract__delete(&kce);
1559 out:
1560 	return err;
1561 
1562 out_close_stdout:
1563 	close(stdout_fd[1]);
1564 	goto out_remove_tmp;
1565 }
1566 
1567 static void calc_percent(struct sym_hist *hist,
1568 			 struct annotation_data *sample,
1569 			 s64 offset, s64 end)
1570 {
1571 	unsigned int hits = 0;
1572 	u64 period = 0;
1573 
1574 	while (offset < end) {
1575 		hits   += hist->addr[offset].nr_samples;
1576 		period += hist->addr[offset].period;
1577 		++offset;
1578 	}
1579 
1580 	if (hist->nr_samples) {
1581 		sample->he.period     = period;
1582 		sample->he.nr_samples = hits;
1583 		sample->percent = 100.0 * hits / hist->nr_samples;
1584 	}
1585 }
1586 
1587 static int annotation__calc_percent(struct annotation *notes,
1588 				    struct perf_evsel *evsel, s64 len)
1589 {
1590 	struct annotation_line *al, *next;
1591 
1592 	list_for_each_entry(al, &notes->src->source, node) {
1593 		s64 end;
1594 		int i;
1595 
1596 		if (al->offset == -1)
1597 			continue;
1598 
1599 		next = annotation_line__next(al, &notes->src->source);
1600 		end  = next ? next->offset : len;
1601 
1602 		for (i = 0; i < al->samples_nr; i++) {
1603 			struct annotation_data *sample;
1604 			struct sym_hist *hist;
1605 
1606 			hist   = annotation__histogram(notes, evsel->idx + i);
1607 			sample = &al->samples[i];
1608 
1609 			calc_percent(hist, sample, al->offset, end);
1610 		}
1611 	}
1612 
1613 	return 0;
1614 }
1615 
1616 int symbol__calc_percent(struct symbol *sym, struct perf_evsel *evsel)
1617 {
1618 	struct annotation *notes = symbol__annotation(sym);
1619 
1620 	return annotation__calc_percent(notes, evsel, symbol__size(sym));
1621 }
1622 
1623 int symbol__annotate(struct symbol *sym, struct map *map,
1624 		     struct perf_evsel *evsel, size_t privsize,
1625 		     struct arch **parch, char *cpuid)
1626 {
1627 	struct annotate_args args = {
1628 		.privsize	= privsize,
1629 		.map		= map,
1630 		.evsel		= evsel,
1631 	};
1632 	const char *arch_name = NULL;
1633 	struct arch *arch;
1634 	int err;
1635 
1636 	if (evsel)
1637 		arch_name = perf_evsel__env_arch(evsel);
1638 
1639 	arch_name = annotate__norm_arch(arch_name);
1640 	if (!arch_name)
1641 		return -1;
1642 
1643 	args.arch = arch = arch__find(arch_name);
1644 	if (arch == NULL)
1645 		return -ENOTSUP;
1646 
1647 	if (parch)
1648 		*parch = arch;
1649 
1650 	if (arch->init) {
1651 		err = arch->init(arch, cpuid);
1652 		if (err) {
1653 			pr_err("%s: failed to initialize %s arch priv area\n", __func__, arch->name);
1654 			return err;
1655 		}
1656 	}
1657 
1658 	err = symbol__disassemble(sym, &args);
1659 	if (err)
1660 		return err;
1661 
1662 	return symbol__calc_percent(sym, evsel);
1663 }
1664 
1665 static void insert_source_line(struct rb_root *root, struct annotation_line *al)
1666 {
1667 	struct annotation_line *iter;
1668 	struct rb_node **p = &root->rb_node;
1669 	struct rb_node *parent = NULL;
1670 	int i, ret;
1671 
1672 	while (*p != NULL) {
1673 		parent = *p;
1674 		iter = rb_entry(parent, struct annotation_line, rb_node);
1675 
1676 		ret = strcmp(iter->path, al->path);
1677 		if (ret == 0) {
1678 			for (i = 0; i < al->samples_nr; i++)
1679 				iter->samples[i].percent_sum += al->samples[i].percent;
1680 			return;
1681 		}
1682 
1683 		if (ret < 0)
1684 			p = &(*p)->rb_left;
1685 		else
1686 			p = &(*p)->rb_right;
1687 	}
1688 
1689 	for (i = 0; i < al->samples_nr; i++)
1690 		al->samples[i].percent_sum = al->samples[i].percent;
1691 
1692 	rb_link_node(&al->rb_node, parent, p);
1693 	rb_insert_color(&al->rb_node, root);
1694 }
1695 
1696 static int cmp_source_line(struct annotation_line *a, struct annotation_line *b)
1697 {
1698 	int i;
1699 
1700 	for (i = 0; i < a->samples_nr; i++) {
1701 		if (a->samples[i].percent_sum == b->samples[i].percent_sum)
1702 			continue;
1703 		return a->samples[i].percent_sum > b->samples[i].percent_sum;
1704 	}
1705 
1706 	return 0;
1707 }
1708 
1709 static void __resort_source_line(struct rb_root *root, struct annotation_line *al)
1710 {
1711 	struct annotation_line *iter;
1712 	struct rb_node **p = &root->rb_node;
1713 	struct rb_node *parent = NULL;
1714 
1715 	while (*p != NULL) {
1716 		parent = *p;
1717 		iter = rb_entry(parent, struct annotation_line, rb_node);
1718 
1719 		if (cmp_source_line(al, iter))
1720 			p = &(*p)->rb_left;
1721 		else
1722 			p = &(*p)->rb_right;
1723 	}
1724 
1725 	rb_link_node(&al->rb_node, parent, p);
1726 	rb_insert_color(&al->rb_node, root);
1727 }
1728 
1729 static void resort_source_line(struct rb_root *dest_root, struct rb_root *src_root)
1730 {
1731 	struct annotation_line *al;
1732 	struct rb_node *node;
1733 
1734 	node = rb_first(src_root);
1735 	while (node) {
1736 		struct rb_node *next;
1737 
1738 		al = rb_entry(node, struct annotation_line, rb_node);
1739 		next = rb_next(node);
1740 		rb_erase(node, src_root);
1741 
1742 		__resort_source_line(dest_root, al);
1743 		node = next;
1744 	}
1745 }
1746 
1747 static void print_summary(struct rb_root *root, const char *filename)
1748 {
1749 	struct annotation_line *al;
1750 	struct rb_node *node;
1751 
1752 	printf("\nSorted summary for file %s\n", filename);
1753 	printf("----------------------------------------------\n\n");
1754 
1755 	if (RB_EMPTY_ROOT(root)) {
1756 		printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
1757 		return;
1758 	}
1759 
1760 	node = rb_first(root);
1761 	while (node) {
1762 		double percent, percent_max = 0.0;
1763 		const char *color;
1764 		char *path;
1765 		int i;
1766 
1767 		al = rb_entry(node, struct annotation_line, rb_node);
1768 		for (i = 0; i < al->samples_nr; i++) {
1769 			percent = al->samples[i].percent_sum;
1770 			color = get_percent_color(percent);
1771 			color_fprintf(stdout, color, " %7.2f", percent);
1772 
1773 			if (percent > percent_max)
1774 				percent_max = percent;
1775 		}
1776 
1777 		path = al->path;
1778 		color = get_percent_color(percent_max);
1779 		color_fprintf(stdout, color, " %s\n", path);
1780 
1781 		node = rb_next(node);
1782 	}
1783 }
1784 
1785 static void symbol__annotate_hits(struct symbol *sym, struct perf_evsel *evsel)
1786 {
1787 	struct annotation *notes = symbol__annotation(sym);
1788 	struct sym_hist *h = annotation__histogram(notes, evsel->idx);
1789 	u64 len = symbol__size(sym), offset;
1790 
1791 	for (offset = 0; offset < len; ++offset)
1792 		if (h->addr[offset].nr_samples != 0)
1793 			printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
1794 			       sym->start + offset, h->addr[offset].nr_samples);
1795 	printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->nr_samples", h->nr_samples);
1796 }
1797 
1798 static int annotated_source__addr_fmt_width(struct list_head *lines, u64 start)
1799 {
1800 	char bf[32];
1801 	struct annotation_line *line;
1802 
1803 	list_for_each_entry_reverse(line, lines, node) {
1804 		if (line->offset != -1)
1805 			return scnprintf(bf, sizeof(bf), "%" PRIx64, start + line->offset);
1806 	}
1807 
1808 	return 0;
1809 }
1810 
1811 int symbol__annotate_printf(struct symbol *sym, struct map *map,
1812 			    struct perf_evsel *evsel, bool full_paths,
1813 			    int min_pcnt, int max_lines, int context)
1814 {
1815 	struct dso *dso = map->dso;
1816 	char *filename;
1817 	const char *d_filename;
1818 	const char *evsel_name = perf_evsel__name(evsel);
1819 	struct annotation *notes = symbol__annotation(sym);
1820 	struct sym_hist *h = annotation__histogram(notes, evsel->idx);
1821 	struct annotation_line *pos, *queue = NULL;
1822 	u64 start = map__rip_2objdump(map, sym->start);
1823 	int printed = 2, queue_len = 0, addr_fmt_width;
1824 	int more = 0;
1825 	u64 len;
1826 	int width = symbol_conf.show_total_period ? 12 : 8;
1827 	int graph_dotted_len;
1828 
1829 	filename = strdup(dso->long_name);
1830 	if (!filename)
1831 		return -ENOMEM;
1832 
1833 	if (full_paths)
1834 		d_filename = filename;
1835 	else
1836 		d_filename = basename(filename);
1837 
1838 	len = symbol__size(sym);
1839 
1840 	if (perf_evsel__is_group_event(evsel))
1841 		width *= evsel->nr_members;
1842 
1843 	graph_dotted_len = printf(" %-*.*s|	Source code & Disassembly of %s for %s (%" PRIu64 " samples)\n",
1844 				  width, width, symbol_conf.show_total_period ? "Period" :
1845 				  symbol_conf.show_nr_samples ? "Samples" : "Percent",
1846 				  d_filename, evsel_name, h->nr_samples);
1847 
1848 	printf("%-*.*s----\n",
1849 	       graph_dotted_len, graph_dotted_len, graph_dotted_line);
1850 
1851 	if (verbose > 0)
1852 		symbol__annotate_hits(sym, evsel);
1853 
1854 	addr_fmt_width = annotated_source__addr_fmt_width(&notes->src->source, start);
1855 
1856 	list_for_each_entry(pos, &notes->src->source, node) {
1857 		int err;
1858 
1859 		if (context && queue == NULL) {
1860 			queue = pos;
1861 			queue_len = 0;
1862 		}
1863 
1864 		err = annotation_line__print(pos, sym, start, evsel, len,
1865 					     min_pcnt, printed, max_lines,
1866 					     queue, addr_fmt_width);
1867 
1868 		switch (err) {
1869 		case 0:
1870 			++printed;
1871 			if (context) {
1872 				printed += queue_len;
1873 				queue = NULL;
1874 				queue_len = 0;
1875 			}
1876 			break;
1877 		case 1:
1878 			/* filtered by max_lines */
1879 			++more;
1880 			break;
1881 		case -1:
1882 		default:
1883 			/*
1884 			 * Filtered by min_pcnt or non IP lines when
1885 			 * context != 0
1886 			 */
1887 			if (!context)
1888 				break;
1889 			if (queue_len == context)
1890 				queue = list_entry(queue->node.next, typeof(*queue), node);
1891 			else
1892 				++queue_len;
1893 			break;
1894 		}
1895 	}
1896 
1897 	free(filename);
1898 
1899 	return more;
1900 }
1901 
1902 void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
1903 {
1904 	struct annotation *notes = symbol__annotation(sym);
1905 	struct sym_hist *h = annotation__histogram(notes, evidx);
1906 
1907 	memset(h, 0, notes->src->sizeof_sym_hist);
1908 }
1909 
1910 void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
1911 {
1912 	struct annotation *notes = symbol__annotation(sym);
1913 	struct sym_hist *h = annotation__histogram(notes, evidx);
1914 	int len = symbol__size(sym), offset;
1915 
1916 	h->nr_samples = 0;
1917 	for (offset = 0; offset < len; ++offset) {
1918 		h->addr[offset].nr_samples = h->addr[offset].nr_samples * 7 / 8;
1919 		h->nr_samples += h->addr[offset].nr_samples;
1920 	}
1921 }
1922 
1923 void annotated_source__purge(struct annotated_source *as)
1924 {
1925 	struct annotation_line *al, *n;
1926 
1927 	list_for_each_entry_safe(al, n, &as->source, node) {
1928 		list_del(&al->node);
1929 		disasm_line__free(disasm_line(al));
1930 	}
1931 }
1932 
1933 static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp)
1934 {
1935 	size_t printed;
1936 
1937 	if (dl->al.offset == -1)
1938 		return fprintf(fp, "%s\n", dl->al.line);
1939 
1940 	printed = fprintf(fp, "%#" PRIx64 " %s", dl->al.offset, dl->ins.name);
1941 
1942 	if (dl->ops.raw[0] != '\0') {
1943 		printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
1944 				   dl->ops.raw);
1945 	}
1946 
1947 	return printed + fprintf(fp, "\n");
1948 }
1949 
1950 size_t disasm__fprintf(struct list_head *head, FILE *fp)
1951 {
1952 	struct disasm_line *pos;
1953 	size_t printed = 0;
1954 
1955 	list_for_each_entry(pos, head, al.node)
1956 		printed += disasm_line__fprintf(pos, fp);
1957 
1958 	return printed;
1959 }
1960 
1961 static void annotation__calc_lines(struct annotation *notes, struct map *map,
1962 				  struct rb_root *root, u64 start)
1963 {
1964 	struct annotation_line *al;
1965 	struct rb_root tmp_root = RB_ROOT;
1966 
1967 	list_for_each_entry(al, &notes->src->source, node) {
1968 		double percent_max = 0.0;
1969 		int i;
1970 
1971 		for (i = 0; i < al->samples_nr; i++) {
1972 			struct annotation_data *sample;
1973 
1974 			sample = &al->samples[i];
1975 
1976 			if (sample->percent > percent_max)
1977 				percent_max = sample->percent;
1978 		}
1979 
1980 		if (percent_max <= 0.5)
1981 			continue;
1982 
1983 		al->path = get_srcline(map->dso, start + al->offset, NULL, false, true);
1984 		insert_source_line(&tmp_root, al);
1985 	}
1986 
1987 	resort_source_line(root, &tmp_root);
1988 }
1989 
1990 static void symbol__calc_lines(struct symbol *sym, struct map *map,
1991 			      struct rb_root *root)
1992 {
1993 	struct annotation *notes = symbol__annotation(sym);
1994 	u64 start = map__rip_2objdump(map, sym->start);
1995 
1996 	annotation__calc_lines(notes, map, root, start);
1997 }
1998 
1999 int symbol__tty_annotate(struct symbol *sym, struct map *map,
2000 			 struct perf_evsel *evsel, bool print_lines,
2001 			 bool full_paths, int min_pcnt, int max_lines)
2002 {
2003 	struct dso *dso = map->dso;
2004 	struct rb_root source_line = RB_ROOT;
2005 
2006 	if (symbol__annotate(sym, map, evsel, 0, NULL, NULL) < 0)
2007 		return -1;
2008 
2009 	if (print_lines) {
2010 		srcline_full_filename = full_paths;
2011 		symbol__calc_lines(sym, map, &source_line);
2012 		print_summary(&source_line, dso->long_name);
2013 	}
2014 
2015 	symbol__annotate_printf(sym, map, evsel, full_paths,
2016 				min_pcnt, max_lines, 0);
2017 
2018 	annotated_source__purge(symbol__annotation(sym)->src);
2019 
2020 	return 0;
2021 }
2022 
2023 bool ui__has_annotation(void)
2024 {
2025 	return use_browser == 1 && perf_hpp_list.sym;
2026 }
2027