@@ -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,117 +100,98 @@ void gdb_api_internals_test()
94
100
}
95
101
}
96
102
97
- bool check_for_gdb ()
98
- {
99
- const bool has_gdb =
100
- run (" gdb" , {" gdb" , " --version" }, " " , " /dev/null" , " /dev/null" ) == 0 ;
101
-
102
- SECTION (" check gdb is on the PATH" )
103
- {
104
- REQUIRE (has_gdb);
105
- }
106
- return has_gdb;
107
- }
108
-
109
103
#endif
110
104
111
105
TEST_CASE (" gdb api internals test" , " [core][memory-analyzer]" )
112
106
{
113
107
#ifdef RUN_GDB_API_TESTS
114
- if (check_for_gdb ())
115
- {
116
- gdb_api_internals_test ();
117
- }
108
+ gdb_api_internals_test ();
118
109
#endif
119
110
}
120
111
121
112
TEST_CASE (" gdb api test" , " [core][memory-analyzer]" )
122
113
{
123
114
#ifdef RUN_GDB_API_TESTS
124
- if (check_for_gdb ())
115
+ check_for_gdb ();
116
+ compile_test_file ();
117
+
125
118
{
126
- compile_test_file ();
119
+ gdb_apit gdb_api (" test" , true );
120
+ gdb_api.create_gdb_process ();
127
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)
128
128
{
129
- gdb_apit gdb_api (" test" , true );
130
- 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;
131
140
132
- try
141
+ std::cerr << " === gdb log begin ===\n " ;
142
+
143
+ while (getline (file, line))
133
144
{
134
- const bool r = gdb_api.run_gdb_to_breakpoint (" checkpoint" );
135
- REQUIRE (r);
145
+ std::cerr << line << ' \n ' ;
136
146
}
137
- catch (const gdb_interaction_exceptiont &e)
138
- {
139
- std::cerr
140
- << " warning: cannot fully unit test GDB API as program cannot "
141
- << " be run with gdb\n " ;
142
- std::cerr << " warning: this may be due to not having the required "
143
- << " permissions (e.g., to invoke ptrace() or to disable ASLR)"
144
- << " \n " ;
145
- std::cerr << " gdb_interaction_exceptiont:" << ' \n ' ;
146
- std::cerr << e.what () << ' \n ' ;
147
147
148
- std::ifstream file ( " gdb.txt " );
149
- CHECK_RETURN (file. is_open ());
150
- std::string line ;
148
+ file. close ( );
149
+
150
+ std::cerr << " === gdb log end === \n " ;
151
151
152
- std::cerr << " === gdb log begin ===\n " ;
152
+ return ;
153
+ }
154
+ }
153
155
154
- while (getline (file, line))
155
- {
156
- std::cerr << line << ' \n ' ;
157
- }
156
+ gdb_apit gdb_api (" test" );
157
+ gdb_api.create_gdb_process ();
158
158
159
- file.close ();
159
+ SECTION (" breakpoint is hit" )
160
+ {
161
+ const bool r = gdb_api.run_gdb_to_breakpoint (" checkpoint" );
162
+ REQUIRE (r);
163
+ }
160
164
161
- 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
+ }
162
170
163
- return ;
164
- }
165
- }
171
+ SECTION (" breakpoint does not exist" )
172
+ {
173
+ REQUIRE_THROWS_AS (
174
+ gdb_api.run_gdb_to_breakpoint (" checkpoint3" ), gdb_interaction_exceptiont);
175
+ }
166
176
167
- gdb_apit gdb_api (" test" );
168
- gdb_api.create_gdb_process ();
177
+ SECTION (" query memory" )
178
+ {
179
+ const bool r = gdb_api.run_gdb_to_breakpoint (" checkpoint" );
180
+ REQUIRE (r);
169
181
170
- SECTION (" breakpoint is hit" )
171
- {
172
- const bool r = gdb_api.run_gdb_to_breakpoint (" checkpoint" );
173
- REQUIRE (r);
174
- }
182
+ REQUIRE (gdb_api.get_value (" x" ) == " 8" );
183
+ REQUIRE (gdb_api.get_value (" s" ) == " abc" );
175
184
176
- SECTION (" breakpoint is not hit" )
177
- {
178
- const bool r = gdb_api.run_gdb_to_breakpoint (" checkpoint2" );
179
- REQUIRE (!r);
180
- }
185
+ const std::regex regex (R"( 0x[1-9a-f][0-9a-f]*)" );
181
186
182
- SECTION (" breakpoint does not exist" )
183
187
{
184
- REQUIRE_THROWS_AS (
185
- gdb_api.run_gdb_to_breakpoint (" checkpoint3" ),
186
- gdb_interaction_exceptiont);
188
+ std::string address = gdb_api.get_memory (" p" );
189
+ REQUIRE (std::regex_match (address, regex));
187
190
}
188
191
189
- SECTION (" query memory" )
190
192
{
191
- const bool r = gdb_api.run_gdb_to_breakpoint (" checkpoint" );
192
- REQUIRE (r);
193
-
194
- REQUIRE (gdb_api.get_value (" x" ) == " 8" );
195
- REQUIRE (gdb_api.get_value (" s" ) == " abc" );
196
-
197
- const std::regex regex (R"( 0x[1-9a-f][0-9a-f]*)" );
198
-
199
- {
200
- std::string address = gdb_api.get_memory (" p" );
201
- REQUIRE (std::regex_match (address, regex));
202
- }
203
-
204
- {
205
- std::string address = gdb_api.get_memory (" vp" );
206
- REQUIRE (std::regex_match (address, regex));
207
- }
193
+ std::string address = gdb_api.get_memory (" vp" );
194
+ REQUIRE (std::regex_match (address, regex));
208
195
}
209
196
}
210
197
#endif
0 commit comments