llvm-symbolizer.cpp revision 249259
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 18#include "LLVMSymbolize.h" 19#include "llvm/ADT/StringRef.h" 20#include "llvm/Support/CommandLine.h" 21#include "llvm/Support/Debug.h" 22#include "llvm/Support/ManagedStatic.h" 23#include "llvm/Support/PrettyStackTrace.h" 24#include "llvm/Support/Signals.h" 25#include "llvm/Support/raw_ostream.h" 26#include <cstdio> 27#include <cstring> 28#include <string> 29 30using namespace llvm; 31using namespace symbolize; 32 33static cl::opt<bool> 34ClUseSymbolTable("use-symbol-table", cl::init(true), 35 cl::desc("Prefer names in symbol table to names " 36 "in debug info")); 37 38static cl::opt<bool> 39ClPrintFunctions("functions", cl::init(true), 40 cl::desc("Print function names as well as line " 41 "information for a given address")); 42 43static cl::opt<bool> 44ClPrintInlining("inlining", cl::init(true), 45 cl::desc("Print all inlined frames for a given address")); 46 47static cl::opt<bool> 48ClDemangle("demangle", cl::init(true), cl::desc("Demangle function names")); 49 50static bool parseCommand(bool &IsData, std::string &ModuleName, 51 uint64_t &ModuleOffset) { 52 const char *kDataCmd = "DATA "; 53 const char *kCodeCmd = "CODE "; 54 const int kMaxInputStringLength = 1024; 55 const char kDelimiters[] = " \n"; 56 char InputString[kMaxInputStringLength]; 57 if (!fgets(InputString, sizeof(InputString), stdin)) 58 return false; 59 IsData = false; 60 ModuleName = ""; 61 std::string ModuleOffsetStr = ""; 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 } 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; 87 } 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); 92 if (StringRef(ModuleOffsetStr).getAsInteger(0, ModuleOffset)) 93 return false; 94 return true; 95} 96 97int main(int argc, char **argv) { 98 // Print stack trace if we signal out. 99 sys::PrintStackTraceOnErrorSignal(); 100 PrettyStackTraceProgram X(argc, argv); 101 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. 102 103 cl::ParseCommandLineOptions(argc, argv, "llvm symbolizer for compiler-rt\n"); 104 LLVMSymbolizer::Options Opts(ClUseSymbolTable, ClPrintFunctions, 105 ClPrintInlining, ClDemangle); 106 LLVMSymbolizer Symbolizer(Opts); 107 108 bool IsData = false; 109 std::string ModuleName; 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(); 117 } 118 return 0; 119} 120