blob: 06f9eeb2157d36fc7623b8f7ff58b26dff2f46ad [file] [log] [blame]
Steve Raasch92638f92003-10-07 10:41:54 -04001/*
Andrew Bardsley960935a2014-10-16 05:49:53 -04002 * Copyright (c) 2014 ARM Limited
3 * All rights reserved
4 *
Nathan Binkert1f834b52007-02-07 22:11:30 -08005 * Copyright (c) 2001-2006 The Regents of The University of Michigan
Steve Raasch92638f92003-10-07 10:41:54 -04006 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met: redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer;
12 * redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution;
15 * neither the name of the copyright holders nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Ali Saidicb0cf2d2006-05-31 19:26:56 -040030 *
31 * Authors: Nathan Binkert
32 * Steve Reinhardt
Andrew Bardsley960935a2014-10-16 05:49:53 -040033 * Andrew Bardsley
Steve Raasch92638f92003-10-07 10:41:54 -040034 */
35
Brandon Potter7a8dda42016-11-09 14:27:37 -060036#include "base/trace.hh"
37
Nathan Binkert39a05562011-04-15 10:44:06 -070038#include <cctype>
Steve Raasch92638f92003-10-07 10:41:54 -040039#include <fstream>
40#include <iostream>
Andrew Bardsley960935a2014-10-16 05:49:53 -040041#include <sstream>
Steve Raasch92638f92003-10-07 10:41:54 -040042#include <string>
Steve Raasch92638f92003-10-07 10:41:54 -040043
Curtis Dunham02881a72015-09-30 15:21:55 -050044#include "base/debug.hh"
Gabe Black1088f0c2017-11-30 17:36:53 -080045#include "base/logging.hh"
Nathan Binkert184decd2007-02-10 15:14:50 -080046#include "base/output.hh"
Steve Reinhardt25693e92003-10-10 11:09:00 -070047#include "base/str.hh"
Steve Raasch92638f92003-10-07 10:41:54 -040048
Andrew Bardsley960935a2014-10-16 05:49:53 -040049const std::string &name()
50{
51 static const std::string default_name("global");
Steve Raasch92638f92003-10-07 10:41:54 -040052
Andrew Bardsley960935a2014-10-16 05:49:53 -040053 return default_name;
54}
Nathan Binkerteddac532011-04-15 10:44:32 -070055
Andrew Bardsley960935a2014-10-16 05:49:53 -040056namespace Trace
57{
58
Andrew Bardsley960935a2014-10-16 05:49:53 -040059// This variable holds the output logger for debug information. Other
60// than setting up/redirecting this logger, do *NOT* reference this
61// directly
62
63Logger *debug_logger = NULL;
64
65Logger *
66getDebugLogger()
67{
68 /* Set a default logger to cerr when no other logger is set */
69 if (!debug_logger)
70 debug_logger = new OstreamLogger(std::cerr);
71
72 return debug_logger;
73}
74
75std::ostream &
Nathan Binkert184decd2007-02-10 15:14:50 -080076output()
77{
Andrew Bardsley960935a2014-10-16 05:49:53 -040078 return getDebugLogger()->getOstream();
Nathan Binkert184decd2007-02-10 15:14:50 -080079}
80
81void
Andrew Bardsley960935a2014-10-16 05:49:53 -040082setDebugLogger(Logger *logger)
Nathan Binkert184decd2007-02-10 15:14:50 -080083{
Andrew Bardsley960935a2014-10-16 05:49:53 -040084 if (!logger)
85 warn("Trying to set debug logger to NULL\n");
86 else
87 debug_logger = logger;
Nathan Binkert184decd2007-02-10 15:14:50 -080088}
Steve Raasch92638f92003-10-07 10:41:54 -040089
Curtis Dunham02881a72015-09-30 15:21:55 -050090void
91enable()
92{
93 Debug::SimpleFlag::enableAll();
94}
95
96void
97disable()
98{
99 Debug::SimpleFlag::disableAll();
100}
101
Nathan Binkert3711ea72004-07-30 10:47:53 -0400102ObjectMatch ignore;
Steve Raasch92638f92003-10-07 10:41:54 -0400103
Andrew Bardsley960935a2014-10-16 05:49:53 -0400104void
105Logger::dump(Tick when, const std::string &name, const void *d, int len)
Steve Raasch92638f92003-10-07 10:41:54 -0400106{
Nathan Binkert184decd2007-02-10 15:14:50 -0800107 if (!name.empty() && ignore.match(name))
Andrew Bardsley960935a2014-10-16 05:49:53 -0400108 return;
Steve Raasch92638f92003-10-07 10:41:54 -0400109
Nathan Binkert184decd2007-02-10 15:14:50 -0800110 const char *data = static_cast<const char *>(d);
111 int c, i, j;
Andreas Sandberg70176fe2014-08-26 10:13:45 -0400112
Steve Raasch92638f92003-10-07 10:41:54 -0400113 for (i = 0; i < len; i += 16) {
Andrew Bardsley960935a2014-10-16 05:49:53 -0400114 std::ostringstream line;
Andreas Sandberg70176fe2014-08-26 10:13:45 -0400115
Andrew Bardsley960935a2014-10-16 05:49:53 -0400116 ccprintf(line, "%08x ", i);
Steve Raasch92638f92003-10-07 10:41:54 -0400117 c = len - i;
118 if (c > 16) c = 16;
119
120 for (j = 0; j < c; j++) {
Andrew Bardsley960935a2014-10-16 05:49:53 -0400121 ccprintf(line, "%02x ", data[i + j] & 0xff);
Steve Raasch92638f92003-10-07 10:41:54 -0400122 if ((j & 0xf) == 7 && j > 0)
Andrew Bardsley960935a2014-10-16 05:49:53 -0400123 ccprintf(line, " ");
Steve Raasch92638f92003-10-07 10:41:54 -0400124 }
125
126 for (; j < 16; j++)
Andrew Bardsley960935a2014-10-16 05:49:53 -0400127 ccprintf(line, " ");
128 ccprintf(line, " ");
Steve Raasch92638f92003-10-07 10:41:54 -0400129
130 for (j = 0; j < c; j++) {
131 int ch = data[i + j] & 0x7f;
Andrew Bardsley960935a2014-10-16 05:49:53 -0400132 ccprintf(line, "%c", (char)(isprint(ch) ? ch : ' '));
Steve Raasch92638f92003-10-07 10:41:54 -0400133 }
134
Andrew Bardsley960935a2014-10-16 05:49:53 -0400135 ccprintf(line, "\n");
136 logMessage(when, name, line.str());
Steve Raasch92638f92003-10-07 10:41:54 -0400137
138 if (c < 16)
139 break;
140 }
141}
Nathan Binkert184decd2007-02-10 15:14:50 -0800142
Andrew Bardsley960935a2014-10-16 05:49:53 -0400143void
144OstreamLogger::logMessage(Tick when, const std::string &name,
145 const std::string &message)
146{
147 if (!name.empty() && ignore.match(name))
148 return;
149
150 if (when != MaxTick)
151 ccprintf(stream, "%7d: ", when);
152
153 if (!name.empty())
154 stream << name << ": ";
155
156 stream << message;
157 stream.flush();
158}
159
Steve Reinhardtc69d48f2011-01-03 14:35:43 -0800160} // namespace Trace