Skip to content

Commit 5e7deb1

Browse files
authored
Merge pull request #4494 from danpoe/refactor/gdb-api-unit-tests
Redirect gdb api unit test output to /dev/null and remove redundant check
2 parents 14f618c + a9565d1 commit 5e7deb1

File tree

1 file changed

+67
-79
lines changed

1 file changed

+67
-79
lines changed

unit/memory-analyzer/gdb_api.cpp

Lines changed: 67 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ void compile_test_file()
3838
run("gcc", {"gcc", "-g", "-o", "test", "memory-analyzer/test.c"}) == 0);
3939
}
4040

41+
void check_for_gdb()
42+
{
43+
REQUIRE(run("gdb", {"gdb", "--version"}, "", "/dev/null", "/dev/null") == 0);
44+
}
45+
4146
class gdb_api_testt : public gdb_apit
4247
{
4348
explicit gdb_api_testt(const char *binary) : gdb_apit(binary)
@@ -49,6 +54,7 @@ class gdb_api_testt : public gdb_apit
4954

5055
void gdb_api_internals_test()
5156
{
57+
check_for_gdb();
5258
compile_test_file();
5359

5460
SECTION("parse gdb output record")
@@ -94,116 +100,98 @@ void gdb_api_internals_test()
94100
}
95101
}
96102

97-
bool check_for_gdb()
98-
{
99-
const bool has_gdb = run("gdb", {"gdb", "--version"}) == 0;
100-
101-
SECTION("check gdb is on the PATH")
102-
{
103-
REQUIRE(has_gdb);
104-
}
105-
return has_gdb;
106-
}
107-
108103
#endif
109104

110105
TEST_CASE("gdb api internals test", "[core][memory-analyzer]")
111106
{
112107
#ifdef RUN_GDB_API_TESTS
113-
if(check_for_gdb())
114-
{
115-
gdb_api_internals_test();
116-
}
108+
gdb_api_internals_test();
117109
#endif
118110
}
119111

120112
TEST_CASE("gdb api test", "[core][memory-analyzer]")
121113
{
122114
#ifdef RUN_GDB_API_TESTS
123-
if(check_for_gdb())
115+
check_for_gdb();
116+
compile_test_file();
117+
124118
{
125-
compile_test_file();
119+
gdb_apit gdb_api("test", true);
120+
gdb_api.create_gdb_process();
126121

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

131-
try
141+
std::cerr << "=== gdb log begin ===\n";
142+
143+
while(getline(file, line))
132144
{
133-
const bool r = gdb_api.run_gdb_to_breakpoint("checkpoint");
134-
REQUIRE(r);
145+
std::cerr << line << '\n';
135146
}
136-
catch(const gdb_interaction_exceptiont &e)
137-
{
138-
std::cerr
139-
<< "warning: cannot fully unit test GDB API as program cannot "
140-
<< "be run with gdb\n";
141-
std::cerr << "warning: this may be due to not having the required "
142-
<< "permissions (e.g., to invoke ptrace() or to disable ASLR)"
143-
<< "\n";
144-
std::cerr << "gdb_interaction_exceptiont:" << '\n';
145-
std::cerr << e.what() << '\n';
146147

147-
std::ifstream file("gdb.txt");
148-
CHECK_RETURN(file.is_open());
149-
std::string line;
148+
file.close();
149+
150+
std::cerr << "=== gdb log end ===\n";
150151

151-
std::cerr << "=== gdb log begin ===\n";
152+
return;
153+
}
154+
}
152155

153-
while(getline(file, line))
154-
{
155-
std::cerr << line << '\n';
156-
}
156+
gdb_apit gdb_api("test");
157+
gdb_api.create_gdb_process();
157158

158-
file.close();
159+
SECTION("breakpoint is hit")
160+
{
161+
const bool r = gdb_api.run_gdb_to_breakpoint("checkpoint");
162+
REQUIRE(r);
163+
}
159164

160-
std::cerr << "=== gdb log end ===\n";
165+
SECTION("breakpoint is not hit")
166+
{
167+
const bool r = gdb_api.run_gdb_to_breakpoint("checkpoint2");
168+
REQUIRE(!r);
169+
}
161170

162-
return;
163-
}
164-
}
171+
SECTION("breakpoint does not exist")
172+
{
173+
REQUIRE_THROWS_AS(
174+
gdb_api.run_gdb_to_breakpoint("checkpoint3"), gdb_interaction_exceptiont);
175+
}
165176

166-
gdb_apit gdb_api("test");
167-
gdb_api.create_gdb_process();
177+
SECTION("query memory")
178+
{
179+
const bool r = gdb_api.run_gdb_to_breakpoint("checkpoint");
180+
REQUIRE(r);
168181

169-
SECTION("breakpoint is hit")
170-
{
171-
const bool r = gdb_api.run_gdb_to_breakpoint("checkpoint");
172-
REQUIRE(r);
173-
}
182+
REQUIRE(gdb_api.get_value("x") == "8");
183+
REQUIRE(gdb_api.get_value("s") == "abc");
174184

175-
SECTION("breakpoint is not hit")
176-
{
177-
const bool r = gdb_api.run_gdb_to_breakpoint("checkpoint2");
178-
REQUIRE(!r);
179-
}
185+
const std::regex regex(R"(0x[1-9a-f][0-9a-f]*)");
180186

181-
SECTION("breakpoint does not exist")
182187
{
183-
REQUIRE_THROWS_AS(
184-
gdb_api.run_gdb_to_breakpoint("checkpoint3"),
185-
gdb_interaction_exceptiont);
188+
std::string address = gdb_api.get_memory("p");
189+
REQUIRE(std::regex_match(address, regex));
186190
}
187191

188-
SECTION("query memory")
189192
{
190-
const bool r = gdb_api.run_gdb_to_breakpoint("checkpoint");
191-
REQUIRE(r);
192-
193-
REQUIRE(gdb_api.get_value("x") == "8");
194-
REQUIRE(gdb_api.get_value("s") == "abc");
195-
196-
const std::regex regex(R"(0x[1-9a-f][0-9a-f]*)");
197-
198-
{
199-
std::string address = gdb_api.get_memory("p");
200-
REQUIRE(std::regex_match(address, regex));
201-
}
202-
203-
{
204-
std::string address = gdb_api.get_memory("vp");
205-
REQUIRE(std::regex_match(address, regex));
206-
}
193+
std::string address = gdb_api.get_memory("vp");
194+
REQUIRE(std::regex_match(address, regex));
207195
}
208196
}
209197
#endif

0 commit comments

Comments
 (0)