Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # tcpaccept Trace TCP accept()s. |
| 4 | # For Linux, uses BCC, eBPF. Embedded C. |
| 5 | # |
| 6 | # USAGE: tcpaccept [-h] [-t] [-p PID] |
| 7 | # |
| 8 | # This uses dynamic tracing of the kernel inet_csk_accept() socket function |
| 9 | # (from tcp_prot.accept), and will need to be modified to match kernel changes. |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 10 | # |
| 11 | # IPv4 addresses are printed as dotted quads. For IPv6 addresses, the last four |
| 12 | # bytes are printed after "..."; check for future versions with better IPv6 |
| 13 | # support. |
| 14 | # |
| 15 | # Copyright (c) 2015 Brendan Gregg. |
| 16 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 17 | # |
| 18 | # 13-Oct-2015 Brendan Gregg Created this. |
| 19 | |
| 20 | from __future__ import print_function |
| 21 | from bcc import BPF |
| 22 | import argparse |
| 23 | |
| 24 | # arguments |
| 25 | examples = """examples: |
Brendan Gregg | 000a4e6 | 2015-10-13 15:41:46 -0700 | [diff] [blame] | 26 | ./tcpaccept # trace all TCP accept()s |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 27 | ./tcpaccept -t # include timestamps |
| 28 | ./tcpaccept -p 181 # only trace PID 181 |
| 29 | """ |
| 30 | parser = argparse.ArgumentParser( |
| 31 | description="Trace TCP accepts", |
| 32 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 33 | epilog=examples) |
| 34 | parser.add_argument("-t", "--timestamp", action="store_true", |
| 35 | help="include timestamp on output") |
| 36 | parser.add_argument("-p", "--pid", |
| 37 | help="trace this PID only") |
| 38 | args = parser.parse_args() |
| 39 | debug = 0 |
| 40 | |
| 41 | # define BPF program |
| 42 | bpf_text = """ |
| 43 | #include <uapi/linux/ptrace.h> |
| 44 | #include <net/sock.h> |
| 45 | #include <bcc/proto.h> |
| 46 | |
| 47 | int kretprobe__inet_csk_accept(struct pt_regs *ctx) |
| 48 | { |
| 49 | struct sock *newsk = (struct sock *)ctx->ax; |
| 50 | u32 pid = bpf_get_current_pid_tgid(); |
| 51 | |
| 52 | if (newsk == NULL) |
| 53 | return 0; |
| 54 | |
Brendan Gregg | 10e1b14 | 2015-10-13 16:35:25 -0700 | [diff] [blame^] | 55 | // check this is TCP |
| 56 | u8 protocol = 0; |
| 57 | // workaround for reading the sk_protocol bitfield: |
| 58 | bpf_probe_read(&protocol, 1, (void *)((long)&newsk->sk_wmem_queued) - 3); |
| 59 | if (protocol != IPPROTO_TCP) |
| 60 | return 0; |
| 61 | |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 62 | // pull in details |
| 63 | u16 family = 0, lport = 0; |
| 64 | u32 saddr = 0, daddr = 0; |
| 65 | bpf_probe_read(&family, sizeof(family), &newsk->__sk_common.skc_family); |
| 66 | bpf_probe_read(&lport, sizeof(lport), &newsk->__sk_common.skc_num); |
| 67 | if (family == AF_INET) { |
| 68 | bpf_probe_read(&saddr, sizeof(saddr), |
| 69 | &newsk->__sk_common.skc_rcv_saddr); |
| 70 | bpf_probe_read(&daddr, sizeof(daddr), |
| 71 | &newsk->__sk_common.skc_daddr); |
| 72 | |
| 73 | // output |
| 74 | bpf_trace_printk("4 %x %x %d\\n", daddr, saddr, lport); |
| 75 | } else if (family == AF_INET6) { |
| 76 | // just grab the last 4 bytes for now |
| 77 | bpf_probe_read(&saddr, sizeof(saddr), |
| 78 | &newsk->__sk_common.skc_v6_rcv_saddr.in6_u.u6_addr32[3]); |
| 79 | bpf_probe_read(&daddr, sizeof(daddr), |
| 80 | &newsk->__sk_common.skc_v6_daddr.in6_u.u6_addr32[3]); |
| 81 | |
| 82 | // output and flip byte order of addresses |
| 83 | bpf_trace_printk("6 %x %x %d\\n", bpf_ntohl(daddr), |
| 84 | bpf_ntohl(saddr), lport); |
| 85 | } |
| 86 | // else drop |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | """ |
| 91 | |
| 92 | # code substitutions |
| 93 | if args.pid: |
| 94 | bpf_text = bpf_text.replace('FILTER', |
| 95 | 'if (pid != %s) { return 0; }' % args.pid) |
| 96 | else: |
| 97 | bpf_text = bpf_text.replace('FILTER', '') |
| 98 | if debug: |
| 99 | print(bpf_text) |
| 100 | |
| 101 | # initialize BPF |
| 102 | b = BPF(text=bpf_text) |
| 103 | |
| 104 | # header |
| 105 | if args.timestamp: |
| 106 | print("%-9s" % ("TIME(s)"), end="") |
| 107 | print("%-6s %-12s %-2s %-16s %-16s %-4s" % ("PID", "COMM", "IP", "RADDR", |
| 108 | "LADDR", "LPORT")) |
| 109 | |
| 110 | start_ts = 0 |
| 111 | |
| 112 | def inet_ntoa(addr): |
| 113 | dq = '' |
| 114 | for i in range(0, 4): |
| 115 | dq = dq + str(addr & 0xff) |
| 116 | if (i != 3): |
| 117 | dq = dq + '.' |
| 118 | addr = addr >> 8 |
| 119 | return dq |
| 120 | |
| 121 | # format output |
| 122 | while 1: |
| 123 | (task, pid, cpu, flags, ts, msg) = b.trace_fields() |
| 124 | (ip_s, raddr_hs, laddr_hs, lport_s) = msg.split(" ") |
| 125 | |
| 126 | if args.timestamp: |
| 127 | if start_ts == 0: |
| 128 | start_ts = ts |
| 129 | print("%-9.3f" % (ts - start_ts), end="") |
| 130 | print("%-6d %-12.12s %-2s %-16s %-16s %-4s" % (pid, task, ip_s, |
| 131 | inet_ntoa(int(raddr_hs, 16)) if ip_s == "4" else "..." + raddr_hs, |
| 132 | inet_ntoa(int(laddr_hs, 16)) if ip_s == "4" else "..." + laddr_hs, |
| 133 | lport_s)) |