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)
{
char* callstack = Backtrace{}.desc();
String debug_info = format("pid: {}\ncallstack:\n{}", getpid(), callstack);
free(callstack);
String debug_info = format("pid: {}\ncallstack:\n{}", getpid(), Backtrace{}.desc());
write_debug(format("assert failed: '{}'\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 <string.h>
#include <stdlib.h>
#include "string.hh"
#if defined(__linux__) || defined(__APPLE__)
# include <execinfo.h>
#elif defined(__CYGWIN__)
# include <windows.h>
#include <stdio.h>
#endif
namespace Kakoune
@ -21,31 +21,29 @@ Backtrace::Backtrace()
#endif
}
char* Backtrace::desc() const
String Backtrace::desc() const
{
#if defined(__linux__) || defined(__APPLE__)
char** symbols = backtrace_symbols(stackframes, num_frames);
int size = 0;
ByteCount size = 0;
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);
res[0] = 0;
String res; res.reserve(size);
for (int i = 0; i < num_frames; ++i)
{
strcat(res, symbols[i]);
strcat(res, "\n");
res += symbols[i];
res += "\n";
}
free(symbols);
return res;
#elif defined(__CYGWIN__)
char* res = (char*)malloc(num_frames * 20);
res[0] = 0;
String res; res.reserve(num_frames * 20);
for (int i = 0; i < num_frames; ++i)
{
char addr[20];
snprintf(addr, 20, "0x%p", stackframes[i]);
strcat(res, addr);
res += addr;
}
return res;
#else

View File

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

View File

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

View File

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