linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [stable 4.9.y 0/4] scripts/gdb Fixes for stable 4.9
@ 2021-01-07 22:52 Florian Fainelli
  2021-01-07 22:52 ` [stable 4.9.y 1/4] scripts/gdb: make lx-dmesg command work (reliably) Florian Fainelli
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Florian Fainelli @ 2021-01-07 22:52 UTC (permalink / raw)
  To: stable, linux-kernel
  Cc: Florian Fainelli, Jan Kiszka, Kieran Bingham, Andrew Morton,
	Leonard Crestez, André Draszik, Du Changbin

Hi Greg, Sasha,

This series contains some scripts/gdb/ fixes that are already present in
newer stable kernels.

Thanks!

André Draszik (1):
  scripts/gdb: make lx-dmesg command work (reliably)

Du Changbin (1):
  scripts/gdb: fix lx-version string output

Leonard Crestez (2):
  scripts/gdb: lx-dmesg: cast log_buf to void* for addr fetch
  scripts/gdb: lx-dmesg: use explicit encoding=utf8 errors=replace

 scripts/gdb/linux/dmesg.py | 22 +++++++++++++++-------
 scripts/gdb/linux/proc.py  |  2 +-
 2 files changed, 16 insertions(+), 8 deletions(-)

-- 
2.25.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [stable 4.9.y 1/4] scripts/gdb: make lx-dmesg command work (reliably)
  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 ` 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
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Florian Fainelli @ 2021-01-07 22:52 UTC (permalink / raw)
  To: stable, linux-kernel
  Cc: André Draszik, Kieran Bingham, Jan Kiszka, Andrew Morton,
	Linus Torvalds, Florian Fainelli, Leonard Crestez, Du Changbin

From: André Draszik <git@andred.net>

commit d6c9708737c2107c38bd75f133d14d5801b8d6d5 upstream

lx-dmesg needs access to the log_buf symbol from printk.c.
Unfortunately, the symbol log_buf also exists in BPF's verifier.c and
hence gdb can pick one or the other.  If it happens to pick BPF's
log_buf, lx-dmesg doesn't work:

  (gdb) lx-dmesg
  Python Exception <class 'gdb.MemoryError'> Cannot access memory at address 0x0:
  Error occurred in Python command: Cannot access memory at address 0x0
  (gdb) p log_buf
  $15 = 0x0

Luckily, GDB has a way to deal with this, see
  https://sourceware.org/gdb/onlinedocs/gdb/Symbols.html

  (gdb) info variables ^log_buf$
  All variables matching regular expression "^log_buf$":

  File <linux.git>/kernel/bpf/verifier.c:
  static char *log_buf;

  File <linux.git>/kernel/printk/printk.c:
  static char *log_buf;
  (gdb) p 'verifier.c'::log_buf
  $1 = 0x0
  (gdb) p 'printk.c'::log_buf
  $2 = 0x811a6aa0 <__log_buf> ""
  (gdb) p &log_buf
  $3 = (char **) 0x8120fe40 <log_buf>
  (gdb) p &'verifier.c'::log_buf
  $4 = (char **) 0x8120fe40 <log_buf>
  (gdb) p &'printk.c'::log_buf
  $5 = (char **) 0x8048b7d0 <log_buf>

By being explicit about the location of the symbol, we can make lx-dmesg
work again.  While at it, do the same for the other symbols we need from
printk.c

Link: http://lkml.kernel.org/r/20170526112222.3414-1-git@andred.net
Signed-off-by: André Draszik <git@andred.net>
Tested-by: Kieran Bingham <kieran@bingham.xyz>
Acked-by: Jan Kiszka <jan.kiszka@siemens.com>
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 | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/scripts/gdb/linux/dmesg.py b/scripts/gdb/linux/dmesg.py
index f9b92ece7834..5afd1098e33a 100644
--- a/scripts/gdb/linux/dmesg.py
+++ b/scripts/gdb/linux/dmesg.py
@@ -23,10 +23,11 @@ class LxDmesg(gdb.Command):
         super(LxDmesg, self).__init__("lx-dmesg", gdb.COMMAND_DATA)
 
     def invoke(self, arg, from_tty):
