def attach_connect(session, target, method, cwd=None, wait=True, log_dir=None): log.info("Attaching {0} to {1} by socket using {2}.", session, target, method.upper()) assert method in ("api", "cli") config = _attach_common_config(session, target, cwd) config["connect"] = {} config["connect"]["host"] = host = attach_connect.host config["connect"]["port"] = port = attach_connect.port if method == "cli": args = [ os.path.dirname(debugpy.__file__), "--listen", compat.filename_str(host) + ":" + str(port), ] if wait: args += ["--wait-for-client"] if log_dir is not None: args += ["--log-to", log_dir] if "subProcess" in config: args += ["--configure-subProcess", str(config["subProcess"])] debuggee_setup = None elif method == "api": args = [] api_config = {k: v for k, v in config.items() if k in {"subProcess"}} debuggee_setup = """ import debugpy if {log_dir!r}: debugpy.log_to({log_dir!r}) debugpy.configure({api_config!r}) debugpy.listen(({host!r}, {port!r})) if {wait!r}: debugpy.wait_for_client() """ debuggee_setup = fmt( debuggee_setup, host=host, port=port, wait=wait, log_dir=log_dir, api_config=api_config, ) else: raise ValueError args += target.cli(session.spawn_debuggee.env) try: del config["subProcess"] except KeyError: pass session.spawn_debuggee(args, cwd=cwd, setup=debuggee_setup) if wait: session.wait_for_adapter_socket() session.connect_to_adapter((host, port)) return session.request_attach()
def attach_by_socket(session, target, method, listener="server", cwd=None, wait=True, log_dir=None): log.info("Attaching {0} to {1} by socket using {2}.", session, target, method.upper()) assert method in ("api", "cli") assert listener in ("server") # TODO: ("adapter", "server") config = _attach_common_config(session, target, cwd) host = config["host"] = attach_by_socket.host port = config["port"] = attach_by_socket.port if method == "cli": args = [os.path.dirname(ptvsd.__file__)] if wait: args += ["--wait"] args += ["--host", compat.filename_str(host), "--port", str(port)] if not config["subProcess"]: args += ["--no-subprocesses"] if log_dir is not None: args += ["--log-dir", log_dir] debug_me = None elif method == "api": args = [] debug_me = """ import ptvsd ptvsd.enable_attach(({host!r}, {port!r}), {args}) if {wait!r}: ptvsd.wait_for_attach() """ attach_args = "" if log_dir is None else fmt("log_dir={0!r}", log_dir) debug_me = fmt(debug_me, host=host, port=port, wait=wait, args=attach_args) else: raise ValueError args += target.cli(session.spawn_debuggee.env) session.spawn_debuggee(args, cwd=cwd, debug_me=debug_me) if wait: session.wait_for_enable_attach() session.connect_to_adapter((host, port)) return session.request_attach()