Răsfoiți Sursa

wasm_runtime_start_debug_instance: Allow to override port (#1421)

Allow the embedder to manage port number for this purpose by itself.
YAMAMOTO Takashi 3 ani în urmă
părinte
comite
3875c6649a

+ 8 - 2
core/iwasm/common/wasm_runtime_common.c

@@ -507,7 +507,7 @@ wasm_runtime_is_xip_file(const uint8 *buf, uint32 size)
 
 
 #if (WASM_ENABLE_THREAD_MGR != 0) && (WASM_ENABLE_DEBUG_INTERP != 0)
 #if (WASM_ENABLE_THREAD_MGR != 0) && (WASM_ENABLE_DEBUG_INTERP != 0)
 uint32
 uint32
-wasm_runtime_start_debug_instance(WASMExecEnv *exec_env)
+wasm_runtime_start_debug_instance_with_port(WASMExecEnv *exec_env, int32_t port)
 {
 {
     WASMModuleInstanceCommon *module_inst =
     WASMModuleInstanceCommon *module_inst =
         wasm_runtime_get_module_inst(exec_env);
         wasm_runtime_get_module_inst(exec_env);
@@ -525,12 +525,18 @@ wasm_runtime_start_debug_instance(WASMExecEnv *exec_env)
         return cluster->debug_inst->control_thread->port;
         return cluster->debug_inst->control_thread->port;
     }
     }
 
 
-    if (wasm_debug_instance_create(cluster)) {
+    if (wasm_debug_instance_create(cluster, port)) {
         return cluster->debug_inst->control_thread->port;
         return cluster->debug_inst->control_thread->port;
     }
     }
 
 
     return 0;
     return 0;
 }
 }
+
+uint32
+wasm_runtime_start_debug_instance(WASMExecEnv *exec_env)
+{
+    return wasm_runtime_start_debug_instance_with_port(exec_env, -1);
+}
 #endif
 #endif
 
 
 #if WASM_ENABLE_MULTI_MODULE != 0
 #if WASM_ENABLE_MULTI_MODULE != 0

+ 5 - 0
core/iwasm/common/wasm_runtime_common.h

@@ -563,6 +563,11 @@ wasm_runtime_call_wasm_v(WASMExecEnv *exec_env,
                          uint32 num_args, ...);
                          uint32 num_args, ...);
 
 
 #if WASM_ENABLE_DEBUG_INTERP != 0
 #if WASM_ENABLE_DEBUG_INTERP != 0
+/* See wasm_export.h for description */
+WASM_RUNTIME_API_EXTERN uint32
+wasm_runtime_start_debug_instance_with_port(WASMExecEnv *exec_env,
+                                            int32_t port);
+
 /* See wasm_export.h for description */
 /* See wasm_export.h for description */
 WASM_RUNTIME_API_EXTERN uint32
 WASM_RUNTIME_API_EXTERN uint32
 wasm_runtime_start_debug_instance(WASMExecEnv *exec_env);
 wasm_runtime_start_debug_instance(WASMExecEnv *exec_env);

+ 9 - 0
core/iwasm/include/wasm_export.h

@@ -516,10 +516,19 @@ wasm_runtime_get_exec_env_singleton(wasm_module_inst_t module_inst);
  *   they are sharing the same cluster with the main exec_env.
  *   they are sharing the same cluster with the main exec_env.
  *
  *
  * @param exec_env the execution environment to start debug instance
  * @param exec_env the execution environment to start debug instance
+ * @param port     the port for the debug server to listen on.
+ *                 0 means automatic assignment.
+ *                 -1 means to use the global setting in RuntimeInitArgs.
  *
  *
  * @return debug port if success, 0 otherwise.
  * @return debug port if success, 0 otherwise.
  */
  */
 WASM_RUNTIME_API_EXTERN uint32_t
 WASM_RUNTIME_API_EXTERN uint32_t
