Index: include/llvm/DebugInfo/PDB/PDBContext.h =================================================================== --- /dev/null +++ include/llvm/DebugInfo/PDB/PDBContext.h @@ -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_LIB_DEBUGINFO_PDB_PDBCONTEXT_H +#define LLVM_LIB_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 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 Session; +}; +} + +#endif Index: lib/DebugInfo/PDB/CMakeLists.txt =================================================================== --- lib/DebugInfo/PDB/CMakeLists.txt +++ lib/DebugInfo/PDB/CMakeLists.txt @@ -32,6 +32,7 @@ add_llvm_library(LLVMDebugInfoPDB IPDBSourceFile.cpp PDB.cpp + PDBContext.cpp PDBExtras.cpp PDBInterfaceAnchors.cpp PDBSymbol.cpp Index: lib/DebugInfo/PDB/PDBContext.cpp =================================================================== --- /dev/null +++ lib/DebugInfo/PDB/PDBContext.cpp @@ -0,0 +1,114 @@ +//===-- 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 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(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 = 0; + DILineInfo Result; + if (auto Func = dyn_cast(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(Symbol.get())) { + Length = Data->getLength(); + } + if (Length > 0) { + auto LineNumbers = Session->findLineNumbersByAddress(Address, Length); + if (LineNumbers && LineNumbers->getChildCount() > 0) { + auto LineInfo = LineNumbers->getNext(); + 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) + return Table; + + while (auto LineInfo = LineNumbers->getNext()) { + DILineInfo LineEntry; + auto SourceFile = Session->getSourceFileById(LineInfo->getSourceFileId()); + LineEntry.Column = LineInfo->getColumnNumber(); + LineEntry.Line = LineInfo->getLineNumber(); + if (SourceFile && + Specifier.FLIKind != DILineInfoSpecifier::FileLineInfoKind::None) + LineEntry.FileName = SourceFile->getFileName(); + if (Specifier.FNKind != DINameKind::None) { + auto Symbol = Session->findSymbolByAddress(LineInfo->getVirtualAddress()); + if (auto Func = dyn_cast(Symbol.get())) { + if (Specifier.FNKind == DINameKind::LinkageName) + LineEntry.FunctionName = Func->getUndecoratedName(); + else + LineEntry.FunctionName = Func->getName(); + } + } + + 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; +} Index: tools/llvm-symbolizer/CMakeLists.txt =================================================================== --- tools/llvm-symbolizer/CMakeLists.txt +++ tools/llvm-symbolizer/CMakeLists.txt @@ -5,6 +5,7 @@ set(LLVM_LINK_COMPONENTS DebugInfoDWARF + DebugInfoPDB Object Support ) Index: tools/llvm-symbolizer/LLVMSymbolize.cpp =================================================================== --- tools/llvm-symbolizer/LLVMSymbolize.cpp +++ tools/llvm-symbolizer/LLVMSymbolize.cpp @@ -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 @@ 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 @@ Modules.insert(make_pair(ModuleName, (ModuleInfo *)nullptr)); return nullptr; } - DIContext *Context = new DWARFContextInMemory(*Objects.second); + DIContext *Context = nullptr; + if (auto CoffObject = dyn_cast(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 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: tools/llvm-symbolizer/llvm-symbolizer.cpp =================================================================== --- tools/llvm-symbolizer/llvm-symbolizer.cpp +++ tools/llvm-symbolizer/llvm-symbolizer.cpp @@ -17,6 +17,7 @@ #include "LLVMSymbolize.h" #include "llvm/ADT/StringRef.h" +#include "llvm/Config/config.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" #include "llvm/Support/FileSystem.h" @@ -29,6 +30,10 @@ #include #include +#if defined(_WIN32) && defined(HAVE_DIA_SDK) +#include +#endif + using namespace llvm; using namespace symbolize; @@ -123,6 +128,10 @@ PrettyStackTraceProgram X(argc, argv); llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. +#if defined(_WIN32) && defined(HAVE_DIA_SDK) + ::CoInitializeEx(nullptr, COINIT_MULTITHREADED); +#endif + cl::ParseCommandLineOptions(argc, argv, "llvm-symbolizer\n"); LLVMSymbolizer::Options Opts(ClUseSymbolTable, ClPrintFunctions, ClPrintInlining, ClDemangle, ClDefaultArch); @@ -146,5 +155,10 @@ outs() << Result << "\n"; outs().flush(); } + +#if defined(_WIN32) && defined(HAVE_DIA_SDK) + ::CoUninitialize(); +#endif + return 0; }