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 {
12 // This class overrides the default list implementation of printing so we
13 // can pretty print the list of debug counter options.  This type of
14 // dynamic option is pretty rare (basically this and pass lists).
15 class DebugCounterList : public cl::list<std::string, DebugCounter> {
16 private:
17   using Base = cl::list<std::string, DebugCounter>;
18 
19 public:
20   template <class... Mods>
21   explicit DebugCounterList(Mods &&... Ms) : Base(std::forward<Mods>(Ms)...) {}
22 
23 private:
24   void printOptionInfo(size_t GlobalWidth) const override {
25     // This is a variant of from generic_parser_base::printOptionInfo.  Sadly,
26     // it's not easy to make it more usable.  We could get it to print these as
27     // options if we were a cl::opt and registered them, but lists don't have
28     // options, nor does the parser for std::string.  The other mechanisms for
29     // options are global and would pollute the global namespace with our
30     // counters.  Rather than go that route, we have just overridden the
31     // printing, which only a few things call anyway.
32     outs() << "  -" << ArgStr;
33     // All of the other options in CommandLine.cpp use ArgStr.size() + 6 for
34     // width, so we do the same.
35     Option::printHelpStr(HelpStr, GlobalWidth, ArgStr.size() + 6);
36     const auto &CounterInstance = DebugCounter::instance();
37     for (const auto &Name : CounterInstance) {
38       const auto Info =
39           CounterInstance.getCounterInfo(CounterInstance.getCounterId(Name));
40       size_t NumSpaces = GlobalWidth - Info.first.size() - 8;
41       outs() << "    =" << Info.first;
42       outs().indent(NumSpaces) << " -   " << Info.second << '\n';
43     }
44   }
45 };
46 
47 struct CreateDebugCounterOption {
48   static void *call() {
49     return new DebugCounterList(
50         "debug-counter", cl::Hidden,
51         cl::desc("Comma separated list of debug counter skip and count"),
52         cl::CommaSeparated, cl::location(DebugCounter::instance()));
53   }
54 };
55 } // namespace
56 
57 static ManagedStatic<DebugCounterList, CreateDebugCounterOption>
58     DebugCounterOption;
59 static bool PrintDebugCounter;
60 
61 void llvm::initDebugCounterOptions() {
62   *DebugCounterOption;
63   static cl::opt<bool, true> RegisterPrintDebugCounter(
64       "print-debug-counter", cl::Hidden, cl::location(PrintDebugCounter),
65       cl::init(false), cl::Optional,
66       cl::desc("Print out debug counter info after all counters accumulated"));
67 }
68 
69 static ManagedStatic<DebugCounter> DC;
70 
71 // Print information when destroyed, iff command line option is specified.
72 DebugCounter::~DebugCounter() {
73   if (isCountingEnabled() && PrintDebugCounter)
74     print(dbgs());
75 }
76 
77 DebugCounter &DebugCounter::instance() { return *DC; }
78 
79 // This is called by the command line parser when it sees a value for the
80 // debug-counter option defined above.
81 void DebugCounter::push_back(const std::string &Val) {
82   if (Val.empty())
83     return;
84   // The strings should come in as counter=value
85   auto CounterPair = StringRef(Val).split('=');
86   if (CounterPair.second.empty()) {
87     errs() << "DebugCounter Error: " << Val << " does not have an = in it\n";
88     return;
89   }
90   // Now we have counter=value.
91   // First, process value.
92   int64_t CounterVal;
93   if (CounterPair.second.getAsInteger(0, CounterVal)) {
94     errs() << "DebugCounter Error: " << CounterPair.second
95            << " is not a number\n";
96     return;
97   }
98   // Now we need to see if this is the skip or the count, remove the suffix, and
99   // add it to the counter values.
100   if (CounterPair.first.endswith("-skip")) {
101     auto CounterName = CounterPair.first.drop_back(5);
102     unsigned CounterID = getCounterId(std::string(CounterName));
103     if (!CounterID) {
104       errs() << "DebugCounter Error: " << CounterName
105              << " is not a registered counter\n";
106       return;
107     }
108     enableAllCounters();
109 
110     CounterInfo &Counter = Counters[CounterID];
111     Counter.Skip = CounterVal;
112     Counter.IsSet = true;
113   } else if (CounterPair.first.endswith("-count")) {
114     auto CounterName = CounterPair.first.drop_back(6);
115     unsigned CounterID = getCounterId(std::string(CounterName));
116     if (!CounterID) {
117       errs() << "DebugCounter Error: " << CounterName
118              << " is not a registered counter\n";
119       return;
120     }
121     enableAllCounters();
122 
123     CounterInfo &Counter = Counters[CounterID];
124     Counter.StopAfter = CounterVal;
125     Counter.IsSet = true;
126   } else {
127     errs() << "DebugCounter Error: " << CounterPair.first
128            << " does not end with -skip or -count\n";
129   }
130 }
131 
132 void DebugCounter::print(raw_ostream &OS) const {
133   SmallVector<StringRef, 16> CounterNames(RegisteredCounters.begin(),
134                                           RegisteredCounters.end());
135   sort(CounterNames);
136 
137   auto &Us = instance();
138   OS << "Counters and values:\n";
139   for (auto &CounterName : CounterNames) {
140     unsigned CounterID = getCounterId(std::string(CounterName));
141     OS << left_justify(RegisteredCounters[CounterID], 32) << ": {"
142        << Us.Counters[CounterID].Count << "," << Us.Counters[CounterID].Skip
143        << "," << Us.Counters[CounterID].StopAfter << "}\n";
144   }
145 }
146 
147 LLVM_DUMP_METHOD void DebugCounter::dump() const {
148   print(dbgs());
149 }
150