From 18832d382c0a8047bf72858b242271ae64fef618 Mon Sep 17 00:00:00 2001 From: saivian1 <1446174581@qq.com> Date: Fri, 9 Dec 2022 17:17:19 +0800 Subject: [PATCH] Update README.md eee --- 8-exitsnoop/README.md | 58 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/8-exitsnoop/README.md b/8-exitsnoop/README.md index 100884f..c2def30 100644 --- a/8-exitsnoop/README.md +++ b/8-exitsnoop/README.md @@ -1,5 +1,63 @@ ## eBPF 入门开发实践指南八:在 eBPF 中使用 fentry 监测捕获 unlink 系统调用: + + + +// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause +/* Copyright (c) 2020 Facebook */ +#include "vmlinux.h" +#include +#include +#include +#include "exitsnoop.h" + +char LICENSE[] SEC("license") = "Dual BSD/GPL"; + +struct { + __uint(type, BPF_MAP_TYPE_RINGBUF); + __uint(max_entries, 256 * 1024); +} rb SEC(".maps"); + +SEC("tp/sched/sched_process_exit") +int handle_exit(struct trace_event_raw_sched_process_template* ctx) +{ + struct task_struct *task; + struct event *e; + pid_t pid, tid; + u64 id, ts, *start_ts, duration_ns = 0; + + /* get PID and TID of exiting thread/process */ + id = bpf_get_current_pid_tgid(); + pid = id >> 32; + tid = (u32)id; + + /* ignore thread exits */ + if (pid != tid) + return 0; + + /* reserve sample from BPF ringbuf */ + e = bpf_ringbuf_reserve(&rb, sizeof(*e), 0); + if (!e) + return 0; + + /* fill out the sample with data */ + task = (struct task_struct *)bpf_get_current_task(); + + e->duration_ns = duration_ns; + e->pid = pid; + e->ppid = BPF_CORE_READ(task, real_parent, tgid); + e->exit_code = (BPF_CORE_READ(task, exit_code) >> 8) & 0xff; + bpf_get_current_comm(&e->comm, sizeof(e->comm)); + + /* send data to user-space for post-processing */ + bpf_ringbuf_submit(e, 0); + return 0; +} + +这段代码是一个 BPF 程序,用于监控 Linux 系统中的进程退出事件。它通过注册一个 tracepoint(“tp/sched/sched_process_exit”)来捕获进程退出事件,并使用 BPF 环形缓冲区来存储捕获的事件数据。 +当系统中发生进程退出事件时,BPF 程序会捕获该事件,并调用“handle_exit”函数来处理它。该函数会检查 + + ## origin origin from: