4 import gdbremote_testcase
5 import lldbgdbserverutils
6 from lldbsuite
.test
.decorators
import *
7 from lldbsuite
.test
.lldbtest
import *
8 from lldbsuite
.test
import lldbutil
11 class TestGdbRemoteAttachWait(gdbremote_testcase
.GdbRemoteTestCaseBase
):
12 def _set_up_inferior(self
):
13 self
._exe
_to
_attach
= "%s_%d" % (self
.testMethodName
, os
.getpid())
14 self
.build(dictionary
={"EXE": self
._exe
_to
_attach
, "CXX_SOURCES": "main.cpp"})
16 if self
.getPlatform() != "windows":
17 # Use a shim to ensure that the process is ready to be attached from
19 self
._exe
_to
_run
= "shim"
20 self
._exe
_to
_attach
= lldbutil
.install_to_target(
21 self
, self
.getBuildArtifact(self
._exe
_to
_attach
)
23 self
._run
_args
= [self
._exe
_to
_attach
]
24 self
.build(dictionary
={"EXE": self
._exe
_to
_run
, "CXX_SOURCES": "shim.cpp"})
26 self
._exe
_to
_run
= self
._exe
_to
_attach
29 def _launch_inferior(self
, args
):
30 inferior
= self
.spawnSubprocess(self
.getBuildArtifact(self
._exe
_to
_run
), args
)
31 self
.assertIsNotNone(inferior
)
32 self
.assertGreater(inferior
.pid
, 0)
33 self
.assertTrue(lldbgdbserverutils
.process_is_running(inferior
.pid
, True))
36 def _launch_and_wait_for_init(self
):
37 sync_file_path
= lldbutil
.append_to_process_working_directory(
40 inferior
= self
._launch
_inferior
(self
._run
_args
+ [sync_file_path
])
41 lldbutil
.wait_for_file_on_target(self
, sync_file_path
)
44 def _attach_packet(self
, packet_type
):
45 return "read packet: ${};{}#00".format(
47 lldbgdbserverutils
.gdbremote_hex_encode_string(self
._exe
_to
_attach
),
50 @skipIfWindows # This test is flaky on Windows
51 def test_attach_with_vAttachWait(self
):
52 self
._set
_up
_inferior
()
54 self
.set_inferior_startup_attach_manually()
55 server
= self
.connect_to_debug_monitor()
58 # Launch the first inferior (we shouldn't attach to this one).
59 self
._launch
_and
_wait
_for
_init
()
61 self
.test_sequence
.add_log_lines([self
._attach
_packet
("vAttachWait")], True)
62 # Run the stream until attachWait.
63 context
= self
.expect_gdbremote_sequence()
64 self
.assertIsNotNone(context
)
66 # Sleep so we're sure that the inferior is launched after we ask for the attach.
69 # Launch the second inferior (we SHOULD attach to this one).
70 inferior_to_attach
= self
._launch
_inferior
(self
._run
_args
)
72 # Make sure the attach succeeded.
73 self
.test_sequence
.add_log_lines(
77 "regex": r
"^\$T([0-9a-fA-F]{2})[^#]*#[0-9a-fA-F]{2}$",
78 "capture": {1: "stop_signal_hex"},
83 self
.add_process_info_collection_packets()
85 # Run the stream sending the response..
86 context
= self
.expect_gdbremote_sequence()
87 self
.assertIsNotNone(context
)
89 # Gather process info response.
90 process_info
= self
.parse_process_info_response(context
)
91 self
.assertIsNotNone(process_info
)
93 # Ensure the process id matches what we expected.
94 pid_text
= process_info
.get("pid", None)
95 self
.assertIsNotNone(pid_text
)
96 reported_pid
= int(pid_text
, base
=16)
97 self
.assertEqual(reported_pid
, inferior_to_attach
.pid
)
99 @skipIfWindows # This test is flaky on Windows
100 def test_launch_before_attach_with_vAttachOrWait(self
):
101 self
._set
_up
_inferior
()
103 self
.set_inferior_startup_attach_manually()
104 server
= self
.connect_to_debug_monitor()
107 inferior
= self
._launch
_and
_wait
_for
_init
()
109 # Add attach packets.
110 self
.test_sequence
.add_log_lines(
113 self
._attach
_packet
("vAttachOrWait"),
114 # Expect a stop notification from the attach.
117 "regex": r
"^\$T([0-9a-fA-F]{2})[^#]*#[0-9a-fA-F]{2}$",
118 "capture": {1: "stop_signal_hex"},
123 self
.add_process_info_collection_packets()
126 context
= self
.expect_gdbremote_sequence()
127 self
.assertIsNotNone(context
)
129 # Gather process info response
130 process_info
= self
.parse_process_info_response(context
)
131 self
.assertIsNotNone(process_info
)
133 # Ensure the process id matches what we expected.
134 pid_text
= process_info
.get("pid", None)
135 self
.assertIsNotNone(pid_text
)
136 reported_pid
= int(pid_text
, base
=16)
137 self
.assertEqual(reported_pid
, inferior
.pid
)
139 @skipIfWindows # This test is flaky on Windows
140 def test_launch_after_attach_with_vAttachOrWait(self
):
141 self
._set
_up
_inferior
()
143 self
.set_inferior_startup_attach_manually()
144 server
= self
.connect_to_debug_monitor()
147 self
.test_sequence
.add_log_lines([self
._attach
_packet
("vAttachOrWait")], True)
148 # Run the stream until attachWait.
149 context
= self
.expect_gdbremote_sequence()
150 self
.assertIsNotNone(context
)
152 # Sleep so we're sure that the inferior is launched after we ask for the attach.
155 # Launch the inferior.
156 inferior
= self
._launch
_inferior
(self
._run
_args
)
158 # Make sure the attach succeeded.
159 self
.test_sequence
.add_log_lines(
163 "regex": r
"^\$T([0-9a-fA-F]{2})[^#]*#[0-9a-fA-F]{2}$",
164 "capture": {1: "stop_signal_hex"},
169 self
.add_process_info_collection_packets()
171 # Run the stream sending the response..
172 context
= self
.expect_gdbremote_sequence()
173 self
.assertIsNotNone(context
)
175 # Gather process info response.
176 process_info
= self
.parse_process_info_response(context
)
177 self
.assertIsNotNone(process_info
)
179 # Ensure the process id matches what we expected.
180 pid_text
= process_info
.get("pid", None)
181 self
.assertIsNotNone(pid_text
)
182 reported_pid
= int(pid_text
, base
=16)
183 self
.assertEqual(reported_pid
, inferior
.pid
)