xref: /linux/kernel/bpf/diagnostics.c (revision 5ad746166341e3c07250ee09518d7e4ab5cfb966)
1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (c) 2026 Meta Platforms, Inc. and affiliates.
3 
4 #include <linux/bpf_verifier.h>
5 #include <linux/ctype.h>
6 #include <linux/stdarg.h>
7 
8 #include "diagnostics.h"
9 
10 bool bpf_diag_enabled(const struct bpf_verifier_env *env)
11 {
12 	return env->log.level & BPF_LOG_LEVEL;
13 }
14 
15 static void diag_write(struct bpf_verifier_env *env, const char *fmt, ...) __printf(2, 3);
16 
17 static void diag_write(struct bpf_verifier_env *env, const char *fmt, ...)
18 {
19 	va_list args;
20 
21 	if (!bpf_diag_enabled(env))
22 		return;
23 
24 	va_start(args, fmt);
25 	bpf_verifier_vlog(&env->log, fmt, args);
26 	va_end(args);
27 }
28 
29 static void bpf_diag_header(struct bpf_verifier_env *env, const char *category,
30 			    const char *problem)
31 {
32 	char first;
33 
34 	if (!bpf_diag_enabled(env))
35 		return;
36 
37 	category = category ?: "Verifier Error";
38 	problem = problem ?: "";
39 
40 	if (!problem[0]) {
41 		diag_write(env, "\nVerification failed: %s\n", category);
42 		return;
43 	}
44 
45 	first = toupper(problem[0]);
46 	diag_write(env, "\nVerification failed: %s: %c%s\n", category, first, problem + 1);
47 }
48