Skip to content

[lldb-dap] Give attach test binaries unique names #138435

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import time
import subprocess
import uuid

import dap_server
from lldbsuite.test.lldbtest import *
Expand Down Expand Up @@ -28,10 +28,17 @@ def create_debug_adapter(self, lldbDAPEnv=None, connection=None):
env=lldbDAPEnv,
)

def build_and_create_debug_adapter(self, lldbDAPEnv=None):
self.build()
def build_and_create_debug_adapter(self, lldbDAPEnv=None, dictionary=None):
self.build(dictionary=dictionary)
self.create_debug_adapter(lldbDAPEnv)

def build_and_create_debug_adapter_for_attach(self):
"""Variant of build_and_create_debug_adapter that builds a uniquely
named binary."""
unique_name = str(uuid.uuid4())
self.build_and_create_debug_adapter(dictionary={"EXE": unique_name})
return self.getBuildArtifact(unique_name)

def set_source_breakpoints(self, source_path, lines, data=None):
"""Sets source breakpoints and returns an array of strings containing
the breakpoint IDs ("1", "2") for each breakpoint that was set.
Expand Down
40 changes: 9 additions & 31 deletions lldb/test/API/tools/lldb-dap/attach/TestDAP_attach.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Test lldb-dap attach request
"""


import dap_server
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
Expand All @@ -25,7 +24,7 @@ def spawn_and_wait(program, delay):
process.wait()


@skipIf
@skip
class TestDAP_attach(lldbdap_testcase.DAPTestCaseBase):
def set_and_hit_breakpoint(self, continueToExit=True):
source = "main.c"
Expand All @@ -45,8 +44,7 @@ def test_by_pid(self):
"""
Tests attaching to a process by process ID.
"""
self.build_and_create_debug_adapter()
program = self.getBuildArtifact("a.out")
program = self.build_and_create_debug_adapter_for_attach()
self.process = subprocess.Popen(
[program],
stdin=subprocess.PIPE,
Expand All @@ -61,34 +59,15 @@ def test_by_name(self):
"""
Tests attaching to a process by process name.
"""
self.build_and_create_debug_adapter()
orig_program = self.getBuildArtifact("a.out")
# Since we are going to attach by process name, we need a unique
# process name that has minimal chance to match a process that is
# already running. To do this we use tempfile.mktemp() to give us a
# full path to a location where we can copy our executable. We then
# run this copy to ensure we don't get the error "more that one
# process matches 'a.out'".
program = tempfile.mktemp()
shutil.copyfile(orig_program, program)
shutil.copymode(orig_program, program)
program = self.build_and_create_debug_adapter_for_attach()

# Use a file as a synchronization point between test and inferior.
pid_file_path = lldbutil.append_to_process_working_directory(
self, "pid_file_%d" % (int(time.time()))
)

def cleanup():
if os.path.exists(program):
os.unlink(program)
self.run_platform_command("rm %s" % (pid_file_path))

# Execute the cleanup function during test case tear down.
self.addTearDownHook(cleanup)

popen = self.spawnSubprocess(program, [pid_file_path])

pid = lldbutil.wait_for_file_on_target(self, pid_file_path)
lldbutil.wait_for_file_on_target(self, pid_file_path)

self.attach(program=program)
self.set_and_hit_breakpoint(continueToExit=True)
Expand All @@ -101,8 +80,7 @@ def test_by_name_waitFor(self):
next instance of a process to be launched, ingoring all current
ones.
"""
self.build_and_create_debug_adapter()
program = self.getBuildArtifact("a.out")
program = self.build_and_create_debug_adapter_for_attach()
self.spawn_thread = threading.Thread(
target=spawn_and_wait,
args=(
Expand Down Expand Up @@ -136,8 +114,8 @@ def test_commands(self):
"terminateCommands" are a list of LLDB commands that get executed when
the debugger session terminates.
"""
self.build_and_create_debug_adapter()
program = self.getBuildArtifact("a.out")
program = self.build_and_create_debug_adapter_for_attach()

# Here we just create a target and launch the process as a way to test
# if we are able to use attach commands to create any kind of a target
# and use it for debugging
Expand Down Expand Up @@ -209,8 +187,8 @@ def test_terminate_commands(self):
Tests that the "terminateCommands", that can be passed during
attach, are run when the debugger is disconnected.
"""
self.build_and_create_debug_adapter()
program = self.getBuildArtifact("a.out")
program = self.build_and_create_debug_adapter_for_attach()

# Here we just create a target and launch the process as a way to test
# if we are able to use attach commands to create any kind of a target
# and use it for debugging
Expand Down
13 changes: 4 additions & 9 deletions lldb/test/API/tools/lldb-dap/attach/TestDAP_attachByPortNum.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Test lldb-dap "port" configuration to "attach" request
"""


import dap_server
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
Expand Down Expand Up @@ -59,8 +58,7 @@ def test_by_port(self):
"""
Tests attaching to a process by port.
"""
self.build_and_create_debug_adapter()
program = self.getBuildArtifact("a.out")
program = self.build_and_create_debug_adapter_for_attach()

debug_server_tool = self.getBuiltinDebugServerTool()

Expand Down Expand Up @@ -91,8 +89,7 @@ def test_by_port_and_pid(self):
"""
Tests attaching to a process by process ID and port number.
"""
self.build_and_create_debug_adapter()
program = self.getBuildArtifact("a.out")
program = self.build_and_create_debug_adapter_for_attach()

# It is not necessary to launch "lldb-server" to obtain the actual port and pid for attaching.
# However, when providing the port number and pid directly, "lldb-dap" throws an error message, which is expected.
Expand All @@ -119,8 +116,7 @@ def test_by_invalid_port(self):
"""
Tests attaching to a process by invalid port number 0.
"""
self.build_and_create_debug_adapter()
program = self.getBuildArtifact("a.out")
program = self.build_and_create_debug_adapter_for_attach()

port = 0
response = self.attach(
Expand All @@ -138,8 +134,7 @@ def test_by_illegal_port(self):
"""
Tests attaching to a process by illegal/greater port number 65536
"""
self.build_and_create_debug_adapter()
program = self.getBuildArtifact("a.out")
program = self.build_and_create_debug_adapter_for_attach()

port = 65536
args = [program]
Expand Down
4 changes: 2 additions & 2 deletions lldb/test/API/tools/lldb-dap/commands/TestDAP_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from lldbsuite.test import lldbtest, lldbutil
from lldbsuite.test.decorators import *


# DAP tests are flakey, see https://github.com/llvm/llvm-project/issues/137660.
@skip
class TestDAP_commands(lldbdap_testcase.DAPTestCaseBase):
Expand Down Expand Up @@ -71,12 +72,11 @@ def test_command_directive_abort_on_error_post_run_commands(self):
self.do_test_abort_on_error(use_post_run_commands=True)

def test_command_directive_abort_on_error_attach_commands(self):
program = self.getBuildArtifact("a.out")
command_quiet = (
"settings set target.show-hex-variable-values-with-leading-zeroes false"
)
command_abort_on_error = "settings set foo bar"
self.build_and_create_debug_adapter()
program = self.build_and_create_debug_adapter_for_attach()
self.attach(
program,
attachCommands=["?!" + command_quiet, "!" + command_abort_on_error],
Expand Down
Loading