From 3875c6649a3df14b87de2c9e86447176cf4c4c46 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Fri, 26 Aug 2022 17:33:47 +0900 Subject: [PATCH] wasm_runtime_start_debug_instance: Allow to override port (#1421) Allow the embedder to manage port number for this purpose by itself. --- core/iwasm/common/wasm_runtime_common.c | 10 ++++++++-- core/iwasm/common/wasm_runtime_common.h | 5 +++++ core/iwasm/include/wasm_export.h | 9 +++++++++ .../libraries/debug-engine/debug_engine.c | 18 +++++++++++------- .../libraries/debug-engine/debug_engine.h | 2 +- 5 files changed, 34 insertions(+), 10 deletions(-) diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index 1230b465..f2602959 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/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) 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 = 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; } - if (wasm_debug_instance_create(cluster)) { + if (wasm_debug_instance_create(cluster, port)) { return cluster->debug_inst->control_thread->port; } return 0; } + +uint32 +wasm_runtime_start_debug_instance(WASMExecEnv *exec_env) +{ + return wasm_runtime_start_debug_instance_with_port(exec_env, -1); +} #endif #if WASM_ENABLE_MULTI_MODULE != 0 diff --git a/core/iwasm/common/wasm_runtime_common.h b/core/iwasm/common/wasm_runtime_common.h index 50304f29..326dc003 100644 --- a/core/iwasm/common/wasm_runtime_common.h +++ b/core/iwasm/common/wasm_runtime_common.h @@ -563,6 +563,11 @@ wasm_runtime_call_wasm_v(WASMExecEnv *exec_env, uint32 num_args, ...); #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 */ WASM_RUNTIME_API_EXTERN uint32 wasm_runtime_start_debug_instance(WASMExecEnv *exec_env); diff --git a/core/iwasm/include/wasm_export.h b/core/iwasm/include/wasm_export.h index da388c85..4beb4d55 100644 --- a/core/iwasm/include/wasm_export.h +++ b/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. * * @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. */ 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); /** diff --git a/core/iwasm/libraries/debug-engine/debug_engine.c b/core/iwasm/libraries/debug-engine/debug_engine.c index 489a60a9..8ea85e7f 100644 --- a/core/iwasm/libraries/debug-engine/debug_engine.c +++ b/core/iwasm/libraries/debug-engine/debug_engine.c @@ -79,10 +79,12 @@ control_thread_routine(void *arg) control_thread->debug_instance = debug_inst; bh_strcpy_s(control_thread->ip_addr, sizeof(control_thread->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); @@ -91,6 +93,7 @@ control_thread_routine(void *arg) if (!control_thread->server) { LOG_ERROR("Failed to create debug server\n"); + control_thread->port = 0; os_cond_signal(&debug_inst->wait_cond); os_mutex_unlock(&debug_inst->wait_lock); return NULL; @@ -176,7 +179,7 @@ control_thread_routine(void *arg) } static WASMDebugControlThread * -wasm_debug_control_thread_create(WASMDebugInstance *debug_instance) +wasm_debug_control_thread_create(WASMDebugInstance *debug_instance, int32 port) { WASMDebugControlThread *control_thread; @@ -186,6 +189,7 @@ wasm_debug_control_thread_create(WASMDebugInstance *debug_instance) return NULL; } memset(control_thread, 0, sizeof(WASMDebugControlThread)); + control_thread->port = port; if (os_mutex_init(&control_thread->wait_lock) != 0) 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 and bound to a runtime cluster */ WASMDebugInstance * -wasm_debug_instance_create(WASMCluster *cluster) +wasm_debug_instance_create(WASMCluster *cluster, int32 port) { WASMDebugInstance *instance; 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; - 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"); goto fail3; } diff --git a/core/iwasm/libraries/debug-engine/debug_engine.h b/core/iwasm/libraries/debug-engine/debug_engine.h index 939d75f9..70901551 100644 --- a/core/iwasm/libraries/debug-engine/debug_engine.h +++ b/core/iwasm/libraries/debug-engine/debug_engine.h @@ -108,7 +108,7 @@ void on_thread_stop_event(WASMDebugInstance *debug_inst, WASMExecEnv *exec_env); WASMDebugInstance * -wasm_debug_instance_create(WASMCluster *cluster); +wasm_debug_instance_create(WASMCluster *cluster, int32 port); void wasm_debug_instance_destroy(WASMCluster *cluster);