From 0de3e96e333090548a43e5e870c4cb8941d6baf1 Mon Sep 17 00:00:00 2001 From: 0xb10c Date: Tue, 20 Dec 2022 12:51:13 +0100 Subject: [PATCH] tracing: use bitcoind pid in bcc tracing examples BCC needs the PID of a bitcoind process to attach to the tracepoints (instead of the binary path used before) when the tracepoints have a semaphore. For reference, we already use the PID in our tracepoint interface tests. See 220a5a2841172a07d6d7849596316f0e0933e272. --- contrib/tracing/README.md | 8 ++++---- contrib/tracing/log_raw_p2p_msgs.py | 13 +++++++------ contrib/tracing/log_utxocache_flush.py | 13 +++++++------ contrib/tracing/mempool_monitor.py | 11 ++++++----- contrib/tracing/p2p_monitor.py | 22 ++++++++++++++-------- 5 files changed, 38 insertions(+), 29 deletions(-) diff --git a/contrib/tracing/README.md b/contrib/tracing/README.md index c471770a7d7..cf59d7e2bbd 100644 --- a/contrib/tracing/README.md +++ b/contrib/tracing/README.md @@ -82,7 +82,7 @@ about the connection. Peers can be selected individually to view recent P2P messages. ``` -$ python3 contrib/tracing/p2p_monitor.py ./build/src/bitcoind +$ python3 contrib/tracing/p2p_monitor.py $(pidof bitcoind) ``` Lists selectable peers and traffic and connection information. @@ -150,7 +150,7 @@ lost. BCC prints: `Possibly lost 2 samples` on lost messages. ``` -$ python3 contrib/tracing/log_raw_p2p_msgs.py ./build/src/bitcoind +$ python3 contrib/tracing/log_raw_p2p_msgs.py $(pidof bitcoind) ``` ``` @@ -241,7 +241,7 @@ A BCC Python script to log the UTXO cache flushes. Based on the `utxocache:flush` tracepoint. ```bash -$ python3 contrib/tracing/log_utxocache_flush.py ./build/src/bitcoind +$ python3 contrib/tracing/log_utxocache_flush.py $(pidof bitcoind) ``` ``` @@ -300,7 +300,7 @@ comprising a timestamp along with all event data available via the event's tracepoint. ```console -$ python3 contrib/tracing/mempool_monitor.py ./build/src/bitcoind +$ python3 contrib/tracing/mempool_monitor.py $(pidof bitcoind) ``` ``` diff --git a/contrib/tracing/log_raw_p2p_msgs.py b/contrib/tracing/log_raw_p2p_msgs.py index c0ab7041062..bd51c2eb7d9 100755 --- a/contrib/tracing/log_raw_p2p_msgs.py +++ b/contrib/tracing/log_raw_p2p_msgs.py @@ -132,8 +132,9 @@ def print_message(event, inbound): ) -def main(bitcoind_path): - bitcoind_with_usdts = USDT(path=str(bitcoind_path)) +def main(pid): + print(f"Hooking into bitcoind with pid {pid}") + bitcoind_with_usdts = USDT(pid=int(pid)) # attaching the trace functions defined in the BPF program to the tracepoints bitcoind_with_usdts.enable_probe( @@ -176,8 +177,8 @@ def main(bitcoind_path): if __name__ == "__main__": - if len(sys.argv) < 2: - print("USAGE:", sys.argv[0], "path/to/bitcoind") + if len(sys.argv) != 2: + print("USAGE:", sys.argv[0], "") exit() - path = sys.argv[1] - main(path) + pid = sys.argv[1] + main(pid) diff --git a/contrib/tracing/log_utxocache_flush.py b/contrib/tracing/log_utxocache_flush.py index 6c568998e99..8ff9cd5e03a 100755 --- a/contrib/tracing/log_utxocache_flush.py +++ b/contrib/tracing/log_utxocache_flush.py @@ -70,8 +70,9 @@ def print_event(event): )) -def main(bitcoind_path): - bitcoind_with_usdts = USDT(path=str(bitcoind_path)) +def main(pid): + print(f"Hooking into bitcoind with pid {pid}") + bitcoind_with_usdts = USDT(pid=int(pid)) # attaching the trace functions defined in the BPF program # to the tracepoints @@ -99,9 +100,9 @@ def main(bitcoind_path): if __name__ == "__main__": - if len(sys.argv) < 2: - print("USAGE: ", sys.argv[0], "path/to/bitcoind") + if len(sys.argv) != 2: + print("USAGE: ", sys.argv[0], "") exit(1) - path = sys.argv[1] - main(path) + pid = sys.argv[1] + main(pid) diff --git a/contrib/tracing/mempool_monitor.py b/contrib/tracing/mempool_monitor.py index afb5e603722..4492cb75708 100755 --- a/contrib/tracing/mempool_monitor.py +++ b/contrib/tracing/mempool_monitor.py @@ -114,8 +114,9 @@ int trace_replaced(struct pt_regs *ctx) { """ -def main(bitcoind_path): - bitcoind_with_usdts = USDT(path=str(bitcoind_path)) +def main(pid): + print(f"Hooking into bitcoind with pid {pid}") + bitcoind_with_usdts = USDT(pid=int(pid)) # attaching the trace functions defined in the BPF program # to the tracepoints @@ -365,8 +366,8 @@ class Dashboard: if __name__ == "__main__": if len(sys.argv) < 2: - print("USAGE: ", sys.argv[0], "path/to/bitcoind") + print("USAGE: ", sys.argv[0], "") exit(1) - path = sys.argv[1] - main(path) + pid = sys.argv[1] + main(pid) diff --git a/contrib/tracing/p2p_monitor.py b/contrib/tracing/p2p_monitor.py index 4ff701cac3c..63a27fc17da 100755 --- a/contrib/tracing/p2p_monitor.py +++ b/contrib/tracing/p2p_monitor.py @@ -14,8 +14,9 @@ # outbound P2P messages. The eBPF program submits the P2P messages to # this script via a BPF ring buffer. -import sys import curses +import os +import sys from curses import wrapper, panel from bcc import BPF, USDT @@ -115,10 +116,10 @@ class Peer: self.total_outbound_msgs += 1 -def main(bitcoind_path): +def main(pid): peers = dict() - - bitcoind_with_usdts = USDT(path=str(bitcoind_path)) + print(f"Hooking into bitcoind with pid {pid}") + bitcoind_with_usdts = USDT(pid=int(pid)) # attaching the trace functions defined in the BPF program to the tracepoints bitcoind_with_usdts.enable_probe( @@ -245,9 +246,14 @@ def render(screen, peers, cur_list_pos, scroll, ROWS_AVALIABLE_FOR_LIST, info_pa (msg.msg_type, msg.size), curses.A_NORMAL) +def running_as_root(): + return os.getuid() == 0 + if __name__ == "__main__": - if len(sys.argv) < 2: - print("USAGE:", sys.argv[0], "path/to/bitcoind") + if len(sys.argv) != 2: + print("USAGE:", sys.argv[0], "") exit() - path = sys.argv[1] - main(path) + if not running_as_root(): + print("You might not have the privileges required to hook into the tracepoints!") + pid = sys.argv[1] + main(pid)