Skip to contentSkip to conversation

Support dynamic opening of capstone/llvm remove BUILD_NONDISTRO

PATCH Superseded Ian Rogers 4 participants 30 messages Jan 22, 2025 – Mar 14, 2025

Subsystems (2)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

v2 cover Support dynamic opening of capstone/llvm remove BUILD_NONDISTRO

Linking against libcapstone and libLLVM can be a significant increase in dependencies and size of memory footprint. For something like perf record the disassembler and addr2line functionality won't be used. Support dynamically loading these libraries using dlopen and then calling the appropriate functions found using dlsym.

BUILD_NONDISTRO is used to build perf against the license incompatible libbfd and libiberty libraries. As this has been opt-in for nearly 2 years, commit dd317df07207 ("perf build: Make binutil libraries opt in"), remove the code to simplify the code base.

The patch series:
1) does some initial clean up;
2) moves the capstone and LLVM code to their own C files,
3) simplifies a little the capstone code;
4) adds perf_ variants of the functions that will either directly call the function or use dlsym to discover it;
5) adds BPF JIT disassembly support to LLVM and capstone disassembly;
6) removes the BUILD_NONDISTRO code, reduces scope and removes what's possible.

The addr2line LLVM functionality is written in C++. To avoid linking against libLLVM for this, a new LIBLLVM_DYNAMIC option is added where the C++ code with the libLLVM dependency will be built into a libperf-llvm.so and that dlsym-ed and called against. Ideally LLVM would extend their C API to avoid this.

The libbfd BPF disassembly supported source lines, this wasn't ported to the capstone and LLVM disassembly.

v2: Add mangling of the function names in libperf-llvm.so to avoid
potential infinite recursion. Add BPF JIT disassembly support to LLVM and capstone. Add/rebase the BUILD_NONDISTRO cleanup onto the series from:
https://lore.kernel.org/lkml/[email protected]/
Some other minor additional clean up.

Ian Rogers (17):
perf build: Remove libtracefs configuration
perf map: Constify objdump offset/address conversion APIs perf capstone: Move capstone functionality into its own file perf llvm: Move llvm functionality into its own file perf capstone: Remove open_capstone_handle
perf capstone: Support for dlopen-ing libcapstone.so perf llvm: Support for dlopen-ing libLLVM.so
perf llvm: Mangle libperf-llvm.so function names perf dso: Move read_symbol from llvm/capstone to dso perf dso: Support BPF programs in dso__read_symbol perf llvm: Disassemble cleanup
perf dso: Clean up read_symbol error handling
perf build: Remove libbfd support
perf build: Remove libiberty support
perf build: Remove unused defines
perf disasm: Remove disasm_bpf
perf disasm: Make ins__scnprintf and ins__is_nop static

 tools/perf/Documentation/perf-check.txt |   1 -
 tools/perf/Makefile.config              |  90 +---
 tools/perf/Makefile.perf                |  35 +-
 tools/perf/builtin-check.c              |   1 -
 tools/perf/builtin-script.c             |   2 -
 tools/perf/tests/Build                  |   1 -
 tools/perf/tests/builtin-test.c         |   1 -
 tools/perf/tests/make                   |   4 +-
 tools/perf/tests/pe-file-parsing.c      | 101 ----
 tools/perf/tests/tests.h                |   1 -
 tools/perf/util/Build                   |   5 +-
 tools/perf/util/annotate.h              |   1 -
 tools/perf/util/capstone.c              | 682 ++++++++++++++++++++++++
 tools/perf/util/capstone.h              |  24 +
 tools/perf/util/demangle-cxx.cpp        |  22 +-
 tools/perf/util/disasm.c                | 632 +---------------------
 tools/perf/util/disasm.h                |   5 +-
 tools/perf/util/disasm_bpf.c            | 195 -------
 tools/perf/util/disasm_bpf.h            |  12 -
 tools/perf/util/dso.c                   |  98 ++++
 tools/perf/util/dso.h                   |   4 +
 tools/perf/util/llvm-c-helpers.cpp      | 120 ++++-
 tools/perf/util/llvm-c-helpers.h        |  24 +-
 tools/perf/util/llvm.c                  | 489 +++++++++++++++++
 tools/perf/util/llvm.h                  |  24 +
 tools/perf/util/map.c                   |  19 +-
 tools/perf/util/map.h                   |   6 +-
 tools/perf/util/print_insn.c            | 117 +---
 tools/perf/util/srcline.c               | 306 +----------
 tools/perf/util/srcline.h               |   6 +
 tools/perf/util/symbol-elf.c            |  95 ----
 tools/perf/util/symbol.c                | 135 -----
 tools/perf/util/symbol.h                |   4 -
 33 files changed, 1552 insertions(+), 1710 deletions(-)
 delete mode 100644 tools/perf/tests/pe-file-parsing.c
 create mode 100644 tools/perf/util/capstone.c
 create mode 100644 tools/perf/util/capstone.h
 delete mode 100644 tools/perf/util/disasm_bpf.c
 delete mode 100644 tools/perf/util/disasm_bpf.h
 create mode 100644 tools/perf/util/llvm.c
 create mode 100644 tools/perf/util/llvm.h
Signature · 1 line
2.48.0.rc2.279.g1de40edade-goog
author reviewer
#1 lore raw

PATCH v2 1/17 perf build: Remove libtracefs configuration

libtracefs isn't used by perf but not having it installed causes build warnings. Given the library isn't used, there is no need for the configuration or warnings so remove.

---
 tools/perf/Makefile.config | 14 --------------
 1 file changed, 14 deletions(-)
tools/perf/Makefile.config +0 -14
index a148ca9efca9..cd773fbbc176 100644
@@ -1174,20 +1174,6 @@ ifneq ($(NO_LIBTRACEEVENT),1)
   else
     $(error ERROR: libtraceevent is missing. Please install libtraceevent-dev/libtraceevent-devel and/or set LIBTRACEEVENT_DIR or build with NO_LIBTRACEEVENT=1)
   endif
-
-  ifeq ($(feature-libtracefs), 1)
-    CFLAGS +=  $(shell $(PKG_CONFIG) --cflags libtracefs)
-    LDFLAGS += $(shell $(PKG_CONFIG) --libs-only-L libtracefs)
-    EXTLIBS += $(shell $(PKG_CONFIG) --libs-only-l libtracefs)
-    LIBTRACEFS_VERSION := $(shell $(PKG_CONFIG) --modversion libtracefs).0.0
-    LIBTRACEFS_VERSION_1 := $(word 1, $(subst ., ,$(LIBTRACEFS_VERSION)))
-    LIBTRACEFS_VERSION_2 := $(word 2, $(subst ., ,$(LIBTRACEFS_VERSION)))
-    LIBTRACEFS_VERSION_3 := $(word 3, $(subst ., ,$(LIBTRACEFS_VERSION)))
-    LIBTRACEFS_VERSION_CPP := $(shell expr $(LIBTRACEFS_VERSION_1) \* 255 \* 255 + $(LIBTRACEFS_VERSION_2) \* 255 + $(LIBTRACEFS_VERSION_3))
-    CFLAGS += -DLIBTRACEFS_VERSION=$(LIBTRACEFS_VERSION_CPP)
-  else
-    $(warning libtracefs is missing. Please install libtracefs-dev/libtracefs-devel)
-  endif
 endif
 
 # Among the variables below, these:
Signature · 1 line
2.48.0.rc2.279.g1de40edade-goog
author reviewer
#2 lore raw

PATCH v2 2/17 perf map: Constify objdump offset/address conversion APIs

In reply to Ian Rogers

Make the map argument const as the conversion act won't modify the map and this allows other callers to use a const struct map.

---
 tools/perf/util/map.c | 19 +++++++++++++++----
 tools/perf/util/map.h |  6 +++---
 2 files changed, 18 insertions(+), 7 deletions(-)
tools/perf/util/map.c +15 -4
index d729438b7d65..a22a423792d7 100644
@@ -514,6 +514,8 @@ void srccode_state_free(struct srccode_state *state)
 	state->line = 0;
 }
 
+static const struct kmap *__map__const_kmap(const struct map *map);
+
 /**
  * map__rip_2objdump - convert symbol start address to objdump address.
  * @map: memory map
@@ -525,9 +527,9 @@ void srccode_state_free(struct srccode_state *state)
  *
  * Return: Address suitable for passing to "objdump --start-address="
  */
