Author: pluto Date: Thu Aug 24 04:27:55 2006 New Revision: 7683 Added: backtracexx/LICENSE backtracexx/README backtracexx/VERSION backtracexx/backtracexx.cpp backtracexx/backtracexx.hpp backtracexx/example.cpp backtracexx/makefile Log: - inital import.
Added: backtracexx/LICENSE ============================================================================== --- (empty file) +++ backtracexx/LICENSE Thu Aug 24 04:27:55 2006 @@ -0,0 +1 @@ +http://www.opensource.org/licenses/lgpl-license.php Added: backtracexx/README ============================================================================== --- (empty file) +++ backtracexx/README Thu Aug 24 04:27:55 2006 @@ -0,0 +1,5 @@ +A backtrace is a summary of how your program got where it is. +Unfortunately glibc's backtrace() and gdb's (bt) produce an unwind +path instead of true backtrace. This small library uses an unwind +informations to produce true backtrace with optionally demangled symbols +and allows you to embed backtracing facility into your application. Added: backtracexx/VERSION ============================================================================== --- (empty file) +++ backtracexx/VERSION Thu Aug 24 04:27:55 2006 @@ -0,0 +1 @@ +0.1 Added: backtracexx/backtracexx.cpp ============================================================================== --- (empty file) +++ backtracexx/backtracexx.cpp Thu Aug 24 04:27:55 2006 @@ -0,0 +1,65 @@ +#include "backtracexx.hpp" +#include <cxxabi.h> +#include <dlfcn.h> +#include <unwind.h> +#include <iostream> +#include <iomanip> +#include <sstream> + +namespace backtracexx +{ + namespace + { + unsigned char const* callpoint( unsigned char const* ip ) + { + // not implemented yet. + return ip; + } + + _Unwind_Reason_Code helper( struct _Unwind_Context* ctx, void* arg ) + { + _Unwind_Ptr ip = _Unwind_GetIP( ctx ); + reinterpret_cast< raw_backtrace_type* >( arg )->push_back( + callpoint( reinterpret_cast< unsigned char const* >( ip ) ) ); + return _URC_NO_REASON; + } + } + + raw_backtrace_type scan() + { + raw_backtrace_type trace; + _Unwind_Backtrace( helper, &trace ); + return trace; + } + + symbolic_backtrace_type symbols( raw_backtrace_type const& bt ) + { + std::ostringstream os; + os.setf( std::ios_base::hex, std::ios_base::basefield ); + os.setf( std::ios_base::showbase ); + symbolic_backtrace_type sbt; + for ( raw_backtrace_type::const_iterator i = bt.begin(), e = bt.end(); i != e; ++i ) + { + os.str( std::string() ); + Dl_info info; + if ( dladdr( *i, &info ) ) + { + long offset = reinterpret_cast< long >( *i ) - reinterpret_cast< long >( info.dli_saddr ); + int status; + char* demangled = abi::__cxa_demangle( info.dli_sname, 0, 0, &status ); + if ( status != -1 ) + { + os << std::setw( 18 ) << *i << " : " + << ( ( status == 0 ) ? demangled : info.dli_sname ) + << '+' << offset << " from " << info.dli_fname; + if ( status == 0 ) + free( demangled ); + } + } + else + os << std::setw( 18 ) << *i << " ??"; + sbt.push_back( os.str() ); + } + return sbt; + } +} Added: backtracexx/backtracexx.hpp ============================================================================== --- (empty file) +++ backtracexx/backtracexx.hpp Thu Aug 24 04:27:55 2006 @@ -0,0 +1,16 @@ +#ifndef backtracexx_hpp +#define backtracexx_hpp + +#include <list> +#include <string> + +namespace backtracexx +{ + typedef std::list< void const* > raw_backtrace_type; + typedef std::list< std::string > symbolic_backtrace_type; + + raw_backtrace_type scan(); + symbolic_backtrace_type symbols( raw_backtrace_type const& ); +} + +#endif Added: backtracexx/example.cpp ============================================================================== --- (empty file) +++ backtracexx/example.cpp Thu Aug 24 04:27:55 2006 @@ -0,0 +1,25 @@ +#include "backtracexx.hpp" +#include <iostream> +#include <iterator> + +void zoo() +{ + backtracexx::symbolic_backtrace_type s = backtracexx::symbols( backtracexx::scan() ); + std::copy(s.begin(), s.end(), std::ostream_iterator< std::string >( std::cout, "\n" ) ); +} + +void bar( void ( *f )() ) +{ + f(); +} + +void foo() +{ + bar( &zoo ); +} + +int main() +{ + foo(); + return 0; +} Added: backtracexx/makefile ============================================================================== --- (empty file) +++ backtracexx/makefile Thu Aug 24 04:27:55 2006 @@ -0,0 +1,15 @@ +CXX := g++ +CXXFLAGS += -Wall -Werror -pedantic + +all: libbacktracexx.so example + +libbacktracexx.so: backtracexx.hpp backtracexx.cpp + $(CXX) backtracexx.cpp -o libbacktracexx.so -shared -ldl $(CXXFLAGS) \ + -O3 -fpic -funwind-tables -fno-exceptions -fno-rtti + +example: example.cpp libbacktracexx.so + $(CXX) example.cpp -o example ./libbacktracexx.so $(CXXFLAGS) \ + -O1 -rdynamic -funwind-tables + +clean: + rm -f libbacktracexx.so example _______________________________________________ pld-cvs-commit mailing list [email protected] http://lists.pld-linux.org/mailman/listinfo/pld-cvs-commit
