Skip to contentSkip to conversation

perf annotate: Add elfutils libasm disassembler support

PATCH Superseded Ian Rogers 2 participants 25 messages Jun 9

Subsystems (2)
lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

v2 cover perf annotate: Add elfutils libasm disassembler support

This series adds a new libasm disassembly backend to perf annotate, leveraging the elfutils libasm library. This allows perf to use the widely available elfutils for disassembly alongside capstone, libLLVM, and objdump.

v2 changes:
- Patch 1: Added a note to the commit message acknowledging that older elfutils static builds lacking -lebl dependencies in pkg-config are explicitly unsupported, while intentionally retaining the feature check and API dependencies.
- Patch 2: Fixed NO_LIBELF=1 build breakages by implicitly disabling libasm and removing the unconditional #warning in favor of silent fallbacks or standard warnings.
- Patch 3: Fixed libasm block boundary tracking and corrected callback error/return values, preventing memory leaks and infinite loops in the disassembly extraction loop.
- Patch 5: Fixed script robustness in tests/shell/annotate.sh by ensuring cleanup functions run reliably, set -e doesn't abort prematurely on grep failures, and configs are properly restored.
- Patch 6: Refactored jit_write_elf to accept the e_machine parameter directly instead of post-patching the generated ELF file with libelf. The target architecture is now extracted dynamically and passed into jit_write_elf, simplifying the interaction with BPF and making it fully compatible with all disassemblers.
- Patch 7: Fixed a TOCTOU race condition when creating the symlink in tests/shell/annotate.sh by placing perf.data inside an isolated temporary directory.
- General: The cover letter now lists capstone, libLLVM, and objdump as the existing disassemblers instead of just objdump.

Ian Rogers (7):
tools build: Add feature check for elfutils libasm
perf build: Add build support and capability for elfutils libasm perf annotate: Implement elfutils libasm disassembler backend perf annotate: Add --disassembler command-line option
perf test: Enhance annotate test coverage and isolate config perf annotate: Support BPF JIT disassembly via genelf
perf test: Add BPF JIT annotation test coverage for all disassemblers

 tools/build/Makefile.feature       |   2 +
 tools/build/feature/Makefile       |   9 ++
 tools/build/feature/test-libasm.c  |  19 +++
 tools/perf/Makefile.config         |  22 ++++
 tools/perf/builtin-annotate.c      |  10 ++
 tools/perf/builtin-check.c         |   1 +
 tools/perf/tests/genelf.c          |   2 +-
 tools/perf/tests/shell/annotate.sh | 133 +++++++++++++++++++++
 tools/perf/util/Build              |   1 +
 tools/perf/util/annotate.c         |   8 +-
 tools/perf/util/annotate.h         |   3 +
 tools/perf/util/disasm.c           | 114 +++++++++++++++++-
 tools/perf/util/disasm.h           |   1 +
 tools/perf/util/genelf.c           |   4 +-
 tools/perf/util/genelf.h           |   2 +-
 tools/perf/util/jitdump.c          |   2 +-
 tools/perf/util/libasm.c           | 184 +++++++++++++++++++++++++++++
 tools/perf/util/libasm.h           |  27 +++++
 18 files changed, 531 insertions(+), 13 deletions(-)
 create mode 100644 tools/build/feature/test-libasm.c
 create mode 100644 tools/perf/util/libasm.c
 create mode 100644 tools/perf/util/libasm.h

--
2.54.0.1064.gd145956f57-goog

author reviewer
#1 lore raw

PATCH v2 1/7 tools build: Add feature check for elfutils libasm

This commit introduces a build-time feature test for the elfutils libasm library in the shared tools/build system. It adds the test program and updates the Makefiles to attempt building it to detect support.

Note on static linking: pkg-config for libdw/libelf on modern systems does not list -lebl as a dependency. Older versions of elfutils that require explicitly linking -lebl statically are unsupported by this feature check.

---
 tools/build/Makefile.feature      |  2 ++
 tools/build/feature/Makefile      |  9 +++++++++
 tools/build/feature/test-libasm.c | 19 +++++++++++++++++++
 3 files changed, 30 insertions(+)
 create mode 100644 tools/build/feature/test-libasm.c
tools/build/Makefile.feature +2 -0
index ed1374af31c1..422329dbc20a 100644
@@ -118,6 +118,7 @@ FEATURE_TESTS_EXTRA :=                  \
          hello                          \
          babeltrace2-ctf-writer         \
          libcapstone                    \
+         libasm                         \
          libcheck                       \
          libbfd-liberty                 \
          libbfd-liberty-z               \
@@ -150,6 +151,7 @@ FEATURE_DISPLAY ?=              \
          numa_num_possible_cpus \
          libpython              \
          libcapstone            \
+         libasm                 \
          llvm-perf              \
          zlib                   \
          lzma                   \
tools/build/feature/Makefile +9 -0
index 62909a9c799d..f0f39a2ab203 100644
@@ -47,6 +47,7 @@ FILES=                                          \
          test-timerfd.bin                       \
          test-babeltrace2-ctf-writer.bin        \
          test-libcapstone.bin			\
+         test-libasm.bin			\
          test-libcheck.bin			\
          test-compile-32.bin                    \
          test-compile-x32.bin                   \
@@ -184,6 +185,11 @@ ifeq ($(findstring -static,${LDFLAGS}),-static)
   DWARFLIBS += -ldl
 endif
 
+ASMLIBS := -lasm -ldw -lelf
+ifeq ($(findstring -static,${LDFLAGS}),-static)
+  ASMLIBS += -lz -llzma -lbz2 -lzstd
+endif
+
 $(OUTPUT)test-libdw.bin:
 	$(BUILD) $(DWLIBS)
 
@@ -311,6 +317,9 @@ $(OUTPUT)test-babeltrace2-ctf-writer.bin:
 $(OUTPUT)test-libcapstone.bin:
 	$(BUILD) # -lcapstone provided by $(FEATURE_CHECK_LDFLAGS-libcapstone)
 
+$(OUTPUT)test-libasm.bin:
+	$(BUILD) $(ASMLIBS)
+
 $(OUTPUT)test-libcheck.bin:
 	$(BUILD) # -lcheck is provided by $(FEATURE_CHECK_LDFLAGS-libcheck)
 
tools/build/feature/test-libasm.c new file +19 -0
new file mode 100644
index 000000000000..f3598ad1cc9e
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <stddef.h>
+#include <elfutils/libasm.h>
+#include <gelf.h>
+
+typedef struct ebl Ebl;
+extern Ebl *ebl_openbackend (Elf *elf);
+extern void ebl_closebackend (Ebl *ebl);
+
+int main(void)
+{
+	Elf *elf = elf_begin(0, ELF_C_READ, NULL);
+	Ebl *ebl = ebl_openbackend(elf);
+	DisasmCtx_t *ctx = disasm_begin(ebl, elf, NULL);
+	disasm_end(ctx);
+	ebl_closebackend(ebl);
+	elf_end(elf);
+	return 0;
+}
Signature · 1 line
2.54.0.1064.gd145956f57-goog
#2 lore raw

Re: [PATCH v2 1/7] tools build: Add feature check for elfutils libasm

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] The libasm feature check test relies on internal elfutils symbols (`ebl_openbackend`)…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The libasm feature check test relies on internal elfutils symbols (ebl_openbackend), guaranteeing linkage failure on all systems. --

commit cf97b2eea73923674f23b7d737c06550cb83a7a9
Author: Ian Rogers <[email protected]>

tools build: Add feature check for elfutils libasm

This commit introduces a build-time feature test for the elfutils libasm library in the shared tools/build system. It adds the test program and updates the Makefiles to attempt building it to detect support.

Quoted 4 lines
tools/build/feature/Makefile +0 -0
index 62909a9c799d3..f0f39a2ab2035 100644

[ ... ]

tools/build/feature/Makefile ifeq ($(findstring -static,${LDFLAGS}),-static) · line 188

Quoted 5 lines
tools/build/feature/Makefile +1 -0
@@ -184,6 +185,11 @@ ifeq ($(findstring -static,${LDFLAGS}),-static)
   DWARFLIBS += -ldl
 endif
 
+ASMLIBS := -lasm -ldw -lelf

[Severity: High]
On older systems where ebl_openbackend() was an exported symbol, it resided in libebl.so. Since ASMLIBS omits -lebl, won't this cause linkage failures during the test build due to missing dependencies?

Quoted 3 lines
tools/build/feature/Makefile +3 -0
+ifeq ($(findstring -static,${LDFLAGS}),-static)
+  ASMLIBS += -lz -llzma -lbz2 -lzstd
+endif

[ ... ]

Quoted 9 lines
tools/build/feature/test-libasm.c new file +3 -0
new file mode 100644
index 0000000000000..f3598ad1cc9e1
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <stddef.h>
+#include <elfutils/libasm.h>

tools/build/feature/test-libasm.c line 13

Quoted 10 lines
tools/build/feature/test-libasm.c new file +10 -0
+#include <gelf.h>
+
+typedef struct ebl Ebl;
+extern Ebl *ebl_openbackend (Elf *elf);
+extern void ebl_closebackend (Ebl *ebl);
+
+int main(void)
+{
+	Elf *elf = elf_begin(0, ELF_C_READ, NULL);
+	Ebl *ebl = ebl_openbackend(elf);

[Severity: High]
Will this test program universally fail to link?

In modern elfutils (>= 0.177), libebl was merged into libdw, and functions like ebl_openbackend() and ebl_closebackend() were made strictly internal with hidden visibility. They are not exported by libdw.so or libasm.so.

Because of the missing -lebl flag on older systems and the hidden visibility on newer systems, it seems this feature check will always fail, making any libasm disassembler backend depending on it unreachable. Is there a different way to initialize the disassembler context without relying on internal symbols?

Quoted 6 lines
tools/build/feature/test-libasm.c new file +6 -0
+	DisasmCtx_t *ctx = disasm_begin(ebl, elf, NULL);
+	disasm_end(ctx);
+	ebl_closebackend(ebl);
+	elf_end(elf);
+	return 0;
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author reviewer
#3 lore raw

PATCH v2 2/7 perf build: Add build support and capability for elfutils libasm

In reply to Ian Rogers

Configure perf build system to utilize the libasm feature check (supporting both static and dynamic builds) and export the config flags. Add "libasm" capability detection to perf check.

Assisted-by Antigravity:Google Gemini 3.5-flash
---
 tools/perf/Makefile.config | 22 ++++++++++++++++++++++
 tools/perf/builtin-check.c |  1 +
 2 files changed, 23 insertions(+)
tools/perf/Makefile.config +22 -0
index 6e7b15fab2ec..8527d20f498f 100644
@@ -199,6 +199,13 @@ endif
 FEATURE_CHECK_CFLAGS-libcapstone := $(LIBCAPSTONE_CFLAGS)
 FEATURE_CHECK_LDFLAGS-libcapstone := $(LIBCAPSTONE_LDFLAGS) -lcapstone
 
+ifdef LIBASM_DIR
+  LIBASM_CFLAGS  := -I$(LIBASM_DIR)/include
+  LIBASM_LDFLAGS := -L$(LIBASM_DIR)/lib
+endif
+FEATURE_CHECK_CFLAGS-libasm := $(LIBASM_CFLAGS)
+FEATURE_CHECK_LDFLAGS-libasm := $(LIBASM_LDFLAGS) -lasm -ldw -lelf
+
 ifdef LIBZSTD_DIR
   LIBZSTD_CFLAGS  := -I$(LIBZSTD_DIR)/lib
   LIBZSTD_LDFLAGS := -L$(LIBZSTD_DIR)/lib
@@ -432,6 +439,7 @@ ifeq ($(feature-file-handle), 1)
 endif
 
 ifdef NO_LIBELF
+  NO_LIBASM := 1
   NO_LIBDW := 1
   NO_LIBUNWIND := 1
   NO_LIBBPF := 1
@@ -1060,6 +1068,20 @@ ifndef NO_CAPSTONE
   endif
 endif
 
+ifndef NO_LIBASM
+  $(call feature_check,libasm)
+  ifeq ($(feature-libasm), 1)
+    CFLAGS += -DHAVE_LIBASM_SUPPORT $(LIBASM_CFLAGS)
+    ifdef LIBASM_DLOPEN
+      CFLAGS += -DLIBASM_DLOPEN
+    else
+      LDFLAGS += $(LIBASM_LDFLAGS)
+      EXTLIBS += -lasm -ldw -lelf
+    endif
+    $(call detected,CONFIG_LIBASM)
+  endif
+endif
+
 ifdef EXTRA_TESTS
     $(call detected,CONFIG_EXTRA_TESTS)
     CFLAGS += -DHAVE_EXTRA_TESTS
tools/perf/builtin-check.c +1 -0
index 60437650c50f..d2ffea4d9b8b 100644
@@ -46,6 +46,7 @@ struct feature_status supported_features[] = {
 	FEATURE_STATUS("babeltrace2-ctf-writer", HAVE_BABELTRACE2_CTF_WRITER_SUPPORT),
 	FEATURE_STATUS("libbpf-strings", HAVE_LIBBPF_STRINGS_SUPPORT),
 	FEATURE_STATUS("libcapstone", HAVE_LIBCAPSTONE_SUPPORT),
+	FEATURE_STATUS("libasm", HAVE_LIBASM_SUPPORT),
 	FEATURE_STATUS("libdw-dwarf-unwind", HAVE_LIBDW_SUPPORT),
 	FEATURE_STATUS("libelf", HAVE_LIBELF_SUPPORT),
 	FEATURE_STATUS("libLLVM", HAVE_LIBLLVM_SUPPORT),
Signature · 1 line
2.54.0.1064.gd145956f57-goog
#4 lore raw

Re: [PATCH v2 2/7] perf build: Add build support and capability for elfutils libasm

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] The LIBASM_DLOPEN build configuration is defined but completely lacks the corresponding C code…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] The LIBASM_DLOPEN build configuration is defined but completely lacks the corresponding C code to dynamically load the library.
- [Medium] Setting NO_LIBDW=1 fails to exclude libdw from the build, leading to unintended linkage and static build failures. --

