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