Given the following code:
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>
int main(int ac, char** av) {
int status;
pid_t cpid = fork();
if(0 == cpid) { /* Child */
return *(volatile int*) 0; /* exits with signal 11 */
} else { /* Parent */
do {
waitpid(cpid, &status, WUNTRACED);
if(WIFSIGNALED(status)) {
printf("\nChild exited with signal %d\n\n", WTERMSIG(status));
return 0;
}
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
printf("\nChild exited normally\n\n");
}
return 0;
}
I get the expected result running the app from Terminal:
$ ./Fork4GdbTests.exe
Child exited with signal 11
Running the app within LLDB (or GDB), strangely, I get:
$ lldb ./Fork4GdbTests.exe
(lldb) target create "./Fork4GdbTests.exe"
Current executable set to './Fork4GdbTests.exe' (x86_64).
(lldb) r
Process 46815 launched: './Fork4GdbTests.exe' (x86_64)
Child exited normally
Process 46815 exited with status = 0 (0x00000000)
My Makefile looks like this (used for Cygwin, also):
CC = gcc
CFLAGS = -Wextra -Wall -Werror -Wno-unused-parameter -g
.PHONY: all clean
all: Fork4GdbTests.exe
%.exe: %.o
$(CC) $(CFLAGS) $? $(LIBS) -o [email protected]
clean:
rm -f *.o *.exe *.stackdump;
In Cygwin, I get the expected result both running from the command prompt and in the debugger. Similar behavior occurs for all kinds of other signals, such as SIGFPE or any signals sent to the child by means of kill().
What is going on?