1 /* Debug counter for debugging support
2 Copyright (C) 2006-2019 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"
31 struct string2counter_map
{
33 enum debug_counter counter
;
36 #define DEBUG_COUNTER(a) { #a , a },
38 static struct string2counter_map map
[debug_counter_number_of_counters
] =
44 typedef std::pair
<unsigned int, unsigned int> limit_tuple
;
46 static vec
<limit_tuple
> limits
[debug_counter_number_of_counters
];
48 static unsigned int count
[debug_counter_number_of_counters
];
51 print_limit_reach (const char *counter
, int limit
, bool upper_p
)
54 sprintf (buffer
, "***dbgcnt: %s limit %d reached for %s.***\n",
55 upper_p
? "upper" : "lower", limit
, counter
);
56 fputs (buffer
, stderr
);
58 fputs (buffer
, dump_file
);
62 dbg_cnt (enum debug_counter index
)
64 unsigned v
= ++count
[index
];
66 if (!limits
[index
].exists ())
68 else if (limits
[index
].is_empty ())
71 unsigned last
= limits
[index
].length () - 1;
72 unsigned int min
= limits
[index
][last
].first
;
73 unsigned int max
= limits
[index
][last
].second
;
79 print_limit_reach (map
[index
].name
, v
, false);
88 print_limit_reach (map
[index
].name
, v
, true);
96 /* Compare limit_tuple intervals by first item in descending order. */
99 cmp_tuples (const void *ptr1
, const void *ptr2
)
101 const limit_tuple
*p1
= (const limit_tuple
*)ptr1
;
102 const limit_tuple
*p2
= (const limit_tuple
*)ptr2
;
104 if (p1
->first
< p2
->first
)
106 else if (p1
->first
> p2
->first
)
112 dbg_cnt_set_limit_by_index (enum debug_counter index
, const char *name
,
113 unsigned int low
, unsigned int high
)
115 if (!limits
[index
].exists ())
116 limits
[index
].create (1);
118 limits
[index
].safe_push (limit_tuple (low
, high
));
119 limits
[index
].qsort (cmp_tuples
);
121 for (unsigned i
= 0; i
< limits
[index
].length () - 1; i
++)
123 limit_tuple t1
= limits
[index
][i
];
124 limit_tuple t2
= limits
[index
][i
+ 1];
125 if (t1
.first
<= t2
.second
)
127 error ("Interval overlap of %<-fdbg-cnt=%s%>: [%u, %u] and "
128 "[%u, %u]\n", name
, t2
.first
, t2
.second
, t1
.first
, t1
.second
);
137 dbg_cnt_set_limit_by_name (const char *name
, unsigned int low
,
142 error ("%<-fdbg-cnt=%s:%d-%d%> has smaller upper limit than the lower",
148 for (i
= debug_counter_number_of_counters
- 1; i
>= 0; i
--)
149 if (strcmp (map
[i
].name
, name
) == 0)
155 return dbg_cnt_set_limit_by_index ((enum debug_counter
) i
, name
, low
, high
);
158 /* Process a single "low:high" pair.
159 Returns NULL if there's no valid pair is found.
160 Otherwise returns a pointer to the end of the pair. */
163 dbg_cnt_process_single_pair (char *name
, char *str
)
165 char *value1
= strtok (str
, "-");
166 char *value2
= strtok (NULL
, "-");
168 unsigned int high
, low
;
176 high
= strtol (value1
, NULL
, 10);
180 low
= strtol (value1
, NULL
, 10);
181 high
= strtol (value2
, NULL
, 10);
184 return dbg_cnt_set_limit_by_name (name
, low
, high
);
188 dbg_cnt_process_opt (const char *arg
)
190 char *str
= xstrdup (arg
);
191 unsigned int start
= 0;
193 auto_vec
<char *> tokens
;
194 for (char *next
= strtok (str
, ","); next
!= NULL
; next
= strtok (NULL
, ","))
195 tokens
.safe_push (next
);
198 for (i
= 0; i
< tokens
.length (); i
++)
200 auto_vec
<char *> ranges
;
201 char *name
= strtok (tokens
[i
], ":");
202 for (char *part
= strtok (NULL
, ":"); part
; part
= strtok (NULL
, ":"))
203 ranges
.safe_push (part
);
205 for (unsigned j
= 0; j
< ranges
.length (); j
++)
207 if (!dbg_cnt_process_single_pair (name
, ranges
[j
]))
210 start
+= strlen (tokens
[i
]) + 1;
213 if (i
!= tokens
.length ())
215 char *buffer
= XALLOCAVEC (char, start
+ 2);
216 sprintf (buffer
, "%*c", start
+ 1, '^');
217 error ("cannot find a valid counter:value pair:");
218 error ("%<-fdbg-cnt=%s%>", arg
);
219 error (" %s", buffer
);
223 /* Print name, limit and count of all counters. */
226 dbg_cnt_list_all_counters (void)
229 printf (" %-30s %s\n", "counter name", "closed intervals");
230 printf ("-----------------------------------------------------------------\n");
231 for (i
= 0; i
< debug_counter_number_of_counters
; i
++)
233 printf (" %-30s ", map
[i
].name
);
234 if (limits
[i
].exists ())
236 for (int j
= limits
[i
].length () - 1; j
>= 0; j
--)
238 printf ("[%u, %u]", limits
[i
][j
].first
, limits
[i
][j
].second
);
257 test_sorted_dbg_counters ()
259 for (unsigned i
= 0; i
< debug_counter_number_of_counters
- 1; i
++)
260 ASSERT_LT (strcmp (map
[i
].name
, map
[i
+ 1].name
), 0);
266 test_sorted_dbg_counters ();
269 } // namespace selftest
271 #endif /* #if CHECKING_P */