-
Notifications
You must be signed in to change notification settings - Fork 183
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
support delve/dlv dap-mode directly #318
Comments
Has there been any developments with regards to this feature request? I would be interested as well. |
vscode-go's node.js adapter is no longer actively maintained, so it would be great if emacs dap mode directly uses Delve's DAP documentation: https://github.com/go-delve/delve/blob/master/Documentation/api/dap/README.md note: |
(setq dap-go-debug-program (executable-find "dlv"))
(defun dap-go--populate-dap-args (conf)
(setq conf (pcase (plist-get conf :mode)
("auto" (dap-go--populate-auto-args conf))
("debug" (dap--put-if-absent conf :program (f-dirname (buffer-file-name))))
("test" (dap--put-if-absent conf :program (buffer-file-name)))
("exec" (dap--put-if-absent conf :program (read-file-name "enter full path to executable without tilde:")))
("local"
(dap--put-if-absent conf :cwd (f-dirname (buffer-file-name)))
(dap--put-if-absent conf :processId (string-to-number (read-string "Enter pid: " "2345"))))
))
(let ((debug-port (dap--find-available-port)))
(plist-put conf :host "localhost")
(plist-put conf :program-to-start (format "%s dap --listen 127.0.0.1:%s" dap-go-debug-program debug-port))
(plist-put conf :debugServer debug-port))
(if (stringp (plist-get conf :args)) (plist-put conf :args (split-string (plist-get conf :args))) ())
(-> conf
(dap--put-if-absent :type "go")
(dap--put-if-absent :name "Go Debug")))
(dap-register-debug-provider "go" 'dap-go--populate-dap-args) That is, remove the line contains (let ((debug-port (dap--find-available-port)))
(plist-put conf :host "localhost")
(plist-put conf :program-to-start (format "%s dap --listen 127.0.0.1:%s" dap-go-debug-program debug-port))
(plist-put conf :debugServer debug-port)) |
I tried above patch, but I got some error with args passed.
I'll try to find out why it failed, but if anybody has guess let me know. |
Thanks @wsw0108! Really, really appreciate it I don't think the snippets were for the latest version.. This worked for me to debug a golang unit test at least. Which is what I'm after, I think it should work with other profiles as well. (require 'dap-mode)
(require 'dap-utils)
(setq dap-go-debug-program (executable-find "dlv"))
(defcustom dap-go-delve-path (or (executable-find "dlv")
(expand-file-name "dlv" (expand-file-name "bin" (getenv "GOPATH"))))
"The path to the delve command."
:group 'dap-go
:type 'string)
(defun dap-go--populate-default-args (conf)
"Populate CONF with the default arguments."
(setq conf (pcase (plist-get conf :mode)
("auto" (dap-go--populate-auto-args conf))
("debug" (dap--put-if-absent conf :program (f-dirname (buffer-file-name))))
("exec" (dap--put-if-absent conf :program (read-file-name "enter full path to executable without tilde:")))
("remote" (dap--put-if-absent conf :program (f-dirname (buffer-file-name)))
(dap--put-if-absent conf :host (read-string "enter host:" "127.0.0.1"))
(dap--put-if-absent conf :port (string-to-number (read-string "Enter port: " "2345"))))
("local"
(dap--put-if-absent conf :cwd (f-dirname (buffer-file-name)))
(dap--put-if-absent conf :processId (string-to-number (read-string "Enter pid: " "2345"))))
))
(let ((debug-port (dap--find-available-port)))
(plist-put conf :host "localhost")
(plist-put conf :program-to-start (format "%s dap --listen 127.0.0.1:%s" dap-go-debug-program debug-port))
(plist-put conf :debugServer debug-port))
(if (stringp (plist-get conf :args)) (plist-put conf :args (split-string (plist-get conf :args))) ())
(-> conf
(dap--put-if-absent :dlvToolPath dap-go-delve-path)
(dap--put-if-absent :packagePathToGoModPathMap
(ht<-alist `((,(f-dirname (buffer-file-name)) . ,(lsp-find-session-folder (lsp-session) (buffer-file-name))))))
(dap--put-if-absent :type "go")
(dap--put-if-absent :name "Go Debug")))
(defun dap-go--populate-auto-args (conf)
"Populate auto arguments."
(dap--put-if-absent conf :program (buffer-file-name))
(if (string-suffix-p "_test.go" (buffer-file-name))
(plist-put conf :mode "test")
(plist-put conf :mode "debug")))
(dap-register-debug-provider "go" 'dap-go--populate-default-args)
(dap-register-debug-template "Go Launch File Configuration"
(list :type "go"
:request "launch"
:name "Launch File"
:mode "auto"
:program nil
:buildFlags nil
:args nil
:env nil
:envFile nil))
(provide 'dap-go)
;;; dap-go.el ends here
|
Is it possible to debug remotely with this solution? |
I had old dlv, solution did not work with it. |
I have latest dlv, but can't create working debug template for remote debug. |
Yeah, I'm having some trouble being able to run tests properly if the implementation is elsewhere. I think it comes down to the fact that I don't know enough about how Delve works. This should hopefully help you as well: (setq dap-print-io t) You will see the DAP messages going over the wire with the above configuration EDIT: I think in my case, it seems to do with (dap--put-if-absent :packagePathToGoModPathMap
(ht<-alist `((,(f-dirname (buffer-file-name)) . ,(lsp-find-session-folder (lsp-session) (buffer-file-name)))))) |
I'm not sure It's not supported over DAP interface.
Again, I haven't dug deep enough for understanding if I'm doing it right or if I'm assuming too much. I'm curious to know about your use case, would you mind sharing some details on how you're setting it up? |
Sure. (dap-register-debug-template "Go Dlv Remote Debug"
(list :type "go"
:request "attach"
:name "Dlv Remote Debug"
:mode "remote"
:program nil
:buildFlags nil
:args nil
:env nil
:envFile nil)) |
Did you start the FYI this is how vscode-go plugin utilizes dlv-dap for |
Yes. And it works for other team members with Goland and doesn't work for me with emacs and dap-mode |
I think there is two options:
and two modes how server operates:
this ticket: |
with this patch to dap-go.el above I can get remote template to work with local dap-server:
my main consern is that they might depricate the rpc version since already dap is used as default in vs-code when debuging locally. |
VS Code Go extension dev team stopped supporting the legacy debug adapter long ago in favor of the native DAP protocol support from delve and plans to remove the code sometime this year. @polinasok made the traditional dlv commands ( |
I know this would work in remote debug if I could get the if statement to work (I really can not understand elisp logic):
variable I try to make is just some kind of reference and once it is deleted inside the if (remote) check execution fails. start the server as 'dlv dap --headless --listen=:12345 --log --log-output=rpc,dap,debugger' |
@apmattil try |
comparison in if statement is not the problem, I get error (about mode missing from conf ?): |
|
@sata You must use |
As described in https://github.com/go-delve/delve/blob/master/Documentation/api/dap/README.md and https://github.com/golang/vscode-go/blob/master/docs/debugging.md#remote-debugging, you have two options for working with a remote debugger: Option 1
Locally start the client and tell the server what to debug at the remote location:
or
Option 2
or
Locally start the client to attach (aka connect) to an existing remote session:
Also, the DAP server is perfectly happy to take a subset of relevant optional attributes like |
No program in remote mode. To use the remote mode, you should already have a dlv server that specified the program on the command line when it was started.
should be "attach" if used with "remote" mode below
Don't need program in remote attach mode as the remote server should already be tracing a target program. This attribute will be ignored.
If this matches vscode-go, then debugServer is a way to specify local port only and is generally advertised for development and testing. host/port is what is advertised to users when connecting to remote or external debug server. If host is omitted, localhost is implied.
Yes. (The mode names are not ideal and we considered revising them, but then decided to stick with the familiar legacy names, so peoples configurations could be used as-is or with minimal changes as we switched adapters behind the scenes.)
|
Thanks for correcting me! I spent some time in the weekend looking at the delve documentation and also debugging delve to understand it better, got it working in the end :) |
sata-form3 That's great to hear! I am adding more info to the documentation, so hopefully it will be more clear now. But please don't hesitate to let us know if there is more that can be clarified to make onboarding easier. |
My current setup: (require 'dap-mode)
(require 'dap-utils)
(setq dap-dlv-go-debug-program (executable-find "dlv"))
(defcustom dap-dlv-go-delve-path (or (executable-find "dlv")
(expand-file-name "dlv" (expand-file-name "bin" (getenv "GOPATH"))))
"The path to the delve command."
:group 'dap-dlv-go
:type 'string)
(defun dap-dlv-go--populate-default-args (conf)
"Populate CONF with the default arguments."
(setq conf (pcase (plist-get conf :mode)
("auto" (dap-dlv-go--populate-auto-args conf))
("debug" (dap--put-if-absent conf :program (f-dirname (buffer-file-name))))
("exec" (dap--put-if-absent conf :program (read-file-name "enter full path to executable without tilde:")))
("remote" (dap--put-if-absent conf :program (f-dirname (buffer-file-name)))
(dap--put-if-absent conf :host (read-string "enter host:" "127.0.0.1"))
(dap--put-if-absent conf :debugPort (string-to-number (read-string "Enter port: " "2345"))))
("local"
(dap--put-if-absent conf :cwd (f-dirname (buffer-file-name)))
(dap--put-if-absent conf :processId (string-to-number (read-string "Enter pid: " "2345"))))))
(let ((debug-port (if (string= (plist-get conf :mode)
"remote")
(plist-get conf :debugPort)
(dap--find-available-port))))
(dap--put-if-absent conf :host "localhost")
(when (not (string= "remote" (plist-get conf :mode)))
(plist-put conf :program-to-start (format "%s dap --listen 127.0.0.1:%s --log" dap-dlv-go-debug-program debug-port)))
(plist-put conf :debugServer debug-port))
(if (stringp (plist-get conf :args)) (plist-put conf :args (split-string (plist-get conf :args))) ())
(-> conf
(dap--put-if-absent :dlvToolPath dap-dlv-go-delve-path)
(dap--put-if-absent :packagePathToGoModPathMap
(ht<-alist `((,(f-dirname (buffer-file-name)) . ,(lsp-find-session-folder (lsp-session) (buffer-file-name))))))
(dap--put-if-absent :type "go")
(dap--put-if-absent :name "Go Debug")))
(defun dap-dlv-go--populate-auto-args (conf)
"Populate auto arguments."
(dap--put-if-absent conf :program (buffer-file-name))
(if (string-suffix-p "_test.go" (buffer-file-name))
(plist-put conf :mode "test")
(plist-put conf :mode "debug")))
(dap-register-debug-provider "go" 'dap-dlv-go--populate-default-args)
(dap-register-debug-template "Go Dlv Launch File Configuration"
(list :type "go"
:request "launch"
:name "Launch File"
:mode "auto"
:program nil
:buildFlags nil
:args nil
:env nil
:envFile nil))
(dap-register-debug-template "Go Dlv Remote Debug"
(list :type "go"
:request "attach"
:name "Dlv Remote Debug"
:mode "remote"
:program nil
:buildFlags nil
:args nil
:env nil
:envFile nil))
(provide 'dap-dlv-go) Then start application on remote machine and start delve on remote machine:
Then connect to debugger from emacs by remote debug template and can't debug. In logs I can see:
Looks like I have missed something. UPD. Maybe it fails because binary that I try to debug has been build inside podman container and has other source paths. But I'm not sure. UPD 2. Bingo! When I build binary locally and put it into container all works fine. |
@polinasok Thank you! Without your explanation I can't make it work. |
Should we use delve directly as only option or should we provide both options? I can provide PR, but we should make the choice. As soon as other option is deprecated maybe we should chose first option, but this code is not tested enough yet. |
I vote for option 1. |
Your binary is built with some set of paths. With remote debugging (or when using symlinks or when building with From https://github.com/golang/vscode-go/blob/master/docs/debugging.md#remote-debugging
|
is the '${workspaceFolder}' vscode variable ? |
About the remote options described in #318 (comment). Option 1 is new and inspired by the DAP model. It makes it easy for the user to switch back and forth between letting the client launch the debugger for them or connecting to their own (redirecting output, issuing signals in the terminal, etc) with just a change of the port field. This also means that a client can ask the remote server to debug any arbitrary code (potential security concern). This is also not available in multi-client mode - e.g. if you want to keep reconnecting to your session. Option 2 is the more familiar traditional option that delve users have been used to for a while. It's a power option of sorts. It requires more familiarity with the dlv command-line args. It makes it possible to connect other clients to the server (terminal, GoLand, etc.) Vscode users found use cases for both. |
Yes, sorry, it's just the client-side root. I will make an edit to the example. |
Oh and one more note. Host can very be localhost. Some users use port forwarding. Others might want this option to use with a local, but external server, so they get access to the terminal where it was launched to enter stdin, issue signals, redirect output. |
By options I mean
Both solutions can be used locally or remotely, with exec or attach etc. @polinasok do you mean the same? |
option one has options sub options @polinasok mentioned. |
@polinasok yes you are right.. just checking host option might not be so great idea, but checking port is just confusing (IMOH). host check version here anyway:
|
No, I misunderstood. I thought you were referring to my [earlier comment](#318 (comment) with two options for remote. In vscode we replaced old with new under the hood back in summer 2021 for everything that uses |
Instead of using vs-code adapter we could use dlv dap-mode directly.. this would eliminate need to install nodejs, vs-code adapter.
now dlv dap-mode is ready : go-delve/delve#1515
vs-code adapter itself is starting to use it, as well vim work has started.
The text was updated successfully, but these errors were encountered: