diff --git a/llvm/docs/CommandGuide/llvm-symbolizer.rst b/llvm/docs/CommandGuide/llvm-symbolizer.rst --- a/llvm/docs/CommandGuide/llvm-symbolizer.rst +++ b/llvm/docs/CommandGuide/llvm-symbolizer.rst @@ -14,7 +14,7 @@ :program:`llvm-symbolizer` reads input names and addresses from the command-line and prints corresponding source code locations to standard output. It can also symbolize logs containing :doc:`Symbolizer Markup ` via -:option:`--filter-markup`. +:option:`--filter-markup`. Addresses may be specified as numbers or symbol names. If no address is specified on the command-line, it reads the addresses from standard input. If no input name is specified on the command-line, but addresses @@ -196,6 +196,17 @@ main foo/test.cpp:15:0 +Example 7 - Addresses as symbol names: + +.. code-block:: console + + $ llvm-symbolizer --obj=test.elf main + main + /tmp/test.cpp:14:0 + $ llvm-symbolizer --obj=test.elf "CODE foz" + foz + /tmp/test.h:1:0 + OPTIONS ------- diff --git a/llvm/docs/ReleaseNotes.rst b/llvm/docs/ReleaseNotes.rst --- a/llvm/docs/ReleaseNotes.rst +++ b/llvm/docs/ReleaseNotes.rst @@ -180,6 +180,8 @@ * ``llvm-nm`` now supports the ``--line-numbers`` (``-l``) option to use debugging information to print symbols' filenames and line numbers. +* llvm-symbolizer and llvm-addr2line now support addresses specified as symbol names. + Changes to LLDB --------------------------------- diff --git a/llvm/include/llvm/DebugInfo/Symbolize/DIPrinter.h b/llvm/include/llvm/DebugInfo/Symbolize/DIPrinter.h --- a/llvm/include/llvm/DebugInfo/Symbolize/DIPrinter.h +++ b/llvm/include/llvm/DebugInfo/Symbolize/DIPrinter.h @@ -34,6 +34,7 @@ struct Request { StringRef ModuleName; std::optional Address; + StringRef Symbol; }; class DIPrinter { @@ -46,6 +47,8 @@ virtual void print(const Request &Request, const DIGlobal &Global) = 0; virtual void print(const Request &Request, const std::vector &Locals) = 0; + virtual void print(const Request &Request, + const std::vector &Locations) = 0; virtual bool printError(const Request &Request, const ErrorInfoBase &ErrorInfo) = 0; @@ -91,6 +94,8 @@ void print(const Request &Request, const DIGlobal &Global) override; void print(const Request &Request, const std::vector &Locals) override; + void print(const Request &Request, + const std::vector &Locations) override; bool printError(const Request &Request, const ErrorInfoBase &ErrorInfo) override; @@ -141,6 +146,8 @@ void print(const Request &Request, const DIGlobal &Global) override; void print(const Request &Request, const std::vector &Locals) override; + void print(const Request &Request, + const std::vector &Locations) override; bool printError(const Request &Request, const ErrorInfoBase &ErrorInfo) override; diff --git a/llvm/include/llvm/DebugInfo/Symbolize/SymbolizableModule.h b/llvm/include/llvm/DebugInfo/Symbolize/SymbolizableModule.h --- a/llvm/include/llvm/DebugInfo/Symbolize/SymbolizableModule.h +++ b/llvm/include/llvm/DebugInfo/Symbolize/SymbolizableModule.h @@ -36,6 +36,9 @@ virtual std::vector symbolizeFrame(object::SectionedAddress ModuleOffset) const = 0; + virtual std::vector + findSymbol(StringRef Symbol) const = 0; + // Return true if this is a 32-bit x86 PE COFF module. virtual bool isWin32Module() const = 0; diff --git a/llvm/include/llvm/DebugInfo/Symbolize/SymbolizableObjectFile.h b/llvm/include/llvm/DebugInfo/Symbolize/SymbolizableObjectFile.h --- a/llvm/include/llvm/DebugInfo/Symbolize/SymbolizableObjectFile.h +++ b/llvm/include/llvm/DebugInfo/Symbolize/SymbolizableObjectFile.h @@ -43,6 +43,8 @@ DIGlobal symbolizeData(object::SectionedAddress ModuleOffset) const override; std::vector symbolizeFrame(object::SectionedAddress ModuleOffset) const override; + std::vector + findSymbol(StringRef Symbol) const override; // Return true if this is a 32-bit x86 PE COFF module. bool isWin32Module() const override; diff --git a/llvm/include/llvm/DebugInfo/Symbolize/Symbolize.h b/llvm/include/llvm/DebugInfo/Symbolize/Symbolize.h --- a/llvm/include/llvm/DebugInfo/Symbolize/Symbolize.h +++ b/llvm/include/llvm/DebugInfo/Symbolize/Symbolize.h @@ -104,6 +104,14 @@ Expected> symbolizeFrame(ArrayRef BuildID, object::SectionedAddress ModuleOffset); + + Expected> findSymbol(const ObjectFile &Obj, + StringRef Symbol); + Expected> findSymbol(StringRef ModuleName, + StringRef Symbol); + Expected> findSymbol(ArrayRef BuildID, + StringRef Symbol); + void flush(); // Evict entries from the binary cache until it is under the maximum size @@ -146,6 +154,9 @@ Expected> symbolizeFrameCommon(const T &ModuleSpecifier, object::SectionedAddress ModuleOffset); + template + Expected> findSymbolCommon(const T &ModuleSpecifier, + StringRef Symbol); Expected getOrCreateModuleInfo(const ObjectFile &Obj); diff --git a/llvm/lib/DebugInfo/Symbolize/DIPrinter.cpp b/llvm/lib/DebugInfo/Symbolize/DIPrinter.cpp --- a/llvm/lib/DebugInfo/Symbolize/DIPrinter.cpp +++ b/llvm/lib/DebugInfo/Symbolize/DIPrinter.cpp @@ -260,6 +260,17 @@ printFooter(); } +void PlainPrinterBase::print(const Request &Request, + const std::vector &Locations) { + if (Locations.empty()) { + print(Request, DILineInfo()); + } else { + for (const DILineInfo &L : Locations) + print(L, false); + printFooter(); + } +} + bool PlainPrinterBase::printError(const Request &Request, const ErrorInfoBase &ErrorInfo) { ErrHandler(ErrorInfo, Request.ModuleName); @@ -273,6 +284,8 @@ static json::Object toJSON(const Request &Request, StringRef ErrorMsg = "") { json::Object Json({{"ModuleName", Request.ModuleName.str()}}); + if (!Request.Symbol.empty()) + Json["SymName"] = Request.Symbol.str(); if (Request.Address) Json["Address"] = toHex(*Request.Address); if (!ErrorMsg.empty()) @@ -362,6 +375,19 @@ printJSON(std::move(Json)); } +void JSONPrinter::print(const Request &Request, + const std::vector &Locations) { + json::Array Definitions; + for (const DILineInfo &L : Locations) + Definitions.push_back(toJSON(L)); + json::Object Json = toJSON(Request); + Json["Loc"] = std::move(Definitions); + if (ObjectList) + ObjectList->push_back(std::move(Json)); + else + printJSON(std::move(Json)); +} + bool JSONPrinter::printError(const Request &Request, const ErrorInfoBase &ErrorInfo) { json::Object Json = toJSON(Request, ErrorInfo.message()); diff --git a/llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp b/llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp --- a/llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp +++ b/llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp @@ -351,6 +351,19 @@ return DebugInfoContext->getLocalsForAddress(ModuleOffset); } +std::vector +SymbolizableObjectFile::findSymbol(StringRef Symbol) const { + std::vector Result; + for (const SymbolDesc &Sym : Symbols) { + if (Sym.Name.equals(Symbol)) { + object::SectionedAddress A{Sym.Addr, + getModuleSectionIndexForAddress(Sym.Addr)}; + Result.push_back(A); + } + } + return Result; +} + /// Search for the first occurence of specified Address in ObjectFile. uint64_t SymbolizableObjectFile::getModuleSectionIndexForAddress( uint64_t Address) const { diff --git a/llvm/lib/DebugInfo/Symbolize/Symbolize.cpp b/llvm/lib/DebugInfo/Symbolize/Symbolize.cpp --- a/llvm/lib/DebugInfo/Symbolize/Symbolize.cpp +++ b/llvm/lib/DebugInfo/Symbolize/Symbolize.cpp @@ -231,6 +231,50 @@ return symbolizeFrameCommon(BuildID, ModuleOffset); } +template +Expected> +LLVMSymbolizer::findSymbolCommon(const T &ModuleSpecifier, StringRef Symbol) { + auto InfoOrErr = getOrCreateModuleInfo(ModuleSpecifier); + if (!InfoOrErr) + return InfoOrErr.takeError(); + + SymbolizableModule *Info = *InfoOrErr; + std::vector Result; + + // A null module means an error has already been reported. Return an empty + // result. + if (!Info) + return Result; + + for (object::SectionedAddress A : Info->findSymbol(Symbol)) { + DILineInfo LineInfo = Info->symbolizeCode( + A, DILineInfoSpecifier(Opts.PathStyle, Opts.PrintFunctions), + Opts.UseSymbolTable); + if (LineInfo.FileName != DILineInfo::BadString) { + if (Opts.Demangle) + LineInfo.FunctionName = DemangleName(LineInfo.FunctionName, Info); + Result.push_back(LineInfo); + } + } + + return Result; +} + +Expected> +LLVMSymbolizer::findSymbol(const ObjectFile &Obj, StringRef Symbol) { + return findSymbolCommon(Obj, Symbol); +} + +Expected> +LLVMSymbolizer::findSymbol(StringRef ModuleName, StringRef Symbol) { + return findSymbolCommon(ModuleName.str(), Symbol); +} + +Expected> +LLVMSymbolizer::findSymbol(ArrayRef BuildID, StringRef Symbol) { + return findSymbolCommon(BuildID, Symbol); +} + void LLVMSymbolizer::flush() { ObjectForUBPathAndArch.clear(); LRUBinaries.clear(); diff --git a/llvm/test/tools/llvm-symbolizer/Inputs/addr.inp b/llvm/test/tools/llvm-symbolizer/Inputs/addr.inp --- a/llvm/test/tools/llvm-symbolizer/Inputs/addr.inp +++ b/llvm/test/tools/llvm-symbolizer/Inputs/addr.inp @@ -1,3 +1,3 @@ -some text +something not a valid address 0x40054d -some text2 +some text possibly a symbol diff --git a/llvm/test/tools/llvm-symbolizer/Inputs/discrim.inp b/llvm/test/tools/llvm-symbolizer/Inputs/discrim.inp --- a/llvm/test/tools/llvm-symbolizer/Inputs/discrim.inp +++ b/llvm/test/tools/llvm-symbolizer/Inputs/discrim.inp @@ -5,4 +5,4 @@ 0x4005b9 0x4005ce 0x4005d4 -some more text +another text diff --git a/llvm/test/tools/llvm-symbolizer/Inputs/symbols.h b/llvm/test/tools/llvm-symbolizer/Inputs/symbols.h new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-symbolizer/Inputs/symbols.h @@ -0,0 +1,19 @@ +// This file is a part of sources used to build `symbols.so`, which is used to +// test symbol location search made by llvm-symbolizer. +// +// Build instructions: +// $ mkdir /tmp/dbginfo +// $ cp symbols.h symbols.part1.cpp symbols.part2.cpp symbols.part3.c symbols.part4.c /tmp/dbginfo/ +// $ cd /tmp/dbginfo +// $ gcc -osymbols.so -shared -fPIC -g symbols.part1.cpp symbols.part2.cpp symbols.part3.c symbols.part4.c + + +extern "C" { +extern int global_01; +int func_01(); +int func_02(int); +} + +template T func_03(T x) { + return x + T(1); +} diff --git a/llvm/test/tools/llvm-symbolizer/Inputs/symbols.part1.cpp b/llvm/test/tools/llvm-symbolizer/Inputs/symbols.part1.cpp new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-symbolizer/Inputs/symbols.part1.cpp @@ -0,0 +1,25 @@ +#include "symbols.h" + +int global_01 = 22; + +int static static_var = 0; + +static int static_func_01(int x) { + static_var = x; + return global_01; +} + +int func_01() { + int res = 1; + return res + static_func_01(22); +} + +int func_04() { + static_var = 0; + return 22; +} + +int func_04(int x) { + int res = static_var; + return res + func_03(x); +} diff --git a/llvm/test/tools/llvm-symbolizer/Inputs/symbols.part2.cpp b/llvm/test/tools/llvm-symbolizer/Inputs/symbols.part2.cpp new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-symbolizer/Inputs/symbols.part2.cpp @@ -0,0 +1,18 @@ +#include "symbols.h" + +int static static_var = 4; + +static int static_func_01(int x) { + static_var--; + return x; +} + +int func_02(int x) { + static_var = x; + return static_func_01(x); +} + +int func_05(int x) { + int res = static_var; + return res + func_03(x); +} diff --git a/llvm/test/tools/llvm-symbolizer/Inputs/symbols.part3.c b/llvm/test/tools/llvm-symbolizer/Inputs/symbols.part3.c new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-symbolizer/Inputs/symbols.part3.c @@ -0,0 +1,12 @@ +static int static_func(int); +static int static_var = 0; + +int static_func(int x) { + static_var++; + return static_var + x; +} + +int func_06(int x) { + return static_func(x); +} + diff --git a/llvm/test/tools/llvm-symbolizer/Inputs/symbols.part4.c b/llvm/test/tools/llvm-symbolizer/Inputs/symbols.part4.c new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-symbolizer/Inputs/symbols.part4.c @@ -0,0 +1,13 @@ +static int static_func(int); +static int static_var = 5; + +int static_func(int x) { + static_var++; + return static_var + x; +} + +int func_07(int x) { + static_var++; + return static_func(x); +} + diff --git a/llvm/test/tools/llvm-symbolizer/Inputs/symbols.so b/llvm/test/tools/llvm-symbolizer/Inputs/symbols.so new file mode 100755 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 GIT binary patch literal 0 Hc$@ %t.1.stderr | FileCheck --check-prefix=NONEXISTENT %s +RUN: FileCheck --input-file=%t.1.stderr --check-prefix=BINARY-NOT-FOUND -DMSG=%errc_ENOENT %s +RUN: llvm-symbolizer "%p/Inputs/666.so func_01" 2> %t.2.stderr | FileCheck --check-prefix=NONEXISTENT %s +RUN: FileCheck --input-file=%t.2.stderr --check-prefix=BINARY-NOT-FOUND -DMSG=%errc_ENOENT %s +BINARY-NOT-FOUND: error: '{{.*}}666.so': [[MSG]] diff --git a/llvm/tools/llvm-symbolizer/llvm-symbolizer.cpp b/llvm/tools/llvm-symbolizer/llvm-symbolizer.cpp --- a/llvm/tools/llvm-symbolizer/llvm-symbolizer.cpp +++ b/llvm/tools/llvm-symbolizer/llvm-symbolizer.cpp @@ -159,7 +159,7 @@ static Error parseCommand(StringRef BinaryName, bool IsAddr2Line, StringRef InputString, Command &Cmd, std::string &ModuleName, object::BuildID &BuildID, - uint64_t &ModuleOffset) { + StringRef &Symbol, uint64_t &ModuleOffset) { ModuleName = BinaryName; if (InputString.consume_front("CODE ")) { Cmd = Command::Code; @@ -224,35 +224,41 @@ return makeStringError("no input filename has been specified"); } - // Parse module offset. + // Parse module offset, which can be specified as a number or as a symbol. InputString = InputString.ltrim(); if (InputString.empty()) return makeStringError("no module offset has been specified"); + + // If input string contains a space, ignore everything after it. This behavior + // is consistent with GNU addr2line. int OffsetLength = InputString.find_first_of(" \n\r"); StringRef Offset = InputString.substr(0, OffsetLength); + // GNU addr2line assumes the offset is hexadecimal and allows a redundant // "0x" or "0X" prefix; do the same for compatibility. if (IsAddr2Line) Offset.consume_front("0x") || Offset.consume_front("0X"); - // If the input is not a valid module offset, it is not an error, but its - // lookup does not make sense. Return error of different kind to distinguish - // from error or success. - if (Offset.getAsInteger(IsAddr2Line ? 16 : 0, ModuleOffset)) - return errorCodeToError(errc::invalid_argument); + // If the input is not a number, treat it is a symbol. + if (Offset.getAsInteger(IsAddr2Line ? 16 : 0, ModuleOffset)) { + Symbol = Offset; + ModuleOffset = 0; + } return Error::success(); } template void executeCommand(StringRef ModuleName, const T &ModuleSpec, Command Cmd, - uint64_t Offset, uint64_t AdjustVMA, bool ShouldInline, - OutputStyle Style, LLVMSymbolizer &Symbolizer, - DIPrinter &Printer) { + StringRef Symbol, uint64_t Offset, uint64_t AdjustVMA, + bool ShouldInline, OutputStyle Style, + LLVMSymbolizer &Symbolizer, DIPrinter &Printer) { uint64_t AdjustedOffset = Offset - AdjustVMA; object::SectionedAddress Address = {AdjustedOffset, object::SectionedAddress::UndefSection}; - Request SymRequest = {ModuleName, Offset}; + Request SymRequest = { + ModuleName, Symbol.empty() ? std::make_optional(Offset) : std::nullopt, + Symbol}; if (Cmd == Command::Data) { Expected ResOrErr = Symbolizer.symbolizeData(ModuleSpec, Address); print(SymRequest, ResOrErr, Printer); @@ -260,6 +266,10 @@ Expected> ResOrErr = Symbolizer.symbolizeFrame(ModuleSpec, Address); print(SymRequest, ResOrErr, Printer); + } else if (!Symbol.empty()) { + Expected> ResOrErr = + Symbolizer.findSymbol(ModuleSpec, Symbol); + print(SymRequest, ResOrErr, Printer); } else if (ShouldInline) { Expected ResOrErr = Symbolizer.symbolizeInlinedCode(ModuleSpec, Address); @@ -288,7 +298,7 @@ } static void printUnknownLineInfo(std::string ModuleName, DIPrinter &Printer) { - Request SymRequest = {ModuleName, std::nullopt}; + Request SymRequest = {ModuleName, std::nullopt, StringRef()}; Printer.print(SymRequest, DILineInfo()); } @@ -301,16 +311,14 @@ std::string ModuleName; object::BuildID BuildID(IncomingBuildID.begin(), IncomingBuildID.end()); uint64_t Offset = 0; + StringRef Symbol; if (Error E = parseCommand(Args.getLastArgValue(OPT_obj_EQ), IsAddr2Line, StringRef(InputString), Cmd, ModuleName, BuildID, - Offset)) { - handleAllErrors( - std::move(E), - [&](const StringError &EI) { - printError(EI, InputString); - printUnknownLineInfo(ModuleName, Printer); - }, - [&](const ECError &EI) { printUnknownLineInfo(ModuleName, Printer); }); + Symbol, Offset)) { + handleAllErrors(std::move(E), [&](const StringError &EI) { + printError(EI, InputString); + printUnknownLineInfo(ModuleName, Printer); + }); return; } bool ShouldInline = Args.hasFlag(OPT_inlines, OPT_no_inlines, !IsAddr2Line); @@ -319,11 +327,11 @@ if (!Args.hasArg(OPT_no_debuginfod)) enableDebuginfod(Symbolizer, Args); std::string BuildIDStr = toHex(BuildID); - executeCommand(BuildIDStr, BuildID, Cmd, Offset, AdjustVMA, ShouldInline, - Style, Symbolizer, Printer); + executeCommand(BuildIDStr, BuildID, Cmd, Symbol, Offset, AdjustVMA, + ShouldInline, Style, Symbolizer, Printer); } else { - executeCommand(ModuleName, ModuleName, Cmd, Offset, AdjustVMA, ShouldInline, - Style, Symbolizer, Printer); + executeCommand(ModuleName, ModuleName, Cmd, Symbol, Offset, AdjustVMA, + ShouldInline, Style, Symbolizer, Printer); } } @@ -527,7 +535,7 @@ if (auto *Arg = Args.getLastArg(OPT_obj_EQ); Arg) { auto Status = Symbolizer.getOrCreateModuleInfo(Arg->getValue()); if (!Status) { - Request SymRequest = {Arg->getValue(), 0}; + Request SymRequest = {Arg->getValue(), 0, StringRef()}; handleAllErrors(Status.takeError(), [&](const ErrorInfoBase &EI) { Printer->printError(SymRequest, EI); }); diff --git a/llvm/unittests/ProfileData/MemProfTest.cpp b/llvm/unittests/ProfileData/MemProfTest.cpp --- a/llvm/unittests/ProfileData/MemProfTest.cpp +++ b/llvm/unittests/ProfileData/MemProfTest.cpp @@ -20,6 +20,7 @@ using ::llvm::DILineInfo; using ::llvm::DILineInfoSpecifier; using ::llvm::DILocal; +using ::llvm::StringRef; using ::llvm::memprof::CallStackMap; using ::llvm::memprof::Frame; using ::llvm::memprof::FrameId; @@ -53,6 +54,9 @@ virtual std::vector symbolizeFrame(SectionedAddress) const { llvm_unreachable("unused"); } + virtual std::vector findSymbol(StringRef Symbol) const { + llvm_unreachable("unused"); + } virtual bool isWin32Module() const { llvm_unreachable("unused"); } virtual uint64_t getModulePreferredBase() const { llvm_unreachable("unused");