trace.cc revision 8229
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 <list>
362SN/A#include <string>
372SN/A#include <vector>
382SN/A
3956SN/A#include "base/misc.hh"
404046Sbinkertn@umich.edu#include "base/output.hh"
414046Sbinkertn@umich.edu#include "base/str.hh"
4256SN/A#include "base/trace.hh"
434039Sbinkertn@umich.edu#include "base/varargs.hh"
442SN/A
452SN/Ausing namespace std;
462SN/A
472SN/Anamespace Trace {
482SN/Aconst string DefaultName("global");
492SN/AFlagVec flags(NumFlags, false);
504074Sbinkertn@umich.edubool enabled = false;
512SN/A
522SN/A//
532SN/A// This variable holds the output stream for debug information.  Other
542SN/A// than setting up/redirecting this stream, do *NOT* reference this
552SN/A// directly; use DebugOut() (see below) to access this stream for
562SN/A// output.
572SN/A//
58488SN/Aostream *dprintf_stream = &cerr;
594046Sbinkertn@umich.eduostream &
604046Sbinkertn@umich.eduoutput()
614046Sbinkertn@umich.edu{
624046Sbinkertn@umich.edu    return *dprintf_stream;
634046Sbinkertn@umich.edu}
644046Sbinkertn@umich.edu
654046Sbinkertn@umich.eduvoid
664046Sbinkertn@umich.edusetOutput(const string &filename)
674046Sbinkertn@umich.edu{
684046Sbinkertn@umich.edu    dprintf_stream = simout.find(filename);
694046Sbinkertn@umich.edu}
702SN/A
711031SN/AObjectMatch ignore;
722SN/A
734046Sbinkertn@umich.eduvoid
744046Sbinkertn@umich.edudprintf(Tick when, const std::string &name, const char *format,
754046Sbinkertn@umich.edu        CPRINTF_DEFINITION)
764046Sbinkertn@umich.edu{
774046Sbinkertn@umich.edu    if (!name.empty() && ignore.match(name))
784046Sbinkertn@umich.edu        return;
792SN/A
804046Sbinkertn@umich.edu    std::ostream &os = *dprintf_stream;
812SN/A
822SN/A    string fmt = "";
834046Sbinkertn@umich.edu    CPrintfArgsList args(VARARGS_ALLARGS);
842SN/A
852SN/A    if (!name.empty()) {
862SN/A        fmt = "%s: " + fmt;
874039Sbinkertn@umich.edu        args.push_front(name);
882SN/A    }
892SN/A
904046Sbinkertn@umich.edu    if (when != (Tick)-1) {
912SN/A        fmt = "%7d: " + fmt;
924046Sbinkertn@umich.edu        args.push_front(when);
932SN/A    }
942SN/A
952SN/A    fmt += format;
962SN/A
974039Sbinkertn@umich.edu    ccprintf(os, fmt.c_str(), args);
982SN/A    os.flush();
992SN/A}
1002SN/A
1014046Sbinkertn@umich.eduvoid
1024046Sbinkertn@umich.edudump(Tick when, const std::string &name, const void *d, int len)
1032SN/A{
1044046Sbinkertn@umich.edu    if (!name.empty() && ignore.match(name))
1054046Sbinkertn@umich.edu        return;
1062SN/A
1074046Sbinkertn@umich.edu    std::ostream &os = *dprintf_stream;
1082SN/A
1094046Sbinkertn@umich.edu    string fmt = "";
1104046Sbinkertn@umich.edu    CPrintfArgsList args;
1114046Sbinkertn@umich.edu
1124046Sbinkertn@umich.edu    if (!name.empty()) {
1134046Sbinkertn@umich.edu        fmt = "%s: " + fmt;
1144046Sbinkertn@umich.edu        args.push_front(name);
1154046Sbinkertn@umich.edu    }
1164046Sbinkertn@umich.edu
1174046Sbinkertn@umich.edu    if (when != (Tick)-1) {
1184046Sbinkertn@umich.edu        fmt = "%7d: " + fmt;
1194046Sbinkertn@umich.edu        args.push_front(when);
1204046Sbinkertn@umich.edu    }
1214046Sbinkertn@umich.edu
1224046Sbinkertn@umich.edu    const char *data = static_cast<const char *>(d);
1232SN/A    int c, i, j;
1242SN/A    for (i = 0; i < len; i += 16) {
1254046Sbinkertn@umich.edu        ccprintf(os, fmt, args);
1264046Sbinkertn@umich.edu        ccprintf(os, "%08x  ", i);
1272SN/A        c = len - i;
1282SN/A        if (c > 16) c = 16;
1292SN/A
1302SN/A        for (j = 0; j < c; j++) {
1312SN/A            ccprintf(os, "%02x ", data[i + j] & 0xff);
1322SN/A            if ((j & 0xf) == 7 && j > 0)
1332SN/A                ccprintf(os, " ");
1342SN/A        }
1352SN/A
1362SN/A        for (; j < 16; j++)
1372SN/A            ccprintf(os, "   ");
1382SN/A        ccprintf(os, "  ");
1392SN/A
1402SN/A        for (j = 0; j < c; j++) {
1412SN/A            int ch = data[i + j] & 0x7f;
1424046Sbinkertn@umich.edu            ccprintf(os, "%c", (char)(isprint(ch) ? ch : ' '));
1432SN/A        }
1442SN/A
1452SN/A        ccprintf(os, "\n");
1462SN/A
1472SN/A        if (c < 16)
1482SN/A            break;
1492SN/A    }
1502SN/A}
1514046Sbinkertn@umich.edu
1524046Sbinkertn@umich.edubool
1534046Sbinkertn@umich.educhangeFlag(const char *s, bool value)
1544046Sbinkertn@umich.edu{
1554046Sbinkertn@umich.edu    using namespace Trace;
1564046Sbinkertn@umich.edu    std::string str(s);
1574046Sbinkertn@umich.edu
1584046Sbinkertn@umich.edu    for (int i = 0; i < numFlagStrings; ++i) {
1594046Sbinkertn@umich.edu        if (str != flagStrings[i])
1604046Sbinkertn@umich.edu            continue;
1614046Sbinkertn@umich.edu
1624046Sbinkertn@umich.edu        if (i < NumFlags) {
1634046Sbinkertn@umich.edu            flags[i] = value;
1644046Sbinkertn@umich.edu        } else {
1654046Sbinkertn@umich.edu            i -= NumFlags;
1664046Sbinkertn@umich.edu
1674046Sbinkertn@umich.edu            const Flags *flagVec = compoundFlags[i];
1684046Sbinkertn@umich.edu            for (int j = 0; flagVec[j] != -1; ++j) {
1694046Sbinkertn@umich.edu                if (flagVec[j] < NumFlags)
1704046Sbinkertn@umich.edu                    flags[flagVec[j]] = value;
1714046Sbinkertn@umich.edu            }
1724046Sbinkertn@umich.edu        }
1734046Sbinkertn@umich.edu
1744046Sbinkertn@umich.edu        return true;
1754046Sbinkertn@umich.edu    }
1764046Sbinkertn@umich.edu
1774046Sbinkertn@umich.edu    // the flag was not found.
1784046Sbinkertn@umich.edu    return false;
1794046Sbinkertn@umich.edu}
1804046Sbinkertn@umich.edu
1814046Sbinkertn@umich.eduvoid
1824046Sbinkertn@umich.edudumpStatus()
1834046Sbinkertn@umich.edu{
1844046Sbinkertn@umich.edu    using namespace Trace;
1854046Sbinkertn@umich.edu    for (int i = 0; i < numFlagStrings; ++i) {
1864046Sbinkertn@umich.edu        if (flags[i])
1874046Sbinkertn@umich.edu            cprintf("%s\n", flagStrings[i]);
1884046Sbinkertn@umich.edu    }
1894046Sbinkertn@umich.edu}
1904046Sbinkertn@umich.edu
1917811Ssteve.reinhardt@amd.com} // namespace Trace
1924046Sbinkertn@umich.edu
1934046Sbinkertn@umich.edu
1944046Sbinkertn@umich.edu// add a set of functions that can easily be invoked from gdb
1954212Ssaidi@eecs.umich.eduvoid
1964212Ssaidi@eecs.umich.edusetTraceFlag(const char *string)
1974212Ssaidi@eecs.umich.edu{
1984212Ssaidi@eecs.umich.edu    Trace::changeFlag(string, true);
1994212Ssaidi@eecs.umich.edu}
2004046Sbinkertn@umich.edu
2014212Ssaidi@eecs.umich.eduvoid
2024212Ssaidi@eecs.umich.educlearTraceFlag(const char *string)
2034212Ssaidi@eecs.umich.edu{
2044212Ssaidi@eecs.umich.edu    Trace::changeFlag(string, false);
2054212Ssaidi@eecs.umich.edu}
2064046Sbinkertn@umich.edu
2074212Ssaidi@eecs.umich.eduvoid
2084212Ssaidi@eecs.umich.edudumpTraceStatus()
2094212Ssaidi@eecs.umich.edu{
2104212Ssaidi@eecs.umich.edu    Trace::dumpStatus();
2114212Ssaidi@eecs.umich.edu}
212