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