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

Generated on Wed Dec 21 2022 10:22:28 for gem5 by doxygen 1.9.1