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
47namespace Trace {
48
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;
56
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;
65
66        std::ostringstream line;
67        ccprintf(line, fmt, args...);
68        logMessage(when, name, line.str());
69    }
70
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);
74
75    /** Log formatted message */
76    virtual void logMessage(Tick when, const std::string &name,
77                            const std::string &message) = 0;
78
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    /** Add objects to ignore */
90    void addIgnore(const ObjectMatch &ignore_) { ignore.add(ignore_); }
91
92    virtual ~Logger() { }
93};
94
95/** Logging wrapper for ostreams with the format:
96 *  <when>: <name>: <message-body> */
97class OstreamLogger : public Logger
98{
99  protected:
100    std::ostream &stream;
101
102  public:
103    OstreamLogger(std::ostream &stream_) : stream(stream_)
104    { }
105
106    void logMessage(Tick when, const std::string &name,
107                    const std::string &message) override;
108
109    std::ostream &getOstream() override { return stream; }
110};
111
112/** Get the current global debug logger.  This takes ownership of the given
113 *  logger which should be allocated using 'new' */
114Logger *getDebugLogger();
115
116/** Get the ostream from the current global logger */
117std::ostream &output();
118
119/** Delete the current global logger and assign a new one */
120void setDebugLogger(Logger *logger);
121
122/** Enable/disable debug logging */
123void enable();
124void disable();
125
126} // namespace Trace
127
128// This silly little class allows us to wrap a string in a functor
129// object so that we can give a name() that DPRINTF will like
130struct StringWrap
131{
132    std::string str;
133    StringWrap(const std::string &s) : str(s) {}
134    const std::string &operator()() const { return str; }
135};
136
137// Return the global context name "global".  This function gets called when
138// the DPRINTF macros are used in a context without a visible name() function
139const std::string &name();
140
141// Interface for things with names. (cf. SimObject but without other
142// functionality).  This is useful when using DPRINTF
143class Named
144{
145  protected:
146    const std::string _name;
147
148  public:
149    Named(const std::string &name_) : _name(name_) { }
150
151  public:
152    const std::string &name() const { return _name; }
153};
154
155//
156// DPRINTF is a debugging trace facility that allows one to
157// selectively enable tracing statements.  To use DPRINTF, there must
158// be a function or functor called name() that returns a const
159// std::string & in the current scope.
160//
161// If you desire that the automatic printing not occur, use DPRINTFR
162// (R for raw)
163//
164
165#if TRACING_ON
166
167#define DTRACE(x) (Debug::x)
168
169#define DDUMP(x, data, count) do {                                        \
170    using namespace Debug;                                                \
171    if (DTRACE(x))                                                        \
172        Trace::getDebugLogger()->dump(curTick(), name(), data, count);    \
173} while (0)
174
175#define DPRINTF(x, ...) do {                                              \
176    using namespace Debug;                                                \
177    if (DTRACE(x)) {                                                      \
178        Trace::getDebugLogger()->dprintf(curTick(), name(),               \
179            __VA_ARGS__);                                                 \
180    }                                                                     \
181} while (0)
182
183#define DPRINTFS(x, s, ...) do {                                          \
184    using namespace Debug;                                                \
185    if (DTRACE(x)) {                                                      \
186        Trace::getDebugLogger()->dprintf(curTick(), s->name(),            \
187            __VA_ARGS__);                                                 \
188    }                                                                     \
189} while (0)
190
191#define DPRINTFR(x, ...) do {                                             \
192    using namespace Debug;                                                \
193    if (DTRACE(x)) {                                                      \
194        Trace::getDebugLogger()->dprintf((Tick)-1, std::string(),         \
195            __VA_ARGS__);                                                 \
196    }                                                                     \
197} while (0)
198
199#define DDUMPN(data, count) do {                                          \
200    Trace::getDebugLogger()->dump(curTick(), name(), data, count);        \
201} while (0)
202
203#define DPRINTFN(...) do {                                                \
204    Trace::getDebugLogger()->dprintf(curTick(), name(), __VA_ARGS__);     \
205} while (0)
206
207#define DPRINTFNR(...) do {                                               \
208    Trace::getDebugLogger()->dprintf((Tick)-1, string(), __VA_ARGS__);    \
209} while (0)
210
211#else // !TRACING_ON
212
213#define DTRACE(x) (false)
214#define DDUMP(x, data, count) do {} while (0)
215#define DPRINTF(x, ...) do {} while (0)
216#define DPRINTFS(x, ...) do {} while (0)
217#define DPRINTFR(...) do {} while (0)
218#define DDUMPN(data, count) do {} while (0)
219#define DPRINTFN(...) do {} while (0)
220#define DPRINTFNR(...) do {} while (0)
221
222#endif  // TRACING_ON
223
224#endif // __BASE_TRACE_HH__
225