| 1 |
import sys |
| 2 |
from rpython.rlib.objectmodel import we_are_translated, fetch_translated_config |
| 3 |
from rpython.rlib.objectmodel import specialize |
| 4 |
from rpython.rlib.rarithmetic import r_longlong |
| 5 |
from rpython.rtyper.lltypesystem import lltype, llmemory, rstr |
| 6 |
from rpython.rtyper.lltypesystem.lloperation import llop |
| 7 |
from rpython.rtyper.extregistry import ExtRegistryEntry |
| 8 |
from rpython.rtyper.annlowlevel import llhelper, hlstr |
| 9 |
from rpython.rtyper.annlowlevel import cast_gcref_to_instance |
| 10 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 11 |
|
| 12 |
|
| 13 |
CMD_PRINT = 1 |
| 14 |
CMD_BACKTRACE = 2 |
| 15 |
CMD_LOCALS = 3 |
| 16 |
CMD_BREAKPOINTS = 4 |
| 17 |
CMD_STACKID = 5 |
| 18 |
CMD_ATTACHID = 6 |
| 19 |
CMD_COMPILEWATCH= 7 |
| 20 |
CMD_CHECKWATCH = 8 |
| 21 |
CMD_WATCHVALUES = 9 |
| 22 |
ANSWER_LINECACHE= 19 |
| 23 |
ANSWER_TEXT = 20 |
| 24 |
ANSWER_STACKID = 21 |
| 25 |
ANSWER_NEXTNID = 22 |
| 26 |
ANSWER_WATCH = 23 |
| 27 |
ANSWER_CHBKPT = 24 |
| 28 |
|
| 29 |
|
| 30 |
def stop_point(place=0): |
| 31 |
"""Indicates a point in the execution of the RPython program where |
| 32 |
the reverse-debugger can stop. When reverse-debugging, we see |
| 33 |
the "time" as the index of the stop-point that happened. |
| 34 |
""" |
| 35 |
if we_are_translated(): |
| 36 |
if fetch_translated_config().translation.reverse_debugger: |
| 37 |
llop.revdb_stop_point(lltype.Void, place) |
| 38 |
|
| 39 |
def register_debug_command(command, lambda_func): |
| 40 |
"""Register the extra RPython-implemented debug command.""" |
| 41 |
|
| 42 |
def send_answer(cmd, arg1=0, arg2=0, arg3=0, extra=""): |
| 43 |
"""For RPython debug commands: writes an answer block to stdout""" |
| 44 |
llop.revdb_send_answer(lltype.Void, cmd, arg1, arg2, arg3, extra) |
| 45 |
|
| 46 |
def send_output(text): |
| 47 |
send_answer(ANSWER_TEXT, extra=text) |
| 48 |
|
| 49 |
def send_print(text): |
| 50 |
send_answer(ANSWER_TEXT, 1, extra=text) |
| 51 |
|
| 52 |
def send_nextnid(unique_id): |
| 53 |
send_answer(ANSWER_NEXTNID, unique_id) |
| 54 |
|
| 55 |
def send_watch(text, ok_flag): |
| 56 |
send_answer(ANSWER_WATCH, ok_flag, extra=text) |
| 57 |
|
| 58 |
def send_linecache(filename, linenum, strip=True): |
| 59 |
send_answer(ANSWER_LINECACHE, linenum, int(strip), extra=filename) |
| 60 |
|
| 61 |
def send_change_breakpoint(breakpointnum, newtext=''): |
| 62 |
send_answer(ANSWER_CHBKPT, breakpointnum, extra=newtext) |
| 63 |
|
| 64 |
def current_time(): |
| 65 |
"""For RPython debug commands: returns the current time.""" |
| 66 |
return llop.revdb_get_value(lltype.SignedLongLong, 'c') |
| 67 |
|
| 68 |
def current_break_time(): |
| 69 |
"""Returns the time configured for the next break. When going forward, |
| 70 |
this is the target time at which we'll stop going forward.""" |
| 71 |
return llop.revdb_get_value(lltype.SignedLongLong, 'b') |
| 72 |
|
| 73 |
def total_time(): |
| 74 |
"""For RPython debug commands: returns the total time (measured |
| 75 |
as the total number of stop-points).""" |
| 76 |
return llop.revdb_get_value(lltype.SignedLongLong, 't') |
| 77 |
|
| 78 |
def currently_created_objects(): |
| 79 |
"""For RPython debug commands: returns the current value of |
| 80 |
the object creation counter. All objects created so far have |
| 81 |
a lower unique id; all objects created afterwards will have a |
| 82 |
unique id greater or equal.""" |
| 83 |
return llop.revdb_get_value(lltype.SignedLongLong, 'u') |
| 84 |
|
| 85 |
def current_place(): |
| 86 |
"""For RPython debug commands: the value of the 'place' argument |
| 87 |
passed to stop_point(). |
| 88 |
""" |
| 89 |
return llop.revdb_get_value(lltype.Signed, 'p') |
| 90 |
|
| 91 |
def flag_io_disabled(): |
| 92 |
"""Returns True if we're in the debugger typing commands.""" |
| 93 |
if we_are_translated(): |
| 94 |
if fetch_translated_config().translation.reverse_debugger: |
| 95 |
flag = llop.revdb_get_value(lltype.Signed, 'i') |
| 96 |
return flag != ord('R') |
| 97 |
return False |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
def breakpoint(num): |
| 108 |
llop.revdb_breakpoint(lltype.Void, num) |
| 109 |
|
| 110 |
def set_thread_breakpoint(tnum): |
| 111 |
llop.revdb_set_thread_breakpoint(lltype.Void, tnum) |
| 112 |
|
| 113 |
@specialize.argtype(0) |
| 114 |
def get_unique_id(x): |
| 115 |
"""Returns the creation number of the object 'x'. For objects created |
| 116 |
by the program, it is globally unique, monotonic, and reproducible |
| 117 |
among multiple processes. For objects created by a debug command, |
| 118 |
this returns a (random) negative number. Right now, this returns 0 |
| 119 |
for all prebuilt objects. |
| 120 |
""" |
| 121 |
return llop.revdb_get_unique_id(lltype.SignedLongLong, x) |
| 122 |
|
| 123 |
def track_object(unique_id, callback): |
| 124 |
"""Track the creation of the object given by its unique_id, which must |
| 125 |
be in the future (i.e. >= currently_created_objects()). Call this |
| 126 |
before go_forward(). If go_forward() goes over the creation of this |
| 127 |
object, then 'callback(gcref)' is called. Careful in callback(), |
| 128 |
gcref is not fully initialized and should not be immediately read from, |
| 129 |
only stored for later. The purpose of callback() is to possibly |
| 130 |
call track_object() again to track the next object, and/or to call |
| 131 |
breakpoint(). Note: object tracking remains activated until one of: |
| 132 |
(1) we reach the creation time in go_forward(); (2) we call |
| 133 |
track_object() to track a different object; (3) we call jump_in_time(). |
| 134 |
""" |
| 135 |
ll_callback = llhelper(_CALLBACK_GCREF_FNPTR, callback) |
| 136 |
llop.revdb_track_object(lltype.Void, unique_id, ll_callback) |
| 137 |
|
| 138 |
def watch_save_state(force=False): |
| 139 |
return llop.revdb_watch_save_state(lltype.Bool, force) |
| 140 |
|
| 141 |
def watch_restore_state(any_watch_point): |
| 142 |
llop.revdb_watch_restore_state(lltype.Void, any_watch_point) |
| 143 |
|
| 144 |
|
| 145 |
def split_breakpoints_arg(breakpoints): |
| 146 |
|
| 147 |
n = 0 |
| 148 |
i = 0 |
| 149 |
while i < len(breakpoints): |
| 150 |
kind = breakpoints[i] |
| 151 |
i += 1 |
| 152 |
if kind != '\x00': |
| 153 |
length = (ord(breakpoints[i]) | |
| 154 |
(ord(breakpoints[i + 1]) << 8) | |
| 155 |
(ord(breakpoints[i + 2]) << 16)) |
| 156 |
assert length >= 0 |
| 157 |
i += 3 |
| 158 |
yield n, kind, breakpoints[i : i + length] |
| 159 |
i += length |
| 160 |
n += 1 |
| 161 |
assert i == len(breakpoints) |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
_CALLBACK_GCREF_FNPTR = lltype.Ptr(lltype.FuncType([llmemory.GCREF], |
| 173 |
lltype.Void)) |
| 174 |
_CMDPTR = rffi.CStructPtr('rpy_revdb_command_s', |
| 175 |
('cmd', rffi.INT), |
| 176 |
('arg1', lltype.SignedLongLong), |
| 177 |
('arg2', lltype.SignedLongLong), |
| 178 |
('arg3', lltype.SignedLongLong), |
| 179 |
hints={'ignore_revdb': True}) |
| 180 |
|
| 181 |
|
| 182 |
class RegisterDebugCommand(ExtRegistryEntry): |
| 183 |
_about_ = register_debug_command |
| 184 |
|
| 185 |
def compute_result_annotation(self, s_command_num, s_lambda_func): |
| 186 |
command_num = s_command_num.const |
| 187 |
lambda_func = s_lambda_func.const |
| 188 |
assert isinstance(command_num, (int, str)) |
| 189 |
t = self.bookkeeper.annotator.translator |
| 190 |
if t.config.translation.reverse_debugger: |
| 191 |
func = lambda_func() |
| 192 |
try: |
| 193 |
cmds = t.revdb_commands |
| 194 |
except AttributeError: |
| 195 |
cmds = t.revdb_commands = {} |
| 196 |
old_func = cmds.setdefault(command_num, func) |
| 197 |
assert old_func is func |
| 198 |
s_func = self.bookkeeper.immutablevalue(func) |
| 199 |
arg_getter = getattr(self, 'arguments_' + str(command_num), |
| 200 |
self.default_arguments) |
| 201 |
self.bookkeeper.emulate_pbc_call(self.bookkeeper.position_key, |
| 202 |
s_func, arg_getter()) |
| 203 |
|
| 204 |
def default_arguments(self): |
| 205 |
from rpython.annotator import model as annmodel |
| 206 |
from rpython.rtyper import llannotation |
| 207 |
return [llannotation.SomePtr(ll_ptrtype=_CMDPTR), |
| 208 |
annmodel.SomeString()] |
| 209 |
|
| 210 |
def arguments_ALLOCATING(self): |
| 211 |
from rpython.annotator import model as annmodel |
| 212 |
from rpython.rtyper import llannotation |
| 213 |
return [annmodel.SomeInteger(knowntype=r_longlong), |
| 214 |
llannotation.lltype_to_annotation(llmemory.GCREF)] |
| 215 |
|
| 216 |
def arguments_WATCHING(self): |
| 217 |
raise Exception("XXX remove me") |
| 218 |
|
| 219 |
def specialize_call(self, hop): |
| 220 |
hop.exception_cannot_occur() |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
_INVALID_STRTOD = -3.46739514239368e+113 |
| 230 |
|
| 231 |
def emulate_strtod(input): |
| 232 |
d = llop.revdb_strtod(lltype.Float, input) |
| 233 |
if d == _INVALID_STRTOD: |
| 234 |
raise ValueError |
| 235 |
return d |
| 236 |
|
| 237 |
def emulate_dtoa(value): |
| 238 |
s = llop.revdb_dtoa(lltype.Ptr(rstr.STR), value) |
| 239 |
s = hlstr(s) |
| 240 |
assert s is not None |
| 241 |
return s |
| 242 |
|
| 243 |
def emulate_modf(x): |
| 244 |
return (llop.revdb_modf(lltype.Float, x, 0), |
| 245 |
llop.revdb_modf(lltype.Float, x, 1)) |
| 246 |
|
| 247 |
def emulate_frexp(x): |
| 248 |
return (llop.revdb_frexp(lltype.Float, x, 0), |
| 249 |
int(llop.revdb_frexp(lltype.Float, x, 1))) |