mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Harald Seiler <hws@denx.de>
To: linux-kernel@vger.kernel.org
Cc: Jiri Olsa <jolsa@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Ingo Molnar <mingo@redhat.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Harald Seiler <hws@denx.de>
Subject: [PATCH] perf python: Convert tracepoint example to python 3
Date: Fri, 20 Dec 2019 13:29:57 +0100	[thread overview]
Message-ID: <20191220122957.8091-1-hws@denx.de> (raw)

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


                 reply	other threads:[~2019-12-20 12:30 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20191220122957.8091-1-hws@denx.de \
    --to=hws@denx.de \
    --cc=acme@kernel.org \
    --cc=jolsa@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®