| 1 |
from __future__ import print_function |
| 2 |
|
| 3 |
import py |
| 4 |
import sys |
| 5 |
import weakref |
| 6 |
import struct |
| 7 |
import os |
| 8 |
import platform |
| 9 |
from rpython.rlib import jit |
| 10 |
from rpython.tool.udir import udir |
| 11 |
from rpython.tool.version import rpythonroot |
| 12 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 13 |
from rpython.translator.tool.cbuild import ExternalCompilationInfo |
| 14 |
from rpython.jit.metainterp import resoperation as resoperations |
| 15 |
from rpython.jit.metainterp.resoperation import rop |
| 16 |
from rpython.jit.metainterp.history import ConstInt, ConstFloat, ConstPtr |
| 17 |
from rpython.rlib.rarithmetic import r_longlong |
| 18 |
from rpython.rtyper.lltypesystem import lltype, llmemory, rffi |
| 19 |
from rpython.rlib.objectmodel import compute_unique_id, always_inline |
| 20 |
from rpython.rlib.objectmodel import we_are_translated, specialize |
| 21 |
from rpython.rlib.unroll import unrolling_iterable |
| 22 |
from rpython.rlib.jit_hooks import register_helper |
| 23 |
from rpython.annotator import model as annmodel |
| 24 |
|
| 25 |
|
| 26 |
ROOT = py.path.local(rpythonroot).join('rpython', 'rlib', 'rjitlog') |
| 27 |
SRC = ROOT.join('src') |
| 28 |
test_jitlog_name = 'JITLOG_FORTESTS' |
| 29 |
|
| 30 |
_libs = [] |
| 31 |
if sys.platform.startswith('linux'): |
| 32 |
_libs = ['dl'] |
| 33 |
eci_kwds = dict( |
| 34 |
include_dirs = [SRC], |
| 35 |
includes = ['rjitlog.h'], |
| 36 |
libraries = _libs, |
| 37 |
separate_module_files = [SRC.join('rjitlog.c')], |
| 38 |
|
| 39 |
post_include_bits=['#define RPYTHON_JITLOG\n'], |
| 40 |
) |
| 41 |
eci = ExternalCompilationInfo(**eci_kwds) |
| 42 |
|
| 43 |
|
| 44 |
jitlog_init = rffi.llexternal("jitlog_init", [rffi.INT], |
| 45 |
rffi.CCHARP, compilation_info=eci) |
| 46 |
jitlog_try_init_using_env = rffi.llexternal("jitlog_try_init_using_env", |
| 47 |
[], lltype.Void, compilation_info=eci) |
| 48 |
jitlog_write_marked = rffi.llexternal("jitlog_write_marked", |
| 49 |
[rffi.CCHARP, rffi.INT], |
| 50 |
lltype.Void, compilation_info=eci, |
| 51 |
releasegil=False) |
| 52 |
jitlog_enabled = rffi.llexternal("jitlog_enabled", [], rffi.INT, |
| 53 |
compilation_info=eci, |
| 54 |
releasegil=False) |
| 55 |
jitlog_teardown = rffi.llexternal("jitlog_teardown", [], lltype.Void, |
| 56 |
compilation_info=eci) |
| 57 |
|
| 58 |
class JitlogError(Exception): |
| 59 |
def __init__(self, msg): |
| 60 |
self.msg = msg |
| 61 |
def __str__(self): |
| 62 |
return self.msg |
| 63 |
|
| 64 |
@register_helper(None) |
| 65 |
def stats_flush_trace_counts(warmrunnerdesc): |
| 66 |
if not we_are_translated(): |
| 67 |
return |
| 68 |
warmrunnerdesc.metainterp_sd.cpu.assembler.flush_trace_counters() |
| 69 |
|
| 70 |
@jit.dont_look_inside |
| 71 |
def enable_jitlog(fileno): |
| 72 |
|
| 73 |
p_error = jitlog_init(fileno) |
| 74 |
if p_error: |
| 75 |
raise JitlogError(rffi.charp2str(p_error)) |
| 76 |
blob = assemble_header() |
| 77 |
jitlog_write_marked(MARK_JITLOG_HEADER + blob, len(blob) + 1) |
| 78 |
|
| 79 |
def disable_jitlog(): |
| 80 |
stats_flush_trace_counts(None) |
| 81 |
jitlog_teardown() |
| 82 |
|
| 83 |
|
| 84 |
def commonprefix(a,b): |
| 85 |
"Given a list of pathnames, returns the longest common leading component" |
| 86 |
assert a is not None |
| 87 |
assert b is not None |
| 88 |
la = len(a) |
| 89 |
lb = len(b) |
| 90 |
c = min(la,lb) |
| 91 |
if c == 0: |
| 92 |
return "" |
| 93 |
for i in range(c): |
| 94 |
if a[i] != b[i]: |
| 95 |
return a[:i] |
| 96 |
return a |
| 97 |
|
| 98 |
@always_inline |
| 99 |
def encode_str(string): |
| 100 |
val = len(string) |
| 101 |
return ''.join([chr((val >> 0) & 0xff), |
| 102 |
chr((val >> 8) & 0xff), |
| 103 |
chr((val >> 16) & 0xff), |
| 104 |
chr((val >> 24) & 0xff), |
| 105 |
string]) |
| 106 |
|
| 107 |
@always_inline |
| 108 |
def encode_le_16bit(val): |
| 109 |
return chr((val >> 0) & 0xff) + chr((val >> 8) & 0xff) |
| 110 |
|
| 111 |
@always_inline |
| 112 |
def encode_le_32bit(val): |
| 113 |
return ''.join([chr((val >> 0) & 0xff), |
| 114 |
chr((val >> 8) & 0xff), |
| 115 |
chr((val >> 16) & 0xff), |
| 116 |
chr((val >> 24) & 0xff)]) |
| 117 |
|
| 118 |
@always_inline |
| 119 |
def encode_le_64bit(val): |
| 120 |
val = r_longlong(val) |
| 121 |
return ''.join([chr((val >> 0) & 0xff), |
| 122 |
chr((val >> 8) & 0xff), |
| 123 |
chr((val >> 16) & 0xff), |
| 124 |
chr((val >> 24) & 0xff), |
| 125 |
chr((val >> 32) & 0xff), |
| 126 |
chr((val >> 40) & 0xff), |
| 127 |
chr((val >> 48) & 0xff), |
| 128 |
chr((val >> 56)& 0xff)]) |
| 129 |
|
| 130 |
@always_inline |
| 131 |
def encode_le_addr(val): |
| 132 |
if IS_32_BIT: |
| 133 |
return encode_le_32bit(val) |
| 134 |
else: |
| 135 |
return encode_le_64bit(val) |
| 136 |
|
| 137 |
def encode_type(type, value): |
| 138 |
if type == "s": |
| 139 |
return encode_str(value) |
| 140 |
elif type == "q": |
| 141 |
return encode_le_64bit(value) |
| 142 |
elif type == "i": |
| 143 |
return encode_le_32bit(value) |
| 144 |
elif type == "h": |
| 145 |
return encode_le_16bit(value) |
| 146 |
else: |
| 147 |
raise NotImplementedError |
| 148 |
|
| 149 |
|
| 150 |
MP_STR = (0x0, "s") |
| 151 |
MP_INT = (0x0, "i") |
| 152 |
|
| 153 |
|
| 154 |
MP_FILENAME = (0x1, "s") |
| 155 |
MP_LINENO = (0x2, "i") |
| 156 |
MP_INDEX = (0x4, "i") |
| 157 |
MP_SCOPE = (0x8, "s") |
| 158 |
MP_OPCODE = (0x10, "s") |
| 159 |
|
| 160 |
class WrappedValue(object): |
| 161 |
def encode(self, log, i, compressor): |
| 162 |
raise NotImplementedError |
| 163 |
|
| 164 |
class StringValue(WrappedValue): |
| 165 |
def __init__(self, sem_type, gen_type, value): |
| 166 |
self.value = value |
| 167 |
|
| 168 |
def encode(self, log, i, compressor): |
| 169 |
str_value = self.value |
| 170 |
last_prefix = compressor.get_last_written(i) |
| 171 |
cp = compressor.compress(i, str_value) |
| 172 |
if cp is None: |
| 173 |
return b'\xff' + encode_str(str_value) |
| 174 |
|
| 175 |
else: |
| 176 |
cp_len = len(cp) |
| 177 |
if cp == last_prefix: |
| 178 |
|
| 179 |
pass |
| 180 |
else: |
| 181 |
compressor.write(log, i, cp) |
| 182 |
if len(str_value) == len(cp): |
| 183 |
return b'\xef' |
| 184 |
return b'\x00' + encode_str(str_value[len(cp):]) |
| 185 |
|
| 186 |
class IntValue(WrappedValue): |
| 187 |
def __init__(self, sem_type, gen_type, value): |
| 188 |
self.value = value |
| 189 |
|
| 190 |
def encode(self, log, i, prefixes): |
| 191 |
return b'\x00' + encode_le_64bit(self.value) |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
@specialize.argtype(2) |
| 199 |
def wrap(sem_type, gen_type, value): |
| 200 |
if isinstance(value, int): |
| 201 |
return IntValue(sem_type, gen_type, value) |
| 202 |
elif isinstance(value, str): |
| 203 |
return StringValue(sem_type, gen_type, value) |
| 204 |
raise NotImplementedError |
| 205 |
|
| 206 |
def returns(*args): |
| 207 |
""" Decorate your get_location function to specify the types. |
| 208 |
Use MP_* constant as parameters. An example impl for get_location |
| 209 |
would return the following: |
| 210 |
|
| 211 |
@returns(MP_FILENAME, MP_LINENO) |
| 212 |
def get_location(...): |
| 213 |
return ("a.py", 0) |
| 214 |
""" |
| 215 |
def decor(method): |
| 216 |
method._loc_types = args |
| 217 |
return method |
| 218 |
return decor |
| 219 |
|
| 220 |
JITLOG_VERSION = 4 |
| 221 |
JITLOG_VERSION_16BIT_LE = struct.pack("<H", JITLOG_VERSION) |
| 222 |
|
| 223 |
marks = [ |
| 224 |
('INPUT_ARGS',), |
| 225 |
('RESOP_META',), |
| 226 |
('RESOP',), |
| 227 |
('RESOP_DESCR',), |
| 228 |
('ASM_ADDR',), |
| 229 |
('ASM',), |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
('TRACE',), |
| 234 |
|
| 235 |
('TRACE_OPT',), |
| 236 |
|
| 237 |
('TRACE_ASM',), |
| 238 |
|
| 239 |
|
| 240 |
('STITCH_BRIDGE',), |
| 241 |
|
| 242 |
('START_TRACE',), |
| 243 |
|
| 244 |
('JITLOG_COUNTER',), |
| 245 |
('INIT_MERGE_POINT',), |
| 246 |
|
| 247 |
('JITLOG_HEADER',), |
| 248 |
('MERGE_POINT',), |
| 249 |
('COMMON_PREFIX',), |
| 250 |
('ABORT_TRACE',), |
| 251 |
('SOURCE_CODE',), |
| 252 |
('REDIRECT_ASSEMBLER',), |
| 253 |
('TMP_CALLBACK',), |
| 254 |
] |
| 255 |
|
| 256 |
start = 0x11 |
| 257 |
for mark, in marks: |
| 258 |
globals()['MARK_' + mark] = chr(start) |
| 259 |
start += 1 |
| 260 |
|
| 261 |
if __name__ == "__main__": |
| 262 |
print("# generated constants from rpython/rlib/jitlog.py") |
| 263 |
print('import struct') |
| 264 |
print('MARK_JITLOG_START = struct.pack("b", %s)' % hex(0x10)) |
| 265 |
for mark, in marks: |
| 266 |
nmr = globals()['MARK_' + mark] |
| 267 |
h = hex(ord(nmr)) |
| 268 |
print('%s = struct.pack("b", %s)' % ('MARK_' + mark, h)) |
| 269 |
print('MARK_JITLOG_END = struct.pack("b", %s)' % hex(start)) |
| 270 |
for key,value in locals().items(): |
| 271 |
if key.startswith("MP_"): |
| 272 |
print('%s = (%s,"%s")' % (key, hex(value[0]), value[1])) |
| 273 |
print('SEM_TYPE_NAMES = {') |
| 274 |
for key,value in locals().items(): |
| 275 |
if key.startswith("MP_") and value[0] != 0: |
| 276 |
print(' %s: "%s",' % (hex(value[0]), key[3:].lower())) |
| 277 |
print('}') |
| 278 |
|
| 279 |
MP_STR = (0x0, "s") |
| 280 |
MP_INT = (0x0, "i") |
| 281 |
|
| 282 |
|
| 283 |
MP_FILENAME = (0x1, "s") |
| 284 |
MP_LINENO = (0x2, "i") |
| 285 |
MP_INDEX = (0x4, "i") |
| 286 |
MP_SCOPE = (0x8, "s") |
| 287 |
MP_OPCODE = (0x10, "s") |
| 288 |
|
| 289 |
del marks |
| 290 |
del start |
| 291 |
|
| 292 |
IS_32_BIT = sys.maxint == 2**31-1 |
| 293 |
|
| 294 |
MACHINE_NAME = platform.machine() |
| 295 |
|
| 296 |
def assemble_header(): |
| 297 |
version = JITLOG_VERSION_16BIT_LE |
| 298 |
count = len(resoperations.opname) |
| 299 |
is_32bit = chr(0x1) |
| 300 |
if not IS_32_BIT: |
| 301 |
is_32bit = chr(0x0) |
| 302 |
content = [version, is_32bit, encode_str(MACHINE_NAME), |
| 303 |
MARK_RESOP_META, encode_le_16bit(count)] |
| 304 |
for opnum, opname in resoperations.opname.items(): |
| 305 |
content.append(encode_le_16bit(opnum)) |
| 306 |
content.append(encode_str(opname.lower())) |
| 307 |
return ''.join(content) |
| 308 |
|
| 309 |
def _log_jit_counter(struct): |
| 310 |
if not jitlog_enabled(): |
| 311 |
return |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
list = [MARK_JITLOG_COUNTER, encode_le_addr(struct.number), |
| 316 |
struct.type, encode_le_64bit(struct.i)] |
| 317 |
content = ''.join(list) |
| 318 |
jitlog_write_marked(content, len(content)) |
| 319 |
|
| 320 |
def redirect_assembler(oldtoken, newtoken, asm_adr): |
| 321 |
if not jitlog_enabled(): |
| 322 |
return |
| 323 |
descr_nmr = compute_unique_id(oldtoken) |
| 324 |
new_descr_nmr = compute_unique_id(newtoken) |
| 325 |
list = [MARK_REDIRECT_ASSEMBLER, encode_le_addr(descr_nmr), |
| 326 |
encode_le_addr(new_descr_nmr), encode_le_addr(asm_adr)] |
| 327 |
content = ''.join(list) |
| 328 |
jitlog_write_marked(content, len(content)) |
| 329 |
|
| 330 |
def tmp_callback(looptoken): |
| 331 |
mark_tmp_callback = ''.join([ |
| 332 |
MARK_TMP_CALLBACK, |
| 333 |
encode_le_addr(compute_unique_id(looptoken)), |
| 334 |
encode_le_64bit(looptoken.number)]) |
| 335 |
jitlog_write_marked(mark_tmp_callback, len(mark_tmp_callback)) |
| 336 |
|
| 337 |
class JitLogger(object): |
| 338 |
def __init__(self, cpu=None): |
| 339 |
self.cpu = cpu |
| 340 |
self.memo = {} |
| 341 |
self.trace_id = 0 |
| 342 |
self.metainterp_sd = None |
| 343 |
|
| 344 |
self.logger_ops = None |
| 345 |
self.logger_noopt = None |
| 346 |
|
| 347 |
def setup_once(self): |
| 348 |
if jitlog_enabled(): |
| 349 |
return |
| 350 |
jitlog_try_init_using_env() |
| 351 |
if not jitlog_enabled(): |
| 352 |
return |
| 353 |
blob = assemble_header() |
| 354 |
jitlog_write_marked(MARK_JITLOG_HEADER + blob, len(blob) + 1) |
| 355 |
|
| 356 |
def finish(self): |
| 357 |
jitlog_teardown() |
| 358 |
|
| 359 |
def next_id(self): |
| 360 |
self.trace_id += 1 |
| 361 |
return self.trace_id |
| 362 |
|
| 363 |
def start_new_trace(self, metainterp_sd, faildescr=None, entry_bridge=False, jd_name=""): |
| 364 |
|
| 365 |
self.trace_id += 1 |
| 366 |
if not jitlog_enabled(): |
| 367 |
return |
| 368 |
self.metainterp_sd = metainterp_sd |
| 369 |
content = [encode_le_addr(self.trace_id)] |
| 370 |
if faildescr: |
| 371 |
content.append(encode_str('bridge')) |
| 372 |
descrnmr = compute_unique_id(faildescr) |
| 373 |
content.append(encode_le_addr(descrnmr)) |
| 374 |
else: |
| 375 |
content.append(encode_str('loop')) |
| 376 |
content.append(encode_le_addr(int(entry_bridge))) |
| 377 |
content.append(encode_str(jd_name)) |
| 378 |
self._write_marked(MARK_START_TRACE, ''.join(content)) |
| 379 |
|
| 380 |
def trace_aborted(self): |
| 381 |
if not jitlog_enabled(): |
| 382 |
return |
| 383 |
self._write_marked(MARK_ABORT_TRACE, encode_le_addr(self.trace_id)) |
| 384 |
|
| 385 |
def _write_marked(self, mark, line): |
| 386 |
if not we_are_translated(): |
| 387 |
assert jitlog_enabled() |
| 388 |
jitlog_write_marked(mark + line, len(line) + 1) |
| 389 |
|
| 390 |
def log_jit_counter(self, struct): |
| 391 |
_log_jit_counter(struct) |
| 392 |
|
| 393 |
def log_trace(self, tag, metainterp_sd, mc, memo=None): |
| 394 |
if not jitlog_enabled(): |
| 395 |
return EMPTY_TRACE_LOG |
| 396 |
assert self.metainterp_sd is not None |
| 397 |
if memo is None: |
| 398 |
memo = {} |
| 399 |
return LogTrace(tag, memo, self.metainterp_sd, mc, self) |
| 400 |
|
| 401 |
def log_patch_guard(self, descr_number, addr): |
| 402 |
if not jitlog_enabled(): |
| 403 |
return |
| 404 |
le_descr_number = encode_le_addr(descr_number) |
| 405 |
le_addr = encode_le_addr(addr) |
| 406 |
lst = [le_descr_number, le_addr] |
| 407 |
self._write_marked(MARK_STITCH_BRIDGE, ''.join(lst)) |
| 408 |
|
| 409 |
class BaseLogTrace(object): |
| 410 |
def write_trace(self, trace): |
| 411 |
return None |
| 412 |
|
| 413 |
def write(self, args, ops, ops_offset={}): |
| 414 |
return None |
| 415 |
|
| 416 |
EMPTY_TRACE_LOG = BaseLogTrace() |
| 417 |
|
| 418 |
class PrefixCompressor(object): |
| 419 |
def __init__(self, count): |
| 420 |
self.prefixes = [None] * count |
| 421 |
self.written_prefixes = [None] * count |
| 422 |
|
| 423 |
def get_last(self, index): |
| 424 |
return self.prefixes[index] |
| 425 |
|
| 426 |
def get_last_written(self, index): |
| 427 |
return self.written_prefixes[index] |
| 428 |
|
| 429 |
def compress(self, index, string): |
| 430 |
assert string is not None |
| 431 |
last = self.get_last(index) |
| 432 |
if last is None: |
| 433 |
self.prefixes[index] = string |
| 434 |
return None |
| 435 |
cp = commonprefix(last, string) |
| 436 |
if len(cp) <= 1: |
| 437 |
self.prefixes[index] = string |
| 438 |
return None |
| 439 |
return cp |
| 440 |
|
| 441 |
|
| 442 |
def write(self, log, index, prefix): |
| 443 |
|
| 444 |
log._write_marked(MARK_COMMON_PREFIX, chr(index) \ |
| 445 |
+ encode_str(prefix)) |
| 446 |
self.written_prefixes[index] = prefix |
| 447 |
|
| 448 |
def encode_merge_point(log, compressor, values): |
| 449 |
line = [] |
| 450 |
i = 0 |
| 451 |
for value in values: |
| 452 |
line.append(value.encode(log,i,compressor)) |
| 453 |
i += 1 |
| 454 |
return ''.join(line) |
| 455 |
|
| 456 |
|
| 457 |
class LogTrace(BaseLogTrace): |
| 458 |
def __init__(self, tag, memo, metainterp_sd, mc, logger): |
| 459 |
self.memo = memo |
| 460 |
self.metainterp_sd = metainterp_sd |
| 461 |
self.tag = tag |
| 462 |
self.mc = mc |
| 463 |
self.logger = logger |
| 464 |
self.common_prefix = None |
| 465 |
|
| 466 |
def write_trace(self, trace): |
| 467 |
ops = [] |
| 468 |
i = trace.get_iter() |
| 469 |
while not i.done(): |
| 470 |
ops.append(i.next()) |
| 471 |
self.write(i.inputargs, ops) |
| 472 |
|
| 473 |
def write(self, args, ops, ops_offset={}): |
| 474 |
log = self.logger |
| 475 |
tid = self.logger.trace_id |
| 476 |
log._write_marked(self.tag, encode_le_addr(tid)) |
| 477 |
|
| 478 |
|
| 479 |
str_args = [self.var_to_str(arg) for arg in args] |
| 480 |
string = encode_str(','.join(str_args)) |
| 481 |
log._write_marked(MARK_INPUT_ARGS, string) |
| 482 |
|
| 483 |
|
| 484 |
if self.mc is not None: |
| 485 |
absaddr = self.mc.absolute_addr() |
| 486 |
rel = self.mc.get_relative_pos() |
| 487 |
|
| 488 |
le_addr1 = encode_le_addr(absaddr) |
| 489 |
le_addr2 = encode_le_addr(absaddr + rel) |
| 490 |
log._write_marked(MARK_ASM_ADDR, le_addr1 + le_addr2) |
| 491 |
for i,op in enumerate(ops): |
| 492 |
if rop.DEBUG_MERGE_POINT == op.getopnum(): |
| 493 |
self.encode_debug_info(op) |
| 494 |
continue |
| 495 |
mark, line = self.encode_op(op) |
| 496 |
log._write_marked(mark, line) |
| 497 |
self.write_core_dump(ops, i, op, ops_offset) |
| 498 |
|
| 499 |
self.memo = {} |
| 500 |
|
| 501 |
def encode_once(self): |
| 502 |
pass |
| 503 |
|
| 504 |
def encode_debug_info(self, op): |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
log = self.logger |
| 509 |
jd_sd = self.metainterp_sd.jitdrivers_sd[op.getarg(0).getint()] |
| 510 |
if not jd_sd.warmstate.get_location: |
| 511 |
return |
| 512 |
values = jd_sd.warmstate.get_location(op.getarglist()[3:]) |
| 513 |
if values is None: |
| 514 |
|
| 515 |
return |
| 516 |
types = jd_sd.warmstate.get_location_types |
| 517 |
|
| 518 |
if self.common_prefix is None: |
| 519 |
|
| 520 |
|
| 521 |
self.common_prefix = PrefixCompressor(len(types)) |
| 522 |
encoded_types = [] |
| 523 |
for i, (semantic_type, generic_type) in enumerate(types): |
| 524 |
encoded_types.append(chr(semantic_type)) |
| 525 |
encoded_types.append(generic_type) |
| 526 |
count = encode_le_16bit(len(types)) |
| 527 |
log._write_marked(MARK_INIT_MERGE_POINT, count + ''.join(encoded_types)) |
| 528 |
|
| 529 |
|
| 530 |
encoded = encode_merge_point(log, self.common_prefix, values) |
| 531 |
log._write_marked(MARK_MERGE_POINT, encoded) |
| 532 |
|
| 533 |
def encode_op(self, op): |
| 534 |
""" an operation is written as follows: |
| 535 |
<marker> <opid (16 bit)> \ |
| 536 |
<len (32 bit)> \ |
| 537 |
<res_val>,<arg_0>,...,<arg_n> \ |
| 538 |
<descr> |
| 539 |
<failarg_0>,...<failarg_n> |
| 540 |
The marker indicates if the last argument is |
| 541 |
a descr or a normal argument. |
| 542 |
""" |
| 543 |
str_args = [self.var_to_str(arg) for arg in op.getarglist()] |
| 544 |
descr = op.getdescr() |
| 545 |
le_opnum = encode_le_16bit(op.getopnum()) |
| 546 |
str_res = self.var_to_str(op) |
| 547 |
line = ','.join([str_res] + str_args) |
| 548 |
failargslist = op.getfailargs() |
| 549 |
failargs = '' |
| 550 |
if failargslist: |
| 551 |
failargs = ','.join([self.var_to_str(farg) for farg in failargslist]) |
| 552 |
|
| 553 |
if descr: |
| 554 |
descr_str = descr.repr_of_descr() |
| 555 |
line = line + ',' + descr_str |
| 556 |
string = encode_str(line) |
| 557 |
descr_number = compute_unique_id(descr) |
| 558 |
le_descr_number = encode_le_addr(descr_number) |
| 559 |
return MARK_RESOP_DESCR, le_opnum + string + le_descr_number + encode_str(failargs) |
| 560 |
else: |
| 561 |
string = encode_str(line) |
| 562 |
return MARK_RESOP, le_opnum + string + encode_str(failargs) |
| 563 |
|
| 564 |
|
| 565 |
def write_core_dump(self, operations, i, op, ops_offset): |
| 566 |
if self.mc is None: |
| 567 |
return |
| 568 |
|
| 569 |
op2 = None |
| 570 |
j = i+1 |
| 571 |
|
| 572 |
while j < len(operations): |
| 573 |
op2 = operations[j] |
| 574 |
if op in ops_offset: |
| 575 |
break |
| 576 |
j += 1 |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
if op not in ops_offset: |
| 581 |
return |
| 582 |
|
| 583 |
|
| 584 |
if op2 is not None and op2 not in ops_offset: |
| 585 |
return |
| 586 |
dump = [] |
| 587 |
|
| 588 |
start_offset = ops_offset[op] |
| 589 |
assert start_offset >= 0 |
| 590 |
|
| 591 |
|
| 592 |
if op2 is None: |
| 593 |
end_offset = self.mc.get_relative_pos() |
| 594 |
else: |
| 595 |
end_offset = ops_offset[op2] |
| 596 |
|
| 597 |
count = end_offset - start_offset |
| 598 |
dump = self.copy_core_dump(self.mc.absolute_addr(), start_offset, count) |
| 599 |
offset = encode_le_16bit(start_offset) |
| 600 |
edump = encode_str(dump) |
| 601 |
self.logger._write_marked(MARK_ASM, offset + edump) |
| 602 |
|
| 603 |
def copy_core_dump(self, addr, offset=0, count=-1): |
| 604 |
dump = [] |
| 605 |
src = rffi.cast(rffi.CCHARP, addr) |
| 606 |
end = self.mc.get_relative_pos() |
| 607 |
if count != -1: |
| 608 |
end = offset + count |
| 609 |
for p in range(offset, end): |
| 610 |
dump.append(src[p]) |
| 611 |
return ''.join(dump) |
| 612 |
|
| 613 |
def var_to_str(self, arg): |
| 614 |
if arg is None: |
| 615 |
return '-' |
| 616 |
try: |
| 617 |
mv = self.memo[arg] |
| 618 |
except KeyError: |
| 619 |
mv = len(self.memo) |
| 620 |
self.memo[arg] = mv |
| 621 |
if isinstance(arg, ConstInt): |
| 622 |
if self.metainterp_sd and int_could_be_an_address(arg.value): |
| 623 |
addr = arg.getaddr() |
| 624 |
name = self.metainterp_sd.get_name_from_address(addr) |
| 625 |
if name: |
| 626 |
return 'ConstClass(' + name + ')' |
| 627 |
return str(arg.value) |
| 628 |
elif isinstance(arg, ConstPtr): |
| 629 |
if arg.value: |
| 630 |
return 'ConstPtr(ptr' + str(mv) + ')' |
| 631 |
return 'ConstPtr(null)' |
| 632 |
if isinstance(arg, ConstFloat): |
| 633 |
return str(arg.getfloat()) |
| 634 |
elif arg is None: |
| 635 |
return 'None' |
| 636 |
elif arg.is_vector(): |
| 637 |
return 'v' + str(mv) |
| 638 |
elif arg.type == 'i': |
| 639 |
return 'i' + str(mv) |
| 640 |
elif arg.type == 'r': |
| 641 |
return 'p' + str(mv) |
| 642 |
elif arg.type == 'f': |
| 643 |
return 'f' + str(mv) |
| 644 |
else: |
| 645 |
return '?' |
| 646 |
|
| 647 |
def int_could_be_an_address(x): |
| 648 |
if we_are_translated(): |
| 649 |
x = rffi.cast(lltype.Signed, x) |
| 650 |
return not (-32768 <= x <= 32767) |
| 651 |
else: |
| 652 |
return isinstance(x, llmemory.AddressAsInt) |