Extract Backtrace out of SafePtr as a general utility

This commit is contained in:
Maxime Coste 2015-05-27 13:48:45 +01:00
parent 4a588d6ce5
commit 7245d2abe9
3 changed files with 28 additions and 21 deletions

View File

@ -1,32 +1,29 @@
#include "safe_ptr.hh"
#ifdef SAFE_PTR_TRACK_CALLSTACKS
#include "backtrace.hh"
#include <string.h>
#include <stdlib.h>
#if defined(__linux__)
#if defined(__linux__) || defined(__APPLE__)
# include <execinfo.h>
#elif defined(__CYGWIN__)
# include <windows.h>
#endif
namespace Kakoune
{
SafeCountable::Backtrace::Backtrace()
Backtrace::Backtrace()
{
#if defined(__linux__)
#if defined(__linux__) || defined(__APPLE__)
num_frames = backtrace(stackframes, max_frames);
#elif defined(__CYGWIN__)
num_frames = CaptureStackBackTrace(0, max_frames, stackframes, nullptr);
#endif
}
const char* SafeCountable::Backtrace::desc() const
const char* Backtrace::desc() const
{
#if defined(__linux__)
#if defined(__linux__) || defined(__APPLE__)
char** symbols = backtrace_symbols(stackframes, num_frames);
int size = 0;
for (int i = 0; i < num_frames; ++i)
@ -57,4 +54,3 @@ const char* SafeCountable::Backtrace::desc() const
}
}
#endif

20
src/backtrace.hh Normal file
View File

@ -0,0 +1,20 @@
#ifndef backtrace_hh_INCLUDED
#define backtrace_hh_INCLUDED
namespace Kakoune
{
struct Backtrace
{
static constexpr int max_frames = 16;
void* stackframes[max_frames];
int num_frames = 0;
Backtrace();
const char* desc() const;
};
}
#endif // backtrace_hh_INCLUDED

View File

@ -5,6 +5,7 @@
#include "assert.hh"
#include "ref_ptr.hh"
#include "backtrace.hh"
#include <type_traits>
#include <utility>
@ -64,16 +65,6 @@ public:
private:
#ifdef SAFE_PTR_TRACK_CALLSTACKS
struct Backtrace
{
static constexpr int max_frames = 16;
void* stackframes[max_frames];
int num_frames = 0;
Backtrace();
const char* desc() const;
};
struct Callstack
{
Callstack(void* p) : ptr(p) {}