]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - packages/Python/lldbsuite/test/python_api/value/TestValueAPI.py
Vendor import of lldb trunk r290819:
[FreeBSD/FreeBSD.git] / packages / Python / lldbsuite / test / python_api / value / TestValueAPI.py
1 """
2 Test some SBValue APIs.
3 """
4
5 from __future__ import print_function
6
7 import os
8 import re
9 import time
10
11 import lldb
12 from lldbsuite.test.decorators import *
13 from lldbsuite.test.lldbtest import *
14 from lldbsuite.test import lldbutil
15
16
17 class ValueAPITestCase(TestBase):
18
19     mydir = TestBase.compute_mydir(__file__)
20
21     def setUp(self):
22         # Call super's setUp().
23         TestBase.setUp(self)
24         # We'll use the test method name as the exe_name.
25         self.exe_name = self.testMethodName
26         # Find the line number to of function 'c'.
27         self.line = line_number('main.c', '// Break at this line')
28
29     @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24772")
30     @add_test_categories(['pyapi'])
31     def test(self):
32         """Exercise some SBValue APIs."""
33         d = {'EXE': self.exe_name}
34         self.build(dictionary=d)
35         self.setTearDownCleanup(dictionary=d)
36         exe = os.path.join(os.getcwd(), self.exe_name)
37
38         # Create a target by the debugger.
39         target = self.dbg.CreateTarget(exe)
40         self.assertTrue(target, VALID_TARGET)
41
42         # Create the breakpoint inside function 'main'.
43         breakpoint = target.BreakpointCreateByLocation('main.c', self.line)
44         self.assertTrue(breakpoint, VALID_BREAKPOINT)
45
46         # Now launch the process, and do not stop at entry point.
47         process = target.LaunchSimple(
48             None, None, self.get_process_working_directory())
49         self.assertTrue(process, PROCESS_IS_VALID)
50
51         # Get Frame #0.
52         self.assertTrue(process.GetState() == lldb.eStateStopped)
53         thread = lldbutil.get_stopped_thread(
54             process, lldb.eStopReasonBreakpoint)
55         self.assertTrue(
56             thread.IsValid(),
57             "There should be a thread stopped due to breakpoint condition")
58         frame0 = thread.GetFrameAtIndex(0)
59
60         # Get global variable 'days_of_week'.
61         list = target.FindGlobalVariables('days_of_week', 1)
62         days_of_week = list.GetValueAtIndex(0)
63         self.assertTrue(days_of_week, VALID_VARIABLE)
64         self.assertTrue(days_of_week.GetNumChildren() == 7, VALID_VARIABLE)
65         self.DebugSBValue(days_of_week)
66
67         # Get global variable 'weekdays'.
68         list = target.FindGlobalVariables('weekdays', 1)
69         weekdays = list.GetValueAtIndex(0)
70         self.assertTrue(weekdays, VALID_VARIABLE)
71         self.assertTrue(weekdays.GetNumChildren() == 5, VALID_VARIABLE)
72         self.DebugSBValue(weekdays)
73
74         # Get global variable 'g_table'.
75         list = target.FindGlobalVariables('g_table', 1)
76         g_table = list.GetValueAtIndex(0)
77         self.assertTrue(g_table, VALID_VARIABLE)
78         self.assertTrue(g_table.GetNumChildren() == 2, VALID_VARIABLE)
79         self.DebugSBValue(g_table)
80
81         fmt = lldbutil.BasicFormatter()
82         cvf = lldbutil.ChildVisitingFormatter(indent_child=2)
83         rdf = lldbutil.RecursiveDecentFormatter(indent_child=2)
84         if self.TraceOn():
85             print(fmt.format(days_of_week))
86             print(cvf.format(days_of_week))
87             print(cvf.format(weekdays))
88             print(rdf.format(g_table))
89
90         # Get variable 'my_int_ptr'.
91         value = frame0.FindVariable('my_int_ptr')
92         self.assertTrue(value, VALID_VARIABLE)
93         self.DebugSBValue(value)
94
95         # Get what 'my_int_ptr' points to.
96         pointed = value.GetChildAtIndex(0)
97         self.assertTrue(pointed, VALID_VARIABLE)
98         self.DebugSBValue(pointed)
99
100         # While we are at it, verify that 'my_int_ptr' points to 'g_my_int'.
101         symbol = target.ResolveLoadAddress(
102             int(pointed.GetLocation(), 0)).GetSymbol()
103         self.assertTrue(symbol)
104         self.expect(symbol.GetName(), exe=False,
105                     startstr='g_my_int')
106
107         # Get variable 'str_ptr'.
108         value = frame0.FindVariable('str_ptr')
109         self.assertTrue(value, VALID_VARIABLE)
110         self.DebugSBValue(value)
111
112         # SBValue::TypeIsPointerType() should return true.
113         self.assertTrue(value.TypeIsPointerType())
114
115         # Verify the SBValue::GetByteSize() API is working correctly.
116         arch = self.getArchitecture()
117         if arch == 'i386':
118             self.assertTrue(value.GetByteSize() == 4)
119         elif arch == 'x86_64':
120             self.assertTrue(value.GetByteSize() == 8)
121
122         # Get child at index 5 => 'Friday'.
123         child = value.GetChildAtIndex(5, lldb.eNoDynamicValues, True)
124         self.assertTrue(child, VALID_VARIABLE)
125         self.DebugSBValue(child)
126
127         self.expect(child.GetSummary(), exe=False,
128                     substrs=['Friday'])
129
130         # Now try to get at the same variable using GetValueForExpressionPath().
131         # These two SBValue objects should have the same value.
132         val2 = value.GetValueForExpressionPath('[5]')
133         self.assertTrue(val2, VALID_VARIABLE)
134         self.DebugSBValue(val2)
135         self.assertTrue(child.GetValue() == val2.GetValue() and
136                         child.GetSummary() == val2.GetSummary())
137
138         val_i = target.EvaluateExpression('i')
139         val_s = target.EvaluateExpression('s')
140         val_a = target.EvaluateExpression('a')
141         self.assertTrue(
142             val_s.GetChildMemberWithName('a').AddressOf(),
143             VALID_VARIABLE)
144         self.assertTrue(
145             val_a.Cast(
146                 val_i.GetType()).AddressOf(),
147             VALID_VARIABLE)
148
149         self.assertTrue(int(lldb.value(frame0.FindVariable('uinthex')))
150                         == 3768803088, 'uinthex == 3768803088')
151         self.assertTrue(int(lldb.value(frame0.FindVariable('sinthex')))
152                         == -526164208, 'sinthex == -526164208')
153
154         self.assertTrue(
155             frame0.FindVariable('uinthex').GetValueAsUnsigned() == 3768803088,
156             'unsigned uinthex == 3768803088')
157         self.assertTrue(
158             frame0.FindVariable('sinthex').GetValueAsUnsigned() == 3768803088,
159             'unsigned sinthex == 3768803088')
160
161         self.assertTrue(
162             frame0.FindVariable('uinthex').GetValueAsSigned() == -
163             526164208,
164             'signed uinthex == -526164208')
165         self.assertTrue(
166             frame0.FindVariable('sinthex').GetValueAsSigned() == -
167             526164208,
168             'signed sinthex == -526164208')