Commit 009f5465 authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Alexei Starovoitov
Browse files

bpf: extract register state printing



Extract printing register state representation logic into a separate
helper, as we are going to reuse it for spilled register state printing
in the next patch. This also nicely reduces code nestedness.

No functional changes.

Acked-by: default avatarEduard Zingerman <eddyz87@gmail.com>
Acked-by: default avatarStanislav Fomichev <sdf@google.com>
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20231118034623.3320920-4-andrii@kernel.org


Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent 42feb662
Loading
Loading
Loading
Loading
+63 −57
Original line number Diff line number Diff line
@@ -553,25 +553,12 @@ static void print_scalar_ranges(struct bpf_verifier_env *env,
	}
}

void print_verifier_state(struct bpf_verifier_env *env, const struct bpf_func_state *state,
			  bool print_all)
static void print_reg_state(struct bpf_verifier_env *env, const struct bpf_reg_state *reg)
{
	const struct bpf_reg_state *reg;
	enum bpf_reg_type t;
	int i;
	const char *sep = "";

	if (state->frameno)
		verbose(env, " frame%d:", state->frameno);
	for (i = 0; i < MAX_BPF_REG; i++) {
		reg = &state->regs[i];
	t = reg->type;
		if (t == NOT_INIT)
			continue;
		if (!print_all && !reg_scratched(env, i))
			continue;
		verbose(env, " R%d", i);
		print_liveness(env, reg->live);
		verbose(env, "=");
	if (t == SCALAR_VALUE && reg->precise)
		verbose(env, "P");
	if ((t == SCALAR_VALUE || t == PTR_TO_STACK) &&
@@ -579,19 +566,18 @@ void print_verifier_state(struct bpf_verifier_env *env, const struct bpf_func_st
		/* reg->off should be 0 for SCALAR_VALUE */
		verbose(env, "%s", t == SCALAR_VALUE ? "" : reg_type_str(env, t));
		verbose(env, "%lld", reg->var_off.value + reg->off);
		} else {
			const char *sep = "";

			verbose(env, "%s", reg_type_str(env, t));
			if (base_type(t) == PTR_TO_BTF_ID)
				verbose(env, "%s", btf_type_name(reg->btf, reg->btf_id));
			verbose(env, "(");
		return;
	}
/*
 * _a stands for append, was shortened to avoid multiline statements below.
 * This macro is used to output a comma separated list of attributes.
 */
#define verbose_a(fmt, ...) ({ verbose(env, "%s" fmt, sep, __VA_ARGS__); sep = ","; })

	verbose(env, "%s", reg_type_str(env, t));
	if (base_type(t) == PTR_TO_BTF_ID)
		verbose(env, "%s", btf_type_name(reg->btf, reg->btf_id));
	verbose(env, "(");
	if (reg->id)
		verbose_a("id=%d", reg->id);
	if (reg->ref_obj_id)
@@ -623,10 +609,30 @@ void print_verifier_state(struct bpf_verifier_env *env, const struct bpf_func_st
			verbose_a("var_off=%s", tn_buf);
		}
	}
#undef verbose_a

	verbose(env, ")");

#undef verbose_a
}

void print_verifier_state(struct bpf_verifier_env *env, const struct bpf_func_state *state,
			  bool print_all)
{
	const struct bpf_reg_state *reg;
	enum bpf_reg_type t;
	int i;

	if (state->frameno)
		verbose(env, " frame%d:", state->frameno);
	for (i = 0; i < MAX_BPF_REG; i++) {
		reg = &state->regs[i];
		if (reg->type == NOT_INIT)
			continue;
		if (!print_all && !reg_scratched(env, i))
			continue;
		verbose(env, " R%d", i);
		print_liveness(env, reg->live);
		verbose(env, "=");
		print_reg_state(env, reg);
	}
	for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
		char types_buf[BPF_REG_SIZE + 1];