trace.cc revision 10292:933dfb9d8279
12SN/A/*
21762SN/A * 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.
272665SN/A *
282665SN/A * Authors: Nathan Binkert
292SN/A *          Steve Reinhardt
302SN/A */
312SN/A
322SN/A#include <cctype>
332SN/A#include <fstream>
3411263Sandreas.sandberg@arm.com#include <iostream>
352SN/A#include <string>
362SN/A
372SN/A#include "base/misc.hh"
381015SN/A#include "base/output.hh"
392SN/A#include "base/str.hh"
402SN/A#include "base/trace.hh"
41599SN/A
421388SN/Ausing namespace std;
434167SN/A
442SN/Anamespace Trace {
452SN/A
462SN/Aconst string DefaultName("global");
475034SN/Abool enabled = false;
485402SN/A
495034SN/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
535543SN/A// output.
545543SN/A//
555543SN/Aostream *dprintf_stream = &cerr;
565543SN/Aostream &
572SN/Aoutput()
582SN/A{
592SN/A    return *dprintf_stream;
602SN/A}
612SN/A
625543SN/Avoid
635543SN/AsetOutput(const string &filename)
645543SN/A{
655543SN/A    dprintf_stream = simout.find(filename);
662SN/A    if (!dprintf_stream)
672SN/A        dprintf_stream = simout.create(filename);
682SN/A}
69599SN/A
70599SN/AObjectMatch ignore;
715543SN/A
725543SN/A
732SN/Abool
742SN/A__dprintf_prologue(Tick when, const std::string &name)
752SN/A{
762SN/A    if (!name.empty() && ignore.match(name))
772SN/A        return false;
782SN/A
792SN/A    std::ostream &os = *dprintf_stream;
802SN/A
812SN/A    if (when != MaxTick)
822SN/A        ccprintf(os, "%7d: ", when);
835878SN/A
842SN/A    if (!name.empty())
852SN/A        os << name << ": ";
862SN/A
872SN/A    return true;
885402SN/A}
892SN/A
905402SN/Avoid
912SN/Adump(Tick when, const std::string &name, const void *d, int len)
922SN/A{
932SN/A    const char *data = static_cast<const char *>(d);
942566SN/A    std::ostream &os = *dprintf_stream;
952SN/A    int c, i, j;
962SN/A
977823SN/A    for (i = 0; i < len; i += 16) {
987823SN/A        if (!__dprintf_prologue(when, name))
991015SN/A            return;
1002SN/A
1015402SN/A        ccprintf(os, "%08x  ", i);
1025402SN/A        c = len - i;
1035402SN/A        if (c > 16) c = 16;
1042SN/A
1052SN/A        for (j = 0; j < c; j++) {
1064762SN/A            ccprintf(os, "%02x ", data[i + j] & 0xff);
1074762SN/A            if ((j & 0xf) == 7 && j > 0)
1082SN/A                ccprintf(os, " ");
1095034SN/A        }
1102SN/A
111        for (; j < 16; j++)
112            ccprintf(os, "   ");
113        ccprintf(os, "  ");
114
115        for (j = 0; j < c; j++) {
116            int ch = data[i + j] & 0x7f;
117            ccprintf(os, "%c", (char)(isprint(ch) ? ch : ' '));
118        }
119
120        ccprintf(os, "\n");
121
122        if (c < 16)
123            break;
124    }
125}
126
127} // namespace Trace
128