I´m running a multi-threaded C++ program in a Linux system (Kernel 2.6.23). My code is compiled using G++ version 4.7.4.
I added the following code to catch segmentation faults:
void segFaultHandler(int sig)
{
void *array[10];
size_t size;
size = backtrace(array, 10);
fprintf(stderr, "Error: signal %d:\n", sig);
backtrace_symbols_fd(array, size, STDERR_FILENO);
exit(1);
}
And on main:
main()
{
signal(SIGSEGV, segFaultHandler);
try {
program code here...
catch(const std::exception& ex)
{
std::cerr << "Error occurred: " << ex.what() << std::endl;
}
catch(...)
{
std::cerr << "Unknown failure occurred. Possible memory corruption" << std::endl;
}
return 0;
}
When running, my program is crashing with the following output:
Error: signal 11:
/home/cross/bin/aeirtu[0x807406e]
[0xffffe420]
/lib/libc.so.6(memcpy+0x2f)[0xb7d9bcbf]
/usr/gcc-4.7.4/lib/libstdc++.so.6(_ZNSt15basic_streambufIcSt11char_traitsIcEE6xsputnEPKci+0x73)[0xb7f26933]
It is not a easy debug as my program crashes only sometimes, so I need a tool to get the crash source and fix it.
From the given output, how can I trace back the function and piece of code that caused the crash ?
You can use valgrind to locate the source of a segfault. If you binary is compiled with debug symbols it will give you the exact source location:
==12834== Invalid write of size 4
==12834== at 0x4004FD: main (test.cc:3)
==12834== Address 0x0 is not stack'd, malloc'd or (recently) free'd
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With