一,前言
我在uboot支持的cortex-M4内核启动流程分析--Apple的学习笔记中就说过了,我计划要单步调试uboot,但是我只有stlink,所以要基于openocd的gdb来调试,所以就做了尝试,花费约2天时间,虽然做了些无用功,专门还装了ubuntu18.04,且基于ubuntu还安装了openocd这些其实都无用的,但是就是这些过程, 让我了解了openocd这些命令的使用方法,最终我才能在win10上安装openocde,然后通过win10的vscode通过ssh连接ubuntu进行gdb单步调试环境搭建成功。
二,win10 openocd搭配ubuntu中的vscode调试
1,stlink连接开发板,且插入stlink。
2,打开一个cmd输入命令,然后可以看到正常识别到stlink,且等待gdb的3333端口。
openocd -f D:\program\OpenOCD-20231002-0.12.0\share\openocd\scripts\interface\stlink-v2.cfg -f D:\program\OpenOCD-20231002-0.12.0\share\openocd\scripts\target\stm32f4x.cfg -c "bindto 0.0.0.0"
3,用ubuntu本地的vscode来单步调试,作为gdb的客户端。launch.json设置如下
{
"version":"0.2.0",
"configurations":[
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "/work/STM32/LED.elf",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "/work/tools/gcc-arm-none-eabi-9-2019-q4-major/bin/arm-none-eabi-gdb",
"miDebuggerServerAddress":"192.168.112.10:3333",
/*"preLaunchTask":"build",*/
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
通过本地vscode用-exec为前缀的gdb命令都是可行的。 注意,连接openocd后,先要暂停,否则报错如下
-exec load u-boot
Unable to perform this action because the process is running.
正常的调试信息如下
-exec load /work/STM32/LED.elf
Loading section .isr_vector, size 0x188 lma 0x8000000
1045+download,{section=".isr_vector",section-size="392",total-size="55434"}
1045+download,{section=".isr_vector",section-sent="392",section-size="392",total-sent="392",total-size="55434"}
Loading section .text, size 0xf1c lma 0x8000188
1045+download,{section=".text",section-size="3868",total-size="55434"}
Loading section .rodata, size 0x10 lma 0x80010a4
1045+download,{section=".rodata",section-size="16",total-size="55434"}
Loading section .ARM, size 0x8 lma 0x80010b4
1045+download,{section=".ARM",section-size="8",total-size="55434"}
Loading section .init_array, size 0x4 lma 0x80010bc
1045+download,{section=".init_array",section-size="4",total-size="55434"}
Loading section .fini_array, size 0x4 lma 0x80010c0
1045+download,{section=".fini_array",section-size="4",total-size="55434"}
Loading section .data, size 0x54 lma 0x80010c4
1045+download,{section=".data",section-size="84",total-size="55434"}
Loading section .mysec, size 0x4 lma 0x8001118
1045+download,{section=".mysec",section-size="4",total-size="55434"}
Start address 0x8000fc4, load size 4380
Transfer rate: 6 KB/sec, 547 bytes/write.
-exec b main
Note: breakpoints 1 and 2 also set at pc 0x80005e6.
Breakpoint 3 at 0x80005e6: file Src/Core/Src/main.c, line 77.
c
Cannot evaluate expression on the specified stack frame.
Breakpoint 1, main () at Src/Core/Src/main.c:77
halted: PC: 0x08000f44
Program received signal SIGINT, Interrupt.
0x08000f92 in HAL_GetTick () at Src/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c:325
Kill the program being debugged? (y or n) [answered Y; input not from terminal]
[Inferior 1 (Remote target) killed]
The program '/work/STM32/LED.elf' has exited with code 0 (0x00000000).
三,win10 openocd搭配ssh连接的vscode远程调试
只是不清楚用ssh连接的远程vscode为什么不行,理论上本地vsocode可行,远程vscode也可行,我有了如上使用经验,也就是说了解了各种问题解决方案,于是又进行了尝试。
1,win上的操作和之前一样,且先将vscode和ubuntu14.04连接上。 a,stlink连接开发板,且插入stlink b xxx,打开一个cmd输入命令,然后可以看到正常识别到stlink,且等待gdb的3333端口。
openocd -f D:\program\OpenOCD-20231002-0.12.0\share\openocd\scripts\interface\stlink-v2.cfg -f D:\program\OpenOCD-20231002-0.12.0\share\openocd\scripts\target\stm32f4x.cfg -c "bindto 0.0.0.0"
2,vscode中launch脚本为
{
"version":"0.2.0",
"configurations":[
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "/work/v9/u-boot-2023.10/stm/u-boot",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "/work/tools/gcc-arm-none-eabi-9-2019-q4-major/bin/arm-none-eabi-gdb",
"miDebuggerServerAddress":"192.168.112.10:3333",
/*"preLaunchTask":"build",*/
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
然后vscode中按F5,就连接上了远程openocd的3333,然后要先按暂停,之后laod 全路径才能成功下载u-boot,再reset和halt停止到入口,就可以单步或全速运行,通过界面方式打断点,也可以用-exec b来打断点调试。
-exec load /work/v9/u-boot-2023.10/stm/u-boot
-exec monitor reset halt
四,小结
我的uboot单步调试目标终于完成了。因为之前qemu无法调试am335x,所以看到stm32F4居然也有uboot,而我是有此芯片的开发板加stlink-v2调试器的,只是应该需要修改设备树的,否则板子不同。所以我就想到用openocd作为调试器的gdbserver端口,然后vscode通过gdbclient来进行单步调试uboot核心流程,作为进一步深入理解的闭环验证工具链。调试环境搭建完工,不用再printf了,调试速度立马升级,分析代码的难度立马降级,哈哈~
标签:openocd,uboot,Apple,vscode,section,stlink,gdb,size From: https://blog.51cto.com/AppleCai/8080218