[llvm] r236653 - A few fixes for llvm-symbolizer on Windows.
Zachary Turner
zturner at google.com
Wed May 6 15:26:31 PDT 2015
Author: zturner
Date: Wed May 6 17:26:30 2015
New Revision: 236653
URL: http://llvm.org/viewvc/llvm-project?rev=236653&view=rev
Log:
A few fixes for llvm-symbolizer on Windows.
Specifically, this patch correctly respects the -demangle option,
and additionally adds a hidden --relative-address option allows
input addresses to be relative to the module load address instead
of absolute addresses into the image.
Added:
llvm/trunk/test/tools/llvm-symbolizer/pdb/Inputs/test.cpp
Removed:
llvm/trunk/test/tools/llvm-symbolizer/pdb/Inputs/test.c
Modified:
llvm/trunk/include/llvm/DebugInfo/PDB/PDBContext.h
llvm/trunk/lib/DebugInfo/PDB/PDBContext.cpp
llvm/trunk/test/tools/llvm-symbolizer/pdb/Inputs/test.exe
llvm/trunk/test/tools/llvm-symbolizer/pdb/Inputs/test.exe.input
llvm/trunk/test/tools/llvm-symbolizer/pdb/Inputs/test.pdb
llvm/trunk/test/tools/llvm-symbolizer/pdb/pdb.test
llvm/trunk/tools/llvm-symbolizer/LLVMSymbolize.cpp
llvm/trunk/tools/llvm-symbolizer/LLVMSymbolize.h
llvm/trunk/tools/llvm-symbolizer/llvm-symbolizer.cpp
Modified: llvm/trunk/include/llvm/DebugInfo/PDB/PDBContext.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/PDB/PDBContext.h?rev=236653&r1=236652&r2=236653&view=diff
==============================================================================
--- llvm/trunk/include/llvm/DebugInfo/PDB/PDBContext.h (original)
+++ llvm/trunk/include/llvm/DebugInfo/PDB/PDBContext.h Wed May 6 17:26:30 2015
@@ -32,7 +32,8 @@ class PDBContext : public DIContext {
public:
PDBContext(const object::COFFObjectFile &Object,
- std::unique_ptr<IPDBSession> PDBSession);
+ std::unique_ptr<IPDBSession> PDBSession,
+ bool RelativeAddress);
static bool classof(const DIContext *DICtx) {
return DICtx->getKind() == CK_PDB;
@@ -51,6 +52,7 @@ public:
DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
private:
+ std::string getFunctionName(uint64_t Address, DINameKind NameKind) const;
std::unique_ptr<IPDBSession> Session;
};
}
Modified: llvm/trunk/lib/DebugInfo/PDB/PDBContext.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/PDB/PDBContext.cpp?rev=236653&r1=236652&r2=236653&view=diff
==============================================================================
--- llvm/trunk/lib/DebugInfo/PDB/PDBContext.cpp (original)
+++ llvm/trunk/lib/DebugInfo/PDB/PDBContext.cpp Wed May 6 17:26:30 2015
@@ -14,43 +14,44 @@
#include "llvm/DebugInfo/PDB/PDBSymbol.h"
#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
+#include "llvm/DebugInfo/PDB/PDBSymbolPublicSymbol.h"
#include "llvm/Object/COFF.h"
using namespace llvm;
using namespace llvm::object;
PDBContext::PDBContext(const COFFObjectFile &Object,
- std::unique_ptr<IPDBSession> PDBSession)
+ std::unique_ptr<IPDBSession> PDBSession,
+ bool RelativeAddress)
: DIContext(CK_PDB), Session(std::move(PDBSession)) {
- uint64_t ImageBase = 0;
- if (Object.is64()) {
- const pe32plus_header *Header = nullptr;
- Object.getPE32PlusHeader(Header);
- if (Header)
- ImageBase = Header->ImageBase;
- } else {
- const pe32_header *Header = nullptr;
- Object.getPE32Header(Header);
- if (Header)
- ImageBase = static_cast<uint64_t>(Header->ImageBase);
+ if (!RelativeAddress) {
+ uint64_t ImageBase = 0;
+ if (Object.is64()) {
+ const pe32plus_header *Header = nullptr;
+ Object.getPE32PlusHeader(Header);
+ if (Header)
+ ImageBase = Header->ImageBase;
+ } else {
+ const pe32_header *Header = nullptr;
+ Object.getPE32Header(Header);
+ if (Header)
+ ImageBase = static_cast<uint64_t>(Header->ImageBase);
+ }
+ Session->setLoadAddress(ImageBase);
}
- Session->setLoadAddress(ImageBase);
}
void PDBContext::dump(raw_ostream &OS, DIDumpType DumpType) {}
DILineInfo PDBContext::getLineInfoForAddress(uint64_t Address,
DILineInfoSpecifier Specifier) {
- auto Symbol = Session->findSymbolByAddress(Address, PDB_SymType::None);
+ DILineInfo Result;
+ Result.FunctionName = getFunctionName(Address, Specifier.FNKind);
uint32_t Length = 1;
- DILineInfo Result;
+ std::unique_ptr<PDBSymbol> Symbol =
+ Session->findSymbolByAddress(Address, PDB_SymType::None);
if (auto Func = dyn_cast_or_null<PDBSymbolFunc>(Symbol.get())) {
- if (Specifier.FNKind == DINameKind::LinkageName)
- Result.FunctionName = Func->getUndecoratedName();
- else if (Specifier.FNKind == DINameKind::ShortName)
- Result.FunctionName = Func->getName();
-
Length = Func->getLength();
} else if (auto Data = dyn_cast_or_null<PDBSymbolData>(Symbol.get())) {
Length = Data->getLength();
@@ -101,3 +102,32 @@ PDBContext::getInliningInfoForAddress(ui
InlineInfo.addFrame(Frame);
return InlineInfo;
}
+
+std::string PDBContext::getFunctionName(uint64_t Address,
+ DINameKind NameKind) const {
+ if (NameKind == DINameKind::None)
+ return std::string();
+
+ if (NameKind == DINameKind::LinkageName) {
+ // It is not possible to get the mangled linkage name through a
+ // PDBSymbolFunc. For that we have to specifically request a
+ // PDBSymbolPublicSymbol.
+ auto PublicSym =
+ Session->findSymbolByAddress(Address, PDB_SymType::PublicSymbol);
+ if (auto PS = dyn_cast_or_null<PDBSymbolPublicSymbol>(PublicSym.get()))
+ return PS->getName();
+ }
+
+ auto FuncSymbol =
+ Session->findSymbolByAddress(Address, PDB_SymType::Function);
+
+ // This could happen either if there was no public symbol (e.g. not
+ // external) or the user requested the short name. In the former case,
+ // although they technically requested the linkage name, if the linkage
+ // name is not available we fallback to at least returning a non-empty
+ // string.
+ if (auto Func = dyn_cast_or_null<PDBSymbolFunc>(FuncSymbol.get()))
+ return Func->getName();
+
+ return std::string();
+}
Removed: llvm/trunk/test/tools/llvm-symbolizer/pdb/Inputs/test.c
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-symbolizer/pdb/Inputs/test.c?rev=236652&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-symbolizer/pdb/Inputs/test.c (original)
+++ llvm/trunk/test/tools/llvm-symbolizer/pdb/Inputs/test.c (removed)
@@ -1,8 +0,0 @@
-// To generate the corresponding EXE/PDB, run:
-// cl /Zi test.c
-void foo() {
-}
-
-int main() {
- foo();
-}
Added: llvm/trunk/test/tools/llvm-symbolizer/pdb/Inputs/test.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-symbolizer/pdb/Inputs/test.cpp?rev=236653&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-symbolizer/pdb/Inputs/test.cpp (added)
+++ llvm/trunk/test/tools/llvm-symbolizer/pdb/Inputs/test.cpp Wed May 6 17:26:30 2015
@@ -0,0 +1,18 @@
+// To generate the corresponding EXE/PDB, run:
+// cl /Zi test.cpp
+
+namespace NS {
+struct Foo {
+ void bar() {}
+};
+}
+
+void foo() {
+}
+
+int main() {
+ foo();
+
+ NS::Foo f;
+ f.bar();
+}
Modified: llvm/trunk/test/tools/llvm-symbolizer/pdb/Inputs/test.exe
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-symbolizer/pdb/Inputs/test.exe?rev=236653&r1=236652&r2=236653&view=diff
==============================================================================
Binary files llvm/trunk/test/tools/llvm-symbolizer/pdb/Inputs/test.exe (original) and llvm/trunk/test/tools/llvm-symbolizer/pdb/Inputs/test.exe Wed May 6 17:26:30 2015 differ
Modified: llvm/trunk/test/tools/llvm-symbolizer/pdb/Inputs/test.exe.input
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-symbolizer/pdb/Inputs/test.exe.input?rev=236653&r1=236652&r2=236653&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-symbolizer/pdb/Inputs/test.exe.input (original)
+++ llvm/trunk/test/tools/llvm-symbolizer/pdb/Inputs/test.exe.input Wed May 6 17:26:30 2015
@@ -1,3 +1,4 @@
-0x401020
0x401030
+0x401040
+0x401060
0x500000
Modified: llvm/trunk/test/tools/llvm-symbolizer/pdb/Inputs/test.pdb
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-symbolizer/pdb/Inputs/test.pdb?rev=236653&r1=236652&r2=236653&view=diff
==============================================================================
Binary files llvm/trunk/test/tools/llvm-symbolizer/pdb/Inputs/test.pdb (original) and llvm/trunk/test/tools/llvm-symbolizer/pdb/Inputs/test.pdb Wed May 6 17:26:30 2015 differ
Modified: llvm/trunk/test/tools/llvm-symbolizer/pdb/pdb.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-symbolizer/pdb/pdb.test?rev=236653&r1=236652&r2=236653&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-symbolizer/pdb/pdb.test (original)
+++ llvm/trunk/test/tools/llvm-symbolizer/pdb/pdb.test Wed May 6 17:26:30 2015
@@ -1,8 +1,18 @@
-RUN: llvm-symbolizer -obj="%p/Inputs/test.exe" < "%p/Inputs/test.exe.input" | FileCheck %s --check-prefix=CHECK
+RUN: llvm-symbolizer -obj="%p/Inputs/test.exe" < "%p/Inputs/test.exe.input" | \
+RUN: FileCheck %s --check-prefix=CHECK
+RUN: llvm-symbolizer -obj="%p/Inputs/test.exe" -demangle=false < \
+RUN: "%p/Inputs/test.exe.input" | FileCheck %s --check-prefix=CHECK-NO-DEMANGLE
-CHECK: _foo
-CHECK-NEXT: test.c:3:0
+CHECK: foo(void)
+CHECK-NEXT: test.cpp:10
CHECK: _main
-CHECK-NEXT: test.c:6:0
-CHECK: ??
-CHECK-NEXT: ??:0:0
+CHECK-NEXT: test.cpp:13:0
+CHECK: NS::Foo::bar(void)
+CHECK-NEXT: test.cpp:6:0
+
+CHECK-NO-DEMANGLE: foo
+CHECK-NO-DEMANGLE-NEXT: test.cpp:10
+CHECK-NO-DEMANGLE: _main
+CHECK-LINKAGE-NAME-NEXT: test.cpp:13:0
+CHECK-NO-DEMANGLE: bar
+CHECK-LINKAGE-NAME-NEXT: test.cpp:6:0
Modified: llvm/trunk/tools/llvm-symbolizer/LLVMSymbolize.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-symbolizer/LLVMSymbolize.cpp?rev=236653&r1=236652&r2=236653&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-symbolizer/LLVMSymbolize.cpp (original)
+++ llvm/trunk/tools/llvm-symbolizer/LLVMSymbolize.cpp Wed May 6 17:26:30 2015
@@ -29,6 +29,11 @@
#include <sstream>
#include <stdlib.h>
+#if defined(_MSC_VER)
+#include <Windows.h>
+#include <DbgHelp.h>
+#endif
+
namespace llvm {
namespace symbolize {
@@ -471,8 +476,10 @@ LLVMSymbolizer::getOrCreateModuleInfo(co
std::unique_ptr<IPDBSession> Session;
PDB_ErrorCode Error = loadDataForEXE(PDB_ReaderType::DIA,
Objects.first->getFileName(), Session);
- if (Error == PDB_ErrorCode::Success)
- Context = new PDBContext(*CoffObject, std::move(Session));
+ if (Error == PDB_ErrorCode::Success) {
+ Context = new PDBContext(*CoffObject, std::move(Session),
+ Opts.RelativeAddresses);
+ }
}
if (!Context)
Context = new DWARFContextInMemory(*Objects.second);
@@ -522,7 +529,17 @@ std::string LLVMSymbolizer::DemangleName
free(DemangledName);
return Result;
#else
- return Name;
+ char DemangledName[1024] = {0};
+ DWORD result = ::UnDecorateSymbolName(
+ Name.c_str(), DemangledName, 1023,
+ UNDNAME_NO_ACCESS_SPECIFIERS | // Strip public, private, protected
+ UNDNAME_NO_ALLOCATION_LANGUAGE | // Strip __thiscall, __stdcall, etc
+ UNDNAME_NO_THROW_SIGNATURES | // Strip throw() specifications
+ UNDNAME_NO_MEMBER_TYPE | // Strip virtual, static, etc specifiers
+ UNDNAME_NO_MS_KEYWORDS | // Strip all MS extension keywords
+ UNDNAME_NO_FUNCTION_RETURNS); // Strip function return types
+
+ return (result == 0) ? Name : std::string(DemangledName);
#endif
}
Modified: llvm/trunk/tools/llvm-symbolizer/LLVMSymbolize.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-symbolizer/LLVMSymbolize.h?rev=236653&r1=236652&r2=236653&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-symbolizer/LLVMSymbolize.h (original)
+++ llvm/trunk/tools/llvm-symbolizer/LLVMSymbolize.h Wed May 6 17:26:30 2015
@@ -35,19 +35,20 @@ class ModuleInfo;
class LLVMSymbolizer {
public:
struct Options {
- bool UseSymbolTable : 1;
FunctionNameKind PrintFunctions;
+ bool UseSymbolTable : 1;
bool PrintInlining : 1;
bool Demangle : 1;
+ bool RelativeAddresses : 1;
std::string DefaultArch;
std::vector<std::string> DsymHints;
- Options(bool UseSymbolTable = true,
- FunctionNameKind PrintFunctions = FunctionNameKind::LinkageName,
- bool PrintInlining = true, bool Demangle = true,
+ Options(FunctionNameKind PrintFunctions = FunctionNameKind::LinkageName,
+ bool UseSymbolTable = true, bool PrintInlining = true,
+ bool Demangle = true, bool RelativeAddresses = false,
std::string DefaultArch = "")
- : UseSymbolTable(UseSymbolTable),
- PrintFunctions(PrintFunctions), PrintInlining(PrintInlining),
- Demangle(Demangle), DefaultArch(DefaultArch) {}
+ : PrintFunctions(PrintFunctions), UseSymbolTable(UseSymbolTable),
+ PrintInlining(PrintInlining), Demangle(Demangle),
+ RelativeAddresses(RelativeAddresses), DefaultArch(DefaultArch) {}
};
LLVMSymbolizer(const Options &Opts = Options()) : Opts(Opts) {}
Modified: llvm/trunk/tools/llvm-symbolizer/llvm-symbolizer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-symbolizer/llvm-symbolizer.cpp?rev=236653&r1=236652&r2=236653&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-symbolizer/llvm-symbolizer.cpp (original)
+++ llvm/trunk/tools/llvm-symbolizer/llvm-symbolizer.cpp Wed May 6 17:26:30 2015
@@ -49,8 +49,13 @@ static cl::opt<FunctionNameKind> ClPrint
clEnumValEnd));
static cl::opt<bool>
-ClPrintInlining("inlining", cl::init(true),
- cl::desc("Print all inlined frames for a given address"));
+ ClUseRelativeAddress("relative-address", cl::init(false),
+ cl::desc("Interpret addresses as relative addresses"),
+ cl::ReallyHidden);
+
+static cl::opt<bool>
+ ClPrintInlining("inlining", cl::init(true),
+ cl::desc("Print all inlined frames for a given address"));
static cl::opt<bool>
ClDemangle("demangle", cl::init(true), cl::desc("Demangle function names"));
@@ -127,8 +132,9 @@ int main(int argc, char **argv) {
llvm::sys::InitializeCOMRAII COM(llvm::sys::COMThreadingMode::MultiThreaded);
cl::ParseCommandLineOptions(argc, argv, "llvm-symbolizer\n");
- LLVMSymbolizer::Options Opts(ClUseSymbolTable, ClPrintFunctions,
- ClPrintInlining, ClDemangle, ClDefaultArch);
+ LLVMSymbolizer::Options Opts(ClPrintFunctions, ClUseSymbolTable,
+ ClPrintInlining, ClDemangle,
+ ClUseRelativeAddress, ClDefaultArch);
for (const auto &hint : ClDsymHint) {
if (sys::path::extension(hint) == ".dSYM") {
Opts.DsymHints.push_back(hint);
More information about the llvm-commits
mailing list