操作环境
PC: OS Debian9.4.0, Qt 5.11.0, IP: 192.168.1.10
BeagleBone Black: Debian 9.3.0, IP: 192.168.1.20
前言
在嵌入式开发中,如何使用QT去调试程序是很重要的。调试无非分为以下几种方式:
- 最初级的方式就是编译目标程序,然后拷贝到开发板,通过printf函数输出一些调试信息来调试程序。
- 移植GDB,编译完目标程序后,在开发板运行GDB server,然后使用QT远程调试。
- 使用QT一键远程调试程序,需要将目标程序拷贝到开发板。
方式1不仅麻烦还很难找出问题。方式2虽然可以调试,但很麻烦,每次编译完程序后都得拷贝到开发板。第3种方式灵活简单。
步骤
1.安装gdb
- PC安装GNU多架构调试器:
apt-get install gdb-multiarch
devin@debian-PC:~$ gdb --version
GNU gdb (Debian 7.12-6) 7.12.0.20161007-git
...
- BBB安装gdbserver
apt-get install gdbserver
devin@beaglebone:~/demo$ gdbserver --version
GNU gdbserver (Debian 7.12-6) 7.12.0.20161007-git
Copyright (C) 2016 Free Software Foundation, Inc.
gdbserver is free software, covered by the GNU General Public License.
This gdbserver was configured as "arm-linux-gnueabihf"
2.配置Qt Kit
- 根据交叉编译的qmake新增Qt Version。参考此博客
- 编译器
- 调试工具
- 配置Kit
- 配置Device
Qt Creator可以通过Device中设置的SSH将可执行文件上传到BBB上。
点击右侧的Test菜单可以查询设置的端口范围内可用的端口。
- 配置sysroot(不清楚作用)
3.测试
- PC IP: 192.168.1.10
- BBB IP: 192.168.1.20
3.1配置Qt Projects
其中工程文件中需要添加以下内容:
target.files = armtest1 # your local executable file.
target.path = /home/devin/gdb # executable file's path on your device(beaglebone). there is no slash in the end of the path.
INSTALLS += target
添加环境变量,否则可能出现这个错误:QXcbConnection: Could not connect to display
3.2 部署可执行文件
- Deploy或者Run之后BBB的 /home/devin/gdb 目录中会多出一个可执行文件armtest1 (需要提前配置好Kit中的Device)。
上传文件时可能会出现下列错误:
error: Upload of file “/home/devin/qtdemo/build-armtest2-Beaglebone_Kit-Debug/armtest1” failed. The server said: “Permission denied”.
可能原因是Device选项卡中设置的SSH登录用户没有权限访问armtest1.pro文件中设置的target.path路径,导致无法上传文件。
- 或者使用sftp将可执行文件armtest1手动传输到BBB上
3.3 一键调试
- ssh登录BBB后使用xhost命令允许别的用户(Device中设置的root用户)启动的图形程序将图形显示在当前屏幕上(BBB默认登录用户为debian)。可以修改默认登录用户。
debian@beaglebone:~$ echo $DISPLAY
debian@beaglebone:~$ xhost
xhost: unable to open display ""
debian@beaglebone:~$ export DISPLAY=:0.0
debian@beaglebone:~$ xhost
access control enabled, only authorized clients can connect
SI:localuser:debian
debian@beaglebone:~$ xhost +
access control disabled, clients can connect from any host
- 使用 F5 / F10调试,注意在3.1中设置DISPLAY环境变量。
3.4 BBB上开启gdbserver手动调试
- 端口号和下图中设置的端口号保持一致,此处port=12345
命令格式: gdbserver <主机IP>:<端口> <可执行程序>
devin@beaglebone:~/demo$ gdbserver :12345 armtest1
Process armtest1 created; pid = 19835
Listening on port 12345
//下面为连接上之后的提示
Remote debugging from host 192.168.1.10
...
- 配置 Debug/Start Debugging/Attach to running debug server,点击OK
- 记得先添加断点(Break at “main”表示在main函数断点)
- F10单步调试,F5全速运行