2 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "base/misc.hh"
33 #include "mem/ruby/common/Debug.hh"
34 #include "mem/ruby/common/Global.hh"
35 #include "mem/ruby/eventqueue/RubyEventQueue.hh"
40 extern Debug
* g_debug_ptr
;
41 ostream
*debug_cout_ptr
;
43 bool Debug::m_protocol_trace
= false;
44 struct DebugComponentData
50 // component character list
51 DebugComponentData debugComponents
[] =
56 {"Event Queue", 'e' },
62 {"Network Queues", 'Q' },
64 {"Network Internals", 'i' },
65 {"Store Buffer", 'b' },
71 extern "C" void changeDebugVerbosity(VerbosityLevel vb
);
72 extern "C" void changeDebugFilter(int filter
);
75 changeDebugVerbosity(VerbosityLevel vb
)
77 g_debug_ptr
->setVerbosity(vb
);
81 changeDebugFilter(int filter
)
83 g_debug_ptr
->setFilter(filter
);
86 Debug::Debug(const Params
*p
)
90 debug_cout_ptr
= &cout
;
92 setFilterString(p
->filter_string
.c_str());
93 setVerbosityString(p
->verbosity_string
.c_str());
94 setDebugOutputFile(p
->output_filename
.c_str());
95 m_starting_cycle
= p
->start_time
;
96 m_protocol_trace
= p
->protocol_trace
;
105 Debug::printVerbosity(ostream
& out
) const
107 switch (getVerbosity()) {
109 out
<< "verbosity = No_Verb" << endl
;
112 out
<< "verbosity = Low_Verb" << endl
;
115 out
<< "verbosity = Med_Verb" << endl
;
118 out
<< "verbosity = High_Verb" << endl
;
121 out
<< "verbosity = unknown" << endl
;
126 Debug::validDebug(int module
, PriorityLevel priority
)
128 int local_module
= (1 << module
);
129 if (m_filter
& local_module
) {
130 if (g_eventQueue_ptr
== NULL
||
131 g_eventQueue_ptr
->getTime() >= m_starting_cycle
) {
132 switch (m_verbosityLevel
) {
136 return (priority
== HighPrio
);
138 return (priority
== HighPrio
|| priority
== MedPrio
);
148 Debug::setDebugTime(Time t
)
150 m_starting_cycle
= t
;
154 Debug::setVerbosity(VerbosityLevel vb
)
156 m_verbosityLevel
= vb
;
160 Debug::setFilter(int filter
)
166 Debug::setVerbosityString(const char *verb_str
)
168 string verb
= verb_str
? verb_str
: "";
169 if (verb
== "none") {
170 setVerbosity(No_Verb
);
171 } else if (verb
== "low") {
172 setVerbosity(Low_Verb
);
173 } else if (verb
== "med") {
174 setVerbosity(Med_Verb
);
175 } else if (verb
== "high") {
176 setVerbosity(High_Verb
);
178 cerr
<< "Error: unrecognized verbosity (use none, low, med, high): "
180 return true; // error
182 return false; // no error
186 Debug::checkFilter(char ch
)
188 for (int i
= 0; i
< NUMBER_OF_COMPS
; i
++) {
189 // Look at all components to find a character match
190 if (debugComponents
[i
].ch
== ch
) {
191 // We found a match - return no error
192 return false; // no error
195 return true; // error
199 Debug::checkFilterString(const char *filter_str
)
201 if (filter_str
== NULL
) {
202 cerr
<< "Error: unrecognized component filter: NULL" << endl
;
203 return true; // error
206 // check for default filter ("none") before reporting RUBY_DEBUG error
207 if (string(filter_str
) == "none") {
208 return false; // no error
211 if (RUBY_DEBUG
== false) {
212 cerr
<< "Error: User specified set of debug components, but the "
213 << "RUBY_DEBUG compile-time flag is false." << endl
214 << "Solution: Re-compile with RUBY_DEBUG set to true." << endl
;
215 return true; // error
218 if (string(filter_str
) == "all") {
219 return false; // no error
222 // scan string checking each character
223 for (unsigned int i
= 0; i
< strlen(filter_str
); i
++) {
224 bool unrecognized
= checkFilter(filter_str
[i
]);
225 if (unrecognized
== true) {
226 return true; // error
229 return false; // no error
233 Debug::setFilterString(const char *filter_str
)
235 if (checkFilterString(filter_str
)) {
236 return true; // error
239 if (string(filter_str
) == "all" ) {
241 } else if (string(filter_str
) == "none") {
244 // scan string adding to bit mask for each component which is present
245 for (unsigned int i
= 0; i
< strlen(filter_str
); i
++) {
246 bool error
= addFilter( filter_str
[i
] );
248 return true; // error
252 return false; // no error
256 Debug::addFilter(char ch
)
258 for (int i
= 0; i
< NUMBER_OF_COMPS
; i
++) {
259 // Look at all components to find a character match
260 if (debugComponents
[i
].ch
== ch
) {
261 // We found a match - update the filter bit mask
262 cout
<< " Debug: Adding to filter: '" << ch
<< "' ("
263 << debugComponents
[i
].desc
<< ")" << endl
;
264 m_filter
|= (1 << i
);
265 return false; // no error
269 // We didn't find the character
270 cerr
<< "Error: unrecognized component filter: " << ch
<< endl
;
272 return true; // error
281 void Debug::allFilter()
287 Debug::usageInstructions(void)
289 cerr
<< "Debug components: " << endl
;
290 for (int i
= 0; i
< NUMBER_OF_COMPS
; i
++) {
291 cerr
<< " " << debugComponents
[i
].ch
<< ": "
292 << debugComponents
[i
].desc
<< endl
;
297 Debug::print(ostream
& out
) const
299 out
<< "[Debug]" << endl
;
303 Debug::setDebugOutputFile (const char *filename
)
305 if (filename
== NULL
|| !strcmp(filename
, "none")) {
306 debug_cout_ptr
= &cout
;
310 if (m_fout
.is_open()) {
313 m_fout
.open(filename
, ios::out
);
314 if (!m_fout
.is_open()) {
315 cerr
<< "setDebugOutputFile: can't open file " << filename
<< endl
;
317 debug_cout_ptr
= &m_fout
;
322 Debug::closeDebugOutputFile ()
324 if (m_fout
.is_open()) {
326 debug_cout_ptr
= &cout
;
331 Debug::debugMsg( const char *fmt
, ...)
335 // you could check validDebug() here before printing the message
337 vfprintf(stdout
, fmt
, args
);
342 RubyDebugParams::create()
344 return new Debug(this);