ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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.
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
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.
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.
Last edited by rob_of_owensboro; 01-12-2006 at 12:53 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.