159 lines · python
1"""2Test python scripted process in lldb3"""4 5import os, json, tempfile6 7import lldb8from lldbsuite.test.decorators import *9from lldbsuite.test.lldbtest import *10from lldbsuite.test import lldbutil11from lldbsuite.test import lldbtest12 13 14class StackCoreScriptedProcesTestCase(TestBase):15 NO_DEBUG_INFO_TESTCASE = True16 17 def create_stack_skinny_corefile(self, file):18 self.build()19 target, process, thread, _ = lldbutil.run_to_source_breakpoint(20 self, "// break here", lldb.SBFileSpec("baz.cpp")21 )22 self.assertTrue(process.IsValid(), "Process is invalid.")23 # FIXME: Use SBAPI to save the process corefile.24 self.runCmd("process save-core -s stack " + file)25 self.assertTrue(os.path.exists(file), "No stack-only corefile found.")26 self.assertTrue(self.dbg.DeleteTarget(target), "Couldn't delete target")27 28 def get_module_with_name(self, target, name):29 for module in target.modules:30 if name in module.GetFileSpec().GetFilename():31 return module32 return None33 34 @skipUnlessDarwin35 @skipIfRemote36 @skipIfAsan # On ASAN builds, this test times-out (rdar://98678134)37 @skipIfDarwin38 def test_launch_scripted_process_stack_frames(self):39 """Test that we can launch an lldb scripted process from the command40 line, check its process ID and read string from memory."""41 self.build()42 target = self.dbg.CreateTarget(self.getBuildArtifact("a.out"))43 self.assertTrue(target, VALID_TARGET)44 45 main_module = self.get_module_with_name(target, "a.out")46 self.assertTrue(main_module, "Invalid main module.")47 error = target.SetModuleLoadAddress(main_module, 0)48 self.assertSuccess(error, "Reloading main module at offset 0 failed.")49 50 scripted_dylib = self.get_module_with_name(target, "libbaz.dylib")51 self.assertTrue(scripted_dylib, "Dynamic library libbaz.dylib not found.")52 self.assertEqual(53 scripted_dylib.GetObjectFileHeaderAddress().GetLoadAddress(target),54 0xFFFFFFFFFFFFFFFF,55 )56 57 os.environ["SKIP_SCRIPTED_PROCESS_LAUNCH"] = "1"58 59 def cleanup():60 del os.environ["SKIP_SCRIPTED_PROCESS_LAUNCH"]61 62 self.addTearDownHook(cleanup)63 64 scripted_process_example_relpath = "stack_core_scripted_process.py"65 self.runCmd(66 "command script import "67 + os.path.join(self.getSourceDir(), scripted_process_example_relpath)68 )69 70 corefile_process = None71 with tempfile.NamedTemporaryFile() as file:72 self.create_stack_skinny_corefile(file.name)73 corefile_target = self.dbg.CreateTarget(None)74 corefile_process = corefile_target.LoadCore(75 self.getBuildArtifact(file.name)76 )77 self.assertTrue(corefile_process, PROCESS_IS_VALID)78 79 # Create a random lib which does not exist in the corefile.80 random_dylib = self.get_module_with_name(corefile_target, "random.dylib")81 self.assertFalse(82 random_dylib, "Dynamic library random.dylib should not be found."83 )84 85 structured_data = lldb.SBStructuredData()86 structured_data.SetFromJSON(87 json.dumps(88 {89 "backing_target_idx": self.dbg.GetIndexOfTarget(90 corefile_process.GetTarget()91 ),92 "custom_modules": [93 {94 "path": self.getBuildArtifact("libbaz.dylib"),95 },96 {97 "path": "/random/path/random.dylib",98 "load_addr": 12345678,99 },100 ],101 }102 )103 )104 launch_info = lldb.SBLaunchInfo(None)105 launch_info.SetProcessPluginName("ScriptedProcess")106 launch_info.SetScriptedProcessClassName(107 "stack_core_scripted_process.StackCoreScriptedProcess"108 )109 launch_info.SetScriptedProcessDictionary(structured_data)110 111 error = lldb.SBError()112 process = target.Launch(launch_info, error)113 self.assertSuccess(error)114 self.assertTrue(process, PROCESS_IS_VALID)115 self.assertEqual(process.GetProcessID(), 42)116 117 self.assertEqual(process.GetNumThreads(), 2)118 thread = process.GetSelectedThread()119 self.assertTrue(thread, "Invalid thread.")120 self.assertEqual(thread.GetName(), "StackCoreScriptedThread.thread-1")121 122 self.assertTrue(target.triple, "Invalid target triple")123 arch = target.triple.split("-")[0]124 supported_arch = ["x86_64", "arm64", "arm64e"]125 self.assertIn(arch, supported_arch)126 # When creating a corefile of a arm process, lldb saves the exception127 # that triggers the breakpoint in the LC_NOTES of the corefile, so they128 # can be reloaded with the corefile on the next debug session.129 if arch in "arm64e":130 self.assertTrue(thread.GetStopReason(), lldb.eStopReasonException)131 # However, it's architecture specific, and corefiles made from intel132 # process don't save any metadata to retrieve to stop reason.133 # To mitigate this, the StackCoreScriptedProcess will report a134 # eStopReasonSignal with a SIGTRAP, mimicking what debugserver does.135 else:136 self.assertTrue(thread.GetStopReason(), lldb.eStopReasonSignal)137 138 self.assertEqual(thread.GetNumFrames(), 5)139 frame = thread.GetSelectedFrame()140 self.assertTrue(frame, "Invalid frame.")141 func = frame.GetFunction()142 self.assertTrue(func, "Invalid function.")143 144 self.assertIn("baz", frame.GetFunctionName())145 self.assertGreater(frame.vars.GetSize(), 0)146 self.assertEqual(int(frame.vars.GetFirstValueByName("k").GetValue()), 42)147 self.assertEqual(148 int(frame.vars.GetFirstValueByName("j").Dereference().GetValue()), 42 * 42149 )150 151 corefile_dylib = self.get_module_with_name(corefile_target, "libbaz.dylib")152 self.assertTrue(corefile_dylib, "Dynamic library libbaz.dylib not found.")153 scripted_dylib = self.get_module_with_name(target, "libbaz.dylib")154 self.assertTrue(scripted_dylib, "Dynamic library libbaz.dylib not found.")155 self.assertEqual(156 scripted_dylib.GetObjectFileHeaderAddress().GetLoadAddress(target),157 corefile_dylib.GetObjectFileHeaderAddress().GetLoadAddress(target),158 )159