From: Markus R. <rol...@us...> - 2006-03-10 00:16:09
|
Update of /cvsroot/simspark/simspark/spark/zeitgeist In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30810 Modified Files: core.h core.cpp Log Message: - implemented an automatic stack trace that is triggered when we receive a segmentation fault. The implementation uses the backtrace facility of recent glibc version and the addr2line tool from the binutils package to demangle symbol and to print line number. Needs some cleanup: test for presence of execinfo.h and addr2line. Index: core.cpp =================================================================== RCS file: /cvsroot/simspark/simspark/spark/zeitgeist/core.cpp,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** core.cpp 5 Dec 2005 20:59:18 -0000 1.1 --- core.cpp 10 Mar 2006 00:16:06 -0000 1.2 *************** *** 35,39 **** --- 35,42 ---- #include <salt/path.h> #include <salt/sharedlibrary.h> + #include <execinfo.h> + #include <signal.h> #include <iostream> + #include <sstream> using namespace boost; *************** *** 146,149 **** --- 149,204 ---- mRandomServer = shared_static_cast<RandomServer> (context->New("zeitgeist/RandomServer", "/sys/server/random")); + + // install fault handler + signal(SIGSEGV, CatchSignal); + } + + void Core::CatchSignal(int sig_num) + { + if (sig_num != SIGSEGV) + { + return; + } + + cerr << "(Core) caught signal " << sig_num << endl; + + // retrieve the name of our executable without access to argc and + // argv (this works only with linux) + + const int exeNameSz = 4096; + char exeName[exeNameSz]; + readlink("/proc/self/exe",exeName,exeNameSz); + + // print stack trace + + const int arSize = 200; + void *addresses[arSize]; + + int depth = backtrace (addresses, arSize); + char **strings = backtrace_symbols (addresses, depth); + + cerr << "(Core) dumping " << depth << " stack frames.\n"; + + for (int i=0; i<depth; ++i) + { + cerr << "[" << i << "] " << strings[i] << "\n"; + + // use the addr2line tool from binutils to retrieve the + // source line from the frame adress + // -C : decode, demangle low-level symbol names + // -e : specify executable name + // -f : display function names, as well as file and line number info + + stringstream ss; + ss << "addr2line -C -f -e \"" << exeName << "\" " << addresses[i]; + system(ss.str().c_str()); + + cerr << "\n"; + } + + free (strings); + + cerr << "(Core) exit" << endl; + exit(1); } Index: core.h =================================================================== RCS file: /cvsroot/simspark/simspark/spark/zeitgeist/core.h,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** core.h 5 Dec 2005 20:59:18 -0000 1.1 --- core.h 10 Mar 2006 00:16:06 -0000 1.2 *************** *** 181,184 **** --- 181,187 ---- const boost::shared_ptr<Leaf>& base); + /** signal handler */ + static void CatchSignal(int sig_num); + // // members |