blob: db9c676175ce76f96e8f9066a46bcdaaea7c379c [file] [log] [blame]
Eli Bendersky54dc2832014-02-12 16:48:02 +00001//===- BreakpointPrinter.cpp - Breakpoint location printer ----------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Eli Bendersky54dc2832014-02-12 16:48:02 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000010/// Breakpoint location printer.
Eli Bendersky54dc2832014-02-12 16:48:02 +000011///
12//===----------------------------------------------------------------------===//
13#include "BreakpointPrinter.h"
14#include "llvm/ADT/StringSet.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000015#include "llvm/IR/DebugInfo.h"
Eli Bendersky54dc2832014-02-12 16:48:02 +000016#include "llvm/IR/Module.h"
17#include "llvm/Pass.h"
18#include "llvm/Support/raw_ostream.h"
19
20using namespace llvm;
21
22namespace {
23
24struct BreakpointPrinter : public ModulePass {
25 raw_ostream &Out;
26 static char ID;
Eli Bendersky54dc2832014-02-12 16:48:02 +000027
28 BreakpointPrinter(raw_ostream &out) : ModulePass(ID), Out(out) {}
29
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000030 void getContextName(const DIScope *Context, std::string &N) {
31 if (auto *NS = dyn_cast<DINamespace>(Context)) {
Duncan P. N. Exon Smith20caafb2015-04-14 03:01:27 +000032 if (!NS->getName().empty()) {
33 getContextName(NS->getScope(), N);
34 N = N + NS->getName().str() + "::";
Eli Bendersky54dc2832014-02-12 16:48:02 +000035 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000036 } else if (auto *TY = dyn_cast<DIType>(Context)) {
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +000037 if (!TY->getName().empty()) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +000038 getContextName(TY->getScope().resolve(), N);
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +000039 N = N + TY->getName().str() + "::";
Eli Bendersky54dc2832014-02-12 16:48:02 +000040 }
41 }
42 }
43
Craig Toppere56917c2014-03-08 08:27:28 +000044 bool runOnModule(Module &M) override {
Eli Bendersky54dc2832014-02-12 16:48:02 +000045 StringSet<> Processed;
46 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
47 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
48 std::string Name;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000049 auto *SP = cast_or_null<DISubprogram>(NMD->getOperand(i));
Eli Bendersky54dc2832014-02-12 16:48:02 +000050 if (!SP)
51 continue;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +000052 getContextName(SP->getScope().resolve(), Name);
Adrian Prantl9d2f0192017-04-26 23:59:52 +000053 Name = Name + SP->getName().str();
David Blaikie03569752014-11-19 02:56:00 +000054 if (!Name.empty() && Processed.insert(Name).second) {
Eli Bendersky54dc2832014-02-12 16:48:02 +000055 Out << Name << "\n";
56 }
57 }
58 return false;
59 }
60
Craig Toppere56917c2014-03-08 08:27:28 +000061 void getAnalysisUsage(AnalysisUsage &AU) const override {
Eli Bendersky54dc2832014-02-12 16:48:02 +000062 AU.setPreservesAll();
63 }
64};
65
66char BreakpointPrinter::ID = 0;
67}
68
69ModulePass *llvm::createBreakpointPrinter(raw_ostream &out) {
70 return new BreakpointPrinter(out);
71}