bootstrap: Standardize on autoreconf -vi
[babeltrace.git] / tests / lib / test_bin_info.c
CommitLineData
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
a1a328b0
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"
394fd13b
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
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 48char *opt_debug_info_dir;
b7b61ced 49char *opt_debug_info_target_prefix;
394fd13b
AB
50
51static
dae52e76 52void 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 68 bin = bin_info_create(path, SO_LOW_ADDR, SO_MEMSZ, true);
d4832a19 69 ok(bin != NULL, "bin_info_create successful");
394fd13b
AB
70
71 /* Test setting build_id */
dae52e76 72 ret = bin_info_set_build_id(bin, build_id, BUILD_ID_LEN);
d4832a19 73 ok(ret == 0, "bin_info_set_build_id successful");
394fd13b
AB
74
75 /* Test function name lookup (with DWARF) */
dae52e76 76 ret = bin_info_lookup_function_name(bin, FUNC_FOO_ADDR, &func_name);
d4832a19 77 ok(ret == 0, "bin_info_lookup_function_name successful");
61288966
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 }
394fd13b
AB
85
86 /* Test source location lookup */
dae52e76 87 ret = bin_info_lookup_source_location(bin, FUNC_FOO_ADDR, &src_loc);
d4832a19 88 ok(ret == 0, "bin_info_lookup_source_location successful");
61288966
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 }
394fd13b 98
dae52e76 99 bin_info_destroy(bin);
394fd13b
AB
100}
101
102static
dae52e76 103void test_bin_info_debug_link(const char *data_dir)
394fd13b
AB
104{
105 int ret;
106 char path[PATH_MAX];
107 char *func_name = NULL;
dae52e76 108 struct bin_info *bin = NULL;
394fd13b 109 struct source_location *src_loc = NULL;
a1a328b0 110 char *dbg_filename = "libhello_debug_link_so.debug";
394fd13b
AB
111 uint32_t crc = 0xe55c2b98;
112
dae52e76 113 diag("bin-info tests - separate DWARF via debug link");
394fd13b
AB
114
115 snprintf(path, PATH_MAX, "%s/%s", data_dir, SO_NAME_DEBUG_LINK);
116
dae52e76 117 bin = bin_info_create(path, SO_LOW_ADDR, SO_MEMSZ, true);
d4832a19 118 ok(bin != NULL, "bin_info_create successful");
394fd13b
AB
119
120 /* Test setting debug link */
dae52e76 121 ret = bin_info_set_debug_link(bin, dbg_filename, crc);
d4832a19 122 ok(ret == 0, "bin_info_set_debug_link successful");
394fd13b
AB
123
124 /* Test function name lookup (with DWARF) */
dae52e76 125 ret = bin_info_lookup_function_name(bin, FUNC_FOO_ADDR_DBG_LINK,
394fd13b 126 &func_name);
d4832a19 127 ok(ret == 0, "bin_info_lookup_function_name successful");
61288966
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 }
394fd13b
AB
135
136 /* Test source location lookup */
dae52e76 137 ret = bin_info_lookup_source_location(bin, FUNC_FOO_ADDR_DBG_LINK,
394fd13b 138 &src_loc);
d4832a19 139 ok(ret == 0, "bin_info_lookup_source_location successful");
61288966
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 }
394fd13b 149
dae52e76 150 bin_info_destroy(bin);
394fd13b
AB
151}
152
153static
dae52e76 154void test_bin_info_elf(const char *data_dir)
394fd13b
AB
155{
156 int ret;
157 char path[PATH_MAX];
158 char *func_name = NULL;
dae52e76 159 struct bin_info *bin = NULL;
394fd13b
AB
160 struct source_location *src_loc = NULL;
161
dae52e76 162 diag("bin-info tests - ELF only");
394fd13b
AB
163
164 snprintf(path, PATH_MAX, "%s/%s", data_dir, SO_NAME_ELF);
165
dae52e76 166 bin = bin_info_create(path, SO_LOW_ADDR, SO_MEMSZ, true);
d4832a19 167 ok(bin != NULL, "bin_info_create successful");
394fd13b
AB
168
169 /* Test function name lookup (with ELF) */
dae52e76 170 ret = bin_info_lookup_function_name(bin, FUNC_FOO_ADDR_ELF, &func_name);
d4832a19 171 ok(ret == 0, "bin_info_lookup_function_name successful");
61288966
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 }
394fd13b
AB
180
181 /* Test function name lookup - erroneous address */
dae52e76 182 ret = bin_info_lookup_function_name(bin, 0, &func_name);
394fd13b 183 ok(ret == -1 && func_name == NULL,
dae52e76 184 "bin_info_lookup_function_name - fail on addr not found");
394fd13b
AB
185
186 /* Test source location location - should fail on ELF only file */
dae52e76
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");
394fd13b
AB
189
190 source_location_destroy(src_loc);
dae52e76 191 bin_info_destroy(bin);
394fd13b
AB
192}
193
194static
dae52e76 195void test_bin_info(const char *data_dir)
394fd13b
AB
196{
197 int ret;
198 char path[PATH_MAX];
199 char *func_name = NULL;
dae52e76 200 struct bin_info *bin = NULL;
394fd13b
AB
201 struct source_location *src_loc = NULL;
202
dae52e76 203 diag("bin-info tests - DWARF bundled with SO file");
394fd13b
AB
204
205 snprintf(path, PATH_MAX, "%s/%s", data_dir, SO_NAME);
206
dae52e76 207 bin = bin_info_create(path, SO_LOW_ADDR, SO_MEMSZ, true);
d4832a19 208 ok(bin != NULL, "bin_info_create successful");
dae52e76
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");
394fd13b
AB
221
222 /* Test function name lookup (with DWARF) */
dae52e76 223 ret = bin_info_lookup_function_name(bin, FUNC_FOO_ADDR, &func_name);
d4832a19 224 ok(ret == 0, "bin_info_lookup_function_name successful");
61288966
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 }
394fd13b
AB
233
234 /* Test function name lookup - erroneous address */
dae52e76 235 ret = bin_info_lookup_function_name(bin, 0, &func_name);
394fd13b 236 ok(ret == -1 && func_name == NULL,
dae52e76 237 "bin_info_lookup_function_name - fail on addr not found");
394fd13b
AB
238
239 /* Test source location lookup */
dae52e76 240 ret = bin_info_lookup_source_location(bin, FUNC_FOO_ADDR, &src_loc);
d4832a19 241 ok(ret == 0, "bin_info_lookup_source_location successful");
61288966
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 }
394fd13b
AB
252
253 /* Test source location lookup - inlined function */
dae52e76 254 ret = bin_info_lookup_source_location(bin, FUNC_FOO_TP_ADDR, &src_loc);
394fd13b 255 ok(ret == 0,
d4832a19 256 "bin_info_lookup_source_location (inlined func) successful");
61288966
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 }
394fd13b
AB
267
268 /* Test source location lookup - erroneous address */
dae52e76 269 ret = bin_info_lookup_source_location(bin, 0, &src_loc);
394fd13b 270 ok(ret == -1 && src_loc == NULL,
dae52e76 271 "bin_info_lookup_source_location - fail on addr not found");
394fd13b 272
dae52e76 273 bin_info_destroy(bin);
394fd13b
AB
274}
275
276int 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 {
3be1e3c9 285 opt_debug_info_dir = argv[1];
394fd13b
AB
286 }
287
dae52e76 288 ret = bin_info_init();
d4832a19 289 ok(ret == 0, "bin_info_init successful");
394fd13b 290
dae52e76
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);
394fd13b
AB
295
296 return EXIT_SUCCESS;
297}
This page took 0.036045 seconds and 4 git commands to generate.