15ad74616SKumar Kartikeya Dwivedi /* SPDX-License-Identifier: GPL-2.0-only */ 25ad74616SKumar Kartikeya Dwivedi /* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */ 35ad74616SKumar Kartikeya Dwivedi 45ad74616SKumar Kartikeya Dwivedi #ifndef __BPF_DIAGNOSTICS_H 55ad74616SKumar Kartikeya Dwivedi #define __BPF_DIAGNOSTICS_H 65ad74616SKumar Kartikeya Dwivedi 75ad74616SKumar Kartikeya Dwivedi #include <linux/compiler_attributes.h> 8b9c5d822SKumar Kartikeya Dwivedi #include <linux/stdarg.h> 95ad74616SKumar Kartikeya Dwivedi #include <linux/types.h> 105ad74616SKumar Kartikeya Dwivedi 11af4ea6e2SKumar Kartikeya Dwivedi struct bpf_func_state; 12af4ea6e2SKumar Kartikeya Dwivedi struct bpf_reg_state; 135ad74616SKumar Kartikeya Dwivedi struct bpf_verifier_env; 14*956a66e5SKumar Kartikeya Dwivedi struct bpf_verifier_state; 15af4ea6e2SKumar Kartikeya Dwivedi struct btf; 16af4ea6e2SKumar Kartikeya Dwivedi 17af4ea6e2SKumar Kartikeya Dwivedi enum bpf_diag_mod_reason { 18af4ea6e2SKumar Kartikeya Dwivedi BPF_DIAG_MOD_WRITE, 19af4ea6e2SKumar Kartikeya Dwivedi BPF_DIAG_MOD_SPILL, 20af4ea6e2SKumar Kartikeya Dwivedi BPF_DIAG_MOD_VAR_WRITE, 21af4ea6e2SKumar Kartikeya Dwivedi BPF_DIAG_MOD_REF_RELEASE, 22af4ea6e2SKumar Kartikeya Dwivedi BPF_DIAG_MOD_PKT_DATA_CHANGE, 23af4ea6e2SKumar Kartikeya Dwivedi BPF_DIAG_MOD_NON_OWN_REF, 24af4ea6e2SKumar Kartikeya Dwivedi BPF_DIAG_MOD_CALLER_SAVED, 25af4ea6e2SKumar Kartikeya Dwivedi }; 265ad74616SKumar Kartikeya Dwivedi 27*956a66e5SKumar Kartikeya Dwivedi enum bpf_diag_context_kind { 28*956a66e5SKumar Kartikeya Dwivedi BPF_DIAG_CONTEXT_NONE, 29*956a66e5SKumar Kartikeya Dwivedi BPF_DIAG_CONTEXT_RCU, 30*956a66e5SKumar Kartikeya Dwivedi BPF_DIAG_CONTEXT_PREEMPT, 31*956a66e5SKumar Kartikeya Dwivedi BPF_DIAG_CONTEXT_IRQ, 32*956a66e5SKumar Kartikeya Dwivedi BPF_DIAG_CONTEXT_LOCK, 33*956a66e5SKumar Kartikeya Dwivedi }; 34*956a66e5SKumar Kartikeya Dwivedi 355ad74616SKumar Kartikeya Dwivedi bool bpf_diag_enabled(const struct bpf_verifier_env *env); 36b9c5d822SKumar Kartikeya Dwivedi int bpf_diag_init(struct bpf_verifier_env *env); 37af4ea6e2SKumar Kartikeya Dwivedi void bpf_diag_init_frame(struct bpf_verifier_env *env, struct bpf_func_state *state); 38b9c5d822SKumar Kartikeya Dwivedi char *bpf_diag_fmt_buf(struct bpf_verifier_env *env, size_t size); 39b9c5d822SKumar Kartikeya Dwivedi const char *bpf_diag_vfmt(struct bpf_verifier_env *env, const char *fmt, va_list args) 40b9c5d822SKumar Kartikeya Dwivedi __printf(2, 0); 41b9c5d822SKumar Kartikeya Dwivedi const char *bpf_diag_fmt(struct bpf_verifier_env *env, const char *fmt, ...) __printf(2, 3); 42af4ea6e2SKumar Kartikeya Dwivedi const char *bpf_diag_fmt_btf_type(struct bpf_verifier_env *env, const struct btf *btf, u32 type_id); 43daf82487SKumar Kartikeya Dwivedi u64 bpf_diag_event_log_save(struct bpf_verifier_env *env); 44daf82487SKumar Kartikeya Dwivedi void bpf_diag_event_log_restore(struct bpf_verifier_env *env, u64 log_pos); 45*956a66e5SKumar Kartikeya Dwivedi u32 bpf_diag_irq_depth(const struct bpf_verifier_state *state); 46b9c5d822SKumar Kartikeya Dwivedi void bpf_diag_free(struct bpf_verifier_env *env); 47daf82487SKumar Kartikeya Dwivedi void bpf_diag_record_branch(struct bpf_verifier_env *env, u32 insn_idx, bool cond_true); 48af4ea6e2SKumar Kartikeya Dwivedi void bpf_diag_mod_begin(struct bpf_verifier_env *env, const struct bpf_reg_state *reg, 49af4ea6e2SKumar Kartikeya Dwivedi const struct bpf_reg_state *origin, enum bpf_diag_mod_reason reason); 50af4ea6e2SKumar Kartikeya Dwivedi void bpf_diag_mod_end(struct bpf_verifier_env *env); 51af4ea6e2SKumar Kartikeya Dwivedi void bpf_diag_record_scrub(struct bpf_verifier_env *env, const struct bpf_reg_state *reg, 52af4ea6e2SKumar Kartikeya Dwivedi enum bpf_diag_mod_reason reason); 53af4ea6e2SKumar Kartikeya Dwivedi void bpf_diag_record_scrub_stack(struct bpf_verifier_env *env, 54af4ea6e2SKumar Kartikeya Dwivedi const struct bpf_func_state *state, s16 min_off, s16 max_off, 55af4ea6e2SKumar Kartikeya Dwivedi enum bpf_diag_mod_reason reason); 569ecd7030SKumar Kartikeya Dwivedi void bpf_diag_record_ref_acquire(struct bpf_verifier_env *env, u32 insn_idx, u32 ref_id); 579ecd7030SKumar Kartikeya Dwivedi void bpf_diag_record_ref_release(struct bpf_verifier_env *env, u32 insn_idx, u32 ref_id); 58*956a66e5SKumar Kartikeya Dwivedi void bpf_diag_record_context(struct bpf_verifier_env *env, u32 insn_idx, 59*956a66e5SKumar Kartikeya Dwivedi enum bpf_diag_context_kind ctx_kind, bool enter, u32 depth); 605ad74616SKumar Kartikeya Dwivedi 615ad74616SKumar Kartikeya Dwivedi #endif /* __BPF_DIAGNOSTICS_H */ 62