-u64 map__rip_2objdump(struct map *map, u64 rip)
+u64 map__rip_2objdump(const struct map *map, u64 rip)
 {
-	struct kmap *kmap = __map__kmap(map);
+	const struct kmap *kmap = __map__const_kmap(map);
 	const struct dso *dso = map__dso(map);
 
 	/*
@@ -570,7 +572,7 @@ u64 map__rip_2objdump(struct map *map, u64 rip)
  *
  * Return: Memory address.
  */
-u64 map__objdump_2mem(struct map *map, u64 ip)
+u64 map__objdump_2mem(const struct map *map, u64 ip)
 {
 	const struct dso *dso = map__dso(map);
 
@@ -587,7 +589,7 @@ u64 map__objdump_2mem(struct map *map, u64 ip)
 }
 
 /* convert objdump address to relative address.  (To be removed) */
-u64 map__objdump_2rip(struct map *map, u64 ip)
+u64 map__objdump_2rip(const struct map *map, u64 ip)
 {
 	const struct dso *dso = map__dso(map);
 
@@ -619,6 +621,15 @@ struct kmap *__map__kmap(struct map *map)
 	return (struct kmap *)(&RC_CHK_ACCESS(map)[1]);
 }
 
+static const struct kmap *__map__const_kmap(const struct map *map)
+{
+	const struct dso *dso = map__dso(map);
+
+	if (!dso || !dso__kernel(dso))
+		return NULL;
+	return (struct kmap *)(&RC_CHK_ACCESS(map)[1]);
+}
+
 struct kmap *map__kmap(struct map *map)
 {
 	struct kmap *kmap = __map__kmap(map);
tools/perf/util/map.h +3 -3
index 4262f5a143be..768501eec70e 100644
@@ -133,13 +133,13 @@ static inline u64 map__unmap_ip(const struct map *map, u64 ip_or_rip)
 }
 
 /* rip/ip <-> addr suitable for passing to `objdump --start-address=` */
-u64 map__rip_2objdump(struct map *map, u64 rip);
+u64 map__rip_2objdump(const struct map *map, u64 rip);
 
 /* objdump address -> memory address */
-u64 map__objdump_2mem(struct map *map, u64 ip);
+u64 map__objdump_2mem(const struct map *map, u64 ip);
 
 /* objdump address -> rip */
-u64 map__objdump_2rip(struct map *map, u64 ip);
+u64 map__objdump_2rip(const struct map *map, u64 ip);
 
 struct symbol;
 struct thread;
Signature · 1 line
2.48.0.rc2.279.g1de40edade-goog
author reviewer
#3 lore raw

PATCH v2 3/17 perf capstone: Move capstone functionality into its own file

In reply to Ian Rogers

Capstone disassembly support was split between disasm.c and print_insn.c. Move support out of these files into capstone.[ch] and remove include capstone/capstone.h from those files. As disassembly routines can fail, make failure the only option without HAVE_LIBCAPSTONE_SUPPORT. For simplicity's sake, duplicate the read_symbol utility function.

The intent with moving capstone support into a single file is that dynamic support, using dlopen for libcapstone, can be added in later patches. This can potentially always succeed or fail, so relying on ifdefs isn't sufficient. Using dlopen is a useful option to minimize the perf tools dependencies and potentially size.

---
 tools/perf/builtin-script.c  |   2 -
 tools/perf/util/Build        |   1 +
 tools/perf/util/capstone.c   | 536 +++++++++++++++++++++++++++++++++++
 tools/perf/util/capstone.h   |  24 ++
 tools/perf/util/disasm.c     | 358 +----------------------
 tools/perf/util/print_insn.c | 117 +-------
 6 files changed, 569 insertions(+), 469 deletions(-)
 create mode 100644 tools/perf/util/capstone.c
 create mode 100644 tools/perf/util/capstone.h
tools/perf/builtin-script.c +0 -2
index 33667b534634..f05b2b70d5a7 100644
@@ -1200,7 +1200,6 @@ static int any_dump_insn(struct evsel *evsel __maybe_unused,
 			 u8 *inbuf, int inlen, int *lenp,
 			 FILE *fp)
 {
-#ifdef HAVE_LIBCAPSTONE_SUPPORT
 	if (PRINT_FIELD(BRSTACKDISASM)) {
 		int printed = fprintf_insn_asm(x->machine, x->thread, x->cpumode, x->is64bit,
 					       (uint8_t *)inbuf, inlen, ip, lenp,
@@ -1209,7 +1208,6 @@ static int any_dump_insn(struct evsel *evsel __maybe_unused,
 		if (printed > 0)
 			return printed;
 	}
-#endif
 	return fprintf(fp, "%s", dump_insn(x, ip, inbuf, inlen, lenp));
 }
 
tools/perf/util/Build +1 -0
index 5ec97e8d6b6d..9542decf9625 100644
@@ -8,6 +8,7 @@ perf-util-y += block-info.o
 perf-util-y += block-range.o
 perf-util-y += build-id.o
 perf-util-y += cacheline.o
+perf-util-y += capstone.o
 perf-util-y += config.o
 perf-util-y += copyfile.o
 perf-util-y += ctype.o
tools/perf/util/capstone.c new file +536 -0

536 lines not on this page · raw message

tools/perf/util/capstone.h new file +24 -0
new file mode 100644
index 000000000000..0f030ea034b6
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __PERF_CAPSTONE_H
+#define __PERF_CAPSTONE_H
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <linux/types.h>
+
+struct annotate_args;
+struct machine;
+struct symbol;
+struct thread;
+
+ssize_t capstone__fprintf_insn_asm(struct machine *machine, struct thread *thread, u8 cpumode,
+				   bool is64bit, const uint8_t *code, size_t code_size,
+				   uint64_t ip, int *lenp, int print_opts, FILE *fp);
+int symbol__disassemble_capstone(const char *filename, struct symbol *sym,
+				 struct annotate_args *args);
+int symbol__disassemble_capstone_powerpc(const char *filename, struct symbol *sym,
+					 struct annotate_args *args);
+
+#endif /* __PERF_CAPSTONE_H */
tools/perf/util/disasm.c +5 -353

391 lines not on this page · raw message

tools/perf/util/print_insn.c +3 -114

138 lines not on this page · raw message

Signature · 1 line
2.48.0.rc2.279.g1de40edade-goog
author reviewer
#4 lore raw

PATCH v2 4/17 perf llvm: Move llvm functionality into its own file

In reply to Ian Rogers

LLVM disassembly support was in disasm.c and addr2line support in srcline.c. Move support out of these files into llvm.[ch] and remove LLVM includes from those files. As disassembl routines can fail, make failure the only option without HAVE_LIBLLVM_SUPPORT. For simplicity's sake, duplicate the read_symbol utility function.

The intent with moving LLVM support into a single file is that dynamic support, using dlopen for libllvm, can be added in later patches. This can potentially always succeed or fail, so relying on ifdefs isn't sufficient. Using dlopen is a useful option to minimize the perf tools dependencies and potentially size.

---
 tools/perf/util/Build     |   1 +
 tools/perf/util/disasm.c  | 262 +-----------------------------
 tools/perf/util/disasm.h  |   2 +
 tools/perf/util/llvm.c    | 326 ++++++++++++++++++++++++++++++++++++++
 tools/perf/util/llvm.h    |  24 +++
 tools/perf/util/srcline.c |  65 ++------
 tools/perf/util/srcline.h |   6 +
 7 files changed, 373 insertions(+), 313 deletions(-)
 create mode 100644 tools/perf/util/llvm.c
 create mode 100644 tools/perf/util/llvm.h
tools/perf/util/Build +1 -0
index 9542decf9625..6fe0b5882c97 100644
@@ -26,6 +26,7 @@ perf-util-y += evswitch.o
 perf-util-y += find_bit.o
 perf-util-y += get_current_dir_name.o
 perf-util-y += levenshtein.o
+perf-util-y += llvm.o
 perf-util-y += mmap.o
 perf-util-y += memswap.o
 perf-util-y += parse-events.o
tools/perf/util/disasm.c +2 -260

298 lines not on this page · raw message

tools/perf/util/disasm.h +2 -0
index c135db2416b5..2cb4e1a6bd30 100644
@@ -128,4 +128,6 @@ int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size,
 
 int symbol__disassemble(struct symbol *sym, struct annotate_args *args);
 
+char *expand_tabs(char *line, char **storage, size_t *storage_len);
+
 #endif /* __PERF_UTIL_DISASM_H */
tools/perf/util/llvm.c new file +326 -0

326 lines not on this page · raw message

tools/perf/util/llvm.h new file +24 -0
new file mode 100644
index 000000000000..8aa19bb6b068
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __PERF_LLVM_H
+#define __PERF_LLVM_H
+
+#include <stdbool.h>
+#include <linux/types.h>
+
+struct annotate_args;
+struct dso;
+struct inline_node;
+struct symbol;
+
+int llvm__addr2line(const char *dso_name, u64 addr,
+		char **file, unsigned int *line, struct dso *dso,
+		bool unwind_inlines, struct inline_node *node,
+		struct symbol *sym);
+
+
+void dso__free_a2l_llvm(struct dso *dso);
+
+int symbol__disassemble_llvm(const char *filename, struct symbol *sym,
+			     struct annotate_args *args);
+
+#endif /* __PERF_LLVM_H */
tools/perf/util/srcline.c +12 -53

103 lines not on this page · raw message

tools/perf/util/srcline.h +6 -0
index 75010d39ea28..80c20169e250 100644
@@ -55,4 +55,10 @@ struct inline_node *inlines__tree_find(struct rb_root_cached *tree, u64 addr);
 /* delete all nodes within the tree of inline_node s */
 void inlines__tree_delete(struct rb_root_cached *tree);
 
+int inline_list__append(struct symbol *symbol, char *srcline, struct inline_node *node);
+char *srcline_from_fileline(const char *file, unsigned int line);
+struct symbol *new_inline_sym(struct dso *dso,
+			      struct symbol *base_sym,
+			      const char *funcname);
+
 #endif /* PERF_SRCLINE_H */
Signature · 1 line
2.48.0.rc2.279.g1de40edade-goog
author reviewer
#5 lore raw

PATCH v2 5/17 perf capstone: Remove open_capstone_handle

In reply to Ian Rogers

open_capstone_handle is similar to capstone_init and used only by symbol__disassemble_capstone. symbol__disassemble_capstone_powerpc already uses capstone_init, transition symbol__disassemble_capstone and eliminate open_capstone_handle.

---
 tools/perf/util/capstone.c | 34 ++++++----------------------------
 1 file changed, 6 insertions(+), 28 deletions(-)
tools/perf/util/capstone.c +6 -28
index c0a6d94ebc18..c9845e4d8781 100644
@@ -137,33 +137,6 @@ ssize_t capstone__fprintf_insn_asm(struct machine *machine __maybe_unused,
 #endif
 }
 
-#ifdef HAVE_LIBCAPSTONE_SUPPORT
-static int open_capstone_handle(struct annotate_args *args, bool is_64bit, csh *handle)
-{
-	struct annotation_options *opt = args->options;
-	cs_mode mode = is_64bit ? CS_MODE_64 : CS_MODE_32;
-
-	/* TODO: support more architectures */
-	if (!arch__is(args->arch, "x86"))
-		return -1;
-
-	if (cs_open(CS_ARCH_X86, mode, handle) != CS_ERR_OK)
-		return -1;
-
-	if (!opt->disassembler_style ||
-	    !strcmp(opt->disassembler_style, "att"))
-		cs_option(*handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_ATT);
-
-	/*
-	 * Resolving address operands to symbols is implemented
-	 * on x86 by investigating instruction details.
-	 */
-	cs_option(*handle, CS_OPT_DETAIL, CS_OPT_ON);
-
-	return 0;
-}
-#endif
-
 #ifdef HAVE_LIBCAPSTONE_SUPPORT
 static void print_capstone_detail(cs_insn *insn, char *buf, size_t len,
 				  struct annotate_args *args, u64 addr)
@@ -309,6 +282,7 @@ int symbol__disassemble_capstone(const char *filename __maybe_unused,
 	cs_insn *insn = NULL;
 	char disasm_buf[512];
 	struct disasm_line *dl;
+	bool disassembler_style = false;
 
 	if (args->options->objdump_path)
 		return -1;
@@ -333,7 +307,11 @@ int symbol__disassemble_capstone(const char *filename __maybe_unused,
 
 	annotation_line__add(&dl->al, &notes->src->source);
 
-	if (open_capstone_handle(args, is_64bit, &handle) < 0)
+	if (!args->options->disassembler_style ||
+	    !strcmp(args->options->disassembler_style, "att"))
+		disassembler_style = true;
+
+	if (capstone_init(maps__machine(args->ms.maps), &handle, is_64bit, disassembler_style) < 0)
 		goto err;
 
 	needs_cs_close = true;
Signature · 1 line
2.48.0.rc2.279.g1de40edade-goog
author reviewer
#6 lore raw

PATCH v2 6/17 perf capstone: Support for dlopen-ing libcapstone.so

In reply to Ian Rogers

If perf wasn't built against libcapstone, no HAVE_LIBCAPSTONE_SUPPORT, support dlopen-ing libcapstone.so and then calling the necessary functions by looking them up using dlsym. Reverse engineer the types in the API using pahole, adding only what's used in the perf code or necessary for the sake of struct size and alignment.

---
 tools/perf/util/capstone.c | 287 ++++++++++++++++++++++++++++++++-----
 1 file changed, 248 insertions(+), 39 deletions(-)
tools/perf/util/capstone.c +248 -39
index c9845e4d8781..8d65c7a55a8b 100644
@@ -11,19 +11,249 @@
 #include "print_insn.h"
 #include "symbol.h"
 #include "thread.h"
+#include <dlfcn.h>
 #include <fcntl.h>
+#include <inttypes.h>
 #include <string.h>
 
 #ifdef HAVE_LIBCAPSTONE_SUPPORT
 #include <capstone/capstone.h>
+#else
+typedef size_t csh;
+enum cs_arch {
+	CS_ARCH_ARM = 0,
+	CS_ARCH_ARM64 = 1,
+	CS_ARCH_X86 = 3,
+	CS_ARCH_SYSZ = 6,
+};
+enum cs_mode {
+	CS_MODE_ARM = 0,
+	CS_MODE_32 = 1 << 2,
+	CS_MODE_64 = 1 << 3,
+	CS_MODE_V8 = 1 << 6,
+	CS_MODE_BIG_ENDIAN = 1 << 31,
+};
+enum cs_opt_type {
+	CS_OPT_SYNTAX = 1,
+	CS_OPT_DETAIL = 2,
+};
+enum cs_opt_value {
+	CS_OPT_SYNTAX_ATT = 2,
+	CS_OPT_ON = 3,
+};
+enum cs_err {
+	CS_ERR_OK = 0,
+	CS_ERR_HANDLE = 3,
+};
+enum x86_op_type {
+	X86_OP_IMM = 2,
+	X86_OP_MEM = 3,
+};
+enum x86_reg {
+	X86_REG_RIP = 41,
+};
+typedef int32_t x86_avx_bcast;
+struct x86_op_mem {
+	enum x86_reg segment;
+	enum x86_reg base;
+	enum x86_reg index;
+	int scale;
+	int64_t disp;
+};
+
+struct cs_x86_op {
+	enum x86_op_type type;
+	union {
+		enum x86_reg  reg;
+		int64_t imm;
+		struct x86_op_mem mem;
+	};
+	uint8_t size;
+	uint8_t access;
+	x86_avx_bcast avx_bcast;
+	bool avx_zero_opmask;
+};
+struct cs_x86_encoding {
+	uint8_t modrm_offset;
+	uint8_t disp_offset;
+	uint8_t disp_size;
+	uint8_t imm_offset;
+	uint8_t imm_size;
+};
+typedef int32_t  x86_xop_cc;
+typedef int32_t  x86_sse_cc;
+typedef int32_t  x86_avx_cc;
+typedef int32_t  x86_avx_rm;
+struct cs_x86 {
+	uint8_t prefix[4];
+	uint8_t opcode[4];
+	uint8_t rex;
+	uint8_t addr_size;
+	uint8_t modrm;
+	uint8_t sib;
+	int64_t disp;
+	enum x86_reg sib_index;
+	int8_t sib_scale;
+	enum x86_reg sib_base;
+	x86_xop_cc xop_cc;
+	x86_sse_cc sse_cc;
+	x86_avx_cc avx_cc;
+	bool avx_sae;
+	x86_avx_rm avx_rm;
+	union {
+		uint64_t eflags;
+		uint64_t fpu_flags;
+	};
+	uint8_t op_count;
+	struct cs_x86_op operands[8];
+	struct cs_x86_encoding encoding;
+};
+struct cs_detail {
+	uint16_t regs_read[12];
+	uint8_t regs_read_count;
+	uint16_t regs_write[20];
+	uint8_t regs_write_count;
+	uint8_t groups[8];
+	uint8_t groups_count;
+
+	union {
+		struct cs_x86 x86;
+	};
+};
+struct cs_insn {
+	unsigned int id;
+	uint64_t address;
+	uint16_t size;
+	uint8_t bytes[16];
+	char mnemonic[32];
+	char op_str[160];
+	struct cs_detail *detail;
+};
+#endif
+
+#ifndef HAVE_LIBCAPSTONE_SUPPORT
+static void *perf_cs_dll_handle(void)
+{
+	static bool dll_handle_init;
+	static void *dll_handle;
+
+	if (!dll_handle_init) {
+		dll_handle_init = true;
+		dll_handle = dlopen("libcapstone.so", RTLD_LAZY);
+		if (!dll_handle)
+			pr_debug("dlopen failed for libcapstone.so\n");
+	}
+	return dll_handle;
+}
+#endif
+
+static enum cs_err perf_cs_open(enum cs_arch arch, enum cs_mode mode, csh *handle)
+{
+#ifdef HAVE_LIBCAPSTONE_SUPPORT
+	return cs_open(arch, mode, handle);
+#else
+	static bool fn_init;
+	static enum cs_err (*fn)(enum cs_arch arch, enum cs_mode mode, csh *handle);
+
+	if (!fn_init) {
+		fn = dlsym(perf_cs_dll_handle(), "cs_open");
+		if (!fn)
+			pr_debug("dlsym failed for cs_open\n");
+		fn_init = true;
+	}
+	if (!fn)
+		return CS_ERR_HANDLE;
+	return fn(arch, mode, handle);
+#endif
+}
+
+static enum cs_err perf_cs_option(csh handle, enum cs_opt_type type, size_t value)
+{
+#ifdef HAVE_LIBCAPSTONE_SUPPORT
+	return cs_option(handle, type, value);
+#else
+	static bool fn_init;
+	static enum cs_err (*fn)(csh handle, enum cs_opt_type type, size_t value);
+
+	if (!fn_init) {
+		fn = dlsym(perf_cs_dll_handle(), "cs_option");
+		if (!fn)
+			pr_debug("dlsym failed for cs_option\n");
+		fn_init = true;
+	}
+	if (!fn)
+		return CS_ERR_HANDLE;
+	return fn(handle, type, value);
+#endif
+}
+
+static size_t perf_cs_disasm(csh handle, const uint8_t *code, size_t code_size,
+			uint64_t address, size_t count, struct cs_insn **insn)
+{
+#ifdef HAVE_LIBCAPSTONE_SUPPORT
+	return cs_disasm(handle, code, code_size, address, count, insn);
+#else
+	static bool fn_init;
+	static enum cs_err (*fn)(csh handle, const uint8_t *code, size_t code_size,
+				 uint64_t address, size_t count, struct cs_insn **insn);
+
+	if (!fn_init) {
+		fn = dlsym(perf_cs_dll_handle(), "cs_disasm");
+		if (!fn)
+			pr_debug("dlsym failed for cs_disasm\n");
+		fn_init = true;
+	}
+	if (!fn)
+		return CS_ERR_HANDLE;
+	return fn(handle, code, code_size, address, count, insn);
 #endif
+}
 
+static void perf_cs_free(struct cs_insn *insn, size_t count)
+{
 #ifdef HAVE_LIBCAPSTONE_SUPPORT
+	cs_free(insn, count);
+#else
+	static bool fn_init;
+	static void (*fn)(struct cs_insn *insn, size_t count);
+
+	if (!fn_init) {
+		fn = dlsym(perf_cs_dll_handle(), "cs_free");
+		if (!fn)
+			pr_debug("dlsym failed for cs_free\n");
+		fn_init = true;
+	}
+	if (!fn)
+		return;
+	fn(insn, count);
+#endif
+}
+
+static enum cs_err perf_cs_close(csh *handle)
+{
+#ifdef HAVE_LIBCAPSTONE_SUPPORT
+	return cs_close(handle);
+#else
+	static bool fn_init;
+	static enum cs_err (*fn)(csh *handle);
+
+	if (!fn_init) {
+		fn = dlsym(perf_cs_dll_handle(), "cs_close");
+		if (!fn)
+			pr_debug("dlsym failed for cs_close\n");
+		fn_init = true;
+	}
+	if (!fn)
+		return CS_ERR_HANDLE;
+	return fn(handle);
+#endif
+}
+
 static int capstone_init(struct machine *machine, csh *cs_handle, bool is64,
 			 bool disassembler_style)
 {
-	cs_arch arch;
-	cs_mode mode;
+	enum cs_arch arch;
+	enum cs_mode mode;
 
 	if (machine__is(machine, "x86_64") && is64) {
 		arch = CS_ARCH_X86;
@@ -44,7 +274,7 @@ static int capstone_init(struct machine *machine, csh *cs_handle, bool is64,
 		return -1;
 	}
 
-	if (cs_open(arch, mode, cs_handle) != CS_ERR_OK) {
+	if (perf_cs_open(arch, mode, cs_handle) != CS_ERR_OK) {
 		pr_warning_once("cs_open failed\n");
 		return -1;
 	}
@@ -56,27 +286,25 @@ static int capstone_init(struct machine *machine, csh *cs_handle, bool is64,
 		 * is set via annotation args
 		 */
 		if (disassembler_style)
-			cs_option(*cs_handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_ATT);
+			perf_cs_option(*cs_handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_ATT);
 		/*
 		 * Resolving address operands to symbols is implemented
 		 * on x86 by investigating instruction details.
 		 */
-		cs_option(*cs_handle, CS_OPT_DETAIL, CS_OPT_ON);
+		perf_cs_option(*cs_handle, CS_OPT_DETAIL, CS_OPT_ON);
 	}
 
 	return 0;
 }
-#endif
 
-#ifdef HAVE_LIBCAPSTONE_SUPPORT
-static size_t print_insn_x86(struct thread *thread, u8 cpumode, cs_insn *insn,
+static size_t print_insn_x86(struct thread *thread, u8 cpumode, struct cs_insn *insn,
 			     int print_opts, FILE *fp)
 {
 	struct addr_location al;
 	size_t printed = 0;
 
 	if (insn->detail && insn->detail->x86.op_count == 1) {
-		cs_x86_op *op = &insn->detail->x86.operands[0];
+		struct cs_x86_op *op = &insn->detail->x86.operands[0];
 
 		addr_location__init(&al);
 		if (op->type == X86_OP_IMM &&
@@ -94,7 +322,6 @@ static size_t print_insn_x86(struct thread *thread, u8 cpumode, cs_insn *insn,
 	printed += fprintf(fp, "%s %s", insn[0].mnemonic, insn[0].op_str);
 	return printed;
 }
-#endif
 
 
 ssize_t capstone__fprintf_insn_asm(struct machine *machine __maybe_unused,
@@ -105,9 +332,8 @@ ssize_t capstone__fprintf_insn_asm(struct machine *machine __maybe_unused,
 				   uint64_t ip __maybe_unused, int *lenp __maybe_unused,
 				   int print_opts __maybe_unused, FILE *fp __maybe_unused)
 {
-#ifdef HAVE_LIBCAPSTONE_SUPPORT
 	size_t printed;
-	cs_insn *insn;
+	struct cs_insn *insn;
 	csh cs_handle;
 	size_t count;
 	int ret;
@@ -117,7 +343,7 @@ ssize_t capstone__fprintf_insn_asm(struct machine *machine __maybe_unused,
 	if (ret < 0)
 		return ret;
 
-	count = cs_disasm(cs_handle, code, code_size, ip, 1, &insn);
+	count = perf_cs_disasm(cs_handle, code, code_size, ip, 1, &insn);
 	if (count > 0) {
 		if (machine__normalized_is(machine, "x86"))
 			printed = print_insn_x86(thread, cpumode, &insn[0], print_opts, fp);
@@ -125,20 +351,16 @@ ssize_t capstone__fprintf_insn_asm(struct machine *machine __maybe_unused,
 			printed = fprintf(fp, "%s %s", insn[0].mnemonic, insn[0].op_str);
 		if (lenp)
 			*lenp = insn->size;
-		cs_free(insn, count);
+		perf_cs_free(insn, count);
 	} else {
 		printed = -1;
 	}
 
-	cs_close(&cs_handle);
+	perf_cs_close(&cs_handle);
 	return printed;
-#else
-	return -1;
-#endif
 }
 
-#ifdef HAVE_LIBCAPSTONE_SUPPORT
-static void print_capstone_detail(cs_insn *insn, char *buf, size_t len,
+static void print_capstone_detail(struct cs_insn *insn, char *buf, size_t len,
 				  struct annotate_args *args, u64 addr)
 {
 	int i;
@@ -153,7 +375,7 @@ static void print_capstone_detail(cs_insn *insn, char *buf, size_t len,
 		return;
 
 	for (i = 0; i < insn->detail->x86.op_count; i++) {
-		cs_x86_op *op = &insn->detail->x86.operands[i];
+		struct cs_x86_op *op = &insn->detail->x86.operands[i];
 		u64 orig_addr;
 
 		if (op->type != X86_OP_MEM)
@@ -194,9 +416,7 @@ static void print_capstone_detail(cs_insn *insn, char *buf, size_t len,
 		break;
 	}
 }
-#endif
 
-#ifdef HAVE_LIBCAPSTONE_SUPPORT
 struct find_file_offset_data {
 	u64 ip;
 	u64 offset;
@@ -213,9 +433,7 @@ static int find_file_offset(u64 start, u64 len, u64 pgoff, void *arg)
 	}
 	return 0;
 }
-#endif
 
-#ifdef HAVE_LIBCAPSTONE_SUPPORT
 static u8 *
 read_symbol(const char *filename, struct map *map, struct symbol *sym,
 	    u64 *len, bool *is_64bit)
@@ -262,13 +480,11 @@ read_symbol(const char *filename, struct map *map, struct symbol *sym,
 	free(buf);
 	return NULL;
 }
-#endif
 
 int symbol__disassemble_capstone(const char *filename __maybe_unused,
 				 struct symbol *sym __maybe_unused,
 				 struct annotate_args *args __maybe_unused)
 {
-#ifdef HAVE_LIBCAPSTONE_SUPPORT
 	struct annotation *notes = symbol__annotation(sym);
 	struct map *map = args->ms.map;
 	u64 start = map__rip_2objdump(map, sym->start);
@@ -279,7 +495,7 @@ int symbol__disassemble_capstone(const char *filename __maybe_unused,
 	bool needs_cs_close = false;
 	u8 *buf = NULL;
 	csh handle;
-	cs_insn *insn = NULL;
+	struct cs_insn *insn = NULL;
 	char disasm_buf[512];
 	struct disasm_line *dl;
 	bool disassembler_style = false;
@@ -316,7 +532,7 @@ int symbol__disassemble_capstone(const char *filename __maybe_unused,
 
 	needs_cs_close = true;
 
-	free_count = count = cs_disasm(handle, buf, len, start, len, &insn);
+	free_count = count = perf_cs_disasm(handle, buf, len, start, len, &insn);
 	for (i = 0, offset = 0; i < count; i++) {
 		int printed;
 
@@ -355,9 +571,9 @@ int symbol__disassemble_capstone(const char *filename __maybe_unused,
 
 out:
 	if (needs_cs_close) {
-		cs_close(&handle);
+		perf_cs_close(&handle);
 		if (free_count > 0)
-			cs_free(insn, free_count);
+			perf_cs_free(insn, free_count);
 	}
 	free(buf);
 	return count < 0 ? count : 0;
@@ -377,16 +593,12 @@ int symbol__disassemble_capstone(const char *filename __maybe_unused,
 	}
 	count = -1;
 	goto out;
-#else
-	return -1;
-#endif
 }
 
 int symbol__disassemble_capstone_powerpc(const char *filename __maybe_unused,
 					 struct symbol *sym __maybe_unused,
 					 struct annotate_args *args __maybe_unused)
 {
-#ifdef HAVE_LIBCAPSTONE_SUPPORT
 	struct annotation *notes = symbol__annotation(sym);
 	struct map *map = args->ms.map;
 	struct dso *dso = map__dso(map);
@@ -499,7 +711,7 @@ int symbol__disassemble_capstone_powerpc(const char *filename __maybe_unused,
 
 out:
 	if (needs_cs_close)
-		cs_close(&handle);
+		perf_cs_close(&handle);
 	free(buf);
 	return count < 0 ? count : 0;
 
@@ -508,7 +720,4 @@ int symbol__disassemble_capstone_powerpc(const char *filename __maybe_unused,
 		close(fd);
 	count = -1;
 	goto out;
-#else
-	return -1;
-#endif
 }
Signature · 1 line
2.48.0.rc2.279.g1de40edade-goog
author reviewer
#7 lore raw

PATCH v2 7/17 perf llvm: Support for dlopen-ing libLLVM.so

In reply to Ian Rogers

If perf wasn't built against libLLVM, no HAVE_LIBLLVM_SUPPORT, support dlopen-ing libLLVM.so and then calling the necessary functions by looking them up using dlsym. As the C++ code in llvm-c-helpers used for addr2line is problematic to call using dlsym, build that C++ code against libLLVM.so as a separate shared object, and support dynamic loading of it. This build option is enabled with LIBLLVM_DYNAMIC=1

---
 tools/perf/Makefile.config         |  13 ++
 tools/perf/Makefile.perf           |  23 ++-
 tools/perf/tests/make              |   2 +
 tools/perf/util/Build              |   2 +-
 tools/perf/util/llvm-c-helpers.cpp | 113 +++++++++++-
 tools/perf/util/llvm.c             | 271 +++++++++++++++++++++++++----
 6 files changed, 386 insertions(+), 38 deletions(-)
tools/perf/Makefile.config +13 -0
index cd773fbbc176..5c2814acc5d5 100644
@@ -963,6 +963,19 @@ ifndef NO_LIBLLVM
     NO_LIBLLVM := 1
   endif
 endif
+ifdef LIBLLVM_DYNAMIC
+  ifndef NO_LIBLLVM
+    $(error LIBLLVM_DYNAMIC should be used with NO_LIBLLVM)
+  endif
+  $(call feature_check,llvm-perf)
+  ifneq ($(feature-llvm-perf), 1)
+    $(warning LIBLLVM_DYNAMIC requires libLLVM.so which wasn't feature detected)
+  endif
+  CFLAGS += -DHAVE_LIBLLVM_DYNAMIC
+  CFLAGS += $(shell $(LLVM_CONFIG) --cflags)
+  CXXFLAGS += -DHAVE_LIBLLVM_DYNAMIC
+  CXXFLAGS += $(shell $(LLVM_CONFIG) --cxxflags)
+endif
 
 ifndef NO_DEMANGLE
   $(call feature_check,cxa-demangle)
tools/perf/Makefile.perf +22 -1
index 55d6ce9ea52f..eae77f6af59d 100644
@@ -456,6 +456,12 @@ ifndef NO_JVMTI
 PROGRAMS += $(OUTPUT)$(LIBJVMTI)
 endif
 
+LIBPERF_LLVM = libperf-llvm.so
+
+ifdef LIBLLVM_DYNAMIC
+PROGRAMS += $(OUTPUT)$(LIBPERF_LLVM)
+endif
+
 DLFILTERS := dlfilter-test-api-v0.so dlfilter-test-api-v2.so dlfilter-show-cycles.so
 DLFILTERS := $(patsubst %,$(OUTPUT)dlfilters/%,$(DLFILTERS))
 
@@ -1019,6 +1025,16 @@ $(LIBSYMBOL)-clean:
 	$(call QUIET_CLEAN, libsymbol)
 	$(Q)$(RM) -r -- $(LIBSYMBOL_OUTPUT)
 
+ifdef LIBLLVM_DYNAMIC
+LIBPERF_LLVM_CXXFLAGS := $(call filter-out,-DHAVE_LIBLLVM_DYNAMIC,$(CXXFLAGS)) -DHAVE_LIBLLVM_SUPPORT
+LIBPERF_LLVM_LIBS = -L$(shell $(LLVM_CONFIG) --libdir) $(LIBLLVM) -lstdc++
+
+$(OUTPUT)$(LIBPERF_LLVM): util/llvm-c-helpers.cpp
+	$(QUIET_LINK)$(CXX) $(LIBPERF_LLVM_CXXFLAGS) $(LIBPERF_LLVM_LIBS) -shared -o $@ $<
+
+$(OUTPUT)perf: $(OUTPUT)$(LIBPERF_LLVM)
+endif
+
 help:
 	@echo 'Perf make targets:'
 	@echo '  doc		- make *all* documentation (see below)'
@@ -1120,6 +1136,11 @@ ifndef NO_JVMTI
 	$(call QUIET_INSTALL, $(LIBJVMTI)) \
 		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(libdir_SQ)'; \
 		$(INSTALL) $(OUTPUT)$(LIBJVMTI) '$(DESTDIR_SQ)$(libdir_SQ)';
+endif
+ifdef LIBLLVM_DYNAMIC
+	$(call QUIET_INSTALL, $(LIBPERF_LLVM)) \
+		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(libdir_SQ)'; \
+		$(INSTALL) $(OUTPUT)$(LIBPERF_LLVM) '$(DESTDIR_SQ)$(libdir_SQ)';
 endif
 	$(call QUIET_INSTALL, libexec) \
 		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)'
@@ -1301,7 +1322,7 @@ clean:: $(LIBAPI)-clean $(LIBBPF)-clean $(LIBSUBCMD)-clean $(LIBSYMBOL)-clean $(
 		-name '\.*.cmd' -delete -o -name '\.*.d' -delete -o -name '*.shellcheck_log' -delete
 	$(Q)$(RM) $(OUTPUT).config-detected
 	$(call QUIET_CLEAN, core-progs) $(RM) $(ALL_PROGRAMS) perf perf-read-vdso32 \
-		perf-read-vdsox32 $(OUTPUT)$(LIBJVMTI).so
+		perf-read-vdsox32 $(OUTPUT)$(LIBJVMTI) $(OUTPUT)$(LIBPERF_LLVM)
 	$(call QUIET_CLEAN, core-gen)   $(RM)  *.spec *.pyc *.pyo */*.pyc */*.pyo \
 		$(OUTPUT)common-cmds.h TAGS tags cscope* $(OUTPUT)PERF-VERSION-FILE \
 		$(OUTPUT)FEATURE-DUMP $(OUTPUT)util/*-bison* $(OUTPUT)util/*-flex* \
tools/perf/tests/make +2 -0
index 0ee94caf9ec1..44d76eacce49 100644
@@ -93,6 +93,7 @@ make_libbpf_dynamic := LIBBPF_DYNAMIC=1
 make_no_libbpf_DEBUG := NO_LIBBPF=1 DEBUG=1
 make_no_libcrypto   := NO_LIBCRYPTO=1
 make_no_libllvm     := NO_LIBLLVM=1
+make_libllvm_dynamic := NO_LIBLLVM=1 LIBLLVM_DYNAMIC=1
 make_with_babeltrace:= LIBBABELTRACE=1
 make_with_coresight := CORESIGHT=1
 make_no_sdt	    := NO_SDT=1
@@ -162,6 +163,7 @@ run += make_no_libbpf
 run += make_no_libbpf_DEBUG
 run += make_no_libcrypto
 run += make_no_libllvm
+run += make_libllvm_dynamic
 run += make_no_sdt
 run += make_no_syscall_tbl
 run += make_with_babeltrace
tools/perf/util/Build +1 -1
index 6fe0b5882c97..eb00c599e179 100644
@@ -27,6 +27,7 @@ perf-util-y += find_bit.o
 perf-util-y += get_current_dir_name.o
 perf-util-y += levenshtein.o
 perf-util-y += llvm.o
+perf-util-y += llvm-c-helpers.o
 perf-util-y += mmap.o
 perf-util-y += memswap.o
 perf-util-y += parse-events.o
@@ -239,7 +240,6 @@ perf-util-$(CONFIG_CXX_DEMANGLE) += demangle-cxx.o
 perf-util-y += demangle-ocaml.o
 perf-util-y += demangle-java.o
 perf-util-y += demangle-rust.o
-perf-util-$(CONFIG_LIBLLVM) += llvm-c-helpers.o
 
 ifdef CONFIG_JITDUMP
 perf-util-$(CONFIG_LIBELF) += jitdump.o
tools/perf/util/llvm-c-helpers.cpp +107 -6

192 lines not on this page · raw message

tools/perf/util/llvm.c +241 -30

372 lines not on this page · raw message

Signature · 1 line
2.48.0.rc2.279.g1de40edade-goog
author reviewer
#8 lore raw

PATCH v2 8/17 perf llvm: Mangle libperf-llvm.so function names

In reply to Ian Rogers

For a function like llvm_addr2line having the libperf-llvm.so exported symbol named llvm_addr2line meant that the perf llvm_addr2line could sometimes erroneously be returned. This led to infinite recursion and eventual stack overflow. To avoid this conflict add a new BUILDING_PERF_LLVMSO when libperf-llvm.so is being built and use it to alter the behavior of MANGLE_PERF_LLVM_API, a macro that prefixes the name when libperf-llvm.so is being built. The prefixed named avoids the name collision.

---
 tools/perf/Makefile.perf           |  3 ++-
 tools/perf/util/llvm-c-helpers.cpp | 29 ++++++++++++++++++-----------
 tools/perf/util/llvm-c-helpers.h   | 24 ++++++++++++++++--------
 3 files changed, 36 insertions(+), 20 deletions(-)
tools/perf/Makefile.perf +2 -1
index eae77f6af59d..a2886abd4f02 100644
@@ -1026,7 +1026,8 @@ $(LIBSYMBOL)-clean:
 	$(Q)$(RM) -r -- $(LIBSYMBOL_OUTPUT)
 
 ifdef LIBLLVM_DYNAMIC
-LIBPERF_LLVM_CXXFLAGS := $(call filter-out,-DHAVE_LIBLLVM_DYNAMIC,$(CXXFLAGS)) -DHAVE_LIBLLVM_SUPPORT
+LIBPERF_LLVM_CXXFLAGS := $(call filter-out,-DHAVE_LIBLLVM_DYNAMIC,$(CXXFLAGS))
+LIBPERF_LLVM_CXXFLAGS += -DHAVE_LIBLLVM_SUPPORT -DBUILDING_PERF_LLVMSO
 LIBPERF_LLVM_LIBS = -L$(shell $(LLVM_CONFIG) --libdir) $(LIBLLVM) -lstdc++
 
 $(OUTPUT)$(LIBPERF_LLVM): util/llvm-c-helpers.cpp
tools/perf/util/llvm-c-helpers.cpp +18 -11
index 5a6f76e6b705..8cea380be5c2 100644
@@ -99,10 +99,12 @@ static int extract_file_and_line(const DILineInfo &line_info, char **file,
 #endif
 
 extern "C"
-int llvm_addr2line(const char *dso_name __maybe_unused, u64 addr __maybe_unused,
-		   char **file __maybe_unused, unsigned int *line __maybe_unused,
-		   bool unwind_inlines __maybe_unused,
-		   llvm_a2l_frame **inline_frames __maybe_unused)
+int MANGLE_PERF_LLVM_API(llvm_addr2line)(const char *dso_name __maybe_unused,
+                                         u64 addr __maybe_unused,
+                                         char **file __maybe_unused,
+                                         unsigned int *line __maybe_unused,
+                                         bool unwind_inlines __maybe_unused,
+                                         llvm_a2l_frame **inline_frames __maybe_unused)
 {
 #if defined(HAVE_LIBLLVM_SUPPORT) && !defined(HAVE_LIBLLVM_DYNAMIC)
 	LLVMSymbolizer *symbolizer = get_symbolizer();
@@ -177,7 +179,8 @@ int llvm_addr2line(const char *dso_name __maybe_unused, u64 addr __maybe_unused,
 		if (!handle)
 			return 0;
 
-		fn = reinterpret_cast<decltype(fn)>(dlsym(handle, "llvm_addr2line"));
+		fn = reinterpret_cast<decltype(fn)>(
+			dlsym(handle, MANGLE_PERF_LLVM_API_STR(llvm_addr2line)));
 		if (!fn)
 			pr_debug("dlsym failed for llvm_addr2line\n");
 		fn_init = true;
@@ -215,8 +218,9 @@ make_symbol_relative_string(struct dso *dso, const char *sym_name,
 #endif
 
 extern "C"
-char *llvm_name_for_code(struct dso *dso __maybe_unused, const char *dso_name __maybe_unused,
-			 u64 addr __maybe_unused)
+char *MANGLE_PERF_LLVM_API(llvm_name_for_code)(struct dso *dso __maybe_unused,
+					       const char *dso_name __maybe_unused,
+					       u64 addr __maybe_unused)
 {
 #if defined(HAVE_LIBLLVM_SUPPORT) && !defined(HAVE_LIBLLVM_DYNAMIC)
 	LLVMSymbolizer *symbolizer = get_symbolizer();
@@ -242,7 +246,8 @@ char *llvm_name_for_code(struct dso *dso __maybe_unused, const char *dso_name __
 		if (!handle)
 			return NULL;
 
-		fn = reinterpret_cast<decltype(fn)>(dlsym(handle, "llvm_name_for_code"));
+		fn = reinterpret_cast<decltype(fn)>(
+			dlsym(handle, MANGLE_PERF_LLVM_API_STR(llvm_name_for_code)));
 		if (!fn)
 			pr_debug("dlsym failed for llvm_name_for_code\n");
 		fn_init = true;
@@ -256,8 +261,9 @@ char *llvm_name_for_code(struct dso *dso __maybe_unused, const char *dso_name __
 }
 
 extern "C"
-char *llvm_name_for_data(struct dso *dso __maybe_unused, const char *dso_name __maybe_unused,
-			 u64 addr __maybe_unused)
+char *MANGLE_PERF_LLVM_API(llvm_name_for_data)(struct dso *dso __maybe_unused,
+					       const char *dso_name __maybe_unused,
+					       u64 addr __maybe_unused)
 {
 #if defined(HAVE_LIBLLVM_SUPPORT) && !defined(HAVE_LIBLLVM_DYNAMIC)
 	LLVMSymbolizer *symbolizer = get_symbolizer();
@@ -283,7 +289,8 @@ char *llvm_name_for_data(struct dso *dso __maybe_unused, const char *dso_name __
 		if (!handle)
 			return NULL;
 
-		fn = reinterpret_cast<decltype(fn)>(dlsym(handle, "llvm_name_for_data"));
+		fn = reinterpret_cast<decltype(fn)>(
+			dlsym(handle, MANGLE_PERF_LLVM_API_STR(llvm_name_for_data)));
 		if (!fn)
 			pr_debug("dlsym failed for llvm_name_for_data\n");
 		fn_init = true;
tools/perf/util/llvm-c-helpers.h +16 -8
index d2b99637a28a..cfcfd540cdae 100644
@@ -13,6 +13,14 @@
 extern "C" {
 #endif
 
+/* Support name mangling so that libperf_llvm.so's names don't match those in perf. */
+#ifdef BUILDING_PERF_LLVMSO
+#define MANGLE_PERF_LLVM_API(x) PERF_LLVM_SO_ ## x
+#else
+#define MANGLE_PERF_LLVM_API(x) x
+#endif
+#define MANGLE_PERF_LLVM_API_STR(x) "PERF_LLVM_SO_" #x
+
 struct dso;
 
 struct llvm_a2l_frame {
@@ -37,12 +45,12 @@ struct llvm_a2l_frame {
  * a newly allocated array with that length. The caller is then responsible
  * for freeing both the strings and the array itself.
  */
-int llvm_addr2line(const char* dso_name,
-                   u64 addr,
-                   char** file,
-                   unsigned int* line,
-                   bool unwind_inlines,
-                   struct llvm_a2l_frame** inline_frames);
+int MANGLE_PERF_LLVM_API(llvm_addr2line)(const char *dso_name,
+					 u64 addr,
+					 char **file,
+					 unsigned int *line,
+					 bool unwind_inlines,
+					 struct llvm_a2l_frame **inline_frames);
 
 /*
  * Simple symbolizers for addresses; will convert something like
@@ -50,8 +58,8 @@ int llvm_addr2line(const char* dso_name,
  *
  * The returned value must be freed by the caller, with free().
  */
-char *llvm_name_for_code(struct dso *dso, const char *dso_name, u64 addr);
-char *llvm_name_for_data(struct dso *dso, const char *dso_name, u64 addr);
+char *MANGLE_PERF_LLVM_API(llvm_name_for_code)(struct dso *dso, const char *dso_name, u64 addr);
+char *MANGLE_PERF_LLVM_API(llvm_name_for_data)(struct dso *dso, const char *dso_name, u64 addr);
 
 #ifdef __cplusplus
 }
Signature · 1 line
2.48.0.rc2.279.g1de40edade-goog
author reviewer
#9 lore raw

PATCH v2 9/17 perf dso: Move read_symbol from llvm/capstone to dso

In reply to Ian Rogers

Move the read_symbol function to dso.h, make the return type const and add a mutable out_buf out parameter. In future changes this will allow a code pointer to be returned without necessary allocating memory.

---
 tools/perf/util/capstone.c | 64 +++++-----------------------
 tools/perf/util/dso.c      | 67 +++++++++++++++++++++++++++++
 tools/perf/util/dso.h      |  4 ++
 tools/perf/util/llvm.c     | 87 +++++++-------------------------------
 4 files changed, 97 insertions(+), 125 deletions(-)
tools/perf/util/capstone.c +11 -53
index 8d65c7a55a8b..f103988184c8 100644
@@ -434,66 +434,23 @@ static int find_file_offset(u64 start, u64 len, u64 pgoff, void *arg)
 	return 0;
 }
 
-static u8 *
-read_symbol(const char *filename, struct map *map, struct symbol *sym,
-	    u64 *len, bool *is_64bit)
-{
-	struct dso *dso = map__dso(map);
-	struct nscookie nsc;
-	u64 start = map__rip_2objdump(map, sym->start);
-	u64 end = map__rip_2objdump(map, sym->end);
-	int fd, count;
-	u8 *buf = NULL;
-	struct find_file_offset_data data = {
-		.ip = start,
-	};
-
-	*is_64bit = false;
-
-	nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
-	fd = open(filename, O_RDONLY);
-	nsinfo__mountns_exit(&nsc);
-	if (fd < 0)
-		return NULL;
-
-	if (file__read_maps(fd, /*exe=*/true, find_file_offset, &data,
-			    is_64bit) == 0)
-		goto err;
-
-	*len = end - start;
-	buf = malloc(*len);
-	if (buf == NULL)
-		goto err;
-
-	count = pread(fd, buf, *len, data.offset);
-	close(fd);
-	fd = -1;
-
-	if ((u64)count != *len)
-		goto err;
-
-	return buf;
-
-err:
-	if (fd >= 0)
-		close(fd);
-	free(buf);
-	return NULL;
-}
-
 int symbol__disassemble_capstone(const char *filename __maybe_unused,
 				 struct symbol *sym __maybe_unused,
 				 struct annotate_args *args __maybe_unused)
 {
 	struct annotation *notes = symbol__annotation(sym);
 	struct map *map = args->ms.map;
+	struct dso *dso = map__dso(map);
 	u64 start = map__rip_2objdump(map, sym->start);
-	u64 len;
 	u64 offset;
 	int i, count, free_count;
 	bool is_64bit = false;
 	bool needs_cs_close = false;
-	u8 *buf = NULL;
+	/* Malloc-ed buffer containing instructions read from disk. */
+	u8 *code_buf = NULL;
+	/* Pointer to code to be disassembled. */
+	const u8 *buf;
+	u64 buf_len;
 	csh handle;
 	struct cs_insn *insn = NULL;
 	char disasm_buf[512];
@@ -503,7 +460,8 @@ int symbol__disassemble_capstone(const char *filename __maybe_unused,
 	if (args->options->objdump_path)
 		return -1;
 
-	buf = read_symbol(filename, map, sym, &len, &is_64bit);
+	buf = dso__read_symbol(dso, filename, map, sym,
+			       &code_buf, &buf_len, &is_64bit);
 	if (buf == NULL)
 		return -1;
 
@@ -532,7 +490,7 @@ int symbol__disassemble_capstone(const char *filename __maybe_unused,
 
 	needs_cs_close = true;
 
-	free_count = count = perf_cs_disasm(handle, buf, len, start, len, &insn);
+	free_count = count = perf_cs_disasm(handle, buf, buf_len, start, buf_len, &insn);
 	for (i = 0, offset = 0; i < count; i++) {
 		int printed;
 
@@ -556,7 +514,7 @@ int symbol__disassemble_capstone(const char *filename __maybe_unused,
 	}
 
 	/* It failed in the middle: probably due to unknown instructions */
-	if (offset != len) {
+	if (offset != buf_len) {
 		struct list_head *list = &notes->src->source;
 
 		/* Discard all lines and fallback to objdump */
@@ -575,7 +533,7 @@ int symbol__disassemble_capstone(const char *filename __maybe_unused,
 		if (free_count > 0)
 			perf_cs_free(insn, free_count);
 	}
-	free(buf);
+	free(code_buf);
 	return count < 0 ? count : 0;
 
 err:
tools/perf/util/dso.c +67 -0

70 lines not on this page · raw message

tools/perf/util/dso.h +4 -0
index bb8e8f444054..734e3a3d95f0 100644
@@ -828,4 +828,8 @@ u64 dso__findnew_global_type(struct dso *dso, u64 addr, u64 offset);
 bool perf_pid_map_tid(const char *dso_name, int *tid);
 bool is_perf_pid_map_name(const char *dso_name);
 
+const u8 *dso__read_symbol(struct dso *dso, const char *symfs_filename,
+			   const struct map *map, const struct symbol *sym,
+			   u8 **out_buf, u64 *out_buf_len, bool *is_64bit);
+
 #endif /* __PERF_DSO */
tools/perf/util/llvm.c +15 -72

122 lines not on this page · raw message

Signature · 1 line
2.48.0.rc2.279.g1de40edade-goog
author reviewer
#10 lore raw

PATCH v2 10/17 perf dso: Support BPF programs in dso__read_symbol

In reply to Ian Rogers

Set the buffer to the code in the BPF linear info. This enables BPF JIT code disassembly by LLVM and capstone. Move the disassmble_bpf calls to disassemble_objdump so that they are only called after falling back to the objdump option.

---
 tools/perf/util/disasm.c | 12 +++---
 tools/perf/util/dso.c    | 85 +++++++++++++++++++++++++---------------
 2 files changed, 60 insertions(+), 37 deletions(-)
tools/perf/util/disasm.c +7 -5
index a9cc588a3006..99b9c21e02b0 100644
@@ -1500,6 +1500,12 @@ static int symbol__disassemble_objdump(const char *filename, struct symbol *sym,
 	struct child_process objdump_process;
 	int err;
 
+	if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_PROG_INFO)
+		return symbol__disassemble_bpf(sym, args);
+
+	if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_IMAGE)
+		return symbol__disassemble_bpf_image(sym, args);
+
 	err = asprintf(&command,
 		 "%s %s%s --start-address=0x%016" PRIx64
 		 " --stop-address=0x%016" PRIx64
@@ -1681,11 +1687,7 @@ int symbol__disassemble(struct symbol *sym, struct annotate_args *args)
 
 	pr_debug("annotating [%p] %30s : [%p] %30s\n", dso, dso__long_name(dso), sym, sym->name);
 
-	if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_PROG_INFO) {
-		return symbol__disassemble_bpf(sym, args);
-	} else if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_IMAGE) {
-		return symbol__disassemble_bpf_image(sym, args);
-	} else if (dso__binary_type(dso) == DSO_BINARY_TYPE__NOT_FOUND) {
+	if (dso__binary_type(dso) == DSO_BINARY_TYPE__NOT_FOUND) {
 		return SYMBOL_ANNOTATE_ERRNO__COULDNT_DETERMINE_FILE_TYPE;
 	} else if (dso__is_kcore(dso)) {
 		kce.addr = map__rip_2objdump(map, sym->start);
tools/perf/util/dso.c +53 -32

101 lines not on this page · raw message

Signature · 1 line
2.48.0.rc2.279.g1de40edade-goog
author reviewer
#11 lore raw

PATCH v2 11/17 perf llvm: Disassemble cleanup

In reply to Ian Rogers

Move the 3 LLVM initialization routines to be called in a single init_llvm function that has its own bool to avoid repeated initialization. Reduce the scope of triplet and avoid copying strings for x86.

---
 tools/perf/util/llvm.c | 32 ++++++++++++++++++++------------
 1 file changed, 20 insertions(+), 12 deletions(-)
tools/perf/util/llvm.c +20 -12
index a0774373f0d6..a28f130c8951 100644
@@ -244,6 +244,17 @@ static void perf_LLVMDisasmDispose(LLVMDisasmContextRef context)
 #endif
 }
 
+static void init_llvm(void)
+{
+	static bool init;
+
+	if (!init) {
+		perf_LLVMInitializeAllTargetInfos();
+		perf_LLVMInitializeAllTargetMCs();
+		perf_LLVMInitializeAllDisassemblers();
+		init = true;
+	}
+}
 
 static void free_llvm_inline_frames(struct llvm_a2l_frame *inline_frames,
 				    int num_frames)
@@ -339,7 +350,6 @@ int symbol__disassemble_llvm(const char *filename, struct symbol *sym,
 	u64 buf_len;
 	u64 pc;
 	bool is_64bit;
-	char triplet[64];
 	char disasm_buf[2048];
 	size_t disasm_len;
 	struct disasm_line *dl;
@@ -352,27 +362,25 @@ int symbol__disassemble_llvm(const char *filename, struct symbol *sym,
 	if (args->options->objdump_path)
 		return -1;
 
-	perf_LLVMInitializeAllTargetInfos();
-	perf_LLVMInitializeAllTargetMCs();
-	perf_LLVMInitializeAllDisassemblers();
-
 	buf = dso__read_symbol(dso, filename, map, sym,
 			       &code_buf, &buf_len, &is_64bit);
 	if (buf == NULL)
 		return -1;
 
+	init_llvm();
 	if (arch__is(args->arch, "x86")) {
-		if (is_64bit)
-			scnprintf(triplet, sizeof(triplet), "x86_64-pc-linux");
-		else
-			scnprintf(triplet, sizeof(triplet), "i686-pc-linux");
+		const char *triplet = is_64bit ? "x86_64-pc-linux" : "i686-pc-linux";
+
+		disasm = perf_LLVMCreateDisasm(triplet, &storage, /*tag_type=*/0,
+					       /*get_op_info=*/NULL, symbol_lookup_callback);
 	} else {
+		char triplet[64];
+
 		scnprintf(triplet, sizeof(triplet), "%s-linux-gnu",
 			  args->arch->name);
+		disasm = perf_LLVMCreateDisasm(triplet, &storage, /*tag_type=*/0,
+					       /*get_op_info=*/NULL, symbol_lookup_callback);
 	}
-
-	disasm = perf_LLVMCreateDisasm(triplet, &storage, 0, NULL,
-				       symbol_lookup_callback);
 	if (disasm == NULL)
 		goto err;
 
Signature · 1 line
2.48.0.rc2.279.g1de40edade-goog
author reviewer
#12 lore raw

PATCH v2 12/17 perf dso: Clean up read_symbol error handling

In reply to Ian Rogers

Ensure errno is set and return to caller for error handling. Unusually for perf the value isn't negated as expected by symbol__strerror_disassemble.

---
 tools/perf/util/capstone.c |  3 ++-
 tools/perf/util/dso.c      | 14 ++++++++++++--
 tools/perf/util/llvm.c     |  3 ++-
 3 files changed, 16 insertions(+), 4 deletions(-)
tools/perf/util/capstone.c +2 -1
index f103988184c8..032548828925 100644
@@ -12,6 +12,7 @@
 #include "symbol.h"
 #include "thread.h"
 #include <dlfcn.h>
+#include <errno.h>
 #include <fcntl.h>
 #include <inttypes.h>
 #include <string.h>
@@ -463,7 +464,7 @@ int symbol__disassemble_capstone(const char *filename __maybe_unused,
 	buf = dso__read_symbol(dso, filename, map, sym,
 			       &code_buf, &buf_len, &is_64bit);
 	if (buf == NULL)
-		return -1;
+		return errno;
 
 	/* add the function address and name */
 	scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:",
tools/perf/util/dso.c +12 -2
index a90799bed230..247e59605f26 100644
@@ -1697,6 +1697,7 @@ const u8 *dso__read_symbol(struct dso *dso, const char *symfs_filename,
 
 	if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_IMAGE) {
 		pr_debug("No BPF image disassembly support\n");
+		errno = EOPNOTSUPP;
 		return NULL;
 	} else if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_PROG_INFO) {
 #ifdef HAVE_LIBBPF_SUPPORT
@@ -1715,6 +1716,7 @@ const u8 *dso__read_symbol(struct dso *dso, const char *symfs_filename,
 		assert(len <= info_linear->info.jited_prog_len);
 #else
 		pr_debug("No BPF program disassembly support\n");
+		errno = EOPNOTSUPP;
 		return NULL;
 #endif
 	} else {
@@ -1725,26 +1727,34 @@ const u8 *dso__read_symbol(struct dso *dso, const char *symfs_filename,
 			.ip = start,
 		};
 		u8 *code_buf = NULL;
+		int saved_errno;
 
 		nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
 		fd = open(symfs_filename, O_RDONLY);
+		saved_errno = errno;
 		nsinfo__mountns_exit(&nsc);
-		if (fd < 0)
+		if (fd < 0) {
+			errno = saved_errno;
 			return NULL;
+		}
 
-		if (file__read_maps(fd, /*exe=*/true, find_file_offset, &data, is_64bit) == 0) {
+		if (file__read_maps(fd, /*exe=*/true, find_file_offset, &data, is_64bit) <= 0) {
 			close(fd);
+			errno = ENOENT;
 			return NULL;
 		}
 		buf = code_buf = malloc(len);
 		if (buf == NULL) {
 			close(fd);
+			errno = ENOMEM;
 			return NULL;
 		}
 		count = pread(fd, code_buf, len, data.offset);
+		saved_errno = errno;
 		close(fd);
 		if ((u64)count != len) {
 			free(code_buf);
+			errno = saved_errno;
 			return NULL;
 		}
 		*out_buf = code_buf;
tools/perf/util/llvm.c +2 -1
index a28f130c8951..1607364ee736 100644
@@ -9,6 +9,7 @@
 #include "srcline.h"
 #include "symbol.h"
 #include <dlfcn.h>
+#include <errno.h>
 #include <fcntl.h>
 #include <inttypes.h>
 #include <unistd.h>
@@ -365,7 +366,7 @@ int symbol__disassemble_llvm(const char *filename, struct symbol *sym,
 	buf = dso__read_symbol(dso, filename, map, sym,
 			       &code_buf, &buf_len, &is_64bit);
 	if (buf == NULL)
-		return -1;
+		return errno;
 
 	init_llvm();
 	if (arch__is(args->arch, "x86")) {
Signature · 1 line
2.48.0.rc2.279.g1de40edade-goog
author reviewer
#13 lore raw

PATCH v2 13/17 perf build: Remove libbfd support

In reply to Ian Rogers

libbfd is license incompatible with perf and building requires the BUILD_NONDISTRO=1 build flag. Remove the code to simplify the code base.

---
 tools/perf/Documentation/perf-check.txt |   1 -
 tools/perf/Makefile.config              |  38 +---
 tools/perf/builtin-check.c              |   1 -
 tools/perf/tests/Build                  |   1 -
 tools/perf/tests/builtin-test.c         |   1 -
 tools/perf/tests/pe-file-parsing.c      | 101 ----------
 tools/perf/tests/tests.h                |   1 -
 tools/perf/util/demangle-cxx.cpp        |  13 +-
 tools/perf/util/disasm_bpf.c            | 166 ----------------
 tools/perf/util/srcline.c               | 243 +-----------------------
 tools/perf/util/symbol-elf.c            |  86 +--------
 tools/perf/util/symbol.c                | 135 -------------
 tools/perf/util/symbol.h                |   4 -
 13 files changed, 7 insertions(+), 784 deletions(-)
 delete mode 100644 tools/perf/tests/pe-file-parsing.c
tools/perf/Documentation/perf-check.txt +0 -1
index a764a4629220..f6ec81ad87b2 100644
@@ -51,7 +51,6 @@ feature::
                 dwarf_getlocations      /  HAVE_LIBDW_SUPPORT
                 dwarf-unwind            /  HAVE_DWARF_UNWIND_SUPPORT
                 auxtrace                /  HAVE_AUXTRACE_SUPPORT
-                libbfd                  /  HAVE_LIBBFD_SUPPORT
                 libcapstone             /  HAVE_LIBCAPSTONE_SUPPORT
                 libcrypto               /  HAVE_LIBCRYPTO_SUPPORT
                 libdw-dwarf-unwind      /  HAVE_LIBDW_SUPPORT
tools/perf/Makefile.config +1 -37
index 5c2814acc5d5..900be0349de9 100644
@@ -911,42 +911,6 @@ ifneq ($(NO_JEVENTS),1)
   endif
 endif
 
-ifdef BUILD_NONDISTRO
-  ifeq ($(feature-libbfd), 1)
-    EXTLIBS += -lbfd -lopcodes
-  else
-    # we are on a system that requires -liberty and (maybe) -lz
-    # to link against -lbfd; test each case individually here
-
-    # call all detections now so we get correct
-    # status in VF output
-    $(call feature_check,libbfd-liberty)
-    $(call feature_check,libbfd-liberty-z)
-
-    ifeq ($(feature-libbfd-liberty), 1)
-      EXTLIBS += -lbfd -lopcodes -liberty
-      FEATURE_CHECK_LDFLAGS-disassembler-four-args += -liberty -ldl
-      FEATURE_CHECK_LDFLAGS-disassembler-init-styled += -liberty -ldl
-    else
-      ifeq ($(feature-libbfd-liberty-z), 1)
-        EXTLIBS += -lbfd -lopcodes -liberty -lz
-        FEATURE_CHECK_LDFLAGS-disassembler-four-args += -liberty -lz -ldl
-        FEATURE_CHECK_LDFLAGS-disassembler-init-styled += -liberty -lz -ldl
-      endif
-    endif
-    $(call feature_check,disassembler-four-args)
-    $(call feature_check,disassembler-init-styled)
-  endif
-
-  CFLAGS += -DHAVE_LIBBFD_SUPPORT
-  CXXFLAGS += -DHAVE_LIBBFD_SUPPORT
-  ifeq ($(feature-libbfd-buildid), 1)
-    CFLAGS += -DHAVE_LIBBFD_BUILDID_SUPPORT
-  else
-    $(warning Old version of libbfd/binutils things like PE executable profiling will not be available)
-  endif
-endif
-
 ifndef NO_LIBLLVM
   $(call feature_check,llvm-perf)
   ifeq ($(feature-llvm-perf), 1)
@@ -1334,6 +1298,6 @@ endif
 
 # re-generate FEATURE-DUMP as we may have called feature_check, found out
 # extra libraries to add to LDFLAGS of some other test and then redo those
-# tests, see the block about libbfd, disassembler-four-args, for instance.
+# tests, see the block about disassembler-four-args, for instance.
 $(shell rm -f $(FEATURE_DUMP_FILENAME))
 $(foreach feat,$(FEATURE_TESTS),$(shell echo "$(call feature_assign,$(feat))" >> $(FEATURE_DUMP_FILENAME)))
tools/perf/builtin-check.c +0 -1
index 61a11a9b4e75..046956ebf816 100644
@@ -31,7 +31,6 @@ struct feature_status supported_features[] = {
 	FEATURE_STATUS("dwarf_getlocations", HAVE_LIBDW_SUPPORT),
 	FEATURE_STATUS("dwarf-unwind", HAVE_DWARF_UNWIND_SUPPORT),
 	FEATURE_STATUS("auxtrace", HAVE_AUXTRACE_SUPPORT),
-	FEATURE_STATUS("libbfd", HAVE_LIBBFD_SUPPORT),
 	FEATURE_STATUS("libcapstone", HAVE_LIBCAPSTONE_SUPPORT),
 	FEATURE_STATUS("libcrypto", HAVE_LIBCRYPTO_SUPPORT),
 	FEATURE_STATUS("libdw-dwarf-unwind", HAVE_LIBDW_SUPPORT),
tools/perf/tests/Build +0 -1
index 4bf8d3f5eae7..165ba84dc93f 100644
@@ -58,7 +58,6 @@ perf-test-y += demangle-java-test.o
 perf-test-y += demangle-ocaml-test.o
 perf-test-y += pfm.o
 perf-test-y += parse-metric.o
-perf-test-y += pe-file-parsing.o
 perf-test-y += expand-cgroup.o
 perf-test-y += perf-time-to-tsc.o
 perf-test-y += dlfilter-test.o
tools/perf/tests/builtin-test.c +0 -1
index 14d30a5053be..e77bf446e821 100644
@@ -127,7 +127,6 @@ static struct test_suite *generic_tests[] = {
 	&suite__demangle_java,
 	&suite__demangle_ocaml,
 	&suite__parse_metric,
-	&suite__pe_file_parsing,
 	&suite__expand_cgroup_events,
 	&suite__perf_time_to_tsc,
 	&suite__dlfilter,
tools/perf/tests/pe-file-parsing.c deleted +0 -101

101 lines not on this page · raw message

tools/perf/tests/tests.h +0 -1
index 8aea344536b8..751c8489059a 100644
@@ -159,7 +159,6 @@ DECLARE_SUITE(demangle_java);
 DECLARE_SUITE(demangle_ocaml);
 DECLARE_SUITE(pfm);
 DECLARE_SUITE(parse_metric);
-DECLARE_SUITE(pe_file_parsing);
 DECLARE_SUITE(expand_cgroup_events);
 DECLARE_SUITE(perf_time_to_tsc);
 DECLARE_SUITE(dlfilter);
tools/perf/util/demangle-cxx.cpp +2 -11
index 85b706641837..bd657eb37efc 100644
@@ -4,16 +4,11 @@
 #include <string.h>
 #include <linux/compiler.h>
 
-#ifdef HAVE_LIBBFD_SUPPORT
-#define PACKAGE 'perf'
-#include <bfd.h>
-#endif
-
 #ifdef HAVE_CXA_DEMANGLE_SUPPORT
 #include <cxxabi.h>
 #endif
 
-#if defined(HAVE_LIBBFD_SUPPORT) || defined(HAVE_CPLUS_DEMANGLE_SUPPORT)
+#if defined(HAVE_CPLUS_DEMANGLE_SUPPORT)
 #ifndef DMGL_PARAMS
 #define DMGL_PARAMS     (1 << 0)  /* Include function args */
 #define DMGL_ANSI       (1 << 1)  /* Include const, volatile, etc */
@@ -29,11 +24,7 @@ extern "C"
 char *cxx_demangle_sym(const char *str, bool params __maybe_unused,
                        bool modifiers __maybe_unused)
 {
-#ifdef HAVE_LIBBFD_SUPPORT
-        int flags = (params ? DMGL_PARAMS : 0) | (modifiers ? DMGL_ANSI : 0);
-
-        return bfd_demangle(NULL, str, flags);
-#elif defined(HAVE_CPLUS_DEMANGLE_SUPPORT)
+#if defined(HAVE_CPLUS_DEMANGLE_SUPPORT)
         int flags = (params ? DMGL_PARAMS : 0) | (modifiers ? DMGL_ANSI : 0);
 
         return cplus_demangle(str, flags);
tools/perf/util/disasm_bpf.c +0 -166

176 lines not on this page · raw message

tools/perf/util/srcline.c +2 -241

256 lines not on this page · raw message

tools/perf/util/symbol-elf.c +2 -84

122 lines not on this page · raw message

tools/perf/util/symbol.c +0 -135

147 lines not on this page · raw message

tools/perf/util/symbol.h +0 -4
index 3fb5d146d9b1..508fd559a8a1 100644
@@ -174,10 +174,6 @@ int symbol__config_symfs(const struct option *opt __maybe_unused,
 
 struct symsrc;
 
-#ifdef HAVE_LIBBFD_SUPPORT
-int dso__load_bfd_symbols(struct dso *dso, const char *debugfile);
-#endif
-
 int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss,
 		  struct symsrc *runtime_ss, int kmodule);
 int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss);
Signature · 1 line
2.48.0.rc2.279.g1de40edade-goog
author reviewer
#14 lore raw

PATCH v2 14/17 perf build: Remove libiberty support

In reply to Ian Rogers

libiberty is license incompatible with perf and building requires the BUILD_NONDISTRO=1 build flag. libiberty is used for HAVE_CPLUS_DEMANGLE_SUPPORT. Remove the code to simplify the code base as it can't be distributed. Remove the BUILD_NONDISTRO build flag and test as they no longer enable/disable support.

---
 tools/perf/Makefile.config       | 12 ------------
 tools/perf/Makefile.perf         | 11 ++++-------
 tools/perf/tests/make            |  2 --
 tools/perf/util/demangle-cxx.cpp | 13 +------------
 tools/perf/util/symbol-elf.c     | 13 -------------
 5 files changed, 5 insertions(+), 46 deletions(-)
tools/perf/Makefile.config +0 -12
index 900be0349de9..513dd5cab605 100644
@@ -949,18 +949,6 @@ ifndef NO_DEMANGLE
     CXXFLAGS += -DHAVE_CXA_DEMANGLE_SUPPORT
     $(call detected,CONFIG_CXX_DEMANGLE)
   endif
-  ifdef BUILD_NONDISTRO
-    ifeq ($(filter -liberty,$(EXTLIBS)),)
-      $(call feature_check,cplus-demangle)
-      ifeq ($(feature-cplus-demangle), 1)
-        EXTLIBS += -liberty
-      endif
-    endif
-    ifneq ($(filter -liberty,$(EXTLIBS)),)
-      CFLAGS += -DHAVE_CPLUS_DEMANGLE_SUPPORT
-      CXXFLAGS += -DHAVE_CPLUS_DEMANGLE_SUPPORT
-    endif
-  endif
 endif
 
 ifndef NO_LZMA
tools/perf/Makefile.perf +4 -7
index a2886abd4f02..398c30b6bdce 100644
@@ -123,9 +123,6 @@ include ../scripts/utilities.mak
 #
 # Set BUILD_BPF_SKEL to 0 to override BUILD_BPF_SKEL and not build BPF skeletons
 #
-# Define BUILD_NONDISTRO to enable building an linking against libbfd and
-# libiberty distribution license incompatible libraries.
-#
 # Define EXTRA_TESTS to enable building extra tests useful mainly to perf
 # developers, such as:
 #	x86 instruction decoder - new instructions test
@@ -329,10 +326,10 @@ LIBSYMBOL_DIR   = $(srctree)/tools/lib/symbol/
 LIBPERF_DIR     = $(srctree)/tools/lib/perf/
 DOC_DIR         = $(srctree)/tools/perf/Documentation/
 
-# Set FEATURE_TESTS to 'all' so all possible feature checkers are executed.
-# Without this setting the output feature dump file misses some features, for
-# example, liberty. Select all checkers so we won't get an incomplete feature
-# dump file.
+# Set FEATURE_TESTS to 'all' so all possible feature checkers are
+# executed.  Without this setting the output feature dump file misses
+# some features. Select all checkers so we won't get an incomplete
+# feature dump file.
 ifeq ($(config),1)
 ifdef MAKECMDGOALS
 ifeq ($(filter feature-dump,$(MAKECMDGOALS)),feature-dump)
tools/perf/tests/make +0 -2
index 44d76eacce49..4a1ed20bff7e 100644
@@ -68,7 +68,6 @@ python_perf_so := $(shell $(MAKE) python_perf_target|grep "Target is:"|awk '{pri
 make_clean_all      := clean all
 make_python_perf_so := $(python_perf_so)
 make_debug          := DEBUG=1
-make_nondistro      := BUILD_NONDISTRO=1
 make_extra_tests    := EXTRA_TESTS=1
 make_jevents_all    := JEVENTS_ARCH=all
 make_no_bpf_skel    := BUILD_BPF_SKEL=0
@@ -139,7 +138,6 @@ MAKE_F := $(MAKE) -f $(MK)
 endif
 run += make_python_perf_so
 run += make_debug
-run += make_nondistro
 run += make_extra_tests
 run += make_jevents_all
 run += make_no_bpf_skel
tools/perf/util/demangle-cxx.cpp +1 -12
index bd657eb37efc..36801ea327a6 100644
@@ -8,13 +8,6 @@
 #include <cxxabi.h>
 #endif
 
-#if defined(HAVE_CPLUS_DEMANGLE_SUPPORT)
-#ifndef DMGL_PARAMS
-#define DMGL_PARAMS     (1 << 0)  /* Include function args */
-#define DMGL_ANSI       (1 << 1)  /* Include const, volatile, etc */
-#endif
-#endif
-
 /*
  * Demangle C++ function signature
  *
@@ -24,11 +17,7 @@ extern "C"
 char *cxx_demangle_sym(const char *str, bool params __maybe_unused,
                        bool modifiers __maybe_unused)
 {
-#if defined(HAVE_CPLUS_DEMANGLE_SUPPORT)
-        int flags = (params ? DMGL_PARAMS : 0) | (modifiers ? DMGL_ANSI : 0);
-
-        return cplus_demangle(str, flags);
-#elif defined(HAVE_CXA_DEMANGLE_SUPPORT)
+#if defined(HAVE_CXA_DEMANGLE_SUPPORT)
         char *output;
         int status;
 
tools/perf/util/symbol-elf.c +0 -13
index 8140f60af1e5..121db55b9709 100644
@@ -27,13 +27,6 @@
 #include <symbol/kallsyms.h>
 #include <internal/lib.h>
 
-#if defined(HAVE_CPLUS_DEMANGLE_SUPPORT)
-#ifndef DMGL_PARAMS
-#define DMGL_PARAMS     (1 << 0)  /* Include function args */
-#define DMGL_ANSI       (1 << 1)  /* Include const, volatile, etc */
-#endif
-#endif
-
 #ifndef EM_AARCH64
 #define EM_AARCH64	183  /* ARM 64 bit */
 #endif
@@ -286,13 +279,7 @@ static bool want_demangle(bool is_kernel_sym)
 char *cxx_demangle_sym(const char *str __maybe_unused, bool params __maybe_unused,
 		       bool modifiers __maybe_unused)
 {
-#if defined(HAVE_CPLUS_DEMANGLE_SUPPORT)
-	int flags = (params ? DMGL_PARAMS : 0) | (modifiers ? DMGL_ANSI : 0);
-
-	return cplus_demangle(str, flags);
-#else
 	return NULL;
-#endif
 }
 #endif /* !HAVE_CXA_DEMANGLE_SUPPORT */
 
Signature · 1 line
2.48.0.rc2.279.g1de40edade-goog
author reviewer
#15 lore raw

PATCH v2 15/17 perf build: Remove unused defines

In reply to Ian Rogers

DISASM_FOUR_ARGS_SIGNATURE and DISASM_INIT_STYLED were used with libbfd support. Remove now that libbfd support is removed.

---
 tools/perf/Makefile.config | 15 ++-------------
 1 file changed, 2 insertions(+), 13 deletions(-)
tools/perf/Makefile.config +2 -13
index 513dd5cab605..ba60a5b47be7 100644
@@ -320,9 +320,6 @@ FEATURE_CHECK_LDFLAGS-libpython := $(PYTHON_EMBED_LDOPTS)
 
 FEATURE_CHECK_LDFLAGS-libaio = -lrt
 
-FEATURE_CHECK_LDFLAGS-disassembler-four-args = -lbfd -lopcodes -ldl
-FEATURE_CHECK_LDFLAGS-disassembler-init-styled = -lbfd -lopcodes -ldl
-
 CORE_CFLAGS += -fno-omit-frame-pointer
 CORE_CFLAGS += -Wall
 CORE_CFLAGS += -Wextra
@@ -349,7 +346,7 @@ endif
 
 ifeq ($(FEATURES_DUMP),)
 # We will display at the end of this Makefile.config, using $(call feature_display_entries)
-# As we may retry some feature detection here, see the disassembler-four-args case, for instance
+# As we may retry some feature detection here.
   FEATURE_DISPLAY_DEFERRED := 1
 include $(srctree)/tools/build/Makefile.feature
 else
@@ -1001,14 +998,6 @@ ifdef HAVE_KVM_STAT_SUPPORT
     CFLAGS += -DHAVE_KVM_STAT_SUPPORT
 endif
 
-ifeq ($(feature-disassembler-four-args), 1)
-    CFLAGS += -DDISASM_FOUR_ARGS_SIGNATURE
-endif
-
-ifeq ($(feature-disassembler-init-styled), 1)
-    CFLAGS += -DDISASM_INIT_STYLED
-endif
-
 ifeq (${IS_64_BIT}, 1)
   ifndef NO_PERF_READ_VDSO32
     $(call feature_check,compile-32)
@@ -1286,6 +1275,6 @@ endif
 
 # re-generate FEATURE-DUMP as we may have called feature_check, found out
 # extra libraries to add to LDFLAGS of some other test and then redo those
-# tests, see the block about disassembler-four-args, for instance.
+# tests.
 $(shell rm -f $(FEATURE_DUMP_FILENAME))
 $(foreach feat,$(FEATURE_TESTS),$(shell echo "$(call feature_assign,$(feat))" >> $(FEATURE_DUMP_FILENAME)))
Signature · 1 line
2.48.0.rc2.279.g1de40edade-goog
author reviewer
#16 lore raw

PATCH v2 16/17 perf disasm: Remove disasm_bpf

In reply to Ian Rogers

BPF disassembly was handled in here by libbfd. The LLVM and capstone disassemblers now support BPF JIT disassembly. As libbfd support was removed the functions here no longer did anything remove them and associated error values.

---
 tools/perf/util/Build        |  1 -
 tools/perf/util/annotate.h   |  1 -
 tools/perf/util/disasm.c     | 12 +++---------
 tools/perf/util/disasm_bpf.c | 29 -----------------------------
 tools/perf/util/disasm_bpf.h | 12 ------------
 5 files changed, 3 insertions(+), 52 deletions(-)
 delete mode 100644 tools/perf/util/disasm_bpf.c
 delete mode 100644 tools/perf/util/disasm_bpf.h
tools/perf/util/Build +0 -1
index eb00c599e179..4b757d157f13 100644
@@ -14,7 +14,6 @@ perf-util-y += copyfile.o
 perf-util-y += ctype.o
 perf-util-y += db-export.o
 perf-util-y += disasm.o
-perf-util-y += disasm_bpf.o
 perf-util-y += env.o
 perf-util-y += event.o
 perf-util-y += evlist.o
tools/perf/util/annotate.h +0 -1
index 0ba5846dad4d..e704a9c913f7 100644
@@ -438,7 +438,6 @@ enum symbol_disassemble_errno {
 	__SYMBOL_ANNOTATE_ERRNO__START		= -10000,
 
 	SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX	= __SYMBOL_ANNOTATE_ERRNO__START,
-	SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF,
 	SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_CPUID_PARSING,
 	SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_REGEXP,
 	SYMBOL_ANNOTATE_ERRNO__BPF_INVALID_FILE,
tools/perf/util/disasm.c +3 -9
index 99b9c21e02b0..ebd86691acf8 100644
@@ -17,7 +17,6 @@
 #include "capstone.h"
 #include "debug.h"
 #include "disasm.h"
-#include "disasm_bpf.h"
 #include "dso.h"
 #include "dwarf-regs.h"
 #include "env.h"
@@ -1230,9 +1229,6 @@ int symbol__strerror_disassemble(struct map_symbol *ms, int errnum, char *buf, s
 			  "  --vmlinux vmlinux\n", build_id_msg ?: "");
 	}
 		break;
-	case SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF:
-		scnprintf(buf, buflen, "Please link with binutils's libopcode to enable BPF annotation");
-		break;
 	case SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_REGEXP:
 		scnprintf(buf, buflen, "Problems with arch specific instruction name regular expressions.");
 		break;
@@ -1500,11 +1496,9 @@ static int symbol__disassemble_objdump(const char *filename, struct symbol *sym,
 	struct child_process objdump_process;
 	int err;
 
-	if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_PROG_INFO)
-		return symbol__disassemble_bpf(sym, args);
-
-	if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_IMAGE)
-		return symbol__disassemble_bpf_image(sym, args);
+	if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_PROG_INFO ||
+	    dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_IMAGE)
+		return -1;
 
 	err = asprintf(&command,
 		 "%s %s%s --start-address=0x%016" PRIx64
tools/perf/util/disasm_bpf.c deleted +0 -29
deleted file mode 100644
index a891a0b909a7..000000000000
@@ -1,29 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0-only
-
-#include "util/annotate.h"
-#include "util/disasm_bpf.h"
-#include "util/symbol.h"
-#include <linux/zalloc.h>
-#include <string.h>
-
-int symbol__disassemble_bpf(struct symbol *sym __maybe_unused, struct annotate_args *args __maybe_unused)
-{
-	return SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF;
-}
-
-int symbol__disassemble_bpf_image(struct symbol *sym, struct annotate_args *args)
-{
-	struct annotation *notes = symbol__annotation(sym);
-	struct disasm_line *dl;
-
-	args->offset = -1;
-	args->line = strdup("to be implemented");
-	args->line_nr = 0;
-	args->fileloc = NULL;
-	dl = disasm_line__new(args);
-	if (dl)
-		annotation_line__add(&dl->al, &notes->src->source);
-
-	zfree(&args->line);
-	return 0;
-}
tools/perf/util/disasm_bpf.h deleted +0 -12
deleted file mode 100644
index 2ecb19545388..000000000000
@@ -1,12 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0-only
-
-#ifndef __PERF_DISASM_BPF_H
-#define __PERF_DISASM_BPF_H
-
-struct symbol;
-struct annotate_args;
-
-int symbol__disassemble_bpf(struct symbol *sym, struct annotate_args *args);
-int symbol__disassemble_bpf_image(struct symbol *sym, struct annotate_args *args);
-
-#endif /* __PERF_DISASM_BPF_H */
Signature · 1 line
2.48.0.rc2.279.g1de40edade-goog
author reviewer
#17 lore raw

PATCH v2 17/17 perf disasm: Make ins__scnprintf and ins__is_nop static

In reply to Ian Rogers

Reduce the scope of ins__scnprintf and ins__is_nop that aren't used outside of disasm.c.

---
 tools/perf/util/disasm.c | 6 +++---
 tools/perf/util/disasm.h | 3 ---
 2 files changed, 3 insertions(+), 6 deletions(-)
tools/perf/util/disasm.c +3 -3
index ebd86691acf8..b2dfd40588ef 100644
@@ -246,8 +246,8 @@ static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size,
 	return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->raw);
 }
 
-int ins__scnprintf(struct ins *ins, char *bf, size_t size,
-		   struct ins_operands *ops, int max_ins_name)
+static int ins__scnprintf(struct ins *ins, char *bf, size_t size,
+			  struct ins_operands *ops, int max_ins_name)
 {
 	if (ins->ops->scnprintf)
 		return ins->ops->scnprintf(ins, bf, size, ops, max_ins_name);
@@ -824,7 +824,7 @@ static struct ins_ops ret_ops = {
 	.scnprintf = ins__raw_scnprintf,
 };
 
-bool ins__is_nop(const struct ins *ins)
+static bool ins__is_nop(const struct ins *ins)
 {
 	return ins->ops == &nop_ops;
 }
tools/perf/util/disasm.h +0 -3
index 2cb4e1a6bd30..09c86f540f7f 100644
@@ -110,13 +110,10 @@ struct arch *arch__find(const char *name);
 bool arch__is(struct arch *arch, const char *name);
 
 struct ins_ops *ins__find(struct arch *arch, const char *name, struct disasm_line *dl);
-int ins__scnprintf(struct ins *ins, char *bf, size_t size,
-		   struct ins_operands *ops, int max_ins_name);
 
 bool ins__is_call(const struct ins *ins);
 bool ins__is_jump(const struct ins *ins);
 bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2);
-bool ins__is_nop(const struct ins *ins);
 bool ins__is_ret(const struct ins *ins);
 bool ins__is_lock(const struct ins *ins);
 
Signature · 1 line
2.48.0.rc2.279.g1de40edade-goog
#18 lore raw
In reply to Ian Rogers
Quoted 5 lines · Ian Rogers

Linking against libcapstone and libLLVM can be a significant increase in dependencies and size of memory footprint. For something like perf record the disassembler and addr2line functionality won't be used. Support dynamically loading these libraries using dlopen and then calling the appropriate functions found using dlsym.

It's unclear to me what this actually fixes. If the code is not used it should not be faulted in and the dynamic linker is lazy too, so if it's not used, it won't even be linked.

I don't see any numbers, but it won't surprise me if it improved actual run time or memory usage significantly.

-Andi

author reviewer
#19 lore raw
Quoted 8 lines · Andi Kleen
Quoted 5 lines · Ian Rogers

Linking against libcapstone and libLLVM can be a significant increase in dependencies and size of memory footprint. For something like perf record the disassembler and addr2line functionality won't be used. Support dynamically loading these libraries using dlopen and then calling the appropriate functions found using dlsym.

Quoted 6 lines

It's unclear to me what this actually fixes. If the code is not used it should not be faulted in and the dynamic linker is lazy too, so if it's not used, it won't even be linked.

I don't see any numbers, but it won't surprise me if it improved actual run time or memory usage significantly.

In certain scenarios, like data centers, it can be useful to statically link all your dependencies to avoid dll hell. The X86 disassembler alone in libllvm is of a size comparable to the perf tool
- I think this speaks to us doing a reasonably good job of size optimization of the events/metrics in the perf tool. We want these dependencies for the performance over forking objdump and addr2line, but we don't want it baked in - unless the person doing the build wants this and this is still the default if the libraries are detected by Makefile.config. Using dlopen also means distributions can have a perf tool that doesn't drag in libLLVM.so and a universe of dependencies, but when it is installed get the performance advantages. In data centres having fast disassembly/addr2line is less of a priority over the binary size cost replicated over 10,000s of machines because those machines don't tend to be running the annotate/report commands.

Fwiw, Namhyung's uftrace is doing something similar for python:
https://github.com/namhyung/uftrace/blob/master/utils/script-python.c#L139
and I wish the perf tool were also doing this. I think it is much nicer to have the tool fail at runtime because of a missing dependency which you can then install should you want it, rather than doing an equivalent within the code base with #ifdefs and needing users to recompile. This patch series significantly reduces the #ifdefs in places like the core disasm code.

Thanks,
Ian

#20 lore raw
Quoted 2 lines

In certain scenarios, like data centers, it can be useful to statically link all your dependencies to avoid dll hell.

Yes but it won't be loaded into memory if not used. Executable loading is all lazy. Maybe look a page fault trace for loading perf if you don't believe me.

So you're trying to optimize disk space here?

I didn't see that in the cover letter.

It doesn't seem like a very good reason for such an intrusive patch kit.

If it's a serious concern maybe investigate an executable compressor?

Quoted 2 lines

The X86
disassembler alone in libllvm is of a size comparable to the perf tool

I agree that LLVM is a serious bloat and DLL hell concern, but I don't think dlopen is the answer here.

-Andi

author reviewer
#21 lore raw
Quoted 11 lines · Andi Kleen
Quoted 3 lines

In certain scenarios, like data centers, it can be useful to statically link all your dependencies to avoid dll hell.

Yes but it won't be loaded into memory if not used. Executable loading is all lazy. Maybe look a page fault trace for loading perf if you don't believe me.

So you're trying to optimize disk space here?

I didn't see that in the cover letter.

For me yes, for distributions it is dependencies. This is already in the v3 message:
https://lore.kernel.org/lkml/[email protected]/

Quoted 1 line

It doesn't seem like a very good reason for such an intrusive patch kit.

The capstone and LLVM code is preexisting. Moving the capstone/llvm code to their own files isn't dependent on dlopen, it does make it nicer to have a single place we're doing dlopen. The change to shim the capstone/LLVM calls looks like this:
https://github.com/googleprodkernel/linux-perf/blob/google_tools_master/tools/perf/util/llvm.c#L160-L182
That is a shim is introduced that either calls through to the function if we're linking against libcapstone/llvm or does the dlsym. There are 7 such functions in the LLVM code. I don't think shimming 7 functions is at the scale of hugely intrusive.

Quoted 1 line

If it's a serious concern maybe investigate an executable compressor?

Perhaps just have a squashfs partition.

Fwiw, excluding dependencies I think compression on the events is a good solution. Convert json events/metrics to a sysfs file with the cpuid in the path, add the compressed file to the binary as data, find "json" events by iterating the directories in the compressed file, etc. A single filesystem approach to event lookup can mean we do some kind of unionfs style lookup of events, which could support users adding their own events/metrics in a directory. Zip doesn't support compressing across files, which is something of a requirement here, other formats do but it's a case of optimizing for some kind of libarchive sweet spot. The opportunity here is that about 70% of the binary is event encodings, a compressed file is about 30% of the current binary size, so we could reduce the binary size by about 40%.

Quoted 5 lines
Quoted 2 lines

The X86
disassembler alone in libllvm is of a size comparable to the perf tool

I agree that LLVM is a serious bloat and DLL hell concern, but I don't think dlopen is the answer here.

Agreed, but it's where the code is at. addr2line command or use LLVM for some performance. I think having an inbuilt solution would be best longer term - we spend energy trying to parse and understand text output from tools/libraries when the information is just sitting there in the instruction encoding. Such a solution would be brittle for things like new dwarf information, so we may want to have fallbacks like LLVM but having a loosely coupled dependency using dlopen feels preferable there, to aid package maintainers.

Thanks,
Ian

maintainer
#22 lore raw
In reply to Ian Rogers
Quoted 5 lines · Ian Rogers

Linking against libcapstone and libLLVM can be a significant increase in dependencies and size of memory footprint. For something like perf record the disassembler and addr2line functionality won't be used. Support dynamically loading these libraries using dlopen and then calling the appropriate functions found using dlsym.

It's not clear from the description how you would use dlopen/dlsym. Based on an offline discussion, you want to leave the current linking model as is, and to support dlopen/dlsym when it's NOT detected at build-time, right?

For that, you need to carry some definitions of the functions and types for the used APIs. But I'm not sure if it's right to carry them in the perf code base.

Quoted 5 lines

BUILD_NONDISTRO is used to build perf against the license incompatible libbfd and libiberty libraries. As this has been opt-in for nearly 2 years, commit dd317df07207 ("perf build: Make binutil libraries opt in"), remove the code to simplify the code base.

This part can be a separate series.

Quoted 5 lines

The patch series:
1) does some initial clean up;
2) moves the capstone and LLVM code to their own C files,
3) simplifies a little the capstone code;

I like changes up to this in general. Let me take a look at the patches.

Thanks,
Namhyung

Quoted 85 lines

4) adds perf_ variants of the functions that will either directly call the function or use dlsym to discover it;
5) adds BPF JIT disassembly support to LLVM and capstone disassembly;
6) removes the BUILD_NONDISTRO code, reduces scope and removes what's possible.

The addr2line LLVM functionality is written in C++. To avoid linking against libLLVM for this, a new LIBLLVM_DYNAMIC option is added where the C++ code with the libLLVM dependency will be built into a libperf-llvm.so and that dlsym-ed and called against. Ideally LLVM would extend their C API to avoid this.

The libbfd BPF disassembly supported source lines, this wasn't ported to the capstone and LLVM disassembly.

v2: Add mangling of the function names in libperf-llvm.so to avoid
potential infinite recursion. Add BPF JIT disassembly support to LLVM and capstone. Add/rebase the BUILD_NONDISTRO cleanup onto the series from:
https://lore.kernel.org/lkml/[email protected]/
Some other minor additional clean up.

Ian Rogers (17):
perf build: Remove libtracefs configuration
perf map: Constify objdump offset/address conversion APIs perf capstone: Move capstone functionality into its own file perf llvm: Move llvm functionality into its own file perf capstone: Remove open_capstone_handle
perf capstone: Support for dlopen-ing libcapstone.so perf llvm: Support for dlopen-ing libLLVM.so
perf llvm: Mangle libperf-llvm.so function names perf dso: Move read_symbol from llvm/capstone to dso perf dso: Support BPF programs in dso__read_symbol perf llvm: Disassemble cleanup
perf dso: Clean up read_symbol error handling
perf build: Remove libbfd support
perf build: Remove libiberty support
perf build: Remove unused defines
perf disasm: Remove disasm_bpf
perf disasm: Make ins__scnprintf and ins__is_nop static

 tools/perf/Documentation/perf-check.txt |   1 -
 tools/perf/Makefile.config              |  90 +---
 tools/perf/Makefile.perf                |  35 +-
 tools/perf/builtin-check.c              |   1 -
 tools/perf/builtin-script.c             |   2 -
 tools/perf/tests/Build                  |   1 -
 tools/perf/tests/builtin-test.c         |   1 -
 tools/perf/tests/make                   |   4 +-
 tools/perf/tests/pe-file-parsing.c      | 101 ----
 tools/perf/tests/tests.h                |   1 -
 tools/perf/util/Build                   |   5 +-
 tools/perf/util/annotate.h              |   1 -
 tools/perf/util/capstone.c              | 682 ++++++++++++++++++++++++
 tools/perf/util/capstone.h              |  24 +
 tools/perf/util/demangle-cxx.cpp        |  22 +-
 tools/perf/util/disasm.c                | 632 +---------------------
 tools/perf/util/disasm.h                |   5 +-
 tools/perf/util/disasm_bpf.c            | 195 -------
 tools/perf/util/disasm_bpf.h            |  12 -
 tools/perf/util/dso.c                   |  98 ++++
 tools/perf/util/dso.h                   |   4 +
 tools/perf/util/llvm-c-helpers.cpp      | 120 ++++-
 tools/perf/util/llvm-c-helpers.h        |  24 +-
 tools/perf/util/llvm.c                  | 489 +++++++++++++++++
 tools/perf/util/llvm.h                  |  24 +
 tools/perf/util/map.c                   |  19 +-
 tools/perf/util/map.h                   |   6 +-
 tools/perf/util/print_insn.c            | 117 +---
 tools/perf/util/srcline.c               | 306 +----------
 tools/perf/util/srcline.h               |   6 +
 tools/perf/util/symbol-elf.c            |  95 ----
 tools/perf/util/symbol.c                | 135 -----
 tools/perf/util/symbol.h                |   4 -
 33 files changed, 1552 insertions(+), 1710 deletions(-)
 delete mode 100644 tools/perf/tests/pe-file-parsing.c
 create mode 100644 tools/perf/util/capstone.c
 create mode 100644 tools/perf/util/capstone.h
 delete mode 100644 tools/perf/util/disasm_bpf.c
 delete mode 100644 tools/perf/util/disasm_bpf.h
 create mode 100644 tools/perf/util/llvm.c
 create mode 100644 tools/perf/util/llvm.h
Signature · 1 line
2.48.0.rc2.279.g1de40edade-goog
author reviewer
#23 lore raw
Quoted 8 lines · Namhyung Kim
Quoted 5 lines · Ian Rogers

Linking against libcapstone and libLLVM can be a significant increase in dependencies and size of memory footprint. For something like perf record the disassembler and addr2line functionality won't be used. Support dynamically loading these libraries using dlopen and then calling the appropriate functions found using dlsym.

Quoted 4 lines

It's not clear from the description how you would use dlopen/dlsym. Based on an offline discussion, you want to leave the current linking model as is, and to support dlopen/dlsym when it's NOT detected at build-time, right?

Yep. Current behavior is no header file than these options fail, new behavior is that we try to use dlopen/dlsym and fail if the dlopen fails.

Quoted 3 lines

For that, you need to carry some definitions of the functions and types for the used APIs. But I'm not sure if it's right to carry them in the perf code base.

Right. I mention that here:
https://lore.kernel.org/lkml/CAP-5=fUhNuybCU-2_5EgcCwgwXnxvyFMvyhzKe=ZP1bssQwXHw@mail.gmail.com/
For LLVM we need 3 typedefs and 5 #defines, for capstone we need 2 structs and 5 enums (if we #ifdef some x86 only formatting code).

The problem in not carrying those definitions is:
1) if the header file isn't present a build won't support LLVM/capstone even by dlopen - everything falls through to objdump/addr2line that have known performance issues;
2) package maintainers either need to spot a warning message to realize they've done this by having a missing header file (hard to spot and brittle) or we require the build to fail and people without capstone.h opt out of the build error with NO_CAPSTONE=1 - something perf developers will probably not like;
3) the LLVM/capstone code needs #ifdefs and __maybe_unused to suppress compiler warnings, or perhaps we have a minimal version of those files, leading to extra code complexity.

I believe the approach here is no worse than what we do with vmlinux.h for BPF code and is robust as depending on dlsym being able to look up the function names. It is not perfect but I think it is more perfect and less complex than the alternative.

Quoted 7 lines
Quoted 5 lines

BUILD_NONDISTRO is used to build perf against the license incompatible libbfd and libiberty libraries. As this has been opt-in for nearly 2 years, commit dd317df07207 ("perf build: Make binutil libraries opt in"), remove the code to simplify the code base.

This part can be a separate series.

Right, I posted it as a series here:
https://lore.kernel.org/lkml/[email protected]/
as mentioned in the v2 notes below. The issue was that Arnaldo pointed out removing BUILD_NONDISTRO removed disassemble_bpf that had only been implemented for libbfd. This series adds the LLVM/capstone definitions built into the symbol__disassemble refactor. Merging that series would conflict with this series, so I posted everything together to avoid having series of patches depending upon one another. I also wanted to check that what is in disasm.c in the end is reasonable, which I believe it is with significantly reduced ifdef-ery.

Quoted 8 lines
Quoted 5 lines

The patch series:
1) does some initial clean up;
2) moves the capstone and LLVM code to their own C files,
3) simplifies a little the capstone code;

I like changes up to this in general. Let me take a look at the patches.

Thanks,
Ian
Ian

Quoted 2 lines

Thanks,
Namhyung

author reviewer
#24 lore raw
Quoted 3 lines · Ian Rogers
Quoted 2 lines · Namhyung Kim

I like changes up to this in general. Let me take a look at the patches.

So it would be nice to make progress with this series given some level of happiness, I don't see any actions currently on the patch series as is. If I may be so bold as to recap the issues that have come up:

1) Andi Kleen mentions that dlopen is inferior to linking against libraries and those libraries aren't a memory overhead if unused.

I agree but pointed-out the data center use case means that saving size on binaries can be important to some (me). We've also been trying to reduce perf's dependencies for distributions as perf dragging in say the whole of libLLVM can be annoying for making minimal distributions that contain perf. Perhaps somebody (Arnaldo?) more involved with distributions can confirm or deny the distribution problem, I'm hoping it is self-evident.

2) Namhyung Kim was uncomfortable with the code defining types/constants that were in header files as the two may drift over time

I agree but in the same way as a function name is an ABI for dlysym, the types/constants are too. Yes a header file may change, but in doing so the ABI has changed and so it would be an incompatible change and everything would be broken. We'd need to fix the code for this, say as we did when libbpf moved to version 1.0, but using a header file would only weakly guard against this problem. The problem with including the header files is that then the build either breaks without the header or we need to support a no linking against a library and not using dlopen case. I suspect a lot of distributions wouldn't understand the build subtlety in this, the necessary build options and things installed, and we'd end up not using things like libLLVM even when it is known to be a large performance win. I also hope one day we can move from parsing text out of forked commands, as it is slower and more brittle, to just directly using libraries. Making dlopen the fallback (probably with a warning on failure) seems like the right direction for this except we won't get it if we need to drag in extra dependency header files for the build to succeed (well we could have a no library or dlopen option, but then we'd probably find distributions packaging this and things like perf annotate getting broken as they don't even know how to dlopen a library).

3) Namhyung Kim (and I) also raises that the libcapstone patch can be smaller by dropping the print_capstone_detail support on x86

Note, given the similarity between capstone and libLLVM for disassembly, it is curious that only capstone gives the extra detail.

I agree. Given the capstone disassembly output will be compromised we should warn for this, probably in Makefile.config to avoid running afoul of -Werror. It isn't clear that having a warning is a good move given the handful of structs needed to support print_capstone_detail. I'd prefer to keep the structs so that we haven't got a warning that looks like it needs cleaning up.

4) Namhyung Kim raised concerns over #if placement

Namhyung raised that he'd prefer:

```
#if HAVE_LIBCAPSTONE_SUPPORT
// lots of code
#else
// lots of code
#endif
```

rather than the #ifs being inside or around individual functions. I raised that the large #ifs is a problem in the current code as you lose context when trying to understand a function. You may look at a function but not realize it isn't being used because of a #if 10s or 100s of lines above. Namhyung raised that the large #ifs is closer to kernel style, I disagreed as I think kernel style is only doing this when it stubs out a bunch of API functions, not when more context would be useful. Hopefully as the person writing the patches the style choice I've made can be respected.

5) Daniel Xu raised issues with the removal of libbfd for Rust support, as the code implies libbfd C++ demangling is a pre-requisite of legacy rust symbol demangling

A separate patch was posted adding Rust v0 symbol demangling with no libbfd dependency:
https://lore.kernel.org/lkml/[email protected]/
The legacy support should work with the non-libbfd demanglers as that's what we have today. We should really clean up Rust demangling and have tests. This is blocked on the Rust community responding to:
https://github.com/rust-lang/rust/issues/60705

Thanks,
Ian

author reviewer
#25 lore raw
Quoted 83 lines · Ian Rogers
Quoted 3 lines · Ian Rogers
Quoted 2 lines · Namhyung Kim

I like changes up to this in general. Let me take a look at the patches.

So it would be nice to make progress with this series given some level of happiness, I don't see any actions currently on the patch series as is. If I may be so bold as to recap the issues that have come up:

1) Andi Kleen mentions that dlopen is inferior to linking against libraries and those libraries aren't a memory overhead if unused.

I agree but pointed-out the data center use case means that saving size on binaries can be important to some (me). We've also been trying to reduce perf's dependencies for distributions as perf dragging in say the whole of libLLVM can be annoying for making minimal distributions that contain perf. Perhaps somebody (Arnaldo?) more involved with distributions can confirm or deny the distribution problem, I'm hoping it is self-evident.

2) Namhyung Kim was uncomfortable with the code defining types/constants that were in header files as the two may drift over time

I agree but in the same way as a function name is an ABI for dlysym, the types/constants are too. Yes a header file may change, but in doing so the ABI has changed and so it would be an incompatible change and everything would be broken. We'd need to fix the code for this, say as we did when libbpf moved to version 1.0, but using a header file would only weakly guard against this problem. The problem with including the header files is that then the build either breaks without the header or we need to support a no linking against a library and not using dlopen case. I suspect a lot of distributions wouldn't understand the build subtlety in this, the necessary build options and things installed, and we'd end up not using things like libLLVM even when it is known to be a large performance win. I also hope one day we can move from parsing text out of forked commands, as it is slower and more brittle, to just directly using libraries. Making dlopen the fallback (probably with a warning on failure) seems like the right direction for this except we won't get it if we need to drag in extra dependency header files for the build to succeed (well we could have a no library or dlopen option, but then we'd probably find distributions packaging this and things like perf annotate getting broken as they don't even know how to dlopen a library).

3) Namhyung Kim (and I) also raises that the libcapstone patch can be smaller by dropping the print_capstone_detail support on x86

Note, given the similarity between capstone and libLLVM for disassembly, it is curious that only capstone gives the extra detail.

I agree. Given the capstone disassembly output will be compromised we should warn for this, probably in Makefile.config to avoid running afoul of -Werror. It isn't clear that having a warning is a good move given the handful of structs needed to support print_capstone_detail. I'd prefer to keep the structs so that we haven't got a warning that looks like it needs cleaning up.

4) Namhyung Kim raised concerns over #if placement

Namhyung raised that he'd prefer:

```
#if HAVE_LIBCAPSTONE_SUPPORT
// lots of code
#else
// lots of code
#endif
```

rather than the #ifs being inside or around individual functions. I raised that the large #ifs is a problem in the current code as you lose context when trying to understand a function. You may look at a function but not realize it isn't being used because of a #if 10s or 100s of lines above. Namhyung raised that the large #ifs is closer to kernel style, I disagreed as I think kernel style is only doing this when it stubs out a bunch of API functions, not when more context would be useful. Hopefully as the person writing the patches the style choice I've made can be respected.

5) Daniel Xu raised issues with the removal of libbfd for Rust support, as the code implies libbfd C++ demangling is a pre-requisite of legacy rust symbol demangling

Quoted 7 lines

A separate patch was posted adding Rust v0 symbol demangling with no libbfd dependency:
https://lore.kernel.org/lkml/[email protected]/
The legacy support should work with the non-libbfd demanglers as that's what we have today. We should really clean up Rust demangling and have tests. This is blocked on the Rust community responding to:
https://github.com/rust-lang/rust/issues/60705

Ping.

Thanks,
Ian

maintainer
#26 lore raw

Hi Ian,

Quoted 84 lines · Ian Rogers
Quoted 83 lines · Ian Rogers
Quoted 3 lines · Ian Rogers
Quoted 2 lines · Namhyung Kim

I like changes up to this in general. Let me take a look at the patches.

So it would be nice to make progress with this series given some level of happiness, I don't see any actions currently on the patch series as is. If I may be so bold as to recap the issues that have come up:

1) Andi Kleen mentions that dlopen is inferior to linking against libraries and those libraries aren't a memory overhead if unused.

I agree but pointed-out the data center use case means that saving size on binaries can be important to some (me). We've also been trying to reduce perf's dependencies for distributions as perf dragging in say the whole of libLLVM can be annoying for making minimal distributions that contain perf. Perhaps somebody (Arnaldo?) more involved with distributions can confirm or deny the distribution problem, I'm hoping it is self-evident.

2) Namhyung Kim was uncomfortable with the code defining types/constants that were in header files as the two may drift over time

I agree but in the same way as a function name is an ABI for dlysym, the types/constants are too. Yes a header file may change, but in doing so the ABI has changed and so it would be an incompatible change and everything would be broken. We'd need to fix the code for this, say as we did when libbpf moved to version 1.0, but using a header file would only weakly guard against this problem. The problem with including the header files is that then the build either breaks without the header or we need to support a no linking against a library and not using dlopen case. I suspect a lot of distributions wouldn't understand the build subtlety in this, the necessary build options and things installed, and we'd end up not using things like libLLVM even when it is known to be a large performance win. I also hope one day we can move from parsing text out of forked commands, as it is slower and more brittle, to just directly using libraries. Making dlopen the fallback (probably with a warning on failure) seems like the right direction for this except we won't get it if we need to drag in extra dependency header files for the build to succeed (well we could have a no library or dlopen option, but then we'd probably find distributions packaging this and things like perf annotate getting broken as they don't even know how to dlopen a library).

3) Namhyung Kim (and I) also raises that the libcapstone patch can be smaller by dropping the print_capstone_detail support on x86

Note, given the similarity between capstone and libLLVM for disassembly, it is curious that only capstone gives the extra detail.

I agree. Given the capstone disassembly output will be compromised we should warn for this, probably in Makefile.config to avoid running afoul of -Werror. It isn't clear that having a warning is a good move given the handful of structs needed to support print_capstone_detail. I'd prefer to keep the structs so that we haven't got a warning that looks like it needs cleaning up.

4) Namhyung Kim raised concerns over #if placement

Namhyung raised that he'd prefer:

```
#if HAVE_LIBCAPSTONE_SUPPORT
// lots of code
#else
// lots of code
#endif
```

rather than the #ifs being inside or around individual functions. I raised that the large #ifs is a problem in the current code as you lose context when trying to understand a function. You may look at a function but not realize it isn't being used because of a #if 10s or 100s of lines above. Namhyung raised that the large #ifs is closer to kernel style, I disagreed as I think kernel style is only doing this when it stubs out a bunch of API functions, not when more context would be useful. Hopefully as the person writing the patches the style choice I've made can be respected.

5) Daniel Xu raised issues with the removal of libbfd for Rust support, as the code implies libbfd C++ demangling is a pre-requisite of legacy rust symbol demangling

Quoted 7 lines
Quoted 7 lines

A separate patch was posted adding Rust v0 symbol demangling with no libbfd dependency:
https://lore.kernel.org/lkml/[email protected]/
The legacy support should work with the non-libbfd demanglers as that's what we have today. We should really clean up Rust demangling and have tests. This is blocked on the Rust community responding to:
https://github.com/rust-lang/rust/issues/60705

I think #ifdef placements is not a big deal, but I still don't want to pull libcapstone details into the perf tree.

For LLVM, I think you should to build llvm-c-helpers anyway which means you still need LLVM headers and don't need to redefine the structures.

Can we do the same for capstone? I think it's best to use capstone headers directly and add a build option to use dlopen().

Thanks,
Namhyung

author reviewer
#27 lore raw
Quoted 96 lines · Namhyung Kim

Hi Ian,

Quoted 84 lines · Ian Rogers
Quoted 83 lines · Ian Rogers
Quoted 3 lines · Ian Rogers
Quoted 2 lines · Namhyung Kim

I like changes up to this in general. Let me take a look at the patches.

So it would be nice to make progress with this series given some level of happiness, I don't see any actions currently on the patch series as is. If I may be so bold as to recap the issues that have come up:

1) Andi Kleen mentions that dlopen is inferior to linking against libraries and those libraries aren't a memory overhead if unused.

I agree but pointed-out the data center use case means that saving size on binaries can be important to some (me). We've also been trying to reduce perf's dependencies for distributions as perf dragging in say the whole of libLLVM can be annoying for making minimal distributions that contain perf. Perhaps somebody (Arnaldo?) more involved with distributions can confirm or deny the distribution problem, I'm hoping it is self-evident.

2) Namhyung Kim was uncomfortable with the code defining types/constants that were in header files as the two may drift over time

I agree but in the same way as a function name is an ABI for dlysym, the types/constants are too. Yes a header file may change, but in doing so the ABI has changed and so it would be an incompatible change and everything would be broken. We'd need to fix the code for this, say as we did when libbpf moved to version 1.0, but using a header file would only weakly guard against this problem. The problem with including the header files is that then the build either breaks without the header or we need to support a no linking against a library and not using dlopen case. I suspect a lot of distributions wouldn't understand the build subtlety in this, the necessary build options and things installed, and we'd end up not using things like libLLVM even when it is known to be a large performance win. I also hope one day we can move from parsing text out of forked commands, as it is slower and more brittle, to just directly using libraries. Making dlopen the fallback (probably with a warning on failure) seems like the right direction for this except we won't get it if we need to drag in extra dependency header files for the build to succeed (well we could have a no library or dlopen option, but then we'd probably find distributions packaging this and things like perf annotate getting broken as they don't even know how to dlopen a library).

3) Namhyung Kim (and I) also raises that the libcapstone patch can be smaller by dropping the print_capstone_detail support on x86

Note, given the similarity between capstone and libLLVM for disassembly, it is curious that only capstone gives the extra detail.

I agree. Given the capstone disassembly output will be compromised we should warn for this, probably in Makefile.config to avoid running afoul of -Werror. It isn't clear that having a warning is a good move given the handful of structs needed to support print_capstone_detail. I'd prefer to keep the structs so that we haven't got a warning that looks like it needs cleaning up.

4) Namhyung Kim raised concerns over #if placement

Namhyung raised that he'd prefer:

```
#if HAVE_LIBCAPSTONE_SUPPORT
// lots of code
#else
// lots of code
#endif
```

rather than the #ifs being inside or around individual functions. I raised that the large #ifs is a problem in the current code as you lose context when trying to understand a function. You may look at a function but not realize it isn't being used because of a #if 10s or 100s of lines above. Namhyung raised that the large #ifs is closer to kernel style, I disagreed as I think kernel style is only doing this when it stubs out a bunch of API functions, not when more context would be useful. Hopefully as the person writing the patches the style choice I've made can be respected.

5) Daniel Xu raised issues with the removal of libbfd for Rust support, as the code implies libbfd C++ demangling is a pre-requisite of legacy rust symbol demangling

Quoted 7 lines
Quoted 7 lines

A separate patch was posted adding Rust v0 symbol demangling with no libbfd dependency:
https://lore.kernel.org/lkml/[email protected]/
The legacy support should work with the non-libbfd demanglers as that's what we have today. We should really clean up Rust demangling and have tests. This is blocked on the Rust community responding to:
https://github.com/rust-lang/rust/issues/60705

Quoted 8 lines

I think #ifdef placements is not a big deal, but I still don't want to pull libcapstone details into the perf tree.

For LLVM, I think you should to build llvm-c-helpers anyway which means you still need LLVM headers and don't need to redefine the structures.

Can we do the same for capstone? I think it's best to use capstone headers directly and add a build option to use dlopen().

So I don't disagree. If we have headers but someone wants dlopen, let's use the headers and let them use dlopen. Unfortunately the way the build is set up isn't for that. The build assumes either:
1) you have libcapstone and its headers and want to link against it,
2) you don't have libcapstone and support is just removed.

In the changes the options become:
1) unchanged, if you have libcapstone then use the headers and link against - this is Andi's preferred approach,
2) if you don't have libcapstone dlopen is used with constants derived from pahole and baked into the sources - much as we do for vmlinux.h in BPF programs.

