linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] perf python: Convert tracepoint example to python 3
@ 2019-12-20 12:29 Harald Seiler
  0 siblings, 0 replies; only message in thread
From: Harald Seiler @ 2019-12-20 12:29 UTC (permalink / raw)
  To: linux-kernel
  Cc: Jiri Olsa, Arnaldo Carvalho de Melo, Ingo Molnar, Peter Zijlstra,
	Harald Seiler

Make the tracepoint example compatible with python 3 and fix string
handling of the prev_comm and next_comm fields so they are properly
truncated.

Signed-off-by: Harald Seiler <hws@denx.de>
---
 tools/perf/python/tracepoint.py | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/tools/perf/python/tracepoint.py b/tools/perf/python/tracepoint.py
index eb76f6516247..a5420cfb3a31 100755
--- a/tools/perf/python/tracepoint.py
+++ b/tools/perf/python/tracepoint.py
@@ -3,6 +3,8 @@
 # -*- python -*-
 # -*- coding: utf-8 -*-
 
+from __future__ import print_function
+
 import perf
 
 class tracepoint(perf.evsel):
@@ -34,15 +36,23 @@ def main():
             if not isinstance(event, perf.sample_event):
                 continue
 
-            print "time %u prev_comm=%s prev_pid=%d prev_prio=%d prev_state=0x%x ==> next_comm=%s next_pid=%d next_prio=%d" % (
+            # The prev_comm and next_comm fields are `bytes` objects but they
+            # are not truncated to the first \x00.  This is due to the
+            # underlying code treating the strings as just an array of
+            # arbitrary bytes.  Convert them to properly truncated python
+            # strings.
+            prev_comm_str = event.prev_comm.split(b'\x00', 1)[0].decode(errors="ignore")
+            next_comm_str = event.next_comm.split(b'\x00', 1)[0].decode(errors="ignore")
+
+            print("time {} prev_comm={} prev_pid={} prev_prio={} prev_state=0x{:x} ==> next_comm={} next_pid={} next_prio={}".format(
                    event.sample_time,
-                   event.prev_comm,
+                   prev_comm_str,
                    event.prev_pid,
                    event.prev_prio,
                    event.prev_state,
-                   event.next_comm,
+                   next_comm_str,
                    event.next_pid,
-                   event.next_prio)
+                   event.next_prio))
 
 if __name__ == '__main__':
     main()
-- 
2.20.1


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2019-12-20 12:30 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-20 12:29 [PATCH] perf python: Convert tracepoint example to python 3 Harald Seiler

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).