trace.hh revision 4042:dbd98b2264ed
1/*
2 * Copyright (c) 2001-2006 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Nathan Binkert
29 *          Steve Reinhardt
30 */
31
32#ifndef __BASE_TRACE_HH__
33#define __BASE_TRACE_HH__
34
35#include <vector>
36
37#include "base/cprintf.hh"
38#include "base/match.hh"
39#include "sim/host.hh"
40#include "sim/root.hh"
41
42#include "base/traceflags.hh"
43
44namespace Trace {
45
46    typedef std::vector<bool> FlagVec;
47
48    extern FlagVec flags;
49
50    extern std::ostream *dprintf_stream;
51
52    inline bool
53    IsOn(int t)
54    {
55        return flags[t];
56    }
57
58    extern bool enabled;
59
60    void dump(const uint8_t *data, int count);
61
62    class Record
63    {
64      protected:
65        Tick cycle;
66
67        Record(Tick _cycle)
68            : cycle(_cycle)
69        {
70        }
71
72      public:
73        virtual ~Record() {}
74
75        virtual void dump(std::ostream &) = 0;
76    };
77
78    class PrintfRecord : public Record
79    {
80      private:
81        const std::string &name;
82        const char *format;
83        CPrintfArgsList args;
84
85      public:
86        PrintfRecord(Tick cycle, const std::string &_name, const char *_format,
87                     CPRINTF_DECLARATION)
88            : Record(cycle), name(_name), format(_format),
89              args(VARARGS_ALLARGS)
90        {
91        }
92
93        virtual ~PrintfRecord();
94
95        virtual void dump(std::ostream &);
96    };
97
98    class DataRecord : public Record
99    {
100      private:
101        const std::string &name;
102        uint8_t *data;
103        int len;
104
105      public:
106        DataRecord(Tick cycle, const std::string &name,
107                   const void *_data, int _len);
108        virtual ~DataRecord();
109
110        virtual void dump(std::ostream &);
111    };
112
113    class Log
114    {
115      private:
116        int	 size;		// number of records in log
117        Record **buffer;	// array of 'size' Record ptrs (circular buf)
118        Record **nextRecPtr;	// next slot to use in buffer
119        Record **wrapRecPtr;	// &buffer[size], for quick wrap check
120
121      public:
122        Log();
123        ~Log();
124
125        void init(int _size);
126
127        void append(Record *);	// append trace record to log
128        void dump(std::ostream &);	// dump contents to stream
129    };
130
131    extern Log theLog;
132
133    extern ObjectMatch ignore;
134
135    inline void
136    dprintf(Tick when, const std::string &name, const char *format,
137            CPRINTF_DECLARATION)
138    {
139        if (!name.empty() && ignore.match(name))
140            return;
141
142        theLog.append(new Trace::PrintfRecord(when, name, format,
143                                              VARARGS_ALLARGS));
144    }
145
146    inline void
147    dataDump(Tick when, const std::string &name, const void *data, int len)
148    {
149        theLog.append(new Trace::DataRecord(when, name, data, len));
150    }
151
152    extern const std::string DefaultName;
153
154};
155
156inline std::ostream &
157DebugOut()
158{
159    return *Trace::dprintf_stream;
160}
161
162// This silly little class allows us to wrap a string in a functor
163// object so that we can give a name() that DPRINTF will like
164struct StringWrap
165{
166    std::string str;
167    StringWrap(const std::string &s) : str(s) {}
168    const std::string &operator()() const { return str; }
169};
170
171inline const std::string &name() { return Trace::DefaultName; }
172
173//
174// DPRINTF is a debugging trace facility that allows one to
175// selectively enable tracing statements.  To use DPRINTF, there must
176// be a function or functor called name() that returns a const
177// std::string & in the current scope.
178//
179// If you desire that the automatic printing not occur, use DPRINTFR
180// (R for raw)
181//
182
183#if TRACING_ON
184
185#define DTRACE(x) (Trace::IsOn(Trace::x) && Trace::enabled)
186
187#define DDUMP(x, data, count) do {                              \
188    if (DTRACE(x))                                              \
189        Trace::dataDump(curTick, name(), data, count);          \
190} while (0)
191
192#define DPRINTF(x, ...) do {                                    \
193    if (DTRACE(x))                                              \
194        Trace::dprintf(curTick, name(), __VA_ARGS__);           \
195} while (0)
196
197#define DPRINTFR(x, ...) do {                                   \
198    if (DTRACE(x))                                              \
199        Trace::dprintf((Tick)-1, std::string(), __VA_ARGS__);   \
200} while (0)
201
202#define DPRINTFN(...) do {                                      \
203    Trace::dprintf(curTick, name(), __VA_ARGS__);               \
204} while (0)
205
206#define DPRINTFNR(...) do {                                     \
207    Trace::dprintf((Tick)-1, string(), __VA_ARGS__);            \
208} while (0)
209
210#else // !TRACING_ON
211
212#define DTRACE(x) (false)
213#define DPRINTF(x, ...) do {} while (0)
214#define DPRINTFR(...) do {} while (0)
215#define DPRINTFN(...) do {} while (0)
216#define DPRINTFNR(...) do {} while (0)
217#define DDUMP(x, data, count) do {} while (0)
218
219#endif	// TRACING_ON
220
221#endif // __BASE_TRACE_HH__
222