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/gems_common/util.hh"
34 #include "mem/ruby/common/Debug.hh"
35 #include "mem/ruby/common/Global.hh"
36 #include "mem/ruby/eventqueue/RubyEventQueue.hh"
41 extern Debug
* g_debug_ptr
;
42 ostream
*debug_cout_ptr
;
44 bool Debug::m_protocol_trace
= false;
45 struct DebugComponentData
51 // component character list
52 DebugComponentData debugComponents
[] =
57 {"Event Queue", 'e' },
63 {"Network Queues", 'Q' },
65 {"Network Internals", 'i' },
66 {"Store Buffer", 'b' },
73 extern "C" void changeDebugVerbosity(VerbosityLevel vb
);
74 extern "C" void changeDebugFilter(int filter
);
77 changeDebugVerbosity(VerbosityLevel vb
)
79 g_debug_ptr
->setVerbosity(vb
);
83 changeDebugFilter(int filter
)
85 g_debug_ptr
->setFilter(filter
);
88 Debug::Debug(const Params
*p
)
92 debug_cout_ptr
= &cout
;
94 setFilterString(p
->filter_string
.c_str());
95 setVerbosityString(p
->verbosity_string
.c_str());
96 setDebugOutputFile(p
->output_filename
.c_str());
97 m_starting_cycle
= p
->start_time
;
98 m_protocol_trace
= p
->protocol_trace
;
107 Debug::printVerbosity(ostream
& out
) const
109 switch (getVerbosity()) {
111 out
<< "verbosity = No_Verb" << endl
;
114 out
<< "verbosity = Low_Verb" << endl
;
117 out
<< "verbosity = Med_Verb" << endl
;
120 out
<< "verbosity = High_Verb" << endl
;
123 out
<< "verbosity = unknown" << endl
;
128 Debug::validDebug(int module
, PriorityLevel priority
)
130 int local_module
= (1 << module
);
131 if (m_filter
& local_module
) {
132 if (g_eventQueue_ptr
== NULL
||
133 g_eventQueue_ptr
->getTime() >= m_starting_cycle
) {
134 switch (m_verbosityLevel
) {
138 return (priority
== HighPrio
);
140 return (priority
== HighPrio
|| priority
== MedPrio
);
150 Debug::setDebugTime(Time t
)
152 m_starting_cycle
= t
;
156 Debug::setVerbosity(VerbosityLevel vb
)
158 m_verbosityLevel
= vb
;
162 Debug::setFilter(int filter
)
168 Debug::setVerbosityString(const char *verb_str
)
170 string verb
= verb_str
? verb_str
: "";
171 if (verb
== "none") {
172 setVerbosity(No_Verb
);
173 } else if (verb
== "low") {
174 setVerbosity(Low_Verb
);
175 } else if (verb
== "med") {
176 setVerbosity(Med_Verb
);
177 } else if (verb
== "high") {
178 setVerbosity(High_Verb
);
180 cerr
<< "Error: unrecognized verbosity (use none, low, med, high): "
182 return true; // error
184 return false; // no error
188 Debug::checkFilter(char ch
)
190 for (int i
= 0; i
< NUMBER_OF_COMPS
; i
++) {
191 // Look at all components to find a character match
192 if (debugComponents
[i
].ch
== ch
) {
193 // We found a match - return no error
194 return false; // no error
197 return true; // error
201 Debug::checkFilterString(const char *filter_str
)
203 if (filter_str
== NULL
) {
204 cerr
<< "Error: unrecognized component filter: NULL" << endl
;
205 return true; // error
208 // check for default filter ("none") before reporting RUBY_DEBUG error
209 if (string(filter_str
) == "none") {
210 return false; // no error
213 if (RUBY_DEBUG
== false) {
214 cerr
<< "Error: User specified set of debug components, but the "
215 << "RUBY_DEBUG compile-time flag is false." << endl
216 << "Solution: Re-compile with RUBY_DEBUG set to true." << endl
;
217 return true; // error
220 if (string(filter_str
) == "all") {
221 return false; // no error
224 // scan string checking each character
225 for (unsigned int i
= 0; i
< strlen(filter_str
); i
++) {
226 bool unrecognized
= checkFilter(filter_str
[i
]);
227 if (unrecognized
== true) {
228 return true; // error
231 return false; // no error
235 Debug::setFilterString(const char *filter_str
)
237 if (checkFilterString(filter_str
)) {
238 return true; // error
241 if (string(filter_str
) == "all" ) {
243 } else if (string(filter_str
) == "none") {
246 // scan string adding to bit mask for each component which is present
247 for (unsigned int i
= 0; i
< strlen(filter_str
); i
++) {
248 bool error
= addFilter( filter_str
[i
] );
250 return true; // error
254 return false; // no error
258 Debug::addFilter(char ch
)
260 for (int i
= 0; i
< NUMBER_OF_COMPS
; i
++) {
261 // Look at all components to find a character match
262 if (debugComponents
[i
].ch
== ch
) {
263 // We found a match - update the filter bit mask
264 cout
<< " Debug: Adding to filter: '" << ch
<< "' ("
265 << debugComponents
[i
].desc
<< ")" << endl
;
266 m_filter
|= (1 << i
);
267 return false; // no error
271 // We didn't find the character
272 cerr
<< "Error: unrecognized component filter: " << ch
<< endl
;
274 return true; // error
283 void Debug::allFilter()
289 Debug::usageInstructions(void)
291 cerr
<< "Debug components: " << endl
;
292 for (int i
= 0; i
< NUMBER_OF_COMPS
; i
++) {
293 cerr
<< " " << debugComponents
[i
].ch
<< ": "
294 << debugComponents
[i
].desc
<< endl
;
299 Debug::print(ostream
& out
) const
301 out
<< "[Debug]" << endl
;
305 Debug::setDebugOutputFile (const char *filename
)
307 if (filename
== NULL
|| !strcmp(filename
, "none")) {
308 debug_cout_ptr
= &cout
;
312 if (m_fout
.is_open()) {
315 m_fout
.open(filename
, ios::out
);
316 if (!m_fout
.is_open()) {
317 cerr
<< "setDebugOutputFile: can't open file " << filename
<< endl
;
319 debug_cout_ptr
= &m_fout
;
324 Debug::closeDebugOutputFile ()
326 if (m_fout
.is_open()) {
328 debug_cout_ptr
= &cout
;
333 Debug::debugMsg( const char *fmt
, ...)
337 // you could check validDebug() here before printing the message
339 vfprintf(stdout
, fmt
, args
);
344 RubyDebugParams::create()
346 return new Debug(this);