| 1 |
from __future__ import print_function |
| 2 |
|
| 3 |
import py, random, sys |
| 4 |
from rpython.rtyper.lltypesystem import lltype, llmemory, rffi, rstr |
| 5 |
from rpython.rtyper.lltypesystem.lloperation import LL_OPERATIONS |
| 6 |
from rpython.translator.c.support import cdecl |
| 7 |
from rpython.rlib import exports, revdb |
| 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 |
def extra_files(): |
| 39 |
srcdir = py.path.local(__file__).join('..', 'src-revdb') |
| 40 |
return [ |
| 41 |
srcdir / 'revdb.c', |
| 42 |
] |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
def prepare_function(funcgen): |
| 56 |
if getattr(getattr(funcgen.graph, 'func', None), '_revdb_c_only_', False): |
| 57 |
extra_enter_text = 'RPY_REVDB_C_ONLY_ENTER' |
| 58 |
extra_return_text = 'RPY_REVDB_C_ONLY_LEAVE' |
| 59 |
return extra_enter_text, extra_return_text |
| 60 |
stack_bottom = False |
| 61 |
for block in funcgen.graph.iterblocks(): |
| 62 |
for op in block.operations: |
| 63 |
if op.opname == 'gc_stack_bottom': |
| 64 |
stack_bottom = True |
| 65 |
if stack_bottom: |
| 66 |
name = funcgen.functionname |
| 67 |
funcgen.db.stack_bottom_funcnames.append(name) |
| 68 |
extra_enter_text = '\n'.join( |
| 69 |
['/* this function is a callback */', |
| 70 |
'RPY_REVDB_CALLBACKLOC(RPY_CALLBACKLOC_%s);' % name] + |
| 71 |
['\t' + emit('/*arg*/', funcgen.lltypename(v), funcgen.expr(v)) |
| 72 |
for v in funcgen.graph.getargs()]) |
| 73 |
extra_return_text = '/* RPY_CALLBACK_LEAVE(); */' |
| 74 |
return extra_enter_text, extra_return_text |
| 75 |
else: |
| 76 |
return None, None |
| 77 |
|
| 78 |
def emit_void(normal_code): |
| 79 |
return 'RPY_REVDB_EMIT_VOID(%s);' % (normal_code,) |
| 80 |
|
| 81 |
def emit(normal_code, tp, value): |
| 82 |
if tp == 'void @': |
| 83 |
return emit_void(normal_code) |
| 84 |
return 'RPY_REVDB_EMIT(%s, %s, %s);' % (normal_code, cdecl(tp, '_e'), value) |
| 85 |
|
| 86 |
def emit_residual_call(funcgen, call_code, v_result, expr_result): |
| 87 |
if hasattr(funcgen, 'revdb_do_next_call'): |
| 88 |
del funcgen.revdb_do_next_call |
| 89 |
return call_code |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
if call_code in ('RPyGilAcquire();', 'RPyGilRelease();'): |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
if call_code == 'RPyGilAcquire();': |
| 102 |
return 'RPY_REVDB_CALL_GIL_ACQUIRE();' |
| 103 |
else: |
| 104 |
return 'RPY_REVDB_CALL_GIL_RELEASE();' |
| 105 |
|
| 106 |
tp = funcgen.lltypename(v_result) |
| 107 |
if tp == 'void @': |
| 108 |
return 'RPY_REVDB_CALL_VOID(%s);' % (call_code,) |
| 109 |
return 'RPY_REVDB_CALL(%s, %s, %s);' % (call_code, cdecl(tp, '_e'), |
| 110 |
expr_result) |
| 111 |
|
| 112 |
def record_malloc_uid(expr): |
| 113 |
return ' RPY_REVDB_REC_UID(%s);' % (expr,) |
| 114 |
|
| 115 |
def boehm_register_finalizer(funcgen, op): |
| 116 |
return 'rpy_reverse_db_register_destructor(%s, %s);' % ( |
| 117 |
funcgen.expr(op.args[0]), funcgen.expr(op.args[1])) |
| 118 |
|
| 119 |
def cast_gcptr_to_int(funcgen, op): |
| 120 |
return '%s = RPY_REVDB_CAST_PTR_TO_INT(%s);' % ( |
| 121 |
funcgen.expr(op.result), funcgen.expr(op.args[0])) |
| 122 |
|
| 123 |
set_revdb_protected = set(opname for opname, opdesc in LL_OPERATIONS.items() |
| 124 |
if opdesc.revdb_protect) |
| 125 |
|
| 126 |
|
| 127 |
def prepare_database(db): |
| 128 |
FUNCPTR = lltype.Ptr(lltype.FuncType([revdb._CMDPTR, lltype.Ptr(rstr.STR)], |
| 129 |
lltype.Void)) |
| 130 |
ALLOCFUNCPTR = lltype.Ptr(lltype.FuncType([rffi.LONGLONG, llmemory.GCREF], |
| 131 |
lltype.Void)) |
| 132 |
|
| 133 |
bk = db.translator.annotator.bookkeeper |
| 134 |
cmds = getattr(db.translator, 'revdb_commands', {}) |
| 135 |
numcmds = [(num, func) for (num, func) in cmds.items() |
| 136 |
if isinstance(num, int)] |
| 137 |
|
| 138 |
S = lltype.Struct('RPY_REVDB_COMMANDS', |
| 139 |
('names', lltype.FixedSizeArray(rffi.INT, len(numcmds) + 1)), |
| 140 |
('funcs', lltype.FixedSizeArray(FUNCPTR, len(numcmds))), |
| 141 |
('alloc', ALLOCFUNCPTR)) |
| 142 |
s = lltype.malloc(S, flavor='raw', immortal=True, zero=True) |
| 143 |
|
| 144 |
i = 0 |
| 145 |
for name, func in cmds.items(): |
| 146 |
fnptr = lltype.getfunctionptr(bk.getdesc(func).getuniquegraph()) |
| 147 |
if isinstance(name, int): |
| 148 |
assert name != 0 |
| 149 |
s.names[i] = rffi.cast(rffi.INT, name) |
| 150 |
s.funcs[i] = fnptr |
| 151 |
i += 1 |
| 152 |
elif name == "ALLOCATING": |
| 153 |
s.alloc = fnptr |
| 154 |
else: |
| 155 |
raise AssertionError("bad tag in register_debug_command(): %r" |
| 156 |
% (name,)) |
| 157 |
|
| 158 |
exports.EXPORTS_obj2name[s._as_obj()] = 'rpy_revdb_commands' |
| 159 |
db.get(s) |
| 160 |
|
| 161 |
db.stack_bottom_funcnames = [] |
| 162 |
|
| 163 |
|
| 164 |
def write_revdb_def_file(db, target_path): |
| 165 |
f = target_path.open('w') |
| 166 |
funcnames = sorted(db.stack_bottom_funcnames) |
| 167 |
|
| 168 |
|
| 169 |
for i, fn in enumerate(funcnames): |
| 170 |
print('#define RPY_CALLBACKLOC_%s %d' % (fn, i), file=f) |
| 171 |
print(file=f) |
| 172 |
print('#define RPY_CALLBACKLOCS \\', file=f) |
| 173 |
funcnames = funcnames or ['NULL'] |
| 174 |
for i, fn in enumerate(funcnames): |
| 175 |
if i == len(funcnames) - 1: |
| 176 |
tail = '' |
| 177 |
else: |
| 178 |
tail = ', \\' |
| 179 |
print('\t(void *)%s%s' % (fn, tail), file=f) |
| 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 |
f.close() |