[prog] This segmetation fault makes no sence to me.

Meredydd meredydd at everybuddy.com
Fri May 30 12:59:25 EST 2003


The first step when encountering a segfault is to acquire a backtrace from a 
debugger. Under Linux, the standard is "gdb" - you'll have it installed.

When you're compiling, make sure your command-line includes "-g" (-g3 or -ggdb 
is in fact unnecessary - just "-g" turns on everything). This produces a 
slightly larger executable that usual, but won't change any of its behaviour. 
For example:

%.o: %.C
        c++ -g -c $<

To run a program under gdb, start the program by executing "gdb programname". 
You will then see a bunch of messages and a "(gdb)" prompt. Type "run", and 
your program will start. If a signal is received (such as signal 11, 
segmentation fault), you'll see a message saying so, and you'll get your 
"(gdb)" prompt back. Type "bt" (short for "backtrace", which is also valid), 
or "bt full" (which gives you more information). This will tell you the exact 
line where the error occurred.

As an example, here is a program which will generate a segfault:

#include <stdio.h>

main()
{
  char * s;
  s=NULL;
  s[2]='e';
}

This is me debugging it with gdb:

meredydd at amethyst:~/programming/test$ pico -w seggie.c
meredydd at amethyst:~/programming/test$ gcc -g seggie.c -o seggie
meredydd at amethyst:~/programming/test$ ./seggie
Segmentation fault
meredydd at amethyst:~/programming/test$ gdb ./seggie
GNU gdb 5.3
Copyright 2002 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-slackware-linux"...
(gdb) run
Starting program: /home/meredydd/programming/test/seggie

Program received signal SIGSEGV, Segmentation fault.
0x08048311 in main () at seggie.c:7
7         s[2]='e';
(gdb) bt full
#0  0x08048311 in main () at seggie.c:7
        s = 0x0
#1  0x4003abb4 in __libc_start_main () from /lib/libc.so.6
No symbol table info available.
(gdb) list
7         s[2]='e';
8       }
9
(gdb) quit
The program is running.  Exit anyway? (y or n) y
meredydd at amethyst:~/programming/test$

...and there we have it. The output from "bt full" looks cryptic, but it tells 
me the line of code where it happened, the line where it happened, and the 
value of all my local variables (everything on the stack). I can see that the 
pointer I just tried to write to is NULL (0x0), and that that is my problem.

GDB is a powerful piece of software, and there's a lot you can do with it. If 
you use KDevelop or some other IDE, I'd also recommend checking out its 
internal debugging system, which generally speeds up the process in my 
experience, as you can just click on a backtrace and be taken to the code 
itself. Of course, many (indeed, around here it seems most) people aren't 
that keen on the eye-candy, and find it harder to use, which is why I've 
given instructions for GDB here.

Oh yes, and I have been splurging a bit round here recently. Time to back off 
and let other people have a go, methinks.

Meredydd
--
Everybuddy project maintainer
http://www.everybuddy.com/

MSN:	blip109 at hotmail.com
AIM:		blip109
Yahoo:	modula7



More information about the Programming mailing list