forked from bpftrace/bpftrace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathundump.bt
More file actions
88 lines (81 loc) · 1.74 KB
/
undump.bt
File metadata and controls
88 lines (81 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env bpftrace
// undump Trace unix domain socket package receive.
// For Linux, uses bpftrace and eBPF.
//
// Example of usage:
//
// Terminal 1, UNIX Socket Server:
//
// ```
// $ nc -lU /var/tmp/unixsocket
// # receive from Client
// Hello, world
// 123abc
// ```
//
// Terminal 2, UNIX socket Client:
//
// ```
// $ nc -U /var/tmp/unixsocket
// # Input some lines
// Hello, world
// 123abc
// ```
//
// Terminal 3, receive tracing:
//
// ```
// $ sudo ./undump.bt
// Attaching 3 probes...
// Dump UNIX socket packages RX. Ctrl-C to end
// TIME COMM PID SIZE DATA
// 20:40:11 nc 139071 13 Hello, world\x0a
// 20:40:14 nc 139071 7 123abc\x0a
// ^C
// ```
//
// This is a bpftrace version of the bcc examples/tracing of the same name.
//
// Copyright 2022 CESTC, Inc.
//
// 22-May-2022 Rong Tao Created this.
// 13-Nov-2025 Rong Tao Support dgram packet capture.
#ifndef BPFTRACE_HAVE_BTF
#include <linux/skbuff.h>
#endif
BEGIN
{
printf("Dump UNIX socket packages RX. Ctrl-C to end\n");
printf("%-8s %-16s %-8s %-8s %-s\n", "TIME", "COMM", "PID", "SIZE", "DATA");
}
kprobe:unix_dgram_recvmsg,
kprobe:unix_stream_recvmsg
{
@start_recv[tid] = nsecs;
}
kretprobe:unix_dgram_recvmsg,
kretprobe:unix_stream_recvmsg
{
delete(@start_recv, tid);
}
// Both unix_{dgram,stream}_recvmsg() call skb_copy_datagram_iter
kprobe:skb_copy_datagram_iter
/has_key(@start_recv, tid)/
{
@skbs[tid] = (struct sk_buff *)arg0;
}
kretprobe:skb_copy_datagram_iter
/has_key(@skbs, tid)/
{
if (retval == 0) {
$skb = @skbs[tid];
time("%H:%M:%S ");
printf("%-16s %-8d %-8d %r\n", comm, pid, $skb.len, buf($skb.data, $skb.len));
}
delete(@skbs, tid);
}
END
{
clear(@start_recv);
clear(@skbs);
}