commit 2a3530a4ffc824890cb03d14fd777712b83cb02e
Author: Ian Rogers <[email protected]>

perf build: Add build support and capability for elfutils libasm

Configure perf build system to utilize the libasm feature check (supporting both static and dynamic builds) and export the config flags. Add "libasm" capability detection to perf check.

Quoted 4 lines
tools/perf/Makefile.config +0 -0
index 6e7b15fab2ecc..8527d20f498f8 100644

[ ... ]

Quoted 8 lines
tools/perf/Makefile.config +1 -0
@@ -432,6 +439,7 @@ ifeq ($(feature-file-handle), 1)
 endif
 
 ifdef NO_LIBELF
+  NO_LIBASM := 1
   NO_LIBDW := 1
   NO_LIBUNWIND := 1
   NO_LIBBPF := 1

[ ... ]

tools/perf/Makefile.config ifndef NO_CAPSTONE · line 1076

Quoted 10 lines
tools/perf/Makefile.config +6 -0
@@ -1060,6 +1068,20 @@ ifndef NO_CAPSTONE
   endif
 endif
 
+ifndef NO_LIBASM
+  $(call feature_check,libasm)
+  ifeq ($(feature-libasm), 1)
+    CFLAGS += -DHAVE_LIBASM_SUPPORT $(LIBASM_CFLAGS)
+    ifdef LIBASM_DLOPEN
+      CFLAGS += -DLIBASM_DLOPEN

[Severity: Medium]
Is there corresponding C code to handle the dlopen and dlsym wrappers for libasm?

It looks like if LIBASM_DLOPEN is defined, the build skips linking libasm directly. However, without the runtime dynamic loading implementation, won't this result in unresolved symbol linker errors when trying to build with LIBASM_DLOPEN=1?

tools/perf/Makefile.config ifndef NO_CAPSTONE · line 1079

Quoted 3 lines
tools/perf/Makefile.config +3 -0
+    else
+      LDFLAGS += $(LIBASM_LDFLAGS)
+      EXTLIBS += -lasm -ldw -lelf

[Severity: Medium]
If a user configures the build with NO_LIBDW=1 to avoid linking libdw, won't this unconditional addition of -ldw override their request?

Setting NO_LIBDW=1 does not implicitly set NO_LIBASM=1. As a result, the libasm feature check still executes and forcibly appends -ldw to EXTLIBS.

For static builds, will this cause linker errors since NO_LIBDW=1 skips adding DWARFLIBS (like -lz and -lzstd) that libdw.a requires?

Quoted 5 lines
tools/perf/Makefile.config +5 -0
+    endif
+    $(call detected,CONFIG_LIBASM)
+  endif
+endif
+

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author reviewer
#5 lore raw

PATCH v2 3/7 perf annotate: Implement elfutils libasm disassembler backend

In reply to Ian Rogers

Implement the core disassembler backend in libasm.c utilizing elfutils' libasm API. Hook the backend into perf annotate disassembler routing. Configure libasm as the first-choice disassembler backend by default (ahead of LLVM/capstone) when available.
Also, expose annotation_options__add_disassemblers_str to allow custom disassembler selection.

Assisted-by Antigravity:Google Gemini 3.5-flash
---
 tools/perf/util/Build      |   1 +
 tools/perf/util/annotate.c |   8 +-
 tools/perf/util/annotate.h |   3 +
 tools/perf/util/disasm.c   |   5 +
 tools/perf/util/libasm.c   | 184 +++++++++++++++++++++++++++++++++++++
 tools/perf/util/libasm.h   |  27 ++++++
 6 files changed, 226 insertions(+), 2 deletions(-)
 create mode 100644 tools/perf/util/libasm.c
 create mode 100644 tools/perf/util/libasm.h
tools/perf/util/Build +1 -0
index b22cdc24082a..f1d5ebb2edfa 100644
@@ -12,6 +12,7 @@ perf-util-y += block-range.o
 perf-util-y += build-id.o
 perf-util-y += cacheline.o
 perf-util-$(CONFIG_LIBCAPSTONE) += capstone.o
+perf-util-$(CONFIG_LIBASM) += libasm.o
 perf-util-y += config.o
 perf-util-y += copyfile.o
 perf-util-y += ctype.o
tools/perf/util/annotate.c +6 -2
index 02505222d8c2..2259ac2c2cd6 100644
@@ -2233,6 +2233,7 @@ const char * const perf_disassembler__strs[] = {
 	[PERF_DISASM_UNKNOWN]  = "unknown",
 	[PERF_DISASM_LLVM]     = "llvm",
 	[PERF_DISASM_CAPSTONE] = "capstone",
+	[PERF_DISASM_LIBASM]   = "libasm",
 	[PERF_DISASM_OBJDUMP]  = "objdump",
 };
 
@@ -2254,8 +2255,8 @@ static void annotation_options__add_disassembler(struct annotation_options *opti
 	pr_err("Failed to add disassembler %d\n", dis);
 }
 
-static int annotation_options__add_disassemblers_str(struct annotation_options *options,
-						const char *str)
+int annotation_options__add_disassemblers_str(struct annotation_options *options,
+					     const char *str)
 {
 	while (str && *str != '\0') {
 		const char *comma = strchr(str, ',');
@@ -2372,6 +2373,9 @@ static void annotation_options__default_init_disassemblers(struct annotation_opt
 		/* Already initialized. */
 		return;
 	}
+#ifdef HAVE_LIBASM_SUPPORT
+	annotation_options__add_disassembler(options, PERF_DISASM_LIBASM);
+#endif
 #ifdef HAVE_LIBLLVM_SUPPORT
 	annotation_options__add_disassembler(options, PERF_DISASM_LLVM);
 #endif
tools/perf/util/annotate.h +3 -0
index 1aa6df7d1618..21ac2b6472c1 100644
@@ -38,6 +38,7 @@ enum perf_disassembler {
 	PERF_DISASM_UNKNOWN = 0,
 	PERF_DISASM_LLVM,
 	PERF_DISASM_CAPSTONE,
+	PERF_DISASM_LIBASM,
 	PERF_DISASM_OBJDUMP,
 };
 #define MAX_DISASSEMBLERS (PERF_DISASM_OBJDUMP + 1)
@@ -484,6 +485,8 @@ int hist_entry__tty_annotate2(struct hist_entry *he, struct evsel *evsel);
 
 void annotation_options__init(void);
 void annotation_options__exit(void);
+int annotation_options__add_disassemblers_str(struct annotation_options *options,
+					      const char *str);
 
 void annotation_config__init(void);
 
tools/perf/util/disasm.c +5 -0
index 59ba88e1f744..42af3603fdff 100644
@@ -22,6 +22,7 @@
 #include "capstone.h"
 #include "debug.h"
 #include "disasm.h"
+#include "libasm.h"
 #include "dso.h"
 #include "dwarf-regs.h"
 #include "env.h"
@@ -1622,6 +1623,10 @@ int symbol__disassemble(struct symbol *sym, struct annotate_args *args)
 			args->options->disassembler_used = PERF_DISASM_CAPSTONE;
 			err = symbol__disassemble_capstone(symfs_filename, sym, args);
 			break;
+		case PERF_DISASM_LIBASM:
+			args->options->disassembler_used = PERF_DISASM_LIBASM;
+			err = symbol__disassemble_libasm(symfs_filename, sym, args);
+			break;
 		case PERF_DISASM_OBJDUMP:
 			args->options->disassembler_used = PERF_DISASM_OBJDUMP;
 			err = symbol__disassemble_objdump(symfs_filename, sym, args);
tools/perf/util/libasm.c new file +184 -0

184 lines not on this page · raw message

tools/perf/util/libasm.h new file +27 -0
new file mode 100644
index 000000000000..d4324edf059e
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __PERF_LIBASM_H
+#define __PERF_LIBASM_H
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <linux/compiler.h>
+#include <linux/types.h>
+
+struct annotate_args;
+struct symbol;
+
+#ifdef HAVE_LIBASM_SUPPORT
+int symbol__disassemble_libasm(const char *filename, struct symbol *sym,
+				 struct annotate_args *args);
+#else /* !HAVE_LIBASM_SUPPORT */
+static inline int symbol__disassemble_libasm(const char *filename __maybe_unused,
+					       struct symbol *sym __maybe_unused,
+					       struct annotate_args *args __maybe_unused)
+{
+	return -1;
+}
+#endif /* HAVE_LIBASM_SUPPORT */
+
+#endif /* __PERF_LIBASM_H */
Signature · 1 line
2.54.0.1064.gd145956f57-goog
author reviewer
#6 lore raw

PATCH v2 4/7 perf annotate: Add --disassembler command-line option

In reply to Ian Rogers

Add the --disassembler command-line option to perf annotate. This allows users to explicitly request or override the disassembler preferences (e.g. --disassembler libasm) directly on the command line.

Assisted-by Antigravity:Google Gemini 3.5-flash
---
 tools/perf/builtin-annotate.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
tools/perf/builtin-annotate.c +10 -0
index 8a0eb30eac24..57a88adfb130 100644
@@ -699,6 +699,7 @@ int cmd_annotate(int argc, const char **argv)
 		.set = 0,
 	};
 	const char *disassembler_style = NULL, *objdump_path = NULL, *addr2line_path = NULL;
+	const char *disassemblers_str = NULL;
 	struct option options[] = {
 	OPT_STRING('i', "input", &input_name, "file",
 		    "input file name"),
@@ -782,6 +783,8 @@ int cmd_annotate(int argc, const char **argv)
 		    "Do not display empty (or dummy) events in the output"),
 	OPT_BOOLEAN(0, "code-with-type", &annotate_opts.code_with_type,
 		    "Show data type info in code annotation (memory instructions only)"),
+	OPT_STRING(0, "disassembler", &disassemblers_str, "names",
+		   "comma separated list of disassemblers to use"),
 	OPT_END()
 	};
 	int ret;
