trace.hh revision 4039:b910b61a52b9
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#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 std::string &name;
85        const char *format;
86        CPrintfArgsList args;
87
88      public:
89        PrintfRecord(Tick cycle, const std::string &_name, const char *_format,
90                     CPRINTF_DECLARATION)
91            : Record(cycle), name(_name), format(_format),
92              args(VARARGS_ALLARGS)
93        {
94        }
95
96        virtual ~PrintfRecord();
97
98        virtual void dump(std::ostream &);
99    };
100
101    class DataRecord : public Record
102    {
103      private:
104        const std::string &name;
105        uint8_t *data;
106        int len;
107
108      public:
109        DataRecord(Tick cycle, const std::string &name,
110                   const void *_data, int _len);
111        virtual ~DataRecord();
112
113        virtual void dump(std::ostream &);
114    };
115
116    class Log
117    {
118      private:
119        int	 size;		// number of records in log
120        Record **buffer;	// array of 'size' Record ptrs (circular buf)
121        Record **nextRecPtr;	// next slot to use in buffer
122        Record **wrapRecPtr;	// &buffer[size], for quick wrap check
123
124      public:
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(Tick when, const std::string &name, const char *format,
140            CPRINTF_DECLARATION)
141    {
142        if (!name.empty() && ignore.match(name))
143            return;
144
145        theLog.append(new Trace::PrintfRecord(when, name, format,
146                                              VARARGS_ALLARGS));
147    }
148
149    inline void
150    dataDump(Tick when, const std::string &name, const void *data, int len)
151    {
152        theLog.append(new Trace::DataRecord(when, name, data, len));
153    }
154
155    extern const std::string DefaultName;
156
157};
158
159std::ostream &DebugOut();
160
161// This silly little class allows us to wrap a string in a functor
162// object so that we can give a name() that DPRINTF will like
163struct StringWrap
164{
165    std::string str;
166    StringWrap(const std::string &s) : str(s) {}
167    const std::string &operator()() const { return str; }
168};
169
170inline const std::string &name() { return Trace::DefaultName; }
171
172//
173// DPRINTF is a debugging trace facility that allows one to
174// selectively enable tracing statements.  To use DPRINTF, there must
175// be a function or functor called name() that returns a const
176// std::string & in the current scope.
177//
178// If you desire that the automatic printing not occur, use DPRINTFR
179// (R for raw)
180//
181
182#if TRACING_ON
183
184#define DTRACE(x) (Trace::IsOn(Trace::x))
185
186#define DDUMP(x, data, count) do {                     \
187    if (DTRACE(x))                                     \
188        Trace::dataDump(curTick, name(), data, count); \
189} while (0)
190
191#define DPRINTF(x, args...) do {                       \
192    if (DTRACE(x))                                     \
193        Trace::dprintf(curTick, name(), args);         \
194} while (0)
195
196#define DPRINTFR(x, args...) do {                      \
197    if (DTRACE(x))                                     \
198        Trace::dprintf((Tick)-1, std::string(), args); \
199} while (0)
200
201#define DPRINTFN(args...) do {                         \
202    Trace::dprintf(curTick, name(), args);             \
203} while (0)
204
205#define DPRINTFNR(args...) do {                        \
206    Trace::dprintf((Tick)-1, string(), args);          \
207} while (0)
208
209#else // !TRACING_ON
210
211#define DTRACE(x) (false)
212#define DPRINTF(x, args...) do {} while (0)
213#define DPRINTFR(args...) do {} while (0)
214#define DPRINTFN(args...) do {} while (0)
215#define DPRINTFNR(args...) do {} while (0)
216#define DDUMP(x, data, count) do {} while (0)
217
218#endif	// TRACING_ON
219
220#endif // __BASE_TRACE_HH__
221