diff mbox series

[1/1] pahole: Add option to obtain a vmlinux matching the running kernel

Message ID Zs977_n0rkleEl94@x1 (mailing list archive)
State Not Applicable
Headers show
Series [1/1] pahole: Add option to obtain a vmlinux matching the running kernel | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch

Commit Message

Arnaldo Carvalho de Melo Aug. 28, 2024, 7:35 p.m. UTC
To use in regression tests scripts, but in general its a nice utility to
use in other kinds of scripts.

All this matches:

  $ pahole --running_kernel_vmlinux
  /lib/modules/6.11.0-rc5+/build/vmlinux
  $ file `pahole --running_kernel_vmlinux`
  /lib/modules/6.11.0-rc5+/build/vmlinux: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, BuildID[sha1]=4dd7f9c4507b82a5bf90671d524e5bb308104ffa, with debug_info, not stripped
  $ perf buildid-list -k
  4dd7f9c4507b82a5bf90671d524e5bb308104ffa
  $ perf buildid-list -i /lib/modules/6.11.0-rc5+/build/vmlinux
  4dd7f9c4507b82a5bf90671d524e5bb308104ffa
  $

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>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 man-pages/pahole.1 |  4 ++++
 pahole.c           | 24 ++++++++++++++++++++++++
 2 files changed, 28 insertions(+)

Comments

Eduard Zingerman Aug. 29, 2024, 12:08 a.m. UTC | #1
On Wed, 2024-08-28 at 16:35 -0300, Arnaldo Carvalho de Melo wrote:

[...]

Tested-by: Eduard Zingerman <eddyz87@gmail.com>

> @@ -3707,6 +3716,21 @@ int main(int argc, char *argv[])
>  		goto out;
>  	}
>  
> +	if (show_running_kernel_vmlinux) {
> +		const char *vmlinux = vmlinux_path__find_running_kernel();
> +
> +		if (vmlinux) {
> +			fprintf(stdout, "%s\n", vmlinux);
> +			rc = EXIT_SUCCESS;
> +		} else {
> +			fputs("pahole: couldn't find a vmlinux that matches the running kernel\n"
> +			      "HINT: Maybe you're inside a container or missing a debuginfo package?\n",
> +			      stderr);
> +		}

Nitpick: when run with valgrind this reports a leak for 'vmlinux':

    ==186== Memcheck, a memory error detector
    ==186== Copyright (C) 2002-2024, and GNU GPL'd, by Julian Seward et al.
    ==186== Using Valgrind-3.23.0 and LibVEX; rerun with -h for copyright info
    ==186== Command: /home/eddy/work/dwarves-fork/build/pahole --running_kernel_vmlinux
    ==186== 
    vmlinux
    ==186== 
    ==186== HEAP SUMMARY:
    ==186==     in use at exit: 8 bytes in 1 blocks
    ==186==   total heap usage: 15 allocs, 14 frees, 21,408 bytes allocated
    ==186== 
    ==186== 8 bytes in 1 blocks are definitely lost in loss record 1 of 1
    ==186==    at 0x4843866: malloc (vg_replace_malloc.c:446)
    ==186==    by 0x4A8F9AE: strdup (strdup.c:42)
    ==186==    by 0x48755EC: vmlinux_path__find_running_kernel (dwarves.c:2718)
    ==186==    by 0x40ABBD: main (pahole.c:3720)
    ==186== 
    ==186== LEAK SUMMARY:
    ==186==    definitely lost: 8 bytes in 1 blocks
    ==186==    indirectly lost: 0 bytes in 0 blocks
    ==186==      possibly lost: 0 bytes in 0 blocks
    ==186==    still reachable: 0 bytes in 0 blocks
    ==186==         suppressed: 0 bytes in 0 blocks

While technically this is not a problem, maybe add a call to free()
just to avoid noise in valgrind output?
(Note that 'const' qualifier is no longer needed for
 vmlinux_path__find_running_kernel, as it returns strdup result).

> +
> +		return rc;
> +	}
> +
>  	if (languages.str && parse_languages())
>  		return rc;
>
diff mbox series

Patch

diff --git a/man-pages/pahole.1 b/man-pages/pahole.1
index 2f623b4e42619114..0a9d8ac49fbf94d5 100644
--- a/man-pages/pahole.1
+++ b/man-pages/pahole.1
@@ -339,6 +339,10 @@  Identical to \-\-btf_features above, but pahole will exit if it encounters an un
 .B \-\-supported_btf_features
 Show set of BTF features supported by \-\-btf_features option and exit.  Useful for checking which features are supported since \-\-btf_features will not emit an error if an unrecognized feature is specified.
 
+.TP
+.B \-\-running_kernel_vmlinux,
+Search for, possibly getting from a debuginfo server, a vmlinux matching the running kernel build-id (from /sys/kernel/notes).
+
 .TP
 .B \-l, \-\-show_first_biggest_size_base_type_member
 Show first biggest size base_type member.
diff --git a/pahole.c b/pahole.c
index 226361dcf349c777..a33490d698ead7d5 100644
--- a/pahole.c
+++ b/pahole.c
@@ -37,6 +37,7 @@  static bool ctf_encode;
 static bool sort_output;
 static bool need_resort;
 static bool first_obj_only;
+static bool show_running_kernel_vmlinux;
 static const char *base_btf_file;
 
 static const char *prettify_input_filename;
@@ -1237,6 +1238,7 @@  ARGP_PROGRAM_VERSION_HOOK_DEF = dwarves_print_version;
 #define ARGP_btf_features_strict 343
 #define ARGP_contains_enumerator 344
 #define ARGP_reproducible_build 345
+#define ARGP_running_kernel_vmlinux 346
 
 /* --btf_features=feature1[,feature2,..] allows us to specify
  * a list of requested BTF features or "default" to enable all default
@@ -1851,6 +1853,11 @@  static const struct argp_option pahole__options[] = {
 		.key = ARGP_reproducible_build,
 		.doc = "Generate reproducile BTF output"
 	},
+	{
+		.name = "running_kernel_vmlinux",
+		.key = ARGP_running_kernel_vmlinux,
+		.doc = "Search for, possibly getting from a debuginfo server, a vmlinux matching the running kernel build-id (from /sys/kernel/notes)"
+	},
 	{
 		.name = NULL,
 	}
@@ -2032,6 +2039,8 @@  static error_t pahole__options_parser(int key, char *arg,
 		conf_load.skip_encoding_btf_inconsistent_proto = true; break;
 	case ARGP_reproducible_build:
 		conf_load.reproducible_build = true;	break;
+	case ARGP_running_kernel_vmlinux:
+		show_running_kernel_vmlinux = true;	break;
 	case ARGP_btf_features:
 		parse_btf_features(arg, false);		break;
 	case ARGP_supported_btf_features:
@@ -3707,6 +3716,21 @@  int main(int argc, char *argv[])
 		goto out;
 	}
 
+	if (show_running_kernel_vmlinux) {
+		const char *vmlinux = vmlinux_path__find_running_kernel();
+
+		if (vmlinux) {
+			fprintf(stdout, "%s\n", vmlinux);
+			rc = EXIT_SUCCESS;
+		} else {
+			fputs("pahole: couldn't find a vmlinux that matches the running kernel\n"
+			      "HINT: Maybe you're inside a container or missing a debuginfo package?\n",
+			      stderr);
+		}
+
+		return rc;
+	}
+
 	if (languages.str && parse_languages())
 		return rc;