From bb65bea69a6486efd50dd57331eed0f6435b2358 Mon Sep 17 00:00:00 2001 From: Gary Lin Date: Wed, 27 Feb 2019 16:52:35 +0800 Subject: [PATCH] examples/tracing: Use printb to improve python3 compatibility 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 --- examples/tracing/disksnoop.py | 9 +++++---- examples/tracing/hello_fields.py | 3 ++- examples/tracing/hello_perf_output.py | 5 +++-- examples/tracing/mallocstacks.py | 3 ++- examples/tracing/mysqld_query.py | 3 ++- examples/tracing/nodejs_http_server.py | 3 ++- examples/tracing/strlen_count.py | 3 ++- examples/tracing/sync_timing.py | 3 ++- examples/tracing/tcpv4connect.py | 11 ++++++----- examples/tracing/trace_perf_output.py | 3 ++- examples/tracing/urandomread-explicit.py | 3 ++- examples/tracing/urandomread.py | 3 ++- 12 files changed, 32 insertions(+), 20 deletions(-) diff --git a/examples/tracing/disksnoop.py b/examples/tracing/disksnoop.py index 17d911a12f49..e181235b626e 100755 --- a/examples/tracing/disksnoop.py +++ b/examples/tracing/disksnoop.py @@ -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 @@ -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() diff --git a/examples/tracing/hello_fields.py b/examples/tracing/hello_fields.py index be53e622287d..b8ee6db47b68 100755 --- a/examples/tracing/hello_fields.py +++ b/examples/tracing/hello_fields.py @@ -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 = """ @@ -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)) diff --git a/examples/tracing/hello_perf_output.py b/examples/tracing/hello_perf_output.py index bcf8b4388bc9..fdc74deb306c 100755 --- a/examples/tracing/hello_perf_output.py +++ b/examples/tracing/hello_perf_output.py @@ -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 = """ @@ -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) diff --git a/examples/tracing/mallocstacks.py b/examples/tracing/mallocstacks.py index 2f3eb25941b7..84b435e108b0 100755 --- a/examples/tracing/mallocstacks.py +++ b/examples/tracing/mallocstacks.py @@ -12,6 +12,7 @@ from __future__ import print_function from bcc import BPF +from bcc.utils import printb from time import sleep import sys @@ -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)) diff --git a/examples/tracing/mysqld_query.py b/examples/tracing/mysqld_query.py index 15ff297af332..aa453ce683c6 100755 --- a/examples/tracing/mysqld_query.py +++ b/examples/tracing/mysqld_query.py @@ -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: @@ -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)) diff --git a/examples/tracing/nodejs_http_server.py b/examples/tracing/nodejs_http_server.py index 1017de563040..1f6a7b90661d 100755 --- a/examples/tracing/nodejs_http_server.py +++ b/examples/tracing/nodejs_http_server.py @@ -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: @@ -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)) diff --git a/examples/tracing/strlen_count.py b/examples/tracing/strlen_count.py index 49d70809e612..eab67109f719 100755 --- a/examples/tracing/strlen_count.py +++ b/examples/tracing/strlen_count.py @@ -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 @@ -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)) diff --git a/examples/tracing/sync_timing.py b/examples/tracing/sync_timing.py index 675ad14c8923..4fad777c46f0 100755 --- a/examples/tracing/sync_timing.py +++ b/examples/tracing/sync_timing.py @@ -10,6 +10,7 @@ from __future__ import print_function from bcc import BPF +from bcc.utils import printb # load BPF program b = BPF(text=""" @@ -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)) diff --git a/examples/tracing/tcpv4connect.py b/examples/tracing/tcpv4connect.py index 8a89469dec06..81385e8f851e 100755 --- a/examples/tracing/tcpv4connect.py +++ b/examples/tracing/tcpv4connect.py @@ -16,6 +16,7 @@ from __future__ import print_function from bcc import BPF +from bcc.utils import printb # define BPF program bpf_text = """ @@ -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 @@ -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 @@ -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)) diff --git a/examples/tracing/trace_perf_output.py b/examples/tracing/trace_perf_output.py index 35a57957320f..635be129cd89 100755 --- a/examples/tracing/trace_perf_output.py +++ b/examples/tracing/trace_perf_output.py @@ -8,6 +8,7 @@ import atexit from bcc import BPF +from bcc.utils import printb import ctypes as ct class Data(ct.Structure): @@ -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: diff --git a/examples/tracing/urandomread-explicit.py b/examples/tracing/urandomread-explicit.py index 448ffdfc473d..0706092a7aa3 100755 --- a/examples/tracing/urandomread-explicit.py +++ b/examples/tracing/urandomread-explicit.py @@ -17,6 +17,7 @@ from __future__ import print_function from bcc import BPF +from bcc.utils import printb # define BPF program bpf_text = """ @@ -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)) diff --git a/examples/tracing/urandomread.py b/examples/tracing/urandomread.py index 319db2ca558f..c1468c8cdd77 100755 --- a/examples/tracing/urandomread.py +++ b/examples/tracing/urandomread.py @@ -13,6 +13,7 @@ from __future__ import print_function from bcc import BPF +from bcc.utils import printb # load BPF program b = BPF(text=""" @@ -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))