Skip to content

Commit 6364a28

Browse files
committed
docs: update README files to streamline eunomia-bpf installation instructions
1 parent c037dbf commit 6364a28

File tree

5 files changed

+25
-16
lines changed

5 files changed

+25
-16
lines changed

src/1-helloworld/README.zh.md

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ eBPF 程序主要由两部分构成:内核态部分和用户态部分。内核
3333
7. 卸载eBPF程序:当不再需要eBPF程序时,用户态程序应使用BCC API将其从内核空间卸载。
3434
8. 调试与优化:使用 bpftool 等工具进行eBPF程序的调试和优化,提高程序性能和稳定性。
3535

36-
通过以上流程,您可以使用BCC工具开发、编译、运行和调试eBPF程序。请注意,其他框架(如libbpf、cilium/ebpf和eunomia-bpf)的开发流程大致相似但略有不同,因此在选择框架时,请参考相应的官方文档和示例。
36+
通过以上流程,您可以使用开发、编译、运行和调试eBPF程序。请注意,其他框架(如libbpf、cilium/ebpf和eunomia-bpf)的开发流程大致相似但略有不同,因此在选择框架时,请参考相应的官方文档和示例。
3737

38-
通过这个过程,你可以开发出一个能够在内核中运行的 eBPF 程序。eunomia-bpf 是一个开源的 eBPF 动态加载运行时和开发工具链,它的目的是简化 eBPF 程序的开发、构建、分发、运行。它基于 libbpf 的 CO-RE 轻量级开发框架,支持通过用户态 WASM 虚拟机控制 eBPF 程序的加载和执行,并将预编译的 eBPF 程序打包为通用的 JSON 或 WASM 模块进行分发。我们会使用 eunomia-bpf 进行演示
38+
我们使用 eunomia-bpf 来编译和运行这个示例。你可以从 <https://github.com/eunomia-bpf/eunomia-bpf> 安装它
3939

4040
## 下载安装 eunomia-bpf 开发工具
4141

