blob: 18415b16595d36def14c788823c051ddc7f1f7e3 [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
Johnny Chenfb8cd372011-06-25 20:43:57 +000012 mydir = os.path.join("lang", "cpp", "class_static")
Johnny Chen3df7f942010-11-18 20:35:54 +000013
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)
Johnny Chen4ebd0192011-05-24 18:22:45 +000077 self.assertTrue(target, VALID_TARGET)
Johnny Chen835a88c2010-11-18 23:33:43 +000078
79 breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line)
Johnny Chen4ebd0192011-05-24 18:22:45 +000080 self.assertTrue(breakpoint, VALID_BREAKPOINT)
Johnny Chen835a88c2010-11-18 23:33:43 +000081
82 # Now launch the process, and do not stop at entry point.
Johnny Chen5a0bee72011-06-15 22:14:12 +000083 process = target.LaunchSimple(None, None, os.getcwd())
84 self.assertTrue(process, PROCESS_IS_VALID)
Johnny Chen835a88c2010-11-18 23:33:43 +000085
86 # The stop reason of the thread should be breakpoint.
Johnny Chen5a0bee72011-06-15 22:14:12 +000087 thread = process.GetThreadAtIndex(0)
Johnny Chen835a88c2010-11-18 23:33:43 +000088 if thread.GetStopReason() != lldb.eStopReasonBreakpoint:
Johnny Chende90f1d2011-04-27 17:43:07 +000089 from lldbutil import stop_reason_to_str
Johnny Chen835a88c2010-11-18 23:33:43 +000090 self.fail(STOPPED_DUE_TO_BREAKPOINT_WITH_STOP_REASON_AS %
Johnny Chende90f1d2011-04-27 17:43:07 +000091 stop_reason_to_str(thread.GetStopReason()))
Johnny Chen835a88c2010-11-18 23:33:43 +000092
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
Johnny Chene69c7482011-04-28 22:57:01 +0000102 for val in valList:
Johnny Chen835a88c2010-11-18 23:33:43 +0000103 self.DebugSBValue(frame, val)
104 self.assertTrue(val.GetValueType() == lldb.eValueTypeVariableGlobal)
105 name = val.GetName()
106 self.assertTrue(name in ['g_points', 'A::g_points'])
107 if name == 'g_points':
108 self.assertTrue(val.GetNumChildren() == 2)
109 elif name == 'A::g_points' and self.getCompiler() in ['clang', 'llvm-gcc']:
Johnny Chena9940702010-11-29 23:58:04 +0000110 # On Mac OS X, gcc 4.2 emits the wrong debug info for A::g_points.
Johnny Chen835a88c2010-11-18 23:33:43 +0000111 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
Johnny Chen94f928b2010-12-14 18:59:15 +0000119 # SBFrame.FindValue() should also work.
120 val = frame.FindValue("A::g_points", lldb.eValueTypeVariableGlobal)
Johnny Chenbeae5232010-11-19 18:07:14 +0000121 self.DebugSBValue(frame, val)
122 self.assertTrue(val.GetName() == 'A::g_points')
123
124 # Also exercise the "parameter" and "local" scopes while we are at it.
Johnny Chen94f928b2010-12-14 18:59:15 +0000125 val = frame.FindValue("argc", lldb.eValueTypeVariableArgument)
Johnny Chenbeae5232010-11-19 18:07:14 +0000126 self.DebugSBValue(frame, val)
127 self.assertTrue(val.GetName() == 'argc')
128
Johnny Chen94f928b2010-12-14 18:59:15 +0000129 val = frame.FindValue("argv", lldb.eValueTypeVariableArgument)
Johnny Chenbeae5232010-11-19 18:07:14 +0000130 self.DebugSBValue(frame, val)
131 self.assertTrue(val.GetName() == 'argv')
132
Johnny Chen94f928b2010-12-14 18:59:15 +0000133 val = frame.FindValue("hello_world", lldb.eValueTypeVariableLocal)
Johnny Chenbeae5232010-11-19 18:07:14 +0000134 self.DebugSBValue(frame, val)
135 self.assertTrue(val.GetName() == 'hello_world')
Johnny Chen835a88c2010-11-18 23:33:43 +0000136
Johnny Chen3df7f942010-11-18 20:35:54 +0000137
138if __name__ == '__main__':
139 import atexit
140 lldb.SBDebugger.Initialize()
141 atexit.register(lambda: lldb.SBDebugger.Terminate())
142 unittest2.main()