Skip to content

Commit

Permalink
examples/tracing: Use printb to improve python3 compatibility
Browse files Browse the repository at this point in the history
When using python3, 'print' will output the bytes arrays with the "b"
prefix. Switch to printb to get rid of the prefix.

Signed-off-by: Gary Lin <[email protected]>
  • Loading branch information
lcp committed Feb 27, 2019
1 parent e8ece56 commit bb65bea
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 20 deletions.
9 changes: 5 additions & 4 deletions examples/tracing/disksnoop.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from __future__ import print_function
from bcc import BPF
from bcc.utils import printb

REQ_WRITE = 1 # from include/linux/blk_types.h

Expand Down Expand Up @@ -56,13 +57,13 @@
(bytes_s, bflags_s, us_s) = msg.split()

if int(bflags_s, 16) & REQ_WRITE:
type_s = "W"
type_s = b"W"
elif bytes_s == "0": # see blk_fill_rwbs() for logic
type_s = "M"
type_s = b"M"
else:
type_s = "R"
type_s = b"R"
ms = float(int(us_s, 10)) / 1000

print("%-18.9f %-2s %-7s %8.2f" % (ts, type_s, bytes_s, ms))
printb(b"%-18.9f %-2s %-7s %8.2f" % (ts, type_s, bytes_s, ms))
except KeyboardInterrupt:
exit()
3 changes: 2 additions & 1 deletion examples/tracing/hello_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# This is a Hello World example that formats output as fields.

from bcc import BPF
from bcc.utils import printb

# define BPF program
prog = """
Expand All @@ -25,4 +26,4 @@
(task, pid, cpu, flags, ts, msg) = b.trace_fields()
except ValueError:
continue
print("%-18.9f %-16s %-6d %s" % (ts, task, pid, msg))
printb(b"%-18.9f %-16s %-6d %s" % (ts, task, pid, msg))
5 changes: 3 additions & 2 deletions examples/tracing/hello_perf_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# This is a Hello World example that uses BPF_PERF_OUTPUT.

from bcc import BPF
from bcc.utils import printb

# define BPF program
prog = """
Expand Down Expand Up @@ -44,8 +45,8 @@ def print_event(cpu, data, size):
if start == 0:
start = event.ts
time_s = (float(event.ts - start)) / 1000000000
print("%-18.9f %-16s %-6d %s" % (time_s, event.comm, event.pid,
"Hello, perf_output!"))
printb(b"%-18.9f %-16s %-6d %s" % (time_s, event.comm, event.pid,
b"Hello, perf_output!"))

# loop with callback to print_event
b["events"].open_perf_buffer(print_event)
Expand Down
3 changes: 2 additions & 1 deletion examples/tracing/mallocstacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from __future__ import print_function
from bcc import BPF
from bcc.utils import printb
from time import sleep
import sys

Expand Down Expand Up @@ -56,4 +57,4 @@
for k, v in reversed(sorted(calls.items(), key=lambda c: c[1].value)):
print("%d bytes allocated at:" % v.value)
for addr in stack_traces.walk(k.value):
print("\t%s" % b.sym(addr, pid, show_offset=True))
printb(b"\t%s" % b.sym(addr, pid, show_offset=True))
3 changes: 2 additions & 1 deletion examples/tracing/mysqld_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from __future__ import print_function
from bcc import BPF, USDT
from bcc.utils import printb
import sys

if len(sys.argv) < 2:
Expand Down Expand Up @@ -58,4 +59,4 @@
except ValueError:
print("value error")
continue
print("%-18.9f %-16s %-6d %s" % (ts, task, pid, msg))
printb(b"%-18.9f %-16s %-6d %s" % (ts, task, pid, msg))
3 changes: 2 additions & 1 deletion examples/tracing/nodejs_http_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from __future__ import print_function
from bcc import BPF, USDT
from bcc.utils import printb
import sys

if len(sys.argv) < 2:
Expand Down Expand Up @@ -51,4 +52,4 @@
except ValueError:
print("value error")
continue
print("%-18.9f %-16s %-6d %s" % (ts, task, pid, msg))
printb(b"%-18.9f %-16s %-6d %s" % (ts, task, pid, msg))
3 changes: 2 additions & 1 deletion examples/tracing/strlen_count.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from __future__ import print_function
from bcc import BPF
from bcc.utils import printb
from time import sleep

