ubuntu 中gdb调试器

目录

         一,调试目标

二,调试的前提条件

三,调试的错误 

四,gdb调试器的相关命令 

1》进入gdb

2》查看源代码 ---- l 

3》设置断点 --- //b 行号

4》查看断点信息 ----- info b 

5》执行程序 ----- r 

6》删除断点 ---- d  断点编号

7》单步调试 ------ /n  或 s 

8》继续执行程序 ------ c 

9》查看变量的值 ----- p 

10》退出 


一,调试目标


    可执行文件

二,调试的前提条件

 
    1》编译时,必须加"-g"
        例如:gcc gdbtest.c -o gdbtest -g
        
    2》调试时,源文件必须在同一个目录中
    

三,调试的错误 


    gdb只能调试可执行文件,所以,当程序有语法错误是不能用gdb调试的
    gdb只能够调试以下错误:
    1》逻辑错误
    2》段错误
        


四,gdb调试器的相关命令 

    应为gdb为字符界面调试器,所以需要掌握调试的命令


1》进入gdb

        root@ubuntu:~/day01_code$ gdb gdbtest
        GNU gdb (Ubuntu 8.1.1-0ubuntu1) 8.1.1
        Copyright (C) 2018 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-linux-gnu".
        Type "show configuration" for configuration details.
        For bug reporting instructions, please see:
        <http://www.gnu.org/software/gdb/bugs/>.
        Find the GDB manual and other documentation resources online at:
        <http://www.gnu.org/software/gdb/documentation/>.
        For help, type "help".
        Type "apropos word" to search for commands related to "word"...
        Reading symbols from gdbtest...done.
        (gdb)

2》查看源代码 ---- l 

(gdb) l 1,30
#include <stdio.h>

void fun(void)
{
    int i;
    for(i = 0 ; i < 7; i++){
    printf("fun:hello world\n");
    }
}

int main(void)
{
    int i;
    fun();
    for(i = 0 ; i < 7; i++){
        printf("main:i = %d\n",i);
    }
    return 0;
}

3》设置断点 --- //b 行号

    (gdb) b 16
    Breakpoint 1 at 0x6bc: file gdbtest.c, line 16.
    (gdb) b 5
    Breakpoint 2 at 0x692: file gdbtest.c, line 5.
    (gdb) 

4》查看断点信息 ----- info b 

        (gdb) info b
        Num     Type           Disp Enb Address            What
        1       breakpoint     keep y   0x00000000000006bc in main at gdbtest.c:16
        2       breakpoint     keep y   0x0000000000000692 in fun at gdbtest.c:5
        (gdb)

5》执行程序 ----- r 

        (gdb) r
        Starting program: /home/root/day01_code/gdbtest 

        Breakpoint 1, main () at gdbtest.c:16
        16        fun();
        (gdb)

6》删除断点 ---- d  断点编号

        16        fun();
        (gdb) n
        fun:hello world
        fun:hello world
        fun:hello world
        fun:hello world
        fun:hello world
        fun:hello world
        fun:hello world
        18        for(i = 0 ; i < 7; i++){
        (gdb) 

7》单步调试 ------ /n  或 s 

        Breakpoint 1, main () at gdbtest.c:16
        16        fun();
        (gdb) n                //如果要执行的语句为函数调用语句,n-表示直接调用该函数
        fun:hello world
        fun:hello world
        fun:hello world
        fun:hello world
        fun:hello world
        fun:hello world
        fun:hello world
        18        for(i = 0 ; i < 7; i++){
        (gdb)


        -----------------------------------------

        Breakpoint 1, main () at gdbtest.c:16
        16        fun();
        (gdb) s                //如果要执行的语句为函数调用语句,s-表示进入被调用的函数体中调试
        fun () at gdbtest.c:6
        6        for(i = 0 ; i < 7; i++){
        (gdb) 
        7            printf("fun:hello world\n");
        (gdb) 

8》继续执行程序 ------ c 

        (gdb) c
        Continuing.
        main:i = 2
        main:i = 3
        main:i = 4
        main:i = 5
        main:i = 6
        [Inferior 1 (process 11507) exited normally]
        (gdb) 
        

9》查看变量的值 ----- p 

        main:i = 1
        18        for(i = 0 ; i < 7; i++){
        (gdb) p i                //查看变量i的值
        $2 = 1
        (gdb)

10》退出 

        (gdb) q
        root@ubuntu:~/day01_code$

  • 2
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值