When you set "miDebuggerPath": "/usr/bin/sudo"
and "miDebuggerArgs": "/usr/bin/gdb"
, CMake Tools (or the Microsoft C++ debug adapter) then appends extra GDB flags like --interpreter=mi
– but those end up being passed to sudo rather than gdb. Sudo complains about unrecognized options (anything starting with a dash), and so it fails.
You need a way for sudo itself to ignore those GDB options, so that they pass through to gdb. There are two main workarounds:
One of the simplest solutions is to create a one-line shell script that runs gdb under sudo, and then reference that script as your debugger path. This way, the extra flags appended by VS Code or CMake Tools go to gdb correctly.
- Create a script (for example
/usr/local/bin/gdb-sudo.sh
):#!/bin/bash # Simple wrapper that invokes GDB with sudo, passing all arguments along exec sudo /usr/bin/gdb "$@"
- Make it executable:
chmod +x /usr/local/bin/gdb-sudo.sh
- In .vscode/settings.json, tell CMake Tools to use this script:
- (Optional) Passwordless usage: If you do not want to type a password each time, modify your
/etc/sudoers
to allow running gdb with no password:
Adjust carefully for your security needs!yourusername ALL=(ALL) NOPASSWD: /usr/bin/gdb
That’s it. When you start debugging through CMake Tools, it will call /usr/local/bin/gdb-sudo.sh
, which in turn calls sudo gdb ...
, and all the extra --interpreter=mi
or other flags will pass correctly to gdb rather than being interpreted by sudo.
Another approach is to pass --
to sudo, which tells sudo “stop parsing options here.” Then everything after --
is treated as a positional argument (the command to run). For example:
{
"cmake.debugConfig": {
"MIMode": "gdb",
// The actual program we run is still "sudo"
"miDebuggerPath": "/usr/bin/sudo",
// Then we use the double-dash plus the command we want to run
"miDebuggerArgs": "-- /usr/bin/gdb",
"setupCommands": [
{
"description": "Enable pretty printing",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
}
With that, the debug adapter may end up calling something like:
/usr/bin/sudo -- /usr/bin/gdb --interpreter=mi --tty=/dev/pts/2 ...
Now --interpreter=mi
is no longer treated as an option to sudo. However, depending on how the debug adapter appends arguments, you may still run into argument ordering issues. The wrapper script is usually more predictable.
- Problem: CMake Tools is appending
--interpreter=mi
(and others) to the debugger command. If yourmiDebuggerPath
is/usr/bin/sudo
, those “dashed” flags look like sudo options and cause errors. - Fix: Wrap the call so that GDB gets those flags, not sudo. The most reliable approach is the wrapper script. Alternatively, you can try adding
--
in yourmiDebuggerArgs
so sudo stops parsing options.
Either way, you will be able to run GDB as root under WSL2 without “unrecognized option” complaints from sudo.