@@ -143,6 +143,12 @@ are **never accessed during normal execution**. The ``debugger_pending_call`` fi
143143indicates when a debugger has requested execution, while ``debugger_script ``
144144provides Python code to be executed when the interpreter reaches a safe point.
145145
146+ The value for ``MAX_SCRIPT_SIZE `` will be a trade-off between binary size and
147+ how big debugging scripts can be. As most of the logic should be in libraries
148+ and arbitrary code can be executed with very short ammount of Python we are
149+ proposing to start with 4kb initially. This value can be extended in the future
150+ if we ever need to.
151+
146152
147153Debug Offsets Table
148154-------------------
@@ -191,7 +197,8 @@ When a debugger wants to attach to a Python process, it follows these steps:
191197
1921985. Write control information:
193199
194- - Write python code to be executed into the ``debugger_script `` field in ``_PyRemoteDebuggerSupport ``
200+ - Write a string of Python code to be executed into the ``debugger_script ``
201+ field in ``_PyRemoteDebuggerSupport ``.
195202 - Set ``debugger_pending_call `` flag in ``_PyRemoteDebuggerSupport ``
196203 - Set ``_PY_EVAL_PLEASE_STOP_BIT `` in the ``eval_breaker `` field
197204
@@ -232,6 +239,11 @@ is checked.
232239 }
233240
234241
242+ If the code being executed raises any Python exception it will be processed as
243+ an `unraisable exception
244+ <https://docs.python.org/3/c-api/exceptions.html#c.PyErr_WriteUnraisable> `__ in
245+ the thread where the code was executed.
246+
235247Python API
236248----------
237249
@@ -242,13 +254,16 @@ arbitrary Python code within the context of a specified Python process:
242254
243255.. code-block :: python
244256
245- def remote_exec (pid : int , code : str ) -> None :
257+ def remote_exec (pid : int , code : str , timeout : int = 0 ) -> None :
246258 """
247259 Executes a block of Python code in a given remote Python process.
248260
249261 Args:
250262 pid (int): The process ID of the target Python process.
251263 code (str): A string containing the Python code to be executed.
264+ timeout (int): An optional timeout for waiting for the remote
265+ process to execute the code. If the timeout is exceeded a
266+ ``TimeoutError`` will be raised.
252267 """
253268
254269 An example usage of the API would look like:
@@ -258,7 +273,9 @@ An example usage of the API would look like:
258273 import sys
259274 # Execute a print statement in a remote Python process with PID 12345
260275 try :
261- sys.remote_exec(12345 , " print('Hello from remote execution!')" )
276+ sys.remote_exec(12345 , " print('Hello from remote execution!')" , timeout = 3 )
277+ except TimeoutError :
278+ print (f " The remote process took too long to execute the code " )
262279 except Exception as e:
263280 print (f " Failed to execute code: { e} " )
264281
@@ -341,7 +358,30 @@ debugging tool stability and reliability.
341358Reference Implementation
342359========================
343360
344- https://github.com/pablogsal/cpython/commits/remote_pdb/
361+ A reference implementation with a prototype adding remote support for ``pdb ``
362+ can be found `here
363+ <https://github.com/pablogsal/cpython/compare/60ff67d010078eca15a74b1429caf779ac4f9c74...remote_pdb> `__.
364+
365+ Rejected Ideas
366+ ==============
367+
368+ Using a path as the debugger input
369+ ----------------------------------
370+
371+ We have selected that the mechanism for executing remote code is that tools
372+ write the code directly in the remote process to eliminate a possible security
373+ vulnerability in which the file to be executed can be altered by parties other
374+ than the debugger process if permissions are not set correctly or filesystem
375+ configurations allow for this to happen. It is also trivial to write code that
376+ executes the contents of a file so the current mechanism doesn't disallow tools
377+ that want to just execute files to just do so if they are ok with the security
378+ profile of such operation.
379+
380+ Thanks
381+ ======
382+
383+ We would like to thank Carl Friedrich Bolz-Tereick for his insightful comments and suggestions
384+ when discussing this proposal.
345385
346386
347387Copyright
0 commit comments