Stack frame info / Back trace printing inside the program instead of using GDB

218 Views Asked by At

we have requirement to "print Back trace info inside the program" if there is issue of SIGSEGV . Based on set of links provided in stack overflow I am using "backtrace_symbols_fd()" with print_trace() function

void print_trace() {
    char pid_buf[30];
    sprintf(pid_buf, "%d", getpid());
    char name_buf[512];
    name_buf[readlink("/proc/self/exe", name_buf, 511)]=0;
    int child_pid = fork();
    if (!child_pid) {
        dup2(2,1); // redirect output to stderr
        fprintf(stdout,"stack trace for %s pid=%s\n",name_buf,pid_buf);
        execlp("gdb", "gdb", "--batch", "-n", "-ex", "thread", "-ex", "bt", name_buf, pid_buf, NULL);
        abort(); /* If gdb failed to start */
    } else {
        waitpid(child_pid,NULL,0);
    }
}

It is not printing the print_trace() info inside the program , it is just printing the SIGSEGV message & signal no.

Can you please suggest what is going wrong inside

0

There are 0 best solutions below