diff --git a/dap-python.el b/dap-python.el index 5be2838a..e273a094 100644 --- a/dap-python.el +++ b/dap-python.el @@ -194,7 +194,8 @@ overriden in individual `dap-python' launch configurations." (plist-get conf :program) (buffer-file-name))) (module (plist-get conf :module)) - (debugger (plist-get conf :debugger))) + (debugger (plist-get conf :debugger)) + (targetPid (plist-get conf :processId))) (cl-remf conf :debugger) (pcase (or debugger dap-python-debugger) ('ptvsd @@ -205,14 +206,17 @@ overriden in individual `dap-python' launch configurations." (when (sequencep python-args) (setq python-args (mapconcat #'shell-quote-argument python-args " "))) (plist-put conf :program-to-start - (format "%s%s -m ptvsd --wait --host %s --port %s%s %s %s" - (or dap-python-terminal "") + (format "%s%s -m ptvsd --wait --host %s --port %s %s" + (or (and dap-python-terminal (concat dap-python-terminal " ") "")) (shell-quote-argument python-executable) host debug-port - (if module (concat " -m " (shell-quote-argument module)) "") - (shell-quote-argument program) - python-args)) + (if targetPid + (format "--pid %s" targetPid) + (format "%s %s %s" + (if module (concat " -m " (shell-quote-argument module)) "") + (shell-quote-argument program) + python-args)))) (plist-put conf :debugServer debug-port) (plist-put conf :port debug-port) (plist-put conf :hostName host) @@ -238,7 +242,8 @@ overriden in individual `dap-python' launch configurations." (cl-remf conf :module)) (unless (plist-get conf :cwd) (cl-remf conf :cwd)) - + (unless targetPid + (cl-remf conf :listen)) (plist-put conf :dap-server-path (list python-executable "-m" "debugpy.adapter")))) (plist-put conf :program program) @@ -262,6 +267,12 @@ overriden in individual `dap-python' launch configurations." :request "launch" :name "Python :: Run file (buffer)")) +(dap-register-debug-template "Python :: Attach to running process" + (list :type "python" + :request "attach" + :processId "${command:pickProcess}" + :name "Python :: Attach to running process")) + (dap-register-debug-template "Python :: Run pytest (buffer)" (list :type "python" :args "" diff --git a/docs/page/configuration.md b/docs/page/configuration.md index 09113ad1..0474c448 100644 --- a/docs/page/configuration.md +++ b/docs/page/configuration.md @@ -109,6 +109,31 @@ settings. :name "My App")) ``` + `dap-python` supports also the "attach" mode to attach and debug a long running script. + A template named "Python :: Attach to running process" is also pre-registered for this purpose. + ```elisp + (dap-register-debug-template "Python :: Attach to running process" + (list :type "python" + :request "attach" + :processId "${command:pickProcess}" + :name "Python :: Attach to running process")) + ``` + The `${command:pickProcess}` configuration variable used by default to facilitate the user + selecting the debug process by a completion popping up window. The real `processId` can + always be specified using its *pid*. + + **Note**: on Linux this is achieved using the [ptrace syscall](https://www.man7.org/linux/man-pages/man2/ptrace.2.html "ptrace syscall man page") + wrapped inside the GDB tool, which means that for distributions that enable YAMA (e.g. Ubuntu) some additional steps may be necessary: + - Install GDB. + - Turn on classic ptrace permission + ```bash + sudo sh -c 'echo 0 > /proc/sys/kernel/yama/ptrace_scope' + ``` + Or starting the debugger under root user + ```elisp + (setq dap-python-terminal "sudo ") + ``` + ## Ruby - Download and extract [VSCode Ruby