trace.cc revision 8232:b28d06a175be
112837Sgabeblack@google.com/*
212837Sgabeblack@google.com * Copyright (c) 2001-2006 The Regents of The University of Michigan
312837Sgabeblack@google.com * All rights reserved.
412837Sgabeblack@google.com *
512837Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
612837Sgabeblack@google.com * modification, are permitted provided that the following conditions are
712837Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
812837Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
912837Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
1012837Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1112837Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1212837Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1312837Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1412837Sgabeblack@google.com * this software without specific prior written permission.
1512837Sgabeblack@google.com *
1612837Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1712837Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1812837Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1912837Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2012837Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2112837Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2212837Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2312837Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2412837Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2512837Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2612837Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2712837Sgabeblack@google.com *
2812837Sgabeblack@google.com * Authors: Nathan Binkert
2912837Sgabeblack@google.com *          Steve Reinhardt
3012837Sgabeblack@google.com */
3112837Sgabeblack@google.com
3212837Sgabeblack@google.com#include <cctype>
3312837Sgabeblack@google.com#include <fstream>
3412837Sgabeblack@google.com#include <iostream>
3512837Sgabeblack@google.com#include <string>
3612837Sgabeblack@google.com
3712837Sgabeblack@google.com#include "base/misc.hh"
3812837Sgabeblack@google.com#include "base/output.hh"
3912837Sgabeblack@google.com#include "base/str.hh"
4012837Sgabeblack@google.com#include "base/trace.hh"
4112837Sgabeblack@google.com#include "base/varargs.hh"
4212837Sgabeblack@google.com
4312837Sgabeblack@google.comusing namespace std;
4412837Sgabeblack@google.com
4512837Sgabeblack@google.comnamespace Trace {
4612837Sgabeblack@google.com
4712837Sgabeblack@google.comconst string DefaultName("global");
4812837Sgabeblack@google.combool enabled = false;
4912837Sgabeblack@google.com
5012837Sgabeblack@google.com//
5112837Sgabeblack@google.com// This variable holds the output stream for debug information.  Other
5212837Sgabeblack@google.com// than setting up/redirecting this stream, do *NOT* reference this
5312837Sgabeblack@google.com// directly; use DebugOut() (see below) to access this stream for
5412837Sgabeblack@google.com// output.
5512837Sgabeblack@google.com//
5612837Sgabeblack@google.comostream *dprintf_stream = &cerr;
5712837Sgabeblack@google.comostream &
5812837Sgabeblack@google.comoutput()
5912837Sgabeblack@google.com{
6012837Sgabeblack@google.com    return *dprintf_stream;
6112837Sgabeblack@google.com}
6212837Sgabeblack@google.com
6312837Sgabeblack@google.comvoid
6412837Sgabeblack@google.comsetOutput(const string &filename)
6512837Sgabeblack@google.com{
6612837Sgabeblack@google.com    dprintf_stream = simout.find(filename);
6712837Sgabeblack@google.com}
6812837Sgabeblack@google.com
6912837Sgabeblack@google.comObjectMatch ignore;
7012837Sgabeblack@google.com
7112837Sgabeblack@google.comvoid
7212837Sgabeblack@google.comdprintf(Tick when, const std::string &name, const char *format,
7312837Sgabeblack@google.com        CPRINTF_DEFINITION)
7412837Sgabeblack@google.com{
7512837Sgabeblack@google.com    if (!name.empty() && ignore.match(name))
7612837Sgabeblack@google.com        return;
7712837Sgabeblack@google.com
7812837Sgabeblack@google.com    std::ostream &os = *dprintf_stream;
7912837Sgabeblack@google.com
8012837Sgabeblack@google.com    string fmt = "";
8112837Sgabeblack@google.com    CPrintfArgsList args(VARARGS_ALLARGS);
8212837Sgabeblack@google.com
8312837Sgabeblack@google.com    if (!name.empty()) {
8412837Sgabeblack@google.com        fmt = "%s: " + fmt;
8512837Sgabeblack@google.com        args.push_front(name);
8612837Sgabeblack@google.com    }
8712837Sgabeblack@google.com
8812837Sgabeblack@google.com    if (when != (Tick)-1) {
8912837Sgabeblack@google.com        fmt = "%7d: " + fmt;
9012837Sgabeblack@google.com        args.push_front(when);
9112837Sgabeblack@google.com    }
9212837Sgabeblack@google.com
9312837Sgabeblack@google.com    fmt += format;
9412837Sgabeblack@google.com
9512837Sgabeblack@google.com    ccprintf(os, fmt.c_str(), args);
9612837Sgabeblack@google.com    os.flush();
9712837Sgabeblack@google.com}
9812837Sgabeblack@google.com
99void
100dump(Tick when, const std::string &name, const void *d, int len)
101{
102    if (!name.empty() && ignore.match(name))
103        return;
104
105    std::ostream &os = *dprintf_stream;
106
107    string fmt = "";
108    CPrintfArgsList args;
109
110    if (!name.empty()) {
111        fmt = "%s: " + fmt;
112        args.push_front(name);
113    }
114
115    if (when != (Tick)-1) {
116        fmt = "%7d: " + fmt;
117        args.push_front(when);
118    }
119
120    const char *data = static_cast<const char *>(d);
121    int c, i, j;
122    for (i = 0; i < len; i += 16) {
123        ccprintf(os, fmt, args);
124        ccprintf(os, "%08x  ", i);
125        c = len - i;
126        if (c > 16) c = 16;
127
128        for (j = 0; j < c; j++) {
129            ccprintf(os, "%02x ", data[i + j] & 0xff);
130            if ((j & 0xf) == 7 && j > 0)
131                ccprintf(os, " ");
132        }
133
134        for (; j < 16; j++)
135            ccprintf(os, "   ");
136        ccprintf(os, "  ");
137
138        for (j = 0; j < c; j++) {
139            int ch = data[i + j] & 0x7f;
140            ccprintf(os, "%c", (char)(isprint(ch) ? ch : ' '));
141        }
142
143        ccprintf(os, "\n");
144
145        if (c < 16)
146            break;
147    }
148}
149
150} // namespace Trace
151