Skip to content

Commit ccd30dd

Browse files
authored
Merge pull request #4420 from xbauch/fix/gdb-api-unit-test
Add check that gdb is on the path
2 parents b034ba3 + 01da544 commit ccd30dd

File tree

2 files changed

+83
-69
lines changed

2 files changed

+83
-69
lines changed

unit/memory-analyzer/gdb_api.cpp

Lines changed: 82 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,19 @@ Author: Malte Mues <mail.mues@gmail.com>
2323
#ifdef RUN_GDB_API_TESTS
2424

2525
#include <cstdio>
26-
#include <cstdlib>
2726
#include <regex>
2827
#include <string>
2928

3029
#include <fstream>
3130
#include <iostream>
3231

3332
#include <memory-analyzer/gdb_api.cpp>
33+
#include <util/run.h>
3434

3535
void compile_test_file()
3636
{
37-
std::string test_file("memory-analyzer/test.c");
38-
39-
std::string cmd("gcc -g -o test ");
40-
cmd += test_file;
41-
42-
const int r = system(cmd.c_str());
43-
REQUIRE(!r);
37+
REQUIRE(
38+
run("gcc", {"gcc", "-g", "-o", "test", "memory-analyzer/test.c"}) == 0);
4439
}
4540

4641
class gdb_api_testt : public gdb_apit
@@ -99,99 +94,117 @@ void gdb_api_internals_test()
9994
}
10095
}
10196

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+
102108
#endif
103109

104110
TEST_CASE("gdb api internals test", "[core][memory-analyzer]")
105111
{
106112
#ifdef RUN_GDB_API_TESTS
107-
gdb_api_internals_test();
113+
if(check_for_gdb())
114+
{
115+
gdb_api_internals_test();
116+
}
108117
#endif
109118
}
110119

111120
TEST_CASE("gdb api test", "[core][memory-analyzer]")
112121
{
113122
#ifdef RUN_GDB_API_TESTS
114-
compile_test_file();
115-
123+
if(check_for_gdb())
116124
{
117-
gdb_apit gdb_api("test", true);
118-
gdb_api.create_gdb_process();
125+
compile_test_file();
119126

120-
try
121127
{
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";
128+
gdb_apit gdb_api("test", true);
129+
gdb_api.create_gdb_process();
140130

141-
while(getline(file, line))
131+
try
142132
{
143-
std::cerr << line << '\n';
133+
const bool r = gdb_api.run_gdb_to_breakpoint("checkpoint");
134+
REQUIRE(r);
144135
}
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';
145146

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

150-
return;
151-
}
152-
}
151+
std::cerr << "=== gdb log begin ===\n";
153152

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

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

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

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

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

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

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

181+
SECTION("breakpoint does not exist")
185182
{
186-
std::string address = gdb_api.get_memory("p");
187-
REQUIRE(std::regex_match(address, regex));
183+
REQUIRE_THROWS_AS(
184+
gdb_api.run_gdb_to_breakpoint("checkpoint3"),
185+
gdb_interaction_exceptiont);
188186
}
189187

188+
SECTION("query memory")
190189
{
191-
std::string address = gdb_api.get_memory("vp");
192-
REQUIRE(std::regex_match(address, regex));
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+
}
193207
}
194208
}
195-
196209
#endif
197210
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
memory-analyzer
22
testing-utils
3+
util

0 commit comments

Comments
 (0)