One way to achieve what you are asking for is to make the build do:
1) the link against libcapstone approach that needs the headers,
2) the dlopen with capstone.h headers,
but we need a way to build without libcapstone, so we get:
3) possibly dlopen with pahole derived constants - but if we have that then why do 2?
4) a no support option.

The problem is that if we don't do (3) then (4) will become the no libcapstone option and frankly people who care will do (1) and so (2) becomes redundant.
It was intentional doing the changes the way I have so that when you don't build with libcapstone, were you later to add the library you would gain support for it. This is with the down side of a small number of constants being in the code. Potentially these could change in the header files, but any such change is an ABI breakage and so unlikely to happen. So I think the way the patches have things set up is best.

Thanks,
Ian

#28 lore raw
In reply to Namhyung Kim
Quoted 8 lines · Namhyung Kim

I think #ifdef placements is not a big deal, but I still don't want to pull libcapstone details into the perf tree.

For LLVM, I think you should to build llvm-c-helpers anyway which means you still need LLVM headers and don't need to redefine the structures.

Can we do the same for capstone? I think it's best to use capstone headers directly and add a build option to use dlopen().

My two cents: if one wants to support some library, then have its devel packages available at build time.

Then, perf nowadays has lots of dependencies, we need to rein on that, making the good to have but not always used things to be dlopen'ed.

