日期:2014-05-16 浏览次数:20962 次
#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
$ 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)