1 /* Debug counter for debugging support
2 Copyright (C) 2006-2020 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>.
20 See dbgcnt.def for usage information. */
24 #include "coretypes.h"
25 #include "diagnostic-core.h"
32 struct string2counter_map {
34 enum debug_counter counter;
37 #define DEBUG_COUNTER(a) { #a , a },
39 static struct string2counter_map map[debug_counter_number_of_counters] =
45 typedef std::pair<unsigned int, unsigned int> limit_tuple;
47 static vec<limit_tuple> limits[debug_counter_number_of_counters];
49 static unsigned int count[debug_counter_number_of_counters];
52 print_limit_reach (const char *counter, int limit, bool upper_p)
55 sprintf (buffer, "***dbgcnt: %s limit %d reached for %s.***\n",
56 upper_p ? "upper" : "lower", limit, counter);
57 fputs (buffer, stderr);
59 fputs (buffer, dump_file);
63 dbg_cnt (enum debug_counter index)
65 unsigned v = ++count[index];
67 if (!limits[index].exists ())
69 else if (limits[index].is_empty ())
72 unsigned last = limits[index].length () - 1;
73 unsigned int min = limits[index][last].first;
74 unsigned int max = limits[index][last].second;
80 print_limit_reach (map[index].name, v, false);
89 print_limit_reach (map[index].name, v, true);
97 /* Compare limit_tuple intervals by first item in descending order. */
100 cmp_tuples (const void *ptr1, const void *ptr2)
102 const limit_tuple *p1 = (const limit_tuple *)ptr1;
103 const limit_tuple *p2 = (const limit_tuple *)ptr2;
105 if (p1->first < p2->first)
107 else if (p1->first > p2->first)
113 dbg_cnt_set_limit_by_index (enum debug_counter index, const char *name,
114 unsigned int low, unsigned int high)
116 if (!limits[index].exists ())
117 limits[index].create (1);
119 limits[index].safe_push (limit_tuple (low, high));
120 limits[index].qsort (cmp_tuples);
122 for (unsigned i = 0; i < limits[index].length () - 1; i++)
124 limit_tuple t1 = limits[index][i];
125 limit_tuple t2 = limits[index][i + 1];
126 if (t1.first <= t2.second)
128 error ("Interval overlap of %<-fdbg-cnt=%s%>: [%u, %u] and "
129 "[%u, %u]\n", name, t2.first, t2.second, t1.first, t1.second);
138 dbg_cnt_set_limit_by_name (const char *name, unsigned int low,
143 error ("%<-fdbg-cnt=%s:%d-%d%> has smaller upper limit than the lower",
149 for (i = debug_counter_number_of_counters - 1; i >= 0; i--)
150 if (strcmp (map[i].name, name) == 0)
155 error ("cannot find a valid counter name %qs of %<-fdbg-cnt=%> option",
160 return dbg_cnt_set_limit_by_index ((enum debug_counter) i, name, low, high);
163 /* Process a single "low:high" pair.
164 Returns NULL if there's no valid pair is found.
165 Otherwise returns a pointer to the end of the pair. */
168 dbg_cnt_process_single_pair (char *name, char *str)
170 char *value1 = strtok (str, "-");
171 char *value2 = strtok (NULL, "-");
173 unsigned int high, low;
180 high = strtol (value1, NULL, 10);
181 /* Let's allow 0:0. */
182 low = high == 0 ? 0 : 1;
186 low = strtol (value1, NULL, 10);
187 high = strtol (value2, NULL, 10);
190 return dbg_cnt_set_limit_by_name (name, low, high);
194 dbg_cnt_process_opt (const char *arg)
196 char *str = xstrdup (arg);
197 unsigned int start = 0;
199 auto_vec<char *> tokens;
200 for (char *next = strtok (str, ","); next != NULL; next = strtok (NULL, ","))
201 tokens.safe_push (next);
204 for (i = 0; i < tokens.length (); i++)
206 auto_vec<char *> ranges;
207 char *name = strtok (tokens[i], ":");
208 for (char *part = strtok (NULL, ":"); part; part = strtok (NULL, ":"))
209 ranges.safe_push (part);
211 for (unsigned j = 0; j < ranges.length (); j++)
213 if (!dbg_cnt_process_single_pair (name, ranges[j]))
216 start += strlen (tokens[i]) + 1;
220 /* Print name, limit and count of all counters. */
223 dbg_cnt_list_all_counters (void)
226 printf (" %-30s %s\n", G_("counter name"), G_("closed intervals"));
227 printf ("-----------------------------------------------------------------\n");
228 for (i = 0; i < debug_counter_number_of_counters; i++)
230 printf (" %-30s ", map[i].name);
231 if (limits[i].exists ())
233 for (int j = limits[i].length () - 1; j >= 0; j--)
235 printf ("[%u, %u]", limits[i][j].first, limits[i][j].second);
254 test_sorted_dbg_counters ()
256 for (unsigned i = 0; i < debug_counter_number_of_counters - 1; i++)
257 ASSERT_LT (strcmp (map[i].name, map[i + 1].name), 0);
263 test_sorted_dbg_counters ();
266 } // namespace selftest
268 #endif /* #if CHECKING_P */