|
2 | 2 | #include <testing-utils/use_catch.h>
|
3 | 3 |
|
4 | 4 | #ifdef __linux__
|
5 |
| -// \file Test that the regex expression used work as expected. |
6 |
| -#define private public |
7 |
| -#include <memory-analyzer/gdb_api.cpp> |
| 5 | + |
| 6 | +#include <cstdio> |
| 7 | +#include <cstdlib> |
| 8 | +#include <regex> |
8 | 9 | #include <string>
|
9 | 10 |
|
10 |
| -SCENARIO( |
11 |
| - "gdb_apit uses regex as expected for memory", |
12 |
| - "[core][memory-analyzer]") |
| 11 | +#include <fstream> |
| 12 | +#include <iostream> |
| 13 | + |
| 14 | +#include <memory-analyzer/gdb_api.cpp> |
| 15 | + |
| 16 | +void compile_test_file() |
13 | 17 | {
|
14 |
| - gdb_apit gdb_api(""); |
15 |
| - GIVEN("The result of a struct pointer") |
16 |
| - { |
17 |
| - const std::string line = "$2 = (struct cycle_buffer *) 0x601050 <buffer>"; |
18 |
| - THEN("the result matches the memory address in the output") |
19 |
| - { |
20 |
| - REQUIRE(gdb_api.extract_memory(line) == "0x601050"); |
21 |
| - } |
22 |
| - THEN("adding '(gdb) ' to the line doesn't have an influence") |
23 |
| - { |
24 |
| - REQUIRE(gdb_api.extract_memory("(gdb) " + line) == "0x601050"); |
25 |
| - } |
26 |
| - } |
| 18 | + std::string test_file("memory-analyzer/test.c"); |
27 | 19 |
|
28 |
| - GIVEN("The result of a typedef pointer") |
29 |
| - { |
30 |
| - const std::string line = "$4 = (cycle_buffer_entry_t *) 0x602010"; |
31 |
| - THEN("the result matches the memory address in the output") |
32 |
| - { |
33 |
| - REQUIRE(gdb_api.extract_memory(line) == "0x602010"); |
34 |
| - } |
35 |
| - THEN("adding '(gdb) ' to the line doesn't have an influence") |
36 |
| - { |
37 |
| - REQUIRE(gdb_api.extract_memory("(gdb) " + line) == "0x602010"); |
38 |
| - } |
39 |
| - } |
| 20 | + std::string cmd("gcc -g -o test "); |
| 21 | + cmd += test_file; |
40 | 22 |
|
41 |
| - GIVEN("The result of a char pointer") |
42 |
| - { |
43 |
| - const std::string line = "$5 = 0x400734 \"snow\""; |
44 |
| - THEN("the result matches the memory address in the output") |
45 |
| - { |
46 |
| - REQUIRE(gdb_api.extract_memory(line) == "0x400734"); |
47 |
| - } |
48 |
| - THEN("adding '(gdb) ' to the line doesn't have an influence") |
49 |
| - { |
50 |
| - REQUIRE(gdb_api.extract_memory("(gdb) " + line) == "0x400734"); |
51 |
| - } |
52 |
| - } |
| 23 | + const int r = system(cmd.c_str()); |
| 24 | + REQUIRE(!r); |
| 25 | +} |
53 | 26 |
|
54 |
| - GIVEN("The result of a null pointer") |
| 27 | +class gdb_api_testt : public gdb_apit |
| 28 | +{ |
| 29 | + gdb_api_testt(const char *binary) : gdb_apit(binary) |
55 | 30 | {
|
56 |
| - const std::string line = "$2 = 0x0"; |
57 |
| - THEN("the result matches the memory address in the output") |
58 |
| - { |
59 |
| - REQUIRE(gdb_api.extract_memory(line) == "0x0"); |
60 |
| - } |
61 |
| - THEN("adding '(gdb) ' to the line doesn't have an influence") |
62 |
| - { |
63 |
| - REQUIRE(gdb_api.extract_memory("(gdb) " + line) == "0x0"); |
64 |
| - } |
65 | 31 | }
|
66 | 32 |
|
67 |
| - GIVEN("The result of a char pointer at very low address") |
68 |
| - { |
69 |
| - const std::string line = "$34 = 0x007456 \"snow\""; |
70 |
| - THEN("the result matches the memory address and not nullpointer") |
71 |
| - { |
72 |
| - REQUIRE(gdb_api.extract_memory(line) == "0x007456"); |
73 |
| - } |
74 |
| - THEN("adding '(gdb) ' to the line doesn't have an influence") |
75 |
| - { |
76 |
| - REQUIRE(gdb_api.extract_memory("(gdb) " + line) == "0x007456"); |
77 |
| - } |
78 |
| - } |
| 33 | + friend void gdb_api_internals_test(); |
| 34 | +}; |
79 | 35 |
|
80 |
| - GIVEN("The result of a char pointer with some more whitespaces") |
81 |
| - { |
82 |
| - const std::string line = "$12 = 0x400752 \"thunder storm\"\n"; |
83 |
| - THEN("the result matches the memory address in the output") |
84 |
| - { |
85 |
| - REQUIRE(gdb_api.extract_memory(line) == "0x400752"); |
86 |
| - } |
87 |
| - THEN("adding '(gdb) ' to the line doesn't have an influence") |
88 |
| - { |
89 |
| - REQUIRE(gdb_api.extract_memory("(gdb) " + line) == "0x400752"); |
90 |
| - } |
91 |
| - } |
| 36 | +void gdb_api_internals_test() |
| 37 | +{ |
| 38 | + compile_test_file(); |
92 | 39 |
|
93 |
| - GIVEN("The result of an array pointer") |
| 40 | + SECTION("parse gdb output record") |
94 | 41 | {
|
95 |
| - const std::string line = "$5 = (struct a_sub_type_t (*)[4]) 0x602080"; |
96 |
| - THEN("the result matches the memory address in the output") |
97 |
| - { |
98 |
| - REQUIRE(gdb_api.extract_memory(line) == "0x602080"); |
99 |
| - } |
100 |
| - THEN("adding '(gdb) ' to the line doesn't have an influence") |
101 |
| - { |
102 |
| - REQUIRE(gdb_api.extract_memory("(gdb) " + line) == "0x602080"); |
103 |
| - } |
104 |
| - } |
| 42 | + gdb_api_testt gdb_api("test"); |
105 | 43 |
|
106 |
| - GIVEN("A constant struct pointer pointing to 0x0") |
107 |
| - { |
108 |
| - const std::string line = "$3 = (const struct name *) 0x0"; |
109 |
| - THEN("the returned memory address should be 0x0") |
110 |
| - { |
111 |
| - REQUIRE(gdb_api.extract_memory(line) == "0x0"); |
112 |
| - } |
113 |
| - } |
| 44 | + gdb_api_testt::gdb_output_recordt gor; |
114 | 45 |
|
115 |
| - GIVEN("An enum address") |
116 |
| - { |
117 |
| - const std::string line = "$2 = (char *(*)[5]) 0x7e5500 <abc>"; |
118 |
| - THEN("the returned address is the address of the enum") |
119 |
| - { |
120 |
| - REQUIRE(gdb_api.extract_memory(line) == "0x7e5500"); |
121 |
| - } |
| 46 | + gor = gdb_api.parse_gdb_output_record( |
| 47 | + "a = \"1\", b = \"2\", c = {1, 2}, d = [3, 4], e=\"0x0\""); |
| 48 | + |
| 49 | + REQUIRE(gor["a"] == "1"); |
| 50 | + REQUIRE(gor["b"] == "2"); |
| 51 | + REQUIRE(gor["c"] == "{1, 2}"); |
| 52 | + REQUIRE(gor["d"] == "[3, 4]"); |
| 53 | + REQUIRE(gor["e"] == "0x0"); |
122 | 54 | }
|
123 | 55 |
|
124 |
| - GIVEN("The result of an int pointer") |
| 56 | + SECTION("read a line from an input stream") |
125 | 57 | {
|
126 |
| - const std::string line = "$1 = (int *) 0x601088 <e>\n"; |
127 |
| - THEN("the result is the value of memory address of the int pointer") |
128 |
| - { |
129 |
| - REQUIRE(gdb_api.extract_memory(line) == "0x601088"); |
130 |
| - } |
131 |
| - THEN("adding '(gdb) ' to the line doesn't have an influence") |
132 |
| - { |
133 |
| - REQUIRE(gdb_api.extract_memory("(gdb) " + line) == "0x601088"); |
134 |
| - } |
| 58 | + gdb_api_testt gdb_api("test"); |
| 59 | + |
| 60 | + FILE *f = fopen("memory-analyzer/input.txt", "r"); |
| 61 | + gdb_api.input_stream = f; |
| 62 | + |
| 63 | + std::string line; |
| 64 | + |
| 65 | + line = gdb_api.read_next_line(); |
| 66 | + REQUIRE(line == "abc\n"); |
| 67 | + |
| 68 | + line = gdb_api.read_next_line(); |
| 69 | + REQUIRE(line == std::string(1120, 'a') + "\n"); |
| 70 | + |
| 71 | + line = gdb_api.read_next_line(); |
| 72 | + REQUIRE(line == "xyz"); |
135 | 73 | }
|
136 | 74 |
|
137 |
| - GIVEN("Non matching result") |
| 75 | + SECTION("start and exit gdb") |
138 | 76 | {
|
139 |
| - const std::string line = "Something that must not match 0x605940"; |
140 |
| - THEN("an exception is thrown") |
141 |
| - { |
142 |
| - REQUIRE_THROWS_AS( |
143 |
| - gdb_api.extract_memory(line), gdb_interaction_exceptiont); |
144 |
| - } |
| 77 | + gdb_api_testt gdb_api("test"); |
| 78 | + |
| 79 | + gdb_api.create_gdb_process(); |
| 80 | + |
| 81 | + // check input and output streams |
| 82 | + REQUIRE(!ferror(gdb_api.input_stream)); |
| 83 | + REQUIRE(!ferror(gdb_api.output_stream)); |
| 84 | + |
| 85 | + gdb_api.terminate_gdb_process(); |
145 | 86 | }
|
146 | 87 | }
|
147 | 88 |
|
148 |
| -SCENARIO( |
149 |
| - "gdb_apit uses regex as expected for value extraction", |
150 |
| - "[core][memory-analyzer]") |
| 89 | +TEST_CASE("gdb api internals test") |
151 | 90 | {
|
152 |
| - gdb_apit gdb_api(""); |
153 |
| - GIVEN("An integer value") |
154 |
| - { |
155 |
| - const std::string line = "$90 = 100"; |
156 |
| - THEN("the result schould be '100'") |
157 |
| - { |
158 |
| - REQUIRE(gdb_api.extract_value(line) == "100"); |
159 |
| - } |
160 |
| - } |
| 91 | + gdb_api_internals_test(); |
| 92 | +} |
161 | 93 |
|
162 |
| - GIVEN("A string value") |
163 |
| - { |
164 |
| - const std::string line = "$5 = 0x602010 \"snow\""; |
165 |
| - THEN("the result should be 'snow'") |
166 |
| - { |
167 |
| - REQUIRE(gdb_api.extract_value(line) == "snow"); |
168 |
| - } |
169 |
| - } |
| 94 | +TEST_CASE("gdb api test") |
| 95 | +{ |
| 96 | + compile_test_file(); |
170 | 97 |
|
171 |
| - GIVEN("A string with withe spaces") |
172 |
| - { |
173 |
| - const std::string line = "$1323 = 0x1243253 \"thunder storm\"\n"; |
174 |
| - THEN("the result should be 'thunder storm'") |
175 |
| - { |
176 |
| - REQUIRE(gdb_api.extract_value(line) == "thunder storm"); |
177 |
| - } |
178 |
| - } |
| 98 | + gdb_apit gdb_api("test"); |
179 | 99 |
|
180 |
| - GIVEN("A byte value") |
181 |
| - { |
182 |
| - const std::string line = "$2 = 243 '\363'"; |
183 |
| - THEN("the result should be 243") |
184 |
| - { |
185 |
| - REQUIRE(gdb_api.extract_value(line) == "243"); |
186 |
| - } |
187 |
| - } |
| 100 | + gdb_api.create_gdb_process(); |
188 | 101 |
|
189 |
| - GIVEN("A negative int value") |
| 102 | + SECTION("breakpoint is hit") |
190 | 103 | {
|
191 |
| - const std::string line = "$8 = -32"; |
192 |
| - THEN("the result should be -32") |
193 |
| - { |
194 |
| - REQUIRE(gdb_api.extract_value(line) == "-32"); |
195 |
| - } |
| 104 | + const bool r = gdb_api.run_gdb_to_breakpoint("checkpoint"); |
| 105 | + REQUIRE(r); |
196 | 106 | }
|
197 | 107 |
|
198 |
| - GIVEN("An enum value") |
| 108 | + SECTION("breakpoint is not hit") |
199 | 109 | {
|
200 |
| - const std::string line = "$1 = INFO"; |
201 |
| - THEN("the result should be INFO") |
202 |
| - { |
203 |
| - REQUIRE(gdb_api.extract_value(line) == "INFO"); |
204 |
| - } |
| 110 | + const bool r = gdb_api.run_gdb_to_breakpoint("checkpoint2"); |
| 111 | + REQUIRE(!r); |
205 | 112 | }
|
206 | 113 |
|
207 |
| - GIVEN("A void pointer value") |
| 114 | + SECTION("breakpoint does not exist") |
208 | 115 | {
|
209 |
| - const std::string line = "$6 = (const void *) 0x71"; |
210 |
| - THEN("the requried result should be 0x71") |
211 |
| - { |
212 |
| - REQUIRE(gdb_api.extract_value(line) == "0x71"); |
213 |
| - } |
| 116 | + REQUIRE_THROWS_AS( |
| 117 | + gdb_api.run_gdb_to_breakpoint("checkpoint3"), gdb_interaction_exceptiont); |
214 | 118 | }
|
215 | 119 |
|
216 |
| - GIVEN("A gdb response that contains 'cannot access memory'") |
| 120 | + SECTION("query memory") |
217 | 121 | {
|
218 |
| - const std::string line = "Cannot access memory at address 0x71"; |
219 |
| - THEN("a gdb_inaccesible_memoryt excepition must be raised") |
| 122 | + const bool r = gdb_api.run_gdb_to_breakpoint("checkpoint"); |
| 123 | + REQUIRE(r); |
| 124 | + |
| 125 | + REQUIRE(gdb_api.get_value("x") == "8"); |
| 126 | + REQUIRE(gdb_api.get_value("s") == "abc"); |
| 127 | + |
| 128 | + const std::regex regex(R"(0x[1-9a-f][0-9a-f]*)"); |
| 129 | + |
220 | 130 | {
|
221 |
| - REQUIRE_THROWS_AS( |
222 |
| - gdb_api.extract_value(line), gdb_inaccessible_memory_exceptiont); |
| 131 | + std::string address = gdb_api.get_memory("p"); |
| 132 | + REQUIRE(std::regex_match(address, regex)); |
223 | 133 | }
|
224 |
| - } |
225 | 134 |
|
226 |
| - GIVEN("A value that must not match") |
227 |
| - { |
228 |
| - const std::string line = "$90 = must not match 20"; |
229 |
| - THEN("an exception is raised") |
230 | 135 | {
|
231 |
| - REQUIRE_THROWS_AS( |
232 |
| - gdb_api.extract_value(line), gdb_interaction_exceptiont); |
| 136 | + std::string address = gdb_api.get_memory("vp"); |
| 137 | + REQUIRE(std::regex_match(address, regex)); |
233 | 138 | }
|
234 | 139 | }
|
| 140 | + |
| 141 | + gdb_api.terminate_gdb_process(); |
235 | 142 | }
|
| 143 | + |
236 | 144 | #endif
|
0 commit comments