Skip to content

Commit b19b526

Browse files
committed
tracing: log_p2p_connections.bt example
A bpftrace script that logs information from the net:*_connection tracepoints. I've tested this script with bpftrace version 0.14.1 and v0.20.2.
1 parent caa5486 commit b19b526

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

contrib/tracing/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,4 +335,25 @@ $ python3 contrib/tracing/mempool_monitor.py $(pidof bitcoind)
335335
│ 13:10:32Z added c78e87be86c828137a6e7e00a177c03b52202ce4c39029b99904c2a094b9da87 with feerate 11.00 sat/vB (1562 sat, 142 vbytes) │
336336
│ │
337337
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
338+
339+
### log_p2p_connections.bt
340+
341+
A `bpftrace` script to log information about opened, closed, misbehaving, and
342+
evicted P2P connections. Uses the `net:*_connection` tracepoints.
343+
344+
```bash
345+
$ bpftrace contrib/tracing/log_p2p_connections.bt
346+
```
347+
348+
This should produce an output similar to the following.
349+
350+
```bash
351+
Attaching 6 probes...
352+
Logging opened, closed, misbehaving, and evicted P2P connections
353+
OUTBOUND conn to 127.0.0.1:15287: id=0, type=block-relay-only, network=0, total_out=1
354+
INBOUND conn from 127.0.0.1:45324: id=1, type=inbound, network=0, total_in=1
355+
MISBEHAVING conn id=1, score_before=0, score_increase=20, message='getdata message size = 50001', threshold_exceeded=false
356+
CLOSED conn to 127.0.0.1:15287: id=0, type=block-relay-only, network=0, established=1231006505
357+
EVICTED conn to 127.0.0.1:45324: id=1, type=inbound, network=0, established=1612312312
358+
...
338359
```
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env bpftrace
2+
3+
BEGIN
4+
{
5+
printf("Logging opened, closed, misbehaving, and evicted P2P connections\n")
6+
}
7+
8+
usdt:./build/src/bitcoind:net:inbound_connection
9+
{
10+
$id = (int64) arg0;
11+
$addr = str(arg1);
12+
$conn_type = str(arg2);
13+
$network = (int32) arg3;
14+
$existing = (uint64) arg4;
15+
printf("INBOUND conn from %s: id=%ld, type=%s, network=%d, total=%d\n", $addr, $id, $conn_type, $network, $existing);
16+
}
17+
18+
usdt:./build/src/bitcoind:net:outbound_connection
19+
{
20+
$id = (int64) arg0;
21+
$addr = str(arg1);
22+
$conn_type = str(arg2);
23+
$network = (int32) arg3;
24+
$existing = (uint64) arg4;
25+
printf("OUTBOUND conn to %s: id=%ld, type=%s, network=%d, total=%d\n", $addr, $id, $conn_type, $network, $existing);
26+
}
27+
28+
usdt:./build/src/bitcoind:net:closed_connection
29+
{
30+
$id = (int64) arg0;
31+
$addr = str(arg1);
32+
$conn_type = str(arg2);
33+
$network = (int32) arg3;
34+
printf("CLOSED conn to %s: id=%ld, type=%s, network=%d, established=%ld\n", $addr, $id, $conn_type, $network, arg4);
35+
}
36+
37+
usdt:./build/src/bitcoind:net:evicted_inbound_connection
38+
{
39+
$id = (int64) arg0;
40+
$addr = str(arg1);
41+
$conn_type = str(arg2);
42+
$network = (int32) arg3;
43+
printf("EVICTED conn to %s: id=%ld, type=%s, network=%d, established=%ld\n", $addr, $id, $conn_type, $network, arg4);
44+
}
45+
46+
usdt:./build/src/bitcoind:net:misbehaving_connection
47+
{
48+
$id = (int64) arg0;
49+
$message = str(arg1);
50+
printf("MISBEHAVING conn id=%ld, message='%s'\n", $id, $message);
51+
}

0 commit comments

Comments
 (0)