From 1967332175b840fbf59547bf3a61ecaecff696ee Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Fri, 19 Sep 2025 04:15:13 +0530 Subject: [PATCH] Add kprobe and vmlinux example --- examples/c-form/ex5.bpf.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 examples/c-form/ex5.bpf.c diff --git a/examples/c-form/ex5.bpf.c b/examples/c-form/ex5.bpf.c new file mode 100644 index 0000000..8eaa54c --- /dev/null +++ b/examples/c-form/ex5.bpf.c @@ -0,0 +1,32 @@ +#include "vmlinux.h" +#include +#include +#include + +// Map: key = struct request*, value = u64 timestamp +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __type(key, struct request *); + __type(value, u64); + __uint(max_entries, 1024); +} start SEC(".maps"); + +// Attach to kprobe for blk_start_request +SEC("kprobe/blk_start_request") +int BPF_KPROBE(trace_start, struct request *req) +{ + u64 ts = bpf_ktime_get_ns(); + bpf_map_update_elem(&start, &req, &ts, BPF_ANY); + return 0; +} + +// Optionally, attach also to blk_mq_start_request +SEC("kprobe/blk_mq_start_request") +int BPF_KPROBE(trace_start_mq, struct request *req) +{ + u64 ts = bpf_ktime_get_ns(); + bpf_map_update_elem(&start, &req, &ts, BPF_ANY); + return 0; +} + +char LICENSE[] SEC("license") = "GPL";