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

时间:2023-02-01 10:54:49
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$