mirror of
https://github.com/bytecodealliance/wasm-micro-runtime.git
synced 2025-07-15 08:48:33 +00:00
enable dump call stack to a buffer
This commit is contained in:
parent
53b775aa4b
commit
53115b2b37
|
@ -1511,7 +1511,9 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
|
|||
|
||||
#if WASM_ENABLE_DUMP_CALL_STACK != 0
|
||||
if (!ret) {
|
||||
aot_dump_call_stack(exec_env);
|
||||
if (aot_create_call_stack(exec_env)) {
|
||||
aot_dump_call_stack(exec_env, true, NULL, 0);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -1568,7 +1570,9 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
|
|||
|
||||
#if WASM_ENABLE_DUMP_CALL_STACK != 0
|
||||
if (aot_get_exception(module_inst)) {
|
||||
aot_dump_call_stack(exec_env);
|
||||
if (aot_create_call_stack(exec_env)) {
|
||||
aot_dump_call_stack(exec_env, true, NULL, 0);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -3018,38 +3022,24 @@ aot_free_frame(WASMExecEnv *exec_env)
|
|||
|| (WASM_ENABLE_PERF_PROFILING != 0) */
|
||||
|
||||
#if WASM_ENABLE_DUMP_CALL_STACK != 0
|
||||
void
|
||||
aot_dump_call_stack(WASMExecEnv *exec_env)
|
||||
bool
|
||||
aot_create_call_stack(struct WASMExecEnv *exec_env)
|
||||
{
|
||||
AOTFrame *cur_frame = (AOTFrame *)exec_env->cur_frame,
|
||||
*first_frame = cur_frame;
|
||||
AOTModuleInstance *module_inst = (AOTModuleInstance *)exec_env->module_inst;
|
||||
const char *func_name;
|
||||
uint32 n = 0;
|
||||
|
||||
os_printf("\n");
|
||||
while (cur_frame) {
|
||||
func_name =
|
||||
get_func_name_from_index(module_inst, cur_frame->func_index);
|
||||
|
||||
/* function name not exported, print number instead */
|
||||
if (func_name == NULL) {
|
||||
os_printf("#%02d $f%d \n", n, cur_frame->func_index);
|
||||
}
|
||||
else {
|
||||
os_printf("#%02d %s \n", n, func_name);
|
||||
}
|
||||
|
||||
cur_frame = cur_frame->prev_frame;
|
||||
n++;
|
||||
}
|
||||
os_printf("\n");
|
||||
|
||||
/* release previous stack frames and create new ones */
|
||||
if (!bh_vector_destroy(module_inst->frames.ptr)
|
||||
|| !bh_vector_init(module_inst->frames.ptr, n, sizeof(WASMCApiFrame),
|
||||
false)) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
cur_frame = first_frame;
|
||||
|
@ -3059,14 +3049,86 @@ aot_dump_call_stack(WASMExecEnv *exec_env)
|
|||
frame.module_offset = 0;
|
||||
frame.func_index = cur_frame->func_index;
|
||||
frame.func_offset = 0;
|
||||
frame.func_name_wp =
|
||||
get_func_name_from_index(module_inst, cur_frame->func_index);
|
||||
|
||||
if (!bh_vector_append(module_inst->frames.ptr, &frame)) {
|
||||
bh_vector_destroy(module_inst->frames.ptr);
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
cur_frame = cur_frame->prev_frame;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#define PRINT_OR_DUMP() \
|
||||
do { \
|
||||
if (print) { \
|
||||
os_printf("%s", line_buf); \
|
||||
} \
|
||||
else if (buf) { \
|
||||
uint32 remain_len = len - total_len; \
|
||||
uint32 string_len = \
|
||||
snprintf(buf + total_len, remain_len, "%s", line_buf); \
|
||||
if (string_len >= remain_len) { \
|
||||
/* Buffer full */ \
|
||||
return len; \
|
||||
} \
|
||||
total_len += string_len; \
|
||||
} \
|
||||
else { \
|
||||
total_len += strlen(line_buf); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
uint32
|
||||
aot_dump_call_stack(WASMExecEnv *exec_env, bool print, char *buf, uint32 len)
|
||||
{
|
||||
AOTModuleInstance *module_inst = (AOTModuleInstance *)exec_env->module_inst;
|
||||
uint32 n = 0, total_len = 0, total_frames;
|
||||
/* reserve 256 bytes for line buffer, any line longer than 256 bytes
|
||||
* will be truncated */
|
||||
char line_buf[256];
|
||||
|
||||
if (!module_inst->frames.ptr) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
total_frames = bh_vector_size(module_inst->frames.ptr);
|
||||
if (total_frames == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
snprintf(line_buf, sizeof(line_buf), "\n");
|
||||
PRINT_OR_DUMP();
|
||||
|
||||
while (n < total_frames) {
|
||||
WASMCApiFrame frame = { 0 };
|
||||
|
||||
if (!bh_vector_get(module_inst->frames.ptr, n, &frame)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* function name not exported, print number instead */
|
||||
if (frame.func_name_wp == NULL) {
|
||||
snprintf(line_buf, sizeof(line_buf), "#%02d $f%d\n", n,
|
||||
frame.func_index);
|
||||
}
|
||||
else {
|
||||
snprintf(line_buf, sizeof(line_buf), "#%02d %s\n", n,
|
||||
frame.func_name_wp);
|
||||
}
|
||||
|
||||
PRINT_OR_DUMP();
|
||||
|
||||
n++;
|
||||
}
|
||||
snprintf(line_buf, sizeof(line_buf), "\n");
|
||||
PRINT_OR_DUMP();
|
||||
|
||||
return total_len + 1;
|
||||
}
|
||||
#endif /* end of WASM_ENABLE_DUMP_CALL_STACK */
|
||||
|
||||
|
|
|
@ -731,8 +731,24 @@ aot_alloc_frame(WASMExecEnv *exec_env, uint32 func_index);
|
|||
void
|
||||
aot_free_frame(WASMExecEnv *exec_env);
|
||||
|
||||
void
|
||||
aot_dump_call_stack(WASMExecEnv *exec_env);
|
||||
bool
|
||||
aot_create_call_stack(struct WASMExecEnv *exec_env);
|
||||
|
||||
/**
|
||||
* @brief dump aot call stack or get the size
|
||||
*
|
||||
* @param exec_env the execution environment
|
||||
* @param print print to stdout
|
||||
* @param buf buffer to store the dumped content
|
||||
* @param len length of the buffer
|
||||
*
|
||||
* @return when print is true, return the bytes printed out to stdout; when
|
||||
* print is false and buf is NULL, return the size required to store the
|
||||
* callstack content; when print is false and buf is not NULL, return the size
|
||||
* dumped to the buffer
|
||||
*/
|
||||
uint32
|
||||
aot_dump_call_stack(WASMExecEnv *exec_env, bool print, char *buf, uint32 len);
|
||||
|
||||
void
|
||||
aot_dump_perf_profiling(const AOTModuleInstance *module_inst);
|
||||
|
|
|
@ -4534,15 +4534,56 @@ wasm_runtime_dump_call_stack(WASMExecEnv *exec_env)
|
|||
wasm_exec_env_get_module_inst(exec_env);
|
||||
#if WASM_ENABLE_INTERP != 0
|
||||
if (module_inst->module_type == Wasm_Module_Bytecode) {
|
||||
wasm_interp_dump_call_stack(exec_env);
|
||||
wasm_interp_dump_call_stack(exec_env, true, NULL, 0);
|
||||
}
|
||||
#endif
|
||||
#if WASM_ENABLE_AOT != 0
|
||||
if (module_inst->module_type == Wasm_Module_AoT) {
|
||||
aot_dump_call_stack(exec_env);
|
||||
aot_dump_call_stack(exec_env, true, NULL, 0);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
uint32_t
|
||||
wasm_runtime_get_call_stack_buf_size(wasm_exec_env_t exec_env)
|
||||
{
|
||||
WASMModuleInstanceCommon *module_inst =
|
||||
wasm_exec_env_get_module_inst(exec_env);
|
||||
|
||||
#if WASM_ENABLE_INTERP != 0
|
||||
if (module_inst->module_type == Wasm_Module_Bytecode) {
|
||||
return wasm_interp_dump_call_stack(exec_env, false, NULL, 0);
|
||||
}
|
||||
#endif
|
||||
#if WASM_ENABLE_AOT != 0
|
||||
if (module_inst->module_type == Wasm_Module_AoT) {
|
||||
return aot_dump_call_stack(exec_env, false, NULL, 0);
|
||||
}
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t
|
||||
wasm_runtime_dump_call_stack_to_buf(wasm_exec_env_t exec_env, char *buf,
|
||||
uint32_t len)
|
||||
{
|
||||
WASMModuleInstanceCommon *module_inst =
|
||||
wasm_exec_env_get_module_inst(exec_env);
|
||||
|
||||
#if WASM_ENABLE_INTERP != 0
|
||||
if (module_inst->module_type == Wasm_Module_Bytecode) {
|
||||
return wasm_interp_dump_call_stack(exec_env, false, buf, len);
|
||||
}
|
||||
#endif
|
||||
#if WASM_ENABLE_AOT != 0
|
||||
if (module_inst->module_type == Wasm_Module_AoT) {
|
||||
return aot_dump_call_stack(exec_env, false, buf, len);
|
||||
}
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif /* end of WASM_ENABLE_DUMP_CALL_STACK */
|
||||
|
||||
bool
|
||||
|
|
|
@ -404,6 +404,7 @@ typedef struct wasm_frame_t {
|
|||
uint32 module_offset;
|
||||
uint32 func_index;
|
||||
uint32 func_offset;
|
||||
const char *func_name_wp;
|
||||
} WASMCApiFrame;
|
||||
|
||||
/* See wasm_export.h for description */
|
||||
|
|
|
@ -919,6 +919,7 @@ wasm_runtime_get_function_attachment(wasm_exec_env_t exec_env);
|
|||
*/
|
||||
WASM_RUNTIME_API_EXTERN void
|
||||
wasm_runtime_set_user_data(wasm_exec_env_t exec_env, void *user_data);
|
||||
|
||||
/**
|
||||
* Get the user data within execution environment.
|
||||
*
|
||||
|
@ -963,7 +964,7 @@ WASM_RUNTIME_API_EXTERN void
|
|||
wasm_runtime_set_max_thread_num(uint32_t num);
|
||||
|
||||
/**
|
||||
* spawn a new exec_env, the spawned exec_env
|
||||
* Spawn a new exec_env, the spawned exec_env
|
||||
* can be used in other threads
|
||||
*
|
||||
* @param num the original exec_env
|
||||
|
@ -982,7 +983,7 @@ WASM_RUNTIME_API_EXTERN void
|
|||
wasm_runtime_destroy_spawned_exec_env(wasm_exec_env_t exec_env);
|
||||
|
||||
/**
|
||||
* spawn a thread from the given exec_env
|
||||
* Spawn a thread from the given exec_env
|
||||
*
|
||||
* @param exec_env the original exec_env
|
||||
* @param tid thread id to be returned to the caller
|
||||
|
@ -996,7 +997,7 @@ wasm_runtime_spawn_thread(wasm_exec_env_t exec_env, wasm_thread_t *tid,
|
|||
wasm_thread_callback_t callback, void *arg);
|
||||
|
||||
/**
|
||||
* waits a spawned thread to terminate
|
||||
* Waits a spawned thread to terminate
|
||||
*
|
||||
* @param tid thread id
|
||||
* @param retval if not NULL, output the return value of the thread
|
||||
|
@ -1046,13 +1047,41 @@ WASM_RUNTIME_API_EXTERN bool
|
|||
wasm_externref_retain(uint32_t externref_idx);
|
||||
|
||||
/**
|
||||
* dump the call stack
|
||||
* Dump the call stack to stdout
|
||||
*
|
||||
* @param exec_env the execution environment
|
||||
*/
|
||||
WASM_RUNTIME_API_EXTERN void
|
||||
wasm_runtime_dump_call_stack(wasm_exec_env_t exec_env);
|
||||
|
||||
/**
|
||||
* Get the size required to store the call stack contents, including
|
||||
* the space for terminating null byte ('\0')
|
||||
*
|
||||
* @param exec_env the execution environment
|
||||
*
|
||||
* @return size required to store the contents, 0 means error
|
||||
*/
|
||||
WASM_RUNTIME_API_EXTERN uint32_t
|
||||
wasm_runtime_get_call_stack_buf_size(wasm_exec_env_t exec_env);
|
||||
|
||||
/**
|
||||
* Dump the call stack to buffer.
|
||||
*
|
||||
* @note this function is not thread-safe, please only use this API
|
||||
* when the exec_env is not executing
|
||||
*
|
||||
* @param exec_env the execution environment
|
||||
* @param buf buffer to store the dumped content
|
||||
* @param len length of the buffer
|
||||
*
|
||||
* @return bytes dumped to the buffer, including the terminating null
|
||||
* byte ('\0'), 0 means error and buf may remain untouched
|
||||
*/
|
||||
WASM_RUNTIME_API_EXTERN uint32_t
|
||||
wasm_runtime_dump_call_stack_to_buf(wasm_exec_env_t exec_env, char *buf,
|
||||
uint32_t len);
|
||||
|
||||
/**
|
||||
* Get a custom section by name
|
||||
*
|
||||
|
|
|
@ -3834,7 +3834,9 @@ wasm_interp_call_wasm(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
|
|||
}
|
||||
else {
|
||||
#if WASM_ENABLE_DUMP_CALL_STACK != 0
|
||||
wasm_interp_dump_call_stack(exec_env);
|
||||
if (wasm_interp_create_call_stack(exec_env)) {
|
||||
wasm_interp_dump_call_stack(exec_env, true, NULL, 0);
|
||||
}
|
||||
#endif
|
||||
LOG_DEBUG("meet an exception %s", wasm_get_exception(module_inst));
|
||||
}
|
||||
|
|
|
@ -3847,7 +3847,9 @@ wasm_interp_call_wasm(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
|
|||
}
|
||||
else {
|
||||
#if WASM_ENABLE_DUMP_CALL_STACK != 0
|
||||
wasm_interp_dump_call_stack(exec_env);
|
||||
if (wasm_interp_create_call_stack(exec_env)) {
|
||||
wasm_interp_dump_call_stack(exec_env, true, NULL, 0);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
|
|
@ -2485,8 +2485,8 @@ wasm_get_module_inst_mem_consumption(const WASMModuleInstance *module_inst,
|
|||
|| (WASM_ENABLE_MEMORY_TRACING != 0) */
|
||||
|
||||
#if WASM_ENABLE_DUMP_CALL_STACK != 0
|
||||
void
|
||||
wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env)
|
||||
bool
|
||||
wasm_interp_create_call_stack(struct WASMExecEnv *exec_env)
|
||||
{
|
||||
WASMModuleInstance *module_inst =
|
||||
(WASMModuleInstance *)wasm_exec_env_get_module_inst(exec_env);
|
||||
|
@ -2507,12 +2507,12 @@ wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env)
|
|||
if (!bh_vector_destroy(module_inst->frames)
|
||||
|| !bh_vector_init(module_inst->frames, n, sizeof(WASMCApiFrame),
|
||||
false)) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
cur_frame = first_frame;
|
||||
n = 0;
|
||||
os_printf("\n");
|
||||
|
||||
while (cur_frame) {
|
||||
WASMCApiFrame frame = { 0 };
|
||||
WASMFunctionInstance *func_inst = cur_frame->function;
|
||||
|
@ -2560,20 +2560,84 @@ wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env)
|
|||
}
|
||||
}
|
||||
|
||||
/* function name not exported, print number instead */
|
||||
if (func_name == NULL) {
|
||||
os_printf("#%02d $f%d \n", n, func_inst - module_inst->functions);
|
||||
}
|
||||
else {
|
||||
os_printf("#%02d %s \n", n, func_name);
|
||||
}
|
||||
frame.func_name_wp = func_name;
|
||||
|
||||
/* keep print */
|
||||
bh_vector_append(module_inst->frames, &frame);
|
||||
|
||||
cur_frame = cur_frame->prev_frame;
|
||||
n++;
|
||||
}
|
||||
os_printf("\n");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#define PRINT_OR_DUMP() \
|
||||
do { \
|
||||
if (print) { \
|
||||
os_printf("%s", line_buf); \
|
||||
} \
|
||||
else if (buf) { \
|
||||
uint32 remain_len = len - total_len; \
|
||||
uint32 string_len = \
|
||||
snprintf(buf + total_len, remain_len, "%s", line_buf); \
|
||||
if (string_len >= remain_len) { \
|
||||
/* Buffer full */ \
|
||||
return len; \
|
||||
} \
|
||||
total_len += string_len; \
|
||||
} \
|
||||
else { \
|
||||
total_len += strlen(line_buf); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
uint32
|
||||
wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env, bool print, char *buf,
|
||||
uint32 len)
|
||||
{
|
||||
WASMModuleInstance *module_inst =
|
||||
(WASMModuleInstance *)wasm_exec_env_get_module_inst(exec_env);
|
||||
uint32 n = 0, total_len = 0, total_frames;
|
||||
/* reserve 256 bytes for line buffer, any line longer than 256 bytes
|
||||
* will be truncated */
|
||||
char line_buf[256];
|
||||
|
||||
if (!module_inst->frames) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
total_frames = bh_vector_size(module_inst->frames);
|
||||
if (total_frames == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
snprintf(line_buf, sizeof(line_buf), "\n");
|
||||
PRINT_OR_DUMP();
|
||||
|
||||
while (n < total_frames) {
|
||||
WASMCApiFrame frame = { 0 };
|
||||
|
||||
if (!bh_vector_get(module_inst->frames, n, &frame)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* function name not exported, print number instead */
|
||||
if (frame.func_name_wp == NULL) {
|
||||
snprintf(line_buf, sizeof(line_buf), "#%02d $f%d\n", n,
|
||||
frame.func_index);
|
||||
}
|
||||
else {
|
||||
snprintf(line_buf, sizeof(line_buf), "#%02d %s\n", n,
|
||||
frame.func_name_wp);
|
||||
}
|
||||
|
||||
PRINT_OR_DUMP();
|
||||
|
||||
n++;
|
||||
}
|
||||
snprintf(line_buf, sizeof(line_buf), "\n");
|
||||
PRINT_OR_DUMP();
|
||||
|
||||
return total_len + 1;
|
||||
}
|
||||
#endif /* end of WASM_ENABLE_DUMP_CALL_STACK */
|
||||
|
|
|
@ -432,8 +432,25 @@ wasm_get_table_inst(const WASMModuleInstance *module_inst, const uint32 tbl_idx)
|
|||
}
|
||||
|
||||
#if WASM_ENABLE_DUMP_CALL_STACK != 0
|
||||
void
|
||||
wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env);
|
||||
bool
|
||||
wasm_interp_create_call_stack(struct WASMExecEnv *exec_env);
|
||||
|
||||
/**
|
||||
* @brief dump wasm call stack or get the size
|
||||
*
|
||||
* @param exec_env the execution environment
|
||||
* @param print print to stdout
|
||||
* @param buf buffer to store the dumped content
|
||||
* @param len length of the buffer
|
||||
*
|
||||
* @return when print is true, return the bytes printed out to stdout; when
|
||||
* print is false and buf is NULL, return the size required to store the
|
||||
* callstack content; when print is false and buf is not NULL, return the size
|
||||
* dumped to the buffer
|
||||
*/
|
||||
uint32
|
||||
wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env, bool print, char *buf,
|
||||
uint32 len);
|
||||
#endif
|
||||
|
||||
const uint8 *
|
||||
|
|
Loading…
Reference in New Issue
Block a user