BreakpointPrinter.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. // //
  21. ///////////////////////////////////////////////////////////////////////////////
  22. using namespace llvm;
  23. namespace {
  24. struct BreakpointPrinter : public ModulePass {
  25. raw_ostream &Out;
  26. static char ID;
  27. DITypeIdentifierMap TypeIdentifierMap;
  28. BreakpointPrinter(raw_ostream &out) : ModulePass(ID), Out(out) {}
  29. void getContextName(const DIScope *Context, std::string &N) {
  30. if (auto *NS = dyn_cast<DINamespace>(Context)) {
  31. if (!NS->getName().empty()) {
  32. getContextName(NS->getScope(), N);
  33. N = N + NS->getName().str() + "::";
  34. }
  35. } else if (auto *TY = dyn_cast<DIType>(Context)) {
  36. if (!TY->getName().empty()) {
  37. getContextName(TY->getScope().resolve(TypeIdentifierMap), N);
  38. N = N + TY->getName().str() + "::";
  39. }
  40. }
  41. }
  42. bool runOnModule(Module &M) override {
  43. TypeIdentifierMap.clear();
  44. NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
  45. if (CU_Nodes)
  46. TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
  47. StringSet<> Processed;
  48. if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
  49. for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
  50. std::string Name;
  51. auto *SP = cast_or_null<DISubprogram>(NMD->getOperand(i));
  52. if (!SP)
  53. continue;
  54. getContextName(SP->getScope().resolve(TypeIdentifierMap), Name);
  55. Name = Name + SP->getDisplayName().str();
  56. if (!Name.empty() && Processed.insert(Name).second) {
  57. Out << Name << "\n";
  58. }
  59. }
  60. return false;
  61. }
  62. void getAnalysisUsage(AnalysisUsage &AU) const override {
  63. AU.setPreservesAll();
  64. }
  65. };
  66. char BreakpointPrinter::ID = 0;
  67. }
  68. ModulePass *llvm::createBreakpointPrinter(raw_ostream &out) {
  69. return new BreakpointPrinter(out);
  70. }