Skip to content

Instantly share code, notes, and snippets.

@0xB10C
Created December 31, 2020 11:40
Show Gist options
  • Save 0xB10C/03c3451258a323f7a2341d155923496d to your computer and use it in GitHub Desktop.
Save 0xB10C/03c3451258a323f7a2341d155923496d to your computer and use it in GitHub Desktop.
from __future__ import print_function
from bcc import BPF, USDT
import sys
from datetime import datetime
if len(sys.argv) < 2:
print("USAGE:", sys.argv[0], "path/to/bitcoind")
exit()
path = sys.argv[1]
debug = False
bpf_text = """
#include <uapi/linux/ptrace.h>
struct connect_block_t
{
u32 height;
u8 hash[32];
};
BPF_PERF_OUTPUT(connected_blocks);
int do_trace(struct pt_regs *ctx) {
struct connect_block_t cb = {};
bpf_usdt_readarg(1, ctx, &cb.height);
bpf_usdt_readarg_p(2, ctx, &cb.hash, sizeof(cb.hash));
connected_blocks.perf_submit(ctx, &cb, sizeof(cb));
return 0;
};
"""
u = USDT(path=str(path))
u.enable_probe(probe="connect_block", fn_name="do_trace")
if debug:
print(u.get_text())
b = BPF(text=bpf_text, usdt_contexts=[u])
def print_connected_blocks(cpu, data, size):
event = b["connected_blocks"].event(data)
height = event.height
hash_bytes = bytearray(event.hash)
hash_bytes.reverse()
hash = hash_bytes.hex()
print(datetime.now(), "height:", height, "hash:", hash)
b["connected_blocks"].open_perf_buffer(print_connected_blocks)
while 1:
try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()
2020-12-31 12:05:40.634456 height: 1901575 hash: 0000000000000012e5bad3eb72856bc8cee92b4da47c7d0ee49db1463345a1f8
2020-12-31 12:10:11.215210 height: 1901576 hash: 00000000000000107e1cef35dc0557a49f5642b0b9b289ebc1cee454797ed1c3
2020-12-31 12:14:00.349253 height: 1901577 hash: 0000000000000004c7abf45ba560c28e7bfa39c19e28284f8a640e33ee4069d1
2020-12-31 12:17:02.398484 height: 1901578 hash: 00000000000000027b2f88e8cc4e4b0f8a7ad9d0142f282d272827201875e7c4
2020-12-31 12:17:17.796756 height: 1901579 hash: 000000000000001a64e5d01f08c3183165fa2d8225d881081c4b80bb1ffba5b8
2020-12-31 12:18:40.852506 height: 1901580 hash: 0000000000000010da1398d31de9d46eac62caf2a2c4ae1f7b91041ba612a0ae
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment