Merge pull request #1383 from pchaigno/pep8

Linter cleanups
diff --git a/tools/bashreadline.py b/tools/bashreadline.py
index db48e83..0be8b26 100755
--- a/tools/bashreadline.py
+++ b/tools/bashreadline.py
@@ -56,7 +56,8 @@
 
 def print_event(cpu, data, size):
     event = ct.cast(data, ct.POINTER(Data)).contents
-    print("%-9s %-6d %s" % (strftime("%H:%M:%S"), event.pid, event.str.decode()))
+    print("%-9s %-6d %s" % (strftime("%H:%M:%S"), event.pid,
+                            event.str.decode()))
 
 b["events"].open_perf_buffer(print_event)
 while 1:
diff --git a/tools/bpflist.py b/tools/bpflist.py
index b2d75d0..5fd109c 100755
--- a/tools/bpflist.py
+++ b/tools/bpflist.py
@@ -40,7 +40,8 @@
 counts = {}
 
 def parse_probes(typ):
-    if args.verbosity > 1: print("open %ss:" % typ)
+    if args.verbosity > 1:
+        print("open %ss:" % typ)
     for probe in open("/sys/kernel/debug/tracing/%s_events" % typ):
         # Probes opened by bcc have a specific pattern that includes the pid
         # of the requesting process.
@@ -48,8 +49,10 @@
         if match:
             pid = int(match.group(1))
             counts[(pid, typ)] = counts.get((pid, typ), 0) + 1
-        if args.verbosity > 1: print(probe.strip())
-    if args.verbosity > 1: print("")
+        if args.verbosity > 1:
+            print(probe.strip())
+    if args.verbosity > 1:
+        print("")
 
 if args.verbosity > 0:
     parse_probes("kprobe")
diff --git a/tools/dbslower.py b/tools/dbslower.py
index b32f6c9..d6f36cc 100755
--- a/tools/dbslower.py
+++ b/tools/dbslower.py
@@ -2,17 +2,18 @@
 #
 # dbslower      Trace MySQL and PostgreSQL queries slower than a threshold.
 #
-# USAGE: dbslower [-v] [-p PID [PID ...]] [-b PATH_TO_BINARY] [-m THRESHOLD] {mysql,postgres}
+# USAGE: dbslower [-v] [-p PID [PID ...]] [-b PATH_TO_BINARY] [-m THRESHOLD]
+#                 {mysql,postgres}
 #
 # By default, a threshold of 1ms is used. Set the threshold to 0 to trace all
-# queries (verbose). 
-# 
-# Script works in two different modes: 
-# 1) USDT probes, which means it needs MySQL and PostgreSQL built with 
+# queries (verbose).
+#
+# Script works in two different modes:
+# 1) USDT probes, which means it needs MySQL and PostgreSQL built with
 # USDT (DTrace) support.
-# 2) uprobe and uretprobe on exported function of binary specified by 
+# 2) uprobe and uretprobe on exported function of binary specified by
 # PATH_TO_BINARY parameter. (At the moment only MySQL support)
-# 
+#
 # If no PID or PATH_TO_BINARY is provided, the script attempts to discover
 # all MySQL or PostgreSQL database processes and uses USDT probes.
 #
@@ -57,12 +58,13 @@
 mode = "USDT"
 if args.path and not args.pids:
     if args.db == "mysql":
-        symbols = BPF.get_user_functions_and_addresses(args.path, "\\w+dispatch_command\\w+")
+        regex = "\\w+dispatch_command\\w+"
+        symbols = BPF.get_user_functions_and_addresses(args.path, regex)
 
         if len(symbols) == 0:
             print("Can't find function 'dispatch_command' in %s" % (args.path))
             exit(1)
-        
+
         (mysql_func_name, addr) = symbols[0]
 
         if mysql_func_name.find("COM_DATA") >= 0:
@@ -71,7 +73,8 @@
             mode = "MYSQL56"
     else:
         # Placeholder for PostrgeSQL
-        # Look on functions initStringInfo, pgstat_report_activity, EndCommand, NullCommand
+        # Look on functions initStringInfo, pgstat_report_activity, EndCommand,
+        # NullCommand
         print("Sorry at the moment PostgreSQL supports only USDT")
         exit(1)
 
