File: T0003.debug-trap.sh

package info (click to toggle)
ble.sh 0.4.0~git20250321.d4c812b-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 12,516 kB
  • sloc: sh: 71,367; awk: 1,316; cpp: 750; ansic: 186; javascript: 43; makefile: 35
file content (86 lines) | stat: -rw-r--r-- 2,399 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/bin/bash

bash44_traverse=1

function user-debug-handler {
  local caller=${FUNCNAME[1]}
  [[ $caller == ble-debug-handler ]] &&
    caller=${FUNCNAME[2]}
  echo "trap($*):$caller: $BASH_COMMAND"
}

# * 4.4 以降では最初に DEBUG を設定した関数呼び出しレベルを書き換える?
#   否、関数終了時に上書きする様にしたら良いという事の気がする。
# * 関数を一つ上がったら trap handler を削除
# * trap handler が全てなくなったら解除
# ? yes: 内側の関数で活性化した時に外側の関数でもちゃんと DEBUG trap が発動
#   するのだろうか。

_debug_trap_handlers=()
function ble/trap-debug {
  local depth=$((${#FUNCNAME[@]}-2))
  if [[ $1 != - ]]; then
    _debug_trap_handlers[depth]=$1

    # 再活性化 (extdebug/functrace が設定されていなければ、
    # 設定を行った関数呼び出しレベルでしか DEBUG trap は発生しない)。
    builtin trap ble-debug-handler DEBUG
  else
    unset '_debug_trap_handlers[depth]'
  fi
}
function ble-debug-handler {
  local depth=$((${#FUNCNAME[@]}-1))
  [[ $_ble_trap_suppress || ${FUNCNAME[1]} == trap ]] && return 0
  #echo "ble-debug-handler"
  for ((;depth>=0;depth--)); do
    local handler=${_debug_trap_handlers[depth]-}
    [[ $handler ]] || continue
    eval "$handler"
    return "$?"
  done
}
function ble-return-handler {
  #echo "ble-return-handler"
  local _ble_trap_suppress=1
  local depth=$((${#FUNCNAME[@]}-1))
  ((depth)) || return 0
  if [[ $bash44_traverse && ${_debug_trap_handlers[depth]+set} ]]; then
    _debug_trap_handlers[depth-1]=${_debug_trap_handlers[depth]}
  fi
}

#------------------------------------------------------------------------------

function f3 {
  builtin trap '_ble_trap_suppress=1 ble-return-handler' RETURN
  trap 'user-debug-handler f3' DEBUG
  echo f3
}
function f2 {
  builtin trap '_ble_trap_suppress=1 ble-return-handler' RETURN
  echo f2:1
  f3
  echo f2:2
  trap - DEBUG
}
function f1 {
  builtin trap '_ble_trap_suppress=1 ble-return-handler' RETURN
  trap 'user-debug-handler f1' DEBUG
  echo f1:1
  f2
  echo f1:3
  trap - DEBUG
}

#------------------------------------------------------------------------------

#trap ble-debug-handler DEBUG

# 通常
f1

echo ----------
trap ble-return-handler RETURN
function trap { local _ble_trap_suppress=1; ble/trap-debug "$1"; }
f1