问题描述
我正在尝试使用 gdb 创建一个小单元测试,对于由 OpenOCD 控制的嵌入式 mcu(这让我可以通过 gdb 服务器控制我的目标).
I'm trying to create a little unit test with gdb,for a embedded mcu that is controlled by OpenOCD (that gives me control over my target via a gdb server).
所以我想用 gdb 的一些脚本来自动化这个.
So I would like to automate this with some scripting of gdb.
我想为 gdb 编写某种脚本,或多或少会这样做:
I would like to write some kind of script for gdb that more or less does this:
- 添加几个断点
- 启动程序
- 当我们停止时,它在哪里停止(获取帧信息)
- 退出.
有什么想法吗?
关于如何在 python gdb 脚本中执行此操作的示例会很好.
A example on how to do this in python gdb scripting would be nice.
谢谢约翰
注意:
假设我们有这个基本结构,或多或少进入 test_failed() 或 test_success()取决于函数 start_test() 返回的内容.
Let's say that we have this basic structure,that more or less goes into test_failed() or test_success()depending on what the function start_test() returns.
void test_failed() {
while(1);
}
void test_success() {
while(1);
}
int main(void) {
int status = start_test();
if( status > 0 ) {
test_failed();
}
test_success();
while(1);
}
在 gdb 中手动执行此操作非常困难,
To do this manually in gdb is very strait forward,
(gdb) break test_success
Breakpoint 1 at 0x20: file src/main.c, line 9.
(gdb) break test_failed
Breakpoint 2 at 0x18: file src/main.c, line 5.
(gdb) cont
Continuing.
Breakpoint 1, test_success () at src/main.c:9
9 while(1);
(gdb) frame
#0 test_success () at src/main.c:9
9 while(1);
(gdb)
所以我尝试的下一步是将这些 gdb 命令添加到 gdb 启动脚本中,该脚本或多或少看起来像这样.
So the next step I tried was to add those gdb commands into a gdb startup script that more or less just looked like this.
break test_success
break test_failed
target remote localhost:3333
cont
frame
并以
arm-none-eabi-gdb --batch --command=commands.gdb main.elf
而且这种作品,但不是很好.我如何使用新的和酷的"python 脚本来做到这一点,gdb 似乎支持.
And this kind of works, but it is not very nice.How do I do this with the "new and cool" python scripts,that gdb seem to support.
推荐答案
仅供参考,最近的 gdb 版本可在 Python 中编写脚本.您可以从 gdb 命令行调用 python 代码.这打开了一个全新的世界,查看相关文档.从命令行运行:
FYI recent gdb versions are scriptable in Python. You can call python code from the gdb command line. This opens a whole new world, check the relevant documentation. From the command line run:
dnf/yum/apt-get install gdb-doc
info gdb extending python
如果您不喜欢基于文本的信息浏览器,这里有一个(其中之一?)替代的图形浏览器:
If you do not like the text-based info browser, here is one (among many?) alternative, graphical browser:
yelp 'info:gdb' # , go to "Extending"
这是一个示例 gdb-python 脚本.它将 gdb 附加到发现运行的第一个your_program".
Here is a sample gdb-python script. It attaches gdb to the first "your_program" found running.
#!/usr/bin/python
import subprocess
import string
def backquotes(cmdwords):
output = subprocess.Popen(cmdwords, stdout=subprocess.PIPE).communicate()[0]
return output.strip()
pid = backquotes(['pgrep', 'your_program'])
gdb.execute("attach " + str(pid))
这篇关于如何编写 gdb 脚本(使用 python)?例子添加断点,运行,我们打到了什么断点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!