diff options
Diffstat (limited to 'src/base/trace.cc')
-rw-r--r-- | src/base/trace.cc | 288 |
1 files changed, 77 insertions, 211 deletions
diff --git a/src/base/trace.cc b/src/base/trace.cc index 6e9838456..7afb038be 100644 --- a/src/base/trace.cc +++ b/src/base/trace.cc @@ -37,8 +37,9 @@ #include <vector> #include "base/misc.hh" -#include "base/trace.hh" +#include "base/output.hh" #include "base/str.hh" +#include "base/trace.hh" #include "base/varargs.hh" using namespace std; @@ -46,6 +47,7 @@ using namespace std; namespace Trace { const string DefaultName("global"); FlagVec flags(NumFlags, false); +bool enabled = true; // // This variable holds the output stream for debug information. Other @@ -54,149 +56,74 @@ FlagVec flags(NumFlags, false); // output. // ostream *dprintf_stream = &cerr; - -ObjectMatch ignore; - -Log theLog; - -Log::Log() +ostream & +output() { - size = 0; - buffer = NULL; + return *dprintf_stream; } - void -Log::init(int _size) +setOutput(const string &filename) { - if (buffer != NULL) { - fatal("Trace::Log::init called twice!"); - } - - size = _size; - - buffer = new Record *[size]; - - for (int i = 0; i < size; ++i) { - buffer[i] = NULL; - } - - nextRecPtr = &buffer[0]; - wrapRecPtr = &buffer[size]; + dprintf_stream = simout.find(filename); } +ObjectMatch ignore; -Log::~Log() +void +dprintf(Tick when, const std::string &name, const char *format, + CPRINTF_DEFINITION) { - for (int i = 0; i < size; ++i) { - delete buffer[i]; - } - - delete [] buffer; -} + if (!name.empty() && ignore.match(name)) + return; + std::ostream &os = *dprintf_stream; -void -Log::append(Record *rec) -{ - // dump record to output stream if there's one open - if (dprintf_stream != NULL) { - rec->dump(*dprintf_stream); - } else { - rec->dump(cout); - } + string fmt = ""; + CPrintfArgsList args(VARARGS_ALLARGS); - // no buffering: justget rid of it now - if (buffer == NULL) { - delete rec; - return; + if (!name.empty()) { + fmt = "%s: " + fmt; + args.push_front(name); } - Record *oldRec = *nextRecPtr; - - if (oldRec != NULL) { - // log has wrapped: overwrite - delete oldRec; + if (when != (Tick)-1) { + fmt = "%7d: " + fmt; + args.push_front(when); } - *nextRecPtr = rec; + fmt += format; - if (++nextRecPtr == wrapRecPtr) { - nextRecPtr = &buffer[0]; - } + ccprintf(os, fmt.c_str(), args); + os.flush(); } - void -Log::dump(ostream &os) +dump(Tick when, const std::string &name, const void *d, int len) { - if (buffer == NULL) { + if (!name.empty() && ignore.match(name)) return; - } - Record **bufPtr = nextRecPtr; - - if (*bufPtr == NULL) { - // next record slot is empty: log must not be full yet. - // start dumping from beginning of buffer - bufPtr = buffer; - } + std::ostream &os = *dprintf_stream; - do { - Record *rec = *bufPtr; - - rec->dump(os); - - if (++bufPtr == wrapRecPtr) { - bufPtr = &buffer[0]; - } - } while (bufPtr != nextRecPtr); -} - -PrintfRecord::~PrintfRecord() -{} - -void -PrintfRecord::dump(ostream &os) -{ string fmt = ""; + CPrintfArgsList args; if (!name.empty()) { fmt = "%s: " + fmt; args.push_front(name); } - if (cycle != (Tick)-1) { + if (when != (Tick)-1) { fmt = "%7d: " + fmt; - args.push_front(cycle); + args.push_front(when); } - fmt += format; - - ccprintf(os, fmt.c_str(), args); - os.flush(); -} - -DataRecord::DataRecord(Tick _cycle, const string &_name, - const void *_data, int _len) - : Record(_cycle), name(_name), len(_len) -{ - data = new uint8_t[len]; - memcpy(data, _data, len); -} - -DataRecord::~DataRecord() -{ - delete [] data; -} - -void -DataRecord::dump(ostream &os) -{ + const char *data = static_cast<const char *>(d); int c, i, j; - for (i = 0; i < len; i += 16) { - ccprintf(os, "%d: %s: %08x ", cycle, name, i); + ccprintf(os, fmt, args); + ccprintf(os, "%08x ", i); c = len - i; if (c > 16) c = 16; @@ -212,8 +139,7 @@ DataRecord::dump(ostream &os) for (j = 0; j < c; j++) { int ch = data[i + j] & 0x7f; - ccprintf(os, - "%c", (char)(isprint(ch) ? ch : ' ')); + ccprintf(os, "%c", (char)(isprint(ch) ? ch : ' ')); } ccprintf(os, "\n"); @@ -222,126 +148,66 @@ DataRecord::dump(ostream &os) break; } } -} // namespace Trace -// -// Returns the current output stream for debug information. As a -// wrapper around Trace::dprintf_stream, this handles cases where debug -// information is generated in the process of parsing .ini options, -// before we process the option that sets up the debug output stream -// itself. -// -std::ostream & -DebugOut() -{ - return *Trace::dprintf_stream; -} - -///////////////////////////////////////////// -// -// C-linkage functions for invoking from gdb -// -///////////////////////////////////////////// - -// -// Dump trace buffer to specified file (cout if NULL) -// -void -dumpTrace(const char *filename) -{ - if (filename != NULL) { - ofstream out(filename); - Trace::theLog.dump(out); - out.close(); - } - else { - Trace::theLog.dump(cout); - } -} - - -// -// Turn on/off trace output to cerr. Typically used when trace output -// is only going to circular buffer, but you want to see what's being -// sent there as you step through some code in gdb. This uses the -// same facility as the "trace to file" feature, and will print error -// messages rather than clobbering an existing ostream pointer. -// -void -echoTrace(bool on) -{ - if (on) { - if (Trace::dprintf_stream != NULL) { - cerr << "Already echoing trace to a file... go do a 'tail -f'" - << " on that file instead." << endl; - } else { - Trace::dprintf_stream = &cerr; - } - } else { - if (Trace::dprintf_stream != &cerr) { - cerr << "Not echoing trace to cerr." << endl; - } else { - Trace::dprintf_stream = NULL; - } - } -} - -void -printTraceFlags() -{ - using namespace Trace; - for (int i = 0; i < numFlagStrings; ++i) - if (flags[i]) - cprintf("%s\n", flagStrings[i]); -} - -void -tweakTraceFlag(const char *string, bool value) +bool +changeFlag(const char *s, bool value) { using namespace Trace; - std::string str(string); + std::string str(s); for (int i = 0; i < numFlagStrings; ++i) { if (str != flagStrings[i]) continue; - int idx = i; - - if (idx < NumFlags) { - flags[idx] = value; + if (i < NumFlags) { + flags[i] = value; } else { - idx -= NumFlags; - if (idx >= NumCompoundFlags) { - ccprintf(cerr, "Invalid compound flag"); - return; - } - - const Flags *flagVec = compoundFlags[idx]; + i -= NumFlags; + const Flags *flagVec = compoundFlags[i]; for (int j = 0; flagVec[j] != -1; ++j) { - if (flagVec[j] >= NumFlags) { - ccprintf(cerr, "Invalid compound flag"); - return; - } - flags[flagVec[j]] = value; + if (flagVec[j] < NumFlags) + flags[flagVec[j]] = value; } } - cprintf("flag %s was %s\n", string, value ? "set" : "cleared"); - return; + return true; } - cprintf("could not find flag %s\n", string); + // the flag was not found. + return false; } void -setTraceFlag(const char *string) +dumpStatus() { - tweakTraceFlag(string, true); + using namespace Trace; + for (int i = 0; i < numFlagStrings; ++i) { + if (flags[i]) + cprintf("%s\n", flagStrings[i]); + } } -void -clearTraceFlag(const char *string) -{ - tweakTraceFlag(string, false); -} +/* namespace Trace */ } + + +// add a set of functions that can easily be invoked from gdb +extern "C" { + void + setTraceFlag(const char *string) + { + Trace::changeFlag(string, true); + } + + void + clearTraceFlag(const char *string) + { + Trace::changeFlag(string, false); + } + + void + dumpTraceStatus() + { + Trace::dumpStatus(); + } +/* extern "C" */ } |