nettrack (707B)
1 #!/usr/bin/env bpftrace 2 #include <net/sock.h> 3 4 BEGIN 5 { 6 printf("Tracing per-PID, per-thread network traffic. Ctrl-C to stop\n"); 7 } 8 9 kprobe:sock_recvmsg, 10 kprobe:sock_sendmsg 11 { 12 $sock = (struct socket *)arg0; 13 $family = $sock->sk->__sk_common.skc_family; 14 if ($family == AF_INET || $family == AF_INET6) { 15 @inetsocket[tid] = 1; 16 } else { 17 @inetsocket[tid] = 0; 18 } 19 } 20 21 kretprobe:sock_recvmsg 22 { 23 if (@inetsocket[tid] && retval < 0x7fffffff) { 24 @recv_bytes[pid, comm] = sum(retval); 25 } 26 delete(@inetsocket[tid]) 27 } 28 29 kretprobe:sock_sendmsg 30 { 31 if (@inetsocket[tid] && retval < 0x7fffffff) { 32 @send_bytes[pid, comm] = sum(retval); 33 } 34 delete(@inetsocket[tid]) 35 } 36 37 END 38 { 39 clear(@inetsocket); 40 }