Skip to content

Commit 3ceec26

Browse files
authored
[lldb-dap] Give attach test binaries unique names (#138435)
Give the test binaries used for attaching unique names to avoid accidentally attaching to the wrong binary. Fixes #138197
1 parent 230f332 commit 3ceec26

File tree

4 files changed

+25
-45
lines changed

4 files changed

+25
-45
lines changed

lldb/packages/Python/lldbsuite/test/tools/lldb-dap/lldbdap_testcase.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
import time
3-
import subprocess
3+
import uuid
44

55
import dap_server
66
from lldbsuite.test.lldbtest import *
@@ -28,10 +28,17 @@ def create_debug_adapter(self, lldbDAPEnv=None, connection=None):
2828
env=lldbDAPEnv,
2929
)
3030

31-
def build_and_create_debug_adapter(self, lldbDAPEnv=None):
32-
self.build()
31+
def build_and_create_debug_adapter(self, lldbDAPEnv=None, dictionary=None):
32+
self.build(dictionary=dictionary)
3333
self.create_debug_adapter(lldbDAPEnv)
3434

35+
def build_and_create_debug_adapter_for_attach(self):
36+
"""Variant of build_and_create_debug_adapter that builds a uniquely
37+
named binary."""
38+
unique_name = str(uuid.uuid4())
39+
self.build_and_create_debug_adapter(dictionary={"EXE": unique_name})
40+
return self.getBuildArtifact(unique_name)
41+
3542
def set_source_breakpoints(self, source_path, lines, data=None):
3643
"""Sets source breakpoints and returns an array of strings containing
3744
the breakpoint IDs ("1", "2") for each breakpoint that was set.

lldb/test/API/tools/lldb-dap/attach/TestDAP_attach.py

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
Test lldb-dap attach request
33
"""
44

5-
65
import dap_server
76
from lldbsuite.test.decorators import *
87
from lldbsuite.test.lldbtest import *
@@ -25,7 +24,7 @@ def spawn_and_wait(program, delay):
2524
process.wait()
2625

2726

