trace.cc revision 10292:933dfb9d8279
11689SN/A/*
22329SN/A * Copyright (c) 2001-2006 The Regents of The University of Michigan
31689SN/A * All rights reserved.
41689SN/A *
51689SN/A * Redistribution and use in source and binary forms, with or without
61689SN/A * modification, are permitted provided that the following conditions are
71689SN/A * met: redistributions of source code must retain the above copyright
81689SN/A * notice, this list of conditions and the following disclaimer;
91689SN/A * redistributions in binary form must reproduce the above copyright
101689SN/A * notice, this list of conditions and the following disclaimer in the
111689SN/A * documentation and/or other materials provided with the distribution;
121689SN/A * neither the name of the copyright holders nor the names of its
131689SN/A * contributors may be used to endorse or promote products derived from
141689SN/A * this software without specific prior written permission.
151689SN/A *
161689SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
171689SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
181689SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
191689SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
201689SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
211689SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
221689SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
231689SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
241689SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
251689SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
261689SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert
291689SN/A *          Steve Reinhardt
301061SN/A */
311061SN/A
321061SN/A#include <cctype>
332292SN/A#include <fstream>
341717SN/A#include <iostream>
358232Snate@binkert.org#include <string>
365529Snate@binkert.org
375529Snate@binkert.org#include "base/misc.hh"
381061SN/A#include "base/output.hh"
393500Sktlim@umich.edu#include "base/str.hh"
403500Sktlim@umich.edu#include "base/trace.hh"
413500Sktlim@umich.edu
423500Sktlim@umich.eduusing namespace std;
433500Sktlim@umich.edu
443500Sktlim@umich.edunamespace Trace {
453500Sktlim@umich.edu
465529Snate@binkert.orgconst string DefaultName("global");
476005Snate@binkert.orgbool enabled = false;
488519SAli.Saidi@ARM.com
498519SAli.Saidi@ARM.com//
508519SAli.Saidi@ARM.com// This variable holds the output stream for debug information.  Other
518519SAli.Saidi@ARM.com// than setting up/redirecting this stream, do *NOT* reference this
521061SN/A// directly; use DebugOut() (see below) to access this stream for
532292SN/A// output.
542292SN/A//
552292SN/Aostream *dprintf_stream = &cerr;
562292SN/Aostream &
572292SN/Aoutput()
582292SN/A{
596221Snate@binkert.org    return *dprintf_stream;
602292SN/A}
612292SN/A
622292SN/Avoid
632292SN/AsetOutput(const string &filename)
642292SN/A{
652292SN/A    dprintf_stream = simout.find(filename);
662292SN/A    if (!dprintf_stream)
672292SN/A        dprintf_stream = simout.create(filename);
682292SN/A}
692292SN/A
702292SN/AObjectMatch ignore;
712292SN/A
722292SN/A
732292SN/Abool
742292SN/A__dprintf_prologue(Tick when, const std::string &name)
752292SN/A{
762678Sktlim@umich.edu    if (!name.empty() && ignore.match(name))
772292SN/A        return false;
782678Sktlim@umich.edu
792292SN/A    std::ostream &os = *dprintf_stream;
802292SN/A
812292SN/A    if (when != MaxTick)
822292SN/A        ccprintf(os, "%7d: ", when);
836221Snate@binkert.org
842292SN/A    if (!name.empty())
852292SN/A        os << name << ": ";
862292SN/A
876005Snate@binkert.org    return true;
882292SN/A}
892292SN/A
908519SAli.Saidi@ARM.comvoid
918519SAli.Saidi@ARM.comdump(Tick when, const std::string &name, const void *d, int len)
921061SN/A{
931061SN/A    const char *data = static_cast<const char *>(d);
941061SN/A    std::ostream &os = *dprintf_stream;
951061SN/A    int c, i, j;
961062SN/A
971062SN/A    for (i = 0; i < len; i += 16) {
981062SN/A        if (!__dprintf_prologue(when, name))
996005Snate@binkert.org            return;
1001062SN/A
1011062SN/A        ccprintf(os, "%08x  ", i);
1021062SN/A        c = len - i;
1036005Snate@binkert.org        if (c > 16) c = 16;
1041062SN/A
1051062SN/A        for (j = 0; j < c; j++) {
1061062SN/A            ccprintf(os, "%02x ", data[i + j] & 0xff);
1076005Snate@binkert.org            if ((j & 0xf) == 7 && j > 0)
1081062SN/A                ccprintf(os, " ");
1091062SN/A        }
1101062SN/A
1116005Snate@binkert.org        for (; j < 16; j++)
1121062SN/A            ccprintf(os, "   ");
1131062SN/A        ccprintf(os, "  ");
1141062SN/A
1151062SN/A        for (j = 0; j < c; j++) {
1161062SN/A            int ch = data[i + j] & 0x7f;
1172307SN/A            ccprintf(os, "%c", (char)(isprint(ch) ? ch : ' '));
1182307SN/A        }
1192367SN/A
1202367SN/A        ccprintf(os, "\n");
1212367SN/A
1222348SN/A        if (c < 16)
1232307SN/A            break;
1242307SN/A    }
1252307SN/A}
1262307SN/A
1272307SN/A} // namespace Trace
1282307SN/A