diff --git a/core/iwasm/aot/aot_runtime.c b/core/iwasm/aot/aot_runtime.c index 2d5f6fde7..134cd9858 100644 --- a/core/iwasm/aot/aot_runtime.c +++ b/core/iwasm/aot/aot_runtime.c @@ -4104,37 +4104,41 @@ aot_frame_update_profile_info(WASMExecEnv *exec_env, bool alloc_frame) #endif /* end of WASM_ENABLE_AOT_STACK_FRAME != 0 */ #if WASM_ENABLE_DUMP_CALL_STACK != 0 -void -aot_iterate_callstack(WASMExecEnv *exec_env, const wasm_frame_callback frame_handler, void* user_data) +void +aot_iterate_callstack(WASMExecEnv *exec_env, + const wasm_frame_callback frame_handler, void *user_data) { -/* -* Note for devs: please refrain from such modifications inside of aot_iterate_callstack - * - any allocations/freeing memory - * - dereferencing any pointers other than: exec_env, exec_env->module_inst, - * exec_env->module_inst->module, pointers between stack's bottom and top_boundary - * For more details check wasm_iterate_callstack in wasm_export.h -*/ + /* + * Note for devs: please refrain from such modifications inside of + * aot_iterate_callstack + * - any allocations/freeing memory + * - dereferencing any pointers other than: exec_env, exec_env->module_inst, + * exec_env->module_inst->module, pointers between stack's bottom and + * top_boundary For more details check wasm_iterate_callstack in + * wasm_export.h + */ if (!is_tiny_frame(exec_env)) { - //TODO: support standard frames + // TODO: support standard frames return; } - uint8* top_boundary = exec_env->wasm_stack.top_boundary; - uint8* top = exec_env->wasm_stack.top; - uint8* bottom = exec_env->wasm_stack.bottom; + uint8 *top_boundary = exec_env->wasm_stack.top_boundary; + uint8 *top = exec_env->wasm_stack.top; + uint8 *bottom = exec_env->wasm_stack.bottom; - bool is_top_index_in_range = top_boundary >= top && top >= (bottom + sizeof(AOTTinyFrame)); + bool is_top_index_in_range = + top_boundary >= top && top >= (bottom + sizeof(AOTTinyFrame)); if (!is_top_index_in_range) { return; } - bool is_top_aligned_with_bottom = (unsigned long)(top - bottom) % sizeof(AOTTinyFrame) == 0; + bool is_top_aligned_with_bottom = + (unsigned long)(top - bottom) % sizeof(AOTTinyFrame) == 0; if (!is_top_aligned_with_bottom) { return; } - AOTTinyFrame* frame = (AOTTinyFrame*)(top - sizeof(AOTTinyFrame)); + AOTTinyFrame *frame = (AOTTinyFrame *)(top - sizeof(AOTTinyFrame)); WASMCApiFrame record_frame; - while (frame && - (uint8_t*)frame >= bottom) { + while (frame && (uint8_t *)frame >= bottom) { record_frame.instance = exec_env->module_inst; record_frame.module_offset = 0; record_frame.func_index = frame->func_index; diff --git a/core/iwasm/aot/aot_runtime.h b/core/iwasm/aot/aot_runtime.h index 5405772c9..7a26be3ea 100644 --- a/core/iwasm/aot/aot_runtime.h +++ b/core/iwasm/aot/aot_runtime.h @@ -777,8 +777,9 @@ aot_frame_update_profile_info(WASMExecEnv *exec_env, bool alloc_frame); bool aot_create_call_stack(struct WASMExecEnv *exec_env); -void -aot_iterate_callstack(WASMExecEnv *exec_env, const wasm_frame_callback frame_handler, void* user_data); +void +aot_iterate_callstack(WASMExecEnv *exec_env, + const wasm_frame_callback frame_handler, void *user_data); /** * @brief Dump wasm call stack or get the size diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index b631a9e11..acf6f5544 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -1741,31 +1741,36 @@ wasm_runtime_destroy_exec_env(WASMExecEnv *exec_env) wasm_exec_env_destroy(exec_env); } -void -wasm_iterate_callstack(const wasm_exec_env_t exec_env, const wasm_frame_callback frame_callback, void* user_data) +void +wasm_iterate_callstack(const wasm_exec_env_t exec_env, + const wasm_frame_callback frame_callback, + void *user_data) { -/* -* Note for devs: please refrain from such modifications inside of wasm_iterate_callstack - * - any allocations/freeing memory - * - dereferencing any pointers other than: exec_env, exec_env->module_inst, - * exec_env->module_inst->module, pointers between stack's bottom and top_boundary - * For more details check wasm_iterate_callstack in wasm_export.h -*/ - #if WASM_ENABLE_DUMP_CALL_STACK - WASMModuleInstance* module_inst = (WASMModuleInstance *)get_module_inst(exec_env); + /* + * Note for devs: please refrain from such modifications inside of + * wasm_iterate_callstack + * - any allocations/freeing memory + * - dereferencing any pointers other than: exec_env, exec_env->module_inst, + * exec_env->module_inst->module, pointers between stack's bottom and + * top_boundary For more details check wasm_iterate_callstack in + * wasm_export.h + */ +#if WASM_ENABLE_DUMP_CALL_STACK + WASMModuleInstance *module_inst = + (WASMModuleInstance *)get_module_inst(exec_env); - #if WASM_ENABLE_INTERP != 0 +#if WASM_ENABLE_INTERP != 0 if (module_inst->module_type == Wasm_Module_Bytecode) { wasm_interp_iterate_callstack(exec_env, frame_callback, user_data); } - #endif +#endif - #if WASM_ENABLE_AOT != 0 +#if WASM_ENABLE_AOT != 0 if (module_inst->module_type == Wasm_Module_AoT) { aot_iterate_callstack(exec_env, frame_callback, user_data); } - #endif - #endif +#endif +#endif } bool diff --git a/core/iwasm/common/wasm_runtime_common.h b/core/iwasm/common/wasm_runtime_common.h index 6157c6431..1650615ef 100644 --- a/core/iwasm/common/wasm_runtime_common.h +++ b/core/iwasm/common/wasm_runtime_common.h @@ -652,8 +652,10 @@ wasm_runtime_create_exec_env(WASMModuleInstanceCommon *module_inst, WASM_RUNTIME_API_EXTERN void wasm_runtime_destroy_exec_env(WASMExecEnv *exec_env); -WASM_RUNTIME_API_EXTERN void -wasm_iterate_callstack(const wasm_exec_env_t exec_env, const wasm_frame_callback frame_handler, void* user_data); +WASM_RUNTIME_API_EXTERN void +wasm_iterate_callstack(const wasm_exec_env_t exec_env, + const wasm_frame_callback frame_handler, + void *user_data); /* See wasm_export.h for description */ WASM_RUNTIME_API_EXTERN WASMModuleInstanceCommon * diff --git a/core/iwasm/include/wasm_export.h b/core/iwasm/include/wasm_export.h index cf75eeb6c..ca6020c3a 100644 --- a/core/iwasm/include/wasm_export.h +++ b/core/iwasm/include/wasm_export.h @@ -127,7 +127,7 @@ struct WASMMemoryInstance; typedef struct WASMMemoryInstance *wasm_memory_inst_t; struct wasm_frame_t; -typedef struct wasm_frame_t * wasm_frame_ptr_t; +typedef struct wasm_frame_t *wasm_frame_ptr_t; /* WASM section */ typedef struct wasm_section_t { @@ -867,21 +867,20 @@ wasm_runtime_create_exec_env(wasm_module_inst_t module_inst, WASM_RUNTIME_API_EXTERN void wasm_runtime_destroy_exec_env(wasm_exec_env_t exec_env); - -typedef bool (*wasm_frame_callback)(void*, wasm_frame_ptr_t); +typedef bool (*wasm_frame_callback)(void *, wasm_frame_ptr_t); /** * @brief Iterate over callstack frames and execute callback on it. * - * Caution: This is not a thread-safe function. Ensure the exec_env + * Caution: This is not a thread-safe function. Ensure the exec_env * is suspended before calling it from another thread. * - * Usage: In the callback to read frames fields use APIs + * Usage: In the callback to read frames fields use APIs * for wasm_frame_t from wasm_c_api.h * - * Note: The function is async-signal-safe if called with verified arguments. - * Meaning it's safe to call it from a signal handler even on a signal interruption - * from another thread if next variables hold valid pointers + * Note: The function is async-signal-safe if called with verified arguments. + * Meaning it's safe to call it from a signal handler even on a signal + * interruption from another thread if next variables hold valid pointers * - exec_env * - exec_env->module_inst * - exec_env->module_inst->module @@ -889,14 +888,16 @@ typedef bool (*wasm_frame_callback)(void*, wasm_frame_ptr_t); * Note for devs: please refrain from such modifications inside of this call * - any allocations/freeing memory * - dereferencing any pointers other than: exec_env, exec_env->module_inst, - * exec_env->module_inst->module, pointers between stack's bottom and top_boundary + * exec_env->module_inst->module, pointers between stack's bottom and + * top_boundary * * @param exec_env the execution environment that containes frames * @param callback the callback function provided by the user * @param user_data context for callback provided by the user */ -WASM_RUNTIME_API_EXTERN void -wasm_iterate_callstack(const wasm_exec_env_t exec_env, const wasm_frame_callback callback, void *user_data); +WASM_RUNTIME_API_EXTERN void +wasm_iterate_callstack(const wasm_exec_env_t exec_env, + const wasm_frame_callback callback, void *user_data); /** * Get the singleton execution environment for the instance. diff --git a/core/iwasm/interpreter/wasm_runtime.c b/core/iwasm/interpreter/wasm_runtime.c index 529e0a2fc..90fed59ba 100644 --- a/core/iwasm/interpreter/wasm_runtime.c +++ b/core/iwasm/interpreter/wasm_runtime.c @@ -4197,32 +4197,36 @@ wasm_get_module_inst_mem_consumption(const WASMModuleInstance *module_inst, || (WASM_ENABLE_MEMORY_TRACING != 0) */ #if WASM_ENABLE_DUMP_CALL_STACK != 0 -uint32 -wasm_interp_iterate_callstack(WASMExecEnv *exec_env, const wasm_frame_callback frame_handler, void* user_data) +uint32 +wasm_interp_iterate_callstack(WASMExecEnv *exec_env, + const wasm_frame_callback frame_handler, + void *user_data) { -/* -* Note for devs: please refrain from such modifications inside of wasm_interp_iterate_callstack - * - any allocations/freeing memory - * - dereferencing any pointers other than: exec_env, exec_env->module_inst, - * exec_env->module_inst->module, pointers between stack's bottom and top_boundary - * For more details check wasm_iterate_callstack in wasm_export.h -*/ - WASMModuleInstance *module_inst = (WASMModuleInstance *)wasm_exec_env_get_module_inst(exec_env); - WASMInterpFrame* cur_frame = wasm_exec_env_get_cur_frame(exec_env); - uint8* top_boundary = exec_env->wasm_stack.top_boundary; - uint8* bottom = exec_env->wasm_stack.bottom; + /* + * Note for devs: please refrain from such modifications inside of + * wasm_interp_iterate_callstack + * - any allocations/freeing memory + * - dereferencing any pointers other than: exec_env, exec_env->module_inst, + * exec_env->module_inst->module, pointers between stack's bottom and + * top_boundary For more details check wasm_iterate_callstack in + * wasm_export.h + */ + WASMModuleInstance *module_inst = + (WASMModuleInstance *)wasm_exec_env_get_module_inst(exec_env); + WASMInterpFrame *cur_frame = wasm_exec_env_get_cur_frame(exec_env); + uint8 *top_boundary = exec_env->wasm_stack.top_boundary; + uint8 *bottom = exec_env->wasm_stack.bottom; WASMCApiFrame record_frame; - while (cur_frame && - (uint8_t*)cur_frame >= bottom && - (uint8_t*)cur_frame + sizeof(WASMInterpFrame) <= top_boundary) { - record_frame.instance = module_inst; - record_frame.module_offset = 0; - record_frame.func_index = cur_frame->func_index; - if (!frame_handler(user_data, &record_frame)) { - break; - } - cur_frame = cur_frame->prev_frame; + while (cur_frame && (uint8_t *)cur_frame >= bottom + && (uint8_t *)cur_frame + sizeof(WASMInterpFrame) <= top_boundary) { + record_frame.instance = module_inst; + record_frame.module_offset = 0; + record_frame.func_index = cur_frame->func_index; + if (!frame_handler(user_data, &record_frame)) { + break; + } + cur_frame = cur_frame->prev_frame; } } diff --git a/core/iwasm/interpreter/wasm_runtime.h b/core/iwasm/interpreter/wasm_runtime.h index 7322bb16c..d67461e91 100644 --- a/core/iwasm/interpreter/wasm_runtime.h +++ b/core/iwasm/interpreter/wasm_runtime.h @@ -731,8 +731,10 @@ wasm_get_table_inst(const WASMModuleInstance *module_inst, uint32 tbl_idx) #if WASM_ENABLE_DUMP_CALL_STACK != 0 -uint32 -wasm_interp_iterate_callstack(WASMExecEnv *exec_env, const wasm_frame_callback frame_handler, void* user_data); +uint32 +wasm_interp_iterate_callstack(WASMExecEnv *exec_env, + const wasm_frame_callback frame_handler, + void *user_data); bool wasm_interp_create_call_stack(struct WASMExecEnv *exec_env);