trace.cc revision 11153
12SN/A/* 210475SAndrew.Bardsley@arm.com * Copyright (c) 2014 ARM Limited 310475SAndrew.Bardsley@arm.com * All rights reserved 410475SAndrew.Bardsley@arm.com * 54039Sbinkertn@umich.edu * Copyright (c) 2001-2006 The Regents of The University of Michigan 62SN/A * All rights reserved. 72SN/A * 82SN/A * Redistribution and use in source and binary forms, with or without 92SN/A * modification, are permitted provided that the following conditions are 102SN/A * met: redistributions of source code must retain the above copyright 112SN/A * notice, this list of conditions and the following disclaimer; 122SN/A * redistributions in binary form must reproduce the above copyright 132SN/A * notice, this list of conditions and the following disclaimer in the 142SN/A * documentation and/or other materials provided with the distribution; 152SN/A * neither the name of the copyright holders nor the names of its 162SN/A * contributors may be used to endorse or promote products derived from 172SN/A * this software without specific prior written permission. 182SN/A * 192SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 202SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 212SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 222SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 232SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 242SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 252SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 262SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 272SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 282SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 292SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 302665Ssaidi@eecs.umich.edu * 312665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert 322665Ssaidi@eecs.umich.edu * Steve Reinhardt 3310475SAndrew.Bardsley@arm.com * Andrew Bardsley 342SN/A */ 352SN/A 368229Snate@binkert.org#include <cctype> 372SN/A#include <fstream> 382SN/A#include <iostream> 3910475SAndrew.Bardsley@arm.com#include <sstream> 402SN/A#include <string> 412SN/A 4211153SCurtis.Dunham@arm.com#include "base/debug.hh" 4356SN/A#include "base/misc.hh" 444046Sbinkertn@umich.edu#include "base/output.hh" 454046Sbinkertn@umich.edu#include "base/str.hh" 4656SN/A#include "base/trace.hh" 472SN/A 4810475SAndrew.Bardsley@arm.comconst std::string &name() 4910475SAndrew.Bardsley@arm.com{ 5010475SAndrew.Bardsley@arm.com static const std::string default_name("global"); 512SN/A 5210475SAndrew.Bardsley@arm.com return default_name; 5310475SAndrew.Bardsley@arm.com} 548232Snate@binkert.org 5510475SAndrew.Bardsley@arm.comnamespace Trace 5610475SAndrew.Bardsley@arm.com{ 5710475SAndrew.Bardsley@arm.com 5810475SAndrew.Bardsley@arm.com// This variable holds the output logger for debug information. Other 5910475SAndrew.Bardsley@arm.com// than setting up/redirecting this logger, do *NOT* reference this 6010475SAndrew.Bardsley@arm.com// directly 6110475SAndrew.Bardsley@arm.com 6210475SAndrew.Bardsley@arm.comLogger *debug_logger = NULL; 6310475SAndrew.Bardsley@arm.com 6410475SAndrew.Bardsley@arm.comLogger * 6510475SAndrew.Bardsley@arm.comgetDebugLogger() 6610475SAndrew.Bardsley@arm.com{ 6710475SAndrew.Bardsley@arm.com /* Set a default logger to cerr when no other logger is set */ 6810475SAndrew.Bardsley@arm.com if (!debug_logger) 6910475SAndrew.Bardsley@arm.com debug_logger = new OstreamLogger(std::cerr); 7010475SAndrew.Bardsley@arm.com 7110475SAndrew.Bardsley@arm.com return debug_logger; 7210475SAndrew.Bardsley@arm.com} 7310475SAndrew.Bardsley@arm.com 7410475SAndrew.Bardsley@arm.comstd::ostream & 754046Sbinkertn@umich.eduoutput() 764046Sbinkertn@umich.edu{ 7710475SAndrew.Bardsley@arm.com return getDebugLogger()->getOstream(); 784046Sbinkertn@umich.edu} 794046Sbinkertn@umich.edu 804046Sbinkertn@umich.eduvoid 8110475SAndrew.Bardsley@arm.comsetDebugLogger(Logger *logger) 824046Sbinkertn@umich.edu{ 8310475SAndrew.Bardsley@arm.com if (!logger) 8410475SAndrew.Bardsley@arm.com warn("Trying to set debug logger to NULL\n"); 8510475SAndrew.Bardsley@arm.com else 8610475SAndrew.Bardsley@arm.com debug_logger = logger; 874046Sbinkertn@umich.edu} 882SN/A 8911153SCurtis.Dunham@arm.comvoid 9011153SCurtis.Dunham@arm.comenable() 9111153SCurtis.Dunham@arm.com{ 9211153SCurtis.Dunham@arm.com Debug::SimpleFlag::enableAll(); 9311153SCurtis.Dunham@arm.com} 9411153SCurtis.Dunham@arm.com 9511153SCurtis.Dunham@arm.comvoid 9611153SCurtis.Dunham@arm.comdisable() 9711153SCurtis.Dunham@arm.com{ 9811153SCurtis.Dunham@arm.com Debug::SimpleFlag::disableAll(); 9911153SCurtis.Dunham@arm.com} 10011153SCurtis.Dunham@arm.com 1011031SN/AObjectMatch ignore; 1022SN/A 10310475SAndrew.Bardsley@arm.comvoid 10410475SAndrew.Bardsley@arm.comLogger::dump(Tick when, const std::string &name, const void *d, int len) 1054046Sbinkertn@umich.edu{ 1064046Sbinkertn@umich.edu if (!name.empty() && ignore.match(name)) 10710475SAndrew.Bardsley@arm.com return; 1082SN/A 10910292SAndreas.Sandberg@ARM.com const char *data = static_cast<const char *>(d); 11010292SAndreas.Sandberg@ARM.com int c, i, j; 1112SN/A 11210292SAndreas.Sandberg@ARM.com for (i = 0; i < len; i += 16) { 11310475SAndrew.Bardsley@arm.com std::ostringstream line; 1142SN/A 11510475SAndrew.Bardsley@arm.com ccprintf(line, "%08x ", i); 1162SN/A c = len - i; 1172SN/A if (c > 16) c = 16; 1182SN/A 1192SN/A for (j = 0; j < c; j++) { 12010475SAndrew.Bardsley@arm.com ccprintf(line, "%02x ", data[i + j] & 0xff); 1212SN/A if ((j & 0xf) == 7 && j > 0) 12210475SAndrew.Bardsley@arm.com ccprintf(line, " "); 1232SN/A } 1242SN/A 1252SN/A for (; j < 16; j++) 12610475SAndrew.Bardsley@arm.com ccprintf(line, " "); 12710475SAndrew.Bardsley@arm.com ccprintf(line, " "); 1282SN/A 1292SN/A for (j = 0; j < c; j++) { 1302SN/A int ch = data[i + j] & 0x7f; 13110475SAndrew.Bardsley@arm.com ccprintf(line, "%c", (char)(isprint(ch) ? ch : ' ')); 1322SN/A } 1332SN/A 13410475SAndrew.Bardsley@arm.com ccprintf(line, "\n"); 13510475SAndrew.Bardsley@arm.com logMessage(when, name, line.str()); 1362SN/A 1372SN/A if (c < 16) 1382SN/A break; 1392SN/A } 1402SN/A} 1414046Sbinkertn@umich.edu 14210475SAndrew.Bardsley@arm.comvoid 14310475SAndrew.Bardsley@arm.comOstreamLogger::logMessage(Tick when, const std::string &name, 14410475SAndrew.Bardsley@arm.com const std::string &message) 14510475SAndrew.Bardsley@arm.com{ 14610475SAndrew.Bardsley@arm.com if (!name.empty() && ignore.match(name)) 14710475SAndrew.Bardsley@arm.com return; 14810475SAndrew.Bardsley@arm.com 14910475SAndrew.Bardsley@arm.com if (when != MaxTick) 15010475SAndrew.Bardsley@arm.com ccprintf(stream, "%7d: ", when); 15110475SAndrew.Bardsley@arm.com 15210475SAndrew.Bardsley@arm.com if (!name.empty()) 15310475SAndrew.Bardsley@arm.com stream << name << ": "; 15410475SAndrew.Bardsley@arm.com 15510475SAndrew.Bardsley@arm.com stream << message; 15610475SAndrew.Bardsley@arm.com stream.flush(); 15710475SAndrew.Bardsley@arm.com} 15810475SAndrew.Bardsley@arm.com 1597811Ssteve.reinhardt@amd.com} // namespace Trace 160