LLVM  14.0.0git
DebugCounter.cpp
Go to the documentation of this file.
2 
3 #include "DebugOptions.h"
4 
6 #include "llvm/Support/Format.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:
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"),
54  }
55 };
56 } // namespace
57 
60 static bool PrintDebugCounter;
61 
64  static cl::opt<bool, true> RegisterPrintDebugCounter(
65  "print-debug-counter", cl::Hidden, cl::location(PrintDebugCounter),
66  cl::init(false), cl::Optional,
67  cl::desc("Print out debug counter info after all counters accumulated"));
68 }
69 
71 
72 // Print information when destroyed, iff command line option is specified.
74  if (isCountingEnabled() && PrintDebugCounter)
75  print(dbgs());
76 }
77 
79 
80 // This is called by the command line parser when it sees a value for the
81 // debug-counter option defined above.
82 void DebugCounter::push_back(const std::string &Val) {
83  if (Val.empty())
84  return;
85  // The strings should come in as counter=value
86  auto CounterPair = StringRef(Val).split('=');
87  if (CounterPair.second.empty()) {
88  errs() << "DebugCounter Error: " << Val << " does not have an = in it\n";
89  return;
90  }
91  // Now we have counter=value.
92  // First, process value.
93  int64_t CounterVal;
94  if (CounterPair.second.getAsInteger(0, CounterVal)) {
95  errs() << "DebugCounter Error: " << CounterPair.second
96  << " is not a number\n";
97  return;
98  }
99  // Now we need to see if this is the skip or the count, remove the suffix, and
100  // add it to the counter values.
101  if (CounterPair.first.endswith("-skip")) {
102  auto CounterName = CounterPair.first.drop_back(5);
103  unsigned CounterID = getCounterId(std::string(CounterName));
104  if (!CounterID) {
105  errs() << "DebugCounter Error: " << CounterName
106  << " is not a registered counter\n";
107  return;
108  }
110 
111  CounterInfo &Counter = Counters[CounterID];
112  Counter.Skip = CounterVal;
113  Counter.IsSet = true;
114  } else if (CounterPair.first.endswith("-count")) {
115  auto CounterName = CounterPair.first.drop_back(6);
116  unsigned CounterID = getCounterId(std::string(CounterName));
117  if (!CounterID) {
118  errs() << "DebugCounter Error: " << CounterName
119  << " is not a registered counter\n";
120  return;
121  }
123 
124  CounterInfo &Counter = Counters[CounterID];
125  Counter.StopAfter = CounterVal;
126  Counter.IsSet = true;
127  } else {
128  errs() << "DebugCounter Error: " << CounterPair.first
129  << " does not end with -skip or -count\n";
130  }
131 }
132 
134  SmallVector<StringRef, 16> CounterNames(RegisteredCounters.begin(),
135  RegisteredCounters.end());
136  sort(CounterNames);
137 
138  auto &Us = instance();
139  OS << "Counters and values:\n";
140  for (auto &CounterName : CounterNames) {
141  unsigned CounterID = getCounterId(std::string(CounterName));
142  OS << left_justify(RegisteredCounters[CounterID], 32) << ": {"
143  << Us.Counters[CounterID].Count << "," << Us.Counters[CounterID].Skip
144  << "," << Us.Counters[CounterID].StopAfter << "}\n";
145  }
146 }
147 
149  print(dbgs());
150 }
DebugCounterOption
static ManagedStatic< DebugCounterList, CreateDebugCounterOption > DebugCounterOption
Definition: DebugCounter.cpp:59
LLVM_DUMP_METHOD
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:491
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::DebugCounter::enableAllCounters
static void enableAllCounters()
Definition: DebugCounter.h:150
llvm::cl::location
LocationClass< Ty > location(Ty &L)
Definition: CommandLine.h:459
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1168
llvm::cl::CommaSeparated
@ CommaSeparated
Definition: CommandLine.h:169
ManagedStatic.h
llvm::DebugCounter
Definition: DebugCounter.h:56
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:143
llvm::DebugCounter::~DebugCounter
~DebugCounter()
Definition: DebugCounter.cpp:73
DebugOptions.h
llvm::errs
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Definition: raw_ostream.cpp:892
Format.h
llvm::DebugCounter::instance
static DebugCounter & instance()
Returns a reference to the singleton instance.
Definition: DebugCounter.cpp:78
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
CommandLine.h
llvm::outs
raw_fd_ostream & outs()
This returns a reference to a raw_fd_ostream for standard output.
Definition: raw_ostream.cpp:884
llvm::StringRef::split
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:749
llvm::ManagedStatic
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
Definition: ManagedStatic.h:83
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
Info
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
llvm::cl::ZeroOrMore
@ ZeroOrMore
Definition: CommandLine.h:120
llvm::cl::opt
Definition: CommandLine.h:1434
llvm::DebugCounter::push_back
void push_back(const std::string &)
Definition: DebugCounter.cpp:82
call
S is passed via registers r2 But gcc stores them to the and then reload them to and r3 before issuing the call(r0 contains the address of the format string)
Definition: README.txt:190
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
llvm::UniqueVector::end
iterator end()
Return an iterator to the end of the vector.
Definition: UniqueVector.h:81
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
DC
static ManagedStatic< DebugCounter > DC
Definition: DebugCounter.cpp:70
llvm::initDebugCounterOptions
void initDebugCounterOptions()
Definition: DebugCounter.cpp:62
llvm::left_justify
FormattedString left_justify(StringRef Str, unsigned Width)
left_justify - append spaces after string so total output is Width characters.
Definition: Format.h:145
llvm::DebugCounter::print
void print(raw_ostream &OS) const
Definition: DebugCounter.cpp:133
llvm::cl::Optional
@ Optional
Definition: CommandLine.h:119
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
std
Definition: BitVector.h:838
llvm::DebugCounter::getCounterId
unsigned getCounterId(const std::string &Name) const
Definition: DebugCounter.h:126
PrintDebugCounter
static bool PrintDebugCounter
Definition: DebugCounter.cpp:60
DebugCounter.h
llvm::sort
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1492
llvm::UniqueVector::begin
iterator begin()
Return an iterator to the start of the vector.
Definition: UniqueVector.h:75
llvm::raw_ostream::indent
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
Definition: raw_ostream.cpp:497
llvm::cl::desc
Definition: CommandLine.h:414
llvm::DebugCounter::dump
LLVM_DUMP_METHOD void dump() const
Definition: DebugCounter.cpp:148
llvm::sampleprof::Base
@ Base
Definition: Discriminator.h:58
llvm::cl::list
Definition: CommandLine.h:1642