Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bash-compatible stack traces (trap ERR, BASH_ARGV, extdebug) #708

Open
andychu opened this issue Apr 15, 2020 · 2 comments
Open

Bash-compatible stack traces (trap ERR, BASH_ARGV, extdebug) #708

andychu opened this issue Apr 15, 2020 · 2 comments

Comments

@andychu
Copy link
Contributor

andychu commented Apr 15, 2020

Probably won't because we don't have trap ERR yet

https://lobste.rs/s/ajoaje/first_two_statements_your_bash_script#c_u6hdcf

trap 'stacktrace "Error trapped rc=${PIPESTATUS[@]}, see trace above"' ERR

stacktrace() {
  local frame="${#FUNCNAME[@]}"
  echo >&2 "Stacktrace:"
  while [[ "${frame}" -gt 1 ]]; do
    ((frame--))
    echo >&2 "  File ${BASH_SOURCE[$frame]}#L${BASH_LINENO[$((frame - 1))]} (in ${FUNCNAME[$frame]}())"
  done
  if [[ "$#" -gt 0 ]]; then
    echo >&2 "$1"
  fi
}
@akinomyoga
Copy link
Collaborator

akinomyoga commented Apr 17, 2020

As for the implementation of stacktrace, the implementation by ble.sh might be more useful. It supports BASH_ARGV (set by extdebug).

_ble_util_stackdump_title=stackdump
function ble/util/stackdump {
  ((bleopt_internal_stackdump_enabled)) || return 1
  local message=$1
  local i nl=$'\n'
  local message="$_ble_term_sgr0$_ble_util_stackdump_title: $message$nl"
  local iarg=$BASH_ARGC args= extdebug=
  shopt -q extdebug 2>/dev/null && extdebug=1
  for ((i=1;i<${#FUNCNAME[*]};i++)); do
    if [[ $extdebug ]] && ((BASH_ARGC[i])); then
      args=("${BASH_ARGV[@]:iarg:BASH_ARGC[i]}")
      ble/array#reverse args
      args=" ${args[*]}"
      ((iarg+=BASH_ARGC[i]))
    else
      args=
    fi
    message="$message  @ ${BASH_SOURCE[i]}:${BASH_LINENO[i-1]} (${FUNCNAME[i]}$args)$nl"
  done
  ble/util/put "$message"
}

@andychu andychu added the stdlib label Apr 19, 2020
@andychu andychu changed the title Make this stack trace snippet work in Oil Bash-compatible stack traces (trap ERR, BASH_ARGV, extdebug) Apr 21, 2020
@andychu
Copy link
Contributor Author

andychu commented May 10, 2020

Note #709 has a code snippet with set -E / errtrace

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants