wasm_runtime_start_debug_instance: Allow to override port (#1421)

Allow the embedder to manage port number for this purpose by itself.
This commit is contained in:
YAMAMOTO Takashi 2022-08-26 17:33:47 +09:00 committed by GitHub
parent ee98b583d5
commit 3875c6649a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 34 additions and 10 deletions

View File

@ -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

View File

@ -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);

View File

@ -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);
/**

View File

@ -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;
}

View File

@ -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);