@@ -97,11 +97,15 @@ int handle_tp(void *ctx)
9797
}
9898
```
9999
100-
这段程序通过定义一个 handle_tp 函数并使用 SEC 宏把它附加到 sys_enter_write tracepoint(即在进入 write 系统调用时执行)。该函数通过使用 bpf_get_current_pid_tgid 和 bpf_printk 函数获取调用 write 系统调用的进程 ID,并在内核日志中打印出来
100+
这个程序定义了一个 handle_tp 函数,通过 SEC 宏将其附加到 sys_enter_write 追踪点。每当进入 write 系统调用时,这个函数就会被执行。它获取当前进程的 ID 并使用 `bpf_printk` 打印到内核日志
101101
102-
- `bpf_printk()`: 一种将信息输出到trace_pipe(/sys/kernel/debug/tracing/trace_pipe)简单机制。 在一些简单用例中这样使用没有问题, but它也有一些限制:最多3 参数; 第一个参数必须是%s(即字符串);同时trace_pipe在内核中全局共享,其他并行使用trace_pipe的程序有可能会将 trace_pipe 的输出扰乱。 一个更好的方式是通过 BPF_PERF_OUTPUT(), 稍后将会讲到。
103-
- `void *ctx`:ctx本来是具体类型的参数, 但是由于我们这里没有使用这个参数,因此就将其写成void *类型。
104-
- `return 0`;:必须这样,返回0 (如果要知道why, 参考 #139 <https://github.com/iovisor/bcc/issues/139>)。
102+
`SEC("tp/syscalls/sys_enter_write")` 宏告诉 eBPF 加载器将函数附加到哪里。格式是 `tp` 表示追踪点,`syscalls` 是子系统,`sys_enter_write` 是具体的事件名称。你可以通过运行 `sudo ls /sys/kernel/debug/tracing/events/syscalls/` 来查看系统中可用的追踪点。
103+
104+
代码开头的 `BPF_NO_GLOBAL_DATA` 宏是为了兼容旧版内核(5.2 之前)。如果你使用的是现代内核(5.15+),这个定义并非必需,但为了可移植性保留它也无妨。
105+
106+
`bpf_printk()` 函数是你调试的好帮手。它将输出发送到 `/sys/kernel/debug/tracing/trace_pipe`,这是查看 eBPF 程序运行情况的简单方式。但它有一些限制:最多 3 个参数,trace_pipe 在内核中全局共享,并且对高频事件可能影响性能。在生产环境中,你应该使用环形缓冲区或性能事件数组,这些我们会在后续教程中介绍。
107+
108+
`ctx` 参数包含追踪点特定的数据,但在这个简单示例中我们不需要它,所以声明为 `void *`。在更高级的程序中,你可以将其转换为适当的类型来访问参数。eBPF 程序必须返回一个整数——对于追踪点来说返回 0 是标准做法。
105109
106110
要编译和运行这段程序,可以使用 ecc 工具和 ecli 命令。首先在 Ubuntu/Debian 上,执行以下命令:
107111
@@ -140,13 +144,14 @@ $ sudo cat /sys/kernel/debug/tracing/trace_pipe | grep "BPF triggered sys_enter_
140144

141145
按 Ctrl+C 停止 ecli 进程之后,可以看到对应的输出也停止。
142146

143-
注意:如果正在使用的 Linux 发行版(例如 Ubuntu )默认情况下没有启用跟踪子系统可能看不到任何输出,使用以下指令打开这个功能
147+
如果看不到任何输出,可能是追踪子系统没有启用,在某些 Linux 发行版上很常见。你可以通过以下命令启用它
144148

145149
```console
146-
$ sudo su
147-
# echo 1 > /sys/kernel/debug/tracing/tracing_on
150+
$ sudo sh -c 'echo 1 > /sys/kernel/debug/tracing/tracing_on'
148151
```
149152

153+
如果仍然看不到输出,确保程序已加载并运行(ecli 终端应该显示 "Running eBPF program...")。尝试在另一个终端中手动触发一些 write 系统调用,例如运行 `echo "test" > /tmp/test.txt`。你也可以通过运行 `sudo bpftool prog list` 来检查你的 eBPF 程序是否已加载。
154+
150155
## eBPF 程序的基本框架
151156

152157
如上所述, eBPF 程序的基本框架包括:
@@ -190,7 +195,7 @@ eBPF 程序的开发和使用流程可以概括为如下几个步骤:
190195
- 使用 eBPF 程序:这包括监测 eBPF 程序的运行情况,并使用 eBPF 内核映射和共享内存进行数据交换和共享。
191196
- 在实际开发中,还可能需要进行其他的步骤,例如配置编译和加载参数,管理 eBPF 内核模块和内核映射,以及使用其他高级功能等。
192197

193-
需要注意的是,BPF 程序的执行是在内核空间进行的,因此需要使用特殊的工具和技术来编写、编译和调试 BPF 程序。eunomia-bpf 是一个开源的 BPF 编译器和工具包,它可以帮助开发者快速和简单地编写和运行 BPF 程序。
198+
需要注意的是,BPF 程序的执行是在内核空间进行的,因此需要使用特殊的工具和技术来编写、编译和调试 BPF 程序。
194199

195200
您还可以访问我们的教程代码仓库 <https://github.com/eunomia-bpf/bpf-developer-tutorial> 以获取更多示例和完整的教程,全部内容均已开源。我们会继续分享更多有关 eBPF 开发实践的内容,帮助您更好地理解和掌握 eBPF 技术。
196201

src/2-kprobe-unlink/README.zh.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ int BPF_KRETPROBE(do_unlinkat_exit, long ret)
7575
char LICENSE[] SEC("license") = "Dual BSD/GPL";
7676
```
7777

78-
接下来,我们定义一个名为`BPF_KPROBE(do_unlinkat)` kprobe,当进入`do_unlinkat`函数时,它会被触发。该函数接受两个参数:`dfd`(文件描述符)和`name`文件名结构体指针)。在这个 kprobe 中,我们获取当前进程的 PID(进程标识符),然后读取文件名。最后,我们使用`bpf_printk`函数在内核日志中打印 PID 和文件名
78+
第一个 kprobe 挂钩到 `do_unlinkat` 的入口点。`BPF_KPROBE` 宏让我们可以轻松访问函数的参数——在这个例子中是 `dfd`(文件描述符)和 `name`指向文件名结构的指针)。我们获取当前进程 ID,然后使用 `BPF_CORE_READ` 从内核内存中安全地读取文件名
7979

