LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 02-17-2015, 10:49 AM   #1
dogpatch
Member
 
Registered: Nov 2005
Location: Central America
Distribution: Mepis, Android
Posts: 490
Blog Entries: 4

Rep: Reputation: 238Reputation: 238Reputation: 238
catch segmentation fault


Am facing the worst bug i've ever encountered. It's in a gcc program which analyzes several grids of binary data read from a file. I can read and analyze each single grid, but when i try to read hundreds or sometimes just dozens of grids in succession, at some point the program crashes with a segmentation fault. Obviously, I'm failing to initialize some variable or array netween grids, but can't see where. to save my soul.

gdb doesn't seem to be available to me, and, anyway, it would be a task beyond my patience to step in a debugger through hundreds of grids, each one of which involves some very heavy looping, only to find that the memory corruption under a debugger may be very different than without it.

My question is: is there a gcc compile option or some other way that i could somehow catch the segmentation fault and dump to standard output or to a file something to indicate where the fault is occurring?
 
Old 02-17-2015, 11:07 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,860
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
As a start, you could try Electric Fence: it's simply adding option -lefence to linkage
 
Old 02-17-2015, 02:20 PM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
You could also try the -fbounds-check option in gcc to crash (with applicable debugging information) if you step outside of the bounds of an array. It WILL slow down the code significantly, but it can be very helpful for catching that occasional indexing error that ultimately leads to a seg fault.

A brute force method would be to start commenting out chunks of code until the seg fault disappears, and then narrow in on which section of code is causing it. This is painful and time consuming, but it can be used as a last resort when gdb, valgrind, and array bounds checking all fails.

Last edited by suicidaleggroll; 02-17-2015 at 02:22 PM.
 
2 members found this post helpful.
Old 02-17-2015, 05:30 PM   #4
dogpatch
Member
 
Registered: Nov 2005
Location: Central America
Distribution: Mepis, Android
Posts: 490

Original Poster
Blog Entries: 4

Rep: Reputation: 238Reputation: 238Reputation: 238
I neglected to mention that I'm using a rather old development platform, and do not have Electric Fence. I may try the -fbounds-check option, just to familiarize myself with it for future use.

Meanwhile, after looking at NevemTeve's suggestion and before seeing suicidaleggroll's comment, i was already using the brute force method and have located the offending code, a recently added array for which i was failing to reset the index. A noobie oversight that i was unable to see at first.

Thanks, and i will mark this as solved.
 
Old 02-17-2015, 11:27 PM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,860
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Note: Electric Fence available in source and can compiled in many Unix platform.
http://linux.softpedia.com/get/Progr...nce-3305.shtml
 
  


Reply

Tags
catch, debug, gcc, segmentation fault



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
Segmentation fault jefsa Linux - Newbie 8 06-26-2013 12:25 PM
segmentation fault simasimon Slackware 4 05-09-2006 08:09 PM
ls -la Segmentation Fault halturata Linux - Newbie 2 05-09-2006 01:31 AM
Segmentation fault multios Slackware 2 05-04-2006 10:58 AM
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 09:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:57 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration