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 self.assertEqual(process.GetNumThreads(), 1) 76 77 thread = process.GetSelectedThread() 78 self.assertTrue(thread, "Invalid thread.") 79 self.assertEqual(thread.GetThreadID(), 0x19) 80 self.assertEqual(thread.GetName(), "MyScriptedThread.thread-1") 81 self.assertEqual(thread.GetStopReason(), lldb.eStopReasonSignal) 82 83 self.assertGreater(thread.GetNumFrames(), 0) 84 85 frame = thread.GetFrameAtIndex(0) 86 register_set = frame.registers # Returns an SBValueList. 87 for regs in register_set: 88 if 'GPR' in regs.name: 89 registers = regs 90 break 91 92 self.assertTrue(registers, "Invalid General Purpose Registers Set") 93 self.assertEqual(registers.GetNumChildren(), 21) 94 for idx, reg in enumerate(registers, start=1): 95 self.assertEqual(idx, int(reg.value, 16)) 96 97 def test_launch_scripted_process_cli(self): 98 """Test that we can launch an lldb scripted process from the command 99 line, check its process ID and read string from memory.""" 100 self.build() 101 target = self.dbg.CreateTarget(self.getBuildArtifact("a.out")) 102 self.assertTrue(target, VALID_TARGET) 103 104 scripted_process_example_relpath = ['..','..','..','..','examples','python','scripted_process','my_scripted_process.py'] 105 self.runCmd("command script import " + os.path.join(self.getSourceDir(), 106 *scripted_process_example_relpath)) 107 108 process = target.GetProcess() 109 self.assertTrue(process, PROCESS_IS_VALID) 110 self.assertEqual(process.GetProcessID(), 42) 111 112 error = lldb.SBError() 113 hello_world = "Hello, world!" 114 memory_read = process.ReadCStringFromMemory(0x50000000000, 115 len(hello_world) + 1, # NULL byte 116 error) 117 118 self.assertTrue(error.Success(), "Failed to read memory from scripted process.") 119 self.assertEqual(hello_world, memory_read) 120