@@ -1511,7 +1511,9 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
15111511
15121512#if WASM_ENABLE_DUMP_CALL_STACK != 0
15131513 if (!ret ) {
1514- aot_dump_call_stack (exec_env );
1514+ if (aot_create_call_stack (exec_env )) {
1515+ aot_dump_call_stack (exec_env , true, NULL , 0 );
1516+ }
15151517 }
15161518#endif
15171519
@@ -1568,7 +1570,9 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
15681570
15691571#if WASM_ENABLE_DUMP_CALL_STACK != 0
15701572 if (aot_get_exception (module_inst )) {
1571- aot_dump_call_stack (exec_env );
1573+ if (aot_create_call_stack (exec_env )) {
1574+ aot_dump_call_stack (exec_env , true, NULL , 0 );
1575+ }
15721576 }
15731577#endif
15741578
@@ -3018,38 +3022,24 @@ aot_free_frame(WASMExecEnv *exec_env)
30183022 || (WASM_ENABLE_PERF_PROFILING != 0) */
30193023
30203024#if WASM_ENABLE_DUMP_CALL_STACK != 0
3021- void
3022- aot_dump_call_stack ( WASMExecEnv * exec_env )
3025+ bool
3026+ aot_create_call_stack ( struct WASMExecEnv * exec_env )
30233027{
30243028 AOTFrame * cur_frame = (AOTFrame * )exec_env -> cur_frame ,
30253029 * first_frame = cur_frame ;
30263030 AOTModuleInstance * module_inst = (AOTModuleInstance * )exec_env -> module_inst ;
3027- const char * func_name ;
30283031 uint32 n = 0 ;
30293032
3030- os_printf ("\n" );
30313033 while (cur_frame ) {
3032- func_name =
3033- get_func_name_from_index (module_inst , cur_frame -> func_index );
3034-
3035- /* function name not exported, print number instead */
3036- if (func_name == NULL ) {
3037- os_printf ("#%02d $f%d \n" , n , cur_frame -> func_index );
3038- }
3039- else {
3040- os_printf ("#%02d %s \n" , n , func_name );
3041- }
3042-
30433034 cur_frame = cur_frame -> prev_frame ;
30443035 n ++ ;
30453036 }
3046- os_printf ("\n" );
30473037
30483038 /* release previous stack frames and create new ones */
30493039 if (!bh_vector_destroy (module_inst -> frames .ptr )
30503040 || !bh_vector_init (module_inst -> frames .ptr , n , sizeof (WASMCApiFrame ),
30513041 false)) {
3052- return ;
3042+ return false ;
30533043 }
30543044
30553045 cur_frame = first_frame ;
@@ -3059,14 +3049,85 @@ aot_dump_call_stack(WASMExecEnv *exec_env)
30593049 frame .module_offset = 0 ;
30603050 frame .func_index = cur_frame -> func_index ;
30613051 frame .func_offset = 0 ;
3052+ frame .func_name_wp =
3053+ get_func_name_from_index (module_inst , cur_frame -> func_index );
30623054
30633055 if (!bh_vector_append (module_inst -> frames .ptr , & frame )) {
30643056 bh_vector_destroy (module_inst -> frames .ptr );
3065- return ;
3057+ return false ;
30663058 }
30673059
30683060 cur_frame = cur_frame -> prev_frame ;
30693061 }
3062+
3063+ return true;
3064+ }
3065+
3066+ #define PRINT_OR_DUMP () \
3067+ do { \
3068+ total_len += \
3069+ wasm_runtime_dump_line_buf_impl(line_buf, print, &buf, &len); \
3070+ if ((!print) && buf && (len == 0)) { \
3071+ return total_len; \
3072+ } \
3073+ } while (0)
3074+
3075+ uint32
3076+ aot_dump_call_stack (WASMExecEnv * exec_env , bool print , char * buf , uint32 len )
3077+ {
3078+ AOTModuleInstance * module_inst = (AOTModuleInstance * )exec_env -> module_inst ;
3079+ uint32 n = 0 , total_len = 0 , total_frames ;
3080+ /* reserve 256 bytes for line buffer, any line longer than 256 bytes
3081+ * will be truncated */
3082+ char line_buf [256 ];
3083+
3084+ if (!module_inst -> frames .ptr ) {
3085+ return 0 ;
3086+ }
3087+
3088+ total_frames = bh_vector_size (module_inst -> frames .ptr );
3089+ if (total_frames == 0 ) {
3090+ return 0 ;
3091+ }
3092+
3093+ snprintf (line_buf , sizeof (line_buf ), "\n" );
3094+ PRINT_OR_DUMP ();
3095+
3096+ while (n < total_frames ) {
3097+ WASMCApiFrame frame = { 0 };
3098+ uint32 line_length , i ;
3099+
3100+ if (!bh_vector_get (module_inst -> frames .ptr , n , & frame )) {
3101+ return 0 ;
3102+ }
3103+
3104+ /* function name not exported, print number instead */
3105+ if (frame .func_name_wp == NULL ) {
3106+ line_length = snprintf (line_buf , sizeof (line_buf ), "#%02d $f%d\n" ,
3107+ n , frame .func_index );
3108+ }
3109+ else {
3110+ line_length = snprintf (line_buf , sizeof (line_buf ), "#%02d %s\n" , n ,
3111+ frame .func_name_wp );
3112+ }
3113+
3114+ if (line_length >= sizeof (line_buf )) {
3115+ uint32 line_buffer_len = sizeof (line_buf );
3116+ /* If line too long, ensure the last character is '\n' */
3117+ for (i = line_buffer_len - 5 ; i < line_buffer_len - 2 ; i ++ ) {
3118+ line_buf [i ] = '.' ;
3119+ }
3120+ line_buf [line_buffer_len - 2 ] = '\n' ;
3121+ }
3122+
3123+ PRINT_OR_DUMP ();
3124+
3125+ n ++ ;
3126+ }
3127+ snprintf (line_buf , sizeof (line_buf ), "\n" );
3128+ PRINT_OR_DUMP ();
3129+
3130+ return total_len + 1 ;
30703131}
30713132#endif /* end of WASM_ENABLE_DUMP_CALL_STACK */
30723133
0 commit comments