| 1 | from rpython.rtyper.lltypesystem import lltype, rffi |
| 2 | from rpython.rlib import rgc |
| 3 | from rpython.rlib.rvmprof import traceback |
| 4 | |
| 5 | from pypy.interpreter.pycode import PyCode |
| 6 | from pypy.module.faulthandler.cintf import pypy_faulthandler_write |
| 7 | from pypy.module.faulthandler.cintf import pypy_faulthandler_write_uint |
| 8 | |
| 9 | |
| 10 | MAX_STRING_LENGTH = 500 |
| 11 | |
| 12 | global_buf = lltype.malloc(rffi.CCHARP.TO, MAX_STRING_LENGTH, flavor='raw', |
| 13 | immortal=True, zero=True) |
| 14 | |
| 15 | def _dump(fd, s): |
| 16 | assert isinstance(s, str) |
| 17 | l = len(s) |
| 18 | if l >= MAX_STRING_LENGTH: |
| 19 | l = MAX_STRING_LENGTH - 1 |
| 20 | i = 0 |
| 21 | while i < l: |
| 22 | global_buf[i] = s[i] |
| 23 | i += 1 |
| 24 | global_buf[l] = '\x00' |
| 25 | pypy_faulthandler_write(fd, global_buf) |
| 26 | |
| 27 | def _dump_nonneg_int(fd, i): |
| 28 | pypy_faulthandler_write_uint(fd, rffi.cast(lltype.Unsigned, i), |
| 29 | rffi.cast(rffi.INT, 1)) |
| 30 | |
| 31 | |
| 32 | def dump_code(pycode, loc, fd): |
| 33 | if pycode is None: |
| 34 | _dump(fd, " File ???") |
| 35 | else: |
| 36 | _dump(fd, ' File "') |
| 37 | _dump(fd, pycode.co_filename) |
| 38 | _dump(fd, '", line ') |
| 39 | _dump_nonneg_int(fd, pycode.co_firstlineno) |
| 40 | _dump(fd, " in ") |
| 41 | _dump(fd, pycode.co_name) |
| 42 | if loc == traceback.LOC_JITTED: |
| 43 | _dump(fd, " [jitted]") |
| 44 | elif loc == traceback.LOC_JITTED_INLINED: |
| 45 | _dump(fd, " [jit inlined]") |
| 46 | _dump(fd, "\n") |
| 47 | |
| 48 | |
| 49 | @rgc.no_collect |
| 50 | def _dump_callback(fd, array_p, array_length): |
| 51 | """We are as careful as we can reasonably be here (i.e. not 100%, |
| 52 | but hopefully close enough). In particular, this is written as |
| 53 | RPython but shouldn't allocate anything. |
| 54 | """ |
| 55 | traceback.walk_traceback(PyCode, dump_code, fd, array_p, array_length) |