1*38fd1498Szrj /* Debug counter for debugging support
2*38fd1498Szrj Copyright (C) 2006-2018 Free Software Foundation, Inc.
3*38fd1498Szrj
4*38fd1498Szrj This file is part of GCC.
5*38fd1498Szrj
6*38fd1498Szrj GCC is free software; you can redistribute it and/or modify it under
7*38fd1498Szrj the terms of the GNU General Public License as published by the Free
8*38fd1498Szrj Software Foundation; either version 3, or (at your option) any later
9*38fd1498Szrj version.
10*38fd1498Szrj
11*38fd1498Szrj GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12*38fd1498Szrj WARRANTY; without even the implied warranty of MERCHANTABILITY or
13*38fd1498Szrj FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14*38fd1498Szrj for more details.
15*38fd1498Szrj
16*38fd1498Szrj You should have received a copy of the GNU General Public License
17*38fd1498Szrj along with GCC; see the file COPYING3. If not see
18*38fd1498Szrj <http://www.gnu.org/licenses/>.
19*38fd1498Szrj
20*38fd1498Szrj See dbgcnt.def for usage information. */
21*38fd1498Szrj
22*38fd1498Szrj #include "config.h"
23*38fd1498Szrj #include "system.h"
24*38fd1498Szrj #include "coretypes.h"
25*38fd1498Szrj #include "diagnostic-core.h"
26*38fd1498Szrj #include "dumpfile.h"
27*38fd1498Szrj
28*38fd1498Szrj #include "dbgcnt.h"
29*38fd1498Szrj
30*38fd1498Szrj struct string2counter_map {
31*38fd1498Szrj const char *name;
32*38fd1498Szrj enum debug_counter counter;
33*38fd1498Szrj };
34*38fd1498Szrj
35*38fd1498Szrj #define DEBUG_COUNTER(a) { #a , a },
36*38fd1498Szrj
37*38fd1498Szrj static struct string2counter_map map[debug_counter_number_of_counters] =
38*38fd1498Szrj {
39*38fd1498Szrj #include "dbgcnt.def"
40*38fd1498Szrj };
41*38fd1498Szrj #undef DEBUG_COUNTER
42*38fd1498Szrj
43*38fd1498Szrj #define DEBUG_COUNTER(a) UINT_MAX,
44*38fd1498Szrj static unsigned int limit[debug_counter_number_of_counters] =
45*38fd1498Szrj {
46*38fd1498Szrj #include "dbgcnt.def"
47*38fd1498Szrj };
48*38fd1498Szrj #undef DEBUG_COUNTER
49*38fd1498Szrj
50*38fd1498Szrj static unsigned int count[debug_counter_number_of_counters];
51*38fd1498Szrj
52*38fd1498Szrj bool
dbg_cnt_is_enabled(enum debug_counter index)53*38fd1498Szrj dbg_cnt_is_enabled (enum debug_counter index)
54*38fd1498Szrj {
55*38fd1498Szrj return count[index] <= limit[index];
56*38fd1498Szrj }
57*38fd1498Szrj
58*38fd1498Szrj bool
dbg_cnt(enum debug_counter index)59*38fd1498Szrj dbg_cnt (enum debug_counter index)
60*38fd1498Szrj {
61*38fd1498Szrj count[index]++;
62*38fd1498Szrj if (dump_file && count[index] == limit[index])
63*38fd1498Szrj fprintf (dump_file, "***dbgcnt: limit reached for %s.***\n",
64*38fd1498Szrj map[index].name);
65*38fd1498Szrj
66*38fd1498Szrj return dbg_cnt_is_enabled (index);
67*38fd1498Szrj }
68*38fd1498Szrj
69*38fd1498Szrj
70*38fd1498Szrj static void
dbg_cnt_set_limit_by_index(enum debug_counter index,int value)71*38fd1498Szrj dbg_cnt_set_limit_by_index (enum debug_counter index, int value)
72*38fd1498Szrj {
73*38fd1498Szrj limit[index] = value;
74*38fd1498Szrj
75*38fd1498Szrj fprintf (stderr, "dbg_cnt '%s' set to %d\n", map[index].name, value);
76*38fd1498Szrj }
77*38fd1498Szrj
78*38fd1498Szrj static bool
dbg_cnt_set_limit_by_name(const char * name,int len,int value)79*38fd1498Szrj dbg_cnt_set_limit_by_name (const char *name, int len, int value)
80*38fd1498Szrj {
81*38fd1498Szrj int i;
82*38fd1498Szrj for (i = debug_counter_number_of_counters - 1; i >= 0; i--)
83*38fd1498Szrj if (strncmp (map[i].name, name, len) == 0
84*38fd1498Szrj && map[i].name[len] == '\0')
85*38fd1498Szrj break;
86*38fd1498Szrj
87*38fd1498Szrj if (i < 0)
88*38fd1498Szrj return false;
89*38fd1498Szrj
90*38fd1498Szrj dbg_cnt_set_limit_by_index ((enum debug_counter) i, value);
91*38fd1498Szrj return true;
92*38fd1498Szrj }
93*38fd1498Szrj
94*38fd1498Szrj
95*38fd1498Szrj /* Process a single "name:value" pair.
96*38fd1498Szrj Returns NULL if there's no valid pair is found.
97*38fd1498Szrj Otherwise returns a pointer to the end of the pair. */
98*38fd1498Szrj
99*38fd1498Szrj static const char *
dbg_cnt_process_single_pair(const char * arg)100*38fd1498Szrj dbg_cnt_process_single_pair (const char *arg)
101*38fd1498Szrj {
102*38fd1498Szrj const char *colon = strchr (arg, ':');
103*38fd1498Szrj char *endptr = NULL;
104*38fd1498Szrj int value;
105*38fd1498Szrj
106*38fd1498Szrj if (colon == NULL)
107*38fd1498Szrj return NULL;
108*38fd1498Szrj
109*38fd1498Szrj value = strtol (colon + 1, &endptr, 10);
110*38fd1498Szrj
111*38fd1498Szrj if (endptr != NULL && endptr != colon + 1
112*38fd1498Szrj && dbg_cnt_set_limit_by_name (arg, colon - arg, value))
113*38fd1498Szrj return endptr;
114*38fd1498Szrj
115*38fd1498Szrj return NULL;
116*38fd1498Szrj }
117*38fd1498Szrj
118*38fd1498Szrj void
dbg_cnt_process_opt(const char * arg)119*38fd1498Szrj dbg_cnt_process_opt (const char *arg)
120*38fd1498Szrj {
121*38fd1498Szrj const char *start = arg;
122*38fd1498Szrj const char *next;
123*38fd1498Szrj do {
124*38fd1498Szrj next = dbg_cnt_process_single_pair (arg);
125*38fd1498Szrj if (next == NULL)
126*38fd1498Szrj break;
127*38fd1498Szrj } while (*next == ',' && (arg = next + 1));
128*38fd1498Szrj
129*38fd1498Szrj if (next == NULL || *next != 0)
130*38fd1498Szrj {
131*38fd1498Szrj char *buffer = XALLOCAVEC (char, arg - start + 2);
132*38fd1498Szrj sprintf (buffer, "%*c", (int)(1 + (arg - start)), '^');
133*38fd1498Szrj error ("cannot find a valid counter:value pair:");
134*38fd1498Szrj error ("-fdbg-cnt=%s", start);
135*38fd1498Szrj error (" %s", buffer);
136*38fd1498Szrj }
137*38fd1498Szrj }
138*38fd1498Szrj
139*38fd1498Szrj /* Print name, limit and count of all counters. */
140*38fd1498Szrj
141*38fd1498Szrj void
dbg_cnt_list_all_counters(void)142*38fd1498Szrj dbg_cnt_list_all_counters (void)
143*38fd1498Szrj {
144*38fd1498Szrj int i;
145*38fd1498Szrj printf (" %-30s %-5s %-5s\n", "counter name", "limit", "value");
146*38fd1498Szrj printf ("----------------------------------------------\n");
147*38fd1498Szrj for (i = 0; i < debug_counter_number_of_counters; i++)
148*38fd1498Szrj printf (" %-30s %5d %5u\n",
149*38fd1498Szrj map[i].name, limit[map[i].counter], count[map[i].counter]);
150*38fd1498Szrj printf ("\n");
151*38fd1498Szrj }
152