blob: 104d53f0954a4855bb46ab8c25d7f904199b5c21 [file] [log] [blame]
Paul Herman10bc1a42015-08-18 22:46:57 +00001"""
2Test scopes in C++.
3"""
4import lldb
5from lldbtest import *
6import lldbutil
7
8class TestCppScopes(TestBase):
Chaoren Lin464be902015-08-19 04:08:56 +00009
Paul Herman10bc1a42015-08-18 22:46:57 +000010 mydir = TestBase.compute_mydir(__file__)
Chaoren Lin464be902015-08-19 04:08:56 +000011
Chaoren Lin9a379c12015-08-19 06:21:09 +000012 @expectedFailureDarwin
Paul Herman10bc1a42015-08-18 22:46:57 +000013 @skipUnlessDarwin
14 @dsym_test
15 def test_with_dsym_and_run_command(self):
16 self.buildDsym()
17 self.check()
18
Chaoren Lin9a379c12015-08-19 06:21:09 +000019 @expectedFailureDarwin
Zachary Turner6e8cbc02015-09-11 19:59:39 +000020 @expectedFailureWindows("llvm.org/pr24764")
Paul Herman10bc1a42015-08-18 22:46:57 +000021 @dwarf_test
22 def test_with_dwarf_and_run_command(self):
23 self.buildDwarf()
24 self.check()
25
26 def setUp(self):
27 TestBase.setUp(self)
28
29 def check(self):
30 # Get main source file
31 src_file = "main.cpp"
32 src_file_spec = lldb.SBFileSpec(src_file)
33 self.assertTrue(src_file_spec.IsValid(), "Main source file")
Chaoren Lin464be902015-08-19 04:08:56 +000034
Paul Herman10bc1a42015-08-18 22:46:57 +000035 # Get the path of the executable
Chaoren Lin464be902015-08-19 04:08:56 +000036 cwd = os.getcwd()
Paul Herman10bc1a42015-08-18 22:46:57 +000037 exe_file = "a.out"
38 exe_path = os.path.join(cwd, exe_file)
Chaoren Lin464be902015-08-19 04:08:56 +000039
Paul Herman10bc1a42015-08-18 22:46:57 +000040 # Load the executable
41 target = self.dbg.CreateTarget(exe_path)
42 self.assertTrue(target.IsValid(), VALID_TARGET)
43
44 # Break on main function
45 main_breakpoint = target.BreakpointCreateBySourceRegex("// break here", src_file_spec)
46 self.assertTrue(main_breakpoint.IsValid() and main_breakpoint.GetNumLocations() >= 1, VALID_BREAKPOINT)
47
48 # Launch the process
49 args = None
50 env = None
Chaoren Lin464be902015-08-19 04:08:56 +000051 process = target.LaunchSimple(args, env, self.get_process_working_directory())
Paul Herman10bc1a42015-08-18 22:46:57 +000052 self.assertTrue(process.IsValid(), PROCESS_IS_VALID)
53
54 # Get the thread of the process
55 self.assertTrue(process.GetState() == lldb.eStateStopped, PROCESS_STOPPED)
56 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
57
Chaoren Lin464be902015-08-19 04:08:56 +000058 # Get current fream of the thread at the breakpoint
Paul Herman10bc1a42015-08-18 22:46:57 +000059 frame = thread.GetSelectedFrame()
60
Chaoren Lin464be902015-08-19 04:08:56 +000061 # Test result for scopes of variables
Paul Herman10bc1a42015-08-18 22:46:57 +000062
63 global_variables = frame.GetVariables(True, True, True, False)
Chaoren Lin464be902015-08-19 04:08:56 +000064 global_variables_assert = {
Paul Herman10bc1a42015-08-18 22:46:57 +000065 'A::a': 1111,
66 'B::a': 2222,
67 'C::a': 3333,
68 '::a': 4444,
69 'a': 4444
70 }
71
72 self.assertTrue(global_variables.GetSize() == 4, "target variable returns all variables")
73 for variable in global_variables:
Chaoren Lin464be902015-08-19 04:08:56 +000074 name = variable.GetName()
Paul Herman10bc1a42015-08-18 22:46:57 +000075 self.assertTrue(name in global_variables_assert, "target variable returns wrong variable " + name)
76
77 for name in global_variables_assert:
78 value = frame.EvaluateExpression(name)
79 assert_value = global_variables_assert[name]
80 self.assertTrue(value.IsValid() and value.GetValueAsSigned() == assert_value, name + " = " + str(assert_value))
81
82if __name__ == '__main__':
83 import atexit
84 lldb.SBDebugger.Initialize()
85 atexit.register(lambda: lldb.SBDebugger.Terminate())
86 unittest2.main()