Support dynamic opening of capstone and libLLVM
Ian Rogers
reviewercover Support dynamic opening of capstone and libLLVM
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.
The patch series:
1) moves the capstone and LLVM code to their own C files,
2) simplifies a little the capstone code,
3) adds perf_ variants of the functions that will either directly call the function or use dlsym to discover it.
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.
Ian Rogers (5):
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
tools/perf/Makefile.config | 13 + tools/perf/Makefile.perf | 23 +- tools/perf/builtin-script.c | 2 - tools/perf/tests/make | 2 + tools/perf/util/Build | 4 +- tools/perf/util/capstone.c | 723 +++++++++++++++++++++++++++++ tools/perf/util/capstone.h | 24 + tools/perf/util/disasm.c | 612 +----------------------- tools/perf/util/disasm.h | 2 + tools/perf/util/llvm-c-helpers.cpp | 113 ++++- tools/perf/util/llvm.c | 537 +++++++++++++++++++++ tools/perf/util/llvm.h | 24 + tools/perf/util/print_insn.c | 117 +---- tools/perf/util/srcline.c | 65 +-- tools/perf/util/srcline.h | 6 + 15 files changed, 1481 insertions(+), 786 deletions(-) create mode 100644 tools/perf/util/capstone.c create mode 100644 tools/perf/util/capstone.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
Ian Rogers
reviewerPATCH 1/5 perf capstone: Move capstone functionality into its own file
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
@@ -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
@@ -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
@@ -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
Ian Rogers
reviewerPATCH 2/5 perf llvm: Move llvm functionality into its own file
In reply to Ian RogersLLVM 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
@@ -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
@@ -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
@@ -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
@@ -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
Ian Rogers
reviewerPATCH 3/5 perf capstone: Remove open_capstone_handle
In reply to Ian Rogersopen_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
@@ -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, ¬es->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
Ian Rogers
reviewerPATCH 4/5 perf capstone: Support for dlopen-ing libcapstone.so
In reply to Ian RogersIf 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
@@ -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_SUPPORTstruct 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
Namhyung Kim
maintainerRe: [PATCH v1 4/5] perf capstone: Support for dlopen-ing libcapstone.so
Hi Ian,
Quoted 5 lines · 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.
I think reverse engineering the types is fragile. Can we simply change to dlopen if libcapstone is available?
Thanks,
Namhyung
Quoted 465 lines
Signed-off-by Ian Rogers <[email protected]>--- tools/perf/util/capstone.c | 287 ++++++++++++++++++++++++++++++++----- 1 file changed, 248 insertions(+), 39 deletions(-)tools/perf/util/capstone.c +248
-39@@ -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_SUPPORTstruct 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
Ian Rogers
reviewerRe: [PATCH v1 4/5] perf capstone: Support for dlopen-ing libcapstone.so
Quoted 4 lines · Namhyung Kim
Hi Ian,
On Mon, Jan 20, 2025 at 09:32:07AM -0800, Ian Rogers wrote:
Quoted 8 lines
Quoted 5 lines
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.I think reverse engineering the types is fragile. Can we simply change to dlopen if libcapstone is available?
The current behavior is if HAVE_LIBCAPSTONE_SUPPORT then we will link against libcapstone and can assume the header files are present.
The reverse engineering of structs and constants I agree is brittle, but these things shouldn't change, it is used in this code when the capstone/capstone.h isn't present. If capstone/capstone.h is required we could change the ifdefs to indicate:
1) capstone is present and we're linking against it (this is HAVE_LIBCAPSTONE_SUPPORT today);
2) capstone is present but use it by way of dlopen;
3) capstone isn't present don't #include, don't link and don't dlopen.
My thought was that this is less good than the reverse engineering.
(1) relies on the builder having libcapstone, which at build time is a warning that flies by. (2) also relies on the builder having to have libcapstone and to opt into a dlopen behavior, they'll need to read Makefiles to discover this. (3) gives a less good perf tool with in the source lots of __maybe_unused and ifdef-ed out functions all over the place.
With the reverse engineered values if you have libcapstone then HAVE_LIBCAPSTONE_SUPPORT works as before - no change. If you don't then you will be opted into getting something better than objdump - albeit it could break if the reverse engineered values drift from those in capstone/capstone.h. Less build options also is less complexity and cleaner code.
Thanks,
Ian
Ian Rogers
reviewerRe: [PATCH v1 4/5] perf capstone: Support for dlopen-ing libcapstone.so
Quoted 35 lines · Ian Rogers
Quoted 4 lines · Namhyung Kim
Hi Ian,
On Mon, Jan 20, 2025 at 09:32:07AM -0800, Ian Rogers wrote:
Quoted 8 lines
Quoted 5 lines
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.I think reverse engineering the types is fragile. Can we simply change to dlopen if libcapstone is available?
The current behavior is if
HAVE_LIBCAPSTONE_SUPPORTthen we will link against libcapstone and can assume the header files are present.The reverse engineering of structs and constants I agree is brittle, but these things shouldn't change, it is used in this code when the
capstone/capstone.hisn't present. Ifcapstone/capstone.his required we could change the ifdefs to indicate:
1) capstone is present and we're linking against it (this isHAVE_LIBCAPSTONE_SUPPORTtoday);
2) capstone is present but use it by way of dlopen;
3) capstone isn't present don't #include, don't link and don't dlopen.My thought was that this is less good than the reverse engineering.
(1) relies on the builder having libcapstone, which at build time is a warning that flies by. (2) also relies on the builder having to have libcapstone and to opt into a dlopen behavior, they'll need to read Makefiles to discover this. (3) gives a less good perf tool with in the source lots of__maybe_unusedand ifdef-ed out functions all over the place.
Quoted 6 lines
With the reverse engineered values if you have libcapstone then
HAVE_LIBCAPSTONE_SUPPORTworks as before - no change. If you don't then you will be opted into getting something better than objdump - albeit it could break if the reverse engineered values drift from those incapstone/capstone.h. Less build options also is less complexity and cleaner code.
To be clear, if the header file values/structs drift then libcapstone will need to version values/structs, we'll need to update the perf code to use these, etc. Otherwise a tool linked against an older libcapstone will break when libcapstone is updated. The code handling not having any libcapstone, dlopen-ed or not, would be significant as every static function will need an #ifdef to avoid a not used warning, every parameter will need a __maybe_unused and the overall code quality in perf will be worse imo because of this. The no libcapstone default when not detected would be crappy for users. The values/types added here are fewer than 20, and so are not a huge maintenance burden. I did a similar thing for the LLVM C++ symbolizer code and its maintenance would have been a burden and so abandoned it in the llvm-c-helpers case that mainly affects addr2line and not disassembly. Hopefully later LLVM C APIs will make this unnecessary.
Anyway, because of this I won't be changing this in later versions. I will be carrying these changes in Google's tree and think ideally it would be carried in the regular tree.
Thanks,
Ian
Ian Rogers
reviewerRe: [PATCH v1 4/5] perf capstone: Support for dlopen-ing libcapstone.so
Quoted 3 lines
Quoted 3 lines · Namhyung Kim
Quoted 2 lines
I think reverse engineering the types is fragile. Can we simply change to dlopen if libcapstone is available?
To understand the scope of the problem, here is the LLVM code:
https://github.com/googleprodkernel/linux-perf/blob/google_tools_master/tools/perf/util/llvm.c#L22
3 typedefs and 5 #defines on 13 lines of code.
The capstone code is larger primarily due to needing to define cs_x86 (and its dependencies) which are used for extra detail in printing:
https://github.com/googleprodkernel/linux-perf/blob/google_tools_master/tools/perf/util/capstone.c#L23-L132
We could opt out of providing the extra detail if capstone.h isn't present. I believe this can remove lines 49 to 123. This would mean there would be 37 lines of reverse engineered types - 2 structs and 5 enums.
My feeling is that this isn't significant and better than returning errors for capstone/llvm, having #ifdefs/__maybe_unused for the cases we lack the types throughout the code and relying on people building noticing a warning to realize they need to install a dependency - the alternative of failing the build if the capstone/LLVM header files aren't present, and unless some NO_... build option isn't passed, likely won't fly with perf developers.
Thanks,
Ian
Namhyung Kim
maintainerRe: [PATCH v1 4/5] perf capstone: Support for dlopen-ing libcapstone.so
Quoted 7 lines · Ian Rogers
Quoted 3 lines
Quoted 3 lines · Namhyung Kim
Quoted 2 lines
I think reverse engineering the types is fragile. Can we simply change to dlopen if libcapstone is available?
To understand the scope of the problem, here is the LLVM code:
https://github.com/googleprodkernel/linux-perf/blob/google_tools_master/tools/perf/util/llvm.c#L22
3 typedefs and 5 #defines on 13 lines of code.
It doesn't look that bad than I thought. :)
Quoted 8 lines
The capstone code is larger primarily due to needing to define
cs_x86(and its dependencies) which are used for extra detail in printing:
https://github.com/googleprodkernel/linux-perf/blob/google_tools_master/tools/perf/util/capstone.c#L23-L132
We could opt out of providing the extra detail ifcapstone.hisn't present. I believe this can remove lines 49 to 123. This would mean there would be 37 lines of reverse engineered types - 2 structs and 5 enums.
Probably, we don't need to carry the full details of instructions when the library support was not built.
Quoted 8 lines
My feeling is that this isn't significant and better than returning errors for capstone/llvm, having #ifdefs/__maybe_unused for the cases we lack the types throughout the code and relying on people building noticing a warning to realize they need to install a dependency - the alternative of failing the build if the capstone/LLVM header files aren't present, and unless some
NO_... build option isn't passed, likely won't fly with perf developers.
I don't think failing the build due to the optional dependencies is an option. I'm not against the idea of dlopen but my concern is to carry those external definitions in the perf code base. Maybe it'd be ok if we could keep it minimally.
Thanks,
Namhyung
Ian Rogers
reviewerPATCH 5/5 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, 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
@@ -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
@@ -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
@@ -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
@@ -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
Ian Rogers
reviewerQuoted 13 lines · Ian Rogers
Linking against libcapstone and libLLVM can be a significant increase in dependencies and size of memory footprint. For something like
perf recordthe disassembler and addr2line functionality won't be used. Support dynamically loading these libraries using dlopen and then calling the appropriate functions found using dlsym.The patch series:
1) moves the capstone and LLVM code to their own C files,
2) simplifies a little the capstone code,
3) addsperf_variants of the functions that will either directly call the function or use dlsym to discover it.
Quoted 4 lines
The addr2line LLVM functionality is written in C++. To avoid linking against libLLVM for this, a new
LIBLLVM_DYNAMICoption 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.
A question for the LLVM folks longer term, could the llvm/DebugInfo/Symbolize/Symbolize.h functionality be added to the llvm-c API (ie the not C++ one) for the sake of perf's use using dlsym for its addr2line implementation? This would enable the libperf-llvm.so library added in these patches to be removed and libLLVM.so used in its place.
Thanks,
Ian
Quoted 31 lines
Ian Rogers (5):
perf capstone: Move capstone functionality into its own file perf llvm: Move llvm functionality into its own file perf capstone: Removeopen_capstone_handle
perf capstone: Support for dlopen-ing libcapstone.so perf llvm: Support for dlopen-ing libLLVM.sotools/perf/Makefile.config | 13 + tools/perf/Makefile.perf | 23 +- tools/perf/builtin-script.c | 2 - tools/perf/tests/make | 2 + tools/perf/util/Build | 4 +- tools/perf/util/capstone.c | 723 +++++++++++++++++++++++++++++ tools/perf/util/capstone.h | 24 + tools/perf/util/disasm.c | 612 +----------------------- tools/perf/util/disasm.h | 2 + tools/perf/util/llvm-c-helpers.cpp | 113 ++++- tools/perf/util/llvm.c | 537 +++++++++++++++++++++ tools/perf/util/llvm.h | 24 + tools/perf/util/print_insn.c | 117 +---- tools/perf/util/srcline.c | 65 +-- tools/perf/util/srcline.h | 6 + 15 files changed, 1481 insertions(+), 786 deletions(-) create mode 100644 tools/perf/util/capstone.c create mode 100644 tools/perf/util/capstone.h create mode 100644 tools/perf/util/llvm.c create mode 100644 tools/perf/util/llvm.h--
2.48.0.rc2.279.g1de40edade-goog