Johnny Chen | d61816b | 2011-03-03 01:41:57 +0000 | [diff] [blame] | 1 | """ |
| 2 | Test SBTarget APIs. |
| 3 | """ |
| 4 | |
| 5 | import os, time |
| 6 | import re |
| 7 | import unittest2 |
| 8 | import lldb, lldbutil |
| 9 | from lldbtest import * |
| 10 | |
| 11 | class TargetAPITestCase(TestBase): |
| 12 | |
| 13 | mydir = os.path.join("python_api", "target") |
| 14 | |
| 15 | @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") |
| 16 | @python_api_test |
Johnny Chen | 466c593 | 2011-06-29 22:45:06 +0000 | [diff] [blame] | 17 | def test_find_global_variables_with_dsym(self): |
| 18 | """Exercise SBTaget.FindGlobalVariables() API.""" |
Johnny Chen | 086b1b7 | 2011-06-30 00:24:31 +0000 | [diff] [blame^] | 19 | d = {'EXE': 'a.out'} |
| 20 | self.buildDsym(dictionary=d) |
| 21 | self.setTearDownCleanup(dictionary=d) |
| 22 | self.find_global_variables('a.out') |
Johnny Chen | 466c593 | 2011-06-29 22:45:06 +0000 | [diff] [blame] | 23 | |
Johnny Chen | 086b1b7 | 2011-06-30 00:24:31 +0000 | [diff] [blame^] | 24 | #rdar://problem/9700873 |
| 25 | @unittest2.skip("segmentation fault -- skipping") |
Johnny Chen | 466c593 | 2011-06-29 22:45:06 +0000 | [diff] [blame] | 26 | @python_api_test |
| 27 | def test_find_global_variables_with_dwarf(self): |
| 28 | """Exercise SBTarget.FindGlobalVariables() API.""" |
Johnny Chen | 086b1b7 | 2011-06-30 00:24:31 +0000 | [diff] [blame^] | 29 | d = {'EXE': 'b.out'} |
| 30 | self.buildDwarf(dictionary=d) |
| 31 | self.setTearDownCleanup(dictionary=d) |
| 32 | self.find_global_variables('b.out') |
Johnny Chen | 466c593 | 2011-06-29 22:45:06 +0000 | [diff] [blame] | 33 | |
| 34 | @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") |
| 35 | @python_api_test |
Johnny Chen | 787f71f | 2011-04-22 23:20:17 +0000 | [diff] [blame] | 36 | def test_get_description_with_dsym(self): |
| 37 | """Exercise SBTaget.GetDescription() API.""" |
| 38 | self.buildDsym() |
| 39 | self.get_description() |
| 40 | |
| 41 | @python_api_test |
| 42 | def test_get_description_with_dwarf(self): |
| 43 | """Exercise SBTarget.GetDescription() API.""" |
| 44 | self.buildDwarf() |
| 45 | self.get_description() |
| 46 | |
| 47 | @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") |
| 48 | @python_api_test |
Johnny Chen | 75625aa | 2011-03-07 22:29:04 +0000 | [diff] [blame] | 49 | def test_launch_new_process_and_redirect_stdout_with_dsym(self): |
| 50 | """Exercise SBTaget.Launch() API.""" |
| 51 | self.buildDsym() |
| 52 | self.launch_new_process_and_redirect_stdout() |
| 53 | |
| 54 | @python_api_test |
| 55 | def test_launch_new_process_and_redirect_stdout_with_dwarf(self): |
| 56 | """Exercise SBTarget.Launch() API.""" |
| 57 | self.buildDwarf() |
| 58 | self.launch_new_process_and_redirect_stdout() |
| 59 | |
| 60 | @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") |
| 61 | @python_api_test |
Johnny Chen | 05178f6 | 2011-03-04 23:40:06 +0000 | [diff] [blame] | 62 | def test_resolve_symbol_context_with_address_with_dsym(self): |
| 63 | """Exercise SBTaget.ResolveSymbolContextForAddress() API.""" |
Johnny Chen | d61816b | 2011-03-03 01:41:57 +0000 | [diff] [blame] | 64 | self.buildDsym() |
Johnny Chen | 05178f6 | 2011-03-04 23:40:06 +0000 | [diff] [blame] | 65 | self.resolve_symbol_context_with_address() |
Johnny Chen | d61816b | 2011-03-03 01:41:57 +0000 | [diff] [blame] | 66 | |
| 67 | @python_api_test |
Johnny Chen | 05178f6 | 2011-03-04 23:40:06 +0000 | [diff] [blame] | 68 | def test_resolve_symbol_context_with_address_with_dwarf(self): |
| 69 | """Exercise SBTarget.ResolveSymbolContextForAddress() API.""" |
Johnny Chen | d61816b | 2011-03-03 01:41:57 +0000 | [diff] [blame] | 70 | self.buildDwarf() |
Johnny Chen | 05178f6 | 2011-03-04 23:40:06 +0000 | [diff] [blame] | 71 | self.resolve_symbol_context_with_address() |
Johnny Chen | d61816b | 2011-03-03 01:41:57 +0000 | [diff] [blame] | 72 | |
| 73 | def setUp(self): |
| 74 | # Call super's setUp(). |
| 75 | TestBase.setUp(self) |
| 76 | # Find the line number to of function 'c'. |
| 77 | self.line1 = line_number('main.c', '// Find the line number for breakpoint 1 here.') |
| 78 | self.line2 = line_number('main.c', '// Find the line number for breakpoint 2 here.') |
| 79 | |
Johnny Chen | 086b1b7 | 2011-06-30 00:24:31 +0000 | [diff] [blame^] | 80 | def find_global_variables(self, exe_name): |
Johnny Chen | 466c593 | 2011-06-29 22:45:06 +0000 | [diff] [blame] | 81 | """Exercise SBTaget.FindGlobalVariables() API.""" |
Johnny Chen | 086b1b7 | 2011-06-30 00:24:31 +0000 | [diff] [blame^] | 82 | exe = os.path.join(os.getcwd(), exe_name) |
Johnny Chen | 466c593 | 2011-06-29 22:45:06 +0000 | [diff] [blame] | 83 | |
| 84 | # Create a target by the debugger. |
| 85 | target = self.dbg.CreateTarget(exe) |
| 86 | self.assertTrue(target, VALID_TARGET) |
| 87 | |
Johnny Chen | 086b1b7 | 2011-06-30 00:24:31 +0000 | [diff] [blame^] | 88 | value_list = target.FindGlobalVariables('my_global_var_of_char_type', 3) |
Johnny Chen | 466c593 | 2011-06-29 22:45:06 +0000 | [diff] [blame] | 89 | self.assertTrue(value_list.GetSize() == 1) |
| 90 | my_global_var = value_list.GetValueAtIndex(0) |
| 91 | self.expect(my_global_var.GetName(), exe=False, |
| 92 | startstr = "my_global_var_of_char_type") |
| 93 | self.expect(my_global_var.GetTypeName(), exe=False, |
| 94 | startstr = "char") |
| 95 | self.expect(my_global_var.GetValue(), exe=False, |
| 96 | startstr = "'X'") |
| 97 | |
Johnny Chen | 086b1b7 | 2011-06-30 00:24:31 +0000 | [diff] [blame^] | 98 | # While we are at it, let's also exercise the similar SBModule.FindGlobalVariables() API. |
| 99 | for m in target.module_iter(): |
| 100 | if m.GetFileSpec().GetDirectory() == os.getcwd() and m.GetFileSpec().GetFilename() == exe_name: |
| 101 | value_list = m.FindGlobalVariables(target, 'my_global_var_of_char_type', 3) |
| 102 | self.assertTrue(value_list.GetSize() == 1) |
| 103 | self.assertTrue(value_list.GetValueAtIndex(0).GetValue() == "'X'") |
| 104 | break |
| 105 | |
Johnny Chen | 787f71f | 2011-04-22 23:20:17 +0000 | [diff] [blame] | 106 | def get_description(self): |
| 107 | """Exercise SBTaget.GetDescription() API.""" |
| 108 | exe = os.path.join(os.getcwd(), "a.out") |
| 109 | |
| 110 | # Create a target by the debugger. |
| 111 | target = self.dbg.CreateTarget(exe) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 112 | self.assertTrue(target, VALID_TARGET) |
Johnny Chen | 787f71f | 2011-04-22 23:20:17 +0000 | [diff] [blame] | 113 | |
Johnny Chen | 90256cd | 2011-04-23 00:13:34 +0000 | [diff] [blame] | 114 | from lldbutil import get_description |
Johnny Chen | fc87e2d | 2011-04-25 20:23:05 +0000 | [diff] [blame] | 115 | |
| 116 | # get_description() allows no option to mean lldb.eDescriptionLevelBrief. |
| 117 | desc = get_description(target) |
| 118 | #desc = get_description(target, option=lldb.eDescriptionLevelBrief) |
Johnny Chen | 90256cd | 2011-04-23 00:13:34 +0000 | [diff] [blame] | 119 | if not desc: |
Johnny Chen | 787f71f | 2011-04-22 23:20:17 +0000 | [diff] [blame] | 120 | self.fail("SBTarget.GetDescription() failed") |
Johnny Chen | 90256cd | 2011-04-23 00:13:34 +0000 | [diff] [blame] | 121 | self.expect(desc, exe=False, |
Johnny Chen | 787f71f | 2011-04-22 23:20:17 +0000 | [diff] [blame] | 122 | substrs = ['a.out']) |
Johnny Chen | 90256cd | 2011-04-23 00:13:34 +0000 | [diff] [blame] | 123 | self.expect(desc, exe=False, matching=False, |
Johnny Chen | 787f71f | 2011-04-22 23:20:17 +0000 | [diff] [blame] | 124 | substrs = ['Target', 'Module', 'Breakpoint']) |
| 125 | |
Johnny Chen | 90256cd | 2011-04-23 00:13:34 +0000 | [diff] [blame] | 126 | desc = get_description(target, option=lldb.eDescriptionLevelFull) |
| 127 | if not desc: |
Johnny Chen | 787f71f | 2011-04-22 23:20:17 +0000 | [diff] [blame] | 128 | self.fail("SBTarget.GetDescription() failed") |
Johnny Chen | 90256cd | 2011-04-23 00:13:34 +0000 | [diff] [blame] | 129 | self.expect(desc, exe=False, |
Johnny Chen | 787f71f | 2011-04-22 23:20:17 +0000 | [diff] [blame] | 130 | substrs = ['a.out', 'Target', 'Module', 'Breakpoint']) |
| 131 | |
| 132 | |
Johnny Chen | 75625aa | 2011-03-07 22:29:04 +0000 | [diff] [blame] | 133 | def launch_new_process_and_redirect_stdout(self): |
| 134 | """Exercise SBTaget.Launch() API with redirected stdout.""" |
| 135 | exe = os.path.join(os.getcwd(), "a.out") |
| 136 | |
| 137 | # Create a target by the debugger. |
| 138 | target = self.dbg.CreateTarget(exe) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 139 | self.assertTrue(target, VALID_TARGET) |
Johnny Chen | 75625aa | 2011-03-07 22:29:04 +0000 | [diff] [blame] | 140 | |
Johnny Chen | d648135 | 2011-03-07 22:46:30 +0000 | [diff] [blame] | 141 | # Add an extra twist of stopping the inferior in a breakpoint, and then continue till it's done. |
| 142 | # We should still see the entire stdout redirected once the process is finished. |
| 143 | line = line_number('main.c', '// a(3) -> c(3)') |
| 144 | breakpoint = target.BreakpointCreateByLocation('main.c', line) |
| 145 | |
Johnny Chen | 75625aa | 2011-03-07 22:29:04 +0000 | [diff] [blame] | 146 | # Now launch the process, do not stop at entry point, and redirect stdout to "stdout.txt" file. |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 147 | # The inferior should run to completion after "process.Continue()" call. |
Johnny Chen | 75625aa | 2011-03-07 22:29:04 +0000 | [diff] [blame] | 148 | error = lldb.SBError() |
| 149 | process = target.Launch (self.dbg.GetListener(), None, None, None, "stdout.txt", None, None, 0, False, error) |
Johnny Chen | d648135 | 2011-03-07 22:46:30 +0000 | [diff] [blame] | 150 | process.Continue() |
| 151 | #self.runCmd("process status") |
Johnny Chen | 75625aa | 2011-03-07 22:29:04 +0000 | [diff] [blame] | 152 | |
| 153 | # The 'stdout.txt' file should now exist. |
| 154 | self.assertTrue(os.path.isfile("stdout.txt"), |
| 155 | "'stdout.txt' exists due to redirected stdout via SBTarget.Launch() API.") |
| 156 | |
| 157 | # Read the output file produced by running the program. |
| 158 | with open('stdout.txt', 'r') as f: |
| 159 | output = f.read() |
| 160 | |
Johnny Chen | d648135 | 2011-03-07 22:46:30 +0000 | [diff] [blame] | 161 | # Let's delete the 'stdout.txt' file as a cleanup step. |
| 162 | try: |
| 163 | os.remove("stdout.txt") |
| 164 | pass |
| 165 | except OSError: |
| 166 | pass |
| 167 | |
Johnny Chen | 75625aa | 2011-03-07 22:29:04 +0000 | [diff] [blame] | 168 | self.expect(output, exe=False, |
| 169 | substrs = ["a(1)", "b(2)", "a(3)"]) |
| 170 | |
| 171 | |
Johnny Chen | 05178f6 | 2011-03-04 23:40:06 +0000 | [diff] [blame] | 172 | def resolve_symbol_context_with_address(self): |
| 173 | """Exercise SBTaget.ResolveSymbolContextForAddress() API.""" |
Johnny Chen | d61816b | 2011-03-03 01:41:57 +0000 | [diff] [blame] | 174 | exe = os.path.join(os.getcwd(), "a.out") |
| 175 | |
| 176 | # Create a target by the debugger. |
| 177 | target = self.dbg.CreateTarget(exe) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 178 | self.assertTrue(target, VALID_TARGET) |
Johnny Chen | d61816b | 2011-03-03 01:41:57 +0000 | [diff] [blame] | 179 | |
| 180 | # Now create the two breakpoints inside function 'a'. |
| 181 | breakpoint1 = target.BreakpointCreateByLocation('main.c', self.line1) |
| 182 | breakpoint2 = target.BreakpointCreateByLocation('main.c', self.line2) |
Johnny Chen | ed40198 | 2011-03-03 19:14:00 +0000 | [diff] [blame] | 183 | #print "breakpoint1:", breakpoint1 |
| 184 | #print "breakpoint2:", breakpoint2 |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 185 | self.assertTrue(breakpoint1 and |
Johnny Chen | d61816b | 2011-03-03 01:41:57 +0000 | [diff] [blame] | 186 | breakpoint1.GetNumLocations() == 1, |
| 187 | VALID_BREAKPOINT) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 188 | self.assertTrue(breakpoint2 and |
Johnny Chen | d61816b | 2011-03-03 01:41:57 +0000 | [diff] [blame] | 189 | breakpoint2.GetNumLocations() == 1, |
| 190 | VALID_BREAKPOINT) |
| 191 | |
| 192 | # Now launch the process, and do not stop at entry point. |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 193 | process = target.LaunchSimple(None, None, os.getcwd()) |
| 194 | self.assertTrue(process, PROCESS_IS_VALID) |
Johnny Chen | d61816b | 2011-03-03 01:41:57 +0000 | [diff] [blame] | 195 | |
| 196 | # Frame #0 should be on self.line1. |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 197 | self.assertTrue(process.GetState() == lldb.eStateStopped) |
| 198 | thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint) |
Johnny Chen | ed40198 | 2011-03-03 19:14:00 +0000 | [diff] [blame] | 199 | self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint condition") |
| 200 | #self.runCmd("process status") |
Johnny Chen | d61816b | 2011-03-03 01:41:57 +0000 | [diff] [blame] | 201 | frame0 = thread.GetFrameAtIndex(0) |
| 202 | lineEntry = frame0.GetLineEntry() |
| 203 | self.assertTrue(lineEntry.GetLine() == self.line1) |
| 204 | |
| 205 | address1 = lineEntry.GetStartAddress() |
| 206 | |
| 207 | # Continue the inferior, the breakpoint 2 should be hit. |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 208 | process.Continue() |
| 209 | self.assertTrue(process.GetState() == lldb.eStateStopped) |
| 210 | thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint) |
Johnny Chen | ed40198 | 2011-03-03 19:14:00 +0000 | [diff] [blame] | 211 | self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint condition") |
| 212 | #self.runCmd("process status") |
Johnny Chen | d61816b | 2011-03-03 01:41:57 +0000 | [diff] [blame] | 213 | frame0 = thread.GetFrameAtIndex(0) |
| 214 | lineEntry = frame0.GetLineEntry() |
| 215 | self.assertTrue(lineEntry.GetLine() == self.line2) |
| 216 | |
| 217 | address2 = lineEntry.GetStartAddress() |
| 218 | |
Johnny Chen | ed40198 | 2011-03-03 19:14:00 +0000 | [diff] [blame] | 219 | #print "address1:", address1 |
| 220 | #print "address2:", address2 |
Johnny Chen | d61816b | 2011-03-03 01:41:57 +0000 | [diff] [blame] | 221 | |
| 222 | # Now call SBTarget.ResolveSymbolContextForAddress() with the addresses from our line entry. |
| 223 | context1 = target.ResolveSymbolContextForAddress(address1, lldb.eSymbolContextEverything) |
| 224 | context2 = target.ResolveSymbolContextForAddress(address2, lldb.eSymbolContextEverything) |
| 225 | |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 226 | self.assertTrue(context1 and context2) |
Johnny Chen | ed40198 | 2011-03-03 19:14:00 +0000 | [diff] [blame] | 227 | #print "context1:", context1 |
| 228 | #print "context2:", context2 |
Johnny Chen | d61816b | 2011-03-03 01:41:57 +0000 | [diff] [blame] | 229 | |
| 230 | # Verify that the context point to the same function 'a'. |
| 231 | symbol1 = context1.GetSymbol() |
| 232 | symbol2 = context2.GetSymbol() |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 233 | self.assertTrue(symbol1 and symbol2) |
Johnny Chen | ed40198 | 2011-03-03 19:14:00 +0000 | [diff] [blame] | 234 | #print "symbol1:", symbol1 |
| 235 | #print "symbol2:", symbol2 |
Johnny Chen | d61816b | 2011-03-03 01:41:57 +0000 | [diff] [blame] | 236 | |
Johnny Chen | 9ae9820 | 2011-04-23 00:34:56 +0000 | [diff] [blame] | 237 | from lldbutil import get_description |
| 238 | desc1 = get_description(symbol1) |
| 239 | desc2 = get_description(symbol2) |
| 240 | self.assertTrue(desc1 and desc2 and desc1 == desc2, |
| 241 | "The two addresses should resolve to the same symbol") |
Johnny Chen | d61816b | 2011-03-03 01:41:57 +0000 | [diff] [blame] | 242 | |
| 243 | |
| 244 | if __name__ == '__main__': |
| 245 | import atexit |
| 246 | lldb.SBDebugger.Initialize() |
| 247 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
| 248 | unittest2.main() |