tools/bpf/bpftool/cfg.c

Source file repositories/reference/linux-study-clean/tools/bpf/bpftool/cfg.c

File Facts

System
Linux kernel
Corpus path
tools/bpf/bpftool/cfg.c
Extension
.c
Size
10394 bytes
Lines
490
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

struct cfg {
	struct list_head funcs;
	int func_num;
};

struct func_node {
	struct list_head l;
	struct list_head bbs;
	struct bpf_insn *start;
	struct bpf_insn *end;
	int idx;
	int bb_num;
};

struct bb_node {
	struct list_head l;
	struct list_head e_prevs;
	struct list_head e_succs;
	struct bpf_insn *head;
	struct bpf_insn *tail;
	int idx;
};

#define EDGE_FLAG_EMPTY		0x0
#define EDGE_FLAG_FALLTHROUGH	0x1
#define EDGE_FLAG_JUMP		0x2
struct edge_node {
	struct list_head l;
	struct bb_node *src;
	struct bb_node *dst;
	int flags;
};

#define ENTRY_BLOCK_INDEX	0
#define EXIT_BLOCK_INDEX	1
#define NUM_FIXED_BLOCKS	2
#define func_prev(func)		list_prev_entry(func, l)
#define func_next(func)		list_next_entry(func, l)
#define bb_prev(bb)		list_prev_entry(bb, l)
#define bb_next(bb)		list_next_entry(bb, l)
#define entry_bb(func)		func_first_bb(func)
#define exit_bb(func)		func_last_bb(func)
#define cfg_first_func(cfg)	\
	list_first_entry(&cfg->funcs, struct func_node, l)
#define cfg_last_func(cfg)	\
	list_last_entry(&cfg->funcs, struct func_node, l)
#define func_first_bb(func)	\
	list_first_entry(&func->bbs, struct bb_node, l)
#define func_last_bb(func)	\
	list_last_entry(&func->bbs, struct bb_node, l)

static struct func_node *cfg_append_func(struct cfg *cfg, struct bpf_insn *insn)
{
	struct func_node *new_func, *func;

	list_for_each_entry(func, &cfg->funcs, l) {
		if (func->start == insn)
			return func;
		else if (func->start > insn)
			break;
	}

	func = func_prev(func);
	new_func = calloc(1, sizeof(*new_func));
	if (!new_func) {
		p_err("OOM when allocating FUNC node");
		return NULL;
	}
	new_func->start = insn;
	new_func->idx = cfg->func_num;
	list_add(&new_func->l, &func->l);
	cfg->func_num++;

	return new_func;
}

static struct bb_node *func_append_bb(struct func_node *func,
				      struct bpf_insn *insn)
{
	struct bb_node *new_bb, *bb;

	list_for_each_entry(bb, &func->bbs, l) {
		if (bb->head == insn)
			return bb;
		else if (bb->head > insn)
			break;
	}

	bb = bb_prev(bb);
	new_bb = calloc(1, sizeof(*new_bb));

Annotation

Implementation Notes