Make llvm-symbolizer work on Windows.
authorZachary Turner <zturner@google.com>
Mon, 27 Apr 2015 17:19:51 +0000 (17:19 +0000)
committerZachary Turner <zturner@google.com>
Mon, 27 Apr 2015 17:19:51 +0000 (17:19 +0000)
Differential Revision: http://reviews.llvm.org/D9234
Reviewed By: Alexey Samsonov

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@235900 91177308-0d34-0410-b5e6-96231b3b80d8

12 files changed:
include/llvm/DebugInfo/PDB/PDBContext.h [new file with mode: 0644]
lib/DebugInfo/PDB/CMakeLists.txt
lib/DebugInfo/PDB/PDBContext.cpp [new file with mode: 0644]
test/tools/llvm-symbolizer/pdb/Inputs/test.c [new file with mode: 0644]
test/tools/llvm-symbolizer/pdb/Inputs/test.exe [new file with mode: 0644]
test/tools/llvm-symbolizer/pdb/Inputs/test.exe.input [new file with mode: 0644]
test/tools/llvm-symbolizer/pdb/Inputs/test.pdb [new file with mode: 0644]
test/tools/llvm-symbolizer/pdb/lit.local.cfg [new file with mode: 0644]
test/tools/llvm-symbolizer/pdb/pdb.test [new file with mode: 0644]
tools/llvm-symbolizer/CMakeLists.txt
tools/llvm-symbolizer/LLVMSymbolize.cpp
tools/llvm-symbolizer/llvm-symbolizer.cpp

