KVM: selftests: Gracefully handle empty stack traces
PATCH 1/1 KVM: selftests: Gracefully handle empty stack traces
Bail out of test_dump_stack() if the stack trace is empty rather than invoking addr2line with zero addresses. The problem with the latter is that addr2line will block waiting for addresses to be passed in via stdin, e.g. if running a selftest from an interactive terminal.
Opportunistically fix up the comment that mentions skipping 3 frames since only 2 are skipped in the code.
--- tools/testing/selftests/kvm/lib/assert.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/lib/assert.c +9 -3
@@ -42,12 +42,18 @@ static void test_dump_stack(void) c = &cmd[0 ]; c +=sprintf (c,"%s" , addr2line);/* -* Skip the first 3 frames: backtrace, test_dump_stack, and -* test_assert. We hope that backtrace isn't inlined and the other two -* we've declared noinline. +* Skip the first 2 frames, which should be test_dump_stack() and +* test_assert(); both of which are declared noinline. Bail if the +* resulting stack trace would be empty. Otherwise, addr2line will block +* waiting for addresses to be passed in via stdin. */ +if (n <=2 ) { +fputs (" (stack trace empty)\n" , stderr); +return ; + }for (i =2 ; i < n; i++) c +=sprintf (c," %lx" , ((unsigned long ) stack[i]) -1 ); + c +=sprintf (c,"%s" , pipeline);#pragma GCC diagnostic push#pragma GCC diagnostic ignored "-Wunused-result"
base-commit: 372d07084593dc7a399bf9bee815711b1fb1bcf2
prerequisite-patch-id: 2e3661ba8856c29b769499bac525b6943d9284b8
prerequisite-patch-id: 1a148d98d96d73a520ed070260608ddf1bdd0f08
Signature · 1 line
2.37.3.998.g577e59143f-goog
Quoted 26 lines · David Matlack
Bail out of
test_dump_stack()if the stack trace is empty rather than invoking addr2line with zero addresses. The problem with the latter is that addr2line will block waiting for addresses to be passed in via stdin, e.g. if running a selftest from an interactive terminal.Opportunistically fix up the comment that mentions skipping 3 frames since only 2 are skipped in the code.
Cc Vipin Sharma <[email protected]>Cc Sean Christopherson <[email protected]>Signed-off-by David Matlack <[email protected]>--- tools/testing/selftests/kvm/lib/assert.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)tools/testing/selftests/kvm/lib/assert.c +0
-2@@ -42,12 +42,18 @@ static void test_dump_stack(void) c = &cmd[0 ]; c +=sprintf (c,"%s" , addr2line);/* -* Skip the first 3 frames: backtrace, test_dump_stack, and -* test_assert. We hope that backtrace isn't inlined and the other two
tools/testing/selftests/kvm/lib/assert.c static void test_dump_stack(void) · line 53
Quoted 10 lines
tools/testing/selftests/kvm/lib/assert.c +8
-1-* we've declared noinline. +* Skip the first 2 frames, which should be test_dump_stack() and +* test_assert(); both of which are declared noinline. Bail if the +* resulting stack trace would be empty. Otherwise, addr2line will block +* waiting for addresses to be passed in via stdin. */ +if (n <=2 ) { +fputs (" (stack trace empty)\n" , stderr); +return ; + }
Shouldn't this condition be put immediately after
n = backtrace(stack,n)
It is more natural to check the return value when an API has returned.
Verified that this change does fix the issue. Thanks for the fix.
Quoted 13 lines
tools/testing/selftests/kvm/lib/assert.c +1
-0for (i =2 ; i < n; i++) c +=sprintf (c," %lx" , ((unsigned long ) stack[i]) -1 ); + c +=sprintf (c,"%s" , pipeline);#pragma GCC diagnostic push#pragma GCC diagnostic ignored "-Wunused-result"base-commit:
372d07084593dc7a399bf9bee815711b1fb1bcf2
prerequisite-patch-id:2e3661ba8856c29b769499bac525b6943d9284b8
prerequisite-patch-id:1a148d98d96d73a520ed070260608ddf1bdd0f08--
2.37.3.998.g577e59143f-goog
Sean Christopherson
reviewerQuoted 6 lines · Vipin Sharma
Quoted 5 lines · David Matlack
Bail out of
test_dump_stack()if the stack trace is empty rather than invoking addr2line with zero addresses. The problem with the latter is that addr2line will block waiting for addresses to be passed in via stdin, e.g. if running a selftest from an interactive terminal.
How does this bug occur? Does backtrace() get inlined?
Quoted 25 lines
Quoted 25 lines
Opportunistically fix up the comment that mentions skipping 3 frames since only 2 are skipped in the code.
Cc Vipin Sharma <[email protected]>Cc Sean Christopherson <[email protected]>Signed-off-by David Matlack <[email protected]>--- tools/testing/selftests/kvm/lib/assert.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)tools/testing/selftests/kvm/lib/assert.c +4
-3@@ -42,12 +42,18 @@ static void test_dump_stack(void) c = &cmd[0 ]; c +=sprintf (c,"%s" , addr2line);/* -* Skip the first 3 frames: backtrace, test_dump_stack, and -* test_assert. We hope that backtrace isn't inlined and the other two -* we've declared noinline. +* Skip the first 2 frames, which should be test_dump_stack() and +* test_assert(); both of which are declared noinline. Bail if the +* resulting stack trace would be empty. Otherwise, addr2line will block +* waiting for addresses to be passed in via stdin.
Quoted 8 lines
tools/testing/selftests/kvm/lib/assert.c static void test_dump_stack(void) · line 53
Quoted 5 lines
tools/testing/selftests/kvm/lib/assert.c +4
-0*/ +if (n <=2 ) { +fputs (" (stack trace empty)\n" , stderr); +return ; + }Shouldn't this condition be put immediately after
n =backtrace(stack,n)
Agreed, that would be more intuitive.
Quoted 9 lines · Sean Christopherson
Quoted 6 lines · Vipin Sharma
Quoted 5 lines · David Matlack
Bail out of
test_dump_stack()if the stack trace is empty rather than invoking addr2line with zero addresses. The problem with the latter is that addr2line will block waiting for addresses to be passed in via stdin, e.g. if running a selftest from an interactive terminal.How does this bug occur? Does
backtrace()get inlined?
backtrace() is returning 0. I haven't debugged it further than that yet. I figured gracefully handling an empty stack trace would be useful to have independent of this specific issue (which I assume has something to do with our Google-specific build process).
backtrace() is not getting inlined.
Quoted 35 lines
Quoted 26 lines
Quoted 26 lines
Opportunistically fix up the comment that mentions skipping 3 frames since only 2 are skipped in the code.
Cc Vipin Sharma <[email protected]>Cc Sean Christopherson <[email protected]>Signed-off-by David Matlack <[email protected]>--- tools/testing/selftests/kvm/lib/assert.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)tools/testing/selftests/kvm/lib/assert.c +4
-3@@ -42,12 +42,18 @@ static void test_dump_stack(void) c = &cmd[0 ]; c +=sprintf (c,"%s" , addr2line);/* -* Skip the first 3 frames: backtrace, test_dump_stack, and -* test_assert. We hope that backtrace isn't inlined and the other two -* we've declared noinline. +* Skip the first 2 frames, which should be test_dump_stack() and +* test_assert(); both of which are declared noinline. Bail if the +* resulting stack trace would be empty. Otherwise, addr2line will block +* waiting for addresses to be passed in via stdin. Quoted 8 lines
tools/testing/selftests/kvm/lib/assert.c static void test_dump_stack(void) · line 53
Quoted 5 lines
tools/testing/selftests/kvm/lib/assert.c +4
-0*/ +if (n <=2 ) { +fputs (" (stack trace empty)\n" , stderr); +return ; + }Shouldn't this condition be put immediately after
n =backtrace(stack,n)
Quoted 1 line
Agreed, that would be more intuitive.
I had that at one point, but then it became confusing that the check is for (n <= 2) and not (!n).
How about this?
tools/testing/selftests/kvm/lib/assert.c +19 -8
@@ -38,16 +38,28 @@ static void test_dump_stack(void) 1];char *c; - n =backtrace (stack, n); c = &cmd[0 ]; c +=sprintf (c,"%s" , addr2line); -/* -* Skip the first 3 frames: backtrace, test_dump_stack, and -* test_assert. We hope that backtrace isn't inlined and the other two -* we've declared noinline. -*/ -for (i =2 ; i < n; i++) - c +=sprintf (c," %lx" , ((unsigned long ) stack[i]) -1 ); + + n =backtrace (stack, n); +if (n >2 ) { +/* +* Skip the first 2 frames, which should be test_dump_stack() +* and test_assert(); both of which are declared noinline. +*/ +for (i =2 ; i < n; i++) + c +=sprintf (c," %lx" , ((unsigned long ) stack[i]) -1 ); + }else { +/* +* Bail if the resulting stack trace would be empty. Otherwise, +* addr2line will block waiting for addresses to be passed in +* via stdin. +*/ +fputs (" (stack trace missing)\n" , stderr); +return ; + } + c +=sprintf (c,"%s" , pipeline);#pragma GCC diagnostic push#pragma GCC diagnostic ignored "-Wunused-result"
Paolo Bonzini
maintainerQuoted 33 lines · David Matlack
How about this?
tools/testing/selftests/kvm/lib/assert.c +12
-8@@ -38,16 +38,28 @@ static void test_dump_stack(void) 1];char *c; - n =backtrace (stack, n); c = &cmd[0 ]; c +=sprintf (c,"%s" , addr2line); -/* -* Skip the first 3 frames: backtrace, test_dump_stack, and -* test_assert. We hope that backtrace isn't inlined and the other two -* we've declared noinline. -*/ -for (i =2 ; i < n; i++) - c +=sprintf (c," %lx" , ((unsigned long ) stack[i]) -1 ); + + n =backtrace (stack, n); +if (n >2 ) { +/* +* Skip the first 2 frames, which should be test_dump_stack() +* and test_assert(); both of which are declared noinline. +*/ +for (i =2 ; i < n; i++) + c +=sprintf (c," %lx" , ((unsigned long ) stack[i]) -1 ); + }else { +/* +* Bail if the resulting stack trace would be empty. Otherwise,
tools/testing/selftests/kvm/lib/assert.c static void test_dump_stack(void) · line 64
Quoted 10 lines
tools/testing/selftests/kvm/lib/assert.c +7
-0+* addr2line will block waiting for addresses to be passed in +* via stdin. +*/ +fputs (" (stack trace missing)\n" , stderr); +return ; + } + c +=sprintf (c,"%s" , pipeline);#pragma GCC diagnostic push#pragma GCC diagnostic ignored "-Wunused-result"
I think your original patch is better, just with
tools/testing/selftests/kvm/lib/assert.c +3 -3
@@ -36,11 +36,9 @@ static void test_dump_stack(void) n * (((sizeof (void *)) *2 ) +1 ) +/* Null terminator: */ 1]; -char *c; +char *c = cmd; n =backtrace (stack, n); - c = &cmd[0 ]; - c +=sprintf (c,"%s" , addr2line);/* * Skip the first 2 frames, which should be test_dump_stack() and * test_assert(); both of which are declared noinline. Bail if the
@@ -51,6 +49,8 @@ static void test_dump_stack(void)fputs (" (stack trace empty)\n" , stderr);return ; } + + c +=sprintf (c,"%s" , addr2line);for (i =2 ; i < n; i++) c +=sprintf (c," %lx" , ((unsigned long ) stack[i]) -1 );
squashed in to keep the "if" and backtrace() call as close as possible.
Paolo