如果你再细看bpf_trace_printk函数的源代码,其实还能看到更多信息(或者说是限制),比如字符串版本中只允许1个%s,详细代码看这里,我简单梳理了这个函数源代码的调用背景,有兴趣的同学可以深入看看。 除了bpf_trace_printk函数可以添加日志之外,还可以使用bpf_perf_event_output函数(如果你使用BCC,它的入口函数是BPF_PER...
bpf_printk()辅助程序和trace_pipe文件通常不会在生产环境中使用,但它对于调试 BPF 代码和深入了解 BPF 程序正在做什么是必不可少的。 由于还没有 BPF 调试器,bpf_printk()` 通常是调试 BPF 代码中问题的最快和最方便的方法。 这就是“最小”应用程序的 BPF 端。 随意向handle_tp()BPF 程序的主体添加任何...
所以说BPF程序是一种虚拟机代码。 #include<linux/bpf.h>#include<linux/pkt_cls.h>#include<bpf_helpers.h>#ifndef __section# define __section(NAME) \__attribute__((section(NAME), used))#endif__section("egress")inttc_egress(struct__sk_buff*skb){bpf_printk("Hello, world\n");returnTC_A...
其中的 uprobe/和 uretprobe/ 是关键,libbpf 根据这两个关键字确定在此段中定义的函数为 uprobe/uretprobe 相关;intBPF_KRETPROBE(uretprobe){func_exit_time=bpf_ktime_get_ns();bpf_printk("function execute time:%lu\n",func_exit_time-func_entry_time);return0;} BPF_KPROBE(uprobe)和BPF_KRETPROBE...
bpf_printk("hello lcc, parent: %d\n", _(parent->tgid)); return 0; } char _license[] SEC("license") = "GPL"; """ class Chello(ClbcBase): def __init__(self): super(Chello, self).__init__("hello", bpf_str=bpfPog) ...
bpf_printk("LSM: block bpf() worked"); return -EPERM;} 这里需要注意的一点是,函数最后多了一个 int ret ,这是由于 LSM hook 是以链表的方式管理,ret 用于反馈上一个 BPF 程序处理的结果,如果当前运行的 BPF 为首个运行,这 ret 的值为 0。 修改Makefile 文件: -APPS = minimal minimal_legacy boot...
bpf_printk("BPF triggered from PID %d.\n", pid); return0; } 这个函数在sys_enter_write这个tracepoint上挂一个钩子,这个钩子会检查执行到这个tracepoint的进程的pid,如果等于当前进程的pid,那么执行输出,对应的用户态程序是minimal.c,源码如下:
调试输出(bpf_trace_printk()) 按事件输出(bpf_perf_event_output()) 基础变量(全局的和每个线程的变量,基于 BPF 映射) 关联数组(通过 BPF 映射) 频率计数(基于 BPF 映射) 柱状图(2 的冥次方、线性及自定义,基于 BPF 映射) 时间戳和时间增量(bpf_ktime_get_ns(),和 BPF 程序) ...
bpf_printk("hello lcc, parent: %d\n", _(parent->tgid)); return 0; } char _license[] SEC("license") = "GPL"; """ class Chello(ClbcBase): def __init__(self): super(Chello, self).__init__("hello", bpf_str=bpfPog) ...
可以看到,上面调用bpf_get_current_pid_tgid和bpf_printk函数的地方反汇编后分别变成了call 14和call 6,这里的数字是哪里来的呢? 这里的数字来自.output/bpf/bpf_helper_defs.h: bpf_helper_defs.h部分源码 在libbpf项目中的sync-kernel.sh提供了这个文件的生成命令: ...