On Friday, February 03, 2012 10:31:44 AM David Korn wrote:
> cc: [email protected]
> Subject: Re: [ast-users] [ksh93] Global `set -x' ?
> --------
> 
> > th bash, `set -x' is global so users can debug a script by `bash -x
> > foo.sh' and every function will be traced. But with ksh, `set -x' does
> > not affect functions defined in the `function NAME' syntax which I
> > think is not convenient. Seems like ksh treats `set -x' the same way as
> > a trap.
> With bash, what if I don't want to see the trace for functions that
> I already know work ok.  How can I disable the tracing of specific
> functions.
> 
> With ksh93, you can enable the tracing of individual functions with
>       typeset -ft name ...
> or all functions with
>       .sh.fun.set() { set -x;}
> 
> David Korn
> [email protected]
> _______________________________________________
> ast-users mailing list
> [email protected]
> https://mailman.research.att.com/mailman/listinfo/ast-users

The "set" options are always global as far as I'm aware. In Bash, "function  
tracing" has the meaning that the DEBUG and RETURN traps are inherited by 
functions, otherwise they are only active in the scope where they're set 
(except with set -T).

Simulating "set -x" using the DEBUG trap should be pretty trivial, except under 
Bash's definition of "PS4" I have no clue what it means by "levels of 
indirection". It seems it shows how many command substitutions deep we are (not 
subshells, not function calls). Anyway this is an approximation. In both of 
these cases only the second call to "f" shows up, and only in the second, every 
"g". "declare -ft" toggles them on.

#!/usr/bin/env bash
FUNCNEST=100
trap 'if ((${#FUNCNAME[@]})); then printf -- "%.s${PS4::1}" "${FUNCNAME[@]}"; 
echo " $BASH_COMMAND"; fi' DEBUG

f() {
    (( ++n % 5 )) && f
    g
};

g() {
    false
}

f
declare -ft f
f

Outputs:
+ f
+ (( ++n % 5 ))
+ f
++ f
++ (( ++n % 5 ))
++ f
+++ f
+++ (( ++n % 5 ))
+++ f
++++ f
++++ (( ++n % 5 ))
++++ f
+++++ f
+++++ (( ++n % 5 ))
+++++ g
++++ g
+++ g
++ g
+ g

And with "declare -ft f g":
+ f
+ (( ++n % 5 ))
+ f
++ f
++ (( ++n % 5 ))
++ f
+++ f
+++ (( ++n % 5 ))
+++ f
++++ f
++++ (( ++n % 5 ))
++++ f
+++++ f
+++++ (( ++n % 5 ))
+++++ g
++++++ g
++++++ false
++++ g
+++++ g
+++++ false
+++ g
++++ g
++++ false
++ g
+++ g
+++ false
+ g
++ g
++ false

A better way with less  overhead might be to "exec {BASH_XTRACEFD}>/dev/null", 
and then then point BASH_XTRACEFD to the tty only where needed. Unfortunately 
using this method you'll obviously end up also seeing any functions called by 
the "traced" function without taking steps to prevent it. 

-- 
Dan Douglas

Attachment: signature.asc
Description: This is a digitally signed message part.

_______________________________________________
ast-users mailing list
[email protected]
https://mailman.research.att.com/mailman/listinfo/ast-users

Reply via email to