2 * Copyright (c) 2001-2005 The Regents of The University of Michigan
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.
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.
36 #include "base/misc.hh"
37 #include "base/trace.hh"
38 #include "base/str.hh"
43 const string
DefaultName("global");
44 FlagVec
flags(NumFlags
, false);
47 // This variable holds the output stream for debug information. Other
48 // than setting up/redirecting this stream, do *NOT* reference this
49 // directly; use DebugOut() (see below) to access this stream for
52 ostream
*dprintf_stream
= &cerr
;
69 fatal("Trace::Log::init called twice!");
74 buffer
= new Record
*[size
];
76 for (int i
= 0; i
< size
; ++i
) {
80 nextRecPtr
= &buffer
[0];
81 wrapRecPtr
= &buffer
[size
];
87 for (int i
= 0; i
< size
; ++i
) {
96 Log::append(Record
*rec
)
98 // dump record to output stream if there's one open
99 if (dprintf_stream
!= NULL
) {
100 rec
->dump(*dprintf_stream
);
105 // no buffering: justget rid of it now
106 if (buffer
== NULL
) {
111 Record
*oldRec
= *nextRecPtr
;
113 if (oldRec
!= NULL
) {
114 // log has wrapped: overwrite
120 if (++nextRecPtr
== wrapRecPtr
) {
121 nextRecPtr
= &buffer
[0];
127 Log::dump(ostream
&os
)
129 if (buffer
== NULL
) {
133 Record
**bufPtr
= nextRecPtr
;
135 if (*bufPtr
== NULL
) {
136 // next record slot is empty: log must not be full yet.
137 // start dumping from beginning of buffer
142 Record
*rec
= *bufPtr
;
146 if (++bufPtr
== wrapRecPtr
) {
149 } while (bufPtr
!= nextRecPtr
);
152 PrintfRecord::~PrintfRecord()
158 PrintfRecord::dump(ostream
&os
)
167 if (cycle
!= (Tick
)-1) {
178 DataRecord::DataRecord(Tick _cycle
, const string
&_name
,
179 const void *_data
, int _len
)
180 : Record(_cycle
), name(_name
), len(_len
)
182 data
= new uint8_t[len
];
183 memcpy(data
, _data
, len
);
186 DataRecord::~DataRecord()
192 DataRecord::dump(ostream
&os
)
196 for (i
= 0; i
< len
; i
+= 16) {
197 ccprintf(os
, "%d: %s: %08x ", cycle
, name
, i
);
201 for (j
= 0; j
< c
; j
++) {
202 ccprintf(os
, "%02x ", data
[i
+ j
] & 0xff);
203 if ((j
& 0xf) == 7 && j
> 0)
211 for (j
= 0; j
< c
; j
++) {
212 int ch
= data
[i
+ j
] & 0x7f;
214 "%c", (char)(isprint(ch
) ? ch
: ' '));
226 // Returns the current output stream for debug information. As a
227 // wrapper around Trace::dprintf_stream, this handles cases where debug
228 // information is generated in the process of parsing .ini options,
229 // before we process the option that sets up the debug output stream
235 return *Trace::dprintf_stream
;
238 /////////////////////////////////////////////
240 // C-linkage functions for invoking from gdb
242 /////////////////////////////////////////////
245 // Dump trace buffer to specified file (cout if NULL)
249 dumpTrace(const char *filename
)
251 if (filename
!= NULL
) {
252 ofstream
out(filename
);
253 Trace::theLog
.dump(out
);
257 Trace::theLog
.dump(cout
);
263 // Turn on/off trace output to cerr. Typically used when trace output
264 // is only going to circular buffer, but you want to see what's being
265 // sent there as you step through some code in gdb. This uses the
266 // same facility as the "trace to file" feature, and will print error
267 // messages rather than clobbering an existing ostream pointer.
274 if (Trace::dprintf_stream
!= NULL
) {
275 cerr
<< "Already echoing trace to a file... go do a 'tail -f'"
276 << " on that file instead." << endl
;
278 Trace::dprintf_stream
= &cerr
;
281 if (Trace::dprintf_stream
!= &cerr
) {
282 cerr
<< "Not echoing trace to cerr." << endl
;
284 Trace::dprintf_stream
= NULL
;
293 using namespace Trace
;
294 for (int i
= 0; i
< numFlagStrings
; ++i
)
296 cprintf("%s\n", flagStrings
[i
]);
300 tweakTraceFlag(const char *string
, bool value
)
302 using namespace Trace
;
303 std::string
str(string
);
305 for (int i
= 0; i
< numFlagStrings
; ++i
) {
306 if (str
!= flagStrings
[i
])
311 if (idx
< NumFlags
) {
315 if (idx
>= NumCompoundFlags
) {
316 ccprintf(cerr
, "Invalid compound flag");
320 const Flags
*flagVec
= compoundFlags
[idx
];
322 for (int j
= 0; flagVec
[j
] != -1; ++j
) {
323 if (flagVec
[j
] >= NumFlags
) {
324 ccprintf(cerr
, "Invalid compound flag");
327 flags
[flagVec
[j
]] = value
;
331 cprintf("flag %s was %s\n", string
, value
? "set" : "cleared");
335 cprintf("could not find flag %s\n", string
);
340 setTraceFlag(const char *string
)
342 tweakTraceFlag(string
, true);
347 clearTraceFlag(const char *string
)
349 tweakTraceFlag(string
, false);