ruby: Migrate all of ruby and slicc to SCons.
[gem5.git] / src / mem / ruby / common / Debug.hh
1
2 /*
3 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 /*
31 * $Id$
32 */
33
34 #ifndef DEBUG_H
35 #define DEBUG_H
36
37 #include <unistd.h>
38 #include <iostream>
39
40 #include "config/ruby_debug.hh"
41
42 extern std::ostream * debug_cout_ptr;
43
44 // component enumeration
45 enum DebugComponents
46 {
47 SYSTEM_COMP,
48 NODE_COMP,
49 QUEUE_COMP,
50 EVENTQUEUE_COMP,
51 NETWORK_COMP,
52 SEQUENCER_COMP,
53 TESTER_COMP,
54 GENERATED_COMP,
55 SLICC_COMP,
56 NETWORKQUEUE_COMP,
57 TIME_COMP,
58 NETWORK_INTERNALS_COMP,
59 STOREBUFFER_COMP,
60 CACHE_COMP,
61 PREDICTOR_COMP,
62 ALLOCATOR_COMP,
63 NUMBER_OF_COMPS
64 };
65
66 enum PriorityLevel {HighPrio, MedPrio, LowPrio};
67 enum VerbosityLevel {No_Verb, Low_Verb, Med_Verb, High_Verb};
68
69 class Debug {
70 public:
71 // Constructors
72 Debug( const char *filterString, const char *verboseString,
73 Time filterStartTime, const char *filename );
74
75 // Destructor
76 ~Debug();
77
78 // Public Methods
79 bool validDebug(int module, PriorityLevel priority);
80 void printVerbosity(ostream& out) const;
81 void setVerbosity(VerbosityLevel vb);
82 static bool checkVerbosityString(const char *verb_str);
83 bool setVerbosityString(const char *);
84 VerbosityLevel getVerbosity() const { return m_verbosityLevel; }
85 void setFilter(int);
86 static bool checkFilter( char);
87 static bool checkFilterString(const char *);
88 bool setFilterString(const char *);
89 void setDebugTime(Time);
90 Time getDebugTime() const { return m_starting_cycle; }
91 bool addFilter(char);
92 void clearFilter();
93 void allFilter();
94 void print(ostream& out) const;
95 /* old school debugging "vararg": sends messages to screen and log */
96 void debugMsg( const char *fmt, ... );
97
98 void setDebugOutputFile (const char * filename);
99 void closeDebugOutputFile ();
100 static void usageInstructions(void);
101
102 private:
103 // Private Methods
104
105 // Private copy constructor and assignment operator
106 Debug(const Debug& obj);
107 Debug& operator=(const Debug& obj);
108
109 // Data Members (m_ prefix)
110 VerbosityLevel m_verbosityLevel;
111 int m_filter;
112 Time m_starting_cycle;
113
114 std::fstream m_fout;
115 };
116
117 // Output operator declaration
118 ostream& operator<<(ostream& out, const Debug& obj);
119
120 // ******************* Definitions *******************
121
122 // Output operator definition
123 extern inline
124 ostream& operator<<(ostream& out, const Debug& obj)
125 {
126 obj.print(out);
127 out << flush;
128 return out;
129 }
130
131 const bool ERROR_MESSAGE_FLAG = true;
132 const bool WARNING_MESSAGE_FLAG = true;
133
134 #ifdef RUBY_NO_ASSERT
135 const bool ASSERT_FLAG = false;
136 #else
137 const bool ASSERT_FLAG = true;
138 #endif
139
140 #undef assert
141 #define assert(EXPR) ASSERT(EXPR)
142 #undef ASSERT
143 #define ASSERT(EXPR)\
144 {\
145 if (ASSERT_FLAG) {\
146 if (!(EXPR)) {\
147 cerr << "failed assertion '"\
148 << #EXPR << "' at fn "\
149 << __PRETTY_FUNCTION__ << " in "\
150 << __FILE__ << ":"\
151 << __LINE__ << endl << flush;\
152 (* debug_cout_ptr) << "failed assertion '"\
153 << #EXPR << "' at fn "\
154 << __PRETTY_FUNCTION__ << " in "\
155 << __FILE__ << ":"\
156 << __LINE__ << endl << flush;\
157 if(isatty(STDIN_FILENO)) {\
158 cerr << "At this point you might want to attach a debug to ";\
159 cerr << "the running and get to the" << endl;\
160 cerr << "crash site; otherwise press enter to continue" << endl;\
161 cerr << "PID: " << getpid();\
162 cerr << endl << flush; \
163 char c; \
164 cin.get(c); \
165 }\
166 abort();\
167 }\
168 }\
169 }
170
171 #define BREAK(X)\
172 {\
173 cerr << "breakpoint '"\
174 << #X << "' reached at fn "\
175 << __PRETTY_FUNCTION__ << " in "\
176 << __FILE__ << ":"\
177 << __LINE__ << endl << flush;\
178 if(isatty(STDIN_FILENO)) {\
179 cerr << "press enter to continue" << endl;\
180 cerr << "PID: " << getpid();\
181 cerr << endl << flush; \
182 char c; \
183 cin.get(c); \
184 }\
185 }
186
187 #define ERROR_MSG(MESSAGE)\
188 {\
189 if (ERROR_MESSAGE_FLAG) {\
190 cerr << "Fatal Error: in fn "\
191 << __PRETTY_FUNCTION__ << " in "\
192 << __FILE__ << ":"\
193 << __LINE__ << ": "\
194 << (MESSAGE) << endl << flush;\
195 (* debug_cout_ptr) << "Fatal Error: in fn "\
196 << __PRETTY_FUNCTION__ << " in "\
197 << __FILE__ << ":"\
198 << __LINE__ << ": "\
199 << (MESSAGE) << endl << flush;\
200 abort();\
201 }\
202 }
203
204 #define WARN_MSG(MESSAGE)\
205 {\
206 if (WARNING_MESSAGE_FLAG) {\
207 cerr << "Warning: in fn "\
208 << __PRETTY_FUNCTION__ << " in "\
209 << __FILE__ << ":"\
210 << __LINE__ << ": "\
211 << (MESSAGE) << endl << flush;\
212 (* debug_cout_ptr) << "Warning: in fn "\
213 << __PRETTY_FUNCTION__ << " in "\
214 << __FILE__ << ":"\
215 << __LINE__ << ": "\
216 << (MESSAGE) << endl << flush;\
217 }\
218 }
219
220 #define WARN_EXPR(EXPR)\
221 {\
222 if (WARNING_MESSAGE_FLAG) {\
223 cerr << "Warning: in fn "\
224 << __PRETTY_FUNCTION__ << " in "\
225 << __FILE__ << ":"\
226 << __LINE__ << ": "\
227 << #EXPR << " is "\
228 << (EXPR) << endl << flush;\
229 (* debug_cout_ptr) << "Warning: in fn "\
230 << __PRETTY_FUNCTION__ << " in "\
231 << __FILE__ << ":"\
232 << __LINE__ << ": "\
233 << #EXPR << " is "\
234 << (EXPR) << endl << flush;\
235 }\
236 }
237
238 #define DEBUG_MSG(module, priority, MESSAGE)\
239 {\
240 if (RUBY_DEBUG) {\
241 if (g_debug_ptr->validDebug(module, priority)) {\
242 (* debug_cout_ptr) << "Debug: in fn "\
243 << __PRETTY_FUNCTION__\
244 << " in " << __FILE__ << ":"\
245 << __LINE__ << ": "\
246 << (MESSAGE) << endl << flush;\
247 }\
248 }\
249 }
250
251 #define DEBUG_EXPR(module, priority, EXPR)\
252 {\
253 if (RUBY_DEBUG) {\
254 if (g_debug_ptr->validDebug(module, priority)) {\
255 (* debug_cout_ptr) << "Debug: in fn "\
256 << __PRETTY_FUNCTION__\
257 << " in " << __FILE__ << ":"\
258 << __LINE__ << ": "\
259 << #EXPR << " is "\
260 << (EXPR) << endl << flush;\
261 }\
262 }\
263 }
264
265 #define DEBUG_NEWLINE(module, priority)\
266 {\
267 if (RUBY_DEBUG) {\
268 if (g_debug_ptr->validDebug(module, priority)) {\
269 (* debug_cout_ptr) << endl << flush;\
270 }\
271 }\
272 }
273
274 #define DEBUG_SLICC(priority, LINE, MESSAGE)\
275 {\
276 if (RUBY_DEBUG) {\
277 if (g_debug_ptr->validDebug(SLICC_COMP, priority)) {\
278 (* debug_cout_ptr) << (LINE) << (MESSAGE) << endl << flush;\
279 }\
280 }\
281 }
282
283 #define DEBUG_OUT( rest... ) \
284 {\
285 if (RUBY_DEBUG) {\
286 cout << "Debug: in fn "\
287 << __PRETTY_FUNCTION__\
288 << " in " << __FILE__ << ":"\
289 << __LINE__ << ": "; \
290 g_debug_ptr->debugMsg(rest); \
291 }\
292 }
293
294 #define ERROR_OUT( rest... ) \
295 {\
296 if (ERROR_MESSAGE_FLAG) {\
297 cout << "error: in fn "\
298 << __PRETTY_FUNCTION__ << " in "\
299 << __FILE__ << ":"\
300 << __LINE__ << ": ";\
301 g_debug_ptr->debugMsg(rest); \
302 }\
303 }
304
305 #endif //DEBUG_H
306