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