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 | } |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 73 | // FIXME: Handle case when filename is given in quotes. |
Dmitry Vyukov | fc183ce | 2013-01-11 07:16:20 +0000 | [diff] [blame] | 74 | if (char *FilePath = strtok(pos, kDelimiters)) { |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 75 | ModuleName = FilePath; |
Alexey Samsonov | c4439c3 | 2013-02-15 08:54:47 +0000 | [diff] [blame^] | 76 | if (char *OffsetStr = strtok((char *)0, kDelimiters)) |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 77 | ModuleOffsetStr = OffsetStr; |
| 78 | } |
Alexey Samsonov | c4c7ea3 | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 79 | if (StringRef(ModuleOffsetStr).getAsInteger(0, ModuleOffset)) |
| 80 | return false; |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 81 | return true; |
| 82 | } |
| 83 | |
| 84 | int main(int argc, char **argv) { |
| 85 | // Print stack trace if we signal out. |
| 86 | sys::PrintStackTraceOnErrorSignal(); |
| 87 | PrettyStackTraceProgram X(argc, argv); |
Alexey Samsonov | c4439c3 | 2013-02-15 08:54:47 +0000 | [diff] [blame^] | 88 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 89 | |
| 90 | cl::ParseCommandLineOptions(argc, argv, "llvm symbolizer for compiler-rt\n"); |
Alexey Samsonov | c4c7ea3 | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 91 | LLVMSymbolizer::Options Opts(ClUseSymbolTable, ClPrintFunctions, |
| 92 | ClPrintInlining, ClDemangle); |
| 93 | LLVMSymbolizer Symbolizer(Opts); |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 94 | |
Dmitry Vyukov | fc183ce | 2013-01-11 07:16:20 +0000 | [diff] [blame] | 95 | bool IsData = false; |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 96 | std::string ModuleName; |
Alexey Samsonov | c4c7ea3 | 2013-01-22 14:21:19 +0000 | [diff] [blame] | 97 | uint64_t ModuleOffset; |
| 98 | while (parseCommand(IsData, ModuleName, ModuleOffset)) { |
| 99 | std::string Result = |
| 100 | IsData ? Symbolizer.symbolizeData(ModuleName, ModuleOffset) |
| 101 | : Symbolizer.symbolizeCode(ModuleName, ModuleOffset); |
| 102 | outs() << Result << "\n"; |
| 103 | outs().flush(); |
Alexander Potapenko | f41954b | 2012-11-12 11:33:29 +0000 | [diff] [blame] | 104 | } |
| 105 | return 0; |
| 106 | } |