Like we did with gtk (that at this point I think is really deprecated, BTW).

gdb has prior art in this area that we could use, it is not even a TUI but it asks if debuginfo should be used and if so it goes on on potentially lenghty updates of the local buildid cache they keep (which is not the one we use, it should be).

And in the recent discussion with Dmitry Vyukov the possibility doing a question to the user about a default behaviour to be set and then using .perfconfig not to bother anymore the user about things is part of helping the user to deal with the myriad possibilites perf offers.

gdb could use that as well, why ask at every session if debuginfod should be used? Annoying.

I think perf should try to use what is available, both at build and at run time, and it shouldn't change the way it output things, but should warn the user about recent developments, things we over time figured out are problematic and thus a new default would be better, but then obtain consent if the user cares about it, and allow for backtracking, to go and change .perfconfig when the user realises the old output/behaviour is not really nice.

But keeping the grass green as it used to be should be the priority.

- Arnaldo

author reviewer
#29 lore raw
Quoted 41 lines · Arnaldo Carvalho de Melo
Quoted 8 lines · Namhyung Kim

I think #ifdef placements is not a big deal, but I still don't want to pull libcapstone details into the perf tree.

For LLVM, I think you should to build llvm-c-helpers anyway which means you still need LLVM headers and don't need to redefine the structures.

