| 1 | #include "llvm/Support/DebugCounter.h" |
| 2 | |
| 3 | #include "DebugOptions.h" |
| 4 | |
| 5 | #include "llvm/Support/CommandLine.h" |
| 6 | #include "llvm/Support/Format.h" |
| 7 | #include "llvm/Support/ManagedStatic.h" |
| 8 | |
| 9 | using namespace llvm; |
| 10 | |
| 11 | namespace llvm { |
| 12 | |
| 13 | void DebugCounter::printChunks(raw_ostream &OS, |
| 14 | ArrayRef<IntegerInclusiveInterval> Chunks) { |
| 15 | IntegerInclusiveIntervalUtils::printIntervals(OS, Intervals: Chunks, Separator: ':'); |
| 16 | } |
| 17 | |
| 18 | } // namespace llvm |
| 19 | |
| 20 | namespace { |
| 21 | // This class overrides the default list implementation of printing so we |
| 22 | // can pretty print the list of debug counter options. This type of |
| 23 | // dynamic option is pretty rare (basically this and pass lists). |
| 24 | class DebugCounterList : public cl::list<std::string, DebugCounter> { |
| 25 | private: |
| 26 | using Base = cl::list<std::string, DebugCounter>; |
| 27 | |
| 28 | public: |
| 29 | template <class... Mods> |
| 30 | explicit DebugCounterList(Mods &&... Ms) : Base(std::forward<Mods>(Ms)...) {} |
| 31 | |
| 32 | private: |
| 33 | void printOptionInfo(size_t GlobalWidth) const override { |
| 34 | // This is a variant of from generic_parser_base::printOptionInfo. Sadly, |
| 35 | // it's not easy to make it more usable. We could get it to print these as |
| 36 | // options if we were a cl::opt and registered them, but lists don't have |
| 37 | // options, nor does the parser for std::string. The other mechanisms for |
| 38 | // options are global and would pollute the global namespace with our |
| 39 | // counters. Rather than go that route, we have just overridden the |
| 40 | // printing, which only a few things call anyway. |
| 41 | outs() << " -" << ArgStr; |
| 42 | // All of the other options in CommandLine.cpp use ArgStr.size() + 6 for |
| 43 | // width, so we do the same. |
| 44 | Option::printHelpStr(HelpStr, Indent: GlobalWidth, FirstLineIndentedBy: ArgStr.size() + 6); |
| 45 | const auto &CounterInstance = DebugCounter::instance(); |
| 46 | for (const auto &Entry : CounterInstance) { |
| 47 | const auto &[Name, Desc] = CounterInstance.getCounterDesc(Info: Entry.second); |
| 48 | size_t NumSpaces = GlobalWidth - Name.size() - 8; |
| 49 | outs() << " =" << Name; |
| 50 | outs().indent(NumSpaces) << " - " << Desc << '\n'; |
| 51 | } |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | // All global objects associated to the DebugCounter, including the DebugCounter |
| 56 | // itself, are owned by a single global instance of the DebugCounterOwner |
| 57 | // struct. This makes it easier to control the order in which constructors and |
| 58 | // destructors are run. |
| 59 | struct DebugCounterOwner : DebugCounter { |
| 60 | DebugCounterList DebugCounterOption{ |
| 61 | "debug-counter" , cl::Hidden, |
| 62 | cl::desc("Comma separated list of debug counter skip and count" ), |
| 63 | cl::CommaSeparated, cl::location<DebugCounter>(L&: *this)}; |
| 64 | cl::opt<bool, true> PrintDebugCounter{ |
| 65 | "print-debug-counter" , |
| 66 | cl::Hidden, |
| 67 | cl::Optional, |
| 68 | cl::location(L&: this->ShouldPrintCounter), |
| 69 | cl::init(Val: false), |
| 70 | cl::desc("Print out debug counter info after all counters accumulated" ), |
| 71 | cl::callback(CB: [&](const bool &Value) { |
| 72 | if (Value) |
| 73 | activateAllCounters(); |
| 74 | })}; |
| 75 | cl::opt<bool, true> PrintDebugCounterQueries{ |
| 76 | "print-debug-counter-queries" , |
| 77 | cl::Hidden, |
| 78 | cl::Optional, |
| 79 | cl::location(L&: this->ShouldPrintCounterQueries), |
| 80 | cl::init(Val: false), |
| 81 | cl::desc("Print out each query of an enabled debug counter" )}; |
| 82 | cl::opt<bool, true> BreakOnLastCount{ |
| 83 | "debug-counter-break-on-last" , |
| 84 | cl::Hidden, |
| 85 | cl::Optional, |
| 86 | cl::location(L&: this->BreakOnLast), |
| 87 | cl::init(Val: false), |
| 88 | cl::desc("Insert a break point on the last enabled count of a " |
| 89 | "chunks list" )}; |
| 90 | |
| 91 | DebugCounterOwner() { |
| 92 | // Our destructor uses the debug stream. By referencing it here, we |
| 93 | // ensure that its destructor runs after our destructor. |
| 94 | (void)dbgs(); |
| 95 | } |
| 96 | |
| 97 | // Print information when destroyed, iff command line option is specified. |
| 98 | ~DebugCounterOwner() { |
| 99 | if (ShouldPrintCounter) |
| 100 | print(OS&: dbgs()); |
| 101 | } |
| 102 | }; |
| 103 | |
| 104 | } // anonymous namespace |
| 105 | |
| 106 | // Use ManagedStatic instead of function-local static variable to ensure |
| 107 | // the destructor (which accesses counters and streams) runs during |
| 108 | // llvm_shutdown() rather than at some unspecified point. |
| 109 | static ManagedStatic<DebugCounterOwner> Owner; |
| 110 | |
| 111 | void llvm::initDebugCounterOptions() { (void)DebugCounter::instance(); } |
| 112 | |
| 113 | DebugCounter &DebugCounter::instance() { return *Owner; } |
| 114 | |
| 115 | // This is called by the command line parser when it sees a value for the |
| 116 | // debug-counter option defined above. |
| 117 | void DebugCounter::push_back(const std::string &Val) { |
| 118 | if (Val.empty()) |
| 119 | return; |
| 120 | |
| 121 | // The strings should come in as counter=chunk_list |
| 122 | auto CounterPair = StringRef(Val).split(Separator: '='); |
| 123 | if (CounterPair.second.empty()) { |
| 124 | errs() << "DebugCounter Error: " << Val << " does not have an = in it\n" ; |
| 125 | exit(status: 1); |
| 126 | } |
| 127 | StringRef CounterName = CounterPair.first; |
| 128 | |
| 129 | CounterInfo *Counter = getCounterInfo(Name: CounterName); |
| 130 | if (!Counter) { |
| 131 | errs() << "DebugCounter Error: " << CounterName |
| 132 | << " is not a registered counter\n" ; |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | auto ExpectedChunks = |
| 137 | IntegerInclusiveIntervalUtils::parseIntervals(IntervalStr: CounterPair.second, Separator: ':'); |
| 138 | if (!ExpectedChunks) { |
| 139 | handleAllErrors(E: ExpectedChunks.takeError(), Handlers: [&](const StringError &E) { |
| 140 | errs() << "DebugCounter Error: " << E.getMessage() << "\n" ; |
| 141 | }); |
| 142 | exit(status: 1); |
| 143 | } |
| 144 | Counter->Chunks = std::move(*ExpectedChunks); |
| 145 | Counter->Active = Counter->IsSet = true; |
| 146 | } |
| 147 | |
| 148 | void DebugCounter::print(raw_ostream &OS) const { |
| 149 | SmallVector<StringRef, 16> CounterNames(Counters.keys()); |
| 150 | sort(C&: CounterNames); |
| 151 | |
| 152 | OS << "Counters and values:\n" ; |
| 153 | for (StringRef CounterName : CounterNames) { |
| 154 | const CounterInfo *C = getCounterInfo(Name: CounterName); |
| 155 | OS << left_justify(Str: C->Name, Width: 32) << ": {" << C->Count << "," ; |
| 156 | printChunks(OS, Chunks: C->Chunks); |
| 157 | OS << "}\n" ; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | bool DebugCounter::handleCounterIncrement(CounterInfo &Info) { |
| 162 | int64_t CurrCount = Info.Count++; |
| 163 | uint64_t CurrIdx = Info.CurrChunkIdx; |
| 164 | |
| 165 | if (Info.Chunks.empty()) |
| 166 | return true; |
| 167 | if (CurrIdx >= Info.Chunks.size()) |
| 168 | return false; |
| 169 | |
| 170 | bool Res = Info.Chunks[CurrIdx].contains(Value: CurrCount); |
| 171 | if (BreakOnLast && CurrIdx == (Info.Chunks.size() - 1) && |
| 172 | CurrCount == Info.Chunks[CurrIdx].getEnd()) { |
| 173 | LLVM_BUILTIN_DEBUGTRAP; |
| 174 | } |
| 175 | if (CurrCount > Info.Chunks[CurrIdx].getEnd()) { |
| 176 | Info.CurrChunkIdx++; |
| 177 | |
| 178 | /// Handle consecutive blocks. |
| 179 | if (Info.CurrChunkIdx < Info.Chunks.size() && |
| 180 | CurrCount == Info.Chunks[Info.CurrChunkIdx].getBegin()) |
| 181 | return true; |
| 182 | } |
| 183 | return Res; |
| 184 | } |
| 185 | |
| 186 | bool DebugCounter::shouldExecuteImpl(CounterInfo &Counter) { |
| 187 | auto &Us = instance(); |
| 188 | bool Res = Us.handleCounterIncrement(Info&: Counter); |
| 189 | if (Us.ShouldPrintCounterQueries && Counter.IsSet) { |
| 190 | dbgs() << "DebugCounter " << Counter.Name << "=" << (Counter.Count - 1) |
| 191 | << (Res ? " execute" : " skip" ) << "\n" ; |
| 192 | } |
| 193 | return Res; |
| 194 | } |
| 195 | |
| 196 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 197 | LLVM_DUMP_METHOD void DebugCounter::dump() const { |
| 198 | print(dbgs()); |
| 199 | } |
| 200 | #endif |
| 201 | |