Skip to content

Commit 755845a

Browse files
authored
dap-python: Support attach to an existing process (#759)
- 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.
1 parent c99c103 commit 755845a

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

dap-python.el

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ strings, for the sake of launch.json feature parity."
177177
(plist-get conf :program)
178178
(buffer-file-name)))
179179
(module (plist-get conf :module))
180-
(debugger (plist-get conf :debugger)))
180+
(debugger (plist-get conf :debugger))
181+
(targetPid (plist-get conf :processId)))
181182
;; These are `dap-python'-specific and always ignored.
182183
(cl-remf conf :debugger)
183184
(cl-remf conf :target-module)
@@ -196,14 +197,17 @@ strings, for the sake of launch.json feature parity."
196197
(cl-remf conf :module)
197198
(cl-remf conf :args)
198199
(plist-put conf :program-to-start
199-
(format "%s%s -m ptvsd --wait --host %s --port %s%s %s%s"
200+
(format "%s%s -m ptvsd --wait --host %s --port %s %s"
200201
(or dap-python-terminal "")
201202
(shell-quote-argument python-executable)
202203
host
203204
debug-port
204-
(if module (concat " -m " (shell-quote-argument module)) "")
205-
(if program (shell-quote-argument program) "")
206-
(if (not (string-empty-p python-args)) (concat " " python-args) "")))
205+
(if targetPid
206+
(format "--pid %s" targetPid)
207+
(format "%s %s %s"
208+
(if module (concat " -m " (shell-quote-argument module)) "")
209+
(if program (shell-quote-argument program) "")
210+
(if (not (string-empty-p python-args)) (concat " " python-args) "")))))
207211
(plist-put conf :debugServer debug-port)
208212
(plist-put conf :port debug-port)
209213
(plist-put conf :hostName host)
@@ -235,11 +239,12 @@ strings, for the sake of launch.json feature parity."
235239
(unless (plist-get conf :cwd)
236240
(cl-remf conf :cwd))
237241

238-
(pcase (plist-get conf :request)
239-
("launch"
242+
(pcase (cons (plist-get conf :request) targetPid)
243+
((or `("launch" . nil)
244+
`("attach" . ,(and pid (guard pid))))
240245
(plist-put conf :dap-server-path
241246
(list python-executable "-m" "debugpy.adapter")))
242-
("attach"
247+
(`("attach" . nil)
243248
(let* ((connect (plist-get conf :connect))
244249
(host (or (plist-get connect :host) "localhost"))
245250
(port (or (plist-get connect :port) 5678)))
@@ -260,6 +265,11 @@ strings, for the sake of launch.json feature parity."
260265
(dap-python--populate-start-file-args conf))
261266

262267
(dap-register-debug-provider "python" 'dap-python--populate-start-file-args)
268+
(dap-register-debug-template "Python :: Attach to running process"
269+
(list :type "python"
270+
:request "attach"
271+
:processId "${command:pickProcess}"
272+
:name "Python :: Attach to running process"))
263273
(dap-register-debug-template "Python :: Run file (buffer)"
264274
(list :type "python"
265275
:args ""

docs/page/configuration.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,27 @@ These parameters are handled by templates of type `python`:
182182
183183
Remaining parameters are forwarded to the respective debugger.
184184
185+
4. Attach to an existing process
186+
`dap-python` supports also the "attach" mode to attach and debug a long running script.
187+
A template named "Python :: Attach to running process" is also pre-registered for this purpose.
188+
```elisp
189+
(dap-register-debug-template "Python :: Attach to running process"
190+
(list :type "python"
191+
:request "attach"
192+
:processId "${command:pickProcess}"
193+
:name "Python :: Attach to running process"))
194+
```
195+
The `${command:pickProcess}` configuration variable used by default to facilitate the user
196+
selecting the debug process by a completion popping up window. The real `processId` can
197+
always be specified using its *pid*.
198+
**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")
199+
wrapped inside the GDB tool, which means that for distributions that enable YAMA (e.g. Ubuntu) some additional steps may be necessary:
200+
- Install GDB.
201+
- Turn on classic ptrace permission
202+
```bash
203+
sudo sh -c 'echo 0 > /proc/sys/kernel/yama/ptrace_scope'
204+
```
205+
185206
186207
## Ruby
187208

0 commit comments

Comments
 (0)