Johnny Chen | 3df7f94 | 2010-11-18 20:35:54 +0000 | [diff] [blame] | 1 | """ |
| 2 | Test display and Python APIs on file and class static variables. |
| 3 | """ |
| 4 | |
| 5 | import os, time |
| 6 | import unittest2 |
| 7 | import lldb |
| 8 | from lldbtest import * |
| 9 | |
| 10 | class StaticVariableTestCase(TestBase): |
| 11 | |
| 12 | mydir = "class_static" |
| 13 | |
| 14 | @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") |
| 15 | def test_with_dsym_and_run_command(self): |
| 16 | """Test that file and class static variables display correctly.""" |
| 17 | self.buildDsym() |
| 18 | self.static_variable_commands() |
| 19 | |
| 20 | def test_with_dwarf_and_run_command(self): |
Johnny Chen | 835a88c | 2010-11-18 23:33:43 +0000 | [diff] [blame] | 21 | """Test that file and class static variables display correctly.""" |
Johnny Chen | 3df7f94 | 2010-11-18 20:35:54 +0000 | [diff] [blame] | 22 | self.buildDwarf() |
| 23 | self.static_variable_commands() |
| 24 | |
Johnny Chen | 835a88c | 2010-11-18 23:33:43 +0000 | [diff] [blame] | 25 | @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") |
Johnny Chen | a47d7cb | 2010-12-10 01:21:27 +0000 | [diff] [blame] | 26 | @python_api_test |
Johnny Chen | 835a88c | 2010-11-18 23:33:43 +0000 | [diff] [blame] | 27 | def test_with_dsym_and_python_api(self): |
| 28 | """Test Python APIs on file and class static variables.""" |
| 29 | self.buildDsym() |
| 30 | self.static_variable_python() |
| 31 | |
Johnny Chen | a47d7cb | 2010-12-10 01:21:27 +0000 | [diff] [blame] | 32 | @python_api_test |
Johnny Chen | 835a88c | 2010-11-18 23:33:43 +0000 | [diff] [blame] | 33 | def test_with_dwarf_and_python_api(self): |
| 34 | """Test Python APIs on file and class static variables.""" |
| 35 | self.buildDwarf() |
| 36 | self.static_variable_python() |
| 37 | |
Johnny Chen | 3df7f94 | 2010-11-18 20:35:54 +0000 | [diff] [blame] | 38 | def setUp(self): |
| 39 | # Call super's setUp(). |
| 40 | TestBase.setUp(self) |
| 41 | # Find the line number to break at. |
| 42 | self.line = line_number('main.cpp', '// Set break point at this line.') |
| 43 | |
| 44 | def static_variable_commands(self): |
Johnny Chen | 835a88c | 2010-11-18 23:33:43 +0000 | [diff] [blame] | 45 | """Test that that file and class static variables display correctly.""" |
Johnny Chen | 3df7f94 | 2010-11-18 20:35:54 +0000 | [diff] [blame] | 46 | self.runCmd("file a.out", CURRENT_EXECUTABLE_SET) |
| 47 | |
| 48 | self.expect("breakpoint set -f main.cpp -l %d" % self.line, |
| 49 | BREAKPOINT_CREATED, |
| 50 | startstr = "Breakpoint created: 1: file ='main.cpp', line = %d, locations = 1" % |
| 51 | self.line) |
| 52 | |
| 53 | self.runCmd("run", RUN_SUCCEEDED) |
| 54 | |
| 55 | # The stop reason of the thread should be breakpoint. |
| 56 | self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT, |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 57 | substrs = ['stopped', |
Johnny Chen | 3df7f94 | 2010-11-18 20:35:54 +0000 | [diff] [blame] | 58 | 'stop reason = breakpoint']) |
| 59 | |
| 60 | # On Mac OS X, gcc 4.2 emits the wrong debug info for A::g_points. |
| 61 | slist = ['(PointType [2]) g_points', 'A::g_points'] |
| 62 | |
| 63 | # 'frame variable -G' finds and displays global variable(s) by name. |
| 64 | self.expect('frame variable -G g_points', VARIABLES_DISPLAYED_CORRECTLY, |
| 65 | substrs = slist) |
| 66 | |
| 67 | # A::g_points is an array of two elements. |
| 68 | if sys.platform.startswith("darwin") and self.getCompiler() in ['clang', 'llvm-gcc']: |
| 69 | self.expect("frame variable A::g_points[1].x", VARIABLES_DISPLAYED_CORRECTLY, |
| 70 | startstr = "(int) A::g_points[1].x = 11") |
| 71 | |
Johnny Chen | 835a88c | 2010-11-18 23:33:43 +0000 | [diff] [blame] | 72 | def static_variable_python(self): |
| 73 | """Test Python APIs on file and class static variables.""" |
| 74 | exe = os.path.join(os.getcwd(), "a.out") |
| 75 | |
| 76 | target = self.dbg.CreateTarget(exe) |
| 77 | self.assertTrue(target.IsValid(), VALID_TARGET) |
| 78 | |
| 79 | breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line) |
| 80 | self.assertTrue(breakpoint.IsValid(), VALID_BREAKPOINT) |
| 81 | |
| 82 | # Now launch the process, and do not stop at entry point. |
Johnny Chen | 822198e | 2011-04-19 18:23:28 +0000 | [diff] [blame] | 83 | self.process = target.LaunchSimple(None, None, os.getcwd()) |
Johnny Chen | 835a88c | 2010-11-18 23:33:43 +0000 | [diff] [blame] | 84 | |
| 85 | self.process = target.GetProcess() |
| 86 | self.assertTrue(self.process.IsValid(), PROCESS_IS_VALID) |
| 87 | |
| 88 | # The stop reason of the thread should be breakpoint. |
| 89 | thread = self.process.GetThreadAtIndex(0) |
| 90 | if thread.GetStopReason() != lldb.eStopReasonBreakpoint: |
Johnny Chen | de90f1d | 2011-04-27 17:43:07 +0000 | [diff] [blame] | 91 | from lldbutil import stop_reason_to_str |
Johnny Chen | 835a88c | 2010-11-18 23:33:43 +0000 | [diff] [blame] | 92 | self.fail(STOPPED_DUE_TO_BREAKPOINT_WITH_STOP_REASON_AS % |
Johnny Chen | de90f1d | 2011-04-27 17:43:07 +0000 | [diff] [blame] | 93 | stop_reason_to_str(thread.GetStopReason())) |
Johnny Chen | 835a88c | 2010-11-18 23:33:43 +0000 | [diff] [blame] | 94 | |
| 95 | # Get the SBValue of 'A::g_points' and 'g_points'. |
| 96 | frame = thread.GetFrameAtIndex(0) |
| 97 | |
| 98 | # arguments => False |
| 99 | # locals => False |
| 100 | # statics => True |
| 101 | # in_scope_only => False |
| 102 | valList = frame.GetVariables(False, False, True, False) |
| 103 | |
Johnny Chen | e69c748 | 2011-04-28 22:57:01 +0000 | [diff] [blame^] | 104 | for val in valList: |
Johnny Chen | 835a88c | 2010-11-18 23:33:43 +0000 | [diff] [blame] | 105 | self.DebugSBValue(frame, val) |
| 106 | self.assertTrue(val.GetValueType() == lldb.eValueTypeVariableGlobal) |
| 107 | name = val.GetName() |
| 108 | self.assertTrue(name in ['g_points', 'A::g_points']) |
| 109 | if name == 'g_points': |
| 110 | self.assertTrue(val.GetNumChildren() == 2) |
| 111 | elif name == 'A::g_points' and self.getCompiler() in ['clang', 'llvm-gcc']: |
Johnny Chen | a994070 | 2010-11-29 23:58:04 +0000 | [diff] [blame] | 112 | # On Mac OS X, gcc 4.2 emits the wrong debug info for A::g_points. |
Johnny Chen | 835a88c | 2010-11-18 23:33:43 +0000 | [diff] [blame] | 113 | self.assertTrue(val.GetNumChildren() == 2) |
| 114 | child1 = val.GetChildAtIndex(1) |
| 115 | self.DebugSBValue(frame, child1) |
| 116 | child1_x = child1.GetChildAtIndex(0) |
| 117 | self.DebugSBValue(frame, child1_x) |
| 118 | self.assertTrue(child1_x.GetTypeName() == 'int' and |
| 119 | child1_x.GetValue(frame) == '11') |
| 120 | |
Johnny Chen | 94f928b | 2010-12-14 18:59:15 +0000 | [diff] [blame] | 121 | # SBFrame.FindValue() should also work. |
| 122 | val = frame.FindValue("A::g_points", lldb.eValueTypeVariableGlobal) |
Johnny Chen | beae523 | 2010-11-19 18:07:14 +0000 | [diff] [blame] | 123 | self.DebugSBValue(frame, val) |
| 124 | self.assertTrue(val.GetName() == 'A::g_points') |
| 125 | |
| 126 | # Also exercise the "parameter" and "local" scopes while we are at it. |
Johnny Chen | 94f928b | 2010-12-14 18:59:15 +0000 | [diff] [blame] | 127 | val = frame.FindValue("argc", lldb.eValueTypeVariableArgument) |
Johnny Chen | beae523 | 2010-11-19 18:07:14 +0000 | [diff] [blame] | 128 | self.DebugSBValue(frame, val) |
| 129 | self.assertTrue(val.GetName() == 'argc') |
| 130 | |
Johnny Chen | 94f928b | 2010-12-14 18:59:15 +0000 | [diff] [blame] | 131 | val = frame.FindValue("argv", lldb.eValueTypeVariableArgument) |
Johnny Chen | beae523 | 2010-11-19 18:07:14 +0000 | [diff] [blame] | 132 | self.DebugSBValue(frame, val) |
| 133 | self.assertTrue(val.GetName() == 'argv') |
| 134 | |
Johnny Chen | 94f928b | 2010-12-14 18:59:15 +0000 | [diff] [blame] | 135 | val = frame.FindValue("hello_world", lldb.eValueTypeVariableLocal) |
Johnny Chen | beae523 | 2010-11-19 18:07:14 +0000 | [diff] [blame] | 136 | self.DebugSBValue(frame, val) |
| 137 | self.assertTrue(val.GetName() == 'hello_world') |
Johnny Chen | 835a88c | 2010-11-18 23:33:43 +0000 | [diff] [blame] | 138 | |
Johnny Chen | 3df7f94 | 2010-11-18 20:35:54 +0000 | [diff] [blame] | 139 | |
| 140 | if __name__ == '__main__': |
| 141 | import atexit |
| 142 | lldb.SBDebugger.Initialize() |
| 143 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
| 144 | unittest2.main() |