blob: 4e412ec4cbb2311f86bf8e60b34d272b01a4e563 [file] [log] [blame]
Johnny Chen3df7f942010-11-18 20:35:54 +00001"""
2Test display and Python APIs on file and class static variables.
3"""
4
5import os, time
6import unittest2
7import lldb
8from lldbtest import *
9
10class 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 Chen835a88c2010-11-18 23:33:43 +000021 """Test that file and class static variables display correctly."""
Johnny Chen3df7f942010-11-18 20:35:54 +000022 self.buildDwarf()
23 self.static_variable_commands()
24
Johnny Chen835a88c2010-11-18 23:33:43 +000025 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
Johnny Chena47d7cb2010-12-10 01:21:27 +000026 @python_api_test
Johnny Chen835a88c2010-11-18 23:33:43 +000027 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 Chena47d7cb2010-12-10 01:21:27 +000032 @python_api_test
Johnny Chen835a88c2010-11-18 23:33:43 +000033 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 Chen3df7f942010-11-18 20:35:54 +000038 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 Chen835a88c2010-11-18 23:33:43 +000045 """Test that that file and class static variables display correctly."""
Johnny Chen3df7f942010-11-18 20:35:54 +000046 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,
57 substrs = ['state is stopped',
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 Chen835a88c2010-11-18 23:33:43 +000072 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.
Greg Clayton6f907e62011-01-23 17:46:22 +000083 error = lldb.SBError()
Johnny Chend762ff12011-02-03 23:15:53 +000084 self.process = target.Launch (self.dbg.GetListener(), None, None, os.ctermid(), os.ctermid(), os.ctermid(), None, 0, False, error)
Johnny Chen835a88c2010-11-18 23:33:43 +000085
86 self.process = target.GetProcess()
87 self.assertTrue(self.process.IsValid(), PROCESS_IS_VALID)
88
89 # The stop reason of the thread should be breakpoint.
90 thread = self.process.GetThreadAtIndex(0)
91 if thread.GetStopReason() != lldb.eStopReasonBreakpoint:
92 from lldbutil import StopReasonString
93 self.fail(STOPPED_DUE_TO_BREAKPOINT_WITH_STOP_REASON_AS %
94 StopReasonString(thread.GetStopReason()))
95
96 # Get the SBValue of 'A::g_points' and 'g_points'.
97 frame = thread.GetFrameAtIndex(0)
98
99 # arguments => False
100 # locals => False
101 # statics => True
102 # in_scope_only => False
103 valList = frame.GetVariables(False, False, True, False)
104
105 from lldbutil import lldb_iter
106 for val in lldb_iter(valList, 'GetSize', 'GetValueAtIndex'):
107 self.DebugSBValue(frame, val)
108 self.assertTrue(val.GetValueType() == lldb.eValueTypeVariableGlobal)
109 name = val.GetName()
110 self.assertTrue(name in ['g_points', 'A::g_points'])
111 if name == 'g_points':
112 self.assertTrue(val.GetNumChildren() == 2)
113 elif name == 'A::g_points' and self.getCompiler() in ['clang', 'llvm-gcc']:
Johnny Chena9940702010-11-29 23:58:04 +0000114 # On Mac OS X, gcc 4.2 emits the wrong debug info for A::g_points.
Johnny Chen835a88c2010-11-18 23:33:43 +0000115 self.assertTrue(val.GetNumChildren() == 2)
116 child1 = val.GetChildAtIndex(1)
117 self.DebugSBValue(frame, child1)
118 child1_x = child1.GetChildAtIndex(0)
119 self.DebugSBValue(frame, child1_x)
120 self.assertTrue(child1_x.GetTypeName() == 'int' and
121 child1_x.GetValue(frame) == '11')
122
Johnny Chen94f928b2010-12-14 18:59:15 +0000123 # SBFrame.FindValue() should also work.
124 val = frame.FindValue("A::g_points", lldb.eValueTypeVariableGlobal)
Johnny Chenbeae5232010-11-19 18:07:14 +0000125 self.DebugSBValue(frame, val)
126 self.assertTrue(val.GetName() == 'A::g_points')
127
128 # Also exercise the "parameter" and "local" scopes while we are at it.
Johnny Chen94f928b2010-12-14 18:59:15 +0000129 val = frame.FindValue("argc", lldb.eValueTypeVariableArgument)
Johnny Chenbeae5232010-11-19 18:07:14 +0000130 self.DebugSBValue(frame, val)
131 self.assertTrue(val.GetName() == 'argc')
132
Johnny Chen94f928b2010-12-14 18:59:15 +0000133 val = frame.FindValue("argv", lldb.eValueTypeVariableArgument)
Johnny Chenbeae5232010-11-19 18:07:14 +0000134 self.DebugSBValue(frame, val)
135 self.assertTrue(val.GetName() == 'argv')
136
Johnny Chen94f928b2010-12-14 18:59:15 +0000137 val = frame.FindValue("hello_world", lldb.eValueTypeVariableLocal)
Johnny Chenbeae5232010-11-19 18:07:14 +0000138 self.DebugSBValue(frame, val)
139 self.assertTrue(val.GetName() == 'hello_world')
Johnny Chen835a88c2010-11-18 23:33:43 +0000140
Johnny Chen3df7f942010-11-18 20:35:54 +0000141
142if __name__ == '__main__':
143 import atexit
144 lldb.SBDebugger.Initialize()
145 atexit.register(lambda: lldb.SBDebugger.Terminate())
146 unittest2.main()