diff --git a/include/llvm/DebugInfo/PDB/PDBContext.h b/include/llvm/DebugInfo/PDB/PDBContext.h
new file mode 100644 (file)
index 0000000..88a11c1
--- /dev/null
@@ -0,0 +1,58 @@
+//===-- PDBContext.h --------------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===/
+
+#ifndef LLVM_DEBUGINFO_PDB_PDBCONTEXT_H
+#define LLVM_DEBUGINFO_PDB_PDBCONTEXT_H
+
+#include "llvm/DebugInfo/DIContext.h"
+#include "llvm/DebugInfo/PDB/IPDBSession.h"
+
+namespace llvm {
+
+namespace object {
+class COFFObjectFile;
+}
+
+/// PDBContext
+/// This data structure is the top level entity that deals with PDB debug
+/// information parsing.  This data structure exists only when there is a
+/// need for a transparent interface to different debug information formats
+/// (e.g. PDB and DWARF).  More control and power over the debug information
+/// access can be had by using the PDB interfaces directly.
+class PDBContext : public DIContext {
+
+  PDBContext(PDBContext &) = delete;
+  PDBContext &operator=(PDBContext &) = delete;
+
+public:
+  PDBContext(const object::COFFObjectFile &Object,
+             std::unique_ptr<IPDBSession> PDBSession);
+
+  static bool classof(const DIContext *DICtx) {
+    return DICtx->getKind() == CK_PDB;
+  }
+
+  void dump(raw_ostream &OS, DIDumpType DumpType = DIDT_All) override;
+
+  DILineInfo getLineInfoForAddress(
+      uint64_t Address,
+      DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
+  DILineInfoTable getLineInfoForAddressRange(
+      uint64_t Address, uint64_t Size,
+      DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
+  DIInliningInfo getInliningInfoForAddress(
+      uint64_t Address,
+      DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
+
+private:
+  std::unique_ptr<IPDBSession> Session;
+};
+}
+
+#endif
index ed8c67411d968af9573ca5e410c6e6894e883832..68d3402c560343febd3b05322a19ef902a9a1a01 100644 (file)
@@ -32,6 +32,7 @@ list(APPEND LIBPDB_ADDITIONAL_HEADER_DIRS "${LLVM_MAIN_INCLUDE_DIR}/llvm/DebugIn
 add_llvm_library(LLVMDebugInfoPDB
   IPDBSourceFile.cpp
   PDB.cpp
+  PDBContext.cpp
   PDBExtras.cpp
   PDBInterfaceAnchors.cpp
   PDBSymbol.cpp
diff --git a/lib/DebugInfo/PDB/PDBContext.cpp b/lib/DebugInfo/PDB/PDBContext.cpp
new file mode 100644 (file)
index 0000000..a260b0e
--- /dev/null
@@ -0,0 +1,103 @@
+//===-- PDBContext.cpp ------------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===/
+
+#include "llvm/DebugInfo/PDB/PDBContext.h"
+#include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
+#include "llvm/DebugInfo/PDB/IPDBLineNumber.h"
+#include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
+#include "llvm/DebugInfo/PDB/PDBSymbol.h"
+#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
+#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
+#include "llvm/Object/COFF.h"
+
+using namespace llvm;
+using namespace llvm::object;
+
+PDBContext::PDBContext(const COFFObjectFile &Object,
+                       std::unique_ptr<IPDBSession> PDBSession)
+    : 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);
+  }
+  Session->setLoadAddress(ImageBase);
+}
+
+void PDBContext::dump(raw_ostream &OS, DIDumpType DumpType) {}
+
+DILineInfo PDBContext::getLineInfoForAddress(uint64_t Address,
+                                             DILineInfoSpecifier Specifier) {
+  auto Symbol = Session->findSymbolByAddress(Address);
+
+  uint32_t Length = 1;
+  DILineInfo Result;
+  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();
+  }
+
+  // If we couldn't find a symbol, then just assume 1 byte, so that we get
+  // only the line number of the first instruction.
+  auto LineNumbers = Session->findLineNumbersByAddress(Address, Length);
+  if (!LineNumbers || LineNumbers->getChildCount() == 0)
+    return Result;
+
+  auto LineInfo = LineNumbers->getNext();
+  assert(LineInfo);
+  auto SourceFile = Session->getSourceFileById(LineInfo->getSourceFileId());
+
+  if (SourceFile &&
+      Specifier.FLIKind != DILineInfoSpecifier::FileLineInfoKind::None)
+    Result.FileName = SourceFile->getFileName();
+  Result.Column = LineInfo->getColumnNumber();
+  Result.Line = LineInfo->getLineNumber();
+  return Result;
+}
+
+DILineInfoTable
+PDBContext::getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
+                                       DILineInfoSpecifier Specifier) {
+  if (Size == 0)
+    return DILineInfoTable();
+
+  DILineInfoTable Table;
+  auto LineNumbers = Session->findLineNumbersByAddress(Address, Size);
+  if (!LineNumbers || LineNumbers->getChildCount() == 0)
+    return Table;
+
+  while (auto LineInfo = LineNumbers->getNext()) {
+    DILineInfo LineEntry =
+        getLineInfoForAddress(LineInfo->getVirtualAddress(), Specifier);
+    Table.push_back(std::make_pair(LineInfo->getVirtualAddress(), LineEntry));
+  }
+  return Table;
+}
+
+DIInliningInfo
+PDBContext::getInliningInfoForAddress(uint64_t Address,
+                                      DILineInfoSpecifier Specifier) {
+  DIInliningInfo InlineInfo;
+  DILineInfo Frame = getLineInfoForAddress(Address, Specifier);
+  InlineInfo.addFrame(Frame);
+  return InlineInfo;
+}
diff --git a/test/tools/llvm-symbolizer/pdb/Inputs/test.c b/test/tools/llvm-symbolizer/pdb/Inputs/test.c
new file mode 100644 (file)
index 0000000..8dd0d38
--- /dev/null
@@ -0,0 +1,8 @@
+// To generate the corresponding EXE/PDB, run:
+// cl /Zi test.c
+void foo() {
+}
+
+int main() {
+  foo();
+}
diff --git a/test/tools/llvm-symbolizer/pdb/Inputs/test.exe b/test/tools/llvm-symbolizer/pdb/Inputs/test.exe
new file mode 100644 (file)
index 0000000..556b78c
Binary files /dev/null and b/test/tools/llvm-symbolizer/pdb/Inputs/test.exe differ
diff --git a/test/tools/llvm-symbolizer/pdb/Inputs/test.exe.input b/test/tools/llvm-symbolizer/pdb/Inputs/test.exe.input
new file mode 100644 (file)
index 0000000..26c9bea
--- /dev/null
@@ -0,0 +1,3 @@
+0x401020
+0x401030
+0x500000
diff --git a/test/tools/llvm-symbolizer/pdb/Inputs/test.pdb b/test/tools/llvm-symbolizer/pdb/Inputs/test.pdb
new file mode 100644 (file)
index 0000000..c539a35
Binary files /dev/null and b/test/tools/llvm-symbolizer/pdb/Inputs/test.pdb differ
diff --git a/test/tools/llvm-symbolizer/pdb/lit.local.cfg b/test/tools/llvm-symbolizer/pdb/lit.local.cfg
new file mode 100644 (file)
index 0000000..8c58f16
--- /dev/null
@@ -0,0 +1 @@
+config.unsupported = not config.have_dia_sdk\r
diff --git a/test/tools/llvm-symbolizer/pdb/pdb.test b/test/tools/llvm-symbolizer/pdb/pdb.test
new file mode 100644 (file)
index 0000000..a8d7439
--- /dev/null
@@ -0,0 +1,8 @@
+RUN: llvm-symbolizer -obj="%p/Inputs/test.exe" < "%p/Inputs/test.exe.input" | FileCheck %s --check-prefix=CHECK\r
+\r
+CHECK: _foo\r
+CHECK-NEXT: test.c:3:0\r
+CHECK: _main\r
+CHECK-NEXT: test.c:6:0\r
+CHECK: ??\r
+CHECK-NEXT: ??:0:0\r
index 6968f1c6216284325fcdcb5b51b6035730c20712..5df3b17a065e74044a34e7fd207145e45657bbe2 100644 (file)
@@ -5,6 +5,7 @@
 
 set(LLVM_LINK_COMPONENTS
   DebugInfoDWARF
+  DebugInfoPDB
   Object
   Support
   )
