misc: Merged release-staging-v19.0.0.0 into develop
[gem5.git] / src / base / debug.hh
1 /*
2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
3 * Copyright (c) 2010 The Hewlett-Packard Development Company
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #ifndef __BASE_DEBUG_HH__
31 #define __BASE_DEBUG_HH__
32
33 #include <map>
34 #include <string>
35 #include <vector>
36
37 namespace Debug {
38
39 void breakpoint();
40
41 class Flag
42 {
43 protected:
44 const char *_name;
45 const char *_desc;
46
47 public:
48 Flag(const char *name, const char *desc);
49 virtual ~Flag();
50
51 std::string name() const { return _name; }
52 std::string desc() const { return _desc; }
53 virtual std::vector<Flag *> kids() { return std::vector<Flag*>(); }
54
55 virtual void enable() = 0;
56 virtual void disable() = 0;
57 virtual void sync() {}
58 };
59
60 class SimpleFlag : public Flag
61 {
62 static bool _active; // whether debug tracings are enabled
63 protected:
64 bool _tracing; // tracing is enabled and flag is on
65 bool _status; // flag status
66
67 public:
68 SimpleFlag(const char *name, const char *desc)
69 : Flag(name, desc), _status(false)
70 { }
71
72 bool status() const { return _tracing; }
73 operator bool() const { return _tracing; }
74 bool operator!() const { return !_tracing; }
75
76 void enable() { _status = true; sync(); }
77 void disable() { _status = false; sync(); }
78
79 void sync() { _tracing = _active && _status; }
80
81 static void enableAll();
82 static void disableAll();
83 };
84
85 class CompoundFlag : public Flag
86 {
87 protected:
88 std::vector<Flag *> _kids;
89
90 void
91 addFlag(Flag *f)
92 {
93 if (f != nullptr)
94 _kids.push_back(f);
95 }
96
97 public:
98 CompoundFlag(const char *name, const char *desc,
99 Flag *f00 = nullptr, Flag *f01 = nullptr,
100 Flag *f02 = nullptr, Flag *f03 = nullptr,
101 Flag *f04 = nullptr, Flag *f05 = nullptr,
102 Flag *f06 = nullptr, Flag *f07 = nullptr,
103 Flag *f08 = nullptr, Flag *f09 = nullptr,
104 Flag *f10 = nullptr, Flag *f11 = nullptr,
105 Flag *f12 = nullptr, Flag *f13 = nullptr,
106 Flag *f14 = nullptr, Flag *f15 = nullptr,
107 Flag *f16 = nullptr, Flag *f17 = nullptr,
108 Flag *f18 = nullptr, Flag *f19 = nullptr)
109 : Flag(name, desc)
110 {
111 addFlag(f00); addFlag(f01); addFlag(f02); addFlag(f03); addFlag(f04);
112 addFlag(f05); addFlag(f06); addFlag(f07); addFlag(f08); addFlag(f09);
113 addFlag(f10); addFlag(f11); addFlag(f12); addFlag(f13); addFlag(f14);
114 addFlag(f15); addFlag(f16); addFlag(f17); addFlag(f18); addFlag(f19);
115 }
116
117 std::vector<Flag *> kids() { return _kids; }
118
119 void enable();
120 void disable();
121 };
122
123 typedef std::map<std::string, Flag *> FlagsMap;
124 FlagsMap &allFlags();
125
126 Flag *findFlag(const std::string &name);
127
128 extern Flag *const All;
129
130 bool changeFlag(const char *s, bool value);
131
132 } // namespace Debug
133
134 void setDebugFlag(const char *string);
135
136 void clearDebugFlag(const char *string);
137
138 void dumpDebugFlags();
139
140 #endif // __BASE_DEBUG_HH__