Alexander Potapenko | f41954b | 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 | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringRef.h" |
Alexey Samsonov | 55e87bb | 2015-11-03 22:20:52 +0000 | [diff] [blame] | 19 | #include "llvm/DebugInfo/Symbolize/DIPrinter.h" |
Alexey Samsonov | 0cd635f | 2015-10-26 17:56:12 +0000 | [diff] [blame] | 20 | #include "llvm/DebugInfo/Symbolize/Symbolize.h" |
Zachary Turner | 7b8e8e5 | 2015-04-27 17:19:51 +0000 | [diff] [blame] | 21 | #include "llvm/Support/COM.h" |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 22 | #include "llvm/Support/CommandLine.h" |
| 23 | #include "llvm/Support/Debug.h" |
Alexander Potapenko | 0fea775 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 24 | #include "llvm/Support/FileSystem.h" |
Rui Ueyama | 0b9d56a | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 25 | #include "llvm/Support/InitLLVM.h" |
Benjamin Kramer | df93f4b | 2015-03-23 18:07:13 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Path.h" |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 28 | #include <cstdio> |
| 29 | #include <cstring> |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 30 | #include <string> |
| 31 | |
| 32 | using namespace llvm; |
Alexey Samsonov | c4c7ea3 | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 33 | using namespace symbolize; |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 34 | |
| 35 | static cl::opt<bool> |
Alexey Samsonov | c4c7ea3 | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 36 | ClUseSymbolTable("use-symbol-table", cl::init(true), |
Alexey Samsonov | c4439c3 | 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 | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 39 | |
Alexey Samsonov | bf6e3f9 | 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 | 3ffe113 | 2016-10-08 19:41:06 +0000 | [diff] [blame] | 47 | "print function linkage name"))); |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 48 | |
| 49 | static cl::opt<bool> |
Zachary Turner | 43afa42 | 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 | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 57 | |
Dmitry Venikov | 2eebdab | 2019-01-16 07:05:58 +0000 | [diff] [blame] | 58 | // -demangle, -C |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 59 | static cl::opt<bool> |
Alexey Samsonov | c4439c3 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 60 | ClDemangle("demangle", cl::init(true), cl::desc("Demangle function names")); |
Dmitry Venikov | 2eebdab | 2019-01-16 07:05:58 +0000 | [diff] [blame] | 61 | static cl::alias |
| 62 | ClDemangleShort("C", cl::desc("Alias for -demangle"), |
| 63 | cl::NotHidden, cl::aliasopt(ClDemangle)); |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 64 | |
Alexey Samsonov | 8175bc3 | 2013-06-28 08:15:40 +0000 | [diff] [blame] | 65 | static cl::opt<std::string> ClDefaultArch("default-arch", cl::init(""), |
| 66 | cl::desc("Default architecture " |
| 67 | "(for multi-arch objects)")); |
| 68 | |
Dmitry Venikov | a16ce91 | 2019-01-11 11:51:52 +0000 | [diff] [blame] | 69 | // -obj, -exe, -e |
Alexey Samsonov | 42c9ecb | 2013-12-24 19:33:22 +0000 | [diff] [blame] | 70 | static cl::opt<std::string> |
| 71 | ClBinaryName("obj", cl::init(""), |
| 72 | cl::desc("Path to object file to be symbolized (if not provided, " |
| 73 | "object file should be specified for each input line)")); |
Dmitry Venikov | a16ce91 | 2019-01-11 11:51:52 +0000 | [diff] [blame] | 74 | static cl::alias |
| 75 | ClBinaryNameAliasExe("exe", cl::desc("Alias for -obj"), |
| 76 | cl::NotHidden, cl::aliasopt(ClBinaryName)); |
| 77 | static cl::alias |
| 78 | ClBinaryNameAliasE("e", cl::desc("Alias for -obj"), |
| 79 | cl::NotHidden, cl::aliasopt(ClBinaryName)); |
| 80 | |
Alexey Samsonov | 42c9ecb | 2013-12-24 19:33:22 +0000 | [diff] [blame] | 81 | |
David Blaikie | 4cf772e | 2017-07-30 01:34:08 +0000 | [diff] [blame] | 82 | static cl::opt<std::string> |
| 83 | ClDwpName("dwp", cl::init(""), |
| 84 | cl::desc("Path to DWP file to be use for any split CUs")); |
| 85 | |
Alexander Potapenko | 0fea775 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 86 | static cl::list<std::string> |
| 87 | ClDsymHint("dsym-hint", cl::ZeroOrMore, |
| 88 | cl::desc("Path to .dSYM bundles to search for debug info for the " |
| 89 | "object files")); |
Dmitry Venikov | 7fd3419 | 2019-01-14 10:10:51 +0000 | [diff] [blame] | 90 | |
| 91 | // -print-address, -addresses, -a |
Hemant Kulkarni | e8d28f3 | 2015-10-12 19:26:44 +0000 | [diff] [blame] | 92 | static cl::opt<bool> |
Dmitry Venikov | 7fd3419 | 2019-01-14 10:10:51 +0000 | [diff] [blame] | 93 | ClPrintAddress("print-address", cl::init(false), |
| 94 | cl::desc("Show address before line information")); |
| 95 | static cl::alias |
| 96 | ClPrintAddressAliasAddresses("addresses", cl::desc("Alias for -print-address"), |
| 97 | cl::NotHidden, cl::aliasopt(ClPrintAddress)); |
| 98 | static cl::alias |
| 99 | ClPrintAddressAliasA("a", cl::desc("Alias for -print-address"), |
| 100 | cl::NotHidden, cl::aliasopt(ClPrintAddress)); |
Alexander Potapenko | 0fea775 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 101 | |
Dmitry Venikov | 2b64d3c | 2019-01-10 15:33:35 +0000 | [diff] [blame] | 102 | // -pretty-print, -p |
Hemant Kulkarni | a07496f | 2015-11-11 20:41:43 +0000 | [diff] [blame] | 103 | static cl::opt<bool> |
| 104 | ClPrettyPrint("pretty-print", cl::init(false), |
| 105 | cl::desc("Make the output more human friendly")); |
Dmitry Venikov | 2b64d3c | 2019-01-10 15:33:35 +0000 | [diff] [blame] | 106 | static cl::alias ClPrettyPrintShort("p", cl::desc("Alias for -pretty-print"), |
| 107 | cl::NotHidden, |
| 108 | cl::aliasopt(ClPrettyPrint)); |
Hemant Kulkarni | a07496f | 2015-11-11 20:41:43 +0000 | [diff] [blame] | 109 | |
Mike Aizatsky | d9750d3 | 2016-01-09 00:14:35 +0000 | [diff] [blame] | 110 | static cl::opt<int> ClPrintSourceContextLines( |
| 111 | "print-source-context-lines", cl::init(0), |
| 112 | cl::desc("Print N number of source file context")); |
| 113 | |
David Blaikie | fbad5bd | 2017-01-31 22:19:38 +0000 | [diff] [blame] | 114 | static cl::opt<bool> ClVerbose("verbose", cl::init(false), |
| 115 | cl::desc("Print verbose line info")); |
| 116 | |
James Henderson | 267dc8d | 2019-01-10 14:10:02 +0000 | [diff] [blame] | 117 | static cl::list<std::string> ClInputAddresses(cl::Positional, |
| 118 | cl::desc("<input addresses>..."), |
| 119 | cl::ZeroOrMore); |
| 120 | |
Reid Kleckner | 1ac3f3e | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 121 | template<typename T> |
| 122 | static bool error(Expected<T> &ResOrErr) { |
| 123 | if (ResOrErr) |
Alexey Samsonov | d2c42d4 | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 124 | return false; |
Reid Kleckner | 1ac3f3e | 2016-06-03 20:25:09 +0000 | [diff] [blame] | 125 | logAllUnhandledErrors(ResOrErr.takeError(), errs(), |
| 126 | "LLVMSymbolizer: error reading file: "); |
Alexey Samsonov | d2c42d4 | 2015-11-04 00:30:24 +0000 | [diff] [blame] | 127 | return true; |
| 128 | } |
| 129 | |
Mike Aizatsky | ba07fc7 | 2016-01-07 23:57:41 +0000 | [diff] [blame] | 130 | static bool parseCommand(StringRef InputString, bool &IsData, |
| 131 | std::string &ModuleName, uint64_t &ModuleOffset) { |
Mike Aizatsky | ba07fc7 | 2016-01-07 23:57:41 +0000 | [diff] [blame] | 132 | const char kDelimiters[] = " \n\r"; |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 133 | ModuleName = ""; |
Fangrui Song | 86df7c7 | 2018-05-25 00:11:15 +0000 | [diff] [blame] | 134 | if (InputString.consume_front("CODE ")) { |
Dmitry Vyukov | fc183ce | 2013-01-11 07:16:20 +0000 | [diff] [blame] | 135 | IsData = false; |
Fangrui Song | 86df7c7 | 2018-05-25 00:11:15 +0000 | [diff] [blame] | 136 | } else if (InputString.consume_front("DATA ")) { |
| 137 | IsData = true; |
Dmitry Vyukov | fc183ce | 2013-01-11 07:16:20 +0000 | [diff] [blame] | 138 | } else { |
| 139 | // If no cmd, assume it's CODE. |
| 140 | IsData = false; |
| 141 | } |
Fangrui Song | 86df7c7 | 2018-05-25 00:11:15 +0000 | [diff] [blame] | 142 | const char *pos = InputString.data(); |
Alexey Samsonov | 42c9ecb | 2013-12-24 19:33:22 +0000 | [diff] [blame] | 143 | // Skip delimiters and parse input filename (if needed). |
Fangrui Song | 7789810 | 2018-05-26 02:29:14 +0000 | [diff] [blame] | 144 | if (ClBinaryName.empty()) { |
Alexey Samsonov | 42c9ecb | 2013-12-24 19:33:22 +0000 | [diff] [blame] | 145 | pos += strspn(pos, kDelimiters); |
| 146 | if (*pos == '"' || *pos == '\'') { |
| 147 | char quote = *pos; |
| 148 | pos++; |
Mike Aizatsky | ba07fc7 | 2016-01-07 23:57:41 +0000 | [diff] [blame] | 149 | const char *end = strchr(pos, quote); |
Craig Topper | 573faec | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 150 | if (!end) |
Alexey Samsonov | 42c9ecb | 2013-12-24 19:33:22 +0000 | [diff] [blame] | 151 | return false; |
| 152 | ModuleName = std::string(pos, end - pos); |
| 153 | pos = end + 1; |
| 154 | } else { |
| 155 | int name_length = strcspn(pos, kDelimiters); |
| 156 | ModuleName = std::string(pos, name_length); |
| 157 | pos += name_length; |
| 158 | } |
Alexey Samsonov | ef148af | 2013-04-05 09:22:24 +0000 | [diff] [blame] | 159 | } else { |
Alexey Samsonov | 42c9ecb | 2013-12-24 19:33:22 +0000 | [diff] [blame] | 160 | ModuleName = ClBinaryName; |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 161 | } |
Alexey Samsonov | ef148af | 2013-04-05 09:22:24 +0000 | [diff] [blame] | 162 | // Skip delimiters and parse module offset. |
| 163 | pos += strspn(pos, kDelimiters); |
| 164 | int offset_length = strcspn(pos, kDelimiters); |
Rafael Espindola | ef85994 | 2015-10-24 23:23:25 +0000 | [diff] [blame] | 165 | return !StringRef(pos, offset_length).getAsInteger(0, ModuleOffset); |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 166 | } |
| 167 | |
James Henderson | 267dc8d | 2019-01-10 14:10:02 +0000 | [diff] [blame] | 168 | static void symbolizeInput(StringRef InputString, LLVMSymbolizer &Symbolizer, |
| 169 | DIPrinter &Printer) { |
| 170 | bool IsData = false; |
| 171 | std::string ModuleName; |
| 172 | uint64_t ModuleOffset = 0; |
| 173 | if (!parseCommand(StringRef(InputString), IsData, ModuleName, ModuleOffset)) { |
| 174 | outs() << InputString; |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | if (ClPrintAddress) { |
| 179 | outs() << "0x"; |
| 180 | outs().write_hex(ModuleOffset); |
| 181 | StringRef Delimiter = ClPrettyPrint ? ": " : "\n"; |
| 182 | outs() << Delimiter; |
| 183 | } |
| 184 | if (IsData) { |
| 185 | auto ResOrErr = Symbolizer.symbolizeData(ModuleName, ModuleOffset); |
| 186 | Printer << (error(ResOrErr) ? DIGlobal() : ResOrErr.get()); |
| 187 | } else if (ClPrintInlining) { |
| 188 | auto ResOrErr = |
| 189 | Symbolizer.symbolizeInlinedCode(ModuleName, ModuleOffset, ClDwpName); |
| 190 | Printer << (error(ResOrErr) ? DIInliningInfo() : ResOrErr.get()); |
| 191 | } else { |
| 192 | auto ResOrErr = |
| 193 | Symbolizer.symbolizeCode(ModuleName, ModuleOffset, ClDwpName); |
| 194 | Printer << (error(ResOrErr) ? DILineInfo() : ResOrErr.get()); |
| 195 | } |
| 196 | outs() << "\n"; |
| 197 | outs().flush(); |
| 198 | } |
| 199 | |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 200 | int main(int argc, char **argv) { |
Rui Ueyama | 0b9d56a | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 201 | InitLLVM X(argc, argv); |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 202 | |
Zachary Turner | 7b8e8e5 | 2015-04-27 17:19:51 +0000 | [diff] [blame] | 203 | llvm::sys::InitializeCOMRAII COM(llvm::sys::COMThreadingMode::MultiThreaded); |
| 204 | |
Alexey Samsonov | 42c9ecb | 2013-12-24 19:33:22 +0000 | [diff] [blame] | 205 | cl::ParseCommandLineOptions(argc, argv, "llvm-symbolizer\n"); |
Alexey Samsonov | b977d3a | 2015-10-30 00:40:20 +0000 | [diff] [blame] | 206 | LLVMSymbolizer::Options Opts(ClPrintFunctions, ClUseSymbolTable, ClDemangle, |
Zachary Turner | 43afa42 | 2015-05-06 22:26:30 +0000 | [diff] [blame] | 207 | ClUseRelativeAddress, ClDefaultArch); |
Hemant Kulkarni | a07496f | 2015-11-11 20:41:43 +0000 | [diff] [blame] | 208 | |
Alexander Potapenko | 0fea775 | 2014-10-17 00:50:19 +0000 | [diff] [blame] | 209 | for (const auto &hint : ClDsymHint) { |
| 210 | if (sys::path::extension(hint) == ".dSYM") { |
| 211 | Opts.DsymHints.push_back(hint); |
| 212 | } else { |
| 213 | errs() << "Warning: invalid dSYM hint: \"" << hint << |
| 214 | "\" (must have the '.dSYM' extension).\n"; |
| 215 | } |
| 216 | } |
Alexey Samsonov | c4c7ea3 | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 217 | LLVMSymbolizer Symbolizer(Opts); |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 218 | |
Hemant Kulkarni | a07496f | 2015-11-11 20:41:43 +0000 | [diff] [blame] | 219 | DIPrinter Printer(outs(), ClPrintFunctions != FunctionNameKind::None, |
David Blaikie | fbad5bd | 2017-01-31 22:19:38 +0000 | [diff] [blame] | 220 | ClPrettyPrint, ClPrintSourceContextLines, ClVerbose); |
Alexey Samsonov | 55e87bb | 2015-11-03 22:20:52 +0000 | [diff] [blame] | 221 | |
James Henderson | 267dc8d | 2019-01-10 14:10:02 +0000 | [diff] [blame] | 222 | if (ClInputAddresses.empty()) { |
| 223 | const int kMaxInputStringLength = 1024; |
| 224 | char InputString[kMaxInputStringLength]; |
Mike Aizatsky | ba07fc7 | 2016-01-07 23:57:41 +0000 | [diff] [blame] | 225 | |
James Henderson | 267dc8d | 2019-01-10 14:10:02 +0000 | [diff] [blame] | 226 | while (fgets(InputString, sizeof(InputString), stdin)) |
| 227 | symbolizeInput(InputString, Symbolizer, Printer); |
| 228 | } else { |
| 229 | for (StringRef Address : ClInputAddresses) |
| 230 | symbolizeInput(Address, Symbolizer, Printer); |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 231 | } |
Zachary Turner | 7b8e8e5 | 2015-04-27 17:19:51 +0000 | [diff] [blame] | 232 | |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 233 | return 0; |
| 234 | } |