trace.cc revision 10292
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"
412SN/A
422SN/Ausing namespace std;
432SN/A
442SN/Anamespace Trace {
458232Snate@binkert.org
462SN/Aconst string DefaultName("global");
474074Sbinkertn@umich.edubool enabled = false;
482SN/A
492SN/A//
502SN/A// This variable holds the output stream for debug information.  Other
512SN/A// than setting up/redirecting this stream, do *NOT* reference this
522SN/A// directly; use DebugOut() (see below) to access this stream for
532SN/A// output.
542SN/A//
55488SN/Aostream *dprintf_stream = &cerr;
564046Sbinkertn@umich.eduostream &
574046Sbinkertn@umich.eduoutput()
584046Sbinkertn@umich.edu{
594046Sbinkertn@umich.edu    return *dprintf_stream;
604046Sbinkertn@umich.edu}
614046Sbinkertn@umich.edu
624046Sbinkertn@umich.eduvoid
634046Sbinkertn@umich.edusetOutput(const string &filename)
644046Sbinkertn@umich.edu{
654046Sbinkertn@umich.edu    dprintf_stream = simout.find(filename);
668640SAli.Saidi@ARM.com    if (!dprintf_stream)
678640SAli.Saidi@ARM.com        dprintf_stream = simout.create(filename);
684046Sbinkertn@umich.edu}
692SN/A
701031SN/AObjectMatch ignore;
712SN/A
7210292SAndreas.Sandberg@ARM.com
7310292SAndreas.Sandberg@ARM.combool
7410292SAndreas.Sandberg@ARM.com__dprintf_prologue(Tick when, const std::string &name)
754046Sbinkertn@umich.edu{
764046Sbinkertn@umich.edu    if (!name.empty() && ignore.match(name))
7710292SAndreas.Sandberg@ARM.com        return false;
782SN/A
794046Sbinkertn@umich.edu    std::ostream &os = *dprintf_stream;
802SN/A
8110292SAndreas.Sandberg@ARM.com    if (when != MaxTick)
8210292SAndreas.Sandberg@ARM.com        ccprintf(os, "%7d: ", when);
832SN/A
8410292SAndreas.Sandberg@ARM.com    if (!name.empty())
8510292SAndreas.Sandberg@ARM.com        os << name << ": ";
862SN/A
8710292SAndreas.Sandberg@ARM.com    return true;
882SN/A}
892SN/A
904046Sbinkertn@umich.eduvoid
914046Sbinkertn@umich.edudump(Tick when, const std::string &name, const void *d, int len)
922SN/A{
9310292SAndreas.Sandberg@ARM.com    const char *data = static_cast<const char *>(d);
9410292SAndreas.Sandberg@ARM.com    std::ostream &os = *dprintf_stream;
9510292SAndreas.Sandberg@ARM.com    int c, i, j;
962SN/A
9710292SAndreas.Sandberg@ARM.com    for (i = 0; i < len; i += 16) {
9810292SAndreas.Sandberg@ARM.com        if (!__dprintf_prologue(when, name))
9910292SAndreas.Sandberg@ARM.com            return;
1002SN/A
1014046Sbinkertn@umich.edu        ccprintf(os, "%08x  ", i);
1022SN/A        c = len - i;
1032SN/A        if (c > 16) c = 16;
1042SN/A
1052SN/A        for (j = 0; j < c; j++) {
1062SN/A            ccprintf(os, "%02x ", data[i + j] & 0xff);
1072SN/A            if ((j & 0xf) == 7 && j > 0)
1082SN/A                ccprintf(os, " ");
1092SN/A        }
1102SN/A
1112SN/A        for (; j < 16; j++)
1122SN/A            ccprintf(os, "   ");
1132SN/A        ccprintf(os, "  ");
1142SN/A
1152SN/A        for (j = 0; j < c; j++) {
1162SN/A            int ch = data[i + j] & 0x7f;
1174046Sbinkertn@umich.edu            ccprintf(os, "%c", (char)(isprint(ch) ? ch : ' '));
1182SN/A        }
1192SN/A
1202SN/A        ccprintf(os, "\n");
1212SN/A
1222SN/A        if (c < 16)
1232SN/A            break;
1242SN/A    }
1252SN/A}
1264046Sbinkertn@umich.edu
1277811Ssteve.reinhardt@amd.com} // namespace Trace
128