1

Working on Linux am trying to build a Crash Handler function - so as before crashing we get stack trace in log file - done some study and came to know,its done either using -g/-rdynamic Flags to generate debug info then use glibc backtrace / backtrace_symbols_fd functions to get stack trace ...

Its not much of use as in my app, am not allowed to use -g/-rdynamic flags .. so reading further reading

I have build the following code which is not working as expected and in generated log file it ends in following error :

(gdb) Hangup detected on fd 0
error detected on stdin
A debugging session is active.

Inferior 1 [process 6625] will be detached.
Quit anyway? (y or n) [answered Y; input not from terminal]
Detaching from program: /u/vivek/demo/testprg, process 6625

Code: Register a Crash Handler function at startup as -

struct sigaction act;
memset(&act, 0, sizeof (act));
act.sa_sigaction = ProcessCrash;
sigfillset (&act.sa_mask);
act.sa_flags = SA_SIGINFO;
sigaction (SIGSEGV, &act, NULL);

Following function called when my program receives SIGSEGV signal -

void ProcessCrash( int signal, siginfo_t * siginfo, void *context)
{
    cerr <<"** ProcessCrash –Handler Function**" << endl;
       ucontext_t *uc = (ucontext_t *)context;


       if (signal == SIGSEGV)
              fprintf(stderr, "\nGot signal %d, faulty address is %p, "
                     "from %p\n\n", signal, siginfo->si_addr, 
                            uc->uc_mcontext.gregs[REG_RIP]);  // REG_EIP is changed to REG_RIP for –m64 arch.
       else
              fprintf(stderr, "Got signal %d#92;\n", signal);

      // [[ using GDB to pring the stack trace

       char gdb[516];

    // command 1
    //sprintf(gdb, "echo 'where\ndetach' | gdb -q %.256s -pid %d > gdbTest.dump", program_invocation_name, getpid());

    // command 2
    sprintf(gdb, "(echo \"source Trace.txt\";cat) | gdb -q %.256s -pid %d > gdbTest.dump", program_invocation_name, getpid());

    fprintf(stderr, "\n** GDB Command-> %s \n", gdb);
    // output of above is 
    //  ** GDB Command-> (echo "source Trace.txt";cat) | gdb -q /u/vivek/demo/testprg -pid 6625 > gdbTest.dump

    // Run Either command 1 or command 2  but we get the same result
        system(gdb);

      // GDB test ]]


       // Produce a core dump 
       abort();

       return; 
}

Trace.txt contents:

set logging on
where
detach
quit

Please let me know is there a way out of it ... as I am not getting ideas to overcome it ..

Community
  • 1
  • 1
Vivek S
  • 47
  • 7
  • 1
    Have you considered that without at least `-g`, `gdb` isn't going to give you useful names anyway? – BoBTFish Sep 23 '13 at 09:10
  • 4
    The `system` function is *not* safe to call in a signal handler. See e.g. the [`signal(7)`](http://linux.die.net/man/7/signal) manual page. – Some programmer dude Sep 23 '13 at 09:11
  • Yes I have tested gdb without -g option and on receiving core it properly indicates the fucntions and files with exact line numbers ... – Vivek S Sep 23 '13 at 09:13
  • 1
    @JoachimPileborg actually was aware of that, instead doing it out of curiosity .. without -g and backtrace .. is there any other way to do it ... gone through the post .. https://github.com/mirrors/gcc/tree/master/libbacktrace donno if it also works without -g – Vivek S Sep 23 '13 at 09:24
  • @JoachimPileborg: it's a bit late to worry about that, the program has already crashed. – n. m. could be an AI Sep 23 '13 at 09:28
  • Are you sure the code is not already compiled with the `-g` option? If you already get function names/line numbers, then chances are the program have been built with `-g`, and so the `backtrace` etc. functions should work fine. If the project is using the GNU autotools for configuration, then the `-g` flag will be enabled by default. – Some programmer dude Sep 23 '13 at 09:40
  • @JoachimPileborg: yes am sure about -g option ... our project doesn't use it for production builds and I had written a small test program as well to test it .. and gdb does it fine (binaries should be in sync with code) .. gcc compiler version is 4.4.7 – Vivek S Sep 23 '13 at 10:16
  • You may want to check out ABRT before rolling your own. ABRT works by hooking into the kernel core-dumping function to capture a backtrace and report it to bugzilla. It has many features like eliminating duplicates, etc. – Tom Tromey Sep 26 '13 at 17:21

1 Answers1

0

Not sure what you intended to do with the (echo \"source Trace.txt\";cat) | part. Simply using gdb -batch -x Trace.txt -q %.256s -pid %d > gdbTest.dump works fine as far as I can tell.

Jester
  • 56,577
  • 4
  • 81
  • 125