gdb调试
 1 #include <stdio.h>
 2 //声明得在main之前 或者main里
 3 void change(int a ,int b){
 4     int tmp=a;
 5     a=b;
 6     b=tmp;
 7 }
 8 int main(){
 9     int a=5;
10     int b=3;
11     change(a,b);
12     printf("a=%d\nb=%d\n",a,b );
13 }

编译成gdb可调试的程序
-g 且不可删除源代码
gcc -g point.c -o point

l或者list列出源代码
默认列出部分的源代码
按list或者回车继续列出剩余代码
start
p a (输出a ,此时a=0)
n 继续执行到下一行
p a (a=5)
s (执行到函数时进去到函数)
bt 当代码在函数里面时打印堆栈信息
f 栈编号 切换到相应函数里
q 退出gdb

[root@centos1 c]# gdb ./point
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-83.el6)
Copyright (C) 2010 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 "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /mnt/hgfs/www/c/point...done.
(gdb) list
1    #include <stdio.h>
2    //声明得在main之前 或者main里
3    void change(int a ,int b){
4        int tmp=a;
5        a=b;
6        b=tmp;
7    }
8    int main(){
9        int a=5;
10        int b=3;
(gdb) 
11        change(a,b);
12        printf("a=%d\nb=%d\n",a,b );
13    }
14    
(gdb) 
Line number 15 out of range; point.c has 14 lines.
gdb) start
Temporary breakpoint 1 at 0x4004ea: file point.c, line 9.
Starting program: /mnt/hgfs/www/c/point 

Temporary breakpoint 1, main () at point.c:9
9        int a=5;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.166.el6.x86_64
(gdb) p a
$1 = 0
(gdb) n
10        int b=3;
(gdb) p a
$2 = 5
(gdb) n
11        change(a,b);
(gdb) p b
$3 = 3
(gdb) s
change (a=5, b=3) at point.c:4
4        int tmp=a;
(gdb) bt
#0  change (a=5, b=3) at point.c:4
#1  0x0000000000400507 in main () at point.c:11
(gdb) p a
$4 = 5
(gdb) p b
$5 = 3
(gdb) n
5        a=b;
(gdb) p a
$6 = 5
(gdb) p b
$7 = 3
(gdb) s 1
6        b=tmp;
(gdb) p b
$8 = 3
(gdb) s 0
(gdb) p b
$9 = 3
(gdb) n
7    }
(gdb) p b
$10 = 5
(gdb) s 1
main () at point.c:12
12        printf("a=%d\nb=%d\n",a,b );
(gdb) p b
$11 = 3
(gdb) bt
#0  main () at point.c:12
(gdb) bt
#0  main () at point.c:12
(gdb) n
a=5
b=3
13    }
(gdb) n
0x000000378401ed5d in __libc_start_main () from /lib64/libc.so.6
(gdb) 

 

 1 #include <stdio.h>
 2 //声明得在main之前 或者main里
 3 void change(int *a ,int *b){
 4     int tmp=*a;
 5     *a=*b;
 6     *b=tmp;
 7 }
 8 int main(){
 9     int a=5;
10     int b=3;
11     change(&a,&b);
12     printf("a=%d\nb=%d\n",a,b );
13 }
简单gdb调试_c简单gdb调试_IT_02
root@centos1 c]# gdb ./point1
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-83.el6)
Copyright (C) 2010 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 "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /mnt/hgfs/www/c/point1...done.
(gdb) list
1    #include <stdio.h>
2    //声明得在main之前 或者main里
3    void change(int *a ,int *b){
4        int tmp=*a;
5        *a=*b;
6        *b=tmp;
7    }
8    int main(){
9        int a=5;
10        int b=3;
(gdb) 
11        change(&a,&b);
12        printf("a=%d\nb=%d\n",a,b );
13    }
14    
(gdb) start
Temporary breakpoint 1 at 0x4004f8: file point1.c, line 9.
Starting program: /mnt/hgfs/www/c/point1 

Temporary breakpoint 1, main () at point1.c:9
9        int a=5;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.166.el6.x86_64
(gdb) n
10        int b=3;
(gdb) n
11        change(&a,&b);
(gdb) s
change (a=0x7fffffffe47c, b=0x7fffffffe478) at point1.c:4
4        int tmp=*a;
(gdb) p a
$1 = (int *) 0x7fffffffe47c
(gdb) p b
$2 = (int *) 0x7fffffffe478
(gdb) bt
#0  change (a=0x7fffffffe47c, b=0x7fffffffe478) at point1.c:4
#1  0x0000000000400519 in main () at point1.c:11
(gdb) p *a
$3 = 5
(gdb) p *b
$4 = 3
(gdb) p tmp
$5 = 0
(gdb) p *tmp
Cannot access memory at address 0x0
(gdb) n
5        *a=*b;
(gdb) p *a
$6 = 5
(gdb) n
6        *b=tmp;
(gdb) n
7    }
(gdb) p a
$7 = (int *) 0x7fffffffe47c
(gdb) p b
$8 = (int *) 0x7fffffffe478
(gdb) p *a
$9 = 3
(gdb) p *b
$10 = 5
(gdb) n
main () at point1.c:12
12        printf("a=%d\nb=%d\n",a,b );
(gdb) n
a=3
b=5
13    }
(gdb) 
View Code