【发布时间】:2021-10-03 15:11:41
【问题描述】:
我有一个小 txt 文件,我想在此处写入 BPF。这是我的 BPF 的 python 代码的样子,但我现在无法打印出任何东西。我一直以加载程序失败告终:带有一堆寄存器错误的无效参数。截至目前,我的字符串基本上说你好,世界,嗨
BPF_ARRAY(lookupTable, char, 512);
int helloworld2(void *ctx)
{
//print the values in the lookup table
#pragma clang loop unroll(full)
for (int i = 0; i < 512; i++) {
char *key = lookupTable.lookup(&i);
if (key) {
bpf_trace_printk("%s\n", key);
}
}
return 0;
}
这是 Python 代码:
b = BPF(src_file="hello.c")
lookupTable = b["lookupTable"]
#add hello.csv to the lookupTable array
f = open("hello.csv","r")
file_contents = f.read()
#append file contents to the lookupTable array
b_string1 = file_contents.encode('utf-8')
b_string1 = ctypes.create_string_buffer(b_string1)
lookupTable[0] = b_string1
f.close()
b.attach_kprobe(event=b.get_syscall_fnname("clone"), fn_name="helloworld2")
b.trace_print()
我在这个 pastebin 中链接了错误,因为它太长了: BPF Error
一个值得注意的错误是提到检测到无限循环,这是我需要检查的内容。
【问题讨论】:
-
您是否检查过 clang 是否真的展开了循环(
bpftool prog dump xlated <prog>一旦加载,或llvm-objdump -S <objfile>,但您不会拥有带有密件抄送的目标文件)?我怀疑它不会,因为您将指向i的指针传递给lookupTable.lookup(&i),这意味着i中的值可能会在循环内部发生变化,并且您可能可能具有此无限环形。查找助手当然是只读的,但 clang 并不知道这一点。 -
由于发送查找指针的性质,Clang 实际上无法展开循环。 @Pchaigno 的具有中介价值的解决方案似乎在使其真正发挥作用方面效果很好。