0

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

ks1322
  • 33,961
  • 14
  • 109
  • 164
  • 1
    "I am using backtrace_symbols_fd()" - no, you are not using it. – ks1322 Aug 13 '20 at 12:31
  • @ks1322 : using backtrace_symbols_fd() in the signal handler function, once backtrace_symbols_fd () is called , next stamen tent it prints print_trace() function. – Gsatish Kumar Aug 13 '20 at 17:39
  • 1
    You can't safely use `fprintf()` in signal handler function, see https://stackoverflow.com/q/9547949/72178. – ks1322 Aug 13 '20 at 18:05
  • If the segmentation violation was due to a stack overflow, print_trace might not be able to allocate those char arrays. If that's the case, it might be worth using `sigaltstack`. – Mark Plotnick Aug 13 '20 at 19:49

0 Answers0