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(DIDescriptor Context, std::string &N) {
33    if (Context.isNameSpace()) {
34      DINameSpace NS(Context);
35      if (!NS.getName().empty()) {
36        getContextName(NS.getContext(), N);
37        N = N + NS.getName().str() + "::";
38      }
39    } else if (Context.isType()) {
40      DIType TY(Context);
41      if (!TY.getName().empty()) {
42        getContextName(TY.getContext().resolve(TypeIdentifierMap), N);
43        N = N + TY.getName().str() + "::";
44      }
45    }
46  }
47
48  bool runOnModule(Module &M) override {
49    TypeIdentifierMap.clear();
50    NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
51    if (CU_Nodes)
52      TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
53
54    StringSet<> Processed;
55    if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
56      for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
57        std::string Name;
58        DISubprogram SP(NMD->getOperand(i));
59        assert((!SP || SP.isSubprogram()) &&
60               "A MDNode in llvm.dbg.sp should be null or a DISubprogram.");
61        if (!SP)
62          continue;
63        getContextName(SP.getContext().resolve(TypeIdentifierMap), Name);
64        Name = Name + SP.getDisplayName().str();
65        if (!Name.empty() && Processed.insert(Name)) {
66          Out << Name << "\n";
67        }
68      }
69    return false;
70  }
71
72  void getAnalysisUsage(AnalysisUsage &AU) const override {
73    AU.setPreservesAll();
74  }
75};
76
77char BreakpointPrinter::ID = 0;
78}
79
80ModulePass *llvm::createBreakpointPrinter(raw_ostream &out) {
81  return new BreakpointPrinter(out);
82}
83