gem5  [DEVELOP-FOR-23.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 gem5
53 {
54 
55 namespace debug
56 {
57 
58 //
59 // This function will cause the process to signal itself with a
60 // SIGTRAP which is ignored if not in gdb, but will cause the debugger
61 // to break if in gdb.
62 //
63 void
65 {
66 #ifndef NDEBUG
67  kill(getpid(), SIGTRAP);
68 #else
69  cprintf("debug::breakpoint suppressed, compiled with NDEBUG\n");
70 #endif
71 }
72 
73 // Used to check the freshness of cached views of all flags.
74 FlagsMap &
76 {
77  // Ensure that the special "All" compound debug flag has been created,
78  // and avoid infinite recursion.
79  static bool done = false;
80  if (!done) {
81  done = true;
83  }
84  static FlagsMap flags;
85  return flags;
86 }
87 
88 bool Flag::_globalEnable = false;
89 
90 Flag *
91 findFlag(const std::string &name)
92 {
93  FlagsMap::iterator i = allFlags().find(name);
94  if (i == allFlags().end()) {
95  return NULL;
96  }
97  return i->second;
98 }
99 
100 Flag::Flag(const char *name, const char *desc)
101  : _name(name), _desc(desc)
102 {
104  allFlags().insert(std::make_pair(name, this));
105 
106  panic_if(!result.second, "Flag %s already defined!", name);
107 
108  sync();
109 }
110 
112 {
113  allFlags().erase(name());
114 }
115 
116 void
118 {
119  _globalEnable = true;
120  for (auto& i : allFlags())
121  i.second->sync();
122 }
123 
124 void
126 {
127  _globalEnable = false;
128  for (auto& i : allFlags())
129  i.second->sync();
130 }
131 
132 SimpleFlag::SimpleFlag(const char *name, const char *desc, bool is_format)
133  : Flag(name, desc), _isFormat(is_format)
134 {
135  // Add non-format flags to the special "All" compound flag.
136  if (!isFormat())
137  AllFlagsFlag::instance().add(this);
138 }
139 
140 void
142 {
143  for (auto& k : _kids)
144  k->enable();
145 }
146 
147 void
149 {
150  for (auto& k : _kids)
151  k->disable();
152 }
153 
155  "Controls all debug flags. It should not be used within C++ code.", {})
156 {}
157 
158 void
160 {
161  ++_version;
162  _kids.push_back(flag);
163 }
164 
165 int AllFlagsFlag::_version = 0;
166 
167 AllFlagsFlag &
169 {
170  static AllFlagsFlag flag;
171  return flag;
172 }
173 
174 bool
175 changeFlag(const char *s, bool value)
176 {
177  Flag *f = findFlag(s);
178  if (!f)
179  return false;
180 
181  if (value)
182  f->enable();
183  else
184  f->disable();
185 
186  return true;
187 }
188 
189 } // namespace debug
190 
191 // add a set of functions that can easily be invoked from gdb
192 void
193 setDebugFlag(const char *string)
194 {
195  debug::changeFlag(string, true);
196 }
197 
198 void
199 clearDebugFlag(const char *string)
200 {
201  debug::changeFlag(string, false);
202 }
203 
204 void
205 dumpDebugFlags(std::ostream &os)
206 {
207  using namespace debug;
208  FlagsMap::iterator i = allFlags().begin();
209  FlagsMap::iterator end = allFlags().end();
210  for (; i != end; ++i) {
211  SimpleFlag *f = dynamic_cast<SimpleFlag *>(i->second);
212  if (f && f->tracing())
213  ccprintf(os, "%s\n", f->name());
214  }
215 }
216 
217 } // namespace gem5
gem5::debug::Flag
Definition: debug.hh:61
gem5::VegaISA::s
Bitfield< 1 > s
Definition: pagetable.hh:64
gem5::debug::AllFlagsFlag::add
void add(SimpleFlag *flag)
Definition: debug.cc:159
gem5::VegaISA::f
Bitfield< 56 > f
Definition: pagetable.hh:53
gem5::debug::Flag::globalDisable
static void globalDisable()
Definition: debug.cc:125
gem5::cprintf
void cprintf(const char *format, const Args &...args)
Definition: cprintf.hh:155
gem5::debug::Flag::name
std::string name() const
Definition: debug.hh:77
gem5::debug::Flag::Flag
Flag(const char *name, const char *desc)
Definition: debug.cc:100
gem5::debug::Flag::globalEnable
static void globalEnable()
Definition: debug.cc:117
gem5::debug::breakpoint
void breakpoint()
Definition: debug.cc:64
gem5::debug::AllFlagsFlag::_version
static int _version
Definition: debug.hh:139
gem5::debug::CompoundFlag::_kids
std::vector< Flag * > _kids
Definition: debug.hh:119
gem5::debug::AllFlagsFlag::instance
static AllFlagsFlag & instance()
Definition: debug.cc:168
gem5::clearDebugFlag
void clearDebugFlag(const char *string)
Definition: debug.cc:199
gem5::debug::allFlags
FlagsMap & allFlags()
Definition: debug.cc:75
gem5::ArmISA::i
Bitfield< 7 > i
Definition: misc_types.hh:67
gem5::ccprintf
void ccprintf(cp::Print &print)
Definition: cprintf.hh:130
gem5::dumpDebugFlags
void dumpDebugFlags(std::ostream &os)
Definition: debug.cc:205
gem5::debug::FlagsMap
std::map< std::string, Flag * > FlagsMap
Definition: debug.hh:150
gem5::setDebugFlag
void setDebugFlag(const char *string)
Definition: debug.cc:193
gem5::debug::CompoundFlag
Definition: debug.hh:116
gem5::debug::Flag::sync
virtual void sync()
Definition: debug.hh:71
gem5::debug::Flag::_globalEnable
static bool _globalEnable
Definition: debug.hh:64
debug.hh
cprintf.hh
flags
uint8_t flags
Definition: helpers.cc:66
std::pair
STL pair class.
Definition: stl.hh:58
gem5::debug::SimpleFlag::SimpleFlag
SimpleFlag(const char *name, const char *desc, bool is_format=false)
Definition: debug.cc:132
gem5::debug::findFlag
Flag * findFlag(const std::string &name)
Definition: debug.cc:91
name
const std::string & name()
Definition: trace.cc:48
gem5::debug::SimpleFlag
Definition: debug.hh:91
gem5::debug::Flag::~Flag
virtual ~Flag()
Definition: debug.cc:111
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:214
gem5::X86ISA::os
Bitfield< 17 > os
Definition: misc.hh:810
gem5::debug::CompoundFlag::disable
void disable() override
Definition: debug.cc:148
gem5::debug::AllFlagsFlag
Definition: debug.hh:136
gem5::debug::changeFlag
bool changeFlag(const char *s, bool value)
Definition: debug.cc:175
logging.hh
gem5::MipsISA::k
Bitfield< 23 > k
Definition: dt_constants.hh:81
gem5::debug::CompoundFlag::enable
void enable() override
Definition: debug.cc:141
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: gpu_translation_state.hh:37
gem5::debug::SimpleFlag::isFormat
bool isFormat() const
Checks whether this flag is a conventional debug flag, or a flag that modifies the way debug informat...
Definition: debug.hh:113
gem5::debug::AllFlagsFlag::AllFlagsFlag
AllFlagsFlag()
Definition: debug.cc:154

Generated on Sun Jul 30 2023 01:56:51 for gem5 by doxygen 1.8.17