From 077b9dab189d38d437db89ea8140bd262fb2210c Mon Sep 17 00:00:00 2001 From: Tai Dinh Date: Mon, 12 Oct 2020 00:06:27 +0200 Subject: [PATCH 1/3] dap-python: Support attach mode - New configuration attribute `processId` is added. This name is picked to provide a consistent interface for both debugpy and ptvsd as well as make it works with launch.json naturally. --- dap-python.el | 23 +++++++++++++++++------ docs/page/configuration.md | 25 +++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/dap-python.el b/dap-python.el index 5be2838a..b5c8ade0 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" + (format "%s%s -m ptvsd --wait --host %s --port %s %s" (or 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..55ca1cc3 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 mean below additional steps are also need to be done as well: + - 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 From a73a23b2001cac71a0eecd7fc293476016586b78 Mon Sep 17 00:00:00 2001 From: Tai Dinh Date: Mon, 12 Oct 2020 20:41:20 +0200 Subject: [PATCH 2/3] Update dap-python.el to allow none trailing whitespace dap-python-terminal --- dap-python.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dap-python.el b/dap-python.el index b5c8ade0..e273a094 100644 --- a/dap-python.el +++ b/dap-python.el @@ -207,7 +207,7 @@ overriden in individual `dap-python' launch configurations." (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" - (or dap-python-terminal "") + (or (and dap-python-terminal (concat dap-python-terminal " ") "")) (shell-quote-argument python-executable) host debug-port From 8edfefb79a96a1d49ea864343db1495f0cd12cd9 Mon Sep 17 00:00:00 2001 From: Tai Dinh Date: Mon, 12 Oct 2020 20:41:54 +0200 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Nikita Bloshchanevich --- docs/page/configuration.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/page/configuration.md b/docs/page/configuration.md index 55ca1cc3..0474c448 100644 --- a/docs/page/configuration.md +++ b/docs/page/configuration.md @@ -123,7 +123,7 @@ settings. 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 mean below additional steps are also need to be done as well: + 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 @@ -131,7 +131,7 @@ settings. ``` Or starting the debugger under root user ```elisp - setq dap-python-terminal "sudo " + (setq dap-python-terminal "sudo ") ``` ## Ruby