emacs-lsp / dap-mode

Emacs :heart: Debug Adapter Protocol
https://emacs-lsp.github.io/dap-mode
GNU General Public License v3.0
1.3k stars 182 forks source link

Initialize request failed: "program" property is required for launch #716

Closed iamtoaster closed 2 months ago

iamtoaster commented 1 year ago

So, I am trying to debug my rust code. I initialized dap-mode in my init.el like this:

(use-package dap-mode
  :ensure t
  :config
  (dap-ui-mode)
  (dap-ui-controls-mode 1)

  (require 'dap-node)

  (require 'dap-codelldb)
  (require 'dap-gdb-lldb)
  ;; installs .extension/vscode
  (dap-gdb-lldb-setup)
  (dap-register-debug-template
   "Rust::LLDB Run Configuration"
   (list :type "lldb"
         :request "launch"
         :name "LLDB::Run"
     :gdbpath "rust-lldb"
         ;; uncomment if lldb-mi is not in PATH (it is)
         ;; :lldbmipath "path/to/lldb-mi"
         )))

Then, I open my rust project, do M-x dap-debug, select the "Rust::LLDB Run Configuration", press enter... And get the *Warnings* frame with the message you can see in the title.

The code to setup dap-mode that I use is a bit modified version of this code, but with dap-lldb replaced with dap-codelldb. That is because when I had dap-lldb, whenever I attempted to use dap-debug with that template, all I got from dap-mode is "make sure you loaded lldb specific dap package".

That project also has a launch.json that was generated by the CodeLLDB extension for VSCode (the configurations defined there work in VSCode, and I didn't modify them), and dap-mode seems to pick it up, but when I try to use one of them, same thing happens.

So, my question is, how do I debug my rust code with dap-mode, because apparently, even google does not know?

redacted-moose commented 8 months ago

Having spent multiple hours trying to figure this out myself, I'll document what I've learned here.

It looks like there's an lldb-mi launch type, this blog post gives an example configuration. Looks like yours was close, you need to use (require 'dap-lldb) instead of (require 'dap-codelldb). I haven't tried this however.

If you don't want to use lldb-mi however, read on.

There's some additional docs here for using GDB as a backend. This seems to be what most people actually use.

codelldb and the lldb backend are actually two different things, and trying to use them together results in a confusing experience because it seems they both provide an 'lldb' launch type that takes different parameters. To avoid this confusion, the following snippet registers a new debug provider called codelldb which uses the codelldb backend to debug native programs.

  (require 'dap-codelldb)

  (dap-codelldb-setup)

  ;; Register a dap-debug provider with interactive file input because dap-codelldb doesn't provide one :sigh:
  (defun dap-codelldb--populate-args (conf)
    "Populate CONF with the required arguments."
    (let ((debug-port (dap--find-available-port)))
      (dap--put-if-absent conf :program-to-start (format "%s --port %s" dap-codelldb-debug-program debug-port))
      (dap--put-if-absent conf :debugServer debug-port))
    (-> conf
        (dap--put-if-absent :host "localhost")
        (dap--put-if-absent :type "lldb")
        (dap--put-if-absent :cargo "")
        (dap--put-if-absent :cwd default-directory)
        (dap--put-if-absent :program (expand-file-name (read-file-name "Select file to debug.")))
        (dap--put-if-absent :name "CodeLLDB Debug")))

  (dap-register-debug-provider "codelldb" #'dap-codelldb--populate-args)
  (dap-register-debug-template
   "CodeLLDB Debug"
   (list :type "codelldb"
         :cwd nil
         :request "launch"
         :program nil
         :name "CodeLLDB Debug")))

Now when you M-x dap-debug and pick the "CodeLLDB Debug" option, you should be prompted to pick a file to debug and dap-mode should handle the rest. You can also override the launch template if you so choose.

Finally, if you're using lsp-mode with rust-analyzer, you can just M-x lsp-rust-analyzer-debug - but this requires you to setup up the cpptools backend with (require 'dap-cpptools) and has a few other quirks (like no pretty printing by default it seems).