scripts/decode_stacktrace.sh: preserve alignment
cover scripts/decode_stacktrace.sh: preserve alignment
Here are a few patches slightly improving the output generated by decode_stacktrace.sh script.
- Patch 1: avoid trailing whitespaces when printing symbols.
- Patch 2: preserve alignment when printing symbols.
- Patch 3: preserve alignment when printing code.
---
Matthieu Baerts (NGI0) (3):
scripts/decode_stacktrace.sh: symbol: avoid trailing whitespaces scripts/decode_stacktrace.sh: symbol: preserve alignment scripts/decode_stacktrace.sh: code: preserve alignment
scripts/decode_stacktrace.sh | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-)
---
base-commit: 76eeb9b8de9880ca38696b2fb56ac45ac0a25c6c
change-id: 20250908-decode_strace_indent-04f69b1f9b14
Best regards,
Signature · 1 line
Matthieu Baerts (NGI0) <[email protected]>
PATCH 1/3 scripts/decode_stacktrace.sh: symbol: avoid trailing whitespaces
Lines having a symbol to decode might not always have info after this symbol. It means ${info_str} might not be set, but it will always be printed after a space, causing trailing whitespaces.
That's a detail, but when the output is opened with an editor marking these trailing whitespaces, that's a bit disturbing. It is easy to remove them by printing this variable with a space only if it is set.
While at it, do the same with ${module} and print everything in one line.
--- scripts/decode_stacktrace.sh | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-)
scripts/decode_stacktrace.sh +1 -6
@@ -323,12 +323,7 @@ handle_line() { parse_symbol # modifies $symbol # Add up the line number to the symbol - if [[ -z ${module} ]] - then - echo "${words[@]}" "$symbol ${info_str}" - else - echo "${words[@]}" "$symbol $module ${info_str}" - fi + echo "${words[@]}" "${symbol}${module:+ ${module}}${info_str:+ ${info_str}}" } while read line; do
Signature · 1 line
2.51.0
Re: [PATCH 1/3] scripts/decode_stacktrace.sh: symbol: avoid trailing whitespaces
Quoted 37 lines · On Mon, Sep 08, 2025 at 05:41:57PM +0200, Matthieu Baerts (NGI0) wrote:
Lines having a symbol to decode might not always have info after this symbol. It means ${info_str} might not be set, but it will always be printed after a space, causing trailing whitespaces.
That's a detail, but when the output is opened with an editor marking these trailing whitespaces, that's a bit disturbing. It is easy to remove them by printing this variable with a space only if it is set.
While at it, do the same with ${module} and print everything in one line.
Signed-off-by Matthieu Baerts (NGI0) <[email protected]>--- scripts/decode_stacktrace.sh | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-)scripts/decode_stacktrace.sh +1
-6@@ -323,12 +323,7 @@ handle_line() { parse_symbol # modifies $symbol # Add up the line number to the symbol - if [[ -z ${module} ]] - then - echo "${words[@]}" "$symbol ${info_str}" - else - echo "${words[@]}" "$symbol $module ${info_str}" - fi + echo "${words[@]}" "${symbol}${module:+ ${module}}${info_str:+ ${info_str}}" } while read line; doSignature · 2 lines
-- 2.51.0
LGTM,
Re: [PATCH 1/3] scripts/decode_stacktrace.sh: symbol: avoid trailing whitespaces
In reply to Matthieu Baerts (NGI0)Quoted 11 lines · On Mon, 08 Sep 2025 17:41:57 +0200 "Matthieu Baerts (NGI0)"…
Lines having a symbol to decode might not always have info after this symbol. It means ${info_str} might not be set, but it will always be printed after a space, causing trailing whitespaces.
That's a detail, but when the output is opened with an editor marking these trailing whitespaces, that's a bit disturbing. It is easy to remove them by printing this variable with a space only if it is set.
While at it, do the same with ${module} and print everything in one line.
Signed-off-by Matthieu Baerts (NGI0) <[email protected]>
Signature · 3 lines
Luca Ceresoli, Bootlin Embedded Linux and Kernel engineering https://bootlin.com
Re: [PATCH 1/3] scripts/decode_stacktrace.sh: symbol: avoid trailing whitespaces
In reply to Matthieu Baerts (NGI0)Quoted 11 lines · On Mon, Sep 08, 2025 at 05:41:57PM +0200, Matthieu Baerts (NGI0) wrote:
Lines having a symbol to decode might not always have info after this symbol. It means ${info_str} might not be set, but it will always be printed after a space, causing trailing whitespaces.
That's a detail, but when the output is opened with an editor marking these trailing whitespaces, that's a bit disturbing. It is easy to remove them by printing this variable with a space only if it is set.
While at it, do the same with ${module} and print everything in one line.
Signed-off-by Matthieu Baerts (NGI0) <[email protected]>
PATCH 2/3 scripts/decode_stacktrace.sh: symbol: preserve alignment
In reply to Matthieu Baerts (NGI0)With lines having a symbol to decode, the script was only trying to preserve the alignment for the timestamps, but not the rest, nor when the caller was set (CONFIG_PRINTK_CALLER=y).
With this sample ...
[ 52.080924] Call Trace: [ 52.080926] <TASK> [ 52.080931] dump_stack_lvl+0x6f/0xb0
... the script was producing the following output:
[ 52.080924] Call Trace: [ 52.080926] <TASK> [ 52.080931] dump_stack_lvl (arch/x86/include/asm/irqflags.h:19)
(dump_stack_lvl is no longer aligned with <TASK>: one missing space)
With this other sample ...
[ 52.080924][ T48] Call Trace: [ 52.080926][ T48] <TASK> [ 52.080931][ T48] dump_stack_lvl+0x6f/0xb0
... the script was producing the following output:
[ 52.080924][ T48] Call Trace: [ 52.080926][ T48] <TASK> [ 52.080931][ T48] dump_stack_lvl (arch/x86/include/asm/irqflags.h:19)
(the misalignment is clearer here)
That's because the script had a workaround for CONFIG_PRINTK_TIME=y only, see the previous comment called "Format timestamps with tabs".
To always preserve spaces, they need to be recorded along the words. That is what is now done with the new 'spaces' array.
Some notes:
- 'extglob' is needed only for this operation, and that's why it is set in a dedicated subshell.
- 'read' is used with '-r' not to treat a <backslash> character in any special way, e.g. when followed by a space.
- When a word is removed from the 'words' array, the corresponding space needs to be removed from the 'spaces' array as well.
With the last sample, we now have:
[ 52.080924][ T48] Call Trace: [ 52.080926][ T48] <TASK> [ 52.080931][ T48] dump_stack_lvl (arch/x86/include/asm/irqflags.h:19)
(the alignment is preserved)
--- scripts/decode_stacktrace.sh | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-)
scripts/decode_stacktrace.sh +12 -14
@@ -255,10 +255,11 @@ handle_line() { basepath=${basepath%/init/main.c:*)} fi - local words + local words spaces - # Tokenize - read -a words <<<"$1" + # Tokenize: words and spaces to preserve the alignment + read -ra words <<<"$1" + IFS='#' read -ra spaces <<<"$(shopt -s extglob; echo "${1//+([^[:space:]])/#}")" # Remove hex numbers. Do it ourselves until it happens in the # kernel
@@ -270,19 +271,13 @@ handle_line() { for i in "${!words[@]}"; do # Remove the address if [[ ${words[$i]} =~ \[\<([^]]+)\>\] ]]; then - unset words[$i] - fi - - # Format timestamps with tabs - if [[ ${words[$i]} == \[ && ${words[$i+1]} == *\] ]]; then - unset words[$i] - words[$i+1]=$(printf "[%13s\n" "${words[$i+1]}") + unset words[$i] spaces[$i] fi done if [[ ${words[$last]} =~ ^[0-9a-f]+\] ]]; then words[$last-1]="${words[$last-1]} ${words[$last]}" - unset words[$last] + unset words[$last] spaces[$last] last=$(( $last - 1 )) fi
@@ -294,7 +289,7 @@ handle_line() { local info_str="" if [[ ${words[$last]} =~ \([A-Z]*\) ]]; then info_str=${words[$last]} - unset words[$last] + unset words[$last] spaces[$last] last=$(( $last - 1 )) fi
@@ -311,7 +306,7 @@ handle_line() { modbuildid= fi symbol=${words[$last-1]} - unset words[$last-1] + unset words[$last-1] spaces[$last-1] else # The symbol is the last element, process it symbol=${words[$last]}
@@ -323,7 +318,10 @@ handle_line() { parse_symbol # modifies $symbol # Add up the line number to the symbol - echo "${words[@]}" "${symbol}${module:+ ${module}}${info_str:+ ${info_str}}" + for i in "${!words[@]}"; do + echo -n "${spaces[i]}${words[i]}" + done + echo "${spaces[$last]}${symbol}${module:+ ${module}}${info_str:+ ${info_str}}" } while read line; do
Signature · 1 line
2.51.0
Re: [PATCH 2/3] scripts/decode_stacktrace.sh: symbol: preserve alignment
Quoted 137 lines · On Mon, Sep 08, 2025 at 05:41:58PM +0200, Matthieu Baerts (NGI0) wrote:
With lines having a symbol to decode, the script was only trying to preserve the alignment for the timestamps, but not the rest, nor when the caller was set (
CONFIG_PRINTK_CALLER=y).With this sample ...
[ 52.080924] Call Trace: [ 52.080926] <TASK> [ 52.080931] dump_stack_lvl+0x6f/0xb0... the script was producing the following output:
[ 52.080924] Call Trace: [ 52.080926] <TASK> [ 52.080931] dump_stack_lvl (arch/x86/include/asm/irqflags.h:19)(
dump_stack_lvlis no longer aligned with <TASK>: one missing space)With this other sample ...
[ 52.080924][ T48] Call Trace: [ 52.080926][ T48] <TASK> [ 52.080931][ T48] dump_stack_lvl+0x6f/0xb0... the script was producing the following output:
[ 52.080924][ T48] Call Trace: [ 52.080926][ T48] <TASK> [ 52.080931][ T48] dump_stack_lvl (arch/x86/include/asm/irqflags.h:19)(the misalignment is clearer here)
That's because the script had a workaround for
CONFIG_PRINTK_TIME=yonly, see the previous comment called "Format timestamps with tabs".To always preserve spaces, they need to be recorded along the words. That is what is now done with the new 'spaces' array.
Some notes:
- 'extglob' is needed only for this operation, and that's why it is set in a dedicated subshell.
- 'read' is used with '-r' not to treat a <backslash> character in any special way, e.g. when followed by a space.
- When a word is removed from the 'words' array, the corresponding space needs to be removed from the 'spaces' array as well.
With the last sample, we now have:
[ 52.080924][ T48] Call Trace: [ 52.080926][ T48] <TASK> [ 52.080931][ T48] dump_stack_lvl (arch/x86/include/asm/irqflags.h:19)(the alignment is preserved)
Signed-off-by Matthieu Baerts (NGI0) <[email protected]>--- scripts/decode_stacktrace.sh | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-)scripts/decode_stacktrace.sh +12
-14@@ -255,10 +255,11 @@ handle_line() { basepath=${basepath%/init/main.c:*)} fi - local words + local words spaces - # Tokenize - read -a words <<<"$1" + # Tokenize: words and spaces to preserve the alignment + read -ra words <<<"$1" + IFS='#' read -ra spaces <<<"$(shopt -s extglob; echo "${1//+([^[:space:]])/#}")" # Remove hex numbers. Do it ourselves until it happens in the # kernel@@ -270,19 +271,13 @@ handle_line() { for i in "${!words[@]}"; do # Remove the address if [[ ${words[$i]} =~ \[\<([^]]+)\>\] ]]; then - unset words[$i] - fi - - # Format timestamps with tabs - if [[ ${words[$i]} == \[ && ${words[$i+1]} == *\] ]]; then - unset words[$i] - words[$i+1]=$(printf "[%13s\n" "${words[$i+1]}") + unset words[$i] spaces[$i] fi done if [[ ${words[$last]} =~ ^[0-9a-f]+\] ]]; then words[$last-1]="${words[$last-1]} ${words[$last]}" - unset words[$last] + unset words[$last] spaces[$last] last=$(( $last - 1 )) fi@@ -294,7 +289,7 @@ handle_line() { local info_str="" if [[ ${words[$last]} =~ \([A-Z]*\) ]]; then info_str=${words[$last]} - unset words[$last] + unset words[$last] spaces[$last] last=$(( $last - 1 )) fi@@ -311,7 +306,7 @@ handle_line() { modbuildid= fi symbol=${words[$last-1]} - unset words[$last-1] + unset words[$last-1] spaces[$last-1] else # The symbol is the last element, process it symbol=${words[$last]}@@ -323,7 +318,10 @@ handle_line() { parse_symbol # modifies $symbol # Add up the line number to the symbol - echo "${words[@]}" "${symbol}${module:+ ${module}}${info_str:+ ${info_str}}" + for i in "${!words[@]}"; do + echo -n "${spaces[i]}${words[i]}" + done + echo "${spaces[$last]}${symbol}${module:+ ${module}}${info_str:+ ${info_str}}" } while read line; doSignature · 2 lines
-- 2.51.0
I just tried this and it works for me. From this...
[ 51.711528][ T6914] ================================================================== [ 51.712906][ T6914] BUG: KASAN: double-free in __kmem_cache_free (mm/slub.c:3875) [ 51.713765][ T6914] Free of addr 85ffff8912b19f80 by task sh/6914 [...] [ 51.717788][ T6914] Call trace: [ 51.718182][ T6914] dump_backtrace (arch/arm64/kernel/stacktrace.c:236) [ 51.718734][ T6914] show_stack (arch/arm64/kernel/stacktrace.c:244) [ 51.719219][ T6914] dump_stack_lvl (lib/dump_stack.c:107)
... to now this:
[ 51.711528][ T6914] ================================================================== [ 51.712906][ T6914] BUG: KASAN: double-free in __kmem_cache_free (mm/slub.c:3875) [ 51.713765][ T6914] Free of addr 85ffff8912b19f80 by task sh/6914 [...] [ 51.717788][ T6914] Call trace: [ 51.718182][ T6914] dump_backtrace (arch/arm64/kernel/stacktrace.c:236) [ 51.718734][ T6914] show_stack (arch/arm64/kernel/stacktrace.c:244) [ 51.719219][ T6914] dump_stack_lvl (lib/dump_stack.c:107)
PATCH 3/3 scripts/decode_stacktrace.sh: code: preserve alignment
In reply to Matthieu Baerts (NGI0)With lines having a code to decode, the alignment was not preserved for the first line.
With this sample ...
[ 52.238089][ T55] RIP: 0010:__ip_queue_xmit+0x127c/0x1820 [ 52.238401][ T55] Code: c1 83 e0 07 48 c1 e9 03 83 c0 03 (...)
... the script was producing the following output:
[ 52.238089][ T55] RIP: 0010:__ip_queue_xmit (...) [ 52.238401][ T55] Code: c1 83 e0 07 48 c1 e9 03 83 c0 03 (...)
That's because scripts/decodecode doesn't preserve the alignment. No need to modify it, it is enough to give only the "Code: (...)" part to this script, and print the prefix without modifications.
With the same sample, we now have:
[ 52.238089][ T55] RIP: 0010:__ip_queue_xmit (...) [ 52.238401][ T55] Code: c1 83 e0 07 48 c1 e9 03 83 c0 03 (...)
--- scripts/decode_stacktrace.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
scripts/decode_stacktrace.sh +3 -1
@@ -242,8 +242,10 @@ debuginfod_get_vmlinux() { decode_code() { local scripts=`dirname "${BASH_SOURCE[0]}"` + local lim="Code: " - echo "$1" | $scripts/decodecode + echo -n "${1%%${lim}*}" + echo "${lim}${1##*${lim}}" | $scripts/decodecode } handle_line() {
Signature · 1 line
2.51.0
Re: [PATCH 3/3] scripts/decode_stacktrace.sh: code: preserve alignment
Quoted 47 lines · On Mon, Sep 08, 2025 at 05:41:59PM +0200, Matthieu Baerts (NGI0) wrote:
With lines having a code to decode, the alignment was not preserved for the first line.
With this sample ...
[ 52.238089][ T55] RIP: 0010:__ip_queue_xmit+0x127c/0x1820 [ 52.238401][ T55] Code: c1 83 e0 07 48 c1 e9 03 83 c0 03 (...)... the script was producing the following output:
[ 52.238089][ T55] RIP: 0010:__ip_queue_xmit (...) [ 52.238401][ T55] Code: c1 83 e0 07 48 c1 e9 03 83 c0 03 (...)That's because
scripts/decodecodedoesn't preserve the alignment. No need to modify it, it is enough to give only the "Code: (...)" part to this script, and print the prefix without modifications.With the same sample, we now have:
[ 52.238089][ T55] RIP: 0010:__ip_queue_xmit (...) [ 52.238401][ T55] Code: c1 83 e0 07 48 c1 e9 03 83 c0 03 (...)Signed-off-by Matthieu Baerts (NGI0) <[email protected]>--- scripts/decode_stacktrace.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)scripts/decode_stacktrace.sh +3
-1@@ -242,8 +242,10 @@ debuginfod_get_vmlinux() { decode_code() { local scripts=`dirname "${BASH_SOURCE[0]}"` + local lim="Code: " - echo "$1" | $scripts/decodecode + echo -n "${1%%${lim}*}" + echo "${lim}${1##*${lim}}" | $scripts/decodecode } handle_line() {Signature · 2 lines
-- 2.51.0
This also worked for me. From this:
[ 143.815379][ T5218] kernel BUG at rust/helpers/bug.c:7! [ 143.815970][ T5218] Oops: invalid opcode: 0000 [#1] PREEMPT SMP KASAN PTI [...] [ 143.842198][ T5218] RIP: 0010:rust_helper_BUG (rust/helpers/bug.c:7 (discriminator 4)) [ 143.842231][ T5218] Code: cc cc cc cc cc 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 00 b8 (...)
... to now:
[ 143.815379][ T5218] kernel BUG at rust/helpers/bug.c:7! [ 143.815970][ T5218] Oops: invalid opcode: 0000 [#1] PREEMPT SMP KASAN PTI [...] [ 143.842198][ T5218] RIP: 0010:rust_helper_BUG (rust/helpers/bug.c:7 (discriminator 4)) [ 143.842231][ T5218] Code: cc cc cc cc cc 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 00 b8 (...)