blob: fad69a2d13c5bfb272ce0291ddfbe93004f313c2 [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 *
Jim Ingham63dfc722012-09-22 00:05:11 +00009import lldbutil
Johnny Chen3df7f942010-11-18 20:35:54 +000010
11class StaticVariableTestCase(TestBase):
12
Greg Clayton4570d3e2013-12-10 23:19:29 +000013 mydir = TestBase.compute_mydir(__file__)
Matt Kopec5d4bc2a2013-08-01 17:22:24 +000014 failing_compilers = ['clang', 'gcc']
Johnny Chen3df7f942010-11-18 20:35:54 +000015
16 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
Johnny Chen24086bc2012-04-06 19:54:10 +000017 @dsym_test
Johnny Chen3df7f942010-11-18 20:35:54 +000018 def test_with_dsym_and_run_command(self):
19 """Test that file and class static variables display correctly."""
20 self.buildDsym()
21 self.static_variable_commands()
22
Johnny Chen24086bc2012-04-06 19:54:10 +000023 @dwarf_test
Johnny Chen3df7f942010-11-18 20:35:54 +000024 def test_with_dwarf_and_run_command(self):
Johnny Chen835a88c2010-11-18 23:33:43 +000025 """Test that file and class static variables display correctly."""
Johnny Chen3df7f942010-11-18 20:35:54 +000026 self.buildDwarf()
27 self.static_variable_commands()
28
Johnny Chen835a88c2010-11-18 23:33:43 +000029 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
Greg Clayton609a4332013-05-14 23:21:47 +000030 @expectedFailureClang(9980907)
Enrico Granata43f62132013-02-23 01:28:30 +000031 @expectedFailureGcc(9980907)
Johnny Chena47d7cb2010-12-10 01:21:27 +000032 @python_api_test
Johnny Chen24086bc2012-04-06 19:54:10 +000033 @dsym_test
Johnny Chen835a88c2010-11-18 23:33:43 +000034 def test_with_dsym_and_python_api(self):
35 """Test Python APIs on file and class static variables."""
36 self.buildDsym()
37 self.static_variable_python()
38
Ed Masteec4f47e2014-04-22 13:42:05 +000039 @expectedFailureDarwin(9980907)
Siva Chandra5ab2e072014-12-19 22:40:05 +000040 @expectedFailureClang('Clang emits incomplete debug info.')
41 @expectedFailureGcc('GCC emits incomplete debug info.')
Johnny Chena47d7cb2010-12-10 01:21:27 +000042 @python_api_test
Johnny Chen24086bc2012-04-06 19:54:10 +000043 @dwarf_test
Johnny Chen835a88c2010-11-18 23:33:43 +000044 def test_with_dwarf_and_python_api(self):
45 """Test Python APIs on file and class static variables."""
46 self.buildDwarf()
47 self.static_variable_python()
48
Johnny Chen3df7f942010-11-18 20:35:54 +000049 def setUp(self):
50 # Call super's setUp().
51 TestBase.setUp(self)
52 # Find the line number to break at.
53 self.line = line_number('main.cpp', '// Set break point at this line.')
54
55 def static_variable_commands(self):
Johnny Chen835a88c2010-11-18 23:33:43 +000056 """Test that that file and class static variables display correctly."""
Johnny Chen3df7f942010-11-18 20:35:54 +000057 self.runCmd("file a.out", CURRENT_EXECUTABLE_SET)
58
Jim Ingham63dfc722012-09-22 00:05:11 +000059 lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.line, num_expected_locations=1, loc_exact=True)
Johnny Chen3df7f942010-11-18 20:35:54 +000060
61 self.runCmd("run", RUN_SUCCEEDED)
62
63 # The stop reason of the thread should be breakpoint.
64 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
Greg Clayton7260f622011-04-18 08:33:37 +000065 substrs = ['stopped',
Johnny Chen3df7f942010-11-18 20:35:54 +000066 'stop reason = breakpoint'])
67
Johnny Chen1dc9a202011-08-22 17:58:14 +000068 # global variables are no longer displayed with the "frame variable" command.
69 self.expect('target variable A::g_points', VARIABLES_DISPLAYED_CORRECTLY,
Siva Chandra89ce9552015-01-05 23:06:14 +000070 patterns=['\(PointType \[[1-9]*\]\) A::g_points = {.*}'])
Johnny Chen1dc9a202011-08-22 17:58:14 +000071 self.expect('target variable g_points', VARIABLES_DISPLAYED_CORRECTLY,
72 substrs = ['(PointType [2]) g_points'])
73
Johnny Chen3df7f942010-11-18 20:35:54 +000074 # On Mac OS X, gcc 4.2 emits the wrong debug info for A::g_points.
Johnny Chen3df7f942010-11-18 20:35:54 +000075 # A::g_points is an array of two elements.
Siva Chandra89ce9552015-01-05 23:06:14 +000076 if sys.platform.startswith("darwin") or sys.platform.startswith("linux"):
Johnny Chen1dc9a202011-08-22 17:58:14 +000077 self.expect("target variable A::g_points[1].x", VARIABLES_DISPLAYED_CORRECTLY,
Johnny Chen3df7f942010-11-18 20:35:54 +000078 startstr = "(int) A::g_points[1].x = 11")
79
Johnny Chen835a88c2010-11-18 23:33:43 +000080 def static_variable_python(self):
81 """Test Python APIs on file and class static variables."""
82 exe = os.path.join(os.getcwd(), "a.out")
83
84 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +000085 self.assertTrue(target, VALID_TARGET)
Johnny Chen835a88c2010-11-18 23:33:43 +000086
87 breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line)
Johnny Chen4ebd0192011-05-24 18:22:45 +000088 self.assertTrue(breakpoint, VALID_BREAKPOINT)
Johnny Chen835a88c2010-11-18 23:33:43 +000089
90 # Now launch the process, and do not stop at entry point.
Greg Claytonc6947512013-12-13 19:18:59 +000091 process = target.LaunchSimple (None, None, self.get_process_working_directory())
Johnny Chen5a0bee72011-06-15 22:14:12 +000092 self.assertTrue(process, PROCESS_IS_VALID)
Johnny Chen835a88c2010-11-18 23:33:43 +000093
94 # The stop reason of the thread should be breakpoint.
Johnny Chen5a0bee72011-06-15 22:14:12 +000095 thread = process.GetThreadAtIndex(0)
Johnny Chen835a88c2010-11-18 23:33:43 +000096 if thread.GetStopReason() != lldb.eStopReasonBreakpoint:
Johnny Chende90f1d2011-04-27 17:43:07 +000097 from lldbutil import stop_reason_to_str
Johnny Chen835a88c2010-11-18 23:33:43 +000098 self.fail(STOPPED_DUE_TO_BREAKPOINT_WITH_STOP_REASON_AS %
Johnny Chende90f1d2011-04-27 17:43:07 +000099 stop_reason_to_str(thread.GetStopReason()))
Johnny Chen835a88c2010-11-18 23:33:43 +0000100
101 # Get the SBValue of 'A::g_points' and 'g_points'.
102 frame = thread.GetFrameAtIndex(0)
103
104 # arguments => False
105 # locals => False
106 # statics => True
107 # in_scope_only => False
108 valList = frame.GetVariables(False, False, True, False)
109
Johnny Chene69c7482011-04-28 22:57:01 +0000110 for val in valList:
Johnny Chen9a07aba2011-07-11 20:06:28 +0000111 self.DebugSBValue(val)
Johnny Chen835a88c2010-11-18 23:33:43 +0000112 name = val.GetName()
113 self.assertTrue(name in ['g_points', 'A::g_points'])
114 if name == 'g_points':
Greg Claytonbc8d2392013-05-14 22:17:29 +0000115 self.assertTrue(val.GetValueType() == lldb.eValueTypeVariableStatic)
Johnny Chen835a88c2010-11-18 23:33:43 +0000116 self.assertTrue(val.GetNumChildren() == 2)
Siva Chandra5ab2e072014-12-19 22:40:05 +0000117 elif name == 'A::g_points':
Greg Claytonbc8d2392013-05-14 22:17:29 +0000118 self.assertTrue(val.GetValueType() == lldb.eValueTypeVariableGlobal)
Johnny Chen835a88c2010-11-18 23:33:43 +0000119 self.assertTrue(val.GetNumChildren() == 2)
120 child1 = val.GetChildAtIndex(1)
Johnny Chen9a07aba2011-07-11 20:06:28 +0000121 self.DebugSBValue(child1)
Johnny Chen835a88c2010-11-18 23:33:43 +0000122 child1_x = child1.GetChildAtIndex(0)
Johnny Chen9a07aba2011-07-11 20:06:28 +0000123 self.DebugSBValue(child1_x)
Johnny Chen835a88c2010-11-18 23:33:43 +0000124 self.assertTrue(child1_x.GetTypeName() == 'int' and
Greg Claytonfe42ac42011-08-03 22:57:10 +0000125 child1_x.GetValue() == '11')
Johnny Chen835a88c2010-11-18 23:33:43 +0000126
Johnny Chen94f928b2010-12-14 18:59:15 +0000127 # SBFrame.FindValue() should also work.
128 val = frame.FindValue("A::g_points", lldb.eValueTypeVariableGlobal)
Johnny Chen9a07aba2011-07-11 20:06:28 +0000129 self.DebugSBValue(val)
Johnny Chenbeae5232010-11-19 18:07:14 +0000130 self.assertTrue(val.GetName() == 'A::g_points')
131
132 # Also exercise the "parameter" and "local" scopes while we are at it.
Johnny Chen94f928b2010-12-14 18:59:15 +0000133 val = frame.FindValue("argc", lldb.eValueTypeVariableArgument)
Johnny Chen9a07aba2011-07-11 20:06:28 +0000134 self.DebugSBValue(val)
Johnny Chenbeae5232010-11-19 18:07:14 +0000135 self.assertTrue(val.GetName() == 'argc')
136
Johnny Chen94f928b2010-12-14 18:59:15 +0000137 val = frame.FindValue("argv", lldb.eValueTypeVariableArgument)
Johnny Chen9a07aba2011-07-11 20:06:28 +0000138 self.DebugSBValue(val)
Johnny Chenbeae5232010-11-19 18:07:14 +0000139 self.assertTrue(val.GetName() == 'argv')
140
Johnny Chen94f928b2010-12-14 18:59:15 +0000141 val = frame.FindValue("hello_world", lldb.eValueTypeVariableLocal)
Johnny Chen9a07aba2011-07-11 20:06:28 +0000142 self.DebugSBValue(val)
Johnny Chenbeae5232010-11-19 18:07:14 +0000143 self.assertTrue(val.GetName() == 'hello_world')
Johnny Chen835a88c2010-11-18 23:33:43 +0000144
Johnny Chen3df7f942010-11-18 20:35:54 +0000145
146if __name__ == '__main__':
147 import atexit
148 lldb.SBDebugger.Initialize()
149 atexit.register(lambda: lldb.SBDebugger.Terminate())
150 unittest2.main()