199451b44SJordan Rupprecht""" 299451b44SJordan RupprechtTest SBTarget APIs. 399451b44SJordan Rupprecht""" 499451b44SJordan Rupprecht 599451b44SJordan Rupprechtimport os 699451b44SJordan Rupprechtimport lldb 799451b44SJordan Rupprechtfrom lldbsuite.test.decorators import * 899451b44SJordan Rupprechtfrom lldbsuite.test.lldbtest import * 999451b44SJordan Rupprechtfrom lldbsuite.test import lldbutil 1099451b44SJordan Rupprecht 1199451b44SJordan Rupprecht 1299451b44SJordan Rupprechtclass TargetAPITestCase(TestBase): 1399451b44SJordan Rupprecht def setUp(self): 1499451b44SJordan Rupprecht # Call super's setUp(). 1599451b44SJordan Rupprecht TestBase.setUp(self) 1699451b44SJordan Rupprecht # Find the line number to of function 'c'. 1799451b44SJordan Rupprecht self.line1 = line_number( 182238dcc3SJonas Devlieghere "main.c", "// Find the line number for breakpoint 1 here." 192238dcc3SJonas Devlieghere ) 2099451b44SJordan Rupprecht self.line2 = line_number( 212238dcc3SJonas Devlieghere "main.c", "// Find the line number for breakpoint 2 here." 222238dcc3SJonas Devlieghere ) 232238dcc3SJonas Devlieghere self.line_main = line_number("main.c", "// Set a break at entry to main.") 2499451b44SJordan Rupprecht 2599451b44SJordan Rupprecht # rdar://problem/9700873 2699451b44SJordan Rupprecht # Find global variable value fails for dwarf if inferior not started 2799451b44SJordan Rupprecht # (Was CrashTracer: [USER] 1 crash in Python at _lldb.so: lldb_private::MemoryCache::Read + 94) 2899451b44SJordan Rupprecht # 2999451b44SJordan Rupprecht # It does not segfaults now. But for dwarf, the variable value is None if 3099451b44SJordan Rupprecht # the inferior process does not exist yet. The radar has been updated. 3199451b44SJordan Rupprecht def test_find_global_variables(self): 3299451b44SJordan Rupprecht """Exercise SBTarget.FindGlobalVariables() API.""" 332238dcc3SJonas Devlieghere d = {"EXE": "b.out"} 3499451b44SJordan Rupprecht self.build(dictionary=d) 3599451b44SJordan Rupprecht self.setTearDownCleanup(dictionary=d) 362238dcc3SJonas Devlieghere self.find_global_variables("b.out") 3799451b44SJordan Rupprecht 3899451b44SJordan Rupprecht def test_find_compile_units(self): 3999451b44SJordan Rupprecht """Exercise SBTarget.FindCompileUnits() API.""" 402238dcc3SJonas Devlieghere d = {"EXE": "b.out"} 4199451b44SJordan Rupprecht self.build(dictionary=d) 4299451b44SJordan Rupprecht self.setTearDownCleanup(dictionary=d) 432238dcc3SJonas Devlieghere self.find_compile_units(self.getBuildArtifact("b.out")) 4499451b44SJordan Rupprecht 45ab05d913Stcwg @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24778") 4699451b44SJordan Rupprecht def test_find_functions(self): 4799451b44SJordan Rupprecht """Exercise SBTarget.FindFunctions() API.""" 482238dcc3SJonas Devlieghere d = {"EXE": "b.out"} 4999451b44SJordan Rupprecht self.build(dictionary=d) 5099451b44SJordan Rupprecht self.setTearDownCleanup(dictionary=d) 512238dcc3SJonas Devlieghere self.find_functions("b.out") 5299451b44SJordan Rupprecht 5399451b44SJordan Rupprecht def test_get_description(self): 5499451b44SJordan Rupprecht """Exercise SBTarget.GetDescription() API.""" 5599451b44SJordan Rupprecht self.build() 5699451b44SJordan Rupprecht self.get_description() 5799451b44SJordan Rupprecht 58ab855530SJonas Devlieghere @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr21765") 5999451b44SJordan Rupprecht def test_resolve_symbol_context_with_address(self): 6099451b44SJordan Rupprecht """Exercise SBTarget.ResolveSymbolContextForAddress() API.""" 6199451b44SJordan Rupprecht self.build() 6299451b44SJordan Rupprecht self.resolve_symbol_context_with_address() 6399451b44SJordan Rupprecht 6499451b44SJordan Rupprecht def test_get_platform(self): 652238dcc3SJonas Devlieghere d = {"EXE": "b.out"} 6699451b44SJordan Rupprecht self.build(dictionary=d) 6799451b44SJordan Rupprecht self.setTearDownCleanup(dictionary=d) 682238dcc3SJonas Devlieghere target = self.create_simple_target("b.out") 6999451b44SJordan Rupprecht platform = target.platform 7099451b44SJordan Rupprecht self.assertTrue(platform, VALID_PLATFORM) 7199451b44SJordan Rupprecht 7299451b44SJordan Rupprecht def test_get_data_byte_size(self): 732238dcc3SJonas Devlieghere d = {"EXE": "b.out"} 7499451b44SJordan Rupprecht self.build(dictionary=d) 7599451b44SJordan Rupprecht self.setTearDownCleanup(dictionary=d) 762238dcc3SJonas Devlieghere target = self.create_simple_target("b.out") 7799451b44SJordan Rupprecht self.assertEqual(target.data_byte_size, 1) 7899451b44SJordan Rupprecht 7999451b44SJordan Rupprecht def test_get_code_byte_size(self): 802238dcc3SJonas Devlieghere d = {"EXE": "b.out"} 8199451b44SJordan Rupprecht self.build(dictionary=d) 8299451b44SJordan Rupprecht self.setTearDownCleanup(dictionary=d) 832238dcc3SJonas Devlieghere target = self.create_simple_target("b.out") 8499451b44SJordan Rupprecht self.assertEqual(target.code_byte_size, 1) 8599451b44SJordan Rupprecht 8699451b44SJordan Rupprecht def test_resolve_file_address(self): 872238dcc3SJonas Devlieghere d = {"EXE": "b.out"} 8899451b44SJordan Rupprecht self.build(dictionary=d) 8999451b44SJordan Rupprecht self.setTearDownCleanup(dictionary=d) 902238dcc3SJonas Devlieghere target = self.create_simple_target("b.out") 9199451b44SJordan Rupprecht 9299451b44SJordan Rupprecht # find the file address in the .data section of the main 9399451b44SJordan Rupprecht # module 9499451b44SJordan Rupprecht data_section = self.find_data_section(target) 9599451b44SJordan Rupprecht data_section_addr = data_section.file_addr 9699451b44SJordan Rupprecht 9799451b44SJordan Rupprecht # resolve the above address, and compare the address produced 9899451b44SJordan Rupprecht # by the resolution against the original address/section 9999451b44SJordan Rupprecht res_file_addr = target.ResolveFileAddress(data_section_addr) 10099451b44SJordan Rupprecht self.assertTrue(res_file_addr.IsValid()) 10199451b44SJordan Rupprecht 10299451b44SJordan Rupprecht self.assertEqual(data_section_addr, res_file_addr.file_addr) 10399451b44SJordan Rupprecht 10499451b44SJordan Rupprecht data_section2 = res_file_addr.section 10599451b44SJordan Rupprecht self.assertIsNotNone(data_section2) 10699451b44SJordan Rupprecht self.assertEqual(data_section.name, data_section2.name) 10799451b44SJordan Rupprecht 10833f9fc77SJim Ingham def test_get_ABIName(self): 1092238dcc3SJonas Devlieghere d = {"EXE": "b.out"} 11033f9fc77SJim Ingham self.build(dictionary=d) 11133f9fc77SJim Ingham self.setTearDownCleanup(dictionary=d) 1122238dcc3SJonas Devlieghere target = self.create_simple_target("b.out") 11333f9fc77SJim Ingham 11433f9fc77SJim Ingham abi_pre_launch = target.GetABIName() 1159c246882SJordan Rupprecht self.assertNotEqual(len(abi_pre_launch), 0, "Got an ABI string") 11633f9fc77SJim Ingham 1172238dcc3SJonas Devlieghere breakpoint = target.BreakpointCreateByLocation("main.c", self.line_main) 11833f9fc77SJim Ingham self.assertTrue(breakpoint, VALID_BREAKPOINT) 11933f9fc77SJim Ingham 12033f9fc77SJim Ingham # Put debugger into synchronous mode so when we target.LaunchSimple returns 12133f9fc77SJim Ingham # it will guaranteed to be at the breakpoint 12233f9fc77SJim Ingham self.dbg.SetAsync(False) 12333f9fc77SJim Ingham 12433f9fc77SJim Ingham # Launch the process, and do not stop at the entry point. 1252238dcc3SJonas Devlieghere process = target.LaunchSimple(None, None, self.get_process_working_directory()) 12633f9fc77SJim Ingham abi_after_launch = target.GetABIName() 1272238dcc3SJonas Devlieghere self.assertEqual( 1282238dcc3SJonas Devlieghere abi_pre_launch, abi_after_launch, "ABI's match before and during run" 1292238dcc3SJonas Devlieghere ) 13033f9fc77SJim Ingham 13199451b44SJordan Rupprecht def test_read_memory(self): 1322238dcc3SJonas Devlieghere d = {"EXE": "b.out"} 13399451b44SJordan Rupprecht self.build(dictionary=d) 13499451b44SJordan Rupprecht self.setTearDownCleanup(dictionary=d) 1352238dcc3SJonas Devlieghere target = self.create_simple_target("b.out") 13699451b44SJordan Rupprecht 1372238dcc3SJonas Devlieghere breakpoint = target.BreakpointCreateByLocation("main.c", self.line_main) 13899451b44SJordan Rupprecht self.assertTrue(breakpoint, VALID_BREAKPOINT) 13999451b44SJordan Rupprecht 14099451b44SJordan Rupprecht # Put debugger into synchronous mode so when we target.LaunchSimple returns 14199451b44SJordan Rupprecht # it will guaranteed to be at the breakpoint 14299451b44SJordan Rupprecht self.dbg.SetAsync(False) 14399451b44SJordan Rupprecht 14499451b44SJordan Rupprecht # Launch the process, and do not stop at the entry point. 1452238dcc3SJonas Devlieghere process = target.LaunchSimple(None, None, self.get_process_working_directory()) 14699451b44SJordan Rupprecht 14799451b44SJordan Rupprecht # find the file address in the .data section of the main 14899451b44SJordan Rupprecht # module 14999451b44SJordan Rupprecht data_section = self.find_data_section(target) 15099451b44SJordan Rupprecht sb_addr = lldb.SBAddress(data_section, 0) 15199451b44SJordan Rupprecht error = lldb.SBError() 15299451b44SJordan Rupprecht content = target.ReadMemory(sb_addr, 1, error) 153779bbbf2SDave Lee self.assertSuccess(error, "Make sure memory read succeeded") 15499451b44SJordan Rupprecht self.assertEqual(len(content), 1) 15599451b44SJordan Rupprecht 156*df6822f4SPavel Labath # Make sure reading from 0x0 fails 157*df6822f4SPavel Labath sb_addr = lldb.SBAddress(0, target) 158*df6822f4SPavel Labath self.assertIsNone(target.ReadMemory(sb_addr, 1, error)) 159*df6822f4SPavel Labath self.assertTrue(error.Fail()) 160*df6822f4SPavel Labath 161927afdffSJonas Devlieghere @skipIfWindows # stdio manipulation unsupported on Windows 162ba37b144SJonas Devlieghere @skipIfRemote # stdio manipulation unsupported on remote iOS devices<rdar://problem/54581135> 16394a59199SMuhammad Omair Javaid @skipIf(oslist=["linux"], archs=["arm", "aarch64"]) 164dbf44b83SJonas Devlieghere @no_debug_info_test 165882d8e60SJonas Devlieghere def test_launch_simple(self): 1662238dcc3SJonas Devlieghere d = {"EXE": "b.out"} 167882d8e60SJonas Devlieghere self.build(dictionary=d) 168882d8e60SJonas Devlieghere self.setTearDownCleanup(dictionary=d) 1692238dcc3SJonas Devlieghere target = self.create_simple_target("b.out") 170882d8e60SJonas Devlieghere 171a97dfdc3SJonas Devlieghere # Set the debugger to synchronous mode so we only continue after the 172a97dfdc3SJonas Devlieghere # process has exited. 173a97dfdc3SJonas Devlieghere self.dbg.SetAsync(False) 174a97dfdc3SJonas Devlieghere 175882d8e60SJonas Devlieghere process = target.LaunchSimple( 1762238dcc3SJonas Devlieghere ["foo", "bar"], ["baz"], self.get_process_working_directory() 1772238dcc3SJonas Devlieghere ) 178bb4efab9SRaphael Isemann process.Continue() 1791b8c7352SJonas Devlieghere self.assertState(process.GetState(), lldb.eStateExited) 180882d8e60SJonas Devlieghere output = process.GetSTDOUT(9999) 1812238dcc3SJonas Devlieghere self.assertIn("arg: foo", output) 1822238dcc3SJonas Devlieghere self.assertIn("arg: bar", output) 1832238dcc3SJonas Devlieghere self.assertIn("env: baz", output) 184882d8e60SJonas Devlieghere 185882d8e60SJonas Devlieghere self.runCmd("setting set target.run-args foo") 186882d8e60SJonas Devlieghere self.runCmd("setting set target.env-vars bar=baz") 1872238dcc3SJonas Devlieghere process = target.LaunchSimple(None, None, self.get_process_working_directory()) 188bb4efab9SRaphael Isemann process.Continue() 1891b8c7352SJonas Devlieghere self.assertState(process.GetState(), lldb.eStateExited) 190882d8e60SJonas Devlieghere output = process.GetSTDOUT(9999) 1912238dcc3SJonas Devlieghere self.assertIn("arg: foo", output) 1922238dcc3SJonas Devlieghere self.assertIn("env: bar=baz", output) 193882d8e60SJonas Devlieghere 194a8bec611SRaphael Isemann # Clear all the run args set above. 195a8bec611SRaphael Isemann self.runCmd("setting clear target.run-args") 1962238dcc3SJonas Devlieghere process = target.LaunchSimple(None, None, self.get_process_working_directory()) 197a8bec611SRaphael Isemann process.Continue() 198a8bec611SRaphael Isemann self.assertEqual(process.GetState(), lldb.eStateExited) 199a8bec611SRaphael Isemann output = process.GetSTDOUT(9999) 2002238dcc3SJonas Devlieghere self.assertNotIn("arg: foo", output) 201a8bec611SRaphael Isemann 202882d8e60SJonas Devlieghere self.runCmd("settings set target.disable-stdio true") 2032238dcc3SJonas Devlieghere process = target.LaunchSimple(None, None, self.get_process_working_directory()) 204bb4efab9SRaphael Isemann process.Continue() 2051b8c7352SJonas Devlieghere self.assertState(process.GetState(), lldb.eStateExited) 206882d8e60SJonas Devlieghere output = process.GetSTDOUT(9999) 207882d8e60SJonas Devlieghere self.assertEqual(output, "") 208882d8e60SJonas Devlieghere 20999451b44SJordan Rupprecht def create_simple_target(self, fn): 21099451b44SJordan Rupprecht exe = self.getBuildArtifact(fn) 21199451b44SJordan Rupprecht target = self.dbg.CreateTarget(exe) 21299451b44SJordan Rupprecht self.assertTrue(target, VALID_TARGET) 21399451b44SJordan Rupprecht return target 21499451b44SJordan Rupprecht 21599451b44SJordan Rupprecht def find_data_section(self, target): 21699451b44SJordan Rupprecht mod = target.GetModuleAtIndex(0) 21799451b44SJordan Rupprecht data_section = None 21899451b44SJordan Rupprecht for s in mod.sections: 21999451b44SJordan Rupprecht sect_type = s.GetSectionType() 22099451b44SJordan Rupprecht if sect_type == lldb.eSectionTypeData: 22199451b44SJordan Rupprecht data_section = s 22299451b44SJordan Rupprecht break 22399451b44SJordan Rupprecht elif sect_type == lldb.eSectionTypeContainer: 22499451b44SJordan Rupprecht for i in range(s.GetNumSubSections()): 22599451b44SJordan Rupprecht ss = s.GetSubSectionAtIndex(i) 22699451b44SJordan Rupprecht sect_type = ss.GetSectionType() 22799451b44SJordan Rupprecht if sect_type == lldb.eSectionTypeData: 22899451b44SJordan Rupprecht data_section = ss 22999451b44SJordan Rupprecht break 23099451b44SJordan Rupprecht 23199451b44SJordan Rupprecht self.assertIsNotNone(data_section) 23299451b44SJordan Rupprecht return data_section 23399451b44SJordan Rupprecht 23499451b44SJordan Rupprecht def find_global_variables(self, exe_name): 2357c704c0fSShao-Ce SUN """Exercise SBTarget.FindGlobalVariables() API.""" 23699451b44SJordan Rupprecht exe = self.getBuildArtifact(exe_name) 23799451b44SJordan Rupprecht 23899451b44SJordan Rupprecht # Create a target by the debugger. 23999451b44SJordan Rupprecht target = self.dbg.CreateTarget(exe) 24099451b44SJordan Rupprecht self.assertTrue(target, VALID_TARGET) 24199451b44SJordan Rupprecht 24299451b44SJordan Rupprecht # rdar://problem/9700873 24399451b44SJordan Rupprecht # Find global variable value fails for dwarf if inferior not started 24499451b44SJordan Rupprecht # (Was CrashTracer: [USER] 1 crash in Python at _lldb.so: lldb_private::MemoryCache::Read + 94) 24599451b44SJordan Rupprecht # 24699451b44SJordan Rupprecht # Remove the lines to create a breakpoint and to start the inferior 24799451b44SJordan Rupprecht # which are workarounds for the dwarf case. 24899451b44SJordan Rupprecht 2492238dcc3SJonas Devlieghere breakpoint = target.BreakpointCreateByLocation("main.c", self.line1) 25099451b44SJordan Rupprecht self.assertTrue(breakpoint, VALID_BREAKPOINT) 25199451b44SJordan Rupprecht 25299451b44SJordan Rupprecht # Now launch the process, and do not stop at entry point. 2532238dcc3SJonas Devlieghere process = target.LaunchSimple(None, None, self.get_process_working_directory()) 25499451b44SJordan Rupprecht self.assertTrue(process, PROCESS_IS_VALID) 25599451b44SJordan Rupprecht # Make sure we hit our breakpoint: 2562238dcc3SJonas Devlieghere thread_list = lldbutil.get_threads_stopped_at_breakpoint(process, breakpoint) 257619e2e09SDave Lee self.assertEqual(len(thread_list), 1) 25899451b44SJordan Rupprecht 2592238dcc3SJonas Devlieghere value_list = target.FindGlobalVariables("my_global_var_of_char_type", 3) 260619e2e09SDave Lee self.assertEqual(value_list.GetSize(), 1) 26199451b44SJordan Rupprecht my_global_var = value_list.GetValueAtIndex(0) 26299451b44SJordan Rupprecht self.DebugSBValue(my_global_var) 26399451b44SJordan Rupprecht self.assertTrue(my_global_var) 2642238dcc3SJonas Devlieghere self.expect( 2652238dcc3SJonas Devlieghere my_global_var.GetName(), exe=False, startstr="my_global_var_of_char_type" 2662238dcc3SJonas Devlieghere ) 2672238dcc3SJonas Devlieghere self.expect(my_global_var.GetTypeName(), exe=False, startstr="char") 2682238dcc3SJonas Devlieghere self.expect(my_global_var.GetValue(), exe=False, startstr="'X'") 26999451b44SJordan Rupprecht 27099451b44SJordan Rupprecht # While we are at it, let's also exercise the similar 27199451b44SJordan Rupprecht # SBModule.FindGlobalVariables() API. 27299451b44SJordan Rupprecht for m in target.module_iter(): 2732238dcc3SJonas Devlieghere if ( 2742238dcc3SJonas Devlieghere os.path.normpath(m.GetFileSpec().GetDirectory()) == self.getBuildDir() 2752238dcc3SJonas Devlieghere and m.GetFileSpec().GetFilename() == exe_name 2762238dcc3SJonas Devlieghere ): 27799451b44SJordan Rupprecht value_list = m.FindGlobalVariables( 2782238dcc3SJonas Devlieghere target, "my_global_var_of_char_type", 3 2792238dcc3SJonas Devlieghere ) 280619e2e09SDave Lee self.assertEqual(value_list.GetSize(), 1) 2812238dcc3SJonas Devlieghere self.assertEqual(value_list.GetValueAtIndex(0).GetValue(), "'X'") 28299451b44SJordan Rupprecht break 28399451b44SJordan Rupprecht 28499451b44SJordan Rupprecht def find_compile_units(self, exe): 28599451b44SJordan Rupprecht """Exercise SBTarget.FindCompileUnits() API.""" 28699451b44SJordan Rupprecht source_name = "main.c" 28799451b44SJordan Rupprecht 28899451b44SJordan Rupprecht # Create a target by the debugger. 28999451b44SJordan Rupprecht target = self.dbg.CreateTarget(exe) 29099451b44SJordan Rupprecht self.assertTrue(target, VALID_TARGET) 29199451b44SJordan Rupprecht 29299451b44SJordan Rupprecht list = target.FindCompileUnits(lldb.SBFileSpec(source_name, False)) 29399451b44SJordan Rupprecht # Executable has been built just from one source file 'main.c', 29499451b44SJordan Rupprecht # so we may check only the first element of list. 2950ed758b2SDave Lee self.assertEqual( 2962238dcc3SJonas Devlieghere list[0].GetCompileUnit().GetFileSpec().GetFilename(), source_name 2972238dcc3SJonas Devlieghere ) 29899451b44SJordan Rupprecht 29999451b44SJordan Rupprecht def find_functions(self, exe_name): 3007c704c0fSShao-Ce SUN """Exercise SBTarget.FindFunctions() API.""" 30199451b44SJordan Rupprecht exe = self.getBuildArtifact(exe_name) 30299451b44SJordan Rupprecht 30399451b44SJordan Rupprecht # Create a target by the debugger. 30499451b44SJordan Rupprecht target = self.dbg.CreateTarget(exe) 30599451b44SJordan Rupprecht self.assertTrue(target, VALID_TARGET) 30699451b44SJordan Rupprecht 30708063f85SJim Ingham # Try it with a null name: 30808063f85SJim Ingham list = target.FindFunctions(None, lldb.eFunctionNameTypeAuto) 309619e2e09SDave Lee self.assertEqual(list.GetSize(), 0) 31008063f85SJim Ingham 3112238dcc3SJonas Devlieghere list = target.FindFunctions("c", lldb.eFunctionNameTypeAuto) 312619e2e09SDave Lee self.assertEqual(list.GetSize(), 1) 31399451b44SJordan Rupprecht 31499451b44SJordan Rupprecht for sc in list: 3152238dcc3SJonas Devlieghere self.assertEqual(sc.GetModule().GetFileSpec().GetFilename(), exe_name) 3162238dcc3SJonas Devlieghere self.assertEqual(sc.GetSymbol().GetName(), "c") 31799451b44SJordan Rupprecht 31899451b44SJordan Rupprecht def get_description(self): 3197c704c0fSShao-Ce SUN """Exercise SBTarget.GetDescription() API.""" 32099451b44SJordan Rupprecht exe = self.getBuildArtifact("a.out") 32199451b44SJordan Rupprecht 32299451b44SJordan Rupprecht # Create a target by the debugger. 32399451b44SJordan Rupprecht target = self.dbg.CreateTarget(exe) 32499451b44SJordan Rupprecht self.assertTrue(target, VALID_TARGET) 32599451b44SJordan Rupprecht 32699451b44SJordan Rupprecht from lldbsuite.test.lldbutil import get_description 32799451b44SJordan Rupprecht 32899451b44SJordan Rupprecht # get_description() allows no option to mean 32999451b44SJordan Rupprecht # lldb.eDescriptionLevelBrief. 33099451b44SJordan Rupprecht desc = get_description(target) 33199451b44SJordan Rupprecht # desc = get_description(target, option=lldb.eDescriptionLevelBrief) 33299451b44SJordan Rupprecht if not desc: 33399451b44SJordan Rupprecht self.fail("SBTarget.GetDescription() failed") 3342238dcc3SJonas Devlieghere self.expect(desc, exe=False, substrs=["a.out"]) 3352238dcc3SJonas Devlieghere self.expect( 3362238dcc3SJonas Devlieghere desc, exe=False, matching=False, substrs=["Target", "Module", "Breakpoint"] 3372238dcc3SJonas Devlieghere ) 33899451b44SJordan Rupprecht 33999451b44SJordan Rupprecht desc = get_description(target, option=lldb.eDescriptionLevelFull) 34099451b44SJordan Rupprecht if not desc: 34199451b44SJordan Rupprecht self.fail("SBTarget.GetDescription() failed") 3422238dcc3SJonas Devlieghere self.expect( 3432238dcc3SJonas Devlieghere desc, exe=False, substrs=["Target", "Module", "a.out", "Breakpoint"] 3442238dcc3SJonas Devlieghere ) 34599451b44SJordan Rupprecht 34666ae40ebSRaphael Isemann @skipIfRemote 34799451b44SJordan Rupprecht @no_debug_info_test 34899451b44SJordan Rupprecht def test_launch_new_process_and_redirect_stdout(self): 3497c704c0fSShao-Ce SUN """Exercise SBTarget.Launch() API with redirected stdout.""" 35099451b44SJordan Rupprecht self.build() 35199451b44SJordan Rupprecht exe = self.getBuildArtifact("a.out") 35299451b44SJordan Rupprecht 35399451b44SJordan Rupprecht # Create a target by the debugger. 35499451b44SJordan Rupprecht target = self.dbg.CreateTarget(exe) 35599451b44SJordan Rupprecht self.assertTrue(target, VALID_TARGET) 35699451b44SJordan Rupprecht 35799451b44SJordan Rupprecht # Add an extra twist of stopping the inferior in a breakpoint, and then continue till it's done. 35899451b44SJordan Rupprecht # We should still see the entire stdout redirected once the process is 35999451b44SJordan Rupprecht # finished. 3602238dcc3SJonas Devlieghere line = line_number("main.c", "// a(3) -> c(3)") 3612238dcc3SJonas Devlieghere breakpoint = target.BreakpointCreateByLocation("main.c", line) 36299451b44SJordan Rupprecht 36399451b44SJordan Rupprecht # Now launch the process, do not stop at entry point, and redirect stdout to "stdout.txt" file. 36499451b44SJordan Rupprecht # The inferior should run to completion after "process.Continue()" 36599451b44SJordan Rupprecht # call. 36699451b44SJordan Rupprecht local_path = self.getBuildArtifact("stdout.txt") 36799451b44SJordan Rupprecht if os.path.exists(local_path): 36899451b44SJordan Rupprecht os.remove(local_path) 36999451b44SJordan Rupprecht 37099451b44SJordan Rupprecht if lldb.remote_platform: 3712238dcc3SJonas Devlieghere stdout_path = lldbutil.append_to_process_working_directory( 3722238dcc3SJonas Devlieghere self, "lldb-stdout-redirect.txt" 3732238dcc3SJonas Devlieghere ) 37499451b44SJordan Rupprecht else: 37599451b44SJordan Rupprecht stdout_path = local_path 37699451b44SJordan Rupprecht error = lldb.SBError() 37799451b44SJordan Rupprecht process = target.Launch( 37899451b44SJordan Rupprecht self.dbg.GetListener(), 37999451b44SJordan Rupprecht None, 38099451b44SJordan Rupprecht None, 38199451b44SJordan Rupprecht None, 38299451b44SJordan Rupprecht stdout_path, 38399451b44SJordan Rupprecht None, 38499451b44SJordan Rupprecht None, 38599451b44SJordan Rupprecht 0, 38699451b44SJordan Rupprecht False, 3872238dcc3SJonas Devlieghere error, 3882238dcc3SJonas Devlieghere ) 38999451b44SJordan Rupprecht process.Continue() 39099451b44SJordan Rupprecht # self.runCmd("process status") 39199451b44SJordan Rupprecht if lldb.remote_platform: 39299451b44SJordan Rupprecht # copy output file to host 39399451b44SJordan Rupprecht lldb.remote_platform.Get( 3942238dcc3SJonas Devlieghere lldb.SBFileSpec(stdout_path), lldb.SBFileSpec(local_path) 3952238dcc3SJonas Devlieghere ) 39699451b44SJordan Rupprecht 39799451b44SJordan Rupprecht # The 'stdout.txt' file should now exist. 39899451b44SJordan Rupprecht self.assertTrue( 39999451b44SJordan Rupprecht os.path.isfile(local_path), 4002238dcc3SJonas Devlieghere "'stdout.txt' exists due to redirected stdout via SBTarget.Launch() API.", 4012238dcc3SJonas Devlieghere ) 40299451b44SJordan Rupprecht 40399451b44SJordan Rupprecht # Read the output file produced by running the program. 4042238dcc3SJonas Devlieghere with open(local_path, "r") as f: 40599451b44SJordan Rupprecht output = f.read() 40699451b44SJordan Rupprecht 4072238dcc3SJonas Devlieghere self.expect(output, exe=False, substrs=["a(1)", "b(2)", "a(3)"]) 40899451b44SJordan Rupprecht 40999451b44SJordan Rupprecht def resolve_symbol_context_with_address(self): 4107c704c0fSShao-Ce SUN """Exercise SBTarget.ResolveSymbolContextForAddress() API.""" 41199451b44SJordan Rupprecht exe = self.getBuildArtifact("a.out") 41299451b44SJordan Rupprecht 41399451b44SJordan Rupprecht # Create a target by the debugger. 41499451b44SJordan Rupprecht target = self.dbg.CreateTarget(exe) 41599451b44SJordan Rupprecht self.assertTrue(target, VALID_TARGET) 41699451b44SJordan Rupprecht 41799451b44SJordan Rupprecht # Now create the two breakpoints inside function 'a'. 4182238dcc3SJonas Devlieghere breakpoint1 = target.BreakpointCreateByLocation("main.c", self.line1) 4192238dcc3SJonas Devlieghere breakpoint2 = target.BreakpointCreateByLocation("main.c", self.line2) 420b321b429SJonas Devlieghere self.trace("breakpoint1:", breakpoint1) 421b321b429SJonas Devlieghere self.trace("breakpoint2:", breakpoint2) 4222238dcc3SJonas Devlieghere self.assertTrue( 4232238dcc3SJonas Devlieghere breakpoint1 and breakpoint1.GetNumLocations() == 1, VALID_BREAKPOINT 4242238dcc3SJonas Devlieghere ) 4252238dcc3SJonas Devlieghere self.assertTrue( 4262238dcc3SJonas Devlieghere breakpoint2 and breakpoint2.GetNumLocations() == 1, VALID_BREAKPOINT 4272238dcc3SJonas Devlieghere ) 42899451b44SJordan Rupprecht 42999451b44SJordan Rupprecht # Now launch the process, and do not stop at entry point. 4302238dcc3SJonas Devlieghere process = target.LaunchSimple(None, None, self.get_process_working_directory()) 43199451b44SJordan Rupprecht self.assertTrue(process, PROCESS_IS_VALID) 43299451b44SJordan Rupprecht 43399451b44SJordan Rupprecht # Frame #0 should be on self.line1. 43447c4c6a7SDave Lee self.assertState(process.GetState(), lldb.eStateStopped) 4352238dcc3SJonas Devlieghere thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint) 43699451b44SJordan Rupprecht self.assertTrue( 43799451b44SJordan Rupprecht thread.IsValid(), 4382238dcc3SJonas Devlieghere "There should be a thread stopped due to breakpoint condition", 4392238dcc3SJonas Devlieghere ) 44099451b44SJordan Rupprecht # self.runCmd("process status") 44199451b44SJordan Rupprecht frame0 = thread.GetFrameAtIndex(0) 44299451b44SJordan Rupprecht lineEntry = frame0.GetLineEntry() 443619e2e09SDave Lee self.assertEqual(lineEntry.GetLine(), self.line1) 44499451b44SJordan Rupprecht 44599451b44SJordan Rupprecht address1 = lineEntry.GetStartAddress() 44699451b44SJordan Rupprecht 44799451b44SJordan Rupprecht # Continue the inferior, the breakpoint 2 should be hit. 44899451b44SJordan Rupprecht process.Continue() 44947c4c6a7SDave Lee self.assertState(process.GetState(), lldb.eStateStopped) 4502238dcc3SJonas Devlieghere thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint) 45199451b44SJordan Rupprecht self.assertTrue( 45299451b44SJordan Rupprecht thread.IsValid(), 4532238dcc3SJonas Devlieghere "There should be a thread stopped due to breakpoint condition", 4542238dcc3SJonas Devlieghere ) 45599451b44SJordan Rupprecht # self.runCmd("process status") 45699451b44SJordan Rupprecht frame0 = thread.GetFrameAtIndex(0) 45799451b44SJordan Rupprecht lineEntry = frame0.GetLineEntry() 458619e2e09SDave Lee self.assertEqual(lineEntry.GetLine(), self.line2) 45999451b44SJordan Rupprecht 46099451b44SJordan Rupprecht address2 = lineEntry.GetStartAddress() 46199451b44SJordan Rupprecht 462b321b429SJonas Devlieghere self.trace("address1:", address1) 463b321b429SJonas Devlieghere self.trace("address2:", address2) 46499451b44SJordan Rupprecht 46599451b44SJordan Rupprecht # Now call SBTarget.ResolveSymbolContextForAddress() with the addresses 46699451b44SJordan Rupprecht # from our line entry. 46799451b44SJordan Rupprecht context1 = target.ResolveSymbolContextForAddress( 4682238dcc3SJonas Devlieghere address1, lldb.eSymbolContextEverything 4692238dcc3SJonas Devlieghere ) 47099451b44SJordan Rupprecht context2 = target.ResolveSymbolContextForAddress( 4712238dcc3SJonas Devlieghere address2, lldb.eSymbolContextEverything 4722238dcc3SJonas Devlieghere ) 47399451b44SJordan Rupprecht 47499451b44SJordan Rupprecht self.assertTrue(context1 and context2) 475b321b429SJonas Devlieghere self.trace("context1:", context1) 476b321b429SJonas Devlieghere self.trace("context2:", context2) 47799451b44SJordan Rupprecht 47899451b44SJordan Rupprecht # Verify that the context point to the same function 'a'. 47999451b44SJordan Rupprecht symbol1 = context1.GetSymbol() 48099451b44SJordan Rupprecht symbol2 = context2.GetSymbol() 48199451b44SJordan Rupprecht self.assertTrue(symbol1 and symbol2) 482b321b429SJonas Devlieghere self.trace("symbol1:", symbol1) 483b321b429SJonas Devlieghere self.trace("symbol2:", symbol2) 48499451b44SJordan Rupprecht 48599451b44SJordan Rupprecht from lldbsuite.test.lldbutil import get_description 4862238dcc3SJonas Devlieghere 48799451b44SJordan Rupprecht desc1 = get_description(symbol1) 48899451b44SJordan Rupprecht desc2 = get_description(symbol2) 4892238dcc3SJonas Devlieghere self.assertTrue( 4902238dcc3SJonas Devlieghere desc1 and desc2 and desc1 == desc2, 4912238dcc3SJonas Devlieghere "The two addresses should resolve to the same symbol", 4922238dcc3SJonas Devlieghere ) 49305d7d694STatyana Krasnukha 49466b829acSJonas Devlieghere @skipIfRemote 4957636b1f6SJim Ingham def test_default_arch(self): 4967636b1f6SJim Ingham """Test the other two target create methods using LLDB_ARCH_DEFAULT.""" 4977636b1f6SJim Ingham self.build() 4987636b1f6SJim Ingham exe = self.getBuildArtifact("a.out") 4992238dcc3SJonas Devlieghere target = self.dbg.CreateTargetWithFileAndArch(exe, lldb.LLDB_ARCH_DEFAULT) 5007636b1f6SJim Ingham self.assertTrue(target.IsValid(), "Default arch made a valid target.") 5017636b1f6SJim Ingham # This should also work with the target's triple: 5027636b1f6SJim Ingham target2 = self.dbg.CreateTargetWithFileAndArch(exe, target.GetTriple()) 5037636b1f6SJim Ingham self.assertTrue(target2.IsValid(), "Round trip with triple works") 5047636b1f6SJim Ingham # And this triple should work for the FileAndTriple API: 5052238dcc3SJonas Devlieghere target3 = self.dbg.CreateTargetWithFileAndTargetTriple(exe, target.GetTriple()) 5067636b1f6SJim Ingham self.assertTrue(target3.IsValid()) 5077636b1f6SJim Ingham 508ee562e23SMed Ismail Bennani @skipIfWindows 509b8923c00SMed Ismail Bennani def test_is_loaded(self): 510b8923c00SMed Ismail Bennani """Exercise SBTarget.IsLoaded(SBModule&) API.""" 5112238dcc3SJonas Devlieghere d = {"EXE": "b.out"} 512b8923c00SMed Ismail Bennani self.build(dictionary=d) 513b8923c00SMed Ismail Bennani self.setTearDownCleanup(dictionary=d) 5142238dcc3SJonas Devlieghere target = self.create_simple_target("b.out") 515b8923c00SMed Ismail Bennani 516b8923c00SMed Ismail Bennani self.assertFalse(target.IsLoaded(lldb.SBModule())) 517b8923c00SMed Ismail Bennani 518b8923c00SMed Ismail Bennani num_modules = target.GetNumModules() 519b8923c00SMed Ismail Bennani for i in range(num_modules): 520b8923c00SMed Ismail Bennani module = target.GetModuleAtIndex(i) 5212238dcc3SJonas Devlieghere self.assertFalse( 5222238dcc3SJonas Devlieghere target.IsLoaded(module), 5232238dcc3SJonas Devlieghere "Target that isn't " "running shouldn't have any module loaded.", 5242238dcc3SJonas Devlieghere ) 525b8923c00SMed Ismail Bennani 5262238dcc3SJonas Devlieghere process = target.LaunchSimple(None, None, self.get_process_working_directory()) 527b8923c00SMed Ismail Bennani 528b8923c00SMed Ismail Bennani for i in range(num_modules): 529b8923c00SMed Ismail Bennani module = target.GetModuleAtIndex(i) 5302238dcc3SJonas Devlieghere self.assertTrue( 5312238dcc3SJonas Devlieghere target.IsLoaded(module), 5322238dcc3SJonas Devlieghere "Running the target should " "have loaded its modules.", 5332238dcc3SJonas Devlieghere ) 5341dd9162bSGreg Clayton 5351dd9162bSGreg Clayton @no_debug_info_test 5361dd9162bSGreg Clayton def test_setting_selected_target_with_invalid_target(self): 5371dd9162bSGreg Clayton """Make sure we don't crash when trying to select invalid target.""" 5381dd9162bSGreg Clayton target = lldb.SBTarget() 5391dd9162bSGreg Clayton self.dbg.SetSelectedTarget(target) 540