Blob Blame History Raw
From f03beca4d6e6bc3fa7089416d752387bd26904dc Mon Sep 17 00:00:00 2001
From: Jerome Marchand <jmarchan@redhat.com>
Date: Fri, 15 Feb 2019 17:35:37 +0100
Subject: [PATCH] tools: fix some python3 bytes vs strings issues (#2205)

It fixes the following errors:

$ execsnoop.py -q
PCOMM            PID    PPID   RET ARGS
Traceback (most recent call last):
  File "_ctypes/callbacks.c", line 234, in 'calling callback function'
  File "/usr/lib/python3.6/site-packages/bcc/table.py", line 572, in raw_cb_
    callback(cpu, data, size)
  File "tools/execsnoop.py", line 229, in print_event
    for arg in argv[event.pid]
  File "tools/execsnoop.py", line 229, in <listcomp>
    for arg in argv[event.pid]
TypeError: a bytes-like object is required, not 'str'

$ offcputime.py -K -f 5
Traceback (most recent call last):
  File "./tools/offcputime.py", line 298, in <module>
    print("%s %d" % (";".join(line), v.value))
TypeError: sequence item 1: expected str instance, bytes found

$ offwaketime.py -f 5
Traceback (most recent call last):
  File "./tools/offwaketime.py", line 350, in <module>
    print("%s %d" % (";".join(line), v.value))
TypeError: sequence item 1: expected str instance, bytes found
---
 tools/execsnoop.py   | 2 +-
 tools/offcputime.py  | 6 ++++--
 tools/offwaketime.py | 8 ++++----
 3 files changed, 9 insertions(+), 7 deletions(-)

diff --git a/tools/execsnoop.py b/tools/execsnoop.py
index c4021165..1ce83e07 100755
--- a/tools/execsnoop.py
+++ b/tools/execsnoop.py
@@ -210,7 +210,7 @@ argv = defaultdict(list)
             skip = True
         if args.quote:
             argv[event.pid] = [
-                "\"" + arg.replace("\"", "\\\"") + "\""
+                b"\"" + arg.replace(b"\"", b"\\\"") + b"\""
                 for arg in argv[event.pid]
             ]
 
diff --git a/tools/offcputime.py b/tools/offcputime.py
index d84ae529..ac3b7281 100755
--- a/tools/offcputime.py
+++ b/tools/offcputime.py
@@ -288,13 +288,15 @@ stack_traces = b.get_table("stack_traces")
             if stack_id_err(k.user_stack_id):
                 line.append("[Missed User Stack]")
             else:
-                line.extend([b.sym(addr, k.tgid) for addr in reversed(user_stack)])
+                line.extend([b.sym(addr, k.tgid).decode('utf-8', 'replace')
+                    for addr in reversed(user_stack)])
         if not args.user_stacks_only:
             line.extend(["-"] if (need_delimiter and k.kernel_stack_id >= 0 and k.user_stack_id >= 0) else [])
             if stack_id_err(k.kernel_stack_id):
                 line.append("[Missed Kernel Stack]")
             else:
-                line.extend([b.ksym(addr) for addr in reversed(kernel_stack)])
+                line.extend([b.ksym(addr).decode('utf-8', 'replace')
+                    for addr in reversed(kernel_stack)])
         print("%s %d" % (";".join(line), v.value))
     else:
         # print default multi-line stack output
diff --git a/tools/offwaketime.py b/tools/offwaketime.py
index 38a9ff25..4a1cebab 100755
--- a/tools/offwaketime.py
+++ b/tools/offwaketime.py
@@ -323,28 +323,28 @@ need_delimiter = args.delimited and not (args.kernel_stacks_only or
             if stack_id_err(k.t_u_stack_id):
                 line.append("[Missed User Stack]")
             else:
-                line.extend([b.sym(addr, k.t_tgid)
+                line.extend([b.sym(addr, k.t_tgid).decode('utf-8', 'replace')
                     for addr in reversed(list(target_user_stack)[1:])])
         if not args.user_stacks_only:
             line.extend(["-"] if (need_delimiter and k.t_k_stack_id > 0 and k.t_u_stack_id > 0) else [])
             if stack_id_err(k.t_k_stack_id):
                 line.append("[Missed Kernel Stack]")
             else:
-                line.extend([b.ksym(addr)
+                line.extend([b.ksym(addr).decode('utf-8', 'replace')
                     for addr in reversed(list(target_kernel_stack)[1:])])
         line.append("--")
         if not args.user_stacks_only:
             if stack_id_err(k.w_k_stack_id):
                 line.append("[Missed Kernel Stack]")
             else:
-                line.extend([b.ksym(addr)
+                line.extend([b.ksym(addr).decode('utf-8', 'replace')
                     for addr in reversed(list(waker_kernel_stack))])
         if not args.kernel_stacks_only:
             line.extend(["-"] if (need_delimiter and k.w_u_stack_id > 0 and k.w_k_stack_id > 0) else [])
             if stack_id_err(k.w_u_stack_id):
                 line.append("[Missed User Stack]")
             else:
-                line.extend([b.sym(addr, k.w_tgid)
+                line.extend([b.sym(addr, k.w_tgid).decode('utf-8', 'replace')
                     for addr in reversed(list(waker_user_stack))])
         line.append(k.waker.decode('utf-8', 'replace'))
         print("%s %d" % (";".join(line), v.value))
-- 
2.20.1