From: sg on
On Nov 3, 9:39 am, Ben Bacarisse <ben.use...(a)bsb.me.uk> wrote:
> sg <gautams.for...(a)gmail.com> writes:
> > Is there any way to print the back trace of the stack programmatically
> > when we encounter SIGSEGV. I have tried using the backtrace() function
> > but I am not able to interpret the out put.
>
> A SIGSEGV might mean that the stack has been messed up, so maybe that is
> the problem.
>
> Have you tried backtrace_symbols_fd?  What is mysterious about the
> output?
>
> --
> Ben.

Hi Ben,

I have tried this and from the output can we point out the place where
exactly or the reason for the SIGSEGV

backtrace() returned 4 addresses
../bt(backtrace_symbols+0x135) [0x80485bd]
../bt [0x42029098]
../bt(__libc_start_main+0x95) [0x42017499]
../bt(backtrace_symbols+0x39) [0x80484c1]

================
#include <stdio.h>
#include <signal.h>
#include <execinfo.h>
#include <stdlib.h>
#include <unistd.h>


void sighandlesegv(int sig)
{
int j, nptrs;
#define SIZE 1000
void *buffer[100];
char **strings;

nptrs = backtrace(buffer, SIZE);
printf("backtrace() returned %d addresses\n", nptrs);

/* The call backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO)
would produce similar output to the following: */

strings = backtrace_symbols(buffer, nptrs);
if (strings == NULL) {
perror("backtrace_symbols");
exit(EXIT_FAILURE);
}

for (j = 0; j < nptrs; j++)
printf("%s\n", strings[j]);

free(strings);
exit(1);
}

int main(int argc,char **argv)
{
int ret,i;
char *ptr;

signal(SIGSEGV,sighandlesegv);
//for(i = 0;i < 5 ;i++)
sleep(1);
*ptr = 100;
}
================

Thanks & Regards,
-SG
From: Ben Bacarisse on
sg <gautams.forums(a)gmail.com> writes:
<snip>
> I have tried this and from the output can we point out the place where
> exactly or the reason for the SIGSEGV
>
> backtrace() returned 4 addresses
> ./bt(backtrace_symbols+0x135) [0x80485bd]
> ./bt [0x42029098]
> ./bt(__libc_start_main+0x95) [0x42017499]
> ./bt(backtrace_symbols+0x39) [0x80484c1]
>
> ================
> #include <stdio.h>
> #include <signal.h>
> #include <execinfo.h>
> #include <stdlib.h>
> #include <unistd.h>
>
>
> void sighandlesegv(int sig)
> {
> int j, nptrs;
> #define SIZE 1000
> void *buffer[100];
> char **strings;
>
> nptrs = backtrace(buffer, SIZE);
> printf("backtrace() returned %d addresses\n", nptrs);
>
> /* The call backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO)
> would produce similar output to the following: */
>
> strings = backtrace_symbols(buffer, nptrs);
> if (strings == NULL) {
> perror("backtrace_symbols");
> exit(EXIT_FAILURE);
> }
>
> for (j = 0; j < nptrs; j++)
> printf("%s\n", strings[j]);
>
> free(strings);
> exit(1);
> }
>
> int main(int argc,char **argv)
> {
> int ret,i;
> char *ptr;
>
> signal(SIGSEGV,sighandlesegv);
> //for(i = 0;i < 5 ;i++)
> sleep(1);
> *ptr = 100;

This may be at least part of the problem. You are causing a
segmentation violation my messing up the stack. Does the output look
any more reasonable if you just raise the signal or provoke it using a
read rather than a write?

> }

In general, you can't be sure of anything after SIGSEGV. What is the
problem to which you think backtrace() is the solution?

--
Ben.
From: Jens Thoms Toerring on
sg <gautams.forums(a)gmail.com> wrote:
> On Nov 3, 9:39 am, Ben Bacarisse <ben.use...(a)bsb.me.uk> wrote:
> > sg <gautams.for...(a)gmail.com> writes:
> > > Is there any way to print the back trace of the stack programmatically
> > > when we encounter SIGSEGV. I have tried using the backtrace() function
> > > but I am not able to interpret the out put.
> >
> > A SIGSEGV might mean that the stack has been messed up, so maybe that is
> > the problem.
> >
> > Have you tried backtrace_symbols_fd?  What is mysterious about the
> > output?
>
> I have tried this and from the output can we point out the place where
> exactly or the reason for the SIGSEGV

> backtrace() returned 4 addresses
> ./bt(backtrace_symbols+0x135) [0x80485bd]
> ./bt [0x42029098]
> ./bt(__libc_start_main+0x95) [0x42017499]
> ./bt(backtrace_symbols+0x39) [0x80484c1]

This gives you the file names and the address. Now you can use
the addr2line utility to get something easier to read from it.
E.g.

addr2line -e bt -f 0x42029098

will print out the function you were in at the line

../bt [0x42029098]

as well as the line number in the source file for bt (assuming
that you compiled with debugging info included).You won't get
that information directly from the backtrace_symbol() function.
But you could for example fork of addr2line (with it's input and
output redirected to pipes), pass the addresses to it and read
back its output and then print that out.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt(a)toerring.de
\__________________________ http://toerring.de