blob: c9182fe69d7b0d50c3f588a13587353c7bd70788 [file] [log] [blame]
Johnny Chen3df7f942010-11-18 20:35:54 +00001"""
2Test display and Python APIs on file and class static variables.
3"""
4
Zachary Turner77db4a82015-10-22 20:06:20 +00005import lldb_shared
6
Johnny Chen3df7f942010-11-18 20:35:54 +00007import os, time
Johnny Chen3df7f942010-11-18 20:35:54 +00008import lldb
9from lldbtest import *
Jim Ingham63dfc722012-09-22 00:05:11 +000010import lldbutil
Johnny Chen3df7f942010-11-18 20:35:54 +000011
12class StaticVariableTestCase(TestBase):
13
Greg Clayton4570d3e2013-12-10 23:19:29 +000014 mydir = TestBase.compute_mydir(__file__)
Johnny Chen835a88c2010-11-18 23:33:43 +000015
Johnny Chen3df7f942010-11-18 20:35:54 +000016 def setUp(self):
17 # Call super's setUp().
18 TestBase.setUp(self)
19 # Find the line number to break at.
20 self.line = line_number('main.cpp', '// Set break point at this line.')
21
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000022 @expectedFailureWindows("llvm.org/pr24764")
23 def test_with_run_command(self):
24 """Test that file and class static variables display correctly."""
25 self.build()
Johnny Chen3df7f942010-11-18 20:35:54 +000026 self.runCmd("file a.out", CURRENT_EXECUTABLE_SET)
27
Jim Ingham63dfc722012-09-22 00:05:11 +000028 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 +000029
Sean Callanan05834cd2015-07-01 23:56:30 +000030 self.runCmd("run", RUN_SUCCEEDED)
Johnny Chen3df7f942010-11-18 20:35:54 +000031
32 # The stop reason of the thread should be breakpoint.
33 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
Greg Clayton7260f622011-04-18 08:33:37 +000034 substrs = ['stopped',
Johnny Chen3df7f942010-11-18 20:35:54 +000035 'stop reason = breakpoint'])
36
Johnny Chen1dc9a202011-08-22 17:58:14 +000037 # global variables are no longer displayed with the "frame variable" command.
38 self.expect('target variable A::g_points', VARIABLES_DISPLAYED_CORRECTLY,
Siva Chandra89ce9552015-01-05 23:06:14 +000039 patterns=['\(PointType \[[1-9]*\]\) A::g_points = {.*}'])
Johnny Chen1dc9a202011-08-22 17:58:14 +000040 self.expect('target variable g_points', VARIABLES_DISPLAYED_CORRECTLY,
41 substrs = ['(PointType [2]) g_points'])
42
Johnny Chen3df7f942010-11-18 20:35:54 +000043 # On Mac OS X, gcc 4.2 emits the wrong debug info for A::g_points.
Johnny Chen3df7f942010-11-18 20:35:54 +000044 # A::g_points is an array of two elements.
Greg Claytone0d0a762015-04-02 18:24:03 +000045 if self.platformIsDarwin() or self.getPlatform() == "linux":
Johnny Chen1dc9a202011-08-22 17:58:14 +000046 self.expect("target variable A::g_points[1].x", VARIABLES_DISPLAYED_CORRECTLY,
Johnny Chen3df7f942010-11-18 20:35:54 +000047 startstr = "(int) A::g_points[1].x = 11")
48
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000049 @expectedFailureDarwin(9980907)
50 @expectedFailureClang('Clang emits incomplete debug info.')
51 @expectedFailureFreeBSD('llvm.org/pr20550 failing on FreeBSD-11')
52 @expectedFailureGcc('GCC emits incomplete debug info.')
53 @python_api_test
54 def test_with_python_api(self):
Johnny Chen835a88c2010-11-18 23:33:43 +000055 """Test Python APIs on file and class static variables."""
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000056 self.build()
Johnny Chen835a88c2010-11-18 23:33:43 +000057 exe = os.path.join(os.getcwd(), "a.out")
58
59 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +000060 self.assertTrue(target, VALID_TARGET)
Johnny Chen835a88c2010-11-18 23:33:43 +000061
62 breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line)
Johnny Chen4ebd0192011-05-24 18:22:45 +000063 self.assertTrue(breakpoint, VALID_BREAKPOINT)
Johnny Chen835a88c2010-11-18 23:33:43 +000064
65 # Now launch the process, and do not stop at entry point.
Greg Claytonc6947512013-12-13 19:18:59 +000066 process = target.LaunchSimple (None, None, self.get_process_working_directory())
Johnny Chen5a0bee72011-06-15 22:14:12 +000067 self.assertTrue(process, PROCESS_IS_VALID)
Johnny Chen835a88c2010-11-18 23:33:43 +000068
69 # The stop reason of the thread should be breakpoint.
Johnny Chen5a0bee72011-06-15 22:14:12 +000070 thread = process.GetThreadAtIndex(0)
Johnny Chen835a88c2010-11-18 23:33:43 +000071 if thread.GetStopReason() != lldb.eStopReasonBreakpoint:
Johnny Chende90f1d2011-04-27 17:43:07 +000072 from lldbutil import stop_reason_to_str
Johnny Chen835a88c2010-11-18 23:33:43 +000073 self.fail(STOPPED_DUE_TO_BREAKPOINT_WITH_STOP_REASON_AS %
Johnny Chende90f1d2011-04-27 17:43:07 +000074 stop_reason_to_str(thread.GetStopReason()))
Johnny Chen835a88c2010-11-18 23:33:43 +000075
76 # Get the SBValue of 'A::g_points' and 'g_points'.
77 frame = thread.GetFrameAtIndex(0)
78
79 # arguments => False
80 # locals => False
81 # statics => True
82 # in_scope_only => False
83 valList = frame.GetVariables(False, False, True, False)
84
Johnny Chene69c7482011-04-28 22:57:01 +000085 for val in valList:
Johnny Chen9a07aba2011-07-11 20:06:28 +000086 self.DebugSBValue(val)
Johnny Chen835a88c2010-11-18 23:33:43 +000087 name = val.GetName()
88 self.assertTrue(name in ['g_points', 'A::g_points'])
89 if name == 'g_points':
Greg Claytonbc8d2392013-05-14 22:17:29 +000090 self.assertTrue(val.GetValueType() == lldb.eValueTypeVariableStatic)
Johnny Chen835a88c2010-11-18 23:33:43 +000091 self.assertTrue(val.GetNumChildren() == 2)
Siva Chandra5ab2e072014-12-19 22:40:05 +000092 elif name == 'A::g_points':
Greg Claytonbc8d2392013-05-14 22:17:29 +000093 self.assertTrue(val.GetValueType() == lldb.eValueTypeVariableGlobal)
Johnny Chen835a88c2010-11-18 23:33:43 +000094 self.assertTrue(val.GetNumChildren() == 2)
95 child1 = val.GetChildAtIndex(1)
Johnny Chen9a07aba2011-07-11 20:06:28 +000096 self.DebugSBValue(child1)
Johnny Chen835a88c2010-11-18 23:33:43 +000097 child1_x = child1.GetChildAtIndex(0)
Johnny Chen9a07aba2011-07-11 20:06:28 +000098 self.DebugSBValue(child1_x)
Johnny Chen835a88c2010-11-18 23:33:43 +000099 self.assertTrue(child1_x.GetTypeName() == 'int' and
Greg Claytonfe42ac42011-08-03 22:57:10 +0000100 child1_x.GetValue() == '11')
Johnny Chen835a88c2010-11-18 23:33:43 +0000101
Johnny Chen94f928b2010-12-14 18:59:15 +0000102 # SBFrame.FindValue() should also work.
103 val = frame.FindValue("A::g_points", lldb.eValueTypeVariableGlobal)
Johnny Chen9a07aba2011-07-11 20:06:28 +0000104 self.DebugSBValue(val)
Johnny Chenbeae5232010-11-19 18:07:14 +0000105 self.assertTrue(val.GetName() == 'A::g_points')
106
107 # Also exercise the "parameter" and "local" scopes while we are at it.
Johnny Chen94f928b2010-12-14 18:59:15 +0000108 val = frame.FindValue("argc", lldb.eValueTypeVariableArgument)
Johnny Chen9a07aba2011-07-11 20:06:28 +0000109 self.DebugSBValue(val)
Johnny Chenbeae5232010-11-19 18:07:14 +0000110 self.assertTrue(val.GetName() == 'argc')
111
Johnny Chen94f928b2010-12-14 18:59:15 +0000112 val = frame.FindValue("argv", lldb.eValueTypeVariableArgument)
Johnny Chen9a07aba2011-07-11 20:06:28 +0000113 self.DebugSBValue(val)
Johnny Chenbeae5232010-11-19 18:07:14 +0000114 self.assertTrue(val.GetName() == 'argv')
115
Johnny Chen94f928b2010-12-14 18:59:15 +0000116 val = frame.FindValue("hello_world", lldb.eValueTypeVariableLocal)
Johnny Chen9a07aba2011-07-11 20:06:28 +0000117 self.DebugSBValue(val)
Johnny Chenbeae5232010-11-19 18:07:14 +0000118 self.assertTrue(val.GetName() == 'hello_world')