public inbox for dwarves@vger.kernel.org
 help / color / mirror / Atom feed
From: Arnaldo Carvalho de Melo <acme@kernel.org>
To: Alan Maguire <alan.maguire@oracle.com>
Cc: Jiri Olsa <jolsa@kernel.org>,
	Clark Williams <williams@redhat.com>,
	Kate Carcia <kcarcia@redhat.com>,
	dwarves@vger.kernel.org,
	Arnaldo Carvalho de Melo <acme@redhat.com>,
	Matthias Schwarzott <zzam@gentoo.org>,
	Andrii Nakryiko <andrii@kernel.org>,
	Eduard Zingerman <eddyz87@gmail.com>, Song Liu <song@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>
Subject: [PATCH 1/5] core: Add method to get the vmlinux BTF filename, allow overriding it via env var
Date: Tue, 19 Nov 2024 10:40:28 -0300	[thread overview]
Message-ID: <20241119134032.783215-2-acme@kernel.org> (raw)
In-Reply-To: <20241119134032.783215-1-acme@kernel.org>

From: Arnaldo Carvalho de Melo <acme@redhat.com>

In systems where BTF isn't available there were reports that the
simplest pahole call, without any args, segfaults.

To have a proper test before fixing this problem, allow overriding the
/sys/kernel/btf/vmlinux filename, so that even in systems with BTF we an
point it to a invalid location, making pahole think that there is no BTF
available and thus fallback to something that currently segfaults.

Using it:

  $ pahole list_head
  struct list_head {
  	struct list_head *         next;                 /*     0     8 */
  	struct list_head *         prev;                 /*     8     8 */

  	/* size: 16, cachelines: 1, members: 2 */
  	/* last cacheline: 16 bytes */
  };
  $
  $ PAHOLE_VMLINUX_BTF_FILENAME=foobar pahole list_head
  Segmentation fault (core dumped)
  $

Reported-by: Matthias Schwarzott <zzam@gentoo.org>
Cc: Alan Maguire <alan.maguire@oracle.com>
Cc: Andrii Nakryiko <andrii@kernel.org>
Cc: Eduard Zingerman <eddyz87@gmail.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Song Liu <song@kernel.org>
Cc: Yonghong Song <yonghong.song@linux.dev>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 dwarves.c          | 17 +++++++++++++++--
 dwarves.h          |  2 ++
 man-pages/pahole.1 |  4 ++++
 pahole.c           |  2 +-
 4 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/dwarves.c b/dwarves.c
index 14ba4f0c6dd7f90f..5b51b55191177e21 100644
--- a/dwarves.c
+++ b/dwarves.c
@@ -2807,6 +2807,19 @@ static int filename__sprintf_build_id(const char *pathname, char *sbuild_id)
 static int vmlinux_path__nr_entries;
 static char **vmlinux_path;
 