-        log_buf_addr = int(str(gdb.parse_and_eval("log_buf")).split()[0], 16)
-        log_first_idx = int(gdb.parse_and_eval("log_first_idx"))
-        log_next_idx = int(gdb.parse_and_eval("log_next_idx"))
-        log_buf_len = int(gdb.parse_and_eval("log_buf_len"))
+        log_buf_addr = int(str(gdb.parse_and_eval(
+            "'printk.c'::log_buf")).split()[0], 16)
+        log_first_idx = int(gdb.parse_and_eval("'printk.c'::log_first_idx"))
+        log_next_idx = int(gdb.parse_and_eval("'printk.c'::log_next_idx"))
+        log_buf_len = int(gdb.parse_and_eval("'printk.c'::log_buf_len"))
 
         inf = gdb.inferiors()[0]
         start = log_buf_addr + log_first_idx
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [stable 4.9.y 2/4] scripts/gdb: lx-dmesg: cast log_buf to void* for addr fetch
  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 ` Florian Fainelli
  2021-01-07 22:52 ` [stable 4.9.y 3/4] scripts/gdb: lx-dmesg: use explicit encoding=utf8 errors=replace Florian Fainelli
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Florian Fainelli @ 2021-01-07 22:52 UTC (permalink / raw)
  To: stable, linux-kernel
  Cc: Leonard Crestez, Jan Kiszka, Jason Wessel, Kieran Bingham,
	Andrew Morton, Linus Torvalds, Florian Fainelli, Kieran Bingham,
	André Draszik, Du Changbin

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

commit c454756f47277b651ad41a5a163499294529e35d upstream

In some cases it is possible for the str() conversion here to throw
encoding errors because log_buf might not point to valid ascii.  For
example:

  (gdb) python print str(gdb.parse_and_eval("log_buf"))
  Traceback (most recent call last):
    File "<string>", line 1, in <module>
  UnicodeEncodeError: 'ascii' codec can't encode character u'\u0303' in
  	position 24: ordinal not in range(128)

Avoid this by explicitly casting to (void *) inside the gdb expression.

Link: http://lkml.kernel.org/r/ba6f85dbb02ca980ebd0e2399b0649423399b565.1498481469.git.leonard.crestez@nxp.com
Signed-off-by: Leonard Crestez <leonard.crestez@nxp.com>
Reviewed-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 | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/scripts/gdb/linux/dmesg.py b/scripts/gdb/linux/dmesg.py
index 5afd1098e33a..f5a030333dfd 100644
--- a/scripts/gdb/linux/dmesg.py
+++ b/scripts/gdb/linux/dmesg.py
@@ -24,7 +24,7 @@ class LxDmesg(gdb.Command):
 
     def invoke(self, arg, from_tty):
         log_buf_addr = int(str(gdb.parse_and_eval(
-            "'printk.c'::log_buf")).split()[0], 16)
+            "(void *)'printk.c'::log_buf")).split()[0], 16)
         log_first_idx = int(gdb.parse_and_eval("'printk.c'::log_first_idx"))
         log_next_idx = int(gdb.parse_and_eval("'printk.c'::log_next_idx"))
         log_buf_len = int(gdb.parse_and_eval("'printk.c'::log_buf_len"))
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [stable 4.9.y 3/4] scripts/gdb: lx-dmesg: use explicit encoding=utf8 errors=replace
  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
  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
  4 siblings, 0 replies; 6+ messages in thread
From: Florian Fainelli @ 2021-01-07 22:52 UTC (permalink / raw)
  To: stable, linux-kernel
  Cc: Leonard Crestez, Jan Kiszka, Jason Wessel, Kieran Bingham,
	Andrew Morton, Linus Torvalds, Florian Fainelli, Kieran Bingham,
	André Draszik, Du Changbin

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


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [stable 4.9.y 4/4] scripts/gdb: fix lx-version string output
  2021-01-07 22:52 [stable 4.9.y 0/4] scripts/gdb Fixes for stable 4.9 Florian Fainelli
                   ` (2 preceding siblings ...)
  2021-01-07 22:52 ` [stable 4.9.y 3/4] scripts/gdb: lx-dmesg: use explicit encoding=utf8 errors=replace Florian Fainelli
