|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | + |
| 3 | +/* |
| 4 | + * Must come before the linux/compiler.h include, which defines several |
| 5 | + * macros (e.g. noinline) that conflict with compiler builtins used |
| 6 | + * by LLVM. |
| 7 | + */ |
| 8 | +#pragma GCC diagnostic push |
| 9 | +#pragma GCC diagnostic ignored "-Wunused-parameter" /* Needed for LLVM <= 15 */ |
| 10 | +#include <llvm/DebugInfo/Symbolize/Symbolize.h> |
| 11 | +#pragma GCC diagnostic pop |
| 12 | + |
| 13 | +#include <stdio.h> |
| 14 | +#include <sys/types.h> |
| 15 | +#include <linux/compiler.h> |
| 16 | +extern "C" { |
| 17 | +#include <linux/zalloc.h> |
| 18 | +} |
| 19 | +#include "symbol_conf.h" |
| 20 | +#include "llvm-c-helpers.h" |
| 21 | + |
| 22 | +using namespace llvm; |
| 23 | +using llvm::symbolize::LLVMSymbolizer; |
| 24 | + |
| 25 | +/* |
| 26 | + * Allocate a static LLVMSymbolizer, which will live to the end of the program. |
| 27 | + * Unlike the bfd paths, LLVMSymbolizer has its own cache, so we do not need |
| 28 | + * to store anything in the dso struct. |
| 29 | + */ |
| 30 | +static LLVMSymbolizer *get_symbolizer() |
| 31 | +{ |
| 32 | + static LLVMSymbolizer *instance = nullptr; |
| 33 | + if (instance == nullptr) { |
| 34 | + LLVMSymbolizer::Options opts; |
| 35 | + /* |
| 36 | + * LLVM sometimes demangles slightly different from the rest |
| 37 | + * of the code, and this mismatch can cause new_inline_sym() |
| 38 | + * to get confused and mark non-inline symbol as inlined |
| 39 | + * (since the name does not properly match up with base_sym). |
| 40 | + * Thus, disable the demangling and let the rest of the code |
| 41 | + * handle it. |
| 42 | + */ |
| 43 | + opts.Demangle = false; |
| 44 | + instance = new LLVMSymbolizer(opts); |
| 45 | + } |
| 46 | + return instance; |
| 47 | +} |
| 48 | + |
| 49 | +/* Returns 0 on error, 1 on success. */ |
| 50 | +static int extract_file_and_line(const DILineInfo &line_info, char **file, |
| 51 | + unsigned int *line) |
| 52 | +{ |
| 53 | + if (file) { |
| 54 | + if (line_info.FileName == "<invalid>") { |
| 55 | + /* Match the convention of libbfd. */ |
| 56 | + *file = nullptr; |
| 57 | + } else { |
| 58 | + /* The caller expects to get something it can free(). */ |
| 59 | + *file = strdup(line_info.FileName.c_str()); |
| 60 | + if (*file == nullptr) |
| 61 | + return 0; |
| 62 | + } |
| 63 | + } |
| 64 | + if (line) |
| 65 | + *line = line_info.Line; |
| 66 | + return 1; |
| 67 | +} |
| 68 | + |
| 69 | +extern "C" |
| 70 | +int llvm_addr2line(const char *dso_name, u64 addr, |
| 71 | + char **file, unsigned int *line, |
| 72 | + bool unwind_inlines, |
| 73 | + llvm_a2l_frame **inline_frames) |
| 74 | +{ |
| 75 | + LLVMSymbolizer *symbolizer = get_symbolizer(); |
| 76 | + object::SectionedAddress sectioned_addr = { |
| 77 | + addr, |
| 78 | + object::SectionedAddress::UndefSection |
| 79 | + }; |
| 80 | + |
| 81 | + if (unwind_inlines) { |
| 82 | + Expected<DIInliningInfo> res_or_err = |
| 83 | + symbolizer->symbolizeInlinedCode(dso_name, |
| 84 | + sectioned_addr); |
| 85 | + if (!res_or_err) |
| 86 | + return 0; |
| 87 | + unsigned num_frames = res_or_err->getNumberOfFrames(); |
| 88 | + if (num_frames == 0) |
| 89 | + return 0; |
| 90 | + |
| 91 | + if (extract_file_and_line(res_or_err->getFrame(0), |
| 92 | + file, line) == 0) |
| 93 | + return 0; |
| 94 | + |
| 95 | + *inline_frames = (llvm_a2l_frame *)calloc( |
| 96 | + num_frames, sizeof(**inline_frames)); |
| 97 | + if (*inline_frames == nullptr) |
| 98 | + return 0; |
| 99 | + |
| 100 | + for (unsigned i = 0; i < num_frames; ++i) { |
| 101 | + const DILineInfo &src = res_or_err->getFrame(i); |
| 102 | + |
| 103 | + llvm_a2l_frame &dst = (*inline_frames)[i]; |
| 104 | + if (src.FileName == "<invalid>") |
| 105 | + /* Match the convention of libbfd. */ |
| 106 | + dst.filename = nullptr; |
| 107 | + else |
| 108 | + dst.filename = strdup(src.FileName.c_str()); |
| 109 | + dst.funcname = strdup(src.FunctionName.c_str()); |
| 110 | + dst.line = src.Line; |
| 111 | + |
| 112 | + if (dst.filename == nullptr || |
| 113 | + dst.funcname == nullptr) { |
| 114 | + for (unsigned j = 0; j <= i; ++j) { |
| 115 | + zfree(&(*inline_frames)[j].filename); |
| 116 | + zfree(&(*inline_frames)[j].funcname); |
| 117 | + } |
| 118 | + zfree(inline_frames); |
| 119 | + return 0; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return num_frames; |
| 124 | + } else { |
| 125 | + if (inline_frames) |
| 126 | + *inline_frames = nullptr; |
| 127 | + |
| 128 | + Expected<DILineInfo> res_or_err = |
| 129 | + symbolizer->symbolizeCode(dso_name, sectioned_addr); |
| 130 | + if (!res_or_err) |
| 131 | + return 0; |
| 132 | + return extract_file_and_line(*res_or_err, file, line); |
| 133 | + } |
| 134 | +} |
0 commit comments