gem5  v20.1.0.0
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 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
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 //
75 FlagsMap &
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 {
97  allFlags().insert(make_pair(name, this));
98 
99  if (!result.second)
100  panic("Flag %s already defined!", name);
101 
102  ++allFlagsVersion;
103 }
104 
106 {
107  // should find and remove flag.
108 }
109 
110 void
112 {
113  _globalEnable = true;
114  for (auto& i : allFlags())
115  i.second->sync();
116 }
117 
118 void
120 {
121  _globalEnable = false;
122  for (auto& i : allFlags())
123  i.second->sync();
124 }
125 
126 void
128 {
129  for (auto& k : _kids)
130  k->enable();
131 }
132 
133 void
135 {
136  for (auto& k : _kids)
137  k->disable();
138 }
139 
140 bool
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
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 }
Debug::CompoundFlag::disable
void disable() override
Definition: debug.cc:134
Debug::Flag::_globalEnable
static bool _globalEnable
Definition: debug.hh:57
Debug::breakpoint
void breakpoint()
Definition: debug.cc:62
ArmISA::i
Bitfield< 7 > i
Definition: miscregs_types.hh:63
Debug::findFlag
Flag * findFlag(const std::string &name)
Definition: debug.cc:85
clearDebugFlag
void clearDebugFlag(const char *string)
Definition: debug.cc:179
Debug::allFlags
FlagsMap & allFlags()
Definition: debug.cc:76
Debug::SimpleFlag
Definition: debug.hh:82
Debug::CompoundFlag::enable
void enable() override
Definition: debug.cc:127
Debug::CompoundFlag::_kids
std::vector< Flag * > _kids
Definition: debug.hh:104
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:185
debug.hh
Debug::allFlagsVersion
int allFlagsVersion
Definition: debug.cc:74
Debug::FlagsMap
std::map< std::string, Flag * > FlagsMap
Definition: debug.hh:122
Debug::Flag::globalEnable
static void globalEnable()
Definition: debug.cc:111
Debug::Flag
Definition: debug.hh:54
cprintf.hh
std::pair
STL pair class.
Definition: stl.hh:58
Debug::changeFlag
bool changeFlag(const char *s, bool value)
Definition: debug.cc:155
Debug::Flag::globalDisable
static void globalDisable()
Definition: debug.cc:119
name
const std::string & name()
Definition: trace.cc:50
Debug::CompoundFlag::status
bool status() const override
Definition: debug.cc:141
Debug
Definition: debug.cc:54
Debug::Flag::name
std::string name() const
Definition: debug.hh:68
std
Overload hash function for BasicBlockRange type.
Definition: vec_reg.hh:587
logging.hh
Debug::Flag::~Flag
virtual ~Flag()
Definition: debug.cc:105
setDebugFlag
void setDebugFlag(const char *string)
Definition: debug.cc:173
ArmISA::s
Bitfield< 4 > s
Definition: miscregs_types.hh:556
panic
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:171
ArmISA::f
Bitfield< 6 > f
Definition: miscregs_types.hh:64

Generated on Wed Sep 30 2020 14:02:07 for gem5 by doxygen 1.8.17