Commit 0e26ba5a authored by Ian Rogers's avatar Ian Rogers Committed by Arnaldo Carvalho de Melo
Browse files

perf disasm: Refactor arch__find and initialization of arch structs



Switch arch__find to using an ELF machine number rather than a
string.

Rather than an array of fixed size arch structs turn the init functions
into new functions indexed by the ELF machine they correspond to.

This allows data to be stored with a struct arch with the container_of
trick, so the priv variable can be removed.

Switch to using the thread to find the arch rather than the evsel as the
evsel only has limited notions of the running thread upon which
disassembly is performed.

Factor out the e_machine and e_flags into their own struct to make them
easier to pass around.

Reviewed-by: default avatarJames Clark <james.clark@linaro.org>
Signed-off-by: default avatarIan Rogers <irogers@google.com>
Cc: Aditya Bodkhe <aditya.b1@linux.ibm.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Albert Ou <aou@eecs.berkeley.edu>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Alexandre Ghiti <alex@ghiti.fr>
Cc: Athira Rajeev <atrajeev@linux.ibm.com>
Cc: Bill Wendling <morbo@google.com>
Cc: Dr. David Alan Gilbert <linux@treblig.org>
Cc: Guo Ren <guoren@kernel.org>
Cc: Howard Chu <howardchu95@gmail.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: John Garry <john.g.garry@oracle.com>
Cc: Julia Lawall <Julia.Lawall@inria.fr>
Cc: Justin Stitt <justinstitt@google.com>
Cc: Krzysztof Łopatowski <krzysztof.m.lopatowski@gmail.com>
Cc: Leo Yan <leo.yan@linux.dev>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Nathan Chancellor <nathan@kernel.org>
Cc: Nick Desaulniers <nick.desaulniers+lkml@gmail.com>
Cc: Palmer Dabbelt <palmer@dabbelt.com>
Cc: Paul Walmsley <pjw@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Sergei Trofimovich <slyich@gmail.com>
Cc: Shimin Guo <shimin.guo@skydio.com>
Cc: Suchit Karunakaran <suchitkarunakaran@gmail.com>
Cc: Thomas Falcon <thomas.falcon@intel.com>
Cc: Tianyou Li <tianyou.li@intel.com>
Cc: Will Deacon <will@kernel.org>
Cc: Zecheng Li <zecheng@google.com>
[ Include elf.h for EM_CSKY and friends and also conditionally define EM_CSKY_ABIMASK for old distros ]
Signed-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent c4e3a003
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -1198,7 +1198,7 @@ int __hist_entry__tui_annotate(struct hist_entry *he, struct map_symbol *ms,
				ui__warning("Annotation has no source code.");
		}
	} else {
		err = evsel__get_arch(evsel, &browser.arch);
		err = thread__get_arch(ms->thread, &browser.arch);
		if (err) {
			annotate_browser__symbol_annotate_error(&browser, err);
			return -1;
+11 −3
Original line number Diff line number Diff line
// SPDX-License-Identifier: GPL-2.0
#include <linux/compiler.h>
#include <linux/zalloc.h>
#include "../disasm.h"

int arc__annotate_init(struct arch *arch, char *cpuid __maybe_unused)
const struct arch *arch__new_arc(const struct e_machine_and_e_flags *id,
				 const char *cpuid __maybe_unused)
{
	arch->initialized = true;
	struct arch *arch = zalloc(sizeof(*arch));

	if (!arch)
		return NULL;

	arch->name = "arc";
	arch->id = *id;
	arch->objdump.comment_char = ';';
	return 0;
	return arch;
}
+21 −18
Original line number Diff line number Diff line
@@ -7,14 +7,15 @@
#include "../annotate.h"
#include "../disasm.h"

struct arm_annotate {
	regex_t call_insn,
		jump_insn;
struct arch_arm {
	struct arch arch;
	regex_t call_insn;
	regex_t jump_insn;
};

static const struct ins_ops *arm__associate_instruction_ops(struct arch *arch, const char *name)
{
	struct arm_annotate *arm = arch->priv;
	struct arch_arm *arm = container_of(arch, struct arch_arm, arch);
	const struct ins_ops *ops;
	regmatch_t match[2];

@@ -29,37 +30,39 @@ static const struct ins_ops *arm__associate_instruction_ops(struct arch *arch, c
	return ops;
}

int arm__annotate_init(struct arch *arch, char *cpuid __maybe_unused)
const struct arch *arch__new_arm(const struct e_machine_and_e_flags *id,
				 const char *cpuid __maybe_unused)
{
	struct arm_annotate *arm;
	int err;
	struct arch_arm *arm = zalloc(sizeof(*arm));
	struct arch *arch;

	if (arch->initialized)
		return 0;

	arm = zalloc(sizeof(*arm));
	if (!arm)
		return ENOMEM;
		return NULL;

	arch = &arm->arch;
	arch->name = "arm";
	arch->id = *id;
	arch->objdump.comment_char	  = ';';
	arch->objdump.skip_functions_char = '+';
	arch->associate_instruction_ops   = arm__associate_instruction_ops;

#define ARM_CONDS "(cc|cs|eq|ge|gt|hi|le|ls|lt|mi|ne|pl|vc|vs)"
	err = regcomp(&arm->call_insn, "^blx?" ARM_CONDS "?$", REG_EXTENDED);
	if (err)
		goto out_free_arm;

	err = regcomp(&arm->jump_insn, "^bx?" ARM_CONDS "?$", REG_EXTENDED);
	if (err)
		goto out_free_call;
#undef ARM_CONDS

	arch->initialized = true;
	arch->priv	  = arm;
	arch->associate_instruction_ops   = arm__associate_instruction_ops;
	arch->objdump.comment_char	  = ';';
	arch->objdump.skip_functions_char = '+';
	return 0;
	return arch;

out_free_call:
	regfree(&arm->call_insn);
out_free_arm:
	free(arm);
	return SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_REGEXP;
	errno = SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_REGEXP;
	return NULL;
}
+21 −18
Original line number Diff line number Diff line
@@ -8,9 +8,10 @@
#include "../annotate.h"
#include "../disasm.h"

struct arm64_annotate {
	regex_t call_insn,
		jump_insn;
struct arch_arm64 {
	struct arch arch;
	regex_t call_insn;
	regex_t jump_insn;
};

static int arm64_mov__parse(const struct arch *arch __maybe_unused,
@@ -70,7 +71,7 @@ static const struct ins_ops arm64_mov_ops = {

static const struct ins_ops *arm64__associate_instruction_ops(struct arch *arch, const char *name)
{
	struct arm64_annotate *arm = arch->priv;
	struct arch_arm64 *arm = container_of(arch, struct arch_arm64, arch);
	const struct ins_ops *ops;
	regmatch_t match[2];

@@ -87,38 +88,40 @@ static const struct ins_ops *arm64__associate_instruction_ops(struct arch *arch,
	return ops;
}

int arm64__annotate_init(struct arch *arch, char *cpuid __maybe_unused)
const struct arch *arch__new_arm64(const struct e_machine_and_e_flags *id,
				   const char *cpuid __maybe_unused)
{
	struct arm64_annotate *arm;
	int err;
	struct arch_arm64 *arm = zalloc(sizeof(*arm));
	struct arch *arch;

	if (arch->initialized)
		return 0;

	arm = zalloc(sizeof(*arm));
	if (!arm)
		return ENOMEM;
		return NULL;

	arch = &arm->arch;
	arch->name = "arm64";
	arch->id = *id;
	arch->objdump.comment_char	  = '/';
	arch->objdump.skip_functions_char = '+';
	arch->associate_instruction_ops   = arm64__associate_instruction_ops;

	/* bl, blr */
	err = regcomp(&arm->call_insn, "^blr?$", REG_EXTENDED);
	if (err)
		goto out_free_arm;

	/* b, b.cond, br, cbz/cbnz, tbz/tbnz */
	err = regcomp(&arm->jump_insn, "^[ct]?br?\\.?(cc|cs|eq|ge|gt|hi|hs|le|lo|ls|lt|mi|ne|pl|vc|vs)?n?z?$",
		      REG_EXTENDED);
	if (err)
		goto out_free_call;

	arch->initialized = true;
	arch->priv	  = arm;
	arch->associate_instruction_ops   = arm64__associate_instruction_ops;
	arch->objdump.comment_char	  = '/';
	arch->objdump.skip_functions_char = '+';
	return 0;
	return arch;

out_free_call:
	regfree(&arm->call_insn);
out_free_arm:
	free(arm);
	return SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_REGEXP;
	errno = SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_REGEXP;
	return NULL;
}
+11 −3
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@
// Copyright (C) 2019 Hangzhou C-SKY Microsystems co.,ltd.
#include <string.h>
#include <linux/compiler.h>
#include <linux/zalloc.h>
#include "../disasm.h"

static const struct ins_ops *csky__associate_ins_ops(struct arch *arch,
@@ -39,10 +40,17 @@ static const struct ins_ops *csky__associate_ins_ops(struct arch *arch,
	return ops;
}

int csky__annotate_init(struct arch *arch, char *cpuid __maybe_unused)
const struct arch *arch__new_csky(const struct e_machine_and_e_flags *id,
				  const char *cpuid __maybe_unused)
{
	arch->initialized = true;
	struct arch *arch = zalloc(sizeof(*arch));

	if (!arch)
		return NULL;

	arch->name = "csky";
	arch->id = *id;
	arch->objdump.comment_char = '/';
	arch->associate_instruction_ops = csky__associate_ins_ops;
	return 0;
	return arch;
}
Loading