一、直接使用GDB调试程序

①嵌入式性能、内存充足的情况下

下载任意版本gdb,交叉编译后,拷贝到嵌入式目标板直接运行gdb

②嵌入式性能、内存不足的情况下,使用gdbserver

下载任意版本gdb,交叉编译后,将gdbserver拷贝到嵌入式目标板
在嵌入式目标板 (ip 192.168.1.2) 运行如下命令

$ gdbserver :port bin_file -v

在PC调试环境 (ip 192.168.1.1) 运行如下命令,其中arm-gdb 是你的交叉编译工具链目录下的一个gdb工具,这里的arm-gdb不是下载的gdb交叉编译后的程序,下载的gdb交叉编译后只能在目标嵌入式板运行,交叉编译工具链目录下的gdb在PC调试环境运行。如arm-linux-gnueabihf-gdb 。
** 注意运行程序只能用c,不能用r **

$ arm-gdb bin_file
(gdb) target remote 192.168.1.2:port
... (设置断点等gdb用法)
(gdb) c

二、调试coredump

①嵌入式性能、内存充足的情况下

下载任意版本gdb,交叉编译后,拷贝到嵌入式目标板直接运行

gdb bin_file core_file
(gdb) bt
②嵌入式性能、内存不足的情况下

在PC调试环境运行如下命令

arm-gdb bin_file core_file
(gdb) bt

三、交叉编译gdb

http://t.csdn.cn/RGWlX

四、通过vscode-gdbserver调试

gdbserver部分如上配置

vscode配置(ubuntu环境):

1、添加配置

点击小齿轮
在这里插入图片描述
选择最后的(gdb) xxx ,该模式为仅调试不编译,其他模式会创建task.json,根据该配置文件编译工程后再调试。这里默认手动编译后,仅单独调试
在这里插入图片描述
点击后跳转launch.json

2、配置launch.json文件

①、 name:调试的项目名
②、 program:需要调试的应用程序可执行文件路径。
③、 cwd:需要调试的应用程序源码路径。
④、 miDebuggerPath:此项需要手动添加,用于指定所使用的交叉编译器 gdb 路径。
⑤、 miDebuggerServerAddress:此项需要手动添加,远程 gdbserver 服务器地址。
⑥、environment:指定环境变量

{
    "configurations": [
        {
            "name": "(gdb) 远程调试",
            "type": "cppdbg",
            "request": "launch",
            "program": "/home/chensong/work/lib/libUsbCan/cmd/debug.linux/usbcan",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [
                {
                    "name": "LD_LIBRARY_PATH",
                    "value": "${workspaceFolder}/output/lib"
                }
            ],
            "externalConsole": false,
            "miDebuggerPath": "/usr/bin/gdb",
            "miDebuggerServerAddress": "192.168.137.61:2345",
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "将反汇编风格设置为 Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ]
        }
    ],
    "version": "2.0.0"
}

3、开始调试

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