[Lldb-commits] [lldb] [lldb-dap] Added "port" property to vscode "attach" command. (PR #91570)

Santhosh Kumar Ellendula via lldb-commits lldb-commits at lists.llvm.org
Fri Jun 7 13:45:13 PDT 2024


================
@@ -0,0 +1,202 @@
+"""
+Test lldb-dap "port" configuration to "attach" request
+"""
+
+
+import dap_server
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test import lldbutil
+from lldbsuite.test import lldbplatformutil
+import lldbdap_testcase
+import os
+import shutil
+import subprocess
+import tempfile
+import threading
+import sys
+import socket
+import select
+
+
+# A class representing a pipe for communicating with debug server.
+# This class includes menthods to open the pipe and read the port number from it.
+class Pipe(object):
+    def __init__(self, prefix):
+        self.name = os.path.join(prefix, "stub_port_number")
+        os.mkfifo(self.name)
+        self._fd = os.open(self.name, os.O_RDONLY | os.O_NONBLOCK)
+
+    def finish_connection(self, timeout):
+        pass
+
+    def read(self, size, timeout):
+        (readers, _, _) = select.select([self._fd], [], [], timeout)
+        if self._fd not in readers:
+            raise TimeoutError
+        return os.read(self._fd, size)
+
+    def close(self):
+        os.close(self._fd)
+
+
+class TestDAP_attachByPortNum(lldbdap_testcase.DAPTestCaseBase):
+    default_timeout = 20
+
+    def set_and_hit_breakpoint(self, continueToExit=True):
+        source = "main.c"
+        main_source_path = os.path.join(os.getcwd(), source)
+        breakpoint1_line = line_number(main_source_path, "// breakpoint 1")
+        lines = [breakpoint1_line]
+        # Set breakpoint in the thread function so we can step the threads
+        breakpoint_ids = self.set_source_breakpoints(main_source_path, lines)
+        self.assertEqual(
+            len(breakpoint_ids), len(lines), "expect correct number of breakpoints"
+        )
+        self.continue_to_breakpoints(breakpoint_ids)
+        if continueToExit:
+            self.continue_to_exit()
+
+    def get_debug_server_command_line_args(self):
+        args = []
+        if lldbplatformutil.getPlatform() == "linux":
+            args = ["gdbserver"]
+        elif lldbplatformutil.getPlatform() == "macosx":
+            args = ["--listen"]
+        if lldb.remote_platform:
+            args += ["*:0"]
+        else:
+            args += ["localhost:0"]
+        return args
+
+    def get_debug_server_pipe(self):
+        pipe = Pipe(self.getBuildDir())
+        self.addTearDownHook(lambda: pipe.close())
+        pipe.finish_connection(self.default_timeout)
+        return pipe
+
+    @skipIfWindows
+    @skipIfNetBSD
+    def test_by_port(self):
+        """
+        Tests attaching to a process by port.
+        """
+        self.build_and_create_debug_adaptor()
+        program = self.getBuildArtifact("a.out")
+
+        debug_server_tool = self.getBuiltinDebugServerTool()
+
+        pipe = self.get_debug_server_pipe()
+        args = self.get_debug_server_command_line_args()
+        args += [program]
+        args += ["--named-pipe", pipe.name]
+
+        self.process = self.spawnSubprocess(
+            debug_server_tool, args, install_remote=False
+        )
+
+        # Read the port number from the debug server pipe.
+        port = pipe.read(10, self.default_timeout)
+        # Trim null byte, convert to int
+        port = int(port[:-1])
+        self.assertIsNotNone(
+            port, " Failed to read the port number from debug server pipe"
+        )
+
+        self.attach(program=program, gdbRemotePort=port, sourceInitFile=True)
+        self.set_and_hit_breakpoint(continueToExit=True)
+        self.process.terminate()
+
+    @skipIfWindows
+    @skipIfNetBSD
+    def test_by_port_and_pid(self):
+        """
+        Tests attaching to a process by process ID and port number.
+        """
+        self.build_and_create_debug_adaptor()
+        program = self.getBuildArtifact("a.out")
+
+        debug_server_tool = self.getBuiltinDebugServerTool()
+        pipe = self.get_debug_server_pipe()
+        args = self.get_debug_server_command_line_args()
+        args += [program]
+        args += ["--named-pipe", pipe.name]
+
+        self.process = self.spawnSubprocess(
+            debug_server_tool, args, install_remote=False
+        )
+
+        # Read the port number from the debug server pipe.
+        port = pipe.read(10, self.default_timeout)
+        # Trim null byte, convert to int
+        port = int(port[:-1])
----------------
santhoshe447 wrote:

Yes, you are right but I winna test with complete cycle.
I will change this if its required.

https://github.com/llvm/llvm-project/pull/91570


More information about the lldb-commits mailing list