};
#undef DEBUG_COUNTER
-#define DEBUG_COUNTER(a) UINT_MAX,
-static unsigned int limit_high[debug_counter_number_of_counters] =
-{
-#include "dbgcnt.def"
-};
-#undef DEBUG_COUNTER
+typedef std::pair<unsigned int, unsigned int> limit_tuple;
-static unsigned int limit_low[debug_counter_number_of_counters];
+static vec<limit_tuple> limits[debug_counter_number_of_counters];
static unsigned int count[debug_counter_number_of_counters];
-bool
-dbg_cnt_is_enabled (enum debug_counter index)
-{
- unsigned v = count[index];
- return v > limit_low[index] && v <= limit_high[index];
-}
-
static void
print_limit_reach (const char *counter, int limit, bool upper_p)
{
bool
dbg_cnt (enum debug_counter index)
{
- count[index]++;
+ unsigned v = ++count[index];
+
+ if (!limits[index].exists ())
+ return true;
+ else if (limits[index].is_empty ())
+ return false;
- /* Do not print the info for default lower limit. */
- if (count[index] == limit_low[index] && limit_low[index] > 0)
- print_limit_reach (map[index].name, limit_low[index], false);
- else if (count[index] == limit_high[index])
- print_limit_reach (map[index].name, limit_high[index], true);
+ unsigned last = limits[index].length () - 1;
+ unsigned int min = limits[index][last].first;
+ unsigned int max = limits[index][last].second;
- return dbg_cnt_is_enabled (index);
+ if (v < min)
+ return false;
+ else if (v == min)
+ {
+ print_limit_reach (map[index].name, v, false);
+ if (min == max)
+ limits[index].pop ();
+ return true;
+ }
+ else if (v < max)
+ return true;
+ else if (v == max)
+ {
+ print_limit_reach (map[index].name, v, true);
+ limits[index].pop ();
+ return true;
+ }
+ else
+ return false;
}
-static void
-dbg_cnt_set_limit_by_index (enum debug_counter index, int low, int high)
-{
- limit_low[index] = low;
- limit_high[index] = high;
+/* Compare limit_tuple intervals by first item in descending order. */
- fprintf (stderr, "dbg_cnt '%s' set to %d-%d\n", map[index].name, low, high);
+static int
+cmp_tuples (const void *ptr1, const void *ptr2)
+{
+ const limit_tuple *p1 = (const limit_tuple *)ptr1;
+ const limit_tuple *p2 = (const limit_tuple *)ptr2;
+
+ if (p1->first < p2->first)
+ return 1;
+ else if (p1->first > p2->first)
+ return -1;
+ return 0;
}
static bool
-dbg_cnt_set_limit_by_name (const char *name, int low, int high)
+dbg_cnt_set_limit_by_index (enum debug_counter index, const char *name,
+ unsigned int low, unsigned int high)
{
- if (high < low)
- {
- error ("%<-fdbg-cnt=%s:%d:%d%> has smaller upper limit than the lower",
- name, low, high);
- return false;
- }
+ if (!limits[index].exists ())
+ limits[index].create (1);
- if (low < 0)
+ limits[index].safe_push (limit_tuple (low, high));
+ limits[index].qsort (cmp_tuples);
+
+ for (unsigned i = 0; i < limits[index].length () - 1; i++)
{
- error ("Lower limit %d of %<-fdbg-cnt=%s%> must be a non-negative "
- "number", low, name);
- return false;
+ limit_tuple t1 = limits[index][i];
+ limit_tuple t2 = limits[index][i + 1];
+ if (t1.first <= t2.second)
+ {
+ error ("Interval overlap of %<-fdbg-cnt=%s%>: [%u, %u] and "
+ "[%u, %u]\n", name, t2.first, t2.second, t1.first, t1.second);
+ return false;
+ }
}
- if (high < 0)
+ return true;
+}
+
+static bool
+dbg_cnt_set_limit_by_name (const char *name, unsigned int low,
+ unsigned int high)
+{
+ if (high < low)
{
- error ("Upper limit %d of %<-fdbg-cnt=%s%> must be a non-negative "
- "number", high, name);
+ error ("%<-fdbg-cnt=%s:%d-%d%> has smaller upper limit than the lower",
+ name, low, high);
return false;
}
if (i < 0)
return false;
- dbg_cnt_set_limit_by_index ((enum debug_counter) i, low, high);
- return true;
+ return dbg_cnt_set_limit_by_index ((enum debug_counter) i, name, low, high);
}
-
-/* Process a single "name:value" pair.
+/* Process a single "low:high" pair.
Returns NULL if there's no valid pair is found.
Otherwise returns a pointer to the end of the pair. */
static bool
-dbg_cnt_process_single_pair (const char *arg)
+dbg_cnt_process_single_pair (char *name, char *str)
{
- char *str = xstrdup (arg);
- char *name = strtok (str, ":");
- char *value1 = strtok (NULL, ":");
- char *value2 = strtok (NULL, ":");
+ char *value1 = strtok (str, "-");
+ char *value2 = strtok (NULL, "-");
- int high, low;
+ unsigned int high, low;
if (value1 == NULL)
return false;
if (value2 == NULL)
{
- low = 0;
+ low = 1;
high = strtol (value1, NULL, 10);
}
else
char *str = xstrdup (arg);
unsigned int start = 0;
- auto_vec<const char *> tokens;
- for (const char *next = strtok (str, ","); next != NULL;
- next = strtok (NULL, ","))
+ auto_vec<char *> tokens;
+ for (char *next = strtok (str, ","); next != NULL; next = strtok (NULL, ","))
tokens.safe_push (next);
unsigned i;
for (i = 0; i < tokens.length (); i++)
{
- if (!dbg_cnt_process_single_pair (tokens[i]))
- break;
- start += strlen (tokens[i]) + 1;
+ auto_vec<char *> ranges;
+ char *name = strtok (tokens[i], ":");
+ for (char *part = strtok (NULL, ":"); part; part = strtok (NULL, ":"))
+ ranges.safe_push (part);
+
+ for (unsigned j = 0; j < ranges.length (); j++)
+ {
+ if (!dbg_cnt_process_single_pair (name, ranges[j]))
+ break;
+ }
+ start += strlen (tokens[i]) + 1;
}
if (i != tokens.length ())
dbg_cnt_list_all_counters (void)
{
int i;
- printf (" %-32s %-11s %-12s\n", "counter name", "low limit",
- "high limit");
+ printf (" %-30s %s\n", "counter name", "closed intervals");
printf ("-----------------------------------------------------------------\n");
for (i = 0; i < debug_counter_number_of_counters; i++)
- printf (" %-30s %11u %12u\n",
- map[i].name, limit_low[map[i].counter], limit_high[map[i].counter]);
+ {
+ printf (" %-30s ", map[i].name);
+ if (limits[i].exists ())
+ {
+ for (int j = limits[i].length () - 1; j >= 0; j--)
+ {
+ printf ("[%u, %u]", limits[i][j].first, limits[i][j].second);
+ if (j > 0)
+ printf (", ");
+ }
+ putchar ('\n');
+ }
+ else
+ printf ("unset\n");
+ }
printf ("\n");
}