4 * Babeltrace SO info tests
6 * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation
7 * Copyright (c) 2015 Antoine Busque <abusque@efficios.com>
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.
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.
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.
26 #include <babeltrace/bin-info.h>
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
44 #define FUNC_FOO_NAME "foo+0xc3"
45 #define FUNC_FOO_NAME_ELF "foo+0x24"
46 #define BUILD_ID_LEN 20
48 char *opt_debug_info_dir
;
49 char *opt_debug_info_target_prefix
;
52 void test_bin_info_build_id(const char *data_dir
)
56 char *func_name
= NULL
;
57 struct bin_info
*bin
= NULL
;
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
64 diag("bin-info tests - separate DWARF via build ID");
66 snprintf(path
, PATH_MAX
, "%s/%s", data_dir
, SO_NAME_BUILD_ID
);
68 bin
= bin_info_create(path
, SO_LOW_ADDR
, SO_MEMSZ
, true);
69 ok(bin
!= NULL
, "bin_info_create successful");
71 /* Test setting build_id */
72 ret
= bin_info_set_build_id(bin
, build_id
, BUILD_ID_LEN
);
73 ok(ret
== 0, "bin_info_set_build_id successful");
75 /* Test function name lookup (with DWARF) */
76 ret
= bin_info_lookup_function_name(bin
, FUNC_FOO_ADDR
, &func_name
);
77 ok(ret
== 0, "bin_info_lookup_function_name successful");
79 ok(strcmp(func_name
, FUNC_FOO_NAME
) == 0,
80 "bin_info_lookup_function_name - correct func_name value");
83 skip(1, "bin_info_lookup_function_name - func_name is NULL");
86 /* Test source location lookup */
87 ret
= bin_info_lookup_source_location(bin
, FUNC_FOO_ADDR
, &src_loc
);
88 ok(ret
== 0, "bin_info_lookup_source_location successful");
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
);
96 skip(2, "bin_info_lookup_source_location - src_loc is NULL");
99 bin_info_destroy(bin
);
103 void test_bin_info_debug_link(const char *data_dir
)
107 char *func_name
= NULL
;
108 struct bin_info
*bin
= NULL
;
109 struct source_location
*src_loc
= NULL
;
110 char *dbg_filename
= "libhello_debug_link_so.debug";
111 uint32_t crc
= 0xe55c2b98;
113 diag("bin-info tests - separate DWARF via debug link");
115 snprintf(path
, PATH_MAX
, "%s/%s", data_dir
, SO_NAME_DEBUG_LINK
);
117 bin
= bin_info_create(path
, SO_LOW_ADDR
, SO_MEMSZ
, true);
118 ok(bin
!= NULL
, "bin_info_create successful");
120 /* Test setting debug link */
121 ret
= bin_info_set_debug_link(bin
, dbg_filename
, crc
);
122 ok(ret
== 0, "bin_info_set_debug_link successful");
124 /* Test function name lookup (with DWARF) */
125 ret
= bin_info_lookup_function_name(bin
, FUNC_FOO_ADDR_DBG_LINK
,
127 ok(ret
== 0, "bin_info_lookup_function_name successful");
129 ok(strcmp(func_name
, FUNC_FOO_NAME
) == 0,
130 "bin_info_lookup_function_name - correct func_name value");
133 skip(1, "bin_info_lookup_function_name - func_name is NULL");
136 /* Test source location lookup */
137 ret
= bin_info_lookup_source_location(bin
, FUNC_FOO_ADDR_DBG_LINK
,
139 ok(ret
== 0, "bin_info_lookup_source_location successful");
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
);
147 skip(2, "bin_info_lookup_source_location - src_loc is NULL");
150 bin_info_destroy(bin
);
154 void test_bin_info_elf(const char *data_dir
)
158 char *func_name
= NULL
;
159 struct bin_info
*bin
= NULL
;
160 struct source_location
*src_loc
= NULL
;
162 diag("bin-info tests - ELF only");
164 snprintf(path
, PATH_MAX
, "%s/%s", data_dir
, SO_NAME_ELF
);
166 bin
= bin_info_create(path
, SO_LOW_ADDR
, SO_MEMSZ
, true);
167 ok(bin
!= NULL
, "bin_info_create successful");
169 /* Test function name lookup (with ELF) */
170 ret
= bin_info_lookup_function_name(bin
, FUNC_FOO_ADDR_ELF
, &func_name
);
171 ok(ret
== 0, "bin_info_lookup_function_name successful");
173 ok(strcmp(func_name
, FUNC_FOO_NAME_ELF
) == 0,
174 "bin_info_lookup_function_name - correct func_name value");
178 skip(1, "bin_info_lookup_function_name - func_name is NULL");
181 /* Test function name lookup - erroneous address */
182 ret
= bin_info_lookup_function_name(bin
, 0, &func_name
);
183 ok(ret
== -1 && func_name
== NULL
,
184 "bin_info_lookup_function_name - fail on addr not found");
186 /* Test source location location - should fail on ELF only file */
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");
190 source_location_destroy(src_loc
);
191 bin_info_destroy(bin
);
195 void test_bin_info(const char *data_dir
)
199 char *func_name
= NULL
;
200 struct bin_info
*bin
= NULL
;
201 struct source_location
*src_loc
= NULL
;
203 diag("bin-info tests - DWARF bundled with SO file");
205 snprintf(path
, PATH_MAX
, "%s/%s", data_dir
, SO_NAME
);
207 bin
= bin_info_create(path
, SO_LOW_ADDR
, SO_MEMSZ
, true);
208 ok(bin
!= NULL
, "bin_info_create successful");
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");
222 /* Test function name lookup (with DWARF) */
223 ret
= bin_info_lookup_function_name(bin
, FUNC_FOO_ADDR
, &func_name
);
224 ok(ret
== 0, "bin_info_lookup_function_name successful");
226 ok(strcmp(func_name
, FUNC_FOO_NAME
) == 0,
227 "bin_info_lookup_function_name - correct func_name value");
231 skip(1, "bin_info_lookup_function_name - func_name is NULL");
234 /* Test function name lookup - erroneous address */
235 ret
= bin_info_lookup_function_name(bin
, 0, &func_name
);
236 ok(ret
== -1 && func_name
== NULL
,
237 "bin_info_lookup_function_name - fail on addr not found");
239 /* Test source location lookup */
240 ret
= bin_info_lookup_source_location(bin
, FUNC_FOO_ADDR
, &src_loc
);
241 ok(ret
== 0, "bin_info_lookup_source_location successful");
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
);
250 skip(2, "bin_info_lookup_source_location - src_loc is NULL");
253 /* Test source location lookup - inlined function */
254 ret
= bin_info_lookup_source_location(bin
, FUNC_FOO_TP_ADDR
, &src_loc
);
256 "bin_info_lookup_source_location (inlined func) successful");
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
);
265 skip(2, "bin_info_lookup_source_location (inlined func) - src_loc is NULL");
268 /* Test source location lookup - erroneous address */
269 ret
= bin_info_lookup_source_location(bin
, 0, &src_loc
);
270 ok(ret
== -1 && src_loc
== NULL
,
271 "bin_info_lookup_source_location - fail on addr not found");
273 bin_info_destroy(bin
);
276 int main(int argc
, char **argv
)
280 plan_tests(NR_TESTS
);
285 opt_debug_info_dir
= argv
[1];
288 ret
= bin_info_init();
289 ok(ret
== 0, "bin_info_init successful");
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
);