trace.hh (10292:933dfb9d8279) trace.hh (10475:5744891a444b)
1/*
2 * Copyright (c) 2014 ARM Limited
3 * All rights reserved
4 *
1/*
2 * Copyright (c) 2014 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2001-2006 The Regents of The University of Michigan
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright

--- 12 unchanged lines hidden (view full) ---

34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * Authors: Nathan Binkert
41 * Steve Reinhardt
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

--- 12 unchanged lines hidden (view full) ---

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
42 */
43
44#ifndef __BASE_TRACE_HH__
45#define __BASE_TRACE_HH__
46
47#include <string>
48
49#include "base/cprintf.hh"
50#include "base/debug.hh"
51#include "base/match.hh"
52#include "base/types.hh"
53#include "sim/core.hh"
54
55namespace Trace {
56
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
47namespace Trace {
48
57using Debug::SimpleFlag;
58using Debug::CompoundFlag;
49/** Debug logging base class. Handles formatting and outputting
50 * time/name/message messages */
51class Logger
52{
53 protected:
54 /** Name match for objects to ignore */
55 ObjectMatch ignore;
59
56
60std::ostream &output();
61void setOutput(const std::string &filename);
57 public:
58 /** Log a single message */
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;
62
65
63extern bool enabled;
64bool changeFlag(const char *str, bool value);
65void dumpStatus();
66 std::ostringstream line;
67 ccprintf(line, fmt, args...);
68 logMessage(when, name, line.str());
69 }
66
70
67extern ObjectMatch ignore;
68extern const std::string DefaultName;
71 /** Dump a block of data of length len */
72 virtual void dump(Tick when, const std::string &name,
73 const void *d, int len);
69
74
70bool __dprintf_prologue(Tick when, const std::string &name);
75 /** Log formatted message */
76 virtual void logMessage(Tick when, const std::string &name,
77 const std::string &message) = 0;
71
78
72template<typename ...Args> void
73dprintf(Tick when, const std::string &name, const char *format,
74 const Args &...args)
79 /** Return an ostream that can be used to send messages to
80 * the 'same place' as formatted logMessage messages. This
81 * can be implemented to use a logger's underlying ostream,
82 * to provide an ostream which formats the output in some
83 * way, or just set to one of std::cout, std::cerr */
84 virtual std::ostream &getOstream() = 0;
85
86 /** Set objects to ignore */
87 void setIgnore(ObjectMatch &ignore_) { ignore = ignore_; }
88
89 virtual ~Logger() { }
90};
91
92/** Logging wrapper for ostreams with the format:
93 * <when>: <name>: <message-body> */
94class OstreamLogger : public Logger
75{
95{
76 if (!__dprintf_prologue(when, name))
77 return;
96 protected:
97 std::ostream &stream;
78
98
79 std::ostream &os(output());
80 ccprintf(os, format, args...);
81 os.flush();
82}
99 public:
100 OstreamLogger(std::ostream &stream_) : stream(stream_)
101 { }
83
102
84void dump(Tick when, const std::string &name, const void *data, int len);
103 void logMessage(Tick when, const std::string &name,
104 const std::string &message) M5_ATTR_OVERRIDE;
85
105
106 std::ostream &getOstream() M5_ATTR_OVERRIDE { return stream; }
107};
108
109/** Get the current global debug logger. This takes ownership of the given
110 * logger which should be allocated using 'new' */
111Logger *getDebugLogger();
112
113/** Get the ostream from the current global logger */
114std::ostream &output();
115
116/** Delete the current global logger and assign a new one */
117void setDebugLogger(Logger *logger);
118
119/** Enable debug logging */
120extern bool enabled;
121
86} // namespace Trace
87
88// This silly little class allows us to wrap a string in a functor
89// object so that we can give a name() that DPRINTF will like
90struct StringWrap
91{
92 std::string str;
93 StringWrap(const std::string &s) : str(s) {}
94 const std::string &operator()() const { return str; }
95};
96
122} // namespace Trace
123
124// This silly little class allows us to wrap a string in a functor
125// object so that we can give a name() that DPRINTF will like
126struct StringWrap
127{
128 std::string str;
129 StringWrap(const std::string &s) : str(s) {}
130 const std::string &operator()() const { return str; }
131};
132
97inline const std::string &name() { return Trace::DefaultName; }
133// Return the global context name "global". This function gets called when
134// the DPRINTF macros are used in a context without a visible name() function
135const std::string &name();
98
99// Interface for things with names. (cf. SimObject but without other
100// functionality). This is useful when using DPRINTF
101class Named
102{
103 protected:
104 const std::string _name;
105

--- 13 unchanged lines hidden (view full) ---

119// If you desire that the automatic printing not occur, use DPRINTFR
120// (R for raw)
121//
122
123#if TRACING_ON
124
125#define DTRACE(x) ((Debug::x) && Trace::enabled)
126
136
137// Interface for things with names. (cf. SimObject but without other
138// functionality). This is useful when using DPRINTF
139class Named
140{
141 protected:
142 const std::string _name;
143

--- 13 unchanged lines hidden (view full) ---

157// If you desire that the automatic printing not occur, use DPRINTFR
158// (R for raw)
159//
160
161#if TRACING_ON
162
163#define DTRACE(x) ((Debug::x) && Trace::enabled)
164
127#define DDUMP(x, data, count) do { \
128 using namespace Debug; \
129 if (DTRACE(x)) \
130 Trace::dump(curTick(), name(), data, count); \
165#define DDUMP(x, data, count) do { \
166 using namespace Debug; \
167 if (DTRACE(x)) \
168 Trace::getDebugLogger()->dump(curTick(), name(), data, count); \
131} while (0)
132
169} while (0)
170
133#define DPRINTF(x, ...) do { \
134 using namespace Debug; \
135 if (DTRACE(x)) \
136 Trace::dprintf(curTick(), name(), __VA_ARGS__); \
171#define DPRINTF(x, ...) do { \
172 using namespace Debug; \
173 if (DTRACE(x)) { \
174 Trace::getDebugLogger()->dprintf(curTick(), name(), \
175 __VA_ARGS__); \
176 } \
137} while (0)
138
177} while (0)
178
139#define DPRINTFS(x, s, ...) do { \
140 using namespace Debug; \
141 if (DTRACE(x)) \
142 Trace::dprintf(curTick(), s->name(), __VA_ARGS__); \
179#define DPRINTFS(x, s, ...) do { \
180 using namespace Debug; \
181 if (DTRACE(x)) { \
182 Trace::getDebugLogger()->dprintf(curTick(), s->name(), \
183 __VA_ARGS__); \
184 } \
143} while (0)
144
185} while (0)
186
145#define DPRINTFR(x, ...) do { \
146 using namespace Debug; \
147 if (DTRACE(x)) \
148 Trace::dprintf((Tick)-1, std::string(), __VA_ARGS__); \
187#define DPRINTFR(x, ...) do { \
188 using namespace Debug; \
189 if (DTRACE(x)) { \
190 Trace::getDebugLogger()->dprintf((Tick)-1, std::string(), \
191 __VA_ARGS__); \
192 } \
149} while (0)
150
193} while (0)
194
151#define DDUMPN(data, count) do { \
152 Trace::dump(curTick(), name(), data, count); \
195#define DDUMPN(data, count) do { \
196 Trace::getDebugLogger()->dump(curTick(), name(), data, count); \
153} while (0)
154
197} while (0)
198
155#define DPRINTFN(...) do { \
156 Trace::dprintf(curTick(), name(), __VA_ARGS__); \
199#define DPRINTFN(...) do { \
200 Trace::getDebugLogger()->dprintf(curTick(), name(), __VA_ARGS__); \
157} while (0)
158
201} while (0)
202
159#define DPRINTFNR(...) do { \
160 Trace::dprintf((Tick)-1, string(), __VA_ARGS__); \
203#define DPRINTFNR(...) do { \
204 Trace::getDebugLogger()->dprintf((Tick)-1, string(), __VA_ARGS__); \
161} while (0)
162
163#else // !TRACING_ON
164
165#define DTRACE(x) (false)
166#define DDUMP(x, data, count) do {} while (0)
167#define DPRINTF(x, ...) do {} while (0)
168#define DPRINTFS(x, ...) do {} while (0)
169#define DPRINTFR(...) do {} while (0)
170#define DDUMPN(data, count) do {} while (0)
171#define DPRINTFN(...) do {} while (0)
172#define DPRINTFNR(...) do {} while (0)
173
174#endif // TRACING_ON
175
176#endif // __BASE_TRACE_HH__
205} while (0)
206
207#else // !TRACING_ON
208
209#define DTRACE(x) (false)
210#define DDUMP(x, data, count) do {} while (0)
211#define DPRINTF(x, ...) do {} while (0)
212#define DPRINTFS(x, ...) do {} while (0)
213#define DPRINTFR(...) do {} while (0)
214#define DDUMPN(data, count) do {} while (0)
215#define DPRINTFN(...) do {} while (0)
216#define DPRINTFNR(...) do {} while (0)
217
218#endif // TRACING_ON
219
220#endif // __BASE_TRACE_HH__