Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 43 additions & 15 deletions initrd/etc/functions
Original file line number Diff line number Diff line change
Expand Up @@ -512,25 +512,53 @@ DO_WITH_DEBUG() {
return "$exit_status"
}

# Trace the current script and function.
# TRACE_FUNC outputs the function call stack in a readable format.
# It helps debug the execution path leading to the current function.
#
# The format of the output is:
# main(/path/to/script:line) -> function1(/path/to/file:line) -> function2(/path/to/file:line)
#
# Usage:
# Call TRACE_FUNC within any function to print the call hierarchy.
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]}"
local i stack_trace=""

# Traverse the call stack from the earliest caller to the direct caller of TRACE_FUNC
for ((i=${#FUNCNAME[@]}-1; i>1; i--)); do
stack_trace+="${FUNCNAME[i]}(${BASH_SOURCE[i]}:${BASH_LINENO[i-1]}) -> "
done

# Append the direct caller (without extra " -> " at the end)
stack_trace+="${FUNCNAME[1]}(${BASH_SOURCE[1]}:${BASH_LINENO[0]})"

# Print the final trace output
TRACE "${stack_trace}"
}

# 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 prints the entire call stack for debugging purposes.
# This function provides more detailed output than TRACE_FUNC, which is useful
# for diagnosing errors, tracking function calls, or understanding unexpected behavior.
#
# The output format:
# call stack: (N frames)
# - 0 - /path/to/file(line): function_name
# - 1 - /path/to/file(line): function_name
#
# Usage:
# Call DEBUG_STACK anywhere to display the full stack trace.
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]}"
local SKIP_FIRST=0

# If TRACE_FUNC called DEBUG_STACK, remove it from the stack to avoid redundancy.
[[ "${FUNCNAME[1]}" == "TRACE_FUNC" ]] && SKIP_FIRST=1

# Get the total number of stack frames
local FRAMES="${#FUNCNAME[@]}"
DEBUG "call stack: ($((FRAMES - 1 - SKIP_FIRST)) frames)"

# Iterate through the stack and print each function call with file and line number
for i in $(seq $((1 + SKIP_FIRST)) "$((FRAMES - 1))"); do
DEBUG "- $((i - 1 - SKIP_FIRST)) - ${BASH_SOURCE[$i]}(${BASH_LINENO[$((i - 1))]}): ${FUNCNAME[$i]}"
done
}
Comment on lines 549 to 563
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like an artifact from some earlier implementation - TRACE_FUNC doesn't call DEBUG_STACK in the final version, so I think you should revert this


Expand Down