pkg-auto: Move debugging stuff to a separate file

I'll use it also in new libraries.
This commit is contained in:
Krzesimir Nowak 2025-04-10 18:17:56 +02:00
parent fff6bd78b2
commit e1a4d8e5a9
2 changed files with 204 additions and 111 deletions

190
pkg_auto/impl/debug.sh Normal file
View File

@ -0,0 +1,190 @@
#!/bin/bash
if [[ -z ${__DEBUG_SH_INCLUDED__:-} ]]; then
__DEBUG_SH_INCLUDED__=x
source "$(dirname "${BASH_SOURCE[0]}")/util.sh"
# Adds package names for which debugging output may be enabled.
function pkg_debug_add() {
if [[ ${#} -eq 0 ]]; then
return
fi
if ! declare -p __D_DEBUG_PACKAGES >/dev/null 2>&1; then
declare -gA __D_DEBUG_PACKAGES=()
fi
local pkg
for pkg; do
__D_DEBUG_PACKAGES["${pkg}"]=x
done
}
# Resets debugging state to zero (no packages to debug, debug output
# disabled).
function pkg_debug_reset() {
unset __D_DEBUG_PACKAGES __D_DEBUG
}
# Enables debugging output when specific packages are processed.
#
# Params:
#
# @ - package names to enable debugging for
function pkg_debug_enable() {
local -A pkg_set=()
local -a vals=()
local pkg
if ! declare -p __D_DEBUG_PACKAGES >/dev/null 2>&1; then
return 0
fi
for pkg; do
if [[ -n ${pkg_set["${pkg}"]:-} ]]; then
continue
fi
pkg_set["${pkg}"]=x
if [[ -n ${__D_DEBUG_PACKAGES["${pkg}"]:-} ]]; then
vals+=( "${pkg}" )
fi
done
if [[ ${#vals[@]} -gt 0 ]]; then
declare -g __D_DEBUG
join_by __D_DEBUG ',' "${vals[@]}"
fi
}
# Get a list of package names for which debugging output may be
# enabled.
function pkg_debug_packages() {
local -n pkg_names_ref=${1}; shift
if ! declare -p __D_DEBUG_PACKAGES >/dev/null 2>&1; then
pkg_names_ref=()
else
pkg_names_ref=( "${!__D_DEBUG_PACKAGES[@]}" )
fi
}
# Checks if debugging output is enabled. Returns true if yes,
# otherwise false.
function pkg_debug_enabled() {
local -i ret=0
[[ -n ${__D_DEBUG:-} ]] || ret=1
return ${ret}
}
# Disables debugging output.
function pkg_debug_disable() {
unset __D_DEBUG
}
# Prints passed parameters if debugging is enabled.
#
# Params:
#
# @ - parameters to print
function pkg_debug() {
if [[ -n ${__D_DEBUG:-} ]]; then
pkg_debug_print_c "${__D_DEBUG}" "${@}"
fi
}
# Prints passed lines if debugging is enabled.
#
# Params:
#
# @ - lines to print
function pkg_debug_lines() {
if [[ -n ${__D_DEBUG:-} ]]; then
pkg_debug_print_lines_c "${__D_DEBUG}" "${@}"
fi
}
# Prints passed parameters unconditionally with debug
# formatting. Useful for more complicated debugging logic using
# pkg_debug_enabled.
#
# if pkg_debug_enabled; then
# pkg_debug_print 'debug message'
# fi
#
# Params:
#
# @ - parameters to print
function pkg_debug_print() {
if [[ -z ${__D_DEBUG:+notempty} ]]; then
info "bad use of pkg_debug_print, __D_DEBUG is unset"
debug_stacktrace
fail '<print failwhale here>'
fi
pkg_debug_print_c "${__D_DEBUG}" "${@}"
}
# Prints passed lines unconditionally with debug formatting. Useful
# for more complicated debugging logic using pkg_debug_enabled.
#
# if pkg_debug_enabled; then
# pkg_debug_print_lines 'debug' 'message'
# fi
#
# Params:
#
# @ - lines to print
function pkg_debug_print_lines() {
if [[ -z ${__D_DEBUG:+notempty} ]]; then
info "bad use of pkg_debug_print_lines, __D_DEBUG is unset"
debug_stacktrace
fail '<print failwhale here>'
fi
pkg_debug_print_lines_c "${__D_DEBUG}" "${@}"
}
# Prints passed parameters unconditionally with custom debug
# formatting. Useful for either more complicated debugging logic using
# pkg_debug_packages or for non-package-specific debugging situations.
#
# local -a dpkgs=()
# pkg_debug_packages dpkgs
# local pkg
# for pkg in "${dpkgs[@]}"; do pkg_debug_print_c "${pkg}" 'debug message'
#
# Params:
#
# 1 - comma-separated package names
# @ - parameters to print
function pkg_debug_print_c() {
local d_debug=${1}
info "DEBUG(${d_debug}): ${*}"
}
# Prints passed lines unconditionally with custom debug formatting.
# Useful for either more complicated debugging logic using
# pkg_debug_packages or for non-package-specific debugging situations.
#
# local -a dpkgs=() lines=( 'debug' 'message; )
# pkg_debug_packages dpkgs
# local pkg
# for pkg in "${dpkgs[@]}"; do pkg_debug_print_lines_c "${pkg}" "${lines[@]}"
#
# Params:
#
# 1 - comma-separated package names
# @ - lines to print
function pkg_debug_print_lines_c() {
local d_debug=${1}
info_lines "${@/#/"DEBUG(${d_debug}): "}"
}
# Prints current stacktrace.
function debug_stacktrace() {
local -i idx=0 last_idx=${#FUNCNAME[@]}
((last_idx--))
while [[ idx -lt last_idx ]]; do
info "at ${FUNCNAME[idx]}, invoked by ${FUNCNAME[$((idx + 1))]} in ${BASH_SOURCE[idx + 1]}:${BASH_LINENO[idx]}"
((++idx))
done
}
fi

View File

@ -51,6 +51,7 @@ __PKG_AUTO_LIB_SH_INCLUDED__=x
source "$(dirname "${BASH_SOURCE[0]}")/util.sh"
source "${PKG_AUTO_IMPL_DIR}/cleanups.sh"
source "${PKG_AUTO_IMPL_DIR}/debug.sh"
source "${PKG_AUTO_IMPL_DIR}/gentoo_ver.sh"
# Sets up the workdir using the passed config. The config can be
@ -128,7 +129,7 @@ function setup_workdir_with_config() {
if [[ -n ${cfg_sdk_image_override} ]]; then
override_sdk_image_name "${cfg_sdk_image_override}"
fi
add_debug_packages "${cfg_debug_packages[@]}"
pkg_debug_add "${cfg_debug_packages[@]}"
}
# Goes over the list of automatically updated packages and synces them
@ -251,27 +252,6 @@ function override_sdk_image_name() {
append_to_globals "SDK_IMAGE=${image_name@Q}"
}
# Adds information about packages to be debugged to the globals file.
#
# Params:
#
# @ - a list of packages to be debugged
function add_debug_packages() {
local -a prepared lines
prepared=( "${@@Q}" )
prepared=( "${prepared[@]/#/' ['}" )
prepared=( "${prepared[@]/%/']=x'}" )
lines=(
''
'local -A DEBUG_PACKAGES'
''
'DEBUG_PACKAGES=('
"${prepared[@]}"
')'
)
append_to_globals "${lines[@]}"
}
# Appends passed lines to the globals file.
#
# Params:
@ -1025,7 +1005,7 @@ function process_listings() {
# acct-group/adm-0-r2::portage-stable
while read -r pkg; do
pkg_debug_enable "${pkg}"
pkg_debug "processing listings: adding tag ${kind^^}"
pkg_debug "processing listing ${arch}/${file}: adding tag ${kind^^}"
pkg_debug_disable
mvm_add pl_pkg_to_tags_set_mvm "${pkg}" "${kind^^}"
# VER_ERE_UNBOUNDED and PKG_ERE_UNBOUNDED come from gentoo_ver.sh
@ -1036,28 +1016,14 @@ function process_listings() {
mvm_iterate pl_pkg_to_tags_set_mvm set_mvm_to_array_mvm_cb "${pkg_to_tags_mvm_var_name}"
mvm_unset pl_pkg_to_tags_set_mvm
#mvm_debug_disable pl_pkg_to_tags_set_mvm
if pkg_debug_possible; then
mvm_iterate "${pkg_to_tags_mvm_var_name}" debug_dump_package_tags "${pkg_to_tags_mvm_var_name}"
fi
}
# A debug function that prints the package tags. Used as a callback to
# mvm_iterate.
#
# Params:
#
# 1 - name of the array mvm variable (extra arg of the callback)
# 2 - name of the package
# 3 - name of the array variable holding tags (unused)
# @ - tags
function debug_dump_package_tags() {
local map_name=${1}; shift
local pkg=${1}; shift
shift # we don't care about array variable name
# rest are array elements, which are tags
pkg_debug_enable "${pkg}"
pkg_debug "tags for ${pkg} stored in ${map_name}: ${*}"
pkg_debug_disable
local -a pl_debug_pkgs pl_tags_array_name
pkg_debug_packages pl_debug_pkgs
for pkg in "${pl_debug_pkgs[@]}"; do
mvm_get "${pkg_to_tags_mvm_var_name}" "${pkg}" pl_tags_array_name
local -n tags_ref=${pl_tags_array_name:-EMPTY_ARRAY}
pkg_debug_print_c "${pkg}" "tags stored in ${pkg_to_tags_mvm_var_name}: ${tags_ref[*]}"
unset -n tags_ref
done
}
# A callback to mvm_iterate that turns a set mvm to an array mvm. It
@ -1689,11 +1655,11 @@ function consistency_checks() {
mvm_get "${name}" "${pkg}" cc_slot_verminmax_map_var_name
verminmax_map_var_names+=("${cc_slot_verminmax_map_var_name}")
done
if pkg_debug_possible; then
if pkg_debug_enabled; then
for name in "${verminmax_map_var_names[@]}"; do
local -n slot_verminmax_map_ref=${name:-empty_map}
pkg_debug "all slots in ${name}: ${!slot_verminmax_map_ref[*]}"
pkg_debug "all vmms in ${name}: ${slot_verminmax_map_ref[*]}"
pkg_debug_print "all slots in ${name}: ${!slot_verminmax_map_ref[*]}"
pkg_debug_print "all vmms in ${name}: ${slot_verminmax_map_ref[*]}"
unset -n slot_verminmax_map_ref
done
fi
@ -3143,67 +3109,4 @@ function handle_scripts() {
generate_summary_stub scripts -- 'TODO: review the diffs'
}
# Enables debug logs when specific packages are processed.
#
# It is expected that globals were already sourced, otherwise
# debugging won't be enabled at all.
#
# Params:
#
# @ - package names to enable debugging for
function pkg_debug_enable() {
local -A pkg_set
pkg_set=()
local -a vals
vals=()
local pkg
for pkg; do
if [[ -n ${pkg_set["${pkg}"]:-} ]]; then
continue
fi
pkg_set["${pkg}"]=x
if [[ -n ${DEBUG_PACKAGES["${pkg}"]:-} ]]; then
vals+=( "${pkg}" )
fi
done
if [[ ${#vals[@]} -gt 0 ]]; then
declare -g PKG_AUTO_LIB_DEBUG
join_by PKG_AUTO_LIB_DEBUG ',' "${vals[@]}"
fi
}
# Returns true or false whether any debugging has been enabled.
function pkg_debug_possible() {
local ret=0
[[ ${#DEBUG_PACKAGES[@]} -gt 0 ]] || ret=1
return ${ret}
}
# Disables debug logs to be printed.
function pkg_debug_disable() {
unset PKG_AUTO_LIB_DEBUG
}
# Prints passed parameters if debugging is enabled.
#
# Params:
#
# @ - parameters to print
function pkg_debug() {
if [[ -n ${PKG_AUTO_LIB_DEBUG:-} ]]; then
info "DEBUG(${PKG_AUTO_LIB_DEBUG}): ${*}"
fi
}
# Prints passed lines if debugging is enabled.
#
# Params:
#
# @ - lines to print
function pkg_debug_lines() {
if [[ -n ${PKG_AUTO_LIB_DEBUG:-} ]]; then
info_lines "${@/#/"DEBUG(${PKG_AUTO_LIB_DEBUG}): "}"
fi
}
fi