xref: /llvm-project/lldb/test/API/functionalities/scripted_process/TestScriptedProcess.py (revision dd391e1ef762d79f86112dc2480a89c9be066ce1)
1"""
2Test python scripted process in lldb
3"""
4
5import os
6
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11from lldbsuite.test import lldbtest
12
13
14class ScriptedProcesTestCase(TestBase):
15
16    mydir = TestBase.compute_mydir(__file__)
17
18    def setUp(self):
19        TestBase.setUp(self)
20        self.source = "main.c"
21
22    def tearDown(self):
23        TestBase.tearDown(self)
24
25    def test_python_plugin_package(self):
26        """Test that the lldb python module has a `plugins.scripted_process`
27        package."""
28        self.expect('script import lldb.plugins',
29                    substrs=["ModuleNotFoundError"], matching=False)
30
31        self.expect('script dir(lldb.plugins)',
32                    substrs=["scripted_process"])
33
34        self.expect('script import lldb.plugins.scripted_process',
35                    substrs=["ModuleNotFoundError"], matching=False)
36
37        self.expect('script dir(lldb.plugins.scripted_process)',
38                    substrs=["ScriptedProcess"])
39
40        self.expect('script from lldb.plugins.scripted_process import ScriptedProcess',
41                    substrs=["ImportError"], matching=False)
42
43        self.expect('script dir(ScriptedProcess)',
44                    substrs=["launch"])
45
46    def test_launch_scripted_process_sbapi(self):
47        """Test that we can launch an lldb scripted process using the SBAPI,
48        check its process ID and read string from memory."""
49        self.build()
50        target = self.dbg.CreateTarget(self.getBuildArtifact("a.out"))
51        self.assertTrue(target, VALID_TARGET)
52
53        scripted_process_example_relpath = ['..','..','..','..','examples','python','scripted_process','my_scripted_process.py']
54        os.environ['SKIP_SCRIPTED_PROCESS_LAUNCH'] = '1'
55        self.runCmd("command script import " + os.path.join(self.getSourceDir(),
56                                                            *scripted_process_example_relpath))
57
58        launch_info = lldb.SBLaunchInfo(None)
59        launch_info.SetProcessPluginName("ScriptedProcess")
60        launch_info.SetScriptedProcessClassName("my_scripted_process.MyScriptedProcess")
61
62        error = lldb.SBError()
63        process = target.Launch(launch_info, error)
64        self.assertTrue(process and process.IsValid(), PROCESS_IS_VALID)
65        self.assertEqual(process.GetProcessID(), 42)
66
67        hello_world = "Hello, world!"
68        memory_read = process.ReadCStringFromMemory(0x50000000000,
69                                                    len(hello_world) + 1, # NULL byte
70                                                    error)
71
72        self.assertTrue(error.Success(), "Failed to read memory from scripted process.")
73        self.assertEqual(hello_world, memory_read)
74
75    def test_launch_scripted_process_cli(self):
76        """Test that we can launch an lldb scripted process from the command
77        line, check its process ID and read string from memory."""
78        self.build()
79        target = self.dbg.CreateTarget(self.getBuildArtifact("a.out"))
80        self.assertTrue(target, VALID_TARGET)
81
82        scripted_process_example_relpath = ['..','..','..','..','examples','python','scripted_process','my_scripted_process.py']
83        self.runCmd("command script import " + os.path.join(self.getSourceDir(),
84                                                            *scripted_process_example_relpath))
85
86        process = target.GetProcess()
87        self.assertTrue(process, PROCESS_IS_VALID)
88        self.assertEqual(process.GetProcessID(), 42)
89
90        error = lldb.SBError()
91        hello_world = "Hello, world!"
92        memory_read = process.ReadCStringFromMemory(0x50000000000,
93                                                    len(hello_world) + 1, # NULL byte
94                                                    error)
95
96        self.assertTrue(error.Success(), "Failed to read memory from scripted process.")
97        self.assertEqual(hello_world, memory_read)
98