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