perf capstone/llvm dlopen support
Ian Rogers
reviewerv10 cover perf capstone/llvm dlopen support
Linking against libcapstone and libLLVM can be a significant increase in dependencies and file size if building statically. The dependencies are also quite cumbersome if bringing perf into a distribution. For something like perf record the disassembler and addr2line functionality of libcapstone and libLLVM won't be used. These patches support dynamically loading these libraries using dlopen and then calling the appropriate functions found using dlsym. Using dlopen allows libcapstone and libLLVM to be installed separately to perf and when that's done the performance will improve as separate commands for objdump and addr2line won't be invoked.
The patch series adds perf_ variants of the capstone/llvm functions that will either directly call the function or use dlopen/dlsym to discover and then call the function.
For capstone if there is no capstone.h then the feature test will fail, there is no capstone support and HAVE_LIBCAPSTONE_SUPPORT won't be defined. This is necessary as the libcapstone structs and enum constants vary across releases and perf cannot know what to use without having the header file. If HAVE_LIBCAPSTONE_SUPPORT is defined and the build made with LIBCAPSTONE_DLOPEN=1 then dlopen is used to open libcapstone.so, etc. If LIBCAPSTONE_DLOPEN isn't given to the build then the build will try to link against libcapstone (as with before these changes).
For LLVM if the feature test succeeded then HAVE_LIBLLVM_SUPPORT is defined and libLLVM is linked against. If the feature test fails, or NO_LIBLLVM=1 given, then dlopen is used to open the library. Five constants and three pointer types are declared to avoid having a header file dependency - ie there is no case where the LLVM support won't try to at least dlopen libLLVM.so.
The LLVM addr2line ABIs are written in C++ with no C counterpart. When !HAVE_LIBLLVM_SUPPORT the LLVM addr2line support is compiled out except when a new LIBLLVM_DYNAMIC build option is given. This option builds a libperf-llvm.so that contains the LLVM addr2line implementation and that creates the dlopen/dlsym calls to use this in the regular code. Ideally LLVM would extend their C API to avoid this.
v10: Make the libcapstone code use capstone.h and conditionally use
dlopen as the constants and structs libcapstone declares vary across releases.
v9: Rebase.
https://lore.kernel.org/lkml/[email protected]/
v8: Rebase down to 3 patches. Update commit and cover messages.
v7: Refactor now the first 5 patches, that largely moved code around,
have landed. Move the dlopen code to the end of the series so that the first 8 patches can be picked improving capstone/LLVM support without adding the dlopen code. Rename the cover letter and disassembler cleanup patches.
v6: Refactor the libbfd along with capstone and LLVM, previous patch
series had tried to avoid this by just removing the deprecated BUILD_NONDISTRO code. Remove the libtracefs removal into its own patch.
v5: Rebase and comment typo fix.
v4: Rebase and addition of a patch removing an unused struct variable.
v3: Add srcline addr2line fallback trying LLVM first then forking a
process. This came up in conversation with Steinar Gunderson <[email protected]>.
Tweak the cover letter message to try to address Andi Kleen's <[email protected]> feedback that the series doesn't really achieve anything.
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 (3):
perf capstone: Support for dlopen-ing libcapstone.so perf llvm: Support for dlopen-ing libLLVM.so perf llvm: Mangle libperf-llvm.so function names
tools/perf/Makefile.config | 21 ++- tools/perf/Makefile.perf | 24 ++- tools/perf/tests/make | 4 + tools/perf/util/Build | 4 +- tools/perf/util/capstone.c | 176 ++++++++++++++----- tools/perf/util/capstone.h | 33 ++++ tools/perf/util/llvm-c-helpers.cpp | 120 ++++++++++++- tools/perf/util/llvm-c-helpers.h | 24 ++- tools/perf/util/llvm.c | 273 +++++++++++++++++++++++++---- 9 files changed, 591 insertions(+), 88 deletions(-)
Signature · 1 line
2.53.0.rc1.225.gd81095ad13-goog
Ian Rogers
reviewerPATCH v10 1/3 perf capstone: Support for dlopen-ing libcapstone.so
If perf is built with LIBCAPSTONE_DLOPEN=1, support dlopen-ing libcapstone.so and then calling the necessary functions by looking them up using dlsym. The types come from capstone.h which means the libcapstone feature check needs to pass, and NO_CAPSTONE=1 hasn't been defined. This will cause the definition of HAVE_LIBCAPSTONE_SUPPORT.
Earlier versions of this code tried to declare the necessary capstone.h constants and structs, but they weren't stable and caused breakages across libcapstone releases.
--- tools/perf/Makefile.config | 8 +- tools/perf/tests/make | 2 + tools/perf/util/Build | 2 +- tools/perf/util/capstone.c | 176 +++++++++++++++++++++++++++++-------- tools/perf/util/capstone.h | 33 +++++++ 5 files changed, 179 insertions(+), 42 deletions(-)
tools/perf/Makefile.config +6 -2
@@ -1078,8 +1078,12 @@ ifndef NO_CAPSTONE $(call feature_check,libcapstone) ifeq ($(feature-libcapstone), 1) CFLAGS += -DHAVE_LIBCAPSTONE_SUPPORT $(LIBCAPSTONE_CFLAGS) - LDFLAGS += $(LICAPSTONE_LDFLAGS) - EXTLIBS += -lcapstone + ifdef LIBCAPSTONE_DLOPEN + CFLAGS += -DLIBCAPSTONE_DLOPEN + else + LDFLAGS += $(LIBCAPSTONE_LDFLAGS) + EXTLIBS += -lcapstone + endif $(call detected,CONFIG_LIBCAPSTONE) else msg := $(warning No libcapstone found, disables disasm engine support for 'perf script', please install libcapstone-dev/capstone-devel);
tools/perf/tests/make +2 -0
@@ -85,6 +85,7 @@ make_no_libdw := NO_LIBDW=1 make_libunwind := LIBUNWIND=1 make_no_backtrace := NO_BACKTRACE=1 make_no_libcapstone := NO_CAPSTONE=1 +make_libcapstone_dlopen := LIBCAPSTONE_DLOPEN=1 make_no_libnuma := NO_LIBNUMA=1 make_no_libbionic := NO_LIBBIONIC=1 make_no_libbpf := NO_LIBBPF=1
@@ -159,6 +160,7 @@ run += make_libunwind run += make_no_libdw_dwarf_unwind run += make_no_backtrace run += make_no_libcapstone +run += make_libcapstone_dlopen run += make_no_libnuma run += make_no_libbionic run += make_no_libbpf
tools/perf/util/Build +1 -1
@@ -11,7 +11,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-$(CONFIG_LIBCAPSTONE) += capstone.o perf-util-y += config.o perf-util-y += copyfile.o perf-util-y += ctype.o
tools/perf/util/capstone.c +137 -39
316 lines not on this page · raw message
tools/perf/util/capstone.h +33 -0
@@ -6,6 +6,7 @@#include <stdint.h> #include <stdio.h> #include <stdlib.h> +#include <linux/compiler.h> #include <linux/types.h> struct annotate_args ;
@@ -13,6 +14,7 @@ struct machine;struct symbol ;struct thread ; +#ifdef HAVE_LIBCAPSTONE_SUPPORT 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);
@@ -21,4 +23,35 @@ int symbol__disassemble_capstone(const char *filename, struct symbol *sym,int symbol__disassemble_capstone_powerpc (const char *filename,struct symbol *sym,struct annotate_args *args); +#else /* !HAVE_LIBCAPSTONE_SUPPORT */ +static inline ssize_t capstone__fprintf_insn_asm (struct machine *machine__maybe_unused , +struct thread *thread__maybe_unused , +u8 cpumode__maybe_unused , +bool is64bit__maybe_unused , +const uint8_t *code__maybe_unused , +size_t code_size__maybe_unused , +uint64_t ip__maybe_unused , +int *lenp__maybe_unused , +int print_opts__maybe_unused , +FILE *fp__maybe_unused ) +{ +return -1 ; +} + +static inline int symbol__disassemble_capstone (const char *filename__maybe_unused , +struct symbol *sym__maybe_unused , +struct annotate_args *args__maybe_unused ) +{ +return -1 ; +} + +static inline int symbol__disassemble_capstone_powerpc (const char *filename__maybe_unused , +struct symbol *sym__maybe_unused , +struct annotate_args *args__maybe_unused ) +{ +return -1 ; +} + +#endif /* HAVE_LIBCAPSTONE_SUPPORT */ +#endif /* __PERF_CAPSTONE_H */
Signature · 1 line
2.53.0.rc1.225.gd81095ad13-goog
Ian Rogers
reviewerPATCH v10 2/3 perf llvm: Support for dlopen-ing libLLVM.so
In reply to Ian RogersIf 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 by default don't support its use if HAVE_LIBLLVM_SUPPORT isn't present. Add a new build option LIBLLVM_DYNAMIC=1 that builds a separate shared object called libperf-llvm.so containing the LLVM addr2line functionality and support dynamic loading of it.
--- 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 | 273 +++++++++++++++++++++++++---- 6 files changed, 388 insertions(+), 38 deletions(-)
tools/perf/Makefile.config +13 -0
@@ -964,6 +964,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
@@ -422,6 +422,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))
@@ -989,6 +995,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)'
@@ -1090,6 +1106,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)'
@@ -1292,7 +1313,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 \ TAGS tags cscope* $(OUTPUT)PERF-VERSION-FILE \ $(OUTPUT)FEATURE-DUMP $(OUTPUT)util/*-bison* $(OUTPUT)util/*-flex* \
tools/perf/tests/make +2 -0
@@ -92,6 +92,7 @@ make_no_libbpf := NO_LIBBPF=1 make_libbpf_dynamic := LIBBPF_DYNAMIC=1 make_no_libbpf_DEBUG := NO_LIBBPF=1 DEBUG=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
@@ -166,6 +167,7 @@ run += make_no_libbionic run += make_no_libbpf run += make_no_libbpf_DEBUG 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
@@ -29,6 +29,7 @@ perf-util-y += find_bit.o perf-util-y += levenshtein.o perf-util-$(CONFIG_LIBBFD) += libbfd.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
@@ -249,7 +250,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-v0.o -perf-util-$(CONFIG_LIBLLVM) += llvm-c-helpers.o CFLAGS_demangle-rust-v0.o += -Wno-shadow -Wno-declaration-after-statement \ -Wno-switch-default -Wno-switch-enum -Wno-missing-field-initializers
tools/perf/util/llvm-c-helpers.cpp +107 -6
192 lines not on this page · raw message
tools/perf/util/llvm.c +243 -30
364 lines not on this page · raw message
Signature · 1 line
2.53.0.rc1.225.gd81095ad13-goog
Ian Rogers
reviewerPATCH v10 3/3 perf llvm: Mangle libperf-llvm.so function names
In reply to Ian RogersFor 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
@@ -996,7 +996,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
@@ -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
@@ -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.53.0.rc1.225.gd81095ad13-goog