trace.hh revision 3918:1f9a98d198e8
1/*
2 * Copyright (c) 2001-2005 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#if TRACING_ON
51    const bool On				= true;
52#else
53    const bool On				= false;
54#endif
55
56    inline bool
57    IsOn(int t)
58    {
59        return flags[t];
60
61    }
62
63    void dump(const uint8_t *data, int count);
64
65    class Record
66    {
67      protected:
68        Tick cycle;
69
70        Record(Tick _cycle)
71            : cycle(_cycle)
72        {
73        }
74
75      public:
76        virtual ~Record() {}
77
78        virtual void dump(std::ostream &) = 0;
79    };
80
81    class PrintfRecord : public Record
82    {
83      private:
84        const char *format;
85        const std::string &name;
86        cp::ArgList &args;
87
88      public:
89        PrintfRecord(const char *_format, cp::ArgList &_args,
90                     Tick cycle, const std::string &_name)
91            : Record(cycle), format(_format), name(_name), args(_args)
92        {
93        }
94
95        virtual ~PrintfRecord();
96
97        virtual void dump(std::ostream &);
98    };
99
100    class DataRecord : public Record
101    {
102      private:
103        const std::string &name;
104        uint8_t *data;
105        int len;
106
107      public:
108        DataRecord(Tick cycle, const std::string &name,
109                   const void *_data, int _len);
110        virtual ~DataRecord();
111
112        virtual void dump(std::ostream &);
113    };
114
115    class Log
116    {
117      private:
118        int	 size;		// number of records in log
119        Record **buffer;	// array of 'size' Record ptrs (circular buf)
120        Record **nextRecPtr;	// next slot to use in buffer
121        Record **wrapRecPtr;	// &buffer[size], for quick wrap check
122
123      public:
124
125        Log();
126        ~Log();
127
128        void init(int _size);
129
130        void append(Record *);	// append trace record to log
131        void dump(std::ostream &);	// dump contents to stream
132    };
133
134    extern Log theLog;
135
136    extern ObjectMatch ignore;
137
138    inline void
139    dprintf(const char *format, cp::ArgList &args, Tick cycle,
140            const std::string &name)
141    {
142        if (name.empty() || !ignore.match(name))
143            theLog.append(new Trace::PrintfRecord(format, args, cycle, name));
144    }
145
146    inline void
147    dataDump(Tick cycle, const std::string &name, const void *data, int len)
148    {
149        theLog.append(new Trace::DataRecord(cycle, name, data, len));
150    }
151
152    extern const std::string DefaultName;
153};
154
155// This silly little class allows us to wrap a string in a functor
156// object so that we can give a name() that DPRINTF will like
157struct StringWrap
158{
159    std::string str;
160    StringWrap(const std::string &s) : str(s) {}
161    const std::string &operator()() const { return str; }
162};
163
164inline const std::string &name() { return Trace::DefaultName; }
165std::ostream &DebugOut();
166
167//
168// DPRINTF is a debugging trace facility that allows one to
169// selectively enable tracing statements.  To use DPRINTF, there must
170// be a function or functor called name() that returns a const
171// std::string & in the current scope.
172//
173// If you desire that the automatic printing not occur, use DPRINTFR
174// (R for raw)
175//
176
177#if TRACING_ON
178
179#define DTRACE(x) (Trace::IsOn(Trace::x))
180
181#define DCOUT(x) if (Trace::IsOn(Trace::x)) DebugOut()
182
183#define DDUMP(x, data, count) \
184do { \
185    if (Trace::IsOn(Trace::x)) \
186        Trace::dataDump(curTick, name(), data, count);	\
187} while (0)
188
189#define __dprintf(cycle, name, format, ...) \
190    Trace::dprintf(format, (*(new cp::ArgList), __VA_ARGS__), cycle, name)
191
192#define DPRINTF(x, ...) \
193do { \
194    if (Trace::IsOn(Trace::x)) \
195        __dprintf(curTick, name(), __VA_ARGS__, cp::ArgListNull()); \
196} while (0)
197
198#define DPRINTFR(x, ...) \
199do { \
200    if (Trace::IsOn(Trace::x)) \
201        __dprintf((Tick)-1, std::string(), __VA_ARGS__, cp::ArgListNull());	\
202} while (0)
203
204#define DPRINTFN(...) \
205do { \
206    __dprintf(curTick, name(), __VA_ARGS__, cp::ArgListNull()); \
207} while (0)
208
209#define DPRINTFNR(...) \
210do { \
211    __dprintf((Tick)-1, string(), __VA_ARGS__, cp::ArgListNull()); \
212} while (0)
213
214#else // !TRACING_ON
215
216#define DTRACE(x) (false)
217#define DCOUT(x) if (0) DebugOut()
218#define DPRINTF(x, ...) do {} while (0)
219#define DPRINTFR(...) do {} while (0)
220#define DPRINTFN(...) do {} while (0)
221#define DPRINTFNR(...) do {} while (0)
222#define DDUMP(x, data, count) do {} while (0)
223
224#endif	// TRACING_ON
225
226#endif // __BASE_TRACE_HH__
227