misc: Changed gem5 version info for gem5 20.2 dev
[gem5.git] / src / base / trace.hh
index 55dd1bd4eac0c7df9fee93da1ffdf6d0f80ee878..aafb9c8e434f5b31028403d3c8dce0297d82f45e 100644 (file)
@@ -1,4 +1,7 @@
 /*
+ * Copyright (c) 2014, 2019 ARM Limited
+ * All rights reserved
+ *
  * Copyright (c) 2001-2006 The Regents of The University of Michigan
  * All rights reserved.
  *
  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * Authors: Nathan Binkert
- *          Steve Reinhardt
  */
 
 #ifndef __BASE_TRACE_HH__
 #define __BASE_TRACE_HH__
 
-#include <vector>
+#include <string>
 
 #include "base/cprintf.hh"
+#include "base/debug.hh"
 #include "base/match.hh"
-#include "sim/host.hh"
-#include "sim/root.hh"
-
-#include "base/traceflags.hh"
+#include "base/types.hh"
+#include "sim/core.hh"
 
 namespace Trace {
 
-    typedef std::vector<bool> FlagVec;
-
-    extern FlagVec flags;
-
-#if TRACING_ON
-    const bool On                              = true;
-#else
-    const bool On                              = false;
-#endif
-
-    inline bool
-    IsOn(int t)
+/** Debug logging base class.  Handles formatting and outputting
+ *  time/name/message messages */
+class Logger
+{
+  protected:
+    /** Name match for objects to ignore */
+    ObjectMatch ignore;
+
+  public:
+    /** Log a single message */
+    template <typename ...Args>
+    void dprintf(Tick when, const std::string &name, const char *fmt,
+                 const Args &...args)
     {
-        return flags[t];
-
+        dprintf_flag(when, name, "", fmt, args...);
     }
 
-    void dump(const uint8_t *data, int count);
-
-    class Record
-    {
-      protected:
-        Tick cycle;
-
-        Record(Tick _cycle)
-            : cycle(_cycle)
-        {
-        }
-
-      public:
-        virtual ~Record() {}
-
-        virtual void dump(std::ostream &) = 0;
-    };
-
-    class PrintfRecord : public Record
+    /** Log a single message with a flag prefix. */
+    template <typename ...Args>
+    void dprintf_flag(Tick when, const std::string &name,
+            const std::string &flag,
+            const char *fmt, const Args &...args)
     {
-      private:
-        const std::string &name;
-        const char *format;
-        CPrintfArgsList args;
-
-      public:
-        PrintfRecord(Tick cycle, const std::string &_name, const char *_format,
-                     CPRINTF_DECLARATION)
-            : Record(cycle), name(_name), format(_format),
-              args(VARARGS_ALLARGS)
-        {
-        }
-
-        virtual ~PrintfRecord();
-
-        virtual void dump(std::ostream &);
-    };
+        if (!name.empty() && ignore.match(name))
+            return;
+        std::ostringstream line;
+        ccprintf(line, fmt, args...);
+        logMessage(when, name, flag, line.str());
+    }
 
-    class DataRecord : public Record
-    {
-      private:
-        const std::string &name;
-        uint8_t *data;
-        int len;
+    /** Dump a block of data of length len */
+    void dump(Tick when, const std::string &name,
+            const void *d, int len, const std::string &flag);
 
-      public:
-        DataRecord(Tick cycle, const std::string &name,
-                   const void *_data, int _len);
-        virtual ~DataRecord();
+    /** Log formatted message */
+    virtual void logMessage(Tick when, const std::string &name,
+            const std::string &flag, const std::string &message) = 0;
 
-        virtual void dump(std::ostream &);
-    };
+    /** Return an ostream that can be used to send messages to
+     *  the 'same place' as formatted logMessage messages.  This
+     *  can be implemented to use a logger's underlying ostream,
+     *  to provide an ostream which formats the output in some
+     *  way, or just set to one of std::cout, std::cerr */
+    virtual std::ostream &getOstream() = 0;
 
-    class Log
-    {
-      private:
-        int     size;          // number of records in log
-        Record **buffer;       // array of 'size' Record ptrs (circular buf)
-        Record **nextRecPtr;   // next slot to use in buffer
-        Record **wrapRecPtr;   // &buffer[size], for quick wrap check
+    /** Set objects to ignore */
+    void setIgnore(ObjectMatch &ignore_) { ignore = ignore_; }
 
-      public:
-        Log();
-        ~Log();
+    /** Add objects to ignore */
+    void addIgnore(const ObjectMatch &ignore_) { ignore.add(ignore_); }
 
-        void init(int _size);
+    virtual ~Logger() { }
+};
 
-        void append(Record *); // append trace record to log
-        void dump(std::ostream &);     // dump contents to stream
-    };
+/** Logging wrapper for ostreams with the format:
+ *  <when>: <name>: <message-body> */
+class OstreamLogger : public Logger
+{
+  protected:
+    std::ostream &stream;
 
-    extern Log theLog;
+  public:
+    OstreamLogger(std::ostream &stream_) : stream(stream_)
+    { }
 
-    extern ObjectMatch ignore;
+    void logMessage(Tick when, const std::string &name,
+            const std::string &flag, const std::string &message) override;
 
-    inline void
-    dprintf(Tick when, const std::string &name, const char *format,
-            CPRINTF_DECLARATION)
-    {
-        if (!name.empty() && ignore.match(name))
-            return;
+    std::ostream &getOstream() override { return stream; }
+};
 
-        theLog.append(new Trace::PrintfRecord(when, name, format,
-                                              VARARGS_ALLARGS));
-    }
+/** Get the current global debug logger.  This takes ownership of the given
+ *  logger which should be allocated using 'new' */
+Logger *getDebugLogger();
 
