Iterate callstack API

This commit is contained in:
Georgii Rylov
2025-01-17 16:16:45 +00:00
parent ba75b8fd56
commit 68e4534822
10 changed files with 154 additions and 2 deletions

View File

@ -26,6 +26,8 @@ typedef struct WASMInterpFrame {
/* Instruction pointer of the bytecode array. */
uint8 *ip;
uint32 func_index;
#if WASM_ENABLE_FAST_JIT != 0
uint8 *jitted_return_addr;
#endif

View File

@ -1264,9 +1264,11 @@ wasm_interp_call_func_native(WASMModuleInstance *module_inst,
init_frame_refs(frame_ref, local_cell_num, cur_func);
#endif
cur_func_index = (uint32)(cur_func - module_inst->e->functions);
frame->func_index = cur_func_index;
wasm_exec_env_set_cur_frame(exec_env, frame);
cur_func_index = (uint32)(cur_func - module_inst->e->functions);
bh_assert(cur_func_index < module_inst->module->import_function_count);
if (!func_import->call_conv_wasm_c_api) {
native_func_pointer = module_inst->import_func_ptrs[cur_func_index];

View File

@ -1205,9 +1205,11 @@ wasm_interp_call_func_native(WASMModuleInstance *module_inst,
init_frame_refs(frame->frame_ref, local_cell_num, cur_func);
#endif
cur_func_index = (uint32)(cur_func - module_inst->e->functions);
frame->func_index = cur_func_index;
wasm_exec_env_set_cur_frame(exec_env, frame);
cur_func_index = (uint32)(cur_func - module_inst->e->functions);
bh_assert(cur_func_index < module_inst->module->import_function_count);
if (!func_import->call_conv_wasm_c_api) {
native_func_pointer = module_inst->import_func_ptrs[cur_func_index];
@ -6032,6 +6034,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
/* Initialize the interpreter context. */
frame->function = cur_func;
frame->func_index = (uint32)(cur_func - module->e->functions);
frame_ip = wasm_get_func_code(cur_func);
frame_ip_end = wasm_get_func_code_end(cur_func);

View File

@ -5,6 +5,7 @@
#include "wasm_runtime.h"
#include "wasm.h"
#include "wasm_exec_env.h"
#include "wasm_loader.h"
#include "wasm_interp.h"
#include "bh_common.h"
@ -4196,6 +4197,35 @@ 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)
{
/*
* 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;
}
}
bool
wasm_interp_create_call_stack(struct WASMExecEnv *exec_env)
{

View File

@ -730,6 +730,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);
bool
wasm_interp_create_call_stack(struct WASMExecEnv *exec_env);