summaryrefslogtreecommitdiff
path: root/gcc/dbgcnt.c
blob: 1eea65889a8cb5f3e2a104463819a8912ddf0616 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* Debug counter for debugging support
   Copyright (C) 2006-2018 Free Software Foundation, Inc.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.

See dbgcnt.def for usage information.  */

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "diagnostic-core.h"
#include "dumpfile.h"

#include "dbgcnt.h"

struct string2counter_map {
  const char *name;
  enum debug_counter counter;
};

#define DEBUG_COUNTER(a) { #a , a },

static struct string2counter_map map[debug_counter_number_of_counters] =
{
#include "dbgcnt.def"
};
#undef DEBUG_COUNTER

#define DEBUG_COUNTER(a) UINT_MAX,
static unsigned int limit_high[debug_counter_number_of_counters] =
{
#include "dbgcnt.def"
};
#undef DEBUG_COUNTER

static unsigned int limit_low[debug_counter_number_of_counters];

static unsigned int count[debug_counter_number_of_counters];

bool
dbg_cnt_is_enabled (enum debug_counter index)
{
  unsigned v = count[index];
  return v > limit_low[index] && v <= limit_high[index];
}

bool
dbg_cnt (enum debug_counter index)
{
  count[index]++;

  if (dump_file)
    {
      /* Do not print the info for default lower limit.  */
      if (count[index] == limit_low[index] && limit_low[index] > 0)
	fprintf (dump_file, "***dbgcnt: lower limit %d reached for %s.***\n",
		 limit_low[index], map[index].name);
      else if (count[index] == limit_high[index])
	fprintf (dump_file, "***dbgcnt: upper limit %d reached for %s.***\n",
		 limit_high[index], map[index].name);
    }

  return dbg_cnt_is_enabled (index);
}

static void
dbg_cnt_set_limit_by_index (enum debug_counter index, int low, int high)
{
  limit_low[index] = low;
  limit_high[index] = high;

  fprintf (stderr, "dbg_cnt '%s' set to %d-%d\n", map[index].name, low, high);
}

static bool
dbg_cnt_set_limit_by_name (const char *name, int low, int high)
{
  if (high < low)
    {
      error ("-fdbg-cnt=%s:%d:%d has smaller upper limit than the lower",
	     name, low, high);
      return false;
    }

  if (low < 0)
    {
      error ("Lower limit %d of -fdbg-cnt=%s must be a non-negative number", low,
	     name);
      return false;
    }

  if (high < 0)
    {
      error ("Upper limit %d of -fdbg-cnt=%s must be a non-negative number", high,
	     name);
      return false;
    }

  int i;
  for (i = debug_counter_number_of_counters - 1; i >= 0; i--)
    if (strcmp (map[i].name, name) == 0)
      break;

  if (i < 0)
    return false;

  dbg_cnt_set_limit_by_index ((enum debug_counter) i, low, high);
  return true;
}


/* Process a single "name:value" pair.
   Returns NULL if there's no valid pair is found.
   Otherwise returns a pointer to the end of the pair. */

static bool
dbg_cnt_process_single_pair (const char *arg)
{
  char *str = xstrdup (arg);
  char *name = strtok (str, ":");
  char *value1 = strtok (NULL, ":");
  char *value2 = strtok (NULL, ":");

  int high, low;

  if (value1 == NULL)
    return false;

  if (value2 == NULL)
    {
      low = 0;
      high = strtol (value1, NULL, 10);
    }
  else
    {
      low = strtol (value1, NULL, 10);
      high = strtol (value2, NULL, 10);
    }

   return dbg_cnt_set_limit_by_name (name, low, high);
}

void
dbg_cnt_process_opt (const char *arg)
{
  char *str = xstrdup (arg);
  const char *next = strtok (str, ",");
  unsigned int start = 0;

   do {
     if (!dbg_cnt_process_single_pair (arg))
       break;
     start += strlen (arg) + 1;
     next = strtok (NULL, ",");
   } while (next != NULL);

   if (next != NULL)
     {
       char *buffer = XALLOCAVEC (char, start + 2);
       sprintf (buffer, "%*c", start + 1, '^');
       error ("cannot find a valid counter:value pair:");
       error ("-fdbg-cnt=%s", next);
       error ("          %s", buffer);
     }
}

/* Print name, limit and count of all counters.   */

void
dbg_cnt_list_all_counters (void)
{
  int i;
  printf ("  %-32s %-11s %-12s\n", "counter name",  "low limit",
	  "high limit");
  printf ("-----------------------------------------------------------------\n");
  for (i = 0; i < debug_counter_number_of_counters; i++)
    printf ("  %-30s %11u %12u\n",
	    map[i].name, limit_low[map[i].counter], limit_high[map[i].counter]);
  printf ("\n");
}