From be9da616df3826eb7c9ba7448adcdc56a97d479b Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Fri, 29 May 2015 13:35:54 +0100 Subject: [PATCH] Return a String in Backtrace::desc --- src/assert.cc | 4 +--- src/backtrace.cc | 22 ++++++++++------------ src/backtrace.hh | 4 +++- src/main.cc | 4 +--- src/string.hh | 2 +- 5 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/assert.cc b/src/assert.cc index 9d0635189..b193f595e 100644 --- a/src/assert.cc +++ b/src/assert.cc @@ -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); diff --git a/src/backtrace.cc b/src/backtrace.cc index 4f8aacf80..a4c65876e 100644 --- a/src/backtrace.cc +++ b/src/backtrace.cc @@ -1,12 +1,12 @@ #include "backtrace.hh" -#include -#include +#include "string.hh" #if defined(__linux__) || defined(__APPLE__) # include #elif defined(__CYGWIN__) # include +#include #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 diff --git a/src/backtrace.hh b/src/backtrace.hh index 8f1463724..a04c6f30a 100644 --- a/src/backtrace.hh +++ b/src/backtrace.hh @@ -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; }; } diff --git a/src/main.cc b/src/main.cc index 822d53fe5..7c7dc86d9 100644 --- a/src/main.cc +++ b/src/main.cc @@ -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); } diff --git a/src/string.hh b/src/string.hh index b5ad8e5a9..2108c64eb 100644 --- a/src/string.hh +++ b/src/string.hh @@ -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; };