For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > May 2004 > Line number at segfault









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Line number at segfault
Russell Shaw

2004-05-19, 12:32 pm

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

2004-05-19, 12:32 pm

Le 19-05-2004, Russell Shaw <rjshawN_o@s_pam.netspace.net.au> a écrit_:
> 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?


-g flag at compile time ?
Jens.Toerring@physik.fu-berlin.de

2004-05-19, 12:32 pm

Francis <fDONTSPAMgudin@nerreallydontim.net> wrote:[color=darkred]
> Le 19-05-2004, Russell Shaw <rjshawN_o@s_pam.netspace.net.au> a écrit_:

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.Toerring@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Web Surfer

2004-05-20, 9:34 am

[This followup was posted to comp.unix.programmer]

In article <m0asn1-k14.ln1@main.anatron.com.au>,
rjshawN_o@s_pam.netspace.net.au says...
> 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?



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

2004-05-20, 10:39 am

Web Surfer wrote:
> [This followup was posted to comp.unix.programmer]
>
> In article <m0asn1-k14.ln1@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)


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

2004-05-20, 1:33 pm

In article <elnun1-8g3.ln1@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, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Russell Shaw

2004-05-20, 1:33 pm

Barry Margolin wrote:
> In article <elnun1-8g3.ln1@main.anatron.com.au>,
> Russell Shaw <rjshawN_o@s_pam.netspace.net.au> wrote:
>
>
> 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;)
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com