日期:2014-05-16  浏览次数:20881 次

求教有关gdb中的step命令的问题
gdb中,如果某一行代码有多个函数调用,例如:

child = GetDecoder( )->Translate( req->address.GetPhysicalAddress( ) )

使用step命令,默认可能是会进入GetDecoder( )函数,如果我想进入Tanslate()函数的话,应该怎么办呢?

谢谢!
------解决方案--------------------
finsh
你说的应该就是跳出这个函数吧.看下面简单例子

代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct sTa{
int (*bar)(int);
};
struct sTa s1;

int bar(int a){
a+=1;
a-=1;
return a+1;
}
struct sTa* foo(void)
{
int b=0;
b++;
s1.bar=bar;
return &s1;
}
int main()
{
int ret=foo()->bar(42);
printf("ret=%d",ret);
return 0;
}


编译
$ gcc 1.c -g -Wall -O0


调试,先进了foo(),finish出来就调用了bar().
然后就可以自己step咯.
$ gdb a.out 
GNU gdb (GDB) 7.6.2 (Debian 7.6.2-1)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/john/tmp/a.out...done.
(gdb) b main
Breakpoint 1 at 0x804843b: file 1.c, line 24.
(gdb) list
15 struct sTa* foo(void)
16 {
17 int b=0;
18 b++;
19 s1.bar=bar;
20 return &s1;
21 }
22 int main()
23 {
24 int ret=foo()->bar(42);
(gdb) r
Starting program: /home/john/tmp/a.out 
warning: Could not load shared library symbols for linux-gate.so.1.
Do you need "set solib-search-path" or "set sysroot"?

Breakpoint 1, main () at 1.c:24
24 int ret=foo()->bar(42);
(gdb) step
foo () at 1.c:17 //进了foo
17 int b=0;
(gdb) finish  //finish出来
Run till exit from #0  foo () at 1.c:17
0x08048440 in main () at 1.c:24
24 int ret=foo()->bar(42);
Value returned is $1 = (struct sTa *) 0x8049750 <s1>
(gdb) step
bar (a=42) at 1.c:11 //进了你希望调试的foo函数
11 a+=1;
(gdb) step //自己玩咯
12 a-=1;
(gdb) 


引用

引用:
gdb中,如果某一行代码有多个函数调用,例如:

child = GetDecoder( )->Translate( req->address.GetPhysicalAddress( ) )

使用step命令,默认可能是会进入GetDecoder( )函数,如果我想进入Tanslate()函数的话,应该怎么办呢?

谢谢!