1 Star 1 Fork 0

RobotCT/bcc

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
.github
SPECS
cmake
debian
docker
docs
examples
cgroupid
cpp
local_storage
lua
networking
perf
ringbuf
tracing
CMakeLists.txt
biolatpcts.py
biolatpcts_example.txt
bitehist.py
bitehist_example.txt
dddos.py
dddos_example.txt
disksnoop.py
disksnoop_example.txt
hello_fields.py
hello_perf_output.py
hello_perf_output_using_ns.py
kvm_hypercall.py
kvm_hypercall.txt
mallocstacks.py
mysqld_query.py
mysqld_query_example.txt
nflatency.py
nodejs_http_server.py
nodejs_http_server_example.txt
stack_buildid_example.py
stacksnoop.py
stacksnoop_example.txt
strlen_count.py
strlen_hist.py
strlen_hist_ifunc.py
strlen_snoop.py
sync_timing.py
task_switch.c
task_switch.py
tcpv4connect.py
tcpv4connect_example.txt
trace_fields.py
trace_perf_output.py
undump.py
undump_example.txt
urandomread-explicit.py
urandomread.py
urandomread_example.txt
vfsreadlat.c
vfsreadlat.py
vfsreadlat_example.txt
usdt_sample
CMakeLists.txt
hello_world.py
images
introspection
libbpf-tools
man
scripts
snap
src
tests
tools
.clang-format
.dockerignore
.gitignore
.gitmodules
.travis.yml
CMakeLists.txt
CODEOWNERS
CONTRIBUTING-SCRIPTS.md
FAQ.txt
INSTALL.md
LICENSE.txt
LINKS.md
QUICKSTART.md
README.md
克隆/下载
tcpv4connect.py 2.36 KB
一键复制 编辑 原始数据 按行查看 历史
#!/usr/bin/python
#
# tcpv4connect Trace TCP IPv4 connect()s.
# For Linux, uses BCC, eBPF. Embedded C.
#
# USAGE: tcpv4connect [-h] [-t] [-p PID]
#
# This is provided as a basic example of TCP connection & socket tracing.
#
# All IPv4 connection attempts are traced, even if they ultimately fail.
#
# Copyright (c) 2015 Brendan Gregg.
# Licensed under the Apache License, Version 2.0 (the "License")
#
# 15-Oct-2015 Brendan Gregg Created this.
from __future__ import print_function
from bcc import BPF
from bcc.utils import printb
# define BPF program
bpf_text = """
#include <uapi/linux/ptrace.h>
#include <net/sock.h>
#include <bcc/proto.h>
BPF_HASH(currsock, u32, struct sock *);
int kprobe__tcp_v4_connect(struct pt_regs *ctx, struct sock *sk)
{
u32 pid = bpf_get_current_pid_tgid();
// stash the sock ptr for lookup on return
currsock.update(&pid, &sk);
return 0;
};
int kretprobe__tcp_v4_connect(struct pt_regs *ctx)
{
int ret = PT_REGS_RC(ctx);
u32 pid = bpf_get_current_pid_tgid();
struct sock **skpp;
skpp = currsock.lookup(&pid);
if (skpp == 0) {
return 0; // missed entry
}
if (ret != 0) {
// failed to send SYNC packet, may not have populated
// socket __sk_common.{skc_rcv_saddr, ...}
currsock.delete(&pid);
return 0;
}
// pull in details
struct sock *skp = *skpp;
u32 saddr = skp->__sk_common.skc_rcv_saddr;
u32 daddr = skp->__sk_common.skc_daddr;
u16 dport = skp->__sk_common.skc_dport;
// output
bpf_trace_printk("trace_tcp4connect %x %x %d\\n", saddr, daddr, ntohs(dport));
currsock.delete(&pid);
return 0;
}
"""
# initialize BPF
b = BPF(text=bpf_text)
# header
print("%-6s %-12s %-16s %-16s %-4s" % ("PID", "COMM", "SADDR", "DADDR",
"DPORT"))
def inet_ntoa(addr):
dq = b''
for i in range(0, 4):
dq = dq + str(addr & 0xff).encode()
if (i != 3):
dq = dq + b'.'
addr = addr >> 8
return dq
# filter and format output
while 1:
# Read messages from kernel pipe
try:
(task, pid, cpu, flags, ts, msg) = b.trace_fields()
(_tag, saddr_hs, daddr_hs, dport_s) = msg.split(b" ")
except ValueError:
# Ignore messages from other tracers
continue
except KeyboardInterrupt:
exit()
# Ignore messages from other tracers
if _tag.decode() != "trace_tcp4connect":
continue
printb(b"%-6d %-12.12s %-16s %-16s %-4s" % (pid, task,
inet_ntoa(int(saddr_hs, 16)),
inet_ntoa(int(daddr_hs, 16)),
dport_s))
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/Dmi-compute/bcc.git
git@gitee.com:Dmi-compute/bcc.git
Dmi-compute
bcc
bcc
master

搜索帮助