LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   What happens when a segmentation fault occurs (https://www.linuxquestions.org/questions/programming-9/what-happens-when-a-segmentation-fault-occurs-402493/)

rob_of_owensboro 01-12-2006 10:41 AM

What happens when a segmentation fault occurs
 
Most of my software runs detached from the terminal. I redirect stdout and stderr to a log file for troubleshooting purposes. One of my processes had a segmentation fault but an error message did not show up in my log file. I assume the message was generated but not flushed to the file. I am wondering if the exit handler is called when a segmentation fault occurs, as this is where I would expect a flush would be performed on all open file descriptors.

I am unable to find any documentation on the internet describing the exact behavior of a process when a segmentaion fault occurs. I just don't know where to look and various searches for Segmentation Fault returned to many hits to sort through.

I would greatly appreciate a solution to my problem and/or the location of documentation that describes the behavior of processes under this condition.

If it matters my Linux kernel is 2.6.9-5.

Hko 01-12-2006 11:51 AM

A segmentation fault occurs when your program (or a library it uses) tries to access memory that wasn't assigned to your program. Apart from from broken hardware (memory), this is always due to a bug. Common bugs that yield a segmentation fault: writing passed the end of a string, array, or general memory block allocated by malloc(), calloc() or new (C++ only), using of freeing (or deleting in C++) a invalid pointer, etc..

If the shell is configured to dump a core file when a segmentation fault occurs, a file calld "core" will be created in the working directory. This core file contains your program, with all memory it consumed. This can be used with a debugger to see the state of the program's variables at the time of the segmentation fault. See also http://www.lrde.epita.fr/people/demaille/gnuprog2.pdf

zoroaster 01-12-2006 12:16 PM

In my experience, segmentation faults are almost always due to incorrect use of pointers. For instance, using a pointer which has not been initialized, or using a pointer after it has been freed. If this is your software, then use gdb to track down the pointers.

rob_of_owensboro 01-12-2006 12:43 PM

I know what caused the segmentation fault. What I want is to make sure errors like these show up in the log file. If a program is ran from a terminal the Segmentation Fault error will show up on the screen (stout,stderr). Since my programs reassign these file descriptors to a log file I expect the see the error message in my file. I state a possible reason for this in my original post but I don't know this for a fact. When a segmentation fault, devide by zero or any other fatal error occurs I would expect it to show up in my log file. If it does not which obvoiusly is the case in a least one situation (Segmentation Fault), I need to find another way to log this error. The programs I am dealing with run continuously 24 hours a day 7 days a week. I have a process that restarts them automatically if they fall over but without an entry in my log telling me why, it is difficult to trouble shoot a process that may fall over once every few hundred or thousand hours.

schneidz 01-12-2006 01:26 PM

i think you would have to redirect std-error

Code:

cp -aR /oldboot/* /newboot 2>/newboot/error.log
The "2>error.log" tells the OS to redirect the stderror output of cp
into the
error.log file in /newboot.

rob_of_owensboro 01-12-2006 03:21 PM

I found my answer. The default signal handler that handles the SIDSEGV (Segmentation Fault) signal does not flush stdout or stderr before terminating the process. I replaced the default signal handler with one of my own to perform this work for both the SIGSEGV and SIGFPE signals. Thanks to everyone for there efforts. Here is my solution in case someone finds it usefull.

#include <signal.h>
#include <stdio.h>
#include <rtime.h>

static void report_fault(int Signal)
{
switch(Signal)
{
case SIGSEGV: printf("%s - Segmentation Fault\n",Time_Stamp_To_Text(Time_Stamp(),DATE_AND_TIME));

break;

case SIGFPE: printf("%s - Floating PointExeption\n",Time_Stamp_To_Text(Time_Stamp(),DATE_AND_TIME));

break;
}

/* Flush and close all open file pointers. */
abort();
}

int main()
{
int *A=0;

signal(SIGSEGV,report_fault);
signal(SIGFPE,report_fault);
*A=1;

return(0);
}


All times are GMT -5. The time now is 02:09 PM.