gem5  v21.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
debug.cc
Go to the documentation of this file.
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 namespace Debug {
53 
54 //
55 // This function will cause the process to signal itself with a
56 // SIGTRAP which is ignored if not in gdb, but will cause the debugger
57 // to break if in gdb.
58 //
59 void
61 {
62 #ifndef NDEBUG
63  kill(getpid(), SIGTRAP);
64 #else
65  cprintf("Debug::breakpoint suppressed, compiled with NDEBUG\n");
66 #endif
67 }
68 
69 //
70 // Flags for debugging purposes. Primarily for trace.hh
71 //
73 FlagsMap &
75 {
76  static FlagsMap flags;
77  return flags;
78 }
79 
80 bool Flag::_globalEnable = false;
81 
82 Flag *
83 findFlag(const std::string &name)
84 {
85  FlagsMap::iterator i = allFlags().find(name);
86  if (i == allFlags().end())
87  return NULL;
88  return i->second;
89 }
90 
91 Flag::Flag(const char *name, const char *desc)
92  : _name(name), _desc(desc)
93 {
95  allFlags().insert(std::make_pair(name, this));
96 
97  panic_if(!result.second, "Flag %s already defined!", name);
98 
100 
101  sync();
102 }
103 
105 {
106  allFlags().erase(name());
107 }
108 
109 void
111 {
112  _globalEnable = true;
113  for (auto& i : allFlags())
114  i.second->sync();
115 }
116 
117 void
119 {
120  _globalEnable = false;
121  for (auto& i : allFlags())
122  i.second->sync();
123 }
124 
125 void
127 {
128  for (auto& k : _kids)
129  k->enable();
130 }
131 
132 void
134 {
135  for (auto& k : _kids)
136  k->disable();
137 }
138 
139 bool
141 {
142  if (_kids.empty())
143  return false;
144 
145  for (auto& k : _kids) {
146  if (!k->enabled())
147  return false;
148  }
149 
150  return true;
151 }
152 
153 bool
154 changeFlag(const char *s, bool value)
155 {
156  Flag *f = findFlag(s);
157  if (!f)
158  return false;
159 
160  if (value)
161  f->enable();
162  else
163  f->disable();
164 
165  return true;
166 }
167 
168 } // namespace Debug
169 
170 // add a set of functions that can easily be invoked from gdb
171 void
172 setDebugFlag(const char *string)
173 {
174  Debug::changeFlag(string, true);
175 }
176 
177 void
178 clearDebugFlag(const char *string)
179 {
180  Debug::changeFlag(string, false);
181 }
182 
183 void
185 {
186  using namespace Debug;
187  FlagsMap::iterator i = allFlags().begin();
188  FlagsMap::iterator end = allFlags().end();
189  for (; i != end; ++i) {
190  SimpleFlag *f = dynamic_cast<SimpleFlag *>(i->second);
191  if (f && f->enabled())
192  cprintf("%s\n", f->name());
193  }
194 }
Debug::CompoundFlag::disable
void disable() override
Definition: debug.cc:133
Debug::Flag::_globalEnable
static bool _globalEnable
Definition: debug.hh:57
Debug::breakpoint
void breakpoint()
Definition: debug.cc:60
ArmISA::i
Bitfield< 7 > i
Definition: miscregs_types.hh:63
Debug::findFlag
Flag * findFlag(const std::string &name)
Definition: debug.cc:83
Debug::Flag::Flag
Flag(const char *name, const char *desc)
Definition: debug.cc:91
clearDebugFlag
void clearDebugFlag(const char *string)
Definition: debug.cc:178
Debug::allFlags
FlagsMap & allFlags()
Definition: debug.cc:74
Debug::SimpleFlag
Definition: debug.hh:81
Debug::CompoundFlag::enable
void enable() override
Definition: debug.cc:126
Debug::CompoundFlag::_kids
std::vector< Flag * > _kids
Definition: debug.hh:114
MipsISA::k
Bitfield< 23 > k
Definition: dt_constants.hh:78
cprintf
void cprintf(const char *format, const Args &...args)
Definition: cprintf.hh:152
dumpDebugFlags
void dumpDebugFlags()
Definition: debug.cc:184
debug.hh
Debug::allFlagsVersion
int allFlagsVersion
Definition: debug.cc:72
Debug::FlagsMap
std::map< std::string, Flag * > FlagsMap
Definition: debug.hh:132
Debug::Flag::globalEnable
static void globalEnable()
Definition: debug.cc:110
Debug::Flag
Definition: debug.hh:54
cprintf.hh
std::pair
STL pair class.
Definition: stl.hh:58
Debug::CompoundFlag::enabled
bool enabled() const override
Definition: debug.cc:140
Debug::changeFlag
bool changeFlag(const char *s, bool value)
Definition: debug.cc:154
Debug::Flag::globalDisable
static void globalDisable()
Definition: debug.cc:118
name
const std::string & name()
Definition: trace.cc:48
Debug
Definition: debug.cc:52
panic_if
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition: logging.hh:197
Debug::Flag::name
std::string name() const
Definition: debug.hh:68
logging.hh
Debug::Flag::~Flag
virtual ~Flag()
Definition: debug.cc:104
setDebugFlag
void setDebugFlag(const char *string)
Definition: debug.cc:172
ArmISA::s
Bitfield< 4 > s
Definition: miscregs_types.hh:556
Debug::Flag::sync
virtual void sync()
Definition: debug.hh:62
ArmISA::f
Bitfield< 6 > f
Definition: miscregs_types.hh:64

Generated on Tue Mar 23 2021 19:41:23 for gem5 by doxygen 1.8.17