trace.cc revision 4042:dbd98b2264ed
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);
49bool enabled = true;
50
51//
52// This variable holds the output stream for debug information.  Other
53// than setting up/redirecting this stream, do *NOT* reference this
54// directly; use DebugOut() (see below) to access this stream for
55// output.
56//
57ostream *dprintf_stream = &cerr;
58
59ObjectMatch ignore;
60
61Log theLog;
62
63Log::Log()
64{
65    size = 0;
66    buffer = NULL;
67}
68
69
70void
71Log::init(int _size)
72{
73    if (buffer != NULL) {
74        fatal("Trace::Log::init called twice!");
75    }
76
77    size = _size;
78
79    buffer = new Record *[size];
80
81    for (int i = 0; i < size; ++i) {
82        buffer[i] = NULL;
83    }
84
85    nextRecPtr = &buffer[0];
86    wrapRecPtr = &buffer[size];
87}
88
89
90Log::~Log()
91{
92    for (int i = 0; i < size; ++i) {
93        delete buffer[i];
94    }
95
96    delete [] buffer;
97}
98
99
100void
101Log::append(Record *rec)
102{
103    // dump record to output stream if there's one open
104    if (dprintf_stream != NULL) {
105        rec->dump(*dprintf_stream);
106    } else {
107        rec->dump(cout);
108    }
109
110    // no buffering: justget rid of it now
111    if (buffer == NULL) {
112        delete rec;
113        return;
114    }
115
116    Record *oldRec = *nextRecPtr;
117
118    if (oldRec != NULL) {
119        // log has wrapped: overwrite
120        delete oldRec;
121    }
122
123    *nextRecPtr = rec;
124
125    if (++nextRecPtr == wrapRecPtr) {
126        nextRecPtr = &buffer[0];
127    }
128}
129
130
131void
132Log::dump(ostream &os)
133{
134    if (buffer == NULL) {
135        return;
136    }
137
138    Record **bufPtr = nextRecPtr;
139
140    if (*bufPtr == NULL) {
141        // next record slot is empty: log must not be full yet.
142        // start dumping from beginning of buffer
143        bufPtr = buffer;
144    }
145
146    do {
147        Record *rec = *bufPtr;
148
149        rec->dump(os);
150
151        if (++bufPtr == wrapRecPtr) {
152            bufPtr = &buffer[0];
153        }
154    } while (bufPtr != nextRecPtr);
155}
156
157PrintfRecord::~PrintfRecord()
158{}
159
160void
161PrintfRecord::dump(ostream &os)
162{
163    string fmt = "";
164
165    if (!name.empty()) {
166        fmt = "%s: " + fmt;
167        args.push_front(name);
168    }
169
170    if (cycle != (Tick)-1) {
171        fmt = "%7d: " + fmt;
172        args.push_front(cycle);
173    }
174
175    fmt += format;
176
177    ccprintf(os, fmt.c_str(), args);
178    os.flush();
179}
180
181DataRecord::DataRecord(Tick _cycle, const string &_name,
182                       const void *_data, int _len)
183    : Record(_cycle), name(_name), len(_len)
184{
185    data = new uint8_t[len];
186    memcpy(data, _data, len);
187}
188
189DataRecord::~DataRecord()
190{
191    delete [] data;
192}
193
194void
195DataRecord::dump(ostream &os)
196{
197    int c, i, j;
198
199    for (i = 0; i < len; i += 16) {
200        ccprintf(os, "%d: %s: %08x  ", cycle, name, i);
201        c = len - i;
202        if (c > 16) c = 16;
203
204        for (j = 0; j < c; j++) {
205            ccprintf(os, "%02x ", data[i + j] & 0xff);
206            if ((j & 0xf) == 7 && j > 0)
207                ccprintf(os, " ");
208        }
209
210        for (; j < 16; j++)
211            ccprintf(os, "   ");
212        ccprintf(os, "  ");
213
214        for (j = 0; j < c; j++) {
215            int ch = data[i + j] & 0x7f;
216            ccprintf(os,
217                     "%c", (char)(isprint(ch) ? ch : ' '));
218        }
219
220        ccprintf(os, "\n");
221
222        if (c < 16)
223            break;
224    }
225}
226} // namespace Trace
227