1""" 2Test python scripted process in lldb 3""" 4 5import os, json, tempfile 6 7import lldb 8from lldbsuite.test.decorators import * 9from lldbsuite.test.lldbtest import * 10from lldbsuite.test import lldbutil 11from lldbsuite.test import lldbtest 12 13class StackCoreScriptedProcesTestCase(TestBase): 14 15 NO_DEBUG_INFO_TESTCASE = True 16 17 mydir = TestBase.compute_mydir(__file__) 18 19 def setUp(self): 20 TestBase.setUp(self) 21 22 def tearDown(self): 23 TestBase.tearDown(self) 24 25 def create_stack_skinny_corefile(self, file): 26 self.build() 27 target, process, thread, _ = lldbutil.run_to_source_breakpoint(self, "// break here", 28 lldb.SBFileSpec("main.cpp")) 29 self.assertTrue(process.IsValid(), "Process is invalid.") 30 # FIXME: Use SBAPI to save the process corefile. 31 self.runCmd("process save-core -s stack " + file) 32 self.assertTrue(os.path.exists(file), "No stack-only corefile found.") 33 self.assertTrue(self.dbg.DeleteTarget(target), "Couldn't delete target") 34 35 @skipUnlessDarwin 36 @skipIfOutOfTreeDebugserver 37 def test_launch_scripted_process_stack_frames(self): 38 """Test that we can launch an lldb scripted process from the command 39 line, check its process ID and read string from memory.""" 40 self.build() 41 target = self.dbg.CreateTarget(self.getBuildArtifact("a.out")) 42 self.assertTrue(target, VALID_TARGET) 43 44 for module in target.modules: 45 if 'a.out' in module.GetFileSpec().GetFilename(): 46 main_module = module 47 break 48 49 self.assertTrue(main_module, "Invalid main module.") 50 error = target.SetModuleLoadAddress(main_module, 0) 51 self.assertTrue(error.Success(), "Reloading main module at offset 0 failed.") 52 53 os.environ['SKIP_SCRIPTED_PROCESS_LAUNCH'] = '1' 54 def cleanup(): 55 del os.environ["SKIP_SCRIPTED_PROCESS_LAUNCH"] 56 self.addTearDownHook(cleanup) 57 58 scripted_process_example_relpath = 'stack_core_scripted_process.py' 59 self.runCmd("command script import " + os.path.join(self.getSourceDir(), 60 scripted_process_example_relpath)) 61 62 corefile_process = None 63 with tempfile.NamedTemporaryFile() as file: 64 self.create_stack_skinny_corefile(file.name) 65 corefile_target = self.dbg.CreateTarget(None) 66 corefile_process = corefile_target.LoadCore(self.getBuildArtifact(file.name)) 67 self.assertTrue(corefile_process, PROCESS_IS_VALID) 68 69 structured_data = lldb.SBStructuredData() 70 structured_data.SetFromJSON(json.dumps({ 71 "backing_target_idx" : self.dbg.GetIndexOfTarget(corefile_process.GetTarget()) 72 })) 73 launch_info = lldb.SBLaunchInfo(None) 74 launch_info.SetProcessPluginName("ScriptedProcess") 75 launch_info.SetScriptedProcessClassName("stack_core_scripted_process.StackCoreScriptedProcess") 76 launch_info.SetScriptedProcessDictionary(structured_data) 77 78 error = lldb.SBError() 79 process = target.Launch(launch_info, error) 80 self.assertTrue(error.Success(), error.GetCString()) 81 self.assertTrue(process, PROCESS_IS_VALID) 82 self.assertEqual(process.GetProcessID(), 42) 83 84 self.assertEqual(process.GetNumThreads(), 3) 85 thread = process.GetSelectedThread() 86 self.assertTrue(thread, "Invalid thread.") 87 self.assertEqual(thread.GetName(), "StackCoreScriptedThread.thread-2") 88 89 self.assertTrue(target.triple, "Invalid target triple") 90 arch = target.triple.split('-')[0] 91 supported_arch = ['x86_64', 'arm64', 'arm64e'] 92 self.assertIn(arch, supported_arch) 93 # When creating a corefile of a arm process, lldb saves the exception 94 # that triggers the breakpoint in the LC_NOTES of the corefile, so they 95 # can be reloaded with the corefile on the next debug session. 96 if arch in 'arm64e': 97 self.assertTrue(thread.GetStopReason(), lldb.eStopReasonException) 98 # However, it's architecture specific, and corefiles made from intel 99 # process don't save any metadata to retrieve to stop reason. 100 # To mitigate this, the StackCoreScriptedProcess will report a 101 # eStopReasonSignal with a SIGTRAP, mimicking what debugserver does. 102 else: 103 self.assertTrue(thread.GetStopReason(), lldb.eStopReasonSignal) 104 105 self.assertEqual(thread.GetNumFrames(), 6) 106 frame = thread.GetSelectedFrame() 107 self.assertTrue(frame, "Invalid frame.") 108 self.assertIn("bar", frame.GetFunctionName()) 109 self.assertEqual(int(frame.FindValue("i", lldb.eValueTypeVariableArgument).GetValue()), 42) 110 self.assertEqual(int(frame.FindValue("j", lldb.eValueTypeVariableLocal).GetValue()), 42 * 42) 111