| 1 | n/a | """Support for remote Python debugging. |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | Some ASCII art to describe the structure: |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | IN PYTHON SUBPROCESS # IN IDLE PROCESS |
|---|
| 6 | n/a | # |
|---|
| 7 | n/a | # oid='gui_adapter' |
|---|
| 8 | n/a | +----------+ # +------------+ +-----+ |
|---|
| 9 | n/a | | GUIProxy |--remote#call-->| GUIAdapter |--calls-->| GUI | |
|---|
| 10 | n/a | +-----+--calls-->+----------+ # +------------+ +-----+ |
|---|
| 11 | n/a | | Idb | # / |
|---|
| 12 | n/a | +-----+<-calls--+------------+ # +----------+<--calls-/ |
|---|
| 13 | n/a | | IdbAdapter |<--remote#call--| IdbProxy | |
|---|
| 14 | n/a | +------------+ # +----------+ |
|---|
| 15 | n/a | oid='idb_adapter' # |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | The purpose of the Proxy and Adapter classes is to translate certain |
|---|
| 18 | n/a | arguments and return values that cannot be transported through the RPC |
|---|
| 19 | n/a | barrier, in particular frame and traceback objects. |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | """ |
|---|
| 22 | n/a | |
|---|
| 23 | n/a | import types |
|---|
| 24 | n/a | from idlelib import debugger |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | debugging = 0 |
|---|
| 27 | n/a | |
|---|
| 28 | n/a | idb_adap_oid = "idb_adapter" |
|---|
| 29 | n/a | gui_adap_oid = "gui_adapter" |
|---|
| 30 | n/a | |
|---|
| 31 | n/a | #======================================= |
|---|
| 32 | n/a | # |
|---|
| 33 | n/a | # In the PYTHON subprocess: |
|---|
| 34 | n/a | |
|---|
| 35 | n/a | frametable = {} |
|---|
| 36 | n/a | dicttable = {} |
|---|
| 37 | n/a | codetable = {} |
|---|
| 38 | n/a | tracebacktable = {} |
|---|
| 39 | n/a | |
|---|
| 40 | n/a | def wrap_frame(frame): |
|---|
| 41 | n/a | fid = id(frame) |
|---|
| 42 | n/a | frametable[fid] = frame |
|---|
| 43 | n/a | return fid |
|---|
| 44 | n/a | |
|---|
| 45 | n/a | def wrap_info(info): |
|---|
| 46 | n/a | "replace info[2], a traceback instance, by its ID" |
|---|
| 47 | n/a | if info is None: |
|---|
| 48 | n/a | return None |
|---|
| 49 | n/a | else: |
|---|
| 50 | n/a | traceback = info[2] |
|---|
| 51 | n/a | assert isinstance(traceback, types.TracebackType) |
|---|
| 52 | n/a | traceback_id = id(traceback) |
|---|
| 53 | n/a | tracebacktable[traceback_id] = traceback |
|---|
| 54 | n/a | modified_info = (info[0], info[1], traceback_id) |
|---|
| 55 | n/a | return modified_info |
|---|
| 56 | n/a | |
|---|
| 57 | n/a | class GUIProxy: |
|---|
| 58 | n/a | |
|---|
| 59 | n/a | def __init__(self, conn, gui_adap_oid): |
|---|
| 60 | n/a | self.conn = conn |
|---|
| 61 | n/a | self.oid = gui_adap_oid |
|---|
| 62 | n/a | |
|---|
| 63 | n/a | def interaction(self, message, frame, info=None): |
|---|
| 64 | n/a | # calls rpc.SocketIO.remotecall() via run.MyHandler instance |
|---|
| 65 | n/a | # pass frame and traceback object IDs instead of the objects themselves |
|---|
| 66 | n/a | self.conn.remotecall(self.oid, "interaction", |
|---|
| 67 | n/a | (message, wrap_frame(frame), wrap_info(info)), |
|---|
| 68 | n/a | {}) |
|---|
| 69 | n/a | |
|---|
| 70 | n/a | class IdbAdapter: |
|---|
| 71 | n/a | |
|---|
| 72 | n/a | def __init__(self, idb): |
|---|
| 73 | n/a | self.idb = idb |
|---|
| 74 | n/a | |
|---|
| 75 | n/a | #----------called by an IdbProxy---------- |
|---|
| 76 | n/a | |
|---|
| 77 | n/a | def set_step(self): |
|---|
| 78 | n/a | self.idb.set_step() |
|---|
| 79 | n/a | |
|---|
| 80 | n/a | def set_quit(self): |
|---|
| 81 | n/a | self.idb.set_quit() |
|---|
| 82 | n/a | |
|---|
| 83 | n/a | def set_continue(self): |
|---|
| 84 | n/a | self.idb.set_continue() |
|---|
| 85 | n/a | |
|---|
| 86 | n/a | def set_next(self, fid): |
|---|
| 87 | n/a | frame = frametable[fid] |
|---|
| 88 | n/a | self.idb.set_next(frame) |
|---|
| 89 | n/a | |
|---|
| 90 | n/a | def set_return(self, fid): |
|---|
| 91 | n/a | frame = frametable[fid] |
|---|
| 92 | n/a | self.idb.set_return(frame) |
|---|
| 93 | n/a | |
|---|
| 94 | n/a | def get_stack(self, fid, tbid): |
|---|
| 95 | n/a | frame = frametable[fid] |
|---|
| 96 | n/a | if tbid is None: |
|---|
| 97 | n/a | tb = None |
|---|
| 98 | n/a | else: |
|---|
| 99 | n/a | tb = tracebacktable[tbid] |
|---|
| 100 | n/a | stack, i = self.idb.get_stack(frame, tb) |
|---|
| 101 | n/a | stack = [(wrap_frame(frame2), k) for frame2, k in stack] |
|---|
| 102 | n/a | return stack, i |
|---|
| 103 | n/a | |
|---|
| 104 | n/a | def run(self, cmd): |
|---|
| 105 | n/a | import __main__ |
|---|
| 106 | n/a | self.idb.run(cmd, __main__.__dict__) |
|---|
| 107 | n/a | |
|---|
| 108 | n/a | def set_break(self, filename, lineno): |
|---|
| 109 | n/a | msg = self.idb.set_break(filename, lineno) |
|---|
| 110 | n/a | return msg |
|---|
| 111 | n/a | |
|---|
| 112 | n/a | def clear_break(self, filename, lineno): |
|---|
| 113 | n/a | msg = self.idb.clear_break(filename, lineno) |
|---|
| 114 | n/a | return msg |
|---|
| 115 | n/a | |
|---|
| 116 | n/a | def clear_all_file_breaks(self, filename): |
|---|
| 117 | n/a | msg = self.idb.clear_all_file_breaks(filename) |
|---|
| 118 | n/a | return msg |
|---|
| 119 | n/a | |
|---|
| 120 | n/a | #----------called by a FrameProxy---------- |
|---|
| 121 | n/a | |
|---|
| 122 | n/a | def frame_attr(self, fid, name): |
|---|
| 123 | n/a | frame = frametable[fid] |
|---|
| 124 | n/a | return getattr(frame, name) |
|---|
| 125 | n/a | |
|---|
| 126 | n/a | def frame_globals(self, fid): |
|---|
| 127 | n/a | frame = frametable[fid] |
|---|
| 128 | n/a | dict = frame.f_globals |
|---|
| 129 | n/a | did = id(dict) |
|---|
| 130 | n/a | dicttable[did] = dict |
|---|
| 131 | n/a | return did |
|---|
| 132 | n/a | |
|---|
| 133 | n/a | def frame_locals(self, fid): |
|---|
| 134 | n/a | frame = frametable[fid] |
|---|
| 135 | n/a | dict = frame.f_locals |
|---|
| 136 | n/a | did = id(dict) |
|---|
| 137 | n/a | dicttable[did] = dict |
|---|
| 138 | n/a | return did |
|---|
| 139 | n/a | |
|---|
| 140 | n/a | def frame_code(self, fid): |
|---|
| 141 | n/a | frame = frametable[fid] |
|---|
| 142 | n/a | code = frame.f_code |
|---|
| 143 | n/a | cid = id(code) |
|---|
| 144 | n/a | codetable[cid] = code |
|---|
| 145 | n/a | return cid |
|---|
| 146 | n/a | |
|---|
| 147 | n/a | #----------called by a CodeProxy---------- |
|---|
| 148 | n/a | |
|---|
| 149 | n/a | def code_name(self, cid): |
|---|
| 150 | n/a | code = codetable[cid] |
|---|
| 151 | n/a | return code.co_name |
|---|
| 152 | n/a | |
|---|
| 153 | n/a | def code_filename(self, cid): |
|---|
| 154 | n/a | code = codetable[cid] |
|---|
| 155 | n/a | return code.co_filename |
|---|
| 156 | n/a | |
|---|
| 157 | n/a | #----------called by a DictProxy---------- |
|---|
| 158 | n/a | |
|---|
| 159 | n/a | def dict_keys(self, did): |
|---|
| 160 | n/a | raise NotImplemented("dict_keys not public or pickleable") |
|---|
| 161 | n/a | ## dict = dicttable[did] |
|---|
| 162 | n/a | ## return dict.keys() |
|---|
| 163 | n/a | |
|---|
| 164 | n/a | ### Needed until dict_keys is type is finished and pickealable. |
|---|
| 165 | n/a | ### Will probably need to extend rpc.py:SocketIO._proxify at that time. |
|---|
| 166 | n/a | def dict_keys_list(self, did): |
|---|
| 167 | n/a | dict = dicttable[did] |
|---|
| 168 | n/a | return list(dict.keys()) |
|---|
| 169 | n/a | |
|---|
| 170 | n/a | def dict_item(self, did, key): |
|---|
| 171 | n/a | dict = dicttable[did] |
|---|
| 172 | n/a | value = dict[key] |
|---|
| 173 | n/a | value = repr(value) ### can't pickle module 'builtins' |
|---|
| 174 | n/a | return value |
|---|
| 175 | n/a | |
|---|
| 176 | n/a | #----------end class IdbAdapter---------- |
|---|
| 177 | n/a | |
|---|
| 178 | n/a | |
|---|
| 179 | n/a | def start_debugger(rpchandler, gui_adap_oid): |
|---|
| 180 | n/a | """Start the debugger and its RPC link in the Python subprocess |
|---|
| 181 | n/a | |
|---|
| 182 | n/a | Start the subprocess side of the split debugger and set up that side of the |
|---|
| 183 | n/a | RPC link by instantiating the GUIProxy, Idb debugger, and IdbAdapter |
|---|
| 184 | n/a | objects and linking them together. Register the IdbAdapter with the |
|---|
| 185 | n/a | RPCServer to handle RPC requests from the split debugger GUI via the |
|---|
| 186 | n/a | IdbProxy. |
|---|
| 187 | n/a | |
|---|
| 188 | n/a | """ |
|---|
| 189 | n/a | gui_proxy = GUIProxy(rpchandler, gui_adap_oid) |
|---|
| 190 | n/a | idb = debugger.Idb(gui_proxy) |
|---|
| 191 | n/a | idb_adap = IdbAdapter(idb) |
|---|
| 192 | n/a | rpchandler.register(idb_adap_oid, idb_adap) |
|---|
| 193 | n/a | return idb_adap_oid |
|---|
| 194 | n/a | |
|---|
| 195 | n/a | |
|---|
| 196 | n/a | #======================================= |
|---|
| 197 | n/a | # |
|---|
| 198 | n/a | # In the IDLE process: |
|---|
| 199 | n/a | |
|---|
| 200 | n/a | |
|---|
| 201 | n/a | class FrameProxy: |
|---|
| 202 | n/a | |
|---|
| 203 | n/a | def __init__(self, conn, fid): |
|---|
| 204 | n/a | self._conn = conn |
|---|
| 205 | n/a | self._fid = fid |
|---|
| 206 | n/a | self._oid = "idb_adapter" |
|---|
| 207 | n/a | self._dictcache = {} |
|---|
| 208 | n/a | |
|---|
| 209 | n/a | def __getattr__(self, name): |
|---|
| 210 | n/a | if name[:1] == "_": |
|---|
| 211 | n/a | raise AttributeError(name) |
|---|
| 212 | n/a | if name == "f_code": |
|---|
| 213 | n/a | return self._get_f_code() |
|---|
| 214 | n/a | if name == "f_globals": |
|---|
| 215 | n/a | return self._get_f_globals() |
|---|
| 216 | n/a | if name == "f_locals": |
|---|
| 217 | n/a | return self._get_f_locals() |
|---|
| 218 | n/a | return self._conn.remotecall(self._oid, "frame_attr", |
|---|
| 219 | n/a | (self._fid, name), {}) |
|---|
| 220 | n/a | |
|---|
| 221 | n/a | def _get_f_code(self): |
|---|
| 222 | n/a | cid = self._conn.remotecall(self._oid, "frame_code", (self._fid,), {}) |
|---|
| 223 | n/a | return CodeProxy(self._conn, self._oid, cid) |
|---|
| 224 | n/a | |
|---|
| 225 | n/a | def _get_f_globals(self): |
|---|
| 226 | n/a | did = self._conn.remotecall(self._oid, "frame_globals", |
|---|
| 227 | n/a | (self._fid,), {}) |
|---|
| 228 | n/a | return self._get_dict_proxy(did) |
|---|
| 229 | n/a | |
|---|
| 230 | n/a | def _get_f_locals(self): |
|---|
| 231 | n/a | did = self._conn.remotecall(self._oid, "frame_locals", |
|---|
| 232 | n/a | (self._fid,), {}) |
|---|
| 233 | n/a | return self._get_dict_proxy(did) |
|---|
| 234 | n/a | |
|---|
| 235 | n/a | def _get_dict_proxy(self, did): |
|---|
| 236 | n/a | if did in self._dictcache: |
|---|
| 237 | n/a | return self._dictcache[did] |
|---|
| 238 | n/a | dp = DictProxy(self._conn, self._oid, did) |
|---|
| 239 | n/a | self._dictcache[did] = dp |
|---|
| 240 | n/a | return dp |
|---|
| 241 | n/a | |
|---|
| 242 | n/a | |
|---|
| 243 | n/a | class CodeProxy: |
|---|
| 244 | n/a | |
|---|
| 245 | n/a | def __init__(self, conn, oid, cid): |
|---|
| 246 | n/a | self._conn = conn |
|---|
| 247 | n/a | self._oid = oid |
|---|
| 248 | n/a | self._cid = cid |
|---|
| 249 | n/a | |
|---|
| 250 | n/a | def __getattr__(self, name): |
|---|
| 251 | n/a | if name == "co_name": |
|---|
| 252 | n/a | return self._conn.remotecall(self._oid, "code_name", |
|---|
| 253 | n/a | (self._cid,), {}) |
|---|
| 254 | n/a | if name == "co_filename": |
|---|
| 255 | n/a | return self._conn.remotecall(self._oid, "code_filename", |
|---|
| 256 | n/a | (self._cid,), {}) |
|---|
| 257 | n/a | |
|---|
| 258 | n/a | |
|---|
| 259 | n/a | class DictProxy: |
|---|
| 260 | n/a | |
|---|
| 261 | n/a | def __init__(self, conn, oid, did): |
|---|
| 262 | n/a | self._conn = conn |
|---|
| 263 | n/a | self._oid = oid |
|---|
| 264 | n/a | self._did = did |
|---|
| 265 | n/a | |
|---|
| 266 | n/a | ## def keys(self): |
|---|
| 267 | n/a | ## return self._conn.remotecall(self._oid, "dict_keys", (self._did,), {}) |
|---|
| 268 | n/a | |
|---|
| 269 | n/a | # 'temporary' until dict_keys is a pickleable built-in type |
|---|
| 270 | n/a | def keys(self): |
|---|
| 271 | n/a | return self._conn.remotecall(self._oid, |
|---|
| 272 | n/a | "dict_keys_list", (self._did,), {}) |
|---|
| 273 | n/a | |
|---|
| 274 | n/a | def __getitem__(self, key): |
|---|
| 275 | n/a | return self._conn.remotecall(self._oid, "dict_item", |
|---|
| 276 | n/a | (self._did, key), {}) |
|---|
| 277 | n/a | |
|---|
| 278 | n/a | def __getattr__(self, name): |
|---|
| 279 | n/a | ##print("*** Failed DictProxy.__getattr__:", name) |
|---|
| 280 | n/a | raise AttributeError(name) |
|---|
| 281 | n/a | |
|---|
| 282 | n/a | |
|---|
| 283 | n/a | class GUIAdapter: |
|---|
| 284 | n/a | |
|---|
| 285 | n/a | def __init__(self, conn, gui): |
|---|
| 286 | n/a | self.conn = conn |
|---|
| 287 | n/a | self.gui = gui |
|---|
| 288 | n/a | |
|---|
| 289 | n/a | def interaction(self, message, fid, modified_info): |
|---|
| 290 | n/a | ##print("*** Interaction: (%s, %s, %s)" % (message, fid, modified_info)) |
|---|
| 291 | n/a | frame = FrameProxy(self.conn, fid) |
|---|
| 292 | n/a | self.gui.interaction(message, frame, modified_info) |
|---|
| 293 | n/a | |
|---|
| 294 | n/a | |
|---|
| 295 | n/a | class IdbProxy: |
|---|
| 296 | n/a | |
|---|
| 297 | n/a | def __init__(self, conn, shell, oid): |
|---|
| 298 | n/a | self.oid = oid |
|---|
| 299 | n/a | self.conn = conn |
|---|
| 300 | n/a | self.shell = shell |
|---|
| 301 | n/a | |
|---|
| 302 | n/a | def call(self, methodname, *args, **kwargs): |
|---|
| 303 | n/a | ##print("*** IdbProxy.call %s %s %s" % (methodname, args, kwargs)) |
|---|
| 304 | n/a | value = self.conn.remotecall(self.oid, methodname, args, kwargs) |
|---|
| 305 | n/a | ##print("*** IdbProxy.call %s returns %r" % (methodname, value)) |
|---|
| 306 | n/a | return value |
|---|
| 307 | n/a | |
|---|
| 308 | n/a | def run(self, cmd, locals): |
|---|
| 309 | n/a | # Ignores locals on purpose! |
|---|
| 310 | n/a | seq = self.conn.asyncqueue(self.oid, "run", (cmd,), {}) |
|---|
| 311 | n/a | self.shell.interp.active_seq = seq |
|---|
| 312 | n/a | |
|---|
| 313 | n/a | def get_stack(self, frame, tbid): |
|---|
| 314 | n/a | # passing frame and traceback IDs, not the objects themselves |
|---|
| 315 | n/a | stack, i = self.call("get_stack", frame._fid, tbid) |
|---|
| 316 | n/a | stack = [(FrameProxy(self.conn, fid), k) for fid, k in stack] |
|---|
| 317 | n/a | return stack, i |
|---|
| 318 | n/a | |
|---|
| 319 | n/a | def set_continue(self): |
|---|
| 320 | n/a | self.call("set_continue") |
|---|
| 321 | n/a | |
|---|
| 322 | n/a | def set_step(self): |
|---|
| 323 | n/a | self.call("set_step") |
|---|
| 324 | n/a | |
|---|
| 325 | n/a | def set_next(self, frame): |
|---|
| 326 | n/a | self.call("set_next", frame._fid) |
|---|
| 327 | n/a | |
|---|
| 328 | n/a | def set_return(self, frame): |
|---|
| 329 | n/a | self.call("set_return", frame._fid) |
|---|
| 330 | n/a | |
|---|
| 331 | n/a | def set_quit(self): |
|---|
| 332 | n/a | self.call("set_quit") |
|---|
| 333 | n/a | |
|---|
| 334 | n/a | def set_break(self, filename, lineno): |
|---|
| 335 | n/a | msg = self.call("set_break", filename, lineno) |
|---|
| 336 | n/a | return msg |
|---|
| 337 | n/a | |
|---|
| 338 | n/a | def clear_break(self, filename, lineno): |
|---|
| 339 | n/a | msg = self.call("clear_break", filename, lineno) |
|---|
| 340 | n/a | return msg |
|---|
| 341 | n/a | |
|---|
| 342 | n/a | def clear_all_file_breaks(self, filename): |
|---|
| 343 | n/a | msg = self.call("clear_all_file_breaks", filename) |
|---|
| 344 | n/a | return msg |
|---|
| 345 | n/a | |
|---|
| 346 | n/a | def start_remote_debugger(rpcclt, pyshell): |
|---|
| 347 | n/a | """Start the subprocess debugger, initialize the debugger GUI and RPC link |
|---|
| 348 | n/a | |
|---|
| 349 | n/a | Request the RPCServer start the Python subprocess debugger and link. Set |
|---|
| 350 | n/a | up the Idle side of the split debugger by instantiating the IdbProxy, |
|---|
| 351 | n/a | debugger GUI, and debugger GUIAdapter objects and linking them together. |
|---|
| 352 | n/a | |
|---|
| 353 | n/a | Register the GUIAdapter with the RPCClient to handle debugger GUI |
|---|
| 354 | n/a | interaction requests coming from the subprocess debugger via the GUIProxy. |
|---|
| 355 | n/a | |
|---|
| 356 | n/a | The IdbAdapter will pass execution and environment requests coming from the |
|---|
| 357 | n/a | Idle debugger GUI to the subprocess debugger via the IdbProxy. |
|---|
| 358 | n/a | |
|---|
| 359 | n/a | """ |
|---|
| 360 | n/a | global idb_adap_oid |
|---|
| 361 | n/a | |
|---|
| 362 | n/a | idb_adap_oid = rpcclt.remotecall("exec", "start_the_debugger",\ |
|---|
| 363 | n/a | (gui_adap_oid,), {}) |
|---|
| 364 | n/a | idb_proxy = IdbProxy(rpcclt, pyshell, idb_adap_oid) |
|---|
| 365 | n/a | gui = debugger.Debugger(pyshell, idb_proxy) |
|---|
| 366 | n/a | gui_adap = GUIAdapter(rpcclt, gui) |
|---|
| 367 | n/a | rpcclt.register(gui_adap_oid, gui_adap) |
|---|
| 368 | n/a | return gui |
|---|
| 369 | n/a | |
|---|
| 370 | n/a | def close_remote_debugger(rpcclt): |
|---|
| 371 | n/a | """Shut down subprocess debugger and Idle side of debugger RPC link |
|---|
| 372 | n/a | |
|---|
| 373 | n/a | Request that the RPCServer shut down the subprocess debugger and link. |
|---|
| 374 | n/a | Unregister the GUIAdapter, which will cause a GC on the Idle process |
|---|
| 375 | n/a | debugger and RPC link objects. (The second reference to the debugger GUI |
|---|
| 376 | n/a | is deleted in pyshell.close_remote_debugger().) |
|---|
| 377 | n/a | |
|---|
| 378 | n/a | """ |
|---|
| 379 | n/a | close_subprocess_debugger(rpcclt) |
|---|
| 380 | n/a | rpcclt.unregister(gui_adap_oid) |
|---|
| 381 | n/a | |
|---|
| 382 | n/a | def close_subprocess_debugger(rpcclt): |
|---|
| 383 | n/a | rpcclt.remotecall("exec", "stop_the_debugger", (idb_adap_oid,), {}) |
|---|
| 384 | n/a | |
|---|
| 385 | n/a | def restart_subprocess_debugger(rpcclt): |
|---|
| 386 | n/a | idb_adap_oid_ret = rpcclt.remotecall("exec", "start_the_debugger",\ |
|---|
| 387 | n/a | (gui_adap_oid,), {}) |
|---|
| 388 | n/a | assert idb_adap_oid_ret == idb_adap_oid, 'Idb restarted with different oid' |
|---|