xref: /llvm-project/lldb/test/API/functionalities/gdb_remote_client/TestPlatformClient.py (revision 8fa2394bad433558f3083cee158043e2fb66d781)
1import lldb
2import binascii
3import os
4import time
5from lldbsuite.test.lldbtest import *
6from lldbsuite.test.decorators import *
7from gdbclientutils import *
8
9def hexlify(string):
10    return binascii.hexlify(string.encode()).decode()
11
12class TestPlatformClient(GDBRemoteTestBase):
13
14    def test_process_list_with_all_users(self):
15        """Test connecting to a remote linux platform"""
16
17        class MyResponder(MockGDBServerResponder):
18            def __init__(self):
19                MockGDBServerResponder.__init__(self)
20                self.currentQsProc = 0
21                self.all_users = False
22
23            def qfProcessInfo(self, packet):
24                if "all_users:1" in packet:
25                    self.all_users = True
26                    name = hexlify("/a/test_process")
27                    args = "-".join(map(hexlify,
28                                        ["/system/bin/sh", "-c", "/data/local/tmp/lldb-server"]))
29                    return "pid:10;ppid:1;uid:2;gid:3;euid:4;egid:5;name:" + name + ";args:" + args + ";"
30                else:
31                    self.all_users = False
32                    return "E04"
33
34            def qsProcessInfo(self):
35                if self.all_users:
36                    if self.currentQsProc == 0:
37                        self.currentQsProc = 1
38                        name = hexlify("/b/another_test_process")
39                        # This intentionally has a badly encoded argument
40                        args = "X".join(map(hexlify,
41                                            ["/system/bin/ls", "--help"]))
42                        return "pid:11;ppid:2;uid:3;gid:4;euid:5;egid:6;name:" + name + ";args:" + args + ";"
43                    elif self.currentQsProc == 1:
44                        self.currentQsProc = 0
45                        return "E04"
46                else:
47                    return "E04"
48
49        self.server.responder = MyResponder()
50
51        try:
52            self.runCmd("platform select remote-linux")
53            self.runCmd("platform connect " + self.server.get_connect_url())
54            self.assertTrue(self.dbg.GetSelectedPlatform().IsConnected())
55            self.expect("platform process list -x",
56                        substrs=["2 matching processes were found", "test_process", "another_test_process"])
57            self.expect("platform process list -xv",
58                        substrs=[
59                            "PID    PARENT USER       GROUP      EFF USER   EFF GROUP  TRIPLE                         ARGUMENTS",
60                            "10     1      2          3          4          5                                         /system/bin/sh -c /data/local/tmp/lldb-server",
61                            "11     2      3          4          5          6"])
62            self.expect("platform process list -xv", substrs=["/system/bin/ls"], matching=False)
63            self.expect("platform process list",
64                        error=True,
65                        substrs=["error: no processes were found on the \"remote-linux\" platform"])
66        finally:
67            self.dbg.GetSelectedPlatform().DisconnectRemote()
68
69    class TimeoutResponder(MockGDBServerResponder):
70        """A mock server, which takes a very long time to compute the working
71        directory."""
72        def __init__(self):
73            MockGDBServerResponder.__init__(self)
74
75        def qGetWorkingDir(self):
76            time.sleep(10)
77            return hexlify("/foo/bar")
78
79    def test_no_timeout(self):
80        """Test that we honor the timeout setting. With a large enough timeout,
81        we should get the CWD successfully."""
82
83        self.server.responder = TestPlatformClient.TimeoutResponder()
84        self.runCmd("settings set plugin.process.gdb-remote.packet-timeout 30")
85        plat = lldb.SBPlatform("remote-linux")
86        try:
87            self.assertSuccess(plat.ConnectRemote(lldb.SBPlatformConnectOptions(
88                self.server.get_connect_url())))
89            self.assertEqual(plat.GetWorkingDirectory(), "/foo/bar")
90        finally:
91            plat.DisconnectRemote()
92
93    def test_timeout(self):
94        """Test that we honor the timeout setting. With a small timeout, CWD
95        retrieval should fail."""
96
97        self.server.responder = TestPlatformClient.TimeoutResponder()
98        self.runCmd("settings set plugin.process.gdb-remote.packet-timeout 3")
99        plat = lldb.SBPlatform("remote-linux")
100        try:
101            self.assertSuccess(plat.ConnectRemote(lldb.SBPlatformConnectOptions(
102                self.server.get_connect_url())))
103            self.assertIsNone(plat.GetWorkingDirectory())
104        finally:
105            plat.DisconnectRemote()
106