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