gem5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
trace.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 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/debug.hh"
45 #include "base/misc.hh"
46 #include "base/output.hh"
47 #include "base/str.hh"
48 
49 const std::string &name()
50 {
51  static const std::string default_name("global");
52 
53  return default_name;
54 }
55 
56 namespace Trace
57 {
58 
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 
64 
65 Logger *
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 &
77 {
78  return getDebugLogger()->getOstream();
79 }
80 
81 void
83 {
84  if (!logger)
85  warn("Trying to set debug logger to NULL\n");
86  else
87  debug_logger = logger;
88 }
89 
90 void
92 {
94 }
95 
96 void
98 {
100 }
101 
103 
104 void
105 Logger::dump(Tick when, const std::string &name, const void *d, int len)
106 {
107  if (!name.empty() && ignore.match(name))
108  return;
109 
110  const char *data = static_cast<const char *>(d);
111  int c, i, j;
112 
113  for (i = 0; i < len; i += 16) {
114  std::ostringstream line;
115 
116  ccprintf(line, "%08x ", i);
117  c = len - i;
118  if (c > 16) c = 16;
119 
120  for (j = 0; j < c; j++) {
121  ccprintf(line, "%02x ", data[i + j] & 0xff);
122  if ((j & 0xf) == 7 && j > 0)
123  ccprintf(line, " ");
124  }
125 
126  for (; j < 16; j++)
127  ccprintf(line, " ");
128  ccprintf(line, " ");
129 
130  for (j = 0; j < c; j++) {
131  int ch = data[i + j] & 0x7f;
132  ccprintf(line, "%c", (char)(isprint(ch) ? ch : ' '));
133  }
134 
135  ccprintf(line, "\n");
136  logMessage(when, name, line.str());
137 
138  if (c < 16)
139  break;
140  }
141 }
142 
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 
160 } // namespace Trace
void ccprintf(cp::Print &print)
Definition: cprintf.hh:130
std::ostream & stream
Definition: trace.hh:97
const std::string & name()
Definition: trace.cc:49
Bitfield< 7 > i
Definition: miscregs.hh:1378
bool match(const std::string &name) const
Definition: match.hh:52
const char data[]
Definition: circlebuf.cc:43
#define warn(...)
Definition: misc.hh:219
const Tick MaxTick
Definition: types.hh:65
virtual std::ostream & getOstream()=0
Return an ostream that can be used to send messages to the 'same place' as formatted logMessage messa...
uint64_t Tick
Tick count type.
Definition: types.hh:63
Debug logging base class.
Definition: trace.hh:51
Bitfield< 9 > d
Definition: miscregs.hh:1375
ObjectMatch ignore
Name match for objects to ignore.
Definition: trace.hh:55
std::ostream & output()
Get the ostream from the current global logger.
Definition: trace.cc:76
Bitfield< 24 > j
Definition: miscregs.hh:1369
Logging wrapper for ostreams with the format: <when>: <name>: <message-body>
Definition: trace.hh:94
ObjectMatch ignore
Definition: trace.cc:102
Logger * debug_logger
Definition: trace.cc:63
Logger * getDebugLogger()
Get the current global debug logger.
Definition: trace.cc:66
Bitfield< 29 > c
Definition: miscregs.hh:1365
Bitfield< 18, 16 > len
Definition: miscregs.hh:1626
void logMessage(Tick when, const std::string &name, const std::string &message) override
Log formatted message.
Definition: trace.cc:144
void enable()
Enable/disable debug logging.
Definition: trace.cc:91
virtual void logMessage(Tick when, const std::string &name, const std::string &message)=0
Log formatted message.
void setDebugLogger(Logger *logger)
Delete the current global logger and assign a new one.
Definition: trace.cc:82
virtual void dump(Tick when, const std::string &name, const void *d, int len)
Dump a block of data of length len.
Definition: trace.cc:105
static void disableAll()
Definition: debug.cc:109
static void enableAll()
Definition: debug.cc:101
void disable()
Definition: trace.cc:97

Generated on Fri Jun 9 2017 13:03:41 for gem5 by doxygen 1.8.6