@@ -825,6 +828,13 @@ int cmd_annotate(int argc, const char **argv)
 			return -ENOMEM;
 	}
 
+	if (disassemblers_str) {
+		memset(annotate_opts.disassemblers, 0, sizeof(annotate_opts.disassemblers));
+		ret = annotation_options__add_disassemblers_str(&annotate_opts, disassemblers_str);
+		if (ret)
+			return -EINVAL;
+	}
+
 	if (annotate_check_args() < 0)
 		return -EINVAL;
 
Signature · 1 line
2.54.0.1064.gd145956f57-goog
author reviewer
#7 lore raw

PATCH v2 5/7 perf test: Enhance annotate test coverage and isolate config

In reply to Ian Rogers

Update the annotate.sh shell test to test different disassembler backends (objdump, llvm, capstone, libasm) utilizing the new --disassembler command-line option.
Isolate the test script from host environment pollution by exporting PERF_CONFIG=/dev/null at the start of the script, ensuring it runs hermetically.

Assisted-by Antigravity:Google Gemini 3.5-flash
---
 tools/perf/tests/shell/annotate.sh | 54 ++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
tools/perf/tests/shell/annotate.sh +54 -0
index 689de58e9238..2c06d2670cb1 100755
@@ -4,6 +4,8 @@
 
 set -e
 
+export PERF_CONFIG=/dev/null
+
 shelldir=$(dirname "$0")
 
 # shellcheck source=lib/perf_has_symbol.sh
@@ -106,8 +108,60 @@ test_basic() {
   echo "${mode} annotate test [Success]"
 }
 
+test_disassembler() {
+  disassembler=$1
+  feature=$2
+
+  if [ -n "${feature}" ]
+  then
+    if ! perf check feature "${feature}" > /dev/null 2>&1
+    then
+      echo "Skip test for ${disassembler} (feature ${feature} not supported)"
+      return 0
+    fi
+  fi
+
+  echo "Test annotate with disassembler: ${disassembler}"
+
+  perf annotate --no-demangle -i "${perfdata}" --stdio --percent-limit 10 --disassembler "${disassembler}" 2> /dev/null > "${perfout}" || ret=$?
+
+  if [ "x${ret}" != "x0" ]
+  then
+    echo "annotate with ${disassembler} [Failed: perf annotate error]"
+    err=1
+    return 0
+  fi
+
+  # check if it has the target symbol
+  if ! grep -q "${testsym}" "${perfout}"
+  then
+    echo "annotate with ${disassembler} [Failed: missing target symbol]"
+    err=1
+    return 0
+  fi
+
+  # check if it has the disassembly lines
+  if ! grep -q "${disasm_regex}" "${perfout}"
+  then
+    echo "annotate with ${disassembler} [Failed: missing disasm output]"
+    err=1
+    return 0
+  fi
+
+  echo "annotate with ${disassembler} [Success]"
+  return 0
+}
+
 test_basic Basic
 test_basic Pipe
 
+if [ "${err}" -eq 0 ]
+then
+  test_disassembler "objdump" ""
+  test_disassembler "llvm" "libLLVM"
+  test_disassembler "capstone" "libcapstone"
+  test_disassembler "libasm" "libasm"
+fi
+
 cleanup
 exit $err
Signature · 1 line
2.54.0.1064.gd145956f57-goog
#8 lore raw

Re: [PATCH v2 5/7] perf test: Enhance annotate test coverage and isolate config

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Uninitialized variable and incompatible data format causes `test_disassembler` to…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Uninitialized variable and incompatible data format causes test_disassembler to unconditionally fail. --

commit 5f0bdfa4980785631dd9a8024ff9f43baa234caa
Author: Ian Rogers <[email protected]>

perf test: Enhance annotate test coverage and isolate config

This commit updates the annotate.sh test to support different disassembler backends via the new --disassembler command-line option. It also isolates the test script from host environment pollution by exporting PERF_CONFIG=/dev/null.

Quoted 4 lines
tools/perf/tests/shell/annotate.sh +0 -0
index 689de58e92384..2c06d2670cb18 100755

[ ... ]

