1//===- BreakpointPrinter.cpp - Breakpoint location printer ----------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// \brief Breakpoint location printer.
12///
13//===----------------------------------------------------------------------===//
14#include "BreakpointPrinter.h"
15#include "llvm/ADT/StringSet.h"
16#include "llvm/IR/DebugInfo.h"
17#include "llvm/IR/Module.h"
18#include "llvm/Pass.h"
19#include "llvm/Support/raw_ostream.h"
20
21using namespace llvm;
22
23namespace {
24
25struct BreakpointPrinter : public ModulePass {
26  raw_ostream &Out;
27  static char ID;
28  DITypeIdentifierMap TypeIdentifierMap;
29
30  BreakpointPrinter(raw_ostream &out) : ModulePass(ID), Out(out) {}
31
32  void getContextName(const DIScope *Context, std::string &N) {
33    if (auto *NS = dyn_cast<DINamespace>(Context)) {
34      if (!NS->getName().empty()) {
35        getContextName(NS->getScope(), N);
36        N = N + NS->getName().str() + "::";
37      }
38    } else if (auto *TY = dyn_cast<DIType>(Context)) {
39      if (!TY->getName().empty()) {
40        getContextName(TY->getScope().resolve(TypeIdentifierMap), N);
41        N = N + TY->getName().str() + "::";
42      }
43    }
44  }
45
46  bool runOnModule(Module &M) override {
47    TypeIdentifierMap.clear();
48    NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
49    if (CU_Nodes)
50      TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
51
52    StringSet<> Processed;
53    if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
54      for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
55        std::string Name;
56        auto *SP = cast_or_null<DISubprogram>(NMD->getOperand(i));
57        if (!SP)
58          continue;
59        getContextName(SP->getScope().resolve(TypeIdentifierMap), Name);
60        Name = Name + SP->getDisplayName().str();
61        if (!Name.empty() && Processed.insert(Name).second) {
62          Out << Name << "\n";
63        }
64      }
65    return false;
66  }
67
68  void getAnalysisUsage(AnalysisUsage &AU) const override {
69    AU.setPreservesAll();
70  }
71};
72
73char BreakpointPrinter::ID = 0;
74}
75
76ModulePass *llvm::createBreakpointPrinter(raw_ostream &out) {
77  return new BreakpointPrinter(out);
78}
79