1 STRING_EXTENSION_OUTSIDE(SBValue)
2 %extend lldb::SBValue {
3 #ifdef SWIGPYTHON
4     %pythoncode %{
5         def __get_dynamic__ (self):
6             '''Helper function for the "SBValue.dynamic" property.'''
7             return self.GetDynamicValue (eDynamicCanRunTarget)
8 
9         class children_access(object):
10             '''A helper object that will lazily hand out thread for a process when supplied an index.'''
11 
12             def __init__(self, sbvalue):
13                 self.sbvalue = sbvalue
14 
15             def __len__(self):
16                 if self.sbvalue:
17                     return int(self.sbvalue.GetNumChildren())
18                 return 0
19 
20             def __getitem__(self, key):
21                 if isinstance(key, int):
22                     count = len(self)
23                     if -count <= key < count:
24                         key %= count
25                         return self.sbvalue.GetChildAtIndex(key)
26                 return None
27 
28         def get_child_access_object(self):
29             '''An accessor function that returns a children_access() object which allows lazy member variable access from a lldb.SBValue object.'''
30             return self.children_access (self)
31 
32         def get_value_child_list(self):
33             '''An accessor function that returns a list() that contains all children in a lldb.SBValue object.'''
34             children = []
35             accessor = self.get_child_access_object()
36             for idx in range(len(accessor)):
37                 children.append(accessor[idx])
38             return children
39 
40         def __iter__(self):
41             '''Iterate over all child values of a lldb.SBValue object.'''
42             return lldb_iter(self, 'GetNumChildren', 'GetChildAtIndex')
43 
44         def __len__(self):
45             '''Return the number of child values of a lldb.SBValue object.'''
46             return self.GetNumChildren()
47 
48         children = property(get_value_child_list, None, doc='''A read only property that returns a list() of lldb.SBValue objects for the children of the value.''')
49         child = property(get_child_access_object, None, doc='''A read only property that returns an object that can access children of a variable by index (child_value = value.children[12]).''')
50         name = property(GetName, None, doc='''A read only property that returns the name of this value as a string.''')
51         type = property(GetType, None, doc='''A read only property that returns a lldb.SBType object that represents the type for this value.''')
52         size = property(GetByteSize, None, doc='''A read only property that returns the size in bytes of this value.''')
53         is_in_scope = property(IsInScope, None, doc='''A read only property that returns a boolean value that indicates whether this value is currently lexically in scope.''')
54         format = property(GetName, SetFormat, doc='''A read/write property that gets/sets the format used for lldb.SBValue().GetValue() for this value. See enumerations that start with "lldb.eFormat".''')
55         value = property(GetValue, SetValueFromCString, doc='''A read/write property that gets/sets value from a string.''')
56         value_type = property(GetValueType, None, doc='''A read only property that returns an lldb enumeration value (see enumerations that start with "lldb.eValueType") that represents the type of this value (local, argument, global, register, etc.).''')
57         changed = property(GetValueDidChange, None, doc='''A read only property that returns a boolean value that indicates if this value has changed since it was last updated.''')
58         data = property(GetData, None, doc='''A read only property that returns an lldb object (lldb.SBData) that represents the bytes that make up the value for this object.''')
59         load_addr = property(GetLoadAddress, None, doc='''A read only property that returns the load address of this value as an integer.''')
60         addr = property(GetAddress, None, doc='''A read only property that returns an lldb.SBAddress that represents the address of this value if it is in memory.''')
61         deref = property(Dereference, None, doc='''A read only property that returns an lldb.SBValue that is created by dereferencing this value.''')
62         address_of = property(AddressOf, None, doc='''A read only property that returns an lldb.SBValue that represents the address-of this value.''')
63         error = property(GetError, None, doc='''A read only property that returns the lldb.SBError that represents the error from the last time the variable value was calculated.''')
64         summary = property(GetSummary, None, doc='''A read only property that returns the summary for this value as a string''')
65         description = property(GetObjectDescription, None, doc='''A read only property that returns the language-specific description of this value as a string''')
66         dynamic = property(__get_dynamic__, None, doc='''A read only property that returns an lldb.SBValue that is created by finding the dynamic type of this value.''')
67         location = property(GetLocation, None, doc='''A read only property that returns the location of this value as a string.''')
68         target = property(GetTarget, None, doc='''A read only property that returns the lldb.SBTarget that this value is associated with.''')
69         process = property(GetProcess, None, doc='''A read only property that returns the lldb.SBProcess that this value is associated with, the returned value might be invalid and should be tested.''')
70         thread = property(GetThread, None, doc='''A read only property that returns the lldb.SBThread that this value is associated with, the returned value might be invalid and should be tested.''')
71         frame = property(GetFrame, None, doc='''A read only property that returns the lldb.SBFrame that this value is associated with, the returned value might be invalid and should be tested.''')
72         num_children = property(GetNumChildren, None, doc='''A read only property that returns the number of child lldb.SBValues that this value has.''')
73         unsigned = property(GetValueAsUnsigned, None, doc='''A read only property that returns the value of this SBValue as an usigned integer.''')
74         signed = property(GetValueAsSigned, None, doc='''A read only property that returns the value of this SBValue as a signed integer.''')
75 
76         def get_expr_path(self):
77             s = SBStream()
78             self.GetExpressionPath (s)
79             return s.GetData()
80 
81         path = property(get_expr_path, None, doc='''A read only property that returns the expression path that one can use to reach this value in an expression.''')
82 
83         def synthetic_child_from_expression(self, name, expr, options=None):
84             if options is None: options = lldb.SBExpressionOptions()
85             child = self.CreateValueFromExpression(name, expr, options)
86             child.SetSyntheticChildrenGenerated(True)
87             return child
88 
89         def synthetic_child_from_data(self, name, data, type):
90             child = self.CreateValueFromData(name, data, type)
91             child.SetSyntheticChildrenGenerated(True)
92             return child
93 
94         def synthetic_child_from_address(self, name, addr, type):
95             child = self.CreateValueFromAddress(name, addr, type)
96             child.SetSyntheticChildrenGenerated(True)
97             return child
98 
99         def __eol_test(val):
100             """Default function for end of list test takes an SBValue object.
101 
102             Return True if val is invalid or it corresponds to a null pointer.
103             Otherwise, return False.
104             """
105             if not val or val.GetValueAsUnsigned() == 0:
106                 return True
107             else:
108                 return False
109 
110         # ==================================================
111         # Iterator for lldb.SBValue treated as a linked list
112         # ==================================================
113         def linked_list_iter(self, next_item_name, end_of_list_test=__eol_test):
114             """Generator adaptor to support iteration for SBValue as a linked list.
115 
116             linked_list_iter() is a special purpose iterator to treat the SBValue as
117             the head of a list data structure, where you specify the child member
118             name which points to the next item on the list and you specify the
119             end-of-list test function which takes an SBValue for an item and returns
120             True if EOL is reached and False if not.
121 
122             linked_list_iter() also detects infinite loop and bails out early.
123 
124             The end_of_list_test arg, if omitted, defaults to the __eol_test
125             function above.
126 
127             For example,
128 
129             # Get Frame #0.
130             ...
131 
132             # Get variable 'task_head'.
133             task_head = frame0.FindVariable('task_head')
134             ...
135 
136             for t in task_head.linked_list_iter('next'):
137                 print t
138             """
139             if end_of_list_test(self):
140                 return
141             item = self
142             visited = set()
143             try:
144                 while not end_of_list_test(item) and not item.GetValueAsUnsigned() in visited:
145                     visited.add(item.GetValueAsUnsigned())
146                     yield item
147                     # Prepare for the next iteration.
148                     item = item.GetChildMemberWithName(next_item_name)
149             except:
150                 # Exception occurred.  Stop the generator.
151                 pass
152 
153             return
154     %}
155 #endif
156 }
157