Can we do the same for capstone? I think it's best to use capstone headers directly and add a build option to use dlopen().

My two cents: if one wants to support some library, then have its devel packages available at build time.

Then, perf nowadays has lots of dependencies, we need to rein on that, making the good to have but not always used things to be dlopen'ed.

Like we did with gtk (that at this point I think is really deprecated, BTW).

gdb has prior art in this area that we could use, it is not even a TUI but it asks if debuginfo should be used and if so it goes on on potentially lenghty updates of the local buildid cache they keep (which is not the one we use, it should be).

And in the recent discussion with Dmitry Vyukov the possibility doing a question to the user about a default behaviour to be set and then using .perfconfig not to bother anymore the user about things is part of helping the user to deal with the myriad possibilites perf offers.

gdb could use that as well, why ask at every session if debuginfod should be used? Annoying.

I think perf should try to use what is available, both at build and at run time, and it shouldn't change the way it output things, but should warn the user about recent developments, things we over time figured out are problematic and thus a new default would be better, but then obtain consent if the user cares about it, and allow for backtracking, to go and change .perfconfig when the user realises the old output/behaviour is not really nice.

Quoted 1 line

But keeping the grass green as it used to be should be the priority.

So I don't understand what you are saying. If I have libcapstone installed should the build link against it or just use its headers for dlopen? Is declaring structs/constants to avoid needing the library acceptable?

