VScode debugging: /dev/gpiomem Permission denied


Goal: I want to set up the VScode debugging on Ubuntu with a Raspberry Pi 400.

What I have done: I am able to compile and run the program using sudo ./program_name. I have to run sudo otherwise I get permission denied error. I would like to not run VScode with root privileges as its generally discouraged.

The problem: When I launch the debugger (press F5) and I include wiringPi.h, I get the following error:

wiringPiSetup: Unable to open /dev/mem or /dev/gpiomem: Permission denied.
  Aborting your program because if it can not access the GPIO
  hardware then it most certianly won't work
  Try running with sudo?
[1] + Done                       "/usr/bin/gdb" --interpreter=mi --tty=${DbgTerm} 0<"/tmp/Microsoft-MIEngine-In-j40gmjsr.mas" 1>"/tmp/Microsoft-MIEngine-Out-vltmyj1b.a3g"

My launch.json file looks like this:

    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
            "name": "C/C++ - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "preLaunchTask": "C/C++: g++ build active file",
            "miDebuggerPath": "/usr/bin/gdb"

What should I be doing to be able to launch the debugger with my setup? Can I add something to the launch.json file?


gdb does not have permission to open /dev/mem or /dev/gpiomem. There are two approaches you can go forward with.

Option 1 — Elevate GDB’s permissions

Instead of running vscode as root (which was already suggested, and you rightfully pointed out this is generally a bad idea), you can run gdb from within vscode as root, by having it call sudo gdb to launch the debugger as compared to just gdb.

Option 2 — De-escelated /dev/mem and /dev/gpiomem

The other option is to give yourself access to /dev/mem and /dev/gpiomem without elevating your permissions. /dev/gpiomem is mostly harmless to give access to, but allowing non-root access to /dev/mem is a major security hole and not advisable. If it all possible, go with option 1.

To give your use permission to open those files, you may do one of two things.

1.) Add yourself to the owning group. /dev/mem is owned by the group kmem, so adding your main user to that group will allow you read access.

2.) Modifying file permissions. sudo chmod ugo+xwr fname will allow any user to read, write, or execute that file. This is generally insecure, but helpful if you activate it only for a moment to solve some issue.

Answered By – Carson

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published