blob: 428e127e4d94118f29eb3ca3bff6707beae5509b [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.
Johnny Chen025d1b82010-12-08 01:25:21 +000083 self.process = target.LaunchProcess([], [], os.ctermid(), 0, False)
Johnny Chen835a88c2010-11-18 23:33:43 +000084
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:
91 from lldbutil import StopReasonString
92 self.fail(STOPPED_DUE_TO_BREAKPOINT_WITH_STOP_REASON_AS %
93 StopReasonString(thread.GetStopReason()))
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
104 from lldbutil import lldb_iter
105 for val in lldb_iter(valList, 'GetSize', 'GetValueAtIndex'):
106 self.DebugSBValue(frame, val)
107 self.assertTrue(val.GetValueType() == lldb.eValueTypeVariableGlobal)
108 name = val.GetName()
109 self.assertTrue(name in ['g_points', 'A::g_points'])
110 if name == 'g_points':
111 self.assertTrue(val.GetNumChildren() == 2)
112 elif name == 'A::g_points' and self.getCompiler() in ['clang', 'llvm-gcc']:
Johnny Chena9940702010-11-29 23:58:04 +0000113 # On Mac OS X, gcc 4.2 emits the wrong debug info for A::g_points.
Johnny Chen835a88c2010-11-18 23:33:43 +0000114 self.assertTrue(val.GetNumChildren() == 2)
115 child1 = val.GetChildAtIndex(1)
116 self.DebugSBValue(frame, child1)
117 child1_x = child1.GetChildAtIndex(0)
118 self.DebugSBValue(frame, child1_x)
119 self.assertTrue(child1_x.GetTypeName() == 'int' and
120 child1_x.GetValue(frame) == '11')
121
Johnny Chenbeae5232010-11-19 18:07:14 +0000122 # SBFrame.LookupVarInScope() should also work.
123 val = frame.LookupVarInScope("A::g_points", "global")
124 self.DebugSBValue(frame, val)
125 self.assertTrue(val.GetName() == 'A::g_points')
126
127 # Also exercise the "parameter" and "local" scopes while we are at it.
128 val = frame.LookupVarInScope("argc", "parameter")
129 self.DebugSBValue(frame, val)
130 self.assertTrue(val.GetName() == 'argc')
131
132 val = frame.LookupVarInScope("argv", "parameter")
133 self.DebugSBValue(frame, val)
134 self.assertTrue(val.GetName() == 'argv')
135
136 val = frame.LookupVarInScope("hello_world", "local")
137 self.DebugSBValue(frame, val)
138 self.assertTrue(val.GetName() == 'hello_world')
Johnny Chen835a88c2010-11-18 23:33:43 +0000139
Johnny Chen3df7f942010-11-18 20:35:54 +0000140
141if __name__ == '__main__':
142 import atexit
143 lldb.SBDebugger.Initialize()
144 atexit.register(lambda: lldb.SBDebugger.Terminate())
145 unittest2.main()