Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 1 | /* |
Andrew Bardsley | 960935a | 2014-10-16 05:49:53 -0400 | [diff] [blame] | 2 | * Copyright (c) 2014 ARM Limited |
| 3 | * All rights reserved |
| 4 | * |
Nathan Binkert | 1f834b5 | 2007-02-07 22:11:30 -0800 | [diff] [blame] | 5 | * Copyright (c) 2001-2006 The Regents of The University of Michigan |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 6 | * 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 Saidi | cb0cf2d | 2006-05-31 19:26:56 -0400 | [diff] [blame] | 30 | * |
| 31 | * Authors: Nathan Binkert |
| 32 | * Steve Reinhardt |
Andrew Bardsley | 960935a | 2014-10-16 05:49:53 -0400 | [diff] [blame] | 33 | * Andrew Bardsley |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 34 | */ |
| 35 | |
Brandon Potter | 7a8dda4 | 2016-11-09 14:27:37 -0600 | [diff] [blame] | 36 | #include "base/trace.hh" |
| 37 | |
Nathan Binkert | 39a0556 | 2011-04-15 10:44:06 -0700 | [diff] [blame] | 38 | #include <cctype> |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 39 | #include <fstream> |
| 40 | #include <iostream> |
Andrew Bardsley | 960935a | 2014-10-16 05:49:53 -0400 | [diff] [blame] | 41 | #include <sstream> |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 42 | #include <string> |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 43 | |
Curtis Dunham | 02881a7 | 2015-09-30 15:21:55 -0500 | [diff] [blame] | 44 | #include "base/debug.hh" |
Gabe Black | 1088f0c | 2017-11-30 17:36:53 -0800 | [diff] [blame] | 45 | #include "base/logging.hh" |
Nathan Binkert | 184decd | 2007-02-10 15:14:50 -0800 | [diff] [blame] | 46 | #include "base/output.hh" |
Steve Reinhardt | 25693e9 | 2003-10-10 11:09:00 -0700 | [diff] [blame] | 47 | #include "base/str.hh" |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 48 | |
Andrew Bardsley | 960935a | 2014-10-16 05:49:53 -0400 | [diff] [blame] | 49 | const std::string &name() |
| 50 | { |
| 51 | static const std::string default_name("global"); |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 52 | |
Andrew Bardsley | 960935a | 2014-10-16 05:49:53 -0400 | [diff] [blame] | 53 | return default_name; |
| 54 | } |
Nathan Binkert | eddac53 | 2011-04-15 10:44:32 -0700 | [diff] [blame] | 55 | |
Andrew Bardsley | 960935a | 2014-10-16 05:49:53 -0400 | [diff] [blame] | 56 | namespace Trace |
| 57 | { |
| 58 | |
Andrew Bardsley | 960935a | 2014-10-16 05:49:53 -0400 | [diff] [blame] | 59 | // This variable holds the output logger for debug information. Other |
| 60 | // than setting up/redirecting this logger, do *NOT* reference this |
| 61 | // directly |
| 62 | |
| 63 | Logger *debug_logger = NULL; |
| 64 | |
| 65 | Logger * |
| 66 | getDebugLogger() |
| 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 | |
| 75 | std::ostream & |
Nathan Binkert | 184decd | 2007-02-10 15:14:50 -0800 | [diff] [blame] | 76 | output() |
| 77 | { |
Andrew Bardsley | 960935a | 2014-10-16 05:49:53 -0400 | [diff] [blame] | 78 | return getDebugLogger()->getOstream(); |
Nathan Binkert | 184decd | 2007-02-10 15:14:50 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void |
Andrew Bardsley | 960935a | 2014-10-16 05:49:53 -0400 | [diff] [blame] | 82 | setDebugLogger(Logger *logger) |
Nathan Binkert | 184decd | 2007-02-10 15:14:50 -0800 | [diff] [blame] | 83 | { |
Andrew Bardsley | 960935a | 2014-10-16 05:49:53 -0400 | [diff] [blame] | 84 | if (!logger) |
| 85 | warn("Trying to set debug logger to NULL\n"); |
| 86 | else |
| 87 | debug_logger = logger; |
Nathan Binkert | 184decd | 2007-02-10 15:14:50 -0800 | [diff] [blame] | 88 | } |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 89 | |
Curtis Dunham | 02881a7 | 2015-09-30 15:21:55 -0500 | [diff] [blame] | 90 | void |
| 91 | enable() |
| 92 | { |
| 93 | Debug::SimpleFlag::enableAll(); |
| 94 | } |
| 95 | |
| 96 | void |
| 97 | disable() |
| 98 | { |
| 99 | Debug::SimpleFlag::disableAll(); |
| 100 | } |
| 101 | |
Nathan Binkert | 3711ea7 | 2004-07-30 10:47:53 -0400 | [diff] [blame] | 102 | ObjectMatch ignore; |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 103 | |
Andrew Bardsley | 960935a | 2014-10-16 05:49:53 -0400 | [diff] [blame] | 104 | void |
| 105 | Logger::dump(Tick when, const std::string &name, const void *d, int len) |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 106 | { |
Nathan Binkert | 184decd | 2007-02-10 15:14:50 -0800 | [diff] [blame] | 107 | if (!name.empty() && ignore.match(name)) |
Andrew Bardsley | 960935a | 2014-10-16 05:49:53 -0400 | [diff] [blame] | 108 | return; |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 109 | |
Nathan Binkert | 184decd | 2007-02-10 15:14:50 -0800 | [diff] [blame] | 110 | const char *data = static_cast<const char *>(d); |
| 111 | int c, i, j; |
Andreas Sandberg | 70176fe | 2014-08-26 10:13:45 -0400 | [diff] [blame] | 112 | |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 113 | for (i = 0; i < len; i += 16) { |
Andrew Bardsley | 960935a | 2014-10-16 05:49:53 -0400 | [diff] [blame] | 114 | std::ostringstream line; |
Andreas Sandberg | 70176fe | 2014-08-26 10:13:45 -0400 | [diff] [blame] | 115 | |
Andrew Bardsley | 960935a | 2014-10-16 05:49:53 -0400 | [diff] [blame] | 116 | ccprintf(line, "%08x ", i); |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 117 | c = len - i; |
| 118 | if (c > 16) c = 16; |
| 119 | |
| 120 | for (j = 0; j < c; j++) { |
Andrew Bardsley | 960935a | 2014-10-16 05:49:53 -0400 | [diff] [blame] | 121 | ccprintf(line, "%02x ", data[i + j] & 0xff); |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 122 | if ((j & 0xf) == 7 && j > 0) |
Andrew Bardsley | 960935a | 2014-10-16 05:49:53 -0400 | [diff] [blame] | 123 | ccprintf(line, " "); |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | for (; j < 16; j++) |
Andrew Bardsley | 960935a | 2014-10-16 05:49:53 -0400 | [diff] [blame] | 127 | ccprintf(line, " "); |
| 128 | ccprintf(line, " "); |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 129 | |
| 130 | for (j = 0; j < c; j++) { |
| 131 | int ch = data[i + j] & 0x7f; |
Andrew Bardsley | 960935a | 2014-10-16 05:49:53 -0400 | [diff] [blame] | 132 | ccprintf(line, "%c", (char)(isprint(ch) ? ch : ' ')); |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 133 | } |
| 134 | |
Andrew Bardsley | 960935a | 2014-10-16 05:49:53 -0400 | [diff] [blame] | 135 | ccprintf(line, "\n"); |
| 136 | logMessage(when, name, line.str()); |
Steve Raasch | 92638f9 | 2003-10-07 10:41:54 -0400 | [diff] [blame] | 137 | |
| 138 | if (c < 16) |
| 139 | break; |
| 140 | } |
| 141 | } |
Nathan Binkert | 184decd | 2007-02-10 15:14:50 -0800 | [diff] [blame] | 142 | |
Andrew Bardsley | 960935a | 2014-10-16 05:49:53 -0400 | [diff] [blame] | 143 | void |
| 144 | OstreamLogger::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 Reinhardt | c69d48f | 2011-01-03 14:35:43 -0800 | [diff] [blame] | 160 | } // namespace Trace |