Quoted 3 lines
tools/perf/tests/shell/annotate.sh +3 -0
+test_disassembler() {
+  disassembler=$1
+  feature=$2

[ ... ]

tools/perf/tests/shell/annotate.sh test_basic() { · line 133

Quoted 8 lines
tools/perf/tests/shell/annotate.sh +8 -0
+  perf annotate --no-demangle -i "${perfdata}" --stdio --percent-limit 10 --disassembler "${disassembler}" 2> /dev/null > "${perfout}" || ret=$?
+
+  if [ "x${ret}" != "x0" ]
+  then
+    echo "annotate with ${disassembler} [Failed: perf annotate error]"
+    err=1
+    return 0
+  fi

[Severity: Medium]
Does this evaluation unconditionally fail the test when perf annotate succeeds?

If the perf annotate command is successful, the short-circuit || ret=$? means ret is skipped and never assigned. Because ret is not initialized as a local variable at the start of test_disassembler(), it remains empty. The subsequent check then evaluates to [ "x" != "x0" ], which is always true and incorrectly flags a success as a failure.

Additionally, will reading ${perfdata} as a regular file here cause an issue due to the test execution order?

tools/perf/tests/shell/annotate.sh test_basic() { · line 160

Quoted 6 lines

test_basic Basic
test_basic Pipe

tools/perf/tests/shell/annotate.sh +3 -0
+if [ "${err}" -eq 0 ]
+then
+  test_disassembler "objdump" ""

The test_basic sequence runs test_basic Pipe immediately before executing test_disassembler(). This overwrites ${perfdata} with a pipe-format stream, so attempting to read it using -i "${perfdata}" inside test_disassembler() may cause perf annotate execution failures.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
author reviewer
#9 lore raw

PATCH v2 6/7 perf annotate: Support BPF JIT disassembly via genelf

In reply to Ian Rogers

For in-memory BPF DSOs (DSO_BINARY_TYPE__BPF_PROG_INFO), write the JITted instruction buffer to a temporary ELF file on disk using the existing genelf framework (jit_write_elf). Reroute disassembly to this temporary ELF file, allowing objdump and libasm to disassemble it natively. Clean up the temporary file afterward.

Assisted-by Antigravity:Google Gemini 3.5-flash
---
 tools/perf/tests/genelf.c |   2 +-
 tools/perf/util/disasm.c  | 111 +++++++++++++++++++++++++++++++++++---
 tools/perf/util/disasm.h  |   1 +
 tools/perf/util/genelf.c  |   4 +-
 tools/perf/util/genelf.h  |   2 +-
 tools/perf/util/jitdump.c |   2 +-
 6 files changed, 110 insertions(+), 12 deletions(-)
tools/perf/tests/genelf.c +1 -1
index 95f3be1b683a..1723370db32e 100644
@@ -38,7 +38,7 @@ static int test__jit_write_elf(struct test_suite *test __maybe_unused,
 
 	pr_info("Writing jit code to: %s\n", path);
 
-	ret = jit_write_elf(fd, 0, "main", x86_code, sizeof(x86_code),
+	ret = jit_write_elf(fd, GEN_ELF_ARCH, 0, "main", x86_code, sizeof(x86_code),
 			NULL, 0, NULL, 0, 0);
 	close(fd);
 
tools/perf/util/disasm.c +104 -7

167 lines not on this page · raw message

tools/perf/util/disasm.h +1 -0
index 25756e3f47e4..32a5b3f5d1c6 100644
@@ -106,6 +106,7 @@ struct annotate_args {
 	char			  *line;
 	int			  line_nr;
 	char			  *fileloc;
+	bool			  is_temp_elf;
 };
 
 const struct arch *arch__find(uint16_t e_machine, uint32_t e_flags, const char *cpuid);
tools/perf/util/genelf.c +2 -2
index 14882def9704..42731b6de797 100644
@@ -179,7 +179,7 @@ static void blake2s_update_tagged(struct blake2s_ctx *ctx, int tag,
  * csize: the code size in bytes
  */
 int
-jit_write_elf(int fd, uint64_t load_addr __maybe_unused, const char *sym,
+jit_write_elf(int fd, uint16_t e_machine, uint64_t load_addr __maybe_unused, const char *sym,
 	      const void *code, int csize,
 	      void *debug __maybe_unused, int nr_debug_entries __maybe_unused,
 	      void *unwinding, uint64_t unwinding_header_size, uint64_t unwinding_size)
@@ -218,7 +218,7 @@ jit_write_elf(int fd, uint64_t load_addr __maybe_unused, const char *sym,
 
 	ehdr->e_ident[EI_DATA] = GEN_ELF_ENDIAN;
 	ehdr->e_ident[EI_CLASS] = GEN_ELF_CLASS;
-	ehdr->e_machine = GEN_ELF_ARCH;
+	ehdr->e_machine = e_machine;
 	ehdr->e_type = ET_DYN;
 	ehdr->e_entry = GEN_ELF_TEXT_OFFSET;
 	ehdr->e_version = EV_CURRENT;
tools/perf/util/genelf.h +1 -1
index 9f0b875d6548..992ca1eb2734 100644
@@ -5,7 +5,7 @@
 #include <linux/math.h>
 
 /* genelf.c */
-int jit_write_elf(int fd, uint64_t code_addr, const char *sym,
+int jit_write_elf(int fd, uint16_t e_machine, uint64_t code_addr, const char *sym,
 		  const void *code, int csize, void *debug, int nr_debug_entries,
 		  void *unwinding, uint64_t unwinding_header_size, uint64_t unwinding_size);
 #ifdef HAVE_LIBDW_SUPPORT
tools/perf/util/jitdump.c +1 -1
index 18fd84a82153..febf3e5a7851 100644
@@ -95,7 +95,7 @@ jit_emit_elf(struct jit_buf_desc *jd,
 		return -1;
 	}
 
-	ret = jit_write_elf(fd, code_addr, sym, (const void *)code, csize, debug, nr_debug_entries,
+	ret = jit_write_elf(fd, GEN_ELF_ARCH, code_addr, sym, (const void *)code, csize, debug, nr_debug_entries,
 			    unwinding, unwinding_header_size, unwinding_size);
 
         close(fd);
Signature · 1 line
2.54.0.1064.gd145956f57-goog
#10 lore raw

Re: [PATCH v2 6/7] perf annotate: Support BPF JIT disassembly via genelf

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Objdump fails to disassemble the temporary BPF ELF file because of an address range mismatch…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Objdump fails to disassemble the temporary BPF ELF file because of an address range mismatch between the ELF file's hardcoded text section and objdump's --start-address boundary. --

commit 515b53823b20255d2b911e2440f76dd5f76eaf39
Author: Ian Rogers <[email protected]>

perf annotate: Support BPF JIT disassembly via genelf

For in-memory BPF DSOs (DSO_BINARY_TYPE__BPF_PROG_INFO), write the JITted instruction buffer to a temporary ELF file on disk using the existing genelf framework (jit_write_elf). Reroute disassembly to this temporary ELF file, allowing objdump and libasm to disassemble it natively. Clean up the temporary file afterward.

Quoted 4 lines
tools/perf/util/disasm.c +0 -0
index 42af3603fdffd..0648afd1b5f31 100644

[ ... ]

Quoted 7 lines
tools/perf/util/disasm.c +7 -0
+#ifdef HAVE_LIBELF_SUPPORT
+#include <unistd.h>
+#include "dwarf-regs.h"
+static int symbol__create_bpf_temp_elf(const char *filename, struct symbol *sym,
+				       struct annotate_args *args,
+				       char *tmp_elf, size_t tmp_elf_sz)
+{

[ ... ]

tools/perf/util/disasm.c static int symbol__disassemble_objdump(const char *filename, struct symbol *sym, · line 1573

Quoted 6 lines
tools/perf/util/disasm.c +6 -0
+	if (tmp_fd >= 0) {
+		uint16_t e_machine = args->arch ? args->arch->id.e_machine : EM_HOST;
+
+		if (jit_write_elf(tmp_fd, e_machine, map__rip_2objdump(map, sym->start),
+				  sym->name, buf, buf_len,
+				  NULL, 0, NULL, 0, 0) == 0) {

[Severity: Medium]
Will this cause objdump to misalign or truncate the disassembly?

When symbol__create_bpf_temp_elf() calls jit_write_elf(), it passes map__rip_2objdump(map, sym->start) as the load_addr argument, but jit_write_elf() explicitly ignores this argument:

tools/perf/util/genelf.c:jit_write_elf() {
    ...
    int
    jit_write_elf(int fd, uint16_t e_machine, uint64_t load_addr __maybe_unused, const char *sym,
    ...
}

Instead, jit_write_elf() hardcodes the .text section VMA to GEN_ELF_TEXT_OFFSET (typically 128 bytes).

Later, when symbol__disassemble_objdump() invokes objdump, it uses --start-address and --stop-address derived from the symbol's start and end addresses:

tools/perf/util/disasm.c:symbol__disassemble_objdump() {
    ...
	err = asprintf(&command,
		 "%s %s%s --start-address=0x%016" PRIx64
		 " --stop-address=0x%016" PRIx64
    ...
}

For BPF DSOs, these addresses typically map to the range [0, csize]. However, the actual instructions inside the generated ELF are placed at [GEN_ELF_TEXT_OFFSET, GEN_ELF_TEXT_OFFSET + csize].

If the BPF program is smaller than GEN_ELF_TEXT_OFFSET, does objdump end up outputting nothing? If it is larger, could this result in objdump reading misaligned and truncated instructions that no longer match the performance samples?

As a side note, it appears that any test added later in the series might silently pass if it only blindly greps for offsets in objdump's output, which would mask this offset and truncation corruption.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
author reviewer
#11 lore raw

PATCH v2 7/7 perf test: Add BPF JIT annotation test coverage for all disassemblers

In reply to Ian Rogers

Expand the annotate.sh shell test to verify BPF JIT disassembly. If the test is run with sufficient privileges (root/CAP_BPF) and captures system BPF programs, it will extract a JITted BPF program symbol from the perf report and run perf annotate on it. This validates the temporary ELF generation and disassembles it using each of the supported disassembler backends (objdump, llvm, capstone, and libasm).

Assisted-by Antigravity:Google Gemini 3.5-flash
---
 tools/perf/tests/shell/annotate.sh | 79 ++++++++++++++++++++++++++++++
 1 file changed, 79 insertions(+)
tools/perf/tests/shell/annotate.sh +79 -0
index 2c06d2670cb1..9719e592befd 100755
@@ -163,5 +163,84 @@ then
   test_disassembler "libasm" "libasm"
 fi
 
+test_bpf_disassembler() {
+  disassembler=$1
+  feature=$2
+
+  if [ -n "${feature}" ]
+  then
+    if ! perf check feature "${feature}" > /dev/null 2>&1
+    then
+      echo "Skip BPF JIT test for ${disassembler} (feature ${feature} not supported)"
+      return 0
+    fi
+  fi
+
+  echo "Test BPF JIT annotate with disassembler: ${disassembler}"
+
+  if ! perf annotate --no-demangle -i "${perfdata}" --stdio "${bpf_sym}" \
+      --disassembler "${disassembler}" 2> /dev/null > "${perfout}"
+  then
+    echo "BPF JIT annotate with ${disassembler} [Failed: perf annotate error]"
+    err=1
+    return 0
+  fi
+
+  if ! grep -q "${disasm_regex}" "${perfout}"
+  then
+    echo "BPF JIT annotate with ${disassembler} [Failed: missing disasm output]"
+    err=1
+    return 0
+  fi
+
+  echo "BPF JIT annotate with ${disassembler} [Success]"
+  return 0
+}
+
+test_bpf() {
+  echo "Test annotate with BPF JIT output"
+
+  if ! perf check -q feature libbpf-strings ; then
+    echo "BPF annotation test [Skipped - libbpf-strings not supported]"
+    return 0
+  fi
+
+  bpftmp=$(mktemp -d /tmp/__perf_test.bpf.XXXXXX)
+  bpf_perfdata="${bpftmp}/perf.data"
+
+  if ! perf record -a -e cycles -F 4000 -o "${bpf_perfdata}" -- sleep 1 2> /dev/null
+  then
+    echo "BPF annotation test [Skipped - perf record -a failed, probably no privileges]"
+    rm -rf "${bpftmp}"
+    return 0
+  fi
+
+  bpf_sym=$(perf report --stdio -i "${bpf_perfdata}" 2>/dev/null | \
+      grep -E -o 'bpf_prog_[0-9a-f]{16}_[0-9A-Za-z_]*' | head -1)
+
+  if [ -z "${bpf_sym}" ]; then
+    echo "BPF annotation test [Skipped - no JITted BPF symbols with samples found]"
+    rm -rf "${bpftmp}"
+    return 0
+  fi
+
+  # temporarily override perfdata so test_bpf_disassembler uses the right file
+  local old_perfdata="${perfdata}"
+  perfdata="${bpf_perfdata}"
+
+  test_bpf_disassembler "objdump" ""
+  test_bpf_disassembler "llvm" "libLLVM"
+  test_bpf_disassembler "capstone" "libcapstone"
+  test_bpf_disassembler "libasm" "libasm"
+
+  perfdata="${old_perfdata}"
+  rm -rf "${bpftmp}"
+}
+
+if [ "${err}" -eq 0 ]
+then
+  test_bpf
+fi
+
 cleanup
 exit $err
Signature · 1 line
2.54.0.1064.gd145956f57-goog
#12 lore raw

Re: [PATCH v2 7/7] perf test: Add BPF JIT annotation test coverage for all disassemblers

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] System-wide profiling of ambient BPF programs creates a race condition that can randomly fail…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] System-wide profiling of ambient BPF programs creates a race condition that can randomly fail the test.
- [Low] Signal interruption leaks the original perfdata file and the bpftmp directory due to temporary variable override. --

commit 2a2752d1eb68187d4c05ebd5a52ce3cce359db69
Author: Ian Rogers <[email protected]>
Subject: perf test: Add BPF JIT annotation test coverage for all disassemblers

This commit expands the annotate.sh shell test to verify BPF JIT disassembly using supported backends. It attempts to capture system BPF programs using perf record, extracts a JITted BPF program symbol, and runs perf annotate on it to validate temporary ELF generation.

Quoted 4 lines
tools/perf/tests/shell/annotate.sh +0 -0
index 2c06d2670cb18..9719e592befd2 100755

[ ... ]

tools/perf/tests/shell/annotate.sh then · line 211

Quoted 12 lines
tools/perf/tests/shell/annotate.sh +12 -0
+test_bpf() {
+  echo "Test annotate with BPF JIT output"
+
+  if ! perf check -q feature libbpf-strings ; then
+    echo "BPF annotation test [Skipped - libbpf-strings not supported]"
+    return 0
+  fi
+
+  bpftmp=$(mktemp -d /tmp/__perf_test.bpf.XXXXXX)
+  bpf_perfdata="${bpftmp}/perf.data"
+
+  if ! perf record -a -e cycles -F 4000 -o "${bpf_perfdata}" -- sleep 1 2> /dev/null

[Severity: Medium]
Will this probabilistic test setup cause unpredictable failures or silent skips?

By using perf record -a -- sleep 1 to capture ambient BPF programs, a race condition occurs. If a short-lived BPF program unloads between the time the kernel emits the KSYMBOL/BPF_EVENT and when perf record's sideband thread calls bpf_prog_get_fd_by_id() to fetch the instructions, the JITed code won't be saved in the perf.data file.

Later, perf report will list the BPF symbol, but perf annotate will fail to disassemble it, causing a false test failure. Furthermore, if no ambient BPF programs are active, the test silently skips, providing zero coverage in most environments.

Quoted 5 lines
tools/perf/tests/shell/annotate.sh +5 -0
+  then
+    echo "BPF annotation test [Skipped - perf record -a failed, probably no privileges]"
+    rm -rf "${bpftmp}"
+    return 0
+  fi

[ ... ]

tools/perf/tests/shell/annotate.sh then · line 236

Quoted 10 lines
tools/perf/tests/shell/annotate.sh +10 -0
+  # temporarily override perfdata so test_bpf_disassembler uses the right file
+  local old_perfdata="${perfdata}"
+  perfdata="${bpf_perfdata}"
+
+  test_bpf_disassembler "objdump" ""
+  test_bpf_disassembler "llvm" "libLLVM"
+  test_bpf_disassembler "capstone" "libcapstone"
+  test_bpf_disassembler "libasm" "libasm"
+
+  perfdata="${old_perfdata}"

[Severity: Low]
Is it possible for the original perfdata and bpftmp directory to be leaked if the script is interrupted during this section?

The script uses a signal trap to run the cleanup() handler, which relies on the global perfdata variable:

cleanup() {
rm -rf "${perfdata}" "${perfout}"
...

If a signal like SIGINT occurs while perfdata is temporarily overridden, cleanup() will only remove the inner bpf_perfdata and fail to delete the original global perfdata file. Additionally, the new bpftmp directory is never registered with the cleanup routine and will be left stranded in /tmp.

Quoted 2 lines
tools/perf/tests/shell/annotate.sh +2 -0
+  rm -rf "${bpftmp}"
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
author reviewer
#13 lore raw

v3 cover perf annotate: Add elfutils libasm disassembler and BPF JIT disassembly support

In reply to Ian Rogers

This patch series introduces support for using the elfutils libasm library as a disassembler backend in perf annotate. This provides a lightweight, library-based alternative to LLVM and Capstone for in-process disassembly.

Additionally, this series implements disassembly support for in-memory BPF JITted programs by writing the JITted instruction buffer to a temporary ELF file on the fly and disassembling it using the ELF-based backends (objdump and libasm).

Changes in v3:
- Address review comments on v2 series.
- Explicitly link against -lasm -ldw -lelf in the feature check.
- Document that older versions of elfutils that require explicitly linking -lebl statically are unsupported.
- Fix instruction-by-instruction disassembly loop logic in libasm.c by returning 1 from output callback and checking for ret != 1.
- Pass the correct ELF machine (e_machine) to jit_write_elf instead of hardcoding host architecture.
- Update annotate.sh tests to use perf test -w workload instead of free to avoid CI environment issues.
- Expand test coverage to test all disassemblers (including libasm) on BPF JIT symbols when run with root privileges.

Ian Rogers (7):
tools build: Add feature check for elfutils libasm
perf build: Add build support and capability for elfutils libasm perf annotate: Implement elfutils libasm disassembler backend perf annotate: Add --disassembler command-line option
perf test: Enhance annotate test coverage and isolate config perf annotate: Support BPF JIT disassembly via genelf
perf test: Add BPF JIT annotation test coverage for all disassemblers

 tools/build/Makefile.feature       |   2 +
 tools/build/feature/Makefile       |   9 ++
 tools/build/feature/test-libasm.c  |  20 ++++
 tools/perf/Makefile.config         |  22 ++++
 tools/perf/builtin-annotate.c      |  10 ++
 tools/perf/builtin-check.c         |   1 +
 tools/perf/tests/genelf.c          |   2 +-
 tools/perf/tests/shell/annotate.sh | 153 ++++++++++++++++++++++++
 tools/perf/util/Build              |   1 +
 tools/perf/util/annotate.c         |   8 +-
 tools/perf/util/annotate.h         |   3 +
 tools/perf/util/disasm.c           | 114 +++++++++++++++++-
 tools/perf/util/disasm.h           |   1 +
 tools/perf/util/genelf.c           |  16 +--
 tools/perf/util/genelf.h           |   2 +-
 tools/perf/util/jitdump.c          |   3 +-
 tools/perf/util/libasm.c           | 184 +++++++++++++++++++++++++++++
 tools/perf/util/libasm.h           |  27 +++++
 18 files changed, 559 insertions(+), 19 deletions(-)
 create mode 100644 tools/build/feature/test-libasm.c
 create mode 100644 tools/perf/util/libasm.c
 create mode 100644 tools/perf/util/libasm.h
Signature · 1 line
2.54.0.1099.g489fc7bff1-goog
author reviewer
#14 lore raw

PATCH v3 1/7 tools build: Add feature check for elfutils libasm

This commit introduces a build-time feature test for the elfutils libasm library in the shared tools/build system. It adds the test program and updates the Makefiles to attempt building it to detect support.

Note on static linking: pkg-config for libdw/libelf on modern systems does not list -lebl as a dependency. Older versions of elfutils that require explicitly linking -lebl statically are unsupported by this feature check.

Assisted-by Antigravity:Google Gemini 3.5-flash
---
 tools/build/Makefile.feature      |  2 ++
 tools/build/feature/Makefile      |  9 +++++++++
 tools/build/feature/test-libasm.c | 20 ++++++++++++++++++++
 3 files changed, 31 insertions(+)
 create mode 100644 tools/build/feature/test-libasm.c
tools/build/Makefile.feature +2 -0
index ed1374af31c1..422329dbc20a 100644
@@ -118,6 +118,7 @@ FEATURE_TESTS_EXTRA :=                  \
          hello                          \
          babeltrace2-ctf-writer         \
          libcapstone                    \
+         libasm                         \
          libcheck                       \
          libbfd-liberty                 \
          libbfd-liberty-z               \
@@ -150,6 +151,7 @@ FEATURE_DISPLAY ?=              \
          numa_num_possible_cpus \
          libpython              \
          libcapstone            \
+         libasm                 \
          llvm-perf              \
          zlib                   \
          lzma                   \
tools/build/feature/Makefile +9 -0
index 62909a9c799d..f0f39a2ab203 100644
@@ -47,6 +47,7 @@ FILES=                                          \
          test-timerfd.bin                       \
          test-babeltrace2-ctf-writer.bin        \
          test-libcapstone.bin			\
+         test-libasm.bin			\
          test-libcheck.bin			\
          test-compile-32.bin                    \
          test-compile-x32.bin                   \
@@ -184,6 +185,11 @@ ifeq ($(findstring -static,${LDFLAGS}),-static)
   DWARFLIBS += -ldl
 endif
 
+ASMLIBS := -lasm -ldw -lelf
+ifeq ($(findstring -static,${LDFLAGS}),-static)
+  ASMLIBS += -lz -llzma -lbz2 -lzstd
+endif
+
 $(OUTPUT)test-libdw.bin:
 	$(BUILD) $(DWLIBS)
 
@@ -311,6 +317,9 @@ $(OUTPUT)test-babeltrace2-ctf-writer.bin:
 $(OUTPUT)test-libcapstone.bin:
 	$(BUILD) # -lcapstone provided by $(FEATURE_CHECK_LDFLAGS-libcapstone)
 
+$(OUTPUT)test-libasm.bin:
+	$(BUILD) $(ASMLIBS)
+
 $(OUTPUT)test-libcheck.bin:
 	$(BUILD) # -lcheck is provided by $(FEATURE_CHECK_LDFLAGS-libcheck)
 
tools/build/feature/test-libasm.c new file +20 -0
new file mode 100644
index 000000000000..928fc46ecc75
@@ -0,0 +1,20 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <stddef.h>
+#include <elfutils/libasm.h>
+#include <gelf.h>
+
+struct ebl;
+extern struct ebl *ebl_openbackend(Elf *elf);
+extern void ebl_closebackend(struct ebl *ebl);
+
+int main(void)
+{
+	Elf *elf = elf_begin(0, ELF_C_READ, NULL);
+	struct ebl *ebl = ebl_openbackend(elf);
+	DisasmCtx_t *ctx = disasm_begin(ebl, elf, NULL);
+
+	disasm_end(ctx);
+	ebl_closebackend(ebl);
+	elf_end(elf);
+	return 0;
+}
Signature · 1 line
2.54.0.1099.g489fc7bff1-goog
#15 lore raw

Re: [PATCH v3 1/7] tools build: Add feature check for elfutils libasm

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] ASMLIBS is missing the -ldl dependency for static builds. - [Medium] Passing NULL to an…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] ASMLIBS is missing the -ldl dependency for static builds.
- [Medium] Passing NULL to an integer/boolean argument in disasm_begin triggers implicit pointer-to-integer conversion errors. --

commit 9ca40464008d6c950047c60bdb1b0966c47a038d
Author: Ian Rogers <[email protected]>

tools build: Add feature check for elfutils libasm

This commit introduces a build-time feature test for the elfutils libasm library in the shared tools/build system. It adds the test program and updates the Makefiles to attempt building it to detect support.

Quoted 4 lines
tools/build/feature/Makefile +0 -0
index 62909a9c799d3..f0f39a2ab2035 100644

[ ... ]

tools/build/feature/Makefile ifeq ($(findstring -static,${LDFLAGS}),-static) · line 191

Quoted 8 lines
tools/build/feature/Makefile +4 -0
@@ -184,6 +185,11 @@ ifeq ($(findstring -static,${LDFLAGS}),-static)
   DWARFLIBS += -ldl
 endif
 
+ASMLIBS := -lasm -ldw -lelf
+ifeq ($(findstring -static,${LDFLAGS}),-static)
+  ASMLIBS += -lz -llzma -lbz2 -lzstd
+endif

[Severity: Medium]
Will this fail to link during static builds? Since libdw dynamically loads backend modules via dlopen, it requires libdl. DWARFLIBS above correctly adds -ldl for static builds, but ASMLIBS omits it, which might cause the feature test to fail and spuriously report libasm as unsupported.

Quoted 4 lines
tools/build/feature/Makefile +1 -0
+
 $(OUTPUT)test-libdw.bin:
 	$(BUILD) $(DWLIBS)
 

[ ... ]

Quoted 5 lines
tools/build/feature/test-libasm.c new file +0 -0
new file mode 100644
index 0000000000000..928fc46ecc755

[ ... ]

Quoted 5 lines
tools/build/feature/test-libasm.c new file +4 -0
@@ -0,0 +1,20 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <stddef.h>
+#include <elfutils/libasm.h>
+#include <gelf.h>

tools/build/feature/test-libasm.c line 14

Quoted 10 lines
tools/build/feature/test-libasm.c new file +10 -0
+
+struct ebl;
+extern struct ebl *ebl_openbackend(Elf *elf);
+extern void ebl_closebackend(struct ebl *ebl);
+
+int main(void)
+{
+	Elf *elf = elf_begin(0, ELF_C_READ, NULL);
+	struct ebl *ebl = ebl_openbackend(elf);
+	DisasmCtx_t *ctx = disasm_begin(ebl, elf, NULL);

[Severity: Medium]
Will this fail to build on modern compilers? The third argument to disasm_begin() is typically a boolean or integer type (like show_bytes or fd), but NULL is passed here. This causes a -Wint-conversion warning, which compilers like GCC 14 elevate to a hard error by default. This could cause the feature test to unconditionally fail.

Quoted 6 lines
tools/build/feature/test-libasm.c new file +6 -0
+
+	disasm_end(ctx);
+	ebl_closebackend(ebl);
+	elf_end(elf);
+	return 0;
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author reviewer
#16 lore raw

PATCH v3 2/7 perf build: Add build support and capability for elfutils libasm

In reply to Ian Rogers

Configure perf build system to utilize the libasm feature check (supporting both static and dynamic builds) and export the config flags. Add "libasm" capability detection to perf check.

Assisted-by Antigravity:Google Gemini 3.5-flash
---
 tools/perf/Makefile.config | 22 ++++++++++++++++++++++
 tools/perf/builtin-check.c |  1 +
 2 files changed, 23 insertions(+)
tools/perf/Makefile.config +22 -0
index 6e7b15fab2ec..632cb069c944 100644
@@ -199,6 +199,13 @@ endif
 FEATURE_CHECK_CFLAGS-libcapstone := $(LIBCAPSTONE_CFLAGS)
 FEATURE_CHECK_LDFLAGS-libcapstone := $(LIBCAPSTONE_LDFLAGS) -lcapstone
 
+ifdef LIBASM_DIR
+  LIBASM_CFLAGS  := -I$(LIBASM_DIR)/include
+  LIBASM_LDFLAGS := -L$(LIBASM_DIR)/lib
+endif
+FEATURE_CHECK_CFLAGS-libasm := $(LIBASM_CFLAGS)
+FEATURE_CHECK_LDFLAGS-libasm := $(LIBASM_LDFLAGS) -lasm -ldw -lelf
+
 ifdef LIBZSTD_DIR
   LIBZSTD_CFLAGS  := -I$(LIBZSTD_DIR)/lib
   LIBZSTD_LDFLAGS := -L$(LIBZSTD_DIR)/lib
@@ -432,6 +439,7 @@ ifeq ($(feature-file-handle), 1)
 endif
 
 ifdef NO_LIBELF
+  NO_LIBASM := 1
   NO_LIBDW := 1
   NO_LIBUNWIND := 1
   NO_LIBBPF := 1
@@ -1060,6 +1068,20 @@ ifndef NO_CAPSTONE
   endif
 endif
 
+ifdef NO_LIBDW
+  NO_LIBASM := 1
+endif
+
+ifndef NO_LIBASM
+  $(call feature_check,libasm)
+  ifeq ($(feature-libasm), 1)
+    CFLAGS += -DHAVE_LIBASM_SUPPORT $(LIBASM_CFLAGS)
+    LDFLAGS += $(LIBASM_LDFLAGS)
+    EXTLIBS += -lasm -ldw -lelf
+    $(call detected,CONFIG_LIBASM)
+  endif
+endif
+
 ifdef EXTRA_TESTS
     $(call detected,CONFIG_EXTRA_TESTS)
     CFLAGS += -DHAVE_EXTRA_TESTS
tools/perf/builtin-check.c +1 -0
index 60437650c50f..d2ffea4d9b8b 100644
@@ -46,6 +46,7 @@ struct feature_status supported_features[] = {
 	FEATURE_STATUS("babeltrace2-ctf-writer", HAVE_BABELTRACE2_CTF_WRITER_SUPPORT),
 	FEATURE_STATUS("libbpf-strings", HAVE_LIBBPF_STRINGS_SUPPORT),
 	FEATURE_STATUS("libcapstone", HAVE_LIBCAPSTONE_SUPPORT),
+	FEATURE_STATUS("libasm", HAVE_LIBASM_SUPPORT),
 	FEATURE_STATUS("libdw-dwarf-unwind", HAVE_LIBDW_SUPPORT),
 	FEATURE_STATUS("libelf", HAVE_LIBELF_SUPPORT),
 	FEATURE_STATUS("libLLVM", HAVE_LIBLLVM_SUPPORT),
Signature · 1 line
2.54.0.1099.g489fc7bff1-goog
author reviewer
#17 lore raw

PATCH v3 3/7 perf annotate: Implement elfutils libasm disassembler backend

In reply to Ian Rogers

Implement the core disassembler backend in libasm.c utilizing elfutils' libasm API. Hook the backend into perf annotate disassembler routing. Configure libasm as the first-choice disassembler backend by default (ahead of LLVM/capstone) when available.
Also, expose annotation_options__add_disassemblers_str to allow custom disassembler selection.

Assisted-by Antigravity:Google Gemini 3.5-flash
---
 tools/perf/util/Build      |   1 +
 tools/perf/util/annotate.c |   8 +-
 tools/perf/util/annotate.h |   3 +
 tools/perf/util/disasm.c   |   5 +
 tools/perf/util/libasm.c   | 184 +++++++++++++++++++++++++++++++++++++
 tools/perf/util/libasm.h   |  27 ++++++
 6 files changed, 226 insertions(+), 2 deletions(-)
 create mode 100644 tools/perf/util/libasm.c
 create mode 100644 tools/perf/util/libasm.h
tools/perf/util/Build +1 -0
index b22cdc24082a..f1d5ebb2edfa 100644
@@ -12,6 +12,7 @@ perf-util-y += block-range.o
 perf-util-y += build-id.o
 perf-util-y += cacheline.o
 perf-util-$(CONFIG_LIBCAPSTONE) += capstone.o
+perf-util-$(CONFIG_LIBASM) += libasm.o
 perf-util-y += config.o
 perf-util-y += copyfile.o
 perf-util-y += ctype.o
tools/perf/util/annotate.c +6 -2
index 02505222d8c2..2259ac2c2cd6 100644
@@ -2233,6 +2233,7 @@ const char * const perf_disassembler__strs[] = {
 	[PERF_DISASM_UNKNOWN]  = "unknown",
 	[PERF_DISASM_LLVM]     = "llvm",
 	[PERF_DISASM_CAPSTONE] = "capstone",
+	[PERF_DISASM_LIBASM]   = "libasm",
 	[PERF_DISASM_OBJDUMP]  = "objdump",
 };
 
@@ -2254,8 +2255,8 @@ static void annotation_options__add_disassembler(struct annotation_options *opti
 	pr_err("Failed to add disassembler %d\n", dis);
 }
 
-static int annotation_options__add_disassemblers_str(struct annotation_options *options,
-						const char *str)
+int annotation_options__add_disassemblers_str(struct annotation_options *options,
+					     const char *str)
 {
 	while (str && *str != '\0') {
 		const char *comma = strchr(str, ',');
@@ -2372,6 +2373,9 @@ static void annotation_options__default_init_disassemblers(struct annotation_opt
 		/* Already initialized. */
 		return;
 	}
+#ifdef HAVE_LIBASM_SUPPORT
+	annotation_options__add_disassembler(options, PERF_DISASM_LIBASM);
+#endif
 #ifdef HAVE_LIBLLVM_SUPPORT
 	annotation_options__add_disassembler(options, PERF_DISASM_LLVM);
 #endif
tools/perf/util/annotate.h +3 -0
index 1aa6df7d1618..21ac2b6472c1 100644
@@ -38,6 +38,7 @@ enum perf_disassembler {
 	PERF_DISASM_UNKNOWN = 0,
 	PERF_DISASM_LLVM,
 	PERF_DISASM_CAPSTONE,
+	PERF_DISASM_LIBASM,
 	PERF_DISASM_OBJDUMP,
 };
 #define MAX_DISASSEMBLERS (PERF_DISASM_OBJDUMP + 1)
@@ -484,6 +485,8 @@ int hist_entry__tty_annotate2(struct hist_entry *he, struct evsel *evsel);
 
 void annotation_options__init(void);
 void annotation_options__exit(void);
+int annotation_options__add_disassemblers_str(struct annotation_options *options,
+					      const char *str);
 
 void annotation_config__init(void);
 
tools/perf/util/disasm.c +5 -0
index 59ba88e1f744..42af3603fdff 100644
@@ -22,6 +22,7 @@
 #include "capstone.h"
 #include "debug.h"
 #include "disasm.h"
+#include "libasm.h"
 #include "dso.h"
 #include "dwarf-regs.h"
 #include "env.h"
@@ -1622,6 +1623,10 @@ int symbol__disassemble(struct symbol *sym, struct annotate_args *args)
 			args->options->disassembler_used = PERF_DISASM_CAPSTONE;
 			err = symbol__disassemble_capstone(symfs_filename, sym, args);
 			break;
+		case PERF_DISASM_LIBASM:
+			args->options->disassembler_used = PERF_DISASM_LIBASM;
+			err = symbol__disassemble_libasm(symfs_filename, sym, args);
+			break;
 		case PERF_DISASM_OBJDUMP:
 			args->options->disassembler_used = PERF_DISASM_OBJDUMP;
 			err = symbol__disassemble_objdump(symfs_filename, sym, args);
tools/perf/util/libasm.c new file +184 -0

184 lines not on this page · raw message

tools/perf/util/libasm.h new file +27 -0
new file mode 100644
index 000000000000..d4324edf059e
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __PERF_LIBASM_H
+#define __PERF_LIBASM_H
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <linux/compiler.h>
+#include <linux/types.h>
+
+struct annotate_args;
+struct symbol;
+
+#ifdef HAVE_LIBASM_SUPPORT
+int symbol__disassemble_libasm(const char *filename, struct symbol *sym,
+				 struct annotate_args *args);
+#else /* !HAVE_LIBASM_SUPPORT */
+static inline int symbol__disassemble_libasm(const char *filename __maybe_unused,
+					       struct symbol *sym __maybe_unused,
+					       struct annotate_args *args __maybe_unused)
+{
+	return -1;
+}
+#endif /* HAVE_LIBASM_SUPPORT */
+
+#endif /* __PERF_LIBASM_H */
Signature · 1 line
2.54.0.1099.g489fc7bff1-goog
#18 lore raw

Re: [PATCH v3 3/7] perf annotate: Implement elfutils libasm disassembler backend

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider: - [High] Missing build system integration leaves the libasm backend as completely dead code. - [High] The…

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [High] Missing build system integration leaves the libasm backend as completely dead code.
- [High] The implementation incorrectly relies on internal, unexported elfutils functions (ebl_openbackend and ebl_closebackend), causing linkage failures.
- [High] Incorrect return value check for disasm_cb causes immediate loop failure.
- [High] Passing a callback with an incompatible pointer type to disasm_cb causes a compilation error.
- [Low] annotation_options__add_disassemblers_str is exposed in the API but never hooked up to command line options.
--

commit 10af4235f6a3a3ed8b8874f806432a73221075b5
Author: Ian Rogers <[email protected]>

perf annotate: Implement elfutils libasm disassembler backend

This commit implements a new core disassembler backend utilizing elfutils' libasm API. It hooks the backend into perf annotate routing, sets it as the default choice when available, and exposes a function to allow custom disassembler selection.

tools/perf/util/Build perf-util-y += block-range.o · line 15

Quoted 9 lines
tools/perf/util/Build +1 -0
index b22cdc24082a5..f1d5ebb2edfa8 100644
@@ -12,6 +12,7 @@ perf-util-y += block-range.o
 perf-util-y += build-id.o
 perf-util-y += cacheline.o
 perf-util-$(CONFIG_LIBCAPSTONE) += capstone.o
+perf-util-$(CONFIG_LIBASM) += libasm.o

[Severity: High]
Does this code require corresponding changes in the build system to actually enable CONFIG_LIBASM and HAVE_LIBASM_SUPPORT?

Without feature detection in tools/build/feature/test-libasm.c and Makefile.config, it seems libasm.o won't be built and the fallback inline stub in libasm.h will be used permanently.

[ ... ]

Quoted 4 lines
tools/perf/util/annotate.c +0 -0
index 02505222d8c2d..2259ac2c2cd6c 100644

[ ... ]

tools/perf/util/annotate.c static void annotation_options__add_disassembler(struct annotation_options *opti · line 2262

Quoted 11 lines
tools/perf/util/annotate.c +2 -2
@@ -2254,8 +2255,8 @@ static void annotation_options__add_disassembler(struct annotation_options *opti
 	pr_err("Failed to add disassembler %d\n", dis);
 }
 
-static int annotation_options__add_disassemblers_str(struct annotation_options *options,
-						const char *str)
+int annotation_options__add_disassemblers_str(struct annotation_options *options,
+					     const char *str)
 {
 	while (str && *str != '\0') {
 		const char *comma = strchr(str, ',');

[Severity: Low]
Is this function ever hooked up to command line options like in cmd_annotate()?

The commit message mentions exposing it to allow custom disassembler selection, but without binding it to arguments, it appears to remain uncalled.

[ ... ]

Quoted 8 lines
tools/perf/util/libasm.c new file +2 -0
new file mode 100644
index 0000000000000..2f0a657335223
@@ -0,0 +1,184 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "libasm.h"

[ ... ]

tools/perf/util/libasm.c line 22

Quoted 6 lines
tools/perf/util/libasm.c new file +6 -0
+#include "namespaces.h"
+#include "symbol.h"
+
+struct ebl;
+extern struct ebl *ebl_openbackend(Elf *elf);
+extern void ebl_closebackend(struct ebl *ebl);

[Severity: High]
Are these internal libebl functions guaranteed to be available for linking?

They don't seem to be exposed in public elfutils headers like <elfutils/libasm.h>. Standard Linux distributions might not export these symbols in libdw.so or provide libebl.a, which could break compilation and linkage.

tools/perf/util/libasm.c line 29

Quoted 7 lines
tools/perf/util/libasm.c new file +7 -0
+
+struct disasm_output_arg {
+	char *buf;
+	size_t size;
+};
+
+static int disasm_output_cb(char *str, size_t len, void *arg)

[Severity: High]
Does the DisasmOutputCB_t signature expect the string argument to be const char *?

Dropping the const qualifier here might cause a compilation failure since the perf tool builds with -Werror.

Quoted 3 lines
tools/perf/util/libasm.c new file +3 -0
+{
+	struct disasm_output_arg *oa = arg;
+	size_t to_copy = len < oa->size - 1 ? len : oa->size - 1;

[ ... ]

Quoted 4 lines
tools/perf/util/libasm.c new file +4 -0
+	offset = 0;
+	while (pc < end) {
+		struct disasm_output_arg oa = {
+			.buf = disasm_buf,

tools/perf/util/libasm.c line 126

Quoted 10 lines
tools/perf/util/libasm.c new file +10 -0
+			.size = sizeof(disasm_buf),
+		};
+		const uint8_t *prev_pc = pc;
+
+		ret = disasm_cb(handle, &pc, end, addr, "%7m %.1o,%.2o,%.3o,%.4o,%.5o",
+				    disasm_output_cb, &oa, NULL);
+		if (ret != 1 || pc == prev_pc) {
+			/* Disassembly failed or got stuck */
+			break;
+		}

[Severity: High]
Does disasm_cb() return 0 on success?

If so, evaluating ret != 1 here will evaluate to true on the very first successful instruction parsing, instantly breaking the loop. This would guarantee that offset != buf_len, causing the code to discard all lines and unconditionally fall back to objdump.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author reviewer
#19 lore raw

PATCH v3 4/7 perf annotate: Add --disassembler command-line option

In reply to Ian Rogers

Add the --disassembler command-line option to perf annotate. This allows users to explicitly request or override the disassembler preferences (e.g. --disassembler libasm) directly on the command line.

Assisted-by Antigravity:Google Gemini 3.5-flash
---
 tools/perf/builtin-annotate.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
tools/perf/builtin-annotate.c +10 -0
index 8a0eb30eac24..57a88adfb130 100644
@@ -699,6 +699,7 @@ int cmd_annotate(int argc, const char **argv)
 		.set = 0,
 	};
 	const char *disassembler_style = NULL, *objdump_path = NULL, *addr2line_path = NULL;
+	const char *disassemblers_str = NULL;
 	struct option options[] = {
 	OPT_STRING('i', "input", &input_name, "file",
 		    "input file name"),
@@ -782,6 +783,8 @@ int cmd_annotate(int argc, const char **argv)
 		    "Do not display empty (or dummy) events in the output"),
 	OPT_BOOLEAN(0, "code-with-type", &annotate_opts.code_with_type,
 		    "Show data type info in code annotation (memory instructions only)"),
+	OPT_STRING(0, "disassembler", &disassemblers_str, "names",
+		   "comma separated list of disassemblers to use"),
 	OPT_END()
 	};
 	int ret;
@@ -825,6 +828,13 @@ int cmd_annotate(int argc, const char **argv)
 			return -ENOMEM;
 	}
 
+	if (disassemblers_str) {
+		memset(annotate_opts.disassemblers, 0, sizeof(annotate_opts.disassemblers));
+		ret = annotation_options__add_disassemblers_str(&annotate_opts, disassemblers_str);
+		if (ret)
+			return -EINVAL;
+	}
+
 	if (annotate_check_args() < 0)
 		return -EINVAL;
 
Signature · 1 line
2.54.0.1099.g489fc7bff1-goog
author reviewer
#20 lore raw

PATCH v3 5/7 perf test: Enhance annotate test coverage and isolate config

In reply to Ian Rogers

Update the annotate.sh shell test to test different disassembler backends (objdump, llvm, capstone, libasm) utilizing the new --disassembler command-line option.
Isolate the test script from host environment pollution by exporting PERF_CONFIG=/dev/null at the start of the script, ensuring it runs hermetically.

Assisted-by Antigravity:Google Gemini 3.5-flash
---
 tools/perf/tests/shell/annotate.sh | 60 ++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)
tools/perf/tests/shell/annotate.sh +60 -0
index 689de58e9238..eae6a46a247b 100755
@@ -4,6 +4,8 @@
 
 set -e
 
+export PERF_CONFIG=/dev/null
+
 shelldir=$(dirname "$0")
 
 # shellcheck source=lib/perf_has_symbol.sh
@@ -106,8 +108,66 @@ test_basic() {
   echo "${mode} annotate test [Success]"
 }
 
+test_disassembler() {
+  disassembler=$1
+  feature=$2
+  local ret=0
+
+  if [ -n "${feature}" ]
+  then
+    if ! perf check feature "${feature}" > /dev/null 2>&1
+    then
+      echo "Skip test for ${disassembler} (feature ${feature} not supported)"
+      return 0
+    fi
+  fi
+
+  echo "Test annotate with disassembler: ${disassembler}"
+
+  perf annotate --no-demangle -i "${perfdata}" --stdio \
+    --percent-limit 10 --disassembler "${disassembler}" \
+    2> /dev/null > "${perfout}" || ret=$?
+
+  if [ "${ret}" -ne 0 ]
+  then
+    echo "annotate with ${disassembler} [Failed: perf annotate error]"
+    err=1
+    return 0
+  fi
+
+  # check if it has the target symbol
+  if ! grep -q "${testsym}" "${perfout}"
+  then
+    echo "annotate with ${disassembler} [Failed: missing target symbol]"
+    err=1
+    return 0
+  fi
+
+  # check if it has the disassembly lines
+  if ! grep -q "${disasm_regex}" "${perfout}"
+  then
+    echo "annotate with ${disassembler} [Failed: missing disasm output]"
+    err=1
+    return 0
+  fi
+
+  echo "annotate with ${disassembler} [Success]"
+  return 0
+}
+
 test_basic Basic
 test_basic Pipe
 
+# Restore perfdata to a regular format for disassembler tests
+perf record -o "${perfdata}" ${testprog} > /dev/null 2>&1
+
+if [ "${err}" -eq 0 ]
+then
+  test_disassembler "objdump" ""
+  test_disassembler "llvm" "libLLVM"
+  test_disassembler "capstone" "libcapstone"
+  test_disassembler "libasm" "libasm"
+fi
+
 cleanup
 exit $err
Signature · 1 line
2.54.0.1099.g489fc7bff1-goog
#21 lore raw

Re: [PATCH v3 5/7] perf test: Enhance annotate test coverage and isolate config

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The patch adds a test for a non-existent 'libasm' disassembler backend and feature, resulting in…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The patch adds a test for a non-existent 'libasm' disassembler backend and feature, resulting in permanently skipped dead code due to AI hallucination. --

commit a204be6f7db310b7e87e271b24c42730bafc0573
Author: Ian Rogers <[email protected]>

perf test: Enhance annotate test coverage and isolate config

This commit updates the annotate.sh test to verify different disassembler backends like objdump, llvm, and capstone. It also isolates the test script from host environment pollution by exporting PERF_CONFIG=/dev/null.

Quoted 4 lines
tools/perf/tests/shell/annotate.sh +0 -0
index 689de58e92384..eae6a46a247b4 100755

[ ... ]

Quoted 1 line
tools/perf/tests/shell/annotate.sh +0 -0
@@ -106,8 +108,66 @@ test_basic() {

[ ... ]

tools/perf/tests/shell/annotate.sh test_basic() { · line 169

Quoted 9 lines
tools/perf/tests/shell/annotate.sh +9 -0
+# Restore perfdata to a regular format for disassembler tests
+perf record -o "${perfdata}" ${testprog} > /dev/null 2>&1
+
+if [ "${err}" -eq 0 ]
+then
+  test_disassembler "objdump" ""
+  test_disassembler "llvm" "libLLVM"
+  test_disassembler "capstone" "libcapstone"
+  test_disassembler "libasm" "libasm"

[Severity: Low]
Is "libasm" a supported feature in perf?

It does not appear to be defined in the supported_features array or as a valid disassembler backend in perf. Because of this, the check in test_disassembler():

    if ! perf check feature "${feature}" > /dev/null 2>&1
    then
      echo "Skip test for ${disassembler} (feature ${feature} not supported)"
      return 0
    fi

will always fail, causing this subtest to silently print the skip message and return 0 on every execution.

Should this test case be removed?

Quoted 4 lines
tools/perf/tests/shell/annotate.sh +2 -0
+fi
+
 cleanup
 exit $err
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
author reviewer
#22 lore raw

PATCH v3 6/7 perf annotate: Support BPF JIT disassembly via genelf

In reply to Ian Rogers

For in-memory BPF DSOs (DSO_BINARY_TYPE__BPF_PROG_INFO), write the JITted instruction buffer to a temporary ELF file on disk using the existing genelf framework (jit_write_elf). Reroute disassembly to this temporary ELF file, allowing objdump and libasm to disassemble it natively. Clean up the temporary file afterward.

Assisted-by Antigravity:Google Gemini 3.5-flash
---
 tools/perf/tests/genelf.c |   2 +-
 tools/perf/util/disasm.c  | 111 +++++++++++++++++++++++++++++++++++---
 tools/perf/util/disasm.h  |   1 +
 tools/perf/util/genelf.c  |  16 +++---
 tools/perf/util/genelf.h  |   2 +-
 tools/perf/util/jitdump.c |   3 +-
 6 files changed, 117 insertions(+), 18 deletions(-)
tools/perf/tests/genelf.c +1 -1
index 95f3be1b683a..1723370db32e 100644
@@ -38,7 +38,7 @@ static int test__jit_write_elf(struct test_suite *test __maybe_unused,
 
 	pr_info("Writing jit code to: %s\n", path);
 
-	ret = jit_write_elf(fd, 0, "main", x86_code, sizeof(x86_code),
+	ret = jit_write_elf(fd, GEN_ELF_ARCH, 0, "main", x86_code, sizeof(x86_code),
 			NULL, 0, NULL, 0, 0);
 	close(fd);
 
tools/perf/util/disasm.c +104 -7

167 lines not on this page · raw message

tools/perf/util/disasm.h +1 -0
index 25756e3f47e4..32a5b3f5d1c6 100644
@@ -106,6 +106,7 @@ struct annotate_args {
 	char			  *line;
 	int			  line_nr;
 	char			  *fileloc;
+	bool			  is_temp_elf;
 };
 
 const struct arch *arch__find(uint16_t e_machine, uint32_t e_flags, const char *cpuid);
tools/perf/util/genelf.c +8 -8
index 14882def9704..c3af040121ee 100644
@@ -179,7 +179,7 @@ static void blake2s_update_tagged(struct blake2s_ctx *ctx, int tag,
  * csize: the code size in bytes
  */
 int
-jit_write_elf(int fd, uint64_t load_addr __maybe_unused, const char *sym,
+jit_write_elf(int fd, uint16_t e_machine, uint64_t load_addr, const char *sym,
 	      const void *code, int csize,
 	      void *debug __maybe_unused, int nr_debug_entries __maybe_unused,
 	      void *unwinding, uint64_t unwinding_header_size, uint64_t unwinding_size)
@@ -218,9 +218,9 @@ jit_write_elf(int fd, uint64_t load_addr __maybe_unused, const char *sym,
 
 	ehdr->e_ident[EI_DATA] = GEN_ELF_ENDIAN;
 	ehdr->e_ident[EI_CLASS] = GEN_ELF_CLASS;
-	ehdr->e_machine = GEN_ELF_ARCH;
+	ehdr->e_machine = e_machine;
 	ehdr->e_type = ET_DYN;
-	ehdr->e_entry = GEN_ELF_TEXT_OFFSET;
+	ehdr->e_entry = load_addr;
 	ehdr->e_version = EV_CURRENT;
 	ehdr->e_shstrndx= unwinding ? 4 : 2; /* shdr index for section name */
 
@@ -230,8 +230,8 @@ jit_write_elf(int fd, uint64_t load_addr __maybe_unused, const char *sym,
 	phdr = elf_newphdr(e, 1);
 	phdr[0].p_type = PT_LOAD;
 	phdr[0].p_offset = GEN_ELF_TEXT_OFFSET;
-	phdr[0].p_vaddr = GEN_ELF_TEXT_OFFSET;
-	phdr[0].p_paddr = GEN_ELF_TEXT_OFFSET;
+	phdr[0].p_vaddr = load_addr;
+	phdr[0].p_paddr = load_addr;
 	phdr[0].p_filesz = csize;
 	phdr[0].p_memsz = csize;
 	phdr[0].p_flags = PF_X | PF_R;
@@ -267,7 +267,7 @@ jit_write_elf(int fd, uint64_t load_addr __maybe_unused, const char *sym,
 
 	shdr->sh_name = 1;
 	shdr->sh_type = SHT_PROGBITS;
-	shdr->sh_addr = GEN_ELF_TEXT_OFFSET;
+	shdr->sh_addr = load_addr;
 	shdr->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
 	shdr->sh_entsize = 0;
 
@@ -278,7 +278,7 @@ jit_write_elf(int fd, uint64_t load_addr __maybe_unused, const char *sym,
 	 * Setup .eh_frame_hdr and .eh_frame
 	 */
 	if (unwinding) {
-		eh_frame_base_offset = ALIGN_8(GEN_ELF_TEXT_OFFSET + csize);
+		eh_frame_base_offset = ALIGN_8(load_addr + csize);
 		retval = jit_add_eh_frame_info(e, unwinding,
 					       unwinding_header_size, unwinding_size,
 					       eh_frame_base_offset);
@@ -324,7 +324,7 @@ jit_write_elf(int fd, uint64_t load_addr __maybe_unused, const char *sym,
 	 * setup symtab section
 	 */
 	symtab[1].st_size  = csize;
-	symtab[1].st_value = GEN_ELF_TEXT_OFFSET;
+	symtab[1].st_value = load_addr;
 
 	scn = elf_newscn(e);
 	if (!scn) {
tools/perf/util/genelf.h +1 -1
index 9f0b875d6548..992ca1eb2734 100644
@@ -5,7 +5,7 @@
 #include <linux/math.h>
 
 /* genelf.c */
-int jit_write_elf(int fd, uint64_t code_addr, const char *sym,
+int jit_write_elf(int fd, uint16_t e_machine, uint64_t code_addr, const char *sym,
 		  const void *code, int csize, void *debug, int nr_debug_entries,
 		  void *unwinding, uint64_t unwinding_header_size, uint64_t unwinding_size);
 #ifdef HAVE_LIBDW_SUPPORT
tools/perf/util/jitdump.c +2 -1
index 18fd84a82153..ddf69032b01c 100644
@@ -95,7 +95,8 @@ jit_emit_elf(struct jit_buf_desc *jd,
 		return -1;
 	}
 
-	ret = jit_write_elf(fd, code_addr, sym, (const void *)code, csize, debug, nr_debug_entries,
+	ret = jit_write_elf(fd, GEN_ELF_ARCH, code_addr, sym,
+			    (const void *)code, csize, debug, nr_debug_entries,
 			    unwinding, unwinding_header_size, unwinding_size);
 
         close(fd);
Signature · 1 line
2.54.0.1099.g489fc7bff1-goog
#23 lore raw

Re: [PATCH v3 6/7] perf annotate: Support BPF JIT disassembly via genelf

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] JIT ELF generation uses host-native endianness and ELF class, breaking cross-platform analysis of…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] JIT ELF generation uses host-native endianness and ELF class, breaking cross-platform analysis of BPF programs.
- [High] Mismatch between ELF text section address and DWARF debug line base address for JIT profiling. --

commit 3c8720c4a004bb87cca5176252e00dcb2ade47e7
Author: Ian Rogers <[email protected]>

perf annotate: Support BPF JIT disassembly via genelf

For in-memory BPF DSOs (DSO_BINARY_TYPE__BPF_PROG_INFO), write the JITted instruction buffer to a temporary ELF file on disk using the existing genelf framework (jit_write_elf). Reroute disassembly to this temporary ELF file, allowing objdump and libasm to disassemble it natively. Clean up the temporary file afterward.

Quoted 4 lines
tools/perf/util/genelf.c +0 -0
index 14882def9704c..c3af040121eef 100644

[ ... ]

tools/perf/util/genelf.c jit_write_elf(int fd, uint64_t load_addr __maybe_unused, const char *sym, · line 221

Quoted 6 lines
tools/perf/util/genelf.c +1 -1
@@ -218,9 +218,9 @@ jit_write_elf(int fd, uint64_t load_addr __maybe_unused, const char *sym,
 
 	ehdr->e_ident[EI_DATA] = GEN_ELF_ENDIAN;
 	ehdr->e_ident[EI_CLASS] = GEN_ELF_CLASS;
-	ehdr->e_machine = GEN_ELF_ARCH;
+	ehdr->e_machine = e_machine;

[Severity: High]
Does this hardcoded endianness and class break cross-platform trace analysis?

Since e_machine is now dynamically set, if a 64-bit x86 host analyzes a 32-bit or big-endian BPF trace (like ARM32 or s390x), wouldn't this generate a corrupt ELF container (e.g., a 64-bit little-endian ELF claiming to be EM_S390) and cause objdump to reject the file or decode with the wrong endianness?

[ ... ]

tools/perf/util/genelf.c jit_write_elf(int fd, uint64_t load_addr __maybe_unused, const char *sym, · line 234

Quoted 8 lines
tools/perf/util/genelf.c +2 -2
@@ -230,8 +230,8 @@ jit_write_elf(int fd, uint64_t load_addr __maybe_unused, const char *sym,
 	phdr = elf_newphdr(e, 1);
 	phdr[0].p_type = PT_LOAD;
 	phdr[0].p_offset = GEN_ELF_TEXT_OFFSET;
-	phdr[0].p_vaddr = GEN_ELF_TEXT_OFFSET;
-	phdr[0].p_paddr = GEN_ELF_TEXT_OFFSET;
+	phdr[0].p_vaddr = load_addr;
+	phdr[0].p_paddr = load_addr;

[Severity: High]
Does changing the ELF virtual addresses to use load_addr break DWARF mapping?

In tools/perf/util/genelf_debug.c:jit_process_debug_info(), the code still passes the hardcoded GEN_ELF_TEXT_OFFSET to add_debug_line():

	add_debug_line(dl, debug, nr_debug_entries, GEN_ELF_TEXT_OFFSET);

If the ELF is generated with load_addr but the DWARF .debug_line information uses a base address of GEN_ELF_TEXT_OFFSET, will perf annotate and standard debuggers fail to map source code lines to the disassembled instructions?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
author reviewer
#24 lore raw

PATCH v3 7/7 perf test: Add BPF JIT annotation test coverage for all disassemblers

In reply to Ian Rogers

Expand the annotate.sh shell test to verify BPF JIT disassembly. If the test is run with sufficient privileges (root/CAP_BPF) and captures system BPF programs, it will extract a JITted BPF program symbol from the perf report and run perf annotate on it. This validates the temporary ELF generation and disassembles it using each of the supported disassembler backends (objdump, llvm, capstone, and libasm).

Assisted-by Antigravity:Google Gemini 3.5-flash
---
 tools/perf/tests/shell/annotate.sh | 93 ++++++++++++++++++++++++++++++
 1 file changed, 93 insertions(+)
tools/perf/tests/shell/annotate.sh +93 -0
index eae6a46a247b..ae03580fa023 100755
@@ -18,6 +18,7 @@ skip_test_missing_symbol ${testsym}
 err=0
 perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
 perfout=$(mktemp /tmp/__perf_test.perf.out.XXXXX)
+bpftmp=""
 testprog="perf test -w noploop"
 # disassembly format: "percent : offset: instruction (operands ...)"
 disasm_regex="[0-9]*\.[0-9]* *: *\w*: *\w*"
@@ -25,6 +26,9 @@ disasm_regex="[0-9]*\.[0-9]* *: *\w*: *\w*"
 cleanup() {
   rm -rf "${perfdata}" "${perfout}"
   rm -rf "${perfdata}".old
+  if [ -n "${bpftmp}" ]; then
+    rm -rf "${bpftmp}"
+  fi
 
   trap - EXIT TERM INT
 }
@@ -169,5 +173,94 @@ then
   test_disassembler "libasm" "libasm"
 fi
 
+test_bpf_disassembler() {
+  disassembler=$1
+  feature=$2
+  bpf_perfdata_file=$3
+
+  if [ -n "${feature}" ]
+  then
+    if ! perf check feature "${feature}" > /dev/null 2>&1
+    then
+      echo "Skip BPF JIT test for ${disassembler} (feature ${feature} not supported)"
+      return 0
+    fi
+  fi
+
+  echo "Test BPF JIT annotate with disassembler: ${disassembler}"
+
+  if ! perf annotate --no-demangle -i "${bpf_perfdata_file}" --stdio "${bpf_sym}" \
+      --disassembler "${disassembler}" 2> /dev/null > "${perfout}"
+  then
+    echo "BPF JIT annotate with ${disassembler} [Failed: perf annotate error]"
+    err=1
+    return 0
+  fi
+
+  if ! grep -q "${disasm_regex}" "${perfout}"
+  then
+    echo "BPF JIT annotate with ${disassembler} [Failed: missing disasm output]"
+    err=1
+    return 0
+  fi
+
+  echo "BPF JIT annotate with ${disassembler} [Success]"
+  return 0
+}
+
+test_bpf() {
+  echo "Test annotate with BPF JIT output"
+
+  if ! perf check -q feature libbpf-strings ; then
+    echo "BPF annotation test [Skipped - libbpf-strings not supported]"
+    return 0
+  fi
+
+  bpftmp=$(mktemp -d /tmp/__perf_test.bpf.XXXXXX)
+  bpf_perfdata="${bpftmp}/perf.data"
+
+  if ! perf record -a -e cycles -F 4000 -o "${bpf_perfdata}" -- sleep 1 2> /dev/null
+  then
+    echo "BPF annotation test [Skipped - perf record -a failed, probably no privileges]"
+    rm -rf "${bpftmp}"
+    bpftmp=""
+    return 0
+  fi
+
+  bpf_symbols=$(perf report --stdio -i "${bpf_perfdata}" 2>/dev/null | \
+      grep -E -o 'bpf_prog_[0-9a-f]{16}_[0-9A-Za-z_]*' | sort -u)
+
+  bpf_sym=""
+  for sym in ${bpf_symbols}
+  do
+    # Check if we can annotate it (meaning JIT code is available)
+    if perf annotate -i "${bpf_perfdata}" --stdio "${sym}" > /dev/null 2>&1
+    then
+      bpf_sym="${sym}"
+      break
+    fi
+  done
+
+  if [ -z "${bpf_sym}" ]; then
+    echo "BPF annotation test [Skipped - no JITted BPF symbols with code found (race?)]"
+    rm -rf "${bpftmp}"
+    bpftmp=""
+    return 0
+  fi
+
+  test_bpf_disassembler "objdump" "" "${bpf_perfdata}"
+  test_bpf_disassembler "llvm" "libLLVM" "${bpf_perfdata}"
+  test_bpf_disassembler "capstone" "libcapstone" "${bpf_perfdata}"
+  test_bpf_disassembler "libasm" "libasm" "${bpf_perfdata}"
+
+  rm -rf "${bpftmp}"
+  bpftmp=""
+}
+
+if [ "${err}" -eq 0 ]
+then
+  test_bpf
+fi
+
 cleanup
 exit $err
Signature · 1 line
2.54.0.1099.g489fc7bff1-goog