LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-12-2006, 10:41 AM   #1
rob_of_owensboro
LQ Newbie
 
Registered: Jan 2006
Location: Owensboro KY
Distribution: RHEL, Fedora, GENTOO
Posts: 6

Rep: Reputation: 0
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.
 
Old 01-12-2006, 11:51 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
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
 
Old 01-12-2006, 12:16 PM   #3
zoroaster
LQ Newbie
 
Registered: Mar 2003
Location: Owen Sound, Canada
Distribution: debian
Posts: 9

Rep: Reputation: 0
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.
 
Old 01-12-2006, 12:43 PM   #4
rob_of_owensboro
LQ Newbie
 
Registered: Jan 2006
Location: Owensboro KY
Distribution: RHEL, Fedora, GENTOO
Posts: 6

Original Poster
Rep: Reputation: 0
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.
 
Old 01-12-2006, 01:26 PM   #5
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-19-live-usb/ aix
Posts: 3,841

Rep: Reputation: 590Reputation: 590Reputation: 590Reputation: 590Reputation: 590Reputation: 590
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.

Last edited by schneidz; 01-12-2006 at 01:29 PM.
 
Old 01-12-2006, 03:21 PM   #6
rob_of_owensboro
LQ Newbie
 
Registered: Jan 2006
Location: Owensboro KY
Distribution: RHEL, Fedora, GENTOO
Posts: 6

Original Poster
Rep: Reputation: 0
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);
}

Last edited by rob_of_owensboro; 01-12-2006 at 03:25 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika Suse/Novell 2 12-02-2005 09:34 AM
segmentation fault tej Linux - Hardware 1 02-08-2005 12:27 AM
segmentation fault pippet Programming 4 01-24-2005 01:02 AM
Segmentation fault sin-x Slackware 2 01-12-2005 03:01 PM
segmentation fault? sgm Programming 1 01-25-2003 08:41 PM


All times are GMT -5. The time now is 08:03 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration