Alexander Potapenko | 8c07f55 | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 1 | //===-- llvm-symbolizer.cpp - Simple addr2line-like symbolizer ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This utility works much like "addr2line". It is able of transforming |
| 11 | // tuples (module name, module offset) to code locations (function name, |
| 12 | // file, line number, column number). It is targeted for compiler-rt tools |
| 13 | // (especially AddressSanitizer and ThreadSanitizer) that can use it |
| 14 | // to symbolize stack traces in their error reports. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Alexander Potapenko | 8c07f55 | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringRef.h" |
Alexey Samsonov | d6aa820 | 2015-11-03 22:20:52 +0000 | [diff] [blame] | 19 | #include "llvm/DebugInfo/Symbolize/DIPrinter.h" |
Alexey Samsonov | 57f8837 | 2015-10-26 17:56:12 +0000 | [diff] [blame] | 20 | #include "llvm/DebugInfo/Symbolize/Symbolize.h" |
Zachary Turner | 20dbd0d | 2015-04-27 17:19:51 +0000 | [diff] [blame] | 21 | #include "llvm/Support/COM.h" |
Alexander Potapenko | 8c07f55 | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 22 | #include "llvm/Support/CommandLine.h" |
| 23 | #include "llvm/Support/Debug.h" |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 24 | #include "llvm/Support/FileSystem.h" |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 25 | #include "llvm/Support/InitLLVM.h" |
Benjamin Kramer | 16132e6 | 2015-03-23 18:07:13 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Path.h" |
Alexander Potapenko | 8c07f55 | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Alexander Potapenko | 8c07f55 | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 28 | #include <cstdio> |
| 29 | #include <cstring> |
Alexander Potapenko | 8c07f55 | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 30 | #include <string> |
| 31 | |
| 32 | using namespace llvm; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 33 | using namespace symbolize; |
Alexander Potapenko | 8c07f55 | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 34 | |
| 35 | static cl::opt<bool> |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 36 | ClUseSymbolTable("use-symbol-table", cl::init(true), |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 37 | cl::desc("Prefer names in symbol table to names " |
| 38 | "in debug info")); |
Alexander Potapenko | 8c07f55 | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 39 | |
Alexey Samsonov | cd01472 | 2014-05-17 00:07:48 +0000 | [diff] [blame] | 40 | static cl::opt<FunctionNameKind> ClPrintFunctions( |
| 41 | "functions", cl::init(FunctionNameKind::LinkageName), |
| 42 | cl::desc("Print function name for a given address:"), |
| 43 | cl::values(clEnumValN(FunctionNameKind::None, "none", "omit function name"), |
| 44 | clEnumValN(FunctionNameKind::ShortName, "short", |
| 45 | "print short function name"), |
| 46 | clEnumValN(FunctionNameKind::LinkageName, "linkage", |
Mehdi Amini | 732afdd | 2016-10-08 19:41:06 +0000 | [diff] [blame] | 47 | "print function linkage name"))); |
Alexander Potapenko | 8c07f55 | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 48 | |
| 49 | static cl::opt<bool> |
Zachary Turner | c007aa4 | 2015-05-06 22:26:30 +0000 | [diff] [blame] | 50 | ClUseRelativeAddress("relative-address", cl::init(false), |
| 51 | cl::desc("Interpret addresses as relative addresses"), |
| 52 | cl::ReallyHidden); |
| 53 | |
| 54 | static cl::opt<bool> |
| 55 | ClPrintInlining("inlining", cl::init(true), |
| 56 | cl::desc("Print all inlined frames for a given address")); |
Alexander Potapenko | 8c07f55 | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 57 | |
| 58 | static cl::opt<bool> |
Alexey Samsonov | d5d7bb5 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 59 | ClDemangle("demangle", cl::init(true), cl::desc("Demangle function names")); |
Alexander Potapenko | 8c07f55 | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 60 | |
Alexey Samsonov | 2ca6536 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 61 | static cl::opt<std::string> ClDefaultArch("default-arch", cl::init(""), |
| 62 | cl::desc("Default architecture " |
| 63 | "(for multi-arch objects)")); |
| 64 | |
Alexey Samsonov | 60e59e2 | 2013-12-24 19:33:22 +0000 | [diff] [blame] | 65 | static cl::opt<std::string> |
| 66 | ClBinaryName("obj", cl::init(""), |
| 67 | cl::desc("Path to object file to be symbolized (if not provided, " |
| 68 | "object file should be specified for each input line)")); |
| 69 | |
David Blaikie | e5adb68 | 2017-07-30 01:34:08 +0000 | [diff] [blame] | 70 | static cl::opt<std::string> |
| 71 | ClDwpName("dwp", cl::init(""), |
| 72 | cl::desc("Path to DWP file to be use for any split CUs")); |
| 73 | |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 74 | static cl::list<std::string> |
| 75 | ClDsymHint("dsym-hint", cl::ZeroOrMore, |
| 76 | cl::desc("Path to .dSYM bundles to search for debug info for the " |
| 77 | "object files")); |
Hemant Kulkarni | 80f82fb | 2015-10-12 19:26:44 +0000 | [diff] [blame] | 78 | static cl::opt<bool> |
| 79 | ClPrintAddress("print-address", cl::init(false), |
| 80 | cl::desc("Show address before line information")); |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 81 | |
Hemant Kulkarni | bdce12a | 2015-11-11 20:41:43 +0000 | [diff] [blame] | 82 | static cl::opt<bool> |
| 83 | ClPrettyPrint("pretty-print", cl::init(false), |
| 84 | cl::desc("Make the output more human friendly")); |
| 85 | |
Mike Aizatsky | 17dbc28 | 2016-01-09 00:14:35 +0000 | [diff] [blame] | 86 | static cl::opt<int> ClPrintSourceContextLines( |
| 87 | "print-source-context-lines", cl::init(0), |
| 88 | cl::desc("Print N number of source file context")); |
| 89 | |
David Blaikie | 0012dd5 | 2017-01-31 22:19:38 +0000 | [diff] [blame] | 90 | static cl::opt<bool> ClVerbose("verbose", cl::init(false), |
| 91 | cl::desc("Print verbose line info")); |
| 92 | |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 93 | template<typename T> |
| 94 | static bool error(Expected<T> &ResOrErr) { |
| 95 | if (ResOrErr) |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 96 | return false; |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 97 | logAllUnhandledErrors(ResOrErr.takeError(), errs(), |
| 98 | "LLVMSymbolizer: error reading file: "); |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 99 | return true; |
| 100 | } |
| 101 | |
Mike Aizatsky | 54a7c69 | 2016-01-07 23:57:41 +0000 | [diff] [blame] | 102 | static bool parseCommand(StringRef InputString, bool &IsData, |
| 103 | std::string &ModuleName, uint64_t &ModuleOffset) { |
Mike Aizatsky | 54a7c69 | 2016-01-07 23:57:41 +0000 | [diff] [blame] | 104 | const char kDelimiters[] = " \n\r"; |
Alexander Potapenko | 8c07f55 | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 105 | ModuleName = ""; |
Fangrui Song | 3b7499d | 2018-05-25 00:11:15 +0000 | [diff] [blame] | 106 | if (InputString.consume_front("CODE ")) { |
Dmitry Vyukov | d08bd13 | 2013-01-11 07:16:20 +0000 | [diff] [blame] | 107 | IsData = false; |
Fangrui Song | 3b7499d | 2018-05-25 00:11:15 +0000 | [diff] [blame] | 108 | } else if (InputString.consume_front("DATA ")) { |
| 109 | IsData = true; |
Dmitry Vyukov | d08bd13 | 2013-01-11 07:16:20 +0000 | [diff] [blame] | 110 | } else { |
| 111 | // If no cmd, assume it's CODE. |
| 112 | IsData = false; |
| 113 | } |
Fangrui Song | 3b7499d | 2018-05-25 00:11:15 +0000 | [diff] [blame] | 114 | const char *pos = InputString.data(); |
Alexey Samsonov | 60e59e2 | 2013-12-24 19:33:22 +0000 | [diff] [blame] | 115 | // Skip delimiters and parse input filename (if needed). |
Fangrui Song | ffebfe1 | 2018-05-26 02:29:14 +0000 | [diff] [blame^] | 116 | if (ClBinaryName.empty()) { |
Alexey Samsonov | 60e59e2 | 2013-12-24 19:33:22 +0000 | [diff] [blame] | 117 | pos += strspn(pos, kDelimiters); |
| 118 | if (*pos == '"' || *pos == '\'') { |
| 119 | char quote = *pos; |
| 120 | pos++; |
Mike Aizatsky | 54a7c69 | 2016-01-07 23:57:41 +0000 | [diff] [blame] | 121 | const char *end = strchr(pos, quote); |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 122 | if (!end) |
Alexey Samsonov | 60e59e2 | 2013-12-24 19:33:22 +0000 | [diff] [blame] | 123 | return false; |
| 124 | ModuleName = std::string(pos, end - pos); |
| 125 | pos = end + 1; |
| 126 | } else { |
| 127 | int name_length = strcspn(pos, kDelimiters); |
| 128 | ModuleName = std::string(pos, name_length); |
| 129 | pos += name_length; |
| 130 | } |
Alexey Samsonov | d206932 | 2013-04-05 09:22:24 +0000 | [diff] [blame] | 131 | } else { |
Alexey Samsonov | 60e59e2 | 2013-12-24 19:33:22 +0000 | [diff] [blame] | 132 | ModuleName = ClBinaryName; |
Alexander Potapenko | 8c07f55 | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 133 | } |
Alexey Samsonov | d206932 | 2013-04-05 09:22:24 +0000 | [diff] [blame] | 134 | // Skip delimiters and parse module offset. |
| 135 | pos += strspn(pos, kDelimiters); |
| 136 | int offset_length = strcspn(pos, kDelimiters); |
Rafael Espindola | 2c5bcc5 | 2015-10-24 23:23:25 +0000 | [diff] [blame] | 137 | return !StringRef(pos, offset_length).getAsInteger(0, ModuleOffset); |
Alexander Potapenko | 8c07f55 | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | int main(int argc, char **argv) { |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 141 | InitLLVM X(argc, argv); |
Alexander Potapenko | 8c07f55 | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 142 | |
Zachary Turner | 20dbd0d | 2015-04-27 17:19:51 +0000 | [diff] [blame] | 143 | llvm::sys::InitializeCOMRAII COM(llvm::sys::COMThreadingMode::MultiThreaded); |
| 144 | |
Alexey Samsonov | 60e59e2 | 2013-12-24 19:33:22 +0000 | [diff] [blame] | 145 | cl::ParseCommandLineOptions(argc, argv, "llvm-symbolizer\n"); |
Alexey Samsonov | 46c1ce6 | 2015-10-30 00:40:20 +0000 | [diff] [blame] | 146 | LLVMSymbolizer::Options Opts(ClPrintFunctions, ClUseSymbolTable, ClDemangle, |
Zachary Turner | c007aa4 | 2015-05-06 22:26:30 +0000 | [diff] [blame] | 147 | ClUseRelativeAddress, ClDefaultArch); |
Hemant Kulkarni | bdce12a | 2015-11-11 20:41:43 +0000 | [diff] [blame] | 148 | |
Alexander Potapenko | 7aaf514 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 149 | for (const auto &hint : ClDsymHint) { |
| 150 | if (sys::path::extension(hint) == ".dSYM") { |
| 151 | Opts.DsymHints.push_back(hint); |
| 152 | } else { |
| 153 | errs() << "Warning: invalid dSYM hint: \"" << hint << |
| 154 | "\" (must have the '.dSYM' extension).\n"; |
| 155 | } |
| 156 | } |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 157 | LLVMSymbolizer Symbolizer(Opts); |
Alexander Potapenko | 8c07f55 | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 158 | |
Hemant Kulkarni | bdce12a | 2015-11-11 20:41:43 +0000 | [diff] [blame] | 159 | DIPrinter Printer(outs(), ClPrintFunctions != FunctionNameKind::None, |
David Blaikie | 0012dd5 | 2017-01-31 22:19:38 +0000 | [diff] [blame] | 160 | ClPrettyPrint, ClPrintSourceContextLines, ClVerbose); |
Alexey Samsonov | d6aa820 | 2015-11-03 22:20:52 +0000 | [diff] [blame] | 161 | |
Mike Aizatsky | 54a7c69 | 2016-01-07 23:57:41 +0000 | [diff] [blame] | 162 | const int kMaxInputStringLength = 1024; |
| 163 | char InputString[kMaxInputStringLength]; |
| 164 | |
| 165 | while (true) { |
| 166 | if (!fgets(InputString, sizeof(InputString), stdin)) |
| 167 | break; |
| 168 | |
| 169 | bool IsData = false; |
| 170 | std::string ModuleName; |
| 171 | uint64_t ModuleOffset = 0; |
| 172 | if (!parseCommand(StringRef(InputString), IsData, ModuleName, |
| 173 | ModuleOffset)) { |
| 174 | outs() << InputString; |
| 175 | continue; |
| 176 | } |
| 177 | |
Hemant Kulkarni | 80f82fb | 2015-10-12 19:26:44 +0000 | [diff] [blame] | 178 | if (ClPrintAddress) { |
| 179 | outs() << "0x"; |
| 180 | outs().write_hex(ModuleOffset); |
Fangrui Song | 3b7499d | 2018-05-25 00:11:15 +0000 | [diff] [blame] | 181 | StringRef Delimiter = ClPrettyPrint ? ": " : "\n"; |
Hemant Kulkarni | bdce12a | 2015-11-11 20:41:43 +0000 | [diff] [blame] | 182 | outs() << Delimiter; |
Hemant Kulkarni | 80f82fb | 2015-10-12 19:26:44 +0000 | [diff] [blame] | 183 | } |
Alexey Samsonov | d6aa820 | 2015-11-03 22:20:52 +0000 | [diff] [blame] | 184 | if (IsData) { |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 185 | auto ResOrErr = Symbolizer.symbolizeData(ModuleName, ModuleOffset); |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 186 | Printer << (error(ResOrErr) ? DIGlobal() : ResOrErr.get()); |
Alexey Samsonov | d6aa820 | 2015-11-03 22:20:52 +0000 | [diff] [blame] | 187 | } else if (ClPrintInlining) { |
David Blaikie | e5adb68 | 2017-07-30 01:34:08 +0000 | [diff] [blame] | 188 | auto ResOrErr = |
| 189 | Symbolizer.symbolizeInlinedCode(ModuleName, ModuleOffset, ClDwpName); |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 190 | Printer << (error(ResOrErr) ? DIInliningInfo() |
Alexey Samsonov | 884adda | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 191 | : ResOrErr.get()); |
Alexey Samsonov | d6aa820 | 2015-11-03 22:20:52 +0000 | [diff] [blame] | 192 | } else { |
David Blaikie | e5adb68 | 2017-07-30 01:34:08 +0000 | [diff] [blame] | 193 | auto ResOrErr = |
| 194 | Symbolizer.symbolizeCode(ModuleName, ModuleOffset, ClDwpName); |
Reid Kleckner | f27f3f8 | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 195 | Printer << (error(ResOrErr) ? DILineInfo() : ResOrErr.get()); |
Alexey Samsonov | d6aa820 | 2015-11-03 22:20:52 +0000 | [diff] [blame] | 196 | } |
| 197 | outs() << "\n"; |
Alexey Samsonov | ea83baf | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 198 | outs().flush(); |
Alexander Potapenko | 8c07f55 | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 199 | } |
Zachary Turner | 20dbd0d | 2015-04-27 17:19:51 +0000 | [diff] [blame] | 200 | |
Alexander Potapenko | 8c07f55 | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 201 | return 0; |
| 202 | } |