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

Generated on Wed Dec 21 2022 10:22:29 for gem5 by doxygen 1.9.1