xref: /freebsd-src/contrib/llvm-project/llvm/lib/Support/DebugCounter.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric #include "llvm/Support/DebugCounter.h"
2fe6060f1SDimitry Andric 
3fe6060f1SDimitry Andric #include "DebugOptions.h"
4fe6060f1SDimitry Andric 
50b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
60b57cec5SDimitry Andric #include "llvm/Support/Format.h"
70b57cec5SDimitry Andric 
80b57cec5SDimitry Andric using namespace llvm;
90b57cec5SDimitry Andric 
10*0fca6ea1SDimitry Andric namespace llvm {
11*0fca6ea1SDimitry Andric 
12*0fca6ea1SDimitry Andric void DebugCounter::Chunk::print(llvm::raw_ostream &OS) {
13*0fca6ea1SDimitry Andric   if (Begin == End)
14*0fca6ea1SDimitry Andric     OS << Begin;
15*0fca6ea1SDimitry Andric   else
16*0fca6ea1SDimitry Andric     OS << Begin << "-" << End;
17*0fca6ea1SDimitry Andric }
18*0fca6ea1SDimitry Andric 
19*0fca6ea1SDimitry Andric void DebugCounter::printChunks(raw_ostream &OS, ArrayRef<Chunk> Chunks) {
20*0fca6ea1SDimitry Andric   if (Chunks.empty()) {
21*0fca6ea1SDimitry Andric     OS << "empty";
22*0fca6ea1SDimitry Andric   } else {
23*0fca6ea1SDimitry Andric     bool IsFirst = true;
24*0fca6ea1SDimitry Andric     for (auto E : Chunks) {
25*0fca6ea1SDimitry Andric       if (!IsFirst)
26*0fca6ea1SDimitry Andric         OS << ':';
27*0fca6ea1SDimitry Andric       else
28*0fca6ea1SDimitry Andric         IsFirst = false;
29*0fca6ea1SDimitry Andric       E.print(OS);
30*0fca6ea1SDimitry Andric     }
31*0fca6ea1SDimitry Andric   }
32*0fca6ea1SDimitry Andric }
33*0fca6ea1SDimitry Andric 
34*0fca6ea1SDimitry Andric bool DebugCounter::parseChunks(StringRef Str, SmallVector<Chunk> &Chunks) {
35*0fca6ea1SDimitry Andric   StringRef Remaining = Str;
36*0fca6ea1SDimitry Andric 
37*0fca6ea1SDimitry Andric   auto ConsumeInt = [&]() -> int64_t {
38*0fca6ea1SDimitry Andric     StringRef Number =
39*0fca6ea1SDimitry Andric         Remaining.take_until([](char c) { return c < '0' || c > '9'; });
40*0fca6ea1SDimitry Andric     int64_t Res;
41*0fca6ea1SDimitry Andric     if (Number.getAsInteger(10, Res)) {
42*0fca6ea1SDimitry Andric       errs() << "Failed to parse int at : " << Remaining << "\n";
43*0fca6ea1SDimitry Andric       return -1;
44*0fca6ea1SDimitry Andric     }
45*0fca6ea1SDimitry Andric     Remaining = Remaining.drop_front(Number.size());
46*0fca6ea1SDimitry Andric     return Res;
47*0fca6ea1SDimitry Andric   };
48*0fca6ea1SDimitry Andric 
49*0fca6ea1SDimitry Andric   while (1) {
50*0fca6ea1SDimitry Andric     int64_t Num = ConsumeInt();
51*0fca6ea1SDimitry Andric     if (Num == -1)
52*0fca6ea1SDimitry Andric       return true;
53*0fca6ea1SDimitry Andric     if (!Chunks.empty() && Num <= Chunks[Chunks.size() - 1].End) {
54*0fca6ea1SDimitry Andric       errs() << "Expected Chunks to be in increasing order " << Num
55*0fca6ea1SDimitry Andric              << " <= " << Chunks[Chunks.size() - 1].End << "\n";
56*0fca6ea1SDimitry Andric       return true;
57*0fca6ea1SDimitry Andric     }
58*0fca6ea1SDimitry Andric     if (Remaining.starts_with("-")) {
59*0fca6ea1SDimitry Andric       Remaining = Remaining.drop_front();
60*0fca6ea1SDimitry Andric       int64_t Num2 = ConsumeInt();
61*0fca6ea1SDimitry Andric       if (Num2 == -1)
62*0fca6ea1SDimitry Andric         return true;
63*0fca6ea1SDimitry Andric       if (Num >= Num2) {
64*0fca6ea1SDimitry Andric         errs() << "Expected " << Num << " < " << Num2 << " in " << Num << "-"
65*0fca6ea1SDimitry Andric                << Num2 << "\n";
66*0fca6ea1SDimitry Andric         return true;
67*0fca6ea1SDimitry Andric       }
68*0fca6ea1SDimitry Andric 
69*0fca6ea1SDimitry Andric       Chunks.push_back({Num, Num2});
70*0fca6ea1SDimitry Andric     } else {
71*0fca6ea1SDimitry Andric       Chunks.push_back({Num, Num});
72*0fca6ea1SDimitry Andric     }
73*0fca6ea1SDimitry Andric     if (Remaining.starts_with(":")) {
74*0fca6ea1SDimitry Andric       Remaining = Remaining.drop_front();
75*0fca6ea1SDimitry Andric       continue;
76*0fca6ea1SDimitry Andric     }
77*0fca6ea1SDimitry Andric     if (Remaining.empty())
78*0fca6ea1SDimitry Andric       break;
79*0fca6ea1SDimitry Andric     errs() << "Failed to parse at : " << Remaining;
80*0fca6ea1SDimitry Andric     return true;
81*0fca6ea1SDimitry Andric   }
82*0fca6ea1SDimitry Andric   return false;
83*0fca6ea1SDimitry Andric }
84*0fca6ea1SDimitry Andric 
85*0fca6ea1SDimitry Andric } // namespace llvm
86*0fca6ea1SDimitry Andric 
870b57cec5SDimitry Andric namespace {
880b57cec5SDimitry Andric // This class overrides the default list implementation of printing so we
890b57cec5SDimitry Andric // can pretty print the list of debug counter options.  This type of
900b57cec5SDimitry Andric // dynamic option is pretty rare (basically this and pass lists).
910b57cec5SDimitry Andric class DebugCounterList : public cl::list<std::string, DebugCounter> {
920b57cec5SDimitry Andric private:
930b57cec5SDimitry Andric   using Base = cl::list<std::string, DebugCounter>;
940b57cec5SDimitry Andric 
950b57cec5SDimitry Andric public:
960b57cec5SDimitry Andric   template <class... Mods>
970b57cec5SDimitry Andric   explicit DebugCounterList(Mods &&... Ms) : Base(std::forward<Mods>(Ms)...) {}
980b57cec5SDimitry Andric 
990b57cec5SDimitry Andric private:
1000b57cec5SDimitry Andric   void printOptionInfo(size_t GlobalWidth) const override {
1010b57cec5SDimitry Andric     // This is a variant of from generic_parser_base::printOptionInfo.  Sadly,
1020b57cec5SDimitry Andric     // it's not easy to make it more usable.  We could get it to print these as
1030b57cec5SDimitry Andric     // options if we were a cl::opt and registered them, but lists don't have
1040b57cec5SDimitry Andric     // options, nor does the parser for std::string.  The other mechanisms for
1050b57cec5SDimitry Andric     // options are global and would pollute the global namespace with our
1060b57cec5SDimitry Andric     // counters.  Rather than go that route, we have just overridden the
1070b57cec5SDimitry Andric     // printing, which only a few things call anyway.
1080b57cec5SDimitry Andric     outs() << "  -" << ArgStr;
1090b57cec5SDimitry Andric     // All of the other options in CommandLine.cpp use ArgStr.size() + 6 for
1100b57cec5SDimitry Andric     // width, so we do the same.
1110b57cec5SDimitry Andric     Option::printHelpStr(HelpStr, GlobalWidth, ArgStr.size() + 6);
1120b57cec5SDimitry Andric     const auto &CounterInstance = DebugCounter::instance();
1135ffd83dbSDimitry Andric     for (const auto &Name : CounterInstance) {
1140b57cec5SDimitry Andric       const auto Info =
1150b57cec5SDimitry Andric           CounterInstance.getCounterInfo(CounterInstance.getCounterId(Name));
1160b57cec5SDimitry Andric       size_t NumSpaces = GlobalWidth - Info.first.size() - 8;
1170b57cec5SDimitry Andric       outs() << "    =" << Info.first;
1180b57cec5SDimitry Andric       outs().indent(NumSpaces) << " -   " << Info.second << '\n';
1190b57cec5SDimitry Andric     }
1200b57cec5SDimitry Andric   }
1210b57cec5SDimitry Andric };
1220b57cec5SDimitry Andric 
123bdd1243dSDimitry Andric // All global objects associated to the DebugCounter, including the DebugCounter
124bdd1243dSDimitry Andric // itself, are owned by a single global instance of the DebugCounterOwner
125bdd1243dSDimitry Andric // struct. This makes it easier to control the order in which constructors and
126bdd1243dSDimitry Andric // destructors are run.
127*0fca6ea1SDimitry Andric struct DebugCounterOwner : DebugCounter {
128bdd1243dSDimitry Andric   DebugCounterList DebugCounterOption{
1290b57cec5SDimitry Andric       "debug-counter", cl::Hidden,
1300b57cec5SDimitry Andric       cl::desc("Comma separated list of debug counter skip and count"),
131*0fca6ea1SDimitry Andric       cl::CommaSeparated, cl::location<DebugCounter>(*this)};
132*0fca6ea1SDimitry Andric   cl::opt<bool, true> PrintDebugCounter{
133*0fca6ea1SDimitry Andric       "print-debug-counter",
134*0fca6ea1SDimitry Andric       cl::Hidden,
135*0fca6ea1SDimitry Andric       cl::Optional,
136*0fca6ea1SDimitry Andric       cl::location(this->ShouldPrintCounter),
137*0fca6ea1SDimitry Andric       cl::init(false),
138bdd1243dSDimitry Andric       cl::desc("Print out debug counter info after all counters accumulated")};
139*0fca6ea1SDimitry Andric   cl::opt<bool, true> BreakOnLastCount{
140*0fca6ea1SDimitry Andric       "debug-counter-break-on-last",
141*0fca6ea1SDimitry Andric       cl::Hidden,
142*0fca6ea1SDimitry Andric       cl::Optional,
143*0fca6ea1SDimitry Andric       cl::location(this->BreakOnLast),
144*0fca6ea1SDimitry Andric       cl::init(false),
145*0fca6ea1SDimitry Andric       cl::desc("Insert a break point on the last enabled count of a "
146*0fca6ea1SDimitry Andric                "chunks list")};
147bdd1243dSDimitry Andric 
148bdd1243dSDimitry Andric   DebugCounterOwner() {
149bdd1243dSDimitry Andric     // Our destructor uses the debug stream. By referencing it here, we
150bdd1243dSDimitry Andric     // ensure that its destructor runs after our destructor.
151bdd1243dSDimitry Andric     (void)dbgs();
152fe6060f1SDimitry Andric   }
1530b57cec5SDimitry Andric 
1540b57cec5SDimitry Andric   // Print information when destroyed, iff command line option is specified.
155bdd1243dSDimitry Andric   ~DebugCounterOwner() {
156*0fca6ea1SDimitry Andric     if (ShouldPrintCounter)
157*0fca6ea1SDimitry Andric       print(dbgs());
1580b57cec5SDimitry Andric   }
159bdd1243dSDimitry Andric };
1600b57cec5SDimitry Andric 
161bdd1243dSDimitry Andric } // anonymous namespace
162bdd1243dSDimitry Andric 
163bdd1243dSDimitry Andric void llvm::initDebugCounterOptions() { (void)DebugCounter::instance(); }
164bdd1243dSDimitry Andric 
165bdd1243dSDimitry Andric DebugCounter &DebugCounter::instance() {
166bdd1243dSDimitry Andric   static DebugCounterOwner O;
167*0fca6ea1SDimitry Andric   return O;
168bdd1243dSDimitry Andric }
1690b57cec5SDimitry Andric 
1700b57cec5SDimitry Andric // This is called by the command line parser when it sees a value for the
1710b57cec5SDimitry Andric // debug-counter option defined above.
1720b57cec5SDimitry Andric void DebugCounter::push_back(const std::string &Val) {
1730b57cec5SDimitry Andric   if (Val.empty())
1740b57cec5SDimitry Andric     return;
175*0fca6ea1SDimitry Andric 
176*0fca6ea1SDimitry Andric   // The strings should come in as counter=chunk_list
1770b57cec5SDimitry Andric   auto CounterPair = StringRef(Val).split('=');
1780b57cec5SDimitry Andric   if (CounterPair.second.empty()) {
1790b57cec5SDimitry Andric     errs() << "DebugCounter Error: " << Val << " does not have an = in it\n";
1800b57cec5SDimitry Andric     return;
1810b57cec5SDimitry Andric   }
182*0fca6ea1SDimitry Andric   StringRef CounterName = CounterPair.first;
183*0fca6ea1SDimitry Andric   SmallVector<Chunk> Chunks;
184*0fca6ea1SDimitry Andric 
185*0fca6ea1SDimitry Andric   if (parseChunks(CounterPair.second, Chunks)) {
1860b57cec5SDimitry Andric     return;
1870b57cec5SDimitry Andric   }
188*0fca6ea1SDimitry Andric 
1895ffd83dbSDimitry Andric   unsigned CounterID = getCounterId(std::string(CounterName));
1900b57cec5SDimitry Andric   if (!CounterID) {
1910b57cec5SDimitry Andric     errs() << "DebugCounter Error: " << CounterName
1920b57cec5SDimitry Andric            << " is not a registered counter\n";
1930b57cec5SDimitry Andric     return;
1940b57cec5SDimitry Andric   }
1950b57cec5SDimitry Andric   enableAllCounters();
1960b57cec5SDimitry Andric 
1970b57cec5SDimitry Andric   CounterInfo &Counter = Counters[CounterID];
1980b57cec5SDimitry Andric   Counter.IsSet = true;
199*0fca6ea1SDimitry Andric   Counter.Chunks = std::move(Chunks);
2000b57cec5SDimitry Andric }
2010b57cec5SDimitry Andric 
2020b57cec5SDimitry Andric void DebugCounter::print(raw_ostream &OS) const {
2030b57cec5SDimitry Andric   SmallVector<StringRef, 16> CounterNames(RegisteredCounters.begin(),
2040b57cec5SDimitry Andric                                           RegisteredCounters.end());
205e8d8bef9SDimitry Andric   sort(CounterNames);
2060b57cec5SDimitry Andric 
2070b57cec5SDimitry Andric   auto &Us = instance();
2080b57cec5SDimitry Andric   OS << "Counters and values:\n";
2090b57cec5SDimitry Andric   for (auto &CounterName : CounterNames) {
2105ffd83dbSDimitry Andric     unsigned CounterID = getCounterId(std::string(CounterName));
2110b57cec5SDimitry Andric     OS << left_justify(RegisteredCounters[CounterID], 32) << ": {"
212*0fca6ea1SDimitry Andric        << Us.Counters[CounterID].Count << ",";
213*0fca6ea1SDimitry Andric     printChunks(OS, Us.Counters[CounterID].Chunks);
214*0fca6ea1SDimitry Andric     OS << "}\n";
2150b57cec5SDimitry Andric   }
2160b57cec5SDimitry Andric }
2170b57cec5SDimitry Andric 
218*0fca6ea1SDimitry Andric bool DebugCounter::shouldExecuteImpl(unsigned CounterName) {
219*0fca6ea1SDimitry Andric   auto &Us = instance();
220*0fca6ea1SDimitry Andric   auto Result = Us.Counters.find(CounterName);
221*0fca6ea1SDimitry Andric   if (Result != Us.Counters.end()) {
222*0fca6ea1SDimitry Andric     auto &CounterInfo = Result->second;
223*0fca6ea1SDimitry Andric     int64_t CurrCount = CounterInfo.Count++;
224*0fca6ea1SDimitry Andric     uint64_t CurrIdx = CounterInfo.CurrChunkIdx;
225*0fca6ea1SDimitry Andric 
226*0fca6ea1SDimitry Andric     if (CounterInfo.Chunks.empty())
227*0fca6ea1SDimitry Andric       return true;
228*0fca6ea1SDimitry Andric     if (CurrIdx >= CounterInfo.Chunks.size())
229*0fca6ea1SDimitry Andric       return false;
230*0fca6ea1SDimitry Andric 
231*0fca6ea1SDimitry Andric     bool Res = CounterInfo.Chunks[CurrIdx].contains(CurrCount);
232*0fca6ea1SDimitry Andric     if (Us.BreakOnLast && CurrIdx == (CounterInfo.Chunks.size() - 1) &&
233*0fca6ea1SDimitry Andric         CurrCount == CounterInfo.Chunks[CurrIdx].End) {
234*0fca6ea1SDimitry Andric       LLVM_BUILTIN_DEBUGTRAP;
235*0fca6ea1SDimitry Andric     }
236*0fca6ea1SDimitry Andric     if (CurrCount > CounterInfo.Chunks[CurrIdx].End) {
237*0fca6ea1SDimitry Andric       CounterInfo.CurrChunkIdx++;
238*0fca6ea1SDimitry Andric 
239*0fca6ea1SDimitry Andric       /// Handle consecutive blocks.
240*0fca6ea1SDimitry Andric       if (CounterInfo.CurrChunkIdx < CounterInfo.Chunks.size() &&
241*0fca6ea1SDimitry Andric           CurrCount == CounterInfo.Chunks[CounterInfo.CurrChunkIdx].Begin)
242*0fca6ea1SDimitry Andric         return true;
243*0fca6ea1SDimitry Andric     }
244*0fca6ea1SDimitry Andric     return Res;
245*0fca6ea1SDimitry Andric   }
246*0fca6ea1SDimitry Andric   // Didn't find the counter, should we warn?
247*0fca6ea1SDimitry Andric   return true;
248*0fca6ea1SDimitry Andric }
249*0fca6ea1SDimitry Andric 
2500b57cec5SDimitry Andric LLVM_DUMP_METHOD void DebugCounter::dump() const {
2510b57cec5SDimitry Andric   print(dbgs());
2520b57cec5SDimitry Andric }
253