All of lore.kernel.org
 help / color / mirror / Atom feed
From: Eduard Zingerman <eddyz87@gmail.com>
To: bpf@vger.kernel.org, ast@kernel.org
Cc: andrii@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev,
	kernel-team@fb.com, yonghong.song@linux.dev,
	jose.marchesi@oracle.com, alan.maguire@oracle.com,
	Eduard Zingerman <eddyz87@gmail.com>
Subject: [PATCH bpf-next v2 3/4] selftests/bpf: tests for btf_dump emit queue API
Date: Fri, 17 May 2024 12:05:54 -0700	[thread overview]
Message-ID: <20240517190555.4032078-4-eddyz87@gmail.com> (raw)
In-Reply-To: <20240517190555.4032078-1-eddyz87@gmail.com>

Test cases for btf_dump__dump_one_type() and company:
- peek a type, save it's dependencies to the emit queue,
  print the content of the emit queue, expect only relevant
  dependencies;
- print a forward declaration for a single type,
  expect no dependencies in the output;
- print a full dependencies for a type,
  expect no dependencies in the output.

Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
---
 .../selftests/bpf/prog_tests/btf_dump.c       | 86 +++++++++++++++++++
 1 file changed, 86 insertions(+)

diff --git a/tools/testing/selftests/bpf/prog_tests/btf_dump.c b/tools/testing/selftests/bpf/prog_tests/btf_dump.c
index e9ea38aa8248..04f32472e221 100644
--- a/tools/testing/selftests/bpf/prog_tests/btf_dump.c
+++ b/tools/testing/selftests/bpf/prog_tests/btf_dump.c
@@ -255,6 +255,90 @@ static void test_btf_dump_incremental(void)
 	btf__free(btf);
 }
 
+static void test_btf_dump_emit_queue(void)
+{
+	struct btf_dump_emit_queue_item *items;
+	struct btf_dump *d = NULL;
+	struct btf *btf = NULL;
+	u32 union_simple;
+	int err, i, cnt;
+
+	btf = btf__parse_elf("btf_dump_test_case_syntax.bpf.o", NULL);
+	if (!ASSERT_OK_PTR(btf, "btf_parse_elf")) {
+		err = -PTR_ERR(btf);
+		btf = NULL;
+		goto err_out;
+	}
+
+	union_simple = btf__find_by_name_kind(btf, "union_simple", BTF_KIND_UNION);
+	ASSERT_GT(union_simple, 0, "'union_simple' id");
+
+	dump_buf_file = open_memstream(&dump_buf, &dump_buf_sz);
+	if (!ASSERT_OK_PTR(dump_buf_file, "dump_memstream"))
+		return;
+	d = btf_dump__new(btf, btf_dump_printf, dump_buf_file, NULL);
+	if (!ASSERT_OK(libbpf_get_error(d), "btf_dump__new"))
+		goto err_out;
+
+	err = btf_dump__order_type(d, union_simple);
+	ASSERT_OK(err, "order type 'union_simple'");
+	cnt = btf_dump__emit_queue_cnt(d);
+	items = btf_dump__emit_queue(d);
+	for (i = 1; i < cnt; i++) {
+		err = btf_dump__dump_one_type(d, items[i].id, items[i].fwd);
+		if (err > 0)
+			fprintf(dump_buf_file, ";\n\n");
+	}
+
+	fflush(dump_buf_file);
+	dump_buf[dump_buf_sz] = 0;
+
+	ASSERT_STREQ(dump_buf,
+"union union_empty {};\n"
+"\n"
+"union union_simple {\n"
+"	void *ptr;\n"
+"	int num;\n"
+"	int_t num2;\n"
+"	union union_empty u;\n"
+"};\n\n", "c_dump1");
+
+	btf_dump__free(d);
+	d = btf_dump__new(btf, btf_dump_printf, dump_buf_file, NULL);
+	if (!ASSERT_OK(libbpf_get_error(d), "btf_dump__new")) {
+		d = NULL;
+		goto err_out;
+	}
+	err = btf_dump__order_type(d, union_simple);
+	ASSERT_OK(err, "order type 'union_simple'");
+
+	rewind(dump_buf_file);
+	btf_dump__dump_one_type(d, union_simple, true);
+	fflush(dump_buf_file);
+	dump_buf[dump_buf_sz] = 0;
+
+	ASSERT_STREQ(dump_buf, "union union_simple", "c_dump2");
+
+	rewind(dump_buf_file);
+	btf_dump__dump_one_type(d, union_simple, false);
+	fflush(dump_buf_file);
+	dump_buf[dump_buf_sz] = 0;
+
+	ASSERT_STREQ(dump_buf,
+"union union_simple {\n"
+"	void *ptr;\n"
+"	int num;\n"
+"	int_t num2;\n"
+"	union union_empty u;\n"
+"}", "c_dump3");
+
+err_out:
+	fclose(dump_buf_file);
+	free(dump_buf);
+	btf_dump__free(d);
+	btf__free(btf);
+}
+
 #define STRSIZE				4096
 
 static void btf_dump_snprintf(void *ctx, const char *fmt, va_list args)
@@ -873,6 +957,8 @@ void test_btf_dump() {
 	}
 	if (test__start_subtest("btf_dump: incremental"))
 		test_btf_dump_incremental();
+	if (test__start_subtest("btf_dump: emit queue"))
+		test_btf_dump_emit_queue();
 
 	btf = libbpf_find_kernel_btf();
 	if (!ASSERT_OK_PTR(btf, "no kernel BTF found"))
-- 
2.34.1


  parent reply	other threads:[~2024-05-17 19:06 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-17 19:05 [PATCH bpf-next v2 0/4] API to access btf_dump emit queue and print single type Eduard Zingerman
2024-05-17 19:05 ` [PATCH bpf-next v2 1/4] libbpf: put forward declarations to btf_dump->emit_queue Eduard Zingerman
2024-05-28 22:05   ` Andrii Nakryiko
2024-05-28 22:25     ` Eduard Zingerman
2024-05-28 22:39       ` Andrii Nakryiko
2024-05-28 22:41         ` Eduard Zingerman
2024-05-17 19:05 ` [PATCH bpf-next v2 2/4] libbpf: API to access btf_dump emit queue and print single type Eduard Zingerman
2024-05-28 22:18   ` Andrii Nakryiko
2024-05-28 22:53     ` Eduard Zingerman
2024-05-28 23:19       ` Andrii Nakryiko
2024-05-28 23:39         ` Eduard Zingerman
2024-06-01  7:22     ` Eduard Zingerman
2024-06-04 17:39       ` Andrii Nakryiko
2024-05-17 19:05 ` Eduard Zingerman [this message]
2024-05-17 19:05 ` [PATCH bpf-next v2 4/4] selftests/bpf: corner case for typedefs handling in btf_dump Eduard Zingerman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240517190555.4032078-4-eddyz87@gmail.com \
    --to=eddyz87@gmail.com \
    --cc=alan.maguire@oracle.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=jose.marchesi@oracle.com \
    --cc=kernel-team@fb.com \
    --cc=martin.lau@linux.dev \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.