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