Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Line number at segfault

5 views
Skip to first unread message

Russell Shaw

unread,
May 19, 2004, 11:10:45 AM5/19/04
to
Hi,
I'm using gcc 3.3.3 on debian.

Is there any way to make the segfault of a C program
print out the file and line number it happened at?

Francis

unread,
May 19, 2004, 11:30:30 AM5/19/04
to

-g flag at compile time ?

Jens.T...@physik.fu-berlin.de

unread,
May 19, 2004, 11:40:35 AM5/19/04
to
Francis <fDONTSP...@nerreallydontim.net> wrote:
> Le 19-05-2004, Russell Shaw <rjshawN_o@s_pam.netspace.net.au> a écrit :
>> I'm using gcc 3.3.3 on debian.
>>
>> Is there any way to make the segfault of a C program
>> print out the file and line number it happened at?

If you can't load the core file into your debugger you can install
a signal handler for SIGSEGV and then use the backtrace() and
backtrace_symbols_fd( ) functions (see the info pages for libc)
to stderr (i.e. file descriptor 2). But of course you need to have
to program compiled with the '-g' option, otherwise all you can
get is a list of addresses, but no line numbers. And also note
that optimization can mess up the results.

Regards, Jens
--
\ Jens Thoms Toerring ___ Jens.T...@physik.fu-berlin.de
\__________________________ http://www.toerring.de

Web Surfer

unread,
May 20, 2004, 8:51:58 AM5/20/04
to
[This followup was posted to comp.unix.programmer]

In article <m0asn1-...@main.anatron.com.au>,
rjshawN_o@s_pam.netspace.net.au says...


Compile your program with the debugging option "-g".
Aftedr your program dies with a "segfault", you can use a symbolic
debugger to display the "status" of the [program when it died
(ie. including the line of cide that caused the problem)

Russell Shaw

unread,
May 20, 2004, 9:15:58 AM5/20/04
to

Hi,
I have -g enabled, but found i can save a lot of time just by
putting printf("Line:%d\n",__LINE__) everywhere in the code
so i can pinpoint the segfault. Because it's new code i'm
writing, i fix dozens of segfaults in a few minutes (it has
a shift-reduce parser, symbol tables, and other fiddly bits).
Getting the program to print the segfault line number would
save running gdb in the frequent compile-run-crash cycle.
I'll try Jens idea.

Barry Margolin

unread,
May 20, 2004, 12:14:46 PM5/20/04
to
In article <elnun1-...@main.anatron.com.au>,
Russell Shaw <rjshawN_o@s_pam.netspace.net.au> wrote:

> Because it's new code i'm
> writing, i fix dozens of segfaults in a few minutes (it has
> a shift-reduce parser, symbol tables, and other fiddly bits).
> Getting the program to print the segfault line number would
> save running gdb in the frequent compile-run-crash cycle.

Why do you have so many segfaults in the first place? Do you have a
chronic problem initializing pointers?

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

Russell Shaw

unread,
May 20, 2004, 12:33:11 PM5/20/04
to
Barry Margolin wrote:
> In article <elnun1-...@main.anatron.com.au>,
> Russell Shaw <rjshawN_o@s_pam.netspace.net.au> wrote:
>
>>Because it's new code i'm
>>writing, i fix dozens of segfaults in a few minutes (it has
>>a shift-reduce parser, symbol tables, and other fiddly bits).
>>Getting the program to print the segfault line number would
>>save running gdb in the frequent compile-run-crash cycle.
>
> Why do you have so many segfaults in the first place? Do you have a
> chronic problem initializing pointers?

It's always problems with variables being assigned via a pointer
in response to a parser action, and other lesser problems such as
just typing the wrong thing or overwriting an array limit.
Parsers can have many small pieces that are only used when
parsing certain input. It's a shift-reduce parser that is
dynamically configurable, and can even change its semantics/syntax
from commands in the file being read. I must use assert() more;)

0 new messages