trace.cc revision 8232
12SN/A/*
24039Sbinkertn@umich.edu * Copyright (c) 2001-2006 The Regents of The University of Michigan
32SN/A * All rights reserved.
42SN/A *
52SN/A * Redistribution and use in source and binary forms, with or without
62SN/A * modification, are permitted provided that the following conditions are
72SN/A * met: redistributions of source code must retain the above copyright
82SN/A * notice, this list of conditions and the following disclaimer;
92SN/A * redistributions in binary form must reproduce the above copyright
102SN/A * notice, this list of conditions and the following disclaimer in the
112SN/A * documentation and/or other materials provided with the distribution;
122SN/A * neither the name of the copyright holders nor the names of its
132SN/A * contributors may be used to endorse or promote products derived from
142SN/A * this software without specific prior written permission.
152SN/A *
162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert
292665Ssaidi@eecs.umich.edu *          Steve Reinhardt
302SN/A */
312SN/A
328229Snate@binkert.org#include <cctype>
332SN/A#include <fstream>
342SN/A#include <iostream>
352SN/A#include <string>
362SN/A
3756SN/A#include "base/misc.hh"
384046Sbinkertn@umich.edu#include "base/output.hh"
394046Sbinkertn@umich.edu#include "base/str.hh"
4056SN/A#include "base/trace.hh"
414039Sbinkertn@umich.edu#include "base/varargs.hh"
422SN/A
432SN/Ausing namespace std;
442SN/A
452SN/Anamespace Trace {
468232Snate@binkert.org
472SN/Aconst string DefaultName("global");
484074Sbinkertn@umich.edubool enabled = false;
492SN/A
502SN/A//
512SN/A// This variable holds the output stream for debug information.  Other
522SN/A// than setting up/redirecting this stream, do *NOT* reference this
532SN/A// directly; use DebugOut() (see below) to access this stream for
542SN/A// output.
552SN/A//
56488SN/Aostream *dprintf_stream = &cerr;
574046Sbinkertn@umich.eduostream &
584046Sbinkertn@umich.eduoutput()
594046Sbinkertn@umich.edu{
604046Sbinkertn@umich.edu    return *dprintf_stream;
614046Sbinkertn@umich.edu}
624046Sbinkertn@umich.edu
634046Sbinkertn@umich.eduvoid
644046Sbinkertn@umich.edusetOutput(const string &filename)
654046Sbinkertn@umich.edu{
664046Sbinkertn@umich.edu    dprintf_stream = simout.find(filename);
674046Sbinkertn@umich.edu}
682SN/A
691031SN/AObjectMatch ignore;
702SN/A
714046Sbinkertn@umich.eduvoid
724046Sbinkertn@umich.edudprintf(Tick when, const std::string &name, const char *format,
734046Sbinkertn@umich.edu        CPRINTF_DEFINITION)
744046Sbinkertn@umich.edu{
754046Sbinkertn@umich.edu    if (!name.empty() && ignore.match(name))
764046Sbinkertn@umich.edu        return;
772SN/A
784046Sbinkertn@umich.edu    std::ostream &os = *dprintf_stream;
792SN/A
802SN/A    string fmt = "";
814046Sbinkertn@umich.edu    CPrintfArgsList args(VARARGS_ALLARGS);
822SN/A
832SN/A    if (!name.empty()) {
842SN/A        fmt = "%s: " + fmt;
854039Sbinkertn@umich.edu        args.push_front(name);
862SN/A    }
872SN/A
884046Sbinkertn@umich.edu    if (when != (Tick)-1) {
892SN/A        fmt = "%7d: " + fmt;
904046Sbinkertn@umich.edu        args.push_front(when);
912SN/A    }
922SN/A
932SN/A    fmt += format;
942SN/A
954039Sbinkertn@umich.edu    ccprintf(os, fmt.c_str(), args);
962SN/A    os.flush();
972SN/A}
982SN/A
994046Sbinkertn@umich.eduvoid
1004046Sbinkertn@umich.edudump(Tick when, const std::string &name, const void *d, int len)
1012SN/A{
1024046Sbinkertn@umich.edu    if (!name.empty() && ignore.match(name))
1034046Sbinkertn@umich.edu        return;
1042SN/A
1054046Sbinkertn@umich.edu    std::ostream &os = *dprintf_stream;
1062SN/A
1074046Sbinkertn@umich.edu    string fmt = "";
1084046Sbinkertn@umich.edu    CPrintfArgsList args;
1094046Sbinkertn@umich.edu
1104046Sbinkertn@umich.edu    if (!name.empty()) {
1114046Sbinkertn@umich.edu        fmt = "%s: " + fmt;
1124046Sbinkertn@umich.edu        args.push_front(name);
1134046Sbinkertn@umich.edu    }
1144046Sbinkertn@umich.edu
1154046Sbinkertn@umich.edu    if (when != (Tick)-1) {
1164046Sbinkertn@umich.edu        fmt = "%7d: " + fmt;
1174046Sbinkertn@umich.edu        args.push_front(when);
1184046Sbinkertn@umich.edu    }
1194046Sbinkertn@umich.edu
1204046Sbinkertn@umich.edu    const char *data = static_cast<const char *>(d);
1212SN/A    int c, i, j;
1222SN/A    for (i = 0; i < len; i += 16) {
1234046Sbinkertn@umich.edu        ccprintf(os, fmt, args);
1244046Sbinkertn@umich.edu        ccprintf(os, "%08x  ", i);
1252SN/A        c = len - i;
1262SN/A        if (c > 16) c = 16;
1272SN/A
1282SN/A        for (j = 0; j < c; j++) {
1292SN/A            ccprintf(os, "%02x ", data[i + j] & 0xff);
1302SN/A            if ((j & 0xf) == 7 && j > 0)
1312SN/A                ccprintf(os, " ");
1322SN/A        }
1332SN/A
1342SN/A        for (; j < 16; j++)
1352SN/A            ccprintf(os, "   ");
1362SN/A        ccprintf(os, "  ");
1372SN/A
1382SN/A        for (j = 0; j < c; j++) {
1392SN/A            int ch = data[i + j] & 0x7f;
1404046Sbinkertn@umich.edu            ccprintf(os, "%c", (char)(isprint(ch) ? ch : ' '));
1412SN/A        }
1422SN/A
1432SN/A        ccprintf(os, "\n");
1442SN/A
1452SN/A        if (c < 16)
1462SN/A            break;
1472SN/A    }
1482SN/A}
1494046Sbinkertn@umich.edu
1507811Ssteve.reinhardt@amd.com} // namespace Trace
151