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