blob: 12ff77853a554389e97bcade285fb873d32fc6d8 [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,
Greg Clayton7260f622011-04-18 08:33:37 +000057 substrs = ['stopped',
Johnny Chen3df7f942010-11-18 20:35:54 +000058 '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 Chen822198e2011-04-19 18:23:28 +000083 self.process = target.LaunchSimple(None, None, os.getcwd())
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:
Johnny Chende90f1d2011-04-27 17:43:07 +000091 from lldbutil import stop_reason_to_str
Johnny Chen835a88c2010-11-18 23:33:43 +000092 self.fail(STOPPED_DUE_TO_BREAKPOINT_WITH_STOP_REASON_AS %
Johnny Chende90f1d2011-04-27 17:43:07 +000093 stop_reason_to_str(thread.GetStopReason()))
Johnny Chen835a88c2010-11-18 23:33:43 +000094
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 Chene69c7482011-04-28 22:57:01 +0000104 for val in valList:
Johnny Chen835a88c2010-11-18 23:33:43 +0000105 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 Chena9940702010-11-29 23:58:04 +0000112 # On Mac OS X, gcc 4.2 emits the wrong debug info for A::g_points.
Johnny Chen835a88c2010-11-18 23:33:43 +0000113 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 Chen94f928b2010-12-14 18:59:15 +0000121 # SBFrame.FindValue() should also work.
122 val = frame.FindValue("A::g_points", lldb.eValueTypeVariableGlobal)
Johnny Chenbeae5232010-11-19 18:07:14 +0000123 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 Chen94f928b2010-12-14 18:59:15 +0000127 val = frame.FindValue("argc", lldb.eValueTypeVariableArgument)
Johnny Chenbeae5232010-11-19 18:07:14 +0000128 self.DebugSBValue(frame, val)
129 self.assertTrue(val.GetName() == 'argc')
130
Johnny Chen94f928b2010-12-14 18:59:15 +0000131 val = frame.FindValue("argv", lldb.eValueTypeVariableArgument)
Johnny Chenbeae5232010-11-19 18:07:14 +0000132 self.DebugSBValue(frame, val)
133 self.assertTrue(val.GetName() == 'argv')
134
Johnny Chen94f928b2010-12-14 18:59:15 +0000135 val = frame.FindValue("hello_world", lldb.eValueTypeVariableLocal)
Johnny Chenbeae5232010-11-19 18:07:14 +0000136 self.DebugSBValue(frame, val)
137 self.assertTrue(val.GetName() == 'hello_world')
Johnny Chen835a88c2010-11-18 23:33:43 +0000138
Johnny Chen3df7f942010-11-18 20:35:54 +0000139
140if __name__ == '__main__':
141 import atexit
142 lldb.SBDebugger.Initialize()
143 atexit.register(lambda: lldb.SBDebugger.Terminate())
144 unittest2.main()