Browse Source

Support random debug port by assigning port = 0 (#807)

Support random debug port by assigning port = 0,
and update help and document.
Xu Jun 4 years ago
parent
commit
83df8600f7

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

@@ -59,10 +59,12 @@ control_thread_routine(void *arg)
     control_thread->debug_engine = g_debug_engine;
     control_thread->debug_instance = debug_inst;
     strcpy(control_thread->ip_addr, g_debug_engine->ip_addr);
-    control_thread->port = g_debug_engine->process_base_port + debug_inst->id;
+    control_thread->port =
+        (g_debug_engine->process_base_port == 0)
+            ? 0
+            : g_debug_engine->process_base_port + debug_inst->id;
 
-    LOG_WARNING("control thread of debug object %p start at %s:%d\n",
-                debug_inst, control_thread->ip_addr, control_thread->port);
+    LOG_WARNING("control thread of debug object %p start\n", debug_inst);
 
     control_thread->server =
         wasm_launch_gdbserver(control_thread->ip_addr, control_thread->port);
@@ -152,8 +154,7 @@ static void
 wasm_debug_control_thread_destroy(WASMDebugInstance *debug_instance)
 {
     WASMDebugControlThread *control_thread = debug_instance->control_thread;
-    LOG_VERBOSE("control thread of debug object %p stop at %s:%d\n",
-                debug_instance, control_thread->ip_addr, control_thread->port);
+    LOG_VERBOSE("control thread of debug object %p stop\n", debug_instance);
     control_thread->status = STOPPED;
     os_mutex_lock(&control_thread->wait_lock);
     wasm_close_gdbserver(control_thread->server);
@@ -201,7 +202,7 @@ wasm_debug_engine_init(char *ip_addr, int platform_port, int process_port)
         g_debug_engine->platform_port =
             platform_port > 0 ? platform_port : 1234;
         g_debug_engine->process_base_port =
-            process_port > 0 ? process_port : 6169;
+            (process_port > 0) ? process_port : 0;
         if (ip_addr)
             sprintf(g_debug_engine->ip_addr, "%s", ip_addr);
         else

+ 9 - 1
core/iwasm/libraries/debug-engine/gdbserver.c

@@ -56,6 +56,7 @@ wasm_launch_gdbserver(char *host, int port)
     int listen_fd = -1;
     const int one = 1;
     struct sockaddr_in addr;
+    socklen_t socklen;
     int ret;
     int sockt_fd = 0;
 
@@ -87,7 +88,6 @@ wasm_launch_gdbserver(char *host, int port)
         goto fail;
     }
 
-    LOG_VERBOSE("Listening on %s:%d\n", host, port);
     addr.sin_family = AF_INET;
     addr.sin_addr.s_addr = inet_addr(host);
     addr.sin_port = htons(port);
@@ -98,6 +98,14 @@ wasm_launch_gdbserver(char *host, int port)
         goto fail;
     }
 
+    socklen = sizeof(addr);
+    if (getsockname(listen_fd, (void *)&addr, &socklen) == -1) {
+        LOG_ERROR("%s", strerror(errno));
+        goto fail;
+    }
+
+    LOG_WARNING("Listening on %s:%d\n", host, ntohs(addr.sin_port));
+
     ret = listen(listen_fd, 1);
     if (ret < 0) {
         LOG_ERROR("wasm gdb server error: listen() failed");

+ 1 - 0
doc/source_debugging.md

@@ -30,6 +30,7 @@ make
 3. Execute iwasm with debug engine enabled
 ``` bash
 iwasm -g=127.0.0.1:1234 test.wasm
+# Use port = 0 to allow a random assigned debug port
 ```
 
 4. Build customized lldb (assume you have already cloned llvm)

+ 1 - 0
product-mini/platforms/posix/main.c

@@ -53,6 +53,7 @@ print_help()
 #endif
 #if WASM_ENABLE_DEBUG_INTERP != 0
     printf("  -g=ip:port             Set the debug sever address, default is debug disabled\n");
+    printf("                           if port is 0, then a random port will be used\n");
 #endif
     return 1;
 }