diff --git a/userspace/libscap/engine/bpf/scap_bpf.c b/userspace/libscap/engine/bpf/scap_bpf.c index 77e5857cdb..95d4792d0a 100644 --- a/userspace/libscap/engine/bpf/scap_bpf.c +++ b/userspace/libscap/engine/bpf/scap_bpf.c @@ -1736,6 +1736,12 @@ const struct scap_stats_v2* scap_bpf_get_stats_v2(struct scap_engine_handle engi return NULL; } + // we can't collect libbpf stats if bpf stats are not enabled + if (!(handle->m_flags & ENGINE_FLAG_BPF_STATS_ENABLED)) + { + flags &= ~PPM_SCAP_STATS_LIBBPF_STATS; + } + if ((flags & PPM_SCAP_STATS_KERNEL_COUNTERS) && (BPF_MAX_KERNEL_COUNTERS_STATS <= nstats_allocated)) { /* KERNEL SIDE STATS COUNTERS */ diff --git a/userspace/libscap/engine/modern_bpf/scap_modern_bpf.c b/userspace/libscap/engine/modern_bpf/scap_modern_bpf.c index 990043354a..3019e64349 100644 --- a/userspace/libscap/engine/modern_bpf/scap_modern_bpf.c +++ b/userspace/libscap/engine/modern_bpf/scap_modern_bpf.c @@ -264,6 +264,12 @@ int32_t scap_modern_bpf__get_stats(struct scap_engine_handle engine, OUT scap_st const struct scap_stats_v2* scap_modern_bpf__get_stats_v2(struct scap_engine_handle engine, uint32_t flags, OUT uint32_t* nstats, OUT int32_t* rc) { + struct modern_bpf_engine* handle = engine.m_handle; + if (!(handle->m_flags & ENGINE_FLAG_BPF_STATS_ENABLED)) + { + // we can't collect libbpf stats if bpf stats are not enabled + flags &= ~PPM_SCAP_STATS_LIBBPF_STATS; + } return pman_get_scap_stats_v2(flags, nstats, rc); }