@@ -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 " "
0 commit comments