tools/testing/selftests/bpf/prog_tests/btf_kind.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/prog_tests/btf_kind.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/prog_tests/btf_kind.c- Extension
.c- Size
- 6499 bytes
- Lines
- 227
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
test_progs.hbpf/btf.hbpf/libbpf.h
Detected Declarations
function test_btf_kind_encodingfunction write_raw_btffunction test_btf_kind_decodingfunction test_btf_kind
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2026, Oracle and/or its affiliates. */
#include <test_progs.h>
#include <bpf/btf.h>
#include <bpf/libbpf.h>
/* Verify kind encoding exists for each kind */
static void test_btf_kind_encoding(void)
{
LIBBPF_OPTS(btf_new_opts, opts);
const struct btf_header *hdr;
const void *raw_btf;
struct btf *btf;
__u32 raw_size;
opts.add_layout = true;
btf = btf__new_empty_opts(&opts);
if (!ASSERT_OK_PTR(btf, "btf_new"))
return;
raw_btf = btf__raw_data(btf, &raw_size);
if (!ASSERT_OK_PTR(raw_btf, "btf__raw_data"))
return;
hdr = raw_btf;
ASSERT_EQ(hdr->layout_off % 4, 0, "layout_aligned");
ASSERT_EQ(hdr->layout_len, sizeof(struct btf_layout) * NR_BTF_KINDS,
"layout_len");
ASSERT_EQ(hdr->str_off, hdr->layout_off + hdr->layout_len, "str_after_layout");
btf__free(btf);
opts.add_layout = false;
btf = btf__new_empty_opts(&opts);
if (!ASSERT_OK_PTR(btf, "btf_new"))
return;
raw_btf = btf__raw_data(btf, &raw_size);
if (!ASSERT_OK_PTR(raw_btf, "btf__raw_data"))
return;
hdr = raw_btf;
ASSERT_EQ(hdr->layout_off, 0, "no_layout_off");
ASSERT_EQ(hdr->layout_len, 0, "no_layout_len");
ASSERT_EQ(hdr->str_off, hdr->type_off + hdr->type_len, "strs_after_types");
btf__free(btf);
}
static int write_raw_btf(void *raw_btf, size_t raw_size, char *file)
{
int fd = mkstemp(file);
ssize_t n;
if (!ASSERT_OK_FD(fd, "open_raw_btf"))
return -1;
n = write(fd, raw_btf, raw_size);
close(fd);
if (!ASSERT_EQ(n, (ssize_t)raw_size, "write_raw_btf"))
return -1;
return 0;
}
/*
* Fabricate an unrecognized kind at BTF_KIND_MAX + 1, and after adding
* the appropriate struct/typedefs to the BTF such that it recognizes
* this kind, ensure that parsing of BTF containing the unrecognized kind
* can succeed.
*/
void test_btf_kind_decoding(void)
{
char btf_kind_file1[] = "/tmp/test_btf_kind.XXXXXX";
char btf_kind_file2[] = "/tmp/test_btf_kind.XXXXXX";
char btf_kind_file3[] = "/tmp/test_btf_kind.XXXXXX";
struct btf *btf = NULL, *new_btf = NULL;
__s32 int_id, unrec_id, id, id2;
LIBBPF_OPTS(btf_new_opts, opts);
struct btf_layout *l;
struct btf_header *hdr;
const void *raw_btf;
struct btf_type *t;
void *new_raw_btf;
void *str_data;
__u32 raw_size;
opts.add_layout = true;
btf = btf__new_empty_opts(&opts);
if (!ASSERT_OK_PTR(btf, "btf_new"))
return;
Annotation
- Immediate include surface: `test_progs.h`, `bpf/btf.h`, `bpf/libbpf.h`.
- Detected declarations: `function test_btf_kind_encoding`, `function write_raw_btf`, `function test_btf_kind_decoding`, `function test_btf_kind`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.