1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-11-24 07:53:41 +03:00

Return a String in Backtrace::desc

This commit is contained in:
Maxime Coste 2015-05-29 13:35:54 +01:00
parent 3c86484c4e
commit be9da616df
5 changed files with 16 additions and 20 deletions

View File

@ -47,9 +47,7 @@ bool notify_fatal_error(const String& msg)
void on_assert_failed(const char* message) void on_assert_failed(const char* message)
{ {
char* callstack = Backtrace{}.desc(); String debug_info = format("pid: {}\ncallstack:\n{}", getpid(), Backtrace{}.desc());
String debug_info = format("pid: {}\ncallstack:\n{}", getpid(), callstack);
free(callstack);
write_debug(format("assert failed: '{}'\n{}", message, debug_info)); write_debug(format("assert failed: '{}'\n{}", message, debug_info));
const auto msg = format("{}\n[Debug Infos]\n{}", message, debug_info); const auto msg = format("{}\n[Debug Infos]\n{}", message, debug_info);

View File

@ -1,12 +1,12 @@
#include "backtrace.hh" #include "backtrace.hh"
#include <string.h> #include "string.hh"
#include <stdlib.h>
#if defined(__linux__) || defined(__APPLE__) #if defined(__linux__) || defined(__APPLE__)
# include <execinfo.h> # include <execinfo.h>
#elif defined(__CYGWIN__) #elif defined(__CYGWIN__)
# include <windows.h> # include <windows.h>
#include <stdio.h>
#endif #endif
namespace Kakoune namespace Kakoune
@ -21,31 +21,29 @@ Backtrace::Backtrace()
#endif #endif
} }
char* Backtrace::desc() const String Backtrace::desc() const
{ {
#if defined(__linux__) || defined(__APPLE__) #if defined(__linux__) || defined(__APPLE__)
char** symbols = backtrace_symbols(stackframes, num_frames); char** symbols = backtrace_symbols(stackframes, num_frames);
int size = 0; ByteCount size = 0;
for (int i = 0; i < num_frames; ++i) for (int i = 0; i < num_frames; ++i)
size += strlen(symbols[i]) + 1; size += StringView::strlen(symbols[i]) + 1;
char* res = (char*)malloc(size+1); String res; res.reserve(size);
res[0] = 0;
for (int i = 0; i < num_frames; ++i) for (int i = 0; i < num_frames; ++i)
{ {
strcat(res, symbols[i]); res += symbols[i];
strcat(res, "\n"); res += "\n";
} }
free(symbols); free(symbols);
return res; return res;
#elif defined(__CYGWIN__) #elif defined(__CYGWIN__)
char* res = (char*)malloc(num_frames * 20); String res; res.reserve(num_frames * 20);
res[0] = 0;
for (int i = 0; i < num_frames; ++i) for (int i = 0; i < num_frames; ++i)
{ {
char addr[20]; char addr[20];
snprintf(addr, 20, "0x%p", stackframes[i]); snprintf(addr, 20, "0x%p", stackframes[i]);
strcat(res, addr); res += addr;
} }
return res; return res;
#else #else

View File

@ -4,6 +4,8 @@
namespace Kakoune namespace Kakoune
{ {
class String;
struct Backtrace struct Backtrace
{ {
static constexpr int max_frames = 16; static constexpr int max_frames = 16;
@ -11,7 +13,7 @@ struct Backtrace
int num_frames = 0; int num_frames = 0;
Backtrace(); Backtrace();
char* desc() const; String desc() const;
}; };
} }

View File

@ -283,9 +283,7 @@ void signal_handler(int signal)
} }
if (signal != SIGTERM) if (signal != SIGTERM)
{ {
char* callstack = Backtrace{}.desc(); auto msg = format("Received {}, exiting.\nCallstack:\n{}", text, Backtrace{}.desc());
auto msg = format("Received {}, exiting.\nCallstack:\n{}", text, callstack);
free(callstack);
write_stderr(msg); write_stderr(msg);
notify_fatal_error(msg); notify_fatal_error(msg);
} }

View File

@ -164,13 +164,13 @@ public:
}; };
ZeroTerminatedString zstr() const { return {begin(), end()}; } ZeroTerminatedString zstr() const { return {begin(), end()}; }
private:
[[gnu::optimize(3)]] // this is recursive for constexpr reason [[gnu::optimize(3)]] // this is recursive for constexpr reason
static constexpr ByteCount strlen(const char* s) static constexpr ByteCount strlen(const char* s)
{ {
return *s == 0 ? 0 : strlen(s+1) + 1; return *s == 0 ? 0 : strlen(s+1) + 1;
} }
private:
const char* m_data = nullptr; const char* m_data = nullptr;
ByteCount m_length = 0; ByteCount m_length = 0;
}; };