@@ -104,94 +104,111 @@ void gdb_api_internals_test()
104
104
TEST_CASE (" gdb api internals test" , " [core][memory-analyzer]" )
105
105
{
106
106
#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
+ }
108
116
#endif
109
117
}
110
118
111
119
TEST_CASE (" gdb api test" , " [core][memory-analyzer]" )
112
120
{
113
121
#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 " )
116
124
{
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 ();
119
130
120
- try
121
131
{
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 ();
140
134
141
- while ( getline (file, line))
135
+ try
142
136
{
143
- std::cerr << line << ' \n ' ;
137
+ const bool r = gdb_api.run_gdb_to_breakpoint (" checkpoint" );
138
+ REQUIRE (r);
144
139
}
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 ' ;
145
150
146
- file.close ();
151
+ std::ifstream file (" gdb.txt" );
152
+ CHECK_RETURN (file.is_open ());
153
+ std::string line;
147
154
148
- std::cerr << " === gdb log end ===\n " ;
155
+ std::cerr << " === gdb log begin ===\n " ;
149
156
150
- return ;
151
- }
152
- }
157
+ while (getline (file, line))
158
+ {
159
+ std::cerr << line << ' \n ' ;
160
+ }
153
161
154
- gdb_apit gdb_api (" test" );
155
- gdb_api.create_gdb_process ();
162
+ file.close ();
156
163
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 " ;
168
165
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
+ }
174
169
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 ();
179
172
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
+ }
182
178
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
+ }
184
184
185
+ SECTION (" breakpoint does not exist" )
185
186
{
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);
188
190
}
189
191
192
+ SECTION (" query memory" )
190
193
{
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
+ }
193
211
}
194
212
}
195
-
196
213
#endif
197
214
}
0 commit comments