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 | |
Alexey Samsonov | c4c7ea3 | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 18 | #include "LLVMSymbolize.h" |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringRef.h" |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 20 | #include "llvm/Support/CommandLine.h" |
| 21 | #include "llvm/Support/Debug.h" |
| 22 | #include "llvm/Support/ManagedStatic.h" |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 23 | #include "llvm/Support/PrettyStackTrace.h" |
| 24 | #include "llvm/Support/Signals.h" |
| 25 | #include "llvm/Support/raw_ostream.h" |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 26 | #include <cstdio> |
| 27 | #include <cstring> |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 28 | #include <string> |
| 29 | |
| 30 | using namespace llvm; |
Alexey Samsonov | c4c7ea3 | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 31 | using namespace symbolize; |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 32 | |
| 33 | static cl::opt<bool> |
Alexey Samsonov | c4c7ea3 | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 34 | ClUseSymbolTable("use-symbol-table", cl::init(true), |
Alexey Samsonov | c4439c3 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 35 | cl::desc("Prefer names in symbol table to names " |
| 36 | "in debug info")); |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 37 | |
| 38 | static cl::opt<bool> |
Alexey Samsonov | c4c7ea3 | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 39 | ClPrintFunctions("functions", cl::init(true), |
Alexey Samsonov | c4439c3 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 40 | cl::desc("Print function names as well as line " |
| 41 | "information for a given address")); |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 42 | |
| 43 | static cl::opt<bool> |
Alexey Samsonov | c4c7ea3 | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 44 | ClPrintInlining("inlining", cl::init(true), |
Alexey Samsonov | c4439c3 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 45 | cl::desc("Print all inlined frames for a given address")); |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 46 | |
| 47 | static cl::opt<bool> |
Alexey Samsonov | c4439c3 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 48 | ClDemangle("demangle", cl::init(true), cl::desc("Demangle function names")); |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 49 | |
Alexey Samsonov | c4439c3 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 50 | static bool parseCommand(bool &IsData, std::string &ModuleName, |
Alexey Samsonov | c4c7ea3 | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 51 | uint64_t &ModuleOffset) { |
Dmitry Vyukov | fc183ce | 2013-01-11 07:16:20 +0000 | [diff] [blame] | 52 | const char *kDataCmd = "DATA "; |
| 53 | const char *kCodeCmd = "CODE "; |
| 54 | const int kMaxInputStringLength = 1024; |
| 55 | const char kDelimiters[] = " \n"; |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 56 | char InputString[kMaxInputStringLength]; |
| 57 | if (!fgets(InputString, sizeof(InputString), stdin)) |
| 58 | return false; |
Dmitry Vyukov | fc183ce | 2013-01-11 07:16:20 +0000 | [diff] [blame] | 59 | IsData = false; |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 60 | ModuleName = ""; |
Alexey Samsonov | c4c7ea3 | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 61 | std::string ModuleOffsetStr = ""; |
Dmitry Vyukov | fc183ce | 2013-01-11 07:16:20 +0000 | [diff] [blame] | 62 | char *pos = InputString; |
| 63 | if (strncmp(pos, kDataCmd, strlen(kDataCmd)) == 0) { |
| 64 | IsData = true; |
| 65 | pos += strlen(kDataCmd); |
| 66 | } else if (strncmp(pos, kCodeCmd, strlen(kCodeCmd)) == 0) { |
| 67 | IsData = false; |
| 68 | pos += strlen(kCodeCmd); |
| 69 | } else { |
| 70 | // If no cmd, assume it's CODE. |
| 71 | IsData = false; |
| 72 | } |
Alexey Samsonov | ef148af | 2013-04-05 09:22:24 +0000 | [diff] [blame^] | 73 | // Skip delimiters and parse input filename. |
| 74 | pos += strspn(pos, kDelimiters); |
| 75 | if (*pos == '"' || *pos == '\'') { |
| 76 | char quote = *pos; |
| 77 | pos++; |
| 78 | char *end = strchr(pos, quote); |
| 79 | if (end == 0) |
| 80 | return false; |
| 81 | ModuleName = std::string(pos, end - pos); |
| 82 | pos = end + 1; |
| 83 | } else { |
| 84 | int name_length = strcspn(pos, kDelimiters); |
| 85 | ModuleName = std::string(pos, name_length); |
| 86 | pos += name_length; |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 87 | } |
Alexey Samsonov | ef148af | 2013-04-05 09:22:24 +0000 | [diff] [blame^] | 88 | // Skip delimiters and parse module offset. |
| 89 | pos += strspn(pos, kDelimiters); |
| 90 | int offset_length = strcspn(pos, kDelimiters); |
| 91 | ModuleOffsetStr = std::string(pos, offset_length); |
Alexey Samsonov | c4c7ea3 | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 92 | if (StringRef(ModuleOffsetStr).getAsInteger(0, ModuleOffset)) |
| 93 | return false; |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 94 | return true; |
| 95 | } |
| 96 | |
| 97 | int main(int argc, char **argv) { |
| 98 | // Print stack trace if we signal out. |
| 99 | sys::PrintStackTraceOnErrorSignal(); |
| 100 | PrettyStackTraceProgram X(argc, argv); |
Alexey Samsonov | c4439c3 | 2013-02-15 08:54:47 +0000 | [diff] [blame] | 101 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 102 | |
| 103 | cl::ParseCommandLineOptions(argc, argv, "llvm symbolizer for compiler-rt\n"); |
Alexey Samsonov | c4c7ea3 | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 104 | LLVMSymbolizer::Options Opts(ClUseSymbolTable, ClPrintFunctions, |
| 105 | ClPrintInlining, ClDemangle); |
| 106 | LLVMSymbolizer Symbolizer(Opts); |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 107 | |
Dmitry Vyukov | fc183ce | 2013-01-11 07:16:20 +0000 | [diff] [blame] | 108 | bool IsData = false; |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 109 | std::string ModuleName; |
Alexey Samsonov | c4c7ea3 | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 110 | uint64_t ModuleOffset; |
| 111 | while (parseCommand(IsData, ModuleName, ModuleOffset)) { |
| 112 | std::string Result = |
| 113 | IsData ? Symbolizer.symbolizeData(ModuleName, ModuleOffset) |
| 114 | : Symbolizer.symbolizeCode(ModuleName, ModuleOffset); |
| 115 | outs() << Result << "\n"; |
| 116 | outs().flush(); |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 117 | } |
| 118 | return 0; |
| 119 | } |