# load BPF program
Expand Down Expand Up @@ -52,4 +53,4 @@
print("%10s %s" % ("COUNT", "STRING"))
counts = b.get_table("counts")
for k, v in sorted(counts.items(), key=lambda counts: counts[1].value):
print("%10d \"%s\"" % (v.value, k.c.encode('string-escape')))
printb(b"%10d \"%s\"" % (v.value, k.c))
3 changes: 2 additions & 1 deletion examples/tracing/sync_timing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from __future__ import print_function
from bcc import BPF
from bcc.utils import printb

# load BPF program
b = BPF(text="""
Expand Down Expand Up @@ -48,4 +49,4 @@
if start == 0:
start = ts
ts = ts - start
print("At time %.2f s: multiple syncs detected, last %s ms ago" % (ts, ms))
printb(b"At time %.2f s: multiple syncs detected, last %s ms ago" % (ts, ms))
11 changes: 6 additions & 5 deletions examples/tracing/tcpv4connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from __future__ import print_function
from bcc import BPF
from bcc.utils import printb

# define BPF program
bpf_text = """
Expand Down Expand Up @@ -76,11 +77,11 @@
"DPORT"))

def inet_ntoa(addr):
dq = ''
dq = b''
for i in range(0, 4):
dq = dq + str(addr & 0xff)
dq = dq + str(addr & 0xff).encode()
if (i != 3):
dq = dq + '.'
dq = dq + b'.'
addr = addr >> 8
return dq

Expand All @@ -89,7 +90,7 @@ def inet_ntoa(addr):
# Read messages from kernel pipe
try:
(task, pid, cpu, flags, ts, msg) = b.trace_fields()
(_tag, saddr_hs, daddr_hs, dport_s) = msg.split(" ")
(_tag, saddr_hs, daddr_hs, dport_s) = msg.split(b" ")
except ValueError:
# Ignore messages from other tracers
continue
Expand All @@ -98,7 +99,7 @@ def inet_ntoa(addr):
if _tag != "trace_tcp4connect":
continue

print("%-6d %-12.12s %-16s %-16s %-4s" % (pid, task,
printb(b"%-6d %-12.12s %-16s %-16s %-4s" % (pid, task,
inet_ntoa(int(saddr_hs, 16)),
inet_ntoa(int(daddr_hs, 16)),
dport_s))
3 changes: 2 additions & 1 deletion examples/tracing/trace_perf_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import atexit
from bcc import BPF
from bcc.utils import printb
import ctypes as ct

class Data(ct.Structure):
Expand Down Expand Up @@ -50,7 +51,7 @@ def print_counter():
global b
print("counter = %d vs %d" % (counter, b["counters"][ct.c_int(0)].value))

print("Tracing " + event_name + ", try `dd if=/dev/zero of=/dev/null`")
printb(b"Tracing " + event_name + b", try `dd if=/dev/zero of=/dev/null`")
print("Tracing... Hit Ctrl-C to end.")
while 1:
try:
Expand Down
3 changes: 2 additions & 1 deletion examples/tracing/urandomread-explicit.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from __future__ import print_function
from bcc import BPF
from bcc.utils import printb

# define BPF program
bpf_text = """
Expand Down Expand Up @@ -49,4 +50,4 @@
(task, pid, cpu, flags, ts, msg) = b.trace_fields()
except ValueError:
continue
print("%-18.9f %-16s %-6d %s" % (ts, task, pid, msg))
printb(b"%-18.9f %-16s %-6d %s" % (ts, task, pid, msg))
3 changes: 2 additions & 1 deletion examples/tracing/urandomread.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from __future__ import print_function
from bcc import BPF
from bcc.utils import printb

# load BPF program
b = BPF(text="""
Expand All @@ -32,4 +33,4 @@
(task, pid, cpu, flags, ts, msg) = b.trace_fields()
except ValueError:
continue
print("%-18.9f %-16s %-6d %s" % (ts, task, pid, msg))
printb(b"%-18.9f %-16s %-6d %s" % (ts, task, pid, msg))

0 comments on commit bb65bea

Please sign in to comment.