The patches as they are will link against libcapstone if it's installed (just as currently happens) if not it will try to use dlopen at runtime, if that fails then you get the current no libcapstone not available at build time behavior. To support this a minimal amount of structs and constants are necessary. See here:
https://github.com/googleprodkernel/linux-perf/blob/google_tools_master/tools/perf/util/llvm.c#L21-L58
https://github.com/googleprodkernel/linux-perf/blob/google_tools_master/tools/perf/util/capstone.c#L23-L132

The patches try to make it look as though the same function exists with dlopen or directly calling by turning for example "cs_disasm" into:

```
static size_t perf_cs_disasm(csh handle, const uint8_t *code, size_t code_size,
uint64_t address, size_t count, struct cs_insn **insn)
{
#ifdef HAVE_LIBCAPSTONE_SUPPORT
return cs_disasm(handle, code, code_size, address, count, insn);
#else
static bool fn_init;
static enum cs_err (*fn)(csh handle, const uint8_t *code, size_t code_size,
uint64_t address, size_t count, struct cs_insn **insn);

if (!fn_init) {
fn = dlsym(perf_cs_dll_handle(), "cs_disasm");
if (!fn)
pr_debug("dlsym failed for cs_disasm\n");
fn_init = true;
}
if (!fn)
return CS_ERR_HANDLE;
return fn(handle, code, code_size, address, count, insn);
#endif
}
```

That is with the library linked it is just a direct call otherwise dlopen/dlsym are used. This means that the perf code using the library is unchanged except turning "cs_disasm" into "perf_cs_disasm" and we can unconditionally assume the perf_cs_disasm function will be available.

If we don't create the structs/constants ourselves, which I think is where controversy lies, then any function that calls cs_disasm needs to handle a situation where there is no cs_disasm at build time. I was trying to avoid this clutter in the code and just have things at the library boundary. The structs/constants become necessary as we can't assume we have access to the header file to support perf's current build.

Thanks,
Ian