Skip to content

Commit 0de3e96

Browse files
committed
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 220a5a2.
1 parent 411c6cf commit 0de3e96

File tree

5 files changed

+38
-29
lines changed

5 files changed

+38
-29
lines changed

contrib/tracing/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ about the connection. Peers can be selected individually to view recent P2P
8282
messages.
8383

8484
```
85-
$ python3 contrib/tracing/p2p_monitor.py ./build/src/bitcoind
85+
$ python3 contrib/tracing/p2p_monitor.py $(pidof bitcoind)
8686
```
8787

8888
Lists selectable peers and traffic and connection information.
@@ -150,7 +150,7 @@ lost. BCC prints: `Possibly lost 2 samples` on lost messages.
150150

151151

152152
```
153-
$ python3 contrib/tracing/log_raw_p2p_msgs.py ./build/src/bitcoind
153+
$ python3 contrib/tracing/log_raw_p2p_msgs.py $(pidof bitcoind)
154154
```
155155

156156
```
@@ -241,7 +241,7 @@ A BCC Python script to log the UTXO cache flushes. Based on the
241241
`utxocache:flush` tracepoint.
242242

243243
```bash
244-
$ python3 contrib/tracing/log_utxocache_flush.py ./build/src/bitcoind
244+
$ python3 contrib/tracing/log_utxocache_flush.py $(pidof bitcoind)
245245
```
246246

247247
```
@@ -300,7 +300,7 @@ comprising a timestamp along with all event data available via the event's
300300
tracepoint.
301301

302302
```console
303-
$ python3 contrib/tracing/mempool_monitor.py ./build/src/bitcoind
303+
$ python3 contrib/tracing/mempool_monitor.py $(pidof bitcoind)
304304
```
305305

306306
```

contrib/tracing/log_raw_p2p_msgs.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,9 @@ def print_message(event, inbound):
132132
)
133133

134134

135-
def main(bitcoind_path):
136-
bitcoind_with_usdts = USDT(path=str(bitcoind_path))
135+
def main(pid):
136+
print(f"Hooking into bitcoind with pid {pid}")
137+
bitcoind_with_usdts = USDT(pid=int(pid))
137138

138139
# attaching the trace functions defined in the BPF program to the tracepoints
139140
bitcoind_with_usdts.enable_probe(
@@ -176,8 +177,8 @@ def handle_outbound(_, data, size):
176177

177178

178179
if __name__ == "__main__":
179-
if len(sys.argv) < 2:
180-
print("USAGE:", sys.argv[0], "path/to/bitcoind")
180+
if len(sys.argv) != 2:
181+
print("USAGE:", sys.argv[0], "<pid of bitcoind>")
181182
exit()
182-
path = sys.argv[1]
183-
main(path)
183+
pid = sys.argv[1]
184+
main(pid)

contrib/tracing/log_utxocache_flush.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ def print_event(event):
7070
))
7171

7272

73-
def main(bitcoind_path):
74-
bitcoind_with_usdts = USDT(path=str(bitcoind_path))
73+
def main(pid):
74+
print(f"Hooking into bitcoind with pid {pid}")
75+
bitcoind_with_usdts = USDT(pid=int(pid))
7576

7677
# attaching the trace functions defined in the BPF program
7778
# to the tracepoints
@@ -99,9 +100,9 @@ def handle_flush(_, data, size):
99100

100101

101102
if __name__ == "__main__":
102-
if len(sys.argv) < 2:
103-
print("USAGE: ", sys.argv[0], "path/to/bitcoind")
103+
if len(sys.argv) != 2:
104+
print("USAGE: ", sys.argv[0], "<pid of bitcoind>")
104105
exit(1)
105106

106-
path = sys.argv[1]
107-
main(path)
107+
pid = sys.argv[1]
108+
main(pid)

contrib/tracing/mempool_monitor.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,9 @@
114114
"""
115115

116116

117-
def main(bitcoind_path):
118-
bitcoind_with_usdts = USDT(path=str(bitcoind_path))
117+
def main(pid):
118+
print(f"Hooking into bitcoind with pid {pid}")
119+
bitcoind_with_usdts = USDT(pid=int(pid))
119120

120121
# attaching the trace functions defined in the BPF program
121122
# to the tracepoints
@@ -365,8 +366,8 @@ def timestamp_age(timestamp):
365366

366367
if __name__ == "__main__":
367368
if len(sys.argv) < 2:
368-
print("USAGE: ", sys.argv[0], "path/to/bitcoind")
369+
print("USAGE: ", sys.argv[0], "<pid of bitcoind>")
369370
exit(1)
370371

371-
path = sys.argv[1]
372-
main(path)
372+
pid = sys.argv[1]
373+
main(pid)

contrib/tracing/p2p_monitor.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
# outbound P2P messages. The eBPF program submits the P2P messages to
1515
# this script via a BPF ring buffer.
1616

17-
import sys
1817
import curses
18+
import os
19+
import sys
1920
from curses import wrapper, panel
2021
from bcc import BPF, USDT
2122

@@ -115,10 +116,10 @@ def add_message(self, message):
115116
self.total_outbound_msgs += 1
116117

117118

118-
def main(bitcoind_path):
119+
def main(pid):
119120
peers = dict()
120-
121-
bitcoind_with_usdts = USDT(path=str(bitcoind_path))
121+
print(f"Hooking into bitcoind with pid {pid}")
122+
bitcoind_with_usdts = USDT(pid=int(pid))
122123

123124
# attaching the trace functions defined in the BPF program to the tracepoints
124125
bitcoind_with_usdts.enable_probe(
@@ -245,9 +246,14 @@ def render(screen, peers, cur_list_pos, scroll, ROWS_AVALIABLE_FOR_LIST, info_pa
245246
(msg.msg_type, msg.size), curses.A_NORMAL)
246247

247248

249+
def running_as_root():
250+
return os.getuid() == 0
251+
248252
if __name__ == "__main__":
249-
if len(sys.argv) < 2:
250-
print("USAGE:", sys.argv[0], "path/to/bitcoind")
253+
if len(sys.argv) != 2:
254+
print("USAGE:", sys.argv[0], "<pid of bitcoind>")
251255
exit()
252-
path = sys.argv[1]
253-
main(path)
256+
if not running_as_root():
257+
print("You might not have the privileges required to hook into the tracepoints!")
258+
pid = sys.argv[1]
259+
main(pid)

0 commit comments

Comments
 (0)