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/profile.py b/tools/profile.py
index 1530b98..d1d3d26 100755
--- a/tools/profile.py
+++ b/tools/profile.py
@@ -268,7 +268,7 @@
def aksym(addr):
if args.annotations:
- return b.ksym(addr) + "_[k]"
+ return b.ksym(addr) + "_[k]".encode()
else:
return b.ksym(addr)
@@ -320,7 +320,7 @@
line.append("[Missed Kernel Stack]")
else:
line.extend([b.ksym(addr) for addr in reversed(kernel_stack)])
- print("%s %d" % (b";".join(line).decode(), v.value))
+ print("%s %d" % (b";".join(line).decode('utf-8', 'replace'), v.value))
else:
# print default multi-line stack output
if not args.user_stacks_only:
@@ -336,8 +336,8 @@
print(" [Missed User Stack]")
else:
for addr in user_stack:
- print(" %s" % b.sym(addr, k.pid).decode())
- print(" %-16s %s (%d)" % ("-", k.name.decode(), k.pid))
+ print(" %s" % b.sym(addr, k.pid).decode('utf-8', 'replace'))
+ print(" %-16s %s (%d)" % ("-", k.name.decode('utf-8', 'replace'), k.pid))
print(" %d\n" % v.value)
# check missing