Debian Codegraph @

rpython/translator/revdb/gencsupp.py

raw symbols SCIP
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 # How It Works: we arrange things so that when replaying, all variables
11 # have the "same" content as they had during recording. More precisely,
12 # we divide all variables according to their type in two categories:
13 #
14 # * non-GC pointers, whose values are bitwise identical.
15 #
16 # * GC pointers: these things are "moving", in the sense that the
17 # bitwise value of a GC pointer during recording is different from
18 # its bitwise value during replaying. The object at the new
19 # address contains the "same" content as the original one, using
20 # this definition of "same".
21 #
22 # Note that most non-GC pointers are not followed during replaying: we
23 # never call external libraries nor call malloc to get pieces of raw
24 # memory. Writes to raw memory are ignored, and reads return a value
25 # recorded in the log file. However, the following are still needed:
26 #
27 # * function pointers pointing to RPython functions;
28 #
29 # * "static-immutable" structs like vtables.
30 #
31 # For now. we must make sure that these are at the same address as
32 # they were. This is very roughly checked. On Linux, it means you
33 # must run with Address Space Layout Randomization disabled. This
34 # might be fixed in the future.
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 ## def mangle_name_prebuilt_raw(database, node, S):
45 ## if (S._gckind != 'gc' and not S._hints.get('is_excdata')
46 ## and not S._hints.get('static_immutable')
47 ## and not S._hints.get('ignore_revdb')
48 ## and not S._hints.get('gcheader')):
49 ## database.all_raw_structures.append(node)
50 ## node.name = 'RPY_RDB_A(%s)' % (node.name,)
51 ## return True
52 ## else:
53 ## return False
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 # a hack for ll_call_destructor() to mean
90 # that the calls should really be done.
91 # Also used in rpython.rlib.clibffi.
92 #
93 if call_code in ('RPyGilAcquire();', 'RPyGilRelease();'):
94 # Could also work with a regular RPY_REVDB_CALL_VOID, but we
95 # use a different byte (0xFD, 0xFE instead of 0xFC) to detect more
96 # sync misses. In a single-threaded environment this 0xFD or 0xFE
97 # byte is not needed at all, but in a multi-threaded
98 # environment it ensures that during replaying, just after
99 # reading the 0xFD or 0xFE, we switch to a different thread if needed
100 # (actually implemented with stacklets).
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 ## db.all_raw_structures = []
163
164 def write_revdb_def_file(db, target_path):
165 f = target_path.open('w')
166 funcnames = sorted(db.stack_bottom_funcnames)
167 ## print("#define RDB_VERSION 0x%x" % random.randrange(0, sys.maxint), file=f)
168 ## print(file=f)
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 ## print(file=f)
181
182 ## def _base(name):
183 ## assert name.startswith('RPY_RDB_A(')
184 ## if name.endswith('.b'):
185 ## name = name[:-2]
186 ## name = name[len('RPY_RDB_A('):-1]
187 ## return name
188
189 ## rawstructs = sorted(db.all_raw_structures, key=lambda node: node.name)
190 ## print('#define RPY_RDB_A(name) (*rpy_rdb_struct.name)', file=f)
191 ## print('struct rpy_rdb_a_s {', file=f)
192 ## for i, node in enumerate(rawstructs):
193 ## print('\t%s;' % (cdecl(node.typename, '*'+_base(node.name)),), file=f)
194 ## if not rawstructs:
195 ## print('\tchar dummy;', file=f)
196 ## print('};', file=f)
197 ## print('RPY_EXTERN struct rpy_rdb_a_s rpy_rdb_struct;', file=f)
198 ## print(file=f)
199 ## print('#define RPY_RDB_STRUCT_CONTENT \\', file=f)
200 ## if not rawstructs:
201 ## print('\t0', file=f)
202 ## else:
203 ## for i, node in enumerate(rawstructs):
204 ## if i == len(rawstructs) - 1:
205 ## tail = ''
206 ## else:
207 ## tail = ', \\'
208 ## name = '&' + _base(node.name)
209 ## if node.typename != node.implementationtypename:
210 ## name = '(%s)%s' % (cdecl(node.typename, '*'), name)
211 ## print('\t%s%s' % (name, tail), file=f)
212 f.close()