Move to kernel style SPDX license identifiers
[babeltrace.git] / tests / plugins / flt.lttng-utils.debug-info / test_bin_info.c
1 /*
2 * SPDX-License-Identifier: GPL-2.0-only
3 *
4 * Copyright (C) 2015 EfficiOS Inc. and Linux Foundation
5 * Copyright (C) 2015 Antoine Busque <abusque@efficios.com>
6 * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com>
7 *
8 * Babeltrace SO info tests
9 */
10
11 #define BT_LOG_OUTPUT_LEVEL BT_LOG_WARNING
12 #define BT_LOG_TAG "TEST/BIN-INFO"
13 #include "logging/log.h"
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <inttypes.h>
19 #include <glib.h>
20
21 #include "common/macros.h"
22 #include "common/assert.h"
23 #include <lttng-utils/debug-info/bin-info.h>
24
25 #include "tap/tap.h"
26
27 #define NR_TESTS 57
28
29 #define SO_NAME "libhello_so"
30 #define DEBUG_NAME "libhello_so.debug"
31 #define FUNC_FOO_FILENAME "./libhello.c"
32 #define FUNC_FOO_PRINTF_NAME_FMT "foo+0x%" PRIx64
33 #define FUNC_FOO_NAME_LEN 64
34
35 #define DWARF_DIR_NAME "dwarf_full"
36 #define ELF_DIR_NAME "elf_only"
37 #define BUILDID_DIR_NAME "build_id"
38 #define DEBUGLINK_DIR_NAME "debug_link"
39
40 /* Lower bound of PIC address mapping */
41 #define SO_LOW_ADDR 0x400000
42 /* Size of PIC address mapping */
43 #define SO_MEMSZ 0x800000
44 /* An address outside the PIC mapping */
45 #define SO_INV_ADDR 0x200000
46
47 #define BUILD_ID_HEX_LEN 20
48
49 static uint64_t opt_func_foo_addr;
50 static uint64_t opt_func_foo_printf_offset;
51 static uint64_t opt_func_foo_printf_line_no;
52 static uint64_t opt_func_foo_tp_offset;
53 static uint64_t opt_func_foo_tp_line_no;
54 static uint64_t opt_debug_link_crc;
55 static gchar *opt_build_id;
56 static gchar *opt_debug_info_dir;
57
58 static uint64_t func_foo_printf_addr;
59 static uint64_t func_foo_tp_addr;
60 static char func_foo_printf_name[FUNC_FOO_NAME_LEN];
61 static uint8_t build_id[BUILD_ID_HEX_LEN];
62
63 static GOptionEntry entries[] = {
64 {"foo-addr", 0, 0, G_OPTION_ARG_INT64, &opt_func_foo_addr,
65 "Offset to printf in foo", "0xX"},
66 {"printf-offset", 0, 0, G_OPTION_ARG_INT64, &opt_func_foo_printf_offset,
67 "Offset to printf in foo", "0xX"},
68 {"printf-lineno", 0, 0, G_OPTION_ARG_INT64,
69 &opt_func_foo_printf_line_no, "Line number to printf in foo", "N"},
70 {"tp-offset", 0, 0, G_OPTION_ARG_INT64, &opt_func_foo_tp_offset,
71 "Offset to tp in foo", "0xX"},
72 {"tp-lineno", 0, 0, G_OPTION_ARG_INT64, &opt_func_foo_tp_line_no,
73 "Line number to tp in foo", "N"},
74 {"debug-link-crc", 0, 0, G_OPTION_ARG_INT64, &opt_debug_link_crc,
75 "Debug link CRC", "0xX"},
76 {"build-id", 0, 0, G_OPTION_ARG_STRING, &opt_build_id, "Build ID",
77 "XXXXXXXXXXXXXXX"},
78 {"debug-info-dir", 0, 0, G_OPTION_ARG_STRING, &opt_debug_info_dir,
79 "Debug info directory", NULL},
80 {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}};
81
82 static
83 int build_id_to_bin(void)
84 {
85 int ret, len, i;
86
87 if (!opt_build_id) {
88 goto error;
89 }
90
91 len = strnlen(opt_build_id, BUILD_ID_HEX_LEN * 2);
92 if (len != (BUILD_ID_HEX_LEN * 2)) {
93 goto error;
94 }
95
96 for (i = 0; i < (len / 2); i++) {
97 ret = sscanf(opt_build_id + 2 * i, "%02hhx", &build_id[i]);
98 if (ret != 1) {
99 goto error;
100 }
101 }
102
103 if (i != BUILD_ID_HEX_LEN) {
104 goto error;
105 }
106
107 return 0;
108 error:
109 return -1;
110 }
111
112 static
113 void subtest_has_address(struct bin_info *bin, uint64_t addr)
114 {
115 int ret;
116
117 ret = bin_info_has_address(bin, SO_LOW_ADDR - 1);
118 ok(ret == 0, "bin_info_has_address - address under SO's range");
119
120 ret = bin_info_has_address(bin, SO_LOW_ADDR);
121 ok(ret == 1, "bin_info_has_address - lower bound of SO's range");
122
123 ret = bin_info_has_address(bin, addr);
124 ok(ret == 1, "bin_info_has_address - address in SO's range");
125
126 ret = bin_info_has_address(bin, SO_LOW_ADDR + SO_MEMSZ - 1);
127 ok(ret == 1, "bin_info_has_address - upper bound of SO's range");
128
129 ret = bin_info_has_address(bin, SO_LOW_ADDR + SO_MEMSZ);
130 ok(ret == 0, "bin_info_has_address - address above SO's range");
131 }
132
133 static
134 void subtest_lookup_function_name(struct bin_info *bin, uint64_t addr,
135 char *func_name)
136 {
137 int ret;
138 char *_func_name = NULL;
139
140 ret = bin_info_lookup_function_name(bin, addr, &_func_name);
141 ok(ret == 0, "bin_info_lookup_function_name successful at 0x%" PRIx64, addr);
142 if (_func_name) {
143 ok(strcmp(_func_name, func_name) == 0,
144 "bin_info_lookup_function_name - correct function name (%s == %s)",
145 func_name, _func_name);
146 free(_func_name);
147 _func_name = NULL;
148 } else {
149 skip(1,
150 "bin_info_lookup_function_name - function name is NULL");
151 }
152
153 /* Test function name lookup - erroneous address */
154 ret = bin_info_lookup_function_name(bin, SO_INV_ADDR, &_func_name);
155 ok(ret == -1 && !_func_name,
156 "bin_info_lookup_function_name - fail on invalid addr");
157 free(_func_name);
158 }
159
160 static
161 void subtest_lookup_source_location(struct bin_info *bin, uint64_t addr,
162 uint64_t line_no, const char *filename)
163 {
164 int ret;
165 struct source_location *src_loc = NULL;
166
167 ret = bin_info_lookup_source_location(bin, addr, &src_loc);
168 ok(ret == 0, "bin_info_lookup_source_location successful at 0x%" PRIx64,
169 addr);
170 if (src_loc) {
171 ok(src_loc->line_no == line_no,
172 "bin_info_lookup_source_location - correct line_no (%" PRIu64 " == %" PRIu64 ")",
173 line_no, src_loc->line_no);
174 ok(strcmp(src_loc->filename, filename) == 0,
175 "bin_info_lookup_source_location - correct filename (%s == %s)",
176 filename, src_loc->filename);
177 source_location_destroy(src_loc);
178 src_loc = NULL;
179 } else {
180 fail("bin_info_lookup_source_location - src_loc is NULL");
181 fail("bin_info_lookup_source_location - src_loc is NULL");
182 }
183
184 /* Test source location lookup - erroneous address */
185 ret = bin_info_lookup_source_location(bin, SO_INV_ADDR, &src_loc);
186 ok(ret == -1 && !src_loc,
187 "bin_info_lookup_source_location - fail on invalid addr");
188 if (src_loc) {
189 source_location_destroy(src_loc);
190 }
191 }
192
193 static
194 void test_bin_info_build_id(const char *bin_info_dir)
195 {
196 int ret;
197 char *data_dir, *bin_path;
198 struct bin_info *bin = NULL;
199 struct bt_fd_cache fdc;
200 uint8_t invalid_build_id[BUILD_ID_HEX_LEN] = {
201 0xa3, 0xfd, 0x8b, 0xff, 0x45, 0xe1, 0xa9, 0x32, 0x15, 0xdd,
202 0x6d, 0xaa, 0xd5, 0x53, 0x98, 0x7e, 0xaf, 0xd4, 0x0c, 0xbb
203 };
204
205 diag("bin-info tests - separate DWARF via build ID");
206
207 data_dir = g_build_filename(bin_info_dir, BUILDID_DIR_NAME, NULL);
208 bin_path =
209 g_build_filename(bin_info_dir, BUILDID_DIR_NAME, SO_NAME, NULL);
210
211 if (!data_dir || !bin_path) {
212 exit(EXIT_FAILURE);
213 }
214
215 ret = bt_fd_cache_init(&fdc, BT_LOG_OUTPUT_LEVEL);
216 if (ret != 0) {
217 diag("Failed to initialize FD cache");
218 exit(EXIT_FAILURE);
219 }
220
221 bin = bin_info_create(&fdc, bin_path, SO_LOW_ADDR, SO_MEMSZ, true,
222 data_dir, NULL, BT_LOG_OUTPUT_LEVEL, NULL);
223 ok(bin, "bin_info_create successful (%s)", bin_path);
224
225 /* Test setting invalid build_id */
226 ret = bin_info_set_build_id(bin, invalid_build_id, BUILD_ID_HEX_LEN);
227 ok(ret == -1, "bin_info_set_build_id fail on invalid build_id");
228
229 /* Test setting correct build_id */
230 ret = bin_info_set_build_id(bin, build_id, BUILD_ID_HEX_LEN);
231 ok(ret == 0, "bin_info_set_build_id successful");
232
233 /* Test bin_info_has_address */
234 subtest_has_address(bin, func_foo_printf_addr);
235
236 /* Test function name lookup (with DWARF) */
237 subtest_lookup_function_name(bin, func_foo_printf_addr,
238 func_foo_printf_name);
239
240 /* Test source location lookup */
241 subtest_lookup_source_location(bin, func_foo_printf_addr,
242 opt_func_foo_printf_line_no,
243 FUNC_FOO_FILENAME);
244
245 bin_info_destroy(bin);
246 bt_fd_cache_fini(&fdc);
247 g_free(data_dir);
248 g_free(bin_path);
249 }
250
251 static
252 void test_bin_info_debug_link(const char *bin_info_dir)
253 {
254 int ret;
255 char *data_dir, *bin_path;
256 struct bin_info *bin = NULL;
257 struct bt_fd_cache fdc;
258
259 diag("bin-info tests - separate DWARF via debug link");
260
261 data_dir = g_build_filename(bin_info_dir, DEBUGLINK_DIR_NAME, NULL);
262 bin_path = g_build_filename(bin_info_dir, DEBUGLINK_DIR_NAME, SO_NAME,
263 NULL);
264
265 if (!data_dir || !bin_path) {
266 exit(EXIT_FAILURE);
267 }
268
269 ret = bt_fd_cache_init(&fdc, BT_LOG_OUTPUT_LEVEL);
270 if (ret != 0) {
271 diag("Failed to initialize FD cache");
272 exit(EXIT_FAILURE);
273 }
274
275 bin = bin_info_create(&fdc, bin_path, SO_LOW_ADDR, SO_MEMSZ, true,
276 data_dir, NULL, BT_LOG_OUTPUT_LEVEL, NULL);
277 ok(bin, "bin_info_create successful (%s)", bin_path);
278
279 /* Test setting debug link */
280 ret = bin_info_set_debug_link(bin, DEBUG_NAME, opt_debug_link_crc);
281 ok(ret == 0, "bin_info_set_debug_link successful");
282
283 /* Test bin_info_has_address */
284 subtest_has_address(bin, func_foo_printf_addr);
285
286 /* Test function name lookup (with DWARF) */
287 subtest_lookup_function_name(bin, func_foo_printf_addr,
288 func_foo_printf_name);
289
290 /* Test source location lookup */
291 subtest_lookup_source_location(bin, func_foo_printf_addr,
292 opt_func_foo_printf_line_no,
293 FUNC_FOO_FILENAME);
294
295 bin_info_destroy(bin);
296 bt_fd_cache_fini(&fdc);
297 g_free(data_dir);
298 g_free(bin_path);
299 }
300
301 static
302 void test_bin_info_elf(const char *bin_info_dir)
303 {
304 int ret;
305 char *data_dir, *bin_path;
306 struct bin_info *bin = NULL;
307 struct source_location *src_loc = NULL;
308 struct bt_fd_cache fdc;
309
310 diag("bin-info tests - ELF only");
311
312 data_dir = g_build_filename(bin_info_dir, ELF_DIR_NAME, NULL);
313 bin_path = g_build_filename(bin_info_dir, ELF_DIR_NAME, SO_NAME, NULL);
314
315 if (!data_dir || !bin_path) {
316 exit(EXIT_FAILURE);
317 }
318
319 ret = bt_fd_cache_init(&fdc, BT_LOG_OUTPUT_LEVEL);
320 if (ret != 0) {
321 diag("Failed to initialize FD cache");
322 exit(EXIT_FAILURE);
323 }
324
325 bin = bin_info_create(&fdc, bin_path, SO_LOW_ADDR, SO_MEMSZ, true,
326 data_dir, NULL, BT_LOG_OUTPUT_LEVEL, NULL);
327 ok(bin, "bin_info_create successful (%s)", bin_path);
328
329 /* Test bin_info_has_address */
330 subtest_has_address(bin, func_foo_printf_addr);
331
332 /* Test function name lookup (with ELF) */
333 subtest_lookup_function_name(bin, func_foo_printf_addr,
334 func_foo_printf_name);
335
336 /* Test source location location - should fail on ELF only file */
337 ret = bin_info_lookup_source_location(bin, func_foo_printf_addr,
338 &src_loc);
339 ok(ret == -1,
340 "bin_info_lookup_source_location - fail on ELF only file");
341
342 source_location_destroy(src_loc);
343 bin_info_destroy(bin);
344 bt_fd_cache_fini(&fdc);
345 g_free(data_dir);
346 g_free(bin_path);
347 }
348
349 static
350 void test_bin_info_bundled(const char *bin_info_dir)
351 {
352 int ret;
353 char *data_dir, *bin_path;
354 struct bin_info *bin = NULL;
355 struct bt_fd_cache fdc;
356
357 diag("bin-info tests - DWARF bundled in SO file");
358
359 data_dir = g_build_filename(bin_info_dir, DWARF_DIR_NAME, NULL);
360 bin_path =
361 g_build_filename(bin_info_dir, DWARF_DIR_NAME, SO_NAME, NULL);
362
363 if (!data_dir || !bin_path) {
364 exit(EXIT_FAILURE);
365 }
366
367 ret = bt_fd_cache_init(&fdc, BT_LOG_OUTPUT_LEVEL);
368 if (ret != 0) {
369 diag("Failed to initialize FD cache");
370 exit(EXIT_FAILURE);
371 }
372
373 bin = bin_info_create(&fdc, bin_path, SO_LOW_ADDR, SO_MEMSZ, true,
374 data_dir, NULL, BT_LOG_OUTPUT_LEVEL, NULL);
375 ok(bin, "bin_info_create successful (%s)", bin_path);
376
377 /* Test bin_info_has_address */
378 subtest_has_address(bin, func_foo_printf_addr);
379
380 /* Test function name lookup (with DWARF) */
381 subtest_lookup_function_name(bin, func_foo_printf_addr,
382 func_foo_printf_name);
383
384 /* Test source location lookup */
385 subtest_lookup_source_location(bin, func_foo_printf_addr,
386 opt_func_foo_printf_line_no,
387 FUNC_FOO_FILENAME);
388
389 /* Test source location lookup - inlined function */
390 subtest_lookup_source_location(bin, func_foo_tp_addr,
391 opt_func_foo_tp_line_no,
392 FUNC_FOO_FILENAME);
393
394 bin_info_destroy(bin);
395 bt_fd_cache_fini(&fdc);
396 g_free(data_dir);
397 g_free(bin_path);
398 }
399
400 int main(int argc, char **argv)
401 {
402 int ret;
403 GError *error = NULL;
404 GOptionContext *context;
405 int status;
406
407 context = g_option_context_new("- bin info test");
408 g_option_context_add_main_entries(context, entries, NULL);
409 if (!g_option_context_parse(context, &argc, &argv, &error)) {
410 fprintf(stderr, "option parsing failed: %s\n", error->message);
411 status = EXIT_FAILURE;
412 goto end;
413 }
414
415 g_snprintf(func_foo_printf_name, FUNC_FOO_NAME_LEN,
416 FUNC_FOO_PRINTF_NAME_FMT, opt_func_foo_printf_offset);
417 func_foo_printf_addr =
418 SO_LOW_ADDR + opt_func_foo_addr + opt_func_foo_printf_offset;
419 func_foo_tp_addr =
420 SO_LOW_ADDR + opt_func_foo_addr + opt_func_foo_tp_offset;
421
422 if (build_id_to_bin()) {
423 fprintf(stderr, "Failed to parse / missing build id\n");
424 status = EXIT_FAILURE;
425 goto end;
426 }
427
428 plan_tests(NR_TESTS);
429
430 ret = bin_info_init(BT_LOG_OUTPUT_LEVEL, NULL);
431 ok(ret == 0, "bin_info_init successful");
432
433 test_bin_info_elf(opt_debug_info_dir);
434 test_bin_info_bundled(opt_debug_info_dir);
435 test_bin_info_build_id(opt_debug_info_dir);
436 test_bin_info_debug_link(opt_debug_info_dir);
437
438 status = EXIT_SUCCESS;
439
440 end:
441 g_option_context_free(context);
442
443 return status;
444 }
This page took 0.038561 seconds and 4 git commands to generate.