@@ -900,7 +900,7 @@ execute_post_inst_function(WASMModuleInstance *module_inst)
900900        return  true;
901901
902902    return  wasm_create_exec_env_and_call_function (module_inst , post_inst_func ,
903-                                                   0 , NULL );
903+                                                   0 , NULL , false );
904904}
905905
906906#if  WASM_ENABLE_BULK_MEMORY  !=  0 
@@ -929,7 +929,7 @@ execute_memory_init_function(WASMModuleInstance *module_inst)
929929        return  true;
930930
931931    return  wasm_create_exec_env_and_call_function (module_inst , memory_init_func ,
932-                                                   0 , NULL );
932+                                                   0 , NULL , false );
933933}
934934#endif 
935935
@@ -944,7 +944,8 @@ execute_start_function(WASMModuleInstance *module_inst)
944944    bh_assert (!func -> is_import_func  &&  func -> param_cell_num  ==  0 
945945              &&  func -> ret_cell_num  ==  0 );
946946
947-     return  wasm_create_exec_env_and_call_function (module_inst , func , 0 , NULL );
947+     return  wasm_create_exec_env_and_call_function (module_inst , func , 0 , NULL ,
948+                                                   false);
948949}
949950
950951static  bool 
@@ -972,11 +973,11 @@ execute_malloc_function(WASMModuleInstance *module_inst,
972973    }
973974
974975    ret  =  wasm_create_exec_env_and_call_function (module_inst , malloc_func , argc ,
975-                                                  argv );
976+                                                  argv , false );
976977
977978    if  (retain_func  &&  ret ) {
978979        ret  =  wasm_create_exec_env_and_call_function (module_inst , retain_func ,
979-                                                      1 , argv );
980+                                                      1 , argv , false );
980981    }
981982
982983    if  (ret )
@@ -992,7 +993,7 @@ execute_free_function(WASMModuleInstance *module_inst,
992993
993994    argv [0 ] =  offset ;
994995    return  wasm_create_exec_env_and_call_function (module_inst , free_func , 1 ,
995-                                                   argv );
996+                                                   argv , false );
996997}
997998
998999#if  WASM_ENABLE_MULTI_MODULE  !=  0 
@@ -1125,6 +1126,19 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, uint32 stack_size,
11251126    if  (!module )
11261127        return  NULL ;
11271128
1129+ #if  WASM_ENABLE_DEBUG_INTERP  !=  0 
1130+     if  (!is_sub_inst ) {
1131+         os_mutex_lock (& module -> ref_count_lock );
1132+         if  (module -> ref_count  !=  0 ) {
1133+             LOG_WARNING (
1134+                 "warning: multiple instances referencing the same module may " 
1135+                 "cause unexpected behaviour during debugging" );
1136+         }
1137+         module -> ref_count ++ ;
1138+         os_mutex_unlock (& module -> ref_count_lock );
1139+     }
1140+ #endif 
1141+ 
11281142    /* Check heap size */ 
11291143    heap_size  =  align_uint (heap_size , 8 );
11301144    if  (heap_size  >  APP_HEAP_SIZE_MAX )
@@ -1133,6 +1147,13 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, uint32 stack_size,
11331147    /* Allocate the memory */ 
11341148    if  (!(module_inst  =  runtime_malloc (sizeof (WASMModuleInstance ), error_buf ,
11351149                                       error_buf_size ))) {
1150+ #if  WASM_ENABLE_DEBUG_INTERP  !=  0 
1151+         if  (!is_sub_inst ) {
1152+             os_mutex_lock (& module -> ref_count_lock );
1153+             module -> ref_count -- ;
1154+             os_mutex_unlock (& module -> ref_count_lock );
1155+         }
1156+ #endif 
11361157        return  NULL ;
11371158    }
11381159
@@ -1519,7 +1540,9 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, uint32 stack_size,
15191540        (WASMModuleInstanceCommon  * )module_inst );
15201541#endif 
15211542    (void )global_data_end ;
1543+ 
15221544    return  module_inst ;
1545+ 
15231546fail :
15241547    wasm_deinstantiate (module_inst , false);
15251548    return  NULL ;
@@ -1576,6 +1599,14 @@ wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst)
15761599    }
15771600#endif 
15781601
1602+ #if  WASM_ENABLE_DEBUG_INTERP  !=  0 
1603+     if  (!is_sub_inst ) {
1604+         os_mutex_lock (& module_inst -> module -> ref_count_lock );
1605+         module_inst -> module -> ref_count -- ;
1606+         os_mutex_unlock (& module_inst -> module -> ref_count_lock );
1607+     }
1608+ #endif 
1609+ 
15791610    wasm_runtime_free (module_inst );
15801611}
15811612
@@ -1661,7 +1692,8 @@ wasm_call_function(WASMExecEnv *exec_env, WASMFunctionInstance *function,
16611692bool 
16621693wasm_create_exec_env_and_call_function (WASMModuleInstance  * module_inst ,
16631694                                       WASMFunctionInstance  * func ,
1664-                                        unsigned  argc , uint32  argv [])
1695+                                        unsigned  argc , uint32  argv [],
1696+                                        bool  enable_debug )
16651697{
16661698    WASMExecEnv  * exec_env ;
16671699    bool  ret ;
@@ -1680,6 +1712,11 @@ wasm_create_exec_env_and_call_function(WASMModuleInstance *module_inst,
16801712        }
16811713
16821714#if  WASM_ENABLE_THREAD_MGR  !=  0 
1715+         if  (enable_debug ) {
1716+ #if  WASM_ENABLE_DEBUG_INTERP  !=  0 
1717+             wasm_runtime_start_debug_instance (exec_env );
1718+ #endif 
1719+         }
16831720    }
16841721#endif 
16851722
0 commit comments