gem5  v20.1.0.0
trace.hh
Go to the documentation of this file.
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 <string>
36 
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"
42 
43 namespace Trace {
44 
47 class Logger
48 {
49  protected:
52 
53  public:
55  template <typename ...Args>
56  void dprintf(Tick when, const std::string &name, const char *fmt,
57  const Args &...args)
58  {
59  dprintf_flag(when, name, "", fmt, args...);
60  }
61 
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)
67  {
68  if (!name.empty() && ignore.match(name))
69  return;
70  std::ostringstream line;
71  ccprintf(line, fmt, args...);
72  logMessage(when, name, flag, line.str());
73  }
74 
76  void dump(Tick when, const std::string &name,
77  const void *d, int len, const std::string &flag);
78 
80  virtual void logMessage(Tick when, const std::string &name,
81  const std::string &flag, const std::string &message) = 0;
82 
88  virtual std::ostream &getOstream() = 0;
89 
91  void setIgnore(ObjectMatch &ignore_) { ignore = ignore_; }
92 
94  void addIgnore(const ObjectMatch &ignore_) { ignore.add(ignore_); }
95 
96  virtual ~Logger() { }
97 };
98 
101 class OstreamLogger : public Logger
102 {
103  protected:
104  std::ostream &stream;
105 
106  public:
107  OstreamLogger(std::ostream &stream_) : stream(stream_)
108  { }
109 
110  void logMessage(Tick when, const std::string &name,
111  const std::string &flag, const std::string &message) override;
112 
113  std::ostream &getOstream() override { return stream; }
114 };
115 
119 
121 std::ostream &output();
122 
124 void setDebugLogger(Logger *logger);
125 
127 void enable();
128 void disable();
129 
130 } // namespace Trace
131 
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
135 {
136  std::string str;
137  StringWrap(const std::string &s) : str(s) {}
138  const std::string &operator()() const { return str; }
139 };
140 
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();
144 
145 // Interface for things with names. (cf. SimObject but without other
146 // functionality). This is useful when using DPRINTF
147 class Named
148 {
149  protected:
150  const std::string _name;
151 
152  public:
153  Named(const std::string &name_) : _name(name_) { }
154 
155  public:
156  const std::string &name() const { return _name; }
157 };
158 
181 #if TRACING_ON
182 
183 #define DDUMP(x, data, count) do { \
184  using namespace Debug; \
185  if (DTRACE(x)) \
186  Trace::getDebugLogger()->dump( \
187  curTick(), name(), data, count, #x); \
188 } while (0)
189 
190 #define DPRINTF(x, ...) do { \
191  using namespace Debug; \
192  if (DTRACE(x)) { \
193  Trace::getDebugLogger()->dprintf_flag( \
194  curTick(), name(), #x, __VA_ARGS__); \
195  } \
196 } while (0)
197 
198 #define DPRINTFS(x, s, ...) do { \
199  using namespace Debug; \
200  if (DTRACE(x)) { \
201  Trace::getDebugLogger()->dprintf_flag( \
202  curTick(), s->name(), #x, __VA_ARGS__); \
203  } \
204 } while (0)
205 
206 #define DPRINTFR(x, ...) do { \
207  using namespace Debug; \
208  if (DTRACE(x)) { \
209  Trace::getDebugLogger()->dprintf_flag( \
210  (Tick)-1, std::string(), #x, __VA_ARGS__); \
211  } \
212 } while (0)
213 
214 #define DDUMPN(data, count) do { \
215  Trace::getDebugLogger()->dump(curTick(), name(), data, count); \
216 } while (0)
217 
218 #define DPRINTFN(...) do { \
219  Trace::getDebugLogger()->dprintf(curTick(), name(), __VA_ARGS__); \
220 } while (0)
221 
222 #define DPRINTFNR(...) do { \
223  Trace::getDebugLogger()->dprintf((Tick)-1, std::string(), __VA_ARGS__); \
224 } while (0)
225 
226 #define DPRINTF_UNCONDITIONAL(x, ...) do { \
227  Trace::getDebugLogger()->dprintf_flag( \
228  curTick(), name(), #x, __VA_ARGS__); \
229 } while (0)
230 
231 #else // !TRACING_ON
232 
233 #define DDUMP(x, data, count) do {} while (0)
234 #define DPRINTF(x, ...) do {} while (0)
235 #define DPRINTFS(x, ...) do {} while (0)
236 #define DPRINTFR(...) do {} while (0)
237 #define DDUMPN(data, count) do {} while (0)
238 #define DPRINTFN(...) do {} while (0)
239 #define DPRINTFNR(...) do {} while (0)
240 #define DPRINTF_UNCONDITIONAL(x, ...) do {} while (0)
241 
242 #endif // TRACING_ON
243  // end of api_trace
245 
246 #endif // __BASE_TRACE_HH__
Trace
Definition: nativetrace.cc:52
ObjectMatch::match
bool match(const std::string &name) const
Definition: match.hh:66
Tick
uint64_t Tick
Tick count type.
Definition: types.hh:63
Trace::getDebugLogger
Logger * getDebugLogger()
Get the current global debug logger.
Definition: trace.cc:67
Trace::Logger::getOstream
virtual std::ostream & getOstream()=0
Return an ostream that can be used to send messages to the 'same place' as formatted logMessage messa...
StringWrap::operator()
const std::string & operator()() const
Definition: trace.hh:138
Trace::Logger::dprintf_flag
void dprintf_flag(Tick when, const std::string &name, const std::string &flag, const char *fmt, const Args &...args)
Log a single message with a flag prefix.
Definition: trace.hh:64
Trace::Logger::setIgnore
void setIgnore(ObjectMatch &ignore_)
Set objects to ignore.
Definition: trace.hh:91
Trace::setDebugLogger
void setDebugLogger(Logger *logger)
Delete the current global logger and assign a new one.
Definition: trace.cc:83
Trace::Logger::~Logger
virtual ~Logger()
Definition: trace.hh:96
Trace::disable
void disable()
Definition: trace.cc:98
StringWrap::StringWrap
StringWrap(const std::string &s)
Definition: trace.hh:137
Trace::Logger::dump
void dump(Tick when, const std::string &name, const void *d, int len, const std::string &flag)
Dump a block of data of length len.
Definition: trace.cc:107
Trace::enable
void enable()
Enable/disable debug logging.
Definition: trace.cc:92
match.hh
Trace::Logger
Debug logging base class.
Definition: trace.hh:47
Trace::OstreamLogger::logMessage
void logMessage(Tick when, const std::string &name, const std::string &flag, const std::string &message) override
Log formatted message.
Definition: trace.cc:147
ArmISA::d
Bitfield< 9 > d
Definition: miscregs_types.hh:60
Trace::OstreamLogger
Logging wrapper for ostreams with the format: <when>: <name>: <message-body>
Definition: trace.hh:101
Trace::Logger::logMessage
virtual void logMessage(Tick when, const std::string &name, const std::string &flag, const std::string &message)=0
Log formatted message.
debug.hh
Trace::OstreamLogger::getOstream
std::ostream & getOstream() override
Return an ostream that can be used to send messages to the 'same place' as formatted logMessage messa...
Definition: trace.hh:113
cprintf.hh
Trace::output
std::ostream & output()
Get the ostream from the current global logger.
Definition: trace.cc:77
Trace::Logger::addIgnore
void addIgnore(const ObjectMatch &ignore_)
Add objects to ignore.
Definition: trace.hh:94
core.hh
Trace::OstreamLogger::stream
std::ostream & stream
Definition: trace.hh:104
Named
Definition: trace.hh:147
Named::Named
Named(const std::string &name_)
Definition: trace.hh:153
StringWrap::str
std::string str
Definition: trace.hh:136
types.hh
ArmISA::len
Bitfield< 18, 16 > len
Definition: miscregs_types.hh:439
Trace::Logger::ignore
ObjectMatch ignore
Name match for objects to ignore.
Definition: trace.hh:51
ccprintf
void ccprintf(cp::Print &print)
Definition: cprintf.hh:127
Logger
Definition: logging.hh:51
Named::_name
const std::string _name
Definition: trace.hh:150
StringWrap
Definition: trace.hh:134
ObjectMatch::add
void add(const ObjectMatch &other)
Definition: match.cc:46
Trace::Logger::dprintf
void dprintf(Tick when, const std::string &name, const char *fmt, const Args &...args)
Log a single message.
Definition: trace.hh:56
name
const std::string & name()
Definition: trace.cc:50
Named::name
const std::string & name() const
Definition: trace.hh:156
ArmISA::s
Bitfield< 4 > s
Definition: miscregs_types.hh:556
Trace::OstreamLogger::OstreamLogger
OstreamLogger(std::ostream &stream_)
Definition: trace.hh:107
ObjectMatch
ObjectMatch contains a vector of expressions.
Definition: match.hh:53

Generated on Wed Sep 30 2020 14:02:08 for gem5 by doxygen 1.8.17