Daily bump.
[official-gcc.git] / gcc / dbgcnt.c
blob458341a53a0fb753edab8ae94a5faead7430e820
1 /* Debug counter for debugging support
2 Copyright (C) 2006-2021 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
9 version.
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
14 for more details.
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. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "diagnostic-core.h"
26 #include "dumpfile.h"
27 #include "selftest.h"
28 #include "intl.h"
30 #include "dbgcnt.h"
32 struct string2counter_map {
33 const char *name;
34 enum debug_counter counter;
37 #define DEBUG_COUNTER(a) { #a , a },
39 static struct string2counter_map map[debug_counter_number_of_counters] =
41 #include "dbgcnt.def"
43 #undef DEBUG_COUNTER
45 typedef std::pair<unsigned int, unsigned int> limit_tuple;
47 static vec<limit_tuple> limits[debug_counter_number_of_counters];
48 static vec<limit_tuple> original_limits[debug_counter_number_of_counters];
50 static unsigned int count[debug_counter_number_of_counters];
52 static void
53 print_limit_reach (const char *counter, int limit, bool upper_p)
55 char buffer[128];
56 sprintf (buffer, "***dbgcnt: %s limit %d reached for %s.***\n",
57 upper_p ? "upper" : "lower", limit, counter);
58 fputs (buffer, stderr);
59 if (dump_file)
60 fputs (buffer, dump_file);
63 bool
64 dbg_cnt (enum debug_counter index)
66 unsigned v = ++count[index];
68 if (!limits[index].exists ())
69 return true;
70 else if (limits[index].is_empty ())
71 return false;
73 unsigned last = limits[index].length () - 1;
74 unsigned int min = limits[index][last].first;
75 unsigned int max = limits[index][last].second;
77 if (v < min)
78 return false;
79 else if (v == min)
81 print_limit_reach (map[index].name, v, false);
82 if (min == max)
84 print_limit_reach (map[index].name, v, true);
85 limits[index].pop ();
87 return true;
89 else if (v < max)
90 return true;
91 else if (v == max)
93 print_limit_reach (map[index].name, v, true);
94 limits[index].pop ();
95 return true;
97 else
98 return false;
101 /* Return the counter for INDEX. */
103 unsigned
104 dbg_cnt_counter (enum debug_counter index)
106 return count[index];
109 /* Compare limit_tuple intervals by first item in descending order. */
111 static int
112 cmp_tuples (const void *ptr1, const void *ptr2)
114 const limit_tuple *p1 = (const limit_tuple *)ptr1;
115 const limit_tuple *p2 = (const limit_tuple *)ptr2;
117 if (p1->first < p2->first)
118 return 1;
119 else if (p1->first > p2->first)
120 return -1;
121 return 0;
124 static bool
125 dbg_cnt_set_limit_by_index (enum debug_counter index, const char *name,
126 unsigned int low, unsigned int high)
128 if (!limits[index].exists ())
129 limits[index].create (1);
131 limits[index].safe_push (limit_tuple (low, high));
132 limits[index].qsort (cmp_tuples);
134 for (unsigned i = 0; i < limits[index].length () - 1; i++)
136 limit_tuple t1 = limits[index][i];
137 limit_tuple t2 = limits[index][i + 1];
138 if (t1.first <= t2.second)
140 error ("Interval overlap of %<-fdbg-cnt=%s%>: [%u, %u] and "
141 "[%u, %u]", name, t2.first, t2.second, t1.first, t1.second);
142 return false;
146 original_limits[index] = limits[index].copy ();
148 return true;
151 static bool
152 dbg_cnt_set_limit_by_name (const char *name, unsigned int low,
153 unsigned int high)
155 if (high < low)
157 error ("%<-fdbg-cnt=%s:%d-%d%> has smaller upper limit than the lower",
158 name, low, high);
159 return false;
162 int i;
163 for (i = debug_counter_number_of_counters - 1; i >= 0; i--)
164 if (strcmp (map[i].name, name) == 0)
165 break;
167 if (i < 0)
169 error ("cannot find a valid counter name %qs of %<-fdbg-cnt=%> option",
170 name);
171 return false;
174 return dbg_cnt_set_limit_by_index ((enum debug_counter) i, name, low, high);
177 /* Process a single "low:high" pair.
178 Returns NULL if there's no valid pair is found.
179 Otherwise returns a pointer to the end of the pair. */
181 static bool
182 dbg_cnt_process_single_pair (char *name, char *str)
184 char *value1 = strtok (str, "-");
185 char *value2 = strtok (NULL, "-");
187 unsigned int high, low;
189 if (value1 == NULL)
190 return false;
192 if (value2 == NULL)
194 high = strtol (value1, NULL, 10);
195 /* Let's allow 0:0. */
196 low = high == 0 ? 0 : 1;
198 else
200 low = strtol (value1, NULL, 10);
201 high = strtol (value2, NULL, 10);
204 return dbg_cnt_set_limit_by_name (name, low, high);
207 void
208 dbg_cnt_process_opt (const char *arg)
210 char *str = xstrdup (arg);
212 auto_vec<char *> tokens;
213 for (char *next = strtok (str, ","); next != NULL; next = strtok (NULL, ","))
214 tokens.safe_push (next);
216 unsigned i;
217 for (i = 0; i < tokens.length (); i++)
219 auto_vec<char *> ranges;
220 char *name = strtok (tokens[i], ":");
221 for (char *part = strtok (NULL, ":"); part; part = strtok (NULL, ":"))
222 ranges.safe_push (part);
224 for (unsigned j = 0; j < ranges.length (); j++)
226 if (!dbg_cnt_process_single_pair (name, ranges[j]))
227 break;
232 /* Print name, limit and count of all counters. */
234 void
235 dbg_cnt_list_all_counters (void)
237 int i;
238 fprintf (stderr, " %-30s%-15s %s\n", G_("counter name"),
239 G_("counter value"), G_("closed intervals"));
240 fprintf (stderr, "-----------------------------------------------------------------\n");
241 for (i = 0; i < debug_counter_number_of_counters; i++)
243 fprintf (stderr, " %-30s%-15d ", map[i].name, count[i]);
244 if (original_limits[i].exists ())
246 for (int j = original_limits[i].length () - 1; j >= 0; j--)
248 fprintf (stderr, "[%u, %u]", original_limits[i][j].first,
249 original_limits[i][j].second);
250 if (j > 0)
251 fprintf (stderr, ", ");
253 fprintf (stderr, "\n");
255 else
256 fprintf (stderr, "unset\n");
258 fprintf (stderr, "\n");
261 #if CHECKING_P
263 namespace selftest {
265 /* Selftests. */
267 static void
268 test_sorted_dbg_counters ()
270 for (unsigned i = 0; i < debug_counter_number_of_counters - 1; i++)
271 ASSERT_LT (strcmp (map[i].name, map[i + 1].name), 0);
274 void
275 dbgcnt_c_tests ()
277 test_sorted_dbg_counters ();
280 } // namespace selftest
282 #endif /* #if CHECKING_P */