10b57cec5SDimitry Andric //===-- ModuleDebugInfoPrinter.cpp - Prints module debug info metadata ----===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This pass decodes the debug info metadata in a module and prints in a
100b57cec5SDimitry Andric // (sufficiently-prepared-) human-readable form.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric // For example, run this pass from opt along with the -analyze option, and
130b57cec5SDimitry Andric // it'll print to standard output.
140b57cec5SDimitry Andric //
150b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
160b57cec5SDimitry Andric
17e8d8bef9SDimitry Andric #include "llvm/Analysis/ModuleDebugInfoPrinter.h"
180b57cec5SDimitry Andric #include "llvm/Analysis/Passes.h"
1981ad6265SDimitry Andric #include "llvm/BinaryFormat/Dwarf.h"
200b57cec5SDimitry Andric #include "llvm/IR/DebugInfo.h"
21e8d8bef9SDimitry Andric #include "llvm/IR/PassManager.h"
22480093f4SDimitry Andric #include "llvm/InitializePasses.h"
230b57cec5SDimitry Andric #include "llvm/Pass.h"
240b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
250b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
260b57cec5SDimitry Andric using namespace llvm;
270b57cec5SDimitry Andric
printFile(raw_ostream & O,StringRef Filename,StringRef Directory,unsigned Line=0)280b57cec5SDimitry Andric static void printFile(raw_ostream &O, StringRef Filename, StringRef Directory,
290b57cec5SDimitry Andric unsigned Line = 0) {
300b57cec5SDimitry Andric if (Filename.empty())
310b57cec5SDimitry Andric return;
320b57cec5SDimitry Andric
330b57cec5SDimitry Andric O << " from ";
340b57cec5SDimitry Andric if (!Directory.empty())
350b57cec5SDimitry Andric O << Directory << "/";
360b57cec5SDimitry Andric O << Filename;
370b57cec5SDimitry Andric if (Line)
380b57cec5SDimitry Andric O << ":" << Line;
390b57cec5SDimitry Andric }
400b57cec5SDimitry Andric
printModuleDebugInfo(raw_ostream & O,const Module * M,const DebugInfoFinder & Finder)41e8d8bef9SDimitry Andric static void printModuleDebugInfo(raw_ostream &O, const Module *M,
42e8d8bef9SDimitry Andric const DebugInfoFinder &Finder) {
430b57cec5SDimitry Andric // Printing the nodes directly isn't particularly helpful (since they
440b57cec5SDimitry Andric // reference other nodes that won't be printed, particularly for the
450b57cec5SDimitry Andric // filenames), so just print a few useful things.
460b57cec5SDimitry Andric for (DICompileUnit *CU : Finder.compile_units()) {
470b57cec5SDimitry Andric O << "Compile unit: ";
480b57cec5SDimitry Andric auto Lang = dwarf::LanguageString(CU->getSourceLanguage());
490b57cec5SDimitry Andric if (!Lang.empty())
500b57cec5SDimitry Andric O << Lang;
510b57cec5SDimitry Andric else
520b57cec5SDimitry Andric O << "unknown-language(" << CU->getSourceLanguage() << ")";
530b57cec5SDimitry Andric printFile(O, CU->getFilename(), CU->getDirectory());
540b57cec5SDimitry Andric O << '\n';
550b57cec5SDimitry Andric }
560b57cec5SDimitry Andric
570b57cec5SDimitry Andric for (DISubprogram *S : Finder.subprograms()) {
580b57cec5SDimitry Andric O << "Subprogram: " << S->getName();
590b57cec5SDimitry Andric printFile(O, S->getFilename(), S->getDirectory(), S->getLine());
600b57cec5SDimitry Andric if (!S->getLinkageName().empty())
610b57cec5SDimitry Andric O << " ('" << S->getLinkageName() << "')";
620b57cec5SDimitry Andric O << '\n';
630b57cec5SDimitry Andric }
640b57cec5SDimitry Andric
65*fcaf7f86SDimitry Andric for (auto *GVU : Finder.global_variables()) {
660b57cec5SDimitry Andric const auto *GV = GVU->getVariable();
670b57cec5SDimitry Andric O << "Global variable: " << GV->getName();
680b57cec5SDimitry Andric printFile(O, GV->getFilename(), GV->getDirectory(), GV->getLine());
690b57cec5SDimitry Andric if (!GV->getLinkageName().empty())
700b57cec5SDimitry Andric O << " ('" << GV->getLinkageName() << "')";
710b57cec5SDimitry Andric O << '\n';
720b57cec5SDimitry Andric }
730b57cec5SDimitry Andric
740b57cec5SDimitry Andric for (const DIType *T : Finder.types()) {
750b57cec5SDimitry Andric O << "Type:";
760b57cec5SDimitry Andric if (!T->getName().empty())
770b57cec5SDimitry Andric O << ' ' << T->getName();
780b57cec5SDimitry Andric printFile(O, T->getFilename(), T->getDirectory(), T->getLine());
790b57cec5SDimitry Andric if (auto *BT = dyn_cast<DIBasicType>(T)) {
800b57cec5SDimitry Andric O << " ";
810b57cec5SDimitry Andric auto Encoding = dwarf::AttributeEncodingString(BT->getEncoding());
820b57cec5SDimitry Andric if (!Encoding.empty())
830b57cec5SDimitry Andric O << Encoding;
840b57cec5SDimitry Andric else
850b57cec5SDimitry Andric O << "unknown-encoding(" << BT->getEncoding() << ')';
860b57cec5SDimitry Andric } else {
870b57cec5SDimitry Andric O << ' ';
880b57cec5SDimitry Andric auto Tag = dwarf::TagString(T->getTag());
890b57cec5SDimitry Andric if (!Tag.empty())
900b57cec5SDimitry Andric O << Tag;
910b57cec5SDimitry Andric else
920b57cec5SDimitry Andric O << "unknown-tag(" << T->getTag() << ")";
930b57cec5SDimitry Andric }
940b57cec5SDimitry Andric if (auto *CT = dyn_cast<DICompositeType>(T)) {
950b57cec5SDimitry Andric if (auto *S = CT->getRawIdentifier())
960b57cec5SDimitry Andric O << " (identifier: '" << S->getString() << "')";
970b57cec5SDimitry Andric }
980b57cec5SDimitry Andric O << '\n';
990b57cec5SDimitry Andric }
1000b57cec5SDimitry Andric }
101e8d8bef9SDimitry Andric
ModuleDebugInfoPrinterPass(raw_ostream & OS)102e8d8bef9SDimitry Andric ModuleDebugInfoPrinterPass::ModuleDebugInfoPrinterPass(raw_ostream &OS)
103e8d8bef9SDimitry Andric : OS(OS) {}
104e8d8bef9SDimitry Andric
run(Module & M,ModuleAnalysisManager & AM)105e8d8bef9SDimitry Andric PreservedAnalyses ModuleDebugInfoPrinterPass::run(Module &M,
106e8d8bef9SDimitry Andric ModuleAnalysisManager &AM) {
107e8d8bef9SDimitry Andric Finder.processModule(M);
108e8d8bef9SDimitry Andric printModuleDebugInfo(OS, &M, Finder);
109e8d8bef9SDimitry Andric return PreservedAnalyses::all();
110e8d8bef9SDimitry Andric }
111