forked from ayuckhulk/lldb-qt-formatters
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.py
286 lines (240 loc) · 8.96 KB
/
helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#
# LLDB data formatter helpers
# Copyright 2016 Aetf <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License or (at your option) version 3 or any later version
# accepted by the membership of KDE e.V. (or its successor approved
# by the membership of KDE e.V.), which shall act as a proxy
# defined in Section 14 of version 3 of the license.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# BEGIN: Utilities for wrapping differences of Python 2.x and Python 3
# Inspired by https://pythonhosted.org/six/
from __future__ import print_function
import sys
import lldb
# Useful for very coarse version differentiation.
PY2 = sys.version_info[0] == 2
PY3 = sys.version_info[0] == 3
# create Python 2.x & 3.x compatible iterator base
if PY3:
Iterator = object
else:
class Iterator(object):
def next(self):
return type(self).__next__(self)
if PY3:
unichr = chr
unicode = str
else:
unichr = unichr
# END
def canonicalized_type_name(name):
"""Canonicalize the type name for FindFirstType usage.
+ 1 space between template arguments (after comma)
+ no space before pointer *
otherwise FindFirstType returns None
"""
return name.replace(' ', '').replace(',', ', ')
def quote(string, quote='"'):
"""Quote a string so it's suitable to be used in quote"""
if isinstance(string, unicode):
ls = []
for uch in string:
code = ord(uch)
if code > 255:
ls += '\\u{:04x}'.format(code)
elif code >= 127:
ls += '\\x{:02x}'.format(code)
elif uch == quote or uch == '\\':
ls += '\\' + chr(code)
elif code == 0:
ls += '\\x00'
else:
ls += chr(code)
return quote + ''.join(ls) + quote
else:
return '{q}{s}{q}'.format(s=string.replace('\\', '\\\\').replace(quote, '\\' + quote),
q=quote)
def unquote(data, quote='"'):
"""Unquote a string"""
if data.startswith(quote) and data.endswith(quote):
data = data[1:-1]
ls = []
esc = False
for ch in data:
if esc:
ls.append(ch)
esc = False
else:
if ch == '\\':
esc = True
else:
ls.append(ch)
if esc:
print('WARNING: unpaired escape')
data = ''.join(ls)
return data
def invoke(val, method, args=''):
"""Try to invoke a method on val, args are passed in as an expression string"""
# first try to get a valid frame
frame = None
for f in [val.frame, lldb.frame, val.process.selected_thread.GetFrameAtIndex(0)]:
if f.IsValid():
frame = f
break
if frame is None:
return lldb.SBValue()
# second try to get a pointer to val
if val.GetType().IsPointerType():
ptype = val.GetType()
addr = val.GetValueAsUnsigned(0)
else:
ptype = val.GetType().GetPointerType()
addr = val.AddressOf().GetValueAsUnsigned(0)
# third, build expression
expr = 'reinterpret_cast<const {}>({})->{}({})'.format(ptype.GetName(), addr, method, args)
res = frame.EvaluateExpression(expr)
# if not res.IsValid():
# print 'Expr {} on value {} failed'.format(expr, val.GetName())
return res
def rename(name, val):
"""Rename a SBValue"""
return val.CreateValueFromData(name, val.GetData(), val.GetType())
def toSBPointer(valobj, addr, pointee_type):
"""Convert a addr integer to SBValue"""
addr = addr & 0xFFFFFFFFFFFFFFFF # force unsigned
return valobj.CreateValueFromAddress(None, addr, pointee_type).AddressOf()
def validAddr(valobj, addr):
"""Test if a address is valid"""
return toSBPointer(valobj, addr,
valobj.GetType().GetBasicType(lldb.eBasicTypeVoid).GetPointerType()).IsValid()
def validPointer(pointer):
"""Test if a SBValue pointer is valid"""
if not pointer.IsValid():
return False
if pointer.GetValueAsUnsigned(0) == 0:
return False
return toSBPointer(pointer, pointer.GetValueAsUnsigned(0), pointer.GetType().GetPointeeType()).IsValid()
class AutoCacheValue(object):
"""An object that can create itself when needed and cache the result"""
def __init__(self, creator):
super(AutoCacheValue, self).__init__()
self.creator = creator
self.cache = None
self.cached = False
def get(self):
if not self.cached:
self.cache = self.creator()
self.cached = True
return self.cache
class HiddenMemberProvider(object):
"""A lldb synthetic provider that can provide hidden children.
Original children is exposed in this way"""
@staticmethod
def _capping_size():
return 255
def __init__(self, valobj, internal_dict):
self.valobj = valobj
# number of normally visible children
self._num_children = 0
# cache for visible children
self._members = []
# cache for hidden children
self._hiddens = []
# child name to index
self._name2idx = {}
# whether to add original children
self._add_original = True
# some useful info
process = self.valobj.GetProcess()
self._endianness = process.GetByteOrder()
self._pointer_size = process.GetAddressByteSize()
self._char_type = valobj.GetType().GetBasicType(lldb.eBasicTypeChar)
def has_children(self):
return self._num_children != 0
def num_children(self):
return self._num_children
def get_child_index(self, name):
if name in self._name2idx:
return self._name2idx[name]
return None
def get_child_at_index(self, idx):
if not self.valobj.IsValid():
return None
if idx < 0:
return None
elif idx < self._num_children:
child = self._members[idx]
# These are hidden children, which won't be queried by lldb, but we know
# they are there, so we can use them in summary provider, to avoid another
# fetch from the inferior, and don't shadow original children
elif idx < self._num_children + len(self._hiddens):
child = self._hiddens[idx - self._num_children]
else:
return None
if isinstance(child, AutoCacheValue):
child = child.get()
return child
@staticmethod
def _getName(var):
if isinstance(var, lldb.SBValue):
return var.GetName()
else:
return var[0]
def update(self):
self._num_children = -1
self._members = []
self._hiddens = []
self._name2idx = {}
if not self.valobj.IsValid():
return
# call _update on subclass
self._update()
# add valobj's original children as hidden children,
# must be called after self._update, so subclass has chance
# to disable it.
if self._add_original:
for v in self.valobj:
self._addChild(v, hidden=True)
# update num_children
if self._num_children < 0:
self._num_children = len(self._members)
# build name to index lookup, hidden value first, so normal value takes precedence
self._name2idx = {
self._getName(self._hiddens[idx]): idx + self._num_children
for idx in range(0, len(self._hiddens))
}
self._name2idx.update({
self._getName(self._members[idx]): idx
for idx in range(0, self._num_children)
})
def _update(self):
"""override in subclass"""
pass
def _addChild(self, var, hidden=False):
if not isinstance(var, lldb.SBValue):
# special handling for (name, expr) tuple of string constants
if len(var) != 2:
print('error, const char[] value should be a tuple with two elements, it is', var)
name, content = var
if isinstance(content, unicode):
content = content.encode()
try:
char_arr_type = self._char_type.GetArrayType(len(content));
strdata = lldb.SBData.CreateDataFromCString(self._endianness, self._pointer_size, content)
var = self.valobj.CreateValueFromData(name, strdata, char_arr_type)
except:
pass
cache = self._hiddens if hidden else self._members
cache.append(var)