You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用eBPF追踪指定PID的accept/close系统调用并计算耗时

问题:eBPF追踪特定进程accept到close耗时无响应

我需要测量特定进程从调用accept系统调用接受连接到调用close关闭连接的墙钟耗时,计划通过eBPF实现对指定PID的这两个系统调用的追踪并计算时间差,但运行后无任何响应,尝试多个kprobe追踪点仍未解决问题,相关代码及尝试的追踪点如下:

BPF代码

#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>
// #include "net/sock.h"
char LICENSE[] SEC("license") = "Dual BSD/GPL";

struct {
    __uint(type, BPF_MAP_TYPE_HASH);
    __uint(max_entries, 8192);
    __type(key, int);
    __type(value, int);
} pid_map SEC(".maps");

u64 get_current_time(){
    u64 ts = bpf_ktime_get_ns();
    return ts;
}

SEC("kprobe/tcp_v4_do_rcv")
int bpf_prog(struct pt_regs *ctx)
{
    int pid = bpf_get_current_pid_tgid() >> 32;
    u64 start_time = get_current_time();
    bpf_printk("BPF triggered from PID %d.\n", pid);
    bpf_map_update_elem(&pid_map, &pid,&start_time, BPF_ANY);
    return 0;
}

SEC("kprobe/tcp_close")
int bpf_prog2(struct pt_regs *ctx)
{
    int pid = bpf_get_current_pid_tgid() >> 32;
    u64 *pid_ptr = bpf_map_lookup_elem(&pid_map, &pid);
    
    if (pid_ptr != NULL && *pid_ptr == pid && *pid_ptr>100ULL) {
        bpf_printk("Process closed connection %d\n: ",pid);
    }
    return 0;
}

Python测试脚本

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.connect(('www.google.com', 80))

s.send(b"GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n")

response = s.recv(1024)
print(response)

s.close()

尝试过的其他kprobe追踪点

  • accept相关:kprobe/__x64_sys_accept、kprobe/inet_csk_accept
  • close相关:kprobe/tcp_v6_destroy_sock、kprobe/tcp_v4_destroy_sock

内容的提问来源于stack exchange,提问作者Ajith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 16:30:44