1 //===-- llvm-symbolizer.cpp - Simple addr2line-like symbolizer ------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This utility works much like "addr2line". It is able of transforming 10 // tuples (module name, module offset) to code locations (function name, 11 // file, line number, column number). It is targeted for compiler-rt tools 12 // (especially AddressSanitizer and ThreadSanitizer) that can use it 13 // to symbolize stack traces in their error reports. 14 // 15 //===----------------------------------------------------------------------===// 16 17 #include "llvm/ADT/StringRef.h" 18 #include "llvm/DebugInfo/Symbolize/DIPrinter.h" 19 #include "llvm/DebugInfo/Symbolize/Symbolize.h" 20 #include "llvm/Support/COM.h" 21 #include "llvm/Support/CommandLine.h" 22 #include "llvm/Support/Debug.h" 23 #include "llvm/Support/FileSystem.h" 24 #include "llvm/Support/InitLLVM.h" 25 #include "llvm/Support/Path.h" 26 #include "llvm/Support/raw_ostream.h" 27 #include <cstdio> 28 #include <cstring> 29 #include <string> 30 31 using namespace llvm; 32 using namespace symbolize; 33 34 static cl::opt<bool> 35 ClUseSymbolTable("use-symbol-table", cl::init(true), 36 cl::desc("Prefer names in symbol table to names " 37 "in debug info")); 38 39 static cl::opt<FunctionNameKind> ClPrintFunctions( 40 "functions", cl::init(FunctionNameKind::LinkageName), 41 cl::desc("Print function name for a given address"), cl::ValueOptional, 42 cl::values(clEnumValN(FunctionNameKind::None, "none", "omit function name"), 43 clEnumValN(FunctionNameKind::ShortName, "short", 44 "print short function name"), 45 clEnumValN(FunctionNameKind::LinkageName, "linkage", 46 "print function linkage name (default)"), 47 // Sentinel value for unspecified value. 48 clEnumValN(FunctionNameKind::LinkageName, "", ""))); 49 static cl::alias ClPrintFunctionsShort("f", cl::desc("Alias for -functions"), 50 cl::NotHidden, cl::Grouping, 51 cl::aliasopt(ClPrintFunctions)); 52 53 static cl::opt<bool> 54 ClUseRelativeAddress("relative-address", cl::init(false), 55 cl::desc("Interpret addresses as relative addresses"), 56 cl::ReallyHidden); 57 58 static cl::opt<bool> 59 ClPrintInlining("inlining", cl::init(true), 60 cl::desc("Print all inlined frames for a given address")); 61 static cl::alias 62 ClPrintInliningAliasI("i", cl::desc("Alias for -inlining"), 63 cl::NotHidden, cl::aliasopt(ClPrintInlining), 64 cl::Grouping); 65 static cl::alias 66 ClPrintInliningAliasInlines("inlines", cl::desc("Alias for -inlining"), 67 cl::NotHidden, cl::aliasopt(ClPrintInlining)); 68 69 // -basenames, -s 70 static cl::opt<bool> ClBasenames("basenames", cl::init(false), 71 cl::desc("Strip directory names from paths")); 72 static cl::alias ClBasenamesShort("s", cl::desc("Alias for -basenames"), 73 cl::NotHidden, cl::aliasopt(ClBasenames)); 74 75 // -demangle, -C, -no-demangle 76 static cl::opt<bool> 77 ClDemangle("demangle", cl::init(true), cl::desc("Demangle function names")); 78 static cl::alias 79 ClDemangleShort("C", cl::desc("Alias for -demangle"), 80 cl::NotHidden, cl::aliasopt(ClDemangle), cl::Grouping); 81 static cl::opt<bool> 82 ClNoDemangle("no-demangle", cl::init(false), 83 cl::desc("Don't demangle function names")); 84 85 static cl::opt<std::string> ClDefaultArch("default-arch", cl::init(""), 86 cl::desc("Default architecture " 87 "(for multi-arch objects)")); 88 89 // -obj, -exe, -e 90 static cl::opt<std::string> 91 ClBinaryName("obj", cl::init(""), 92 cl::desc("Path to object file to be symbolized (if not provided, " 93 "object file should be specified for each input line)")); 94 static cl::alias 95 ClBinaryNameAliasExe("exe", cl::desc("Alias for -obj"), 96 cl::NotHidden, cl::aliasopt(ClBinaryName)); 97 static cl::alias 98 ClBinaryNameAliasE("e", cl::desc("Alias for -obj"), 99 cl::NotHidden, cl::aliasopt(ClBinaryName)); 100 101 102 static cl::opt<std::string> 103 ClDwpName("dwp", cl::init(""), 104 cl::desc("Path to DWP file to be use for any split CUs")); 105 106 static cl::list<std::string> 107 ClDsymHint("dsym-hint", cl::ZeroOrMore, 108 cl::desc("Path to .dSYM bundles to search for debug info for the " 109 "object files")); 110 111 // -print-address, -addresses, -a 112 static cl::opt<bool> 113 ClPrintAddress("print-address", cl::init(false), 114 cl::desc("Show address before line information")); 115 static cl::alias 116 ClPrintAddressAliasAddresses("addresses", cl::desc("Alias for -print-address"), 117 cl::NotHidden, cl::aliasopt(ClPrintAddress)); 118 static cl::alias 119 ClPrintAddressAliasA("a", cl::desc("Alias for -print-address"), 120 cl::NotHidden, cl::aliasopt(ClPrintAddress), cl::Grouping); 121 122 // -pretty-print, -p 123 static cl::opt<bool> 124 ClPrettyPrint("pretty-print", cl::init(false), 125 cl::desc("Make the output more human friendly")); 126 static cl::alias ClPrettyPrintShort("p", cl::desc("Alias for -pretty-print"), 127 cl::NotHidden, 128 cl::aliasopt(ClPrettyPrint), cl::Grouping); 129 130 static cl::opt<int> ClPrintSourceContextLines( 131 "print-source-context-lines", cl::init(0), 132 cl::desc("Print N number of source file context")); 133 134 static cl::opt<bool> ClVerbose("verbose", cl::init(false), 135 cl::desc("Print verbose line info")); 136 137 // -adjust-vma 138 static cl::opt<unsigned long long> 139 ClAdjustVMA("adjust-vma", cl::init(0), cl::value_desc("offset"), 140 cl::desc("Add specified offset to object file addresses")); 141 142 static cl::list<std::string> ClInputAddresses(cl::Positional, 143 cl::desc("<input addresses>..."), 144 cl::ZeroOrMore); 145 146 static cl::opt<std::string> 147 ClFallbackDebugPath("fallback-debug-path", cl::init(""), 148 cl::desc("Fallback path for debug binaries.")); 149 150 template<typename T> 151 static bool error(Expected<T> &ResOrErr) { 152 if (ResOrErr) 153 return false; 154 logAllUnhandledErrors(ResOrErr.takeError(), errs(), 155 "LLVMSymbolizer: error reading file: "); 156 return true; 157 } 158 159 static bool parseCommand(StringRef InputString, bool &IsData, 160 std::string &ModuleName, uint64_t &ModuleOffset) { 161 const char kDelimiters[] = " \n\r"; 162 ModuleName = ""; 163 if (InputString.consume_front("CODE ")) { 164 IsData = false; 165 } else if (InputString.consume_front("DATA ")) { 166 IsData = true; 167 } else { 168 // If no cmd, assume it's CODE. 169 IsData = false; 170 } 171 const char *pos = InputString.data(); 172 // Skip delimiters and parse input filename (if needed). 173 if (ClBinaryName.empty()) { 174 pos += strspn(pos, kDelimiters); 175 if (*pos == '"' || *pos == '\'') { 176 char quote = *pos; 177 pos++; 178 const char *end = strchr(pos, quote); 179 if (!end) 180 return false; 181 ModuleName = std::string(pos, end - pos); 182 pos = end + 1; 183 } else { 184 int name_length = strcspn(pos, kDelimiters); 185 ModuleName = std::string(pos, name_length); 186 pos += name_length; 187 } 188 } else { 189 ModuleName = ClBinaryName; 190 } 191 // Skip delimiters and parse module offset. 192 pos += strspn(pos, kDelimiters); 193 int offset_length = strcspn(pos, kDelimiters); 194 return !StringRef(pos, offset_length).getAsInteger(0, ModuleOffset); 195 } 196 197 static void symbolizeInput(StringRef InputString, LLVMSymbolizer &Symbolizer, 198 DIPrinter &Printer) { 199 bool IsData = false; 200 std::string ModuleName; 201 uint64_t Offset = 0; 202 if (!parseCommand(StringRef(InputString), IsData, ModuleName, Offset)) { 203 outs() << InputString; 204 return; 205 } 206 207 if (ClPrintAddress) { 208 outs() << "0x"; 209 outs().write_hex(Offset); 210 StringRef Delimiter = ClPrettyPrint ? ": " : "\n"; 211 outs() << Delimiter; 212 } 213 Offset -= ClAdjustVMA; 214 if (IsData) { 215 auto ResOrErr = Symbolizer.symbolizeData( 216 ModuleName, {Offset, object::SectionedAddress::UndefSection}); 217 Printer << (error(ResOrErr) ? DIGlobal() : ResOrErr.get()); 218 } else if (ClPrintInlining) { 219 auto ResOrErr = Symbolizer.symbolizeInlinedCode( 220 ModuleName, {Offset, object::SectionedAddress::UndefSection}, 221 ClDwpName); 222 Printer << (error(ResOrErr) ? DIInliningInfo() : ResOrErr.get()); 223 } else { 224 auto ResOrErr = Symbolizer.symbolizeCode( 225 ModuleName, {Offset, object::SectionedAddress::UndefSection}, 226 ClDwpName); 227 Printer << (error(ResOrErr) ? DILineInfo() : ResOrErr.get()); 228 } 229 outs() << "\n"; 230 outs().flush(); 231 } 232 233 int main(int argc, char **argv) { 234 InitLLVM X(argc, argv); 235 236 llvm::sys::InitializeCOMRAII COM(llvm::sys::COMThreadingMode::MultiThreaded); 237 cl::ParseCommandLineOptions(argc, argv, "llvm-symbolizer\n"); 238 239 // If both --demangle and --no-demangle are specified then pick the last one. 240 if (ClNoDemangle.getPosition() > ClDemangle.getPosition()) 241 ClDemangle = !ClNoDemangle; 242 243 LLVMSymbolizer::Options Opts(ClPrintFunctions, ClUseSymbolTable, ClDemangle, 244 ClUseRelativeAddress, ClDefaultArch, 245 ClFallbackDebugPath); 246 247 for (const auto &hint : ClDsymHint) { 248 if (sys::path::extension(hint) == ".dSYM") { 249 Opts.DsymHints.push_back(hint); 250 } else { 251 errs() << "Warning: invalid dSYM hint: \"" << hint << 252 "\" (must have the '.dSYM' extension).\n"; 253 } 254 } 255 LLVMSymbolizer Symbolizer(Opts); 256 257 DIPrinter Printer(outs(), ClPrintFunctions != FunctionNameKind::None, 258 ClPrettyPrint, ClPrintSourceContextLines, ClVerbose, 259 ClBasenames); 260 261 if (ClInputAddresses.empty()) { 262 const int kMaxInputStringLength = 1024; 263 char InputString[kMaxInputStringLength]; 264 265 while (fgets(InputString, sizeof(InputString), stdin)) 266 symbolizeInput(InputString, Symbolizer, Printer); 267 } else { 268 for (StringRef Address : ClInputAddresses) 269 symbolizeInput(Address, Symbolizer, Printer); 270 } 271 272 return 0; 273 } 274