@@ -38,6 +38,11 @@ void compile_test_file()
38
38
run (" gcc" , {" gcc" , " -g" , " -o" , " test" , " memory-analyzer/test.c" }) == 0 );
39
39
}
40
40
41
+ void check_for_gdb ()
42
+ {
43
+ REQUIRE (run (" gdb" , {" gdb" , " --version" }, " " , " /dev/null" , " /dev/null" ) == 0 );
44
+ }
45
+
41
46
class gdb_api_testt : public gdb_apit
42
47
{
43
48
explicit gdb_api_testt (const char *binary) : gdb_apit(binary)
@@ -49,6 +54,7 @@ class gdb_api_testt : public gdb_apit
49
54
50
55
void gdb_api_internals_test ()
51
56
{
57
+ check_for_gdb ();
52
58
compile_test_file ();
53
59
54
60
SECTION (" parse gdb output record" )
@@ -94,116 +100,98 @@ void gdb_api_internals_test()
94
100
}
95
101
}
96
102
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
-
108
103
#endif
109
104
110
105
TEST_CASE (" gdb api internals test" , " [core][memory-analyzer]" )
111
106
{
112
107
#ifdef RUN_GDB_API_TESTS
113
- if (check_for_gdb ())
114
- {
115
- gdb_api_internals_test ();
116
- }
108
+ gdb_api_internals_test ();
117
109
#endif
118
110
}
119
111
120
112
TEST_CASE (" gdb api test" , " [core][memory-analyzer]" )
121
113
{
122
114
#ifdef RUN_GDB_API_TESTS
123
- if (check_for_gdb ())
115
+ check_for_gdb ();
116
+ compile_test_file ();
117
+
124
118
{
125
- compile_test_file ();
119
+ gdb_apit gdb_api (" test" , true );
120
+ gdb_api.create_gdb_process ();
126
121
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)
127
128
{
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;
130
140
131
- try
141
+ std::cerr << " === gdb log begin ===\n " ;
142
+
143
+ while (getline (file, line))
132
144
{
133
- const bool r = gdb_api.run_gdb_to_breakpoint (" checkpoint" );
134
- REQUIRE (r);
145
+ std::cerr << line << ' \n ' ;
135
146
}
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 ' ;
146
147
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 " ;
150
151
151
- std::cerr << " === gdb log begin ===\n " ;
152
+ return ;
153
+ }
154
+ }
152
155
153
- while (getline (file, line))
154
- {
155
- std::cerr << line << ' \n ' ;
156
- }
156
+ gdb_apit gdb_api (" test" );
157
+ gdb_api.create_gdb_process ();
157
158
158
- file.close ();
159
+ SECTION (" breakpoint is hit" )
160
+ {
161
+ const bool r = gdb_api.run_gdb_to_breakpoint (" checkpoint" );
162
+ REQUIRE (r);
163
+ }
159
164
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
+ }
161
170
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
+ }
165
176
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);
168
181
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" );
174
184
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]*)" );
180
186
181
- SECTION (" breakpoint does not exist" )
182
187
{
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));
186
190
}
187
191
188
- SECTION (" query memory" )
189
192
{
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));
207
195
}
208
196
}
209
197
#endif
0 commit comments