28-
@skipIf
27+
@skip
2928
class TestDAP_attach(lldbdap_testcase.DAPTestCaseBase):
3029
def set_and_hit_breakpoint(self, continueToExit=True):
3130
source = "main.c"
@@ -45,8 +44,7 @@ def test_by_pid(self):
4544
"""
4645
Tests attaching to a process by process ID.
4746
"""
48-
self.build_and_create_debug_adapter()
49-
program = self.getBuildArtifact("a.out")
47+
program = self.build_and_create_debug_adapter_for_attach()
5048
self.process = subprocess.Popen(
5149
[program],
5250
stdin=subprocess.PIPE,
@@ -61,34 +59,15 @@ def test_by_name(self):
6159
"""
6260
Tests attaching to a process by process name.
6361
"""
64-
self.build_and_create_debug_adapter()
65-
orig_program = self.getBuildArtifact("a.out")
66-
# Since we are going to attach by process name, we need a unique
67-
# process name that has minimal chance to match a process that is
68-
# already running. To do this we use tempfile.mktemp() to give us a
69-
# full path to a location where we can copy our executable. We then
70-
# run this copy to ensure we don't get the error "more that one
71-
# process matches 'a.out'".
72-
program = tempfile.mktemp()
73-
shutil.copyfile(orig_program, program)
74-
shutil.copymode(orig_program, program)
62+
program = self.build_and_create_debug_adapter_for_attach()
7563

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

81-
def cleanup():
82-
if os.path.exists(program):
83-
os.unlink(program)
84-
self.run_platform_command("rm %s" % (pid_file_path))
85-
86-
# Execute the cleanup function during test case tear down.
87-
self.addTearDownHook(cleanup)
88-
8969
popen = self.spawnSubprocess(program, [pid_file_path])
90-
91-
pid = lldbutil.wait_for_file_on_target(self, pid_file_path)
70+
lldbutil.wait_for_file_on_target(self, pid_file_path)
9271

9372
self.attach(program=program)
9473
self.set_and_hit_breakpoint(continueToExit=True)
@@ -101,8 +80,7 @@ def test_by_name_waitFor(self):
10180
next instance of a process to be launched, ingoring all current
10281
ones.
10382
"""
104-
self.build_and_create_debug_adapter()
105-
program = self.getBuildArtifact("a.out")
83+
program = self.build_and_create_debug_adapter_for_attach()
10684
self.spawn_thread = threading.Thread(
10785
target=spawn_and_wait,
10886
args=(
@@ -136,8 +114,8 @@ def test_commands(self):
136114
"terminateCommands" are a list of LLDB commands that get executed when
137115
the debugger session terminates.
138116
"""
139-
self.build_and_create_debug_adapter()
140-
program = self.getBuildArtifact("a.out")
117+
program = self.build_and_create_debug_adapter_for_attach()
118+
141119
# Here we just create a target and launch the process as a way to test
142120
# if we are able to use attach commands to create any kind of a target
143121
# and use it for debugging
@@ -209,8 +187,8 @@ def test_terminate_commands(self):
209187
Tests that the "terminateCommands", that can be passed during
210188
attach, are run when the debugger is disconnected.
211189
"""
212-
self.build_and_create_debug_adapter()
213-
program = self.getBuildArtifact("a.out")
190+
program = self.build_and_create_debug_adapter_for_attach()
191+
214192
# Here we just create a target and launch the process as a way to test
215193
# if we are able to use attach commands to create any kind of a target
216194
# and use it for debugging

lldb/test/API/tools/lldb-dap/attach/TestDAP_attachByPortNum.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
Test lldb-dap "port" configuration to "attach" request
33
"""
44

5-
65
import dap_server
76
from lldbsuite.test.decorators import *
87
from lldbsuite.test.lldbtest import *
@@ -59,8 +58,7 @@ def test_by_port(self):
5958
"""
6059
Tests attaching to a process by port.
6160
"""
62-
self.build_and_create_debug_adapter()
63-
program = self.getBuildArtifact("a.out")
61+
program = self.build_and_create_debug_adapter_for_attach()
6462

6563
debug_server_tool = self.getBuiltinDebugServerTool()
6664

@@ -91,8 +89,7 @@ def test_by_port_and_pid(self):
9189
"""
9290
Tests attaching to a process by process ID and port number.
9391
"""
94-
self.build_and_create_debug_adapter()
95-
program = self.getBuildArtifact("a.out")
92+
program = self.build_and_create_debug_adapter_for_attach()
9693

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

125121
port = 0
126122
response = self.attach(
@@ -138,8 +134,7 @@ def test_by_illegal_port(self):
138134
"""
139135
Tests attaching to a process by illegal/greater port number 65536
140136
"""
141-
self.build_and_create_debug_adapter()
142-
program = self.getBuildArtifact("a.out")
137+
program = self.build_and_create_debug_adapter_for_attach()
143138

144139
port = 65536
145140
args = [program]

lldb/test/API/tools/lldb-dap/commands/TestDAP_commands.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from lldbsuite.test import lldbtest, lldbutil
66
from lldbsuite.test.decorators import *
77

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

7374
def test_command_directive_abort_on_error_attach_commands(self):
74-
program = self.getBuildArtifact("a.out")
7575
command_quiet = (
7676
"settings set target.show-hex-variable-values-with-leading-zeroes false"
7777
)
7878
command_abort_on_error = "settings set foo bar"
79-
self.build_and_create_debug_adapter()
79+
program = self.build_and_create_debug_adapter_for_attach()
8080
self.attach(
8181
program,
8282
attachCommands=["?!" + command_quiet, "!" + command_abort_on_error],

0 commit comments

Comments
 (0)