这是我用来分析内核函数的 bpf 程序pick_next_task_fiar
。
#include <uapi/linux/ptrace.h>
#include <linux/sched.h>
#include <linux/nsproxy.h>
#include <linux/pid_namespace.h>
struct rq; // forward declaration
struct val_t {
pid_t pid;
u64 vruntime;
int type; // Note, 0 for previous task, 1 for next task.
};
BPF_PERF_OUTPUT(events);
int kprobe_pick_next_fair(struct pt_regs *ctx, struct rq *rq,
struct task_struct *prev)
{
int cpu = rq->cpu;
struct val_t data = {};
data.pid = prev->pid;
data.vruntime = prev->se.vruntime;
data.type = 0;
events.perf_submit(ctx, &data, sizeof(data));
return 0;
};
它报告错误如下:
int cpu = rq->cpu;
~~^
/virtual/main.c:8:8: note: forward declaration of 'struct rq'
struct rq; // forward declaration
^
1 error generated.
Traceback (most recent call last):
File "picknextfair__back.py", line 73, in <module>
b = BPF(text=bpf_text)
File "/usr/lib/python2.7/dist-packages/bcc/__init__.py", line 297, in __init__
raise Exception("Failed to compile BPF text:\n%s" % text)
Exception: Failed to compile BPF text:
我的问题是为什么 bpf 无法识别 ,struct rq
因为我已经包含了# include <linux/sched.h>
. 然而,它确实承认struct task_struct
. 这两个结构在同一个头文件中。
内核版本:ubuntu 16.04 上的 4.4.0-141-generic