Skip to contentSkip to conversation

Improve stack pretty printing

PATCH In review Nina Schoetterl-Glausch 2 participants 5 messages Apr 4, 2023 – Apr 20, 2023 KVM

lore
Share & tools
lore
#0 lore raw

v2 cover Improve stack pretty printing

I noticed some bugs/deficiencies in the pretty_print_stacks script. Namely, it doesn't cope with 0 addresses, which might occur on s390x when backtracing through a interrupt stack frame. Since an interrupt is not a function call, the calling convention doesn't apply and we cannot tell where the stack is.

Additionally, the script stops printing the stack if addr2line cannot determine the line number, instead of skipping the printing of the source.

Lastly, the file path determination was broken for me because I use git worktrees and there being symlinks in the paths.
The proposed change works for me and fixes the issue.
I HAVE NOT TESTED THIS ON OTHER PLATFORMS/OSes, so I don't know if the code is portable.

v1 -> v2
* Cc kvm list (get_maintainers.pl doesn't list it for the file)
* pick up Tested-by (thanks Thomas)

Nina Schoetterl-Glausch (3):
pretty_print_stacks: prevent invalid address arguments
pretty_print_stacks: support unknown line numbers
pretty_print_stacks: modify relative path calculation

 scripts/pretty_print_stacks.py | 35 ++++++++++++++++++----------------
 1 file changed, 19 insertions(+), 16 deletions(-)

base-commit: 5b5d27da2973b20ec29b18df4d749fb2190458af

Signature · 1 line
2.37.2
#1 lore raw

PATCH v2 1/3 pretty_print_stacks: prevent invalid address arguments

If a return address is zero, the script will pass -1 to addr2line which it considers an invalid flag.
Zero is an unlikely return address, but we might see it, for example because the backtrace is unreliable on s390x if there is an interrupt frame on the stack.

Fixes a9143a24 ("scripts: pretty print stack traces")
---
 scripts/pretty_print_stacks.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/pretty_print_stacks.py +1 -1
index 1e59cde6..ba6f0825 100755
@@ -22,7 +22,7 @@ def pretty_print_stack(binary, line):
         if addr.startswith('@'):
             addrs[i] = addr[1:]
         else:
-            addrs[i] = '%lx' % (int(addrs[i], 16) - 1)
+            addrs[i] = '%lx' % max((int(addrs[i], 16) - 1), 0)
 
     # Output like this:
     #        0x004002be: start64 at path/to/kvm-unit-tests/x86/cstart64.S:208
Signature · 1 line
2.37.2
#2 lore raw

PATCH v2 2/3 pretty_print_stacks: support unknown line numbers

In reply to Nina Schoetterl-Glausch

Handle the case where addr2line cannot determine a line number. Skip printing lines for this frame in this case.

Fixes a9143a24 ("scripts: pretty print stack traces")
---
 scripts/pretty_print_stacks.py | 27 ++++++++++++++-------------
 1 file changed, 14 insertions(+), 13 deletions(-)
scripts/pretty_print_stacks.py +14 -13
index ba6f0825..1a27f309 100755
@@ -37,23 +37,24 @@ def pretty_print_stack(binary, line):
         return
 
     for line in out.splitlines():
-        m = re.match(b'(.*) at [^ ]*/kvm-unit-tests/([^ ]*):([0-9]+)(.*)', line)
+        m = re.match(b'(.*) at [^ ]*/kvm-unit-tests/([^ ]*):(([0-9]+)|\?)(.*)', line)
         if m is None:
             puts('%s\n' % line)
             return
 
-        head, path, line, tail = m.groups()
-        line = int(line)
-        puts('%s at %s:%d%s\n' % (head.decode(), path.decode(), line, tail.decode()))
-        try:
-            lines = open(path).readlines()
-        except IOError:
-            continue
-        if line > 1:
-            puts('        %s\n' % lines[line - 2].rstrip())
-        puts('      > %s\n' % lines[line - 1].rstrip())
-        if line < len(lines):
-            puts('        %s\n' % lines[line].rstrip())
+        head, path, maybeline, line, tail = m.groups()
+        puts('%s at %s:%s%s\n' % (head.decode(), path.decode(), maybeline.decode(), tail.decode()))
+        if line:
+            line = int(line)
+            try:
+                lines = open(path).readlines()
+            except IOError:
+                continue
+            if line > 1:
+                puts('        %s\n' % lines[line - 2].rstrip())
+            puts('      > %s\n' % lines[line - 1].rstrip())
+            if line < len(lines):
+                puts('        %s\n' % lines[line].rstrip())
 
 def main():
     if len(sys.argv) != 2:
Signature · 1 line
2.37.2
#3 lore raw

PATCH v2 3/3 pretty_print_stacks: modify relative path calculation

In reply to Nina Schoetterl-Glausch

Don't assume we can get a relative path by cutting of certain prefixes, since this isn't guaranteed to work.
Instead use a library function.
Also normalize paths in order to take care of symlinks.

Fixes a9143a24 ("scripts: pretty print stack traces")
---
 scripts/pretty_print_stacks.py | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
scripts/pretty_print_stacks.py +5 -3
index 1a27f309..90026b72 100755
@@ -4,6 +4,7 @@ import re
 import subprocess
 import sys
 import traceback
+import os
 
 config = {}
 
@@ -25,8 +26,8 @@ def pretty_print_stack(binary, line):
             addrs[i] = '%lx' % max((int(addrs[i], 16) - 1), 0)
 
     # Output like this:
-    #        0x004002be: start64 at path/to/kvm-unit-tests/x86/cstart64.S:208
-    #         (inlined by) test_ept_violation at path/to/kvm-unit-tests/x86/vmx_tests.c:1719 (discriminator 1)
+    #        0x004002be: start64 at path/to/kvm-unit-tests-repo-worktree/x86/cstart64.S:208
+    #         (inlined by) test_ept_violation at path/to/kvm-unit-tests-repo-worktree/x86/vmx_tests.c:1719 (discriminator 1)
     cmd = [config.get('ADDR2LINE', 'addr2line'), '-e', binary, '-i', '-f', '--pretty', '--address']
     cmd.extend(addrs)
 
@@ -37,12 +38,13 @@ def pretty_print_stack(binary, line):
         return
 
     for line in out.splitlines():
-        m = re.match(b'(.*) at [^ ]*/kvm-unit-tests/([^ ]*):(([0-9]+)|\?)(.*)', line)
+        m = re.match(b'(.*) at (.*):(([0-9]+)|\?)([^:]*)', line)
         if m is None:
             puts('%s\n' % line)
             return
 
         head, path, maybeline, line, tail = m.groups()
+        path = os.path.relpath(os.path.realpath(path), start=os.path.realpath(os.getcwdb()))
         puts('%s at %s:%s%s\n' % (head.decode(), path.decode(), maybeline.decode(), tail.decode()))
         if line:
             line = int(line)
Signature · 1 line
2.37.2
#4 lore raw
In reply to Nina Schoetterl-Glausch
Quoted 6 lines · Nina Schoetterl-Glausch

I noticed some bugs/deficiencies in the pretty_print_stacks script. Namely, it doesn't cope with 0 addresses, which might occur on s390x when backtracing through a interrupt stack frame. Since an interrupt is not a function call, the calling convention doesn't apply and we cannot tell where the stack is.

Quoted 7 lines

Additionally, the script stops printing the stack if addr2line cannot determine the line number, instead of skipping the printing of the source.

Lastly, the file path determination was broken for me because I use git worktrees and there being symlinks in the paths.
The proposed change works for me and fixes the issue.

Thanks! Since there were no objections or other suggestions, I went ahead and pushed the three patches now.

Thomas