Skip to content

Commit 3875c66

Browse files
authored
wasm_runtime_start_debug_instance: Allow to override port (#1421)
Allow the embedder to manage port number for this purpose by itself.
1 parent ee98b58 commit 3875c66

5 files changed

Lines changed: 34 additions & 10 deletions

File tree

core/iwasm/common/wasm_runtime_common.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ wasm_runtime_is_xip_file(const uint8 *buf, uint32 size)
507507

508508
#if (WASM_ENABLE_THREAD_MGR != 0) && (WASM_ENABLE_DEBUG_INTERP != 0)
509509
uint32
510-
wasm_runtime_start_debug_instance(WASMExecEnv *exec_env)
510+
wasm_runtime_start_debug_instance_with_port(WASMExecEnv *exec_env, int32_t port)
511511
{
512512
WASMModuleInstanceCommon *module_inst =
513513
wasm_runtime_get_module_inst(exec_env);
@@ -525,12 +525,18 @@ wasm_runtime_start_debug_instance(WASMExecEnv *exec_env)
525525
return cluster->debug_inst->control_thread->port;
526526
}
527527

528-
if (wasm_debug_instance_create(cluster)) {
528+
if (wasm_debug_instance_create(cluster, port)) {
529529
return cluster->debug_inst->control_thread->port;
530530
}
531531

532532
return 0;
533533
}
534+
535+
uint32
536+
wasm_runtime_start_debug_instance(WASMExecEnv *exec_env)
537+
{
538+
return wasm_runtime_start_debug_instance_with_port(exec_env, -1);
539+
}
534540
#endif
535541

536542
#if WASM_ENABLE_MULTI_MODULE != 0

core/iwasm/common/wasm_runtime_common.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,11 @@ wasm_runtime_call_wasm_v(WASMExecEnv *exec_env,
563563
uint32 num_args, ...);
564564

565565
#if WASM_ENABLE_DEBUG_INTERP != 0
566+
/* See wasm_export.h for description */
567+
WASM_RUNTIME_API_EXTERN uint32
568+
wasm_runtime_start_debug_instance_with_port(WASMExecEnv *exec_env,
569+
int32_t port);
570+
566571
/* See wasm_export.h for description */
567572
WASM_RUNTIME_API_EXTERN uint32
568573
wasm_runtime_start_debug_instance(WASMExecEnv *exec_env);

core/iwasm/include/wasm_export.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,10 +516,19 @@ wasm_runtime_get_exec_env_singleton(wasm_module_inst_t module_inst);
516516
* they are sharing the same cluster with the main exec_env.
517517
*
518518
* @param exec_env the execution environment to start debug instance
519+
* @param port the port for the debug server to listen on.
520+
* 0 means automatic assignment.
521+
* -1 means to use the global setting in RuntimeInitArgs.
519522
*
520523
* @return debug port if success, 0 otherwise.
521524
*/
522525
WASM_RUNTIME_API_EXTERN uint32_t
526+
wasm_runtime_start_debug_instance_with_port(wasm_exec_env_t exec_env, int32_t port);
527+
528+
/**
529+
* Same as wasm_runtime_start_debug_instance_with_port(env, -1).
530+
*/
531+
WASM_RUNTIME_API_EXTERN uint32_t
523532
wasm_runtime_start_debug_instance(wasm_exec_env_t exec_env);
524533

525534
/**

core/iwasm/libraries/debug-engine/debug_engine.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,12 @@ control_thread_routine(void *arg)
7979
control_thread->debug_instance = debug_inst;
8080
bh_strcpy_s(control_thread->ip_addr, sizeof(control_thread->ip_addr),
8181
g_debug_engine->ip_addr);
82-
control_thread->port =
83-
(g_debug_engine->process_base_port == 0)
84-
? 0
85-
: g_debug_engine->process_base_port + debug_inst->id - 1;
82+
if (control_thread->port == -1) {
83+
control_thread->port =
84+
(g_debug_engine->process_base_port == 0)
85+
? 0
86+
: g_debug_engine->process_base_port + debug_inst->id - 1;
87+
}
8688

8789
LOG_WARNING("control thread of debug object %p start\n", debug_inst);
8890

@@ -91,6 +93,7 @@ control_thread_routine(void *arg)
9193

9294
if (!control_thread->server) {
9395
LOG_ERROR("Failed to create debug server\n");
96+
control_thread->port = 0;
9497
os_cond_signal(&debug_inst->wait_cond);
9598
os_mutex_unlock(&debug_inst->wait_lock);
9699
return NULL;
@@ -176,7 +179,7 @@ control_thread_routine(void *arg)
176179
}
177180

178181
static WASMDebugControlThread *
179-
wasm_debug_control_thread_create(WASMDebugInstance *debug_instance)
182+
wasm_debug_control_thread_create(WASMDebugInstance *debug_instance, int32 port)
180183
{
181184
WASMDebugControlThread *control_thread;
182185

@@ -186,6 +189,7 @@ wasm_debug_control_thread_create(WASMDebugInstance *debug_instance)
186189
return NULL;
187190
}
188191
memset(control_thread, 0, sizeof(WASMDebugControlThread));
192+
control_thread->port = port;
189193

190194
if (os_mutex_init(&control_thread->wait_lock) != 0)
191195
goto fail;
@@ -309,7 +313,7 @@ wasm_debug_engine_init(char *ip_addr, int32 process_port)
309313
/* A debug Instance is a debug "process" in gdb remote protocol
310314
and bound to a runtime cluster */
311315
WASMDebugInstance *
312-
wasm_debug_instance_create(WASMCluster *cluster)
316+
wasm_debug_instance_create(WASMCluster *cluster, int32 port)
313317
{
314318
WASMDebugInstance *instance;
315319
WASMExecEnv *exec_env = NULL;
@@ -359,7 +363,7 @@ wasm_debug_instance_create(WASMCluster *cluster)
359363
}
360364
instance->exec_mem_info.current_pos = instance->exec_mem_info.start_offset;
361365

362-
if (!wasm_debug_control_thread_create(instance)) {
366+
if (!wasm_debug_control_thread_create(instance, port)) {
363367
LOG_ERROR("WASM Debug Engine error: failed to create control thread");
364368
goto fail3;
365369
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ void
108108
on_thread_stop_event(WASMDebugInstance *debug_inst, WASMExecEnv *exec_env);
109109

110110
WASMDebugInstance *
111-
wasm_debug_instance_create(WASMCluster *cluster);
111+
wasm_debug_instance_create(WASMCluster *cluster, int32 port);
112112

113113
void
114114
wasm_debug_instance_destroy(WASMCluster *cluster);

0 commit comments

Comments
 (0)