debugger_r.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. frame = frametable[fid]
  73. if tbid is None:
  74. tb = None
  75. else:
  76. tb = tracebacktable[tbid]
  77. stack, i = self.idb.get_stack(frame, tb)
  78. stack = [(wrap_frame(frame2), k) for frame2, k in stack]
  79. return stack, i
  80. def run(self, cmd):
  81. import __main__
  82. self.idb.run(cmd, __main__.__dict__)
  83. def set_break(self, filename, lineno):
  84. msg = self.idb.set_break(filename, lineno)
  85. return msg
  86. def clear_break(self, filename, lineno):
  87. msg = self.idb.clear_break(filename, lineno)
  88. return msg
  89. def clear_all_file_breaks(self, filename):
  90. msg = self.idb.clear_all_file_breaks(filename)
  91. return msg
  92. #----------called by a FrameProxy----------
  93. def frame_attr(self, fid, name):
  94. frame = frametable[fid]
  95. return getattr(frame, name)
  96. def frame_globals(self, fid):
  97. frame = frametable[fid]
  98. dict = frame.f_globals
  99. did = id(dict)
  100. dicttable[did] = dict
  101. return did
  102. def frame_locals(self, fid):
  103. frame = frametable[fid]
  104. dict = frame.f_locals
  105. did = id(dict)
  106. dicttable[did] = dict
  107. return did
  108. def frame_code(self, fid):
  109. frame = frametable[fid]
  110. code = frame.f_code
  111. cid = id(code)
  112. codetable[cid] = code
  113. return cid
  114. #----------called by a CodeProxy----------
  115. def code_name(self, cid):
  116. code = codetable[cid]
  117. return code.co_name
  118. def code_filename(self, cid):
  119. code = codetable[cid]
  120. return code.co_filename
  121. #----------called by a DictProxy----------
  122. def dict_keys(self, did):
  123. raise NotImplementedError("dict_keys not public or pickleable")
  124. ## dict = dicttable[did]
  125. ## return dict.keys()
  126. ### Needed until dict_keys is type is finished and pickealable.
  127. ### Will probably need to extend rpc.py:SocketIO._proxify at that time.
  128. def dict_keys_list(self, did):
  129. dict = dicttable[did]
  130. return list(dict.keys())
  131. def dict_item(self, did, key):
  132. dict = dicttable[did]
  133. value = dict[key]
  134. value = repr(value) ### can't pickle module 'builtins'
  135. return value
  136. #----------end class IdbAdapter----------
  137. def start_debugger(rpchandler, gui_adap_oid):
  138. """Start the debugger and its RPC link in the Python subprocess
  139. Start the subprocess side of the split debugger and set up that side of the
  140. RPC link by instantiating the GUIProxy, Idb debugger, and IdbAdapter
  141. objects and linking them together. Register the IdbAdapter with the
  142. RPCServer to handle RPC requests from the split debugger GUI via the
  143. IdbProxy.
  144. """
  145. gui_proxy = GUIProxy(rpchandler, gui_adap_oid)
  146. idb = debugger.Idb(gui_proxy)
  147. idb_adap = IdbAdapter(idb)
  148. rpchandler.register(idb_adap_oid, idb_adap)
  149. return idb_adap_oid
  150. #=======================================
  151. #
  152. # In the IDLE process:
  153. class FrameProxy:
  154. def __init__(self, conn, fid):
  155. self._conn = conn
  156. self._fid = fid
  157. self._oid = "idb_adapter"
  158. self._dictcache = {}
  159. def __getattr__(self, name):
  160. if name[:1] == "_":
  161. raise AttributeError(name)
  162. if name == "f_code":
  163. return self._get_f_code()
  164. if name == "f_globals":
  165. return self._get_f_globals()
  166. if name == "f_locals":
  167. return self._get_f_locals()
  168. return self._conn.remotecall(self._oid, "frame_attr",
  169. (self._fid, name), {})
  170. def _get_f_code(self):
  171. cid = self._conn.remotecall(self._oid, "frame_code", (self._fid,), {})
  172. return CodeProxy(self._conn, self._oid, cid)
  173. def _get_f_globals(self):
  174. did = self._conn.remotecall(self._oid, "frame_globals",
  175. (self._fid,), {})
  176. return self._get_dict_proxy(did)
  177. def _get_f_locals(self):
  178. did = self._conn.remotecall(self._oid, "frame_locals",
  179. (self._fid,), {})
  180. return self._get_dict_proxy(did)
  181. def _get_dict_proxy(self, did):
  182. if did in self._dictcache:
  183. return self._dictcache[did]
  184. dp = DictProxy(self._conn, self._oid, did)
  185. self._dictcache[did] = dp
  186. return dp
  187. class CodeProxy:
  188. def __init__(self, conn, oid, cid):
  189. self._conn = conn
  190. self._oid = oid
  191. self._cid = cid
  192. def __getattr__(self, name):
  193. if name == "co_name":
  194. return self._conn.remotecall(self._oid, "code_name",
  195. (self._cid,), {})
  196. if name == "co_filename":
  197. return self._conn.remotecall(self._oid, "code_filename",
  198. (self._cid,), {})
  199. class DictProxy:
  200. def __init__(self, conn, oid, did):
  201. self._conn = conn
  202. self._oid = oid
  203. self._did = did
  204. ## def keys(self):
  205. ## return self._conn.remotecall(self._oid, "dict_keys", (self._did,), {})
  206. # 'temporary' until dict_keys is a pickleable built-in type
  207. def keys(self):
  208. return self._conn.remotecall(self._oid,
  209. "dict_keys_list", (self._did,), {})
  210. def __getitem__(self, key):
  211. return self._conn.remotecall(self._oid, "dict_item",
  212. (self._did, key), {})
  213. def __getattr__(self, name):
  214. ##print("*** Failed DictProxy.__getattr__:", name)
  215. raise AttributeError(name)
  216. class GUIAdapter:
  217. def __init__(self, conn, gui):
  218. self.conn = conn
  219. self.gui = gui
  220. def interaction(self, message, fid, modified_info):
  221. ##print("*** Interaction: (%s, %s, %s)" % (message, fid, modified_info))
  222. frame = FrameProxy(self.conn, fid)
  223. self.gui.interaction(message, frame, modified_info)
  224. class IdbProxy:
  225. def __init__(self, conn, shell, oid):
  226. self.oid = oid
  227. self.conn = conn
  228. self.shell = shell
  229. def call(self, methodname, *args, **kwargs):
  230. ##print("*** IdbProxy.call %s %s %s" % (methodname, args, kwargs))
  231. value = self.conn.remotecall(self.oid, methodname, args, kwargs)
  232. ##print("*** IdbProxy.call %s returns %r" % (methodname, value))
  233. return value
  234. def run(self, cmd, locals):
  235. # Ignores locals on purpose!
  236. seq = self.conn.asyncqueue(self.oid, "run", (cmd,), {})
  237. self.shell.interp.active_seq = seq
  238. def get_stack(self, frame, tbid):
  239. # passing frame and traceback IDs, not the objects themselves
  240. stack, i = self.call("get_stack", frame._fid, tbid)
  241. stack = [(FrameProxy(self.conn, fid), k) for fid, k in stack]
  242. return stack, i
  243. def set_continue(self):
  244. self.call("set_continue")
  245. def set_step(self):
  246. self.call("set_step")
  247. def set_next(self, frame):
  248. self.call("set_next", frame._fid)
  249. def set_return(self, frame):
  250. self.call("set_return", frame._fid)
  251. def set_quit(self):
  252. self.call("set_quit")
  253. def set_break(self, filename, lineno):
  254. msg = self.call("set_break", filename, lineno)
  255. return msg
  256. def clear_break(self, filename, lineno):
  257. msg = self.call("clear_break", filename, lineno)
  258. return msg
  259. def clear_all_file_breaks(self, filename):
  260. msg = self.call("clear_all_file_breaks", filename)
  261. return msg
  262. def start_remote_debugger(rpcclt, pyshell):
  263. """Start the subprocess debugger, initialize the debugger GUI and RPC link
  264. Request the RPCServer start the Python subprocess debugger and link. Set
  265. up the Idle side of the split debugger by instantiating the IdbProxy,
  266. debugger GUI, and debugger GUIAdapter objects and linking them together.
  267. Register the GUIAdapter with the RPCClient to handle debugger GUI
  268. interaction requests coming from the subprocess debugger via the GUIProxy.
  269. The IdbAdapter will pass execution and environment requests coming from the
  270. Idle debugger GUI to the subprocess debugger via the IdbProxy.
  271. """
  272. global idb_adap_oid
  273. idb_adap_oid = rpcclt.remotecall("exec", "start_the_debugger",\
  274. (gui_adap_oid,), {})
  275. idb_proxy = IdbProxy(rpcclt, pyshell, idb_adap_oid)
  276. gui = debugger.Debugger(pyshell, idb_proxy)
  277. gui_adap = GUIAdapter(rpcclt, gui)
  278. rpcclt.register(gui_adap_oid, gui_adap)
  279. return gui
  280. def close_remote_debugger(rpcclt):
  281. """Shut down subprocess debugger and Idle side of debugger RPC link
  282. Request that the RPCServer shut down the subprocess debugger and link.
  283. Unregister the GUIAdapter, which will cause a GC on the Idle process
  284. debugger and RPC link objects. (The second reference to the debugger GUI
  285. is deleted in pyshell.close_remote_debugger().)
  286. """
  287. close_subprocess_debugger(rpcclt)
  288. rpcclt.unregister(gui_adap_oid)
  289. def close_subprocess_debugger(rpcclt):
  290. rpcclt.remotecall("exec", "stop_the_debugger", (idb_adap_oid,), {})
  291. def restart_subprocess_debugger(rpcclt):
  292. idb_adap_oid_ret = rpcclt.remotecall("exec", "start_the_debugger",\
  293. (gui_adap_oid,), {})
  294. assert idb_adap_oid_ret == idb_adap_oid, 'Idb restarted with different oid'
  295. if __name__ == "__main__":
  296. from unittest import main
  297. main('idlelib.idle_test.test_debugger', verbosity=2, exit=False)