def enable_attach(address=(DEFAULT_HOST, DEFAULT_PORT), redirect_output=None, log_dir=None): """Enables a client to attach to this process remotely to debug Python code. Parameters ---------- address : (str, int), optional Specifies the interface and port on which the debugging server should listen for TCP connections. It is in the same format as used for regular sockets of the `socket.AF_INET` family, i.e. a tuple of ``(hostname, port)``. On client side, the server is identified by the Qualifier string in the usual ``'hostname:port'`` format, e.g.: ``'myhost.cloudapp.net:5678'``. Default is ``('0.0.0.0', 5678)``. redirect_output : bool, optional (Deprecated) Specifies whether any output (on both `stdout` and `stderr`) produced by this program should be sent to the debugger. Default is ``True``. log_dir : str, optional Name of the directory that debugger will create its log files in. If not specified, logging is disabled. Return ------ Returns tuple (host, port) as used to by the debugging server. If `enable_attach` was called with port '0'. The return value will contain the actual ephemeral port number. Notes ----- This function returns immediately after setting up the debugging server, and does not block program execution. If you need to block until debugger is attached, call `ptvsd.wait_for_attach`. The debugger can be detached and re-attached multiple times after `enable_attach` is called. Only the thread on which this function is called, and any threads that are created after it returns, will be visible in the debugger once it is attached. Any threads that are already running before this function is called will not be visible. """ if log_dir: ptvsd.options.log_dir = log_dir ptvsd.log.to_file() ptvsd.log.info('enable_attach{0!r}', (address, redirect_output)) if redirect_output is not None: ptvsd.log.info('redirect_output deprecation warning.') warnings.warn(_redirect_output_deprecation_msg, DeprecationWarning, stacklevel=2) if is_attached(): ptvsd.log.info('enable_attach() ignored - already attached.') return debugger_attached.clear() # Ensure port is int port = address[1] address = (address[0], port if type(port) is int else int(port)) ptvsd_enable_attach(address) return (address[0], ptvsd.options.port)
def enable_attach(address=(DEFAULT_HOST, DEFAULT_PORT), redirect_output=True, log_dir=None): """Enables a client to attach to this process remotely to debug Python code. Parameters ---------- address : (str, int), optional Specifies the interface and port on which the debugging server should listen for TCP connections. It is in the same format as used for regular sockets of the `socket.AF_INET` family, i.e. a tuple of ``(hostname, port)``. On client side, the server is identified by the Qualifier string in the usual ``'hostname:port'`` format, e.g.: ``'myhost.cloudapp.net:5678'``. Default is ``('0.0.0.0', 5678)``. redirect_output : bool, optional Specifies whether any output (on both `stdout` and `stderr`) produced by this program should be sent to the debugger. Default is ``True``. log_dir : str, optional Name of the directory that debugger will create its log files in. If not specified, logging is disabled. Notes ----- This function returns immediately after setting up the debugging server, and does not block program execution. If you need to block until debugger is attached, call `ptvsd.wait_for_attach`. The debugger can be detached and re-attached multiple times after `enable_attach` is called. Only the thread on which this function is called, and any threads that are created after it returns, will be visible in the debugger once it is attached. Any threads that are already running before this function is called will not be visible. """ if log_dir: ptvsd.options.log_dir = log_dir ptvsd.log.to_file() ptvsd.log.info('enable_attach{0!r}', (address, redirect_output)) if is_attached(): ptvsd.log.info('enable_attach() ignored - already attached.') return debugger_attached.clear() # Ensure port is int port = address[1] address = (address[0], port if type(port) is int else int(port)) ptvsd_enable_attach( address, redirect_output=redirect_output, )
def enable_attach(address=(DEFAULT_HOST, DEFAULT_PORT), redirect_output=True): """Enables a client to attach to this process remotely to debug Python code. Parameters ---------- address : (str, int), optional Specifies the interface and port on which the debugging server should listen for TCP connections. It is in the same format as used for regular sockets of the `socket.AF_INET` family, i.e. a tuple of ``(hostname, port)``. On client side, the server is identified by the Qualifier string in the usual ``'hostname:port'`` format, e.g.: ``'myhost.cloudapp.net:5678'``. Default is ``('0.0.0.0', 5678)``. redirect_output : bool, optional Specifies whether any output (on both `stdout` and `stderr`) produced by this program should be sent to the debugger. Default is ``True``. Notes ----- This function returns immediately after setting up the debugging server, and does not block program execution. If you need to block until debugger is attached, call `ptvsd.wait_for_attach`. The debugger can be detached and re-attached multiple times after `enable_attach` is called. Only the thread on which this function is called, and any threads that are created after it returns, will be visible in the debugger once it is attached. Any threads that are already running before this function is called will not be visible. """ global _enabled if _enabled: return _enabled = True _attached.clear() # Note: this only starts pydevd (e.g. sets it up) and enables # debugging for *future* threads. It does not actually enable # debugging in the *current* thread. That is done in # wait_for_attach(). Thus this approach is problematic if # wait_for_attach() is never called. # TODO: Is there any way to ensure that debug_current_thread() # gets called in the current thread, regardless of if # wait_for_attach() gets called? _, wait, debug_current_thread = ptvsd_enable_attach( address, on_attach=_attached.set, redirect_output=redirect_output, ) global _debug_current_thread _debug_current_thread = debug_current_thread # Give it a chance to finish starting. This helps reduce possible # issues due to relying on wait_for_attach(). if wait(WAIT_TIMEOUT): debug_current_thread() else: _pending_threads.add(threading.current_thread().ident)
def enable_attach(address=(DEFAULT_HOST, DEFAULT_PORT), redirect_output=True): """Enables a client to attach to this process remotely to debug Python code. Parameters ---------- address : (str, int), optional Specifies the interface and port on which the debugging server should listen for TCP connections. It is in the same format as used for regular sockets of the `socket.AF_INET` family, i.e. a tuple of ``(hostname, port)``. On client side, the server is identified by the Qualifier string in the usual ``'hostname:port'`` format, e.g.: ``'myhost.cloudapp.net:5678'``. Default is ``('0.0.0.0', 5678)``. redirect_output : bool, optional Specifies whether any output (on both `stdout` and `stderr`) produced by this program should be sent to the debugger. Default is ``True``. Notes ----- This function returns immediately after setting up the debugging server, and does not block program execution. If you need to block until debugger is attached, call `ptvsd.wait_for_attach`. The debugger can be detached and re-attached multiple times after `enable_attach` is called. Only the thread on which this function is called, and any threads that are created after it returns, will be visible in the debugger once it is attached. Any threads that are already running before this function is called will not be visible. """ if is_attached(): return debugger_attached.clear() # Ensure port is int port = address[1] address = (address[0], port if type(port) is int else int(port)) global _attach_port _attach_port = address[1] ptvsd_enable_attach( address, redirect_output=redirect_output, )