vscode下调试代码时发现程序需要root权限才能运行!

尝试 sudo code . 不成功



解决办法

1. 修改 launch.json

{
  // 使用 IntelliSense 了解相关属性。 
  // 悬停以查看现有属性的描述。
  // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "name": "(gdb) 启动",
      "type": "cppdbg",
      "request": "launch",
      "program": "${workspaceFolder}/relay/dhcrelay",
      "args": ["-d", "-i", "eth0", "192.168.18.1"],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}/relay",
      "environment": [],
      "externalConsole": false,
      "MIMode": "gdb",
      // 增加 miDebuggerPath=
      "miDebuggerPath":"${workspaceFolder}/relay/gdb",
      "setupCommands": [
        {
          "description": "为 gdb 启用整齐打印",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        },
        {
          "description": "将反汇编风格设置为 Intel",
          "text": "-gdb-set disassembly-flavor intel",
          "ignoreFailures": true
        }
      ]
    }
  ]
}


2. 创建 ${workspaceFolder}/relay/gdb

#!/bin/bash
sudo /usr/bin/gdb "$@"


3. 配置/etc/sudoers

$ sudo su -
# chmod a+w /etc/sudoers
# vi /etc/sudoers
{用户名}     ALL=(ALL:ALL) NOPASSWD:/usr/bin/gdb

# chmod a-w /etc/sudoers
# exit