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" #include "backtrace.hh"
#ifdef SAFE_PTR_TRACK_CALLSTACKS
#include <string.h> #include <string.h>
#include <stdlib.h>
#if defined(__linux__) #if defined(__linux__) || defined(__APPLE__)
# include <execinfo.h> # include <execinfo.h>
#elif defined(__CYGWIN__) #elif defined(__CYGWIN__)
# include <windows.h> # include <windows.h>
#endif #endif
namespace Kakoune namespace Kakoune
{ {
Backtrace::Backtrace()
SafeCountable::Backtrace::Backtrace()
{ {
#if defined(__linux__) #if defined(__linux__) || defined(__APPLE__)
num_frames = backtrace(stackframes, max_frames); num_frames = backtrace(stackframes, max_frames);
#elif defined(__CYGWIN__) #elif defined(__CYGWIN__)
num_frames = CaptureStackBackTrace(0, max_frames, stackframes, nullptr); num_frames = CaptureStackBackTrace(0, max_frames, stackframes, nullptr);
#endif #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); char** symbols = backtrace_symbols(stackframes, num_frames);
int size = 0; int size = 0;
for (int i = 0; i < num_frames; ++i) 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 "assert.hh"
#include "ref_ptr.hh" #include "ref_ptr.hh"
#include "backtrace.hh"
#include <type_traits> #include <type_traits>
#include <utility> #include <utility>
@ -64,16 +65,6 @@ public:
private: private:
#ifdef SAFE_PTR_TRACK_CALLSTACKS #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 struct Callstack
{ {
Callstack(void* p) : ptr(p) {} Callstack(void* p) : ptr(p) {}