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