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