Eli Bendersky | a0ea8fa | 2014-02-12 16:48:02 +0000 | [diff] [blame] | 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 |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 11 | /// Breakpoint location printer. |
Eli Bendersky | a0ea8fa | 2014-02-12 16:48:02 +0000 | [diff] [blame] | 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | #include "BreakpointPrinter.h" |
| 15 | #include "llvm/ADT/StringSet.h" |
Chandler Carruth | f4ec8bf | 2014-03-06 00:46:21 +0000 | [diff] [blame] | 16 | #include "llvm/IR/DebugInfo.h" |
Eli Bendersky | a0ea8fa | 2014-02-12 16:48:02 +0000 | [diff] [blame] | 17 | #include "llvm/IR/Module.h" |
| 18 | #include "llvm/Pass.h" |
| 19 | #include "llvm/Support/raw_ostream.h" |
| 20 | |
| 21 | using namespace llvm; |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | struct BreakpointPrinter : public ModulePass { |
| 26 | raw_ostream &Out; |
| 27 | static char ID; |
Eli Bendersky | a0ea8fa | 2014-02-12 16:48:02 +0000 | [diff] [blame] | 28 | |
| 29 | BreakpointPrinter(raw_ostream &out) : ModulePass(ID), Out(out) {} |
| 30 | |
Duncan P. N. Exon Smith | e56023a | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 31 | void getContextName(const DIScope *Context, std::string &N) { |
| 32 | if (auto *NS = dyn_cast<DINamespace>(Context)) { |
Duncan P. N. Exon Smith | 41ed493 | 2015-04-14 03:01:27 +0000 | [diff] [blame] | 33 | if (!NS->getName().empty()) { |
| 34 | getContextName(NS->getScope(), N); |
| 35 | N = N + NS->getName().str() + "::"; |
Eli Bendersky | a0ea8fa | 2014-02-12 16:48:02 +0000 | [diff] [blame] | 36 | } |
Duncan P. N. Exon Smith | e56023a | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 37 | } else if (auto *TY = dyn_cast<DIType>(Context)) { |
Duncan P. N. Exon Smith | 7f76d29 | 2015-04-16 01:01:28 +0000 | [diff] [blame] | 38 | if (!TY->getName().empty()) { |
Duncan P. N. Exon Smith | de74840 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 39 | getContextName(TY->getScope().resolve(), N); |
Duncan P. N. Exon Smith | 7f76d29 | 2015-04-16 01:01:28 +0000 | [diff] [blame] | 40 | N = N + TY->getName().str() + "::"; |
Eli Bendersky | a0ea8fa | 2014-02-12 16:48:02 +0000 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
Craig Topper | c83e68f | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 45 | bool runOnModule(Module &M) override { |
Eli Bendersky | a0ea8fa | 2014-02-12 16:48:02 +0000 | [diff] [blame] | 46 | StringSet<> Processed; |
| 47 | if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp")) |
| 48 | for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) { |
| 49 | std::string Name; |
Duncan P. N. Exon Smith | e56023a | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 50 | auto *SP = cast_or_null<DISubprogram>(NMD->getOperand(i)); |
Eli Bendersky | a0ea8fa | 2014-02-12 16:48:02 +0000 | [diff] [blame] | 51 | if (!SP) |
| 52 | continue; |
Duncan P. N. Exon Smith | de74840 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 53 | getContextName(SP->getScope().resolve(), Name); |
Adrian Prantl | 9d291ed | 2017-04-26 23:59:52 +0000 | [diff] [blame] | 54 | Name = Name + SP->getName().str(); |
David Blaikie | 8f4a49f | 2014-11-19 02:56:00 +0000 | [diff] [blame] | 55 | if (!Name.empty() && Processed.insert(Name).second) { |
Eli Bendersky | a0ea8fa | 2014-02-12 16:48:02 +0000 | [diff] [blame] | 56 | Out << Name << "\n"; |
| 57 | } |
| 58 | } |
| 59 | return false; |
| 60 | } |
| 61 | |
Craig Topper | c83e68f | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 62 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Eli Bendersky | a0ea8fa | 2014-02-12 16:48:02 +0000 | [diff] [blame] | 63 | AU.setPreservesAll(); |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | char BreakpointPrinter::ID = 0; |
| 68 | } |
| 69 | |
| 70 | ModulePass *llvm::createBreakpointPrinter(raw_ostream &out) { |
| 71 | return new BreakpointPrinter(out); |
| 72 | } |