-    inline void
-    dataDump(Tick when, const std::string &name, const void *data, int len)
-    {
-        theLog.append(new Trace::DataRecord(when, name, data, len));
-    }
+/** Get the ostream from the current global logger */
+std::ostream &output();
 
-    extern const std::string DefaultName;
+/** Delete the current global logger and assign a new one */
+void setDebugLogger(Logger *logger);
 
-};
+/** Enable/disable debug logging */
+void enable();
+void disable();
 
-std::ostream &DebugOut();
+} // namespace Trace
 
 // This silly little class allows us to wrap a string in a functor
 // object so that we can give a name() that DPRINTF will like
@@ -167,54 +138,109 @@ struct StringWrap
     const std::string &operator()() const { return str; }
 };
 
-inline const std::string &name() { return Trace::DefaultName; }
+// Return the global context name "global".  This function gets called when
+// the DPRINTF macros are used in a context without a visible name() function
+const std::string &name();
+
+// Interface for things with names. (cf. SimObject but without other
+// functionality).  This is useful when using DPRINTF
+class Named
+{
+  protected:
+    const std::string _name;
+
+  public:
+    Named(const std::string &name_) : _name(name_) { }
 
-//
-// DPRINTF is a debugging trace facility that allows one to
-// selectively enable tracing statements.  To use DPRINTF, there must
-// be a function or functor called name() that returns a const
-// std::string & in the current scope.
-//
-// If you desire that the automatic printing not occur, use DPRINTFR
-// (R for raw)
-//
+  public:
+    const std::string &name() const { return _name; }
+};
+
+/**
+ * DPRINTF is a debugging trace facility that allows one to
+ * selectively enable tracing statements.  To use DPRINTF, there must
+ * be a function or functor called name() that returns a const
+ * std::string & in the current scope.
+ *
+ * If you desire that the automatic printing not occur, use DPRINTFR
+ * (R for raw)
+ *
+ * \def DDUMP(x, data, count)
+ * \def DPRINTF(x, ...)
+ * \def DPRINTFS(x, s, ...)
+ * \def DPRINTFR(x, ...)
+ * \def DDUMPN(data, count)
+ * \def DPRINTFN(...)
+ * \def DPRINTFNR(...)
+ * \def DPRINTF_UNCONDITIONAL(x, ...)
+ *
+ * @ingroup api_trace
+ * @{
+ */
 
 #if TRACING_ON
 
