@@ -23,24 +23,19 @@ Author: Malte Mues <mail.mues@gmail.com>
23
23
#ifdef RUN_GDB_API_TESTS
24
24
25
25
#include < cstdio>
26
- #include < cstdlib>
27
26
#include < regex>
28
27
#include < string>
29
28
30
29
#include < fstream>
31
30
#include < iostream>
32
31
33
32
#include < memory-analyzer/gdb_api.cpp>
33
+ #include < util/run.h>
34
34
35
35
void compile_test_file ()
36
36
{
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 );
44
39
}
45
40
46
41
class gdb_api_testt : public gdb_apit
@@ -99,99 +94,117 @@ void gdb_api_internals_test()
99
94
}
100
95
}
101
96
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
+
102
108
#endif
103
109
104
110
TEST_CASE (" gdb api internals test" , " [core][memory-analyzer]" )
105
111
{
106
112
#ifdef RUN_GDB_API_TESTS
107
- gdb_api_internals_test ();
113
+ if (check_for_gdb ())
114
+ {
115
+ gdb_api_internals_test ();
116
+ }
108
117
#endif
109
118
}
110
119
111
120
TEST_CASE (" gdb api test" , " [core][memory-analyzer]" )
112
121
{
113
122
#ifdef RUN_GDB_API_TESTS
114
- compile_test_file ();
115
-
123
+ if (check_for_gdb ())
116
124
{
117
- gdb_apit gdb_api (" test" , true );
118
- gdb_api.create_gdb_process ();
125
+ compile_test_file ();
119
126
120
- try
121
127
{
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 ();
140
130
141
- while ( getline (file, line))
131
+ try
142
132
{
143
- std::cerr << line << ' \n ' ;
133
+ const bool r = gdb_api.run_gdb_to_breakpoint (" checkpoint" );
134
+ REQUIRE (r);
144
135
}
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 ' ;
145
146
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 ;
149
150
150
- return ;
151
- }
152
- }
151
+ std::cerr << " === gdb log begin ===\n " ;
153
152
154
- gdb_apit gdb_api (" test" );
155
- gdb_api.create_gdb_process ();
153
+ while (getline (file, line))
154
+ {
155
+ std::cerr << line << ' \n ' ;
156
+ }
156
157
157
- SECTION (" breakpoint is hit" )
158
- {
159
- const bool r = gdb_api.run_gdb_to_breakpoint (" checkpoint" );
160
- REQUIRE (r);
161
- }
158
+ file.close ();
162
159
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 " ;
168
161
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
+ }
174
165
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 ();
179
168
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
+ }
182
174
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
+ }
184
180
181
+ SECTION (" breakpoint does not exist" )
185
182
{
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);
188
186
}
189
187
188
+ SECTION (" query memory" )
190
189
{
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
+ }
193
207
}
194
208
}
195
-
196
209
#endif
197
210
}
0 commit comments