tools/testing/selftests/bpf/prog_tests/libbpf_str.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/prog_tests/libbpf_str.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/prog_tests/libbpf_str.c- Extension
.c- Size
- 5878 bytes
- Lines
- 226
- 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
ctype.htest_progs.hbpf/btf.h
Detected Declarations
function uppercasefunction test_libbpf_bpf_attach_type_strfunction test_libbpf_bpf_link_type_strfunction test_libbpf_bpf_map_type_strfunction test_libbpf_bpf_prog_type_strfunction test_libbpf_str
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
#include <ctype.h>
#include <test_progs.h>
#include <bpf/btf.h>
/*
* Utility function uppercasing an entire string.
*/
static void uppercase(char *s)
{
for (; *s != '\0'; s++)
*s = toupper(*s);
}
/*
* Test case to check that all bpf_attach_type variants are covered by
* libbpf_bpf_attach_type_str.
*/
static void test_libbpf_bpf_attach_type_str(void)
{
struct btf *btf;
const struct btf_type *t;
const struct btf_enum *e;
int i, n, id;
btf = btf__parse("/sys/kernel/btf/vmlinux", NULL);
if (!ASSERT_OK_PTR(btf, "btf_parse"))
return;
/* find enum bpf_attach_type and enumerate each value */
id = btf__find_by_name_kind(btf, "bpf_attach_type", BTF_KIND_ENUM);
if (!ASSERT_GT(id, 0, "bpf_attach_type_id"))
goto cleanup;
t = btf__type_by_id(btf, id);
e = btf_enum(t);
n = btf_vlen(t);
for (i = 0; i < n; e++, i++) {
enum bpf_attach_type attach_type = (enum bpf_attach_type)e->val;
const char *attach_type_name;
const char *attach_type_str;
char buf[256];
if (attach_type == __MAX_BPF_ATTACH_TYPE)
continue;
attach_type_name = btf__str_by_offset(btf, e->name_off);
attach_type_str = libbpf_bpf_attach_type_str(attach_type);
ASSERT_OK_PTR(attach_type_str, attach_type_name);
snprintf(buf, sizeof(buf), "BPF_%s", attach_type_str);
uppercase(buf);
ASSERT_STREQ(buf, attach_type_name, "exp_str_value");
}
cleanup:
btf__free(btf);
}
/*
* Test case to check that all bpf_link_type variants are covered by
* libbpf_bpf_link_type_str.
*/
static void test_libbpf_bpf_link_type_str(void)
{
struct btf *btf;
const struct btf_type *t;
const struct btf_enum *e;
int i, n, id;
btf = btf__parse("/sys/kernel/btf/vmlinux", NULL);
if (!ASSERT_OK_PTR(btf, "btf_parse"))
return;
/* find enum bpf_link_type and enumerate each value */
id = btf__find_by_name_kind(btf, "bpf_link_type", BTF_KIND_ENUM);
if (!ASSERT_GT(id, 0, "bpf_link_type_id"))
goto cleanup;
t = btf__type_by_id(btf, id);
e = btf_enum(t);
n = btf_vlen(t);
for (i = 0; i < n; e++, i++) {
enum bpf_link_type link_type = (enum bpf_link_type)e->val;
const char *link_type_name;
const char *link_type_str;
char buf[256];
if (link_type == __MAX_BPF_LINK_TYPE)
Annotation
- Immediate include surface: `ctype.h`, `test_progs.h`, `bpf/btf.h`.
- Detected declarations: `function uppercase`, `function test_libbpf_bpf_attach_type_str`, `function test_libbpf_bpf_link_type_str`, `function test_libbpf_bpf_map_type_str`, `function test_libbpf_bpf_prog_type_str`, `function test_libbpf_str`.
- 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.