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