+const char *vmlinux_path__btf_filename(void)
+{
+	static const char *vmlinux_btf;
+
+	if (vmlinux_btf == NULL) {
+		vmlinux_btf = getenv("PAHOLE_VMLINUX_BTF_FILENAME");
+		if (vmlinux_btf == NULL)
+			vmlinux_btf = "/sys/kernel/btf/vmlinux";
+	}
+
+	return vmlinux_btf;
+}
+
 static void vmlinux_path__exit(void)
 {
 	while (--vmlinux_path__nr_entries >= 0)
@@ -2933,7 +2946,7 @@ static int cus__load_running_kernel(struct cus *cus, struct conf_load *conf)
 	int err = 0;
 
 	if ((!conf || conf->format_path == NULL || strncmp(conf->format_path, "btf", 3) == 0) &&
-	    access("/sys/kernel/btf/vmlinux", R_OK) == 0) {
+	    access(vmlinux_path__btf_filename(), R_OK) == 0) {
 		int loader = debugging_formats__loader("btf");
 		if (loader == -1)
 			goto try_elf;
@@ -2941,7 +2954,7 @@ static int cus__load_running_kernel(struct cus *cus, struct conf_load *conf)
 		if (conf && conf->conf_fprintf)
 			conf->conf_fprintf->has_alignment_info = debug_fmt_table[loader]->has_alignment_info;
 
-		if (debug_fmt_table[loader]->load_file(cus, conf, "/sys/kernel/btf/vmlinux") == 0)
+		if (debug_fmt_table[loader]->load_file(cus, conf, vmlinux_path__btf_filename()) == 0)
 			return 0;
 	}
 try_elf:
diff --git a/dwarves.h b/dwarves.h
index 49988f1cf9d45be8..1cb0d629f5265523 100644
--- a/dwarves.h
+++ b/dwarves.h
@@ -1628,6 +1628,8 @@ void dwarves__resolve_cacheline_size(const struct conf_load *conf, uint16_t user
 
 const char *dwarf_tag_name(const uint32_t tag);
 
+const char *vmlinux_path__btf_filename(void);
+
 const char *vmlinux_path__find_running_kernel(void);
 
 struct argp_state;
diff --git a/man-pages/pahole.1 b/man-pages/pahole.1
index bd28259b65429acc..39e7b465c64e7e2c 100644
--- a/man-pages/pahole.1
+++ b/man-pages/pahole.1
@@ -75,6 +75,10 @@ struct list_head {
 };
 $
 .fi
+It is possible to override the /sys/kernel/btf/vmlinux file location by setting
+the PAHOLE_VMLINUX_BTF_FILENAME environment variable. This may be useful for
+testing, scripting when using a different BTF for vmlinux. Used in the pahole
+regression tests.
 
 If BTF is not present and no file is passed, then a vmlinux that matches the
 build-id for the running kernel will be looked up in the usual places,
diff --git a/pahole.c b/pahole.c
index 55d04cf82a3da683..b94cb1a979a6923d 100644
--- a/pahole.c
+++ b/pahole.c
@@ -3754,7 +3754,7 @@ try_sole_arg_as_class_names:
 		if (filename &&
 		    strstarts(filename, "/sys/kernel/btf/") &&
 		    strstr(filename, "/vmlinux") == NULL) {
-			base_btf_file = "/sys/kernel/btf/vmlinux";
+			base_btf_file = vmlinux_path__btf_filename();
 			conf_load.base_btf = btf__parse(base_btf_file, NULL);
 			if (libbpf_get_error(conf_load.base_btf)) {
 				fprintf(stderr, "Failed to parse base BTF '%s': %ld\n",
-- 
2.47.0


  reply	other threads:[~2024-11-19 13:40 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-19 13:40 [PATCH 0/5] Fix segfaults related to missing BTF support Arnaldo Carvalho de Melo
2024-11-19 13:40 ` Arnaldo Carvalho de Melo [this message]
2024-11-19 13:40 ` [PATCH 2/5] tests default_vmlinux_btf: Introduce test for using BTF by default Arnaldo Carvalho de Melo
2024-11-19 13:40 ` [PATCH 3/5] pahole: Honour exclusive BTF loading Arnaldo Carvalho de Melo
2024-11-19 17:47   ` Alan Maguire
2024-11-19 20:18     ` Arnaldo Carvalho de Melo
2024-11-19 20:19     ` Arnaldo Carvalho de Melo
2024-11-19 22:04   ` Eduard Zingerman
2024-11-19 22:28     ` Alan Maguire
2024-11-19 22:33       ` Eduard Zingerman
2024-11-19 13:40 ` [PATCH 4/5] tests default_vmlinux_btf: Cover the no args segfault too Arnaldo Carvalho de Melo
2024-11-19 13:40 ` [PATCH 5/5] core, libctf: Check if constructor arguments are NULL before using them Arnaldo Carvalho de Melo
  -- strict thread matches above, loose matches on Subject: below --
2024-11-18 20:41 [PATCH 0/5] Fix segfaults related to missing BTF support Arnaldo Carvalho de Melo
     [not found] ` <20241118204146.772762-2-acme@kernel.org>
     [not found]   ` <ac93d9fe-2e84-4e00-94ef-50a3074d49fc@oracle.com>
     [not found]     ` <ZzyO9aibti18J6sK@x1>
2024-11-19 17:48       ` [PATCH 1/5] core: Add method to get the vmlinux BTF filename, allow overriding it via env var Arnaldo Carvalho de Melo
     [not found]   ` <9992d2487775011278aef17d1a2db98b8cc74e7d.camel@gmail.com>
2024-11-19 18:32     ` Arnaldo Carvalho de Melo

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=20241119134032.783215-2-acme@kernel.org \
    --to=acme@kernel.org \
    --cc=acme@redhat.com \
    --cc=alan.maguire@oracle.com \
    --cc=andrii@kernel.org \
    --cc=dwarves@vger.kernel.org \
    --cc=eddyz87@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kcarcia@redhat.com \
    --cc=song@kernel.org \
    --cc=williams@redhat.com \
    --cc=yonghong.song@linux.dev \
    --cc=zzam@gentoo.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