xref: /llvm-project/lldb/test/API/python_api/value/change_values/TestChangeValueAPI.py (revision 4714215efb0486682feaa3a99162e80a934be8f9)
199451b44SJordan Rupprecht"""
299451b44SJordan RupprechtTest some SBValue APIs.
399451b44SJordan Rupprecht"""
499451b44SJordan Rupprecht
599451b44SJordan Rupprechtimport lldb
699451b44SJordan Rupprechtfrom lldbsuite.test.decorators import *
799451b44SJordan Rupprechtfrom lldbsuite.test.lldbtest import *
899451b44SJordan Rupprechtfrom lldbsuite.test import lldbutil
999451b44SJordan Rupprecht
1099451b44SJordan Rupprecht
1199451b44SJordan Rupprechtclass ChangeValueAPITestCase(TestBase):
1299451b44SJordan Rupprecht    def setUp(self):
1399451b44SJordan Rupprecht        # Call super's setUp().
1499451b44SJordan Rupprecht        TestBase.setUp(self)
1599451b44SJordan Rupprecht        # We'll use the test method name as the exe_name.
1699451b44SJordan Rupprecht        self.exe_name = self.testMethodName
1799451b44SJordan Rupprecht        # Find the line number to of function 'c'.
182238dcc3SJonas Devlieghere        self.line = line_number("main.c", "// Stop here and set values")
192238dcc3SJonas Devlieghere        self.check_line = line_number("main.c", "// Stop here and check values")
202238dcc3SJonas Devlieghere        self.end_line = line_number("main.c", "// Set a breakpoint here at the end")
2199451b44SJordan Rupprecht
2299451b44SJordan Rupprecht    @expectedFlakeyLinux("llvm.org/pr25652")
2399451b44SJordan Rupprecht    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24772")
2499451b44SJordan Rupprecht    def test_change_value(self):
2599451b44SJordan Rupprecht        """Exercise the SBValue::SetValueFromCString API."""
262238dcc3SJonas Devlieghere        d = {"EXE": self.exe_name}
2799451b44SJordan Rupprecht        self.build(dictionary=d)
2899451b44SJordan Rupprecht        self.setTearDownCleanup(dictionary=d)
2999451b44SJordan Rupprecht        exe = self.getBuildArtifact(self.exe_name)
3099451b44SJordan Rupprecht
3199451b44SJordan Rupprecht        # Create a target by the debugger.
3299451b44SJordan Rupprecht        target = self.dbg.CreateTarget(exe)
3399451b44SJordan Rupprecht        self.assertTrue(target, VALID_TARGET)
3499451b44SJordan Rupprecht
3599451b44SJordan Rupprecht        # Create the breakpoint inside function 'main'.
362238dcc3SJonas Devlieghere        breakpoint = target.BreakpointCreateByLocation("main.c", self.line)
3799451b44SJordan Rupprecht        self.assertTrue(breakpoint, VALID_BREAKPOINT)
3899451b44SJordan Rupprecht
3999451b44SJordan Rupprecht        # Create the breakpoint inside the function 'main'
402238dcc3SJonas Devlieghere        check_breakpoint = target.BreakpointCreateByLocation("main.c", self.check_line)
4199451b44SJordan Rupprecht        self.assertTrue(check_breakpoint, VALID_BREAKPOINT)
4299451b44SJordan Rupprecht
4399451b44SJordan Rupprecht        # Create the breakpoint inside function 'main'.
442238dcc3SJonas Devlieghere        end_breakpoint = target.BreakpointCreateByLocation("main.c", self.end_line)
4599451b44SJordan Rupprecht        self.assertTrue(end_breakpoint, VALID_BREAKPOINT)
4699451b44SJordan Rupprecht
4799451b44SJordan Rupprecht        # Now launch the process, and do not stop at entry point.
482238dcc3SJonas Devlieghere        process = target.LaunchSimple(None, None, self.get_process_working_directory())
4999451b44SJordan Rupprecht        self.assertTrue(process, PROCESS_IS_VALID)
5099451b44SJordan Rupprecht
5199451b44SJordan Rupprecht        # Get Frame #0.
52ce825e46SJonas Devlieghere        self.assertState(process.GetState(), lldb.eStateStopped)
532238dcc3SJonas Devlieghere        thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
5499451b44SJordan Rupprecht        self.assertTrue(
5599451b44SJordan Rupprecht            thread.IsValid(),
562238dcc3SJonas Devlieghere            "There should be a thread stopped due to breakpoint condition",
572238dcc3SJonas Devlieghere        )
5899451b44SJordan Rupprecht        frame0 = thread.GetFrameAtIndex(0)
5999451b44SJordan Rupprecht        self.assertTrue(frame0.IsValid(), "Got a valid frame.")
6099451b44SJordan Rupprecht
6199451b44SJordan Rupprecht        # Get the val variable and change it:
6299451b44SJordan Rupprecht        error = lldb.SBError()
6399451b44SJordan Rupprecht
6499451b44SJordan Rupprecht        val_value = frame0.FindVariable("val")
6599451b44SJordan Rupprecht        self.assertTrue(val_value.IsValid(), "Got the SBValue for val")
6699451b44SJordan Rupprecht        actual_value = val_value.GetValueAsSigned(error, 0)
67779bbbf2SDave Lee        self.assertSuccess(error, "Got a value from val")
6880fcecb1SJonas Devlieghere        self.assertEqual(actual_value, 100, "Got the right value from val")
6999451b44SJordan Rupprecht
7099451b44SJordan Rupprecht        result = val_value.SetValueFromCString("12345")
7199451b44SJordan Rupprecht        self.assertTrue(result, "Setting val returned True.")
7299451b44SJordan Rupprecht        actual_value = val_value.GetValueAsSigned(error, 0)
73779bbbf2SDave Lee        self.assertSuccess(error, "Got a changed value from val")
742238dcc3SJonas Devlieghere        self.assertEqual(actual_value, 12345, "Got the right changed value from val")
7599451b44SJordan Rupprecht
7699451b44SJordan Rupprecht        # Now check that we can set a structure element:
7799451b44SJordan Rupprecht
7899451b44SJordan Rupprecht        mine_value = frame0.FindVariable("mine")
7999451b44SJordan Rupprecht        self.assertTrue(mine_value.IsValid(), "Got the SBValue for mine")
8099451b44SJordan Rupprecht
8199451b44SJordan Rupprecht        mine_second_value = mine_value.GetChildMemberWithName("second_val")
822238dcc3SJonas Devlieghere        self.assertTrue(mine_second_value.IsValid(), "Got second_val from mine")
8399451b44SJordan Rupprecht        actual_value = mine_second_value.GetValueAsUnsigned(error, 0)
842238dcc3SJonas Devlieghere        self.assertTrue(error.Success(), "Got an unsigned value for second_val")
8580fcecb1SJonas Devlieghere        self.assertEqual(actual_value, 5555)
8699451b44SJordan Rupprecht
8799451b44SJordan Rupprecht        result = mine_second_value.SetValueFromCString("98765")
8899451b44SJordan Rupprecht        self.assertTrue(result, "Success setting mine.second_value.")
8999451b44SJordan Rupprecht        actual_value = mine_second_value.GetValueAsSigned(error, 0)
902238dcc3SJonas Devlieghere        self.assertTrue(error.Success(), "Got a changed value from mine.second_val")
9180fcecb1SJonas Devlieghere        self.assertEqual(
922238dcc3SJonas Devlieghere            actual_value, 98765, "Got the right changed value from mine.second_val"
932238dcc3SJonas Devlieghere        )
9499451b44SJordan Rupprecht
9599451b44SJordan Rupprecht        # Next do the same thing with the pointer version.
9699451b44SJordan Rupprecht        ptr_value = frame0.FindVariable("ptr")
9799451b44SJordan Rupprecht        self.assertTrue(ptr_value.IsValid(), "Got the SBValue for ptr")
9899451b44SJordan Rupprecht
9999451b44SJordan Rupprecht        ptr_second_value = ptr_value.GetChildMemberWithName("second_val")
10099451b44SJordan Rupprecht        self.assertTrue(ptr_second_value.IsValid(), "Got second_val from ptr")
10199451b44SJordan Rupprecht        actual_value = ptr_second_value.GetValueAsUnsigned(error, 0)
1022238dcc3SJonas Devlieghere        self.assertTrue(error.Success(), "Got an unsigned value for ptr->second_val")
10380fcecb1SJonas Devlieghere        self.assertEqual(actual_value, 6666)
10499451b44SJordan Rupprecht
10599451b44SJordan Rupprecht        result = ptr_second_value.SetValueFromCString("98765")
10699451b44SJordan Rupprecht        self.assertTrue(result, "Success setting ptr->second_value.")
10799451b44SJordan Rupprecht        actual_value = ptr_second_value.GetValueAsSigned(error, 0)
1082238dcc3SJonas Devlieghere        self.assertTrue(error.Success(), "Got a changed value from ptr->second_val")
10980fcecb1SJonas Devlieghere        self.assertEqual(
1102238dcc3SJonas Devlieghere            actual_value, 98765, "Got the right changed value from ptr->second_val"
1112238dcc3SJonas Devlieghere        )
11299451b44SJordan Rupprecht
113*4714215eSJonas Devlieghere        ptr_fourth_value = ptr_value.GetChildMemberWithName("fourth_val")
114*4714215eSJonas Devlieghere        self.assertTrue(ptr_fourth_value.IsValid(), "Got fourth_val from ptr")
115*4714215eSJonas Devlieghere        fourth_actual_value = ptr_fourth_value.GetValueAsUnsigned(error, 1)
116*4714215eSJonas Devlieghere        self.assertTrue(error.Success(), "Got an unsigned value for ptr->fourth_val")
117*4714215eSJonas Devlieghere        self.assertEqual(fourth_actual_value, 0)
118*4714215eSJonas Devlieghere
119*4714215eSJonas Devlieghere        result = ptr_fourth_value.SetValueFromCString("true")
120*4714215eSJonas Devlieghere        self.assertTrue(result, "Success setting ptr->fourth_val.")
121*4714215eSJonas Devlieghere        fourth_actual_value = ptr_fourth_value.GetValueAsSigned(error, 0)
122*4714215eSJonas Devlieghere        self.assertTrue(error.Success(), "Got a changed value from ptr->fourth_val")
123*4714215eSJonas Devlieghere        self.assertEqual(
124*4714215eSJonas Devlieghere            fourth_actual_value, 1, "Got the right changed value from ptr->fourth_val"
125*4714215eSJonas Devlieghere        )
126*4714215eSJonas Devlieghere
127*4714215eSJonas Devlieghere        result = ptr_fourth_value.SetValueFromCString("NO")
128*4714215eSJonas Devlieghere        self.assertFalse(result, "Failed setting ptr->fourth_val.")
129*4714215eSJonas Devlieghere        fourth_actual_value = ptr_fourth_value.GetValueAsSigned(error, 0)
130*4714215eSJonas Devlieghere        self.assertTrue(error.Success(), "Got the original value from ptr->fourth_val")
131*4714215eSJonas Devlieghere        self.assertEqual(
132*4714215eSJonas Devlieghere            fourth_actual_value,
133*4714215eSJonas Devlieghere            1,
134*4714215eSJonas Devlieghere            "Got the original changed value from ptr->fourth_val",
135*4714215eSJonas Devlieghere        )
136*4714215eSJonas Devlieghere
13799451b44SJordan Rupprecht        # gcc may set multiple locations for breakpoint
13899451b44SJordan Rupprecht        breakpoint.SetEnabled(False)
13999451b44SJordan Rupprecht
1407606a543SJonas Devlieghere        # Now continue.
14199451b44SJordan Rupprecht        process.Continue()
14299451b44SJordan Rupprecht
143ce825e46SJonas Devlieghere        self.assertState(process.GetState(), lldb.eStateStopped)
1442238dcc3SJonas Devlieghere        thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
14599451b44SJordan Rupprecht        self.assertTrue(
14699451b44SJordan Rupprecht            thread.IsValid(),
1472238dcc3SJonas Devlieghere            "There should be a thread stopped due to breakpoint condition",
1482238dcc3SJonas Devlieghere        )
14999451b44SJordan Rupprecht
1502238dcc3SJonas Devlieghere        expected_value = (
151*4714215eSJonas Devlieghere            "Val - 12345 Mine - 55, 98765, 55555555, 0. Ptr - 66, 98765, 66666666, 1"
1522238dcc3SJonas Devlieghere        )
15399451b44SJordan Rupprecht        stdout = process.GetSTDOUT(1000)
1549c246882SJordan Rupprecht        self.assertIn(expected_value, stdout, "STDOUT showed changed values.")
15599451b44SJordan Rupprecht
15699451b44SJordan Rupprecht        # Finally, change the stack pointer to 0, and we should not make it to
15799451b44SJordan Rupprecht        # our end breakpoint.
15899451b44SJordan Rupprecht        frame0 = thread.GetFrameAtIndex(0)
15999451b44SJordan Rupprecht        self.assertTrue(frame0.IsValid(), "Second time: got a valid frame.")
16099451b44SJordan Rupprecht        sp_value = frame0.FindValue("sp", lldb.eValueTypeRegister)
16199451b44SJordan Rupprecht        self.assertTrue(sp_value.IsValid(), "Got a stack pointer value")
16299451b44SJordan Rupprecht        result = sp_value.SetValueFromCString("1")
16399451b44SJordan Rupprecht        self.assertTrue(result, "Setting sp returned true.")
16499451b44SJordan Rupprecht        actual_value = sp_value.GetValueAsUnsigned(error, 0)
165779bbbf2SDave Lee        self.assertSuccess(error, "Got a changed value for sp")
1662238dcc3SJonas Devlieghere        self.assertEqual(actual_value, 1, "Got the right changed value for sp.")
16799451b44SJordan Rupprecht
16899451b44SJordan Rupprecht        # Boundary condition test the SBValue.CreateValueFromExpression() API.
16999451b44SJordan Rupprecht        # LLDB should not crash!
17099451b44SJordan Rupprecht        nosuchval = mine_value.CreateValueFromExpression(None, None)
17199451b44SJordan Rupprecht
17299451b44SJordan Rupprecht        process.Continue()
17399451b44SJordan Rupprecht
174ce825e46SJonas Devlieghere        self.assertState(process.GetState(), lldb.eStateStopped)
1752238dcc3SJonas Devlieghere        thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
1769c246882SJordan Rupprecht        self.assertIsNone(
1779c246882SJordan Rupprecht            thread,
1782238dcc3SJonas Devlieghere            "We should not have managed to hit our second breakpoint with sp == 1",
1792238dcc3SJonas Devlieghere        )
18099451b44SJordan Rupprecht
18199451b44SJordan Rupprecht        process.Kill()
182