index c3988e136177ff5cd42cf47159cce2d766e1f9d6..326cab5a186041d827caf24eeddb7c5f7a0b93e9 100644 (file)
@@ -15,6 +15,8 @@
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/Config/config.h"
 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
+#include "llvm/DebugInfo/PDB/PDB.h"
+#include "llvm/DebugInfo/PDB/PDBContext.h"
 #include "llvm/Object/ELFObjectFile.h"
 #include "llvm/Object/MachO.h"
 #include "llvm/Support/Casting.h"
@@ -164,6 +166,7 @@ DILineInfo ModuleInfo::symbolizeCode(
 DIInliningInfo ModuleInfo::symbolizeInlinedCode(
     uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const {
   DIInliningInfo InlinedContext;
+
   if (DebugInfoContext) {
     InlinedContext = DebugInfoContext->getInliningInfoForAddress(
         ModuleOffset, getDILineInfoSpecifier(Opts));
@@ -461,7 +464,18 @@ LLVMSymbolizer::getOrCreateModuleInfo(const std::string &ModuleName) {
     Modules.insert(make_pair(ModuleName, (ModuleInfo *)nullptr));
     return nullptr;
   }
-  DIContext *Context = new DWARFContextInMemory(*Objects.second);
+  DIContext *Context = nullptr;
+  if (auto CoffObject = dyn_cast<COFFObjectFile>(Objects.first)) {
+    // If this is a COFF object, assume it contains PDB debug information.  If
+    // we don't find any we will fall back to the DWARF case.
+    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 (!Context)
+    Context = new DWARFContextInMemory(*Objects.second);
   assert(Context);
   ModuleInfo *Info = new ModuleInfo(Objects.first, Context);
   Modules.insert(make_pair(ModuleName, Info));
index cb40472f9d2fd128e22b21d496be0a678bc11bdc..7a77a4476e3b8a2c9f1ae9dcd6c1b200320e85ad 100644 (file)
@@ -17,6 +17,7 @@
 
 #include "LLVMSymbolize.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/Support/COM.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/FileSystem.h"
@@ -123,6 +124,8 @@ int main(int argc, char **argv) {
   PrettyStackTraceProgram X(argc, argv);
   llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
 
+  llvm::sys::InitializeCOMRAII COM(llvm::sys::COMThreadingMode::MultiThreaded);
+
   cl::ParseCommandLineOptions(argc, argv, "llvm-symbolizer\n");
   LLVMSymbolizer::Options Opts(ClUseSymbolTable, ClPrintFunctions,
                                ClPrintInlining, ClDemangle, ClDefaultArch);
@@ -146,5 +149,6 @@ int main(int argc, char **argv) {
     outs() << Result << "\n";
     outs().flush();
   }
+
   return 0;
 }