+wasm_runtime_start_debug_instance_with_port(wasm_exec_env_t exec_env, int32_t port);
+
+/**
+ * Same as wasm_runtime_start_debug_instance_with_port(env, -1).
+ */
+WASM_RUNTIME_API_EXTERN uint32_t
 wasm_runtime_start_debug_instance(wasm_exec_env_t exec_env);
 wasm_runtime_start_debug_instance(wasm_exec_env_t exec_env);
 
 
 /**
 /**

+ 11 - 7
core/iwasm/libraries/debug-engine/debug_engine.c

@@ -79,10 +79,12 @@ control_thread_routine(void *arg)
     control_thread->debug_instance = debug_inst;
     control_thread->debug_instance = debug_inst;
     bh_strcpy_s(control_thread->ip_addr, sizeof(control_thread->ip_addr),
     bh_strcpy_s(control_thread->ip_addr, sizeof(control_thread->ip_addr),
                 g_debug_engine->ip_addr);
                 g_debug_engine->ip_addr);
-    control_thread->port =
-        (g_debug_engine->process_base_port == 0)
-            ? 0
-            : g_debug_engine->process_base_port + debug_inst->id - 1;
+    if (control_thread->port == -1) {
+        control_thread->port =
+            (g_debug_engine->process_base_port == 0)
+                ? 0
+                : g_debug_engine->process_base_port + debug_inst->id - 1;
+    }
 
 
     LOG_WARNING("control thread of debug object %p start\n", debug_inst);
     LOG_WARNING("control thread of debug object %p start\n", debug_inst);
 
 
@@ -91,6 +93,7 @@ control_thread_routine(void *arg)
 
 
     if (!control_thread->server) {
     if (!control_thread->server) {
         LOG_ERROR("Failed to create debug server\n");
         LOG_ERROR("Failed to create debug server\n");
+        control_thread->port = 0;
         os_cond_signal(&debug_inst->wait_cond);
         os_cond_signal(&debug_inst->wait_cond);
         os_mutex_unlock(&debug_inst->wait_lock);
         os_mutex_unlock(&debug_inst->wait_lock);
         return NULL;
         return NULL;
@@ -176,7 +179,7 @@ control_thread_routine(void *arg)
 }
 }
 
 
 static WASMDebugControlThread *
 static WASMDebugControlThread *
-wasm_debug_control_thread_create(WASMDebugInstance *debug_instance)
+wasm_debug_control_thread_create(WASMDebugInstance *debug_instance, int32 port)
 {
 {
     WASMDebugControlThread *control_thread;
     WASMDebugControlThread *control_thread;
 
 
@@ -186,6 +189,7 @@ wasm_debug_control_thread_create(WASMDebugInstance *debug_instance)
         return NULL;
         return NULL;
     }
     }
     memset(control_thread, 0, sizeof(WASMDebugControlThread));
     memset(control_thread, 0, sizeof(WASMDebugControlThread));
+    control_thread->port = port;
 
 
     if (os_mutex_init(&control_thread->wait_lock) != 0)
     if (os_mutex_init(&control_thread->wait_lock) != 0)
         goto fail;
         goto fail;
@@ -309,7 +313,7 @@ wasm_debug_engine_init(char *ip_addr, int32 process_port)
 /* A debug Instance is a debug "process" in gdb remote protocol
 /* A debug Instance is a debug "process" in gdb remote protocol
    and bound to a runtime cluster */
    and bound to a runtime cluster */
 WASMDebugInstance *
 WASMDebugInstance *
-wasm_debug_instance_create(WASMCluster *cluster)
+wasm_debug_instance_create(WASMCluster *cluster, int32 port)
 {
 {
     WASMDebugInstance *instance;
     WASMDebugInstance *instance;
     WASMExecEnv *exec_env = NULL;
     WASMExecEnv *exec_env = NULL;
@@ -359,7 +363,7 @@ wasm_debug_instance_create(WASMCluster *cluster)
     }
     }
     instance->exec_mem_info.current_pos = instance->exec_mem_info.start_offset;
     instance->exec_mem_info.current_pos = instance->exec_mem_info.start_offset;
 
 
-    if (!wasm_debug_control_thread_create(instance)) {
+    if (!wasm_debug_control_thread_create(instance, port)) {
         LOG_ERROR("WASM Debug Engine error: failed to create control thread");
         LOG_ERROR("WASM Debug Engine error: failed to create control thread");
         goto fail3;
         goto fail3;
     }
     }

+ 1 - 1
core/iwasm/libraries/debug-engine/debug_engine.h

@@ -108,7 +108,7 @@ void
 on_thread_stop_event(WASMDebugInstance *debug_inst, WASMExecEnv *exec_env);
 on_thread_stop_event(WASMDebugInstance *debug_inst, WASMExecEnv *exec_env);
 
 
 WASMDebugInstance *
 WASMDebugInstance *
-wasm_debug_instance_create(WASMCluster *cluster);
+wasm_debug_instance_create(WASMCluster *cluster, int32 port);
 
 
 void
 void
 wasm_debug_instance_destroy(WASMCluster *cluster);
 wasm_debug_instance_destroy(WASMCluster *cluster);