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
];
48 static vec
<limit_tuple
> original_limits
[debug_counter_number_of_counters
];
50 static unsigned int count
[debug_counter_number_of_counters
];
53 print_limit_reach (const char *counter
, int limit
, bool upper_p
)
56 sprintf (buffer
, "***dbgcnt: %s limit %d reached for %s.***\n",
57 upper_p
? "upper" : "lower", limit
, counter
);
58 fputs (buffer
, stderr
);
60 fputs (buffer
, dump_file
);
64 dbg_cnt (enum debug_counter index
)
66 unsigned v
= ++count
[index
];
68 if (!limits
[index
].exists ())
70 else if (limits
[index
].is_empty ())
73 unsigned last
= limits
[index
].length () - 1;
74 unsigned int min
= limits
[index
][last
].first
;
75 unsigned int max
= limits
[index
][last
].second
;
81 print_limit_reach (map
[index
].name
, v
, false);
84 print_limit_reach (map
[index
].name
, v
, true);
93 print_limit_reach (map
[index
].name
, v
, true);
101 /* Compare limit_tuple intervals by first item in descending order. */
104 cmp_tuples (const void *ptr1
, const void *ptr2
)
106 const limit_tuple
*p1
= (const limit_tuple
*)ptr1
;
107 const limit_tuple
*p2
= (const limit_tuple
*)ptr2
;
109 if (p1
->first
< p2
->first
)
111 else if (p1
->first
> p2
->first
)
117 dbg_cnt_set_limit_by_index (enum debug_counter index
, const char *name
,
118 unsigned int low
, unsigned int high
)
120 if (!limits
[index
].exists ())
121 limits
[index
].create (1);
123 limits
[index
].safe_push (limit_tuple (low
, high
));
124 limits
[index
].qsort (cmp_tuples
);
126 for (unsigned i
= 0; i
< limits
[index
].length () - 1; i
++)
128 limit_tuple t1
= limits
[index
][i
];
129 limit_tuple t2
= limits
[index
][i
+ 1];
130 if (t1
.first
<= t2
.second
)
132 error ("Interval overlap of %<-fdbg-cnt=%s%>: [%u, %u] and "
133 "[%u, %u]", name
, t2
.first
, t2
.second
, t1
.first
, t1
.second
);
138 original_limits
[index
] = limits
[index
].copy ();
144 dbg_cnt_set_limit_by_name (const char *name
, unsigned int low
,
149 error ("%<-fdbg-cnt=%s:%d-%d%> has smaller upper limit than the lower",
155 for (i
= debug_counter_number_of_counters
- 1; i
>= 0; i
--)
156 if (strcmp (map
[i
].name
, name
) == 0)
161 error ("cannot find a valid counter name %qs of %<-fdbg-cnt=%> option",
166 return dbg_cnt_set_limit_by_index ((enum debug_counter
) i
, name
, low
, high
);
169 /* Process a single "low:high" pair.
170 Returns NULL if there's no valid pair is found.
171 Otherwise returns a pointer to the end of the pair. */
174 dbg_cnt_process_single_pair (char *name
, char *str
)
176 char *value1
= strtok (str
, "-");
177 char *value2
= strtok (NULL
, "-");
179 unsigned int high
, low
;
186 high
= strtol (value1
, NULL
, 10);
187 /* Let's allow 0:0. */
188 low
= high
== 0 ? 0 : 1;
192 low
= strtol (value1
, NULL
, 10);
193 high
= strtol (value2
, NULL
, 10);
196 return dbg_cnt_set_limit_by_name (name
, low
, high
);
200 dbg_cnt_process_opt (const char *arg
)
202 char *str
= xstrdup (arg
);
203 unsigned int start
= 0;
205 auto_vec
<char *> tokens
;
206 for (char *next
= strtok (str
, ","); next
!= NULL
; next
= strtok (NULL
, ","))
207 tokens
.safe_push (next
);
210 for (i
= 0; i
< tokens
.length (); i
++)
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
);
217 for (unsigned j
= 0; j
< ranges
.length (); j
++)
219 if (!dbg_cnt_process_single_pair (name
, ranges
[j
]))
222 start
+= strlen (tokens
[i
]) + 1;
226 /* Print name, limit and count of all counters. */
229 dbg_cnt_list_all_counters (void)
232 fprintf (stderr
, " %-30s%-15s %s\n", G_("counter name"),
233 G_("counter value"), G_("closed intervals"));
234 fprintf (stderr
, "-----------------------------------------------------------------\n");
235 for (i
= 0; i
< debug_counter_number_of_counters
; i
++)
237 fprintf (stderr
, " %-30s%-15d ", map
[i
].name
, count
[i
]);
238 if (original_limits
[i
].exists ())
240 for (int j
= original_limits
[i
].length () - 1; j
>= 0; j
--)
242 fprintf (stderr
, "[%u, %u]", original_limits
[i
][j
].first
,
243 original_limits
[i
][j
].second
);
245 fprintf (stderr
, ", ");
247 fprintf (stderr
, "\n");
250 fprintf (stderr
, "unset\n");
252 fprintf (stderr
, "\n");
262 test_sorted_dbg_counters ()
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);
271 test_sorted_dbg_counters ();
274 } // namespace selftest
276 #endif /* #if CHECKING_P */