Skip to content

Commit

Permalink
added backtrace
Browse files Browse the repository at this point in the history
  • Loading branch information
match065 committed May 13, 2012
1 parent 9631031 commit 66e7783
Show file tree
Hide file tree
Showing 13 changed files with 615 additions and 3 deletions.
13 changes: 13 additions & 0 deletions cmake/Modules/FindBFD.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
find_library( DL_LIBRARY NAMES dl PATH /usr/lib /usr/lib64 )
find_library( BFD_LIBRARY NAMES bfd PATH /usr/lib /usr/lib64 )

if ( DL_LIBRARY AND BFD_LIBRARY )
set( BFD_FOUND TRUE )
endif (DL_LIBRARY AND BFD_LIBRARY)

if ( BFD_FOUND )

message( STATUS "Found libbfd: ${BFD_LIBRARY}")


endif ( BFD_FOUND )
12 changes: 11 additions & 1 deletion libgrive/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,20 @@ find_package(CURL REQUIRED)
find_package(EXPAT REQUIRED)
find_package(CppUnit)
find_package(Boost REQUIRED)
find_package(BFD)

IF ( CPPUNIT_FOUND )
set( OPT_INCS ${CPPUNIT_INCLUDE_DIR} )
ENDIF ( CPPUNIT_FOUND )

if ( BFD_FOUND )
set( OPT_LIBS ${DL_LIBRARY} ${BFD_LIBRARY} )
file( GLOB OPT_SRC
src/bfd/*.cc
)

endif ( BFD_FOUND )

include_directories(
${libgrive_SOURCE_DIR}/src
${libgrive_SOURCE_DIR}/test
Expand Down Expand Up @@ -45,12 +54,13 @@ file (GLOB LIBGRIVE_SRC

add_definitions( -DVERSION="${GRIVE_VERSION}" -DTEST_DATA="${libgrive_SOURCE_DIR}/test/data/" )

add_library( grive SHARED ${LIBGRIVE_SRC} )
add_library( grive SHARED ${LIBGRIVE_SRC} ${OPT_SRC} )

target_link_libraries( grive
${CURL_LIBRARIES}
${JSONC_LIBRARY}
${OPENSSL_LIBRARIES}
${OPT_LIBS}
expat
)

Expand Down
25 changes: 25 additions & 0 deletions libgrive/src/bfd/Addr.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
grive: an GPL program to sync a local directory with Google Drive
Copyright (C) 2006 Wan Wai Ho
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation version 2
of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#pragma once

namespace gr
{
typedef void* addr_t ;
}
72 changes: 72 additions & 0 deletions libgrive/src/bfd/Backtrace.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
grive: an GPL program to sync a local directory with Google Drive
Copyright (C) 2012 Wan Wai Ho
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation version 2
of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include "Backtrace.hh"

#include "SymbolInfo.hh"
#include "util/CArray.hh"

#include <algorithm>
#include <cstring>
#include <sstream>

namespace gr {

Backtrace::Backtrace( std::size_t skip ) :
m_count( SymbolInfo::Instance()->Backtrace(m_stack, Count(m_stack) )),
m_skip( std::min( skip, m_count ) )
{
}

Backtrace::Backtrace( const Backtrace& bt ) :
m_count( bt.m_count ),
m_skip( bt.m_skip )
{
std::memcpy( m_stack, bt.m_stack, m_count * sizeof(m_stack[0]) ) ;
}

/*! \brief operator<< for printing backtraces
\internal
This function will call SymbolInfo::PrintTrace() to print out a backtrace
to the stream. It will use the SymbolInfo::Instance() singleton to get
the symbol information.
\param os The output stream.
\param b The backtrace information.
\sa SymbolInfo::Backtrace(), SymbolInfo::Instance()
*/
std::ostream& operator<<( std::ostream& os, const gr::Backtrace& b )
{
// the 1st function in the stack is SymbolInfo::Backtrace()
// the 2nd one is the Backtrace() constructor
// both are not interesting to user
for ( std::size_t i = b.m_skip ; i < b.m_count ; i++ )
SymbolInfo::Instance()->PrintTrace( b.m_stack[i], os, i - b.m_skip ) ;

return os ;
}

std::string Backtrace::ToString( ) const
{
std::ostringstream oss ;
oss << *this ;
return oss.str( ) ;
}

} // end of namespace
56 changes: 56 additions & 0 deletions libgrive/src/bfd/Backtrace.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
grive: an GPL program to sync a local directory with Google Drive
Copyright (C) 2012 Wan Wai Ho
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation version 2
of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#pragma once

#include "Addr.hh"

#include <iosfwd>
#include <string>

namespace gr {

/// A shortcut to print out backtrace information
/** \internal
The sole reason for this class to exists is to provide the
operator<< overload to allow:
\code
std::cout << Backtrace() << std::endl ;
\endcode
\sa SymbolInfo
*/
class Backtrace
{
public :
explicit Backtrace( std::size_t skip = 2 ) ;
Backtrace( const Backtrace& bt ) ;

friend std::ostream& operator<<( std::ostream& os,
const gr::Backtrace& bt ) ;

std::string ToString( ) const ;

private :
addr_t m_stack[100] ;
std::size_t m_count, m_skip ;
} ;

} // end of namespace
71 changes: 71 additions & 0 deletions libgrive/src/bfd/Debug.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
grive: an GPL program to sync a local directory with Google Drive
Copyright (C) 2006 Wan Wai Ho
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation version 2
of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include "Debug.hh"

#include <cassert>

#ifdef __GNUC__
#include <cxxabi.h>
#endif

#include <cstdlib>
#include <ostream>
#include <iomanip>

namespace gr {

std::string Demangle( const char *name )
{
#ifdef __GNUC__
assert( name != 0 ) ;

char *cname = abi::__cxa_demangle( name, 0, 0, 0 ) ;
std::string result( name ) ;
if ( cname != 0 )
{
result = cname ;
std::free( cname ) ;
}
return result ;
#else
return std::string( ) ;
#endif
}

std::ostream& PrintHex( std::ostream& os, const void *buf, std::size_t size )
{
const unsigned char *raw = static_cast<const unsigned char*>( buf ) ;

for ( std::size_t i = 0 ; i < size ; i++ )
{
if ( i % 8 == 0 && i > 0 )
os << std::endl ;

if ( i % 8 == 0 )
os << std::hex << std::setw(8) << std::setfill('0') << i << " " ;

os << "0x" << std::hex << std::setw(2) << std::setfill('0')
<< (int)raw[i] << ", " ;
}
os << std::endl ;
return os ;
}

} // end of namespace
32 changes: 32 additions & 0 deletions libgrive/src/bfd/Debug.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
grive: an GPL program to sync a local directory with Google Drive
Copyright (C) 2006 Wan Wai Ho
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation version 2
of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#pragma once

#include <cstddef>
#include <iosfwd>
#include <string>

namespace gr {

std::string Demangle( const char *name ) ;

std::ostream& PrintHex( std::ostream& os, const void *buf, std::size_t size ) ;

} // end of namespace
Loading

0 comments on commit 66e7783

Please sign in to comment.