Commit | Line | Data |
---|---|---|
6ab5bdaa | 1 | /* |
d5ddf820 | 2 | * test_bin_info.c |
6ab5bdaa AB |
3 | * |
4 | * Babeltrace SO info tests | |
5 | * | |
6 | * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation | |
7 | * Copyright (c) 2015 Antoine Busque <abusque@efficios.com> | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; under version 2 of the License. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License along | |
19 | * with this program; if not, write to the Free Software Foundation, Inc., | |
20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
21 | */ | |
22 | ||
23 | #include <stdio.h> | |
24 | #include <stdlib.h> | |
25 | #include <string.h> | |
1e638f98 FD |
26 | |
27 | #include <babeltrace/assert-internal.h> | |
c283522b | 28 | #include <lttng-utils/debug-info/bin-info.h> |
1e638f98 | 29 | |
6ab5bdaa AB |
30 | #include "tap/tap.h" |
31 | ||
32 | #define NR_TESTS 36 | |
24d3147c | 33 | #define SO_NAME "libhello_so" |
6a284733 MJ |
34 | |
35 | #define DWARF_DIR_NAME "dwarf_full" | |
36 | #define ELF_DIR_NAME "elf_only" | |
37 | #define BUILDID_DIR_NAME "build_id" | |
38 | #define DEBUGLINK_DIR_NAME "debug_link" | |
39 | ||
6ab5bdaa AB |
40 | #define SO_LOW_ADDR 0x400000 |
41 | #define SO_MEMSZ 0x400000 | |
6a284733 MJ |
42 | #define FUNC_FOO_ADDR 0x402367 |
43 | #define FUNC_FOO_LINE_NO 36 | |
44 | #define FUNC_FOO_FILENAME "./libhello.c" | |
45 | #define FUNC_FOO_TP_ADDR 0x402300 | |
46 | #define FUNC_FOO_TP_LINE_NO 35 | |
47 | #define FUNC_FOO_TP_FILENAME "./libhello.c" | |
48 | #define FUNC_FOO_ADDR_ELF 0x402367 | |
49 | #define FUNC_FOO_ADDR_DBG_LINK 0x402367 | |
50 | #define FUNC_FOO_NAME "foo+0xf0" | |
51 | #define FUNC_FOO_NAME_ELF "foo+0xf0" | |
6ab5bdaa AB |
52 | #define BUILD_ID_LEN 20 |
53 | ||
05984e0c | 54 | char *opt_debug_info_dir; |
5cde0dc1 | 55 | char *opt_debug_info_target_prefix; |
6ab5bdaa AB |
56 | |
57 | static | |
6a284733 | 58 | void test_bin_info_build_id(const char *bin_info_dir) |
6ab5bdaa AB |
59 | { |
60 | int ret; | |
6a284733 | 61 | char *data_dir, *bin_path; |
6ab5bdaa | 62 | char *func_name = NULL; |
d5ddf820 | 63 | struct bin_info *bin = NULL; |
6ab5bdaa | 64 | struct source_location *src_loc = NULL; |
1e638f98 | 65 | struct bt_fd_cache fdc; |
6ab5bdaa AB |
66 | uint8_t build_id[BUILD_ID_LEN] = { |
67 | 0xcd, 0xd9, 0x8c, 0xdd, 0x87, 0xf7, 0xfe, 0x64, 0xc1, 0x3b, | |
68 | 0x6d, 0xaa, 0xd5, 0x53, 0x98, 0x7e, 0xaf, 0xd4, 0x0c, 0xbb | |
69 | }; | |
70 | ||
d5ddf820 | 71 | diag("bin-info tests - separate DWARF via build ID"); |
6ab5bdaa | 72 | |
6a284733 MJ |
73 | data_dir = g_build_filename(bin_info_dir, BUILDID_DIR_NAME, NULL); |
74 | bin_path = g_build_filename(bin_info_dir, BUILDID_DIR_NAME, SO_NAME, NULL); | |
75 | ||
76 | if (data_dir == NULL || bin_path == NULL) { | |
77 | exit(EXIT_FAILURE); | |
78 | } | |
6ab5bdaa | 79 | |
1e638f98 FD |
80 | ret = bt_fd_cache_init(&fdc); |
81 | BT_ASSERT(ret == 0); | |
6a284733 | 82 | bin = bin_info_create(&fdc, bin_path, SO_LOW_ADDR, SO_MEMSZ, true, data_dir, NULL); |
4caab45b | 83 | ok(bin != NULL, "bin_info_create successful"); |
6ab5bdaa AB |
84 | |
85 | /* Test setting build_id */ | |
d5ddf820 | 86 | ret = bin_info_set_build_id(bin, build_id, BUILD_ID_LEN); |
4caab45b | 87 | ok(ret == 0, "bin_info_set_build_id successful"); |
6ab5bdaa AB |
88 | |
89 | /* Test function name lookup (with DWARF) */ | |
d5ddf820 | 90 | ret = bin_info_lookup_function_name(bin, FUNC_FOO_ADDR, &func_name); |
4caab45b | 91 | ok(ret == 0, "bin_info_lookup_function_name successful"); |
98038c00 AB |
92 | if (func_name) { |
93 | ok(strcmp(func_name, FUNC_FOO_NAME) == 0, | |
94 | "bin_info_lookup_function_name - correct func_name value"); | |
95 | free(func_name); | |
96 | } else { | |
97 | skip(1, "bin_info_lookup_function_name - func_name is NULL"); | |
98 | } | |
6ab5bdaa AB |
99 | |
100 | /* Test source location lookup */ | |
d5ddf820 | 101 | ret = bin_info_lookup_source_location(bin, FUNC_FOO_ADDR, &src_loc); |
4caab45b | 102 | ok(ret == 0, "bin_info_lookup_source_location successful"); |
98038c00 AB |
103 | if (src_loc) { |
104 | ok(src_loc->line_no == FUNC_FOO_LINE_NO, | |
105 | "bin_info_lookup_source_location - correct line_no"); | |
106 | ok(strcmp(src_loc->filename, FUNC_FOO_FILENAME) == 0, | |
107 | "bin_info_lookup_source_location - correct filename"); | |
108 | source_location_destroy(src_loc); | |
109 | } else { | |
110 | skip(2, "bin_info_lookup_source_location - src_loc is NULL"); | |
111 | } | |
6ab5bdaa | 112 | |
d5ddf820 | 113 | bin_info_destroy(bin); |
1e638f98 | 114 | bt_fd_cache_fini(&fdc); |
6a284733 MJ |
115 | g_free(data_dir); |
116 | g_free(bin_path); | |
6ab5bdaa AB |
117 | } |
118 | ||
119 | static | |
6a284733 | 120 | void test_bin_info_debug_link(const char *bin_info_dir) |
6ab5bdaa AB |
121 | { |
122 | int ret; | |
6a284733 | 123 | char *data_dir, *bin_path; |
6ab5bdaa | 124 | char *func_name = NULL; |
d5ddf820 | 125 | struct bin_info *bin = NULL; |
6ab5bdaa | 126 | struct source_location *src_loc = NULL; |
6a284733 MJ |
127 | char *dbg_filename = "libhello_so.debug"; |
128 | uint32_t crc = 0x289a8fdc; | |
1e638f98 | 129 | struct bt_fd_cache fdc; |
6ab5bdaa | 130 | |
d5ddf820 | 131 | diag("bin-info tests - separate DWARF via debug link"); |
6ab5bdaa | 132 | |
6a284733 MJ |
133 | data_dir = g_build_filename(bin_info_dir, DEBUGLINK_DIR_NAME, NULL); |
134 | bin_path = g_build_filename(bin_info_dir, DEBUGLINK_DIR_NAME, SO_NAME, NULL); | |
135 | ||
136 | if (data_dir == NULL || bin_path == NULL) { | |
137 | exit(EXIT_FAILURE); | |
138 | } | |
6ab5bdaa | 139 | |
1e638f98 FD |
140 | ret = bt_fd_cache_init(&fdc); |
141 | BT_ASSERT(ret == 0); | |
6a284733 | 142 | bin = bin_info_create(&fdc, bin_path, SO_LOW_ADDR, SO_MEMSZ, true, data_dir, |
1e638f98 | 143 | NULL); |
4caab45b | 144 | ok(bin != NULL, "bin_info_create successful"); |
6ab5bdaa AB |
145 | |
146 | /* Test setting debug link */ | |
d5ddf820 | 147 | ret = bin_info_set_debug_link(bin, dbg_filename, crc); |
4caab45b | 148 | ok(ret == 0, "bin_info_set_debug_link successful"); |
6ab5bdaa AB |
149 | |
150 | /* Test function name lookup (with DWARF) */ | |
d5ddf820 | 151 | ret = bin_info_lookup_function_name(bin, FUNC_FOO_ADDR_DBG_LINK, |
6ab5bdaa | 152 | &func_name); |
4caab45b | 153 | ok(ret == 0, "bin_info_lookup_function_name successful"); |
98038c00 AB |
154 | if (func_name) { |
155 | ok(strcmp(func_name, FUNC_FOO_NAME) == 0, | |
156 | "bin_info_lookup_function_name - correct func_name value"); | |
157 | free(func_name); | |
158 | } else { | |
159 | skip(1, "bin_info_lookup_function_name - func_name is NULL"); | |
160 | } | |
6ab5bdaa AB |
161 | |
162 | /* Test source location lookup */ | |
d5ddf820 | 163 | ret = bin_info_lookup_source_location(bin, FUNC_FOO_ADDR_DBG_LINK, |
6ab5bdaa | 164 | &src_loc); |
4caab45b | 165 | ok(ret == 0, "bin_info_lookup_source_location successful"); |
98038c00 AB |
166 | if (src_loc) { |
167 | ok(src_loc->line_no == FUNC_FOO_LINE_NO, | |
168 | "bin_info_lookup_source_location - correct line_no"); | |
169 | ok(strcmp(src_loc->filename, FUNC_FOO_FILENAME) == 0, | |
170 | "bin_info_lookup_source_location - correct filename"); | |
171 | source_location_destroy(src_loc); | |
172 | } else { | |
173 | skip(2, "bin_info_lookup_source_location - src_loc is NULL"); | |
174 | } | |
6ab5bdaa | 175 | |
d5ddf820 | 176 | bin_info_destroy(bin); |
1e638f98 | 177 | bt_fd_cache_fini(&fdc); |
6a284733 MJ |
178 | g_free(data_dir); |
179 | g_free(bin_path); | |
6ab5bdaa AB |
180 | } |
181 | ||
182 | static | |
6a284733 | 183 | void test_bin_info_elf(const char *bin_info_dir) |
6ab5bdaa AB |
184 | { |
185 | int ret; | |
6a284733 | 186 | char *data_dir, *bin_path; |
6ab5bdaa | 187 | char *func_name = NULL; |
d5ddf820 | 188 | struct bin_info *bin = NULL; |
6ab5bdaa | 189 | struct source_location *src_loc = NULL; |
1e638f98 | 190 | struct bt_fd_cache fdc; |
6ab5bdaa | 191 | |
d5ddf820 | 192 | diag("bin-info tests - ELF only"); |
6ab5bdaa | 193 | |
6a284733 MJ |
194 | data_dir = g_build_filename(bin_info_dir, ELF_DIR_NAME, NULL); |
195 | bin_path = g_build_filename(bin_info_dir, ELF_DIR_NAME, SO_NAME, NULL); | |
196 | ||
197 | if (data_dir == NULL || bin_path == NULL) { | |
198 | exit(EXIT_FAILURE); | |
199 | } | |
6ab5bdaa | 200 | |
1e638f98 FD |
201 | ret = bt_fd_cache_init(&fdc); |
202 | BT_ASSERT(ret == 0); | |
6a284733 | 203 | bin = bin_info_create(&fdc, bin_path, SO_LOW_ADDR, SO_MEMSZ, true, data_dir, NULL); |
4caab45b | 204 | ok(bin != NULL, "bin_info_create successful"); |
6ab5bdaa AB |
205 | |
206 | /* Test function name lookup (with ELF) */ | |
d5ddf820 | 207 | ret = bin_info_lookup_function_name(bin, FUNC_FOO_ADDR_ELF, &func_name); |
4caab45b | 208 | ok(ret == 0, "bin_info_lookup_function_name successful"); |
98038c00 AB |
209 | if (func_name) { |
210 | ok(strcmp(func_name, FUNC_FOO_NAME_ELF) == 0, | |
211 | "bin_info_lookup_function_name - correct func_name value"); | |
212 | free(func_name); | |
213 | func_name = NULL; | |
214 | } else { | |
215 | skip(1, "bin_info_lookup_function_name - func_name is NULL"); | |
216 | } | |
6ab5bdaa AB |
217 | |
218 | /* Test function name lookup - erroneous address */ | |
d5ddf820 | 219 | ret = bin_info_lookup_function_name(bin, 0, &func_name); |
6ab5bdaa | 220 | ok(ret == -1 && func_name == NULL, |
d5ddf820 | 221 | "bin_info_lookup_function_name - fail on addr not found"); |
6ab5bdaa AB |
222 | |
223 | /* Test source location location - should fail on ELF only file */ | |
d5ddf820 AB |
224 | ret = bin_info_lookup_source_location(bin, FUNC_FOO_ADDR_ELF, &src_loc); |
225 | ok(ret == -1, "bin_info_lookup_source_location - fail on ELF only file"); | |
6ab5bdaa AB |
226 | |
227 | source_location_destroy(src_loc); | |
d5ddf820 | 228 | bin_info_destroy(bin); |
1e638f98 | 229 | bt_fd_cache_fini(&fdc); |
6a284733 MJ |
230 | g_free(data_dir); |
231 | g_free(bin_path); | |
6ab5bdaa AB |
232 | } |
233 | ||
234 | static | |
6a284733 | 235 | void test_bin_info(const char *bin_info_dir) |
6ab5bdaa AB |
236 | { |
237 | int ret; | |
6a284733 | 238 | char *data_dir, *bin_path; |
6ab5bdaa | 239 | char *func_name = NULL; |
d5ddf820 | 240 | struct bin_info *bin = NULL; |
6ab5bdaa | 241 | struct source_location *src_loc = NULL; |
1e638f98 | 242 | struct bt_fd_cache fdc; |
6ab5bdaa | 243 | |
d5ddf820 | 244 | diag("bin-info tests - DWARF bundled with SO file"); |
6ab5bdaa | 245 | |
1e638f98 | 246 | |
6a284733 MJ |
247 | data_dir = g_build_filename(bin_info_dir, DWARF_DIR_NAME, NULL); |
248 | bin_path = g_build_filename(bin_info_dir, DWARF_DIR_NAME, SO_NAME, NULL); | |
249 | ||
250 | if (data_dir == NULL || bin_path == NULL) { | |
251 | exit(EXIT_FAILURE); | |
252 | } | |
6ab5bdaa | 253 | |
1e638f98 FD |
254 | ret = bt_fd_cache_init(&fdc); |
255 | BT_ASSERT(ret == 0); | |
6a284733 | 256 | bin = bin_info_create(&fdc, bin_path, SO_LOW_ADDR, SO_MEMSZ, true, data_dir, NULL); |
4caab45b | 257 | ok(bin != NULL, "bin_info_create successful"); |
d5ddf820 AB |
258 | |
259 | /* Test bin_info_has_address */ | |
260 | ret = bin_info_has_address(bin, 0); | |
261 | ok(ret == 0, "bin_info_has_address - address under so's range"); | |
262 | ret = bin_info_has_address(bin, SO_LOW_ADDR); | |
263 | ok(ret == 1, "bin_info_has_address - lower bound of so's range"); | |
264 | ret = bin_info_has_address(bin, FUNC_FOO_ADDR); | |
265 | ok(ret == 1, "bin_info_has_address - address in so's range"); | |
266 | ret = bin_info_has_address(bin, SO_LOW_ADDR + SO_MEMSZ - 1); | |
267 | ok(ret == 1, "bin_info_has_address - upper bound of so's range"); | |
268 | ret = bin_info_has_address(bin, SO_LOW_ADDR + SO_MEMSZ); | |
269 | ok(ret == 0, "bin_info_has_address - address above so's range"); | |
6ab5bdaa AB |
270 | |
271 | /* Test function name lookup (with DWARF) */ | |
d5ddf820 | 272 | ret = bin_info_lookup_function_name(bin, FUNC_FOO_ADDR, &func_name); |
4caab45b | 273 | ok(ret == 0, "bin_info_lookup_function_name successful"); |
98038c00 AB |
274 | if (func_name) { |
275 | ok(strcmp(func_name, FUNC_FOO_NAME) == 0, | |
276 | "bin_info_lookup_function_name - correct func_name value"); | |
277 | free(func_name); | |
278 | func_name = NULL; | |
279 | } else { | |
280 | skip(1, "bin_info_lookup_function_name - func_name is NULL"); | |
281 | } | |
6ab5bdaa AB |
282 | |
283 | /* Test function name lookup - erroneous address */ | |
d5ddf820 | 284 | ret = bin_info_lookup_function_name(bin, 0, &func_name); |
6ab5bdaa | 285 | ok(ret == -1 && func_name == NULL, |
d5ddf820 | 286 | "bin_info_lookup_function_name - fail on addr not found"); |
6ab5bdaa AB |
287 | |
288 | /* Test source location lookup */ | |
d5ddf820 | 289 | ret = bin_info_lookup_source_location(bin, FUNC_FOO_ADDR, &src_loc); |
4caab45b | 290 | ok(ret == 0, "bin_info_lookup_source_location successful"); |
98038c00 AB |
291 | if (src_loc) { |
292 | ok(src_loc->line_no == FUNC_FOO_LINE_NO, | |
293 | "bin_info_lookup_source_location - correct line_no"); | |
294 | ok(strcmp(src_loc->filename, FUNC_FOO_FILENAME) == 0, | |
295 | "bin_info_lookup_source_location - correct filename"); | |
296 | source_location_destroy(src_loc); | |
297 | src_loc = NULL; | |
298 | } else { | |
299 | skip(2, "bin_info_lookup_source_location - src_loc is NULL"); | |
300 | } | |
6ab5bdaa AB |
301 | |
302 | /* Test source location lookup - inlined function */ | |
d5ddf820 | 303 | ret = bin_info_lookup_source_location(bin, FUNC_FOO_TP_ADDR, &src_loc); |
6ab5bdaa | 304 | ok(ret == 0, |
4caab45b | 305 | "bin_info_lookup_source_location (inlined func) successful"); |
98038c00 AB |
306 | if (src_loc) { |
307 | ok(src_loc->line_no == FUNC_FOO_TP_LINE_NO, | |
308 | "bin_info_lookup_source_location (inlined func) - correct line_no"); | |
309 | ok(strcmp(src_loc->filename, FUNC_FOO_TP_FILENAME) == 0, | |
310 | "bin_info_lookup_source_location (inlined func) - correct filename"); | |
311 | source_location_destroy(src_loc); | |
312 | src_loc = NULL; | |
313 | } else { | |
314 | skip(2, "bin_info_lookup_source_location (inlined func) - src_loc is NULL"); | |
315 | } | |
6ab5bdaa AB |
316 | |
317 | /* Test source location lookup - erroneous address */ | |
d5ddf820 | 318 | ret = bin_info_lookup_source_location(bin, 0, &src_loc); |
6ab5bdaa | 319 | ok(ret == -1 && src_loc == NULL, |
d5ddf820 | 320 | "bin_info_lookup_source_location - fail on addr not found"); |
6ab5bdaa | 321 | |
d5ddf820 | 322 | bin_info_destroy(bin); |
1e638f98 | 323 | bt_fd_cache_fini(&fdc); |
6a284733 MJ |
324 | g_free(data_dir); |
325 | g_free(bin_path); | |
6ab5bdaa AB |
326 | } |
327 | ||
328 | int main(int argc, char **argv) | |
329 | { | |
330 | int ret; | |
331 | ||
332 | plan_tests(NR_TESTS); | |
333 | ||
334 | if (argc != 2) { | |
335 | return EXIT_FAILURE; | |
336 | } else { | |
05984e0c | 337 | opt_debug_info_dir = argv[1]; |
6ab5bdaa AB |
338 | } |
339 | ||
d5ddf820 | 340 | ret = bin_info_init(); |
4caab45b | 341 | ok(ret == 0, "bin_info_init successful"); |
6ab5bdaa | 342 | |
d5ddf820 AB |
343 | test_bin_info(opt_debug_info_dir); |
344 | test_bin_info_elf(opt_debug_info_dir); | |
345 | test_bin_info_build_id(opt_debug_info_dir); | |
346 | test_bin_info_debug_link(opt_debug_info_dir); | |
6ab5bdaa AB |
347 | |
348 | return EXIT_SUCCESS; | |
349 | } |