Skip to content

Instantly share code, notes, and snippets.

@zethon
Last active March 13, 2025 14:58
Show Gist options
  • Save zethon/e5381174f9de9db73559534ead587c34 to your computer and use it in GitHub Desktop.
Save zethon/e5381174f9de9db73559534ead587c34 to your computer and use it in GitHub Desktop.
Getting VSCode's CMakeTools on Win10 to Launch a WSL2 Process as Root

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:


1) Use a Small “Wrapper” Script

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.

Steps

  1. 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 "$@"
  2. Make it executable:
    chmod +x /usr/local/bin/gdb-sudo.sh
  3. In .vscode/settings.json, tell CMake Tools to use this script:
    {
      // ... other settings
      "cmake.debugConfig": {
        "MIMode": "gdb",
        "miDebuggerPath": "/usr/local/bin/gdb-sudo.sh", // Use the wrapper
        "setupCommands": [
          {
            "description": "Enable pretty printing",
            "text": "-enable-pretty-printing",
            "ignoreFailures": true
          }
        ]
      }
    }
  4. (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:
    yourusername ALL=(ALL) NOPASSWD: /usr/bin/gdb
    
    Adjust carefully for your security needs!

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.


2) Use a “--” Delimiter in miDebuggerArgs

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.


Summary

  • Problem: CMake Tools is appending --interpreter=mi (and others) to the debugger command. If your miDebuggerPath 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 your miDebuggerArgs so sudo stops parsing options.

Either way, you will be able to run GDB as root under WSL2 without “unrecognized option” complaints from sudo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment