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