compilation command
neelkanth@ubuntu:~$ gcc -rdynamic test.c
Stack Trace Output, when the code crashed with SIGSEGV
C program:
neelkanth@ubuntu:~$ cat test.c
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <execinfo.h>
#include <stdlib.h>
void signalhandler(int signo)
{
int nptrs, i;
void *buffer[1024];
char **strings;
printf("Entry: %s %d\n", __func__, __LINE__);
printf("-- STACK TRACE START\n");
nptrs = backtrace(buffer, 1024);
strings = backtrace_symbols(buffer, nptrs);
if (strings == NULL) {
return ;
}
for (i = 0; i < nptrs; ++i) {
printf("%d\t%s\n", i, strings[i]);
}
printf("Exit: %s %d\n", __func__, __LINE__);
printf("Sending PID: %ld, UID: %ld\n", getppid(), getuid());
exit(-1);
}
void func3()
{
printf("Entry: %s %d\n", __func__, __LINE__);
dummycrash();
printf("Exit: %s %d\n", __func__, __LINE__);
return;
}
void func2()
{
func3();
printf("Entry: %s %d\n", __func__, __LINE__);
printf("Exit: %s %d\n", __func__, __LINE__);
return;
}
void func1()
{
printf("Entry %s %d\n", __func__, __LINE__);
func2();
printf("Exit: %s %d\n", __func__, __LINE__);
return;
}
void dummycrash()
{
printf("Entry %s %d\n", __func__, __LINE__);
char *temp = NULL;
strcpy(temp, "crash");
printf("Exit: %s %d\n", __func__, __LINE__);
}
int main (int argc, char *argv[])
{
int nptrs, i;
void *buffer[1024];
char **strings;
signal(SIGSEGV, signalhandler);
func1();
while (1)
sleep (10);
return 0;
}