Paul Herman | 10bc1a4 | 2015-08-18 22:46:57 +0000 | [diff] [blame] | 1 | """ |
| 2 | Test scopes in C++. |
| 3 | """ |
| 4 | import lldb |
| 5 | from lldbtest import * |
| 6 | import lldbutil |
| 7 | |
| 8 | class TestCppScopes(TestBase): |
Chaoren Lin | 464be90 | 2015-08-19 04:08:56 +0000 | [diff] [blame] | 9 | |
Paul Herman | 10bc1a4 | 2015-08-18 22:46:57 +0000 | [diff] [blame] | 10 | mydir = TestBase.compute_mydir(__file__) |
Chaoren Lin | 464be90 | 2015-08-19 04:08:56 +0000 | [diff] [blame] | 11 | |
Chaoren Lin | 9a379c1 | 2015-08-19 06:21:09 +0000 | [diff] [blame] | 12 | @expectedFailureDarwin |
Zachary Turner | 6e8cbc0 | 2015-09-11 19:59:39 +0000 | [diff] [blame] | 13 | @expectedFailureWindows("llvm.org/pr24764") |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 14 | def test_with_run_command(self): |
| 15 | self.build() |
Paul Herman | 10bc1a4 | 2015-08-18 22:46:57 +0000 | [diff] [blame] | 16 | |
Paul Herman | 10bc1a4 | 2015-08-18 22:46:57 +0000 | [diff] [blame] | 17 | # Get main source file |
| 18 | src_file = "main.cpp" |
| 19 | src_file_spec = lldb.SBFileSpec(src_file) |
| 20 | self.assertTrue(src_file_spec.IsValid(), "Main source file") |
Chaoren Lin | 464be90 | 2015-08-19 04:08:56 +0000 | [diff] [blame] | 21 | |
Paul Herman | 10bc1a4 | 2015-08-18 22:46:57 +0000 | [diff] [blame] | 22 | # Get the path of the executable |
Chaoren Lin | 464be90 | 2015-08-19 04:08:56 +0000 | [diff] [blame] | 23 | cwd = os.getcwd() |
Paul Herman | 10bc1a4 | 2015-08-18 22:46:57 +0000 | [diff] [blame] | 24 | exe_file = "a.out" |
| 25 | exe_path = os.path.join(cwd, exe_file) |
Chaoren Lin | 464be90 | 2015-08-19 04:08:56 +0000 | [diff] [blame] | 26 | |
Paul Herman | 10bc1a4 | 2015-08-18 22:46:57 +0000 | [diff] [blame] | 27 | # Load the executable |
| 28 | target = self.dbg.CreateTarget(exe_path) |
| 29 | self.assertTrue(target.IsValid(), VALID_TARGET) |
| 30 | |
| 31 | # Break on main function |
| 32 | main_breakpoint = target.BreakpointCreateBySourceRegex("// break here", src_file_spec) |
| 33 | self.assertTrue(main_breakpoint.IsValid() and main_breakpoint.GetNumLocations() >= 1, VALID_BREAKPOINT) |
| 34 | |
| 35 | # Launch the process |
| 36 | args = None |
| 37 | env = None |
Chaoren Lin | 464be90 | 2015-08-19 04:08:56 +0000 | [diff] [blame] | 38 | process = target.LaunchSimple(args, env, self.get_process_working_directory()) |
Paul Herman | 10bc1a4 | 2015-08-18 22:46:57 +0000 | [diff] [blame] | 39 | self.assertTrue(process.IsValid(), PROCESS_IS_VALID) |
| 40 | |
| 41 | # Get the thread of the process |
| 42 | self.assertTrue(process.GetState() == lldb.eStateStopped, PROCESS_STOPPED) |
| 43 | thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint) |
| 44 | |
Chaoren Lin | 464be90 | 2015-08-19 04:08:56 +0000 | [diff] [blame] | 45 | # Get current fream of the thread at the breakpoint |
Paul Herman | 10bc1a4 | 2015-08-18 22:46:57 +0000 | [diff] [blame] | 46 | frame = thread.GetSelectedFrame() |
| 47 | |
Chaoren Lin | 464be90 | 2015-08-19 04:08:56 +0000 | [diff] [blame] | 48 | # Test result for scopes of variables |
Paul Herman | 10bc1a4 | 2015-08-18 22:46:57 +0000 | [diff] [blame] | 49 | |
| 50 | global_variables = frame.GetVariables(True, True, True, False) |
Chaoren Lin | 464be90 | 2015-08-19 04:08:56 +0000 | [diff] [blame] | 51 | global_variables_assert = { |
Paul Herman | 10bc1a4 | 2015-08-18 22:46:57 +0000 | [diff] [blame] | 52 | 'A::a': 1111, |
| 53 | 'B::a': 2222, |
| 54 | 'C::a': 3333, |
| 55 | '::a': 4444, |
| 56 | 'a': 4444 |
| 57 | } |
| 58 | |
| 59 | self.assertTrue(global_variables.GetSize() == 4, "target variable returns all variables") |
| 60 | for variable in global_variables: |
Chaoren Lin | 464be90 | 2015-08-19 04:08:56 +0000 | [diff] [blame] | 61 | name = variable.GetName() |
Paul Herman | 10bc1a4 | 2015-08-18 22:46:57 +0000 | [diff] [blame] | 62 | self.assertTrue(name in global_variables_assert, "target variable returns wrong variable " + name) |
| 63 | |
| 64 | for name in global_variables_assert: |
| 65 | value = frame.EvaluateExpression(name) |
| 66 | assert_value = global_variables_assert[name] |
| 67 | self.assertTrue(value.IsValid() and value.GetValueAsSigned() == assert_value, name + " = " + str(assert_value)) |