RemoteDebugger.py 11 KB

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