linux-stable/tools/perf/util/llvm-c-helpers.h
Steinar H. Gunderson 0488568178 perf annotate: LLVM-based disassembler
Support using LLVM as a disassembler method, allowing helperless
annotation in non-distro builds. (It is also much faster than
using libbfd or bfd objdump on binaries with a lot of debug
information.)

This is nearly identical to the output of llvm-objdump; there are
some very rare whitespace differences, some minor changes to demangling
(since we use perf's regular demangling and not LLVM's own) and
the occasional case where llvm-objdump makes a different choice
when multiple symbols share the same address.

It should work across all of LLVM's supported architectures, although
I've only tested 64-bit x86, and finding the right triple from perf's
idea of machine architecture can sometimes be a bit tricky. Ideally, we
should have some way of finding the triplet just from the file itself.

Committer notes:

Address this on 32-bit systems by using PRIu64 from inttypes.h

     3    17.58 almalinux:9-i386              : FAIL gcc version 11.4.1 20231218 (Red Hat 11.4.1-3) (GCC)
      util/llvm-c-helpers.cpp: In function ‘char* make_symbol_relative_string(dso*, const char*, u64, u64)’:
      util/llvm-c-helpers.cpp:150:52: error: format ‘%lx’ expects argument of type ‘long unsigned int’, but argument 5 has type ‘u64’ {aka
  +‘long long unsigned int’} [-Werror=format=]
        150 |                 snprintf(buf, sizeof(buf), "%s+0x%lx",
            |                                                  ~~^
            |                                                    |
            |                                                    long unsigned int
            |                                                  %llx
        151 |                          demangled ? demangled : sym_name, addr - base_addr);
            |                                                            ~~~~~~~~~~~~~~~~
            |                                                                 |
            |                                                                 u64 {aka long long unsigned int}
      cc1plus: all warnings being treated as errors

Signed-off-by: Steinar H. Gunderson <sesse@google.com>
Cc: Ian Rogers <irogers@google.com>
Link: https://lore.kernel.org/r/20240803152008.2818485-3-sesse@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2024-09-03 10:39:20 -03:00

61 lines
1.9 KiB
C

/* SPDX-License-Identifier: GPL-2.0 */
#ifndef __PERF_LLVM_C_HELPERS
#define __PERF_LLVM_C_HELPERS 1
/*
* Helpers to call into LLVM C++ code from C, for the parts that do not have
* C APIs.
*/
#include <linux/compiler.h>
#ifdef __cplusplus
extern "C" {
#endif
struct dso;
struct llvm_a2l_frame {
char* filename;
char* funcname;
unsigned int line;
};
/*
* Implement addr2line() using libLLVM. LLVM is a C++ API, and
* many of the linux/ headers cannot be included in a C++ compile unit,
* so we need to make a little bridge code here. llvm_addr2line() will
* convert the inline frame information from LLVM's internal structures
* and put them into a flat array given in inline_frames. The caller
* is then responsible for taking that array and convert it into perf's
* regular inline frame structures (which depend on e.g. struct list_head).
*
* If the address could not be resolved, or an error occurred (e.g. OOM),
* returns 0. Otherwise, returns the number of inline frames (which means 1
* if the address was not part of an inlined function). If unwind_inlines
* is set and the return code is nonzero, inline_frames will be set to
* 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);
/*
* Simple symbolizers for addresses; will convert something like
* 0x12345 to "func+0x123". Will return NULL if no symbol was found.
*
* 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);
#ifdef __cplusplus
}
#endif
#endif /* __PERF_LLVM_C_HELPERS */