Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2307878
  • 博文数量: 395
  • 博客积分: 10994
  • 博客等级: 上将
  • 技术积分: 5586
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-17 19:49
文章存档

2014年(1)

2013年(10)

2012年(74)

2011年(303)

2010年(7)

分类: LINUX

2011-07-25 18:29:38

下面介绍一下gdb的常用命令:

list(可以略写位l

run

set args

show args (设置命令行参数)

print

whatis(ptye)

break

continue

info break

delect breakpoint

disable breakpoint

enable breakpoint

next/step:(next使程序但不运行,代码中遇到一个函数想要进入该函数的话,要使用step,使用

finish退出函数回到调用该函数处)

quit

 

演示:

[root@localhost p3.4]# vi p3.4.c

#include

int main(){

        char input;

        int i;

        for(i=0;i<2;i++){

                printf("please input a charactor\n");

                scanf("%c",&input);

 

                switch(input){

                        case 'a':

                        printf("You input the charactor 'a'\n");

                        case 'b':

                        printf("You input the charactor 'b'\n");

                        case 'c':

                        printf("You input the charactor 'c'\n");

                        case 'd':

                        printf("You input the charactor 'd'\n");

                        default:

                        printf("What you input is not belong to the judgement of the

program\n");

                }

        }

        return 0;

}

~

[root@localhost p3.4]# ls

p3.4  p3.4.c

[root@localhost p3.4]# ./p3.4

please input a charactor

af

You input the charactor 'a'

You input the charactor 'b'

You input the charactor 'c'

You input the charactor 'd'

What you input is not belong to the judgement of the program

please input a charactor

What you input is not belong to the judgement of the program

可以看到上面得到显示前面不正常,而f显示正确

1.使用gdb来调试

[root@localhost p3.4]# gcc -o p3.4.debug -g p3.4.c

[root@localhost p3.4]# ls

p3.4  p3.4.c  p3.4.debug

[root@localhost p3.4]# gdb p3.4.debug

GNU gdb (GDB) Fedora (7.0-3.fc12)

Copyright (C) 2009 Free Software Foundation, Inc.

License GPLv3+: GNU GPL version 3 or later <

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 "i686-redhat-linux-gnu".

For bug reporting instructions, please see:

<

Reading symbols from

/home/linux_book/bianchengjishuxiangjie/p3/p3.8/p3.4/p3.4.debug...done.

(gdb) run

Starting program: /home/linux_book/bianchengjishuxiangjie/p3/p3.8/p3.4/p3.4.debug

warning: .dynamic section for "/lib/libc.so.6" is not at the expected address

warning: difference appears to be caused by prelink, adjusting expectations

please input a charactor

af

You input the charactor 'a'

You input the charactor 'b'

You input the charactor 'c'

You input the charactor 'd'

What you input is not belong to the judgement of the program

please input a charactor

What you input is not belong to the judgement of the program

Program exited normally.

Missing separate debuginfos, use: debuginfo-install glibc-2.11-2.i686

(gdb) file p3.4

p3.4        p3.4.c      p3.4.debug 

(gdb) file p3.4

(gdb) file /home/

chuangming/ linux_book/ nfsshare/   smb/        zhang/     

feifei/     lost+found/ ruanjian/   tonnyy/    

(gdb) list

1       #include

2

3       int main(){

4               char input;

5               int i;

6               for(i=0;i<2;i++){

7                       printf("please input a charactor\n");

8                       scanf("%c",&input);

9

10

(gdb) list

11                      switch(input){

12                              case 'a':

13                              printf("You input the charactor 'a'\n");

14                              case 'b':

15                              printf("You input the charactor 'b'\n");

16                              case 'c':

17                              printf("You input the charactor 'c'\n");

18                              case 'd':

19                              printf("You input the charactor 'd'\n");

20                              default:

(gdb) list

21                              printf("What you input is not belong to the judgement

of the program\n");

 

(gdb) break 7

Breakpoint 1 at 0x804842a: file p3.4.c, line 7.

(gdb) break 12

Breakpoint 2 at 0x8048470: file p3.4.c, line 12.

(gdb) run

Starting program: /home/linux_book/bianchengjishuxiangjie/p3/p3.8/p3.4/p3.4.debug

 

Breakpoint 1, main () at p3.4.c:7

7                       printf("please input a charactor\n");

Missing separate debuginfos, use: debuginfo-install glibc-2.11-2.i686

 

gdb) run

Starting program: /home/linux_book/bianchengjishuxiangjie/p3/p3.8/p3.4/p3.4.debug

 

Breakpoint 1, main () at p3.4.c:7

7                       printf("please input a charactor\n");

Missing separate debuginfos, use: debuginfo-install glibc-2.11-2.i686

(gdb) watch input

Hardware watchpoint 3: input

(gdb) next

please input a charactor

8                       scanf("%c",&input);

(gdb) next

af

Hardware watchpoint 3: input

 

Old value = 8 '\b'

New value = 97 'a'

0x0032e366 in _IO_vfscanf_internal () from /lib/libc.so.6

(gdb) next

Single stepping until exit from function _IO_vfscanf_internal,

which has no line number information.

0x00339589 in __isoc99_scanf () from /lib/libc.so.6

(gdb) next

Single stepping until exit from function __isoc99_scanf,

which has no line number information.

main () at p3.4.c:11

11                      switch(input){

(gdb) next

 

Breakpoint 2, main () at p3.4.c:13

13                              printf("You input the charactor 'a'\n");

(gdb) next

You input the charactor 'a'

15                              printf("You input the charactor 'b'\n");

(gdb) next

You input the charactor 'b'

17                              printf("You input the charactor 'c'\n");

(gdb) n

next             nexti            ni               nosharedlibrary 

(gdb) next

You input the charactor 'c'

19                              printf("You input the charactor 'd'\n");

(gdb) next

You input the charactor 'd'

21                              printf("What you input is not belong to the judgement

of the program\n");

(gdb) next

What you input is not belong to the judgement of the program

6               for(i=0;i<2;i++){

(gdb) n

 

Breakpoint 1, main () at p3.4.c:7

 

 

....

....

下面练习break delet break,enable break

 

(gdb) break 7

Breakpoint 1 at 0x804842a: file p3.4.c, line 7.

(gdb) break 12

Breakpoint 2 at 0x8048470: file p3.4.c, line 12.

(gdb) info break

Num     Type           Disp Enb Address    What

1       breakpoint     keep y   0x0804842a in main at p3.4.c:7

2       breakpoint     keep y   0x08048470 in main at p3.4.c:12

(gdb) delete  breakpoints 1

(gdb) info break

Num     Type           Disp Enb Address    What

2       breakpoint     keep y   0x08048470 in main at p3.4.c:12

(gdb) break 7

Breakpoint 3 at 0x804842a: file p3.4.c, line 7.

(gdb) info break

Num     Type           Disp Enb Address    What

2       breakpoint     keep y   0x08048470 in main at p3.4.c:12

3       breakpoint     keep y   0x0804842a in main at p3.4.c:7

(gdb) disable breakpoint 2

(gdb) info break

Num     Type           Disp Enb Address    What

2       breakpoint     keep n   0x08048470 in main at p3.4.c:12

3       breakpoint     keep y   0x0804842a in main at p3.4.c:7

(gdb) enable breakpoint 2

(gdb) info break

Num     Type           Disp Enb Address    What

2       breakpoint     keep y   0x08048470 in main at p3.4.c:12

3       breakpoint     keep y   0x0804842a in main at p3.4.c:7

 

 

下面练习set args

#include

int main(int argc,char *argv[]){

        char input;

        int i;

 

        if(argc<2){

                printf("insufficient parameter!\n");

                return 1;

        }

 

        input =argv[1][0];

 

        switch(input){

                case 'a':

                printf("You input the charactor 'a'\n");

                case 'b':

                printf("You input the charactor 'b'\n");

                case 'c':

                printf("You input the charactor 'c'\n");

                case 'd':

                printf("You input the charactor 'd'\n");

                default:

                printf("What you input is not belong to the judgement of the

program\n");

        }

 

        return 0;

}

[root@localhost p3.4]# gdb p3.4.debug1

GNU gdb (GDB) Fedora (7.0-3.fc12)

Copyright (C) 2009 Free Software Foundation, Inc.

License GPLv3+: GNU GPL version 3 or later <

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 "i686-redhat-linux-gnu".

For bug reporting instructions, please see:

<

Reading symbols from

/home/linux_book/bianchengjishuxiangjie/p3/p3.8/p3.4/p3.4.debug1...done.

(gdb) l

1       #include

2

3       int main(int argc,char *argv[]){

4               char input;

5               int i;

6

7               if(argc<2){

8                       printf("insufficient parameter!\n");

9                       return 1;

10              }

(gdb) l

11

12              input =argv[1][0];

13

14              switch(input){

15                      case 'a':

16                      printf("You input the charactor 'a'\n");

17                      case 'b':

18                      printf("You input the charactor 'b'\n");

19                      case 'c':

20                      printf("You input the charactor 'c'\n");

(gdb) l

21                      case 'd':

22                      printf("You input the charactor 'd'\n");

23                      default:

24                      printf("What you input is not belong to the judgement of the

program\n");

25              }

26

27              return 0;

28      }

(gdb) show args

Argument list to give program being debugged when it is started is "".

(gdb) set args b

(gdb) show args

Argument list to give program being debugged when it is started is "b".

(gdb) run

Starting program: /home/linux_book/bianchengjishuxiangjie/p3/p3.8/p3.4/p3.4.debug1 b

You input the charactor 'b'

You input the charactor 'c'

You input the charactor 'd'

What you input is not belong to the judgement of the program

 

Program exited normally.

Missing separate debuginfos, use: debuginfo-install glibc-2.11-2.i686

阅读(1591) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~