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