Skip to content
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

dap-python: Support attach mode #384

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
25 changes: 18 additions & 7 deletions dap-python.el
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nbfalcon not part of this review, but do we support vterm here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yyoncho we could use vterm here, but not with the facilities that I have already implemented for "runInTerminal", as they depend on a debug-session. However, adding vterm support to dap-debug shouldn't be that much effort (basically let-bind vterm-shell to :program-to-start and then call (vterm)). I could implement it if I have some more spare time.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be max. 20 lines of Elisp, probably 4-10.

(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)
Expand All @@ -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)
Expand All @@ -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 ""
Expand Down
25 changes: 25 additions & 0 deletions docs/page/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down