Python 3 incompatibilities/niggles
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.
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:
- 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)
- tests for tools/*
How about this, is there a better way to do this?
str(event.
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
In fact that breaks the older version :(
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.
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?
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.
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 .