如何脚本GDB(与Python)?添加断点示例,运行,我们碰到了什么断点? [英] How to script gdb (with python)? Example add breakpoints, run, what breakpoint did we hit?

查看:210
本文介绍了如何脚本GDB(与Python)?添加断点示例,运行,我们碰到了什么断点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用gdb创建一个单元测试,
用于由 OpenOCD (这使我可以通过gdb服务器控制我的目标)。

所以我想用gdb的一些脚本来自动执行此操作。 / p>

我想为gdb编写某种脚本,或多或少地这样做:


  1. 添加几个断点

  2. 启动程序

  3. 当我们停下时,它停在哪里(获取框架信息) li>
  4. 退出。

任何想法?



如何在python gdb脚本中执行此操作的示例很好。



感谢
Johan






注意

假设我们有这个基本结构,
根据函数start_test()返回的内容,或多或少地进入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);

$ / code>

在gdb中手动执行此操作非常困难,

 (gdb)break test_success 
断点1在0x20:文件src / main.c,第9行。
(gdb) break test_failed
在0x18的断点2:file src / main.c,第5行。
(gdb)cont
继续。

断点1,test_success()在src / main.c:9
9 while(1);
(gdb)frame
#0 src / main.c中的test_success():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 

这类作品,但不是很好。
我怎么用新的和酷的python脚本来做到这一点,gdb似乎支持

解决方案


  info gdb扩展python 

如果您不喜欢基于文本的信息浏览器,那么可以使用以下图形浏览器之一:

  yelp'info:gdb?扩展GDB'

这里是一个示例gdb-python脚本。它将gdb附加到发现的第一个your_program中。

 #!/ usr / bin / python 

导入子流程
导入字符串

def反引号(cmdwords):
输出= subprocess.Popen(cmdwords,stdout = subprocess.PIPE).communicate()[0]
return output.strip()

pid = backquotes(['pgrep','your_program'])

gdb.execute(attach+ str(pid ))


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).

So I would like to automate this with some scripting of gdb.

I would like to write some kind of script for gdb that more or less does this:

  1. Add a couple of breakpoints
  2. Start the program
  3. When we stop, where did it stop (get the frame info)
  4. Quit.

Any ideas?

A example on how to do this in python gdb scripting would be nice.

Thanks Johan


Note:

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);    
}

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) 

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

and start it with

arm-none-eabi-gdb --batch --command=commands.gdb main.elf

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.

解决方案

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:

 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?Extending GDB'

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)?添加断点示例,运行,我们碰到了什么断点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