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 "Opts.inc" 18 #include "llvm/ADT/StringRef.h" 19 #include "llvm/Config/config.h" 20 #include "llvm/DebugInfo/Symbolize/DIPrinter.h" 21 #include "llvm/DebugInfo/Symbolize/Symbolize.h" 22 #include "llvm/Option/Arg.h" 23 #include "llvm/Option/ArgList.h" 24 #include "llvm/Option/Option.h" 25 #include "llvm/Support/COM.h" 26 #include "llvm/Support/CommandLine.h" 27 #include "llvm/Support/Debug.h" 28 #include "llvm/Support/FileSystem.h" 29 #include "llvm/Support/InitLLVM.h" 30 #include "llvm/Support/Path.h" 31 #include "llvm/Support/StringSaver.h" 32 #include "llvm/Support/raw_ostream.h" 33 #include <algorithm> 34 #include <cstdio> 35 #include <cstring> 36 #include <string> 37 38 using namespace llvm; 39 using namespace symbolize; 40 41 namespace { 42 enum ID { 43 OPT_INVALID = 0, // This is not an option ID. 44 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ 45 HELPTEXT, METAVAR, VALUES) \ 46 OPT_##ID, 47 #include "Opts.inc" 48 #undef OPTION 49 }; 50 51 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; 52 #include "Opts.inc" 53 #undef PREFIX 54 55 const opt::OptTable::Info InfoTable[] = { 56 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ 57 HELPTEXT, METAVAR, VALUES) \ 58 { \ 59 PREFIX, NAME, HELPTEXT, \ 60 METAVAR, OPT_##ID, opt::Option::KIND##Class, \ 61 PARAM, FLAGS, OPT_##GROUP, \ 62 OPT_##ALIAS, ALIASARGS, VALUES}, 63 #include "Opts.inc" 64 #undef OPTION 65 }; 66 67 class SymbolizerOptTable : public opt::OptTable { 68 public: 69 SymbolizerOptTable() : OptTable(InfoTable) { 70 setGroupedShortOptions(true); 71 } 72 }; 73 } // namespace 74 75 template <typename T> 76 static void print(const Request &Request, Expected<T> &ResOrErr, 77 DIPrinter &Printer) { 78 if (ResOrErr) { 79 // No error, print the result. 80 Printer.print(Request, *ResOrErr); 81 return; 82 } 83 84 // Handle the error. 85 bool PrintEmpty = true; 86 handleAllErrors(std::move(ResOrErr.takeError()), 87 [&](const ErrorInfoBase &EI) { 88 PrintEmpty = Printer.printError( 89 Request, EI, "LLVMSymbolizer: error reading file: "); 90 }); 91 92 if (PrintEmpty) 93 Printer.print(Request, T()); 94 } 95 96 enum class OutputStyle { LLVM, GNU, JSON }; 97 98 enum class Command { 99 Code, 100 Data, 101 Frame, 102 }; 103 104 static bool parseCommand(StringRef BinaryName, bool IsAddr2Line, 105 StringRef InputString, Command &Cmd, 106 std::string &ModuleName, uint64_t &ModuleOffset) { 107 const char kDelimiters[] = " \n\r"; 108 ModuleName = ""; 109 if (InputString.consume_front("CODE ")) { 110 Cmd = Command::Code; 111 } else if (InputString.consume_front("DATA ")) { 112 Cmd = Command::Data; 113 } else if (InputString.consume_front("FRAME ")) { 114 Cmd = Command::Frame; 115 } else { 116 // If no cmd, assume it's CODE. 117 Cmd = Command::Code; 118 } 119 const char *Pos = InputString.data(); 120 // Skip delimiters and parse input filename (if needed). 121 if (BinaryName.empty()) { 122 Pos += strspn(Pos, kDelimiters); 123 if (*Pos == '"' || *Pos == '\'') { 124 char Quote = *Pos; 125 Pos++; 126 const char *End = strchr(Pos, Quote); 127 if (!End) 128 return false; 129 ModuleName = std::string(Pos, End - Pos); 130 Pos = End + 1; 131 } else { 132 int NameLength = strcspn(Pos, kDelimiters); 133 ModuleName = std::string(Pos, NameLength); 134 Pos += NameLength; 135 } 136 } else { 137 ModuleName = BinaryName.str(); 138 } 139 // Skip delimiters and parse module offset. 140 Pos += strspn(Pos, kDelimiters); 141 int OffsetLength = strcspn(Pos, kDelimiters); 142 StringRef Offset(Pos, OffsetLength); 143 // GNU addr2line assumes the offset is hexadecimal and allows a redundant 144 // "0x" or "0X" prefix; do the same for compatibility. 145 if (IsAddr2Line) 146 Offset.consume_front("0x") || Offset.consume_front("0X"); 147 return !Offset.getAsInteger(IsAddr2Line ? 16 : 0, ModuleOffset); 148 } 149 150 static void symbolizeInput(const opt::InputArgList &Args, uint64_t AdjustVMA, 151 bool IsAddr2Line, OutputStyle Style, 152 StringRef InputString, LLVMSymbolizer &Symbolizer, 153 DIPrinter &Printer) { 154 Command Cmd; 155 std::string ModuleName; 156 uint64_t Offset = 0; 157 if (!parseCommand(Args.getLastArgValue(OPT_obj_EQ), IsAddr2Line, 158 StringRef(InputString), Cmd, ModuleName, Offset)) { 159 Printer.printInvalidCommand({ModuleName, None}, InputString); 160 return; 161 } 162 163 uint64_t AdjustedOffset = Offset - AdjustVMA; 164 if (Cmd == Command::Data) { 165 Expected<DIGlobal> ResOrErr = Symbolizer.symbolizeData( 166 ModuleName, {AdjustedOffset, object::SectionedAddress::UndefSection}); 167 print({ModuleName, Offset}, ResOrErr, Printer); 168 } else if (Cmd == Command::Frame) { 169 Expected<std::vector<DILocal>> ResOrErr = Symbolizer.symbolizeFrame( 170 ModuleName, {AdjustedOffset, object::SectionedAddress::UndefSection}); 171 print({ModuleName, Offset}, ResOrErr, Printer); 172 } else if (Args.hasFlag(OPT_inlines, OPT_no_inlines, !IsAddr2Line)) { 173 Expected<DIInliningInfo> ResOrErr = Symbolizer.symbolizeInlinedCode( 174 ModuleName, {AdjustedOffset, object::SectionedAddress::UndefSection}); 175 print({ModuleName, Offset}, ResOrErr, Printer); 176 } else if (Style == OutputStyle::GNU) { 177 // With PrintFunctions == FunctionNameKind::LinkageName (default) 178 // and UseSymbolTable == true (also default), Symbolizer.symbolizeCode() 179 // may override the name of an inlined function with the name of the topmost 180 // caller function in the inlining chain. This contradicts the existing 181 // behavior of addr2line. Symbolizer.symbolizeInlinedCode() overrides only 182 // the topmost function, which suits our needs better. 183 Expected<DIInliningInfo> ResOrErr = Symbolizer.symbolizeInlinedCode( 184 ModuleName, {AdjustedOffset, object::SectionedAddress::UndefSection}); 185 Expected<DILineInfo> Res0OrErr = 186 !ResOrErr 187 ? Expected<DILineInfo>(ResOrErr.takeError()) 188 : ((ResOrErr->getNumberOfFrames() == 0) ? DILineInfo() 189 : ResOrErr->getFrame(0)); 190 print({ModuleName, Offset}, Res0OrErr, Printer); 191 } else { 192 Expected<DILineInfo> ResOrErr = Symbolizer.symbolizeCode( 193 ModuleName, {AdjustedOffset, object::SectionedAddress::UndefSection}); 194 print({ModuleName, Offset}, ResOrErr, Printer); 195 } 196 } 197 198 static void printHelp(StringRef ToolName, const SymbolizerOptTable &Tbl, 199 raw_ostream &OS) { 200 const char HelpText[] = " [options] addresses..."; 201 Tbl.printHelp(OS, (ToolName + HelpText).str().c_str(), 202 ToolName.str().c_str()); 203 // TODO Replace this with OptTable API once it adds extrahelp support. 204 OS << "\nPass @FILE as argument to read options from FILE.\n"; 205 } 206 207 static opt::InputArgList parseOptions(int Argc, char *Argv[], bool IsAddr2Line, 208 StringSaver &Saver, 209 SymbolizerOptTable &Tbl) { 210 StringRef ToolName = IsAddr2Line ? "llvm-addr2line" : "llvm-symbolizer"; 211 // The environment variable specifies initial options which can be overridden 212 // by commnad line options. 213 Tbl.setInitialOptionsFromEnvironment(IsAddr2Line ? "LLVM_ADDR2LINE_OPTS" 214 : "LLVM_SYMBOLIZER_OPTS"); 215 bool HasError = false; 216 opt::InputArgList Args = 217 Tbl.parseArgs(Argc, Argv, OPT_UNKNOWN, Saver, [&](StringRef Msg) { 218 errs() << ("error: " + Msg + "\n"); 219 HasError = true; 220 }); 221 if (HasError) 222 exit(1); 223 if (Args.hasArg(OPT_help)) { 224 printHelp(ToolName, Tbl, outs()); 225 exit(0); 226 } 227 if (Args.hasArg(OPT_version)) { 228 outs() << ToolName << '\n'; 229 cl::PrintVersionMessage(); 230 exit(0); 231 } 232 233 return Args; 234 } 235 236 template <typename T> 237 static void parseIntArg(const opt::InputArgList &Args, int ID, T &Value) { 238 if (const opt::Arg *A = Args.getLastArg(ID)) { 239 StringRef V(A->getValue()); 240 if (!llvm::to_integer(V, Value, 0)) { 241 errs() << A->getSpelling() + 242 ": expected a non-negative integer, but got '" + V + "'"; 243 exit(1); 244 } 245 } else { 246 Value = 0; 247 } 248 } 249 250 static FunctionNameKind decideHowToPrintFunctions(const opt::InputArgList &Args, 251 bool IsAddr2Line) { 252 if (Args.hasArg(OPT_functions)) 253 return FunctionNameKind::LinkageName; 254 if (const opt::Arg *A = Args.getLastArg(OPT_functions_EQ)) 255 return StringSwitch<FunctionNameKind>(A->getValue()) 256 .Case("none", FunctionNameKind::None) 257 .Case("short", FunctionNameKind::ShortName) 258 .Default(FunctionNameKind::LinkageName); 259 return IsAddr2Line ? FunctionNameKind::None : FunctionNameKind::LinkageName; 260 } 261 262 int main(int argc, char **argv) { 263 InitLLVM X(argc, argv); 264 sys::InitializeCOMRAII COM(sys::COMThreadingMode::MultiThreaded); 265 266 bool IsAddr2Line = sys::path::stem(argv[0]).contains("addr2line"); 267 BumpPtrAllocator A; 268 StringSaver Saver(A); 269 SymbolizerOptTable Tbl; 270 opt::InputArgList Args = parseOptions(argc, argv, IsAddr2Line, Saver, Tbl); 271 272 LLVMSymbolizer::Options Opts; 273 uint64_t AdjustVMA; 274 PrinterConfig Config; 275 parseIntArg(Args, OPT_adjust_vma_EQ, AdjustVMA); 276 if (const opt::Arg *A = Args.getLastArg(OPT_basenames, OPT_relativenames)) { 277 Opts.PathStyle = 278 A->getOption().matches(OPT_basenames) 279 ? DILineInfoSpecifier::FileLineInfoKind::BaseNameOnly 280 : DILineInfoSpecifier::FileLineInfoKind::RelativeFilePath; 281 } else { 282 Opts.PathStyle = DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath; 283 } 284 Opts.DebugFileDirectory = Args.getAllArgValues(OPT_debug_file_directory_EQ); 285 Opts.DefaultArch = Args.getLastArgValue(OPT_default_arch_EQ).str(); 286 Opts.Demangle = Args.hasFlag(OPT_demangle, OPT_no_demangle, !IsAddr2Line); 287 Opts.DWPName = Args.getLastArgValue(OPT_dwp_EQ).str(); 288 Opts.FallbackDebugPath = 289 Args.getLastArgValue(OPT_fallback_debug_path_EQ).str(); 290 Opts.PrintFunctions = decideHowToPrintFunctions(Args, IsAddr2Line); 291 parseIntArg(Args, OPT_print_source_context_lines_EQ, 292 Config.SourceContextLines); 293 Opts.RelativeAddresses = Args.hasArg(OPT_relative_address); 294 Opts.UntagAddresses = 295 Args.hasFlag(OPT_untag_addresses, OPT_no_untag_addresses, !IsAddr2Line); 296 Opts.UseDIA = Args.hasArg(OPT_use_dia); 297 #if !defined(LLVM_ENABLE_DIA_SDK) 298 if (Opts.UseDIA) { 299 WithColor::warning() << "DIA not available; using native PDB reader\n"; 300 Opts.UseDIA = false; 301 } 302 #endif 303 Opts.UseSymbolTable = true; 304 Config.PrintAddress = Args.hasArg(OPT_addresses); 305 Config.PrintFunctions = Opts.PrintFunctions != FunctionNameKind::None; 306 Config.Pretty = Args.hasArg(OPT_pretty_print); 307 Config.Verbose = Args.hasArg(OPT_verbose); 308 309 for (const opt::Arg *A : Args.filtered(OPT_dsym_hint_EQ)) { 310 StringRef Hint(A->getValue()); 311 if (sys::path::extension(Hint) == ".dSYM") { 312 Opts.DsymHints.emplace_back(Hint); 313 } else { 314 errs() << "Warning: invalid dSYM hint: \"" << Hint 315 << "\" (must have the '.dSYM' extension).\n"; 316 } 317 } 318 319 auto Style = IsAddr2Line ? OutputStyle::GNU : OutputStyle::LLVM; 320 if (const opt::Arg *A = Args.getLastArg(OPT_output_style_EQ)) { 321 if (strcmp(A->getValue(), "GNU") == 0) 322 Style = OutputStyle::GNU; 323 else if (strcmp(A->getValue(), "JSON") == 0) 324 Style = OutputStyle::JSON; 325 else 326 Style = OutputStyle::LLVM; 327 } 328 329 LLVMSymbolizer Symbolizer(Opts); 330 std::unique_ptr<DIPrinter> Printer; 331 if (Style == OutputStyle::GNU) 332 Printer = std::make_unique<GNUPrinter>(outs(), errs(), Config); 333 else if (Style == OutputStyle::JSON) 334 Printer = std::make_unique<JSONPrinter>(outs(), Config); 335 else 336 Printer = std::make_unique<LLVMPrinter>(outs(), errs(), Config); 337 338 std::vector<std::string> InputAddresses = Args.getAllArgValues(OPT_INPUT); 339 if (InputAddresses.empty()) { 340 const int kMaxInputStringLength = 1024; 341 char InputString[kMaxInputStringLength]; 342 343 while (fgets(InputString, sizeof(InputString), stdin)) { 344 // Strip newline characters. 345 std::string StrippedInputString(InputString); 346 llvm::erase_if(StrippedInputString, 347 [](char c) { return c == '\r' || c == '\n'; }); 348 symbolizeInput(Args, AdjustVMA, IsAddr2Line, Style, StrippedInputString, 349 Symbolizer, *Printer); 350 outs().flush(); 351 } 352 } else { 353 Printer->listBegin(); 354 for (StringRef Address : InputAddresses) 355 symbolizeInput(Args, AdjustVMA, IsAddr2Line, Style, Address, Symbolizer, 356 *Printer); 357 Printer->listEnd(); 358 } 359 360 return 0; 361 } 362