Re: [ILUG] Segmentation Fault

From: Padraig Brady (padraig at domain antefacto.com)
Date: Thu 13 Sep 2001 - 15:00:04 IST


Aidan Kehoe wrote:

> Ar an 13u la de mi 9, scriobh Michael Treacy :
> >
> > Ok, here's the question: in general, what causes segmentation faults?
> >
>
>The address space of a program is divided into segments; only some of
>those segments, notably the stack (where local variables are stored)
>and the heap (where malloc'ed ones (in C) are), are writable. Try to
>write anywhere else (e.g. to a constant string, to the address 0, to
>the address -1) and you have a segmentation fault. So, it means you're
>writing where you're not supposed to.
>
An interesting gotcha those const strings. I.E. if you declare.
char* string="This can't be modified";
string[0]='t'; /* seg fault */
Note allowing this can be quite useful and is sometimes assumed
(especially by older code) and so some compilers allow it by default.
With gcc you have to explicitly allow it using -fwriteable-strings
Note finding this error can be hard when you cast/pass pointers
to these const string around and so gcc gives another option:
-Wwrite-strings (which isn't included by -Wall) to help you.
anyway getting back on topic...

>
>
> > I've gone through the second fortran program, even added in print commands
> > to see how far down the program, I was getting, but the first one isn't even
> > reached.
>
I've never used fortran but I thinkt he following will work:
Use gdb on the core file generated by the segfault. If it just says
segmentation fault and not segmentation fault (core dumped),
then you probably don't have write permissions to the working
directory or ulimit is set too low. To remove ulimits do:
ulimit -c unlimited.

Then on the generated core file do:
gdb -c core my_fortran_executable

Inside gdb type:
bt
which will generate a backtrace and you can see
where the program died.

>
>Does it reach it when you're not trying to read this input file? Have
>you tried reading from another input file? I'd advise trying GDB,
>except it doesn't seem to like fortran, unfortunately;
>
>(gdb.info)
>
> GDB can be used to debug programs written in Fortran, although it
>does not yet support entering expressions, printing values, or similar
>features using Fortran syntax. It may be necessary to refer to some
>variables with a trailing underscore.
>
>Chris Higgins' strace suggestion is probably the way to go. If you're
>not on Linux, investigate ktrace (some BSD) and truss (SVR4).
>
Padraig.



This archive was generated by hypermail 2.1.6 : Thu 06 Feb 2003 - 13:12:08 GMT