1"""
2Make sure the getting a variable path works and doesn't crash.
3"""
4
5
6
7import lldb
8import lldbsuite.test.lldbutil as lldbutil
9from lldbsuite.test.lldbtest import *
10
11
12class TestVarPath(TestBase):
13
14    mydir = TestBase.compute_mydir(__file__)
15
16    # If your test case doesn't stress debug info, the
17    # set this to true.  That way it won't be run once for
18    # each debug info format.
19    NO_DEBUG_INFO_TESTCASE = True
20
21    def test_frame_var(self):
22        self.build()
23        self.do_test()
24
25    def verify_point(self, frame, var_name, var_typename, x_value, y_value):
26        v = frame.GetValueForVariablePath(var_name)
27        self.assertTrue(v.GetError().Success(), "Make sure we find '%s'" % (var_name))
28        self.assertEquals(v.GetType().GetName(), var_typename,
29                        "Make sure '%s' has type '%s'" % (var_name, var_typename))
30
31        if '*' in var_typename:
32            valid_prefix = var_name + '->'
33            invalid_prefix = var_name + '.'
34        else:
35            valid_prefix = var_name + '.'
36            invalid_prefix = var_name + '->'
37
38        valid_x_path = valid_prefix + 'x'
39        valid_y_path = valid_prefix + 'y'
40        invalid_x_path = invalid_prefix + 'x'
41        invalid_y_path = invalid_prefix + 'y'
42        invalid_m_path = invalid_prefix + 'm'
43
44        v = frame.GetValueForVariablePath(valid_x_path)
45        self.assertTrue(v.GetError().Success(), "Make sure we find '%s'" % (valid_x_path))
46        self.assertEquals(v.GetValue(), str(x_value), "Make sure '%s' has a value of %i" % (valid_x_path, x_value))
47        self.assertEquals(v.GetType().GetName(), "int", "Make sure '%s' has type 'int'" % (valid_x_path))
48        v = frame.GetValueForVariablePath(invalid_x_path)
49        self.assertTrue(v.GetError().Fail(), "Make sure we don't find '%s'" % (invalid_x_path))
50
51        v = frame.GetValueForVariablePath(valid_y_path)
52        self.assertTrue(v.GetError().Success(), "Make sure we find '%s'" % (valid_y_path))
53        self.assertEquals(v.GetValue(), str(y_value), "Make sure '%s' has a value of %i" % (valid_y_path, y_value))
54        self.assertEquals(v.GetType().GetName(), "int", "Make sure '%s' has type 'int'" % (valid_y_path))
55        v = frame.GetValueForVariablePath(invalid_y_path)
56        self.assertTrue(v.GetError().Fail(), "Make sure we don't find '%s'" % (invalid_y_path))
57
58        v = frame.GetValueForVariablePath(invalid_m_path)
59        self.assertTrue(v.GetError().Fail(), "Make sure we don't find '%s'" % (invalid_m_path))
60
61    def do_test(self):
62        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
63            self, "// Set a breakpoint here", lldb.SBFileSpec("main.cpp"))
64
65        frame = thread.GetFrameAtIndex(0)
66        v = frame.GetValueForVariablePath('no_such_variable')
67        self.assertTrue(v.GetError().Fail(), "Make sure we don't find 'no_such_variable'")
68
69        # Test an instance
70        self.verify_point(frame, 'pt', 'Point', 1, 2)
71        # Test a pointer
72        self.verify_point(frame, 'pt_ptr', 'Point *', 3030, 4040)
73        # Test using a pointer as an array
74        self.verify_point(frame, 'pt_ptr[-1]', 'Point', 1010, 2020)
75        self.verify_point(frame, 'pt_ptr[0]', 'Point', 3030, 4040)
76        self.verify_point(frame, 'pt_ptr[1]', 'Point', 5050, 6060)
77        # Test arrays
78        v = frame.GetValueForVariablePath('points')
79        self.assertTrue(v.GetError().Success(),
80                        "Make sure we find 'points'")
81        self.verify_point(frame, 'points[0]', 'Point', 1010, 2020)
82        self.verify_point(frame, 'points[1]', 'Point', 3030, 4040)
83        self.verify_point(frame, 'points[2]', 'Point', 5050, 6060)
84        v = frame.GetValueForVariablePath('points[0]+5')
85        self.assertTrue(v.GetError().Fail(),
86                        "Make sure we do not ignore characters between ']' and the end")
87        # Test a reference
88        self.verify_point(frame, 'pt_ref', 'Point &', 1, 2)
89        v = frame.GetValueForVariablePath('pt_sp')
90        self.assertTrue(v.GetError().Success(), "Make sure we find 'pt_sp'")
91        # Make sure we don't crash when looking for non existant child
92        # in type with synthetic children. This used to cause a crash.
93        v = frame.GetValueForVariablePath('pt_sp->not_valid_child')
94        self.assertTrue(v.GetError().Fail(),
95                        "Make sure we don't find 'pt_sp->not_valid_child'")
96
97
98
99