1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
5 static int duration = 0;
7 void btf_dump_printf(void *ctx, const char *fmt, va_list args)
9 vfprintf(ctx, fmt, args);
12 static struct btf_dump_test_case {
16 struct btf_dump_opts opts;
17 } btf_dump_test_cases[] = {
18 {"btf_dump: syntax", "btf_dump_test_case_syntax", true, {}},
19 {"btf_dump: ordering", "btf_dump_test_case_ordering", false, {}},
20 {"btf_dump: padding", "btf_dump_test_case_padding", true, {}},
21 {"btf_dump: packing", "btf_dump_test_case_packing", true, {}},
22 {"btf_dump: bitfields", "btf_dump_test_case_bitfields", true, {}},
23 {"btf_dump: multidim", "btf_dump_test_case_multidim", false, {}},
24 {"btf_dump: namespacing", "btf_dump_test_case_namespacing", false, {}},
27 static int btf_dump_all_types(const struct btf *btf,
28 const struct btf_dump_opts *opts)
30 size_t type_cnt = btf__get_nr_types(btf);
34 d = btf_dump__new(btf, NULL, opts, btf_dump_printf);
38 for (id = 1; id <= type_cnt; id++) {
39 err = btf_dump__dump_type(d, id);
49 static int test_btf_dump_case(int n, struct btf_dump_test_case *t)
51 char test_file[256], out_file[256], diff_cmd[1024];
52 struct btf *btf = NULL;
56 snprintf(test_file, sizeof(test_file), "%s.o", t->file);
58 btf = btf__parse_elf(test_file, NULL);
59 if (CHECK(IS_ERR(btf), "btf_parse_elf",
60 "failed to load test BTF: %ld\n", PTR_ERR(btf))) {
66 /* tests with t->known_ptr_sz have no "long" or "unsigned long" type,
67 * so it's impossible to determine correct pointer size; but if they
68 * do, it should be 8 regardless of host architecture, becaues BPF
69 * target is always 64-bit
71 if (!t->known_ptr_sz) {
72 btf__set_pointer_size(btf, 8);
74 CHECK(btf__pointer_size(btf) != 8, "ptr_sz", "exp %d, got %zu\n",
75 8, btf__pointer_size(btf));
78 snprintf(out_file, sizeof(out_file), "/tmp/%s.output.XXXXXX", t->file);
79 fd = mkstemp(out_file);
80 if (CHECK(fd < 0, "create_tmp", "failed to create file: %d\n", fd)) {
85 if (CHECK(f == NULL, "open_tmp", "failed to open file: %s(%d)\n",
86 strerror(errno), errno)) {
92 err = btf_dump_all_types(btf, &t->opts);
95 if (CHECK(err, "btf_dump", "failure during C dumping: %d\n", err)) {
99 snprintf(test_file, sizeof(test_file), "progs/%s.c", t->file);
100 if (access(test_file, R_OK) == -1)
102 * When the test is run with O=, kselftest copies TEST_FILES
103 * without preserving the directory structure.
105 snprintf(test_file, sizeof(test_file), "%s.c", t->file);
107 * Diff test output and expected test output, contained between
108 * START-EXPECTED-OUTPUT and END-EXPECTED-OUTPUT lines in test case.
109 * For expected output lines, everything before '*' is stripped out.
110 * Also lines containing comment start and comment end markers are
113 snprintf(diff_cmd, sizeof(diff_cmd),
114 "awk '/START-EXPECTED-OUTPUT/{out=1;next} "
115 "/END-EXPECTED-OUTPUT/{out=0} "
116 "/\\/\\*|\\*\\//{next} " /* ignore comment start/end lines */
117 "out {sub(/^[ \\t]*\\*/, \"\"); print}' '%s' | diff -u - '%s'",
118 test_file, out_file);
119 err = system(diff_cmd);
120 if (CHECK(err, "diff",
121 "differing test output, output=%s, err=%d, diff cmd:\n%s\n",
122 out_file, err, diff_cmd))
132 void test_btf_dump() {
135 for (i = 0; i < ARRAY_SIZE(btf_dump_test_cases); i++) {
136 struct btf_dump_test_case *t = &btf_dump_test_cases[i];
138 if (!test__start_subtest(t->name))
141 test_btf_dump_case(i, &btf_dump_test_cases[i]);