【C语言】【unix c】GDB调试工具的使用

110 篇文章 1 订阅
GDB调试工具的使用
    1、如何使用gdb调试工具调试程序(man 1 GDB 察看帮助)
        1、在编译链接程序的时候,加上 -g/-gdb参数。编译输出的可执行文件包含调试信息。
            命令: tarena@ubuntu:~/day/day25/tmath$ gcc t_sul.c t_add.c t_math.c -g

        2、使用GDB调试工具对带有调试信息的可执行文件进行调试
            gdb a.out
            命令: tarena@ubuntu:~/day/day25/tmath$ gdb a.out 
            提示: GNU gdb (Ubuntu/Linaro 7.4-2012.02-0ubuntu2) 7.4-2012.02
                Copyright (C) 2012 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 "i686-linux-gnu".
                For bug reporting instructions, please see:
                <http://bugs.launchpad.net/gdb-linaro/>...
                Reading symbols from /home/tarena/day/day25/tmath/a.out...done.
    2、gdb调试命令:
        【l】: list :列出程序的清单
            命令: (gdb) l
            结果: 1   #include <stdio.h>
                2   #include "t_math.h"
                3   
                4   int main() {
                5       int a = 6, b = 2;
                6       
                7       printf("%d*%d=%d\n", a, b, t_mul(a, b));
                8       printf("%d+%d=%d\n", a, b, t_add(a, b));
                9       printf("%d-%d=%d\n", a, b, t_sub(a, b));
                10      printf("%d/%d=%d\n", a, b, t_div(a, b));

        【b 函数名字或者行号】: breakpoint : 设置断点
            命令: (gdb) b main
            提示: Breakpoint 1 at 0x8048429: file t_math.c, line 5.

        【r】: run : 执行程序
            命令: (gdb) r
            提示: Starting program: /home/tarena/day/day25/tmath/a.out 

            提示: Breakpoint(断点) 1, main () at t_math.c:5
            提示: 5       int a = 6, b = 2;  //提示下一行内容

        【p 变量名】: 输出变量的值
            命令: (gdb) p a
            结果: $1 = 6

        【n】: next :  执行下一条,将函数都跳过
            命令: (gdb) n
            结果: 7       printf("%d*%d=%d\n", a, b, t_mul(a, b));

        【s】: step : 下一步,进入函数
            命令: (gdb) s
            结果: t_mul (x=6, y=2) at t_sul.c:4 //进入了函数里面
            提示: 4       return x * y;
            命令: (gdb) s
            结果: 5   }
            命令: (gdb) s
            结果: 6*2=12q】:quit : 退出调试
            命令: (gdb) q
            提示: A debugging session is active.

                    Inferior 1 [process 3440] will be killed.

            提示: Quit anyway? (y or n) y
            结果: tarena@ubuntu:~/day/day25/tmath$ 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘星燎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值