gem5  v19.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
trace.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, 2019 ARM Limited
3  * All rights reserved
4  *
5  * Copyright (c) 2001-2006 The Regents of The University of Michigan
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.
30  *
31  * Authors: Nathan Binkert
32  * Steve Reinhardt
33  * Andrew Bardsley
34  */
35 
36 #include "base/trace.hh"
37 
38 #include <cctype>
39 #include <fstream>
40 #include <iostream>
41 #include <sstream>
42 #include <string>
43 
44 #include "base/atomicio.hh"
45 #include "base/debug.hh"
46 #include "base/logging.hh"
47 #include "base/output.hh"
48 #include "base/str.hh"
49 #include "debug/FmtFlag.hh"
50 #include "debug/FmtStackTrace.hh"
51 #include "debug/FmtTicksOff.hh"
52 #include "sim/backtrace.hh"
53 
54 const std::string &name()
55 {
56  static const std::string default_name("global");
57 
58  return default_name;
59 }
60 
61 namespace Trace
62 {
63 
64 // This variable holds the output logger for debug information. Other
65 // than setting up/redirecting this logger, do *NOT* reference this
66 // directly
67 
69 
70 Logger *
72 {
73  /* Set a default logger to cerr when no other logger is set */
74  if (!debug_logger)
75  debug_logger = new OstreamLogger(std::cerr);
76 
77  return debug_logger;
78 }
79 
80 std::ostream &
82 {
83  return getDebugLogger()->getOstream();
84 }
85 
86 void
88 {
89  if (!logger)
90  warn("Trying to set debug logger to NULL\n");
91  else
92  debug_logger = logger;
93 }
94 
95 void
97 {
99 }
100 
101 void
103 {
105 }
106 
108 
109 
110 void
111 Logger::dump(Tick when, const std::string &name,
112  const void *d, int len, const std::string &flag)
113 {
114  if (!name.empty() && ignore.match(name))
115  return;
116 
117  const char *data = static_cast<const char *>(d);
118  int c, i, j;
119 
120  for (i = 0; i < len; i += 16) {
121  std::ostringstream line;
122 
123  ccprintf(line, "%08x ", i);
124  c = len - i;
125  if (c > 16) c = 16;
126 
127  for (j = 0; j < c; j++) {
128  ccprintf(line, "%02x ", data[i + j] & 0xff);
129  if ((j & 0xf) == 7 && j > 0)
130  ccprintf(line, " ");
131  }
132 
133  for (; j < 16; j++)
134  ccprintf(line, " ");
135  ccprintf(line, " ");
136 
137  for (j = 0; j < c; j++) {
138  int ch = data[i + j] & 0x7f;
139  ccprintf(line, "%c", (char)(isprint(ch) ? ch : ' '));
140  }
141 
142  ccprintf(line, "\n");
143  logMessage(when, name, flag, line.str());
144 
145  if (c < 16)
146  break;
147  }
148 }
149 
150 void
151 OstreamLogger::logMessage(Tick when, const std::string &name,
152  const std::string &flag, const std::string &message)
153 {
154  if (!name.empty() && ignore.match(name))
155  return;
156 
157  if (!DTRACE(FmtTicksOff) && (when != MaxTick))
158  ccprintf(stream, "%7d: ", when);
159 
160  if (DTRACE(FmtFlag) && !flag.empty())
161  stream << flag << ": ";
162 
163  if (!name.empty())
164  stream << name << ": ";
165 
166  stream << message;
167  stream.flush();
168 
169  if (DTRACE(FmtStackTrace)) {
170  print_backtrace();
171  STATIC_ERR("\n");
172  }
173 }
174 
175 } // namespace Trace
void ccprintf(cp::Print &print)
Definition: cprintf.hh:131
const std::string & name()
Definition: trace.cc:54
Bitfield< 7 > i
void print_backtrace()
Print a gem5 post-mortem report.
const Tick MaxTick
Definition: types.hh:65
bool match(const std::string &name) const
Definition: match.hh:69
virtual std::ostream & getOstream()=0
Return an ostream that can be used to send messages to the &#39;same place&#39; as formatted logMessage messa...
#define DTRACE(x)
Definition: trace.hh:227
uint64_t Tick
Tick count type.
Definition: types.hh:63
Debug logging base class.
Definition: trace.hh:51
Bitfield< 9 > d
Bitfield< 18, 16 > len
std::ostream & output()
Get the ostream from the current global logger.
Definition: trace.cc:81
Bitfield< 24 > j
Logging wrapper for ostreams with the format: <when>: <name>: <message-body>
Definition: trace.hh:105
virtual void logMessage(Tick when, const std::string &name, const std::string &flag, const std::string &message)=0
Log formatted message.
ObjectMatch ignore
Definition: trace.cc:107
Logger * debug_logger
Definition: trace.cc:68
Logger * getDebugLogger()
Get the current global debug logger.
Definition: trace.cc:71
Bitfield< 29 > c
#define STATIC_ERR(m)
Statically allocate a string and write it to STDERR.
Definition: atomicio.hh:66
void enable()
Enable/disable debug logging.
Definition: trace.cc:96
void setDebugLogger(Logger *logger)
Delete the current global logger and assign a new one.
Definition: trace.cc:87
ObjectMatch contains a vector of expressions.
Definition: match.hh:56
#define warn(...)
Definition: logging.hh:212
void dump(Tick when, const std::string &name, const void *d, int len, const std::string &flag)
Dump a block of data of length len.
Definition: trace.cc:111
void logMessage(Tick when, const std::string &name, const std::string &flag, const std::string &message) override
Log formatted message.
Definition: trace.cc:151
const char data[]
static void disableAll()
Definition: debug.cc:109
static void enableAll()
Definition: debug.cc:101
void disable()
Definition: trace.cc:102

Generated on Fri Feb 28 2020 16:26:58 for gem5 by doxygen 1.8.13