Bytes/string encoding (#2004)
* tools: uses 'replace' error handler by default in decode()
Tools might encouter characters from non utf-8 charset (e.g. a file
name). When this happen, it's better to replace the unexpected
character by a question mark than crash the tool when all we do is
to print the string.
* tools: fix a bytes/string issue in attach_perf_event()
diff --git a/tools/tcpconnect.py b/tools/tcpconnect.py
index f0b23b0..ac84326 100755
--- a/tools/tcpconnect.py
+++ b/tools/tcpconnect.py
@@ -202,7 +202,7 @@
start_ts = event.ts_us
print("%-9.3f" % ((float(event.ts_us) - start_ts) / 1000000), end="")
print("%-6d %-12.12s %-2d %-16s %-16s %-4d" % (event.pid,
- event.task.decode(), event.ip,
+ event.task.decode('utf-8', 'replace'), event.ip,
inet_ntop(AF_INET, pack("I", event.saddr)),
inet_ntop(AF_INET, pack("I", event.daddr)), event.dport))
@@ -214,8 +214,9 @@
start_ts = event.ts_us
print("%-9.3f" % ((float(event.ts_us) - start_ts) / 1000000), end="")
print("%-6d %-12.12s %-2d %-16s %-16s %-4d" % (event.pid,
- event.task.decode(), event.ip, inet_ntop(AF_INET6, event.saddr),
- inet_ntop(AF_INET6, event.daddr), event.dport))
+ event.task.decode('utf-8', 'replace'), event.ip,
+ inet_ntop(AF_INET6, event.saddr), inet_ntop(AF_INET6, event.daddr),
+ event.dport))
# initialize BPF
b = BPF(text=bpf_text)