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 30 using namespace llvm; 31 using namespace symbolize; 32 33 static cl::opt<bool> 34 ClUseSymbolTable("use-symbol-table", cl::init(true), 35 cl::desc("Prefer names in symbol table to names " 36 "in debug info")); 37 38 static cl::opt<FunctionNameKind> ClPrintFunctions( 39 "functions", cl::init(FunctionNameKind::LinkageName), 40 cl::desc("Print function name for a given address:"), 41 cl::values(clEnumValN(FunctionNameKind::None, "none", "omit function name"), 42 clEnumValN(FunctionNameKind::ShortName, "short", 43 "print short function name"), 44 clEnumValN(FunctionNameKind::LinkageName, "linkage", 45 "print function linkage name"), 46 clEnumValEnd)); 47 48 static cl::opt<bool> 49 ClPrintInlining("inlining", cl::init(true), 50 cl::desc("Print all inlined frames for a given address")); 51 52 static cl::opt<bool> 53 ClDemangle("demangle", cl::init(true), cl::desc("Demangle function names")); 54 55 static cl::opt<std::string> ClDefaultArch("default-arch", cl::init(""), 56 cl::desc("Default architecture " 57 "(for multi-arch objects)")); 58 59 static cl::opt<std::string> 60 ClBinaryName("obj", cl::init(""), 61 cl::desc("Path to object file to be symbolized (if not provided, " 62 "object file should be specified for each input line)")); 63 64 static bool parseCommand(bool &IsData, std::string &ModuleName, 65 uint64_t &ModuleOffset) { 66 const char *kDataCmd = "DATA "; 67 const char *kCodeCmd = "CODE "; 68 const int kMaxInputStringLength = 1024; 69 const char kDelimiters[] = " \n"; 70 char InputString[kMaxInputStringLength]; 71 if (!fgets(InputString, sizeof(InputString), stdin)) 72 return false; 73 IsData = false; 74 ModuleName = ""; 75 char *pos = InputString; 76 if (strncmp(pos, kDataCmd, strlen(kDataCmd)) == 0) { 77 IsData = true; 78 pos += strlen(kDataCmd); 79 } else if (strncmp(pos, kCodeCmd, strlen(kCodeCmd)) == 0) { 80 IsData = false; 81 pos += strlen(kCodeCmd); 82 } else { 83 // If no cmd, assume it's CODE. 84 IsData = false; 85 } 86 // Skip delimiters and parse input filename (if needed). 87 if (ClBinaryName == "") { 88 pos += strspn(pos, kDelimiters); 89 if (*pos == '"' || *pos == '\'') { 90 char quote = *pos; 91 pos++; 92 char *end = strchr(pos, quote); 93 if (!end) 94 return false; 95 ModuleName = std::string(pos, end - pos); 96 pos = end + 1; 97 } else { 98 int name_length = strcspn(pos, kDelimiters); 99 ModuleName = std::string(pos, name_length); 100 pos += name_length; 101 } 102 } else { 103 ModuleName = ClBinaryName; 104 } 105 // Skip delimiters and parse module offset. 106 pos += strspn(pos, kDelimiters); 107 int offset_length = strcspn(pos, kDelimiters); 108 if (StringRef(pos, offset_length).getAsInteger(0, ModuleOffset)) 109 return false; 110 return true; 111 } 112 113 int main(int argc, char **argv) { 114 // Print stack trace if we signal out. 115 sys::PrintStackTraceOnErrorSignal(); 116 PrettyStackTraceProgram X(argc, argv); 117 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. 118 119 cl::ParseCommandLineOptions(argc, argv, "llvm-symbolizer\n"); 120 LLVMSymbolizer::Options Opts(ClUseSymbolTable, ClPrintFunctions, 121 ClPrintInlining, ClDemangle, ClDefaultArch); 122 LLVMSymbolizer Symbolizer(Opts); 123 124 bool IsData = false; 125 std::string ModuleName; 126 uint64_t ModuleOffset; 127 while (parseCommand(IsData, ModuleName, ModuleOffset)) { 128 std::string Result = 129 IsData ? Symbolizer.symbolizeData(ModuleName, ModuleOffset) 130 : Symbolizer.symbolizeCode(ModuleName, ModuleOffset); 131 outs() << Result << "\n"; 132 outs().flush(); 133 } 134 return 0; 135 } 136