functions: TRACE_FUNC and DEBUG_STACK

Add TRACE_FUNC to trace the file, line, and name of the calling
function.  File and function names don't have to be duplicated in a
TRACE statement with this (they tend to become inaccurate as functions
are renamed and the TRACE statement is forgotten).

Add DEBUG_STACK to dump the bash stack to debug output.

Configure bash with --enable-debugger.  Bash doesn't actually include
the entire debugger, this is just some supporting variables for it.
Evidently, BASH_SOURCE[n] is only set within a function if this is
enabled.  I couldn't find this indicated in any documentation, but it
happened in practice.

Compressed initrd size only increased by 2560 bytes for librem_mini_v2,
I think that is fine.  This also gives us BASH_ARGC/BASH_ARGV which
might be useful for diagnostics.

Signed-off-by: Jonathon Hall <jonathon.hall@puri.sm>
This commit is contained in:
Jonathon Hall 2024-01-10 17:04:21 -05:00
parent 2b41a740c4
commit e0b46d086a
No known key found for this signature in database
GPG Key ID: 1E9C3CA91AE25114
2 changed files with 26 additions and 1 deletions

View File

@ -33,6 +33,28 @@ DO_WITH_DEBUG() {
"$@"
}
# Trace the current script and function.
TRACE_FUNC() {
# Index [1] for BASH_SOURCE and FUNCNAME give us the caller location.
# FUNCNAME is 'main' if called from a script outside any function.
# BASH_LINENO is offset by 1, it provides the line that the
# corresponding FUNCNAME was _called from_, so BASH_LINENO[0] is the
# location of the caller.
TRACE "${BASH_SOURCE[1]}(${BASH_LINENO[0]}): ${FUNCNAME[1]}"
}
# Show the entire current call stack in debug output - useful if a catastrophic
# error or something very unexpected occurs, like totally invalid parameters.
DEBUG_STACK() {
local FRAMES
FRAMES="${#FUNCNAME[@]}"
DEBUG "call stack: ($((FRAMES-1)) frames)"
# Don't print DEBUG_STACK itself, start from 1
for i in $(seq 1 "$((FRAMES-1))"); do
DEBUG "- $((i-1)) - ${BASH_SOURCE[$i]}(${BASH_LINENO[$((i-1))]}): ${FUNCNAME[$i]}"
done
}
pcrs() {
if [ "$CONFIG_TPM2_TOOLS" = "y" ]; then
tpm2 pcrread sha256

View File

@ -7,6 +7,9 @@ bash_tar := bash-$(bash_version).tar.gz
bash_url := https://ftpmirror.gnu.org/bash/$(bash_tar)
bash_hash := 5bac17218d3911834520dad13cd1f85ab944e1c09ae1aba55906be1f8192f558
# --enable-debugger: Enables BASH_SOURCE tracing through functions as well as
# BASH_ARGV/BASH_ARGC. (Otherwise BASH_SOURCE[0] is empty when calling a
# function, it's only set in top level script code.)
bash_configure := CFLAGS="-g0 -Os" LDFLAGS="-s" ./configure \
$(CROSS_TOOLS) \
--host $(target) \
@ -16,7 +19,7 @@ bash_configure := CFLAGS="-g0 -Os" LDFLAGS="-s" ./configure \
--mandir=/usr/share/man \
--without-bash-malloc \
--disable-coprocesses \
--disable-debugger \
--enable-debugger \
--disable-net-redirections \
--enable-single-help-strings \
--disable-nls \