BreakpointPrinter.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. //===- BreakpointPrinter.cpp - Breakpoint location printer ----------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. ///
  9. /// \file
  10. /// Breakpoint location printer.
  11. ///
  12. //===----------------------------------------------------------------------===//
  13. #include "BreakpointPrinter.h"
  14. #include "llvm/ADT/StringSet.h"
  15. #include "llvm/IR/DebugInfo.h"
  16. #include "llvm/IR/Module.h"
  17. #include "llvm/Pass.h"
  18. #include "llvm/Support/raw_ostream.h"
  19. using namespace llvm;
  20. namespace {
  21. struct BreakpointPrinter : public ModulePass {
  22. raw_ostream &Out;
  23. static char ID;
  24. BreakpointPrinter(raw_ostream &out) : ModulePass(ID), Out(out) {}
  25. void getContextName(const DIScope *Context, std::string &N) {
  26. if (auto *NS = dyn_cast<DINamespace>(Context)) {
  27. if (!NS->getName().empty()) {
  28. getContextName(NS->getScope(), N);
  29. N = N + NS->getName().str() + "::";
  30. }
  31. } else if (auto *TY = dyn_cast<DIType>(Context)) {
  32. if (!TY->getName().empty()) {
  33. getContextName(TY->getScope(), N);
  34. N = N + TY->getName().str() + "::";
  35. }
  36. }
  37. }
  38. bool runOnModule(Module &M) override {
  39. StringSet<> Processed;
  40. if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
  41. for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
  42. std::string Name;
  43. auto *SP = cast_or_null<DISubprogram>(NMD->getOperand(i));
  44. if (!SP)
  45. continue;
  46. getContextName(SP->getScope(), Name);
  47. Name = Name + SP->getName().str();
  48. if (!Name.empty() && Processed.insert(Name).second) {
  49. Out << Name << "\n";
  50. }
  51. }
  52. return false;
  53. }
  54. void getAnalysisUsage(AnalysisUsage &AU) const override {
  55. AU.setPreservesAll();
  56. }
  57. };
  58. char BreakpointPrinter::ID = 0;
  59. }
  60. ModulePass *llvm::createBreakpointPrinter(raw_ostream &out) {
  61. return new BreakpointPrinter(out);
  62. }