]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/dbgcnt.c
Correct a function pre/postcondition [PR102403].
[thirdparty/gcc.git] / gcc / dbgcnt.c
CommitLineData
6fb5fa3c 1/* Debug counter for debugging support
99dee823 2 Copyright (C) 2006-2021 Free Software Foundation, Inc.
6fb5fa3c
DB
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
9dcd6f09 8Software Foundation; either version 3, or (at your option) any later
6fb5fa3c
DB
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
9dcd6f09 17along with GCC; see the file COPYING3. If not see
b8698a0f 18<http://www.gnu.org/licenses/>.
6fb5fa3c
DB
19
20See dbgcnt.def for usage information. */
21
22#include "config.h"
23#include "system.h"
24#include "coretypes.h"
718f9c0f 25#include "diagnostic-core.h"
7ee2468b 26#include "dumpfile.h"
5024c8bb 27#include "selftest.h"
03df119d 28#include "intl.h"
6fb5fa3c
DB
29
30#include "dbgcnt.h"
31
32struct string2counter_map {
33 const char *name;
34 enum debug_counter counter;
35};
36
37#define DEBUG_COUNTER(a) { #a , a },
38
39static struct string2counter_map map[debug_counter_number_of_counters] =
40{
41#include "dbgcnt.def"
42};
43#undef DEBUG_COUNTER
44
83a49336 45typedef std::pair<unsigned int, unsigned int> limit_tuple;
6fb5fa3c 46
83a49336 47static vec<limit_tuple> limits[debug_counter_number_of_counters];
a30d4fc5 48static vec<limit_tuple> original_limits[debug_counter_number_of_counters];
cdc3b883 49
6fb5fa3c
DB
50static unsigned int count[debug_counter_number_of_counters];
51
dd7fa0d4
ML
52static void
53print_limit_reach (const char *counter, int limit, bool upper_p)
54{
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);
61}
62
6fb5fa3c
DB
63bool
64dbg_cnt (enum debug_counter index)
65{
83a49336
ML
66 unsigned v = ++count[index];
67
68 if (!limits[index].exists ())
69 return true;
70 else if (limits[index].is_empty ())
71 return false;
cdc3b883 72
83a49336
ML
73 unsigned last = limits[index].length () - 1;
74 unsigned int min = limits[index][last].first;
75 unsigned int max = limits[index][last].second;
cc806ac1 76
83a49336
ML
77 if (v < min)
78 return false;
79 else if (v == min)
80 {
81 print_limit_reach (map[index].name, v, false);
82 if (min == max)
8988ec5b
ML
83 {
84 print_limit_reach (map[index].name, v, true);
85 limits[index].pop ();
86 }
83a49336
ML
87 return true;
88 }
89 else if (v < max)
90 return true;
91 else if (v == max)
92 {
93 print_limit_reach (map[index].name, v, true);
94 limits[index].pop ();
95 return true;
96 }
97 else
98 return false;
6fb5fa3c
DB
99}
100
83a49336 101/* Compare limit_tuple intervals by first item in descending order. */
6fb5fa3c 102
83a49336
ML
103static int
104cmp_tuples (const void *ptr1, const void *ptr2)
105{
106 const limit_tuple *p1 = (const limit_tuple *)ptr1;
107 const limit_tuple *p2 = (const limit_tuple *)ptr2;
108
109 if (p1->first < p2->first)
110 return 1;
111 else if (p1->first > p2->first)
112 return -1;
113 return 0;
6fb5fa3c
DB
114}
115
0a090f42 116static bool
83a49336
ML
117dbg_cnt_set_limit_by_index (enum debug_counter index, const char *name,
118 unsigned int low, unsigned int high)
6fb5fa3c 119{
83a49336
ML
120 if (!limits[index].exists ())
121 limits[index].create (1);
cdc3b883 122
83a49336
ML
123 limits[index].safe_push (limit_tuple (low, high));
124 limits[index].qsort (cmp_tuples);
125
126 for (unsigned i = 0; i < limits[index].length () - 1; i++)
cdc3b883 127 {
83a49336
ML
128 limit_tuple t1 = limits[index][i];
129 limit_tuple t2 = limits[index][i + 1];
130 if (t1.first <= t2.second)
131 {
132 error ("Interval overlap of %<-fdbg-cnt=%s%>: [%u, %u] and "
47fe9634 133 "[%u, %u]", name, t2.first, t2.second, t1.first, t1.second);
83a49336
ML
134 return false;
135 }
cdc3b883
ML
136 }
137
a30d4fc5
ML
138 original_limits[index] = limits[index].copy ();
139
83a49336
ML
140 return true;
141}
142
143static bool
144dbg_cnt_set_limit_by_name (const char *name, unsigned int low,
145 unsigned int high)
146{
147 if (high < low)
cdc3b883 148 {
83a49336
ML
149 error ("%<-fdbg-cnt=%s:%d-%d%> has smaller upper limit than the lower",
150 name, low, high);
cdc3b883
ML
151 return false;
152 }
153
6fb5fa3c
DB
154 int i;
155 for (i = debug_counter_number_of_counters - 1; i >= 0; i--)
cdc3b883 156 if (strcmp (map[i].name, name) == 0)
6fb5fa3c
DB
157 break;
158
159 if (i < 0)
342ae9ad
ML
160 {
161 error ("cannot find a valid counter name %qs of %<-fdbg-cnt=%> option",
162 name);
163 return false;
164 }
6fb5fa3c 165
83a49336 166 return dbg_cnt_set_limit_by_index ((enum debug_counter) i, name, low, high);
6fb5fa3c
DB
167}
168
83a49336 169/* Process a single "low:high" pair.
0a090f42
SP
170 Returns NULL if there's no valid pair is found.
171 Otherwise returns a pointer to the end of the pair. */
172
cdc3b883 173static bool
83a49336 174dbg_cnt_process_single_pair (char *name, char *str)
6fb5fa3c 175{
83a49336
ML
176 char *value1 = strtok (str, "-");
177 char *value2 = strtok (NULL, "-");
cdc3b883 178
83a49336 179 unsigned int high, low;
cdc3b883
ML
180
181 if (value1 == NULL)
5433e401 182 return false;
cdc3b883
ML
183
184 if (value2 == NULL)
185 {
cdc3b883 186 high = strtol (value1, NULL, 10);
342ae9ad
ML
187 /* Let's allow 0:0. */
188 low = high == 0 ? 0 : 1;
cdc3b883
ML
189 }
190 else
191 {
192 low = strtol (value1, NULL, 10);
193 high = strtol (value2, NULL, 10);
194 }
195
2a7108b9 196 return dbg_cnt_set_limit_by_name (name, low, high);
0a090f42 197}
6fb5fa3c 198
0a090f42
SP
199void
200dbg_cnt_process_opt (const char *arg)
201{
cdc3b883 202 char *str = xstrdup (arg);
cdc3b883
ML
203 unsigned int start = 0;
204
83a49336
ML
205 auto_vec<char *> tokens;
206 for (char *next = strtok (str, ","); next != NULL; next = strtok (NULL, ","))
2a7108b9
ML
207 tokens.safe_push (next);
208
209 unsigned i;
210 for (i = 0; i < tokens.length (); i++)
211 {
83a49336
ML
212 auto_vec<char *> ranges;
213 char *name = strtok (tokens[i], ":");
214 for (char *part = strtok (NULL, ":"); part; part = strtok (NULL, ":"))
215 ranges.safe_push (part);
216
217 for (unsigned j = 0; j < ranges.length (); j++)
218 {
219 if (!dbg_cnt_process_single_pair (name, ranges[j]))
220 break;
221 }
222 start += strlen (tokens[i]) + 1;
2a7108b9 223 }
6fb5fa3c 224}
0a090f42
SP
225
226/* Print name, limit and count of all counters. */
227
b8698a0f 228void
cc806ac1 229dbg_cnt_list_all_counters (void)
0a090f42
SP
230{
231 int i;
a30d4fc5
ML
232 fprintf (stderr, " %-30s%-15s %s\n", G_("counter name"),
233 G_("counter value"), G_("closed intervals"));
234 fprintf (stderr, "-----------------------------------------------------------------\n");
0a090f42 235 for (i = 0; i < debug_counter_number_of_counters; i++)
83a49336 236 {
a30d4fc5
ML
237 fprintf (stderr, " %-30s%-15d ", map[i].name, count[i]);
238 if (original_limits[i].exists ())
83a49336 239 {
a30d4fc5 240 for (int j = original_limits[i].length () - 1; j >= 0; j--)
83a49336 241 {
a30d4fc5
ML
242 fprintf (stderr, "[%u, %u]", original_limits[i][j].first,
243 original_limits[i][j].second);
83a49336 244 if (j > 0)
a30d4fc5 245 fprintf (stderr, ", ");
83a49336 246 }
a30d4fc5 247 fprintf (stderr, "\n");
83a49336
ML
248 }
249 else
a30d4fc5 250 fprintf (stderr, "unset\n");
83a49336 251 }
a30d4fc5 252 fprintf (stderr, "\n");
0a090f42 253}
5024c8bb
ML
254
255#if CHECKING_P
256
257namespace selftest {
258
259/* Selftests. */
260
261static void
262test_sorted_dbg_counters ()
263{
264 for (unsigned i = 0; i < debug_counter_number_of_counters - 1; i++)
265 ASSERT_LT (strcmp (map[i].name, map[i + 1].name), 0);
266}
267
268void
269dbgcnt_c_tests ()
270{
271 test_sorted_dbg_counters ();
272}
273
274} // namespace selftest
275
276#endif /* #if CHECKING_P */