2 * Copyright (c) 2014, 2019 ARM Limited
5 * Copyright (c) 2001-2006 The Regents of The University of Michigan
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met: redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer;
12 * redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution;
15 * neither the name of the copyright holders nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #ifndef __BASE_TRACE_HH__
33 #define __BASE_TRACE_HH__
37 #include "base/cprintf.hh"
38 #include "base/debug.hh"
39 #include "base/match.hh"
40 #include "base/types.hh"
41 #include "sim/core.hh"
45 /** Debug logging base class. Handles formatting and outputting
46 * time/name/message messages */
50 /** Name match for objects to ignore */
54 /** Log a single message */
55 template <typename ...Args>
56 void dprintf(Tick when, const std::string &name, const char *fmt,
59 dprintf_flag(when, name, "", fmt, args...);
62 /** Log a single message with a flag prefix. */
63 template <typename ...Args>
64 void dprintf_flag(Tick when, const std::string &name,
65 const std::string &flag,
66 const char *fmt, const Args &...args)
68 if (!name.empty() && ignore.match(name))
70 std::ostringstream line;
71 ccprintf(line, fmt, args...);
72 logMessage(when, name, flag, line.str());
75 /** Dump a block of data of length len */
76 void dump(Tick when, const std::string &name,
77 const void *d, int len, const std::string &flag);
79 /** Log formatted message */
80 virtual void logMessage(Tick when, const std::string &name,
81 const std::string &flag, const std::string &message) = 0;
83 /** Return an ostream that can be used to send messages to
84 * the 'same place' as formatted logMessage messages. This
85 * can be implemented to use a logger's underlying ostream,
86 * to provide an ostream which formats the output in some
87 * way, or just set to one of std::cout, std::cerr */
88 virtual std::ostream &getOstream() = 0;
90 /** Set objects to ignore */
91 void setIgnore(ObjectMatch &ignore_) { ignore = ignore_; }
93 /** Add objects to ignore */
94 void addIgnore(const ObjectMatch &ignore_) { ignore.add(ignore_); }
99 /** Logging wrapper for ostreams with the format:
100 * <when>: <name>: <message-body> */
101 class OstreamLogger : public Logger
104 std::ostream &stream;
107 OstreamLogger(std::ostream &stream_) : stream(stream_)
110 void logMessage(Tick when, const std::string &name,
111 const std::string &flag, const std::string &message) override;
113 std::ostream &getOstream() override { return stream; }
116 /** Get the current global debug logger. This takes ownership of the given
117 * logger which should be allocated using 'new' */
118 Logger *getDebugLogger();
120 /** Get the ostream from the current global logger */
121 std::ostream &output();
123 /** Delete the current global logger and assign a new one */
124 void setDebugLogger(Logger *logger);
126 /** Enable/disable debug logging */
132 // This silly little class allows us to wrap a string in a functor
133 // object so that we can give a name() that DPRINTF will like
137 StringWrap(const std::string &s) : str(s) {}
138 const std::string &operator()() const { return str; }
141 // Return the global context name "global". This function gets called when
142 // the DPRINTF macros are used in a context without a visible name() function
143 const std::string &name();
145 // Interface for things with names. (cf. SimObject but without other
146 // functionality). This is useful when using DPRINTF
150 const std::string _name;
153 Named(const std::string &name_) : _name(name_) { }
156 const std::string &name() const { return _name; }
160 // DPRINTF is a debugging trace facility that allows one to
161 // selectively enable tracing statements. To use DPRINTF, there must
162 // be a function or functor called name() that returns a const
163 // std::string & in the current scope.
165 // If you desire that the automatic printing not occur, use DPRINTFR
171 #define DTRACE(x) (Debug::x)
173 #define DDUMP(x, data, count) do { \
174 using namespace Debug; \
176 Trace::getDebugLogger()->dump( \
177 curTick(), name(), data, count, #x); \
180 #define DPRINTF(x, ...) do { \
181 using namespace Debug; \
183 Trace::getDebugLogger()->dprintf_flag( \
184 curTick(), name(), #x, __VA_ARGS__); \
188 #define DPRINTFS(x, s, ...) do { \
189 using namespace Debug; \
191 Trace::getDebugLogger()->dprintf_flag( \
192 curTick(), s->name(), #x, __VA_ARGS__); \
196 #define DPRINTFR(x, ...) do { \
197 using namespace Debug; \
199 Trace::getDebugLogger()->dprintf_flag( \
200 (Tick)-1, std::string(), #x, __VA_ARGS__); \
204 #define DDUMPN(data, count) do { \
205 Trace::getDebugLogger()->dump(curTick(), name(), data, count); \
208 #define DPRINTFN(...) do { \
209 Trace::getDebugLogger()->dprintf(curTick(), name(), __VA_ARGS__); \
212 #define DPRINTFNR(...) do { \
213 Trace::getDebugLogger()->dprintf((Tick)-1, std::string(), __VA_ARGS__); \
216 #define DPRINTF_UNCONDITIONAL(x, ...) do { \
217 Trace::getDebugLogger()->dprintf_flag( \
218 curTick(), name(), #x, __VA_ARGS__); \
223 #define DTRACE(x) (false)
224 #define DDUMP(x, data, count) do {} while (0)
225 #define DPRINTF(x, ...) do {} while (0)
226 #define DPRINTFS(x, ...) do {} while (0)
227 #define DPRINTFR(...) do {} while (0)
228 #define DDUMPN(data, count) do {} while (0)
229 #define DPRINTFN(...) do {} while (0)
230 #define DPRINTFNR(...) do {} while (0)
231 #define DPRINTF_UNCONDITIONAL(x, ...) do {} while (0)
235 #endif // __BASE_TRACE_HH__