我使用一个gdb文件运行.gdbinit文件,该文件具有一些无法展开的方便变量。
1.我的设置
我编写了以下.gdbinit文件,通过黑魔法探针将可执行文件闪存到微控制器(请参阅https://github.com/blacksphere/blackmagic/wiki):
# .gdbinit file:
# ------------------------------------------- #
# GDB commands #
# FOR STM32F767ZI #
# ------------------------------------------- #
target extended-remote $com
monitor version
monitor swdp_scan
attach 1
file mcu_application.elf
load
start
detach
quit黑魔法探测器附着在COM端口上,在一台计算机上和另一台计算机上可以不同。因此,我不想硬编码在.gdbinit文件中。GDB方便变量看起来是最优雅的解决方案:
因此,我在$com文件中使用了方便变量.gdbinit,并在调用GDB时在命令行中定义了它:
arm-none-eabi-gdb -x .gdbinit -ex "set $com = \"COM9\""2.错误
GDB启动但抛出错误消息:
.gdbinit:6: Error in sourced command file:
$com: No such file or directory.看起来GDB不识别$com方便变量。因此,我检查GDB是否实际存储了变量:
(gdb) show convenience
$com = "COM9"
$trace_file = void
$trace_func = void
$trace_line = -1
$tracepoint = -1
$trace_frame = -1
$_inferior = 1
...这证明GDB正确地将其存储为"COM9"。因此,问题在于未能将其扩大。
3.更多的审判
当我观察到在执行$com时未能展开.gdbinit时,我认为可以在GDB中直接发出命令:
(gdb) set $com = "COM9"
(gdb) show convenience
$com = "COM9"
$trace_file = void
$trace_func = void
...
(gdb) target extended-remote $com
$com: No such file or directory.但错误依然存在。
4.问题
您知道如何使GDB中的方便变量正常工作吗?或者你知道有另一种机制来达到同样的目标吗?
5.解决办法
谢谢你的回答,马克·普洛特尼克!正如您所建议的,我给我的.gdbinit文件提供了以下内容:
define flash-remote
target extended-remote $arg0
monitor version
monitor swdp_scan
attach 1
file mcu_application.elf
load
start
detach
quit
end但是,在调用GDB时,我不得不删除参数COM9的引号。因此,与其:
arm-none-eabi-gdb -x .gdbinit -ex "flash-remote \"COM9\""我以这种方式调用GDB:
arm-none-eabi-gdb -x .gdbinit -ex "flash-remote COM9"现在起作用了!你救了我的命!
发布于 2020-07-06 19:15:53
方便变量只在特定的上下文(主要是表达式)中展开,例如print、x、eval、set和if的参数。
您可以使用eval来做您想做的事情:
eval "target extended-remote %s", $com但是,直到最近,gdb在计算表达式时都会将字符串值存储在目标的地址空间中,这需要一个正在运行的进程。因此,在旧的gdb中,您可能会得到错误消息,这个表达式的值要求目标程序是活动的。
Gdb确实有一个更通用的宏工具:用户定义的命令。
一种可能是将其放在.gdbinit中:
define flash-remote
target extended-remote $arg0
monitor version
monitor swdp_scan
attach 1
file mcu_application.elf
load
start
detach
quit
end然后像这样调用gdb:
arm-none-eabi-gdb -ex "flash-remote \"COM9\""发布于 2020-07-05 16:10:37
GDB 手册清楚地记录了在任何-ex命令之前对.gdbinit进行评估。
您可以编写一个简单的外壳包装器,它创建具有适当替换的临时/tmp/.gdbinit.$unique_suffix,调用gdb -x /tmp/.gdbinit....,并在GDB退出后删除临时文件。
发布于 2020-07-05 20:35:33
在Windows上选择COM端口的格式是"//./COM9",所以在GDB中的测试应该使用:
$com = COM9
target extended-remote //.$com我还没有对此进行测试,但我希望它能奏效。
https://stackoverflow.com/questions/62733921
复制相似问题