#include "llvm/Support/DebugCounter.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/Options.h"
using namespace llvm;
namespace {
class DebugCounterList : public cl::list<std::string, DebugCounter> {
private:
using Base = cl::list<std::string, DebugCounter>;
public:
template <class... Mods>
explicit DebugCounterList(Mods &&... Ms) : Base(std::forward<Mods>(Ms)...) {}
private:
void printOptionInfo(size_t GlobalWidth) const override {
outs() << " -" << ArgStr;
Option::printHelpStr(HelpStr, GlobalWidth, ArgStr.size() + 6);
const auto &CounterInstance = DebugCounter::instance();
for (auto Name : CounterInstance) {
const auto Info =
CounterInstance.getCounterInfo(CounterInstance.getCounterId(Name));
size_t NumSpaces = GlobalWidth - Info.first.size() - 8;
outs() << " =" << Info.first;
outs().indent(NumSpaces) << " - " << Info.second << '\n';
}
}
};
}
static DebugCounterList DebugCounterOption(
"debug-counter", cl::Hidden,
cl::desc("Comma separated list of debug counter skip and count"),
cl::CommaSeparated, cl::ZeroOrMore, cl::location(DebugCounter::instance()));
static ManagedStatic<DebugCounter> DC;
DebugCounter &DebugCounter::instance() { return *DC; }
void DebugCounter::push_back(const std::string &Val) {
if (Val.empty())
return;
auto CounterPair = StringRef(Val).split('=');
if (CounterPair.second.empty()) {
errs() << "DebugCounter Error: " << Val << " does not have an = in it\n";
return;
}
int64_t CounterVal;
if (CounterPair.second.getAsInteger(0, CounterVal)) {
errs() << "DebugCounter Error: " << CounterPair.second
<< " is not a number\n";
return;
}
if (CounterPair.first.endswith("-skip")) {
auto CounterName = CounterPair.first.drop_back(5);
unsigned CounterID = getCounterId(CounterName);
if (!CounterID) {
errs() << "DebugCounter Error: " << CounterName
<< " is not a registered counter\n";
return;
}
enableAllCounters();
Counters[CounterID].Skip = CounterVal;
Counters[CounterID].IsSet = true;
} else if (CounterPair.first.endswith("-count")) {
auto CounterName = CounterPair.first.drop_back(6);
unsigned CounterID = getCounterId(CounterName);
if (!CounterID) {
errs() << "DebugCounter Error: " << CounterName
<< " is not a registered counter\n";
return;
}
enableAllCounters();
Counters[CounterID].StopAfter = CounterVal;
Counters[CounterID].IsSet = true;
} else {
errs() << "DebugCounter Error: " << CounterPair.first
<< " does not end with -skip or -count\n";
}
}
void DebugCounter::print(raw_ostream &OS) const {
OS << "Counters and values:\n";
for (const auto &KV : Counters)
OS << left_justify(RegisteredCounters[KV.first], 32) << ": {"
<< KV.second.Count << "," << KV.second.Skip << ","
<< KV.second.StopAfter << "}\n";
}
LLVM_DUMP_METHOD void DebugCounter::dump() const {
print(dbgs());
}