misc: Replace enable_if<>::type with enable_if_t<>.
[gem5.git] / src / base / debug.cc
1 /*
2 * Copyright (c) 2020 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2003-2005 The Regents of The University of Michigan
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 */
40
41 #include "base/debug.hh"
42
43 #include <sys/types.h>
44 #include <unistd.h>
45
46 #include <algorithm>
47 #include <csignal>
48
49 #include "base/cprintf.hh"
50 #include "base/logging.hh"
51
52 using namespace std;
53
54 namespace Debug {
55
56 //
57 // This function will cause the process to signal itself with a
58 // SIGTRAP which is ignored if not in gdb, but will cause the debugger
59 // to break if in gdb.
60 //
61 void
62 breakpoint()
63 {
64 #ifndef NDEBUG
65 kill(getpid(), SIGTRAP);
66 #else
67 cprintf("Debug::breakpoint suppressed, compiled with NDEBUG\n");
68 #endif
69 }
70
71 //
72 // Flags for debugging purposes. Primarily for trace.hh
73 //
74 int allFlagsVersion = 0;
75 FlagsMap &
76 allFlags()
77 {
78 static FlagsMap flags;
79 return flags;
80 }
81
82 bool Flag::_globalEnable = false;
83
84 Flag *
85 findFlag(const std::string &name)
86 {
87 FlagsMap::iterator i = allFlags().find(name);
88 if (i == allFlags().end())
89 return NULL;
90 return i->second;
91 }
92
93 Flag::Flag(const char *name, const char *desc)
94 : _name(name), _desc(desc)
95 {
96 pair<FlagsMap::iterator, bool> result =
97 allFlags().insert(make_pair(name, this));
98
99 if (!result.second)
100 panic("Flag %s already defined!", name);
101
102 ++allFlagsVersion;
103 }
104
105 Flag::~Flag()
106 {
107 // should find and remove flag.
108 }
109
110 void
111 Flag::globalEnable()
112 {
113 _globalEnable = true;
114 for (auto& i : allFlags())
115 i.second->sync();
116 }
117
118 void
119 Flag::globalDisable()
120 {
121 _globalEnable = false;
122 for (auto& i : allFlags())
123 i.second->sync();
124 }
125
126 void
127 CompoundFlag::enable()
128 {
129 for (auto& k : _kids)
130 k->enable();
131 }
132
133 void
134 CompoundFlag::disable()
135 {
136 for (auto& k : _kids)
137 k->disable();
138 }
139
140 bool
141 CompoundFlag::status() const
142 {
143 if (_kids.empty())
144 return false;
145
146 for (auto& k : _kids) {
147 if (!k->status())
148 return false;
149 }
150
151 return true;
152 }
153
154 bool
155 changeFlag(const char *s, bool value)
156 {
157 Flag *f = findFlag(s);
158 if (!f)
159 return false;
160
161 if (value)
162 f->enable();
163 else
164 f->disable();
165
166 return true;
167 }
168
169 } // namespace Debug
170
171 // add a set of functions that can easily be invoked from gdb
172 void
173 setDebugFlag(const char *string)
174 {
175 Debug::changeFlag(string, true);
176 }
177
178 void
179 clearDebugFlag(const char *string)
180 {
181 Debug::changeFlag(string, false);
182 }
183
184 void
185 dumpDebugFlags()
186 {
187 using namespace Debug;
188 FlagsMap::iterator i = allFlags().begin();
189 FlagsMap::iterator end = allFlags().end();
190 for (; i != end; ++i) {
191 SimpleFlag *f = dynamic_cast<SimpleFlag *>(i->second);
192 if (f && f->status())
193 cprintf("%s\n", f->name());
194 }
195 }