-#define DTRACE(x) (Trace::IsOn(Trace::x))
+#define DDUMP(x, data, count) do {               \
+    using namespace Debug;                       \
+    if (M5_UNLIKELY(DTRACE(x)))                  \
+        Trace::getDebugLogger()->dump(           \
+            curTick(), name(), data, count, #x); \
+} while (0)
+
+#define DPRINTF(x, ...) do {                     \
+    using namespace Debug;                       \
+    if (M5_UNLIKELY(DTRACE(x))) {                \
+        Trace::getDebugLogger()->dprintf_flag(   \
+            curTick(), name(), #x, __VA_ARGS__); \
+    }                                            \
+} while (0)
 
-#define DDUMP(x, data, count) do {                     \
-    if (DTRACE(x))                                     \
-        Trace::dataDump(curTick, name(), data, count); \
+#define DPRINTFS(x, s, ...) do {                        \
+    using namespace Debug;                              \
+    if (M5_UNLIKELY(DTRACE(x))) {                       \
+        Trace::getDebugLogger()->dprintf_flag(          \
+                curTick(), s->name(), #x, __VA_ARGS__); \
+    }                                                   \
 } while (0)
 
-#define DPRINTF(x, args...) do {                       \
-    if (DTRACE(x))                                     \
-        Trace::dprintf(curTick, name(), args);         \
+#define DPRINTFR(x, ...) do {                          \
+    using namespace Debug;                             \
+    if (M5_UNLIKELY(DTRACE(x))) {                      \
+        Trace::getDebugLogger()->dprintf_flag(         \
+            (Tick)-1, std::string(), #x, __VA_ARGS__); \
+    }                                                  \
 } while (0)
 
-#define DPRINTFR(x, args...) do {                      \
-    if (DTRACE(x))                                     \
-        Trace::dprintf((Tick)-1, std::string(), args); \
+#define DDUMPN(data, count) do {                                       \
+    Trace::getDebugLogger()->dump(curTick(), name(), data, count);     \
 } while (0)
 
-#define DPRINTFN(args...) do {                         \
-    Trace::dprintf(curTick, name(), args);             \
+#define DPRINTFN(...) do {                                             \
+    Trace::getDebugLogger()->dprintf(curTick(), name(), __VA_ARGS__);  \
 } while (0)
 
-#define DPRINTFNR(args...) do {                        \
-    Trace::dprintf((Tick)-1, string(), args);          \
+#define DPRINTFNR(...) do {                                                 \
+    Trace::getDebugLogger()->dprintf((Tick)-1, std::string(), __VA_ARGS__); \
+} while (0)
+
+#define DPRINTF_UNCONDITIONAL(x, ...) do {    \
+    Trace::getDebugLogger()->dprintf_flag(    \
+        curTick(), name(), #x, __VA_ARGS__);  \
 } while (0)
 
 #else // !TRACING_ON
 
-#define DTRACE(x) (false)
-#define DPRINTF(x, args...) do {} while (0)
-#define DPRINTFR(args...) do {} while (0)
-#define DPRINTFN(args...) do {} while (0)
-#define DPRINTFNR(args...) do {} while (0)
 #define DDUMP(x, data, count) do {} while (0)
+#define DPRINTF(x, ...) do {} while (0)
+#define DPRINTFS(x, ...) do {} while (0)
+#define DPRINTFR(...) do {} while (0)
+#define DDUMPN(data, count) do {} while (0)
+#define DPRINTFN(...) do {} while (0)
+#define DPRINTFNR(...) do {} while (0)
+#define DPRINTF_UNCONDITIONAL(x, ...) do {} while (0)
+
+#endif  // TRACING_ON
 
-#endif // TRACING_ON
+/** @} */ // end of api_trace
 
 #endif // __BASE_TRACE_HH__