Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrading to CMake, allowing backtrace to work on MacOS #1

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
cmake_minimum_required(VERSION 3.1)

project(backtracexx)

add_library(${PROJECT_NAME} SHARED "backtracexx.cpp")
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(${PROJECT_NAME} ${CMAKE_DL_LIBS})

add_executable(${PROJECT_NAME}_example "example.cpp")
set_target_properties(${PROJECT_NAME} PROPERTIES ENABLE_EXPORTS 1)
target_link_libraries(${PROJECT_NAME}_example ${PROJECT_NAME})

79 changes: 74 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,75 @@
This small library uses unwind information and produces backtrace
with optionally demangled symbols.
# backtracexx : cross-platform backtrace generation on Linux/MacOS/Windows

This small library uses unwind information and produces backtrace with optionally demangled symbols.

The [original sources](http://svn.pld-linux.org/cgi-bin/viewsvn/backtracexx/) are no longer available, but were imported to [GitHub](https://github.com/borneq/backtracexx) by Andrzej Borucki.

## Building

Note: currently, a GCC flavor of C++ compiler is required (that is, the code will not compile with e.g. Clang):

```
mkdir build
cmake -DCMAKE_CXX_COMPILER=g++-11 ..
make
```

## Usage

The following example program produces the output below:

```c++
#include "backtracexx.hpp"

#include <csetjmp>
#include <csignal>
#include <iostream>

jmp_buf context;

void signalHandler( int signalNumber )
{
std::cerr << backtracexx::scan();
longjmp( context, 1 );
}

void zoo()
{
if ( setjmp( context ) == 0 )
{
volatile int* p = 0;
*p = 0;
}
}

void bar( void ( *f )() )
{
f();
}

void foo()
{
bar( &zoo );
}

int main( int argc, char const* const* argv )
{
signal( SIGSEGV, signalHandler );
foo();
return 0;
}
```

```
./backtracexx_example
=== backtrace ====
0x10c81c55c : backtracexx::scan[abi:cxx11](void*)+0x33 [/Users/dmikushin/backtracexx/build/libbacktracexx.dylib @ 0x10c81a000 ]
0x10c811588 : signalHandler(int)+0x1d [/Users/dmikushin/backtracexx/build/./backtracexx_example @ 0x10c80f000 ]
0x7fff6383b42d : _sigtramp+0x1d [/usr/lib/system/libsystem_platform.dylib @ 0x7fff63837000 ]
0x10c811605 : zoo()+0x2d [/Users/dmikushin/backtracexx/build/./backtracexx_example @ 0x10c80f000 ]
0x10c81161f : bar(void (*)())+0x12 [/Users/dmikushin/backtracexx/build/./backtracexx_example @ 0x10c80f000 ]
0x10c811635 : foo()+0x13 [/Users/dmikushin/backtracexx/build/./backtracexx_example @ 0x10c80f000 ]
0x10c811660 : main+0x28 [/Users/dmikushin/backtracexx/build/./backtracexx_example @ 0x10c80f000 ]
==================
```

Sources available at:
(original) http://svn.pld-linux.org/cgi-bin/viewsvn/backtracexx/
(imported to) https://github.com/borneq/backtracexx
7 changes: 4 additions & 3 deletions backtracexx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#if defined( __GNUC__ )
#include <cxxabi.h>
#if defined( __linux__ )
#if defined( __linux__ ) || defined( __APPLE__ )
#include <dlfcn.h>
#endif
#include <unwind.h>
Expand Down Expand Up @@ -45,7 +45,7 @@ namespace backtracexx

bool lookupSymbol( Frame& frame )
{
#if defined( __linux__ )
#if defined( __linux__ ) || defined( __APPLE__ )

Dl_info info;
if ( ::dladdr( frame.address, &info ) )
Expand All @@ -55,6 +55,7 @@ namespace backtracexx
frame.moduleName = info.dli_fname;
if ( info.dli_saddr )
{
frame.symbolMangled = info.dli_sname;
frame.displacement = reinterpret_cast< ::ptrdiff_t >( frame.address )
- reinterpret_cast< ::ptrdiff_t >( info.dli_saddr );
int status;
Expand All @@ -81,7 +82,7 @@ namespace backtracexx
{
struct TraceHelper
{
_Unwind_Ptr prevIp;
_Unwind_Ptr prevIp = -1;
unsigned recursionDepth;
Trace trace;
};
Expand Down
1 change: 1 addition & 0 deletions backtracexx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace backtracexx

void const* address;
std::string symbol;
std::string symbolMangled;
long displacement;
std::string moduleName;
void const* moduleBaseAddress;
Expand Down
19 changes: 0 additions & 19 deletions makefile

This file was deleted.

19 changes: 0 additions & 19 deletions makefile.mingw

This file was deleted.

181 changes: 0 additions & 181 deletions msvc/backtracexx/backtracexx.vcproj

This file was deleted.

Loading