blob: 88aeb72a7991de0d81fff258df53b1fb7c56b6dd [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__)
Johnny Chen835a88c2010-11-18 23:33:43 +000014
Johnny Chen3df7f942010-11-18 20:35:54 +000015 def setUp(self):
16 # Call super's setUp().
17 TestBase.setUp(self)
18 # Find the line number to break at.
19 self.line = line_number('main.cpp', '// Set break point at this line.')
20
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000021 @expectedFailureWindows("llvm.org/pr24764")
22 def test_with_run_command(self):
23 """Test that file and class static variables display correctly."""
24 self.build()
Johnny Chen3df7f942010-11-18 20:35:54 +000025 self.runCmd("file a.out", CURRENT_EXECUTABLE_SET)
26
Jim Ingham63dfc722012-09-22 00:05:11 +000027 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 +000028
Sean Callanan05834cd2015-07-01 23:56:30 +000029 self.runCmd("run", RUN_SUCCEEDED)
Johnny Chen3df7f942010-11-18 20:35:54 +000030
31 # The stop reason of the thread should be breakpoint.
32 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
Greg Clayton7260f622011-04-18 08:33:37 +000033 substrs = ['stopped',
Johnny Chen3df7f942010-11-18 20:35:54 +000034 'stop reason = breakpoint'])
35
Johnny Chen1dc9a202011-08-22 17:58:14 +000036 # global variables are no longer displayed with the "frame variable" command.
37 self.expect('target variable A::g_points', VARIABLES_DISPLAYED_CORRECTLY,
Siva Chandra89ce9552015-01-05 23:06:14 +000038 patterns=['\(PointType \[[1-9]*\]\) A::g_points = {.*}'])
Johnny Chen1dc9a202011-08-22 17:58:14 +000039 self.expect('target variable g_points', VARIABLES_DISPLAYED_CORRECTLY,
40 substrs = ['(PointType [2]) g_points'])
41
Johnny Chen3df7f942010-11-18 20:35:54 +000042 # On Mac OS X, gcc 4.2 emits the wrong debug info for A::g_points.
Johnny Chen3df7f942010-11-18 20:35:54 +000043 # A::g_points is an array of two elements.
Greg Claytone0d0a762015-04-02 18:24:03 +000044 if self.platformIsDarwin() or self.getPlatform() == "linux":
Johnny Chen1dc9a202011-08-22 17:58:14 +000045 self.expect("target variable A::g_points[1].x", VARIABLES_DISPLAYED_CORRECTLY,
Johnny Chen3df7f942010-11-18 20:35:54 +000046 startstr = "(int) A::g_points[1].x = 11")
47
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000048 @expectedFailureDarwin(9980907)
49 @expectedFailureClang('Clang emits incomplete debug info.')
50 @expectedFailureFreeBSD('llvm.org/pr20550 failing on FreeBSD-11')
51 @expectedFailureGcc('GCC emits incomplete debug info.')
52 @python_api_test
53 def test_with_python_api(self):
Johnny Chen835a88c2010-11-18 23:33:43 +000054 """Test Python APIs on file and class static variables."""
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000055 self.build()
Johnny Chen835a88c2010-11-18 23:33:43 +000056 exe = os.path.join(os.getcwd(), "a.out")
57
58 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +000059 self.assertTrue(target, VALID_TARGET)
Johnny Chen835a88c2010-11-18 23:33:43 +000060
61 breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line)
Johnny Chen4ebd0192011-05-24 18:22:45 +000062 self.assertTrue(breakpoint, VALID_BREAKPOINT)
Johnny Chen835a88c2010-11-18 23:33:43 +000063
64 # Now launch the process, and do not stop at entry point.
Greg Claytonc6947512013-12-13 19:18:59 +000065 process = target.LaunchSimple (None, None, self.get_process_working_directory())
Johnny Chen5a0bee72011-06-15 22:14:12 +000066 self.assertTrue(process, PROCESS_IS_VALID)
Johnny Chen835a88c2010-11-18 23:33:43 +000067
68 # The stop reason of the thread should be breakpoint.
Johnny Chen5a0bee72011-06-15 22:14:12 +000069 thread = process.GetThreadAtIndex(0)
Johnny Chen835a88c2010-11-18 23:33:43 +000070 if thread.GetStopReason() != lldb.eStopReasonBreakpoint:
Johnny Chende90f1d2011-04-27 17:43:07 +000071 from lldbutil import stop_reason_to_str
Johnny Chen835a88c2010-11-18 23:33:43 +000072 self.fail(STOPPED_DUE_TO_BREAKPOINT_WITH_STOP_REASON_AS %
Johnny Chende90f1d2011-04-27 17:43:07 +000073 stop_reason_to_str(thread.GetStopReason()))
Johnny Chen835a88c2010-11-18 23:33:43 +000074
75 # Get the SBValue of 'A::g_points' and 'g_points'.
76 frame = thread.GetFrameAtIndex(0)
77
78 # arguments => False
79 # locals => False
80 # statics => True
81 # in_scope_only => False
82 valList = frame.GetVariables(False, False, True, False)
83
Johnny Chene69c7482011-04-28 22:57:01 +000084 for val in valList:
Johnny Chen9a07aba2011-07-11 20:06:28 +000085 self.DebugSBValue(val)
Johnny Chen835a88c2010-11-18 23:33:43 +000086 name = val.GetName()
87 self.assertTrue(name in ['g_points', 'A::g_points'])
88 if name == 'g_points':
Greg Claytonbc8d2392013-05-14 22:17:29 +000089 self.assertTrue(val.GetValueType() == lldb.eValueTypeVariableStatic)
Johnny Chen835a88c2010-11-18 23:33:43 +000090 self.assertTrue(val.GetNumChildren() == 2)
Siva Chandra5ab2e072014-12-19 22:40:05 +000091 elif name == 'A::g_points':
Greg Claytonbc8d2392013-05-14 22:17:29 +000092 self.assertTrue(val.GetValueType() == lldb.eValueTypeVariableGlobal)
Johnny Chen835a88c2010-11-18 23:33:43 +000093 self.assertTrue(val.GetNumChildren() == 2)
94 child1 = val.GetChildAtIndex(1)
Johnny Chen9a07aba2011-07-11 20:06:28 +000095 self.DebugSBValue(child1)
Johnny Chen835a88c2010-11-18 23:33:43 +000096 child1_x = child1.GetChildAtIndex(0)
Johnny Chen9a07aba2011-07-11 20:06:28 +000097 self.DebugSBValue(child1_x)
Johnny Chen835a88c2010-11-18 23:33:43 +000098 self.assertTrue(child1_x.GetTypeName() == 'int' and
Greg Claytonfe42ac42011-08-03 22:57:10 +000099 child1_x.GetValue() == '11')
Johnny Chen835a88c2010-11-18 23:33:43 +0000100
Johnny Chen94f928b2010-12-14 18:59:15 +0000101 # SBFrame.FindValue() should also work.
102 val = frame.FindValue("A::g_points", lldb.eValueTypeVariableGlobal)
Johnny Chen9a07aba2011-07-11 20:06:28 +0000103 self.DebugSBValue(val)
Johnny Chenbeae5232010-11-19 18:07:14 +0000104 self.assertTrue(val.GetName() == 'A::g_points')
105
106 # Also exercise the "parameter" and "local" scopes while we are at it.
Johnny Chen94f928b2010-12-14 18:59:15 +0000107 val = frame.FindValue("argc", lldb.eValueTypeVariableArgument)
Johnny Chen9a07aba2011-07-11 20:06:28 +0000108 self.DebugSBValue(val)
Johnny Chenbeae5232010-11-19 18:07:14 +0000109 self.assertTrue(val.GetName() == 'argc')
110
Johnny Chen94f928b2010-12-14 18:59:15 +0000111 val = frame.FindValue("argv", lldb.eValueTypeVariableArgument)
Johnny Chen9a07aba2011-07-11 20:06:28 +0000112 self.DebugSBValue(val)
Johnny Chenbeae5232010-11-19 18:07:14 +0000113 self.assertTrue(val.GetName() == 'argv')
114
Johnny Chen94f928b2010-12-14 18:59:15 +0000115 val = frame.FindValue("hello_world", lldb.eValueTypeVariableLocal)
Johnny Chen9a07aba2011-07-11 20:06:28 +0000116 self.DebugSBValue(val)
Johnny Chenbeae5232010-11-19 18:07:14 +0000117 self.assertTrue(val.GetName() == 'hello_world')
Johnny Chen835a88c2010-11-18 23:33:43 +0000118
Johnny Chen3df7f942010-11-18 20:35:54 +0000119
120if __name__ == '__main__':
121 import atexit
122 lldb.SBDebugger.Initialize()
123 atexit.register(lambda: lldb.SBDebugger.Terminate())
124 unittest2.main()