linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Florian Fainelli <f.fainelli@gmail.com>
To: stable@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: "Leonard Crestez" <leonard.crestez@nxp.com>,
	"Jan Kiszka" <jan.kiszka@siemens.com>,
	"Jason Wessel" <jason.wessel@windriver.com>,
	"Kieran Bingham" <kieran@ksquared.org.uk>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"Linus Torvalds" <torvalds@linux-foundation.org>,
	"Florian Fainelli" <f.fainelli@gmail.com>,
	"Kieran Bingham" <kieran@ksquared.org.uk>,
	"André Draszik" <git@andred.net>,
	"Du Changbin" <changbin.du@gmail.com>
Subject: [stable 4.9.y 3/4] scripts/gdb: lx-dmesg: use explicit encoding=utf8 errors=replace
Date: Thu,  7 Jan 2021 14:52:28 -0800	[thread overview]
Message-ID: <20210107225229.1502459-4-f.fainelli@gmail.com> (raw)
In-Reply-To: <20210107225229.1502459-1-f.fainelli@gmail.com>

From: Leonard Crestez <leonard.crestez@nxp.com>

commit 46d10a094353c05144f3b0530516bdac3ce7c435 upstream

Use errors=replace because it is never desirable for lx-dmesg to fail on
string decoding errors, not even if the log buffer is corrupt and we
show incorrect info.

The kernel will sometimes print utf8, for example the copyright symbol
from jffs2.  In order to make this work specify 'utf8' everywhere
because python2 otherwise defaults to 'ascii'.

In theory the second errors='replace' is not be required because
everything that can be decoded as utf8 should also be encodable back to
utf8.  But it's better to be extra safe here.  It's worth noting that
this is definitely not true for encoding='ascii', unknown characters are
replaced with U+FFFD REPLACEMENT CHARACTER and they fail to encode back
to ascii.

Link: http://lkml.kernel.org/r/acee067f3345954ed41efb77b80eebdc038619c6.1498481469.git.leonard.crestez@nxp.com
Signed-off-by: Leonard Crestez <leonard.crestez@nxp.com>
Acked-by: Jan Kiszka <jan.kiszka@siemens.com>
Cc: Jason Wessel <jason.wessel@windriver.com>
Cc: Kieran Bingham <kieran@ksquared.org.uk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Florian Fainelli <f.fainelli@gmail.com>
---
 scripts/gdb/linux/dmesg.py | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/scripts/gdb/linux/dmesg.py b/scripts/gdb/linux/dmesg.py
index f5a030333dfd..6d2e09a2ad2f 100644
--- a/scripts/gdb/linux/dmesg.py
+++ b/scripts/gdb/linux/dmesg.py
@@ -12,6 +12,7 @@
 #
 
 import gdb
+import sys
 
 from linux import utils
 
@@ -52,13 +53,19 @@ class LxDmesg(gdb.Command):
                 continue
 
             text_len = utils.read_u16(log_buf[pos + 10:pos + 12])
-            text = log_buf[pos + 16:pos + 16 + text_len].decode()
+            text = log_buf[pos + 16:pos + 16 + text_len].decode(
+                encoding='utf8', errors='replace')
             time_stamp = utils.read_u64(log_buf[pos:pos + 8])
 
             for line in text.splitlines():
-                gdb.write("[{time:12.6f}] {line}\n".format(
+                msg = u"[{time:12.6f}] {line}\n".format(
                     time=time_stamp / 1000000000.0,
-                    line=line))
+                    line=line)
+                # With python2 gdb.write will attempt to convert unicode to
+                # ascii and might fail so pass an utf8-encoded str instead.
+                if sys.hexversion < 0x03000000:
+                    msg = msg.encode(encoding='utf8', errors='replace')
+                gdb.write(msg)
 
             pos += length
 
-- 
2.25.1


  parent reply	other threads:[~2021-01-07 22:53 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-07 22:52 [stable 4.9.y 0/4] scripts/gdb Fixes for stable 4.9 Florian Fainelli
2021-01-07 22:52 ` [stable 4.9.y 1/4] scripts/gdb: make lx-dmesg command work (reliably) Florian Fainelli
2021-01-07 22:52 ` [stable 4.9.y 2/4] scripts/gdb: lx-dmesg: cast log_buf to void* for addr fetch Florian Fainelli
2021-01-07 22:52 ` Florian Fainelli [this message]
2021-01-07 22:52 ` [stable 4.9.y 4/4] scripts/gdb: fix lx-version string output Florian Fainelli
2021-01-10 12:14 ` [stable 4.9.y 0/4] scripts/gdb Fixes for stable 4.9 Sasha Levin

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=20210107225229.1502459-4-f.fainelli@gmail.com \
    --to=f.fainelli@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=changbin.du@gmail.com \
    --cc=git@andred.net \
    --cc=jan.kiszka@siemens.com \
    --cc=jason.wessel@windriver.com \
    --cc=kieran@ksquared.org.uk \
    --cc=leonard.crestez@nxp.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=torvalds@linux-foundation.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;
as well as URLs for NNTP newsgroup(s).