base: Add XOR and modulo operator to ChannelAddr
[gem5.git] / src / base / trace.hh
1 /*
2 * Copyright (c) 2014, 2019 ARM Limited
3 * All rights reserved
4 *
5 * Copyright (c) 2001-2006 The Regents of The University of Michigan
6 * All rights reserved.
7 *
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.
18 *
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.
30 */
31
32 #ifndef __BASE_TRACE_HH__
33 #define __BASE_TRACE_HH__
34
35 #include <ostream>
36 #include <string>
37 #include <sstream>
38
39 #include "base/compiler.hh"
40 #include "base/cprintf.hh"
41 #include "base/debug.hh"
42 #include "base/match.hh"
43 #include "base/types.hh"
44 #include "sim/core.hh"
45
46 namespace Trace {
47
48 /** Debug logging base class. Handles formatting and outputting
49 * time/name/message messages */
50 class Logger
51 {
52 protected:
53 /** Name match for objects to ignore */
54 ObjectMatch ignore;
55
56 public:
57 /** Log a single message */
58 template <typename ...Args>
59 void dprintf(Tick when, const std::string &name, const char *fmt,
60 const Args &...args)
61 {
62 dprintf_flag(when, name, "", fmt, args...);
63 }
64
65 /** Log a single message with a flag prefix. */
66 template <typename ...Args>
67 void dprintf_flag(Tick when, const std::string &name,
68 const std::string &flag,
69 const char *fmt, const Args &...args)
70 {
71 if (!name.empty() && ignore.match(name))
72 return;
73 std::ostringstream line;
74 ccprintf(line, fmt, args...);
75 logMessage(when, name, flag, line.str());
76 }
77
78 /** Dump a block of data of length len */
79 void dump(Tick when, const std::string &name,
80 const void *d, int len, const std::string &flag);
81
82 /** Log formatted message */
83 virtual void logMessage(Tick when, const std::string &name,
84 const std::string &flag, const std::string &message) = 0;
85
86 /** Return an ostream that can be used to send messages to
87 * the 'same place' as formatted logMessage messages. This
88 * can be implemented to use a logger's underlying ostream,
89 * to provide an ostream which formats the output in some
90 * way, or just set to one of std::cout, std::cerr */
91 virtual std::ostream &getOstream() = 0;
92
93 /** Set objects to ignore */
94 void setIgnore(ObjectMatch &ignore_) { ignore = ignore_; }
95
96 /** Add objects to ignore */
97 void addIgnore(const ObjectMatch &ignore_) { ignore.add(ignore_); }
98
99 virtual ~Logger() { }
100 };
101
102 /** Logging wrapper for ostreams with the format:
103 * <when>: <name>: <message-body> */
104 class OstreamLogger : public Logger
105 {
106 protected:
107 std::ostream &stream;
108
109 public:
110 OstreamLogger(std::ostream &stream_) : stream(stream_)
111 { }
112
113 void logMessage(Tick when, const std::string &name,
114 const std::string &flag, const std::string &message) override;
115
116 std::ostream &getOstream() override { return stream; }
117 };
118
119 /** Get the current global debug logger. This takes ownership of the given
120 * logger which should be allocated using 'new' */
121 Logger *getDebugLogger();
122
123 /** Get the ostream from the current global logger */
124 std::ostream &output();
125
126 /** Delete the current global logger and assign a new one */
127 void setDebugLogger(Logger *logger);
128
129 /** Enable/disable debug logging */
130 void enable();
131 void disable();
132
133 } // namespace Trace
134
135 // This silly little class allows us to wrap a string in a functor
136 // object so that we can give a name() that DPRINTF will like
137 struct StringWrap
138 {
139 std::string str;
140 StringWrap(const std::string &s) : str(s) {}
141 const std::string &operator()() const { return str; }
142 };
143
144 // Return the global context name "global". This function gets called when
145 // the DPRINTF macros are used in a context without a visible name() function
146 const std::string &name();
147
148 // Interface for things with names. (cf. SimObject but without other
149 // functionality). This is useful when using DPRINTF
150 class Named
151 {
152 protected:
153 const std::string _name;
154
155 public:
156 Named(const std::string &name_) : _name(name_) { }
157
158 public:
159 const std::string &name() const { return _name; }
160 };
161
162 /**
163 * DPRINTF is a debugging trace facility that allows one to
164 * selectively enable tracing statements. To use DPRINTF, there must
165 * be a function or functor called name() that returns a const
166 * std::string & in the current scope.
167 *
168 * If you desire that the automatic printing not occur, use DPRINTFR
169 * (R for raw)
170 *
171 * \def DDUMP(x, data, count)
172 * \def DPRINTF(x, ...)
173 * \def DPRINTFS(x, s, ...)
174 * \def DPRINTFR(x, ...)
175 * \def DDUMPN(data, count)
176 * \def DPRINTFN(...)
177 * \def DPRINTFNR(...)
178 * \def DPRINTF_UNCONDITIONAL(x, ...)
179 *
180 * @ingroup api_trace
181 * @{
182 */
183
184 #if TRACING_ON
185
186 #define DDUMP(x, data, count) do { \
187 using namespace Debug; \
188 if (M5_UNLIKELY(DTRACE(x))) \
189 Trace::getDebugLogger()->dump( \
190 curTick(), name(), data, count, #x); \
191 } while (0)
192
193 #define DPRINTF(x, ...) do { \
194 using namespace Debug; \
195 if (M5_UNLIKELY(DTRACE(x))) { \
196 Trace::getDebugLogger()->dprintf_flag( \
197 curTick(), name(), #x, __VA_ARGS__); \
198 } \
199 } while (0)
200
201 #define DPRINTFS(x, s, ...) do { \
202 using namespace Debug; \
203 if (M5_UNLIKELY(DTRACE(x))) { \
204 Trace::getDebugLogger()->dprintf_flag( \
205 curTick(), s->name(), #x, __VA_ARGS__); \
206 } \
207 } while (0)
208
209 #define DPRINTFR(x, ...) do { \
210 using namespace Debug; \
211 if (M5_UNLIKELY(DTRACE(x))) { \
212 Trace::getDebugLogger()->dprintf_flag( \
213 (Tick)-1, std::string(), #x, __VA_ARGS__); \
214 } \
215 } while (0)
216
217 #define DDUMPN(data, count) do { \
218 Trace::getDebugLogger()->dump(curTick(), name(), data, count); \
219 } while (0)
220
221 #define DPRINTFN(...) do { \
222 Trace::getDebugLogger()->dprintf(curTick(), name(), __VA_ARGS__); \
223 } while (0)
224
225 #define DPRINTFNR(...) do { \
226 Trace::getDebugLogger()->dprintf((Tick)-1, std::string(), __VA_ARGS__); \
227 } while (0)
228
229 #define DPRINTF_UNCONDITIONAL(x, ...) do { \
230 Trace::getDebugLogger()->dprintf_flag( \
231 curTick(), name(), #x, __VA_ARGS__); \
232 } while (0)
233
234 #else // !TRACING_ON
235
236 #define DDUMP(x, data, count) do {} while (0)
237 #define DPRINTF(x, ...) do {} while (0)
238 #define DPRINTFS(x, ...) do {} while (0)
239 #define DPRINTFR(...) do {} while (0)
240 #define DDUMPN(data, count) do {} while (0)
241 #define DPRINTFN(...) do {} while (0)
242 #define DPRINTFNR(...) do {} while (0)
243 #define DPRINTF_UNCONDITIONAL(x, ...) do {} while (0)
244
245 #endif // TRACING_ON
246
247 /** @} */ // end of api_trace
248
249 #endif // __BASE_TRACE_HH__