Skip to content

Commit 65a30ea

Browse files
Petr BauchPetr Bauch
authored andcommitted
Add check that gdb is on the path
No functional change only wrapped the test-cases in if-statements checking that gdb can be called. The rest is clang-formatting.
1 parent 870bfd7 commit 65a30ea

File tree

1 file changed

+78
-61
lines changed

1 file changed

+78
-61
lines changed

unit/memory-analyzer/gdb_api.cpp

Lines changed: 78 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -104,94 +104,111 @@ void gdb_api_internals_test()
104104
TEST_CASE("gdb api internals test", "[core][memory-analyzer]")
105105
{
106106
#ifdef RUN_GDB_API_TESTS
107-
gdb_api_internals_test();
107+
int has_gdb = system("gdb --version");
108+
SECTION("check gdb is on the PATH")
109+
{
110+
REQUIRE(has_gdb == 0);
111+
}
112+
if(has_gdb == 0)
113+
{
114+
gdb_api_internals_test();
115+
}
108116
#endif
109117
}
110118

111119
TEST_CASE("gdb api test", "[core][memory-analyzer]")
112120
{
113121
#ifdef RUN_GDB_API_TESTS
114-
compile_test_file();
115-
122+
int has_gdb = system("gdb --version");
123+
SECTION("check gdb is on the PATH")
116124
{
117-
gdb_apit gdb_api("test", true);
118-
gdb_api.create_gdb_process();
125+
REQUIRE(has_gdb == 0);
126+
}
127+
if(has_gdb == 0)
128+
{
129+
compile_test_file();
119130

120-
try
121131
{
122-
const bool r = gdb_api.run_gdb_to_breakpoint("checkpoint");
123-
REQUIRE(r);
124-
}
125-
catch(const gdb_interaction_exceptiont &e)
126-
{
127-
std::cerr << "warning: cannot fully unit test GDB API as program cannot "
128-
<< "be run with gdb\n";
129-
std::cerr << "warning: this may be due to not having the required "
130-
<< "permissions (e.g., to invoke ptrace() or to disable ASLR)"
131-
<< "\n";
132-
std::cerr << "gdb_interaction_exceptiont:" << '\n';
133-
std::cerr << e.what() << '\n';
134-
135-
std::ifstream file("gdb.txt");
136-
CHECK_RETURN(file.is_open());
137-
std::string line;
138-
139-
std::cerr << "=== gdb log begin ===\n";
132+
gdb_apit gdb_api("test", true);
133+
gdb_api.create_gdb_process();
140134

141-
while(getline(file, line))
135+
try
142136
{
143-
std::cerr << line << '\n';
137+
const bool r = gdb_api.run_gdb_to_breakpoint("checkpoint");
138+
REQUIRE(r);
144139
}
140+
catch(const gdb_interaction_exceptiont &e)
141+
{
142+
std::cerr
143+
<< "warning: cannot fully unit test GDB API as program cannot "
144+
<< "be run with gdb\n";
145+
std::cerr << "warning: this may be due to not having the required "
146+
<< "permissions (e.g., to invoke ptrace() or to disable ASLR)"
147+
<< "\n";
148+
std::cerr << "gdb_interaction_exceptiont:" << '\n';
149+
std::cerr << e.what() << '\n';
145150

146-
file.close();
151+
std::ifstream file("gdb.txt");
152+
CHECK_RETURN(file.is_open());
153+
std::string line;
147154

148-
std::cerr << "=== gdb log end ===\n";
155+
std::cerr << "=== gdb log begin ===\n";
149156

150-
return;
151-
}
152-
}
157+
while(getline(file, line))
158+
{
159+
std::cerr << line << '\n';
160+
}
153161

154-
gdb_apit gdb_api("test");
155-
gdb_api.create_gdb_process();
162+
file.close();
156163

157-
SECTION("breakpoint is hit")
158-
{
159-
const bool r = gdb_api.run_gdb_to_breakpoint("checkpoint");
160-
REQUIRE(r);
161-
}
162-
163-
SECTION("breakpoint is not hit")
164-
{
165-
const bool r = gdb_api.run_gdb_to_breakpoint("checkpoint2");
166-
REQUIRE(!r);
167-
}
164+
std::cerr << "=== gdb log end ===\n";
168165

169-
SECTION("breakpoint does not exist")
170-
{
171-
REQUIRE_THROWS_AS(
172-
gdb_api.run_gdb_to_breakpoint("checkpoint3"), gdb_interaction_exceptiont);
173-
}
166+
return;
167+
}
168+
}
174169

175-
SECTION("query memory")
176-
{
177-
const bool r = gdb_api.run_gdb_to_breakpoint("checkpoint");
178-
REQUIRE(r);
170+
gdb_apit gdb_api("test");
171+
gdb_api.create_gdb_process();
179172

180-
REQUIRE(gdb_api.get_value("x") == "8");
181-
REQUIRE(gdb_api.get_value("s") == "abc");
173+
SECTION("breakpoint is hit")
174+
{
175+
const bool r = gdb_api.run_gdb_to_breakpoint("checkpoint");
176+
REQUIRE(r);
177+
}
182178

183-
const std::regex regex(R"(0x[1-9a-f][0-9a-f]*)");
179+
SECTION("breakpoint is not hit")
180+
{
181+
const bool r = gdb_api.run_gdb_to_breakpoint("checkpoint2");
182+
REQUIRE(!r);
183+
}
184184

185+
SECTION("breakpoint does not exist")
185186
{
186-
std::string address = gdb_api.get_memory("p");
187-
REQUIRE(std::regex_match(address, regex));
187+
REQUIRE_THROWS_AS(
188+
gdb_api.run_gdb_to_breakpoint("checkpoint3"),
189+
gdb_interaction_exceptiont);
188190
}
189191

192+
SECTION("query memory")
190193
{
191-
std::string address = gdb_api.get_memory("vp");
192-
REQUIRE(std::regex_match(address, regex));
194+
const bool r = gdb_api.run_gdb_to_breakpoint("checkpoint");
195+
REQUIRE(r);
196+
197+
REQUIRE(gdb_api.get_value("x") == "8");
198+
REQUIRE(gdb_api.get_value("s") == "abc");
199+
200+
const std::regex regex(R"(0x[1-9a-f][0-9a-f]*)");
201+
202+
{
203+
std::string address = gdb_api.get_memory("p");
204+
REQUIRE(std::regex_match(address, regex));
205+
}
206+
207+
{
208+
std::string address = gdb_api.get_memory("vp");
209+
REQUIRE(std::regex_match(address, regex));
210+
}
193211
}
194212
}
195-
196213
#endif
197214
}

0 commit comments

Comments
 (0)