You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PetaLinux的VSCode调试器中使用sudo运行代码?

在PetaLinux中使用sudo权限调试GDB程序

问题场景

我在PetaLinux环境中使用sudo权限运行C程序,示例命令:

$ sudo ./test  # 注:原命令中`sduo`为笔误,应为`sudo`

我的PetaLinux版本信息:

$ uname -a
Linux petalinux_project 5.15.36-xilinx-v2022.2 #1 SMP PREEMPT Mon Oct 3 07:50:07 UTC 2022 armv7l armv7l armv7l GNU/Linux

现在我想要使用gdb调试器,但不知道如何添加sudo权限。若不使用sudo运行调试器,我的程序就会崩溃。当前我的launch.json配置如下:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug",
            "type": "gdb",
            "request": "launch",
            "target": <exe_file_path>,
            "cwd": "${workspaceRoot}",
            "valuesFormatting": "parseText",
        }
    ]
}

解决方法

方案1:通过sudo启动GDB

修改launch.json,指定用sudo调用GDB,同时传递目标程序路径:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug with sudo",
            "type": "gdb",
            "request": "launch",
            "program": "<exe_file_path>",  // 替换为你的可执行文件实际路径
            "cwd": "${workspaceRoot}",
            "valuesFormatting": "parseText",
            "miDebuggerPath": "/usr/bin/sudo",
            "miDebuggerArgs": "/usr/bin/gdb --interpreter=mi <exe_file_path>"  // 替换为你的可执行文件实际路径
        }
    ]
}

方案2:配置GDB以sudo运行目标程序

通过miDebuggerArgs传递运行参数,让GDB以sudo权限启动程序:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug with sudo",
            "type": "gdb",
            "request": "launch",
            "program": "<exe_file_path>",  // 替换为你的可执行文件实际路径
            "cwd": "${workspaceRoot}",
            "valuesFormatting": "parseText",
            "miDebuggerArgs": "-ex 'run sudo <exe_file_path>'"
        }
    ]
}

关键注意事项

  • 修正配置字段:原配置中的target字段需改为program,这是VS Code GDB调试的标准配置字段。
  • 免密sudo设置:为避免调试时卡在密码输入环节,可通过visudo命令添加规则:your_username ALL=(ALL) NOPASSWD: /usr/bin/gdb(替换your_username为实际用户名)。
  • 前置验证:先确保sudo ./<exe_file_path>能正常运行程序,排除程序本身的权限外问题。

内容的提问来源于stack exchange,提问作者RookieRyan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 23:42:54