time: improve time datastructure
[gem5.git] / src / base / trace.cc
index 9fa615f4d5ba628fa913a814d2f049d33b01b8b5..22e05e6641908fb0026959152caaad518fc1a79c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001-2005 The Regents of The University of Michigan
+ * Copyright (c) 2001-2006 The Regents of The University of Michigan
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
 #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;
 
 namespace Trace {
 const string DefaultName("global");
 FlagVec flags(NumFlags, false);
+bool enabled = false;
 
 //
 // This variable holds the output stream for debug information.  Other
@@ -53,151 +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;
-    }
-
-    do {
-        Record *rec = *bufPtr;
 
-        rec->dump(os);
+    std::ostream &os = *dprintf_stream;
 
-        if (++bufPtr == wrapRecPtr) {
-            bufPtr = &buffer[0];
-        }
-    } while (bufPtr != nextRecPtr);
-}
-
-PrintfRecord::~PrintfRecord()
-{
-    delete &args;
-}
-
-void
-PrintfRecord::dump(ostream &os)
-{
     string fmt = "";
+    CPrintfArgsList args;
 
     if (!name.empty()) {
         fmt = "%s: " + fmt;
-        args.prepend(name);
+        args.push_front(name);
     }
 
-    if (cycle != (Tick)-1) {
+    if (when != (Tick)-1) {
         fmt = "%7d: " + fmt;
-        args.prepend(cycle);
+        args.push_front(when);
     }
 
-    fmt += format;
-
-    args.dump(os, fmt);
-    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;
 
@@ -213,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");
@@ -223,126 +148,64 @@ 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()
+bool
+changeFlag(const char *s, bool value)
 {
     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)
-{
-    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
+dumpStatus()
+{
+    using namespace Trace;
+    for (int i = 0; i < numFlagStrings; ++i) {
+        if (flags[i])
+            cprintf("%s\n", flagStrings[i]);
+    }
+}
+
+} // namespace Trace
+
+
+// add a set of functions that can easily be invoked from gdb
 void
 setTraceFlag(const char *string)
 {
-    tweakTraceFlag(string, true);
+    Trace::changeFlag(string, true);
 }
 
 void
 clearTraceFlag(const char *string)
 {
-    tweakTraceFlag(string, false);
+    Trace::changeFlag(string, false);
+}
+
+void
+dumpTraceStatus()
+{
+    Trace::dumpStatus();
 }