Commit 30b06acb authored by Philip Quinn's avatar Philip Quinn

Replace 'goto' error handling with KJ_DEFER.

parent ef501b1c
...@@ -55,6 +55,7 @@ String getStackSymbols(ArrayPtr<void* const> trace) { ...@@ -55,6 +55,7 @@ String getStackSymbols(ArrayPtr<void* const> trace) {
// is in use. // is in use.
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock(&mutex); pthread_mutex_lock(&mutex);
KJ_DEFER(pthread_mutex_unlock(&mutex));
// Don't heapcheck / intercept syscalls. // Don't heapcheck / intercept syscalls.
const char* preload = getenv("LD_PRELOAD"); const char* preload = getenv("LD_PRELOAD");
...@@ -63,9 +64,9 @@ String getStackSymbols(ArrayPtr<void* const> trace) { ...@@ -63,9 +64,9 @@ String getStackSymbols(ArrayPtr<void* const> trace) {
oldPreload = heapString(preload); oldPreload = heapString(preload);
unsetenv("LD_PRELOAD"); unsetenv("LD_PRELOAD");
} }
KJ_DEFER(if (oldPreload != nullptr) { setenv("LD_PRELOAD", oldPreload.cStr(), true); });
String lines[8]; String lines[8];
size_t lines_read = 0;
FILE* p = nullptr; FILE* p = nullptr;
#if __linux__ #if __linux__
...@@ -74,7 +75,7 @@ String getStackSymbols(ArrayPtr<void* const> trace) { ...@@ -74,7 +75,7 @@ String getStackSymbols(ArrayPtr<void* const> trace) {
char exe[512]; char exe[512];
ssize_t n = readlink("/proc/self/exe", exe, sizeof(exe)); ssize_t n = readlink("/proc/self/exe", exe, sizeof(exe));
if (n < 0 || n >= static_cast<ssize_t>(sizeof(exe))) { if (n < 0 || n >= static_cast<ssize_t>(sizeof(exe))) {
goto cleanup; return nullptr;
} }
exe[n] = '\0'; exe[n] = '\0';
...@@ -86,14 +87,15 @@ String getStackSymbols(ArrayPtr<void* const> trace) { ...@@ -86,14 +87,15 @@ String getStackSymbols(ArrayPtr<void* const> trace) {
#endif #endif
if (p == nullptr) { if (p == nullptr) {
goto cleanup; return nullptr;
} }
char line[512]; char line[512];
while (lines_read < kj::size(lines) && fgets(line, sizeof(line), p) != nullptr) { size_t i = 0;
while (i < kj::size(lines) && fgets(line, sizeof(line), p) != nullptr) {
// Don't include exception-handling infrastructure in stack trace. // Don't include exception-handling infrastructure in stack trace.
// addr2line output matches file names; atos output matches symbol names. // addr2line output matches file names; atos output matches symbol names.
if (lines_read == 0 && if (i == 0 &&
(strstr(line, "kj/common.c++") != nullptr || (strstr(line, "kj/common.c++") != nullptr ||
strstr(line, "kj/exception.") != nullptr || strstr(line, "kj/exception.") != nullptr ||
strstr(line, "kj/debug.") != nullptr || strstr(line, "kj/debug.") != nullptr ||
...@@ -104,7 +106,7 @@ String getStackSymbols(ArrayPtr<void* const> trace) { ...@@ -104,7 +106,7 @@ String getStackSymbols(ArrayPtr<void* const> trace) {
size_t len = strlen(line); size_t len = strlen(line);
if (len > 0 && line[len-1] == '\n') line[len-1] = '\0'; if (len > 0 && line[len-1] == '\n') line[len-1] = '\0';
lines[lines_read++] = str("\n", line, ": called here"); lines[i++] = str("\n", line, ": called here");
} }
// Skip remaining input. // Skip remaining input.
...@@ -112,14 +114,7 @@ String getStackSymbols(ArrayPtr<void* const> trace) { ...@@ -112,14 +114,7 @@ String getStackSymbols(ArrayPtr<void* const> trace) {
pclose(p); pclose(p);
cleanup: return strArray(arrayPtr(lines, i), "");
if (oldPreload != nullptr) {
setenv("LD_PRELOAD", oldPreload.cStr(), true);
}
pthread_mutex_unlock(&mutex);
return (lines_read > 0 ? strArray(arrayPtr(lines, lines_read), "") : nullptr);
#else #else
return nullptr; return nullptr;
#endif #endif
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment