mirror of
https://github.com/torvalds/linux.git
synced 2025-12-07 20:06:24 +00:00
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>
This commit is contained in:
committed by
Arnaldo Carvalho de Melo
parent
6eca7c5ac2
commit
0488568178
@@ -8,8 +8,10 @@
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wunused-parameter" /* Needed for LLVM <= 15 */
|
||||
#include <llvm/DebugInfo/Symbolize/Symbolize.h>
|
||||
#include <llvm/Support/TargetSelect.h>
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <linux/compiler.h>
|
||||
@@ -19,6 +21,9 @@ extern "C" {
|
||||
#include "symbol_conf.h"
|
||||
#include "llvm-c-helpers.h"
|
||||
|
||||
extern "C"
|
||||
char *dso__demangle_sym(struct dso *dso, int kmodule, const char *elf_name);
|
||||
|
||||
using namespace llvm;
|
||||
using llvm::symbolize::LLVMSymbolizer;
|
||||
|
||||
@@ -132,3 +137,61 @@ int llvm_addr2line(const char *dso_name, u64 addr,
|
||||
return extract_file_and_line(*res_or_err, file, line);
|
||||
}
|
||||
}
|
||||
|
||||
static char *
|
||||
make_symbol_relative_string(struct dso *dso, const char *sym_name,
|
||||
u64 addr, u64 base_addr)
|
||||
{
|
||||
if (!strcmp(sym_name, "<invalid>"))
|
||||
return NULL;
|
||||
|
||||
char *demangled = dso__demangle_sym(dso, 0, sym_name);
|
||||
if (base_addr && base_addr != addr) {
|
||||
char buf[256];
|
||||
snprintf(buf, sizeof(buf), "%s+0x%" PRIx64,
|
||||
demangled ? demangled : sym_name, addr - base_addr);
|
||||
free(demangled);
|
||||
return strdup(buf);
|
||||
} else {
|
||||
if (demangled)
|
||||
return demangled;
|
||||
else
|
||||
return strdup(sym_name);
|
||||
}
|
||||
}
|
||||
|
||||
extern "C"
|
||||
char *llvm_name_for_code(struct dso *dso, const char *dso_name, u64 addr)
|
||||
{
|
||||
LLVMSymbolizer *symbolizer = get_symbolizer();
|
||||
object::SectionedAddress sectioned_addr = {
|
||||
addr,
|
||||
object::SectionedAddress::UndefSection
|
||||
};
|
||||
Expected<DILineInfo> res_or_err =
|
||||
symbolizer->symbolizeCode(dso_name, sectioned_addr);
|
||||
if (!res_or_err) {
|
||||
return NULL;
|
||||
}
|
||||
return make_symbol_relative_string(
|
||||
dso, res_or_err->FunctionName.c_str(),
|
||||
addr, res_or_err->StartAddress ? *res_or_err->StartAddress : 0);
|
||||
}
|
||||
|
||||
extern "C"
|
||||
char *llvm_name_for_data(struct dso *dso, const char *dso_name, u64 addr)
|
||||
{
|
||||
LLVMSymbolizer *symbolizer = get_symbolizer();
|
||||
object::SectionedAddress sectioned_addr = {
|
||||
addr,
|
||||
object::SectionedAddress::UndefSection
|
||||
};
|
||||
Expected<DIGlobal> res_or_err =
|
||||
symbolizer->symbolizeData(dso_name, sectioned_addr);
|
||||
if (!res_or_err) {
|
||||
return NULL;
|
||||
}
|
||||
return make_symbol_relative_string(
|
||||
dso, res_or_err->Name.c_str(),
|
||||
addr, res_or_err->Start);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user