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' },
72 extern "C" void changeDebugVerbosity(VerbosityLevel vb
);
73 extern "C" void changeDebugFilter(int filter
);
76 changeDebugVerbosity(VerbosityLevel vb
)
78 g_debug_ptr
->setVerbosity(vb
);
82 changeDebugFilter(int filter
)
84 g_debug_ptr
->setFilter(filter
);
87 Debug::Debug(const Params
*p
)
91 debug_cout_ptr
= &cout
;
93 setFilterString(p
->filter_string
.c_str());
94 setVerbosityString(p
->verbosity_string
.c_str());
95 setDebugOutputFile(p
->output_filename
.c_str());
96 m_starting_cycle
= p
->start_time
;
97 m_protocol_trace
= p
->protocol_trace
;
106 Debug::printVerbosity(ostream
& out
) const
108 switch (getVerbosity()) {
110 out
<< "verbosity = No_Verb" << endl
;
113 out
<< "verbosity = Low_Verb" << endl
;
116 out
<< "verbosity = Med_Verb" << endl
;
119 out
<< "verbosity = High_Verb" << endl
;
122 out
<< "verbosity = unknown" << endl
;
127 Debug::validDebug(int module
, PriorityLevel priority
)
129 int local_module
= (1 << module
);
130 if (m_filter
& local_module
) {
131 if (g_eventQueue_ptr
== NULL
||
132 g_eventQueue_ptr
->getTime() >= m_starting_cycle
) {
133 switch (m_verbosityLevel
) {
137 return (priority
== HighPrio
);
139 return (priority
== HighPrio
|| priority
== MedPrio
);
149 Debug::setDebugTime(Time t
)
151 m_starting_cycle
= t
;
155 Debug::setVerbosity(VerbosityLevel vb
)
157 m_verbosityLevel
= vb
;
161 Debug::setFilter(int filter
)
167 Debug::setVerbosityString(const char *verb_str
)
169 string verb
= verb_str
? verb_str
: "";
170 if (verb
== "none") {
171 setVerbosity(No_Verb
);
172 } else if (verb
== "low") {
173 setVerbosity(Low_Verb
);
174 } else if (verb
== "med") {
175 setVerbosity(Med_Verb
);
176 } else if (verb
== "high") {
177 setVerbosity(High_Verb
);
179 cerr
<< "Error: unrecognized verbosity (use none, low, med, high): "
181 return true; // error
183 return false; // no error
187 Debug::checkFilter(char ch
)
189 for (int i
= 0; i
< NUMBER_OF_COMPS
; i
++) {
190 // Look at all components to find a character match
191 if (debugComponents
[i
].ch
== ch
) {
192 // We found a match - return no error
193 return false; // no error
196 return true; // error
200 Debug::checkFilterString(const char *filter_str
)
202 if (filter_str
== NULL
) {
203 cerr
<< "Error: unrecognized component filter: NULL" << endl
;
204 return true; // error
207 // check for default filter ("none") before reporting RUBY_DEBUG error
208 if (string(filter_str
) == "none") {
209 return false; // no error
212 if (RUBY_DEBUG
== false) {
213 cerr
<< "Error: User specified set of debug components, but the "
214 << "RUBY_DEBUG compile-time flag is false." << endl
215 << "Solution: Re-compile with RUBY_DEBUG set to true." << endl
;
216 return true; // error
219 if (string(filter_str
) == "all") {
220 return false; // no error
223 // scan string checking each character
224 for (unsigned int i
= 0; i
< strlen(filter_str
); i
++) {
225 bool unrecognized
= checkFilter(filter_str
[i
]);
226 if (unrecognized
== true) {
227 return true; // error
230 return false; // no error
234 Debug::setFilterString(const char *filter_str
)
236 if (checkFilterString(filter_str
)) {
237 return true; // error
240 if (string(filter_str
) == "all" ) {
242 } else if (string(filter_str
) == "none") {
245 // scan string adding to bit mask for each component which is present
246 for (unsigned int i
= 0; i
< strlen(filter_str
); i
++) {
247 bool error
= addFilter( filter_str
[i
] );
249 return true; // error
253 return false; // no error
257 Debug::addFilter(char ch
)
259 for (int i
= 0; i
< NUMBER_OF_COMPS
; i
++) {
260 // Look at all components to find a character match
261 if (debugComponents
[i
].ch
== ch
) {
262 // We found a match - update the filter bit mask
263 cout
<< " Debug: Adding to filter: '" << ch
<< "' ("
264 << debugComponents
[i
].desc
<< ")" << endl
;
265 m_filter
|= (1 << i
);
266 return false; // no error
270 // We didn't find the character
271 cerr
<< "Error: unrecognized component filter: " << ch
<< endl
;
273 return true; // error
282 void Debug::allFilter()
288 Debug::usageInstructions(void)
290 cerr
<< "Debug components: " << endl
;
291 for (int i
= 0; i
< NUMBER_OF_COMPS
; i
++) {
292 cerr
<< " " << debugComponents
[i
].ch
<< ": "
293 << debugComponents
[i
].desc
<< endl
;
298 Debug::print(ostream
& out
) const
300 out
<< "[Debug]" << endl
;
304 Debug::setDebugOutputFile (const char *filename
)
306 if (filename
== NULL
|| !strcmp(filename
, "none")) {
307 debug_cout_ptr
= &cout
;
311 if (m_fout
.is_open()) {
314 m_fout
.open(filename
, ios::out
);
315 if (!m_fout
.is_open()) {
316 cerr
<< "setDebugOutputFile: can't open file " << filename
<< endl
;
318 debug_cout_ptr
= &m_fout
;
323 Debug::closeDebugOutputFile ()
325 if (m_fout
.is_open()) {
327 debug_cout_ptr
= &cout
;
332 Debug::debugMsg( const char *fmt
, ...)
336 // you could check validDebug() here before printing the message
338 vfprintf(stdout
, fmt
, args
);
343 RubyDebugParams::create()
345 return new Debug(this);