OSDN Git Service

* config/pa/fptr.c: Update license header.
[pf3gnuchains/gcc-fork.git] / gcc / dbgcnt.c
1 /* Debug counter for debugging support
2    Copyright (C) 2006, 2007 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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 2, or (at your option) any later
9 version.
10
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
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING.  If not, write to the Free
18 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301, USA.  
20
21 See dbgcnt.def for usage information.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "errors.h"
27
28 #include "dbgcnt.h"
29
30 struct string2counter_map {
31   const char *name;
32   enum debug_counter counter;
33 };
34
35 #define DEBUG_COUNTER(a) { #a , a },
36
37 static struct string2counter_map map[debug_counter_number_of_counters] =
38 {
39 #include "dbgcnt.def"
40 };
41 #undef DEBUG_COUNTER
42
43 #define DEBUG_COUNTER(a) UINT_MAX,
44 static unsigned int limit[debug_counter_number_of_counters] =
45 {
46 #include "dbgcnt.def"
47 };
48 #undef DEBUG_COUNTER
49
50 static unsigned int count[debug_counter_number_of_counters];
51
52 bool
53 dbg_cnt_is_enabled (enum debug_counter index)
54 {
55   return count[index] <= limit[index];
56 }
57
58 bool
59 dbg_cnt (enum debug_counter index)
60 {
61   count[index]++;
62   return dbg_cnt_is_enabled (index);
63 }
64
65
66 static void
67 dbg_cnt_set_limit_by_index (enum debug_counter index, int value)
68 {
69   limit[index] = value;
70
71   fprintf (stderr, "dbg_cnt '%s' set to %d\n", map[index].name, value);
72 }
73
74 static bool
75 dbg_cnt_set_limit_by_name (const char *name, int len, int value)
76 {
77   int i;
78   for (i = debug_counter_number_of_counters - 1; i >= 0; i--)
79     if (!strncmp (map[i].name, name, len))
80       break;
81
82   if (i < 0)
83     return false;
84
85   dbg_cnt_set_limit_by_index (i, value);
86   return true;
87 }
88
89
90 /* Process a single "name:value" pair.
91    Returns NULL if there's no valid pair is found.
92    Otherwise returns a pointer to the end of the pair. */
93
94 static const char *
95 dbg_cnt_process_single_pair (const char *arg)
96 {
97    char *colon = strchr (arg, ':');
98    char *endptr = NULL;
99    int value;
100    
101    if (colon == NULL)
102      return NULL;
103
104    value = strtol (colon + 1, &endptr, 10);
105
106    if (endptr != NULL && endptr != colon + 1
107        && dbg_cnt_set_limit_by_name (arg, colon - arg, value))
108      return endptr;
109    
110    return NULL;
111 }
112
113 void
114 dbg_cnt_process_opt (const char *arg)
115 {
116    const char *start = arg;
117    const char *next;
118    do {
119      next = dbg_cnt_process_single_pair (arg);
120      if (next == NULL)
121        break;
122    } while (*next == ',' && (arg = next + 1));
123
124    if (next == NULL || *next != 0)
125      {
126        char *buffer = alloca (arg - start + 2);
127        sprintf (buffer, "%*c", (int)(1 + (arg - start)), '^');
128        error ("Can not find a valid counter:value pair:");
129        error ("-fdbg-cnt=%s", start);
130        error ("          %s", buffer);
131      }
132 }
133
134 /* Print name, limit and count of all counters.   */
135
136 void dbg_cnt_list_all_counters (void)
137 {
138   int i;
139   printf ("  %-30s %-5s %-5s\n", "counter name",  "limit", "value");
140   printf ("----------------------------------------------\n");
141   for (i = 0; i < debug_counter_number_of_counters; i++)
142     printf ("  %-30s %5d %5u\n",
143             map[i].name, limit[map[i].counter], count[map[i].counter]);
144   printf ("\n");
145 }