llvm-symbolizer.cpp revision 296417
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 "llvm/ADT/StringRef.h" 19#include "llvm/DebugInfo/Symbolize/DIPrinter.h" 20#include "llvm/DebugInfo/Symbolize/Symbolize.h" 21#include "llvm/Support/COM.h" 22#include "llvm/Support/CommandLine.h" 23#include "llvm/Support/Debug.h" 24#include "llvm/Support/FileSystem.h" 25#include "llvm/Support/ManagedStatic.h" 26#include "llvm/Support/Path.h" 27#include "llvm/Support/PrettyStackTrace.h" 28#include "llvm/Support/Signals.h" 29#include "llvm/Support/raw_ostream.h" 30#include <cstdio> 31#include <cstring> 32#include <string> 33 34using namespace llvm; 35using namespace symbolize; 36 37static cl::opt<bool> 38ClUseSymbolTable("use-symbol-table", cl::init(true), 39 cl::desc("Prefer names in symbol table to names " 40 "in debug info")); 41 42static cl::opt<FunctionNameKind> ClPrintFunctions( 43 "functions", cl::init(FunctionNameKind::LinkageName), 44 cl::desc("Print function name for a given address:"), 45 cl::values(clEnumValN(FunctionNameKind::None, "none", "omit function name"), 46 clEnumValN(FunctionNameKind::ShortName, "short", 47 "print short function name"), 48 clEnumValN(FunctionNameKind::LinkageName, "linkage", 49 "print function linkage name"), 50 clEnumValEnd)); 51 52static cl::opt<bool> 53 ClUseRelativeAddress("relative-address", cl::init(false), 54 cl::desc("Interpret addresses as relative addresses"), 55 cl::ReallyHidden); 56 57static cl::opt<bool> 58 ClPrintInlining("inlining", cl::init(true), 59 cl::desc("Print all inlined frames for a given address")); 60 61static cl::opt<bool> 62ClDemangle("demangle", cl::init(true), cl::desc("Demangle function names")); 63 64static cl::opt<std::string> ClDefaultArch("default-arch", cl::init(""), 65 cl::desc("Default architecture " 66 "(for multi-arch objects)")); 67 68static cl::opt<std::string> 69ClBinaryName("obj", cl::init(""), 70 cl::desc("Path to object file to be symbolized (if not provided, " 71 "object file should be specified for each input line)")); 72 73static cl::list<std::string> 74ClDsymHint("dsym-hint", cl::ZeroOrMore, 75 cl::desc("Path to .dSYM bundles to search for debug info for the " 76 "object files")); 77static cl::opt<bool> 78 ClPrintAddress("print-address", cl::init(false), 79 cl::desc("Show address before line information")); 80 81static cl::opt<bool> 82 ClPrettyPrint("pretty-print", cl::init(false), 83 cl::desc("Make the output more human friendly")); 84 85static cl::opt<int> ClPrintSourceContextLines( 86 "print-source-context-lines", cl::init(0), 87 cl::desc("Print N number of source file context")); 88 89static bool error(std::error_code ec) { 90 if (!ec) 91 return false; 92 errs() << "LLVMSymbolizer: error reading file: " << ec.message() << ".\n"; 93 return true; 94} 95 96static bool parseCommand(StringRef InputString, bool &IsData, 97 std::string &ModuleName, uint64_t &ModuleOffset) { 98 const char *kDataCmd = "DATA "; 99 const char *kCodeCmd = "CODE "; 100 const char kDelimiters[] = " \n\r"; 101 IsData = false; 102 ModuleName = ""; 103 const char *pos = InputString.data(); 104 if (strncmp(pos, kDataCmd, strlen(kDataCmd)) == 0) { 105 IsData = true; 106 pos += strlen(kDataCmd); 107 } else if (strncmp(pos, kCodeCmd, strlen(kCodeCmd)) == 0) { 108 IsData = false; 109 pos += strlen(kCodeCmd); 110 } else { 111 // If no cmd, assume it's CODE. 112 IsData = false; 113 } 114 // Skip delimiters and parse input filename (if needed). 115 if (ClBinaryName == "") { 116 pos += strspn(pos, kDelimiters); 117 if (*pos == '"' || *pos == '\'') { 118 char quote = *pos; 119 pos++; 120 const char *end = strchr(pos, quote); 121 if (!end) 122 return false; 123 ModuleName = std::string(pos, end - pos); 124 pos = end + 1; 125 } else { 126 int name_length = strcspn(pos, kDelimiters); 127 ModuleName = std::string(pos, name_length); 128 pos += name_length; 129 } 130 } else { 131 ModuleName = ClBinaryName; 132 } 133 // Skip delimiters and parse module offset. 134 pos += strspn(pos, kDelimiters); 135 int offset_length = strcspn(pos, kDelimiters); 136 return !StringRef(pos, offset_length).getAsInteger(0, ModuleOffset); 137} 138 139int main(int argc, char **argv) { 140 // Print stack trace if we signal out. 141 sys::PrintStackTraceOnErrorSignal(); 142 PrettyStackTraceProgram X(argc, argv); 143 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. 144 145 llvm::sys::InitializeCOMRAII COM(llvm::sys::COMThreadingMode::MultiThreaded); 146 147 cl::ParseCommandLineOptions(argc, argv, "llvm-symbolizer\n"); 148 LLVMSymbolizer::Options Opts(ClPrintFunctions, ClUseSymbolTable, ClDemangle, 149 ClUseRelativeAddress, ClDefaultArch); 150 151 for (const auto &hint : ClDsymHint) { 152 if (sys::path::extension(hint) == ".dSYM") { 153 Opts.DsymHints.push_back(hint); 154 } else { 155 errs() << "Warning: invalid dSYM hint: \"" << hint << 156 "\" (must have the '.dSYM' extension).\n"; 157 } 158 } 159 LLVMSymbolizer Symbolizer(Opts); 160 161 DIPrinter Printer(outs(), ClPrintFunctions != FunctionNameKind::None, 162 ClPrettyPrint, ClPrintSourceContextLines); 163 164 const int kMaxInputStringLength = 1024; 165 char InputString[kMaxInputStringLength]; 166 167 while (true) { 168 if (!fgets(InputString, sizeof(InputString), stdin)) 169 break; 170 171 bool IsData = false; 172 std::string ModuleName; 173 uint64_t ModuleOffset = 0; 174 if (!parseCommand(StringRef(InputString), IsData, ModuleName, 175 ModuleOffset)) { 176 outs() << InputString; 177 continue; 178 } 179 180 if (ClPrintAddress) { 181 outs() << "0x"; 182 outs().write_hex(ModuleOffset); 183 StringRef Delimiter = (ClPrettyPrint == true) ? ": " : "\n"; 184 outs() << Delimiter; 185 } 186 if (IsData) { 187 auto ResOrErr = Symbolizer.symbolizeData(ModuleName, ModuleOffset); 188 Printer << (error(ResOrErr.getError()) ? DIGlobal() : ResOrErr.get()); 189 } else if (ClPrintInlining) { 190 auto ResOrErr = Symbolizer.symbolizeInlinedCode(ModuleName, ModuleOffset); 191 Printer << (error(ResOrErr.getError()) ? DIInliningInfo() 192 : ResOrErr.get()); 193 } else { 194 auto ResOrErr = Symbolizer.symbolizeCode(ModuleName, ModuleOffset); 195 Printer << (error(ResOrErr.getError()) ? DILineInfo() : ResOrErr.get()); 196 } 197 outs() << "\n"; 198 outs().flush(); 199 } 200 201 return 0; 202} 203