gem5  [DEVELOP-FOR-23.0]
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
trace.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, 2019, 2021 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/logging.hh"
43 #include "base/match.hh"
44 #include "base/types.hh"
45 #include "sim/cur_tick.hh"
46 
47 // Return the global context name "global". This function gets called when
48 // the DPRINTF macros are used in a context without a visible name() function
49 // @todo This should be moved to the gem5 namespace
50 const std::string &name();
51 
52 namespace gem5
53 {
54 
55 namespace trace {
56 
59 class Logger
60 {
61  protected:
66 
67  bool isEnabled(const std::string &name) const
68  {
69  if (name.empty()) // Enable the logger with a empty name.
70  return true;
71  bool ignore_match = ignore.match(name);
72  bool activate_match = activate.match(name);
73  if (ignore_match && activate_match)
74  panic("%s in both ignore and activate.\n", name);
75  if (ignore_match)
76  return false;
77  if (!activate.empty() && !activate_match)
78  return false;
79  return true;
80  }
81 
82  public:
84  template <typename ...Args>
85  void dprintf(Tick when, const std::string &name, const char *fmt,
86  const Args &...args)
87  {
88  dprintf_flag(when, name, "", fmt, args...);
89  }
90 
92  template <typename ...Args>
93  void dprintf_flag(Tick when, const std::string &name,
94  const std::string &flag,
95  const char *fmt, const Args &...args)
96  {
97  if (!isEnabled(name))
98  return;
99  std::ostringstream line;
100  ccprintf(line, fmt, args...);
101  logMessage(when, name, flag, line.str());
102  }
103 
105  void dump(Tick when, const std::string &name,
106  const void *d, int len, const std::string &flag);
107 
109  virtual void logMessage(Tick when, const std::string &name,
110  const std::string &flag, const std::string &message) = 0;
111 
117  virtual std::ostream &getOstream() = 0;
118 
120  void setIgnore(ObjectMatch &ignore_) { ignore = ignore_; }
121 
123  void addIgnore(const ObjectMatch &ignore_) { ignore.add(ignore_); }
124 
126  void setActivate(ObjectMatch &activate_) { activate = activate_; }
127 
129  void addActivate(const ObjectMatch &activate_) { activate.add(activate_); }
130 
131  virtual ~Logger() { }
132 };
133 
136 class OstreamLogger : public Logger
137 {
138  protected:
139  std::ostream &stream;
140 
141  public:
142  OstreamLogger(std::ostream &stream_) : stream(stream_)
143  { }
144 
145  void logMessage(Tick when, const std::string &name,
146  const std::string &flag, const std::string &message) override;
147 
148  std::ostream &getOstream() override { return stream; }
149 };
150 
154 
156 std::ostream &output();
157 
159 void setDebugLogger(Logger *logger);
160 
162 void enable();
163 void disable();
164 
165 } // namespace trace
166 
167 // This silly little class allows us to wrap a string in a functor
168 // object so that we can give a name() that DPRINTF will like
170 {
171  std::string str;
172  StringWrap(const std::string &s) : str(s) {}
173  const std::string &operator()() const { return str; }
174 };
175 
204 #define DDUMP(x, data, count) do { \
205  if (GEM5_UNLIKELY(TRACING_ON && ::gem5::debug::x)) \
206  ::gem5::trace::getDebugLogger()->dump( \
207  ::gem5::curTick(), name(), data, count, #x); \
208 } while (0)
209 
210 #define DPRINTF(x, ...) do { \
211  if (GEM5_UNLIKELY(TRACING_ON && ::gem5::debug::x)) { \
212  ::gem5::trace::getDebugLogger()->dprintf_flag( \
213  ::gem5::curTick(), name(), #x, __VA_ARGS__); \
214  } \
215 } while (0)
216 
217 #define DPRINTFS(x, s, ...) do { \
218  if (GEM5_UNLIKELY(TRACING_ON && ::gem5::debug::x)) { \
219  ::gem5::trace::getDebugLogger()->dprintf_flag( \
220  ::gem5::curTick(), (s)->name(), #x, __VA_ARGS__); \
221  } \
222 } while (0)
223 
224 #define DPRINTFR(x, ...) do { \
225  if (GEM5_UNLIKELY(TRACING_ON && ::gem5::debug::x)) { \
226  ::gem5::trace::getDebugLogger()->dprintf_flag( \
227  (::gem5::Tick)-1, std::string(), #x, __VA_ARGS__); \
228  } \
229 } while (0)
230 
231 #define DPRINTFV(x, ...) do { \
232  if (GEM5_UNLIKELY(TRACING_ON && (x))) { \
233  ::gem5::trace::getDebugLogger()->dprintf_flag( \
234  ::gem5::curTick(), name(), x.name(), __VA_ARGS__); \
235  } \
236 } while (0)
237 
238 #define DPRINTFN(...) do { \
239  if (TRACING_ON) { \
240  ::gem5::trace::getDebugLogger()->dprintf( \
241  ::gem5::curTick(), name(), __VA_ARGS__); \
242  } \
243 } while (0)
244 
245 #define DPRINTFNR(...) do { \
246  if (TRACING_ON) { \
247  ::gem5::trace::getDebugLogger()->dprintf( \
248  (::gem5::Tick)-1, "", __VA_ARGS__); \
249  } \
250 } while (0)
251 
252 #define DPRINTF_UNCONDITIONAL(x, ...) \
253  GEM5_DEPRECATED_MACRO_STMT(DPRINTF_UNCONDITIONAL, \
254  do { \
255  if (TRACING_ON) { \
256  ::gem5::trace::getDebugLogger()->dprintf_flag( \
257  ::gem5::curTick(), name(), #x, __VA_ARGS__); \
258  } \
259  } while (0), \
260  "Use DPRINTFN or DPRINTF with a debug flag instead.")
261  // end of api_trace
263 
264 } // namespace gem5
265 
266 #endif // __BASE_TRACE_HH__
gem5::trace::Logger::activate
ObjectMatch activate
Name match for objects to activate log.
Definition: trace.hh:65
gem5::VegaISA::s
Bitfield< 1 > s
Definition: pagetable.hh:64
gem5::ObjectMatch
ObjectMatch contains a vector of expressions.
Definition: match.hh:56
gem5::trace::OstreamLogger::OstreamLogger
OstreamLogger(std::ostream &stream_)
Definition: trace.hh:142
gem5::Logger
Definition: logging.hh:54
gem5::ObjectMatch::match
bool match(const std::string &name) const
Definition: match.hh:73
gem5::trace::Logger::logMessage
virtual void logMessage(Tick when, const std::string &name, const std::string &flag, const std::string &message)=0
Log formatted message.
gem5::trace::Logger::~Logger
virtual ~Logger()
Definition: trace.hh:131
cur_tick.hh
gem5::trace::Logger::addActivate
void addActivate(const ObjectMatch &activate_)
Add objects to activate.
Definition: trace.hh:129
gem5::trace::Logger::setIgnore
void setIgnore(ObjectMatch &ignore_)
Set objects to ignore.
Definition: trace.hh:120
gem5::ccprintf
void ccprintf(cp::Print &print)
Definition: cprintf.hh:130
gem5::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:148
match.hh
gem5::StringWrap::operator()
const std::string & operator()() const
Definition: trace.hh:173
gem5::ArmISA::d
Bitfield< 9 > d
Definition: misc_types.hh:64
gem5::trace::Logger::isEnabled
bool isEnabled(const std::string &name) const
Definition: trace.hh:67
gem5::StringWrap
Definition: trace.hh:169
gem5::Tick
uint64_t Tick
Tick count type.
Definition: types.hh:58
gem5::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:93
len
uint16_t len
Definition: helpers.cc:62
debug.hh
gem5::trace::OstreamLogger
Logging wrapper for ostreams with the format: <when>: <name>: <message-body>
Definition: trace.hh:136
cprintf.hh
compiler.hh
gem5::trace::Logger::dprintf
void dprintf(Tick when, const std::string &name, const char *fmt, const Args &...args)
Log a single message.
Definition: trace.hh:85
gem5::trace::getDebugLogger
Logger * getDebugLogger()
Get the current global debug logger.
Definition: trace.cc:68
gem5::trace::Logger::setActivate
void setActivate(ObjectMatch &activate_)
Set objects to activate.
Definition: trace.hh:126
gem5::trace::output
std::ostream & output()
Get the ostream from the current global logger.
Definition: trace.cc:78
gem5::trace::OstreamLogger::stream
std::ostream & stream
Definition: trace.hh:139
types.hh
gem5::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:108
gem5::ObjectMatch::empty
bool empty() const
Definition: match.hh:69
logging.hh
gem5::trace::setDebugLogger
void setDebugLogger(Logger *logger)
Delete the current global logger and assign a new one.
Definition: trace.cc:84
gem5::trace::Logger::ignore
ObjectMatch ignore
Name match for objects to ignore.
Definition: trace.hh:63
gem5::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:148
gem5::trace::Logger::addIgnore
void addIgnore(const ObjectMatch &ignore_)
Add objects to ignore.
Definition: trace.hh:123
name
const std::string & name()
Definition: trace.cc:48
gem5::trace::Logger
Debug logging base class.
Definition: trace.hh:59
gem5::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...
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: gpu_translation_state.hh:37
gem5::trace::enable
void enable()
Enable/disable debug logging.
Definition: trace.cc:93
gem5::ObjectMatch::add
void add(const ObjectMatch &other)
Definition: match.cc:47
gem5::StringWrap::StringWrap
StringWrap(const std::string &s)
Definition: trace.hh:172
panic
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:188
gem5::trace::disable
void disable()
Definition: trace.cc:99
gem5::StringWrap::str
std::string str
Definition: trace.hh:171

Generated on Sun Jul 30 2023 01:56:51 for gem5 by doxygen 1.8.17