Skip to content
C++ runtime backtraces for windows, os x and linux
C++ Python Other
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
main Build as lib Jun 24, 2014
trace_perf shared_state: better timing to cause deadlock from multiple reads Jun 19, 2015
windows
.gitignore
LICENSE Initial commit Feb 18, 2014
Makefile.unittest Makefile.unittest: make objects depend on makefiles Jun 19, 2015
README.md Tidy Jul 1, 2015
backtrace.cpp
backtrace.h
backtrace.pro
backtrace.sln
backtrace.vcxproj
barrier.cpp Enabled warnings Mar 30, 2014
barrier.h Barrier logs performance but doesn't fail unit tests because of timin… Mar 26, 2014
cva_list.h
demangle.cpp demangle: uses std::ostream Jun 19, 2015
demangle.h demangle: uses std::ostream Jun 19, 2015
detectgdb.cpp detectgdb: assume attached debugger when building debug build for ios Jun 19, 2015
detectgdb.h Initial commit Feb 18, 2014
exceptionassert.cpp
exceptionassert.h
expectexception.h
prettifysegfault.cpp
prettifysegfault.h Works on Windows 7, 64-bit Mar 3, 2014
prettifysegfaultnoinline.cpp
shared_state.cpp shared_state: better timing to cause deadlock from multiple reads Jun 19, 2015
shared_state.h shared_state: type traits need to define enable_implicit_lock to enab… Jun 19, 2015
shared_state.pdf Updated shared_state.pdf Apr 1, 2014
shared_state_mutex.h shared_state: shared_state_nomutex disable locking Jun 19, 2015
shared_state_traits_backtrace.cpp
shared_state_traits_backtrace.h
shared_timed_mutex_polyfill.h
signalname.cpp
signalname.h Initial commit Feb 18, 2014
tasktimer.cpp TaskTimer works during global scope unwind, but only with single thre… Apr 2, 2014
tasktimer.h
timer.cpp Uses C++11 instead of boost where applicable. Mar 20, 2014
timer.h Uses C++11 instead of boost where applicable. Mar 20, 2014
trace_perf.cpp trace_perf: Output errors of missing tests in a format that an IDE re… Jun 24, 2014
trace_perf.h
unittest.cpp
unittest.h
unused.h
verifyexecutiontime.cpp
verifyexecutiontime.h

README.md

Backtraces rationale

The Backtrace class should store a backtrace of the call stack in 1 ms on OS X. Windows and linux was supported but are not currently maintained.

To make bugs go squish you want some sort of indication as to where it is. This is a bunch of small classes that makes use of runtime backtraces in C++ to decorate exceptions and segfaults with info about their origin. Each class header comment defines its expected behaviour.

Backtrace example

string backtrace = Backtrace::make_string ();
cout << backtrace;

Example output (compiled with clang-500.2.79):

backtrace (5 frames)
Backtrace::make_string(int) (in backtrace-unittest) (backtrace.cpp:264)
Backtrace::test() (in backtrace-unittest) (backtrace.cpp:283)
BacktraceTest::UnitTest::test() (in backtrace-unittest) (unittest.cpp:34)
start (in libdyld.dylib) + 1
0x0000000000000001

PrettifySegfault example

The PrettifySegfault class should attempt to capture any null-pointer exception (SIGSEGV and SIGILL) in the program, log a backtrace, and then throw a regular C++ exception from the function causing the signal.

void nasty_function() {
  *(int*) NULL = 0;
}

Somewhere else

    try {
      nasty_function();       
    } catch (const exception& x) {
      string backtrace_and_signal_name = boost::diagnostic_information(x);
    }

shared_state example

The shared_state<T> class is a smart pointer that guarantees thread-safe access to objects of type T.

Make an instance of an object thread-safe by storing it in a shared_state smart pointer.

    shared_state<MyType> a {new MyType};
    a->foo ();

    shared_state<const MyType> ac {a};
    ac->bar ();

The call to foo() will have mutually exclusive write access and the call to bar() will have shared read-only const access. Given

    class MyType {
    public:
      void foo();
      void bar() const;
    };

To keep the lock over multiple method calls, do:

    shared_state<MyType> a {new MyType};

    {
      auto w = a.write();
      w->foo();
      w->bar();
    }

    {
      auto r = a.read();
      r->bar();
      r->bar();
    }

.read() and .write() are implicitly called by the -> operator on shared_state and create thread-safe critical sections. For shared read-only access when using .read() and for mutually exclusive read-and-write access using .write(). They both throw exceptions on lock timeout, embed a backtrace in an exception object like this:

#include "shared_state_traits_backtrace.h"

class MyType {
public:
    typedef shared_state_traits_backtrace shared_state_traits;
    ...
};


... {
  shared_state<MyType> a(new MyType);
  ...
  try {
    auto w = a.write();
    ...
  } catch (lock_failed& x) {
    const Backtrace* backtrace = boost::get_error_info<Backtrace::info>(x);
    ...
  }
... }

See shared_state.pdf and shared_state.h for details.

ExceptionAssert example

The ExceptionAssert class should store details about an assertion that failed.

    try {
         EXCEPTION_ASSERT_EQUALS( 1, 2 );
    } catch (const exception& x) {
         string what = boost::diagnostic_information(x);
    }

More examples

Please refer to the static test() function in each class for more complete examples.

How to use this in your own code

The .pro file for QMAKE builds a static library. The project depends on the boost library.

License

GPL v3.0

Other utilities

  • Demangle should perform a system specific demangling of compiled C++ names.
  • The DetectGdb class should detect whether the current process was started through, or is running through, gdb (or as a child of another process).
  • The Timer class should measure time with a high accuracy.
  • The TaskTimer class should log how long time it takes to execute a scope while distinguishing nested scopes and different threads.
You can’t perform that action at this time.