nvim-dap - C++ - codelldb - clang lldb

https://github.com/vadimcn/codelldb

mac


local dap = require("dap")
local cmd = os.getenv('HOME') .. '/tool/debug/codelldb/adapter/codelldb'

dap.adapters.codelldb = function(on_adapter)
  -- This asks the system for a free port
  local tcp = vim.loop.new_tcp()
  tcp:bind('127.0.0.1', 0)
  local port = tcp:getsockname().port
  tcp:shutdown()
  tcp:close()

  -- Start codelldb with the port
  local stdout = vim.loop.new_pipe(false)
  local stderr = vim.loop.new_pipe(false)
  local opts = {
    stdio = {nil, stdout, stderr},
    args = {'--port', tostring(port)},
  }
  local handle
  local pid_or_err
  handle, pid_or_err = vim.loop.spawn(cmd, opts, function(code)
    stdout:close()
    stderr:close()
    handle:close()
    if code ~= 0 then
      print("codelldb exited with code", code)
    end
  end)
  if not handle then
    vim.notify("Error running codelldb: " .. tostring(pid_or_err), vim.log.levels.ERROR)
    stdout:close()
    stderr:close()
    return
  end
  vim.notify('codelldb started. pid=' .. pid_or_err)
  stderr:read_start(function(err, chunk)
    assert(not err, err)
    if chunk then
      vim.schedule(function()
        require("dap.repl").append(chunk)
      end)
    end
  end)
  local adapter = {
    type = 'server',
    host = '127.0.0.1',
    port = port
  }
  -- 💀
  -- Wait for codelldb to get ready and start listening before telling nvim-dap to connect
  -- If you get connect errors, try to increase 500 to a higher value, or check the stderr (Open the REPL)
  vim.defer_fn(function() on_adapter(adapter) end, 500)
end

dap.configurations.cpp = {
  {
    name = "runit",
    type = "codelldb",
    request = "launch",

    program = function()
      return vim.fn.input('', vim.fn.getcwd() .. '/bin/testdir/', 'file')
    end,

    args = {"--log_level=all"},
    cwd = "${workspaceFolder}/bin/testdir",
    stopOnEntry = false,
    terminal = 'integrated',

    pid = function()
            local handle = io.popen('pgrep hw$')
            local result = handle:read()
            handle:close()
            return result
    end
  },
}

dap.configurations.c = dap.configurations.cpp
dap.configurations.rust = dap.configurations.cpp

dapui = require("dapui")
dap.listeners.after.event_initialized["dapui_config"] = function()
  dapui.open()
end
dap.listeners.before.event_terminated["dapui_config"] = function()
  dapui.close()
end
dap.listeners.before.event_exited["dapui_config"] = function()
  dapui.close()
end

m$ msys2 clang64

clang lldb

local dap = require("dap")
local cmd = 'C:\\gnu\\tool\\codelldb\\extension\\adapter\\codelldb.exe'

dap.adapters.codelldb = function(on_adapter)
  -- This asks the system for a free port
  local tcp = vim.loop.new_tcp()
  tcp:bind('127.0.0.1', 0)
  local port = tcp:getsockname().port
  tcp:shutdown()
  tcp:close()

  -- Start codelldb with the port
  local stdout = vim.loop.new_pipe(false)
  local stderr = vim.loop.new_pipe(false)
  local opts = {
    stdio = {nil, stdout, stderr},
    args = {'--port', tostring(port)},
  }
  local handle
  local pid_or_err
  handle, pid_or_err = vim.loop.spawn(cmd, opts, function(code)
    stdout:close()
    stderr:close()
    handle:close()
    if code ~= 0 then
      print("codelldb exited with code", code)
    end
  end)
  if not handle then
    vim.notify("Error running codelldb: " .. tostring(pid_or_err), vim.log.levels.ERROR)
    stdout:close()
    stderr:close()
    return
  end
  vim.notify('codelldb started. pid=' .. pid_or_err)
  stderr:read_start(function(err, chunk)
    assert(not err, err)
    if chunk then
      vim.schedule(function()
        require("dap.repl").append(chunk)
      end)
    end
  end)
  local adapter = {
    type = 'server',
    host = '127.0.0.1',
    port = port
  }
  -- 💀
  -- Wait for codelldb to get ready and start listening before telling nvim-dap to connect
  -- If you get connect errors, try to increase 500 to a higher value, or check the stderr (Open the REPL)
  vim.defer_fn(function() on_adapter(adapter) end, 500)
end

dap.configurations.cpp = {
  {
    name = "runit",
    type = "codelldb",
    request = "launch",

    program = function()
      return vim.fn.input('', vim.fn.getcwd() .. '/bin', 'file')
    end,

    args = {},
    cwd = '${workspaceFolder}/bin',
    stopOnEntry = false,
    terminal = 'integrated',

    pid = function()
            local handle = io.popen('pgrep hw$')
            local result = handle:read()
            handle:close()
            return result
    end
  },
}

dap.configurations.c = dap.configurations.cpp
dap.configurations.rust = dap.configurations.cpp

dapui = require("dapui")
dap.listeners.after.event_initialized["dapui_config"] = function()
  dapui.open()
end
dap.listeners.before.event_terminated["dapui_config"] = function()
  dapui.close()
end
dap.listeners.before.event_exited["dapui_config"] = function()
  dapui.close()
end

lldb CLI

lldb .\hello.exe
(lldb) target create ".\\hello.exe"
(rrent executable set to 'C:\Users\dev\Desktop\cpp\bin\hello.exe' (x86_64).
(lldb) b main
Breakpoint 1: where = hello.exe`main + 22 at hello.cpp:148:5, address = 0x0000000140001676
(lldb) r
(lldb) Process 4052 launched: 'C:\Users\dev\Desktop\cpp\bin\hello.exe' (x86_64)
Process 4052 stopped
* thread #1, stop reason = breakpoint 1.1
    frame #0: 0x00007ff69f201676 hello.exe`main at hello.cpp:148:5
   145  //--------------------------------------------------------------------------------------------------
   146
   147  int main() {
-> 148      test();
   149      basicTest();
   150      daTest();
   151      use_case();
(lldb)
posted @   fndefbwefsowpvqfx  阅读(1112)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
历史上的今天:
2022-03-31 Physical Device Surface Formats
2022-03-31 Physical Device Queue Family Properties
2022-03-31 Physical Device Properties Features
点击右上角即可分享
微信分享提示