• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
4  *
5  * Parts came from builtin-annotate.c, see those files for further
6  * copyright notes.
7  */
8 
9 #include <errno.h>
10 #include <inttypes.h>
11 #include <libgen.h>
12 #include <stdlib.h>
13 #include "util.h" // hex_width()
14 #include "ui/ui.h"
15 #include "sort.h"
16 #include "build-id.h"
17 #include "color.h"
18 #include "config.h"
19 #include "dso.h"
20 #include "env.h"
21 #include "map.h"
22 #include "maps.h"
23 #include "symbol.h"
24 #include "srcline.h"
25 #include "units.h"
26 #include "debug.h"
27 #include "annotate.h"
28 #include "evsel.h"
29 #include "evlist.h"
30 #include "bpf-event.h"
31 #include "bpf-utils.h"
32 #include "block-range.h"
33 #include "string2.h"
34 #include "util/event.h"
35 #include "util/sharded_mutex.h"
36 #include "arch/common.h"
37 #include "namespaces.h"
38 #include <regex.h>
39 #include <linux/bitops.h>
40 #include <linux/kernel.h>
41 #include <linux/string.h>
42 #include <linux/zalloc.h>
43 #include <subcmd/parse-options.h>
44 #include <subcmd/run-command.h>
45 
46 /* FIXME: For the HE_COLORSET */
47 #include "ui/browser.h"
48 
49 /*
50  * FIXME: Using the same values as slang.h,
51  * but that header may not be available everywhere
52  */
53 #define LARROW_CHAR	((unsigned char)',')
54 #define RARROW_CHAR	((unsigned char)'+')
55 #define DARROW_CHAR	((unsigned char)'.')
56 #define UARROW_CHAR	((unsigned char)'-')
57 
58 #include <linux/ctype.h>
59 
60 /* global annotation options */
61 struct annotation_options annotate_opts;
62 
63 static regex_t	 file_lineno;
64 
65 static struct ins_ops *ins__find(struct arch *arch, const char *name);
66 static void ins__sort(struct arch *arch);
67 static int disasm_line__parse(char *line, const char **namep, char **rawp);
68 static int call__scnprintf(struct ins *ins, char *bf, size_t size,
69 			  struct ins_operands *ops, int max_ins_name);
70 static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
71 			  struct ins_operands *ops, int max_ins_name);
72 
73 struct arch {
74 	const char	*name;
75 	struct ins	*instructions;
76 	size_t		nr_instructions;
77 	size_t		nr_instructions_allocated;
78 	struct ins_ops  *(*associate_instruction_ops)(struct arch *arch, const char *name);
79 	bool		sorted_instructions;
80 	bool		initialized;
81 	const char	*insn_suffix;
82 	void		*priv;
83 	unsigned int	model;
84 	unsigned int	family;
85 	int		(*init)(struct arch *arch, char *cpuid);
86 	bool		(*ins_is_fused)(struct arch *arch, const char *ins1,
87 					const char *ins2);
88 	struct		{
89 		char comment_char;
90 		char skip_functions_char;
91 	} objdump;
92 };
93 
94 static struct ins_ops call_ops;
95 static struct ins_ops dec_ops;
96 static struct ins_ops jump_ops;
97 static struct ins_ops mov_ops;
98 static struct ins_ops nop_ops;
99 static struct ins_ops lock_ops;
100 static struct ins_ops ret_ops;
101 
arch__grow_instructions(struct arch * arch)102 static int arch__grow_instructions(struct arch *arch)
103 {
104 	struct ins *new_instructions;
105 	size_t new_nr_allocated;
106 
107 	if (arch->nr_instructions_allocated == 0 && arch->instructions)
108 		goto grow_from_non_allocated_table;
109 
110 	new_nr_allocated = arch->nr_instructions_allocated + 128;
111 	new_instructions = realloc(arch->instructions, new_nr_allocated * sizeof(struct ins));
112 	if (new_instructions == NULL)
113 		return -1;
114 
115 out_update_instructions:
116 	arch->instructions = new_instructions;
117 	arch->nr_instructions_allocated = new_nr_allocated;
118 	return 0;
119 
120 grow_from_non_allocated_table:
121 	new_nr_allocated = arch->nr_instructions + 128;
122 	new_instructions = calloc(new_nr_allocated, sizeof(struct ins));
123 	if (new_instructions == NULL)
124 		return -1;
125 
126 	memcpy(new_instructions, arch->instructions, arch->nr_instructions);
127 	goto out_update_instructions;
128 }
129 
arch__associate_ins_ops(struct arch * arch,const char * name,struct ins_ops * ops)130 static int arch__associate_ins_ops(struct arch* arch, const char *name, struct ins_ops *ops)
131 {
132 	struct ins *ins;
133 
134 	if (arch->nr_instructions == arch->nr_instructions_allocated &&
135 	    arch__grow_instructions(arch))
136 		return -1;
137 
138 	ins = &arch->instructions[arch->nr_instructions];
139 	ins->name = strdup(name);
140 	if (!ins->name)
141 		return -1;
142 
143 	ins->ops  = ops;
144 	arch->nr_instructions++;
145 
146 	ins__sort(arch);
147 	return 0;
148 }
149 
150 #include "arch/arc/annotate/instructions.c"
151 #include "arch/arm/annotate/instructions.c"
152 #include "arch/arm64/annotate/instructions.c"
153 #include "arch/csky/annotate/instructions.c"
154 #include "arch/loongarch/annotate/instructions.c"
155 #include "arch/mips/annotate/instructions.c"
156 #include "arch/x86/annotate/instructions.c"
157 #include "arch/powerpc/annotate/instructions.c"
158 #include "arch/riscv64/annotate/instructions.c"
159 #include "arch/s390/annotate/instructions.c"
160 #include "arch/sparc/annotate/instructions.c"
161 
162 static struct arch architectures[] = {
163 	{
164 		.name = "arc",
165 		.init = arc__annotate_init,
166 	},
167 	{
168 		.name = "arm",
169 		.init = arm__annotate_init,
170 	},
171 	{
172 		.name = "arm64",
173 		.init = arm64__annotate_init,
174 	},
175 	{
176 		.name = "csky",
177 		.init = csky__annotate_init,
178 	},
179 	{
180 		.name = "mips",
181 		.init = mips__annotate_init,
182 		.objdump = {
183 			.comment_char = '#',
184 		},
185 	},
186 	{
187 		.name = "x86",
188 		.init = x86__annotate_init,
189 		.instructions = x86__instructions,
190 		.nr_instructions = ARRAY_SIZE(x86__instructions),
191 		.insn_suffix = "bwlq",
192 		.objdump =  {
193 			.comment_char = '#',
194 		},
195 	},
196 	{
197 		.name = "powerpc",
198 		.init = powerpc__annotate_init,
199 	},
200 	{
201 		.name = "riscv64",
202 		.init = riscv64__annotate_init,
203 	},
204 	{
205 		.name = "s390",
206 		.init = s390__annotate_init,
207 		.objdump =  {
208 			.comment_char = '#',
209 		},
210 	},
211 	{
212 		.name = "sparc",
213 		.init = sparc__annotate_init,
214 		.objdump = {
215 			.comment_char = '#',
216 		},
217 	},
218 	{
219 		.name = "loongarch",
220 		.init = loongarch__annotate_init,
221 		.objdump = {
222 			.comment_char = '#',
223 		},
224 	},
225 };
226 
ins__delete(struct ins_operands * ops)227 static void ins__delete(struct ins_operands *ops)
228 {
229 	if (ops == NULL)
230 		return;
231 	zfree(&ops->source.raw);
232 	zfree(&ops->source.name);
233 	zfree(&ops->target.raw);
234 	zfree(&ops->target.name);
235 }
236 
ins__raw_scnprintf(struct ins * ins,char * bf,size_t size,struct ins_operands * ops,int max_ins_name)237 static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size,
238 			      struct ins_operands *ops, int max_ins_name)
239 {
240 	return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->raw);
241 }
242 
ins__scnprintf(struct ins * ins,char * bf,size_t size,struct ins_operands * ops,int max_ins_name)243 int ins__scnprintf(struct ins *ins, char *bf, size_t size,
244 		   struct ins_operands *ops, int max_ins_name)
245 {
246 	if (ins->ops->scnprintf)
247 		return ins->ops->scnprintf(ins, bf, size, ops, max_ins_name);
248 
249 	return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
250 }
251 
ins__is_fused(struct arch * arch,const char * ins1,const char * ins2)252 bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2)
253 {
254 	if (!arch || !arch->ins_is_fused)
255 		return false;
256 
257 	return arch->ins_is_fused(arch, ins1, ins2);
258 }
259 
call__parse(struct arch * arch,struct ins_operands * ops,struct map_symbol * ms)260 static int call__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms)
261 {
262 	char *endptr, *tok, *name;
263 	struct map *map = ms->map;
264 	struct addr_map_symbol target = {
265 		.ms = { .map = map, },
266 	};
267 
268 	ops->target.addr = strtoull(ops->raw, &endptr, 16);
269 
270 	name = strchr(endptr, '<');
271 	if (name == NULL)
272 		goto indirect_call;
273 
274 	name++;
275 
276 	if (arch->objdump.skip_functions_char &&
277 	    strchr(name, arch->objdump.skip_functions_char))
278 		return -1;
279 
280 	tok = strchr(name, '>');
281 	if (tok == NULL)
282 		return -1;
283 
284 	*tok = '\0';
285 	ops->target.name = strdup(name);
286 	*tok = '>';
287 
288 	if (ops->target.name == NULL)
289 		return -1;
290 find_target:
291 	target.addr = map__objdump_2mem(map, ops->target.addr);
292 
293 	if (maps__find_ams(ms->maps, &target) == 0 &&
294 	    map__rip_2objdump(target.ms.map, map__map_ip(target.ms.map, target.addr)) == ops->target.addr)
295 		ops->target.sym = target.ms.sym;
296 
297 	return 0;
298 
299 indirect_call:
300 	tok = strchr(endptr, '*');
301 	if (tok != NULL) {
302 		endptr++;
303 
304 		/* Indirect call can use a non-rip register and offset: callq  *0x8(%rbx).
305 		 * Do not parse such instruction.  */
306 		if (strstr(endptr, "(%r") == NULL)
307 			ops->target.addr = strtoull(endptr, NULL, 16);
308 	}
309 	goto find_target;
310 }
311 
call__scnprintf(struct ins * ins,char * bf,size_t size,struct ins_operands * ops,int max_ins_name)312 static int call__scnprintf(struct ins *ins, char *bf, size_t size,
313 			   struct ins_operands *ops, int max_ins_name)
314 {
315 	if (ops->target.sym)
316 		return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.sym->name);
317 
318 	if (ops->target.addr == 0)
319 		return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
320 
321 	if (ops->target.name)
322 		return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.name);
323 
324 	return scnprintf(bf, size, "%-*s *%" PRIx64, max_ins_name, ins->name, ops->target.addr);
325 }
326 
327 static struct ins_ops call_ops = {
328 	.parse	   = call__parse,
329 	.scnprintf = call__scnprintf,
330 };
331 
ins__is_call(const struct ins * ins)332 bool ins__is_call(const struct ins *ins)
333 {
334 	return ins->ops == &call_ops || ins->ops == &s390_call_ops || ins->ops == &loongarch_call_ops;
335 }
336 
337 /*
338  * Prevents from matching commas in the comment section, e.g.:
339  * ffff200008446e70:       b.cs    ffff2000084470f4 <generic_exec_single+0x314>  // b.hs, b.nlast
340  *
341  * and skip comma as part of function arguments, e.g.:
342  * 1d8b4ac <linemap_lookup(line_maps const*, unsigned int)+0xcc>
343  */
validate_comma(const char * c,struct ins_operands * ops)344 static inline const char *validate_comma(const char *c, struct ins_operands *ops)
345 {
346 	if (ops->raw_comment && c > ops->raw_comment)
347 		return NULL;
348 
349 	if (ops->raw_func_start && c > ops->raw_func_start)
350 		return NULL;
351 
352 	return c;
353 }
354 
jump__parse(struct arch * arch,struct ins_operands * ops,struct map_symbol * ms)355 static int jump__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms)
356 {
357 	struct map *map = ms->map;
358 	struct symbol *sym = ms->sym;
359 	struct addr_map_symbol target = {
360 		.ms = { .map = map, },
361 	};
362 	const char *c = strchr(ops->raw, ',');
363 	u64 start, end;
364 
365 	ops->raw_comment = strchr(ops->raw, arch->objdump.comment_char);
366 	ops->raw_func_start = strchr(ops->raw, '<');
367 
368 	c = validate_comma(c, ops);
369 
370 	/*
371 	 * Examples of lines to parse for the _cpp_lex_token@@Base
372 	 * function:
373 	 *
374 	 * 1159e6c: jne    115aa32 <_cpp_lex_token@@Base+0xf92>
375 	 * 1159e8b: jne    c469be <cpp_named_operator2name@@Base+0xa72>
376 	 *
377 	 * The first is a jump to an offset inside the same function,
378 	 * the second is to another function, i.e. that 0xa72 is an
379 	 * offset in the cpp_named_operator2name@@base function.
380 	 */
381 	/*
382 	 * skip over possible up to 2 operands to get to address, e.g.:
383 	 * tbnz	 w0, #26, ffff0000083cd190 <security_file_permission+0xd0>
384 	 */
385 	if (c++ != NULL) {
386 		ops->target.addr = strtoull(c, NULL, 16);
387 		if (!ops->target.addr) {
388 			c = strchr(c, ',');
389 			c = validate_comma(c, ops);
390 			if (c++ != NULL)
391 				ops->target.addr = strtoull(c, NULL, 16);
392 		}
393 	} else {
394 		ops->target.addr = strtoull(ops->raw, NULL, 16);
395 	}
396 
397 	target.addr = map__objdump_2mem(map, ops->target.addr);
398 	start = map__unmap_ip(map, sym->start);
399 	end = map__unmap_ip(map, sym->end);
400 
401 	ops->target.outside = target.addr < start || target.addr > end;
402 
403 	/*
404 	 * FIXME: things like this in _cpp_lex_token (gcc's cc1 program):
405 
406 		cpp_named_operator2name@@Base+0xa72
407 
408 	 * Point to a place that is after the cpp_named_operator2name
409 	 * boundaries, i.e.  in the ELF symbol table for cc1
410 	 * cpp_named_operator2name is marked as being 32-bytes long, but it in
411 	 * fact is much larger than that, so we seem to need a symbols__find()
412 	 * routine that looks for >= current->start and  < next_symbol->start,
413 	 * possibly just for C++ objects?
414 	 *
415 	 * For now lets just make some progress by marking jumps to outside the
416 	 * current function as call like.
417 	 *
418 	 * Actual navigation will come next, with further understanding of how
419 	 * the symbol searching and disassembly should be done.
420 	 */
421 	if (maps__find_ams(ms->maps, &target) == 0 &&
422 	    map__rip_2objdump(target.ms.map, map__map_ip(target.ms.map, target.addr)) == ops->target.addr)
423 		ops->target.sym = target.ms.sym;
424 
425 	if (!ops->target.outside) {
426 		ops->target.offset = target.addr - start;
427 		ops->target.offset_avail = true;
428 	} else {
429 		ops->target.offset_avail = false;
430 	}
431 
432 	return 0;
433 }
434 
jump__scnprintf(struct ins * ins,char * bf,size_t size,struct ins_operands * ops,int max_ins_name)435 static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
436 			   struct ins_operands *ops, int max_ins_name)
437 {
438 	const char *c;
439 
440 	if (!ops->target.addr || ops->target.offset < 0)
441 		return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
442 
443 	if (ops->target.outside && ops->target.sym != NULL)
444 		return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.sym->name);
445 
446 	c = strchr(ops->raw, ',');
447 	c = validate_comma(c, ops);
448 
449 	if (c != NULL) {
450 		const char *c2 = strchr(c + 1, ',');
451 
452 		c2 = validate_comma(c2, ops);
453 		/* check for 3-op insn */
454 		if (c2 != NULL)
455 			c = c2;
456 		c++;
457 
458 		/* mirror arch objdump's space-after-comma style */
459 		if (*c == ' ')
460 			c++;
461 	}
462 
463 	return scnprintf(bf, size, "%-*s %.*s%" PRIx64, max_ins_name,
464 			 ins->name, c ? c - ops->raw : 0, ops->raw,
465 			 ops->target.offset);
466 }
467 
468 static struct ins_ops jump_ops = {
469 	.parse	   = jump__parse,
470 	.scnprintf = jump__scnprintf,
471 };
472 
ins__is_jump(const struct ins * ins)473 bool ins__is_jump(const struct ins *ins)
474 {
475 	return ins->ops == &jump_ops || ins->ops == &loongarch_jump_ops;
476 }
477 
comment__symbol(char * raw,char * comment,u64 * addrp,char ** namep)478 static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep)
479 {
480 	char *endptr, *name, *t;
481 
482 	if (strstr(raw, "(%rip)") == NULL)
483 		return 0;
484 
485 	*addrp = strtoull(comment, &endptr, 16);
486 	if (endptr == comment)
487 		return 0;
488 	name = strchr(endptr, '<');
489 	if (name == NULL)
490 		return -1;
491 
492 	name++;
493 
494 	t = strchr(name, '>');
495 	if (t == NULL)
496 		return 0;
497 
498 	*t = '\0';
499 	*namep = strdup(name);
500 	*t = '>';
501 
502 	return 0;
503 }
504 
lock__parse(struct arch * arch,struct ins_operands * ops,struct map_symbol * ms)505 static int lock__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms)
506 {
507 	ops->locked.ops = zalloc(sizeof(*ops->locked.ops));
508 	if (ops->locked.ops == NULL)
509 		return 0;
510 
511 	if (disasm_line__parse(ops->raw, &ops->locked.ins.name, &ops->locked.ops->raw) < 0)
512 		goto out_free_ops;
513 
514 	ops->locked.ins.ops = ins__find(arch, ops->locked.ins.name);
515 
516 	if (ops->locked.ins.ops == NULL)
517 		goto out_free_ops;
518 
519 	if (ops->locked.ins.ops->parse &&
520 	    ops->locked.ins.ops->parse(arch, ops->locked.ops, ms) < 0)
521 		goto out_free_ops;
522 
523 	return 0;
524 
525 out_free_ops:
526 	zfree(&ops->locked.ops);
527 	return 0;
528 }
529 
lock__scnprintf(struct ins * ins,char * bf,size_t size,struct ins_operands * ops,int max_ins_name)530 static int lock__scnprintf(struct ins *ins, char *bf, size_t size,
531 			   struct ins_operands *ops, int max_ins_name)
532 {
533 	int printed;
534 
535 	if (ops->locked.ins.ops == NULL)
536 		return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
537 
538 	printed = scnprintf(bf, size, "%-*s ", max_ins_name, ins->name);
539 	return printed + ins__scnprintf(&ops->locked.ins, bf + printed,
540 					size - printed, ops->locked.ops, max_ins_name);
541 }
542 
lock__delete(struct ins_operands * ops)543 static void lock__delete(struct ins_operands *ops)
544 {
545 	struct ins *ins = &ops->locked.ins;
546 
547 	if (ins->ops && ins->ops->free)
548 		ins->ops->free(ops->locked.ops);
549 	else
550 		ins__delete(ops->locked.ops);
551 
552 	zfree(&ops->locked.ops);
553 	zfree(&ops->target.raw);
554 	zfree(&ops->target.name);
555 }
556 
557 static struct ins_ops lock_ops = {
558 	.free	   = lock__delete,
559 	.parse	   = lock__parse,
560 	.scnprintf = lock__scnprintf,
561 };
562 
mov__parse(struct arch * arch,struct ins_operands * ops,struct map_symbol * ms __maybe_unused)563 static int mov__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms __maybe_unused)
564 {
565 	char *s = strchr(ops->raw, ','), *target, *comment, prev;
566 
567 	if (s == NULL)
568 		return -1;
569 
570 	*s = '\0';
571 
572 	/*
573 	 * x86 SIB addressing has something like 0x8(%rax, %rcx, 1)
574 	 * then it needs to have the closing parenthesis.
575 	 */
576 	if (strchr(ops->raw, '(')) {
577 		*s = ',';
578 		s = strchr(ops->raw, ')');
579 		if (s == NULL || s[1] != ',')
580 			return -1;
581 		*++s = '\0';
582 	}
583 
584 	ops->source.raw = strdup(ops->raw);
585 	*s = ',';
586 
587 	if (ops->source.raw == NULL)
588 		return -1;
589 
590 	target = skip_spaces(++s);
591 	comment = strchr(s, arch->objdump.comment_char);
592 
593 	if (comment != NULL)
594 		s = comment - 1;
595 	else
596 		s = strchr(s, '\0') - 1;
597 
598 	while (s > target && isspace(s[0]))
599 		--s;
600 	s++;
601 	prev = *s;
602 	*s = '\0';
603 
604 	ops->target.raw = strdup(target);
605 	*s = prev;
606 
607 	if (ops->target.raw == NULL)
608 		goto out_free_source;
609 
610 	if (comment == NULL)
611 		return 0;
612 
613 	comment = skip_spaces(comment);
614 	comment__symbol(ops->source.raw, comment + 1, &ops->source.addr, &ops->source.name);
615 	comment__symbol(ops->target.raw, comment + 1, &ops->target.addr, &ops->target.name);
616 
617 	return 0;
618 
619 out_free_source:
620 	zfree(&ops->source.raw);
621 	return -1;
622 }
623 
mov__scnprintf(struct ins * ins,char * bf,size_t size,struct ins_operands * ops,int max_ins_name)624 static int mov__scnprintf(struct ins *ins, char *bf, size_t size,
625 			   struct ins_operands *ops, int max_ins_name)
626 {
627 	return scnprintf(bf, size, "%-*s %s,%s", max_ins_name, ins->name,
628 			 ops->source.name ?: ops->source.raw,
629 			 ops->target.name ?: ops->target.raw);
630 }
631 
632 static struct ins_ops mov_ops = {
633 	.parse	   = mov__parse,
634 	.scnprintf = mov__scnprintf,
635 };
636 
dec__parse(struct arch * arch __maybe_unused,struct ins_operands * ops,struct map_symbol * ms __maybe_unused)637 static int dec__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map_symbol *ms __maybe_unused)
638 {
639 	char *target, *comment, *s, prev;
640 
641 	target = s = ops->raw;
642 
643 	while (s[0] != '\0' && !isspace(s[0]))
644 		++s;
645 	prev = *s;
646 	*s = '\0';
647 
648 	ops->target.raw = strdup(target);
649 	*s = prev;
650 
651 	if (ops->target.raw == NULL)
652 		return -1;
653 
654 	comment = strchr(s, arch->objdump.comment_char);
655 	if (comment == NULL)
656 		return 0;
657 
658 	comment = skip_spaces(comment);
659 	comment__symbol(ops->target.raw, comment + 1, &ops->target.addr, &ops->target.name);
660 
661 	return 0;
662 }
663 
dec__scnprintf(struct ins * ins,char * bf,size_t size,struct ins_operands * ops,int max_ins_name)664 static int dec__scnprintf(struct ins *ins, char *bf, size_t size,
665 			   struct ins_operands *ops, int max_ins_name)
666 {
667 	return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name,
668 			 ops->target.name ?: ops->target.raw);
669 }
670 
671 static struct ins_ops dec_ops = {
672 	.parse	   = dec__parse,
673 	.scnprintf = dec__scnprintf,
674 };
675 
nop__scnprintf(struct ins * ins __maybe_unused,char * bf,size_t size,struct ins_operands * ops __maybe_unused,int max_ins_name)676 static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size,
677 			  struct ins_operands *ops __maybe_unused, int max_ins_name)
678 {
679 	return scnprintf(bf, size, "%-*s", max_ins_name, "nop");
680 }
681 
682 static struct ins_ops nop_ops = {
683 	.scnprintf = nop__scnprintf,
684 };
685 
686 static struct ins_ops ret_ops = {
687 	.scnprintf = ins__raw_scnprintf,
688 };
689 
ins__is_ret(const struct ins * ins)690 bool ins__is_ret(const struct ins *ins)
691 {
692 	return ins->ops == &ret_ops;
693 }
694 
ins__is_lock(const struct ins * ins)695 bool ins__is_lock(const struct ins *ins)
696 {
697 	return ins->ops == &lock_ops;
698 }
699 
ins__key_cmp(const void * name,const void * insp)700 static int ins__key_cmp(const void *name, const void *insp)
701 {
702 	const struct ins *ins = insp;
703 
704 	return strcmp(name, ins->name);
705 }
706 
ins__cmp(const void * a,const void * b)707 static int ins__cmp(const void *a, const void *b)
708 {
709 	const struct ins *ia = a;
710 	const struct ins *ib = b;
711 
712 	return strcmp(ia->name, ib->name);
713 }
714 
ins__sort(struct arch * arch)715 static void ins__sort(struct arch *arch)
716 {
717 	const int nmemb = arch->nr_instructions;
718 
719 	qsort(arch->instructions, nmemb, sizeof(struct ins), ins__cmp);
720 }
721 
__ins__find(struct arch * arch,const char * name)722 static struct ins_ops *__ins__find(struct arch *arch, const char *name)
723 {
724 	struct ins *ins;
725 	const int nmemb = arch->nr_instructions;
726 
727 	if (!arch->sorted_instructions) {
728 		ins__sort(arch);
729 		arch->sorted_instructions = true;
730 	}
731 
732 	ins = bsearch(name, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp);
733 	if (ins)
734 		return ins->ops;
735 
736 	if (arch->insn_suffix) {
737 		char tmp[32];
738 		char suffix;
739 		size_t len = strlen(name);
740 
741 		if (len == 0 || len >= sizeof(tmp))
742 			return NULL;
743 
744 		suffix = name[len - 1];
745 		if (strchr(arch->insn_suffix, suffix) == NULL)
746 			return NULL;
747 
748 		strcpy(tmp, name);
749 		tmp[len - 1] = '\0'; /* remove the suffix and check again */
750 
751 		ins = bsearch(tmp, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp);
752 	}
753 	return ins ? ins->ops : NULL;
754 }
755 
ins__find(struct arch * arch,const char * name)756 static struct ins_ops *ins__find(struct arch *arch, const char *name)
757 {
758 	struct ins_ops *ops = __ins__find(arch, name);
759 
760 	if (!ops && arch->associate_instruction_ops)
761 		ops = arch->associate_instruction_ops(arch, name);
762 
763 	return ops;
764 }
765 
arch__key_cmp(const void * name,const void * archp)766 static int arch__key_cmp(const void *name, const void *archp)
767 {
768 	const struct arch *arch = archp;
769 
770 	return strcmp(name, arch->name);
771 }
772 
arch__cmp(const void * a,const void * b)773 static int arch__cmp(const void *a, const void *b)
774 {
775 	const struct arch *aa = a;
776 	const struct arch *ab = b;
777 
778 	return strcmp(aa->name, ab->name);
779 }
780 
arch__sort(void)781 static void arch__sort(void)
782 {
783 	const int nmemb = ARRAY_SIZE(architectures);
784 
785 	qsort(architectures, nmemb, sizeof(struct arch), arch__cmp);
786 }
787 
arch__find(const char * name)788 static struct arch *arch__find(const char *name)
789 {
790 	const int nmemb = ARRAY_SIZE(architectures);
791 	static bool sorted;
792 
793 	if (!sorted) {
794 		arch__sort();
795 		sorted = true;
796 	}
797 
798 	return bsearch(name, architectures, nmemb, sizeof(struct arch), arch__key_cmp);
799 }
800 
annotated_source__new(void)801 static struct annotated_source *annotated_source__new(void)
802 {
803 	struct annotated_source *src = zalloc(sizeof(*src));
804 
805 	if (src != NULL)
806 		INIT_LIST_HEAD(&src->source);
807 
808 	return src;
809 }
810 
annotated_source__delete(struct annotated_source * src)811 static __maybe_unused void annotated_source__delete(struct annotated_source *src)
812 {
813 	if (src == NULL)
814 		return;
815 	zfree(&src->histograms);
816 	zfree(&src->cycles_hist);
817 	free(src);
818 }
819 
annotated_source__alloc_histograms(struct annotated_source * src,size_t size,int nr_hists)820 static int annotated_source__alloc_histograms(struct annotated_source *src,
821 					      size_t size, int nr_hists)
822 {
823 	size_t sizeof_sym_hist;
824 
825 	/*
826 	 * Add buffer of one element for zero length symbol.
827 	 * When sample is taken from first instruction of
828 	 * zero length symbol, perf still resolves it and
829 	 * shows symbol name in perf report and allows to
830 	 * annotate it.
831 	 */
832 	if (size == 0)
833 		size = 1;
834 
835 	/* Check for overflow when calculating sizeof_sym_hist */
836 	if (size > (SIZE_MAX - sizeof(struct sym_hist)) / sizeof(struct sym_hist_entry))
837 		return -1;
838 
839 	sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(struct sym_hist_entry));
840 
841 	/* Check for overflow in zalloc argument */
842 	if (sizeof_sym_hist > SIZE_MAX / nr_hists)
843 		return -1;
844 
845 	src->sizeof_sym_hist = sizeof_sym_hist;
846 	src->nr_histograms   = nr_hists;
847 	src->histograms	     = calloc(nr_hists, sizeof_sym_hist) ;
848 	return src->histograms ? 0 : -1;
849 }
850 
851 /* The cycles histogram is lazily allocated. */
symbol__alloc_hist_cycles(struct symbol * sym)852 static int symbol__alloc_hist_cycles(struct symbol *sym)
853 {
854 	struct annotation *notes = symbol__annotation(sym);
855 	const size_t size = symbol__size(sym);
856 
857 	notes->src->cycles_hist = calloc(size, sizeof(struct cyc_hist));
858 	if (notes->src->cycles_hist == NULL)
859 		return -1;
860 	return 0;
861 }
862 
symbol__annotate_zero_histograms(struct symbol * sym)863 void symbol__annotate_zero_histograms(struct symbol *sym)
864 {
865 	struct annotation *notes = symbol__annotation(sym);
866 
867 	annotation__lock(notes);
868 	if (notes->src != NULL) {
869 		memset(notes->src->histograms, 0,
870 		       notes->src->nr_histograms * notes->src->sizeof_sym_hist);
871 		if (notes->src->cycles_hist)
872 			memset(notes->src->cycles_hist, 0,
873 				symbol__size(sym) * sizeof(struct cyc_hist));
874 	}
875 	annotation__unlock(notes);
876 }
877 
__symbol__account_cycles(struct cyc_hist * ch,u64 start,unsigned offset,unsigned cycles,unsigned have_start)878 static int __symbol__account_cycles(struct cyc_hist *ch,
879 				    u64 start,
880 				    unsigned offset, unsigned cycles,
881 				    unsigned have_start)
882 {
883 	/*
884 	 * For now we can only account one basic block per
885 	 * final jump. But multiple could be overlapping.
886 	 * Always account the longest one. So when
887 	 * a shorter one has been already seen throw it away.
888 	 *
889 	 * We separately always account the full cycles.
890 	 */
891 	ch[offset].num_aggr++;
892 	ch[offset].cycles_aggr += cycles;
893 
894 	if (cycles > ch[offset].cycles_max)
895 		ch[offset].cycles_max = cycles;
896 
897 	if (ch[offset].cycles_min) {
898 		if (cycles && cycles < ch[offset].cycles_min)
899 			ch[offset].cycles_min = cycles;
900 	} else
901 		ch[offset].cycles_min = cycles;
902 
903 	if (!have_start && ch[offset].have_start)
904 		return 0;
905 	if (ch[offset].num) {
906 		if (have_start && (!ch[offset].have_start ||
907 				   ch[offset].start > start)) {
908 			ch[offset].have_start = 0;
909 			ch[offset].cycles = 0;
910 			ch[offset].num = 0;
911 			if (ch[offset].reset < 0xffff)
912 				ch[offset].reset++;
913 		} else if (have_start &&
914 			   ch[offset].start < start)
915 			return 0;
916 	}
917 
918 	if (ch[offset].num < NUM_SPARKS)
919 		ch[offset].cycles_spark[ch[offset].num] = cycles;
920 
921 	ch[offset].have_start = have_start;
922 	ch[offset].start = start;
923 	ch[offset].cycles += cycles;
924 	ch[offset].num++;
925 	return 0;
926 }
927 
__symbol__inc_addr_samples(struct map_symbol * ms,struct annotated_source * src,int evidx,u64 addr,struct perf_sample * sample)928 static int __symbol__inc_addr_samples(struct map_symbol *ms,
929 				      struct annotated_source *src, int evidx, u64 addr,
930 				      struct perf_sample *sample)
931 {
932 	struct symbol *sym = ms->sym;
933 	unsigned offset;
934 	struct sym_hist *h;
935 
936 	pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map__unmap_ip(ms->map, addr));
937 
938 	if ((addr < sym->start || addr >= sym->end) &&
939 	    (addr != sym->end || sym->start != sym->end)) {
940 		pr_debug("%s(%d): ERANGE! sym->name=%s, start=%#" PRIx64 ", addr=%#" PRIx64 ", end=%#" PRIx64 "\n",
941 		       __func__, __LINE__, sym->name, sym->start, addr, sym->end);
942 		return -ERANGE;
943 	}
944 
945 	offset = addr - sym->start;
946 	h = annotated_source__histogram(src, evidx);
947 	if (h == NULL) {
948 		pr_debug("%s(%d): ENOMEM! sym->name=%s, start=%#" PRIx64 ", addr=%#" PRIx64 ", end=%#" PRIx64 ", func: %d\n",
949 			 __func__, __LINE__, sym->name, sym->start, addr, sym->end, sym->type == STT_FUNC);
950 		return -ENOMEM;
951 	}
952 	h->nr_samples++;
953 	h->addr[offset].nr_samples++;
954 	h->period += sample->period;
955 	h->addr[offset].period += sample->period;
956 
957 	pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
958 		  ", evidx=%d] => nr_samples: %" PRIu64 ", period: %" PRIu64 "\n",
959 		  sym->start, sym->name, addr, addr - sym->start, evidx,
960 		  h->addr[offset].nr_samples, h->addr[offset].period);
961 	return 0;
962 }
963 
symbol__cycles_hist(struct symbol * sym)964 static struct cyc_hist *symbol__cycles_hist(struct symbol *sym)
965 {
966 	struct annotation *notes = symbol__annotation(sym);
967 
968 	if (notes->src == NULL) {
969 		notes->src = annotated_source__new();
970 		if (notes->src == NULL)
971 			return NULL;
972 		goto alloc_cycles_hist;
973 	}
974 
975 	if (!notes->src->cycles_hist) {
976 alloc_cycles_hist:
977 		symbol__alloc_hist_cycles(sym);
978 	}
979 
980 	return notes->src->cycles_hist;
981 }
982 
symbol__hists(struct symbol * sym,int nr_hists)983 struct annotated_source *symbol__hists(struct symbol *sym, int nr_hists)
984 {
985 	struct annotation *notes = symbol__annotation(sym);
986 
987 	if (notes->src == NULL) {
988 		notes->src = annotated_source__new();
989 		if (notes->src == NULL)
990 			return NULL;
991 		goto alloc_histograms;
992 	}
993 
994 	if (notes->src->histograms == NULL) {
995 alloc_histograms:
996 		annotated_source__alloc_histograms(notes->src, symbol__size(sym),
997 						   nr_hists);
998 	}
999 
1000 	return notes->src;
1001 }
1002 
symbol__inc_addr_samples(struct map_symbol * ms,struct evsel * evsel,u64 addr,struct perf_sample * sample)1003 static int symbol__inc_addr_samples(struct map_symbol *ms,
1004 				    struct evsel *evsel, u64 addr,
1005 				    struct perf_sample *sample)
1006 {
1007 	struct symbol *sym = ms->sym;
1008 	struct annotated_source *src;
1009 
1010 	if (sym == NULL)
1011 		return 0;
1012 	src = symbol__hists(sym, evsel->evlist->core.nr_entries);
1013 	return src ? __symbol__inc_addr_samples(ms, src, evsel->core.idx, addr, sample) : 0;
1014 }
1015 
symbol__account_cycles(u64 addr,u64 start,struct symbol * sym,unsigned cycles)1016 static int symbol__account_cycles(u64 addr, u64 start,
1017 				  struct symbol *sym, unsigned cycles)
1018 {
1019 	struct cyc_hist *cycles_hist;
1020 	unsigned offset;
1021 
1022 	if (sym == NULL)
1023 		return 0;
1024 	cycles_hist = symbol__cycles_hist(sym);
1025 	if (cycles_hist == NULL)
1026 		return -ENOMEM;
1027 	if (addr < sym->start || addr >= sym->end)
1028 		return -ERANGE;
1029 
1030 	if (start) {
1031 		if (start < sym->start || start >= sym->end)
1032 			return -ERANGE;
1033 		if (start >= addr)
1034 			start = 0;
1035 	}
1036 	offset = addr - sym->start;
1037 	return __symbol__account_cycles(cycles_hist,
1038 					start ? start - sym->start : 0,
1039 					offset, cycles,
1040 					!!start);
1041 }
1042 
addr_map_symbol__account_cycles(struct addr_map_symbol * ams,struct addr_map_symbol * start,unsigned cycles)1043 int addr_map_symbol__account_cycles(struct addr_map_symbol *ams,
1044 				    struct addr_map_symbol *start,
1045 				    unsigned cycles)
1046 {
1047 	u64 saddr = 0;
1048 	int err;
1049 
1050 	if (!cycles)
1051 		return 0;
1052 
1053 	/*
1054 	 * Only set start when IPC can be computed. We can only
1055 	 * compute it when the basic block is completely in a single
1056 	 * function.
1057 	 * Special case the case when the jump is elsewhere, but
1058 	 * it starts on the function start.
1059 	 */
1060 	if (start &&
1061 		(start->ms.sym == ams->ms.sym ||
1062 		 (ams->ms.sym &&
1063 		  start->addr == ams->ms.sym->start + map__start(ams->ms.map))))
1064 		saddr = start->al_addr;
1065 	if (saddr == 0)
1066 		pr_debug2("BB with bad start: addr %"PRIx64" start %"PRIx64" sym %"PRIx64" saddr %"PRIx64"\n",
1067 			ams->addr,
1068 			start ? start->addr : 0,
1069 			ams->ms.sym ? ams->ms.sym->start + map__start(ams->ms.map) : 0,
1070 			saddr);
1071 	err = symbol__account_cycles(ams->al_addr, saddr, ams->ms.sym, cycles);
1072 	if (err)
1073 		pr_debug2("account_cycles failed %d\n", err);
1074 	return err;
1075 }
1076 
annotation__count_insn(struct annotation * notes,u64 start,u64 end)1077 static unsigned annotation__count_insn(struct annotation *notes, u64 start, u64 end)
1078 {
1079 	unsigned n_insn = 0;
1080 	u64 offset;
1081 
1082 	for (offset = start; offset <= end; offset++) {
1083 		if (notes->offsets[offset])
1084 			n_insn++;
1085 	}
1086 	return n_insn;
1087 }
1088 
annotation__count_and_fill(struct annotation * notes,u64 start,u64 end,struct cyc_hist * ch)1089 static void annotation__count_and_fill(struct annotation *notes, u64 start, u64 end, struct cyc_hist *ch)
1090 {
1091 	unsigned n_insn;
1092 	unsigned int cover_insn = 0;
1093 	u64 offset;
1094 
1095 	n_insn = annotation__count_insn(notes, start, end);
1096 	if (n_insn && ch->num && ch->cycles) {
1097 		float ipc = n_insn / ((double)ch->cycles / (double)ch->num);
1098 
1099 		/* Hide data when there are too many overlaps. */
1100 		if (ch->reset >= 0x7fff)
1101 			return;
1102 
1103 		for (offset = start; offset <= end; offset++) {
1104 			struct annotation_line *al = notes->offsets[offset];
1105 
1106 			if (al && al->cycles && al->cycles->ipc == 0.0) {
1107 				al->cycles->ipc = ipc;
1108 				cover_insn++;
1109 			}
1110 		}
1111 
1112 		if (cover_insn) {
1113 			notes->hit_cycles += ch->cycles;
1114 			notes->hit_insn += n_insn * ch->num;
1115 			notes->cover_insn += cover_insn;
1116 		}
1117 	}
1118 }
1119 
annotation__compute_ipc(struct annotation * notes,size_t size)1120 static int annotation__compute_ipc(struct annotation *notes, size_t size)
1121 {
1122 	int err = 0;
1123 	s64 offset;
1124 
1125 	if (!notes->src || !notes->src->cycles_hist)
1126 		return 0;
1127 
1128 	notes->total_insn = annotation__count_insn(notes, 0, size - 1);
1129 	notes->hit_cycles = 0;
1130 	notes->hit_insn = 0;
1131 	notes->cover_insn = 0;
1132 
1133 	annotation__lock(notes);
1134 	for (offset = size - 1; offset >= 0; --offset) {
1135 		struct cyc_hist *ch;
1136 
1137 		ch = &notes->src->cycles_hist[offset];
1138 		if (ch && ch->cycles) {
1139 			struct annotation_line *al;
1140 
1141 			al = notes->offsets[offset];
1142 			if (al && al->cycles == NULL) {
1143 				al->cycles = zalloc(sizeof(*al->cycles));
1144 				if (al->cycles == NULL) {
1145 					err = ENOMEM;
1146 					break;
1147 				}
1148 			}
1149 			if (ch->have_start)
1150 				annotation__count_and_fill(notes, ch->start, offset, ch);
1151 			if (al && ch->num_aggr) {
1152 				al->cycles->avg = ch->cycles_aggr / ch->num_aggr;
1153 				al->cycles->max = ch->cycles_max;
1154 				al->cycles->min = ch->cycles_min;
1155 			}
1156 			notes->have_cycles = true;
1157 		}
1158 	}
1159 
1160 	if (err) {
1161 		while (++offset < (s64)size) {
1162 			struct cyc_hist *ch = &notes->src->cycles_hist[offset];
1163 
1164 			if (ch && ch->cycles) {
1165 				struct annotation_line *al = notes->offsets[offset];
1166 				if (al)
1167 					zfree(&al->cycles);
1168 			}
1169 		}
1170 	}
1171 
1172 	annotation__unlock(notes);
1173 	return 0;
1174 }
1175 
addr_map_symbol__inc_samples(struct addr_map_symbol * ams,struct perf_sample * sample,struct evsel * evsel)1176 int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, struct perf_sample *sample,
1177 				 struct evsel *evsel)
1178 {
1179 	return symbol__inc_addr_samples(&ams->ms, evsel, ams->al_addr, sample);
1180 }
1181 
hist_entry__inc_addr_samples(struct hist_entry * he,struct perf_sample * sample,struct evsel * evsel,u64 ip)1182 int hist_entry__inc_addr_samples(struct hist_entry *he, struct perf_sample *sample,
1183 				 struct evsel *evsel, u64 ip)
1184 {
1185 	return symbol__inc_addr_samples(&he->ms, evsel, ip, sample);
1186 }
1187 
disasm_line__init_ins(struct disasm_line * dl,struct arch * arch,struct map_symbol * ms)1188 static void disasm_line__init_ins(struct disasm_line *dl, struct arch *arch, struct map_symbol *ms)
1189 {
1190 	dl->ins.ops = ins__find(arch, dl->ins.name);
1191 
1192 	if (!dl->ins.ops)
1193 		return;
1194 
1195 	if (dl->ins.ops->parse && dl->ins.ops->parse(arch, &dl->ops, ms) < 0)
1196 		dl->ins.ops = NULL;
1197 }
1198 
disasm_line__parse(char * line,const char ** namep,char ** rawp)1199 static int disasm_line__parse(char *line, const char **namep, char **rawp)
1200 {
1201 	char tmp, *name = skip_spaces(line);
1202 
1203 	if (name[0] == '\0')
1204 		return -1;
1205 
1206 	*rawp = name + 1;
1207 
1208 	while ((*rawp)[0] != '\0' && !isspace((*rawp)[0]))
1209 		++*rawp;
1210 
1211 	tmp = (*rawp)[0];
1212 	(*rawp)[0] = '\0';
1213 	*namep = strdup(name);
1214 
1215 	if (*namep == NULL)
1216 		goto out;
1217 
1218 	(*rawp)[0] = tmp;
1219 	*rawp = strim(*rawp);
1220 
1221 	return 0;
1222 
1223 out:
1224 	return -1;
1225 }
1226 
1227 struct annotate_args {
1228 	struct arch		  *arch;
1229 	struct map_symbol	  ms;
1230 	struct evsel		  *evsel;
1231 	struct annotation_options *options;
1232 	s64			  offset;
1233 	char			  *line;
1234 	int			  line_nr;
1235 	char			  *fileloc;
1236 };
1237 
annotation_line__init(struct annotation_line * al,struct annotate_args * args,int nr)1238 static void annotation_line__init(struct annotation_line *al,
1239 				  struct annotate_args *args,
1240 				  int nr)
1241 {
1242 	al->offset = args->offset;
1243 	al->line = strdup(args->line);
1244 	al->line_nr = args->line_nr;
1245 	al->fileloc = args->fileloc;
1246 	al->data_nr = nr;
1247 }
1248 
annotation_line__exit(struct annotation_line * al)1249 static void annotation_line__exit(struct annotation_line *al)
1250 {
1251 	zfree_srcline(&al->path);
1252 	zfree(&al->line);
1253 	zfree(&al->cycles);
1254 }
1255 
disasm_line_size(int nr)1256 static size_t disasm_line_size(int nr)
1257 {
1258 	struct annotation_line *al;
1259 
1260 	return (sizeof(struct disasm_line) + (sizeof(al->data[0]) * nr));
1261 }
1262 
1263 /*
1264  * Allocating the disasm annotation line data with
1265  * following structure:
1266  *
1267  *    -------------------------------------------
1268  *    struct disasm_line | struct annotation_line
1269  *    -------------------------------------------
1270  *
1271  * We have 'struct annotation_line' member as last member
1272  * of 'struct disasm_line' to have an easy access.
1273  */
disasm_line__new(struct annotate_args * args)1274 static struct disasm_line *disasm_line__new(struct annotate_args *args)
1275 {
1276 	struct disasm_line *dl = NULL;
1277 	int nr = 1;
1278 
1279 	if (evsel__is_group_event(args->evsel))
1280 		nr = args->evsel->core.nr_members;
1281 
1282 	dl = zalloc(disasm_line_size(nr));
1283 	if (!dl)
1284 		return NULL;
1285 
1286 	annotation_line__init(&dl->al, args, nr);
1287 	if (dl->al.line == NULL)
1288 		goto out_delete;
1289 
1290 	if (args->offset != -1) {
1291 		if (disasm_line__parse(dl->al.line, &dl->ins.name, &dl->ops.raw) < 0)
1292 			goto out_free_line;
1293 
1294 		disasm_line__init_ins(dl, args->arch, &args->ms);
1295 	}
1296 
1297 	return dl;
1298 
1299 out_free_line:
1300 	zfree(&dl->al.line);
1301 out_delete:
1302 	free(dl);
1303 	return NULL;
1304 }
1305 
disasm_line__free(struct disasm_line * dl)1306 void disasm_line__free(struct disasm_line *dl)
1307 {
1308 	if (dl->ins.ops && dl->ins.ops->free)
1309 		dl->ins.ops->free(&dl->ops);
1310 	else
1311 		ins__delete(&dl->ops);
1312 	zfree(&dl->ins.name);
1313 	annotation_line__exit(&dl->al);
1314 	free(dl);
1315 }
1316 
disasm_line__scnprintf(struct disasm_line * dl,char * bf,size_t size,bool raw,int max_ins_name)1317 int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw, int max_ins_name)
1318 {
1319 	if (raw || !dl->ins.ops)
1320 		return scnprintf(bf, size, "%-*s %s", max_ins_name, dl->ins.name, dl->ops.raw);
1321 
1322 	return ins__scnprintf(&dl->ins, bf, size, &dl->ops, max_ins_name);
1323 }
1324 
annotation__exit(struct annotation * notes)1325 void annotation__exit(struct annotation *notes)
1326 {
1327 	annotated_source__delete(notes->src);
1328 }
1329 
1330 static struct sharded_mutex *sharded_mutex;
1331 
annotation__init_sharded_mutex(void)1332 static void annotation__init_sharded_mutex(void)
1333 {
1334 	/* As many mutexes as there are CPUs. */
1335 	sharded_mutex = sharded_mutex__new(cpu__max_present_cpu().cpu);
1336 }
1337 
annotation__hash(const struct annotation * notes)1338 static size_t annotation__hash(const struct annotation *notes)
1339 {
1340 	return (size_t)notes;
1341 }
1342 
annotation__get_mutex(const struct annotation * notes)1343 static struct mutex *annotation__get_mutex(const struct annotation *notes)
1344 {
1345 	static pthread_once_t once = PTHREAD_ONCE_INIT;
1346 
1347 	pthread_once(&once, annotation__init_sharded_mutex);
1348 	if (!sharded_mutex)
1349 		return NULL;
1350 
1351 	return sharded_mutex__get_mutex(sharded_mutex, annotation__hash(notes));
1352 }
1353 
annotation__lock(struct annotation * notes)1354 void annotation__lock(struct annotation *notes)
1355 	NO_THREAD_SAFETY_ANALYSIS
1356 {
1357 	struct mutex *mutex = annotation__get_mutex(notes);
1358 
1359 	if (mutex)
1360 		mutex_lock(mutex);
1361 }
1362 
annotation__unlock(struct annotation * notes)1363 void annotation__unlock(struct annotation *notes)
1364 	NO_THREAD_SAFETY_ANALYSIS
1365 {
1366 	struct mutex *mutex = annotation__get_mutex(notes);
1367 
1368 	if (mutex)
1369 		mutex_unlock(mutex);
1370 }
1371 
annotation__trylock(struct annotation * notes)1372 bool annotation__trylock(struct annotation *notes)
1373 {
1374 	struct mutex *mutex = annotation__get_mutex(notes);
1375 
1376 	if (!mutex)
1377 		return false;
1378 
1379 	return mutex_trylock(mutex);
1380 }
1381 
1382 
annotation_line__add(struct annotation_line * al,struct list_head * head)1383 static void annotation_line__add(struct annotation_line *al, struct list_head *head)
1384 {
1385 	list_add_tail(&al->node, head);
1386 }
1387 
1388 struct annotation_line *
annotation_line__next(struct annotation_line * pos,struct list_head * head)1389 annotation_line__next(struct annotation_line *pos, struct list_head *head)
1390 {
1391 	list_for_each_entry_continue(pos, head, node)
1392 		if (pos->offset >= 0)
1393 			return pos;
1394 
1395 	return NULL;
1396 }
1397 
annotate__address_color(struct block_range * br)1398 static const char *annotate__address_color(struct block_range *br)
1399 {
1400 	double cov = block_range__coverage(br);
1401 
1402 	if (cov >= 0) {
1403 		/* mark red for >75% coverage */
1404 		if (cov > 0.75)
1405 			return PERF_COLOR_RED;
1406 
1407 		/* mark dull for <1% coverage */
1408 		if (cov < 0.01)
1409 			return PERF_COLOR_NORMAL;
1410 	}
1411 
1412 	return PERF_COLOR_MAGENTA;
1413 }
1414 
annotate__asm_color(struct block_range * br)1415 static const char *annotate__asm_color(struct block_range *br)
1416 {
1417 	double cov = block_range__coverage(br);
1418 
1419 	if (cov >= 0) {
1420 		/* mark dull for <1% coverage */
1421 		if (cov < 0.01)
1422 			return PERF_COLOR_NORMAL;
1423 	}
1424 
1425 	return PERF_COLOR_BLUE;
1426 }
1427 
annotate__branch_printf(struct block_range * br,u64 addr)1428 static void annotate__branch_printf(struct block_range *br, u64 addr)
1429 {
1430 	bool emit_comment = true;
1431 
1432 	if (!br)
1433 		return;
1434 
1435 #if 1
1436 	if (br->is_target && br->start == addr) {
1437 		struct block_range *branch = br;
1438 		double p;
1439 
1440 		/*
1441 		 * Find matching branch to our target.
1442 		 */
1443 		while (!branch->is_branch)
1444 			branch = block_range__next(branch);
1445 
1446 		p = 100 *(double)br->entry / branch->coverage;
1447 
1448 		if (p > 0.1) {
1449 			if (emit_comment) {
1450 				emit_comment = false;
1451 				printf("\t#");
1452 			}
1453 
1454 			/*
1455 			 * The percentage of coverage joined at this target in relation
1456 			 * to the next branch.
1457 			 */
1458 			printf(" +%.2f%%", p);
1459 		}
1460 	}
1461 #endif
1462 	if (br->is_branch && br->end == addr) {
1463 		double p = 100*(double)br->taken / br->coverage;
1464 
1465 		if (p > 0.1) {
1466 			if (emit_comment) {
1467 				emit_comment = false;
1468 				printf("\t#");
1469 			}
1470 
1471 			/*
1472 			 * The percentage of coverage leaving at this branch, and
1473 			 * its prediction ratio.
1474 			 */
1475 			printf(" -%.2f%% (p:%.2f%%)", p, 100*(double)br->pred  / br->taken);
1476 		}
1477 	}
1478 }
1479 
disasm_line__print(struct disasm_line * dl,u64 start,int addr_fmt_width)1480 static int disasm_line__print(struct disasm_line *dl, u64 start, int addr_fmt_width)
1481 {
1482 	s64 offset = dl->al.offset;
1483 	const u64 addr = start + offset;
1484 	struct block_range *br;
1485 
1486 	br = block_range__find(addr);
1487 	color_fprintf(stdout, annotate__address_color(br), "  %*" PRIx64 ":", addr_fmt_width, addr);
1488 	color_fprintf(stdout, annotate__asm_color(br), "%s", dl->al.line);
1489 	annotate__branch_printf(br, addr);
1490 	return 0;
1491 }
1492 
1493 static int
annotation_line__print(struct annotation_line * al,struct symbol * sym,u64 start,struct evsel * evsel,u64 len,int min_pcnt,int printed,int max_lines,struct annotation_line * queue,int addr_fmt_width,int percent_type)1494 annotation_line__print(struct annotation_line *al, struct symbol *sym, u64 start,
1495 		       struct evsel *evsel, u64 len, int min_pcnt, int printed,
1496 		       int max_lines, struct annotation_line *queue, int addr_fmt_width,
1497 		       int percent_type)
1498 {
1499 	struct disasm_line *dl = container_of(al, struct disasm_line, al);
1500 	static const char *prev_line;
1501 
1502 	if (al->offset != -1) {
1503 		double max_percent = 0.0;
1504 		int i, nr_percent = 1;
1505 		const char *color;
1506 		struct annotation *notes = symbol__annotation(sym);
1507 
1508 		for (i = 0; i < al->data_nr; i++) {
1509 			double percent;
1510 
1511 			percent = annotation_data__percent(&al->data[i],
1512 							   percent_type);
1513 
1514 			if (percent > max_percent)
1515 				max_percent = percent;
1516 		}
1517 
1518 		if (al->data_nr > nr_percent)
1519 			nr_percent = al->data_nr;
1520 
1521 		if (max_percent < min_pcnt)
1522 			return -1;
1523 
1524 		if (max_lines && printed >= max_lines)
1525 			return 1;
1526 
1527 		if (queue != NULL) {
1528 			list_for_each_entry_from(queue, &notes->src->source, node) {
1529 				if (queue == al)
1530 					break;
1531 				annotation_line__print(queue, sym, start, evsel, len,
1532 						       0, 0, 1, NULL, addr_fmt_width,
1533 						       percent_type);
1534 			}
1535 		}
1536 
1537 		color = get_percent_color(max_percent);
1538 
1539 		for (i = 0; i < nr_percent; i++) {
1540 			struct annotation_data *data = &al->data[i];
1541 			double percent;
1542 
1543 			percent = annotation_data__percent(data, percent_type);
1544 			color = get_percent_color(percent);
1545 
1546 			if (symbol_conf.show_total_period)
1547 				color_fprintf(stdout, color, " %11" PRIu64,
1548 					      data->he.period);
1549 			else if (symbol_conf.show_nr_samples)
1550 				color_fprintf(stdout, color, " %7" PRIu64,
1551 					      data->he.nr_samples);
1552 			else
1553 				color_fprintf(stdout, color, " %7.2f", percent);
1554 		}
1555 
1556 		printf(" : ");
1557 
1558 		disasm_line__print(dl, start, addr_fmt_width);
1559 
1560 		/*
1561 		 * Also color the filename and line if needed, with
1562 		 * the same color than the percentage. Don't print it
1563 		 * twice for close colored addr with the same filename:line
1564 		 */
1565 		if (al->path) {
1566 			if (!prev_line || strcmp(prev_line, al->path)) {
1567 				color_fprintf(stdout, color, " // %s", al->path);
1568 				prev_line = al->path;
1569 			}
1570 		}
1571 
1572 		printf("\n");
1573 	} else if (max_lines && printed >= max_lines)
1574 		return 1;
1575 	else {
1576 		int width = symbol_conf.show_total_period ? 12 : 8;
1577 
1578 		if (queue)
1579 			return -1;
1580 
1581 		if (evsel__is_group_event(evsel))
1582 			width *= evsel->core.nr_members;
1583 
1584 		if (!*al->line)
1585 			printf(" %*s:\n", width, " ");
1586 		else
1587 			printf(" %*s: %-*d %s\n", width, " ", addr_fmt_width, al->line_nr, al->line);
1588 	}
1589 
1590 	return 0;
1591 }
1592 
1593 /*
1594  * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw)
1595  * which looks like following
1596  *
1597  *  0000000000415500 <_init>:
1598  *    415500:       sub    $0x8,%rsp
1599  *    415504:       mov    0x2f5ad5(%rip),%rax        # 70afe0 <_DYNAMIC+0x2f8>
1600  *    41550b:       test   %rax,%rax
1601  *    41550e:       je     415515 <_init+0x15>
1602  *    415510:       callq  416e70 <__gmon_start__@plt>
1603  *    415515:       add    $0x8,%rsp
1604  *    415519:       retq
1605  *
1606  * it will be parsed and saved into struct disasm_line as
1607  *  <offset>       <name>  <ops.raw>
1608  *
1609  * The offset will be a relative offset from the start of the symbol and -1
1610  * means that it's not a disassembly line so should be treated differently.
1611  * The ops.raw part will be parsed further according to type of the instruction.
1612  */
symbol__parse_objdump_line(struct symbol * sym,struct annotate_args * args,char * parsed_line,int * line_nr,char ** fileloc)1613 static int symbol__parse_objdump_line(struct symbol *sym,
1614 				      struct annotate_args *args,
1615 				      char *parsed_line, int *line_nr, char **fileloc)
1616 {
1617 	struct map *map = args->ms.map;
1618 	struct annotation *notes = symbol__annotation(sym);
1619 	struct disasm_line *dl;
1620 	char *tmp;
1621 	s64 line_ip, offset = -1;
1622 	regmatch_t match[2];
1623 
1624 	/* /filename:linenr ? Save line number and ignore. */
1625 	if (regexec(&file_lineno, parsed_line, 2, match, 0) == 0) {
1626 		*line_nr = atoi(parsed_line + match[1].rm_so);
1627 		free(*fileloc);
1628 		*fileloc = strdup(parsed_line);
1629 		return 0;
1630 	}
1631 
1632 	/* Process hex address followed by ':'. */
1633 	line_ip = strtoull(parsed_line, &tmp, 16);
1634 	if (parsed_line != tmp && tmp[0] == ':' && tmp[1] != '\0') {
1635 		u64 start = map__rip_2objdump(map, sym->start),
1636 		    end = map__rip_2objdump(map, sym->end);
1637 
1638 		offset = line_ip - start;
1639 		if ((u64)line_ip < start || (u64)line_ip >= end)
1640 			offset = -1;
1641 		else
1642 			parsed_line = tmp + 1;
1643 	}
1644 
1645 	args->offset  = offset;
1646 	args->line    = parsed_line;
1647 	args->line_nr = *line_nr;
1648 	args->fileloc = *fileloc;
1649 	args->ms.sym  = sym;
1650 
1651 	dl = disasm_line__new(args);
1652 	(*line_nr)++;
1653 
1654 	if (dl == NULL)
1655 		return -1;
1656 
1657 	if (!disasm_line__has_local_offset(dl)) {
1658 		dl->ops.target.offset = dl->ops.target.addr -
1659 					map__rip_2objdump(map, sym->start);
1660 		dl->ops.target.offset_avail = true;
1661 	}
1662 
1663 	/* kcore has no symbols, so add the call target symbol */
1664 	if (dl->ins.ops && ins__is_call(&dl->ins) && !dl->ops.target.sym) {
1665 		struct addr_map_symbol target = {
1666 			.addr = dl->ops.target.addr,
1667 			.ms = { .map = map, },
1668 		};
1669 
1670 		if (!maps__find_ams(args->ms.maps, &target) &&
1671 		    target.ms.sym->start == target.al_addr)
1672 			dl->ops.target.sym = target.ms.sym;
1673 	}
1674 
1675 	annotation_line__add(&dl->al, &notes->src->source);
1676 	return 0;
1677 }
1678 
symbol__init_regexpr(void)1679 static __attribute__((constructor)) void symbol__init_regexpr(void)
1680 {
1681 	regcomp(&file_lineno, "^/[^:]+:([0-9]+)", REG_EXTENDED);
1682 }
1683 
delete_last_nop(struct symbol * sym)1684 static void delete_last_nop(struct symbol *sym)
1685 {
1686 	struct annotation *notes = symbol__annotation(sym);
1687 	struct list_head *list = &notes->src->source;
1688 	struct disasm_line *dl;
1689 
1690 	while (!list_empty(list)) {
1691 		dl = list_entry(list->prev, struct disasm_line, al.node);
1692 
1693 		if (dl->ins.ops) {
1694 			if (dl->ins.ops != &nop_ops)
1695 				return;
1696 		} else {
1697 			if (!strstr(dl->al.line, " nop ") &&
1698 			    !strstr(dl->al.line, " nopl ") &&
1699 			    !strstr(dl->al.line, " nopw "))
1700 				return;
1701 		}
1702 
1703 		list_del_init(&dl->al.node);
1704 		disasm_line__free(dl);
1705 	}
1706 }
1707 
symbol__strerror_disassemble(struct map_symbol * ms,int errnum,char * buf,size_t buflen)1708 int symbol__strerror_disassemble(struct map_symbol *ms, int errnum, char *buf, size_t buflen)
1709 {
1710 	struct dso *dso = map__dso(ms->map);
1711 
1712 	BUG_ON(buflen == 0);
1713 
1714 	if (errnum >= 0) {
1715 		str_error_r(errnum, buf, buflen);
1716 		return 0;
1717 	}
1718 
1719 	switch (errnum) {
1720 	case SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX: {
1721 		char bf[SBUILD_ID_SIZE + 15] = " with build id ";
1722 		char *build_id_msg = NULL;
1723 
1724 		if (dso->has_build_id) {
1725 			build_id__sprintf(&dso->bid, bf + 15);
1726 			build_id_msg = bf;
1727 		}
1728 		scnprintf(buf, buflen,
1729 			  "No vmlinux file%s\nwas found in the path.\n\n"
1730 			  "Note that annotation using /proc/kcore requires CAP_SYS_RAWIO capability.\n\n"
1731 			  "Please use:\n\n"
1732 			  "  perf buildid-cache -vu vmlinux\n\n"
1733 			  "or:\n\n"
1734 			  "  --vmlinux vmlinux\n", build_id_msg ?: "");
1735 	}
1736 		break;
1737 	case SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF:
1738 		scnprintf(buf, buflen, "Please link with binutils's libopcode to enable BPF annotation");
1739 		break;
1740 	case SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_REGEXP:
1741 		scnprintf(buf, buflen, "Problems with arch specific instruction name regular expressions.");
1742 		break;
1743 	case SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_CPUID_PARSING:
1744 		scnprintf(buf, buflen, "Problems while parsing the CPUID in the arch specific initialization.");
1745 		break;
1746 	case SYMBOL_ANNOTATE_ERRNO__BPF_INVALID_FILE:
1747 		scnprintf(buf, buflen, "Invalid BPF file: %s.", dso->long_name);
1748 		break;
1749 	case SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF:
1750 		scnprintf(buf, buflen, "The %s BPF file has no BTF section, compile with -g or use pahole -J.",
1751 			  dso->long_name);
1752 		break;
1753 	default:
1754 		scnprintf(buf, buflen, "Internal error: Invalid %d error code\n", errnum);
1755 		break;
1756 	}
1757 
1758 	return 0;
1759 }
1760 
dso__disassemble_filename(struct dso * dso,char * filename,size_t filename_size)1761 static int dso__disassemble_filename(struct dso *dso, char *filename, size_t filename_size)
1762 {
1763 	char linkname[PATH_MAX];
1764 	char *build_id_filename;
1765 	char *build_id_path = NULL;
1766 	char *pos;
1767 	int len;
1768 
1769 	if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
1770 	    !dso__is_kcore(dso))
1771 		return SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX;
1772 
1773 	build_id_filename = dso__build_id_filename(dso, NULL, 0, false);
1774 	if (build_id_filename) {
1775 		__symbol__join_symfs(filename, filename_size, build_id_filename);
1776 		free(build_id_filename);
1777 	} else {
1778 		if (dso->has_build_id)
1779 			return ENOMEM;
1780 		goto fallback;
1781 	}
1782 
1783 	build_id_path = strdup(filename);
1784 	if (!build_id_path)
1785 		return ENOMEM;
1786 
1787 	/*
1788 	 * old style build-id cache has name of XX/XXXXXXX.. while
1789 	 * new style has XX/XXXXXXX../{elf,kallsyms,vdso}.
1790 	 * extract the build-id part of dirname in the new style only.
1791 	 */
1792 	pos = strrchr(build_id_path, '/');
1793 	if (pos && strlen(pos) < SBUILD_ID_SIZE - 2)
1794 		dirname(build_id_path);
1795 
1796 	if (dso__is_kcore(dso))
1797 		goto fallback;
1798 
1799 	len = readlink(build_id_path, linkname, sizeof(linkname) - 1);
1800 	if (len < 0)
1801 		goto fallback;
1802 
1803 	linkname[len] = '\0';
1804 	if (strstr(linkname, DSO__NAME_KALLSYMS) ||
1805 		access(filename, R_OK)) {
1806 fallback:
1807 		/*
1808 		 * If we don't have build-ids or the build-id file isn't in the
1809 		 * cache, or is just a kallsyms file, well, lets hope that this
1810 		 * DSO is the same as when 'perf record' ran.
1811 		 */
1812 		if (dso->kernel && dso->long_name[0] == '/')
1813 			snprintf(filename, filename_size, "%s", dso->long_name);
1814 		else
1815 			__symbol__join_symfs(filename, filename_size, dso->long_name);
1816 
1817 		mutex_lock(&dso->lock);
1818 		if (access(filename, R_OK) && errno == ENOENT && dso->nsinfo) {
1819 			char *new_name = dso__filename_with_chroot(dso, filename);
1820 			if (new_name) {
1821 				strlcpy(filename, new_name, filename_size);
1822 				free(new_name);
1823 			}
1824 		}
1825 		mutex_unlock(&dso->lock);
1826 	}
1827 
1828 	free(build_id_path);
1829 	return 0;
1830 }
1831 
1832 #if defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBBPF_SUPPORT)
1833 #define PACKAGE "perf"
1834 #include <bfd.h>
1835 #include <dis-asm.h>
1836 #include <bpf/bpf.h>
1837 #include <bpf/btf.h>
1838 #include <bpf/libbpf.h>
1839 #include <linux/btf.h>
1840 #include <tools/dis-asm-compat.h>
1841 
symbol__disassemble_bpf(struct symbol * sym,struct annotate_args * args)1842 static int symbol__disassemble_bpf(struct symbol *sym,
1843 				   struct annotate_args *args)
1844 {
1845 	struct annotation *notes = symbol__annotation(sym);
1846 	struct bpf_prog_linfo *prog_linfo = NULL;
1847 	struct bpf_prog_info_node *info_node;
1848 	int len = sym->end - sym->start;
1849 	disassembler_ftype disassemble;
1850 	struct map *map = args->ms.map;
1851 	struct perf_bpil *info_linear;
1852 	struct disassemble_info info;
1853 	struct dso *dso = map__dso(map);
1854 	int pc = 0, count, sub_id;
1855 	struct btf *btf = NULL;
1856 	char tpath[PATH_MAX];
1857 	size_t buf_size;
1858 	int nr_skip = 0;
1859 	char *buf;
1860 	bfd *bfdf;
1861 	int ret;
1862 	FILE *s;
1863 
1864 	if (dso->binary_type != DSO_BINARY_TYPE__BPF_PROG_INFO)
1865 		return SYMBOL_ANNOTATE_ERRNO__BPF_INVALID_FILE;
1866 
1867 	pr_debug("%s: handling sym %s addr %" PRIx64 " len %" PRIx64 "\n", __func__,
1868 		  sym->name, sym->start, sym->end - sym->start);
1869 
1870 	memset(tpath, 0, sizeof(tpath));
1871 	perf_exe(tpath, sizeof(tpath));
1872 
1873 	bfdf = bfd_openr(tpath, NULL);
1874 	if (bfdf == NULL)
1875 		abort();
1876 
1877 	if (!bfd_check_format(bfdf, bfd_object))
1878 		abort();
1879 
1880 	s = open_memstream(&buf, &buf_size);
1881 	if (!s) {
1882 		ret = errno;
1883 		goto out;
1884 	}
1885 	init_disassemble_info_compat(&info, s,
1886 				     (fprintf_ftype) fprintf,
1887 				     fprintf_styled);
1888 	info.arch = bfd_get_arch(bfdf);
1889 	info.mach = bfd_get_mach(bfdf);
1890 
1891 	info_node = perf_env__find_bpf_prog_info(dso->bpf_prog.env,
1892 						 dso->bpf_prog.id);
1893 	if (!info_node) {
1894 		ret = SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF;
1895 		goto out;
1896 	}
1897 	info_linear = info_node->info_linear;
1898 	sub_id = dso->bpf_prog.sub_id;
1899 
1900 	info.buffer = (void *)(uintptr_t)(info_linear->info.jited_prog_insns);
1901 	info.buffer_length = info_linear->info.jited_prog_len;
1902 
1903 	if (info_linear->info.nr_line_info)
1904 		prog_linfo = bpf_prog_linfo__new(&info_linear->info);
1905 
1906 	if (info_linear->info.btf_id) {
1907 		struct btf_node *node;
1908 
1909 		node = perf_env__find_btf(dso->bpf_prog.env,
1910 					  info_linear->info.btf_id);
1911 		if (node)
1912 			btf = btf__new((__u8 *)(node->data),
1913 				       node->data_size);
1914 	}
1915 
1916 	disassemble_init_for_target(&info);
1917 
1918 #ifdef DISASM_FOUR_ARGS_SIGNATURE
1919 	disassemble = disassembler(info.arch,
1920 				   bfd_big_endian(bfdf),
1921 				   info.mach,
1922 				   bfdf);
1923 #else
1924 	disassemble = disassembler(bfdf);
1925 #endif
1926 	if (disassemble == NULL)
1927 		abort();
1928 
1929 	fflush(s);
1930 	do {
1931 		const struct bpf_line_info *linfo = NULL;
1932 		struct disasm_line *dl;
1933 		size_t prev_buf_size;
1934 		const char *srcline;
1935 		u64 addr;
1936 
1937 		addr = pc + ((u64 *)(uintptr_t)(info_linear->info.jited_ksyms))[sub_id];
1938 		count = disassemble(pc, &info);
1939 
1940 		if (prog_linfo)
1941 			linfo = bpf_prog_linfo__lfind_addr_func(prog_linfo,
1942 								addr, sub_id,
1943 								nr_skip);
1944 
1945 		if (linfo && btf) {
1946 			srcline = btf__name_by_offset(btf, linfo->line_off);
1947 			nr_skip++;
1948 		} else
1949 			srcline = NULL;
1950 
1951 		fprintf(s, "\n");
1952 		prev_buf_size = buf_size;
1953 		fflush(s);
1954 
1955 		if (!annotate_opts.hide_src_code && srcline) {
1956 			args->offset = -1;
1957 			args->line = strdup(srcline);
1958 			args->line_nr = 0;
1959 			args->fileloc = NULL;
1960 			args->ms.sym  = sym;
1961 			dl = disasm_line__new(args);
1962 			if (dl) {
1963 				annotation_line__add(&dl->al,
1964 						     &notes->src->source);
1965 			}
1966 		}
1967 
1968 		args->offset = pc;
1969 		args->line = buf + prev_buf_size;
1970 		args->line_nr = 0;
1971 		args->fileloc = NULL;
1972 		args->ms.sym  = sym;
1973 		dl = disasm_line__new(args);
1974 		if (dl)
1975 			annotation_line__add(&dl->al, &notes->src->source);
1976 
1977 		pc += count;
1978 	} while (count > 0 && pc < len);
1979 
1980 	ret = 0;
1981 out:
1982 	free(prog_linfo);
1983 	btf__free(btf);
1984 	fclose(s);
1985 	bfd_close(bfdf);
1986 	return ret;
1987 }
1988 #else // defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBBPF_SUPPORT)
symbol__disassemble_bpf(struct symbol * sym __maybe_unused,struct annotate_args * args __maybe_unused)1989 static int symbol__disassemble_bpf(struct symbol *sym __maybe_unused,
1990 				   struct annotate_args *args __maybe_unused)
1991 {
1992 	return SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF;
1993 }
1994 #endif // defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBBPF_SUPPORT)
1995 
1996 static int
symbol__disassemble_bpf_image(struct symbol * sym,struct annotate_args * args)1997 symbol__disassemble_bpf_image(struct symbol *sym,
1998 			      struct annotate_args *args)
1999 {
2000 	struct annotation *notes = symbol__annotation(sym);
2001 	struct disasm_line *dl;
2002 
2003 	args->offset = -1;
2004 	args->line = strdup("to be implemented");
2005 	args->line_nr = 0;
2006 	args->fileloc = NULL;
2007 	dl = disasm_line__new(args);
2008 	if (dl)
2009 		annotation_line__add(&dl->al, &notes->src->source);
2010 
2011 	zfree(&args->line);
2012 	return 0;
2013 }
2014 
2015 /*
2016  * Possibly create a new version of line with tabs expanded. Returns the
2017  * existing or new line, storage is updated if a new line is allocated. If
2018  * allocation fails then NULL is returned.
2019  */
expand_tabs(char * line,char ** storage,size_t * storage_len)2020 static char *expand_tabs(char *line, char **storage, size_t *storage_len)
2021 {
2022 	size_t i, src, dst, len, new_storage_len, num_tabs;
2023 	char *new_line;
2024 	size_t line_len = strlen(line);
2025 
2026 	for (num_tabs = 0, i = 0; i < line_len; i++)
2027 		if (line[i] == '\t')
2028 			num_tabs++;
2029 
2030 	if (num_tabs == 0)
2031 		return line;
2032 
2033 	/*
2034 	 * Space for the line and '\0', less the leading and trailing
2035 	 * spaces. Each tab may introduce 7 additional spaces.
2036 	 */
2037 	new_storage_len = line_len + 1 + (num_tabs * 7);
2038 
2039 	new_line = malloc(new_storage_len);
2040 	if (new_line == NULL) {
2041 		pr_err("Failure allocating memory for tab expansion\n");
2042 		return NULL;
2043 	}
2044 
2045 	/*
2046 	 * Copy regions starting at src and expand tabs. If there are two
2047 	 * adjacent tabs then 'src == i', the memcpy is of size 0 and the spaces
2048 	 * are inserted.
2049 	 */
2050 	for (i = 0, src = 0, dst = 0; i < line_len && num_tabs; i++) {
2051 		if (line[i] == '\t') {
2052 			len = i - src;
2053 			memcpy(&new_line[dst], &line[src], len);
2054 			dst += len;
2055 			new_line[dst++] = ' ';
2056 			while (dst % 8 != 0)
2057 				new_line[dst++] = ' ';
2058 			src = i + 1;
2059 			num_tabs--;
2060 		}
2061 	}
2062 
2063 	/* Expand the last region. */
2064 	len = line_len - src;
2065 	memcpy(&new_line[dst], &line[src], len);
2066 	dst += len;
2067 	new_line[dst] = '\0';
2068 
2069 	free(*storage);
2070 	*storage = new_line;
2071 	*storage_len = new_storage_len;
2072 	return new_line;
2073 
2074 }
2075 
symbol__disassemble(struct symbol * sym,struct annotate_args * args)2076 static int symbol__disassemble(struct symbol *sym, struct annotate_args *args)
2077 {
2078 	struct annotation_options *opts = &annotate_opts;
2079 	struct map *map = args->ms.map;
2080 	struct dso *dso = map__dso(map);
2081 	char *command;
2082 	FILE *file;
2083 	char symfs_filename[PATH_MAX];
2084 	struct kcore_extract kce;
2085 	bool delete_extract = false;
2086 	bool decomp = false;
2087 	int lineno = 0;
2088 	char *fileloc = NULL;
2089 	int nline;
2090 	char *line;
2091 	size_t line_len;
2092 	const char *objdump_argv[] = {
2093 		"/bin/sh",
2094 		"-c",
2095 		NULL, /* Will be the objdump command to run. */
2096 		"--",
2097 		NULL, /* Will be the symfs path. */
2098 		NULL,
2099 	};
2100 	struct child_process objdump_process;
2101 	int err = dso__disassemble_filename(dso, symfs_filename, sizeof(symfs_filename));
2102 
2103 	if (err)
2104 		return err;
2105 
2106 	pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
2107 		 symfs_filename, sym->name, map__unmap_ip(map, sym->start),
2108 		 map__unmap_ip(map, sym->end));
2109 
2110 	pr_debug("annotating [%p] %30s : [%p] %30s\n",
2111 		 dso, dso->long_name, sym, sym->name);
2112 
2113 	if (dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO) {
2114 		return symbol__disassemble_bpf(sym, args);
2115 	} else if (dso->binary_type == DSO_BINARY_TYPE__BPF_IMAGE) {
2116 		return symbol__disassemble_bpf_image(sym, args);
2117 	} else if (dso__is_kcore(dso)) {
2118 		kce.kcore_filename = symfs_filename;
2119 		kce.addr = map__rip_2objdump(map, sym->start);
2120 		kce.offs = sym->start;
2121 		kce.len = sym->end - sym->start;
2122 		if (!kcore_extract__create(&kce)) {
2123 			delete_extract = true;
2124 			strlcpy(symfs_filename, kce.extract_filename,
2125 				sizeof(symfs_filename));
2126 		}
2127 	} else if (dso__needs_decompress(dso)) {
2128 		char tmp[KMOD_DECOMP_LEN];
2129 
2130 		if (dso__decompress_kmodule_path(dso, symfs_filename,
2131 						 tmp, sizeof(tmp)) < 0)
2132 			return -1;
2133 
2134 		decomp = true;
2135 		strcpy(symfs_filename, tmp);
2136 	}
2137 
2138 	err = asprintf(&command,
2139 		 "%s %s%s --start-address=0x%016" PRIx64
2140 		 " --stop-address=0x%016" PRIx64
2141 		 " -l -d %s %s %s %c%s%c %s%s -C \"$1\"",
2142 		 opts->objdump_path ?: "objdump",
2143 		 opts->disassembler_style ? "-M " : "",
2144 		 opts->disassembler_style ?: "",
2145 		 map__rip_2objdump(map, sym->start),
2146 		 map__rip_2objdump(map, sym->end),
2147 		 opts->show_asm_raw ? "" : "--no-show-raw-insn",
2148 		 opts->annotate_src ? "-S" : "",
2149 		 opts->prefix ? "--prefix " : "",
2150 		 opts->prefix ? '"' : ' ',
2151 		 opts->prefix ?: "",
2152 		 opts->prefix ? '"' : ' ',
2153 		 opts->prefix_strip ? "--prefix-strip=" : "",
2154 		 opts->prefix_strip ?: "");
2155 
2156 	if (err < 0) {
2157 		pr_err("Failure allocating memory for the command to run\n");
2158 		goto out_remove_tmp;
2159 	}
2160 
2161 	pr_debug("Executing: %s\n", command);
2162 
2163 	objdump_argv[2] = command;
2164 	objdump_argv[4] = symfs_filename;
2165 
2166 	/* Create a pipe to read from for stdout */
2167 	memset(&objdump_process, 0, sizeof(objdump_process));
2168 	objdump_process.argv = objdump_argv;
2169 	objdump_process.out = -1;
2170 	objdump_process.err = -1;
2171 	objdump_process.no_stderr = 1;
2172 	if (start_command(&objdump_process)) {
2173 		pr_err("Failure starting to run %s\n", command);
2174 		err = -1;
2175 		goto out_free_command;
2176 	}
2177 
2178 	file = fdopen(objdump_process.out, "r");
2179 	if (!file) {
2180 		pr_err("Failure creating FILE stream for %s\n", command);
2181 		/*
2182 		 * If we were using debug info should retry with
2183 		 * original binary.
2184 		 */
2185 		err = -1;
2186 		goto out_close_stdout;
2187 	}
2188 
2189 	/* Storage for getline. */
2190 	line = NULL;
2191 	line_len = 0;
2192 
2193 	nline = 0;
2194 	while (!feof(file)) {
2195 		const char *match;
2196 		char *expanded_line;
2197 
2198 		if (getline(&line, &line_len, file) < 0 || !line)
2199 			break;
2200 
2201 		/* Skip lines containing "filename:" */
2202 		match = strstr(line, symfs_filename);
2203 		if (match && match[strlen(symfs_filename)] == ':')
2204 			continue;
2205 
2206 		expanded_line = strim(line);
2207 		expanded_line = expand_tabs(expanded_line, &line, &line_len);
2208 		if (!expanded_line)
2209 			break;
2210 
2211 		/*
2212 		 * The source code line number (lineno) needs to be kept in
2213 		 * across calls to symbol__parse_objdump_line(), so that it
2214 		 * can associate it with the instructions till the next one.
2215 		 * See disasm_line__new() and struct disasm_line::line_nr.
2216 		 */
2217 		if (symbol__parse_objdump_line(sym, args, expanded_line,
2218 					       &lineno, &fileloc) < 0)
2219 			break;
2220 		nline++;
2221 	}
2222 	free(line);
2223 	free(fileloc);
2224 
2225 	err = finish_command(&objdump_process);
2226 	if (err)
2227 		pr_err("Error running %s\n", command);
2228 
2229 	if (nline == 0) {
2230 		err = -1;
2231 		pr_err("No output from %s\n", command);
2232 	}
2233 
2234 	/*
2235 	 * kallsyms does not have symbol sizes so there may a nop at the end.
2236 	 * Remove it.
2237 	 */
2238 	if (dso__is_kcore(dso))
2239 		delete_last_nop(sym);
2240 
2241 	fclose(file);
2242 
2243 out_close_stdout:
2244 	close(objdump_process.out);
2245 
2246 out_free_command:
2247 	free(command);
2248 
2249 out_remove_tmp:
2250 	if (decomp)
2251 		unlink(symfs_filename);
2252 
2253 	if (delete_extract)
2254 		kcore_extract__delete(&kce);
2255 
2256 	return err;
2257 }
2258 
calc_percent(struct sym_hist * sym_hist,struct hists * hists,struct annotation_data * data,s64 offset,s64 end)2259 static void calc_percent(struct sym_hist *sym_hist,
2260 			 struct hists *hists,
2261 			 struct annotation_data *data,
2262 			 s64 offset, s64 end)
2263 {
2264 	unsigned int hits = 0;
2265 	u64 period = 0;
2266 
2267 	while (offset < end) {
2268 		hits   += sym_hist->addr[offset].nr_samples;
2269 		period += sym_hist->addr[offset].period;
2270 		++offset;
2271 	}
2272 
2273 	if (sym_hist->nr_samples) {
2274 		data->he.period     = period;
2275 		data->he.nr_samples = hits;
2276 		data->percent[PERCENT_HITS_LOCAL] = 100.0 * hits / sym_hist->nr_samples;
2277 	}
2278 
2279 	if (hists->stats.nr_non_filtered_samples)
2280 		data->percent[PERCENT_HITS_GLOBAL] = 100.0 * hits / hists->stats.nr_non_filtered_samples;
2281 
2282 	if (sym_hist->period)
2283 		data->percent[PERCENT_PERIOD_LOCAL] = 100.0 * period / sym_hist->period;
2284 
2285 	if (hists->stats.total_period)
2286 		data->percent[PERCENT_PERIOD_GLOBAL] = 100.0 * period / hists->stats.total_period;
2287 }
2288 
annotation__calc_percent(struct annotation * notes,struct evsel * leader,s64 len)2289 static void annotation__calc_percent(struct annotation *notes,
2290 				     struct evsel *leader, s64 len)
2291 {
2292 	struct annotation_line *al, *next;
2293 	struct evsel *evsel;
2294 
2295 	list_for_each_entry(al, &notes->src->source, node) {
2296 		s64 end;
2297 		int i = 0;
2298 
2299 		if (al->offset == -1)
2300 			continue;
2301 
2302 		next = annotation_line__next(al, &notes->src->source);
2303 		end  = next ? next->offset : len;
2304 
2305 		for_each_group_evsel(evsel, leader) {
2306 			struct hists *hists = evsel__hists(evsel);
2307 			struct annotation_data *data;
2308 			struct sym_hist *sym_hist;
2309 
2310 			BUG_ON(i >= al->data_nr);
2311 
2312 			sym_hist = annotation__histogram(notes, evsel->core.idx);
2313 			data = &al->data[i++];
2314 
2315 			calc_percent(sym_hist, hists, data, al->offset, end);
2316 		}
2317 	}
2318 }
2319 
symbol__calc_percent(struct symbol * sym,struct evsel * evsel)2320 void symbol__calc_percent(struct symbol *sym, struct evsel *evsel)
2321 {
2322 	struct annotation *notes = symbol__annotation(sym);
2323 
2324 	annotation__calc_percent(notes, evsel, symbol__size(sym));
2325 }
2326 
symbol__annotate(struct map_symbol * ms,struct evsel * evsel,struct arch ** parch)2327 int symbol__annotate(struct map_symbol *ms, struct evsel *evsel,
2328 		     struct arch **parch)
2329 {
2330 	struct symbol *sym = ms->sym;
2331 	struct annotation *notes = symbol__annotation(sym);
2332 	struct annotate_args args = {
2333 		.evsel		= evsel,
2334 		.options	= &annotate_opts,
2335 	};
2336 	struct perf_env *env = evsel__env(evsel);
2337 	const char *arch_name = perf_env__arch(env);
2338 	struct arch *arch;
2339 	int err;
2340 
2341 	if (!arch_name)
2342 		return errno;
2343 
2344 	args.arch = arch = arch__find(arch_name);
2345 	if (arch == NULL) {
2346 		pr_err("%s: unsupported arch %s\n", __func__, arch_name);
2347 		return ENOTSUP;
2348 	}
2349 
2350 	if (parch)
2351 		*parch = arch;
2352 
2353 	if (arch->init) {
2354 		err = arch->init(arch, env ? env->cpuid : NULL);
2355 		if (err) {
2356 			pr_err("%s: failed to initialize %s arch priv area\n", __func__, arch->name);
2357 			return err;
2358 		}
2359 	}
2360 
2361 	args.ms = *ms;
2362 	if (annotate_opts.full_addr)
2363 		notes->start = map__objdump_2mem(ms->map, ms->sym->start);
2364 	else
2365 		notes->start = map__rip_2objdump(ms->map, ms->sym->start);
2366 
2367 	return symbol__disassemble(sym, &args);
2368 }
2369 
insert_source_line(struct rb_root * root,struct annotation_line * al)2370 static void insert_source_line(struct rb_root *root, struct annotation_line *al)
2371 {
2372 	struct annotation_line *iter;
2373 	struct rb_node **p = &root->rb_node;
2374 	struct rb_node *parent = NULL;
2375 	unsigned int percent_type = annotate_opts.percent_type;
2376 	int i, ret;
2377 
2378 	while (*p != NULL) {
2379 		parent = *p;
2380 		iter = rb_entry(parent, struct annotation_line, rb_node);
2381 
2382 		ret = strcmp(iter->path, al->path);
2383 		if (ret == 0) {
2384 			for (i = 0; i < al->data_nr; i++) {
2385 				iter->data[i].percent_sum += annotation_data__percent(&al->data[i],
2386 										      percent_type);
2387 			}
2388 			return;
2389 		}
2390 
2391 		if (ret < 0)
2392 			p = &(*p)->rb_left;
2393 		else
2394 			p = &(*p)->rb_right;
2395 	}
2396 
2397 	for (i = 0; i < al->data_nr; i++) {
2398 		al->data[i].percent_sum = annotation_data__percent(&al->data[i],
2399 								   percent_type);
2400 	}
2401 
2402 	rb_link_node(&al->rb_node, parent, p);
2403 	rb_insert_color(&al->rb_node, root);
2404 }
2405 
cmp_source_line(struct annotation_line * a,struct annotation_line * b)2406 static int cmp_source_line(struct annotation_line *a, struct annotation_line *b)
2407 {
2408 	int i;
2409 
2410 	for (i = 0; i < a->data_nr; i++) {
2411 		if (a->data[i].percent_sum == b->data[i].percent_sum)
2412 			continue;
2413 		return a->data[i].percent_sum > b->data[i].percent_sum;
2414 	}
2415 
2416 	return 0;
2417 }
2418 
__resort_source_line(struct rb_root * root,struct annotation_line * al)2419 static void __resort_source_line(struct rb_root *root, struct annotation_line *al)
2420 {
2421 	struct annotation_line *iter;
2422 	struct rb_node **p = &root->rb_node;
2423 	struct rb_node *parent = NULL;
2424 
2425 	while (*p != NULL) {
2426 		parent = *p;
2427 		iter = rb_entry(parent, struct annotation_line, rb_node);
2428 
2429 		if (cmp_source_line(al, iter))
2430 			p = &(*p)->rb_left;
2431 		else
2432 			p = &(*p)->rb_right;
2433 	}
2434 
2435 	rb_link_node(&al->rb_node, parent, p);
2436 	rb_insert_color(&al->rb_node, root);
2437 }
2438 
resort_source_line(struct rb_root * dest_root,struct rb_root * src_root)2439 static void resort_source_line(struct rb_root *dest_root, struct rb_root *src_root)
2440 {
2441 	struct annotation_line *al;
2442 	struct rb_node *node;
2443 
2444 	node = rb_first(src_root);
2445 	while (node) {
2446 		struct rb_node *next;
2447 
2448 		al = rb_entry(node, struct annotation_line, rb_node);
2449 		next = rb_next(node);
2450 		rb_erase(node, src_root);
2451 
2452 		__resort_source_line(dest_root, al);
2453 		node = next;
2454 	}
2455 }
2456 
print_summary(struct rb_root * root,const char * filename)2457 static void print_summary(struct rb_root *root, const char *filename)
2458 {
2459 	struct annotation_line *al;
2460 	struct rb_node *node;
2461 
2462 	printf("\nSorted summary for file %s\n", filename);
2463 	printf("----------------------------------------------\n\n");
2464 
2465 	if (RB_EMPTY_ROOT(root)) {
2466 		printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
2467 		return;
2468 	}
2469 
2470 	node = rb_first(root);
2471 	while (node) {
2472 		double percent, percent_max = 0.0;
2473 		const char *color;
2474 		char *path;
2475 		int i;
2476 
2477 		al = rb_entry(node, struct annotation_line, rb_node);
2478 		for (i = 0; i < al->data_nr; i++) {
2479 			percent = al->data[i].percent_sum;
2480 			color = get_percent_color(percent);
2481 			color_fprintf(stdout, color, " %7.2f", percent);
2482 
2483 			if (percent > percent_max)
2484 				percent_max = percent;
2485 		}
2486 
2487 		path = al->path;
2488 		color = get_percent_color(percent_max);
2489 		color_fprintf(stdout, color, " %s\n", path);
2490 
2491 		node = rb_next(node);
2492 	}
2493 }
2494 
symbol__annotate_hits(struct symbol * sym,struct evsel * evsel)2495 static void symbol__annotate_hits(struct symbol *sym, struct evsel *evsel)
2496 {
2497 	struct annotation *notes = symbol__annotation(sym);
2498 	struct sym_hist *h = annotation__histogram(notes, evsel->core.idx);
2499 	u64 len = symbol__size(sym), offset;
2500 
2501 	for (offset = 0; offset < len; ++offset)
2502 		if (h->addr[offset].nr_samples != 0)
2503 			printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
2504 			       sym->start + offset, h->addr[offset].nr_samples);
2505 	printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->nr_samples", h->nr_samples);
2506 }
2507 
annotated_source__addr_fmt_width(struct list_head * lines,u64 start)2508 static int annotated_source__addr_fmt_width(struct list_head *lines, u64 start)
2509 {
2510 	char bf[32];
2511 	struct annotation_line *line;
2512 
2513 	list_for_each_entry_reverse(line, lines, node) {
2514 		if (line->offset != -1)
2515 			return scnprintf(bf, sizeof(bf), "%" PRIx64, start + line->offset);
2516 	}
2517 
2518 	return 0;
2519 }
2520 
symbol__annotate_printf(struct map_symbol * ms,struct evsel * evsel)2521 int symbol__annotate_printf(struct map_symbol *ms, struct evsel *evsel)
2522 {
2523 	struct map *map = ms->map;
2524 	struct symbol *sym = ms->sym;
2525 	struct dso *dso = map__dso(map);
2526 	char *filename;
2527 	const char *d_filename;
2528 	const char *evsel_name = evsel__name(evsel);
2529 	struct annotation *notes = symbol__annotation(sym);
2530 	struct sym_hist *h = annotation__histogram(notes, evsel->core.idx);
2531 	struct annotation_line *pos, *queue = NULL;
2532 	struct annotation_options *opts = &annotate_opts;
2533 	u64 start = map__rip_2objdump(map, sym->start);
2534 	int printed = 2, queue_len = 0, addr_fmt_width;
2535 	int more = 0;
2536 	bool context = opts->context;
2537 	u64 len;
2538 	int width = symbol_conf.show_total_period ? 12 : 8;
2539 	int graph_dotted_len;
2540 	char buf[512];
2541 
2542 	filename = strdup(dso->long_name);
2543 	if (!filename)
2544 		return -ENOMEM;
2545 
2546 	if (opts->full_path)
2547 		d_filename = filename;
2548 	else
2549 		d_filename = basename(filename);
2550 
2551 	len = symbol__size(sym);
2552 
2553 	if (evsel__is_group_event(evsel)) {
2554 		width *= evsel->core.nr_members;
2555 		evsel__group_desc(evsel, buf, sizeof(buf));
2556 		evsel_name = buf;
2557 	}
2558 
2559 	graph_dotted_len = printf(" %-*.*s|	Source code & Disassembly of %s for %s (%" PRIu64 " samples, "
2560 				  "percent: %s)\n",
2561 				  width, width, symbol_conf.show_total_period ? "Period" :
2562 				  symbol_conf.show_nr_samples ? "Samples" : "Percent",
2563 				  d_filename, evsel_name, h->nr_samples,
2564 				  percent_type_str(opts->percent_type));
2565 
2566 	printf("%-*.*s----\n",
2567 	       graph_dotted_len, graph_dotted_len, graph_dotted_line);
2568 
2569 	if (verbose > 0)
2570 		symbol__annotate_hits(sym, evsel);
2571 
2572 	addr_fmt_width = annotated_source__addr_fmt_width(&notes->src->source, start);
2573 
2574 	list_for_each_entry(pos, &notes->src->source, node) {
2575 		int err;
2576 
2577 		if (context && queue == NULL) {
2578 			queue = pos;
2579 			queue_len = 0;
2580 		}
2581 
2582 		err = annotation_line__print(pos, sym, start, evsel, len,
2583 					     opts->min_pcnt, printed, opts->max_lines,
2584 					     queue, addr_fmt_width, opts->percent_type);
2585 
2586 		switch (err) {
2587 		case 0:
2588 			++printed;
2589 			if (context) {
2590 				printed += queue_len;
2591 				queue = NULL;
2592 				queue_len = 0;
2593 			}
2594 			break;
2595 		case 1:
2596 			/* filtered by max_lines */
2597 			++more;
2598 			break;
2599 		case -1:
2600 		default:
2601 			/*
2602 			 * Filtered by min_pcnt or non IP lines when
2603 			 * context != 0
2604 			 */
2605 			if (!context)
2606 				break;
2607 			if (queue_len == context)
2608 				queue = list_entry(queue->node.next, typeof(*queue), node);
2609 			else
2610 				++queue_len;
2611 			break;
2612 		}
2613 	}
2614 
2615 	free(filename);
2616 
2617 	return more;
2618 }
2619 
FILE__set_percent_color(void * fp __maybe_unused,double percent __maybe_unused,bool current __maybe_unused)2620 static void FILE__set_percent_color(void *fp __maybe_unused,
2621 				    double percent __maybe_unused,
2622 				    bool current __maybe_unused)
2623 {
2624 }
2625 
FILE__set_jumps_percent_color(void * fp __maybe_unused,int nr __maybe_unused,bool current __maybe_unused)2626 static int FILE__set_jumps_percent_color(void *fp __maybe_unused,
2627 					 int nr __maybe_unused, bool current __maybe_unused)
2628 {
2629 	return 0;
2630 }
2631 
FILE__set_color(void * fp __maybe_unused,int color __maybe_unused)2632 static int FILE__set_color(void *fp __maybe_unused, int color __maybe_unused)
2633 {
2634 	return 0;
2635 }
2636 
FILE__printf(void * fp,const char * fmt,...)2637 static void FILE__printf(void *fp, const char *fmt, ...)
2638 {
2639 	va_list args;
2640 
2641 	va_start(args, fmt);
2642 	vfprintf(fp, fmt, args);
2643 	va_end(args);
2644 }
2645 
FILE__write_graph(void * fp,int graph)2646 static void FILE__write_graph(void *fp, int graph)
2647 {
2648 	const char *s;
2649 	switch (graph) {
2650 
2651 	case DARROW_CHAR: s = "↓"; break;
2652 	case UARROW_CHAR: s = "↑"; break;
2653 	case LARROW_CHAR: s = "←"; break;
2654 	case RARROW_CHAR: s = "→"; break;
2655 	default:		s = "?"; break;
2656 	}
2657 
2658 	fputs(s, fp);
2659 }
2660 
symbol__annotate_fprintf2(struct symbol * sym,FILE * fp)2661 static int symbol__annotate_fprintf2(struct symbol *sym, FILE *fp)
2662 {
2663 	struct annotation *notes = symbol__annotation(sym);
2664 	struct annotation_write_ops wops = {
2665 		.first_line		 = true,
2666 		.obj			 = fp,
2667 		.set_color		 = FILE__set_color,
2668 		.set_percent_color	 = FILE__set_percent_color,
2669 		.set_jumps_percent_color = FILE__set_jumps_percent_color,
2670 		.printf			 = FILE__printf,
2671 		.write_graph		 = FILE__write_graph,
2672 	};
2673 	struct annotation_line *al;
2674 
2675 	list_for_each_entry(al, &notes->src->source, node) {
2676 		if (annotation_line__filter(al, notes))
2677 			continue;
2678 		annotation_line__write(al, notes, &wops);
2679 		fputc('\n', fp);
2680 		wops.first_line = false;
2681 	}
2682 
2683 	return 0;
2684 }
2685 
map_symbol__annotation_dump(struct map_symbol * ms,struct evsel * evsel)2686 int map_symbol__annotation_dump(struct map_symbol *ms, struct evsel *evsel)
2687 {
2688 	const char *ev_name = evsel__name(evsel);
2689 	char buf[1024];
2690 	char *filename;
2691 	int err = -1;
2692 	FILE *fp;
2693 
2694 	if (asprintf(&filename, "%s.annotation", ms->sym->name) < 0)
2695 		return -1;
2696 
2697 	fp = fopen(filename, "w");
2698 	if (fp == NULL)
2699 		goto out_free_filename;
2700 
2701 	if (evsel__is_group_event(evsel)) {
2702 		evsel__group_desc(evsel, buf, sizeof(buf));
2703 		ev_name = buf;
2704 	}
2705 
2706 	fprintf(fp, "%s() %s\nEvent: %s\n\n",
2707 		ms->sym->name, map__dso(ms->map)->long_name, ev_name);
2708 	symbol__annotate_fprintf2(ms->sym, fp);
2709 
2710 	fclose(fp);
2711 	err = 0;
2712 out_free_filename:
2713 	free(filename);
2714 	return err;
2715 }
2716 
symbol__annotate_zero_histogram(struct symbol * sym,int evidx)2717 void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
2718 {
2719 	struct annotation *notes = symbol__annotation(sym);
2720 	struct sym_hist *h = annotation__histogram(notes, evidx);
2721 
2722 	memset(h, 0, notes->src->sizeof_sym_hist);
2723 }
2724 
symbol__annotate_decay_histogram(struct symbol * sym,int evidx)2725 void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
2726 {
2727 	struct annotation *notes = symbol__annotation(sym);
2728 	struct sym_hist *h = annotation__histogram(notes, evidx);
2729 	int len = symbol__size(sym), offset;
2730 
2731 	h->nr_samples = 0;
2732 	for (offset = 0; offset < len; ++offset) {
2733 		h->addr[offset].nr_samples = h->addr[offset].nr_samples * 7 / 8;
2734 		h->nr_samples += h->addr[offset].nr_samples;
2735 	}
2736 }
2737 
annotated_source__purge(struct annotated_source * as)2738 void annotated_source__purge(struct annotated_source *as)
2739 {
2740 	struct annotation_line *al, *n;
2741 
2742 	list_for_each_entry_safe(al, n, &as->source, node) {
2743 		list_del_init(&al->node);
2744 		disasm_line__free(disasm_line(al));
2745 	}
2746 }
2747 
disasm_line__fprintf(struct disasm_line * dl,FILE * fp)2748 static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp)
2749 {
2750 	size_t printed;
2751 
2752 	if (dl->al.offset == -1)
2753 		return fprintf(fp, "%s\n", dl->al.line);
2754 
2755 	printed = fprintf(fp, "%#" PRIx64 " %s", dl->al.offset, dl->ins.name);
2756 
2757 	if (dl->ops.raw[0] != '\0') {
2758 		printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
2759 				   dl->ops.raw);
2760 	}
2761 
2762 	return printed + fprintf(fp, "\n");
2763 }
2764 
disasm__fprintf(struct list_head * head,FILE * fp)2765 size_t disasm__fprintf(struct list_head *head, FILE *fp)
2766 {
2767 	struct disasm_line *pos;
2768 	size_t printed = 0;
2769 
2770 	list_for_each_entry(pos, head, al.node)
2771 		printed += disasm_line__fprintf(pos, fp);
2772 
2773 	return printed;
2774 }
2775 
disasm_line__is_valid_local_jump(struct disasm_line * dl,struct symbol * sym)2776 bool disasm_line__is_valid_local_jump(struct disasm_line *dl, struct symbol *sym)
2777 {
2778 	if (!dl || !dl->ins.ops || !ins__is_jump(&dl->ins) ||
2779 	    !disasm_line__has_local_offset(dl) || dl->ops.target.offset < 0 ||
2780 	    dl->ops.target.offset >= (s64)symbol__size(sym))
2781 		return false;
2782 
2783 	return true;
2784 }
2785 
annotation__mark_jump_targets(struct annotation * notes,struct symbol * sym)2786 void annotation__mark_jump_targets(struct annotation *notes, struct symbol *sym)
2787 {
2788 	u64 offset, size = symbol__size(sym);
2789 
2790 	/* PLT symbols contain external offsets */
2791 	if (strstr(sym->name, "@plt"))
2792 		return;
2793 
2794 	for (offset = 0; offset < size; ++offset) {
2795 		struct annotation_line *al = notes->offsets[offset];
2796 		struct disasm_line *dl;
2797 
2798 		dl = disasm_line(al);
2799 
2800 		if (!disasm_line__is_valid_local_jump(dl, sym))
2801 			continue;
2802 
2803 		al = notes->offsets[dl->ops.target.offset];
2804 
2805 		/*
2806 		 * FIXME: Oops, no jump target? Buggy disassembler? Or do we
2807 		 * have to adjust to the previous offset?
2808 		 */
2809 		if (al == NULL)
2810 			continue;
2811 
2812 		if (++al->jump_sources > notes->max_jump_sources)
2813 			notes->max_jump_sources = al->jump_sources;
2814 	}
2815 }
2816 
annotation__set_offsets(struct annotation * notes,s64 size)2817 void annotation__set_offsets(struct annotation *notes, s64 size)
2818 {
2819 	struct annotation_line *al;
2820 
2821 	notes->max_line_len = 0;
2822 	notes->nr_entries = 0;
2823 	notes->nr_asm_entries = 0;
2824 
2825 	list_for_each_entry(al, &notes->src->source, node) {
2826 		size_t line_len = strlen(al->line);
2827 
2828 		if (notes->max_line_len < line_len)
2829 			notes->max_line_len = line_len;
2830 		al->idx = notes->nr_entries++;
2831 		if (al->offset != -1) {
2832 			al->idx_asm = notes->nr_asm_entries++;
2833 			/*
2834 			 * FIXME: short term bandaid to cope with assembly
2835 			 * routines that comes with labels in the same column
2836 			 * as the address in objdump, sigh.
2837 			 *
2838 			 * E.g. copy_user_generic_unrolled
2839  			 */
2840 			if (al->offset < size)
2841 				notes->offsets[al->offset] = al;
2842 		} else
2843 			al->idx_asm = -1;
2844 	}
2845 }
2846 
width_jumps(int n)2847 static inline int width_jumps(int n)
2848 {
2849 	if (n >= 100)
2850 		return 5;
2851 	if (n / 10)
2852 		return 2;
2853 	return 1;
2854 }
2855 
annotation__max_ins_name(struct annotation * notes)2856 static int annotation__max_ins_name(struct annotation *notes)
2857 {
2858 	int max_name = 0, len;
2859 	struct annotation_line *al;
2860 
2861         list_for_each_entry(al, &notes->src->source, node) {
2862 		if (al->offset == -1)
2863 			continue;
2864 
2865 		len = strlen(disasm_line(al)->ins.name);
2866 		if (max_name < len)
2867 			max_name = len;
2868 	}
2869 
2870 	return max_name;
2871 }
2872 
annotation__init_column_widths(struct annotation * notes,struct symbol * sym)2873 void annotation__init_column_widths(struct annotation *notes, struct symbol *sym)
2874 {
2875 	notes->widths.addr = notes->widths.target =
2876 		notes->widths.min_addr = hex_width(symbol__size(sym));
2877 	notes->widths.max_addr = hex_width(sym->end);
2878 	notes->widths.jumps = width_jumps(notes->max_jump_sources);
2879 	notes->widths.max_ins_name = annotation__max_ins_name(notes);
2880 }
2881 
annotation__update_column_widths(struct annotation * notes)2882 void annotation__update_column_widths(struct annotation *notes)
2883 {
2884 	if (annotate_opts.use_offset)
2885 		notes->widths.target = notes->widths.min_addr;
2886 	else if (annotate_opts.full_addr)
2887 		notes->widths.target = BITS_PER_LONG / 4;
2888 	else
2889 		notes->widths.target = notes->widths.max_addr;
2890 
2891 	notes->widths.addr = notes->widths.target;
2892 
2893 	if (annotate_opts.show_nr_jumps)
2894 		notes->widths.addr += notes->widths.jumps + 1;
2895 }
2896 
annotation__toggle_full_addr(struct annotation * notes,struct map_symbol * ms)2897 void annotation__toggle_full_addr(struct annotation *notes, struct map_symbol *ms)
2898 {
2899 	annotate_opts.full_addr = !annotate_opts.full_addr;
2900 
2901 	if (annotate_opts.full_addr)
2902 		notes->start = map__objdump_2mem(ms->map, ms->sym->start);
2903 	else
2904 		notes->start = map__rip_2objdump(ms->map, ms->sym->start);
2905 
2906 	annotation__update_column_widths(notes);
2907 }
2908 
annotation__calc_lines(struct annotation * notes,struct map_symbol * ms,struct rb_root * root)2909 static void annotation__calc_lines(struct annotation *notes, struct map_symbol *ms,
2910 				   struct rb_root *root)
2911 {
2912 	struct annotation_line *al;
2913 	struct rb_root tmp_root = RB_ROOT;
2914 
2915 	list_for_each_entry(al, &notes->src->source, node) {
2916 		double percent_max = 0.0;
2917 		u64 addr;
2918 		int i;
2919 
2920 		for (i = 0; i < al->data_nr; i++) {
2921 			double percent;
2922 
2923 			percent = annotation_data__percent(&al->data[i],
2924 							   annotate_opts.percent_type);
2925 
2926 			if (percent > percent_max)
2927 				percent_max = percent;
2928 		}
2929 
2930 		if (percent_max <= 0.5)
2931 			continue;
2932 
2933 		addr = map__rip_2objdump(ms->map, ms->sym->start);
2934 		al->path = get_srcline(map__dso(ms->map), addr + al->offset, NULL,
2935 				       false, true, ms->sym->start + al->offset);
2936 		insert_source_line(&tmp_root, al);
2937 	}
2938 
2939 	resort_source_line(root, &tmp_root);
2940 }
2941 
symbol__calc_lines(struct map_symbol * ms,struct rb_root * root)2942 static void symbol__calc_lines(struct map_symbol *ms, struct rb_root *root)
2943 {
2944 	struct annotation *notes = symbol__annotation(ms->sym);
2945 
2946 	annotation__calc_lines(notes, ms, root);
2947 }
2948 
symbol__tty_annotate2(struct map_symbol * ms,struct evsel * evsel)2949 int symbol__tty_annotate2(struct map_symbol *ms, struct evsel *evsel)
2950 {
2951 	struct dso *dso = map__dso(ms->map);
2952 	struct symbol *sym = ms->sym;
2953 	struct rb_root source_line = RB_ROOT;
2954 	struct hists *hists = evsel__hists(evsel);
2955 	char buf[1024];
2956 	int err;
2957 
2958 	err = symbol__annotate2(ms, evsel, NULL);
2959 	if (err) {
2960 		char msg[BUFSIZ];
2961 
2962 		dso->annotate_warned = true;
2963 		symbol__strerror_disassemble(ms, err, msg, sizeof(msg));
2964 		ui__error("Couldn't annotate %s:\n%s", sym->name, msg);
2965 		return -1;
2966 	}
2967 
2968 	if (annotate_opts.print_lines) {
2969 		srcline_full_filename = annotate_opts.full_path;
2970 		symbol__calc_lines(ms, &source_line);
2971 		print_summary(&source_line, dso->long_name);
2972 	}
2973 
2974 	hists__scnprintf_title(hists, buf, sizeof(buf));
2975 	fprintf(stdout, "%s, [percent: %s]\n%s() %s\n",
2976 		buf, percent_type_str(annotate_opts.percent_type), sym->name,
2977 		dso->long_name);
2978 	symbol__annotate_fprintf2(sym, stdout);
2979 
2980 	annotated_source__purge(symbol__annotation(sym)->src);
2981 
2982 	return 0;
2983 }
2984 
symbol__tty_annotate(struct map_symbol * ms,struct evsel * evsel)2985 int symbol__tty_annotate(struct map_symbol *ms, struct evsel *evsel)
2986 {
2987 	struct dso *dso = map__dso(ms->map);
2988 	struct symbol *sym = ms->sym;
2989 	struct rb_root source_line = RB_ROOT;
2990 	int err;
2991 
2992 	err = symbol__annotate(ms, evsel, NULL);
2993 	if (err) {
2994 		char msg[BUFSIZ];
2995 
2996 		dso->annotate_warned = true;
2997 		symbol__strerror_disassemble(ms, err, msg, sizeof(msg));
2998 		ui__error("Couldn't annotate %s:\n%s", sym->name, msg);
2999 		return -1;
3000 	}
3001 
3002 	symbol__calc_percent(sym, evsel);
3003 
3004 	if (annotate_opts.print_lines) {
3005 		srcline_full_filename = annotate_opts.full_path;
3006 		symbol__calc_lines(ms, &source_line);
3007 		print_summary(&source_line, dso->long_name);
3008 	}
3009 
3010 	symbol__annotate_printf(ms, evsel);
3011 
3012 	annotated_source__purge(symbol__annotation(sym)->src);
3013 
3014 	return 0;
3015 }
3016 
ui__has_annotation(void)3017 bool ui__has_annotation(void)
3018 {
3019 	return use_browser == 1 && perf_hpp_list.sym;
3020 }
3021 
3022 
annotation_line__max_percent(struct annotation_line * al,struct annotation * notes,unsigned int percent_type)3023 static double annotation_line__max_percent(struct annotation_line *al,
3024 					   struct annotation *notes,
3025 					   unsigned int percent_type)
3026 {
3027 	double percent_max = 0.0;
3028 	int i;
3029 
3030 	for (i = 0; i < notes->nr_events; i++) {
3031 		double percent;
3032 
3033 		percent = annotation_data__percent(&al->data[i],
3034 						   percent_type);
3035 
3036 		if (percent > percent_max)
3037 			percent_max = percent;
3038 	}
3039 
3040 	return percent_max;
3041 }
3042 
disasm_line__write(struct disasm_line * dl,struct annotation * notes,void * obj,char * bf,size_t size,void (* obj__printf)(void * obj,const char * fmt,...),void (* obj__write_graph)(void * obj,int graph))3043 static void disasm_line__write(struct disasm_line *dl, struct annotation *notes,
3044 			       void *obj, char *bf, size_t size,
3045 			       void (*obj__printf)(void *obj, const char *fmt, ...),
3046 			       void (*obj__write_graph)(void *obj, int graph))
3047 {
3048 	if (dl->ins.ops && dl->ins.ops->scnprintf) {
3049 		if (ins__is_jump(&dl->ins)) {
3050 			bool fwd;
3051 
3052 			if (dl->ops.target.outside)
3053 				goto call_like;
3054 			fwd = dl->ops.target.offset > dl->al.offset;
3055 			obj__write_graph(obj, fwd ? DARROW_CHAR : UARROW_CHAR);
3056 			obj__printf(obj, " ");
3057 		} else if (ins__is_call(&dl->ins)) {
3058 call_like:
3059 			obj__write_graph(obj, RARROW_CHAR);
3060 			obj__printf(obj, " ");
3061 		} else if (ins__is_ret(&dl->ins)) {
3062 			obj__write_graph(obj, LARROW_CHAR);
3063 			obj__printf(obj, " ");
3064 		} else {
3065 			obj__printf(obj, "  ");
3066 		}
3067 	} else {
3068 		obj__printf(obj, "  ");
3069 	}
3070 
3071 	disasm_line__scnprintf(dl, bf, size, !annotate_opts.use_offset, notes->widths.max_ins_name);
3072 }
3073 
ipc_coverage_string(char * bf,int size,struct annotation * notes)3074 static void ipc_coverage_string(char *bf, int size, struct annotation *notes)
3075 {
3076 	double ipc = 0.0, coverage = 0.0;
3077 
3078 	if (notes->hit_cycles)
3079 		ipc = notes->hit_insn / ((double)notes->hit_cycles);
3080 
3081 	if (notes->total_insn) {
3082 		coverage = notes->cover_insn * 100.0 /
3083 			((double)notes->total_insn);
3084 	}
3085 
3086 	scnprintf(bf, size, "(Average IPC: %.2f, IPC Coverage: %.1f%%)",
3087 		  ipc, coverage);
3088 }
3089 
__annotation_line__write(struct annotation_line * al,struct annotation * notes,bool first_line,bool current_entry,bool change_color,int width,void * obj,unsigned int percent_type,int (* obj__set_color)(void * obj,int color),void (* obj__set_percent_color)(void * obj,double percent,bool current),int (* obj__set_jumps_percent_color)(void * obj,int nr,bool current),void (* obj__printf)(void * obj,const char * fmt,...),void (* obj__write_graph)(void * obj,int graph))3090 static void __annotation_line__write(struct annotation_line *al, struct annotation *notes,
3091 				     bool first_line, bool current_entry, bool change_color, int width,
3092 				     void *obj, unsigned int percent_type,
3093 				     int  (*obj__set_color)(void *obj, int color),
3094 				     void (*obj__set_percent_color)(void *obj, double percent, bool current),
3095 				     int  (*obj__set_jumps_percent_color)(void *obj, int nr, bool current),
3096 				     void (*obj__printf)(void *obj, const char *fmt, ...),
3097 				     void (*obj__write_graph)(void *obj, int graph))
3098 
3099 {
3100 	double percent_max = annotation_line__max_percent(al, notes, percent_type);
3101 	int pcnt_width = annotation__pcnt_width(notes),
3102 	    cycles_width = annotation__cycles_width(notes);
3103 	bool show_title = false;
3104 	char bf[256];
3105 	int printed;
3106 
3107 	if (first_line && (al->offset == -1 || percent_max == 0.0)) {
3108 		if (notes->have_cycles && al->cycles) {
3109 			if (al->cycles->ipc == 0.0 && al->cycles->avg == 0)
3110 				show_title = true;
3111 		} else
3112 			show_title = true;
3113 	}
3114 
3115 	if (al->offset != -1 && percent_max != 0.0) {
3116 		int i;
3117 
3118 		for (i = 0; i < notes->nr_events; i++) {
3119 			double percent;
3120 
3121 			percent = annotation_data__percent(&al->data[i], percent_type);
3122 
3123 			obj__set_percent_color(obj, percent, current_entry);
3124 			if (symbol_conf.show_total_period) {
3125 				obj__printf(obj, "%11" PRIu64 " ", al->data[i].he.period);
3126 			} else if (symbol_conf.show_nr_samples) {
3127 				obj__printf(obj, "%6" PRIu64 " ",
3128 						   al->data[i].he.nr_samples);
3129 			} else {
3130 				obj__printf(obj, "%6.2f ", percent);
3131 			}
3132 		}
3133 	} else {
3134 		obj__set_percent_color(obj, 0, current_entry);
3135 
3136 		if (!show_title)
3137 			obj__printf(obj, "%-*s", pcnt_width, " ");
3138 		else {
3139 			obj__printf(obj, "%-*s", pcnt_width,
3140 					   symbol_conf.show_total_period ? "Period" :
3141 					   symbol_conf.show_nr_samples ? "Samples" : "Percent");
3142 		}
3143 	}
3144 
3145 	if (notes->have_cycles) {
3146 		if (al->cycles && al->cycles->ipc)
3147 			obj__printf(obj, "%*.2f ", ANNOTATION__IPC_WIDTH - 1, al->cycles->ipc);
3148 		else if (!show_title)
3149 			obj__printf(obj, "%*s", ANNOTATION__IPC_WIDTH, " ");
3150 		else
3151 			obj__printf(obj, "%*s ", ANNOTATION__IPC_WIDTH - 1, "IPC");
3152 
3153 		if (!annotate_opts.show_minmax_cycle) {
3154 			if (al->cycles && al->cycles->avg)
3155 				obj__printf(obj, "%*" PRIu64 " ",
3156 					   ANNOTATION__CYCLES_WIDTH - 1, al->cycles->avg);
3157 			else if (!show_title)
3158 				obj__printf(obj, "%*s",
3159 					    ANNOTATION__CYCLES_WIDTH, " ");
3160 			else
3161 				obj__printf(obj, "%*s ",
3162 					    ANNOTATION__CYCLES_WIDTH - 1,
3163 					    "Cycle");
3164 		} else {
3165 			if (al->cycles) {
3166 				char str[32];
3167 
3168 				scnprintf(str, sizeof(str),
3169 					"%" PRIu64 "(%" PRIu64 "/%" PRIu64 ")",
3170 					al->cycles->avg, al->cycles->min,
3171 					al->cycles->max);
3172 
3173 				obj__printf(obj, "%*s ",
3174 					    ANNOTATION__MINMAX_CYCLES_WIDTH - 1,
3175 					    str);
3176 			} else if (!show_title)
3177 				obj__printf(obj, "%*s",
3178 					    ANNOTATION__MINMAX_CYCLES_WIDTH,
3179 					    " ");
3180 			else
3181 				obj__printf(obj, "%*s ",
3182 					    ANNOTATION__MINMAX_CYCLES_WIDTH - 1,
3183 					    "Cycle(min/max)");
3184 		}
3185 
3186 		if (show_title && !*al->line) {
3187 			ipc_coverage_string(bf, sizeof(bf), notes);
3188 			obj__printf(obj, "%*s", ANNOTATION__AVG_IPC_WIDTH, bf);
3189 		}
3190 	}
3191 
3192 	obj__printf(obj, " ");
3193 
3194 	if (!*al->line)
3195 		obj__printf(obj, "%-*s", width - pcnt_width - cycles_width, " ");
3196 	else if (al->offset == -1) {
3197 		if (al->line_nr && annotate_opts.show_linenr)
3198 			printed = scnprintf(bf, sizeof(bf), "%-*d ", notes->widths.addr + 1, al->line_nr);
3199 		else
3200 			printed = scnprintf(bf, sizeof(bf), "%-*s  ", notes->widths.addr, " ");
3201 		obj__printf(obj, bf);
3202 		obj__printf(obj, "%-*s", width - printed - pcnt_width - cycles_width + 1, al->line);
3203 	} else {
3204 		u64 addr = al->offset;
3205 		int color = -1;
3206 
3207 		if (!annotate_opts.use_offset)
3208 			addr += notes->start;
3209 
3210 		if (!annotate_opts.use_offset) {
3211 			printed = scnprintf(bf, sizeof(bf), "%" PRIx64 ": ", addr);
3212 		} else {
3213 			if (al->jump_sources &&
3214 			    annotate_opts.offset_level >= ANNOTATION__OFFSET_JUMP_TARGETS) {
3215 				if (annotate_opts.show_nr_jumps) {
3216 					int prev;
3217 					printed = scnprintf(bf, sizeof(bf), "%*d ",
3218 							    notes->widths.jumps,
3219 							    al->jump_sources);
3220 					prev = obj__set_jumps_percent_color(obj, al->jump_sources,
3221 									    current_entry);
3222 					obj__printf(obj, bf);
3223 					obj__set_color(obj, prev);
3224 				}
3225 print_addr:
3226 				printed = scnprintf(bf, sizeof(bf), "%*" PRIx64 ": ",
3227 						    notes->widths.target, addr);
3228 			} else if (ins__is_call(&disasm_line(al)->ins) &&
3229 				   annotate_opts.offset_level >= ANNOTATION__OFFSET_CALL) {
3230 				goto print_addr;
3231 			} else if (annotate_opts.offset_level == ANNOTATION__MAX_OFFSET_LEVEL) {
3232 				goto print_addr;
3233 			} else {
3234 				printed = scnprintf(bf, sizeof(bf), "%-*s  ",
3235 						    notes->widths.addr, " ");
3236 			}
3237 		}
3238 
3239 		if (change_color)
3240 			color = obj__set_color(obj, HE_COLORSET_ADDR);
3241 		obj__printf(obj, bf);
3242 		if (change_color)
3243 			obj__set_color(obj, color);
3244 
3245 		disasm_line__write(disasm_line(al), notes, obj, bf, sizeof(bf), obj__printf, obj__write_graph);
3246 
3247 		obj__printf(obj, "%-*s", width - pcnt_width - cycles_width - 3 - printed, bf);
3248 	}
3249 
3250 }
3251 
annotation_line__write(struct annotation_line * al,struct annotation * notes,struct annotation_write_ops * wops)3252 void annotation_line__write(struct annotation_line *al, struct annotation *notes,
3253 			    struct annotation_write_ops *wops)
3254 {
3255 	__annotation_line__write(al, notes, wops->first_line, wops->current_entry,
3256 				 wops->change_color, wops->width, wops->obj,
3257 				 annotate_opts.percent_type,
3258 				 wops->set_color, wops->set_percent_color,
3259 				 wops->set_jumps_percent_color, wops->printf,
3260 				 wops->write_graph);
3261 }
3262 
symbol__annotate2(struct map_symbol * ms,struct evsel * evsel,struct arch ** parch)3263 int symbol__annotate2(struct map_symbol *ms, struct evsel *evsel,
3264 		      struct arch **parch)
3265 {
3266 	struct symbol *sym = ms->sym;
3267 	struct annotation *notes = symbol__annotation(sym);
3268 	size_t size = symbol__size(sym);
3269 	int nr_pcnt = 1, err;
3270 
3271 	notes->offsets = zalloc(size * sizeof(struct annotation_line *));
3272 	if (notes->offsets == NULL)
3273 		return ENOMEM;
3274 
3275 	if (evsel__is_group_event(evsel))
3276 		nr_pcnt = evsel->core.nr_members;
3277 
3278 	err = symbol__annotate(ms, evsel, parch);
3279 	if (err)
3280 		goto out_free_offsets;
3281 
3282 	notes->options = &annotate_opts;
3283 
3284 	symbol__calc_percent(sym, evsel);
3285 
3286 	annotation__set_offsets(notes, size);
3287 	annotation__mark_jump_targets(notes, sym);
3288 
3289 	err = annotation__compute_ipc(notes, size);
3290 	if (err)
3291 		goto out_free_offsets;
3292 
3293 	annotation__init_column_widths(notes, sym);
3294 	notes->nr_events = nr_pcnt;
3295 
3296 	annotation__update_column_widths(notes);
3297 	sym->annotate2 = 1;
3298 
3299 	return 0;
3300 
3301 out_free_offsets:
3302 	zfree(&notes->offsets);
3303 	return err;
3304 }
3305 
annotation__config(const char * var,const char * value,void * data)3306 static int annotation__config(const char *var, const char *value, void *data)
3307 {
3308 	struct annotation_options *opt = data;
3309 
3310 	if (!strstarts(var, "annotate."))
3311 		return 0;
3312 
3313 	if (!strcmp(var, "annotate.offset_level")) {
3314 		perf_config_u8(&opt->offset_level, "offset_level", value);
3315 
3316 		if (opt->offset_level > ANNOTATION__MAX_OFFSET_LEVEL)
3317 			opt->offset_level = ANNOTATION__MAX_OFFSET_LEVEL;
3318 		else if (opt->offset_level < ANNOTATION__MIN_OFFSET_LEVEL)
3319 			opt->offset_level = ANNOTATION__MIN_OFFSET_LEVEL;
3320 	} else if (!strcmp(var, "annotate.hide_src_code")) {
3321 		opt->hide_src_code = perf_config_bool("hide_src_code", value);
3322 	} else if (!strcmp(var, "annotate.jump_arrows")) {
3323 		opt->jump_arrows = perf_config_bool("jump_arrows", value);
3324 	} else if (!strcmp(var, "annotate.show_linenr")) {
3325 		opt->show_linenr = perf_config_bool("show_linenr", value);
3326 	} else if (!strcmp(var, "annotate.show_nr_jumps")) {
3327 		opt->show_nr_jumps = perf_config_bool("show_nr_jumps", value);
3328 	} else if (!strcmp(var, "annotate.show_nr_samples")) {
3329 		symbol_conf.show_nr_samples = perf_config_bool("show_nr_samples",
3330 								value);
3331 	} else if (!strcmp(var, "annotate.show_total_period")) {
3332 		symbol_conf.show_total_period = perf_config_bool("show_total_period",
3333 								value);
3334 	} else if (!strcmp(var, "annotate.use_offset")) {
3335 		opt->use_offset = perf_config_bool("use_offset", value);
3336 	} else if (!strcmp(var, "annotate.disassembler_style")) {
3337 		opt->disassembler_style = strdup(value);
3338 		if (!opt->disassembler_style) {
3339 			pr_err("Not enough memory for annotate.disassembler_style\n");
3340 			return -1;
3341 		}
3342 	} else if (!strcmp(var, "annotate.objdump")) {
3343 		opt->objdump_path = strdup(value);
3344 		if (!opt->objdump_path) {
3345 			pr_err("Not enough memory for annotate.objdump\n");
3346 			return -1;
3347 		}
3348 	} else if (!strcmp(var, "annotate.addr2line")) {
3349 		symbol_conf.addr2line_path = strdup(value);
3350 		if (!symbol_conf.addr2line_path) {
3351 			pr_err("Not enough memory for annotate.addr2line\n");
3352 			return -1;
3353 		}
3354 	} else if (!strcmp(var, "annotate.demangle")) {
3355 		symbol_conf.demangle = perf_config_bool("demangle", value);
3356 	} else if (!strcmp(var, "annotate.demangle_kernel")) {
3357 		symbol_conf.demangle_kernel = perf_config_bool("demangle_kernel", value);
3358 	} else {
3359 		pr_debug("%s variable unknown, ignoring...", var);
3360 	}
3361 
3362 	return 0;
3363 }
3364 
annotation_options__init(struct annotation_options * opt)3365 void annotation_options__init(struct annotation_options *opt)
3366 {
3367 	memset(opt, 0, sizeof(*opt));
3368 
3369 	/* Default values. */
3370 	opt->use_offset = true;
3371 	opt->jump_arrows = true;
3372 	opt->annotate_src = true;
3373 	opt->offset_level = ANNOTATION__OFFSET_JUMP_TARGETS;
3374 	opt->percent_type = PERCENT_PERIOD_LOCAL;
3375 }
3376 
3377 
annotation_options__exit(struct annotation_options * opt)3378 void annotation_options__exit(struct annotation_options *opt)
3379 {
3380 	zfree(&opt->disassembler_style);
3381 	zfree(&opt->objdump_path);
3382 }
3383 
annotation_config__init(struct annotation_options * opt)3384 void annotation_config__init(struct annotation_options *opt)
3385 {
3386 	perf_config(annotation__config, opt);
3387 }
3388 
parse_percent_type(char * str1,char * str2)3389 static unsigned int parse_percent_type(char *str1, char *str2)
3390 {
3391 	unsigned int type = (unsigned int) -1;
3392 
3393 	if (!strcmp("period", str1)) {
3394 		if (!strcmp("local", str2))
3395 			type = PERCENT_PERIOD_LOCAL;
3396 		else if (!strcmp("global", str2))
3397 			type = PERCENT_PERIOD_GLOBAL;
3398 	}
3399 
3400 	if (!strcmp("hits", str1)) {
3401 		if (!strcmp("local", str2))
3402 			type = PERCENT_HITS_LOCAL;
3403 		else if (!strcmp("global", str2))
3404 			type = PERCENT_HITS_GLOBAL;
3405 	}
3406 
3407 	return type;
3408 }
3409 
annotate_parse_percent_type(const struct option * opt __maybe_unused,const char * _str,int unset __maybe_unused)3410 int annotate_parse_percent_type(const struct option *opt __maybe_unused, const char *_str,
3411 				int unset __maybe_unused)
3412 {
3413 	unsigned int type;
3414 	char *str1, *str2;
3415 	int err = -1;
3416 
3417 	str1 = strdup(_str);
3418 	if (!str1)
3419 		return -ENOMEM;
3420 
3421 	str2 = strchr(str1, '-');
3422 	if (!str2)
3423 		goto out;
3424 
3425 	*str2++ = 0;
3426 
3427 	type = parse_percent_type(str1, str2);
3428 	if (type == (unsigned int) -1)
3429 		type = parse_percent_type(str2, str1);
3430 	if (type != (unsigned int) -1) {
3431 		annotate_opts.percent_type = type;
3432 		err = 0;
3433 	}
3434 
3435 out:
3436 	free(str1);
3437 	return err;
3438 }
3439 
annotate_check_args(struct annotation_options * args)3440 int annotate_check_args(struct annotation_options *args)
3441 {
3442 	if (args->prefix_strip && !args->prefix) {
3443 		pr_err("--prefix-strip requires --prefix\n");
3444 		return -1;
3445 	}
3446 	return 0;
3447 }
3448