xref: /llvm-project/lldb/test/API/python_api/value/change_values/TestChangeValueAPI.py (revision 99451b4453688a94c6014cac233d371ab4cc342d)
1"""
2Test some SBValue APIs.
3"""
4
5
6
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11
12
13class ChangeValueAPITestCase(TestBase):
14
15    mydir = TestBase.compute_mydir(__file__)
16
17    def setUp(self):
18        # Call super's setUp().
19        TestBase.setUp(self)
20        # We'll use the test method name as the exe_name.
21        self.exe_name = self.testMethodName
22        # Find the line number to of function 'c'.
23        self.line = line_number('main.c', '// Stop here and set values')
24        self.check_line = line_number(
25            'main.c', '// Stop here and check values')
26        self.end_line = line_number(
27            'main.c', '// Set a breakpoint here at the end')
28
29    @add_test_categories(['pyapi'])
30    @expectedFlakeyLinux("llvm.org/pr25652")
31    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24772")
32    def test_change_value(self):
33        """Exercise the SBValue::SetValueFromCString API."""
34        d = {'EXE': self.exe_name}
35        self.build(dictionary=d)
36        self.setTearDownCleanup(dictionary=d)
37        exe = self.getBuildArtifact(self.exe_name)
38
39        # Create a target by the debugger.
40        target = self.dbg.CreateTarget(exe)
41        self.assertTrue(target, VALID_TARGET)
42
43        # Create the breakpoint inside function 'main'.
44        breakpoint = target.BreakpointCreateByLocation('main.c', self.line)
45        self.assertTrue(breakpoint, VALID_BREAKPOINT)
46
47        # Create the breakpoint inside the function 'main'
48        check_breakpoint = target.BreakpointCreateByLocation(
49            'main.c', self.check_line)
50        self.assertTrue(check_breakpoint, VALID_BREAKPOINT)
51
52        # Create the breakpoint inside function 'main'.
53        end_breakpoint = target.BreakpointCreateByLocation(
54            'main.c', self.end_line)
55        self.assertTrue(end_breakpoint, VALID_BREAKPOINT)
56
57        # Now launch the process, and do not stop at entry point.
58        process = target.LaunchSimple(
59            None, None, self.get_process_working_directory())
60        self.assertTrue(process, PROCESS_IS_VALID)
61
62        # Get Frame #0.
63        self.assertTrue(process.GetState() == lldb.eStateStopped)
64        thread = lldbutil.get_stopped_thread(
65            process, lldb.eStopReasonBreakpoint)
66        self.assertTrue(
67            thread.IsValid(),
68            "There should be a thread stopped due to breakpoint condition")
69        frame0 = thread.GetFrameAtIndex(0)
70        self.assertTrue(frame0.IsValid(), "Got a valid frame.")
71
72        # Get the val variable and change it:
73        error = lldb.SBError()
74
75        val_value = frame0.FindVariable("val")
76        self.assertTrue(val_value.IsValid(), "Got the SBValue for val")
77        actual_value = val_value.GetValueAsSigned(error, 0)
78        self.assertTrue(error.Success(), "Got a value from val")
79        self.assertTrue(actual_value == 100, "Got the right value from val")
80
81        result = val_value.SetValueFromCString("12345")
82        self.assertTrue(result, "Setting val returned True.")
83        actual_value = val_value.GetValueAsSigned(error, 0)
84        self.assertTrue(error.Success(), "Got a changed value from val")
85        self.assertTrue(
86            actual_value == 12345,
87            "Got the right changed value from val")
88
89        # Now check that we can set a structure element:
90
91        mine_value = frame0.FindVariable("mine")
92        self.assertTrue(mine_value.IsValid(), "Got the SBValue for mine")
93
94        mine_second_value = mine_value.GetChildMemberWithName("second_val")
95        self.assertTrue(
96            mine_second_value.IsValid(),
97            "Got second_val from mine")
98        actual_value = mine_second_value.GetValueAsUnsigned(error, 0)
99        self.assertTrue(
100            error.Success(),
101            "Got an unsigned value for second_val")
102        self.assertTrue(actual_value == 5555)
103
104        result = mine_second_value.SetValueFromCString("98765")
105        self.assertTrue(result, "Success setting mine.second_value.")
106        actual_value = mine_second_value.GetValueAsSigned(error, 0)
107        self.assertTrue(
108            error.Success(),
109            "Got a changed value from mine.second_val")
110        self.assertTrue(actual_value == 98765,
111                        "Got the right changed value from mine.second_val")
112
113        # Next do the same thing with the pointer version.
114        ptr_value = frame0.FindVariable("ptr")
115        self.assertTrue(ptr_value.IsValid(), "Got the SBValue for ptr")
116
117        ptr_second_value = ptr_value.GetChildMemberWithName("second_val")
118        self.assertTrue(ptr_second_value.IsValid(), "Got second_val from ptr")
119        actual_value = ptr_second_value.GetValueAsUnsigned(error, 0)
120        self.assertTrue(
121            error.Success(),
122            "Got an unsigned value for ptr->second_val")
123        self.assertTrue(actual_value == 6666)
124
125        result = ptr_second_value.SetValueFromCString("98765")
126        self.assertTrue(result, "Success setting ptr->second_value.")
127        actual_value = ptr_second_value.GetValueAsSigned(error, 0)
128        self.assertTrue(
129            error.Success(),
130            "Got a changed value from ptr->second_val")
131        self.assertTrue(actual_value == 98765,
132                        "Got the right changed value from ptr->second_val")
133
134        # gcc may set multiple locations for breakpoint
135        breakpoint.SetEnabled(False)
136
137        # Now continue, grab the stdout and make sure we changed the real
138        # values as well...
139        process.Continue()
140
141        self.assertTrue(process.GetState() == lldb.eStateStopped)
142        thread = lldbutil.get_stopped_thread(
143            process, lldb.eStopReasonBreakpoint)
144        self.assertTrue(
145            thread.IsValid(),
146            "There should be a thread stopped due to breakpoint condition")
147
148        expected_value = "Val - 12345 Mine - 55, 98765, 55555555. Ptr - 66, 98765, 66666666"
149        stdout = process.GetSTDOUT(1000)
150        self.assertTrue(
151            expected_value in stdout,
152            "STDOUT showed changed values.")
153
154        # Finally, change the stack pointer to 0, and we should not make it to
155        # our end breakpoint.
156        frame0 = thread.GetFrameAtIndex(0)
157        self.assertTrue(frame0.IsValid(), "Second time: got a valid frame.")
158        sp_value = frame0.FindValue("sp", lldb.eValueTypeRegister)
159        self.assertTrue(sp_value.IsValid(), "Got a stack pointer value")
160        result = sp_value.SetValueFromCString("1")
161        self.assertTrue(result, "Setting sp returned true.")
162        actual_value = sp_value.GetValueAsUnsigned(error, 0)
163        self.assertTrue(error.Success(), "Got a changed value for sp")
164        self.assertTrue(
165            actual_value == 1,
166            "Got the right changed value for sp.")
167
168        # Boundary condition test the SBValue.CreateValueFromExpression() API.
169        # LLDB should not crash!
170        nosuchval = mine_value.CreateValueFromExpression(None, None)
171
172        process.Continue()
173
174        self.assertTrue(process.GetState() == lldb.eStateStopped)
175        thread = lldbutil.get_stopped_thread(
176            process, lldb.eStopReasonBreakpoint)
177        self.assertTrue(
178            thread is None,
179            "We should not have managed to hit our second breakpoint with sp == 1")
180
181        process.Kill()
182