Commit | Line | Data |
---|---|---|
394fd13b | 1 | /* |
dae52e76 | 2 | * test_bin_info.c |
394fd13b 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> | |
dae52e76 | 26 | #include <babeltrace/bin-info.h> |
394fd13b AB |
27 | #include "tap/tap.h" |
28 | ||
29 | #define NR_TESTS 36 | |
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" | |
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 | |
8ba0c27b | 44 | #define FUNC_FOO_NAME "foo+0xc3" |
394fd13b AB |
45 | #define FUNC_FOO_NAME_ELF "foo+0x24" |
46 | #define BUILD_ID_LEN 20 | |
47 | ||
3be1e3c9 | 48 | char *opt_debug_info_dir; |
b7b61ced | 49 | char *opt_debug_info_target_prefix; |
394fd13b AB |
50 | |
51 | static | |
dae52e76 | 52 | void test_bin_info_build_id(const char *data_dir) |
394fd13b AB |
53 | { |
54 | int ret; | |
55 | char path[PATH_MAX]; | |
56 | char *func_name = NULL; | |
dae52e76 | 57 | struct bin_info *bin = NULL; |
394fd13b 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 | ||
dae52e76 | 64 | diag("bin-info tests - separate DWARF via build ID"); |
394fd13b AB |
65 | |
66 | snprintf(path, PATH_MAX, "%s/%s", data_dir, SO_NAME_BUILD_ID); | |
67 | ||
dae52e76 AB |
68 | bin = bin_info_create(path, SO_LOW_ADDR, SO_MEMSZ, true); |
69 | ok(bin != NULL, "bin_info_create succesful"); | |
394fd13b AB |
70 | |
71 | /* Test setting build_id */ | |
dae52e76 AB |
72 | ret = bin_info_set_build_id(bin, build_id, BUILD_ID_LEN); |
73 | ok(ret == 0, "bin_info_set_build_id succesful"); | |
394fd13b AB |
74 | |
75 | /* Test function name lookup (with DWARF) */ | |
dae52e76 AB |
76 | ret = bin_info_lookup_function_name(bin, FUNC_FOO_ADDR, &func_name); |
77 | ok(ret == 0, "bin_info_lookup_function_name succesful"); | |
394fd13b | 78 | ok(strcmp(func_name, FUNC_FOO_NAME) == 0, |
dae52e76 | 79 | "bin_info_lookup_function_name - correct func_name value"); |
394fd13b AB |
80 | free(func_name); |
81 | ||
82 | /* Test source location lookup */ | |
dae52e76 AB |
83 | ret = bin_info_lookup_source_location(bin, FUNC_FOO_ADDR, &src_loc); |
84 | ok(ret == 0, "bin_info_lookup_source_location succesful"); | |
394fd13b | 85 | ok(src_loc->line_no == FUNC_FOO_LINE_NO, |
dae52e76 | 86 | "bin_info_lookup_source_location - correct line_no"); |
394fd13b | 87 | ok(strcmp(src_loc->filename, FUNC_FOO_FILENAME) == 0, |
dae52e76 | 88 | "bin_info_lookup_source_location - correct filename"); |
394fd13b AB |
89 | source_location_destroy(src_loc); |
90 | ||
dae52e76 | 91 | bin_info_destroy(bin); |
394fd13b AB |
92 | } |
93 | ||
94 | static | |
dae52e76 | 95 | void test_bin_info_debug_link(const char *data_dir) |
394fd13b AB |
96 | { |
97 | int ret; | |
98 | char path[PATH_MAX]; | |
99 | char *func_name = NULL; | |
dae52e76 | 100 | struct bin_info *bin = NULL; |
394fd13b AB |
101 | struct source_location *src_loc = NULL; |
102 | char *dbg_filename = "libhello_debug_link.so.debug"; | |
103 | uint32_t crc = 0xe55c2b98; | |
104 | ||
dae52e76 | 105 | diag("bin-info tests - separate DWARF via debug link"); |
394fd13b AB |
106 | |
107 | snprintf(path, PATH_MAX, "%s/%s", data_dir, SO_NAME_DEBUG_LINK); | |
108 | ||
dae52e76 AB |
109 | bin = bin_info_create(path, SO_LOW_ADDR, SO_MEMSZ, true); |
110 | ok(bin != NULL, "bin_info_create succesful"); | |
394fd13b AB |
111 | |
112 | /* Test setting debug link */ | |
dae52e76 AB |
113 | ret = bin_info_set_debug_link(bin, dbg_filename, crc); |
114 | ok(ret == 0, "bin_info_set_debug_link succesful"); | |
394fd13b AB |
115 | |
116 | /* Test function name lookup (with DWARF) */ | |
dae52e76 | 117 | ret = bin_info_lookup_function_name(bin, FUNC_FOO_ADDR_DBG_LINK, |
394fd13b | 118 | &func_name); |
dae52e76 | 119 | ok(ret == 0, "bin_info_lookup_function_name succesful"); |
394fd13b | 120 | ok(strcmp(func_name, FUNC_FOO_NAME) == 0, |
dae52e76 | 121 | "bin_info_lookup_function_name - correct func_name value"); |
394fd13b AB |
122 | free(func_name); |
123 | ||
124 | /* Test source location lookup */ | |
dae52e76 | 125 | ret = bin_info_lookup_source_location(bin, FUNC_FOO_ADDR_DBG_LINK, |
394fd13b | 126 | &src_loc); |
dae52e76 | 127 | ok(ret == 0, "bin_info_lookup_source_location succesful"); |
394fd13b | 128 | ok(src_loc->line_no == FUNC_FOO_LINE_NO, |
dae52e76 | 129 | "bin_info_lookup_source_location - correct line_no"); |
394fd13b | 130 | ok(strcmp(src_loc->filename, FUNC_FOO_FILENAME) == 0, |
dae52e76 | 131 | "bin_info_lookup_source_location - correct filename"); |
394fd13b AB |
132 | source_location_destroy(src_loc); |
133 | ||
dae52e76 | 134 | bin_info_destroy(bin); |
394fd13b AB |
135 | } |
136 | ||
137 | static | |
dae52e76 | 138 | void test_bin_info_elf(const char *data_dir) |
394fd13b AB |
139 | { |
140 | int ret; | |
141 | char path[PATH_MAX]; | |
142 | char *func_name = NULL; | |
dae52e76 | 143 | struct bin_info *bin = NULL; |
394fd13b AB |
144 | struct source_location *src_loc = NULL; |
145 | ||
dae52e76 | 146 | diag("bin-info tests - ELF only"); |
394fd13b AB |
147 | |
148 | snprintf(path, PATH_MAX, "%s/%s", data_dir, SO_NAME_ELF); | |
149 | ||
dae52e76 AB |
150 | bin = bin_info_create(path, SO_LOW_ADDR, SO_MEMSZ, true); |
151 | ok(bin != NULL, "bin_info_create succesful"); | |
394fd13b AB |
152 | |
153 | /* Test function name lookup (with ELF) */ | |
dae52e76 AB |
154 | ret = bin_info_lookup_function_name(bin, FUNC_FOO_ADDR_ELF, &func_name); |
155 | ok(ret == 0, "bin_info_lookup_function_name succesful"); | |
394fd13b | 156 | ok(strcmp(func_name, FUNC_FOO_NAME_ELF) == 0, |
dae52e76 | 157 | "bin_info_lookup_function_name - correct func_name value"); |
394fd13b AB |
158 | free(func_name); |
159 | func_name = NULL; | |
160 | ||
161 | /* Test function name lookup - erroneous address */ | |
dae52e76 | 162 | ret = bin_info_lookup_function_name(bin, 0, &func_name); |
394fd13b | 163 | ok(ret == -1 && func_name == NULL, |
dae52e76 | 164 | "bin_info_lookup_function_name - fail on addr not found"); |
394fd13b AB |
165 | |
166 | /* Test source location location - should fail on ELF only file */ | |
dae52e76 AB |
167 | ret = bin_info_lookup_source_location(bin, FUNC_FOO_ADDR_ELF, &src_loc); |
168 | ok(ret == -1, "bin_info_lookup_source_location - fail on ELF only file"); | |
394fd13b AB |
169 | |
170 | source_location_destroy(src_loc); | |
dae52e76 | 171 | bin_info_destroy(bin); |
394fd13b AB |
172 | } |
173 | ||
174 | static | |
dae52e76 | 175 | void test_bin_info(const char *data_dir) |
394fd13b AB |
176 | { |
177 | int ret; | |
178 | char path[PATH_MAX]; | |
179 | char *func_name = NULL; | |
dae52e76 | 180 | struct bin_info *bin = NULL; |
394fd13b AB |
181 | struct source_location *src_loc = NULL; |
182 | ||
dae52e76 | 183 | diag("bin-info tests - DWARF bundled with SO file"); |
394fd13b AB |
184 | |
185 | snprintf(path, PATH_MAX, "%s/%s", data_dir, SO_NAME); | |
186 | ||
dae52e76 AB |
187 | bin = bin_info_create(path, SO_LOW_ADDR, SO_MEMSZ, true); |
188 | ok(bin != NULL, "bin_info_create succesful"); | |
189 | ||
190 | /* Test bin_info_has_address */ | |
191 | ret = bin_info_has_address(bin, 0); | |
192 | ok(ret == 0, "bin_info_has_address - address under so's range"); | |
193 | ret = bin_info_has_address(bin, SO_LOW_ADDR); | |
194 | ok(ret == 1, "bin_info_has_address - lower bound of so's range"); | |
195 | ret = bin_info_has_address(bin, FUNC_FOO_ADDR); | |
196 | ok(ret == 1, "bin_info_has_address - address in so's range"); | |
197 | ret = bin_info_has_address(bin, SO_LOW_ADDR + SO_MEMSZ - 1); | |
198 | ok(ret == 1, "bin_info_has_address - upper bound of so's range"); | |
199 | ret = bin_info_has_address(bin, SO_LOW_ADDR + SO_MEMSZ); | |
200 | ok(ret == 0, "bin_info_has_address - address above so's range"); | |
394fd13b AB |
201 | |
202 | /* Test function name lookup (with DWARF) */ | |
dae52e76 AB |
203 | ret = bin_info_lookup_function_name(bin, FUNC_FOO_ADDR, &func_name); |
204 | ok(ret == 0, "bin_info_lookup_function_name succesful"); | |
394fd13b | 205 | ok(strcmp(func_name, FUNC_FOO_NAME) == 0, |
dae52e76 | 206 | "bin_info_lookup_function_name - correct func_name value"); |
394fd13b AB |
207 | free(func_name); |
208 | func_name = NULL; | |
209 | ||
210 | /* Test function name lookup - erroneous address */ | |
dae52e76 | 211 | ret = bin_info_lookup_function_name(bin, 0, &func_name); |
394fd13b | 212 | ok(ret == -1 && func_name == NULL, |
dae52e76 | 213 | "bin_info_lookup_function_name - fail on addr not found"); |
394fd13b AB |
214 | |
215 | /* Test source location lookup */ | |
dae52e76 AB |
216 | ret = bin_info_lookup_source_location(bin, FUNC_FOO_ADDR, &src_loc); |
217 | ok(ret == 0, "bin_info_lookup_source_location succesful"); | |
394fd13b | 218 | ok(src_loc->line_no == FUNC_FOO_LINE_NO, |
dae52e76 | 219 | "bin_info_lookup_source_location - correct line_no"); |
394fd13b | 220 | ok(strcmp(src_loc->filename, FUNC_FOO_FILENAME) == 0, |
dae52e76 | 221 | "bin_info_lookup_source_location - correct filename"); |
394fd13b AB |
222 | source_location_destroy(src_loc); |
223 | src_loc = NULL; | |
224 | ||
225 | /* Test source location lookup - inlined function */ | |
dae52e76 | 226 | ret = bin_info_lookup_source_location(bin, FUNC_FOO_TP_ADDR, &src_loc); |
394fd13b | 227 | ok(ret == 0, |
dae52e76 | 228 | "bin_info_lookup_source_location (inlined func) succesful"); |
394fd13b | 229 | ok(src_loc->line_no == FUNC_FOO_TP_LINE_NO, |
dae52e76 | 230 | "bin_info_lookup_source_location (inlined func) - correct line_no"); |
394fd13b | 231 | ok(strcmp(src_loc->filename, FUNC_FOO_TP_FILENAME) == 0, |
dae52e76 | 232 | "bin_info_lookup_source_location (inlined func) - correct filename"); |
394fd13b AB |
233 | source_location_destroy(src_loc); |
234 | src_loc = NULL; | |
235 | ||
236 | /* Test source location lookup - erroneous address */ | |
dae52e76 | 237 | ret = bin_info_lookup_source_location(bin, 0, &src_loc); |
394fd13b | 238 | ok(ret == -1 && src_loc == NULL, |
dae52e76 | 239 | "bin_info_lookup_source_location - fail on addr not found"); |
394fd13b | 240 | |
dae52e76 | 241 | bin_info_destroy(bin); |
394fd13b AB |
242 | } |
243 | ||
244 | int main(int argc, char **argv) | |
245 | { | |
246 | int ret; | |
247 | ||
248 | plan_tests(NR_TESTS); | |
249 | ||
250 | if (argc != 2) { | |
251 | return EXIT_FAILURE; | |
252 | } else { | |
3be1e3c9 | 253 | opt_debug_info_dir = argv[1]; |
394fd13b AB |
254 | } |
255 | ||
dae52e76 AB |
256 | ret = bin_info_init(); |
257 | ok(ret == 0, "bin_info_init succesful"); | |
394fd13b | 258 | |
dae52e76 AB |
259 | test_bin_info(opt_debug_info_dir); |
260 | test_bin_info_elf(opt_debug_info_dir); | |
261 | test_bin_info_build_id(opt_debug_info_dir); | |
262 | test_bin_info_debug_link(opt_debug_info_dir); | |
394fd13b AB |
263 | |
264 | return EXIT_SUCCESS; | |
265 | } |