我正在尝试用gdb创建一个小单元测试,用于由OpenOCD控制的嵌入式mcu (通过gdb服务器让我控制目标)。
因此,我想用一些gdb脚本来自动化这一点。
我想为gdb编写某种脚本,或多或少地这样做:
有什么想法吗?
关于如何在python脚本中这样做的示例将是一个不错的例子。
谢谢约翰
注意:
假设我们有这个基本结构,多少取决于函数test_failed()或test_success()返回的是什么。
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用手做这件事是非常艰难的,
(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启动脚本中,这个脚本大致如下所示。
break test_success
break test_failed
target remote localhost:3333
cont
frame
开始的时候
arm-none-eabi-gdb --batch --command=commands.gdb main.elf
这样的作品,但不是很好。如何使用gdb似乎支持的“新的和很酷的”python脚本来实现这一点。
发布于 2010-11-10 00:25:34
FYI最近的gdb版本在Python中是可脚本的。您可以从gdb命令行调用python代码。这打开了一个全新的世界,检查相关的文档。从命令行运行:
dnf/yum/apt-get install gdb-doc
info gdb extending python
如果您不喜欢基于文本的信息浏览器,这里有一个(许多?)另一种图形浏览器:
yelp 'info:gdb' # , go to "Extending"
下面是一个示例脚本。它将gdb附加到第一个正在运行的"your_program“。
#!/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))
发布于 2014-08-15 09:13:00
我正在使用的一个简化示例是:
class DebugPrintingBreakpoint(gdb.Breakpoint):
debugging_IDs = frozenset({37, 153, 420})
def stop(self):
top = gdb.newest_frame()
someVector = top.read_var('aVectorVar')
# Access the begin() & end() pointer of std::vector in GNU Standard C++ lib
first = someVector['_M_impl']['_M_start']
last = someVector['_M_impl']['_M_finish']
values = []
while first != last:
values.append(int(first.dereference()['intID']))
first = first + 1
if not set(values) & debugging_IDs:
return False # skip: none of the items we're looking for can be found by ID in the vector on the stack
print("Found other accompanying IDs: {}".format(values))
return True # drop to gdb's prompt
# Ensure shared libraries are loaded already
gdb.execute("start")
# Set our breakpoint, which happens to reside in some shared lib, hence the "start" previously
DebugPrintingBreakpoint("source.cpp:42")
gdb.execute("continue")
您可以从gdb的提示符执行此脚本,如下所示:
(gdb) source script.py
或者从命令行:
$ gdb --command script.py ./executable.elf
有关更多信息,请参见完整的GDB Python文档。
发布于 2010-10-31 08:27:49
好吧,我在问问题的时候找到了答案.这是一件非常简单的事情。
如果您希望它们以特定的顺序执行,则不应该同时使用“-命令”和"--eval“!
一种更可预测的方法是将所有内容都放在commands.gdb文件中,然后忽略--eval。
所以它变成了这样:
arm-none-eabi-gdb --batch --command=commands.gdb main.elf
其中commands.gdb看起来是这样的:
break test_success
break test_failed
target remote localhost:3333
cont
frame
但是,用像python这样的东西来做这件事可能会好得多。
https://stackoverflow.com/questions/4060565
复制相似问题