I understand that dash is a "model" shell - it was written recently and well and it only implements what is required of POSIX shell. I looked at the dash source code to understand how "tracing" of a program such as shell, is done.

"Tracing" of dash is performed by compiling it with DEBUG defined and invoking with -o debug. When you do that, a file trace is opened up with fopen to which information is dumped about what dash is doing when it runs.

Except the whole mechanism, appears to me, does not work, even in this "perfectly written" shell. If I have this in a shell script:

exec 3>&1

immediately all information after that, is very likely no longer dumped to file trace but winds up on stdout. That is because fopen calls open and that likely returns file descriptor 3.

This is not a contrived example. There are real-world commonly used scripts that have lines like


(I saw that in 2 autotools configure scripts: for IP Infusion and Kerberos software).

So how can a shell be debugged/traced, when it has to run scripts like these?

(I am not looking for tools outside shell - I want to know what is the correct way to trace, inside a program such as shell. Or is it not possible in general?)

  • "strace -f" may or may not be helpful – barrycarter Jan 10 '16 at 22:42
  • @barrycarter yes, thank you, good point, but... I want to do it inside the shell - clarified the question. Basically, I want to know, if God wrote a shell, how would they implement tracing. Or is it not possible in general due to the above problem. – user322908 Jan 10 '16 at 22:50

You could use a socket (syslogd does this for example). But then how would you protect against commands like

LD_PRELOAD=my_bogus_socket_funcs.so somecommand

Or you could use dup2 to change the file descriptor to a higher number. But that is no guarantee, maybe only protection against script mistakes (such as using 3 instead of 2 in redirects).

If the script wants to change tracing behaviour on purpose, I believe there is not much a shell could (or should) do.

| improve this answer | |
  • it is not that "script wants to change tracing on purpose", these scripts do not. I am not looking to run rogue scripts, just poorly written ones. The script uses 3 for some other purpose, it has that hardcoded, and it does not know, that somebody may already have that open. – user322908 Jan 10 '16 at 23:16
  • yes I think dup2 to a "magic number file descriptor" is probably my best bet... thank you – user322908 Jan 10 '16 at 23:20
  • 1
    In that particular case either approach would have worked. My point is that unless you know the mistakes that are in the script, there is really no way of guaranteeing that they would run. Scripts may have valid reasons to do things like overriding socket functions (I have used it myself). – Krumelur Jan 10 '16 at 23:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.