@ 2021-01-07 22:52 ` Florian Fainelli
  2021-01-10 12:14 ` [stable 4.9.y 0/4] scripts/gdb Fixes for stable 4.9 Sasha Levin
  4 siblings, 0 replies; 6+ messages in thread
From: Florian Fainelli @ 2021-01-07 22:52 UTC (permalink / raw)
  To: stable, linux-kernel
  Cc: Du Changbin, Kieran Bingham, Jan Kiszka, Jason Wessel,
	Daniel Thompson, Andrew Morton, Linus Torvalds, Florian Fainelli,
	Kieran Bingham, Leonard Crestez, André Draszik

From: Du Changbin <changbin.du@gmail.com>

commit b058809bfc8faeb7b7cae047666e23375a060059 upstream

A bug is present in GDB which causes early string termination when
parsing variables.  This has been reported [0], but we should ensure
that we can support at least basic printing of the core kernel strings.

For current gdb version (has been tested with 7.3 and 8.1), 'lx-version'
only prints one character.

  (gdb) lx-version
  L(gdb)

This can be fixed by casting 'linux_banner' as (char *).

  (gdb) lx-version
  Linux version 4.19.0-rc1+ (changbin@acer) (gcc version 7.3.0 (Ubuntu 7.3.0-16ubuntu3)) #21 SMP Sat Sep 1 21:43:30 CST 2018

[0] https://sourceware.org/bugzilla/show_bug.cgi?id=20077

[kbingham@kernel.org: add detail to commit message]
Link: http://lkml.kernel.org/r/20181111162035.8356-1-kieran.bingham@ideasonboard.com
Fixes: 2d061d999424 ("scripts/gdb: add version command")
Signed-off-by: Du Changbin <changbin.du@gmail.com>
Signed-off-by: Kieran Bingham <kbingham@kernel.org>
Acked-by: Jan Kiszka <jan.kiszka@siemens.com>
Cc: Jan Kiszka <jan.kiszka@siemens.com>
Cc: Jason Wessel <jason.wessel@windriver.com>
Cc: Daniel Thompson <daniel.thompson@linaro.org>
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/proc.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/scripts/gdb/linux/proc.py b/scripts/gdb/linux/proc.py
index 38b1f09d1cd9..822e3767bc05 100644
--- a/scripts/gdb/linux/proc.py
+++ b/scripts/gdb/linux/proc.py
@@ -40,7 +40,7 @@ class LxVersion(gdb.Command):
 
     def invoke(self, arg, from_tty):
         # linux_banner should contain a newline
-        gdb.write(gdb.parse_and_eval("linux_banner").string())
+        gdb.write(gdb.parse_and_eval("(char *)linux_banner").string())
 
 LxVersion()
 
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [stable 4.9.y 0/4] scripts/gdb Fixes for stable 4.9
  2021-01-07 22:52 [stable 4.9.y 0/4] scripts/gdb Fixes for stable 4.9 Florian Fainelli
                   ` (3 preceding siblings ...)
  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 ` Sasha Levin
  4 siblings, 0 replies; 6+ messages in thread
From: Sasha Levin @ 2021-01-10 12:14 UTC (permalink / raw)
  To: Florian Fainelli
  Cc: stable, linux-kernel, Jan Kiszka, Kieran Bingham, Andrew Morton,
	Leonard Crestez, André Draszik, Du Changbin

On Thu, Jan 07, 2021 at 02:52:25PM -0800, Florian Fainelli wrote:
>Hi Greg, Sasha,
>
>This series contains some scripts/gdb/ fixes that are already present in
>newer stable kernels.

Queued up, thanks!

-- 
Thanks,
Sasha

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-01-10 12:15 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [stable 4.9.y 3/4] scripts/gdb: lx-dmesg: use explicit encoding=utf8 errors=replace Florian Fainelli
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

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).