Coder Social home page Coder Social logo

liuxw7 / backtrace Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gustafsson/backtrace

0.0 1.0 0.0 676 KB

C++ runtime backtraces for windows, os x and linux

License: GNU General Public License v3.0

C++ 98.27% C 0.09% Python 1.16% Shell 0.14% QMake 0.34%

backtrace's Introduction

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.

backtrace's People

Contributors

gustafsson avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.