Debugger

Flow

Debugger.excalidraw

  • Debugger: Cho phép dev monitor và control execution step-by-step
  • Debug Adapter Protocol:
    • Standardized communication protocol - Giao thức giữa IDE/Editor với Debugger. Quy định/ convert các thông tin về sending commands, receiving events, exchange data.
    • Hiểu nôm na là kênh trung gian, convert command/response từ debugger để hiển thị lên IDE/Editor, và ngược lại.
    • DAP do Microsoft phát triển, cho phép các IDE khác nhau có thể sử dụng cùng 1 giao thức để tương tác với nhiều debuggers của các ngôn ngữ khác nhau.
  • Debugger Adapter

Notes

Debugger Engine: debase or ruby-debug-base Backend (Read more)

IDE: RubyMine, VS Code, Eclipse Frontend

Ruby Debugger: - pry - byebug - ruby/debug

ruby-debug-ide Ruby Debugger - Protocol to establish communication between them.

Redirect commands from IDE to the debugger engine answers and events

Source: https://github.com/ruby-debug/ruby-debu g-ide/blob/master/protocol-spec.md

Debug Adapter - ‘gadgets’: The Debug Adapter Protocol defines the protocol used between an editor or IDE and a debugger or runtime. https://microsoft.github.io/debug-adapter-protocol/

Vimspector: ‘middle man’ that talks to a debugger. provides a standard protocol to communicate with different debuggers. With Vimspector, you can communicate the same way with a Node debugger, Python debugger, Go debugger, etc.

2 mode: Attach vs Launch Launch: Chạy debugger trên chính running process attach: Chạy rails process, sau đó attach Vimspector vào. (file config sẽ bỏ phần programs và args)

rdebug-ide --host 0.0.0.0 --port 1234 --dispatcher-port 1234 -- bin/rails s

This will launch the ruby-debug-ide program

https://github.com/puremourning/vimspector

https://github.com/puremourning/vimspector/wiki/Additional-Language-Support#ruby-vimspector-config

Windows: https://puremourning.github.io/vimspector-web/#ui-overview

Mapping https://github.com/puremourning/vimspector#mappings

https://dev.to/iggredible/debugging-in-vim-with-vimspector-4n0m

https://code.visualstudio.com/blogs/2018/08/07/debug-adapter-protocol-website


Python in VSCode

https://wiki.python.org/moin/PythonDebuggingTools


A debug adapter is a program that can communicate with a debugger UI using the Debug Adapter Protocol. An adapter can act as a bridge between the UI and a separate debugger (such as GDB or LLDB), or can be a debugger in and of itself (such as “vsdbg”, which supports CoreCLR debugging on Linux and macOS). T


Cần 2 cái:

  1. A relevant gadget: Debug adapter: https://github.com/puremourning/vimspector#supported-languages
  2. Vimspector config file
{
  "configurations": {
    "run": {
      "adapter": "vscode-node",
      "breakpoints": {
        "exception": {
          "all": "N",
          "uncaught": "N"
        }
      },
      "configuration": {
        "request": "launch",
        "protocol": "auto",
        "stopOnEntry": true,
        "console": "integratedTerminal",
        "program": "${workspaceRoot}/simple.js",
        "cwd": "${workspaceRoot}"
      }
    }
  }
}
 
{
  "configurations": {
    "Python: Launch": {
      "adapter": "debugpy",
      "filetypes": [ "python" ],
      "breakpoints": {
        "exception": {
          "caught": "N",
          "uncaught": "Y"
        }
      },
      "configuration": {
        "name": "Python: Launch",
        "type": "python",
        "request": "launch",
        "cwd": "${fileDirname}",
        "python": "${workspaceRoot}/.venv/bin/python",
        "stopOnEntry": false,
        "console": "externalTerminal",
        "debugOptions": [],
        "module": "dynatrace_account_exporter",
        "env": {
          "PYTHONPATH": "${workspaceRoot}"
        },
        "args": [
        ]
      }
    }
  }
}

To traverse through the file, you can either:

  1. Step out (steps out of the scope)
  2. Step into (steps into the function scope)
  3. Step over (steps to the next line, in scope) - Next 1 step

Here are the key maps that I use. Note that I use hlj keys similar to the Vim movement keys.

nmap <Leader>dh <Plug>VimspectorStepOut
nmap <Leader>dl <Plug>VimspectorStepInto
nmap <Leader>dj <Plug>VimspectorStepOver

If you are not sure what stepping out, stepping into, and stepping over are, I found these short video tutorials very helpful:

  1. Variables window
  2. Watch window: Watch for specific values.
  3. Stack Trace window
  4. Console window
  5. Terminal window

Issue with debugpy when debugging in multi sessions: Lý do cơ bản không debug được với multi sessions là vì nó implement không đúng với spec, nên nhiều thằng không hỗ trợ =)) https://github.com/mfussenegger/nvim-dap/issues/362

vim.keymap.set("n", "gd", function() vim.lsp.buf.definition() end, opts)
vim.keymap.set("n", "K", function() vim.lsp.buf.hover() end, opts)
vim.keymap.set("n", "<leader>vws", function() vim.lsp.buf.workspace_symbol() end, opts)
vim.keymap.set("n", "<leader>vd", function() vim.diagnostic.open_float() end, opts)
vim.keymap.set("n", "[d", function() vim.diagnostic.goto_next() end, opts)
vim.keymap.set("n", "]d", function() vim.diagnostic.goto_prev() end, opts)
vim.keymap.set("n", "<leader>vca", function() vim.lsp.buf.code_action() end, opts)
vim.keymap.set("n", "<leader>vrr", function() vim.lsp.buf.references() end, opts)
vim.keymap.set("n", "<leader>vrn", function() vim.lsp.buf.rename() end, opts)
vim.keymap.set("i", "<C-h>", function() vim.lsp.buf.signature_help() end, opts)

Questions