mirror of
https://github.com/eunomia-bpf/bpf-developer-tutorial.git
synced 2026-02-10 21:55:12 +08:00
fix(execsnoop): get the correct process name (#58)
* fix(execsnoop): get the correct process name * docs(execsnoop): format the code in the document * docs(execsnoop): fix the title
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
# eBPF 入门实践教程七:捕获进程执行/退出时间,通过 perf event array 向用户态打印输出
|
# eBPF 入门实践教程七:捕获进程执行事件,通过 perf event array 向用户态打印输出
|
||||||
|
|
||||||
eBPF (Extended Berkeley Packet Filter) 是 Linux 内核上的一个强大的网络和性能分析工具,它允许开发者在内核运行时动态加载、更新和运行用户定义的代码。
|
eBPF (Extended Berkeley Packet Filter) 是 Linux 内核上的一个强大的网络和性能分析工具,它允许开发者在内核运行时动态加载、更新和运行用户定义的代码。
|
||||||
|
|
||||||
@@ -63,7 +63,8 @@ int tracepoint__syscalls__sys_enter_execve(struct trace_event_raw_sys_enter* ctx
|
|||||||
event.uid = uid;
|
event.uid = uid;
|
||||||
task = (struct task_struct*)bpf_get_current_task();
|
task = (struct task_struct*)bpf_get_current_task();
|
||||||
event.ppid = BPF_CORE_READ(task, real_parent, tgid);
|
event.ppid = BPF_CORE_READ(task, real_parent, tgid);
|
||||||
bpf_get_current_comm(&event.comm, sizeof(event.comm));
|
char *cmd_ptr = (char *) BPF_CORE_READ(ctx, args[0]);
|
||||||
|
bpf_probe_read_str(&event.comm, sizeof(event.comm), cmd_ptr);
|
||||||
bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU, &event, sizeof(event));
|
bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU, &event, sizeof(event));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
# eBPF Beginner's Practical Tutorial Seven: Capturing Process Execution/Exit Time, Printing Output to User Space via perf event array
|
# eBPF Beginner's Practical Tutorial Seven: Capturing Process Execution Event, Printing Output to User Space via perf event array
|
||||||
|
|
||||||
eBPF (Extended Berkeley Packet Filter) is a powerful network and performance analysis tool on the Linux kernel that allows developers to dynamically load, update, and run user-defined code at runtime.
|
eBPF (Extended Berkeley Packet Filter) is a powerful network and performance analysis tool on the Linux kernel that allows developers to dynamically load, update, and run user-defined code at runtime.
|
||||||
|
|
||||||
@@ -63,7 +63,8 @@ int tracepoint_syscalls_sys_enter_execve(struct trace_event_raw_sys_enter* ctx)
|
|||||||
event.uid = uid;
|
event.uid = uid;
|
||||||
task = (struct task_struct*)bpf_get_current_task();
|
task = (struct task_struct*)bpf_get_current_task();
|
||||||
event.ppid = BPF_CORE_READ(task, real_parent, tgid);
|
event.ppid = BPF_CORE_READ(task, real_parent, tgid);
|
||||||
bpf_get_current_comm(&event.comm, sizeof(event.comm));
|
char *cmd_ptr = (char *) BPF_CORE_READ(ctx, args[0]);
|
||||||
|
bpf_probe_read_str(&event.comm, sizeof(event.comm), cmd_ptr);
|
||||||
bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU, &event, sizeof(event));
|
bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU, &event, sizeof(event));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -73,7 +74,7 @@ char LICENSE[] SEC("license") = "GPL";
|
|||||||
|
|
||||||
This code defines an eBPF program for capturing the entry of the `execve` system call.
|
This code defines an eBPF program for capturing the entry of the `execve` system call.
|
||||||
|
|
||||||
In the entry program, we first obtain the process ID and user ID of the current process, then use the `bpf_get_current_task` function to obtain the `task_struct` structure of the current process, and use the `bpf_get_current_comm` function to read the process name. Finally, we use the `bpf_perf_event_output` function to output the process execution event to the perf buffer.
|
In the entry program, we first obtain the process ID and user ID of the current process, then use the `bpf_get_current_task` function to obtain the `task_struct` structure of the current process, and use the `bpf_probe_read_str` function to read the process name. Finally, we use the `bpf_perf_event_output` function to output the process execution event to the perf buffer.
|
||||||
|
|
||||||
With this code, we can capture process execution events in the Linux kernel and analyze the process execution conditions.Instructions: Translate the following Chinese text to English while maintaining the original formatting:
|
With this code, we can capture process execution events in the Linux kernel and analyze the process execution conditions.Instructions: Translate the following Chinese text to English while maintaining the original formatting:
|
||||||
|
|
||||||
|
|||||||
3
src/7-execsnoop/execsnoop.bpf.c
Normal file → Executable file
3
src/7-execsnoop/execsnoop.bpf.c
Normal file → Executable file
@@ -26,7 +26,8 @@ int tracepoint__syscalls__sys_enter_execve(struct trace_event_raw_sys_enter* ctx
|
|||||||
event.uid = uid;
|
event.uid = uid;
|
||||||
task = (struct task_struct*)bpf_get_current_task();
|
task = (struct task_struct*)bpf_get_current_task();
|
||||||
event.ppid = BPF_CORE_READ(task, real_parent, tgid);
|
event.ppid = BPF_CORE_READ(task, real_parent, tgid);
|
||||||
bpf_get_current_comm(&event.comm, sizeof(event.comm));
|
char *cmd_ptr = (char *) BPF_CORE_READ(ctx, args[0]);
|
||||||
|
bpf_probe_read_str(&event.comm, sizeof(event.comm), cmd_ptr);
|
||||||
bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU, &event, sizeof(event));
|
bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU, &event, sizeof(event));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user