manual merge of linux_system and makefile
[gem5.git] / base / trace.hh
1 /*
2 * Copyright (c) 2003 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
48 namespace 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 RawDataRecord : public Record
105 {
106 private:
107 uint8_t *data;
108 int len;
109
110 public:
111 RawDataRecord(Tick cycle, const void *_data, int _len);
112 virtual ~RawDataRecord();
113
114 virtual void dump(std::ostream &);
115 };
116
117 class Log
118 {
119 private:
120 int size; // number of records in log
121 Record **buffer; // array of 'size' Record ptrs (circular buf)
122 Record **nextRecPtr; // next slot to use in buffer
123 Record **wrapRecPtr; // &buffer[size], for quick wrap check
124
125 public:
126
127 Log();
128 ~Log();
129
130 void init(int _size);
131
132 void append(Record *); // append trace record to log
133 void dump(std::ostream &); // dump contents to stream
134 };
135
136 extern Log theLog;
137
138 extern int dprintf_ignore_size;
139
140 bool
141 dprintf_ignore_name(const std::string &name);
142
143 inline void
144 dprintf(const char *format, cp::ArgList &args, Tick cycle,
145 const std::string &name)
146 {
147 if (!dprintf_ignore_size || name.empty() || !dprintf_ignore_name(name))
148 theLog.append(new Trace::PrintfRecord(format, args, cycle, name));
149 }
150
151 inline void
152 rawDump(const void *data, int len)
153 {
154 theLog.append(new Trace::RawDataRecord(curTick, data, len));
155 }
156
157 extern const std::string DefaultName;
158 };
159
160 inline const std::string &name() { return Trace::DefaultName; }
161
162 std::ostream &DebugOut();
163
164 //
165 // DPRINTF is a debugging trace facility that allows one to
166 // selectively enable tracing statements. To use DPRINTF, there must
167 // be a function or functor called name() that returns a const
168 // std::string & in the current scope.
169 //
170 // If you desire that the automatic printing not occur, use DPRINTFR
171 // (R for raw)
172 //
173
174 #if TRACING_ON
175
176 #define DTRACE(x) (Trace::IsOn(Trace::x))
177
178 #define DCOUT(x) if (Trace::IsOn(Trace::x)) DebugOut()
179
180 #define DDUMP(x, data, count) \
181 do { \
182 if (Trace::IsOn(Trace::x)) \
183 Trace::rawDump(data, count); \
184 } while (0)
185
186 #define __dprintf(cycle, name, format, args...) \
187 Trace::dprintf(format, (*(new cp::ArgList), args), cycle, name)
188
189 #define DPRINTF(x, args...) \
190 do { \
191 if (Trace::IsOn(Trace::x)) \
192 __dprintf(curTick, name(), args, cp::ArgListNull()); \
193 } while (0)
194
195 #define DPRINTFR(x, args...) \
196 do { \
197 if (Trace::IsOn(Trace::x)) \
198 __dprintf((Tick)-1, string(), args, cp::ArgListNull()); \
199 } while (0)
200
201 #define DPRINTFN(args...) \
202 do { \
203 __dprintf(curTick, name(), args, cp::ArgListNull()); \
204 } while (0)
205
206 #define DPRINTFNR(args...) \
207 do { \
208 __dprintf((Tick)-1, string(), args, cp::ArgListNull()); \
209 } while (0)
210
211 #else // !TRACING_ON
212
213 #define DTRACE(x) (false)
214 #define DCOUT(x) if (0) DebugOut()
215 #define DPRINTF(x, args...) do {} while (0)
216 #define DPRINTFR(args...) do {} while (0)
217 #define DPRINTFN(args...) do {} while (0)
218 #define DPRINTFNR(args...) do {} while (0)
219 #define DDUMP(x, data, count) do {} while (0)
220
221 #endif // TRACING_ON
222
223 #endif // __TRACE_HH__