blob: 5bb54810f2ee01819fceaaa3ff8184f175081a6c [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 Turner35d017f2015-10-23 17:04:29 +00005from __future__ import print_function
6
Zachary Turner77db4a82015-10-22 20:06:20 +00007import lldb_shared
8
Johnny Chen3df7f942010-11-18 20:35:54 +00009import os, time
Johnny Chen3df7f942010-11-18 20:35:54 +000010import lldb
11from lldbtest import *
Jim Ingham63dfc722012-09-22 00:05:11 +000012import lldbutil
Johnny Chen3df7f942010-11-18 20:35:54 +000013
14class StaticVariableTestCase(TestBase):
15
Greg Clayton4570d3e2013-12-10 23:19:29 +000016 mydir = TestBase.compute_mydir(__file__)
Johnny Chen835a88c2010-11-18 23:33:43 +000017
Johnny Chen3df7f942010-11-18 20:35:54 +000018 def setUp(self):
19 # Call super's setUp().
20 TestBase.setUp(self)
21 # Find the line number to break at.
22 self.line = line_number('main.cpp', '// Set break point at this line.')
23
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000024 @expectedFailureWindows("llvm.org/pr24764")
25 def test_with_run_command(self):
26 """Test that file and class static variables display correctly."""
27 self.build()
Johnny Chen3df7f942010-11-18 20:35:54 +000028 self.runCmd("file a.out", CURRENT_EXECUTABLE_SET)
29
Jim Ingham63dfc722012-09-22 00:05:11 +000030 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 +000031
Sean Callanan05834cd2015-07-01 23:56:30 +000032 self.runCmd("run", RUN_SUCCEEDED)
Johnny Chen3df7f942010-11-18 20:35:54 +000033
34 # The stop reason of the thread should be breakpoint.
35 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
Greg Clayton7260f622011-04-18 08:33:37 +000036 substrs = ['stopped',
Johnny Chen3df7f942010-11-18 20:35:54 +000037 'stop reason = breakpoint'])
38
Johnny Chen1dc9a202011-08-22 17:58:14 +000039 # global variables are no longer displayed with the "frame variable" command.
40 self.expect('target variable A::g_points', VARIABLES_DISPLAYED_CORRECTLY,
Siva Chandra89ce9552015-01-05 23:06:14 +000041 patterns=['\(PointType \[[1-9]*\]\) A::g_points = {.*}'])
Johnny Chen1dc9a202011-08-22 17:58:14 +000042 self.expect('target variable g_points', VARIABLES_DISPLAYED_CORRECTLY,
43 substrs = ['(PointType [2]) g_points'])
44
Johnny Chen3df7f942010-11-18 20:35:54 +000045 # On Mac OS X, gcc 4.2 emits the wrong debug info for A::g_points.
Johnny Chen3df7f942010-11-18 20:35:54 +000046 # A::g_points is an array of two elements.
Greg Claytone0d0a762015-04-02 18:24:03 +000047 if self.platformIsDarwin() or self.getPlatform() == "linux":
Johnny Chen1dc9a202011-08-22 17:58:14 +000048 self.expect("target variable A::g_points[1].x", VARIABLES_DISPLAYED_CORRECTLY,
Johnny Chen3df7f942010-11-18 20:35:54 +000049 startstr = "(int) A::g_points[1].x = 11")
50
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000051 @expectedFailureDarwin(9980907)
52 @expectedFailureClang('Clang emits incomplete debug info.')
53 @expectedFailureFreeBSD('llvm.org/pr20550 failing on FreeBSD-11')
54 @expectedFailureGcc('GCC emits incomplete debug info.')
Pavel Labathdc8b2d32015-10-26 09:28:32 +000055 @add_test_categories(['pyapi'])
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000056 def test_with_python_api(self):
Johnny Chen835a88c2010-11-18 23:33:43 +000057 """Test Python APIs on file and class static variables."""
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000058 self.build()
Johnny Chen835a88c2010-11-18 23:33:43 +000059 exe = os.path.join(os.getcwd(), "a.out")
60
61 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +000062 self.assertTrue(target, VALID_TARGET)
Johnny Chen835a88c2010-11-18 23:33:43 +000063
64 breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line)
Johnny Chen4ebd0192011-05-24 18:22:45 +000065 self.assertTrue(breakpoint, VALID_BREAKPOINT)
Johnny Chen835a88c2010-11-18 23:33:43 +000066
67 # Now launch the process, and do not stop at entry point.
Greg Claytonc6947512013-12-13 19:18:59 +000068 process = target.LaunchSimple (None, None, self.get_process_working_directory())
Johnny Chen5a0bee72011-06-15 22:14:12 +000069 self.assertTrue(process, PROCESS_IS_VALID)
Johnny Chen835a88c2010-11-18 23:33:43 +000070
71 # The stop reason of the thread should be breakpoint.
Johnny Chen5a0bee72011-06-15 22:14:12 +000072 thread = process.GetThreadAtIndex(0)
Johnny Chen835a88c2010-11-18 23:33:43 +000073 if thread.GetStopReason() != lldb.eStopReasonBreakpoint:
Johnny Chende90f1d2011-04-27 17:43:07 +000074 from lldbutil import stop_reason_to_str
Johnny Chen835a88c2010-11-18 23:33:43 +000075 self.fail(STOPPED_DUE_TO_BREAKPOINT_WITH_STOP_REASON_AS %
Johnny Chende90f1d2011-04-27 17:43:07 +000076 stop_reason_to_str(thread.GetStopReason()))
Johnny Chen835a88c2010-11-18 23:33:43 +000077
78 # Get the SBValue of 'A::g_points' and 'g_points'.
79 frame = thread.GetFrameAtIndex(0)
80
81 # arguments => False
82 # locals => False
83 # statics => True
84 # in_scope_only => False
85 valList = frame.GetVariables(False, False, True, False)
86
Johnny Chene69c7482011-04-28 22:57:01 +000087 for val in valList:
Johnny Chen9a07aba2011-07-11 20:06:28 +000088 self.DebugSBValue(val)
Johnny Chen835a88c2010-11-18 23:33:43 +000089 name = val.GetName()
90 self.assertTrue(name in ['g_points', 'A::g_points'])
91 if name == 'g_points':
Greg Claytonbc8d2392013-05-14 22:17:29 +000092 self.assertTrue(val.GetValueType() == lldb.eValueTypeVariableStatic)
Johnny Chen835a88c2010-11-18 23:33:43 +000093 self.assertTrue(val.GetNumChildren() == 2)
Siva Chandra5ab2e072014-12-19 22:40:05 +000094 elif name == 'A::g_points':
Greg Claytonbc8d2392013-05-14 22:17:29 +000095 self.assertTrue(val.GetValueType() == lldb.eValueTypeVariableGlobal)
Johnny Chen835a88c2010-11-18 23:33:43 +000096 self.assertTrue(val.GetNumChildren() == 2)
97 child1 = val.GetChildAtIndex(1)
Johnny Chen9a07aba2011-07-11 20:06:28 +000098 self.DebugSBValue(child1)
Johnny Chen835a88c2010-11-18 23:33:43 +000099 child1_x = child1.GetChildAtIndex(0)
Johnny Chen9a07aba2011-07-11 20:06:28 +0000100 self.DebugSBValue(child1_x)
Johnny Chen835a88c2010-11-18 23:33:43 +0000101 self.assertTrue(child1_x.GetTypeName() == 'int' and
Greg Claytonfe42ac42011-08-03 22:57:10 +0000102 child1_x.GetValue() == '11')
Johnny Chen835a88c2010-11-18 23:33:43 +0000103
Johnny Chen94f928b2010-12-14 18:59:15 +0000104 # SBFrame.FindValue() should also work.
105 val = frame.FindValue("A::g_points", lldb.eValueTypeVariableGlobal)
Johnny Chen9a07aba2011-07-11 20:06:28 +0000106 self.DebugSBValue(val)
Johnny Chenbeae5232010-11-19 18:07:14 +0000107 self.assertTrue(val.GetName() == 'A::g_points')
108
109 # Also exercise the "parameter" and "local" scopes while we are at it.
Johnny Chen94f928b2010-12-14 18:59:15 +0000110 val = frame.FindValue("argc", lldb.eValueTypeVariableArgument)
Johnny Chen9a07aba2011-07-11 20:06:28 +0000111 self.DebugSBValue(val)
Johnny Chenbeae5232010-11-19 18:07:14 +0000112 self.assertTrue(val.GetName() == 'argc')
113
Johnny Chen94f928b2010-12-14 18:59:15 +0000114 val = frame.FindValue("argv", lldb.eValueTypeVariableArgument)
Johnny Chen9a07aba2011-07-11 20:06:28 +0000115 self.DebugSBValue(val)
Johnny Chenbeae5232010-11-19 18:07:14 +0000116 self.assertTrue(val.GetName() == 'argv')
117
Johnny Chen94f928b2010-12-14 18:59:15 +0000118 val = frame.FindValue("hello_world", lldb.eValueTypeVariableLocal)
Johnny Chen9a07aba2011-07-11 20:06:28 +0000119 self.DebugSBValue(val)
Johnny Chenbeae5232010-11-19 18:07:14 +0000120 self.assertTrue(val.GetName() == 'hello_world')