| 1 |
import os |
| 2 |
from rpython.rtyper.lltypesystem import lltype, llmemory, rffi |
| 3 |
from rpython.rlib.rarithmetic import widen, ovfcheck_float_to_longlong |
| 4 |
from rpython.rlib.objectmodel import keepalive_until_here |
| 5 |
from rpython.rtyper.annlowlevel import llhelper |
| 6 |
|
| 7 |
from pypy.interpreter.error import OperationError, oefmt |
| 8 |
from pypy.interpreter.error import exception_from_saved_errno |
| 9 |
from pypy.interpreter.gateway import unwrap_spec |
| 10 |
from pypy.module.faulthandler import cintf, dumper |
| 11 |
|
| 12 |
|
| 13 |
class Handler(object): |
| 14 |
def __init__(self, space): |
| 15 |
"NOT_RPYTHON" |
| 16 |
self.space = space |
| 17 |
self._cleanup_() |
| 18 |
|
| 19 |
def _cleanup_(self): |
| 20 |
self.fatal_error_w_file = None |
| 21 |
self.dump_traceback_later_w_file = None |
| 22 |
self.user_w_files = None |
| 23 |
|
| 24 |
def check_err(self, p_err): |
| 25 |
if p_err: |
| 26 |
raise oefmt(self.space.w_RuntimeError, 'faulthandler: %8', |
| 27 |
rffi.charp2str(p_err)) |
| 28 |
|
| 29 |
def get_fileno_and_file(self, w_file): |
| 30 |
space = self.space |
| 31 |
if space.is_none(w_file): |
| 32 |
w_file = space.sys.get('stderr') |
| 33 |
if space.is_none(w_file): |
| 34 |
raise oefmt(space.w_RuntimeError, "sys.stderr is None") |
| 35 |
elif space.isinstance_w(w_file, space.w_int): |
| 36 |
fd = space.c_int_w(w_file) |
| 37 |
if fd < 0: |
| 38 |
raise oefmt(space.w_ValueError, |
| 39 |
"file is not a valid file descriptor") |
| 40 |
return fd, None |
| 41 |
|
| 42 |
fd = space.c_int_w(space.call_method(w_file, 'fileno')) |
| 43 |
try: |
| 44 |
space.call_method(w_file, 'flush') |
| 45 |
except OperationError as e: |
| 46 |
if e.async(space): |
| 47 |
raise |
| 48 |
pass |
| 49 |
return fd, w_file |
| 50 |
|
| 51 |
def setup(self): |
| 52 |
dump_callback = llhelper(cintf.DUMP_CALLBACK, dumper._dump_callback) |
| 53 |
self.check_err(cintf.pypy_faulthandler_setup(dump_callback)) |
| 54 |
|
| 55 |
def enable(self, w_file, all_threads): |
| 56 |
fileno, w_file = self.get_fileno_and_file(w_file) |
| 57 |
self.setup() |
| 58 |
self.fatal_error_w_file = w_file |
| 59 |
self.check_err(cintf.pypy_faulthandler_enable( |
| 60 |
rffi.cast(rffi.INT_real, fileno), |
| 61 |
rffi.cast(rffi.INT_real, all_threads))) |
| 62 |
|
| 63 |
def disable(self): |
| 64 |
cintf.pypy_faulthandler_disable() |
| 65 |
self.fatal_error_w_file = None |
| 66 |
|
| 67 |
def is_enabled(self): |
| 68 |
return bool(widen(cintf.pypy_faulthandler_is_enabled())) |
| 69 |
|
| 70 |
def dump_traceback(self, w_file, all_threads): |
| 71 |
fileno, w_file = self.get_fileno_and_file(w_file) |
| 72 |
self.setup() |
| 73 |
cintf.pypy_faulthandler_dump_traceback( |
| 74 |
rffi.cast(rffi.INT_real, fileno), |
| 75 |
rffi.cast(rffi.INT_real, all_threads), |
| 76 |
llmemory.NULL) |
| 77 |
keepalive_until_here(w_file) |
| 78 |
|
| 79 |
def dump_traceback_later(self, timeout, repeat, w_file, exit): |
| 80 |
space = self.space |
| 81 |
timeout *= 1e6 |
| 82 |
try: |
| 83 |
microseconds = ovfcheck_float_to_longlong(timeout) |
| 84 |
except OverflowError: |
| 85 |
raise oefmt(space.w_OverflowError, "timeout value is too large") |
| 86 |
if microseconds <= 0: |
| 87 |
raise oefmt(space.w_ValueError, "timeout must be greater than 0") |
| 88 |
fileno, w_file = self.get_fileno_and_file(w_file) |
| 89 |
self.setup() |
| 90 |
self.check_err(cintf.pypy_faulthandler_dump_traceback_later( |
| 91 |
rffi.cast(rffi.LONGLONG, microseconds), |
| 92 |
rffi.cast(rffi.INT, repeat), |
| 93 |
rffi.cast(rffi.INT_real, fileno), |
| 94 |
rffi.cast(rffi.INT, exit))) |
| 95 |
self.dump_traceback_later_w_file = w_file |
| 96 |
|
| 97 |
def cancel_dump_traceback_later(self): |
| 98 |
cintf.pypy_faulthandler_cancel_dump_traceback_later() |
| 99 |
self.dump_traceback_later_w_file = None |
| 100 |
|
| 101 |
def check_signum(self, signum): |
| 102 |
err = rffi.cast(lltype.Signed, |
| 103 |
cintf.pypy_faulthandler_check_signum(signum)) |
| 104 |
if err < 0: |
| 105 |
space = self.space |
| 106 |
if err == -1: |
| 107 |
raise oefmt(space.w_RuntimeError, |
| 108 |
"signal %d cannot be registered, " |
| 109 |
"use enable() instead", signum) |
| 110 |
else: |
| 111 |
raise oefmt(space.w_ValueError, "signal number out of range") |
| 112 |
|
| 113 |
def register(self, signum, w_file, all_threads, chain): |
| 114 |
self.check_signum(signum) |
| 115 |
fileno, w_file = self.get_fileno_and_file(w_file) |
| 116 |
self.setup() |
| 117 |
self.check_err(cintf.pypy_faulthandler_register( |
| 118 |
rffi.cast(rffi.INT_real, signum), |
| 119 |
rffi.cast(rffi.INT_real, fileno), |
| 120 |
rffi.cast(rffi.INT_real, all_threads), |
| 121 |
rffi.cast(rffi.INT_real, chain))) |
| 122 |
if self.user_w_files is None: |
| 123 |
self.user_w_files = {} |
| 124 |
self.user_w_files[signum] = w_file |
| 125 |
|
| 126 |
def unregister(self, signum): |
| 127 |
self.check_signum(signum) |
| 128 |
change = cintf.pypy_faulthandler_unregister( |
| 129 |
rffi.cast(rffi.INT, signum)) |
| 130 |
if self.user_w_files is not None: |
| 131 |
self.user_w_files.pop(signum, None) |
| 132 |
return rffi.cast(lltype.Signed, change) == 1 |
| 133 |
|
| 134 |
def finish(self): |
| 135 |
cintf.pypy_faulthandler_teardown() |
| 136 |
self._cleanup_() |
| 137 |
|
| 138 |
|
| 139 |
def finish(space): |
| 140 |
"Finalize the faulthandler logic (called from shutdown())" |
| 141 |
space.fromcache(Handler).finish() |
| 142 |
|
| 143 |
|
| 144 |
@unwrap_spec(all_threads=int) |
| 145 |
def enable(space, w_file=None, all_threads=0): |
| 146 |
"enable(file=sys.stderr, all_threads=True): enable the fault handler" |
| 147 |
space.fromcache(Handler).enable(w_file, all_threads) |
| 148 |
|
| 149 |
def disable(space): |
| 150 |
"disable(): disable the fault handler" |
| 151 |
space.fromcache(Handler).disable() |
| 152 |
|
| 153 |
def is_enabled(space): |
| 154 |
"is_enabled()->bool: check if the handler is enabled" |
| 155 |
return space.newbool(space.fromcache(Handler).is_enabled()) |
| 156 |
|
| 157 |
@unwrap_spec(all_threads=int) |
| 158 |
def dump_traceback(space, w_file=None, all_threads=0): |
| 159 |
"""dump the traceback of the current thread into file |
| 160 |
including all threads if all_threads is True""" |
| 161 |
space.fromcache(Handler).dump_traceback(w_file, all_threads) |
| 162 |
|
| 163 |
@unwrap_spec(timeout=float, repeat=int, exit=int) |
| 164 |
def dump_traceback_later(space, timeout, repeat=0, w_file=None, exit=0): |
| 165 |
"""dump the traceback of all threads in timeout seconds, |
| 166 |
or each timeout seconds if repeat is True. If exit is True, |
| 167 |
call _exit(1) which is not safe.""" |
| 168 |
space.fromcache(Handler).dump_traceback_later(timeout, repeat, w_file, exit) |
| 169 |
|
| 170 |
def cancel_dump_traceback_later(space): |
| 171 |
"""cancel the previous call to dump_traceback_later().""" |
| 172 |
space.fromcache(Handler).cancel_dump_traceback_later() |
| 173 |
|
| 174 |
@unwrap_spec(signum=int, all_threads=int, chain=int) |
| 175 |
def register(space, signum, w_file=None, all_threads=1, chain=0): |
| 176 |
space.fromcache(Handler).register(signum, w_file, all_threads, chain) |
| 177 |
|
| 178 |
@unwrap_spec(signum=int) |
| 179 |
def unregister(space, signum): |
| 180 |
return space.newbool(space.fromcache(Handler).unregister(signum)) |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
@unwrap_spec(release_gil=int) |
| 186 |
def read_null(space, release_gil=0): |
| 187 |
if release_gil: |
| 188 |
cintf.pypy_faulthandler_read_null_releasegil() |
| 189 |
else: |
| 190 |
cintf.pypy_faulthandler_read_null() |
| 191 |
|
| 192 |
@unwrap_spec(release_gil=int) |
| 193 |
def sigsegv(space, release_gil=0): |
| 194 |
if release_gil: |
| 195 |
cintf.pypy_faulthandler_sigsegv_releasegil() |
| 196 |
else: |
| 197 |
cintf.pypy_faulthandler_sigsegv() |
| 198 |
|
| 199 |
def sigfpe(space): |
| 200 |
cintf.pypy_faulthandler_sigfpe() |
| 201 |
|
| 202 |
def sigabrt(space): |
| 203 |
cintf.pypy_faulthandler_sigabrt() |
| 204 |
|
| 205 |
@unwrap_spec(levels=int) |
| 206 |
def stack_overflow(space, levels=100000000): |
| 207 |
levels = float(levels) |
| 208 |
return space.newfloat(cintf.pypy_faulthandler_stackoverflow(levels)) |