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