TL;DR
All job control / crash messages are hidden when they occur within a function. I go into more detail below, but @Barmar has pointed out that this issue can be reproduced by running a crashing binary inside of a function, e.g:
crun() {
/tmp/faulty $1 $2 $3
}
I've defined a function in my .zshrc
to compile & run source code with the function below:
crun() {
local file=$1
shift
local exepath="$(mktemp)"
if [[ $file =~ "\.c$" ]]; then
gcc -g -Wall $file -o $exepath || return $?
else
echo "no filetype detected"
return 126
fi
$exepath "$@"
}
Which can be called in this fashion:
% crun source.cc arg_1 arg_2
This works for normal programs, but has the problem that the shell's job control messages, such as those generated from a segfault, do not appear.
As an example:
% echo 'int main=0;' >> /tmp/faulty.c # a crashing c program
% crun faulty.c
% # no output generated
Whereas the equivalent interactive commands would generate this:
% g++ faulty.c -o /tmp/faulty && /tmp/faulty
[1] 2894 segmentation fault (core dumped) # 🢀 zsh's job control output for SIGSEGV
Is there any way to display these messages for a crashing executable whose path is dynamically calculated? Ideally without writing your own trap/signal handlers + exec
, using sh -c "$exepath $@"
, or writing a totally new system(3)
wrapper entirely)
crun
within the shell itself. How else are you running it when it doesn't work? – Barmar Jul 11 '17 at 0:07crun
inside of another file and running the generated binary withexec
(because the shell's process image is replaced with a program which segfaults, which the parent shell catches it). I included the aside just for some context on the difference between interactive and non-interactive execution and how that can be relevant. – ŹV - Jul 11 '17 at 0:23if
. When I take that out, I get the segmentation fault message from the shell. – Barmar Jul 11 '17 at 0:31crun() { /tmp/faulty; }
and it won't show the messasge. – Barmar Jul 11 '17 at 0:42