I am trying to understand why the behaviour of segfault messages can differ depending on execution environment.
I have the following C code that I am using to deliberately trigger a segfault:
#include <stdio.h>
int main() {
int* p = NULL;
printf("%dn", *p);
}
On my local Linux, when I compile this code and execute the resulting binary, I see the segfault message even when I redirect both stdout and stderr to /dev/null
:
$ ./segfault > /dev/null 2>&1
Segmentation fault (core dumped)
$
On a Docker container in Jenkins, I compile and run the exact same C code. I run the binary using the sh
step like this:
sh './segfault > /dev/null || true'
sh './segfault > /dev/null 2>&1 || true'
Here is the output in Jenkins:
+ ./segfault
Segmentation fault (core dumped)
+ true
+ ./segfault
+ true
As you can see, the segfault message is being written to stderr in Jenkins (you can see this from how when I don’t redirect to stderr, the message appears, but if I do redirect to stderr, the message does not appear). But the segfault message is not written to stderr on my local Linux.
I verified that redirecting stdout and stderr to /dev/null
when running the Docker container interactively in my local Linux system also resulted in the segfault message appearing in the container’s interactive shell output:
$ gcc segfault.c
$ ./a.out >/dev/null 2>&1
Segmentation fault (core dumped)
I looked at the Java source code for the sh
step, but nothing stood out to me as a cause for this different behaviour in Jenkins (but it is very possible I missed something).
My local Linux is Ubuntu 20.04. The Docker image I used on Jenkins is the gcc image. Both use x86_64 architecture.
On my local Linux, here is my kernel release and kernel version:
$ uname -rv
5.13.0-30-generic #33~20.04.1-Ubuntu SMP Mon Feb 7 14:25:10 UTC 2022
This exactly matches the kernel release and kernel version of the gcc image (at the time of writing).
My local Linux has gcc 9.4.0, and (at the time of writing) the gcc image has gcc 12.2.0.
Why is the behaviour different in Jenkins versus locally?
Is the cause one of the following? Or is it something else?
- Difference between Docker containers and non-Docker Linux
- Difference in gcc version I compile with
- Some magic Jenkins thing
2
Answers
When we use NULL pointer, the program receiving signal SIGSEGV (segmentation fault).
I have updated the code to call "mysig" when segmentation fault happens.
Once happened, it is making default function to create core dump file.
Once receiving that signal, it is
Above program will create a core file.
Using the core file, we can analyse where the program crashed using gdb.exe/gdb/dbx based on your operating system.
gdb tutorial online:
Sample program:
Sample output:
It’s the shell from which
./segfault
is invoked. If you switch todash
on your computer and run the same command you won’t see the Segmentation fault message there either.