Collect stacktraces without allocating kotlin objects.

This commit is contained in:
Alexander Shabalin
2021-07-12 08:49:33 +00:00
committed by Space
parent fca89c2118
commit 274dabb61d
4 changed files with 145 additions and 85 deletions
@@ -17,10 +17,9 @@
#include "Common.h"
#include "ExecFormat.h"
#include "Memory.h"
#include "KString.h"
#include "Natives.h"
#include "Porting.h"
#include "SourceInfo.h"
#include "Types.h"
#include "utf8.h"
@@ -30,23 +29,18 @@ namespace {
#if USE_GCC_UNWIND
struct Backtrace {
Backtrace(int count, int skip) : index(0), skipCount(skip) {
Backtrace(int count, int skip) : skipCount(skip) {
uint32_t size = count - skipCount;
if (size < 0) {
size = 0;
}
auto result = AllocArrayInstance(theNativePtrArrayTypeInfo, size, arrayHolder.slot());
// TODO: throw cached OOME?
RuntimeCheck(result != nullptr, "Cannot create backtrace array");
array.reserve(size);
}
void setNextElement(_Unwind_Ptr element) { Kotlin_NativePtrArray_set(obj(), index++, (KNativePtr)element); }
void setNextElement(_Unwind_Ptr element) { array.push_back(reinterpret_cast<void*>(element)); }
ObjHeader* obj() { return arrayHolder.obj(); }
int index;
int skipCount;
ObjHolder arrayHolder;
KStdVector<void*> array;
};
_Unwind_Reason_Code depthCountCallback(struct _Unwind_Context* context, void* arg) {
@@ -67,9 +61,6 @@ _Unwind_Reason_Code unwindCallback(struct _Unwind_Context* context, void* arg) {
#else
_Unwind_Ptr address = _Unwind_GetIP(context);
#endif
// We run the unwinding process in the native thread state. But setting a next element
// requires writing to a Kotlin array which must be performed in the runnable thread state.
kotlin::ThreadStateGuard guard(kotlin::ThreadState::kRunnable);
backtrace->setNextElement(address);
return _URC_NO_REASON;
@@ -79,9 +70,8 @@ _Unwind_Reason_Code unwindCallback(struct _Unwind_Context* context, void* arg) {
THREAD_LOCAL_VARIABLE bool disallowSourceInfo = false;
#if !KONAN_NO_BACKTRACE && !USE_GCC_UNWIND
SourceInfo getSourceInfo(KConstRef stackTrace, int32_t index) {
return disallowSourceInfo ? SourceInfo{.fileName = nullptr, .lineNumber = -1, .column = -1}
: Kotlin_getSourceInfo(*PrimitiveArrayAddressOfElementAt<KNativePtr>(stackTrace->array(), index));
SourceInfo getSourceInfo(void* symbol) {
return disallowSourceInfo ? SourceInfo{.fileName = nullptr, .lineNumber = -1, .column = -1} : Kotlin_getSourceInfo(symbol);
}
#endif
@@ -89,70 +79,65 @@ SourceInfo getSourceInfo(KConstRef stackTrace, int32_t index) {
// TODO: this implementation is just a hack, e.g. the result is inexact;
// however it is better to have an inexact stacktrace than not to have any.
extern "C" NO_INLINE OBJ_GETTER0(Kotlin_getCurrentStackTrace) {
NO_INLINE KStdVector<void*> kotlin::GetCurrentStackTrace(int extraSkipFrames) noexcept {
#if KONAN_NO_BACKTRACE
return AllocArrayInstance(theNativePtrArrayTypeInfo, 0, OBJ_RESULT);
return {};
#else
// Skips first 2 elements as irrelevant: this function and primary Throwable constructor.
constexpr int kSkipFrames = 2;
// Skips this function frame + anything asked by the caller.
const int kSkipFrames = 1 + extraSkipFrames;
#if USE_GCC_UNWIND
int depth = 0;
CallWithThreadState<ThreadState::kNative>(_Unwind_Backtrace, depthCountCallback, static_cast<void*>(&depth));
_Unwind_Backtrace(depthCountCallback, static_cast<void*>(&depth));
Backtrace result(depth, kSkipFrames);
if (result.obj()->array()->count_ > 0) {
CallWithThreadState<ThreadState::kNative>(_Unwind_Backtrace, unwindCallback, static_cast<void*>(&result));
if (result.array.capacity() > 0) {
_Unwind_Backtrace(unwindCallback, static_cast<void*>(&result));
}
RETURN_OBJ(result.obj());
return std::move(result.array);
#else
const int maxSize = 32;
void* buffer[maxSize];
int size = kotlin::CallWithThreadState<kotlin::ThreadState::kNative>(backtrace, buffer, maxSize);
if (size < kSkipFrames) return AllocArrayInstance(theNativePtrArrayTypeInfo, 0, OBJ_RESULT);
int size = backtrace(buffer, maxSize);
if (size < kSkipFrames) return {};
ObjHolder resultHolder;
ObjHeader* result = AllocArrayInstance(theNativePtrArrayTypeInfo, size - kSkipFrames, resultHolder.slot());
KStdVector<void*> result;
result.reserve(size - kSkipFrames);
for (int index = kSkipFrames; index < size; ++index) {
Kotlin_NativePtrArray_set(result, index - kSkipFrames, buffer[index]);
result.push_back(buffer[index]);
}
RETURN_OBJ(result);
return result;
#endif
#endif // !KONAN_NO_BACKTRACE
}
OBJ_GETTER(kotlin::GetStackTraceStrings, KConstRef stackTrace) {
KStdVector<KStdString> kotlin::GetStackTraceStrings(void* const* stackTrace, size_t stackTraceSize) noexcept {
#if KONAN_NO_BACKTRACE
ObjHeader* result = AllocArrayInstance(theArrayTypeInfo, 1, OBJ_RESULT);
ObjHolder holder;
CreateStringFromCString("<UNIMPLEMENTED>", holder.slot());
UpdateHeapRef(ArrayAddressOfElementAt(result->array(), 0), holder.obj());
return result;
KStdVector<KStdString> strings;
strings.push_back("<UNIMPLEMENTED>");
return strings;
#else
int32_t size = static_cast<int32_t>(stackTrace->array()->count_);
ObjHolder resultHolder;
ObjHeader* strings = AllocArrayInstance(theArrayTypeInfo, size, resultHolder.slot());
KStdVector<KStdString> strings;
strings.reserve(stackTraceSize);
#if USE_GCC_UNWIND
for (int32_t index = 0; index < size; ++index) {
KNativePtr address = Kotlin_NativePtrArray_get(stackTrace, index);
for (size_t index = 0; index < stackTraceSize; ++index) {
KNativePtr address = stackTrace[index];
char symbol[512];
if (!CallWithThreadState<ThreadState::kNative>(AddressToSymbol, (const void*)address, symbol, sizeof(symbol))) {
if (!AddressToSymbol(address, symbol, sizeof(symbol))) {
// Make empty string:
symbol[0] = '\0';
}
char line[512];
konan::snprintf(line, sizeof(line) - 1, "%s (%p)", symbol, (void*)(intptr_t)address);
ObjHolder holder;
CreateStringFromCString(line, holder.slot());
UpdateHeapRef(ArrayAddressOfElementAt(strings->array(), index), holder.obj());
strings.push_back(line);
}
#else
if (size > 0) {
char** symbols = CallWithThreadState<ThreadState::kNative>(
backtrace_symbols, PrimitiveArrayAddressOfElementAt<KNativePtr>(stackTrace->array(), 0), size);
if (stackTraceSize > 0) {
char** symbols = backtrace_symbols(stackTrace, static_cast<int>(stackTraceSize));
RuntimeCheck(symbols != nullptr, "Not enough memory to retrieve the stacktrace");
for (int32_t index = 0; index < size; ++index) {
auto sourceInfo = CallWithThreadState<ThreadState::kNative>(getSourceInfo, stackTrace, index);
for (size_t index = 0; index < stackTraceSize; ++index) {
KNativePtr address = stackTrace[index];
auto sourceInfo = getSourceInfo(address);
const char* symbol = symbols[index];
const char* result;
char line[1024];
@@ -167,15 +152,13 @@ OBJ_GETTER(kotlin::GetStackTraceStrings, KConstRef stackTrace) {
} else {
result = symbol;
}
ObjHolder holder;
CreateStringFromCString(result, holder.slot());
UpdateHeapRef(ArrayAddressOfElementAt(strings->array(), index), holder.obj());
strings.push_back(result);
}
// Not konan::free. Used to free memory allocated in backtrace_symbols where malloc is used.
free(symbols);
}
#endif
RETURN_OBJ(strings);
return strings;
#endif // !KONAN_NO_BACKTRACE
}
@@ -183,23 +166,23 @@ void kotlin::DisallowSourceInfo() {
disallowSourceInfo = true;
}
void kotlin::PrintStackTraceStderr() {
NO_INLINE void kotlin::PrintStackTraceStderr() {
// NOTE: This might be called from both runnable and native states (including in uninitialized runtime)
// TODO: This is intended for runtime use. Try to avoid memory allocations and signal unsafe functions.
kotlin::ThreadStateGuard guard(kotlin::ThreadState::kRunnable, true);
ObjHolder stackTrace;
Kotlin_getCurrentStackTrace(stackTrace.slot());
ObjHolder stackTraceStrings;
kotlin::GetStackTraceStrings(stackTrace.obj(), stackTraceStrings.slot());
ArrayHeader* stackTraceStringsArray = stackTraceStrings.obj()->array();
for (uint32_t i = 0; i < stackTraceStringsArray->count_; ++i) {
ArrayHeader* symbol = (*ArrayAddressOfElementAt(stackTraceStringsArray, i))->array();
auto* utf16 = CharArrayAddressOfElementAt(symbol, 0);
KStdString utf8;
utf8::with_replacement::utf16to8(utf16, utf16 + symbol->count_, std::back_inserter(utf8));
kotlin::ThreadStateGuard guard(kotlin::ThreadState::kNative);
konan::consoleErrorUtf8(utf8.c_str(), utf8.size());
// TODO: This might have to go into `GetCurrentStackTrace`, but this changes the generated stacktrace for
// `Throwable`.
#if KONAN_WINDOWS
// Skip this function and `_Unwind_Backtrace`.
constexpr int kSkipFrames = 2;
#else
// Skip this function.
constexpr int kSkipFrames = 1;
#endif
auto stackTrace = GetCurrentStackTrace(kSkipFrames);
auto stackTraceStrings = GetStackTraceStrings(stackTrace.data(), stackTrace.size());
for (auto& frame : stackTraceStrings) {
konan::consoleErrorUtf8(frame.c_str(), frame.size());
konan::consoleErrorf("\n");
}
}