ruby: Import ruby and slicc from GEMS
[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 extern std::ostream * debug_cout_ptr;
41
42 // component enumeration
43 enum DebugComponents
44 {
45 #undef DEFINE_COMP
46 #define DEFINE_COMP(component, character, description) component,
47 #include "Debug.def"
48 NUMBER_OF_COMPS
49 };
50
51 enum PriorityLevel {HighPrio, MedPrio, LowPrio};
52 enum VerbosityLevel {No_Verb, Low_Verb, Med_Verb, High_Verb};
53
54 class Debug {
55 public:
56 // Constructors
57 Debug( const char *filterString, const char *verboseString,
58 Time filterStartTime, const char *filename );
59
60 // Destructor
61 ~Debug();
62
63 // Public Methods
64 bool validDebug(int module, PriorityLevel priority);
65 void printVerbosity(ostream& out) const;
66 void setVerbosity(VerbosityLevel vb);
67 static bool checkVerbosityString(const char *verb_str);
68 bool setVerbosityString(const char *);
69 VerbosityLevel getVerbosity() const { return m_verbosityLevel; }
70 void setFilter(int);
71 static bool checkFilter( char);
72 static bool checkFilterString(const char *);
73 bool setFilterString(const char *);
74 void setDebugTime(Time);
75 Time getDebugTime() const { return m_starting_cycle; }
76 bool addFilter(char);
77 void clearFilter();
78 void allFilter();
79 void print(ostream& out) const;
80 /* old school debugging "vararg": sends messages to screen and log */
81 void debugMsg( const char *fmt, ... );
82
83 void setDebugOutputFile (const char * filename);
84 void closeDebugOutputFile ();
85 static void usageInstructions(void);
86
87 private:
88 // Private Methods
89
90 // Private copy constructor and assignment operator
91 Debug(const Debug& obj);
92 Debug& operator=(const Debug& obj);
93
94 // Data Members (m_ prefix)
95 VerbosityLevel m_verbosityLevel;
96 int m_filter;
97 Time m_starting_cycle;
98
99 std::fstream m_fout;
100 };
101
102 // Output operator declaration
103 ostream& operator<<(ostream& out, const Debug& obj);
104
105 // ******************* Definitions *******************
106
107 // Output operator definition
108 extern inline
109 ostream& operator<<(ostream& out, const Debug& obj)
110 {
111 obj.print(out);
112 out << flush;
113 return out;
114 }
115
116 const bool ERROR_MESSAGE_FLAG = true;
117 const bool WARNING_MESSAGE_FLAG = true;
118
119 #ifdef RUBY_NO_ASSERT
120 const bool ASSERT_FLAG = false;
121 #else
122 const bool ASSERT_FLAG = true;
123 #endif
124
125 #undef assert
126 #define assert(EXPR) ASSERT(EXPR)
127 #undef ASSERT
128 #define ASSERT(EXPR)\
129 {\
130 if (ASSERT_FLAG) {\
131 if (!(EXPR)) {\
132 cerr << "failed assertion '"\
133 << #EXPR << "' at fn "\
134 << __PRETTY_FUNCTION__ << " in "\
135 << __FILE__ << ":"\
136 << __LINE__ << endl << flush;\
137 (* debug_cout_ptr) << "failed assertion '"\
138 << #EXPR << "' at fn "\
139 << __PRETTY_FUNCTION__ << " in "\
140 << __FILE__ << ":"\
141 << __LINE__ << endl << flush;\
142 if(isatty(STDIN_FILENO)) {\
143 cerr << "At this point you might want to attach a debug to ";\
144 cerr << "the running and get to the" << endl;\
145 cerr << "crash site; otherwise press enter to continue" << endl;\
146 cerr << "PID: " << getpid();\
147 cerr << endl << flush; \
148 char c; \
149 cin.get(c); \
150 }\
151 abort();\
152 }\
153 }\
154 }
155
156 #define BREAK(X)\
157 {\
158 cerr << "breakpoint '"\
159 << #X << "' reached at fn "\
160 << __PRETTY_FUNCTION__ << " in "\
161 << __FILE__ << ":"\
162 << __LINE__ << endl << flush;\
163 if(isatty(STDIN_FILENO)) {\
164 cerr << "press enter to continue" << endl;\
165 cerr << "PID: " << getpid();\
166 cerr << endl << flush; \
167 char c; \
168 cin.get(c); \
169 }\
170 }
171
172 #define ERROR_MSG(MESSAGE)\
173 {\
174 if (ERROR_MESSAGE_FLAG) {\
175 cerr << "Fatal Error: in fn "\
176 << __PRETTY_FUNCTION__ << " in "\
177 << __FILE__ << ":"\
178 << __LINE__ << ": "\
179 << (MESSAGE) << endl << flush;\
180 (* debug_cout_ptr) << "Fatal Error: in fn "\
181 << __PRETTY_FUNCTION__ << " in "\
182 << __FILE__ << ":"\
183 << __LINE__ << ": "\
184 << (MESSAGE) << endl << flush;\
185 abort();\
186 }\
187 }
188
189 #define WARN_MSG(MESSAGE)\
190 {\
191 if (WARNING_MESSAGE_FLAG) {\
192 cerr << "Warning: in fn "\
193 << __PRETTY_FUNCTION__ << " in "\
194 << __FILE__ << ":"\
195 << __LINE__ << ": "\
196 << (MESSAGE) << endl << flush;\
197 (* debug_cout_ptr) << "Warning: in fn "\
198 << __PRETTY_FUNCTION__ << " in "\
199 << __FILE__ << ":"\
200 << __LINE__ << ": "\
201 << (MESSAGE) << endl << flush;\
202 }\
203 }
204
205 #define WARN_EXPR(EXPR)\
206 {\
207 if (WARNING_MESSAGE_FLAG) {\
208 cerr << "Warning: in fn "\
209 << __PRETTY_FUNCTION__ << " in "\
210 << __FILE__ << ":"\
211 << __LINE__ << ": "\
212 << #EXPR << " is "\
213 << (EXPR) << endl << flush;\
214 (* debug_cout_ptr) << "Warning: in fn "\
215 << __PRETTY_FUNCTION__ << " in "\
216 << __FILE__ << ":"\
217 << __LINE__ << ": "\
218 << #EXPR << " is "\
219 << (EXPR) << endl << flush;\
220 }\
221 }
222
223 #define DEBUG_MSG(module, priority, MESSAGE)\
224 {\
225 if (RUBY_DEBUG) {\
226 if (g_debug_ptr->validDebug(module, priority)) {\
227 (* debug_cout_ptr) << "Debug: in fn "\
228 << __PRETTY_FUNCTION__\
229 << " in " << __FILE__ << ":"\
230 << __LINE__ << ": "\
231 << (MESSAGE) << endl << flush;\
232 }\
233 }\
234 }
235
236 #define DEBUG_EXPR(module, priority, EXPR)\
237 {\
238 if (RUBY_DEBUG) {\
239 if (g_debug_ptr->validDebug(module, priority)) {\
240 (* debug_cout_ptr) << "Debug: in fn "\
241 << __PRETTY_FUNCTION__\
242 << " in " << __FILE__ << ":"\
243 << __LINE__ << ": "\
244 << #EXPR << " is "\
245 << (EXPR) << endl << flush;\
246 }\
247 }\
248 }
249
250 #define DEBUG_NEWLINE(module, priority)\
251 {\
252 if (RUBY_DEBUG) {\
253 if (g_debug_ptr->validDebug(module, priority)) {\
254 (* debug_cout_ptr) << endl << flush;\
255 }\
256 }\
257 }
258
259 #define DEBUG_SLICC(priority, LINE, MESSAGE)\
260 {\
261 if (RUBY_DEBUG) {\
262 if (g_debug_ptr->validDebug(SLICC_COMP, priority)) {\
263 (* debug_cout_ptr) << (LINE) << (MESSAGE) << endl << flush;\
264 }\
265 }\
266 }
267
268 #define DEBUG_OUT( rest... ) \
269 {\
270 if (RUBY_DEBUG) {\
271 cout << "Debug: in fn "\
272 << __PRETTY_FUNCTION__\
273 << " in " << __FILE__ << ":"\
274 << __LINE__ << ": "; \
275 g_debug_ptr->debugMsg(rest); \
276 }\
277 }
278
279 #define ERROR_OUT( rest... ) \
280 {\
281 if (ERROR_MESSAGE_FLAG) {\
282 cout << "error: in fn "\
283 << __PRETTY_FUNCTION__ << " in "\
284 << __FILE__ << ":"\
285 << __LINE__ << ": ";\
286 g_debug_ptr->debugMsg(rest); \
287 }\
288 }
289
290 #endif //DEBUG_H
291