blob: cfcfaa96f7eba11d80a2276ab3dae58b94701dfa [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")
26 def test_with_dsym_and_python_api(self):
27 """Test Python APIs on file and class static variables."""
28 self.buildDsym()
29 self.static_variable_python()
30
31 def test_with_dwarf_and_python_api(self):
32 """Test Python APIs on file and class static variables."""
33 self.buildDwarf()
34 self.static_variable_python()
35
Johnny Chen3df7f942010-11-18 20:35:54 +000036 def setUp(self):
37 # Call super's setUp().
38 TestBase.setUp(self)
39 # Find the line number to break at.
40 self.line = line_number('main.cpp', '// Set break point at this line.')
41
42 def static_variable_commands(self):
Johnny Chen835a88c2010-11-18 23:33:43 +000043 """Test that that file and class static variables display correctly."""
Johnny Chen3df7f942010-11-18 20:35:54 +000044 self.runCmd("file a.out", CURRENT_EXECUTABLE_SET)
45
46 self.expect("breakpoint set -f main.cpp -l %d" % self.line,
47 BREAKPOINT_CREATED,
48 startstr = "Breakpoint created: 1: file ='main.cpp', line = %d, locations = 1" %
49 self.line)
50
51 self.runCmd("run", RUN_SUCCEEDED)
52
53 # The stop reason of the thread should be breakpoint.
54 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
55 substrs = ['state is stopped',
56 'stop reason = breakpoint'])
57
58 # On Mac OS X, gcc 4.2 emits the wrong debug info for A::g_points.
59 slist = ['(PointType [2]) g_points', 'A::g_points']
60
61 # 'frame variable -G' finds and displays global variable(s) by name.
62 self.expect('frame variable -G g_points', VARIABLES_DISPLAYED_CORRECTLY,
63 substrs = slist)
64
65 # A::g_points is an array of two elements.
66 if sys.platform.startswith("darwin") and self.getCompiler() in ['clang', 'llvm-gcc']:
67 self.expect("frame variable A::g_points[1].x", VARIABLES_DISPLAYED_CORRECTLY,
68 startstr = "(int) A::g_points[1].x = 11")
69
Johnny Chen835a88c2010-11-18 23:33:43 +000070 def static_variable_python(self):
71 """Test Python APIs on file and class static variables."""
72 exe = os.path.join(os.getcwd(), "a.out")
73
74 target = self.dbg.CreateTarget(exe)
75 self.assertTrue(target.IsValid(), VALID_TARGET)
76
77 breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line)
78 self.assertTrue(breakpoint.IsValid(), VALID_BREAKPOINT)
79
80 # Now launch the process, and do not stop at entry point.
81 self.process = target.LaunchProcess([''], [''], os.ctermid(), 0, False)
82
83 self.process = target.GetProcess()
84 self.assertTrue(self.process.IsValid(), PROCESS_IS_VALID)
85
86 # The stop reason of the thread should be breakpoint.
87 thread = self.process.GetThreadAtIndex(0)
88 if thread.GetStopReason() != lldb.eStopReasonBreakpoint:
89 from lldbutil import StopReasonString
90 self.fail(STOPPED_DUE_TO_BREAKPOINT_WITH_STOP_REASON_AS %
91 StopReasonString(thread.GetStopReason()))
92
93 # Get the SBValue of 'A::g_points' and 'g_points'.
94 frame = thread.GetFrameAtIndex(0)
95
96 # arguments => False
97 # locals => False
98 # statics => True
99 # in_scope_only => False
100 valList = frame.GetVariables(False, False, True, False)
101
102 from lldbutil import lldb_iter
103 for val in lldb_iter(valList, 'GetSize', 'GetValueAtIndex'):
104 self.DebugSBValue(frame, val)
105 self.assertTrue(val.GetValueType() == lldb.eValueTypeVariableGlobal)
106 name = val.GetName()
107 self.assertTrue(name in ['g_points', 'A::g_points'])
108 if name == 'g_points':
109 self.assertTrue(val.GetNumChildren() == 2)
110 elif name == 'A::g_points' and self.getCompiler() in ['clang', 'llvm-gcc']:
111 self.assertTrue(val.GetNumChildren() == 2)
112 child1 = val.GetChildAtIndex(1)
113 self.DebugSBValue(frame, child1)
114 child1_x = child1.GetChildAtIndex(0)
115 self.DebugSBValue(frame, child1_x)
116 self.assertTrue(child1_x.GetTypeName() == 'int' and
117 child1_x.GetValue(frame) == '11')
118
119 #variable = frame.LookupVarInScope("A::g_points", "global")
120 #print "variable:", repr(variable)
121
Johnny Chen3df7f942010-11-18 20:35:54 +0000122
123if __name__ == '__main__':
124 import atexit
125 lldb.SBDebugger.Initialize()
126 atexit.register(lambda: lldb.SBDebugger.Terminate())
127 unittest2.main()