@@ -109,7 +112,7 @@
 
 #if defined(MYSQL56) || defined(MYSQL57)
 /*
-Trace only packets with enum_server_command == COM_QUERY 
+Trace only packets with enum_server_command == COM_QUERY
 */
     #ifdef MYSQL56
     u64 command  = (u64) PT_REGS_PARM1(ctx);
@@ -148,7 +151,7 @@
     u64 delta = bpf_ktime_get_ns() - tempp->timestamp;
 #ifdef THRESHOLD
     if (delta >= THRESHOLD) {
-#endif //THRESHOLD     
+#endif //THRESHOLD
         struct data_t data = {};
         data.pid = pid >> 32;   // only process id
         data.timestamp = tempp->timestamp;
@@ -164,13 +167,16 @@
 """.replace("DEFINE_USDT", "#define USDT" if mode == "USDT" else "") \
    .replace("DEFINE_MYSQL56", "#define MYSQL56" if mode == "MYSQL56" else "") \
    .replace("DEFINE_MYSQL57", "#define MYSQL57" if mode == "MYSQL57" else "") \
-   .replace("DEFINE_THRESHOLD", ("#define THRESHOLD " + str(threshold_ns)) if threshold_ns > 0 else "")
+   .replace("DEFINE_THRESHOLD",
+            "#define THRESHOLD %d" % threshold_ns if threshold_ns > 0 else "")
 
 if mode.startswith("MYSQL"):
     # Uprobes mode
     bpf = BPF(text=program)
-    bpf.attach_uprobe(name=args.path, sym=mysql_func_name, fn_name="query_start")
-    bpf.attach_uretprobe(name=args.path, sym=mysql_func_name, fn_name="query_end")
+    bpf.attach_uprobe(name=args.path, sym=mysql_func_name,
+                      fn_name="query_start")
+    bpf.attach_uretprobe(name=args.path, sym=mysql_func_name,
+                         fn_name="query_end")
 else:
     # USDT mode
     if not args.pids or len(args.pids) == 0:
diff --git a/tools/lib/ucalls.py b/tools/lib/ucalls.py
index 04214e8..491565c 100755
--- a/tools/lib/ucalls.py
+++ b/tools/lib/ucalls.py
@@ -67,7 +67,8 @@
     return_probe = "method__return"
     read_class = "bpf_usdt_readarg(2, ctx, &clazz);"
     read_method = "bpf_usdt_readarg(4, ctx, &method);"
-    extra_message = "If you do not see any results, make sure you ran java with option -XX:+ExtendedDTraceProbes"
+    extra_message = ("If you do not see any results, make sure you ran java"
+                     " with option -XX:+ExtendedDTraceProbes")
 elif language == "python":
     entry_probe = "function__entry"
     return_probe = "function__return"
@@ -84,7 +85,8 @@
     return_probe = "function__return"
     read_class = "bpf_usdt_readarg(4, ctx, &clazz);"
     read_method = "bpf_usdt_readarg(1, ctx, &method);"
-    extra_message = "If you do not see any results, make sure the environment variable USE_ZEND_DTRACE is set to 1"
+    extra_message = ("If you do not see any results, make sure the environment"
+                     " variable USE_ZEND_DTRACE is set to 1")
 elif not language or language == "none":
     if not args.syscalls:
         print("Nothing to do; use -S to trace syscalls.")
diff --git a/tools/memleak.py b/tools/memleak.py
index a9030f7..5788e32 100755
--- a/tools/memleak.py
+++ b/tools/memleak.py
@@ -282,7 +282,7 @@
 
         memptrs.delete(&pid);
 
-        if (bpf_probe_read(&addr, sizeof(void*), (void*)(size_t)*memptr64) != 0)
+        if (bpf_probe_read(&addr, sizeof(void*), (void*)(size_t)*memptr64))
                 return 0;
 
         u64 addr64 = (u64)(size_t)addr;
@@ -384,7 +384,7 @@
         stack_flags += "|BPF_F_USER_STACK"
 bpf_source = bpf_source.replace("STACK_FLAGS", stack_flags)
 
-bpf_program = BPF(text=bpf_source)
+bpf = BPF(text=bpf_source)
 
 if not kernel_trace:
         print("Attaching to pid %d, Ctrl+C to quit." % pid)
@@ -394,12 +394,12 @@
                         fn_prefix = sym
 
                 try:
-                        bpf_program.attach_uprobe(name=obj, sym=sym,
-                                                  fn_name=fn_prefix+"_enter",
-                                                  pid=pid)
-                        bpf_program.attach_uretprobe(name=obj, sym=sym,
-                                                     fn_name=fn_prefix+"_exit",
-                                                     pid=pid)
+                        bpf.attach_uprobe(name=obj, sym=sym,
+                                          fn_name=fn_prefix + "_enter",
+                                          pid=pid)
+                        bpf.attach_uretprobe(name=obj, sym=sym,
+                                             fn_name=fn_prefix + "_exit",
+                                             pid=pid)
                 except Exception:
                         if can_fail:
                                 return
@@ -413,8 +413,8 @@
         attach_probes("valloc")
         attach_probes("memalign")
         attach_probes("pvalloc")
-        attach_probes("aligned_alloc", can_fail=True) # added in C11
-        bpf_program.attach_uprobe(name=obj, sym="free", fn_name="free_enter",
+        attach_probes("aligned_alloc", can_fail=True)  # added in C11
+        bpf.attach_uprobe(name=obj, sym="free", fn_name="free_enter",
                                   pid=pid)
 
 else:
@@ -425,11 +425,12 @@
         #
         # Memory allocations in Linux kernel are not limited to malloc/free
         # equivalents. It's also common to allocate a memory page or multiple
-        # pages. Page allocator have two interfaces, one working with page frame
-        # numbers (PFN), while other working with page addresses. It's possible
-        # to allocate pages with one kind of functions, and free them with
-        # another. Code in kernel can easy convert PFNs to addresses and back,
-        # but it's hard to do the same in eBPF kprobe without fragile hacks.
+        # pages. Page allocator have two interfaces, one working with page
+        # frame numbers (PFN), while other working with page addresses. It's
+        # possible to allocate pages with one kind of functions, and free them
+        # with another. Code in kernel can easy convert PFNs to addresses and
+        # back, but it's hard to do the same in eBPF kprobe without fragile
+        # hacks.
         #
         # Fortunately, Linux exposes tracepoints for memory allocations, which
         # can be instrumented by eBPF programs. Tracepoint for page allocations
@@ -440,8 +441,8 @@
         print("[%s] Top %d stacks with outstanding allocations:" %
               (datetime.now().strftime("%H:%M:%S"), top_stacks))
         alloc_info = {}
-        allocs = bpf_program["allocs"]
-        stack_traces = bpf_program["stack_traces"]
+        allocs = bpf["allocs"]
+        stack_traces = bpf["stack_traces"]
         for address, info in sorted(allocs.items(), key=lambda a: a[1].size):
                 if BPF.monotonic_time() - min_age_ns < info.timestamp_ns:
                         continue
@@ -453,7 +454,7 @@
                         stack = list(stack_traces.walk(info.stack_id))
                         combined = []
                         for addr in stack:
-                                combined.append(bpf_program.sym(addr, pid,
+                                combined.append(bpf.sym(addr, pid,
                                         show_module=True, show_offset=True))
                         alloc_info[info.stack_id] = Allocation(combined,
                                                                info.size)
@@ -467,8 +468,8 @@
                       (alloc.size, alloc.count, "\n\t\t".join(alloc.stack)))
 
 def print_outstanding_combined():
-        stack_traces = bpf_program["stack_traces"]
-        stacks = sorted(bpf_program["combined_allocs"].items(),
+        stack_traces = bpf["stack_traces"]
+        stacks = sorted(bpf["combined_allocs"].items(),
                         key=lambda a: -a[1].total_size)
         cnt = 1
         entries = []
@@ -476,7 +477,7 @@
                 try:
                         trace = []
                         for addr in stack_traces.walk(stack_id.value):
-                                sym = bpf_program.sym(addr, pid,
+                                sym = bpf.sym(addr, pid,
                                                       show_module=True,
                                                       show_offset=True)
                                 trace.append(sym)
@@ -500,7 +501,7 @@
 count_so_far = 0
 while True:
         if trace_all:
-                print(bpf_program.trace_fields())
+                print(bpf.trace_fields())
         else:
                 try:
                         sleep(interval)
diff --git a/tools/nfsslower.py b/tools/nfsslower.py
index 167c37e..cacd473 100755
--- a/tools/nfsslower.py
+++ b/tools/nfsslower.py
@@ -318,7 +318,7 @@
                                                     "OFF_KB",
                                                     "LAT(ms)",
                                                     "FILENAME"))
-    
+
 b["events"].open_perf_buffer(print_event, page_cnt=64)
 while 1:
         b.kprobe_poll()
diff --git a/tools/sslsniff.py b/tools/sslsniff.py
index 1a4df29..a81b4a4 100755
--- a/tools/sslsniff.py
+++ b/tools/sslsniff.py
@@ -199,13 +199,9 @@
         e_mark = "-" * 5 + " END DATA (TRUNCATED, " + str(truncated_bytes) + \
                 " bytes lost) " + "-" * 5
 
-    print("%-12s %-18.9f %-16s %-6d %-6d\n%s\n%s\n%s\n\n" % (rw, time_s,
-                                                             event.comm.decode(),
-                                                             event.pid,
-                                                             event.len,
-                                                             s_mark,
-                                                             event.v0.decode(),
-                                                             e_mark))
+    fmt = "%-12s %-18.9f %-16s %-6d %-6d\n%s\n%s\n%s\n\n"
+    print(fmt % (rw, time_s, event.comm.decode(), event.pid, event.len, s_mark,
+                 event.v0.decode(), e_mark))
 
 b["perf_SSL_write"].open_perf_buffer(print_event_write)
 b["perf_SSL_read"].open_perf_buffer(print_event_read)
diff --git a/tools/tcptop.py b/tools/tcptop.py
index 9ca10f6..4b448e8 100755
--- a/tools/tcptop.py
+++ b/tools/tcptop.py
@@ -149,7 +149,7 @@
     u64 *val, zero = 0;
 
     if (copied <= 0)
-	    return 0;
+        return 0;
 
     if (family == AF_INET) {
         struct ipv4_key_t ipv4_key = {.pid = pid};
diff --git a/tools/tcptracer.py b/tools/tcptracer.py
index bb44fbb..5fd80ca 100755
--- a/tools/tcptracer.py
+++ b/tools/tcptracer.py
@@ -546,7 +546,7 @@
         if args.verbose:
             print("%-14d" % (event.ts_ns - start_ts), end="")
         else:
-            print("%-9.3f" % ((float(event.ts_ns) - start_ts) / 1000000000), end="")
+            print("%-9.3f" % ((event.ts_ns - start_ts) / 1000000000.0), end="")
     if event.type == 1:
         type_str = "C"
     elif event.type == 2:
@@ -583,7 +583,7 @@
         if args.verbose:
             print("%-14d" % (event.ts_ns - start_ts), end="")
         else:
-            print("%-9.3f" % ((float(event.ts_ns) - start_ts) / 1000000000), end="")
+            print("%-9.3f" % ((event.ts_ns - start_ts) / 1000000000.0), end="")
     if event.type == 1:
         type_str = "C"
     elif event.type == 2:
@@ -601,8 +601,8 @@
     print("%-6d %-16s %-2d %-16s %-16s %-6d %-6d" %
           (event.pid, event.comm.decode('utf-8'),
            event.ip,
-           "["+inet_ntop(AF_INET6, event.saddr)+"]",
-           "["+inet_ntop(AF_INET6, event.daddr)+"]",
+           "[" + inet_ntop(AF_INET6, event.saddr) + "]",
+           "[" + inet_ntop(AF_INET6, event.daddr) + "]",
            event.sport,
            event.dport), end="")
     if args.verbose and not args.netns:
diff --git a/tools/trace.py b/tools/trace.py
index 793cd9f..4390328 100755
--- a/tools/trace.py
+++ b/tools/trace.py
@@ -492,8 +492,8 @@
                     time = strftime("%H:%M:%S") if Probe.use_localtime else \
                            Probe._time_off_str(event.timestamp_ns)
                     print("%-8s %-6d %-6d %-12s %-16s %s" %
-                          (time[:8], event.tgid, event.pid, event.comm.decode(),
-                           self._display_function(), msg))
+                          (time[:8], event.tgid, event.pid,
+                           event.comm.decode(), self._display_function(), msg))
 
                 if self.kernel_stack:
                         self.print_stack(bpf, event.kernel_stack_id, -1)