使用 CDT 调试器,第 2 部分: 使用 Eclipse CDT 和 MI 访问 gdb
2009-12-19 00:00:00 来源:WEB开发网示例能清楚地说明这一点。比如说您想要调试一个基于以下代码的应用程序。
清单 1. 一个简单的 C 语言应用程序:simple.c
int main() {
int x = 4;
x += 6; // x = 10
x *= 5; // x = 50
return (0);
}
用 gcc -g -O0 simple.c -o simple 编译代码后,一个常规的调试会话如清单 2 所示。
清单 2. 调试会话
$ gdb -q simple
(gdb) break main
(gdb) run
1 int main() {
(gdb) step
2 int x = 4;
(gdb) step
3 x += 6; // x = 10
(gdb) print x
$1 = 4
(gdb) step
4 x *= 5; // x = 50
(gdb) print x
$2 = 10
(gdb) quit
清单 3 显示使用 MI 命令的同一个 gdb 会话(以粗体显示)。
清单 3. 使用 MI 的调试会话
$ gdb -q -i mi simple
(gdb)
-break-insert-main
^done,bkpt={number="1",type="breakpoint",disp="keep",enabled="y",addr="0x00401075",
func="main",file="simple.c",fullname="/home/mscarpino/simple.c",line="1",times="0"}
(gdb)
-exec-run
^running
(gdb)
*stopped,reason="breakpoint-hit",bkptno="1",thread-id="1",frame={addr="0x00401075",
func="main",args=[],file="simple.c",fullname="/home/mscarpino/simple.c",line="1"}
(gdb)
-exec-step
^running
(gdb)
*stopped,reason="end-stepping-range",thread-id="1",frame={addr="0x0040107a",
func="main",args=[],file="simple.c",fullname="/home/mscarpino/simple.c",line="2"}
(gdb)
-exec-step
^running
(gdb)
*stopped,reason="end-stepping-range",thread-id="1",frame={addr="0x00401081",
func="main",args=[],file="simple.c",fullname="/home/mscarpino/simple.c",line="3"}
(gdb)
-var-create x_name * x
^done,name="x_name",numchild="0",type="int"
(gdb)
-var-evaluate-expression x_name
^done,value="4"
(gdb)
-exec-step
^running
(gdb)
*stopped,reason="end-stepping-range",thread-id="1",frame={addr="0x00401081",
func="main",args=[],file="simple.c",fullname="/home/mscarpino/simple.c",line="4"}
(gdb)
-var-update x_name
^done,changelist=[{name="x_name",in_scope="true",type_changed="false"}]
(gdb)
-var-evaluate-expression x_name
^done,value="10"
(gdb)
-var-delete x_name
^done,ndeleted="1"
(gdb)
-gdb-exit
更多精彩
赞助商链接