Path: blob/main/contrib/llvm-project/llvm/tools/llvm-pdbutil/PrettyCompilandDumper.cpp
35260 views
//===- PrettyCompilandDumper.cpp - llvm-pdbutil compiland dumper -*- C++ *-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "PrettyCompilandDumper.h"910#include "PrettyFunctionDumper.h"11#include "llvm-pdbutil.h"1213#include "llvm/ADT/StringExtras.h"14#include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"15#include "llvm/DebugInfo/PDB/IPDBLineNumber.h"16#include "llvm/DebugInfo/PDB/IPDBSession.h"17#include "llvm/DebugInfo/PDB/IPDBSourceFile.h"18#include "llvm/DebugInfo/PDB/PDBExtras.h"19#include "llvm/DebugInfo/PDB/PDBSymbol.h"20#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"21#include "llvm/DebugInfo/PDB/PDBSymbolData.h"22#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"23#include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h"24#include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h"25#include "llvm/DebugInfo/PDB/PDBSymbolLabel.h"26#include "llvm/DebugInfo/PDB/PDBSymbolThunk.h"27#include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"28#include "llvm/DebugInfo/PDB/PDBSymbolUnknown.h"29#include "llvm/DebugInfo/PDB/PDBSymbolUsingNamespace.h"30#include "llvm/Support/Format.h"31#include "llvm/Support/Path.h"32#include "llvm/Support/raw_ostream.h"3334#include <utility>3536using namespace llvm;37using namespace llvm::pdb;3839CompilandDumper::CompilandDumper(LinePrinter &P)40: PDBSymDumper(true), Printer(P) {}4142void CompilandDumper::dump(const PDBSymbolCompilandDetails &Symbol) {}4344void CompilandDumper::dump(const PDBSymbolCompilandEnv &Symbol) {}4546void CompilandDumper::start(const PDBSymbolCompiland &Symbol,47CompilandDumpFlags opts) {48std::string FullName = Symbol.getName();49if (Printer.IsCompilandExcluded(FullName))50return;5152Printer.NewLine();53WithColor(Printer, PDB_ColorItem::Path).get() << FullName;5455if (opts & Flags::Lines) {56const IPDBSession &Session = Symbol.getSession();57if (auto Files = Session.getSourceFilesForCompiland(Symbol)) {58Printer.Indent();59while (auto File = Files->getNext()) {60Printer.NewLine();61WithColor(Printer, PDB_ColorItem::Path).get() << File->getFileName();62if (File->getChecksumType() != PDB_Checksum::None) {63auto ChecksumType = File->getChecksumType();64auto ChecksumHexString = toHex(File->getChecksum());65WithColor(Printer, PDB_ColorItem::Comment).get()66<< " (" << ChecksumType << ": " << ChecksumHexString << ")";67}6869auto Lines = Session.findLineNumbers(Symbol, *File);70if (!Lines)71continue;7273Printer.Indent();74while (auto Line = Lines->getNext()) {75Printer.NewLine();76uint32_t LineStart = Line->getLineNumber();77uint32_t LineEnd = Line->getLineNumberEnd();7879Printer << "Line ";80PDB_ColorItem StatementColor = Line->isStatement()81? PDB_ColorItem::Keyword82: PDB_ColorItem::LiteralValue;83WithColor(Printer, StatementColor).get() << LineStart;84if (LineStart != LineEnd)85WithColor(Printer, StatementColor).get() << " - " << LineEnd;8687uint32_t ColumnStart = Line->getColumnNumber();88uint32_t ColumnEnd = Line->getColumnNumberEnd();89if (ColumnStart != 0 || ColumnEnd != 0) {90Printer << ", Column: ";91WithColor(Printer, StatementColor).get() << ColumnStart;92if (ColumnEnd != ColumnStart)93WithColor(Printer, StatementColor).get() << " - " << ColumnEnd;94}9596Printer << ", Address: ";97if (Line->getLength() > 0) {98uint64_t AddrStart = Line->getVirtualAddress();99uint64_t AddrEnd = AddrStart + Line->getLength() - 1;100WithColor(Printer, PDB_ColorItem::Address).get()101<< "[" << format_hex(AddrStart, 10) << " - "102<< format_hex(AddrEnd, 10) << "]";103Printer << " (" << Line->getLength() << " bytes)";104} else {105uint64_t AddrStart = Line->getVirtualAddress();106WithColor(Printer, PDB_ColorItem::Address).get()107<< "[" << format_hex(AddrStart, 10) << "] ";108Printer << "(0 bytes)";109}110}111Printer.Unindent();112}113Printer.Unindent();114}115}116117if (opts & Flags::Children) {118if (auto ChildrenEnum = Symbol.findAllChildren()) {119Printer.Indent();120while (auto Child = ChildrenEnum->getNext())121Child->dump(*this);122Printer.Unindent();123}124}125}126127void CompilandDumper::dump(const PDBSymbolData &Symbol) {128if (!shouldDumpSymLevel(opts::pretty::SymLevel::Data))129return;130if (Printer.IsSymbolExcluded(Symbol.getName()))131return;132133Printer.NewLine();134135switch (auto LocType = Symbol.getLocationType()) {136case PDB_LocType::Static:137Printer << "data: ";138WithColor(Printer, PDB_ColorItem::Address).get()139<< "[" << format_hex(Symbol.getVirtualAddress(), 10) << "]";140141WithColor(Printer, PDB_ColorItem::Comment).get()142<< " [sizeof = " << getTypeLength(Symbol) << "]";143144break;145case PDB_LocType::Constant:146Printer << "constant: ";147WithColor(Printer, PDB_ColorItem::LiteralValue).get()148<< "[" << Symbol.getValue() << "]";149WithColor(Printer, PDB_ColorItem::Comment).get()150<< " [sizeof = " << getTypeLength(Symbol) << "]";151break;152default:153Printer << "data(unexpected type=" << LocType << ")";154}155156Printer << " ";157WithColor(Printer, PDB_ColorItem::Identifier).get() << Symbol.getName();158}159160void CompilandDumper::dump(const PDBSymbolFunc &Symbol) {161if (!shouldDumpSymLevel(opts::pretty::SymLevel::Functions))162return;163if (Symbol.getLength() == 0)164return;165if (Printer.IsSymbolExcluded(Symbol.getName()))166return;167168Printer.NewLine();169FunctionDumper Dumper(Printer);170Dumper.start(Symbol, FunctionDumper::PointerType::None);171}172173void CompilandDumper::dump(const PDBSymbolLabel &Symbol) {174if (Printer.IsSymbolExcluded(Symbol.getName()))175return;176177Printer.NewLine();178Printer << "label ";179WithColor(Printer, PDB_ColorItem::Address).get()180<< "[" << format_hex(Symbol.getVirtualAddress(), 10) << "] ";181WithColor(Printer, PDB_ColorItem::Identifier).get() << Symbol.getName();182}183184void CompilandDumper::dump(const PDBSymbolThunk &Symbol) {185if (!shouldDumpSymLevel(opts::pretty::SymLevel::Thunks))186return;187if (Printer.IsSymbolExcluded(Symbol.getName()))188return;189190Printer.NewLine();191Printer << "thunk ";192codeview::ThunkOrdinal Ordinal = Symbol.getThunkOrdinal();193uint64_t VA = Symbol.getVirtualAddress();194if (Ordinal == codeview::ThunkOrdinal::TrampIncremental) {195uint64_t Target = Symbol.getTargetVirtualAddress();196WithColor(Printer, PDB_ColorItem::Address).get() << format_hex(VA, 10);197Printer << " -> ";198WithColor(Printer, PDB_ColorItem::Address).get() << format_hex(Target, 10);199} else {200WithColor(Printer, PDB_ColorItem::Address).get()201<< "[" << format_hex(VA, 10) << " - "202<< format_hex(VA + Symbol.getLength(), 10) << "]";203}204Printer << " (";205WithColor(Printer, PDB_ColorItem::Register).get() << Ordinal;206Printer << ") ";207std::string Name = Symbol.getName();208if (!Name.empty())209WithColor(Printer, PDB_ColorItem::Identifier).get() << Name;210}211212void CompilandDumper::dump(const PDBSymbolTypeTypedef &Symbol) {}213214void CompilandDumper::dump(const PDBSymbolUnknown &Symbol) {215Printer.NewLine();216Printer << "unknown (" << Symbol.getSymTag() << ")";217}218219void CompilandDumper::dump(const PDBSymbolUsingNamespace &Symbol) {220if (Printer.IsSymbolExcluded(Symbol.getName()))221return;222223Printer.NewLine();224Printer << "using namespace ";225std::string Name = Symbol.getName();226WithColor(Printer, PDB_ColorItem::Identifier).get() << Name;227}228229230