如何解决将字符串发送到 BPF Map Space 并打印出来
我有一个小的 txt 文件,我想在这里写入 BPF。这是我的 Python 代码对于 BPF 的样子,但我现在无法打印出任何东西。我一直以失败加载程序结束:无效参数和一堆寄存器错误。到目前为止,我的字符串基本上是说你好,世界,你好
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
一个值得注意的错误是提到了检测到的无限循环,这是我需要检查的内容。
解决方法
问题是i
是通过bpf_map_lookup_elem
中的指针传递的,所以编译器实际上无法展开循环(从它的角度来看,i
可能不会线性增加) .
使用中间变量足以解决这个问题:
BPF_ARRAY(lookupTable,char,512);
#define MAX_LENGTH 1
int helloworld2(void *ctx)
{
//print the values in the lookup table
#pragma clang loop unroll(full)
for (int i = 0; i < 1; i++) {
int k = i;
char *key = lookupTable.lookup(&k);
if (key) {
bpf_trace_printk("%s\n",key);
}
}
return 0;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。