trace.cc revision 8640
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); 678640SAli.Saidi@ARM.com if (!dprintf_stream) 688640SAli.Saidi@ARM.com dprintf_stream = simout.create(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 1527811Ssteve.reinhardt@amd.com} // namespace Trace 153