From 20b0337e0a8e8f6a3b84976dca17d2dcbc91e910 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Wed, 29 Mar 2023 22:25:17 +0000 Subject: [PATCH] Hook up backtraces on android. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4251 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- src/util.js.c | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/src/util.js.c b/src/util.js.c index 31f0b414..88a7b8eb 100644 --- a/src/util.js.c +++ b/src/util.js.c @@ -14,11 +14,11 @@ #include -#ifndef _WIN32 -#ifndef __ANDROID__ +#if defined(__ANDROID__) +#include +#elif !defined(_WIN32) #include #endif -#endif static JSValue _util_utf8_encode(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { @@ -469,13 +469,41 @@ const char* tf_util_backtrace_string() return tf_util_backtrace_to_string(buffer, count); } +#if defined(__ANDROID__) +typedef struct _android_backtrace_t +{ + void** current; + void** end; +} android_backtrace_t; + +static _Unwind_Reason_Code _android_unwind_callback(struct _Unwind_Context* context, void* arg) +{ + android_backtrace_t* state = arg; + uintptr_t pc = _Unwind_GetIP(context); + if (pc) + { + if (state->current == state->end) + { + return _URC_END_OF_STACK; + } + else + { + *state->current++ = (void*)pc; + } + } + return _URC_NO_REASON; +} +#endif + int tf_util_backtrace(void** buffer, int count) { #ifdef _WIN32 return CaptureStackBackTrace(0, count, buffer, NULL); -#elif !defined(__ANDROID__) - return backtrace(buffer, count); +#elif defined(__ANDROID__) + android_backtrace_t state = { .current = buffer, .end = buffer + count }; + _Unwind_Backtrace(_android_unwind_callback, &state); + return state.current - buffer; #else - return 0; + return backtrace(buffer, count); #endif }