Consider the following script which calculates the sum of the first N
integers recursively:
$ cat >sum.sh <<!
sum() {
[ $1 -eq 0 ] && echo $2 || sum $(($1 - 1)) $(($2 + $1))
}
sum 5
!
Executing the script with the x option gives the following output:
$ sh -x sum.sh
+ sum 5
15
I would expect the recursive calls to be traced, similar to how GNU
bash/sh behaves. With the patch below applied the output is as expected:
$ sh -x sum.sh
+ sum 5
+ [ 5 -eq 0 ]
+ sum 4 5
+ [ 4 -eq 0 ]
+ sum 3 9
+ [ 3 -eq 0 ]
+ sum 2 12
+ [ 2 -eq 0 ]
+ sum 1 14
+ [ 1 -eq 0 ]
+ sum 0 15
+ [ 0 -eq 0 ]
+ echo 15
15
The patch make sure to assigns the TRACE flag to every user-defined
function if the x option is present. The piece of code that led me to
this:
$ sed -n 606,607p /usr/src/bin/ksh/exec.c
old_xflag = Flag(FXTRACE);
Flag(FXTRACE) = tp->flag & TRACE ? true : false;
Index: exec.c
===================================================================
RCS file: /cvs/src/bin/ksh/exec.c,v
retrieving revision 1.68
diff -u -p -r1.68 exec.c
--- exec.c 11 Dec 2016 17:49:19 -0000 1.68
+++ exec.c 12 Jan 2017 20:10:48 -0000
@@ -796,6 +796,9 @@ define(const char *name, struct op *t)
if (t->u.ksh_func)
tp->flag |= FKSH;
+ if (Flag(FXTRACE))
+ tp->flag |= TRACE;
+
return 0;
}