Skip to content

Commit

Permalink
tracing: use bitcoind pid in bcc tracing examples
Browse files Browse the repository at this point in the history
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 220a5a2.
  • Loading branch information
0xB10C committed Oct 28, 2024
1 parent 411c6cf commit 0de3e96
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 29 deletions.
8 changes: 4 additions & 4 deletions contrib/tracing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
```

```
Expand Down Expand Up @@ -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)
```

```
Expand Down Expand Up @@ -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)
```

```
Expand Down
13 changes: 7 additions & 6 deletions contrib/tracing/log_raw_p2p_msgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -176,8 +177,8 @@ def handle_outbound(_, data, size):


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], "<pid of bitcoind>")
exit()
path = sys.argv[1]
main(path)
pid = sys.argv[1]
main(pid)
13 changes: 7 additions & 6 deletions contrib/tracing/log_utxocache_flush.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -99,9 +100,9 @@ def handle_flush(_, data, size):


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], "<pid of bitcoind>")
exit(1)

path = sys.argv[1]
main(path)
pid = sys.argv[1]
main(pid)
11 changes: 6 additions & 5 deletions contrib/tracing/mempool_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@
"""


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
Expand Down Expand Up @@ -365,8 +366,8 @@ def timestamp_age(timestamp):

if __name__ == "__main__":
if len(sys.argv) < 2:
print("USAGE: ", sys.argv[0], "path/to/bitcoind")
print("USAGE: ", sys.argv[0], "<pid of bitcoind>")
exit(1)

path = sys.argv[1]
main(path)
pid = sys.argv[1]
main(pid)
22 changes: 14 additions & 8 deletions contrib/tracing/p2p_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -115,10 +116,10 @@ def add_message(self, message):
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(
Expand Down Expand Up @@ -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], "<pid of bitcoind>")
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)

0 comments on commit 0de3e96

Please sign in to comment.