2 * Copyright (c) 2003 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
= NULL
;
54 int dprintf_ignore_size
;
55 vector
<string
> dprintf_ignore
;
56 vector
<vector
<string
> > ignore_tokens
;
57 vector
<int> ignore_size
;
60 dprintf_ignore_name(const string
&name
)
62 vector
<string
> name_tokens
;
63 tokenize(name_tokens
, name
, '.');
64 int ntsize
= name_tokens
.size();
66 for (int i
= 0; i
< dprintf_ignore_size
; ++i
) {
68 int jstop
= ignore_size
[i
];
69 for (int j
= 0; j
< jstop
; ++j
) {
73 const string
&ignore
= ignore_tokens
[i
][j
];
74 if (ignore
!= "*" && ignore
!= name_tokens
[j
]) {
100 if (buffer
!= NULL
) {
101 fatal("Trace::Log::init called twice!");
106 buffer
= new (Record
*)[size
];
108 for (int i
= 0; i
< size
; ++i
) {
112 nextRecPtr
= &buffer
[0];
113 wrapRecPtr
= &buffer
[size
];
119 for (int i
= 0; i
< size
; ++i
) {
128 Log::append(Record
*rec
)
130 // dump record to output stream if there's one open
131 if (dprintf_stream
!= NULL
) {
132 rec
->dump(*dprintf_stream
);
135 // no buffering: justget rid of it now
136 if (buffer
== NULL
) {
141 Record
*oldRec
= *nextRecPtr
;
143 if (oldRec
!= NULL
) {
144 // log has wrapped: overwrite
150 if (++nextRecPtr
== wrapRecPtr
) {
151 nextRecPtr
= &buffer
[0];
157 Log::dump(ostream
&os
)
159 if (buffer
== NULL
) {
163 Record
**bufPtr
= nextRecPtr
;
165 if (*bufPtr
== NULL
) {
166 // next record slot is empty: log must not be full yet.
167 // start dumping from beginning of buffer
172 Record
*rec
= *bufPtr
;
176 if (++bufPtr
== wrapRecPtr
) {
179 } while (bufPtr
!= nextRecPtr
);
182 PrintfRecord::~PrintfRecord()
189 PrintfRecord::dump(ostream
&os
)
198 if (cycle
!= (Tick
)-1) {
211 RawDataRecord::RawDataRecord(Tick _cycle
,
212 const uint8_t *_data
, int _len
)
213 : Record(_cycle
), len(_len
)
215 data
= new uint8_t[len
];
216 memcpy(data
, _data
, len
);
220 RawDataRecord::~RawDataRecord()
227 RawDataRecord::dump(ostream
&os
)
231 for (i
= 0; i
< len
; i
+= 16) {
232 ccprintf(os
, "%08x ", i
);
236 for (j
= 0; j
< c
; j
++) {
237 ccprintf(os
, "%02x ", data
[i
+ j
] & 0xff);
238 if ((j
& 0xf) == 7 && j
> 0)
246 for (j
= 0; j
< c
; j
++) {
247 int ch
= data
[i
+ j
] & 0x7f;
249 "%c", (char)(isprint(ch
) ? ch
: ' '));
261 // Returns the current output stream for debug information. As a
262 // wrapper around Trace::dprintf_stream, this handles cases where debug
263 // information is generated in the process of parsing .ini options,
264 // before we process the option that sets up the debug output stream
270 if (Trace::dprintf_stream
)
271 return *Trace::dprintf_stream
;
276 /////////////////////////////////////////////
278 // C-linkage functions for invoking from gdb
280 /////////////////////////////////////////////
283 // Dump trace buffer to specified file (cout if NULL)
287 dumpTrace(const char *filename
)
289 if (filename
!= NULL
) {
290 ofstream
out(filename
);
291 Trace::theLog
.dump(out
);
295 Trace::theLog
.dump(cout
);
301 // Turn on/off trace output to cerr. Typically used when trace output
302 // is only going to circular buffer, but you want to see what's being
303 // sent there as you step through some code in gdb. This uses the
304 // same facility as the "trace to file" feature, and will print error
305 // messages rather than clobbering an existing ostream pointer.
312 if (Trace::dprintf_stream
!= NULL
) {
313 cerr
<< "Already echoing trace to a file... go do a 'tail -f'"
314 << " on that file instead." << endl
;
316 Trace::dprintf_stream
= &cerr
;
319 if (Trace::dprintf_stream
!= &cerr
) {
320 cerr
<< "Not echoing trace to cerr." << endl
;
322 Trace::dprintf_stream
= NULL
;