tools/testing/selftests/bpf/progs/test_legacy_printk.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/progs/test_legacy_printk.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/progs/test_legacy_printk.c- Extension
.c- Size
- 1623 bytes
- Lines
- 74
- 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.
Dependency Surface
linux/bpf.hbpf/bpf_helpers.h
Detected Declarations
function handle_legacyfunction handle_modern
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2021 Facebook */
#include <linux/bpf.h>
#define BPF_NO_GLOBAL_DATA
#include <bpf/bpf_helpers.h>
char LICENSE[] SEC("license") = "GPL";
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__type(key, int);
__type(value, int);
__uint(max_entries, 1);
} my_pid_map SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__type(key, int);
__type(value, int);
__uint(max_entries, 1);
} res_map SEC(".maps");
volatile int my_pid_var = 0;
volatile int res_var = 0;
SEC("tp/raw_syscalls/sys_enter")
int handle_legacy(void *ctx)
{
int zero = 0, *my_pid, cur_pid, *my_res;
my_pid = bpf_map_lookup_elem(&my_pid_map, &zero);
if (!my_pid)
return 1;
cur_pid = bpf_get_current_pid_tgid() >> 32;
if (cur_pid != *my_pid)
return 1;
my_res = bpf_map_lookup_elem(&res_map, &zero);
if (!my_res)
return 1;
if (*my_res == 0)
/* use bpf_printk() in combination with BPF_NO_GLOBAL_DATA to
* force .rodata.str1.1 section that previously caused
* problems on old kernels due to libbpf always tried to
* create a global data map for it
*/
bpf_printk("Legacy-case bpf_printk test, pid %d\n", cur_pid);
*my_res = 1;
return *my_res;
}
SEC("tp/raw_syscalls/sys_enter")
int handle_modern(void *ctx)
{
int cur_pid;
cur_pid = bpf_get_current_pid_tgid() >> 32;
if (cur_pid != my_pid_var)
return 1;
if (res_var == 0)
/* we need bpf_printk() to validate libbpf logic around unused
* global maps and legacy kernels; see comment in handle_legacy()
*/
bpf_printk("Modern-case bpf_printk test, pid %d\n", cur_pid);
res_var = 1;
return res_var;
}
Annotation
- Immediate include surface: `linux/bpf.h`, `bpf/bpf_helpers.h`.
- Detected declarations: `function handle_legacy`, `function handle_modern`.
- 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.