kcov icon indicating copy to clipboard operation
kcov copied to clipboard

bash: Support korn shell as well

Open SimonKagstrom opened this issue 8 years ago • 11 comments

PS4 seems to be present, e.g., http://www.livefirelabs.com/unix_tip_trick_shell_script/apr_2003/04282003.htm, so this should be technically possible.

SimonKagstrom avatar Apr 22 '16 08:04 SimonKagstrom

It would be nice to support zsh as well. Zsh support is the same as ksh support technically. So I comment here instead of creating a new issue.

The ksh and the zsh has PS4, but they does not have a feature like BASH_XTRACEFD. However, it can be implemented using the DEBUG trap they have.

Current kcov not support ksh and zsh officially, But I managed to get it working with ksh and zsh.

Here is DEBUG trap handler for ksh and zsh. Ksh has some bugs, so it needs a workaround. See this issue for details.

# For ksh
trap '(echo "kcov@${.sh.file}@${LINENO}@" >/dev/fd/$KCOV_BASH_XTRACEFD); IFS=$IFS' DEBUG

# For zsh
trap '
  if [ ${#funcsourcetrace[@]} -gt 0 ]; then
    echo kcov@${funcsourcetrace[1]%:*}@$((${funcsourcetrace[1]##*:}+LINENO))@
  else
    echo kcov@${0}@${LINENO}@
  fi >&$KCOV_BASH_XTRACEFD
' DEBUG

BTW, I prefer using DEBUG trap instead of PS4 for coverage. Because trace information (statement to execute) is not needed for coverage. And its output and parsing causes a slow down.

ko1nksm avatar May 12 '20 23:05 ko1nksm

Wow! Nice work!

So in order to add this, I guess the bash engine would have to recognise ksh and zsh "hash bangs" and use the specific helpers for them? Sounds like it should be a fairly easy change.

SimonKagstrom avatar May 13 '20 06:05 SimonKagstrom

Hi @SimonKagstrom,

You should not use "hash bangs". Because /bin/sh may be bash or may be dash. Even zsh and ksh scripts may use #!/bin/sh.

A better way is use shell script to detect the shell. You can also put all debug handlers to one file.

if [ "${BASH_VERSION:-}" ]; then
  trap ... DEBUG # for bash
elif [ "${KSH_VERSION:-}" ]; then
  trap ... DEBUG # for ksh
elif [ "${ZH_VERSION:-}" ]; then
  trap ... DEBUG # for zsh
fi

Another problem is BASH_ENV. You should use different methods for ksh and zsh.

  • ksh: Use ENV environment variable instead of BASH_ENV and run script by ksh with -E option.
  • zsh: Zsh loads $ZDOTDIR/.zshenv automatically. Therefore create it to temporary directory (e.g. out-dir), and setting that directory path to ZDOTDIR.

Currently kcov only detects the bash version, but it needs to detect the shell type in addition.

For example. (Use this script instead of bash -c 'echo "$BASH_VERSION")

if [ "${BASH_VERSION:-}" ]; then
  echo "bash $BASH_VERSION"
elif [ "${KSH_VERSION:-}" ]; then
  echo "ksh $KSH_VERSION"
elif [ "${ZH_VERSION:-}" ]; then
  echo "zsh $ZH_VERSION"
fi

And after supporting zsh and ksh. Kcov options --bash-* are not appiciate name. :smile:

ko1nksm avatar May 13 '20 23:05 ko1nksm

It uses the existence of hash bangs to find out what "engine" it should use (as opposed to compiled programs and Python scripts), so it needs to use them. However, it's easy enough to add the ksh and zsh hash bangs.

Anyway, after that I guess the best way is to read the /bin/sh symlink to find out what shell it is if it's not explicitly given in the hash bang? And then adding your ksh/zsh-specific debug handlers to the bash-helper.sh (renamed to shell-helper.sh) or something.

SimonKagstrom avatar May 14 '20 09:05 SimonKagstrom

I've started work on a branch issue-137-ksh-zsh-support with your suggestions, but I'll work sporadically on it for now.

SimonKagstrom avatar May 16 '20 19:05 SimonKagstrom

I've updated the branch now, with some sort of basic first working version. At least I think it's working. There are some hardcoded paths (/bin/zsh, /bin/ksh) which are not good.

SimonKagstrom avatar Jun 06 '20 13:06 SimonKagstrom

That is great! I will try.

Oh... sorry. I've made a mistake.

elif [ "${ZH_VERSION:-}" ]; then
  echo "zsh $ZH_VERSION"
fi

Wrong: ZH_VERSION Correct: ZSH_VERSION

ko1nksm avatar Jun 08 '20 15:06 ko1nksm

You're the zsh expert so I just copied your code blindly :-)

SimonKagstrom avatar Jun 08 '20 16:06 SimonKagstrom

Any news on that?

pdesjardins90 avatar Feb 23 '21 03:02 pdesjardins90

Unfortunately, I haven't been working on this. Daytime work plus life has intervened, so not much kcov work the last year.

I did update the branch were the work was started on though, so if you want to try it out it's in the

issue-137-ksh-zsh-support

branch. It's not finished, but at least via some basic testing it worked.

SimonKagstrom avatar Feb 23 '21 19:02 SimonKagstrom

No problem at all, was just wondering if you were still considering it! I'll try to have a look soon

pdesjardins90 avatar Feb 23 '21 21:02 pdesjardins90