xref: /src/contrib/llvm-project/llvm/lib/Support/DebugCounter.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
171d5a254SDimitry Andric #include "llvm/Support/DebugCounter.h"
2344a3780SDimitry Andric 
3344a3780SDimitry Andric #include "DebugOptions.h"
4344a3780SDimitry Andric 
571d5a254SDimitry Andric #include "llvm/Support/CommandLine.h"
671d5a254SDimitry Andric #include "llvm/Support/Format.h"
771d5a254SDimitry Andric 
871d5a254SDimitry Andric using namespace llvm;
971d5a254SDimitry Andric 
10ac9a064cSDimitry Andric namespace llvm {
11ac9a064cSDimitry Andric 
print(llvm::raw_ostream & OS)12ac9a064cSDimitry Andric void DebugCounter::Chunk::print(llvm::raw_ostream &OS) {
13ac9a064cSDimitry Andric   if (Begin == End)
14ac9a064cSDimitry Andric     OS << Begin;
15ac9a064cSDimitry Andric   else
16ac9a064cSDimitry Andric     OS << Begin << "-" << End;
17ac9a064cSDimitry Andric }
18ac9a064cSDimitry Andric 
printChunks(raw_ostream & OS,ArrayRef<Chunk> Chunks)19ac9a064cSDimitry Andric void DebugCounter::printChunks(raw_ostream &OS, ArrayRef<Chunk> Chunks) {
20ac9a064cSDimitry Andric   if (Chunks.empty()) {
21ac9a064cSDimitry Andric     OS << "empty";
22ac9a064cSDimitry Andric   } else {
23ac9a064cSDimitry Andric     bool IsFirst = true;
24ac9a064cSDimitry Andric     for (auto E : Chunks) {
25ac9a064cSDimitry Andric       if (!IsFirst)
26ac9a064cSDimitry Andric         OS << ':';
27ac9a064cSDimitry Andric       else
28ac9a064cSDimitry Andric         IsFirst = false;
29ac9a064cSDimitry Andric       E.print(OS);
30ac9a064cSDimitry Andric     }
31ac9a064cSDimitry Andric   }
32ac9a064cSDimitry Andric }
33ac9a064cSDimitry Andric 
parseChunks(StringRef Str,SmallVector<Chunk> & Chunks)34ac9a064cSDimitry Andric bool DebugCounter::parseChunks(StringRef Str, SmallVector<Chunk> &Chunks) {
35ac9a064cSDimitry Andric   StringRef Remaining = Str;
36ac9a064cSDimitry Andric 
37ac9a064cSDimitry Andric   auto ConsumeInt = [&]() -> int64_t {
38ac9a064cSDimitry Andric     StringRef Number =
39ac9a064cSDimitry Andric         Remaining.take_until([](char c) { return c < '0' || c > '9'; });
40ac9a064cSDimitry Andric     int64_t Res;
41ac9a064cSDimitry Andric     if (Number.getAsInteger(10, Res)) {
42ac9a064cSDimitry Andric       errs() << "Failed to parse int at : " << Remaining << "\n";
43ac9a064cSDimitry Andric       return -1;
44ac9a064cSDimitry Andric     }
45ac9a064cSDimitry Andric     Remaining = Remaining.drop_front(Number.size());
46ac9a064cSDimitry Andric     return Res;
47ac9a064cSDimitry Andric   };
48ac9a064cSDimitry Andric 
49ac9a064cSDimitry Andric   while (1) {
50ac9a064cSDimitry Andric     int64_t Num = ConsumeInt();
51ac9a064cSDimitry Andric     if (Num == -1)
52ac9a064cSDimitry Andric       return true;
53ac9a064cSDimitry Andric     if (!Chunks.empty() && Num <= Chunks[Chunks.size() - 1].End) {
54ac9a064cSDimitry Andric       errs() << "Expected Chunks to be in increasing order " << Num
55ac9a064cSDimitry Andric              << " <= " << Chunks[Chunks.size() - 1].End << "\n";
56ac9a064cSDimitry Andric       return true;
57ac9a064cSDimitry Andric     }
58ac9a064cSDimitry Andric     if (Remaining.starts_with("-")) {
59ac9a064cSDimitry Andric       Remaining = Remaining.drop_front();
60ac9a064cSDimitry Andric       int64_t Num2 = ConsumeInt();
61ac9a064cSDimitry Andric       if (Num2 == -1)
62ac9a064cSDimitry Andric         return true;
63ac9a064cSDimitry Andric       if (Num >= Num2) {
64ac9a064cSDimitry Andric         errs() << "Expected " << Num << " < " << Num2 << " in " << Num << "-"
65ac9a064cSDimitry Andric                << Num2 << "\n";
66ac9a064cSDimitry Andric         return true;
67ac9a064cSDimitry Andric       }
68ac9a064cSDimitry Andric 
69ac9a064cSDimitry Andric       Chunks.push_back({Num, Num2});
70ac9a064cSDimitry Andric     } else {
71ac9a064cSDimitry Andric       Chunks.push_back({Num, Num});
72ac9a064cSDimitry Andric     }
73ac9a064cSDimitry Andric     if (Remaining.starts_with(":")) {
74ac9a064cSDimitry Andric       Remaining = Remaining.drop_front();
75ac9a064cSDimitry Andric       continue;
76ac9a064cSDimitry Andric     }
77ac9a064cSDimitry Andric     if (Remaining.empty())
78ac9a064cSDimitry Andric       break;
79ac9a064cSDimitry Andric     errs() << "Failed to parse at : " << Remaining;
80ac9a064cSDimitry Andric     return true;
81ac9a064cSDimitry Andric   }
82ac9a064cSDimitry Andric   return false;
83ac9a064cSDimitry Andric }
84ac9a064cSDimitry Andric 
85ac9a064cSDimitry Andric } // namespace llvm
86ac9a064cSDimitry Andric 
87ab44ce3dSDimitry Andric namespace {
8871d5a254SDimitry Andric // This class overrides the default list implementation of printing so we
8971d5a254SDimitry Andric // can pretty print the list of debug counter options.  This type of
9071d5a254SDimitry Andric // dynamic option is pretty rare (basically this and pass lists).
9171d5a254SDimitry Andric class DebugCounterList : public cl::list<std::string, DebugCounter> {
9271d5a254SDimitry Andric private:
9371d5a254SDimitry Andric   using Base = cl::list<std::string, DebugCounter>;
9471d5a254SDimitry Andric 
9571d5a254SDimitry Andric public:
9671d5a254SDimitry Andric   template <class... Mods>
DebugCounterList(Mods &&...Ms)9771d5a254SDimitry Andric   explicit DebugCounterList(Mods &&... Ms) : Base(std::forward<Mods>(Ms)...) {}
9871d5a254SDimitry Andric 
9971d5a254SDimitry Andric private:
printOptionInfo(size_t GlobalWidth) const10071d5a254SDimitry Andric   void printOptionInfo(size_t GlobalWidth) const override {
10171d5a254SDimitry Andric     // This is a variant of from generic_parser_base::printOptionInfo.  Sadly,
10271d5a254SDimitry Andric     // it's not easy to make it more usable.  We could get it to print these as
10371d5a254SDimitry Andric     // options if we were a cl::opt and registered them, but lists don't have
10471d5a254SDimitry Andric     // options, nor does the parser for std::string.  The other mechanisms for
10571d5a254SDimitry Andric     // options are global and would pollute the global namespace with our
10671d5a254SDimitry Andric     // counters.  Rather than go that route, we have just overridden the
10771d5a254SDimitry Andric     // printing, which only a few things call anyway.
10871d5a254SDimitry Andric     outs() << "  -" << ArgStr;
10971d5a254SDimitry Andric     // All of the other options in CommandLine.cpp use ArgStr.size() + 6 for
11071d5a254SDimitry Andric     // width, so we do the same.
11171d5a254SDimitry Andric     Option::printHelpStr(HelpStr, GlobalWidth, ArgStr.size() + 6);
11271d5a254SDimitry Andric     const auto &CounterInstance = DebugCounter::instance();
113cfca06d7SDimitry Andric     for (const auto &Name : CounterInstance) {
11471d5a254SDimitry Andric       const auto Info =
11571d5a254SDimitry Andric           CounterInstance.getCounterInfo(CounterInstance.getCounterId(Name));
11671d5a254SDimitry Andric       size_t NumSpaces = GlobalWidth - Info.first.size() - 8;
11771d5a254SDimitry Andric       outs() << "    =" << Info.first;
11871d5a254SDimitry Andric       outs().indent(NumSpaces) << " -   " << Info.second << '\n';
11971d5a254SDimitry Andric     }
12071d5a254SDimitry Andric   }
12171d5a254SDimitry Andric };
12271d5a254SDimitry Andric 
123e3b55780SDimitry Andric // All global objects associated to the DebugCounter, including the DebugCounter
124e3b55780SDimitry Andric // itself, are owned by a single global instance of the DebugCounterOwner
125e3b55780SDimitry Andric // struct. This makes it easier to control the order in which constructors and
126e3b55780SDimitry Andric // destructors are run.
127ac9a064cSDimitry Andric struct DebugCounterOwner : DebugCounter {
128e3b55780SDimitry Andric   DebugCounterList DebugCounterOption{
129eb11fae6SDimitry Andric       "debug-counter", cl::Hidden,
13071d5a254SDimitry Andric       cl::desc("Comma separated list of debug counter skip and count"),
131ac9a064cSDimitry Andric       cl::CommaSeparated, cl::location<DebugCounter>(*this)};
132ac9a064cSDimitry Andric   cl::opt<bool, true> PrintDebugCounter{
133ac9a064cSDimitry Andric       "print-debug-counter",
134ac9a064cSDimitry Andric       cl::Hidden,
135ac9a064cSDimitry Andric       cl::Optional,
136ac9a064cSDimitry Andric       cl::location(this->ShouldPrintCounter),
137ac9a064cSDimitry Andric       cl::init(false),
138e3b55780SDimitry Andric       cl::desc("Print out debug counter info after all counters accumulated")};
139ac9a064cSDimitry Andric   cl::opt<bool, true> BreakOnLastCount{
140ac9a064cSDimitry Andric       "debug-counter-break-on-last",
141ac9a064cSDimitry Andric       cl::Hidden,
142ac9a064cSDimitry Andric       cl::Optional,
143ac9a064cSDimitry Andric       cl::location(this->BreakOnLast),
144ac9a064cSDimitry Andric       cl::init(false),
145ac9a064cSDimitry Andric       cl::desc("Insert a break point on the last enabled count of a "
146ac9a064cSDimitry Andric                "chunks list")};
147e3b55780SDimitry Andric 
DebugCounterOwner__anon47e9bf180311::DebugCounterOwner148e3b55780SDimitry Andric   DebugCounterOwner() {
149e3b55780SDimitry Andric     // Our destructor uses the debug stream. By referencing it here, we
150e3b55780SDimitry Andric     // ensure that its destructor runs after our destructor.
151e3b55780SDimitry Andric     (void)dbgs();
152344a3780SDimitry Andric   }
15371d5a254SDimitry Andric 
154d8e91e46SDimitry Andric   // Print information when destroyed, iff command line option is specified.
~DebugCounterOwner__anon47e9bf180311::DebugCounterOwner155e3b55780SDimitry Andric   ~DebugCounterOwner() {
156ac9a064cSDimitry Andric     if (ShouldPrintCounter)
157ac9a064cSDimitry Andric       print(dbgs());
158d8e91e46SDimitry Andric   }
159e3b55780SDimitry Andric };
160d8e91e46SDimitry Andric 
161e3b55780SDimitry Andric } // anonymous namespace
162e3b55780SDimitry Andric 
initDebugCounterOptions()163e3b55780SDimitry Andric void llvm::initDebugCounterOptions() { (void)DebugCounter::instance(); }
164e3b55780SDimitry Andric 
instance()165e3b55780SDimitry Andric DebugCounter &DebugCounter::instance() {
166e3b55780SDimitry Andric   static DebugCounterOwner O;
167ac9a064cSDimitry Andric   return O;
168e3b55780SDimitry Andric }
16971d5a254SDimitry Andric 
17071d5a254SDimitry Andric // This is called by the command line parser when it sees a value for the
17171d5a254SDimitry Andric // debug-counter option defined above.
push_back(const std::string & Val)17271d5a254SDimitry Andric void DebugCounter::push_back(const std::string &Val) {
17371d5a254SDimitry Andric   if (Val.empty())
17471d5a254SDimitry Andric     return;
175ac9a064cSDimitry Andric 
176ac9a064cSDimitry Andric   // The strings should come in as counter=chunk_list
17771d5a254SDimitry Andric   auto CounterPair = StringRef(Val).split('=');
17871d5a254SDimitry Andric   if (CounterPair.second.empty()) {
17971d5a254SDimitry Andric     errs() << "DebugCounter Error: " << Val << " does not have an = in it\n";
18071d5a254SDimitry Andric     return;
18171d5a254SDimitry Andric   }
182ac9a064cSDimitry Andric   StringRef CounterName = CounterPair.first;
183ac9a064cSDimitry Andric   SmallVector<Chunk> Chunks;
184ac9a064cSDimitry Andric 
185ac9a064cSDimitry Andric   if (parseChunks(CounterPair.second, Chunks)) {
18671d5a254SDimitry Andric     return;
18771d5a254SDimitry Andric   }
188ac9a064cSDimitry Andric 
189cfca06d7SDimitry Andric   unsigned CounterID = getCounterId(std::string(CounterName));
19071d5a254SDimitry Andric   if (!CounterID) {
19171d5a254SDimitry Andric     errs() << "DebugCounter Error: " << CounterName
19271d5a254SDimitry Andric            << " is not a registered counter\n";
19371d5a254SDimitry Andric     return;
19471d5a254SDimitry Andric   }
195d8e91e46SDimitry Andric   enableAllCounters();
196d8e91e46SDimitry Andric 
197d8e91e46SDimitry Andric   CounterInfo &Counter = Counters[CounterID];
198d8e91e46SDimitry Andric   Counter.IsSet = true;
199ac9a064cSDimitry Andric   Counter.Chunks = std::move(Chunks);
20071d5a254SDimitry Andric }
20171d5a254SDimitry Andric 
print(raw_ostream & OS) const2027c7aba6eSDimitry Andric void DebugCounter::print(raw_ostream &OS) const {
203d8e91e46SDimitry Andric   SmallVector<StringRef, 16> CounterNames(RegisteredCounters.begin(),
204d8e91e46SDimitry Andric                                           RegisteredCounters.end());
205b60736ecSDimitry Andric   sort(CounterNames);
206d8e91e46SDimitry Andric 
207d8e91e46SDimitry Andric   auto &Us = instance();
20871d5a254SDimitry Andric   OS << "Counters and values:\n";
209d8e91e46SDimitry Andric   for (auto &CounterName : CounterNames) {
210cfca06d7SDimitry Andric     unsigned CounterID = getCounterId(std::string(CounterName));
211d8e91e46SDimitry Andric     OS << left_justify(RegisteredCounters[CounterID], 32) << ": {"
212ac9a064cSDimitry Andric        << Us.Counters[CounterID].Count << ",";
213ac9a064cSDimitry Andric     printChunks(OS, Us.Counters[CounterID].Chunks);
214ac9a064cSDimitry Andric     OS << "}\n";
215d8e91e46SDimitry Andric   }
21671d5a254SDimitry Andric }
2177c7aba6eSDimitry Andric 
shouldExecuteImpl(unsigned CounterName)218ac9a064cSDimitry Andric bool DebugCounter::shouldExecuteImpl(unsigned CounterName) {
219ac9a064cSDimitry Andric   auto &Us = instance();
220ac9a064cSDimitry Andric   auto Result = Us.Counters.find(CounterName);
221ac9a064cSDimitry Andric   if (Result != Us.Counters.end()) {
222ac9a064cSDimitry Andric     auto &CounterInfo = Result->second;
223ac9a064cSDimitry Andric     int64_t CurrCount = CounterInfo.Count++;
224ac9a064cSDimitry Andric     uint64_t CurrIdx = CounterInfo.CurrChunkIdx;
225ac9a064cSDimitry Andric 
226ac9a064cSDimitry Andric     if (CounterInfo.Chunks.empty())
227ac9a064cSDimitry Andric       return true;
228ac9a064cSDimitry Andric     if (CurrIdx >= CounterInfo.Chunks.size())
229ac9a064cSDimitry Andric       return false;
230ac9a064cSDimitry Andric 
231ac9a064cSDimitry Andric     bool Res = CounterInfo.Chunks[CurrIdx].contains(CurrCount);
232ac9a064cSDimitry Andric     if (Us.BreakOnLast && CurrIdx == (CounterInfo.Chunks.size() - 1) &&
233ac9a064cSDimitry Andric         CurrCount == CounterInfo.Chunks[CurrIdx].End) {
234ac9a064cSDimitry Andric       LLVM_BUILTIN_DEBUGTRAP;
235ac9a064cSDimitry Andric     }
236ac9a064cSDimitry Andric     if (CurrCount > CounterInfo.Chunks[CurrIdx].End) {
237ac9a064cSDimitry Andric       CounterInfo.CurrChunkIdx++;
238ac9a064cSDimitry Andric 
239ac9a064cSDimitry Andric       /// Handle consecutive blocks.
240ac9a064cSDimitry Andric       if (CounterInfo.CurrChunkIdx < CounterInfo.Chunks.size() &&
241ac9a064cSDimitry Andric           CurrCount == CounterInfo.Chunks[CounterInfo.CurrChunkIdx].Begin)
242ac9a064cSDimitry Andric         return true;
243ac9a064cSDimitry Andric     }
244ac9a064cSDimitry Andric     return Res;
245ac9a064cSDimitry Andric   }
246ac9a064cSDimitry Andric   // Didn't find the counter, should we warn?
247ac9a064cSDimitry Andric   return true;
248ac9a064cSDimitry Andric }
249ac9a064cSDimitry Andric 
dump() const2507c7aba6eSDimitry Andric LLVM_DUMP_METHOD void DebugCounter::dump() const {
2517c7aba6eSDimitry Andric   print(dbgs());
2527c7aba6eSDimitry Andric }
253