使用expect调用gdb实现自动调试程序

使用gdb调试程序时可以设置断点停在某一行、某个函数或者某个条件满足时停下。但是有时想匹配到一个任意的字符串时让程序停止。这个功能gdb中无法实现,但是借助expect可以实现自动化调试,设置好断点,然后让expect来捕获模式串,一旦获取到,即停止expect,控制权返回到gdb。

 1#!/usr/bin/expect
 2#####################################################################
 3#  File:   Automatically debug program by gdb
 4#  Usage:  Specify the expected string
 5#
 6#  Author: houcy
 7#  Date:   2010-01-29
 8#  Plan:   Wrap a GUI window to make it user-friendly
 9#
10#  Copyrights Reserved
11#####################################################################
12
13if {$argc < 3} {
14    send_user "Usage: $argv0 spectre netlist pattern1 pattern2 \r\n"
15    exit
16}
17
18set timeout 120
19set done 1
20set num 0
21
22# get command line param
23set spectre [lindex $argv 0]
24set netlist [lindex $argv 1]
25
26# set pattern array
27for {set i 2} { $i<$argc } {incr i} {
28    set pattern($i) [lindex $argv $i]
29    puts $pattern($i)
30}
31
32# start gdb process
33spawn gdb $spectre
34
35# for expect debug
36exp_internal 0
37
38# set some breakpoints
39expect "(gdb)" { send "b tranDoAnalysis\r" }
40
41# run program
42expect "(gdb)" { send "run $netlist\r" }
43
44# continue to run
45expect "(gdb)" { send "b aiEvaluateEHForSoa\r" } 
46expect "(gdb)" { send "c\r" } 
47expect "(gdb)" { send "b assertExpressionResult::evaluateExprs\r" } 
48expect "(gdb)" { send "c\r" }
49
50# watch target string, or else continue
51while ($done) {
52        for { set j 2 } { $j<$argc } {incr j} {
53            expect "$pattern($j)" { send_user "Got $pattern($j) here! num=$num\r\n"; set done 0 ; break}
54        }
55        if { $done == 0 } { break;}
56        expect {
57            "(gdb)" { send "c\r" ; set num [expr $num+1] ; send_user "num=$num\r\n" }
58            eof { send_user "It's the end! Bye!\r\n" }
59            timeout { send_user "Timeout, please check the reason!\r\n" }
60        }
61}
62
63# back to gdb control
64interact
65

运行程序:
1bj2amdlnx20> ./gdb.exp spectre netlist sweepValue=1.e-10

expect捕获到模式串,返回到gdb中:
1(gdb) b assertExpressionResult::evaluateExprs
2Breakpoint 3 at 0x8b57fe0: file assertInterface.cc, line 2174.
3(gdb) c
4Continuing.
5
6Breakpoint 3, assertExpressionResult::evaluateExprs (sweepValue=1e-10, checklimitSeverity=0, violationDest=4 '\004', 
7Got sweepValue=1e-10 here! num=0
8(gdb)

posted on 2010-02-02 09:46 挑灯看剑 阅读(1596) 评论(0)  编辑 收藏 引用 所属分类: 工作随笔

只有注册用户登录后才能发表评论。
<2010年2月>
31123456
78910111213
14151617181920
21222324252627
28123456
78910111213

导航

公告

【自我介绍】 08年南开大学硕士毕业 最近关注:算法、Linux、c++、高并发 爱好:滑旱冰、打乒乓球、台球、保龄球

常用链接

随笔分类(139)

文章分类

我常去的网站

技术博客(都是大牛)

技术站点

搜索

积分与排名