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> | |
149bc52e | 26 | #include <lttng-utils/bin-info.h> |
6ab5bdaa AB |
27 | #include "tap/tap.h" |
28 | ||
29 | #define NR_TESTS 36 | |
24d3147c MJ |
30 | #define SO_NAME "libhello_so" |
31 | #define SO_NAME_ELF "libhello_elf_so" | |
32 | #define SO_NAME_BUILD_ID "libhello_build_id_so" | |
33 | #define SO_NAME_DEBUG_LINK "libhello_debug_link_so" | |
6ab5bdaa AB |
34 | #define SO_LOW_ADDR 0x400000 |
35 | #define SO_MEMSZ 0x400000 | |
36 | #define FUNC_FOO_ADDR 0x4014ee | |
37 | #define FUNC_FOO_LINE_NO 8 | |
38 | #define FUNC_FOO_FILENAME "/efficios/libhello.c" | |
39 | #define FUNC_FOO_TP_ADDR 0x4014d3 | |
40 | #define FUNC_FOO_TP_LINE_NO 7 | |
41 | #define FUNC_FOO_TP_FILENAME "/efficios/libhello.c" | |
42 | #define FUNC_FOO_ADDR_ELF 0x4013ef | |
43 | #define FUNC_FOO_ADDR_DBG_LINK 0x40148e | |
ff334941 | 44 | #define FUNC_FOO_NAME "foo+0xc3" |
6ab5bdaa AB |
45 | #define FUNC_FOO_NAME_ELF "foo+0x24" |
46 | #define BUILD_ID_LEN 20 | |
47 | ||
05984e0c | 48 | char *opt_debug_info_dir; |
5cde0dc1 | 49 | char *opt_debug_info_target_prefix; |
6ab5bdaa AB |
50 | |
51 | static | |
d5ddf820 | 52 | void test_bin_info_build_id(const char *data_dir) |
6ab5bdaa AB |
53 | { |
54 | int ret; | |
55 | char path[PATH_MAX]; | |
56 | char *func_name = NULL; | |
d5ddf820 | 57 | struct bin_info *bin = NULL; |
6ab5bdaa AB |
58 | struct source_location *src_loc = NULL; |
59 | uint8_t build_id[BUILD_ID_LEN] = { | |
60 | 0xcd, 0xd9, 0x8c, 0xdd, 0x87, 0xf7, 0xfe, 0x64, 0xc1, 0x3b, | |
61 | 0x6d, 0xaa, 0xd5, 0x53, 0x98, 0x7e, 0xaf, 0xd4, 0x0c, 0xbb | |
62 | }; | |
63 | ||
d5ddf820 | 64 | diag("bin-info tests - separate DWARF via build ID"); |
6ab5bdaa AB |
65 | |
66 | snprintf(path, PATH_MAX, "%s/%s", data_dir, SO_NAME_BUILD_ID); | |
67 | ||
149bc52e | 68 | bin = bin_info_create(path, SO_LOW_ADDR, SO_MEMSZ, true, data_dir, NULL); |
4caab45b | 69 | ok(bin != NULL, "bin_info_create successful"); |
6ab5bdaa AB |
70 | |
71 | /* Test setting build_id */ | |
d5ddf820 | 72 | ret = bin_info_set_build_id(bin, build_id, BUILD_ID_LEN); |
4caab45b | 73 | ok(ret == 0, "bin_info_set_build_id successful"); |
6ab5bdaa AB |
74 | |
75 | /* Test function name lookup (with DWARF) */ | |
d5ddf820 | 76 | ret = bin_info_lookup_function_name(bin, FUNC_FOO_ADDR, &func_name); |
4caab45b | 77 | ok(ret == 0, "bin_info_lookup_function_name successful"); |
98038c00 AB |
78 | if (func_name) { |
79 | ok(strcmp(func_name, FUNC_FOO_NAME) == 0, | |
80 | "bin_info_lookup_function_name - correct func_name value"); | |
81 | free(func_name); | |
82 | } else { | |
83 | skip(1, "bin_info_lookup_function_name - func_name is NULL"); | |
84 | } | |
6ab5bdaa AB |
85 | |
86 | /* Test source location lookup */ | |
d5ddf820 | 87 | ret = bin_info_lookup_source_location(bin, FUNC_FOO_ADDR, &src_loc); |
4caab45b | 88 | ok(ret == 0, "bin_info_lookup_source_location successful"); |
98038c00 AB |
89 | if (src_loc) { |
90 | ok(src_loc->line_no == FUNC_FOO_LINE_NO, | |
91 | "bin_info_lookup_source_location - correct line_no"); | |
92 | ok(strcmp(src_loc->filename, FUNC_FOO_FILENAME) == 0, | |
93 | "bin_info_lookup_source_location - correct filename"); | |
94 | source_location_destroy(src_loc); | |
95 | } else { | |
96 | skip(2, "bin_info_lookup_source_location - src_loc is NULL"); | |
97 | } | |
6ab5bdaa | 98 | |
d5ddf820 | 99 | bin_info_destroy(bin); |
6ab5bdaa AB |
100 | } |
101 | ||
102 | static | |
d5ddf820 | 103 | void test_bin_info_debug_link(const char *data_dir) |
6ab5bdaa AB |
104 | { |
105 | int ret; | |
106 | char path[PATH_MAX]; | |
107 | char *func_name = NULL; | |
d5ddf820 | 108 | struct bin_info *bin = NULL; |
6ab5bdaa | 109 | struct source_location *src_loc = NULL; |
24d3147c | 110 | char *dbg_filename = "libhello_debug_link_so.debug"; |
6ab5bdaa AB |
111 | uint32_t crc = 0xe55c2b98; |
112 | ||
d5ddf820 | 113 | diag("bin-info tests - separate DWARF via debug link"); |
6ab5bdaa AB |
114 | |
115 | snprintf(path, PATH_MAX, "%s/%s", data_dir, SO_NAME_DEBUG_LINK); | |
116 | ||
149bc52e | 117 | bin = bin_info_create(path, SO_LOW_ADDR, SO_MEMSZ, true, data_dir, NULL); |
4caab45b | 118 | ok(bin != NULL, "bin_info_create successful"); |
6ab5bdaa AB |
119 | |
120 | /* Test setting debug link */ | |
d5ddf820 | 121 | ret = bin_info_set_debug_link(bin, dbg_filename, crc); |
4caab45b | 122 | ok(ret == 0, "bin_info_set_debug_link successful"); |
6ab5bdaa AB |
123 | |
124 | /* Test function name lookup (with DWARF) */ | |
d5ddf820 | 125 | ret = bin_info_lookup_function_name(bin, FUNC_FOO_ADDR_DBG_LINK, |
6ab5bdaa | 126 | &func_name); |
4caab45b | 127 | ok(ret == 0, "bin_info_lookup_function_name successful"); |
98038c00 AB |
128 | if (func_name) { |
129 | ok(strcmp(func_name, FUNC_FOO_NAME) == 0, | |
130 | "bin_info_lookup_function_name - correct func_name value"); | |
131 | free(func_name); | |
132 | } else { | |
133 | skip(1, "bin_info_lookup_function_name - func_name is NULL"); | |
134 | } | |
6ab5bdaa AB |
135 | |
136 | /* Test source location lookup */ | |
d5ddf820 | 137 | ret = bin_info_lookup_source_location(bin, FUNC_FOO_ADDR_DBG_LINK, |
6ab5bdaa | 138 | &src_loc); |
4caab45b | 139 | ok(ret == 0, "bin_info_lookup_source_location successful"); |
98038c00 AB |
140 | if (src_loc) { |
141 | ok(src_loc->line_no == FUNC_FOO_LINE_NO, | |
142 | "bin_info_lookup_source_location - correct line_no"); | |
143 | ok(strcmp(src_loc->filename, FUNC_FOO_FILENAME) == 0, | |
144 | "bin_info_lookup_source_location - correct filename"); | |
145 | source_location_destroy(src_loc); | |
146 | } else { | |
147 | skip(2, "bin_info_lookup_source_location - src_loc is NULL"); | |
148 | } | |
6ab5bdaa | 149 | |
d5ddf820 | 150 | bin_info_destroy(bin); |
6ab5bdaa AB |
151 | } |
152 | ||
153 | static | |
d5ddf820 | 154 | void test_bin_info_elf(const char *data_dir) |
6ab5bdaa AB |
155 | { |
156 | int ret; | |
157 | char path[PATH_MAX]; | |
158 | char *func_name = NULL; | |
d5ddf820 | 159 | struct bin_info *bin = NULL; |
6ab5bdaa AB |
160 | struct source_location *src_loc = NULL; |
161 | ||
d5ddf820 | 162 | diag("bin-info tests - ELF only"); |
6ab5bdaa AB |
163 | |
164 | snprintf(path, PATH_MAX, "%s/%s", data_dir, SO_NAME_ELF); | |
165 | ||
149bc52e | 166 | bin = bin_info_create(path, SO_LOW_ADDR, SO_MEMSZ, true, data_dir, NULL); |
4caab45b | 167 | ok(bin != NULL, "bin_info_create successful"); |
6ab5bdaa AB |
168 | |
169 | /* Test function name lookup (with ELF) */ | |
d5ddf820 | 170 | ret = bin_info_lookup_function_name(bin, FUNC_FOO_ADDR_ELF, &func_name); |
4caab45b | 171 | ok(ret == 0, "bin_info_lookup_function_name successful"); |
98038c00 AB |
172 | if (func_name) { |
173 | ok(strcmp(func_name, FUNC_FOO_NAME_ELF) == 0, | |
174 | "bin_info_lookup_function_name - correct func_name value"); | |
175 | free(func_name); | |
176 | func_name = NULL; | |
177 | } else { | |
178 | skip(1, "bin_info_lookup_function_name - func_name is NULL"); | |
179 | } | |
6ab5bdaa AB |
180 | |
181 | /* Test function name lookup - erroneous address */ | |
d5ddf820 | 182 | ret = bin_info_lookup_function_name(bin, 0, &func_name); |
6ab5bdaa | 183 | ok(ret == -1 && func_name == NULL, |
d5ddf820 | 184 | "bin_info_lookup_function_name - fail on addr not found"); |
6ab5bdaa AB |
185 | |
186 | /* Test source location location - should fail on ELF only file */ | |
d5ddf820 AB |
187 | ret = bin_info_lookup_source_location(bin, FUNC_FOO_ADDR_ELF, &src_loc); |
188 | ok(ret == -1, "bin_info_lookup_source_location - fail on ELF only file"); | |
6ab5bdaa AB |
189 | |
190 | source_location_destroy(src_loc); | |
d5ddf820 | 191 | bin_info_destroy(bin); |
6ab5bdaa AB |
192 | } |
193 | ||
194 | static | |
d5ddf820 | 195 | void test_bin_info(const char *data_dir) |
6ab5bdaa AB |
196 | { |
197 | int ret; | |
198 | char path[PATH_MAX]; | |
199 | char *func_name = NULL; | |
d5ddf820 | 200 | struct bin_info *bin = NULL; |
6ab5bdaa AB |
201 | struct source_location *src_loc = NULL; |
202 | ||
d5ddf820 | 203 | diag("bin-info tests - DWARF bundled with SO file"); |
6ab5bdaa AB |
204 | |
205 | snprintf(path, PATH_MAX, "%s/%s", data_dir, SO_NAME); | |
206 | ||
149bc52e | 207 | bin = bin_info_create(path, SO_LOW_ADDR, SO_MEMSZ, true, data_dir, NULL); |
4caab45b | 208 | ok(bin != NULL, "bin_info_create successful"); |
d5ddf820 AB |
209 | |
210 | /* Test bin_info_has_address */ | |
211 | ret = bin_info_has_address(bin, 0); | |
212 | ok(ret == 0, "bin_info_has_address - address under so's range"); | |
213 | ret = bin_info_has_address(bin, SO_LOW_ADDR); | |
214 | ok(ret == 1, "bin_info_has_address - lower bound of so's range"); | |
215 | ret = bin_info_has_address(bin, FUNC_FOO_ADDR); | |
216 | ok(ret == 1, "bin_info_has_address - address in so's range"); | |
217 | ret = bin_info_has_address(bin, SO_LOW_ADDR + SO_MEMSZ - 1); | |
218 | ok(ret == 1, "bin_info_has_address - upper bound of so's range"); | |
219 | ret = bin_info_has_address(bin, SO_LOW_ADDR + SO_MEMSZ); | |
220 | ok(ret == 0, "bin_info_has_address - address above so's range"); | |
6ab5bdaa AB |
221 | |
222 | /* Test function name lookup (with DWARF) */ | |
d5ddf820 | 223 | ret = bin_info_lookup_function_name(bin, FUNC_FOO_ADDR, &func_name); |
4caab45b | 224 | ok(ret == 0, "bin_info_lookup_function_name successful"); |
98038c00 AB |
225 | if (func_name) { |
226 | ok(strcmp(func_name, FUNC_FOO_NAME) == 0, | |
227 | "bin_info_lookup_function_name - correct func_name value"); | |
228 | free(func_name); | |
229 | func_name = NULL; | |
230 | } else { | |
231 | skip(1, "bin_info_lookup_function_name - func_name is NULL"); | |
232 | } | |
6ab5bdaa AB |
233 | |
234 | /* Test function name lookup - erroneous address */ | |
d5ddf820 | 235 | ret = bin_info_lookup_function_name(bin, 0, &func_name); |
6ab5bdaa | 236 | ok(ret == -1 && func_name == NULL, |
d5ddf820 | 237 | "bin_info_lookup_function_name - fail on addr not found"); |
6ab5bdaa AB |
238 | |
239 | /* Test source location lookup */ | |
d5ddf820 | 240 | ret = bin_info_lookup_source_location(bin, FUNC_FOO_ADDR, &src_loc); |
4caab45b | 241 | ok(ret == 0, "bin_info_lookup_source_location successful"); |
98038c00 AB |
242 | if (src_loc) { |
243 | ok(src_loc->line_no == FUNC_FOO_LINE_NO, | |
244 | "bin_info_lookup_source_location - correct line_no"); | |
245 | ok(strcmp(src_loc->filename, FUNC_FOO_FILENAME) == 0, | |
246 | "bin_info_lookup_source_location - correct filename"); | |
247 | source_location_destroy(src_loc); | |
248 | src_loc = NULL; | |
249 | } else { | |
250 | skip(2, "bin_info_lookup_source_location - src_loc is NULL"); | |
251 | } | |
6ab5bdaa AB |
252 | |
253 | /* Test source location lookup - inlined function */ | |
d5ddf820 | 254 | ret = bin_info_lookup_source_location(bin, FUNC_FOO_TP_ADDR, &src_loc); |
6ab5bdaa | 255 | ok(ret == 0, |
4caab45b | 256 | "bin_info_lookup_source_location (inlined func) successful"); |
98038c00 AB |
257 | if (src_loc) { |
258 | ok(src_loc->line_no == FUNC_FOO_TP_LINE_NO, | |
259 | "bin_info_lookup_source_location (inlined func) - correct line_no"); | |
260 | ok(strcmp(src_loc->filename, FUNC_FOO_TP_FILENAME) == 0, | |
261 | "bin_info_lookup_source_location (inlined func) - correct filename"); | |
262 | source_location_destroy(src_loc); | |
263 | src_loc = NULL; | |
264 | } else { | |
265 | skip(2, "bin_info_lookup_source_location (inlined func) - src_loc is NULL"); | |
266 | } | |
6ab5bdaa AB |
267 | |
268 | /* Test source location lookup - erroneous address */ | |
d5ddf820 | 269 | ret = bin_info_lookup_source_location(bin, 0, &src_loc); |
6ab5bdaa | 270 | ok(ret == -1 && src_loc == NULL, |
d5ddf820 | 271 | "bin_info_lookup_source_location - fail on addr not found"); |
6ab5bdaa | 272 | |
d5ddf820 | 273 | bin_info_destroy(bin); |
6ab5bdaa AB |
274 | } |
275 | ||
276 | int main(int argc, char **argv) | |
277 | { | |
278 | int ret; | |
279 | ||
280 | plan_tests(NR_TESTS); | |
281 | ||
282 | if (argc != 2) { | |
283 | return EXIT_FAILURE; | |
284 | } else { | |
05984e0c | 285 | opt_debug_info_dir = argv[1]; |
6ab5bdaa AB |
286 | } |
287 | ||
d5ddf820 | 288 | ret = bin_info_init(); |
4caab45b | 289 | ok(ret == 0, "bin_info_init successful"); |
6ab5bdaa | 290 | |
d5ddf820 AB |
291 | test_bin_info(opt_debug_info_dir); |
292 | test_bin_info_elf(opt_debug_info_dir); | |
293 | test_bin_info_build_id(opt_debug_info_dir); | |
294 | test_bin_info_debug_link(opt_debug_info_dir); | |
6ab5bdaa AB |
295 | |
296 | return EXIT_SUCCESS; | |
297 | } |