trace.cc (10475:5744891a444b) trace.cc (11153:20bbfe5b2b86)
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 <cctype>
37#include <fstream>
38#include <iostream>
39#include <sstream>
40#include <string>
41
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 <cctype>
37#include <fstream>
38#include <iostream>
39#include <sstream>
40#include <string>
41
42#include "base/debug.hh"
42#include "base/misc.hh"
43#include "base/output.hh"
44#include "base/str.hh"
45#include "base/trace.hh"
46
47const std::string &name()
48{
49 static const std::string default_name("global");
50
51 return default_name;
52}
53
54namespace Trace
55{
56
43#include "base/misc.hh"
44#include "base/output.hh"
45#include "base/str.hh"
46#include "base/trace.hh"
47
48const std::string &name()
49{
50 static const std::string default_name("global");
51
52 return default_name;
53}
54
55namespace Trace
56{
57
57bool enabled = false;
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
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 &
76output()
77{
78 return getDebugLogger()->getOstream();
79}
80
81void
82setDebugLogger(Logger *logger)
83{
84 if (!logger)
85 warn("Trying to set debug logger to NULL\n");
86 else
87 debug_logger = logger;
88}
89
58// This variable holds the output logger for debug information. Other
59// than setting up/redirecting this logger, do *NOT* reference this
60// directly
61
62Logger *debug_logger = NULL;
63
64Logger *
65getDebugLogger()
66{
67 /* Set a default logger to cerr when no other logger is set */
68 if (!debug_logger)
69 debug_logger = new OstreamLogger(std::cerr);
70
71 return debug_logger;
72}
73
74std::ostream &
75output()
76{
77 return getDebugLogger()->getOstream();
78}
79
80void
81setDebugLogger(Logger *logger)
82{
83 if (!logger)
84 warn("Trying to set debug logger to NULL\n");
85 else
86 debug_logger = logger;
87}
88
89void
90enable()
91{
92 Debug::SimpleFlag::enableAll();
93}
94
95void
96disable()
97{
98 Debug::SimpleFlag::disableAll();
99}
100
90ObjectMatch ignore;
91
92void
93Logger::dump(Tick when, const std::string &name, const void *d, int len)
94{
95 if (!name.empty() && ignore.match(name))
96 return;
97
98 const char *data = static_cast<const char *>(d);
99 int c, i, j;
100
101 for (i = 0; i < len; i += 16) {
102 std::ostringstream line;
103
104 ccprintf(line, "%08x ", i);
105 c = len - i;
106 if (c > 16) c = 16;
107
108 for (j = 0; j < c; j++) {
109 ccprintf(line, "%02x ", data[i + j] & 0xff);
110 if ((j & 0xf) == 7 && j > 0)
111 ccprintf(line, " ");
112 }
113
114 for (; j < 16; j++)
115 ccprintf(line, " ");
116 ccprintf(line, " ");
117
118 for (j = 0; j < c; j++) {
119 int ch = data[i + j] & 0x7f;
120 ccprintf(line, "%c", (char)(isprint(ch) ? ch : ' '));
121 }
122
123 ccprintf(line, "\n");
124 logMessage(when, name, line.str());
125
126 if (c < 16)
127 break;
128 }
129}
130
131void
132OstreamLogger::logMessage(Tick when, const std::string &name,
133 const std::string &message)
134{
135 if (!name.empty() && ignore.match(name))
136 return;
137
138 if (when != MaxTick)
139 ccprintf(stream, "%7d: ", when);
140
141 if (!name.empty())
142 stream << name << ": ";
143
144 stream << message;
145 stream.flush();
146}
147
148} // namespace Trace
101ObjectMatch ignore;
102
103void
104Logger::dump(Tick when, const std::string &name, const void *d, int len)
105{
106 if (!name.empty() && ignore.match(name))
107 return;
108
109 const char *data = static_cast<const char *>(d);
110 int c, i, j;
111
112 for (i = 0; i < len; i += 16) {
113 std::ostringstream line;
114
115 ccprintf(line, "%08x ", i);
116 c = len - i;
117 if (c > 16) c = 16;
118
119 for (j = 0; j < c; j++) {
120 ccprintf(line, "%02x ", data[i + j] & 0xff);
121 if ((j & 0xf) == 7 && j > 0)
122 ccprintf(line, " ");
123 }
124
125 for (; j < 16; j++)
126 ccprintf(line, " ");
127 ccprintf(line, " ");
128
129 for (j = 0; j < c; j++) {
130 int ch = data[i + j] & 0x7f;
131 ccprintf(line, "%c", (char)(isprint(ch) ? ch : ' '));
132 }
133
134 ccprintf(line, "\n");
135 logMessage(when, name, line.str());
136
137 if (c < 16)
138 break;
139 }
140}
141
142void
143OstreamLogger::logMessage(Tick when, const std::string &name,
144 const std::string &message)
145{
146 if (!name.empty() && ignore.match(name))
147 return;
148
149 if (when != MaxTick)
150 ccprintf(stream, "%7d: ", when);
151
152 if (!name.empty())
153 stream << name << ": ";
154
155 stream << message;
156 stream.flush();
157}
158
159} // namespace Trace