From 5f908225d35566af3ab813d0b90b86c461b3b356 Mon Sep 17 00:00:00 2001 From: cs1111 Date: Mon, 8 Sep 2025 20:10:29 +0800 Subject: [PATCH] async stack open normally Signed-off-by: cs1111 Change-Id: I4e970797cb1626e6bc9848c64f46bd01edf6a196 --- src/dfx/async_stack/libuv_async_stack.c | 63 +++---------------------- src/dfx/async_stack/libuv_async_stack.h | 4 ++ src/timer.c | 7 ++- 3 files changed, 17 insertions(+), 57 deletions(-) diff --git a/src/dfx/async_stack/libuv_async_stack.c b/src/dfx/async_stack/libuv_async_stack.c index ddcc86b..88d737e 100644 --- a/src/dfx/async_stack/libuv_async_stack.c +++ b/src/dfx/async_stack/libuv_async_stack.c @@ -18,67 +18,18 @@ #include #include #include +static UvCollectAsyncStackFunc g_collectAsyncStackFunc = NULL; +static UvSetStackIdFunc g_setStackIdFunc = NULL; -typedef void(*LibuvSetStackIdFunc)(uint64_t stackId); -typedef uint64_t(*LibuvCollectAsyncStackFunc)(); -static LibuvCollectAsyncStackFunc g_collectAsyncStackFunc = NULL; -static LibuvSetStackIdFunc g_setStackIdFunc = NULL; -typedef enum { - ASYNC_DFX_NOT_INIT, - ASYNC_DFX_DISABLE, - ASYNC_DFX_ENABLE -} AsyncDfxInitStatus; - -static AsyncDfxInitStatus g_enabledLibuvAsyncStackStatus = ASYNC_DFX_NOT_INIT; -static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER; - -static void LoadDfxAsyncStackLib() -{ - g_enabledLibuvAsyncStackStatus = ASYNC_DFX_DISABLE; - const char* debuggableEnv = getenv("HAP_DEBUGGABLE"); - if ((debuggableEnv == NULL) || (strcmp(debuggableEnv, "true") != 0)) { - return; - } - - // if async stack is not enabled, the lib should not be unloaded - void* asyncStackLibHandle = dlopen("libasync_stack.z.so", RTLD_NOW); - if (asyncStackLibHandle == NULL) { - return; - } - - g_collectAsyncStackFunc = (LibuvCollectAsyncStackFunc)(dlsym(asyncStackLibHandle, "CollectAsyncStack")); - if (g_collectAsyncStackFunc == NULL) { - dlclose(asyncStackLibHandle); - asyncStackLibHandle = NULL; - return; - } - - g_setStackIdFunc = (LibuvSetStackIdFunc)(dlsym(asyncStackLibHandle, "SetStackId")); - if (g_setStackIdFunc == NULL) { - g_collectAsyncStackFunc = NULL; - dlclose(asyncStackLibHandle); - asyncStackLibHandle = NULL; - return; - } - - g_enabledLibuvAsyncStackStatus = ASYNC_DFX_ENABLE; -} - -static AsyncDfxInitStatus LibuvAsyncStackInit() +void LibuvSetAsyncStackFunc(UvCollectAsyncStackFunc collectAsyncStackFunc, UvSetStackIdFunc setStackIdFunc) { - if (g_enabledLibuvAsyncStackStatus == ASYNC_DFX_NOT_INIT) { - pthread_mutex_lock(&g_mutex); - if (g_enabledLibuvAsyncStackStatus == ASYNC_DFX_NOT_INIT) { - LoadDfxAsyncStackLib(); - } - pthread_mutex_unlock(&g_mutex); - } - return g_enabledLibuvAsyncStackStatus; + g_collectAsyncStackFunc = collectAsyncStackFunc; + g_setStackIdFunc = setStackIdFunc; } uint64_t LibuvCollectAsyncStack(void) { - if (LibuvAsyncStackInit() == ASYNC_DFX_ENABLE) { + if (g_collectAsyncStackFunc != NULL) { return g_collectAsyncStackFunc(); } @@ -87,7 +38,7 @@ uint64_t LibuvCollectAsyncStack(void) void LibuvSetStackId(uint64_t stackId) { - if (LibuvAsyncStackInit() == ASYNC_DFX_ENABLE) { + if (g_collectAsyncStackFunc != NULL) { return g_setStackIdFunc(stackId); } } diff --git a/src/dfx/async_stack/libuv_async_stack.h b/src/dfx/async_stack/libuv_async_stack.h index 5b592c4..ce1c1df 100644 --- a/src/dfx/async_stack/libuv_async_stack.h +++ b/src/dfx/async_stack/libuv_async_stack.h @@ -15,6 +15,10 @@ #ifndef LIBUV_DFX_ASYNC_STACK_H #define LIBUV_DFX_ASYNC_STACK_H #include +#include "uv.h" +typedef void(*UvSetStackIdFunc)(uint64_t stackId); +typedef uint64_t(*UvCollectAsyncStackFunc)(); uint64_t LibuvCollectAsyncStack(void); void LibuvSetStackId(uint64_t stackId); +UV_EXTERN void LibuvSetAsyncStackFunc(UvCollectAsyncStackFunc collectAsyncStackFunc, UvSetStackIdFunc setStackIdFunc); #endif /* LIBUV_DFX_ASYNC_STACK_H */ diff --git a/src/timer.c b/src/timer.c index 6bb1a45..379f343 100644 --- a/src/timer.c +++ b/src/timer.c @@ -66,6 +66,9 @@ int uv_timer_init(uv_loop_t* loop, uv_timer_t* handle) { handle->timer_cb = NULL; handle->timeout = 0; handle->repeat = 0; +#ifdef ASYNC_STACKTRACE + handle->u.reserved[3] = 0; +#endif return 0; } @@ -96,7 +99,9 @@ int uv_timer_start(uv_timer_t* handle, handle->start_id = handle->loop->timer_counter++; #ifdef ASYNC_STACKTRACE - handle->u.reserved[3] = (void*)LibuvCollectAsyncStack(); + if (handle->u.reserved[3] == 0) { + handle->u.reserved[3] = (void*)LibuvCollectAsyncStack(); + } #endif heap_insert(timer_heap(handle->loop), -- Gitee