Reputation: 7126
I have set up backstrace_symbols
on my script using:
void handler(int sig) {
void *array[20];
size_t size;
char **strings;
size_t i;
size = backtrace(array, 20);
strings = backtrace_symbols(array,size);
tracelog.open("/var/log/safesquid/safesquid/debug/trace.log", ios::app);
tracelog << sig << endl;
for (i=0; i<size; i++)
tracelog << strings[i] << endl;
free(strings);
exit(1);
}
In the main function, it is called it using
signal(SIGSEGV, handler);
Backtrace:
11
/opt/scripts/cplusconnector(_Z7handleri+0x2b) [0x40233b]
/lib64/libc.so.6(+0x32920) [0x7fe7b503c920]
/lib64/libc.so.6(+0x131b5f) [0x7fe7b513bb5f]
/opt/scripts/cplusconnector(main+0x163) [0x4035c3]
/lib64/libc.so.6(__libc_start_main+0xfd) [0x7fe7b5028cdd]
/opt/scripts/cplusconnector() [0x402199]
11
/opt/scripts/cplusconnector(_Z7handleri+0x2b) [0x40233b]
/lib64/libc.so.6(+0x32920) [0x7fd9db71a920]
/lib64/libc.so.6(+0x131b5f) [0x7fd9db819b5f]
/opt/scripts/cplusconnector(main+0x163) [0x4035c3]
/lib64/libc.so.6(__libc_start_main+0xfd) [0x7fd9db706cdd]
/opt/scripts/cplusconnector() [0x402199]
[root@server dev]# addr2line -e test-unstrippped 0x402199 0x7f1999f16cdd 0x4035c3
??:0
??:0
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/basic_string.h:975
Where could the problem be and why does running the program result in a segmentation fault?
update:
it was compiled using these options
g++ cplusconnector-k.cpp -g -rdynamic -O2 -I/usr/include/mysql -L/usr/lib/mysql -lmysqlcppconn -o test `mysql_config --cflags --libs`
update:
i removed the optimization, and tried resolving the symbol using add2line from the backtrace, and it pointed to this line in code..
/opt/webcache/scripts/cplusconnector(_Z7handleri+0x25) [0x401d49]
/lib64/libc.so.6(+0x32920) [0x7f6ad974b920]
/lib64/libc.so.6(+0x131b5f) [0x7f6ad984ab5f]
/usr/lib64/libstdc++.so.6(_ZNSsaSEPKc+0x1c) [0x7f6ad9dcd12c]
/opt/webcache/scripts/cplusconnector(main+0x12b) [0x4027ae]
/lib64/libc.so.6(__libc_start_main+0xfd) [0x7f6ad9737cdd]
/opt/webcache/scripts/cplusconnector() [0x401c69]
[root@server dev]# addr2line -e test 0x401d49 0x7f6ad974b920 0x7f6ad984ab5f 0x7f6ad9dcd12c 0x4027ae 0x7f6ad9737cdd 0x401c69
/usr/local/dev/cplusconnector-k.cpp:42
??:0
??:0
??:0
/usr/local/dev/cplusconnector-k.cpp:138
??:0
??:0
line: 42 is outside the main function and is global:
ofstream httplog;
line:138 is inside the main function:
if (std::string::npos != host.find(string("1.0.0.1"))){
return 0;
}
any ideas??
Upvotes: 1
Views: 1956
Reputation: 1
To debug your program, you could enable core dumping (using the setrlimit(2) syscall, often thru the ulimit bash builtin). Then, run gdb
with the program and the core dump.
As for why the backtrace_symbols
is giving you such names, it is because of name mangling. See also this.
Consider also using Ian Taylor's libbacktrace (integrated into recent versions of GCC, e.g. GCC 6 and above). Compile your code with -g
(and perhaps also some optimization flag like -O
)
Upvotes: 2
Reputation: 41
backtrace() cannot be called from signal handlers.
There is a (short) list of methods that may be called from signal handlers. I am too lazy too Google for it now though
Upvotes: 1
Reputation: 1907
I see there is indeed a good reason to use free()
For the name mangling use abi::__cxa_demangle.
Upvotes: 2