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