Skip to content

dap-python: Support attach to an existing process #759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions dap-python.el
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ strings, for the sake of launch.json feature parity."
(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)))
;; These are `dap-python'-specific and always ignored.
(cl-remf conf :debugger)
(cl-remf conf :target-module)
Expand All @@ -196,14 +197,17 @@ strings, for the sake of launch.json feature parity."
(cl-remf conf :module)
(cl-remf conf :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)) "")
(if program (shell-quote-argument program) "")
(if (not (string-empty-p python-args)) (concat " " python-args) "")))
(if targetPid
(format "--pid %s" targetPid)
(format "%s %s %s"
(if module (concat " -m " (shell-quote-argument module)) "")
(if program (shell-quote-argument program) "")
(if (not (string-empty-p python-args)) (concat " " python-args) "")))))
(plist-put conf :debugServer debug-port)
(plist-put conf :port debug-port)
(plist-put conf :hostName host)
Expand Down Expand Up @@ -235,11 +239,12 @@ strings, for the sake of launch.json feature parity."
(unless (plist-get conf :cwd)
(cl-remf conf :cwd))

(pcase (plist-get conf :request)
("launch"
(pcase (cons (plist-get conf :request) targetPid)
((or `("launch" . nil)
`("attach" . ,(and pid (guard pid))))
(plist-put conf :dap-server-path
(list python-executable "-m" "debugpy.adapter")))
("attach"
(`("attach" . nil)
(let* ((connect (plist-get conf :connect))
(host (or (plist-get connect :host) "localhost"))
(port (or (plist-get connect :port) 5678)))
Expand All @@ -260,6 +265,11 @@ strings, for the sake of launch.json feature parity."
(dap-python--populate-start-file-args conf))

(dap-register-debug-provider "python" 'dap-python--populate-start-file-args)
(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 file (buffer)"
(list :type "python"
:args ""
Expand Down
21 changes: 21 additions & 0 deletions docs/page/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,27 @@ These parameters are handled by templates of type `python`:

Remaining parameters are forwarded to the respective debugger.

4. Attach to an existing process
`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'
```


## Ruby

Expand Down