trace.cc 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#include <ctype.h>
33#include <fstream>
34#include <iostream>
35#include <list>
36#include <string>
37#include <vector>
38
39#include "base/misc.hh"
40#include "base/trace.hh"
41#include "base/str.hh"
42#include "base/varargs.hh"
43
44using namespace std;
45
46namespace Trace {
47const string DefaultName("global");
48FlagVec flags(NumFlags, false);
49
50//
51// This variable holds the output stream for debug information.  Other
52// than setting up/redirecting this stream, do *NOT* reference this
53// directly; use DebugOut() (see below) to access this stream for
54// output.
55//
56ostream *dprintf_stream = &cerr;
57
58ObjectMatch ignore;
59
60Log theLog;
61
62Log::Log()
63{
64    size = 0;
65    buffer = NULL;
66}
67
68
69void
70Log::init(int _size)
71{
72    if (buffer != NULL) {
73        fatal("Trace::Log::init called twice!");
74    }
75
76    size = _size;
77
78    buffer = new Record *[size];
79
80    for (int i = 0; i < size; ++i) {
81        buffer[i] = NULL;
82    }
83
84    nextRecPtr = &buffer[0];
85    wrapRecPtr = &buffer[size];
86}
87
88
89Log::~Log()
90{
91    for (int i = 0; i < size; ++i) {
92        delete buffer[i];
93    }
94
95    delete [] buffer;
96}
97
98
99void
100Log::append(Record *rec)
101{
102    // dump record to output stream if there's one open
103    if (dprintf_stream != NULL) {
104        rec->dump(*dprintf_stream);
105    } else {
106        rec->dump(cout);
107    }
108
109    // no buffering: justget rid of it now
110    if (buffer == NULL) {
111        delete rec;
112        return;
113    }
114
115    Record *oldRec = *nextRecPtr;
116
117    if (oldRec != NULL) {
118        // log has wrapped: overwrite
119        delete oldRec;
120    }
121
122    *nextRecPtr = rec;
123
124    if (++nextRecPtr == wrapRecPtr) {
125        nextRecPtr = &buffer[0];
126    }
127}
128
129
130void
131Log::dump(ostream &os)
132{
133    if (buffer == NULL) {
134        return;
135    }
136
137    Record **bufPtr = nextRecPtr;
138
139    if (*bufPtr == NULL) {
140        // next record slot is empty: log must not be full yet.
141        // start dumping from beginning of buffer
142        bufPtr = buffer;
143    }
144
145    do {
146        Record *rec = *bufPtr;
147
148        rec->dump(os);
149
150        if (++bufPtr == wrapRecPtr) {
151            bufPtr = &buffer[0];
152        }
153    } while (bufPtr != nextRecPtr);
154}
155
156PrintfRecord::~PrintfRecord()
157{}
158
159void
160PrintfRecord::dump(ostream &os)
161{
162    string fmt = "";
163
164    if (!name.empty()) {
165        fmt = "%s: " + fmt;
166        args.push_front(name);
167    }
168
169    if (cycle != (Tick)-1) {
170        fmt = "%7d: " + fmt;
171        args.push_front(cycle);
172    }
173
174    fmt += format;
175
176    ccprintf(os, fmt.c_str(), args);
177    os.flush();
178}
179
180DataRecord::DataRecord(Tick _cycle, const string &_name,
181                       const void *_data, int _len)
182    : Record(_cycle), name(_name), len(_len)
183{
184    data = new uint8_t[len];
185    memcpy(data, _data, len);
186}
187
188DataRecord::~DataRecord()
189{
190    delete [] data;
191}
192
193void
194DataRecord::dump(ostream &os)
195{
196    int c, i, j;
197
198    for (i = 0; i < len; i += 16) {
199        ccprintf(os, "%d: %s: %08x  ", cycle, name, i);
200        c = len - i;
201        if (c > 16) c = 16;
202
203        for (j = 0; j < c; j++) {
204            ccprintf(os, "%02x ", data[i + j] & 0xff);
205            if ((j & 0xf) == 7 && j > 0)
206                ccprintf(os, " ");
207        }
208
209        for (; j < 16; j++)
210            ccprintf(os, "   ");
211        ccprintf(os, "  ");
212
213        for (j = 0; j < c; j++) {
214            int ch = data[i + j] & 0x7f;
215            ccprintf(os,
216                     "%c", (char)(isprint(ch) ? ch : ' '));
217        }
218
219        ccprintf(os, "\n");
220
221        if (c < 16)
222            break;
223    }
224}
225} // namespace Trace
226
227//
228// Returns the current output stream for debug information.  As a
229// wrapper around Trace::dprintf_stream, this handles cases where debug
230// information is generated in the process of parsing .ini options,
231// before we process the option that sets up the debug output stream
232// itself.
233//
234std::ostream &
235DebugOut()
236{
237    return *Trace::dprintf_stream;
238}
239
240/////////////////////////////////////////////
241//
242// C-linkage functions for invoking from gdb
243//
244/////////////////////////////////////////////
245
246//
247// Dump trace buffer to specified file (cout if NULL)
248//
249void
250dumpTrace(const char *filename)
251{
252    if (filename != NULL) {
253        ofstream out(filename);
254        Trace::theLog.dump(out);
255        out.close();
256    }
257    else {
258        Trace::theLog.dump(cout);
259    }
260}
261
262
263//
264// Turn on/off trace output to cerr.  Typically used when trace output
265// is only going to circular buffer, but you want to see what's being
266// sent there as you step through some code in gdb.  This uses the
267// same facility as the "trace to file" feature, and will print error
268// messages rather than clobbering an existing ostream pointer.
269//
270void
271echoTrace(bool on)
272{
273    if (on) {
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;
277        } else {
278            Trace::dprintf_stream = &cerr;
279        }
280    } else {
281        if (Trace::dprintf_stream != &cerr) {
282            cerr << "Not echoing trace to cerr." << endl;
283        } else {
284            Trace::dprintf_stream = NULL;
285        }
286    }
287}
288
289void
290printTraceFlags()
291{
292    using namespace Trace;
293    for (int i = 0; i < numFlagStrings; ++i)
294        if (flags[i])
295            cprintf("%s\n", flagStrings[i]);
296}
297
298void
299tweakTraceFlag(const char *string, bool value)
300{
301    using namespace Trace;
302    std::string str(string);
303
304    for (int i = 0; i < numFlagStrings; ++i) {
305        if (str != flagStrings[i])
306            continue;
307
308        int idx = i;
309
310        if (idx < NumFlags) {
311            flags[idx] = value;
312        } else {
313            idx -= NumFlags;
314            if (idx >= NumCompoundFlags) {
315                ccprintf(cerr, "Invalid compound flag");
316                return;
317            }
318
319            const Flags *flagVec = compoundFlags[idx];
320
321            for (int j = 0; flagVec[j] != -1; ++j) {
322                if (flagVec[j] >= NumFlags) {
323                    ccprintf(cerr, "Invalid compound flag");
324                    return;
325                }
326                flags[flagVec[j]] = value;
327            }
328        }
329
330        cprintf("flag %s was %s\n", string, value ? "set" : "cleared");
331        return;
332    }
333
334    cprintf("could not find flag %s\n", string);
335}
336
337void
338setTraceFlag(const char *string)
339{
340    tweakTraceFlag(string, true);
341}
342
343void
344clearTraceFlag(const char *string)
345{
346    tweakTraceFlag(string, false);
347}
348