8080
```c
8181
SEC("kprobe/do_unlinkat")
@@ -91,7 +91,9 @@ int BPF_KPROBE(do_unlinkat, int dfd, struct filename *name)
9191
}
9292
```
9393
94-
接下来,我们定义一个名为`BPF_KRETPROBE(do_unlinkat_exit)`的 kretprobe,当从`do_unlinkat`函数退出时,它会被触发。这个 kretprobe 的目的是捕获函数的返回值(ret)。我们再次获取当前进程的 PID,并使用`bpf_printk`函数在内核日志中打印 PID 和返回值。
94+
你可能想知道为什么不能直接访问 `name->name`。原因是 eBPF 程序运行在受限环境中,需要特殊的辅助函数来安全地读取内核内存。`BPF_CORE_READ` 不仅安全地处理这个问题,还提供了 CO-RE(一次编译 - 到处运行)支持,这意味着你的程序可以在不同的内核版本上工作,即使结构体布局发生了变化。
95+
96+
kretprobe 是对应的部分,在函数返回时触发。这里我们可以捕获返回值来查看 unlink 操作是否成功。返回值为 0 表示成功,负值表示错误。
9597
9698
```c
9799
SEC("kretprobe/do_unlinkat")
@@ -105,7 +107,9 @@ int BPF_KRETPROBE(do_unlinkat_exit, long ret)
105107
}
106108
```
107109

108-
eunomia-bpf 是一个结合 Wasm 的开源 eBPF 动态加载运行时和开发工具链,它的目的是简化 eBPF 程序的开发、构建、分发、运行。可以参考 <https://github.com/eunomia-bpf/eunomia-bpf> 下载和安装 ecc 编译工具链和 ecli 运行时。
110+
通过结合 kprobe 和 kretprobe,你可以获得完整的画面:可以看到正在删除什么文件以及操作是否成功。这种模式对于调试、安全监控或构建可观测性工具非常有用。
111+
112+
我们使用 eunomia-bpf 来编译和运行这个示例。你可以从 <https://github.com/eunomia-bpf/eunomia-bpf> 安装它。
109113

110114
要编译这个程序,请使用 ecc 工具:
111115

src/25-signal/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ In summary, this eBPF program provides a method that allows system administrator
154154

155155
## Compilation and Execution
156156

157-
eunomia-bpf is an open-source eBPF dynamic loading runtime and development toolchain combined with Wasm. Its purpose is to simplify the development, building, distribution, and execution of eBPF programs. You can refer to <https://github.com/eunomia-bpf/eunomia-bpf> to download and install the `ecc` compiler toolchain and `ecli` runtime. We use eunomia-bpf to compile and run this example.
157+
We use eunomia-bpf to compile and run this example. You can install it from <https://github.com/eunomia-bpf/eunomia-bpf>.
158158

159159
Compilation:
160160

src/3-fentry-unlink/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ The fexit probe is even more interesting. It gets triggered when the function re
5252
5353
The `BPF_PROG` macro is similar to `BPF_KPROBE` from the previous tutorial, but it's designed for fentry/fexit. It handles the parameter unwrapping automatically so you can focus on your logic.
5454
55-
Through this example, you can learn how to use fentry and fexit probes in eBPF to monitor and capture kernel function calls, such as the unlink system call in this tutorial. "eunomia-bpf is an open source eBPF dynamic loading runtime and development toolchain combined with Wasm. Its goal is to simplify the development, building, distribution, and running of eBPF programs. You can refer to [here](https://github.com/eunomia-bpf/eunomia-bpf) to download and install the ecc compilation toolchain and ecli runtime. We use eunomia-bpf to compile and run this example.
55+
We use eunomia-bpf to compile and run this example. You can install it from <https://github.com/eunomia-bpf/eunomia-bpf>.
5656
5757
To compile and run the above code:
5858

src/34-syscall/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Analysis of the kernel code:
4949
- We create an `args_t` structure to store the file name and flags.
5050
- We use `bpf_probe_write_user` to modify the file name in the user space memory to "hijacked".
5151
52-
The `eunomia-bpf` is an open-source eBPF dynamic loading runtime and development toolchain aimed at making eBPF program development, building, distribution, and execution easier. You can refer to <https://github.com/eunomia-bpf/eunomia-bpf> or <https://eunomia.dev/tutorials/1-helloworld/> for installing ecc compiler toolchain and ecli runtime. We will use `eunomia-bpf` to compile and run this example.
52+
We use eunomia-bpf to compile and run this example. You can install it from <https://github.com/eunomia-bpf/eunomia-bpf>.
5353
5454
Compile the code:
5555

0 commit comments

Comments
 (0)