bcc icon indicating copy to clipboard operation
bcc copied to clipboard

Python 3 incompatibilities/niggles

Open markdrayton opened this issue 10 years ago • 7 comments

I noticed a few scripts have issues with Python 3. E.g., funccount doesn't work:

$ sudo /usr/share/bcc/tools/funccount vfs_*
Traceback (most recent call last):
  File "/usr/share/bcc/tools/funccount", line 93, in <module>
    addr = b.ksymname(fn)
  File "/usr/lib/python3/dist-packages/bcc/__init__.py", line 664, in ksymname
    return BPF._ksym_cache.resolve_name(name)
  File "/usr/lib/python3/dist-packages/bcc/__init__.py", line 79, in resolve_name
    if lib.bcc_symcache_resolve_name(self.cache, name, ct.pointer(addr)) < 0:
ctypes.ArgumentError: argument 2: <class 'TypeError'>: wrong type

and biosnoop (and others) print char strings as bytes:

$ sudo /usr/share/bcc/tools/biosnoop 
TIME(s)        COMM           PID    DISK    T  SECTOR    BYTES   LAT(ms)
0.000000000    b'jbd2/sda1-8' 173    b'sda'  W  4598320   602112     1.38
0.001985000    b'jbd2/sda1-8' 173    b'sda'  W  4599496   4096       0.11
0.302378000    b'?'           0      b''     R  -1        8          0.23

This is not an exhaustive list — we just need to go through them all and fix any issues.

markdrayton avatar May 10 '16 18:05 markdrayton

Yes, this has been a problem since the beginning (well since python3 even existed, but nevermind), and it's actually been getting worse from what I can tell.

I would welcome any patches, and can test them out on my Arch dev machine, but haven't had the time to track them down. Unfortunately the tools aren't covered under the buildbot tests.

Above and beyond the point fixes, we have a need for:

  1. a python lint that runs similar to the c++ lint (it's non-voting in the buildbot but hey at least the infrastructure is there)
  2. tests for tools/*

drzaeus77 avatar May 10 '16 23:05 drzaeus77

How about this, is there a better way to do this? str(event.)[2:-1] TIME(s) COMM PID DISK T SECTOR BYTES LAT(ms) 0.000000000 b'?' 0 b'' R -1 8 0.21 0.544657000 b'jbd2/dm-1-8' 229 b'sda' W 53303776 61440 0.84 0.544994000 b'jbd2/dm-1-8' 229 b'sda' W 53303896 4096 0.29 2.047953000 b'?' 0 b'' R -1 8 0.20

TIME(s) COMM PID DISK T SECTOR BYTES LAT(ms) 0.000000000 ? 0 R -1 8 0.20 2.048016000 ? 0 R -1 8 0.23 2.848647000 jbd2/dm-1-8 229 sda W 53305528 57344 0.81 2.849010000 jbd2/dm-1-8 229 sda W 53305640 4096 0.30 4.095873000 ? 0 R -1 8 0.15

mcaleavya avatar May 11 '16 10:05 mcaleavya

In fact that breaks the older version :(

mcaleavya avatar May 11 '16 10:05 mcaleavya

I think this is what's needed to fix the bytes/string mix:

diff --git a/tools/biosnoop.py b/tools/biosnoop.py
index 3030e19..53ee495 100755
--- a/tools/biosnoop.py
+++ b/tools/biosnoop.py
@@ -164,8 +164,9 @@ def print_event(cpu, data, size):
         delta = float(delta) + (event.ts - prev_ts)

     print("%-14.9f %-14.14s %-6s %-7s %-2s %-9s %-7s %7.2f" % (
-        delta / 1000000, event.name, event.pid, event.disk_name, rwflg, val,
-        event.len, float(event.delta) / 1000000))
+        delta / 1000000, event.name.decode('ascii'), event.pid,
+        event.disk_name.decode('ascii'), rwflg, val, event.len,
+        float(event.delta) / 1000000))

     prev_ts = event.ts
     start_ts = 1

This works for Python 2 and 3, as far as I can see. We need to go through all scripts and make this fix. I'll try to find time for this but it might not be for a while.

markdrayton avatar May 11 '16 12:05 markdrayton

Is it worth building up a list of these? seeing b'foo' all over the place is pretty unpleasant; I'm going to try to submit PRs to fix them when I come across them, but how often (if ever?) will .decode('ascii') not be the actual solution?

frioux avatar Jan 11 '20 20:01 frioux

Agreed that b'foo' is not nice. If the tool itself uses print, I guess this is the solution. Some tools are using printb, the goal is to put the decode('ascii') in the library. Is there a way, we can do the pretty print work in library instead of having decode thing in every tool?

BTW, I am also okay to add existing decode in existing tools. I am just wondering whether we have a better method.

yonghong-song avatar Jan 13 '20 16:01 yonghong-song

At least in some cases the decoding might be able to be done in the BPF module, but I'm not sure you can know that it's a string or not there?

-- Sent from a rotary phone rented from Ma Bell

On Mon, Jan 13, 2020, 8:29 AM yonghong-song [email protected] wrote:

Agreed that b'foo' is not nice. If the tool itself uses print, I guess this is the solution. Some tools are using printb, the goal is to put the decode('ascii') in the library. Is there a way, we can do the pretty print work in library instead of having decode thing in every tool?

BTW, I am also okay to add existing decode in existing tools. I am just wondering whether we have a better method.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/iovisor/bcc/issues/532?email_source=notifications&email_token=AAAB7YZSDSRLT3PZRD57CBDQ5SJFZA5CNFSM4CDNRPBKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEIZLWHA#issuecomment-573750044, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAB7Y7Y4YMOROHS4MDQYZTQ5SJFZANCNFSM4CDNRPBA .

frioux avatar Jan 13 '20 16:01 frioux