gem5 v24.0.0.0
Loading...
Searching...
No Matches
debug.test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Daniel R. Carvalho
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <gtest/gtest.h>
30
31#include "base/debug.hh"
32#include "base/gtest/logging.hh"
33
34using namespace gem5;
35
37TEST(DebugFlagTest, NameDesc)
38{
39 debug::SimpleFlag flag_a("FlagNameDescTestKidA", "Kid A");
40 EXPECT_EQ("FlagNameDescTestKidA", flag_a.name());
41 EXPECT_EQ("Kid A", flag_a.desc());
42
43 debug::SimpleFlag flag_b("FlagNameDescTestKidB", "Kid B");
44 EXPECT_EQ("FlagNameDescTestKidB", flag_b.name());
45 EXPECT_EQ("Kid B", flag_b.desc());
46
47 debug::CompoundFlag compound_flag("FlagNameDescTest", "Compound Flag",
48 {&flag_a, &flag_b});
49 EXPECT_EQ("FlagNameDescTest", compound_flag.name());
50 EXPECT_EQ("Compound Flag", compound_flag.desc());
51}
52
54TEST(DebugFlagDeathTest, UniqueNames)
55{
56 debug::SimpleFlag flag("FlagUniqueNamesTest", "A");
57 gtestLogOutput.str("");
58 EXPECT_ANY_THROW(debug::SimpleFlag("FlagUniqueNamesTest", "B"));
59 const std::string expected = "panic: panic condition !result.second "
60 "occurred: Flag FlagUniqueNamesTest already defined!\n";
61 std::string actual = gtestLogOutput.str();
62 EXPECT_EQ(expected, actual);
63}
64
66TEST(DebugFlagTest, IsFormat)
67{
68 debug::SimpleFlag flag_a("FlagIsFormatTestA", "", true);
69 EXPECT_TRUE(flag_a.isFormat());
70 debug::SimpleFlag flag_b("FlagIsFormatTestB", "", false);
71 EXPECT_FALSE(flag_b.isFormat());
72 debug::SimpleFlag flag_c("FlagIsFormatTestC", "");
73 EXPECT_FALSE(flag_c.isFormat());
74}
75
77TEST(DebugSimpleFlagTest, Enabled)
78{
80 debug::SimpleFlag flag("SimpleFlagEnabledTest", "");
81
82 // By default flags are initialized disabled
83 ASSERT_FALSE(flag.tracing());
84
85 // Flags must be globally enabled before individual flags are enabled
86 flag.enable();
87 ASSERT_FALSE(flag.tracing());
89 ASSERT_TRUE(!TRACING_ON || flag.tracing());
90
91 // Verify that the global enabler works
93 ASSERT_FALSE(flag.tracing());
95 ASSERT_TRUE(!TRACING_ON || flag.tracing());
96
97 // Test disabling the flag with global enabled
98 flag.disable();
99 ASSERT_FALSE(flag.tracing());
100}
101
106TEST(DebugCompoundFlagTest, Enabled)
107{
109 debug::SimpleFlag flag_a("CompoundFlagEnabledTestKidA", "");
110 debug::SimpleFlag flag_b("CompoundFlagEnabledTestKidB", "");
111 debug::CompoundFlag flag("CompoundFlagEnabledTest", "",
112 {&flag_a, &flag_b});
113
114 // By default flags are initialized disabled
115 ASSERT_FALSE(flag.tracing());
116
117 // Flags must be globally enabled before individual flags are enabled
118 flag.enable();
119 ASSERT_FALSE(flag_a.tracing());
120 ASSERT_FALSE(flag_b.tracing());
121 ASSERT_FALSE(flag.tracing());
123 for (auto &kid : flag.kids()) {
124 ASSERT_TRUE(!TRACING_ON || kid->tracing());
125 }
126 ASSERT_TRUE(!TRACING_ON || flag_a.tracing());
127 ASSERT_TRUE(!TRACING_ON || flag_b.tracing());
128
129 // Test disabling the flag with global enabled
130 flag.disable();
131 for (auto &kid : flag.kids()) {
132 ASSERT_FALSE(kid->tracing());
133 }
134 ASSERT_FALSE(flag_a.tracing());
135 ASSERT_FALSE(flag_b.tracing());
136 ASSERT_FALSE(flag.tracing());
137}
138
140TEST(DebugFlagTest, ConversionOperator)
141{
143 debug::SimpleFlag flag("FlagConversionOperatorTest", "");
144
145 ASSERT_EQ(flag, flag.tracing());
146 flag.enable();
147 ASSERT_EQ(flag, flag.tracing());
148 flag.disable();
149}
150
155TEST(DebugCompoundFlagTest, EnabledKids)
156{
158 debug::SimpleFlag flag_a("CompoundFlagEnabledKidsTestKidA", "");
159 debug::SimpleFlag flag_b("CompoundFlagEnabledKidsTestKidB", "");
160 debug::CompoundFlag flag("CompoundFlagEnabledKidsTest", "",
161 {&flag_a, &flag_b});
162
163 // Test enabling only flag A
164 ASSERT_FALSE(flag_a.tracing());
165 ASSERT_FALSE(flag_b.tracing());
166 ASSERT_FALSE(flag.tracing());
167 flag_a.enable();
168 ASSERT_TRUE(!TRACING_ON || flag_a.tracing());
169 ASSERT_FALSE(flag_b.tracing());
170 ASSERT_FALSE(flag.tracing());
171
172 // Test that enabling both flags enables the compound flag
173 ASSERT_TRUE(!TRACING_ON || flag_a.tracing());
174 ASSERT_FALSE(flag_b.tracing());
175 ASSERT_FALSE(flag.tracing());
176 flag_b.enable();
177 ASSERT_TRUE(!TRACING_ON || flag_a.tracing());
178 ASSERT_TRUE(!TRACING_ON || flag_b.tracing());
179
180 // Test that disabling one of the flags disables the compound flag
181 flag_a.disable();
182 ASSERT_FALSE(flag_a.tracing());
183 ASSERT_TRUE(!TRACING_ON || flag_b.tracing());
184 ASSERT_FALSE(flag.tracing());
185}
186
188TEST(DebugFlagTest, FindFlag)
189{
191 debug::SimpleFlag flag_a("FlagFindFlagTestA", "");
192 debug::SimpleFlag flag_b("FlagFindFlagTestB", "");
193
194 // Enable the found flags and verify that the original flags are
195 // enabled too
196 debug::Flag *flag;
197 EXPECT_TRUE(flag = debug::findFlag("FlagFindFlagTestA"));
198 ASSERT_FALSE(flag_a.tracing());
199 flag->enable();
200 ASSERT_TRUE(!TRACING_ON || flag_a.tracing());
201 EXPECT_TRUE(flag = debug::findFlag("FlagFindFlagTestB"));
202 ASSERT_FALSE(flag_b.tracing());
203 flag->enable();
204 ASSERT_TRUE(!TRACING_ON || flag_b.tracing());
205
206 // Search for a non-existent flag
207 EXPECT_FALSE(debug::findFlag("FlagFindFlagTestC"));
208}
209
211TEST(DebugFlagTest, ChangeFlag)
212{
214 debug::SimpleFlag flag_a("FlagChangeFlagTestA", "");
215 debug::SimpleFlag flag_b("FlagChangeFlagTestB", "");
216
217 // Enable the found flags and verify that the original flags are
218 // enabled too
219 ASSERT_FALSE(flag_a.tracing());
220 EXPECT_TRUE(debug::changeFlag("FlagChangeFlagTestA", true));
221 ASSERT_TRUE(!TRACING_ON || flag_a.tracing());
222 EXPECT_TRUE(debug::changeFlag("FlagChangeFlagTestA", false));
223 ASSERT_FALSE(flag_a.tracing());
224
225 // Disable and enable a flag
226 ASSERT_FALSE(flag_b.tracing());
227 EXPECT_TRUE(debug::changeFlag("FlagChangeFlagTestB", false));
228 ASSERT_FALSE(flag_b.tracing());
229 EXPECT_TRUE(debug::changeFlag("FlagChangeFlagTestB", true));
230 ASSERT_TRUE(!TRACING_ON || flag_b.tracing());
231
232 // Change a non-existent flag
233 ASSERT_FALSE(debug::changeFlag("FlagChangeFlagTestC", true));
234}
235
237TEST(DebugFlagTest, SetClearDebugFlag)
238{
240 debug::SimpleFlag flag_a("FlagSetClearDebugFlagTestA", "");
241 debug::SimpleFlag flag_b("FlagSetClearDebugFlagTestB", "");
242
243 // Enable and disable a flag
244 ASSERT_FALSE(flag_a.tracing());
245 setDebugFlag("FlagSetClearDebugFlagTestA");
246 ASSERT_TRUE(!TRACING_ON || flag_a.tracing());
247 clearDebugFlag("FlagSetClearDebugFlagTestA");
248 ASSERT_FALSE(flag_a.tracing());
249
250 // Disable and enable a flag
251 ASSERT_FALSE(flag_b.tracing());
252 clearDebugFlag("FlagSetClearDebugFlagTestB");
253 ASSERT_FALSE(flag_b.tracing());
254 setDebugFlag("FlagSetClearDebugFlagTestB");
255 ASSERT_TRUE(!TRACING_ON || flag_b.tracing());
256
257 // Change a non-existent flag
258 setDebugFlag("FlagSetClearDebugFlagTestC");
259 clearDebugFlag("FlagSetClearDebugFlagTestC");
260}
261
263TEST(DebugFlagTest, NoDumpDebugFlags)
264{
266 debug::SimpleFlag flag("FlagDumpDebugFlagTest", "");
267
268 // Verify that the names of the enabled flags are printed
269 gtestLogOutput.str("");
271 std::string output = gtestLogOutput.str();
272 EXPECT_EQ(output, "");
273 ASSERT_FALSE(flag.tracing());
274}
275
277TEST(DebugFlagTest, DumpDebugFlags)
278{
280 debug::SimpleFlag flag_a("FlagDumpDebugFlagTestA", "");
281 debug::SimpleFlag flag_b("FlagDumpDebugFlagTestB", "");
282 debug::SimpleFlag flag_c("FlagDumpDebugFlagTestC", "");
283 debug::SimpleFlag flag_d("FlagDumpDebugFlagTestD", "");
284 debug::SimpleFlag flag_e("FlagDumpDebugFlagTestE", "");
285 debug::CompoundFlag compound_flag_a("CompoundFlagDumpDebugFlagTestA", "",
286 {&flag_d});
287 debug::CompoundFlag compound_flag_b("CompoundFlagDumpDebugFlagTestB", "",
288 {&flag_e});
289
290 // Enable a few flags
291 ASSERT_FALSE(flag_a.tracing());
292 ASSERT_FALSE(flag_b.tracing());
293 ASSERT_FALSE(flag_c.tracing());
294 ASSERT_FALSE(flag_d.tracing());
295 ASSERT_FALSE(flag_e.tracing());
296 flag_a.enable();
297 flag_c.enable();
298 compound_flag_b.enable();
299
300 // Verify that the names of the enabled flags are printed if TRACING_ON.
301 if (TRACING_ON) {
302 std::ostringstream os;
304 std::string output = os.str();
305 EXPECT_EQ(output, "FlagDumpDebugFlagTestA\nFlagDumpDebugFlagTestC\n" \
306 "FlagDumpDebugFlagTestE\n");
307 }
308}
std::string name() const
Definition debug.hh:77
static void globalEnable()
Definition debug.cc:117
virtual void enable()=0
static void globalDisable()
Definition debug.cc:125
std::string desc() const
Definition debug.hh:78
bool tracing() const
Definition debug.hh:80
void enable() override
Definition debug.hh:104
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
void disable() override
Definition debug.hh:105
TEST(DebugFlagTest, NameDesc)
Test assignment of names and descriptions.
Definition debug.test.cc:37
std::vector< SwitchingFiber * > expected({ &a, &b, &a, &a, &a, &b, &c, &a, &c, &c, &c })
Bitfield< 17 > os
Definition misc.hh:838
bool changeFlag(const char *s, bool value)
Definition debug.cc:175
Flag * findFlag(const std::string &name)
Definition debug.cc:91
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
void setDebugFlag(const char *string)
Definition debug.cc:193
void clearDebugFlag(const char *string)
Definition debug.cc:199
static void output(const char *filename)
Definition debug.cc:60
void dumpDebugFlags(std::ostream &os)
Definition debug.cc:205
thread_local GTestLogOutput gtestLogOutput
Definition logging.cc:33

Generated on Tue Jun 18 2024 16:24:00 for gem5 by doxygen 1.11.0