美文网首页
gbc调试工具的使用

gbc调试工具的使用

作者: mnikn | 来源:发表于2017-03-20 22:20 被阅读56次

    gdb概述

    当程序编译完成后,它可能无法正常运行;或许程序会彻底崩溃;或许只是不能正常地运行某些功能;或许它的输出会被挂起;或许不会提示要求正常的输入。无论在何种情况下,都需要一种工具来跟踪这些问题,gdb(GNU debugger)是一个调试器,是用来帮助程序员寻找程序中的错误的软件。

    gdb主要帮忙用户完成下面4个方面的功能:

    • 启动程序,可以按照用户自定义的要求随心所欲的运行程序。
    • 可让被调试的程序在用户所指定的调试的断点处停住 (断点可以是条件表达式)。
    • 当程序停住时,可以检查此时程序中所发生的事。
    • 动态地改变程序的执行环境。

    gdb的使用

    //test_gdb.c
    #include <stdio.h>
    int func(int n)
    {
        int sum=0,i;
        for(i=0; i<n; i++) {
            sum+=i;
        }
        return sum;
    }
    
    int main(void)
    {
        int i;
        long result = 0;
        for(i=1; i<=100; i++) {
            result += i;
        }
        printf("result[1-100] = %ld \n", result );
        printf("result[1-250] = %d \n", func(250) );
     }
    

    gdb主要调试的是C/C++的程序。要调试C/C++的程序,首先在编译时,必须要把调试信息加到可执行文件中。使用编译器的 -g 参数即可。如果没有-g,将看不见程序的函数名和变量名,代替它们的全是运行时的内存地址。使用gcc生成执行文件:

    gcc -g test_gdb.c -o test_gdb
    

    用gdb来调试:

    gdb test_gdb
    
    GNU gdb (GDB) 7.12
    Copyright (C) 2016 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-apple-darwin16.1.0".
    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 test_gdb...Reading symbols from /Users/zhengzhizhao/Local Documents/learnCode/linux-project/test_gdb.dSYM/Contents/Resources/DWARF/test_gdb...done.
    done.
    
    // l命令相当于list命令,从第一行开始列出源码:
    (gdb) l 
    1   #include <stdio.h>
    2   int func(int n)
    3   {
    4       int sum=0,i;
    5       for(i=0; i<n; i++) {
    6           sum+=i;
    7       }
    8       return sum;
    9   }
    10
    
    // 设置断点,在源程序第16行处。
    (gdb) break 16
    Breakpoint 1 at 0x100000f08: file test_gdb.c, line 16.
    
    (gdb) break func
    Breakpoint 2 at 0x100000ea7: file test_gdb.c, line 4.
    
    // 查看断点 
    (gdb) info break
    Num     Type           Disp Enb Address            What
    1       breakpoint     keep y   0x0000000100000f08 in main at test_gdb.c:16
    2       breakpoint     keep y   0x0000000100000ea7 in func at test_gdb.c:4
    
    // 运行程序,run
    (gdb) r
    Starting program: /Users/zhengzhizhao/Local Documents/learnCode/linux-project/test_gdb 
    
    Breakpoint 1, main () at test.c:16
    16                   long result = 0;
    
    // 单条语句执行,next
    (gdb) n
    17                   for(i=1; i<=100; i++)
    (gdb) n
    19                           result += i;
    (gdb) n
    17                   for(i=1; i<=100; i++)
    (gdb) n
    19                           result += i;
    (gdb) n
    17                   for(i=1; i<=100; i++)
    
    // 继续运行程序,continue
    (gdb) c
    Continuing.
    result[1-100] = 5050  <----------程序输出。
    
    Breakpoint 2, func (n=250) at test.c:5
    5                   int sum=0,i;
    (gdb) n
    6                    for(i=0; i<n; i++)
    // 打印变量i的值,print。
    (gdb) p I    
    $1 = 1107620064
    (gdb) n
    8                           sum+=i;
    (gdb) n
    6                    for(i=0; i<n; i++)
    
    (gdb) p sum
    $2 = 0
    
    // 查看函数堆栈
    (gdb) bt
    #0 func (n=250) at test.c:6
    #1 0x080483b2 in main () at test.c:22
    #2 0x42015574 in __libc_start_main () from /lib/tls/libc.so.6
    (gdb) finish <--------------------- 退出函数。
    Run till exit from #0 func (n=250) at test.c:6
    0x080483b2 in main () at test.c:22
    22   printf("result[1-250] = %d /n", func(250) );
    Value returned is $3 = 31125
    (gdb) c 
    Continuing.
    result[1-250] = 31125
    
    Program exited with code 027. 
    
    // 退出gdb,quit
    (gdb) q 
    

    相关文章

      网友评论

          本文标题:gbc调试工具的使用

          本文链接:https://www.haomeiwen.com/subject/myointtx.html