147 lines · python
1"""2Test lldb-dap "port" configuration to "attach" request3"""4 5from lldbsuite.test.decorators import *6from lldbsuite.test.lldbtest import *7from lldbsuite.test import lldbplatformutil8from lldbgdbserverutils import Pipe9import lldbdap_testcase10import lldb11 12 13@skip(bugnumber="https://github.com/llvm/llvm-project/issues/138803")14class TestDAP_attachByPortNum(lldbdap_testcase.DAPTestCaseBase):15 def set_and_hit_breakpoint(self, continueToExit=True):16 self.dap_server.wait_for_stopped()17 18 source = "main.c"19 breakpoint1_line = line_number(source, "// breakpoint 1")20 lines = [breakpoint1_line]21 # Set breakpoint in the thread function so we can step the threads22 breakpoint_ids = self.set_source_breakpoints(source, lines)23 self.assertEqual(24 len(breakpoint_ids), len(lines), "expect correct number of breakpoints"25 )26 self.continue_to_breakpoints(breakpoint_ids)27 if continueToExit:28 self.continue_to_exit()29 30 def get_debug_server_command_line_args(self):31 args = []32 if lldbplatformutil.getPlatform() == "linux":33 args = ["gdbserver"]34 if lldb.remote_platform:35 args += ["*:0"]36 else:37 args += ["localhost:0"]38 return args39 40 def get_debug_server_pipe(self):41 pipe = Pipe(self.getBuildDir())42 self.addTearDownHook(lambda: pipe.close())43 pipe.finish_connection(self.DEFAULT_TIMEOUT)44 return pipe45 46 @skipIfWindows47 @skipIfNetBSD48 def test_by_port(self):49 """50 Tests attaching to a process by port.51 """52 program = self.build_and_create_debug_adapter_for_attach()53 54 debug_server_tool = self.getBuiltinDebugServerTool()55 56 pipe = self.get_debug_server_pipe()57 args = self.get_debug_server_command_line_args()58 args += [program]59 args += ["--named-pipe", pipe.name]60 61 self.process = self.spawnSubprocess(62 debug_server_tool, args, install_remote=False63 )64 65 # Read the port number from the debug server pipe.66 port = pipe.read(10, self.DEFAULT_TIMEOUT)67 # Trim null byte, convert to int68 port = int(port[:-1])69 self.assertIsNotNone(70 port, " Failed to read the port number from debug server pipe"71 )72 73 self.attach(74 program=program,75 gdbRemotePort=port,76 sourceInitFile=True,77 stopOnEntry=True,78 )79 self.set_and_hit_breakpoint(continueToExit=True)80 81 @skipIfWindows82 @skipIfNetBSD83 def test_fails_if_both_port_and_pid_are_set(self):84 """85 Tests attaching to a process by process ID and port number.86 """87 program = self.build_and_create_debug_adapter_for_attach()88 89 # It is not necessary to launch "lldb-server" to obtain the actual port90 # and pid for attaching. However, when providing the port number and pid91 # directly, "lldb-dap" throws an error message, which is expected. So,92 # used random pid and port numbers here.93 94 pid = 135495 port = 123496 97 response = self.attach(98 program=program,99 pid=pid,100 gdbRemotePort=port,101 sourceInitFile=True,102 expectFailure=True,103 )104 self.assertFalse(105 response["success"], "The user can't specify both pid and port"106 )107 108 @skipIfWindows109 @skipIfNetBSD110 def test_by_invalid_port(self):111 """112 Tests attaching to a process by invalid port number 0.113 """114 program = self.build_and_create_debug_adapter_for_attach()115 116 port = 0117 response = self.attach(118 program=program, gdbRemotePort=port, sourceInitFile=True, expectFailure=True119 )120 self.assertFalse(121 response["success"],122 "The user can't attach with invalid port (%s)" % port,123 )124 125 @skipIfWindows126 @skipIfNetBSD127 def test_by_illegal_port(self):128 """129 Tests attaching to a process by illegal/greater port number 65536130 """131 program = self.build_and_create_debug_adapter_for_attach()132 133 port = 65536134 args = [program]135 debug_server_tool = self.getBuiltinDebugServerTool()136 self.process = self.spawnSubprocess(137 debug_server_tool, args, install_remote=False138 )139 140 response = self.attach(141 program=program, gdbRemotePort=port, sourceInitFile=True, expectFailure=True142 )143 self.assertFalse(144 response["success"],145 "The user can't attach with illegal port (%s)" % port,146 )147