| 1 |
import py, sys, zlib, re |
| 2 |
from pypy.tool import gdb_pypy |
| 3 |
|
| 4 |
class FakeGdb(object): |
| 5 |
|
| 6 |
COMMAND_NONE = -1 |
| 7 |
|
| 8 |
TYPE_CODE_PTR = 1 |
| 9 |
TYPE_CODE_ARRAY = 2 |
| 10 |
TYPE_CODE_STRUCT = 3 |
| 11 |
|
| 12 |
def __init__(self, typeids, exprs): |
| 13 |
self.typeids_z = zlib.compress(typeids) |
| 14 |
exprs['*(long*)pypy_g_rpython_memory_gctypelayout_GCData' |
| 15 |
'.gcd_inst_typeids_z'] = len(self.typeids_z) |
| 16 |
self.exprs = exprs |
| 17 |
self._parsed = [] |
| 18 |
|
| 19 |
def parse_and_eval(self, expr): |
| 20 |
self._parsed.append(expr) |
| 21 |
return self.exprs[expr] |
| 22 |
|
| 23 |
def execute(self, command): |
| 24 |
r = re.compile(r"dump binary memory (\S+) (\S+) (\S+)$") |
| 25 |
match = r.match(command) |
| 26 |
assert match |
| 27 |
fn, start, stop = match.groups() |
| 28 |
assert start == ( |
| 29 |
'(char*)(((long*)pypy_g_rpython_memory_gctypelayout_GCData' |
| 30 |
'.gcd_inst_typeids_z)+1)') |
| 31 |
assert stop == ( |
| 32 |
'(char*)(((long*)pypy_g_rpython_memory_gctypelayout_GCData' |
| 33 |
'.gcd_inst_typeids_z)+1)+%d' % (len(self.typeids_z),)) |
| 34 |
with open(fn, 'wb') as f: |
| 35 |
f.write(self.typeids_z) |
| 36 |
|
| 37 |
|
| 38 |
class Mock(object): |
| 39 |
def __init__(self, **attrs): |
| 40 |
self.__dict__.update(attrs) |
| 41 |
|
| 42 |
class Field(Mock): |
| 43 |
pass |
| 44 |
|
| 45 |
class Struct(object): |
| 46 |
code = FakeGdb.TYPE_CODE_STRUCT |
| 47 |
|
| 48 |
def __init__(self, fieldnames, tag): |
| 49 |
self._fields = [Field(name=name) for name in fieldnames] |
| 50 |
self.tag = tag |
| 51 |
|
| 52 |
def fields(self): |
| 53 |
return self._fields[:] |
| 54 |
|
| 55 |
class Pointer(object): |
| 56 |
code = FakeGdb.TYPE_CODE_PTR |
| 57 |
|
| 58 |
def __init__(self, target): |
| 59 |
self._target = target |
| 60 |
|
| 61 |
def target(self): |
| 62 |
return self._target |
| 63 |
|
| 64 |
class Value(dict): |
| 65 |
def __init__(self, *args, **kwds): |
| 66 |
type_tag = kwds.pop('type_tag', None) |
| 67 |
dict.__init__(self, *args, **kwds) |
| 68 |
self.type = Struct(self.keys(), type_tag) |
| 69 |
for key, val in self.iteritems(): |
| 70 |
if isinstance(val, dict): |
| 71 |
self[key] = Value(val) |
| 72 |
|
| 73 |
class PtrValue(Value): |
| 74 |
def __init__(self, *args, **kwds): |
| 75 |
|
| 76 |
|
| 77 |
Value.__init__(self, *args, **kwds) |
| 78 |
self.type = Pointer(self.type) |
| 79 |
|
| 80 |
def test_mock_objects(): |
| 81 |
d = {'a': 1, |
| 82 |
'b': 2, |
| 83 |
'super': { |
| 84 |
'c': 3, |
| 85 |
} |
| 86 |
} |
| 87 |
val = Value(d) |
| 88 |
assert val['a'] == 1 |
| 89 |
assert val['b'] == 2 |
| 90 |
assert isinstance(val['super'], Value) |
| 91 |
assert val['super']['c'] == 3 |
| 92 |
fields = val.type.fields() |
| 93 |
names = [f.name for f in fields] |
| 94 |
assert sorted(names) == ['a', 'b', 'super'] |
| 95 |
|
| 96 |
def test_find_field_with_suffix(): |
| 97 |
obj = Value(x_foo = 1, |
| 98 |
y_bar = 2, |
| 99 |
z_foobar = 3) |
| 100 |
assert gdb_pypy.find_field_with_suffix(obj, 'foo') == 1 |
| 101 |
assert gdb_pypy.find_field_with_suffix(obj, 'foobar') == 3 |
| 102 |
py.test.raises(KeyError, "gdb_pypy.find_field_with_suffix(obj, 'bar')") |
| 103 |
py.test.raises(KeyError, "gdb_pypy.find_field_with_suffix(obj, 'xxx')") |
| 104 |
|
| 105 |
def test_lookup(): |
| 106 |
d = {'r_super': { |
| 107 |
'_gcheader': { |
| 108 |
'h_tid': 123, |
| 109 |
} |
| 110 |
}, |
| 111 |
'r_foo': 42, |
| 112 |
} |
| 113 |
obj = Value(d) |
| 114 |
assert gdb_pypy.lookup(obj, 'foo') == 42 |
| 115 |
hdr = gdb_pypy.lookup(obj, 'gcheader') |
| 116 |
assert hdr['h_tid'] == 123 |
| 117 |
|
| 118 |
def exprmember(n): |
| 119 |
if sys.maxint < 2**32: |
| 120 |
TIDT = "int*" |
| 121 |
else: |
| 122 |
TIDT = "char*" |
| 123 |
return ('((%s)(&pypy_g_typeinfo.member%d)) - (%s)&pypy_g_typeinfo' |
| 124 |
% (TIDT, n, TIDT)) |
| 125 |
|
| 126 |
def test_load_typeids(tmpdir): |
| 127 |
typeids = """ |
| 128 |
member0 ? |
| 129 |
member1 GcStruct xxx {} |
| 130 |
""".lstrip() |
| 131 |
exprs = {exprmember(1): 111} |
| 132 |
gdb = FakeGdb(typeids, exprs) |
| 133 |
cmd = gdb_pypy.RPyType(gdb) |
| 134 |
typeids = cmd.load_typeids() |
| 135 |
assert typeids[0] == '(null typeid)' |
| 136 |
assert typeids[111] == 'GcStruct xxx {}' |
| 137 |
py.test.raises(KeyError, "typeids[50]") |
| 138 |
py.test.raises(KeyError, "typeids[150]") |
| 139 |
|
| 140 |
def test_RPyType(tmpdir): |
| 141 |
typeids = """ |
| 142 |
member0 ? |
| 143 |
member1 GcStruct xxx {} |
| 144 |
member2 GcStruct yyy {} |
| 145 |
member3 GcStruct zzz {} |
| 146 |
""".lstrip() |
| 147 |
|
| 148 |
d = {'r_super': { |
| 149 |
'_gcheader': { |
| 150 |
'h_tid': 123, |
| 151 |
} |
| 152 |
}, |
| 153 |
'r_foo': 42, |
| 154 |
} |
| 155 |
myvar = Value(d) |
| 156 |
exprs = { |
| 157 |
'*myvar': myvar, |
| 158 |
exprmember(1): 0, |
| 159 |
exprmember(2): 123, |
| 160 |
exprmember(3): 456, |
| 161 |
} |
| 162 |
gdb = FakeGdb(typeids, exprs) |
| 163 |
cmd = gdb_pypy.RPyType(gdb) |
| 164 |
assert cmd.do_invoke('*myvar', True) == 'GcStruct yyy {}' |
| 165 |
|
| 166 |
def test_pprint_string(monkeypatch): |
| 167 |
d = {'_gcheader': { |
| 168 |
'h_tid': 123 |
| 169 |
}, |
| 170 |
'rs_hash': 456, |
| 171 |
'rs_chars': { |
| 172 |
'length': 6, |
| 173 |
'items': map(ord, 'foobar'), |
| 174 |
} |
| 175 |
} |
| 176 |
p_string = PtrValue(d, type_tag='pypy_rpy_string0') |
| 177 |
printer = gdb_pypy.RPyStringPrinter.lookup(p_string, FakeGdb) |
| 178 |
assert printer.to_string() == "r'foobar'" |
| 179 |
monkeypatch.setattr(gdb_pypy, 'MAX_DISPLAY_LENGTH', 5) |
| 180 |
assert printer.to_string() == "r'fooba...'" |
| 181 |
|
| 182 |
def test_pprint_list(): |
| 183 |
d = {'_gcheader': { |
| 184 |
'h_tid': 123 |
| 185 |
}, |
| 186 |
'l_length': 3, |
| 187 |
'l_items': |
| 188 |
|
| 189 |
{'_gcheader': { |
| 190 |
'h_tid': 456 |
| 191 |
}, |
| 192 |
'length': 5, |
| 193 |
'items': [40, 41, 42, -1, -2], |
| 194 |
} |
| 195 |
} |
| 196 |
mylist = PtrValue(d, type_tag='pypy_list0') |
| 197 |
printer = gdb_pypy.RPyListPrinter.lookup(mylist, FakeGdb) |
| 198 |
assert printer.to_string() == 'r[40, 41, 42] (len=3, alloc=5)' |
| 199 |
|
| 200 |
mylist.type.target().tag = 'pypy_list1234' |
| 201 |
printer = gdb_pypy.RPyListPrinter.lookup(mylist, FakeGdb) |
| 202 |
assert printer.to_string() == 'r[40, 41, 42] (len=3, alloc=5)' |
| 203 |
|
| 204 |
mylist.type.target().tag = None |
| 205 |
assert gdb_pypy.RPyListPrinter.lookup(mylist, FakeGdb) is None |
| 206 |
|
| 207 |
def test_pprint_array(): |
| 208 |
d = {'_gcheder': {'h_tid': 234}, 'length': 3, 'items': [20, 21, 22]} |
| 209 |
mylist = PtrValue(d, type_tag='pypy_array1') |
| 210 |
printer = gdb_pypy.RPyListPrinter.lookup(mylist, FakeGdb) |
| 211 |
assert printer.to_string() == 'r[20, 21, 22] (len=3)' |
| 212 |
|
| 213 |
def test_pprint_null_list(): |
| 214 |
mylist = PtrValue({}, type_tag='pypy_list1') |
| 215 |
printer = gdb_pypy.RPyListPrinter.lookup(mylist, FakeGdb) |
| 216 |
assert printer.to_string() == 'r(null_list)' |
| 217 |
|
| 218 |
def test_pprint_null_array(): |
| 219 |
mylist = PtrValue({}, type_tag='pypy_array1') |
| 220 |
printer = gdb_pypy.RPyListPrinter.lookup(mylist, FakeGdb) |
| 221 |
assert printer.to_string() == 'r(null_array)' |
| 222 |
|
| 223 |
def test_typeidsmap(): |
| 224 |
gdb = FakeGdb('', {exprmember(1): 111, |
| 225 |
exprmember(2): 222, |
| 226 |
exprmember(3): 333}) |
| 227 |
typeids = gdb_pypy.TypeIdsMap(["member0 ?\n", |
| 228 |
"member1 FooBar\n", |
| 229 |
"member2 Baz\n", |
| 230 |
"member3 Bok\n"], gdb) |
| 231 |
assert gdb._parsed == [] |
| 232 |
assert typeids.get(111) == "FooBar" |
| 233 |
assert gdb._parsed == [exprmember(2), exprmember(1)] |
| 234 |
assert typeids.get(222) == "Baz" |
| 235 |
assert gdb._parsed == [exprmember(2), exprmember(1)] |
| 236 |
assert typeids.get(333) == "Bok" |
| 237 |
assert gdb._parsed == [exprmember(2), exprmember(1), exprmember(3)] |
| 238 |
assert typeids.get(400) == None |
| 239 |
assert typeids.get(300) == None |
| 240 |
assert typeids.get(200) == None |
| 241 |
assert typeids.get(100) == None |