Building the Linux kernel with Clang and LLVM
 help / color / mirror / Atom feed
From: Adrian Hunter <adrian.hunter@intel.com>
To: Changbin Du <changbin.du@huawei.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>,
	Nathan Chancellor <nathan@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
	"Liang, Kan" <kan.liang@linux.intel.com>,
	Nick Desaulniers <ndesaulniers@google.com>,
	Bill Wendling <morbo@google.com>,
	Justin Stitt <justinstitt@google.com>,
	linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org,
	llvm@lists.linux.dev, Hui Wang <hw.huiwang@huawei.com>
Subject: Re: [PATCH v6 4/8] perf: symbol: generalize vmlinux path searching
Date: Wed, 11 Sep 2024 11:03:55 +0300	[thread overview]
Message-ID: <17b837bd-d1b5-48f4-9a16-922b71f195e3@intel.com> (raw)
In-Reply-To: <20240725021549.880167-5-changbin.du@huawei.com>

On 25/07/24 05:15, Changbin Du wrote:
> This generalizes the vmlinux path searching logic. Later we will add
> another instance for vdso.
> 
> The search pattern is described by struct dso_filename_pattern, and the
> formatted paths are hold in struct dso_filename_paths.
> 
> Signed-off-by: Changbin Du <changbin.du@huawei.com>
> ---
>  tools/perf/util/machine.c |   4 +-
>  tools/perf/util/symbol.c  | 112 +++++++++++++++++++++-----------------
>  tools/perf/util/symbol.h  |   8 ++-
>  3 files changed, 70 insertions(+), 54 deletions(-)
> 
> diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c
> index 8477edefc299..68315520f15b 100644
> --- a/tools/perf/util/machine.c
> +++ b/tools/perf/util/machine.c
> @@ -896,9 +896,9 @@ size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
>  			printed += fprintf(fp, "[0] %s\n", filename);
>  	}
>  
> -	for (i = 0; i < vmlinux_path__nr_entries; ++i) {
> +	for (i = 0; i < vmlinux_paths.nr_entries; ++i) {
>  		printed += fprintf(fp, "[%d] %s\n", i + dso__has_build_id(kdso),
> -				   vmlinux_path[i]);
> +				   vmlinux_paths.paths[i]);
>  	}
>  	return printed;
>  }
> diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
> index ad3b7b929e94..6bf75c98e1f2 100644
> --- a/tools/perf/util/symbol.c
> +++ b/tools/perf/util/symbol.c
> @@ -48,8 +48,7 @@ static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map);
>  static int dso__load_vdso_sym(struct dso *dso, struct map *map);
>  static bool symbol__is_idle(const char *name);
>  
> -int vmlinux_path__nr_entries;
> -char **vmlinux_path;
> +struct dso_filename_paths vmlinux_paths;
>  
>  struct symbol_conf symbol_conf = {
>  	.nanosecs		= false,
> @@ -2042,10 +2041,10 @@ int dso__load_vmlinux_path(struct dso *dso, struct map *map)
>  	char *filename = NULL;
>  
>  	pr_debug("Looking at the vmlinux_path (%d entries long)\n",
> -		 vmlinux_path__nr_entries + 1);
> +		 vmlinux_paths.nr_entries + 1);
>  
> -	for (i = 0; i < vmlinux_path__nr_entries; ++i) {
> -		err = dso__load_vmlinux(dso, map, vmlinux_path[i], false);
> +	for (i = 0; i < vmlinux_paths.nr_entries; ++i) {
> +		err = dso__load_vmlinux(dso, map, vmlinux_paths.paths[i], false);
>  		if (err > 0)
>  			goto out;
>  	}
> @@ -2209,7 +2208,7 @@ static int dso__load_kernel_sym(struct dso *dso, struct map *map)
>  			return err;
>  	}
>  
> -	if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) {
> +	if (!symbol_conf.ignore_vmlinux && vmlinux_paths.paths != NULL) {
>  		err = dso__load_vmlinux_path(dso, map);
>  		if (err > 0)
>  			return err;
> @@ -2284,57 +2283,55 @@ static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map)
>  	return err;
>  }
>  
> -static void vmlinux_path__exit(void)
> -{
> -	while (--vmlinux_path__nr_entries >= 0)
> -		zfree(&vmlinux_path[vmlinux_path__nr_entries]);
> -	vmlinux_path__nr_entries = 0;
> -
> -	zfree(&vmlinux_path);
> -}
> -
> -static const char * const vmlinux_paths[] = {
> -	"vmlinux",
> -	"/boot/vmlinux"
> +struct dso_filename_pattern {
> +	const char *pattern;
> +	/*
> +	 * 0 for matching directly,
> +	 * 1 for matching by kernel_version,
> +	 * 2 for matching by kernel_version + arch.
> +	 */
> +	int match_type;
>  };
>  
> -static const char * const vmlinux_paths_upd[] = {
> -	"/boot/vmlinux-%s",
> -	"/usr/lib/debug/boot/vmlinux-%s",
> -	"/lib/modules/%s/build/vmlinux",
> -	"/usr/lib/debug/lib/modules/%s/vmlinux",
> -	"/usr/lib/debug/boot/vmlinux-%s.debug"
> +struct dso_filename_pattern vmlinux_patterns[] = {
> +	{"vmlinux", 0},
> +	{"/boot/vmlinux", 0},
> +	{"/boot/vmlinux-%s", 1},
> +	{"/usr/lib/debug/boot/vmlinux-%s", 1},
> +	{"/lib/modules/%s/build/vmlinux", 1},
> +	{"/usr/lib/debug/lib/modules/%s/vmlinux", 1},
> +	{"/usr/lib/debug/boot/vmlinux-%s.debug", 1},
>  };
>  
> -static int vmlinux_path__add(const char *new_entry)
> +static int dso_filename_path__add(struct dso_filename_paths *paths, const char *new_entry)
>  {
> -	vmlinux_path[vmlinux_path__nr_entries] = strdup(new_entry);
> -	if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
> +	paths->paths[paths->nr_entries] = strdup(new_entry);
> +	if (paths->paths[paths->nr_entries] == NULL)
>  		return -1;
> -	++vmlinux_path__nr_entries;
> +	++paths->nr_entries;
>  
>  	return 0;
>  }
>  
> -static int vmlinux_path__init(struct perf_env *env)
> +static void dso_filename_path__exit(struct dso_filename_paths *paths)
>  {
> -	struct utsname uts;
> -	char bf[PATH_MAX];
> -	char *kernel_version;
> -	unsigned int i;
> +	while (--paths->nr_entries >= 0)
> +		zfree(&paths->paths[paths->nr_entries]);
> +	paths->nr_entries = 0;
>  
> -	vmlinux_path = malloc(sizeof(char *) * (ARRAY_SIZE(vmlinux_paths) +
> -			      ARRAY_SIZE(vmlinux_paths_upd)));
> -	if (vmlinux_path == NULL)
> -		return -1;
> -
> -	for (i = 0; i < ARRAY_SIZE(vmlinux_paths); i++)
> -		if (vmlinux_path__add(vmlinux_paths[i]) < 0)
> -			goto out_fail;
> +	zfree(&paths->paths);
> +}
>  
> -	/* only try kernel version if no symfs was given */
> -	if (symbol_conf.symfs[0] != 0)
> -		return 0;
> +static int dso_filename_path__init(struct dso_filename_paths *paths,
> +				   struct dso_filename_pattern *patterns,
> +				   int nr_patterns,
> +				   struct perf_env *env)
> +{
> +	struct utsname uts;
> +	char bf[PATH_MAX];
> +	const char *kernel_version;
> +	const char *arch = perf_env__arch(env);
> +	int i;
>  
>  	if (env) {
>  		kernel_version = env->os_release;
> @@ -2345,16 +2342,28 @@ static int vmlinux_path__init(struct perf_env *env)
>  		kernel_version = uts.release;
>  	}
>  
> -	for (i = 0; i < ARRAY_SIZE(vmlinux_paths_upd); i++) {
> -		snprintf(bf, sizeof(bf), vmlinux_paths_upd[i], kernel_version);
> -		if (vmlinux_path__add(bf) < 0)
> +	paths->paths = malloc(sizeof(char *) * nr_patterns);
> +	if (paths->paths == NULL)
> +		return -1;
> +
> +	for (i = 0; i < nr_patterns; i++) {
> +		if (patterns[i].match_type == 0)
> +			strlcpy(bf, patterns[i].pattern, sizeof(bf));
> +		else if (symbol_conf.symfs[0] == 0) {
> +			/* only try kernel version if no symfs was given */
> +			if (patterns[i].match_type == 1)
> +				snprintf(bf, sizeof(bf), patterns[i].pattern, kernel_version);
> +			else if (patterns[i].match_type == 2)
> +				snprintf(bf, sizeof(bf), patterns[i].pattern, kernel_version, arch);
> +		}
> +		if (dso_filename_path__add(paths, bf) < 0)
>  			goto out_fail;
>  	}
>  
>  	return 0;
>  
>  out_fail:
> -	vmlinux_path__exit();
> +	dso_filename_path__exit(paths);
>  	return -1;
>  }
>  
> @@ -2550,8 +2559,11 @@ int symbol__init(struct perf_env *env)
>  
>  	symbol__elf_init();
>  
> -	if (symbol_conf.try_vmlinux_path && vmlinux_path__init(env) < 0)
> +	if (symbol_conf.try_vmlinux_path &&
> +	    dso_filename_path__init(&vmlinux_paths, vmlinux_patterns,
> +				    ARRAY_SIZE(vmlinux_patterns), env) < 0) {
>  		return -1;
> +	}
>  
>  	if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
>  		pr_err("'.' is the only non valid --field-separator argument\n");
> @@ -2628,7 +2640,7 @@ void symbol__exit(void)
>  	intlist__delete(symbol_conf.tid_list);
>  	intlist__delete(symbol_conf.pid_list);
>  	intlist__delete(symbol_conf.addr_list);
> -	vmlinux_path__exit();
> +	dso_filename_path__exit(&vmlinux_paths);
>  	symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
>  	symbol_conf.bt_stop_list = NULL;
>  	symbol_conf.initialized = false;
> diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
> index 3fb5d146d9b1..30056884945b 100644
> --- a/tools/perf/util/symbol.h
> +++ b/tools/perf/util/symbol.h
> @@ -101,8 +101,12 @@ static inline int __symbol__join_symfs(char *bf, size_t size, const char *path)
>  
>  #define symbol__join_symfs(bf, path) __symbol__join_symfs(bf, sizeof(bf), path)
>  
> -extern int vmlinux_path__nr_entries;
> -extern char **vmlinux_path;
> +struct dso_filename_paths {
> +	int nr_entries;
> +	char **paths;
> +};

Feels a bit over engineered.  We only need the nth path so
a simpler, more encapsulated API could be just:

const char *vdso_path(size_t i);

Also wonder why the paths cannot just be created by asprint().
e.g. notwithstanding that perf_env__os_release() does not exist:


#define MAX_VDSO_PATHS 8
static char *vdso_paths[MAX_VDSO_PATHS];
static size_t vdso_path__nr_entries;

static int vdso_path__init(struct perf_env *env)
{
	const char *k = perf_env__os_release(env);
	const char *a = perf_env__arch(env);
	int i = 0;

#define PATH_INIT(fmt, ...)						\
	(i < MAX_VDSO_PATHS ? ({					\
		int ret = asprintf(&vdso_paths[i], fmt, ##__VA_ARGS__);	\
		if (ret >= 0) {						\
			i += 1;						\
			ret = 0;					\
		}							\
		ret;							\
	}) : -1)

	if (PATH_INIT("/lib/modules/%s/vdso/vdso.so", k) ||
	    PATH_INIT("/lib/modules/%s/vdso/vdso64.so", k) ||
	    PATH_INIT("/lib/modules/%s/vdso/vdso32.so", k) ||
	    PATH_INIT("/lib/modules/%s/build/arch/%s/vdso/vdso.so.dbg", k, a) ||
	    PATH_INIT("/lib/modules/%s/build/arch/%s/kernel/vdso/vdso.so.dbg", k, a) ||
	    PATH_INIT("/lib/modules/%s/build/arch/%s/entry/vdso/vdso32.so.dbg", k, a) ||
	    PATH_INIT("/lib/modules/%s/build/arch/%s/entry/vdso/vdso64.so.dbg", k, a))
		goto out_err;

#undef PATH_INIT

	vdso_path__nr_entries = i;
	return 0;

out_err:
	while (i)
		zfree(&vdso_paths[--i]);
	return -ENOMEM;
}

static void vdso_path__exit(void)
{
	while (vdso_path__nr_entries)
		zfree(&vdso_paths[--vdso_path__nr_entries]);
}

const char *vdso_path(size_t i)
{
	if (i >= vdso_path__nr_entries)
		return NULL;

	return vdso_paths[i];
}


  reply	other threads:[~2024-09-11  8:04 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-25  2:15 [PATCH v6 0/8] perf: Support searching local debugging vdso or specify vdso path in cmdline Changbin Du
2024-07-25  2:15 ` [PATCH v6 1/8] perf: support " Changbin Du
2024-09-11  8:03   ` Adrian Hunter
2024-09-12 10:09     ` duchangbin
2024-07-25  2:15 ` [PATCH v6 2/8] perf: disasm: refactor function dso__disassemble_filename Changbin Du
2024-07-25  2:15 ` [PATCH v6 3/8] perf: disasm: use build_id_path if fallback failed Changbin Du
2024-07-25  2:15 ` [PATCH v6 4/8] perf: symbol: generalize vmlinux path searching Changbin Du
2024-09-11  8:03   ` Adrian Hunter [this message]
2024-07-25  2:15 ` [PATCH v6 5/8] perf: build-id: add support for build-id cache vdso debug Changbin Du
2024-09-11  8:04   ` Adrian Hunter
2024-07-25  2:15 ` [PATCH v6 6/8] perf: build-id: extend build_id_cache__find_debug() to find local debugging vdso Changbin Du
2024-09-11  8:04   ` Adrian Hunter
2024-07-25  2:15 ` [PATCH v6 7/8] perf: disasm: prefer debugging files in build-id cache Changbin Du
2024-09-11  8:05   ` Adrian Hunter
2024-07-25  2:15 ` [PATCH v6 8/8] perf buildid-cache: recognize vdso when adding files Changbin Du
2024-09-11  8:05   ` Adrian Hunter
2024-09-12 11:10     ` duchangbin
  -- strict thread matches above, loose matches on Subject: below --
2024-08-16 10:58 [RESEND PATCH v6 0/8] perf: Support searching local debugging vdso or specify vdso path in cmdline Changbin Du
2024-08-16 10:58 ` [PATCH v6 4/8] perf: symbol: generalize vmlinux path searching Changbin Du

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=17b837bd-d1b5-48f4-9a16-922b71f195e3@intel.com \
    --to=adrian.hunter@intel.com \
    --cc=acme@kernel.org \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=changbin.du@huawei.com \
    --cc=hw.huiwang@huawei.com \
    --cc=irogers@google.com \
    --cc=jolsa@kernel.org \
    --cc=justinstitt@google.com \
    --cc=kan.liang@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=morbo@google.com \
    --cc=namhyung@kernel.org \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox