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.assertEquals(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.assertEquals(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.assertEquals(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.assertEquals(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.assertEquals(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.assertEquals(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. 138 process.Continue() 139 140 self.assertEquals(process.GetState(), lldb.eStateStopped) 141 thread = lldbutil.get_stopped_thread( 142 process, lldb.eStopReasonBreakpoint) 143 self.assertTrue( 144 thread.IsValid(), 145 "There should be a thread stopped due to breakpoint condition") 146 147 # Grab the stdout and make sure we changed the real values as well. 148 # This doesn't work for reproducers as the inferior doesn't run. 149 if not configuration.is_reproducer(): 150 expected_value = "Val - 12345 Mine - 55, 98765, 55555555. Ptr - 66, 98765, 66666666" 151 stdout = process.GetSTDOUT(1000) 152 self.assertTrue( 153 expected_value in stdout, 154 "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.assertTrue(error.Success(), "Got a changed value for sp") 166 self.assertTrue( 167 actual_value == 1, 168 "Got the right changed value for sp.") 169 170 # Boundary condition test the SBValue.CreateValueFromExpression() API. 171 # LLDB should not crash! 172 nosuchval = mine_value.CreateValueFromExpression(None, None) 173 174 process.Continue() 175 176 self.assertEquals(process.GetState(), lldb.eStateStopped) 177 thread = lldbutil.get_stopped_thread( 178 process, lldb.eStopReasonBreakpoint) 179 self.assertTrue( 180 thread is None, 181 "We should not have managed to hit our second breakpoint with sp == 1") 182 183 process.Kill() 184