Debugging C with DAP in Emacs
Table of Contents
Debug Adapter Protocol (DAP) is a protocol in the spirit of LSP, for unifing debugger features and interfaces.
dap-mode
in Emacs provides the necessary ui for Emacs to connect with debuggers. And for any language it can connect to the language specific debugger if we configure it properly.
Many such configuration (called debug-provider
) are bundled with dap-mode
. For C & C++, I use dap-codelldb
. A debug-provider
has a name and provides a function that populates the dap
startup parameters. Usually the provider populates the debugger program location, server port and other similar stuff. For example the dap-codelldb registers a debug-provider named "lldb".
< Collapse code block
(dap-register-debug-provider "lldb" (lambda (conf) (let ((debug-port (dap--find-available-port))) (plist-put conf :program-to-start (format "%s --port %s" dap-codelldb-debug-program debug-port)) (plist-put conf :debugServer debug-port)) (plist-put conf :host "localhost") (plist-put conf :type "lldb") (plist-put conf :cargo "") conf))
For any project we might need to debug muliple program, it is the purpose of debug-template
to specify other parameters such as :program
, :cwd
(working direction) and program arguments. Instead of defining debug-template in elisp, dap also supports .vscode/launch.json
format used by VSCode. A sample launch.json is as follows [See this for launch.json format and this for variables supported in launch.json]:
< Collapse code block
{ "version": "0.2.0", "configurations": [ { "name": "Debug main program", "type": "lldb", "request": "launch", "program": "${workspaceFolder}/out/main", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false } ] }
Or you can specify the same template in elisp. Note that the dap-variables (like "workspaceFolder", "fileDirname", "env:" etc. See code.visualstudio.com) are exapanded properly by dap (see dap-variables-expand
for implementation)
< Collapse code block
(dap-register-debug-template "Debug main" (list :type "lldb" :program "${workspaceFolder}/out/main" :cwd "${workspaceFolder}" :request "launch"))
1. Installation
M-x package-install dap-mode
: Install dap-mode(require 'dap-codelldb)
: Load thedap-codelldb
provider (it is bundled with dap-mode)M-x dap-codelldb-setup
: Install the debug server program