gem5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
trace.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 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  * Authors: Nathan Binkert
32  * Steve Reinhardt
33  * Andrew Bardsley
34  */
35 
36 #ifndef __BASE_TRACE_HH__
37 #define __BASE_TRACE_HH__
38 
39 #include <string>
40 
41 #include "base/cprintf.hh"
42 #include "base/debug.hh"
43 #include "base/match.hh"
44 #include "base/types.hh"
45 #include "sim/core.hh"
46 
47 namespace Trace {
48 
51 class Logger
52 {
53  protected:
56 
57  public:
59  template <typename ...Args>
60  void dprintf(Tick when, const std::string &name, const char *fmt,
61  const Args &...args)
62  {
63  if (!name.empty() && ignore.match(name))
64  return;
65 
66  std::ostringstream line;
67  ccprintf(line, fmt, args...);
68  logMessage(when, name, line.str());
69  }
70 
72  virtual void dump(Tick when, const std::string &name,
73  const void *d, int len);
74 
76  virtual void logMessage(Tick when, const std::string &name,
77  const std::string &message) = 0;
78 
84  virtual std::ostream &getOstream() = 0;
85 
87  void setIgnore(ObjectMatch &ignore_) { ignore = ignore_; }
88 
89  virtual ~Logger() { }
90 };
91 
94 class OstreamLogger : public Logger
95 {
96  protected:
97  std::ostream &stream;
98 
99  public:
100  OstreamLogger(std::ostream &stream_) : stream(stream_)
101  { }
102 
103  void logMessage(Tick when, const std::string &name,
104  const std::string &message) override;
105 
106  std::ostream &getOstream() override { return stream; }
107 };
108 
112 
114 std::ostream &output();
115 
117 void setDebugLogger(Logger *logger);
118 
120 void enable();
121 void disable();
122 
123 } // namespace Trace
124 
125 // This silly little class allows us to wrap a string in a functor
126 // object so that we can give a name() that DPRINTF will like
128 {
129  std::string str;
130  StringWrap(const std::string &s) : str(s) {}
131  const std::string &operator()() const { return str; }
132 };
133 
134 // Return the global context name "global". This function gets called when
135 // the DPRINTF macros are used in a context without a visible name() function
136 const std::string &name();
137 
138 // Interface for things with names. (cf. SimObject but without other
139 // functionality). This is useful when using DPRINTF
140 class Named
141 {
142  protected:
143  const std::string _name;
144 
145  public:
146  Named(const std::string &name_) : _name(name_) { }
147 
148  public:
149  const std::string &name() const { return _name; }
150 };
151 
152 //
153 // DPRINTF is a debugging trace facility that allows one to
154 // selectively enable tracing statements. To use DPRINTF, there must
155 // be a function or functor called name() that returns a const
156 // std::string & in the current scope.
157 //
158 // If you desire that the automatic printing not occur, use DPRINTFR
159 // (R for raw)
160 //
161 
162 #if TRACING_ON
163 
164 #define DTRACE(x) (Debug::x)
165 
166 #define DDUMP(x, data, count) do { \
167  using namespace Debug; \
168  if (DTRACE(x)) \
169  Trace::getDebugLogger()->dump(curTick(), name(), data, count); \
170 } while (0)
171 
172 #define DPRINTF(x, ...) do { \
173  using namespace Debug; \
174  if (DTRACE(x)) { \
175  Trace::getDebugLogger()->dprintf(curTick(), name(), \
176  __VA_ARGS__); \
177  } \
178 } while (0)
179 
180 #define DPRINTFS(x, s, ...) do { \
181  using namespace Debug; \
182  if (DTRACE(x)) { \
183  Trace::getDebugLogger()->dprintf(curTick(), s->name(), \
184  __VA_ARGS__); \
185  } \
186 } while (0)
187 
188 #define DPRINTFR(x, ...) do { \
189  using namespace Debug; \
190  if (DTRACE(x)) { \
191  Trace::getDebugLogger()->dprintf((Tick)-1, std::string(), \
192  __VA_ARGS__); \
193  } \
194 } while (0)
195 
196 #define DDUMPN(data, count) do { \
197  Trace::getDebugLogger()->dump(curTick(), name(), data, count); \
198 } while (0)
199 
200 #define DPRINTFN(...) do { \
201  Trace::getDebugLogger()->dprintf(curTick(), name(), __VA_ARGS__); \
202 } while (0)
203 
204 #define DPRINTFNR(...) do { \
205  Trace::getDebugLogger()->dprintf((Tick)-1, string(), __VA_ARGS__); \
206 } while (0)
207 
208 #else // !TRACING_ON
209 
210 #define DTRACE(x) (false)
211 #define DDUMP(x, data, count) do {} while (0)
212 #define DPRINTF(x, ...) do {} while (0)
213 #define DPRINTFS(x, ...) do {} while (0)
214 #define DPRINTFR(...) do {} while (0)
215 #define DDUMPN(data, count) do {} while (0)
216 #define DPRINTFN(...) do {} while (0)
217 #define DPRINTFNR(...) do {} while (0)
218 
219 #endif // TRACING_ON
220 
221 #endif // __BASE_TRACE_HH__
Definition: misc.hh:60
void ccprintf(cp::Print &print)
Definition: cprintf.hh:130
const std::string & name() const
Definition: trace.hh:149
std::ostream & stream
Definition: trace.hh:97
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:106
bool match(const std::string &name) const
Definition: match.hh:52
StringWrap(const std::string &s)
Definition: trace.hh:130
void setIgnore(ObjectMatch &ignore_)
Set objects to ignore.
Definition: trace.hh:87
const std::string & operator()() const
Definition: trace.hh:131
virtual ~Logger()
Definition: trace.hh:89
Definition: trace.hh:140
const std::string & name()
Definition: trace.cc:49
virtual std::ostream & getOstream()=0
Return an ostream that can be used to send messages to the 'same place' as formatted logMessage messa...
Bitfield< 4 > s
Definition: miscregs.hh:1738
OstreamLogger(std::ostream &stream_)
Definition: trace.hh:100
uint64_t Tick
Tick count type.
Definition: types.hh:63
Debug logging base class.
Definition: trace.hh:51
Bitfield< 9 > d
Definition: miscregs.hh:1375
ObjectMatch ignore
Name match for objects to ignore.
Definition: trace.hh:55
const std::string _name
Definition: trace.hh:143
std::ostream & output()
Get the ostream from the current global logger.
Definition: trace.cc:76
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,16,32,64}_t.
Logging wrapper for ostreams with the format: <when>: <name>: <message-body>
Definition: trace.hh:94
std::string str
Definition: trace.hh:129
Logger * getDebugLogger()
Get the current global debug logger.
Definition: trace.cc:66
Bitfield< 18, 16 > len
Definition: miscregs.hh:1626
void logMessage(Tick when, const std::string &name, const std::string &message) override
Log formatted message.
Definition: trace.cc:144
void enable()
Enable/disable debug logging.
Definition: trace.cc:91
virtual void logMessage(Tick when, const std::string &name, const std::string &message)=0
Log formatted message.
void setDebugLogger(Logger *logger)
Delete the current global logger and assign a new one.
Definition: trace.cc:82
Named(const std::string &name_)
Definition: trace.hh:146
void dprintf(Tick when, const std::string &name, const char *fmt, const Args &...args)
Log a single message.
Definition: trace.hh:60
virtual void dump(Tick when, const std::string &name, const void *d, int len)
Dump a block of data of length len.
Definition: trace.cc:105
void disable()
Definition: trace.cc:97

Generated on Fri Jun 9 2017 13:03:41 for gem5 by doxygen 1.8.6