Skip to content

DAP support rust

Zeioth edited this page Aug 8, 2023 · 13 revisions

Create a .solution file in your working directory. Then pass the -g parameter to the compiler like this

[HELLO WORLD]
entry_point="/path/to/my/entry_point_file/main.rs"
output="/path/where/the/program/will/be/written/hello_world"
parameters="-g"

That will tell the compiler to compile in debug mode.

How to setup DAP to debug with Rust

Please note that this section has nothing to do with compiler.nvim. I'm documenting this to make your life easier. To debug Rust with DAP you have to:

Here you have an example of how to configure DAP for rust

local dap = require("dap")

-- Rust
dap.adapters.codelldb = {
  type = 'server',
  port = "${port}",
  executable = {
    command = vim.fn.stdpath('data')..'/mason/bin/codelldb',
    args = {"--port", "${port}"},
    detached = function() if vim.fn.has('win32') == 1 then return false else return true end end,
  }
}
dap.configurations.rust = {
  {
    name = 'Launch',
    type = 'lldb',
    request = 'launch',
    program = function() -- Ask the user what executable wants to debug
      return vim.fn.input('Path to executable: ', vim.fn.getcwd() .. '/bin/program', 'file')
    end,
    cwd = '${workspaceFolder}',
    stopOnEntry = false,
    args = {},
    initCommands = function() -- add rust types support
      -- Find out where to look for the pretty printer Python module
      local rustc_sysroot = vim.fn.trim(vim.fn.system('rustc --print sysroot'))

      local script_import = 'command script import "' .. rustc_sysroot .. '/lib/rustlib/etc/lldb_lookup.py"'
      local commands_file = rustc_sysroot .. '/lib/rustlib/etc/lldb_commands'

      local commands = {}
      local file = io.open(commands_file, 'r')
      if file then
        for line in file:lines() do
    table.insert(commands, line)
        end
        file:close()
      end
      table.insert(commands, 1, script_import)

      return commands
    end,
  }
}

How to check if everything works

Compile your program with Build solution, add a break point to your code, and then run DAP. You will see something like this.

screenshot_2023-08-08_02-33-14_125206035

Congratulations, now you can compile and debug Rust.

Troubleshooting

  • If you find any issue while configuring DAP, run :DapSetLogLevel trace and :DapShowLog to find the reason.
Clone this wiki locally