如何利用Vscode + gdbserver远程调试
1、修改终端IP地址配置为动态获取IP (或者正确修改DNS服务器配置和网关)
请先将终端ip地址设置为DHCP方式,否则apt update可能会失败
1.1 修改以下配置文件:
# sudo vi /etc/network/interfaces.d/FE0
auto FE0
allow-hotplug FE0
iface FE0 inet dhcp
1.2 保存重启终端,确认终端正确获取ip地址
sysadm@SCT230A:~$ ifconfig
FE0 Link encap:Ethernet HWaddr 26:a5:78:57:8c:0d
inet addr:192.168.2.238 Bcast:192.168.2.255 Mask:255.255.255.0
inet6 addr: fe80::24a5:78ff:fe57:8c0d/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:461935 errors:0 dropped:0 overruns:0 frame:0
TX packets:131747 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:48093705 (48.0 MB) TX bytes:55169793 (55.1 MB)
Interrupt:117
1.3 执行apt update命令,确认工作正常。
sysadm@SCT230A:~$ sudo apt update
Hit:1 https://download.docker.com/linux/ubuntu xenial InRelease
Hit:2 http://ports.ubuntu.com xenial InRelease
Hit:3 http://ports.ubuntu.com xenial-security InRelease
Hit:4 http://ports.ubuntu.com xenial-updates InRelease
Hit:5 http://ports.ubuntu.com xenial-backports InRelease
Hit:6 http://ports.ubuntu.com/ubuntu-ports xenial InRelease
Reading package lists... Done
Building dependency tree
Reading state information... Done
155 packages can be upgraded. Run 'apt list --upgradable' to see them.
2、给OS打补丁
2.1 将/tools/dpkg_gdbserver_patch目录下的dpkg.tar.gz 和 patch_dpkg_gdbserver.sh上传到终端
wcq@wcq-Vostro-3583:dpkg_gdbserver_patch$ ls -lh
总用量 1.9M
-rw-r--r-- 1 wcq wcq 1.9M Mar 16 18:55 dpkg.tar.gz
-rwxrwxr-x 1 wcq wcq 1.1K Mar 17 10:07 patch_dpkg_gdbserver.sh
2,2 执行patch_dpkg_gdbserver.sh
sysadm@SCT230A:~$ chmod +x patch_dpkg_gdbserver.sh
sysadm@SCT230A:~$ ./patch_dpkg_gdbserver.sh
dpkg gdbsesrver patch...
3、使用Debug方式编译源代码,并配置好运行环境
3.1 在project_root代码目录下,执行./build.sh Debug 编译
wcq@wcq-Vostro-3583:project_root$ ./build.sh Debug
BuildType Debug
-- Configuring done
-- Generating done
CMake Warning:
Manually-specified variables were not used by the project:
CMAKE_TOOLCHAIN_FILE
-- Build files have been written to: /home/wcq/Project/project_root/build/Debug
ninja: no work to do.
-- Install configuration: "Debug"
3.2 将编译生成的bin,lib 和 hal_lib打包并上传到终端的/home/sysadm目录
cd build/Debug/
tar -cvzf project.tar.gz bin hal_lib/ lib
#发送 project.tar.gz到本机
sz project.tar.gz
3.3 在终端/home/sysadm目录下接收project.tar.gz, 并设置好运行环境
sysadm@SCT230A:~$ cd /home/sysadm
sysadm@SCT230A:~$ mkdir -p project
#接收project.tar.gz 到终端目录
sysadm@SCT230A:~$ rz
sysadm@SCT230A:~$ cd project
sysadm@SCT230A:~/project$ tar -xvzf ../project.tar.gz
#更新/lib/hal_lib文件
sysadm@SCT230A:~/project$ sudo cp -rf hal_lib/* /lib/hal_lib/
3.4 确认/etc/ld.conf正确配置
确认/lib/hal_lib/local目录被正确配置。若需要修改/etc/ld.so.conf,可能需要再次重启终端。
sysadm@SCT230A:~/project$ cat /etc/ld.so.conf
include /etc/ld.so.conf.d/*.conf
/lib/hal_lib
/lib/hal_lib/local
3.5 确认nano-srvd能正常运行
若调试gui程序,需要确认nano-srvd能正常运行。
注意:这一步之前可能需要手动安装内核驱动模块,参见相关文档。
sysadm@SCT230A:~/project/bin$ sudo ./nanox-srvd
[sudo] password for sysadm:
KBD_Open, device /dev/input/event3
GdOpenKeyboard:206.
GdOpenScreen:40.
fbe_open:34.
Open /dev/fb0 OK.
4、使用gdbserver + vscode调试代码
以调试 desktop_gui_simulate 为例:
4.1 配置好vscode相关插件
推荐:C/C++ Extension Pack, 它包含c/c++开发过程中常用的插件,也集成了Remote - SSH 插件。
4.2 配置好SSH远程开发环境,确保能够访问开发服务器的代码工程。
参考:https://blog.csdn.net/fengxiaolu311/article/details/104774530/
4.3 .vscode/launch.json配置如下:
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "build/Debug/bin/desktop_gui_simulate",
"args": [
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [
{ "name" : "LD_LIBRARY_PATH",
"value": "/lib/hal_lib:/lib/hal_lib/local"
}
],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"miDebuggerPath": "/opt/ext-toolchain/bin/arm-linux-gnueabihf-gdb",
"miDebuggerServerAddress": "192.168.2.238:9999"
}
]
}
注意:
- “program”: “build/Debug/bin/desktop_gui_simulate”: 程序路径根据需要修改
- “miDebuggerServerAddress”: “192.168.2.238:9999”: ip地址和端口需要根据需要进行修改
4.4 终端启动gdbserver
sysadm@SCT230A:~/project/bin$ sudo gdbserver :9999 ./desktop_gui_simulate
Process ./desktop_gui_simulate created; pid = 6093
Listening on port 9999
4.5 vscode 快捷键 F5进入调试模式,Enjoy it!
5、参考资料
1、https://code.visualstudio.com/docs/remote/ssh
2、https://nnfw.readthedocs.io/en/stable/howto/how-to-remote-debugging-with-visual-studio-code.html
|