Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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