Skip to content

Commit 46d10a0

Browse files
cdleonardtorvalds
authored andcommitted
scripts/gdb: lx-dmesg: use explicit encoding=utf8 errors=replace
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>
1 parent c454756 commit 46d10a0

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

‎scripts/gdb/linux/dmesg.py‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#
1313

1414
import gdb
15+
import sys
1516

1617
from linux import utils
1718

@@ -52,13 +53,19 @@ def invoke(self, arg, from_tty):
5253
continue
5354

5455
text_len = utils.read_u16(log_buf[pos + 10:pos + 12])
55-
text = log_buf[pos + 16:pos + 16 + text_len].decode()
56+
text = log_buf[pos + 16:pos + 16 + text_len].decode(
57+
encoding='utf8', errors='replace')
5658
time_stamp = utils.read_u64(log_buf[pos:pos + 8])
5759

5860
for line in text.splitlines():
59-
gdb.write("[{time:12.6f}] {line}\n".format(
61+
msg = u"[{time:12.6f}] {line}\n".format(
6062
time=time_stamp / 1000000000.0,
61-
line=line))
63+
line=line)
64+
# With python2 gdb.write will attempt to convert unicode to
65+
# ascii and might fail so pass an utf8-encoded str instead.
66+
if sys.hexversion < 0x03000000:
67+
msg = msg.encode(encoding='utf8', errors='replace')
68+
gdb.write(msg)
6269

6370
pos += length
6471

0 commit comments

Comments
 (0)