summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2015-05-29 13:35:54 +0100
committerMaxime Coste <frrrwww@gmail.com>2015-05-29 13:35:54 +0100
commitbe9da616df3826eb7c9ba7448adcdc56a97d479b (patch)
treecf34cdb0c669f40a1fe28e4d949fb397fde1065a /src
parent3c86484c4ed284fa8f1feff380959f5b52b264c6 (diff)
Return a String in Backtrace::desc
Diffstat (limited to 'src')
-rw-r--r--src/assert.cc4
-rw-r--r--src/backtrace.cc22
-rw-r--r--src/backtrace.hh4
-rw-r--r--src/main.cc4
-rw-r--r--src/string.hh2
5 files changed, 16 insertions, 20 deletions
diff --git a/src/assert.cc b/src/assert.cc
index 9d063518..b193f595 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 4f8aacf8..a4c65876 100644
--- a/src/backtrace.cc
+++ b/src/backtrace.cc
@@ -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
diff --git a/src/backtrace.hh b/src/backtrace.hh
index 8f146372..a04c6f30 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 822d53fe..7c7dc86d 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 b5ad8e5a..2108c64e 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;
};